| ... | @@ -61,6 +61,7 @@ debug_loclists_index: ?Symbol.Index = null, | ... | @@ -61,6 +61,7 @@ debug_loclists_index: ?Symbol.Index = null, |
| 61 | debug_rnglists_index: ?Symbol.Index = null, | 61 | debug_rnglists_index: ?Symbol.Index = null, |
| 62 | eh_frame_index: ?Symbol.Index = null, | 62 | eh_frame_index: ?Symbol.Index = null, |
| 63 | bss_index: ?Symbol.Index = null, | 63 | bss_index: ?Symbol.Index = null, |
| | 64 | data_index: ?Symbol.Index = null, |
| 64 | | 65 | |
| 65 | pub const global_symbol_bit: u32 = 0x80000000; | 66 | pub const global_symbol_bit: u32 = 0x80000000; |
| 66 | pub const symbol_mask: u32 = 0x7fffffff; | 67 | pub const symbol_mask: u32 = 0x7fffffff; |
| ... | @@ -104,7 +105,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { | ... | @@ -104,7 +105,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 104 | } | 105 | } |
| 105 | }.fillSection; | 106 | }.fillSection; |
| 106 | | 107 | |
| 107 | comptime assert(Elf.number_of_zig_segments == 4); | 108 | comptime assert(Elf.number_of_zig_segments == 2); |
| 108 | | 109 | |
| 109 | if (!elf_file.base.isRelocatable()) { | 110 | if (!elf_file.base.isRelocatable()) { |
| 110 | if (elf_file.phdr_zig_load_re_index == null) { | 111 | if (elf_file.phdr_zig_load_re_index == null) { |
| ... | @@ -135,21 +136,6 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { | ... | @@ -135,21 +136,6 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 135 | .flags = elf.PF_R | elf.PF_W, | 136 | .flags = elf.PF_R | elf.PF_W, |
| 136 | }); | 137 | }); |
| 137 | } | 138 | } |
| 138 | | | |
| 139 | if (elf_file.phdr_zig_load_rw_index == null) { | | |
| 140 | const alignment = elf_file.page_size; | | |
| 141 | const filesz: u64 = 1024; | | |
| 142 | const off = try elf_file.findFreeSpace(filesz, alignment); | | |
| 143 | elf_file.phdr_zig_load_rw_index = try elf_file.addPhdr(.{ | | |
| 144 | .type = elf.PT_LOAD, | | |
| 145 | .offset = off, | | |
| 146 | .filesz = filesz, | | |
| 147 | .addr = if (ptr_size >= 4) 0x10000000 else 0xc000, | | |
| 148 | .memsz = filesz, | | |
| 149 | .@"align" = alignment, | | |
| 150 | .flags = elf.PF_R | elf.PF_W, | | |
| 151 | }); | | |
| 152 | } | | |
| 153 | } | 139 | } |
| 154 | | 140 | |
| 155 | if (elf_file.zig_text_section_index == null) { | 141 | if (elf_file.zig_text_section_index == null) { |
| ... | @@ -194,27 +180,6 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { | ... | @@ -194,27 +180,6 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 194 | } | 180 | } |
| 195 | } | 181 | } |
| 196 | | 182 | |
| 197 | if (elf_file.zig_data_section_index == null) { | | |
| 198 | elf_file.zig_data_section_index = try elf_file.addSection(.{ | | |
| 199 | .name = try elf_file.insertShString(".data.zig"), | | |
| 200 | .type = elf.SHT_PROGBITS, | | |
| 201 | .addralign = ptr_size, | | |
| 202 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, | | |
| 203 | .offset = std.math.maxInt(u64), | | |
| 204 | }); | | |
| 205 | const shdr = &elf_file.sections.items(.shdr)[elf_file.zig_data_section_index.?]; | | |
| 206 | const phndx = &elf_file.sections.items(.phndx)[elf_file.zig_data_section_index.?]; | | |
| 207 | try fillSection(elf_file, shdr, 1024, elf_file.phdr_zig_load_rw_index); | | |
| 208 | if (elf_file.base.isRelocatable()) { | | |
| 209 | _ = try elf_file.addRelaShdr( | | |
| 210 | try elf_file.insertShString(".rela.data.zig"), | | |
| 211 | elf_file.zig_data_section_index.?, | | |
| 212 | ); | | |
| 213 | } else { | | |
| 214 | phndx.* = elf_file.phdr_zig_load_rw_index.?; | | |
| 215 | } | | |
| 216 | } | | |
| 217 | | | |
| 218 | switch (comp.config.debug_format) { | 183 | switch (comp.config.debug_format) { |
| 219 | .strip => {}, | 184 | .strip => {}, |
| 220 | .dwarf => |v| { | 185 | .dwarf => |v| { |
| ... | @@ -1246,6 +1211,8 @@ fn getNavShdrIndex( | ... | @@ -1246,6 +1211,8 @@ fn getNavShdrIndex( |
| 1246 | sym_index: Symbol.Index, | 1211 | sym_index: Symbol.Index, |
| 1247 | code: []const u8, | 1212 | code: []const u8, |
| 1248 | ) error{OutOfMemory}!u32 { | 1213 | ) error{OutOfMemory}!u32 { |
| | 1214 | const gpa = elf_file.base.comp.gpa; |
| | 1215 | const ptr_size = elf_file.ptrWidthBytes(); |
| 1249 | const ip = &zcu.intern_pool; | 1216 | const ip = &zcu.intern_pool; |
| 1250 | const any_non_single_threaded = elf_file.base.comp.config.any_non_single_threaded; | 1217 | const any_non_single_threaded = elf_file.base.comp.config.any_non_single_threaded; |
| 1251 | const nav_val = zcu.navValue(nav_index); | 1218 | const nav_val = zcu.navValue(nav_index); |
| ... | @@ -1276,7 +1243,19 @@ fn getNavShdrIndex( | ... | @@ -1276,7 +1243,19 @@ fn getNavShdrIndex( |
| 1276 | if (is_const) return elf_file.zig_data_rel_ro_section_index.?; | 1243 | if (is_const) return elf_file.zig_data_rel_ro_section_index.?; |
| 1277 | if (nav_init != .none and Value.fromInterned(nav_init).isUndefDeep(zcu)) | 1244 | if (nav_init != .none and Value.fromInterned(nav_init).isUndefDeep(zcu)) |
| 1278 | return switch (zcu.navFileScope(nav_index).mod.optimize_mode) { | 1245 | return switch (zcu.navFileScope(nav_index).mod.optimize_mode) { |
| 1279 | .Debug, .ReleaseSafe => elf_file.zig_data_section_index.?, | 1246 | .Debug, .ReleaseSafe => { |
| | 1247 | if (self.data_index) |symbol_index| |
| | 1248 | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| | 1249 | const osec = try elf_file.addSection(.{ |
| | 1250 | .name = try elf_file.insertShString(".data"), |
| | 1251 | .type = elf.SHT_PROGBITS, |
| | 1252 | .addralign = ptr_size, |
| | 1253 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| | 1254 | .offset = std.math.maxInt(u64), |
| | 1255 | }); |
| | 1256 | self.data_index = try self.addSectionSymbol(gpa, ".data", .@"1", osec); |
| | 1257 | return osec; |
| | 1258 | }, |
| 1280 | .ReleaseFast, .ReleaseSmall => { | 1259 | .ReleaseFast, .ReleaseSmall => { |
| 1281 | if (self.bss_index) |symbol_index| | 1260 | if (self.bss_index) |symbol_index| |
| 1282 | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; | 1261 | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| ... | @@ -1286,7 +1265,7 @@ fn getNavShdrIndex( | ... | @@ -1286,7 +1265,7 @@ fn getNavShdrIndex( |
| 1286 | .name = try elf_file.insertShString(".bss"), | 1265 | .name = try elf_file.insertShString(".bss"), |
| 1287 | .addralign = 1, | 1266 | .addralign = 1, |
| 1288 | }); | 1267 | }); |
| 1289 | self.bss_index = try self.addSectionSymbol(elf_file.base.comp.gpa, ".bss", .@"1", osec); | 1268 | self.bss_index = try self.addSectionSymbol(gpa, ".bss", .@"1", osec); |
| 1290 | return osec; | 1269 | return osec; |
| 1291 | }, | 1270 | }, |
| 1292 | }; | 1271 | }; |
| ... | @@ -1302,10 +1281,20 @@ fn getNavShdrIndex( | ... | @@ -1302,10 +1281,20 @@ fn getNavShdrIndex( |
| 1302 | .name = try elf_file.insertShString(".bss"), | 1281 | .name = try elf_file.insertShString(".bss"), |
| 1303 | .addralign = 1, | 1282 | .addralign = 1, |
| 1304 | }); | 1283 | }); |
| 1305 | self.bss_index = try self.addSectionSymbol(elf_file.base.comp.gpa, ".bss", .@"1", osec); | 1284 | self.bss_index = try self.addSectionSymbol(gpa, ".bss", .@"1", osec); |
| 1306 | return osec; | 1285 | return osec; |
| 1307 | } | 1286 | } |
| 1308 | return elf_file.zig_data_section_index.?; | 1287 | if (self.data_index) |symbol_index| |
| | 1288 | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| | 1289 | const osec = try elf_file.addSection(.{ |
| | 1290 | .name = try elf_file.insertShString(".data"), |
| | 1291 | .type = elf.SHT_PROGBITS, |
| | 1292 | .addralign = ptr_size, |
| | 1293 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| | 1294 | .offset = std.math.maxInt(u64), |
| | 1295 | }); |
| | 1296 | self.data_index = try self.addSectionSymbol(gpa, ".data", .@"1", osec); |
| | 1297 | return osec; |
| 1309 | } | 1298 | } |
| 1310 | | 1299 | |
| 1311 | fn updateNavCode( | 1300 | fn updateNavCode( |
| ... | @@ -2014,6 +2003,16 @@ fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void { | ... | @@ -2014,6 +2003,16 @@ fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void { |
| 2014 | } | 2003 | } |
| 2015 | shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits().?); | 2004 | shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits().?); |
| 2016 | | 2005 | |
| | 2006 | const sect_atom_ptr = for ([_]?Symbol.Index{self.data_index}) |maybe_sym_index| { |
| | 2007 | const sect_sym_index = maybe_sym_index orelse continue; |
| | 2008 | const sect_atom_ptr = self.symbol(sect_sym_index).atom(elf_file).?; |
| | 2009 | if (sect_atom_ptr.output_section_index == atom_ptr.output_section_index) break sect_atom_ptr; |
| | 2010 | } else null; |
| | 2011 | if (sect_atom_ptr) |sap| { |
| | 2012 | sap.size = shdr.sh_size; |
| | 2013 | sap.alignment = Atom.Alignment.fromNonzeroByteUnits(shdr.sh_addralign); |
| | 2014 | } |
| | 2015 | |
| 2017 | // This function can also reallocate an atom. | 2016 | // This function can also reallocate an atom. |
| 2018 | // In this case we need to "unplug" it from its previous location before | 2017 | // In this case we need to "unplug" it from its previous location before |
| 2019 | // plugging it in to its new location. | 2018 | // plugging it in to its new location. |