Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-fno-plt disregarded when compiling *.zig #22254

Open
xBZZZZ opened this issue Dec 16, 2024 · 1 comment
Open

-fno-plt disregarded when compiling *.zig #22254

xBZZZZ opened this issue Dec 16, 2024 · 1 comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Milestone

Comments

@xBZZZZ
Copy link

xBZZZZ commented Dec 16, 2024

Zig Version

0.14.0-dev.2487+af89bb05d

Steps to Reproduce and Observed Behavior

  1. create file plt_test.c with content:
    int imported_function(void);
    int exported_function(void){
    return imported_function()+1;
    }
  2. run
    zig cc -target x86_64-linux-gnu -nostdlib -shared -Os -fno-plt -o plt_test_c.so plt_test.c
  3. run
    eu-objdump --disassemble plt_test_c.so
    it outputs
    plt_test_c.so: elf64-elf_x86_64
    
    Disassembly of section .text:
    
        1314:    50                       push    %rax
        1315:    ff 15 c5 10 00 00        callq   *0x10c5(%rip)              # 0x23e0
        131b:    ff c0                    inc     %eax
        131d:    59                       pop     %rcx
        131e:    c3                       retq
    no .plt section because -fno-plt
  4. create file plt_test.zig with content:
    extern fn imported_function() callconv(.C) i32;
    export fn exported_function() callconv(.C) i32{
    return imported_function()+1;
    }
  5. run
    zig cc -target x86_64-linux-gnu -nostdlib -shared -Os -fno-plt -o plt_test_zig.so plt_test.zig
  6. run
    eu-objdump --disassemble plt_test_zig.so
    it outputs
    plt_test_zig.so: elf64-elf_x86_64
    
    Disassembly of section .text:
    
        1314:    50                       push    %rax
        1315:    e8 16 00 00 00           callq   0x1330
        131a:    ff c0                    inc     %eax
        131c:    59                       pop     %rcx
        131d:    c3                       retq
    Disassembly of section .plt:
    
        1320:    ff 35 f2 10 00 00        pushq   0x10f2(%rip)               # 0x2418
        1326:    ff 25 f4 10 00 00        jmpq    *0x10f4(%rip)              # 0x2420
        132c:    0f 1f 40 00              nopl    0x0(%rax)
        1330:    ff 25 f2 10 00 00        jmpq    *0x10f2(%rip)              # 0x2428
        1336:    68 00 00 00 00           pushq   $0x0
        133b:    e9 e0 ff ff ff           jmpq    0x1320
    .plt section exists, -fno-plt disregarded

Expected Behavior

zig cc -target x86_64-linux-gnu -nostdlib -shared -Os -fno-plt -o plt_test_zig.so plt_test.zig
doesn't generate .plt section and
eu-objdump --disassemble plt_test_zig.so
outputs
plt_test_zig.so: elf64-elf_x86_64

Disassembly of section .text:

    1314:    50                       push    %rax
    1315:    ff 15 c5 10 00 00        callq   *0x10c5(%rip)              # 0x23e0
    131b:    ff c0                    inc     %eax
    131d:    59                       pop     %rcx
    131e:    c3                       retq
@xBZZZZ xBZZZZ added the bug Observed behavior contradicts documented or intended behavior label Dec 16, 2024
@alexrp
Copy link
Member

alexrp commented Dec 16, 2024

This happens because we make no effort to handle this flag in the Zig driver; we just pass it through to Clang.

To support this option, we need to set this module flag: https://github.com/llvm/llvm-project/blob/9ee454a57c061e47223e079cdc64d315580367ed/llvm/lib/IR/Module.cpp#L717-L719

@alexrp alexrp added enhancement Solving this issue will likely involve adding new logic or components to the codebase. backend-llvm The LLVM backend outputs an LLVM IR Module. and removed bug Observed behavior contradicts documented or intended behavior labels Dec 16, 2024
@alexrp alexrp added this to the 0.15.0 milestone Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Projects
None yet
Development

No branches or pull requests

2 participants