authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-14 10:05:01+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-14 11:02:43+02:00
log8eea5eddf773a8d1f9f883e71d00409e454ee0bd
treead94c3581a3e8a93d44b05d8abedb35820ee2152
parent826179bff40fdbd8c3b11138897fcfbb3367def8

macho: fix bug with symbol growth and realloc


2 files changed, 35 insertions(+), 2 deletions(-)

src/link/MachO.zig+7-1
......@@ -1193,7 +1193,9 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
11931193 const need_realloc = code.len > capacity or !mem.isAlignedGeneric(u64, symbol.n_value, required_alignment);
11941194 if (need_realloc) {
11951195 const vaddr = try self.growTextBlock(&decl.link.macho, code.len, required_alignment);
1196 log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl.name, symbol.n_value, vaddr });
1196
1197 log.debug("growing {s} and moving from 0x{x} to 0x{x}", .{ decl.name, symbol.n_value, vaddr });
1198
11971199 if (vaddr != symbol.n_value) {
11981200 log.debug(" (writing new offset table entry)", .{});
11991201 self.offset_table.items[decl.link.macho.offset_table_index] = .{
......@@ -1203,6 +1205,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
12031205 };
12041206 try self.writeOffsetTableEntry(decl.link.macho.offset_table_index);
12051207 }
1208
1209 symbol.n_value = vaddr;
12061210 } else if (code.len < decl.link.macho.size) {
12071211 self.shrinkTextBlock(&decl.link.macho, code.len);
12081212 }
......@@ -1219,7 +1223,9 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
12191223 const decl_name = mem.spanZ(decl.name);
12201224 const name_str_index = try self.makeString(decl_name);
12211225 const addr = try self.allocateTextBlock(&decl.link.macho, code.len, required_alignment);
1226
12221227 log.debug("allocated text block for {s} at 0x{x}", .{ decl_name, addr });
1228
12231229 errdefer self.freeTextBlock(&decl.link.macho);
12241230
12251231 symbol.* = .{
test/stage2/darwin.zig+28-1
......@@ -41,7 +41,34 @@ pub fn addCases(ctx: *TestContext) !void {
4141 "Hello, World!\n",
4242 );
4343
44 // Now change the message only
44 // Print it 4 times and force growth and realloc.
45 case.addCompareOutput(
46 \\extern "c" fn write(usize, usize, usize) usize;
47 \\extern "c" fn exit(usize) noreturn;
48 \\
49 \\export fn _start() noreturn {
50 \\ print();
51 \\ print();
52 \\ print();
53 \\ print();
54 \\
55 \\ exit(0);
56 \\}
57 \\
58 \\fn print() void {
59 \\ const msg = @ptrToInt("Hello, World!\n");
60 \\ const len = 14;
61 \\ _ = write(1, msg, len);
62 \\}
63 ,
64 \\Hello, World!
65 \\Hello, World!
66 \\Hello, World!
67 \\Hello, World!
68 \\
69 );
70
71 // Print it once, and change the message.
4572 case.addCompareOutput(
4673 \\extern "c" fn write(usize, usize, usize) usize;
4774 \\extern "c" fn exit(usize) noreturn;