| ... | ... | @@ -44,7 +44,6 @@ strtab: StringTable(.strtab) = .{}, |
| 44 | 44 | |
| 45 | 45 | got: GotSection = .{}, |
| 46 | 46 | |
| 47 | | symtab_section_index: ?u16 = null, |
| 48 | 47 | text_section_index: ?u16 = null, |
| 49 | 48 | rodata_section_index: ?u16 = null, |
| 50 | 49 | got_section_index: ?u16 = null, |
| ... | ... | @@ -56,6 +55,7 @@ debug_aranges_section_index: ?u16 = null, |
| 56 | 55 | debug_line_section_index: ?u16 = null, |
| 57 | 56 | shstrtab_section_index: ?u16 = null, |
| 58 | 57 | strtab_section_index: ?u16 = null, |
| 58 | symtab_section_index: ?u16 = null, |
| 59 | 59 | |
| 60 | 60 | symbols: std.ArrayListUnmanaged(Symbol) = .{}, |
| 61 | 61 | symbols_extra: std.ArrayListUnmanaged(u32) = .{}, |
| ... | ... | @@ -1070,7 +1070,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1070 | 1070 | try dw.writeDbgAbbrev(); |
| 1071 | 1071 | if (!self.shdr_table_dirty) { |
| 1072 | 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 | 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 | 1092 | try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz); |
| 1093 | 1093 | if (!self.shdr_table_dirty) { |
| 1094 | 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 | 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 | 1137 | defer gpa.free(buf); |
| 1138 | 1138 | |
| 1139 | 1139 | for (buf, 0..) |*phdr, i| { |
| 1140 | | phdr.* = progHeaderTo32(self.phdrs.items[i]); |
| 1140 | phdr.* = phdrTo32(self.phdrs.items[i]); |
| 1141 | 1141 | if (foreign_endian) { |
| 1142 | 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 | 1219 | defer gpa.free(buf); |
| 1220 | 1220 | |
| 1221 | 1221 | for (buf, 0..) |*shdr, i| { |
| 1222 | | shdr.* = sectHeaderTo32(slice.items(.shdr)[i]); |
| 1222 | shdr.* = shdrTo32(slice.items(.shdr)[i]); |
| 1223 | 1223 | log.debug("writing section {?s}: {}", .{ self.shstrtab.get(shdr.sh_name), shdr.* }); |
| 1224 | 1224 | if (foreign_endian) { |
| 1225 | 1225 | mem.byteSwapAllFields(elf.Elf32_Shdr, shdr); |
| ... | ... | @@ -2682,50 +2682,6 @@ pub fn deleteDeclExport( |
| 2682 | 2682 | sym_index.* = 0; |
| 2683 | 2683 | } |
| 2684 | 2684 | |
| 2685 | | fn 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 | | |
| 2706 | | fn 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 | | |
| 2729 | 2685 | fn updateSymtabSize(self: *Elf) !void { |
| 2730 | 2686 | var sizes = SymtabSize{}; |
| 2731 | 2687 | |
| ... | ... | @@ -2832,7 +2788,7 @@ pub fn archPtrWidthBytes(self: Elf) u8 { |
| 2832 | 2788 | return @as(u8, @intCast(@divExact(self.base.options.target.ptrBitWidth(), 8))); |
| 2833 | 2789 | } |
| 2834 | 2790 | |
| 2835 | | fn progHeaderTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr { |
| 2791 | fn phdrTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr { |
| 2836 | 2792 | return .{ |
| 2837 | 2793 | .p_type = phdr.p_type, |
| 2838 | 2794 | .p_flags = phdr.p_flags, |
| ... | ... | @@ -2845,7 +2801,30 @@ fn progHeaderTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr { |
| 2845 | 2801 | }; |
| 2846 | 2802 | } |
| 2847 | 2803 | |
| 2848 | | fn sectHeaderTo32(shdr: elf.Elf64_Shdr) elf.Elf32_Shdr { |
| 2804 | fn 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 | |
| 2827 | fn shdrTo32(shdr: elf.Elf64_Shdr) elf.Elf32_Shdr { |
| 2849 | 2828 | return .{ |
| 2850 | 2829 | .sh_name = shdr.sh_name, |
| 2851 | 2830 | .sh_type = shdr.sh_type, |