DoctorWkt / acwj

A Compiler Writing Journey
GNU General Public License v3.0
10.55k stars 1.02k forks source link

unsupported symbol modifier in branch relocation: “call printf@PLT” #10

Closed MichaelScofield closed 4 years ago

MichaelScofield commented 4 years ago

Firstly, thank you for sharing this great compiler writing jounery, I'm rather interested!

I'm in chapter 4 (generating assembly code), and trying to follow the code.

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
  3. The souce codes are compiled with cc -o comp1 -g cg.c expr.c gen.c main.c scan.c tree.c
MichaelScofield commented 4 years ago

Nevermind, change to call _printf works.

DoctorWkt commented 4 years ago

Thanks Michael. I'd be pretty sure the macOS compiler and environment is different enough from Ubuntu/Debian Linux that some changes to the code generator will be required.