authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-10-04 10:38:43+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-09 12:38:53-07:00
logc92c72d08cfb102206594d723e29dd0616290d31
tree1ca758efd321f90419178324b44a1e0440f60d72
parentef7bac4aa58abcf860c222f15eaba8e9c8c702a4

elf: do not create atoms for section symbols that do not require it


3 files changed, 123 insertions(+), 78 deletions(-)

src/link/Elf.zig+1-6
...@@ -3472,12 +3472,7 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {...@@ -3472,12 +3472,7 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {
3472 }3472 }
3473 }3473 }
34743474
3475 if (self.zigObjectPtr()) |zo| {3475 if (self.zigObjectPtr()) |zo| zo.resetShdrIndexes(backlinks);
3476 for (zo.atoms_indexes.items) |atom_index| {
3477 const atom_ptr = zo.atom(atom_index) orelse continue;
3478 atom_ptr.output_section_index = backlinks[atom_ptr.output_section_index];
3479 }
3480 }
34813476
3482 for (self.comdat_group_sections.items) |*cg| {3477 for (self.comdat_group_sections.items) |*cg| {
3483 cg.shndx = backlinks[cg.shndx];3478 cg.shndx = backlinks[cg.shndx];
src/link/Elf/Atom.zig+2-1
...@@ -935,9 +935,10 @@ fn format2(...@@ -935,9 +935,10 @@ fn format2(
935 _ = unused_fmt_string;935 _ = unused_fmt_string;
936 const atom = ctx.atom;936 const atom = ctx.atom;
937 const elf_file = ctx.elf_file;937 const elf_file = ctx.elf_file;
938 try writer.print("atom({d}) : {s} : @{x} : shdr({d}) : align({x}) : size({x})", .{938 try writer.print("atom({d}) : {s} : @{x} : shdr({d}) : align({x}) : size({x}) : prev({}) : next({})", .{
939 atom.atom_index, atom.name(elf_file), atom.address(elf_file),939 atom.atom_index, atom.name(elf_file), atom.address(elf_file),
940 atom.output_section_index, atom.alignment.toByteUnits() orelse 0, atom.size,940 atom.output_section_index, atom.alignment.toByteUnits() orelse 0, atom.size,
941 atom.prev_atom_ref, atom.next_atom_ref,
941 });942 });
942 if (atom.fdes(elf_file).len > 0) {943 if (atom.fdes(elf_file).len > 0) {
943 try writer.writeAll(" : fdes{ ");944 try writer.writeAll(" : fdes{ ");
src/link/Elf/ZigObject.zig+120-71
...@@ -101,6 +101,28 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -101,6 +101,28 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
101 .dwarf => |v| {101 .dwarf => |v| {
102 var dwarf = Dwarf.init(&elf_file.base, v);102 var dwarf = Dwarf.init(&elf_file.base, v);
103103
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 if (self.debug_str_index == null) {126 if (self.debug_str_index == null) {
105 const osec = try elf_file.addSection(.{127 const osec = try elf_file.addSection(.{
106 .name = try elf_file.insertShString(".debug_str"),128 .name = try elf_file.insertShString(".debug_str"),
...@@ -110,7 +132,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -110,7 +132,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
110 .addralign = 1,132 .addralign = 1,
111 });133 });
112 self.debug_str_section_dirty = true;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 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_str_index.?).ref;136 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_str_index.?).ref;
115 }137 }
116138
...@@ -121,7 +143,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -121,7 +143,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
121 .addralign = 1,143 .addralign = 1,
122 });144 });
123 self.debug_info_section_dirty = true;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 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_info_index.?).ref;147 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_info_index.?).ref;
126 }148 }
127149
...@@ -132,7 +154,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -132,7 +154,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
132 .addralign = 1,154 .addralign = 1,
133 });155 });
134 self.debug_abbrev_section_dirty = true;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 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_abbrev_index.?).ref;158 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_abbrev_index.?).ref;
137 }159 }
138160
...@@ -143,7 +165,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -143,7 +165,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
143 .addralign = 16,165 .addralign = 16,
144 });166 });
145 self.debug_aranges_section_dirty = true;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 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_aranges_index.?).ref;169 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_aranges_index.?).ref;
148 }170 }
149171
...@@ -154,7 +176,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -154,7 +176,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
154 .addralign = 1,176 .addralign = 1,
155 });177 });
156 self.debug_line_section_dirty = true;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 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_line_index.?).ref;180 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_line_index.?).ref;
159 }181 }
160182
...@@ -167,7 +189,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -167,7 +189,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
167 .addralign = 1,189 .addralign = 1,
168 });190 });
169 self.debug_line_str_section_dirty = true;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 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_line_str_index.?).ref;193 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_line_str_index.?).ref;
172 }194 }
173195
...@@ -178,7 +200,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -178,7 +200,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
178 .addralign = 1,200 .addralign = 1,
179 });201 });
180 self.debug_loclists_section_dirty = true;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 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_loclists_index.?).ref;204 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_loclists_index.?).ref;
183 }205 }
184206
...@@ -189,7 +211,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -189,7 +211,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
189 .addralign = 1,211 .addralign = 1,
190 });212 });
191 self.debug_rnglists_section_dirty = true;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 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_rnglists_index.?).ref;215 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.debug_rnglists_index.?).ref;
194 }216 }
195217
...@@ -204,7 +226,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {...@@ -204,7 +226,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
204 .addralign = ptr_size,226 .addralign = ptr_size,
205 });227 });
206 self.eh_frame_section_dirty = true;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 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.eh_frame_index.?).ref;230 elf_file.sections.items(.last_atom)[osec] = self.symbol(self.eh_frame_index.?).ref;
209 }231 }
210232
...@@ -997,7 +1019,7 @@ pub fn lowerUav(...@@ -997,7 +1019,7 @@ pub fn lowerUav(
997 }1019 }
9981020
999 const osec = if (self.data_relro_index) |sym_index|1021 const osec = if (self.data_relro_index) |sym_index|
1000 self.symbol(sym_index).atom(elf_file).?.output_section_index1022 self.symbol(sym_index).outputShndx(elf_file).?
1001 else osec: {1023 else osec: {
1002 const osec = try elf_file.addSection(.{1024 const osec = try elf_file.addSection(.{
1003 .name = try elf_file.insertShString(".data.rel.ro"),1025 .name = try elf_file.insertShString(".data.rel.ro"),
...@@ -1006,7 +1028,7 @@ pub fn lowerUav(...@@ -1006,7 +1028,7 @@ pub fn lowerUav(
1006 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,1028 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1007 .offset = std.math.maxInt(u64),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 break :osec osec;1032 break :osec osec;
1011 };1033 };
10121034
...@@ -1112,24 +1134,14 @@ pub fn getOrCreateMetadataForNav(...@@ -1112,24 +1134,14 @@ pub fn getOrCreateMetadataForNav(
1112 return gop.value_ptr.symbol_index;1134 return gop.value_ptr.symbol_index;
1113}1135}
11141136
1115// FIXME: we always create an atom to basically store size and alignment, however, this is only true for1137fn addSectionSymbol(self: *ZigObject, allocator: Allocator, name_off: u32, shndx: u32) !Symbol.Index {
1116// sections that have a single atom like the debug sections. It would be a better solution to decouple this1138 const index = try self.newLocalSymbol(allocator, name_off);
1117// concept from the atom, maybe.
1118fn 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);
1127 const sym = self.symbol(index);1139 const sym = self.symbol(index);
1128 const esym = &self.symtab.items(.elf_sym)[sym.esym_index];1140 const esym = &self.symtab.items(.elf_sym)[sym.esym_index];
1129 esym.st_info |= elf.STT_SECTION;1141 esym.st_info |= elf.STT_SECTION;
1130 const atom_ptr = self.atom(sym.ref.index).?;1142 // TODO create fake shdrs?
1131 atom_ptr.alignment = alignment;1143 // esym.st_shndx = shndx;
1132 atom_ptr.output_section_index = shndx;1144 sym.output_section_index = shndx;
1133 return index;1145 return index;
1134}1146}
11351147
...@@ -1148,7 +1160,7 @@ fn getNavShdrIndex(...@@ -1148,7 +1160,7 @@ fn getNavShdrIndex(
1148 const nav_val = zcu.navValue(nav_index);1160 const nav_val = zcu.navValue(nav_index);
1149 if (ip.isFunctionType(nav_val.typeOf(zcu).toIntern())) {1161 if (ip.isFunctionType(nav_val.typeOf(zcu).toIntern())) {
1150 if (self.text_index) |symbol_index|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 const osec = try elf_file.addSection(.{1164 const osec = try elf_file.addSection(.{
1153 .type = elf.SHT_PROGBITS,1165 .type = elf.SHT_PROGBITS,
1154 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,1166 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
...@@ -1156,7 +1168,7 @@ fn getNavShdrIndex(...@@ -1156,7 +1168,7 @@ fn getNavShdrIndex(
1156 .addralign = 1,1168 .addralign = 1,
1157 .offset = std.math.maxInt(u64),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 return osec;1172 return osec;
1161 }1173 }
1162 const is_const, const is_threadlocal, const nav_init = switch (ip.indexToKey(nav_val.toIntern())) {1174 const is_const, const is_threadlocal, const nav_init = switch (ip.indexToKey(nav_val.toIntern())) {
...@@ -1171,18 +1183,18 @@ fn getNavShdrIndex(...@@ -1171,18 +1183,18 @@ fn getNavShdrIndex(
1171 } else true;1183 } else true;
1172 if (is_bss) {1184 if (is_bss) {
1173 if (self.tbss_index) |symbol_index|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 const osec = try elf_file.addSection(.{1187 const osec = try elf_file.addSection(.{
1176 .name = try elf_file.insertShString(".tbss"),1188 .name = try elf_file.insertShString(".tbss"),
1177 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,1189 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,
1178 .type = elf.SHT_NOBITS,1190 .type = elf.SHT_NOBITS,
1179 .addralign = 1,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 return osec;1194 return osec;
1183 }1195 }
1184 if (self.tdata_index) |symbol_index|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 const osec = try elf_file.addSection(.{1198 const osec = try elf_file.addSection(.{
1187 .type = elf.SHT_PROGBITS,1199 .type = elf.SHT_PROGBITS,
1188 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,1200 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,
...@@ -1190,12 +1202,12 @@ fn getNavShdrIndex(...@@ -1190,12 +1202,12 @@ fn getNavShdrIndex(
1190 .addralign = 1,1202 .addralign = 1,
1191 .offset = std.math.maxInt(u64),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 return osec;1206 return osec;
1195 }1207 }
1196 if (is_const) {1208 if (is_const) {
1197 if (self.data_relro_index) |symbol_index|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 const osec = try elf_file.addSection(.{1211 const osec = try elf_file.addSection(.{
1200 .name = try elf_file.insertShString(".data.rel.ro"),1212 .name = try elf_file.insertShString(".data.rel.ro"),
1201 .type = elf.SHT_PROGBITS,1213 .type = elf.SHT_PROGBITS,
...@@ -1203,14 +1215,14 @@ fn getNavShdrIndex(...@@ -1203,14 +1215,14 @@ fn getNavShdrIndex(
1203 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,1215 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1204 .offset = std.math.maxInt(u64),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 return osec;1219 return osec;
1208 }1220 }
1209 if (nav_init != .none and Value.fromInterned(nav_init).isUndefDeep(zcu))1221 if (nav_init != .none and Value.fromInterned(nav_init).isUndefDeep(zcu))
1210 return switch (zcu.navFileScope(nav_index).mod.optimize_mode) {1222 return switch (zcu.navFileScope(nav_index).mod.optimize_mode) {
1211 .Debug, .ReleaseSafe => {1223 .Debug, .ReleaseSafe => {
1212 if (self.data_index) |symbol_index|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 const osec = try elf_file.addSection(.{1226 const osec = try elf_file.addSection(.{
1215 .name = try elf_file.insertShString(".data"),1227 .name = try elf_file.insertShString(".data"),
1216 .type = elf.SHT_PROGBITS,1228 .type = elf.SHT_PROGBITS,
...@@ -1218,24 +1230,19 @@ fn getNavShdrIndex(...@@ -1218,24 +1230,19 @@ fn getNavShdrIndex(
1218 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,1230 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1219 .offset = std.math.maxInt(u64),1231 .offset = std.math.maxInt(u64),
1220 });1232 });
1221 self.data_index = try self.addSectionSymbol(1233 self.data_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".data"), osec);
1222 gpa,
1223 ".data",
1224 Atom.Alignment.fromNonzeroByteUnits(ptr_size),
1225 osec,
1226 );
1227 return osec;1234 return osec;
1228 },1235 },
1229 .ReleaseFast, .ReleaseSmall => {1236 .ReleaseFast, .ReleaseSmall => {
1230 if (self.bss_index) |symbol_index|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 const osec = try elf_file.addSection(.{1239 const osec = try elf_file.addSection(.{
1233 .type = elf.SHT_NOBITS,1240 .type = elf.SHT_NOBITS,
1234 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,1241 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1235 .name = try elf_file.insertShString(".bss"),1242 .name = try elf_file.insertShString(".bss"),
1236 .addralign = 1,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 return osec;1246 return osec;
1240 },1247 },
1241 };1248 };
...@@ -1244,18 +1251,18 @@ fn getNavShdrIndex(...@@ -1244,18 +1251,18 @@ fn getNavShdrIndex(
1244 } else true;1251 } else true;
1245 if (is_bss) {1252 if (is_bss) {
1246 if (self.bss_index) |symbol_index|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 const osec = try elf_file.addSection(.{1255 const osec = try elf_file.addSection(.{
1249 .type = elf.SHT_NOBITS,1256 .type = elf.SHT_NOBITS,
1250 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,1257 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1251 .name = try elf_file.insertShString(".bss"),1258 .name = try elf_file.insertShString(".bss"),
1252 .addralign = 1,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 return osec;1262 return osec;
1256 }1263 }
1257 if (self.data_index) |symbol_index|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 const osec = try elf_file.addSection(.{1266 const osec = try elf_file.addSection(.{
1260 .name = try elf_file.insertShString(".data"),1267 .name = try elf_file.insertShString(".data"),
1261 .type = elf.SHT_PROGBITS,1268 .type = elf.SHT_PROGBITS,
...@@ -1263,12 +1270,7 @@ fn getNavShdrIndex(...@@ -1263,12 +1270,7 @@ fn getNavShdrIndex(
1263 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,1270 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1264 .offset = std.math.maxInt(u64),1271 .offset = std.math.maxInt(u64),
1265 });1272 });
1266 self.data_index = try self.addSectionSymbol(1273 self.data_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".data"), osec);
1267 gpa,
1268 ".data",
1269 Atom.Alignment.fromNonzeroByteUnits(ptr_size),
1270 osec,
1271 );
1272 return osec;1274 return osec;
1273}1275}
12741276
...@@ -1521,7 +1523,7 @@ pub fn updateFunc(...@@ -1521,7 +1523,7 @@ pub fn updateFunc(
1521 .addralign = 1,1523 .addralign = 1,
1522 .offset = std.math.maxInt(u64),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 break :osec osec;1527 break :osec osec;
1526 };1528 };
1527 const name_off = try self.addString(gpa, name);1529 const name_off = try self.addString(gpa, name);
...@@ -1688,7 +1690,7 @@ fn updateLazySymbol(...@@ -1688,7 +1690,7 @@ fn updateLazySymbol(
16881690
1689 const output_section_index = switch (sym.kind) {1691 const output_section_index = switch (sym.kind) {
1690 .code => if (self.text_index) |sym_index|1692 .code => if (self.text_index) |sym_index|
1691 self.symbol(sym_index).atom(elf_file).?.output_section_index1693 self.symbol(sym_index).outputShndx(elf_file).?
1692 else osec: {1694 else osec: {
1693 const osec = try elf_file.addSection(.{1695 const osec = try elf_file.addSection(.{
1694 .name = try elf_file.insertShString(".text"),1696 .name = try elf_file.insertShString(".text"),
...@@ -1697,11 +1699,11 @@ fn updateLazySymbol(...@@ -1697,11 +1699,11 @@ fn updateLazySymbol(
1697 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,1699 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
1698 .offset = std.math.maxInt(u64),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 break :osec osec;1703 break :osec osec;
1702 },1704 },
1703 .const_data => if (self.rodata_index) |sym_index|1705 .const_data => if (self.rodata_index) |sym_index|
1704 self.symbol(sym_index).atom(elf_file).?.output_section_index1706 self.symbol(sym_index).outputShndx(elf_file).?
1705 else osec: {1707 else osec: {
1706 const osec = try elf_file.addSection(.{1708 const osec = try elf_file.addSection(.{
1707 .name = try elf_file.insertShString(".rodata"),1709 .name = try elf_file.insertShString(".rodata"),
...@@ -1710,7 +1712,7 @@ fn updateLazySymbol(...@@ -1710,7 +1712,7 @@ fn updateLazySymbol(
1710 .flags = elf.SHF_ALLOC,1712 .flags = elf.SHF_ALLOC,
1711 .offset = std.math.maxInt(u64),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 break :osec osec;1716 break :osec osec;
1715 },1717 },
1716 };1718 };
...@@ -2011,20 +2013,10 @@ fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void {...@@ -2011,20 +2013,10 @@ fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void {
2011 }2013 }
2012 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits().?);2014 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits().?);
20132015
2014 const sect_atom_ptr = for ([_]?Symbol.Index{2016 if (self.sectionSymbol(atom_ptr.output_section_index, elf_file)) |sym| {
2015 self.text_index,2017 assert(sym.atom(elf_file) == null and sym.mergeSubsection(elf_file) == null);
2016 self.rodata_index,2018 const esym = &self.symtab.items(.elf_sym)[sym.esym_index];
2017 self.data_relro_index,2019 esym.st_size += atom_ptr.size + Elf.padToIdeal(atom_ptr.size);
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);
2028 }2020 }
20292021
2030 // This function can also reallocate an atom.2022 // This function can also reallocate an atom.
...@@ -2053,10 +2045,67 @@ fn growAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void {...@@ -2053,10 +2045,67 @@ fn growAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void {
2053 }2045 }
2054}2046}
20552047
2048pub 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
2056pub fn asFile(self: *ZigObject) File {2078pub fn asFile(self: *ZigObject) File {
2057 return .{ .zig_object = self };2079 return .{ .zig_object = self };
2058}2080}
20592081
2082pub 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
2060pub fn addString(self: *ZigObject, allocator: Allocator, string: []const u8) !u32 {2109pub fn addString(self: *ZigObject, allocator: Allocator, string: []const u8) !u32 {
2061 return self.strtab.insert(allocator, string);2110 return self.strtab.insert(allocator, string);
2062}2111}