Commit a16e6706b3

Jakub Konka <kubkon@jakubkonka.com>
2023-11-08 15:10:28
elf: LLVM emits relocs to undef local symbols - color me surprised!
1 parent d8b1ef9
Changed files (4)
src/link/Elf/Atom.zig
@@ -319,7 +319,7 @@ pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.El
                 r_sym = elf_file.sectionSymbolOutputSymtabIndex(target.outputShndx().?);
             },
             else => {
-                r_sym = target.outputSymtabIndex(elf_file);
+                r_sym = target.outputSymtabIndex(elf_file) orelse 0;
             },
         }
 
src/link/Elf/file.zig
@@ -165,8 +165,7 @@ pub const File = union(enum) {
     pub fn writeSymtab(file: File, elf_file: *Elf) void {
         for (file.locals()) |local_index| {
             const local = elf_file.symbol(local_index);
-            if (!local.flags.output_symtab) continue;
-            const idx = local.outputSymtabIndex(elf_file);
+            const idx = local.outputSymtabIndex(elf_file) orelse continue;
             const out_sym = &elf_file.symtab.items[idx];
             out_sym.st_name = @intCast(elf_file.strtab.items.len);
             elf_file.strtab.appendSliceAssumeCapacity(local.name(elf_file));
@@ -178,11 +177,10 @@ pub const File = union(enum) {
             const global = elf_file.symbol(global_index);
             const file_ptr = global.file(elf_file) orelse continue;
             if (file_ptr.index() != file.index()) continue;
-            if (!global.flags.output_symtab) continue;
+            const idx = global.outputSymtabIndex(elf_file) orelse continue;
             const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
             elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
             elf_file.strtab.appendAssumeCapacity(0);
-            const idx = global.outputSymtabIndex(elf_file);
             const out_sym = &elf_file.symtab.items[idx];
             out_sym.st_name = st_name;
             global.setOutputSym(elf_file, out_sym);
src/link/Elf/Symbol.zig
@@ -107,8 +107,8 @@ pub fn address(symbol: Symbol, opts: struct { plt: bool = true }, elf_file: *Elf
     return symbol.value;
 }
 
-pub fn outputSymtabIndex(symbol: Symbol, elf_file: *Elf) u32 {
-    assert(symbol.flags.output_symtab);
+pub fn outputSymtabIndex(symbol: Symbol, elf_file: *Elf) ?u32 {
+    if (!symbol.flags.output_symtab) return null;
     const file_ptr = symbol.file(elf_file).?;
     const symtab_ctx = switch (file_ptr) {
         inline else => |x| x.output_symtab_ctx,
src/link/Elf/ZigObject.zig
@@ -538,6 +538,8 @@ pub fn addAtomsToRelaSections(self: ZigObject, elf_file: *Elf) !void {
         if (!atom.flags.alive) continue;
         _ = atom.relocsShndx() orelse continue;
         const out_shndx = atom.outputShndx().?;
+        const out_shdr = elf_file.shdrs.items[out_shndx];
+        if (out_shdr.sh_type == elf.SHT_NOBITS) continue;
 
         const gpa = elf_file.base.allocator;
         const sec = elf_file.output_rela_sections.getPtr(out_shndx).?;