| ... | ... | @@ -77,6 +77,7 @@ lazy_imports: std.StringArrayHashMapUnmanaged(Import) = .{}, |
| 77 | 77 | tlv_bootstrap: ?Import = null, |
| 78 | 78 | threadlocal_offsets: std.ArrayListUnmanaged(u64) = .{}, |
| 79 | 79 | local_rebases: std.ArrayListUnmanaged(Pointer) = .{}, |
| 80 | nonlazy_pointers: std.StringArrayHashMapUnmanaged(GotEntry) = .{}, |
| 80 | 81 | |
| 81 | 82 | strtab: std.ArrayListUnmanaged(u8) = .{}, |
| 82 | 83 | |
| ... | ... | @@ -85,6 +86,16 @@ stub_helper_stubs_start_off: ?u64 = null, |
| 85 | 86 | mappings: std.AutoHashMapUnmanaged(MappingKey, SectionMapping) = .{}, |
| 86 | 87 | unhandled_sections: std.AutoHashMapUnmanaged(MappingKey, u0) = .{}, |
| 87 | 88 | |
| 89 | // TODO this will require scanning the relocations at least one to work out |
| 90 | // the exact amount of local GOT indirections. For the time being, set some |
| 91 | // default value. |
| 92 | const max_local_got_indirections: u16 = 1000; |
| 93 | |
| 94 | const GotEntry = struct { |
| 95 | index: u32, |
| 96 | target_addr: u64, |
| 97 | }; |
| 98 | |
| 88 | 99 | const MappingKey = struct { |
| 89 | 100 | object_id: u16, |
| 90 | 101 | source_sect_id: u16, |
| ... | ... | @@ -214,6 +225,10 @@ pub fn deinit(self: *Zld) void { |
| 214 | 225 | self.allocator.free(entry.key); |
| 215 | 226 | } |
| 216 | 227 | self.nonlazy_imports.deinit(self.allocator); |
| 228 | for (self.nonlazy_pointers.items()) |*entry| { |
| 229 | self.allocator.free(entry.key); |
| 230 | } |
| 231 | self.nonlazy_pointers.deinit(self.allocator); |
| 217 | 232 | for (self.exports.items()) |*entry| { |
| 218 | 233 | self.allocator.free(entry.key); |
| 219 | 234 | } |
| ... | ... | @@ -874,7 +889,10 @@ fn allocateDataConstSegment(self: *Zld) !void { |
| 874 | 889 | |
| 875 | 890 | // Set got size |
| 876 | 891 | const got = &seg.sections.items[self.got_section_index.?]; |
| 877 | | got.size += nonlazy * @sizeOf(u64); |
| 892 | // TODO this will require scanning the relocations at least one to work out |
| 893 | // the exact amount of local GOT indirections. For the time being, set some |
| 894 | // default value. |
| 895 | got.size += (max_local_got_indirections + nonlazy) * @sizeOf(u64); |
| 878 | 896 | |
| 879 | 897 | try self.allocateSegment(self.data_const_segment_cmd_index.?, 0); |
| 880 | 898 | } |
| ... | ... | @@ -1358,13 +1376,65 @@ fn doRelocs(self: *Zld) !void { |
| 1358 | 1376 | const rel_type = @intToEnum(macho.reloc_type_x86_64, rel.r_type); |
| 1359 | 1377 | |
| 1360 | 1378 | switch (rel_type) { |
| 1361 | | .X86_64_RELOC_BRANCH, |
| 1362 | | .X86_64_RELOC_GOT_LOAD, |
| 1363 | | .X86_64_RELOC_GOT, |
| 1364 | | => { |
| 1379 | .X86_64_RELOC_BRANCH => { |
| 1380 | assert(rel.r_length == 2); |
| 1381 | const inst = code[off..][0..4]; |
| 1382 | const displacement = @bitCast(u32, @intCast(i32, @intCast(i64, target_addr) - @intCast(i64, this_addr) - 4)); |
| 1383 | mem.writeIntLittle(u32, inst, displacement); |
| 1384 | }, |
| 1385 | .X86_64_RELOC_GOT_LOAD => { |
| 1365 | 1386 | assert(rel.r_length == 2); |
| 1366 | 1387 | const inst = code[off..][0..4]; |
| 1367 | 1388 | const displacement = @bitCast(u32, @intCast(i32, @intCast(i64, target_addr) - @intCast(i64, this_addr) - 4)); |
| 1389 | |
| 1390 | blk: { |
| 1391 | const data_const_seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; |
| 1392 | const got = data_const_seg.sections.items[self.got_section_index.?]; |
| 1393 | if (got.addr <= target_addr and target_addr < got.addr + got.size) break :blk; |
| 1394 | log.debug(" | rewriting to leaq", .{}); |
| 1395 | code[off - 2] = 0x8d; |
| 1396 | } |
| 1397 | |
| 1398 | mem.writeIntLittle(u32, inst, displacement); |
| 1399 | }, |
| 1400 | .X86_64_RELOC_GOT => { |
| 1401 | assert(rel.r_length == 2); |
| 1402 | // TODO Instead of referring to the target symbol directly, we refer to it |
| 1403 | // indirectly via GOT. Getting actual target address should be done in the |
| 1404 | // helper relocTargetAddr function rather than here. |
| 1405 | const sym = object.symtab.items[rel.r_symbolnum]; |
| 1406 | const sym_name = try self.allocator.dupe(u8, object.getString(sym.n_strx)); |
| 1407 | const res = try self.nonlazy_pointers.getOrPut(self.allocator, sym_name); |
| 1408 | defer if (res.found_existing) self.allocator.free(sym_name); |
| 1409 | |
| 1410 | const data_const_seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; |
| 1411 | const got = data_const_seg.sections.items[self.got_section_index.?]; |
| 1412 | |
| 1413 | if (!res.found_existing) { |
| 1414 | const index = @intCast(u32, self.nonlazy_pointers.items().len) - 1; |
| 1415 | assert(index < max_local_got_indirections); // TODO This is just a temp solution. |
| 1416 | res.entry.value = .{ |
| 1417 | .index = index, |
| 1418 | .target_addr = target_addr, |
| 1419 | }; |
| 1420 | var buf: [@sizeOf(u64)]u8 = undefined; |
| 1421 | mem.writeIntLittle(u64, &buf, target_addr); |
| 1422 | const got_offset = got.offset + (index + self.nonlazy_imports.items().len) * @sizeOf(u64); |
| 1423 | |
| 1424 | log.debug(" | GOT off 0x{x}", .{got.offset}); |
| 1425 | log.debug(" | writing GOT entry 0x{x} at 0x{x}", .{ target_addr, got_offset }); |
| 1426 | |
| 1427 | try self.file.?.pwriteAll(&buf, got_offset); |
| 1428 | } |
| 1429 | |
| 1430 | const index = res.entry.value.index + self.nonlazy_imports.items().len; |
| 1431 | const actual_target_addr = got.addr + index * @sizeOf(u64); |
| 1432 | |
| 1433 | log.debug(" | GOT addr 0x{x}", .{got.addr}); |
| 1434 | log.debug(" | actual target address in GOT 0x{x}", .{actual_target_addr}); |
| 1435 | |
| 1436 | const inst = code[off..][0..4]; |
| 1437 | const displacement = @bitCast(u32, @intCast(i32, @intCast(i64, actual_target_addr) - @intCast(i64, this_addr) - 4)); |
| 1368 | 1438 | mem.writeIntLittle(u32, inst, displacement); |
| 1369 | 1439 | }, |
| 1370 | 1440 | .X86_64_RELOC_TLV => { |
| ... | ... | @@ -2384,6 +2454,23 @@ fn writeRebaseInfoTable(self: *Zld) !void { |
| 2384 | 2454 | try pointers.ensureCapacity(pointers.items.len + self.local_rebases.items.len); |
| 2385 | 2455 | pointers.appendSliceAssumeCapacity(self.local_rebases.items); |
| 2386 | 2456 | |
| 2457 | if (self.got_section_index) |idx| { |
| 2458 | // TODO this should be cleaned up! |
| 2459 | try pointers.ensureCapacity(pointers.items.len + self.nonlazy_pointers.items().len); |
| 2460 | const seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; |
| 2461 | const sect = seg.sections.items[idx]; |
| 2462 | const base_offset = sect.addr - seg.inner.vmaddr; |
| 2463 | const segment_id = @intCast(u16, self.data_const_segment_cmd_index.?); |
| 2464 | const index_offset = @intCast(u32, self.nonlazy_imports.items().len); |
| 2465 | for (self.nonlazy_pointers.items()) |entry| { |
| 2466 | const index = index_offset + entry.value.index; |
| 2467 | pointers.appendAssumeCapacity(.{ |
| 2468 | .offset = base_offset + index * @sizeOf(u64), |
| 2469 | .segment_id = segment_id, |
| 2470 | }); |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2387 | 2474 | if (self.la_symbol_ptr_section_index) |idx| { |
| 2388 | 2475 | try pointers.ensureCapacity(pointers.items.len + self.lazy_imports.items().len); |
| 2389 | 2476 | const seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| ... | ... | @@ -2851,8 +2938,9 @@ fn writeDynamicSymbolTable(self: *Zld) !void { |
| 2851 | 2938 | |
| 2852 | 2939 | const lazy = self.lazy_imports.items(); |
| 2853 | 2940 | const nonlazy = self.nonlazy_imports.items(); |
| 2941 | const got_locals = self.nonlazy_pointers.items(); |
| 2854 | 2942 | dysymtab.indirectsymoff = @intCast(u32, seg.inner.fileoff + seg.inner.filesize); |
| 2855 | | dysymtab.nindirectsyms = @intCast(u32, lazy.len * 2 + nonlazy.len); |
| 2943 | dysymtab.nindirectsyms = @intCast(u32, lazy.len * 2 + nonlazy.len + got_locals.len); |
| 2856 | 2944 | const needed_size = dysymtab.nindirectsyms * @sizeOf(u32); |
| 2857 | 2945 | seg.inner.filesize += needed_size; |
| 2858 | 2946 | |
| ... | ... | @@ -2867,20 +2955,24 @@ fn writeDynamicSymbolTable(self: *Zld) !void { |
| 2867 | 2955 | var writer = stream.writer(); |
| 2868 | 2956 | |
| 2869 | 2957 | stubs.reserved1 = 0; |
| 2870 | | for (self.lazy_imports.items()) |_, i| { |
| 2958 | for (lazy) |_, i| { |
| 2871 | 2959 | const symtab_idx = @intCast(u32, dysymtab.iundefsym + i); |
| 2872 | 2960 | try writer.writeIntLittle(u32, symtab_idx); |
| 2873 | 2961 | } |
| 2874 | 2962 | |
| 2875 | 2963 | const base_id = @intCast(u32, lazy.len); |
| 2876 | 2964 | got.reserved1 = base_id; |
| 2877 | | for (self.nonlazy_imports.items()) |_, i| { |
| 2965 | for (nonlazy) |_, i| { |
| 2878 | 2966 | const symtab_idx = @intCast(u32, dysymtab.iundefsym + i + base_id); |
| 2879 | 2967 | try writer.writeIntLittle(u32, symtab_idx); |
| 2880 | 2968 | } |
| 2969 | // TODO there should be one common set of GOT entries. |
| 2970 | for (got_locals) |_| { |
| 2971 | try writer.writeIntLittle(u32, macho.INDIRECT_SYMBOL_LOCAL); |
| 2972 | } |
| 2881 | 2973 | |
| 2882 | | la_symbol_ptr.reserved1 = got.reserved1 + @intCast(u32, nonlazy.len); |
| 2883 | | for (self.lazy_imports.items()) |_, i| { |
| 2974 | la_symbol_ptr.reserved1 = got.reserved1 + @intCast(u32, nonlazy.len) + @intCast(u32, got_locals.len); |
| 2975 | for (lazy) |_, i| { |
| 2884 | 2976 | const symtab_idx = @intCast(u32, dysymtab.iundefsym + i); |
| 2885 | 2977 | try writer.writeIntLittle(u32, symtab_idx); |
| 2886 | 2978 | } |