| ... | @@ -37,10 +37,7 @@ id: ?Id = null, | ... | @@ -37,10 +37,7 @@ id: ?Id = null, |
| 37 | /// a symbol is referenced by an object file. | 37 | /// a symbol is referenced by an object file. |
| 38 | symbols: std.StringArrayHashMapUnmanaged(void) = .{}, | 38 | symbols: std.StringArrayHashMapUnmanaged(void) = .{}, |
| 39 | | 39 | |
| 40 | // TODO we should keep track of already parsed dylibs so that | 40 | dependent_libs: std.StringArrayHashMapUnmanaged(void) = .{}, |
| 41 | // we don't unnecessarily reparse them again. | | |
| 42 | // TODO add dylib dep analysis and extraction for .dylib files. | | |
| 43 | dylibs: std.ArrayListUnmanaged(*Dylib) = .{}, | | |
| 44 | | 41 | |
| 45 | pub const Id = struct { | 42 | pub const Id = struct { |
| 46 | name: []const u8, | 43 | name: []const u8, |
| ... | @@ -66,7 +63,7 @@ pub fn createAndParseFromPath( | ... | @@ -66,7 +63,7 @@ pub fn createAndParseFromPath( |
| 66 | path: []const u8, | 63 | path: []const u8, |
| 67 | syslibroot: ?[]const u8, | 64 | syslibroot: ?[]const u8, |
| 68 | recurse_libs: bool, | 65 | recurse_libs: bool, |
| 69 | ) Error!?*Dylib { | 66 | ) Error!?[]*Dylib { |
| 70 | const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) { | 67 | const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) { |
| 71 | error.FileNotFound => return null, | 68 | error.FileNotFound => return null, |
| 72 | else => |e| return e, | 69 | else => |e| return e, |
| ... | @@ -87,7 +84,7 @@ pub fn createAndParseFromPath( | ... | @@ -87,7 +84,7 @@ pub fn createAndParseFromPath( |
| 87 | .syslibroot = syslibroot, | 84 | .syslibroot = syslibroot, |
| 88 | }; | 85 | }; |
| 89 | | 86 | |
| 90 | dylib.parse(recurse_libs) catch |err| switch (err) { | 87 | dylib.parse() catch |err| switch (err) { |
| 91 | error.EndOfStream, error.NotDylib => { | 88 | error.EndOfStream, error.NotDylib => { |
| 92 | try file.seekTo(0); | 89 | try file.seekTo(0); |
| 93 | | 90 | |
| ... | @@ -98,12 +95,20 @@ pub fn createAndParseFromPath( | ... | @@ -98,12 +95,20 @@ pub fn createAndParseFromPath( |
| 98 | }; | 95 | }; |
| 99 | defer lib_stub.deinit(); | 96 | defer lib_stub.deinit(); |
| 100 | | 97 | |
| 101 | try dylib.parseFromStub(lib_stub, recurse_libs); | 98 | try dylib.parseFromStub(lib_stub); |
| 102 | }, | 99 | }, |
| 103 | else => |e| return e, | 100 | else => |e| return e, |
| 104 | }; | 101 | }; |
| 105 | | 102 | |
| 106 | return dylib; | 103 | var dylibs = std.ArrayList(*Dylib).init(allocator); |
| | 104 | defer dylibs.deinit(); |
| | 105 | try dylibs.append(dylib); |
| | 106 | |
| | 107 | if (recurse_libs) { |
| | 108 | try dylib.parseDependentLibs(&dylibs); |
| | 109 | } |
| | 110 | |
| | 111 | return dylibs.toOwnedSlice(); |
| 107 | } | 112 | } |
| 108 | | 113 | |
| 109 | pub fn deinit(self: *Dylib) void { | 114 | pub fn deinit(self: *Dylib) void { |
| ... | @@ -116,7 +121,11 @@ pub fn deinit(self: *Dylib) void { | ... | @@ -116,7 +121,11 @@ pub fn deinit(self: *Dylib) void { |
| 116 | self.allocator.free(key); | 121 | self.allocator.free(key); |
| 117 | } | 122 | } |
| 118 | self.symbols.deinit(self.allocator); | 123 | self.symbols.deinit(self.allocator); |
| 119 | self.dylibs.deinit(self.allocator); | 124 | |
| | 125 | for (self.dependent_libs.keys()) |key| { |
| | 126 | self.allocator.free(key); |
| | 127 | } |
| | 128 | self.dependent_libs.deinit(self.allocator); |
| 120 | | 129 | |
| 121 | if (self.name) |name| { | 130 | if (self.name) |name| { |
| 122 | self.allocator.free(name); | 131 | self.allocator.free(name); |
| ... | @@ -133,7 +142,7 @@ pub fn closeFile(self: Dylib) void { | ... | @@ -133,7 +142,7 @@ pub fn closeFile(self: Dylib) void { |
| 133 | } | 142 | } |
| 134 | } | 143 | } |
| 135 | | 144 | |
| 136 | pub fn parse(self: *Dylib, recurse_libs: bool) !void { | 145 | pub fn parse(self: *Dylib) !void { |
| 137 | log.debug("parsing shared library '{s}'", .{self.name.?}); | 146 | log.debug("parsing shared library '{s}'", .{self.name.?}); |
| 138 | | 147 | |
| 139 | var reader = self.file.?.reader(); | 148 | var reader = self.file.?.reader(); |
| ... | @@ -235,6 +244,13 @@ fn parseSymbols(self: *Dylib) !void { | ... | @@ -235,6 +244,13 @@ fn parseSymbols(self: *Dylib) !void { |
| 235 | } | 244 | } |
| 236 | } | 245 | } |
| 237 | | 246 | |
| | 247 | fn hasTarget(targets: []const []const u8, target: []const u8) bool { |
| | 248 | for (targets) |t| { |
| | 249 | if (mem.eql(u8, t, target)) return true; |
| | 250 | } |
| | 251 | return false; |
| | 252 | } |
| | 253 | |
| 238 | fn addObjCClassSymbols(self: *Dylib, sym_name: []const u8) !void { | 254 | fn addObjCClassSymbols(self: *Dylib, sym_name: []const u8) !void { |
| 239 | const expanded = &[_][]const u8{ | 255 | const expanded = &[_][]const u8{ |
| 240 | try std.fmt.allocPrint(self.allocator, "_OBJC_CLASS_$_{s}", .{sym_name}), | 256 | try std.fmt.allocPrint(self.allocator, "_OBJC_CLASS_$_{s}", .{sym_name}), |
| ... | @@ -247,7 +263,7 @@ fn addObjCClassSymbols(self: *Dylib, sym_name: []const u8) !void { | ... | @@ -247,7 +263,7 @@ fn addObjCClassSymbols(self: *Dylib, sym_name: []const u8) !void { |
| 247 | } | 263 | } |
| 248 | } | 264 | } |
| 249 | | 265 | |
| 250 | pub fn parseFromStub(self: *Dylib, lib_stub: LibStub, recurse_libs: bool) !void { | 266 | pub fn parseFromStub(self: *Dylib, lib_stub: LibStub) !void { |
| 251 | if (lib_stub.inner.len == 0) return error.EmptyStubFile; | 267 | if (lib_stub.inner.len == 0) return error.EmptyStubFile; |
| 252 | | 268 | |
| 253 | log.debug("parsing shared library from stub '{s}'", .{self.name.?}); | 269 | log.debug("parsing shared library from stub '{s}'", .{self.name.?}); |
| ... | @@ -270,6 +286,17 @@ pub fn parseFromStub(self: *Dylib, lib_stub: LibStub, recurse_libs: bool) !void | ... | @@ -270,6 +286,17 @@ pub fn parseFromStub(self: *Dylib, lib_stub: LibStub, recurse_libs: bool) !void |
| 270 | for (lib_stub.inner) |stub| { | 286 | for (lib_stub.inner) |stub| { |
| 271 | if (!hasTarget(stub.targets, target_string)) continue; | 287 | if (!hasTarget(stub.targets, target_string)) continue; |
| 272 | | 288 | |
| | 289 | if (stub.reexported_libraries) |reexports| { |
| | 290 | for (reexports) |reexp| { |
| | 291 | if (!hasTarget(reexp.targets, target_string)) continue; |
| | 292 | |
| | 293 | try self.dependent_libs.ensureUnusedCapacity(self.allocator, reexp.libraries.len); |
| | 294 | for (reexp.libraries) |lib| { |
| | 295 | self.dependent_libs.putAssumeCapacity(try self.allocator.dupe(u8, lib), {}); |
| | 296 | } |
| | 297 | } |
| | 298 | } |
| | 299 | |
| 273 | if (stub.exports) |exports| { | 300 | if (stub.exports) |exports| { |
| 274 | for (exports) |exp| { | 301 | for (exports) |exp| { |
| 275 | if (!hasTarget(exp.targets, target_string)) continue; | 302 | if (!hasTarget(exp.targets, target_string)) continue; |
| ... | @@ -314,69 +341,53 @@ pub fn parseFromStub(self: *Dylib, lib_stub: LibStub, recurse_libs: bool) !void | ... | @@ -314,69 +341,53 @@ pub fn parseFromStub(self: *Dylib, lib_stub: LibStub, recurse_libs: bool) !void |
| 314 | } | 341 | } |
| 315 | } | 342 | } |
| 316 | } | 343 | } |
| | 344 | } |
| 317 | | 345 | |
| 318 | for (lib_stub.inner) |stub| { | 346 | pub fn parseDependentLibs(self: *Dylib, out: *std.ArrayList(*Dylib)) !void { |
| 319 | if (!hasTarget(stub.targets, target_string)) continue; | 347 | outer: for (self.dependent_libs.keys()) |lib| { |
| 320 | | 348 | const dirname = fs.path.dirname(lib) orelse { |
| 321 | if (stub.reexported_libraries) |reexports| reexports: { | 349 | log.warn("unable to resolve dependency {s}", .{lib}); |
| 322 | if (!recurse_libs) break :reexports; | 350 | continue; |
| | 351 | }; |
| | 352 | const filename = fs.path.basename(lib); |
| | 353 | const without_ext = if (mem.lastIndexOfScalar(u8, filename, '.')) |index| |
| | 354 | filename[0..index] |
| | 355 | else |
| | 356 | filename; |
| | 357 | |
| | 358 | for (&[_][]const u8{ "dylib", "tbd" }) |ext| { |
| | 359 | const with_ext = try std.fmt.allocPrint(self.allocator, "{s}.{s}", .{ |
| | 360 | without_ext, |
| | 361 | ext, |
| | 362 | }); |
| | 363 | defer self.allocator.free(with_ext); |
| | 364 | |
| | 365 | const lib_path = if (self.syslibroot) |syslibroot| |
| | 366 | try fs.path.join(self.allocator, &.{ syslibroot, dirname, with_ext }) |
| | 367 | else |
| | 368 | try fs.path.join(self.allocator, &.{ dirname, with_ext }); |
| | 369 | |
| | 370 | log.debug("trying dependency at fully resolved path {s}", .{lib_path}); |
| | 371 | |
| | 372 | const dylibs = (try createAndParseFromPath( |
| | 373 | self.allocator, |
| | 374 | self.arch.?, |
| | 375 | lib_path, |
| | 376 | self.syslibroot, |
| | 377 | true, |
| | 378 | )) orelse { |
| | 379 | continue; |
| | 380 | }; |
| 323 | | 381 | |
| 324 | for (reexports) |reexp| { | 382 | try out.appendSlice(dylibs); |
| 325 | if (!hasTarget(reexp.targets, target_string)) continue; | | |
| 326 | | 383 | |
| 327 | outer: for (reexp.libraries) |lib| { | 384 | continue :outer; |
| 328 | const dirname = fs.path.dirname(lib) orelse { | 385 | } else { |
| 329 | log.warn("unable to resolve dependency {s}", .{lib}); | 386 | log.warn("unable to resolve dependency {s}", .{lib}); |
| 330 | continue; | | |
| 331 | }; | | |
| 332 | const filename = fs.path.basename(lib); | | |
| 333 | const without_ext = if (mem.lastIndexOfScalar(u8, filename, '.')) |index| | | |
| 334 | filename[0..index] | | |
| 335 | else | | |
| 336 | filename; | | |
| 337 | | | |
| 338 | for (&[_][]const u8{ "dylib", "tbd" }) |ext| { | | |
| 339 | const with_ext = try std.fmt.allocPrint(self.allocator, "{s}.{s}", .{ | | |
| 340 | without_ext, | | |
| 341 | ext, | | |
| 342 | }); | | |
| 343 | defer self.allocator.free(with_ext); | | |
| 344 | | | |
| 345 | const lib_path = if (self.syslibroot) |syslibroot| | | |
| 346 | try fs.path.join(self.allocator, &.{ syslibroot, dirname, with_ext }) | | |
| 347 | else | | |
| 348 | try fs.path.join(self.allocator, &.{ dirname, with_ext }); | | |
| 349 | | | |
| 350 | log.debug("trying dependency at fully resolved path {s}", .{lib_path}); | | |
| 351 | | | |
| 352 | const dylib = (try createAndParseFromPath( | | |
| 353 | self.allocator, | | |
| 354 | self.arch.?, | | |
| 355 | lib_path, | | |
| 356 | self.syslibroot, | | |
| 357 | true, | | |
| 358 | )) orelse { | | |
| 359 | continue; | | |
| 360 | }; | | |
| 361 | | | |
| 362 | try self.dylibs.append(self.allocator, dylib); | | |
| 363 | continue :outer; | | |
| 364 | } else { | | |
| 365 | log.warn("unable to resolve dependency {s}", .{lib}); | | |
| 366 | } | | |
| 367 | } | | |
| 368 | } | | |
| 369 | } | 387 | } |
| 370 | } | 388 | } |
| 371 | } | 389 | } |
| 372 | | 390 | |
| 373 | fn hasTarget(targets: []const []const u8, target: []const u8) bool { | | |
| 374 | for (targets) |t| { | | |
| 375 | if (mem.eql(u8, t, target)) return true; | | |
| 376 | } | | |
| 377 | return false; | | |
| 378 | } | | |
| 379 | | | |
| 380 | pub fn createProxy(self: *Dylib, sym_name: []const u8) !?*Symbol { | 391 | pub fn createProxy(self: *Dylib, sym_name: []const u8) !?*Symbol { |
| 381 | if (!self.symbols.contains(sym_name)) return null; | 392 | if (!self.symbols.contains(sym_name)) return null; |
| 382 | | 393 | |