| author | |
| committer | |
| log | f24ceec35a6fd1e5e6a671461b78919b5f588a32 |
| tree | 00e4242cf5dcdae789e0d8f1de77303f4a2e6e23 |
| parent | 98dc28bbe223cb7183aabe7ed7a847c67c1a4df9 |
| parent | 7a186d9eb6a84fb22bdb53b9c81a70169e9fa65f |
| signature |
elf: handle emitting relocatables and static libraries - humble beginnings26 files changed, 1913 insertions(+), 1010 deletions(-)
CMakeLists.txt+1-1| ... | ... | @@ -624,7 +624,7 @@ set(ZIG_STAGE2_SOURCES |
| 624 | 624 | "${CMAKE_SOURCE_DIR}/src/link/Plan9/aout.zig" |
| 625 | 625 | "${CMAKE_SOURCE_DIR}/src/link/Wasm.zig" |
| 626 | 626 | "${CMAKE_SOURCE_DIR}/src/link/msdos-stub.bin" |
| 627 | "${CMAKE_SOURCE_DIR}/src/link/strtab.zig" | |
| 627 | "${CMAKE_SOURCE_DIR}/src/link/StringTable.zig" | |
| 628 | 628 | "${CMAKE_SOURCE_DIR}/src/link/tapi.zig" |
| 629 | 629 | "${CMAKE_SOURCE_DIR}/src/link/tapi/Tokenizer.zig" |
| 630 | 630 | "${CMAKE_SOURCE_DIR}/src/link/tapi/parse.zig" |
src/Compilation.zig+59-11| ... | ... | @@ -810,16 +810,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 810 | 810 | return error.ExportTableAndImportTableConflict; |
| 811 | 811 | } |
| 812 | 812 | |
| 813 | // The `have_llvm` condition is here only because native backends cannot yet build compiler-rt. | |
| 814 | // Once they are capable this condition could be removed. When removing this condition, | |
| 815 | // also test the use case of `build-obj -fcompiler-rt` with the native backends | |
| 816 | // and make sure the compiler-rt symbols are emitted. | |
| 817 | const is_p9 = options.target.os.tag == .plan9; | |
| 818 | const is_spv = options.target.cpu.arch.isSpirV(); | |
| 819 | const capable_of_building_compiler_rt = build_options.have_llvm and !is_p9 and !is_spv; | |
| 820 | const capable_of_building_zig_libc = build_options.have_llvm and !is_p9 and !is_spv; | |
| 821 | const capable_of_building_ssp = build_options.have_llvm and !is_p9 and !is_spv; | |
| 822 | ||
| 823 | 813 | const comp: *Compilation = comp: { |
| 824 | 814 | // For allocations that have the same lifetime as Compilation. This arena is used only during this |
| 825 | 815 | // initialization and then is freed in deinit(). |
| ... | ... | @@ -1094,6 +1084,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1094 | 1084 | if (stack_check and !target_util.supportsStackProbing(options.target)) |
| 1095 | 1085 | return error.StackCheckUnsupportedByTarget; |
| 1096 | 1086 | |
| 1087 | const capable_of_building_ssp = canBuildLibSsp(options.target, use_llvm); | |
| 1088 | ||
| 1097 | 1089 | const stack_protector: u32 = options.want_stack_protector orelse b: { |
| 1098 | 1090 | if (!target_util.supportsStackProtector(options.target)) break :b @as(u32, 0); |
| 1099 | 1091 | |
| ... | ... | @@ -1754,6 +1746,9 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1754 | 1746 | |
| 1755 | 1747 | const target = comp.getTarget(); |
| 1756 | 1748 | |
| 1749 | const capable_of_building_compiler_rt = canBuildLibCompilerRt(target, comp.bin_file.options.use_llvm); | |
| 1750 | const capable_of_building_zig_libc = canBuildZigLibC(target, comp.bin_file.options.use_llvm); | |
| 1751 | ||
| 1757 | 1752 | // Add a `CObject` for each `c_source_files`. |
| 1758 | 1753 | try comp.c_object_table.ensureTotalCapacity(gpa, options.c_source_files.len); |
| 1759 | 1754 | for (options.c_source_files) |c_source_file| { |
| ... | ... | @@ -6240,9 +6235,62 @@ pub fn dump_argv(argv: []const []const u8) void { |
| 6240 | 6235 | nosuspend stderr.print("{s}\n", .{argv[argv.len - 1]}) catch {}; |
| 6241 | 6236 | } |
| 6242 | 6237 | |
| 6238 | fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool { | |
| 6239 | switch (target.os.tag) { | |
| 6240 | .plan9 => return false, | |
| 6241 | else => {}, | |
| 6242 | } | |
| 6243 | switch (target.cpu.arch) { | |
| 6244 | .spirv32, .spirv64 => return false, | |
| 6245 | else => {}, | |
| 6246 | } | |
| 6247 | return switch (zigBackend(target, use_llvm)) { | |
| 6248 | .stage2_llvm => true, | |
| 6249 | .stage2_x86_64 => if (target.ofmt == .elf) true else build_options.have_llvm, | |
| 6250 | else => build_options.have_llvm, | |
| 6251 | }; | |
| 6252 | } | |
| 6253 | ||
| 6254 | fn canBuildLibSsp(target: std.Target, use_llvm: bool) bool { | |
| 6255 | switch (target.os.tag) { | |
| 6256 | .plan9 => return false, | |
| 6257 | else => {}, | |
| 6258 | } | |
| 6259 | switch (target.cpu.arch) { | |
| 6260 | .spirv32, .spirv64 => return false, | |
| 6261 | else => {}, | |
| 6262 | } | |
| 6263 | return switch (zigBackend(target, use_llvm)) { | |
| 6264 | .stage2_llvm => true, | |
| 6265 | else => build_options.have_llvm, | |
| 6266 | }; | |
| 6267 | } | |
| 6268 | ||
| 6269 | /// Not to be confused with canBuildLibC, which builds musl, glibc, and similar. | |
| 6270 | /// This one builds lib/c.zig. | |
| 6271 | fn canBuildZigLibC(target: std.Target, use_llvm: bool) bool { | |
| 6272 | switch (target.os.tag) { | |
| 6273 | .plan9 => return false, | |
| 6274 | else => {}, | |
| 6275 | } | |
| 6276 | switch (target.cpu.arch) { | |
| 6277 | .spirv32, .spirv64 => return false, | |
| 6278 | else => {}, | |
| 6279 | } | |
| 6280 | return switch (zigBackend(target, use_llvm)) { | |
| 6281 | .stage2_llvm => true, | |
| 6282 | .stage2_x86_64 => if (target.ofmt == .elf) true else build_options.have_llvm, | |
| 6283 | else => build_options.have_llvm, | |
| 6284 | }; | |
| 6285 | } | |
| 6286 | ||
| 6243 | 6287 | pub fn getZigBackend(comp: Compilation) std.builtin.CompilerBackend { |
| 6244 | if (comp.bin_file.options.use_llvm) return .stage2_llvm; | |
| 6245 | 6288 | const target = comp.bin_file.options.target; |
| 6289 | return zigBackend(target, comp.bin_file.options.use_llvm); | |
| 6290 | } | |
| 6291 | ||
| 6292 | fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend { | |
| 6293 | if (use_llvm) return .stage2_llvm; | |
| 6246 | 6294 | if (target.ofmt == .c) return .stage2_c; |
| 6247 | 6295 | return switch (target.cpu.arch) { |
| 6248 | 6296 | .wasm32, .wasm64 => std.builtin.CompilerBackend.stage2_wasm, |
src/arch/x86_64/CodeGen.zig+2-3| ... | ... | @@ -10796,7 +10796,7 @@ fn genCall(self: *Self, info: union(enum) { |
| 10796 | 10796 | if (self.bin_file.cast(link.File.Elf)) |elf_file| { |
| 10797 | 10797 | const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func.owner_decl); |
| 10798 | 10798 | const sym = elf_file.symbol(sym_index); |
| 10799 | _ = try sym.getOrCreateZigGotEntry(sym_index, elf_file); | |
| 10799 | sym.flags.needs_zig_got = true; | |
| 10800 | 10800 | if (self.bin_file.options.pic) { |
| 10801 | 10801 | const callee_reg: Register = switch (resolved_cc) { |
| 10802 | 10802 | .SysV => callee: { |
| ... | ... | @@ -13682,8 +13682,7 @@ fn genLazySymbolRef( |
| 13682 | 13682 | const sym_index = elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, lazy_sym) catch |err| |
| 13683 | 13683 | return self.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 13684 | 13684 | const sym = elf_file.symbol(sym_index); |
| 13685 | _ = try sym.getOrCreateZigGotEntry(sym_index, elf_file); | |
| 13686 | ||
| 13685 | sym.flags.needs_zig_got = true; | |
| 13687 | 13686 | if (self.bin_file.options.pic) { |
| 13688 | 13687 | switch (tag) { |
| 13689 | 13688 | .lea, .call => try self.genSetReg(reg, Type.usize, .{ |
src/arch/x86_64/Emit.zig+30-13| ... | ... | @@ -85,10 +85,19 @@ pub fn emitMir(emit: *Emit) Error!void { |
| 85 | 85 | @tagName(emit.lower.bin_file.tag), |
| 86 | 86 | }), |
| 87 | 87 | .linker_reloc => |data| if (emit.lower.bin_file.cast(link.File.Elf)) |elf_file| { |
| 88 | const is_obj_or_static_lib = switch (emit.lower.bin_file.options.output_mode) { | |
| 89 | .Exe => false, | |
| 90 | .Obj => true, | |
| 91 | .Lib => emit.lower.bin_file.options.link_mode == .Static, | |
| 92 | }; | |
| 88 | 93 | const atom = elf_file.symbol(data.atom_index).atom(elf_file).?; |
| 89 | const sym = elf_file.symbol(elf_file.zigObjectPtr().?.symbol(data.sym_index)); | |
| 94 | const sym_index = elf_file.zigObjectPtr().?.symbol(data.sym_index); | |
| 95 | const sym = elf_file.symbol(sym_index); | |
| 96 | if (sym.flags.needs_zig_got and !is_obj_or_static_lib) { | |
| 97 | _ = try sym.getOrCreateZigGotEntry(sym_index, elf_file); | |
| 98 | } | |
| 90 | 99 | if (emit.lower.bin_file.options.pic) { |
| 91 | const r_type: u32 = if (sym.flags.has_zig_got) | |
| 100 | const r_type: u32 = if (sym.flags.needs_zig_got and !is_obj_or_static_lib) | |
| 92 | 101 | link.File.Elf.R_X86_64_ZIG_GOTPCREL |
| 93 | 102 | else if (sym.flags.needs_got) |
| 94 | 103 | std.elf.R_X86_64_GOTPCREL |
| ... | ... | @@ -100,17 +109,25 @@ pub fn emitMir(emit: *Emit) Error!void { |
| 100 | 109 | .r_addend = -4, |
| 101 | 110 | }); |
| 102 | 111 | } else { |
| 103 | const r_type: u32 = if (sym.flags.has_zig_got) | |
| 104 | link.File.Elf.R_X86_64_ZIG_GOT32 | |
| 105 | else if (sym.flags.needs_got) | |
| 106 | std.elf.R_X86_64_GOT32 | |
| 107 | else | |
| 108 | std.elf.R_X86_64_32; | |
| 109 | try atom.addReloc(elf_file, .{ | |
| 110 | .r_offset = end_offset - 4, | |
| 111 | .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | r_type, | |
| 112 | .r_addend = 0, | |
| 113 | }); | |
| 112 | if (lowered_inst.encoding.mnemonic == .call and sym.flags.needs_zig_got and is_obj_or_static_lib) { | |
| 113 | try atom.addReloc(elf_file, .{ | |
| 114 | .r_offset = end_offset - 4, | |
| 115 | .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | std.elf.R_X86_64_PC32, | |
| 116 | .r_addend = -4, | |
| 117 | }); | |
| 118 | } else { | |
| 119 | const r_type: u32 = if (sym.flags.needs_zig_got and !is_obj_or_static_lib) | |
| 120 | link.File.Elf.R_X86_64_ZIG_GOT32 | |
| 121 | else if (sym.flags.needs_got) | |
| 122 | std.elf.R_X86_64_GOT32 | |
| 123 | else | |
| 124 | std.elf.R_X86_64_32; | |
| 125 | try atom.addReloc(elf_file, .{ | |
| 126 | .r_offset = end_offset - 4, | |
| 127 | .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | r_type, | |
| 128 | .r_addend = 0, | |
| 129 | }); | |
| 130 | } | |
| 114 | 131 | } |
| 115 | 132 | } else unreachable, |
| 116 | 133 | .linker_got, |
src/arch/x86_64/Lower.zig+29-5| ... | ... | @@ -319,6 +319,19 @@ fn reloc(lower: *Lower, target: Reloc.Target) Immediate { |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand) Error!void { |
| 322 | const needsZigGot = struct { | |
| 323 | fn needsZigGot(sym: bits.Symbol, ctx: *link.File) bool { | |
| 324 | const elf_file = ctx.cast(link.File.Elf).?; | |
| 325 | const sym_index = elf_file.zigObjectPtr().?.symbol(sym.sym_index); | |
| 326 | return elf_file.symbol(sym_index).flags.needs_zig_got; | |
| 327 | } | |
| 328 | }.needsZigGot; | |
| 329 | ||
| 330 | const is_obj_or_static_lib = switch (lower.bin_file.options.output_mode) { | |
| 331 | .Exe => false, | |
| 332 | .Obj => true, | |
| 333 | .Lib => lower.bin_file.options.link_mode == .Static, | |
| 334 | }; | |
| 322 | 335 | var emit_prefix = prefix; |
| 323 | 336 | var emit_mnemonic = mnemonic; |
| 324 | 337 | var emit_ops_storage: [4]Operand = undefined; |
| ... | ... | @@ -334,19 +347,30 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand) |
| 334 | 347 | assert(mem_op.sib.scale_index.scale == 0); |
| 335 | 348 | _ = lower.reloc(.{ .linker_reloc = sym }); |
| 336 | 349 | break :op if (lower.bin_file.options.pic) switch (mnemonic) { |
| 337 | .mov, .lea => .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) }, | |
| 350 | .lea => { | |
| 351 | break :op .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) }; | |
| 352 | }, | |
| 353 | .mov => { | |
| 354 | if (is_obj_or_static_lib and needsZigGot(sym, lower.bin_file)) emit_mnemonic = .lea; | |
| 355 | break :op .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) }; | |
| 356 | }, | |
| 338 | 357 | else => unreachable, |
| 339 | 358 | } else switch (mnemonic) { |
| 340 | .call => .{ .mem = Memory.sib(mem_op.sib.ptr_size, .{ | |
| 359 | .call => break :op if (is_obj_or_static_lib and needsZigGot(sym, lower.bin_file)) .{ | |
| 360 | .imm = Immediate.s(0), | |
| 361 | } else .{ .mem = Memory.sib(mem_op.sib.ptr_size, .{ | |
| 341 | 362 | .base = .{ .reg = .ds }, |
| 342 | 363 | }) }, |
| 343 | 364 | .lea => { |
| 344 | 365 | emit_mnemonic = .mov; |
| 345 | 366 | break :op .{ .imm = Immediate.s(0) }; |
| 346 | 367 | }, |
| 347 | .mov => .{ .mem = Memory.sib(mem_op.sib.ptr_size, .{ | |
| 348 | .base = .{ .reg = .ds }, | |
| 349 | }) }, | |
| 368 | .mov => { | |
| 369 | if (is_obj_or_static_lib and needsZigGot(sym, lower.bin_file)) emit_mnemonic = .lea; | |
| 370 | break :op .{ .mem = Memory.sib(mem_op.sib.ptr_size, .{ | |
| 371 | .base = .{ .reg = .ds }, | |
| 372 | }) }; | |
| 373 | }, | |
| 350 | 374 | else => unreachable, |
| 351 | 375 | }; |
| 352 | 376 | }, |
src/codegen.zig+1-1| ... | ... | @@ -912,7 +912,7 @@ fn genDeclRef( |
| 912 | 912 | } |
| 913 | 913 | const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, decl_index); |
| 914 | 914 | const sym = elf_file.symbol(sym_index); |
| 915 | _ = try sym.getOrCreateZigGotEntry(sym_index, elf_file); | |
| 915 | sym.flags.needs_zig_got = true; | |
| 916 | 916 | return GenResult.mcv(.{ .load_symbol = sym.esym_index }); |
| 917 | 917 | } else if (bin_file.cast(link.File.MachO)) |macho_file| { |
| 918 | 918 | if (is_extern) { |
src/link/Coff.zig+8-8| ... | ... | @@ -33,10 +33,10 @@ need_got_table: std.AutoHashMapUnmanaged(u32, void) = .{}, |
| 33 | 33 | locals_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| 34 | 34 | globals_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| 35 | 35 | |
| 36 | strtab: StringTable(.strtab) = .{}, | |
| 36 | strtab: StringTable = .{}, | |
| 37 | 37 | strtab_offset: ?u32 = null, |
| 38 | 38 | |
| 39 | temp_strtab: StringTable(.temp_strtab) = .{}, | |
| 39 | temp_strtab: StringTable = .{}, | |
| 40 | 40 | |
| 41 | 41 | got_table: TableSection(SymbolWithLoc) = .{}, |
| 42 | 42 | |
| ... | ... | @@ -419,7 +419,7 @@ fn populateMissingMetadata(self: *Coff) !void { |
| 419 | 419 | } |
| 420 | 420 | |
| 421 | 421 | if (self.strtab_offset == null) { |
| 422 | const file_size = @as(u32, @intCast(self.strtab.len())); | |
| 422 | const file_size = @as(u32, @intCast(self.strtab.buffer.items.len)); | |
| 423 | 423 | self.strtab_offset = self.findFreeSpace(file_size, @alignOf(u32)); // 4bytes aligned seems like a good idea here |
| 424 | 424 | log.debug("found strtab free space 0x{x} to 0x{x}", .{ self.strtab_offset.?, self.strtab_offset.? + file_size }); |
| 425 | 425 | } |
| ... | ... | @@ -2143,7 +2143,7 @@ fn writeStrtab(self: *Coff) !void { |
| 2143 | 2143 | if (self.strtab_offset == null) return; |
| 2144 | 2144 | |
| 2145 | 2145 | const allocated_size = self.allocatedSize(self.strtab_offset.?); |
| 2146 | const needed_size = @as(u32, @intCast(self.strtab.len())); | |
| 2146 | const needed_size = @as(u32, @intCast(self.strtab.buffer.items.len)); | |
| 2147 | 2147 | |
| 2148 | 2148 | if (needed_size > allocated_size) { |
| 2149 | 2149 | self.strtab_offset = null; |
| ... | ... | @@ -2155,10 +2155,10 @@ fn writeStrtab(self: *Coff) !void { |
| 2155 | 2155 | var buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2156 | 2156 | defer buffer.deinit(); |
| 2157 | 2157 | try buffer.ensureTotalCapacityPrecise(needed_size); |
| 2158 | buffer.appendSliceAssumeCapacity(self.strtab.items()); | |
| 2158 | buffer.appendSliceAssumeCapacity(self.strtab.buffer.items); | |
| 2159 | 2159 | // Here, we do a trick in that we do not commit the size of the strtab to strtab buffer, instead |
| 2160 | 2160 | // we write the length of the strtab to a temporary buffer that goes to file. |
| 2161 | mem.writeInt(u32, buffer.items[0..4], @as(u32, @intCast(self.strtab.len())), .little); | |
| 2161 | mem.writeInt(u32, buffer.items[0..4], @as(u32, @intCast(self.strtab.buffer.items.len)), .little); | |
| 2162 | 2162 | |
| 2163 | 2163 | try self.base.file.?.pwriteAll(buffer.items, self.strtab_offset.?); |
| 2164 | 2164 | } |
| ... | ... | @@ -2326,7 +2326,7 @@ fn detectAllocCollision(self: *Coff, start: u32, size: u32) ?u32 { |
| 2326 | 2326 | const end = start + padToIdeal(size); |
| 2327 | 2327 | |
| 2328 | 2328 | if (self.strtab_offset) |off| { |
| 2329 | const tight_size = @as(u32, @intCast(self.strtab.len())); | |
| 2329 | const tight_size = @as(u32, @intCast(self.strtab.buffer.items.len)); | |
| 2330 | 2330 | const increased_size = padToIdeal(tight_size); |
| 2331 | 2331 | const test_end = off + increased_size; |
| 2332 | 2332 | if (end > off and start < test_end) { |
| ... | ... | @@ -2667,7 +2667,7 @@ const InternPool = @import("../InternPool.zig"); |
| 2667 | 2667 | const Object = @import("Coff/Object.zig"); |
| 2668 | 2668 | const Relocation = @import("Coff/Relocation.zig"); |
| 2669 | 2669 | const TableSection = @import("table_section.zig").TableSection; |
| 2670 | const StringTable = @import("strtab.zig").StringTable; | |
| 2670 | const StringTable = @import("StringTable.zig"); | |
| 2671 | 2671 | const Type = @import("../type.zig").Type; |
| 2672 | 2672 | const TypedValue = @import("../TypedValue.zig"); |
| 2673 | 2673 |
src/link/Dwarf.zig+2-2| ... | ... | @@ -23,7 +23,7 @@ abbrev_table_offset: ?u64 = null, |
| 23 | 23 | |
| 24 | 24 | /// TODO replace with InternPool |
| 25 | 25 | /// Table of debug symbol names. |
| 26 | strtab: StringTable(.strtab) = .{}, | |
| 26 | strtab: StringTable = .{}, | |
| 27 | 27 | |
| 28 | 28 | /// Quick lookup array of all defined source files referenced by at least one Decl. |
| 29 | 29 | /// They will end up in the DWARF debug_line header as two lists: |
| ... | ... | @@ -2760,6 +2760,6 @@ const LinkFn = File.LinkFn; |
| 2760 | 2760 | const LinkerLoad = @import("../codegen.zig").LinkerLoad; |
| 2761 | 2761 | const Module = @import("../Module.zig"); |
| 2762 | 2762 | const InternPool = @import("../InternPool.zig"); |
| 2763 | const StringTable = @import("strtab.zig").StringTable; | |
| 2763 | const StringTable = @import("StringTable.zig"); | |
| 2764 | 2764 | const Type = @import("../type.zig").Type; |
| 2765 | 2765 | const Value = @import("../value.zig").Value; |
src/link/Elf.zig+647-346| ... | ... | @@ -66,13 +66,15 @@ page_size: u32, |
| 66 | 66 | default_sym_version: elf.Elf64_Versym, |
| 67 | 67 | |
| 68 | 68 | /// .shstrtab buffer |
| 69 | shstrtab: StringTable(.strtab) = .{}, | |
| 69 | shstrtab: std.ArrayListUnmanaged(u8) = .{}, | |
| 70 | /// .symtab buffer | |
| 71 | symtab: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{}, | |
| 70 | 72 | /// .strtab buffer |
| 71 | strtab: StringTable(.strtab) = .{}, | |
| 73 | strtab: std.ArrayListUnmanaged(u8) = .{}, | |
| 72 | 74 | /// Dynamic symbol table. Only populated and emitted when linking dynamically. |
| 73 | 75 | dynsym: DynsymSection = .{}, |
| 74 | 76 | /// .dynstrtab buffer |
| 75 | dynstrtab: StringTable(.dynstrtab) = .{}, | |
| 77 | dynstrtab: std.ArrayListUnmanaged(u8) = .{}, | |
| 76 | 78 | /// Version symbol table. Only populated and emitted when linking dynamically. |
| 77 | 79 | versym: std.ArrayListUnmanaged(elf.Elf64_Versym) = .{}, |
| 78 | 80 | /// .verneed section |
| ... | ... | @@ -97,13 +99,17 @@ plt_got: PltGotSection = .{}, |
| 97 | 99 | copy_rel: CopyRelSection = .{}, |
| 98 | 100 | /// .rela.plt section |
| 99 | 101 | rela_plt: std.ArrayListUnmanaged(elf.Elf64_Rela) = .{}, |
| 100 | /// .zig.got section | |
| 102 | /// .got.zig section | |
| 101 | 103 | zig_got: ZigGotSection = .{}, |
| 102 | 104 | |
| 103 | /// Tracked section headers with incremental updates to Zig object | |
| 105 | /// Tracked section headers with incremental updates to Zig object. | |
| 106 | /// .rela.* sections are only used when emitting a relocatable object file. | |
| 104 | 107 | zig_text_section_index: ?u16 = null, |
| 105 | zig_rodata_section_index: ?u16 = null, | |
| 108 | zig_text_rela_section_index: ?u16 = null, | |
| 109 | zig_data_rel_ro_section_index: ?u16 = null, | |
| 110 | zig_data_rel_ro_rela_section_index: ?u16 = null, | |
| 106 | 111 | zig_data_section_index: ?u16 = null, |
| 112 | zig_data_rela_section_index: ?u16 = null, | |
| 107 | 113 | zig_bss_section_index: ?u16 = null, |
| 108 | 114 | zig_got_section_index: ?u16 = null, |
| 109 | 115 | |
| ... | ... | @@ -156,9 +162,10 @@ start_stop_indexes: std.ArrayListUnmanaged(u32) = .{}, |
| 156 | 162 | /// An array of symbols parsed across all input files. |
| 157 | 163 | symbols: std.ArrayListUnmanaged(Symbol) = .{}, |
| 158 | 164 | symbols_extra: std.ArrayListUnmanaged(u32) = .{}, |
| 159 | resolver: std.AutoArrayHashMapUnmanaged(u32, Symbol.Index) = .{}, | |
| 160 | 165 | symbols_free_list: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 161 | 166 | |
| 167 | resolver: std.AutoArrayHashMapUnmanaged(u32, Symbol.Index) = .{}, | |
| 168 | ||
| 162 | 169 | has_text_reloc: bool = false, |
| 163 | 170 | num_ifunc_dynrelocs: usize = 0, |
| 164 | 171 | |
| ... | ... | @@ -175,6 +182,10 @@ comdat_groups: std.ArrayListUnmanaged(ComdatGroup) = .{}, |
| 175 | 182 | comdat_groups_owners: std.ArrayListUnmanaged(ComdatGroupOwner) = .{}, |
| 176 | 183 | comdat_groups_table: std.AutoHashMapUnmanaged(u32, ComdatGroupOwner.Index) = .{}, |
| 177 | 184 | |
| 185 | /// Global string table used to provide quick access to global symbol resolvers | |
| 186 | /// such as `resolver` and `comdat_groups_table`. | |
| 187 | strings: StringTable = .{}, | |
| 188 | ||
| 178 | 189 | /// When allocating, the ideal_capacity is calculated by |
| 179 | 190 | /// actual_capacity + (actual_capacity / ideal_factor) |
| 180 | 191 | const ideal_factor = 3; |
| ... | ... | @@ -227,13 +238,15 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 227 | 238 | // Append null file at index 0 |
| 228 | 239 | try self.files.append(allocator, .null); |
| 229 | 240 | // Append null byte to string tables |
| 230 | try self.shstrtab.buffer.append(allocator, 0); | |
| 231 | try self.strtab.buffer.append(allocator, 0); | |
| 241 | try self.shstrtab.append(allocator, 0); | |
| 242 | try self.strtab.append(allocator, 0); | |
| 232 | 243 | // There must always be a null shdr in index 0 |
| 233 | 244 | _ = try self.addSection(.{ .name = "" }); |
| 245 | // Append null symbol in output symtab | |
| 246 | try self.symtab.append(allocator, null_sym); | |
| 234 | 247 | |
| 235 | 248 | if (!is_obj_or_ar) { |
| 236 | try self.dynstrtab.buffer.append(allocator, 0); | |
| 249 | try self.dynstrtab.append(allocator, 0); | |
| 237 | 250 | |
| 238 | 251 | // Initialize PT_PHDR program header |
| 239 | 252 | const p_align: u16 = switch (self.ptr_width) { |
| ... | ... | @@ -347,6 +360,7 @@ pub fn deinit(self: *Elf) void { |
| 347 | 360 | } |
| 348 | 361 | self.output_sections.deinit(gpa); |
| 349 | 362 | self.shstrtab.deinit(gpa); |
| 363 | self.symtab.deinit(gpa); | |
| 350 | 364 | self.strtab.deinit(gpa); |
| 351 | 365 | self.symbols.deinit(gpa); |
| 352 | 366 | self.symbols_extra.deinit(gpa); |
| ... | ... | @@ -364,6 +378,7 @@ pub fn deinit(self: *Elf) void { |
| 364 | 378 | self.comdat_groups.deinit(gpa); |
| 365 | 379 | self.comdat_groups_owners.deinit(gpa); |
| 366 | 380 | self.comdat_groups_table.deinit(gpa); |
| 381 | self.strings.deinit(gpa); | |
| 367 | 382 | |
| 368 | 383 | self.got.deinit(gpa); |
| 369 | 384 | self.plt.deinit(gpa); |
| ... | ... | @@ -473,262 +488,302 @@ fn findFreeSpace(self: *Elf, object_size: u64, min_alignment: u64) u64 { |
| 473 | 488 | return start; |
| 474 | 489 | } |
| 475 | 490 | |
| 476 | const AllocateSegmentOpts = struct { | |
| 477 | addr: u64, | |
| 478 | memsz: u64, | |
| 479 | filesz: u64, | |
| 480 | alignment: u64, | |
| 481 | flags: u32 = elf.PF_R, | |
| 482 | }; | |
| 483 | ||
| 484 | pub fn allocateSegment(self: *Elf, opts: AllocateSegmentOpts) error{OutOfMemory}!u16 { | |
| 485 | const off = self.findFreeSpace(opts.filesz, opts.alignment); | |
| 486 | const index = try self.addPhdr(.{ | |
| 487 | .type = elf.PT_LOAD, | |
| 488 | .offset = off, | |
| 489 | .filesz = opts.filesz, | |
| 490 | .addr = opts.addr, | |
| 491 | .memsz = opts.memsz, | |
| 492 | .@"align" = opts.alignment, | |
| 493 | .flags = opts.flags, | |
| 494 | }); | |
| 495 | log.debug("allocating phdr({d})({c}{c}{c}) from 0x{x} to 0x{x} (0x{x} - 0x{x})", .{ | |
| 496 | index, | |
| 497 | if (opts.flags & elf.PF_R != 0) @as(u8, 'R') else '_', | |
| 498 | if (opts.flags & elf.PF_W != 0) @as(u8, 'W') else '_', | |
| 499 | if (opts.flags & elf.PF_X != 0) @as(u8, 'X') else '_', | |
| 500 | off, | |
| 501 | off + opts.filesz, | |
| 502 | opts.addr, | |
| 503 | opts.addr + opts.memsz, | |
| 504 | }); | |
| 505 | return index; | |
| 506 | } | |
| 507 | ||
| 508 | const AllocateAllocSectionOpts = struct { | |
| 509 | name: [:0]const u8, | |
| 510 | phdr_index: u16, | |
| 511 | alignment: u64 = 1, | |
| 512 | flags: u64 = elf.SHF_ALLOC, | |
| 513 | type: u32 = elf.SHT_PROGBITS, | |
| 514 | }; | |
| 515 | ||
| 516 | pub fn allocateAllocSection(self: *Elf, opts: AllocateAllocSectionOpts) error{OutOfMemory}!u16 { | |
| 517 | const gpa = self.base.allocator; | |
| 518 | const phdr = &self.phdrs.items[opts.phdr_index]; | |
| 519 | const index = try self.addSection(.{ | |
| 520 | .name = opts.name, | |
| 521 | .type = opts.type, | |
| 522 | .flags = opts.flags, | |
| 523 | .addralign = opts.alignment, | |
| 524 | .offset = std.math.maxInt(u64), | |
| 525 | }); | |
| 526 | const shdr = &self.shdrs.items[index]; | |
| 527 | try self.phdr_to_shdr_table.putNoClobber(gpa, index, opts.phdr_index); | |
| 528 | log.debug("allocating '{s}' in phdr({d}) from 0x{x} to 0x{x} (0x{x} - 0x{x})", .{ | |
| 529 | opts.name, | |
| 530 | opts.phdr_index, | |
| 531 | phdr.p_offset, | |
| 532 | phdr.p_offset + phdr.p_filesz, | |
| 533 | phdr.p_vaddr, | |
| 534 | phdr.p_vaddr + phdr.p_memsz, | |
| 535 | }); | |
| 536 | shdr.sh_addr = phdr.p_vaddr; | |
| 537 | shdr.sh_offset = phdr.p_offset; | |
| 538 | shdr.sh_size = phdr.p_memsz; | |
| 539 | return index; | |
| 540 | } | |
| 541 | ||
| 542 | const AllocateNonAllocSectionOpts = struct { | |
| 543 | name: [:0]const u8, | |
| 544 | size: u64, | |
| 545 | alignment: u16 = 1, | |
| 546 | flags: u32 = 0, | |
| 547 | type: u32 = elf.SHT_PROGBITS, | |
| 548 | link: u32 = 0, | |
| 549 | info: u32 = 0, | |
| 550 | entsize: u64 = 0, | |
| 551 | }; | |
| 552 | ||
| 553 | fn allocateNonAllocSection(self: *Elf, opts: AllocateNonAllocSectionOpts) error{OutOfMemory}!u16 { | |
| 554 | const index = try self.addSection(.{ | |
| 555 | .name = opts.name, | |
| 556 | .type = opts.type, | |
| 557 | .flags = opts.flags, | |
| 558 | .link = opts.link, | |
| 559 | .info = opts.info, | |
| 560 | .addralign = opts.alignment, | |
| 561 | .entsize = opts.entsize, | |
| 562 | .offset = std.math.maxInt(u64), | |
| 563 | }); | |
| 564 | const shdr = &self.shdrs.items[index]; | |
| 565 | const off = self.findFreeSpace(opts.size, opts.alignment); | |
| 566 | log.debug("allocating '{s}' from 0x{x} to 0x{x} ", .{ opts.name, off, off + opts.size }); | |
| 567 | shdr.sh_offset = off; | |
| 568 | shdr.sh_size = opts.size; | |
| 569 | return index; | |
| 570 | } | |
| 571 | ||
| 572 | 491 | /// TODO move to ZigObject |
| 573 | 492 | pub fn initMetadata(self: *Elf) !void { |
| 574 | 493 | const gpa = self.base.allocator; |
| 575 | 494 | const ptr_size = self.ptrWidthBytes(); |
| 576 | 495 | const ptr_bit_width = self.base.options.target.ptrBitWidth(); |
| 577 | 496 | const is_linux = self.base.options.target.os.tag == .linux; |
| 497 | const zig_object = self.zigObjectPtr().?; | |
| 498 | ||
| 499 | const fillSection = struct { | |
| 500 | fn fillSection(elf_file: *Elf, shdr: *elf.Elf64_Shdr, size: u64, phndx: ?u16) void { | |
| 501 | if (elf_file.isRelocatable()) { | |
| 502 | const off = elf_file.findFreeSpace(size, shdr.sh_addralign); | |
| 503 | shdr.sh_offset = off; | |
| 504 | shdr.sh_size = size; | |
| 505 | } else { | |
| 506 | const phdr = elf_file.phdrs.items[phndx.?]; | |
| 507 | shdr.sh_addr = phdr.p_vaddr; | |
| 508 | shdr.sh_offset = phdr.p_offset; | |
| 509 | shdr.sh_size = phdr.p_memsz; | |
| 510 | } | |
| 511 | } | |
| 512 | }.fillSection; | |
| 578 | 513 | |
| 579 | 514 | comptime assert(number_of_zig_segments == 5); |
| 580 | 515 | |
| 581 | if (self.phdr_zig_load_re_index == null) { | |
| 582 | self.phdr_zig_load_re_index = try self.allocateSegment(.{ | |
| 583 | .addr = if (ptr_bit_width >= 32) 0x8000000 else 0x8000, | |
| 584 | .memsz = self.base.options.program_code_size_hint, | |
| 585 | .filesz = self.base.options.program_code_size_hint, | |
| 586 | .alignment = self.page_size, | |
| 587 | .flags = elf.PF_X | elf.PF_R | elf.PF_W, | |
| 588 | }); | |
| 589 | } | |
| 516 | if (!self.isRelocatable()) { | |
| 517 | if (self.phdr_zig_load_re_index == null) { | |
| 518 | const filesz = self.base.options.program_code_size_hint; | |
| 519 | const off = self.findFreeSpace(filesz, self.page_size); | |
| 520 | self.phdr_zig_load_re_index = try self.addPhdr(.{ | |
| 521 | .type = elf.PT_LOAD, | |
| 522 | .offset = off, | |
| 523 | .filesz = filesz, | |
| 524 | .addr = if (ptr_bit_width >= 32) 0x8000000 else 0x8000, | |
| 525 | .memsz = filesz, | |
| 526 | .@"align" = self.page_size, | |
| 527 | .flags = elf.PF_X | elf.PF_R | elf.PF_W, | |
| 528 | }); | |
| 529 | } | |
| 590 | 530 | |
| 591 | if (self.phdr_zig_got_index == null) { | |
| 592 | // We really only need ptr alignment but since we are using PROGBITS, linux requires | |
| 593 | // page align. | |
| 594 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | |
| 595 | self.phdr_zig_got_index = try self.allocateSegment(.{ | |
| 596 | .addr = if (ptr_bit_width >= 32) 0x4000000 else 0x4000, | |
| 597 | .memsz = @as(u64, ptr_size) * self.base.options.symbol_count_hint, | |
| 598 | .filesz = @as(u64, ptr_size) * self.base.options.symbol_count_hint, | |
| 599 | .alignment = alignment, | |
| 600 | .flags = elf.PF_R | elf.PF_W, | |
| 601 | }); | |
| 602 | } | |
| 531 | if (self.phdr_zig_got_index == null) { | |
| 532 | // We really only need ptr alignment but since we are using PROGBITS, linux requires | |
| 533 | // page align. | |
| 534 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | |
| 535 | const filesz = @as(u64, ptr_size) * self.base.options.symbol_count_hint; | |
| 536 | const off = self.findFreeSpace(filesz, alignment); | |
| 537 | self.phdr_zig_got_index = try self.addPhdr(.{ | |
| 538 | .type = elf.PT_LOAD, | |
| 539 | .offset = off, | |
| 540 | .filesz = filesz, | |
| 541 | .addr = if (ptr_bit_width >= 32) 0x4000000 else 0x4000, | |
| 542 | .memsz = filesz, | |
| 543 | .@"align" = alignment, | |
| 544 | .flags = elf.PF_R | elf.PF_W, | |
| 545 | }); | |
| 546 | } | |
| 603 | 547 | |
| 604 | if (self.phdr_zig_load_ro_index == null) { | |
| 605 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | |
| 606 | self.phdr_zig_load_ro_index = try self.allocateSegment(.{ | |
| 607 | .addr = if (ptr_bit_width >= 32) 0xc000000 else 0xa000, | |
| 608 | .memsz = 1024, | |
| 609 | .filesz = 1024, | |
| 610 | .alignment = alignment, | |
| 611 | .flags = elf.PF_R | elf.PF_W, | |
| 612 | }); | |
| 613 | } | |
| 548 | if (self.phdr_zig_load_ro_index == null) { | |
| 549 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | |
| 550 | const filesz: u64 = 1024; | |
| 551 | const off = self.findFreeSpace(filesz, alignment); | |
| 552 | self.phdr_zig_load_ro_index = try self.addPhdr(.{ | |
| 553 | .type = elf.PT_LOAD, | |
| 554 | .offset = off, | |
| 555 | .filesz = filesz, | |
| 556 | .addr = if (ptr_bit_width >= 32) 0xc000000 else 0xa000, | |
| 557 | .memsz = filesz, | |
| 558 | .@"align" = alignment, | |
| 559 | .flags = elf.PF_R | elf.PF_W, | |
| 560 | }); | |
| 561 | } | |
| 614 | 562 | |
| 615 | if (self.phdr_zig_load_rw_index == null) { | |
| 616 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | |
| 617 | self.phdr_zig_load_rw_index = try self.allocateSegment(.{ | |
| 618 | .addr = if (ptr_bit_width >= 32) 0x10000000 else 0xc000, | |
| 619 | .memsz = 1024, | |
| 620 | .filesz = 1024, | |
| 621 | .alignment = alignment, | |
| 622 | .flags = elf.PF_R | elf.PF_W, | |
| 623 | }); | |
| 624 | } | |
| 563 | if (self.phdr_zig_load_rw_index == null) { | |
| 564 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | |
| 565 | const filesz: u64 = 1024; | |
| 566 | const off = self.findFreeSpace(filesz, alignment); | |
| 567 | self.phdr_zig_load_rw_index = try self.addPhdr(.{ | |
| 568 | .type = elf.PT_LOAD, | |
| 569 | .offset = off, | |
| 570 | .filesz = filesz, | |
| 571 | .addr = if (ptr_bit_width >= 32) 0x10000000 else 0xc000, | |
| 572 | .memsz = filesz, | |
| 573 | .@"align" = alignment, | |
| 574 | .flags = elf.PF_R | elf.PF_W, | |
| 575 | }); | |
| 576 | } | |
| 625 | 577 | |
| 626 | if (self.phdr_zig_load_zerofill_index == null) { | |
| 627 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | |
| 628 | self.phdr_zig_load_zerofill_index = try self.addPhdr(.{ | |
| 629 | .type = elf.PT_LOAD, | |
| 630 | .addr = if (ptr_bit_width >= 32) 0x14000000 else 0xf000, | |
| 631 | .memsz = 1024, | |
| 632 | .@"align" = alignment, | |
| 633 | .flags = elf.PF_R | elf.PF_W, | |
| 634 | }); | |
| 578 | if (self.phdr_zig_load_zerofill_index == null) { | |
| 579 | const alignment = if (is_linux) self.page_size else @as(u16, ptr_size); | |
| 580 | self.phdr_zig_load_zerofill_index = try self.addPhdr(.{ | |
| 581 | .type = elf.PT_LOAD, | |
| 582 | .addr = if (ptr_bit_width >= 32) 0x14000000 else 0xf000, | |
| 583 | .memsz = 1024, | |
| 584 | .@"align" = alignment, | |
| 585 | .flags = elf.PF_R | elf.PF_W, | |
| 586 | }); | |
| 587 | } | |
| 635 | 588 | } |
| 636 | 589 | |
| 637 | 590 | if (self.zig_text_section_index == null) { |
| 638 | self.zig_text_section_index = try self.allocateAllocSection(.{ | |
| 639 | .name = ".zig.text", | |
| 640 | .phdr_index = self.phdr_zig_load_re_index.?, | |
| 591 | self.zig_text_section_index = try self.addSection(.{ | |
| 592 | .name = ".text.zig", | |
| 593 | .type = elf.SHT_PROGBITS, | |
| 641 | 594 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| 595 | .addralign = 1, | |
| 596 | .offset = std.math.maxInt(u64), | |
| 642 | 597 | }); |
| 598 | const shdr = &self.shdrs.items[self.zig_text_section_index.?]; | |
| 599 | fillSection(self, shdr, self.base.options.program_code_size_hint, self.phdr_zig_load_re_index); | |
| 600 | if (self.isRelocatable()) { | |
| 601 | try zig_object.addSectionSymbol(self.zig_text_section_index.?, self); | |
| 602 | self.zig_text_rela_section_index = try self.addRelaShdr( | |
| 603 | ".rela.text.zig", | |
| 604 | self.zig_text_section_index.?, | |
| 605 | ); | |
| 606 | } else { | |
| 607 | try self.phdr_to_shdr_table.putNoClobber( | |
| 608 | gpa, | |
| 609 | self.zig_text_section_index.?, | |
| 610 | self.phdr_zig_load_re_index.?, | |
| 611 | ); | |
| 612 | } | |
| 643 | 613 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_text_section_index.?, .{}); |
| 644 | 614 | } |
| 645 | 615 | |
| 646 | if (self.zig_got_section_index == null) { | |
| 647 | self.zig_got_section_index = try self.allocateAllocSection(.{ | |
| 648 | .name = ".zig.got", | |
| 649 | .phdr_index = self.phdr_zig_got_index.?, | |
| 650 | .alignment = ptr_size, | |
| 616 | if (self.zig_got_section_index == null and !self.isRelocatable()) { | |
| 617 | self.zig_got_section_index = try self.addSection(.{ | |
| 618 | .name = ".got.zig", | |
| 619 | .type = elf.SHT_PROGBITS, | |
| 620 | .addralign = ptr_size, | |
| 651 | 621 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 622 | .offset = std.math.maxInt(u64), | |
| 652 | 623 | }); |
| 653 | } | |
| 654 | ||
| 655 | if (self.zig_rodata_section_index == null) { | |
| 656 | self.zig_rodata_section_index = try self.allocateAllocSection(.{ | |
| 657 | .name = ".zig.rodata", | |
| 658 | .phdr_index = self.phdr_zig_load_ro_index.?, | |
| 624 | const shdr = &self.shdrs.items[self.zig_got_section_index.?]; | |
| 625 | const phndx = self.phdr_zig_got_index.?; | |
| 626 | const phdr = self.phdrs.items[phndx]; | |
| 627 | shdr.sh_addr = phdr.p_vaddr; | |
| 628 | shdr.sh_offset = phdr.p_offset; | |
| 629 | shdr.sh_size = phdr.p_memsz; | |
| 630 | try self.phdr_to_shdr_table.putNoClobber( | |
| 631 | gpa, | |
| 632 | self.zig_got_section_index.?, | |
| 633 | self.phdr_zig_got_index.?, | |
| 634 | ); | |
| 635 | } | |
| 636 | ||
| 637 | if (self.zig_data_rel_ro_section_index == null) { | |
| 638 | self.zig_data_rel_ro_section_index = try self.addSection(.{ | |
| 639 | .name = ".data.rel.ro.zig", | |
| 640 | .type = elf.SHT_PROGBITS, | |
| 641 | .addralign = 1, | |
| 659 | 642 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, // TODO rename this section to .data.rel.ro |
| 643 | .offset = std.math.maxInt(u64), | |
| 660 | 644 | }); |
| 661 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_rodata_section_index.?, .{}); | |
| 645 | const shdr = &self.shdrs.items[self.zig_data_rel_ro_section_index.?]; | |
| 646 | fillSection(self, shdr, 1024, self.phdr_zig_load_ro_index); | |
| 647 | if (self.isRelocatable()) { | |
| 648 | try zig_object.addSectionSymbol(self.zig_data_rel_ro_section_index.?, self); | |
| 649 | self.zig_data_rel_ro_rela_section_index = try self.addRelaShdr( | |
| 650 | ".rela.data.rel.ro.zig", | |
| 651 | self.zig_data_rel_ro_section_index.?, | |
| 652 | ); | |
| 653 | } else { | |
| 654 | try self.phdr_to_shdr_table.putNoClobber( | |
| 655 | gpa, | |
| 656 | self.zig_data_rel_ro_section_index.?, | |
| 657 | self.phdr_zig_load_ro_index.?, | |
| 658 | ); | |
| 659 | } | |
| 660 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_data_rel_ro_section_index.?, .{}); | |
| 662 | 661 | } |
| 663 | 662 | |
| 664 | 663 | if (self.zig_data_section_index == null) { |
| 665 | self.zig_data_section_index = try self.allocateAllocSection(.{ | |
| 666 | .name = ".zig.data", | |
| 667 | .phdr_index = self.phdr_zig_load_rw_index.?, | |
| 668 | .alignment = ptr_size, | |
| 664 | self.zig_data_section_index = try self.addSection(.{ | |
| 665 | .name = ".data.zig", | |
| 666 | .type = elf.SHT_PROGBITS, | |
| 667 | .addralign = ptr_size, | |
| 669 | 668 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 669 | .offset = std.math.maxInt(u64), | |
| 670 | 670 | }); |
| 671 | const shdr = &self.shdrs.items[self.zig_data_section_index.?]; | |
| 672 | fillSection(self, shdr, 1024, self.phdr_zig_load_rw_index); | |
| 673 | if (self.isRelocatable()) { | |
| 674 | try zig_object.addSectionSymbol(self.zig_data_section_index.?, self); | |
| 675 | self.zig_data_rela_section_index = try self.addRelaShdr( | |
| 676 | ".rela.data.zig", | |
| 677 | self.zig_data_section_index.?, | |
| 678 | ); | |
| 679 | } else { | |
| 680 | try self.phdr_to_shdr_table.putNoClobber( | |
| 681 | gpa, | |
| 682 | self.zig_data_section_index.?, | |
| 683 | self.phdr_zig_load_rw_index.?, | |
| 684 | ); | |
| 685 | } | |
| 671 | 686 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_data_section_index.?, .{}); |
| 672 | 687 | } |
| 673 | 688 | |
| 674 | 689 | if (self.zig_bss_section_index == null) { |
| 675 | self.zig_bss_section_index = try self.allocateAllocSection(.{ | |
| 676 | .name = ".zig.bss", | |
| 677 | .phdr_index = self.phdr_zig_load_zerofill_index.?, | |
| 678 | .alignment = ptr_size, | |
| 679 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, | |
| 690 | self.zig_bss_section_index = try self.addSection(.{ | |
| 691 | .name = ".bss.zig", | |
| 680 | 692 | .type = elf.SHT_NOBITS, |
| 693 | .addralign = ptr_size, | |
| 694 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, | |
| 695 | .offset = 0, | |
| 681 | 696 | }); |
| 697 | const shdr = &self.shdrs.items[self.zig_bss_section_index.?]; | |
| 698 | if (self.phdr_zig_load_zerofill_index) |phndx| { | |
| 699 | const phdr = self.phdrs.items[phndx]; | |
| 700 | shdr.sh_addr = phdr.p_vaddr; | |
| 701 | shdr.sh_size = phdr.p_memsz; | |
| 702 | try self.phdr_to_shdr_table.putNoClobber(gpa, self.zig_bss_section_index.?, phndx); | |
| 703 | } else { | |
| 704 | try zig_object.addSectionSymbol(self.zig_bss_section_index.?, self); | |
| 705 | shdr.sh_size = 1024; | |
| 706 | } | |
| 682 | 707 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.zig_bss_section_index.?, .{}); |
| 683 | 708 | } |
| 684 | 709 | |
| 685 | const zig_object = self.zigObjectPtr().?; | |
| 686 | 710 | if (zig_object.dwarf) |*dw| { |
| 687 | 711 | if (self.debug_str_section_index == null) { |
| 688 | 712 | assert(dw.strtab.buffer.items.len == 0); |
| 689 | 713 | try dw.strtab.buffer.append(gpa, 0); |
| 690 | self.debug_str_section_index = try self.allocateNonAllocSection(.{ | |
| 714 | self.debug_str_section_index = try self.addSection(.{ | |
| 691 | 715 | .name = ".debug_str", |
| 692 | .size = @intCast(dw.strtab.buffer.items.len), | |
| 693 | 716 | .flags = elf.SHF_MERGE | elf.SHF_STRINGS, |
| 694 | 717 | .entsize = 1, |
| 718 | .type = elf.SHT_PROGBITS, | |
| 719 | .addralign = 1, | |
| 720 | .offset = std.math.maxInt(u64), | |
| 695 | 721 | }); |
| 722 | const shdr = &self.shdrs.items[self.debug_str_section_index.?]; | |
| 723 | const size = @as(u64, @intCast(dw.strtab.buffer.items.len)); | |
| 724 | const off = self.findFreeSpace(size, 1); | |
| 725 | shdr.sh_offset = off; | |
| 726 | shdr.sh_size = size; | |
| 696 | 727 | zig_object.debug_strtab_dirty = true; |
| 697 | 728 | } |
| 698 | 729 | |
| 699 | 730 | if (self.debug_info_section_index == null) { |
| 700 | self.debug_info_section_index = try self.allocateNonAllocSection(.{ | |
| 731 | self.debug_info_section_index = try self.addSection(.{ | |
| 701 | 732 | .name = ".debug_info", |
| 702 | .size = 200, | |
| 703 | .alignment = 1, | |
| 733 | .type = elf.SHT_PROGBITS, | |
| 734 | .addralign = 1, | |
| 735 | .offset = std.math.maxInt(u64), | |
| 704 | 736 | }); |
| 737 | const shdr = &self.shdrs.items[self.debug_info_section_index.?]; | |
| 738 | const size: u64 = 200; | |
| 739 | const off = self.findFreeSpace(size, 1); | |
| 740 | shdr.sh_offset = off; | |
| 741 | shdr.sh_size = size; | |
| 705 | 742 | zig_object.debug_info_header_dirty = true; |
| 706 | 743 | } |
| 707 | 744 | |
| 708 | 745 | if (self.debug_abbrev_section_index == null) { |
| 709 | self.debug_abbrev_section_index = try self.allocateNonAllocSection(.{ | |
| 746 | self.debug_abbrev_section_index = try self.addSection(.{ | |
| 710 | 747 | .name = ".debug_abbrev", |
| 711 | .size = 128, | |
| 712 | .alignment = 1, | |
| 748 | .type = elf.SHT_PROGBITS, | |
| 749 | .addralign = 1, | |
| 750 | .offset = std.math.maxInt(u64), | |
| 713 | 751 | }); |
| 752 | const shdr = &self.shdrs.items[self.debug_abbrev_section_index.?]; | |
| 753 | const size: u64 = 128; | |
| 754 | const off = self.findFreeSpace(size, 1); | |
| 755 | shdr.sh_offset = off; | |
| 756 | shdr.sh_size = size; | |
| 714 | 757 | zig_object.debug_abbrev_section_dirty = true; |
| 715 | 758 | } |
| 716 | 759 | |
| 717 | 760 | if (self.debug_aranges_section_index == null) { |
| 718 | self.debug_aranges_section_index = try self.allocateNonAllocSection(.{ | |
| 761 | self.debug_aranges_section_index = try self.addSection(.{ | |
| 719 | 762 | .name = ".debug_aranges", |
| 720 | .size = 160, | |
| 721 | .alignment = 16, | |
| 763 | .type = elf.SHT_PROGBITS, | |
| 764 | .addralign = 16, | |
| 765 | .offset = std.math.maxInt(u64), | |
| 722 | 766 | }); |
| 767 | const shdr = &self.shdrs.items[self.debug_aranges_section_index.?]; | |
| 768 | const size: u64 = 160; | |
| 769 | const off = self.findFreeSpace(size, 16); | |
| 770 | shdr.sh_offset = off; | |
| 771 | shdr.sh_size = size; | |
| 723 | 772 | zig_object.debug_aranges_section_dirty = true; |
| 724 | 773 | } |
| 725 | 774 | |
| 726 | 775 | if (self.debug_line_section_index == null) { |
| 727 | self.debug_line_section_index = try self.allocateNonAllocSection(.{ | |
| 776 | self.debug_line_section_index = try self.addSection(.{ | |
| 728 | 777 | .name = ".debug_line", |
| 729 | .size = 250, | |
| 730 | .alignment = 1, | |
| 778 | .type = elf.SHT_PROGBITS, | |
| 779 | .addralign = 1, | |
| 780 | .offset = std.math.maxInt(u64), | |
| 731 | 781 | }); |
| 782 | const shdr = &self.shdrs.items[self.debug_line_section_index.?]; | |
| 783 | const size: u64 = 250; | |
| 784 | const off = self.findFreeSpace(size, 1); | |
| 785 | shdr.sh_offset = off; | |
| 786 | shdr.sh_size = size; | |
| 732 | 787 | zig_object.debug_line_header_dirty = true; |
| 733 | 788 | } |
| 734 | 789 | } |
| ... | ... | @@ -736,18 +791,18 @@ pub fn initMetadata(self: *Elf) !void { |
| 736 | 791 | |
| 737 | 792 | pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void { |
| 738 | 793 | const shdr = &self.shdrs.items[shdr_index]; |
| 739 | const phdr_index = self.phdr_to_shdr_table.get(shdr_index).?; | |
| 740 | const phdr = &self.phdrs.items[phdr_index]; | |
| 794 | const maybe_phdr = if (self.phdr_to_shdr_table.get(shdr_index)) |phndx| &self.phdrs.items[phndx] else null; | |
| 741 | 795 | const is_zerofill = shdr.sh_type == elf.SHT_NOBITS; |
| 742 | 796 | |
| 743 | 797 | if (needed_size > self.allocatedSize(shdr.sh_offset) and !is_zerofill) { |
| 744 | 798 | const existing_size = shdr.sh_size; |
| 745 | 799 | shdr.sh_size = 0; |
| 746 | 800 | // Must move the entire section. |
| 747 | const new_offset = self.findFreeSpace(needed_size, self.page_size); | |
| 801 | const alignment = if (maybe_phdr) |phdr| phdr.p_align else shdr.sh_addralign; | |
| 802 | const new_offset = self.findFreeSpace(needed_size, alignment); | |
| 748 | 803 | |
| 749 | 804 | log.debug("new '{s}' file offset 0x{x} to 0x{x}", .{ |
| 750 | self.shstrtab.getAssumeExists(shdr.sh_name), | |
| 805 | self.getShString(shdr.sh_name), | |
| 751 | 806 | new_offset, |
| 752 | 807 | new_offset + existing_size, |
| 753 | 808 | }); |
| ... | ... | @@ -757,25 +812,27 @@ pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void { |
| 757 | 812 | if (amt != existing_size) return error.InputOutput; |
| 758 | 813 | |
| 759 | 814 | shdr.sh_offset = new_offset; |
| 760 | phdr.p_offset = new_offset; | |
| 815 | if (maybe_phdr) |phdr| phdr.p_offset = new_offset; | |
| 761 | 816 | } |
| 762 | 817 | |
| 763 | 818 | shdr.sh_size = needed_size; |
| 764 | 819 | if (!is_zerofill) { |
| 765 | phdr.p_filesz = needed_size; | |
| 820 | if (maybe_phdr) |phdr| phdr.p_filesz = needed_size; | |
| 766 | 821 | } |
| 767 | 822 | |
| 768 | const mem_capacity = self.allocatedVirtualSize(phdr.p_vaddr); | |
| 769 | if (needed_size > mem_capacity) { | |
| 770 | var err = try self.addErrorWithNotes(2); | |
| 771 | try err.addMsg(self, "fatal linker error: cannot expand load segment phdr({d}) in virtual memory", .{ | |
| 772 | phdr_index, | |
| 773 | }); | |
| 774 | try err.addNote(self, "TODO: emit relocations to memory locations in self-hosted backends", .{}); | |
| 775 | try err.addNote(self, "as a workaround, try increasing pre-allocated virtual memory of each segment", .{}); | |
| 776 | } | |
| 823 | if (maybe_phdr) |phdr| { | |
| 824 | const mem_capacity = self.allocatedVirtualSize(phdr.p_vaddr); | |
| 825 | if (needed_size > mem_capacity) { | |
| 826 | var err = try self.addErrorWithNotes(2); | |
| 827 | try err.addMsg(self, "fatal linker error: cannot expand load segment phdr({d}) in virtual memory", .{ | |
| 828 | self.phdr_to_shdr_table.get(shdr_index).?, | |
| 829 | }); | |
| 830 | try err.addNote(self, "TODO: emit relocations to memory locations in self-hosted backends", .{}); | |
| 831 | try err.addNote(self, "as a workaround, try increasing pre-allocated virtual memory of each segment", .{}); | |
| 832 | } | |
| 777 | 833 | |
| 778 | phdr.p_memsz = needed_size; | |
| 834 | phdr.p_memsz = needed_size; | |
| 835 | } | |
| 779 | 836 | |
| 780 | 837 | self.markDirty(shdr_index); |
| 781 | 838 | } |
| ... | ... | @@ -796,7 +853,7 @@ pub fn growNonAllocSection( |
| 796 | 853 | const new_offset = self.findFreeSpace(needed_size, min_alignment); |
| 797 | 854 | |
| 798 | 855 | log.debug("new '{s}' file offset 0x{x} to 0x{x}", .{ |
| 799 | self.shstrtab.getAssumeExists(shdr.sh_name), | |
| 856 | self.getShString(shdr.sh_name), | |
| 800 | 857 | new_offset, |
| 801 | 858 | new_offset + existing_size, |
| 802 | 859 | }); |
| ... | ... | @@ -847,10 +904,6 @@ pub fn flush(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) link |
| 847 | 904 | if (use_lld) { |
| 848 | 905 | return self.linkWithLLD(comp, prog_node); |
| 849 | 906 | } |
| 850 | if (self.base.options.output_mode == .Lib and self.isStatic()) { | |
| 851 | // TODO writing static library files | |
| 852 | return error.TODOImplementWritingLibFiles; | |
| 853 | } | |
| 854 | 907 | try self.flushModule(comp, prog_node); |
| 855 | 908 | } |
| 856 | 909 | |
| ... | ... | @@ -886,7 +939,12 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 886 | 939 | } else null; |
| 887 | 940 | const gc_sections = self.base.options.gc_sections orelse false; |
| 888 | 941 | |
| 889 | if (self.base.options.output_mode == .Obj and self.zig_object_index == null) { | |
| 942 | if (self.isRelocatable() and self.zig_object_index == null) { | |
| 943 | if (self.isStaticLib()) { | |
| 944 | var err = try self.addErrorWithNotes(0); | |
| 945 | try err.addMsg(self, "fatal linker error: emitting static libs unimplemented", .{}); | |
| 946 | return; | |
| 947 | } | |
| 890 | 948 | // TODO this will become -r route I guess. For now, just copy the object file. |
| 891 | 949 | assert(self.base.file == null); // TODO uncomment once we implement -r |
| 892 | 950 | const the_object_path = blk: { |
| ... | ... | @@ -1159,6 +1217,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1159 | 1217 | Compilation.dump_argv(argv.items); |
| 1160 | 1218 | } |
| 1161 | 1219 | |
| 1220 | if (self.zigObjectPtr()) |zig_object| try zig_object.flushModule(self); | |
| 1221 | ||
| 1162 | 1222 | // Here we will parse input positional and library files (if referenced). |
| 1163 | 1223 | // This will roughly match in any linker backend we support. |
| 1164 | 1224 | var positionals = std.ArrayList(Compilation.LinkObject).init(arena); |
| ... | ... | @@ -1226,6 +1286,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1226 | 1286 | try positionals.append(.{ .path = ssp.full_object_path }); |
| 1227 | 1287 | } |
| 1228 | 1288 | |
| 1289 | if (self.isStaticLib()) return self.flushStaticLib(comp, positionals.items); | |
| 1290 | ||
| 1229 | 1291 | for (positionals.items) |obj| { |
| 1230 | 1292 | var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined }; |
| 1231 | 1293 | self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch |err| |
| ... | ... | @@ -1331,8 +1393,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1331 | 1393 | try self.handleAndReportParseError(obj.path, err, &parse_ctx); |
| 1332 | 1394 | } |
| 1333 | 1395 | |
| 1334 | if (self.zigObjectPtr()) |zig_object| try zig_object.flushModule(self); | |
| 1335 | ||
| 1336 | 1396 | // Dedup shared objects |
| 1337 | 1397 | { |
| 1338 | 1398 | var seen_dsos = std.StringHashMap(void).init(gpa); |
| ... | ... | @@ -1353,7 +1413,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1353 | 1413 | |
| 1354 | 1414 | // If we haven't already, create a linker-generated input file comprising of |
| 1355 | 1415 | // linker-defined synthetic symbols only such as `_DYNAMIC`, etc. |
| 1356 | if (self.linker_defined_index == null) { | |
| 1416 | if (self.linker_defined_index == null and !self.isRelocatable()) { | |
| 1357 | 1417 | const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); |
| 1358 | 1418 | self.files.set(index, .{ .linker_defined = .{ .index = index } }); |
| 1359 | 1419 | self.linker_defined_index = index; |
| ... | ... | @@ -1366,6 +1426,9 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1366 | 1426 | // symbol for potential resolution at load-time. |
| 1367 | 1427 | self.resolveSymbols(); |
| 1368 | 1428 | self.markEhFrameAtomsDead(); |
| 1429 | ||
| 1430 | if (self.isObject()) return self.flushObject(comp); | |
| 1431 | ||
| 1369 | 1432 | try self.convertCommonSymbols(); |
| 1370 | 1433 | self.markImportsExports(); |
| 1371 | 1434 | |
| ... | ... | @@ -1449,14 +1512,150 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1449 | 1512 | try self.writeAtoms(); |
| 1450 | 1513 | try self.writeSyntheticSections(); |
| 1451 | 1514 | |
| 1452 | if (self.entry_index == null and self.base.options.effectiveOutputMode() == .Exe) { | |
| 1515 | if (self.entry_index == null and self.isExe()) { | |
| 1453 | 1516 | log.debug("flushing. no_entry_point_found = true", .{}); |
| 1454 | 1517 | self.error_flags.no_entry_point_found = true; |
| 1455 | 1518 | } else { |
| 1456 | 1519 | log.debug("flushing. no_entry_point_found = false", .{}); |
| 1457 | 1520 | self.error_flags.no_entry_point_found = false; |
| 1458 | try self.writeHeader(); | |
| 1521 | try self.writeElfHeader(); | |
| 1522 | } | |
| 1523 | } | |
| 1524 | ||
| 1525 | pub fn flushStaticLib( | |
| 1526 | self: *Elf, | |
| 1527 | comp: *Compilation, | |
| 1528 | positionals: []const Compilation.LinkObject, | |
| 1529 | ) link.File.FlushError!void { | |
| 1530 | _ = comp; | |
| 1531 | if (positionals.len > 0) { | |
| 1532 | var err = try self.addErrorWithNotes(1); | |
| 1533 | try err.addMsg(self, "fatal linker error: too many input positionals", .{}); | |
| 1534 | try err.addNote(self, "TODO implement linking objects into an static library", .{}); | |
| 1535 | return; | |
| 1536 | } | |
| 1537 | const gpa = self.base.allocator; | |
| 1538 | ||
| 1539 | // First, we flush relocatable object file generated with our backends. | |
| 1540 | if (self.zigObjectPtr()) |zig_object| { | |
| 1541 | zig_object.resolveSymbols(self); | |
| 1542 | zig_object.claimUnresolvedObject(self); | |
| 1543 | ||
| 1544 | try self.initSymtab(); | |
| 1545 | try self.initShStrtab(); | |
| 1546 | try self.sortShdrs(); | |
| 1547 | zig_object.updateRelaSectionSizes(self); | |
| 1548 | try self.updateSymtabSize(); | |
| 1549 | self.updateShStrtabSize(); | |
| 1550 | ||
| 1551 | try self.allocateNonAllocSections(); | |
| 1552 | ||
| 1553 | try self.writeShdrTable(); | |
| 1554 | try zig_object.writeRelaSections(self); | |
| 1555 | try self.writeSymtab(); | |
| 1556 | try self.writeShStrtab(); | |
| 1557 | try self.writeElfHeader(); | |
| 1558 | } | |
| 1559 | ||
| 1560 | // TODO parse positionals that we want to make part of the archive | |
| 1561 | ||
| 1562 | // TODO update ar symtab from parsed positionals | |
| 1563 | ||
| 1564 | var ar_symtab: Archive.ArSymtab = .{}; | |
| 1565 | defer ar_symtab.deinit(gpa); | |
| 1566 | ||
| 1567 | if (self.zigObjectPtr()) |zig_object| { | |
| 1568 | try zig_object.updateArSymtab(&ar_symtab, self); | |
| 1569 | } | |
| 1570 | ||
| 1571 | ar_symtab.sort(); | |
| 1572 | ||
| 1573 | // Save object paths in filenames strtab. | |
| 1574 | var ar_strtab: Archive.ArStrtab = .{}; | |
| 1575 | defer ar_strtab.deinit(gpa); | |
| 1576 | ||
| 1577 | if (self.zigObjectPtr()) |zig_object| { | |
| 1578 | try zig_object.updateArStrtab(gpa, &ar_strtab); | |
| 1579 | zig_object.updateArSize(self); | |
| 1580 | } | |
| 1581 | ||
| 1582 | // Update file offsets of contributing objects. | |
| 1583 | const total_size: usize = blk: { | |
| 1584 | var pos: usize = Archive.SARMAG; | |
| 1585 | pos += @sizeOf(Archive.ar_hdr) + ar_symtab.size(.p64); | |
| 1586 | ||
| 1587 | if (ar_strtab.size() > 0) { | |
| 1588 | pos = mem.alignForward(usize, pos, 2); | |
| 1589 | pos += @sizeOf(Archive.ar_hdr) + ar_strtab.size(); | |
| 1590 | } | |
| 1591 | ||
| 1592 | if (self.zigObjectPtr()) |zig_object| { | |
| 1593 | pos = mem.alignForward(usize, pos, 2); | |
| 1594 | zig_object.output_ar_state.file_off = pos; | |
| 1595 | pos += @sizeOf(Archive.ar_hdr) + (math.cast(usize, zig_object.output_ar_state.size) orelse return error.Overflow); | |
| 1596 | } | |
| 1597 | ||
| 1598 | break :blk pos; | |
| 1599 | }; | |
| 1600 | ||
| 1601 | if (build_options.enable_logging) { | |
| 1602 | state_log.debug("ar_symtab\n{}\n", .{ar_symtab.fmt(self)}); | |
| 1603 | state_log.debug("ar_strtab\n{}\n", .{ar_strtab}); | |
| 1604 | } | |
| 1605 | ||
| 1606 | var buffer = std.ArrayList(u8).init(gpa); | |
| 1607 | defer buffer.deinit(); | |
| 1608 | try buffer.ensureTotalCapacityPrecise(total_size); | |
| 1609 | ||
| 1610 | // Write magic | |
| 1611 | try buffer.writer().writeAll(Archive.ARMAG); | |
| 1612 | ||
| 1613 | // Write symtab | |
| 1614 | try ar_symtab.write(.p64, self, buffer.writer()); | |
| 1615 | ||
| 1616 | // Write strtab | |
| 1617 | if (ar_strtab.size() > 0) { | |
| 1618 | if (!mem.isAligned(buffer.items.len, 2)) try buffer.writer().writeByte(0); | |
| 1619 | try ar_strtab.write(buffer.writer()); | |
| 1620 | } | |
| 1621 | ||
| 1622 | // Write object files | |
| 1623 | if (self.zigObjectPtr()) |zig_object| { | |
| 1624 | if (!mem.isAligned(buffer.items.len, 2)) try buffer.writer().writeByte(0); | |
| 1625 | try zig_object.writeAr(self, buffer.writer()); | |
| 1626 | } | |
| 1627 | ||
| 1628 | assert(buffer.items.len == total_size); | |
| 1629 | ||
| 1630 | try self.base.file.?.setEndPos(total_size); | |
| 1631 | try self.base.file.?.pwriteAll(buffer.items, 0); | |
| 1632 | } | |
| 1633 | ||
| 1634 | pub fn flushObject(self: *Elf, comp: *Compilation) link.File.FlushError!void { | |
| 1635 | _ = comp; | |
| 1636 | ||
| 1637 | if (self.objects.items.len > 0) { | |
| 1638 | var err = try self.addErrorWithNotes(1); | |
| 1639 | try err.addMsg(self, "fatal linker error: too many input positionals", .{}); | |
| 1640 | try err.addNote(self, "TODO implement '-r' option", .{}); | |
| 1641 | return; | |
| 1642 | } | |
| 1643 | ||
| 1644 | self.claimUnresolvedObject(); | |
| 1645 | ||
| 1646 | try self.initSections(); | |
| 1647 | try self.sortShdrs(); | |
| 1648 | try self.updateSectionSizes(); | |
| 1649 | ||
| 1650 | try self.allocateNonAllocSections(); | |
| 1651 | ||
| 1652 | if (build_options.enable_logging) { | |
| 1653 | state_log.debug("{}", .{self.dumpState()}); | |
| 1459 | 1654 | } |
| 1655 | ||
| 1656 | try self.writeShdrTable(); | |
| 1657 | try self.writeSyntheticSections(); | |
| 1658 | try self.writeElfHeader(); | |
| 1460 | 1659 | } |
| 1461 | 1660 | |
| 1462 | 1661 | const ParseError = error{ |
| ... | ... | @@ -1696,7 +1895,7 @@ fn accessLibPath( |
| 1696 | 1895 | /// 6. Re-run symbol resolution on pruned objects and shared objects sets. |
| 1697 | 1896 | fn resolveSymbols(self: *Elf) void { |
| 1698 | 1897 | // Resolve symbols in the ZigObject. For now, we assume that it's always live. |
| 1699 | if (self.zigObjectPtr()) |zig_object| zig_object.resolveSymbols(self); | |
| 1898 | if (self.zigObjectPtr()) |zig_object| zig_object.asFile().resolveSymbols(self); | |
| 1700 | 1899 | // Resolve symbols on the set of all objects and shared objects (even if some are unneeded). |
| 1701 | 1900 | for (self.objects.items) |index| self.file(index).?.resolveSymbols(self); |
| 1702 | 1901 | for (self.shared_objects.items) |index| self.file(index).?.resolveSymbols(self); |
| ... | ... | @@ -1705,7 +1904,7 @@ fn resolveSymbols(self: *Elf) void { |
| 1705 | 1904 | self.markLive(); |
| 1706 | 1905 | |
| 1707 | 1906 | // Reset state of all globals after marking live objects. |
| 1708 | if (self.zigObjectPtr()) |zig_object| zig_object.resetGlobals(self); | |
| 1907 | if (self.zigObjectPtr()) |zig_object| zig_object.asFile().resetGlobals(self); | |
| 1709 | 1908 | for (self.objects.items) |index| self.file(index).?.resetGlobals(self); |
| 1710 | 1909 | for (self.shared_objects.items) |index| self.file(index).?.resetGlobals(self); |
| 1711 | 1910 | |
| ... | ... | @@ -1767,7 +1966,7 @@ fn resolveSymbols(self: *Elf) void { |
| 1767 | 1966 | /// This routine will prune unneeded objects extracted from archives and |
| 1768 | 1967 | /// unneeded shared objects. |
| 1769 | 1968 | fn markLive(self: *Elf) void { |
| 1770 | if (self.zigObjectPtr()) |zig_object| zig_object.markLive(self); | |
| 1969 | if (self.zigObjectPtr()) |zig_object| zig_object.asFile().markLive(self); | |
| 1771 | 1970 | for (self.objects.items) |index| { |
| 1772 | 1971 | const file_ptr = self.file(index).?; |
| 1773 | 1972 | if (file_ptr.isAlive()) file_ptr.markLive(self); |
| ... | ... | @@ -1845,6 +2044,12 @@ fn claimUnresolved(self: *Elf) void { |
| 1845 | 2044 | } |
| 1846 | 2045 | } |
| 1847 | 2046 | |
| 2047 | fn claimUnresolvedObject(self: *Elf) void { | |
| 2048 | if (self.zigObjectPtr()) |zig_object| { | |
| 2049 | zig_object.claimUnresolvedObject(self); | |
| 2050 | } | |
| 2051 | } | |
| 2052 | ||
| 1848 | 2053 | /// In scanRelocs we will go over all live atoms and scan their relocs. |
| 1849 | 2054 | /// This will help us work out what synthetics to emit, GOT indirection, etc. |
| 1850 | 2055 | /// This is also the point where we will report undefined symbols for any |
| ... | ... | @@ -1873,7 +2078,7 @@ fn scanRelocs(self: *Elf) !void { |
| 1873 | 2078 | |
| 1874 | 2079 | for (self.symbols.items, 0..) |*sym, i| { |
| 1875 | 2080 | const index = @as(u32, @intCast(i)); |
| 1876 | if (!sym.isLocal() and !sym.flags.has_dynamic) { | |
| 2081 | if (!sym.isLocal(self) and !sym.flags.has_dynamic) { | |
| 1877 | 2082 | log.debug("'{s}' is non-local", .{sym.name(self)}); |
| 1878 | 2083 | try self.dynsym.addSymbol(index, self); |
| 1879 | 2084 | } |
| ... | ... | @@ -2704,7 +2909,7 @@ fn writePhdrTable(self: *Elf) !void { |
| 2704 | 2909 | } |
| 2705 | 2910 | } |
| 2706 | 2911 | |
| 2707 | fn writeHeader(self: *Elf) !void { | |
| 2912 | fn writeElfHeader(self: *Elf) !void { | |
| 2708 | 2913 | var hdr_buf: [@sizeOf(elf.Elf64_Ehdr)]u8 = undefined; |
| 2709 | 2914 | |
| 2710 | 2915 | var index: usize = 0; |
| ... | ... | @@ -2735,7 +2940,7 @@ fn writeHeader(self: *Elf) !void { |
| 2735 | 2940 | |
| 2736 | 2941 | assert(index == 16); |
| 2737 | 2942 | |
| 2738 | const elf_type: elf.ET = switch (self.base.options.effectiveOutputMode()) { | |
| 2943 | const elf_type: elf.ET = switch (self.base.options.output_mode) { | |
| 2739 | 2944 | .Exe => if (self.base.options.pie) .DYN else .EXEC, |
| 2740 | 2945 | .Obj => .REL, |
| 2741 | 2946 | .Lib => switch (self.base.options.link_mode) { |
| ... | ... | @@ -2755,7 +2960,7 @@ fn writeHeader(self: *Elf) !void { |
| 2755 | 2960 | index += 4; |
| 2756 | 2961 | |
| 2757 | 2962 | const e_entry = if (self.entry_index) |entry_index| self.symbol(entry_index).value else 0; |
| 2758 | const phdr_table_offset = self.phdrs.items[self.phdr_table_index.?].p_offset; | |
| 2963 | const phdr_table_offset = if (self.phdr_table_index) |phndx| self.phdrs.items[phndx].p_offset else 0; | |
| 2759 | 2964 | switch (self.ptr_width) { |
| 2760 | 2965 | .p32 => { |
| 2761 | 2966 | mem.writeInt(u32, hdr_buf[index..][0..4], @as(u32, @intCast(e_entry)), endian); |
| ... | ... | @@ -3054,10 +3259,6 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 3054 | 3259 | } |
| 3055 | 3260 | |
| 3056 | 3261 | fn initSections(self: *Elf) !void { |
| 3057 | const small_ptr = switch (self.ptr_width) { | |
| 3058 | .p32 => true, | |
| 3059 | .p64 => false, | |
| 3060 | }; | |
| 3061 | 3262 | const ptr_size = self.ptrWidthBytes(); |
| 3062 | 3263 | |
| 3063 | 3264 | for (self.objects.items) |index| { |
| ... | ... | @@ -3247,6 +3448,15 @@ fn initSections(self: *Elf) !void { |
| 3247 | 3448 | } |
| 3248 | 3449 | } |
| 3249 | 3450 | |
| 3451 | try self.initSymtab(); | |
| 3452 | try self.initShStrtab(); | |
| 3453 | } | |
| 3454 | ||
| 3455 | fn initSymtab(self: *Elf) !void { | |
| 3456 | const small_ptr = switch (self.ptr_width) { | |
| 3457 | .p32 => true, | |
| 3458 | .p64 => false, | |
| 3459 | }; | |
| 3250 | 3460 | if (self.symtab_section_index == null) { |
| 3251 | 3461 | self.symtab_section_index = try self.addSection(.{ |
| 3252 | 3462 | .name = ".symtab", |
| ... | ... | @@ -3265,6 +3475,9 @@ fn initSections(self: *Elf) !void { |
| 3265 | 3475 | .offset = std.math.maxInt(u64), |
| 3266 | 3476 | }); |
| 3267 | 3477 | } |
| 3478 | } | |
| 3479 | ||
| 3480 | fn initShStrtab(self: *Elf) !void { | |
| 3268 | 3481 | if (self.shstrtab_section_index == null) { |
| 3269 | 3482 | self.shstrtab_section_index = try self.addSection(.{ |
| 3270 | 3483 | .name = ".shstrtab", |
| ... | ... | @@ -3358,7 +3571,7 @@ fn sortInitFini(self: *Elf) !void { |
| 3358 | 3571 | elf.SHT_FINI_ARRAY, |
| 3359 | 3572 | => is_init_fini = true, |
| 3360 | 3573 | else => { |
| 3361 | const name = self.shstrtab.getAssumeExists(shdr.sh_name); | |
| 3574 | const name = self.getShString(shdr.sh_name); | |
| 3362 | 3575 | is_ctor_dtor = mem.indexOf(u8, name, ".ctors") != null or mem.indexOf(u8, name, ".dtors") != null; |
| 3363 | 3576 | }, |
| 3364 | 3577 | } |
| ... | ... | @@ -3520,7 +3733,7 @@ fn sortPhdrs(self: *Elf) error{OutOfMemory}!void { |
| 3520 | 3733 | |
| 3521 | 3734 | fn shdrRank(self: *Elf, shndx: u16) u8 { |
| 3522 | 3735 | const shdr = self.shdrs.items[shndx]; |
| 3523 | const name = self.shstrtab.getAssumeExists(shdr.sh_name); | |
| 3736 | const name = self.getShString(shdr.sh_name); | |
| 3524 | 3737 | const flags = shdr.sh_flags; |
| 3525 | 3738 | |
| 3526 | 3739 | switch (shdr.sh_type) { |
| ... | ... | @@ -3620,9 +3833,12 @@ fn sortShdrs(self: *Elf) !void { |
| 3620 | 3833 | &self.versym_section_index, |
| 3621 | 3834 | &self.verneed_section_index, |
| 3622 | 3835 | &self.zig_text_section_index, |
| 3836 | &self.zig_text_rela_section_index, | |
| 3623 | 3837 | &self.zig_got_section_index, |
| 3624 | &self.zig_rodata_section_index, | |
| 3838 | &self.zig_data_rel_ro_section_index, | |
| 3839 | &self.zig_data_rel_ro_rela_section_index, | |
| 3625 | 3840 | &self.zig_data_section_index, |
| 3841 | &self.zig_data_rela_section_index, | |
| 3626 | 3842 | &self.zig_bss_section_index, |
| 3627 | 3843 | &self.debug_str_section_index, |
| 3628 | 3844 | &self.debug_info_section_index, |
| ... | ... | @@ -3681,6 +3897,31 @@ fn sortShdrs(self: *Elf) !void { |
| 3681 | 3897 | shdr.sh_info = self.plt_section_index.?; |
| 3682 | 3898 | } |
| 3683 | 3899 | |
| 3900 | for (&[_]?u16{ | |
| 3901 | self.zig_text_rela_section_index, | |
| 3902 | self.zig_data_rel_ro_rela_section_index, | |
| 3903 | self.zig_data_rela_section_index, | |
| 3904 | }) |maybe_index| { | |
| 3905 | const index = maybe_index orelse continue; | |
| 3906 | const shdr = &self.shdrs.items[index]; | |
| 3907 | shdr.sh_link = self.symtab_section_index.?; | |
| 3908 | shdr.sh_info = backlinks[shdr.sh_info]; | |
| 3909 | } | |
| 3910 | ||
| 3911 | { | |
| 3912 | var last_atom_and_free_list_table = try self.last_atom_and_free_list_table.clone(gpa); | |
| 3913 | defer last_atom_and_free_list_table.deinit(gpa); | |
| 3914 | ||
| 3915 | self.last_atom_and_free_list_table.clearRetainingCapacity(); | |
| 3916 | ||
| 3917 | var it = last_atom_and_free_list_table.iterator(); | |
| 3918 | while (it.next()) |entry| { | |
| 3919 | const shndx = entry.key_ptr.*; | |
| 3920 | const meta = entry.value_ptr.*; | |
| 3921 | self.last_atom_and_free_list_table.putAssumeCapacityNoClobber(backlinks[shndx], meta); | |
| 3922 | } | |
| 3923 | } | |
| 3924 | ||
| 3684 | 3925 | { |
| 3685 | 3926 | var phdr_to_shdr_table = try self.phdr_to_shdr_table.clone(gpa); |
| 3686 | 3927 | defer phdr_to_shdr_table.deinit(gpa); |
| ... | ... | @@ -3698,23 +3939,19 @@ fn sortShdrs(self: *Elf) !void { |
| 3698 | 3939 | if (self.zigObjectPtr()) |zig_object| { |
| 3699 | 3940 | for (zig_object.atoms.items) |atom_index| { |
| 3700 | 3941 | const atom_ptr = self.atom(atom_index) orelse continue; |
| 3701 | if (!atom_ptr.flags.alive) continue; | |
| 3702 | const out_shndx = atom_ptr.outputShndx() orelse continue; | |
| 3703 | atom_ptr.output_section_index = backlinks[out_shndx]; | |
| 3942 | atom_ptr.output_section_index = backlinks[atom_ptr.output_section_index]; | |
| 3704 | 3943 | } |
| 3705 | 3944 | |
| 3706 | 3945 | for (zig_object.locals()) |local_index| { |
| 3707 | 3946 | const local = self.symbol(local_index); |
| 3708 | const atom_ptr = local.atom(self) orelse continue; | |
| 3709 | if (!atom_ptr.flags.alive) continue; | |
| 3710 | const out_shndx = local.outputShndx() orelse continue; | |
| 3711 | local.output_section_index = backlinks[out_shndx]; | |
| 3947 | local.output_section_index = backlinks[local.output_section_index]; | |
| 3712 | 3948 | } |
| 3713 | 3949 | |
| 3714 | 3950 | for (zig_object.globals()) |global_index| { |
| 3715 | 3951 | const global = self.symbol(global_index); |
| 3716 | 3952 | const atom_ptr = global.atom(self) orelse continue; |
| 3717 | 3953 | if (!atom_ptr.flags.alive) continue; |
| 3954 | // TODO claim unresolved for objects | |
| 3718 | 3955 | if (global.file(self).?.index() != zig_object.index) continue; |
| 3719 | 3956 | const out_shndx = global.outputShndx() orelse continue; |
| 3720 | 3957 | global.output_section_index = backlinks[out_shndx]; |
| ... | ... | @@ -3737,6 +3974,10 @@ fn updateSectionSizes(self: *Elf) !void { |
| 3737 | 3974 | } |
| 3738 | 3975 | } |
| 3739 | 3976 | |
| 3977 | if (self.zigObjectPtr()) |zig_object| { | |
| 3978 | zig_object.updateRelaSectionSizes(self); | |
| 3979 | } | |
| 3980 | ||
| 3740 | 3981 | if (self.eh_frame_section_index) |index| { |
| 3741 | 3982 | self.shdrs.items[index].sh_size = try eh_frame.calcEhFrameSize(self); |
| 3742 | 3983 | } |
| ... | ... | @@ -3801,7 +4042,7 @@ fn updateSectionSizes(self: *Elf) !void { |
| 3801 | 4042 | } |
| 3802 | 4043 | |
| 3803 | 4044 | if (self.dynstrtab_section_index) |index| { |
| 3804 | self.shdrs.items[index].sh_size = self.dynstrtab.buffer.items.len; | |
| 4045 | self.shdrs.items[index].sh_size = self.dynstrtab.items.len; | |
| 3805 | 4046 | } |
| 3806 | 4047 | |
| 3807 | 4048 | if (self.versym_section_index) |index| { |
| ... | ... | @@ -3812,30 +4053,13 @@ fn updateSectionSizes(self: *Elf) !void { |
| 3812 | 4053 | self.shdrs.items[index].sh_size = self.verneed.size(); |
| 3813 | 4054 | } |
| 3814 | 4055 | |
| 3815 | if (self.symtab_section_index != null) { | |
| 3816 | try self.updateSymtabSize(); | |
| 3817 | } | |
| 3818 | ||
| 3819 | if (self.strtab_section_index) |index| { | |
| 3820 | // TODO I don't really this here but we need it to add symbol names from GOT and other synthetic | |
| 3821 | // sections into .strtab for easier debugging. | |
| 3822 | if (self.zig_got_section_index) |_| { | |
| 3823 | try self.zig_got.updateStrtab(self); | |
| 3824 | } | |
| 3825 | if (self.got_section_index) |_| { | |
| 3826 | try self.got.updateStrtab(self); | |
| 3827 | } | |
| 3828 | if (self.plt_section_index) |_| { | |
| 3829 | try self.plt.updateStrtab(self); | |
| 3830 | } | |
| 3831 | if (self.plt_got_section_index) |_| { | |
| 3832 | try self.plt_got.updateStrtab(self); | |
| 3833 | } | |
| 3834 | self.shdrs.items[index].sh_size = self.strtab.buffer.items.len; | |
| 3835 | } | |
| 4056 | try self.updateSymtabSize(); | |
| 4057 | self.updateShStrtabSize(); | |
| 4058 | } | |
| 3836 | 4059 | |
| 4060 | fn updateShStrtabSize(self: *Elf) void { | |
| 3837 | 4061 | if (self.shstrtab_section_index) |index| { |
| 3838 | self.shdrs.items[index].sh_size = self.shstrtab.buffer.items.len; | |
| 4062 | self.shdrs.items[index].sh_size = self.shstrtab.items.len; | |
| 3839 | 4063 | } |
| 3840 | 4064 | } |
| 3841 | 4065 | |
| ... | ... | @@ -4074,7 +4298,7 @@ fn allocateNonAllocSections(self: *Elf) !void { |
| 4074 | 4298 | |
| 4075 | 4299 | if (self.isDebugSection(@intCast(shndx))) { |
| 4076 | 4300 | log.debug("moving {s} from 0x{x} to 0x{x}", .{ |
| 4077 | self.shstrtab.getAssumeExists(shdr.sh_name), | |
| 4301 | self.getShString(shdr.sh_name), | |
| 4078 | 4302 | shdr.sh_offset, |
| 4079 | 4303 | new_offset, |
| 4080 | 4304 | }); |
| ... | ... | @@ -4187,7 +4411,7 @@ fn writeAtoms(self: *Elf) !void { |
| 4187 | 4411 | |
| 4188 | 4412 | const atom_list = self.output_sections.get(@intCast(shndx)) orelse continue; |
| 4189 | 4413 | |
| 4190 | log.debug("writing atoms in '{s}' section", .{self.shstrtab.getAssumeExists(shdr.sh_name)}); | |
| 4414 | log.debug("writing atoms in '{s}' section", .{self.getShString(shdr.sh_name)}); | |
| 4191 | 4415 | |
| 4192 | 4416 | // TODO really, really handle debug section separately |
| 4193 | 4417 | const base_offset = if (self.isDebugSection(@intCast(shndx))) blk: { |
| ... | ... | @@ -4256,65 +4480,70 @@ fn updateSymtabSize(self: *Elf) !void { |
| 4256 | 4480 | var sizes = SymtabSize{}; |
| 4257 | 4481 | |
| 4258 | 4482 | if (self.zigObjectPtr()) |zig_object| { |
| 4259 | zig_object.updateSymtabSize(self); | |
| 4260 | sizes.nlocals += zig_object.output_symtab_size.nlocals; | |
| 4261 | sizes.nglobals += zig_object.output_symtab_size.nglobals; | |
| 4483 | zig_object.asFile().updateSymtabSize(self); | |
| 4484 | sizes.add(zig_object.output_symtab_size); | |
| 4262 | 4485 | } |
| 4263 | 4486 | |
| 4264 | 4487 | for (self.objects.items) |index| { |
| 4265 | const object = self.file(index).?.object; | |
| 4266 | object.updateSymtabSize(self); | |
| 4267 | sizes.nlocals += object.output_symtab_size.nlocals; | |
| 4268 | sizes.nglobals += object.output_symtab_size.nglobals; | |
| 4488 | const file_ptr = self.file(index).?; | |
| 4489 | file_ptr.updateSymtabSize(self); | |
| 4490 | sizes.add(file_ptr.object.output_symtab_size); | |
| 4269 | 4491 | } |
| 4270 | 4492 | |
| 4271 | 4493 | for (self.shared_objects.items) |index| { |
| 4272 | const shared_object = self.file(index).?.shared_object; | |
| 4273 | shared_object.updateSymtabSize(self); | |
| 4274 | sizes.nglobals += shared_object.output_symtab_size.nglobals; | |
| 4494 | const file_ptr = self.file(index).?; | |
| 4495 | file_ptr.updateSymtabSize(self); | |
| 4496 | sizes.add(file_ptr.shared_object.output_symtab_size); | |
| 4275 | 4497 | } |
| 4276 | 4498 | |
| 4277 | 4499 | if (self.zig_got_section_index) |_| { |
| 4278 | 4500 | self.zig_got.updateSymtabSize(self); |
| 4279 | sizes.nlocals += self.zig_got.output_symtab_size.nlocals; | |
| 4501 | sizes.add(self.zig_got.output_symtab_size); | |
| 4280 | 4502 | } |
| 4281 | 4503 | |
| 4282 | 4504 | if (self.got_section_index) |_| { |
| 4283 | 4505 | self.got.updateSymtabSize(self); |
| 4284 | sizes.nlocals += self.got.output_symtab_size.nlocals; | |
| 4506 | sizes.add(self.got.output_symtab_size); | |
| 4285 | 4507 | } |
| 4286 | 4508 | |
| 4287 | 4509 | if (self.plt_section_index) |_| { |
| 4288 | 4510 | self.plt.updateSymtabSize(self); |
| 4289 | sizes.nlocals += self.plt.output_symtab_size.nlocals; | |
| 4511 | sizes.add(self.plt.output_symtab_size); | |
| 4290 | 4512 | } |
| 4291 | 4513 | |
| 4292 | 4514 | if (self.plt_got_section_index) |_| { |
| 4293 | 4515 | self.plt_got.updateSymtabSize(self); |
| 4294 | sizes.nlocals += self.plt_got.output_symtab_size.nlocals; | |
| 4516 | sizes.add(self.plt_got.output_symtab_size); | |
| 4295 | 4517 | } |
| 4296 | 4518 | |
| 4297 | 4519 | if (self.linker_defined_index) |index| { |
| 4298 | const linker_defined = self.file(index).?.linker_defined; | |
| 4299 | linker_defined.updateSymtabSize(self); | |
| 4300 | sizes.nlocals += linker_defined.output_symtab_size.nlocals; | |
| 4520 | const file_ptr = self.file(index).?; | |
| 4521 | file_ptr.updateSymtabSize(self); | |
| 4522 | sizes.add(file_ptr.linker_defined.output_symtab_size); | |
| 4301 | 4523 | } |
| 4302 | 4524 | |
| 4303 | const shdr = &self.shdrs.items[self.symtab_section_index.?]; | |
| 4304 | shdr.sh_info = sizes.nlocals + 1; | |
| 4305 | shdr.sh_link = self.strtab_section_index.?; | |
| 4525 | const symtab_shdr = &self.shdrs.items[self.symtab_section_index.?]; | |
| 4526 | symtab_shdr.sh_info = sizes.nlocals + 1; | |
| 4527 | symtab_shdr.sh_link = self.strtab_section_index.?; | |
| 4306 | 4528 | |
| 4307 | 4529 | const sym_size: u64 = switch (self.ptr_width) { |
| 4308 | 4530 | .p32 => @sizeOf(elf.Elf32_Sym), |
| 4309 | 4531 | .p64 => @sizeOf(elf.Elf64_Sym), |
| 4310 | 4532 | }; |
| 4311 | 4533 | const needed_size = (sizes.nlocals + sizes.nglobals + 1) * sym_size; |
| 4312 | shdr.sh_size = needed_size; | |
| 4534 | symtab_shdr.sh_size = needed_size; | |
| 4535 | ||
| 4536 | const strtab = &self.shdrs.items[self.strtab_section_index.?]; | |
| 4537 | strtab.sh_size = sizes.strsize + 1; | |
| 4313 | 4538 | } |
| 4314 | 4539 | |
| 4315 | 4540 | fn writeSyntheticSections(self: *Elf) !void { |
| 4316 | 4541 | const gpa = self.base.allocator; |
| 4317 | 4542 | |
| 4543 | if (self.zigObjectPtr()) |zig_object| { | |
| 4544 | try zig_object.writeRelaSections(self); | |
| 4545 | } | |
| 4546 | ||
| 4318 | 4547 | if (self.interp_section_index) |shndx| { |
| 4319 | 4548 | const shdr = self.shdrs.items[shndx]; |
| 4320 | 4549 | const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow; |
| ... | ... | @@ -4370,7 +4599,7 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4370 | 4599 | |
| 4371 | 4600 | if (self.dynstrtab_section_index) |shndx| { |
| 4372 | 4601 | const shdr = self.shdrs.items[shndx]; |
| 4373 | try self.base.file.?.pwriteAll(self.dynstrtab.buffer.items, shdr.sh_offset); | |
| 4602 | try self.base.file.?.pwriteAll(self.dynstrtab.items, shdr.sh_offset); | |
| 4374 | 4603 | } |
| 4375 | 4604 | |
| 4376 | 4605 | if (self.eh_frame_section_index) |shndx| { |
| ... | ... | @@ -4438,94 +4667,97 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4438 | 4667 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.rela_plt.items), shdr.sh_offset); |
| 4439 | 4668 | } |
| 4440 | 4669 | |
| 4441 | if (self.shstrtab_section_index) |index| { | |
| 4442 | const shdr = self.shdrs.items[index]; | |
| 4443 | try self.base.file.?.pwriteAll(self.shstrtab.buffer.items, shdr.sh_offset); | |
| 4444 | } | |
| 4670 | try self.writeSymtab(); | |
| 4671 | try self.writeShStrtab(); | |
| 4672 | } | |
| 4445 | 4673 | |
| 4446 | if (self.strtab_section_index) |index| { | |
| 4674 | fn writeShStrtab(self: *Elf) !void { | |
| 4675 | if (self.shstrtab_section_index) |index| { | |
| 4447 | 4676 | const shdr = self.shdrs.items[index]; |
| 4448 | try self.base.file.?.pwriteAll(self.strtab.buffer.items, shdr.sh_offset); | |
| 4449 | } | |
| 4450 | ||
| 4451 | if (self.symtab_section_index) |_| { | |
| 4452 | try self.writeSymtab(); | |
| 4677 | try self.base.file.?.pwriteAll(self.shstrtab.items, shdr.sh_offset); | |
| 4453 | 4678 | } |
| 4454 | 4679 | } |
| 4455 | 4680 | |
| 4456 | 4681 | fn writeSymtab(self: *Elf) !void { |
| 4457 | 4682 | const gpa = self.base.allocator; |
| 4458 | const shdr = &self.shdrs.items[self.symtab_section_index.?]; | |
| 4683 | const symtab_shdr = self.shdrs.items[self.symtab_section_index.?]; | |
| 4684 | const strtab_shdr = self.shdrs.items[self.strtab_section_index.?]; | |
| 4459 | 4685 | const sym_size: u64 = switch (self.ptr_width) { |
| 4460 | 4686 | .p32 => @sizeOf(elf.Elf32_Sym), |
| 4461 | 4687 | .p64 => @sizeOf(elf.Elf64_Sym), |
| 4462 | 4688 | }; |
| 4463 | const nsyms = math.cast(usize, @divExact(shdr.sh_size, sym_size)) orelse return error.Overflow; | |
| 4689 | const nsyms = math.cast(usize, @divExact(symtab_shdr.sh_size, sym_size)) orelse return error.Overflow; | |
| 4464 | 4690 | |
| 4465 | log.debug("writing {d} symbols at 0x{x}", .{ nsyms, shdr.sh_offset }); | |
| 4691 | log.debug("writing {d} symbols at 0x{x}", .{ nsyms, symtab_shdr.sh_offset }); | |
| 4466 | 4692 | |
| 4467 | const symtab = try gpa.alloc(elf.Elf64_Sym, nsyms); | |
| 4468 | defer gpa.free(symtab); | |
| 4469 | symtab[0] = null_sym; | |
| 4693 | try self.symtab.resize(gpa, nsyms); | |
| 4694 | const needed_strtab_size = math.cast(usize, strtab_shdr.sh_size - 1) orelse return error.Overflow; | |
| 4695 | try self.strtab.ensureUnusedCapacity(gpa, needed_strtab_size); | |
| 4470 | 4696 | |
| 4471 | var ctx: struct { ilocal: usize, iglobal: usize, symtab: []elf.Elf64_Sym } = .{ | |
| 4697 | const Ctx = struct { | |
| 4698 | ilocal: usize, | |
| 4699 | iglobal: usize, | |
| 4700 | ||
| 4701 | fn incr(this: *@This(), ss: SymtabSize) void { | |
| 4702 | this.ilocal += ss.nlocals; | |
| 4703 | this.iglobal += ss.nglobals; | |
| 4704 | } | |
| 4705 | }; | |
| 4706 | var ctx: Ctx = .{ | |
| 4472 | 4707 | .ilocal = 1, |
| 4473 | .iglobal = shdr.sh_info, | |
| 4474 | .symtab = symtab, | |
| 4708 | .iglobal = symtab_shdr.sh_info, | |
| 4475 | 4709 | }; |
| 4476 | 4710 | |
| 4477 | 4711 | if (self.zigObjectPtr()) |zig_object| { |
| 4478 | zig_object.writeSymtab(self, ctx); | |
| 4479 | ctx.ilocal += zig_object.output_symtab_size.nlocals; | |
| 4480 | ctx.iglobal += zig_object.output_symtab_size.nglobals; | |
| 4712 | zig_object.asFile().writeSymtab(self, ctx); | |
| 4713 | ctx.incr(zig_object.output_symtab_size); | |
| 4481 | 4714 | } |
| 4482 | 4715 | |
| 4483 | 4716 | for (self.objects.items) |index| { |
| 4484 | const object = self.file(index).?.object; | |
| 4485 | object.writeSymtab(self, ctx); | |
| 4486 | ctx.ilocal += object.output_symtab_size.nlocals; | |
| 4487 | ctx.iglobal += object.output_symtab_size.nglobals; | |
| 4717 | const file_ptr = self.file(index).?; | |
| 4718 | file_ptr.writeSymtab(self, ctx); | |
| 4719 | ctx.incr(file_ptr.object.output_symtab_size); | |
| 4488 | 4720 | } |
| 4489 | 4721 | |
| 4490 | 4722 | for (self.shared_objects.items) |index| { |
| 4491 | const shared_object = self.file(index).?.shared_object; | |
| 4492 | shared_object.writeSymtab(self, ctx); | |
| 4493 | ctx.iglobal += shared_object.output_symtab_size.nglobals; | |
| 4723 | const file_ptr = self.file(index).?; | |
| 4724 | file_ptr.writeSymtab(self, ctx); | |
| 4725 | ctx.incr(file_ptr.shared_object.output_symtab_size); | |
| 4494 | 4726 | } |
| 4495 | 4727 | |
| 4496 | 4728 | if (self.zig_got_section_index) |_| { |
| 4497 | try self.zig_got.writeSymtab(self, ctx); | |
| 4498 | ctx.ilocal += self.zig_got.output_symtab_size.nlocals; | |
| 4729 | self.zig_got.writeSymtab(self, ctx); | |
| 4730 | ctx.incr(self.zig_got.output_symtab_size); | |
| 4499 | 4731 | } |
| 4500 | 4732 | |
| 4501 | 4733 | if (self.got_section_index) |_| { |
| 4502 | try self.got.writeSymtab(self, ctx); | |
| 4503 | ctx.ilocal += self.got.output_symtab_size.nlocals; | |
| 4734 | self.got.writeSymtab(self, ctx); | |
| 4735 | ctx.incr(self.got.output_symtab_size); | |
| 4504 | 4736 | } |
| 4505 | 4737 | |
| 4506 | 4738 | if (self.plt_section_index) |_| { |
| 4507 | try self.plt.writeSymtab(self, ctx); | |
| 4508 | ctx.ilocal += self.plt.output_symtab_size.nlocals; | |
| 4739 | self.plt.writeSymtab(self, ctx); | |
| 4740 | ctx.incr(self.plt.output_symtab_size); | |
| 4509 | 4741 | } |
| 4510 | 4742 | |
| 4511 | 4743 | if (self.plt_got_section_index) |_| { |
| 4512 | try self.plt_got.writeSymtab(self, ctx); | |
| 4513 | ctx.ilocal += self.plt_got.output_symtab_size.nlocals; | |
| 4744 | self.plt_got.writeSymtab(self, ctx); | |
| 4745 | ctx.incr(self.plt_got.output_symtab_size); | |
| 4514 | 4746 | } |
| 4515 | 4747 | |
| 4516 | 4748 | if (self.linker_defined_index) |index| { |
| 4517 | const linker_defined = self.file(index).?.linker_defined; | |
| 4518 | linker_defined.writeSymtab(self, ctx); | |
| 4519 | ctx.ilocal += linker_defined.output_symtab_size.nlocals; | |
| 4749 | const file_ptr = self.file(index).?; | |
| 4750 | file_ptr.writeSymtab(self, ctx); | |
| 4751 | ctx.incr(file_ptr.linker_defined.output_symtab_size); | |
| 4520 | 4752 | } |
| 4521 | 4753 | |
| 4522 | 4754 | const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian(); |
| 4523 | 4755 | switch (self.ptr_width) { |
| 4524 | 4756 | .p32 => { |
| 4525 | const buf = try gpa.alloc(elf.Elf32_Sym, symtab.len); | |
| 4757 | const buf = try gpa.alloc(elf.Elf32_Sym, self.symtab.items.len); | |
| 4526 | 4758 | defer gpa.free(buf); |
| 4527 | 4759 | |
| 4528 | for (buf, symtab) |*out, sym| { | |
| 4760 | for (buf, self.symtab.items) |*out, sym| { | |
| 4529 | 4761 | out.* = .{ |
| 4530 | 4762 | .st_name = sym.st_name, |
| 4531 | 4763 | .st_info = sym.st_info, |
| ... | ... | @@ -4536,15 +4768,17 @@ fn writeSymtab(self: *Elf) !void { |
| 4536 | 4768 | }; |
| 4537 | 4769 | if (foreign_endian) mem.byteSwapAllFields(elf.Elf32_Sym, out); |
| 4538 | 4770 | } |
| 4539 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), shdr.sh_offset); | |
| 4771 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), symtab_shdr.sh_offset); | |
| 4540 | 4772 | }, |
| 4541 | 4773 | .p64 => { |
| 4542 | 4774 | if (foreign_endian) { |
| 4543 | for (symtab) |*sym| mem.byteSwapAllFields(elf.Elf64_Sym, sym); | |
| 4775 | for (self.symtab.items) |*sym| mem.byteSwapAllFields(elf.Elf64_Sym, sym); | |
| 4544 | 4776 | } |
| 4545 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(symtab), shdr.sh_offset); | |
| 4777 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.symtab.items), symtab_shdr.sh_offset); | |
| 4546 | 4778 | }, |
| 4547 | 4779 | } |
| 4780 | ||
| 4781 | try self.base.file.?.pwriteAll(self.strtab.items, strtab_shdr.sh_offset); | |
| 4548 | 4782 | } |
| 4549 | 4783 | |
| 4550 | 4784 | /// Always 4 or 8 depending on whether this is 32-bit ELF or 64-bit ELF. |
| ... | ... | @@ -4848,18 +5082,30 @@ pub fn isStatic(self: Elf) bool { |
| 4848 | 5082 | return self.base.options.link_mode == .Static; |
| 4849 | 5083 | } |
| 4850 | 5084 | |
| 5085 | pub fn isObject(self: Elf) bool { | |
| 5086 | return self.base.options.output_mode == .Obj; | |
| 5087 | } | |
| 5088 | ||
| 4851 | 5089 | pub fn isExe(self: Elf) bool { |
| 4852 | return self.base.options.effectiveOutputMode() == .Exe; | |
| 5090 | return self.base.options.output_mode == .Exe; | |
| 5091 | } | |
| 5092 | ||
| 5093 | pub fn isStaticLib(self: Elf) bool { | |
| 5094 | return self.base.options.output_mode == .Lib and self.isStatic(); | |
| 5095 | } | |
| 5096 | ||
| 5097 | pub fn isRelocatable(self: Elf) bool { | |
| 5098 | return self.isObject() or self.isStaticLib(); | |
| 4853 | 5099 | } |
| 4854 | 5100 | |
| 4855 | 5101 | pub fn isDynLib(self: Elf) bool { |
| 4856 | return self.base.options.effectiveOutputMode() == .Lib and self.base.options.link_mode == .Dynamic; | |
| 5102 | return self.base.options.output_mode == .Lib and !self.isStatic(); | |
| 4857 | 5103 | } |
| 4858 | 5104 | |
| 4859 | 5105 | pub fn isZigSection(self: Elf, shndx: u16) bool { |
| 4860 | 5106 | inline for (&[_]?u16{ |
| 4861 | 5107 | self.zig_text_section_index, |
| 4862 | self.zig_rodata_section_index, | |
| 5108 | self.zig_data_rel_ro_section_index, | |
| 4863 | 5109 | self.zig_data_section_index, |
| 4864 | 5110 | self.zig_bss_section_index, |
| 4865 | 5111 | self.zig_got_section_index, |
| ... | ... | @@ -4909,6 +5155,26 @@ fn addPhdr(self: *Elf, opts: struct { |
| 4909 | 5155 | return index; |
| 4910 | 5156 | } |
| 4911 | 5157 | |
| 5158 | fn addRelaShdr(self: *Elf, name: [:0]const u8, shndx: u16) !u16 { | |
| 5159 | const entsize: u64 = switch (self.ptr_width) { | |
| 5160 | .p32 => @sizeOf(elf.Elf32_Rela), | |
| 5161 | .p64 => @sizeOf(elf.Elf64_Rela), | |
| 5162 | }; | |
| 5163 | const addralign: u64 = switch (self.ptr_width) { | |
| 5164 | .p32 => @alignOf(elf.Elf32_Rela), | |
| 5165 | .p64 => @alignOf(elf.Elf64_Rela), | |
| 5166 | }; | |
| 5167 | return self.addSection(.{ | |
| 5168 | .name = name, | |
| 5169 | .type = elf.SHT_RELA, | |
| 5170 | .flags = elf.SHF_INFO_LINK, | |
| 5171 | .entsize = entsize, | |
| 5172 | .info = shndx, | |
| 5173 | .addralign = addralign, | |
| 5174 | .offset = std.math.maxInt(u64), | |
| 5175 | }); | |
| 5176 | } | |
| 5177 | ||
| 4912 | 5178 | pub const AddSectionOpts = struct { |
| 4913 | 5179 | name: [:0]const u8, |
| 4914 | 5180 | type: u32 = elf.SHT_NULL, |
| ... | ... | @@ -4925,7 +5191,7 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 { |
| 4925 | 5191 | const index = @as(u16, @intCast(self.shdrs.items.len)); |
| 4926 | 5192 | const shdr = try self.shdrs.addOne(gpa); |
| 4927 | 5193 | shdr.* = .{ |
| 4928 | .sh_name = try self.shstrtab.insert(gpa, opts.name), | |
| 5194 | .sh_name = try self.insertShString(opts.name), | |
| 4929 | 5195 | .sh_type = opts.type, |
| 4930 | 5196 | .sh_flags = opts.flags, |
| 4931 | 5197 | .sh_addr = 0, |
| ... | ... | @@ -4941,7 +5207,7 @@ pub fn addSection(self: *Elf, opts: AddSectionOpts) !u16 { |
| 4941 | 5207 | |
| 4942 | 5208 | pub fn sectionByName(self: *Elf, name: [:0]const u8) ?u16 { |
| 4943 | 5209 | for (self.shdrs.items, 0..) |*shdr, i| { |
| 4944 | const this_name = self.shstrtab.getAssumeExists(shdr.sh_name); | |
| 5210 | const this_name = self.getShString(shdr.sh_name); | |
| 4945 | 5211 | if (mem.eql(u8, this_name, name)) return @as(u16, @intCast(i)); |
| 4946 | 5212 | } else return null; |
| 4947 | 5213 | } |
| ... | ... | @@ -5114,13 +5380,15 @@ const GetOrPutGlobalResult = struct { |
| 5114 | 5380 | index: Symbol.Index, |
| 5115 | 5381 | }; |
| 5116 | 5382 | |
| 5117 | pub fn getOrPutGlobal(self: *Elf, name_off: u32) !GetOrPutGlobalResult { | |
| 5383 | pub fn getOrPutGlobal(self: *Elf, name: []const u8) !GetOrPutGlobalResult { | |
| 5118 | 5384 | const gpa = self.base.allocator; |
| 5385 | const name_off = try self.strings.insert(gpa, name); | |
| 5119 | 5386 | const gop = try self.resolver.getOrPut(gpa, name_off); |
| 5120 | 5387 | if (!gop.found_existing) { |
| 5121 | 5388 | const index = try self.addSymbol(); |
| 5122 | 5389 | const global = self.symbol(index); |
| 5123 | 5390 | global.name_offset = name_off; |
| 5391 | global.flags.global = true; | |
| 5124 | 5392 | gop.value_ptr.* = index; |
| 5125 | 5393 | } |
| 5126 | 5394 | return .{ |
| ... | ... | @@ -5130,7 +5398,7 @@ pub fn getOrPutGlobal(self: *Elf, name_off: u32) !GetOrPutGlobalResult { |
| 5130 | 5398 | } |
| 5131 | 5399 | |
| 5132 | 5400 | pub fn globalByName(self: *Elf, name: []const u8) ?Symbol.Index { |
| 5133 | const name_off = self.strtab.getOffset(name) orelse return null; | |
| 5401 | const name_off = self.strings.getOffset(name) orelse return null; | |
| 5134 | 5402 | return self.resolver.get(name_off); |
| 5135 | 5403 | } |
| 5136 | 5404 | |
| ... | ... | @@ -5148,8 +5416,9 @@ const GetOrCreateComdatGroupOwnerResult = struct { |
| 5148 | 5416 | index: ComdatGroupOwner.Index, |
| 5149 | 5417 | }; |
| 5150 | 5418 | |
| 5151 | pub fn getOrCreateComdatGroupOwner(self: *Elf, off: u32) !GetOrCreateComdatGroupOwnerResult { | |
| 5419 | pub fn getOrCreateComdatGroupOwner(self: *Elf, name: [:0]const u8) !GetOrCreateComdatGroupOwnerResult { | |
| 5152 | 5420 | const gpa = self.base.allocator; |
| 5421 | const off = try self.strings.insert(gpa, name); | |
| 5153 | 5422 | const gop = try self.comdat_groups_table.getOrPut(gpa, off); |
| 5154 | 5423 | if (!gop.found_existing) { |
| 5155 | 5424 | const index = @as(ComdatGroupOwner.Index, @intCast(self.comdat_groups_owners.items.len)); |
| ... | ... | @@ -5239,6 +5508,30 @@ fn addErrorWithNotesAssumeCapacity(self: *Elf, note_count: usize) error{OutOfMem |
| 5239 | 5508 | return .{ .index = index }; |
| 5240 | 5509 | } |
| 5241 | 5510 | |
| 5511 | pub fn getShString(self: Elf, off: u32) [:0]const u8 { | |
| 5512 | assert(off < self.shstrtab.items.len); | |
| 5513 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.shstrtab.items.ptr + off)), 0); | |
| 5514 | } | |
| 5515 | ||
| 5516 | pub fn insertShString(self: *Elf, name: [:0]const u8) error{OutOfMemory}!u32 { | |
| 5517 | const off = @as(u32, @intCast(self.shstrtab.items.len)); | |
| 5518 | try self.shstrtab.ensureUnusedCapacity(self.base.allocator, name.len + 1); | |
| 5519 | self.shstrtab.writer(self.base.allocator).print("{s}\x00", .{name}) catch unreachable; | |
| 5520 | return off; | |
| 5521 | } | |
| 5522 | ||
| 5523 | pub fn getDynString(self: Elf, off: u32) [:0]const u8 { | |
| 5524 | assert(off < self.dynstrtab.items.len); | |
| 5525 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.dynstrtab.items.ptr + off)), 0); | |
| 5526 | } | |
| 5527 | ||
| 5528 | pub fn insertDynString(self: *Elf, name: []const u8) error{OutOfMemory}!u32 { | |
| 5529 | const off = @as(u32, @intCast(self.dynstrtab.items.len)); | |
| 5530 | try self.dynstrtab.ensureUnusedCapacity(self.base.allocator, name.len + 1); | |
| 5531 | self.dynstrtab.writer(self.base.allocator).print("{s}\x00", .{name}) catch unreachable; | |
| 5532 | return off; | |
| 5533 | } | |
| 5534 | ||
| 5242 | 5535 | fn reportUndefined(self: *Elf, undefs: anytype) !void { |
| 5243 | 5536 | const gpa = self.base.allocator; |
| 5244 | 5537 | const max_notes = 4; |
| ... | ... | @@ -5340,8 +5633,8 @@ fn formatShdr( |
| 5340 | 5633 | _ = unused_fmt_string; |
| 5341 | 5634 | const shdr = ctx.shdr; |
| 5342 | 5635 | try writer.print("{s} : @{x} ({x}) : align({x}) : size({x})", .{ |
| 5343 | ctx.elf_file.shstrtab.getAssumeExists(shdr.sh_name), shdr.sh_offset, | |
| 5344 | shdr.sh_addr, shdr.sh_addralign, | |
| 5636 | ctx.elf_file.getShString(shdr.sh_name), shdr.sh_offset, | |
| 5637 | shdr.sh_addr, shdr.sh_addralign, | |
| 5345 | 5638 | shdr.sh_size, |
| 5346 | 5639 | }); |
| 5347 | 5640 | } |
| ... | ... | @@ -5444,6 +5737,7 @@ fn fmtDumpState( |
| 5444 | 5737 | } |
| 5445 | 5738 | try writer.print("{}\n", .{self.got.fmt(self)}); |
| 5446 | 5739 | try writer.print("{}\n", .{self.zig_got.fmt(self)}); |
| 5740 | ||
| 5447 | 5741 | try writer.writeAll("Output shdrs\n"); |
| 5448 | 5742 | for (self.shdrs.items, 0..) |shdr, shndx| { |
| 5449 | 5743 | try writer.print("shdr({d}) : phdr({?d}) : {}\n", .{ |
| ... | ... | @@ -5516,6 +5810,13 @@ pub const ComdatGroup = struct { |
| 5516 | 5810 | pub const SymtabSize = struct { |
| 5517 | 5811 | nlocals: u32 = 0, |
| 5518 | 5812 | nglobals: u32 = 0, |
| 5813 | strsize: u32 = 0, | |
| 5814 | ||
| 5815 | fn add(ss: *SymtabSize, other: SymtabSize) void { | |
| 5816 | ss.nlocals += other.nlocals; | |
| 5817 | ss.nglobals += other.nglobals; | |
| 5818 | ss.strsize += other.strsize; | |
| 5819 | } | |
| 5519 | 5820 | }; |
| 5520 | 5821 | |
| 5521 | 5822 | pub const null_sym = elf.Elf64_Sym{ |
| ... | ... | @@ -5621,7 +5922,7 @@ const PltSection = synthetic_sections.PltSection; |
| 5621 | 5922 | const PltGotSection = synthetic_sections.PltGotSection; |
| 5622 | 5923 | const SharedObject = @import("Elf/SharedObject.zig"); |
| 5623 | 5924 | const Symbol = @import("Elf/Symbol.zig"); |
| 5624 | const StringTable = @import("strtab.zig").StringTable; | |
| 5925 | const StringTable = @import("StringTable.zig"); | |
| 5625 | 5926 | const TypedValue = @import("../TypedValue.zig"); |
| 5626 | 5927 | const VerneedSection = synthetic_sections.VerneedSection; |
| 5627 | 5928 | const ZigGotSection = synthetic_sections.ZigGotSection; |
src/link/Elf/Archive.zig+272-63| ... | ... | @@ -4,20 +4,150 @@ data: []const u8, |
| 4 | 4 | objects: std.ArrayListUnmanaged(Object) = .{}, |
| 5 | 5 | strtab: []const u8 = &[0]u8{}, |
| 6 | 6 | |
| 7 | pub fn isArchive(path: []const u8) !bool { | |
| 8 | const file = try std.fs.cwd().openFile(path, .{}); | |
| 9 | defer file.close(); | |
| 10 | const reader = file.reader(); | |
| 11 | const magic = reader.readBytesNoEof(SARMAG) catch return false; | |
| 12 | if (!mem.eql(u8, &magic, ARMAG)) return false; | |
| 13 | return true; | |
| 14 | } | |
| 15 | ||
| 16 | pub fn deinit(self: *Archive, allocator: Allocator) void { | |
| 17 | allocator.free(self.path); | |
| 18 | allocator.free(self.data); | |
| 19 | self.objects.deinit(allocator); | |
| 20 | } | |
| 21 | ||
| 22 | pub fn parse(self: *Archive, elf_file: *Elf) !void { | |
| 23 | const gpa = elf_file.base.allocator; | |
| 24 | ||
| 25 | var stream = std.io.fixedBufferStream(self.data); | |
| 26 | const reader = stream.reader(); | |
| 27 | _ = try reader.readBytesNoEof(SARMAG); | |
| 28 | ||
| 29 | while (true) { | |
| 30 | if (stream.pos >= self.data.len) break; | |
| 31 | ||
| 32 | if (stream.pos % 2 != 0) { | |
| 33 | stream.pos += 1; | |
| 34 | } | |
| 35 | const hdr = try reader.readStruct(ar_hdr); | |
| 36 | ||
| 37 | if (!mem.eql(u8, &hdr.ar_fmag, ARFMAG)) { | |
| 38 | // TODO convert into an error | |
| 39 | log.debug( | |
| 40 | "{s}: invalid header delimiter: expected '{s}', found '{s}'", | |
| 41 | .{ self.path, std.fmt.fmtSliceEscapeLower(ARFMAG), std.fmt.fmtSliceEscapeLower(&hdr.ar_fmag) }, | |
| 42 | ); | |
| 43 | return; | |
| 44 | } | |
| 45 | ||
| 46 | const size = try hdr.size(); | |
| 47 | defer { | |
| 48 | _ = stream.seekBy(size) catch {}; | |
| 49 | } | |
| 50 | ||
| 51 | if (hdr.isSymtab()) continue; | |
| 52 | if (hdr.isStrtab()) { | |
| 53 | self.strtab = self.data[stream.pos..][0..size]; | |
| 54 | continue; | |
| 55 | } | |
| 56 | ||
| 57 | const name = ar_hdr.getValue(&hdr.ar_name); | |
| 58 | ||
| 59 | if (mem.eql(u8, name, "__.SYMDEF") or mem.eql(u8, name, "__.SYMDEF SORTED")) continue; | |
| 60 | ||
| 61 | const object_name = blk: { | |
| 62 | if (name[0] == '/') { | |
| 63 | const off = try std.fmt.parseInt(u32, name[1..], 10); | |
| 64 | const object_name = self.getString(off); | |
| 65 | break :blk try gpa.dupe(u8, object_name[0 .. object_name.len - 1]); // To account for trailing '/' | |
| 66 | } | |
| 67 | break :blk try gpa.dupe(u8, name); | |
| 68 | }; | |
| 69 | ||
| 70 | const object = Object{ | |
| 71 | .archive = try gpa.dupe(u8, self.path), | |
| 72 | .path = object_name, | |
| 73 | .data = try gpa.dupe(u8, self.data[stream.pos..][0..size]), | |
| 74 | .index = undefined, | |
| 75 | .alive = false, | |
| 76 | }; | |
| 77 | ||
| 78 | log.debug("extracting object '{s}' from archive '{s}'", .{ object.path, self.path }); | |
| 79 | ||
| 80 | try self.objects.append(gpa, object); | |
| 81 | } | |
| 82 | } | |
| 83 | ||
| 84 | fn getString(self: Archive, off: u32) []const u8 { | |
| 85 | assert(off < self.strtab.len); | |
| 86 | return mem.sliceTo(@as([*:strtab_delimiter]const u8, @ptrCast(self.strtab.ptr + off)), 0); | |
| 87 | } | |
| 88 | ||
| 89 | pub fn setArHdr(opts: struct { | |
| 90 | name: union(enum) { | |
| 91 | symtab: void, | |
| 92 | strtab: void, | |
| 93 | name: []const u8, | |
| 94 | name_off: u32, | |
| 95 | }, | |
| 96 | size: u32, | |
| 97 | }) ar_hdr { | |
| 98 | var hdr: ar_hdr = .{ | |
| 99 | .ar_name = undefined, | |
| 100 | .ar_date = undefined, | |
| 101 | .ar_uid = undefined, | |
| 102 | .ar_gid = undefined, | |
| 103 | .ar_mode = undefined, | |
| 104 | .ar_size = undefined, | |
| 105 | .ar_fmag = undefined, | |
| 106 | }; | |
| 107 | @memset(mem.asBytes(&hdr), 0x20); | |
| 108 | @memcpy(&hdr.ar_fmag, Archive.ARFMAG); | |
| 109 | ||
| 110 | { | |
| 111 | var stream = std.io.fixedBufferStream(&hdr.ar_name); | |
| 112 | const writer = stream.writer(); | |
| 113 | switch (opts.name) { | |
| 114 | .symtab => writer.print("{s}", .{Archive.SYM64NAME}) catch unreachable, | |
| 115 | .strtab => writer.print("//", .{}) catch unreachable, | |
| 116 | .name => |x| writer.print("{s}", .{x}) catch unreachable, | |
| 117 | .name_off => |x| writer.print("/{d}", .{x}) catch unreachable, | |
| 118 | } | |
| 119 | } | |
| 120 | { | |
| 121 | var stream = std.io.fixedBufferStream(&hdr.ar_size); | |
| 122 | stream.writer().print("{d}", .{opts.size}) catch unreachable; | |
| 123 | } | |
| 124 | ||
| 125 | return hdr; | |
| 126 | } | |
| 127 | ||
| 7 | 128 | // Archive files start with the ARMAG identifying string. Then follows a |
| 8 | 129 | // `struct ar_hdr', and as many bytes of member file data as its `ar_size' |
| 9 | 130 | // member indicates, for each member file. |
| 10 | 131 | /// String that begins an archive file. |
| 11 | 132 | pub const ARMAG: *const [SARMAG:0]u8 = "!<arch>\n"; |
| 12 | 133 | /// Size of that string. |
| 13 | pub const SARMAG: u4 = 8; | |
| 134 | pub const SARMAG = 8; | |
| 14 | 135 | |
| 15 | 136 | /// String in ar_fmag at the end of each header. |
| 16 | 137 | const ARFMAG: *const [2:0]u8 = "`\n"; |
| 17 | 138 | |
| 139 | /// Strtab identifier | |
| 140 | const STRNAME: *const [2:0]u8 = "//"; | |
| 141 | ||
| 142 | /// 32-bit symtab identifier | |
| 143 | const SYMNAME: *const [1:0]u8 = "/"; | |
| 144 | ||
| 145 | /// 64-bit symtab identifier | |
| 18 | 146 | const SYM64NAME: *const [7:0]u8 = "/SYM64/"; |
| 19 | 147 | |
| 20 | const ar_hdr = extern struct { | |
| 148 | const strtab_delimiter = '\n'; | |
| 149 | ||
| 150 | pub const ar_hdr = extern struct { | |
| 21 | 151 | /// Member file name, sometimes / terminated. |
| 22 | 152 | ar_name: [16]u8, |
| 23 | 153 | |
| ... | ... | @@ -54,93 +184,170 @@ const ar_hdr = extern struct { |
| 54 | 184 | } |
| 55 | 185 | |
| 56 | 186 | fn isStrtab(self: ar_hdr) bool { |
| 57 | return mem.eql(u8, getValue(&self.ar_name), "//"); | |
| 187 | return mem.eql(u8, getValue(&self.ar_name), STRNAME); | |
| 58 | 188 | } |
| 59 | 189 | |
| 60 | 190 | fn isSymtab(self: ar_hdr) bool { |
| 61 | return mem.eql(u8, getValue(&self.ar_name), "/"); | |
| 191 | return mem.eql(u8, getValue(&self.ar_name), SYMNAME) or mem.eql(u8, getValue(&self.ar_name), SYM64NAME); | |
| 62 | 192 | } |
| 63 | 193 | }; |
| 64 | 194 | |
| 65 | pub fn isArchive(path: []const u8) !bool { | |
| 66 | const file = try std.fs.cwd().openFile(path, .{}); | |
| 67 | defer file.close(); | |
| 68 | const reader = file.reader(); | |
| 69 | const magic = reader.readBytesNoEof(Archive.SARMAG) catch return false; | |
| 70 | if (!mem.eql(u8, &magic, ARMAG)) return false; | |
| 71 | return true; | |
| 72 | } | |
| 195 | pub const ArSymtab = struct { | |
| 196 | symtab: std.ArrayListUnmanaged(Entry) = .{}, | |
| 197 | strtab: StringTable = .{}, | |
| 73 | 198 | |
| 74 | pub fn deinit(self: *Archive, allocator: Allocator) void { | |
| 75 | allocator.free(self.path); | |
| 76 | allocator.free(self.data); | |
| 77 | self.objects.deinit(allocator); | |
| 78 | } | |
| 199 | pub fn deinit(ar: *ArSymtab, allocator: Allocator) void { | |
| 200 | ar.symtab.deinit(allocator); | |
| 201 | ar.strtab.deinit(allocator); | |
| 202 | } | |
| 79 | 203 | |
| 80 | pub fn parse(self: *Archive, elf_file: *Elf) !void { | |
| 81 | const gpa = elf_file.base.allocator; | |
| 204 | pub fn sort(ar: *ArSymtab) void { | |
| 205 | mem.sort(Entry, ar.symtab.items, {}, Entry.lessThan); | |
| 206 | } | |
| 82 | 207 | |
| 83 | var stream = std.io.fixedBufferStream(self.data); | |
| 84 | const reader = stream.reader(); | |
| 85 | _ = try reader.readBytesNoEof(SARMAG); | |
| 208 | pub fn size(ar: ArSymtab, kind: enum { p32, p64 }) usize { | |
| 209 | const ptr_size: usize = switch (kind) { | |
| 210 | .p32 => 4, | |
| 211 | .p64 => 8, | |
| 212 | }; | |
| 213 | var ss: usize = ptr_size + ar.symtab.items.len * ptr_size; | |
| 214 | for (ar.symtab.items) |entry| { | |
| 215 | ss += ar.strtab.getAssumeExists(entry.off).len + 1; | |
| 216 | } | |
| 217 | return ss; | |
| 218 | } | |
| 86 | 219 | |
| 87 | while (true) { | |
| 88 | if (stream.pos % 2 != 0) { | |
| 89 | stream.pos += 1; | |
| 220 | pub fn write(ar: ArSymtab, kind: enum { p32, p64 }, elf_file: *Elf, writer: anytype) !void { | |
| 221 | assert(kind == .p64); // TODO p32 | |
| 222 | const hdr = setArHdr(.{ .name = .symtab, .size = @intCast(ar.size(.p64)) }); | |
| 223 | try writer.writeAll(mem.asBytes(&hdr)); | |
| 224 | ||
| 225 | const gpa = elf_file.base.allocator; | |
| 226 | var offsets = std.AutoHashMap(File.Index, u64).init(gpa); | |
| 227 | defer offsets.deinit(); | |
| 228 | try offsets.ensureUnusedCapacity(@intCast(elf_file.objects.items.len + 1)); | |
| 229 | ||
| 230 | if (elf_file.zigObjectPtr()) |zig_object| { | |
| 231 | offsets.putAssumeCapacityNoClobber(zig_object.index, zig_object.output_ar_state.file_off); | |
| 90 | 232 | } |
| 91 | 233 | |
| 92 | const hdr = reader.readStruct(ar_hdr) catch break; | |
| 234 | // Number of symbols | |
| 235 | try writer.writeInt(u64, @as(u64, @intCast(ar.symtab.items.len)), .big); | |
| 93 | 236 | |
| 94 | if (!mem.eql(u8, &hdr.ar_fmag, ARFMAG)) { | |
| 95 | // TODO convert into an error | |
| 96 | log.debug( | |
| 97 | "{s}: invalid header delimiter: expected '{s}', found '{s}'", | |
| 98 | .{ self.path, std.fmt.fmtSliceEscapeLower(ARFMAG), std.fmt.fmtSliceEscapeLower(&hdr.ar_fmag) }, | |
| 99 | ); | |
| 100 | return; | |
| 237 | // Offsets to files | |
| 238 | for (ar.symtab.items) |entry| { | |
| 239 | const off = offsets.get(entry.file_index).?; | |
| 240 | try writer.writeInt(u64, off, .big); | |
| 101 | 241 | } |
| 102 | 242 | |
| 103 | const size = try hdr.size(); | |
| 104 | defer { | |
| 105 | _ = stream.seekBy(size) catch {}; | |
| 243 | // Strings | |
| 244 | for (ar.symtab.items) |entry| { | |
| 245 | try writer.print("{s}\x00", .{ar.strtab.getAssumeExists(entry.off)}); | |
| 106 | 246 | } |
| 247 | } | |
| 107 | 248 | |
| 108 | if (hdr.isSymtab()) continue; | |
| 109 | if (hdr.isStrtab()) { | |
| 110 | self.strtab = self.data[stream.pos..][0..size]; | |
| 111 | continue; | |
| 249 | pub fn format( | |
| 250 | ar: ArSymtab, | |
| 251 | comptime unused_fmt_string: []const u8, | |
| 252 | options: std.fmt.FormatOptions, | |
| 253 | writer: anytype, | |
| 254 | ) !void { | |
| 255 | _ = ar; | |
| 256 | _ = unused_fmt_string; | |
| 257 | _ = options; | |
| 258 | _ = writer; | |
| 259 | @compileError("do not format ar symtab directly; use fmt instead"); | |
| 260 | } | |
| 261 | ||
| 262 | const FormatContext = struct { | |
| 263 | ar: ArSymtab, | |
| 264 | elf_file: *Elf, | |
| 265 | }; | |
| 266 | ||
| 267 | pub fn fmt(ar: ArSymtab, elf_file: *Elf) std.fmt.Formatter(format2) { | |
| 268 | return .{ .data = .{ | |
| 269 | .ar = ar, | |
| 270 | .elf_file = elf_file, | |
| 271 | } }; | |
| 272 | } | |
| 273 | ||
| 274 | fn format2( | |
| 275 | ctx: FormatContext, | |
| 276 | comptime unused_fmt_string: []const u8, | |
| 277 | options: std.fmt.FormatOptions, | |
| 278 | writer: anytype, | |
| 279 | ) !void { | |
| 280 | _ = unused_fmt_string; | |
| 281 | _ = options; | |
| 282 | const ar = ctx.ar; | |
| 283 | const elf_file = ctx.elf_file; | |
| 284 | for (ar.symtab.items, 0..) |entry, i| { | |
| 285 | const name = ar.strtab.getAssumeExists(entry.off); | |
| 286 | const file = elf_file.file(entry.file_index).?; | |
| 287 | try writer.print(" {d}: {s} in file({d})({})\n", .{ i, name, entry.file_index, file.fmtPath() }); | |
| 112 | 288 | } |
| 289 | } | |
| 113 | 290 | |
| 114 | const name = ar_hdr.getValue(&hdr.ar_name); | |
| 291 | const Entry = struct { | |
| 292 | /// Offset into the string table. | |
| 293 | off: u32, | |
| 294 | /// Index of the file defining the global. | |
| 295 | file_index: File.Index, | |
| 115 | 296 | |
| 116 | if (mem.eql(u8, name, "__.SYMDEF") or mem.eql(u8, name, "__.SYMDEF SORTED")) continue; | |
| 297 | pub fn lessThan(ctx: void, lhs: Entry, rhs: Entry) bool { | |
| 298 | _ = ctx; | |
| 299 | if (lhs.off == rhs.off) return lhs.file_index < rhs.file_index; | |
| 300 | return lhs.off < rhs.off; | |
| 301 | } | |
| 302 | }; | |
| 303 | }; | |
| 117 | 304 | |
| 118 | const object_name = blk: { | |
| 119 | if (name[0] == '/') { | |
| 120 | const off = try std.fmt.parseInt(u32, name[1..], 10); | |
| 121 | break :blk self.getString(off); | |
| 122 | } | |
| 123 | break :blk name; | |
| 124 | }; | |
| 305 | pub const ArStrtab = struct { | |
| 306 | buffer: std.ArrayListUnmanaged(u8) = .{}, | |
| 125 | 307 | |
| 126 | const object = Object{ | |
| 127 | .archive = try gpa.dupe(u8, self.path), | |
| 128 | .path = try gpa.dupe(u8, object_name[0 .. object_name.len - 1]), // To account for trailing '/' | |
| 129 | .data = try gpa.dupe(u8, self.data[stream.pos..][0..size]), | |
| 130 | .index = undefined, | |
| 131 | .alive = false, | |
| 132 | }; | |
| 308 | pub fn deinit(ar: *ArStrtab, allocator: Allocator) void { | |
| 309 | ar.buffer.deinit(allocator); | |
| 310 | } | |
| 133 | 311 | |
| 134 | log.debug("extracting object '{s}' from archive '{s}'", .{ object.path, self.path }); | |
| 312 | pub fn insert(ar: *ArStrtab, allocator: Allocator, name: []const u8) error{OutOfMemory}!u32 { | |
| 313 | const off = @as(u32, @intCast(ar.buffer.items.len)); | |
| 314 | try ar.buffer.writer(allocator).print("{s}/{c}", .{ name, strtab_delimiter }); | |
| 315 | return off; | |
| 316 | } | |
| 135 | 317 | |
| 136 | try self.objects.append(gpa, object); | |
| 318 | pub fn size(ar: ArStrtab) usize { | |
| 319 | return ar.buffer.items.len; | |
| 137 | 320 | } |
| 138 | } | |
| 139 | 321 | |
| 140 | fn getString(self: Archive, off: u32) []const u8 { | |
| 141 | assert(off < self.strtab.len); | |
| 142 | return mem.sliceTo(@as([*:'\n']const u8, @ptrCast(self.strtab.ptr + off)), 0); | |
| 143 | } | |
| 322 | pub fn write(ar: ArStrtab, writer: anytype) !void { | |
| 323 | const hdr = setArHdr(.{ .name = .strtab, .size = @intCast(ar.size()) }); | |
| 324 | try writer.writeAll(mem.asBytes(&hdr)); | |
| 325 | try writer.writeAll(ar.buffer.items); | |
| 326 | } | |
| 327 | ||
| 328 | pub fn format( | |
| 329 | ar: ArStrtab, | |
| 330 | comptime unused_fmt_string: []const u8, | |
| 331 | options: std.fmt.FormatOptions, | |
| 332 | writer: anytype, | |
| 333 | ) !void { | |
| 334 | _ = unused_fmt_string; | |
| 335 | _ = options; | |
| 336 | try writer.print("{s}", .{std.fmt.fmtSliceEscapeLower(ar.buffer.items)}); | |
| 337 | } | |
| 338 | }; | |
| 339 | ||
| 340 | pub const ArState = struct { | |
| 341 | /// Name offset in the string table. | |
| 342 | name_off: u32 = 0, | |
| 343 | ||
| 344 | /// File offset of the ar_hdr describing the contributing | |
| 345 | /// object in the archive. | |
| 346 | file_off: u64 = 0, | |
| 347 | ||
| 348 | /// Total size of the contributing object (excludes ar_hdr). | |
| 349 | size: u64 = 0, | |
| 350 | }; | |
| 144 | 351 | |
| 145 | 352 | const std = @import("std"); |
| 146 | 353 | const assert = std.debug.assert; |
| ... | ... | @@ -152,4 +359,6 @@ const mem = std.mem; |
| 152 | 359 | const Allocator = mem.Allocator; |
| 153 | 360 | const Archive = @This(); |
| 154 | 361 | const Elf = @import("../Elf.zig"); |
| 362 | const File = @import("file.zig").File; | |
| 155 | 363 | const Object = @import("Object.zig"); |
| 364 | const StringTable = @import("../StringTable.zig"); |
src/link/Elf/Atom.zig+7-3| ... | ... | @@ -42,7 +42,10 @@ next_index: Index = 0, |
| 42 | 42 | pub const Alignment = @import("../../InternPool.zig").Alignment; |
| 43 | 43 | |
| 44 | 44 | pub fn name(self: Atom, elf_file: *Elf) []const u8 { |
| 45 | return elf_file.strtab.getAssumeExists(self.name_offset); | |
| 45 | const file_ptr = self.file(elf_file).?; | |
| 46 | return switch (file_ptr) { | |
| 47 | inline else => |x| x.getString(self.name_offset), | |
| 48 | }; | |
| 46 | 49 | } |
| 47 | 50 | |
| 48 | 51 | pub fn file(self: Atom, elf_file: *Elf) ?File { |
| ... | ... | @@ -602,7 +605,8 @@ fn dynAbsRelocAction(symbol: *const Symbol, elf_file: *Elf) RelocAction { |
| 602 | 605 | } |
| 603 | 606 | |
| 604 | 607 | fn outputType(elf_file: *Elf) u2 { |
| 605 | return switch (elf_file.base.options.effectiveOutputMode()) { | |
| 608 | assert(!elf_file.isRelocatable()); | |
| 609 | return switch (elf_file.base.options.output_mode) { | |
| 606 | 610 | .Obj => unreachable, |
| 607 | 611 | .Lib => 0, |
| 608 | 612 | .Exe => if (elf_file.base.options.pie) 1 else 2, |
| ... | ... | @@ -692,7 +696,7 @@ fn reportUndefined( |
| 692 | 696 | ) !void { |
| 693 | 697 | const rel_esym = switch (self.file(elf_file).?) { |
| 694 | 698 | .zig_object => |x| x.elfSym(rel.r_sym()).*, |
| 695 | .object => |x| x.symtab[rel.r_sym()], | |
| 699 | .object => |x| x.symtab.items[rel.r_sym()], | |
| 696 | 700 | else => unreachable, |
| 697 | 701 | }; |
| 698 | 702 | const esym = sym.elfSym(elf_file); |
src/link/Elf/LinkerDefined.zig+15-27| ... | ... | @@ -1,11 +1,13 @@ |
| 1 | 1 | index: File.Index, |
| 2 | 2 | symtab: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{}, |
| 3 | strtab: std.ArrayListUnmanaged(u8) = .{}, | |
| 3 | 4 | symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 4 | 5 | |
| 5 | 6 | output_symtab_size: Elf.SymtabSize = .{}, |
| 6 | 7 | |
| 7 | 8 | pub fn deinit(self: *LinkerDefined, allocator: Allocator) void { |
| 8 | 9 | self.symtab.deinit(allocator); |
| 10 | self.strtab.deinit(allocator); | |
| 9 | 11 | self.symbols.deinit(allocator); |
| 10 | 12 | } |
| 11 | 13 | |
| ... | ... | @@ -13,16 +15,17 @@ pub fn addGlobal(self: *LinkerDefined, name: [:0]const u8, elf_file: *Elf) !u32 |
| 13 | 15 | const gpa = elf_file.base.allocator; |
| 14 | 16 | try self.symtab.ensureUnusedCapacity(gpa, 1); |
| 15 | 17 | try self.symbols.ensureUnusedCapacity(gpa, 1); |
| 18 | const name_off = @as(u32, @intCast(self.strtab.items.len)); | |
| 19 | try self.strtab.writer(gpa).print("{s}\x00", .{name}); | |
| 16 | 20 | self.symtab.appendAssumeCapacity(.{ |
| 17 | .st_name = try elf_file.strtab.insert(gpa, name), | |
| 21 | .st_name = name_off, | |
| 18 | 22 | .st_info = elf.STB_GLOBAL << 4, |
| 19 | 23 | .st_other = @intFromEnum(elf.STV.HIDDEN), |
| 20 | 24 | .st_shndx = elf.SHN_ABS, |
| 21 | 25 | .st_value = 0, |
| 22 | 26 | .st_size = 0, |
| 23 | 27 | }); |
| 24 | const off = try elf_file.strtab.insert(gpa, name); | |
| 25 | const gop = try elf_file.getOrPutGlobal(off); | |
| 28 | const gop = try elf_file.getOrPutGlobal(name); | |
| 26 | 29 | self.symbols.addOneAssumeCapacity().* = gop.index; |
| 27 | 30 | return gop.index; |
| 28 | 31 | } |
| ... | ... | @@ -37,7 +40,6 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void { |
| 37 | 40 | const global = elf_file.symbol(index); |
| 38 | 41 | if (self.asFile().symbolRank(this_sym, false) < global.symbolRank(elf_file)) { |
| 39 | 42 | global.value = 0; |
| 40 | global.name_offset = global.name_offset; | |
| 41 | 43 | global.atom_index = 0; |
| 42 | 44 | global.file_index = self.index; |
| 43 | 45 | global.esym_index = sym_idx; |
| ... | ... | @@ -46,26 +48,6 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void { |
| 46 | 48 | } |
| 47 | 49 | } |
| 48 | 50 | |
| 49 | pub fn updateSymtabSize(self: *LinkerDefined, elf_file: *Elf) void { | |
| 50 | for (self.globals()) |global_index| { | |
| 51 | const global = elf_file.symbol(global_index); | |
| 52 | if (global.file(elf_file)) |file| if (file.index() != self.index) continue; | |
| 53 | global.flags.output_symtab = true; | |
| 54 | self.output_symtab_size.nlocals += 1; | |
| 55 | } | |
| 56 | } | |
| 57 | ||
| 58 | pub fn writeSymtab(self: *LinkerDefined, elf_file: *Elf, ctx: anytype) void { | |
| 59 | var ilocal = ctx.ilocal; | |
| 60 | for (self.globals()) |global_index| { | |
| 61 | const global = elf_file.symbol(global_index); | |
| 62 | if (global.file(elf_file)) |file| if (file.index() != self.index) continue; | |
| 63 | if (!global.flags.output_symtab) continue; | |
| 64 | global.setOutputSym(elf_file, &ctx.symtab[ilocal]); | |
| 65 | ilocal += 1; | |
| 66 | } | |
| 67 | } | |
| 68 | ||
| 69 | 51 | pub fn globals(self: *LinkerDefined) []const Symbol.Index { |
| 70 | 52 | return self.symbols.items; |
| 71 | 53 | } |
| ... | ... | @@ -74,6 +56,11 @@ pub fn asFile(self: *LinkerDefined) File { |
| 74 | 56 | return .{ .linker_defined = self }; |
| 75 | 57 | } |
| 76 | 58 | |
| 59 | pub fn getString(self: LinkerDefined, off: u32) [:0]const u8 { | |
| 60 | assert(off < self.strtab.items.len); | |
| 61 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0); | |
| 62 | } | |
| 63 | ||
| 77 | 64 | pub fn fmtSymtab(self: *LinkerDefined, elf_file: *Elf) std.fmt.Formatter(formatSymtab) { |
| 78 | 65 | return .{ .data = .{ |
| 79 | 66 | .self = self, |
| ... | ... | @@ -101,12 +88,13 @@ fn formatSymtab( |
| 101 | 88 | } |
| 102 | 89 | } |
| 103 | 90 | |
| 104 | const std = @import("std"); | |
| 91 | const assert = std.debug.assert; | |
| 105 | 92 | const elf = std.elf; |
| 93 | const mem = std.mem; | |
| 94 | const std = @import("std"); | |
| 106 | 95 | |
| 107 | const Allocator = std.mem.Allocator; | |
| 96 | const Allocator = mem.Allocator; | |
| 108 | 97 | const Elf = @import("../Elf.zig"); |
| 109 | 98 | const File = @import("file.zig").File; |
| 110 | 99 | const LinkerDefined = @This(); |
| 111 | // const Object = @import("Object.zig"); | |
| 112 | 100 | const Symbol = @import("Symbol.zig"); |
src/link/Elf/Object.zig+51-116| ... | ... | @@ -5,11 +5,10 @@ index: File.Index, |
| 5 | 5 | |
| 6 | 6 | header: ?elf.Elf64_Ehdr = null, |
| 7 | 7 | shdrs: std.ArrayListUnmanaged(ElfShdr) = .{}, |
| 8 | strings: StringTable(.object_strings) = .{}, | |
| 9 | symtab: []align(1) const elf.Elf64_Sym = &[0]elf.Elf64_Sym{}, | |
| 10 | strtab: []const u8 = &[0]u8{}, | |
| 11 | first_global: ?Symbol.Index = null, | |
| 12 | 8 | |
| 9 | symtab: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{}, | |
| 10 | strtab: std.ArrayListUnmanaged(u8) = .{}, | |
| 11 | first_global: ?Symbol.Index = null, | |
| 13 | 12 | symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 14 | 13 | atoms: std.ArrayListUnmanaged(Atom.Index) = .{}, |
| 15 | 14 | comdat_groups: std.ArrayListUnmanaged(Elf.ComdatGroup.Index) = .{}, |
| ... | ... | @@ -39,7 +38,8 @@ pub fn deinit(self: *Object, allocator: Allocator) void { |
| 39 | 38 | allocator.free(self.path); |
| 40 | 39 | allocator.free(self.data); |
| 41 | 40 | self.shdrs.deinit(allocator); |
| 42 | self.strings.deinit(allocator); | |
| 41 | self.symtab.deinit(allocator); | |
| 42 | self.strtab.deinit(allocator); | |
| 43 | 43 | self.symbols.deinit(allocator); |
| 44 | 44 | self.atoms.deinit(allocator); |
| 45 | 45 | self.comdat_groups.deinit(allocator); |
| ... | ... | @@ -68,7 +68,7 @@ pub fn parse(self: *Object, elf_file: *Elf) !void { |
| 68 | 68 | self.shdrs.appendAssumeCapacity(try ElfShdr.fromElf64Shdr(shdr)); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | try self.strings.buffer.appendSlice(gpa, self.shdrContents(self.header.?.e_shstrndx)); | |
| 71 | try self.strtab.appendSlice(gpa, self.shdrContents(self.header.?.e_shstrndx)); | |
| 72 | 72 | |
| 73 | 73 | const symtab_index = for (self.shdrs.items, 0..) |shdr, i| switch (shdr.sh_type) { |
| 74 | 74 | elf.SHT_SYMTAB => break @as(u16, @intCast(i)), |
| ... | ... | @@ -79,10 +79,22 @@ pub fn parse(self: *Object, elf_file: *Elf) !void { |
| 79 | 79 | const shdr = shdrs[index]; |
| 80 | 80 | self.first_global = shdr.sh_info; |
| 81 | 81 | |
| 82 | const symtab = self.shdrContents(index); | |
| 83 | const nsyms = @divExact(symtab.len, @sizeOf(elf.Elf64_Sym)); | |
| 84 | self.symtab = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(symtab.ptr))[0..nsyms]; | |
| 85 | self.strtab = self.shdrContents(@as(u16, @intCast(shdr.sh_link))); | |
| 82 | const raw_symtab = self.shdrContents(index); | |
| 83 | const nsyms = @divExact(raw_symtab.len, @sizeOf(elf.Elf64_Sym)); | |
| 84 | const symtab = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(raw_symtab.ptr))[0..nsyms]; | |
| 85 | ||
| 86 | const strtab_bias = @as(u32, @intCast(self.strtab.items.len)); | |
| 87 | try self.strtab.appendSlice(gpa, self.shdrContents(@as(u16, @intCast(shdr.sh_link)))); | |
| 88 | ||
| 89 | try self.symtab.ensureUnusedCapacity(gpa, symtab.len); | |
| 90 | for (symtab) |sym| { | |
| 91 | const out_sym = self.symtab.addOneAssumeCapacity(); | |
| 92 | out_sym.* = sym; | |
| 93 | out_sym.st_name = if (sym.st_name == 0 and sym.st_type() == elf.STT_SECTION) | |
| 94 | shdrs[sym.st_shndx].sh_name | |
| 95 | else | |
| 96 | sym.st_name + strtab_bias; | |
| 97 | } | |
| 86 | 98 | } |
| 87 | 99 | |
| 88 | 100 | try self.initAtoms(elf_file); |
| ... | ... | @@ -108,16 +120,16 @@ fn initAtoms(self: *Object, elf_file: *Elf) !void { |
| 108 | 120 | |
| 109 | 121 | switch (shdr.sh_type) { |
| 110 | 122 | elf.SHT_GROUP => { |
| 111 | if (shdr.sh_info >= self.symtab.len) { | |
| 123 | if (shdr.sh_info >= self.symtab.items.len) { | |
| 112 | 124 | // TODO convert into an error |
| 113 | 125 | log.debug("{}: invalid symbol index in sh_info", .{self.fmtPath()}); |
| 114 | 126 | continue; |
| 115 | 127 | } |
| 116 | const group_info_sym = self.symtab[shdr.sh_info]; | |
| 128 | const group_info_sym = self.symtab.items[shdr.sh_info]; | |
| 117 | 129 | const group_signature = blk: { |
| 118 | 130 | if (group_info_sym.st_name == 0 and group_info_sym.st_type() == elf.STT_SECTION) { |
| 119 | 131 | const sym_shdr = shdrs[group_info_sym.st_shndx]; |
| 120 | break :blk self.strings.getAssumeExists(sym_shdr.sh_name); | |
| 132 | break :blk self.getString(sym_shdr.sh_name); | |
| 121 | 133 | } |
| 122 | 134 | break :blk self.getString(group_info_sym.st_name); |
| 123 | 135 | }; |
| ... | ... | @@ -133,11 +145,8 @@ fn initAtoms(self: *Object, elf_file: *Elf) !void { |
| 133 | 145 | continue; |
| 134 | 146 | } |
| 135 | 147 | |
| 136 | // Note the assumption about a global strtab used here to disambiguate common | |
| 137 | // COMDAT owners. | |
| 138 | 148 | const gpa = elf_file.base.allocator; |
| 139 | const group_signature_off = try elf_file.strtab.insert(gpa, group_signature); | |
| 140 | const gop = try elf_file.getOrCreateComdatGroupOwner(group_signature_off); | |
| 149 | const gop = try elf_file.getOrCreateComdatGroupOwner(group_signature); | |
| 141 | 150 | const comdat_group_index = try elf_file.addComdatGroup(); |
| 142 | 151 | const comdat_group = elf_file.comdatGroup(comdat_group_index); |
| 143 | 152 | comdat_group.* = .{ |
| ... | ... | @@ -157,10 +166,9 @@ fn initAtoms(self: *Object, elf_file: *Elf) !void { |
| 157 | 166 | => {}, |
| 158 | 167 | |
| 159 | 168 | else => { |
| 160 | const name = self.strings.getAssumeExists(shdr.sh_name); | |
| 161 | 169 | const shndx = @as(u16, @intCast(i)); |
| 162 | 170 | if (self.skipShdr(shndx, elf_file)) continue; |
| 163 | try self.addAtom(shdr, shndx, name, elf_file); | |
| 171 | try self.addAtom(shdr, shndx, elf_file); | |
| 164 | 172 | }, |
| 165 | 173 | } |
| 166 | 174 | } |
| ... | ... | @@ -177,17 +185,11 @@ fn initAtoms(self: *Object, elf_file: *Elf) !void { |
| 177 | 185 | }; |
| 178 | 186 | } |
| 179 | 187 | |
| 180 | fn addAtom( | |
| 181 | self: *Object, | |
| 182 | shdr: ElfShdr, | |
| 183 | shndx: u16, | |
| 184 | name: [:0]const u8, | |
| 185 | elf_file: *Elf, | |
| 186 | ) error{OutOfMemory}!void { | |
| 188 | fn addAtom(self: *Object, shdr: ElfShdr, shndx: u16, elf_file: *Elf) error{OutOfMemory}!void { | |
| 187 | 189 | const atom_index = try elf_file.addAtom(); |
| 188 | 190 | const atom = elf_file.atom(atom_index).?; |
| 189 | 191 | atom.atom_index = atom_index; |
| 190 | atom.name_offset = try elf_file.strtab.insert(elf_file.base.allocator, name); | |
| 192 | atom.name_offset = shdr.sh_name; | |
| 191 | 193 | atom.file_index = self.index; |
| 192 | 194 | atom.input_section_index = shndx; |
| 193 | 195 | self.atoms.items[shndx] = atom_index; |
| ... | ... | @@ -205,7 +207,7 @@ fn addAtom( |
| 205 | 207 | |
| 206 | 208 | fn initOutputSection(self: Object, elf_file: *Elf, shdr: ElfShdr) error{OutOfMemory}!u16 { |
| 207 | 209 | const name = blk: { |
| 208 | const name = self.strings.getAssumeExists(shdr.sh_name); | |
| 210 | const name = self.getString(shdr.sh_name); | |
| 209 | 211 | if (shdr.sh_flags & elf.SHF_MERGE != 0) break :blk name; |
| 210 | 212 | const sh_name_prefixes: []const [:0]const u8 = &.{ |
| 211 | 213 | ".text", ".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss", |
| ... | ... | @@ -248,7 +250,7 @@ fn initOutputSection(self: Object, elf_file: *Elf, shdr: ElfShdr) error{OutOfMem |
| 248 | 250 | |
| 249 | 251 | fn skipShdr(self: *Object, index: u16, elf_file: *Elf) bool { |
| 250 | 252 | const shdr = self.shdrs.items[index]; |
| 251 | const name = self.strings.getAssumeExists(shdr.sh_name); | |
| 253 | const name = self.getString(shdr.sh_name); | |
| 252 | 254 | const ignore = blk: { |
| 253 | 255 | if (mem.startsWith(u8, name, ".note")) break :blk true; |
| 254 | 256 | if (mem.startsWith(u8, name, ".comment")) break :blk true; |
| ... | ... | @@ -262,33 +264,24 @@ fn skipShdr(self: *Object, index: u16, elf_file: *Elf) bool { |
| 262 | 264 | |
| 263 | 265 | fn initSymtab(self: *Object, elf_file: *Elf) !void { |
| 264 | 266 | const gpa = elf_file.base.allocator; |
| 265 | const first_global = self.first_global orelse self.symtab.len; | |
| 266 | const shdrs = self.shdrs.items; | |
| 267 | const first_global = self.first_global orelse self.symtab.items.len; | |
| 267 | 268 | |
| 268 | try self.symbols.ensureTotalCapacityPrecise(gpa, self.symtab.len); | |
| 269 | try self.symbols.ensureTotalCapacityPrecise(gpa, self.symtab.items.len); | |
| 269 | 270 | |
| 270 | for (self.symtab[0..first_global], 0..) |sym, i| { | |
| 271 | for (self.symtab.items[0..first_global], 0..) |sym, i| { | |
| 271 | 272 | const index = try elf_file.addSymbol(); |
| 272 | 273 | self.symbols.appendAssumeCapacity(index); |
| 273 | 274 | const sym_ptr = elf_file.symbol(index); |
| 274 | const name = blk: { | |
| 275 | if (sym.st_name == 0 and sym.st_type() == elf.STT_SECTION) { | |
| 276 | const shdr = shdrs[sym.st_shndx]; | |
| 277 | break :blk self.strings.getAssumeExists(shdr.sh_name); | |
| 278 | } | |
| 279 | break :blk self.getString(sym.st_name); | |
| 280 | }; | |
| 281 | 275 | sym_ptr.value = sym.st_value; |
| 282 | sym_ptr.name_offset = try elf_file.strtab.insert(gpa, name); | |
| 276 | sym_ptr.name_offset = sym.st_name; | |
| 283 | 277 | sym_ptr.esym_index = @as(u32, @intCast(i)); |
| 284 | 278 | sym_ptr.atom_index = if (sym.st_shndx == elf.SHN_ABS) 0 else self.atoms.items[sym.st_shndx]; |
| 285 | 279 | sym_ptr.file_index = self.index; |
| 286 | 280 | } |
| 287 | 281 | |
| 288 | for (self.symtab[first_global..]) |sym| { | |
| 282 | for (self.symtab.items[first_global..]) |sym| { | |
| 289 | 283 | const name = self.getString(sym.st_name); |
| 290 | const off = try elf_file.strtab.insert(gpa, name); | |
| 291 | const gop = try elf_file.getOrPutGlobal(off); | |
| 284 | const gop = try elf_file.getOrPutGlobal(name); | |
| 292 | 285 | self.symbols.addOneAssumeCapacity().* = gop.index; |
| 293 | 286 | } |
| 294 | 287 | } |
| ... | ... | @@ -437,7 +430,7 @@ pub fn resolveSymbols(self: *Object, elf_file: *Elf) void { |
| 437 | 430 | const first_global = self.first_global orelse return; |
| 438 | 431 | for (self.globals(), 0..) |index, i| { |
| 439 | 432 | const esym_index = @as(Symbol.Index, @intCast(first_global + i)); |
| 440 | const esym = self.symtab[esym_index]; | |
| 433 | const esym = self.symtab.items[esym_index]; | |
| 441 | 434 | |
| 442 | 435 | if (esym.st_shndx == elf.SHN_UNDEF) continue; |
| 443 | 436 | |
| ... | ... | @@ -467,7 +460,7 @@ pub fn claimUnresolved(self: *Object, elf_file: *Elf) void { |
| 467 | 460 | const first_global = self.first_global orelse return; |
| 468 | 461 | for (self.globals(), 0..) |index, i| { |
| 469 | 462 | const esym_index = @as(u32, @intCast(first_global + i)); |
| 470 | const esym = self.symtab[esym_index]; | |
| 463 | const esym = self.symtab.items[esym_index]; | |
| 471 | 464 | if (esym.st_shndx != elf.SHN_UNDEF) continue; |
| 472 | 465 | |
| 473 | 466 | const global = elf_file.symbol(index); |
| ... | ... | @@ -491,20 +484,11 @@ pub fn claimUnresolved(self: *Object, elf_file: *Elf) void { |
| 491 | 484 | } |
| 492 | 485 | } |
| 493 | 486 | |
| 494 | pub fn resetGlobals(self: *Object, elf_file: *Elf) void { | |
| 495 | for (self.globals()) |index| { | |
| 496 | const global = elf_file.symbol(index); | |
| 497 | const off = global.name_offset; | |
| 498 | global.* = .{}; | |
| 499 | global.name_offset = off; | |
| 500 | } | |
| 501 | } | |
| 502 | ||
| 503 | 487 | pub fn markLive(self: *Object, elf_file: *Elf) void { |
| 504 | 488 | const first_global = self.first_global orelse return; |
| 505 | 489 | for (self.globals(), 0..) |index, i| { |
| 506 | 490 | const sym_idx = first_global + i; |
| 507 | const sym = self.symtab[sym_idx]; | |
| 491 | const sym = self.symtab.items[sym_idx]; | |
| 508 | 492 | if (sym.st_bind() == elf.STB_WEAK) continue; |
| 509 | 493 | |
| 510 | 494 | const global = elf_file.symbol(index); |
| ... | ... | @@ -531,7 +515,7 @@ pub fn checkDuplicates(self: *Object, elf_file: *Elf) void { |
| 531 | 515 | const first_global = self.first_global orelse return; |
| 532 | 516 | for (self.globals(), 0..) |index, i| { |
| 533 | 517 | const sym_idx = @as(u32, @intCast(first_global + i)); |
| 534 | const this_sym = self.symtab[sym_idx]; | |
| 518 | const this_sym = self.symtab.items[sym_idx]; | |
| 535 | 519 | const global = elf_file.symbol(index); |
| 536 | 520 | const global_file = global.getFile(elf_file) orelse continue; |
| 537 | 521 | |
| ... | ... | @@ -560,7 +544,7 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void { |
| 560 | 544 | const first_global = self.first_global orelse return; |
| 561 | 545 | for (self.globals(), 0..) |index, i| { |
| 562 | 546 | const sym_idx = @as(u32, @intCast(first_global + i)); |
| 563 | const this_sym = self.symtab[sym_idx]; | |
| 547 | const this_sym = self.symtab.items[sym_idx]; | |
| 564 | 548 | if (this_sym.st_shndx != elf.SHN_COMMON) continue; |
| 565 | 549 | |
| 566 | 550 | const global = elf_file.symbol(index); |
| ... | ... | @@ -584,8 +568,10 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void { |
| 584 | 568 | const name = if (is_tls) ".tls_common" else ".common"; |
| 585 | 569 | |
| 586 | 570 | const atom = elf_file.atom(atom_index).?; |
| 571 | const name_offset = @as(u32, @intCast(self.strtab.items.len)); | |
| 572 | try self.strtab.writer(gpa).print("{s}\x00", .{name}); | |
| 587 | 573 | atom.atom_index = atom_index; |
| 588 | atom.name_offset = try elf_file.strtab.insert(gpa, name); | |
| 574 | atom.name_offset = name_offset; | |
| 589 | 575 | atom.file_index = self.index; |
| 590 | 576 | atom.size = this_sym.st_size; |
| 591 | 577 | const alignment = this_sym.st_value; |
| ... | ... | @@ -597,7 +583,7 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void { |
| 597 | 583 | const shdr = try self.shdrs.addOne(gpa); |
| 598 | 584 | const sh_size = math.cast(usize, this_sym.st_size) orelse return error.Overflow; |
| 599 | 585 | shdr.* = .{ |
| 600 | .sh_name = try self.strings.insert(gpa, name), | |
| 586 | .sh_name = name_offset, | |
| 601 | 587 | .sh_type = elf.SHT_NOBITS, |
| 602 | 588 | .sh_flags = sh_flags, |
| 603 | 589 | .sh_addr = 0, |
| ... | ... | @@ -665,56 +651,6 @@ pub fn allocateAtoms(self: Object, elf_file: *Elf) void { |
| 665 | 651 | } |
| 666 | 652 | } |
| 667 | 653 | |
| 668 | pub fn updateSymtabSize(self: *Object, elf_file: *Elf) void { | |
| 669 | for (self.locals()) |local_index| { | |
| 670 | const local = elf_file.symbol(local_index); | |
| 671 | if (local.atom(elf_file)) |atom| if (!atom.flags.alive) continue; | |
| 672 | const esym = local.elfSym(elf_file); | |
| 673 | switch (esym.st_type()) { | |
| 674 | elf.STT_SECTION, elf.STT_NOTYPE => continue, | |
| 675 | else => {}, | |
| 676 | } | |
| 677 | local.flags.output_symtab = true; | |
| 678 | self.output_symtab_size.nlocals += 1; | |
| 679 | } | |
| 680 | ||
| 681 | for (self.globals()) |global_index| { | |
| 682 | const global = elf_file.symbol(global_index); | |
| 683 | if (global.file(elf_file)) |file| if (file.index() != self.index) continue; | |
| 684 | if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue; | |
| 685 | global.flags.output_symtab = true; | |
| 686 | if (global.isLocal()) { | |
| 687 | self.output_symtab_size.nlocals += 1; | |
| 688 | } else { | |
| 689 | self.output_symtab_size.nglobals += 1; | |
| 690 | } | |
| 691 | } | |
| 692 | } | |
| 693 | ||
| 694 | pub fn writeSymtab(self: *Object, elf_file: *Elf, ctx: anytype) void { | |
| 695 | var ilocal = ctx.ilocal; | |
| 696 | for (self.locals()) |local_index| { | |
| 697 | const local = elf_file.symbol(local_index); | |
| 698 | if (!local.flags.output_symtab) continue; | |
| 699 | local.setOutputSym(elf_file, &ctx.symtab[ilocal]); | |
| 700 | ilocal += 1; | |
| 701 | } | |
| 702 | ||
| 703 | var iglobal = ctx.iglobal; | |
| 704 | for (self.globals()) |global_index| { | |
| 705 | const global = elf_file.symbol(global_index); | |
| 706 | if (global.file(elf_file)) |file| if (file.index() != self.index) continue; | |
| 707 | if (!global.flags.output_symtab) continue; | |
| 708 | if (global.isLocal()) { | |
| 709 | global.setOutputSym(elf_file, &ctx.symtab[ilocal]); | |
| 710 | ilocal += 1; | |
| 711 | } else { | |
| 712 | global.setOutputSym(elf_file, &ctx.symtab[iglobal]); | |
| 713 | iglobal += 1; | |
| 714 | } | |
| 715 | } | |
| 716 | } | |
| 717 | ||
| 718 | 654 | pub fn locals(self: Object) []const Symbol.Index { |
| 719 | 655 | const end = self.first_global orelse self.symbols.items.len; |
| 720 | 656 | return self.symbols.items[0..end]; |
| ... | ... | @@ -760,11 +696,6 @@ pub fn codeDecompressAlloc(self: Object, elf_file: *Elf, atom_index: Atom.Index) |
| 760 | 696 | } else return gpa.dupe(u8, data); |
| 761 | 697 | } |
| 762 | 698 | |
| 763 | fn getString(self: *Object, off: u32) [:0]const u8 { | |
| 764 | assert(off < self.strtab.len); | |
| 765 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.ptr + off)), 0); | |
| 766 | } | |
| 767 | ||
| 768 | 699 | pub fn comdatGroupMembers(self: *Object, index: u16) []align(1) const u32 { |
| 769 | 700 | const raw = self.shdrContents(index); |
| 770 | 701 | const nmembers = @divExact(raw.len, @sizeOf(u32)); |
| ... | ... | @@ -782,6 +713,11 @@ pub fn getRelocs(self: *Object, shndx: u32) []align(1) const elf.Elf64_Rela { |
| 782 | 713 | return @as([*]align(1) const elf.Elf64_Rela, @ptrCast(raw.ptr))[0..num]; |
| 783 | 714 | } |
| 784 | 715 | |
| 716 | pub fn getString(self: Object, off: u32) [:0]const u8 { | |
| 717 | assert(off < self.strtab.items.len); | |
| 718 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0); | |
| 719 | } | |
| 720 | ||
| 785 | 721 | pub fn format( |
| 786 | 722 | self: *Object, |
| 787 | 723 | comptime unused_fmt_string: []const u8, |
| ... | ... | @@ -991,6 +927,5 @@ const Cie = eh_frame.Cie; |
| 991 | 927 | const Elf = @import("../Elf.zig"); |
| 992 | 928 | const Fde = eh_frame.Fde; |
| 993 | 929 | const File = @import("file.zig").File; |
| 994 | const StringTable = @import("../strtab.zig").StringTable; | |
| 995 | 930 | const Symbol = @import("Symbol.zig"); |
| 996 | 931 | const Alignment = Atom.Alignment; |
src/link/Elf/SharedObject.zig+50-62| ... | ... | @@ -4,19 +4,20 @@ index: File.Index, |
| 4 | 4 | |
| 5 | 5 | header: ?elf.Elf64_Ehdr = null, |
| 6 | 6 | shdrs: std.ArrayListUnmanaged(ElfShdr) = .{}, |
| 7 | symtab: []align(1) const elf.Elf64_Sym = &[0]elf.Elf64_Sym{}, | |
| 8 | strtab: []const u8 = &[0]u8{}, | |
| 7 | ||
| 8 | symtab: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{}, | |
| 9 | strtab: std.ArrayListUnmanaged(u8) = .{}, | |
| 9 | 10 | /// Version symtab contains version strings of the symbols if present. |
| 10 | 11 | versyms: std.ArrayListUnmanaged(elf.Elf64_Versym) = .{}, |
| 11 | 12 | verstrings: std.ArrayListUnmanaged(u32) = .{}, |
| 13 | symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, | |
| 14 | aliases: ?std.ArrayListUnmanaged(u32) = null, | |
| 12 | 15 | |
| 16 | dynsym_sect_index: ?u16 = null, | |
| 13 | 17 | dynamic_sect_index: ?u16 = null, |
| 14 | 18 | versym_sect_index: ?u16 = null, |
| 15 | 19 | verdef_sect_index: ?u16 = null, |
| 16 | 20 | |
| 17 | symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, | |
| 18 | aliases: ?std.ArrayListUnmanaged(u32) = null, | |
| 19 | ||
| 20 | 21 | needed: bool, |
| 21 | 22 | alive: bool, |
| 22 | 23 | |
| ... | ... | @@ -36,6 +37,8 @@ pub fn isSharedObject(path: []const u8) !bool { |
| 36 | 37 | pub fn deinit(self: *SharedObject, allocator: Allocator) void { |
| 37 | 38 | allocator.free(self.path); |
| 38 | 39 | allocator.free(self.data); |
| 40 | self.symtab.deinit(allocator); | |
| 41 | self.strtab.deinit(allocator); | |
| 39 | 42 | self.versyms.deinit(allocator); |
| 40 | 43 | self.verstrings.deinit(allocator); |
| 41 | 44 | self.symbols.deinit(allocator); |
| ... | ... | @@ -51,7 +54,6 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void { |
| 51 | 54 | self.header = try reader.readStruct(elf.Elf64_Ehdr); |
| 52 | 55 | const shoff = std.math.cast(usize, self.header.?.e_shoff) orelse return error.Overflow; |
| 53 | 56 | |
| 54 | var dynsym_index: ?u16 = null; | |
| 55 | 57 | const shdrs = @as( |
| 56 | 58 | [*]align(1) const elf.Elf64_Shdr, |
| 57 | 59 | @ptrCast(self.data.ptr + shoff), |
| ... | ... | @@ -61,7 +63,7 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void { |
| 61 | 63 | for (shdrs, 0..) |shdr, i| { |
| 62 | 64 | self.shdrs.appendAssumeCapacity(try ElfShdr.fromElf64Shdr(shdr)); |
| 63 | 65 | switch (shdr.sh_type) { |
| 64 | elf.SHT_DYNSYM => dynsym_index = @as(u16, @intCast(i)), | |
| 66 | elf.SHT_DYNSYM => self.dynsym_sect_index = @as(u16, @intCast(i)), | |
| 65 | 67 | elf.SHT_DYNAMIC => self.dynamic_sect_index = @as(u16, @intCast(i)), |
| 66 | 68 | elf.SHT_GNU_VERSYM => self.versym_sect_index = @as(u16, @intCast(i)), |
| 67 | 69 | elf.SHT_GNU_VERDEF => self.verdef_sect_index = @as(u16, @intCast(i)), |
| ... | ... | @@ -69,20 +71,13 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void { |
| 69 | 71 | } |
| 70 | 72 | } |
| 71 | 73 | |
| 72 | if (dynsym_index) |index| { | |
| 73 | const shdr = self.shdrs.items[index]; | |
| 74 | const symtab = self.shdrContents(index); | |
| 75 | const nsyms = @divExact(symtab.len, @sizeOf(elf.Elf64_Sym)); | |
| 76 | self.symtab = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(symtab.ptr))[0..nsyms]; | |
| 77 | self.strtab = self.shdrContents(@as(u16, @intCast(shdr.sh_link))); | |
| 78 | } | |
| 79 | ||
| 80 | 74 | try self.parseVersions(elf_file); |
| 81 | 75 | try self.initSymtab(elf_file); |
| 82 | 76 | } |
| 83 | 77 | |
| 84 | 78 | fn parseVersions(self: *SharedObject, elf_file: *Elf) !void { |
| 85 | 79 | const gpa = elf_file.base.allocator; |
| 80 | const symtab = self.getSymtabRaw(); | |
| 86 | 81 | |
| 87 | 82 | try self.verstrings.resize(gpa, 2); |
| 88 | 83 | self.verstrings.items[elf.VER_NDX_LOCAL] = 0; |
| ... | ... | @@ -107,7 +102,7 @@ fn parseVersions(self: *SharedObject, elf_file: *Elf) !void { |
| 107 | 102 | } |
| 108 | 103 | } |
| 109 | 104 | |
| 110 | try self.versyms.ensureTotalCapacityPrecise(gpa, self.symtab.len); | |
| 105 | try self.versyms.ensureTotalCapacityPrecise(gpa, symtab.len); | |
| 111 | 106 | |
| 112 | 107 | if (self.versym_sect_index) |shndx| { |
| 113 | 108 | const versyms_raw = self.shdrContents(shndx); |
| ... | ... | @@ -120,30 +115,39 @@ fn parseVersions(self: *SharedObject, elf_file: *Elf) !void { |
| 120 | 115 | ver; |
| 121 | 116 | self.versyms.appendAssumeCapacity(normalized_ver); |
| 122 | 117 | } |
| 123 | } else for (0..self.symtab.len) |_| { | |
| 118 | } else for (0..symtab.len) |_| { | |
| 124 | 119 | self.versyms.appendAssumeCapacity(elf.VER_NDX_GLOBAL); |
| 125 | 120 | } |
| 126 | 121 | } |
| 127 | 122 | |
| 128 | 123 | fn initSymtab(self: *SharedObject, elf_file: *Elf) !void { |
| 129 | 124 | const gpa = elf_file.base.allocator; |
| 125 | const symtab = self.getSymtabRaw(); | |
| 126 | const strtab = self.getStrtabRaw(); | |
| 130 | 127 | |
| 131 | try self.symbols.ensureTotalCapacityPrecise(gpa, self.symtab.len); | |
| 128 | try self.strtab.appendSlice(gpa, strtab); | |
| 129 | try self.symtab.ensureTotalCapacityPrecise(gpa, symtab.len); | |
| 130 | try self.symbols.ensureTotalCapacityPrecise(gpa, symtab.len); | |
| 132 | 131 | |
| 133 | for (self.symtab, 0..) |sym, i| { | |
| 132 | for (symtab, 0..) |sym, i| { | |
| 134 | 133 | const hidden = self.versyms.items[i] & elf.VERSYM_HIDDEN != 0; |
| 135 | 134 | const name = self.getString(sym.st_name); |
| 136 | 135 | // We need to garble up the name so that we don't pick this symbol |
| 137 | 136 | // during symbol resolution. Thank you GNU! |
| 138 | const off = if (hidden) blk: { | |
| 139 | const full_name = try std.fmt.allocPrint(gpa, "{s}@{s}", .{ | |
| 137 | const name_off = if (hidden) blk: { | |
| 138 | const mangled = try std.fmt.allocPrint(gpa, "{s}@{s}", .{ | |
| 140 | 139 | name, |
| 141 | 140 | self.versionString(self.versyms.items[i]), |
| 142 | 141 | }); |
| 143 | defer gpa.free(full_name); | |
| 144 | break :blk try elf_file.strtab.insert(gpa, full_name); | |
| 145 | } else try elf_file.strtab.insert(gpa, name); | |
| 146 | const gop = try elf_file.getOrPutGlobal(off); | |
| 142 | defer gpa.free(mangled); | |
| 143 | const name_off = @as(u32, @intCast(self.strtab.items.len)); | |
| 144 | try self.strtab.writer(gpa).print("{s}\x00", .{mangled}); | |
| 145 | break :blk name_off; | |
| 146 | } else sym.st_name; | |
| 147 | const out_sym = self.symtab.addOneAssumeCapacity(); | |
| 148 | out_sym.* = sym; | |
| 149 | out_sym.st_name = name_off; | |
| 150 | const gop = try elf_file.getOrPutGlobal(self.getString(name_off)); | |
| 147 | 151 | self.symbols.addOneAssumeCapacity().* = gop.index; |
| 148 | 152 | } |
| 149 | 153 | } |
| ... | ... | @@ -151,7 +155,7 @@ fn initSymtab(self: *SharedObject, elf_file: *Elf) !void { |
| 151 | 155 | pub fn resolveSymbols(self: *SharedObject, elf_file: *Elf) void { |
| 152 | 156 | for (self.globals(), 0..) |index, i| { |
| 153 | 157 | const esym_index = @as(u32, @intCast(i)); |
| 154 | const this_sym = self.symtab[esym_index]; | |
| 158 | const this_sym = self.symtab.items[esym_index]; | |
| 155 | 159 | |
| 156 | 160 | if (this_sym.st_shndx == elf.SHN_UNDEF) continue; |
| 157 | 161 | |
| ... | ... | @@ -166,18 +170,9 @@ pub fn resolveSymbols(self: *SharedObject, elf_file: *Elf) void { |
| 166 | 170 | } |
| 167 | 171 | } |
| 168 | 172 | |
| 169 | pub fn resetGlobals(self: *SharedObject, elf_file: *Elf) void { | |
| 170 | for (self.globals()) |index| { | |
| 171 | const global = elf_file.symbol(index); | |
| 172 | const off = global.name_offset; | |
| 173 | global.* = .{}; | |
| 174 | global.name_offset = off; | |
| 175 | } | |
| 176 | } | |
| 177 | ||
| 178 | 173 | pub fn markLive(self: *SharedObject, elf_file: *Elf) void { |
| 179 | 174 | for (self.globals(), 0..) |index, i| { |
| 180 | const sym = self.symtab[i]; | |
| 175 | const sym = self.symtab.items[i]; | |
| 181 | 176 | if (sym.st_shndx != elf.SHN_UNDEF) continue; |
| 182 | 177 | |
| 183 | 178 | const global = elf_file.symbol(index); |
| ... | ... | @@ -193,27 +188,6 @@ pub fn markLive(self: *SharedObject, elf_file: *Elf) void { |
| 193 | 188 | } |
| 194 | 189 | } |
| 195 | 190 | |
| 196 | pub fn updateSymtabSize(self: *SharedObject, elf_file: *Elf) void { | |
| 197 | for (self.globals()) |global_index| { | |
| 198 | const global = elf_file.symbol(global_index); | |
| 199 | if (global.file(elf_file)) |file| if (file.index() != self.index) continue; | |
| 200 | if (global.isLocal()) continue; | |
| 201 | global.flags.output_symtab = true; | |
| 202 | self.output_symtab_size.nglobals += 1; | |
| 203 | } | |
| 204 | } | |
| 205 | ||
| 206 | pub fn writeSymtab(self: *SharedObject, elf_file: *Elf, ctx: anytype) void { | |
| 207 | var iglobal = ctx.iglobal; | |
| 208 | for (self.globals()) |global_index| { | |
| 209 | const global = elf_file.symbol(global_index); | |
| 210 | if (global.file(elf_file)) |file| if (file.index() != self.index) continue; | |
| 211 | if (!global.flags.output_symtab) continue; | |
| 212 | global.setOutputSym(elf_file, &ctx.symtab[iglobal]); | |
| 213 | iglobal += 1; | |
| 214 | } | |
| 215 | } | |
| 216 | ||
| 217 | 191 | pub fn globals(self: SharedObject) []const Symbol.Index { |
| 218 | 192 | return self.symbols.items; |
| 219 | 193 | } |
| ... | ... | @@ -223,11 +197,6 @@ pub fn shdrContents(self: SharedObject, index: u16) []const u8 { |
| 223 | 197 | return self.data[shdr.sh_offset..][0..shdr.sh_size]; |
| 224 | 198 | } |
| 225 | 199 | |
| 226 | pub fn getString(self: SharedObject, off: u32) [:0]const u8 { | |
| 227 | assert(off < self.strtab.len); | |
| 228 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.ptr + off)), 0); | |
| 229 | } | |
| 230 | ||
| 231 | 200 | pub fn versionString(self: SharedObject, index: elf.Elf64_Versym) [:0]const u8 { |
| 232 | 201 | const off = self.verstrings.items[index & elf.VERSYM_VERSION]; |
| 233 | 202 | return self.getString(off); |
| ... | ... | @@ -309,6 +278,25 @@ pub fn symbolAliases(self: *SharedObject, index: u32, elf_file: *Elf) []const u3 |
| 309 | 278 | return aliases.items[start..end]; |
| 310 | 279 | } |
| 311 | 280 | |
| 281 | pub fn getString(self: SharedObject, off: u32) [:0]const u8 { | |
| 282 | assert(off < self.strtab.items.len); | |
| 283 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0); | |
| 284 | } | |
| 285 | ||
| 286 | pub fn getSymtabRaw(self: SharedObject) []align(1) const elf.Elf64_Sym { | |
| 287 | const index = self.dynsym_sect_index orelse return &[0]elf.Elf64_Sym{}; | |
| 288 | const raw_symtab = self.shdrContents(index); | |
| 289 | const nsyms = @divExact(raw_symtab.len, @sizeOf(elf.Elf64_Sym)); | |
| 290 | const symtab = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(raw_symtab.ptr))[0..nsyms]; | |
| 291 | return symtab; | |
| 292 | } | |
| 293 | ||
| 294 | pub fn getStrtabRaw(self: SharedObject) []const u8 { | |
| 295 | const index = self.dynsym_sect_index orelse return &[0]u8{}; | |
| 296 | const shdr = self.shdrs.items[index]; | |
| 297 | return self.shdrContents(@as(u16, @intCast(shdr.sh_link))); | |
| 298 | } | |
| 299 | ||
| 312 | 300 | pub fn format( |
| 313 | 301 | self: SharedObject, |
| 314 | 302 | comptime unused_fmt_string: []const u8, |
src/link/Elf/Symbol.zig+29-20| ... | ... | @@ -42,7 +42,8 @@ pub fn outputShndx(symbol: Symbol) ?u16 { |
| 42 | 42 | return symbol.output_section_index; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | pub fn isLocal(symbol: Symbol) bool { | |
| 45 | pub fn isLocal(symbol: Symbol, elf_file: *Elf) bool { | |
| 46 | if (elf_file.isRelocatable()) return symbol.elfSym(elf_file).st_bind() == elf.STB_LOCAL; | |
| 46 | 47 | return !(symbol.flags.import or symbol.flags.@"export"); |
| 47 | 48 | } |
| 48 | 49 | |
| ... | ... | @@ -58,7 +59,11 @@ pub fn @"type"(symbol: Symbol, elf_file: *Elf) u4 { |
| 58 | 59 | } |
| 59 | 60 | |
| 60 | 61 | pub fn name(symbol: Symbol, elf_file: *Elf) [:0]const u8 { |
| 61 | return elf_file.strtab.getAssumeExists(symbol.name_offset); | |
| 62 | if (symbol.flags.global) return elf_file.strings.getAssumeExists(symbol.name_offset); | |
| 63 | const file_ptr = symbol.file(elf_file).?; | |
| 64 | return switch (file_ptr) { | |
| 65 | inline else => |x| x.getString(symbol.name_offset), | |
| 66 | }; | |
| 62 | 67 | } |
| 63 | 68 | |
| 64 | 69 | pub fn atom(symbol: Symbol, elf_file: *Elf) ?*Atom { |
| ... | ... | @@ -71,11 +76,10 @@ pub fn file(symbol: Symbol, elf_file: *Elf) ?File { |
| 71 | 76 | |
| 72 | 77 | pub fn elfSym(symbol: Symbol, elf_file: *Elf) elf.Elf64_Sym { |
| 73 | 78 | const file_ptr = symbol.file(elf_file).?; |
| 74 | switch (file_ptr) { | |
| 75 | .zig_object => |x| return x.elfSym(symbol.esym_index).*, | |
| 76 | .linker_defined => |x| return x.symtab.items[symbol.esym_index], | |
| 77 | inline else => |x| return x.symtab[symbol.esym_index], | |
| 78 | } | |
| 79 | return switch (file_ptr) { | |
| 80 | .zig_object => |x| x.elfSym(symbol.esym_index).*, | |
| 81 | inline else => |x| x.symtab.items[symbol.esym_index], | |
| 82 | }; | |
| 79 | 83 | } |
| 80 | 84 | |
| 81 | 85 | pub fn symbolRank(symbol: Symbol, elf_file: *Elf) u32 { |
| ... | ... | @@ -164,6 +168,8 @@ const GetOrCreateZigGotEntryResult = struct { |
| 164 | 168 | }; |
| 165 | 169 | |
| 166 | 170 | pub fn getOrCreateZigGotEntry(symbol: *Symbol, symbol_index: Index, elf_file: *Elf) !GetOrCreateZigGotEntryResult { |
| 171 | assert(!elf_file.isRelocatable()); | |
| 172 | assert(symbol.flags.needs_zig_got); | |
| 167 | 173 | if (symbol.flags.has_zig_got) return .{ .found_existing = true, .index = symbol.extra(elf_file).?.zig_got }; |
| 168 | 174 | const index = try elf_file.zig_got.addSymbol(symbol_index, elf_file); |
| 169 | 175 | return .{ .found_existing = false, .index = index }; |
| ... | ... | @@ -201,14 +207,11 @@ pub fn setExtra(symbol: Symbol, extras: Extra, elf_file: *Elf) void { |
| 201 | 207 | } |
| 202 | 208 | |
| 203 | 209 | pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void { |
| 204 | const file_ptr = symbol.file(elf_file) orelse { | |
| 205 | out.* = Elf.null_sym; | |
| 206 | return; | |
| 207 | }; | |
| 210 | const file_ptr = symbol.file(elf_file).?; | |
| 208 | 211 | const esym = symbol.elfSym(elf_file); |
| 209 | 212 | const st_type = symbol.type(elf_file); |
| 210 | 213 | const st_bind: u8 = blk: { |
| 211 | if (symbol.isLocal()) break :blk 0; | |
| 214 | if (symbol.isLocal(elf_file)) break :blk 0; | |
| 212 | 215 | if (symbol.flags.weak) break :blk elf.STB_WEAK; |
| 213 | 216 | if (file_ptr == .shared_object) break :blk elf.STB_GLOBAL; |
| 214 | 217 | break :blk esym.st_bind(); |
| ... | ... | @@ -216,6 +219,8 @@ pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void { |
| 216 | 219 | const st_shndx = blk: { |
| 217 | 220 | if (symbol.flags.has_copy_rel) break :blk elf_file.copy_rel_section_index.?; |
| 218 | 221 | if (file_ptr == .shared_object or esym.st_shndx == elf.SHN_UNDEF) break :blk elf.SHN_UNDEF; |
| 222 | // TODO I think this is wrong and obsolete | |
| 223 | if (elf_file.isRelocatable() and st_type == elf.STT_SECTION) break :blk symbol.outputShndx().?; | |
| 219 | 224 | if (symbol.atom(elf_file) == null and file_ptr != .linker_defined) |
| 220 | 225 | break :blk elf.SHN_ABS; |
| 221 | 226 | break :blk symbol.outputShndx() orelse elf.SHN_UNDEF; |
| ... | ... | @@ -232,14 +237,11 @@ pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void { |
| 232 | 237 | break :blk symbol.value - elf_file.tlsAddress(); |
| 233 | 238 | break :blk symbol.value; |
| 234 | 239 | }; |
| 235 | out.* = .{ | |
| 236 | .st_name = symbol.name_offset, | |
| 237 | .st_info = (st_bind << 4) | st_type, | |
| 238 | .st_other = esym.st_other, | |
| 239 | .st_shndx = st_shndx, | |
| 240 | .st_value = st_value, | |
| 241 | .st_size = esym.st_size, | |
| 242 | }; | |
| 240 | out.st_info = (st_bind << 4) | st_type; | |
| 241 | out.st_other = esym.st_other; | |
| 242 | out.st_shndx = st_shndx; | |
| 243 | out.st_value = st_value; | |
| 244 | out.st_size = esym.st_size; | |
| 243 | 245 | } |
| 244 | 246 | |
| 245 | 247 | pub fn format( |
| ... | ... | @@ -340,6 +342,12 @@ pub const Flags = packed struct { |
| 340 | 342 | /// Whether this symbol is weak. |
| 341 | 343 | weak: bool = false, |
| 342 | 344 | |
| 345 | /// Whether the symbol has its name interned in global symbol | |
| 346 | /// resolver table. | |
| 347 | /// This happens for any symbol that is considered a global | |
| 348 | /// symbol, but is not necessarily an import or export. | |
| 349 | global: bool = false, | |
| 350 | ||
| 343 | 351 | /// Whether the symbol makes into the output symtab. |
| 344 | 352 | output_symtab: bool = false, |
| 345 | 353 | |
| ... | ... | @@ -373,6 +381,7 @@ pub const Flags = packed struct { |
| 373 | 381 | has_tlsdesc: bool = false, |
| 374 | 382 | |
| 375 | 383 | /// Whether the symbol contains .zig.got indirection. |
| 384 | needs_zig_got: bool = false, | |
| 376 | 385 | has_zig_got: bool = false, |
| 377 | 386 | }; |
| 378 | 387 |
src/link/Elf/ZigObject.zig+305-105| ... | ... | @@ -9,6 +9,7 @@ index: File.Index, |
| 9 | 9 | |
| 10 | 10 | local_esyms: std.MultiArrayList(ElfSym) = .{}, |
| 11 | 11 | global_esyms: std.MultiArrayList(ElfSym) = .{}, |
| 12 | strtab: StringTable = .{}, | |
| 12 | 13 | local_symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 13 | 14 | global_symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 14 | 15 | globals_lookup: std.AutoHashMapUnmanaged(u32, Symbol.Index) = .{}, |
| ... | ... | @@ -19,6 +20,7 @@ relocs: std.ArrayListUnmanaged(std.ArrayListUnmanaged(elf.Elf64_Rela)) = .{}, |
| 19 | 20 | num_dynrelocs: u32 = 0, |
| 20 | 21 | |
| 21 | 22 | output_symtab_size: Elf.SymtabSize = .{}, |
| 23 | output_ar_state: Archive.ArState = .{}, | |
| 22 | 24 | |
| 23 | 25 | dwarf: ?Dwarf = null, |
| 24 | 26 | |
| ... | ... | @@ -74,8 +76,9 @@ pub fn init(self: *ZigObject, elf_file: *Elf) !void { |
| 74 | 76 | const gpa = elf_file.base.allocator; |
| 75 | 77 | |
| 76 | 78 | try self.atoms.append(gpa, 0); // null input section |
| 79 | try self.strtab.buffer.append(gpa, 0); | |
| 77 | 80 | |
| 78 | const name_off = try elf_file.strtab.insert(gpa, std.fs.path.stem(self.path)); | |
| 81 | const name_off = try self.strtab.insert(gpa, std.fs.path.stem(self.path)); | |
| 79 | 82 | const symbol_index = try elf_file.addSymbol(); |
| 80 | 83 | try self.local_symbols.append(gpa, symbol_index); |
| 81 | 84 | const symbol_ptr = elf_file.symbol(symbol_index); |
| ... | ... | @@ -85,7 +88,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf) !void { |
| 85 | 88 | const esym_index = try self.addLocalEsym(gpa); |
| 86 | 89 | const esym = &self.local_esyms.items(.elf_sym)[esym_index]; |
| 87 | 90 | esym.st_name = name_off; |
| 88 | esym.st_info |= elf.STT_FILE; | |
| 91 | esym.st_info = elf.STT_FILE; | |
| 89 | 92 | esym.st_shndx = elf.SHN_ABS; |
| 90 | 93 | symbol_ptr.esym_index = esym_index; |
| 91 | 94 | |
| ... | ... | @@ -97,6 +100,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf) !void { |
| 97 | 100 | pub fn deinit(self: *ZigObject, allocator: Allocator) void { |
| 98 | 101 | self.local_esyms.deinit(allocator); |
| 99 | 102 | self.global_esyms.deinit(allocator); |
| 103 | self.strtab.deinit(allocator); | |
| 100 | 104 | self.local_symbols.deinit(allocator); |
| 101 | 105 | self.global_symbols.deinit(allocator); |
| 102 | 106 | self.globals_lookup.deinit(allocator); |
| ... | ... | @@ -177,16 +181,16 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf) !void { |
| 177 | 181 | } |
| 178 | 182 | |
| 179 | 183 | if (self.debug_info_header_dirty) { |
| 180 | const text_phdr = &elf_file.phdrs.items[elf_file.phdr_zig_load_re_index.?]; | |
| 181 | const low_pc = text_phdr.p_vaddr; | |
| 182 | const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz; | |
| 184 | const text_shdr = elf_file.shdrs.items[elf_file.zig_text_section_index.?]; | |
| 185 | const low_pc = text_shdr.sh_addr; | |
| 186 | const high_pc = text_shdr.sh_addr + text_shdr.sh_size; | |
| 183 | 187 | try dw.writeDbgInfoHeader(elf_file.base.options.module.?, low_pc, high_pc); |
| 184 | 188 | self.debug_info_header_dirty = false; |
| 185 | 189 | } |
| 186 | 190 | |
| 187 | 191 | if (self.debug_aranges_section_dirty) { |
| 188 | const text_phdr = &elf_file.phdrs.items[elf_file.phdr_zig_load_re_index.?]; | |
| 189 | try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz); | |
| 192 | const text_shdr = elf_file.shdrs.items[elf_file.zig_text_section_index.?]; | |
| 193 | try dw.writeDbgAranges(text_shdr.sh_addr, text_shdr.sh_size); | |
| 190 | 194 | self.debug_aranges_section_dirty = false; |
| 191 | 195 | } |
| 192 | 196 | |
| ... | ... | @@ -207,6 +211,8 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf) !void { |
| 207 | 211 | self.saveDebugSectionsSizes(elf_file); |
| 208 | 212 | } |
| 209 | 213 | |
| 214 | try self.sortSymbols(elf_file); | |
| 215 | ||
| 210 | 216 | // The point of flushModule() is to commit changes, so in theory, nothing should |
| 211 | 217 | // be dirty after this. However, it is possible for some things to remain |
| 212 | 218 | // dirty because they fail to be written in the event of compile errors, |
| ... | ... | @@ -281,6 +287,22 @@ pub fn addAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index { |
| 281 | 287 | return symbol_index; |
| 282 | 288 | } |
| 283 | 289 | |
| 290 | pub fn addSectionSymbol(self: *ZigObject, shndx: u16, elf_file: *Elf) !void { | |
| 291 | assert(elf_file.isRelocatable()); | |
| 292 | const gpa = elf_file.base.allocator; | |
| 293 | const symbol_index = try elf_file.addSymbol(); | |
| 294 | try self.local_symbols.append(gpa, symbol_index); | |
| 295 | const symbol_ptr = elf_file.symbol(symbol_index); | |
| 296 | symbol_ptr.file_index = self.index; | |
| 297 | symbol_ptr.output_section_index = shndx; | |
| 298 | ||
| 299 | const esym_index = try self.addLocalEsym(gpa); | |
| 300 | const esym = &self.local_esyms.items(.elf_sym)[esym_index]; | |
| 301 | esym.st_info = elf.STT_SECTION; | |
| 302 | esym.st_shndx = shndx; | |
| 303 | symbol_ptr.esym_index = esym_index; | |
| 304 | } | |
| 305 | ||
| 284 | 306 | /// TODO actually create fake input shdrs and return that instead. |
| 285 | 307 | pub fn inputShdr(self: ZigObject, atom_index: Atom.Index, elf_file: *Elf) Object.ElfShdr { |
| 286 | 308 | _ = self; |
| ... | ... | @@ -334,7 +356,7 @@ pub fn resolveSymbols(self: *ZigObject, elf_file: *Elf) void { |
| 334 | 356 | } |
| 335 | 357 | } |
| 336 | 358 | |
| 337 | pub fn claimUnresolved(self: *ZigObject, elf_file: *Elf) void { | |
| 359 | pub fn claimUnresolved(self: ZigObject, elf_file: *Elf) void { | |
| 338 | 360 | for (self.globals(), 0..) |index, i| { |
| 339 | 361 | const esym_index = @as(Symbol.Index, @intCast(i)) | global_symbol_bit; |
| 340 | 362 | const esym = self.global_esyms.items(.elf_sym)[i]; |
| ... | ... | @@ -362,6 +384,26 @@ pub fn claimUnresolved(self: *ZigObject, elf_file: *Elf) void { |
| 362 | 384 | } |
| 363 | 385 | } |
| 364 | 386 | |
| 387 | pub fn claimUnresolvedObject(self: ZigObject, elf_file: *Elf) void { | |
| 388 | for (self.globals(), 0..) |index, i| { | |
| 389 | const esym_index = @as(Symbol.Index, @intCast(i)) | global_symbol_bit; | |
| 390 | const esym = self.global_esyms.items(.elf_sym)[i]; | |
| 391 | ||
| 392 | if (esym.st_shndx != elf.SHN_UNDEF) continue; | |
| 393 | ||
| 394 | const global = elf_file.symbol(index); | |
| 395 | if (global.file(elf_file)) |file| { | |
| 396 | if (global.elfSym(elf_file).st_shndx != elf.SHN_UNDEF or | |
| 397 | file.index() <= self.index) continue; | |
| 398 | } | |
| 399 | ||
| 400 | global.value = 0; | |
| 401 | global.atom_index = 0; | |
| 402 | global.esym_index = esym_index; | |
| 403 | global.file_index = self.index; | |
| 404 | } | |
| 405 | } | |
| 406 | ||
| 365 | 407 | pub fn scanRelocs(self: *ZigObject, elf_file: *Elf, undefs: anytype) !void { |
| 366 | 408 | for (self.atoms.items) |atom_index| { |
| 367 | 409 | const atom = elf_file.atom(atom_index) orelse continue; |
| ... | ... | @@ -379,15 +421,6 @@ pub fn scanRelocs(self: *ZigObject, elf_file: *Elf, undefs: anytype) !void { |
| 379 | 421 | } |
| 380 | 422 | } |
| 381 | 423 | |
| 382 | pub fn resetGlobals(self: *ZigObject, elf_file: *Elf) void { | |
| 383 | for (self.globals()) |index| { | |
| 384 | const global = elf_file.symbol(index); | |
| 385 | const off = global.name_offset; | |
| 386 | global.* = .{}; | |
| 387 | global.name_offset = off; | |
| 388 | } | |
| 389 | } | |
| 390 | ||
| 391 | 424 | pub fn markLive(self: *ZigObject, elf_file: *Elf) void { |
| 392 | 425 | for (self.globals(), 0..) |index, i| { |
| 393 | 426 | const esym = self.global_esyms.items(.elf_sym)[i]; |
| ... | ... | @@ -404,79 +437,241 @@ pub fn markLive(self: *ZigObject, elf_file: *Elf) void { |
| 404 | 437 | } |
| 405 | 438 | } |
| 406 | 439 | |
| 407 | pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) void { | |
| 408 | for (self.locals()) |local_index| { | |
| 409 | const local = elf_file.symbol(local_index); | |
| 410 | const esym = local.elfSym(elf_file); | |
| 411 | switch (esym.st_type()) { | |
| 412 | elf.STT_SECTION, elf.STT_NOTYPE => { | |
| 413 | local.flags.output_symtab = false; | |
| 414 | continue; | |
| 415 | }, | |
| 416 | else => {}, | |
| 417 | } | |
| 418 | local.flags.output_symtab = true; | |
| 419 | self.output_symtab_size.nlocals += 1; | |
| 420 | } | |
| 440 | fn sortSymbols(self: *ZigObject, elf_file: *Elf) error{OutOfMemory}!void { | |
| 441 | _ = self; | |
| 442 | _ = elf_file; | |
| 443 | // const Entry = struct { | |
| 444 | // index: Symbol.Index, | |
| 445 | ||
| 446 | // const Ctx = struct { | |
| 447 | // zobj: ZigObject, | |
| 448 | // efile: *Elf, | |
| 449 | // }; | |
| 450 | ||
| 451 | // pub fn lessThan(ctx: Ctx, lhs: @This(), rhs: @This()) bool { | |
| 452 | // const lhs_sym = ctx.efile.symbol(zobj.symbol(lhs.index)); | |
| 453 | // const rhs_sym = ctx.efile.symbol(zobj.symbol(rhs.index)); | |
| 454 | // if (lhs_sym.outputShndx() != null and rhs_sym.outputShndx() != null) { | |
| 455 | // if (lhs_sym.output_section_index == rhs_sym.output_section_index) { | |
| 456 | // if (lhs_sym.value == rhs_sym.value) { | |
| 457 | // return lhs_sym.name_offset < rhs_sym.name_offset; | |
| 458 | // } | |
| 459 | // return lhs_sym.value < rhs_sym.value; | |
| 460 | // } | |
| 461 | // return lhs_sym.output_section_index < rhs_sym.output_section_index; | |
| 462 | // } | |
| 463 | // if (lhs_sym.outputShndx() != null) { | |
| 464 | // if (rhs_sym.isAbs(ctx.efile)) return false; | |
| 465 | // return true; | |
| 466 | // } | |
| 467 | // return false; | |
| 468 | // } | |
| 469 | // }; | |
| 470 | ||
| 471 | // const gpa = elf_file.base.allocator; | |
| 472 | ||
| 473 | // { | |
| 474 | // const sorted = try gpa.alloc(Entry, self.local_symbols.items.len); | |
| 475 | // defer gpa.free(sorted); | |
| 476 | // for (0..self.local_symbols.items.len) |index| { | |
| 477 | // sorted[i] = .{ .index = @as(Symbol.Index, @intCast(index)) }; | |
| 478 | // } | |
| 479 | // mem.sort(Entry, sorted, .{ .zobj = self, .efile = elf_file }, Entry.lessThan); | |
| 480 | ||
| 481 | // const backlinks = try gpa.alloc(Symbol.Index, sorted.len); | |
| 482 | // defer gpa.free(backlinks); | |
| 483 | // for (sorted, 0..) |entry, i| { | |
| 484 | // backlinks[entry.index] = @as(Symbol.Index, @intCast(i)); | |
| 485 | // } | |
| 486 | ||
| 487 | // const local_symbols = try self.local_symbols.toOwnedSlice(gpa); | |
| 488 | // defer gpa.free(local_symbols); | |
| 489 | ||
| 490 | // try self.local_symbols.ensureTotalCapacityPrecise(gpa, local_symbols.len); | |
| 491 | // for (sorted) |entry| { | |
| 492 | // self.local_symbols.appendAssumeCapacity(local_symbols[entry.index]); | |
| 493 | // } | |
| 494 | ||
| 495 | // for (self.) | |
| 496 | // } | |
| 497 | ||
| 498 | // const sorted_globals = try gpa.alloc(Entry, self.global_symbols.items.len); | |
| 499 | // defer gpa.free(sorted_globals); | |
| 500 | // for (self.global_symbols.items, 0..) |index, i| { | |
| 501 | // sorted_globals[i] = .{ .index = index }; | |
| 502 | // } | |
| 503 | // mem.sort(Entry, sorted_globals, elf_file, Entry.lessThan); | |
| 504 | } | |
| 505 | ||
| 506 | pub fn updateArSymtab(self: ZigObject, ar_symtab: *Archive.ArSymtab, elf_file: *Elf) error{OutOfMemory}!void { | |
| 507 | const gpa = elf_file.base.allocator; | |
| 508 | ||
| 509 | try ar_symtab.symtab.ensureUnusedCapacity(gpa, self.globals().len); | |
| 421 | 510 | |
| 422 | 511 | for (self.globals()) |global_index| { |
| 423 | 512 | const global = elf_file.symbol(global_index); |
| 424 | if (global.file(elf_file)) |file| if (file.index() != self.index) { | |
| 425 | global.flags.output_symtab = false; | |
| 426 | continue; | |
| 427 | }; | |
| 428 | global.flags.output_symtab = true; | |
| 429 | if (global.isLocal()) { | |
| 430 | self.output_symtab_size.nlocals += 1; | |
| 431 | } else { | |
| 432 | self.output_symtab_size.nglobals += 1; | |
| 433 | } | |
| 513 | const file_ptr = global.file(elf_file).?; | |
| 514 | assert(file_ptr.index() == self.index); | |
| 515 | if (global.type(elf_file) == elf.SHN_UNDEF) continue; | |
| 516 | ||
| 517 | const off = try ar_symtab.strtab.insert(gpa, global.name(elf_file)); | |
| 518 | ar_symtab.symtab.appendAssumeCapacity(.{ .off = off, .file_index = self.index }); | |
| 434 | 519 | } |
| 435 | 520 | } |
| 436 | 521 | |
| 437 | pub fn writeSymtab(self: *ZigObject, elf_file: *Elf, ctx: anytype) void { | |
| 438 | var ilocal = ctx.ilocal; | |
| 439 | for (self.locals()) |local_index| { | |
| 440 | const local = elf_file.symbol(local_index); | |
| 441 | if (!local.flags.output_symtab) continue; | |
| 442 | local.setOutputSym(elf_file, &ctx.symtab[ilocal]); | |
| 443 | ilocal += 1; | |
| 522 | pub fn updateArStrtab( | |
| 523 | self: *ZigObject, | |
| 524 | allocator: Allocator, | |
| 525 | ar_strtab: *Archive.ArStrtab, | |
| 526 | ) error{OutOfMemory}!void { | |
| 527 | const name = try std.fmt.allocPrint(allocator, "{s}.o", .{std.fs.path.stem(self.path)}); | |
| 528 | defer allocator.free(name); | |
| 529 | if (name.len <= 15) return; | |
| 530 | const name_off = try ar_strtab.insert(allocator, name); | |
| 531 | self.output_ar_state.name_off = name_off; | |
| 532 | } | |
| 533 | ||
| 534 | pub fn updateArSize(self: *ZigObject, elf_file: *Elf) void { | |
| 535 | var end_pos: u64 = elf_file.shdr_table_offset.?; | |
| 536 | for (elf_file.shdrs.items) |shdr| { | |
| 537 | end_pos = @max(end_pos, shdr.sh_offset + shdr.sh_size); | |
| 444 | 538 | } |
| 539 | self.output_ar_state.size = end_pos; | |
| 540 | } | |
| 445 | 541 | |
| 446 | var iglobal = ctx.iglobal; | |
| 447 | for (self.globals()) |global_index| { | |
| 448 | const global = elf_file.symbol(global_index); | |
| 449 | if (global.file(elf_file)) |file| if (file.index() != self.index) continue; | |
| 450 | if (!global.flags.output_symtab) continue; | |
| 451 | if (global.isLocal()) { | |
| 452 | global.setOutputSym(elf_file, &ctx.symtab[ilocal]); | |
| 453 | ilocal += 1; | |
| 454 | } else { | |
| 455 | global.setOutputSym(elf_file, &ctx.symtab[iglobal]); | |
| 456 | iglobal += 1; | |
| 542 | pub fn writeAr(self: ZigObject, elf_file: *Elf, writer: anytype) !void { | |
| 543 | const gpa = elf_file.base.allocator; | |
| 544 | ||
| 545 | const size = std.math.cast(usize, self.output_ar_state.size) orelse return error.Overflow; | |
| 546 | const contents = try gpa.alloc(u8, size); | |
| 547 | defer gpa.free(contents); | |
| 548 | ||
| 549 | const amt = try elf_file.base.file.?.preadAll(contents, 0); | |
| 550 | if (amt != self.output_ar_state.size) return error.InputOutput; | |
| 551 | ||
| 552 | const name = try std.fmt.allocPrint(gpa, "{s}.o", .{std.fs.path.stem(self.path)}); | |
| 553 | defer gpa.free(name); | |
| 554 | ||
| 555 | const hdr = Archive.setArHdr(.{ | |
| 556 | .name = if (name.len <= 15) .{ .name = name } else .{ .name_off = self.output_ar_state.name_off }, | |
| 557 | .size = @intCast(size), | |
| 558 | }); | |
| 559 | try writer.writeAll(mem.asBytes(&hdr)); | |
| 560 | try writer.writeAll(contents); | |
| 561 | } | |
| 562 | ||
| 563 | pub fn updateRelaSectionSizes(self: ZigObject, elf_file: *Elf) void { | |
| 564 | _ = self; | |
| 565 | ||
| 566 | for (&[_]?u16{ | |
| 567 | elf_file.zig_text_rela_section_index, | |
| 568 | elf_file.zig_data_rel_ro_rela_section_index, | |
| 569 | elf_file.zig_data_rela_section_index, | |
| 570 | }) |maybe_index| { | |
| 571 | const index = maybe_index orelse continue; | |
| 572 | const shdr = &elf_file.shdrs.items[index]; | |
| 573 | const meta = elf_file.last_atom_and_free_list_table.get(@intCast(shdr.sh_info)).?; | |
| 574 | const last_atom_index = meta.last_atom_index; | |
| 575 | ||
| 576 | var atom = elf_file.atom(last_atom_index) orelse continue; | |
| 577 | while (true) { | |
| 578 | const relocs = atom.relocs(elf_file); | |
| 579 | shdr.sh_size += relocs.len * shdr.sh_entsize; | |
| 580 | if (elf_file.atom(atom.prev_index)) |prev| { | |
| 581 | atom = prev; | |
| 582 | } else break; | |
| 583 | } | |
| 584 | } | |
| 585 | ||
| 586 | for (&[_]?u16{ | |
| 587 | elf_file.zig_text_rela_section_index, | |
| 588 | elf_file.zig_data_rel_ro_rela_section_index, | |
| 589 | elf_file.zig_data_rela_section_index, | |
| 590 | }) |maybe_index| { | |
| 591 | const index = maybe_index orelse continue; | |
| 592 | const shdr = &elf_file.shdrs.items[index]; | |
| 593 | if (shdr.sh_size == 0) shdr.sh_offset = 0; | |
| 594 | } | |
| 595 | } | |
| 596 | ||
| 597 | pub fn writeRelaSections(self: ZigObject, elf_file: *Elf) !void { | |
| 598 | const gpa = elf_file.base.allocator; | |
| 599 | ||
| 600 | for (&[_]?u16{ | |
| 601 | elf_file.zig_text_rela_section_index, | |
| 602 | elf_file.zig_data_rel_ro_rela_section_index, | |
| 603 | elf_file.zig_data_rela_section_index, | |
| 604 | }) |maybe_index| { | |
| 605 | const index = maybe_index orelse continue; | |
| 606 | const shdr = elf_file.shdrs.items[index]; | |
| 607 | const meta = elf_file.last_atom_and_free_list_table.get(@intCast(shdr.sh_info)).?; | |
| 608 | const last_atom_index = meta.last_atom_index; | |
| 609 | ||
| 610 | var atom = elf_file.atom(last_atom_index) orelse continue; | |
| 611 | ||
| 612 | var relocs = std.ArrayList(elf.Elf64_Rela).init(gpa); | |
| 613 | defer relocs.deinit(); | |
| 614 | try relocs.ensureTotalCapacityPrecise(@intCast(@divExact(shdr.sh_size, shdr.sh_entsize))); | |
| 615 | ||
| 616 | while (true) { | |
| 617 | for (atom.relocs(elf_file)) |rel| { | |
| 618 | const target = elf_file.symbol(self.symbol(rel.r_sym())); | |
| 619 | const r_offset = atom.value + rel.r_offset; | |
| 620 | const r_sym: u32 = if (target.flags.global) | |
| 621 | (target.esym_index & symbol_mask) + @as(u32, @intCast(self.local_esyms.slice().len)) | |
| 622 | else | |
| 623 | target.esym_index; | |
| 624 | const r_type = switch (rel.r_type()) { | |
| 625 | Elf.R_X86_64_ZIG_GOT32, | |
| 626 | Elf.R_X86_64_ZIG_GOTPCREL, | |
| 627 | => unreachable, // Sanity check if we accidentally emitted those. | |
| 628 | else => |r_type| r_type, | |
| 629 | }; | |
| 630 | relocs.appendAssumeCapacity(.{ | |
| 631 | .r_offset = r_offset, | |
| 632 | .r_addend = rel.r_addend, | |
| 633 | .r_info = (@as(u64, @intCast(r_sym + 1)) << 32) | r_type, | |
| 634 | }); | |
| 635 | } | |
| 636 | if (elf_file.atom(atom.prev_index)) |prev| { | |
| 637 | atom = prev; | |
| 638 | } else break; | |
| 457 | 639 | } |
| 640 | ||
| 641 | const SortRelocs = struct { | |
| 642 | pub fn lessThan(ctx: void, lhs: elf.Elf64_Rela, rhs: elf.Elf64_Rela) bool { | |
| 643 | _ = ctx; | |
| 644 | return lhs.r_offset < rhs.r_offset; | |
| 645 | } | |
| 646 | }; | |
| 647 | ||
| 648 | mem.sort(elf.Elf64_Rela, relocs.items, {}, SortRelocs.lessThan); | |
| 649 | ||
| 650 | try elf_file.base.file.?.pwriteAll(mem.sliceAsBytes(relocs.items), shdr.sh_offset); | |
| 458 | 651 | } |
| 459 | 652 | } |
| 460 | 653 | |
| 461 | pub fn symbol(self: *ZigObject, index: Symbol.Index) Symbol.Index { | |
| 462 | const is_global = index & global_symbol_bit != 0; | |
| 654 | inline fn isGlobal(index: Symbol.Index) bool { | |
| 655 | return index & global_symbol_bit != 0; | |
| 656 | } | |
| 657 | ||
| 658 | pub fn symbol(self: ZigObject, index: Symbol.Index) Symbol.Index { | |
| 463 | 659 | const actual_index = index & symbol_mask; |
| 464 | if (is_global) return self.global_symbols.items[actual_index]; | |
| 660 | if (isGlobal(index)) return self.global_symbols.items[actual_index]; | |
| 465 | 661 | return self.local_symbols.items[actual_index]; |
| 466 | 662 | } |
| 467 | 663 | |
| 468 | 664 | pub fn elfSym(self: *ZigObject, index: Symbol.Index) *elf.Elf64_Sym { |
| 469 | const is_global = index & global_symbol_bit != 0; | |
| 470 | 665 | const actual_index = index & symbol_mask; |
| 471 | if (is_global) return &self.global_esyms.items(.elf_sym)[actual_index]; | |
| 666 | if (isGlobal(index)) return &self.global_esyms.items(.elf_sym)[actual_index]; | |
| 472 | 667 | return &self.local_esyms.items(.elf_sym)[actual_index]; |
| 473 | 668 | } |
| 474 | 669 | |
| 475 | pub fn locals(self: *ZigObject) []const Symbol.Index { | |
| 670 | pub fn locals(self: ZigObject) []const Symbol.Index { | |
| 476 | 671 | return self.local_symbols.items; |
| 477 | 672 | } |
| 478 | 673 | |
| 479 | pub fn globals(self: *ZigObject) []const Symbol.Index { | |
| 674 | pub fn globals(self: ZigObject) []const Symbol.Index { | |
| 480 | 675 | return self.global_symbols.items; |
| 481 | 676 | } |
| 482 | 677 | |
| ... | ... | @@ -570,7 +765,7 @@ pub fn lowerAnonDecl( |
| 570 | 765 | name, |
| 571 | 766 | tv, |
| 572 | 767 | decl_alignment, |
| 573 | elf_file.zig_rodata_section_index.?, | |
| 768 | elf_file.zig_data_rel_ro_section_index.?, | |
| 574 | 769 | src_loc, |
| 575 | 770 | ) catch |err| switch (err) { |
| 576 | 771 | error.OutOfMemory => return error.OutOfMemory, |
| ... | ... | @@ -682,7 +877,7 @@ fn getDeclShdrIndex(self: *ZigObject, elf_file: *Elf, decl_index: Module.Decl.In |
| 682 | 877 | .Fn => elf_file.zig_text_section_index.?, |
| 683 | 878 | else => blk: { |
| 684 | 879 | if (decl.getOwnedVariable(mod)) |variable| { |
| 685 | if (variable.is_const) break :blk elf_file.zig_rodata_section_index.?; | |
| 880 | if (variable.is_const) break :blk elf_file.zig_data_rel_ro_section_index.?; | |
| 686 | 881 | if (variable.init.toValue().isUndefDeep(mod)) { |
| 687 | 882 | const mode = elf_file.base.options.optimize_mode; |
| 688 | 883 | if (mode == .Debug or mode == .ReleaseSafe) break :blk elf_file.zig_data_section_index.?; |
| ... | ... | @@ -696,7 +891,7 @@ fn getDeclShdrIndex(self: *ZigObject, elf_file: *Elf, decl_index: Module.Decl.In |
| 696 | 891 | if (is_all_zeroes) break :blk elf_file.zig_bss_section_index.?; |
| 697 | 892 | break :blk elf_file.zig_data_section_index.?; |
| 698 | 893 | } |
| 699 | break :blk elf_file.zig_rodata_section_index.?; | |
| 894 | break :blk elf_file.zig_data_rel_ro_section_index.?; | |
| 700 | 895 | }, |
| 701 | 896 | }; |
| 702 | 897 | return shdr_index; |
| ... | ... | @@ -727,7 +922,7 @@ fn updateDeclCode( |
| 727 | 922 | sym.output_section_index = shdr_index; |
| 728 | 923 | atom_ptr.output_section_index = shdr_index; |
| 729 | 924 | |
| 730 | sym.name_offset = try elf_file.strtab.insert(gpa, decl_name); | |
| 925 | sym.name_offset = try self.strtab.insert(gpa, decl_name); | |
| 731 | 926 | atom_ptr.flags.alive = true; |
| 732 | 927 | atom_ptr.name_offset = sym.name_offset; |
| 733 | 928 | esym.st_name = sym.name_offset; |
| ... | ... | @@ -749,10 +944,12 @@ fn updateDeclCode( |
| 749 | 944 | sym.value = atom_ptr.value; |
| 750 | 945 | esym.st_value = atom_ptr.value; |
| 751 | 946 | |
| 752 | log.debug(" (writing new offset table entry)", .{}); | |
| 753 | assert(sym.flags.has_zig_got); | |
| 754 | const extra = sym.extra(elf_file).?; | |
| 755 | try elf_file.zig_got.writeOne(elf_file, extra.zig_got); | |
| 947 | if (!elf_file.isRelocatable()) { | |
| 948 | log.debug(" (writing new offset table entry)", .{}); | |
| 949 | assert(sym.flags.has_zig_got); | |
| 950 | const extra = sym.extra(elf_file).?; | |
| 951 | try elf_file.zig_got.writeOne(elf_file, extra.zig_got); | |
| 952 | } | |
| 756 | 953 | } |
| 757 | 954 | } else if (code.len < old_size) { |
| 758 | 955 | atom_ptr.shrink(elf_file); |
| ... | ... | @@ -762,10 +959,13 @@ fn updateDeclCode( |
| 762 | 959 | errdefer self.freeDeclMetadata(elf_file, sym_index); |
| 763 | 960 | |
| 764 | 961 | sym.value = atom_ptr.value; |
| 962 | sym.flags.needs_zig_got = true; | |
| 765 | 963 | esym.st_value = atom_ptr.value; |
| 766 | 964 | |
| 767 | const gop = try sym.getOrCreateZigGotEntry(sym_index, elf_file); | |
| 768 | try elf_file.zig_got.writeOne(elf_file, gop.index); | |
| 965 | if (!elf_file.isRelocatable()) { | |
| 966 | const gop = try sym.getOrCreateZigGotEntry(sym_index, elf_file); | |
| 967 | try elf_file.zig_got.writeOne(elf_file, gop.index); | |
| 968 | } | |
| 769 | 969 | } |
| 770 | 970 | |
| 771 | 971 | if (elf_file.base.child_pid) |pid| { |
| ... | ... | @@ -791,9 +991,7 @@ fn updateDeclCode( |
| 791 | 991 | |
| 792 | 992 | const shdr = elf_file.shdrs.items[shdr_index]; |
| 793 | 993 | if (shdr.sh_type != elf.SHT_NOBITS) { |
| 794 | const phdr_index = elf_file.phdr_to_shdr_table.get(shdr_index).?; | |
| 795 | const section_offset = sym.value - elf_file.phdrs.items[phdr_index].p_vaddr; | |
| 796 | const file_offset = shdr.sh_offset + section_offset; | |
| 994 | const file_offset = shdr.sh_offset + sym.value - shdr.sh_addr; | |
| 797 | 995 | try elf_file.base.file.?.pwriteAll(code, file_offset); |
| 798 | 996 | } |
| 799 | 997 | } |
| ... | ... | @@ -967,7 +1165,7 @@ fn updateLazySymbol( |
| 967 | 1165 | sym.ty.fmt(mod), |
| 968 | 1166 | }); |
| 969 | 1167 | defer gpa.free(name); |
| 970 | break :blk try elf_file.strtab.insert(gpa, name); | |
| 1168 | break :blk try self.strtab.insert(gpa, name); | |
| 971 | 1169 | }; |
| 972 | 1170 | |
| 973 | 1171 | const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl| |
| ... | ... | @@ -997,10 +1195,9 @@ fn updateLazySymbol( |
| 997 | 1195 | |
| 998 | 1196 | const output_section_index = switch (sym.kind) { |
| 999 | 1197 | .code => elf_file.zig_text_section_index.?, |
| 1000 | .const_data => elf_file.zig_rodata_section_index.?, | |
| 1198 | .const_data => elf_file.zig_data_rel_ro_section_index.?, | |
| 1001 | 1199 | }; |
| 1002 | 1200 | const local_sym = elf_file.symbol(symbol_index); |
| 1003 | const phdr_index = elf_file.phdr_to_shdr_table.get(output_section_index).?; | |
| 1004 | 1201 | local_sym.name_offset = name_str_index; |
| 1005 | 1202 | local_sym.output_section_index = output_section_index; |
| 1006 | 1203 | const local_esym = &self.local_esyms.items(.elf_sym)[local_sym.esym_index]; |
| ... | ... | @@ -1018,13 +1215,16 @@ fn updateLazySymbol( |
| 1018 | 1215 | errdefer self.freeDeclMetadata(elf_file, symbol_index); |
| 1019 | 1216 | |
| 1020 | 1217 | local_sym.value = atom_ptr.value; |
| 1218 | local_sym.flags.needs_zig_got = true; | |
| 1021 | 1219 | local_esym.st_value = atom_ptr.value; |
| 1022 | 1220 | |
| 1023 | const gop = try local_sym.getOrCreateZigGotEntry(symbol_index, elf_file); | |
| 1024 | try elf_file.zig_got.writeOne(elf_file, gop.index); | |
| 1221 | if (!elf_file.isRelocatable()) { | |
| 1222 | const gop = try local_sym.getOrCreateZigGotEntry(symbol_index, elf_file); | |
| 1223 | try elf_file.zig_got.writeOne(elf_file, gop.index); | |
| 1224 | } | |
| 1025 | 1225 | |
| 1026 | const section_offset = atom_ptr.value - elf_file.phdrs.items[phdr_index].p_vaddr; | |
| 1027 | const file_offset = elf_file.shdrs.items[output_section_index].sh_offset + section_offset; | |
| 1226 | const shdr = elf_file.shdrs.items[output_section_index]; | |
| 1227 | const file_offset = shdr.sh_offset + atom_ptr.value - shdr.sh_addr; | |
| 1028 | 1228 | try elf_file.base.file.?.pwriteAll(code, file_offset); |
| 1029 | 1229 | } |
| 1030 | 1230 | |
| ... | ... | @@ -1051,7 +1251,7 @@ pub fn lowerUnnamedConst( |
| 1051 | 1251 | name, |
| 1052 | 1252 | typed_value, |
| 1053 | 1253 | typed_value.ty.abiAlignment(mod), |
| 1054 | elf_file.zig_rodata_section_index.?, | |
| 1254 | elf_file.zig_data_rel_ro_section_index.?, | |
| 1055 | 1255 | decl.srcLoc(mod), |
| 1056 | 1256 | )) { |
| 1057 | 1257 | .ok => |sym_index| sym_index, |
| ... | ... | @@ -1098,9 +1298,8 @@ fn lowerConst( |
| 1098 | 1298 | .fail => |em| return .{ .fail = em }, |
| 1099 | 1299 | }; |
| 1100 | 1300 | |
| 1101 | const phdr_index = elf_file.phdr_to_shdr_table.get(output_section_index).?; | |
| 1102 | 1301 | const local_sym = elf_file.symbol(sym_index); |
| 1103 | const name_str_index = try elf_file.strtab.insert(gpa, name); | |
| 1302 | const name_str_index = try self.strtab.insert(gpa, name); | |
| 1104 | 1303 | local_sym.name_offset = name_str_index; |
| 1105 | 1304 | local_sym.output_section_index = output_section_index; |
| 1106 | 1305 | const local_esym = &self.local_esyms.items(.elf_sym)[local_sym.esym_index]; |
| ... | ... | @@ -1121,8 +1320,8 @@ fn lowerConst( |
| 1121 | 1320 | local_sym.value = atom_ptr.value; |
| 1122 | 1321 | local_esym.st_value = atom_ptr.value; |
| 1123 | 1322 | |
| 1124 | const section_offset = atom_ptr.value - elf_file.phdrs.items[phdr_index].p_vaddr; | |
| 1125 | const file_offset = elf_file.shdrs.items[output_section_index].sh_offset + section_offset; | |
| 1323 | const shdr = elf_file.shdrs.items[output_section_index]; | |
| 1324 | const file_offset = shdr.sh_offset + atom_ptr.value - shdr.sh_addr; | |
| 1126 | 1325 | try elf_file.base.file.?.pwriteAll(code, file_offset); |
| 1127 | 1326 | |
| 1128 | 1327 | return .{ .ok = sym_index }; |
| ... | ... | @@ -1195,18 +1394,12 @@ pub fn updateExports( |
| 1195 | 1394 | }; |
| 1196 | 1395 | const stt_bits: u8 = @as(u4, @truncate(esym.st_info)); |
| 1197 | 1396 | const exp_name = mod.intern_pool.stringToSlice(exp.opts.name); |
| 1198 | const name_off = try elf_file.strtab.insert(gpa, exp_name); | |
| 1199 | const global_esym_index = if (metadata.@"export"(self, elf_file, exp_name)) |exp_index| | |
| 1397 | const name_off = try self.strtab.insert(gpa, exp_name); | |
| 1398 | const global_esym_index = if (metadata.@"export"(self, exp_name)) |exp_index| | |
| 1200 | 1399 | exp_index.* |
| 1201 | 1400 | else blk: { |
| 1202 | const global_esym_index = try self.addGlobalEsym(gpa); | |
| 1203 | const lookup_gop = try self.globals_lookup.getOrPut(gpa, name_off); | |
| 1204 | const global_esym = self.elfSym(global_esym_index); | |
| 1205 | global_esym.st_name = name_off; | |
| 1206 | lookup_gop.value_ptr.* = global_esym_index; | |
| 1401 | const global_esym_index = try self.getGlobalSymbol(elf_file, exp_name, null); | |
| 1207 | 1402 | try metadata.exports.append(gpa, global_esym_index); |
| 1208 | const gop = try elf_file.getOrPutGlobal(name_off); | |
| 1209 | try self.global_symbols.append(gpa, gop.index); | |
| 1210 | 1403 | break :blk global_esym_index; |
| 1211 | 1404 | }; |
| 1212 | 1405 | |
| ... | ... | @@ -1216,6 +1409,7 @@ pub fn updateExports( |
| 1216 | 1409 | global_esym.st_shndx = esym.st_shndx; |
| 1217 | 1410 | global_esym.st_info = (stb_bits << 4) | stt_bits; |
| 1218 | 1411 | global_esym.st_name = name_off; |
| 1412 | global_esym.st_size = esym.st_size; | |
| 1219 | 1413 | self.global_esyms.items(.shndx)[actual_esym_index] = esym_shndx; |
| 1220 | 1414 | } |
| 1221 | 1415 | } |
| ... | ... | @@ -1248,7 +1442,7 @@ pub fn deleteDeclExport( |
| 1248 | 1442 | const metadata = self.decls.getPtr(decl_index) orelse return; |
| 1249 | 1443 | const mod = elf_file.base.options.module.?; |
| 1250 | 1444 | const exp_name = mod.intern_pool.stringToSlice(name); |
| 1251 | const esym_index = metadata.@"export"(self, elf_file, exp_name) orelse return; | |
| 1445 | const esym_index = metadata.@"export"(self, exp_name) orelse return; | |
| 1252 | 1446 | log.debug("deleting export '{s}'", .{exp_name}); |
| 1253 | 1447 | const esym = &self.global_esyms.items(.elf_sym)[esym_index.*]; |
| 1254 | 1448 | _ = self.globals_lookup.remove(esym.st_name); |
| ... | ... | @@ -1265,19 +1459,23 @@ pub fn deleteDeclExport( |
| 1265 | 1459 | pub fn getGlobalSymbol(self: *ZigObject, elf_file: *Elf, name: []const u8, lib_name: ?[]const u8) !u32 { |
| 1266 | 1460 | _ = lib_name; |
| 1267 | 1461 | const gpa = elf_file.base.allocator; |
| 1268 | const off = try elf_file.strtab.insert(gpa, name); | |
| 1462 | const off = try self.strtab.insert(gpa, name); | |
| 1269 | 1463 | const lookup_gop = try self.globals_lookup.getOrPut(gpa, off); |
| 1270 | 1464 | if (!lookup_gop.found_existing) { |
| 1271 | 1465 | const esym_index = try self.addGlobalEsym(gpa); |
| 1272 | 1466 | const esym = self.elfSym(esym_index); |
| 1273 | 1467 | esym.st_name = off; |
| 1274 | 1468 | lookup_gop.value_ptr.* = esym_index; |
| 1275 | const gop = try elf_file.getOrPutGlobal(off); | |
| 1469 | const gop = try elf_file.getOrPutGlobal(name); | |
| 1276 | 1470 | try self.global_symbols.append(gpa, gop.index); |
| 1277 | 1471 | } |
| 1278 | 1472 | return lookup_gop.value_ptr.*; |
| 1279 | 1473 | } |
| 1280 | 1474 | |
| 1475 | pub fn getString(self: ZigObject, off: u32) [:0]const u8 { | |
| 1476 | return self.strtab.getAssumeExists(off); | |
| 1477 | } | |
| 1478 | ||
| 1281 | 1479 | pub fn fmtSymtab(self: *ZigObject, elf_file: *Elf) std.fmt.Formatter(formatSymtab) { |
| 1282 | 1480 | return .{ .data = .{ |
| 1283 | 1481 | .self = self, |
| ... | ... | @@ -1350,9 +1548,9 @@ const DeclMetadata = struct { |
| 1350 | 1548 | /// A list of all exports aliases of this Decl. |
| 1351 | 1549 | exports: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 1352 | 1550 | |
| 1353 | fn @"export"(m: DeclMetadata, zig_object: *ZigObject, elf_file: *Elf, name: []const u8) ?*u32 { | |
| 1551 | fn @"export"(m: DeclMetadata, zig_object: *ZigObject, name: []const u8) ?*u32 { | |
| 1354 | 1552 | for (m.exports.items) |*exp| { |
| 1355 | const exp_name = elf_file.strtab.getAssumeExists(zig_object.elfSym(exp.*).st_name); | |
| 1553 | const exp_name = zig_object.getString(zig_object.elfSym(exp.*).st_name); | |
| 1356 | 1554 | if (mem.eql(u8, name, exp_name)) return exp; |
| 1357 | 1555 | } |
| 1358 | 1556 | return null; |
| ... | ... | @@ -1377,6 +1575,7 @@ const std = @import("std"); |
| 1377 | 1575 | |
| 1378 | 1576 | const Air = @import("../../Air.zig"); |
| 1379 | 1577 | const Allocator = std.mem.Allocator; |
| 1578 | const Archive = @import("Archive.zig"); | |
| 1380 | 1579 | const Atom = @import("Atom.zig"); |
| 1381 | 1580 | const Dwarf = @import("../Dwarf.zig"); |
| 1382 | 1581 | const Elf = @import("../Elf.zig"); |
| ... | ... | @@ -1386,5 +1585,6 @@ const Liveness = @import("../../Liveness.zig"); |
| 1386 | 1585 | const Module = @import("../../Module.zig"); |
| 1387 | 1586 | const Object = @import("Object.zig"); |
| 1388 | 1587 | const Symbol = @import("Symbol.zig"); |
| 1588 | const StringTable = @import("../StringTable.zig"); | |
| 1389 | 1589 | const TypedValue = @import("../../TypedValue.zig"); |
| 1390 | 1590 | const ZigObject = @This(); |
src/link/Elf/eh_frame.zig+1-1| ... | ... | @@ -43,7 +43,7 @@ pub const Fde = struct { |
| 43 | 43 | pub fn atom(fde: Fde, elf_file: *Elf) *Atom { |
| 44 | 44 | const object = elf_file.file(fde.file_index).?.object; |
| 45 | 45 | const rel = fde.relocs(elf_file)[0]; |
| 46 | const sym = object.symtab[rel.r_sym()]; | |
| 46 | const sym = object.symtab.items[rel.r_sym()]; | |
| 47 | 47 | const atom_index = object.atoms.items[sym.st_shndx]; |
| 48 | 48 | return elf_file.atom(atom_index).?; |
| 49 | 49 | } |
src/link/Elf/file.zig+103-8| ... | ... | @@ -68,9 +68,12 @@ pub const File = union(enum) { |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | pub fn resetGlobals(file: File, elf_file: *Elf) void { |
| 71 | switch (file) { | |
| 72 | .linker_defined => unreachable, | |
| 73 | inline else => |x| x.resetGlobals(elf_file), | |
| 71 | for (file.globals()) |global_index| { | |
| 72 | const global = elf_file.symbol(global_index); | |
| 73 | const name_offset = global.name_offset; | |
| 74 | global.* = .{}; | |
| 75 | global.name_offset = name_offset; | |
| 76 | global.flags.global = true; | |
| 74 | 77 | } |
| 75 | 78 | } |
| 76 | 79 | |
| ... | ... | @@ -83,24 +86,37 @@ pub const File = union(enum) { |
| 83 | 86 | |
| 84 | 87 | pub fn markLive(file: File, elf_file: *Elf) void { |
| 85 | 88 | switch (file) { |
| 86 | .linker_defined => unreachable, | |
| 89 | .linker_defined => {}, | |
| 87 | 90 | inline else => |x| x.markLive(elf_file), |
| 88 | 91 | } |
| 89 | 92 | } |
| 90 | 93 | |
| 91 | 94 | pub fn atoms(file: File) []const Atom.Index { |
| 92 | 95 | return switch (file) { |
| 93 | .linker_defined => unreachable, | |
| 94 | .shared_object => unreachable, | |
| 96 | .linker_defined, .shared_object => &[0]Atom.Index{}, | |
| 95 | 97 | .zig_object => |x| x.atoms.items, |
| 96 | 98 | .object => |x| x.atoms.items, |
| 97 | 99 | }; |
| 98 | 100 | } |
| 99 | 101 | |
| 102 | pub fn cies(file: File) []const Cie { | |
| 103 | return switch (file) { | |
| 104 | .zig_object => &[0]Cie{}, | |
| 105 | .object => |x| x.cies.items, | |
| 106 | inline else => unreachable, | |
| 107 | }; | |
| 108 | } | |
| 109 | ||
| 110 | pub fn symbol(file: File, ind: Symbol.Index) Symbol.Index { | |
| 111 | return switch (file) { | |
| 112 | .zig_object => |x| x.symbol(ind), | |
| 113 | inline else => |x| x.symbols.items[ind], | |
| 114 | }; | |
| 115 | } | |
| 116 | ||
| 100 | 117 | pub fn locals(file: File) []const Symbol.Index { |
| 101 | 118 | return switch (file) { |
| 102 | .linker_defined => unreachable, | |
| 103 | .shared_object => unreachable, | |
| 119 | .linker_defined, .shared_object => &[0]Symbol.Index{}, | |
| 104 | 120 | inline else => |x| x.locals(), |
| 105 | 121 | }; |
| 106 | 122 | } |
| ... | ... | @@ -111,6 +127,83 @@ pub const File = union(enum) { |
| 111 | 127 | }; |
| 112 | 128 | } |
| 113 | 129 | |
| 130 | pub fn updateSymtabSize(file: File, elf_file: *Elf) void { | |
| 131 | const output_symtab_size = switch (file) { | |
| 132 | inline else => |x| &x.output_symtab_size, | |
| 133 | }; | |
| 134 | for (file.locals()) |local_index| { | |
| 135 | const local = elf_file.symbol(local_index); | |
| 136 | if (local.atom(elf_file)) |atom| if (!atom.flags.alive) continue; | |
| 137 | const esym = local.elfSym(elf_file); | |
| 138 | switch (esym.st_type()) { | |
| 139 | elf.STT_SECTION => if (!elf_file.isRelocatable()) continue, | |
| 140 | elf.STT_NOTYPE => continue, | |
| 141 | else => {}, | |
| 142 | } | |
| 143 | local.flags.output_symtab = true; | |
| 144 | output_symtab_size.nlocals += 1; | |
| 145 | output_symtab_size.strsize += @as(u32, @intCast(local.name(elf_file).len)) + 1; | |
| 146 | } | |
| 147 | ||
| 148 | for (file.globals()) |global_index| { | |
| 149 | const global = elf_file.symbol(global_index); | |
| 150 | const file_ptr = global.file(elf_file) orelse continue; | |
| 151 | if (file_ptr.index() != file.index()) continue; | |
| 152 | if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue; | |
| 153 | global.flags.output_symtab = true; | |
| 154 | if (global.isLocal(elf_file)) { | |
| 155 | output_symtab_size.nlocals += 1; | |
| 156 | } else { | |
| 157 | output_symtab_size.nglobals += 1; | |
| 158 | } | |
| 159 | output_symtab_size.strsize += @as(u32, @intCast(global.name(elf_file).len)) + 1; | |
| 160 | } | |
| 161 | } | |
| 162 | ||
| 163 | pub fn writeSymtab(file: File, elf_file: *Elf, ctx: anytype) void { | |
| 164 | var ilocal = ctx.ilocal; | |
| 165 | for (file.locals()) |local_index| { | |
| 166 | const local = elf_file.symbol(local_index); | |
| 167 | if (!local.flags.output_symtab) continue; | |
| 168 | const out_sym = &elf_file.symtab.items[ilocal]; | |
| 169 | out_sym.st_name = @intCast(elf_file.strtab.items.len); | |
| 170 | elf_file.strtab.appendSliceAssumeCapacity(local.name(elf_file)); | |
| 171 | elf_file.strtab.appendAssumeCapacity(0); | |
| 172 | local.setOutputSym(elf_file, out_sym); | |
| 173 | ilocal += 1; | |
| 174 | } | |
| 175 | ||
| 176 | var iglobal = ctx.iglobal; | |
| 177 | for (file.globals()) |global_index| { | |
| 178 | const global = elf_file.symbol(global_index); | |
| 179 | const file_ptr = global.file(elf_file) orelse continue; | |
| 180 | if (file_ptr.index() != file.index()) continue; | |
| 181 | if (!global.flags.output_symtab) continue; | |
| 182 | const st_name = @as(u32, @intCast(elf_file.strtab.items.len)); | |
| 183 | elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file)); | |
| 184 | elf_file.strtab.appendAssumeCapacity(0); | |
| 185 | if (global.isLocal(elf_file)) { | |
| 186 | const out_sym = &elf_file.symtab.items[ilocal]; | |
| 187 | out_sym.st_name = st_name; | |
| 188 | global.setOutputSym(elf_file, out_sym); | |
| 189 | ilocal += 1; | |
| 190 | } else { | |
| 191 | const out_sym = &elf_file.symtab.items[iglobal]; | |
| 192 | out_sym.st_name = st_name; | |
| 193 | global.setOutputSym(elf_file, out_sym); | |
| 194 | iglobal += 1; | |
| 195 | } | |
| 196 | } | |
| 197 | } | |
| 198 | ||
| 199 | pub fn updateArSymtab(file: File, ar_symtab: *Archive.ArSymtab, elf_file: *Elf) !void { | |
| 200 | return switch (file) { | |
| 201 | .zig_object => |x| x.updateArSymtab(ar_symtab, elf_file), | |
| 202 | .object => @panic("TODO"), | |
| 203 | inline else => unreachable, | |
| 204 | }; | |
| 205 | } | |
| 206 | ||
| 114 | 207 | pub const Index = u32; |
| 115 | 208 | |
| 116 | 209 | pub const Entry = union(enum) { |
| ... | ... | @@ -126,7 +219,9 @@ const std = @import("std"); |
| 126 | 219 | const elf = std.elf; |
| 127 | 220 | |
| 128 | 221 | const Allocator = std.mem.Allocator; |
| 222 | const Archive = @import("Archive.zig"); | |
| 129 | 223 | const Atom = @import("Atom.zig"); |
| 224 | const Cie = @import("eh_frame.zig").Cie; | |
| 130 | 225 | const Elf = @import("../Elf.zig"); |
| 131 | 226 | const LinkerDefined = @import("LinkerDefined.zig"); |
| 132 | 227 | const Object = @import("Object.zig"); |
src/link/Elf/gc.zig+26-17| ... | ... | @@ -1,19 +1,27 @@ |
| 1 | 1 | pub fn gcAtoms(elf_file: *Elf) !void { |
| 2 | var roots = std.ArrayList(*Atom).init(elf_file.base.allocator); | |
| 2 | const gpa = elf_file.base.allocator; | |
| 3 | const num_files = elf_file.objects.items.len + @intFromBool(elf_file.zig_object_index != null); | |
| 4 | var files = try std.ArrayList(File.Index).initCapacity(gpa, num_files); | |
| 5 | defer files.deinit(); | |
| 6 | if (elf_file.zig_object_index) |index| files.appendAssumeCapacity(index); | |
| 7 | for (elf_file.objects.items) |index| files.appendAssumeCapacity(index); | |
| 8 | ||
| 9 | var roots = std.ArrayList(*Atom).init(gpa); | |
| 3 | 10 | defer roots.deinit(); |
| 4 | try collectRoots(&roots, elf_file); | |
| 11 | try collectRoots(&roots, files.items, elf_file); | |
| 12 | ||
| 5 | 13 | mark(roots, elf_file); |
| 6 | prune(elf_file); | |
| 14 | prune(files.items, elf_file); | |
| 7 | 15 | } |
| 8 | 16 | |
| 9 | fn collectRoots(roots: *std.ArrayList(*Atom), elf_file: *Elf) !void { | |
| 17 | fn collectRoots(roots: *std.ArrayList(*Atom), files: []const File.Index, elf_file: *Elf) !void { | |
| 10 | 18 | if (elf_file.entry_index) |index| { |
| 11 | 19 | const global = elf_file.symbol(index); |
| 12 | 20 | try markSymbol(global, roots, elf_file); |
| 13 | 21 | } |
| 14 | 22 | |
| 15 | for (elf_file.objects.items) |index| { | |
| 16 | for (elf_file.file(index).?.object.globals()) |global_index| { | |
| 23 | for (files) |index| { | |
| 24 | for (elf_file.file(index).?.globals()) |global_index| { | |
| 17 | 25 | const global = elf_file.symbol(global_index); |
| 18 | 26 | if (global.file(elf_file)) |file| { |
| 19 | 27 | if (file.index() == index and global.flags.@"export") |
| ... | ... | @@ -22,10 +30,10 @@ fn collectRoots(roots: *std.ArrayList(*Atom), elf_file: *Elf) !void { |
| 22 | 30 | } |
| 23 | 31 | } |
| 24 | 32 | |
| 25 | for (elf_file.objects.items) |index| { | |
| 26 | const object = elf_file.file(index).?.object; | |
| 33 | for (files) |index| { | |
| 34 | const file = elf_file.file(index).?; | |
| 27 | 35 | |
| 28 | for (object.atoms.items) |atom_index| { | |
| 36 | for (file.atoms()) |atom_index| { | |
| 29 | 37 | const atom = elf_file.atom(atom_index) orelse continue; |
| 30 | 38 | if (!atom.flags.alive) continue; |
| 31 | 39 | |
| ... | ... | @@ -49,9 +57,9 @@ fn collectRoots(roots: *std.ArrayList(*Atom), elf_file: *Elf) !void { |
| 49 | 57 | } |
| 50 | 58 | |
| 51 | 59 | // Mark every atom referenced by CIE as alive. |
| 52 | for (object.cies.items) |cie| { | |
| 60 | for (file.cies()) |cie| { | |
| 53 | 61 | for (cie.relocs(elf_file)) |rel| { |
| 54 | const sym = elf_file.symbol(object.symbols.items[rel.r_sym()]); | |
| 62 | const sym = elf_file.symbol(file.symbol(rel.r_sym())); | |
| 55 | 63 | try markSymbol(sym, roots, elf_file); |
| 56 | 64 | } |
| 57 | 65 | } |
| ... | ... | @@ -73,11 +81,11 @@ fn markLive(atom: *Atom, elf_file: *Elf) void { |
| 73 | 81 | if (@import("build_options").enable_logging) track_live_level.incr(); |
| 74 | 82 | |
| 75 | 83 | assert(atom.flags.visited); |
| 76 | const object = atom.file(elf_file).?.object; | |
| 84 | const file = atom.file(elf_file).?; | |
| 77 | 85 | |
| 78 | 86 | for (atom.fdes(elf_file)) |fde| { |
| 79 | 87 | for (fde.relocs(elf_file)[1..]) |rel| { |
| 80 | const target_sym = elf_file.symbol(object.symbols.items[rel.r_sym()]); | |
| 88 | const target_sym = elf_file.symbol(file.symbol(rel.r_sym())); | |
| 81 | 89 | const target_atom = target_sym.atom(elf_file) orelse continue; |
| 82 | 90 | target_atom.flags.alive = true; |
| 83 | 91 | gc_track_live_log.debug("{}marking live atom({d})", .{ track_live_level, target_atom.atom_index }); |
| ... | ... | @@ -86,7 +94,7 @@ fn markLive(atom: *Atom, elf_file: *Elf) void { |
| 86 | 94 | } |
| 87 | 95 | |
| 88 | 96 | for (atom.relocs(elf_file)) |rel| { |
| 89 | const target_sym = elf_file.symbol(object.symbols.items[rel.r_sym()]); | |
| 97 | const target_sym = elf_file.symbol(file.symbol(rel.r_sym())); | |
| 90 | 98 | const target_atom = target_sym.atom(elf_file) orelse continue; |
| 91 | 99 | target_atom.flags.alive = true; |
| 92 | 100 | gc_track_live_log.debug("{}marking live atom({d})", .{ track_live_level, target_atom.atom_index }); |
| ... | ... | @@ -101,9 +109,9 @@ fn mark(roots: std.ArrayList(*Atom), elf_file: *Elf) void { |
| 101 | 109 | } |
| 102 | 110 | } |
| 103 | 111 | |
| 104 | fn prune(elf_file: *Elf) void { | |
| 105 | for (elf_file.objects.items) |index| { | |
| 106 | for (elf_file.file(index).?.object.atoms.items) |atom_index| { | |
| 112 | fn prune(files: []const File.Index, elf_file: *Elf) void { | |
| 113 | for (files) |index| { | |
| 114 | for (elf_file.file(index).?.atoms()) |atom_index| { | |
| 107 | 115 | const atom = elf_file.atom(atom_index) orelse continue; |
| 108 | 116 | if (atom.flags.alive and !atom.flags.visited) { |
| 109 | 117 | atom.flags.alive = false; |
| ... | ... | @@ -158,4 +166,5 @@ const mem = std.mem; |
| 158 | 166 | const Allocator = mem.Allocator; |
| 159 | 167 | const Atom = @import("Atom.zig"); |
| 160 | 168 | const Elf = @import("../Elf.zig"); |
| 169 | const File = @import("file.zig").File; | |
| 161 | 170 | const Symbol = @import("Symbol.zig"); |
src/link/Elf/synthetic_sections.zig+39-67| ... | ... | @@ -9,7 +9,7 @@ pub const DynamicSection = struct { |
| 9 | 9 | |
| 10 | 10 | pub fn addNeeded(dt: *DynamicSection, shared: *SharedObject, elf_file: *Elf) !void { |
| 11 | 11 | const gpa = elf_file.base.allocator; |
| 12 | const off = try elf_file.dynstrtab.insert(gpa, shared.soname()); | |
| 12 | const off = try elf_file.insertDynString(shared.soname()); | |
| 13 | 13 | try dt.needed.append(gpa, off); |
| 14 | 14 | } |
| 15 | 15 | |
| ... | ... | @@ -22,11 +22,11 @@ pub const DynamicSection = struct { |
| 22 | 22 | if (i > 0) try rpath.append(':'); |
| 23 | 23 | try rpath.appendSlice(path); |
| 24 | 24 | } |
| 25 | dt.rpath = try elf_file.dynstrtab.insert(gpa, rpath.items); | |
| 25 | dt.rpath = try elf_file.insertDynString(rpath.items); | |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | pub fn setSoname(dt: *DynamicSection, soname: []const u8, elf_file: *Elf) !void { |
| 29 | dt.soname = try elf_file.dynstrtab.insert(elf_file.base.allocator, soname); | |
| 29 | dt.soname = try elf_file.insertDynString(soname); | |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | fn getFlags(dt: DynamicSection, elf_file: *Elf) ?u64 { |
| ... | ... | @@ -359,31 +359,24 @@ pub const ZigGotSection = struct { |
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | pub fn updateSymtabSize(zig_got: *ZigGotSection, elf_file: *Elf) void { |
| 362 | _ = elf_file; | |
| 363 | 362 | zig_got.output_symtab_size.nlocals = @as(u32, @intCast(zig_got.entries.items.len)); |
| 364 | } | |
| 365 | ||
| 366 | pub fn updateStrtab(zig_got: ZigGotSection, elf_file: *Elf) !void { | |
| 367 | const gpa = elf_file.base.allocator; | |
| 368 | 363 | for (zig_got.entries.items) |entry| { |
| 369 | const symbol_name = elf_file.symbol(entry).name(elf_file); | |
| 370 | const name = try std.fmt.allocPrint(gpa, "{s}$ziggot", .{symbol_name}); | |
| 371 | defer gpa.free(name); | |
| 372 | _ = try elf_file.strtab.insert(gpa, name); | |
| 364 | const name = elf_file.symbol(entry).name(elf_file); | |
| 365 | zig_got.output_symtab_size.strsize += @as(u32, @intCast(name.len + "$ziggot".len)) + 1; | |
| 373 | 366 | } |
| 374 | 367 | } |
| 375 | 368 | |
| 376 | pub fn writeSymtab(zig_got: ZigGotSection, elf_file: *Elf, ctx: anytype) !void { | |
| 377 | const gpa = elf_file.base.allocator; | |
| 369 | pub fn writeSymtab(zig_got: ZigGotSection, elf_file: *Elf, ctx: anytype) void { | |
| 378 | 370 | for (zig_got.entries.items, ctx.ilocal.., 0..) |entry, ilocal, index| { |
| 379 | 371 | const symbol = elf_file.symbol(entry); |
| 380 | 372 | const symbol_name = symbol.name(elf_file); |
| 381 | const name = try std.fmt.allocPrint(gpa, "{s}$ziggot", .{symbol_name}); | |
| 382 | defer gpa.free(name); | |
| 383 | const st_name = try elf_file.strtab.insert(gpa, name); | |
| 373 | const st_name = @as(u32, @intCast(elf_file.strtab.items.len)); | |
| 374 | elf_file.strtab.appendSliceAssumeCapacity(symbol_name); | |
| 375 | elf_file.strtab.appendSliceAssumeCapacity("$ziggot"); | |
| 376 | elf_file.strtab.appendAssumeCapacity(0); | |
| 384 | 377 | const st_value = zig_got.entryAddress(@intCast(index), elf_file); |
| 385 | 378 | const st_size = elf_file.archPtrWidthBytes(); |
| 386 | ctx.symtab[ilocal] = .{ | |
| 379 | elf_file.symtab.items[ilocal] = .{ | |
| 387 | 380 | .st_name = st_name, |
| 388 | 381 | .st_info = elf.STT_OBJECT, |
| 389 | 382 | .st_other = 0, |
| ... | ... | @@ -767,25 +760,17 @@ pub const GotSection = struct { |
| 767 | 760 | } |
| 768 | 761 | |
| 769 | 762 | pub fn updateSymtabSize(got: *GotSection, elf_file: *Elf) void { |
| 770 | _ = elf_file; | |
| 771 | 763 | got.output_symtab_size.nlocals = @as(u32, @intCast(got.entries.items.len)); |
| 772 | } | |
| 773 | ||
| 774 | pub fn updateStrtab(got: GotSection, elf_file: *Elf) !void { | |
| 775 | const gpa = elf_file.base.allocator; | |
| 776 | 764 | for (got.entries.items) |entry| { |
| 777 | 765 | const symbol_name = switch (entry.tag) { |
| 778 | 766 | .tlsld => "", |
| 779 | 767 | inline else => elf_file.symbol(entry.symbol_index).name(elf_file), |
| 780 | 768 | }; |
| 781 | const name = try std.fmt.allocPrint(gpa, "{s}${s}", .{ symbol_name, @tagName(entry.tag) }); | |
| 782 | defer gpa.free(name); | |
| 783 | _ = try elf_file.strtab.insert(gpa, name); | |
| 769 | got.output_symtab_size.strsize += @as(u32, @intCast(symbol_name.len + @tagName(entry.tag).len)) + 1 + 1; | |
| 784 | 770 | } |
| 785 | 771 | } |
| 786 | 772 | |
| 787 | pub fn writeSymtab(got: GotSection, elf_file: *Elf, ctx: anytype) !void { | |
| 788 | const gpa = elf_file.base.allocator; | |
| 773 | pub fn writeSymtab(got: GotSection, elf_file: *Elf, ctx: anytype) void { | |
| 789 | 774 | for (got.entries.items, ctx.ilocal..) |entry, ilocal| { |
| 790 | 775 | const symbol = switch (entry.tag) { |
| 791 | 776 | .tlsld => null, |
| ... | ... | @@ -795,12 +780,14 @@ pub const GotSection = struct { |
| 795 | 780 | .tlsld => "", |
| 796 | 781 | inline else => symbol.?.name(elf_file), |
| 797 | 782 | }; |
| 798 | const name = try std.fmt.allocPrint(gpa, "{s}${s}", .{ symbol_name, @tagName(entry.tag) }); | |
| 799 | defer gpa.free(name); | |
| 800 | const st_name = try elf_file.strtab.insert(gpa, name); | |
| 783 | const st_name = @as(u32, @intCast(elf_file.strtab.items.len)); | |
| 784 | elf_file.strtab.appendSliceAssumeCapacity(symbol_name); | |
| 785 | elf_file.strtab.appendAssumeCapacity('$'); | |
| 786 | elf_file.strtab.appendSliceAssumeCapacity(@tagName(entry.tag)); | |
| 787 | elf_file.strtab.appendAssumeCapacity(0); | |
| 801 | 788 | const st_value = entry.address(elf_file); |
| 802 | 789 | const st_size: u64 = entry.len() * elf_file.archPtrWidthBytes(); |
| 803 | ctx.symtab[ilocal] = .{ | |
| 790 | elf_file.symtab.items[ilocal] = .{ | |
| 804 | 791 | .st_name = st_name, |
| 805 | 792 | .st_info = elf.STT_OBJECT, |
| 806 | 793 | .st_other = 0, |
| ... | ... | @@ -922,30 +909,22 @@ pub const PltSection = struct { |
| 922 | 909 | } |
| 923 | 910 | |
| 924 | 911 | pub fn updateSymtabSize(plt: *PltSection, elf_file: *Elf) void { |
| 925 | _ = elf_file; | |
| 926 | 912 | plt.output_symtab_size.nlocals = @as(u32, @intCast(plt.symbols.items.len)); |
| 927 | } | |
| 928 | ||
| 929 | pub fn updateStrtab(plt: PltSection, elf_file: *Elf) !void { | |
| 930 | const gpa = elf_file.base.allocator; | |
| 931 | 913 | for (plt.symbols.items) |sym_index| { |
| 932 | const sym = elf_file.symbol(sym_index); | |
| 933 | const name = try std.fmt.allocPrint(gpa, "{s}$plt", .{sym.name(elf_file)}); | |
| 934 | defer gpa.free(name); | |
| 935 | _ = try elf_file.strtab.insert(gpa, name); | |
| 914 | const name = elf_file.symbol(sym_index).name(elf_file); | |
| 915 | plt.output_symtab_size.strsize += @as(u32, @intCast(name.len + "$plt".len)) + 1; | |
| 936 | 916 | } |
| 937 | 917 | } |
| 938 | 918 | |
| 939 | pub fn writeSymtab(plt: PltSection, elf_file: *Elf, ctx: anytype) !void { | |
| 940 | const gpa = elf_file.base.allocator; | |
| 941 | ||
| 919 | pub fn writeSymtab(plt: PltSection, elf_file: *Elf, ctx: anytype) void { | |
| 942 | 920 | var ilocal = ctx.ilocal; |
| 943 | 921 | for (plt.symbols.items) |sym_index| { |
| 944 | 922 | const sym = elf_file.symbol(sym_index); |
| 945 | const name = try std.fmt.allocPrint(gpa, "{s}$plt", .{sym.name(elf_file)}); | |
| 946 | defer gpa.free(name); | |
| 947 | const st_name = try elf_file.strtab.insert(gpa, name); | |
| 948 | ctx.symtab[ilocal] = .{ | |
| 923 | const st_name = @as(u32, @intCast(elf_file.strtab.items.len)); | |
| 924 | elf_file.strtab.appendSliceAssumeCapacity(sym.name(elf_file)); | |
| 925 | elf_file.strtab.appendSliceAssumeCapacity("$plt"); | |
| 926 | elf_file.strtab.appendAssumeCapacity(0); | |
| 927 | elf_file.symtab.items[ilocal] = .{ | |
| 949 | 928 | .st_name = st_name, |
| 950 | 929 | .st_info = elf.STT_FUNC, |
| 951 | 930 | .st_other = 0, |
| ... | ... | @@ -1029,29 +1008,22 @@ pub const PltGotSection = struct { |
| 1029 | 1008 | } |
| 1030 | 1009 | |
| 1031 | 1010 | pub fn updateSymtabSize(plt_got: *PltGotSection, elf_file: *Elf) void { |
| 1032 | _ = elf_file; | |
| 1033 | 1011 | plt_got.output_symtab_size.nlocals = @as(u32, @intCast(plt_got.symbols.items.len)); |
| 1034 | } | |
| 1035 | ||
| 1036 | pub fn updateStrtab(plt_got: PltGotSection, elf_file: *Elf) !void { | |
| 1037 | const gpa = elf_file.base.allocator; | |
| 1038 | 1012 | for (plt_got.symbols.items) |sym_index| { |
| 1039 | const sym = elf_file.symbol(sym_index); | |
| 1040 | const name = try std.fmt.allocPrint(gpa, "{s}$pltgot", .{sym.name(elf_file)}); | |
| 1041 | defer gpa.free(name); | |
| 1042 | _ = try elf_file.strtab.insert(gpa, name); | |
| 1013 | const name = elf_file.symbol(sym_index).name(elf_file); | |
| 1014 | plt_got.output_symtab_size.strsize += @as(u32, @intCast(name.len + "$pltgot".len)) + 1; | |
| 1043 | 1015 | } |
| 1044 | 1016 | } |
| 1045 | 1017 | |
| 1046 | pub fn writeSymtab(plt_got: PltGotSection, elf_file: *Elf, ctx: anytype) !void { | |
| 1047 | const gpa = elf_file.base.allocator; | |
| 1018 | pub fn writeSymtab(plt_got: PltGotSection, elf_file: *Elf, ctx: anytype) void { | |
| 1048 | 1019 | var ilocal = ctx.ilocal; |
| 1049 | 1020 | for (plt_got.symbols.items) |sym_index| { |
| 1050 | 1021 | const sym = elf_file.symbol(sym_index); |
| 1051 | const name = try std.fmt.allocPrint(gpa, "{s}$pltgot", .{sym.name(elf_file)}); | |
| 1052 | defer gpa.free(name); | |
| 1053 | const st_name = try elf_file.strtab.insert(gpa, name); | |
| 1054 | ctx.symtab[ilocal] = .{ | |
| 1022 | const st_name = @as(u32, @intCast(elf_file.strtab.items.len)); | |
| 1023 | elf_file.strtab.appendSliceAssumeCapacity(sym.name(elf_file)); | |
| 1024 | elf_file.strtab.appendSliceAssumeCapacity("$pltgot"); | |
| 1025 | elf_file.strtab.appendAssumeCapacity(0); | |
| 1026 | elf_file.symtab.items[ilocal] = .{ | |
| 1055 | 1027 | .st_name = st_name, |
| 1056 | 1028 | .st_info = elf.STT_FUNC, |
| 1057 | 1029 | .st_other = 0, |
| ... | ... | @@ -1166,7 +1138,7 @@ pub const DynsymSection = struct { |
| 1166 | 1138 | new_extra.dynamic = index; |
| 1167 | 1139 | sym.setExtra(new_extra, elf_file); |
| 1168 | 1140 | } else try sym.addExtra(.{ .dynamic = index }, elf_file); |
| 1169 | const off = try elf_file.dynstrtab.insert(gpa, sym.name(elf_file)); | |
| 1141 | const off = try elf_file.insertDynString(sym.name(elf_file)); | |
| 1170 | 1142 | try dynsym.entries.append(gpa, .{ .symbol_index = sym_index, .off = off }); |
| 1171 | 1143 | } |
| 1172 | 1144 | |
| ... | ... | @@ -1251,7 +1223,7 @@ pub const HashSection = struct { |
| 1251 | 1223 | @memset(chains, 0); |
| 1252 | 1224 | |
| 1253 | 1225 | for (elf_file.dynsym.entries.items, 1..) |entry, i| { |
| 1254 | const name = elf_file.dynstrtab.getAssumeExists(entry.off); | |
| 1226 | const name = elf_file.getDynString(entry.off); | |
| 1255 | 1227 | const hash = hasher(name) % buckets.len; |
| 1256 | 1228 | chains[@as(u32, @intCast(i))] = buckets[hash]; |
| 1257 | 1229 | buckets[hash] = @as(u32, @intCast(i)); |
| ... | ... | @@ -1490,7 +1462,7 @@ pub const VerneedSection = struct { |
| 1490 | 1462 | sym.* = .{ |
| 1491 | 1463 | .vn_version = 1, |
| 1492 | 1464 | .vn_cnt = 0, |
| 1493 | .vn_file = try elf_file.dynstrtab.insert(gpa, soname), | |
| 1465 | .vn_file = try elf_file.insertDynString(soname), | |
| 1494 | 1466 | .vn_aux = 0, |
| 1495 | 1467 | .vn_next = 0, |
| 1496 | 1468 | }; |
| ... | ... | @@ -1509,7 +1481,7 @@ pub const VerneedSection = struct { |
| 1509 | 1481 | .vna_hash = HashSection.hasher(version), |
| 1510 | 1482 | .vna_flags = 0, |
| 1511 | 1483 | .vna_other = vern.index, |
| 1512 | .vna_name = try elf_file.dynstrtab.insert(gpa, version), | |
| 1484 | .vna_name = try elf_file.insertDynString(version), | |
| 1513 | 1485 | .vna_next = 0, |
| 1514 | 1486 | }; |
| 1515 | 1487 | verneed_sym.vn_cnt += 1; |
src/link/MachO.zig+2-2| ... | ... | @@ -58,7 +58,7 @@ globals_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| 58 | 58 | dyld_stub_binder_index: ?u32 = null, |
| 59 | 59 | dyld_private_atom_index: ?Atom.Index = null, |
| 60 | 60 | |
| 61 | strtab: StringTable(.strtab) = .{}, | |
| 61 | strtab: StringTable = .{}, | |
| 62 | 62 | |
| 63 | 63 | got_table: TableSection(SymbolWithLoc) = .{}, |
| 64 | 64 | stub_table: TableSection(SymbolWithLoc) = .{}, |
| ... | ... | @@ -5643,7 +5643,7 @@ const Module = @import("../Module.zig"); |
| 5643 | 5643 | const InternPool = @import("../InternPool.zig"); |
| 5644 | 5644 | const Platform = load_commands.Platform; |
| 5645 | 5645 | const Relocation = @import("MachO/Relocation.zig"); |
| 5646 | const StringTable = @import("strtab.zig").StringTable; | |
| 5646 | const StringTable = @import("StringTable.zig"); | |
| 5647 | 5647 | const TableSection = @import("table_section.zig").TableSection; |
| 5648 | 5648 | const Trie = @import("MachO/Trie.zig"); |
| 5649 | 5649 | const Type = @import("../type.zig").Type; |
src/link/MachO/DebugSymbols.zig+2-2| ... | ... | @@ -22,7 +22,7 @@ debug_aranges_section_dirty: bool = false, |
| 22 | 22 | debug_info_header_dirty: bool = false, |
| 23 | 23 | debug_line_header_dirty: bool = false, |
| 24 | 24 | |
| 25 | strtab: StringTable(.strtab) = .{}, | |
| 25 | strtab: StringTable = .{}, | |
| 26 | 26 | relocs: std.ArrayListUnmanaged(Reloc) = .{}, |
| 27 | 27 | |
| 28 | 28 | pub const Reloc = struct { |
| ... | ... | @@ -567,5 +567,5 @@ const Allocator = mem.Allocator; |
| 567 | 567 | const Dwarf = @import("../Dwarf.zig"); |
| 568 | 568 | const MachO = @import("../MachO.zig"); |
| 569 | 569 | const Module = @import("../../Module.zig"); |
| 570 | const StringTable = @import("../strtab.zig").StringTable; | |
| 570 | const StringTable = @import("../StringTable.zig"); | |
| 571 | 571 | const Type = @import("../../type.zig").Type; |
src/link/MachO/zld.zig-1| ... | ... | @@ -1227,7 +1227,6 @@ const LibStub = @import("../tapi.zig").LibStub; |
| 1227 | 1227 | const Object = @import("Object.zig"); |
| 1228 | 1228 | const Platform = load_commands.Platform; |
| 1229 | 1229 | const Section = MachO.Section; |
| 1230 | const StringTable = @import("../strtab.zig").StringTable; | |
| 1231 | 1230 | const SymbolWithLoc = MachO.SymbolWithLoc; |
| 1232 | 1231 | const TableSection = @import("../table_section.zig").TableSection; |
| 1233 | 1232 | const Trie = @import("Trie.zig"); |
src/link/StringTable.zig created+49| ... | ... | @@ -0,0 +1,49 @@ |
| 1 | buffer: std.ArrayListUnmanaged(u8) = .{}, | |
| 2 | table: std.HashMapUnmanaged(u32, void, StringIndexContext, std.hash_map.default_max_load_percentage) = .{}, | |
| 3 | ||
| 4 | pub fn deinit(self: *Self, gpa: Allocator) void { | |
| 5 | self.buffer.deinit(gpa); | |
| 6 | self.table.deinit(gpa); | |
| 7 | } | |
| 8 | ||
| 9 | pub fn insert(self: *Self, gpa: Allocator, string: []const u8) !u32 { | |
| 10 | const gop = try self.table.getOrPutContextAdapted(gpa, @as([]const u8, string), StringIndexAdapter{ | |
| 11 | .bytes = &self.buffer, | |
| 12 | }, StringIndexContext{ | |
| 13 | .bytes = &self.buffer, | |
| 14 | }); | |
| 15 | if (gop.found_existing) return gop.key_ptr.*; | |
| 16 | ||
| 17 | try self.buffer.ensureUnusedCapacity(gpa, string.len + 1); | |
| 18 | const new_off = @as(u32, @intCast(self.buffer.items.len)); | |
| 19 | ||
| 20 | self.buffer.appendSliceAssumeCapacity(string); | |
| 21 | self.buffer.appendAssumeCapacity(0); | |
| 22 | ||
| 23 | gop.key_ptr.* = new_off; | |
| 24 | ||
| 25 | return new_off; | |
| 26 | } | |
| 27 | ||
| 28 | pub fn getOffset(self: *Self, string: []const u8) ?u32 { | |
| 29 | return self.table.getKeyAdapted(string, StringIndexAdapter{ | |
| 30 | .bytes = &self.buffer, | |
| 31 | }); | |
| 32 | } | |
| 33 | ||
| 34 | pub fn get(self: Self, off: u32) ?[:0]const u8 { | |
| 35 | if (off >= self.buffer.items.len) return null; | |
| 36 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.buffer.items.ptr + off)), 0); | |
| 37 | } | |
| 38 | ||
| 39 | pub fn getAssumeExists(self: Self, off: u32) [:0]const u8 { | |
| 40 | return self.get(off) orelse unreachable; | |
| 41 | } | |
| 42 | ||
| 43 | const std = @import("std"); | |
| 44 | const mem = std.mem; | |
| 45 | ||
| 46 | const Allocator = mem.Allocator; | |
| 47 | const Self = @This(); | |
| 48 | const StringIndexAdapter = std.hash_map.StringIndexAdapter; | |
| 49 | const StringIndexContext = std.hash_map.StringIndexContext; |
src/link/strtab.zig deleted-121| ... | ... | @@ -1,121 +0,0 @@ |
| 1 | const std = @import("std"); | |
| 2 | const mem = std.mem; | |
| 3 | ||
| 4 | const Allocator = mem.Allocator; | |
| 5 | const StringIndexAdapter = std.hash_map.StringIndexAdapter; | |
| 6 | const StringIndexContext = std.hash_map.StringIndexContext; | |
| 7 | ||
| 8 | pub fn StringTable(comptime log_scope: @Type(.EnumLiteral)) type { | |
| 9 | return struct { | |
| 10 | const Self = @This(); | |
| 11 | ||
| 12 | const log = std.log.scoped(log_scope); | |
| 13 | ||
| 14 | buffer: std.ArrayListUnmanaged(u8) = .{}, | |
| 15 | table: std.HashMapUnmanaged(u32, bool, StringIndexContext, std.hash_map.default_max_load_percentage) = .{}, | |
| 16 | ||
| 17 | pub fn deinit(self: *Self, gpa: Allocator) void { | |
| 18 | self.buffer.deinit(gpa); | |
| 19 | self.table.deinit(gpa); | |
| 20 | } | |
| 21 | ||
| 22 | pub fn toOwnedSlice(self: *Self, gpa: Allocator) []const u8 { | |
| 23 | const result = self.buffer.toOwnedSlice(gpa); | |
| 24 | self.table.clearRetainingCapacity(); | |
| 25 | return result; | |
| 26 | } | |
| 27 | ||
| 28 | pub const PrunedResult = struct { | |
| 29 | buffer: []const u8, | |
| 30 | idx_map: std.AutoHashMap(u32, u32), | |
| 31 | }; | |
| 32 | ||
| 33 | pub fn toPrunedResult(self: *Self, gpa: Allocator) !PrunedResult { | |
| 34 | var buffer = std.ArrayList(u8).init(gpa); | |
| 35 | defer buffer.deinit(); | |
| 36 | try buffer.ensureTotalCapacity(self.buffer.items.len); | |
| 37 | buffer.appendAssumeCapacity(0); | |
| 38 | ||
| 39 | var idx_map = std.AutoHashMap(u32, u32).init(gpa); | |
| 40 | errdefer idx_map.deinit(); | |
| 41 | try idx_map.ensureTotalCapacity(self.table.count()); | |
| 42 | ||
| 43 | var it = self.table.iterator(); | |
| 44 | while (it.next()) |entry| { | |
| 45 | const off = entry.key_ptr.*; | |
| 46 | const save = entry.value_ptr.*; | |
| 47 | if (!save) continue; | |
| 48 | const new_off = @as(u32, @intCast(buffer.items.len)); | |
| 49 | buffer.appendSliceAssumeCapacity(self.getAssumeExists(off)); | |
| 50 | idx_map.putAssumeCapacityNoClobber(off, new_off); | |
| 51 | } | |
| 52 | ||
| 53 | self.buffer.clearRetainingCapacity(); | |
| 54 | self.table.clearRetainingCapacity(); | |
| 55 | ||
| 56 | return PrunedResult{ | |
| 57 | .buffer = buffer.toOwnedSlice(), | |
| 58 | .idx_map = idx_map, | |
| 59 | }; | |
| 60 | } | |
| 61 | ||
| 62 | pub fn insert(self: *Self, gpa: Allocator, string: []const u8) !u32 { | |
| 63 | const gop = try self.table.getOrPutContextAdapted(gpa, @as([]const u8, string), StringIndexAdapter{ | |
| 64 | .bytes = &self.buffer, | |
| 65 | }, StringIndexContext{ | |
| 66 | .bytes = &self.buffer, | |
| 67 | }); | |
| 68 | if (gop.found_existing) { | |
| 69 | const off = gop.key_ptr.*; | |
| 70 | gop.value_ptr.* = true; | |
| 71 | log.debug("reusing string '{s}' at offset 0x{x}", .{ string, off }); | |
| 72 | return off; | |
| 73 | } | |
| 74 | ||
| 75 | try self.buffer.ensureUnusedCapacity(gpa, string.len + 1); | |
| 76 | const new_off = @as(u32, @intCast(self.buffer.items.len)); | |
| 77 | ||
| 78 | log.debug("writing new string '{s}' at offset 0x{x}", .{ string, new_off }); | |
| 79 | ||
| 80 | self.buffer.appendSliceAssumeCapacity(string); | |
| 81 | self.buffer.appendAssumeCapacity(0); | |
| 82 | ||
| 83 | gop.key_ptr.* = new_off; | |
| 84 | gop.value_ptr.* = true; | |
| 85 | ||
| 86 | return new_off; | |
| 87 | } | |
| 88 | ||
| 89 | pub fn delete(self: *Self, string: []const u8) void { | |
| 90 | const value_ptr = self.table.getPtrAdapted(@as([]const u8, string), StringIndexAdapter{ | |
| 91 | .bytes = &self.buffer, | |
| 92 | }) orelse return; | |
| 93 | value_ptr.* = false; | |
| 94 | log.debug("marked '{s}' for deletion", .{string}); | |
| 95 | } | |
| 96 | ||
| 97 | pub fn getOffset(self: *Self, string: []const u8) ?u32 { | |
| 98 | return self.table.getKeyAdapted(string, StringIndexAdapter{ | |
| 99 | .bytes = &self.buffer, | |
| 100 | }); | |
| 101 | } | |
| 102 | ||
| 103 | pub fn get(self: Self, off: u32) ?[:0]const u8 { | |
| 104 | log.debug("getting string at 0x{x}", .{off}); | |
| 105 | if (off >= self.buffer.items.len) return null; | |
| 106 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.buffer.items.ptr + off)), 0); | |
| 107 | } | |
| 108 | ||
| 109 | pub fn getAssumeExists(self: Self, off: u32) [:0]const u8 { | |
| 110 | return self.get(off) orelse unreachable; | |
| 111 | } | |
| 112 | ||
| 113 | pub fn items(self: Self) []const u8 { | |
| 114 | return self.buffer.items; | |
| 115 | } | |
| 116 | ||
| 117 | pub fn len(self: Self) usize { | |
| 118 | return self.buffer.items.len; | |
| 119 | } | |
| 120 | }; | |
| 121 | } |
test/link/elf.zig+183-5| ... | ... | @@ -6,9 +6,13 @@ pub fn build(b: *Build) void { |
| 6 | 6 | const elf_step = b.step("test-elf", "Run ELF tests"); |
| 7 | 7 | b.default_step = elf_step; |
| 8 | 8 | |
| 9 | const musl_target = CrossTarget{ | |
| 9 | const default_target = CrossTarget{ | |
| 10 | 10 | .cpu_arch = .x86_64, // TODO relax this once ELF linker is able to handle other archs |
| 11 | 11 | .os_tag = .linux, |
| 12 | }; | |
| 13 | const musl_target = CrossTarget{ | |
| 14 | .cpu_arch = .x86_64, | |
| 15 | .os_tag = .linux, | |
| 12 | 16 | .abi = .musl, |
| 13 | 17 | }; |
| 14 | 18 | const glibc_target = CrossTarget{ |
| ... | ... | @@ -18,7 +22,10 @@ pub fn build(b: *Build) void { |
| 18 | 22 | }; |
| 19 | 23 | |
| 20 | 24 | // Exercise linker with self-hosted backend (no LLVM) |
| 21 | elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false })); | |
| 25 | elf_step.dependOn(testGcSectionsZig(b, .{ .use_llvm = false, .target = default_target })); | |
| 26 | elf_step.dependOn(testLinkingObj(b, .{ .use_llvm = false, .target = default_target })); | |
| 27 | elf_step.dependOn(testLinkingStaticLib(b, .{ .use_llvm = false, .target = default_target })); | |
| 28 | elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false, .target = default_target })); | |
| 22 | 29 | elf_step.dependOn(testImportingDataDynamic(b, .{ .use_llvm = false, .target = glibc_target })); |
| 23 | 30 | elf_step.dependOn(testImportingDataStatic(b, .{ .use_llvm = false, .target = musl_target })); |
| 24 | 31 | |
| ... | ... | @@ -876,6 +883,110 @@ fn testGcSections(b: *Build, opts: Options) *Step { |
| 876 | 883 | return test_step; |
| 877 | 884 | } |
| 878 | 885 | |
| 886 | fn testGcSectionsZig(b: *Build, opts: Options) *Step { | |
| 887 | const test_step = addTestStep(b, "gc-sections-zig", opts); | |
| 888 | ||
| 889 | const obj = addObject(b, "obj", .{ | |
| 890 | .target = opts.target, | |
| 891 | .use_llvm = true, | |
| 892 | .use_lld = true, | |
| 893 | }); | |
| 894 | addCSourceBytes(obj, | |
| 895 | \\int live_var1 = 1; | |
| 896 | \\int live_var2 = 2; | |
| 897 | \\int dead_var1 = 3; | |
| 898 | \\int dead_var2 = 4; | |
| 899 | \\void live_fn1() {} | |
| 900 | \\void live_fn2() { live_fn1(); } | |
| 901 | \\void dead_fn1() {} | |
| 902 | \\void dead_fn2() { dead_fn1(); } | |
| 903 | , &.{}); | |
| 904 | obj.link_function_sections = true; | |
| 905 | obj.link_data_sections = true; | |
| 906 | ||
| 907 | { | |
| 908 | const exe = addExecutable(b, "test1", opts); | |
| 909 | addZigSourceBytes(exe, | |
| 910 | \\const std = @import("std"); | |
| 911 | \\extern var live_var1: i32; | |
| 912 | \\extern var live_var2: i32; | |
| 913 | \\extern fn live_fn2() void; | |
| 914 | \\pub fn main() void { | |
| 915 | \\ const stdout = std.io.getStdOut(); | |
| 916 | \\ stdout.writer().print("{d} {d}\n", .{ live_var1, live_var2 }) catch unreachable; | |
| 917 | \\ live_fn2(); | |
| 918 | \\} | |
| 919 | ); | |
| 920 | exe.addObject(obj); | |
| 921 | exe.link_gc_sections = false; | |
| 922 | ||
| 923 | const run = addRunArtifact(exe); | |
| 924 | run.expectStdOutEqual("1 2\n"); | |
| 925 | test_step.dependOn(&run.step); | |
| 926 | ||
| 927 | const check = exe.checkObject(); | |
| 928 | check.checkInSymtab(); | |
| 929 | check.checkContains("live_var1"); | |
| 930 | check.checkInSymtab(); | |
| 931 | check.checkContains("live_var2"); | |
| 932 | check.checkInSymtab(); | |
| 933 | check.checkContains("dead_var1"); | |
| 934 | check.checkInSymtab(); | |
| 935 | check.checkContains("dead_var2"); | |
| 936 | check.checkInSymtab(); | |
| 937 | check.checkContains("live_fn1"); | |
| 938 | check.checkInSymtab(); | |
| 939 | check.checkContains("live_fn2"); | |
| 940 | check.checkInSymtab(); | |
| 941 | check.checkContains("dead_fn1"); | |
| 942 | check.checkInSymtab(); | |
| 943 | check.checkContains("dead_fn2"); | |
| 944 | test_step.dependOn(&check.step); | |
| 945 | } | |
| 946 | ||
| 947 | { | |
| 948 | const exe = addExecutable(b, "test2", opts); | |
| 949 | addZigSourceBytes(exe, | |
| 950 | \\const std = @import("std"); | |
| 951 | \\extern var live_var1: i32; | |
| 952 | \\extern var live_var2: i32; | |
| 953 | \\extern fn live_fn2() void; | |
| 954 | \\pub fn main() void { | |
| 955 | \\ const stdout = std.io.getStdOut(); | |
| 956 | \\ stdout.writer().print("{d} {d}\n", .{ live_var1, live_var2 }) catch unreachable; | |
| 957 | \\ live_fn2(); | |
| 958 | \\} | |
| 959 | ); | |
| 960 | exe.addObject(obj); | |
| 961 | exe.link_gc_sections = true; | |
| 962 | ||
| 963 | const run = addRunArtifact(exe); | |
| 964 | run.expectStdOutEqual("1 2\n"); | |
| 965 | test_step.dependOn(&run.step); | |
| 966 | ||
| 967 | const check = exe.checkObject(); | |
| 968 | check.checkInSymtab(); | |
| 969 | check.checkContains("live_var1"); | |
| 970 | check.checkInSymtab(); | |
| 971 | check.checkContains("live_var2"); | |
| 972 | check.checkInSymtab(); | |
| 973 | check.checkNotPresent("dead_var1"); | |
| 974 | check.checkInSymtab(); | |
| 975 | check.checkNotPresent("dead_var2"); | |
| 976 | check.checkInSymtab(); | |
| 977 | check.checkContains("live_fn1"); | |
| 978 | check.checkInSymtab(); | |
| 979 | check.checkContains("live_fn2"); | |
| 980 | check.checkInSymtab(); | |
| 981 | check.checkNotPresent("dead_fn1"); | |
| 982 | check.checkInSymtab(); | |
| 983 | check.checkNotPresent("dead_fn2"); | |
| 984 | test_step.dependOn(&check.step); | |
| 985 | } | |
| 986 | ||
| 987 | return test_step; | |
| 988 | } | |
| 989 | ||
| 879 | 990 | fn testHiddenWeakUndef(b: *Build, opts: Options) *Step { |
| 880 | 991 | const test_step = addTestStep(b, "hidden-weak-undef", opts); |
| 881 | 992 | |
| ... | ... | @@ -1714,6 +1825,72 @@ fn testLinkingCpp(b: *Build, opts: Options) *Step { |
| 1714 | 1825 | return test_step; |
| 1715 | 1826 | } |
| 1716 | 1827 | |
| 1828 | fn testLinkingObj(b: *Build, opts: Options) *Step { | |
| 1829 | const test_step = addTestStep(b, "linking-obj", opts); | |
| 1830 | ||
| 1831 | const obj = addObject(b, "aobj", opts); | |
| 1832 | addZigSourceBytes(obj, | |
| 1833 | \\extern var mod: usize; | |
| 1834 | \\export fn callMe() usize { | |
| 1835 | \\ return me * mod; | |
| 1836 | \\} | |
| 1837 | \\var me: usize = 42; | |
| 1838 | ); | |
| 1839 | ||
| 1840 | const exe = addExecutable(b, "testobj", opts); | |
| 1841 | addZigSourceBytes(exe, | |
| 1842 | \\const std = @import("std"); | |
| 1843 | \\extern fn callMe() usize; | |
| 1844 | \\export var mod: usize = 2; | |
| 1845 | \\pub fn main() void { | |
| 1846 | \\ std.debug.print("{d}\n", .{callMe()}); | |
| 1847 | \\} | |
| 1848 | ); | |
| 1849 | exe.addObject(obj); | |
| 1850 | ||
| 1851 | const run = addRunArtifact(exe); | |
| 1852 | run.expectStdErrEqual("84\n"); | |
| 1853 | test_step.dependOn(&run.step); | |
| 1854 | ||
| 1855 | return test_step; | |
| 1856 | } | |
| 1857 | ||
| 1858 | fn testLinkingStaticLib(b: *Build, opts: Options) *Step { | |
| 1859 | const test_step = addTestStep(b, "linking-static-lib", opts); | |
| 1860 | ||
| 1861 | const lib = b.addStaticLibrary(.{ | |
| 1862 | .name = "alib", | |
| 1863 | .target = opts.target, | |
| 1864 | .optimize = opts.optimize, | |
| 1865 | .use_llvm = opts.use_llvm, | |
| 1866 | .use_lld = false, | |
| 1867 | }); | |
| 1868 | addZigSourceBytes(lib, | |
| 1869 | \\extern var mod: usize; | |
| 1870 | \\export fn callMe() usize { | |
| 1871 | \\ return me * mod; | |
| 1872 | \\} | |
| 1873 | \\var me: usize = 42; | |
| 1874 | ); | |
| 1875 | ||
| 1876 | const exe = addExecutable(b, "testlib", opts); | |
| 1877 | addZigSourceBytes(exe, | |
| 1878 | \\const std = @import("std"); | |
| 1879 | \\extern fn callMe() usize; | |
| 1880 | \\export var mod: usize = 2; | |
| 1881 | \\pub fn main() void { | |
| 1882 | \\ std.debug.print("{d}\n", .{callMe()}); | |
| 1883 | \\} | |
| 1884 | ); | |
| 1885 | exe.linkLibrary(lib); | |
| 1886 | ||
| 1887 | const run = addRunArtifact(exe); | |
| 1888 | run.expectStdErrEqual("84\n"); | |
| 1889 | test_step.dependOn(&run.step); | |
| 1890 | ||
| 1891 | return test_step; | |
| 1892 | } | |
| 1893 | ||
| 1717 | 1894 | fn testLinkingZig(b: *Build, opts: Options) *Step { |
| 1718 | 1895 | const test_step = addTestStep(b, "linking-zig-static", opts); |
| 1719 | 1896 | |
| ... | ... | @@ -3114,6 +3291,7 @@ const Options = struct { |
| 3114 | 3291 | target: CrossTarget = .{ .cpu_arch = .x86_64, .os_tag = .linux }, |
| 3115 | 3292 | optimize: std.builtin.OptimizeMode = .Debug, |
| 3116 | 3293 | use_llvm: bool = true, |
| 3294 | use_lld: bool = false, | |
| 3117 | 3295 | }; |
| 3118 | 3296 | |
| 3119 | 3297 | fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step { |
| ... | ... | @@ -3134,7 +3312,7 @@ fn addExecutable(b: *Build, name: []const u8, opts: Options) *Compile { |
| 3134 | 3312 | .target = opts.target, |
| 3135 | 3313 | .optimize = opts.optimize, |
| 3136 | 3314 | .use_llvm = opts.use_llvm, |
| 3137 | .use_lld = false, | |
| 3315 | .use_lld = opts.use_lld, | |
| 3138 | 3316 | }); |
| 3139 | 3317 | } |
| 3140 | 3318 | |
| ... | ... | @@ -3144,7 +3322,7 @@ fn addObject(b: *Build, name: []const u8, opts: Options) *Compile { |
| 3144 | 3322 | .target = opts.target, |
| 3145 | 3323 | .optimize = opts.optimize, |
| 3146 | 3324 | .use_llvm = opts.use_llvm, |
| 3147 | .use_lld = false, | |
| 3325 | .use_lld = opts.use_lld, | |
| 3148 | 3326 | }); |
| 3149 | 3327 | } |
| 3150 | 3328 | |
| ... | ... | @@ -3164,7 +3342,7 @@ fn addSharedLibrary(b: *Build, name: []const u8, opts: Options) *Compile { |
| 3164 | 3342 | .target = opts.target, |
| 3165 | 3343 | .optimize = opts.optimize, |
| 3166 | 3344 | .use_llvm = opts.use_llvm, |
| 3167 | .use_lld = false, | |
| 3345 | .use_lld = opts.use_lld, | |
| 3168 | 3346 | }); |
| 3169 | 3347 | } |
| 3170 | 3348 |