authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-10-11 11:04:11+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-10-13 08:38:17+02:00
log951721343f0e04f39df0a049b93c774c3cde0741
treee8524ba979644983026f630215d5b04eb3900434
parentcf2aea7b41c42e171cc4a5516d77ed6f5df040c6

Reuse text blocks; enable all incremental tests


2 files changed, 55 insertions(+), 12 deletions(-)

src/link/MachO.zig+20-12
......@@ -845,12 +845,21 @@ pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {
845845 try self.local_symbols.ensureCapacity(self.base.allocator, self.local_symbols.items.len + 1);
846846 try self.offset_table.ensureCapacity(self.base.allocator, self.offset_table.items.len + 1);
847847
848 log.debug("allocating symbol index {} for {}\n", .{ self.local_symbols.items.len, decl.name });
849 decl.link.macho.local_sym_index = @intCast(u32, self.local_symbols.items.len);
850 _ = self.local_symbols.addOneAssumeCapacity();
848 if (self.local_symbol_free_list.popOrNull()) |i| {
849 log.debug("reusing symbol index {} for {}\n", .{i, decl.name});
850 decl.link.macho.local_sym_index = i;
851 } else {
852 log.debug("allocating symbol index {} for {}\n", .{ self.local_symbols.items.len, decl.name });
853 decl.link.macho.local_sym_index = @intCast(u32, self.local_symbols.items.len);
854 _ = self.local_symbols.addOneAssumeCapacity();
855 }
851856
852 decl.link.macho.offset_table_index = @intCast(u32, self.offset_table.items.len);
853 _ = self.offset_table.addOneAssumeCapacity();
857 if (self.offset_table_free_list.popOrNull()) |i| {
858 decl.link.macho.offset_table_index = i;
859 } else {
860 decl.link.macho.offset_table_index = @intCast(u32, self.offset_table.items.len);
861 _ = self.offset_table.addOneAssumeCapacity();
862 }
854863
855864 self.local_symbols.items[decl.link.macho.local_sym_index] = .{
856865 .n_strx = 0,
......@@ -1401,12 +1410,11 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64,
14011410 }
14021411 else if (self.last_text_block) |last| {
14031412 const last_symbol = self.local_symbols.items[last.local_sym_index];
1404 // TODO pad out with NOPs and reenable
1405 // const ideal_capacity = last.size * alloc_num / alloc_den;
1406 // const ideal_capacity_end_addr = last_symbol.n_value + ideal_capacity;
1407 // const new_start_addr = mem.alignForwardGeneric(u64, ideal_capacity_end_addr, alignment);
1408 const end_vaddr = last_symbol.n_value + last.size;
1409 const new_start_vaddr = mem.alignForwardGeneric(u64, end_vaddr, alignment);
1413 // TODO We should pad out the excess capacity with NOPs. For executables,
1414 // no padding seems to be OK, but it will probably not be for objects.
1415 const ideal_capacity = last.size * alloc_num / alloc_den;
1416 const ideal_capacity_end_vaddr = last_symbol.n_value + ideal_capacity;
1417 const new_start_vaddr = mem.alignForwardGeneric(u64, ideal_capacity_end_vaddr, alignment);
14101418 block_placement = last;
14111419 break :blk new_start_vaddr;
14121420 } else {
......@@ -1421,7 +1429,7 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64,
14211429 assert(needed_size <= text_capacity); // TODO must move the entire text section.
14221430
14231431 self.last_text_block = text_block;
1424 text_section.size = needed_size; // TODO temp until we pad out with NOPs
1432 text_section.size = needed_size;
14251433
14261434 self.cmd_table_dirty = true;
14271435 }
test/stage2/test.zig+35
......@@ -218,6 +218,41 @@ pub fn addCases(ctx: *TestContext) !void {
218218 ,
219219 "What is up? This is a longer message that will force the data to be relocated in virtual address space.\n",
220220 );
221 // Now we print it twice.
222 case.addCompareOutput(
223 \\export fn _start() noreturn {
224 \\ print();
225 \\ print();
226 \\
227 \\ exit();
228 \\}
229 \\
230 \\fn print() void {
231 \\ asm volatile ("syscall"
232 \\ :
233 \\ : [number] "{rax}" (0x2000004),
234 \\ [arg1] "{rdi}" (1),
235 \\ [arg2] "{rsi}" (@ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n")),
236 \\ [arg3] "{rdx}" (104)
237 \\ : "memory"
238 \\ );
239 \\ return;
240 \\}
241 \\
242 \\fn exit() noreturn {
243 \\ asm volatile ("syscall"
244 \\ :
245 \\ : [number] "{rax}" (0x2000001),
246 \\ [arg1] "{rdi}" (0)
247 \\ : "memory"
248 \\ );
249 \\ unreachable;
250 \\}
251 ,
252 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
253 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
254 \\
255 );
221256 }
222257
223258 {