Undefined symbols for architecture x86_64: "printf" ld: symbol(s) not found for interested architecture x86_64

Asked 2 years ago, Updated 2 years ago, 160 views

Nasm to count from 1 to 100 - Qiita

Using the above article as a reference, I ran the following code and received an error:When I checked it, it seems that I need to set the option or not, but I am not sure, thank you for your cooperation

get the external printf;printf function

section.data
    fmt: db "iteration: %d", 10,0; format used in printf

section.text
    global_main
_main:
    movrdx, 1;initial value
    movrcx, 10

_loop:
    push rcx;rcx evacuation
    push rdx;rdx evacuation

    movrdi, fmt;formatting
    movrsi,rdx;counting value

    call printf;call printf
    ; Register value disappears when calling printf

    Get pop rdx;rdx
    Get pop rcx;rcx

    add rdx, 1; add 1 to rdx

    cmprcx, 0
    loop_loop;loopback

fin:
    movrax, 0x2000001
    movrdi, 0
    syscall

Error

$ld-macosx_version_min 10.14-lSystem-op100p100.o
    Undefined symbols for architecture x86_64:
     "printf", referenced from:
          _loop in p100.o
      ld —symbol(s) not found for interfered architecture x86_64

Also,

movrsi,rdx

I don't know why

x86

2022-09-29 20:26

1 Answers

I think the reason for Undefined symbols is that ld has failed to find libSystem.dynlib (speculation: most likely wrong). In short, the option -L for ld is not enough.I don't know exactly what to do because I don't have a Mac in Oiran.

movrsi,rdx is required because the x86-64 Application Binary Interface (ABI) specification requires it.If I explain it in detail, it will be a book. https://en.wikipedia.org/wiki/X86_calling_conventions
See from the System VAMD64 ABI in .

Your program uses rcx and rdx as register variables, but the ABI specification stipulates that you use rcx and rdx as arguments when calling a function (printf in this example).

I'd like to say that

The presentation program does not work properly because it does not meet the x86-64 ABI register storage rules and stack specifications.It's a coincidence if it's working now (it's just that the fault hasn't come to light).
Why should I push rcx?
However, it is a coincidence that it works because it does not meet the register preservation rules and stack specifications (I thought I should point it out, but I abbreviated it because it is not the main idea).

Modern ABI is designed with the assumption that compilers are used, and very little is expected to handwrite assemblers.The function (main in this example) is required to implement code that meets the specifications determined at its entrance and exit.You can remember to implement it with a compiler, but it's very troublesome to write an assembler by handwriting it by hand.Assembler handwriting will not meet implementation costs unless you are familiar with the rules and CPU specifications.

x86-64 If you're not a professional, it's a complete detour to learn the handwriting of the assembler, and it's expensive to learn, but you don't get much.If Oira's junior says he wants to learn Asembra handwriting, I'll let him stop it at least once (if he already has some knowledge and interest, I'll let him go).Take your time to consider if it's really necessary.


2022-09-29 20:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.