| author | |
| committer | |
| log | 897a554109baa3288d575cac0833e10edd1a316c |
| tree | 3d3560bf0ea0b2a4943c27b8e38aa45a8f0fce37 |
| parent | 80cafad9d32fed9f6a786f4d87f40b8ee622015e |
6 files changed, 323 insertions(+), 66 deletions(-)
src/link/MachO.zig+3-1| ... | @@ -606,7 +606,9 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node | ... | @@ -606,7 +606,9 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node |
| 606 | self.allocateSyntheticSymbols(); | 606 | self.allocateSyntheticSymbols(); |
| 607 | try self.allocateLinkeditSegment(); | 607 | try self.allocateLinkeditSegment(); |
| 608 | 608 | ||
| 609 | state_log.debug("{}", .{self.dumpState()}); | 609 | if (build_options.enable_logging) { |
| 610 | state_log.debug("{}", .{self.dumpState()}); | ||
| 611 | } | ||
| 610 | 612 | ||
| 611 | try self.initDyldInfoSections(); | 613 | try self.initDyldInfoSections(); |
| 612 | 614 |
src/link/MachO/Archive.zig+228-61| ... | @@ -1,63 +1,5 @@ | ... | @@ -1,63 +1,5 @@ |
| 1 | objects: std.ArrayListUnmanaged(Object) = .{}, | 1 | objects: std.ArrayListUnmanaged(Object) = .{}, |
| 2 | 2 | ||
| 3 | // Archive files start with the ARMAG identifying string. Then follows a | ||
| 4 | // `struct ar_hdr', and as many bytes of member file data as its `ar_size' | ||
| 5 | // member indicates, for each member file. | ||
| 6 | /// String that begins an archive file. | ||
| 7 | pub const ARMAG: *const [SARMAG:0]u8 = "!<arch>\n"; | ||
| 8 | /// Size of that string. | ||
| 9 | pub const SARMAG: u4 = 8; | ||
| 10 | |||
| 11 | /// String in ar_fmag at the end of each header. | ||
| 12 | const ARFMAG: *const [2:0]u8 = "`\n"; | ||
| 13 | |||
| 14 | const ar_hdr = extern struct { | ||
| 15 | /// Member file name, sometimes / terminated. | ||
| 16 | ar_name: [16]u8, | ||
| 17 | |||
| 18 | /// File date, decimal seconds since Epoch. | ||
| 19 | ar_date: [12]u8, | ||
| 20 | |||
| 21 | /// User ID, in ASCII format. | ||
| 22 | ar_uid: [6]u8, | ||
| 23 | |||
| 24 | /// Group ID, in ASCII format. | ||
| 25 | ar_gid: [6]u8, | ||
| 26 | |||
| 27 | /// File mode, in ASCII octal. | ||
| 28 | ar_mode: [8]u8, | ||
| 29 | |||
| 30 | /// File size, in ASCII decimal. | ||
| 31 | ar_size: [10]u8, | ||
| 32 | |||
| 33 | /// Always contains ARFMAG. | ||
| 34 | ar_fmag: [2]u8, | ||
| 35 | |||
| 36 | fn date(self: ar_hdr) !u64 { | ||
| 37 | const value = mem.trimRight(u8, &self.ar_date, &[_]u8{@as(u8, 0x20)}); | ||
| 38 | return std.fmt.parseInt(u64, value, 10); | ||
| 39 | } | ||
| 40 | |||
| 41 | fn size(self: ar_hdr) !u32 { | ||
| 42 | const value = mem.trimRight(u8, &self.ar_size, &[_]u8{@as(u8, 0x20)}); | ||
| 43 | return std.fmt.parseInt(u32, value, 10); | ||
| 44 | } | ||
| 45 | |||
| 46 | fn name(self: *const ar_hdr) ?[]const u8 { | ||
| 47 | const value = &self.ar_name; | ||
| 48 | if (mem.startsWith(u8, value, "#1/")) return null; | ||
| 49 | const sentinel = mem.indexOfScalar(u8, value, '/') orelse value.len; | ||
| 50 | return value[0..sentinel]; | ||
| 51 | } | ||
| 52 | |||
| 53 | fn nameLength(self: ar_hdr) !?u32 { | ||
| 54 | const value = &self.ar_name; | ||
| 55 | if (!mem.startsWith(u8, value, "#1/")) return null; | ||
| 56 | const trimmed = mem.trimRight(u8, self.ar_name["#1/".len..], &[_]u8{0x20}); | ||
| 57 | return try std.fmt.parseInt(u32, trimmed, 10); | ||
| 58 | } | ||
| 59 | }; | ||
| 60 | |||
| 61 | pub fn isArchive(path: []const u8, fat_arch: ?fat.Arch) !bool { | 3 | pub fn isArchive(path: []const u8, fat_arch: ?fat.Arch) !bool { |
| 62 | const file = try std.fs.cwd().openFile(path, .{}); | 4 | const file = try std.fs.cwd().openFile(path, .{}); |
| 63 | defer file.close(); | 5 | defer file.close(); |
| ... | @@ -85,9 +27,9 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: | ... | @@ -85,9 +27,9 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: |
| 85 | try handle.seekTo(offset); | 27 | try handle.seekTo(offset); |
| 86 | 28 | ||
| 87 | const reader = handle.reader(); | 29 | const reader = handle.reader(); |
| 88 | _ = try reader.readBytesNoEof(Archive.SARMAG); | 30 | _ = try reader.readBytesNoEof(SARMAG); |
| 89 | 31 | ||
| 90 | var pos: usize = Archive.SARMAG; | 32 | var pos: usize = SARMAG; |
| 91 | while (true) { | 33 | while (true) { |
| 92 | if (pos >= size) break; | 34 | if (pos >= size) break; |
| 93 | if (!mem.isAligned(pos, 2)) { | 35 | if (!mem.isAligned(pos, 2)) { |
| ... | @@ -123,7 +65,10 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: | ... | @@ -123,7 +65,10 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: |
| 123 | pos += hdr_size; | 65 | pos += hdr_size; |
| 124 | } | 66 | } |
| 125 | 67 | ||
| 126 | if (mem.eql(u8, name, "__.SYMDEF") or mem.eql(u8, name, "__.SYMDEF SORTED")) continue; | 68 | if (mem.eql(u8, name, SYMDEF) or |
| 69 | mem.eql(u8, name, SYMDEF64) or | ||
| 70 | mem.eql(u8, name, SYMDEF_SORTED) or | ||
| 71 | mem.eql(u8, name, SYMDEF64_SORTED)) continue; | ||
| 127 | 72 | ||
| 128 | const object = Object{ | 73 | const object = Object{ |
| 129 | .archive = .{ | 74 | .archive = .{ |
| ... | @@ -143,6 +88,227 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: | ... | @@ -143,6 +88,227 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: |
| 143 | } | 88 | } |
| 144 | } | 89 | } |
| 145 | 90 | ||
| 91 | pub fn writeHeader( | ||
| 92 | object_name: []const u8, | ||
| 93 | object_size: u32, | ||
| 94 | format: Format, | ||
| 95 | writer: anytype, | ||
| 96 | ) !void { | ||
| 97 | var hdr: ar_hdr = .{ | ||
| 98 | .ar_name = undefined, | ||
| 99 | .ar_date = undefined, | ||
| 100 | .ar_uid = undefined, | ||
| 101 | .ar_gid = undefined, | ||
| 102 | .ar_mode = undefined, | ||
| 103 | .ar_size = undefined, | ||
| 104 | .ar_fmag = undefined, | ||
| 105 | }; | ||
| 106 | @memset(mem.asBytes(&hdr), 0x20); | ||
| 107 | inline for (@typeInfo(ar_hdr).Struct.fields) |field| { | ||
| 108 | var stream = std.io.fixedBufferStream(&@field(hdr, field.name)); | ||
| 109 | stream.writer().print("0", .{}) catch unreachable; | ||
| 110 | } | ||
| 111 | @memcpy(&hdr.ar_fmag, ARFMAG); | ||
| 112 | |||
| 113 | const object_name_len = mem.alignForward(u32, object_name.len + 1, format.ptrWidth()); | ||
| 114 | const total_object_size = object_size + object_name_len; | ||
| 115 | |||
| 116 | { | ||
| 117 | var stream = std.io.fixedBufferStream(&hdr.ar_name); | ||
| 118 | stream.writer().print("#1/{d}", .{object_name_len}) catch unreachable; | ||
| 119 | } | ||
| 120 | { | ||
| 121 | var stream = std.io.fixedBufferStream(&hdr.ar_size); | ||
| 122 | stream.writer().print("{d}", .{total_object_size}) catch unreachable; | ||
| 123 | } | ||
| 124 | |||
| 125 | try writer.writeAll(mem.asBytes(&hdr)); | ||
| 126 | try writer.print("{s}\x00", .{object_name}); | ||
| 127 | |||
| 128 | const padding = object_name_len - object_name.len - 1; | ||
| 129 | if (padding > 0) { | ||
| 130 | try writer.writeByteNTimes(0, padding); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | // Archive files start with the ARMAG identifying string. Then follows a | ||
| 135 | // `struct ar_hdr', and as many bytes of member file data as its `ar_size' | ||
| 136 | // member indicates, for each member file. | ||
| 137 | /// String that begins an archive file. | ||
| 138 | pub const ARMAG: *const [SARMAG:0]u8 = "!<arch>\n"; | ||
| 139 | /// Size of that string. | ||
| 140 | pub const SARMAG: u4 = 8; | ||
| 141 | |||
| 142 | /// String in ar_fmag at the end of each header. | ||
| 143 | const ARFMAG: *const [2:0]u8 = "`\n"; | ||
| 144 | |||
| 145 | const SYMDEF = "__.SYMDEF"; | ||
| 146 | const SYMDEF64 = "__.SYMDEF_64"; | ||
| 147 | const SYMDEF_SORTED = "__.SYMDEF SORTED"; | ||
| 148 | const SYMDEF64_SORTED = "__.SYMDEF_64 SORTED"; | ||
| 149 | |||
| 150 | const ar_hdr = extern struct { | ||
| 151 | /// Member file name, sometimes / terminated. | ||
| 152 | ar_name: [16]u8, | ||
| 153 | |||
| 154 | /// File date, decimal seconds since Epoch. | ||
| 155 | ar_date: [12]u8, | ||
| 156 | |||
| 157 | /// User ID, in ASCII format. | ||
| 158 | ar_uid: [6]u8, | ||
| 159 | |||
| 160 | /// Group ID, in ASCII format. | ||
| 161 | ar_gid: [6]u8, | ||
| 162 | |||
| 163 | /// File mode, in ASCII octal. | ||
| 164 | ar_mode: [8]u8, | ||
| 165 | |||
| 166 | /// File size, in ASCII decimal. | ||
| 167 | ar_size: [10]u8, | ||
| 168 | |||
| 169 | /// Always contains ARFMAG. | ||
| 170 | ar_fmag: [2]u8, | ||
| 171 | |||
| 172 | fn date(self: ar_hdr) !u64 { | ||
| 173 | const value = mem.trimRight(u8, &self.ar_date, &[_]u8{@as(u8, 0x20)}); | ||
| 174 | return std.fmt.parseInt(u64, value, 10); | ||
| 175 | } | ||
| 176 | |||
| 177 | fn size(self: ar_hdr) !u32 { | ||
| 178 | const value = mem.trimRight(u8, &self.ar_size, &[_]u8{@as(u8, 0x20)}); | ||
| 179 | return std.fmt.parseInt(u32, value, 10); | ||
| 180 | } | ||
| 181 | |||
| 182 | fn name(self: *const ar_hdr) ?[]const u8 { | ||
| 183 | const value = &self.ar_name; | ||
| 184 | if (mem.startsWith(u8, value, "#1/")) return null; | ||
| 185 | const sentinel = mem.indexOfScalar(u8, value, '/') orelse value.len; | ||
| 186 | return value[0..sentinel]; | ||
| 187 | } | ||
| 188 | |||
| 189 | fn nameLength(self: ar_hdr) !?u32 { | ||
| 190 | const value = &self.ar_name; | ||
| 191 | if (!mem.startsWith(u8, value, "#1/")) return null; | ||
| 192 | const trimmed = mem.trimRight(u8, self.ar_name["#1/".len..], &[_]u8{0x20}); | ||
| 193 | return try std.fmt.parseInt(u32, trimmed, 10); | ||
| 194 | } | ||
| 195 | }; | ||
| 196 | |||
| 197 | pub const ArSymtab = struct { | ||
| 198 | entries: std.ArrayListUnmanaged(Entry) = .{}, | ||
| 199 | strtab: StringTable = .{}, | ||
| 200 | format: Format = .p32, | ||
| 201 | |||
| 202 | pub fn deinit(ar: *ArSymtab, allocator: Allocator) void { | ||
| 203 | ar.entries.deinit(allocator); | ||
| 204 | ar.strtab.deinit(allocator); | ||
| 205 | } | ||
| 206 | |||
| 207 | pub fn sort(ar: *ArSymtab) void { | ||
| 208 | mem.sort(Entry, ar.entries.items, {}, Entry.lessThan); | ||
| 209 | } | ||
| 210 | |||
| 211 | pub fn size(ar: ArSymtab) usize { | ||
| 212 | const ptr_width = ar.format.ptrWidth(); | ||
| 213 | return ptr_width + ar.entries.items.len * 2 * ptr_width + ptr_width + mem.alignForward(usize, ar.strtab.buffer.items.len, ptr_width); | ||
| 214 | } | ||
| 215 | |||
| 216 | pub fn write(ar: ArSymtab, macho_file: *MachO, writer: anytype) !void { | ||
| 217 | // Header | ||
| 218 | try writeHeader(SYMDEF, ar.size()); | ||
| 219 | // Symtab size | ||
| 220 | try ar.writeInt(ar.entries.items.len * 2); | ||
| 221 | // Symtab entries | ||
| 222 | for (ar.entries.items) |entry| { | ||
| 223 | const file_off = switch (macho_file.getFile(entry.file).?) { | ||
| 224 | .zig_object => |x| x.output_ar_state.file_off, | ||
| 225 | .object => |x| x.output_ar_state.file_off, | ||
| 226 | else => unreachable, | ||
| 227 | }; | ||
| 228 | // Name offset | ||
| 229 | try ar.writeInt(entry.off); | ||
| 230 | // File offset | ||
| 231 | try ar.writeInt(file_off); | ||
| 232 | } | ||
| 233 | // Strtab size | ||
| 234 | const strtab_size = mem.alignForward(u64, ar.strtab.buffer.items.len, ar.format.ptrWidth()); | ||
| 235 | const padding = strtab_size - ar.strtab.buffer.items.len; | ||
| 236 | try ar.writeInt(strtab_size); | ||
| 237 | // Strtab | ||
| 238 | try writer.writeAll(ar.strtab.buffer.items); | ||
| 239 | if (padding > 0) { | ||
| 240 | try writer.writeByteNTimes(0, padding); | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | fn writeInt(ar: ArSymtab, value: u64, writer: anytype) !void { | ||
| 245 | switch (ar.format) { | ||
| 246 | .p32 => try writer.writeInt(u32, std.math.cast(u32, value) orelse return error.Overflow, .little), | ||
| 247 | .p64 => try writer.writeInt(u64, value, .little), | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | const FormatContext = struct { | ||
| 252 | ar: ArSymtab, | ||
| 253 | macho_file: *MachO, | ||
| 254 | }; | ||
| 255 | |||
| 256 | pub fn fmt(ar: ArSymtab, macho_file: *MachO) std.fmt.Formatter(format2) { | ||
| 257 | return .{ .data = .{ .ar = ar, .macho_file = macho_file } }; | ||
| 258 | } | ||
| 259 | |||
| 260 | fn format2( | ||
| 261 | ctx: FormatContext, | ||
| 262 | comptime unused_fmt_string: []const u8, | ||
| 263 | options: std.fmt.FormatOptions, | ||
| 264 | writer: anytype, | ||
| 265 | ) !void { | ||
| 266 | _ = unused_fmt_string; | ||
| 267 | _ = options; | ||
| 268 | const ar = ctx.ar; | ||
| 269 | const macho_file = ctx.macho_file; | ||
| 270 | for (ar.entries.items, 0..) |entry, i| { | ||
| 271 | const name = ar.strtab.getAssumeExists(entry.off); | ||
| 272 | const file = macho_file.getFile(entry.file).?; | ||
| 273 | try writer.print(" {d}: {s} in file({d})({})\n", .{ i, name, entry.file, file.fmtPath() }); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | const Entry = struct { | ||
| 278 | /// Symbol name offset | ||
| 279 | off: u32, | ||
| 280 | /// Exporting file | ||
| 281 | file: File.Index, | ||
| 282 | |||
| 283 | pub fn lessThan(ctx: void, lhs: Entry, rhs: Entry) bool { | ||
| 284 | _ = ctx; | ||
| 285 | if (lhs.off == rhs.off) return lhs.file < rhs.file; | ||
| 286 | return lhs.off < rhs.off; | ||
| 287 | } | ||
| 288 | }; | ||
| 289 | }; | ||
| 290 | |||
| 291 | const Format = enum { | ||
| 292 | p32, | ||
| 293 | p64, | ||
| 294 | |||
| 295 | fn ptrWidth(self: Format) usize { | ||
| 296 | return switch (self) { | ||
| 297 | .p32 => @as(usize, 4), | ||
| 298 | .p64 => 8, | ||
| 299 | }; | ||
| 300 | } | ||
| 301 | }; | ||
| 302 | |||
| 303 | pub const ArState = struct { | ||
| 304 | /// File offset of the ar_hdr describing the contributing | ||
| 305 | /// object in the archive. | ||
| 306 | file_off: u64 = 0, | ||
| 307 | |||
| 308 | /// Total size of the contributing object (excludes ar_hdr and long name with padding). | ||
| 309 | size: u64 = 0, | ||
| 310 | }; | ||
| 311 | |||
| 146 | const fat = @import("fat.zig"); | 312 | const fat = @import("fat.zig"); |
| 147 | const link = @import("../../link.zig"); | 313 | const link = @import("../../link.zig"); |
| 148 | const log = std.log.scoped(.link); | 314 | const log = std.log.scoped(.link); |
| ... | @@ -155,3 +321,4 @@ const Archive = @This(); | ... | @@ -155,3 +321,4 @@ const Archive = @This(); |
| 155 | const File = @import("file.zig").File; | 321 | const File = @import("file.zig").File; |
| 156 | const MachO = @import("../MachO.zig"); | 322 | const MachO = @import("../MachO.zig"); |
| 157 | const Object = @import("Object.zig"); | 323 | const Object = @import("Object.zig"); |
| 324 | const StringTable = @import("../StringTable.zig"); |
src/link/MachO/Object.zig+31-2| ... | @@ -1,4 +1,4 @@ | ... | @@ -1,4 +1,4 @@ |
| 1 | archive: ?Archive = null, | 1 | archive: ?InArchive = null, |
| 2 | path: []const u8, | 2 | path: []const u8, |
| 3 | file_handle: File.HandleIndex, | 3 | file_handle: File.HandleIndex, |
| 4 | mtime: u64, | 4 | mtime: u64, |
| ... | @@ -29,8 +29,9 @@ hidden: bool = false, | ... | @@ -29,8 +29,9 @@ hidden: bool = false, |
| 29 | 29 | ||
| 30 | dynamic_relocs: MachO.DynamicRelocs = .{}, | 30 | dynamic_relocs: MachO.DynamicRelocs = .{}, |
| 31 | output_symtab_ctx: MachO.SymtabCtx = .{}, | 31 | output_symtab_ctx: MachO.SymtabCtx = .{}, |
| 32 | output_ar_state: Archive.ArState = .{}, | ||
| 32 | 33 | ||
| 33 | const Archive = struct { | 34 | const InArchive = struct { |
| 34 | path: []const u8, | 35 | path: []const u8, |
| 35 | offset: u64, | 36 | offset: u64, |
| 36 | }; | 37 | }; |
| ... | @@ -1232,6 +1233,33 @@ fn addSection(self: *Object, allocator: Allocator, segname: []const u8, sectname | ... | @@ -1232,6 +1233,33 @@ fn addSection(self: *Object, allocator: Allocator, segname: []const u8, sectname |
| 1232 | return n_sect; | 1233 | return n_sect; |
| 1233 | } | 1234 | } |
| 1234 | 1235 | ||
| 1236 | pub fn updateArSymtab(self: Object, ar_symtab: *Archive.ArSymtab, macho_file: *MachO) error{OutOfMemory}!void { | ||
| 1237 | const gpa = macho_file.base.comp.gpa; | ||
| 1238 | for (self.symtab.items(.nlist)) |nlist| { | ||
| 1239 | if (!nlist.ext() or (nlist.undf() and !nlist.tentative())) continue; | ||
| 1240 | const off = try ar_symtab.strtab.insert(gpa, self.getString(nlist.n_strx)); | ||
| 1241 | try ar_symtab.entries.append(gpa, .{ .off = off, .file = self.index }); | ||
| 1242 | } | ||
| 1243 | } | ||
| 1244 | |||
| 1245 | pub fn updateArSize(self: *Object, macho_file: *MachO) !void { | ||
| 1246 | const file = macho_file.getFileHandle(self.file_handle); | ||
| 1247 | const size = (try file.stat()).size; | ||
| 1248 | self.output_ar_state.size = size; | ||
| 1249 | } | ||
| 1250 | |||
| 1251 | pub fn writeAr(self: Object, macho_file: *MachO, writer: anytype) !void { | ||
| 1252 | // Header | ||
| 1253 | try Archive.writeHeader(self.path, self.output_ar_state.size, writer); | ||
| 1254 | // Data | ||
| 1255 | const file = macho_file.getFileHandle(self.file_handle); | ||
| 1256 | // TODO try using copyRangeAll | ||
| 1257 | const gpa = macho_file.base.comp.gpa; | ||
| 1258 | const data = try file.readToEndAlloc(gpa, self.output_ar_state.size); | ||
| 1259 | defer gpa.free(data); | ||
| 1260 | try writer.writeAll(data); | ||
| 1261 | } | ||
| 1262 | |||
| 1235 | pub fn calcSymtabSize(self: *Object, macho_file: *MachO) !void { | 1263 | pub fn calcSymtabSize(self: *Object, macho_file: *MachO) !void { |
| 1236 | const tracy = trace(@src()); | 1264 | const tracy = trace(@src()); |
| 1237 | defer tracy.end(); | 1265 | defer tracy.end(); |
| ... | @@ -2241,6 +2269,7 @@ const trace = @import("../../tracy.zig").trace; | ... | @@ -2241,6 +2269,7 @@ const trace = @import("../../tracy.zig").trace; |
| 2241 | const std = @import("std"); | 2269 | const std = @import("std"); |
| 2242 | 2270 | ||
| 2243 | const Allocator = mem.Allocator; | 2271 | const Allocator = mem.Allocator; |
| 2272 | const Archive = @import("Archive.zig"); | ||
| 2244 | const Atom = @import("Atom.zig"); | 2273 | const Atom = @import("Atom.zig"); |
| 2245 | const Cie = eh_frame.Cie; | 2274 | const Cie = eh_frame.Cie; |
| 2246 | const DwarfInfo = @import("DwarfInfo.zig"); | 2275 | const DwarfInfo = @import("DwarfInfo.zig"); |
src/link/MachO/ZigObject.zig+24| ... | @@ -48,6 +48,7 @@ relocs: RelocationTable = .{}, | ... | @@ -48,6 +48,7 @@ relocs: RelocationTable = .{}, |
| 48 | 48 | ||
| 49 | dynamic_relocs: MachO.DynamicRelocs = .{}, | 49 | dynamic_relocs: MachO.DynamicRelocs = .{}, |
| 50 | output_symtab_ctx: MachO.SymtabCtx = .{}, | 50 | output_symtab_ctx: MachO.SymtabCtx = .{}, |
| 51 | output_ar_state: Archive.ArState = .{}, | ||
| 51 | 52 | ||
| 52 | pub fn init(self: *ZigObject, macho_file: *MachO) !void { | 53 | pub fn init(self: *ZigObject, macho_file: *MachO) !void { |
| 53 | const comp = macho_file.base.comp; | 54 | const comp = macho_file.base.comp; |
| ... | @@ -297,6 +298,29 @@ pub fn readFileContents(self: *ZigObject, macho_file: *MachO) !void { | ... | @@ -297,6 +298,29 @@ pub fn readFileContents(self: *ZigObject, macho_file: *MachO) !void { |
| 297 | if (amt != size) return error.InputOutput; | 298 | if (amt != size) return error.InputOutput; |
| 298 | } | 299 | } |
| 299 | 300 | ||
| 301 | pub fn updateArSymtab(self: ZigObject, ar_symtab: *Archive.ArSymtab, macho_file: *MachO) error{OutOfMemory}!void { | ||
| 302 | const gpa = macho_file.base.comp.gpa; | ||
| 303 | for (self.symbols.items) |sym_index| { | ||
| 304 | const sym = macho_file.getSymbol(sym_index); | ||
| 305 | const file = sym.getFile(macho_file).?; | ||
| 306 | assert(file.getIndex() == self.index); | ||
| 307 | if (!sym.flags.@"export") continue; | ||
| 308 | const off = try ar_symtab.strtab.insert(gpa, sym.getName(macho_file)); | ||
| 309 | try ar_symtab.entries.append(gpa, .{ .off = off, .file = self.index }); | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | pub fn updateArSize(self: *ZigObject) void { | ||
| 314 | self.output_ar_state.size = self.data.items.len; | ||
| 315 | } | ||
| 316 | |||
| 317 | pub fn writeAr(self: ZigObject, writer: anytype) !void { | ||
| 318 | // Header | ||
| 319 | try Archive.writeHeader(self.path, self.output_ar_state.size, writer); | ||
| 320 | // Data | ||
| 321 | try writer.writeAll(self.data.items); | ||
| 322 | } | ||
| 323 | |||
| 300 | pub fn scanRelocs(self: *ZigObject, macho_file: *MachO) !void { | 324 | pub fn scanRelocs(self: *ZigObject, macho_file: *MachO) !void { |
| 301 | for (self.atoms.items) |atom_index| { | 325 | for (self.atoms.items) |atom_index| { |
| 302 | const atom = macho_file.getAtom(atom_index) orelse continue; | 326 | const atom = macho_file.getAtom(atom_index) orelse continue; |
src/link/MachO/file.zig+8| ... | @@ -175,6 +175,13 @@ pub const File = union(enum) { | ... | @@ -175,6 +175,13 @@ pub const File = union(enum) { |
| 175 | }; | 175 | }; |
| 176 | } | 176 | } |
| 177 | 177 | ||
| 178 | pub fn updateArSymtab(file: File, ar_symtab: *Archive.ArSymtab, macho_file: *MachO) error{OutOfMemory}!void { | ||
| 179 | return switch (file) { | ||
| 180 | .dylib, .internal => unreachable, | ||
| 181 | inline else => |x| x.updateArSymtab(ar_symtab, macho_file), | ||
| 182 | }; | ||
| 183 | } | ||
| 184 | |||
| 178 | pub fn calcSymtabSize(file: File, macho_file: *MachO) !void { | 185 | pub fn calcSymtabSize(file: File, macho_file: *MachO) !void { |
| 179 | return switch (file) { | 186 | return switch (file) { |
| 180 | inline else => |x| x.calcSymtabSize(macho_file), | 187 | inline else => |x| x.calcSymtabSize(macho_file), |
| ... | @@ -206,6 +213,7 @@ const macho = std.macho; | ... | @@ -206,6 +213,7 @@ const macho = std.macho; |
| 206 | const std = @import("std"); | 213 | const std = @import("std"); |
| 207 | 214 | ||
| 208 | const Allocator = std.mem.Allocator; | 215 | const Allocator = std.mem.Allocator; |
| 216 | const Archive = @import("Archive.zig"); | ||
| 209 | const Atom = @import("Atom.zig"); | 217 | const Atom = @import("Atom.zig"); |
| 210 | const InternalObject = @import("InternalObject.zig"); | 218 | const InternalObject = @import("InternalObject.zig"); |
| 211 | const MachO = @import("../MachO.zig"); | 219 | const MachO = @import("../MachO.zig"); |
src/link/MachO/relocatable.zig+29-2| ... | @@ -64,7 +64,9 @@ pub fn flushObject(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]c | ... | @@ -64,7 +64,9 @@ pub fn flushObject(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]c |
| 64 | }; | 64 | }; |
| 65 | off = allocateSectionsRelocs(macho_file, off); | 65 | off = allocateSectionsRelocs(macho_file, off); |
| 66 | 66 | ||
| 67 | state_log.debug("{}", .{macho_file.dumpState()}); | 67 | if (build_options.enable_logging) { |
| 68 | state_log.debug("{}", .{macho_file.dumpState()}); | ||
| 69 | } | ||
| 68 | 70 | ||
| 69 | try macho_file.calcSymtabSize(); | 71 | try macho_file.calcSymtabSize(); |
| 70 | try writeAtoms(macho_file); | 72 | try writeAtoms(macho_file); |
| ... | @@ -111,6 +113,7 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? | ... | @@ -111,6 +113,7 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? |
| 111 | // First, we flush relocatable object file generated with our backends. | 113 | // First, we flush relocatable object file generated with our backends. |
| 112 | if (macho_file.getZigObject()) |zo| { | 114 | if (macho_file.getZigObject()) |zo| { |
| 113 | zo.resolveSymbols(macho_file); | 115 | zo.resolveSymbols(macho_file); |
| 116 | zo.asFile().markExportsRelocatable(macho_file); | ||
| 114 | zo.asFile().claimUnresolvedRelocatable(macho_file); | 117 | zo.asFile().claimUnresolvedRelocatable(macho_file); |
| 115 | try macho_file.sortSections(); | 118 | try macho_file.sortSections(); |
| 116 | try macho_file.addAtomsToSections(); | 119 | try macho_file.addAtomsToSections(); |
| ... | @@ -126,7 +129,9 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? | ... | @@ -126,7 +129,9 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? |
| 126 | }; | 129 | }; |
| 127 | off = allocateSectionsRelocs(macho_file, off); | 130 | off = allocateSectionsRelocs(macho_file, off); |
| 128 | 131 | ||
| 129 | state_log.debug("{}", .{macho_file.dumpState()}); | 132 | if (build_options.enable_logging) { |
| 133 | state_log.debug("{}", .{macho_file.dumpState()}); | ||
| 134 | } | ||
| 130 | 135 | ||
| 131 | try macho_file.calcSymtabSize(); | 136 | try macho_file.calcSymtabSize(); |
| 132 | try writeAtoms(macho_file); | 137 | try writeAtoms(macho_file); |
| ... | @@ -150,6 +155,26 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? | ... | @@ -150,6 +155,26 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? |
| 150 | try zo.readFileContents(macho_file); | 155 | try zo.readFileContents(macho_file); |
| 151 | } | 156 | } |
| 152 | 157 | ||
| 158 | var files = std.ArrayList(File.Index).init(gpa); | ||
| 159 | defer files.deinit(); | ||
| 160 | try files.ensureTotalCapacityPrecise(macho_file.objects.items.len + 1); | ||
| 161 | if (macho_file.getZigObject()) |zo| files.appendAssumeCapacity(zo.index); | ||
| 162 | for (macho_file.objects.items) |index| files.appendAssumeCapacity(index); | ||
| 163 | |||
| 164 | // Update ar symtab from parsed objects | ||
| 165 | var ar_symtab: Archive.ArSymtab = .{}; | ||
| 166 | defer ar_symtab.deinit(gpa); | ||
| 167 | |||
| 168 | for (files.items) |index| { | ||
| 169 | try macho_file.getFile(index).?.updateArSymtab(&ar_symtab, macho_file); | ||
| 170 | } | ||
| 171 | |||
| 172 | ar_symtab.sort(); | ||
| 173 | |||
| 174 | if (build_options.enable_logging) { | ||
| 175 | state_log.debug("ar_symtab\n{}\n", .{ar_symtab.fmt(macho_file)}); | ||
| 176 | } | ||
| 177 | |||
| 153 | var err = try macho_file.addErrorWithNotes(0); | 178 | var err = try macho_file.addErrorWithNotes(0); |
| 154 | try err.addMsg(macho_file, "TODO implement flushStaticLib", .{}); | 179 | try err.addMsg(macho_file, "TODO implement flushStaticLib", .{}); |
| 155 | 180 | ||
| ... | @@ -646,6 +671,7 @@ fn writeHeader(macho_file: *MachO, ncmds: usize, sizeofcmds: usize) !void { | ... | @@ -646,6 +671,7 @@ fn writeHeader(macho_file: *MachO, ncmds: usize, sizeofcmds: usize) !void { |
| 646 | } | 671 | } |
| 647 | 672 | ||
| 648 | const assert = std.debug.assert; | 673 | const assert = std.debug.assert; |
| 674 | const build_options = @import("build_options"); | ||
| 649 | const eh_frame = @import("eh_frame.zig"); | 675 | const eh_frame = @import("eh_frame.zig"); |
| 650 | const link = @import("../../link.zig"); | 676 | const link = @import("../../link.zig"); |
| 651 | const load_commands = @import("load_commands.zig"); | 677 | const load_commands = @import("load_commands.zig"); |
| ... | @@ -657,6 +683,7 @@ const state_log = std.log.scoped(.link_state); | ... | @@ -657,6 +683,7 @@ const state_log = std.log.scoped(.link_state); |
| 657 | const std = @import("std"); | 683 | const std = @import("std"); |
| 658 | const trace = @import("../../tracy.zig").trace; | 684 | const trace = @import("../../tracy.zig").trace; |
| 659 | 685 | ||
| 686 | const Archive = @import("Archive.zig"); | ||
| 660 | const Atom = @import("Atom.zig"); | 687 | const Atom = @import("Atom.zig"); |
| 661 | const Compilation = @import("../../Compilation.zig"); | 688 | const Compilation = @import("../../Compilation.zig"); |
| 662 | const File = @import("file.zig").File; | 689 | const File = @import("file.zig").File; |