authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-23 18:18:03+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-23 23:09:47+02:00
log9b6f39c3a020ab2a917bc32ec9164c95b3a8a3b5
treeb0ef5b9d280db4316b6834e0add2726ac1e92184
parent4bf27da6a6f6cf476ca907dd661c0c40c7c68286

elf: when looking for system libs, check .so and .a, and access path


1 files changed, 37 insertions(+), 26 deletions(-)

src/link/Elf.zig+37-26
...@@ -1392,11 +1392,33 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1392,11 +1392,33 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1392 if (self.base.options.libc_installation) |lc| {1392 if (self.base.options.libc_installation) |lc| {
1393 const flags = target_util.libcFullLinkFlags(target);1393 const flags = target_util.libcFullLinkFlags(target);
1394 try system_libs.ensureUnusedCapacity(flags.len);1394 try system_libs.ensureUnusedCapacity(flags.len);
1395
1396 var test_path = std.ArrayList(u8).init(arena);
1397 var checked_paths = std.ArrayList([]const u8).init(arena);
1398
1395 for (flags) |flag| {1399 for (flags) |flag| {
1396 const lib_path = try std.fmt.allocPrint(arena, "{s}{c}lib{s}.so", .{1400 checked_paths.clearRetainingCapacity();
1397 lc.crt_dir.?, fs.path.sep, flag["-l".len..],1401 const lib_name = flag["-l".len..];
1398 });1402
1399 system_libs.appendAssumeCapacity(.{ .path = lib_path });1403 success: {
1404 if (!self.isStatic()) {
1405 if (try self.accessLibPath(&test_path, &checked_paths, lc.crt_dir.?, lib_name, .Dynamic))
1406 break :success;
1407 }
1408 if (try self.accessLibPath(&test_path, &checked_paths, lc.crt_dir.?, lib_name, .Static))
1409 break :success;
1410
1411 try self.reportMissingLibraryError(
1412 checked_paths.items,
1413 "missing system library: '{s}' was not found",
1414 .{lib_name},
1415 );
1416
1417 continue;
1418 }
1419
1420 const resolved_path = try arena.dupe(u8, test_path.items);
1421 system_libs.appendAssumeCapacity(.{ .path = resolved_path });
1400 }1422 }
1401 } else if (target.isGnuLibC()) {1423 } else if (target.isGnuLibC()) {
1402 try system_libs.ensureUnusedCapacity(glibc.libs.len + 1);1424 try system_libs.ensureUnusedCapacity(glibc.libs.len + 1);
...@@ -1422,10 +1444,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1422,10 +1444,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
14221444
1423 for (system_libs.items) |lib| {1445 for (system_libs.items) |lib| {
1424 var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined };1446 var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined };
1425 const in_file = std.fs.cwd().openFile(lib.path, .{}) catch |err| {1447 const in_file = try std.fs.cwd().openFile(lib.path, .{});
1426 try self.handleAndReportParseError(lib.path, err, &parse_ctx);
1427 continue;
1428 };
1429 defer in_file.close();1448 defer in_file.close();
1430 self.parseLibrary(in_file, lib, false, &parse_ctx) catch |err|1449 self.parseLibrary(in_file, lib, false, &parse_ctx) catch |err|
1431 try self.handleAndReportParseError(lib.path, err, &parse_ctx);1450 try self.handleAndReportParseError(lib.path, err, &parse_ctx);
...@@ -1832,15 +1851,6 @@ fn parseLdScript(self: *Elf, in_file: std.fs.File, lib: SystemLib, ctx: *ParseEr...@@ -1832,15 +1851,6 @@ fn parseLdScript(self: *Elf, in_file: std.fs.File, lib: SystemLib, ctx: *ParseEr
1832 if (try self.accessLibPath(&test_path, &checked_paths, lib_dir, lib_name, .Static))1851 if (try self.accessLibPath(&test_path, &checked_paths, lib_dir, lib_name, .Static))
1833 break :success;1852 break :success;
1834 }1853 }
1835
1836 try self.reportMissingLibraryError(
1837 checked_paths.items,
1838 "missing library dependency: GNU ld script '{s}' requires '{s}', but file not found",
1839 .{
1840 lib.path,
1841 scr_obj.path,
1842 },
1843 );
1844 } else {1854 } else {
1845 var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;1855 var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;
1846 if (fs.realpath(scr_obj.path, &buffer)) |path| {1856 if (fs.realpath(scr_obj.path, &buffer)) |path| {
...@@ -1854,16 +1864,17 @@ fn parseLdScript(self: *Elf, in_file: std.fs.File, lib: SystemLib, ctx: *ParseEr...@@ -1854,16 +1864,17 @@ fn parseLdScript(self: *Elf, in_file: std.fs.File, lib: SystemLib, ctx: *ParseEr
1854 if (try self.accessLibPath(&test_path, &checked_paths, lib_dir, scr_obj.path, null))1864 if (try self.accessLibPath(&test_path, &checked_paths, lib_dir, scr_obj.path, null))
1855 break :success;1865 break :success;
1856 }1866 }
1857
1858 try self.reportMissingLibraryError(
1859 checked_paths.items,
1860 "missing library dependency: GNU ld script '{s}' requires '{s}', but file not found",
1861 .{
1862 lib.path,
1863 scr_obj.path,
1864 },
1865 );
1866 }1867 }
1868
1869 try self.reportMissingLibraryError(
1870 checked_paths.items,
1871 "missing library dependency: GNU ld script '{s}' requires '{s}', but file not found",
1872 .{
1873 lib.path,
1874 scr_obj.path,
1875 },
1876 );
1877 continue;
1867 }1878 }
18681879
1869 const full_path = test_path.items;1880 const full_path = test_path.items;