authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-13 17:24:46+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-13 20:33:08+01:00
log216a5594f62aca7524012e0a2bbf226c4064fae4
treedb3497aaf292af43f3dffbabfa124a678c176363
parente401930fa862e3b9b3eedc8eb405c501fbf3de30

elf: use u32 for all section indexes


6 files changed, 78 insertions(+), 78 deletions(-)

src/link/Elf.zig+57-57
......@@ -138,40 +138,40 @@ comdat_group_sections: std.ArrayListUnmanaged(ComdatGroupSection) = .{},
138138
139139/// Tracked section headers with incremental updates to Zig object.
140140/// .rela.* sections are only used when emitting a relocatable object file.
141zig_text_section_index: ?u16 = null,
142zig_data_rel_ro_section_index: ?u16 = null,
143zig_data_section_index: ?u16 = null,
144zig_bss_section_index: ?u16 = null,
145zig_got_section_index: ?u16 = null,
146
147debug_info_section_index: ?u16 = null,
148debug_abbrev_section_index: ?u16 = null,
149debug_str_section_index: ?u16 = null,
150debug_aranges_section_index: ?u16 = null,
151debug_line_section_index: ?u16 = null,
152
153copy_rel_section_index: ?u16 = null,
154dynamic_section_index: ?u16 = null,
155dynstrtab_section_index: ?u16 = null,
156dynsymtab_section_index: ?u16 = null,
157eh_frame_section_index: ?u16 = null,
158eh_frame_rela_section_index: ?u16 = null,
159eh_frame_hdr_section_index: ?u16 = null,
160hash_section_index: ?u16 = null,
161gnu_hash_section_index: ?u16 = null,
162got_section_index: ?u16 = null,
163got_plt_section_index: ?u16 = null,
164interp_section_index: ?u16 = null,
165plt_section_index: ?u16 = null,
166plt_got_section_index: ?u16 = null,
167rela_dyn_section_index: ?u16 = null,
168rela_plt_section_index: ?u16 = null,
169versym_section_index: ?u16 = null,
170verneed_section_index: ?u16 = null,
171
172shstrtab_section_index: ?u16 = null,
173strtab_section_index: ?u16 = null,
174symtab_section_index: ?u16 = null,
141zig_text_section_index: ?u32 = null,
142zig_data_rel_ro_section_index: ?u32 = null,
143zig_data_section_index: ?u32 = null,
144zig_bss_section_index: ?u32 = null,
145zig_got_section_index: ?u32 = null,
146
147debug_info_section_index: ?u32 = null,
148debug_abbrev_section_index: ?u32 = null,
149debug_str_section_index: ?u32 = null,
150debug_aranges_section_index: ?u32 = null,
151debug_line_section_index: ?u32 = null,
152
153copy_rel_section_index: ?u32 = null,
154dynamic_section_index: ?u32 = null,
155dynstrtab_section_index: ?u32 = null,
156dynsymtab_section_index: ?u32 = null,
157eh_frame_section_index: ?u32 = null,
158eh_frame_rela_section_index: ?u32 = null,
159eh_frame_hdr_section_index: ?u32 = null,
160hash_section_index: ?u32 = null,
161gnu_hash_section_index: ?u32 = null,
162got_section_index: ?u32 = null,
163got_plt_section_index: ?u32 = null,
164interp_section_index: ?u32 = null,
165plt_section_index: ?u32 = null,
166plt_got_section_index: ?u32 = null,
167rela_dyn_section_index: ?u32 = null,
168rela_plt_section_index: ?u32 = null,
169versym_section_index: ?u32 = null,
170verneed_section_index: ?u32 = null,
171
172shstrtab_section_index: ?u32 = null,
173strtab_section_index: ?u32 = null,
174symtab_section_index: ?u32 = null,
175175
176176// Linker-defined symbols
177177dynamic_index: ?Symbol.Index = null,
......@@ -931,7 +931,7 @@ pub fn initMetadata(self: *Elf, options: InitMetadataOptions) !void {
931931 try self.base.file.?.pwriteAll(&[1]u8{0}, end_pos);
932932}
933933
934pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {
934pub fn growAllocSection(self: *Elf, shdr_index: u32, needed_size: u64) !void {
935935 const shdr = &self.shdrs.items[shdr_index];
936936 const maybe_phdr = if (self.phdr_to_shdr_table.get(shdr_index)) |phndx| &self.phdrs.items[phndx] else null;
937937 const is_zerofill = shdr.sh_type == elf.SHT_NOBITS;
......@@ -981,7 +981,7 @@ pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {
981981
982982pub fn growNonAllocSection(
983983 self: *Elf,
984 shdr_index: u16,
984 shdr_index: u32,
985985 needed_size: u64,
986986 min_alignment: u32,
987987 requires_file_copy: bool,
......@@ -1018,7 +1018,7 @@ pub fn growNonAllocSection(
10181018 self.markDirty(shdr_index);
10191019}
10201020
1021pub fn markDirty(self: *Elf, shdr_index: u16) void {
1021pub fn markDirty(self: *Elf, shdr_index: u32) void {
10221022 const zig_object = self.zigObjectPtr().?;
10231023 if (zig_object.dwarf) |_| {
10241024 if (self.debug_info_section_index.? == shdr_index) {
......@@ -2969,7 +2969,7 @@ pub fn writeElfHeader(self: *Elf) !void {
29692969 mem.writeInt(u16, hdr_buf[index..][0..2], e_shnum, endian);
29702970 index += 2;
29712971
2972 mem.writeInt(u16, hdr_buf[index..][0..2], self.shstrtab_section_index.?, endian);
2972 mem.writeInt(u16, hdr_buf[index..][0..2], @intCast(self.shstrtab_section_index.?), endian);
29732973 index += 2;
29742974
29752975 assert(index == e_ehsize);
......@@ -3709,7 +3709,7 @@ fn sortPhdrs(self: *Elf) error{OutOfMemory}!void {
37093709 }
37103710}
37113711
3712fn shdrRank(self: *Elf, shndx: u16) u8 {
3712fn shdrRank(self: *Elf, shndx: u32) u8 {
37133713 const shdr = self.shdrs.items[shndx];
37143714 const name = self.getShString(shdr.sh_name);
37153715 const flags = shdr.sh_flags;
......@@ -3759,7 +3759,7 @@ fn shdrRank(self: *Elf, shndx: u16) u8 {
37593759
37603760pub fn sortShdrs(self: *Elf) !void {
37613761 const Entry = struct {
3762 shndx: u16,
3762 shndx: u32,
37633763
37643764 pub fn lessThan(elf_file: *Elf, lhs: @This(), rhs: @This()) bool {
37653765 return elf_file.shdrRank(lhs.shndx) < elf_file.shdrRank(rhs.shndx);
......@@ -3770,15 +3770,15 @@ pub fn sortShdrs(self: *Elf) !void {
37703770 var entries = try std.ArrayList(Entry).initCapacity(gpa, self.shdrs.items.len);
37713771 defer entries.deinit();
37723772 for (0..self.shdrs.items.len) |shndx| {
3773 entries.appendAssumeCapacity(.{ .shndx = @as(u16, @intCast(shndx)) });
3773 entries.appendAssumeCapacity(.{ .shndx = @intCast(shndx) });
37743774 }
37753775
37763776 mem.sort(Entry, entries.items, self, Entry.lessThan);
37773777
3778 const backlinks = try gpa.alloc(u16, entries.items.len);
3778 const backlinks = try gpa.alloc(u32, entries.items.len);
37793779 defer gpa.free(backlinks);
37803780 for (entries.items, 0..) |entry, i| {
3781 backlinks[entry.shndx] = @as(u16, @intCast(i));
3781 backlinks[entry.shndx] = @intCast(i);
37823782 }
37833783
37843784 const slice = try self.shdrs.toOwnedSlice(gpa);
......@@ -3792,10 +3792,10 @@ pub fn sortShdrs(self: *Elf) !void {
37923792 try self.resetShdrIndexes(backlinks);
37933793}
37943794
3795fn resetShdrIndexes(self: *Elf, backlinks: []const u16) !void {
3795fn resetShdrIndexes(self: *Elf, backlinks: []const u32) !void {
37963796 const gpa = self.base.comp.gpa;
37973797
3798 for (&[_]*?u16{
3798 for (&[_]*?u32{
37993799 &self.eh_frame_section_index,
38003800 &self.eh_frame_rela_section_index,
38013801 &self.eh_frame_hdr_section_index,
......@@ -4191,7 +4191,7 @@ pub fn allocateAllocSections(self: *Elf) error{OutOfMemory}!void {
41914191 // virtual and file offsets. However, the simple one will do for one
41924192 // as we are more interested in quick turnaround and compatibility
41934193 // with `findFreeSpace` mechanics than anything else.
4194 const Cover = std.ArrayList(u16);
4194 const Cover = std.ArrayList(u32);
41954195 const gpa = self.base.comp.gpa;
41964196 var covers: [max_number_of_object_segments]Cover = undefined;
41974197 for (&covers) |*cover| {
......@@ -4347,7 +4347,7 @@ pub fn allocateNonAllocSections(self: *Elf) !void {
43474347}
43484348
43494349fn allocateSpecialPhdrs(self: *Elf) void {
4350 for (&[_]struct { ?u16, ?u16 }{
4350 for (&[_]struct { ?u16, ?u32 }{
43514351 .{ self.phdr_interp_index, self.interp_section_index },
43524352 .{ self.phdr_dynamic_index, self.dynamic_section_index },
43534353 .{ self.phdr_gnu_eh_frame_index, self.eh_frame_hdr_section_index },
......@@ -4370,7 +4370,7 @@ fn allocateSpecialPhdrs(self: *Elf) void {
43704370 if (self.phdr_tls_index) |index| {
43714371 const slice = self.shdrs.items;
43724372 const phdr = &self.phdrs.items[index];
4373 var shndx: u16 = 0;
4373 var shndx: u32 = 0;
43744374 while (shndx < slice.len) {
43754375 const shdr = slice[shndx];
43764376 if (shdr.sh_flags & elf.SHF_TLS == 0) {
......@@ -5138,8 +5138,8 @@ const CsuObjects = struct {
51385138 }
51395139};
51405140
5141pub fn isZigSection(self: Elf, shndx: u16) bool {
5142 inline for (&[_]?u16{
5141pub fn isZigSection(self: Elf, shndx: u32) bool {
5142 inline for (&[_]?u32{
51435143 self.zig_text_section_index,
51445144 self.zig_data_rel_ro_section_index,
51455145 self.zig_data_section_index,
......@@ -5153,8 +5153,8 @@ pub fn isZigSection(self: Elf, shndx: u16) bool {
51535153 return false;
51545154}
51555155
5156pub fn isDebugSection(self: Elf, shndx: u16) bool {
5157 inline for (&[_]?u16{
5156pub fn isDebugSection(self: Elf, shndx: u32) bool {
5157 inline for (&[_]?u32{
51585158 self.debug_info_section_index,
51595159 self.debug_abbrev_section_index,
51605160 self.debug_str_section_index,
......@@ -5192,7 +5192,7 @@ fn addPhdr(self: *Elf, opts: struct {
51925192 return index;
51935193}
51945194
5195pub fn addRelaShdr(self: *Elf, name: [:0]const u8, shndx: u16) !u16 {
5195pub fn addRelaShdr(self: *Elf, name: [:0]const u8, shndx: u32) !u32 {
51965196 const entsize: u64 = switch (self.ptr_width) {
51975197 .p32 => @sizeOf(elf.Elf32_Rela),
51985198 .p64 => @sizeOf(elf.Elf64_Rela),
......@@ -5223,9 +5223,9 @@ pub const AddSectionOpts = struct {
52235223 offset: u64 = 0,
52245224};
52255225
5226pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 {
5226pub fn addSection(self: *Elf, opts: AddSectionOpts) !u32 {
52275227 const gpa = self.base.comp.gpa;
5228 const index = @as(u16, @intCast(self.shdrs.items.len));
5228 const index = @as(u32, @intCast(self.shdrs.items.len));
52295229 const shdr = try self.shdrs.addOne(gpa);
52305230 shdr.* = .{
52315231 .sh_name = try self.insertShString(opts.name),
......@@ -5242,10 +5242,10 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 {
52425242 return index;
52435243}
52445244
5245pub fn sectionByName(self: *Elf, name: [:0]const u8) ?u16 {
5245pub fn sectionByName(self: *Elf, name: [:0]const u8) ?u32 {
52465246 for (self.shdrs.items, 0..) |*shdr, i| {
52475247 const this_name = self.getShString(shdr.sh_name);
5248 if (mem.eql(u8, this_name, name)) return @as(u16, @intCast(i));
5248 if (mem.eql(u8, this_name, name)) return @intCast(i);
52495249 } else return null;
52505250}
52515251
src/link/Elf/Atom.zig+2-2
......@@ -17,7 +17,7 @@ alignment: Alignment = .@"1",
1717input_section_index: u32 = 0,
1818
1919/// Index of the output section.
20output_section_index: u16 = 0,
20output_section_index: u32 = 0,
2121
2222/// Index of the input section containing this atom's relocs.
2323relocs_section_index: u32 = 0,
......@@ -77,7 +77,7 @@ pub fn relocsShndx(self: Atom) ?u32 {
7777 return self.relocs_section_index;
7878}
7979
80pub fn outputShndx(self: Atom) ?u16 {
80pub fn outputShndx(self: Atom) ?u32 {
8181 if (self.output_section_index == 0) return null;
8282 return self.output_section_index;
8383}
src/link/Elf/Object.zig+5-5
......@@ -126,7 +126,7 @@ fn parseCommon(self: *Object, allocator: Allocator, handle: std.fs.File, elf_fil
126126 try self.strtab.appendSlice(allocator, shstrtab);
127127
128128 const symtab_index = for (self.shdrs.items, 0..) |shdr, i| switch (shdr.sh_type) {
129 elf.SHT_SYMTAB => break @as(u16, @intCast(i)),
129 elf.SHT_SYMTAB => break @as(u32, @intCast(i)),
130130 else => {},
131131 } else null;
132132
......@@ -223,7 +223,7 @@ fn initAtoms(self: *Object, allocator: Allocator, handle: std.fs.File, elf_file:
223223 => {},
224224
225225 else => {
226 const shndx = @as(u16, @intCast(i));
226 const shndx = @as(u32, @intCast(i));
227227 if (self.skipShdr(shndx, elf_file)) continue;
228228 try self.addAtom(allocator, handle, shdr, shndx, elf_file);
229229 },
......@@ -268,7 +268,7 @@ fn addAtom(self: *Object, allocator: Allocator, handle: std.fs.File, shdr: elf.E
268268 }
269269}
270270
271fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) error{OutOfMemory}!u16 {
271fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) error{OutOfMemory}!u32 {
272272 const name = blk: {
273273 const name = self.getString(shdr.sh_name);
274274 if (elf_file.base.isRelocatable()) break :blk name;
......@@ -316,7 +316,7 @@ fn initOutputSection(self: Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) error{O
316316 return out_shndx;
317317}
318318
319fn skipShdr(self: *Object, index: u16, elf_file: *Elf) bool {
319fn skipShdr(self: *Object, index: u32, elf_file: *Elf) bool {
320320 const comp = elf_file.base.comp;
321321 const shdr = self.shdrs.items[index];
322322 const name = self.getString(shdr.sh_name);
......@@ -673,7 +673,7 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void {
673673
674674 var sh_flags: u32 = elf.SHF_ALLOC | elf.SHF_WRITE;
675675 if (is_tls) sh_flags |= elf.SHF_TLS;
676 const shndx = @as(u16, @intCast(self.shdrs.items.len));
676 const shndx = @as(u32, @intCast(self.shdrs.items.len));
677677 const shdr = try self.shdrs.addOne(gpa);
678678 const sh_size = math.cast(usize, this_sym.st_size) orelse return error.Overflow;
679679 shdr.* = .{
src/link/Elf/Symbol.zig+5-5
......@@ -15,7 +15,7 @@ file_index: File.Index = 0,
1515atom_index: Atom.Index = 0,
1616
1717/// Assigned output section index for this atom.
18output_section_index: u16 = 0,
18output_section_index: u32 = 0,
1919
2020/// Index of the source symbol this symbol references.
2121/// Use `elfSym` to pull the source symbol from the relevant file.
......@@ -37,7 +37,7 @@ pub fn isAbs(symbol: Symbol, elf_file: *Elf) bool {
3737 file_ptr != .linker_defined;
3838}
3939
40pub fn outputShndx(symbol: Symbol) ?u16 {
40pub fn outputShndx(symbol: Symbol) ?u32 {
4141 if (symbol.output_section_index == 0) return null;
4242 return symbol.output_section_index;
4343}
......@@ -237,12 +237,12 @@ pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void {
237237 if (file_ptr == .shared_object) break :blk elf.STB_GLOBAL;
238238 break :blk esym.st_bind();
239239 };
240 const st_shndx = blk: {
241 if (symbol.flags.has_copy_rel) break :blk elf_file.copy_rel_section_index.?;
240 const st_shndx: u16 = blk: {
241 if (symbol.flags.has_copy_rel) break :blk @intCast(elf_file.copy_rel_section_index.?);
242242 if (file_ptr == .shared_object or esym.st_shndx == elf.SHN_UNDEF) break :blk elf.SHN_UNDEF;
243243 if (elf_file.base.isRelocatable() and esym.st_shndx == elf.SHN_COMMON) break :blk elf.SHN_COMMON;
244244 if (symbol.atom(elf_file) == null and file_ptr != .linker_defined) break :blk elf.SHN_ABS;
245 break :blk symbol.outputShndx() orelse elf.SHN_UNDEF;
245 break :blk @intCast(symbol.outputShndx() orelse elf.SHN_UNDEF);
246246 };
247247 const st_value = blk: {
248248 if (symbol.flags.has_copy_rel) break :blk symbol.address(.{}, elf_file);
src/link/Elf/ZigObject.zig+4-4
......@@ -840,7 +840,7 @@ fn getDeclShdrIndex(
840840 elf_file: *Elf,
841841 decl: *const Module.Decl,
842842 code: []const u8,
843) error{OutOfMemory}!u16 {
843) error{OutOfMemory}!u32 {
844844 _ = self;
845845 const mod = elf_file.base.comp.module.?;
846846 const any_non_single_threaded = elf_file.base.comp.config.any_non_single_threaded;
......@@ -894,7 +894,7 @@ fn updateDeclCode(
894894 elf_file: *Elf,
895895 decl_index: InternPool.DeclIndex,
896896 sym_index: Symbol.Index,
897 shdr_index: u16,
897 shdr_index: u32,
898898 code: []const u8,
899899 stt_bits: u8,
900900) !void {
......@@ -993,7 +993,7 @@ fn updateTlv(
993993 elf_file: *Elf,
994994 decl_index: InternPool.DeclIndex,
995995 sym_index: Symbol.Index,
996 shndx: u16,
996 shndx: u32,
997997 code: []const u8,
998998) !void {
999999 const gpa = elf_file.base.comp.gpa;
......@@ -1334,7 +1334,7 @@ fn lowerConst(
13341334 name: []const u8,
13351335 tv: TypedValue,
13361336 required_alignment: InternPool.Alignment,
1337 output_section_index: u16,
1337 output_section_index: u32,
13381338 src_loc: Module.SrcLoc,
13391339) !LowerConstResult {
13401340 const gpa = elf_file.base.comp.gpa;
src/link/Elf/synthetic_sections.zig+5-5
......@@ -388,7 +388,7 @@ pub const ZigGotSection = struct {
388388 .st_name = st_name,
389389 .st_info = elf.STT_OBJECT,
390390 .st_other = 0,
391 .st_shndx = elf_file.zig_got_section_index.?,
391 .st_shndx = @intCast(elf_file.zig_got_section_index.?),
392392 .st_value = st_value,
393393 .st_size = st_size,
394394 };
......@@ -813,7 +813,7 @@ pub const GotSection = struct {
813813 .st_name = st_name,
814814 .st_info = elf.STT_OBJECT,
815815 .st_other = 0,
816 .st_shndx = elf_file.got_section_index.?,
816 .st_shndx = @intCast(elf_file.got_section_index.?),
817817 .st_value = st_value,
818818 .st_size = st_size,
819819 };
......@@ -953,7 +953,7 @@ pub const PltSection = struct {
953953 .st_name = st_name,
954954 .st_info = elf.STT_FUNC,
955955 .st_other = 0,
956 .st_shndx = elf_file.plt_section_index.?,
956 .st_shndx = @intCast(elf_file.plt_section_index.?),
957957 .st_value = sym.pltAddress(elf_file),
958958 .st_size = 16,
959959 };
......@@ -1082,7 +1082,7 @@ pub const PltGotSection = struct {
10821082 .st_name = st_name,
10831083 .st_info = elf.STT_FUNC,
10841084 .st_other = 0,
1085 .st_shndx = elf_file.plt_got_section_index.?,
1085 .st_shndx = @intCast(elf_file.plt_got_section_index.?),
10861086 .st_value = sym.pltGotAddress(elf_file),
10871087 .st_size = 16,
10881088 };
......@@ -1132,7 +1132,7 @@ pub const CopyRelSection = struct {
11321132 }
11331133 }
11341134
1135 pub fn updateSectionSize(copy_rel: CopyRelSection, shndx: u16, elf_file: *Elf) !void {
1135 pub fn updateSectionSize(copy_rel: CopyRelSection, shndx: u32, elf_file: *Elf) !void {
11361136 const shdr = &elf_file.shdrs.items[shndx];
11371137 for (copy_rel.symbols.items) |sym_index| {
11381138 const symbol = elf_file.symbol(sym_index);