1pub const Atom = @import("Elf/Atom.zig");
2
3base: link.File,
4zig_object: ?*ZigObject,
5rpath_table: std.array_hash_map.String(void),
6image_base: u64,
7z_nodelete: bool,
8z_notext: bool,
9z_defs: bool,
10z_origin: bool,
11z_nocopyreloc: bool,
12z_now: bool,
13z_relro: bool,
14/// TODO make this non optional and resolve the default in open()
15z_common_page_size: ?u64,
16/// TODO make this non optional and resolve the default in open()
17z_max_page_size: ?u64,
18soname: ?[]const u8,
19entry_name: ?[]const u8,
20
21ptr_width: PtrWidth,
22
23/// A list of all input files.
24/// First index is a special "null file". Order is otherwise not observed.
25files: std.MultiArrayList(File.Entry) = .{},
26/// Long-lived list of all file descriptors.
27/// We store them globally rather than per actual File so that we can re-use
28/// one file handle per every object file within an archive.
29file_handles: std.ArrayList(File.Handle) = .empty,
30zig_object_index: ?File.Index = null,
31linker_defined_index: ?File.Index = null,
32objects: std.ArrayList(File.Index) = .empty,
33shared_objects: std.array_hash_map.String(File.Index) = .empty,
34
35/// List of all output sections and their associated metadata.
36sections: std.MultiArrayList(Section) = .{},
37/// File offset into the shdr table.
38shdr_table_offset: ?u64 = null,
39
40/// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write.
41/// Same order as in the file.
42phdrs: ProgramHeaderList = .empty,
43
44/// Special program headers.
45phdr_indexes: ProgramHeaderIndexes = .{},
46section_indexes: SectionIndexes = .{},
47
48page_size: u32,
49default_sym_version: elf.Versym,
50
51/// .shstrtab buffer
52shstrtab: std.ArrayList(u8) = .empty,
53/// .symtab buffer
54symtab: std.ArrayList(elf.Elf64_Sym) = .empty,
55/// .strtab buffer
56strtab: std.ArrayList(u8) = .empty,
57/// Dynamic symbol table. Only populated and emitted when linking dynamically.
58dynsym: DynsymSection = .{},
59/// .dynstrtab buffer
60dynstrtab: std.ArrayList(u8) = .empty,
61/// Version symbol table. Only populated and emitted when linking dynamically.
62versym: std.ArrayList(elf.Versym) = .empty,
63/// .verneed section
64verneed: VerneedSection = .{},
65/// .got section
66got: GotSection = .{},
67/// .rela.dyn section
68rela_dyn: std.ArrayList(elf.Elf64_Rela) = .empty,
69/// .dynamic section
70dynamic: DynamicSection = .{},
71/// .hash section
72hash: HashSection = .{},
73/// .gnu.hash section
74gnu_hash: GnuHashSection = .{},
75/// .plt section
76plt: PltSection = .{},
77/// .got.plt section
78got_plt: GotPltSection = .{},
79/// .plt.got section
80plt_got: PltGotSection = .{},
81/// .copyrel section
82copy_rel: CopyRelSection = .{},
83/// .rela.plt section
84rela_plt: std.ArrayList(elf.Elf64_Rela) = .empty,
85/// SHT_GROUP sections
86/// Applies only to a relocatable.
87group_sections: std.ArrayList(GroupSection) = .empty,
88
89resolver: SymbolResolver = .{},
90
91has_text_reloc: bool = false,
92num_ifunc_dynrelocs: usize = 0,
93
94/// List of range extension thunks.
95thunks: std.ArrayList(Thunk) = .empty,
96
97/// List of output merge sections with deduped contents.
98merge_sections: std.ArrayList(Merge.Section) = .empty,
99comment_merge_section_index: ?Merge.Section.Index = null,
100
101/// `--verbose-link` output.
102/// Initialized on creation, appended to as inputs are added, printed during `flush`.
103dump_argv_list: std.ArrayList([]const u8),
104
105const SectionIndexes = struct {
106 copy_rel: ?u32 = null,
107 dynamic: ?u32 = null,
108 dynstrtab: ?u32 = null,
109 dynsymtab: ?u32 = null,
110 eh_frame: ?u32 = null,
111 eh_frame_rela: ?u32 = null,
112 eh_frame_hdr: ?u32 = null,
113 hash: ?u32 = null,
114 gnu_hash: ?u32 = null,
115 got: ?u32 = null,
116 got_plt: ?u32 = null,
117 interp: ?u32 = null,
118 plt: ?u32 = null,
119 plt_got: ?u32 = null,
120 rela_dyn: ?u32 = null,
121 rela_plt: ?u32 = null,
122 versym: ?u32 = null,
123 verneed: ?u32 = null,
124
125 shstrtab: ?u32 = null,
126 strtab: ?u32 = null,
127 symtab: ?u32 = null,
128};
129
130const ProgramHeaderList = std.ArrayList(elf.Elf64.Phdr);
131
132const OptionalProgramHeaderIndex = enum(u16) {
133 none = std.math.maxInt(u16),
134 _,
135
136 fn unwrap(i: OptionalProgramHeaderIndex) ?ProgramHeaderIndex {
137 if (i == .none) return null;
138 return @fromBackingInt(@intCast(@backingInt(i)));
139 }
140
141 fn int(i: OptionalProgramHeaderIndex) ?u16 {
142 if (i == .none) return null;
143 return @backingInt(i);
144 }
145};
146
147const ProgramHeaderIndex = enum(u16) {
148 _,
149
150 fn toOptional(i: ProgramHeaderIndex) OptionalProgramHeaderIndex {
151 const result: OptionalProgramHeaderIndex = @fromBackingInt(@intCast(@backingInt(i)));
152 assert(result != .none);
153 return result;
154 }
155
156 fn int(i: ProgramHeaderIndex) u16 {
157 return @backingInt(i);
158 }
159};
160
161const ProgramHeaderIndexes = struct {
162 /// PT.PHDR
163 table: OptionalProgramHeaderIndex = .none,
164 /// PT.LOAD for PHDR table
165 /// We add this special load segment to ensure the EHDR and PHDR table are always
166 /// loaded into memory.
167 table_load: OptionalProgramHeaderIndex = .none,
168 /// PT.INTERP
169 interp: OptionalProgramHeaderIndex = .none,
170 /// PT.DYNAMIC
171 dynamic: OptionalProgramHeaderIndex = .none,
172 /// PT.GNU_EH_FRAME
173 gnu_eh_frame: OptionalProgramHeaderIndex = .none,
174 /// PT.GNU_STACK
175 gnu_stack: OptionalProgramHeaderIndex = .none,
176 /// PT.TLS
177 /// TODO I think ELF permits multiple TLS segments but for now, assume one per file.
178 tls: OptionalProgramHeaderIndex = .none,
179};
180
181/// When allocating, the ideal_capacity is calculated by
182/// actual_capacity + (actual_capacity / ideal_factor)
183const ideal_factor = 3;
184
185/// In order for a slice of bytes to be considered eligible to keep metadata pointing at
186/// it as a possible place to put new symbols, it must have enough room for this many bytes
187/// (plus extra for reserved capacity).
188const minimum_atom_size = 64;
189pub const min_text_capacity = padToIdeal(minimum_atom_size);
190
191pub const PtrWidth = enum { p32, p64 };
192
193pub fn createEmpty(
194 arena: Allocator,
195 comp: *Compilation,
196 emit: Path,
197 options: link.File.OpenOptions,
198) !*Elf {
199 const target = &comp.root_mod.resolved_target.result;
200 assert(target.ofmt == .elf);
201
202 const use_llvm = comp.config.use_llvm;
203 const opt_zcu = comp.zcu;
204 const output_mode = comp.config.output_mode;
205 const link_mode = comp.config.link_mode;
206 const optimize_mode = comp.root_mod.optimize_mode;
207 const is_native_os = comp.root_mod.resolved_target.is_native_os;
208 const ptr_width: PtrWidth = switch (target.ptrBitWidth()) {
209 0...32 => .p32,
210 33...64 => .p64,
211 else => return error.UnsupportedELFArchitecture,
212 };
213
214 // This is the max page size that the target system can run with, aka the ABI page size. Not to
215 // be confused with the common page size, which is the page size that's used in practice on most
216 // systems.
217 const page_size: u32 = switch (target.cpu.arch) {
218 .bpfel,
219 .bpfeb,
220 .sparc64,
221 => 0x100000,
222 .aarch64,
223 .aarch64_be,
224 .amdgcn,
225 .hexagon,
226 .mips,
227 .mipsel,
228 .mips64,
229 .mips64el,
230 .powerpc,
231 .powerpcle,
232 .powerpc64,
233 .powerpc64le,
234 .sparc,
235 => 0x10000,
236 .loongarch32,
237 .loongarch64,
238 => 0x4000,
239 .arc,
240 .m68k,
241 => 0x2000,
242 .msp430,
243 => 0x4,
244 .avr,
245 => 0x1,
246 else => 0x1000,
247 };
248
249 const is_dyn_lib = output_mode == .Lib and link_mode == .dynamic;
250 const default_sym_version: elf.Versym = if (is_dyn_lib or comp.config.rdynamic) .GLOBAL else .LOCAL;
251
252 var rpath_table: std.array_hash_map.String(void) = .empty;
253 try rpath_table.entries.resize(arena, options.rpath_list.len);
254 @memcpy(rpath_table.entries.items(.key), options.rpath_list);
255 try rpath_table.reIndex(arena);
256
257 const self = try arena.create(Elf);
258 self.* = .{
259 .base = .{
260 .tag = .elf,
261 .comp = comp,
262 .emit = emit,
263 .gc_sections = options.gc_sections orelse (optimize_mode != .debug and output_mode != .Obj),
264 .print_gc_sections = options.print_gc_sections,
265 .stack_size = options.stack_size orelse 16777216,
266 .allow_shlib_undefined = options.allow_shlib_undefined orelse !is_native_os,
267 .file = null,
268 .build_id = options.build_id,
269 },
270 .zig_object = null,
271 .rpath_table = rpath_table,
272 .ptr_width = ptr_width,
273 .page_size = page_size,
274 .default_sym_version = default_sym_version,
275
276 .entry_name = switch (options.entry) {
277 .disabled => null,
278 .default => if (output_mode != .Exe) null else defaultEntrySymbolName(target.cpu.arch),
279 .enabled => defaultEntrySymbolName(target.cpu.arch),
280 .named => |name| name,
281 },
282
283 .image_base = b: {
284 if (is_dyn_lib) break :b 0;
285 if (output_mode == .Exe and (comp.config.pie or target.os.tag == .haiku)) break :b 0;
286 break :b options.image_base orelse switch (ptr_width) {
287 .p32 => 0x10000,
288 .p64 => 0x1000000,
289 };
290 },
291
292 .z_nodelete = options.z_nodelete,
293 .z_notext = options.z_notext,
294 .z_defs = options.z_defs,
295 .z_origin = options.z_origin,
296 .z_nocopyreloc = options.z_nocopyreloc,
297 .z_now = options.z_now,
298 .z_relro = options.z_relro,
299 .z_common_page_size = options.z_common_page_size,
300 .z_max_page_size = options.z_max_page_size,
301 .soname = options.soname,
302 .dump_argv_list = .empty,
303 };
304 errdefer self.base.destroy();
305
306 // --verbose-link
307 if (comp.verbose_link) try dumpArgvInit(self, arena);
308
309 const is_obj = output_mode == .Obj;
310 const is_obj_or_ar = is_obj or (output_mode == .Lib and link_mode == .static);
311
312 const io = comp.io;
313
314 // What path should this ELF linker code output to?
315 const sub_path = emit.sub_path;
316 self.base.file = try emit.root_dir.handle.createFile(io, sub_path, .{
317 .truncate = true,
318 .read = true,
319 .permissions = link.File.determinePermissions(output_mode, link_mode),
320 });
321
322 const gpa = comp.gpa;
323
324 // Append null file at index 0
325 try self.files.append(gpa, .null);
326 // Append null byte to string tables
327 try self.shstrtab.append(gpa, 0);
328 try self.strtab.append(gpa, 0);
329 // There must always be a null shdr in index 0
330 _ = try self.addSection(.{});
331 // Append null symbol in output symtab
332 try self.symtab.append(gpa, null_sym);
333
334 if (!is_obj_or_ar) {
335 try self.dynstrtab.append(gpa, 0);
336
337 // Initialize PT.PHDR program header
338 const p_align: u16 = switch (self.ptr_width) {
339 .p32 => @alignOf(elf.Elf32.Phdr),
340 .p64 => @alignOf(elf.Elf64.Phdr),
341 };
342 const ehsize: u64 = switch (self.ptr_width) {
343 .p32 => @sizeOf(elf.Elf32_Ehdr),
344 .p64 => @sizeOf(elf.Elf64_Ehdr),
345 };
346 const phsize: u64 = switch (self.ptr_width) {
347 .p32 => @sizeOf(elf.Elf32.Phdr),
348 .p64 => @sizeOf(elf.Elf64.Phdr),
349 };
350 const max_nphdrs = comptime getMaxNumberOfPhdrs();
351 const reserved: u64 = mem.alignForward(u64, padToIdeal(max_nphdrs * phsize), self.page_size);
352 self.phdr_indexes.table = (try self.addPhdr(.{
353 .type = @backingInt(elf.PT.PHDR),
354 .flags = elf.PF_R,
355 .@"align" = p_align,
356 .addr = self.image_base + ehsize,
357 .offset = ehsize,
358 .filesz = reserved,
359 .memsz = reserved,
360 })).toOptional();
361 self.phdr_indexes.table_load = (try self.addPhdr(.{
362 .type = @backingInt(elf.PT.LOAD),
363 .flags = elf.PF_R,
364 .@"align" = self.page_size,
365 .addr = self.image_base,
366 .offset = 0,
367 .filesz = reserved + ehsize,
368 .memsz = reserved + ehsize,
369 })).toOptional();
370 }
371
372 if (opt_zcu) |zcu| {
373 if (!use_llvm) {
374 const index: File.Index = @intCast(try self.files.addOne(gpa));
375 self.files.set(index, .zig_object);
376 self.zig_object_index = index;
377 const zig_object = try arena.create(ZigObject);
378 self.zig_object = zig_object;
379 zig_object.* = .{
380 .index = index,
381 .basename = try std.fmt.allocPrint(arena, "{s}.o", .{
382 fs.path.stem(zcu.main_mod.root_src_path),
383 }),
384 };
385 try zig_object.init(self, .{
386 .symbol_count_hint = options.symbol_count_hint,
387 .program_code_size_hint = options.program_code_size_hint,
388 });
389 }
390 }
391
392 return self;
393}
394
395pub fn open(
396 arena: Allocator,
397 comp: *Compilation,
398 emit: Path,
399 options: link.File.OpenOptions,
400) !*Elf {
401 // TODO: restore saved linker state, don't truncate the file, and
402 // participate in incremental compilation.
403 return createEmpty(arena, comp, emit, options);
404}
405
406pub fn deinit(self: *Elf) void {
407 const comp = self.base.comp;
408 const gpa = comp.gpa;
409 const io = comp.io;
410
411 for (self.file_handles.items) |fh| {
412 fh.close(io);
413 }
414 self.file_handles.deinit(gpa);
415
416 for (self.files.items(.tags), self.files.items(.data)) |tag, *data| switch (tag) {
417 .null, .zig_object => {},
418 .linker_defined => data.linker_defined.deinit(gpa),
419 .object => data.object.deinit(gpa),
420 .shared_object => data.shared_object.deinit(gpa),
421 };
422 if (self.zig_object) |zig_object| {
423 zig_object.deinit(gpa);
424 }
425 self.files.deinit(gpa);
426 self.objects.deinit(gpa);
427 self.shared_objects.deinit(gpa);
428
429 for (self.sections.items(.atom_list_2), self.sections.items(.atom_list), self.sections.items(.free_list)) |*atom_list, *atoms, *free_list| {
430 atom_list.deinit(gpa);
431 atoms.deinit(gpa);
432 free_list.deinit(gpa);
433 }
434 self.sections.deinit(gpa);
435 self.phdrs.deinit(gpa);
436 self.shstrtab.deinit(gpa);
437 self.symtab.deinit(gpa);
438 self.strtab.deinit(gpa);
439 self.resolver.deinit(gpa);
440
441 for (self.thunks.items) |*th| {
442 th.deinit(gpa);
443 }
444 self.thunks.deinit(gpa);
445 for (self.merge_sections.items) |*sect| {
446 sect.deinit(gpa);
447 }
448 self.merge_sections.deinit(gpa);
449
450 self.got.deinit(gpa);
451 self.plt.deinit(gpa);
452 self.plt_got.deinit(gpa);
453 self.dynsym.deinit(gpa);
454 self.dynstrtab.deinit(gpa);
455 self.dynamic.deinit(gpa);
456 self.hash.deinit(gpa);
457 self.versym.deinit(gpa);
458 self.verneed.deinit(gpa);
459 self.copy_rel.deinit(gpa);
460 self.rela_dyn.deinit(gpa);
461 self.rela_plt.deinit(gpa);
462 self.group_sections.deinit(gpa);
463 self.dump_argv_list.deinit(gpa);
464}
465
466pub fn getNavVAddr(self: *Elf, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index, reloc_info: link.File.RelocInfo) !u64 {
467 return self.zigObjectPtr().?.getNavVAddr(self, pt, nav_index, reloc_info);
468}
469
470pub fn lowerUav(
471 self: *Elf,
472 pt: Zcu.PerThread,
473 uav: InternPool.Index,
474 explicit_alignment: InternPool.Alignment,
475) !link.File.SymbolId {
476 return self.zigObjectPtr().?.lowerUav(self, pt, uav, explicit_alignment);
477}
478
479pub fn getUavVAddr(self: *Elf, uav: InternPool.Index, reloc_info: link.File.RelocInfo) !u64 {
480 return self.zigObjectPtr().?.getUavVAddr(self, uav, reloc_info);
481}
482
483/// Returns end pos of collision, if any.
484fn detectAllocCollision(self: *Elf, start: u64, size: u64) !?u64 {
485 const comp = self.base.comp;
486 const io = comp.io;
487 const small_ptr = self.ptr_width == .p32;
488 const ehdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Ehdr) else @sizeOf(elf.Elf64_Ehdr);
489 if (start < ehdr_size)
490 return ehdr_size;
491
492 var at_end = true;
493 const end = start + padToIdeal(size);
494
495 if (self.shdr_table_offset) |off| {
496 const shdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Shdr) else @sizeOf(elf.Elf64_Shdr);
497 const tight_size = self.sections.items(.shdr).len * shdr_size;
498 const increased_size = padToIdeal(tight_size);
499 const test_end = off +| increased_size;
500 if (start < test_end) {
501 if (end > off) return test_end;
502 if (test_end < std.math.maxInt(u64)) at_end = false;
503 }
504 }
505
506 for (self.sections.items(.shdr)) |shdr| {
507 if (shdr.sh_type == elf.SHT_NOBITS) continue;
508 const increased_size = padToIdeal(shdr.sh_size);
509 const test_end = shdr.sh_offset +| increased_size;
510 if (start < test_end) {
511 if (end > shdr.sh_offset) return test_end;
512 if (test_end < std.math.maxInt(u64)) at_end = false;
513 }
514 }
515
516 for (self.phdrs.items) |phdr| {
517 if (phdr.type != .LOAD) continue;
518 const increased_size = padToIdeal(phdr.filesz);
519 const test_end = phdr.offset +| increased_size;
520 if (start < test_end) {
521 if (end > phdr.offset) return test_end;
522 if (test_end < std.math.maxInt(u64)) at_end = false;
523 }
524 }
525
526 if (at_end) try self.base.file.?.setLength(io, end);
527 return null;
528}
529
530pub fn allocatedSize(self: *Elf, start: u64) u64 {
531 if (start == 0) return 0;
532 var min_pos: u64 = std.math.maxInt(u64);
533 if (self.shdr_table_offset) |off| {
534 if (off > start and off < min_pos) min_pos = off;
535 }
536 for (self.sections.items(.shdr)) |section| {
537 if (section.sh_offset <= start) continue;
538 if (section.sh_offset < min_pos) min_pos = section.sh_offset;
539 }
540 for (self.phdrs.items) |phdr| {
541 if (phdr.offset <= start) continue;
542 if (phdr.offset < min_pos) min_pos = phdr.offset;
543 }
544 return min_pos - start;
545}
546
547pub fn findFreeSpace(self: *Elf, object_size: u64, min_alignment: u64) !u64 {
548 var start: u64 = 0;
549 while (try self.detectAllocCollision(start, object_size)) |item_end| {
550 start = mem.alignForward(u64, item_end, min_alignment);
551 }
552 return start;
553}
554
555pub fn growSection(self: *Elf, shdr_index: u32, needed_size: u64, min_alignment: u64) !void {
556 const comp = self.base.comp;
557 const io = comp.io;
558 const shdr = &self.sections.items(.shdr)[shdr_index];
559
560 if (shdr.sh_type != elf.SHT_NOBITS) {
561 const allocated_size = self.allocatedSize(shdr.sh_offset);
562 log.debug("allocated size {x} of '{s}', needed size {x}", .{
563 allocated_size,
564 self.getShString(shdr.sh_name),
565 needed_size,
566 });
567
568 if (needed_size > allocated_size) {
569 const existing_size = shdr.sh_size;
570 shdr.sh_size = 0;
571 // Must move the entire section.
572 const new_offset = try self.findFreeSpace(needed_size, min_alignment);
573
574 log.debug("moving '{s}' from 0x{x} to 0x{x}", .{
575 self.getShString(shdr.sh_name),
576 shdr.sh_offset,
577 new_offset,
578 });
579
580 try self.base.copyRangeAll(shdr.sh_offset, new_offset, existing_size);
581
582 shdr.sh_offset = new_offset;
583 } else if (shdr.sh_offset + allocated_size == std.math.maxInt(u64)) {
584 try self.base.file.?.setLength(io, shdr.sh_offset + needed_size);
585 }
586 }
587
588 shdr.sh_size = needed_size;
589 self.markDirty(shdr_index);
590}
591
592fn markDirty(self: *Elf, shdr_index: u32) void {
593 if (self.zigObjectPtr()) |zo| {
594 for ([_]?Symbol.Index{
595 zo.debug_info_index,
596 zo.debug_abbrev_index,
597 zo.debug_aranges_index,
598 zo.debug_str_index,
599 zo.debug_line_index,
600 zo.debug_line_str_index,
601 zo.debug_loclists_index,
602 zo.debug_rnglists_index,
603 }, [_]*bool{
604 &zo.debug_info_section_dirty,
605 &zo.debug_abbrev_section_dirty,
606 &zo.debug_aranges_section_dirty,
607 &zo.debug_str_section_dirty,
608 &zo.debug_line_section_dirty,
609 &zo.debug_line_str_section_dirty,
610 &zo.debug_loclists_section_dirty,
611 &zo.debug_rnglists_section_dirty,
612 }) |maybe_sym_index, dirty| {
613 const sym_index = maybe_sym_index orelse continue;
614 if (zo.symbol(sym_index).atom(self).?.output_section_index == shdr_index) {
615 dirty.* = true;
616 break;
617 }
618 }
619 }
620}
621
622const AllocateChunkResult = struct {
623 value: u64,
624 placement: Ref,
625};
626
627pub fn allocateChunk(self: *Elf, args: struct {
628 size: u64,
629 shndx: u32,
630 alignment: Atom.Alignment,
631 requires_padding: bool = true,
632}) !AllocateChunkResult {
633 const slice = self.sections.slice();
634 const shdr = &slice.items(.shdr)[args.shndx];
635 const free_list = &slice.items(.free_list)[args.shndx];
636 const last_atom_ref = &slice.items(.last_atom)[args.shndx];
637 const new_atom_ideal_capacity = if (args.requires_padding) padToIdeal(args.size) else args.size;
638
639 // First we look for an appropriately sized free list node.
640 // The list is unordered. We'll just take the first thing that works.
641 const res: AllocateChunkResult = blk: {
642 var i: usize = if (self.base.child_pid == null) 0 else free_list.items.len;
643 while (i < free_list.items.len) {
644 const big_atom_ref = free_list.items[i];
645 const big_atom = self.atom(big_atom_ref).?;
646 // We now have a pointer to a live atom that has too much capacity.
647 // Is it enough that we could fit this new atom?
648 const cap = big_atom.capacity(self);
649 const ideal_capacity = if (args.requires_padding) padToIdeal(cap) else cap;
650 const ideal_capacity_end_vaddr = std.math.add(u64, @intCast(big_atom.value), ideal_capacity) catch ideal_capacity;
651 const capacity_end_vaddr = @as(u64, @intCast(big_atom.value)) + cap;
652 const new_start_vaddr_unaligned = capacity_end_vaddr - new_atom_ideal_capacity;
653 const new_start_vaddr = args.alignment.backward(new_start_vaddr_unaligned);
654 if (new_start_vaddr < ideal_capacity_end_vaddr) {
655 // Additional bookkeeping here to notice if this free list node
656 // should be deleted because the block that it points to has grown to take up
657 // more of the extra capacity.
658 if (!big_atom.freeListEligible(self)) {
659 _ = free_list.swapRemove(i);
660 } else {
661 i += 1;
662 }
663 continue;
664 }
665 // At this point we know that we will place the new block here. But the
666 // remaining question is whether there is still yet enough capacity left
667 // over for there to still be a free list node.
668 const remaining_capacity = new_start_vaddr - ideal_capacity_end_vaddr;
669 const keep_free_list_node = remaining_capacity >= min_text_capacity;
670
671 if (!keep_free_list_node) {
672 _ = free_list.swapRemove(i);
673 }
674 break :blk .{ .value = new_start_vaddr, .placement = big_atom_ref };
675 } else if (self.atom(last_atom_ref.*)) |last_atom| {
676 const ideal_capacity = if (args.requires_padding) padToIdeal(last_atom.size) else last_atom.size;
677 const ideal_capacity_end_vaddr = @as(u64, @intCast(last_atom.value)) + ideal_capacity;
678 const new_start_vaddr = args.alignment.forward(ideal_capacity_end_vaddr);
679 break :blk .{ .value = new_start_vaddr, .placement = last_atom.ref() };
680 } else {
681 break :blk .{ .value = 0, .placement = .{} };
682 }
683 };
684
685 const expand_section = if (self.atom(res.placement)) |placement_atom|
686 placement_atom.nextAtom(self) == null
687 else
688 true;
689 if (expand_section) {
690 const needed_size = res.value + args.size;
691 try self.growSection(args.shndx, needed_size, args.alignment.toByteUnits().?);
692 }
693
694 log.debug("allocated chunk (size({x}),align({x})) in {s} at 0x{x} (file(0x{x}))", .{
695 args.size,
696 args.alignment.toByteUnits().?,
697 self.getShString(shdr.sh_name),
698 shdr.sh_addr + res.value,
699 shdr.sh_offset + res.value,
700 });
701 log.debug(" placement {f}, {s}", .{
702 res.placement,
703 if (self.atom(res.placement)) |atom_ptr| atom_ptr.name(self) else "",
704 });
705
706 return res;
707}
708
709pub fn loadInput(self: *Elf, input: link.Input) !void {
710 const comp = self.base.comp;
711 const gpa = comp.gpa;
712 const io = comp.io;
713 const diags = &comp.link_diags;
714 const target = self.getTarget();
715 const debug_fmt_strip = comp.config.debug_format == .strip;
716 const default_sym_version = self.default_sym_version;
717
718 if (comp.verbose_link) {
719 comp.mutex.lockUncancelable(io); // protect comp.arena
720 defer comp.mutex.unlock(io);
721
722 const argv = &self.dump_argv_list;
723 switch (input) {
724 .res => unreachable,
725 .dso_exact => |dso_exact| try argv.appendSlice(gpa, &.{ "-l", dso_exact.name }),
726 .object, .archive => |obj| try argv.append(gpa, try obj.path.toString(comp.arena)),
727 .dso => |dso| try argv.append(gpa, try dso.path.toString(comp.arena)),
728 }
729 }
730
731 switch (input) {
732 .res => unreachable,
733 .dso_exact => @panic("TODO"),
734 .object => |obj| try parseObject(self, obj),
735 .archive => |obj| if (self.base.isStaticLib()) {
736 // Ignore static library inputs when generating a static library.
737 } else {
738 try parseArchive(gpa, io, diags, &self.file_handles, &self.files, target, debug_fmt_strip, default_sym_version, &self.objects, obj);
739 },
740 .dso => |dso| try parseDso(gpa, io, diags, dso, &self.shared_objects, &self.files, target),
741 }
742}
743
744pub fn flush(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.Error!void {
745 const tracy = trace(@src());
746 defer tracy.end();
747
748 const comp = self.base.comp;
749 const io = comp.io;
750 const diags = &comp.link_diags;
751
752 if (comp.verbose_link) try Compilation.dumpArgv(io, self.dump_argv_list.items);
753
754 const sub_prog_node = prog_node.start("ELF Flush", 0);
755 defer sub_prog_node.end();
756
757 return flushInner(self, arena, tid) catch |err| switch (err) {
758 error.OutOfMemory, error.AlreadyReported => |e| return e,
759 else => |e| return diags.fail("ELF flush failed: {t}", .{e}),
760 };
761}
762
763fn flushInner(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id) !void {
764 _ = arena;
765
766 const comp = self.base.comp;
767 const gpa = comp.gpa;
768 const diags = &comp.link_diags;
769
770 if (self.zigObjectPtr()) |zig_object| try zig_object.flush(self, tid);
771
772 switch (comp.config.output_mode) {
773 .Obj => return relocatable.flushObject(self, comp),
774 .Lib => switch (comp.config.link_mode) {
775 .dynamic => {},
776 .static => return relocatable.flushStaticLib(self, comp),
777 },
778 .Exe => {},
779 }
780
781 if (diags.hasErrors()) return error.AlreadyReported;
782
783 // If we haven't already, create a linker-generated input file comprising of
784 // linker-defined synthetic symbols only such as `_DYNAMIC`, etc.
785 if (self.linker_defined_index == null) {
786 const index: File.Index = @intCast(try self.files.addOne(gpa));
787 self.files.set(index, .{ .linker_defined = .{ .index = index } });
788 self.linker_defined_index = index;
789 const object = self.linkerDefinedPtr().?;
790 try object.init(gpa);
791 try object.initSymbols(self);
792 }
793
794 // Now, we are ready to resolve the symbols across all input files.
795 // We will first resolve the files in the ZigObject, next in the parsed
796 // input Object files.
797 // Any qualifing unresolved symbol will be upgraded to an absolute, weak
798 // symbol for potential resolution at load-time.
799 try self.resolveSymbols();
800 self.markEhFrameAtomsDead();
801 try self.resolveMergeSections();
802
803 for (self.objects.items) |index| {
804 try self.file(index).?.object.convertCommonSymbols(self);
805 }
806 self.markImportsExports();
807
808 if (self.base.gc_sections) {
809 try gc.gcAtoms(self);
810 }
811
812 self.checkDuplicates() catch |err| switch (err) {
813 error.HasDuplicates => return error.AlreadyReported,
814 else => |e| return e,
815 };
816
817 try self.addCommentString();
818 try self.finalizeMergeSections();
819 try self.initOutputSections();
820 if (self.linkerDefinedPtr()) |obj| {
821 try obj.initStartStopSymbols(self);
822 }
823 self.claimUnresolved();
824
825 // Scan and create missing synthetic entries such as GOT indirection.
826 try self.scanRelocs();
827
828 // Generate and emit synthetic sections.
829 try self.initSyntheticSections();
830 try self.initSpecialPhdrs();
831 try sortShdrs(
832 gpa,
833 &self.section_indexes,
834 &self.sections,
835 self.shstrtab.items,
836 self.merge_sections.items,
837 self.group_sections.items,
838 self.zigObjectPtr(),
839 self.files,
840 );
841
842 try self.setDynamicSection(self.rpath_table.keys());
843 self.sortDynamicSymtab();
844 try self.setHashSections();
845 try self.setVersionSymtab();
846
847 try self.sortInitFini();
848 try self.updateMergeSectionSizes();
849 try self.updateSectionSizes();
850
851 try self.addLoadPhdrs();
852 try self.allocatePhdrTable();
853 try self.allocateAllocSections();
854 try sortPhdrs(gpa, &self.phdrs, &self.phdr_indexes, self.sections.items(.phndx));
855 try self.allocateNonAllocSections();
856 self.allocateSpecialPhdrs();
857 if (self.linkerDefinedPtr()) |obj| {
858 obj.allocateSymbols(self);
859 }
860
861 // Dump the state for easy debugging.
862 // State can be dumped via `--debug-log link_state`.
863 if (build_options.enable_logging) {
864 state_log.debug("{f}", .{self.dumpState()});
865 }
866
867 // Beyond this point, everything has been allocated a virtual address and we can resolve
868 // the relocations, and commit objects to file.
869 for (self.objects.items) |index| {
870 self.file(index).?.object.dirty = false;
871 }
872 // TODO: would state tracking be more appropriate here? perhaps even custom relocation type?
873 self.rela_dyn.clearRetainingCapacity();
874 self.rela_plt.clearRetainingCapacity();
875
876 if (self.zigObjectPtr()) |zo| {
877 var undefs: std.array_hash_map.Auto(SymbolResolver.Index, std.array_list.Managed(Ref)) = .empty;
878 defer {
879 for (undefs.values()) |*refs| refs.deinit();
880 undefs.deinit(gpa);
881 }
882
883 var has_reloc_errors = false;
884 for (zo.atoms_indexes.items) |atom_index| {
885 const atom_ptr = zo.atom(atom_index) orelse continue;
886 if (!atom_ptr.alive) continue;
887 const out_shndx = atom_ptr.output_section_index;
888 const shdr = &self.sections.items(.shdr)[out_shndx];
889 if (shdr.sh_type == elf.SHT_NOBITS) continue;
890 const code = try zo.codeAlloc(self, atom_index);
891 defer gpa.free(code);
892 const file_offset = atom_ptr.offset(self);
893 (if (shdr.sh_flags & elf.SHF_ALLOC == 0)
894 atom_ptr.resolveRelocsNonAlloc(self, code, &undefs)
895 else
896 atom_ptr.resolveRelocsAlloc(self, code)) catch |err| switch (err) {
897 error.RelocFailure, error.RelaxFailure => has_reloc_errors = true,
898 error.UnsupportedCpuArch => {
899 try self.reportUnsupportedCpuArch();
900 return error.AlreadyReported;
901 },
902 else => |e| return e,
903 };
904 try self.pwriteAll(code, file_offset);
905 }
906
907 try self.reportUndefinedSymbols(&undefs);
908
909 if (has_reloc_errors) return error.AlreadyReported;
910 }
911
912 try self.writePhdrTable();
913 try self.writeShdrTable();
914 try self.writeAtoms();
915 try self.writeMergeSections();
916
917 self.writeSyntheticSections() catch |err| switch (err) {
918 error.RelocFailure => return error.AlreadyReported,
919 error.UnsupportedCpuArch => {
920 try self.reportUnsupportedCpuArch();
921 return error.AlreadyReported;
922 },
923 else => |e| return e,
924 };
925
926 if (self.base.isExe() and self.linkerDefinedPtr().?.entry_index == null) {
927 log.debug("flushing. no_entry_point_found = true", .{});
928 diags.flags.no_entry_point_found = true;
929 } else {
930 log.debug("flushing. no_entry_point_found = false", .{});
931 diags.flags.no_entry_point_found = false;
932 try self.writeElfHeader();
933 }
934
935 if (diags.hasErrors()) return error.AlreadyReported;
936}
937
938fn dumpArgvInit(self: *Elf, arena: Allocator) !void {
939 const comp = self.base.comp;
940 const gpa = comp.gpa;
941 const target = self.getTarget();
942 const full_out_path = try self.base.emit.root_dir.join(arena, &[_][]const u8{self.base.emit.sub_path});
943
944 const argv = &self.dump_argv_list;
945
946 try argv.append(gpa, "zig");
947
948 if (self.base.isStaticLib()) {
949 try argv.append(gpa, "ar");
950 } else {
951 try argv.append(gpa, "ld");
952 }
953
954 if (self.base.isObject()) {
955 try argv.append(gpa, "-r");
956 }
957
958 try argv.append(gpa, "-o");
959 try argv.append(gpa, full_out_path);
960
961 if (!self.base.isRelocatable()) {
962 if (!self.base.isStatic()) {
963 if (target.dynamic_linker.get()) |path| {
964 try argv.appendSlice(gpa, &.{ "-dynamic-linker", try arena.dupe(u8, path) });
965 }
966 }
967
968 if (self.base.isDynLib()) {
969 if (self.soname) |name| {
970 try argv.append(gpa, "-soname");
971 try argv.append(gpa, name);
972 }
973 }
974
975 if (self.entry_name) |name| {
976 try argv.appendSlice(gpa, &.{ "--entry", name });
977 }
978
979 for (self.rpath_table.keys()) |rpath| {
980 try argv.appendSlice(gpa, &.{ "-rpath", rpath });
981 }
982
983 try argv.appendSlice(gpa, &.{
984 "-z",
985 try std.fmt.allocPrint(arena, "stack-size={d}", .{self.base.stack_size}),
986 });
987
988 try argv.append(gpa, try std.fmt.allocPrint(arena, "--image-base={d}", .{self.image_base}));
989
990 if (self.base.gc_sections) {
991 try argv.append(gpa, "--gc-sections");
992 }
993
994 if (self.base.print_gc_sections) {
995 try argv.append(gpa, "--print-gc-sections");
996 }
997
998 if (comp.link_eh_frame_hdr) {
999 try argv.append(gpa, "--eh-frame-hdr");
1000 }
1001
1002 if (comp.config.rdynamic) {
1003 try argv.append(gpa, "--export-dynamic");
1004 }
1005
1006 if (self.z_notext) {
1007 try argv.append(gpa, "-z");
1008 try argv.append(gpa, "notext");
1009 }
1010
1011 if (self.z_nocopyreloc) {
1012 try argv.append(gpa, "-z");
1013 try argv.append(gpa, "nocopyreloc");
1014 }
1015
1016 if (self.z_now) {
1017 try argv.append(gpa, "-z");
1018 try argv.append(gpa, "now");
1019 }
1020
1021 if (self.base.isStatic()) {
1022 try argv.append(gpa, "-static");
1023 } else if (self.isEffectivelyDynLib()) {
1024 try argv.append(gpa, "-shared");
1025 }
1026
1027 if (comp.config.pie and self.base.isExe()) {
1028 try argv.append(gpa, "-pie");
1029 }
1030
1031 if (comp.config.debug_format == .strip) {
1032 try argv.append(gpa, "-s");
1033 }
1034
1035 if (comp.config.link_libc) {
1036 if (self.base.comp.libc_installation) |lci| {
1037 try argv.append(gpa, "-L");
1038 try argv.append(gpa, lci.crt_dir.?);
1039 }
1040 }
1041 }
1042}
1043
1044fn parseObject(self: *Elf, obj: link.Input.Object) !void {
1045 const tracy = trace(@src());
1046 defer tracy.end();
1047
1048 const comp = self.base.comp;
1049 const io = comp.io;
1050 const gpa = comp.gpa;
1051 const diags = &comp.link_diags;
1052 const target = &comp.root_mod.resolved_target.result;
1053 const debug_fmt_strip = comp.config.debug_format == .strip;
1054 const default_sym_version = self.default_sym_version;
1055 const file_handles = &self.file_handles;
1056
1057 const handle = obj.file;
1058 const fh = try addFileHandle(gpa, file_handles, handle);
1059
1060 const index: File.Index = @intCast(try self.files.addOne(gpa));
1061 self.files.set(index, .{ .object = .{
1062 .path = .{
1063 .root_dir = obj.path.root_dir,
1064 .sub_path = try gpa.dupe(u8, obj.path.sub_path),
1065 },
1066 .file_handle = fh,
1067 .index = index,
1068 } });
1069 try self.objects.append(gpa, index);
1070
1071 const object = self.file(index).?.object;
1072 try object.parseCommon(gpa, io, diags, obj.path, handle, target);
1073 if (!self.base.isStaticLib()) {
1074 try object.parse(gpa, io, diags, obj.path, handle, target, debug_fmt_strip, default_sym_version);
1075 }
1076}
1077
1078fn parseArchive(
1079 gpa: Allocator,
1080 io: Io,
1081 diags: *Diags,
1082 file_handles: *std.ArrayList(File.Handle),
1083 files: *std.MultiArrayList(File.Entry),
1084 target: *const std.Target,
1085 debug_fmt_strip: bool,
1086 default_sym_version: elf.Versym,
1087 objects: *std.ArrayList(File.Index),
1088 obj: link.Input.Object,
1089) !void {
1090 const tracy = trace(@src());
1091 defer tracy.end();
1092
1093 const fh = try addFileHandle(gpa, file_handles, obj.file);
1094 var archive = try Archive.parse(gpa, io, diags, file_handles, obj.path, fh);
1095 defer archive.deinit(gpa);
1096
1097 for (archive.objects) |extracted| {
1098 const index: File.Index = @intCast(try files.addOne(gpa));
1099 files.set(index, .{ .object = extracted });
1100 const object = &files.items(.data)[index].object;
1101 object.index = index;
1102 object.alive = obj.must_link;
1103 try object.parseCommon(gpa, io, diags, obj.path, obj.file, target);
1104 try object.parse(gpa, io, diags, obj.path, obj.file, target, debug_fmt_strip, default_sym_version);
1105 try objects.append(gpa, index);
1106 }
1107}
1108
1109fn parseDso(
1110 gpa: Allocator,
1111 io: Io,
1112 diags: *Diags,
1113 dso: link.Input.Dso,
1114 shared_objects: *std.array_hash_map.String(File.Index),
1115 files: *std.MultiArrayList(File.Entry),
1116 target: *const std.Target,
1117) !void {
1118 const tracy = trace(@src());
1119 defer tracy.end();
1120
1121 const handle = dso.file;
1122
1123 const stat = Stat.fromFs(try handle.stat(io));
1124 var header = try SharedObject.parseHeader(gpa, io, diags, dso.path, handle, stat, target);
1125 defer header.deinit(gpa);
1126
1127 const soname = header.soname() orelse dso.path.basename();
1128
1129 const gop = try shared_objects.getOrPut(gpa, soname);
1130 if (gop.found_existing) return;
1131 errdefer _ = shared_objects.pop();
1132
1133 const index: File.Index = @intCast(try files.addOne(gpa));
1134 errdefer _ = files.pop();
1135
1136 gop.value_ptr.* = index;
1137
1138 var parsed = try SharedObject.parse(gpa, io, &header, handle);
1139 errdefer parsed.deinit(gpa);
1140
1141 const duped_path: Path = .{
1142 .root_dir = dso.path.root_dir,
1143 .sub_path = try gpa.dupe(u8, dso.path.sub_path),
1144 };
1145 errdefer gpa.free(duped_path.sub_path);
1146
1147 files.set(index, .{
1148 .shared_object = .{
1149 .parsed = parsed,
1150 .path = duped_path,
1151 .index = index,
1152 .needed = dso.needed,
1153 .alive = dso.needed,
1154 .aliases = null,
1155 .symbols = .empty,
1156 .symbols_extra = .empty,
1157 .symbols_resolver = .empty,
1158 .output_symtab_ctx = .{},
1159 },
1160 });
1161 const so = fileLookup(files.*, index, null).?.shared_object;
1162
1163 // TODO: save this work for later
1164 const nsyms = parsed.symbols.len;
1165 try so.symbols.ensureTotalCapacityPrecise(gpa, nsyms);
1166 try so.symbols_extra.ensureTotalCapacityPrecise(gpa, nsyms * @typeInfo(Symbol.Extra).@"struct".field_names.len);
1167 try so.symbols_resolver.ensureTotalCapacityPrecise(gpa, nsyms);
1168 so.symbols_resolver.appendNTimesAssumeCapacity(0, nsyms);
1169
1170 for (parsed.symtab, parsed.symbols, parsed.versyms, 0..) |esym, sym, versym, i| {
1171 const out_sym_index = so.addSymbolAssumeCapacity();
1172 const out_sym = &so.symbols.items[out_sym_index];
1173 out_sym.value = @intCast(esym.st_value);
1174 out_sym.name_offset = sym.mangled_name;
1175 out_sym.ref = .{ .index = 0, .file = 0 };
1176 out_sym.esym_index = @intCast(i);
1177 out_sym.version_index = versym;
1178 out_sym.extra_index = so.addSymbolExtraAssumeCapacity(.{});
1179 }
1180}
1181
1182/// When resolving symbols, we approach the problem similarly to `mold`.
1183/// 1. Resolve symbols across all objects (including those preemptively extracted archives).
1184/// 2. Resolve symbols across all shared objects.
1185/// 3. Mark live objects (see `Elf.markLive`)
1186/// 4. Reset state of all resolved globals since we will redo this bit on the pruned set.
1187/// 5. Remove references to dead objects/shared objects
1188/// 6. Re-run symbol resolution on pruned objects and shared objects sets.
1189pub fn resolveSymbols(self: *Elf) !void {
1190 // This function mutates `shared_objects`.
1191 const shared_objects = &self.shared_objects;
1192
1193 // Resolve symbols in the ZigObject. For now, we assume that it's always live.
1194 if (self.zigObjectPtr()) |zo| try zo.asFile().resolveSymbols(self);
1195 // Resolve symbols on the set of all objects and shared objects (even if some are unneeded).
1196 for (self.objects.items) |index| try self.file(index).?.resolveSymbols(self);
1197 for (shared_objects.values()) |index| try self.file(index).?.resolveSymbols(self);
1198 if (self.linkerDefinedPtr()) |obj| try obj.asFile().resolveSymbols(self);
1199
1200 // Mark live objects.
1201 self.markLive();
1202
1203 // Reset state of all globals after marking live objects.
1204 self.resolver.reset();
1205
1206 // Prune dead objects and shared objects.
1207 var i: usize = 0;
1208 while (i < self.objects.items.len) {
1209 const index = self.objects.items[i];
1210 if (!self.file(index).?.isAlive()) {
1211 _ = self.objects.orderedRemove(i);
1212 } else i += 1;
1213 }
1214 // TODO This loop has 2 major flaws:
1215 // 1. It is O(N^2) which is never allowed in the codebase.
1216 // 2. It mutates shared_objects, which is a non-starter for incremental compilation.
1217 i = 0;
1218 while (i < shared_objects.values().len) {
1219 const index = shared_objects.values()[i];
1220 if (!self.file(index).?.isAlive()) {
1221 _ = shared_objects.orderedRemoveAt(i);
1222 } else i += 1;
1223 }
1224
1225 {
1226 // Dedup groups.
1227 var table = std.StringHashMap(Ref).init(self.base.comp.gpa);
1228 defer table.deinit();
1229
1230 for (self.objects.items) |index| {
1231 try self.file(index).?.object.resolveGroups(self, &table);
1232 }
1233
1234 for (self.objects.items) |index| {
1235 self.file(index).?.object.markGroupsDead(self);
1236 }
1237 }
1238
1239 // Re-resolve the symbols.
1240 if (self.zigObjectPtr()) |zo| try zo.asFile().resolveSymbols(self);
1241 for (self.objects.items) |index| try self.file(index).?.resolveSymbols(self);
1242 for (shared_objects.values()) |index| try self.file(index).?.resolveSymbols(self);
1243 if (self.linkerDefinedPtr()) |obj| try obj.asFile().resolveSymbols(self);
1244}
1245
1246/// Traverses all objects and shared objects marking any object referenced by
1247/// a live object/shared object as alive itself.
1248/// This routine will prune unneeded objects extracted from archives and
1249/// unneeded shared objects.
1250fn markLive(self: *Elf) void {
1251 const shared_objects = self.shared_objects.values();
1252 if (self.zigObjectPtr()) |zig_object| zig_object.asFile().markLive(self);
1253 for (self.objects.items) |index| {
1254 const file_ptr = self.file(index).?;
1255 if (file_ptr.isAlive()) file_ptr.markLive(self);
1256 }
1257 for (shared_objects) |index| {
1258 const file_ptr = self.file(index).?;
1259 if (file_ptr.isAlive()) file_ptr.markLive(self);
1260 }
1261}
1262
1263pub fn markEhFrameAtomsDead(self: *Elf) void {
1264 for (self.objects.items) |index| {
1265 const file_ptr = self.file(index).?;
1266 if (!file_ptr.isAlive()) continue;
1267 file_ptr.object.markEhFrameAtomsDead(self);
1268 }
1269}
1270
1271fn markImportsExports(self: *Elf) void {
1272 const shared_objects = self.shared_objects.values();
1273 if (self.zigObjectPtr()) |zo| {
1274 zo.markImportsExports(self);
1275 }
1276 for (self.objects.items) |index| {
1277 self.file(index).?.object.markImportsExports(self);
1278 }
1279 if (!self.isEffectivelyDynLib()) {
1280 for (shared_objects) |index| {
1281 self.file(index).?.shared_object.markImportExports(self);
1282 }
1283 }
1284}
1285
1286fn claimUnresolved(self: *Elf) void {
1287 if (self.zigObjectPtr()) |zig_object| {
1288 zig_object.claimUnresolved(self);
1289 }
1290 for (self.objects.items) |index| {
1291 self.file(index).?.object.claimUnresolved(self);
1292 }
1293}
1294
1295/// In scanRelocs we will go over all live atoms and scan their relocs.
1296/// This will help us work out what synthetics to emit, GOT indirection, etc.
1297/// This is also the point where we will report undefined symbols for any
1298/// alloc sections.
1299fn scanRelocs(self: *Elf) !void {
1300 const gpa = self.base.comp.gpa;
1301 const shared_objects = self.shared_objects.values();
1302
1303 var undefs: std.array_hash_map.Auto(SymbolResolver.Index, std.array_list.Managed(Ref)) = .empty;
1304 defer {
1305 for (undefs.values()) |*refs| refs.deinit();
1306 undefs.deinit(gpa);
1307 }
1308
1309 var has_reloc_errors = false;
1310 if (self.zigObjectPtr()) |zo| {
1311 zo.asFile().scanRelocs(self, &undefs) catch |err| switch (err) {
1312 error.RelaxFailure => unreachable,
1313 error.UnsupportedCpuArch => {
1314 try self.reportUnsupportedCpuArch();
1315 return error.AlreadyReported;
1316 },
1317 error.RelocFailure => has_reloc_errors = true,
1318 else => |e| return e,
1319 };
1320 }
1321 for (self.objects.items) |index| {
1322 self.file(index).?.scanRelocs(self, &undefs) catch |err| switch (err) {
1323 error.RelaxFailure => unreachable,
1324 error.UnsupportedCpuArch => {
1325 try self.reportUnsupportedCpuArch();
1326 return error.AlreadyReported;
1327 },
1328 error.RelocFailure => has_reloc_errors = true,
1329 else => |e| return e,
1330 };
1331 }
1332
1333 try self.reportUndefinedSymbols(&undefs);
1334
1335 if (has_reloc_errors) return error.AlreadyReported;
1336
1337 if (self.zigObjectPtr()) |zo| {
1338 try zo.asFile().createSymbolIndirection(self);
1339 }
1340 for (self.objects.items) |index| {
1341 try self.file(index).?.createSymbolIndirection(self);
1342 }
1343 for (shared_objects) |index| {
1344 try self.file(index).?.createSymbolIndirection(self);
1345 }
1346 if (self.linkerDefinedPtr()) |obj| {
1347 try obj.asFile().createSymbolIndirection(self);
1348 }
1349 if (self.got.flags.needs_tlsld) {
1350 log.debug("program needs TLSLD", .{});
1351 try self.got.addTlsLdSymbol(self);
1352 }
1353}
1354
1355pub fn initOutputSection(self: *Elf, args: struct {
1356 name: [:0]const u8,
1357 flags: u64,
1358 type: u32,
1359}) error{OutOfMemory}!u32 {
1360 const name = blk: {
1361 if (self.base.isRelocatable()) break :blk args.name;
1362 if (args.flags & elf.SHF_MERGE != 0) break :blk args.name;
1363 const name_prefixes: []const [:0]const u8 = &.{
1364 ".text", ".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",
1365 ".preinit_array", ".init_array", ".fini_array", ".tbss", ".tdata", ".gcc_except_table",
1366 ".ctors", ".dtors", ".gnu.warning",
1367 };
1368 inline for (name_prefixes) |prefix| {
1369 if (mem.eql(u8, args.name, prefix) or mem.startsWith(u8, args.name, prefix ++ ".")) {
1370 break :blk prefix;
1371 }
1372 }
1373 break :blk args.name;
1374 };
1375 const @"type" = tt: {
1376 if (self.getTarget().cpu.arch == .x86_64 and args.type == elf.SHT_X86_64_UNWIND)
1377 break :tt elf.SHT_PROGBITS;
1378 switch (args.type) {
1379 elf.SHT_NULL => unreachable,
1380 elf.SHT_PROGBITS => {
1381 if (mem.eql(u8, args.name, ".preinit_array") or mem.startsWith(u8, args.name, ".preinit_array."))
1382 break :tt elf.SHT_PREINIT_ARRAY;
1383 if (mem.eql(u8, args.name, ".init_array") or mem.startsWith(u8, args.name, ".init_array."))
1384 break :tt elf.SHT_INIT_ARRAY;
1385 if (mem.eql(u8, args.name, ".fini_array") or mem.startsWith(u8, args.name, ".fini_array."))
1386 break :tt elf.SHT_FINI_ARRAY;
1387 break :tt args.type;
1388 },
1389 else => break :tt args.type,
1390 }
1391 };
1392 const flags = blk: {
1393 var flags = args.flags;
1394 if (!self.base.isRelocatable()) {
1395 flags &= ~@as(u64, elf.SHF_COMPRESSED | elf.SHF_GROUP | elf.SHF_GNU_RETAIN);
1396 }
1397 break :blk switch (@"type") {
1398 elf.SHT_INIT_ARRAY, elf.SHT_FINI_ARRAY => flags | elf.SHF_WRITE,
1399 else => flags,
1400 };
1401 };
1402 const out_shndx = self.sectionByName(name) orelse try self.addSection(.{
1403 .type = @"type",
1404 .flags = flags,
1405 .name = try self.insertShString(name),
1406 });
1407 return out_shndx;
1408}
1409
1410pub fn writeShdrTable(self: *Elf) !void {
1411 const gpa = self.base.comp.gpa;
1412 const target_endian = self.getTarget().cpu.arch.endian();
1413 const foreign_endian = target_endian != builtin.cpu.arch.endian();
1414 const shsize: u64 = switch (self.ptr_width) {
1415 .p32 => @sizeOf(elf.Elf32_Shdr),
1416 .p64 => @sizeOf(elf.Elf64_Shdr),
1417 };
1418 const shalign: u16 = switch (self.ptr_width) {
1419 .p32 => @alignOf(elf.Elf32_Shdr),
1420 .p64 => @alignOf(elf.Elf64_Shdr),
1421 };
1422
1423 const shoff = self.shdr_table_offset orelse 0;
1424 const needed_size = self.sections.items(.shdr).len * shsize;
1425
1426 if (needed_size > self.allocatedSize(shoff)) {
1427 self.shdr_table_offset = null;
1428 self.shdr_table_offset = try self.findFreeSpace(needed_size, shalign);
1429 }
1430
1431 log.debug("writing section headers from 0x{x} to 0x{x}", .{
1432 self.shdr_table_offset.?,
1433 self.shdr_table_offset.? + needed_size,
1434 });
1435
1436 switch (self.ptr_width) {
1437 .p32 => {
1438 const buf = try gpa.alloc(elf.Elf32_Shdr, self.sections.items(.shdr).len);
1439 defer gpa.free(buf);
1440
1441 for (buf, 0..) |*shdr, i| {
1442 assert(self.sections.items(.shdr)[i].sh_offset != math.maxInt(u64));
1443 shdr.* = shdrTo32(self.sections.items(.shdr)[i]);
1444 if (foreign_endian) {
1445 mem.byteSwapAllFields(elf.Elf32_Shdr, shdr);
1446 }
1447 }
1448 try self.pwriteAll(@ptrCast(buf), self.shdr_table_offset.?);
1449 },
1450 .p64 => {
1451 const buf = try gpa.alloc(elf.Elf64_Shdr, self.sections.items(.shdr).len);
1452 defer gpa.free(buf);
1453
1454 for (buf, 0..) |*shdr, i| {
1455 assert(self.sections.items(.shdr)[i].sh_offset != math.maxInt(u64));
1456 shdr.* = self.sections.items(.shdr)[i];
1457 if (foreign_endian) {
1458 mem.byteSwapAllFields(elf.Elf64_Shdr, shdr);
1459 }
1460 }
1461 try self.pwriteAll(@ptrCast(buf), self.shdr_table_offset.?);
1462 },
1463 }
1464}
1465
1466fn writePhdrTable(self: *Elf) !void {
1467 const gpa = self.base.comp.gpa;
1468 const target_endian = self.getTarget().cpu.arch.endian();
1469 const foreign_endian = target_endian != builtin.cpu.arch.endian();
1470 const phdr_table = &self.phdrs.items[self.phdr_indexes.table.int().?];
1471
1472 log.debug("writing program headers from 0x{x} to 0x{x}", .{
1473 phdr_table.offset,
1474 phdr_table.offset + phdr_table.filesz,
1475 });
1476
1477 switch (self.ptr_width) {
1478 .p32 => {
1479 const buf = try gpa.alloc(elf.Elf32.Phdr, self.phdrs.items.len);
1480 defer gpa.free(buf);
1481
1482 for (buf, 0..) |*phdr, i| {
1483 phdr.* = phdrTo32(self.phdrs.items[i]);
1484 if (foreign_endian) {
1485 mem.byteSwapAllFields(elf.Elf32.Phdr, phdr);
1486 }
1487 }
1488 try self.pwriteAll(@ptrCast(buf), phdr_table.offset);
1489 },
1490 .p64 => {
1491 const buf = try gpa.alloc(elf.Elf64.Phdr, self.phdrs.items.len);
1492 defer gpa.free(buf);
1493
1494 for (buf, 0..) |*phdr, i| {
1495 phdr.* = self.phdrs.items[i];
1496 if (foreign_endian) {
1497 mem.byteSwapAllFields(elf.Elf64.Phdr, phdr);
1498 }
1499 }
1500 try self.pwriteAll(@ptrCast(buf), phdr_table.offset);
1501 },
1502 }
1503}
1504
1505pub fn writeElfHeader(self: *Elf) !void {
1506 const diags = &self.base.comp.link_diags;
1507 if (diags.hasErrors()) return; // We had errors, so skip flushing to render the output unusable
1508
1509 const comp = self.base.comp;
1510 var hdr_buf: [@sizeOf(elf.Elf64_Ehdr)]u8 = undefined;
1511
1512 var index: usize = 0;
1513 hdr_buf[0..4].* = elf.MAGIC.*;
1514 index += 4;
1515
1516 hdr_buf[index] = switch (self.ptr_width) {
1517 .p32 => elf.ELFCLASS32,
1518 .p64 => elf.ELFCLASS64,
1519 };
1520 index += 1;
1521
1522 const target = self.getTarget();
1523 const endian = target.cpu.arch.endian();
1524 hdr_buf[index] = switch (endian) {
1525 .little => elf.ELFDATA2LSB,
1526 .big => elf.ELFDATA2MSB,
1527 };
1528 index += 1;
1529
1530 hdr_buf[index] = 1; // ELF version
1531 index += 1;
1532
1533 hdr_buf[index] = @backingInt(@as(elf.OSABI, switch (target.cpu.arch) {
1534 .amdgcn => switch (target.os.tag) {
1535 .amdhsa => .AMDGPU_HSA,
1536 .amdpal => .AMDGPU_PAL,
1537 .mesa3d => .AMDGPU_MESA3D,
1538 else => .NONE,
1539 },
1540 .msp430 => .STANDALONE,
1541 else => switch (target.os.tag) {
1542 .freebsd, .ps4 => .FREEBSD,
1543 .hermit => .STANDALONE,
1544 .illumos => .SOLARIS,
1545 .openbsd => .OPENBSD,
1546 else => .NONE,
1547 },
1548 }));
1549 index += 1;
1550
1551 // ABI Version, possibly used by glibc but not by static executables
1552 // padding
1553 @memset(hdr_buf[index..][0..8], 0);
1554 index += 8;
1555
1556 assert(index == 16);
1557
1558 const output_mode = comp.config.output_mode;
1559 const link_mode = comp.config.link_mode;
1560 const elf_type: elf.ET = switch (output_mode) {
1561 .Exe => if (comp.config.pie or target.os.tag == .haiku) .DYN else .EXEC,
1562 .Obj => .REL,
1563 .Lib => switch (link_mode) {
1564 .static => @as(elf.ET, .REL),
1565 .dynamic => .DYN,
1566 },
1567 };
1568 mem.writeInt(u16, hdr_buf[index..][0..2], @backingInt(elf_type), endian);
1569 index += 2;
1570
1571 const machine = target.toElfMachine();
1572 mem.writeInt(u16, hdr_buf[index..][0..2], @backingInt(machine), endian);
1573 index += 2;
1574
1575 // ELF Version, again
1576 mem.writeInt(u32, hdr_buf[index..][0..4], 1, endian);
1577 index += 4;
1578
1579 const e_entry: u64 = if (self.linkerDefinedPtr()) |obj| blk: {
1580 const entry_sym = obj.entrySymbol(self) orelse break :blk 0;
1581 break :blk @intCast(entry_sym.address(.{}, self));
1582 } else 0;
1583 const phdr_table_offset = if (self.phdr_indexes.table.int()) |phndx| self.phdrs.items[phndx].offset else 0;
1584 switch (self.ptr_width) {
1585 .p32 => {
1586 mem.writeInt(u32, hdr_buf[index..][0..4], @intCast(e_entry), endian);
1587 index += 4;
1588
1589 // e_phoff
1590 mem.writeInt(u32, hdr_buf[index..][0..4], @intCast(phdr_table_offset), endian);
1591 index += 4;
1592
1593 // e_shoff
1594 mem.writeInt(u32, hdr_buf[index..][0..4], @intCast(self.shdr_table_offset.?), endian);
1595 index += 4;
1596 },
1597 .p64 => {
1598 // e_entry
1599 mem.writeInt(u64, hdr_buf[index..][0..8], e_entry, endian);
1600 index += 8;
1601
1602 // e_phoff
1603 mem.writeInt(u64, hdr_buf[index..][0..8], phdr_table_offset, endian);
1604 index += 8;
1605
1606 // e_shoff
1607 mem.writeInt(u64, hdr_buf[index..][0..8], self.shdr_table_offset.?, endian);
1608 index += 8;
1609 },
1610 }
1611
1612 const e_flags = 0;
1613 mem.writeInt(u32, hdr_buf[index..][0..4], e_flags, endian);
1614 index += 4;
1615
1616 const e_ehsize: u16 = switch (self.ptr_width) {
1617 .p32 => @sizeOf(elf.Elf32_Ehdr),
1618 .p64 => @sizeOf(elf.Elf64_Ehdr),
1619 };
1620 mem.writeInt(u16, hdr_buf[index..][0..2], e_ehsize, endian);
1621 index += 2;
1622
1623 const e_phentsize: u16 = switch (self.ptr_width) {
1624 .p32 => @sizeOf(elf.Elf32.Phdr),
1625 .p64 => @sizeOf(elf.Elf64.Phdr),
1626 };
1627 mem.writeInt(u16, hdr_buf[index..][0..2], e_phentsize, endian);
1628 index += 2;
1629
1630 const e_phnum = @as(u16, @intCast(self.phdrs.items.len));
1631 mem.writeInt(u16, hdr_buf[index..][0..2], e_phnum, endian);
1632 index += 2;
1633
1634 const e_shentsize: u16 = switch (self.ptr_width) {
1635 .p32 => @sizeOf(elf.Elf32_Shdr),
1636 .p64 => @sizeOf(elf.Elf64_Shdr),
1637 };
1638 mem.writeInt(u16, hdr_buf[index..][0..2], e_shentsize, endian);
1639 index += 2;
1640
1641 const e_shnum: u16 = @intCast(self.sections.items(.shdr).len);
1642 mem.writeInt(u16, hdr_buf[index..][0..2], e_shnum, endian);
1643 index += 2;
1644
1645 mem.writeInt(u16, hdr_buf[index..][0..2], @intCast(self.section_indexes.shstrtab.?), endian);
1646 index += 2;
1647
1648 assert(index == e_ehsize);
1649
1650 try self.pwriteAll(hdr_buf[0..index], 0);
1651}
1652
1653pub fn freeNav(self: *Elf, nav: InternPool.Nav.Index) void {
1654 return self.zigObjectPtr().?.freeNav(self, nav);
1655}
1656
1657pub fn updateFunc(
1658 self: *Elf,
1659 pt: Zcu.PerThread,
1660 func_index: InternPool.Index,
1661 mir: *const codegen.AnyMir,
1662) link.Error!void {
1663 return self.zigObjectPtr().?.updateFunc(self, pt, func_index, mir);
1664}
1665
1666pub fn updateNav(
1667 self: *Elf,
1668 pt: Zcu.PerThread,
1669 nav: InternPool.Nav.Index,
1670) link.Error!void {
1671 return self.zigObjectPtr().?.updateNav(self, pt, nav);
1672}
1673
1674pub fn updateContainerType(
1675 self: *Elf,
1676 pt: Zcu.PerThread,
1677 ty: InternPool.Index,
1678 success: bool,
1679) link.Error!void {
1680 return self.zigObjectPtr().?.updateContainerType(pt, ty, success) catch |err| switch (err) {
1681 error.OutOfMemory => |e| return e,
1682 };
1683}
1684
1685pub fn updateExports(
1686 self: *Elf,
1687 pt: Zcu.PerThread,
1688 export_indices: []const Zcu.Export.Index,
1689) link.Error!void {
1690 return self.zigObjectPtr().?.updateExports(self, pt, export_indices);
1691}
1692
1693pub fn updateLineNumber(self: *Elf, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) link.Error!void {
1694 return self.zigObjectPtr().?.updateLineNumber(pt, ti_id);
1695}
1696
1697fn checkDuplicates(self: *Elf) !void {
1698 const gpa = self.base.comp.gpa;
1699
1700 var dupes: std.array_hash_map.Auto(SymbolResolver.Index, std.ArrayList(File.Index)) = .empty;
1701 defer {
1702 for (dupes.values()) |*list| {
1703 list.deinit(gpa);
1704 }
1705 dupes.deinit(gpa);
1706 }
1707
1708 if (self.zigObjectPtr()) |zig_object| {
1709 try zig_object.checkDuplicates(&dupes, self);
1710 }
1711 for (self.objects.items) |index| {
1712 try self.file(index).?.object.checkDuplicates(&dupes, self);
1713 }
1714
1715 try self.reportDuplicates(dupes);
1716}
1717
1718pub fn addCommentString(self: *Elf) !void {
1719 const gpa = self.base.comp.gpa;
1720 if (self.comment_merge_section_index != null) return;
1721 const msec_index = try self.getOrCreateMergeSection(".comment", elf.SHF_MERGE | elf.SHF_STRINGS, elf.SHT_PROGBITS);
1722 const msec = self.mergeSection(msec_index);
1723 const res = try msec.insertZ(gpa, "zig " ++ builtin.zig_version_string);
1724 if (res.found_existing) return;
1725 const msub_index = try msec.addMergeSubsection(gpa);
1726 const msub = msec.mergeSubsection(msub_index);
1727 msub.merge_section_index = msec_index;
1728 msub.string_index = res.key.pos;
1729 msub.alignment = .@"1";
1730 msub.size = res.key.len;
1731 msub.entsize = 1;
1732 msub.alive = true;
1733 res.sub.* = msub_index;
1734 self.comment_merge_section_index = msec_index;
1735}
1736
1737pub fn resolveMergeSections(self: *Elf) !void {
1738 const tracy = trace(@src());
1739 defer tracy.end();
1740
1741 var has_errors = false;
1742 for (self.objects.items) |index| {
1743 const object = self.file(index).?.object;
1744 if (!object.alive) continue;
1745 if (!object.dirty) continue;
1746 object.initInputMergeSections(self) catch |err| switch (err) {
1747 error.AlreadyReported => has_errors = true,
1748 else => |e| return e,
1749 };
1750 }
1751
1752 if (has_errors) return error.AlreadyReported;
1753
1754 for (self.objects.items) |index| {
1755 const object = self.file(index).?.object;
1756 if (!object.alive) continue;
1757 if (!object.dirty) continue;
1758 try object.initOutputMergeSections(self);
1759 }
1760
1761 for (self.objects.items) |index| {
1762 const object = self.file(index).?.object;
1763 if (!object.alive) continue;
1764 if (!object.dirty) continue;
1765 object.resolveMergeSubsections(self) catch |err| switch (err) {
1766 error.AlreadyReported => has_errors = true,
1767 else => |e| return e,
1768 };
1769 }
1770
1771 if (has_errors) return error.AlreadyReported;
1772}
1773
1774pub fn finalizeMergeSections(self: *Elf) !void {
1775 for (self.merge_sections.items) |*msec| {
1776 try msec.finalize(self.base.comp.gpa);
1777 }
1778}
1779
1780pub fn updateMergeSectionSizes(self: *Elf) !void {
1781 for (self.merge_sections.items) |*msec| {
1782 msec.updateSize();
1783 }
1784 for (self.merge_sections.items) |*msec| {
1785 const shdr = &self.sections.items(.shdr)[msec.output_section_index];
1786 const offset = msec.alignment.forward(shdr.sh_size);
1787 const padding = offset - shdr.sh_size;
1788 msec.value = @intCast(offset);
1789 shdr.sh_size += padding + msec.size;
1790 shdr.sh_addralign = @max(shdr.sh_addralign, msec.alignment.toByteUnits() orelse 1);
1791 shdr.sh_entsize = if (shdr.sh_entsize == 0) msec.entsize else @min(shdr.sh_entsize, msec.entsize);
1792 }
1793}
1794
1795pub fn writeMergeSections(self: *Elf) !void {
1796 const gpa = self.base.comp.gpa;
1797 var buffer = std.array_list.Managed(u8).init(gpa);
1798 defer buffer.deinit();
1799
1800 for (self.merge_sections.items) |*msec| {
1801 const shdr = self.sections.items(.shdr)[msec.output_section_index];
1802 const fileoff = try self.cast(usize, msec.value + shdr.sh_offset);
1803 const size = try self.cast(usize, msec.size);
1804 try buffer.ensureTotalCapacity(size);
1805 buffer.appendNTimesAssumeCapacity(0, size);
1806
1807 for (msec.finalized_subsections.items) |msub_index| {
1808 const msub = msec.mergeSubsection(msub_index);
1809 assert(msub.alive);
1810 const string = msub.getString(self);
1811 const off = try self.cast(usize, msub.value);
1812 @memcpy(buffer.items[off..][0..string.len], string);
1813 }
1814
1815 try self.pwriteAll(buffer.items, fileoff);
1816 buffer.clearRetainingCapacity();
1817 }
1818}
1819
1820fn initOutputSections(self: *Elf) !void {
1821 for (self.objects.items) |index| {
1822 try self.file(index).?.object.initOutputSections(self);
1823 }
1824 for (self.merge_sections.items) |*msec| {
1825 if (msec.finalized_subsections.items.len == 0) continue;
1826 try msec.initOutputSection(self);
1827 }
1828}
1829
1830fn initSyntheticSections(self: *Elf) !void {
1831 const comp = self.base.comp;
1832 const target = self.getTarget();
1833 const ptr_size = self.ptrWidthBytes();
1834
1835 const is_exe_or_dyn_lib = switch (comp.config.output_mode) {
1836 .Exe => true,
1837 .Lib => comp.config.link_mode == .dynamic,
1838 .Obj => false,
1839 };
1840 const have_dynamic_linker = comp.config.link_mode == .dynamic and is_exe_or_dyn_lib;
1841
1842 const needs_eh_frame = blk: {
1843 if (self.zigObjectPtr()) |zo|
1844 if (zo.eh_frame_index != null) break :blk true;
1845 break :blk for (self.objects.items) |index| {
1846 if (self.file(index).?.object.cies.items.len > 0) break true;
1847 } else false;
1848 };
1849
1850 if (needs_eh_frame) {
1851 if (self.section_indexes.eh_frame == null) {
1852 self.section_indexes.eh_frame = self.sectionByName(".eh_frame") orelse try self.addSection(.{
1853 .name = try self.insertShString(".eh_frame"),
1854 .type = if (target.cpu.arch == .x86_64)
1855 elf.SHT_X86_64_UNWIND
1856 else
1857 elf.SHT_PROGBITS,
1858 .flags = elf.SHF_ALLOC,
1859 .addralign = ptr_size,
1860 });
1861 }
1862 if (comp.link_eh_frame_hdr and self.section_indexes.eh_frame_hdr == null) {
1863 self.section_indexes.eh_frame_hdr = try self.addSection(.{
1864 .name = try self.insertShString(".eh_frame_hdr"),
1865 .type = elf.SHT_PROGBITS,
1866 .flags = elf.SHF_ALLOC,
1867 .addralign = 4,
1868 });
1869 }
1870 }
1871
1872 if (self.got.entries.items.len > 0 and self.section_indexes.got == null) {
1873 self.section_indexes.got = try self.addSection(.{
1874 .name = try self.insertShString(".got"),
1875 .type = elf.SHT_PROGBITS,
1876 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1877 .addralign = ptr_size,
1878 });
1879 }
1880
1881 if (have_dynamic_linker) {
1882 if (self.section_indexes.got_plt == null) {
1883 self.section_indexes.got_plt = try self.addSection(.{
1884 .name = try self.insertShString(".got.plt"),
1885 .type = elf.SHT_PROGBITS,
1886 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1887 .addralign = @alignOf(u64),
1888 });
1889 }
1890 } else {
1891 assert(self.plt.symbols.items.len == 0);
1892 }
1893
1894 const needs_rela_dyn = blk: {
1895 if (self.got.flags.needs_rela or self.got.flags.needs_tlsld or self.copy_rel.symbols.items.len > 0)
1896 break :blk true;
1897 if (self.zigObjectPtr()) |zig_object| {
1898 if (zig_object.num_dynrelocs > 0) break :blk true;
1899 }
1900 for (self.objects.items) |index| {
1901 if (self.file(index).?.object.num_dynrelocs > 0) break :blk true;
1902 }
1903 break :blk false;
1904 };
1905 if (needs_rela_dyn and self.section_indexes.rela_dyn == null) {
1906 self.section_indexes.rela_dyn = try self.addSection(.{
1907 .name = try self.insertShString(".rela.dyn"),
1908 .type = elf.SHT_RELA,
1909 .flags = elf.SHF_ALLOC,
1910 .addralign = @alignOf(elf.Elf64_Rela),
1911 .entsize = @sizeOf(elf.Elf64_Rela),
1912 });
1913 }
1914
1915 if (self.plt.symbols.items.len > 0) {
1916 if (self.section_indexes.plt == null) {
1917 self.section_indexes.plt = try self.addSection(.{
1918 .name = try self.insertShString(".plt"),
1919 .type = elf.SHT_PROGBITS,
1920 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
1921 .addralign = 16,
1922 });
1923 }
1924 if (self.section_indexes.rela_plt == null) {
1925 self.section_indexes.rela_plt = try self.addSection(.{
1926 .name = try self.insertShString(".rela.plt"),
1927 .type = elf.SHT_RELA,
1928 .flags = elf.SHF_ALLOC,
1929 .addralign = @alignOf(elf.Elf64_Rela),
1930 .entsize = @sizeOf(elf.Elf64_Rela),
1931 });
1932 }
1933 }
1934
1935 if (self.plt_got.symbols.items.len > 0 and self.section_indexes.plt_got == null) {
1936 self.section_indexes.plt_got = try self.addSection(.{
1937 .name = try self.insertShString(".plt.got"),
1938 .type = elf.SHT_PROGBITS,
1939 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
1940 .addralign = 16,
1941 });
1942 }
1943
1944 if (self.copy_rel.symbols.items.len > 0 and self.section_indexes.copy_rel == null) {
1945 self.section_indexes.copy_rel = try self.addSection(.{
1946 .name = try self.insertShString(".copyrel"),
1947 .type = elf.SHT_NOBITS,
1948 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1949 });
1950 }
1951
1952 if (needs_interp: {
1953 if (comp.config.link_mode == .static) break :needs_interp false;
1954 if (target.dynamic_linker.get() == null) break :needs_interp false;
1955 break :needs_interp switch (comp.config.output_mode) {
1956 .Exe => true,
1957 .Lib => comp.root_mod.resolved_target.is_explicit_dynamic_linker,
1958 .Obj => false,
1959 };
1960 } and self.section_indexes.interp == null) {
1961 self.section_indexes.interp = try self.addSection(.{
1962 .name = try self.insertShString(".interp"),
1963 .type = elf.SHT_PROGBITS,
1964 .flags = elf.SHF_ALLOC,
1965 .addralign = 1,
1966 });
1967 }
1968
1969 if (have_dynamic_linker or comp.config.pie or self.isEffectivelyDynLib()) {
1970 if (self.section_indexes.dynstrtab == null) {
1971 self.section_indexes.dynstrtab = try self.addSection(.{
1972 .name = try self.insertShString(".dynstr"),
1973 .flags = elf.SHF_ALLOC,
1974 .type = elf.SHT_STRTAB,
1975 .entsize = 1,
1976 .addralign = 1,
1977 });
1978 }
1979 if (self.section_indexes.dynamic == null) {
1980 self.section_indexes.dynamic = try self.addSection(.{
1981 .name = try self.insertShString(".dynamic"),
1982 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
1983 .type = elf.SHT_DYNAMIC,
1984 .entsize = @sizeOf(elf.Elf64_Dyn),
1985 .addralign = @alignOf(elf.Elf64_Dyn),
1986 });
1987 }
1988 if (self.section_indexes.dynsymtab == null) {
1989 self.section_indexes.dynsymtab = try self.addSection(.{
1990 .name = try self.insertShString(".dynsym"),
1991 .flags = elf.SHF_ALLOC,
1992 .type = elf.SHT_DYNSYM,
1993 .addralign = @alignOf(elf.Elf64_Sym),
1994 .entsize = @sizeOf(elf.Elf64_Sym),
1995 .info = 1,
1996 });
1997 }
1998 if (self.section_indexes.hash == null) {
1999 self.section_indexes.hash = try self.addSection(.{
2000 .name = try self.insertShString(".hash"),
2001 .flags = elf.SHF_ALLOC,
2002 .type = elf.SHT_HASH,
2003 .addralign = 4,
2004 .entsize = 4,
2005 });
2006 }
2007 if (self.section_indexes.gnu_hash == null) {
2008 self.section_indexes.gnu_hash = try self.addSection(.{
2009 .name = try self.insertShString(".gnu.hash"),
2010 .flags = elf.SHF_ALLOC,
2011 .type = elf.SHT_GNU_HASH,
2012 .addralign = 8,
2013 });
2014 }
2015
2016 const needs_versions = for (self.dynsym.entries.items) |entry| {
2017 const sym = self.symbol(entry.ref).?;
2018 if (sym.flags.import and sym.version_index.VERSION > elf.Versym.GLOBAL.VERSION) break true;
2019 } else false;
2020 if (needs_versions) {
2021 if (self.section_indexes.versym == null) {
2022 self.section_indexes.versym = try self.addSection(.{
2023 .name = try self.insertShString(".gnu.version"),
2024 .flags = elf.SHF_ALLOC,
2025 .type = elf.SHT_GNU_VERSYM,
2026 .addralign = @alignOf(elf.Versym),
2027 .entsize = @sizeOf(elf.Versym),
2028 });
2029 }
2030 if (self.section_indexes.verneed == null) {
2031 self.section_indexes.verneed = try self.addSection(.{
2032 .name = try self.insertShString(".gnu.version_r"),
2033 .flags = elf.SHF_ALLOC,
2034 .type = elf.SHT_GNU_VERNEED,
2035 .addralign = @alignOf(elf.Elf64_Verneed),
2036 });
2037 }
2038 }
2039 }
2040
2041 try self.initSymtab();
2042 try self.initShStrtab();
2043}
2044
2045pub fn initSymtab(self: *Elf) !void {
2046 const small_ptr = switch (self.ptr_width) {
2047 .p32 => true,
2048 .p64 => false,
2049 };
2050 if (self.section_indexes.symtab == null) {
2051 self.section_indexes.symtab = try self.addSection(.{
2052 .name = try self.insertShString(".symtab"),
2053 .type = elf.SHT_SYMTAB,
2054 .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym),
2055 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),
2056 });
2057 }
2058 if (self.section_indexes.strtab == null) {
2059 self.section_indexes.strtab = try self.addSection(.{
2060 .name = try self.insertShString(".strtab"),
2061 .type = elf.SHT_STRTAB,
2062 .entsize = 1,
2063 .addralign = 1,
2064 });
2065 }
2066}
2067
2068pub fn initShStrtab(self: *Elf) !void {
2069 if (self.section_indexes.shstrtab == null) {
2070 self.section_indexes.shstrtab = try self.addSection(.{
2071 .name = try self.insertShString(".shstrtab"),
2072 .type = elf.SHT_STRTAB,
2073 .entsize = 1,
2074 .addralign = 1,
2075 });
2076 }
2077}
2078
2079fn initSpecialPhdrs(self: *Elf) !void {
2080 comptime assert(max_number_of_special_phdrs == 5);
2081
2082 if (self.section_indexes.interp != null and self.phdr_indexes.interp == .none) {
2083 self.phdr_indexes.interp = (try self.addPhdr(.{
2084 .type = @backingInt(elf.PT.INTERP),
2085 .flags = elf.PF_R,
2086 .@"align" = 1,
2087 })).toOptional();
2088 }
2089 if (self.section_indexes.dynamic != null and self.phdr_indexes.dynamic == .none) {
2090 self.phdr_indexes.dynamic = (try self.addPhdr(.{
2091 .type = @backingInt(elf.PT.DYNAMIC),
2092 .flags = elf.PF_R | elf.PF_W,
2093 })).toOptional();
2094 }
2095 if (self.section_indexes.eh_frame_hdr != null and self.phdr_indexes.gnu_eh_frame == .none) {
2096 self.phdr_indexes.gnu_eh_frame = (try self.addPhdr(.{
2097 .type = @backingInt(elf.PT.GNU_EH_FRAME),
2098 .flags = elf.PF_R,
2099 })).toOptional();
2100 }
2101 if (self.phdr_indexes.gnu_stack == .none) {
2102 self.phdr_indexes.gnu_stack = (try self.addPhdr(.{
2103 .type = @backingInt(elf.PT.GNU_STACK),
2104 .flags = elf.PF_W | elf.PF_R,
2105 .memsz = self.base.stack_size,
2106 .@"align" = 1,
2107 })).toOptional();
2108 }
2109
2110 const has_tls = for (self.sections.items(.shdr)) |shdr| {
2111 if (shdr.sh_flags & elf.SHF_TLS != 0) break true;
2112 } else false;
2113 if (has_tls and self.phdr_indexes.tls == .none) {
2114 self.phdr_indexes.tls = (try self.addPhdr(.{
2115 .type = @backingInt(elf.PT.TLS),
2116 .flags = elf.PF_R,
2117 .@"align" = 1,
2118 })).toOptional();
2119 }
2120}
2121
2122/// We need to sort constructors/destuctors in the following sections:
2123/// * .init_array
2124/// * .fini_array
2125/// * .preinit_array
2126/// * .ctors
2127/// * .dtors
2128/// The prority of inclusion is defined as part of the input section's name. For example, .init_array.10000.
2129/// If no priority value has been specified,
2130/// * for .init_array, .fini_array and .preinit_array, we automatically assign that section max value of maxInt(i32)
2131/// and push it to the back of the queue,
2132/// * for .ctors and .dtors, we automatically assign that section min value of -1
2133/// and push it to the front of the queue,
2134/// crtbegin and ctrend are assigned minInt(i32) and maxInt(i32) respectively.
2135/// Ties are broken by the file prority which corresponds to the inclusion of input sections in this output section
2136/// we are about to sort.
2137fn sortInitFini(self: *Elf) !void {
2138 const gpa = self.base.comp.gpa;
2139 const slice = self.sections.slice();
2140
2141 const Entry = struct {
2142 priority: i32,
2143 atom_ref: Ref,
2144
2145 pub fn lessThan(ctx: *Elf, lhs: @This(), rhs: @This()) bool {
2146 if (lhs.priority == rhs.priority) {
2147 return ctx.atom(lhs.atom_ref).?.priority(ctx) < ctx.atom(rhs.atom_ref).?.priority(ctx);
2148 }
2149 return lhs.priority < rhs.priority;
2150 }
2151 };
2152
2153 for (slice.items(.shdr), slice.items(.atom_list_2)) |shdr, *atom_list| {
2154 if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue;
2155 if (atom_list.atoms.keys().len == 0) continue;
2156
2157 var is_init_fini = false;
2158 var is_ctor_dtor = false;
2159 switch (shdr.sh_type) {
2160 elf.SHT_PREINIT_ARRAY,
2161 elf.SHT_INIT_ARRAY,
2162 elf.SHT_FINI_ARRAY,
2163 => is_init_fini = true,
2164 else => {
2165 const name = self.getShString(shdr.sh_name);
2166 is_ctor_dtor = mem.find(u8, name, ".ctors") != null or mem.find(u8, name, ".dtors") != null;
2167 },
2168 }
2169 if (!is_init_fini and !is_ctor_dtor) continue;
2170
2171 var entries = std.array_list.Managed(Entry).init(gpa);
2172 try entries.ensureTotalCapacityPrecise(atom_list.atoms.keys().len);
2173 defer entries.deinit();
2174
2175 for (atom_list.atoms.keys()) |ref| {
2176 const atom_ptr = self.atom(ref).?;
2177 const object = atom_ptr.file(self).?.object;
2178 const priority = blk: {
2179 if (is_ctor_dtor) {
2180 const basename = object.path.basename();
2181 if (mem.eql(u8, basename, "crtbegin.o")) break :blk std.math.minInt(i32);
2182 if (mem.eql(u8, basename, "crtend.o")) break :blk std.math.maxInt(i32);
2183 }
2184 const default: i32 = if (is_ctor_dtor) -1 else std.math.maxInt(i32);
2185 const name = atom_ptr.name(self);
2186 var it = mem.splitBackwardsScalar(u8, name, '.');
2187 const priority = std.fmt.parseUnsigned(u16, it.first(), 10) catch default;
2188 break :blk priority;
2189 };
2190 entries.appendAssumeCapacity(.{ .priority = priority, .atom_ref = ref });
2191 }
2192
2193 mem.sort(Entry, entries.items, self, Entry.lessThan);
2194
2195 atom_list.atoms.clearRetainingCapacity();
2196 for (entries.items) |entry| {
2197 _ = atom_list.atoms.getOrPutAssumeCapacity(entry.atom_ref);
2198 }
2199 }
2200}
2201
2202fn setDynamicSection(self: *Elf, rpaths: []const []const u8) !void {
2203 if (self.section_indexes.dynamic == null) return;
2204
2205 const shared_objects = self.shared_objects.values();
2206
2207 for (shared_objects) |index| {
2208 const shared_object = self.file(index).?.shared_object;
2209 if (!shared_object.alive) continue;
2210 try self.dynamic.addNeeded(shared_object, self);
2211 }
2212
2213 if (self.isEffectivelyDynLib()) {
2214 if (self.soname) |soname| {
2215 try self.dynamic.setSoname(soname, self);
2216 }
2217 }
2218
2219 try self.dynamic.setRpath(rpaths, self);
2220}
2221
2222fn sortDynamicSymtab(self: *Elf) void {
2223 if (self.section_indexes.gnu_hash == null) return;
2224 self.dynsym.sort(self);
2225}
2226
2227fn setVersionSymtab(self: *Elf) !void {
2228 const gpa = self.base.comp.gpa;
2229 if (self.section_indexes.versym == null) return;
2230 try self.versym.resize(gpa, self.dynsym.count());
2231 self.versym.items[0] = .LOCAL;
2232 for (self.dynsym.entries.items, 1..) |entry, i| {
2233 const sym = self.symbol(entry.ref).?;
2234 self.versym.items[i] = sym.version_index;
2235 }
2236
2237 if (self.section_indexes.verneed) |shndx| {
2238 try self.verneed.generate(self);
2239 const shdr = &self.sections.items(.shdr)[shndx];
2240 shdr.sh_info = @as(u32, @intCast(self.verneed.verneed.items.len));
2241 }
2242}
2243
2244fn setHashSections(self: *Elf) !void {
2245 if (self.section_indexes.hash != null) {
2246 try self.hash.generate(self);
2247 }
2248 if (self.section_indexes.gnu_hash != null) {
2249 try self.gnu_hash.calcSize(self);
2250 }
2251}
2252
2253fn phdrRank(phdr: elf.Elf64.Phdr) u8 {
2254 return switch (phdr.type) {
2255 .NULL => 0,
2256 .PHDR => 1,
2257 .INTERP => 2,
2258 .LOAD => 3,
2259 .DYNAMIC, .TLS => 4,
2260 .GNU_EH_FRAME => 5,
2261 .GNU_STACK => 6,
2262 else => 7,
2263 };
2264}
2265
2266fn sortPhdrs(
2267 gpa: Allocator,
2268 phdrs: *ProgramHeaderList,
2269 special_indexes: *ProgramHeaderIndexes,
2270 section_indexes: []OptionalProgramHeaderIndex,
2271) error{OutOfMemory}!void {
2272 const Entry = struct {
2273 phndx: u16,
2274
2275 pub fn lessThan(program_headers: []const elf.Elf64.Phdr, lhs: @This(), rhs: @This()) bool {
2276 const lhs_phdr = program_headers[lhs.phndx];
2277 const rhs_phdr = program_headers[rhs.phndx];
2278 const lhs_rank = phdrRank(lhs_phdr);
2279 const rhs_rank = phdrRank(rhs_phdr);
2280 if (lhs_rank == rhs_rank) return lhs_phdr.vaddr < rhs_phdr.vaddr;
2281 return lhs_rank < rhs_rank;
2282 }
2283 };
2284
2285 const entries = try gpa.alloc(Entry, phdrs.items.len);
2286 defer gpa.free(entries);
2287 for (entries, 0..) |*entry, phndx| {
2288 entry.* = .{ .phndx = @intCast(phndx) };
2289 }
2290
2291 // The `@as` here works around a bug in the C backend.
2292 mem.sort(Entry, entries, @as([]const elf.Elf64.Phdr, phdrs.items), Entry.lessThan);
2293
2294 const backlinks = try gpa.alloc(u16, entries.len);
2295 defer gpa.free(backlinks);
2296 const slice = try phdrs.toOwnedSlice(gpa);
2297 defer gpa.free(slice);
2298 try phdrs.resize(gpa, slice.len);
2299
2300 for (entries, phdrs.items, 0..) |entry, *phdr, i| {
2301 backlinks[entry.phndx] = @intCast(i);
2302 phdr.* = slice[entry.phndx];
2303 }
2304
2305 inline for (@typeInfo(ProgramHeaderIndexes).@"struct".field_names) |field_name| {
2306 if (@field(special_indexes, field_name).int()) |special_index| {
2307 @field(special_indexes, field_name) = @fromBackingInt(@intCast(backlinks[special_index]));
2308 }
2309 }
2310
2311 for (section_indexes) |*opt_phndx| {
2312 if (opt_phndx.int()) |index| {
2313 opt_phndx.* = @fromBackingInt(@intCast(backlinks[index]));
2314 }
2315 }
2316}
2317
2318fn shdrRank(shdr: elf.Elf64_Shdr, shstrtab: []const u8) u8 {
2319 const name = shString(shstrtab, shdr.sh_name);
2320 const flags = shdr.sh_flags;
2321
2322 switch (shdr.sh_type) {
2323 elf.SHT_NULL => return 0,
2324 elf.SHT_DYNSYM => return 2,
2325 elf.SHT_HASH => return 3,
2326 elf.SHT_GNU_HASH => return 3,
2327 elf.SHT_GNU_VERSYM => return 4,
2328 elf.SHT_GNU_VERDEF => return 4,
2329 elf.SHT_GNU_VERNEED => return 4,
2330
2331 elf.SHT_PREINIT_ARRAY,
2332 elf.SHT_INIT_ARRAY,
2333 elf.SHT_FINI_ARRAY,
2334 => return 0xf1,
2335
2336 elf.SHT_DYNAMIC => return 0xf2,
2337
2338 elf.SHT_RELA, elf.SHT_GROUP => return 0xf,
2339
2340 elf.SHT_PROGBITS => if (flags & elf.SHF_ALLOC != 0) {
2341 if (flags & elf.SHF_EXECINSTR != 0) {
2342 return 0xf0;
2343 } else if (flags & elf.SHF_WRITE != 0) {
2344 return if (flags & elf.SHF_TLS != 0) 0xf3 else 0xf5;
2345 } else if (mem.eql(u8, name, ".interp")) {
2346 return 1;
2347 } else if (mem.startsWith(u8, name, ".eh_frame")) {
2348 return 0xe1;
2349 } else {
2350 return 0xe0;
2351 }
2352 } else {
2353 if (mem.startsWith(u8, name, ".debug")) {
2354 return 0xf7;
2355 } else {
2356 return 0xf8;
2357 }
2358 },
2359 elf.SHT_X86_64_UNWIND => return 0xe1,
2360
2361 elf.SHT_NOBITS => return if (flags & elf.SHF_TLS != 0) 0xf4 else 0xf6,
2362 elf.SHT_SYMTAB => return 0xf9,
2363 elf.SHT_STRTAB => return if (mem.eql(u8, name, ".dynstr")) 0x4 else 0xfa,
2364 else => return 0xff,
2365 }
2366}
2367
2368pub fn sortShdrs(
2369 gpa: Allocator,
2370 section_indexes: *SectionIndexes,
2371 sections: *std.MultiArrayList(Section),
2372 shstrtab: []const u8,
2373 merge_sections: []Merge.Section,
2374 comdat_group_sections: []GroupSection,
2375 zig_object_ptr: ?*ZigObject,
2376 files: std.MultiArrayList(File.Entry),
2377) !void {
2378 const Entry = struct {
2379 shndx: u32,
2380
2381 const Context = struct {
2382 shdrs: []const elf.Elf64_Shdr,
2383 shstrtab: []const u8,
2384 };
2385
2386 pub fn lessThan(ctx: Context, lhs: @This(), rhs: @This()) bool {
2387 const lhs_rank = shdrRank(ctx.shdrs[lhs.shndx], ctx.shstrtab);
2388 const rhs_rank = shdrRank(ctx.shdrs[rhs.shndx], ctx.shstrtab);
2389 if (lhs_rank == rhs_rank) {
2390 const lhs_name = shString(ctx.shstrtab, ctx.shdrs[lhs.shndx].sh_name);
2391 const rhs_name = shString(ctx.shstrtab, ctx.shdrs[rhs.shndx].sh_name);
2392 return std.mem.lessThan(u8, lhs_name, rhs_name);
2393 }
2394 return lhs_rank < rhs_rank;
2395 }
2396 };
2397
2398 const shdrs = sections.items(.shdr);
2399
2400 const entries = try gpa.alloc(Entry, shdrs.len);
2401 defer gpa.free(entries);
2402 for (entries, 0..shdrs.len) |*entry, shndx| {
2403 entry.* = .{ .shndx = @intCast(shndx) };
2404 }
2405
2406 const sort_context: Entry.Context = .{
2407 .shdrs = shdrs,
2408 .shstrtab = shstrtab,
2409 };
2410 mem.sortUnstable(Entry, entries, sort_context, Entry.lessThan);
2411
2412 const backlinks = try gpa.alloc(u32, entries.len);
2413 defer gpa.free(backlinks);
2414 {
2415 var slice = sections.toOwnedSlice();
2416 defer slice.deinit(gpa);
2417 try sections.resize(gpa, slice.len);
2418
2419 for (entries, 0..) |entry, i| {
2420 backlinks[entry.shndx] = @intCast(i);
2421 sections.set(i, slice.get(entry.shndx));
2422 }
2423 }
2424
2425 inline for (@typeInfo(SectionIndexes).@"struct".field_names) |field_name| {
2426 if (@field(section_indexes, field_name)) |special_index| {
2427 @field(section_indexes, field_name) = backlinks[special_index];
2428 }
2429 }
2430
2431 for (merge_sections) |*msec| {
2432 msec.output_section_index = backlinks[msec.output_section_index];
2433 }
2434
2435 const slice = sections.slice();
2436 for (slice.items(.shdr), slice.items(.atom_list_2)) |*shdr, *atom_list| {
2437 atom_list.output_section_index = backlinks[atom_list.output_section_index];
2438 for (atom_list.atoms.keys()) |ref| {
2439 fileLookup(files, ref.file, zig_object_ptr).?.atom(ref.index).?.output_section_index = atom_list.output_section_index;
2440 }
2441 if (shdr.sh_type == elf.SHT_RELA) {
2442 shdr.sh_link = section_indexes.symtab.?;
2443 shdr.sh_info = backlinks[shdr.sh_info];
2444 }
2445 }
2446
2447 if (zig_object_ptr) |zo| zo.resetShdrIndexes(backlinks);
2448
2449 for (comdat_group_sections) |*cg| {
2450 cg.shndx = backlinks[cg.shndx];
2451 }
2452
2453 if (section_indexes.symtab) |index| {
2454 const shdr = &slice.items(.shdr)[index];
2455 shdr.sh_link = section_indexes.strtab.?;
2456 }
2457
2458 if (section_indexes.dynamic) |index| {
2459 const shdr = &slice.items(.shdr)[index];
2460 shdr.sh_link = section_indexes.dynstrtab.?;
2461 }
2462
2463 if (section_indexes.dynsymtab) |index| {
2464 const shdr = &slice.items(.shdr)[index];
2465 shdr.sh_link = section_indexes.dynstrtab.?;
2466 }
2467
2468 if (section_indexes.hash) |index| {
2469 const shdr = &slice.items(.shdr)[index];
2470 shdr.sh_link = section_indexes.dynsymtab.?;
2471 }
2472
2473 if (section_indexes.gnu_hash) |index| {
2474 const shdr = &slice.items(.shdr)[index];
2475 shdr.sh_link = section_indexes.dynsymtab.?;
2476 }
2477
2478 if (section_indexes.versym) |index| {
2479 const shdr = &slice.items(.shdr)[index];
2480 shdr.sh_link = section_indexes.dynsymtab.?;
2481 }
2482
2483 if (section_indexes.verneed) |index| {
2484 const shdr = &slice.items(.shdr)[index];
2485 shdr.sh_link = section_indexes.dynstrtab.?;
2486 }
2487
2488 if (section_indexes.rela_dyn) |index| {
2489 const shdr = &slice.items(.shdr)[index];
2490 shdr.sh_link = section_indexes.dynsymtab orelse 0;
2491 }
2492
2493 if (section_indexes.rela_plt) |index| {
2494 const shdr = &slice.items(.shdr)[index];
2495 shdr.sh_link = section_indexes.dynsymtab.?;
2496 shdr.sh_info = section_indexes.plt.?;
2497 }
2498
2499 if (section_indexes.eh_frame_rela) |index| {
2500 const shdr = &slice.items(.shdr)[index];
2501 shdr.sh_link = section_indexes.symtab.?;
2502 shdr.sh_info = section_indexes.eh_frame.?;
2503 }
2504}
2505
2506fn updateSectionSizes(self: *Elf) !void {
2507 const slice = self.sections.slice();
2508 for (slice.items(.shdr), slice.items(.atom_list_2)) |shdr, *atom_list| {
2509 if (atom_list.atoms.keys().len == 0) continue;
2510 if (!atom_list.dirty) continue;
2511 if (self.requiresThunks() and shdr.sh_flags & elf.SHF_EXECINSTR != 0) continue;
2512 atom_list.updateSize(self);
2513 try atom_list.allocate(self);
2514 atom_list.dirty = false;
2515 }
2516
2517 if (self.requiresThunks()) {
2518 for (slice.items(.shdr), slice.items(.atom_list_2)) |shdr, *atom_list| {
2519 if (shdr.sh_flags & elf.SHF_EXECINSTR == 0) continue;
2520 if (atom_list.atoms.keys().len == 0) continue;
2521 if (!atom_list.dirty) continue;
2522
2523 // Create jump/branch range extenders if needed.
2524 try self.createThunks(atom_list);
2525 try atom_list.allocate(self);
2526 atom_list.dirty = false;
2527 }
2528
2529 // This might not be needed if there was a link from Atom/Thunk to AtomList.
2530 for (self.thunks.items) |*th| {
2531 th.value += slice.items(.atom_list_2)[th.output_section_index].value;
2532 }
2533 }
2534
2535 const shdrs = slice.items(.shdr);
2536 if (self.section_indexes.eh_frame) |index| {
2537 shdrs[index].sh_size = try eh_frame.calcEhFrameSize(self);
2538 }
2539
2540 if (self.section_indexes.eh_frame_hdr) |index| {
2541 shdrs[index].sh_size = eh_frame.calcEhFrameHdrSize(self);
2542 }
2543
2544 if (self.section_indexes.got) |index| {
2545 shdrs[index].sh_size = self.got.size(self);
2546 }
2547
2548 if (self.section_indexes.plt) |index| {
2549 shdrs[index].sh_size = self.plt.size(self);
2550 }
2551
2552 if (self.section_indexes.got_plt) |index| {
2553 shdrs[index].sh_size = self.got_plt.size(self);
2554 }
2555
2556 if (self.section_indexes.plt_got) |index| {
2557 shdrs[index].sh_size = self.plt_got.size(self);
2558 }
2559
2560 if (self.section_indexes.rela_dyn) |shndx| {
2561 var num = self.got.numRela(self) + self.copy_rel.numRela();
2562 if (self.zigObjectPtr()) |zig_object| {
2563 num += zig_object.num_dynrelocs;
2564 }
2565 for (self.objects.items) |index| {
2566 num += self.file(index).?.object.num_dynrelocs;
2567 }
2568 shdrs[shndx].sh_size = num * @sizeOf(elf.Elf64_Rela);
2569 }
2570
2571 if (self.section_indexes.rela_plt) |index| {
2572 shdrs[index].sh_size = self.plt.numRela() * @sizeOf(elf.Elf64_Rela);
2573 }
2574
2575 if (self.section_indexes.copy_rel) |index| {
2576 try self.copy_rel.updateSectionSize(index, self);
2577 }
2578
2579 if (self.section_indexes.interp) |index| {
2580 shdrs[index].sh_size = self.getTarget().dynamic_linker.get().?.len + 1;
2581 }
2582
2583 if (self.section_indexes.hash) |index| {
2584 shdrs[index].sh_size = self.hash.size();
2585 }
2586
2587 if (self.section_indexes.gnu_hash) |index| {
2588 shdrs[index].sh_size = self.gnu_hash.size();
2589 }
2590
2591 if (self.section_indexes.dynamic) |index| {
2592 shdrs[index].sh_size = self.dynamic.size(self);
2593 }
2594
2595 if (self.section_indexes.dynsymtab) |index| {
2596 shdrs[index].sh_size = self.dynsym.size();
2597 }
2598
2599 if (self.section_indexes.dynstrtab) |index| {
2600 shdrs[index].sh_size = self.dynstrtab.items.len;
2601 }
2602
2603 if (self.section_indexes.versym) |index| {
2604 shdrs[index].sh_size = self.versym.items.len * @sizeOf(elf.Versym);
2605 }
2606
2607 if (self.section_indexes.verneed) |index| {
2608 shdrs[index].sh_size = self.verneed.size();
2609 }
2610
2611 try self.updateSymtabSize();
2612 self.updateShStrtabSize();
2613}
2614
2615pub fn updateShStrtabSize(self: *Elf) void {
2616 if (self.section_indexes.shstrtab) |index| {
2617 self.sections.items(.shdr)[index].sh_size = self.shstrtab.items.len;
2618 }
2619}
2620
2621fn shdrToPhdrFlags(sh_flags: u64) u32 {
2622 const write = sh_flags & elf.SHF_WRITE != 0;
2623 const exec = sh_flags & elf.SHF_EXECINSTR != 0;
2624 var out_flags: u32 = elf.PF_R;
2625 if (write) out_flags |= elf.PF_W;
2626 if (exec) out_flags |= elf.PF_X;
2627 return out_flags;
2628}
2629
2630/// Returns maximum number of program headers that may be emitted by the linker.
2631/// (This is an upper bound so that we can reserve enough space for the header and progam header
2632/// table without running out of space and being forced to move things around.)
2633fn getMaxNumberOfPhdrs() u64 {
2634 // The estimated maximum number of segments the linker can emit for input sections are:
2635 var num: u64 = max_number_of_object_segments;
2636 // Any other non-loadable program headers, including TLS, DYNAMIC, GNU_STACK, GNU_EH_FRAME, INTERP:
2637 num += max_number_of_special_phdrs;
2638 // PHDR program header and corresponding read-only load segment:
2639 num += 2;
2640 return num;
2641}
2642
2643fn addLoadPhdrs(self: *Elf) error{OutOfMemory}!void {
2644 for (self.sections.items(.shdr)) |shdr| {
2645 if (shdr.sh_type == elf.SHT_NULL) continue;
2646 if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue;
2647 const flags = shdrToPhdrFlags(shdr.sh_flags);
2648 if (self.getPhdr(.{ .flags = flags, .type = @backingInt(elf.PT.LOAD) }) == .none) {
2649 _ = try self.addPhdr(.{ .flags = flags, .type = @backingInt(elf.PT.LOAD) });
2650 }
2651 }
2652}
2653
2654/// Allocates PHDR table in virtual memory and in file.
2655fn allocatePhdrTable(self: *Elf) error{OutOfMemory}!void {
2656 const diags = &self.base.comp.link_diags;
2657 const phdr_table = &self.phdrs.items[self.phdr_indexes.table.int().?];
2658 const phdr_table_load = &self.phdrs.items[self.phdr_indexes.table_load.int().?];
2659
2660 const ehsize: u64 = switch (self.ptr_width) {
2661 .p32 => @sizeOf(elf.Elf32_Ehdr),
2662 .p64 => @sizeOf(elf.Elf64_Ehdr),
2663 };
2664 const phsize: u64 = switch (self.ptr_width) {
2665 .p32 => @sizeOf(elf.Elf32.Phdr),
2666 .p64 => @sizeOf(elf.Elf64.Phdr),
2667 };
2668 const needed_size = self.phdrs.items.len * phsize;
2669 const available_space = self.allocatedSize(phdr_table.offset);
2670
2671 if (needed_size > available_space) {
2672 // In this case, we have two options:
2673 // 1. increase the available padding for EHDR + PHDR table so that we don't overflow it
2674 // (revisit getMaxNumberOfPhdrs())
2675 // 2. shift everything in file to free more space for EHDR + PHDR table
2676 // TODO verify `getMaxNumberOfPhdrs()` is accurate and convert this into no-op
2677 var err = try diags.addErrorWithNotes(1);
2678 try err.addMsg("fatal linker error: not enough space reserved for EHDR and PHDR table", .{});
2679 err.addNote("required 0x{x}, available 0x{x}", .{ needed_size, available_space });
2680 }
2681
2682 phdr_table_load.filesz = needed_size + ehsize;
2683 phdr_table_load.memsz = needed_size + ehsize;
2684 phdr_table.filesz = needed_size;
2685 phdr_table.memsz = needed_size;
2686}
2687
2688/// Allocates alloc sections and creates load segments for sections
2689/// extracted from input object files.
2690pub fn allocateAllocSections(self: *Elf) !void {
2691 // We use this struct to track maximum alignment of all TLS sections.
2692 // According to https://github.com/rui314/mold/commit/bd46edf3f0fe9e1a787ea453c4657d535622e61f in mold,
2693 // in-file offsets have to be aligned against the start of TLS program header.
2694 // If that's not ensured, then in a multi-threaded context, TLS variables across a shared object
2695 // boundary may not get correctly loaded at an aligned address.
2696 const Align = struct {
2697 tls_start_align: u64 = 1,
2698 first_tls_index: ?usize = null,
2699
2700 fn isFirstTlsShdr(this: @This(), other: usize) bool {
2701 if (this.first_tls_index) |index| return index == other;
2702 return false;
2703 }
2704
2705 fn @"align"(this: @This(), index: usize, sh_addralign: u64, addr: u64) u64 {
2706 const alignment = if (this.isFirstTlsShdr(index)) this.tls_start_align else sh_addralign;
2707 return mem.alignForward(u64, addr, alignment);
2708 }
2709 };
2710
2711 const slice = self.sections.slice();
2712 var alignment = Align{};
2713 for (slice.items(.shdr), 0..) |shdr, i| {
2714 if (shdr.sh_type == elf.SHT_NULL) continue;
2715 if (shdr.sh_flags & elf.SHF_TLS == 0) continue;
2716 if (alignment.first_tls_index == null) alignment.first_tls_index = i;
2717 alignment.tls_start_align = @max(alignment.tls_start_align, shdr.sh_addralign);
2718 }
2719
2720 // Next, calculate segment covers by scanning all alloc sections.
2721 // If a section matches segment flags with the preceeding section,
2722 // we put it in the same segment. Otherwise, we create a new cover.
2723 // This algorithm is simple but suboptimal in terms of space re-use:
2724 // normally we would also take into account any gaps in allocated
2725 // virtual and file offsets. However, the simple one will do for one
2726 // as we are more interested in quick turnaround and compatibility
2727 // with `findFreeSpace` mechanics than anything else.
2728 const Cover = std.array_list.Managed(u32);
2729 const gpa = self.base.comp.gpa;
2730 var covers: [max_number_of_object_segments]Cover = undefined;
2731 for (&covers) |*cover| {
2732 cover.* = Cover.init(gpa);
2733 }
2734 defer for (&covers) |*cover| {
2735 cover.deinit();
2736 };
2737
2738 for (slice.items(.shdr), 0..) |shdr, shndx| {
2739 if (shdr.sh_type == elf.SHT_NULL) continue;
2740 if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue;
2741 const flags = shdrToPhdrFlags(shdr.sh_flags);
2742 try covers[flags - 1].append(@intCast(shndx));
2743 }
2744
2745 // Now we can proceed with allocating the sections in virtual memory.
2746 // As the base address we take the end address of the PHDR table.
2747 // When allocating we first find the largest required alignment
2748 // of any section that is contained in a cover and use it to align
2749 // the start address of the segement (and first section).
2750 const phdr_table = &self.phdrs.items[self.phdr_indexes.table_load.int().?];
2751 var addr = phdr_table.vaddr + phdr_table.memsz;
2752
2753 for (covers) |cover| {
2754 if (cover.items.len == 0) continue;
2755
2756 var @"align": u64 = self.page_size;
2757 for (cover.items) |shndx| {
2758 const shdr = slice.items(.shdr)[shndx];
2759 if (shdr.sh_type == elf.SHT_NOBITS and shdr.sh_flags & elf.SHF_TLS != 0) continue;
2760 @"align" = @max(@"align", shdr.sh_addralign);
2761 }
2762
2763 addr = mem.alignForward(u64, addr, @"align");
2764
2765 var memsz: u64 = 0;
2766 var filesz: u64 = 0;
2767 var i: usize = 0;
2768 while (i < cover.items.len) : (i += 1) {
2769 const shndx = cover.items[i];
2770 const shdr = &slice.items(.shdr)[shndx];
2771 if (shdr.sh_type == elf.SHT_NOBITS and shdr.sh_flags & elf.SHF_TLS != 0) {
2772 // .tbss is a little special as it's used only by the loader meaning it doesn't
2773 // need to be actually mmap'ed at runtime. We still need to correctly increment
2774 // the addresses of every TLS zerofill section tho. Thus, we hack it so that
2775 // we increment the start address like normal, however, after we are done,
2776 // the next ALLOC section will get its start address allocated within the same
2777 // range as the .tbss sections. We will get something like this:
2778 //
2779 // ...
2780 // .tbss 0x10
2781 // .tcommon 0x20
2782 // .data 0x10
2783 // ...
2784 var tbss_addr = addr;
2785 while (i < cover.items.len and
2786 slice.items(.shdr)[cover.items[i]].sh_type == elf.SHT_NOBITS and
2787 slice.items(.shdr)[cover.items[i]].sh_flags & elf.SHF_TLS != 0) : (i += 1)
2788 {
2789 const tbss_shndx = cover.items[i];
2790 const tbss_shdr = &slice.items(.shdr)[tbss_shndx];
2791 tbss_addr = alignment.@"align"(tbss_shndx, tbss_shdr.sh_addralign, tbss_addr);
2792 tbss_shdr.sh_addr = tbss_addr;
2793 tbss_addr += tbss_shdr.sh_size;
2794 }
2795 i -= 1;
2796 continue;
2797 }
2798 const next = alignment.@"align"(shndx, shdr.sh_addralign, addr);
2799 const padding = next - addr;
2800 addr = next;
2801 shdr.sh_addr = addr;
2802 if (shdr.sh_type != elf.SHT_NOBITS) {
2803 filesz += padding + shdr.sh_size;
2804 }
2805 memsz += padding + shdr.sh_size;
2806 addr += shdr.sh_size;
2807 }
2808
2809 const first = slice.items(.shdr)[cover.items[0]];
2810 const phndx = self.getPhdr(.{ .type = @backingInt(elf.PT.LOAD), .flags = shdrToPhdrFlags(first.sh_flags) }).unwrap().?;
2811 const phdr = &self.phdrs.items[phndx.int()];
2812 const allocated_size = self.allocatedSize(phdr.offset);
2813 if (filesz > allocated_size) {
2814 const old_offset = phdr.offset;
2815 phdr.offset = 0;
2816 var new_offset = try self.findFreeSpace(filesz, @"align");
2817 phdr.offset = new_offset;
2818
2819 log.debug("moving phdr({d}) from 0x{x} to 0x{x}", .{ phndx, old_offset, new_offset });
2820
2821 for (cover.items) |shndx| {
2822 const shdr = &slice.items(.shdr)[shndx];
2823 slice.items(.phndx)[shndx] = phndx.toOptional();
2824 if (shdr.sh_type == elf.SHT_NOBITS) {
2825 shdr.sh_offset = 0;
2826 continue;
2827 }
2828 new_offset = alignment.@"align"(shndx, shdr.sh_addralign, new_offset);
2829
2830 log.debug("moving {s} from 0x{x} to 0x{x}", .{
2831 self.getShString(shdr.sh_name),
2832 shdr.sh_offset,
2833 new_offset,
2834 });
2835
2836 if (shdr.sh_offset > 0) {
2837 // Get size actually commited to the output file.
2838 const existing_size = self.sectionSize(shndx);
2839 try self.base.copyRangeAll(shdr.sh_offset, new_offset, existing_size);
2840 }
2841
2842 shdr.sh_offset = new_offset;
2843 new_offset += shdr.sh_size;
2844 }
2845 }
2846
2847 phdr.vaddr = first.sh_addr;
2848 phdr.paddr = first.sh_addr;
2849 phdr.memsz = memsz;
2850 phdr.filesz = filesz;
2851 phdr.@"align" = @"align";
2852
2853 addr = mem.alignForward(u64, addr, self.page_size);
2854 }
2855}
2856
2857/// Allocates non-alloc sections (debug info, symtabs, etc.).
2858pub fn allocateNonAllocSections(self: *Elf) !void {
2859 for (self.sections.items(.shdr), 0..) |*shdr, shndx| {
2860 if (shdr.sh_type == elf.SHT_NULL) continue;
2861 if (shdr.sh_flags & elf.SHF_ALLOC != 0) continue;
2862 const needed_size = shdr.sh_size;
2863 if (needed_size > self.allocatedSize(shdr.sh_offset)) {
2864 shdr.sh_size = 0;
2865 const new_offset = try self.findFreeSpace(needed_size, shdr.sh_addralign);
2866
2867 log.debug("moving {s} from 0x{x} to 0x{x}", .{
2868 self.getShString(shdr.sh_name),
2869 shdr.sh_offset,
2870 new_offset,
2871 });
2872
2873 if (shdr.sh_offset > 0) {
2874 const existing_size = self.sectionSize(@intCast(shndx));
2875 try self.base.copyRangeAll(shdr.sh_offset, new_offset, existing_size);
2876 }
2877
2878 shdr.sh_offset = new_offset;
2879 shdr.sh_size = needed_size;
2880 }
2881 }
2882}
2883
2884fn allocateSpecialPhdrs(self: *Elf) void {
2885 const slice = self.sections.slice();
2886
2887 for (&[_]struct { OptionalProgramHeaderIndex, ?u32 }{
2888 .{ self.phdr_indexes.interp, self.section_indexes.interp },
2889 .{ self.phdr_indexes.dynamic, self.section_indexes.dynamic },
2890 .{ self.phdr_indexes.gnu_eh_frame, self.section_indexes.eh_frame_hdr },
2891 }) |pair| {
2892 if (pair[0].int()) |index| {
2893 const shdr = slice.items(.shdr)[pair[1].?];
2894 const phdr = &self.phdrs.items[index];
2895 phdr.@"align" = shdr.sh_addralign;
2896 phdr.offset = shdr.sh_offset;
2897 phdr.vaddr = shdr.sh_addr;
2898 phdr.paddr = shdr.sh_addr;
2899 phdr.filesz = shdr.sh_size;
2900 phdr.memsz = shdr.sh_size;
2901 }
2902 }
2903
2904 // Set the TLS segment boundaries.
2905 // We assume TLS sections are laid out contiguously and that there is
2906 // a single TLS segment.
2907 if (self.phdr_indexes.tls.int()) |index| {
2908 const shdrs = slice.items(.shdr);
2909 const phdr = &self.phdrs.items[index];
2910 var shndx: u32 = 0;
2911 while (shndx < shdrs.len) {
2912 const shdr = shdrs[shndx];
2913 if (shdr.sh_flags & elf.SHF_TLS == 0) {
2914 shndx += 1;
2915 continue;
2916 }
2917 phdr.offset = shdr.sh_offset;
2918 phdr.vaddr = shdr.sh_addr;
2919 phdr.paddr = shdr.sh_addr;
2920 phdr.@"align" = shdr.sh_addralign;
2921 shndx += 1;
2922 phdr.@"align" = @max(phdr.@"align", shdr.sh_addralign);
2923 if (shdr.sh_type != elf.SHT_NOBITS) {
2924 phdr.filesz = shdr.sh_offset + shdr.sh_size - phdr.offset;
2925 }
2926 phdr.memsz = shdr.sh_addr + shdr.sh_size - phdr.vaddr;
2927
2928 while (shndx < shdrs.len) : (shndx += 1) {
2929 const next = shdrs[shndx];
2930 if (next.sh_flags & elf.SHF_TLS == 0) break;
2931 phdr.@"align" = @max(phdr.@"align", next.sh_addralign);
2932 if (next.sh_type != elf.SHT_NOBITS) {
2933 phdr.filesz = next.sh_offset + next.sh_size - phdr.offset;
2934 }
2935 phdr.memsz = next.sh_addr + next.sh_size - phdr.vaddr;
2936 }
2937 }
2938 }
2939}
2940
2941fn writeAtoms(self: *Elf) !void {
2942 const gpa = self.base.comp.gpa;
2943
2944 var undefs: std.array_hash_map.Auto(SymbolResolver.Index, std.array_list.Managed(Ref)) = .empty;
2945 defer {
2946 for (undefs.values()) |*refs| refs.deinit();
2947 undefs.deinit(gpa);
2948 }
2949
2950 var buffer: std.Io.Writer.Allocating = .init(gpa);
2951 defer buffer.deinit();
2952
2953 const slice = self.sections.slice();
2954 var has_reloc_errors = false;
2955 for (slice.items(.shdr), slice.items(.atom_list_2)) |shdr, atom_list| {
2956 if (shdr.sh_type == elf.SHT_NOBITS) continue;
2957 if (atom_list.atoms.keys().len == 0) continue;
2958 atom_list.write(&buffer, &undefs, self) catch |err| switch (err) {
2959 error.UnsupportedCpuArch => {
2960 try self.reportUnsupportedCpuArch();
2961 return error.AlreadyReported;
2962 },
2963 error.RelocFailure, error.RelaxFailure => has_reloc_errors = true,
2964 else => |e| return e,
2965 };
2966 }
2967
2968 try self.reportUndefinedSymbols(&undefs);
2969 if (has_reloc_errors) return error.AlreadyReported;
2970
2971 if (self.requiresThunks()) {
2972 for (self.thunks.items) |th| {
2973 const thunk_size = th.size(self);
2974 try buffer.ensureUnusedCapacity(thunk_size);
2975 const shdr = slice.items(.shdr)[th.output_section_index];
2976 const offset = @as(u64, @intCast(th.value)) + shdr.sh_offset;
2977 try th.write(self, &buffer.writer);
2978 assert(buffer.written().len == thunk_size);
2979 try self.pwriteAll(buffer.written(), offset);
2980 buffer.clearRetainingCapacity();
2981 }
2982 }
2983}
2984
2985pub fn updateSymtabSize(self: *Elf) !void {
2986 var nlocals: u32 = 0;
2987 var nglobals: u32 = 0;
2988 var strsize: u32 = 0;
2989
2990 const gpa = self.base.comp.gpa;
2991 const shared_objects = self.shared_objects.values();
2992
2993 var files = std.array_list.Managed(File.Index).init(gpa);
2994 defer files.deinit();
2995 try files.ensureTotalCapacityPrecise(self.objects.items.len + shared_objects.len + 2);
2996
2997 if (self.zig_object_index) |index| files.appendAssumeCapacity(index);
2998 for (self.objects.items) |index| files.appendAssumeCapacity(index);
2999 for (shared_objects) |index| files.appendAssumeCapacity(index);
3000 if (self.linker_defined_index) |index| files.appendAssumeCapacity(index);
3001
3002 // Section symbols
3003 nlocals += @intCast(self.sections.slice().len);
3004
3005 if (self.requiresThunks()) for (self.thunks.items) |*th| {
3006 th.output_symtab_ctx.reset();
3007 th.output_symtab_ctx.ilocal = nlocals;
3008 th.calcSymtabSize(self);
3009 nlocals += th.output_symtab_ctx.nlocals;
3010 strsize += th.output_symtab_ctx.strsize;
3011 };
3012
3013 for (files.items) |index| {
3014 const file_ptr = self.file(index).?;
3015 const ctx = switch (file_ptr) {
3016 inline else => |x| &x.output_symtab_ctx,
3017 };
3018 ctx.reset();
3019 ctx.ilocal = nlocals;
3020 ctx.iglobal = nglobals;
3021 try file_ptr.updateSymtabSize(self);
3022 nlocals += ctx.nlocals;
3023 nglobals += ctx.nglobals;
3024 strsize += ctx.strsize;
3025 }
3026
3027 if (self.section_indexes.got) |_| {
3028 self.got.output_symtab_ctx.reset();
3029 self.got.output_symtab_ctx.ilocal = nlocals;
3030 self.got.updateSymtabSize(self);
3031 nlocals += self.got.output_symtab_ctx.nlocals;
3032 strsize += self.got.output_symtab_ctx.strsize;
3033 }
3034
3035 if (self.section_indexes.plt) |_| {
3036 self.plt.output_symtab_ctx.reset();
3037 self.plt.output_symtab_ctx.ilocal = nlocals;
3038 self.plt.updateSymtabSize(self);
3039 nlocals += self.plt.output_symtab_ctx.nlocals;
3040 strsize += self.plt.output_symtab_ctx.strsize;
3041 }
3042
3043 if (self.section_indexes.plt_got) |_| {
3044 self.plt_got.output_symtab_ctx.reset();
3045 self.plt_got.output_symtab_ctx.ilocal = nlocals;
3046 self.plt_got.updateSymtabSize(self);
3047 nlocals += self.plt_got.output_symtab_ctx.nlocals;
3048 strsize += self.plt_got.output_symtab_ctx.strsize;
3049 }
3050
3051 for (files.items) |index| {
3052 const file_ptr = self.file(index).?;
3053 const ctx = switch (file_ptr) {
3054 inline else => |x| &x.output_symtab_ctx,
3055 };
3056 ctx.iglobal += nlocals;
3057 }
3058
3059 const slice = self.sections.slice();
3060 const symtab_shdr = &slice.items(.shdr)[self.section_indexes.symtab.?];
3061 symtab_shdr.sh_info = nlocals;
3062 symtab_shdr.sh_link = self.section_indexes.strtab.?;
3063
3064 const sym_size: u64 = switch (self.ptr_width) {
3065 .p32 => @sizeOf(elf.Elf32_Sym),
3066 .p64 => @sizeOf(elf.Elf64_Sym),
3067 };
3068 const needed_size = (nlocals + nglobals) * sym_size;
3069 symtab_shdr.sh_size = needed_size;
3070
3071 const strtab = &slice.items(.shdr)[self.section_indexes.strtab.?];
3072 strtab.sh_size = strsize + 1;
3073}
3074
3075fn writeSyntheticSections(self: *Elf) !void {
3076 const gpa = self.base.comp.gpa;
3077 const slice = self.sections.slice();
3078
3079 if (self.section_indexes.interp) |shndx| {
3080 var buffer: [256]u8 = undefined;
3081 const interp = self.getTarget().dynamic_linker.get().?;
3082 @memcpy(buffer[0..interp.len], interp);
3083 buffer[interp.len] = 0;
3084 const contents = buffer[0 .. interp.len + 1];
3085 const shdr = slice.items(.shdr)[shndx];
3086 assert(shdr.sh_size == contents.len);
3087 try self.pwriteAll(contents, shdr.sh_offset);
3088 }
3089
3090 if (self.section_indexes.hash) |shndx| {
3091 const shdr = slice.items(.shdr)[shndx];
3092 try self.pwriteAll(self.hash.buffer.items, shdr.sh_offset);
3093 }
3094
3095 if (self.section_indexes.gnu_hash) |shndx| {
3096 const shdr = slice.items(.shdr)[shndx];
3097 var aw: std.Io.Writer.Allocating = .init(gpa);
3098 try aw.ensureUnusedCapacity(self.gnu_hash.size());
3099 defer aw.deinit();
3100 try self.gnu_hash.write(self, &aw.writer);
3101 try self.pwriteAll(aw.written(), shdr.sh_offset);
3102 }
3103
3104 if (self.section_indexes.versym) |shndx| {
3105 const shdr = slice.items(.shdr)[shndx];
3106 try self.pwriteAll(@ptrCast(self.versym.items), shdr.sh_offset);
3107 }
3108
3109 if (self.section_indexes.verneed) |shndx| {
3110 const shdr = slice.items(.shdr)[shndx];
3111 var buffer = try std.Io.Writer.Allocating.initCapacity(gpa, self.verneed.size());
3112 defer buffer.deinit();
3113 try self.verneed.write(&buffer.writer);
3114 try self.pwriteAll(buffer.written(), shdr.sh_offset);
3115 }
3116
3117 if (self.section_indexes.dynamic) |shndx| {
3118 const shdr = slice.items(.shdr)[shndx];
3119 var buffer = try std.Io.Writer.Allocating.initCapacity(gpa, self.dynamic.size(self));
3120 defer buffer.deinit();
3121 try self.dynamic.write(self, &buffer.writer);
3122 try self.pwriteAll(buffer.written(), shdr.sh_offset);
3123 }
3124
3125 if (self.section_indexes.dynsymtab) |shndx| {
3126 const shdr = slice.items(.shdr)[shndx];
3127 var buffer = try std.Io.Writer.Allocating.initCapacity(gpa, self.dynsym.size());
3128 defer buffer.deinit();
3129 try self.dynsym.write(self, &buffer.writer);
3130 try self.pwriteAll(buffer.written(), shdr.sh_offset);
3131 }
3132
3133 if (self.section_indexes.dynstrtab) |shndx| {
3134 const shdr = slice.items(.shdr)[shndx];
3135 try self.pwriteAll(self.dynstrtab.items, shdr.sh_offset);
3136 }
3137
3138 if (self.section_indexes.eh_frame) |shndx| {
3139 const existing_size = existing_size: {
3140 const zo = self.zigObjectPtr() orelse break :existing_size 0;
3141 const sym = zo.symbol(zo.eh_frame_index orelse break :existing_size 0);
3142 break :existing_size sym.atom(self).?.size;
3143 };
3144 const shdr = slice.items(.shdr)[shndx];
3145 const sh_size = try self.cast(usize, shdr.sh_size);
3146 var buffer = try std.Io.Writer.Allocating.initCapacity(gpa, @intCast(sh_size - existing_size));
3147 defer buffer.deinit();
3148 try eh_frame.writeEhFrame(self, &buffer.writer);
3149 assert(buffer.written().len == sh_size - existing_size);
3150 try self.pwriteAll(buffer.written(), shdr.sh_offset + existing_size);
3151 }
3152
3153 if (self.section_indexes.eh_frame_hdr) |shndx| {
3154 const shdr = slice.items(.shdr)[shndx];
3155 const sh_size = try self.cast(usize, shdr.sh_size);
3156 var buffer = try std.Io.Writer.Allocating.initCapacity(gpa, sh_size);
3157 defer buffer.deinit();
3158 try eh_frame.writeEhFrameHdr(self, &buffer.writer);
3159 try self.pwriteAll(buffer.written(), shdr.sh_offset);
3160 }
3161
3162 if (self.section_indexes.got) |index| {
3163 const shdr = slice.items(.shdr)[index];
3164 var buffer = try std.Io.Writer.Allocating.initCapacity(gpa, self.got.size(self));
3165 defer buffer.deinit();
3166 try self.got.write(self, &buffer.writer);
3167 try self.pwriteAll(buffer.written(), shdr.sh_offset);
3168 }
3169
3170 if (self.section_indexes.rela_dyn) |shndx| {
3171 const shdr = slice.items(.shdr)[shndx];
3172 try self.got.addRela(self);
3173 try self.copy_rel.addRela(self);
3174 self.sortRelaDyn();
3175 try self.pwriteAll(@ptrCast(self.rela_dyn.items), shdr.sh_offset);
3176 }
3177
3178 if (self.section_indexes.plt) |shndx| {
3179 const shdr = slice.items(.shdr)[shndx];
3180 var buffer = try std.Io.Writer.Allocating.initCapacity(gpa, self.plt.size(self));
3181 defer buffer.deinit();
3182 try self.plt.write(self, &buffer.writer);
3183 try self.pwriteAll(buffer.written(), shdr.sh_offset);
3184 }
3185
3186 if (self.section_indexes.got_plt) |shndx| {
3187 const shdr = slice.items(.shdr)[shndx];
3188 var buffer = try std.Io.Writer.Allocating.initCapacity(gpa, self.got_plt.size(self));
3189 defer buffer.deinit();
3190 try self.got_plt.write(self, &buffer.writer);
3191 try self.pwriteAll(buffer.written(), shdr.sh_offset);
3192 }
3193
3194 if (self.section_indexes.plt_got) |shndx| {
3195 const shdr = slice.items(.shdr)[shndx];
3196 var buffer = try std.Io.Writer.Allocating.initCapacity(gpa, self.plt_got.size(self));
3197 defer buffer.deinit();
3198 try self.plt_got.write(self, &buffer.writer);
3199 try self.pwriteAll(buffer.written(), shdr.sh_offset);
3200 }
3201
3202 if (self.section_indexes.rela_plt) |shndx| {
3203 const shdr = slice.items(.shdr)[shndx];
3204 try self.plt.addRela(self);
3205 try self.pwriteAll(@ptrCast(self.rela_plt.items), shdr.sh_offset);
3206 }
3207
3208 try self.writeSymtab();
3209 try self.writeShStrtab();
3210}
3211
3212pub fn writeShStrtab(self: *Elf) !void {
3213 if (self.section_indexes.shstrtab) |index| {
3214 const shdr = self.sections.items(.shdr)[index];
3215 log.debug("writing .shstrtab from 0x{x} to 0x{x}", .{ shdr.sh_offset, shdr.sh_offset + shdr.sh_size });
3216 try self.pwriteAll(self.shstrtab.items, shdr.sh_offset);
3217 }
3218}
3219
3220pub fn writeSymtab(self: *Elf) !void {
3221 const gpa = self.base.comp.gpa;
3222 const shared_objects = self.shared_objects.values();
3223
3224 const slice = self.sections.slice();
3225 const symtab_shdr = slice.items(.shdr)[self.section_indexes.symtab.?];
3226 const strtab_shdr = slice.items(.shdr)[self.section_indexes.strtab.?];
3227 const sym_size: u64 = switch (self.ptr_width) {
3228 .p32 => @sizeOf(elf.Elf32_Sym),
3229 .p64 => @sizeOf(elf.Elf64_Sym),
3230 };
3231 const nsyms = try self.cast(usize, @divExact(symtab_shdr.sh_size, sym_size));
3232
3233 log.debug("writing {d} symbols in .symtab from 0x{x} to 0x{x}", .{
3234 nsyms,
3235 symtab_shdr.sh_offset,
3236 symtab_shdr.sh_offset + symtab_shdr.sh_size,
3237 });
3238 log.debug("writing .strtab from 0x{x} to 0x{x}", .{
3239 strtab_shdr.sh_offset,
3240 strtab_shdr.sh_offset + strtab_shdr.sh_size,
3241 });
3242
3243 try self.symtab.resize(gpa, nsyms);
3244 const needed_strtab_size = try self.cast(usize, strtab_shdr.sh_size - 1);
3245 // TODO we could resize instead and in ZigObject/Object always access as slice
3246 self.strtab.clearRetainingCapacity();
3247 self.strtab.appendAssumeCapacity(0);
3248 try self.strtab.ensureUnusedCapacity(gpa, needed_strtab_size);
3249
3250 for (slice.items(.shdr), 0..) |shdr, shndx| {
3251 const out_sym = &self.symtab.items[shndx];
3252 out_sym.* = .{
3253 .st_name = 0,
3254 .st_value = shdr.sh_addr,
3255 .st_info = if (shdr.sh_type == elf.SHT_NULL) elf.STT_NOTYPE else elf.STT_SECTION,
3256 .st_shndx = @intCast(shndx),
3257 .st_size = 0,
3258 .st_other = 0,
3259 };
3260 }
3261
3262 if (self.requiresThunks()) for (self.thunks.items) |th| {
3263 th.writeSymtab(self);
3264 };
3265
3266 if (self.zigObjectPtr()) |zig_object| {
3267 zig_object.asFile().writeSymtab(self);
3268 }
3269
3270 for (self.objects.items) |index| {
3271 const file_ptr = self.file(index).?;
3272 file_ptr.writeSymtab(self);
3273 }
3274
3275 for (shared_objects) |index| {
3276 const file_ptr = self.file(index).?;
3277 file_ptr.writeSymtab(self);
3278 }
3279
3280 if (self.linkerDefinedPtr()) |obj| {
3281 obj.asFile().writeSymtab(self);
3282 }
3283
3284 if (self.section_indexes.got) |_| {
3285 self.got.writeSymtab(self);
3286 }
3287
3288 if (self.section_indexes.plt) |_| {
3289 self.plt.writeSymtab(self);
3290 }
3291
3292 if (self.section_indexes.plt_got) |_| {
3293 self.plt_got.writeSymtab(self);
3294 }
3295
3296 const foreign_endian = self.getTarget().cpu.arch.endian() != builtin.cpu.arch.endian();
3297 switch (self.ptr_width) {
3298 .p32 => {
3299 const buf = try gpa.alloc(elf.Elf32_Sym, self.symtab.items.len);
3300 defer gpa.free(buf);
3301
3302 for (buf, self.symtab.items) |*out, sym| {
3303 out.* = .{
3304 .st_name = sym.st_name,
3305 .st_info = sym.st_info,
3306 .st_other = sym.st_other,
3307 .st_shndx = sym.st_shndx,
3308 .st_value = @intCast(sym.st_value),
3309 .st_size = @intCast(sym.st_size),
3310 };
3311 if (foreign_endian) mem.byteSwapAllFields(elf.Elf32_Sym, out);
3312 }
3313 try self.pwriteAll(@ptrCast(buf), symtab_shdr.sh_offset);
3314 },
3315 .p64 => {
3316 if (foreign_endian) {
3317 for (self.symtab.items) |*sym| mem.byteSwapAllFields(elf.Elf64_Sym, sym);
3318 }
3319 try self.pwriteAll(@ptrCast(self.symtab.items), symtab_shdr.sh_offset);
3320 },
3321 }
3322
3323 try self.pwriteAll(self.strtab.items, strtab_shdr.sh_offset);
3324}
3325
3326/// Always 4 or 8 depending on whether this is 32-bit ELF or 64-bit ELF.
3327pub fn ptrWidthBytes(self: Elf) u8 {
3328 return switch (self.ptr_width) {
3329 .p32 => 4,
3330 .p64 => 8,
3331 };
3332}
3333
3334/// Does not necessarily match `ptrWidthBytes` for example can be 2 bytes
3335/// in a 32-bit ELF file.
3336pub fn archPtrWidthBytes(self: Elf) u8 {
3337 return @intCast(@divExact(self.getTarget().ptrBitWidth(), 8));
3338}
3339
3340fn phdrTo32(phdr: elf.Elf64.Phdr) elf.Elf32.Phdr {
3341 return .{
3342 .type = phdr.type,
3343 .flags = phdr.flags,
3344 .offset = @intCast(phdr.offset),
3345 .vaddr = @intCast(phdr.vaddr),
3346 .paddr = @intCast(phdr.paddr),
3347 .filesz = @intCast(phdr.filesz),
3348 .memsz = @intCast(phdr.memsz),
3349 .@"align" = @intCast(phdr.@"align"),
3350 };
3351}
3352
3353fn shdrTo32(shdr: elf.Elf64_Shdr) elf.Elf32_Shdr {
3354 return .{
3355 .sh_name = shdr.sh_name,
3356 .sh_type = shdr.sh_type,
3357 .sh_flags = @as(u32, @intCast(shdr.sh_flags)),
3358 .sh_addr = @as(u32, @intCast(shdr.sh_addr)),
3359 .sh_offset = @as(u32, @intCast(shdr.sh_offset)),
3360 .sh_size = @as(u32, @intCast(shdr.sh_size)),
3361 .sh_link = shdr.sh_link,
3362 .sh_info = shdr.sh_info,
3363 .sh_addralign = @as(u32, @intCast(shdr.sh_addralign)),
3364 .sh_entsize = @as(u32, @intCast(shdr.sh_entsize)),
3365 };
3366}
3367
3368pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {
3369 return actual_size +| (actual_size / ideal_factor);
3370}
3371
3372/// If a target compiles other output modes as dynamic libraries,
3373/// this function returns true for those too.
3374pub fn isEffectivelyDynLib(self: Elf) bool {
3375 if (self.base.isDynLib()) return true;
3376 return switch (self.getTarget().os.tag) {
3377 .haiku => self.base.isExe(),
3378 else => false,
3379 };
3380}
3381
3382fn getPhdr(self: *Elf, opts: struct {
3383 type: u32 = 0,
3384 flags: u32 = 0,
3385}) OptionalProgramHeaderIndex {
3386 for (self.phdrs.items, 0..) |phdr, phndx| {
3387 if (self.phdr_indexes.table_load.int()) |index| {
3388 if (phndx == index) continue;
3389 }
3390 if (@backingInt(phdr.type) == opts.type and @backingInt(phdr.flags) == opts.flags)
3391 return @fromBackingInt(@intCast(phndx));
3392 }
3393 return .none;
3394}
3395
3396fn addPhdr(self: *Elf, opts: struct {
3397 type: u32 = 0,
3398 flags: u32 = 0,
3399 @"align": u64 = 0,
3400 offset: u64 = 0,
3401 addr: u64 = 0,
3402 filesz: u64 = 0,
3403 memsz: u64 = 0,
3404}) error{OutOfMemory}!ProgramHeaderIndex {
3405 const gpa = self.base.comp.gpa;
3406 const index: ProgramHeaderIndex = @fromBackingInt(@intCast(self.phdrs.items.len));
3407 try self.phdrs.append(gpa, .{
3408 .type = @fromBackingInt(opts.type),
3409 .flags = @fromBackingInt(opts.flags),
3410 .offset = opts.offset,
3411 .vaddr = opts.addr,
3412 .paddr = opts.addr,
3413 .filesz = opts.filesz,
3414 .memsz = opts.memsz,
3415 .@"align" = opts.@"align",
3416 });
3417 return index;
3418}
3419
3420pub fn addRelaShdr(self: *Elf, name: u32, shndx: u32) !u32 {
3421 const entsize: u64 = switch (self.ptr_width) {
3422 .p32 => @sizeOf(elf.Elf32_Rela),
3423 .p64 => @sizeOf(elf.Elf64_Rela),
3424 };
3425 const addralign: u64 = switch (self.ptr_width) {
3426 .p32 => @alignOf(elf.Elf32_Rela),
3427 .p64 => @alignOf(elf.Elf64_Rela),
3428 };
3429 return self.addSection(.{
3430 .name = name,
3431 .type = elf.SHT_RELA,
3432 .flags = elf.SHF_INFO_LINK,
3433 .entsize = entsize,
3434 .info = shndx,
3435 .addralign = addralign,
3436 });
3437}
3438
3439pub const AddSectionOpts = struct {
3440 name: u32 = 0,
3441 type: u32 = elf.SHT_NULL,
3442 flags: u64 = 0,
3443 link: u32 = 0,
3444 info: u32 = 0,
3445 addralign: u64 = 0,
3446 entsize: u64 = 0,
3447};
3448
3449pub fn addSection(self: *Elf, opts: AddSectionOpts) !u32 {
3450 const gpa = self.base.comp.gpa;
3451 const index: u32 = @intCast(try self.sections.addOne(gpa));
3452 self.sections.set(index, .{
3453 .shdr = .{
3454 .sh_name = opts.name,
3455 .sh_type = opts.type,
3456 .sh_flags = opts.flags,
3457 .sh_addr = 0,
3458 .sh_offset = 0,
3459 .sh_size = 0,
3460 .sh_link = opts.link,
3461 .sh_info = opts.info,
3462 .sh_addralign = opts.addralign,
3463 .sh_entsize = opts.entsize,
3464 },
3465 });
3466 return index;
3467}
3468
3469pub fn sectionByName(self: *Elf, name: [:0]const u8) ?u32 {
3470 for (self.sections.items(.shdr), 0..) |*shdr, i| {
3471 const this_name = self.getShString(shdr.sh_name);
3472 if (mem.eql(u8, this_name, name)) return @intCast(i);
3473 } else return null;
3474}
3475
3476const RelaDyn = struct {
3477 offset: u64,
3478 sym: u64 = 0,
3479 type: u32,
3480 addend: i64 = 0,
3481 target: ?*const Symbol = null,
3482};
3483
3484pub fn addRelaDyn(self: *Elf, opts: RelaDyn) !void {
3485 try self.rela_dyn.ensureUnusedCapacity(self.base.alloctor, 1);
3486 self.addRelaDynAssumeCapacity(opts);
3487}
3488
3489pub fn addRelaDynAssumeCapacity(self: *Elf, opts: RelaDyn) void {
3490 relocs_log.debug(" {f}: [{x} => {d}({s})] + {x}", .{
3491 relocation.fmtRelocType(opts.type, self.getTarget().cpu.arch),
3492 opts.offset,
3493 opts.sym,
3494 if (opts.target) |sym| sym.name(self) else "",
3495 opts.addend,
3496 });
3497 self.rela_dyn.appendAssumeCapacity(.{
3498 .r_offset = opts.offset,
3499 .r_info = (opts.sym << 32) | opts.type,
3500 .r_addend = opts.addend,
3501 });
3502}
3503
3504fn sortRelaDyn(self: *Elf) void {
3505 const Sort = struct {
3506 fn rank(rel: elf.Elf64_Rela, ctx: *Elf) u2 {
3507 const cpu_arch = ctx.getTarget().cpu.arch;
3508 const r_type = rel.r_type();
3509 const r_kind = relocation.decode(r_type, cpu_arch).?;
3510 return switch (r_kind) {
3511 .rel => 0,
3512 .irel => 2,
3513 else => 1,
3514 };
3515 }
3516
3517 pub fn lessThan(ctx: *Elf, lhs: elf.Elf64_Rela, rhs: elf.Elf64_Rela) bool {
3518 if (rank(lhs, ctx) == rank(rhs, ctx)) {
3519 if (lhs.r_sym() == rhs.r_sym()) return lhs.r_offset < rhs.r_offset;
3520 return lhs.r_sym() < rhs.r_sym();
3521 }
3522 return rank(lhs, ctx) < rank(rhs, ctx);
3523 }
3524 };
3525 mem.sort(elf.Elf64_Rela, self.rela_dyn.items, self, Sort.lessThan);
3526}
3527
3528pub fn calcNumIRelativeRelocs(self: *Elf) usize {
3529 var count: usize = self.num_ifunc_dynrelocs;
3530
3531 for (self.got.entries.items) |entry| {
3532 if (entry.tag != .got) continue;
3533 const sym = self.symbol(entry.ref).?;
3534 if (sym.isIFunc(self)) count += 1;
3535 }
3536
3537 return count;
3538}
3539
3540pub fn getStartStopBasename(self: Elf, shdr: elf.Elf64_Shdr) ?[]const u8 {
3541 const name = self.getShString(shdr.sh_name);
3542 if (shdr.sh_flags & elf.SHF_ALLOC != 0 and name.len > 0) {
3543 if (Elf.isCIdentifier(name)) return name;
3544 }
3545 return null;
3546}
3547
3548pub fn isCIdentifier(name: []const u8) bool {
3549 if (name.len == 0) return false;
3550 const first_c = name[0];
3551 if (!std.ascii.isAlphabetic(first_c) and first_c != '_') return false;
3552 for (name[1..]) |c| {
3553 if (!std.ascii.isAlphanumeric(c) and c != '_') return false;
3554 }
3555 return true;
3556}
3557
3558pub fn addThunk(self: *Elf) !Thunk.Index {
3559 const index = @as(Thunk.Index, @intCast(self.thunks.items.len));
3560 const th = try self.thunks.addOne(self.base.comp.gpa);
3561 th.* = .{};
3562 return index;
3563}
3564
3565pub fn thunk(self: *Elf, index: Thunk.Index) *Thunk {
3566 assert(index < self.thunks.items.len);
3567 return &self.thunks.items[index];
3568}
3569
3570pub fn file(self: *Elf, index: File.Index) ?File {
3571 return fileLookup(self.files, index, self.zig_object);
3572}
3573
3574fn fileLookup(files: std.MultiArrayList(File.Entry), index: File.Index, zig_object: ?*ZigObject) ?File {
3575 const tag = files.items(.tags)[index];
3576 return switch (tag) {
3577 .null => null,
3578 .linker_defined => .{ .linker_defined = &files.items(.data)[index].linker_defined },
3579 .zig_object => .{ .zig_object = zig_object.? },
3580 .object => .{ .object = &files.items(.data)[index].object },
3581 .shared_object => .{ .shared_object = &files.items(.data)[index].shared_object },
3582 };
3583}
3584
3585pub fn addFileHandle(
3586 gpa: Allocator,
3587 file_handles: *std.ArrayList(File.Handle),
3588 handle: Io.File,
3589) Allocator.Error!File.HandleIndex {
3590 try file_handles.append(gpa, handle);
3591 return @intCast(file_handles.items.len - 1);
3592}
3593
3594pub fn fileHandle(self: Elf, index: File.HandleIndex) File.Handle {
3595 return self.file_handles.items[index];
3596}
3597
3598pub fn atom(self: *Elf, ref: Ref) ?*Atom {
3599 const file_ptr = self.file(ref.file) orelse return null;
3600 return file_ptr.atom(ref.index);
3601}
3602
3603pub fn group(self: *Elf, ref: Ref) *Group {
3604 return self.file(ref.file).?.group(ref.index);
3605}
3606
3607pub fn symbol(self: *Elf, ref: Ref) ?*Symbol {
3608 const file_ptr = self.file(ref.file) orelse return null;
3609 return file_ptr.symbol(ref.index);
3610}
3611
3612pub fn getGlobalSymbol(self: *Elf, name: []const u8, lib_name: ?[]const u8) !u32 {
3613 return self.zigObjectPtr().?.getGlobalSymbol(self, name, lib_name);
3614}
3615
3616pub fn zigObjectPtr(self: *Elf) ?*ZigObject {
3617 return self.zig_object;
3618}
3619
3620pub fn linkerDefinedPtr(self: *Elf) ?*LinkerDefined {
3621 const index = self.linker_defined_index orelse return null;
3622 return self.file(index).?.linker_defined;
3623}
3624
3625pub fn getOrCreateMergeSection(self: *Elf, name: [:0]const u8, flags: u64, @"type": u32) !Merge.Section.Index {
3626 const gpa = self.base.comp.gpa;
3627 const out_name = name: {
3628 if (self.base.isRelocatable()) break :name name;
3629 if (mem.eql(u8, name, ".rodata") or mem.startsWith(u8, name, ".rodata"))
3630 break :name if (flags & elf.SHF_STRINGS != 0) ".rodata.str" else ".rodata.cst";
3631 break :name name;
3632 };
3633 for (self.merge_sections.items, 0..) |msec, index| {
3634 if (mem.eql(u8, msec.name(self), out_name)) return @intCast(index);
3635 }
3636 const out_off = try self.insertShString(out_name);
3637 const out_flags = flags & ~@as(u64, elf.SHF_COMPRESSED | elf.SHF_GROUP);
3638 const index: Merge.Section.Index = @intCast(self.merge_sections.items.len);
3639 const msec = try self.merge_sections.addOne(gpa);
3640 msec.* = .{
3641 .name_offset = out_off,
3642 .flags = out_flags,
3643 .type = @"type",
3644 };
3645 return index;
3646}
3647
3648pub fn mergeSection(self: *Elf, index: Merge.Section.Index) *Merge.Section {
3649 assert(index < self.merge_sections.items.len);
3650 return &self.merge_sections.items[index];
3651}
3652
3653pub fn gotAddress(self: *Elf) i64 {
3654 const shndx = blk: {
3655 if (self.getTarget().cpu.arch == .x86_64 and self.section_indexes.got_plt != null)
3656 break :blk self.section_indexes.got_plt.?;
3657 break :blk if (self.section_indexes.got) |shndx| shndx else null;
3658 };
3659 return if (shndx) |index| @intCast(self.sections.items(.shdr)[index].sh_addr) else 0;
3660}
3661
3662pub fn tpAddress(self: *Elf) i64 {
3663 const index = self.phdr_indexes.tls.int() orelse return 0;
3664 const phdr = self.phdrs.items[index];
3665 const addr = switch (self.getTarget().cpu.arch) {
3666 .x86_64 => mem.alignForward(u64, phdr.vaddr + phdr.memsz, phdr.@"align"),
3667 .aarch64, .aarch64_be => mem.alignBackward(u64, phdr.vaddr - 16, phdr.@"align"),
3668 .riscv64, .riscv64be => phdr.vaddr,
3669 else => |arch| std.debug.panic("TODO implement getTpAddress for {s}", .{@tagName(arch)}),
3670 };
3671 return @intCast(addr);
3672}
3673
3674pub fn dtpAddress(self: *Elf) i64 {
3675 const index = self.phdr_indexes.tls.int() orelse return 0;
3676 const phdr = self.phdrs.items[index];
3677 return @intCast(phdr.vaddr);
3678}
3679
3680pub fn tlsAddress(self: *Elf) i64 {
3681 const index = self.phdr_indexes.tls.int() orelse return 0;
3682 const phdr = self.phdrs.items[index];
3683 return @intCast(phdr.vaddr);
3684}
3685
3686pub fn getShString(self: Elf, off: u32) [:0]const u8 {
3687 return shString(self.shstrtab.items, off);
3688}
3689
3690fn shString(
3691 shstrtab: []const u8,
3692 off: u32,
3693) [:0]const u8 {
3694 const slice = shstrtab[off..];
3695 return slice[0..mem.findScalar(u8, slice, 0).? :0];
3696}
3697
3698pub fn insertShString(self: *Elf, name: [:0]const u8) error{OutOfMemory}!u32 {
3699 const gpa = self.base.comp.gpa;
3700 const off = @as(u32, @intCast(self.shstrtab.items.len));
3701 try self.shstrtab.ensureUnusedCapacity(gpa, name.len + 1);
3702 self.shstrtab.print(gpa, "{s}\x00", .{name}) catch unreachable;
3703 return off;
3704}
3705
3706pub fn getDynString(self: Elf, off: u32) [:0]const u8 {
3707 assert(off < self.dynstrtab.items.len);
3708 return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.dynstrtab.items.ptr + off)), 0);
3709}
3710
3711pub fn insertDynString(self: *Elf, name: []const u8) error{OutOfMemory}!u32 {
3712 const gpa = self.base.comp.gpa;
3713 const off = @as(u32, @intCast(self.dynstrtab.items.len));
3714 try self.dynstrtab.ensureUnusedCapacity(gpa, name.len + 1);
3715 self.dynstrtab.print(gpa, "{s}\x00", .{name}) catch unreachable;
3716 return off;
3717}
3718
3719fn reportUndefinedSymbols(self: *Elf, undefs: anytype) !void {
3720 const gpa = self.base.comp.gpa;
3721 const diags = &self.base.comp.link_diags;
3722 const max_notes = 4;
3723
3724 try diags.msgs.ensureUnusedCapacity(gpa, undefs.count());
3725
3726 for (undefs.keys(), undefs.values()) |key, refs| {
3727 const undef_sym = self.resolver.keys.items[key - 1];
3728 const nrefs = @min(refs.items.len, max_notes);
3729 const nnotes = nrefs + @intFromBool(refs.items.len > max_notes);
3730
3731 var err = try diags.addErrorWithNotesAssumeCapacity(nnotes);
3732 try err.addMsg("undefined symbol: {s}", .{undef_sym.name(self)});
3733
3734 for (refs.items[0..nrefs]) |ref| {
3735 const atom_ptr = self.atom(ref).?;
3736 const file_ptr = atom_ptr.file(self).?;
3737 err.addNote("referenced by {f}:{s}", .{ file_ptr.fmtPath(), atom_ptr.name(self) });
3738 }
3739
3740 if (refs.items.len > max_notes) {
3741 const remaining = refs.items.len - max_notes;
3742 err.addNote("referenced {d} more times", .{remaining});
3743 }
3744 }
3745}
3746
3747fn reportDuplicates(self: *Elf, dupes: anytype) error{ HasDuplicates, OutOfMemory }!void {
3748 if (dupes.keys().len == 0) return; // Nothing to do
3749 const diags = &self.base.comp.link_diags;
3750
3751 const max_notes = 3;
3752
3753 for (dupes.keys(), dupes.values()) |key, notes| {
3754 const sym = self.resolver.keys.items[key - 1];
3755 const nnotes = @min(notes.items.len, max_notes) + @intFromBool(notes.items.len > max_notes);
3756
3757 var err = try diags.addErrorWithNotes(nnotes + 1);
3758 try err.addMsg("duplicate symbol definition: {s}", .{sym.name(self)});
3759 err.addNote("defined by {f}", .{sym.file(self).?.fmtPath()});
3760
3761 var inote: usize = 0;
3762 while (inote < @min(notes.items.len, max_notes)) : (inote += 1) {
3763 const file_ptr = self.file(notes.items[inote]).?;
3764 err.addNote("defined by {f}", .{file_ptr.fmtPath()});
3765 }
3766
3767 if (notes.items.len > max_notes) {
3768 const remaining = notes.items.len - max_notes;
3769 err.addNote("defined {d} more times", .{remaining});
3770 }
3771 }
3772
3773 return error.HasDuplicates;
3774}
3775
3776fn reportUnsupportedCpuArch(self: *Elf) error{OutOfMemory}!void {
3777 const diags = &self.base.comp.link_diags;
3778 var err = try diags.addErrorWithNotes(0);
3779 try err.addMsg("fatal linker error: unsupported CPU architecture {s}", .{
3780 @tagName(self.getTarget().cpu.arch),
3781 });
3782}
3783
3784pub fn addFileError(
3785 self: *Elf,
3786 file_index: File.Index,
3787 comptime format: []const u8,
3788 args: anytype,
3789) error{OutOfMemory}!void {
3790 const diags = &self.base.comp.link_diags;
3791 var err = try diags.addErrorWithNotes(1);
3792 try err.addMsg(format, args);
3793 err.addNote("while parsing {f}", .{self.file(file_index).?.fmtPath()});
3794}
3795
3796pub fn failFile(
3797 self: *Elf,
3798 file_index: File.Index,
3799 comptime format: []const u8,
3800 args: anytype,
3801) error{ OutOfMemory, AlreadyReported } {
3802 try addFileError(self, file_index, format, args);
3803 return error.AlreadyReported;
3804}
3805
3806const FormatShdr = struct {
3807 elf_file: *Elf,
3808 shdr: elf.Elf64_Shdr,
3809};
3810
3811fn fmtShdr(self: *Elf, shdr: elf.Elf64_Shdr) std.fmt.Alt(FormatShdr, formatShdr) {
3812 return .{ .data = .{
3813 .shdr = shdr,
3814 .elf_file = self,
3815 } };
3816}
3817
3818fn formatShdr(ctx: FormatShdr, writer: *std.Io.Writer) std.Io.Writer.Error!void {
3819 const shdr = ctx.shdr;
3820 try writer.print("{s} : @{x} ({x}) : align({x}) : size({x}) : entsize({x}) : flags({f})", .{
3821 ctx.elf_file.getShString(shdr.sh_name), shdr.sh_offset,
3822 shdr.sh_addr, shdr.sh_addralign,
3823 shdr.sh_size, shdr.sh_entsize,
3824 fmtShdrFlags(shdr.sh_flags),
3825 });
3826}
3827
3828pub fn fmtShdrFlags(sh_flags: u64) std.fmt.Alt(u64, formatShdrFlags) {
3829 return .{ .data = sh_flags };
3830}
3831
3832fn formatShdrFlags(sh_flags: u64, writer: *std.Io.Writer) std.Io.Writer.Error!void {
3833 if (elf.SHF_WRITE & sh_flags != 0) {
3834 try writer.writeAll("W");
3835 }
3836 if (elf.SHF_ALLOC & sh_flags != 0) {
3837 try writer.writeAll("A");
3838 }
3839 if (elf.SHF_EXECINSTR & sh_flags != 0) {
3840 try writer.writeAll("X");
3841 }
3842 if (elf.SHF_MERGE & sh_flags != 0) {
3843 try writer.writeAll("M");
3844 }
3845 if (elf.SHF_STRINGS & sh_flags != 0) {
3846 try writer.writeAll("S");
3847 }
3848 if (elf.SHF_INFO_LINK & sh_flags != 0) {
3849 try writer.writeAll("I");
3850 }
3851 if (elf.SHF_LINK_ORDER & sh_flags != 0) {
3852 try writer.writeAll("L");
3853 }
3854 if (elf.SHF_EXCLUDE & sh_flags != 0) {
3855 try writer.writeAll("E");
3856 }
3857 if (elf.SHF_COMPRESSED & sh_flags != 0) {
3858 try writer.writeAll("C");
3859 }
3860 if (elf.SHF_GROUP & sh_flags != 0) {
3861 try writer.writeAll("G");
3862 }
3863 if (elf.SHF_OS_NONCONFORMING & sh_flags != 0) {
3864 try writer.writeAll("O");
3865 }
3866 if (elf.SHF_TLS & sh_flags != 0) {
3867 try writer.writeAll("T");
3868 }
3869 if (elf.SHF_X86_64_LARGE & sh_flags != 0) {
3870 try writer.writeAll("l");
3871 }
3872 if (elf.SHF_MIPS_ADDR & sh_flags != 0 or elf.SHF_ARM_PURECODE & sh_flags != 0) {
3873 try writer.writeAll("p");
3874 }
3875}
3876
3877const FormatPhdr = struct {
3878 elf_file: *Elf,
3879 phdr: elf.Elf64.Phdr,
3880};
3881
3882fn fmtPhdr(self: *Elf, phdr: elf.Elf64.Phdr) std.fmt.Alt(FormatPhdr, formatPhdr) {
3883 return .{ .data = .{
3884 .phdr = phdr,
3885 .elf_file = self,
3886 } };
3887}
3888
3889fn formatPhdr(ctx: FormatPhdr, writer: *std.Io.Writer) std.Io.Writer.Error!void {
3890 const phdr = ctx.phdr;
3891 const write = phdr.flags.W;
3892 const read = phdr.flags.R;
3893 const exec = phdr.flags.X;
3894 var flags: [3]u8 = @splat('_');
3895 if (exec) flags[0] = 'X';
3896 if (write) flags[1] = 'W';
3897 if (read) flags[2] = 'R';
3898 const p_type = switch (phdr.type) {
3899 .LOAD => "LOAD",
3900 .TLS => "TLS",
3901 .GNU_EH_FRAME => "GNU_EH_FRAME",
3902 .GNU_STACK => "GNU_STACK",
3903 .DYNAMIC => "DYNAMIC",
3904 .INTERP => "INTERP",
3905 .NULL => "NULL",
3906 .PHDR => "PHDR",
3907 .NOTE => "NOTE",
3908 else => "UNKNOWN",
3909 };
3910 try writer.print("{s} : {s} : @{x} ({x}) : align({x}) : filesz({x}) : memsz({x})", .{
3911 p_type, flags, phdr.offset, phdr.vaddr,
3912 phdr.@"align", phdr.filesz, phdr.memsz,
3913 });
3914}
3915
3916pub fn dumpState(self: *Elf) std.fmt.Alt(*Elf, fmtDumpState) {
3917 return .{ .data = self };
3918}
3919
3920fn fmtDumpState(self: *Elf, writer: *std.Io.Writer) std.Io.Writer.Error!void {
3921 const shared_objects = self.shared_objects.values();
3922
3923 if (self.zigObjectPtr()) |zig_object| {
3924 try writer.print("zig_object({d}) : {s}\n", .{ zig_object.index, zig_object.basename });
3925 try writer.print("{f}{f}", .{
3926 zig_object.fmtAtoms(self),
3927 zig_object.fmtSymtab(self),
3928 });
3929 try writer.writeByte('\n');
3930 }
3931
3932 for (self.objects.items) |index| {
3933 const object = self.file(index).?.object;
3934 try writer.print("object({d}) : {f}", .{ index, object.fmtPath() });
3935 if (!object.alive) try writer.writeAll(" : [*]");
3936 try writer.writeByte('\n');
3937 try writer.print("{f}{f}{f}{f}{f}\n", .{
3938 object.fmtAtoms(self),
3939 object.fmtCies(self),
3940 object.fmtFdes(self),
3941 object.fmtSymtab(self),
3942 object.fmtGroups(self),
3943 });
3944 }
3945
3946 for (shared_objects) |index| {
3947 const shared_object = self.file(index).?.shared_object;
3948 try writer.print("shared_object({d}) : {f} : needed({})", .{
3949 index, shared_object.path, shared_object.needed,
3950 });
3951 if (!shared_object.alive) try writer.writeAll(" : [*]");
3952 try writer.writeByte('\n');
3953 try writer.print("{f}\n", .{shared_object.fmtSymtab(self)});
3954 }
3955
3956 if (self.linker_defined_index) |index| {
3957 const linker_defined = self.file(index).?.linker_defined;
3958 try writer.print("linker_defined({d}) : (linker defined)\n", .{index});
3959 try writer.print("{f}\n", .{linker_defined.fmtSymtab(self)});
3960 }
3961
3962 const slice = self.sections.slice();
3963 {
3964 try writer.writeAll("atom lists\n");
3965 for (slice.items(.shdr), slice.items(.atom_list_2), 0..) |shdr, atom_list, shndx| {
3966 try writer.print("shdr({d}) : {s} : {f}\n", .{ shndx, self.getShString(shdr.sh_name), atom_list.fmt(self) });
3967 }
3968 }
3969
3970 if (self.requiresThunks()) {
3971 try writer.writeAll("thunks\n");
3972 for (self.thunks.items, 0..) |th, index| {
3973 try writer.print("thunk({d}) : {f}\n", .{ index, th.fmt(self) });
3974 }
3975 }
3976
3977 try writer.print("{f}\n", .{self.got.fmt(self)});
3978 try writer.print("{f}\n", .{self.plt.fmt(self)});
3979
3980 try writer.writeAll("Output groups\n");
3981 for (self.group_sections.items) |cg| {
3982 try writer.print(" shdr({d}) : GROUP({f})\n", .{ cg.shndx, cg.cg_ref });
3983 }
3984
3985 try writer.writeAll("\nOutput merge sections\n");
3986 for (self.merge_sections.items) |msec| {
3987 try writer.print(" shdr({d}) : {f}\n", .{ msec.output_section_index, msec.fmt(self) });
3988 }
3989
3990 try writer.writeAll("\nOutput shdrs\n");
3991 for (slice.items(.shdr), slice.items(.phndx), 0..) |shdr, phndx, shndx| {
3992 try writer.print(" shdr({d}) : phdr({d}) : {f}\n", .{
3993 shndx,
3994 phndx,
3995 self.fmtShdr(shdr),
3996 });
3997 }
3998 try writer.writeAll("\nOutput phdrs\n");
3999 for (self.phdrs.items, 0..) |phdr, phndx| {
4000 try writer.print(" phdr({d}) : {f}\n", .{ phndx, self.fmtPhdr(phdr) });
4001 }
4002}
4003
4004/// Caller owns the memory.
4005pub fn preadAllAlloc(allocator: Allocator, io: Io, io_file: Io.File, offset: u64, size: u64) ![]u8 {
4006 const buffer = try allocator.alloc(u8, math.cast(usize, size) orelse return error.Overflow);
4007 errdefer allocator.free(buffer);
4008 const amt = try io_file.readPositionalAll(io, buffer, offset);
4009 if (amt != size) return error.InputOutput;
4010 return buffer;
4011}
4012
4013/// Binary search
4014pub fn bsearch(comptime T: type, haystack: []const T, predicate: anytype) usize {
4015 var min: usize = 0;
4016 var max: usize = haystack.len;
4017 while (min < max) {
4018 const index = (min + max) / 2;
4019 const curr = haystack[index];
4020 if (predicate.predicate(curr)) {
4021 min = index + 1;
4022 } else {
4023 max = index;
4024 }
4025 }
4026 return min;
4027}
4028
4029/// Linear search
4030pub fn lsearch(comptime T: type, haystack: []const T, predicate: anytype) usize {
4031 var i: usize = 0;
4032 while (i < haystack.len) : (i += 1) {
4033 if (predicate.predicate(haystack[i])) break;
4034 }
4035 return i;
4036}
4037
4038pub fn getTarget(self: *const Elf) *const std.Target {
4039 return &self.base.comp.root_mod.resolved_target.result;
4040}
4041
4042fn requiresThunks(self: Elf) bool {
4043 return switch (self.getTarget().cpu.arch) {
4044 .aarch64, .aarch64_be => true,
4045 .x86_64, .riscv64, .riscv64be => false,
4046 else => @panic("TODO unimplemented architecture"),
4047 };
4048}
4049
4050/// The following three values are only observed at compile-time and used to emit a compile error
4051/// to remind the programmer to update expected maximum numbers of different program header types
4052/// so that we reserve enough space for the program header table up-front.
4053/// Bump these numbers when adding or deleting a Zig specific pre-allocated segment, or adding
4054/// more special-purpose program headers.
4055const max_number_of_object_segments = 9;
4056const max_number_of_special_phdrs = 5;
4057
4058const default_entry_addr = 0x8000000;
4059
4060pub const base_tag: link.File.Tag = .elf;
4061
4062pub const Group = struct {
4063 signature_off: u32,
4064 file_index: File.Index,
4065 shndx: u32,
4066 members_start: u32,
4067 members_len: u32,
4068 is_comdat: bool,
4069 alive: bool = true,
4070
4071 pub fn file(cg: Group, elf_file: *Elf) File {
4072 return elf_file.file(cg.file_index).?;
4073 }
4074
4075 pub fn signature(cg: Group, elf_file: *Elf) [:0]const u8 {
4076 return cg.file(elf_file).object.getString(cg.signature_off);
4077 }
4078
4079 pub fn members(cg: Group, elf_file: *Elf) []const u32 {
4080 const object = cg.file(elf_file).object;
4081 return object.group_data.items[cg.members_start..][0..cg.members_len];
4082 }
4083
4084 pub const Index = u32;
4085};
4086
4087pub const SymtabCtx = struct {
4088 ilocal: u32 = 0,
4089 iglobal: u32 = 0,
4090 nlocals: u32 = 0,
4091 nglobals: u32 = 0,
4092 strsize: u32 = 0,
4093
4094 pub fn reset(ctx: *SymtabCtx) void {
4095 ctx.ilocal = 0;
4096 ctx.iglobal = 0;
4097 ctx.nlocals = 0;
4098 ctx.nglobals = 0;
4099 ctx.strsize = 0;
4100 }
4101};
4102
4103pub const null_sym = elf.Elf64_Sym{
4104 .st_name = 0,
4105 .st_info = 0,
4106 .st_other = 0,
4107 .st_shndx = 0,
4108 .st_value = 0,
4109 .st_size = 0,
4110};
4111
4112pub const null_shdr = elf.Elf64_Shdr{
4113 .sh_name = 0,
4114 .sh_type = 0,
4115 .sh_flags = 0,
4116 .sh_addr = 0,
4117 .sh_offset = 0,
4118 .sh_size = 0,
4119 .sh_link = 0,
4120 .sh_info = 0,
4121 .sh_addralign = 0,
4122 .sh_entsize = 0,
4123};
4124
4125pub const SystemLib = struct {
4126 needed: bool = false,
4127 path: Path,
4128};
4129
4130pub const Ref = struct {
4131 index: u32 = 0,
4132 file: u32 = 0,
4133
4134 pub fn eql(ref: Ref, other: Ref) bool {
4135 return ref.index == other.index and ref.file == other.file;
4136 }
4137
4138 pub fn format(ref: Ref, writer: *std.Io.Writer) std.Io.Writer.Error!void {
4139 try writer.print("ref({d},{d})", .{ ref.index, ref.file });
4140 }
4141};
4142
4143pub const SymbolResolver = struct {
4144 keys: std.ArrayList(Key) = .empty,
4145 values: std.ArrayList(Ref) = .empty,
4146 table: std.array_hash_map.Auto(void, void) = .empty,
4147
4148 const Result = struct {
4149 found_existing: bool,
4150 index: Index,
4151 ref: *Ref,
4152 };
4153
4154 pub fn deinit(resolver: *SymbolResolver, allocator: Allocator) void {
4155 resolver.keys.deinit(allocator);
4156 resolver.values.deinit(allocator);
4157 resolver.table.deinit(allocator);
4158 }
4159
4160 pub fn getOrPut(
4161 resolver: *SymbolResolver,
4162 allocator: Allocator,
4163 ref: Ref,
4164 elf_file: *Elf,
4165 ) !Result {
4166 const adapter = Adapter{ .keys = resolver.keys.items, .elf_file = elf_file };
4167 const key = Key{ .index = ref.index, .file_index = ref.file };
4168 const gop = try resolver.table.getOrPutAdapted(allocator, key, adapter);
4169 if (!gop.found_existing) {
4170 try resolver.keys.append(allocator, key);
4171 _ = try resolver.values.addOne(allocator);
4172 }
4173 return .{
4174 .found_existing = gop.found_existing,
4175 .index = @intCast(gop.index + 1),
4176 .ref = &resolver.values.items[gop.index],
4177 };
4178 }
4179
4180 pub fn get(resolver: SymbolResolver, index: Index) ?Ref {
4181 if (index == 0) return null;
4182 return resolver.values.items[index - 1];
4183 }
4184
4185 pub fn reset(resolver: *SymbolResolver) void {
4186 resolver.keys.clearRetainingCapacity();
4187 resolver.values.clearRetainingCapacity();
4188 resolver.table.clearRetainingCapacity();
4189 }
4190
4191 const Key = struct {
4192 index: Symbol.Index,
4193 file_index: File.Index,
4194
4195 fn name(key: Key, elf_file: *Elf) [:0]const u8 {
4196 const ref = Ref{ .index = key.index, .file = key.file_index };
4197 return elf_file.symbol(ref).?.name(elf_file);
4198 }
4199
4200 fn file(key: Key, elf_file: *Elf) ?File {
4201 return elf_file.file(key.file_index);
4202 }
4203
4204 fn eql(key: Key, other: Key, elf_file: *Elf) bool {
4205 const key_name = key.name(elf_file);
4206 const other_name = other.name(elf_file);
4207 return mem.eql(u8, key_name, other_name);
4208 }
4209
4210 fn hash(key: Key, elf_file: *Elf) u32 {
4211 return @truncate(Hash.hash(0, key.name(elf_file)));
4212 }
4213 };
4214
4215 const Adapter = struct {
4216 keys: []const Key,
4217 elf_file: *Elf,
4218
4219 pub fn eql(ctx: @This(), key: Key, b_void: void, b_map_index: usize) bool {
4220 _ = b_void;
4221 const other = ctx.keys[b_map_index];
4222 return key.eql(other, ctx.elf_file);
4223 }
4224
4225 pub fn hash(ctx: @This(), key: Key) u32 {
4226 return key.hash(ctx.elf_file);
4227 }
4228 };
4229
4230 pub const Index = u32;
4231};
4232
4233const Section = struct {
4234 /// Section header.
4235 shdr: elf.Elf64_Shdr,
4236
4237 /// Assigned program header index if any.
4238 phndx: OptionalProgramHeaderIndex = .none,
4239
4240 /// List of atoms contributing to this section.
4241 /// TODO currently this is only used for relocations tracking in relocatable mode
4242 /// but will be merged with atom_list_2.
4243 atom_list: std.ArrayList(Ref) = .empty,
4244
4245 /// List of atoms contributing to this section.
4246 /// This can be used by sections that require special handling such as init/fini array, etc.
4247 atom_list_2: AtomList = .{},
4248
4249 /// Index of the last allocated atom in this section.
4250 last_atom: Ref = .{ .index = 0, .file = 0 },
4251
4252 /// A list of atoms that have surplus capacity. This list can have false
4253 /// positives, as functions grow and shrink over time, only sometimes being added
4254 /// or removed from the freelist.
4255 ///
4256 /// An atom has surplus capacity when its overcapacity value is greater than
4257 /// padToIdeal(minimum_atom_size). That is, when it has so
4258 /// much extra capacity, that we could fit a small new symbol in it, itself with
4259 /// ideal_capacity or more.
4260 ///
4261 /// Ideal capacity is defined by size + (size / ideal_factor)
4262 ///
4263 /// Overcapacity is measured by actual_capacity - ideal_capacity. Note that
4264 /// overcapacity can be negative. A simple way to have negative overcapacity is to
4265 /// allocate a fresh text block, which will have ideal capacity, and then grow it
4266 /// by 1 byte. It will then have -1 overcapacity.
4267 free_list: std.ArrayList(Ref) = .empty,
4268};
4269
4270pub fn sectionSize(self: *Elf, shndx: u32) u64 {
4271 const last_atom_ref = self.sections.items(.last_atom)[shndx];
4272 const atom_ptr = self.atom(last_atom_ref) orelse return 0;
4273 return @as(u64, @intCast(atom_ptr.value)) + atom_ptr.size;
4274}
4275
4276fn defaultEntrySymbolName(cpu_arch: std.Target.Cpu.Arch) []const u8 {
4277 return switch (cpu_arch) {
4278 .mips, .mipsel, .mips64, .mips64el => "__start",
4279 else => "_start",
4280 };
4281}
4282
4283fn createThunks(elf_file: *Elf, atom_list: *AtomList) !void {
4284 const gpa = elf_file.base.comp.gpa;
4285 const cpu_arch = elf_file.getTarget().cpu.arch;
4286
4287 // A branch will need an extender if its target is larger than
4288 // `2^(jump_bits - 1) - margin` where margin is some arbitrary number.
4289 const max_distance = switch (cpu_arch) {
4290 .aarch64, .aarch64_be => 0x500_000,
4291 .x86_64, .riscv64, .riscv64be => unreachable,
4292 else => @panic("unhandled arch"),
4293 };
4294
4295 const advance = struct {
4296 fn advance(list: *AtomList, size: u64, alignment: Atom.Alignment) !i64 {
4297 const offset = alignment.forward(list.size);
4298 const padding = offset - list.size;
4299 list.size += padding + size;
4300 list.alignment = list.alignment.max(alignment);
4301 return @intCast(offset);
4302 }
4303 }.advance;
4304
4305 for (atom_list.atoms.keys()) |ref| {
4306 elf_file.atom(ref).?.value = -1;
4307 }
4308
4309 var i: usize = 0;
4310 while (i < atom_list.atoms.keys().len) {
4311 const start = i;
4312 const start_atom = elf_file.atom(atom_list.atoms.keys()[start]).?;
4313 assert(start_atom.alive);
4314 start_atom.value = try advance(atom_list, start_atom.size, start_atom.alignment);
4315 i += 1;
4316
4317 while (i < atom_list.atoms.keys().len) : (i += 1) {
4318 const atom_ptr = elf_file.atom(atom_list.atoms.keys()[i]).?;
4319 assert(atom_ptr.alive);
4320 if (@as(i64, @intCast(atom_ptr.alignment.forward(atom_list.size))) - start_atom.value >= max_distance)
4321 break;
4322 atom_ptr.value = try advance(atom_list, atom_ptr.size, atom_ptr.alignment);
4323 }
4324
4325 // Insert a thunk at the group end
4326 const thunk_index = try elf_file.addThunk();
4327 const thunk_ptr = elf_file.thunk(thunk_index);
4328 thunk_ptr.output_section_index = atom_list.output_section_index;
4329
4330 // Scan relocs in the group and create trampolines for any unreachable callsite
4331 for (atom_list.atoms.keys()[start..i]) |ref| {
4332 const atom_ptr = elf_file.atom(ref).?;
4333 const file_ptr = atom_ptr.file(elf_file).?;
4334 log.debug("atom({f}) {s}", .{ ref, atom_ptr.name(elf_file) });
4335 for (atom_ptr.relocs(elf_file)) |rel| {
4336 const is_reachable = switch (cpu_arch) {
4337 .aarch64, .aarch64_be => r: {
4338 const r_type: elf.R_AARCH64 = @fromBackingInt(@intCast(rel.r_type()));
4339 if (r_type != .CALL26 and r_type != .JUMP26) break :r true;
4340 const target_ref = file_ptr.resolveSymbol(rel.r_sym(), elf_file);
4341 const target = elf_file.symbol(target_ref).?;
4342 if (target.flags.has_plt) break :r false;
4343 if (atom_ptr.output_section_index != target.output_section_index) break :r false;
4344 const target_atom = target.atom(elf_file).?;
4345 if (target_atom.value == -1) break :r false;
4346 const saddr = atom_ptr.address(elf_file) + @as(i64, @intCast(rel.r_offset));
4347 const taddr = target.address(.{}, elf_file);
4348 _ = math.cast(i28, taddr + rel.r_addend - saddr) orelse break :r false;
4349 break :r true;
4350 },
4351 .x86_64, .riscv64, .riscv64be => unreachable,
4352 else => @panic("unsupported arch"),
4353 };
4354 if (is_reachable) continue;
4355 const target = file_ptr.resolveSymbol(rel.r_sym(), elf_file);
4356 try thunk_ptr.symbols.put(gpa, target, {});
4357 }
4358 atom_ptr.addExtra(.{ .thunk = thunk_index }, elf_file);
4359 }
4360
4361 thunk_ptr.value = try advance(atom_list, thunk_ptr.size(elf_file), Atom.Alignment.fromNonzeroByteUnits(2));
4362
4363 log.debug("thunk({d}) : {f}", .{ thunk_index, thunk_ptr.fmt(elf_file) });
4364 }
4365}
4366
4367pub fn stringTableLookup(strtab: []const u8, off: u32) [:0]const u8 {
4368 const slice = strtab[off..];
4369 return slice[0..mem.findScalar(u8, slice, 0).? :0];
4370}
4371
4372pub fn pwriteAll(elf_file: *Elf, bytes: []const u8, offset: u64) error{AlreadyReported}!void {
4373 const comp = elf_file.base.comp;
4374 const io = comp.io;
4375 const diags = &comp.link_diags;
4376 elf_file.base.file.?.writePositionalAll(io, bytes, offset) catch |err|
4377 return diags.fail("failed to write: {t}", .{err});
4378}
4379
4380pub fn setLength(elf_file: *Elf, length: u64) error{AlreadyReported}!void {
4381 const comp = elf_file.base.comp;
4382 const io = comp.i;
4383 const diags = &comp.link_diags;
4384 elf_file.base.file.?.setLength(io, length) catch |err| {
4385 return diags.fail("failed to set file end pos: {s}", .{@errorName(err)});
4386 };
4387}
4388
4389pub fn cast(elf_file: *Elf, comptime T: type, x: anytype) error{AlreadyReported}!T {
4390 return std.math.cast(T, x) orelse {
4391 const comp = elf_file.base.comp;
4392 const diags = &comp.link_diags;
4393 return diags.fail("encountered {d}, overflowing {d}-bit value", .{ x, @bitSizeOf(T) });
4394 };
4395}
4396
4397const std = @import("std");
4398const Io = std.Io;
4399const build_options = @import("build_options");
4400const builtin = @import("builtin");
4401const assert = std.debug.assert;
4402const elf = std.elf;
4403const fs = std.fs;
4404const log = std.log.scoped(.link);
4405const relocs_log = std.log.scoped(.link_relocs);
4406const state_log = std.log.scoped(.link_state);
4407const math = std.math;
4408const mem = std.mem;
4409const Allocator = std.mem.Allocator;
4410const Hash = std.hash.Wyhash;
4411const Path = std.Build.Cache.Path;
4412const Stat = std.Build.Cache.File.Stat;
4413
4414const codegen = @import("../codegen.zig");
4415const dev = @import("../dev.zig");
4416const eh_frame = @import("Elf/eh_frame.zig");
4417const gc = @import("Elf/gc.zig");
4418const musl = @import("../libs/musl.zig");
4419const link = @import("../link.zig");
4420const relocatable = @import("Elf/relocatable.zig");
4421const relocation = @import("Elf/relocation.zig");
4422const target_util = @import("../target.zig");
4423const trace = @import("../tracy.zig").trace;
4424const synthetic_sections = @import("Elf/synthetic_sections.zig");
4425
4426const Merge = @import("Elf/Merge.zig");
4427const Archive = @import("Elf/Archive.zig");
4428const AtomList = @import("Elf/AtomList.zig");
4429const Compilation = @import("../Compilation.zig");
4430const GroupSection = synthetic_sections.GroupSection;
4431const CopyRelSection = synthetic_sections.CopyRelSection;
4432const Diags = @import("../link.zig").Diags;
4433const DynamicSection = synthetic_sections.DynamicSection;
4434const DynsymSection = synthetic_sections.DynsymSection;
4435const Dwarf = @import("Dwarf.zig");
4436const Elf = @This();
4437const File = @import("Elf/file.zig").File;
4438const GnuHashSection = synthetic_sections.GnuHashSection;
4439const GotSection = synthetic_sections.GotSection;
4440const GotPltSection = synthetic_sections.GotPltSection;
4441const HashSection = synthetic_sections.HashSection;
4442const LinkerDefined = @import("Elf/LinkerDefined.zig");
4443const Zcu = @import("../Zcu.zig");
4444const Object = @import("Elf/Object.zig");
4445const InternPool = @import("../InternPool.zig");
4446const PltSection = synthetic_sections.PltSection;
4447const PltGotSection = synthetic_sections.PltGotSection;
4448const SharedObject = @import("Elf/SharedObject.zig");
4449const Symbol = @import("Elf/Symbol.zig");
4450const StringTable = @import("StringTable.zig");
4451const Thunk = @import("Elf/Thunk.zig");
4452const Value = @import("../Value.zig");
4453const VerneedSection = synthetic_sections.VerneedSection;
4454const ZigObject = @import("Elf/ZigObject.zig");