elf: LLVM emits relocs to undef local symbols - color me surprised!

This commit is contained in:
Jakub Konka 2023-11-08 15:10:28 +01:00
parent d8b1ef9430
commit a16e6706b3
4 changed files with 7 additions and 7 deletions

View File

@ -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;
},
}

View File

@ -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,

View File

@ -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).?;

View File

@ -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);