authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-28 06:57:55+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-04 13:34:25+02:00
log37a1f0e7f2586b1fd3c88c3483f0c9f731f41a30
tree05d2e12c49f172ab2271925156403a9c7fae6551
parent8f1ce3c85b6463206147119dd56ba415f6e4d28e

elf: allocate .bss in ZigObject similarly to .eh_frame


2 files changed, 61 insertions(+), 69 deletions(-)

src/link/Elf.zig+9-7
......@@ -61,8 +61,6 @@ phdr_zig_load_re_index: ?u16 = null,
6161phdr_zig_load_ro_index: ?u16 = null,
6262/// The index into the program headers of a PT_LOAD program header with Write flag
6363phdr_zig_load_rw_index: ?u16 = null,
64/// The index into the program headers of a PT_LOAD program header with zerofill data.
65phdr_zig_load_zerofill_index: ?u16 = null,
6664
6765/// Special program headers
6866/// PT_PHDR
......@@ -129,7 +127,6 @@ comdat_group_sections: std.ArrayListUnmanaged(ComdatGroupSection) = .{},
129127zig_text_section_index: ?u32 = null,
130128zig_data_rel_ro_section_index: ?u32 = null,
131129zig_data_section_index: ?u32 = null,
132zig_bss_section_index: ?u32 = null,
133130
134131debug_info_section_index: ?u32 = null,
135132debug_abbrev_section_index: ?u32 = null,
......@@ -3367,7 +3364,6 @@ fn sortPhdrs(self: *Elf) error{OutOfMemory}!void {
33673364 for (&[_]*?u16{
33683365 &self.phdr_zig_load_re_index,
33693366 &self.phdr_zig_load_ro_index,
3370 &self.phdr_zig_load_zerofill_index,
33713367 &self.phdr_table_index,
33723368 &self.phdr_table_load_index,
33733369 &self.phdr_interp_index,
......@@ -3496,7 +3492,6 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {
34963492 &self.zig_text_section_index,
34973493 &self.zig_data_rel_ro_section_index,
34983494 &self.zig_data_section_index,
3499 &self.zig_bss_section_index,
35003495 &self.debug_info_section_index,
35013496 &self.debug_abbrev_section_index,
35023497 &self.debug_str_section_index,
......@@ -3591,9 +3586,17 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {
35913586
35923587fn updateSectionSizes(self: *Elf) !void {
35933588 const slice = self.sections.slice();
3594 for (slice.items(.shdr), slice.items(.atom_list)) |*shdr, atom_list| {
3589 for (slice.items(.shdr), slice.items(.atom_list), 0..) |*shdr, atom_list, shndx| {
35953590 if (atom_list.items.len == 0) continue;
35963591 if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;
3592 if (self.zigObjectPtr()) |zo| {
3593 if (zo.bss_index) |sym_index| {
3594 const atom_ptr = zo.symbol(sym_index).atom(self).?;
3595 if (shndx == atom_ptr.output_section_index) {
3596 shdr.sh_size = atom_ptr.size;
3597 }
3598 }
3599 }
35973600 for (atom_list.items) |ref| {
35983601 const atom_ptr = self.atom(ref) orelse continue;
35993602 if (!atom_ptr.alive) continue;
......@@ -4804,7 +4807,6 @@ pub fn isZigSection(self: Elf, shndx: u32) bool {
48044807 self.zig_text_section_index,
48054808 self.zig_data_rel_ro_section_index,
48064809 self.zig_data_section_index,
4807 self.zig_bss_section_index,
48084810 }) |index| {
48094811 if (index == shndx) return true;
48104812 }
src/link/Elf/ZigObject.zig+52-62
......@@ -60,6 +60,7 @@ debug_line_str_index: ?Symbol.Index = null,
6060debug_loclists_index: ?Symbol.Index = null,
6161debug_rnglists_index: ?Symbol.Index = null,
6262eh_frame_index: ?Symbol.Index = null,
63bss_index: ?Symbol.Index = null,
6364
6465pub const global_symbol_bit: u32 = 0x80000000;
6566pub const symbol_mask: u32 = 0x7fffffff;
......@@ -149,17 +150,6 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
149150 .flags = elf.PF_R | elf.PF_W,
150151 });
151152 }
152
153 if (elf_file.phdr_zig_load_zerofill_index == null) {
154 const alignment = elf_file.page_size;
155 elf_file.phdr_zig_load_zerofill_index = try elf_file.addPhdr(.{
156 .type = elf.PT_LOAD,
157 .addr = if (ptr_size >= 4) 0x14000000 else 0xf000,
158 .memsz = 1024,
159 .@"align" = alignment,
160 .flags = elf.PF_R | elf.PF_W,
161 });
162 }
163153 }
164154
165155 if (elf_file.zig_text_section_index == null) {
......@@ -225,51 +215,11 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
225215 }
226216 }
227217
228 if (elf_file.zig_bss_section_index == null) {
229 elf_file.zig_bss_section_index = try elf_file.addSection(.{
230 .name = try elf_file.insertShString(".bss.zig"),
231 .type = elf.SHT_NOBITS,
232 .addralign = ptr_size,
233 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
234 .offset = 0,
235 });
236 const shdr = &elf_file.sections.items(.shdr)[elf_file.zig_bss_section_index.?];
237 const phndx = &elf_file.sections.items(.phndx)[elf_file.zig_bss_section_index.?];
238 if (elf_file.base.isRelocatable()) {
239 shdr.sh_size = 1024;
240 } else {
241 phndx.* = elf_file.phdr_zig_load_zerofill_index.?;
242 const phdr = elf_file.phdrs.items[phndx.*.?];
243 shdr.sh_addr = phdr.p_vaddr;
244 shdr.sh_size = phdr.p_memsz;
245 }
246 }
247
248218 switch (comp.config.debug_format) {
249219 .strip => {},
250220 .dwarf => |v| {
251221 var dwarf = Dwarf.init(&elf_file.base, v);
252222
253 const addSectionSymbol = struct {
254 fn addSectionSymbol(
255 zig_object: *ZigObject,
256 alloc: Allocator,
257 name: [:0]const u8,
258 alignment: Atom.Alignment,
259 shndx: u32,
260 ) !Symbol.Index {
261 const name_off = try zig_object.addString(alloc, name);
262 const index = try zig_object.newSymbolWithAtom(alloc, name_off);
263 const sym = zig_object.symbol(index);
264 const esym = &zig_object.symtab.items(.elf_sym)[sym.esym_index];
265 esym.st_info |= elf.STT_SECTION;
266 const atom_ptr = zig_object.atom(sym.ref.index).?;
267 atom_ptr.alignment = alignment;
268 atom_ptr.output_section_index = shndx;
269 return index;
270 }
271 }.addSectionSymbol;
272
273223 if (elf_file.debug_str_section_index == null) {
274224 elf_file.debug_str_section_index = try elf_file.addSection(.{
275225 .name = try elf_file.insertShString(".debug_str"),
......@@ -279,7 +229,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
279229 .addralign = 1,
280230 });
281231 self.debug_str_section_dirty = true;
282 self.debug_str_index = try addSectionSymbol(self, gpa, ".debug_str", .@"1", elf_file.debug_str_section_index.?);
232 self.debug_str_index = try self.addSectionSymbol(gpa, ".debug_str", .@"1", elf_file.debug_str_section_index.?);
283233 }
284234
285235 if (elf_file.debug_info_section_index == null) {
......@@ -289,7 +239,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
289239 .addralign = 1,
290240 });
291241 self.debug_info_section_dirty = true;
292 self.debug_info_index = try addSectionSymbol(self, gpa, ".debug_info", .@"1", elf_file.debug_info_section_index.?);
242 self.debug_info_index = try self.addSectionSymbol(gpa, ".debug_info", .@"1", elf_file.debug_info_section_index.?);
293243 }
294244
295245 if (elf_file.debug_abbrev_section_index == null) {
......@@ -299,7 +249,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
299249 .addralign = 1,
300250 });
301251 self.debug_abbrev_section_dirty = true;
302 self.debug_abbrev_index = try addSectionSymbol(self, gpa, ".debug_abbrev", .@"1", elf_file.debug_abbrev_section_index.?);
252 self.debug_abbrev_index = try self.addSectionSymbol(gpa, ".debug_abbrev", .@"1", elf_file.debug_abbrev_section_index.?);
303253 }
304254
305255 if (elf_file.debug_aranges_section_index == null) {
......@@ -309,7 +259,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
309259 .addralign = 16,
310260 });
311261 self.debug_aranges_section_dirty = true;
312 self.debug_aranges_index = try addSectionSymbol(self, gpa, ".debug_aranges", .@"16", elf_file.debug_aranges_section_index.?);
262 self.debug_aranges_index = try self.addSectionSymbol(gpa, ".debug_aranges", .@"16", elf_file.debug_aranges_section_index.?);
313263 }
314264
315265 if (elf_file.debug_line_section_index == null) {
......@@ -319,7 +269,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
319269 .addralign = 1,
320270 });
321271 self.debug_line_section_dirty = true;
322 self.debug_line_index = try addSectionSymbol(self, gpa, ".debug_line", .@"1", elf_file.debug_line_section_index.?);
272 self.debug_line_index = try self.addSectionSymbol(gpa, ".debug_line", .@"1", elf_file.debug_line_section_index.?);
323273 }
324274
325275 if (elf_file.debug_line_str_section_index == null) {
......@@ -331,7 +281,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
331281 .addralign = 1,
332282 });
333283 self.debug_line_str_section_dirty = true;
334 self.debug_line_str_index = try addSectionSymbol(self, gpa, ".debug_line_str", .@"1", elf_file.debug_line_str_section_index.?);
284 self.debug_line_str_index = try self.addSectionSymbol(gpa, ".debug_line_str", .@"1", elf_file.debug_line_str_section_index.?);
335285 }
336286
337287 if (elf_file.debug_loclists_section_index == null) {
......@@ -341,7 +291,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
341291 .addralign = 1,
342292 });
343293 self.debug_loclists_section_dirty = true;
344 self.debug_loclists_index = try addSectionSymbol(self, gpa, ".debug_loclists", .@"1", elf_file.debug_loclists_section_index.?);
294 self.debug_loclists_index = try self.addSectionSymbol(gpa, ".debug_loclists", .@"1", elf_file.debug_loclists_section_index.?);
345295 }
346296
347297 if (elf_file.debug_rnglists_section_index == null) {
......@@ -351,7 +301,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
351301 .addralign = 1,
352302 });
353303 self.debug_rnglists_section_dirty = true;
354 self.debug_rnglists_index = try addSectionSymbol(self, gpa, ".debug_rnglists", .@"1", elf_file.debug_rnglists_section_index.?);
304 self.debug_rnglists_index = try self.addSectionSymbol(gpa, ".debug_rnglists", .@"1", elf_file.debug_rnglists_section_index.?);
355305 }
356306
357307 if (elf_file.eh_frame_section_index == null) {
......@@ -365,7 +315,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
365315 .addralign = ptr_size,
366316 });
367317 self.eh_frame_section_dirty = true;
368 self.eh_frame_index = try addSectionSymbol(self, gpa, ".eh_frame", Atom.Alignment.fromNonzeroByteUnits(ptr_size), elf_file.eh_frame_section_index.?);
318 self.eh_frame_index = try self.addSectionSymbol(gpa, ".eh_frame", Atom.Alignment.fromNonzeroByteUnits(ptr_size), elf_file.eh_frame_section_index.?);
369319 }
370320
371321 try dwarf.initMetadata();
......@@ -1270,6 +1220,24 @@ pub fn getOrCreateMetadataForNav(
12701220 return gop.value_ptr.symbol_index;
12711221}
12721222
1223fn addSectionSymbol(
1224 self: *ZigObject,
1225 allocator: Allocator,
1226 name: [:0]const u8,
1227 alignment: Atom.Alignment,
1228 shndx: u32,
1229) !Symbol.Index {
1230 const name_off = try self.addString(allocator, name);
1231 const index = try self.newSymbolWithAtom(allocator, name_off);
1232 const sym = self.symbol(index);
1233 const esym = &self.symtab.items(.elf_sym)[sym.esym_index];
1234 esym.st_info |= elf.STT_SECTION;
1235 const atom_ptr = self.atom(sym.ref.index).?;
1236 atom_ptr.alignment = alignment;
1237 atom_ptr.output_section_index = shndx;
1238 return index;
1239}
1240
12731241fn getNavShdrIndex(
12741242 self: *ZigObject,
12751243 elf_file: *Elf,
......@@ -1309,12 +1277,34 @@ fn getNavShdrIndex(
13091277 if (nav_init != .none and Value.fromInterned(nav_init).isUndefDeep(zcu))
13101278 return switch (zcu.navFileScope(nav_index).mod.optimize_mode) {
13111279 .Debug, .ReleaseSafe => elf_file.zig_data_section_index.?,
1312 .ReleaseFast, .ReleaseSmall => elf_file.zig_bss_section_index.?,
1280 .ReleaseFast, .ReleaseSmall => {
1281 if (self.bss_index) |symbol_index|
1282 return self.symbol(symbol_index).atom(elf_file).?.output_section_index;
1283 const osec = try elf_file.addSection(.{
1284 .type = elf.SHT_NOBITS,
1285 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1286 .name = try elf_file.insertShString(".bss"),
1287 .addralign = 1,
1288 });
1289 self.bss_index = try self.addSectionSymbol(elf_file.base.comp.gpa, ".bss", .@"1", osec);
1290 return osec;
1291 },
13131292 };
13141293 const is_bss = !has_relocs and for (code) |byte| {
13151294 if (byte != 0) break false;
13161295 } else true;
1317 if (is_bss) return elf_file.zig_bss_section_index.?;
1296 if (is_bss) {
1297 if (self.bss_index) |symbol_index|
1298 return self.symbol(symbol_index).atom(elf_file).?.output_section_index;
1299 const osec = try elf_file.addSection(.{
1300 .type = elf.SHT_NOBITS,
1301 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1302 .name = try elf_file.insertShString(".bss"),
1303 .addralign = 1,
1304 });
1305 self.bss_index = try self.addSectionSymbol(elf_file.base.comp.gpa, ".bss", .@"1", osec);
1306 return osec;
1307 }
13181308 return elf_file.zig_data_section_index.?;
13191309}
13201310