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