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
17101710 const old_base_addr = sect.addr;
17111711 sect.size = 0;
17121712 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));
17141715 sect.offset = new_offset;
17151716 sect.addr = seg.inner.vmaddr + sect.offset - seg.inner.fileoff;
17161717 log.debug(" (found new {s},{s} free space from 0x{x} to 0x{x})", .{
......@@ -2741,6 +2742,7 @@ fn setEntryPoint(self: *MachO) !void {
27412742 const ec = &self.load_commands.items[self.main_cmd_index.?].Main;
27422743 ec.entryoff = @intCast(u32, sym.n_value - seg.inner.vmaddr);
27432744 ec.stacksize = self.base.options.stack_size_override orelse 0;
2745 self.entry_addr = sym.n_value;
27442746 self.load_commands_dirty = true;
27452747}
27462748
......@@ -3234,15 +3236,7 @@ pub fn updateDeclExports(
32343236 n_type |= macho.N_PEXT;
32353237 n_desc |= macho.N_WEAK_DEF;
32363238 },
3237 .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 },
3239 .Strong => {},
32463240 .Weak => {
32473241 // Weak linkage is specified as part of n_desc field.
32483242 // Symbol's n_type is like for a symbol with strong linkage.
......@@ -4244,7 +4238,14 @@ fn relocateSymbolTable(self: *MachO) !void {
42444238 new_symoff + existing_size,
42454239 });
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 );
42484249 if (amt != existing_size) return error.InputOutput;
42494250 symtab.symoff = @intCast(u32, new_symoff);
42504251 self.strtab_needs_relocation = true;