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,...@@ -61,8 +61,6 @@ phdr_zig_load_re_index: ?u16 = null,
61phdr_zig_load_ro_index: ?u16 = null,61phdr_zig_load_ro_index: ?u16 = null,
62/// The index into the program headers of a PT_LOAD program header with Write flag62/// The index into the program headers of a PT_LOAD program header with Write flag
63phdr_zig_load_rw_index: ?u16 = null,63phdr_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
67/// Special program headers65/// Special program headers
68/// PT_PHDR66/// PT_PHDR
...@@ -129,7 +127,6 @@ comdat_group_sections: std.ArrayListUnmanaged(ComdatGroupSection) = .{},...@@ -129,7 +127,6 @@ comdat_group_sections: std.ArrayListUnmanaged(ComdatGroupSection) = .{},
129zig_text_section_index: ?u32 = null,127zig_text_section_index: ?u32 = null,
130zig_data_rel_ro_section_index: ?u32 = null,128zig_data_rel_ro_section_index: ?u32 = null,
131zig_data_section_index: ?u32 = null,129zig_data_section_index: ?u32 = null,
132zig_bss_section_index: ?u32 = null,
133130
134debug_info_section_index: ?u32 = null,131debug_info_section_index: ?u32 = null,
135debug_abbrev_section_index: ?u32 = null,132debug_abbrev_section_index: ?u32 = null,
...@@ -3367,7 +3364,6 @@ fn sortPhdrs(self: *Elf) error{OutOfMemory}!void {...@@ -3367,7 +3364,6 @@ fn sortPhdrs(self: *Elf) error{OutOfMemory}!void {
3367 for (&[_]*?u16{3364 for (&[_]*?u16{
3368 &self.phdr_zig_load_re_index,3365 &self.phdr_zig_load_re_index,
3369 &self.phdr_zig_load_ro_index,3366 &self.phdr_zig_load_ro_index,
3370 &self.phdr_zig_load_zerofill_index,
3371 &self.phdr_table_index,3367 &self.phdr_table_index,
3372 &self.phdr_table_load_index,3368 &self.phdr_table_load_index,
3373 &self.phdr_interp_index,3369 &self.phdr_interp_index,
...@@ -3496,7 +3492,6 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {...@@ -3496,7 +3492,6 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {
3496 &self.zig_text_section_index,3492 &self.zig_text_section_index,
3497 &self.zig_data_rel_ro_section_index,3493 &self.zig_data_rel_ro_section_index,
3498 &self.zig_data_section_index,3494 &self.zig_data_section_index,
3499 &self.zig_bss_section_index,
3500 &self.debug_info_section_index,3495 &self.debug_info_section_index,
3501 &self.debug_abbrev_section_index,3496 &self.debug_abbrev_section_index,
3502 &self.debug_str_section_index,3497 &self.debug_str_section_index,
...@@ -3591,9 +3586,17 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {...@@ -3591,9 +3586,17 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {
35913586
3592fn updateSectionSizes(self: *Elf) !void {3587fn updateSectionSizes(self: *Elf) !void {
3593 const slice = self.sections.slice();3588 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| {
3595 if (atom_list.items.len == 0) continue;3590 if (atom_list.items.len == 0) continue;
3596 if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;3591 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 }
3597 for (atom_list.items) |ref| {3600 for (atom_list.items) |ref| {
3598 const atom_ptr = self.atom(ref) orelse continue;3601 const atom_ptr = self.atom(ref) orelse continue;
3599 if (!atom_ptr.alive) continue;3602 if (!atom_ptr.alive) continue;
...@@ -4804,7 +4807,6 @@ pub fn isZigSection(self: Elf, shndx: u32) bool {...@@ -4804,7 +4807,6 @@ pub fn isZigSection(self: Elf, shndx: u32) bool {
4804 self.zig_text_section_index,4807 self.zig_text_section_index,
4805 self.zig_data_rel_ro_section_index,4808 self.zig_data_rel_ro_section_index,
4806 self.zig_data_section_index,4809 self.zig_data_section_index,
4807 self.zig_bss_section_index,
4808 }) |index| {4810 }) |index| {
4809 if (index == shndx) return true;4811 if (index == shndx) return true;
4810 }4812 }
src/link/Elf/ZigObject.zig+52-62
...@@ -60,6 +60,7 @@ debug_line_str_index: ?Symbol.Index = null,...@@ -60,6 +60,7 @@ debug_line_str_index: ?Symbol.Index = null,
60debug_loclists_index: ?Symbol.Index = null,60debug_loclists_index: ?Symbol.Index = null,
61debug_rnglists_index: ?Symbol.Index = null,61debug_rnglists_index: ?Symbol.Index = null,
62eh_frame_index: ?Symbol.Index = null,62eh_frame_index: ?Symbol.Index = null,
63bss_index: ?Symbol.Index = null,
6364
64pub const global_symbol_bit: u32 = 0x80000000;65pub const global_symbol_bit: u32 = 0x80000000;
65pub const symbol_mask: u32 = 0x7fffffff;66pub const symbol_mask: u32 = 0x7fffffff;
...@@ -149,17 +150,6 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -149,17 +150,6 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
149 .flags = elf.PF_R | elf.PF_W,150 .flags = elf.PF_R | elf.PF_W,
150 });151 });
151 }152 }
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 }
163 }153 }
164154
165 if (elf_file.zig_text_section_index == null) {155 if (elf_file.zig_text_section_index == null) {
...@@ -225,51 +215,11 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -225,51 +215,11 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
225 }215 }
226 }216 }
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
248 switch (comp.config.debug_format) {218 switch (comp.config.debug_format) {
249 .strip => {},219 .strip => {},
250 .dwarf => |v| {220 .dwarf => |v| {
251 var dwarf = Dwarf.init(&elf_file.base, v);221 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
273 if (elf_file.debug_str_section_index == null) {223 if (elf_file.debug_str_section_index == null) {
274 elf_file.debug_str_section_index = try elf_file.addSection(.{224 elf_file.debug_str_section_index = try elf_file.addSection(.{
275 .name = try elf_file.insertShString(".debug_str"),225 .name = try elf_file.insertShString(".debug_str"),
...@@ -279,7 +229,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -279,7 +229,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
279 .addralign = 1,229 .addralign = 1,
280 });230 });
281 self.debug_str_section_dirty = true;231 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.?);
283 }233 }
284234
285 if (elf_file.debug_info_section_index == null) {235 if (elf_file.debug_info_section_index == null) {
...@@ -289,7 +239,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -289,7 +239,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
289 .addralign = 1,239 .addralign = 1,
290 });240 });
291 self.debug_info_section_dirty = true;241 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.?);
293 }243 }
294244
295 if (elf_file.debug_abbrev_section_index == null) {245 if (elf_file.debug_abbrev_section_index == null) {
...@@ -299,7 +249,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -299,7 +249,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
299 .addralign = 1,249 .addralign = 1,
300 });250 });
301 self.debug_abbrev_section_dirty = true;251 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.?);
303 }253 }
304254
305 if (elf_file.debug_aranges_section_index == null) {255 if (elf_file.debug_aranges_section_index == null) {
...@@ -309,7 +259,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -309,7 +259,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
309 .addralign = 16,259 .addralign = 16,
310 });260 });
311 self.debug_aranges_section_dirty = true;261 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.?);
313 }263 }
314264
315 if (elf_file.debug_line_section_index == null) {265 if (elf_file.debug_line_section_index == null) {
...@@ -319,7 +269,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -319,7 +269,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
319 .addralign = 1,269 .addralign = 1,
320 });270 });
321 self.debug_line_section_dirty = true;271 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.?);
323 }273 }
324274
325 if (elf_file.debug_line_str_section_index == null) {275 if (elf_file.debug_line_str_section_index == null) {
...@@ -331,7 +281,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -331,7 +281,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
331 .addralign = 1,281 .addralign = 1,
332 });282 });
333 self.debug_line_str_section_dirty = true;283 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.?);
335 }285 }
336286
337 if (elf_file.debug_loclists_section_index == null) {287 if (elf_file.debug_loclists_section_index == null) {
...@@ -341,7 +291,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -341,7 +291,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
341 .addralign = 1,291 .addralign = 1,
342 });292 });
343 self.debug_loclists_section_dirty = true;293 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.?);
345 }295 }
346296
347 if (elf_file.debug_rnglists_section_index == null) {297 if (elf_file.debug_rnglists_section_index == null) {
...@@ -351,7 +301,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -351,7 +301,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
351 .addralign = 1,301 .addralign = 1,
352 });302 });
353 self.debug_rnglists_section_dirty = true;303 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.?);
355 }305 }
356306
357 if (elf_file.eh_frame_section_index == null) {307 if (elf_file.eh_frame_section_index == null) {
...@@ -365,7 +315,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -365,7 +315,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
365 .addralign = ptr_size,315 .addralign = ptr_size,
366 });316 });
367 self.eh_frame_section_dirty = true;317 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.?);
369 }319 }
370320
371 try dwarf.initMetadata();321 try dwarf.initMetadata();
...@@ -1270,6 +1220,24 @@ pub fn getOrCreateMetadataForNav(...@@ -1270,6 +1220,24 @@ pub fn getOrCreateMetadataForNav(
1270 return gop.value_ptr.symbol_index;1220 return gop.value_ptr.symbol_index;
1271}1221}
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
1273fn getNavShdrIndex(1241fn getNavShdrIndex(
1274 self: *ZigObject,1242 self: *ZigObject,
1275 elf_file: *Elf,1243 elf_file: *Elf,
...@@ -1309,12 +1277,34 @@ fn getNavShdrIndex(...@@ -1309,12 +1277,34 @@ fn getNavShdrIndex(
1309 if (nav_init != .none and Value.fromInterned(nav_init).isUndefDeep(zcu))1277 if (nav_init != .none and Value.fromInterned(nav_init).isUndefDeep(zcu))
1310 return switch (zcu.navFileScope(nav_index).mod.optimize_mode) {1278 return switch (zcu.navFileScope(nav_index).mod.optimize_mode) {
1311 .Debug, .ReleaseSafe => elf_file.zig_data_section_index.?,1279 .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 },
1313 };1292 };
1314 const is_bss = !has_relocs and for (code) |byte| {1293 const is_bss = !has_relocs and for (code) |byte| {
1315 if (byte != 0) break false;1294 if (byte != 0) break false;
1316 } else true;1295 } 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 }
1318 return elf_file.zig_data_section_index.?;1308 return elf_file.zig_data_section_index.?;
1319}1309}
13201310