| ... | @@ -1353,10 +1353,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node | ... | @@ -1353,10 +1353,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1353 | } | 1353 | } |
| 1354 | | 1354 | |
| 1355 | for (positionals.items) |obj| { | 1355 | for (positionals.items) |obj| { |
| 1356 | const in_file = try std.fs.cwd().openFile(obj.path, .{}); | | |
| 1357 | defer in_file.close(); | | |
| 1358 | var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined }; | 1356 | var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined }; |
| 1359 | self.parsePositional(in_file, obj.path, obj.must_link, &parse_ctx) catch |err| | 1357 | self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch |err| |
| 1360 | try self.handleAndReportParseError(obj.path, err, &parse_ctx); | 1358 | try self.handleAndReportParseError(obj.path, err, &parse_ctx); |
| 1361 | } | 1359 | } |
| 1362 | | 1360 | |
| ... | @@ -1437,9 +1435,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node | ... | @@ -1437,9 +1435,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1437 | | 1435 | |
| 1438 | for (system_libs.items) |lib| { | 1436 | for (system_libs.items) |lib| { |
| 1439 | var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined }; | 1437 | var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined }; |
| 1440 | const in_file = try std.fs.cwd().openFile(lib.path, .{}); | 1438 | self.parseLibrary(lib, false, &parse_ctx) catch |err| |
| 1441 | defer in_file.close(); | | |
| 1442 | self.parseLibrary(in_file, lib, false, &parse_ctx) catch |err| | | |
| 1443 | try self.handleAndReportParseError(lib.path, err, &parse_ctx); | 1439 | try self.handleAndReportParseError(lib.path, err, &parse_ctx); |
| 1444 | } | 1440 | } |
| 1445 | | 1441 | |
| ... | @@ -1456,10 +1452,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node | ... | @@ -1456,10 +1452,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1456 | if (csu.crtn) |v| try positionals.append(.{ .path = v }); | 1452 | if (csu.crtn) |v| try positionals.append(.{ .path = v }); |
| 1457 | | 1453 | |
| 1458 | for (positionals.items) |obj| { | 1454 | for (positionals.items) |obj| { |
| 1459 | const in_file = try std.fs.cwd().openFile(obj.path, .{}); | | |
| 1460 | defer in_file.close(); | | |
| 1461 | var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined }; | 1455 | var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined }; |
| 1462 | self.parsePositional(in_file, obj.path, obj.must_link, &parse_ctx) catch |err| | 1456 | self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch |err| |
| 1463 | try self.handleAndReportParseError(obj.path, err, &parse_ctx); | 1457 | try self.handleAndReportParseError(obj.path, err, &parse_ctx); |
| 1464 | } | 1458 | } |
| 1465 | | 1459 | |
| ... | @@ -1679,51 +1673,40 @@ const ParseError = error{ | ... | @@ -1679,51 +1673,40 @@ const ParseError = error{ |
| 1679 | InvalidCharacter, | 1673 | InvalidCharacter, |
| 1680 | } || LdScript.Error || std.os.AccessError || std.os.SeekError || std.fs.File.OpenError || std.fs.File.ReadError; | 1674 | } || LdScript.Error || std.os.AccessError || std.os.SeekError || std.fs.File.OpenError || std.fs.File.ReadError; |
| 1681 | | 1675 | |
| 1682 | fn parsePositional( | 1676 | fn parsePositional(self: *Elf, path: []const u8, must_link: bool, ctx: *ParseErrorCtx) ParseError!void { |
| 1683 | self: *Elf, | | |
| 1684 | in_file: std.fs.File, | | |
| 1685 | path: []const u8, | | |
| 1686 | must_link: bool, | | |
| 1687 | ctx: *ParseErrorCtx, | | |
| 1688 | ) ParseError!void { | | |
| 1689 | const tracy = trace(@src()); | 1677 | const tracy = trace(@src()); |
| 1690 | defer tracy.end(); | 1678 | defer tracy.end(); |
| 1691 | | 1679 | if (try Object.isObject(path)) { |
| 1692 | if (Object.isObject(in_file)) { | 1680 | try self.parseObject(path, ctx); |
| 1693 | try self.parseObject(in_file, path, ctx); | | |
| 1694 | } else { | 1681 | } else { |
| 1695 | try self.parseLibrary(in_file, .{ .path = path }, must_link, ctx); | 1682 | try self.parseLibrary(.{ .path = path }, must_link, ctx); |
| 1696 | } | 1683 | } |
| 1697 | } | 1684 | } |
| 1698 | | 1685 | |
| 1699 | fn parseLibrary( | 1686 | fn parseLibrary(self: *Elf, lib: SystemLib, must_link: bool, ctx: *ParseErrorCtx) ParseError!void { |
| 1700 | self: *Elf, | | |
| 1701 | in_file: std.fs.File, | | |
| 1702 | lib: SystemLib, | | |
| 1703 | must_link: bool, | | |
| 1704 | ctx: *ParseErrorCtx, | | |
| 1705 | ) ParseError!void { | | |
| 1706 | const tracy = trace(@src()); | 1687 | const tracy = trace(@src()); |
| 1707 | defer tracy.end(); | 1688 | defer tracy.end(); |
| 1708 | | 1689 | |
| 1709 | if (Archive.isArchive(in_file)) { | 1690 | if (try Archive.isArchive(lib.path)) { |
| 1710 | try self.parseArchive(in_file, lib.path, must_link, ctx); | 1691 | try self.parseArchive(lib.path, must_link, ctx); |
| 1711 | } else if (SharedObject.isSharedObject(in_file)) { | 1692 | } else if (try SharedObject.isSharedObject(lib.path)) { |
| 1712 | try self.parseSharedObject(in_file, lib, ctx); | 1693 | try self.parseSharedObject(lib, ctx); |
| 1713 | } else { | 1694 | } else { |
| 1714 | // TODO if the script has a top-level comment identifying it as GNU ld script, | 1695 | // TODO if the script has a top-level comment identifying it as GNU ld script, |
| 1715 | // then report parse errors. Otherwise return UnknownFileType. | 1696 | // then report parse errors. Otherwise return UnknownFileType. |
| 1716 | self.parseLdScript(in_file, lib, ctx) catch |err| switch (err) { | 1697 | self.parseLdScript(lib, ctx) catch |err| switch (err) { |
| 1717 | else => return error.UnknownFileType, | 1698 | else => return error.UnknownFileType, |
| 1718 | }; | 1699 | }; |
| 1719 | } | 1700 | } |
| 1720 | } | 1701 | } |
| 1721 | | 1702 | |
| 1722 | fn parseObject(self: *Elf, in_file: std.fs.File, path: []const u8, ctx: *ParseErrorCtx) ParseError!void { | 1703 | fn parseObject(self: *Elf, path: []const u8, ctx: *ParseErrorCtx) ParseError!void { |
| 1723 | const tracy = trace(@src()); | 1704 | const tracy = trace(@src()); |
| 1724 | defer tracy.end(); | 1705 | defer tracy.end(); |
| 1725 | | 1706 | |
| 1726 | const gpa = self.base.allocator; | 1707 | const gpa = self.base.allocator; |
| | 1708 | const in_file = try std.fs.cwd().openFile(path, .{}); |
| | 1709 | defer in_file.close(); |
| 1727 | const data = try in_file.readToEndAlloc(gpa, std.math.maxInt(u32)); | 1710 | const data = try in_file.readToEndAlloc(gpa, std.math.maxInt(u32)); |
| 1728 | const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); | 1711 | const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); |
| 1729 | self.files.set(index, .{ .object = .{ | 1712 | self.files.set(index, .{ .object = .{ |
| ... | @@ -1740,17 +1723,13 @@ fn parseObject(self: *Elf, in_file: std.fs.File, path: []const u8, ctx: *ParseEr | ... | @@ -1740,17 +1723,13 @@ fn parseObject(self: *Elf, in_file: std.fs.File, path: []const u8, ctx: *ParseEr |
| 1740 | if (ctx.detected_cpu_arch != self.base.options.target.cpu.arch) return error.InvalidCpuArch; | 1723 | if (ctx.detected_cpu_arch != self.base.options.target.cpu.arch) return error.InvalidCpuArch; |
| 1741 | } | 1724 | } |
| 1742 | | 1725 | |
| 1743 | fn parseArchive( | 1726 | fn parseArchive(self: *Elf, path: []const u8, must_link: bool, ctx: *ParseErrorCtx) ParseError!void { |
| 1744 | self: *Elf, | | |
| 1745 | in_file: std.fs.File, | | |
| 1746 | path: []const u8, | | |
| 1747 | must_link: bool, | | |
| 1748 | ctx: *ParseErrorCtx, | | |
| 1749 | ) ParseError!void { | | |
| 1750 | const tracy = trace(@src()); | 1727 | const tracy = trace(@src()); |
| 1751 | defer tracy.end(); | 1728 | defer tracy.end(); |
| 1752 | | 1729 | |
| 1753 | const gpa = self.base.allocator; | 1730 | const gpa = self.base.allocator; |
| | 1731 | const in_file = try std.fs.cwd().openFile(path, .{}); |
| | 1732 | defer in_file.close(); |
| 1754 | const data = try in_file.readToEndAlloc(gpa, std.math.maxInt(u32)); | 1733 | const data = try in_file.readToEndAlloc(gpa, std.math.maxInt(u32)); |
| 1755 | var archive = Archive{ .path = try gpa.dupe(u8, path), .data = data }; | 1734 | var archive = Archive{ .path = try gpa.dupe(u8, path), .data = data }; |
| 1756 | defer archive.deinit(gpa); | 1735 | defer archive.deinit(gpa); |
| ... | @@ -1773,16 +1752,13 @@ fn parseArchive( | ... | @@ -1773,16 +1752,13 @@ fn parseArchive( |
| 1773 | } | 1752 | } |
| 1774 | } | 1753 | } |
| 1775 | | 1754 | |
| 1776 | fn parseSharedObject( | 1755 | fn parseSharedObject(self: *Elf, lib: SystemLib, ctx: *ParseErrorCtx) ParseError!void { |
| 1777 | self: *Elf, | | |
| 1778 | in_file: std.fs.File, | | |
| 1779 | lib: SystemLib, | | |
| 1780 | ctx: *ParseErrorCtx, | | |
| 1781 | ) ParseError!void { | | |
| 1782 | const tracy = trace(@src()); | 1756 | const tracy = trace(@src()); |
| 1783 | defer tracy.end(); | 1757 | defer tracy.end(); |
| 1784 | | 1758 | |
| 1785 | const gpa = self.base.allocator; | 1759 | const gpa = self.base.allocator; |
| | 1760 | const in_file = try std.fs.cwd().openFile(lib.path, .{}); |
| | 1761 | defer in_file.close(); |
| 1786 | const data = try in_file.readToEndAlloc(gpa, std.math.maxInt(u32)); | 1762 | const data = try in_file.readToEndAlloc(gpa, std.math.maxInt(u32)); |
| 1787 | const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); | 1763 | const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); |
| 1788 | self.files.set(index, .{ .shared_object = .{ | 1764 | self.files.set(index, .{ .shared_object = .{ |
| ... | @@ -1801,11 +1777,13 @@ fn parseSharedObject( | ... | @@ -1801,11 +1777,13 @@ fn parseSharedObject( |
| 1801 | if (ctx.detected_cpu_arch != self.base.options.target.cpu.arch) return error.InvalidCpuArch; | 1777 | if (ctx.detected_cpu_arch != self.base.options.target.cpu.arch) return error.InvalidCpuArch; |
| 1802 | } | 1778 | } |
| 1803 | | 1779 | |
| 1804 | fn parseLdScript(self: *Elf, in_file: std.fs.File, lib: SystemLib, ctx: *ParseErrorCtx) ParseError!void { | 1780 | fn parseLdScript(self: *Elf, lib: SystemLib, ctx: *ParseErrorCtx) ParseError!void { |
| 1805 | const tracy = trace(@src()); | 1781 | const tracy = trace(@src()); |
| 1806 | defer tracy.end(); | 1782 | defer tracy.end(); |
| 1807 | | 1783 | |
| 1808 | const gpa = self.base.allocator; | 1784 | const gpa = self.base.allocator; |
| | 1785 | const in_file = try std.fs.cwd().openFile(lib.path, .{}); |
| | 1786 | defer in_file.close(); |
| 1809 | const data = try in_file.readToEndAlloc(gpa, std.math.maxInt(u32)); | 1787 | const data = try in_file.readToEndAlloc(gpa, std.math.maxInt(u32)); |
| 1810 | defer gpa.free(data); | 1788 | defer gpa.free(data); |
| 1811 | | 1789 | |
| ... | @@ -1871,11 +1849,8 @@ fn parseLdScript(self: *Elf, in_file: std.fs.File, lib: SystemLib, ctx: *ParseEr | ... | @@ -1871,11 +1849,8 @@ fn parseLdScript(self: *Elf, in_file: std.fs.File, lib: SystemLib, ctx: *ParseEr |
| 1871 | } | 1849 | } |
| 1872 | | 1850 | |
| 1873 | const full_path = test_path.items; | 1851 | const full_path = test_path.items; |
| 1874 | const scr_file = try std.fs.cwd().openFile(full_path, .{}); | | |
| 1875 | defer scr_file.close(); | | |
| 1876 | | | |
| 1877 | var scr_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined }; | 1852 | var scr_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined }; |
| 1878 | self.parseLibrary(scr_file, .{ | 1853 | self.parseLibrary(.{ |
| 1879 | .needed = scr_obj.needed, | 1854 | .needed = scr_obj.needed, |
| 1880 | .path = full_path, | 1855 | .path = full_path, |
| 1881 | }, false, &scr_ctx) catch |err| try self.handleAndReportParseError(full_path, err, &scr_ctx); | 1856 | }, false, &scr_ctx) catch |err| try self.handleAndReportParseError(full_path, err, &scr_ctx); |
| ... | @@ -1893,14 +1868,16 @@ fn accessLibPath( | ... | @@ -1893,14 +1868,16 @@ fn accessLibPath( |
| 1893 | const sep = fs.path.sep_str; | 1868 | const sep = fs.path.sep_str; |
| 1894 | const target = self.base.options.target; | 1869 | const target = self.base.options.target; |
| 1895 | test_path.clearRetainingCapacity(); | 1870 | test_path.clearRetainingCapacity(); |
| | 1871 | const prefix = if (link_mode != null) "lib" else ""; |
| | 1872 | const suffix = if (link_mode) |mode| switch (mode) { |
| | 1873 | .Static => target.staticLibSuffix(), |
| | 1874 | .Dynamic => target.dynamicLibSuffix(), |
| | 1875 | } else ""; |
| 1896 | try test_path.writer().print("{s}" ++ sep ++ "{s}{s}{s}", .{ | 1876 | try test_path.writer().print("{s}" ++ sep ++ "{s}{s}{s}", .{ |
| 1897 | lib_dir_path, | 1877 | lib_dir_path, |
| 1898 | target.libPrefix(), | 1878 | prefix, |
| 1899 | lib_name, | 1879 | lib_name, |
| 1900 | if (link_mode) |mode| switch (mode) { | 1880 | suffix, |
| 1901 | .Static => target.staticLibSuffix(), | | |
| 1902 | .Dynamic => target.dynamicLibSuffix(), | | |
| 1903 | } else "", | | |
| 1904 | }); | 1881 | }); |
| 1905 | if (checked_paths) |cpaths| { | 1882 | if (checked_paths) |cpaths| { |
| 1906 | try cpaths.append(try self.base.allocator.dupe(u8, test_path.items)); | 1883 | try cpaths.append(try self.base.allocator.dupe(u8, test_path.items)); |