1//! ZigObject encapsulates the state of the incrementally compiled Zig module.
2//! It stores the associated input local and global symbols, allocated atoms,
3//! and any relocations that may have been emitted.
4//! Think about this as fake in-memory Object file for the Zig module.
5
6data: std.ArrayList(u8) = .empty,
7/// Externally owned memory.
8basename: []const u8,
9index: File.Index,
10
11symtab: std.MultiArrayList(ElfSym) = .{},
12strtab: StringTable = .{},
13symbols: std.ArrayList(Symbol) = .empty,
14symbols_extra: std.ArrayList(u32) = .empty,
15symbols_resolver: std.ArrayList(Elf.SymbolResolver.Index) = .empty,
16local_symbols: std.ArrayList(Symbol.Index) = .empty,
17global_symbols: std.ArrayList(Symbol.Index) = .empty,
18globals_lookup: std.AutoHashMapUnmanaged(u32, Symbol.Index) = .empty,
19
20atoms: std.ArrayList(Atom) = .empty,
21atoms_indexes: std.ArrayList(Atom.Index) = .empty,
22atoms_extra: std.ArrayList(u32) = .empty,
23relocs: std.ArrayList(std.ArrayList(elf.Elf64_Rela)) = .empty,
24
25num_dynrelocs: u32 = 0,
26
27output_symtab_ctx: Elf.SymtabCtx = .{},
28output_ar_state: Archive.ArState = .{},
29
30dwarf: ?Dwarf = null,
31
32/// Table of tracked LazySymbols.
33lazy_syms: LazySymbolTable = .{},
34
35/// Table of tracked `Nav`s.
36navs: NavTable = .{},
37
38/// TLS variables indexed by Atom.Index.
39tls_variables: TlsTable = .{},
40
41/// Table of tracked `Uav`s.
42uavs: UavTable = .{},
43
44debug_info_section_dirty: bool = false,
45debug_abbrev_section_dirty: bool = false,
46debug_aranges_section_dirty: bool = false,
47debug_str_section_dirty: bool = false,
48debug_line_section_dirty: bool = false,
49debug_line_str_section_dirty: bool = false,
50debug_loclists_section_dirty: bool = false,
51debug_rnglists_section_dirty: bool = false,
52eh_frame_section_dirty: bool = false,
53
54text_index: ?Symbol.Index = null,
55rodata_index: ?Symbol.Index = null,
56data_relro_index: ?Symbol.Index = null,
57data_index: ?Symbol.Index = null,
58bss_index: ?Symbol.Index = null,
59tdata_index: ?Symbol.Index = null,
60tbss_index: ?Symbol.Index = null,
61eh_frame_index: ?Symbol.Index = null,
62debug_info_index: ?Symbol.Index = null,
63debug_abbrev_index: ?Symbol.Index = null,
64debug_aranges_index: ?Symbol.Index = null,
65debug_str_index: ?Symbol.Index = null,
66debug_line_index: ?Symbol.Index = null,
67debug_line_str_index: ?Symbol.Index = null,
68debug_loclists_index: ?Symbol.Index = null,
69debug_rnglists_index: ?Symbol.Index = null,
70
71pub const global_symbol_bit: u32 = 0x80000000;
72pub const symbol_mask: u32 = 0x7fffffff;
73pub const SHN_ATOM: u16 = 0x100;
74
75const InitOptions = struct {
76 symbol_count_hint: u64,
77 program_code_size_hint: u64,
78};
79
80pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void {
81 _ = options;
82 const comp = elf_file.base.comp;
83 const gpa = comp.gpa;
84 const ptr_size = elf_file.ptrWidthBytes();
85
86 try self.atoms.append(gpa, .{ .extra_index = try self.addAtomExtra(gpa, .{}) }); // null input section
87 try self.relocs.append(gpa, .empty); // null relocs section
88 try self.strtab.buffer.append(gpa, 0);
89
90 {
91 const name_off = try self.strtab.insert(gpa, self.basename);
92 const symbol_index = try self.newLocalSymbol(gpa, name_off);
93 const sym = self.symbol(symbol_index);
94 const esym = &self.symtab.items(.elf_sym)[sym.esym_index];
95 esym.st_info = elf.STT_FILE;
96 esym.st_shndx = elf.SHN_ABS;
97 }
98
99 switch (comp.config.debug_format) {
100 .strip => {},
101 .dwarf => |v| {
102 var dwarf = Dwarf.init(&elf_file.base, v);
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
126 if (self.debug_str_index == null) {
127 const osec = try elf_file.addSection(.{
128 .name = try elf_file.insertShString(".debug_str"),
129 .flags = elf.SHF_MERGE | elf.SHF_STRINGS,
130 .entsize = 1,
131 .type = elf.SHT_PROGBITS,
132 .addralign = 1,
133 });
134 self.debug_str_section_dirty = true;
135 self.debug_str_index = try addSectionSymbolWithAtom(self, gpa, ".debug_str", .@"1", osec);
136 }
137
138 if (self.debug_info_index == null) {
139 const osec = try elf_file.addSection(.{
140 .name = try elf_file.insertShString(".debug_info"),
141 .type = elf.SHT_PROGBITS,
142 .addralign = 1,
143 });
144 self.debug_info_section_dirty = true;
145 self.debug_info_index = try addSectionSymbolWithAtom(self, gpa, ".debug_info", .@"1", osec);
146 }
147
148 if (self.debug_abbrev_index == null) {
149 const osec = try elf_file.addSection(.{
150 .name = try elf_file.insertShString(".debug_abbrev"),
151 .type = elf.SHT_PROGBITS,
152 .addralign = 1,
153 });
154 self.debug_abbrev_section_dirty = true;
155 self.debug_abbrev_index = try addSectionSymbolWithAtom(self, gpa, ".debug_abbrev", .@"1", osec);
156 }
157
158 if (self.debug_aranges_index == null) {
159 const osec = try elf_file.addSection(.{
160 .name = try elf_file.insertShString(".debug_aranges"),
161 .type = elf.SHT_PROGBITS,
162 .addralign = 16,
163 });
164 self.debug_aranges_section_dirty = true;
165 self.debug_aranges_index = try addSectionSymbolWithAtom(self, gpa, ".debug_aranges", .@"16", osec);
166 }
167
168 if (self.debug_line_index == null) {
169 const osec = try elf_file.addSection(.{
170 .name = try elf_file.insertShString(".debug_line"),
171 .type = elf.SHT_PROGBITS,
172 .addralign = 1,
173 });
174 self.debug_line_section_dirty = true;
175 self.debug_line_index = try addSectionSymbolWithAtom(self, gpa, ".debug_line", .@"1", osec);
176 }
177
178 if (self.debug_line_str_index == null) {
179 const osec = try elf_file.addSection(.{
180 .name = try elf_file.insertShString(".debug_line_str"),
181 .flags = elf.SHF_MERGE | elf.SHF_STRINGS,
182 .entsize = 1,
183 .type = elf.SHT_PROGBITS,
184 .addralign = 1,
185 });
186 self.debug_line_str_section_dirty = true;
187 self.debug_line_str_index = try addSectionSymbolWithAtom(self, gpa, ".debug_line_str", .@"1", osec);
188 }
189
190 if (self.debug_loclists_index == null) {
191 const osec = try elf_file.addSection(.{
192 .name = try elf_file.insertShString(".debug_loclists"),
193 .type = elf.SHT_PROGBITS,
194 .addralign = 1,
195 });
196 self.debug_loclists_section_dirty = true;
197 self.debug_loclists_index = try addSectionSymbolWithAtom(self, gpa, ".debug_loclists", .@"1", osec);
198 }
199
200 if (self.debug_rnglists_index == null) {
201 const osec = try elf_file.addSection(.{
202 .name = try elf_file.insertShString(".debug_rnglists"),
203 .type = elf.SHT_PROGBITS,
204 .addralign = 1,
205 });
206 self.debug_rnglists_section_dirty = true;
207 self.debug_rnglists_index = try addSectionSymbolWithAtom(self, gpa, ".debug_rnglists", .@"1", osec);
208 }
209
210 if (self.eh_frame_index == null) {
211 const osec = try elf_file.addSection(.{
212 .name = try elf_file.insertShString(".eh_frame"),
213 .type = if (elf_file.getTarget().cpu.arch == .x86_64)
214 elf.SHT_X86_64_UNWIND
215 else
216 elf.SHT_PROGBITS,
217 .flags = elf.SHF_ALLOC,
218 .addralign = ptr_size,
219 });
220 self.eh_frame_section_dirty = true;
221 self.eh_frame_index = try addSectionSymbolWithAtom(self, gpa, ".eh_frame", Atom.Alignment.fromNonzeroByteUnits(ptr_size), osec);
222 }
223
224 try dwarf.initMetadata();
225 self.dwarf = dwarf;
226 },
227 .code_view => unreachable,
228 }
229}
230
231pub fn deinit(self: *ZigObject, allocator: Allocator) void {
232 self.data.deinit(allocator);
233 self.symtab.deinit(allocator);
234 self.strtab.deinit(allocator);
235 self.symbols.deinit(allocator);
236 self.symbols_extra.deinit(allocator);
237 self.symbols_resolver.deinit(allocator);
238 self.local_symbols.deinit(allocator);
239 self.global_symbols.deinit(allocator);
240 self.globals_lookup.deinit(allocator);
241 self.atoms.deinit(allocator);
242 self.atoms_indexes.deinit(allocator);
243 self.atoms_extra.deinit(allocator);
244 for (self.relocs.items) |*list| {
245 list.deinit(allocator);
246 }
247 self.relocs.deinit(allocator);
248
249 for (self.navs.values()) |*meta| {
250 meta.exports.deinit(allocator);
251 }
252 self.navs.deinit(allocator);
253
254 self.lazy_syms.deinit(allocator);
255
256 for (self.uavs.values()) |*meta| {
257 meta.exports.deinit(allocator);
258 }
259 self.uavs.deinit(allocator);
260 self.tls_variables.deinit(allocator);
261
262 if (self.dwarf) |*dwarf| {
263 dwarf.deinit();
264 }
265}
266
267pub fn flush(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !void {
268 // Handle any lazy symbols that were emitted by incremental compilation.
269 if (self.lazy_syms.getPtr(.anyerror_type)) |metadata| {
270 const active = elf_file.base.comp.zcu.?.activate(tid);
271 defer active.deactivate();
272 const pt = active.pt;
273
274 // Most lazy symbols can be updated on first use, but
275 // anyerror needs to wait for everything to be flushed.
276 if (metadata.text_state != .unused) try self.updateLazySymbol(
277 elf_file,
278 pt,
279 .{ .kind = .code, .ty = .anyerror_type },
280 metadata.text_symbol_index,
281 );
282 if (metadata.rodata_state != .unused) try self.updateLazySymbol(
283 elf_file,
284 pt,
285 .{ .kind = .const_data, .ty = .anyerror_type },
286 metadata.rodata_symbol_index,
287 );
288 }
289 for (self.lazy_syms.values()) |*metadata| {
290 if (metadata.text_state != .unused) metadata.text_state = .flushed;
291 if (metadata.rodata_state != .unused) metadata.rodata_state = .flushed;
292 }
293
294 if (build_options.enable_logging) {
295 const active = elf_file.base.comp.zcu.?.activate(tid);
296 defer active.deactivate();
297 for (self.navs.keys(), self.navs.values()) |nav_index, meta| {
298 checkNavAllocated(active.pt, nav_index, meta);
299 }
300 for (self.uavs.keys(), self.uavs.values()) |uav_index, meta| {
301 checkUavAllocated(active.pt, uav_index, meta);
302 }
303 }
304
305 if (self.dwarf) |*dwarf| {
306 {
307 const active = elf_file.base.comp.zcu.?.activate(tid);
308 defer active.deactivate();
309 try dwarf.flush(active.pt);
310 }
311
312 const gpa = elf_file.base.comp.gpa;
313 const cpu_arch = elf_file.getTarget().cpu.arch;
314
315 // TODO invert this logic so that we manage the output section with the atom, not the
316 // other way around
317 for ([_]u32{
318 self.debug_info_index.?,
319 self.debug_abbrev_index.?,
320 self.debug_str_index.?,
321 self.debug_aranges_index.?,
322 self.debug_line_index.?,
323 self.debug_line_str_index.?,
324 self.debug_loclists_index.?,
325 self.debug_rnglists_index.?,
326 self.eh_frame_index.?,
327 }, [_]*Dwarf.Section{
328 &dwarf.debug_info.section,
329 &dwarf.debug_abbrev.section,
330 &dwarf.debug_str.section,
331 &dwarf.debug_aranges.section,
332 &dwarf.debug_line.section,
333 &dwarf.debug_line_str.section,
334 &dwarf.debug_loclists.section,
335 &dwarf.debug_rnglists.section,
336 &dwarf.debug_frame.section,
337 }, [_]Dwarf.Section.Index{
338 .debug_info,
339 .debug_abbrev,
340 .debug_str,
341 .debug_aranges,
342 .debug_line,
343 .debug_line_str,
344 .debug_loclists,
345 .debug_rnglists,
346 .debug_frame,
347 }) |sym_index, sect, sect_index| {
348 const sym = self.symbol(sym_index);
349 const atom_ptr = self.atom(sym.ref.index).?;
350 if (!atom_ptr.alive) continue;
351
352 const relocs = &self.relocs.items[atom_ptr.relocsShndx().?];
353 for (sect.units.items) |*unit| {
354 try relocs.ensureUnusedCapacity(gpa, unit.cross_unit_relocs.items.len +
355 unit.cross_section_relocs.items.len);
356 for (unit.cross_unit_relocs.items) |reloc| {
357 const target_unit = sect.getUnit(reloc.target_unit);
358 const r_offset = unit.off + reloc.source_off;
359 const r_addend: i64 = @intCast(target_unit.off + reloc.target_off + (if (reloc.target_entry.unwrap()) |target_entry|
360 target_unit.header_len + target_unit.getEntry(target_entry).assertNonEmpty(target_unit, sect, dwarf).off
361 else
362 0));
363 const r_type = relocation.dwarf.crossSectionRelocType(dwarf.format, cpu_arch);
364 atom_ptr.addRelocAssumeCapacity(.{
365 .r_offset = r_offset,
366 .r_addend = r_addend,
367 .r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
368 }, self);
369 }
370 for (unit.cross_section_relocs.items) |reloc| {
371 const target_sym_index = switch (reloc.target_sec) {
372 .debug_abbrev => self.debug_abbrev_index.?,
373 .debug_aranges => self.debug_aranges_index.?,
374 .debug_frame => self.eh_frame_index.?,
375 .debug_info => self.debug_info_index.?,
376 .debug_line => self.debug_line_index.?,
377 .debug_line_str => self.debug_line_str_index.?,
378 .debug_loclists => self.debug_loclists_index.?,
379 .debug_rnglists => self.debug_rnglists_index.?,
380 .debug_str => self.debug_str_index.?,
381 };
382 const target_sec = switch (reloc.target_sec) {
383 inline else => |target_sec| &@field(dwarf, @tagName(target_sec)).section,
384 };
385 const target_unit = target_sec.getUnit(reloc.target_unit);
386 const r_offset = unit.off + reloc.source_off;
387 const r_addend: i64 = @intCast(target_unit.off + reloc.target_off + (if (reloc.target_entry.unwrap()) |target_entry|
388 target_unit.header_len + target_unit.getEntry(target_entry).assertNonEmpty(target_unit, sect, dwarf).off
389 else
390 0));
391 const r_type = relocation.dwarf.crossSectionRelocType(dwarf.format, cpu_arch);
392 atom_ptr.addRelocAssumeCapacity(.{
393 .r_offset = r_offset,
394 .r_addend = r_addend,
395 .r_info = (@as(u64, @intCast(target_sym_index)) << 32) | r_type,
396 }, self);
397 }
398
399 for (unit.entries.items) |*entry| {
400 const entry_off = unit.off + unit.header_len + entry.off;
401
402 try relocs.ensureUnusedCapacity(gpa, entry.cross_entry_relocs.items.len +
403 entry.cross_unit_relocs.items.len + entry.cross_section_relocs.items.len +
404 entry.external_relocs.items.len);
405 for (entry.cross_entry_relocs.items) |reloc| {
406 const r_offset = entry_off + reloc.source_off;
407 const r_addend: i64 = @intCast(unit.off + reloc.target_off + (if (reloc.target_entry.unwrap()) |target_entry|
408 unit.header_len + unit.getEntry(target_entry).assertNonEmpty(unit, sect, dwarf).off
409 else
410 0));
411 const r_type = relocation.dwarf.crossSectionRelocType(dwarf.format, cpu_arch);
412 atom_ptr.addRelocAssumeCapacity(.{
413 .r_offset = r_offset,
414 .r_addend = r_addend,
415 .r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
416 }, self);
417 }
418 for (entry.cross_unit_relocs.items) |reloc| {
419 const target_unit = sect.getUnit(reloc.target_unit);
420 const r_offset = entry_off + reloc.source_off;
421 const r_addend: i64 = @intCast(target_unit.off + reloc.target_off + (if (reloc.target_entry.unwrap()) |target_entry|
422 target_unit.header_len + target_unit.getEntry(target_entry).assertNonEmpty(target_unit, sect, dwarf).off
423 else
424 0));
425 const r_type = relocation.dwarf.crossSectionRelocType(dwarf.format, cpu_arch);
426 atom_ptr.addRelocAssumeCapacity(.{
427 .r_offset = r_offset,
428 .r_addend = r_addend,
429 .r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
430 }, self);
431 }
432 for (entry.cross_section_relocs.items) |reloc| {
433 const target_sym_index = switch (reloc.target_sec) {
434 .debug_abbrev => self.debug_abbrev_index.?,
435 .debug_aranges => self.debug_aranges_index.?,
436 .debug_frame => self.eh_frame_index.?,
437 .debug_info => self.debug_info_index.?,
438 .debug_line => self.debug_line_index.?,
439 .debug_line_str => self.debug_line_str_index.?,
440 .debug_loclists => self.debug_loclists_index.?,
441 .debug_rnglists => self.debug_rnglists_index.?,
442 .debug_str => self.debug_str_index.?,
443 };
444 const target_sec = switch (reloc.target_sec) {
445 inline else => |target_sec| &@field(dwarf, @tagName(target_sec)).section,
446 };
447 const target_unit = target_sec.getUnit(reloc.target_unit);
448 const r_offset = entry_off + reloc.source_off;
449 const r_addend: i64 = @intCast(target_unit.off + reloc.target_off + (if (reloc.target_entry.unwrap()) |target_entry|
450 target_unit.header_len + target_unit.getEntry(target_entry).assertNonEmpty(target_unit, sect, dwarf).off
451 else
452 0));
453 const r_type = relocation.dwarf.crossSectionRelocType(dwarf.format, cpu_arch);
454 atom_ptr.addRelocAssumeCapacity(.{
455 .r_offset = r_offset,
456 .r_addend = r_addend,
457 .r_info = (@as(u64, @intCast(target_sym_index)) << 32) | r_type,
458 }, self);
459 }
460 for (entry.external_relocs.items) |reloc| {
461 const target_sym = self.symbol(@backingInt(reloc.target_sym));
462 const r_offset = entry_off + reloc.source_off;
463 const r_addend: i64 = @intCast(reloc.target_off);
464 const r_type = relocation.dwarf.externalRelocType(target_sym.*, sect_index, dwarf.address_size, cpu_arch);
465 atom_ptr.addRelocAssumeCapacity(.{
466 .r_offset = r_offset,
467 .r_addend = r_addend,
468 .r_info = (@as(u64, @intCast(@backingInt(reloc.target_sym))) << 32) | r_type,
469 }, self);
470 }
471 }
472 }
473 }
474
475 self.debug_abbrev_section_dirty = false;
476 self.debug_aranges_section_dirty = false;
477 self.debug_rnglists_section_dirty = false;
478 self.debug_str_section_dirty = false;
479 }
480
481 // The point of flush() is to commit changes, so in theory, nothing should
482 // be dirty after this. However, it is possible for some things to remain
483 // dirty because they fail to be written in the event of compile errors,
484 // such as debug_line_header_dirty and debug_info_header_dirty.
485 assert(!self.debug_abbrev_section_dirty);
486 assert(!self.debug_aranges_section_dirty);
487 assert(!self.debug_rnglists_section_dirty);
488 assert(!self.debug_str_section_dirty);
489}
490
491fn newSymbol(self: *ZigObject, allocator: Allocator, name_off: u32, st_bind: u4) !Symbol.Index {
492 try self.symtab.ensureUnusedCapacity(allocator, 1);
493 try self.symbols.ensureUnusedCapacity(allocator, 1);
494 try self.symbols_extra.ensureUnusedCapacity(allocator, @sizeOf(Symbol.Extra));
495
496 const index = self.addSymbolAssumeCapacity();
497 const sym = &self.symbols.items[index];
498 sym.name_offset = name_off;
499 sym.extra_index = self.addSymbolExtraAssumeCapacity(.{});
500
501 const esym_idx: u32 = @intCast(self.symtab.addOneAssumeCapacity());
502 const esym = ElfSym{ .elf_sym = .{
503 .st_value = 0,
504 .st_name = name_off,
505 .st_info = @as(u8, @intCast(st_bind)) << 4,
506 .st_other = 0,
507 .st_size = 0,
508 .st_shndx = 0,
509 } };
510 self.symtab.set(index, esym);
511 sym.esym_index = esym_idx;
512
513 return index;
514}
515
516fn newLocalSymbol(self: *ZigObject, allocator: Allocator, name_off: u32) !Symbol.Index {
517 try self.local_symbols.ensureUnusedCapacity(allocator, 1);
518 const fake_index: Symbol.Index = @intCast(self.local_symbols.items.len);
519 const index = try self.newSymbol(allocator, name_off, elf.STB_LOCAL);
520 self.local_symbols.appendAssumeCapacity(index);
521 return fake_index;
522}
523
524fn newGlobalSymbol(self: *ZigObject, allocator: Allocator, name_off: u32) !Symbol.Index {
525 try self.global_symbols.ensureUnusedCapacity(allocator, 1);
526 try self.symbols_resolver.ensureUnusedCapacity(allocator, 1);
527 const fake_index: Symbol.Index = @intCast(self.global_symbols.items.len);
528 const index = try self.newSymbol(allocator, name_off, elf.STB_GLOBAL);
529 self.global_symbols.appendAssumeCapacity(index);
530 self.symbols_resolver.addOneAssumeCapacity().* = 0;
531 return fake_index | global_symbol_bit;
532}
533
534fn newAtom(self: *ZigObject, allocator: Allocator, name_off: u32) !Atom.Index {
535 try self.atoms.ensureUnusedCapacity(allocator, 1);
536 try self.atoms_extra.ensureUnusedCapacity(allocator, @sizeOf(Atom.Extra));
537 try self.atoms_indexes.ensureUnusedCapacity(allocator, 1);
538 try self.relocs.ensureUnusedCapacity(allocator, 1);
539
540 const index = self.addAtomAssumeCapacity();
541 self.atoms_indexes.appendAssumeCapacity(index);
542 const atom_ptr = self.atom(index).?;
543 atom_ptr.name_offset = name_off;
544
545 const relocs_index: u32 = @intCast(self.relocs.items.len);
546 self.relocs.addOneAssumeCapacity().* = .empty;
547 atom_ptr.relocs_section_index = relocs_index;
548
549 return index;
550}
551
552fn newSymbolWithAtom(self: *ZigObject, allocator: Allocator, name_off: u32) !Symbol.Index {
553 const atom_index = try self.newAtom(allocator, name_off);
554 const sym_index = try self.newLocalSymbol(allocator, name_off);
555 const sym = self.symbol(sym_index);
556 sym.ref = .{ .index = atom_index, .file = self.index };
557 self.symtab.items(.shndx)[sym.esym_index] = atom_index;
558 self.symtab.items(.elf_sym)[sym.esym_index].st_shndx = SHN_ATOM;
559 return sym_index;
560}
561
562/// TODO actually create fake input shdrs and return that instead.
563pub fn inputShdr(self: *ZigObject, atom_index: Atom.Index, elf_file: *Elf) elf.Elf64_Shdr {
564 const atom_ptr = self.atom(atom_index) orelse return Elf.null_shdr;
565 const shndx = atom_ptr.output_section_index;
566 var shdr = elf_file.sections.items(.shdr)[shndx];
567 shdr.sh_addr = 0;
568 shdr.sh_offset = 0;
569 shdr.sh_size = atom_ptr.size;
570 shdr.sh_addralign = atom_ptr.alignment.toByteUnits() orelse 1;
571 return shdr;
572}
573
574pub fn resolveSymbols(self: *ZigObject, elf_file: *Elf) !void {
575 const gpa = elf_file.base.comp.gpa;
576
577 for (self.global_symbols.items, 0..) |index, i| {
578 const global = &self.symbols.items[index];
579 const esym = global.elfSym(elf_file);
580 const shndx = self.symtab.items(.shndx)[global.esym_index];
581 const resolv = &self.symbols_resolver.items[i];
582 const gop = try elf_file.resolver.getOrPut(gpa, .{
583 .index = @intCast(i | global_symbol_bit),
584 .file = self.index,
585 }, elf_file);
586 if (!gop.found_existing) {
587 gop.ref.* = .{ .index = 0, .file = 0 };
588 }
589 resolv.* = gop.index;
590
591 if (esym.st_shndx == elf.SHN_UNDEF) continue;
592 if (esym.st_shndx != elf.SHN_ABS and esym.st_shndx != elf.SHN_COMMON) {
593 assert(esym.st_shndx == SHN_ATOM);
594 const atom_ptr = self.atom(shndx) orelse continue;
595 if (!atom_ptr.alive) continue;
596 }
597 if (elf_file.symbol(gop.ref.*) == null) {
598 gop.ref.* = .{ .index = @intCast(i | global_symbol_bit), .file = self.index };
599 continue;
600 }
601
602 if (self.asFile().symbolRank(esym, false) < elf_file.symbol(gop.ref.*).?.symbolRank(elf_file)) {
603 gop.ref.* = .{ .index = @intCast(i | global_symbol_bit), .file = self.index };
604 }
605 }
606}
607
608pub fn claimUnresolved(self: *ZigObject, elf_file: *Elf) void {
609 for (self.global_symbols.items, 0..) |index, i| {
610 const global = &self.symbols.items[index];
611 const esym = self.symtab.items(.elf_sym)[index];
612 if (esym.st_shndx != elf.SHN_UNDEF) continue;
613 if (elf_file.symbol(self.resolveSymbol(@intCast(i | global_symbol_bit), elf_file)) != null) continue;
614
615 const is_import = blk: {
616 if (!elf_file.isEffectivelyDynLib()) break :blk false;
617 const vis: elf.STV = @fromBackingInt(@intCast(@as(u3, @truncate(esym.st_other))));
618 if (vis == .HIDDEN) break :blk false;
619 break :blk true;
620 };
621
622 global.value = 0;
623 global.ref = .{ .index = 0, .file = 0 };
624 global.esym_index = @intCast(index);
625 global.file_index = self.index;
626 global.version_index = if (is_import) .LOCAL else elf_file.default_sym_version;
627 global.flags.import = is_import;
628
629 const idx = self.symbols_resolver.items[i];
630 elf_file.resolver.values.items[idx - 1] = .{ .index = @intCast(i | global_symbol_bit), .file = self.index };
631 }
632}
633
634pub fn claimUnresolvedRelocatable(self: ZigObject, elf_file: *Elf) void {
635 for (self.global_symbols.items, 0..) |index, i| {
636 const global = &self.symbols.items[index];
637 const esym = self.symtab.items(.elf_sym)[index];
638 if (esym.st_shndx != elf.SHN_UNDEF) continue;
639 if (elf_file.symbol(self.resolveSymbol(@intCast(i | global_symbol_bit), elf_file)) != null) continue;
640
641 global.value = 0;
642 global.ref = .{ .index = 0, .file = 0 };
643 global.esym_index = @intCast(index);
644 global.file_index = self.index;
645
646 const idx = self.symbols_resolver.items[i];
647 elf_file.resolver.values.items[idx - 1] = .{ .index = @intCast(i | global_symbol_bit), .file = self.index };
648 }
649}
650
651pub fn scanRelocs(self: *ZigObject, elf_file: *Elf, undefs: anytype) !void {
652 const gpa = elf_file.base.comp.gpa;
653 for (self.atoms_indexes.items) |atom_index| {
654 const atom_ptr = self.atom(atom_index) orelse continue;
655 if (!atom_ptr.alive) continue;
656 const shdr = atom_ptr.inputShdr(elf_file);
657 if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue;
658 if (shdr.sh_type == elf.SHT_NOBITS) continue;
659 if (atom_ptr.scanRelocsRequiresCode(elf_file)) {
660 // TODO ideally we don't have to fetch the code here.
661 // Perhaps it would make sense to save the code until flush where we
662 // would free all of generated code?
663 const code = try self.codeAlloc(elf_file, atom_index);
664 defer gpa.free(code);
665 try atom_ptr.scanRelocs(elf_file, code, undefs);
666 } else try atom_ptr.scanRelocs(elf_file, null, undefs);
667 }
668}
669
670pub fn markLive(self: *ZigObject, elf_file: *Elf) void {
671 for (self.global_symbols.items, 0..) |index, i| {
672 const global = self.symbols.items[index];
673 const esym = self.symtab.items(.elf_sym)[index];
674 if (esym.st_bind() == elf.STB_WEAK) continue;
675
676 const ref = self.resolveSymbol(@intCast(i | global_symbol_bit), elf_file);
677 const sym = elf_file.symbol(ref) orelse continue;
678 const file = sym.file(elf_file).?;
679 const should_keep = esym.st_shndx == elf.SHN_UNDEF or
680 (esym.st_shndx == elf.SHN_COMMON and global.elfSym(elf_file).st_shndx != elf.SHN_COMMON);
681 if (should_keep and !file.isAlive()) {
682 file.setAlive();
683 file.markLive(elf_file);
684 }
685 }
686}
687
688pub fn markImportsExports(self: *ZigObject, elf_file: *Elf) void {
689 for (0..self.global_symbols.items.len) |i| {
690 const ref = self.resolveSymbol(@intCast(i | global_symbol_bit), elf_file);
691 const sym = elf_file.symbol(ref) orelse continue;
692 const file = sym.file(elf_file).?;
693 if (sym.version_index == elf.Versym.LOCAL) continue;
694 const vis: elf.STV = @fromBackingInt(@intCast(@as(u3, @truncate(sym.elfSym(elf_file).st_other))));
695 if (vis == .HIDDEN) continue;
696 if (file == .shared_object and !sym.isAbs(elf_file)) {
697 sym.flags.import = true;
698 continue;
699 }
700 if (file.index() == self.index) {
701 sym.flags.@"export" = true;
702 if (elf_file.isEffectivelyDynLib() and vis != .PROTECTED) {
703 sym.flags.import = true;
704 }
705 }
706 }
707}
708
709pub fn checkDuplicates(self: *ZigObject, dupes: anytype, elf_file: *Elf) error{OutOfMemory}!void {
710 const gpa = elf_file.base.comp.gpa;
711
712 for (self.global_symbols.items, 0..) |index, i| {
713 const esym = self.symtab.items(.elf_sym)[index];
714 const shndx = self.symtab.items(.shndx)[index];
715 const ref = self.resolveSymbol(@intCast(i | global_symbol_bit), elf_file);
716 const ref_sym = elf_file.symbol(ref) orelse continue;
717 const ref_file = ref_sym.file(elf_file).?;
718
719 if (self.index == ref_file.index() or
720 esym.st_shndx == elf.SHN_UNDEF or
721 esym.st_bind() == elf.STB_WEAK or
722 esym.st_shndx == elf.SHN_COMMON) continue;
723
724 if (esym.st_shndx == SHN_ATOM) {
725 const atom_ptr = self.atom(shndx) orelse continue;
726 if (!atom_ptr.alive) continue;
727 }
728
729 const gop = try dupes.getOrPut(gpa, self.symbols_resolver.items[i]);
730 if (!gop.found_existing) {
731 gop.value_ptr.* = .empty;
732 }
733 try gop.value_ptr.append(elf_file.base.comp.gpa, self.index);
734 }
735}
736
737/// This is just a temporary helper function that allows us to re-read what we wrote to file into a buffer.
738/// We need this so that we can write to an archive.
739/// TODO implement writing ZigObject data directly to a buffer instead.
740pub fn readFileContents(self: *ZigObject, elf_file: *Elf) !void {
741 const comp = elf_file.base.comp;
742 const gpa = comp.gpa;
743 const io = comp.io;
744 const shsize: u64 = switch (elf_file.ptr_width) {
745 .p32 => @sizeOf(elf.Elf32_Shdr),
746 .p64 => @sizeOf(elf.Elf64_Shdr),
747 };
748 var end_pos: u64 = elf_file.shdr_table_offset.? + elf_file.sections.items(.shdr).len * shsize;
749 for (elf_file.sections.items(.shdr)) |shdr| {
750 if (shdr.sh_type == elf.SHT_NOBITS) continue;
751 end_pos = @max(end_pos, shdr.sh_offset + shdr.sh_size);
752 }
753 const size = std.math.cast(usize, end_pos) orelse return error.Overflow;
754 try self.data.resize(gpa, size);
755
756 const amt = try elf_file.base.file.?.readPositionalAll(io, self.data.items, 0);
757 if (amt != size) return error.InputOutput;
758}
759
760pub fn updateArSymtab(self: ZigObject, ar_symtab: *Archive.ArSymtab, elf_file: *Elf) error{OutOfMemory}!void {
761 const gpa = elf_file.base.comp.gpa;
762
763 try ar_symtab.symtab.ensureUnusedCapacity(gpa, self.global_symbols.items.len);
764
765 for (self.global_symbols.items, 0..) |index, i| {
766 const global = self.symbols.items[index];
767 const ref = self.resolveSymbol(@intCast(i | global_symbol_bit), elf_file);
768 const sym = elf_file.symbol(ref).?;
769 assert(sym.file(elf_file).?.index() == self.index);
770 if (global.outputShndx(elf_file) == null) continue;
771
772 const off = try ar_symtab.strtab.insert(gpa, global.name(elf_file));
773 ar_symtab.symtab.appendAssumeCapacity(.{ .off = off, .file_index = self.index });
774 }
775}
776
777pub fn updateArSize(self: *ZigObject) void {
778 self.output_ar_state.size = self.data.items.len;
779}
780
781pub fn writeAr(self: ZigObject, writer: anytype) !void {
782 const name = self.basename;
783 const hdr = Archive.setArHdr(.{
784 .name = if (name.len <= Archive.max_member_name_len)
785 .{ .name = name }
786 else
787 .{ .name_off = self.output_ar_state.name_off },
788 .size = self.data.items.len,
789 });
790 try writer.writeAll(mem.asBytes(&hdr));
791 try writer.writeAll(self.data.items);
792}
793
794pub fn initRelaSections(self: *ZigObject, elf_file: *Elf) !void {
795 const gpa = elf_file.base.comp.gpa;
796 for (self.atoms_indexes.items) |atom_index| {
797 const atom_ptr = self.atom(atom_index) orelse continue;
798 if (!atom_ptr.alive) continue;
799 if (atom_ptr.output_section_index == elf_file.section_indexes.eh_frame) continue;
800 const rela_shndx = atom_ptr.relocsShndx() orelse continue;
801 // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level
802 if (self.relocs.items[rela_shndx].items.len == 0) continue;
803 const out_shndx = atom_ptr.output_section_index;
804 const out_shdr = elf_file.sections.items(.shdr)[out_shndx];
805 if (out_shdr.sh_type == elf.SHT_NOBITS) continue;
806 const rela_sect_name = try std.fmt.allocPrintSentinel(gpa, ".rela{s}", .{
807 elf_file.getShString(out_shdr.sh_name),
808 }, 0);
809 defer gpa.free(rela_sect_name);
810 _ = elf_file.sectionByName(rela_sect_name) orelse
811 try elf_file.addRelaShdr(try elf_file.insertShString(rela_sect_name), out_shndx);
812 }
813}
814
815pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
816 const gpa = elf_file.base.comp.gpa;
817 for (self.atoms_indexes.items) |atom_index| {
818 const atom_ptr = self.atom(atom_index) orelse continue;
819 if (!atom_ptr.alive) continue;
820 if (atom_ptr.output_section_index == elf_file.section_indexes.eh_frame) continue;
821 const rela_shndx = atom_ptr.relocsShndx() orelse continue;
822 // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level
823 if (self.relocs.items[rela_shndx].items.len == 0) continue;
824 const out_shndx = atom_ptr.output_section_index;
825 const out_shdr = elf_file.sections.items(.shdr)[out_shndx];
826 if (out_shdr.sh_type == elf.SHT_NOBITS) continue;
827 const rela_sect_name = try std.fmt.allocPrintSentinel(gpa, ".rela{s}", .{
828 elf_file.getShString(out_shdr.sh_name),
829 }, 0);
830 defer gpa.free(rela_sect_name);
831 const out_rela_shndx = elf_file.sectionByName(rela_sect_name).?;
832 const out_rela_shdr = &elf_file.sections.items(.shdr)[out_rela_shndx];
833 out_rela_shdr.sh_info = out_shndx;
834 out_rela_shdr.sh_link = elf_file.section_indexes.symtab.?;
835 const atom_list = &elf_file.sections.items(.atom_list)[out_rela_shndx];
836 try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
837 }
838}
839
840pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {
841 for (self.local_symbols.items) |index| {
842 const local = &self.symbols.items[index];
843 if (local.atom(elf_file)) |atom_ptr| if (!atom_ptr.alive) continue;
844 const name = local.name(elf_file);
845 assert(name.len > 0);
846 const esym = local.elfSym(elf_file);
847 switch (esym.st_type()) {
848 elf.STT_SECTION, elf.STT_NOTYPE => continue,
849 else => {},
850 }
851 local.flags.output_symtab = true;
852 local.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
853 self.output_symtab_ctx.nlocals += 1;
854 self.output_symtab_ctx.strsize += @as(u32, @intCast(name.len)) + 1;
855 }
856
857 for (self.global_symbols.items, self.symbols_resolver.items) |index, resolv| {
858 const global = &self.symbols.items[index];
859 const ref = elf_file.resolver.values.items[resolv - 1];
860 const ref_sym = elf_file.symbol(ref) orelse continue;
861 if (ref_sym.file(elf_file).?.index() != self.index) continue;
862 if (global.atom(elf_file)) |atom_ptr| if (!atom_ptr.alive) continue;
863 global.flags.output_symtab = true;
864 if (global.isLocal(elf_file)) {
865 global.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
866 self.output_symtab_ctx.nlocals += 1;
867 } else {
868 global.addExtra(.{ .symtab = self.output_symtab_ctx.nglobals }, elf_file);
869 self.output_symtab_ctx.nglobals += 1;
870 }
871 self.output_symtab_ctx.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1;
872 }
873}
874
875pub fn writeSymtab(self: ZigObject, elf_file: *Elf) void {
876 for (self.local_symbols.items) |index| {
877 const local = &self.symbols.items[index];
878 const idx = local.outputSymtabIndex(elf_file) orelse continue;
879 const out_sym = &elf_file.symtab.items[idx];
880 out_sym.st_name = @intCast(elf_file.strtab.items.len);
881 elf_file.strtab.appendSliceAssumeCapacity(local.name(elf_file));
882 elf_file.strtab.appendAssumeCapacity(0);
883 local.setOutputSym(elf_file, out_sym);
884 }
885
886 for (self.global_symbols.items, self.symbols_resolver.items) |index, resolv| {
887 const global = self.symbols.items[index];
888 const ref = elf_file.resolver.values.items[resolv - 1];
889 const ref_sym = elf_file.symbol(ref) orelse continue;
890 if (ref_sym.file(elf_file).?.index() != self.index) continue;
891 const idx = global.outputSymtabIndex(elf_file) orelse continue;
892 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
893 elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
894 elf_file.strtab.appendAssumeCapacity(0);
895 const out_sym = &elf_file.symtab.items[idx];
896 out_sym.st_name = st_name;
897 global.setOutputSym(elf_file, out_sym);
898 }
899}
900
901/// Returns atom's code.
902/// Caller owns the memory.
903pub fn codeAlloc(self: *ZigObject, elf_file: *Elf, atom_index: Atom.Index) ![]u8 {
904 const comp = elf_file.base.comp;
905 const gpa = comp.gpa;
906 const io = comp.io;
907 const atom_ptr = self.atom(atom_index).?;
908 const file_offset = atom_ptr.offset(elf_file);
909 const size = std.math.cast(usize, atom_ptr.size) orelse return error.Overflow;
910 const code = try gpa.alloc(u8, size);
911 errdefer gpa.free(code);
912 const amt = try elf_file.base.file.?.readPositionalAll(io, code, file_offset);
913 if (amt != code.len) {
914 log.err("fetching code for {s} failed", .{atom_ptr.name(elf_file)});
915 return error.InputOutput;
916 }
917 return code;
918}
919
920pub fn getNavVAddr(
921 self: *ZigObject,
922 elf_file: *Elf,
923 pt: Zcu.PerThread,
924 nav_index: InternPool.Nav.Index,
925 reloc_info: link.File.RelocInfo,
926) !u64 {
927 const zcu = pt.zcu;
928 const ip = &zcu.intern_pool;
929 const nav = ip.getNav(nav_index);
930 log.debug("getNavVAddr {f}({d})", .{ nav.fqn.fmt(ip), nav_index });
931 const this_sym_index = if (nav.getExtern(ip)) |@"extern"| try self.getGlobalSymbol(
932 elf_file,
933 nav.name.toSlice(ip),
934 @"extern".lib_name.toSlice(ip),
935 ) else try self.getOrCreateMetadataForNav(zcu, nav_index);
936 const this_sym = self.symbol(this_sym_index);
937 const vaddr = this_sym.address(.{}, elf_file);
938 switch (reloc_info.parent) {
939 .none => unreachable,
940 .atom_index => |atom_index| {
941 const parent_atom = self.symbol(@backingInt(atom_index)).atom(elf_file).?;
942 const r_type = relocation.encode(.abs, elf_file.getTarget().cpu.arch);
943 try parent_atom.addReloc(elf_file.base.comp.gpa, .{
944 .r_offset = reloc_info.offset,
945 .r_info = (@as(u64, @intCast(this_sym_index)) << 32) | r_type,
946 .r_addend = reloc_info.addend,
947 }, self);
948 },
949 .debug_output => |debug_output| try debug_output.dwarf.infoExternalReloc(.{
950 .source_off = @intCast(reloc_info.offset),
951 .target_sym = @fromBackingInt(@intCast(this_sym_index)),
952 .target_off = reloc_info.addend,
953 }),
954 }
955 return @intCast(vaddr);
956}
957
958pub fn getUavVAddr(
959 self: *ZigObject,
960 elf_file: *Elf,
961 uav: InternPool.Index,
962 reloc_info: link.File.RelocInfo,
963) !u64 {
964 const sym_index = self.uavs.get(uav).?.symbol_index;
965 const sym = self.symbol(sym_index);
966 const vaddr = sym.address(.{}, elf_file);
967 switch (reloc_info.parent) {
968 .none => unreachable,
969 .atom_index => |atom_index| {
970 const parent_atom = self.symbol(@backingInt(atom_index)).atom(elf_file).?;
971 const r_type = relocation.encode(.abs, elf_file.getTarget().cpu.arch);
972 try parent_atom.addReloc(elf_file.base.comp.gpa, .{
973 .r_offset = reloc_info.offset,
974 .r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
975 .r_addend = reloc_info.addend,
976 }, self);
977 },
978 .debug_output => |debug_output| try debug_output.dwarf.infoExternalReloc(.{
979 .source_off = @intCast(reloc_info.offset),
980 .target_sym = @fromBackingInt(@intCast(sym_index)),
981 .target_off = reloc_info.addend,
982 }),
983 }
984 return @intCast(vaddr);
985}
986
987pub fn lowerUav(
988 self: *ZigObject,
989 elf_file: *Elf,
990 pt: Zcu.PerThread,
991 uav: InternPool.Index,
992 explicit_alignment: InternPool.Alignment,
993) !link.File.SymbolId {
994 const zcu = pt.zcu;
995 const gpa = zcu.gpa;
996 const val = Value.fromInterned(uav);
997 const uav_alignment = switch (explicit_alignment) {
998 .none => val.typeOf(zcu).abiAlignment(zcu),
999 else => explicit_alignment,
1000 };
1001 if (self.uavs.get(uav)) |metadata| {
1002 assert(metadata.allocated);
1003 const sym = self.symbol(metadata.symbol_index);
1004 const existing_alignment = sym.atom(elf_file).?.alignment;
1005 if (uav_alignment.order(existing_alignment).compare(.lte))
1006 return @fromBackingInt(@intCast(metadata.symbol_index));
1007 }
1008
1009 const osec = if (self.data_relro_index) |sym_index|
1010 self.symbol(sym_index).outputShndx(elf_file).?
1011 else osec: {
1012 const osec = try elf_file.addSection(.{
1013 .name = try elf_file.insertShString(".data.rel.ro"),
1014 .type = elf.SHT_PROGBITS,
1015 .addralign = 1,
1016 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1017 });
1018 self.data_relro_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".data.rel.ro"), osec);
1019 break :osec osec;
1020 };
1021
1022 var name_buf: [32]u8 = undefined;
1023 const name = std.mem.print(&name_buf, "__anon_{d}", .{
1024 @backingInt(uav),
1025 }) catch unreachable;
1026 const sym_index = self.lowerConst(
1027 elf_file,
1028 pt,
1029 name,
1030 val,
1031 uav_alignment,
1032 osec,
1033 ) catch |err| switch (err) {
1034 error.OutOfMemory => |e| return e,
1035 else => |e| return elf_file.base.comp.link_diags.fail(
1036 "failed to lower constant value: {t}",
1037 .{e},
1038 ),
1039 };
1040 try self.uavs.put(gpa, uav, .{
1041 .symbol_index = @backingInt(sym_index),
1042 .allocated = true,
1043 });
1044 return sym_index;
1045}
1046
1047pub fn getOrCreateMetadataForLazySymbol(
1048 self: *ZigObject,
1049 elf_file: *Elf,
1050 pt: Zcu.PerThread,
1051 lazy_sym: link.File.LazySymbol,
1052) !Symbol.Index {
1053 const gop = try self.lazy_syms.getOrPut(pt.zcu.gpa, lazy_sym.ty);
1054 errdefer _ = if (!gop.found_existing) self.lazy_syms.pop();
1055 if (!gop.found_existing) gop.value_ptr.* = .{};
1056 const symbol_index_ptr, const state_ptr = switch (lazy_sym.kind) {
1057 .code => .{ &gop.value_ptr.text_symbol_index, &gop.value_ptr.text_state },
1058 .const_data => .{ &gop.value_ptr.rodata_symbol_index, &gop.value_ptr.rodata_state },
1059 };
1060 switch (state_ptr.*) {
1061 .unused => symbol_index_ptr.* = try self.newSymbolWithAtom(pt.zcu.gpa, 0),
1062 .pending_flush => return symbol_index_ptr.*,
1063 .flushed => {},
1064 }
1065 state_ptr.* = .pending_flush;
1066 const symbol_index = symbol_index_ptr.*;
1067 // anyerror needs to be deferred until flush
1068 if (lazy_sym.ty != .anyerror_type) try self.updateLazySymbol(elf_file, pt, lazy_sym, symbol_index);
1069 return symbol_index;
1070}
1071
1072fn freeNavMetadata(self: *ZigObject, elf_file: *Elf, sym_index: Symbol.Index) void {
1073 const sym = self.symbol(sym_index);
1074 sym.atom(elf_file).?.free(elf_file);
1075 log.debug("adding %{d} to local symbols free list", .{sym_index});
1076 self.symbols.items[sym_index] = .{};
1077 // TODO free GOT entry here
1078}
1079
1080pub fn freeNav(self: *ZigObject, elf_file: *Elf, nav_index: InternPool.Nav.Index) void {
1081 const gpa = elf_file.base.comp.gpa;
1082
1083 log.debug("freeNav ({d})", .{nav_index});
1084
1085 if (self.navs.fetchRemove(nav_index)) |const_kv| {
1086 var kv = const_kv;
1087 const sym_index = kv.value.symbol_index;
1088 self.freeNavMetadata(elf_file, sym_index);
1089 kv.value.exports.deinit(gpa);
1090 }
1091
1092 if (self.dwarf) |*dwarf| {
1093 dwarf.freeNav(nav_index);
1094 }
1095}
1096
1097pub fn getOrCreateMetadataForNav(self: *ZigObject, zcu: *Zcu, nav_index: InternPool.Nav.Index) !Symbol.Index {
1098 const gpa = zcu.gpa;
1099 const ip = &zcu.intern_pool;
1100 const gop = try self.navs.getOrPut(gpa, nav_index);
1101 if (!gop.found_existing) {
1102 const symbol_index = try self.newSymbolWithAtom(gpa, 0);
1103 const sym = self.symbol(symbol_index);
1104 if (ip.getNav(nav_index).resolved.?.@"threadlocal" and zcu.comp.config.any_non_single_threaded) {
1105 sym.flags.is_tls = true;
1106 }
1107 gop.value_ptr.* = .{ .symbol_index = symbol_index };
1108 }
1109 return gop.value_ptr.symbol_index;
1110}
1111
1112fn addSectionSymbol(self: *ZigObject, allocator: Allocator, name_off: u32, shndx: u32) !Symbol.Index {
1113 const index = try self.newLocalSymbol(allocator, name_off);
1114 const sym = self.symbol(index);
1115 const esym = &self.symtab.items(.elf_sym)[sym.esym_index];
1116 esym.st_info |= elf.STT_SECTION;
1117 // TODO create fake shdrs?
1118 // esym.st_shndx = shndx;
1119 sym.output_section_index = shndx;
1120 return index;
1121}
1122
1123fn getNavShdrIndex(
1124 self: *ZigObject,
1125 elf_file: *Elf,
1126 zcu: *Zcu,
1127 nav_index: InternPool.Nav.Index,
1128 sym_index: Symbol.Index,
1129 code: []const u8,
1130) error{OutOfMemory}!u32 {
1131 const gpa = elf_file.base.comp.gpa;
1132 const ptr_size = elf_file.ptrWidthBytes();
1133 const ip = &zcu.intern_pool;
1134 const nav = ip.getNav(nav_index);
1135 const nav_val: Value = .fromInterned(nav.resolved.?.value);
1136 const is_func = ip.isFunctionType(nav_val.typeOf(zcu).toIntern());
1137 if (ip.getNav(nav_index).resolved.?.@"linksection".unwrap()) |@"linksection"| {
1138 const section_name = @"linksection".toSlice(ip);
1139 if (elf_file.sectionByName(section_name)) |osec| {
1140 if (is_func) {
1141 elf_file.sections.items(.shdr)[osec].sh_flags |= elf.SHF_EXECINSTR;
1142 } else {
1143 elf_file.sections.items(.shdr)[osec].sh_flags |= elf.SHF_WRITE;
1144 }
1145 return osec;
1146 }
1147 const osec = try elf_file.addSection(.{
1148 .type = elf.SHT_PROGBITS,
1149 .flags = elf.SHF_ALLOC | @as(u64, if (is_func) elf.SHF_EXECINSTR else elf.SHF_WRITE),
1150 .name = try elf_file.insertShString(section_name),
1151 .addralign = 1,
1152 });
1153 const section_index = try self.addSectionSymbol(gpa, try self.addString(gpa, section_name), osec);
1154 if (std.mem.eql(u8, section_name, ".text")) {
1155 elf_file.sections.items(.shdr)[osec].sh_flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR;
1156 self.text_index = section_index;
1157 } else if (std.mem.startsWith(u8, section_name, ".text.")) {
1158 elf_file.sections.items(.shdr)[osec].sh_flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR;
1159 } else if (std.mem.eql(u8, section_name, ".rodata")) {
1160 elf_file.sections.items(.shdr)[osec].sh_flags = elf.SHF_ALLOC;
1161 self.rodata_index = section_index;
1162 } else if (std.mem.startsWith(u8, section_name, ".rodata.")) {
1163 elf_file.sections.items(.shdr)[osec].sh_flags = elf.SHF_ALLOC;
1164 } else if (std.mem.eql(u8, section_name, ".data.rel.ro")) {
1165 elf_file.sections.items(.shdr)[osec].sh_flags = elf.SHF_ALLOC | elf.SHF_WRITE;
1166 self.data_relro_index = section_index;
1167 } else if (std.mem.eql(u8, section_name, ".data")) {
1168 elf_file.sections.items(.shdr)[osec].sh_flags = elf.SHF_ALLOC | elf.SHF_WRITE;
1169 self.data_index = section_index;
1170 } else if (std.mem.startsWith(u8, section_name, ".data.")) {
1171 elf_file.sections.items(.shdr)[osec].sh_flags = elf.SHF_ALLOC | elf.SHF_WRITE;
1172 } else if (std.mem.eql(u8, section_name, ".bss")) {
1173 const shdr = &elf_file.sections.items(.shdr)[osec];
1174 shdr.sh_type = elf.SHT_NOBITS;
1175 shdr.sh_flags = elf.SHF_ALLOC | elf.SHF_WRITE;
1176 self.bss_index = section_index;
1177 } else if (std.mem.startsWith(u8, section_name, ".bss.")) {
1178 const shdr = &elf_file.sections.items(.shdr)[osec];
1179 shdr.sh_type = elf.SHT_NOBITS;
1180 shdr.sh_flags = elf.SHF_ALLOC | elf.SHF_WRITE;
1181 } else if (std.mem.eql(u8, section_name, ".tdata")) {
1182 elf_file.sections.items(.shdr)[osec].sh_flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS;
1183 self.tdata_index = section_index;
1184 } else if (std.mem.startsWith(u8, section_name, ".tdata.")) {
1185 elf_file.sections.items(.shdr)[osec].sh_flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS;
1186 } else if (std.mem.eql(u8, section_name, ".tbss")) {
1187 const shdr = &elf_file.sections.items(.shdr)[osec];
1188 shdr.sh_type = elf.SHT_NOBITS;
1189 shdr.sh_flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS;
1190 self.tbss_index = section_index;
1191 } else if (std.mem.startsWith(u8, section_name, ".tbss.")) {
1192 const shdr = &elf_file.sections.items(.shdr)[osec];
1193 shdr.sh_type = elf.SHT_NOBITS;
1194 shdr.sh_flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS;
1195 } else if (std.mem.eql(u8, section_name, ".eh_frame")) {
1196 const target = &zcu.navFileScope(nav_index).mod.?.resolved_target.result;
1197 const shdr = &elf_file.sections.items(.shdr)[osec];
1198 if (target.cpu.arch == .x86_64) shdr.sh_type = elf.SHT_X86_64_UNWIND;
1199 shdr.sh_flags = elf.SHF_ALLOC;
1200 self.eh_frame_index = section_index;
1201 } else if (std.mem.eql(u8, section_name, ".debug_info")) {
1202 elf_file.sections.items(.shdr)[osec].sh_flags = 0;
1203 self.debug_info_index = section_index;
1204 } else if (std.mem.eql(u8, section_name, ".debug_abbrev")) {
1205 elf_file.sections.items(.shdr)[osec].sh_flags = 0;
1206 self.debug_abbrev_index = section_index;
1207 } else if (std.mem.eql(u8, section_name, ".debug_aranges")) {
1208 elf_file.sections.items(.shdr)[osec].sh_flags = 0;
1209 self.debug_aranges_index = section_index;
1210 } else if (std.mem.eql(u8, section_name, ".debug_str")) {
1211 elf_file.sections.items(.shdr)[osec].sh_flags = 0;
1212 self.debug_str_index = section_index;
1213 } else if (std.mem.eql(u8, section_name, ".debug_line")) {
1214 elf_file.sections.items(.shdr)[osec].sh_flags = 0;
1215 self.debug_line_index = section_index;
1216 } else if (std.mem.eql(u8, section_name, ".debug_line_str")) {
1217 elf_file.sections.items(.shdr)[osec].sh_flags = 0;
1218 self.debug_line_str_index = section_index;
1219 } else if (std.mem.eql(u8, section_name, ".debug_loclists")) {
1220 elf_file.sections.items(.shdr)[osec].sh_flags = 0;
1221 self.debug_loclists_index = section_index;
1222 } else if (std.mem.eql(u8, section_name, ".debug_rnglists")) {
1223 elf_file.sections.items(.shdr)[osec].sh_flags = 0;
1224 self.debug_rnglists_index = section_index;
1225 } else if (std.mem.startsWith(u8, section_name, ".debug")) {
1226 elf_file.sections.items(.shdr)[osec].sh_flags = 0;
1227 } else if (std.mem.eql(u8, section_name, ".preinit_array") or std.mem.startsWith(u8, section_name, ".preinit_array.")) {
1228 const shdr = &elf_file.sections.items(.shdr)[osec];
1229 shdr.sh_type = elf.SHT_PREINIT_ARRAY;
1230 shdr.sh_flags = elf.SHF_ALLOC | elf.SHF_WRITE;
1231 } else if (std.mem.eql(u8, section_name, ".init_array") or std.mem.startsWith(u8, section_name, ".init_array.")) {
1232 const shdr = &elf_file.sections.items(.shdr)[osec];
1233 shdr.sh_type = elf.SHT_INIT_ARRAY;
1234 shdr.sh_flags = elf.SHF_ALLOC | elf.SHF_WRITE;
1235 } else if (std.mem.eql(u8, section_name, ".fini_array") or std.mem.startsWith(u8, section_name, ".fini_array.")) {
1236 const shdr = &elf_file.sections.items(.shdr)[osec];
1237 shdr.sh_type = elf.SHT_FINI_ARRAY;
1238 shdr.sh_flags = elf.SHF_ALLOC | elf.SHF_WRITE;
1239 }
1240 return osec;
1241 }
1242 if (is_func) {
1243 if (self.text_index) |symbol_index|
1244 return self.symbol(symbol_index).outputShndx(elf_file).?;
1245 const osec = try elf_file.addSection(.{
1246 .type = elf.SHT_PROGBITS,
1247 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
1248 .name = try elf_file.insertShString(".text"),
1249 .addralign = 1,
1250 });
1251 self.text_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".text"), osec);
1252 return osec;
1253 }
1254 const has_relocs = self.symbol(sym_index).atom(elf_file).?.relocs(elf_file).len > 0;
1255 if (nav.resolved.?.@"threadlocal" and elf_file.base.comp.config.any_non_single_threaded) {
1256 const is_bss = !has_relocs and for (code) |byte| {
1257 if (byte != 0) break false;
1258 } else true;
1259 if (is_bss) {
1260 if (self.tbss_index) |symbol_index|
1261 return self.symbol(symbol_index).outputShndx(elf_file).?;
1262 const osec = try elf_file.addSection(.{
1263 .name = try elf_file.insertShString(".tbss"),
1264 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,
1265 .type = elf.SHT_NOBITS,
1266 .addralign = 1,
1267 });
1268 self.tbss_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".tbss"), osec);
1269 return osec;
1270 }
1271 if (self.tdata_index) |symbol_index|
1272 return self.symbol(symbol_index).outputShndx(elf_file).?;
1273 const osec = try elf_file.addSection(.{
1274 .type = elf.SHT_PROGBITS,
1275 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,
1276 .name = try elf_file.insertShString(".tdata"),
1277 .addralign = 1,
1278 });
1279 self.tdata_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".tdata"), osec);
1280 return osec;
1281 }
1282 if (nav.resolved.?.@"const") {
1283 if (self.data_relro_index) |symbol_index|
1284 return self.symbol(symbol_index).outputShndx(elf_file).?;
1285 const osec = try elf_file.addSection(.{
1286 .name = try elf_file.insertShString(".data.rel.ro"),
1287 .type = elf.SHT_PROGBITS,
1288 .addralign = 1,
1289 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1290 });
1291 self.data_relro_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".data.rel.ro"), osec);
1292 return osec;
1293 }
1294 if (nav_val.isUndef(zcu))
1295 return switch (zcu.navFileScope(nav_index).mod.?.optimize_mode) {
1296 .debug, .safe => {
1297 if (self.data_index) |symbol_index|
1298 return self.symbol(symbol_index).outputShndx(elf_file).?;
1299 const osec = try elf_file.addSection(.{
1300 .name = try elf_file.insertShString(".data"),
1301 .type = elf.SHT_PROGBITS,
1302 .addralign = ptr_size,
1303 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1304 });
1305 self.data_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".data"), osec);
1306 return osec;
1307 },
1308 .fast, .small => {
1309 if (self.bss_index) |symbol_index|
1310 return self.symbol(symbol_index).outputShndx(elf_file).?;
1311 const osec = try elf_file.addSection(.{
1312 .type = elf.SHT_NOBITS,
1313 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1314 .name = try elf_file.insertShString(".bss"),
1315 .addralign = 1,
1316 });
1317 self.bss_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".bss"), osec);
1318 return osec;
1319 },
1320 };
1321 const is_bss = !has_relocs and for (code) |byte| {
1322 if (byte != 0) break false;
1323 } else true;
1324 if (is_bss) {
1325 if (self.bss_index) |symbol_index|
1326 return self.symbol(symbol_index).outputShndx(elf_file).?;
1327 const osec = try elf_file.addSection(.{
1328 .type = elf.SHT_NOBITS,
1329 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1330 .name = try elf_file.insertShString(".bss"),
1331 .addralign = 1,
1332 });
1333 self.bss_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".bss"), osec);
1334 return osec;
1335 }
1336 if (self.data_index) |symbol_index|
1337 return self.symbol(symbol_index).outputShndx(elf_file).?;
1338 const osec = try elf_file.addSection(.{
1339 .name = try elf_file.insertShString(".data"),
1340 .type = elf.SHT_PROGBITS,
1341 .addralign = ptr_size,
1342 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1343 });
1344 self.data_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".data"), osec);
1345 return osec;
1346}
1347
1348fn updateNavCode(
1349 self: *ZigObject,
1350 elf_file: *Elf,
1351 pt: Zcu.PerThread,
1352 nav_index: InternPool.Nav.Index,
1353 sym_index: Symbol.Index,
1354 shdr_index: u32,
1355 code: []const u8,
1356 stt_bits: u8,
1357) link.Error!void {
1358 const zcu = pt.zcu;
1359 const gpa = zcu.gpa;
1360 const comp = elf_file.base.comp;
1361 const io = comp.io;
1362 const ip = &zcu.intern_pool;
1363 const nav = ip.getNav(nav_index);
1364
1365 log.debug("updateNavCode {f}({d})", .{ nav.fqn.fmt(ip), nav_index });
1366
1367 const mod = zcu.navFileScope(nav_index).mod.?;
1368 const target = &mod.resolved_target.result;
1369 const required_alignment = switch (nav.resolved.?.@"align") {
1370 .none => switch (mod.optimize_mode) {
1371 .debug, .safe, .fast => target_util.defaultFunctionAlignment(target),
1372 .small => target_util.minFunctionAlignment(target),
1373 }.maxStrict(Type.fromInterned(nav.resolved.?.type).abiAlignment(zcu)),
1374 else => |a| a.maxStrict(target_util.minFunctionAlignment(target)),
1375 };
1376
1377 const sym = self.symbol(sym_index);
1378 const esym = &self.symtab.items(.elf_sym)[sym.esym_index];
1379 const atom_ptr = sym.atom(elf_file).?;
1380 const name_offset = try self.strtab.insert(gpa, nav.fqn.toSlice(ip));
1381
1382 atom_ptr.alive = true;
1383 atom_ptr.name_offset = name_offset;
1384 atom_ptr.output_section_index = shdr_index;
1385
1386 sym.name_offset = name_offset;
1387 esym.st_name = name_offset;
1388 esym.st_info |= stt_bits;
1389 esym.st_size = code.len;
1390
1391 const old_size = atom_ptr.size;
1392 const old_vaddr = atom_ptr.value;
1393 atom_ptr.alignment = required_alignment;
1394 atom_ptr.size = code.len;
1395
1396 if (old_size > 0 and elf_file.base.child_pid == null) {
1397 const capacity = atom_ptr.capacity(elf_file);
1398 const need_realloc = code.len > capacity or !required_alignment.check(@intCast(atom_ptr.value));
1399 if (need_realloc) {
1400 self.allocateAtom(atom_ptr, true, elf_file) catch |err|
1401 return elf_file.base.cgFail(nav_index, "failed to allocate atom: {s}", .{@errorName(err)});
1402
1403 log.debug("growing {f} from 0x{x} to 0x{x}", .{ nav.fqn.fmt(ip), old_vaddr, atom_ptr.value });
1404 if (old_vaddr != atom_ptr.value) {
1405 sym.value = 0;
1406 esym.st_value = 0;
1407 }
1408 } else if (code.len < old_size) {
1409 // TODO shrink section size
1410 }
1411 } else {
1412 self.allocateAtom(atom_ptr, true, elf_file) catch |err|
1413 return elf_file.base.cgFail(nav_index, "failed to allocate atom: {s}", .{@errorName(err)});
1414
1415 errdefer self.freeNavMetadata(elf_file, sym_index);
1416 sym.value = 0;
1417 esym.st_value = 0;
1418 }
1419
1420 self.navs.getPtr(nav_index).?.allocated = true;
1421
1422 if (elf_file.base.child_pid) |pid| {
1423 switch (builtin.os.tag) {
1424 .linux => {
1425 var code_vec: [1]std.posix.iovec_const = .{.{
1426 .base = code.ptr,
1427 .len = code.len,
1428 }};
1429 var remote_vec: [1]std.posix.iovec_const = .{.{
1430 .base = @as([*]u8, @ptrFromInt(@as(usize, @intCast(sym.address(.{}, elf_file))))),
1431 .len = code.len,
1432 }};
1433 const rc = std.os.linux.process_vm_writev(pid, &code_vec, &remote_vec, 0);
1434 switch (std.os.linux.errno(rc)) {
1435 .SUCCESS => assert(rc == code.len),
1436 else => |errno| log.warn("process_vm_writev failure: {s}", .{@tagName(errno)}),
1437 }
1438 },
1439 else => return elf_file.base.cgFail(nav_index, "ELF hot swap unavailable on host operating system '{s}'", .{@tagName(builtin.os.tag)}),
1440 }
1441 }
1442
1443 const shdr = elf_file.sections.items(.shdr)[shdr_index];
1444 if (shdr.sh_type != elf.SHT_NOBITS) {
1445 const file_offset = atom_ptr.offset(elf_file);
1446 elf_file.base.file.?.writePositionalAll(io, code, file_offset) catch |err|
1447 return elf_file.base.cgFail(nav_index, "failed to write to output file: {t}", .{err});
1448 log.debug("writing {f} from 0x{x} to 0x{x}", .{ nav.fqn.fmt(ip), file_offset, file_offset + code.len });
1449 }
1450}
1451
1452fn updateTlv(
1453 self: *ZigObject,
1454 elf_file: *Elf,
1455 pt: Zcu.PerThread,
1456 nav_index: InternPool.Nav.Index,
1457 sym_index: Symbol.Index,
1458 shndx: u32,
1459 code: []const u8,
1460) link.Error!void {
1461 const zcu = pt.zcu;
1462 const ip = &zcu.intern_pool;
1463 const gpa = zcu.gpa;
1464 const comp = elf_file.base.comp;
1465 const io = comp.io;
1466 const nav = ip.getNav(nav_index);
1467
1468 log.debug("updateTlv {f}({d})", .{ nav.fqn.fmt(ip), nav_index });
1469
1470 const required_alignment = zcu.navAlignment(nav_index);
1471
1472 const sym = self.symbol(sym_index);
1473 const esym = &self.symtab.items(.elf_sym)[sym.esym_index];
1474 const atom_ptr = sym.atom(elf_file).?;
1475 const name_offset = try self.strtab.insert(gpa, nav.fqn.toSlice(ip));
1476
1477 atom_ptr.alive = true;
1478 atom_ptr.name_offset = name_offset;
1479 atom_ptr.output_section_index = shndx;
1480
1481 sym.name_offset = name_offset;
1482 esym.st_name = name_offset;
1483 esym.st_info = elf.STT_TLS;
1484 esym.st_size = code.len;
1485
1486 atom_ptr.alignment = required_alignment;
1487 atom_ptr.size = code.len;
1488
1489 const gop = try self.tls_variables.getOrPut(gpa, atom_ptr.atom_index);
1490 assert(!gop.found_existing); // TODO incremental updates
1491
1492 self.allocateAtom(atom_ptr, true, elf_file) catch |err|
1493 return elf_file.base.cgFail(nav_index, "failed to allocate atom: {s}", .{@errorName(err)});
1494 sym.value = 0;
1495 esym.st_value = 0;
1496
1497 self.navs.getPtr(nav_index).?.allocated = true;
1498
1499 const shdr = elf_file.sections.items(.shdr)[shndx];
1500 if (shdr.sh_type != elf.SHT_NOBITS) {
1501 const file_offset = atom_ptr.offset(elf_file);
1502 elf_file.base.file.?.writePositionalAll(io, code, file_offset) catch |err|
1503 return elf_file.base.cgFail(nav_index, "failed to write to output file: {t}", .{err});
1504 log.debug("writing TLV {s} from 0x{x} to 0x{x}", .{
1505 atom_ptr.name(elf_file),
1506 file_offset,
1507 file_offset + code.len,
1508 });
1509 }
1510}
1511
1512pub fn updateFunc(
1513 self: *ZigObject,
1514 elf_file: *Elf,
1515 pt: Zcu.PerThread,
1516 func_index: InternPool.Index,
1517 mir: *const codegen.AnyMir,
1518) link.Error!void {
1519 const tracy = trace(@src());
1520 defer tracy.end();
1521
1522 const zcu = pt.zcu;
1523 const ip = &zcu.intern_pool;
1524 const gpa = elf_file.base.comp.gpa;
1525 const func = zcu.funcInfo(func_index);
1526
1527 log.debug("updateFunc {f}({d})", .{ ip.getNav(func.owner_nav).fqn.fmt(ip), func.owner_nav });
1528
1529 const sym_index = try self.getOrCreateMetadataForNav(zcu, func.owner_nav);
1530 self.atom(self.symbol(sym_index).ref.index).?.freeRelocs(self);
1531
1532 var aw: std.Io.Writer.Allocating = .init(gpa);
1533 defer aw.deinit();
1534
1535 var debug_wip_nav = if (self.dwarf) |*dwarf| try dwarf.initWipNav(
1536 pt,
1537 func.owner_nav,
1538 @fromBackingInt(@intCast(sym_index)),
1539 ) else null;
1540 defer if (debug_wip_nav) |*wip_nav| wip_nav.deinit();
1541
1542 codegen.emitFunction(
1543 &elf_file.base,
1544 pt,
1545 func_index,
1546 @fromBackingInt(@intCast(sym_index)),
1547 mir,
1548 &aw.writer,
1549 if (debug_wip_nav) |*dn| .{ .dwarf = dn } else .none,
1550 ) catch |err| switch (err) {
1551 error.WriteFailed => return error.OutOfMemory,
1552 else => |e| return e,
1553 };
1554 const code = aw.written();
1555
1556 const shndx = try self.getNavShdrIndex(elf_file, zcu, func.owner_nav, sym_index, code);
1557 log.debug("setting shdr({x},{s}) for {f}", .{
1558 shndx,
1559 elf_file.getShString(elf_file.sections.items(.shdr)[shndx].sh_name),
1560 ip.getNav(func.owner_nav).fqn.fmt(ip),
1561 });
1562 const old_rva, const old_alignment = blk: {
1563 const atom_ptr = self.atom(self.symbol(sym_index).ref.index).?;
1564 break :blk .{ atom_ptr.value, atom_ptr.alignment };
1565 };
1566 try self.updateNavCode(elf_file, pt, func.owner_nav, sym_index, shndx, code, elf.STT_FUNC);
1567 const new_rva, const new_alignment = blk: {
1568 const atom_ptr = self.atom(self.symbol(sym_index).ref.index).?;
1569 break :blk .{ atom_ptr.value, atom_ptr.alignment };
1570 };
1571
1572 if (debug_wip_nav) |*wip_nav| self.dwarf.?.finishWipNavFunc(pt, func.owner_nav, code.len, wip_nav) catch |err|
1573 return elf_file.base.cgFail(func.owner_nav, "failed to finish dwarf function: {s}", .{@errorName(err)});
1574
1575 // Exports will be updated by `Zcu.processExports` after the update.
1576
1577 if (old_rva != new_rva and old_rva > 0) {
1578 // If we had to reallocate the function, we re-use the existing slot for a trampoline.
1579 // In the rare case that the function has been further overaligned we skip creating a
1580 // trampoline and update all symbols referring this function.
1581 if (old_alignment.order(new_alignment) == .lt) {
1582 @panic("TODO update all symbols referring this function");
1583 }
1584
1585 // Create a trampoline to the new location at `old_rva`.
1586 if (!self.symbol(sym_index).flags.has_trampoline) {
1587 const name = try std.fmt.allocPrint(gpa, "{s}$trampoline", .{
1588 self.symbol(sym_index).name(elf_file),
1589 });
1590 defer gpa.free(name);
1591 const osec = if (self.text_index) |sect_sym_index|
1592 self.symbol(sect_sym_index).outputShndx(elf_file).?
1593 else osec: {
1594 const osec = try elf_file.addSection(.{
1595 .name = try elf_file.insertShString(".text"),
1596 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
1597 .type = elf.SHT_PROGBITS,
1598 .addralign = 1,
1599 });
1600 self.text_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".text"), osec);
1601 break :osec osec;
1602 };
1603 const name_off = try self.addString(gpa, name);
1604 const tr_size = trampolineSize(elf_file.getTarget().cpu.arch);
1605 const tr_sym_index = try self.newSymbolWithAtom(gpa, name_off);
1606 const tr_sym = self.symbol(tr_sym_index);
1607 const tr_esym = &self.symtab.items(.elf_sym)[tr_sym.esym_index];
1608 tr_esym.st_info |= elf.STT_OBJECT;
1609 tr_esym.st_size = tr_size;
1610 const tr_atom_ptr = tr_sym.atom(elf_file).?;
1611 tr_atom_ptr.value = old_rva;
1612 tr_atom_ptr.alive = true;
1613 tr_atom_ptr.alignment = old_alignment;
1614 tr_atom_ptr.output_section_index = osec;
1615 tr_atom_ptr.size = tr_size;
1616 const target_sym = self.symbol(sym_index);
1617 target_sym.addExtra(.{ .trampoline = tr_sym_index }, elf_file);
1618 target_sym.flags.has_trampoline = true;
1619 }
1620 const target_sym = self.symbol(sym_index);
1621 writeTrampoline(self.symbol(target_sym.extra(elf_file).trampoline).*, target_sym.*, elf_file) catch |err|
1622 return elf_file.base.cgFail(func.owner_nav, "failed to write trampoline: {s}", .{@errorName(err)});
1623 }
1624}
1625
1626pub fn updateNav(
1627 self: *ZigObject,
1628 elf_file: *Elf,
1629 pt: Zcu.PerThread,
1630 nav_index: InternPool.Nav.Index,
1631) link.Error!void {
1632 const tracy = trace(@src());
1633 defer tracy.end();
1634
1635 const zcu = pt.zcu;
1636 const ip = &zcu.intern_pool;
1637 const nav = ip.getNav(nav_index);
1638
1639 log.debug("updateNav {f}({d})", .{ nav.fqn.fmt(ip), nav_index });
1640
1641 switch (ip.indexToKey(nav.resolved.?.value)) {
1642 else => {},
1643 .@"extern" => |@"extern"| {
1644 const sym_index = try self.getGlobalSymbol(
1645 elf_file,
1646 nav.name.toSlice(ip),
1647 @"extern".lib_name.toSlice(ip),
1648 );
1649 if (nav.resolved.?.@"threadlocal" and elf_file.base.comp.config.any_non_single_threaded) {
1650 self.symbol(sym_index).flags.is_tls = true;
1651 }
1652 if (self.dwarf) |*dwarf| {
1653 var debug_wip_nav = try dwarf.initWipNav(pt, nav_index, @fromBackingInt(@intCast(sym_index)));
1654 defer debug_wip_nav.deinit();
1655 dwarf.finishWipNav(pt, nav_index, &debug_wip_nav) catch |err| switch (err) {
1656 error.OutOfMemory, error.Canceled, error.AlreadyReported => |e| return e,
1657 else => |e| return elf_file.base.cgFail(nav_index, "failed to finish dwarf nav: {s}", .{@errorName(e)}),
1658 };
1659 }
1660 return;
1661 },
1662 }
1663
1664 if (Type.fromInterned(nav.resolved.?.type).hasRuntimeBits(zcu)) {
1665 const sym_index = try self.getOrCreateMetadataForNav(zcu, nav_index);
1666 self.symbol(sym_index).atom(elf_file).?.freeRelocs(self);
1667
1668 var aw: std.Io.Writer.Allocating = .init(zcu.gpa);
1669 defer aw.deinit();
1670
1671 var debug_wip_nav = if (self.dwarf) |*dwarf| try dwarf.initWipNav(pt, nav_index, @fromBackingInt(@intCast(sym_index))) else null;
1672 defer if (debug_wip_nav) |*wip_nav| wip_nav.deinit();
1673
1674 codegen.generateSymbol(
1675 &elf_file.base,
1676 pt,
1677 .fromInterned(nav.resolved.?.value),
1678 &aw.writer,
1679 .{ .atom_index = @fromBackingInt(@intCast(sym_index)) },
1680 ) catch |err| switch (err) {
1681 error.WriteFailed => return error.OutOfMemory,
1682 else => |e| return e,
1683 };
1684 const code = aw.written();
1685
1686 const shndx = try self.getNavShdrIndex(elf_file, zcu, nav_index, sym_index, code);
1687 log.debug("setting shdr({x},{s}) for {f}", .{
1688 shndx,
1689 elf_file.getShString(elf_file.sections.items(.shdr)[shndx].sh_name),
1690 nav.fqn.fmt(ip),
1691 });
1692 if (elf_file.sections.items(.shdr)[shndx].sh_flags & elf.SHF_TLS != 0)
1693 try self.updateTlv(elf_file, pt, nav_index, sym_index, shndx, code)
1694 else
1695 try self.updateNavCode(elf_file, pt, nav_index, sym_index, shndx, code, elf.STT_OBJECT);
1696
1697 if (debug_wip_nav) |*wip_nav| self.dwarf.?.finishWipNav(pt, nav_index, wip_nav) catch |err| switch (err) {
1698 error.OutOfMemory, error.Canceled, error.AlreadyReported => |e| return e,
1699 else => |e| return elf_file.base.cgFail(nav_index, "failed to finish dwarf nav: {s}", .{@errorName(e)}),
1700 };
1701 } else if (self.dwarf) |*dwarf| try dwarf.updateComptimeNav(pt, nav_index);
1702
1703 // Exports will be updated by `Zcu.processExports` after the update.
1704}
1705
1706pub fn updateContainerType(
1707 self: *ZigObject,
1708 pt: Zcu.PerThread,
1709 ty: InternPool.Index,
1710 success: bool,
1711) !void {
1712 const tracy = trace(@src());
1713 defer tracy.end();
1714
1715 if (self.dwarf) |*dwarf| try dwarf.updateContainerType(pt, ty, success);
1716}
1717
1718fn updateLazySymbol(
1719 self: *ZigObject,
1720 elf_file: *Elf,
1721 pt: Zcu.PerThread,
1722 sym: link.File.LazySymbol,
1723 symbol_index: Symbol.Index,
1724) !void {
1725 const zcu = pt.zcu;
1726 const gpa = zcu.gpa;
1727
1728 var required_alignment: InternPool.Alignment = .none;
1729 var aw: std.Io.Writer.Allocating = .init(gpa);
1730 defer aw.deinit();
1731
1732 const name_str_index = blk: {
1733 const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{f}", .{
1734 @tagName(sym.kind),
1735 Type.fromInterned(sym.ty).fmt(pt),
1736 });
1737 defer gpa.free(name);
1738 break :blk try self.strtab.insert(gpa, name);
1739 };
1740
1741 codegen.generateLazySymbol(
1742 &elf_file.base,
1743 pt,
1744 sym,
1745 &required_alignment,
1746 &aw.writer,
1747 .none,
1748 .{ .atom_index = @fromBackingInt(@intCast(symbol_index)) },
1749 ) catch |err| switch (err) {
1750 error.WriteFailed => return error.OutOfMemory,
1751 else => |e| return e,
1752 };
1753 const code = aw.written();
1754
1755 const output_section_index = switch (sym.kind) {
1756 .code => if (self.text_index) |sym_index|
1757 self.symbol(sym_index).outputShndx(elf_file).?
1758 else osec: {
1759 const osec = try elf_file.addSection(.{
1760 .name = try elf_file.insertShString(".text"),
1761 .type = elf.SHT_PROGBITS,
1762 .addralign = 1,
1763 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
1764 });
1765 self.text_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".text"), osec);
1766 break :osec osec;
1767 },
1768 .const_data => if (self.rodata_index) |sym_index|
1769 self.symbol(sym_index).outputShndx(elf_file).?
1770 else osec: {
1771 const osec = try elf_file.addSection(.{
1772 .name = try elf_file.insertShString(".rodata"),
1773 .type = elf.SHT_PROGBITS,
1774 .addralign = 1,
1775 .flags = elf.SHF_ALLOC,
1776 });
1777 self.rodata_index = try self.addSectionSymbol(gpa, try self.addString(gpa, ".rodata"), osec);
1778 break :osec osec;
1779 },
1780 };
1781 const local_sym = self.symbol(symbol_index);
1782 local_sym.name_offset = name_str_index;
1783 const local_esym = &self.symtab.items(.elf_sym)[local_sym.esym_index];
1784 local_esym.st_name = name_str_index;
1785 local_esym.st_info |= elf.STT_OBJECT;
1786 local_esym.st_size = code.len;
1787 const atom_ptr = local_sym.atom(elf_file).?;
1788 atom_ptr.alive = true;
1789 atom_ptr.name_offset = name_str_index;
1790 atom_ptr.alignment = required_alignment;
1791 atom_ptr.size = code.len;
1792 atom_ptr.output_section_index = output_section_index;
1793
1794 try self.allocateAtom(atom_ptr, true, elf_file);
1795 errdefer self.freeNavMetadata(elf_file, symbol_index);
1796
1797 local_sym.value = 0;
1798 local_esym.st_value = 0;
1799
1800 try elf_file.pwriteAll(code, atom_ptr.offset(elf_file));
1801}
1802
1803fn lowerConst(
1804 self: *ZigObject,
1805 elf_file: *Elf,
1806 pt: Zcu.PerThread,
1807 name: []const u8,
1808 val: Value,
1809 required_alignment: InternPool.Alignment,
1810 output_section_index: u32,
1811) !link.File.SymbolId {
1812 const gpa = pt.zcu.gpa;
1813
1814 var aw: std.Io.Writer.Allocating = .init(gpa);
1815 defer aw.deinit();
1816
1817 const name_off = try self.addString(gpa, name);
1818 const sym_index = try self.newSymbolWithAtom(gpa, name_off);
1819
1820 codegen.generateSymbol(
1821 &elf_file.base,
1822 pt,
1823 val,
1824 &aw.writer,
1825 .{ .atom_index = @fromBackingInt(@intCast(sym_index)) },
1826 ) catch |err| switch (err) {
1827 error.WriteFailed => return error.OutOfMemory,
1828 else => |e| return e,
1829 };
1830 const code = aw.written();
1831
1832 const local_sym = self.symbol(sym_index);
1833 const local_esym = &self.symtab.items(.elf_sym)[local_sym.esym_index];
1834 local_esym.st_info |= elf.STT_OBJECT;
1835 local_esym.st_size = code.len;
1836 const atom_ptr = local_sym.atom(elf_file).?;
1837 atom_ptr.alive = true;
1838 atom_ptr.alignment = required_alignment;
1839 atom_ptr.size = code.len;
1840 atom_ptr.output_section_index = output_section_index;
1841
1842 try self.allocateAtom(atom_ptr, true, elf_file);
1843 errdefer self.freeNavMetadata(elf_file, sym_index);
1844
1845 try elf_file.pwriteAll(code, atom_ptr.offset(elf_file));
1846
1847 return @fromBackingInt(@intCast(sym_index));
1848}
1849
1850pub fn updateExports(
1851 self: *ZigObject,
1852 elf_file: *Elf,
1853 pt: Zcu.PerThread,
1854 export_indices: []const Zcu.Export.Index,
1855) link.Error!void {
1856 const tracy = trace(@src());
1857 defer tracy.end();
1858
1859 const zcu = pt.zcu;
1860 const gpa = elf_file.base.comp.gpa;
1861
1862 // Delete all existing exports first
1863 for (self.navs.values()) |*metadata| {
1864 for (metadata.exports.items) |sym_index| {
1865 const esym_index = self.symbol(sym_index).esym_index;
1866 const esym = &self.symtab.items(.elf_sym)[esym_index];
1867 _ = self.globals_lookup.remove(esym.st_name);
1868 esym.* = Elf.null_sym;
1869 self.symtab.items(.shndx)[esym_index] = elf.SHN_UNDEF;
1870 }
1871 metadata.exports.clearRetainingCapacity();
1872 }
1873 for (self.uavs.values()) |*metadata| {
1874 for (metadata.exports.items) |sym_index| {
1875 const esym_index = self.symbol(sym_index).esym_index;
1876 const esym = &self.symtab.items(.elf_sym)[esym_index];
1877 _ = self.globals_lookup.remove(esym.st_name);
1878 esym.* = Elf.null_sym;
1879 self.symtab.items(.shndx)[esym_index] = elf.SHN_UNDEF;
1880 }
1881 metadata.exports.clearRetainingCapacity();
1882 }
1883
1884 for (export_indices) |export_index| {
1885 const exp = export_index.ptr(zcu);
1886 const metadata = switch (exp.exported) {
1887 .nav => |nav| blk: {
1888 _ = try self.getOrCreateMetadataForNav(zcu, nav);
1889 break :blk self.navs.getPtr(nav).?;
1890 },
1891 .uav => |uav| self.uavs.getPtr(uav) orelse blk: {
1892 _ = try self.lowerUav(elf_file, pt, uav, .none);
1893 break :blk self.uavs.getPtr(uav).?;
1894 },
1895 };
1896 const sym_index = metadata.symbol_index;
1897 const esym_index = self.symbol(sym_index).esym_index;
1898 const esym = self.symtab.items(.elf_sym)[esym_index];
1899 const esym_shndx = self.symtab.items(.shndx)[esym_index];
1900 if (exp.opts.section.unwrap()) |section_name| {
1901 if (!section_name.eqlSlice(".text", &zcu.intern_pool)) {
1902 try zcu.failed_exports.ensureUnusedCapacity(gpa, 1);
1903 zcu.failed_exports.putAssumeCapacityNoClobber(export_index, try Zcu.ErrorMsg.create(
1904 gpa,
1905 exp.src,
1906 "Unimplemented: ExportOptions.section",
1907 .{},
1908 ));
1909 continue;
1910 }
1911 }
1912 const stb_bits: u8 = switch (exp.opts.linkage) {
1913 .internal => elf.STB_LOCAL,
1914 .strong => elf.STB_GLOBAL,
1915 .weak => elf.STB_WEAK,
1916 .link_once => {
1917 try zcu.failed_exports.ensureUnusedCapacity(gpa, 1);
1918 zcu.failed_exports.putAssumeCapacityNoClobber(export_index, try Zcu.ErrorMsg.create(
1919 gpa,
1920 exp.src,
1921 "Unimplemented: GlobalLinkage.LinkOnce",
1922 .{},
1923 ));
1924 continue;
1925 },
1926 };
1927 const stt_bits: u8 = @as(u4, @truncate(esym.st_info));
1928 const exp_name = exp.opts.name.toSlice(&zcu.intern_pool);
1929 const name_off = try self.strtab.insert(gpa, exp_name);
1930 const global_sym_index = try self.getGlobalSymbol(elf_file, exp_name, null);
1931 try metadata.exports.append(gpa, global_sym_index);
1932
1933 const value = self.symbol(sym_index).value;
1934 const global_sym = self.symbol(global_sym_index);
1935 global_sym.value = value;
1936 global_sym.flags.weak = exp.opts.linkage == .weak;
1937 global_sym.version_index = elf_file.default_sym_version;
1938 global_sym.ref = .{ .index = esym_shndx, .file = self.index };
1939 const global_esym = &self.symtab.items(.elf_sym)[global_sym.esym_index];
1940 global_esym.st_value = @intCast(value);
1941 global_esym.st_shndx = esym.st_shndx;
1942 global_esym.st_info = (stb_bits << 4) | stt_bits;
1943 global_esym.st_name = name_off;
1944 global_esym.st_size = esym.st_size;
1945 self.symtab.items(.shndx)[global_sym.esym_index] = esym_shndx;
1946 }
1947}
1948
1949pub fn updateLineNumber(self: *ZigObject, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index, line: u32) link.Error!void {
1950 if (self.dwarf) |*dwarf| {
1951 const comp = dwarf.bin_file.comp;
1952 const diags = &comp.link_diags;
1953 dwarf.updateLineNumber(pt.zcu, ti_id, line) catch |err| switch (err) {
1954 error.OutOfMemory, error.Canceled, error.AlreadyReported => |e| return e,
1955 else => |e| return diags.fail("failed to update dwarf line numbers: {s}", .{@errorName(e)}),
1956 };
1957 }
1958}
1959
1960pub fn getGlobalSymbol(self: *ZigObject, elf_file: *Elf, name: []const u8, lib_name: ?[]const u8) !u32 {
1961 _ = lib_name;
1962 const gpa = elf_file.base.comp.gpa;
1963 const off = try self.strtab.insert(gpa, name);
1964 const lookup_gop = try self.globals_lookup.getOrPut(gpa, off);
1965 if (!lookup_gop.found_existing) {
1966 lookup_gop.value_ptr.* = try self.newGlobalSymbol(gpa, off);
1967 }
1968 return lookup_gop.value_ptr.*;
1969}
1970
1971const max_trampoline_len = 12;
1972
1973fn trampolineSize(cpu_arch: std.Target.Cpu.Arch) u64 {
1974 const len = switch (cpu_arch) {
1975 .x86_64 => 5, // jmp rel32
1976 else => @panic("TODO implement trampoline size for this CPU arch"),
1977 };
1978 comptime assert(len <= max_trampoline_len);
1979 return len;
1980}
1981
1982fn writeTrampoline(tr_sym: Symbol, target: Symbol, elf_file: *Elf) !void {
1983 const comp = elf_file.base.comp;
1984 const io = comp.io;
1985 const atom_ptr = tr_sym.atom(elf_file).?;
1986 const fileoff = atom_ptr.offset(elf_file);
1987 const source_addr = tr_sym.address(.{}, elf_file);
1988 const target_addr = target.address(.{ .trampoline = false }, elf_file);
1989 var buf: [max_trampoline_len]u8 = undefined;
1990 const out = switch (elf_file.getTarget().cpu.arch) {
1991 .x86_64 => try x86_64.writeTrampolineCode(source_addr, target_addr, &buf),
1992 else => @panic("TODO implement write trampoline for this CPU arch"),
1993 };
1994 try elf_file.base.file.?.writePositionalAll(io, out, fileoff);
1995
1996 if (elf_file.base.child_pid) |pid| {
1997 switch (builtin.os.tag) {
1998 .linux => {
1999 var local_vec: [1]std.posix.iovec_const = .{.{
2000 .base = out.ptr,
2001 .len = out.len,
2002 }};
2003 var remote_vec: [1]std.posix.iovec_const = .{.{
2004 .base = @as([*]u8, @ptrFromInt(@as(usize, @intCast(source_addr)))),
2005 .len = out.len,
2006 }};
2007 const rc = std.os.linux.process_vm_writev(pid, &local_vec, &remote_vec, 0);
2008 switch (std.os.linux.errno(rc)) {
2009 .SUCCESS => assert(rc == out.len),
2010 else => |errno| log.warn("process_vm_writev failure: {s}", .{@tagName(errno)}),
2011 }
2012 },
2013 else => return error.HotSwapUnavailableOnHostOperatingSystem,
2014 }
2015 }
2016}
2017
2018pub fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, requires_padding: bool, elf_file: *Elf) !void {
2019 const slice = elf_file.sections.slice();
2020 const shdr = &slice.items(.shdr)[atom_ptr.output_section_index];
2021 const last_atom_ref = &slice.items(.last_atom)[atom_ptr.output_section_index];
2022
2023 if (last_atom_ref.eql(atom_ptr.ref())) {
2024 if (atom_ptr.prevAtom(elf_file)) |prev_atom| {
2025 prev_atom.next_atom_ref = .{};
2026 last_atom_ref.* = prev_atom.ref();
2027 } else {
2028 last_atom_ref.* = .{};
2029 }
2030 }
2031
2032 const alloc_res = try elf_file.allocateChunk(.{
2033 .shndx = atom_ptr.output_section_index,
2034 .size = atom_ptr.size,
2035 .alignment = atom_ptr.alignment,
2036 .requires_padding = requires_padding,
2037 });
2038 atom_ptr.value = @intCast(alloc_res.value);
2039 log.debug("allocated {s} at {x}\n placement {f}", .{
2040 atom_ptr.name(elf_file),
2041 atom_ptr.offset(elf_file),
2042 alloc_res.placement,
2043 });
2044
2045 const expand_section = if (elf_file.atom(alloc_res.placement)) |placement_atom|
2046 placement_atom.nextAtom(elf_file) == null
2047 else
2048 true;
2049 if (expand_section) {
2050 last_atom_ref.* = atom_ptr.ref();
2051 if (self.dwarf) |_| {
2052 // The .debug_info section has `low_pc` and `high_pc` values which is the virtual address
2053 // range of the compilation unit. When we expand the text section, this range changes,
2054 // so the DW_TAG.compile_unit tag of the .debug_info section becomes dirty.
2055 self.debug_info_section_dirty = true;
2056 // This becomes dirty for the same reason. We could potentially make this more
2057 // fine-grained with the addition of support for more compilation units. It is planned to
2058 // model each package as a different compilation unit.
2059 self.debug_aranges_section_dirty = true;
2060 self.debug_rnglists_section_dirty = true;
2061 }
2062 }
2063 shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits().?);
2064
2065 // This function can also reallocate an atom.
2066 // In this case we need to "unplug" it from its previous location before
2067 // plugging it in to its new location.
2068 if (atom_ptr.prevAtom(elf_file)) |prev| {
2069 prev.next_atom_ref = atom_ptr.next_atom_ref;
2070 }
2071 if (atom_ptr.nextAtom(elf_file)) |next| {
2072 next.prev_atom_ref = atom_ptr.prev_atom_ref;
2073 }
2074
2075 if (elf_file.atom(alloc_res.placement)) |big_atom| {
2076 atom_ptr.prev_atom_ref = alloc_res.placement;
2077 atom_ptr.next_atom_ref = big_atom.next_atom_ref;
2078 big_atom.next_atom_ref = atom_ptr.ref();
2079 } else {
2080 atom_ptr.prev_atom_ref = .{ .index = 0, .file = 0 };
2081 atom_ptr.next_atom_ref = .{ .index = 0, .file = 0 };
2082 }
2083
2084 log.debug(" prev {f}, next {f}", .{ atom_ptr.prev_atom_ref, atom_ptr.next_atom_ref });
2085}
2086
2087pub fn resetShdrIndexes(self: *ZigObject, backlinks: []const u32) void {
2088 for (self.atoms_indexes.items) |atom_index| {
2089 const atom_ptr = self.atom(atom_index) orelse continue;
2090 atom_ptr.output_section_index = backlinks[atom_ptr.output_section_index];
2091 }
2092 inline for ([_]?Symbol.Index{
2093 self.text_index,
2094 self.rodata_index,
2095 self.data_relro_index,
2096 self.data_index,
2097 self.bss_index,
2098 self.tdata_index,
2099 self.tbss_index,
2100 self.eh_frame_index,
2101 self.debug_info_index,
2102 self.debug_abbrev_index,
2103 self.debug_aranges_index,
2104 self.debug_str_index,
2105 self.debug_line_index,
2106 self.debug_line_str_index,
2107 self.debug_loclists_index,
2108 self.debug_rnglists_index,
2109 }) |maybe_sym_index| {
2110 if (maybe_sym_index) |sym_index| {
2111 const sym = self.symbol(sym_index);
2112 sym.output_section_index = backlinks[sym.output_section_index];
2113 }
2114 }
2115}
2116
2117pub fn asFile(self: *ZigObject) File {
2118 return .{ .zig_object = self };
2119}
2120
2121pub fn sectionSymbol(self: *ZigObject, shndx: u32, elf_file: *Elf) ?*Symbol {
2122 inline for ([_]?Symbol.Index{
2123 self.text_index,
2124 self.rodata_index,
2125 self.data_relro_index,
2126 self.data_index,
2127 self.bss_index,
2128 self.tdata_index,
2129 self.tbss_index,
2130 self.eh_frame_index,
2131 self.debug_info_index,
2132 self.debug_abbrev_index,
2133 self.debug_aranges_index,
2134 self.debug_str_index,
2135 self.debug_line_index,
2136 self.debug_line_str_index,
2137 self.debug_loclists_index,
2138 self.debug_rnglists_index,
2139 }) |maybe_sym_index| {
2140 if (maybe_sym_index) |sym_index| {
2141 const sym = self.symbol(sym_index);
2142 if (sym.outputShndx(elf_file) == shndx) return sym;
2143 }
2144 }
2145 return null;
2146}
2147
2148pub fn addString(self: *ZigObject, allocator: Allocator, string: []const u8) !u32 {
2149 return self.strtab.insert(allocator, string);
2150}
2151
2152pub fn getString(self: ZigObject, off: u32) [:0]const u8 {
2153 return self.strtab.getAssumeExists(off);
2154}
2155
2156fn addAtom(self: *ZigObject, allocator: Allocator) !Atom.Index {
2157 try self.atoms.ensureUnusedCapacity(allocator, 1);
2158 try self.atoms_extra.ensureUnusedCapacity(allocator, @sizeOf(Atom.Extra));
2159 return self.addAtomAssumeCapacity();
2160}
2161
2162fn addAtomAssumeCapacity(self: *ZigObject) Atom.Index {
2163 const atom_index: Atom.Index = @intCast(self.atoms.items.len);
2164 const atom_ptr = self.atoms.addOneAssumeCapacity();
2165 atom_ptr.* = .{
2166 .file_index = self.index,
2167 .atom_index = atom_index,
2168 .extra_index = self.addAtomExtraAssumeCapacity(.{}),
2169 };
2170 return atom_index;
2171}
2172
2173pub fn atom(self: *ZigObject, atom_index: Atom.Index) ?*Atom {
2174 if (atom_index == 0) return null;
2175 assert(atom_index < self.atoms.items.len);
2176 return &self.atoms.items[atom_index];
2177}
2178
2179fn addAtomExtra(self: *ZigObject, allocator: Allocator, extra: Atom.Extra) !u32 {
2180 const field_count = @typeInfo(Atom.Extra).@"struct".field_names.len;
2181 try self.atoms_extra.ensureUnusedCapacity(allocator, field_count);
2182 return self.addAtomExtraAssumeCapacity(extra);
2183}
2184
2185fn addAtomExtraAssumeCapacity(self: *ZigObject, extra: Atom.Extra) u32 {
2186 const index = @as(u32, @intCast(self.atoms_extra.items.len));
2187 const info = @typeInfo(Atom.Extra).@"struct";
2188 inline for (info.field_names, info.field_types) |field_name, field_type| {
2189 self.atoms_extra.appendAssumeCapacity(switch (field_type) {
2190 u32 => @field(extra, field_name),
2191 else => @compileError("bad field type"),
2192 });
2193 }
2194 return index;
2195}
2196
2197pub fn atomExtra(self: ZigObject, index: u32) Atom.Extra {
2198 const info = @typeInfo(Atom.Extra).@"struct";
2199 var i: usize = index;
2200 var result: Atom.Extra = undefined;
2201 inline for (info.field_names, info.field_types) |field_name, field_type| {
2202 @field(result, field_name) = switch (field_type) {
2203 u32 => self.atoms_extra.items[i],
2204 else => @compileError("bad field type"),
2205 };
2206 i += 1;
2207 }
2208 return result;
2209}
2210
2211pub fn setAtomExtra(self: *ZigObject, index: u32, extra: Atom.Extra) void {
2212 assert(index > 0);
2213 const info = @typeInfo(Atom.Extra).@"struct";
2214 inline for (info.field_names, info.field_types, 0..) |field_name, field_type, i| {
2215 self.atoms_extra.items[index + i] = switch (field_type) {
2216 u32 => @field(extra, field_name),
2217 else => @compileError("bad field type"),
2218 };
2219 }
2220}
2221
2222inline fn isGlobal(index: Symbol.Index) bool {
2223 return index & global_symbol_bit != 0;
2224}
2225
2226pub fn symbol(self: *ZigObject, index: Symbol.Index) *Symbol {
2227 const actual_index = index & symbol_mask;
2228 if (isGlobal(index)) return &self.symbols.items[self.global_symbols.items[actual_index]];
2229 return &self.symbols.items[self.local_symbols.items[actual_index]];
2230}
2231
2232pub fn resolveSymbol(self: ZigObject, index: Symbol.Index, elf_file: *Elf) Elf.Ref {
2233 if (isGlobal(index)) {
2234 const resolv = self.symbols_resolver.items[index & symbol_mask];
2235 return elf_file.resolver.get(resolv).?;
2236 }
2237 return .{ .index = index, .file = self.index };
2238}
2239
2240fn addSymbol(self: *ZigObject, allocator: Allocator) !Symbol.Index {
2241 try self.symbols.ensureUnusedCapacity(allocator, 1);
2242 return self.addSymbolAssumeCapacity();
2243}
2244
2245fn addSymbolAssumeCapacity(self: *ZigObject) Symbol.Index {
2246 const index: Symbol.Index = @intCast(self.symbols.items.len);
2247 self.symbols.appendAssumeCapacity(.{ .file_index = self.index });
2248 return index;
2249}
2250
2251pub fn addSymbolExtra(self: *ZigObject, allocator: Allocator, extra: Symbol.Extra) !u32 {
2252 const field_count = @typeInfo(Symbol.Extra).@"struct".field_names.len;
2253 try self.symbols_extra.ensureUnusedCapacity(allocator, field_count);
2254 return self.addSymbolExtraAssumeCapacity(extra);
2255}
2256
2257pub fn addSymbolExtraAssumeCapacity(self: *ZigObject, extra: Symbol.Extra) u32 {
2258 const index = @as(u32, @intCast(self.symbols_extra.items.len));
2259 const info = @typeInfo(Symbol.Extra).@"struct";
2260 inline for (info.field_names, info.field_types) |field_name, field_type| {
2261 self.symbols_extra.appendAssumeCapacity(switch (field_type) {
2262 u32 => @field(extra, field_name),
2263 else => @compileError("bad field type"),
2264 });
2265 }
2266 return index;
2267}
2268
2269pub fn symbolExtra(self: *ZigObject, index: u32) Symbol.Extra {
2270 const info = @typeInfo(Symbol.Extra).@"struct";
2271 var i: usize = index;
2272 var result: Symbol.Extra = undefined;
2273 inline for (info.field_names, info.field_types) |field_name, field_type| {
2274 @field(result, field_name) = switch (field_type) {
2275 u32 => self.symbols_extra.items[i],
2276 else => @compileError("bad field type"),
2277 };
2278 i += 1;
2279 }
2280 return result;
2281}
2282
2283pub fn setSymbolExtra(self: *ZigObject, index: u32, extra: Symbol.Extra) void {
2284 const info = @typeInfo(Symbol.Extra).@"struct";
2285 inline for (info.field_names, info.field_types, 0..) |field_name, field_type, i| {
2286 self.symbols_extra.items[index + i] = switch (field_type) {
2287 u32 => @field(extra, field_name),
2288 else => @compileError("bad field type"),
2289 };
2290 }
2291}
2292
2293const Format = struct {
2294 self: *ZigObject,
2295 elf_file: *Elf,
2296
2297 fn symtab(f: Format, writer: *std.Io.Writer) std.Io.Writer.Error!void {
2298 const self = f.self;
2299 const elf_file = f.elf_file;
2300 try writer.writeAll(" locals\n");
2301 for (self.local_symbols.items) |index| {
2302 const local = self.symbols.items[index];
2303 try writer.print(" {f}\n", .{local.fmt(elf_file)});
2304 }
2305 try writer.writeAll(" globals\n");
2306 for (f.self.global_symbols.items) |index| {
2307 const global = self.symbols.items[index];
2308 try writer.print(" {f}\n", .{global.fmt(elf_file)});
2309 }
2310 }
2311
2312 fn atoms(f: Format, writer: *std.Io.Writer) std.Io.Writer.Error!void {
2313 try writer.writeAll(" atoms\n");
2314 for (f.self.atoms_indexes.items) |atom_index| {
2315 const atom_ptr = f.self.atom(atom_index) orelse continue;
2316 try writer.print(" {f}\n", .{atom_ptr.fmt(f.elf_file)});
2317 }
2318 }
2319};
2320
2321pub fn fmtSymtab(self: *ZigObject, elf_file: *Elf) std.fmt.Alt(Format, Format.symtab) {
2322 return .{ .data = .{
2323 .self = self,
2324 .elf_file = elf_file,
2325 } };
2326}
2327
2328pub fn fmtAtoms(self: *ZigObject, elf_file: *Elf) std.fmt.Alt(Format, Format.atoms) {
2329 return .{ .data = .{
2330 .self = self,
2331 .elf_file = elf_file,
2332 } };
2333}
2334
2335const ElfSym = struct {
2336 elf_sym: elf.Elf64_Sym,
2337 shndx: u32 = elf.SHN_UNDEF,
2338};
2339
2340const LazySymbolMetadata = struct {
2341 const State = enum { unused, pending_flush, flushed };
2342 text_symbol_index: Symbol.Index = undefined,
2343 rodata_symbol_index: Symbol.Index = undefined,
2344 text_state: State = .unused,
2345 rodata_state: State = .unused,
2346};
2347
2348const AvMetadata = struct {
2349 symbol_index: Symbol.Index,
2350 /// A list of all exports aliases of this Av.
2351 exports: std.ArrayList(Symbol.Index) = .empty,
2352 /// Set to true if the AV has been initialized and allocated.
2353 allocated: bool = false,
2354};
2355
2356fn checkNavAllocated(pt: Zcu.PerThread, index: InternPool.Nav.Index, meta: AvMetadata) void {
2357 if (!meta.allocated) {
2358 const zcu = pt.zcu;
2359 const ip = &zcu.intern_pool;
2360 const nav = ip.getNav(index);
2361 log.err("NAV {f}({d}) assigned symbol {d} but not allocated!", .{
2362 nav.fqn.fmt(ip),
2363 index,
2364 meta.symbol_index,
2365 });
2366 }
2367}
2368
2369fn checkUavAllocated(pt: Zcu.PerThread, index: InternPool.Index, meta: AvMetadata) void {
2370 if (!meta.allocated) {
2371 const zcu = pt.zcu;
2372 const uav = Value.fromInterned(index);
2373 const ty = uav.typeOf(zcu);
2374 log.err("UAV {f}({d}) assigned symbol {d} but not allocated!", .{
2375 ty.fmt(pt),
2376 index,
2377 meta.symbol_index,
2378 });
2379 }
2380}
2381
2382const TlsVariable = struct {
2383 symbol_index: Symbol.Index,
2384 code: []const u8 = &[0]u8{},
2385
2386 fn deinit(tlv: *TlsVariable, allocator: Allocator) void {
2387 allocator.free(tlv.code);
2388 }
2389};
2390
2391const AtomList = std.ArrayList(Atom.Index);
2392const NavTable = std.array_hash_map.Auto(InternPool.Nav.Index, AvMetadata);
2393const UavTable = std.array_hash_map.Auto(InternPool.Index, AvMetadata);
2394const LazySymbolTable = std.array_hash_map.Auto(InternPool.Index, LazySymbolMetadata);
2395const TlsTable = std.array_hash_map.Auto(Atom.Index, void);
2396
2397const x86_64 = struct {
2398 fn writeTrampolineCode(source_addr: i64, target_addr: i64, buf: *[max_trampoline_len]u8) ![]u8 {
2399 dev.checkAny(&.{ .llvm_backend, .x86_64_backend });
2400 const disp = @as(i64, @intCast(target_addr)) - source_addr - 5;
2401 var bytes = [_]u8{
2402 0xe9, 0x00, 0x00, 0x00, 0x00, // jmp rel32
2403 };
2404 assert(bytes.len == trampolineSize(.x86_64));
2405 mem.writeInt(i32, bytes[1..][0..4], @intCast(disp), .little);
2406 @memcpy(buf[0..bytes.len], &bytes);
2407 return buf[0..bytes.len];
2408 }
2409};
2410
2411const assert = std.debug.assert;
2412const build_options = @import("build_options");
2413const builtin = @import("builtin");
2414const codegen = @import("../../codegen.zig");
2415const dev = @import("../../dev.zig");
2416const elf = std.elf;
2417const link = @import("../../link.zig");
2418const log = std.log.scoped(.link);
2419const mem = std.mem;
2420const relocation = @import("relocation.zig");
2421const target_util = @import("../../target.zig");
2422const trace = @import("../../tracy.zig").trace;
2423const std = @import("std");
2424const Allocator = std.mem.Allocator;
2425
2426const Archive = @import("Archive.zig");
2427const Atom = @import("Atom.zig");
2428const Dwarf = @import("../Dwarf.zig");
2429const Elf = @import("../Elf.zig");
2430const File = @import("file.zig").File;
2431const InternPool = @import("../../InternPool.zig");
2432const Zcu = @import("../../Zcu.zig");
2433const Object = @import("Object.zig");
2434const Symbol = @import("Symbol.zig");
2435const StringTable = @import("../StringTable.zig");
2436const Type = @import("../../Type.zig");
2437const Value = @import("../../Value.zig");
2438const AnalUnit = InternPool.AnalUnit;
2439const ZigObject = @This();