authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-09-03 11:47:58+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-09-03 11:47:58+02:00
log7536a2f8cfdd66fc3b277514ec28ccbedfe83fc1
tree8c458b0af035a8da1281ab6b15131d32ecf848d3
parenta783f3a36952449f11b7e763490bdb4076bd62c0

macho: minor fixes to allow the linker to output malformed stage1


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

src/link/MachO.zig+12-11
...@@ -1710,7 +1710,8 @@ pub fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64...@@ -1710,7 +1710,8 @@ pub fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64
1710 const old_base_addr = sect.addr;1710 const old_base_addr = sect.addr;
1711 sect.size = 0;1711 sect.size = 0;
1712 const padding: ?u64 = if (match.seg == self.text_segment_cmd_index.?) self.header_pad else null;1712 const padding: ?u64 = if (match.seg == self.text_segment_cmd_index.?) self.header_pad else null;
1713 const new_offset = @intCast(u32, seg.findFreeSpace(needed_size, atom.alignment, padding));1713 const atom_alignment = try math.powi(u64, 2, atom.alignment);
1714 const new_offset = @intCast(u32, seg.findFreeSpace(needed_size, atom_alignment, padding));
1714 sect.offset = new_offset;1715 sect.offset = new_offset;
1715 sect.addr = seg.inner.vmaddr + sect.offset - seg.inner.fileoff;1716 sect.addr = seg.inner.vmaddr + sect.offset - seg.inner.fileoff;
1716 log.debug(" (found new {s},{s} free space from 0x{x} to 0x{x})", .{1717 log.debug(" (found new {s},{s} free space from 0x{x} to 0x{x})", .{
...@@ -2741,6 +2742,7 @@ fn setEntryPoint(self: *MachO) !void {...@@ -2741,6 +2742,7 @@ fn setEntryPoint(self: *MachO) !void {
2741 const ec = &self.load_commands.items[self.main_cmd_index.?].Main;2742 const ec = &self.load_commands.items[self.main_cmd_index.?].Main;
2742 ec.entryoff = @intCast(u32, sym.n_value - seg.inner.vmaddr);2743 ec.entryoff = @intCast(u32, sym.n_value - seg.inner.vmaddr);
2743 ec.stacksize = self.base.options.stack_size_override orelse 0;2744 ec.stacksize = self.base.options.stack_size_override orelse 0;
2745 self.entry_addr = sym.n_value;
2744 self.load_commands_dirty = true;2746 self.load_commands_dirty = true;
2745}2747}
27462748
...@@ -3234,15 +3236,7 @@ pub fn updateDeclExports(...@@ -3234,15 +3236,7 @@ pub fn updateDeclExports(
3234 n_type |= macho.N_PEXT;3236 n_type |= macho.N_PEXT;
3235 n_desc |= macho.N_WEAK_DEF;3237 n_desc |= macho.N_WEAK_DEF;
3236 },3238 },
3237 .Strong => {3239 .Strong => {},
3238 // Check if the export is _main, and note if os.
3239 // Otherwise, don't do anything since we already have all the flags
3240 // set that we need for global (strong) linkage.
3241 // n_type == N_SECT | N_EXT
3242 if (mem.eql(u8, exp_name, "_main")) {
3243 self.entry_addr = decl_sym.n_value;
3244 }
3245 },
3246 .Weak => {3240 .Weak => {
3247 // Weak linkage is specified as part of n_desc field.3241 // Weak linkage is specified as part of n_desc field.
3248 // Symbol's n_type is like for a symbol with strong linkage.3242 // Symbol's n_type is like for a symbol with strong linkage.
...@@ -4244,7 +4238,14 @@ fn relocateSymbolTable(self: *MachO) !void {...@@ -4244,7 +4238,14 @@ fn relocateSymbolTable(self: *MachO) !void {
4244 new_symoff + existing_size,4238 new_symoff + existing_size,
4245 });4239 });
42464240
4247 const amt = try self.base.file.?.copyRangeAll(symtab.symoff, self.base.file.?, new_symoff, existing_size);4241 // TODO copyRangeAll doesn't seem to extend the file beyond its allocated size
4242 try self.base.file.?.pwriteAll(&[_]u8{0}, new_symoff + existing_size - 1);
4243 const amt = try self.base.file.?.copyRangeAll(
4244 symtab.symoff,
4245 self.base.file.?,
4246 new_symoff,
4247 existing_size,
4248 );
4248 if (amt != existing_size) return error.InputOutput;4249 if (amt != existing_size) return error.InputOutput;
4249 symtab.symoff = @intCast(u32, new_symoff);4250 symtab.symoff = @intCast(u32, new_symoff);
4250 self.strtab_needs_relocation = true;4251 self.strtab_needs_relocation = true;