authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-30 11:34:21+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-04 13:34:25+02:00
logda60159d85f2b23e35b62f0626163d798413916f
treea539ff934e8bb2a472eb3b04797ac4dfa611363a
parentacb91f4b30e6cbb14bb81406ca6ca71241dc3a98

elf+dwarf: refer sections via section symbols


3 files changed, 100 insertions(+), 106 deletions(-)

src/link/Dwarf.zig+48-37
......@@ -375,12 +375,17 @@ pub const Section = struct {
375375 fn resize(sec: *Section, dwarf: *Dwarf, len: u64) UpdateError!void {
376376 if (len <= sec.len) return;
377377 if (dwarf.bin_file.cast(.elf)) |elf_file| {
378 const zo = elf_file.zigObjectPtr().?;
379 const atom = zo.symbol(sec.index).atom(elf_file).?;
380 const shndx = atom.output_section_index;
378381 if (sec == &dwarf.debug_frame.section)
379 try elf_file.growAllocSection(sec.index, len)
382 try elf_file.growAllocSection(shndx, len)
380383 else
381 try elf_file.growNonAllocSection(sec.index, len, @intCast(sec.alignment.toByteUnits().?), true);
382 const shdr = &elf_file.sections.items(.shdr)[sec.index];
383 sec.off = shdr.sh_offset;
384 try elf_file.growNonAllocSection(shndx, len, @intCast(sec.alignment.toByteUnits().?), true);
385 const shdr = elf_file.sections.items(.shdr)[shndx];
386 atom.size = shdr.sh_size;
387 atom.alignment = InternPool.Alignment.fromNonzeroByteUnits(shdr.sh_addralign);
388 sec.off = shdr.sh_offset + @as(u64, @intCast(atom.value));
384389 sec.len = shdr.sh_size;
385390 } else if (dwarf.bin_file.cast(.macho)) |macho_file| {
386391 const header = if (macho_file.d_sym) |*d_sym| header: {
......@@ -402,7 +407,11 @@ pub const Section = struct {
402407 sec.off += len;
403408 sec.len -= len;
404409 if (dwarf.bin_file.cast(.elf)) |elf_file| {
405 const shdr = &elf_file.sections.items(.shdr)[sec.index];
410 const zo = elf_file.zigObjectPtr().?;
411 const atom = zo.symbol(sec.index).atom(elf_file).?;
412 const shndx = atom.output_section_index;
413 const shdr = &elf_file.sections.items(.shdr)[shndx];
414 atom.size = sec.len;
406415 shdr.sh_offset = sec.off;
407416 shdr.sh_size = sec.len;
408417 } else if (dwarf.bin_file.cast(.macho)) |macho_file| {
......@@ -891,9 +900,11 @@ const Entry = struct {
891900 if (std.debug.runtime_safety) {
892901 log.err("missing {} from {s}", .{
893902 @as(Entry.Index, @enumFromInt(entry - unit.entries.items.ptr)),
894 std.mem.sliceTo(if (dwarf.bin_file.cast(.elf)) |elf_file|
895 elf_file.shstrtab.items[elf_file.sections.items(.shdr)[sec.index].sh_name..]
896 else if (dwarf.bin_file.cast(.macho)) |macho_file|
903 std.mem.sliceTo(if (dwarf.bin_file.cast(.elf)) |elf_file| sh_name: {
904 const zo = elf_file.zigObjectPtr().?;
905 const shndx = zo.symbol(sec.index).atom(elf_file).?.output_section_index;
906 break :sh_name elf_file.shstrtab.items[elf_file.sections.items(.shdr)[shndx].sh_name..];
907 } else if (dwarf.bin_file.cast(.macho)) |macho_file|
897908 if (macho_file.d_sym) |*d_sym|
898909 &d_sym.sections.items[sec.index].segname
899910 else
......@@ -961,7 +972,8 @@ const Entry = struct {
961972 .none, .debug_frame => {},
962973 .eh_frame => return if (dwarf.bin_file.cast(.elf)) |elf_file| {
963974 const zo = elf_file.zigObjectPtr().?;
964 const entry_addr: i64 = @intCast(entry_off - sec.off + elf_file.shdrs.items[sec.index].sh_addr);
975 const shndx = zo.symbol(sec.index).atom(elf_file).?.output_section_index;
976 const entry_addr: i64 = @intCast(entry_off - sec.off + elf_file.shdrs.items[shndx].sh_addr);
965977 for (entry.external_relocs.items) |reloc| {
966978 const symbol = zo.symbol(reloc.target_sym);
967979 try dwarf.resolveReloc(
......@@ -1877,34 +1889,7 @@ pub fn init(lf: *link.File, format: DW.Format) Dwarf {
18771889}
18781890
18791891pub fn reloadSectionMetadata(dwarf: *Dwarf) void {
1880 if (dwarf.bin_file.cast(.elf)) |elf_file| {
1881 for ([_]*Section{
1882 &dwarf.debug_abbrev.section,
1883 &dwarf.debug_aranges.section,
1884 &dwarf.debug_frame.section,
1885 &dwarf.debug_info.section,
1886 &dwarf.debug_line.section,
1887 &dwarf.debug_line_str.section,
1888 &dwarf.debug_loclists.section,
1889 &dwarf.debug_rnglists.section,
1890 &dwarf.debug_str.section,
1891 }, [_]u32{
1892 elf_file.debug_abbrev_section_index.?,
1893 elf_file.debug_aranges_section_index.?,
1894 elf_file.eh_frame_section_index.?,
1895 elf_file.debug_info_section_index.?,
1896 elf_file.debug_line_section_index.?,
1897 elf_file.debug_line_str_section_index.?,
1898 elf_file.debug_loclists_section_index.?,
1899 elf_file.debug_rnglists_section_index.?,
1900 elf_file.debug_str_section_index.?,
1901 }) |sec, section_index| {
1902 const shdr = &elf_file.sections.items(.shdr)[section_index];
1903 sec.index = section_index;
1904 sec.off = shdr.sh_offset;
1905 sec.len = shdr.sh_size;
1906 }
1907 } else if (dwarf.bin_file.cast(.macho)) |macho_file| {
1892 if (dwarf.bin_file.cast(.macho)) |macho_file| {
19081893 if (macho_file.d_sym) |*d_sym| {
19091894 for ([_]*Section{
19101895 &dwarf.debug_abbrev.section,
......@@ -1960,6 +1945,32 @@ pub fn reloadSectionMetadata(dwarf: *Dwarf) void {
19601945}
19611946
19621947pub fn initMetadata(dwarf: *Dwarf) UpdateError!void {
1948 if (dwarf.bin_file.cast(.elf)) |elf_file| {
1949 const zo = elf_file.zigObjectPtr().?;
1950 for ([_]*Section{
1951 &dwarf.debug_abbrev.section,
1952 &dwarf.debug_aranges.section,
1953 &dwarf.debug_frame.section,
1954 &dwarf.debug_info.section,
1955 &dwarf.debug_line.section,
1956 &dwarf.debug_line_str.section,
1957 &dwarf.debug_loclists.section,
1958 &dwarf.debug_rnglists.section,
1959 &dwarf.debug_str.section,
1960 }, [_]u32{
1961 zo.debug_abbrev_index.?,
1962 zo.debug_aranges_index.?,
1963 zo.eh_frame_index.?,
1964 zo.debug_info_index.?,
1965 zo.debug_line_index.?,
1966 zo.debug_line_str_index.?,
1967 zo.debug_loclists_index.?,
1968 zo.debug_rnglists_index.?,
1969 zo.debug_str_index.?,
1970 }) |sec, sym_index| {
1971 sec.index = sym_index;
1972 }
1973 }
19631974 dwarf.reloadSectionMetadata();
19641975
19651976 dwarf.debug_abbrev.section.pad_to_ideal = false;
src/link/Elf.zig+25-36
......@@ -114,15 +114,6 @@ rela_plt: std.ArrayListUnmanaged(elf.Elf64_Rela) = .{},
114114/// Applies only to a relocatable.
115115comdat_group_sections: std.ArrayListUnmanaged(ComdatGroupSection) = .{},
116116
117debug_info_section_index: ?u32 = null,
118debug_abbrev_section_index: ?u32 = null,
119debug_str_section_index: ?u32 = null,
120debug_aranges_section_index: ?u32 = null,
121debug_line_section_index: ?u32 = null,
122debug_line_str_section_index: ?u32 = null,
123debug_loclists_section_index: ?u32 = null,
124debug_rnglists_section_index: ?u32 = null,
125
126117copy_rel_section_index: ?u32 = null,
127118dynamic_section_index: ?u32 = null,
128119dynstrtab_section_index: ?u32 = null,
......@@ -636,24 +627,31 @@ pub fn growNonAllocSection(
636627}
637628
638629pub fn markDirty(self: *Elf, shdr_index: u32) void {
639 const zig_object = self.zigObjectPtr().?;
640 if (zig_object.dwarf) |_| {
641 if (self.debug_info_section_index.? == shdr_index) {
642 zig_object.debug_info_section_dirty = true;
643 } else if (self.debug_abbrev_section_index.? == shdr_index) {
644 zig_object.debug_abbrev_section_dirty = true;
645 } else if (self.debug_str_section_index.? == shdr_index) {
646 zig_object.debug_str_section_dirty = true;
647 } else if (self.debug_aranges_section_index.? == shdr_index) {
648 zig_object.debug_aranges_section_dirty = true;
649 } else if (self.debug_line_section_index.? == shdr_index) {
650 zig_object.debug_line_section_dirty = true;
651 } else if (self.debug_line_str_section_index.? == shdr_index) {
652 zig_object.debug_line_str_section_dirty = true;
653 } else if (self.debug_loclists_section_index.? == shdr_index) {
654 zig_object.debug_loclists_section_dirty = true;
655 } else if (self.debug_rnglists_section_index.? == shdr_index) {
656 zig_object.debug_rnglists_section_dirty = true;
630 if (self.zigObjectPtr()) |zo| {
631 for ([_]?Symbol.Index{
632 zo.debug_info_index,
633 zo.debug_abbrev_index,
634 zo.debug_aranges_index,
635 zo.debug_str_index,
636 zo.debug_line_index,
637 zo.debug_line_str_index,
638 zo.debug_loclists_index,
639 zo.debug_rnglists_index,
640 }, [_]*bool{
641 &zo.debug_info_section_dirty,
642 &zo.debug_abbrev_section_dirty,
643 &zo.debug_aranges_section_dirty,
644 &zo.debug_str_section_dirty,
645 &zo.debug_line_section_dirty,
646 &zo.debug_line_str_section_dirty,
647 &zo.debug_loclists_section_dirty,
648 &zo.debug_rnglists_section_dirty,
649 }) |maybe_sym_index, dirty| {
650 const sym_index = maybe_sym_index orelse continue;
651 if (zo.symbol(sym_index).atom(self).?.output_section_index == shdr_index) {
652 dirty.* = true;
653 break;
654 }
657655 }
658656 }
659657}
......@@ -3473,14 +3471,6 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {
34733471 &self.copy_rel_section_index,
34743472 &self.versym_section_index,
34753473 &self.verneed_section_index,
3476 &self.debug_info_section_index,
3477 &self.debug_abbrev_section_index,
3478 &self.debug_str_section_index,
3479 &self.debug_aranges_section_index,
3480 &self.debug_line_section_index,
3481 &self.debug_line_str_section_index,
3482 &self.debug_loclists_section_index,
3483 &self.debug_rnglists_section_index,
34843474 }) |maybe_index| {
34853475 if (maybe_index.*) |*index| {
34863476 index.* = backlinks[index.*];
......@@ -3505,7 +3495,6 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {
35053495 const atom_ptr = zo.atom(atom_index) orelse continue;
35063496 atom_ptr.output_section_index = backlinks[atom_ptr.output_section_index];
35073497 }
3508 if (zo.dwarf) |*dwarf| dwarf.reloadSectionMetadata();
35093498 }
35103499
35113500 for (self.comdat_group_sections.items) |*cg| {
src/link/Elf/ZigObject.zig+27-33
......@@ -99,8 +99,8 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
9999 .dwarf => |v| {
100100 var dwarf = Dwarf.init(&elf_file.base, v);
101101
102 if (elf_file.debug_str_section_index == null) {
103 elf_file.debug_str_section_index = try elf_file.addSection(.{
102 if (self.debug_str_index == null) {
103 const osec = try elf_file.addSection(.{
104104 .name = try elf_file.insertShString(".debug_str"),
105105 .flags = elf.SHF_MERGE | elf.SHF_STRINGS,
106106 .entsize = 1,
......@@ -108,51 +108,51 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
108108 .addralign = 1,
109109 });
110110 self.debug_str_section_dirty = true;
111 self.debug_str_index = try self.addSectionSymbol(gpa, ".debug_str", .@"1", elf_file.debug_str_section_index.?);
111 self.debug_str_index = try self.addSectionSymbol(gpa, ".debug_str", .@"1", osec);
112112 }
113113
114 if (elf_file.debug_info_section_index == null) {
115 elf_file.debug_info_section_index = try elf_file.addSection(.{
114 if (self.debug_info_index == null) {
115 const osec = try elf_file.addSection(.{
116116 .name = try elf_file.insertShString(".debug_info"),
117117 .type = elf.SHT_PROGBITS,
118118 .addralign = 1,
119119 });
120120 self.debug_info_section_dirty = true;
121 self.debug_info_index = try self.addSectionSymbol(gpa, ".debug_info", .@"1", elf_file.debug_info_section_index.?);
121 self.debug_info_index = try self.addSectionSymbol(gpa, ".debug_info", .@"1", osec);
122122 }
123123
124 if (elf_file.debug_abbrev_section_index == null) {
125 elf_file.debug_abbrev_section_index = try elf_file.addSection(.{
124 if (self.debug_abbrev_index == null) {
125 const osec = try elf_file.addSection(.{
126126 .name = try elf_file.insertShString(".debug_abbrev"),
127127 .type = elf.SHT_PROGBITS,
128128 .addralign = 1,
129129 });
130130 self.debug_abbrev_section_dirty = true;
131 self.debug_abbrev_index = try self.addSectionSymbol(gpa, ".debug_abbrev", .@"1", elf_file.debug_abbrev_section_index.?);
131 self.debug_abbrev_index = try self.addSectionSymbol(gpa, ".debug_abbrev", .@"1", osec);
132132 }
133133
134 if (elf_file.debug_aranges_section_index == null) {
135 elf_file.debug_aranges_section_index = try elf_file.addSection(.{
134 if (self.debug_aranges_index == null) {
135 const osec = try elf_file.addSection(.{
136136 .name = try elf_file.insertShString(".debug_aranges"),
137137 .type = elf.SHT_PROGBITS,
138138 .addralign = 16,
139139 });
140140 self.debug_aranges_section_dirty = true;
141 self.debug_aranges_index = try self.addSectionSymbol(gpa, ".debug_aranges", .@"16", elf_file.debug_aranges_section_index.?);
141 self.debug_aranges_index = try self.addSectionSymbol(gpa, ".debug_aranges", .@"16", osec);
142142 }
143143
144 if (elf_file.debug_line_section_index == null) {
145 elf_file.debug_line_section_index = try elf_file.addSection(.{
144 if (self.debug_line_index == null) {
145 const osec = try elf_file.addSection(.{
146146 .name = try elf_file.insertShString(".debug_line"),
147147 .type = elf.SHT_PROGBITS,
148148 .addralign = 1,
149149 });
150150 self.debug_line_section_dirty = true;
151 self.debug_line_index = try self.addSectionSymbol(gpa, ".debug_line", .@"1", elf_file.debug_line_section_index.?);
151 self.debug_line_index = try self.addSectionSymbol(gpa, ".debug_line", .@"1", osec);
152152 }
153153
154 if (elf_file.debug_line_str_section_index == null) {
155 elf_file.debug_line_str_section_index = try elf_file.addSection(.{
154 if (self.debug_line_str_index == null) {
155 const osec = try elf_file.addSection(.{
156156 .name = try elf_file.insertShString(".debug_line_str"),
157157 .flags = elf.SHF_MERGE | elf.SHF_STRINGS,
158158 .entsize = 1,
......@@ -160,31 +160,31 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
160160 .addralign = 1,
161161 });
162162 self.debug_line_str_section_dirty = true;
163 self.debug_line_str_index = try self.addSectionSymbol(gpa, ".debug_line_str", .@"1", elf_file.debug_line_str_section_index.?);
163 self.debug_line_str_index = try self.addSectionSymbol(gpa, ".debug_line_str", .@"1", osec);
164164 }
165165
166 if (elf_file.debug_loclists_section_index == null) {
167 elf_file.debug_loclists_section_index = try elf_file.addSection(.{
166 if (self.debug_loclists_index == null) {
167 const osec = try elf_file.addSection(.{
168168 .name = try elf_file.insertShString(".debug_loclists"),
169169 .type = elf.SHT_PROGBITS,
170170 .addralign = 1,
171171 });
172172 self.debug_loclists_section_dirty = true;
173 self.debug_loclists_index = try self.addSectionSymbol(gpa, ".debug_loclists", .@"1", elf_file.debug_loclists_section_index.?);
173 self.debug_loclists_index = try self.addSectionSymbol(gpa, ".debug_loclists", .@"1", osec);
174174 }
175175
176 if (elf_file.debug_rnglists_section_index == null) {
177 elf_file.debug_rnglists_section_index = try elf_file.addSection(.{
176 if (self.debug_rnglists_index == null) {
177 const osec = try elf_file.addSection(.{
178178 .name = try elf_file.insertShString(".debug_rnglists"),
179179 .type = elf.SHT_PROGBITS,
180180 .addralign = 1,
181181 });
182182 self.debug_rnglists_section_dirty = true;
183 self.debug_rnglists_index = try self.addSectionSymbol(gpa, ".debug_rnglists", .@"1", elf_file.debug_rnglists_section_index.?);
183 self.debug_rnglists_index = try self.addSectionSymbol(gpa, ".debug_rnglists", .@"1", osec);
184184 }
185185
186 if (elf_file.eh_frame_section_index == null) {
187 elf_file.eh_frame_section_index = try elf_file.addSection(.{
186 if (self.eh_frame_index == null) {
187 const osec = try elf_file.addSection(.{
188188 .name = try elf_file.insertShString(".eh_frame"),
189189 .type = if (elf_file.getTarget().cpu.arch == .x86_64)
190190 elf.SHT_X86_64_UNWIND
......@@ -194,7 +194,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
194194 .addralign = ptr_size,
195195 });
196196 self.eh_frame_section_dirty = true;
197 self.eh_frame_index = try self.addSectionSymbol(gpa, ".eh_frame", Atom.Alignment.fromNonzeroByteUnits(ptr_size), elf_file.eh_frame_section_index.?);
197 self.eh_frame_index = try self.addSectionSymbol(gpa, ".eh_frame", Atom.Alignment.fromNonzeroByteUnits(ptr_size), osec);
198198 }
199199
200200 try dwarf.initMetadata();
......@@ -328,12 +328,6 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi
328328 const sym = self.symbol(sym_index);
329329 const atom_ptr = self.atom(sym.ref.index).?;
330330 if (!atom_ptr.alive) continue;
331 const shndx = sym.outputShndx(elf_file).?;
332 const shdr = elf_file.sections.items(.shdr)[shndx];
333 const esym = &self.symtab.items(.elf_sym)[sym.esym_index];
334 esym.st_size = shdr.sh_size;
335 atom_ptr.size = shdr.sh_size;
336 atom_ptr.alignment = Atom.Alignment.fromNonzeroByteUnits(shdr.sh_addralign);
337331
338332 log.debug("parsing relocs in {s}", .{sym.name(elf_file)});
339333