| ... | ... | @@ -101,6 +101,28 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 101 | 101 | .dwarf => |v| { |
| 102 | 102 | var dwarf = Dwarf.init(&elf_file.base, v); |
| 103 | 103 | |
| 104 | const addSectionSymbolWithAtom = struct { |
| 105 | fn addSectionSymbolWithAtom( |
| 106 | zo: *ZigObject, |
| 107 | allocator: Allocator, |
| 108 | name: [:0]const u8, |
| 109 | alignment: Atom.Alignment, |
| 110 | shndx: u32, |
| 111 | ) !Symbol.Index { |
| 112 | const name_off = try zo.addString(allocator, name); |
| 113 | const sym_index = try zo.addSectionSymbol(allocator, name_off, shndx); |
| 114 | const sym = zo.symbol(sym_index); |
| 115 | const atom_index = try zo.newAtom(allocator, name_off); |
| 116 | const atom_ptr = zo.atom(atom_index).?; |
| 117 | atom_ptr.alignment = alignment; |
| 118 | atom_ptr.output_section_index = shndx; |
| 119 | sym.ref = .{ .index = atom_index, .file = zo.index }; |
| 120 | zo.symtab.items(.shndx)[sym.esym_index] = atom_index; |
| 121 | zo.symtab.items(.elf_sym)[sym.esym_index].st_shndx = SHN_ATOM; |
| 122 | return sym_index; |
| 123 | } |
| 124 | }.addSectionSymbolWithAtom; |
| 125 | |
| 104 | 126 | if (self.debug_str_index == null) { |
| 105 | 127 | const osec = try elf_file.addSection(.{ |
| 106 | 128 | .name = try elf_file.insertShString(".debug_str"), |
| ... | ... | @@ -110,7 +132,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 110 | 132 | .addralign = 1, |
| 111 | 133 | }); |
| 112 | 134 | self.debug_str_section_dirty = true; |
| 113 | | self.debug_str_index = try self.addSectionSymbol(gpa, ".debug_str", .@"1", osec); |
| 135 | self.debug_str_index = try addSectionSymbolWithAtom(self, gpa, ".debug_str", .@"1", osec); |
| 114 | 136 | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_str_index.?).ref; |
| 115 | 137 | } |
| 116 | 138 | |
| ... | ... | @@ -121,7 +143,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 121 | 143 | .addralign = 1, |
| 122 | 144 | }); |
| 123 | 145 | self.debug_info_section_dirty = true; |
| 124 | | self.debug_info_index = try self.addSectionSymbol(gpa, ".debug_info", .@"1", osec); |
| 146 | self.debug_info_index = try addSectionSymbolWithAtom(self, gpa, ".debug_info", .@"1", osec); |
| 125 | 147 | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_info_index.?).ref; |
| 126 | 148 | } |
| 127 | 149 | |
| ... | ... | @@ -132,7 +154,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 132 | 154 | .addralign = 1, |
| 133 | 155 | }); |
| 134 | 156 | self.debug_abbrev_section_dirty = true; |
| 135 | | self.debug_abbrev_index = try self.addSectionSymbol(gpa, ".debug_abbrev", .@"1", osec); |
| 157 | self.debug_abbrev_index = try addSectionSymbolWithAtom(self, gpa, ".debug_abbrev", .@"1", osec); |
| 136 | 158 | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_abbrev_index.?).ref; |
| 137 | 159 | } |
| 138 | 160 | |
| ... | ... | @@ -143,7 +165,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 143 | 165 | .addralign = 16, |
| 144 | 166 | }); |
| 145 | 167 | self.debug_aranges_section_dirty = true; |
| 146 | | self.debug_aranges_index = try self.addSectionSymbol(gpa, ".debug_aranges", .@"16", osec); |
| 168 | self.debug_aranges_index = try addSectionSymbolWithAtom(self, gpa, ".debug_aranges", .@"16", osec); |
| 147 | 169 | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_aranges_index.?).ref; |
| 148 | 170 | } |
| 149 | 171 | |
| ... | ... | @@ -154,7 +176,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 154 | 176 | .addralign = 1, |
| 155 | 177 | }); |
| 156 | 178 | self.debug_line_section_dirty = true; |
| 157 | | self.debug_line_index = try self.addSectionSymbol(gpa, ".debug_line", .@"1", osec); |
| 179 | self.debug_line_index = try addSectionSymbolWithAtom(self, gpa, ".debug_line", .@"1", osec); |
| 158 | 180 | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_line_index.?).ref; |
| 159 | 181 | } |
| 160 | 182 | |
| ... | ... | @@ -167,7 +189,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 167 | 189 | .addralign = 1, |
| 168 | 190 | }); |
| 169 | 191 | self.debug_line_str_section_dirty = true; |
| 170 | | self.debug_line_str_index = try self.addSectionSymbol(gpa, ".debug_line_str", .@"1", osec); |
| 192 | self.debug_line_str_index = try addSectionSymbolWithAtom(self, gpa, ".debug_line_str", .@"1", osec); |
| 171 | 193 | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_line_str_index.?).ref; |
| 172 | 194 | } |
| 173 | 195 | |
| ... | ... | @@ -178,7 +200,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 178 | 200 | .addralign = 1, |
| 179 | 201 | }); |
| 180 | 202 | self.debug_loclists_section_dirty = true; |
| 181 | | self.debug_loclists_index = try self.addSectionSymbol(gpa, ".debug_loclists", .@"1", osec); |
| 203 | self.debug_loclists_index = try addSectionSymbolWithAtom(self, gpa, ".debug_loclists", .@"1", osec); |
| 182 | 204 | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_loclists_index.?).ref; |
| 183 | 205 | } |
| 184 | 206 | |
| ... | ... | @@ -189,7 +211,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 189 | 211 | .addralign = 1, |
| 190 | 212 | }); |
| 191 | 213 | self.debug_rnglists_section_dirty = true; |
| 192 | | self.debug_rnglists_index = try self.addSectionSymbol(gpa, ".debug_rnglists", .@"1", osec); |
| 214 | self.debug_rnglists_index = try addSectionSymbolWithAtom(self, gpa, ".debug_rnglists", .@"1", osec); |
| 193 | 215 | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_rnglists_index.?).ref; |
| 194 | 216 | } |
| 195 | 217 | |
| ... | ... | @@ -204,7 +226,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 204 | 226 | .addralign = ptr_size, |
| 205 | 227 | }); |
| 206 | 228 | self.eh_frame_section_dirty = true; |
| 207 | | self.eh_frame_index = try self.addSectionSymbol(gpa, ".eh_frame", Atom.Alignment.fromNonzeroByteUnits(ptr_size), osec); |
| 229 | self.eh_frame_index = try addSectionSymbolWithAtom(self, gpa, ".eh_frame", Atom.Alignment.fromNonzeroByteUnits(ptr_size), osec); |
| 208 | 230 | elf_file.sections.items(.last_atom)[osec] = self.symbol(self.eh_frame_index.?).ref; |
| 209 | 231 | } |
| 210 | 232 | |
| ... | ... | @@ -997,7 +1019,7 @@ pub fn lowerUav( |
| 997 | 1019 | } |
| 998 | 1020 | |
| 999 | 1021 | const osec = if (self.data_relro_index) |sym_index| |
| 1000 | | self.symbol(sym_index).atom(elf_file).?.output_section_index |
| 1022 | self.symbol(sym_index).outputShndx(elf_file).? |
| 1001 | 1023 | else osec: { |
| 1002 | 1024 | const osec = try elf_file.addSection(.{ |
| 1003 | 1025 | .name = try elf_file.insertShString(".data.rel.ro"), |
| ... | ... | @@ -1006,7 +1028,7 @@ pub fn lowerUav( |
| 1006 | 1028 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 1007 | 1029 | .offset = std.math.maxInt(u64), |
| 1008 | 1030 | }); |
| 1009 | | self.data_relro_index = try self.addSectionSymbol(gpa, ".data.rel.ro", .@"1", osec); |
| 1031 | self.data_relro_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".data.rel.ro"), osec); |
| 1010 | 1032 | break :osec osec; |
| 1011 | 1033 | }; |
| 1012 | 1034 | |
| ... | ... | @@ -1112,24 +1134,14 @@ pub fn getOrCreateMetadataForNav( |
| 1112 | 1134 | return gop.value_ptr.symbol_index; |
| 1113 | 1135 | } |
| 1114 | 1136 | |
| 1115 | | // FIXME: we always create an atom to basically store size and alignment, however, this is only true for |
| 1116 | | // sections that have a single atom like the debug sections. It would be a better solution to decouple this |
| 1117 | | // concept from the atom, maybe. |
| 1118 | | fn addSectionSymbol( |
| 1119 | | self: *ZigObject, |
| 1120 | | allocator: Allocator, |
| 1121 | | name: [:0]const u8, |
| 1122 | | alignment: Atom.Alignment, |
| 1123 | | shndx: u32, |
| 1124 | | ) !Symbol.Index { |
| 1125 | | const name_off = try self.addString(allocator, name); |
| 1126 | | const index = try self.newSymbolWithAtom(allocator, name_off); |
| 1137 | fn addSectionSymbol(self: *ZigObject, allocator: Allocator, name_off: u32, shndx: u32) !Symbol.Index { |
| 1138 | const index = try self.newLocalSymbol(allocator, name_off); |
| 1127 | 1139 | const sym = self.symbol(index); |
| 1128 | 1140 | const esym = &self.symtab.items(.elf_sym)[sym.esym_index]; |
| 1129 | 1141 | esym.st_info |= elf.STT_SECTION; |
| 1130 | | const atom_ptr = self.atom(sym.ref.index).?; |
| 1131 | | atom_ptr.alignment = alignment; |
| 1132 | | atom_ptr.output_section_index = shndx; |
| 1142 | // TODO create fake shdrs? |
| 1143 | // esym.st_shndx = shndx; |
| 1144 | sym.output_section_index = shndx; |
| 1133 | 1145 | return index; |
| 1134 | 1146 | } |
| 1135 | 1147 | |
| ... | ... | @@ -1148,7 +1160,7 @@ fn getNavShdrIndex( |
| 1148 | 1160 | const nav_val = zcu.navValue(nav_index); |
| 1149 | 1161 | if (ip.isFunctionType(nav_val.typeOf(zcu).toIntern())) { |
| 1150 | 1162 | if (self.text_index) |symbol_index| |
| 1151 | | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| 1163 | return self.symbol(symbol_index).outputShndx(elf_file).?; |
| 1152 | 1164 | const osec = try elf_file.addSection(.{ |
| 1153 | 1165 | .type = elf.SHT_PROGBITS, |
| 1154 | 1166 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| ... | ... | @@ -1156,7 +1168,7 @@ fn getNavShdrIndex( |
| 1156 | 1168 | .addralign = 1, |
| 1157 | 1169 | .offset = std.math.maxInt(u64), |
| 1158 | 1170 | }); |
| 1159 | | self.text_index = try self.addSectionSymbol(gpa, ".text", .@"1", osec); |
| 1171 | self.text_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".text"), osec); |
| 1160 | 1172 | return osec; |
| 1161 | 1173 | } |
| 1162 | 1174 | const is_const, const is_threadlocal, const nav_init = switch (ip.indexToKey(nav_val.toIntern())) { |
| ... | ... | @@ -1171,18 +1183,18 @@ fn getNavShdrIndex( |
| 1171 | 1183 | } else true; |
| 1172 | 1184 | if (is_bss) { |
| 1173 | 1185 | if (self.tbss_index) |symbol_index| |
| 1174 | | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| 1186 | return self.symbol(symbol_index).outputShndx(elf_file).?; |
| 1175 | 1187 | const osec = try elf_file.addSection(.{ |
| 1176 | 1188 | .name = try elf_file.insertShString(".tbss"), |
| 1177 | 1189 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS, |
| 1178 | 1190 | .type = elf.SHT_NOBITS, |
| 1179 | 1191 | .addralign = 1, |
| 1180 | 1192 | }); |
| 1181 | | self.tbss_index = try self.addSectionSymbol(gpa, ".tbss", .@"1", osec); |
| 1193 | self.tbss_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".tbss"), osec); |
| 1182 | 1194 | return osec; |
| 1183 | 1195 | } |
| 1184 | 1196 | if (self.tdata_index) |symbol_index| |
| 1185 | | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| 1197 | return self.symbol(symbol_index).outputShndx(elf_file).?; |
| 1186 | 1198 | const osec = try elf_file.addSection(.{ |
| 1187 | 1199 | .type = elf.SHT_PROGBITS, |
| 1188 | 1200 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS, |
| ... | ... | @@ -1190,12 +1202,12 @@ fn getNavShdrIndex( |
| 1190 | 1202 | .addralign = 1, |
| 1191 | 1203 | .offset = std.math.maxInt(u64), |
| 1192 | 1204 | }); |
| 1193 | | self.tdata_index = try self.addSectionSymbol(gpa, ".tdata", .@"1", osec); |
| 1205 | self.tdata_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".tdata"), osec); |
| 1194 | 1206 | return osec; |
| 1195 | 1207 | } |
| 1196 | 1208 | if (is_const) { |
| 1197 | 1209 | if (self.data_relro_index) |symbol_index| |
| 1198 | | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| 1210 | return self.symbol(symbol_index).outputShndx(elf_file).?; |
| 1199 | 1211 | const osec = try elf_file.addSection(.{ |
| 1200 | 1212 | .name = try elf_file.insertShString(".data.rel.ro"), |
| 1201 | 1213 | .type = elf.SHT_PROGBITS, |
| ... | ... | @@ -1203,14 +1215,14 @@ fn getNavShdrIndex( |
| 1203 | 1215 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 1204 | 1216 | .offset = std.math.maxInt(u64), |
| 1205 | 1217 | }); |
| 1206 | | self.data_relro_index = try self.addSectionSymbol(gpa, ".data.rel.ro", .@"1", osec); |
| 1218 | self.data_relro_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".data.rel.ro"), osec); |
| 1207 | 1219 | return osec; |
| 1208 | 1220 | } |
| 1209 | 1221 | if (nav_init != .none and Value.fromInterned(nav_init).isUndefDeep(zcu)) |
| 1210 | 1222 | return switch (zcu.navFileScope(nav_index).mod.optimize_mode) { |
| 1211 | 1223 | .Debug, .ReleaseSafe => { |
| 1212 | 1224 | if (self.data_index) |symbol_index| |
| 1213 | | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| 1225 | return self.symbol(symbol_index).outputShndx(elf_file).?; |
| 1214 | 1226 | const osec = try elf_file.addSection(.{ |
| 1215 | 1227 | .name = try elf_file.insertShString(".data"), |
| 1216 | 1228 | .type = elf.SHT_PROGBITS, |
| ... | ... | @@ -1218,24 +1230,19 @@ fn getNavShdrIndex( |
| 1218 | 1230 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 1219 | 1231 | .offset = std.math.maxInt(u64), |
| 1220 | 1232 | }); |
| 1221 | | self.data_index = try self.addSectionSymbol( |
| 1222 | | gpa, |
| 1223 | | ".data", |
| 1224 | | Atom.Alignment.fromNonzeroByteUnits(ptr_size), |
| 1225 | | osec, |
| 1226 | | ); |
| 1233 | self.data_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".data"), osec); |
| 1227 | 1234 | return osec; |
| 1228 | 1235 | }, |
| 1229 | 1236 | .ReleaseFast, .ReleaseSmall => { |
| 1230 | 1237 | if (self.bss_index) |symbol_index| |
| 1231 | | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| 1238 | return self.symbol(symbol_index).outputShndx(elf_file).?; |
| 1232 | 1239 | const osec = try elf_file.addSection(.{ |
| 1233 | 1240 | .type = elf.SHT_NOBITS, |
| 1234 | 1241 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 1235 | 1242 | .name = try elf_file.insertShString(".bss"), |
| 1236 | 1243 | .addralign = 1, |
| 1237 | 1244 | }); |
| 1238 | | self.bss_index = try self.addSectionSymbol(gpa, ".bss", .@"1", osec); |
| 1245 | self.bss_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".bss"), osec); |
| 1239 | 1246 | return osec; |
| 1240 | 1247 | }, |
| 1241 | 1248 | }; |
| ... | ... | @@ -1244,18 +1251,18 @@ fn getNavShdrIndex( |
| 1244 | 1251 | } else true; |
| 1245 | 1252 | if (is_bss) { |
| 1246 | 1253 | if (self.bss_index) |symbol_index| |
| 1247 | | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| 1254 | return self.symbol(symbol_index).outputShndx(elf_file).?; |
| 1248 | 1255 | const osec = try elf_file.addSection(.{ |
| 1249 | 1256 | .type = elf.SHT_NOBITS, |
| 1250 | 1257 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 1251 | 1258 | .name = try elf_file.insertShString(".bss"), |
| 1252 | 1259 | .addralign = 1, |
| 1253 | 1260 | }); |
| 1254 | | self.bss_index = try self.addSectionSymbol(gpa, ".bss", .@"1", osec); |
| 1261 | self.bss_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".bss"), osec); |
| 1255 | 1262 | return osec; |
| 1256 | 1263 | } |
| 1257 | 1264 | if (self.data_index) |symbol_index| |
| 1258 | | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| 1265 | return self.symbol(symbol_index).outputShndx(elf_file).?; |
| 1259 | 1266 | const osec = try elf_file.addSection(.{ |
| 1260 | 1267 | .name = try elf_file.insertShString(".data"), |
| 1261 | 1268 | .type = elf.SHT_PROGBITS, |
| ... | ... | @@ -1263,12 +1270,7 @@ fn getNavShdrIndex( |
| 1263 | 1270 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 1264 | 1271 | .offset = std.math.maxInt(u64), |
| 1265 | 1272 | }); |
| 1266 | | self.data_index = try self.addSectionSymbol( |
| 1267 | | gpa, |
| 1268 | | ".data", |
| 1269 | | Atom.Alignment.fromNonzeroByteUnits(ptr_size), |
| 1270 | | osec, |
| 1271 | | ); |
| 1273 | self.data_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".data"), osec); |
| 1272 | 1274 | return osec; |
| 1273 | 1275 | } |
| 1274 | 1276 | |
| ... | ... | @@ -1521,7 +1523,7 @@ pub fn updateFunc( |
| 1521 | 1523 | .addralign = 1, |
| 1522 | 1524 | .offset = std.math.maxInt(u64), |
| 1523 | 1525 | }); |
| 1524 | | self.text_index = try self.addSectionSymbol(gpa, ".text", .@"1", osec); |
| 1526 | self.text_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".text"), osec); |
| 1525 | 1527 | break :osec osec; |
| 1526 | 1528 | }; |
| 1527 | 1529 | const name_off = try self.addString(gpa, name); |
| ... | ... | @@ -1688,7 +1690,7 @@ fn updateLazySymbol( |
| 1688 | 1690 | |
| 1689 | 1691 | const output_section_index = switch (sym.kind) { |
| 1690 | 1692 | .code => if (self.text_index) |sym_index| |
| 1691 | | self.symbol(sym_index).atom(elf_file).?.output_section_index |
| 1693 | self.symbol(sym_index).outputShndx(elf_file).? |
| 1692 | 1694 | else osec: { |
| 1693 | 1695 | const osec = try elf_file.addSection(.{ |
| 1694 | 1696 | .name = try elf_file.insertShString(".text"), |
| ... | ... | @@ -1697,11 +1699,11 @@ fn updateLazySymbol( |
| 1697 | 1699 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| 1698 | 1700 | .offset = std.math.maxInt(u64), |
| 1699 | 1701 | }); |
| 1700 | | self.text_index = try self.addSectionSymbol(gpa, ".text", .@"1", osec); |
| 1702 | self.text_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".text"), osec); |
| 1701 | 1703 | break :osec osec; |
| 1702 | 1704 | }, |
| 1703 | 1705 | .const_data => if (self.rodata_index) |sym_index| |
| 1704 | | self.symbol(sym_index).atom(elf_file).?.output_section_index |
| 1706 | self.symbol(sym_index).outputShndx(elf_file).? |
| 1705 | 1707 | else osec: { |
| 1706 | 1708 | const osec = try elf_file.addSection(.{ |
| 1707 | 1709 | .name = try elf_file.insertShString(".rodata"), |
| ... | ... | @@ -1710,7 +1712,7 @@ fn updateLazySymbol( |
| 1710 | 1712 | .flags = elf.SHF_ALLOC, |
| 1711 | 1713 | .offset = std.math.maxInt(u64), |
| 1712 | 1714 | }); |
| 1713 | | self.rodata_index = try self.addSectionSymbol(gpa, ".rodata", .@"1", osec); |
| 1715 | self.rodata_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".rodata"), osec); |
| 1714 | 1716 | break :osec osec; |
| 1715 | 1717 | }, |
| 1716 | 1718 | }; |
| ... | ... | @@ -2011,20 +2013,10 @@ fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void { |
| 2011 | 2013 | } |
| 2012 | 2014 | shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits().?); |
| 2013 | 2015 | |
| 2014 | | const sect_atom_ptr = for ([_]?Symbol.Index{ |
| 2015 | | self.text_index, |
| 2016 | | self.rodata_index, |
| 2017 | | self.data_relro_index, |
| 2018 | | self.data_index, |
| 2019 | | self.tdata_index, |
| 2020 | | }) |maybe_sym_index| { |
| 2021 | | const sect_sym_index = maybe_sym_index orelse continue; |
| 2022 | | const sect_atom_ptr = self.symbol(sect_sym_index).atom(elf_file).?; |
| 2023 | | if (sect_atom_ptr.output_section_index == atom_ptr.output_section_index) break sect_atom_ptr; |
| 2024 | | } else null; |
| 2025 | | if (sect_atom_ptr) |sap| { |
| 2026 | | sap.size = shdr.sh_size; |
| 2027 | | sap.alignment = Atom.Alignment.fromNonzeroByteUnits(shdr.sh_addralign); |
| 2016 | if (self.sectionSymbol(atom_ptr.output_section_index, elf_file)) |sym| { |
| 2017 | assert(sym.atom(elf_file) == null and sym.mergeSubsection(elf_file) == null); |
| 2018 | const esym = &self.symtab.items(.elf_sym)[sym.esym_index]; |
| 2019 | esym.st_size += atom_ptr.size + Elf.padToIdeal(atom_ptr.size); |
| 2028 | 2020 | } |
| 2029 | 2021 | |
| 2030 | 2022 | // This function can also reallocate an atom. |
| ... | ... | @@ -2053,10 +2045,67 @@ fn growAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void { |
| 2053 | 2045 | } |
| 2054 | 2046 | } |
| 2055 | 2047 | |
| 2048 | pub fn resetShdrIndexes(self: *ZigObject, backlinks: anytype) void { |
| 2049 | for (self.atoms_indexes.items) |atom_index| { |
| 2050 | const atom_ptr = self.atom(atom_index) orelse continue; |
| 2051 | atom_ptr.output_section_index = backlinks[atom_ptr.output_section_index]; |
| 2052 | } |
| 2053 | inline for ([_]?Symbol.Index{ |
| 2054 | self.text_index, |
| 2055 | self.rodata_index, |
| 2056 | self.data_relro_index, |
| 2057 | self.data_index, |
| 2058 | self.bss_index, |
| 2059 | self.tdata_index, |
| 2060 | self.tbss_index, |
| 2061 | self.eh_frame_index, |
| 2062 | self.debug_info_index, |
| 2063 | self.debug_abbrev_index, |
| 2064 | self.debug_aranges_index, |
| 2065 | self.debug_str_index, |
| 2066 | self.debug_line_index, |
| 2067 | self.debug_line_str_index, |
| 2068 | self.debug_loclists_index, |
| 2069 | self.debug_rnglists_index, |
| 2070 | }) |maybe_sym_index| { |
| 2071 | if (maybe_sym_index) |sym_index| { |
| 2072 | const sym = self.symbol(sym_index); |
| 2073 | sym.output_section_index = backlinks[sym.output_section_index]; |
| 2074 | } |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2056 | 2078 | pub fn asFile(self: *ZigObject) File { |
| 2057 | 2079 | return .{ .zig_object = self }; |
| 2058 | 2080 | } |
| 2059 | 2081 | |
| 2082 | pub fn sectionSymbol(self: *ZigObject, shndx: u32, elf_file: *Elf) ?*Symbol { |
| 2083 | inline for ([_]?Symbol.Index{ |
| 2084 | self.text_index, |
| 2085 | self.rodata_index, |
| 2086 | self.data_relro_index, |
| 2087 | self.data_index, |
| 2088 | self.bss_index, |
| 2089 | self.tdata_index, |
| 2090 | self.tbss_index, |
| 2091 | self.eh_frame_index, |
| 2092 | self.debug_info_index, |
| 2093 | self.debug_abbrev_index, |
| 2094 | self.debug_aranges_index, |
| 2095 | self.debug_str_index, |
| 2096 | self.debug_line_index, |
| 2097 | self.debug_line_str_index, |
| 2098 | self.debug_loclists_index, |
| 2099 | self.debug_rnglists_index, |
| 2100 | }) |maybe_sym_index| { |
| 2101 | if (maybe_sym_index) |sym_index| { |
| 2102 | const sym = self.symbol(sym_index); |
| 2103 | if (sym.outputShndx(elf_file) == shndx) return sym; |
| 2104 | } |
| 2105 | } |
| 2106 | return null; |
| 2107 | } |
| 2108 | |
| 2060 | 2109 | pub fn addString(self: *ZigObject, allocator: Allocator, string: []const u8) !u32 { |
| 2061 | 2110 | return self.strtab.insert(allocator, string); |
| 2062 | 2111 | } |