0

I'm following a C compiler writing journey to generate assembly code for evaluating a binary expression value.

Here's the out assembly codes (saved in file "out.s"):

    .text
LC0:
    .string "%d\n"
_printint:
    pushq   %rbp
    movq    %rsp, %rbp
    subq    $16, %rsp
    movl    %edi, -4(%rbp)
    movl    -4(%rbp), %eax
    movl    %eax, %esi
    leaq    LC0(%rip), %rdi
    movl    $0, %eax
    call    printf@PLT
    nop
    leave
    ret

    .globl  _main
_main:
    pushq   %rbp
    movq    %rsp, %rbp
    movq    $2, %r8
    movq    $3, %r9
    movq    $5, %r10
    imulq   %r9, %r10
    addq    %r8, %r10
    movq    $8, %r8
    movq    $3, %r9
    movq    %r8,%rax
    cqo
    idivq   %r9
    movq    %rax,%r8
    subq    %r8, %r10
    movq    %r10, %rdi
    call    _printint
    movl    $0, %eax
    popq    %rbp
    ret

When running it with

cc -o out out.s

, it complained with

out.s:13:2: error: unsupported symbol modifier in branch relocation
 call printf@PLT
 ^

How is the error happened and how to fix it?

Thanks in advance!

PS:

  1. I am using macOS Catalina 10.15.2
  2. cc version is
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin19.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
  1. The souce codes are compiled with cc -o comp1 -g cg.c expr.c gen.c main.c scan.c tree.c
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
bayinamy
  • 487
  • 4
  • 14
  • 1
    Do MacOS Mach-O objects even use a PLT? Or does it just need to be lower case? That doesn't look like asm for MacOS; on MacOS C symbol names are prefixed with a leading `_` in asm, like `_printf`. I think that `.s` file looks like it's for Linux. Same x86-64 System V calling convention; different name-mangling convention. – Peter Cordes Jan 20 '20 at 07:07
  • `call _printf` works. Thanks @PeterCordes – bayinamy Jan 20 '20 at 08:17
  • 1
    @PeterCordes MacOS & iOS don't use PLT. https://stackoverflow.com/questions/18260207/weak-symbol-aliases-on-os-x-similar-to-those-on-linux-or-a-closest-equivalent – Kamil.S Jan 20 '20 at 08:41
  • @Kamil.S: the answer on that question doesn't say that. Defaulting to non-interposable symbols doesn't imply no PLT. I assume your statement is true (and would be the obvious explanation for why this doesn't assemble on Mach-O, instead of just failing to link), but your link doesn't directly back that up, unless I missed something in comments or a side-note in the question or answer. – Peter Cordes Jan 20 '20 at 08:52
  • @PeterCordes I also found this https://stackoverflow.com/a/6447676/5329717, but no Apple doc reference to back this up unfortunately. – Kamil.S Jan 20 '20 at 09:34

1 Answers1

0

call _printf works. Thanks @PeterCordes.

Wondering the error prompt by cc though.

bayinamy
  • 487
  • 4
  • 14