authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-17 11:03:39+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-17 16:01:16+01:00
log3562edf13772bca66b4f04fc87be25b518393221
tree22f4c5b6a8e0f534c4db29f613117fa40fe37f49
parentb25cf7db0253f331f44b279fcbcdf71faa1afb92

macho: improve x86_64 tests; clean fixups on error

When codegen ends in failure, we need to manually clean up any fixups that may have been gathered during that `codegen.generateSymbol` call. Otherwise, we will end trapping.

2 files changed, 38 insertions(+), 75 deletions(-)

src/link/MachO.zig+3
......@@ -1162,6 +1162,9 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
11621162 .externally_managed => |x| x,
11631163 .appended => code_buffer.items,
11641164 .fail => |em| {
1165 // Clear any PIE fixups and stub fixups for this decl.
1166 self.pie_fixups.shrinkRetainingCapacity(0);
1167 self.stub_fixups.shrinkRetainingCapacity(0);
11651168 decl.analysis = .codegen_failure;
11661169 try module.failed_decls.put(module.gpa, decl, em);
11671170 return;
test/stage2/test.zig+35-75
......@@ -11,7 +11,7 @@ const linux_x64 = std.zig.CrossTarget{
1111 .os_tag = .linux,
1212};
1313
14const macosx_x64 = std.zig.CrossTarget{
14const macos_x64 = std.zig.CrossTarget{
1515 .cpu_arch = .x86_64,
1616 .os_tag = .macos,
1717};
......@@ -146,7 +146,7 @@ pub fn addCases(ctx: *TestContext) !void {
146146 }
147147
148148 {
149 var case = ctx.exe("hello world with updates", macosx_x64);
149 var case = ctx.exe("hello world with updates", macos_x64);
150150 case.addError("", &[_][]const u8{"error: no entry point found"});
151151
152152 // Incorrect return type
......@@ -157,97 +157,75 @@ pub fn addCases(ctx: *TestContext) !void {
157157
158158 // Regular old hello world
159159 case.addCompareOutput(
160 \\extern "c" fn write(usize, usize, usize) usize;
161 \\extern "c" fn exit(usize) noreturn;
162 \\
160163 \\export fn _start() noreturn {
161164 \\ print();
162165 \\
163 \\ exit();
166 \\ exit(0);
164167 \\}
165168 \\
166169 \\fn print() void {
167 \\ asm volatile ("syscall"
168 \\ :
169 \\ : [number] "{rax}" (0x2000004),
170 \\ [arg1] "{rdi}" (1),
171 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
172 \\ [arg3] "{rdx}" (14)
173 \\ : "memory"
174 \\ );
175 \\ return;
170 \\ const msg = @ptrToInt("Hello, World!\n");
171 \\ const len = 14;
172 \\ const nwritten = write(1, msg, len);
173 \\ assert(nwritten == len);
176174 \\}
177175 \\
178 \\fn exit() noreturn {
179 \\ asm volatile ("syscall"
180 \\ :
181 \\ : [number] "{rax}" (0x2000001),
182 \\ [arg1] "{rdi}" (0)
183 \\ : "memory"
184 \\ );
185 \\ unreachable;
176 \\fn assert(ok: bool) void {
177 \\ if (!ok) unreachable; // assertion failure
186178 \\}
187179 ,
188180 "Hello, World!\n",
189181 );
182
190183 // Now change the message only
191184 case.addCompareOutput(
185 \\extern "c" fn write(usize, usize, usize) usize;
186 \\extern "c" fn exit(usize) noreturn;
187 \\
192188 \\export fn _start() noreturn {
193189 \\ print();
194190 \\
195 \\ exit();
191 \\ exit(0);
196192 \\}
197193 \\
198194 \\fn print() void {
199 \\ asm volatile ("syscall"
200 \\ :
201 \\ : [number] "{rax}" (0x2000004),
202 \\ [arg1] "{rdi}" (1),
203 \\ [arg2] "{rsi}" (@ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n")),
204 \\ [arg3] "{rdx}" (104)
205 \\ : "memory"
206 \\ );
207 \\ return;
195 \\ const msg = @ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n");
196 \\ const len = 104;
197 \\ const nwritten = write(1, msg, len);
198 \\ assert(nwritten == len);
208199 \\}
209200 \\
210 \\fn exit() noreturn {
211 \\ asm volatile ("syscall"
212 \\ :
213 \\ : [number] "{rax}" (0x2000001),
214 \\ [arg1] "{rdi}" (0)
215 \\ : "memory"
216 \\ );
217 \\ unreachable;
201 \\fn assert(ok: bool) void {
202 \\ if (!ok) unreachable; // assertion failure
218203 \\}
219204 ,
220205 "What is up? This is a longer message that will force the data to be relocated in virtual address space.\n",
221206 );
207
222208 // Now we print it twice.
223209 case.addCompareOutput(
210 \\extern "c" fn write(usize, usize, usize) usize;
211 \\extern "c" fn exit(usize) noreturn;
212 \\
224213 \\export fn _start() noreturn {
225214 \\ print();
226215 \\ print();
227216 \\
228 \\ exit();
217 \\ exit(0);
229218 \\}
230219 \\
231220 \\fn print() void {
232 \\ asm volatile ("syscall"
233 \\ :
234 \\ : [number] "{rax}" (0x2000004),
235 \\ [arg1] "{rdi}" (1),
236 \\ [arg2] "{rsi}" (@ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n")),
237 \\ [arg3] "{rdx}" (104)
238 \\ : "memory"
239 \\ );
240 \\ return;
221 \\ const msg = @ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n");
222 \\ const len = 104;
223 \\ const nwritten = write(1, msg, len);
224 \\ assert(nwritten == len);
241225 \\}
242226 \\
243 \\fn exit() noreturn {
244 \\ asm volatile ("syscall"
245 \\ :
246 \\ : [number] "{rax}" (0x2000001),
247 \\ [arg1] "{rdi}" (0)
248 \\ : "memory"
249 \\ );
250 \\ unreachable;
227 \\fn assert(ok: bool) void {
228 \\ if (!ok) unreachable; // assertion failure
251229 \\}
252230 ,
253231 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
......@@ -1530,25 +1508,7 @@ pub fn addCases(ctx: *TestContext) !void {
15301508 }
15311509
15321510 {
1533 var case = ctx.exe("hello world linked to libc", macosx_x64);
1534
1535 // TODO rewrite this test once we handle more int conversions and return args.
1536 case.addCompareOutput(
1537 \\extern "c" fn write(usize, usize, usize) void;
1538 \\extern "c" fn exit(usize) noreturn;
1539 \\
1540 \\export fn _start() noreturn {
1541 \\ write(1, @ptrToInt("Hello,"), 6);
1542 \\ write(1, @ptrToInt(" World!\n,"), 8);
1543 \\ exit(0);
1544 \\}
1545 ,
1546 "Hello, World!\n",
1547 );
1548 }
1549
1550 {
1551 var case = ctx.exe("only libc exit", macosx_x64);
1511 var case = ctx.exe("only libc exit", macos_x64);
15521512
15531513 // This test case covers an infrequent scenarion where the string table *may* be relocated
15541514 // into the position preceeding the symbol table which results in a dyld error.