| ... | ... | @@ -173,6 +173,12 @@ pub const TextBlock = struct { |
| 173 | 173 | self.tlv_offsets.deinit(); |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | pub fn resolveRelocs(self: *TextBlock, zld: *Zld) !void { |
| 177 | for (self.relocs.items) |rel| { |
| 178 | try rel.resolve(zld); |
| 179 | } |
| 180 | } |
| 181 | |
| 176 | 182 | pub fn print_this(self: *const TextBlock, zld: *Zld) void { |
| 177 | 183 | log.warn("TextBlock", .{}); |
| 178 | 184 | log.warn(" | {}: {}", .{ self.local_sym_index, zld.locals.items[self.local_sym_index] }); |
| ... | ... | @@ -328,11 +334,10 @@ pub fn link(self: *Zld, files: []const []const u8, output: Output, args: LinkArg |
| 328 | 334 | const sect = seg.sections.items[entry.key_ptr.sect]; |
| 329 | 335 | |
| 330 | 336 | log.warn("\n\n{s},{s} contents:", .{ segmentName(sect), sectionName(sect) }); |
| 331 | | log.warn("{}", .{sect}); |
| 337 | log.warn(" {}", .{sect}); |
| 332 | 338 | entry.value_ptr.*.print(self); |
| 333 | 339 | } |
| 334 | | return error.TODO; |
| 335 | | // try self.flush(); |
| 340 | try self.flush(); |
| 336 | 341 | } |
| 337 | 342 | |
| 338 | 343 | fn parseInputFiles(self: *Zld, files: []const []const u8, syslibroot: ?[]const u8) !void { |
| ... | ... | @@ -1041,6 +1046,8 @@ fn allocateSegment(self: *Zld, index: u16, offset: u64) !void { |
| 1041 | 1046 | } |
| 1042 | 1047 | |
| 1043 | 1048 | fn allocateTextBlocks(self: *Zld) !void { |
| 1049 | log.warn("allocating text blocks", .{}); |
| 1050 | |
| 1044 | 1051 | var it = self.blocks.iterator(); |
| 1045 | 1052 | while (it.next()) |entry| { |
| 1046 | 1053 | const match = entry.key_ptr.*; |
| ... | ... | @@ -1050,13 +1057,34 @@ fn allocateTextBlocks(self: *Zld) !void { |
| 1050 | 1057 | const sect = seg.sections.items[match.sect]; |
| 1051 | 1058 | var base_addr: u64 = sect.addr + sect.size; |
| 1052 | 1059 | |
| 1060 | log.warn(" within section {s},{s}", .{ segmentName(sect), sectionName(sect) }); |
| 1061 | log.warn(" {}", .{sect}); |
| 1062 | |
| 1053 | 1063 | while (true) { |
| 1054 | | base_addr -= block.size; |
| 1064 | const block_alignment = try math.powi(u32, 2, block.alignment); |
| 1065 | base_addr = mem.alignBackwardGeneric(u64, base_addr - block.size, block_alignment); |
| 1055 | 1066 | |
| 1056 | 1067 | const sym = self.locals.items[block.local_sym_index]; |
| 1057 | 1068 | assert(sym.payload == .regular); |
| 1058 | 1069 | sym.payload.regular.address = base_addr; |
| 1059 | 1070 | |
| 1071 | log.warn(" {s}: start=0x{x}, end=0x{x}, size={}, align={}", .{ |
| 1072 | sym.name, |
| 1073 | base_addr, |
| 1074 | base_addr + block.size, |
| 1075 | block.size, |
| 1076 | block.alignment, |
| 1077 | }); |
| 1078 | |
| 1079 | // Update each alias (if any) |
| 1080 | if (block.aliases) |aliases| { |
| 1081 | for (aliases) |index| { |
| 1082 | const alias_sym = self.locals.items[index]; |
| 1083 | assert(alias_sym.payload == .regular); |
| 1084 | alias_sym.payload.regular.address = base_addr; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1060 | 1088 | // Update each symbol contained within the TextBlock |
| 1061 | 1089 | if (block.contained) |contained| { |
| 1062 | 1090 | for (contained) |sym_at_off| { |
| ... | ... | @@ -1073,6 +1101,37 @@ fn allocateTextBlocks(self: *Zld) !void { |
| 1073 | 1101 | } |
| 1074 | 1102 | } |
| 1075 | 1103 | |
| 1104 | fn writeTextBlocks(self: *Zld) !void { |
| 1105 | var it = self.blocks.iterator(); |
| 1106 | while (it.next()) |entry| { |
| 1107 | const match = entry.key_ptr.*; |
| 1108 | var block: *TextBlock = entry.value_ptr.*; |
| 1109 | |
| 1110 | const seg = self.load_commands.items[match.seg].Segment; |
| 1111 | const sect = seg.sections.items[match.sect]; |
| 1112 | |
| 1113 | log.warn("writing text blocks for section {s},{s}", .{ segmentName(sect), sectionName(sect) }); |
| 1114 | |
| 1115 | var code = try self.allocator.alloc(u8, sect.size); |
| 1116 | defer self.allocator.free(code); |
| 1117 | |
| 1118 | var base_off: u64 = sect.size; |
| 1119 | |
| 1120 | while (true) { |
| 1121 | base_off -= block.size; |
| 1122 | |
| 1123 | try block.resolveRelocs(self); |
| 1124 | mem.copy(u8, code[base_off..][0..block.size], block.code); |
| 1125 | |
| 1126 | if (block.prev) |prev| { |
| 1127 | block = prev; |
| 1128 | } else break; |
| 1129 | } |
| 1130 | |
| 1131 | try self.file.?.pwriteAll(code, sect.offset); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1076 | 1135 | fn writeStubHelperCommon(self: *Zld) !void { |
| 1077 | 1136 | const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| 1078 | 1137 | const stub_helper = &text_segment.sections.items[self.stub_helper_section_index.?]; |
| ... | ... | @@ -1941,104 +2000,105 @@ fn addRpaths(self: *Zld, rpaths: []const []const u8) !void { |
| 1941 | 2000 | } |
| 1942 | 2001 | |
| 1943 | 2002 | fn flush(self: *Zld) !void { |
| 1944 | | try self.writeStubHelperCommon(); |
| 1945 | | |
| 1946 | | if (self.common_section_index) |index| { |
| 1947 | | const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 1948 | | const sect = &seg.sections.items[index]; |
| 1949 | | sect.offset = 0; |
| 1950 | | } |
| 1951 | | |
| 1952 | | if (self.bss_section_index) |index| { |
| 1953 | | const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 1954 | | const sect = &seg.sections.items[index]; |
| 1955 | | sect.offset = 0; |
| 1956 | | } |
| 1957 | | |
| 1958 | | if (self.tlv_bss_section_index) |index| { |
| 1959 | | const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 1960 | | const sect = &seg.sections.items[index]; |
| 1961 | | sect.offset = 0; |
| 1962 | | } |
| 1963 | | |
| 1964 | | if (self.tlv_section_index) |index| { |
| 1965 | | const seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 1966 | | const sect = &seg.sections.items[index]; |
| 1967 | | |
| 1968 | | var buffer = try self.allocator.alloc(u8, @intCast(usize, sect.size)); |
| 1969 | | defer self.allocator.free(buffer); |
| 1970 | | _ = try self.file.?.preadAll(buffer, sect.offset); |
| 1971 | | |
| 1972 | | var stream = std.io.fixedBufferStream(buffer); |
| 1973 | | var writer = stream.writer(); |
| 1974 | | |
| 1975 | | std.sort.sort(TlvOffset, self.threadlocal_offsets.items, {}, TlvOffset.cmp); |
| 1976 | | |
| 1977 | | const seek_amt = 2 * @sizeOf(u64); |
| 1978 | | for (self.threadlocal_offsets.items) |tlv| { |
| 1979 | | try writer.context.seekBy(seek_amt); |
| 1980 | | try writer.writeIntLittle(u64, tlv.offset); |
| 1981 | | } |
| 1982 | | |
| 1983 | | try self.file.?.pwriteAll(buffer, sect.offset); |
| 1984 | | } |
| 1985 | | |
| 1986 | | if (self.mod_init_func_section_index) |index| { |
| 1987 | | const seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; |
| 1988 | | const sect = &seg.sections.items[index]; |
| 1989 | | |
| 1990 | | var initializers = std.ArrayList(u64).init(self.allocator); |
| 1991 | | defer initializers.deinit(); |
| 1992 | | |
| 1993 | | for (self.objects.items) |object| { |
| 1994 | | for (object.initializers.items) |sym_id| { |
| 1995 | | const address = object.symbols.items[sym_id].payload.regular.address; |
| 1996 | | try initializers.append(address); |
| 1997 | | } |
| 1998 | | } |
| 1999 | | |
| 2000 | | _ = try self.file.?.pwriteAll(mem.sliceAsBytes(initializers.items), sect.offset); |
| 2001 | | sect.size = @intCast(u32, initializers.items.len * @sizeOf(u64)); |
| 2002 | | } |
| 2003 | | |
| 2004 | | try self.writeGotEntries(); |
| 2005 | | try self.setEntryPoint(); |
| 2006 | | try self.writeRebaseInfoTable(); |
| 2007 | | try self.writeBindInfoTable(); |
| 2008 | | try self.writeLazyBindInfoTable(); |
| 2009 | | try self.writeExportInfo(); |
| 2010 | | try self.writeDataInCode(); |
| 2011 | | |
| 2012 | | { |
| 2013 | | const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment; |
| 2014 | | const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab; |
| 2015 | | symtab.symoff = @intCast(u32, seg.inner.fileoff + seg.inner.filesize); |
| 2016 | | } |
| 2017 | | |
| 2018 | | try self.writeSymbolTable(); |
| 2019 | | try self.writeStringTable(); |
| 2020 | | |
| 2021 | | { |
| 2022 | | // Seal __LINKEDIT size |
| 2023 | | const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment; |
| 2024 | | seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size.?); |
| 2025 | | } |
| 2026 | | |
| 2027 | | if (self.target.?.cpu.arch == .aarch64) { |
| 2028 | | try self.writeCodeSignaturePadding(); |
| 2029 | | } |
| 2030 | | |
| 2031 | | try self.writeLoadCommands(); |
| 2032 | | try self.writeHeader(); |
| 2033 | | |
| 2034 | | if (self.target.?.cpu.arch == .aarch64) { |
| 2035 | | try self.writeCodeSignature(); |
| 2036 | | } |
| 2037 | | |
| 2038 | | if (comptime std.Target.current.isDarwin() and std.Target.current.cpu.arch == .aarch64) { |
| 2039 | | const out_path = self.output.?.path; |
| 2040 | | try fs.cwd().copyFile(out_path, fs.cwd(), out_path, .{}); |
| 2041 | | } |
| 2003 | try self.writeTextBlocks(); |
| 2004 | // try self.writeStubHelperCommon(); |
| 2005 | |
| 2006 | // if (self.common_section_index) |index| { |
| 2007 | // const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 2008 | // const sect = &seg.sections.items[index]; |
| 2009 | // sect.offset = 0; |
| 2010 | // } |
| 2011 | |
| 2012 | // if (self.bss_section_index) |index| { |
| 2013 | // const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 2014 | // const sect = &seg.sections.items[index]; |
| 2015 | // sect.offset = 0; |
| 2016 | // } |
| 2017 | |
| 2018 | // if (self.tlv_bss_section_index) |index| { |
| 2019 | // const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 2020 | // const sect = &seg.sections.items[index]; |
| 2021 | // sect.offset = 0; |
| 2022 | // } |
| 2023 | |
| 2024 | // if (self.tlv_section_index) |index| { |
| 2025 | // const seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 2026 | // const sect = &seg.sections.items[index]; |
| 2027 | |
| 2028 | // var buffer = try self.allocator.alloc(u8, @intCast(usize, sect.size)); |
| 2029 | // defer self.allocator.free(buffer); |
| 2030 | // _ = try self.file.?.preadAll(buffer, sect.offset); |
| 2031 | |
| 2032 | // var stream = std.io.fixedBufferStream(buffer); |
| 2033 | // var writer = stream.writer(); |
| 2034 | |
| 2035 | // std.sort.sort(TlvOffset, self.threadlocal_offsets.items, {}, TlvOffset.cmp); |
| 2036 | |
| 2037 | // const seek_amt = 2 * @sizeOf(u64); |
| 2038 | // for (self.threadlocal_offsets.items) |tlv| { |
| 2039 | // try writer.context.seekBy(seek_amt); |
| 2040 | // try writer.writeIntLittle(u64, tlv.offset); |
| 2041 | // } |
| 2042 | |
| 2043 | // try self.file.?.pwriteAll(buffer, sect.offset); |
| 2044 | // } |
| 2045 | |
| 2046 | // if (self.mod_init_func_section_index) |index| { |
| 2047 | // const seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; |
| 2048 | // const sect = &seg.sections.items[index]; |
| 2049 | |
| 2050 | // var initializers = std.ArrayList(u64).init(self.allocator); |
| 2051 | // defer initializers.deinit(); |
| 2052 | |
| 2053 | // for (self.objects.items) |object| { |
| 2054 | // for (object.initializers.items) |sym_id| { |
| 2055 | // const address = object.symbols.items[sym_id].payload.regular.address; |
| 2056 | // try initializers.append(address); |
| 2057 | // } |
| 2058 | // } |
| 2059 | |
| 2060 | // _ = try self.file.?.pwriteAll(mem.sliceAsBytes(initializers.items), sect.offset); |
| 2061 | // sect.size = @intCast(u32, initializers.items.len * @sizeOf(u64)); |
| 2062 | // } |
| 2063 | |
| 2064 | // try self.writeGotEntries(); |
| 2065 | // try self.setEntryPoint(); |
| 2066 | // try self.writeRebaseInfoTable(); |
| 2067 | // try self.writeBindInfoTable(); |
| 2068 | // try self.writeLazyBindInfoTable(); |
| 2069 | // try self.writeExportInfo(); |
| 2070 | // try self.writeDataInCode(); |
| 2071 | |
| 2072 | // { |
| 2073 | // const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment; |
| 2074 | // const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab; |
| 2075 | // symtab.symoff = @intCast(u32, seg.inner.fileoff + seg.inner.filesize); |
| 2076 | // } |
| 2077 | |
| 2078 | // try self.writeSymbolTable(); |
| 2079 | // try self.writeStringTable(); |
| 2080 | |
| 2081 | // { |
| 2082 | // // Seal __LINKEDIT size |
| 2083 | // const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment; |
| 2084 | // seg.inner.vmsize = mem.alignForwardGeneric(u64, seg.inner.filesize, self.page_size.?); |
| 2085 | // } |
| 2086 | |
| 2087 | // if (self.target.?.cpu.arch == .aarch64) { |
| 2088 | // try self.writeCodeSignaturePadding(); |
| 2089 | // } |
| 2090 | |
| 2091 | // try self.writeLoadCommands(); |
| 2092 | // try self.writeHeader(); |
| 2093 | |
| 2094 | // if (self.target.?.cpu.arch == .aarch64) { |
| 2095 | // try self.writeCodeSignature(); |
| 2096 | // } |
| 2097 | |
| 2098 | // if (comptime std.Target.current.isDarwin() and std.Target.current.cpu.arch == .aarch64) { |
| 2099 | // const out_path = self.output.?.path; |
| 2100 | // try fs.cwd().copyFile(out_path, fs.cwd(), out_path, .{}); |
| 2101 | // } |
| 2042 | 2102 | } |
| 2043 | 2103 | |
| 2044 | 2104 | fn writeGotEntries(self: *Zld) !void { |