From 5e64d9745ba54d4dd61f8f98be4a3b7e6f2d8205 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 6 Sep 2021 10:38:51 +0200 Subject: [PATCH] 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. --- src/link/MachO/Object.zig | 6 +++--- src/link/MachO/TextBlock.zig | 17 +++++++---------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig index 72dbb05de9..a3f0c56065 100644 --- a/src/link/MachO/Object.zig +++ b/src/link/MachO/Object.zig @@ -525,7 +525,7 @@ pub fn parseTextBlocks( break :blk self.header.?.flags & macho.MH_SUBSECTIONS_VIA_SYMBOLS != 0; }; - macho_file.has_dices = blk: { + macho_file.has_dices = macho_file.has_dices or blk: { if (self.text_section_index) |index| { if (index != id) break :blk false; if (self.data_in_code_entries.items.len == 0) break :blk false; @@ -558,7 +558,7 @@ pub fn parseTextBlocks( .n_type = macho.N_SECT, .n_sect = @intCast(u8, macho_file.section_ordinals.getIndex(match).? + 1), .n_desc = 0, - .n_value = sect.addr, + .n_value = 0, }); try self.sections_as_symbols.putNoClobber(allocator, sect_id, block_local_sym_index); break :blk block_local_sym_index; @@ -660,7 +660,7 @@ pub fn parseTextBlocks( .n_type = macho.N_SECT, .n_sect = @intCast(u8, macho_file.section_ordinals.getIndex(match).? + 1), .n_desc = 0, - .n_value = sect.addr, + .n_value = 0, }); try self.sections_as_symbols.putNoClobber(allocator, sect_id, block_local_sym_index); break :blk block_local_sym_index; diff --git a/src/link/MachO/TextBlock.zig b/src/link/MachO/TextBlock.zig index 160ba5cd8c..3517e532a0 100644 --- a/src/link/MachO/TextBlock.zig +++ b/src/link/MachO/TextBlock.zig @@ -646,7 +646,7 @@ fn initRelocFromObject(rel: macho.relocation_info, context: RelocContext) !Reloc .n_type = macho.N_SECT, .n_sect = @intCast(u8, context.macho_file.section_ordinals.getIndex(match).? + 1), .n_desc = 0, - .n_value = sect.addr, + .n_value = 0, }); try context.object.sections_as_symbols.putNoClobber(context.allocator, sect_id, local_sym_index); break :blk local_sym_index; @@ -956,9 +956,9 @@ fn parseUnsigned( mem.readIntLittle(i32, self.code.items[out.offset..][0..4]); if (rel.r_extern == 0) { - assert(out.where == .local); - const target_sym = context.macho_file.locals.items[out.where_index]; - addend -= @intCast(i64, target_sym.n_value); + const source_seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment; + const source_sect_base_addr = source_seg.sections.items[rel.r_symbolnum - 1].addr; + addend -= @intCast(i64, source_sect_base_addr); } out.payload = .{ @@ -1053,12 +1053,9 @@ fn parseSigned(self: TextBlock, rel: macho.relocation_info, out: *Relocation, co var addend: i64 = mem.readIntLittle(i32, self.code.items[out.offset..][0..4]) + correction; if (rel.r_extern == 0) { - const source_sym = context.macho_file.locals.items[self.local_sym_index]; - const target_sym = switch (out.where) { - .local => context.macho_file.locals.items[out.where_index], - .undef => context.macho_file.undefs.items[out.where_index], - }; - addend = @intCast(i64, source_sym.n_value + out.offset + 4) + addend - @intCast(i64, target_sym.n_value); + const source_seg = context.object.load_commands.items[context.object.segment_cmd_index.?].Segment; + const source_sect_base_addr = source_seg.sections.items[rel.r_symbolnum - 1].addr; + addend = @intCast(i64, out.offset) + addend - @intCast(i64, source_sect_base_addr) + 4 + correction; } out.payload = .{