| ... | @@ -51,6 +51,12 @@ debug_loclists_section_dirty: bool = false, | ... | @@ -51,6 +51,12 @@ debug_loclists_section_dirty: bool = false, |
| 51 | debug_rnglists_section_dirty: bool = false, | 51 | debug_rnglists_section_dirty: bool = false, |
| 52 | eh_frame_section_dirty: bool = false, | 52 | eh_frame_section_dirty: bool = false, |
| 53 | | 53 | |
| | 54 | text_index: ?Symbol.Index = null, |
| | 55 | data_relro_index: ?Symbol.Index = null, |
| | 56 | rodata_index: ?Symbol.Index = null, |
| | 57 | data_index: ?Symbol.Index = null, |
| | 58 | bss_index: ?Symbol.Index = null, |
| | 59 | eh_frame_index: ?Symbol.Index = null, |
| 54 | debug_info_index: ?Symbol.Index = null, | 60 | debug_info_index: ?Symbol.Index = null, |
| 55 | debug_abbrev_index: ?Symbol.Index = null, | 61 | debug_abbrev_index: ?Symbol.Index = null, |
| 56 | debug_aranges_index: ?Symbol.Index = null, | 62 | debug_aranges_index: ?Symbol.Index = null, |
| ... | @@ -59,11 +65,6 @@ debug_line_index: ?Symbol.Index = null, | ... | @@ -59,11 +65,6 @@ debug_line_index: ?Symbol.Index = null, |
| 59 | debug_line_str_index: ?Symbol.Index = null, | 65 | debug_line_str_index: ?Symbol.Index = null, |
| 60 | debug_loclists_index: ?Symbol.Index = null, | 66 | debug_loclists_index: ?Symbol.Index = null, |
| 61 | debug_rnglists_index: ?Symbol.Index = null, | 67 | debug_rnglists_index: ?Symbol.Index = null, |
| 62 | eh_frame_index: ?Symbol.Index = null, | | |
| 63 | bss_index: ?Symbol.Index = null, | | |
| 64 | data_index: ?Symbol.Index = null, | | |
| 65 | data_relro_index: ?Symbol.Index = null, | | |
| 66 | rodata_index: ?Symbol.Index = null, | | |
| 67 | | 68 | |
| 68 | pub const global_symbol_bit: u32 = 0x80000000; | 69 | pub const global_symbol_bit: u32 = 0x80000000; |
| 69 | pub const symbol_mask: u32 = 0x7fffffff; | 70 | pub const symbol_mask: u32 = 0x7fffffff; |
| ... | @@ -75,6 +76,7 @@ const InitOptions = struct { | ... | @@ -75,6 +76,7 @@ const InitOptions = struct { |
| 75 | }; | 76 | }; |
| 76 | | 77 | |
| 77 | pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { | 78 | pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| | 79 | _ = options; |
| 78 | const comp = elf_file.base.comp; | 80 | const comp = elf_file.base.comp; |
| 79 | const gpa = comp.gpa; | 81 | const gpa = comp.gpa; |
| 80 | const ptr_size = elf_file.ptrWidthBytes(); | 82 | const ptr_size = elf_file.ptrWidthBytes(); |
| ... | @@ -92,60 +94,6 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { | ... | @@ -92,60 +94,6 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 92 | esym.st_shndx = elf.SHN_ABS; | 94 | esym.st_shndx = elf.SHN_ABS; |
| 93 | } | 95 | } |
| 94 | | 96 | |
| 95 | const fillSection = struct { | | |
| 96 | fn fillSection(ef: *Elf, shdr: *elf.Elf64_Shdr, size: u64, phndx: ?u16) !void { | | |
| 97 | if (ef.base.isRelocatable()) { | | |
| 98 | const off = try ef.findFreeSpace(size, shdr.sh_addralign); | | |
| 99 | shdr.sh_offset = off; | | |
| 100 | shdr.sh_size = size; | | |
| 101 | } else { | | |
| 102 | const phdr = ef.phdrs.items[phndx.?]; | | |
| 103 | shdr.sh_addr = phdr.p_vaddr; | | |
| 104 | shdr.sh_offset = phdr.p_offset; | | |
| 105 | shdr.sh_size = phdr.p_memsz; | | |
| 106 | } | | |
| 107 | } | | |
| 108 | }.fillSection; | | |
| 109 | | | |
| 110 | comptime assert(Elf.number_of_zig_segments == 2); | | |
| 111 | | | |
| 112 | if (!elf_file.base.isRelocatable()) { | | |
| 113 | if (elf_file.phdr_zig_load_re_index == null) { | | |
| 114 | const filesz = options.program_code_size_hint; | | |
| 115 | const off = try elf_file.findFreeSpace(filesz, elf_file.page_size); | | |
| 116 | elf_file.phdr_zig_load_re_index = try elf_file.addPhdr(.{ | | |
| 117 | .type = elf.PT_LOAD, | | |
| 118 | .offset = off, | | |
| 119 | .filesz = filesz, | | |
| 120 | .addr = if (ptr_size >= 4) 0x4000000 else 0x4000, | | |
| 121 | .memsz = filesz, | | |
| 122 | .@"align" = elf_file.page_size, | | |
| 123 | .flags = elf.PF_X | elf.PF_R | elf.PF_W, | | |
| 124 | }); | | |
| 125 | } | | |
| 126 | } | | |
| 127 | | | |
| 128 | if (elf_file.zig_text_section_index == null) { | | |
| 129 | elf_file.zig_text_section_index = try elf_file.addSection(.{ | | |
| 130 | .name = try elf_file.insertShString(".text.zig"), | | |
| 131 | .type = elf.SHT_PROGBITS, | | |
| 132 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, | | |
| 133 | .addralign = 1, | | |
| 134 | .offset = std.math.maxInt(u64), | | |
| 135 | }); | | |
| 136 | const shdr = &elf_file.sections.items(.shdr)[elf_file.zig_text_section_index.?]; | | |
| 137 | const phndx = &elf_file.sections.items(.phndx)[elf_file.zig_text_section_index.?]; | | |
| 138 | try fillSection(elf_file, shdr, options.program_code_size_hint, elf_file.phdr_zig_load_re_index); | | |
| 139 | if (elf_file.base.isRelocatable()) { | | |
| 140 | _ = try elf_file.addRelaShdr( | | |
| 141 | try elf_file.insertShString(".rela.text.zig"), | | |
| 142 | elf_file.zig_text_section_index.?, | | |
| 143 | ); | | |
| 144 | } else { | | |
| 145 | phndx.* = elf_file.phdr_zig_load_re_index.?; | | |
| 146 | } | | |
| 147 | } | | |
| 148 | | | |
| 149 | switch (comp.config.debug_format) { | 97 | switch (comp.config.debug_format) { |
| 150 | .strip => {}, | 98 | .strip => {}, |
| 151 | .dwarf => |v| { | 99 | .dwarf => |v| { |
| ... | @@ -1196,7 +1144,19 @@ fn getNavShdrIndex( | ... | @@ -1196,7 +1144,19 @@ fn getNavShdrIndex( |
| 1196 | const ip = &zcu.intern_pool; | 1144 | const ip = &zcu.intern_pool; |
| 1197 | const any_non_single_threaded = elf_file.base.comp.config.any_non_single_threaded; | 1145 | const any_non_single_threaded = elf_file.base.comp.config.any_non_single_threaded; |
| 1198 | const nav_val = zcu.navValue(nav_index); | 1146 | const nav_val = zcu.navValue(nav_index); |
| 1199 | if (ip.isFunctionType(nav_val.typeOf(zcu).toIntern())) return elf_file.zig_text_section_index.?; | 1147 | if (ip.isFunctionType(nav_val.typeOf(zcu).toIntern())) { |
| | 1148 | if (self.text_index) |symbol_index| |
| | 1149 | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| | 1150 | const osec = try elf_file.addSection(.{ |
| | 1151 | .type = elf.SHT_PROGBITS, |
| | 1152 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| | 1153 | .name = try elf_file.insertShString(".text"), |
| | 1154 | .addralign = 1, |
| | 1155 | .offset = std.math.maxInt(u64), |
| | 1156 | }); |
| | 1157 | self.text_index = try self.addSectionSymbol(gpa, ".text", .@"1", osec); |
| | 1158 | return osec; |
| | 1159 | } |
| 1200 | const is_const, const is_threadlocal, const nav_init = switch (ip.indexToKey(nav_val.toIntern())) { | 1160 | const is_const, const is_threadlocal, const nav_init = switch (ip.indexToKey(nav_val.toIntern())) { |
| 1201 | .variable => |variable| .{ false, variable.is_threadlocal, variable.init }, | 1161 | .variable => |variable| .{ false, variable.is_threadlocal, variable.init }, |
| 1202 | .@"extern" => |@"extern"| .{ @"extern".is_const, @"extern".is_threadlocal, .none }, | 1162 | .@"extern" => |@"extern"| .{ @"extern".is_const, @"extern".is_threadlocal, .none }, |
| ... | @@ -1538,6 +1498,19 @@ pub fn updateFunc( | ... | @@ -1538,6 +1498,19 @@ pub fn updateFunc( |
| 1538 | self.symbol(sym_index).name(elf_file), | 1498 | self.symbol(sym_index).name(elf_file), |
| 1539 | }); | 1499 | }); |
| 1540 | defer gpa.free(name); | 1500 | defer gpa.free(name); |
| | 1501 | const osec = if (self.text_index) |sect_sym_index| |
| | 1502 | self.symbol(sect_sym_index).atom(elf_file).?.output_section_index |
| | 1503 | else osec: { |
| | 1504 | const osec = try elf_file.addSection(.{ |
| | 1505 | .name = try elf_file.insertShString(".text"), |
| | 1506 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| | 1507 | .type = elf.SHT_PROGBITS, |
| | 1508 | .addralign = 1, |
| | 1509 | .offset = std.math.maxInt(u64), |
| | 1510 | }); |
| | 1511 | self.text_index = try self.addSectionSymbol(gpa, ".text", .@"1", osec); |
| | 1512 | break :osec osec; |
| | 1513 | }; |
| 1541 | const name_off = try self.addString(gpa, name); | 1514 | const name_off = try self.addString(gpa, name); |
| 1542 | const tr_size = trampolineSize(elf_file.getTarget().cpu.arch); | 1515 | const tr_size = trampolineSize(elf_file.getTarget().cpu.arch); |
| 1543 | const tr_sym_index = try self.newSymbolWithAtom(gpa, name_off); | 1516 | const tr_sym_index = try self.newSymbolWithAtom(gpa, name_off); |
| ... | @@ -1549,7 +1522,7 @@ pub fn updateFunc( | ... | @@ -1549,7 +1522,7 @@ pub fn updateFunc( |
| 1549 | tr_atom_ptr.value = old_rva; | 1522 | tr_atom_ptr.value = old_rva; |
| 1550 | tr_atom_ptr.alive = true; | 1523 | tr_atom_ptr.alive = true; |
| 1551 | tr_atom_ptr.alignment = old_alignment; | 1524 | tr_atom_ptr.alignment = old_alignment; |
| 1552 | tr_atom_ptr.output_section_index = elf_file.zig_text_section_index.?; | 1525 | tr_atom_ptr.output_section_index = osec; |
| 1553 | tr_atom_ptr.size = tr_size; | 1526 | tr_atom_ptr.size = tr_size; |
| 1554 | const target_sym = self.symbol(sym_index); | 1527 | const target_sym = self.symbol(sym_index); |
| 1555 | target_sym.addExtra(.{ .trampoline = tr_sym_index }, elf_file); | 1528 | target_sym.addExtra(.{ .trampoline = tr_sym_index }, elf_file); |
| ... | @@ -1703,7 +1676,19 @@ fn updateLazySymbol( | ... | @@ -1703,7 +1676,19 @@ fn updateLazySymbol( |
| 1703 | }; | 1676 | }; |
| 1704 | | 1677 | |
| 1705 | const output_section_index = switch (sym.kind) { | 1678 | const output_section_index = switch (sym.kind) { |
| 1706 | .code => elf_file.zig_text_section_index.?, | 1679 | .code => if (self.text_index) |sym_index| |
| | 1680 | self.symbol(sym_index).atom(elf_file).?.output_section_index |
| | 1681 | else osec: { |
| | 1682 | const osec = try elf_file.addSection(.{ |
| | 1683 | .name = try elf_file.insertShString(".text"), |
| | 1684 | .type = elf.SHT_PROGBITS, |
| | 1685 | .addralign = 1, |
| | 1686 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| | 1687 | .offset = std.math.maxInt(u64), |
| | 1688 | }); |
| | 1689 | self.text_index = try self.addSectionSymbol(gpa, ".text", .@"1", osec); |
| | 1690 | break :osec osec; |
| | 1691 | }, |
| 1707 | .const_data => if (self.rodata_index) |sym_index| | 1692 | .const_data => if (self.rodata_index) |sym_index| |
| 1708 | self.symbol(sym_index).atom(elf_file).?.output_section_index | 1693 | self.symbol(sym_index).atom(elf_file).?.output_section_index |
| 1709 | else osec: { | 1694 | else osec: { |