| ... | ... | @@ -37,10 +37,7 @@ id: ?Id = null, |
| 37 | 37 | /// a symbol is referenced by an object file. |
| 38 | 38 | symbols: std.StringArrayHashMapUnmanaged(void) = .{}, |
| 39 | 39 | |
| 40 | | // TODO we should keep track of already parsed dylibs so that |
| 41 | | // we don't unnecessarily reparse them again. |
| 42 | | // TODO add dylib dep analysis and extraction for .dylib files. |
| 43 | | dylibs: std.ArrayListUnmanaged(*Dylib) = .{}, |
| 40 | dependent_libs: std.StringArrayHashMapUnmanaged(void) = .{}, |
| 44 | 41 | |
| 45 | 42 | pub const Id = struct { |
| 46 | 43 | name: []const u8, |
| ... | ... | @@ -66,7 +63,7 @@ pub fn createAndParseFromPath( |
| 66 | 63 | path: []const u8, |
| 67 | 64 | syslibroot: ?[]const u8, |
| 68 | 65 | recurse_libs: bool, |
| 69 | | ) Error!?*Dylib { |
| 66 | ) Error!?[]*Dylib { |
| 70 | 67 | const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) { |
| 71 | 68 | error.FileNotFound => return null, |
| 72 | 69 | else => |e| return e, |
| ... | ... | @@ -87,7 +84,7 @@ pub fn createAndParseFromPath( |
| 87 | 84 | .syslibroot = syslibroot, |
| 88 | 85 | }; |
| 89 | 86 | |
| 90 | | dylib.parse(recurse_libs) catch |err| switch (err) { |
| 87 | dylib.parse() catch |err| switch (err) { |
| 91 | 88 | error.EndOfStream, error.NotDylib => { |
| 92 | 89 | try file.seekTo(0); |
| 93 | 90 | |
| ... | ... | @@ -98,12 +95,20 @@ pub fn createAndParseFromPath( |
| 98 | 95 | }; |
| 99 | 96 | defer lib_stub.deinit(); |
| 100 | 97 | |
| 101 | | try dylib.parseFromStub(lib_stub, recurse_libs); |
| 98 | try dylib.parseFromStub(lib_stub); |
| 102 | 99 | }, |
| 103 | 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 | 114 | pub fn deinit(self: *Dylib) void { |
| ... | ... | @@ -116,7 +121,11 @@ pub fn deinit(self: *Dylib) void { |
| 116 | 121 | self.allocator.free(key); |
| 117 | 122 | } |
| 118 | 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 | 130 | if (self.name) |name| { |
| 122 | 131 | self.allocator.free(name); |
| ... | ... | @@ -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 | 146 | log.debug("parsing shared library '{s}'", .{self.name.?}); |
| 138 | 147 | |
| 139 | 148 | var reader = self.file.?.reader(); |
| ... | ... | @@ -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 | 254 | fn addObjCClassSymbols(self: *Dylib, sym_name: []const u8) !void { |
| 239 | 255 | const expanded = &[_][]const u8{ |
| 240 | 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 | 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 | 267 | if (lib_stub.inner.len == 0) return error.EmptyStubFile; |
| 252 | 268 | |
| 253 | 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 | 286 | for (lib_stub.inner) |stub| { |
| 271 | 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 | 300 | if (stub.exports) |exports| { |
| 274 | 301 | for (exports) |exp| { |
| 275 | 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 | 341 | } |
| 315 | 342 | } |
| 316 | 343 | } |
| 344 | } |
| 317 | 345 | |
| 318 | | for (lib_stub.inner) |stub| { |
| 319 | | if (!hasTarget(stub.targets, target_string)) continue; |
| 320 | | |
| 321 | | if (stub.reexported_libraries) |reexports| reexports: { |
| 322 | | if (!recurse_libs) break :reexports; |
| 346 | pub fn parseDependentLibs(self: *Dylib, out: *std.ArrayList(*Dylib)) !void { |
| 347 | outer: for (self.dependent_libs.keys()) |lib| { |
| 348 | const dirname = fs.path.dirname(lib) orelse { |
| 349 | log.warn("unable to resolve dependency {s}", .{lib}); |
| 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| { |
| 325 | | if (!hasTarget(reexp.targets, target_string)) continue; |
| 382 | try out.appendSlice(dylibs); |
| 326 | 383 | |
| 327 | | outer: for (reexp.libraries) |lib| { |
| 328 | | const dirname = fs.path.dirname(lib) orelse { |
| 329 | | 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 | | } |
| 384 | continue :outer; |
| 385 | } else { |
| 386 | log.warn("unable to resolve dependency {s}", .{lib}); |
| 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 | 391 | pub fn createProxy(self: *Dylib, sym_name: []const u8) !?*Symbol { |
| 381 | 392 | if (!self.symbols.contains(sym_name)) return null; |
| 382 | 393 | |