authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-09-06 10:38:51+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-09-06 10:38:51+02:00
log5e64d9745ba54d4dd61f8f98be4a3b7e6f2d8205
tree0137249076a56a111388acb8f3f46abfee013918
parent61dca19107a30011ba67754ff867c858024eb5c5

macho: fix noninclusion of data-in-code

Also, calculate non-extern, section offset based addends for SIGNED and UNSIGNED relocations on x86_64 upfront as an offset wrt to the target symbol representing position of the section/atom within the final artifact.

2 files changed, 10 insertions(+), 13 deletions(-)

src/link/MachO/Object.zig+3-3
......@@ -525,7 +525,7 @@ pub fn parseTextBlocks(
525525 break :blk self.header.?.flags & macho.MH_SUBSECTIONS_VIA_SYMBOLS != 0;
526526 };
527527
528 macho_file.has_dices = blk: {
528 macho_file.has_dices = macho_file.has_dices or blk: {
529529 if (self.text_section_index) |index| {
530530 if (index != id) break :blk false;
531531 if (self.data_in_code_entries.items.len == 0) break :blk false;
......@@ -558,7 +558,7 @@ pub fn parseTextBlocks(
558558 .n_type = macho.N_SECT,
559559 .n_sect = @intCast(u8, macho_file.section_ordinals.getIndex(match).? + 1),
560560 .n_desc = 0,
561 .n_value = sect.addr,
561 .n_value = 0,
562562 });
563563 try self.sections_as_symbols.putNoClobber(allocator, sect_id, block_local_sym_index);
564564 break :blk block_local_sym_index;
......@@ -660,7 +660,7 @@ pub fn parseTextBlocks(
660660 .n_type = macho.N_SECT,
661661 .n_sect = @intCast(u8, macho_file.section_ordinals.getIndex(match).? + 1),
662662 .n_desc = 0,
663 .n_value = sect.addr,
663 .n_value = 0,
664664 });
665665 try self.sections_as_symbols.putNoClobber(allocator, sect_id, block_local_sym_index);
666666 break :blk block_local_sym_index;
src/link/MachO/TextBlock.zig+7-10
......@@ -646,7 +646,7 @@ fn initRelocFromObject(rel: macho.relocation_info, context: RelocContext) !Reloc
646646 .n_type = macho.N_SECT,
647647 .n_sect = @intCast(u8, context.macho_file.section_ordinals.getIndex(match).? + 1),
648648 .n_desc = 0,
649 .n_value = sect.addr,
649 .n_value = 0,
650650 });
651651 try context.object.sections_as_symbols.putNoClobber(context.allocator, sect_id, local_sym_index);
652652 break :blk local_sym_index;
......@@ -956,9 +956,9 @@ fn parseUnsigned(
956956 mem.readIntLittle(i32, self.code.items[out.offset..][0..4]);
957957
958958 if (rel.r_extern == 0) {
959 assert(out.where == .local);
960 const target_sym = context.macho_file.locals.items[out.where_index];
961 addend -= @intCast(i64, target_sym.n_value);
959 const source_seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment;
960 const source_sect_base_addr = source_seg.sections.items[rel.r_symbolnum - 1].addr;
961 addend -= @intCast(i64, source_sect_base_addr);
962962 }
963963
964964 out.payload = .{
......@@ -1053,12 +1053,9 @@ fn parseSigned(self: TextBlock, rel: macho.relocation_info, out: *Relocation, co
10531053 var addend: i64 = mem.readIntLittle(i32, self.code.items[out.offset..][0..4]) + correction;
10541054
10551055 if (rel.r_extern == 0) {
1056 const source_sym = context.macho_file.locals.items[self.local_sym_index];
1057 const target_sym = switch (out.where) {
1058 .local => context.macho_file.locals.items[out.where_index],
1059 .undef => context.macho_file.undefs.items[out.where_index],
1060 };
1061 addend = @intCast(i64, source_sym.n_value + out.offset + 4) + addend - @intCast(i64, target_sym.n_value);
1056 const source_seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment;
1057 const source_sect_base_addr = source_seg.sections.items[rel.r_symbolnum - 1].addr;
1058 addend = @intCast(i64, out.offset) + addend - @intCast(i64, source_sect_base_addr) + 4 + correction;
10621059 }
10631060
10641061 out.payload = .{