authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-09 11:49:32+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-09 11:49:32+01:00
log666ac6bf9be68a96d15085c016d123404e256d32
treed303d0695ad43dbc0e4c78e0caf4b09d488353e0
parentf607126614ab249dbf8f965ca0911fda560bc580

elf: track .rela.eh_frame section and emit .eh_frame section symbol


1 files changed, 23 insertions(+), 1 deletions(-)

src/link/Elf.zig+23-1
...@@ -122,6 +122,7 @@ dynamic_section_index: ?u16 = null,...@@ -122,6 +122,7 @@ dynamic_section_index: ?u16 = null,
122dynstrtab_section_index: ?u16 = null,122dynstrtab_section_index: ?u16 = null,
123dynsymtab_section_index: ?u16 = null,123dynsymtab_section_index: ?u16 = null,
124eh_frame_section_index: ?u16 = null,124eh_frame_section_index: ?u16 = null,
125eh_frame_rela_section_index: ?u16 = null,
125eh_frame_hdr_section_index: ?u16 = null,126eh_frame_hdr_section_index: ?u16 = null,
126hash_section_index: ?u16 = null,127hash_section_index: ?u16 = null,
127gnu_hash_section_index: ?u16 = null,128gnu_hash_section_index: ?u16 = null,
...@@ -3583,7 +3584,7 @@ fn initSectionsObject(self: *Elf) !void {...@@ -3583,7 +3584,7 @@ fn initSectionsObject(self: *Elf) !void {
3583 .addralign = ptr_size,3584 .addralign = ptr_size,
3584 .offset = std.math.maxInt(u64),3585 .offset = std.math.maxInt(u64),
3585 });3586 });
3586 // _ = try self.addRelaShdr(".rela.eh_frame", self.eh_frame_section_index.?);3587 self.eh_frame_rela_section_index = try self.addRelaShdr(".rela.eh_frame", self.eh_frame_section_index.?);
3587 }3588 }
35883589
3589 try self.initSymtab();3590 try self.initSymtab();
...@@ -3954,6 +3955,7 @@ fn sortShdrs(self: *Elf) !void {...@@ -3954,6 +3955,7 @@ fn sortShdrs(self: *Elf) !void {
39543955
3955 for (&[_]*?u16{3956 for (&[_]*?u16{
3956 &self.eh_frame_section_index,3957 &self.eh_frame_section_index,
3958 &self.eh_frame_rela_section_index,
3957 &self.eh_frame_hdr_section_index,3959 &self.eh_frame_hdr_section_index,
3958 &self.got_section_index,3960 &self.got_section_index,
3959 &self.symtab_section_index,3961 &self.symtab_section_index,
...@@ -4769,6 +4771,9 @@ fn updateSymtabSize(self: *Elf) !void {...@@ -4769,6 +4771,9 @@ fn updateSymtabSize(self: *Elf) !void {
4769 for (self.output_sections.keys()) |_| {4771 for (self.output_sections.keys()) |_| {
4770 nlocals += 1;4772 nlocals += 1;
4771 }4773 }
4774 if (self.eh_frame_section_index) |_| {
4775 nlocals += 1;
4776 }
47724777
4773 for (files.items) |index| {4778 for (files.items) |index| {
4774 const file_ptr = self.file(index).?;4779 const file_ptr = self.file(index).?;
...@@ -5162,9 +5167,26 @@ fn writeSectionSymbols(self: *Elf) void {...@@ -5162,9 +5167,26 @@ fn writeSectionSymbols(self: *Elf) void {
5162 };5167 };
5163 ilocal += 1;5168 ilocal += 1;
5164 }5169 }
5170
5171 if (self.eh_frame_section_index) |shndx| {
5172 const shdr = self.shdrs.items[shndx];
5173 const out_sym = &self.symtab.items[ilocal];
5174 out_sym.* = .{
5175 .st_name = 0,
5176 .st_value = shdr.sh_addr,
5177 .st_info = elf.STT_SECTION,
5178 .st_shndx = @intCast(shndx),
5179 .st_size = 0,
5180 .st_other = 0,
5181 };
5182 ilocal += 1;
5183 }
5165}5184}
51665185
5167pub fn sectionSymbolOutputSymtabIndex(self: Elf, shndx: u32) u32 {5186pub fn sectionSymbolOutputSymtabIndex(self: Elf, shndx: u32) u32 {
5187 if (self.eh_frame_section_index) |index| {
5188 if (index == shndx) return @intCast(self.output_sections.keys().len + 1);
5189 }
5168 return @intCast(self.output_sections.getIndex(shndx).? + 1);5190 return @intCast(self.output_sections.getIndex(shndx).? + 1);
5169}5191}
51705192