| ... | ... | @@ -12,7 +12,10 @@ linker_defined_index: ?File.Index = null, |
| 12 | 12 | |
| 13 | 13 | /// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write. |
| 14 | 14 | /// Same order as in the file. |
| 15 | | sections: std.MultiArrayList(Section) = .{}, |
| 15 | shdrs: std.ArrayListUnmanaged(elf.Elf64_Shdr) = .{}, |
| 16 | /// Given index to a section, pulls index of containing phdr if any. |
| 17 | phdr_to_shdr_table: std.AutoHashMapUnmanaged(u16, u16) = .{}, |
| 18 | /// File offset into the shdr table. |
| 16 | 19 | shdr_table_offset: ?u64 = null, |
| 17 | 20 | |
| 18 | 21 | /// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write. |
| ... | ... | @@ -85,7 +88,6 @@ symbols: std.ArrayListUnmanaged(Symbol) = .{}, |
| 85 | 88 | symbols_extra: std.ArrayListUnmanaged(u32) = .{}, |
| 86 | 89 | resolver: std.AutoArrayHashMapUnmanaged(u32, Symbol.Index) = .{}, |
| 87 | 90 | unresolved: std.AutoArrayHashMapUnmanaged(u32, void) = .{}, |
| 88 | | |
| 89 | 91 | symbols_free_list: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 90 | 92 | |
| 91 | 93 | phdr_table_dirty: bool = false, |
| ... | ... | @@ -109,6 +111,8 @@ decls: std.AutoHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{}, |
| 109 | 111 | |
| 110 | 112 | /// List of atoms that are owned directly by the linker. |
| 111 | 113 | atoms: std.ArrayListUnmanaged(Atom) = .{}, |
| 114 | /// Table of last atom index in a section and matching atom free list if any. |
| 115 | last_atom_and_free_list_table: std.AutoArrayHashMapUnmanaged(u16, LastAtomAndFreeList) = .{}, |
| 112 | 116 | |
| 113 | 117 | /// Table of unnamed constants associated with a parent `Decl`. |
| 114 | 118 | /// We store them here so that we can free the constants whenever the `Decl` |
| ... | ... | @@ -176,21 +180,18 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 176 | 180 | try self.atoms.append(allocator, .{}); |
| 177 | 181 | // Append null file at index 0 |
| 178 | 182 | try self.files.append(allocator, .null); |
| 179 | | // There must always be a null section in index 0 |
| 180 | | try self.sections.append(allocator, .{ |
| 181 | | .shdr = .{ |
| 182 | | .sh_name = 0, |
| 183 | | .sh_type = elf.SHT_NULL, |
| 184 | | .sh_flags = 0, |
| 185 | | .sh_addr = 0, |
| 186 | | .sh_offset = 0, |
| 187 | | .sh_size = 0, |
| 188 | | .sh_link = 0, |
| 189 | | .sh_info = 0, |
| 190 | | .sh_addralign = 0, |
| 191 | | .sh_entsize = 0, |
| 192 | | }, |
| 193 | | .phdr_index = undefined, |
| 183 | // There must always be a null shdr in index 0 |
| 184 | try self.shdrs.append(allocator, .{ |
| 185 | .sh_name = 0, |
| 186 | .sh_type = elf.SHT_NULL, |
| 187 | .sh_flags = 0, |
| 188 | .sh_addr = 0, |
| 189 | .sh_offset = 0, |
| 190 | .sh_size = 0, |
| 191 | .sh_link = 0, |
| 192 | .sh_info = 0, |
| 193 | .sh_addralign = 0, |
| 194 | .sh_entsize = 0, |
| 194 | 195 | }); |
| 195 | 196 | |
| 196 | 197 | try self.populateMissingMetadata(); |
| ... | ... | @@ -256,11 +257,8 @@ pub fn deinit(self: *Elf) void { |
| 256 | 257 | }; |
| 257 | 258 | self.files.deinit(gpa); |
| 258 | 259 | |
| 259 | | for (self.sections.items(.free_list)) |*free_list| { |
| 260 | | free_list.deinit(gpa); |
| 261 | | } |
| 262 | | self.sections.deinit(gpa); |
| 263 | | |
| 260 | self.shdrs.deinit(gpa); |
| 261 | self.phdr_to_shdr_table.deinit(gpa); |
| 264 | 262 | self.phdrs.deinit(gpa); |
| 265 | 263 | self.shstrtab.deinit(gpa); |
| 266 | 264 | self.strtab.deinit(gpa); |
| ... | ... | @@ -281,6 +279,10 @@ pub fn deinit(self: *Elf) void { |
| 281 | 279 | } |
| 282 | 280 | |
| 283 | 281 | self.atoms.deinit(gpa); |
| 282 | for (self.last_atom_and_free_list_table.values()) |*value| { |
| 283 | value.free_list.deinit(gpa); |
| 284 | } |
| 285 | self.last_atom_and_free_list_table.deinit(gpa); |
| 284 | 286 | self.lazy_syms.deinit(gpa); |
| 285 | 287 | |
| 286 | 288 | { |
| ... | ... | @@ -332,7 +334,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 { |
| 332 | 334 | |
| 333 | 335 | if (self.shdr_table_offset) |off| { |
| 334 | 336 | const shdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Shdr) else @sizeOf(elf.Elf64_Shdr); |
| 335 | | const tight_size = self.sections.slice().len * shdr_size; |
| 337 | const tight_size = self.shdrs.items.len * shdr_size; |
| 336 | 338 | const increased_size = padToIdeal(tight_size); |
| 337 | 339 | const test_end = off + increased_size; |
| 338 | 340 | if (end > off and start < test_end) { |
| ... | ... | @@ -340,7 +342,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) ?u64 { |
| 340 | 342 | } |
| 341 | 343 | } |
| 342 | 344 | |
| 343 | | for (self.sections.items(.shdr)) |section| { |
| 345 | for (self.shdrs.items) |section| { |
| 344 | 346 | const increased_size = padToIdeal(section.sh_size); |
| 345 | 347 | const test_end = section.sh_offset + increased_size; |
| 346 | 348 | if (end > section.sh_offset and start < test_end) { |
| ... | ... | @@ -364,7 +366,7 @@ pub fn allocatedSize(self: *Elf, start: u64) u64 { |
| 364 | 366 | if (self.shdr_table_offset) |off| { |
| 365 | 367 | if (off > start and off < min_pos) min_pos = off; |
| 366 | 368 | } |
| 367 | | for (self.sections.items(.shdr)) |section| { |
| 369 | for (self.shdrs.items) |section| { |
| 368 | 370 | if (section.sh_offset <= start) continue; |
| 369 | 371 | if (section.sh_offset < min_pos) min_pos = section.sh_offset; |
| 370 | 372 | } |
| ... | ... | @@ -522,197 +524,174 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 522 | 524 | } |
| 523 | 525 | |
| 524 | 526 | if (self.shstrtab_section_index == null) { |
| 525 | | self.shstrtab_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 527 | self.shstrtab_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 526 | 528 | assert(self.shstrtab.buffer.items.len == 0); |
| 527 | 529 | try self.shstrtab.buffer.append(gpa, 0); // need a 0 at position 0 |
| 528 | 530 | const off = self.findFreeSpace(self.shstrtab.buffer.items.len, 1); |
| 529 | 531 | log.debug("found .shstrtab free space 0x{x} to 0x{x}", .{ off, off + self.shstrtab.buffer.items.len }); |
| 530 | | try self.sections.append(gpa, .{ |
| 531 | | .shdr = .{ |
| 532 | | .sh_name = try self.shstrtab.insert(gpa, ".shstrtab"), |
| 533 | | .sh_type = elf.SHT_STRTAB, |
| 534 | | .sh_flags = 0, |
| 535 | | .sh_addr = 0, |
| 536 | | .sh_offset = off, |
| 537 | | .sh_size = self.shstrtab.buffer.items.len, |
| 538 | | .sh_link = 0, |
| 539 | | .sh_info = 0, |
| 540 | | .sh_addralign = 1, |
| 541 | | .sh_entsize = 0, |
| 542 | | }, |
| 543 | | .phdr_index = undefined, |
| 532 | try self.shdrs.append(gpa, .{ |
| 533 | .sh_name = try self.shstrtab.insert(gpa, ".shstrtab"), |
| 534 | .sh_type = elf.SHT_STRTAB, |
| 535 | .sh_flags = 0, |
| 536 | .sh_addr = 0, |
| 537 | .sh_offset = off, |
| 538 | .sh_size = self.shstrtab.buffer.items.len, |
| 539 | .sh_link = 0, |
| 540 | .sh_info = 0, |
| 541 | .sh_addralign = 1, |
| 542 | .sh_entsize = 0, |
| 544 | 543 | }); |
| 545 | 544 | self.shstrtab_dirty = true; |
| 546 | 545 | self.shdr_table_dirty = true; |
| 547 | 546 | } |
| 548 | 547 | |
| 549 | 548 | if (self.strtab_section_index == null) { |
| 550 | | self.strtab_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 549 | self.strtab_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 551 | 550 | assert(self.strtab.buffer.items.len == 0); |
| 552 | 551 | try self.strtab.buffer.append(gpa, 0); // need a 0 at position 0 |
| 553 | 552 | const off = self.findFreeSpace(self.strtab.buffer.items.len, 1); |
| 554 | 553 | log.debug("found .strtab free space 0x{x} to 0x{x}", .{ off, off + self.strtab.buffer.items.len }); |
| 555 | | try self.sections.append(gpa, .{ |
| 556 | | .shdr = .{ |
| 557 | | .sh_name = try self.shstrtab.insert(gpa, ".strtab"), |
| 558 | | .sh_type = elf.SHT_STRTAB, |
| 559 | | .sh_flags = 0, |
| 560 | | .sh_addr = 0, |
| 561 | | .sh_offset = off, |
| 562 | | .sh_size = self.strtab.buffer.items.len, |
| 563 | | .sh_link = 0, |
| 564 | | .sh_info = 0, |
| 565 | | .sh_addralign = 1, |
| 566 | | .sh_entsize = 0, |
| 567 | | }, |
| 568 | | .phdr_index = undefined, |
| 554 | try self.shdrs.append(gpa, .{ |
| 555 | .sh_name = try self.shstrtab.insert(gpa, ".strtab"), |
| 556 | .sh_type = elf.SHT_STRTAB, |
| 557 | .sh_flags = 0, |
| 558 | .sh_addr = 0, |
| 559 | .sh_offset = off, |
| 560 | .sh_size = self.strtab.buffer.items.len, |
| 561 | .sh_link = 0, |
| 562 | .sh_info = 0, |
| 563 | .sh_addralign = 1, |
| 564 | .sh_entsize = 0, |
| 569 | 565 | }); |
| 570 | 566 | self.strtab_dirty = true; |
| 571 | 567 | self.shdr_table_dirty = true; |
| 572 | 568 | } |
| 573 | 569 | |
| 574 | 570 | if (self.text_section_index == null) { |
| 575 | | self.text_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 571 | self.text_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 576 | 572 | const phdr = &self.phdrs.items[self.phdr_load_re_index.?]; |
| 577 | | |
| 578 | | try self.sections.append(gpa, .{ |
| 579 | | .shdr = .{ |
| 580 | | .sh_name = try self.shstrtab.insert(gpa, ".text"), |
| 581 | | .sh_type = elf.SHT_PROGBITS, |
| 582 | | .sh_flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| 583 | | .sh_addr = phdr.p_vaddr, |
| 584 | | .sh_offset = phdr.p_offset, |
| 585 | | .sh_size = phdr.p_filesz, |
| 586 | | .sh_link = 0, |
| 587 | | .sh_info = 0, |
| 588 | | .sh_addralign = 1, |
| 589 | | .sh_entsize = 0, |
| 590 | | }, |
| 591 | | .phdr_index = self.phdr_load_re_index.?, |
| 573 | try self.shdrs.append(gpa, .{ |
| 574 | .sh_name = try self.shstrtab.insert(gpa, ".text"), |
| 575 | .sh_type = elf.SHT_PROGBITS, |
| 576 | .sh_flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| 577 | .sh_addr = phdr.p_vaddr, |
| 578 | .sh_offset = phdr.p_offset, |
| 579 | .sh_size = phdr.p_filesz, |
| 580 | .sh_link = 0, |
| 581 | .sh_info = 0, |
| 582 | .sh_addralign = 1, |
| 583 | .sh_entsize = 0, |
| 592 | 584 | }); |
| 585 | try self.phdr_to_shdr_table.putNoClobber(gpa, self.text_section_index.?, self.phdr_load_re_index.?); |
| 586 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.text_section_index.?, .{}); |
| 593 | 587 | self.shdr_table_dirty = true; |
| 594 | 588 | } |
| 595 | 589 | |
| 596 | 590 | if (self.got_section_index == null) { |
| 597 | | self.got_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 591 | self.got_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 598 | 592 | const phdr = &self.phdrs.items[self.phdr_got_index.?]; |
| 599 | | |
| 600 | | try self.sections.append(gpa, .{ |
| 601 | | .shdr = .{ |
| 602 | | .sh_name = try self.shstrtab.insert(gpa, ".got"), |
| 603 | | .sh_type = elf.SHT_PROGBITS, |
| 604 | | .sh_flags = elf.SHF_ALLOC, |
| 605 | | .sh_addr = phdr.p_vaddr, |
| 606 | | .sh_offset = phdr.p_offset, |
| 607 | | .sh_size = phdr.p_filesz, |
| 608 | | .sh_link = 0, |
| 609 | | .sh_info = 0, |
| 610 | | .sh_addralign = @as(u16, ptr_size), |
| 611 | | .sh_entsize = 0, |
| 612 | | }, |
| 613 | | .phdr_index = self.phdr_got_index.?, |
| 593 | try self.shdrs.append(gpa, .{ |
| 594 | .sh_name = try self.shstrtab.insert(gpa, ".got"), |
| 595 | .sh_type = elf.SHT_PROGBITS, |
| 596 | .sh_flags = elf.SHF_ALLOC, |
| 597 | .sh_addr = phdr.p_vaddr, |
| 598 | .sh_offset = phdr.p_offset, |
| 599 | .sh_size = phdr.p_filesz, |
| 600 | .sh_link = 0, |
| 601 | .sh_info = 0, |
| 602 | .sh_addralign = @as(u16, ptr_size), |
| 603 | .sh_entsize = 0, |
| 614 | 604 | }); |
| 605 | try self.phdr_to_shdr_table.putNoClobber(gpa, self.got_section_index.?, self.phdr_got_index.?); |
| 615 | 606 | self.shdr_table_dirty = true; |
| 616 | 607 | } |
| 617 | 608 | |
| 618 | 609 | if (self.rodata_section_index == null) { |
| 619 | | self.rodata_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 610 | self.rodata_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 620 | 611 | const phdr = &self.phdrs.items[self.phdr_load_ro_index.?]; |
| 621 | | |
| 622 | | try self.sections.append(gpa, .{ |
| 623 | | .shdr = .{ |
| 624 | | .sh_name = try self.shstrtab.insert(gpa, ".rodata"), |
| 625 | | .sh_type = elf.SHT_PROGBITS, |
| 626 | | .sh_flags = elf.SHF_ALLOC, |
| 627 | | .sh_addr = phdr.p_vaddr, |
| 628 | | .sh_offset = phdr.p_offset, |
| 629 | | .sh_size = phdr.p_filesz, |
| 630 | | .sh_link = 0, |
| 631 | | .sh_info = 0, |
| 632 | | .sh_addralign = 1, |
| 633 | | .sh_entsize = 0, |
| 634 | | }, |
| 635 | | .phdr_index = self.phdr_load_ro_index.?, |
| 612 | try self.shdrs.append(gpa, .{ |
| 613 | .sh_name = try self.shstrtab.insert(gpa, ".rodata"), |
| 614 | .sh_type = elf.SHT_PROGBITS, |
| 615 | .sh_flags = elf.SHF_ALLOC, |
| 616 | .sh_addr = phdr.p_vaddr, |
| 617 | .sh_offset = phdr.p_offset, |
| 618 | .sh_size = phdr.p_filesz, |
| 619 | .sh_link = 0, |
| 620 | .sh_info = 0, |
| 621 | .sh_addralign = 1, |
| 622 | .sh_entsize = 0, |
| 636 | 623 | }); |
| 624 | try self.phdr_to_shdr_table.putNoClobber(gpa, self.rodata_section_index.?, self.phdr_load_ro_index.?); |
| 625 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.rodata_section_index.?, .{}); |
| 637 | 626 | self.shdr_table_dirty = true; |
| 638 | 627 | } |
| 639 | 628 | |
| 640 | 629 | if (self.data_section_index == null) { |
| 641 | | self.data_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 630 | self.data_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 642 | 631 | const phdr = &self.phdrs.items[self.phdr_load_rw_index.?]; |
| 643 | | |
| 644 | | try self.sections.append(gpa, .{ |
| 645 | | .shdr = .{ |
| 646 | | .sh_name = try self.shstrtab.insert(gpa, ".data"), |
| 647 | | .sh_type = elf.SHT_PROGBITS, |
| 648 | | .sh_flags = elf.SHF_WRITE | elf.SHF_ALLOC, |
| 649 | | .sh_addr = phdr.p_vaddr, |
| 650 | | .sh_offset = phdr.p_offset, |
| 651 | | .sh_size = phdr.p_filesz, |
| 652 | | .sh_link = 0, |
| 653 | | .sh_info = 0, |
| 654 | | .sh_addralign = @as(u16, ptr_size), |
| 655 | | .sh_entsize = 0, |
| 656 | | }, |
| 657 | | .phdr_index = self.phdr_load_rw_index.?, |
| 632 | try self.shdrs.append(gpa, .{ |
| 633 | .sh_name = try self.shstrtab.insert(gpa, ".data"), |
| 634 | .sh_type = elf.SHT_PROGBITS, |
| 635 | .sh_flags = elf.SHF_WRITE | elf.SHF_ALLOC, |
| 636 | .sh_addr = phdr.p_vaddr, |
| 637 | .sh_offset = phdr.p_offset, |
| 638 | .sh_size = phdr.p_filesz, |
| 639 | .sh_link = 0, |
| 640 | .sh_info = 0, |
| 641 | .sh_addralign = @as(u16, ptr_size), |
| 642 | .sh_entsize = 0, |
| 658 | 643 | }); |
| 644 | try self.phdr_to_shdr_table.putNoClobber(gpa, self.data_section_index.?, self.phdr_load_rw_index.?); |
| 645 | try self.last_atom_and_free_list_table.putNoClobber(gpa, self.data_section_index.?, .{}); |
| 659 | 646 | self.shdr_table_dirty = true; |
| 660 | 647 | } |
| 661 | 648 | |
| 662 | 649 | if (self.symtab_section_index == null) { |
| 663 | | self.symtab_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 650 | self.symtab_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 664 | 651 | const min_align: u16 = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym); |
| 665 | 652 | const each_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym); |
| 666 | 653 | const file_size = self.base.options.symbol_count_hint * each_size; |
| 667 | 654 | const off = self.findFreeSpace(file_size, min_align); |
| 668 | 655 | log.debug("found symtab free space 0x{x} to 0x{x}", .{ off, off + file_size }); |
| 669 | | |
| 670 | | try self.sections.append(gpa, .{ |
| 671 | | .shdr = .{ |
| 672 | | .sh_name = try self.shstrtab.insert(gpa, ".symtab"), |
| 673 | | .sh_type = elf.SHT_SYMTAB, |
| 674 | | .sh_flags = 0, |
| 675 | | .sh_addr = 0, |
| 676 | | .sh_offset = off, |
| 677 | | .sh_size = file_size, |
| 678 | | // The section header index of the associated string table. |
| 679 | | .sh_link = self.strtab_section_index.?, |
| 680 | | .sh_info = @as(u32, @intCast(self.symbols.items.len)), |
| 681 | | .sh_addralign = min_align, |
| 682 | | .sh_entsize = each_size, |
| 683 | | }, |
| 684 | | .phdr_index = undefined, |
| 656 | try self.shdrs.append(gpa, .{ |
| 657 | .sh_name = try self.shstrtab.insert(gpa, ".symtab"), |
| 658 | .sh_type = elf.SHT_SYMTAB, |
| 659 | .sh_flags = 0, |
| 660 | .sh_addr = 0, |
| 661 | .sh_offset = off, |
| 662 | .sh_size = file_size, |
| 663 | // The section header index of the associated string table. |
| 664 | .sh_link = self.strtab_section_index.?, |
| 665 | .sh_info = @as(u32, @intCast(self.symbols.items.len)), |
| 666 | .sh_addralign = min_align, |
| 667 | .sh_entsize = each_size, |
| 685 | 668 | }); |
| 686 | 669 | self.shdr_table_dirty = true; |
| 687 | 670 | } |
| 688 | 671 | |
| 689 | 672 | if (self.dwarf) |*dw| { |
| 690 | 673 | if (self.debug_str_section_index == null) { |
| 691 | | self.debug_str_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 674 | self.debug_str_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 692 | 675 | assert(dw.strtab.buffer.items.len == 0); |
| 693 | 676 | try dw.strtab.buffer.append(gpa, 0); |
| 694 | | try self.sections.append(gpa, .{ |
| 695 | | .shdr = .{ |
| 696 | | .sh_name = try self.shstrtab.insert(gpa, ".debug_str"), |
| 697 | | .sh_type = elf.SHT_PROGBITS, |
| 698 | | .sh_flags = elf.SHF_MERGE | elf.SHF_STRINGS, |
| 699 | | .sh_addr = 0, |
| 700 | | .sh_offset = 0, |
| 701 | | .sh_size = 0, |
| 702 | | .sh_link = 0, |
| 703 | | .sh_info = 0, |
| 704 | | .sh_addralign = 1, |
| 705 | | .sh_entsize = 1, |
| 706 | | }, |
| 707 | | .phdr_index = undefined, |
| 677 | try self.shdrs.append(gpa, .{ |
| 678 | .sh_name = try self.shstrtab.insert(gpa, ".debug_str"), |
| 679 | .sh_type = elf.SHT_PROGBITS, |
| 680 | .sh_flags = elf.SHF_MERGE | elf.SHF_STRINGS, |
| 681 | .sh_addr = 0, |
| 682 | .sh_offset = 0, |
| 683 | .sh_size = 0, |
| 684 | .sh_link = 0, |
| 685 | .sh_info = 0, |
| 686 | .sh_addralign = 1, |
| 687 | .sh_entsize = 1, |
| 708 | 688 | }); |
| 709 | 689 | self.debug_strtab_dirty = true; |
| 710 | 690 | self.shdr_table_dirty = true; |
| 711 | 691 | } |
| 712 | 692 | |
| 713 | 693 | if (self.debug_info_section_index == null) { |
| 714 | | self.debug_info_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 715 | | |
| 694 | self.debug_info_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 716 | 695 | const file_size_hint = 200; |
| 717 | 696 | const p_align = 1; |
| 718 | 697 | const off = self.findFreeSpace(file_size_hint, p_align); |
| ... | ... | @@ -720,28 +699,24 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 720 | 699 | off, |
| 721 | 700 | off + file_size_hint, |
| 722 | 701 | }); |
| 723 | | try self.sections.append(gpa, .{ |
| 724 | | .shdr = .{ |
| 725 | | .sh_name = try self.shstrtab.insert(gpa, ".debug_info"), |
| 726 | | .sh_type = elf.SHT_PROGBITS, |
| 727 | | .sh_flags = 0, |
| 728 | | .sh_addr = 0, |
| 729 | | .sh_offset = off, |
| 730 | | .sh_size = file_size_hint, |
| 731 | | .sh_link = 0, |
| 732 | | .sh_info = 0, |
| 733 | | .sh_addralign = p_align, |
| 734 | | .sh_entsize = 0, |
| 735 | | }, |
| 736 | | .phdr_index = undefined, |
| 702 | try self.shdrs.append(gpa, .{ |
| 703 | .sh_name = try self.shstrtab.insert(gpa, ".debug_info"), |
| 704 | .sh_type = elf.SHT_PROGBITS, |
| 705 | .sh_flags = 0, |
| 706 | .sh_addr = 0, |
| 707 | .sh_offset = off, |
| 708 | .sh_size = file_size_hint, |
| 709 | .sh_link = 0, |
| 710 | .sh_info = 0, |
| 711 | .sh_addralign = p_align, |
| 712 | .sh_entsize = 0, |
| 737 | 713 | }); |
| 738 | 714 | self.shdr_table_dirty = true; |
| 739 | 715 | self.debug_info_header_dirty = true; |
| 740 | 716 | } |
| 741 | 717 | |
| 742 | 718 | if (self.debug_abbrev_section_index == null) { |
| 743 | | self.debug_abbrev_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 744 | | |
| 719 | self.debug_abbrev_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 745 | 720 | const file_size_hint = 128; |
| 746 | 721 | const p_align = 1; |
| 747 | 722 | const off = self.findFreeSpace(file_size_hint, p_align); |
| ... | ... | @@ -749,28 +724,24 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 749 | 724 | off, |
| 750 | 725 | off + file_size_hint, |
| 751 | 726 | }); |
| 752 | | try self.sections.append(gpa, .{ |
| 753 | | .shdr = .{ |
| 754 | | .sh_name = try self.shstrtab.insert(gpa, ".debug_abbrev"), |
| 755 | | .sh_type = elf.SHT_PROGBITS, |
| 756 | | .sh_flags = 0, |
| 757 | | .sh_addr = 0, |
| 758 | | .sh_offset = off, |
| 759 | | .sh_size = file_size_hint, |
| 760 | | .sh_link = 0, |
| 761 | | .sh_info = 0, |
| 762 | | .sh_addralign = p_align, |
| 763 | | .sh_entsize = 0, |
| 764 | | }, |
| 765 | | .phdr_index = undefined, |
| 727 | try self.shdrs.append(gpa, .{ |
| 728 | .sh_name = try self.shstrtab.insert(gpa, ".debug_abbrev"), |
| 729 | .sh_type = elf.SHT_PROGBITS, |
| 730 | .sh_flags = 0, |
| 731 | .sh_addr = 0, |
| 732 | .sh_offset = off, |
| 733 | .sh_size = file_size_hint, |
| 734 | .sh_link = 0, |
| 735 | .sh_info = 0, |
| 736 | .sh_addralign = p_align, |
| 737 | .sh_entsize = 0, |
| 766 | 738 | }); |
| 767 | 739 | self.shdr_table_dirty = true; |
| 768 | 740 | self.debug_abbrev_section_dirty = true; |
| 769 | 741 | } |
| 770 | 742 | |
| 771 | 743 | if (self.debug_aranges_section_index == null) { |
| 772 | | self.debug_aranges_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 773 | | |
| 744 | self.debug_aranges_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 774 | 745 | const file_size_hint = 160; |
| 775 | 746 | const p_align = 16; |
| 776 | 747 | const off = self.findFreeSpace(file_size_hint, p_align); |
| ... | ... | @@ -778,28 +749,24 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 778 | 749 | off, |
| 779 | 750 | off + file_size_hint, |
| 780 | 751 | }); |
| 781 | | try self.sections.append(gpa, .{ |
| 782 | | .shdr = .{ |
| 783 | | .sh_name = try self.shstrtab.insert(gpa, ".debug_aranges"), |
| 784 | | .sh_type = elf.SHT_PROGBITS, |
| 785 | | .sh_flags = 0, |
| 786 | | .sh_addr = 0, |
| 787 | | .sh_offset = off, |
| 788 | | .sh_size = file_size_hint, |
| 789 | | .sh_link = 0, |
| 790 | | .sh_info = 0, |
| 791 | | .sh_addralign = p_align, |
| 792 | | .sh_entsize = 0, |
| 793 | | }, |
| 794 | | .phdr_index = undefined, |
| 752 | try self.shdrs.append(gpa, .{ |
| 753 | .sh_name = try self.shstrtab.insert(gpa, ".debug_aranges"), |
| 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, |
| 795 | 763 | }); |
| 796 | 764 | self.shdr_table_dirty = true; |
| 797 | 765 | self.debug_aranges_section_dirty = true; |
| 798 | 766 | } |
| 799 | 767 | |
| 800 | 768 | if (self.debug_line_section_index == null) { |
| 801 | | self.debug_line_section_index = @as(u16, @intCast(self.sections.slice().len)); |
| 802 | | |
| 769 | self.debug_line_section_index = @as(u16, @intCast(self.shdrs.items.len)); |
| 803 | 770 | const file_size_hint = 250; |
| 804 | 771 | const p_align = 1; |
| 805 | 772 | const off = self.findFreeSpace(file_size_hint, p_align); |
| ... | ... | @@ -807,20 +774,17 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 807 | 774 | off, |
| 808 | 775 | off + file_size_hint, |
| 809 | 776 | }); |
| 810 | | try self.sections.append(gpa, .{ |
| 811 | | .shdr = .{ |
| 812 | | .sh_name = try self.shstrtab.insert(gpa, ".debug_line"), |
| 813 | | .sh_type = elf.SHT_PROGBITS, |
| 814 | | .sh_flags = 0, |
| 815 | | .sh_addr = 0, |
| 816 | | .sh_offset = off, |
| 817 | | .sh_size = file_size_hint, |
| 818 | | .sh_link = 0, |
| 819 | | .sh_info = 0, |
| 820 | | .sh_addralign = p_align, |
| 821 | | .sh_entsize = 0, |
| 822 | | }, |
| 823 | | .phdr_index = undefined, |
| 777 | try self.shdrs.append(gpa, .{ |
| 778 | .sh_name = try self.shstrtab.insert(gpa, ".debug_line"), |
| 779 | .sh_type = elf.SHT_PROGBITS, |
| 780 | .sh_flags = 0, |
| 781 | .sh_addr = 0, |
| 782 | .sh_offset = off, |
| 783 | .sh_size = file_size_hint, |
| 784 | .sh_link = 0, |
| 785 | .sh_info = 0, |
| 786 | .sh_addralign = p_align, |
| 787 | .sh_entsize = 0, |
| 824 | 788 | }); |
| 825 | 789 | self.shdr_table_dirty = true; |
| 826 | 790 | self.debug_line_header_dirty = true; |
| ... | ... | @@ -836,7 +800,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 836 | 800 | .p64 => @alignOf(elf.Elf64_Shdr), |
| 837 | 801 | }; |
| 838 | 802 | if (self.shdr_table_offset == null) { |
| 839 | | self.shdr_table_offset = self.findFreeSpace(self.sections.slice().len * shsize, shalign); |
| 803 | self.shdr_table_offset = self.findFreeSpace(self.shdrs.items.len * shsize, shalign); |
| 840 | 804 | self.shdr_table_dirty = true; |
| 841 | 805 | } |
| 842 | 806 | |
| ... | ... | @@ -854,7 +818,7 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 854 | 818 | // offset + it's filesize. |
| 855 | 819 | var max_file_offset: u64 = 0; |
| 856 | 820 | |
| 857 | | for (self.sections.items(.shdr)) |shdr| { |
| 821 | for (self.shdrs.items) |shdr| { |
| 858 | 822 | if (shdr.sh_offset + shdr.sh_size > max_file_offset) { |
| 859 | 823 | max_file_offset = shdr.sh_offset + shdr.sh_size; |
| 860 | 824 | } |
| ... | ... | @@ -885,19 +849,17 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 885 | 849 | |
| 886 | 850 | pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void { |
| 887 | 851 | // TODO Also detect virtual address collisions. |
| 888 | | const shdr = &self.sections.items(.shdr)[shdr_index]; |
| 889 | | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 852 | const shdr = &self.shdrs.items[shdr_index]; |
| 853 | const phdr_index = self.phdr_to_shdr_table.get(shdr_index).?; |
| 890 | 854 | const phdr = &self.phdrs.items[phdr_index]; |
| 891 | | const last_atom_index = self.sections.items(.last_atom_index)[shdr_index]; |
| 892 | 855 | |
| 893 | 856 | if (needed_size > self.allocatedSize(shdr.sh_offset)) { |
| 894 | 857 | // Must move the entire section. |
| 895 | 858 | const new_offset = self.findFreeSpace(needed_size, self.page_size); |
| 896 | | const existing_size = if (self.atom(last_atom_index)) |last| blk: { |
| 859 | const existing_size = if (self.last_atom_and_free_list_table.get(shdr_index)) |meta| blk: { |
| 860 | const last = self.atom(meta.last_atom_index) orelse break :blk 0; |
| 897 | 861 | break :blk (last.value + last.size) - phdr.p_vaddr; |
| 898 | | } else if (shdr_index == self.got_section_index.?) blk: { |
| 899 | | break :blk shdr.sh_size; |
| 900 | | } else 0; |
| 862 | } else shdr.sh_size; |
| 901 | 863 | shdr.sh_size = 0; |
| 902 | 864 | |
| 903 | 865 | log.debug("new '{?s}' file offset 0x{x} to 0x{x}", .{ |
| ... | ... | @@ -927,7 +889,7 @@ pub fn growNonAllocSection( |
| 927 | 889 | min_alignment: u32, |
| 928 | 890 | requires_file_copy: bool, |
| 929 | 891 | ) !void { |
| 930 | | const shdr = &self.sections.items(.shdr)[shdr_index]; |
| 892 | const shdr = &self.shdrs.items[shdr_index]; |
| 931 | 893 | |
| 932 | 894 | if (needed_size > self.allocatedSize(shdr.sh_offset)) { |
| 933 | 895 | const existing_size = if (self.symtab_section_index.? == shdr_index) blk: { |
| ... | ... | @@ -940,7 +902,12 @@ pub fn growNonAllocSection( |
| 940 | 902 | shdr.sh_size = 0; |
| 941 | 903 | // Move all the symbols to a new file location. |
| 942 | 904 | const new_offset = self.findFreeSpace(needed_size, min_alignment); |
| 943 | | log.debug("moving '{?s}' from 0x{x} to 0x{x}", .{ self.shstrtab.get(shdr.sh_name), shdr.sh_offset, new_offset }); |
| 905 | |
| 906 | log.debug("moving '{?s}' from 0x{x} to 0x{x}", .{ |
| 907 | self.shstrtab.get(shdr.sh_name), |
| 908 | shdr.sh_offset, |
| 909 | new_offset, |
| 910 | }); |
| 944 | 911 | |
| 945 | 912 | if (requires_file_copy) { |
| 946 | 913 | const amt = try self.base.file.?.copyRangeAll( |
| ... | ... | @@ -1071,7 +1038,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1071 | 1038 | const atom_index = entry.key_ptr.*; |
| 1072 | 1039 | const relocs = entry.value_ptr.*; |
| 1073 | 1040 | const atom_ptr = self.atom(atom_index).?; |
| 1074 | | const source_shdr = self.sections.items(.shdr)[atom_ptr.output_section_index]; |
| 1041 | const source_shdr = &self.shdrs.items[atom_ptr.output_section_index]; |
| 1075 | 1042 | |
| 1076 | 1043 | log.debug("relocating '{s}'", .{atom_ptr.name(self)}); |
| 1077 | 1044 | |
| ... | ... | @@ -1209,9 +1176,9 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1209 | 1176 | |
| 1210 | 1177 | { |
| 1211 | 1178 | const shdr_index = self.shstrtab_section_index.?; |
| 1212 | | if (self.shstrtab_dirty or self.shstrtab.buffer.items.len != self.sections.items(.shdr)[shdr_index].sh_size) { |
| 1179 | if (self.shstrtab_dirty or self.shstrtab.buffer.items.len != self.shdrs.items[shdr_index].sh_size) { |
| 1213 | 1180 | try self.growNonAllocSection(shdr_index, self.shstrtab.buffer.items.len, 1, false); |
| 1214 | | const shstrtab_sect = self.sections.items(.shdr)[shdr_index]; |
| 1181 | const shstrtab_sect = &self.shdrs.items[shdr_index]; |
| 1215 | 1182 | try self.base.file.?.pwriteAll(self.shstrtab.buffer.items, shstrtab_sect.sh_offset); |
| 1216 | 1183 | self.shstrtab_dirty = false; |
| 1217 | 1184 | } |
| ... | ... | @@ -1219,9 +1186,9 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1219 | 1186 | |
| 1220 | 1187 | { |
| 1221 | 1188 | const shdr_index = self.strtab_section_index.?; |
| 1222 | | if (self.strtab_dirty or self.strtab.buffer.items.len != self.sections.items(.shdr)[shdr_index].sh_size) { |
| 1189 | if (self.strtab_dirty or self.strtab.buffer.items.len != self.shdrs.items[shdr_index].sh_size) { |
| 1223 | 1190 | try self.growNonAllocSection(shdr_index, self.strtab.buffer.items.len, 1, false); |
| 1224 | | const strtab_sect = self.sections.items(.shdr)[shdr_index]; |
| 1191 | const strtab_sect = self.shdrs.items[shdr_index]; |
| 1225 | 1192 | try self.base.file.?.pwriteAll(self.strtab.buffer.items, strtab_sect.sh_offset); |
| 1226 | 1193 | self.strtab_dirty = false; |
| 1227 | 1194 | } |
| ... | ... | @@ -1229,9 +1196,9 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1229 | 1196 | |
| 1230 | 1197 | if (self.dwarf) |dwarf| { |
| 1231 | 1198 | const shdr_index = self.debug_str_section_index.?; |
| 1232 | | if (self.debug_strtab_dirty or dwarf.strtab.buffer.items.len != self.sections.items(.shdr)[shdr_index].sh_size) { |
| 1199 | if (self.debug_strtab_dirty or dwarf.strtab.buffer.items.len != self.shdrs.items[shdr_index].sh_size) { |
| 1233 | 1200 | try self.growNonAllocSection(shdr_index, dwarf.strtab.buffer.items.len, 1, false); |
| 1234 | | const debug_strtab_sect = self.sections.items(.shdr)[shdr_index]; |
| 1201 | const debug_strtab_sect = self.shdrs.items[shdr_index]; |
| 1235 | 1202 | try self.base.file.?.pwriteAll(dwarf.strtab.buffer.items, debug_strtab_sect.sh_offset); |
| 1236 | 1203 | self.debug_strtab_dirty = false; |
| 1237 | 1204 | } |
| ... | ... | @@ -1247,7 +1214,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1247 | 1214 | .p64 => @alignOf(elf.Elf64_Shdr), |
| 1248 | 1215 | }; |
| 1249 | 1216 | const allocated_size = self.allocatedSize(self.shdr_table_offset.?); |
| 1250 | | const needed_size = self.sections.slice().len * shsize; |
| 1217 | const needed_size = self.shdrs.items.len * shsize; |
| 1251 | 1218 | |
| 1252 | 1219 | if (needed_size > allocated_size) { |
| 1253 | 1220 | self.shdr_table_offset = null; // free the space |
| ... | ... | @@ -1256,12 +1223,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1256 | 1223 | |
| 1257 | 1224 | switch (self.ptr_width) { |
| 1258 | 1225 | .p32 => { |
| 1259 | | const slice = self.sections.slice(); |
| 1260 | | const buf = try gpa.alloc(elf.Elf32_Shdr, slice.len); |
| 1226 | const buf = try gpa.alloc(elf.Elf32_Shdr, self.shdrs.items.len); |
| 1261 | 1227 | defer gpa.free(buf); |
| 1262 | 1228 | |
| 1263 | 1229 | for (buf, 0..) |*shdr, i| { |
| 1264 | | shdr.* = shdrTo32(slice.items(.shdr)[i]); |
| 1230 | shdr.* = shdrTo32(self.shdrs.items[i]); |
| 1265 | 1231 | log.debug("writing section {?s}: {}", .{ self.shstrtab.get(shdr.sh_name), shdr.* }); |
| 1266 | 1232 | if (foreign_endian) { |
| 1267 | 1233 | mem.byteSwapAllFields(elf.Elf32_Shdr, shdr); |
| ... | ... | @@ -1270,12 +1236,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1270 | 1236 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(buf), self.shdr_table_offset.?); |
| 1271 | 1237 | }, |
| 1272 | 1238 | .p64 => { |
| 1273 | | const slice = self.sections.slice(); |
| 1274 | | const buf = try gpa.alloc(elf.Elf64_Shdr, slice.len); |
| 1239 | const buf = try gpa.alloc(elf.Elf64_Shdr, self.shdrs.items.len); |
| 1275 | 1240 | defer gpa.free(buf); |
| 1276 | 1241 | |
| 1277 | 1242 | for (buf, 0..) |*shdr, i| { |
| 1278 | | shdr.* = slice.items(.shdr)[i]; |
| 1243 | shdr.* = self.shdrs.items[i]; |
| 1279 | 1244 | log.debug("writing section {?s}: {}", .{ self.shstrtab.get(shdr.sh_name), shdr.* }); |
| 1280 | 1245 | if (foreign_endian) { |
| 1281 | 1246 | mem.byteSwapAllFields(elf.Elf64_Shdr, shdr); |
| ... | ... | @@ -2118,7 +2083,7 @@ fn writeElfHeader(self: *Elf) !void { |
| 2118 | 2083 | mem.writeInt(u16, hdr_buf[index..][0..2], e_shentsize, endian); |
| 2119 | 2084 | index += 2; |
| 2120 | 2085 | |
| 2121 | | const e_shnum = @as(u16, @intCast(self.sections.slice().len)); |
| 2086 | const e_shnum = @as(u16, @intCast(self.shdrs.items.len)); |
| 2122 | 2087 | mem.writeInt(u16, hdr_buf[index..][0..2], e_shnum, endian); |
| 2123 | 2088 | index += 2; |
| 2124 | 2089 | |
| ... | ... | @@ -2305,9 +2270,9 @@ fn updateDeclCode( |
| 2305 | 2270 | try self.got.writeEntry(self, got_index); |
| 2306 | 2271 | } |
| 2307 | 2272 | |
| 2308 | | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 2273 | const phdr_index = self.phdr_to_shdr_table.get(shdr_index).?; |
| 2309 | 2274 | const section_offset = sym.value - self.phdrs.items[phdr_index].p_vaddr; |
| 2310 | | const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset; |
| 2275 | const file_offset = self.shdrs.items[shdr_index].sh_offset + section_offset; |
| 2311 | 2276 | |
| 2312 | 2277 | if (self.base.child_pid) |pid| { |
| 2313 | 2278 | switch (builtin.os.tag) { |
| ... | ... | @@ -2510,7 +2475,7 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol. |
| 2510 | 2475 | }; |
| 2511 | 2476 | |
| 2512 | 2477 | const local_sym = self.symbol(symbol_index); |
| 2513 | | const phdr_index = self.sections.items(.phdr_index)[local_sym.output_section_index]; |
| 2478 | const phdr_index = self.phdr_to_shdr_table.get(local_sym.output_section_index).?; |
| 2514 | 2479 | local_sym.name_offset = name_str_index; |
| 2515 | 2480 | const local_esym = local_sym.sourceSymbol(self); |
| 2516 | 2481 | local_esym.st_name = name_str_index; |
| ... | ... | @@ -2537,7 +2502,7 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol. |
| 2537 | 2502 | try self.got.writeEntry(self, got_index); |
| 2538 | 2503 | |
| 2539 | 2504 | const section_offset = atom_ptr.value - self.phdrs.items[phdr_index].p_vaddr; |
| 2540 | | const file_offset = self.sections.items(.shdr)[local_sym.output_section_index].sh_offset + section_offset; |
| 2505 | const file_offset = self.shdrs.items[local_sym.output_section_index].sh_offset + section_offset; |
| 2541 | 2506 | try self.base.file.?.pwriteAll(code, file_offset); |
| 2542 | 2507 | } |
| 2543 | 2508 | |
| ... | ... | @@ -2584,7 +2549,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2584 | 2549 | |
| 2585 | 2550 | const required_alignment = typed_value.ty.abiAlignment(mod); |
| 2586 | 2551 | const shdr_index = self.rodata_section_index.?; |
| 2587 | | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 2552 | const phdr_index = self.phdr_to_shdr_table.get(shdr_index).?; |
| 2588 | 2553 | const local_sym = self.symbol(sym_index); |
| 2589 | 2554 | local_sym.name_offset = name_str_index; |
| 2590 | 2555 | const local_esym = local_sym.sourceSymbol(self); |
| ... | ... | @@ -2607,7 +2572,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2607 | 2572 | try unnamed_consts.append(gpa, atom_ptr.atom_index); |
| 2608 | 2573 | |
| 2609 | 2574 | const section_offset = atom_ptr.value - self.phdrs.items[phdr_index].p_vaddr; |
| 2610 | | const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset; |
| 2575 | const file_offset = self.shdrs.items[shdr_index].sh_offset + section_offset; |
| 2611 | 2576 | try self.base.file.?.pwriteAll(code, file_offset); |
| 2612 | 2577 | |
| 2613 | 2578 | return sym_index; |
| ... | ... | @@ -2775,7 +2740,7 @@ fn addLinkerDefinedSymbols(self: *Elf) !void { |
| 2775 | 2740 | fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 2776 | 2741 | // _DYNAMIC |
| 2777 | 2742 | if (self.dynamic_section_index) |shndx| { |
| 2778 | | const shdr = self.sections.items(.shdr)[shndx]; |
| 2743 | const shdr = &self.shdrs.items[shndx]; |
| 2779 | 2744 | const symbol_ptr = self.symbol(self.dynamic_index.?); |
| 2780 | 2745 | symbol_ptr.value = shdr.sh_addr; |
| 2781 | 2746 | symbol_ptr.output_section_index = shndx; |
| ... | ... | @@ -2792,7 +2757,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 2792 | 2757 | if (self.sectionByName(".init_array")) |shndx| { |
| 2793 | 2758 | const start_sym = self.symbol(self.init_array_start_index.?); |
| 2794 | 2759 | const end_sym = self.symbol(self.init_array_end_index.?); |
| 2795 | | const shdr = &self.sections.items(.shdr)[shndx]; |
| 2760 | const shdr = &self.shdrs.items[shndx]; |
| 2796 | 2761 | start_sym.output_section_index = shndx; |
| 2797 | 2762 | start_sym.value = shdr.sh_addr; |
| 2798 | 2763 | end_sym.output_section_index = shndx; |
| ... | ... | @@ -2803,7 +2768,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 2803 | 2768 | if (self.sectionByName(".fini_array")) |shndx| { |
| 2804 | 2769 | const start_sym = self.symbol(self.fini_array_start_index.?); |
| 2805 | 2770 | const end_sym = self.symbol(self.fini_array_end_index.?); |
| 2806 | | const shdr = &self.sections.items(.shdr)[shndx]; |
| 2771 | const shdr = &self.shdrs.items[shndx]; |
| 2807 | 2772 | start_sym.output_section_index = shndx; |
| 2808 | 2773 | start_sym.value = shdr.sh_addr; |
| 2809 | 2774 | end_sym.output_section_index = shndx; |
| ... | ... | @@ -2814,7 +2779,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 2814 | 2779 | if (self.sectionByName(".preinit_array")) |shndx| { |
| 2815 | 2780 | const start_sym = self.symbol(self.preinit_array_start_index.?); |
| 2816 | 2781 | const end_sym = self.symbol(self.preinit_array_end_index.?); |
| 2817 | | const shdr = &self.sections.items(.shdr)[shndx]; |
| 2782 | const shdr = &self.shdrs.items[shndx]; |
| 2818 | 2783 | start_sym.output_section_index = shndx; |
| 2819 | 2784 | start_sym.value = shdr.sh_addr; |
| 2820 | 2785 | end_sym.output_section_index = shndx; |
| ... | ... | @@ -2823,7 +2788,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 2823 | 2788 | |
| 2824 | 2789 | // _GLOBAL_OFFSET_TABLE_ |
| 2825 | 2790 | if (self.got_plt_section_index) |shndx| { |
| 2826 | | const shdr = &self.sections.items(.shdr)[shndx]; |
| 2791 | const shdr = &self.shdrs.items[shndx]; |
| 2827 | 2792 | const symbol_ptr = self.symbol(self.got_index.?); |
| 2828 | 2793 | symbol_ptr.value = shdr.sh_addr; |
| 2829 | 2794 | symbol_ptr.output_section_index = shndx; |
| ... | ... | @@ -2831,7 +2796,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 2831 | 2796 | |
| 2832 | 2797 | // _PROCEDURE_LINKAGE_TABLE_ |
| 2833 | 2798 | if (self.plt_section_index) |shndx| { |
| 2834 | | const shdr = &self.sections.items(.shdr)[shndx]; |
| 2799 | const shdr = &self.shdrs.items[shndx]; |
| 2835 | 2800 | const symbol_ptr = self.symbol(self.plt_index.?); |
| 2836 | 2801 | symbol_ptr.value = shdr.sh_addr; |
| 2837 | 2802 | symbol_ptr.output_section_index = shndx; |
| ... | ... | @@ -2839,7 +2804,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 2839 | 2804 | |
| 2840 | 2805 | // __dso_handle |
| 2841 | 2806 | if (self.dso_handle_index) |index| { |
| 2842 | | const shdr = &self.sections.items(.shdr)[1]; |
| 2807 | const shdr = &self.shdrs.items[1]; |
| 2843 | 2808 | const symbol_ptr = self.symbol(index); |
| 2844 | 2809 | symbol_ptr.value = shdr.sh_addr; |
| 2845 | 2810 | symbol_ptr.output_section_index = 0; |
| ... | ... | @@ -2847,7 +2812,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 2847 | 2812 | |
| 2848 | 2813 | // __GNU_EH_FRAME_HDR |
| 2849 | 2814 | if (self.eh_frame_hdr_section_index) |shndx| { |
| 2850 | | const shdr = &self.sections.items(.shdr)[shndx]; |
| 2815 | const shdr = &self.shdrs.items[shndx]; |
| 2851 | 2816 | const symbol_ptr = self.symbol(self.gnu_eh_frame_hdr_index.?); |
| 2852 | 2817 | symbol_ptr.value = shdr.sh_addr; |
| 2853 | 2818 | symbol_ptr.output_section_index = shndx; |
| ... | ... | @@ -2856,7 +2821,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 2856 | 2821 | // __rela_iplt_start, __rela_iplt_end |
| 2857 | 2822 | if (self.rela_dyn_section_index) |shndx| blk: { |
| 2858 | 2823 | if (self.base.options.link_mode != .Static or self.base.options.pie) break :blk; |
| 2859 | | const shdr = &self.sections.items(.shdr)[shndx]; |
| 2824 | const shdr = &self.shdrs.items[shndx]; |
| 2860 | 2825 | const end_addr = shdr.sh_addr + shdr.sh_size; |
| 2861 | 2826 | const start_addr = end_addr - self.calcNumIRelativeRelocs() * @sizeOf(elf.Elf64_Rela); |
| 2862 | 2827 | const start_sym = self.symbol(self.rela_iplt_start_index.?); |
| ... | ... | @@ -2870,7 +2835,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 2870 | 2835 | // _end |
| 2871 | 2836 | { |
| 2872 | 2837 | const end_symbol = self.symbol(self.end_index.?); |
| 2873 | | for (self.sections.items(.shdr), 0..) |shdr, shndx| { |
| 2838 | for (self.shdrs.items, 0..) |*shdr, shndx| { |
| 2874 | 2839 | if (shdr.sh_flags & elf.SHF_ALLOC != 0) { |
| 2875 | 2840 | end_symbol.value = shdr.sh_addr + shdr.sh_size; |
| 2876 | 2841 | end_symbol.output_section_index = @intCast(shndx); |
| ... | ... | @@ -2886,7 +2851,7 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void { |
| 2886 | 2851 | const name = start.name(self); |
| 2887 | 2852 | const stop = self.symbol(self.start_stop_indexes.items[index + 1]); |
| 2888 | 2853 | const shndx = self.sectionByName(name["__start_".len..]).?; |
| 2889 | | const shdr = self.sections.items(.shdr)[shndx]; |
| 2854 | const shdr = &self.shdrs.items[shndx]; |
| 2890 | 2855 | start.value = shdr.sh_addr; |
| 2891 | 2856 | start.output_section_index = shndx; |
| 2892 | 2857 | stop.value = shdr.sh_addr + shdr.sh_size; |
| ... | ... | @@ -2916,7 +2881,7 @@ fn updateSymtabSize(self: *Elf) !void { |
| 2916 | 2881 | sizes.nlocals += linker_defined.output_symtab_size.nlocals; |
| 2917 | 2882 | } |
| 2918 | 2883 | |
| 2919 | | const shdr = &self.sections.items(.shdr)[self.symtab_section_index.?]; |
| 2884 | const shdr = &self.shdrs.items[self.symtab_section_index.?]; |
| 2920 | 2885 | shdr.sh_info = sizes.nlocals + 1; |
| 2921 | 2886 | self.markDirty(self.symtab_section_index.?, null); |
| 2922 | 2887 | |
| ... | ... | @@ -2934,7 +2899,7 @@ fn updateSymtabSize(self: *Elf) !void { |
| 2934 | 2899 | |
| 2935 | 2900 | fn writeSymtab(self: *Elf) !void { |
| 2936 | 2901 | const gpa = self.base.allocator; |
| 2937 | | const shdr = &self.sections.items(.shdr)[self.symtab_section_index.?]; |
| 2902 | const shdr = &self.shdrs.items[self.symtab_section_index.?]; |
| 2938 | 2903 | const sym_size: u64 = switch (self.ptr_width) { |
| 2939 | 2904 | .p32 => @sizeOf(elf.Elf32_Sym), |
| 2940 | 2905 | .p64 => @sizeOf(elf.Elf64_Sym), |
| ... | ... | @@ -3031,7 +2996,7 @@ fn writeShdr(self: *Elf, index: usize) !void { |
| 3031 | 2996 | switch (self.ptr_width) { |
| 3032 | 2997 | .p32 => { |
| 3033 | 2998 | var shdr: [1]elf.Elf32_Shdr = undefined; |
| 3034 | | shdr[0] = shdrTo32(self.sections.items(.shdr)[index]); |
| 2999 | shdr[0] = shdrTo32(self.shdrs.items[index]); |
| 3035 | 3000 | if (foreign_endian) { |
| 3036 | 3001 | mem.byteSwapAllFields(elf.Elf32_Shdr, &shdr[0]); |
| 3037 | 3002 | } |
| ... | ... | @@ -3039,7 +3004,7 @@ fn writeShdr(self: *Elf, index: usize) !void { |
| 3039 | 3004 | return self.base.file.?.pwriteAll(mem.sliceAsBytes(&shdr), offset); |
| 3040 | 3005 | }, |
| 3041 | 3006 | .p64 => { |
| 3042 | | var shdr = [1]elf.Elf64_Shdr{self.sections.items(.shdr)[index]}; |
| 3007 | var shdr = [1]elf.Elf64_Shdr{self.shdrs.items[index]}; |
| 3043 | 3008 | if (foreign_endian) { |
| 3044 | 3009 | mem.byteSwapAllFields(elf.Elf64_Shdr, &shdr[0]); |
| 3045 | 3010 | } |
| ... | ... | @@ -3327,7 +3292,7 @@ pub fn defaultEntryAddress(self: Elf) u64 { |
| 3327 | 3292 | } |
| 3328 | 3293 | |
| 3329 | 3294 | pub fn sectionByName(self: *Elf, name: [:0]const u8) ?u16 { |
| 3330 | | for (self.sections.items(.shdr), 0..) |shdr, i| { |
| 3295 | for (self.shdrs.items, 0..) |*shdr, i| { |
| 3331 | 3296 | const this_name = self.shstrtab.getAssumeExists(shdr.sh_name); |
| 3332 | 3297 | if (mem.eql(u8, this_name, name)) return @as(u16, @intCast(i)); |
| 3333 | 3298 | } else return null; |
| ... | ... | @@ -3481,10 +3446,7 @@ const default_entry_addr = 0x8000000; |
| 3481 | 3446 | |
| 3482 | 3447 | pub const base_tag: link.File.Tag = .elf; |
| 3483 | 3448 | |
| 3484 | | const Section = struct { |
| 3485 | | shdr: elf.Elf64_Shdr, |
| 3486 | | phdr_index: u16, |
| 3487 | | |
| 3449 | const LastAtomAndFreeList = struct { |
| 3488 | 3450 | /// Index of the last allocated atom in this section. |
| 3489 | 3451 | last_atom_index: Atom.Index = 0, |
| 3490 | 3452 | |