| author | |
| committer | |
| log | 2b3bda43e352152f0150bf2e795419cf1bcfcd90 |
| tree | f6c550980ea30b8e3ded451b6145ebb203101b22 |
| parent | 3622fe08dbdcaccb04204b48257e1d5fcbe0d164 |
6 files changed, 231 insertions(+), 184 deletions(-)
src/link/MachO.zig+1-1| ... | ... | @@ -724,7 +724,7 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void { |
| 724 | 724 | try fs.cwd().copyFile(the_object_path, fs.cwd(), full_out_path, .{}); |
| 725 | 725 | } |
| 726 | 726 | } else { |
| 727 | var zld = Zld.init(self.base.allocator); | |
| 727 | var zld = try Zld.init(self.base.allocator); | |
| 728 | 728 | defer { |
| 729 | 729 | zld.closeFiles(); |
| 730 | 730 | zld.deinit(); |
src/link/MachO/Dylib.zig+8-14| ... | ... | @@ -146,7 +146,12 @@ pub const CreateOpts = struct { |
| 146 | 146 | id: ?Id = null, |
| 147 | 147 | }; |
| 148 | 148 | |
| 149 | pub fn createAndParseFromPath(allocator: *Allocator, arch: Arch, path: []const u8, opts: CreateOpts) Error!?[]*Dylib { | |
| 149 | pub fn createAndParseFromPath( | |
| 150 | allocator: *Allocator, | |
| 151 | arch: Arch, | |
| 152 | path: []const u8, | |
| 153 | opts: CreateOpts, | |
| 154 | ) Error!?[]*Dylib { | |
| 150 | 155 | const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) { |
| 151 | 156 | error.FileNotFound => return null, |
| 152 | 157 | else => |e| return e, |
| ... | ... | @@ -505,18 +510,7 @@ pub fn parseDependentLibs(self: *Dylib, out: *std.ArrayList(*Dylib)) !void { |
| 505 | 510 | |
| 506 | 511 | pub fn createProxy(self: *Dylib, sym_name: []const u8) !?*Symbol { |
| 507 | 512 | if (!self.symbols.contains(sym_name)) return null; |
| 508 | ||
| 509 | const name = try self.allocator.dupe(u8, sym_name); | |
| 510 | const proxy = try self.allocator.create(Symbol.Proxy); | |
| 511 | errdefer self.allocator.destroy(proxy); | |
| 512 | ||
| 513 | proxy.* = .{ | |
| 514 | .base = .{ | |
| 515 | .@"type" = .proxy, | |
| 516 | .name = name, | |
| 517 | }, | |
| 513 | return Symbol.Proxy.new(self.allocator, sym_name, .{ | |
| 518 | 514 | .file = self, |
| 519 | }; | |
| 520 | ||
| 521 | return &proxy.base; | |
| 515 | }); | |
| 522 | 516 | } |
src/link/MachO/Object.zig+7-28| ... | ... | @@ -9,12 +9,12 @@ const log = std.log.scoped(.object); |
| 9 | 9 | const macho = std.macho; |
| 10 | 10 | const mem = std.mem; |
| 11 | 11 | const reloc = @import("reloc.zig"); |
| 12 | const parseName = @import("Zld.zig").parseName; | |
| 12 | 13 | |
| 13 | 14 | const Allocator = mem.Allocator; |
| 14 | 15 | const Arch = std.Target.Cpu.Arch; |
| 15 | 16 | const Relocation = reloc.Relocation; |
| 16 | 17 | const Symbol = @import("Symbol.zig"); |
| 17 | const parseName = @import("Zld.zig").parseName; | |
| 18 | 18 | |
| 19 | 19 | usingnamespace @import("commands.zig"); |
| 20 | 20 | |
| ... | ... | @@ -437,47 +437,26 @@ pub fn parseSymbols(self: *Object) !void { |
| 437 | 437 | if (Symbol.isWeakDef(sym) or Symbol.isPext(sym)) break :linkage .linkage_unit; |
| 438 | 438 | break :linkage .global; |
| 439 | 439 | }; |
| 440 | const regular = try self.allocator.create(Symbol.Regular); | |
| 441 | errdefer self.allocator.destroy(regular); | |
| 442 | regular.* = .{ | |
| 443 | .base = .{ | |
| 444 | .@"type" = .regular, | |
| 445 | .name = name, | |
| 446 | }, | |
| 440 | break :symbol try Symbol.Regular.new(self.allocator, name, .{ | |
| 447 | 441 | .linkage = linkage, |
| 448 | 442 | .address = sym.n_value, |
| 449 | 443 | .section = sym.n_sect - 1, |
| 450 | 444 | .weak_ref = Symbol.isWeakRef(sym), |
| 451 | 445 | .file = self, |
| 452 | }; | |
| 453 | break :symbol &regular.base; | |
| 446 | }); | |
| 454 | 447 | } |
| 455 | 448 | |
| 456 | 449 | if (sym.n_value != 0) { |
| 457 | const tentative = try self.allocator.create(Symbol.Tentative); | |
| 458 | errdefer self.allocator.destroy(tentative); | |
| 459 | tentative.* = .{ | |
| 460 | .base = .{ | |
| 461 | .@"type" = .tentative, | |
| 462 | .name = name, | |
| 463 | }, | |
| 450 | break :symbol try Symbol.Tentative.new(self.allocator, name, .{ | |
| 464 | 451 | .size = sym.n_value, |
| 465 | 452 | .alignment = (sym.n_desc >> 8) & 0x0f, |
| 466 | 453 | .file = self, |
| 467 | }; | |
| 468 | break :symbol &tentative.base; | |
| 454 | }); | |
| 469 | 455 | } |
| 470 | 456 | |
| 471 | const undef = try self.allocator.create(Symbol.Unresolved); | |
| 472 | errdefer self.allocator.destroy(undef); | |
| 473 | undef.* = .{ | |
| 474 | .base = .{ | |
| 475 | .@"type" = .unresolved, | |
| 476 | .name = name, | |
| 477 | }, | |
| 457 | break :symbol try Symbol.Unresolved.new(self.allocator, name, .{ | |
| 478 | 458 | .file = self, |
| 479 | }; | |
| 480 | break :symbol &undef.base; | |
| 459 | }); | |
| 481 | 460 | }; |
| 482 | 461 | |
| 483 | 462 | try self.symbols.append(self.allocator, symbol); |
src/link/MachO/StringTable.zig+7-27| ... | ... | @@ -8,7 +8,6 @@ const Allocator = mem.Allocator; |
| 8 | 8 | |
| 9 | 9 | allocator: *Allocator, |
| 10 | 10 | buffer: std.ArrayListUnmanaged(u8) = .{}, |
| 11 | used_offsets: std.ArrayListUnmanaged(u32) = .{}, | |
| 12 | 11 | cache: std.StringHashMapUnmanaged(u32) = .{}, |
| 13 | 12 | |
| 14 | 13 | pub const Error = error{OutOfMemory}; |
| ... | ... | @@ -22,8 +21,13 @@ pub fn init(allocator: *Allocator) Error!StringTable { |
| 22 | 21 | } |
| 23 | 22 | |
| 24 | 23 | pub fn deinit(self: *StringTable) void { |
| 24 | { | |
| 25 | var it = self.cache.keyIterator(); | |
| 26 | while (it.next()) |key| { | |
| 27 | self.allocator.free(key.*); | |
| 28 | } | |
| 29 | } | |
| 25 | 30 | self.cache.deinit(self.allocator); |
| 26 | self.used_offsets.deinit(self.allocator); | |
| 27 | 31 | self.buffer.deinit(self.allocator); |
| 28 | 32 | } |
| 29 | 33 | |
| ... | ... | @@ -33,8 +37,6 @@ pub fn getOrPut(self: *StringTable, string: []const u8) Error!u32 { |
| 33 | 37 | return off; |
| 34 | 38 | } |
| 35 | 39 | |
| 36 | const invalidate_cache = self.needsToGrow(string.len + 1); | |
| 37 | ||
| 38 | 40 | try self.buffer.ensureUnusedCapacity(self.allocator, string.len + 1); |
| 39 | 41 | const new_off = @intCast(u32, self.buffer.items.len); |
| 40 | 42 | |
| ... | ... | @@ -43,25 +45,7 @@ pub fn getOrPut(self: *StringTable, string: []const u8) Error!u32 { |
| 43 | 45 | self.buffer.appendSliceAssumeCapacity(string); |
| 44 | 46 | self.buffer.appendAssumeCapacity(0); |
| 45 | 47 | |
| 46 | if (invalidate_cache) { | |
| 47 | log.debug("invalidating cache", .{}); | |
| 48 | // Re-create the cache. | |
| 49 | self.cache.clearRetainingCapacity(); | |
| 50 | for (self.used_offsets.items) |off| { | |
| 51 | try self.cache.putNoClobber(self.allocator, self.get(off).?, off); | |
| 52 | } | |
| 53 | } | |
| 54 | ||
| 55 | { | |
| 56 | log.debug("cache:", .{}); | |
| 57 | var it = self.cache.iterator(); | |
| 58 | while (it.next()) |entry| { | |
| 59 | log.debug(" | {s} => {}", .{ entry.key_ptr.*, entry.value_ptr.* }); | |
| 60 | } | |
| 61 | } | |
| 62 | ||
| 63 | try self.cache.putNoClobber(self.allocator, self.get(new_off).?, new_off); | |
| 64 | try self.used_offsets.append(self.allocator, new_off); | |
| 48 | try self.cache.putNoClobber(self.allocator, try self.allocator.dupe(u8, string), new_off); | |
| 65 | 49 | |
| 66 | 50 | return new_off; |
| 67 | 51 | } |
| ... | ... | @@ -78,7 +62,3 @@ pub fn asSlice(self: StringTable) []const u8 { |
| 78 | 62 | pub fn size(self: StringTable) u64 { |
| 79 | 63 | return self.buffer.items.len; |
| 80 | 64 | } |
| 81 | ||
| 82 | fn needsToGrow(self: StringTable, needed_space: u64) bool { | |
| 83 | return self.buffer.capacity < needed_space + self.size(); | |
| 84 | } |
src/link/MachO/Symbol.zig+174-15| ... | ... | @@ -7,6 +7,7 @@ const mem = std.mem; |
| 7 | 7 | const Allocator = mem.Allocator; |
| 8 | 8 | const Dylib = @import("Dylib.zig"); |
| 9 | 9 | const Object = @import("Object.zig"); |
| 10 | const StringTable = @import("StringTable.zig"); | |
| 10 | 11 | |
| 11 | 12 | pub const Type = enum { |
| 12 | 13 | regular, |
| ... | ... | @@ -19,7 +20,7 @@ pub const Type = enum { |
| 19 | 20 | @"type": Type, |
| 20 | 21 | |
| 21 | 22 | /// Symbol name. Owned slice. |
| 22 | name: []u8, | |
| 23 | name: []const u8, | |
| 23 | 24 | |
| 24 | 25 | /// Alias of. |
| 25 | 26 | alias: ?*Symbol = null, |
| ... | ... | @@ -43,23 +44,14 @@ pub const Regular = struct { |
| 43 | 44 | section: u8, |
| 44 | 45 | |
| 45 | 46 | /// Whether the symbol is a weak ref. |
| 46 | weak_ref: bool, | |
| 47 | weak_ref: bool = false, | |
| 47 | 48 | |
| 48 | 49 | /// Object file where to locate this symbol. |
| 49 | file: *Object, | |
| 50 | /// null means self-reference. | |
| 51 | file: ?*Object = null, | |
| 50 | 52 | |
| 51 | 53 | /// Debug stab if defined. |
| 52 | stab: ?struct { | |
| 53 | /// Stab kind | |
| 54 | kind: enum { | |
| 55 | function, | |
| 56 | global, | |
| 57 | static, | |
| 58 | }, | |
| 59 | ||
| 60 | /// Size of the stab. | |
| 61 | size: u64, | |
| 62 | } = null, | |
| 54 | stab: ?Stab = null, | |
| 63 | 55 | |
| 64 | 56 | /// True if symbol was already committed into the final |
| 65 | 57 | /// symbol table. |
| ... | ... | @@ -73,6 +65,68 @@ pub const Regular = struct { |
| 73 | 65 | global, |
| 74 | 66 | }; |
| 75 | 67 | |
| 68 | pub const Stab = struct { | |
| 69 | /// Stab kind | |
| 70 | kind: enum { | |
| 71 | function, | |
| 72 | global, | |
| 73 | static, | |
| 74 | }, | |
| 75 | ||
| 76 | /// Size of the stab. | |
| 77 | size: u64, | |
| 78 | }; | |
| 79 | ||
| 80 | const Opts = struct { | |
| 81 | linkage: Linkage = .translation_unit, | |
| 82 | address: u64 = 0, | |
| 83 | section: u8 = 0, | |
| 84 | weak_ref: bool = false, | |
| 85 | file: ?*Object = null, | |
| 86 | stab: ?Stab = null, | |
| 87 | }; | |
| 88 | ||
| 89 | pub fn new(allocator: *Allocator, name: []const u8, opts: Opts) !*Symbol { | |
| 90 | const reg = try allocator.create(Regular); | |
| 91 | errdefer allocator.destroy(reg); | |
| 92 | ||
| 93 | reg.* = .{ | |
| 94 | .base = .{ | |
| 95 | .@"type" = .regular, | |
| 96 | .name = try allocator.dupe(u8, name), | |
| 97 | }, | |
| 98 | .linkage = opts.linkage, | |
| 99 | .address = opts.address, | |
| 100 | .section = opts.section, | |
| 101 | .weak_ref = opts.weak_ref, | |
| 102 | .file = opts.file, | |
| 103 | .stab = opts.stab, | |
| 104 | }; | |
| 105 | ||
| 106 | return &reg.base; | |
| 107 | } | |
| 108 | ||
| 109 | pub fn asNlist(regular: *Regular, strtab: *StringTable) !macho.nlist_64 { | |
| 110 | const n_strx = try strtab.getOrPut(regular.base.name); | |
| 111 | var nlist = macho.nlist_64{ | |
| 112 | .n_strx = n_strx, | |
| 113 | .n_type = macho.N_SECT, | |
| 114 | .n_sect = regular.section, | |
| 115 | .n_desc = 0, | |
| 116 | .n_value = regular.address, | |
| 117 | }; | |
| 118 | ||
| 119 | if (regular.linkage != .translation_unit) { | |
| 120 | nlist.n_type |= macho.N_EXT; | |
| 121 | } | |
| 122 | if (regular.linkage == .linkage_unit) { | |
| 123 | nlist.n_type |= macho.N_PEXT; | |
| 124 | nlist.n_desc |= macho.N_WEAK_DEF; | |
| 125 | } | |
| 126 | ||
| 127 | return nlist; | |
| 128 | } | |
| 129 | ||
| 76 | 130 | pub fn isTemp(regular: *Regular) bool { |
| 77 | 131 | if (regular.linkage == .translation_unit) { |
| 78 | 132 | return mem.startsWith(u8, regular.base.name, "l") or mem.startsWith(u8, regular.base.name, "L"); |
| ... | ... | @@ -97,6 +151,36 @@ pub const Proxy = struct { |
| 97 | 151 | |
| 98 | 152 | pub const base_type: Symbol.Type = .proxy; |
| 99 | 153 | |
| 154 | const Opts = struct { | |
| 155 | file: ?*Dylib = null, | |
| 156 | }; | |
| 157 | ||
| 158 | pub fn new(allocator: *Allocator, name: []const u8, opts: Opts) !*Symbol { | |
| 159 | const proxy = try allocator.create(Proxy); | |
| 160 | errdefer allocator.destroy(proxy); | |
| 161 | ||
| 162 | proxy.* = .{ | |
| 163 | .base = .{ | |
| 164 | .@"type" = .proxy, | |
| 165 | .name = try allocator.dupe(u8, name), | |
| 166 | }, | |
| 167 | .file = opts.file, | |
| 168 | }; | |
| 169 | ||
| 170 | return &proxy.base; | |
| 171 | } | |
| 172 | ||
| 173 | pub fn asNlist(proxy: *Proxy, strtab: *StringTable) !macho.nlist_64 { | |
| 174 | const n_strx = try strtab.getOrPut(proxy.base.name); | |
| 175 | return macho.nlist_64{ | |
| 176 | .n_strx = n_strx, | |
| 177 | .n_type = macho.N_UNDF | macho.N_EXT, | |
| 178 | .n_sect = 0, | |
| 179 | .n_desc = (proxy.dylibOrdinal() * macho.N_SYMBOL_RESOLVER) | macho.REFERENCE_FLAG_UNDEFINED_NON_LAZY, | |
| 180 | .n_value = 0, | |
| 181 | }; | |
| 182 | } | |
| 183 | ||
| 100 | 184 | pub fn deinit(proxy: *Proxy, allocator: *Allocator) void { |
| 101 | 185 | proxy.bind_info.deinit(allocator); |
| 102 | 186 | } |
| ... | ... | @@ -115,6 +199,36 @@ pub const Unresolved = struct { |
| 115 | 199 | file: ?*Object = null, |
| 116 | 200 | |
| 117 | 201 | pub const base_type: Symbol.Type = .unresolved; |
| 202 | ||
| 203 | const Opts = struct { | |
| 204 | file: ?*Object = null, | |
| 205 | }; | |
| 206 | ||
| 207 | pub fn new(allocator: *Allocator, name: []const u8, opts: Opts) !*Symbol { | |
| 208 | const undef = try allocator.create(Unresolved); | |
| 209 | errdefer allocator.destroy(undef); | |
| 210 | ||
| 211 | undef.* = .{ | |
| 212 | .base = .{ | |
| 213 | .@"type" = .unresolved, | |
| 214 | .name = try allocator.dupe(u8, name), | |
| 215 | }, | |
| 216 | .file = opts.file, | |
| 217 | }; | |
| 218 | ||
| 219 | return &undef.base; | |
| 220 | } | |
| 221 | ||
| 222 | pub fn asNlist(undef: *Unresolved, strtab: *StringTable) !macho.nlist_64 { | |
| 223 | const n_strx = try strtab.getOrPut(undef.base.name); | |
| 224 | return macho.nlist_64{ | |
| 225 | .n_strx = n_strx, | |
| 226 | .n_type = macho.N_UNDF, | |
| 227 | .n_sect = 0, | |
| 228 | .n_desc = 0, | |
| 229 | .n_value = 0, | |
| 230 | }; | |
| 231 | } | |
| 118 | 232 | }; |
| 119 | 233 | |
| 120 | 234 | pub const Tentative = struct { |
| ... | ... | @@ -127,13 +241,49 @@ pub const Tentative = struct { |
| 127 | 241 | alignment: u16, |
| 128 | 242 | |
| 129 | 243 | /// File where this symbol was referenced. |
| 130 | file: *Object, | |
| 244 | file: ?*Object = null, | |
| 131 | 245 | |
| 132 | 246 | pub const base_type: Symbol.Type = .tentative; |
| 247 | ||
| 248 | const Opts = struct { | |
| 249 | size: u64 = 0, | |
| 250 | alignment: u16 = 0, | |
| 251 | file: ?*Object = null, | |
| 252 | }; | |
| 253 | ||
| 254 | pub fn new(allocator: *Allocator, name: []const u8, opts: Opts) !*Symbol { | |
| 255 | const tent = try allocator.create(Tentative); | |
| 256 | errdefer allocator.destroy(tent); | |
| 257 | ||
| 258 | tent.* = .{ | |
| 259 | .base = .{ | |
| 260 | .@"type" = .tentative, | |
| 261 | .name = try allocator.dupe(u8, name), | |
| 262 | }, | |
| 263 | .size = opts.size, | |
| 264 | .alignment = opts.alignment, | |
| 265 | .file = opts.file, | |
| 266 | }; | |
| 267 | ||
| 268 | return &tent.base; | |
| 269 | } | |
| 270 | ||
| 271 | pub fn asNlist(tent: *Tentative, strtab: *StringTable) !macho.nlist_64 { | |
| 272 | // TODO | |
| 273 | const n_strx = try strtab.getOrPut(tent.base.name); | |
| 274 | return macho.nlist_64{ | |
| 275 | .n_strx = n_strx, | |
| 276 | .n_type = macho.N_UNDF, | |
| 277 | .n_sect = 0, | |
| 278 | .n_desc = 0, | |
| 279 | .n_value = 0, | |
| 280 | }; | |
| 281 | } | |
| 133 | 282 | }; |
| 134 | 283 | |
| 135 | 284 | pub fn deinit(base: *Symbol, allocator: *Allocator) void { |
| 136 | 285 | allocator.free(base.name); |
| 286 | ||
| 137 | 287 | switch (base.@"type") { |
| 138 | 288 | .proxy => @fieldParentPtr(Proxy, "base", base).deinit(allocator), |
| 139 | 289 | else => {}, |
| ... | ... | @@ -154,6 +304,15 @@ pub fn getTopmostAlias(base: *Symbol) *Symbol { |
| 154 | 304 | return base; |
| 155 | 305 | } |
| 156 | 306 | |
| 307 | pub fn asNlist(base: *Symbol, strtab: *StringTable) !macho.nlist_64 { | |
| 308 | return switch (base.tag) { | |
| 309 | .regular => @fieldParentPtr(Regular, "base", base).asNlist(strtab), | |
| 310 | .proxy => @fieldParentPtr(Proxy, "base", base).asNlist(strtab), | |
| 311 | .unresolved => @fieldParentPtr(Unresolved, "base", base).asNlist(strtab), | |
| 312 | .tentative => @fieldParentPtr(Tentative, "base", base).asNlist(strtab), | |
| 313 | }; | |
| 314 | } | |
| 315 | ||
| 157 | 316 | pub fn isStab(sym: macho.nlist_64) bool { |
| 158 | 317 | return (macho.N_STAB & sym.n_type) != 0; |
| 159 | 318 | } |
src/link/MachO/Zld.zig+34-99| ... | ... | @@ -17,6 +17,7 @@ const Archive = @import("Archive.zig"); |
| 17 | 17 | const CodeSignature = @import("CodeSignature.zig"); |
| 18 | 18 | const Dylib = @import("Dylib.zig"); |
| 19 | 19 | const Object = @import("Object.zig"); |
| 20 | const StringTable = @import("StringTable.zig"); | |
| 20 | 21 | const Symbol = @import("Symbol.zig"); |
| 21 | 22 | const Trie = @import("Trie.zig"); |
| 22 | 23 | |
| ... | ... | @@ -24,6 +25,7 @@ usingnamespace @import("commands.zig"); |
| 24 | 25 | usingnamespace @import("bind.zig"); |
| 25 | 26 | |
| 26 | 27 | allocator: *Allocator, |
| 28 | strtab: StringTable, | |
| 27 | 29 | |
| 28 | 30 | target: ?std.Target = null, |
| 29 | 31 | page_size: ?u16 = null, |
| ... | ... | @@ -109,9 +111,6 @@ tentatives: std.StringArrayHashMapUnmanaged(*Symbol) = .{}, |
| 109 | 111 | /// Set if the linker found tentative definitions in any of the objects. |
| 110 | 112 | tentative_defs_offset: u64 = 0, |
| 111 | 113 | |
| 112 | strtab: std.ArrayListUnmanaged(u8) = .{}, | |
| 113 | strtab_dir: std.StringHashMapUnmanaged(u32) = .{}, | |
| 114 | ||
| 115 | 114 | threadlocal_offsets: std.ArrayListUnmanaged(TlvOffset) = .{}, // TODO merge with Symbol abstraction |
| 116 | 115 | local_rebases: std.ArrayListUnmanaged(Pointer) = .{}, |
| 117 | 116 | stubs: std.ArrayListUnmanaged(*Symbol) = .{}, |
| ... | ... | @@ -138,8 +137,11 @@ const TlvOffset = struct { |
| 138 | 137 | /// Default path to dyld |
| 139 | 138 | const DEFAULT_DYLD_PATH: [*:0]const u8 = "/usr/lib/dyld"; |
| 140 | 139 | |
| 141 | pub fn init(allocator: *Allocator) Zld { | |
| 142 | return .{ .allocator = allocator }; | |
| 140 | pub fn init(allocator: *Allocator) !Zld { | |
| 141 | return Zld{ | |
| 142 | .allocator = allocator, | |
| 143 | .strtab = try StringTable.init(allocator), | |
| 144 | }; | |
| 143 | 145 | } |
| 144 | 146 | |
| 145 | 147 | pub fn deinit(self: *Zld) void { |
| ... | ... | @@ -180,15 +182,7 @@ pub fn deinit(self: *Zld) void { |
| 180 | 182 | self.tentatives.deinit(self.allocator); |
| 181 | 183 | self.globals.deinit(self.allocator); |
| 182 | 184 | self.unresolved.deinit(self.allocator); |
| 183 | self.strtab.deinit(self.allocator); | |
| 184 | ||
| 185 | { | |
| 186 | var it = self.strtab_dir.keyIterator(); | |
| 187 | while (it.next()) |key| { | |
| 188 | self.allocator.free(key.*); | |
| 189 | } | |
| 190 | } | |
| 191 | self.strtab_dir.deinit(self.allocator); | |
| 185 | self.strtab.deinit(); | |
| 192 | 186 | } |
| 193 | 187 | |
| 194 | 188 | pub fn closeFiles(self: Zld) void { |
| ... | ... | @@ -1137,16 +1131,7 @@ fn allocateTentativeSymbols(self: *Zld) !void { |
| 1137 | 1131 | // Convert tentative definitions into regular symbols. |
| 1138 | 1132 | for (self.tentatives.values()) |sym| { |
| 1139 | 1133 | const tent = sym.cast(Symbol.Tentative) orelse unreachable; |
| 1140 | const reg = try self.allocator.create(Symbol.Regular); | |
| 1141 | errdefer self.allocator.destroy(reg); | |
| 1142 | ||
| 1143 | reg.* = .{ | |
| 1144 | .base = .{ | |
| 1145 | .@"type" = .regular, | |
| 1146 | .name = try self.allocator.dupe(u8, tent.base.name), | |
| 1147 | .got_index = tent.base.got_index, | |
| 1148 | .stubs_index = tent.base.stubs_index, | |
| 1149 | }, | |
| 1134 | const reg = try Symbol.Regular.new(self.allocator, tent.base.name, .{ | |
| 1150 | 1135 | .linkage = .global, |
| 1151 | 1136 | .address = base_address, |
| 1152 | 1137 | .section = section, |
| ... | ... | @@ -1156,16 +1141,18 @@ fn allocateTentativeSymbols(self: *Zld) !void { |
| 1156 | 1141 | .kind = .global, |
| 1157 | 1142 | .size = 0, |
| 1158 | 1143 | }, |
| 1159 | }; | |
| 1144 | }); | |
| 1145 | reg.got_index = tent.base.got_index; | |
| 1146 | reg.stubs_index = tent.base.stubs_index; | |
| 1160 | 1147 | |
| 1161 | try self.globals.putNoClobber(self.allocator, reg.base.name, &reg.base); | |
| 1162 | tent.base.alias = &reg.base; | |
| 1148 | try self.globals.putNoClobber(self.allocator, reg.name, reg); | |
| 1149 | tent.base.alias = reg; | |
| 1163 | 1150 | |
| 1164 | 1151 | if (tent.base.got_index) |idx| { |
| 1165 | self.got_entries.items[idx] = &reg.base; | |
| 1152 | self.got_entries.items[idx] = reg; | |
| 1166 | 1153 | } |
| 1167 | 1154 | if (tent.base.stubs_index) |idx| { |
| 1168 | self.stubs.items[idx] = &reg.base; | |
| 1155 | self.stubs.items[idx] = reg; | |
| 1169 | 1156 | } |
| 1170 | 1157 | |
| 1171 | 1158 | const address = mem.alignForwardGeneric(u64, base_address + tent.size, alignment); |
| ... | ... | @@ -1615,15 +1602,8 @@ fn resolveSymbols(self: *Zld) !void { |
| 1615 | 1602 | { |
| 1616 | 1603 | const name = try self.allocator.dupe(u8, "dyld_stub_binder"); |
| 1617 | 1604 | errdefer self.allocator.free(name); |
| 1618 | const undef = try self.allocator.create(Symbol.Unresolved); | |
| 1619 | errdefer self.allocator.destroy(undef); | |
| 1620 | undef.* = .{ | |
| 1621 | .base = .{ | |
| 1622 | .@"type" = .unresolved, | |
| 1623 | .name = name, | |
| 1624 | }, | |
| 1625 | }; | |
| 1626 | try unresolved.append(&undef.base); | |
| 1605 | const undef = try Symbol.Unresolved.new(self.allocator, name, .{}); | |
| 1606 | try unresolved.append(undef); | |
| 1627 | 1607 | } |
| 1628 | 1608 | |
| 1629 | 1609 | var referenced = std.AutoHashMap(*Dylib, void).init(self.allocator); |
| ... | ... | @@ -1641,16 +1621,7 @@ fn resolveSymbols(self: *Zld) !void { |
| 1641 | 1621 | // TODO this is just a temp patch until I work out what to actually |
| 1642 | 1622 | // do with ___dso_handle and __mh_execute_header symbols which are |
| 1643 | 1623 | // synthetically created by the linker on macOS. |
| 1644 | const name = try self.allocator.dupe(u8, undef.name); | |
| 1645 | const proxy = try self.allocator.create(Symbol.Proxy); | |
| 1646 | errdefer self.allocator.destroy(proxy); | |
| 1647 | proxy.* = .{ | |
| 1648 | .base = .{ | |
| 1649 | .@"type" = .proxy, | |
| 1650 | .name = name, | |
| 1651 | }, | |
| 1652 | }; | |
| 1653 | break :inner &proxy.base; | |
| 1624 | break :inner try Symbol.Proxy.new(self.allocator, undef.name, .{}); | |
| 1654 | 1625 | } |
| 1655 | 1626 | |
| 1656 | 1627 | self.unresolved.putAssumeCapacityNoClobber(undef.name, undef); |
| ... | ... | @@ -2126,7 +2097,6 @@ fn populateMetadata(self: *Zld) !void { |
| 2126 | 2097 | .strsize = 0, |
| 2127 | 2098 | }, |
| 2128 | 2099 | }); |
| 2129 | try self.strtab.append(self.allocator, 0); | |
| 2130 | 2100 | } |
| 2131 | 2101 | |
| 2132 | 2102 | if (self.dysymtab_cmd_index == null) { |
| ... | ... | @@ -2752,7 +2722,7 @@ fn writeDebugInfo(self: *Zld) !void { |
| 2752 | 2722 | const dirname = std.fs.path.dirname(tu_path) orelse "./"; |
| 2753 | 2723 | // Current dir |
| 2754 | 2724 | try stabs.append(.{ |
| 2755 | .n_strx = try self.makeString(tu_path[0 .. dirname.len + 1]), | |
| 2725 | .n_strx = try self.strtab.getOrPut(tu_path[0 .. dirname.len + 1]), | |
| 2756 | 2726 | .n_type = macho.N_SO, |
| 2757 | 2727 | .n_sect = 0, |
| 2758 | 2728 | .n_desc = 0, |
| ... | ... | @@ -2760,7 +2730,7 @@ fn writeDebugInfo(self: *Zld) !void { |
| 2760 | 2730 | }); |
| 2761 | 2731 | // Artifact name |
| 2762 | 2732 | try stabs.append(.{ |
| 2763 | .n_strx = try self.makeString(tu_path[dirname.len + 1 ..]), | |
| 2733 | .n_strx = try self.strtab.getOrPut(tu_path[dirname.len + 1 ..]), | |
| 2764 | 2734 | .n_type = macho.N_SO, |
| 2765 | 2735 | .n_sect = 0, |
| 2766 | 2736 | .n_desc = 0, |
| ... | ... | @@ -2768,7 +2738,7 @@ fn writeDebugInfo(self: *Zld) !void { |
| 2768 | 2738 | }); |
| 2769 | 2739 | // Path to object file with debug info |
| 2770 | 2740 | try stabs.append(.{ |
| 2771 | .n_strx = try self.makeString(object.name.?), | |
| 2741 | .n_strx = try self.strtab.getOrPut(object.name.?), | |
| 2772 | 2742 | .n_type = macho.N_OSO, |
| 2773 | 2743 | .n_sect = 0, |
| 2774 | 2744 | .n_desc = 1, |
| ... | ... | @@ -2801,7 +2771,7 @@ fn writeDebugInfo(self: *Zld) !void { |
| 2801 | 2771 | .n_value = reg.address, |
| 2802 | 2772 | }); |
| 2803 | 2773 | try stabs.append(.{ |
| 2804 | .n_strx = try self.makeString(sym.name), | |
| 2774 | .n_strx = try self.strtab.getOrPut(sym.name), | |
| 2805 | 2775 | .n_type = macho.N_FUN, |
| 2806 | 2776 | .n_sect = reg.section, |
| 2807 | 2777 | .n_desc = 0, |
| ... | ... | @@ -2824,7 +2794,7 @@ fn writeDebugInfo(self: *Zld) !void { |
| 2824 | 2794 | }, |
| 2825 | 2795 | .global => { |
| 2826 | 2796 | try stabs.append(.{ |
| 2827 | .n_strx = try self.makeString(sym.name), | |
| 2797 | .n_strx = try self.strtab.getOrPut(sym.name), | |
| 2828 | 2798 | .n_type = macho.N_GSYM, |
| 2829 | 2799 | .n_sect = 0, |
| 2830 | 2800 | .n_desc = 0, |
| ... | ... | @@ -2833,7 +2803,7 @@ fn writeDebugInfo(self: *Zld) !void { |
| 2833 | 2803 | }, |
| 2834 | 2804 | .static => { |
| 2835 | 2805 | try stabs.append(.{ |
| 2836 | .n_strx = try self.makeString(sym.name), | |
| 2806 | .n_strx = try self.strtab.getOrPut(sym.name), | |
| 2837 | 2807 | .n_type = macho.N_STSYM, |
| 2838 | 2808 | .n_sect = reg.section, |
| 2839 | 2809 | .n_desc = 0, |
| ... | ... | @@ -2892,24 +2862,14 @@ fn writeSymbolTable(self: *Zld) !void { |
| 2892 | 2862 | if (reg.isTemp()) continue; |
| 2893 | 2863 | if (reg.visited) continue; |
| 2894 | 2864 | |
| 2865 | const nlist = try reg.asNlist(&self.strtab); | |
| 2866 | ||
| 2895 | 2867 | switch (reg.linkage) { |
| 2896 | 2868 | .translation_unit => { |
| 2897 | try locals.append(.{ | |
| 2898 | .n_strx = try self.makeString(sym.name), | |
| 2899 | .n_type = macho.N_SECT, | |
| 2900 | .n_sect = reg.section, | |
| 2901 | .n_desc = 0, | |
| 2902 | .n_value = reg.address, | |
| 2903 | }); | |
| 2869 | try locals.append(nlist); | |
| 2904 | 2870 | }, |
| 2905 | 2871 | else => { |
| 2906 | try exports.append(.{ | |
| 2907 | .n_strx = try self.makeString(sym.name), | |
| 2908 | .n_type = macho.N_SECT | macho.N_EXT, | |
| 2909 | .n_sect = reg.section, | |
| 2910 | .n_desc = 0, | |
| 2911 | .n_value = reg.address, | |
| 2912 | }); | |
| 2872 | try exports.append(nlist); | |
| 2913 | 2873 | }, |
| 2914 | 2874 | } |
| 2915 | 2875 | |
| ... | ... | @@ -2922,13 +2882,8 @@ fn writeSymbolTable(self: *Zld) !void { |
| 2922 | 2882 | |
| 2923 | 2883 | for (self.imports.values()) |sym| { |
| 2924 | 2884 | const proxy = sym.cast(Symbol.Proxy) orelse unreachable; |
| 2925 | try undefs.append(.{ | |
| 2926 | .n_strx = try self.makeString(sym.name), | |
| 2927 | .n_type = macho.N_UNDF | macho.N_EXT, | |
| 2928 | .n_sect = 0, | |
| 2929 | .n_desc = (proxy.dylibOrdinal() * macho.N_SYMBOL_RESOLVER) | macho.REFERENCE_FLAG_UNDEFINED_NON_LAZY, | |
| 2930 | .n_value = 0, | |
| 2931 | }); | |
| 2885 | const nlist = try proxy.asNlist(&self.strtab); | |
| 2886 | try undefs.append(nlist); | |
| 2932 | 2887 | } |
| 2933 | 2888 | |
| 2934 | 2889 | const nlocals = locals.items.len; |
| ... | ... | @@ -3017,14 +2972,14 @@ fn writeStringTable(self: *Zld) !void { |
| 3017 | 2972 | const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment; |
| 3018 | 2973 | const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab; |
| 3019 | 2974 | symtab.stroff = @intCast(u32, seg.inner.fileoff + seg.inner.filesize); |
| 3020 | symtab.strsize = @intCast(u32, mem.alignForwardGeneric(u64, self.strtab.items.len, @alignOf(u64))); | |
| 2975 | symtab.strsize = @intCast(u32, mem.alignForwardGeneric(u64, self.strtab.size(), @alignOf(u64))); | |
| 3021 | 2976 | seg.inner.filesize += symtab.strsize; |
| 3022 | 2977 | |
| 3023 | 2978 | log.debug("writing string table from 0x{x} to 0x{x}", .{ symtab.stroff, symtab.stroff + symtab.strsize }); |
| 3024 | 2979 | |
| 3025 | try self.file.?.pwriteAll(self.strtab.items, symtab.stroff); | |
| 2980 | try self.file.?.pwriteAll(self.strtab.asSlice(), symtab.stroff); | |
| 3026 | 2981 | |
| 3027 | if (symtab.strsize > self.strtab.items.len and self.target.?.cpu.arch == .x86_64) { | |
| 2982 | if (symtab.strsize > self.strtab.size() and self.target.?.cpu.arch == .x86_64) { | |
| 3028 | 2983 | // This is the last section, so we need to pad it out. |
| 3029 | 2984 | try self.file.?.pwriteAll(&[_]u8{0}, seg.inner.fileoff + seg.inner.filesize - 1); |
| 3030 | 2985 | } |
| ... | ... | @@ -3173,26 +3128,6 @@ fn writeHeader(self: *Zld) !void { |
| 3173 | 3128 | try self.file.?.pwriteAll(mem.asBytes(&header), 0); |
| 3174 | 3129 | } |
| 3175 | 3130 | |
| 3176 | fn makeString(self: *Zld, bytes: []const u8) !u32 { | |
| 3177 | if (self.strtab_dir.get(bytes)) |offset| { | |
| 3178 | log.debug("reusing '{s}' from string table at offset 0x{x}", .{ bytes, offset }); | |
| 3179 | return offset; | |
| 3180 | } | |
| 3181 | ||
| 3182 | try self.strtab.ensureCapacity(self.allocator, self.strtab.items.len + bytes.len + 1); | |
| 3183 | const offset = @intCast(u32, self.strtab.items.len); | |
| 3184 | log.debug("writing new string '{s}' into string table at offset 0x{x}", .{ bytes, offset }); | |
| 3185 | self.strtab.appendSliceAssumeCapacity(bytes); | |
| 3186 | self.strtab.appendAssumeCapacity(0); | |
| 3187 | try self.strtab_dir.putNoClobber(self.allocator, try self.allocator.dupe(u8, bytes), offset); | |
| 3188 | return offset; | |
| 3189 | } | |
| 3190 | ||
| 3191 | fn getString(self: *const Zld, str_off: u32) []const u8 { | |
| 3192 | assert(str_off < self.strtab.items.len); | |
| 3193 | return mem.spanZ(@ptrCast([*:0]const u8, self.strtab.items.ptr + str_off)); | |
| 3194 | } | |
| 3195 | ||
| 3196 | 3131 | pub fn parseName(name: *const [16]u8) []const u8 { |
| 3197 | 3132 | const len = mem.indexOfScalar(u8, name, @as(u8, 0)) orelse name.len; |
| 3198 | 3133 | return name[0..len]; |