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 @@...@@ -1,5 +1,4 @@
1const MachO = @This();1const MachO = @This();
2
3const std = @import("std");2const std = @import("std");
4const Allocator = std.mem.Allocator;3const Allocator = std.mem.Allocator;
5const assert = std.debug.assert;4const assert = std.debug.assert;
...@@ -375,6 +374,12 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {...@@ -375,6 +374,12 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
375 try self.writeSymbolTable();374 try self.writeSymbolTable();
376 try self.writeStringTable();375 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
378 {383 {
379 // TODO rework how we preallocate space for the entire __LINKEDIT segment instead of384 // TODO rework how we preallocate space for the entire __LINKEDIT segment instead of
380 // doing dynamic updates like this.385 // doing dynamic updates like this.
...@@ -947,7 +952,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {...@@ -947,7 +952,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
947 },952 },
948 };953 };
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;
951 assert(decl.link.macho.local_sym_index != 0); // Caller forgot to call allocateDeclIndexes()957 assert(decl.link.macho.local_sym_index != 0); // Caller forgot to call allocateDeclIndexes()
952 const symbol = &self.local_symbols.items[decl.link.macho.local_sym_index];958 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 {...@@ -997,7 +1003,6 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
997 const text_section = self.sections.items[self.text_section_index.?];1003 const text_section = self.sections.items[self.text_section_index.?];
998 const section_offset = symbol.n_value - text_section.addr;1004 const section_offset = symbol.n_value - text_section.addr;
999 const file_offset = text_section.offset + section_offset;1005 const file_offset = text_section.offset + section_offset;
1000
1001 try self.base.file.?.pwriteAll(code, file_offset);1006 try self.base.file.?.pwriteAll(code, file_offset);
10021007
1003 // Since we updated the vaddr and the size, each corresponding export symbol also needs to be updated.1008 // 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 {...@@ -1168,7 +1173,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
1168 .addr = text_segment.vmaddr + off,1173 .addr = text_segment.vmaddr + off,
1169 .size = file_size,1174 .size = file_size,
1170 .offset = off,1175 .offset = off,
1171 .@"align" = 12, // 2^12 = 40961176 .@"align" = 2, // 2^12 = 4096
1172 .reloff = 0,1177 .reloff = 0,
1173 .nreloc = 0,1178 .nreloc = 0,
1174 .flags = flags,1179 .flags = flags,
...@@ -1193,7 +1198,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {...@@ -1193,7 +1198,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
1193 .segname = makeStaticString("__DATA"),1198 .segname = makeStaticString("__DATA"),
1194 .vmaddr = text_segment.vmaddr + text_segment.vmsize,1199 .vmaddr = text_segment.vmaddr + text_segment.vmsize,
1195 .vmsize = 0,1200 .vmsize = 0,
1196 .fileoff = 0,1201 .fileoff = text_segment.fileoff + text_segment.filesize,
1197 .filesize = 0,1202 .filesize = 0,
1198 .maxprot = maxprot,1203 .maxprot = maxprot,
1199 .initprot = initprot,1204 .initprot = initprot,
...@@ -1210,7 +1215,8 @@ pub fn populateMissingMetadata(self: *MachO) !void {...@@ -1210,7 +1215,8 @@ pub fn populateMissingMetadata(self: *MachO) !void {
1210 data_segment.nsects += 1;1215 data_segment.nsects += 1;
12111216
1212 const file_size = @sizeOf(u64) * self.base.options.symbol_count_hint;1217 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
1215 log.debug("found __got section free space 0x{x} to 0x{x}\n", .{ off, off + file_size });1221 log.debug("found __got section free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
12161222