| ... | ... | @@ -1,100 +1,4 @@ |
| 1 | | const Elf = @This(); |
| 2 | | |
| 3 | | const std = @import("std"); |
| 4 | | const build_options = @import("build_options"); |
| 5 | | const builtin = @import("builtin"); |
| 6 | | const assert = std.debug.assert; |
| 7 | | const elf = std.elf; |
| 8 | | const fs = std.fs; |
| 9 | | const log = std.log.scoped(.link); |
| 10 | | const math = std.math; |
| 11 | | const mem = std.mem; |
| 12 | | |
| 13 | | const codegen = @import("../codegen.zig"); |
| 14 | | const glibc = @import("../glibc.zig"); |
| 15 | | const link = @import("../link.zig"); |
| 16 | | const lldMain = @import("../main.zig").lldMain; |
| 17 | | const musl = @import("../musl.zig"); |
| 18 | | const target_util = @import("../target.zig"); |
| 19 | | const trace = @import("../tracy.zig").trace; |
| 20 | | |
| 21 | | const Air = @import("../Air.zig"); |
| 22 | | const Allocator = std.mem.Allocator; |
| 23 | | pub const Atom = @import("Elf/Atom.zig"); |
| 24 | | const Cache = std.Build.Cache; |
| 25 | | const Compilation = @import("../Compilation.zig"); |
| 26 | | const Dwarf = @import("Dwarf.zig"); |
| 27 | | const File = link.File; |
| 28 | | const Liveness = @import("../Liveness.zig"); |
| 29 | | const LlvmObject = @import("../codegen/llvm.zig").Object; |
| 30 | | const Module = @import("../Module.zig"); |
| 31 | | const InternPool = @import("../InternPool.zig"); |
| 32 | | const Package = @import("../Package.zig"); |
| 33 | | const StringTable = @import("strtab.zig").StringTable; |
| 34 | | const TableSection = @import("table_section.zig").TableSection; |
| 35 | | const Type = @import("../type.zig").Type; |
| 36 | | const TypedValue = @import("../TypedValue.zig"); |
| 37 | | const Value = @import("../value.zig").Value; |
| 38 | | |
| 39 | | const default_entry_addr = 0x8000000; |
| 40 | | |
| 41 | | pub const base_tag: File.Tag = .elf; |
| 42 | | |
| 43 | | const Section = struct { |
| 44 | | shdr: elf.Elf64_Shdr, |
| 45 | | phdr_index: u16, |
| 46 | | |
| 47 | | /// Index of the last allocated atom in this section. |
| 48 | | last_atom_index: ?Atom.Index = null, |
| 49 | | |
| 50 | | /// A list of atoms that have surplus capacity. This list can have false |
| 51 | | /// positives, as functions grow and shrink over time, only sometimes being added |
| 52 | | /// or removed from the freelist. |
| 53 | | /// |
| 54 | | /// An atom has surplus capacity when its overcapacity value is greater than |
| 55 | | /// padToIdeal(minimum_atom_size). That is, when it has so |
| 56 | | /// much extra capacity, that we could fit a small new symbol in it, itself with |
| 57 | | /// ideal_capacity or more. |
| 58 | | /// |
| 59 | | /// Ideal capacity is defined by size + (size / ideal_factor) |
| 60 | | /// |
| 61 | | /// Overcapacity is measured by actual_capacity - ideal_capacity. Note that |
| 62 | | /// overcapacity can be negative. A simple way to have negative overcapacity is to |
| 63 | | /// allocate a fresh text block, which will have ideal capacity, and then grow it |
| 64 | | /// by 1 byte. It will then have -1 overcapacity. |
| 65 | | free_list: std.ArrayListUnmanaged(Atom.Index) = .{}, |
| 66 | | }; |
| 67 | | |
| 68 | | const LazySymbolMetadata = struct { |
| 69 | | const State = enum { unused, pending_flush, flushed }; |
| 70 | | text_atom: Atom.Index = undefined, |
| 71 | | rodata_atom: Atom.Index = undefined, |
| 72 | | text_state: State = .unused, |
| 73 | | rodata_state: State = .unused, |
| 74 | | }; |
| 75 | | |
| 76 | | const DeclMetadata = struct { |
| 77 | | atom: Atom.Index, |
| 78 | | shdr: u16, |
| 79 | | /// A list of all exports aliases of this Decl. |
| 80 | | exports: std.ArrayListUnmanaged(u32) = .{}, |
| 81 | | |
| 82 | | fn getExport(m: DeclMetadata, elf_file: *const Elf, name: []const u8) ?u32 { |
| 83 | | for (m.exports.items) |exp| { |
| 84 | | if (mem.eql(u8, name, elf_file.getGlobalName(exp))) return exp; |
| 85 | | } |
| 86 | | return null; |
| 87 | | } |
| 88 | | |
| 89 | | fn getExportPtr(m: *DeclMetadata, elf_file: *Elf, name: []const u8) ?*u32 { |
| 90 | | for (m.exports.items) |*exp| { |
| 91 | | if (mem.eql(u8, name, elf_file.getGlobalName(exp.*))) return exp; |
| 92 | | } |
| 93 | | return null; |
| 94 | | } |
| 95 | | }; |
| 96 | | |
| 97 | | base: File, |
| 1 | base: link.File, |
| 98 | 2 | dwarf: ?Dwarf = null, |
| 99 | 3 | |
| 100 | 4 | ptr_width: PtrWidth, |
| ... | ... | @@ -102,14 +6,25 @@ ptr_width: PtrWidth, |
| 102 | 6 | /// If this is not null, an object file is created by LLVM and linked with LLD afterwards. |
| 103 | 7 | llvm_object: ?*LlvmObject = null, |
| 104 | 8 | |
| 9 | /// A list of all input files. |
| 10 | /// Index of each input file also encodes the priority or precedence of one input file |
| 11 | /// over another. |
| 12 | files: std.MultiArrayList(File.Entry) = .{}, |
| 13 | zig_module_index: ?File.Index = null, |
| 14 | linker_defined_index: ?File.Index = null, |
| 15 | objects: std.ArrayListUnmanaged(File.Index) = .{}, |
| 16 | |
| 105 | 17 | /// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write. |
| 106 | 18 | /// Same order as in the file. |
| 107 | | sections: std.MultiArrayList(Section) = .{}, |
| 19 | shdrs: std.ArrayListUnmanaged(elf.Elf64_Shdr) = .{}, |
| 20 | /// Given index to a section, pulls index of containing phdr if any. |
| 21 | phdr_to_shdr_table: std.AutoHashMapUnmanaged(u16, u16) = .{}, |
| 22 | /// File offset into the shdr table. |
| 108 | 23 | shdr_table_offset: ?u64 = null, |
| 109 | 24 | |
| 110 | 25 | /// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write. |
| 111 | 26 | /// Same order as in the file. |
| 112 | | program_headers: std.ArrayListUnmanaged(elf.Elf64_Phdr) = .{}, |
| 27 | phdrs: std.ArrayListUnmanaged(elf.Elf64_Phdr) = .{}, |
| 113 | 28 | /// The index into the program headers of the PT_PHDR program header |
| 114 | 29 | phdr_table_index: ?u16 = null, |
| 115 | 30 | /// The index into the program headers of the PT_LOAD program header containing the phdr |
| ... | ... | @@ -128,17 +43,26 @@ phdr_load_rw_index: ?u16 = null, |
| 128 | 43 | |
| 129 | 44 | entry_addr: ?u64 = null, |
| 130 | 45 | page_size: u32, |
| 46 | default_sym_version: elf.Elf64_Versym, |
| 131 | 47 | |
| 132 | 48 | /// .shstrtab buffer |
| 133 | 49 | shstrtab: StringTable(.strtab) = .{}, |
| 134 | 50 | /// .strtab buffer |
| 135 | 51 | strtab: StringTable(.strtab) = .{}, |
| 136 | 52 | |
| 137 | | symtab_section_index: ?u16 = null, |
| 53 | /// Representation of the GOT table as committed to the file. |
| 54 | got: GotSection = .{}, |
| 55 | |
| 138 | 56 | text_section_index: ?u16 = null, |
| 139 | 57 | rodata_section_index: ?u16 = null, |
| 140 | | got_section_index: ?u16 = null, |
| 141 | 58 | data_section_index: ?u16 = null, |
| 59 | eh_frame_section_index: ?u16 = null, |
| 60 | eh_frame_hdr_section_index: ?u16 = null, |
| 61 | dynamic_section_index: ?u16 = null, |
| 62 | got_section_index: ?u16 = null, |
| 63 | got_plt_section_index: ?u16 = null, |
| 64 | plt_section_index: ?u16 = null, |
| 65 | rela_dyn_section_index: ?u16 = null, |
| 142 | 66 | debug_info_section_index: ?u16 = null, |
| 143 | 67 | debug_abbrev_section_index: ?u16 = null, |
| 144 | 68 | debug_str_section_index: ?u16 = null, |
| ... | ... | @@ -146,24 +70,36 @@ debug_aranges_section_index: ?u16 = null, |
| 146 | 70 | debug_line_section_index: ?u16 = null, |
| 147 | 71 | shstrtab_section_index: ?u16 = null, |
| 148 | 72 | strtab_section_index: ?u16 = null, |
| 73 | symtab_section_index: ?u16 = null, |
| 149 | 74 | |
| 150 | | /// The same order as in the file. ELF requires global symbols to all be after the |
| 151 | | /// local symbols, they cannot be mixed. So we must buffer all the global symbols and |
| 152 | | /// write them at the end. These are only the local symbols. The length of this array |
| 153 | | /// is the value used for sh_info in the .symtab section. |
| 154 | | local_symbols: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{}, |
| 155 | | global_symbols: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{}, |
| 156 | | |
| 157 | | local_symbol_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| 158 | | global_symbol_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| 159 | | |
| 160 | | got_table: TableSection(u32) = .{}, |
| 75 | // Linker-defined symbols |
| 76 | dynamic_index: ?Symbol.Index = null, |
| 77 | ehdr_start_index: ?Symbol.Index = null, |
| 78 | init_array_start_index: ?Symbol.Index = null, |
| 79 | init_array_end_index: ?Symbol.Index = null, |
| 80 | fini_array_start_index: ?Symbol.Index = null, |
| 81 | fini_array_end_index: ?Symbol.Index = null, |
| 82 | preinit_array_start_index: ?Symbol.Index = null, |
| 83 | preinit_array_end_index: ?Symbol.Index = null, |
| 84 | got_index: ?Symbol.Index = null, |
| 85 | plt_index: ?Symbol.Index = null, |
| 86 | end_index: ?Symbol.Index = null, |
| 87 | gnu_eh_frame_hdr_index: ?Symbol.Index = null, |
| 88 | dso_handle_index: ?Symbol.Index = null, |
| 89 | rela_iplt_start_index: ?Symbol.Index = null, |
| 90 | rela_iplt_end_index: ?Symbol.Index = null, |
| 91 | start_stop_indexes: std.ArrayListUnmanaged(u32) = .{}, |
| 92 | |
| 93 | /// An array of symbols parsed across all input files. |
| 94 | symbols: std.ArrayListUnmanaged(Symbol) = .{}, |
| 95 | symbols_extra: std.ArrayListUnmanaged(u32) = .{}, |
| 96 | resolver: std.AutoArrayHashMapUnmanaged(u32, Symbol.Index) = .{}, |
| 97 | symbols_free_list: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 161 | 98 | |
| 162 | 99 | phdr_table_dirty: bool = false, |
| 163 | 100 | shdr_table_dirty: bool = false, |
| 164 | 101 | shstrtab_dirty: bool = false, |
| 165 | 102 | strtab_dirty: bool = false, |
| 166 | | got_table_count_dirty: bool = false, |
| 167 | 103 | |
| 168 | 104 | debug_strtab_dirty: bool = false, |
| 169 | 105 | debug_abbrev_section_dirty: bool = false, |
| ... | ... | @@ -171,7 +107,8 @@ debug_aranges_section_dirty: bool = false, |
| 171 | 107 | debug_info_header_dirty: bool = false, |
| 172 | 108 | debug_line_header_dirty: bool = false, |
| 173 | 109 | |
| 174 | | error_flags: File.ErrorFlags = File.ErrorFlags{}, |
| 110 | error_flags: link.File.ErrorFlags = link.File.ErrorFlags{}, |
| 111 | misc_errors: std.ArrayListUnmanaged(link.File.ErrorMsg) = .{}, |
| 175 | 112 | |
| 176 | 113 | /// Table of tracked LazySymbols. |
| 177 | 114 | lazy_syms: LazySymbolTable = .{}, |
| ... | ... | @@ -181,9 +118,8 @@ decls: std.AutoHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{}, |
| 181 | 118 | |
| 182 | 119 | /// List of atoms that are owned directly by the linker. |
| 183 | 120 | atoms: std.ArrayListUnmanaged(Atom) = .{}, |
| 184 | | |
| 185 | | /// Table of atoms indexed by the symbol index. |
| 186 | | atom_by_index_table: std.AutoHashMapUnmanaged(u32, Atom.Index) = .{}, |
| 121 | /// Table of last atom index in a section and matching atom free list if any. |
| 122 | last_atom_and_free_list_table: std.AutoArrayHashMapUnmanaged(u16, LastAtomAndFreeList) = .{}, |
| 187 | 123 | |
| 188 | 124 | /// Table of unnamed constants associated with a parent `Decl`. |
| 189 | 125 | /// We store them here so that we can free the constants whenever the `Decl` |
| ... | ... | @@ -204,15 +140,13 @@ atom_by_index_table: std.AutoHashMapUnmanaged(u32, Atom.Index) = .{}, |
| 204 | 140 | /// |
| 205 | 141 | /// value assigned to label `foo` is an unnamed constant belonging/associated |
| 206 | 142 | /// with `Decl` `main`, and lives as long as that `Decl`. |
| 207 | | unnamed_const_atoms: UnnamedConstTable = .{}, |
| 143 | unnamed_consts: UnnamedConstTable = .{}, |
| 208 | 144 | |
| 209 | | /// A table of relocations indexed by the owning them `TextBlock`. |
| 210 | | /// Note that once we refactor `TextBlock`'s lifetime and ownership rules, |
| 211 | | /// this will be a table indexed by index into the list of Atoms. |
| 212 | | relocs: RelocTable = .{}, |
| 145 | comdat_groups: std.ArrayListUnmanaged(ComdatGroup) = .{}, |
| 146 | comdat_groups_owners: std.ArrayListUnmanaged(ComdatGroupOwner) = .{}, |
| 147 | comdat_groups_table: std.AutoHashMapUnmanaged(u32, ComdatGroupOwner.Index) = .{}, |
| 213 | 148 | |
| 214 | | const RelocTable = std.AutoHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Atom.Reloc)); |
| 215 | | const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Atom.Index)); |
| 149 | const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Symbol.Index)); |
| 216 | 150 | const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, LazySymbolMetadata); |
| 217 | 151 | |
| 218 | 152 | /// When allocating, the ideal_capacity is calculated by |
| ... | ... | @@ -237,40 +171,34 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 237 | 171 | const self = try createEmpty(allocator, options); |
| 238 | 172 | errdefer self.base.destroy(); |
| 239 | 173 | |
| 240 | | const file = try options.emit.?.directory.handle.createFile(sub_path, .{ |
| 174 | self.base.file = try options.emit.?.directory.handle.createFile(sub_path, .{ |
| 241 | 175 | .truncate = false, |
| 242 | 176 | .read = true, |
| 243 | 177 | .mode = link.determineMode(options), |
| 244 | 178 | }); |
| 245 | 179 | |
| 246 | | self.base.file = file; |
| 247 | 180 | self.shdr_table_dirty = true; |
| 248 | 181 | |
| 249 | 182 | // Index 0 is always a null symbol. |
| 250 | | try self.local_symbols.append(allocator, .{ |
| 251 | | .st_name = 0, |
| 252 | | .st_info = 0, |
| 253 | | .st_other = 0, |
| 254 | | .st_shndx = 0, |
| 255 | | .st_value = 0, |
| 256 | | .st_size = 0, |
| 257 | | }); |
| 258 | | |
| 259 | | // There must always be a null section in index 0 |
| 260 | | try self.sections.append(allocator, .{ |
| 261 | | .shdr = .{ |
| 262 | | .sh_name = 0, |
| 263 | | .sh_type = elf.SHT_NULL, |
| 264 | | .sh_flags = 0, |
| 265 | | .sh_addr = 0, |
| 266 | | .sh_offset = 0, |
| 267 | | .sh_size = 0, |
| 268 | | .sh_link = 0, |
| 269 | | .sh_info = 0, |
| 270 | | .sh_addralign = 0, |
| 271 | | .sh_entsize = 0, |
| 272 | | }, |
| 273 | | .phdr_index = undefined, |
| 183 | try self.symbols.append(allocator, .{}); |
| 184 | // Index 0 is always a null symbol. |
| 185 | try self.symbols_extra.append(allocator, 0); |
| 186 | // Allocate atom index 0 to null atom |
| 187 | try self.atoms.append(allocator, .{}); |
| 188 | // Append null file at index 0 |
| 189 | try self.files.append(allocator, .null); |
| 190 | // There must always be a null shdr in index 0 |
| 191 | try self.shdrs.append(allocator, .{ |
| 192 | .sh_name = 0, |
| 193 | .sh_type = elf.SHT_NULL, |
| 194 | .sh_flags = 0, |
| 195 | .sh_addr = 0, |
| 196 | .sh_offset = 0, |
| 197 | .sh_size = 0, |
| 198 | .sh_link = 0, |
| 199 | .sh_info = 0, |
| 200 | .sh_addralign = 0, |
| 201 | .sh_entsize = 0, |
| 274 | 202 | }); |
| 275 | 203 | |
| 276 | 204 | try self.populateMissingMetadata(); |
| ... | ... | @@ -292,6 +220,10 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Elf { |
| 292 | 220 | .sparc64 => 0x2000, |
| 293 | 221 | else => 0x1000, |
| 294 | 222 | }; |
| 223 | const default_sym_version: elf.Elf64_Versym = if (options.output_mode == .Lib and options.link_mode == .Dynamic) |
| 224 | elf.VER_NDX_GLOBAL |
| 225 | else |
| 226 | elf.VER_NDX_LOCAL; |
| 295 | 227 | |
| 296 | 228 | var dwarf: ?Dwarf = if (!options.strip and options.module != null) |
| 297 | 229 | Dwarf.init(gpa, &self.base, options.target) |
| ... | ... | @@ -308,11 +240,13 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Elf { |
| 308 | 240 | .dwarf = dwarf, |
| 309 | 241 | .ptr_width = ptr_width, |
| 310 | 242 | .page_size = page_size, |
| 243 | .default_sym_version = default_sym_version, |
| 311 | 244 | }; |
| 312 | 245 | const use_llvm = options.use_llvm; |
| 313 | 246 | if (use_llvm) { |
| 314 | 247 | self.llvm_object = try LlvmObject.create(gpa, options); |
| 315 | 248 | } |
| 249 | |
| 316 | 250 | return self; |
| 317 | 251 | } |
| 318 | 252 | |
| ... | ... | @@ -321,19 +255,27 @@ pub fn deinit(self: *Elf) void { |
| 321 | 255 | |
| 322 | 256 | if (self.llvm_object) |llvm_object| llvm_object.destroy(gpa); |
| 323 | 257 | |
| 324 | | for (self.sections.items(.free_list)) |*free_list| { |
| 325 | | free_list.deinit(gpa); |
| 326 | | } |
| 327 | | self.sections.deinit(gpa); |
| 258 | for (self.files.items(.tags), self.files.items(.data)) |tag, *data| switch (tag) { |
| 259 | .null => {}, |
| 260 | .zig_module => data.zig_module.deinit(gpa), |
| 261 | .linker_defined => data.linker_defined.deinit(gpa), |
| 262 | .object => data.object.deinit(gpa), |
| 263 | // .shared_object => data.shared_object.deinit(gpa), |
| 264 | }; |
| 265 | self.files.deinit(gpa); |
| 266 | self.objects.deinit(gpa); |
| 328 | 267 | |
| 329 | | self.program_headers.deinit(gpa); |
| 268 | self.shdrs.deinit(gpa); |
| 269 | self.phdr_to_shdr_table.deinit(gpa); |
| 270 | self.phdrs.deinit(gpa); |
| 330 | 271 | self.shstrtab.deinit(gpa); |
| 331 | 272 | self.strtab.deinit(gpa); |
| 332 | | self.local_symbols.deinit(gpa); |
| 333 | | self.global_symbols.deinit(gpa); |
| 334 | | self.global_symbol_free_list.deinit(gpa); |
| 335 | | self.local_symbol_free_list.deinit(gpa); |
| 336 | | self.got_table.deinit(gpa); |
| 273 | self.symbols.deinit(gpa); |
| 274 | self.symbols_extra.deinit(gpa); |
| 275 | self.symbols_free_list.deinit(gpa); |
| 276 | self.got.deinit(gpa); |
| 277 | self.resolver.deinit(gpa); |
| 278 | self.start_stop_indexes.deinit(gpa); |
| 337 | 279 | |
| 338 | 280 | { |
| 339 | 281 | var it = self.decls.iterator(); |
| ... | ... | @@ -344,43 +286,41 @@ pub fn deinit(self: *Elf) void { |
| 344 | 286 | } |
| 345 | 287 | |
| 346 | 288 | self.atoms.deinit(gpa); |
| 347 | | self.atom_by_index_table.deinit(gpa); |
| 348 | | self.lazy_syms.deinit(gpa); |
| 349 | | |
| 350 | | { |
| 351 | | var it = self.unnamed_const_atoms.valueIterator(); |
| 352 | | while (it.next()) |atoms| { |
| 353 | | atoms.deinit(gpa); |
| 354 | | } |
| 355 | | self.unnamed_const_atoms.deinit(gpa); |
| 289 | for (self.last_atom_and_free_list_table.values()) |*value| { |
| 290 | value.free_list.deinit(gpa); |
| 356 | 291 | } |
| 292 | self.last_atom_and_free_list_table.deinit(gpa); |
| 293 | self.lazy_syms.deinit(gpa); |
| 357 | 294 | |
| 358 | 295 | { |
| 359 | | var it = self.relocs.valueIterator(); |
| 360 | | while (it.next()) |relocs| { |
| 361 | | relocs.deinit(gpa); |
| 296 | var it = self.unnamed_consts.valueIterator(); |
| 297 | while (it.next()) |syms| { |
| 298 | syms.deinit(gpa); |
| 362 | 299 | } |
| 363 | | self.relocs.deinit(gpa); |
| 300 | self.unnamed_consts.deinit(gpa); |
| 364 | 301 | } |
| 365 | 302 | |
| 366 | 303 | if (self.dwarf) |*dw| { |
| 367 | 304 | dw.deinit(); |
| 368 | 305 | } |
| 306 | |
| 307 | self.misc_errors.deinit(gpa); |
| 308 | self.comdat_groups.deinit(gpa); |
| 309 | self.comdat_groups_owners.deinit(gpa); |
| 310 | self.comdat_groups_table.deinit(gpa); |
| 369 | 311 | } |
| 370 | 312 | |
| 371 | | pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: File.RelocInfo) !u64 { |
| 313 | pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: link.File.RelocInfo) !u64 { |
| 372 | 314 | assert(self.llvm_object == null); |
| 373 | 315 | |
| 374 | | const this_atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 375 | | const this_atom = self.getAtom(this_atom_index); |
| 376 | | const target = this_atom.getSymbolIndex().?; |
| 377 | | const vaddr = this_atom.getSymbol(self).st_value; |
| 378 | | const atom_index = self.getAtomIndexForSymbol(reloc_info.parent_atom_index).?; |
| 379 | | try Atom.addRelocation(self, atom_index, .{ |
| 380 | | .target = target, |
| 381 | | .offset = reloc_info.offset, |
| 382 | | .addend = reloc_info.addend, |
| 383 | | .prev_vaddr = vaddr, |
| 316 | const this_sym_index = try self.getOrCreateMetadataForDecl(decl_index); |
| 317 | const this_sym = self.symbol(this_sym_index); |
| 318 | const vaddr = this_sym.value; |
| 319 | const parent_atom = self.symbol(reloc_info.parent_atom_index).atom(self).?; |
| 320 | try parent_atom.addReloc(self, .{ |
| 321 | .r_offset = reloc_info.offset, |
| 322 | .r_info = (@as(u64, @intCast(this_sym.esym_index)) << 32) | elf.R_X86_64_64, |
| 323 | .r_addend = reloc_info.addend, |
| 384 | 324 | }); |
| 385 | 325 | |
| 386 | 326 | return vaddr; |
| ... | ... | @@ -397,7 +337,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 { |
| 397 | 337 | |
| 398 | 338 | if (self.shdr_table_offset) |off| { |
| 399 | 339 | const shdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Shdr) else @sizeOf(elf.Elf64_Shdr); |
| 400 | | const tight_size = self.sections.slice().len * shdr_size; |
| 340 | const tight_size = self.shdrs.items.len * shdr_size; |
| 401 | 341 | const increased_size = padToIdeal(tight_size); |
| 402 | 342 | const test_end = off + increased_size; |
| 403 | 343 | if (end > off and start < test_end) { |
| ... | ... | @@ -405,17 +345,17 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 { |
| 405 | 345 | } |
| 406 | 346 | } |
| 407 | 347 | |
| 408 | | for (self.sections.items(.shdr)) |section| { |
| 348 | for (self.shdrs.items) |section| { |
| 409 | 349 | const increased_size = padToIdeal(section.sh_size); |
| 410 | 350 | const test_end = section.sh_offset + increased_size; |
| 411 | 351 | if (end > section.sh_offset and start < test_end) { |
| 412 | 352 | return test_end; |
| 413 | 353 | } |
| 414 | 354 | } |
| 415 | | for (self.program_headers.items) |program_header| { |
| 416 | | const increased_size = padToIdeal(program_header.p_filesz); |
| 417 | | const test_end = program_header.p_offset + increased_size; |
| 418 | | if (end > program_header.p_offset and start < test_end) { |
| 355 | for (self.phdrs.items) |phdr| { |
| 356 | const increased_size = padToIdeal(phdr.p_filesz); |
| 357 | const test_end = phdr.p_offset + increased_size; |
| 358 | if (end > phdr.p_offset and start < test_end) { |
| 419 | 359 | return test_end; |
| 420 | 360 | } |
| 421 | 361 | } |
| ... | ... | @@ -429,13 +369,13 @@ pub fn allocatedSize(self: *Elf, start: u64) u64 { |
| 429 | 369 | if (self.shdr_table_offset) |off| { |
| 430 | 370 | if (off > start and off < min_pos) min_pos = off; |
| 431 | 371 | } |
| 432 | | for (self.sections.items(.shdr)) |section| { |
| 372 | for (self.shdrs.items) |section| { |
| 433 | 373 | if (section.sh_offset <= start) continue; |
| 434 | 374 | if (section.sh_offset < min_pos) min_pos = section.sh_offset; |
| 435 | 375 | } |
| 436 | | for (self.program_headers.items) |program_header| { |
| 437 | | if (program_header.p_offset <= start) continue; |
| 438 | | if (program_header.p_offset < min_pos) min_pos = program_header.p_offset; |
| 376 | for (self.phdrs.items) |phdr| { |
| 377 | if (phdr.p_offset <= start) continue; |
| 378 | if (phdr.p_offset < min_pos) min_pos = phdr.p_offset; |
| 439 | 379 | } |
| 440 | 380 | return min_pos - start; |
| 441 | 381 | } |
| ... | ... | @@ -457,19 +397,20 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 457 | 397 | .p64 => false, |
| 458 | 398 | }; |
| 459 | 399 | const ptr_size: u8 = self.ptrWidthBytes(); |
| 400 | const image_base = self.calcImageBase(); |
| 460 | 401 | |
| 461 | 402 | if (self.phdr_table_index == null) { |
| 462 | | self.phdr_table_index = @as(u16, @intCast(self.program_headers.items.len)); |
| 403 | self.phdr_table_index = @as(u16, @intCast(self.phdrs.items.len)); |
| 463 | 404 | const p_align: u16 = switch (self.ptr_width) { |
| 464 | 405 | .p32 => @alignOf(elf.Elf32_Phdr), |
| 465 | 406 | .p64 => @alignOf(elf.Elf64_Phdr), |
| 466 | 407 | }; |
| 467 | | try self.program_headers.append(gpa, .{ |
| 408 | try self.phdrs.append(gpa, .{ |
| 468 | 409 | .p_type = elf.PT_PHDR, |
| 469 | 410 | .p_offset = 0, |
| 470 | 411 | .p_filesz = 0, |
| 471 | | .p_vaddr = 0, |
| 472 | | .p_paddr = 0, |
| 412 | .p_vaddr = image_base, |
| 413 | .p_paddr = image_base, |
| 473 | 414 | .p_memsz = 0, |
| 474 | 415 | .p_align = p_align, |
| 475 | 416 | .p_flags = elf.PF_R, |
| ... | ... | @@ -478,31 +419,29 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 478 | 419 | } |
| 479 | 420 | |
| 480 | 421 | if (self.phdr_table_load_index == null) { |
| 481 | | self.phdr_table_load_index = @as(u16, @intCast(self.program_headers.items.len)); |
| 422 | self.phdr_table_load_index = @as(u16, @intCast(self.phdrs.items.len)); |
| 482 | 423 | // TODO Same as for GOT |
| 483 | | const phdr_addr: u64 = if (self.base.options.target.ptrBitWidth() >= 32) 0x1000000 else 0x1000; |
| 484 | | const p_align = self.page_size; |
| 485 | | try self.program_headers.append(gpa, .{ |
| 424 | try self.phdrs.append(gpa, .{ |
| 486 | 425 | .p_type = elf.PT_LOAD, |
| 487 | 426 | .p_offset = 0, |
| 488 | 427 | .p_filesz = 0, |
| 489 | | .p_vaddr = phdr_addr, |
| 490 | | .p_paddr = phdr_addr, |
| 428 | .p_vaddr = image_base, |
| 429 | .p_paddr = image_base, |
| 491 | 430 | .p_memsz = 0, |
| 492 | | .p_align = p_align, |
| 431 | .p_align = self.page_size, |
| 493 | 432 | .p_flags = elf.PF_R, |
| 494 | 433 | }); |
| 495 | 434 | self.phdr_table_dirty = true; |
| 496 | 435 | } |
| 497 | 436 | |
| 498 | 437 | if (self.phdr_load_re_index == null) { |
| 499 | | self.phdr_load_re_index = @as(u16, @intCast(self.program_headers.items.len)); |
| 438 | self.phdr_load_re_index = @as(u16, @intCast(self.phdrs.items.len)); |
| 500 | 439 | const file_size = self.base.options.program_code_size_hint; |
| 501 | 440 | const p_align = self.page_size; |
| 502 | 441 | const off = self.findFreeSpace(file_size, p_align); |
| 503 | 442 | log.debug("found PT_LOAD RE free space 0x{x} to 0x{x}", .{ off, off + file_size }); |
| 504 | | const entry_addr: u64 = self.entry_addr orelse if (self.base.options.target.cpu.arch == .spu_2) @as(u64, 0) else default_entry_addr; |
| 505 | | try self.program_headers.append(gpa, .{ |
| 443 | const entry_addr = self.defaultEntryAddress(); |
| 444 | try self.phdrs.append(gpa, .{ |
| 506 | 445 | .p_type = elf.PT_LOAD, |
| 507 | 446 | .p_offset = off, |
| 508 | 447 | .p_filesz = file_size, |
| ... | ... | @@ -517,7 +456,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 517 | 456 | } |
| 518 | 457 | |
| 519 | 458 | if (self.phdr_got_index == null) { |
| 520 | | self.phdr_got_index = @as(u16, @intCast(self.program_headers.items.len)); |
| 459 | self.phdr_got_index = @as(u16, @intCast(self.phdrs.items.len)); |
| 521 | 460 | const file_size = @as(u64, ptr_size) * self.base.options.symbol_count_hint; |
| 522 | 461 | // We really only need ptr alignment but since we are using PROGBITS, linux requires |
| 523 | 462 | // page align. |
| ... | ... | @@ -528,7 +467,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 528 | 467 | // we'll need to re-use that function anyway, in case the GOT grows and overlaps something |
| 529 | 468 | // else in virtual memory. |
| 530 | 469 | const got_addr: u32 = if (self.base.options.target.ptrBitWidth() >= 32) 0x4000000 else 0x8000; |
| 531 | | try self.program_headers.append(gpa, .{ |
| 470 | try self.phdrs.append(gpa, .{ |
| 532 | 471 | .p_type = elf.PT_LOAD, |
| 533 | 472 | .p_offset = off, |
| 534 | 473 | .p_filesz = file_size, |
| ... | ... | @@ -542,7 +481,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 542 | 481 | } |
| 543 | 482 | |
| 544 | 483 | if (self.phdr_load_ro_index == null) { |
| 545 | | self.phdr_load_ro_index = @as(u16, @intCast(self.program_headers.items.len)); |
| 484 | self.phdr_load_ro_index = @as(u16, @intCast(self.phdrs.items.len)); |
| 546 | 485 | // TODO Find a hint about how much data need to be in rodata ? |
| 547 | 486 | const file_size = 1024; |
| 548 | 487 | // Same reason as for GOT |
| ... | ... | @@ -551,7 +490,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 551 | 490 | log.debug("found PT_LOAD RO free space 0x{x} to 0x{x}", .{ off, off + file_size }); |
| 552 | 491 | // TODO Same as for GOT |
| 553 | 492 | const rodata_addr: u32 = if (self.base.options.target.ptrBitWidth() >= 32) 0xc000000 else 0xa000; |
| 554 | | try self.program_headers.append(gpa, .{ |
| 493 | try self.phdrs.append(gpa, .{ |
| 555 | 494 | .p_type = elf.PT_LOAD, |
| 556 | 495 | .p_offset = off, |
| 557 | 496 | .p_filesz = file_size, |
| ... | ... | @@ -565,7 +504,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 565 | 504 | } |
| 566 | 505 | |
| 567 | 506 | if (self.phdr_load_rw_index == null) { |
| 568 | | self.phdr_load_rw_index = @as(u16, @intCast(self.program_headers.items.len)); |
| 507 | self.phdr_load_rw_index = @as(u16, @intCast(self.phdrs.items.len)); |
| 569 | 508 | // TODO Find a hint about how much data need to be in data ? |
| 570 | 509 | const file_size = 1024; |
| 571 | 510 | // Same reason as for GOT |
| ... | ... | @@ -574,7 +513,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 574 | 513 | log.debug("found PT_LOAD RW free space 0x{x} to 0x{x}", .{ off, off + file_size }); |
| 575 | 514 | // TODO Same as for GOT |
| 576 | 515 | const rwdata_addr: u32 = if (self.base.options.target.ptrBitWidth() >= 32) 0x10000000 else 0xc000; |
| 577 | | try self.program_headers.append(gpa, .{ |
| 516 | try self.phdrs.append(gpa, .{ |
| 578 | 517 | .p_type = elf.PT_LOAD, |
| 579 | 518 | .p_offset = off, |
| 580 | 519 | .p_filesz = file_size, |
| ... | ... | @@ -588,197 +527,174 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 588 | 527 | } |
| 589 | 528 | |
| 590 | 529 | if (self.shstrtab_section_index == null) { |
| 591 | | self.shstrtab_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 530 | self.shstrtab_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 592 | 531 | assert(self.shstrtab.buffer.items.len == 0); |
| 593 | 532 | try self.shstrtab.buffer.append(gpa, 0); // need a 0 at position 0 |
| 594 | 533 | const off = self.findFreeSpace(self.shstrtab.buffer.items.len, 1); |
| 595 | 534 | log.debug("found .shstrtab free space 0x{x} to 0x{x}", .{ off, off + self.shstrtab.buffer.items.len }); |
| 596 | | try self.sections.append(gpa, .{ |
| 597 | | .shdr = .{ |
| 598 | | .sh_name = try self.shstrtab.insert(gpa, ".shstrtab"), |
| 599 | | .sh_type = elf.SHT_STRTAB, |
| 600 | | .sh_flags = 0, |
| 601 | | .sh_addr = 0, |
| 602 | | .sh_offset = off, |
| 603 | | .sh_size = self.shstrtab.buffer.items.len, |
| 604 | | .sh_link = 0, |
| 605 | | .sh_info = 0, |
| 606 | | .sh_addralign = 1, |
| 607 | | .sh_entsize = 0, |
| 608 | | }, |
| 609 | | .phdr_index = undefined, |
| 535 | try self.shdrs.append(gpa, .{ |
| 536 | .sh_name = try self.shstrtab.insert(gpa, ".shstrtab"), |
| 537 | .sh_type = elf.SHT_STRTAB, |
| 538 | .sh_flags = 0, |
| 539 | .sh_addr = 0, |
| 540 | .sh_offset = off, |
| 541 | .sh_size = self.shstrtab.buffer.items.len, |
| 542 | .sh_link = 0, |
| 543 | .sh_info = 0, |
| 544 | .sh_addralign = 1, |
| 545 | .sh_entsize = 0, |
| 610 | 546 | }); |
| 611 | 547 | self.shstrtab_dirty = true; |
| 612 | 548 | self.shdr_table_dirty = true; |
| 613 | 549 | } |
| 614 | 550 | |
| 615 | 551 | if (self.strtab_section_index == null) { |
| 616 | | self.strtab_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 552 | self.strtab_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 617 | 553 | assert(self.strtab.buffer.items.len == 0); |
| 618 | 554 | try self.strtab.buffer.append(gpa, 0); // need a 0 at position 0 |
| 619 | 555 | const off = self.findFreeSpace(self.strtab.buffer.items.len, 1); |
| 620 | 556 | log.debug("found .strtab free space 0x{x} to 0x{x}", .{ off, off + self.strtab.buffer.items.len }); |
| 621 | | try self.sections.append(gpa, .{ |
| 622 | | .shdr = .{ |
| 623 | | .sh_name = try self.shstrtab.insert(gpa, ".strtab"), |
| 624 | | .sh_type = elf.SHT_STRTAB, |
| 625 | | .sh_flags = 0, |
| 626 | | .sh_addr = 0, |
| 627 | | .sh_offset = off, |
| 628 | | .sh_size = self.strtab.buffer.items.len, |
| 629 | | .sh_link = 0, |
| 630 | | .sh_info = 0, |
| 631 | | .sh_addralign = 1, |
| 632 | | .sh_entsize = 0, |
| 633 | | }, |
| 634 | | .phdr_index = undefined, |
| 557 | try self.shdrs.append(gpa, .{ |
| 558 | .sh_name = try self.shstrtab.insert(gpa, ".strtab"), |
| 559 | .sh_type = elf.SHT_STRTAB, |
| 560 | .sh_flags = 0, |
| 561 | .sh_addr = 0, |
| 562 | .sh_offset = off, |
| 563 | .sh_size = self.strtab.buffer.items.len, |
| 564 | .sh_link = 0, |
| 565 | .sh_info = 0, |
| 566 | .sh_addralign = 1, |
| 567 | .sh_entsize = 0, |
| 635 | 568 | }); |
| 636 | 569 | self.strtab_dirty = true; |
| 637 | 570 | self.shdr_table_dirty = true; |
| 638 | 571 | } |
| 639 | 572 | |
| 640 | 573 | if (self.text_section_index == null) { |
| 641 | | self.text_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 642 | | const phdr = &self.program_headers.items[self.phdr_load_re_index.?]; |
| 643 | | |
| 644 | | try self.sections.append(gpa, .{ |
| 645 | | .shdr = .{ |
| 646 | | .sh_name = try self.shstrtab.insert(gpa, ".text"), |
| 647 | | .sh_type = elf.SHT_PROGBITS, |
| 648 | | .sh_flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| 649 | | .sh_addr = phdr.p_vaddr, |
| 650 | | .sh_offset = phdr.p_offset, |
| 651 | | .sh_size = phdr.p_filesz, |
| 652 | | .sh_link = 0, |
| 653 | | .sh_info = 0, |
| 654 | | .sh_addralign = 1, |
| 655 | | .sh_entsize = 0, |
| 656 | | }, |
| 657 | | .phdr_index = self.phdr_load_re_index.?, |
| 574 | self.text_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 575 | const phdr = &self.phdrs.items[self.phdr_load_re_index.?]; |
| 576 | try self.shdrs.append(gpa, .{ |
| 577 | .sh_name = try self.shstrtab.insert(gpa, ".text"), |
| 578 | .sh_type = elf.SHT_PROGBITS, |
| 579 | .sh_flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| 580 | .sh_addr = phdr.p_vaddr, |
| 581 | .sh_offset = phdr.p_offset, |
| 582 | .sh_size = phdr.p_filesz, |
| 583 | .sh_link = 0, |
| 584 | .sh_info = 0, |
| 585 | .sh_addralign = 1, |
| 586 | .sh_entsize = 0, |
| 658 | 587 | }); |
| 588 | try self.phdr_to_shdr_table.putNoClobber(gpa, self.text_section_index.?, self.phdr_load_re_index.?); |
| 589 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.text_section_index.?, .{}); |
| 659 | 590 | self.shdr_table_dirty = true; |
| 660 | 591 | } |
| 661 | 592 | |
| 662 | 593 | if (self.got_section_index == null) { |
| 663 | | self.got_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 664 | | const phdr = &self.program_headers.items[self.phdr_got_index.?]; |
| 665 | | |
| 666 | | try self.sections.append(gpa, .{ |
| 667 | | .shdr = .{ |
| 668 | | .sh_name = try self.shstrtab.insert(gpa, ".got"), |
| 669 | | .sh_type = elf.SHT_PROGBITS, |
| 670 | | .sh_flags = elf.SHF_ALLOC, |
| 671 | | .sh_addr = phdr.p_vaddr, |
| 672 | | .sh_offset = phdr.p_offset, |
| 673 | | .sh_size = phdr.p_filesz, |
| 674 | | .sh_link = 0, |
| 675 | | .sh_info = 0, |
| 676 | | .sh_addralign = @as(u16, ptr_size), |
| 677 | | .sh_entsize = 0, |
| 678 | | }, |
| 679 | | .phdr_index = self.phdr_got_index.?, |
| 594 | self.got_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 595 | const phdr = &self.phdrs.items[self.phdr_got_index.?]; |
| 596 | try self.shdrs.append(gpa, .{ |
| 597 | .sh_name = try self.shstrtab.insert(gpa, ".got"), |
| 598 | .sh_type = elf.SHT_PROGBITS, |
| 599 | .sh_flags = elf.SHF_ALLOC, |
| 600 | .sh_addr = phdr.p_vaddr, |
| 601 | .sh_offset = phdr.p_offset, |
| 602 | .sh_size = phdr.p_filesz, |
| 603 | .sh_link = 0, |
| 604 | .sh_info = 0, |
| 605 | .sh_addralign = @as(u16, ptr_size), |
| 606 | .sh_entsize = 0, |
| 680 | 607 | }); |
| 608 | try self.phdr_to_shdr_table.putNoClobber(gpa, self.got_section_index.?, self.phdr_got_index.?); |
| 681 | 609 | self.shdr_table_dirty = true; |
| 682 | 610 | } |
| 683 | 611 | |
| 684 | 612 | if (self.rodata_section_index == null) { |
| 685 | | self.rodata_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 686 | | const phdr = &self.program_headers.items[self.phdr_load_ro_index.?]; |
| 687 | | |
| 688 | | try self.sections.append(gpa, .{ |
| 689 | | .shdr = .{ |
| 690 | | .sh_name = try self.shstrtab.insert(gpa, ".rodata"), |
| 691 | | .sh_type = elf.SHT_PROGBITS, |
| 692 | | .sh_flags = elf.SHF_ALLOC, |
| 693 | | .sh_addr = phdr.p_vaddr, |
| 694 | | .sh_offset = phdr.p_offset, |
| 695 | | .sh_size = phdr.p_filesz, |
| 696 | | .sh_link = 0, |
| 697 | | .sh_info = 0, |
| 698 | | .sh_addralign = 1, |
| 699 | | .sh_entsize = 0, |
| 700 | | }, |
| 701 | | .phdr_index = self.phdr_load_ro_index.?, |
| 613 | self.rodata_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 614 | const phdr = &self.phdrs.items[self.phdr_load_ro_index.?]; |
| 615 | try self.shdrs.append(gpa, .{ |
| 616 | .sh_name = try self.shstrtab.insert(gpa, ".rodata"), |
| 617 | .sh_type = elf.SHT_PROGBITS, |
| 618 | .sh_flags = elf.SHF_ALLOC, |
| 619 | .sh_addr = phdr.p_vaddr, |
| 620 | .sh_offset = phdr.p_offset, |
| 621 | .sh_size = phdr.p_filesz, |
| 622 | .sh_link = 0, |
| 623 | .sh_info = 0, |
| 624 | .sh_addralign = 1, |
| 625 | .sh_entsize = 0, |
| 702 | 626 | }); |
| 627 | try self.phdr_to_shdr_table.putNoClobber(gpa, self.rodata_section_index.?, self.phdr_load_ro_index.?); |
| 628 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.rodata_section_index.?, .{}); |
| 703 | 629 | self.shdr_table_dirty = true; |
| 704 | 630 | } |
| 705 | 631 | |
| 706 | 632 | if (self.data_section_index == null) { |
| 707 | | self.data_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 708 | | const phdr = &self.program_headers.items[self.phdr_load_rw_index.?]; |
| 709 | | |
| 710 | | try self.sections.append(gpa, .{ |
| 711 | | .shdr = .{ |
| 712 | | .sh_name = try self.shstrtab.insert(gpa, ".data"), |
| 713 | | .sh_type = elf.SHT_PROGBITS, |
| 714 | | .sh_flags = elf.SHF_WRITE | elf.SHF_ALLOC, |
| 715 | | .sh_addr = phdr.p_vaddr, |
| 716 | | .sh_offset = phdr.p_offset, |
| 717 | | .sh_size = phdr.p_filesz, |
| 718 | | .sh_link = 0, |
| 719 | | .sh_info = 0, |
| 720 | | .sh_addralign = @as(u16, ptr_size), |
| 721 | | .sh_entsize = 0, |
| 722 | | }, |
| 723 | | .phdr_index = self.phdr_load_rw_index.?, |
| 633 | self.data_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 634 | const phdr = &self.phdrs.items[self.phdr_load_rw_index.?]; |
| 635 | try self.shdrs.append(gpa, .{ |
| 636 | .sh_name = try self.shstrtab.insert(gpa, ".data"), |
| 637 | .sh_type = elf.SHT_PROGBITS, |
| 638 | .sh_flags = elf.SHF_WRITE | elf.SHF_ALLOC, |
| 639 | .sh_addr = phdr.p_vaddr, |
| 640 | .sh_offset = phdr.p_offset, |
| 641 | .sh_size = phdr.p_filesz, |
| 642 | .sh_link = 0, |
| 643 | .sh_info = 0, |
| 644 | .sh_addralign = @as(u16, ptr_size), |
| 645 | .sh_entsize = 0, |
| 724 | 646 | }); |
| 647 | try self.phdr_to_shdr_table.putNoClobber(gpa, self.data_section_index.?, self.phdr_load_rw_index.?); |
| 648 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.data_section_index.?, .{}); |
| 725 | 649 | self.shdr_table_dirty = true; |
| 726 | 650 | } |
| 727 | 651 | |
| 728 | 652 | if (self.symtab_section_index == null) { |
| 729 | | self.symtab_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 653 | self.symtab_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 730 | 654 | const min_align: u16 = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym); |
| 731 | 655 | const each_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym); |
| 732 | 656 | const file_size = self.base.options.symbol_count_hint * each_size; |
| 733 | 657 | const off = self.findFreeSpace(file_size, min_align); |
| 734 | 658 | log.debug("found symtab free space 0x{x} to 0x{x}", .{ off, off + file_size }); |
| 735 | | |
| 736 | | try self.sections.append(gpa, .{ |
| 737 | | .shdr = .{ |
| 738 | | .sh_name = try self.shstrtab.insert(gpa, ".symtab"), |
| 739 | | .sh_type = elf.SHT_SYMTAB, |
| 740 | | .sh_flags = 0, |
| 741 | | .sh_addr = 0, |
| 742 | | .sh_offset = off, |
| 743 | | .sh_size = file_size, |
| 744 | | // The section header index of the associated string table. |
| 745 | | .sh_link = self.strtab_section_index.?, |
| 746 | | .sh_info = @as(u32, @intCast(self.local_symbols.items.len)), |
| 747 | | .sh_addralign = min_align, |
| 748 | | .sh_entsize = each_size, |
| 749 | | }, |
| 750 | | .phdr_index = undefined, |
| 659 | try self.shdrs.append(gpa, .{ |
| 660 | .sh_name = try self.shstrtab.insert(gpa, ".symtab"), |
| 661 | .sh_type = elf.SHT_SYMTAB, |
| 662 | .sh_flags = 0, |
| 663 | .sh_addr = 0, |
| 664 | .sh_offset = off, |
| 665 | .sh_size = file_size, |
| 666 | // The section header index of the associated string table. |
| 667 | .sh_link = self.strtab_section_index.?, |
| 668 | .sh_info = @as(u32, @intCast(self.symbols.items.len)), |
| 669 | .sh_addralign = min_align, |
| 670 | .sh_entsize = each_size, |
| 751 | 671 | }); |
| 752 | 672 | self.shdr_table_dirty = true; |
| 753 | 673 | } |
| 754 | 674 | |
| 755 | 675 | if (self.dwarf) |*dw| { |
| 756 | 676 | if (self.debug_str_section_index == null) { |
| 757 | | self.debug_str_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 677 | self.debug_str_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 758 | 678 | assert(dw.strtab.buffer.items.len == 0); |
| 759 | 679 | try dw.strtab.buffer.append(gpa, 0); |
| 760 | | try self.sections.append(gpa, .{ |
| 761 | | .shdr = .{ |
| 762 | | .sh_name = try self.shstrtab.insert(gpa, ".debug_str"), |
| 763 | | .sh_type = elf.SHT_PROGBITS, |
| 764 | | .sh_flags = elf.SHF_MERGE | elf.SHF_STRINGS, |
| 765 | | .sh_addr = 0, |
| 766 | | .sh_offset = 0, |
| 767 | | .sh_size = 0, |
| 768 | | .sh_link = 0, |
| 769 | | .sh_info = 0, |
| 770 | | .sh_addralign = 1, |
| 771 | | .sh_entsize = 1, |
| 772 | | }, |
| 773 | | .phdr_index = undefined, |
| 680 | try self.shdrs.append(gpa, .{ |
| 681 | .sh_name = try self.shstrtab.insert(gpa, ".debug_str"), |
| 682 | .sh_type = elf.SHT_PROGBITS, |
| 683 | .sh_flags = elf.SHF_MERGE | elf.SHF_STRINGS, |
| 684 | .sh_addr = 0, |
| 685 | .sh_offset = 0, |
| 686 | .sh_size = 0, |
| 687 | .sh_link = 0, |
| 688 | .sh_info = 0, |
| 689 | .sh_addralign = 1, |
| 690 | .sh_entsize = 1, |
| 774 | 691 | }); |
| 775 | 692 | self.debug_strtab_dirty = true; |
| 776 | 693 | self.shdr_table_dirty = true; |
| 777 | 694 | } |
| 778 | 695 | |
| 779 | 696 | if (self.debug_info_section_index == null) { |
| 780 | | self.debug_info_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 781 | | |
| 697 | self.debug_info_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 782 | 698 | const file_size_hint = 200; |
| 783 | 699 | const p_align = 1; |
| 784 | 700 | const off = self.findFreeSpace(file_size_hint, p_align); |
| ... | ... | @@ -786,28 +702,24 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 786 | 702 | off, |
| 787 | 703 | off + file_size_hint, |
| 788 | 704 | }); |
| 789 | | try self.sections.append(gpa, .{ |
| 790 | | .shdr = .{ |
| 791 | | .sh_name = try self.shstrtab.insert(gpa, ".debug_info"), |
| 792 | | .sh_type = elf.SHT_PROGBITS, |
| 793 | | .sh_flags = 0, |
| 794 | | .sh_addr = 0, |
| 795 | | .sh_offset = off, |
| 796 | | .sh_size = file_size_hint, |
| 797 | | .sh_link = 0, |
| 798 | | .sh_info = 0, |
| 799 | | .sh_addralign = p_align, |
| 800 | | .sh_entsize = 0, |
| 801 | | }, |
| 802 | | .phdr_index = undefined, |
| 705 | try self.shdrs.append(gpa, .{ |
| 706 | .sh_name = try self.shstrtab.insert(gpa, ".debug_info"), |
| 707 | .sh_type = elf.SHT_PROGBITS, |
| 708 | .sh_flags = 0, |
| 709 | .sh_addr = 0, |
| 710 | .sh_offset = off, |
| 711 | .sh_size = file_size_hint, |
| 712 | .sh_link = 0, |
| 713 | .sh_info = 0, |
| 714 | .sh_addralign = p_align, |
| 715 | .sh_entsize = 0, |
| 803 | 716 | }); |
| 804 | 717 | self.shdr_table_dirty = true; |
| 805 | 718 | self.debug_info_header_dirty = true; |
| 806 | 719 | } |
| 807 | 720 | |
| 808 | 721 | if (self.debug_abbrev_section_index == null) { |
| 809 | | self.debug_abbrev_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 810 | | |
| 722 | self.debug_abbrev_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 811 | 723 | const file_size_hint = 128; |
| 812 | 724 | const p_align = 1; |
| 813 | 725 | const off = self.findFreeSpace(file_size_hint, p_align); |
| ... | ... | @@ -815,28 +727,24 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 815 | 727 | off, |
| 816 | 728 | off + file_size_hint, |
| 817 | 729 | }); |
| 818 | | try self.sections.append(gpa, .{ |
| 819 | | .shdr = .{ |
| 820 | | .sh_name = try self.shstrtab.insert(gpa, ".debug_abbrev"), |
| 821 | | .sh_type = elf.SHT_PROGBITS, |
| 822 | | .sh_flags = 0, |
| 823 | | .sh_addr = 0, |
| 824 | | .sh_offset = off, |
| 825 | | .sh_size = file_size_hint, |
| 826 | | .sh_link = 0, |
| 827 | | .sh_info = 0, |
| 828 | | .sh_addralign = p_align, |
| 829 | | .sh_entsize = 0, |
| 830 | | }, |
| 831 | | .phdr_index = undefined, |
| 730 | try self.shdrs.append(gpa, .{ |
| 731 | .sh_name = try self.shstrtab.insert(gpa, ".debug_abbrev"), |
| 732 | .sh_type = elf.SHT_PROGBITS, |
| 733 | .sh_flags = 0, |
| 734 | .sh_addr = 0, |
| 735 | .sh_offset = off, |
| 736 | .sh_size = file_size_hint, |
| 737 | .sh_link = 0, |
| 738 | .sh_info = 0, |
| 739 | .sh_addralign = p_align, |
| 740 | .sh_entsize = 0, |
| 832 | 741 | }); |
| 833 | 742 | self.shdr_table_dirty = true; |
| 834 | 743 | self.debug_abbrev_section_dirty = true; |
| 835 | 744 | } |
| 836 | 745 | |
| 837 | 746 | if (self.debug_aranges_section_index == null) { |
| 838 | | self.debug_aranges_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 839 | | |
| 747 | self.debug_aranges_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 840 | 748 | const file_size_hint = 160; |
| 841 | 749 | const p_align = 16; |
| 842 | 750 | const off = self.findFreeSpace(file_size_hint, p_align); |
| ... | ... | @@ -844,28 +752,24 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 844 | 752 | off, |
| 845 | 753 | off + file_size_hint, |
| 846 | 754 | }); |
| 847 | | try self.sections.append(gpa, .{ |
| 848 | | .shdr = .{ |
| 849 | | .sh_name = try self.shstrtab.insert(gpa, ".debug_aranges"), |
| 850 | | .sh_type = elf.SHT_PROGBITS, |
| 851 | | .sh_flags = 0, |
| 852 | | .sh_addr = 0, |
| 853 | | .sh_offset = off, |
| 854 | | .sh_size = file_size_hint, |
| 855 | | .sh_link = 0, |
| 856 | | .sh_info = 0, |
| 857 | | .sh_addralign = p_align, |
| 858 | | .sh_entsize = 0, |
| 859 | | }, |
| 860 | | .phdr_index = undefined, |
| 755 | try self.shdrs.append(gpa, .{ |
| 756 | .sh_name = try self.shstrtab.insert(gpa, ".debug_aranges"), |
| 757 | .sh_type = elf.SHT_PROGBITS, |
| 758 | .sh_flags = 0, |
| 759 | .sh_addr = 0, |
| 760 | .sh_offset = off, |
| 761 | .sh_size = file_size_hint, |
| 762 | .sh_link = 0, |
| 763 | .sh_info = 0, |
| 764 | .sh_addralign = p_align, |
| 765 | .sh_entsize = 0, |
| 861 | 766 | }); |
| 862 | 767 | self.shdr_table_dirty = true; |
| 863 | 768 | self.debug_aranges_section_dirty = true; |
| 864 | 769 | } |
| 865 | 770 | |
| 866 | 771 | if (self.debug_line_section_index == null) { |
| 867 | | self.debug_line_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 868 | | |
| 772 | self.debug_line_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 869 | 773 | const file_size_hint = 250; |
| 870 | 774 | const p_align = 1; |
| 871 | 775 | const off = self.findFreeSpace(file_size_hint, p_align); |
| ... | ... | @@ -873,20 +777,17 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 873 | 777 | off, |
| 874 | 778 | off + file_size_hint, |
| 875 | 779 | }); |
| 876 | | try self.sections.append(gpa, .{ |
| 877 | | .shdr = .{ |
| 878 | | .sh_name = try self.shstrtab.insert(gpa, ".debug_line"), |
| 879 | | .sh_type = elf.SHT_PROGBITS, |
| 880 | | .sh_flags = 0, |
| 881 | | .sh_addr = 0, |
| 882 | | .sh_offset = off, |
| 883 | | .sh_size = file_size_hint, |
| 884 | | .sh_link = 0, |
| 885 | | .sh_info = 0, |
| 886 | | .sh_addralign = p_align, |
| 887 | | .sh_entsize = 0, |
| 888 | | }, |
| 889 | | .phdr_index = undefined, |
| 780 | try self.shdrs.append(gpa, .{ |
| 781 | .sh_name = try self.shstrtab.insert(gpa, ".debug_line"), |
| 782 | .sh_type = elf.SHT_PROGBITS, |
| 783 | .sh_flags = 0, |
| 784 | .sh_addr = 0, |
| 785 | .sh_offset = off, |
| 786 | .sh_size = file_size_hint, |
| 787 | .sh_link = 0, |
| 788 | .sh_info = 0, |
| 789 | .sh_addralign = p_align, |
| 790 | .sh_entsize = 0, |
| 890 | 791 | }); |
| 891 | 792 | self.shdr_table_dirty = true; |
| 892 | 793 | self.debug_line_header_dirty = true; |
| ... | ... | @@ -902,13 +803,13 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 902 | 803 | .p64 => @alignOf(elf.Elf64_Shdr), |
| 903 | 804 | }; |
| 904 | 805 | if (self.shdr_table_offset == null) { |
| 905 | | self.shdr_table_offset = self.findFreeSpace(self.sections.slice().len * shsize, shalign); |
| 806 | self.shdr_table_offset = self.findFreeSpace(self.shdrs.items.len * shsize, shalign); |
| 906 | 807 | self.shdr_table_dirty = true; |
| 907 | 808 | } |
| 908 | 809 | |
| 909 | 810 | { |
| 910 | 811 | // Iterate over symbols, populating free_list and last_text_block. |
| 911 | | if (self.local_symbols.items.len != 1) { |
| 812 | if (self.symbols.items.len != 1) { |
| 912 | 813 | @panic("TODO implement setting up free_list and last_text_block from existing ELF file"); |
| 913 | 814 | } |
| 914 | 815 | // We are starting with an empty file. The default values are correct, null and empty list. |
| ... | ... | @@ -920,7 +821,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 920 | 821 | // offset + it's filesize. |
| 921 | 822 | var max_file_offset: u64 = 0; |
| 922 | 823 | |
| 923 | | for (self.sections.items(.shdr)) |shdr| { |
| 824 | for (self.shdrs.items) |shdr| { |
| 924 | 825 | if (shdr.sh_offset + shdr.sh_size > max_file_offset) { |
| 925 | 826 | max_file_offset = shdr.sh_offset + shdr.sh_size; |
| 926 | 827 | } |
| ... | ... | @@ -928,25 +829,47 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 928 | 829 | |
| 929 | 830 | try self.base.file.?.pwriteAll(&[_]u8{0}, max_file_offset); |
| 930 | 831 | } |
| 832 | |
| 833 | if (self.base.options.module) |module| { |
| 834 | if (self.zig_module_index == null) { |
| 835 | const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); |
| 836 | self.files.set(index, .{ .zig_module = .{ |
| 837 | .index = index, |
| 838 | .path = module.main_pkg.root_src_path, |
| 839 | } }); |
| 840 | self.zig_module_index = index; |
| 841 | const zig_module = self.file(index).?.zig_module; |
| 842 | |
| 843 | const name_off = try self.strtab.insert(gpa, std.fs.path.stem(module.main_pkg.root_src_path)); |
| 844 | const symbol_index = try self.addSymbol(); |
| 845 | try zig_module.local_symbols.append(gpa, symbol_index); |
| 846 | const symbol_ptr = self.symbol(symbol_index); |
| 847 | symbol_ptr.file_index = zig_module.index; |
| 848 | symbol_ptr.name_offset = name_off; |
| 849 | |
| 850 | const esym_index = try zig_module.addLocalEsym(gpa); |
| 851 | const esym = &zig_module.local_esyms.items[esym_index]; |
| 852 | esym.st_name = name_off; |
| 853 | esym.st_info |= elf.STT_FILE; |
| 854 | esym.st_shndx = elf.SHN_ABS; |
| 855 | symbol_ptr.esym_index = esym_index; |
| 856 | } |
| 857 | } |
| 931 | 858 | } |
| 932 | 859 | |
| 933 | | fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void { |
| 860 | pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void { |
| 934 | 861 | // TODO Also detect virtual address collisions. |
| 935 | | const shdr = &self.sections.items(.shdr)[shdr_index]; |
| 936 | | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 937 | | const phdr = &self.program_headers.items[phdr_index]; |
| 938 | | const maybe_last_atom_index = self.sections.items(.last_atom_index)[shdr_index]; |
| 862 | const shdr = &self.shdrs.items[shdr_index]; |
| 863 | const phdr_index = self.phdr_to_shdr_table.get(shdr_index).?; |
| 864 | const phdr = &self.phdrs.items[phdr_index]; |
| 939 | 865 | |
| 940 | 866 | if (needed_size > self.allocatedSize(shdr.sh_offset)) { |
| 941 | 867 | // Must move the entire section. |
| 942 | 868 | const new_offset = self.findFreeSpace(needed_size, self.page_size); |
| 943 | | const existing_size = if (maybe_last_atom_index) |last_atom_index| blk: { |
| 944 | | const last = self.getAtom(last_atom_index); |
| 945 | | const sym = last.getSymbol(self); |
| 946 | | break :blk (sym.st_value + sym.st_size) - phdr.p_vaddr; |
| 947 | | } else if (shdr_index == self.got_section_index.?) blk: { |
| 948 | | break :blk shdr.sh_size; |
| 949 | | } else 0; |
| 869 | const existing_size = if (self.last_atom_and_free_list_table.get(shdr_index)) |meta| blk: { |
| 870 | const last = self.atom(meta.last_atom_index) orelse break :blk 0; |
| 871 | break :blk (last.value + last.size) - phdr.p_vaddr; |
| 872 | } else shdr.sh_size; |
| 950 | 873 | shdr.sh_size = 0; |
| 951 | 874 | |
| 952 | 875 | log.debug("new '{?s}' file offset 0x{x} to 0x{x}", .{ |
| ... | ... | @@ -976,7 +899,7 @@ pub fn growNonAllocSection( |
| 976 | 899 | min_alignment: u32, |
| 977 | 900 | requires_file_copy: bool, |
| 978 | 901 | ) !void { |
| 979 | | const shdr = &self.sections.items(.shdr)[shdr_index]; |
| 902 | const shdr = &self.shdrs.items[shdr_index]; |
| 980 | 903 | |
| 981 | 904 | if (needed_size > self.allocatedSize(shdr.sh_offset)) { |
| 982 | 905 | const existing_size = if (self.symtab_section_index.? == shdr_index) blk: { |
| ... | ... | @@ -989,7 +912,12 @@ pub fn growNonAllocSection( |
| 989 | 912 | shdr.sh_size = 0; |
| 990 | 913 | // Move all the symbols to a new file location. |
| 991 | 914 | const new_offset = self.findFreeSpace(needed_size, min_alignment); |
| 992 | | log.debug("moving '{?s}' from 0x{x} to 0x{x}", .{ self.shstrtab.get(shdr.sh_name), shdr.sh_offset, new_offset }); |
| 915 | |
| 916 | log.debug("moving '{?s}' from 0x{x} to 0x{x}", .{ |
| 917 | self.shstrtab.get(shdr.sh_name), |
| 918 | shdr.sh_offset, |
| 919 | new_offset, |
| 920 | }); |
| 993 | 921 | |
| 994 | 922 | if (requires_file_copy) { |
| 995 | 923 | const amt = try self.base.file.?.copyRangeAll( |
| ... | ... | @@ -1065,21 +993,50 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1065 | 993 | // corresponds to the Zig source code. |
| 1066 | 994 | const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented; |
| 1067 | 995 | |
| 996 | const compiler_rt_path: ?[]const u8 = blk: { |
| 997 | if (comp.compiler_rt_lib) |x| break :blk x.full_object_path; |
| 998 | if (comp.compiler_rt_obj) |x| break :blk x.full_object_path; |
| 999 | break :blk null; |
| 1000 | }; |
| 1001 | _ = compiler_rt_path; |
| 1002 | |
| 1003 | // Here we will parse input positional and library files (if referenced). |
| 1004 | // This will roughly match in any linker backend we support. |
| 1005 | var positionals = std.ArrayList(Compilation.LinkObject).init(gpa); |
| 1006 | defer positionals.deinit(); |
| 1007 | try positionals.ensureUnusedCapacity(self.base.options.objects.len); |
| 1008 | positionals.appendSliceAssumeCapacity(self.base.options.objects); |
| 1009 | |
| 1010 | // This is a set of object files emitted by clang in a single `build-exe` invocation. |
| 1011 | // For instance, the implicit `a.o` as compiled by `zig build-exe a.c` will end up |
| 1012 | // in this set. |
| 1013 | for (comp.c_object_table.keys()) |key| { |
| 1014 | try positionals.append(.{ .path = key.status.success.object_path }); |
| 1015 | } |
| 1016 | |
| 1017 | for (positionals.items) |obj| { |
| 1018 | const in_file = try std.fs.cwd().openFile(obj.path, .{}); |
| 1019 | defer in_file.close(); |
| 1020 | |
| 1021 | var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined }; |
| 1022 | self.parsePositional(in_file, obj.path, obj.must_link, &parse_ctx) catch |err| |
| 1023 | try self.handleAndReportParseError(obj.path, err, &parse_ctx); |
| 1024 | } |
| 1025 | |
| 1026 | // Handle any lazy symbols that were emitted by incremental compilation. |
| 1068 | 1027 | if (self.lazy_syms.getPtr(.none)) |metadata| { |
| 1069 | 1028 | // Most lazy symbols can be updated on first use, but |
| 1070 | 1029 | // anyerror needs to wait for everything to be flushed. |
| 1071 | | if (metadata.text_state != .unused) self.updateLazySymbolAtom( |
| 1072 | | File.LazySymbol.initDecl(.code, null, module), |
| 1073 | | metadata.text_atom, |
| 1074 | | self.text_section_index.?, |
| 1030 | if (metadata.text_state != .unused) self.updateLazySymbol( |
| 1031 | link.File.LazySymbol.initDecl(.code, null, module), |
| 1032 | metadata.text_symbol_index, |
| 1075 | 1033 | ) catch |err| return switch (err) { |
| 1076 | 1034 | error.CodegenFail => error.FlushFailure, |
| 1077 | 1035 | else => |e| e, |
| 1078 | 1036 | }; |
| 1079 | | if (metadata.rodata_state != .unused) self.updateLazySymbolAtom( |
| 1080 | | File.LazySymbol.initDecl(.const_data, null, module), |
| 1081 | | metadata.rodata_atom, |
| 1082 | | self.rodata_section_index.?, |
| 1037 | if (metadata.rodata_state != .unused) self.updateLazySymbol( |
| 1038 | link.File.LazySymbol.initDecl(.const_data, null, module), |
| 1039 | metadata.rodata_symbol_index, |
| 1083 | 1040 | ) catch |err| return switch (err) { |
| 1084 | 1041 | error.CodegenFail => error.FlushFailure, |
| 1085 | 1042 | else => |e| e, |
| ... | ... | @@ -1097,54 +1054,67 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1097 | 1054 | try dw.flushModule(module); |
| 1098 | 1055 | } |
| 1099 | 1056 | |
| 1100 | | { |
| 1101 | | var it = self.relocs.iterator(); |
| 1102 | | while (it.next()) |entry| { |
| 1103 | | const atom_index = entry.key_ptr.*; |
| 1104 | | const relocs = entry.value_ptr.*; |
| 1105 | | const atom = self.getAtom(atom_index); |
| 1106 | | const source_sym = atom.getSymbol(self); |
| 1107 | | const source_shdr = self.sections.items(.shdr)[source_sym.st_shndx]; |
| 1108 | | |
| 1109 | | log.debug("relocating '{?s}'", .{self.strtab.get(source_sym.st_name)}); |
| 1110 | | |
| 1111 | | for (relocs.items) |*reloc| { |
| 1112 | | const target_sym = self.local_symbols.items[reloc.target]; |
| 1113 | | const target_vaddr = target_sym.st_value + reloc.addend; |
| 1114 | | |
| 1115 | | if (target_vaddr == reloc.prev_vaddr) continue; |
| 1116 | | |
| 1117 | | const section_offset = (source_sym.st_value + reloc.offset) - source_shdr.sh_addr; |
| 1118 | | const file_offset = source_shdr.sh_offset + section_offset; |
| 1119 | | |
| 1120 | | log.debug(" ({x}: [() => 0x{x}] ({?s}))", .{ |
| 1121 | | reloc.offset, |
| 1122 | | target_vaddr, |
| 1123 | | self.strtab.get(target_sym.st_name), |
| 1124 | | }); |
| 1125 | | |
| 1126 | | switch (self.ptr_width) { |
| 1127 | | .p32 => try self.base.file.?.pwriteAll(mem.asBytes(&@as(u32, @intCast(target_vaddr))), file_offset), |
| 1128 | | .p64 => try self.base.file.?.pwriteAll(mem.asBytes(&target_vaddr), file_offset), |
| 1129 | | } |
| 1130 | | |
| 1131 | | reloc.prev_vaddr = target_vaddr; |
| 1132 | | } |
| 1133 | | } |
| 1134 | | } |
| 1135 | | |
| 1136 | | try self.writeSymbols(); |
| 1137 | | |
| 1138 | | if (build_options.enable_logging) { |
| 1139 | | self.logSymtab(); |
| 1140 | | } |
| 1057 | // If we haven't already, create a linker-generated input file comprising of |
| 1058 | // linker-defined synthetic symbols only such as `_DYNAMIC`, etc. |
| 1059 | if (self.linker_defined_index == null) { |
| 1060 | const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); |
| 1061 | self.files.set(index, .{ .linker_defined = .{ .index = index } }); |
| 1062 | self.linker_defined_index = index; |
| 1063 | } |
| 1064 | try self.addLinkerDefinedSymbols(); |
| 1065 | |
| 1066 | // Now, we are ready to resolve the symbols across all input files. |
| 1067 | // We will first resolve the files in the ZigModule, next in the parsed |
| 1068 | // input Object files. |
| 1069 | // Any qualifing unresolved symbol will be upgraded to an absolute, weak |
| 1070 | // symbol for potential resolution at load-time. |
| 1071 | self.resolveSymbols(); |
| 1072 | self.markImportsExports(); |
| 1073 | self.claimUnresolved(); |
| 1074 | |
| 1075 | // Scan and create missing synthetic entries such as GOT indirection. |
| 1076 | try self.scanRelocs(); |
| 1077 | |
| 1078 | // Allocate atoms parsed from input object files, followed by allocating |
| 1079 | // linker-defined synthetic symbols. |
| 1080 | try self.allocateObjects(); |
| 1081 | self.allocateLinkerDefinedSymbols(); |
| 1082 | |
| 1083 | // Beyond this point, everything has been allocated a virtual address and we can resolve |
| 1084 | // the relocations, and commit objects to file. |
| 1085 | if (self.zig_module_index) |index| { |
| 1086 | for (self.file(index).?.zig_module.atoms.keys()) |atom_index| { |
| 1087 | const atom_ptr = self.atom(atom_index).?; |
| 1088 | if (!atom_ptr.alive) continue; |
| 1089 | const shdr = &self.shdrs.items[atom_ptr.output_section_index]; |
| 1090 | const file_offset = shdr.sh_offset + atom_ptr.value - shdr.sh_addr; |
| 1091 | const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow; |
| 1092 | const code = try gpa.alloc(u8, size); |
| 1093 | defer gpa.free(code); |
| 1094 | const amt = try self.base.file.?.preadAll(code, file_offset); |
| 1095 | if (amt != code.len) return error.InputOutput; |
| 1096 | try atom_ptr.resolveRelocs(self, code); |
| 1097 | try self.base.file.?.pwriteAll(code, file_offset); |
| 1098 | } |
| 1099 | } |
| 1100 | try self.writeObjects(); |
| 1101 | |
| 1102 | // Generate and emit the symbol table. |
| 1103 | try self.updateSymtabSize(); |
| 1104 | try self.writeSymtab(); |
| 1105 | |
| 1106 | // Dump the state for easy debugging. |
| 1107 | // State can be dumped via `--debug-log link_state`. |
| 1108 | if (build_options.enable_logging) { |
| 1109 | state_log.debug("{}", .{self.dumpState()}); |
| 1110 | } |
| 1141 | 1111 | |
| 1142 | 1112 | if (self.dwarf) |*dw| { |
| 1143 | 1113 | if (self.debug_abbrev_section_dirty) { |
| 1144 | 1114 | try dw.writeDbgAbbrev(); |
| 1145 | 1115 | if (!self.shdr_table_dirty) { |
| 1146 | 1116 | // Then it won't get written with the others and we need to do it. |
| 1147 | | try self.writeSectHeader(self.debug_abbrev_section_index.?); |
| 1117 | try self.writeShdr(self.debug_abbrev_section_index.?); |
| 1148 | 1118 | } |
| 1149 | 1119 | self.debug_abbrev_section_dirty = false; |
| 1150 | 1120 | } |
| ... | ... | @@ -1152,7 +1122,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1152 | 1122 | if (self.debug_info_header_dirty) { |
| 1153 | 1123 | // Currently only one compilation unit is supported, so the address range is simply |
| 1154 | 1124 | // identical to the main program header virtual address and memory size. |
| 1155 | | const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?]; |
| 1125 | const text_phdr = &self.phdrs.items[self.phdr_load_re_index.?]; |
| 1156 | 1126 | const low_pc = text_phdr.p_vaddr; |
| 1157 | 1127 | const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz; |
| 1158 | 1128 | try dw.writeDbgInfoHeader(module, low_pc, high_pc); |
| ... | ... | @@ -1162,11 +1132,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1162 | 1132 | if (self.debug_aranges_section_dirty) { |
| 1163 | 1133 | // Currently only one compilation unit is supported, so the address range is simply |
| 1164 | 1134 | // identical to the main program header virtual address and memory size. |
| 1165 | | const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?]; |
| 1135 | const text_phdr = &self.phdrs.items[self.phdr_load_re_index.?]; |
| 1166 | 1136 | try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz); |
| 1167 | 1137 | if (!self.shdr_table_dirty) { |
| 1168 | 1138 | // Then it won't get written with the others and we need to do it. |
| 1169 | | try self.writeSectHeader(self.debug_aranges_section_index.?); |
| 1139 | try self.writeShdr(self.debug_aranges_section_index.?); |
| 1170 | 1140 | } |
| 1171 | 1141 | self.debug_aranges_section_dirty = false; |
| 1172 | 1142 | } |
| ... | ... | @@ -1184,11 +1154,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1184 | 1154 | }; |
| 1185 | 1155 | |
| 1186 | 1156 | const phdr_table_index = self.phdr_table_index.?; |
| 1187 | | const phdr_table = &self.program_headers.items[phdr_table_index]; |
| 1188 | | const phdr_table_load = &self.program_headers.items[self.phdr_table_load_index.?]; |
| 1157 | const phdr_table = &self.phdrs.items[phdr_table_index]; |
| 1158 | const phdr_table_load = &self.phdrs.items[self.phdr_table_load_index.?]; |
| 1189 | 1159 | |
| 1190 | 1160 | const allocated_size = self.allocatedSize(phdr_table.p_offset); |
| 1191 | | const needed_size = self.program_headers.items.len * phsize; |
| 1161 | const needed_size = self.phdrs.items.len * phsize; |
| 1192 | 1162 | |
| 1193 | 1163 | if (needed_size > allocated_size) { |
| 1194 | 1164 | phdr_table.p_offset = 0; // free the space |
| ... | ... | @@ -1207,11 +1177,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1207 | 1177 | |
| 1208 | 1178 | switch (self.ptr_width) { |
| 1209 | 1179 | .p32 => { |
| 1210 | | const buf = try gpa.alloc(elf.Elf32_Phdr, self.program_headers.items.len); |
| 1180 | const buf = try gpa.alloc(elf.Elf32_Phdr, self.phdrs.items.len); |
| 1211 | 1181 | defer gpa.free(buf); |
| 1212 | 1182 | |
| 1213 | 1183 | for (buf, 0..) |*phdr, i| { |
| 1214 | | phdr.* = progHeaderTo32(self.program_headers.items[i]); |
| 1184 | phdr.* = phdrTo32(self.phdrs.items[i]); |
| 1215 | 1185 | if (foreign_endian) { |
| 1216 | 1186 | mem.byteSwapAllFields(elf.Elf32_Phdr, phdr); |
| 1217 | 1187 | } |
| ... | ... | @@ -1219,11 +1189,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1219 | 1189 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), phdr_table.p_offset); |
| 1220 | 1190 | }, |
| 1221 | 1191 | .p64 => { |
| 1222 | | const buf = try gpa.alloc(elf.Elf64_Phdr, self.program_headers.items.len); |
| 1192 | const buf = try gpa.alloc(elf.Elf64_Phdr, self.phdrs.items.len); |
| 1223 | 1193 | defer gpa.free(buf); |
| 1224 | 1194 | |
| 1225 | 1195 | for (buf, 0..) |*phdr, i| { |
| 1226 | | phdr.* = self.program_headers.items[i]; |
| 1196 | phdr.* = self.phdrs.items[i]; |
| 1227 | 1197 | if (foreign_endian) { |
| 1228 | 1198 | mem.byteSwapAllFields(elf.Elf64_Phdr, phdr); |
| 1229 | 1199 | } |
| ... | ... | @@ -1241,9 +1211,9 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1241 | 1211 | |
| 1242 | 1212 | { |
| 1243 | 1213 | const shdr_index = self.shstrtab_section_index.?; |
| 1244 | | if (self.shstrtab_dirty or self.shstrtab.buffer.items.len != self.sections.items(.shdr)[shdr_index].sh_size) { |
| 1214 | if (self.shstrtab_dirty or self.shstrtab.buffer.items.len != self.shdrs.items[shdr_index].sh_size) { |
| 1245 | 1215 | try self.growNonAllocSection(shdr_index, self.shstrtab.buffer.items.len, 1, false); |
| 1246 | | const shstrtab_sect = self.sections.items(.shdr)[shdr_index]; |
| 1216 | const shstrtab_sect = &self.shdrs.items[shdr_index]; |
| 1247 | 1217 | try self.base.file.?.pwriteAll(self.shstrtab.buffer.items, shstrtab_sect.sh_offset); |
| 1248 | 1218 | self.shstrtab_dirty = false; |
| 1249 | 1219 | } |
| ... | ... | @@ -1251,9 +1221,9 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1251 | 1221 | |
| 1252 | 1222 | { |
| 1253 | 1223 | const shdr_index = self.strtab_section_index.?; |
| 1254 | | if (self.strtab_dirty or self.strtab.buffer.items.len != self.sections.items(.shdr)[shdr_index].sh_size) { |
| 1224 | if (self.strtab_dirty or self.strtab.buffer.items.len != self.shdrs.items[shdr_index].sh_size) { |
| 1255 | 1225 | try self.growNonAllocSection(shdr_index, self.strtab.buffer.items.len, 1, false); |
| 1256 | | const strtab_sect = self.sections.items(.shdr)[shdr_index]; |
| 1226 | const strtab_sect = self.shdrs.items[shdr_index]; |
| 1257 | 1227 | try self.base.file.?.pwriteAll(self.strtab.buffer.items, strtab_sect.sh_offset); |
| 1258 | 1228 | self.strtab_dirty = false; |
| 1259 | 1229 | } |
| ... | ... | @@ -1261,9 +1231,9 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1261 | 1231 | |
| 1262 | 1232 | if (self.dwarf) |dwarf| { |
| 1263 | 1233 | const shdr_index = self.debug_str_section_index.?; |
| 1264 | | if (self.debug_strtab_dirty or dwarf.strtab.buffer.items.len != self.sections.items(.shdr)[shdr_index].sh_size) { |
| 1234 | if (self.debug_strtab_dirty or dwarf.strtab.buffer.items.len != self.shdrs.items[shdr_index].sh_size) { |
| 1265 | 1235 | try self.growNonAllocSection(shdr_index, dwarf.strtab.buffer.items.len, 1, false); |
| 1266 | | const debug_strtab_sect = self.sections.items(.shdr)[shdr_index]; |
| 1236 | const debug_strtab_sect = self.shdrs.items[shdr_index]; |
| 1267 | 1237 | try self.base.file.?.pwriteAll(dwarf.strtab.buffer.items, debug_strtab_sect.sh_offset); |
| 1268 | 1238 | self.debug_strtab_dirty = false; |
| 1269 | 1239 | } |
| ... | ... | @@ -1279,7 +1249,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1279 | 1249 | .p64 => @alignOf(elf.Elf64_Shdr), |
| 1280 | 1250 | }; |
| 1281 | 1251 | const allocated_size = self.allocatedSize(self.shdr_table_offset.?); |
| 1282 | | const needed_size = self.sections.slice().len * shsize; |
| 1252 | const needed_size = self.shdrs.items.len * shsize; |
| 1283 | 1253 | |
| 1284 | 1254 | if (needed_size > allocated_size) { |
| 1285 | 1255 | self.shdr_table_offset = null; // free the space |
| ... | ... | @@ -1288,12 +1258,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1288 | 1258 | |
| 1289 | 1259 | switch (self.ptr_width) { |
| 1290 | 1260 | .p32 => { |
| 1291 | | const slice = self.sections.slice(); |
| 1292 | | const buf = try gpa.alloc(elf.Elf32_Shdr, slice.len); |
| 1261 | const buf = try gpa.alloc(elf.Elf32_Shdr, self.shdrs.items.len); |
| 1293 | 1262 | defer gpa.free(buf); |
| 1294 | 1263 | |
| 1295 | 1264 | for (buf, 0..) |*shdr, i| { |
| 1296 | | shdr.* = sectHeaderTo32(slice.items(.shdr)[i]); |
| 1265 | shdr.* = shdrTo32(self.shdrs.items[i]); |
| 1297 | 1266 | log.debug("writing section {?s}: {}", .{ self.shstrtab.get(shdr.sh_name), shdr.* }); |
| 1298 | 1267 | if (foreign_endian) { |
| 1299 | 1268 | mem.byteSwapAllFields(elf.Elf32_Shdr, shdr); |
| ... | ... | @@ -1302,12 +1271,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1302 | 1271 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), self.shdr_table_offset.?); |
| 1303 | 1272 | }, |
| 1304 | 1273 | .p64 => { |
| 1305 | | const slice = self.sections.slice(); |
| 1306 | | const buf = try gpa.alloc(elf.Elf64_Shdr, slice.len); |
| 1274 | const buf = try gpa.alloc(elf.Elf64_Shdr, self.shdrs.items.len); |
| 1307 | 1275 | defer gpa.free(buf); |
| 1308 | 1276 | |
| 1309 | 1277 | for (buf, 0..) |*shdr, i| { |
| 1310 | | shdr.* = slice.items(.shdr)[i]; |
| 1278 | shdr.* = self.shdrs.items[i]; |
| 1311 | 1279 | log.debug("writing section {?s}: {}", .{ self.shstrtab.get(shdr.sh_name), shdr.* }); |
| 1312 | 1280 | if (foreign_endian) { |
| 1313 | 1281 | mem.byteSwapAllFields(elf.Elf64_Shdr, shdr); |
| ... | ... | @@ -1338,7 +1306,194 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1338 | 1306 | assert(!self.shstrtab_dirty); |
| 1339 | 1307 | assert(!self.strtab_dirty); |
| 1340 | 1308 | assert(!self.debug_strtab_dirty); |
| 1341 | | assert(!self.got_table_count_dirty); |
| 1309 | assert(!self.got.dirty); |
| 1310 | } |
| 1311 | |
| 1312 | const ParseError = error{ |
| 1313 | UnknownFileType, |
| 1314 | InvalidCpuArch, |
| 1315 | OutOfMemory, |
| 1316 | Overflow, |
| 1317 | InputOutput, |
| 1318 | EndOfStream, |
| 1319 | FileSystem, |
| 1320 | NotSupported, |
| 1321 | } || std.os.SeekError || std.fs.File.OpenError || std.fs.File.ReadError; |
| 1322 | |
| 1323 | fn parsePositional( |
| 1324 | self: *Elf, |
| 1325 | in_file: std.fs.File, |
| 1326 | path: []const u8, |
| 1327 | must_link: bool, |
| 1328 | ctx: *ParseErrorCtx, |
| 1329 | ) ParseError!void { |
| 1330 | const tracy = trace(@src()); |
| 1331 | defer tracy.end(); |
| 1332 | _ = must_link; |
| 1333 | |
| 1334 | if (Object.isObject(in_file)) { |
| 1335 | try self.parseObject(in_file, path, ctx); |
| 1336 | } else return error.UnknownFileType; |
| 1337 | } |
| 1338 | |
| 1339 | fn parseObject(self: *Elf, in_file: std.fs.File, path: []const u8, ctx: *ParseErrorCtx) ParseError!void { |
| 1340 | const tracy = trace(@src()); |
| 1341 | defer tracy.end(); |
| 1342 | |
| 1343 | const gpa = self.base.allocator; |
| 1344 | const data = try in_file.readToEndAlloc(gpa, std.math.maxInt(u32)); |
| 1345 | const index = @as(File.Index, @intCast(try self.files.addOne(gpa))); |
| 1346 | self.files.set(index, .{ .object = .{ |
| 1347 | .path = path, |
| 1348 | .data = data, |
| 1349 | .index = index, |
| 1350 | } }); |
| 1351 | try self.objects.append(gpa, index); |
| 1352 | |
| 1353 | const object = self.file(index).?.object; |
| 1354 | try object.parse(self); |
| 1355 | |
| 1356 | ctx.detected_cpu_arch = object.header.?.e_machine.toTargetCpuArch().?; |
| 1357 | if (ctx.detected_cpu_arch != self.base.options.target.cpu.arch) return error.InvalidCpuArch; |
| 1358 | } |
| 1359 | |
| 1360 | fn resolveSymbols(self: *Elf) void { |
| 1361 | if (self.zig_module_index) |index| { |
| 1362 | const zig_module = self.file(index).?.zig_module; |
| 1363 | zig_module.resolveSymbols(self); |
| 1364 | } |
| 1365 | |
| 1366 | for (self.objects.items) |index| { |
| 1367 | const object = self.file(index).?.object; |
| 1368 | object.resolveSymbols(self); |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | fn markImportsExports(self: *Elf) void { |
| 1373 | const mark = struct { |
| 1374 | fn mark(elf_file: *Elf, file_index: File.Index) void { |
| 1375 | for (elf_file.file(file_index).?.globals()) |global_index| { |
| 1376 | const global = elf_file.symbol(global_index); |
| 1377 | if (global.version_index == elf.VER_NDX_LOCAL) continue; |
| 1378 | const file_ptr = global.file(elf_file) orelse continue; |
| 1379 | const vis = @as(elf.STV, @enumFromInt(global.elfSym(elf_file).st_other)); |
| 1380 | if (vis == .HIDDEN) continue; |
| 1381 | // if (file == .shared and !global.isAbs(self)) { |
| 1382 | // global.flags.import = true; |
| 1383 | // continue; |
| 1384 | // } |
| 1385 | if (file_ptr.index() == file_index) { |
| 1386 | global.flags.@"export" = true; |
| 1387 | if (elf_file.isDynLib() and vis != .PROTECTED) { |
| 1388 | global.flags.import = true; |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | }.mark; |
| 1394 | |
| 1395 | if (self.zig_module_index) |index| { |
| 1396 | mark(self, index); |
| 1397 | } |
| 1398 | |
| 1399 | for (self.objects.items) |index| { |
| 1400 | mark(self, index); |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | fn claimUnresolved(self: *Elf) void { |
| 1405 | if (self.zig_module_index) |index| { |
| 1406 | const zig_module = self.file(index).?.zig_module; |
| 1407 | zig_module.claimUnresolved(self); |
| 1408 | } |
| 1409 | for (self.objects.items) |index| { |
| 1410 | const object = self.file(index).?.object; |
| 1411 | object.claimUnresolved(self); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | /// In scanRelocs we will go over all live atoms and scan their relocs. |
| 1416 | /// This will help us work out what synthetics to emit, GOT indirection, etc. |
| 1417 | /// This is also the point where we will report undefined symbols for any |
| 1418 | /// alloc sections. |
| 1419 | fn scanRelocs(self: *Elf) !void { |
| 1420 | const gpa = self.base.allocator; |
| 1421 | |
| 1422 | var undefs = std.AutoHashMap(Symbol.Index, std.ArrayList(Atom.Index)).init(gpa); |
| 1423 | defer { |
| 1424 | var it = undefs.iterator(); |
| 1425 | while (it.next()) |entry| { |
| 1426 | entry.value_ptr.deinit(); |
| 1427 | } |
| 1428 | undefs.deinit(); |
| 1429 | } |
| 1430 | |
| 1431 | if (self.zig_module_index) |index| { |
| 1432 | const zig_module = self.file(index).?.zig_module; |
| 1433 | try zig_module.scanRelocs(self, &undefs); |
| 1434 | } |
| 1435 | for (self.objects.items) |index| { |
| 1436 | const object = self.file(index).?.object; |
| 1437 | try object.scanRelocs(self, &undefs); |
| 1438 | } |
| 1439 | |
| 1440 | try self.reportUndefined(&undefs); |
| 1441 | |
| 1442 | for (self.symbols.items) |*sym| { |
| 1443 | if (sym.flags.needs_got) { |
| 1444 | log.debug("'{s}' needs GOT", .{sym.name(self)}); |
| 1445 | // TODO how can we tell we need to write it again, aka the entry is dirty? |
| 1446 | const gop = try sym.getOrCreateGotEntry(self); |
| 1447 | try self.got.writeEntry(self, gop.index); |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | fn allocateObjects(self: *Elf) !void { |
| 1453 | for (self.objects.items) |index| { |
| 1454 | const object = self.file(index).?.object; |
| 1455 | for (object.atoms.items) |atom_index| { |
| 1456 | const atom_ptr = self.atom(atom_index) orelse continue; |
| 1457 | if (!atom_ptr.alive) continue; |
| 1458 | try atom_ptr.allocate(self); |
| 1459 | } |
| 1460 | |
| 1461 | for (object.locals()) |local_index| { |
| 1462 | const local = self.symbol(local_index); |
| 1463 | const atom_ptr = local.atom(self) orelse continue; |
| 1464 | if (!atom_ptr.alive) continue; |
| 1465 | local.value = atom_ptr.value; |
| 1466 | } |
| 1467 | |
| 1468 | for (object.globals()) |global_index| { |
| 1469 | const global = self.symbol(global_index); |
| 1470 | const atom_ptr = global.atom(self) orelse continue; |
| 1471 | if (!atom_ptr.alive) continue; |
| 1472 | if (global.file_index == index) { |
| 1473 | global.value = atom_ptr.value; |
| 1474 | } |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | fn writeObjects(self: *Elf) !void { |
| 1480 | const gpa = self.base.allocator; |
| 1481 | |
| 1482 | for (self.objects.items) |index| { |
| 1483 | const object = self.file(index).?.object; |
| 1484 | for (object.atoms.items) |atom_index| { |
| 1485 | const atom_ptr = self.atom(atom_index) orelse continue; |
| 1486 | if (!atom_ptr.alive) continue; |
| 1487 | |
| 1488 | const shdr = &self.shdrs.items[atom_ptr.output_section_index]; |
| 1489 | const file_offset = shdr.sh_offset + atom_ptr.value - shdr.sh_addr; |
| 1490 | const code = try atom_ptr.codeInObjectUncompressAlloc(self); |
| 1491 | defer gpa.free(code); |
| 1492 | |
| 1493 | try atom_ptr.resolveRelocs(self, code); |
| 1494 | try self.base.file.?.pwriteAll(code, file_offset); |
| 1495 | } |
| 1496 | } |
| 1342 | 1497 | } |
| 1343 | 1498 | |
| 1344 | 1499 | fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| ... | ... | @@ -1563,9 +1718,9 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v |
| 1563 | 1718 | try argv.append(entry); |
| 1564 | 1719 | } |
| 1565 | 1720 | |
| 1566 | | for (self.base.options.force_undefined_symbols.keys()) |symbol| { |
| 1721 | for (self.base.options.force_undefined_symbols.keys()) |sym| { |
| 1567 | 1722 | try argv.append("-u"); |
| 1568 | | try argv.append(symbol); |
| 1723 | try argv.append(sym); |
| 1569 | 1724 | } |
| 1570 | 1725 | |
| 1571 | 1726 | switch (self.base.options.hash_style) { |
| ... | ... | @@ -2092,7 +2247,7 @@ fn writeElfHeader(self: *Elf) !void { |
| 2092 | 2247 | |
| 2093 | 2248 | const e_entry = if (elf_type == .REL) 0 else self.entry_addr.?; |
| 2094 | 2249 | |
| 2095 | | const phdr_table_offset = self.program_headers.items[self.phdr_table_index.?].p_offset; |
| 2250 | const phdr_table_offset = self.phdrs.items[self.phdr_table_index.?].p_offset; |
| 2096 | 2251 | switch (self.ptr_width) { |
| 2097 | 2252 | .p32 => { |
| 2098 | 2253 | mem.writeInt(u32, hdr_buf[index..][0..4], @as(u32, @intCast(e_entry)), endian); |
| ... | ... | @@ -2139,7 +2294,7 @@ fn writeElfHeader(self: *Elf) !void { |
| 2139 | 2294 | mem.writeInt(u16, hdr_buf[index..][0..2], e_phentsize, endian); |
| 2140 | 2295 | index += 2; |
| 2141 | 2296 | |
| 2142 | | const e_phnum = @as(u16, @intCast(self.program_headers.items.len)); |
| 2297 | const e_phnum = @as(u16, @intCast(self.phdrs.items.len)); |
| 2143 | 2298 | mem.writeInt(u16, hdr_buf[index..][0..2], e_phnum, endian); |
| 2144 | 2299 | index += 2; |
| 2145 | 2300 | |
| ... | ... | @@ -2150,7 +2305,7 @@ fn writeElfHeader(self: *Elf) !void { |
| 2150 | 2305 | mem.writeInt(u16, hdr_buf[index..][0..2], e_shentsize, endian); |
| 2151 | 2306 | index += 2; |
| 2152 | 2307 | |
| 2153 | | const e_shnum = @as(u16, @intCast(self.sections.slice().len)); |
| 2308 | const e_shnum = @as(u16, @intCast(self.shdrs.items.len)); |
| 2154 | 2309 | mem.writeInt(u16, hdr_buf[index..][0..2], e_shnum, endian); |
| 2155 | 2310 | index += 2; |
| 2156 | 2311 | |
| ... | ... | @@ -2162,268 +2317,23 @@ fn writeElfHeader(self: *Elf) !void { |
| 2162 | 2317 | try self.base.file.?.pwriteAll(hdr_buf[0..index], 0); |
| 2163 | 2318 | } |
| 2164 | 2319 | |
| 2165 | | fn freeAtom(self: *Elf, atom_index: Atom.Index) void { |
| 2166 | | const atom = self.getAtom(atom_index); |
| 2167 | | log.debug("freeAtom {d} ({s})", .{ atom_index, atom.getName(self) }); |
| 2168 | | |
| 2169 | | Atom.freeRelocations(self, atom_index); |
| 2170 | | |
| 2171 | | const gpa = self.base.allocator; |
| 2172 | | const shndx = atom.getSymbol(self).st_shndx; |
| 2173 | | const free_list = &self.sections.items(.free_list)[shndx]; |
| 2174 | | var already_have_free_list_node = false; |
| 2175 | | { |
| 2176 | | var i: usize = 0; |
| 2177 | | // TODO turn free_list into a hash map |
| 2178 | | while (i < free_list.items.len) { |
| 2179 | | if (free_list.items[i] == atom_index) { |
| 2180 | | _ = free_list.swapRemove(i); |
| 2181 | | continue; |
| 2182 | | } |
| 2183 | | if (free_list.items[i] == atom.prev_index) { |
| 2184 | | already_have_free_list_node = true; |
| 2185 | | } |
| 2186 | | i += 1; |
| 2187 | | } |
| 2188 | | } |
| 2189 | | |
| 2190 | | const maybe_last_atom_index = &self.sections.items(.last_atom_index)[shndx]; |
| 2191 | | if (maybe_last_atom_index.*) |last_atom_index| { |
| 2192 | | if (last_atom_index == atom_index) { |
| 2193 | | if (atom.prev_index) |prev_index| { |
| 2194 | | // TODO shrink the section size here |
| 2195 | | maybe_last_atom_index.* = prev_index; |
| 2196 | | } else { |
| 2197 | | maybe_last_atom_index.* = null; |
| 2198 | | } |
| 2199 | | } |
| 2200 | | } |
| 2201 | | |
| 2202 | | if (atom.prev_index) |prev_index| { |
| 2203 | | const prev = self.getAtomPtr(prev_index); |
| 2204 | | prev.next_index = atom.next_index; |
| 2205 | | |
| 2206 | | if (!already_have_free_list_node and prev.*.freeListEligible(self)) { |
| 2207 | | // The free list is heuristics, it doesn't have to be perfect, so we can |
| 2208 | | // ignore the OOM here. |
| 2209 | | free_list.append(gpa, prev_index) catch {}; |
| 2210 | | } |
| 2211 | | } else { |
| 2212 | | self.getAtomPtr(atom_index).prev_index = null; |
| 2213 | | } |
| 2214 | | |
| 2215 | | if (atom.next_index) |next_index| { |
| 2216 | | self.getAtomPtr(next_index).prev_index = atom.prev_index; |
| 2217 | | } else { |
| 2218 | | self.getAtomPtr(atom_index).next_index = null; |
| 2219 | | } |
| 2220 | | |
| 2221 | | // Appending to free lists is allowed to fail because the free lists are heuristics based anyway. |
| 2222 | | const local_sym_index = atom.getSymbolIndex().?; |
| 2223 | | |
| 2224 | | log.debug("adding %{d} to local symbols free list", .{local_sym_index}); |
| 2225 | | self.local_symbol_free_list.append(gpa, local_sym_index) catch {}; |
| 2226 | | self.local_symbols.items[local_sym_index] = .{ |
| 2227 | | .st_name = 0, |
| 2228 | | .st_info = 0, |
| 2229 | | .st_other = 0, |
| 2230 | | .st_shndx = 0, |
| 2231 | | .st_value = 0, |
| 2232 | | .st_size = 0, |
| 2233 | | }; |
| 2234 | | _ = self.atom_by_index_table.remove(local_sym_index); |
| 2235 | | self.getAtomPtr(atom_index).local_sym_index = 0; |
| 2236 | | |
| 2237 | | self.got_table.freeEntry(gpa, local_sym_index); |
| 2238 | | } |
| 2239 | | |
| 2240 | | fn shrinkAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64) void { |
| 2241 | | _ = self; |
| 2242 | | _ = atom_index; |
| 2243 | | _ = new_block_size; |
| 2244 | | } |
| 2245 | | |
| 2246 | | fn growAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 { |
| 2247 | | const atom = self.getAtom(atom_index); |
| 2248 | | const sym = atom.getSymbol(self); |
| 2249 | | const align_ok = mem.alignBackward(u64, sym.st_value, alignment) == sym.st_value; |
| 2250 | | const need_realloc = !align_ok or new_block_size > atom.capacity(self); |
| 2251 | | if (!need_realloc) return sym.st_value; |
| 2252 | | return self.allocateAtom(atom_index, new_block_size, alignment); |
| 2253 | | } |
| 2254 | | |
| 2255 | | pub fn createAtom(self: *Elf) !Atom.Index { |
| 2256 | | const gpa = self.base.allocator; |
| 2257 | | const atom_index = @as(Atom.Index, @intCast(self.atoms.items.len)); |
| 2258 | | const atom = try self.atoms.addOne(gpa); |
| 2259 | | const local_sym_index = try self.allocateLocalSymbol(); |
| 2260 | | try self.atom_by_index_table.putNoClobber(gpa, local_sym_index, atom_index); |
| 2261 | | atom.* = .{ |
| 2262 | | .local_sym_index = local_sym_index, |
| 2263 | | .prev_index = null, |
| 2264 | | .next_index = null, |
| 2265 | | }; |
| 2266 | | log.debug("creating ATOM(%{d}) at index {d}", .{ local_sym_index, atom_index }); |
| 2267 | | return atom_index; |
| 2268 | | } |
| 2269 | | |
| 2270 | | fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 { |
| 2271 | | const atom = self.getAtom(atom_index); |
| 2272 | | const sym = atom.getSymbol(self); |
| 2273 | | const phdr_index = self.sections.items(.phdr_index)[sym.st_shndx]; |
| 2274 | | const phdr = &self.program_headers.items[phdr_index]; |
| 2275 | | const shdr = &self.sections.items(.shdr)[sym.st_shndx]; |
| 2276 | | const free_list = &self.sections.items(.free_list)[sym.st_shndx]; |
| 2277 | | const maybe_last_atom_index = &self.sections.items(.last_atom_index)[sym.st_shndx]; |
| 2278 | | const new_atom_ideal_capacity = padToIdeal(new_block_size); |
| 2279 | | |
| 2280 | | // We use these to indicate our intention to update metadata, placing the new atom, |
| 2281 | | // and possibly removing a free list node. |
| 2282 | | // It would be simpler to do it inside the for loop below, but that would cause a |
| 2283 | | // problem if an error was returned later in the function. So this action |
| 2284 | | // is actually carried out at the end of the function, when errors are no longer possible. |
| 2285 | | var atom_placement: ?Atom.Index = null; |
| 2286 | | var free_list_removal: ?usize = null; |
| 2287 | | |
| 2288 | | // First we look for an appropriately sized free list node. |
| 2289 | | // The list is unordered. We'll just take the first thing that works. |
| 2290 | | const vaddr = blk: { |
| 2291 | | var i: usize = if (self.base.child_pid == null) 0 else free_list.items.len; |
| 2292 | | while (i < free_list.items.len) { |
| 2293 | | const big_atom_index = free_list.items[i]; |
| 2294 | | const big_atom = self.getAtom(big_atom_index); |
| 2295 | | // We now have a pointer to a live atom that has too much capacity. |
| 2296 | | // Is it enough that we could fit this new atom? |
| 2297 | | const big_atom_sym = big_atom.getSymbol(self); |
| 2298 | | const capacity = big_atom.capacity(self); |
| 2299 | | const ideal_capacity = padToIdeal(capacity); |
| 2300 | | const ideal_capacity_end_vaddr = std.math.add(u64, big_atom_sym.st_value, ideal_capacity) catch ideal_capacity; |
| 2301 | | const capacity_end_vaddr = big_atom_sym.st_value + capacity; |
| 2302 | | const new_start_vaddr_unaligned = capacity_end_vaddr - new_atom_ideal_capacity; |
| 2303 | | const new_start_vaddr = mem.alignBackward(u64, new_start_vaddr_unaligned, alignment); |
| 2304 | | if (new_start_vaddr < ideal_capacity_end_vaddr) { |
| 2305 | | // Additional bookkeeping here to notice if this free list node |
| 2306 | | // should be deleted because the block that it points to has grown to take up |
| 2307 | | // more of the extra capacity. |
| 2308 | | if (!big_atom.freeListEligible(self)) { |
| 2309 | | _ = free_list.swapRemove(i); |
| 2310 | | } else { |
| 2311 | | i += 1; |
| 2312 | | } |
| 2313 | | continue; |
| 2314 | | } |
| 2315 | | // At this point we know that we will place the new block here. But the |
| 2316 | | // remaining question is whether there is still yet enough capacity left |
| 2317 | | // over for there to still be a free list node. |
| 2318 | | const remaining_capacity = new_start_vaddr - ideal_capacity_end_vaddr; |
| 2319 | | const keep_free_list_node = remaining_capacity >= min_text_capacity; |
| 2320 | | |
| 2321 | | // Set up the metadata to be updated, after errors are no longer possible. |
| 2322 | | atom_placement = big_atom_index; |
| 2323 | | if (!keep_free_list_node) { |
| 2324 | | free_list_removal = i; |
| 2325 | | } |
| 2326 | | break :blk new_start_vaddr; |
| 2327 | | } else if (maybe_last_atom_index.*) |last_index| { |
| 2328 | | const last = self.getAtom(last_index); |
| 2329 | | const last_sym = last.getSymbol(self); |
| 2330 | | const ideal_capacity = padToIdeal(last_sym.st_size); |
| 2331 | | const ideal_capacity_end_vaddr = last_sym.st_value + ideal_capacity; |
| 2332 | | const new_start_vaddr = mem.alignForward(u64, ideal_capacity_end_vaddr, alignment); |
| 2333 | | // Set up the metadata to be updated, after errors are no longer possible. |
| 2334 | | atom_placement = last_index; |
| 2335 | | break :blk new_start_vaddr; |
| 2336 | | } else { |
| 2337 | | break :blk phdr.p_vaddr; |
| 2338 | | } |
| 2339 | | }; |
| 2340 | | |
| 2341 | | const expand_section = if (atom_placement) |placement_index| |
| 2342 | | self.getAtom(placement_index).next_index == null |
| 2343 | | else |
| 2344 | | true; |
| 2345 | | if (expand_section) { |
| 2346 | | const needed_size = (vaddr + new_block_size) - phdr.p_vaddr; |
| 2347 | | try self.growAllocSection(sym.st_shndx, needed_size); |
| 2348 | | maybe_last_atom_index.* = atom_index; |
| 2349 | | |
| 2350 | | if (self.dwarf) |_| { |
| 2351 | | // The .debug_info section has `low_pc` and `high_pc` values which is the virtual address |
| 2352 | | // range of the compilation unit. When we expand the text section, this range changes, |
| 2353 | | // so the DW_TAG.compile_unit tag of the .debug_info section becomes dirty. |
| 2354 | | self.debug_info_header_dirty = true; |
| 2355 | | // This becomes dirty for the same reason. We could potentially make this more |
| 2356 | | // fine-grained with the addition of support for more compilation units. It is planned to |
| 2357 | | // model each package as a different compilation unit. |
| 2358 | | self.debug_aranges_section_dirty = true; |
| 2359 | | } |
| 2360 | | } |
| 2361 | | shdr.sh_addralign = @max(shdr.sh_addralign, alignment); |
| 2362 | | |
| 2363 | | // This function can also reallocate an atom. |
| 2364 | | // In this case we need to "unplug" it from its previous location before |
| 2365 | | // plugging it in to its new location. |
| 2366 | | if (atom.prev_index) |prev_index| { |
| 2367 | | const prev = self.getAtomPtr(prev_index); |
| 2368 | | prev.next_index = atom.next_index; |
| 2369 | | } |
| 2370 | | if (atom.next_index) |next_index| { |
| 2371 | | const next = self.getAtomPtr(next_index); |
| 2372 | | next.prev_index = atom.prev_index; |
| 2373 | | } |
| 2374 | | |
| 2375 | | if (atom_placement) |big_atom_index| { |
| 2376 | | const big_atom = self.getAtomPtr(big_atom_index); |
| 2377 | | const atom_ptr = self.getAtomPtr(atom_index); |
| 2378 | | atom_ptr.prev_index = big_atom_index; |
| 2379 | | atom_ptr.next_index = big_atom.next_index; |
| 2380 | | big_atom.next_index = atom_index; |
| 2381 | | } else { |
| 2382 | | const atom_ptr = self.getAtomPtr(atom_index); |
| 2383 | | atom_ptr.prev_index = null; |
| 2384 | | atom_ptr.next_index = null; |
| 2385 | | } |
| 2386 | | if (free_list_removal) |i| { |
| 2387 | | _ = free_list.swapRemove(i); |
| 2388 | | } |
| 2389 | | return vaddr; |
| 2390 | | } |
| 2391 | | |
| 2392 | | pub fn allocateLocalSymbol(self: *Elf) !u32 { |
| 2393 | | try self.local_symbols.ensureUnusedCapacity(self.base.allocator, 1); |
| 2394 | | |
| 2395 | | const index = blk: { |
| 2396 | | if (self.local_symbol_free_list.popOrNull()) |index| { |
| 2397 | | log.debug(" (reusing symbol index {d})", .{index}); |
| 2398 | | break :blk index; |
| 2399 | | } else { |
| 2400 | | log.debug(" (allocating symbol index {d})", .{self.local_symbols.items.len}); |
| 2401 | | const index = @as(u32, @intCast(self.local_symbols.items.len)); |
| 2402 | | _ = self.local_symbols.addOneAssumeCapacity(); |
| 2403 | | break :blk index; |
| 2404 | | } |
| 2405 | | }; |
| 2406 | | |
| 2407 | | self.local_symbols.items[index] = .{ |
| 2408 | | .st_name = 0, |
| 2409 | | .st_info = 0, |
| 2410 | | .st_other = 0, |
| 2411 | | .st_shndx = 0, |
| 2412 | | .st_value = 0, |
| 2413 | | .st_size = 0, |
| 2414 | | }; |
| 2415 | | |
| 2416 | | return index; |
| 2417 | | } |
| 2418 | | |
| 2419 | 2320 | fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void { |
| 2420 | | const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return; |
| 2421 | | for (unnamed_consts.items) |atom| { |
| 2422 | | self.freeAtom(atom); |
| 2321 | const unnamed_consts = self.unnamed_consts.getPtr(decl_index) orelse return; |
| 2322 | for (unnamed_consts.items) |sym_index| { |
| 2323 | self.freeDeclMetadata(sym_index); |
| 2423 | 2324 | } |
| 2424 | 2325 | unnamed_consts.clearAndFree(self.base.allocator); |
| 2425 | 2326 | } |
| 2426 | 2327 | |
| 2328 | fn freeDeclMetadata(self: *Elf, sym_index: Symbol.Index) void { |
| 2329 | const sym = self.symbol(sym_index); |
| 2330 | sym.atom(self).?.free(self); |
| 2331 | log.debug("adding %{d} to local symbols free list", .{sym_index}); |
| 2332 | self.symbols_free_list.append(self.base.allocator, sym_index) catch {}; |
| 2333 | self.symbols.items[sym_index] = .{}; |
| 2334 | // TODO free GOT entry here |
| 2335 | } |
| 2336 | |
| 2427 | 2337 | pub fn freeDecl(self: *Elf, decl_index: Module.Decl.Index) void { |
| 2428 | 2338 | if (self.llvm_object) |llvm_object| return llvm_object.freeDecl(decl_index); |
| 2429 | 2339 | |
| ... | ... | @@ -2434,7 +2344,8 @@ pub fn freeDecl(self: *Elf, decl_index: Module.Decl.Index) void { |
| 2434 | 2344 | |
| 2435 | 2345 | if (self.decls.fetchRemove(decl_index)) |const_kv| { |
| 2436 | 2346 | var kv = const_kv; |
| 2437 | | self.freeAtom(kv.value.atom); |
| 2347 | const sym_index = kv.value.symbol_index; |
| 2348 | self.freeDeclMetadata(sym_index); |
| 2438 | 2349 | self.freeUnnamedConsts(decl_index); |
| 2439 | 2350 | kv.value.exports.deinit(self.base.allocator); |
| 2440 | 2351 | } |
| ... | ... | @@ -2444,40 +2355,50 @@ pub fn freeDecl(self: *Elf, decl_index: Module.Decl.Index) void { |
| 2444 | 2355 | } |
| 2445 | 2356 | } |
| 2446 | 2357 | |
| 2447 | | pub fn getOrCreateAtomForLazySymbol(self: *Elf, sym: File.LazySymbol) !Atom.Index { |
| 2358 | pub fn getOrCreateMetadataForLazySymbol(self: *Elf, sym: link.File.LazySymbol) !Symbol.Index { |
| 2448 | 2359 | const mod = self.base.options.module.?; |
| 2449 | 2360 | const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl(mod)); |
| 2450 | 2361 | errdefer _ = if (!gop.found_existing) self.lazy_syms.pop(); |
| 2451 | 2362 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| 2452 | | const metadata: struct { atom: *Atom.Index, state: *LazySymbolMetadata.State } = switch (sym.kind) { |
| 2453 | | .code => .{ .atom = &gop.value_ptr.text_atom, .state = &gop.value_ptr.text_state }, |
| 2454 | | .const_data => .{ .atom = &gop.value_ptr.rodata_atom, .state = &gop.value_ptr.rodata_state }, |
| 2363 | const metadata: struct { |
| 2364 | symbol_index: *Symbol.Index, |
| 2365 | state: *LazySymbolMetadata.State, |
| 2366 | } = switch (sym.kind) { |
| 2367 | .code => .{ |
| 2368 | .symbol_index = &gop.value_ptr.text_symbol_index, |
| 2369 | .state = &gop.value_ptr.text_state, |
| 2370 | }, |
| 2371 | .const_data => .{ |
| 2372 | .symbol_index = &gop.value_ptr.rodata_symbol_index, |
| 2373 | .state = &gop.value_ptr.rodata_state, |
| 2374 | }, |
| 2455 | 2375 | }; |
| 2376 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2456 | 2377 | switch (metadata.state.*) { |
| 2457 | | .unused => metadata.atom.* = try self.createAtom(), |
| 2458 | | .pending_flush => return metadata.atom.*, |
| 2378 | .unused => metadata.symbol_index.* = try zig_module.addAtom(switch (sym.kind) { |
| 2379 | .code => self.text_section_index.?, |
| 2380 | .const_data => self.rodata_section_index.?, |
| 2381 | }, self), |
| 2382 | .pending_flush => return metadata.symbol_index.*, |
| 2459 | 2383 | .flushed => {}, |
| 2460 | 2384 | } |
| 2461 | 2385 | metadata.state.* = .pending_flush; |
| 2462 | | const atom = metadata.atom.*; |
| 2386 | const symbol_index = metadata.symbol_index.*; |
| 2463 | 2387 | // anyerror needs to be deferred until flushModule |
| 2464 | | if (sym.getDecl(mod) != .none) try self.updateLazySymbolAtom(sym, atom, switch (sym.kind) { |
| 2465 | | .code => self.text_section_index.?, |
| 2466 | | .const_data => self.rodata_section_index.?, |
| 2467 | | }); |
| 2468 | | return atom; |
| 2388 | if (sym.getDecl(mod) != .none) try self.updateLazySymbol(sym, symbol_index); |
| 2389 | return symbol_index; |
| 2469 | 2390 | } |
| 2470 | 2391 | |
| 2471 | | pub fn getOrCreateAtomForDecl(self: *Elf, decl_index: Module.Decl.Index) !Atom.Index { |
| 2392 | pub fn getOrCreateMetadataForDecl(self: *Elf, decl_index: Module.Decl.Index) !Symbol.Index { |
| 2472 | 2393 | const gop = try self.decls.getOrPut(self.base.allocator, decl_index); |
| 2473 | 2394 | if (!gop.found_existing) { |
| 2395 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2474 | 2396 | gop.value_ptr.* = .{ |
| 2475 | | .atom = try self.createAtom(), |
| 2476 | | .shdr = self.getDeclShdrIndex(decl_index), |
| 2397 | .symbol_index = try zig_module.addAtom(self.getDeclShdrIndex(decl_index), self), |
| 2477 | 2398 | .exports = .{}, |
| 2478 | 2399 | }; |
| 2479 | 2400 | } |
| 2480 | | return gop.value_ptr.atom; |
| 2401 | return gop.value_ptr.symbol_index; |
| 2481 | 2402 | } |
| 2482 | 2403 | |
| 2483 | 2404 | fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 { |
| ... | ... | @@ -2506,9 +2427,16 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 { |
| 2506 | 2427 | return shdr_index; |
| 2507 | 2428 | } |
| 2508 | 2429 | |
| 2509 | | fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, stt_bits: u8) !*elf.Elf64_Sym { |
| 2430 | fn updateDeclCode( |
| 2431 | self: *Elf, |
| 2432 | decl_index: Module.Decl.Index, |
| 2433 | sym_index: Symbol.Index, |
| 2434 | code: []const u8, |
| 2435 | stt_bits: u8, |
| 2436 | ) !void { |
| 2510 | 2437 | const gpa = self.base.allocator; |
| 2511 | 2438 | const mod = self.base.options.module.?; |
| 2439 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2512 | 2440 | const decl = mod.declPtr(decl_index); |
| 2513 | 2441 | |
| 2514 | 2442 | const decl_name = mod.intern_pool.stringToSlice(try decl.getFullyQualifiedName(mod)); |
| ... | ... | @@ -2516,63 +2444,60 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s |
| 2516 | 2444 | log.debug("updateDeclCode {s}{*}", .{ decl_name, decl }); |
| 2517 | 2445 | const required_alignment = decl.getAlignment(mod); |
| 2518 | 2446 | |
| 2519 | | const decl_metadata = self.decls.get(decl_index).?; |
| 2520 | | const atom_index = decl_metadata.atom; |
| 2521 | | const atom = self.getAtom(atom_index); |
| 2522 | | const local_sym_index = atom.getSymbolIndex().?; |
| 2523 | | |
| 2524 | | const shdr_index = decl_metadata.shdr; |
| 2525 | | if (atom.getSymbol(self).st_size != 0 and self.base.child_pid == null) { |
| 2526 | | const local_sym = atom.getSymbolPtr(self); |
| 2527 | | local_sym.st_name = try self.strtab.insert(gpa, decl_name); |
| 2528 | | local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits; |
| 2529 | | local_sym.st_other = 0; |
| 2530 | | local_sym.st_shndx = shdr_index; |
| 2531 | | |
| 2532 | | const capacity = atom.capacity(self); |
| 2533 | | const need_realloc = code.len > capacity or |
| 2534 | | !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment); |
| 2535 | | |
| 2447 | const sym = self.symbol(sym_index); |
| 2448 | const esym = &zig_module.local_esyms.items[sym.esym_index]; |
| 2449 | const atom_ptr = sym.atom(self).?; |
| 2450 | const shdr_index = sym.output_section_index; |
| 2451 | |
| 2452 | sym.name_offset = try self.strtab.insert(gpa, decl_name); |
| 2453 | atom_ptr.alive = true; |
| 2454 | atom_ptr.name_offset = sym.name_offset; |
| 2455 | esym.st_name = sym.name_offset; |
| 2456 | esym.st_info |= stt_bits; |
| 2457 | esym.st_size = code.len; |
| 2458 | |
| 2459 | const old_size = atom_ptr.size; |
| 2460 | const old_vaddr = atom_ptr.value; |
| 2461 | atom_ptr.alignment = math.log2_int(u64, required_alignment); |
| 2462 | atom_ptr.size = code.len; |
| 2463 | |
| 2464 | if (old_size > 0 and self.base.child_pid == null) { |
| 2465 | const capacity = atom_ptr.capacity(self); |
| 2466 | const need_realloc = code.len > capacity or !mem.isAlignedGeneric(u64, sym.value, required_alignment); |
| 2536 | 2467 | if (need_realloc) { |
| 2537 | | const vaddr = try self.growAtom(atom_index, code.len, required_alignment); |
| 2538 | | log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl_name, local_sym.st_value, vaddr }); |
| 2539 | | if (vaddr != local_sym.st_value) { |
| 2540 | | local_sym.st_value = vaddr; |
| 2468 | try atom_ptr.grow(self); |
| 2469 | log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl_name, old_vaddr, atom_ptr.value }); |
| 2470 | if (old_vaddr != atom_ptr.value) { |
| 2471 | sym.value = atom_ptr.value; |
| 2472 | esym.st_value = atom_ptr.value; |
| 2541 | 2473 | |
| 2542 | 2474 | log.debug(" (writing new offset table entry)", .{}); |
| 2543 | | const got_entry_index = self.got_table.lookup.get(local_sym_index).?; |
| 2544 | | self.got_table.entries.items[got_entry_index] = local_sym_index; |
| 2545 | | try self.writeOffsetTableEntry(got_entry_index); |
| 2475 | const extra = sym.extra(self).?; |
| 2476 | try self.got.writeEntry(self, extra.got); |
| 2546 | 2477 | } |
| 2547 | | } else if (code.len < local_sym.st_size) { |
| 2548 | | self.shrinkAtom(atom_index, code.len); |
| 2478 | } else if (code.len < old_size) { |
| 2479 | atom_ptr.shrink(self); |
| 2549 | 2480 | } |
| 2550 | | local_sym.st_size = code.len; |
| 2551 | 2481 | } else { |
| 2552 | | const local_sym = atom.getSymbolPtr(self); |
| 2553 | | local_sym.* = .{ |
| 2554 | | .st_name = try self.strtab.insert(gpa, decl_name), |
| 2555 | | .st_info = (elf.STB_LOCAL << 4) | stt_bits, |
| 2556 | | .st_other = 0, |
| 2557 | | .st_shndx = shdr_index, |
| 2558 | | .st_value = 0, |
| 2559 | | .st_size = 0, |
| 2560 | | }; |
| 2561 | | const vaddr = try self.allocateAtom(atom_index, code.len, required_alignment); |
| 2562 | | errdefer self.freeAtom(atom_index); |
| 2563 | | log.debug("allocated text block for {s} at 0x{x}", .{ decl_name, vaddr }); |
| 2482 | try atom_ptr.allocate(self); |
| 2483 | errdefer self.freeDeclMetadata(sym_index); |
| 2484 | log.debug("allocated atom for {s} at 0x{x} to 0x{x}", .{ |
| 2485 | decl_name, |
| 2486 | atom_ptr.value, |
| 2487 | atom_ptr.value + atom_ptr.size, |
| 2488 | }); |
| 2564 | 2489 | |
| 2565 | | local_sym.st_value = vaddr; |
| 2566 | | local_sym.st_size = code.len; |
| 2490 | sym.value = atom_ptr.value; |
| 2491 | esym.st_value = atom_ptr.value; |
| 2567 | 2492 | |
| 2568 | | const got_entry_index = try atom.getOrCreateOffsetTableEntry(self); |
| 2569 | | try self.writeOffsetTableEntry(got_entry_index); |
| 2493 | sym.flags.needs_got = true; |
| 2494 | const gop = try sym.getOrCreateGotEntry(self); |
| 2495 | try self.got.writeEntry(self, gop.index); |
| 2570 | 2496 | } |
| 2571 | 2497 | |
| 2572 | | const local_sym = atom.getSymbolPtr(self); |
| 2573 | | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 2574 | | const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr; |
| 2575 | | const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset; |
| 2498 | const phdr_index = self.phdr_to_shdr_table.get(shdr_index).?; |
| 2499 | const section_offset = sym.value - self.phdrs.items[phdr_index].p_vaddr; |
| 2500 | const file_offset = self.shdrs.items[shdr_index].sh_offset + section_offset; |
| 2576 | 2501 | |
| 2577 | 2502 | if (self.base.child_pid) |pid| { |
| 2578 | 2503 | switch (builtin.os.tag) { |
| ... | ... | @@ -2582,7 +2507,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s |
| 2582 | 2507 | .iov_len = code.len, |
| 2583 | 2508 | }}; |
| 2584 | 2509 | var remote_vec: [1]std.os.iovec_const = .{.{ |
| 2585 | | .iov_base = @as([*]u8, @ptrFromInt(@as(usize, @intCast(local_sym.st_value)))), |
| 2510 | .iov_base = @as([*]u8, @ptrFromInt(@as(usize, @intCast(sym.value)))), |
| 2586 | 2511 | .iov_len = code.len, |
| 2587 | 2512 | }}; |
| 2588 | 2513 | const rc = std.os.linux.process_vm_writev(pid, &code_vec, &remote_vec, 0); |
| ... | ... | @@ -2596,8 +2521,6 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s |
| 2596 | 2521 | } |
| 2597 | 2522 | |
| 2598 | 2523 | try self.base.file.?.pwriteAll(code, file_offset); |
| 2599 | | |
| 2600 | | return local_sym; |
| 2601 | 2524 | } |
| 2602 | 2525 | |
| 2603 | 2526 | pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { |
| ... | ... | @@ -2613,9 +2536,9 @@ pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: A |
| 2613 | 2536 | const decl_index = func.owner_decl; |
| 2614 | 2537 | const decl = mod.declPtr(decl_index); |
| 2615 | 2538 | |
| 2616 | | const atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 2539 | const sym_index = try self.getOrCreateMetadataForDecl(decl_index); |
| 2617 | 2540 | self.freeUnnamedConsts(decl_index); |
| 2618 | | Atom.freeRelocations(self, atom_index); |
| 2541 | self.symbol(sym_index).atom(self).?.freeRelocs(self); |
| 2619 | 2542 | |
| 2620 | 2543 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2621 | 2544 | defer code_buffer.deinit(); |
| ... | ... | @@ -2638,13 +2561,14 @@ pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: A |
| 2638 | 2561 | return; |
| 2639 | 2562 | }, |
| 2640 | 2563 | }; |
| 2641 | | const local_sym = try self.updateDeclCode(decl_index, code, elf.STT_FUNC); |
| 2564 | try self.updateDeclCode(decl_index, sym_index, code, elf.STT_FUNC); |
| 2642 | 2565 | if (decl_state) |*ds| { |
| 2566 | const sym = self.symbol(sym_index); |
| 2643 | 2567 | try self.dwarf.?.commitDeclState( |
| 2644 | 2568 | mod, |
| 2645 | 2569 | decl_index, |
| 2646 | | local_sym.st_value, |
| 2647 | | local_sym.st_size, |
| 2570 | sym.value, |
| 2571 | sym.atom(self).?.size, |
| 2648 | 2572 | ds, |
| 2649 | 2573 | ); |
| 2650 | 2574 | } |
| ... | ... | @@ -2658,7 +2582,7 @@ pub fn updateDecl( |
| 2658 | 2582 | self: *Elf, |
| 2659 | 2583 | mod: *Module, |
| 2660 | 2584 | decl_index: Module.Decl.Index, |
| 2661 | | ) File.UpdateDeclError!void { |
| 2585 | ) link.File.UpdateDeclError!void { |
| 2662 | 2586 | if (build_options.skip_non_native and builtin.object_format != .elf) { |
| 2663 | 2587 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 2664 | 2588 | } |
| ... | ... | @@ -2678,9 +2602,8 @@ pub fn updateDecl( |
| 2678 | 2602 | } |
| 2679 | 2603 | } |
| 2680 | 2604 | |
| 2681 | | const atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 2682 | | Atom.freeRelocations(self, atom_index); |
| 2683 | | const atom = self.getAtom(atom_index); |
| 2605 | const sym_index = try self.getOrCreateMetadataForDecl(decl_index); |
| 2606 | self.symbol(sym_index).atom(self).?.freeRelocs(self); |
| 2684 | 2607 | |
| 2685 | 2608 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2686 | 2609 | defer code_buffer.deinit(); |
| ... | ... | @@ -2697,14 +2620,14 @@ pub fn updateDecl( |
| 2697 | 2620 | }, &code_buffer, .{ |
| 2698 | 2621 | .dwarf = ds, |
| 2699 | 2622 | }, .{ |
| 2700 | | .parent_atom_index = atom.getSymbolIndex().?, |
| 2623 | .parent_atom_index = sym_index, |
| 2701 | 2624 | }) |
| 2702 | 2625 | else |
| 2703 | 2626 | try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{ |
| 2704 | 2627 | .ty = decl.ty, |
| 2705 | 2628 | .val = decl_val, |
| 2706 | 2629 | }, &code_buffer, .none, .{ |
| 2707 | | .parent_atom_index = atom.getSymbolIndex().?, |
| 2630 | .parent_atom_index = sym_index, |
| 2708 | 2631 | }); |
| 2709 | 2632 | |
| 2710 | 2633 | const code = switch (res) { |
| ... | ... | @@ -2716,13 +2639,14 @@ pub fn updateDecl( |
| 2716 | 2639 | }, |
| 2717 | 2640 | }; |
| 2718 | 2641 | |
| 2719 | | const local_sym = try self.updateDeclCode(decl_index, code, elf.STT_OBJECT); |
| 2642 | try self.updateDeclCode(decl_index, sym_index, code, elf.STT_OBJECT); |
| 2720 | 2643 | if (decl_state) |*ds| { |
| 2644 | const sym = self.symbol(sym_index); |
| 2721 | 2645 | try self.dwarf.?.commitDeclState( |
| 2722 | 2646 | mod, |
| 2723 | 2647 | decl_index, |
| 2724 | | local_sym.st_value, |
| 2725 | | local_sym.st_size, |
| 2648 | sym.value, |
| 2649 | sym.atom(self).?.size, |
| 2726 | 2650 | ds, |
| 2727 | 2651 | ); |
| 2728 | 2652 | } |
| ... | ... | @@ -2732,14 +2656,10 @@ pub fn updateDecl( |
| 2732 | 2656 | return self.updateDeclExports(mod, decl_index, mod.getDeclExports(decl_index)); |
| 2733 | 2657 | } |
| 2734 | 2658 | |
| 2735 | | fn updateLazySymbolAtom( |
| 2736 | | self: *Elf, |
| 2737 | | sym: File.LazySymbol, |
| 2738 | | atom_index: Atom.Index, |
| 2739 | | shdr_index: u16, |
| 2740 | | ) !void { |
| 2659 | fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol.Index) !void { |
| 2741 | 2660 | const gpa = self.base.allocator; |
| 2742 | 2661 | const mod = self.base.options.module.?; |
| 2662 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2743 | 2663 | |
| 2744 | 2664 | var required_alignment: u32 = undefined; |
| 2745 | 2665 | var code_buffer = std.ArrayList(u8).init(gpa); |
| ... | ... | @@ -2755,9 +2675,6 @@ fn updateLazySymbolAtom( |
| 2755 | 2675 | }; |
| 2756 | 2676 | const name = self.strtab.get(name_str_index).?; |
| 2757 | 2677 | |
| 2758 | | const atom = self.getAtom(atom_index); |
| 2759 | | const local_sym_index = atom.getSymbolIndex().?; |
| 2760 | | |
| 2761 | 2678 | const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl| |
| 2762 | 2679 | mod.declPtr(owner_decl).srcLoc(mod) |
| 2763 | 2680 | else |
| ... | ... | @@ -2773,7 +2690,7 @@ fn updateLazySymbolAtom( |
| 2773 | 2690 | &required_alignment, |
| 2774 | 2691 | &code_buffer, |
| 2775 | 2692 | .none, |
| 2776 | | .{ .parent_atom_index = local_sym_index }, |
| 2693 | .{ .parent_atom_index = symbol_index }, |
| 2777 | 2694 | ); |
| 2778 | 2695 | const code = switch (res) { |
| 2779 | 2696 | .ok => code_buffer.items, |
| ... | ... | @@ -2783,28 +2700,37 @@ fn updateLazySymbolAtom( |
| 2783 | 2700 | }, |
| 2784 | 2701 | }; |
| 2785 | 2702 | |
| 2786 | | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 2787 | | const local_sym = atom.getSymbolPtr(self); |
| 2788 | | local_sym.* = .{ |
| 2789 | | .st_name = name_str_index, |
| 2790 | | .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT, |
| 2791 | | .st_other = 0, |
| 2792 | | .st_shndx = shdr_index, |
| 2793 | | .st_value = 0, |
| 2794 | | .st_size = 0, |
| 2795 | | }; |
| 2796 | | const vaddr = try self.allocateAtom(atom_index, code.len, required_alignment); |
| 2797 | | errdefer self.freeAtom(atom_index); |
| 2798 | | log.debug("allocated text block for {s} at 0x{x}", .{ name, vaddr }); |
| 2703 | const local_sym = self.symbol(symbol_index); |
| 2704 | const phdr_index = self.phdr_to_shdr_table.get(local_sym.output_section_index).?; |
| 2705 | local_sym.name_offset = name_str_index; |
| 2706 | const local_esym = &zig_module.local_esyms.items[local_sym.esym_index]; |
| 2707 | local_esym.st_name = name_str_index; |
| 2708 | local_esym.st_info |= elf.STT_OBJECT; |
| 2709 | local_esym.st_size = code.len; |
| 2710 | const atom_ptr = local_sym.atom(self).?; |
| 2711 | atom_ptr.alive = true; |
| 2712 | atom_ptr.name_offset = name_str_index; |
| 2713 | atom_ptr.alignment = math.log2_int(u64, required_alignment); |
| 2714 | atom_ptr.size = code.len; |
| 2715 | |
| 2716 | try atom_ptr.allocate(self); |
| 2717 | errdefer self.freeDeclMetadata(symbol_index); |
| 2718 | |
| 2719 | log.debug("allocated atom for {s} at 0x{x} to 0x{x}", .{ |
| 2720 | name, |
| 2721 | atom_ptr.value, |
| 2722 | atom_ptr.value + atom_ptr.size, |
| 2723 | }); |
| 2799 | 2724 | |
| 2800 | | local_sym.st_value = vaddr; |
| 2801 | | local_sym.st_size = code.len; |
| 2725 | local_sym.value = atom_ptr.value; |
| 2726 | local_esym.st_value = atom_ptr.value; |
| 2802 | 2727 | |
| 2803 | | const got_entry_index = try atom.getOrCreateOffsetTableEntry(self); |
| 2804 | | try self.writeOffsetTableEntry(got_entry_index); |
| 2728 | local_sym.flags.needs_got = true; |
| 2729 | const gop = try local_sym.getOrCreateGotEntry(self); |
| 2730 | try self.got.writeEntry(self, gop.index); |
| 2805 | 2731 | |
| 2806 | | const section_offset = vaddr - self.program_headers.items[phdr_index].p_vaddr; |
| 2807 | | const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset; |
| 2732 | const section_offset = atom_ptr.value - self.phdrs.items[phdr_index].p_vaddr; |
| 2733 | const file_offset = self.shdrs.items[local_sym.output_section_index].sh_offset + section_offset; |
| 2808 | 2734 | try self.base.file.?.pwriteAll(code, file_offset); |
| 2809 | 2735 | } |
| 2810 | 2736 | |
| ... | ... | @@ -2815,7 +2741,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2815 | 2741 | defer code_buffer.deinit(); |
| 2816 | 2742 | |
| 2817 | 2743 | const mod = self.base.options.module.?; |
| 2818 | | const gop = try self.unnamed_const_atoms.getOrPut(gpa, decl_index); |
| 2744 | const gop = try self.unnamed_consts.getOrPut(gpa, decl_index); |
| 2819 | 2745 | if (!gop.found_existing) { |
| 2820 | 2746 | gop.value_ptr.* = .{}; |
| 2821 | 2747 | } |
| ... | ... | @@ -2831,12 +2757,13 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2831 | 2757 | }; |
| 2832 | 2758 | const name = self.strtab.get(name_str_index).?; |
| 2833 | 2759 | |
| 2834 | | const atom_index = try self.createAtom(); |
| 2760 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2761 | const sym_index = try zig_module.addAtom(self.rodata_section_index.?, self); |
| 2835 | 2762 | |
| 2836 | 2763 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), typed_value, &code_buffer, .{ |
| 2837 | 2764 | .none = {}, |
| 2838 | 2765 | }, .{ |
| 2839 | | .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?, |
| 2766 | .parent_atom_index = sym_index, |
| 2840 | 2767 | }); |
| 2841 | 2768 | const code = switch (res) { |
| 2842 | 2769 | .ok => code_buffer.items, |
| ... | ... | @@ -2850,25 +2777,34 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2850 | 2777 | |
| 2851 | 2778 | const required_alignment = typed_value.ty.abiAlignment(mod); |
| 2852 | 2779 | const shdr_index = self.rodata_section_index.?; |
| 2853 | | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 2854 | | const local_sym = self.getAtom(atom_index).getSymbolPtr(self); |
| 2855 | | local_sym.st_name = name_str_index; |
| 2856 | | local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT; |
| 2857 | | local_sym.st_other = 0; |
| 2858 | | local_sym.st_shndx = shdr_index; |
| 2859 | | local_sym.st_size = code.len; |
| 2860 | | local_sym.st_value = try self.allocateAtom(atom_index, code.len, required_alignment); |
| 2861 | | errdefer self.freeAtom(atom_index); |
| 2862 | | |
| 2863 | | log.debug("allocated text block for {s} at 0x{x}", .{ name, local_sym.st_value }); |
| 2864 | | |
| 2865 | | try unnamed_consts.append(gpa, atom_index); |
| 2866 | | |
| 2867 | | const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr; |
| 2868 | | const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset; |
| 2780 | const phdr_index = self.phdr_to_shdr_table.get(shdr_index).?; |
| 2781 | const local_sym = self.symbol(sym_index); |
| 2782 | local_sym.name_offset = name_str_index; |
| 2783 | const local_esym = &zig_module.local_esyms.items[local_sym.esym_index]; |
| 2784 | local_esym.st_name = name_str_index; |
| 2785 | local_esym.st_info |= elf.STT_OBJECT; |
| 2786 | local_esym.st_size = code.len; |
| 2787 | const atom_ptr = local_sym.atom(self).?; |
| 2788 | atom_ptr.alive = true; |
| 2789 | atom_ptr.name_offset = name_str_index; |
| 2790 | atom_ptr.alignment = math.log2_int(u64, required_alignment); |
| 2791 | atom_ptr.size = code.len; |
| 2792 | |
| 2793 | try atom_ptr.allocate(self); |
| 2794 | errdefer self.freeDeclMetadata(sym_index); |
| 2795 | |
| 2796 | log.debug("allocated atom for {s} at 0x{x} to 0x{x}", .{ name, atom_ptr.value, atom_ptr.value + atom_ptr.size }); |
| 2797 | |
| 2798 | local_sym.value = atom_ptr.value; |
| 2799 | local_esym.st_value = atom_ptr.value; |
| 2800 | |
| 2801 | try unnamed_consts.append(gpa, atom_ptr.atom_index); |
| 2802 | |
| 2803 | const section_offset = atom_ptr.value - self.phdrs.items[phdr_index].p_vaddr; |
| 2804 | const file_offset = self.shdrs.items[shdr_index].sh_offset + section_offset; |
| 2869 | 2805 | try self.base.file.?.pwriteAll(code, file_offset); |
| 2870 | 2806 | |
| 2871 | | return self.getAtom(atom_index).getSymbolIndex().?; |
| 2807 | return sym_index; |
| 2872 | 2808 | } |
| 2873 | 2809 | |
| 2874 | 2810 | pub fn updateDeclExports( |
| ... | ... | @@ -2876,7 +2812,7 @@ pub fn updateDeclExports( |
| 2876 | 2812 | mod: *Module, |
| 2877 | 2813 | decl_index: Module.Decl.Index, |
| 2878 | 2814 | exports: []const *Module.Export, |
| 2879 | | ) File.UpdateDeclExportsError!void { |
| 2815 | ) link.File.UpdateDeclExportsError!void { |
| 2880 | 2816 | if (build_options.skip_non_native and builtin.object_format != .elf) { |
| 2881 | 2817 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 2882 | 2818 | } |
| ... | ... | @@ -2889,14 +2825,12 @@ pub fn updateDeclExports( |
| 2889 | 2825 | |
| 2890 | 2826 | const gpa = self.base.allocator; |
| 2891 | 2827 | |
| 2828 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2892 | 2829 | const decl = mod.declPtr(decl_index); |
| 2893 | | const atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 2894 | | const atom = self.getAtom(atom_index); |
| 2895 | | const decl_sym = atom.getSymbol(self); |
| 2830 | const decl_sym_index = try self.getOrCreateMetadataForDecl(decl_index); |
| 2831 | const decl_sym = self.symbol(decl_sym_index); |
| 2832 | const decl_esym = zig_module.local_esyms.items[decl_sym.esym_index]; |
| 2896 | 2833 | const decl_metadata = self.decls.getPtr(decl_index).?; |
| 2897 | | const shdr_index = decl_metadata.shdr; |
| 2898 | | |
| 2899 | | try self.global_symbols.ensureUnusedCapacity(gpa, exports.len); |
| 2900 | 2834 | |
| 2901 | 2835 | for (exports) |exp| { |
| 2902 | 2836 | const exp_name = mod.intern_pool.stringToSlice(exp.opts.name); |
| ... | ... | @@ -2905,7 +2839,7 @@ pub fn updateDeclExports( |
| 2905 | 2839 | try mod.failed_exports.ensureUnusedCapacity(mod.gpa, 1); |
| 2906 | 2840 | mod.failed_exports.putAssumeCapacityNoClobber( |
| 2907 | 2841 | exp, |
| 2908 | | try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(mod), "Unimplemented: ExportOptions.section", .{}), |
| 2842 | try Module.ErrorMsg.create(gpa, decl.srcLoc(mod), "Unimplemented: ExportOptions.section", .{}), |
| 2909 | 2843 | ); |
| 2910 | 2844 | continue; |
| 2911 | 2845 | } |
| ... | ... | @@ -2915,7 +2849,7 @@ pub fn updateDeclExports( |
| 2915 | 2849 | .Strong => blk: { |
| 2916 | 2850 | const entry_name = self.base.options.entry orelse "_start"; |
| 2917 | 2851 | if (mem.eql(u8, exp_name, entry_name)) { |
| 2918 | | self.entry_addr = decl_sym.st_value; |
| 2852 | self.entry_addr = decl_sym.value; |
| 2919 | 2853 | } |
| 2920 | 2854 | break :blk elf.STB_GLOBAL; |
| 2921 | 2855 | }, |
| ... | ... | @@ -2924,37 +2858,30 @@ pub fn updateDeclExports( |
| 2924 | 2858 | try mod.failed_exports.ensureUnusedCapacity(mod.gpa, 1); |
| 2925 | 2859 | mod.failed_exports.putAssumeCapacityNoClobber( |
| 2926 | 2860 | exp, |
| 2927 | | try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(mod), "Unimplemented: GlobalLinkage.LinkOnce", .{}), |
| 2861 | try Module.ErrorMsg.create(gpa, decl.srcLoc(mod), "Unimplemented: GlobalLinkage.LinkOnce", .{}), |
| 2928 | 2862 | ); |
| 2929 | 2863 | continue; |
| 2930 | 2864 | }, |
| 2931 | 2865 | }; |
| 2932 | | const stt_bits: u8 = @as(u4, @truncate(decl_sym.st_info)); |
| 2933 | | if (decl_metadata.getExport(self, exp_name)) |i| { |
| 2934 | | const sym = &self.global_symbols.items[i]; |
| 2935 | | sym.* = .{ |
| 2936 | | .st_name = try self.strtab.insert(gpa, exp_name), |
| 2937 | | .st_info = (stb_bits << 4) | stt_bits, |
| 2938 | | .st_other = 0, |
| 2939 | | .st_shndx = shdr_index, |
| 2940 | | .st_value = decl_sym.st_value, |
| 2941 | | .st_size = decl_sym.st_size, |
| 2942 | | }; |
| 2943 | | } else { |
| 2944 | | const i = if (self.global_symbol_free_list.popOrNull()) |i| i else blk: { |
| 2945 | | _ = self.global_symbols.addOneAssumeCapacity(); |
| 2946 | | break :blk self.global_symbols.items.len - 1; |
| 2947 | | }; |
| 2948 | | try decl_metadata.exports.append(gpa, @as(u32, @intCast(i))); |
| 2949 | | self.global_symbols.items[i] = .{ |
| 2950 | | .st_name = try self.strtab.insert(gpa, exp_name), |
| 2951 | | .st_info = (stb_bits << 4) | stt_bits, |
| 2952 | | .st_other = 0, |
| 2953 | | .st_shndx = shdr_index, |
| 2954 | | .st_value = decl_sym.st_value, |
| 2955 | | .st_size = decl_sym.st_size, |
| 2956 | | }; |
| 2957 | | } |
| 2866 | const stt_bits: u8 = @as(u4, @truncate(decl_esym.st_info)); |
| 2867 | |
| 2868 | const name_off = try self.strtab.insert(gpa, exp_name); |
| 2869 | const sym_index = if (decl_metadata.@"export"(self, exp_name)) |exp_index| exp_index.* else blk: { |
| 2870 | const sym_index = try zig_module.addGlobalEsym(gpa); |
| 2871 | const lookup_gop = try zig_module.globals_lookup.getOrPut(gpa, name_off); |
| 2872 | const esym = zig_module.elfSym(sym_index); |
| 2873 | esym.st_name = name_off; |
| 2874 | lookup_gop.value_ptr.* = sym_index; |
| 2875 | try decl_metadata.exports.append(gpa, sym_index); |
| 2876 | const gop = try self.getOrPutGlobal(name_off); |
| 2877 | try zig_module.global_symbols.append(gpa, gop.index); |
| 2878 | break :blk sym_index; |
| 2879 | }; |
| 2880 | const esym = &zig_module.global_esyms.items[sym_index & 0x0fffffff]; |
| 2881 | esym.st_value = decl_sym.value; |
| 2882 | esym.st_shndx = decl_sym.atom_index; |
| 2883 | esym.st_info = (stb_bits << 4) | stt_bits; |
| 2884 | esym.st_name = name_off; |
| 2958 | 2885 | } |
| 2959 | 2886 | } |
| 2960 | 2887 | |
| ... | ... | @@ -2982,124 +2909,224 @@ pub fn deleteDeclExport( |
| 2982 | 2909 | if (self.llvm_object) |_| return; |
| 2983 | 2910 | const metadata = self.decls.getPtr(decl_index) orelse return; |
| 2984 | 2911 | const mod = self.base.options.module.?; |
| 2985 | | const sym_index = metadata.getExportPtr(self, mod.intern_pool.stringToSlice(name)) orelse return; |
| 2986 | | self.global_symbol_free_list.append(self.base.allocator, sym_index.*) catch {}; |
| 2987 | | self.global_symbols.items[sym_index.*].st_info = 0; |
| 2988 | | sym_index.* = 0; |
| 2912 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2913 | const exp_name = mod.intern_pool.stringToSlice(name); |
| 2914 | const esym_index = metadata.@"export"(self, exp_name) orelse return; |
| 2915 | log.debug("deleting export '{s}'", .{exp_name}); |
| 2916 | const esym = &zig_module.global_esyms.items[esym_index.*]; |
| 2917 | _ = zig_module.globals_lookup.remove(esym.st_name); |
| 2918 | const sym_index = self.resolver.get(esym.st_name).?; |
| 2919 | const sym = self.symbol(sym_index); |
| 2920 | if (sym.file_index == zig_module.index) { |
| 2921 | _ = self.resolver.swapRemove(esym.st_name); |
| 2922 | sym.* = .{}; |
| 2923 | } |
| 2924 | esym.* = null_sym; |
| 2989 | 2925 | } |
| 2990 | 2926 | |
| 2991 | | fn writeProgHeader(self: *Elf, index: usize) !void { |
| 2992 | | const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian(); |
| 2993 | | const offset = self.program_headers.items[index].p_offset; |
| 2994 | | switch (self.ptr_width) { |
| 2995 | | .p32 => { |
| 2996 | | var phdr = [1]elf.Elf32_Phdr{progHeaderTo32(self.program_headers.items[index])}; |
| 2997 | | if (foreign_endian) { |
| 2998 | | mem.byteSwapAllFields(elf.Elf32_Phdr, &phdr[0]); |
| 2999 | | } |
| 3000 | | return self.base.file.?.pwriteAll(mem.sliceAsBytes(&phdr), offset); |
| 3001 | | }, |
| 3002 | | .p64 => { |
| 3003 | | var phdr = [1]elf.Elf64_Phdr{self.program_headers.items[index]}; |
| 3004 | | if (foreign_endian) { |
| 3005 | | mem.byteSwapAllFields(elf.Elf64_Phdr, &phdr[0]); |
| 3006 | | } |
| 3007 | | return self.base.file.?.pwriteAll(mem.sliceAsBytes(&phdr), offset); |
| 3008 | | }, |
| 3009 | | } |
| 2927 | fn addLinkerDefinedSymbols(self: *Elf) !void { |
| 2928 | const linker_defined_index = self.linker_defined_index orelse return; |
| 2929 | const linker_defined = self.file(linker_defined_index).?.linker_defined; |
| 2930 | self.dynamic_index = try linker_defined.addGlobal("_DYNAMIC", self); |
| 2931 | self.ehdr_start_index = try linker_defined.addGlobal("__ehdr_start", self); |
| 2932 | self.init_array_start_index = try linker_defined.addGlobal("__init_array_start", self); |
| 2933 | self.init_array_end_index = try linker_defined.addGlobal("__init_array_end", self); |
| 2934 | self.fini_array_start_index = try linker_defined.addGlobal("__fini_array_start", self); |
| 2935 | self.fini_array_end_index = try linker_defined.addGlobal("__fini_array_end", self); |
| 2936 | self.preinit_array_start_index = try linker_defined.addGlobal("__preinit_array_start", self); |
| 2937 | self.preinit_array_end_index = try linker_defined.addGlobal("__preinit_array_end", self); |
| 2938 | self.got_index = try linker_defined.addGlobal("_GLOBAL_OFFSET_TABLE_", self); |
| 2939 | self.plt_index = try linker_defined.addGlobal("_PROCEDURE_LINKAGE_TABLE_", self); |
| 2940 | self.end_index = try linker_defined.addGlobal("_end", self); |
| 2941 | |
| 2942 | if (self.base.options.eh_frame_hdr) { |
| 2943 | self.gnu_eh_frame_hdr_index = try linker_defined.addGlobal("__GNU_EH_FRAME_HDR", self); |
| 2944 | } |
| 2945 | |
| 2946 | if (self.globalByName("__dso_handle")) |index| { |
| 2947 | if (self.symbol(index).file(self) == null) |
| 2948 | self.dso_handle_index = try linker_defined.addGlobal("__dso_handle", self); |
| 2949 | } |
| 2950 | |
| 2951 | self.rela_iplt_start_index = try linker_defined.addGlobal("__rela_iplt_start", self); |
| 2952 | self.rela_iplt_end_index = try linker_defined.addGlobal("__rela_iplt_end", self); |
| 2953 | |
| 2954 | // for (self.objects.items) |index| { |
| 2955 | // const object = self.getFile(index).?.object; |
| 2956 | // for (object.atoms.items) |atom_index| { |
| 2957 | // if (self.getStartStopBasename(atom_index)) |name| { |
| 2958 | // const gpa = self.base.allocator; |
| 2959 | // try self.start_stop_indexes.ensureUnusedCapacity(gpa, 2); |
| 2960 | |
| 2961 | // const start = try std.fmt.allocPrintZ(gpa, "__start_{s}", .{name}); |
| 2962 | // defer gpa.free(start); |
| 2963 | // const stop = try std.fmt.allocPrintZ(gpa, "__stop_{s}", .{name}); |
| 2964 | // defer gpa.free(stop); |
| 2965 | |
| 2966 | // self.start_stop_indexes.appendAssumeCapacity(try internal.addSyntheticGlobal(start, self)); |
| 2967 | // self.start_stop_indexes.appendAssumeCapacity(try internal.addSyntheticGlobal(stop, self)); |
| 2968 | // } |
| 2969 | // } |
| 2970 | // } |
| 2971 | |
| 2972 | linker_defined.resolveSymbols(self); |
| 3010 | 2973 | } |
| 3011 | 2974 | |
| 3012 | | fn writeSectHeader(self: *Elf, index: usize) !void { |
| 3013 | | const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian(); |
| 3014 | | switch (self.ptr_width) { |
| 3015 | | .p32 => { |
| 3016 | | var shdr: [1]elf.Elf32_Shdr = undefined; |
| 3017 | | shdr[0] = sectHeaderTo32(self.sections.items(.shdr)[index]); |
| 3018 | | if (foreign_endian) { |
| 3019 | | mem.byteSwapAllFields(elf.Elf32_Shdr, &shdr[0]); |
| 3020 | | } |
| 3021 | | const offset = self.shdr_table_offset.? + index * @sizeOf(elf.Elf32_Shdr); |
| 3022 | | return self.base.file.?.pwriteAll(mem.sliceAsBytes(&shdr), offset); |
| 3023 | | }, |
| 3024 | | .p64 => { |
| 3025 | | var shdr = [1]elf.Elf64_Shdr{self.sections.items(.shdr)[index]}; |
| 3026 | | if (foreign_endian) { |
| 3027 | | mem.byteSwapAllFields(elf.Elf64_Shdr, &shdr[0]); |
| 2975 | fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 2976 | // _DYNAMIC |
| 2977 | if (self.dynamic_section_index) |shndx| { |
| 2978 | const shdr = &self.shdrs.items[shndx]; |
| 2979 | const symbol_ptr = self.symbol(self.dynamic_index.?); |
| 2980 | symbol_ptr.value = shdr.sh_addr; |
| 2981 | symbol_ptr.output_section_index = shndx; |
| 2982 | } |
| 2983 | |
| 2984 | // __ehdr_start |
| 2985 | { |
| 2986 | const symbol_ptr = self.symbol(self.ehdr_start_index.?); |
| 2987 | symbol_ptr.value = self.calcImageBase(); |
| 2988 | symbol_ptr.output_section_index = 1; |
| 2989 | } |
| 2990 | |
| 2991 | // __init_array_start, __init_array_end |
| 2992 | if (self.sectionByName(".init_array")) |shndx| { |
| 2993 | const start_sym = self.symbol(self.init_array_start_index.?); |
| 2994 | const end_sym = self.symbol(self.init_array_end_index.?); |
| 2995 | const shdr = &self.shdrs.items[shndx]; |
| 2996 | start_sym.output_section_index = shndx; |
| 2997 | start_sym.value = shdr.sh_addr; |
| 2998 | end_sym.output_section_index = shndx; |
| 2999 | end_sym.value = shdr.sh_addr + shdr.sh_size; |
| 3000 | } |
| 3001 | |
| 3002 | // __fini_array_start, __fini_array_end |
| 3003 | if (self.sectionByName(".fini_array")) |shndx| { |
| 3004 | const start_sym = self.symbol(self.fini_array_start_index.?); |
| 3005 | const end_sym = self.symbol(self.fini_array_end_index.?); |
| 3006 | const shdr = &self.shdrs.items[shndx]; |
| 3007 | start_sym.output_section_index = shndx; |
| 3008 | start_sym.value = shdr.sh_addr; |
| 3009 | end_sym.output_section_index = shndx; |
| 3010 | end_sym.value = shdr.sh_addr + shdr.sh_size; |
| 3011 | } |
| 3012 | |
| 3013 | // __preinit_array_start, __preinit_array_end |
| 3014 | if (self.sectionByName(".preinit_array")) |shndx| { |
| 3015 | const start_sym = self.symbol(self.preinit_array_start_index.?); |
| 3016 | const end_sym = self.symbol(self.preinit_array_end_index.?); |
| 3017 | const shdr = &self.shdrs.items[shndx]; |
| 3018 | start_sym.output_section_index = shndx; |
| 3019 | start_sym.value = shdr.sh_addr; |
| 3020 | end_sym.output_section_index = shndx; |
| 3021 | end_sym.value = shdr.sh_addr + shdr.sh_size; |
| 3022 | } |
| 3023 | |
| 3024 | // _GLOBAL_OFFSET_TABLE_ |
| 3025 | if (self.got_plt_section_index) |shndx| { |
| 3026 | const shdr = &self.shdrs.items[shndx]; |
| 3027 | const symbol_ptr = self.symbol(self.got_index.?); |
| 3028 | symbol_ptr.value = shdr.sh_addr; |
| 3029 | symbol_ptr.output_section_index = shndx; |
| 3030 | } |
| 3031 | |
| 3032 | // _PROCEDURE_LINKAGE_TABLE_ |
| 3033 | if (self.plt_section_index) |shndx| { |
| 3034 | const shdr = &self.shdrs.items[shndx]; |
| 3035 | const symbol_ptr = self.symbol(self.plt_index.?); |
| 3036 | symbol_ptr.value = shdr.sh_addr; |
| 3037 | symbol_ptr.output_section_index = shndx; |
| 3038 | } |
| 3039 | |
| 3040 | // __dso_handle |
| 3041 | if (self.dso_handle_index) |index| { |
| 3042 | const shdr = &self.shdrs.items[1]; |
| 3043 | const symbol_ptr = self.symbol(index); |
| 3044 | symbol_ptr.value = shdr.sh_addr; |
| 3045 | symbol_ptr.output_section_index = 0; |
| 3046 | } |
| 3047 | |
| 3048 | // __GNU_EH_FRAME_HDR |
| 3049 | if (self.eh_frame_hdr_section_index) |shndx| { |
| 3050 | const shdr = &self.shdrs.items[shndx]; |
| 3051 | const symbol_ptr = self.symbol(self.gnu_eh_frame_hdr_index.?); |
| 3052 | symbol_ptr.value = shdr.sh_addr; |
| 3053 | symbol_ptr.output_section_index = shndx; |
| 3054 | } |
| 3055 | |
| 3056 | // __rela_iplt_start, __rela_iplt_end |
| 3057 | if (self.rela_dyn_section_index) |shndx| blk: { |
| 3058 | if (self.base.options.link_mode != .Static or self.base.options.pie) break :blk; |
| 3059 | const shdr = &self.shdrs.items[shndx]; |
| 3060 | const end_addr = shdr.sh_addr + shdr.sh_size; |
| 3061 | const start_addr = end_addr - self.calcNumIRelativeRelocs() * @sizeOf(elf.Elf64_Rela); |
| 3062 | const start_sym = self.symbol(self.rela_iplt_start_index.?); |
| 3063 | const end_sym = self.symbol(self.rela_iplt_end_index.?); |
| 3064 | start_sym.value = start_addr; |
| 3065 | start_sym.output_section_index = shndx; |
| 3066 | end_sym.value = end_addr; |
| 3067 | end_sym.output_section_index = shndx; |
| 3068 | } |
| 3069 | |
| 3070 | // _end |
| 3071 | { |
| 3072 | const end_symbol = self.symbol(self.end_index.?); |
| 3073 | for (self.shdrs.items, 0..) |*shdr, shndx| { |
| 3074 | if (shdr.sh_flags & elf.SHF_ALLOC != 0) { |
| 3075 | end_symbol.value = shdr.sh_addr + shdr.sh_size; |
| 3076 | end_symbol.output_section_index = @intCast(shndx); |
| 3028 | 3077 | } |
| 3029 | | const offset = self.shdr_table_offset.? + index * @sizeOf(elf.Elf64_Shdr); |
| 3030 | | return self.base.file.?.pwriteAll(mem.sliceAsBytes(&shdr), offset); |
| 3031 | | }, |
| 3078 | } |
| 3079 | } |
| 3080 | |
| 3081 | // __start_*, __stop_* |
| 3082 | { |
| 3083 | var index: usize = 0; |
| 3084 | while (index < self.start_stop_indexes.items.len) : (index += 2) { |
| 3085 | const start = self.symbol(self.start_stop_indexes.items[index]); |
| 3086 | const name = start.name(self); |
| 3087 | const stop = self.symbol(self.start_stop_indexes.items[index + 1]); |
| 3088 | const shndx = self.sectionByName(name["__start_".len..]).?; |
| 3089 | const shdr = &self.shdrs.items[shndx]; |
| 3090 | start.value = shdr.sh_addr; |
| 3091 | start.output_section_index = shndx; |
| 3092 | stop.value = shdr.sh_addr + shdr.sh_size; |
| 3093 | stop.output_section_index = shndx; |
| 3094 | } |
| 3032 | 3095 | } |
| 3033 | 3096 | } |
| 3034 | 3097 | |
| 3035 | | fn writeOffsetTableEntry(self: *Elf, index: @TypeOf(self.got_table).Index) !void { |
| 3036 | | const entry_size: u16 = self.archPtrWidthBytes(); |
| 3037 | | if (self.got_table_count_dirty) { |
| 3038 | | const needed_size = self.got_table.entries.items.len * entry_size; |
| 3039 | | try self.growAllocSection(self.got_section_index.?, needed_size); |
| 3040 | | self.got_table_count_dirty = false; |
| 3098 | fn updateSymtabSize(self: *Elf) !void { |
| 3099 | var sizes = SymtabSize{}; |
| 3100 | |
| 3101 | if (self.zig_module_index) |index| { |
| 3102 | const zig_module = self.file(index).?.zig_module; |
| 3103 | zig_module.updateSymtabSize(self); |
| 3104 | sizes.nlocals += zig_module.output_symtab_size.nlocals; |
| 3105 | sizes.nglobals += zig_module.output_symtab_size.nglobals; |
| 3041 | 3106 | } |
| 3042 | | const endian = self.base.options.target.cpu.arch.endian(); |
| 3043 | | const shdr = &self.sections.items(.shdr)[self.got_section_index.?]; |
| 3044 | | const off = shdr.sh_offset + @as(u64, entry_size) * index; |
| 3045 | | const phdr = &self.program_headers.items[self.phdr_got_index.?]; |
| 3046 | | const vaddr = phdr.p_vaddr + @as(u64, entry_size) * index; |
| 3047 | | const got_entry = self.got_table.entries.items[index]; |
| 3048 | | const got_value = self.getSymbol(got_entry).st_value; |
| 3049 | | switch (entry_size) { |
| 3050 | | 2 => { |
| 3051 | | var buf: [2]u8 = undefined; |
| 3052 | | mem.writeInt(u16, &buf, @as(u16, @intCast(got_value)), endian); |
| 3053 | | try self.base.file.?.pwriteAll(&buf, off); |
| 3054 | | }, |
| 3055 | | 4 => { |
| 3056 | | var buf: [4]u8 = undefined; |
| 3057 | | mem.writeInt(u32, &buf, @as(u32, @intCast(got_value)), endian); |
| 3058 | | try self.base.file.?.pwriteAll(&buf, off); |
| 3059 | | }, |
| 3060 | | 8 => { |
| 3061 | | var buf: [8]u8 = undefined; |
| 3062 | | mem.writeInt(u64, &buf, got_value, endian); |
| 3063 | | try self.base.file.?.pwriteAll(&buf, off); |
| 3064 | | |
| 3065 | | if (self.base.child_pid) |pid| { |
| 3066 | | switch (builtin.os.tag) { |
| 3067 | | .linux => { |
| 3068 | | var local_vec: [1]std.os.iovec_const = .{.{ |
| 3069 | | .iov_base = &buf, |
| 3070 | | .iov_len = buf.len, |
| 3071 | | }}; |
| 3072 | | var remote_vec: [1]std.os.iovec_const = .{.{ |
| 3073 | | .iov_base = @as([*]u8, @ptrFromInt(@as(usize, @intCast(vaddr)))), |
| 3074 | | .iov_len = buf.len, |
| 3075 | | }}; |
| 3076 | | const rc = std.os.linux.process_vm_writev(pid, &local_vec, &remote_vec, 0); |
| 3077 | | switch (std.os.errno(rc)) { |
| 3078 | | .SUCCESS => assert(rc == buf.len), |
| 3079 | | else => |errno| log.warn("process_vm_writev failure: {s}", .{@tagName(errno)}), |
| 3080 | | } |
| 3081 | | }, |
| 3082 | | else => return error.HotSwapUnavailableOnHostOperatingSystem, |
| 3083 | | } |
| 3084 | | } |
| 3085 | | }, |
| 3086 | | else => unreachable, |
| 3107 | |
| 3108 | for (self.objects.items) |index| { |
| 3109 | const object = self.file(index).?.object; |
| 3110 | object.updateSymtabSize(self); |
| 3111 | sizes.nlocals += object.output_symtab_size.nlocals; |
| 3112 | sizes.nglobals += object.output_symtab_size.nglobals; |
| 3087 | 3113 | } |
| 3088 | | } |
| 3089 | 3114 | |
| 3090 | | fn elf32SymFromSym(sym: elf.Elf64_Sym, out: *elf.Elf32_Sym) void { |
| 3091 | | out.* = .{ |
| 3092 | | .st_name = sym.st_name, |
| 3093 | | .st_value = @as(u32, @intCast(sym.st_value)), |
| 3094 | | .st_size = @as(u32, @intCast(sym.st_size)), |
| 3095 | | .st_info = sym.st_info, |
| 3096 | | .st_other = sym.st_other, |
| 3097 | | .st_shndx = sym.st_shndx, |
| 3098 | | }; |
| 3099 | | } |
| 3115 | if (self.got_section_index) |_| { |
| 3116 | self.got.updateSymtabSize(self); |
| 3117 | sizes.nlocals += self.got.output_symtab_size.nlocals; |
| 3118 | } |
| 3119 | |
| 3120 | if (self.linker_defined_index) |index| { |
| 3121 | const linker_defined = self.file(index).?.linker_defined; |
| 3122 | linker_defined.updateSymtabSize(self); |
| 3123 | sizes.nlocals += linker_defined.output_symtab_size.nlocals; |
| 3124 | } |
| 3125 | |
| 3126 | const shdr = &self.shdrs.items[self.symtab_section_index.?]; |
| 3127 | shdr.sh_info = sizes.nlocals + 1; |
| 3128 | self.markDirty(self.symtab_section_index.?, null); |
| 3100 | 3129 | |
| 3101 | | fn writeSymbols(self: *Elf) !void { |
| 3102 | | const gpa = self.base.allocator; |
| 3103 | 3130 | const sym_size: u64 = switch (self.ptr_width) { |
| 3104 | 3131 | .p32 => @sizeOf(elf.Elf32_Sym), |
| 3105 | 3132 | .p64 => @sizeOf(elf.Elf64_Sym), |
| ... | ... | @@ -3108,54 +3135,80 @@ fn writeSymbols(self: *Elf) !void { |
| 3108 | 3135 | .p32 => @alignOf(elf.Elf32_Sym), |
| 3109 | 3136 | .p64 => @alignOf(elf.Elf64_Sym), |
| 3110 | 3137 | }; |
| 3138 | const needed_size = (sizes.nlocals + sizes.nglobals + 1) * sym_size; |
| 3139 | try self.growNonAllocSection(self.symtab_section_index.?, needed_size, sym_align, true); |
| 3140 | } |
| 3111 | 3141 | |
| 3112 | | const shdr = &self.sections.items(.shdr)[self.symtab_section_index.?]; |
| 3113 | | shdr.sh_info = @intCast(self.local_symbols.items.len); |
| 3114 | | self.markDirty(self.symtab_section_index.?, null); |
| 3142 | fn writeSymtab(self: *Elf) !void { |
| 3143 | const gpa = self.base.allocator; |
| 3144 | const shdr = &self.shdrs.items[self.symtab_section_index.?]; |
| 3145 | const sym_size: u64 = switch (self.ptr_width) { |
| 3146 | .p32 => @sizeOf(elf.Elf32_Sym), |
| 3147 | .p64 => @sizeOf(elf.Elf64_Sym), |
| 3148 | }; |
| 3149 | const nsyms = math.cast(usize, @divExact(shdr.sh_size, sym_size)) orelse return error.Overflow; |
| 3115 | 3150 | |
| 3116 | | const nsyms = self.local_symbols.items.len + self.global_symbols.items.len; |
| 3117 | | const needed_size = nsyms * sym_size; |
| 3118 | | try self.growNonAllocSection(self.symtab_section_index.?, needed_size, sym_align, true); |
| 3151 | log.debug("writing {d} symbols at 0x{x}", .{ nsyms, shdr.sh_offset }); |
| 3152 | |
| 3153 | const symtab = try gpa.alloc(elf.Elf64_Sym, nsyms); |
| 3154 | defer gpa.free(symtab); |
| 3155 | symtab[0] = null_sym; |
| 3156 | |
| 3157 | var ctx: struct { ilocal: usize, iglobal: usize, symtab: []elf.Elf64_Sym } = .{ |
| 3158 | .ilocal = 1, |
| 3159 | .iglobal = shdr.sh_info, |
| 3160 | .symtab = symtab, |
| 3161 | }; |
| 3162 | |
| 3163 | if (self.zig_module_index) |index| { |
| 3164 | const zig_module = self.file(index).?.zig_module; |
| 3165 | zig_module.writeSymtab(self, ctx); |
| 3166 | ctx.ilocal += zig_module.output_symtab_size.nlocals; |
| 3167 | ctx.iglobal += zig_module.output_symtab_size.nglobals; |
| 3168 | } |
| 3169 | |
| 3170 | for (self.objects.items) |index| { |
| 3171 | const object = self.file(index).?.object; |
| 3172 | object.writeSymtab(self, ctx); |
| 3173 | ctx.ilocal += object.output_symtab_size.nlocals; |
| 3174 | ctx.iglobal += object.output_symtab_size.nglobals; |
| 3175 | } |
| 3176 | |
| 3177 | if (self.got_section_index) |_| { |
| 3178 | try self.got.writeSymtab(self, ctx); |
| 3179 | ctx.ilocal += self.got.output_symtab_size.nlocals; |
| 3180 | } |
| 3181 | |
| 3182 | if (self.linker_defined_index) |index| { |
| 3183 | const linker_defined = self.file(index).?.linker_defined; |
| 3184 | linker_defined.writeSymtab(self, ctx); |
| 3185 | ctx.ilocal += linker_defined.output_symtab_size.nlocals; |
| 3186 | } |
| 3119 | 3187 | |
| 3120 | 3188 | const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian(); |
| 3121 | | log.debug("writing {d} symbols at 0x{x}", .{ nsyms, shdr.sh_offset }); |
| 3122 | 3189 | switch (self.ptr_width) { |
| 3123 | 3190 | .p32 => { |
| 3124 | | const buf = try gpa.alloc(elf.Elf32_Sym, nsyms); |
| 3191 | const buf = try gpa.alloc(elf.Elf32_Sym, symtab.len); |
| 3125 | 3192 | defer gpa.free(buf); |
| 3126 | 3193 | |
| 3127 | | for (buf[0..self.local_symbols.items.len], self.local_symbols.items) |*sym, local| { |
| 3128 | | elf32SymFromSym(local, sym); |
| 3129 | | if (foreign_endian) { |
| 3130 | | mem.byteSwapAllFields(elf.Elf32_Sym, sym); |
| 3131 | | } |
| 3132 | | } |
| 3133 | | |
| 3134 | | for (buf[self.local_symbols.items.len..], self.global_symbols.items) |*sym, global| { |
| 3135 | | elf32SymFromSym(global, sym); |
| 3136 | | if (foreign_endian) { |
| 3137 | | mem.byteSwapAllFields(elf.Elf32_Sym, sym); |
| 3138 | | } |
| 3194 | for (buf, symtab) |*out, sym| { |
| 3195 | out.* = .{ |
| 3196 | .st_name = sym.st_name, |
| 3197 | .st_info = sym.st_info, |
| 3198 | .st_other = sym.st_other, |
| 3199 | .st_shndx = sym.st_shndx, |
| 3200 | .st_value = @as(u32, @intCast(sym.st_value)), |
| 3201 | .st_size = @as(u32, @intCast(sym.st_size)), |
| 3202 | }; |
| 3203 | if (foreign_endian) mem.byteSwapAllFields(elf.Elf32_Sym, out); |
| 3139 | 3204 | } |
| 3140 | 3205 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), shdr.sh_offset); |
| 3141 | 3206 | }, |
| 3142 | 3207 | .p64 => { |
| 3143 | | const buf = try gpa.alloc(elf.Elf64_Sym, nsyms); |
| 3144 | | defer gpa.free(buf); |
| 3145 | | for (buf[0..self.local_symbols.items.len], self.local_symbols.items) |*sym, local| { |
| 3146 | | sym.* = local; |
| 3147 | | if (foreign_endian) { |
| 3148 | | mem.byteSwapAllFields(elf.Elf64_Sym, sym); |
| 3149 | | } |
| 3150 | | } |
| 3151 | | |
| 3152 | | for (buf[self.local_symbols.items.len..], self.global_symbols.items) |*sym, global| { |
| 3153 | | sym.* = global; |
| 3154 | | if (foreign_endian) { |
| 3155 | | mem.byteSwapAllFields(elf.Elf64_Sym, sym); |
| 3156 | | } |
| 3208 | if (foreign_endian) { |
| 3209 | for (symtab) |*sym| mem.byteSwapAllFields(elf.Elf64_Sym, sym); |
| 3157 | 3210 | } |
| 3158 | | try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), shdr.sh_offset); |
| 3211 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(symtab), shdr.sh_offset); |
| 3159 | 3212 | }, |
| 3160 | 3213 | } |
| 3161 | 3214 | } |
| ... | ... | @@ -3170,11 +3223,11 @@ fn ptrWidthBytes(self: Elf) u8 { |
| 3170 | 3223 | |
| 3171 | 3224 | /// Does not necessarily match `ptrWidthBytes` for example can be 2 bytes |
| 3172 | 3225 | /// in a 32-bit ELF file. |
| 3173 | | fn archPtrWidthBytes(self: Elf) u8 { |
| 3174 | | return @as(u8, @intCast(self.base.options.target.ptrBitWidth() / 8)); |
| 3226 | pub fn archPtrWidthBytes(self: Elf) u8 { |
| 3227 | return @as(u8, @intCast(@divExact(self.base.options.target.ptrBitWidth(), 8))); |
| 3175 | 3228 | } |
| 3176 | 3229 | |
| 3177 | | fn progHeaderTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr { |
| 3230 | fn phdrTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr { |
| 3178 | 3231 | return .{ |
| 3179 | 3232 | .p_type = phdr.p_type, |
| 3180 | 3233 | .p_flags = phdr.p_flags, |
| ... | ... | @@ -3187,7 +3240,30 @@ fn progHeaderTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr { |
| 3187 | 3240 | }; |
| 3188 | 3241 | } |
| 3189 | 3242 | |
| 3190 | | fn sectHeaderTo32(shdr: elf.Elf64_Shdr) elf.Elf32_Shdr { |
| 3243 | fn writeShdr(self: *Elf, index: usize) !void { |
| 3244 | const foreign_endian = self.base.options.target.cpu.arch.endian() != builtin.cpu.arch.endian(); |
| 3245 | switch (self.ptr_width) { |
| 3246 | .p32 => { |
| 3247 | var shdr: [1]elf.Elf32_Shdr = undefined; |
| 3248 | shdr[0] = shdrTo32(self.shdrs.items[index]); |
| 3249 | if (foreign_endian) { |
| 3250 | mem.byteSwapAllFields(elf.Elf32_Shdr, &shdr[0]); |
| 3251 | } |
| 3252 | const offset = self.shdr_table_offset.? + index * @sizeOf(elf.Elf32_Shdr); |
| 3253 | return self.base.file.?.pwriteAll(mem.sliceAsBytes(&shdr), offset); |
| 3254 | }, |
| 3255 | .p64 => { |
| 3256 | var shdr = [1]elf.Elf64_Shdr{self.shdrs.items[index]}; |
| 3257 | if (foreign_endian) { |
| 3258 | mem.byteSwapAllFields(elf.Elf64_Shdr, &shdr[0]); |
| 3259 | } |
| 3260 | const offset = self.shdr_table_offset.? + index * @sizeOf(elf.Elf64_Shdr); |
| 3261 | return self.base.file.?.pwriteAll(mem.sliceAsBytes(&shdr), offset); |
| 3262 | }, |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | fn shdrTo32(shdr: elf.Elf64_Shdr) elf.Elf32_Shdr { |
| 3191 | 3267 | return .{ |
| 3192 | 3268 | .sh_name = shdr.sh_name, |
| 3193 | 3269 | .sh_type = shdr.sh_type, |
| ... | ... | @@ -3448,61 +3524,474 @@ const CsuObjects = struct { |
| 3448 | 3524 | } |
| 3449 | 3525 | }; |
| 3450 | 3526 | |
| 3451 | | fn logSymtab(self: Elf) void { |
| 3452 | | log.debug("locals:", .{}); |
| 3453 | | for (self.local_symbols.items, 0..) |sym, id| { |
| 3454 | | log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx }); |
| 3527 | pub fn calcImageBase(self: Elf) u64 { |
| 3528 | if (self.base.options.pic) return 0; // TODO flag an error if PIC and image_base_override |
| 3529 | return self.base.options.image_base_override orelse switch (self.ptr_width) { |
| 3530 | .p32 => 0x1000, |
| 3531 | .p64 => 0x1000000, |
| 3532 | }; |
| 3533 | } |
| 3534 | |
| 3535 | pub fn defaultEntryAddress(self: Elf) u64 { |
| 3536 | if (self.entry_addr) |addr| return addr; |
| 3537 | return switch (self.base.options.target.cpu.arch) { |
| 3538 | .spu_2 => 0, |
| 3539 | else => default_entry_addr, |
| 3540 | }; |
| 3541 | } |
| 3542 | |
| 3543 | pub fn isDynLib(self: Elf) bool { |
| 3544 | return self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic; |
| 3545 | } |
| 3546 | |
| 3547 | pub fn sectionByName(self: *Elf, name: [:0]const u8) ?u16 { |
| 3548 | for (self.shdrs.items, 0..) |*shdr, i| { |
| 3549 | const this_name = self.shstrtab.getAssumeExists(shdr.sh_name); |
| 3550 | if (mem.eql(u8, this_name, name)) return @as(u16, @intCast(i)); |
| 3551 | } else return null; |
| 3552 | } |
| 3553 | |
| 3554 | pub fn calcNumIRelativeRelocs(self: *Elf) u64 { |
| 3555 | _ = self; |
| 3556 | unreachable; // TODO |
| 3557 | } |
| 3558 | |
| 3559 | pub fn atom(self: *Elf, atom_index: Atom.Index) ?*Atom { |
| 3560 | if (atom_index == 0) return null; |
| 3561 | assert(atom_index < self.atoms.items.len); |
| 3562 | return &self.atoms.items[atom_index]; |
| 3563 | } |
| 3564 | |
| 3565 | pub fn addAtom(self: *Elf) !Atom.Index { |
| 3566 | const index = @as(Atom.Index, @intCast(self.atoms.items.len)); |
| 3567 | const atom_ptr = try self.atoms.addOne(self.base.allocator); |
| 3568 | atom_ptr.* = .{ .atom_index = index }; |
| 3569 | return index; |
| 3570 | } |
| 3571 | |
| 3572 | pub fn file(self: *Elf, index: File.Index) ?File { |
| 3573 | const tag = self.files.items(.tags)[index]; |
| 3574 | return switch (tag) { |
| 3575 | .null => null, |
| 3576 | .linker_defined => .{ .linker_defined = &self.files.items(.data)[index].linker_defined }, |
| 3577 | .zig_module => .{ .zig_module = &self.files.items(.data)[index].zig_module }, |
| 3578 | .object => .{ .object = &self.files.items(.data)[index].object }, |
| 3579 | }; |
| 3580 | } |
| 3581 | |
| 3582 | /// Returns pointer-to-symbol described at sym_index. |
| 3583 | pub fn symbol(self: *Elf, sym_index: Symbol.Index) *Symbol { |
| 3584 | return &self.symbols.items[sym_index]; |
| 3585 | } |
| 3586 | |
| 3587 | pub fn addSymbol(self: *Elf) !Symbol.Index { |
| 3588 | try self.symbols.ensureUnusedCapacity(self.base.allocator, 1); |
| 3589 | const index = blk: { |
| 3590 | if (self.symbols_free_list.popOrNull()) |index| { |
| 3591 | log.debug(" (reusing symbol index {d})", .{index}); |
| 3592 | break :blk index; |
| 3593 | } else { |
| 3594 | log.debug(" (allocating symbol index {d})", .{self.symbols.items.len}); |
| 3595 | const index = @as(Symbol.Index, @intCast(self.symbols.items.len)); |
| 3596 | _ = self.symbols.addOneAssumeCapacity(); |
| 3597 | break :blk index; |
| 3598 | } |
| 3599 | }; |
| 3600 | self.symbols.items[index] = .{ .index = index }; |
| 3601 | return index; |
| 3602 | } |
| 3603 | |
| 3604 | pub fn addSymbolExtra(self: *Elf, extra: Symbol.Extra) !u32 { |
| 3605 | const fields = @typeInfo(Symbol.Extra).Struct.fields; |
| 3606 | try self.symbols_extra.ensureUnusedCapacity(self.base.allocator, fields.len); |
| 3607 | return self.addSymbolExtraAssumeCapacity(extra); |
| 3608 | } |
| 3609 | |
| 3610 | pub fn addSymbolExtraAssumeCapacity(self: *Elf, extra: Symbol.Extra) u32 { |
| 3611 | const index = @as(u32, @intCast(self.symbols_extra.items.len)); |
| 3612 | const fields = @typeInfo(Symbol.Extra).Struct.fields; |
| 3613 | inline for (fields) |field| { |
| 3614 | self.symbols_extra.appendAssumeCapacity(switch (field.type) { |
| 3615 | u32 => @field(extra, field.name), |
| 3616 | else => @compileError("bad field type"), |
| 3617 | }); |
| 3618 | } |
| 3619 | return index; |
| 3620 | } |
| 3621 | |
| 3622 | pub fn symbolExtra(self: *Elf, index: u32) ?Symbol.Extra { |
| 3623 | if (index == 0) return null; |
| 3624 | const fields = @typeInfo(Symbol.Extra).Struct.fields; |
| 3625 | var i: usize = index; |
| 3626 | var result: Symbol.Extra = undefined; |
| 3627 | inline for (fields) |field| { |
| 3628 | @field(result, field.name) = switch (field.type) { |
| 3629 | u32 => self.symbols_extra.items[i], |
| 3630 | else => @compileError("bad field type"), |
| 3631 | }; |
| 3632 | i += 1; |
| 3455 | 3633 | } |
| 3456 | | log.debug("globals:", .{}); |
| 3457 | | for (self.global_symbols.items, 0..) |sym, id| { |
| 3458 | | log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx }); |
| 3634 | return result; |
| 3635 | } |
| 3636 | |
| 3637 | pub fn setSymbolExtra(self: *Elf, index: u32, extra: Symbol.Extra) void { |
| 3638 | assert(index > 0); |
| 3639 | const fields = @typeInfo(Symbol.Extra).Struct.fields; |
| 3640 | inline for (fields, 0..) |field, i| { |
| 3641 | self.symbols_extra.items[index + i] = switch (field.type) { |
| 3642 | u32 => @field(extra, field.name), |
| 3643 | else => @compileError("bad field type"), |
| 3644 | }; |
| 3459 | 3645 | } |
| 3460 | 3646 | } |
| 3461 | 3647 | |
| 3462 | | pub fn getProgramHeader(self: *const Elf, shdr_index: u16) elf.Elf64_Phdr { |
| 3463 | | const index = self.sections.items(.phdr_index)[shdr_index]; |
| 3464 | | return self.program_headers.items[index]; |
| 3648 | const GetOrPutGlobalResult = struct { |
| 3649 | found_existing: bool, |
| 3650 | index: Symbol.Index, |
| 3651 | }; |
| 3652 | |
| 3653 | pub fn getOrPutGlobal(self: *Elf, name_off: u32) !GetOrPutGlobalResult { |
| 3654 | const gpa = self.base.allocator; |
| 3655 | const gop = try self.resolver.getOrPut(gpa, name_off); |
| 3656 | if (!gop.found_existing) { |
| 3657 | const index = try self.addSymbol(); |
| 3658 | const global = self.symbol(index); |
| 3659 | global.name_offset = name_off; |
| 3660 | gop.value_ptr.* = index; |
| 3661 | } |
| 3662 | return .{ |
| 3663 | .found_existing = gop.found_existing, |
| 3664 | .index = gop.value_ptr.*, |
| 3665 | }; |
| 3465 | 3666 | } |
| 3466 | 3667 | |
| 3467 | | pub fn getProgramHeaderPtr(self: *Elf, shdr_index: u16) *elf.Elf64_Phdr { |
| 3468 | | const index = self.sections.items(.phdr_index)[shdr_index]; |
| 3469 | | return &self.program_headers.items[index]; |
| 3668 | pub fn globalByName(self: *Elf, name: []const u8) ?Symbol.Index { |
| 3669 | const name_off = self.strtab.getOffset(name) orelse return null; |
| 3670 | return self.resolver.get(name_off); |
| 3470 | 3671 | } |
| 3471 | 3672 | |
| 3472 | | /// Returns pointer-to-symbol described at sym_index. |
| 3473 | | pub fn getSymbolPtr(self: *Elf, sym_index: u32) *elf.Elf64_Sym { |
| 3474 | | return &self.local_symbols.items[sym_index]; |
| 3673 | pub fn getGlobalSymbol(self: *Elf, name: []const u8, lib_name: ?[]const u8) !u32 { |
| 3674 | _ = lib_name; |
| 3675 | const gpa = self.base.allocator; |
| 3676 | const off = try self.strtab.insert(gpa, name); |
| 3677 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 3678 | const lookup_gop = try zig_module.globals_lookup.getOrPut(gpa, off); |
| 3679 | if (!lookup_gop.found_existing) { |
| 3680 | const esym_index = try zig_module.addGlobalEsym(gpa); |
| 3681 | const esym = zig_module.elfSym(esym_index); |
| 3682 | esym.st_name = off; |
| 3683 | lookup_gop.value_ptr.* = esym_index; |
| 3684 | const gop = try self.getOrPutGlobal(off); |
| 3685 | try zig_module.global_symbols.append(gpa, gop.index); |
| 3686 | } |
| 3687 | return lookup_gop.value_ptr.*; |
| 3475 | 3688 | } |
| 3476 | 3689 | |
| 3477 | | /// Returns symbol at sym_index. |
| 3478 | | pub fn getSymbol(self: *const Elf, sym_index: u32) elf.Elf64_Sym { |
| 3479 | | return self.local_symbols.items[sym_index]; |
| 3690 | const GetOrCreateComdatGroupOwnerResult = struct { |
| 3691 | found_existing: bool, |
| 3692 | index: ComdatGroupOwner.Index, |
| 3693 | }; |
| 3694 | |
| 3695 | pub fn getOrCreateComdatGroupOwner(self: *Elf, off: u32) !GetOrCreateComdatGroupOwnerResult { |
| 3696 | const gpa = self.base.allocator; |
| 3697 | const gop = try self.comdat_groups_table.getOrPut(gpa, off); |
| 3698 | if (!gop.found_existing) { |
| 3699 | const index = @as(ComdatGroupOwner.Index, @intCast(self.comdat_groups_owners.items.len)); |
| 3700 | const owner = try self.comdat_groups_owners.addOne(gpa); |
| 3701 | owner.* = .{}; |
| 3702 | gop.value_ptr.* = index; |
| 3703 | } |
| 3704 | return .{ |
| 3705 | .found_existing = gop.found_existing, |
| 3706 | .index = gop.value_ptr.*, |
| 3707 | }; |
| 3480 | 3708 | } |
| 3481 | 3709 | |
| 3482 | | /// Returns name of the symbol at sym_index. |
| 3483 | | pub fn getSymbolName(self: *const Elf, sym_index: u32) []const u8 { |
| 3484 | | const sym = self.local_symbols.items[sym_index]; |
| 3485 | | return self.strtab.get(sym.st_name).?; |
| 3710 | pub fn addComdatGroup(self: *Elf) !ComdatGroup.Index { |
| 3711 | const index = @as(ComdatGroup.Index, @intCast(self.comdat_groups.items.len)); |
| 3712 | _ = try self.comdat_groups.addOne(self.base.allocator); |
| 3713 | return index; |
| 3486 | 3714 | } |
| 3487 | 3715 | |
| 3488 | | /// Returns name of the global symbol at index. |
| 3489 | | pub fn getGlobalName(self: *const Elf, index: u32) []const u8 { |
| 3490 | | const sym = self.global_symbols.items[index]; |
| 3491 | | return self.strtab.get(sym.st_name).?; |
| 3716 | pub fn comdatGroup(self: *Elf, index: ComdatGroup.Index) *ComdatGroup { |
| 3717 | assert(index < self.comdat_groups.items.len); |
| 3718 | return &self.comdat_groups.items[index]; |
| 3492 | 3719 | } |
| 3493 | 3720 | |
| 3494 | | pub fn getAtom(self: *const Elf, atom_index: Atom.Index) Atom { |
| 3495 | | assert(atom_index < self.atoms.items.len); |
| 3496 | | return self.atoms.items[atom_index]; |
| 3721 | pub fn comdatGroupOwner(self: *Elf, index: ComdatGroupOwner.Index) *ComdatGroupOwner { |
| 3722 | assert(index < self.comdat_groups_owners.items.len); |
| 3723 | return &self.comdat_groups_owners.items[index]; |
| 3497 | 3724 | } |
| 3498 | 3725 | |
| 3499 | | pub fn getAtomPtr(self: *Elf, atom_index: Atom.Index) *Atom { |
| 3500 | | assert(atom_index < self.atoms.items.len); |
| 3501 | | return &self.atoms.items[atom_index]; |
| 3726 | fn reportUndefined(self: *Elf, undefs: anytype) !void { |
| 3727 | const gpa = self.base.allocator; |
| 3728 | const max_notes = 4; |
| 3729 | |
| 3730 | try self.misc_errors.ensureUnusedCapacity(gpa, undefs.count()); |
| 3731 | |
| 3732 | var it = undefs.iterator(); |
| 3733 | while (it.next()) |entry| { |
| 3734 | const undef_index = entry.key_ptr.*; |
| 3735 | const atoms = entry.value_ptr.*.items; |
| 3736 | const nnotes = @min(atoms.len, max_notes); |
| 3737 | |
| 3738 | var notes = try std.ArrayList(link.File.ErrorMsg).initCapacity(gpa, max_notes + 1); |
| 3739 | defer notes.deinit(); |
| 3740 | |
| 3741 | for (atoms[0..nnotes]) |atom_index| { |
| 3742 | const atom_ptr = self.atom(atom_index).?; |
| 3743 | const file_ptr = self.file(atom_ptr.file_index).?; |
| 3744 | const note = try std.fmt.allocPrint(gpa, "referenced by {s}:{s}", .{ |
| 3745 | file_ptr.fmtPath(), |
| 3746 | atom_ptr.name(self), |
| 3747 | }); |
| 3748 | notes.appendAssumeCapacity(.{ .msg = note }); |
| 3749 | } |
| 3750 | |
| 3751 | if (atoms.len > max_notes) { |
| 3752 | const remaining = atoms.len - max_notes; |
| 3753 | const note = try std.fmt.allocPrint(gpa, "referenced {d} more times", .{remaining}); |
| 3754 | notes.appendAssumeCapacity(.{ .msg = note }); |
| 3755 | } |
| 3756 | |
| 3757 | var err_msg = link.File.ErrorMsg{ |
| 3758 | .msg = try std.fmt.allocPrint(gpa, "undefined symbol: {s}", .{self.symbol(undef_index).name(self)}), |
| 3759 | }; |
| 3760 | err_msg.notes = try notes.toOwnedSlice(); |
| 3761 | |
| 3762 | self.misc_errors.appendAssumeCapacity(err_msg); |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | const ParseErrorCtx = struct { |
| 3767 | detected_cpu_arch: std.Target.Cpu.Arch, |
| 3768 | }; |
| 3769 | |
| 3770 | fn handleAndReportParseError( |
| 3771 | self: *Elf, |
| 3772 | path: []const u8, |
| 3773 | err: ParseError, |
| 3774 | ctx: *const ParseErrorCtx, |
| 3775 | ) error{OutOfMemory}!void { |
| 3776 | const cpu_arch = self.base.options.target.cpu.arch; |
| 3777 | switch (err) { |
| 3778 | error.UnknownFileType => try self.reportParseError(path, "unknown file type", .{}), |
| 3779 | error.InvalidCpuArch => try self.reportParseError( |
| 3780 | path, |
| 3781 | "invalid cpu architecture: expected '{s}', but found '{s}'", |
| 3782 | .{ @tagName(cpu_arch), @tagName(ctx.detected_cpu_arch) }, |
| 3783 | ), |
| 3784 | else => |e| try self.reportParseError( |
| 3785 | path, |
| 3786 | "unexpected error: parsing object failed with error {s}", |
| 3787 | .{@errorName(e)}, |
| 3788 | ), |
| 3789 | } |
| 3790 | } |
| 3791 | |
| 3792 | fn reportParseError( |
| 3793 | self: *Elf, |
| 3794 | path: []const u8, |
| 3795 | comptime format: []const u8, |
| 3796 | args: anytype, |
| 3797 | ) error{OutOfMemory}!void { |
| 3798 | const gpa = self.base.allocator; |
| 3799 | try self.misc_errors.ensureUnusedCapacity(gpa, 1); |
| 3800 | var notes = try gpa.alloc(link.File.ErrorMsg, 1); |
| 3801 | errdefer gpa.free(notes); |
| 3802 | notes[0] = .{ .msg = try std.fmt.allocPrint(gpa, "while parsing {s}", .{path}) }; |
| 3803 | self.misc_errors.appendAssumeCapacity(.{ |
| 3804 | .msg = try std.fmt.allocPrint(gpa, format, args), |
| 3805 | .notes = notes, |
| 3806 | }); |
| 3502 | 3807 | } |
| 3503 | 3808 | |
| 3504 | | /// Returns atom if there is an atom referenced by the symbol. |
| 3505 | | /// Returns null on failure. |
| 3506 | | pub fn getAtomIndexForSymbol(self: *Elf, sym_index: u32) ?Atom.Index { |
| 3507 | | return self.atom_by_index_table.get(sym_index); |
| 3809 | fn dumpState(self: *Elf) std.fmt.Formatter(fmtDumpState) { |
| 3810 | return .{ .data = self }; |
| 3508 | 3811 | } |
| 3812 | |
| 3813 | fn fmtDumpState( |
| 3814 | self: *Elf, |
| 3815 | comptime unused_fmt_string: []const u8, |
| 3816 | options: std.fmt.FormatOptions, |
| 3817 | writer: anytype, |
| 3818 | ) !void { |
| 3819 | _ = unused_fmt_string; |
| 3820 | _ = options; |
| 3821 | |
| 3822 | if (self.zig_module_index) |index| { |
| 3823 | const zig_module = self.file(index).?.zig_module; |
| 3824 | try writer.print("zig_module({d}) : {s}\n", .{ index, zig_module.path }); |
| 3825 | try writer.print("{}\n", .{zig_module.fmtSymtab(self)}); |
| 3826 | } |
| 3827 | |
| 3828 | for (self.objects.items) |index| { |
| 3829 | const object = self.file(index).?.object; |
| 3830 | try writer.print("object({d}) : {}", .{ index, object.fmtPath() }); |
| 3831 | if (!object.alive) try writer.writeAll(" : [*]"); |
| 3832 | try writer.writeByte('\n'); |
| 3833 | try writer.print("{}{}{}{}{}\n", .{ |
| 3834 | object.fmtAtoms(self), |
| 3835 | object.fmtCies(self), |
| 3836 | object.fmtFdes(self), |
| 3837 | object.fmtSymtab(self), |
| 3838 | object.fmtComdatGroups(self), |
| 3839 | }); |
| 3840 | } |
| 3841 | |
| 3842 | if (self.linker_defined_index) |index| { |
| 3843 | const linker_defined = self.file(index).?.linker_defined; |
| 3844 | try writer.print("linker_defined({d}) : (linker defined)\n", .{index}); |
| 3845 | try writer.print("{}\n", .{linker_defined.fmtSymtab(self)}); |
| 3846 | } |
| 3847 | try writer.print("{}\n", .{self.got.fmt(self)}); |
| 3848 | } |
| 3849 | |
| 3850 | /// Binary search |
| 3851 | pub fn bsearch(comptime T: type, haystack: []align(1) const T, predicate: anytype) usize { |
| 3852 | if (!@hasDecl(@TypeOf(predicate), "predicate")) |
| 3853 | @compileError("Predicate is required to define fn predicate(@This(), T) bool"); |
| 3854 | |
| 3855 | var min: usize = 0; |
| 3856 | var max: usize = haystack.len; |
| 3857 | while (min < max) { |
| 3858 | const index = (min + max) / 2; |
| 3859 | const curr = haystack[index]; |
| 3860 | if (predicate.predicate(curr)) { |
| 3861 | min = index + 1; |
| 3862 | } else { |
| 3863 | max = index; |
| 3864 | } |
| 3865 | } |
| 3866 | return min; |
| 3867 | } |
| 3868 | |
| 3869 | /// Linear search |
| 3870 | pub fn lsearch(comptime T: type, haystack: []align(1) const T, predicate: anytype) usize { |
| 3871 | if (!@hasDecl(@TypeOf(predicate), "predicate")) |
| 3872 | @compileError("Predicate is required to define fn predicate(@This(), T) bool"); |
| 3873 | |
| 3874 | var i: usize = 0; |
| 3875 | while (i < haystack.len) : (i += 1) { |
| 3876 | if (predicate.predicate(haystack[i])) break; |
| 3877 | } |
| 3878 | return i; |
| 3879 | } |
| 3880 | |
| 3881 | const default_entry_addr = 0x8000000; |
| 3882 | |
| 3883 | pub const base_tag: link.File.Tag = .elf; |
| 3884 | |
| 3885 | const LastAtomAndFreeList = struct { |
| 3886 | /// Index of the last allocated atom in this section. |
| 3887 | last_atom_index: Atom.Index = 0, |
| 3888 | |
| 3889 | /// A list of atoms that have surplus capacity. This list can have false |
| 3890 | /// positives, as functions grow and shrink over time, only sometimes being added |
| 3891 | /// or removed from the freelist. |
| 3892 | /// |
| 3893 | /// An atom has surplus capacity when its overcapacity value is greater than |
| 3894 | /// padToIdeal(minimum_atom_size). That is, when it has so |
| 3895 | /// much extra capacity, that we could fit a small new symbol in it, itself with |
| 3896 | /// ideal_capacity or more. |
| 3897 | /// |
| 3898 | /// Ideal capacity is defined by size + (size / ideal_factor) |
| 3899 | /// |
| 3900 | /// Overcapacity is measured by actual_capacity - ideal_capacity. Note that |
| 3901 | /// overcapacity can be negative. A simple way to have negative overcapacity is to |
| 3902 | /// allocate a fresh text block, which will have ideal capacity, and then grow it |
| 3903 | /// by 1 byte. It will then have -1 overcapacity. |
| 3904 | free_list: std.ArrayListUnmanaged(Atom.Index) = .{}, |
| 3905 | }; |
| 3906 | |
| 3907 | const LazySymbolMetadata = struct { |
| 3908 | const State = enum { unused, pending_flush, flushed }; |
| 3909 | text_symbol_index: Symbol.Index = undefined, |
| 3910 | rodata_symbol_index: Symbol.Index = undefined, |
| 3911 | text_state: State = .unused, |
| 3912 | rodata_state: State = .unused, |
| 3913 | }; |
| 3914 | |
| 3915 | const DeclMetadata = struct { |
| 3916 | symbol_index: Symbol.Index, |
| 3917 | /// A list of all exports aliases of this Decl. |
| 3918 | exports: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 3919 | |
| 3920 | fn @"export"(m: DeclMetadata, elf_file: *Elf, name: []const u8) ?*u32 { |
| 3921 | const zig_module = elf_file.file(elf_file.zig_module_index.?).?.zig_module; |
| 3922 | for (m.exports.items) |*exp| { |
| 3923 | const exp_name = elf_file.strtab.getAssumeExists(zig_module.elfSym(exp.*).st_name); |
| 3924 | if (mem.eql(u8, name, exp_name)) return exp; |
| 3925 | } |
| 3926 | return null; |
| 3927 | } |
| 3928 | }; |
| 3929 | |
| 3930 | const ComdatGroupOwner = struct { |
| 3931 | file: File.Index = 0, |
| 3932 | const Index = u32; |
| 3933 | }; |
| 3934 | |
| 3935 | pub const ComdatGroup = struct { |
| 3936 | owner: ComdatGroupOwner.Index, |
| 3937 | shndx: u16, |
| 3938 | pub const Index = u32; |
| 3939 | }; |
| 3940 | |
| 3941 | pub const SymtabSize = struct { |
| 3942 | nlocals: u32 = 0, |
| 3943 | nglobals: u32 = 0, |
| 3944 | }; |
| 3945 | |
| 3946 | pub const null_sym = elf.Elf64_Sym{ |
| 3947 | .st_name = 0, |
| 3948 | .st_info = 0, |
| 3949 | .st_other = 0, |
| 3950 | .st_shndx = 0, |
| 3951 | .st_value = 0, |
| 3952 | .st_size = 0, |
| 3953 | }; |
| 3954 | |
| 3955 | const std = @import("std"); |
| 3956 | const build_options = @import("build_options"); |
| 3957 | const builtin = @import("builtin"); |
| 3958 | const assert = std.debug.assert; |
| 3959 | const elf = std.elf; |
| 3960 | const fs = std.fs; |
| 3961 | const log = std.log.scoped(.link); |
| 3962 | const state_log = std.log.scoped(.link_state); |
| 3963 | const math = std.math; |
| 3964 | const mem = std.mem; |
| 3965 | |
| 3966 | const codegen = @import("../codegen.zig"); |
| 3967 | const glibc = @import("../glibc.zig"); |
| 3968 | const link = @import("../link.zig"); |
| 3969 | const lldMain = @import("../main.zig").lldMain; |
| 3970 | const musl = @import("../musl.zig"); |
| 3971 | const target_util = @import("../target.zig"); |
| 3972 | const trace = @import("../tracy.zig").trace; |
| 3973 | const synthetic_sections = @import("Elf/synthetic_sections.zig"); |
| 3974 | |
| 3975 | const Air = @import("../Air.zig"); |
| 3976 | const Allocator = std.mem.Allocator; |
| 3977 | pub const Atom = @import("Elf/Atom.zig"); |
| 3978 | const Cache = std.Build.Cache; |
| 3979 | const Compilation = @import("../Compilation.zig"); |
| 3980 | const Dwarf = @import("Dwarf.zig"); |
| 3981 | const Elf = @This(); |
| 3982 | const File = @import("Elf/file.zig").File; |
| 3983 | const GotSection = synthetic_sections.GotSection; |
| 3984 | const LinkerDefined = @import("Elf/LinkerDefined.zig"); |
| 3985 | const Liveness = @import("../Liveness.zig"); |
| 3986 | const LlvmObject = @import("../codegen/llvm.zig").Object; |
| 3987 | const Module = @import("../Module.zig"); |
| 3988 | const Object = @import("Elf/Object.zig"); |
| 3989 | const InternPool = @import("../InternPool.zig"); |
| 3990 | const Package = @import("../Package.zig"); |
| 3991 | const Symbol = @import("Elf/Symbol.zig"); |
| 3992 | const StringTable = @import("strtab.zig").StringTable; |
| 3993 | const TableSection = @import("table_section.zig").TableSection; |
| 3994 | const Type = @import("../type.zig").Type; |
| 3995 | const TypedValue = @import("../TypedValue.zig"); |
| 3996 | const Value = @import("../value.zig").Value; |
| 3997 | const ZigModule = @import("Elf/ZigModule.zig"); |