authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-28 12:06:21+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-28 12:06:21+02:00
log22127a879234459dad65c73ffae4d5ec9da548c1
treea746af3dbaecd03566dc642f808a641d64346df7
parent42011a82498655ab3471aa92642878de7c7f024a

elf: pre-allocate TLS load segment and PT_TLS phdr


1 files changed, 33 insertions(+), 0 deletions(-)

src/link/Elf.zig+33
...@@ -43,6 +43,10 @@ phdr_load_ro_index: ?u16 = null,...@@ -43,6 +43,10 @@ phdr_load_ro_index: ?u16 = null,
43phdr_load_rw_index: ?u16 = null,43phdr_load_rw_index: ?u16 = null,
44/// The index into the program headers of a PT_LOAD program header with zerofill data.44/// The index into the program headers of a PT_LOAD program header with zerofill data.
45phdr_load_zerofill_index: ?u16 = null,45phdr_load_zerofill_index: ?u16 = null,
46/// The index into the program headers of the PT_TLS program header.
47phdr_tls_index: ?u16 = null,
48/// The index into the program headers of a PT_LOAD program header with TLS data.
49phdr_load_tls_index: ?u16 = null,
4650
47entry_addr: ?u64 = null,51entry_addr: ?u64 = null,
48page_size: u32,52page_size: u32,
...@@ -56,10 +60,13 @@ strtab: StringTable(.strtab) = .{},...@@ -56,10 +60,13 @@ strtab: StringTable(.strtab) = .{},
56/// Representation of the GOT table as committed to the file.60/// Representation of the GOT table as committed to the file.
57got: GotSection = .{},61got: GotSection = .{},
5862
63/// Tracked section headers
59text_section_index: ?u16 = null,64text_section_index: ?u16 = null,
60rodata_section_index: ?u16 = null,65rodata_section_index: ?u16 = null,
61data_section_index: ?u16 = null,66data_section_index: ?u16 = null,
62bss_section_index: ?u16 = null,67bss_section_index: ?u16 = null,
68tdata_section_index: ?u16 = null,
69tbss_section_index: ?u16 = null,
63eh_frame_section_index: ?u16 = null,70eh_frame_section_index: ?u16 = null,
64eh_frame_hdr_section_index: ?u16 = null,71eh_frame_hdr_section_index: ?u16 = null,
65dynamic_section_index: ?u16 = null,72dynamic_section_index: ?u16 = null,
...@@ -623,6 +630,32 @@ pub fn populateMissingMetadata(self: *Elf) !void {...@@ -623,6 +630,32 @@ pub fn populateMissingMetadata(self: *Elf) !void {
623 phdr.p_memsz = 1024;630 phdr.p_memsz = 1024;
624 }631 }
625632
633 if (!self.base.options.single_threaded) {
634 if (self.phdr_load_tls_index == null) {
635 const alignment = if (is_linux) self.page_size else @as(u16, ptr_size);
636 self.phdr_load_tls_index = try self.allocateSegment(.{
637 .size = 1024,
638 .alignment = alignment,
639 .flags = elf.PF_R | elf.PF_W,
640 });
641 }
642
643 if (self.phdr_tls_index == null) {
644 const phdr = &self.phdrs.items[self.phdr_load_tls_index.?];
645 try self.phdrs.append(gpa, .{
646 .p_type = elf.PT_TLS,
647 .p_offset = phdr.p_offset,
648 .p_vaddr = phdr.p_vaddr,
649 .p_paddr = phdr.p_paddr,
650 .p_filesz = phdr.p_filesz,
651 .p_memsz = phdr.p_memsz,
652 .p_align = ptr_size,
653 .p_flags = elf.PF_R,
654 });
655 self.phdr_table_dirty = true;
656 }
657 }
658
626 if (self.shstrtab_section_index == null) {659 if (self.shstrtab_section_index == null) {
627 assert(self.shstrtab.buffer.items.len == 0);660 assert(self.shstrtab.buffer.items.len == 0);
628 try self.shstrtab.buffer.append(gpa, 0); // need a 0 at position 0661 try self.shstrtab.buffer.append(gpa, 0); // need a 0 at position 0