authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-06 15:12:43+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-06 15:12:43+02:00
loga2b5a464e65955f09e3034830ca275ae84ebddea
tree0b8725ffdf09ea3bdb7f380688e7e2c8e18b3c91
parent93120a81fee404e3ae90c5ea6d0ad2e71037bee0

elf: set atom names


1 files changed, 68 insertions(+), 70 deletions(-)

src/link/Elf.zig+68-70
......@@ -971,7 +971,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
971971 // TODO This linker code currently assumes there is only 1 compilation unit and it
972972 // corresponds to the Zig source code.
973973 const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented;
974 _ = module;
975974
976975 self.linker_defined_index = blk: {
977976 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));
......@@ -979,82 +978,77 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
979978 break :blk index;
980979 };
981980
982 std.debug.print("{}\n", .{self.dumpState()});
983 return error.FlushFailure;
984
985 // if (self.lazy_syms.getPtr(.none)) |metadata| {
986 // // Most lazy symbols can be updated on first use, but
987 // // anyerror needs to wait for everything to be flushed.
988 // if (metadata.text_state != .unused) self.updateLazySymbolAtom(
989 // link.File.LazySymbol.initDecl(.code, null, module),
990 // metadata.text_atom,
991 // self.text_section_index.?,
992 // ) catch |err| return switch (err) {
993 // error.CodegenFail => error.FlushFailure,
994 // else => |e| e,
995 // };
996 // if (metadata.rodata_state != .unused) self.updateLazySymbolAtom(
997 // link.File.LazySymbol.initDecl(.const_data, null, module),
998 // metadata.rodata_atom,
999 // self.rodata_section_index.?,
1000 // ) catch |err| return switch (err) {
1001 // error.CodegenFail => error.FlushFailure,
1002 // else => |e| e,
1003 // };
1004 // }
1005 // for (self.lazy_syms.values()) |*metadata| {
1006 // if (metadata.text_state != .unused) metadata.text_state = .flushed;
1007 // if (metadata.rodata_state != .unused) metadata.rodata_state = .flushed;
1008 // }
981 if (self.lazy_syms.getPtr(.none)) |metadata| {
982 // Most lazy symbols can be updated on first use, but
983 // anyerror needs to wait for everything to be flushed.
984 if (metadata.text_state != .unused) self.updateLazySymbol(
985 link.File.LazySymbol.initDecl(.code, null, module),
986 metadata.text_symbol_index,
987 ) catch |err| return switch (err) {
988 error.CodegenFail => error.FlushFailure,
989 else => |e| e,
990 };
991 if (metadata.rodata_state != .unused) self.updateLazySymbol(
992 link.File.LazySymbol.initDecl(.const_data, null, module),
993 metadata.rodata_symbol_index,
994 ) catch |err| return switch (err) {
995 error.CodegenFail => error.FlushFailure,
996 else => |e| e,
997 };
998 }
999 for (self.lazy_syms.values()) |*metadata| {
1000 if (metadata.text_state != .unused) metadata.text_state = .flushed;
1001 if (metadata.rodata_state != .unused) metadata.rodata_state = .flushed;
1002 }
10091003
1010 // const target_endian = self.base.options.target.cpu.arch.endian();
1011 // const foreign_endian = target_endian != builtin.cpu.arch.endian();
1004 const target_endian = self.base.options.target.cpu.arch.endian();
1005 const foreign_endian = target_endian != builtin.cpu.arch.endian();
1006 _ = foreign_endian;
10121007
1013 // if (self.dwarf) |*dw| {
1014 // try dw.flushModule(module);
1015 // }
1008 if (self.dwarf) |*dw| {
1009 try dw.flushModule(module);
1010 }
10161011
1017 // {
1018 // var it = self.relocs.iterator();
1019 // while (it.next()) |entry| {
1020 // const atom_index = entry.key_ptr.*;
1021 // const relocs = entry.value_ptr.*;
1022 // const atom_ptr = self.atom(atom_index);
1023 // const source_sym = atom_ptr.symbol(self);
1024 // const source_shdr = self.sections.items(.shdr)[source_sym.st_shndx];
1025
1026 // log.debug("relocating '{?s}'", .{self.strtab.get(source_sym.st_name)});
1027
1028 // for (relocs.items) |*reloc| {
1029 // const target_sym = self.locals.items[reloc.target];
1030 // const target_vaddr = target_sym.st_value + reloc.addend;
1031
1032 // if (target_vaddr == reloc.prev_vaddr) continue;
1033
1034 // const section_offset = (source_sym.st_value + reloc.offset) - source_shdr.sh_addr;
1035 // const file_offset = source_shdr.sh_offset + section_offset;
1036
1037 // log.debug(" ({x}: [() => 0x{x}] ({?s}))", .{
1038 // reloc.offset,
1039 // target_vaddr,
1040 // self.strtab.get(target_sym.st_name),
1041 // });
1042
1043 // switch (self.ptr_width) {
1044 // .p32 => try self.base.file.?.pwriteAll(mem.asBytes(&@as(u32, @intCast(target_vaddr))), file_offset),
1045 // .p64 => try self.base.file.?.pwriteAll(mem.asBytes(&target_vaddr), file_offset),
1046 // }
1012 {
1013 var it = self.relocs.iterator();
1014 while (it.next()) |entry| {
1015 const atom_index = entry.key_ptr.*;
1016 const relocs = entry.value_ptr.*;
1017 const atom_ptr = self.atom(atom_index);
1018 const source_shdr = self.sections.items(.shdr)[atom_ptr.output_section_index];
10471019
1048 // reloc.prev_vaddr = target_vaddr;
1049 // }
1050 // }
1051 // }
1020 log.debug("relocating '{s}'", .{atom_ptr.name(self)});
1021
1022 for (relocs.items) |*reloc| {
1023 const target_sym = self.symbol(reloc.target);
1024 const target_vaddr = target_sym.value + reloc.addend;
1025
1026 if (target_vaddr == reloc.prev_vaddr) continue;
1027
1028 const section_offset = (atom_ptr.value + reloc.offset) - source_shdr.sh_addr;
1029 const file_offset = source_shdr.sh_offset + section_offset;
1030
1031 log.debug(" ({x}: [() => 0x{x}] ({s}))", .{
1032 reloc.offset,
1033 target_vaddr,
1034 target_sym.name(self),
1035 });
1036
1037 switch (self.ptr_width) {
1038 .p32 => try self.base.file.?.pwriteAll(mem.asBytes(&@as(u32, @intCast(target_vaddr))), file_offset),
1039 .p64 => try self.base.file.?.pwriteAll(mem.asBytes(&target_vaddr), file_offset),
1040 }
1041
1042 reloc.prev_vaddr = target_vaddr;
1043 }
1044 }
1045 }
10521046
10531047 // try self.writeSymbols();
10541048
1055 // if (build_options.enable_logging) {
1056 // self.logSymtab();
1057 // }
1049 if (build_options.enable_logging) {
1050 state_log.debug("{}", .{self.dumpState()});
1051 }
10581052
10591053 // if (self.dwarf) |*dw| {
10601054 // if (self.debug_abbrev_section_dirty) {
......@@ -2211,6 +2205,7 @@ fn updateDeclCode(
22112205 const shdr_index = sym.output_section_index;
22122206
22132207 sym.name_offset = try self.strtab.insert(gpa, decl_name);
2208 atom_ptr.name_offset = sym.name_offset;
22142209 esym.st_name = sym.name_offset;
22152210 esym.st_info |= stt_bits;
22162211 esym.st_size = code.len;
......@@ -2466,6 +2461,7 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol.
24662461 local_esym.st_info |= elf.STT_OBJECT;
24672462 local_esym.st_size = code.len;
24682463 const atom_ptr = local_sym.atom(self).?;
2464 atom_ptr.name_offset = name_str_index;
24692465 atom_ptr.alignment = math.log2_int(u64, required_alignment);
24702466 atom_ptr.size = code.len;
24712467
......@@ -2540,6 +2536,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
25402536 local_esym.st_info |= elf.STT_OBJECT;
25412537 local_esym.st_size = code.len;
25422538 const atom_ptr = local_sym.atom(self).?;
2539 atom_ptr.name_offset = name_str_index;
25432540 atom_ptr.alignment = math.log2_int(u64, required_alignment);
25442541 atom_ptr.size = code.len;
25452542
......@@ -3290,6 +3287,7 @@ const assert = std.debug.assert;
32903287const elf = std.elf;
32913288const fs = std.fs;
32923289const log = std.log.scoped(.link);
3290const state_log = std.log.scoped(.link_state);
32933291const math = std.math;
32943292const mem = std.mem;
32953293