authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-13 07:29:42+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-18 09:13:08+02:00
log01fc33c949cb8609324cfbcdf7f0524b93ff2561
treebc4d571365cecc08eec3d012f291246fe6ae5521
parent853ca403c4164a67f32773a069b8f5f5579c4542

macho: emit relocs for non-zig-sections in ZigObject


3 files changed, 33 insertions(+), 0 deletions(-)

src/link/MachO/Atom.zig+1
......@@ -979,6 +979,7 @@ pub fn writeRelocs(self: Atom, macho_file: *MachO, code: []u8, buffer: []macho.r
979979 defer i += 1;
980980 const rel_offset = rel.offset - self.off;
981981 const r_address: i32 = math.cast(i32, self.value + rel_offset) orelse return error.Overflow;
982 assert(r_address >= 0);
982983 const r_symbolnum = r_symbolnum: {
983984 const r_symbolnum: u32 = switch (rel.tag) {
984985 .local => rel.getTargetAtom(self, macho_file).out_n_sect + 1,
src/link/MachO/ZigObject.zig+31
......@@ -482,6 +482,37 @@ pub fn writeRelocs(self: *ZigObject, macho_file: *MachO) !void {
482482 }
483483}
484484
485// TODO we need this because not everything gets written out incrementally.
486// For example, TLS data gets written out via traditional route.
487// Is there any better way of handling this?
488pub fn writeAtomsRelocatable(self: *ZigObject, macho_file: *MachO) !void {
489 const tracy = trace(@src());
490 defer tracy.end();
491
492 for (self.getAtoms()) |atom_index| {
493 const atom = self.getAtom(atom_index) orelse continue;
494 if (!atom.flags.alive) continue;
495 const sect = atom.getInputSection(macho_file);
496 if (sect.isZerofill()) continue;
497 if (macho_file.isZigSection(atom.out_n_sect)) continue;
498 const off = atom.value;
499 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items;
500 try self.getAtomData(macho_file, atom.*, buffer[off..][0..atom.size]);
501 const relocs = macho_file.sections.items(.relocs)[atom.out_n_sect].items;
502 const extra = atom.getExtra(macho_file);
503 try atom.writeRelocs(macho_file, buffer[off..][0..atom.size], relocs[extra.rel_out_index..][0..extra.rel_out_count]);
504 }
505}
506
507// TODO we need this because not everything gets written out incrementally.
508// For example, TLS data gets written out via traditional route.
509// Is there any better way of handling this?
510pub fn writeAtoms(self: *ZigObject, macho_file: *MachO) !void {
511 const gpa = macho_file.base.comp.gpa;
512 _ = gpa;
513 _ = self;
514}
515
485516pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) void {
486517 const tracy = trace(@src());
487518 defer tracy.end();
src/link/MachO/relocatable.zig+1
......@@ -657,6 +657,7 @@ fn writeSections(macho_file: *MachO) !void {
657657
658658 if (macho_file.getZigObject()) |zo| {
659659 try zo.writeRelocs(macho_file);
660 try zo.writeAtomsRelocatable(macho_file);
660661 zo.writeSymtab(macho_file, macho_file);
661662 }
662663