authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-09 16:03:39+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-09 16:03:39+02:00
loga6e9163284f75a9b251167eb7efb9b77169c5640
treece1afd2fc28d02e319f9ecb18636b3850fdcae50
parenta3372050b184aa2d103f11afcd1f87bf6077fa3d

elf: clean up naming and remove unused functions


1 files changed, 30 insertions(+), 51 deletions(-)

src/link/Elf.zig+30-51
...@@ -44,7 +44,6 @@ strtab: StringTable(.strtab) = .{},...@@ -44,7 +44,6 @@ strtab: StringTable(.strtab) = .{},
4444
45got: GotSection = .{},45got: GotSection = .{},
4646
47symtab_section_index: ?u16 = null,
48text_section_index: ?u16 = null,47text_section_index: ?u16 = null,
49rodata_section_index: ?u16 = null,48rodata_section_index: ?u16 = null,
50got_section_index: ?u16 = null,49got_section_index: ?u16 = null,
...@@ -56,6 +55,7 @@ debug_aranges_section_index: ?u16 = null,...@@ -56,6 +55,7 @@ debug_aranges_section_index: ?u16 = null,
56debug_line_section_index: ?u16 = null,55debug_line_section_index: ?u16 = null,
57shstrtab_section_index: ?u16 = null,56shstrtab_section_index: ?u16 = null,
58strtab_section_index: ?u16 = null,57strtab_section_index: ?u16 = null,
58symtab_section_index: ?u16 = null,
5959
60symbols: std.ArrayListUnmanaged(Symbol) = .{},60symbols: std.ArrayListUnmanaged(Symbol) = .{},
61symbols_extra: std.ArrayListUnmanaged(u32) = .{},61symbols_extra: std.ArrayListUnmanaged(u32) = .{},
...@@ -1070,7 +1070,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1070,7 +1070,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1070 try dw.writeDbgAbbrev();1070 try dw.writeDbgAbbrev();
1071 if (!self.shdr_table_dirty) {1071 if (!self.shdr_table_dirty) {
1072 // Then it won't get written with the others and we need to do it.1072 // Then it won't get written with the others and we need to do it.
1073 try self.writeSectHeader(self.debug_abbrev_section_index.?);1073 try self.writeShdr(self.debug_abbrev_section_index.?);
1074 }1074 }
1075 self.debug_abbrev_section_dirty = false;1075 self.debug_abbrev_section_dirty = false;
1076 }1076 }
...@@ -1092,7 +1092,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1092,7 +1092,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1092 try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz);1092 try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz);
1093 if (!self.shdr_table_dirty) {1093 if (!self.shdr_table_dirty) {
1094 // Then it won't get written with the others and we need to do it.1094 // Then it won't get written with the others and we need to do it.
1095 try self.writeSectHeader(self.debug_aranges_section_index.?);1095 try self.writeShdr(self.debug_aranges_section_index.?);
1096 }1096 }
1097 self.debug_aranges_section_dirty = false;1097 self.debug_aranges_section_dirty = false;
1098 }1098 }
...@@ -1137,7 +1137,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1137,7 +1137,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1137 defer gpa.free(buf);1137 defer gpa.free(buf);
11381138
1139 for (buf, 0..) |*phdr, i| {1139 for (buf, 0..) |*phdr, i| {
1140 phdr.* = progHeaderTo32(self.phdrs.items[i]);1140 phdr.* = phdrTo32(self.phdrs.items[i]);
1141 if (foreign_endian) {1141 if (foreign_endian) {
1142 mem.byteSwapAllFields(elf.Elf32_Phdr, phdr);1142 mem.byteSwapAllFields(elf.Elf32_Phdr, phdr);
1143 }1143 }
...@@ -1219,7 +1219,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1219,7 +1219,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1219 defer gpa.free(buf);1219 defer gpa.free(buf);
12201220
1221 for (buf, 0..) |*shdr, i| {1221 for (buf, 0..) |*shdr, i| {
1222 shdr.* = sectHeaderTo32(slice.items(.shdr)[i]);1222 shdr.* = shdrTo32(slice.items(.shdr)[i]);
1223 log.debug("writing section {?s}: {}", .{ self.shstrtab.get(shdr.sh_name), shdr.* });1223 log.debug("writing section {?s}: {}", .{ self.shstrtab.get(shdr.sh_name), shdr.* });
1224 if (foreign_endian) {1224 if (foreign_endian) {
1225 mem.byteSwapAllFields(elf.Elf32_Shdr, shdr);1225 mem.byteSwapAllFields(elf.Elf32_Shdr, shdr);
...@@ -2682,50 +2682,6 @@ pub fn deleteDeclExport(...@@ -2682,50 +2682,6 @@ pub fn deleteDeclExport(
2682 sym_index.* = 0;2682 sym_index.* = 0;
2683}2683}
26842684
2685fn writeProgHeader(self: *Elf, index: usize) !void {
2686 const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian();
2687 const offset = self.phdrs.items[index].p_offset;
2688 switch (self.ptr_width) {
2689 .p32 => {
2690 var phdr = [1]elf.Elf32_Phdr{progHeaderTo32(self.phdrs.items[index])};
2691 if (foreign_endian) {
2692 mem.byteSwapAllFields(elf.Elf32_Phdr, &phdr[0]);
2693 }
2694 return self.base.file.?.pwriteAll(mem.sliceAsBytes(&phdr), offset);
2695 },
2696 .p64 => {
2697 var phdr = [1]elf.Elf64_Phdr{self.phdrs.items[index]};
2698 if (foreign_endian) {
2699 mem.byteSwapAllFields(elf.Elf64_Phdr, &phdr[0]);
2700 }
2701 return self.base.file.?.pwriteAll(mem.sliceAsBytes(&phdr), offset);
2702 },
2703 }
2704}
2705
2706fn writeSectHeader(self: *Elf, index: usize) !void {
2707 const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian();
2708 switch (self.ptr_width) {
2709 .p32 => {
2710 var shdr: [1]elf.Elf32_Shdr = undefined;
2711 shdr[0] = sectHeaderTo32(self.sections.items(.shdr)[index]);
2712 if (foreign_endian) {
2713 mem.byteSwapAllFields(elf.Elf32_Shdr, &shdr[0]);
2714 }
2715 const offset = self.shdr_table_offset.? + index * @sizeOf(elf.Elf32_Shdr);
2716 return self.base.file.?.pwriteAll(mem.sliceAsBytes(&shdr), offset);
2717 },
2718 .p64 => {
2719 var shdr = [1]elf.Elf64_Shdr{self.sections.items(.shdr)[index]};
2720 if (foreign_endian) {
2721 mem.byteSwapAllFields(elf.Elf64_Shdr, &shdr[0]);
2722 }
2723 const offset = self.shdr_table_offset.? + index * @sizeOf(elf.Elf64_Shdr);
2724 return self.base.file.?.pwriteAll(mem.sliceAsBytes(&shdr), offset);
2725 },
2726 }
2727}
2728
2729fn updateSymtabSize(self: *Elf) !void {2685fn updateSymtabSize(self: *Elf) !void {
2730 var sizes = SymtabSize{};2686 var sizes = SymtabSize{};
27312687
...@@ -2832,7 +2788,7 @@ pub fn archPtrWidthBytes(self: Elf) u8 {...@@ -2832,7 +2788,7 @@ pub fn archPtrWidthBytes(self: Elf) u8 {
2832 return @as(u8, @intCast(@divExact(self.base.options.target.ptrBitWidth(), 8)));2788 return @as(u8, @intCast(@divExact(self.base.options.target.ptrBitWidth(), 8)));
2833}2789}
28342790
2835fn progHeaderTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr {2791fn phdrTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr {
2836 return .{2792 return .{
2837 .p_type = phdr.p_type,2793 .p_type = phdr.p_type,
2838 .p_flags = phdr.p_flags,2794 .p_flags = phdr.p_flags,
...@@ -2845,7 +2801,30 @@ fn progHeaderTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr {...@@ -2845,7 +2801,30 @@ fn progHeaderTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr {
2845 };2801 };
2846}2802}
28472803
2848fn sectHeaderTo32(shdr: elf.Elf64_Shdr) elf.Elf32_Shdr {2804fn writeShdr(self: *Elf, index: usize) !void {
2805 const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian();
2806 switch (self.ptr_width) {
2807 .p32 => {
2808 var shdr: [1]elf.Elf32_Shdr = undefined;
2809 shdr[0] = shdrTo32(self.sections.items(.shdr)[index]);
2810 if (foreign_endian) {
2811 mem.byteSwapAllFields(elf.Elf32_Shdr, &shdr[0]);
2812 }
2813 const offset = self.shdr_table_offset.? + index * @sizeOf(elf.Elf32_Shdr);
2814 return self.base.file.?.pwriteAll(mem.sliceAsBytes(&shdr), offset);
2815 },
2816 .p64 => {
2817 var shdr = [1]elf.Elf64_Shdr{self.sections.items(.shdr)[index]};
2818 if (foreign_endian) {
2819 mem.byteSwapAllFields(elf.Elf64_Shdr, &shdr[0]);
2820 }
2821 const offset = self.shdr_table_offset.? + index * @sizeOf(elf.Elf64_Shdr);
2822 return self.base.file.?.pwriteAll(mem.sliceAsBytes(&shdr), offset);
2823 },
2824 }
2825}
2826
2827fn shdrTo32(shdr: elf.Elf64_Shdr) elf.Elf32_Shdr {
2849 return .{2828 return .{
2850 .sh_name = shdr.sh_name,2829 .sh_name = shdr.sh_name,
2851 .sh_type = shdr.sh_type,2830 .sh_type = shdr.sh_type,