| ... | ... | @@ -60,6 +60,7 @@ debug_line_str_index: ?Symbol.Index = null, |
| 60 | 60 | debug_loclists_index: ?Symbol.Index = null, |
| 61 | 61 | debug_rnglists_index: ?Symbol.Index = null, |
| 62 | 62 | eh_frame_index: ?Symbol.Index = null, |
| 63 | bss_index: ?Symbol.Index = null, |
| 63 | 64 | |
| 64 | 65 | pub const global_symbol_bit: u32 = 0x80000000; |
| 65 | 66 | pub const symbol_mask: u32 = 0x7fffffff; |
| ... | ... | @@ -149,17 +150,6 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 149 | 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 | } |
| 164 | 154 | |
| 165 | 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 | 215 | } |
| 226 | 216 | } |
| 227 | 217 | |
| 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 | 218 | switch (comp.config.debug_format) { |
| 249 | 219 | .strip => {}, |
| 250 | 220 | .dwarf => |v| { |
| 251 | 221 | var dwarf = Dwarf.init(&elf_file.base, v); |
| 252 | 222 | |
| 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 | 223 | if (elf_file.debug_str_section_index == null) { |
| 274 | 224 | elf_file.debug_str_section_index = try elf_file.addSection(.{ |
| 275 | 225 | .name = try elf_file.insertShString(".debug_str"), |
| ... | ... | @@ -279,7 +229,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 279 | 229 | .addralign = 1, |
| 280 | 230 | }); |
| 281 | 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 | } |
| 284 | 234 | |
| 285 | 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 | 239 | .addralign = 1, |
| 290 | 240 | }); |
| 291 | 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 | } |
| 294 | 244 | |
| 295 | 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 | 249 | .addralign = 1, |
| 300 | 250 | }); |
| 301 | 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 | } |
| 304 | 254 | |
| 305 | 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 | 259 | .addralign = 16, |
| 310 | 260 | }); |
| 311 | 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 | } |
| 314 | 264 | |
| 315 | 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 | 269 | .addralign = 1, |
| 320 | 270 | }); |
| 321 | 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 | } |
| 324 | 274 | |
| 325 | 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 | 281 | .addralign = 1, |
| 332 | 282 | }); |
| 333 | 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 | } |
| 336 | 286 | |
| 337 | 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 | 291 | .addralign = 1, |
| 342 | 292 | }); |
| 343 | 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 | } |
| 346 | 296 | |
| 347 | 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 | 301 | .addralign = 1, |
| 352 | 302 | }); |
| 353 | 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 | } |
| 356 | 306 | |
| 357 | 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 | 315 | .addralign = ptr_size, |
| 366 | 316 | }); |
| 367 | 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 | } |
| 370 | 320 | |
| 371 | 321 | try dwarf.initMetadata(); |
| ... | ... | @@ -1270,6 +1220,24 @@ pub fn getOrCreateMetadataForNav( |
| 1270 | 1220 | return gop.value_ptr.symbol_index; |
| 1271 | 1221 | } |
| 1272 | 1222 | |
| 1223 | fn 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 | |
| 1273 | 1241 | fn getNavShdrIndex( |
| 1274 | 1242 | self: *ZigObject, |
| 1275 | 1243 | elf_file: *Elf, |
| ... | ... | @@ -1309,12 +1277,34 @@ fn getNavShdrIndex( |
| 1309 | 1277 | if (nav_init != .none and Value.fromInterned(nav_init).isUndefDeep(zcu)) |
| 1310 | 1278 | return switch (zcu.navFileScope(nav_index).mod.optimize_mode) { |
| 1311 | 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 | 1293 | const is_bss = !has_relocs and for (code) |byte| { |
| 1315 | 1294 | if (byte != 0) break false; |
| 1316 | 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 | 1308 | return elf_file.zig_data_section_index.?; |
| 1319 | 1309 | } |
| 1320 | 1310 | |