authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-16 16:13:44+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-26 11:50:09+01:00
log7a407246ed503d93218bb0292150ffbf12dfb735
treed5dcac7f1f764bdc93c48fc578c875320c2278bb
parent6ac7e99dadfe14a0f4fb8a4fd19e9324747d9545

stage2 MachO: remove discontinuities between segments


1 files changed, 12 insertions(+), 6 deletions(-)

src/link/MachO.zig+12-6
......@@ -1,5 +1,4 @@
11const MachO = @This();
2
32const std = @import("std");
43const Allocator = std.mem.Allocator;
54const assert = std.debug.assert;
......@@ -375,6 +374,12 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
375374 try self.writeSymbolTable();
376375 try self.writeStringTable();
377376
377 {
378 // Seal __DATA,__got section size
379 const got = &self.sections.items[self.got_section_index.?];
380 got.size = @intCast(u32, self.offset_table.items.len * @sizeOf(u64));
381 }
382
378383 {
379384 // TODO rework how we preallocate space for the entire __LINKEDIT segment instead of
380385 // doing dynamic updates like this.
......@@ -947,7 +952,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
947952 },
948953 };
949954
950 const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
955 // const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
956 const required_alignment = 4;
951957 assert(decl.link.macho.local_sym_index != 0); // Caller forgot to call allocateDeclIndexes()
952958 const symbol = &self.local_symbols.items[decl.link.macho.local_sym_index];
953959
......@@ -997,7 +1003,6 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
9971003 const text_section = self.sections.items[self.text_section_index.?];
9981004 const section_offset = symbol.n_value - text_section.addr;
9991005 const file_offset = text_section.offset + section_offset;
1000
10011006 try self.base.file.?.pwriteAll(code, file_offset);
10021007
10031008 // Since we updated the vaddr and the size, each corresponding export symbol also needs to be updated.
......@@ -1168,7 +1173,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
11681173 .addr = text_segment.vmaddr + off,
11691174 .size = file_size,
11701175 .offset = off,
1171 .@"align" = 12, // 2^12 = 4096
1176 .@"align" = 2, // 2^12 = 4096
11721177 .reloff = 0,
11731178 .nreloc = 0,
11741179 .flags = flags,
......@@ -1193,7 +1198,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
11931198 .segname = makeStaticString("__DATA"),
11941199 .vmaddr = text_segment.vmaddr + text_segment.vmsize,
11951200 .vmsize = 0,
1196 .fileoff = 0,
1201 .fileoff = text_segment.fileoff + text_segment.filesize,
11971202 .filesize = 0,
11981203 .maxprot = maxprot,
11991204 .initprot = initprot,
......@@ -1210,7 +1215,8 @@ pub fn populateMissingMetadata(self: *MachO) !void {
12101215 data_segment.nsects += 1;
12111216
12121217 const file_size = @sizeOf(u64) * self.base.options.symbol_count_hint;
1213 const off = @intCast(u32, self.findFreeSpace(file_size, 0x1000));
1218 // const off = @intCast(u32, self.findFreeSpace(file_size, 0x1000));
1219 const off = @intCast(u32, data_segment.fileoff);
12141220
12151221 log.debug("found __got section free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
12161222