authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-08 14:40:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-08 18:02:59-07:00
log2c41c453b634b3d1f378f6f1b335314f0a4be2de
tree82ee4addc7157c67a314ff123aa36ab294e2c4af
parente1e151df0d948be7464b448c61033d4c1d80d86b

link.Elf: avoid converting rpath data in flush()

The goal is to minimize as much as possible how much logic is inside flush(). So let's start by moving out obvious stuff. This data can be preformatted before flush().

10 files changed, 20 insertions(+), 35 deletions(-)

src/link.zig-1
......@@ -67,7 +67,6 @@ pub const File = struct {
6767 gc_sections: bool,
6868 print_gc_sections: bool,
6969 build_id: std.zig.BuildId,
70 rpath_list: []const []const u8,
7170 allow_shlib_undefined: bool,
7271 stack_size: u64,
7372
src/link/C.zig-1
......@@ -148,7 +148,6 @@ pub fn createEmpty(
148148 .file = file,
149149 .disable_lld_caching = options.disable_lld_caching,
150150 .build_id = options.build_id,
151 .rpath_list = options.rpath_list,
152151 },
153152 };
154153
src/link/Coff.zig-1
......@@ -263,7 +263,6 @@ pub fn createEmpty(
263263 .file = null,
264264 .disable_lld_caching = options.disable_lld_caching,
265265 .build_id = options.build_id,
266 .rpath_list = options.rpath_list,
267266 },
268267 .ptr_width = ptr_width,
269268 .page_size = page_size,
src/link/Elf.zig+13-22
......@@ -1,4 +1,5 @@
11base: link.File,
2rpath_table: std.StringArrayHashMapUnmanaged(void),
23image_base: u64,
34emit_relocs: bool,
45z_nodelete: bool,
......@@ -239,6 +240,11 @@ pub fn createEmpty(
239240 else
240241 try std.fmt.allocPrint(arena, "{s}.o", .{emit.sub_path});
241242
243 var rpath_table: std.StringArrayHashMapUnmanaged(void) = .empty;
244 try rpath_table.entries.resize(arena, options.rpath_list.len);
245 @memcpy(rpath_table.entries.items(.key), options.rpath_list);
246 try rpath_table.reIndex(arena);
247
242248 const self = try arena.create(Elf);
243249 self.* = .{
244250 .base = .{
......@@ -253,8 +259,8 @@ pub fn createEmpty(
253259 .file = null,
254260 .disable_lld_caching = options.disable_lld_caching,
255261 .build_id = options.build_id,
256 .rpath_list = options.rpath_list,
257262 },
263 .rpath_table = rpath_table,
258264 .ptr_width = ptr_width,
259265 .page_size = page_size,
260266 .default_sym_version = default_sym_version,
......@@ -829,14 +835,6 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
829835
830836 if (module_obj_path) |path| try positionals.append(.{ .path = path });
831837
832 // rpaths
833 var rpath_table = std.StringArrayHashMap(void).init(gpa);
834 defer rpath_table.deinit();
835
836 for (self.base.rpath_list) |rpath| {
837 _ = try rpath_table.put(rpath, {});
838 }
839
840838 if (comp.config.any_sanitize_thread) {
841839 try positionals.append(.{ .path = comp.tsan_lib.?.full_object_path });
842840 }
......@@ -1056,7 +1054,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
10561054 try self.initSpecialPhdrs();
10571055 try self.sortShdrs();
10581056
1059 try self.setDynamicSection(rpath_table.keys());
1057 try self.setDynamicSection(self.rpath_table.keys());
10601058 self.sortDynamicSymtab();
10611059 try self.setHashSections();
10621060 try self.setVersionSymtab();
......@@ -1207,9 +1205,8 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
12071205 try argv.appendSlice(&.{ "--entry", name });
12081206 }
12091207
1210 for (self.base.rpath_list) |rpath| {
1211 try argv.append("-rpath");
1212 try argv.append(rpath);
1208 for (self.rpath_table.keys()) |rpath| {
1209 try argv.appendSlice(&.{ "-rpath", rpath });
12131210 }
12141211
12151212 try argv.appendSlice(&.{
......@@ -1978,7 +1975,7 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
19781975 man.hash.add(self.emit_relocs);
19791976 man.hash.add(comp.config.rdynamic);
19801977 man.hash.addListOfBytes(self.lib_dirs);
1981 man.hash.addListOfBytes(self.base.rpath_list);
1978 man.hash.addListOfBytes(self.rpath_table.keys());
19821979 if (output_mode == .Exe) {
19831980 man.hash.add(self.base.stack_size);
19841981 man.hash.add(self.base.build_id);
......@@ -2263,14 +2260,8 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
22632260 if (csu.crti) |v| try argv.append(v);
22642261 if (csu.crtbegin) |v| try argv.append(v);
22652262
2266 // rpaths
2267 var rpath_table = std.StringHashMap(void).init(gpa);
2268 defer rpath_table.deinit();
2269 for (self.base.rpath_list) |rpath| {
2270 if ((try rpath_table.fetchPut(rpath, {})) == null) {
2271 try argv.append("-rpath");
2272 try argv.append(rpath);
2273 }
2263 for (self.rpath_table.keys()) |rpath| {
2264 try argv.appendSlice(&.{ "-rpath", rpath });
22742265 }
22752266
22762267 for (self.symbol_wrap_set.keys()) |symbol_name| {
src/link/MachO.zig+6-5
......@@ -1,5 +1,7 @@
11base: link.File,
22
3rpath_list: []const []const u8,
4
35/// If this is not null, an object file is created by LLVM and emitted to zcu_object_sub_path.
46llvm_object: ?LlvmObject.Ptr = null,
57
......@@ -192,8 +194,8 @@ pub fn createEmpty(
192194 .file = null,
193195 .disable_lld_caching = options.disable_lld_caching,
194196 .build_id = options.build_id,
195 .rpath_list = options.rpath_list,
196197 },
198 .rpath_list = options.rpath_list,
197199 .pagezero_size = options.pagezero_size,
198200 .headerpad_size = options.headerpad_size,
199201 .headerpad_max_install_names = options.headerpad_max_install_names,
......@@ -662,9 +664,8 @@ fn dumpArgv(self: *MachO, comp: *Compilation) !void {
662664 try argv.append(syslibroot);
663665 }
664666
665 for (self.base.rpath_list) |rpath| {
666 try argv.append("-rpath");
667 try argv.append(rpath);
667 for (self.rpath_list) |rpath| {
668 try argv.appendSlice(&.{ "-rpath", rpath });
668669 }
669670
670671 if (self.pagezero_size) |size| {
......@@ -2842,7 +2843,7 @@ fn writeLoadCommands(self: *MachO) !struct { usize, usize, u64 } {
28422843 ncmds += 1;
28432844 }
28442845
2845 for (self.base.rpath_list) |rpath| {
2846 for (self.rpath_list) |rpath| {
28462847 try load_commands.writeRpathLC(rpath, writer);
28472848 ncmds += 1;
28482849 }
src/link/MachO/load_commands.zig+1-1
......@@ -63,7 +63,7 @@ pub fn calcLoadCommandsSize(macho_file: *MachO, assume_max_path_len: bool) !u32
6363 }
6464 // LC_RPATH
6565 {
66 for (macho_file.base.rpath_list) |rpath| {
66 for (macho_file.rpath_list) |rpath| {
6767 sizeofcmds += calcInstallNameLen(
6868 @sizeOf(macho.rpath_command),
6969 rpath,
src/link/NvPtx.zig-1
......@@ -60,7 +60,6 @@ pub fn createEmpty(
6060 .file = null,
6161 .disable_lld_caching = options.disable_lld_caching,
6262 .build_id = options.build_id,
63 .rpath_list = options.rpath_list,
6463 },
6564 .llvm_object = llvm_object,
6665 };
src/link/Plan9.zig-1
......@@ -304,7 +304,6 @@ pub fn createEmpty(
304304 .file = null,
305305 .disable_lld_caching = options.disable_lld_caching,
306306 .build_id = options.build_id,
307 .rpath_list = options.rpath_list,
308307 },
309308 .sixtyfour_bit = sixtyfour_bit,
310309 .bases = undefined,
src/link/SpirV.zig-1
......@@ -74,7 +74,6 @@ pub fn createEmpty(
7474 .file = null,
7575 .disable_lld_caching = options.disable_lld_caching,
7676 .build_id = options.build_id,
77 .rpath_list = options.rpath_list,
7877 },
7978 .object = codegen.Object.init(gpa),
8079 };
src/link/Wasm.zig-1
......@@ -398,7 +398,6 @@ pub fn createEmpty(
398398 .file = null,
399399 .disable_lld_caching = options.disable_lld_caching,
400400 .build_id = options.build_id,
401 .rpath_list = options.rpath_list,
402401 },
403402 .name = undefined,
404403 .import_table = options.import_table,