zig icon indicating copy to clipboard operation
zig copied to clipboard

support -mrelax-relocations=no

Open garrettlennoxbeck opened this issue 2 years ago • 0 comments

Here is a snippet from LLVM showing how this is used:

https://github.com/llvm/llvm-project/blob/8b8f2ef06e341ef634f85fa01800f4e441cacd91/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp#L207-L219

Specifically for Zig, when I'm building a relocatable object to be linked against an older target, I need to ensure that the R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX aren't present in my final binary, or linking will fail because the older target won't recognize the link type. -mrelax-relocations=no forces LLVM to not emit these relocation types, instead emitting the backward-compatible R_X86_64_GOTPCREL type.

Here is a demonstration of this:

[jrz@jrz reloc_test]$ cat main.zig 
const std = @import("std");

extern const my_extern: i32;

pub fn main() !void {
    std.debug.print("{d}\n", .{my_extern});
}
[jrz@jrz reloc_test]$ zig build-obj main.zig 
[jrz@jrz reloc_test]$ readelf -Wr main.o | grep GOT
000000000000051b  000005450000002a R_X86_64_REX_GOTPCRELX 0000000000000000 my_extern - 4
000000000003c2e7  0000054a0000002a R_X86_64_REX_GOTPCRELX 0000000000000000 _DYNAMIC - 4
[jrz@jrz reloc_test]$ zig build-obj -mrelax-relocations=no main.zig 
[jrz@jrz reloc_test]$ readelf -Wr main.o | grep GOT
000000000000051b  0000054500000009 R_X86_64_GOTPCREL      0000000000000000 my_extern - 4
000000000003c2e7  0000054a00000009 R_X86_64_GOTPCREL      0000000000000000 _DYNAMIC - 4

In addition, this is exposed to the build system via the Compile step's new relax_elf_relocations bool.

garrettlennoxbeck avatar Dec 20 '23 23:12 garrettlennoxbeck