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(...@@ -525,7 +525,7 @@ pub fn parseTextBlocks(
525 break :blk self.header.?.flags & macho.MH_SUBSECTIONS_VIA_SYMBOLS != 0;525 break :blk self.header.?.flags & macho.MH_SUBSECTIONS_VIA_SYMBOLS != 0;
526 };526 };
527527
528 macho_file.has_dices = blk: {528 macho_file.has_dices = macho_file.has_dices or blk: {
529 if (self.text_section_index) |index| {529 if (self.text_section_index) |index| {
530 if (index != id) break :blk false;530 if (index != id) break :blk false;
531 if (self.data_in_code_entries.items.len == 0) break :blk false;531 if (self.data_in_code_entries.items.len == 0) break :blk false;
...@@ -558,7 +558,7 @@ pub fn parseTextBlocks(...@@ -558,7 +558,7 @@ pub fn parseTextBlocks(
558 .n_type = macho.N_SECT,558 .n_type = macho.N_SECT,
559 .n_sect = @intCast(u8, macho_file.section_ordinals.getIndex(match).? + 1),559 .n_sect = @intCast(u8, macho_file.section_ordinals.getIndex(match).? + 1),
560 .n_desc = 0,560 .n_desc = 0,
561 .n_value = sect.addr,561 .n_value = 0,
562 });562 });
563 try self.sections_as_symbols.putNoClobber(allocator, sect_id, block_local_sym_index);563 try self.sections_as_symbols.putNoClobber(allocator, sect_id, block_local_sym_index);
564 break :blk block_local_sym_index;564 break :blk block_local_sym_index;
...@@ -660,7 +660,7 @@ pub fn parseTextBlocks(...@@ -660,7 +660,7 @@ pub fn parseTextBlocks(
660 .n_type = macho.N_SECT,660 .n_type = macho.N_SECT,
661 .n_sect = @intCast(u8, macho_file.section_ordinals.getIndex(match).? + 1),661 .n_sect = @intCast(u8, macho_file.section_ordinals.getIndex(match).? + 1),
662 .n_desc = 0,662 .n_desc = 0,
663 .n_value = sect.addr,663 .n_value = 0,
664 });664 });
665 try self.sections_as_symbols.putNoClobber(allocator, sect_id, block_local_sym_index);665 try self.sections_as_symbols.putNoClobber(allocator, sect_id, block_local_sym_index);
666 break :blk block_local_sym_index;666 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...@@ -646,7 +646,7 @@ fn initRelocFromObject(rel: macho.relocation_info, context: RelocContext) !Reloc
646 .n_type = macho.N_SECT,646 .n_type = macho.N_SECT,
647 .n_sect = @intCast(u8, context.macho_file.section_ordinals.getIndex(match).? + 1),647 .n_sect = @intCast(u8, context.macho_file.section_ordinals.getIndex(match).? + 1),
648 .n_desc = 0,648 .n_desc = 0,
649 .n_value = sect.addr,649 .n_value = 0,
650 });650 });
651 try context.object.sections_as_symbols.putNoClobber(context.allocator, sect_id, local_sym_index);651 try context.object.sections_as_symbols.putNoClobber(context.allocator, sect_id, local_sym_index);
652 break :blk local_sym_index;652 break :blk local_sym_index;
...@@ -956,9 +956,9 @@ fn parseUnsigned(...@@ -956,9 +956,9 @@ fn parseUnsigned(
956 mem.readIntLittle(i32, self.code.items[out.offset..][0..4]);956 mem.readIntLittle(i32, self.code.items[out.offset..][0..4]);
957957
958 if (rel.r_extern == 0) {958 if (rel.r_extern == 0) {
959 assert(out.where == .local);959 const source_seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment;
960 const target_sym = context.macho_file.locals.items[out.where_index];960 const source_sect_base_addr = source_seg.sections.items[rel.r_symbolnum - 1].addr;
961 addend -= @intCast(i64, target_sym.n_value);961 addend -= @intCast(i64, source_sect_base_addr);
962 }962 }
963963
964 out.payload = .{964 out.payload = .{
...@@ -1053,12 +1053,9 @@ fn parseSigned(self: TextBlock, rel: macho.relocation_info, out: *Relocation, co...@@ -1053,12 +1053,9 @@ fn parseSigned(self: TextBlock, rel: macho.relocation_info, out: *Relocation, co
1053 var addend: i64 = mem.readIntLittle(i32, self.code.items[out.offset..][0..4]) + correction;1053 var addend: i64 = mem.readIntLittle(i32, self.code.items[out.offset..][0..4]) + correction;
10541054
1055 if (rel.r_extern == 0) {1055 if (rel.r_extern == 0) {
1056 const source_sym = context.macho_file.locals.items[self.local_sym_index];1056 const source_seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment;
1057 const target_sym = switch (out.where) {1057 const source_sect_base_addr = source_seg.sections.items[rel.r_symbolnum - 1].addr;
1058 .local => context.macho_file.locals.items[out.where_index],1058 addend = @intCast(i64, out.offset) + addend - @intCast(i64, source_sect_base_addr) + 4 + correction;
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);
1062 }1059 }
10631060
1064 out.payload = .{1061 out.payload = .{