| ... | ... | @@ -1,43 +1,89 @@ |
| 1 | 1 | const Elf = @This(); |
| 2 | 2 | |
| 3 | 3 | const std = @import("std"); |
| 4 | const build_options = @import("build_options"); |
| 4 | 5 | const builtin = @import("builtin"); |
| 5 | | const math = std.math; |
| 6 | | const mem = std.mem; |
| 7 | 6 | const assert = std.debug.assert; |
| 8 | | const Allocator = std.mem.Allocator; |
| 9 | | const fs = std.fs; |
| 10 | 7 | const elf = std.elf; |
| 8 | const fs = std.fs; |
| 11 | 9 | const log = std.log.scoped(.link); |
| 10 | const math = std.math; |
| 11 | const mem = std.mem; |
| 12 | 12 | |
| 13 | | const Atom = @import("Elf/Atom.zig"); |
| 14 | | const Module = @import("../Module.zig"); |
| 15 | | const Compilation = @import("../Compilation.zig"); |
| 16 | | const Dwarf = @import("Dwarf.zig"); |
| 17 | 13 | const codegen = @import("../codegen.zig"); |
| 18 | | const lldMain = @import("../main.zig").lldMain; |
| 19 | | const trace = @import("../tracy.zig").trace; |
| 20 | | const Package = @import("../Package.zig"); |
| 21 | | const Value = @import("../value.zig").Value; |
| 22 | | const Type = @import("../type.zig").Type; |
| 23 | | const TypedValue = @import("../TypedValue.zig"); |
| 24 | | const link = @import("../link.zig"); |
| 25 | | const File = link.File; |
| 26 | | const build_options = @import("build_options"); |
| 27 | | const target_util = @import("../target.zig"); |
| 28 | 14 | const glibc = @import("../glibc.zig"); |
| 15 | const link = @import("../link.zig"); |
| 16 | const lldMain = @import("../main.zig").lldMain; |
| 29 | 17 | const musl = @import("../musl.zig"); |
| 30 | | const Cache = @import("../Cache.zig"); |
| 18 | const target_util = @import("../target.zig"); |
| 19 | const trace = @import("../tracy.zig").trace; |
| 20 | |
| 31 | 21 | const Air = @import("../Air.zig"); |
| 22 | const Allocator = std.mem.Allocator; |
| 23 | pub const Atom = @import("Elf/Atom.zig"); |
| 24 | const Cache = @import("../Cache.zig"); |
| 25 | const Compilation = @import("../Compilation.zig"); |
| 26 | const Dwarf = @import("Dwarf.zig"); |
| 27 | const File = link.File; |
| 32 | 28 | const Liveness = @import("../Liveness.zig"); |
| 33 | 29 | const LlvmObject = @import("../codegen/llvm.zig").Object; |
| 34 | | |
| 35 | | pub const TextBlock = Atom; |
| 30 | const Module = @import("../Module.zig"); |
| 31 | const Package = @import("../Package.zig"); |
| 32 | const StringTable = @import("strtab.zig").StringTable; |
| 33 | const Type = @import("../type.zig").Type; |
| 34 | const TypedValue = @import("../TypedValue.zig"); |
| 35 | const Value = @import("../value.zig").Value; |
| 36 | 36 | |
| 37 | 37 | const default_entry_addr = 0x8000000; |
| 38 | 38 | |
| 39 | 39 | pub const base_tag: File.Tag = .elf; |
| 40 | 40 | |
| 41 | const Section = struct { |
| 42 | shdr: elf.Elf64_Shdr, |
| 43 | phdr_index: u16, |
| 44 | |
| 45 | /// Index of the last allocated atom in this section. |
| 46 | last_atom_index: ?Atom.Index = null, |
| 47 | |
| 48 | /// A list of atoms that have surplus capacity. This list can have false |
| 49 | /// positives, as functions grow and shrink over time, only sometimes being added |
| 50 | /// or removed from the freelist. |
| 51 | /// |
| 52 | /// An atom has surplus capacity when its overcapacity value is greater than |
| 53 | /// padToIdeal(minimum_atom_size). That is, when it has so |
| 54 | /// much extra capacity, that we could fit a small new symbol in it, itself with |
| 55 | /// ideal_capacity or more. |
| 56 | /// |
| 57 | /// Ideal capacity is defined by size + (size / ideal_factor) |
| 58 | /// |
| 59 | /// Overcapacity is measured by actual_capacity - ideal_capacity. Note that |
| 60 | /// overcapacity can be negative. A simple way to have negative overcapacity is to |
| 61 | /// allocate a fresh text block, which will have ideal capacity, and then grow it |
| 62 | /// by 1 byte. It will then have -1 overcapacity. |
| 63 | free_list: std.ArrayListUnmanaged(Atom.Index) = .{}, |
| 64 | }; |
| 65 | |
| 66 | const DeclMetadata = struct { |
| 67 | atom: Atom.Index, |
| 68 | shdr: u16, |
| 69 | /// A list of all exports aliases of this Decl. |
| 70 | exports: std.ArrayListUnmanaged(u32) = .{}, |
| 71 | |
| 72 | fn getExport(m: DeclMetadata, elf_file: *const Elf, name: []const u8) ?u32 { |
| 73 | for (m.exports.items) |exp| { |
| 74 | if (mem.eql(u8, name, elf_file.getSymbolName(exp))) return exp; |
| 75 | } |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | fn getExportPtr(m: *DeclMetadata, elf_file: *Elf, name: []const u8) ?*u32 { |
| 80 | for (m.exports.items) |*exp| { |
| 81 | if (mem.eql(u8, name, elf_file.getSymbolName(exp.*))) return exp; |
| 82 | } |
| 83 | return null; |
| 84 | } |
| 85 | }; |
| 86 | |
| 41 | 87 | base: File, |
| 42 | 88 | dwarf: ?Dwarf = null, |
| 43 | 89 | |
| ... | ... | @@ -48,12 +94,12 @@ llvm_object: ?*LlvmObject = null, |
| 48 | 94 | |
| 49 | 95 | /// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write. |
| 50 | 96 | /// Same order as in the file. |
| 51 | | sections: std.ArrayListUnmanaged(elf.Elf64_Shdr) = std.ArrayListUnmanaged(elf.Elf64_Shdr){}, |
| 97 | sections: std.MultiArrayList(Section) = .{}, |
| 52 | 98 | shdr_table_offset: ?u64 = null, |
| 53 | 99 | |
| 54 | 100 | /// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write. |
| 55 | 101 | /// Same order as in the file. |
| 56 | | program_headers: std.ArrayListUnmanaged(elf.Elf64_Phdr) = std.ArrayListUnmanaged(elf.Elf64_Phdr){}, |
| 102 | program_headers: std.ArrayListUnmanaged(elf.Elf64_Phdr) = .{}, |
| 57 | 103 | phdr_table_offset: ?u64 = null, |
| 58 | 104 | /// The index into the program headers of a PT_LOAD program header with Read and Execute flags |
| 59 | 105 | phdr_load_re_index: ?u16 = null, |
| ... | ... | @@ -65,12 +111,10 @@ phdr_load_ro_index: ?u16 = null, |
| 65 | 111 | /// The index into the program headers of a PT_LOAD program header with Write flag |
| 66 | 112 | phdr_load_rw_index: ?u16 = null, |
| 67 | 113 | |
| 68 | | phdr_shdr_table: std.AutoHashMapUnmanaged(u16, u16) = .{}, |
| 69 | | |
| 70 | 114 | entry_addr: ?u64 = null, |
| 71 | 115 | page_size: u32, |
| 72 | 116 | |
| 73 | | shstrtab: std.ArrayListUnmanaged(u8) = std.ArrayListUnmanaged(u8){}, |
| 117 | shstrtab: StringTable(.strtab) = .{}, |
| 74 | 118 | shstrtab_index: ?u16 = null, |
| 75 | 119 | |
| 76 | 120 | symtab_section_index: ?u16 = null, |
| ... | ... | @@ -113,39 +157,14 @@ debug_line_header_dirty: bool = false, |
| 113 | 157 | |
| 114 | 158 | error_flags: File.ErrorFlags = File.ErrorFlags{}, |
| 115 | 159 | |
| 116 | | /// Pointer to the last allocated atom |
| 117 | | atoms: std.AutoHashMapUnmanaged(u16, *TextBlock) = .{}, |
| 118 | | |
| 119 | | /// A list of text blocks that have surplus capacity. This list can have false |
| 120 | | /// positives, as functions grow and shrink over time, only sometimes being added |
| 121 | | /// or removed from the freelist. |
| 122 | | /// |
| 123 | | /// A text block has surplus capacity when its overcapacity value is greater than |
| 124 | | /// padToIdeal(minimum_text_block_size). That is, when it has so |
| 125 | | /// much extra capacity, that we could fit a small new symbol in it, itself with |
| 126 | | /// ideal_capacity or more. |
| 127 | | /// |
| 128 | | /// Ideal capacity is defined by size + (size / ideal_factor) |
| 129 | | /// |
| 130 | | /// Overcapacity is measured by actual_capacity - ideal_capacity. Note that |
| 131 | | /// overcapacity can be negative. A simple way to have negative overcapacity is to |
| 132 | | /// allocate a fresh text block, which will have ideal capacity, and then grow it |
| 133 | | /// by 1 byte. It will then have -1 overcapacity. |
| 134 | | atom_free_lists: std.AutoHashMapUnmanaged(u16, std.ArrayListUnmanaged(*TextBlock)) = .{}, |
| 135 | | |
| 136 | | /// Table of Decls that are currently alive. |
| 137 | | /// We store them here so that we can properly dispose of any allocated |
| 138 | | /// memory within the atom in the incremental linker. |
| 139 | | /// TODO consolidate this. |
| 140 | | decls: std.AutoHashMapUnmanaged(Module.Decl.Index, ?u16) = .{}, |
| 160 | /// Table of tracked Decls. |
| 161 | decls: std.AutoHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{}, |
| 141 | 162 | |
| 142 | 163 | /// List of atoms that are owned directly by the linker. |
| 143 | | /// Currently these are only atoms that are the result of linking |
| 144 | | /// object files. Atoms which take part in incremental linking are |
| 145 | | /// at present owned by Module.Decl. |
| 146 | | /// TODO consolidate this. |
| 147 | | managed_atoms: std.ArrayListUnmanaged(*TextBlock) = .{}, |
| 148 | | atom_by_index_table: std.AutoHashMapUnmanaged(u32, *TextBlock) = .{}, |
| 164 | atoms: std.ArrayListUnmanaged(Atom) = .{}, |
| 165 | |
| 166 | /// Table of atoms indexed by the symbol index. |
| 167 | atom_by_index_table: std.AutoHashMapUnmanaged(u32, Atom.Index) = .{}, |
| 149 | 168 | |
| 150 | 169 | /// Table of unnamed constants associated with a parent `Decl`. |
| 151 | 170 | /// We store them here so that we can free the constants whenever the `Decl` |
| ... | ... | @@ -173,15 +192,8 @@ unnamed_const_atoms: UnnamedConstTable = .{}, |
| 173 | 192 | /// this will be a table indexed by index into the list of Atoms. |
| 174 | 193 | relocs: RelocTable = .{}, |
| 175 | 194 | |
| 176 | | const Reloc = struct { |
| 177 | | target: u32, |
| 178 | | offset: u64, |
| 179 | | addend: u32, |
| 180 | | prev_vaddr: u64, |
| 181 | | }; |
| 182 | | |
| 183 | | const RelocTable = std.AutoHashMapUnmanaged(*TextBlock, std.ArrayListUnmanaged(Reloc)); |
| 184 | | const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(*TextBlock)); |
| 195 | const RelocTable = std.AutoHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Atom.Reloc)); |
| 196 | const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Atom.Index)); |
| 185 | 197 | |
| 186 | 198 | /// When allocating, the ideal_capacity is calculated by |
| 187 | 199 | /// actual_capacity + (actual_capacity / ideal_factor) |
| ... | ... | @@ -190,15 +202,11 @@ const ideal_factor = 3; |
| 190 | 202 | /// In order for a slice of bytes to be considered eligible to keep metadata pointing at |
| 191 | 203 | /// it as a possible place to put new symbols, it must have enough room for this many bytes |
| 192 | 204 | /// (plus extra for reserved capacity). |
| 193 | | const minimum_text_block_size = 64; |
| 194 | | pub const min_text_capacity = padToIdeal(minimum_text_block_size); |
| 205 | const minimum_atom_size = 64; |
| 206 | pub const min_text_capacity = padToIdeal(minimum_atom_size); |
| 195 | 207 | |
| 196 | 208 | pub const PtrWidth = enum { p32, p64 }; |
| 197 | 209 | |
| 198 | | pub const Export = struct { |
| 199 | | sym_index: ?u32 = null, |
| 200 | | }; |
| 201 | | |
| 202 | 210 | pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Options) !*Elf { |
| 203 | 211 | assert(options.target.ofmt == .elf); |
| 204 | 212 | |
| ... | ... | @@ -230,16 +238,19 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 230 | 238 | |
| 231 | 239 | // There must always be a null section in index 0 |
| 232 | 240 | try self.sections.append(allocator, .{ |
| 233 | | .sh_name = 0, |
| 234 | | .sh_type = elf.SHT_NULL, |
| 235 | | .sh_flags = 0, |
| 236 | | .sh_addr = 0, |
| 237 | | .sh_offset = 0, |
| 238 | | .sh_size = 0, |
| 239 | | .sh_link = 0, |
| 240 | | .sh_info = 0, |
| 241 | | .sh_addralign = 0, |
| 242 | | .sh_entsize = 0, |
| 241 | .shdr = .{ |
| 242 | .sh_name = 0, |
| 243 | .sh_type = elf.SHT_NULL, |
| 244 | .sh_flags = 0, |
| 245 | .sh_addr = 0, |
| 246 | .sh_offset = 0, |
| 247 | .sh_size = 0, |
| 248 | .sh_link = 0, |
| 249 | .sh_info = 0, |
| 250 | .sh_addralign = 0, |
| 251 | .sh_entsize = 0, |
| 252 | }, |
| 253 | .phdr_index = undefined, |
| 243 | 254 | }); |
| 244 | 255 | |
| 245 | 256 | try self.populateMissingMetadata(); |
| ... | ... | @@ -286,75 +297,67 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Elf { |
| 286 | 297 | } |
| 287 | 298 | |
| 288 | 299 | pub fn deinit(self: *Elf) void { |
| 300 | const gpa = self.base.allocator; |
| 301 | |
| 289 | 302 | if (build_options.have_llvm) { |
| 290 | | if (self.llvm_object) |llvm_object| llvm_object.destroy(self.base.allocator); |
| 291 | | } |
| 292 | | |
| 293 | | self.sections.deinit(self.base.allocator); |
| 294 | | self.program_headers.deinit(self.base.allocator); |
| 295 | | self.shstrtab.deinit(self.base.allocator); |
| 296 | | self.local_symbols.deinit(self.base.allocator); |
| 297 | | self.global_symbols.deinit(self.base.allocator); |
| 298 | | self.global_symbol_free_list.deinit(self.base.allocator); |
| 299 | | self.local_symbol_free_list.deinit(self.base.allocator); |
| 300 | | self.offset_table_free_list.deinit(self.base.allocator); |
| 301 | | self.offset_table.deinit(self.base.allocator); |
| 302 | | self.phdr_shdr_table.deinit(self.base.allocator); |
| 303 | | self.decls.deinit(self.base.allocator); |
| 304 | | |
| 305 | | self.atoms.deinit(self.base.allocator); |
| 303 | if (self.llvm_object) |llvm_object| llvm_object.destroy(gpa); |
| 304 | } |
| 305 | |
| 306 | for (self.sections.items(.free_list)) |*free_list| { |
| 307 | free_list.deinit(gpa); |
| 308 | } |
| 309 | self.sections.deinit(gpa); |
| 310 | |
| 311 | self.program_headers.deinit(gpa); |
| 312 | self.shstrtab.deinit(gpa); |
| 313 | self.local_symbols.deinit(gpa); |
| 314 | self.global_symbols.deinit(gpa); |
| 315 | self.global_symbol_free_list.deinit(gpa); |
| 316 | self.local_symbol_free_list.deinit(gpa); |
| 317 | self.offset_table_free_list.deinit(gpa); |
| 318 | self.offset_table.deinit(gpa); |
| 319 | |
| 306 | 320 | { |
| 307 | | var it = self.atom_free_lists.valueIterator(); |
| 308 | | while (it.next()) |free_list| { |
| 309 | | free_list.deinit(self.base.allocator); |
| 321 | var it = self.decls.iterator(); |
| 322 | while (it.next()) |entry| { |
| 323 | entry.value_ptr.exports.deinit(gpa); |
| 310 | 324 | } |
| 311 | | self.atom_free_lists.deinit(self.base.allocator); |
| 325 | self.decls.deinit(gpa); |
| 312 | 326 | } |
| 313 | 327 | |
| 314 | | for (self.managed_atoms.items) |atom| { |
| 315 | | self.base.allocator.destroy(atom); |
| 316 | | } |
| 317 | | self.managed_atoms.deinit(self.base.allocator); |
| 328 | self.atoms.deinit(gpa); |
| 329 | self.atom_by_index_table.deinit(gpa); |
| 318 | 330 | |
| 319 | 331 | { |
| 320 | 332 | var it = self.unnamed_const_atoms.valueIterator(); |
| 321 | 333 | while (it.next()) |atoms| { |
| 322 | | atoms.deinit(self.base.allocator); |
| 334 | atoms.deinit(gpa); |
| 323 | 335 | } |
| 324 | | self.unnamed_const_atoms.deinit(self.base.allocator); |
| 336 | self.unnamed_const_atoms.deinit(gpa); |
| 325 | 337 | } |
| 326 | 338 | |
| 327 | 339 | { |
| 328 | 340 | var it = self.relocs.valueIterator(); |
| 329 | 341 | while (it.next()) |relocs| { |
| 330 | | relocs.deinit(self.base.allocator); |
| 342 | relocs.deinit(gpa); |
| 331 | 343 | } |
| 332 | | self.relocs.deinit(self.base.allocator); |
| 344 | self.relocs.deinit(gpa); |
| 333 | 345 | } |
| 334 | 346 | |
| 335 | | self.atom_by_index_table.deinit(self.base.allocator); |
| 336 | | |
| 337 | | if (self.dwarf) |*dw| { |
| 338 | | dw.deinit(); |
| 339 | | } |
| 347 | // if (self.dwarf) |*dw| { |
| 348 | // dw.deinit(); |
| 349 | // } |
| 340 | 350 | } |
| 341 | 351 | |
| 342 | 352 | pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: File.RelocInfo) !u64 { |
| 343 | | const mod = self.base.options.module.?; |
| 344 | | const decl = mod.declPtr(decl_index); |
| 345 | | |
| 346 | 353 | assert(self.llvm_object == null); |
| 347 | 354 | |
| 348 | | try decl.link.elf.ensureInitialized(self); |
| 349 | | const target = decl.link.elf.getSymbolIndex().?; |
| 350 | | |
| 351 | | const vaddr = self.local_symbols.items[target].st_value; |
| 352 | | const atom = self.atom_by_index_table.get(reloc_info.parent_atom_index).?; |
| 353 | | const gop = try self.relocs.getOrPut(self.base.allocator, atom); |
| 354 | | if (!gop.found_existing) { |
| 355 | | gop.value_ptr.* = .{}; |
| 356 | | } |
| 357 | | try gop.value_ptr.append(self.base.allocator, .{ |
| 355 | const this_atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 356 | const this_atom = self.getAtom(this_atom_index); |
| 357 | const target = this_atom.getSymbolIndex().?; |
| 358 | const vaddr = this_atom.getSymbol(self).st_value; |
| 359 | const atom_index = self.getAtomIndexForSymbol(reloc_info.parent_atom_index).?; |
| 360 | try Atom.addRelocation(self, atom_index, .{ |
| 358 | 361 | .target = target, |
| 359 | 362 | .offset = reloc_info.offset, |
| 360 | 363 | .addend = reloc_info.addend, |
| ... | ... | @@ -375,7 +378,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 { |
| 375 | 378 | |
| 376 | 379 | if (self.shdr_table_offset) |off| { |
| 377 | 380 | const shdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Shdr) else @sizeOf(elf.Elf64_Shdr); |
| 378 | | const tight_size = self.sections.items.len * shdr_size; |
| 381 | const tight_size = self.sections.slice().len * shdr_size; |
| 379 | 382 | const increased_size = padToIdeal(tight_size); |
| 380 | 383 | const test_end = off + increased_size; |
| 381 | 384 | if (end > off and start < test_end) { |
| ... | ... | @@ -385,7 +388,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 { |
| 385 | 388 | |
| 386 | 389 | if (self.phdr_table_offset) |off| { |
| 387 | 390 | const phdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Phdr) else @sizeOf(elf.Elf64_Phdr); |
| 388 | | const tight_size = self.sections.items.len * phdr_size; |
| 391 | const tight_size = self.sections.slice().len * phdr_size; |
| 389 | 392 | const increased_size = padToIdeal(tight_size); |
| 390 | 393 | const test_end = off + increased_size; |
| 391 | 394 | if (end > off and start < test_end) { |
| ... | ... | @@ -393,7 +396,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 { |
| 393 | 396 | } |
| 394 | 397 | } |
| 395 | 398 | |
| 396 | | for (self.sections.items) |section| { |
| 399 | for (self.sections.items(.shdr)) |section| { |
| 397 | 400 | const increased_size = padToIdeal(section.sh_size); |
| 398 | 401 | const test_end = section.sh_offset + increased_size; |
| 399 | 402 | if (end > section.sh_offset and start < test_end) { |
| ... | ... | @@ -420,7 +423,7 @@ pub fn allocatedSize(self: *Elf, start: u64) u64 { |
| 420 | 423 | if (self.phdr_table_offset) |off| { |
| 421 | 424 | if (off > start and off < min_pos) min_pos = off; |
| 422 | 425 | } |
| 423 | | for (self.sections.items) |section| { |
| 426 | for (self.sections.items(.shdr)) |section| { |
| 424 | 427 | if (section.sh_offset <= start) continue; |
| 425 | 428 | if (section.sh_offset < min_pos) min_pos = section.sh_offset; |
| 426 | 429 | } |
| ... | ... | @@ -439,31 +442,10 @@ pub fn findFreeSpace(self: *Elf, object_size: u64, min_alignment: u32) u64 { |
| 439 | 442 | return start; |
| 440 | 443 | } |
| 441 | 444 | |
| 442 | | /// TODO Improve this to use a table. |
| 443 | | fn makeString(self: *Elf, bytes: []const u8) !u32 { |
| 444 | | try self.shstrtab.ensureUnusedCapacity(self.base.allocator, bytes.len + 1); |
| 445 | | const result = self.shstrtab.items.len; |
| 446 | | self.shstrtab.appendSliceAssumeCapacity(bytes); |
| 447 | | self.shstrtab.appendAssumeCapacity(0); |
| 448 | | return @intCast(u32, result); |
| 449 | | } |
| 450 | | |
| 451 | | pub fn getString(self: Elf, str_off: u32) []const u8 { |
| 452 | | assert(str_off < self.shstrtab.items.len); |
| 453 | | return mem.sliceTo(@ptrCast([*:0]const u8, self.shstrtab.items.ptr + str_off), 0); |
| 454 | | } |
| 455 | | |
| 456 | | fn updateString(self: *Elf, old_str_off: u32, new_name: []const u8) !u32 { |
| 457 | | const existing_name = self.getString(old_str_off); |
| 458 | | if (mem.eql(u8, existing_name, new_name)) { |
| 459 | | return old_str_off; |
| 460 | | } |
| 461 | | return self.makeString(new_name); |
| 462 | | } |
| 463 | | |
| 464 | 445 | pub fn populateMissingMetadata(self: *Elf) !void { |
| 465 | 446 | assert(self.llvm_object == null); |
| 466 | 447 | |
| 448 | const gpa = self.base.allocator; |
| 467 | 449 | const small_ptr = switch (self.ptr_width) { |
| 468 | 450 | .p32 => true, |
| 469 | 451 | .p64 => false, |
| ... | ... | @@ -477,7 +459,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 477 | 459 | const off = self.findFreeSpace(file_size, p_align); |
| 478 | 460 | log.debug("found PT_LOAD RE free space 0x{x} to 0x{x}", .{ off, off + file_size }); |
| 479 | 461 | const entry_addr: u64 = self.entry_addr orelse if (self.base.options.target.cpu.arch == .spu_2) @as(u64, 0) else default_entry_addr; |
| 480 | | try self.program_headers.append(self.base.allocator, .{ |
| 462 | try self.program_headers.append(gpa, .{ |
| 481 | 463 | .p_type = elf.PT_LOAD, |
| 482 | 464 | .p_offset = off, |
| 483 | 465 | .p_filesz = file_size, |
| ... | ... | @@ -487,7 +469,6 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 487 | 469 | .p_align = p_align, |
| 488 | 470 | .p_flags = elf.PF_X | elf.PF_R, |
| 489 | 471 | }); |
| 490 | | try self.atom_free_lists.putNoClobber(self.base.allocator, self.phdr_load_re_index.?, .{}); |
| 491 | 472 | self.entry_addr = null; |
| 492 | 473 | self.phdr_table_dirty = true; |
| 493 | 474 | } |
| ... | ... | @@ -504,7 +485,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 504 | 485 | // we'll need to re-use that function anyway, in case the GOT grows and overlaps something |
| 505 | 486 | // else in virtual memory. |
| 506 | 487 | const got_addr: u32 = if (self.base.options.target.cpu.arch.ptrBitWidth() >= 32) 0x4000000 else 0x8000; |
| 507 | | try self.program_headers.append(self.base.allocator, .{ |
| 488 | try self.program_headers.append(gpa, .{ |
| 508 | 489 | .p_type = elf.PT_LOAD, |
| 509 | 490 | .p_offset = off, |
| 510 | 491 | .p_filesz = file_size, |
| ... | ... | @@ -527,7 +508,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 527 | 508 | log.debug("found PT_LOAD RO free space 0x{x} to 0x{x}", .{ off, off + file_size }); |
| 528 | 509 | // TODO Same as for GOT |
| 529 | 510 | const rodata_addr: u32 = if (self.base.options.target.cpu.arch.ptrBitWidth() >= 32) 0xc000000 else 0xa000; |
| 530 | | try self.program_headers.append(self.base.allocator, .{ |
| 511 | try self.program_headers.append(gpa, .{ |
| 531 | 512 | .p_type = elf.PT_LOAD, |
| 532 | 513 | .p_offset = off, |
| 533 | 514 | .p_filesz = file_size, |
| ... | ... | @@ -537,7 +518,6 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 537 | 518 | .p_align = p_align, |
| 538 | 519 | .p_flags = elf.PF_R, |
| 539 | 520 | }); |
| 540 | | try self.atom_free_lists.putNoClobber(self.base.allocator, self.phdr_load_ro_index.?, .{}); |
| 541 | 521 | self.phdr_table_dirty = true; |
| 542 | 522 | } |
| 543 | 523 | |
| ... | ... | @@ -551,7 +531,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 551 | 531 | log.debug("found PT_LOAD RW free space 0x{x} to 0x{x}", .{ off, off + file_size }); |
| 552 | 532 | // TODO Same as for GOT |
| 553 | 533 | const rwdata_addr: u32 = if (self.base.options.target.cpu.arch.ptrBitWidth() >= 32) 0x10000000 else 0xc000; |
| 554 | | try self.program_headers.append(self.base.allocator, .{ |
| 534 | try self.program_headers.append(gpa, .{ |
| 555 | 535 | .p_type = elf.PT_LOAD, |
| 556 | 536 | .p_offset = off, |
| 557 | 537 | .p_filesz = file_size, |
| ... | ... | @@ -561,278 +541,290 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 561 | 541 | .p_align = p_align, |
| 562 | 542 | .p_flags = elf.PF_R | elf.PF_W, |
| 563 | 543 | }); |
| 564 | | try self.atom_free_lists.putNoClobber(self.base.allocator, self.phdr_load_rw_index.?, .{}); |
| 565 | 544 | self.phdr_table_dirty = true; |
| 566 | 545 | } |
| 567 | 546 | |
| 568 | 547 | if (self.shstrtab_index == null) { |
| 569 | | self.shstrtab_index = @intCast(u16, self.sections.items.len); |
| 570 | | assert(self.shstrtab.items.len == 0); |
| 571 | | try self.shstrtab.append(self.base.allocator, 0); // need a 0 at position 0 |
| 572 | | const off = self.findFreeSpace(self.shstrtab.items.len, 1); |
| 573 | | log.debug("found shstrtab free space 0x{x} to 0x{x}", .{ off, off + self.shstrtab.items.len }); |
| 574 | | try self.sections.append(self.base.allocator, .{ |
| 575 | | .sh_name = try self.makeString(".shstrtab"), |
| 576 | | .sh_type = elf.SHT_STRTAB, |
| 577 | | .sh_flags = 0, |
| 578 | | .sh_addr = 0, |
| 579 | | .sh_offset = off, |
| 580 | | .sh_size = self.shstrtab.items.len, |
| 581 | | .sh_link = 0, |
| 582 | | .sh_info = 0, |
| 583 | | .sh_addralign = 1, |
| 584 | | .sh_entsize = 0, |
| 548 | self.shstrtab_index = @intCast(u16, self.sections.slice().len); |
| 549 | assert(self.shstrtab.buffer.items.len == 0); |
| 550 | try self.shstrtab.buffer.append(gpa, 0); // need a 0 at position 0 |
| 551 | const off = self.findFreeSpace(self.shstrtab.buffer.items.len, 1); |
| 552 | log.debug("found shstrtab free space 0x{x} to 0x{x}", .{ off, off + self.shstrtab.buffer.items.len }); |
| 553 | try self.sections.append(gpa, .{ |
| 554 | .shdr = .{ |
| 555 | .sh_name = try self.shstrtab.insert(gpa, ".shstrtab"), |
| 556 | .sh_type = elf.SHT_STRTAB, |
| 557 | .sh_flags = 0, |
| 558 | .sh_addr = 0, |
| 559 | .sh_offset = off, |
| 560 | .sh_size = self.shstrtab.buffer.items.len, |
| 561 | .sh_link = 0, |
| 562 | .sh_info = 0, |
| 563 | .sh_addralign = 1, |
| 564 | .sh_entsize = 0, |
| 565 | }, |
| 566 | .phdr_index = undefined, |
| 585 | 567 | }); |
| 586 | 568 | self.shstrtab_dirty = true; |
| 587 | 569 | self.shdr_table_dirty = true; |
| 588 | 570 | } |
| 589 | 571 | |
| 590 | 572 | if (self.text_section_index == null) { |
| 591 | | self.text_section_index = @intCast(u16, self.sections.items.len); |
| 573 | self.text_section_index = @intCast(u16, self.sections.slice().len); |
| 592 | 574 | const phdr = &self.program_headers.items[self.phdr_load_re_index.?]; |
| 593 | 575 | |
| 594 | | try self.sections.append(self.base.allocator, .{ |
| 595 | | .sh_name = try self.makeString(".text"), |
| 596 | | .sh_type = elf.SHT_PROGBITS, |
| 597 | | .sh_flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| 598 | | .sh_addr = phdr.p_vaddr, |
| 599 | | .sh_offset = phdr.p_offset, |
| 600 | | .sh_size = phdr.p_filesz, |
| 601 | | .sh_link = 0, |
| 602 | | .sh_info = 0, |
| 603 | | .sh_addralign = 1, |
| 604 | | .sh_entsize = 0, |
| 576 | try self.sections.append(gpa, .{ |
| 577 | .shdr = .{ |
| 578 | .sh_name = try self.shstrtab.insert(gpa, ".text"), |
| 579 | .sh_type = elf.SHT_PROGBITS, |
| 580 | .sh_flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| 581 | .sh_addr = phdr.p_vaddr, |
| 582 | .sh_offset = phdr.p_offset, |
| 583 | .sh_size = phdr.p_filesz, |
| 584 | .sh_link = 0, |
| 585 | .sh_info = 0, |
| 586 | .sh_addralign = 1, |
| 587 | .sh_entsize = 0, |
| 588 | }, |
| 589 | .phdr_index = self.phdr_load_re_index.?, |
| 605 | 590 | }); |
| 606 | | try self.phdr_shdr_table.putNoClobber( |
| 607 | | self.base.allocator, |
| 608 | | self.phdr_load_re_index.?, |
| 609 | | self.text_section_index.?, |
| 610 | | ); |
| 611 | 591 | self.shdr_table_dirty = true; |
| 612 | 592 | } |
| 613 | 593 | |
| 614 | 594 | if (self.got_section_index == null) { |
| 615 | | self.got_section_index = @intCast(u16, self.sections.items.len); |
| 595 | self.got_section_index = @intCast(u16, self.sections.slice().len); |
| 616 | 596 | const phdr = &self.program_headers.items[self.phdr_got_index.?]; |
| 617 | 597 | |
| 618 | | try self.sections.append(self.base.allocator, .{ |
| 619 | | .sh_name = try self.makeString(".got"), |
| 620 | | .sh_type = elf.SHT_PROGBITS, |
| 621 | | .sh_flags = elf.SHF_ALLOC, |
| 622 | | .sh_addr = phdr.p_vaddr, |
| 623 | | .sh_offset = phdr.p_offset, |
| 624 | | .sh_size = phdr.p_filesz, |
| 625 | | .sh_link = 0, |
| 626 | | .sh_info = 0, |
| 627 | | .sh_addralign = @as(u16, ptr_size), |
| 628 | | .sh_entsize = 0, |
| 598 | try self.sections.append(gpa, .{ |
| 599 | .shdr = .{ |
| 600 | .sh_name = try self.shstrtab.insert(gpa, ".got"), |
| 601 | .sh_type = elf.SHT_PROGBITS, |
| 602 | .sh_flags = elf.SHF_ALLOC, |
| 603 | .sh_addr = phdr.p_vaddr, |
| 604 | .sh_offset = phdr.p_offset, |
| 605 | .sh_size = phdr.p_filesz, |
| 606 | .sh_link = 0, |
| 607 | .sh_info = 0, |
| 608 | .sh_addralign = @as(u16, ptr_size), |
| 609 | .sh_entsize = 0, |
| 610 | }, |
| 611 | .phdr_index = self.phdr_got_index.?, |
| 629 | 612 | }); |
| 630 | | try self.phdr_shdr_table.putNoClobber( |
| 631 | | self.base.allocator, |
| 632 | | self.phdr_got_index.?, |
| 633 | | self.got_section_index.?, |
| 634 | | ); |
| 635 | 613 | self.shdr_table_dirty = true; |
| 636 | 614 | } |
| 637 | 615 | |
| 638 | 616 | if (self.rodata_section_index == null) { |
| 639 | | self.rodata_section_index = @intCast(u16, self.sections.items.len); |
| 617 | self.rodata_section_index = @intCast(u16, self.sections.slice().len); |
| 640 | 618 | const phdr = &self.program_headers.items[self.phdr_load_ro_index.?]; |
| 641 | 619 | |
| 642 | | try self.sections.append(self.base.allocator, .{ |
| 643 | | .sh_name = try self.makeString(".rodata"), |
| 644 | | .sh_type = elf.SHT_PROGBITS, |
| 645 | | .sh_flags = elf.SHF_ALLOC, |
| 646 | | .sh_addr = phdr.p_vaddr, |
| 647 | | .sh_offset = phdr.p_offset, |
| 648 | | .sh_size = phdr.p_filesz, |
| 649 | | .sh_link = 0, |
| 650 | | .sh_info = 0, |
| 651 | | .sh_addralign = 1, |
| 652 | | .sh_entsize = 0, |
| 620 | try self.sections.append(gpa, .{ |
| 621 | .shdr = .{ |
| 622 | .sh_name = try self.shstrtab.insert(gpa, ".rodata"), |
| 623 | .sh_type = elf.SHT_PROGBITS, |
| 624 | .sh_flags = elf.SHF_ALLOC, |
| 625 | .sh_addr = phdr.p_vaddr, |
| 626 | .sh_offset = phdr.p_offset, |
| 627 | .sh_size = phdr.p_filesz, |
| 628 | .sh_link = 0, |
| 629 | .sh_info = 0, |
| 630 | .sh_addralign = 1, |
| 631 | .sh_entsize = 0, |
| 632 | }, |
| 633 | .phdr_index = self.phdr_load_ro_index.?, |
| 653 | 634 | }); |
| 654 | | try self.phdr_shdr_table.putNoClobber( |
| 655 | | self.base.allocator, |
| 656 | | self.phdr_load_ro_index.?, |
| 657 | | self.rodata_section_index.?, |
| 658 | | ); |
| 659 | 635 | self.shdr_table_dirty = true; |
| 660 | 636 | } |
| 661 | 637 | |
| 662 | 638 | if (self.data_section_index == null) { |
| 663 | | self.data_section_index = @intCast(u16, self.sections.items.len); |
| 639 | self.data_section_index = @intCast(u16, self.sections.slice().len); |
| 664 | 640 | const phdr = &self.program_headers.items[self.phdr_load_rw_index.?]; |
| 665 | 641 | |
| 666 | | try self.sections.append(self.base.allocator, .{ |
| 667 | | .sh_name = try self.makeString(".data"), |
| 668 | | .sh_type = elf.SHT_PROGBITS, |
| 669 | | .sh_flags = elf.SHF_WRITE | elf.SHF_ALLOC, |
| 670 | | .sh_addr = phdr.p_vaddr, |
| 671 | | .sh_offset = phdr.p_offset, |
| 672 | | .sh_size = phdr.p_filesz, |
| 673 | | .sh_link = 0, |
| 674 | | .sh_info = 0, |
| 675 | | .sh_addralign = @as(u16, ptr_size), |
| 676 | | .sh_entsize = 0, |
| 642 | try self.sections.append(gpa, .{ |
| 643 | .shdr = .{ |
| 644 | .sh_name = try self.shstrtab.insert(gpa, ".data"), |
| 645 | .sh_type = elf.SHT_PROGBITS, |
| 646 | .sh_flags = elf.SHF_WRITE | elf.SHF_ALLOC, |
| 647 | .sh_addr = phdr.p_vaddr, |
| 648 | .sh_offset = phdr.p_offset, |
| 649 | .sh_size = phdr.p_filesz, |
| 650 | .sh_link = 0, |
| 651 | .sh_info = 0, |
| 652 | .sh_addralign = @as(u16, ptr_size), |
| 653 | .sh_entsize = 0, |
| 654 | }, |
| 655 | .phdr_index = self.phdr_load_rw_index.?, |
| 677 | 656 | }); |
| 678 | | try self.phdr_shdr_table.putNoClobber( |
| 679 | | self.base.allocator, |
| 680 | | self.phdr_load_rw_index.?, |
| 681 | | self.data_section_index.?, |
| 682 | | ); |
| 683 | 657 | self.shdr_table_dirty = true; |
| 684 | 658 | } |
| 685 | 659 | |
| 686 | 660 | if (self.symtab_section_index == null) { |
| 687 | | self.symtab_section_index = @intCast(u16, self.sections.items.len); |
| 661 | self.symtab_section_index = @intCast(u16, self.sections.slice().len); |
| 688 | 662 | const min_align: u16 = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym); |
| 689 | 663 | const each_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym); |
| 690 | 664 | const file_size = self.base.options.symbol_count_hint * each_size; |
| 691 | 665 | const off = self.findFreeSpace(file_size, min_align); |
| 692 | 666 | log.debug("found symtab free space 0x{x} to 0x{x}", .{ off, off + file_size }); |
| 693 | 667 | |
| 694 | | try self.sections.append(self.base.allocator, .{ |
| 695 | | .sh_name = try self.makeString(".symtab"), |
| 696 | | .sh_type = elf.SHT_SYMTAB, |
| 697 | | .sh_flags = 0, |
| 698 | | .sh_addr = 0, |
| 699 | | .sh_offset = off, |
| 700 | | .sh_size = file_size, |
| 701 | | // The section header index of the associated string table. |
| 702 | | .sh_link = self.shstrtab_index.?, |
| 703 | | .sh_info = @intCast(u32, self.local_symbols.items.len), |
| 704 | | .sh_addralign = min_align, |
| 705 | | .sh_entsize = each_size, |
| 668 | try self.sections.append(gpa, .{ |
| 669 | .shdr = .{ |
| 670 | .sh_name = try self.shstrtab.insert(gpa, ".symtab"), |
| 671 | .sh_type = elf.SHT_SYMTAB, |
| 672 | .sh_flags = 0, |
| 673 | .sh_addr = 0, |
| 674 | .sh_offset = off, |
| 675 | .sh_size = file_size, |
| 676 | // The section header index of the associated string table. |
| 677 | .sh_link = self.shstrtab_index.?, |
| 678 | .sh_info = @intCast(u32, self.local_symbols.items.len), |
| 679 | .sh_addralign = min_align, |
| 680 | .sh_entsize = each_size, |
| 681 | }, |
| 682 | .phdr_index = undefined, |
| 706 | 683 | }); |
| 707 | 684 | self.shdr_table_dirty = true; |
| 708 | 685 | try self.writeSymbol(0); |
| 709 | 686 | } |
| 710 | 687 | |
| 711 | | if (self.dwarf) |*dw| { |
| 712 | | if (self.debug_str_section_index == null) { |
| 713 | | self.debug_str_section_index = @intCast(u16, self.sections.items.len); |
| 714 | | assert(dw.strtab.items.len == 0); |
| 715 | | try dw.strtab.append(self.base.allocator, 0); |
| 716 | | try self.sections.append(self.base.allocator, .{ |
| 717 | | .sh_name = try self.makeString(".debug_str"), |
| 718 | | .sh_type = elf.SHT_PROGBITS, |
| 719 | | .sh_flags = elf.SHF_MERGE | elf.SHF_STRINGS, |
| 720 | | .sh_addr = 0, |
| 721 | | .sh_offset = 0, |
| 722 | | .sh_size = 0, |
| 723 | | .sh_link = 0, |
| 724 | | .sh_info = 0, |
| 725 | | .sh_addralign = 1, |
| 726 | | .sh_entsize = 1, |
| 727 | | }); |
| 728 | | self.debug_strtab_dirty = true; |
| 729 | | self.shdr_table_dirty = true; |
| 730 | | } |
| 731 | | |
| 732 | | if (self.debug_info_section_index == null) { |
| 733 | | self.debug_info_section_index = @intCast(u16, self.sections.items.len); |
| 734 | | |
| 735 | | const file_size_hint = 200; |
| 736 | | const p_align = 1; |
| 737 | | const off = self.findFreeSpace(file_size_hint, p_align); |
| 738 | | log.debug("found .debug_info free space 0x{x} to 0x{x}", .{ |
| 739 | | off, |
| 740 | | off + file_size_hint, |
| 741 | | }); |
| 742 | | try self.sections.append(self.base.allocator, .{ |
| 743 | | .sh_name = try self.makeString(".debug_info"), |
| 744 | | .sh_type = elf.SHT_PROGBITS, |
| 745 | | .sh_flags = 0, |
| 746 | | .sh_addr = 0, |
| 747 | | .sh_offset = off, |
| 748 | | .sh_size = file_size_hint, |
| 749 | | .sh_link = 0, |
| 750 | | .sh_info = 0, |
| 751 | | .sh_addralign = p_align, |
| 752 | | .sh_entsize = 0, |
| 753 | | }); |
| 754 | | self.shdr_table_dirty = true; |
| 755 | | self.debug_info_header_dirty = true; |
| 756 | | } |
| 757 | | |
| 758 | | if (self.debug_abbrev_section_index == null) { |
| 759 | | self.debug_abbrev_section_index = @intCast(u16, self.sections.items.len); |
| 760 | | |
| 761 | | const file_size_hint = 128; |
| 762 | | const p_align = 1; |
| 763 | | const off = self.findFreeSpace(file_size_hint, p_align); |
| 764 | | log.debug("found .debug_abbrev free space 0x{x} to 0x{x}", .{ |
| 765 | | off, |
| 766 | | off + file_size_hint, |
| 767 | | }); |
| 768 | | try self.sections.append(self.base.allocator, .{ |
| 769 | | .sh_name = try self.makeString(".debug_abbrev"), |
| 770 | | .sh_type = elf.SHT_PROGBITS, |
| 771 | | .sh_flags = 0, |
| 772 | | .sh_addr = 0, |
| 773 | | .sh_offset = off, |
| 774 | | .sh_size = file_size_hint, |
| 775 | | .sh_link = 0, |
| 776 | | .sh_info = 0, |
| 777 | | .sh_addralign = p_align, |
| 778 | | .sh_entsize = 0, |
| 779 | | }); |
| 780 | | self.shdr_table_dirty = true; |
| 781 | | self.debug_abbrev_section_dirty = true; |
| 782 | | } |
| 783 | | |
| 784 | | if (self.debug_aranges_section_index == null) { |
| 785 | | self.debug_aranges_section_index = @intCast(u16, self.sections.items.len); |
| 786 | | |
| 787 | | const file_size_hint = 160; |
| 788 | | const p_align = 16; |
| 789 | | const off = self.findFreeSpace(file_size_hint, p_align); |
| 790 | | log.debug("found .debug_aranges free space 0x{x} to 0x{x}", .{ |
| 791 | | off, |
| 792 | | off + file_size_hint, |
| 793 | | }); |
| 794 | | try self.sections.append(self.base.allocator, .{ |
| 795 | | .sh_name = try self.makeString(".debug_aranges"), |
| 796 | | .sh_type = elf.SHT_PROGBITS, |
| 797 | | .sh_flags = 0, |
| 798 | | .sh_addr = 0, |
| 799 | | .sh_offset = off, |
| 800 | | .sh_size = file_size_hint, |
| 801 | | .sh_link = 0, |
| 802 | | .sh_info = 0, |
| 803 | | .sh_addralign = p_align, |
| 804 | | .sh_entsize = 0, |
| 805 | | }); |
| 806 | | self.shdr_table_dirty = true; |
| 807 | | self.debug_aranges_section_dirty = true; |
| 808 | | } |
| 809 | | |
| 810 | | if (self.debug_line_section_index == null) { |
| 811 | | self.debug_line_section_index = @intCast(u16, self.sections.items.len); |
| 812 | | |
| 813 | | const file_size_hint = 250; |
| 814 | | const p_align = 1; |
| 815 | | const off = self.findFreeSpace(file_size_hint, p_align); |
| 816 | | log.debug("found .debug_line free space 0x{x} to 0x{x}", .{ |
| 817 | | off, |
| 818 | | off + file_size_hint, |
| 819 | | }); |
| 820 | | try self.sections.append(self.base.allocator, .{ |
| 821 | | .sh_name = try self.makeString(".debug_line"), |
| 822 | | .sh_type = elf.SHT_PROGBITS, |
| 823 | | .sh_flags = 0, |
| 824 | | .sh_addr = 0, |
| 825 | | .sh_offset = off, |
| 826 | | .sh_size = file_size_hint, |
| 827 | | .sh_link = 0, |
| 828 | | .sh_info = 0, |
| 829 | | .sh_addralign = p_align, |
| 830 | | .sh_entsize = 0, |
| 831 | | }); |
| 832 | | self.shdr_table_dirty = true; |
| 833 | | self.debug_line_header_dirty = true; |
| 834 | | } |
| 835 | | } |
| 688 | // if (self.dwarf) |*dw| { |
| 689 | // if (self.debug_str_section_index == null) { |
| 690 | // self.debug_str_section_index = @intCast(u16, self.sections.slice().len); |
| 691 | // assert(dw.strtab.items.len == 0); |
| 692 | // try dw.strtab.append(gpa, 0); |
| 693 | // try self.sections.append(gpa, .{ |
| 694 | // .shdr = .{ |
| 695 | // .sh_name = try self.shstrtab.insert(gpa, ".debug_str"), |
| 696 | // .sh_type = elf.SHT_PROGBITS, |
| 697 | // .sh_flags = elf.SHF_MERGE | elf.SHF_STRINGS, |
| 698 | // .sh_addr = 0, |
| 699 | // .sh_offset = 0, |
| 700 | // .sh_size = 0, |
| 701 | // .sh_link = 0, |
| 702 | // .sh_info = 0, |
| 703 | // .sh_addralign = 1, |
| 704 | // .sh_entsize = 1, |
| 705 | // }, |
| 706 | // .phdr_index = undefined, |
| 707 | // }); |
| 708 | // self.debug_strtab_dirty = true; |
| 709 | // self.shdr_table_dirty = true; |
| 710 | // } |
| 711 | |
| 712 | // if (self.debug_info_section_index == null) { |
| 713 | // self.debug_info_section_index = @intCast(u16, self.sections.slice().len); |
| 714 | |
| 715 | // const file_size_hint = 200; |
| 716 | // const p_align = 1; |
| 717 | // const off = self.findFreeSpace(file_size_hint, p_align); |
| 718 | // log.debug("found .debug_info free space 0x{x} to 0x{x}", .{ |
| 719 | // off, |
| 720 | // off + file_size_hint, |
| 721 | // }); |
| 722 | // try self.sections.append(gpa, .{ |
| 723 | // .shdr = .{ |
| 724 | // .sh_name = try self.shstrtab.insert(gpa, ".debug_info"), |
| 725 | // .sh_type = elf.SHT_PROGBITS, |
| 726 | // .sh_flags = 0, |
| 727 | // .sh_addr = 0, |
| 728 | // .sh_offset = off, |
| 729 | // .sh_size = file_size_hint, |
| 730 | // .sh_link = 0, |
| 731 | // .sh_info = 0, |
| 732 | // .sh_addralign = p_align, |
| 733 | // .sh_entsize = 0, |
| 734 | // }, |
| 735 | // .phdr_index = undefined, |
| 736 | // }); |
| 737 | // self.shdr_table_dirty = true; |
| 738 | // self.debug_info_header_dirty = true; |
| 739 | // } |
| 740 | |
| 741 | // if (self.debug_abbrev_section_index == null) { |
| 742 | // self.debug_abbrev_section_index = @intCast(u16, self.sections.slice().len); |
| 743 | |
| 744 | // const file_size_hint = 128; |
| 745 | // const p_align = 1; |
| 746 | // const off = self.findFreeSpace(file_size_hint, p_align); |
| 747 | // log.debug("found .debug_abbrev free space 0x{x} to 0x{x}", .{ |
| 748 | // off, |
| 749 | // off + file_size_hint, |
| 750 | // }); |
| 751 | // try self.sections.append(gpa, .{ |
| 752 | // .shdr = .{ |
| 753 | // .sh_name = try self.shstrtab.insert(gpa, ".debug_abbrev"), |
| 754 | // .sh_type = elf.SHT_PROGBITS, |
| 755 | // .sh_flags = 0, |
| 756 | // .sh_addr = 0, |
| 757 | // .sh_offset = off, |
| 758 | // .sh_size = file_size_hint, |
| 759 | // .sh_link = 0, |
| 760 | // .sh_info = 0, |
| 761 | // .sh_addralign = p_align, |
| 762 | // .sh_entsize = 0, |
| 763 | // }, |
| 764 | // .phdr_index = undefined, |
| 765 | // }); |
| 766 | // self.shdr_table_dirty = true; |
| 767 | // self.debug_abbrev_section_dirty = true; |
| 768 | // } |
| 769 | |
| 770 | // if (self.debug_aranges_section_index == null) { |
| 771 | // self.debug_aranges_section_index = @intCast(u16, self.sections.slice().len); |
| 772 | |
| 773 | // const file_size_hint = 160; |
| 774 | // const p_align = 16; |
| 775 | // const off = self.findFreeSpace(file_size_hint, p_align); |
| 776 | // log.debug("found .debug_aranges free space 0x{x} to 0x{x}", .{ |
| 777 | // off, |
| 778 | // off + file_size_hint, |
| 779 | // }); |
| 780 | // try self.sections.append(gpa, .{ |
| 781 | // .shdr = .{ |
| 782 | // .sh_name = try self.shstrtab.insert(gpa, ".debug_aranges"), |
| 783 | // .sh_type = elf.SHT_PROGBITS, |
| 784 | // .sh_flags = 0, |
| 785 | // .sh_addr = 0, |
| 786 | // .sh_offset = off, |
| 787 | // .sh_size = file_size_hint, |
| 788 | // .sh_link = 0, |
| 789 | // .sh_info = 0, |
| 790 | // .sh_addralign = p_align, |
| 791 | // .sh_entsize = 0, |
| 792 | // }, |
| 793 | // .phdr_index = undefined, |
| 794 | // }); |
| 795 | // self.shdr_table_dirty = true; |
| 796 | // self.debug_aranges_section_dirty = true; |
| 797 | // } |
| 798 | |
| 799 | // if (self.debug_line_section_index == null) { |
| 800 | // self.debug_line_section_index = @intCast(u16, self.sections.slice().len); |
| 801 | |
| 802 | // const file_size_hint = 250; |
| 803 | // const p_align = 1; |
| 804 | // const off = self.findFreeSpace(file_size_hint, p_align); |
| 805 | // log.debug("found .debug_line free space 0x{x} to 0x{x}", .{ |
| 806 | // off, |
| 807 | // off + file_size_hint, |
| 808 | // }); |
| 809 | // try self.sections.append(gpa, .{ |
| 810 | // .shdr = .{ |
| 811 | // .sh_name = try self.shstrtab.insert(gpa, ".debug_line"), |
| 812 | // .sh_type = elf.SHT_PROGBITS, |
| 813 | // .sh_flags = 0, |
| 814 | // .sh_addr = 0, |
| 815 | // .sh_offset = off, |
| 816 | // .sh_size = file_size_hint, |
| 817 | // .sh_link = 0, |
| 818 | // .sh_info = 0, |
| 819 | // .sh_addralign = p_align, |
| 820 | // .sh_entsize = 0, |
| 821 | // }, |
| 822 | // .phdr_index = undefined, |
| 823 | // }); |
| 824 | // self.shdr_table_dirty = true; |
| 825 | // self.debug_line_header_dirty = true; |
| 826 | // } |
| 827 | // } |
| 836 | 828 | |
| 837 | 829 | const shsize: u64 = switch (self.ptr_width) { |
| 838 | 830 | .p32 => @sizeOf(elf.Elf32_Shdr), |
| ... | ... | @@ -843,7 +835,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 843 | 835 | .p64 => @alignOf(elf.Elf64_Shdr), |
| 844 | 836 | }; |
| 845 | 837 | if (self.shdr_table_offset == null) { |
| 846 | | self.shdr_table_offset = self.findFreeSpace(self.sections.items.len * shsize, shalign); |
| 838 | self.shdr_table_offset = self.findFreeSpace(self.sections.slice().len * shsize, shalign); |
| 847 | 839 | self.shdr_table_dirty = true; |
| 848 | 840 | } |
| 849 | 841 | |
| ... | ... | @@ -874,7 +866,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 874 | 866 | // offset + it's filesize. |
| 875 | 867 | var max_file_offset: u64 = 0; |
| 876 | 868 | |
| 877 | | for (self.sections.items) |shdr| { |
| 869 | for (self.sections.items(.shdr)) |shdr| { |
| 878 | 870 | if (shdr.sh_offset + shdr.sh_size > max_file_offset) { |
| 879 | 871 | max_file_offset = shdr.sh_offset + shdr.sh_size; |
| 880 | 872 | } |
| ... | ... | @@ -884,15 +876,18 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 884 | 876 | } |
| 885 | 877 | } |
| 886 | 878 | |
| 887 | | fn growAllocSection(self: *Elf, shdr_index: u16, phdr_index: u16, needed_size: u64) !void { |
| 879 | fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void { |
| 888 | 880 | // TODO Also detect virtual address collisions. |
| 889 | | const shdr = &self.sections.items[shdr_index]; |
| 881 | const shdr = &self.sections.items(.shdr)[shdr_index]; |
| 882 | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 890 | 883 | const phdr = &self.program_headers.items[phdr_index]; |
| 884 | const maybe_last_atom_index = self.sections.items(.last_atom_index)[shdr_index]; |
| 891 | 885 | |
| 892 | 886 | if (needed_size > self.allocatedSize(shdr.sh_offset)) { |
| 893 | 887 | // Must move the entire section. |
| 894 | 888 | const new_offset = self.findFreeSpace(needed_size, self.page_size); |
| 895 | | const existing_size = if (self.atoms.get(phdr_index)) |last| blk: { |
| 889 | const existing_size = if (maybe_last_atom_index) |last_atom_index| blk: { |
| 890 | const last = self.getAtom(last_atom_index); |
| 896 | 891 | const sym = last.getSymbol(self); |
| 897 | 892 | break :blk (sym.st_value + sym.st_size) - phdr.p_vaddr; |
| 898 | 893 | } else if (shdr_index == self.got_section_index.?) blk: { |
| ... | ... | @@ -900,8 +895,8 @@ fn growAllocSection(self: *Elf, shdr_index: u16, phdr_index: u16, needed_size: u |
| 900 | 895 | } else 0; |
| 901 | 896 | shdr.sh_size = 0; |
| 902 | 897 | |
| 903 | | log.debug("new '{s}' file offset 0x{x} to 0x{x}", .{ |
| 904 | | self.getString(shdr.sh_name), |
| 898 | log.debug("new '{?s}' file offset 0x{x} to 0x{x}", .{ |
| 899 | self.shstrtab.get(shdr.sh_name), |
| 905 | 900 | new_offset, |
| 906 | 901 | new_offset + existing_size, |
| 907 | 902 | }); |
| ... | ... | @@ -927,7 +922,7 @@ pub fn growNonAllocSection( |
| 927 | 922 | min_alignment: u32, |
| 928 | 923 | requires_file_copy: bool, |
| 929 | 924 | ) !void { |
| 930 | | const shdr = &self.sections.items[shdr_index]; |
| 925 | const shdr = &self.sections.items(.shdr)[shdr_index]; |
| 931 | 926 | |
| 932 | 927 | if (needed_size > self.allocatedSize(shdr.sh_offset)) { |
| 933 | 928 | const existing_size = if (self.symtab_section_index.? == shdr_index) blk: { |
| ... | ... | @@ -940,7 +935,7 @@ pub fn growNonAllocSection( |
| 940 | 935 | shdr.sh_size = 0; |
| 941 | 936 | // Move all the symbols to a new file location. |
| 942 | 937 | const new_offset = self.findFreeSpace(needed_size, min_alignment); |
| 943 | | log.debug("moving '{s}' from 0x{x} to 0x{x}", .{ self.getString(shdr.sh_name), shdr.sh_offset, new_offset }); |
| 938 | log.debug("moving '{?s}' from 0x{x} to 0x{x}", .{ self.shstrtab.get(shdr.sh_name), shdr.sh_offset, new_offset }); |
| 944 | 939 | |
| 945 | 940 | if (requires_file_copy) { |
| 946 | 941 | const amt = try self.base.file.?.copyRangeAll( |
| ... | ... | @@ -961,25 +956,26 @@ pub fn growNonAllocSection( |
| 961 | 956 | } |
| 962 | 957 | |
| 963 | 958 | pub fn markDirty(self: *Elf, shdr_index: u16, phdr_index: ?u16) void { |
| 959 | _ = shdr_index; |
| 964 | 960 | self.shdr_table_dirty = true; // TODO look into only writing one section |
| 965 | 961 | |
| 966 | 962 | if (phdr_index) |_| { |
| 967 | 963 | self.phdr_table_dirty = true; // TODO look into making only the one program header dirty |
| 968 | 964 | } |
| 969 | 965 | |
| 970 | | if (self.dwarf) |_| { |
| 971 | | if (self.debug_info_section_index.? == shdr_index) { |
| 972 | | self.debug_info_header_dirty = true; |
| 973 | | } else if (self.debug_line_section_index.? == shdr_index) { |
| 974 | | self.debug_line_header_dirty = true; |
| 975 | | } else if (self.debug_abbrev_section_index.? == shdr_index) { |
| 976 | | self.debug_abbrev_section_dirty = true; |
| 977 | | } else if (self.debug_str_section_index.? == shdr_index) { |
| 978 | | self.debug_strtab_dirty = true; |
| 979 | | } else if (self.debug_aranges_section_index.? == shdr_index) { |
| 980 | | self.debug_aranges_section_dirty = true; |
| 981 | | } |
| 982 | | } |
| 966 | // if (self.dwarf) |_| { |
| 967 | // if (self.debug_info_section_index.? == shdr_index) { |
| 968 | // self.debug_info_header_dirty = true; |
| 969 | // } else if (self.debug_line_section_index.? == shdr_index) { |
| 970 | // self.debug_line_header_dirty = true; |
| 971 | // } else if (self.debug_abbrev_section_index.? == shdr_index) { |
| 972 | // self.debug_abbrev_section_dirty = true; |
| 973 | // } else if (self.debug_str_section_index.? == shdr_index) { |
| 974 | // self.debug_strtab_dirty = true; |
| 975 | // } else if (self.debug_aranges_section_index.? == shdr_index) { |
| 976 | // self.debug_aranges_section_dirty = true; |
| 977 | // } |
| 978 | // } |
| 983 | 979 | } |
| 984 | 980 | |
| 985 | 981 | pub fn flush(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) link.File.FlushError!void { |
| ... | ... | @@ -1011,6 +1007,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1011 | 1007 | } |
| 1012 | 1008 | } |
| 1013 | 1009 | |
| 1010 | const gpa = self.base.allocator; |
| 1014 | 1011 | var sub_prog_node = prog_node.start("ELF Flush", 0); |
| 1015 | 1012 | sub_prog_node.activate(); |
| 1016 | 1013 | defer sub_prog_node.end(); |
| ... | ... | @@ -1018,23 +1015,25 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1018 | 1015 | // TODO This linker code currently assumes there is only 1 compilation unit and it |
| 1019 | 1016 | // corresponds to the Zig source code. |
| 1020 | 1017 | const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented; |
| 1018 | _ = module; |
| 1021 | 1019 | |
| 1022 | 1020 | const target_endian = self.base.options.target.cpu.arch.endian(); |
| 1023 | 1021 | const foreign_endian = target_endian != builtin.cpu.arch.endian(); |
| 1024 | 1022 | |
| 1025 | | if (self.dwarf) |*dw| { |
| 1026 | | try dw.flushModule(module); |
| 1027 | | } |
| 1023 | // if (self.dwarf) |*dw| { |
| 1024 | // try dw.flushModule(module); |
| 1025 | // } |
| 1028 | 1026 | |
| 1029 | 1027 | { |
| 1030 | 1028 | var it = self.relocs.iterator(); |
| 1031 | 1029 | while (it.next()) |entry| { |
| 1032 | | const atom = entry.key_ptr.*; |
| 1030 | const atom_index = entry.key_ptr.*; |
| 1033 | 1031 | const relocs = entry.value_ptr.*; |
| 1032 | const atom = self.getAtom(atom_index); |
| 1034 | 1033 | const source_sym = atom.getSymbol(self); |
| 1035 | | const source_shdr = self.sections.items[source_sym.st_shndx]; |
| 1034 | const source_shdr = self.sections.items(.shdr)[source_sym.st_shndx]; |
| 1036 | 1035 | |
| 1037 | | log.debug("relocating '{s}'", .{self.getString(source_sym.st_name)}); |
| 1036 | log.debug("relocating '{?s}'", .{self.shstrtab.get(source_sym.st_name)}); |
| 1038 | 1037 | |
| 1039 | 1038 | for (relocs.items) |*reloc| { |
| 1040 | 1039 | const target_sym = self.local_symbols.items[reloc.target]; |
| ... | ... | @@ -1045,10 +1044,10 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1045 | 1044 | const section_offset = (source_sym.st_value + reloc.offset) - source_shdr.sh_addr; |
| 1046 | 1045 | const file_offset = source_shdr.sh_offset + section_offset; |
| 1047 | 1046 | |
| 1048 | | log.debug(" ({x}: [() => 0x{x}] ({s}))", .{ |
| 1047 | log.debug(" ({x}: [() => 0x{x}] ({?s}))", .{ |
| 1049 | 1048 | reloc.offset, |
| 1050 | 1049 | target_vaddr, |
| 1051 | | self.getString(target_sym.st_name), |
| 1050 | self.shstrtab.get(target_sym.st_name), |
| 1052 | 1051 | }); |
| 1053 | 1052 | |
| 1054 | 1053 | switch (self.ptr_width) { |
| ... | ... | @@ -1069,43 +1068,43 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1069 | 1068 | self.logSymtab(); |
| 1070 | 1069 | } |
| 1071 | 1070 | |
| 1072 | | if (self.dwarf) |*dw| { |
| 1073 | | if (self.debug_abbrev_section_dirty) { |
| 1074 | | try dw.writeDbgAbbrev(); |
| 1075 | | if (!self.shdr_table_dirty) { |
| 1076 | | // Then it won't get written with the others and we need to do it. |
| 1077 | | try self.writeSectHeader(self.debug_abbrev_section_index.?); |
| 1078 | | } |
| 1079 | | self.debug_abbrev_section_dirty = false; |
| 1080 | | } |
| 1081 | | |
| 1082 | | if (self.debug_info_header_dirty) { |
| 1083 | | // Currently only one compilation unit is supported, so the address range is simply |
| 1084 | | // identical to the main program header virtual address and memory size. |
| 1085 | | const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?]; |
| 1086 | | const low_pc = text_phdr.p_vaddr; |
| 1087 | | const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz; |
| 1088 | | try dw.writeDbgInfoHeader(module, low_pc, high_pc); |
| 1089 | | self.debug_info_header_dirty = false; |
| 1090 | | } |
| 1091 | | |
| 1092 | | if (self.debug_aranges_section_dirty) { |
| 1093 | | // Currently only one compilation unit is supported, so the address range is simply |
| 1094 | | // identical to the main program header virtual address and memory size. |
| 1095 | | const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?]; |
| 1096 | | try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz); |
| 1097 | | if (!self.shdr_table_dirty) { |
| 1098 | | // Then it won't get written with the others and we need to do it. |
| 1099 | | try self.writeSectHeader(self.debug_aranges_section_index.?); |
| 1100 | | } |
| 1101 | | self.debug_aranges_section_dirty = false; |
| 1102 | | } |
| 1103 | | |
| 1104 | | if (self.debug_line_header_dirty) { |
| 1105 | | try dw.writeDbgLineHeader(); |
| 1106 | | self.debug_line_header_dirty = false; |
| 1107 | | } |
| 1108 | | } |
| 1071 | // if (self.dwarf) |*dw| { |
| 1072 | // if (self.debug_abbrev_section_dirty) { |
| 1073 | // try dw.writeDbgAbbrev(); |
| 1074 | // if (!self.shdr_table_dirty) { |
| 1075 | // // Then it won't get written with the others and we need to do it. |
| 1076 | // try self.writeSectHeader(self.debug_abbrev_section_index.?); |
| 1077 | // } |
| 1078 | // self.debug_abbrev_section_dirty = false; |
| 1079 | // } |
| 1080 | |
| 1081 | // if (self.debug_info_header_dirty) { |
| 1082 | // // Currently only one compilation unit is supported, so the address range is simply |
| 1083 | // // identical to the main program header virtual address and memory size. |
| 1084 | // const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?]; |
| 1085 | // const low_pc = text_phdr.p_vaddr; |
| 1086 | // const high_pc = text_phdr.p_vaddr + text_phdr.p_memsz; |
| 1087 | // try dw.writeDbgInfoHeader(module, low_pc, high_pc); |
| 1088 | // self.debug_info_header_dirty = false; |
| 1089 | // } |
| 1090 | |
| 1091 | // if (self.debug_aranges_section_dirty) { |
| 1092 | // // Currently only one compilation unit is supported, so the address range is simply |
| 1093 | // // identical to the main program header virtual address and memory size. |
| 1094 | // const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?]; |
| 1095 | // try dw.writeDbgAranges(text_phdr.p_vaddr, text_phdr.p_memsz); |
| 1096 | // if (!self.shdr_table_dirty) { |
| 1097 | // // Then it won't get written with the others and we need to do it. |
| 1098 | // try self.writeSectHeader(self.debug_aranges_section_index.?); |
| 1099 | // } |
| 1100 | // self.debug_aranges_section_dirty = false; |
| 1101 | // } |
| 1102 | |
| 1103 | // if (self.debug_line_header_dirty) { |
| 1104 | // try dw.writeDbgLineHeader(); |
| 1105 | // self.debug_line_header_dirty = false; |
| 1106 | // } |
| 1107 | // } |
| 1109 | 1108 | |
| 1110 | 1109 | if (self.phdr_table_dirty) { |
| 1111 | 1110 | const phsize: u64 = switch (self.ptr_width) { |
| ... | ... | @@ -1126,8 +1125,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1126 | 1125 | |
| 1127 | 1126 | switch (self.ptr_width) { |
| 1128 | 1127 | .p32 => { |
| 1129 | | const buf = try self.base.allocator.alloc(elf.Elf32_Phdr, self.program_headers.items.len); |
| 1130 | | defer self.base.allocator.free(buf); |
| 1128 | const buf = try gpa.alloc(elf.Elf32_Phdr, self.program_headers.items.len); |
| 1129 | defer gpa.free(buf); |
| 1131 | 1130 | |
| 1132 | 1131 | for (buf) |*phdr, i| { |
| 1133 | 1132 | phdr.* = progHeaderTo32(self.program_headers.items[i]); |
| ... | ... | @@ -1138,8 +1137,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1138 | 1137 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), self.phdr_table_offset.?); |
| 1139 | 1138 | }, |
| 1140 | 1139 | .p64 => { |
| 1141 | | const buf = try self.base.allocator.alloc(elf.Elf64_Phdr, self.program_headers.items.len); |
| 1142 | | defer self.base.allocator.free(buf); |
| 1140 | const buf = try gpa.alloc(elf.Elf64_Phdr, self.program_headers.items.len); |
| 1141 | defer gpa.free(buf); |
| 1143 | 1142 | |
| 1144 | 1143 | for (buf) |*phdr, i| { |
| 1145 | 1144 | phdr.* = self.program_headers.items[i]; |
| ... | ... | @@ -1155,23 +1154,23 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1155 | 1154 | |
| 1156 | 1155 | { |
| 1157 | 1156 | const shdr_index = self.shstrtab_index.?; |
| 1158 | | if (self.shstrtab_dirty or self.shstrtab.items.len != self.sections.items[shdr_index].sh_size) { |
| 1159 | | try self.growNonAllocSection(shdr_index, self.shstrtab.items.len, 1, false); |
| 1160 | | const shstrtab_sect = self.sections.items[shdr_index]; |
| 1161 | | try self.base.file.?.pwriteAll(self.shstrtab.items, shstrtab_sect.sh_offset); |
| 1157 | if (self.shstrtab_dirty or self.shstrtab.buffer.items.len != self.sections.items(.shdr)[shdr_index].sh_size) { |
| 1158 | try self.growNonAllocSection(shdr_index, self.shstrtab.buffer.items.len, 1, false); |
| 1159 | const shstrtab_sect = self.sections.items(.shdr)[shdr_index]; |
| 1160 | try self.base.file.?.pwriteAll(self.shstrtab.buffer.items, shstrtab_sect.sh_offset); |
| 1162 | 1161 | self.shstrtab_dirty = false; |
| 1163 | 1162 | } |
| 1164 | 1163 | } |
| 1165 | 1164 | |
| 1166 | | if (self.dwarf) |dwarf| { |
| 1167 | | const shdr_index = self.debug_str_section_index.?; |
| 1168 | | if (self.debug_strtab_dirty or dwarf.strtab.items.len != self.sections.items[shdr_index].sh_size) { |
| 1169 | | try self.growNonAllocSection(shdr_index, dwarf.strtab.items.len, 1, false); |
| 1170 | | const debug_strtab_sect = self.sections.items[shdr_index]; |
| 1171 | | try self.base.file.?.pwriteAll(dwarf.strtab.items, debug_strtab_sect.sh_offset); |
| 1172 | | self.debug_strtab_dirty = false; |
| 1173 | | } |
| 1174 | | } |
| 1165 | // if (self.dwarf) |dwarf| { |
| 1166 | // const shdr_index = self.debug_str_section_index.?; |
| 1167 | // if (self.debug_strtab_dirty or dwarf.strtab.items.len != self.sections.items(.shdr)[shdr_index].sh_size) { |
| 1168 | // try self.growNonAllocSection(shdr_index, dwarf.strtab.items.len, 1, false); |
| 1169 | // const debug_strtab_sect = self.sections.items(.shdr)[shdr_index]; |
| 1170 | // try self.base.file.?.pwriteAll(dwarf.strtab.items, debug_strtab_sect.sh_offset); |
| 1171 | // self.debug_strtab_dirty = false; |
| 1172 | // } |
| 1173 | // } |
| 1175 | 1174 | |
| 1176 | 1175 | if (self.shdr_table_dirty) { |
| 1177 | 1176 | const shsize: u64 = switch (self.ptr_width) { |
| ... | ... | @@ -1183,7 +1182,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1183 | 1182 | .p64 => @alignOf(elf.Elf64_Shdr), |
| 1184 | 1183 | }; |
| 1185 | 1184 | const allocated_size = self.allocatedSize(self.shdr_table_offset.?); |
| 1186 | | const needed_size = self.sections.items.len * shsize; |
| 1185 | const needed_size = self.sections.slice().len * shsize; |
| 1187 | 1186 | |
| 1188 | 1187 | if (needed_size > allocated_size) { |
| 1189 | 1188 | self.shdr_table_offset = null; // free the space |
| ... | ... | @@ -1192,12 +1191,13 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1192 | 1191 | |
| 1193 | 1192 | switch (self.ptr_width) { |
| 1194 | 1193 | .p32 => { |
| 1195 | | const buf = try self.base.allocator.alloc(elf.Elf32_Shdr, self.sections.items.len); |
| 1196 | | defer self.base.allocator.free(buf); |
| 1194 | const slice = self.sections.slice(); |
| 1195 | const buf = try gpa.alloc(elf.Elf32_Shdr, slice.len); |
| 1196 | defer gpa.free(buf); |
| 1197 | 1197 | |
| 1198 | 1198 | for (buf) |*shdr, i| { |
| 1199 | | shdr.* = sectHeaderTo32(self.sections.items[i]); |
| 1200 | | log.debug("writing section {s}: {}", .{ self.getString(shdr.sh_name), shdr.* }); |
| 1199 | shdr.* = sectHeaderTo32(slice.items(.shdr)[i]); |
| 1200 | log.debug("writing section {?s}: {}", .{ self.shstrtab.get(shdr.sh_name), shdr.* }); |
| 1201 | 1201 | if (foreign_endian) { |
| 1202 | 1202 | mem.byteSwapAllFields(elf.Elf32_Shdr, shdr); |
| 1203 | 1203 | } |
| ... | ... | @@ -1205,12 +1205,13 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1205 | 1205 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), self.shdr_table_offset.?); |
| 1206 | 1206 | }, |
| 1207 | 1207 | .p64 => { |
| 1208 | | const buf = try self.base.allocator.alloc(elf.Elf64_Shdr, self.sections.items.len); |
| 1209 | | defer self.base.allocator.free(buf); |
| 1208 | const slice = self.sections.slice(); |
| 1209 | const buf = try gpa.alloc(elf.Elf64_Shdr, slice.len); |
| 1210 | defer gpa.free(buf); |
| 1210 | 1211 | |
| 1211 | 1212 | for (buf) |*shdr, i| { |
| 1212 | | shdr.* = self.sections.items[i]; |
| 1213 | | log.debug("writing section {s}: {}", .{ self.getString(shdr.sh_name), shdr.* }); |
| 1213 | shdr.* = slice.items(.shdr)[i]; |
| 1214 | log.debug("writing section {?s}: {}", .{ self.shstrtab.get(shdr.sh_name), shdr.* }); |
| 1214 | 1215 | if (foreign_endian) { |
| 1215 | 1216 | mem.byteSwapAllFields(elf.Elf64_Shdr, shdr); |
| 1216 | 1217 | } |
| ... | ... | @@ -2021,7 +2022,7 @@ fn writeElfHeader(self: *Elf) !void { |
| 2021 | 2022 | mem.writeInt(u16, hdr_buf[index..][0..2], e_shentsize, endian); |
| 2022 | 2023 | index += 2; |
| 2023 | 2024 | |
| 2024 | | const e_shnum = @intCast(u16, self.sections.items.len); |
| 2025 | const e_shnum = @intCast(u16, self.sections.slice().len); |
| 2025 | 2026 | mem.writeInt(u16, hdr_buf[index..][0..2], e_shnum, endian); |
| 2026 | 2027 | index += 2; |
| 2027 | 2028 | |
| ... | ... | @@ -2033,124 +2034,150 @@ fn writeElfHeader(self: *Elf) !void { |
| 2033 | 2034 | try self.base.file.?.pwriteAll(hdr_buf[0..index], 0); |
| 2034 | 2035 | } |
| 2035 | 2036 | |
| 2036 | | fn freeTextBlock(self: *Elf, text_block: *TextBlock, phdr_index: u16) void { |
| 2037 | | const local_sym = text_block.getSymbol(self); |
| 2038 | | const name_str_index = local_sym.st_name; |
| 2039 | | const name = self.getString(name_str_index); |
| 2040 | | log.debug("freeTextBlock {*} ({s})", .{ text_block, name }); |
| 2037 | fn freeAtom(self: *Elf, atom_index: Atom.Index) void { |
| 2038 | const atom = self.getAtom(atom_index); |
| 2039 | log.debug("freeAtom {d} ({s})", .{ atom_index, atom.getName(self) }); |
| 2041 | 2040 | |
| 2042 | | self.freeRelocationsForTextBlock(text_block); |
| 2041 | Atom.freeRelocations(self, atom_index); |
| 2043 | 2042 | |
| 2044 | | const free_list = self.atom_free_lists.getPtr(phdr_index).?; |
| 2043 | const gpa = self.base.allocator; |
| 2044 | const shndx = atom.getSymbol(self).st_shndx; |
| 2045 | const free_list = &self.sections.items(.free_list)[shndx]; |
| 2045 | 2046 | var already_have_free_list_node = false; |
| 2046 | 2047 | { |
| 2047 | 2048 | var i: usize = 0; |
| 2048 | 2049 | // TODO turn free_list into a hash map |
| 2049 | 2050 | while (i < free_list.items.len) { |
| 2050 | | if (free_list.items[i] == text_block) { |
| 2051 | if (free_list.items[i] == atom_index) { |
| 2051 | 2052 | _ = free_list.swapRemove(i); |
| 2052 | 2053 | continue; |
| 2053 | 2054 | } |
| 2054 | | if (free_list.items[i] == text_block.prev) { |
| 2055 | if (free_list.items[i] == atom.prev_index) { |
| 2055 | 2056 | already_have_free_list_node = true; |
| 2056 | 2057 | } |
| 2057 | 2058 | i += 1; |
| 2058 | 2059 | } |
| 2059 | 2060 | } |
| 2060 | 2061 | |
| 2061 | | if (self.atoms.getPtr(phdr_index)) |last_block| { |
| 2062 | | if (last_block.* == text_block) { |
| 2063 | | if (text_block.prev) |prev| { |
| 2062 | const maybe_last_atom_index = &self.sections.items(.last_atom_index)[shndx]; |
| 2063 | if (maybe_last_atom_index.*) |last_atom_index| { |
| 2064 | if (last_atom_index == atom_index) { |
| 2065 | if (atom.prev_index) |prev_index| { |
| 2064 | 2066 | // TODO shrink the section size here |
| 2065 | | last_block.* = prev; |
| 2067 | maybe_last_atom_index.* = prev_index; |
| 2066 | 2068 | } else { |
| 2067 | | _ = self.atoms.fetchRemove(phdr_index); |
| 2069 | maybe_last_atom_index.* = null; |
| 2068 | 2070 | } |
| 2069 | 2071 | } |
| 2070 | 2072 | } |
| 2071 | 2073 | |
| 2072 | | if (text_block.prev) |prev| { |
| 2073 | | prev.next = text_block.next; |
| 2074 | if (atom.prev_index) |prev_index| { |
| 2075 | const prev = self.getAtomPtr(prev_index); |
| 2076 | prev.next_index = atom.next_index; |
| 2074 | 2077 | |
| 2075 | | if (!already_have_free_list_node and prev.freeListEligible(self)) { |
| 2078 | if (!already_have_free_list_node and prev.*.freeListEligible(self)) { |
| 2076 | 2079 | // The free list is heuristics, it doesn't have to be perfect, so we can |
| 2077 | 2080 | // ignore the OOM here. |
| 2078 | | free_list.append(self.base.allocator, prev) catch {}; |
| 2081 | free_list.append(gpa, prev_index) catch {}; |
| 2079 | 2082 | } |
| 2080 | 2083 | } else { |
| 2081 | | text_block.prev = null; |
| 2084 | self.getAtomPtr(atom_index).prev_index = null; |
| 2082 | 2085 | } |
| 2083 | 2086 | |
| 2084 | | if (text_block.next) |next| { |
| 2085 | | next.prev = text_block.prev; |
| 2087 | if (atom.next_index) |next_index| { |
| 2088 | self.getAtomPtr(next_index).prev_index = atom.prev_index; |
| 2086 | 2089 | } else { |
| 2087 | | text_block.next = null; |
| 2090 | self.getAtomPtr(atom_index).next_index = null; |
| 2088 | 2091 | } |
| 2089 | 2092 | |
| 2090 | 2093 | // Appending to free lists is allowed to fail because the free lists are heuristics based anyway. |
| 2091 | | const local_sym_index = text_block.getSymbolIndex().?; |
| 2092 | | self.local_symbol_free_list.append(self.base.allocator, local_sym_index) catch {}; |
| 2094 | const local_sym_index = atom.getSymbolIndex().?; |
| 2095 | |
| 2096 | self.local_symbol_free_list.append(gpa, local_sym_index) catch {}; |
| 2093 | 2097 | self.local_symbols.items[local_sym_index].st_info = 0; |
| 2098 | self.local_symbols.items[local_sym_index].st_shndx = 0; |
| 2094 | 2099 | _ = self.atom_by_index_table.remove(local_sym_index); |
| 2095 | | text_block.local_sym_index = 0; |
| 2100 | self.getAtomPtr(atom_index).local_sym_index = 0; |
| 2096 | 2101 | |
| 2097 | | self.offset_table_free_list.append(self.base.allocator, text_block.offset_table_index) catch {}; |
| 2102 | self.offset_table_free_list.append(self.base.allocator, atom.offset_table_index) catch {}; |
| 2098 | 2103 | |
| 2099 | | if (self.dwarf) |*dw| { |
| 2100 | | dw.freeAtom(&text_block.dbg_info_atom); |
| 2101 | | } |
| 2104 | // if (self.dwarf) |*dw| { |
| 2105 | // dw.freeAtom(&atom.dbg_info_atom); |
| 2106 | // } |
| 2102 | 2107 | } |
| 2103 | 2108 | |
| 2104 | | fn shrinkTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, phdr_index: u16) void { |
| 2109 | fn shrinkAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64) void { |
| 2105 | 2110 | _ = self; |
| 2106 | | _ = text_block; |
| 2111 | _ = atom_index; |
| 2107 | 2112 | _ = new_block_size; |
| 2108 | | _ = phdr_index; |
| 2109 | 2113 | } |
| 2110 | 2114 | |
| 2111 | | fn growTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, alignment: u64, phdr_index: u16) !u64 { |
| 2112 | | const sym = text_block.getSymbol(self); |
| 2115 | fn growAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 { |
| 2116 | const atom = self.getAtom(atom_index); |
| 2117 | const sym = atom.getSymbol(self); |
| 2113 | 2118 | const align_ok = mem.alignBackwardGeneric(u64, sym.st_value, alignment) == sym.st_value; |
| 2114 | | const need_realloc = !align_ok or new_block_size > text_block.capacity(self); |
| 2119 | const need_realloc = !align_ok or new_block_size > atom.capacity(self); |
| 2115 | 2120 | if (!need_realloc) return sym.st_value; |
| 2116 | | return self.allocateTextBlock(text_block, new_block_size, alignment, phdr_index); |
| 2121 | return self.allocateAtom(atom_index, new_block_size, alignment); |
| 2122 | } |
| 2123 | |
| 2124 | pub fn createAtom(self: *Elf) !Atom.Index { |
| 2125 | const gpa = self.base.allocator; |
| 2126 | const atom_index = @intCast(Atom.Index, self.atoms.items.len); |
| 2127 | const atom = try self.atoms.addOne(gpa); |
| 2128 | const local_sym_index = try self.allocateLocalSymbol(); |
| 2129 | const offset_table_index = try self.allocateGotOffset(); |
| 2130 | try self.atom_by_index_table.putNoClobber(gpa, local_sym_index, atom_index); |
| 2131 | atom.* = .{ |
| 2132 | .local_sym_index = local_sym_index, |
| 2133 | .offset_table_index = offset_table_index, |
| 2134 | .prev_index = null, |
| 2135 | .next_index = null, |
| 2136 | .dbg_info_atom = undefined, |
| 2137 | }; |
| 2138 | log.debug("creating ATOM(%{d}) at index {d}", .{ local_sym_index, atom_index }); |
| 2139 | return atom_index; |
| 2117 | 2140 | } |
| 2118 | 2141 | |
| 2119 | | fn allocateTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, alignment: u64, phdr_index: u16) !u64 { |
| 2120 | | const shdr_index = self.phdr_shdr_table.get(phdr_index).?; |
| 2142 | fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 { |
| 2143 | const atom = self.getAtom(atom_index); |
| 2144 | const sym = atom.getSymbol(self); |
| 2145 | const phdr_index = self.sections.items(.phdr_index)[sym.st_shndx]; |
| 2121 | 2146 | const phdr = &self.program_headers.items[phdr_index]; |
| 2122 | | const shdr = &self.sections.items[shdr_index]; |
| 2123 | | const new_block_ideal_capacity = padToIdeal(new_block_size); |
| 2147 | const shdr = &self.sections.items(.shdr)[sym.st_shndx]; |
| 2148 | const free_list = &self.sections.items(.free_list)[sym.st_shndx]; |
| 2149 | const maybe_last_atom_index = &self.sections.items(.last_atom_index)[sym.st_shndx]; |
| 2150 | const new_atom_ideal_capacity = padToIdeal(new_block_size); |
| 2124 | 2151 | |
| 2125 | | // We use these to indicate our intention to update metadata, placing the new block, |
| 2152 | // We use these to indicate our intention to update metadata, placing the new atom, |
| 2126 | 2153 | // and possibly removing a free list node. |
| 2127 | 2154 | // It would be simpler to do it inside the for loop below, but that would cause a |
| 2128 | 2155 | // problem if an error was returned later in the function. So this action |
| 2129 | 2156 | // is actually carried out at the end of the function, when errors are no longer possible. |
| 2130 | | var block_placement: ?*TextBlock = null; |
| 2157 | var atom_placement: ?Atom.Index = null; |
| 2131 | 2158 | var free_list_removal: ?usize = null; |
| 2132 | | var free_list = self.atom_free_lists.get(phdr_index).?; |
| 2133 | 2159 | |
| 2134 | 2160 | // First we look for an appropriately sized free list node. |
| 2135 | 2161 | // The list is unordered. We'll just take the first thing that works. |
| 2136 | 2162 | const vaddr = blk: { |
| 2137 | 2163 | var i: usize = 0; |
| 2138 | 2164 | while (i < free_list.items.len) { |
| 2139 | | const big_block = free_list.items[i]; |
| 2140 | | // We now have a pointer to a live text block that has too much capacity. |
| 2141 | | // Is it enough that we could fit this new text block? |
| 2142 | | const sym = big_block.getSymbol(self); |
| 2143 | | const capacity = big_block.capacity(self); |
| 2165 | const big_atom_index = free_list.items[i]; |
| 2166 | const big_atom = self.getAtom(big_atom_index); |
| 2167 | // We now have a pointer to a live atom that has too much capacity. |
| 2168 | // Is it enough that we could fit this new atom? |
| 2169 | const big_atom_sym = big_atom.getSymbol(self); |
| 2170 | const capacity = big_atom.capacity(self); |
| 2144 | 2171 | const ideal_capacity = padToIdeal(capacity); |
| 2145 | | const ideal_capacity_end_vaddr = std.math.add(u64, sym.st_value, ideal_capacity) catch ideal_capacity; |
| 2146 | | const capacity_end_vaddr = sym.st_value + capacity; |
| 2147 | | const new_start_vaddr_unaligned = capacity_end_vaddr - new_block_ideal_capacity; |
| 2172 | const ideal_capacity_end_vaddr = std.math.add(u64, big_atom_sym.st_value, ideal_capacity) catch ideal_capacity; |
| 2173 | const capacity_end_vaddr = big_atom_sym.st_value + capacity; |
| 2174 | const new_start_vaddr_unaligned = capacity_end_vaddr - new_atom_ideal_capacity; |
| 2148 | 2175 | const new_start_vaddr = mem.alignBackwardGeneric(u64, new_start_vaddr_unaligned, alignment); |
| 2149 | 2176 | if (new_start_vaddr < ideal_capacity_end_vaddr) { |
| 2150 | 2177 | // Additional bookkeeping here to notice if this free list node |
| 2151 | 2178 | // should be deleted because the block that it points to has grown to take up |
| 2152 | 2179 | // more of the extra capacity. |
| 2153 | | if (!big_block.freeListEligible(self)) { |
| 2180 | if (!big_atom.freeListEligible(self)) { |
| 2154 | 2181 | _ = free_list.swapRemove(i); |
| 2155 | 2182 | } else { |
| 2156 | 2183 | i += 1; |
| ... | ... | @@ -2164,60 +2191,69 @@ fn allocateTextBlock(self: *Elf, text_block: *TextBlock, new_block_size: u64, al |
| 2164 | 2191 | const keep_free_list_node = remaining_capacity >= min_text_capacity; |
| 2165 | 2192 | |
| 2166 | 2193 | // Set up the metadata to be updated, after errors are no longer possible. |
| 2167 | | block_placement = big_block; |
| 2194 | atom_placement = big_atom_index; |
| 2168 | 2195 | if (!keep_free_list_node) { |
| 2169 | 2196 | free_list_removal = i; |
| 2170 | 2197 | } |
| 2171 | 2198 | break :blk new_start_vaddr; |
| 2172 | | } else if (self.atoms.get(phdr_index)) |last| { |
| 2173 | | const sym = last.getSymbol(self); |
| 2174 | | const ideal_capacity = padToIdeal(sym.st_size); |
| 2175 | | const ideal_capacity_end_vaddr = sym.st_value + ideal_capacity; |
| 2199 | } else if (maybe_last_atom_index.*) |last_index| { |
| 2200 | const last = self.getAtom(last_index); |
| 2201 | const last_sym = last.getSymbol(self); |
| 2202 | const ideal_capacity = padToIdeal(last_sym.st_size); |
| 2203 | const ideal_capacity_end_vaddr = last_sym.st_value + ideal_capacity; |
| 2176 | 2204 | const new_start_vaddr = mem.alignForwardGeneric(u64, ideal_capacity_end_vaddr, alignment); |
| 2177 | 2205 | // Set up the metadata to be updated, after errors are no longer possible. |
| 2178 | | block_placement = last; |
| 2206 | atom_placement = last_index; |
| 2179 | 2207 | break :blk new_start_vaddr; |
| 2180 | 2208 | } else { |
| 2181 | 2209 | break :blk phdr.p_vaddr; |
| 2182 | 2210 | } |
| 2183 | 2211 | }; |
| 2184 | 2212 | |
| 2185 | | const expand_text_section = block_placement == null or block_placement.?.next == null; |
| 2186 | | if (expand_text_section) { |
| 2213 | const expand_section = if (atom_placement) |placement_index| |
| 2214 | self.getAtom(placement_index).next_index == null |
| 2215 | else |
| 2216 | true; |
| 2217 | if (expand_section) { |
| 2187 | 2218 | const needed_size = (vaddr + new_block_size) - phdr.p_vaddr; |
| 2188 | | try self.growAllocSection(shdr_index, phdr_index, needed_size); |
| 2189 | | _ = try self.atoms.put(self.base.allocator, phdr_index, text_block); |
| 2190 | | |
| 2191 | | if (self.dwarf) |_| { |
| 2192 | | // The .debug_info section has `low_pc` and `high_pc` values which is the virtual address |
| 2193 | | // range of the compilation unit. When we expand the text section, this range changes, |
| 2194 | | // so the DW_TAG.compile_unit tag of the .debug_info section becomes dirty. |
| 2195 | | self.debug_info_header_dirty = true; |
| 2196 | | // This becomes dirty for the same reason. We could potentially make this more |
| 2197 | | // fine-grained with the addition of support for more compilation units. It is planned to |
| 2198 | | // model each package as a different compilation unit. |
| 2199 | | self.debug_aranges_section_dirty = true; |
| 2200 | | } |
| 2219 | try self.growAllocSection(sym.st_shndx, needed_size); |
| 2220 | maybe_last_atom_index.* = atom_index; |
| 2221 | |
| 2222 | // if (self.dwarf) |_| { |
| 2223 | // // The .debug_info section has `low_pc` and `high_pc` values which is the virtual address |
| 2224 | // // range of the compilation unit. When we expand the text section, this range changes, |
| 2225 | // // so the DW_TAG.compile_unit tag of the .debug_info section becomes dirty. |
| 2226 | // self.debug_info_header_dirty = true; |
| 2227 | // // This becomes dirty for the same reason. We could potentially make this more |
| 2228 | // // fine-grained with the addition of support for more compilation units. It is planned to |
| 2229 | // // model each package as a different compilation unit. |
| 2230 | // self.debug_aranges_section_dirty = true; |
| 2231 | // } |
| 2201 | 2232 | } |
| 2202 | 2233 | shdr.sh_addralign = math.max(shdr.sh_addralign, alignment); |
| 2203 | 2234 | |
| 2204 | | // This function can also reallocate a text block. |
| 2235 | // This function can also reallocate an atom. |
| 2205 | 2236 | // In this case we need to "unplug" it from its previous location before |
| 2206 | 2237 | // plugging it in to its new location. |
| 2207 | | if (text_block.prev) |prev| { |
| 2208 | | prev.next = text_block.next; |
| 2238 | if (atom.prev_index) |prev_index| { |
| 2239 | const prev = self.getAtomPtr(prev_index); |
| 2240 | prev.next_index = atom.next_index; |
| 2209 | 2241 | } |
| 2210 | | if (text_block.next) |next| { |
| 2211 | | next.prev = text_block.prev; |
| 2242 | if (atom.next_index) |next_index| { |
| 2243 | const next = self.getAtomPtr(next_index); |
| 2244 | next.prev_index = atom.prev_index; |
| 2212 | 2245 | } |
| 2213 | 2246 | |
| 2214 | | if (block_placement) |big_block| { |
| 2215 | | text_block.prev = big_block; |
| 2216 | | text_block.next = big_block.next; |
| 2217 | | big_block.next = text_block; |
| 2247 | if (atom_placement) |big_atom_index| { |
| 2248 | const big_atom = self.getAtomPtr(big_atom_index); |
| 2249 | const atom_ptr = self.getAtomPtr(atom_index); |
| 2250 | atom_ptr.prev_index = big_atom_index; |
| 2251 | atom_ptr.next_index = big_atom.next_index; |
| 2252 | big_atom.next_index = atom_index; |
| 2218 | 2253 | } else { |
| 2219 | | text_block.prev = null; |
| 2220 | | text_block.next = null; |
| 2254 | const atom_ptr = self.getAtomPtr(atom_index); |
| 2255 | atom_ptr.prev_index = null; |
| 2256 | atom_ptr.next_index = null; |
| 2221 | 2257 | } |
| 2222 | 2258 | if (free_list_removal) |i| { |
| 2223 | 2259 | _ = free_list.swapRemove(i); |
| ... | ... | @@ -2272,15 +2308,10 @@ pub fn allocateGotOffset(self: *Elf) !u32 { |
| 2272 | 2308 | return index; |
| 2273 | 2309 | } |
| 2274 | 2310 | |
| 2275 | | fn freeRelocationsForTextBlock(self: *Elf, text_block: *TextBlock) void { |
| 2276 | | var removed_relocs = self.relocs.fetchRemove(text_block); |
| 2277 | | if (removed_relocs) |*relocs| relocs.value.deinit(self.base.allocator); |
| 2278 | | } |
| 2279 | | |
| 2280 | 2311 | fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void { |
| 2281 | 2312 | const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return; |
| 2282 | 2313 | for (unnamed_consts.items) |atom| { |
| 2283 | | self.freeTextBlock(atom, self.phdr_load_ro_index.?); |
| 2314 | self.freeAtom(atom); |
| 2284 | 2315 | } |
| 2285 | 2316 | unnamed_consts.clearAndFree(self.base.allocator); |
| 2286 | 2317 | } |
| ... | ... | @@ -2295,43 +2326,57 @@ pub fn freeDecl(self: *Elf, decl_index: Module.Decl.Index) void { |
| 2295 | 2326 | |
| 2296 | 2327 | log.debug("freeDecl {*}", .{decl}); |
| 2297 | 2328 | |
| 2298 | | if (self.decls.fetchRemove(decl_index)) |kv| { |
| 2299 | | if (kv.value) |index| { |
| 2300 | | self.freeTextBlock(&decl.link.elf, index); |
| 2301 | | self.freeUnnamedConsts(decl_index); |
| 2302 | | } |
| 2329 | if (self.decls.fetchRemove(decl_index)) |const_kv| { |
| 2330 | var kv = const_kv; |
| 2331 | self.freeAtom(kv.value.atom); |
| 2332 | self.freeUnnamedConsts(decl_index); |
| 2333 | kv.value.exports.deinit(self.base.allocator); |
| 2303 | 2334 | } |
| 2304 | 2335 | |
| 2305 | | if (self.dwarf) |*dw| { |
| 2306 | | dw.freeDecl(decl); |
| 2336 | // if (self.dwarf) |*dw| { |
| 2337 | // dw.freeDecl(decl); |
| 2338 | // } |
| 2339 | } |
| 2340 | |
| 2341 | pub fn getOrCreateAtomForDecl(self: *Elf, decl_index: Module.Decl.Index) !Atom.Index { |
| 2342 | const gop = try self.decls.getOrPut(self.base.allocator, decl_index); |
| 2343 | if (!gop.found_existing) { |
| 2344 | gop.value_ptr.* = .{ |
| 2345 | .atom = try self.createAtom(), |
| 2346 | .shdr = self.getDeclShdrIndex(decl_index), |
| 2347 | .exports = .{}, |
| 2348 | }; |
| 2307 | 2349 | } |
| 2350 | return gop.value_ptr.atom; |
| 2308 | 2351 | } |
| 2309 | 2352 | |
| 2310 | | fn getDeclPhdrIndex(self: *Elf, decl: *Module.Decl) !u16 { |
| 2353 | fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 { |
| 2354 | const decl = self.base.options.module.?.declPtr(decl_index); |
| 2311 | 2355 | const ty = decl.ty; |
| 2312 | 2356 | const zig_ty = ty.zigTypeTag(); |
| 2313 | 2357 | const val = decl.val; |
| 2314 | | const phdr_index: u16 = blk: { |
| 2358 | const shdr_index: u16 = blk: { |
| 2315 | 2359 | if (val.isUndefDeep()) { |
| 2316 | 2360 | // TODO in release-fast and release-small, we should put undef in .bss |
| 2317 | | break :blk self.phdr_load_rw_index.?; |
| 2361 | break :blk self.data_section_index.?; |
| 2318 | 2362 | } |
| 2319 | 2363 | |
| 2320 | 2364 | switch (zig_ty) { |
| 2321 | 2365 | // TODO: what if this is a function pointer? |
| 2322 | | .Fn => break :blk self.phdr_load_re_index.?, |
| 2366 | .Fn => break :blk self.text_section_index.?, |
| 2323 | 2367 | else => { |
| 2324 | 2368 | if (val.castTag(.variable)) |_| { |
| 2325 | | break :blk self.phdr_load_rw_index.?; |
| 2369 | break :blk self.data_section_index.?; |
| 2326 | 2370 | } |
| 2327 | | break :blk self.phdr_load_ro_index.?; |
| 2371 | break :blk self.rodata_section_index.?; |
| 2328 | 2372 | }, |
| 2329 | 2373 | } |
| 2330 | 2374 | }; |
| 2331 | | return phdr_index; |
| 2375 | return shdr_index; |
| 2332 | 2376 | } |
| 2333 | 2377 | |
| 2334 | 2378 | fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, stt_bits: u8) !*elf.Elf64_Sym { |
| 2379 | const gpa = self.base.allocator; |
| 2335 | 2380 | const mod = self.base.options.module.?; |
| 2336 | 2381 | const decl = mod.declPtr(decl_index); |
| 2337 | 2382 | |
| ... | ... | @@ -2341,60 +2386,65 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s |
| 2341 | 2386 | log.debug("updateDeclCode {s}{*}", .{ decl_name, decl }); |
| 2342 | 2387 | const required_alignment = decl.getAlignment(self.base.options.target); |
| 2343 | 2388 | |
| 2344 | | const decl_ptr = self.decls.getPtr(decl_index).?; |
| 2345 | | if (decl_ptr.* == null) { |
| 2346 | | decl_ptr.* = try self.getDeclPhdrIndex(decl); |
| 2347 | | } |
| 2348 | | const phdr_index = decl_ptr.*.?; |
| 2349 | | const shdr_index = self.phdr_shdr_table.get(phdr_index).?; |
| 2389 | const decl_metadata = self.decls.get(decl_index).?; |
| 2390 | const atom_index = decl_metadata.atom; |
| 2391 | const atom = self.getAtom(atom_index); |
| 2392 | |
| 2393 | const shdr_index = decl_metadata.shdr; |
| 2394 | if (atom.getSymbol(self).st_size != 0) { |
| 2395 | const local_sym = atom.getSymbolPtr(self); |
| 2396 | local_sym.st_name = try self.shstrtab.insert(gpa, decl_name); |
| 2397 | local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits; |
| 2398 | local_sym.st_other = 0; |
| 2399 | local_sym.st_shndx = shdr_index; |
| 2350 | 2400 | |
| 2351 | | const local_sym = decl.link.elf.getSymbolPtr(self); |
| 2352 | | if (local_sym.st_size != 0) { |
| 2353 | | const capacity = decl.link.elf.capacity(self); |
| 2401 | const capacity = atom.capacity(self); |
| 2354 | 2402 | const need_realloc = code.len > capacity or |
| 2355 | 2403 | !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment); |
| 2404 | |
| 2356 | 2405 | if (need_realloc) { |
| 2357 | | const vaddr = try self.growTextBlock(&decl.link.elf, code.len, required_alignment, phdr_index); |
| 2406 | const vaddr = try self.growAtom(atom_index, code.len, required_alignment); |
| 2358 | 2407 | log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl_name, local_sym.st_value, vaddr }); |
| 2359 | 2408 | if (vaddr != local_sym.st_value) { |
| 2360 | 2409 | local_sym.st_value = vaddr; |
| 2361 | 2410 | |
| 2362 | 2411 | log.debug(" (writing new offset table entry)", .{}); |
| 2363 | | self.offset_table.items[decl.link.elf.offset_table_index] = vaddr; |
| 2364 | | try self.writeOffsetTableEntry(decl.link.elf.offset_table_index); |
| 2412 | self.offset_table.items[atom.offset_table_index] = vaddr; |
| 2413 | try self.writeOffsetTableEntry(atom.offset_table_index); |
| 2365 | 2414 | } |
| 2366 | 2415 | } else if (code.len < local_sym.st_size) { |
| 2367 | | self.shrinkTextBlock(&decl.link.elf, code.len, phdr_index); |
| 2416 | self.shrinkAtom(atom_index, code.len); |
| 2368 | 2417 | } |
| 2369 | 2418 | local_sym.st_size = code.len; |
| 2370 | | local_sym.st_name = try self.updateString(local_sym.st_name, decl_name); |
| 2371 | | local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits; |
| 2372 | | local_sym.st_other = 0; |
| 2373 | | local_sym.st_shndx = shdr_index; |
| 2419 | |
| 2374 | 2420 | // TODO this write could be avoided if no fields of the symbol were changed. |
| 2375 | | try self.writeSymbol(decl.link.elf.getSymbolIndex().?); |
| 2421 | try self.writeSymbol(atom.getSymbolIndex().?); |
| 2376 | 2422 | } else { |
| 2377 | | const name_str_index = try self.makeString(decl_name); |
| 2378 | | const vaddr = try self.allocateTextBlock(&decl.link.elf, code.len, required_alignment, phdr_index); |
| 2379 | | errdefer self.freeTextBlock(&decl.link.elf, phdr_index); |
| 2380 | | log.debug("allocated text block for {s} at 0x{x}", .{ decl_name, vaddr }); |
| 2381 | | |
| 2423 | const local_sym = atom.getSymbolPtr(self); |
| 2382 | 2424 | local_sym.* = .{ |
| 2383 | | .st_name = name_str_index, |
| 2425 | .st_name = try self.shstrtab.insert(gpa, decl_name), |
| 2384 | 2426 | .st_info = (elf.STB_LOCAL << 4) | stt_bits, |
| 2385 | 2427 | .st_other = 0, |
| 2386 | 2428 | .st_shndx = shdr_index, |
| 2387 | | .st_value = vaddr, |
| 2388 | | .st_size = code.len, |
| 2429 | .st_value = 0, |
| 2430 | .st_size = 0, |
| 2389 | 2431 | }; |
| 2390 | | self.offset_table.items[decl.link.elf.offset_table_index] = vaddr; |
| 2432 | const vaddr = try self.allocateAtom(atom_index, code.len, required_alignment); |
| 2433 | errdefer self.freeAtom(atom_index); |
| 2434 | log.debug("allocated text block for {s} at 0x{x}", .{ decl_name, vaddr }); |
| 2391 | 2435 | |
| 2392 | | try self.writeSymbol(decl.link.elf.getSymbolIndex().?); |
| 2393 | | try self.writeOffsetTableEntry(decl.link.elf.offset_table_index); |
| 2436 | self.offset_table.items[atom.offset_table_index] = vaddr; |
| 2437 | local_sym.st_value = vaddr; |
| 2438 | local_sym.st_size = code.len; |
| 2439 | |
| 2440 | try self.writeSymbol(atom.getSymbolIndex().?); |
| 2441 | try self.writeOffsetTableEntry(atom.offset_table_index); |
| 2394 | 2442 | } |
| 2395 | 2443 | |
| 2444 | const local_sym = atom.getSymbolPtr(self); |
| 2445 | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 2396 | 2446 | const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr; |
| 2397 | | const file_offset = self.sections.items[shdr_index].sh_offset + section_offset; |
| 2447 | const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset; |
| 2398 | 2448 | try self.base.file.?.pwriteAll(code, file_offset); |
| 2399 | 2449 | |
| 2400 | 2450 | return local_sym; |
| ... | ... | @@ -2413,28 +2463,23 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven |
| 2413 | 2463 | |
| 2414 | 2464 | const decl_index = func.owner_decl; |
| 2415 | 2465 | const decl = module.declPtr(decl_index); |
| 2416 | | const atom = &decl.link.elf; |
| 2417 | | try atom.ensureInitialized(self); |
| 2418 | | const gop = try self.decls.getOrPut(self.base.allocator, decl_index); |
| 2419 | | if (gop.found_existing) { |
| 2420 | | self.freeUnnamedConsts(decl_index); |
| 2421 | | self.freeRelocationsForTextBlock(atom); |
| 2422 | | } else { |
| 2423 | | gop.value_ptr.* = null; |
| 2424 | | } |
| 2466 | |
| 2467 | const atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 2468 | self.freeUnnamedConsts(decl_index); |
| 2469 | Atom.freeRelocations(self, atom_index); |
| 2425 | 2470 | |
| 2426 | 2471 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2427 | 2472 | defer code_buffer.deinit(); |
| 2428 | 2473 | |
| 2429 | | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(module, decl_index) else null; |
| 2430 | | defer if (decl_state) |*ds| ds.deinit(); |
| 2474 | // var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(module, decl_index) else null; |
| 2475 | // defer if (decl_state) |*ds| ds.deinit(); |
| 2431 | 2476 | |
| 2432 | | const res = if (decl_state) |*ds| |
| 2433 | | try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .{ |
| 2434 | | .dwarf = ds, |
| 2435 | | }) |
| 2436 | | else |
| 2437 | | try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .none); |
| 2477 | // const res = if (decl_state) |*ds| |
| 2478 | // try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .{ |
| 2479 | // .dwarf = ds, |
| 2480 | // }) |
| 2481 | // else |
| 2482 | const res = try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .none); |
| 2438 | 2483 | |
| 2439 | 2484 | const code = switch (res) { |
| 2440 | 2485 | .ok => code_buffer.items, |
| ... | ... | @@ -2445,15 +2490,16 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven |
| 2445 | 2490 | }, |
| 2446 | 2491 | }; |
| 2447 | 2492 | const local_sym = try self.updateDeclCode(decl_index, code, elf.STT_FUNC); |
| 2448 | | if (decl_state) |*ds| { |
| 2449 | | try self.dwarf.?.commitDeclState( |
| 2450 | | module, |
| 2451 | | decl_index, |
| 2452 | | local_sym.st_value, |
| 2453 | | local_sym.st_size, |
| 2454 | | ds, |
| 2455 | | ); |
| 2456 | | } |
| 2493 | _ = local_sym; |
| 2494 | // if (decl_state) |*ds| { |
| 2495 | // try self.dwarf.?.commitDeclState( |
| 2496 | // module, |
| 2497 | // decl_index, |
| 2498 | // local_sym.st_value, |
| 2499 | // local_sym.st_size, |
| 2500 | // ds, |
| 2501 | // ); |
| 2502 | // } |
| 2457 | 2503 | |
| 2458 | 2504 | // Since we updated the vaddr and the size, each corresponding export |
| 2459 | 2505 | // symbol also needs to be updated. |
| ... | ... | @@ -2483,41 +2529,34 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v |
| 2483 | 2529 | } |
| 2484 | 2530 | } |
| 2485 | 2531 | |
| 2486 | | assert(!self.unnamed_const_atoms.contains(decl_index)); |
| 2487 | | |
| 2488 | | const atom = &decl.link.elf; |
| 2489 | | try atom.ensureInitialized(self); |
| 2490 | | const gop = try self.decls.getOrPut(self.base.allocator, decl_index); |
| 2491 | | if (gop.found_existing) { |
| 2492 | | self.freeRelocationsForTextBlock(atom); |
| 2493 | | } else { |
| 2494 | | gop.value_ptr.* = null; |
| 2495 | | } |
| 2532 | const atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 2533 | Atom.freeRelocations(self, atom_index); |
| 2534 | const atom = self.getAtom(atom_index); |
| 2496 | 2535 | |
| 2497 | 2536 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2498 | 2537 | defer code_buffer.deinit(); |
| 2499 | 2538 | |
| 2500 | | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(module, decl_index) else null; |
| 2501 | | defer if (decl_state) |*ds| ds.deinit(); |
| 2539 | // var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(module, decl_index) else null; |
| 2540 | // defer if (decl_state) |*ds| ds.deinit(); |
| 2502 | 2541 | |
| 2503 | 2542 | // TODO implement .debug_info for global variables |
| 2504 | 2543 | const decl_val = if (decl.val.castTag(.variable)) |payload| payload.data.init else decl.val; |
| 2505 | | const res = if (decl_state) |*ds| |
| 2506 | | try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ |
| 2507 | | .ty = decl.ty, |
| 2508 | | .val = decl_val, |
| 2509 | | }, &code_buffer, .{ |
| 2510 | | .dwarf = ds, |
| 2511 | | }, .{ |
| 2512 | | .parent_atom_index = decl.link.elf.getSymbolIndex().?, |
| 2513 | | }) |
| 2514 | | else |
| 2515 | | try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ |
| 2516 | | .ty = decl.ty, |
| 2517 | | .val = decl_val, |
| 2518 | | }, &code_buffer, .none, .{ |
| 2519 | | .parent_atom_index = decl.link.elf.getSymbolIndex().?, |
| 2520 | | }); |
| 2544 | // const res = if (decl_state) |*ds| |
| 2545 | // try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ |
| 2546 | // .ty = decl.ty, |
| 2547 | // .val = decl_val, |
| 2548 | // }, &code_buffer, .{ |
| 2549 | // .dwarf = ds, |
| 2550 | // }, .{ |
| 2551 | // .parent_atom_index = atom.getSymbolIndex().?, |
| 2552 | // }) |
| 2553 | // else |
| 2554 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), .{ |
| 2555 | .ty = decl.ty, |
| 2556 | .val = decl_val, |
| 2557 | }, &code_buffer, .none, .{ |
| 2558 | .parent_atom_index = atom.getSymbolIndex().?, |
| 2559 | }); |
| 2521 | 2560 | |
| 2522 | 2561 | const code = switch (res) { |
| 2523 | 2562 | .ok => code_buffer.items, |
| ... | ... | @@ -2529,15 +2568,16 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v |
| 2529 | 2568 | }; |
| 2530 | 2569 | |
| 2531 | 2570 | const local_sym = try self.updateDeclCode(decl_index, code, elf.STT_OBJECT); |
| 2532 | | if (decl_state) |*ds| { |
| 2533 | | try self.dwarf.?.commitDeclState( |
| 2534 | | module, |
| 2535 | | decl_index, |
| 2536 | | local_sym.st_value, |
| 2537 | | local_sym.st_size, |
| 2538 | | ds, |
| 2539 | | ); |
| 2540 | | } |
| 2571 | _ = local_sym; |
| 2572 | // if (decl_state) |*ds| { |
| 2573 | // try self.dwarf.?.commitDeclState( |
| 2574 | // module, |
| 2575 | // decl_index, |
| 2576 | // local_sym.st_value, |
| 2577 | // local_sym.st_size, |
| 2578 | // ds, |
| 2579 | // ); |
| 2580 | // } |
| 2541 | 2581 | |
| 2542 | 2582 | // Since we updated the vaddr and the size, each corresponding export |
| 2543 | 2583 | // symbol also needs to be updated. |
| ... | ... | @@ -2545,36 +2585,31 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v |
| 2545 | 2585 | } |
| 2546 | 2586 | |
| 2547 | 2587 | pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module.Decl.Index) !u32 { |
| 2548 | | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2588 | const gpa = self.base.allocator; |
| 2589 | |
| 2590 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 2549 | 2591 | defer code_buffer.deinit(); |
| 2550 | 2592 | |
| 2551 | 2593 | const mod = self.base.options.module.?; |
| 2552 | | const decl = mod.declPtr(decl_index); |
| 2553 | | |
| 2554 | | const gop = try self.unnamed_const_atoms.getOrPut(self.base.allocator, decl_index); |
| 2594 | const gop = try self.unnamed_const_atoms.getOrPut(gpa, decl_index); |
| 2555 | 2595 | if (!gop.found_existing) { |
| 2556 | 2596 | gop.value_ptr.* = .{}; |
| 2557 | 2597 | } |
| 2558 | 2598 | const unnamed_consts = gop.value_ptr; |
| 2559 | 2599 | |
| 2560 | | const atom = try self.base.allocator.create(TextBlock); |
| 2561 | | errdefer self.base.allocator.destroy(atom); |
| 2562 | | atom.* = TextBlock.empty; |
| 2563 | | // TODO for unnamed consts we don't need GOT offset/entry allocated |
| 2564 | | try atom.ensureInitialized(self); |
| 2565 | | try self.managed_atoms.append(self.base.allocator, atom); |
| 2566 | | |
| 2600 | const decl = mod.declPtr(decl_index); |
| 2567 | 2601 | const name_str_index = blk: { |
| 2568 | 2602 | const decl_name = try decl.getFullyQualifiedName(mod); |
| 2569 | | defer self.base.allocator.free(decl_name); |
| 2570 | | |
| 2603 | defer gpa.free(decl_name); |
| 2571 | 2604 | const index = unnamed_consts.items.len; |
| 2572 | | const name = try std.fmt.allocPrint(self.base.allocator, "__unnamed_{s}_{d}", .{ decl_name, index }); |
| 2573 | | defer self.base.allocator.free(name); |
| 2574 | | |
| 2575 | | break :blk try self.makeString(name); |
| 2605 | const name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index }); |
| 2606 | defer gpa.free(name); |
| 2607 | break :blk try self.shstrtab.insert(gpa, name); |
| 2576 | 2608 | }; |
| 2577 | | const name = self.getString(name_str_index); |
| 2609 | const name = self.shstrtab.get(name_str_index).?; |
| 2610 | |
| 2611 | const atom_index = try self.createAtom(); |
| 2612 | const atom = self.getAtomPtr(atom_index); |
| 2578 | 2613 | |
| 2579 | 2614 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .{ |
| 2580 | 2615 | .none = {}, |
| ... | ... | @@ -2592,28 +2627,24 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2592 | 2627 | }; |
| 2593 | 2628 | |
| 2594 | 2629 | const required_alignment = typed_value.ty.abiAlignment(self.base.options.target); |
| 2595 | | const phdr_index = self.phdr_load_ro_index.?; |
| 2596 | | const shdr_index = self.phdr_shdr_table.get(phdr_index).?; |
| 2597 | | const vaddr = try self.allocateTextBlock(atom, code.len, required_alignment, phdr_index); |
| 2598 | | errdefer self.freeTextBlock(atom, phdr_index); |
| 2599 | | |
| 2600 | | log.debug("allocated text block for {s} at 0x{x}", .{ name, vaddr }); |
| 2601 | | |
| 2630 | const shdr_index = self.rodata_section_index.?; |
| 2631 | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 2602 | 2632 | const local_sym = atom.getSymbolPtr(self); |
| 2603 | | local_sym.* = .{ |
| 2604 | | .st_name = name_str_index, |
| 2605 | | .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT, |
| 2606 | | .st_other = 0, |
| 2607 | | .st_shndx = shdr_index, |
| 2608 | | .st_value = vaddr, |
| 2609 | | .st_size = code.len, |
| 2610 | | }; |
| 2633 | local_sym.st_name = name_str_index; |
| 2634 | local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT; |
| 2635 | local_sym.st_other = 0; |
| 2636 | local_sym.st_shndx = shdr_index; |
| 2637 | local_sym.st_size = code.len; |
| 2638 | local_sym.st_value = try self.allocateAtom(atom_index, code.len, required_alignment); |
| 2639 | errdefer self.freeAtom(atom_index); |
| 2640 | |
| 2641 | log.debug("allocated text block for {s} at 0x{x}", .{ name, local_sym.st_value }); |
| 2611 | 2642 | |
| 2612 | 2643 | try self.writeSymbol(atom.getSymbolIndex().?); |
| 2613 | | try unnamed_consts.append(self.base.allocator, atom); |
| 2644 | try unnamed_consts.append(gpa, atom_index); |
| 2614 | 2645 | |
| 2615 | 2646 | const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr; |
| 2616 | | const file_offset = self.sections.items[shdr_index].sh_offset + section_offset; |
| 2647 | const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset; |
| 2617 | 2648 | try self.base.file.?.pwriteAll(code, file_offset); |
| 2618 | 2649 | |
| 2619 | 2650 | return atom.getSymbolIndex().?; |
| ... | ... | @@ -2635,20 +2666,16 @@ pub fn updateDeclExports( |
| 2635 | 2666 | const tracy = trace(@src()); |
| 2636 | 2667 | defer tracy.end(); |
| 2637 | 2668 | |
| 2638 | | const decl = module.declPtr(decl_index); |
| 2639 | | const atom = &decl.link.elf; |
| 2640 | | |
| 2641 | | if (atom.getSymbolIndex() == null) return; |
| 2669 | const gpa = self.base.allocator; |
| 2642 | 2670 | |
| 2671 | const decl = module.declPtr(decl_index); |
| 2672 | const atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 2673 | const atom = self.getAtom(atom_index); |
| 2643 | 2674 | const decl_sym = atom.getSymbol(self); |
| 2644 | | try self.global_symbols.ensureUnusedCapacity(self.base.allocator, exports.len); |
| 2675 | const decl_metadata = self.decls.getPtr(decl_index).?; |
| 2676 | const shdr_index = decl_metadata.shdr; |
| 2645 | 2677 | |
| 2646 | | const gop = try self.decls.getOrPut(self.base.allocator, decl_index); |
| 2647 | | if (!gop.found_existing) { |
| 2648 | | gop.value_ptr.* = try self.getDeclPhdrIndex(decl); |
| 2649 | | } |
| 2650 | | const phdr_index = gop.value_ptr.*.?; |
| 2651 | | const shdr_index = self.phdr_shdr_table.get(phdr_index).?; |
| 2678 | try self.global_symbols.ensureUnusedCapacity(gpa, exports.len); |
| 2652 | 2679 | |
| 2653 | 2680 | for (exports) |exp| { |
| 2654 | 2681 | if (exp.options.section) |section_name| { |
| ... | ... | @@ -2681,10 +2708,10 @@ pub fn updateDeclExports( |
| 2681 | 2708 | }, |
| 2682 | 2709 | }; |
| 2683 | 2710 | const stt_bits: u8 = @truncate(u4, decl_sym.st_info); |
| 2684 | | if (exp.link.elf.sym_index) |i| { |
| 2711 | if (decl_metadata.getExport(self, exp.options.name)) |i| { |
| 2685 | 2712 | const sym = &self.global_symbols.items[i]; |
| 2686 | 2713 | sym.* = .{ |
| 2687 | | .st_name = try self.updateString(sym.st_name, exp.options.name), |
| 2714 | .st_name = try self.shstrtab.insert(gpa, exp.options.name), |
| 2688 | 2715 | .st_info = (stb_bits << 4) | stt_bits, |
| 2689 | 2716 | .st_other = 0, |
| 2690 | 2717 | .st_shndx = shdr_index, |
| ... | ... | @@ -2692,21 +2719,19 @@ pub fn updateDeclExports( |
| 2692 | 2719 | .st_size = decl_sym.st_size, |
| 2693 | 2720 | }; |
| 2694 | 2721 | } else { |
| 2695 | | const name = try self.makeString(exp.options.name); |
| 2696 | 2722 | const i = if (self.global_symbol_free_list.popOrNull()) |i| i else blk: { |
| 2697 | 2723 | _ = self.global_symbols.addOneAssumeCapacity(); |
| 2698 | 2724 | break :blk self.global_symbols.items.len - 1; |
| 2699 | 2725 | }; |
| 2726 | try decl_metadata.exports.append(gpa, @intCast(u32, i)); |
| 2700 | 2727 | self.global_symbols.items[i] = .{ |
| 2701 | | .st_name = name, |
| 2728 | .st_name = try self.shstrtab.insert(gpa, exp.options.name), |
| 2702 | 2729 | .st_info = (stb_bits << 4) | stt_bits, |
| 2703 | 2730 | .st_other = 0, |
| 2704 | 2731 | .st_shndx = shdr_index, |
| 2705 | 2732 | .st_value = decl_sym.st_value, |
| 2706 | 2733 | .st_size = decl_sym.st_size, |
| 2707 | 2734 | }; |
| 2708 | | |
| 2709 | | exp.link.elf.sym_index = @intCast(u32, i); |
| 2710 | 2735 | } |
| 2711 | 2736 | } |
| 2712 | 2737 | } |
| ... | ... | @@ -2722,17 +2747,19 @@ pub fn updateDeclLineNumber(self: *Elf, mod: *Module, decl: *const Module.Decl) |
| 2722 | 2747 | log.debug("updateDeclLineNumber {s}{*}", .{ decl_name, decl }); |
| 2723 | 2748 | |
| 2724 | 2749 | if (self.llvm_object) |_| return; |
| 2725 | | if (self.dwarf) |*dw| { |
| 2726 | | try dw.updateDeclLineNumber(decl); |
| 2727 | | } |
| 2750 | // if (self.dwarf) |*dw| { |
| 2751 | // try dw.updateDeclLineNumber(decl); |
| 2752 | // } |
| 2728 | 2753 | } |
| 2729 | 2754 | |
| 2730 | | pub fn deleteExport(self: *Elf, exp: Export) void { |
| 2755 | pub fn deleteDeclExport(self: *Elf, decl_index: Module.Decl.Index, name: []const u8) void { |
| 2731 | 2756 | if (self.llvm_object) |_| return; |
| 2732 | | |
| 2733 | | const sym_index = exp.sym_index orelse return; |
| 2734 | | self.global_symbol_free_list.append(self.base.allocator, sym_index) catch {}; |
| 2735 | | self.global_symbols.items[sym_index].st_info = 0; |
| 2757 | const metadata = self.decls.getPtr(decl_index) orelse return; |
| 2758 | const sym_index = metadata.getExportPtr(self, name) orelse return; |
| 2759 | log.debug("deleting export '{s}'", .{name}); |
| 2760 | self.global_symbol_free_list.append(self.base.allocator, sym_index.*) catch {}; |
| 2761 | self.global_symbols.items[sym_index.*].st_info = 0; |
| 2762 | sym_index.* = 0; |
| 2736 | 2763 | } |
| 2737 | 2764 | |
| 2738 | 2765 | fn writeProgHeader(self: *Elf, index: usize) !void { |
| ... | ... | @@ -2761,7 +2788,7 @@ fn writeSectHeader(self: *Elf, index: usize) !void { |
| 2761 | 2788 | switch (self.ptr_width) { |
| 2762 | 2789 | .p32 => { |
| 2763 | 2790 | var shdr: [1]elf.Elf32_Shdr = undefined; |
| 2764 | | shdr[0] = sectHeaderTo32(self.sections.items[index]); |
| 2791 | shdr[0] = sectHeaderTo32(self.sections.items(.shdr)[index]); |
| 2765 | 2792 | if (foreign_endian) { |
| 2766 | 2793 | mem.byteSwapAllFields(elf.Elf32_Shdr, &shdr[0]); |
| 2767 | 2794 | } |
| ... | ... | @@ -2769,7 +2796,7 @@ fn writeSectHeader(self: *Elf, index: usize) !void { |
| 2769 | 2796 | return self.base.file.?.pwriteAll(mem.sliceAsBytes(&shdr), offset); |
| 2770 | 2797 | }, |
| 2771 | 2798 | .p64 => { |
| 2772 | | var shdr = [1]elf.Elf64_Shdr{self.sections.items[index]}; |
| 2799 | var shdr = [1]elf.Elf64_Shdr{self.sections.items(.shdr)[index]}; |
| 2773 | 2800 | if (foreign_endian) { |
| 2774 | 2801 | mem.byteSwapAllFields(elf.Elf64_Shdr, &shdr[0]); |
| 2775 | 2802 | } |
| ... | ... | @@ -2783,11 +2810,11 @@ fn writeOffsetTableEntry(self: *Elf, index: usize) !void { |
| 2783 | 2810 | const entry_size: u16 = self.archPtrWidthBytes(); |
| 2784 | 2811 | if (self.offset_table_count_dirty) { |
| 2785 | 2812 | const needed_size = self.offset_table.items.len * entry_size; |
| 2786 | | try self.growAllocSection(self.got_section_index.?, self.phdr_got_index.?, needed_size); |
| 2813 | try self.growAllocSection(self.got_section_index.?, needed_size); |
| 2787 | 2814 | self.offset_table_count_dirty = false; |
| 2788 | 2815 | } |
| 2789 | 2816 | const endian = self.base.options.target.cpu.arch.endian(); |
| 2790 | | const shdr = &self.sections.items[self.got_section_index.?]; |
| 2817 | const shdr = &self.sections.items(.shdr)[self.got_section_index.?]; |
| 2791 | 2818 | const off = shdr.sh_offset + @as(u64, entry_size) * index; |
| 2792 | 2819 | switch (entry_size) { |
| 2793 | 2820 | 2 => { |
| ... | ... | @@ -2813,7 +2840,7 @@ fn writeSymbol(self: *Elf, index: usize) !void { |
| 2813 | 2840 | const tracy = trace(@src()); |
| 2814 | 2841 | defer tracy.end(); |
| 2815 | 2842 | |
| 2816 | | const syms_sect = &self.sections.items[self.symtab_section_index.?]; |
| 2843 | const syms_sect = &self.sections.items(.shdr)[self.symtab_section_index.?]; |
| 2817 | 2844 | // Make sure we are not pointlessly writing symbol data that will have to get relocated |
| 2818 | 2845 | // due to running out of space. |
| 2819 | 2846 | if (self.local_symbols.items.len != syms_sect.sh_info) { |
| ... | ... | @@ -2835,7 +2862,7 @@ fn writeSymbol(self: *Elf, index: usize) !void { |
| 2835 | 2862 | .p64 => syms_sect.sh_offset + @sizeOf(elf.Elf64_Sym) * index, |
| 2836 | 2863 | }; |
| 2837 | 2864 | const local = self.local_symbols.items[index]; |
| 2838 | | log.debug("writing symbol {d}, '{s}' at 0x{x}", .{ index, self.getString(local.st_name), off }); |
| 2865 | log.debug("writing symbol {d}, '{?s}' at 0x{x}", .{ index, self.shstrtab.get(local.st_name), off }); |
| 2839 | 2866 | log.debug(" ({})", .{local}); |
| 2840 | 2867 | switch (self.ptr_width) { |
| 2841 | 2868 | .p32 => { |
| ... | ... | @@ -2865,7 +2892,7 @@ fn writeSymbol(self: *Elf, index: usize) !void { |
| 2865 | 2892 | } |
| 2866 | 2893 | |
| 2867 | 2894 | fn writeAllGlobalSymbols(self: *Elf) !void { |
| 2868 | | const syms_sect = &self.sections.items[self.symtab_section_index.?]; |
| 2895 | const syms_sect = &self.sections.items(.shdr)[self.symtab_section_index.?]; |
| 2869 | 2896 | const sym_size: u64 = switch (self.ptr_width) { |
| 2870 | 2897 | .p32 => @sizeOf(elf.Elf32_Sym), |
| 2871 | 2898 | .p64 => @sizeOf(elf.Elf64_Sym), |
| ... | ... | @@ -3215,10 +3242,52 @@ const CsuObjects = struct { |
| 3215 | 3242 | fn logSymtab(self: Elf) void { |
| 3216 | 3243 | log.debug("locals:", .{}); |
| 3217 | 3244 | for (self.local_symbols.items) |sym, id| { |
| 3218 | | log.debug(" {d}: {s}: @{x} in {d}", .{ id, self.getString(sym.st_name), sym.st_value, sym.st_shndx }); |
| 3245 | log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.shstrtab.get(sym.st_name), sym.st_value, sym.st_shndx }); |
| 3219 | 3246 | } |
| 3220 | 3247 | log.debug("globals:", .{}); |
| 3221 | 3248 | for (self.global_symbols.items) |sym, id| { |
| 3222 | | log.debug(" {d}: {s}: @{x} in {d}", .{ id, self.getString(sym.st_name), sym.st_value, sym.st_shndx }); |
| 3249 | log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.shstrtab.get(sym.st_name), sym.st_value, sym.st_shndx }); |
| 3223 | 3250 | } |
| 3224 | 3251 | } |
| 3252 | |
| 3253 | pub fn getProgramHeader(self: *const Elf, shdr_index: u16) elf.Elf64_Phdr { |
| 3254 | const index = self.sections.items(.phdr_index)[shdr_index]; |
| 3255 | return self.program_headers.items[index]; |
| 3256 | } |
| 3257 | |
| 3258 | pub fn getProgramHeaderPtr(self: *Elf, shdr_index: u16) *elf.Elf64_Phdr { |
| 3259 | const index = self.sections.items(.phdr_index)[shdr_index]; |
| 3260 | return &self.program_headers.items[index]; |
| 3261 | } |
| 3262 | |
| 3263 | /// Returns pointer-to-symbol described at sym_index. |
| 3264 | pub fn getSymbolPtr(self: *Elf, sym_index: u32) *elf.Elf64_Sym { |
| 3265 | return &self.local_symbols.items[sym_index]; |
| 3266 | } |
| 3267 | |
| 3268 | /// Returns symbol at sym_index. |
| 3269 | pub fn getSymbol(self: *const Elf, sym_index: u32) elf.Elf64_Sym { |
| 3270 | return self.local_symbols.items[sym_index]; |
| 3271 | } |
| 3272 | |
| 3273 | /// Returns name of the symbol at sym_index. |
| 3274 | pub fn getSymbolName(self: *const Elf, sym_index: u32) []const u8 { |
| 3275 | const sym = self.local_symbols.items[sym_index]; |
| 3276 | return self.shstrtab.get(sym.st_name).?; |
| 3277 | } |
| 3278 | |
| 3279 | pub fn getAtom(self: *const Elf, atom_index: Atom.Index) Atom { |
| 3280 | assert(atom_index < self.atoms.items.len); |
| 3281 | return self.atoms.items[atom_index]; |
| 3282 | } |
| 3283 | |
| 3284 | pub fn getAtomPtr(self: *Elf, atom_index: Atom.Index) *Atom { |
| 3285 | assert(atom_index < self.atoms.items.len); |
| 3286 | return &self.atoms.items[atom_index]; |
| 3287 | } |
| 3288 | |
| 3289 | /// Returns atom if there is an atom referenced by the symbol. |
| 3290 | /// Returns null on failure. |
| 3291 | pub fn getAtomIndexForSymbol(self: *Elf, sym_index: u32) ?Atom.Index { |
| 3292 | return self.atom_by_index_table.get(sym_index); |
| 3293 | } |