| ... | @@ -971,7 +971,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node | ... | @@ -971,7 +971,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 971 | // TODO This linker code currently assumes there is only 1 compilation unit and it | 971 | // TODO This linker code currently assumes there is only 1 compilation unit and it |
| 972 | // corresponds to the Zig source code. | 972 | // corresponds to the Zig source code. |
| 973 | const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented; | 973 | const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented; |
| 974 | _ = module; | | |
| 975 | | 974 | |
| 976 | self.linker_defined_index = blk: { | 975 | self.linker_defined_index = blk: { |
| 977 | const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); | 976 | const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); |
| ... | @@ -979,82 +978,77 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node | ... | @@ -979,82 +978,77 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 979 | break :blk index; | 978 | break :blk index; |
| 980 | }; | 979 | }; |
| 981 | | 980 | |
| 982 | std.debug.print("{}\n", .{self.dumpState()}); | 981 | if (self.lazy_syms.getPtr(.none)) |metadata| { |
| 983 | return error.FlushFailure; | 982 | // Most lazy symbols can be updated on first use, but |
| 984 | | 983 | // anyerror needs to wait for everything to be flushed. |
| 985 | // if (self.lazy_syms.getPtr(.none)) |metadata| { | 984 | if (metadata.text_state != .unused) self.updateLazySymbol( |
| 986 | // // Most lazy symbols can be updated on first use, but | 985 | link.File.LazySymbol.initDecl(.code, null, module), |
| 987 | // // anyerror needs to wait for everything to be flushed. | 986 | metadata.text_symbol_index, |
| 988 | // if (metadata.text_state != .unused) self.updateLazySymbolAtom( | 987 | ) catch |err| return switch (err) { |
| 989 | // link.File.LazySymbol.initDecl(.code, null, module), | 988 | error.CodegenFail => error.FlushFailure, |
| 990 | // metadata.text_atom, | 989 | else => |e| e, |
| 991 | // self.text_section_index.?, | 990 | }; |
| 992 | // ) catch |err| return switch (err) { | 991 | if (metadata.rodata_state != .unused) self.updateLazySymbol( |
| 993 | // error.CodegenFail => error.FlushFailure, | 992 | link.File.LazySymbol.initDecl(.const_data, null, module), |
| 994 | // else => |e| e, | 993 | metadata.rodata_symbol_index, |
| 995 | // }; | 994 | ) catch |err| return switch (err) { |
| 996 | // if (metadata.rodata_state != .unused) self.updateLazySymbolAtom( | 995 | error.CodegenFail => error.FlushFailure, |
| 997 | // link.File.LazySymbol.initDecl(.const_data, null, module), | 996 | else => |e| e, |
| 998 | // metadata.rodata_atom, | 997 | }; |
| 999 | // self.rodata_section_index.?, | 998 | } |
| 1000 | // ) catch |err| return switch (err) { | 999 | for (self.lazy_syms.values()) |*metadata| { |
| 1001 | // error.CodegenFail => error.FlushFailure, | 1000 | if (metadata.text_state != .unused) metadata.text_state = .flushed; |
| 1002 | // else => |e| e, | 1001 | if (metadata.rodata_state != .unused) metadata.rodata_state = .flushed; |
| 1003 | // }; | 1002 | } |
| 1004 | // } | | |
| 1005 | // for (self.lazy_syms.values()) |*metadata| { | | |
| 1006 | // if (metadata.text_state != .unused) metadata.text_state = .flushed; | | |
| 1007 | // if (metadata.rodata_state != .unused) metadata.rodata_state = .flushed; | | |
| 1008 | // } | | |
| 1009 | | 1003 | |
| 1010 | // const target_endian = self.base.options.target.cpu.arch.endian(); | 1004 | const target_endian = self.base.options.target.cpu.arch.endian(); |
| 1011 | // const foreign_endian = target_endian != builtin.cpu.arch.endian(); | 1005 | const foreign_endian = target_endian != builtin.cpu.arch.endian(); |
| | 1006 | _ = foreign_endian; |
| 1012 | | 1007 | |
| 1013 | // if (self.dwarf) |*dw| { | 1008 | if (self.dwarf) |*dw| { |
| 1014 | // try dw.flushModule(module); | 1009 | try dw.flushModule(module); |
| 1015 | // } | 1010 | } |
| 1016 | | 1011 | |
| 1017 | // { | 1012 | { |
| 1018 | // var it = self.relocs.iterator(); | 1013 | var it = self.relocs.iterator(); |
| 1019 | // while (it.next()) |entry| { | 1014 | while (it.next()) |entry| { |
| 1020 | // const atom_index = entry.key_ptr.*; | 1015 | const atom_index = entry.key_ptr.*; |
| 1021 | // const relocs = entry.value_ptr.*; | 1016 | const relocs = entry.value_ptr.*; |
| 1022 | // const atom_ptr = self.atom(atom_index); | 1017 | const atom_ptr = self.atom(atom_index); |
| 1023 | // const source_sym = atom_ptr.symbol(self); | 1018 | const source_shdr = self.sections.items(.shdr)[atom_ptr.output_section_index]; |
| 1024 | // const source_shdr = self.sections.items(.shdr)[source_sym.st_shndx]; | | |
| 1025 | | | |
| 1026 | // log.debug("relocating '{?s}'", .{self.strtab.get(source_sym.st_name)}); | | |
| 1027 | | | |
| 1028 | // for (relocs.items) |*reloc| { | | |
| 1029 | // const target_sym = self.locals.items[reloc.target]; | | |
| 1030 | // const target_vaddr = target_sym.st_value + reloc.addend; | | |
| 1031 | | | |
| 1032 | // if (target_vaddr == reloc.prev_vaddr) continue; | | |
| 1033 | | | |
| 1034 | // const section_offset = (source_sym.st_value + reloc.offset) - source_shdr.sh_addr; | | |
| 1035 | // const file_offset = source_shdr.sh_offset + section_offset; | | |
| 1036 | | | |
| 1037 | // log.debug(" ({x}: [() => 0x{x}] ({?s}))", .{ | | |
| 1038 | // reloc.offset, | | |
| 1039 | // target_vaddr, | | |
| 1040 | // self.strtab.get(target_sym.st_name), | | |
| 1041 | // }); | | |
| 1042 | | | |
| 1043 | // switch (self.ptr_width) { | | |
| 1044 | // .p32 => try self.base.file.?.pwriteAll(mem.asBytes(&@as(u32, @intCast(target_vaddr))), file_offset), | | |
| 1045 | // .p64 => try self.base.file.?.pwriteAll(mem.asBytes(&target_vaddr), file_offset), | | |
| 1046 | // } | | |
| 1047 | | 1019 | |
| 1048 | // reloc.prev_vaddr = target_vaddr; | 1020 | log.debug("relocating '{s}'", .{atom_ptr.name(self)}); |
| 1049 | // } | 1021 | |
| 1050 | // } | 1022 | for (relocs.items) |*reloc| { |
| 1051 | // } | 1023 | const target_sym = self.symbol(reloc.target); |
| | 1024 | const target_vaddr = target_sym.value + reloc.addend; |
| | 1025 | |
| | 1026 | if (target_vaddr == reloc.prev_vaddr) continue; |
| | 1027 | |
| | 1028 | const section_offset = (atom_ptr.value + reloc.offset) - source_shdr.sh_addr; |
| | 1029 | const file_offset = source_shdr.sh_offset + section_offset; |
| | 1030 | |
| | 1031 | log.debug(" ({x}: [() => 0x{x}] ({s}))", .{ |
| | 1032 | reloc.offset, |
| | 1033 | target_vaddr, |
| | 1034 | target_sym.name(self), |
| | 1035 | }); |
| | 1036 | |
| | 1037 | switch (self.ptr_width) { |
| | 1038 | .p32 => try self.base.file.?.pwriteAll(mem.asBytes(&@as(u32, @intCast(target_vaddr))), file_offset), |
| | 1039 | .p64 => try self.base.file.?.pwriteAll(mem.asBytes(&target_vaddr), file_offset), |
| | 1040 | } |
| | 1041 | |
| | 1042 | reloc.prev_vaddr = target_vaddr; |
| | 1043 | } |
| | 1044 | } |
| | 1045 | } |
| 1052 | | 1046 | |
| 1053 | // try self.writeSymbols(); | 1047 | // try self.writeSymbols(); |
| 1054 | | 1048 | |
| 1055 | // if (build_options.enable_logging) { | 1049 | if (build_options.enable_logging) { |
| 1056 | // self.logSymtab(); | 1050 | state_log.debug("{}", .{self.dumpState()}); |
| 1057 | // } | 1051 | } |
| 1058 | | 1052 | |
| 1059 | // if (self.dwarf) |*dw| { | 1053 | // if (self.dwarf) |*dw| { |
| 1060 | // if (self.debug_abbrev_section_dirty) { | 1054 | // if (self.debug_abbrev_section_dirty) { |
| ... | @@ -2211,6 +2205,7 @@ fn updateDeclCode( | ... | @@ -2211,6 +2205,7 @@ fn updateDeclCode( |
| 2211 | const shdr_index = sym.output_section_index; | 2205 | const shdr_index = sym.output_section_index; |
| 2212 | | 2206 | |
| 2213 | sym.name_offset = try self.strtab.insert(gpa, decl_name); | 2207 | sym.name_offset = try self.strtab.insert(gpa, decl_name); |
| | 2208 | atom_ptr.name_offset = sym.name_offset; |
| 2214 | esym.st_name = sym.name_offset; | 2209 | esym.st_name = sym.name_offset; |
| 2215 | esym.st_info |= stt_bits; | 2210 | esym.st_info |= stt_bits; |
| 2216 | esym.st_size = code.len; | 2211 | esym.st_size = code.len; |
| ... | @@ -2466,6 +2461,7 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol. | ... | @@ -2466,6 +2461,7 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol. |
| 2466 | local_esym.st_info |= elf.STT_OBJECT; | 2461 | local_esym.st_info |= elf.STT_OBJECT; |
| 2467 | local_esym.st_size = code.len; | 2462 | local_esym.st_size = code.len; |
| 2468 | const atom_ptr = local_sym.atom(self).?; | 2463 | const atom_ptr = local_sym.atom(self).?; |
| | 2464 | atom_ptr.name_offset = name_str_index; |
| 2469 | atom_ptr.alignment = math.log2_int(u64, required_alignment); | 2465 | atom_ptr.alignment = math.log2_int(u64, required_alignment); |
| 2470 | atom_ptr.size = code.len; | 2466 | atom_ptr.size = code.len; |
| 2471 | | 2467 | |
| ... | @@ -2540,6 +2536,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module | ... | @@ -2540,6 +2536,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2540 | local_esym.st_info |= elf.STT_OBJECT; | 2536 | local_esym.st_info |= elf.STT_OBJECT; |
| 2541 | local_esym.st_size = code.len; | 2537 | local_esym.st_size = code.len; |
| 2542 | const atom_ptr = local_sym.atom(self).?; | 2538 | const atom_ptr = local_sym.atom(self).?; |
| | 2539 | atom_ptr.name_offset = name_str_index; |
| 2543 | atom_ptr.alignment = math.log2_int(u64, required_alignment); | 2540 | atom_ptr.alignment = math.log2_int(u64, required_alignment); |
| 2544 | atom_ptr.size = code.len; | 2541 | atom_ptr.size = code.len; |
| 2545 | | 2542 | |
| ... | @@ -3290,6 +3287,7 @@ const assert = std.debug.assert; | ... | @@ -3290,6 +3287,7 @@ const assert = std.debug.assert; |
| 3290 | const elf = std.elf; | 3287 | const elf = std.elf; |
| 3291 | const fs = std.fs; | 3288 | const fs = std.fs; |
| 3292 | const log = std.log.scoped(.link); | 3289 | const log = std.log.scoped(.link); |
| | 3290 | const state_log = std.log.scoped(.link_state); |
| 3293 | const math = std.math; | 3291 | const math = std.math; |
| 3294 | const mem = std.mem; | 3292 | const mem = std.mem; |
| 3295 | | 3293 | |