authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-25 22:02:59+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-26 11:50:09+01:00
log64eae8f39240109ce23c21e975c1d29191b4692a
tree4a5bbbb6e06459889640b2d5ee5bbd08bef2a3d3
parentc749b78df50160bedae40f90765442dd1f49de3a

stage2 macho: move PIE fixups to link file; fix tests


2 files changed, 24 insertions(+), 30 deletions(-)

src/codegen.zig+4-4
......@@ -2601,7 +2601,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
26012601 }).toU32());
26022602 // adr x28, #8
26032603 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), Instruction.adr(.x28, 8).toU32());
2604 try self.mod_fn.owner_decl.link.macho.addPieFixup(self.bin_file.allocator, .{
2604 try macho_file.pie_fixups.append(self.bin_file.allocator, .{
26052605 .address = addr,
26062606 .start = self.code.items.len,
26072607 .len = 4,
......@@ -2626,7 +2626,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
26262626 }).toU32());
26272627 // adr x28, #8
26282628 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), Instruction.adr(.x28, 8).toU32());
2629 try self.mod_fn.owner_decl.link.macho.addPieFixup(self.bin_file.allocator, .{
2629 try macho_file.pie_fixups.append(self.bin_file.allocator, .{
26302630 .address = addr,
26312631 .start = self.code.items.len,
26322632 .len = 4,
......@@ -2838,7 +2838,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
28382838 // later in the linker.
28392839 if (reg.id() == 0) { // %rax is special-cased
28402840 try self.code.ensureCapacity(self.code.items.len + 5);
2841 try self.mod_fn.owner_decl.link.macho.addPieFixup(self.bin_file.allocator, .{
2841 try macho_file.pie_fixups.append(self.bin_file.allocator, .{
28422842 .address = x,
28432843 .start = self.code.items.len,
28442844 .len = 5,
......@@ -2855,7 +2855,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
28552855 try self.code.ensureCapacity(self.code.items.len + 10);
28562856 // push %rax
28572857 self.code.appendSliceAssumeCapacity(&[_]u8{0x50});
2858 try self.mod_fn.owner_decl.link.macho.addPieFixup(self.bin_file.allocator, .{
2858 try macho_file.pie_fixups.append(self.bin_file.allocator, .{
28592859 .address = x,
28602860 .start = self.code.items.len,
28612861 .len = 5,
src/link/MachO.zig+20-26
......@@ -175,6 +175,22 @@ libsystem_cmd_dirty: bool = false,
175175text_block_free_list: std.ArrayListUnmanaged(*TextBlock) = .{},
176176/// Pointer to the last allocated text block
177177last_text_block: ?*TextBlock = null,
178/// A list of all PIE fixups required for this run of the linker.
179/// Warning, this is currently NOT thread-safe. See the TODO below.
180/// TODO Move this list inside `updateDecl` where it should be allocated
181/// prior to calling `generateSymbol`, and then immediately deallocated
182/// rather than sitting in the global scope.
183pie_fixups: std.ArrayListUnmanaged(PieFixup) = .{},
184
185pub const PieFixup = struct {
186 /// Target address we wanted to address in absolute terms.
187 address: u64,
188 /// Where in the byte stream we should perform the fixup.
189 start: usize,
190 /// The length of the byte stream. For x86_64, this will be
191 /// variable. For aarch64, it will be fixed at 4 bytes.
192 len: usize,
193};
178194
179195/// `alloc_num / alloc_den` is the factor of padding when allocating.
180196const alloc_num = 4;
......@@ -215,20 +231,10 @@ pub const TextBlock = struct {
215231 /// Unlike in Elf, we need to store the size of this symbol as part of
216232 /// the TextBlock since macho.nlist_64 lacks this information.
217233 size: u64,
218 /// List of PIE fixups in the code.
219 /// This is a table of all position-relative positions that will need fixups
220 /// after codegen when linker assigns addresses to GOT entries.
221 pie_fixups: std.ArrayListUnmanaged(PieFixup) = .{},
222234 /// Points to the previous and next neighbours
223235 prev: ?*TextBlock,
224236 next: ?*TextBlock,
225237
226 pub const PieFixup = struct {
227 address: u64,
228 start: usize,
229 len: usize,
230 };
231
232238 pub const empty = TextBlock{
233239 .local_sym_index = 0,
234240 .offset_table_index = undefined,
......@@ -237,14 +243,6 @@ pub const TextBlock = struct {
237243 .next = null,
238244 };
239245
240 pub fn addPieFixup(self: *TextBlock, alloc: *Allocator, fixup: PieFixup) !void {
241 return self.pie_fixups.append(alloc, fixup);
242 }
243
244 fn deinit(self: *TextBlock, alloc: *Allocator) void {
245 self.pie_fixups.deinit(alloc);
246 }
247
248246 /// Returns how much room there is to grow in virtual address space.
249247 /// File offset relocation happens transparently, so it is not included in
250248 /// this calculation.
......@@ -849,9 +847,7 @@ fn darwinArchString(arch: std.Target.Cpu.Arch) []const u8 {
849847}
850848
851849pub fn deinit(self: *MachO) void {
852 for (self.text_block_free_list.items) |tb| {
853 tb.deinit(self.base.allocator);
854 }
850 self.pie_fixups.deinit(self.base.allocator);
855851 self.text_block_free_list.deinit(self.base.allocator);
856852 self.offset_table.deinit(self.base.allocator);
857853 self.offset_table_free_list.deinit(self.base.allocator);
......@@ -894,9 +890,7 @@ fn freeTextBlock(self: *MachO, text_block: *TextBlock) void {
894890 if (!already_have_free_list_node and prev.freeListEligible(self.*)) {
895891 // The free list is heuristics, it doesn't have to be perfect, so we can ignore
896892 // the OOM here.
897 self.text_block_free_list.append(self.base.allocator, prev) catch {
898 prev.deinit(self.base.allocator);
899 };
893 self.text_block_free_list.append(self.base.allocator, prev) catch {};
900894 }
901895 } else {
902896 text_block.prev = null;
......@@ -1018,7 +1012,7 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
10181012
10191013 // Perform PIE fixups (if any)
10201014 const got_section = self.sections.items[self.got_section_index.?];
1021 while (decl.link.macho.pie_fixups.popOrNull()) |fixup| {
1015 while (self.pie_fixups.popOrNull()) |fixup| {
10221016 const target_addr = fixup.address;
10231017 const this_addr = symbol.n_value + fixup.start;
10241018 if (self.base.options.target.cpu.arch == .x86_64) {
......@@ -1761,7 +1755,7 @@ fn writeCodeSignature(self: *MachO) !void {
17611755}
17621756
17631757fn writeExportTrie(self: *MachO) !void {
1764 assert(self.global_symbols.items.len > 0);
1758 if (self.global_symbols.items.len == 0) return;
17651759
17661760 var trie: Trie = .{};
17671761 defer trie.deinit(self.base.allocator);