From a16e6706b337e2c7247238d08f205594d777a63a Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 8 Nov 2023 15:10:28 +0100 Subject: [PATCH] elf: LLVM emits relocs to undef local symbols - color me surprised! --- src/link/Elf/Atom.zig | 2 +- src/link/Elf/Symbol.zig | 4 ++-- src/link/Elf/ZigObject.zig | 2 ++ src/link/Elf/file.zig | 6 ++---- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/link/Elf/Atom.zig b/src/link/Elf/Atom.zig index 0b1de1feeb..ec12f17721 100644 --- a/src/link/Elf/Atom.zig +++ b/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; }, } diff --git a/src/link/Elf/Symbol.zig b/src/link/Elf/Symbol.zig index ed3260b77a..a2ffc62014 100644 --- a/src/link/Elf/Symbol.zig +++ b/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, diff --git a/src/link/Elf/ZigObject.zig b/src/link/Elf/ZigObject.zig index 798ced73a5..03b2572549 100644 --- a/src/link/Elf/ZigObject.zig +++ b/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).?; diff --git a/src/link/Elf/file.zig b/src/link/Elf/file.zig index 357e1ee12d..29a76daad9 100644 --- a/src/link/Elf/file.zig +++ b/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);