authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-20 17:28:31+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-01-20 17:28:31+01:00
log58344e0017a1e866ee9744b4862c57dbf39284b6
tree8d86d17bd12371a6369dee44ee867616a4e4ef5d
parent8098b3f84cf24878e3388e056f60aba69033e0f6
parenta26ab9afeeeca405118fb3411dce0810ca723b5f
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #7829 from kubkon/macho-safer

stage2 macho: make int casts fallible where necessary

6 files changed, 208 insertions(+), 285 deletions(-)

src/link/MachO.zig+78-47
......@@ -120,6 +120,7 @@ stub_helper_stubs_start_off: ?u64 = null,
120120
121121/// Table of symbol names aka the string table.
122122string_table: std.ArrayListUnmanaged(u8) = .{},
123string_table_directory: std.StringHashMapUnmanaged(u32) = .{},
123124
124125/// Table of trampolines to the actual symbols in __text section.
125126offset_table: std.ArrayListUnmanaged(u64) = .{},
......@@ -142,11 +143,11 @@ string_table_needs_relocation: bool = false,
142143/// or removed from the freelist.
143144///
144145/// A text block has surplus capacity when its overcapacity value is greater than
145/// minimum_text_block_size * alloc_num / alloc_den. That is, when it has so
146/// padToIdeal(minimum_text_block_size). That is, when it has so
146147/// much extra capacity, that we could fit a small new symbol in it, itself with
147148/// ideal_capacity or more.
148149///
149/// Ideal capacity is defined by size * alloc_num / alloc_den.
150/// Ideal capacity is defined by size + (size / ideal_factor).
150151///
151152/// Overcapacity is measured by actual_capacity - ideal_capacity. Note that
152153/// overcapacity can be negative. A simple way to have negative overcapacity is to
......@@ -191,9 +192,9 @@ pub const StubFixup = struct {
191192 len: usize,
192193};
193194
194/// `alloc_num / alloc_den` is the factor of padding when allocating.
195pub const alloc_num = 4;
196pub const alloc_den = 3;
195/// When allocating, the ideal_capacity is calculated by
196/// actual_capacity + (actual_capacity / ideal_factor)
197const ideal_factor = 2;
197198
198199/// Default path to dyld
199200/// TODO instead of hardcoding it, we should probably look through some env vars and search paths
......@@ -213,7 +214,7 @@ const LIB_SYSTEM_PATH: [*:0]const u8 = DEFAULT_LIB_SEARCH_PATH ++ "/libSystem.B.
213214/// it as a possible place to put new symbols, it must have enough room for this many bytes
214215/// (plus extra for reserved capacity).
215216const minimum_text_block_size = 64;
216const min_text_capacity = minimum_text_block_size * alloc_num / alloc_den;
217const min_text_capacity = padToIdeal(minimum_text_block_size);
217218
218219pub const TextBlock = struct {
219220 /// Each decl always gets a local symbol with the fully qualified name.
......@@ -276,7 +277,7 @@ pub const TextBlock = struct {
276277 const self_sym = macho_file.local_symbols.items[self.local_sym_index];
277278 const next_sym = macho_file.local_symbols.items[next.local_sym_index];
278279 const cap = next_sym.n_value - self_sym.n_value;
279 const ideal_cap = self.size * alloc_num / alloc_den;
280 const ideal_cap = padToIdeal(self.size);
280281 if (cap <= ideal_cap) return false;
281282 const surplus = cap - ideal_cap;
282283 return surplus >= min_text_capacity;
......@@ -872,7 +873,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
872873 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
873874 const text_section = text_segment.sections.items[self.text_section_index.?];
874875 const after_last_cmd_offset = self.header.?.sizeofcmds + @sizeOf(macho.mach_header_64);
875 const needed_size = @sizeOf(macho.linkedit_data_command) * alloc_num / alloc_den;
876 const needed_size = padToIdeal(@sizeOf(macho.linkedit_data_command));
876877
877878 if (needed_size + after_last_cmd_offset > text_section.offset) {
878879 log.err("Unable to extend padding between the end of load commands and start of __text section.", .{});
......@@ -942,7 +943,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
942943 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
943944 const text_section = text_segment.sections.items[self.text_section_index.?];
944945 const after_last_cmd_offset = self.header.?.sizeofcmds + @sizeOf(macho.mach_header_64);
945 const needed_size = @sizeOf(macho.linkedit_data_command) * alloc_num / alloc_den;
946 const needed_size = padToIdeal(@sizeOf(macho.linkedit_data_command));
946947
947948 if (needed_size + after_last_cmd_offset > text_section.offset) {
948949 log.err("Unable to extend padding between the end of load commands and start of __text section.", .{});
......@@ -1023,6 +1024,13 @@ pub fn deinit(self: *MachO) void {
10231024 self.text_block_free_list.deinit(self.base.allocator);
10241025 self.offset_table.deinit(self.base.allocator);
10251026 self.offset_table_free_list.deinit(self.base.allocator);
1027 {
1028 var it = self.string_table_directory.iterator();
1029 while (it.next()) |entry| {
1030 self.base.allocator.free(entry.key);
1031 }
1032 }
1033 self.string_table_directory.deinit(self.base.allocator);
10261034 self.string_table.deinit(self.base.allocator);
10271035 self.global_symbols.deinit(self.base.allocator);
10281036 self.global_symbol_free_list.deinit(self.base.allocator);
......@@ -1229,14 +1237,16 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
12291237 const this_addr = symbol.n_value + fixup.start;
12301238 switch (self.base.options.target.cpu.arch) {
12311239 .x86_64 => {
1232 const displacement = @intCast(u32, target_addr - this_addr - fixup.len);
1240 assert(target_addr >= this_addr + fixup.len);
1241 const displacement = try math.cast(u32, target_addr - this_addr - fixup.len);
12331242 var placeholder = code_buffer.items[fixup.start + fixup.len - @sizeOf(u32) ..][0..@sizeOf(u32)];
12341243 mem.writeIntSliceLittle(u32, placeholder, displacement);
12351244 },
12361245 .aarch64 => {
1237 const displacement = @intCast(u27, target_addr - this_addr);
1246 assert(target_addr >= this_addr);
1247 const displacement = try math.cast(u27, target_addr - this_addr);
12381248 var placeholder = code_buffer.items[fixup.start..][0..fixup.len];
1239 mem.writeIntSliceLittle(u32, placeholder, aarch64.Instruction.b(@intCast(i28, displacement)).toU32());
1249 mem.writeIntSliceLittle(u32, placeholder, aarch64.Instruction.b(@as(i28, displacement)).toU32());
12401250 },
12411251 else => unreachable, // unsupported target architecture
12421252 }
......@@ -1249,14 +1259,16 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
12491259 const text_addr = symbol.n_value + fixup.start;
12501260 switch (self.base.options.target.cpu.arch) {
12511261 .x86_64 => {
1252 const displacement = @intCast(u32, stub_addr - text_addr - fixup.len);
1262 assert(stub_addr >= text_addr + fixup.len);
1263 const displacement = try math.cast(u32, stub_addr - text_addr - fixup.len);
12531264 var placeholder = code_buffer.items[fixup.start + fixup.len - @sizeOf(u32) ..][0..@sizeOf(u32)];
12541265 mem.writeIntSliceLittle(u32, placeholder, displacement);
12551266 },
12561267 .aarch64 => {
1257 const displacement = @intCast(u32, stub_addr - text_addr);
1268 assert(stub_addr >= text_addr);
1269 const displacement = try math.cast(i28, stub_addr - text_addr);
12581270 var placeholder = code_buffer.items[fixup.start..][0..fixup.len];
1259 mem.writeIntSliceLittle(u32, placeholder, aarch64.Instruction.bl(@intCast(i28, displacement)).toU32());
1271 mem.writeIntSliceLittle(u32, placeholder, aarch64.Instruction.bl(displacement).toU32());
12601272 },
12611273 else => unreachable, // unsupported target architecture
12621274 }
......@@ -1479,7 +1491,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
14791491 const program_code_size_hint = self.base.options.program_code_size_hint;
14801492 const offset_table_size_hint = @sizeOf(u64) * self.base.options.symbol_count_hint;
14811493 const ideal_size = self.header_pad + program_code_size_hint + 3 * offset_table_size_hint;
1482 const needed_size = mem.alignForwardGeneric(u64, satMul(ideal_size, alloc_num) / alloc_den, self.page_size);
1494 const needed_size = mem.alignForwardGeneric(u64, padToIdeal(ideal_size), self.page_size);
14831495
14841496 log.debug("found __TEXT segment free space 0x{x} to 0x{x}", .{ 0, needed_size });
14851497
......@@ -1644,7 +1656,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
16441656 const address_and_offset = self.nextSegmentAddressAndOffset();
16451657
16461658 const ideal_size = @sizeOf(u64) * self.base.options.symbol_count_hint;
1647 const needed_size = mem.alignForwardGeneric(u64, satMul(ideal_size, alloc_num) / alloc_den, self.page_size);
1659 const needed_size = mem.alignForwardGeneric(u64, padToIdeal(ideal_size), self.page_size);
16481660
16491661 log.debug("found __DATA_CONST segment free space 0x{x} to 0x{x}", .{ address_and_offset.offset, address_and_offset.offset + needed_size });
16501662
......@@ -1701,7 +1713,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
17011713 const address_and_offset = self.nextSegmentAddressAndOffset();
17021714
17031715 const ideal_size = 2 * @sizeOf(u64) * self.base.options.symbol_count_hint;
1704 const needed_size = mem.alignForwardGeneric(u64, satMul(ideal_size, alloc_num) / alloc_den, self.page_size);
1716 const needed_size = mem.alignForwardGeneric(u64, padToIdeal(ideal_size), self.page_size);
17051717
17061718 log.debug("found __DATA segment free space 0x{x} to 0x{x}", .{ address_and_offset.offset, address_and_offset.offset + needed_size });
17071719
......@@ -2074,7 +2086,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
20742086 code[1] = 0x8d;
20752087 code[2] = 0x1d;
20762088 {
2077 const displacement = @intCast(u32, data.addr - stub_helper.addr - 7);
2089 const displacement = try math.cast(u32, data.addr - stub_helper.addr - 7);
20782090 mem.writeIntLittle(u32, code[3..7], displacement);
20792091 }
20802092 // push %r11
......@@ -2084,7 +2096,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
20842096 code[9] = 0xff;
20852097 code[10] = 0x25;
20862098 {
2087 const displacement = @intCast(u32, got.addr - stub_helper.addr - code_size);
2099 const displacement = try math.cast(u32, got.addr - stub_helper.addr - code_size);
20882100 mem.writeIntLittle(u32, code[11..], displacement);
20892101 }
20902102 self.stub_helper_stubs_start_off = stub_helper.offset + code_size;
......@@ -2093,8 +2105,8 @@ pub fn populateMissingMetadata(self: *MachO) !void {
20932105 .aarch64 => {
20942106 var code: [4 * @sizeOf(u32)]u8 = undefined;
20952107 {
2096 const displacement = data.addr - stub_helper.addr;
2097 mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.adr(.x17, @intCast(i21, displacement)).toU32());
2108 const displacement = try math.cast(i21, data.addr - stub_helper.addr);
2109 mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.adr(.x17, displacement).toU32());
20982110 }
20992111 mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.stp(
21002112 .x16,
......@@ -2103,9 +2115,10 @@ pub fn populateMissingMetadata(self: *MachO) !void {
21032115 aarch64.Instruction.LoadStorePairOffset.pre_index(-16),
21042116 ).toU32());
21052117 {
2106 const displacement = got.addr - stub_helper.addr - 2 * @sizeOf(u32);
2118 const displacement = try math.divExact(u64, got.addr - stub_helper.addr - 2 * @sizeOf(u32), 4);
2119 const literal = try math.cast(u19, displacement);
21072120 mem.writeIntLittle(u32, code[8..12], aarch64.Instruction.ldr(.x16, .{
2108 .literal = @intCast(u19, displacement / 4),
2121 .literal = literal,
21092122 }).toU32());
21102123 }
21112124 mem.writeIntLittle(u32, code[12..16], aarch64.Instruction.br(.x16).toU32());
......@@ -2120,7 +2133,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
21202133fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {
21212134 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
21222135 const text_section = &text_segment.sections.items[self.text_section_index.?];
2123 const new_block_ideal_capacity = new_block_size * alloc_num / alloc_den;
2136 const new_block_ideal_capacity = padToIdeal(new_block_size);
21242137
21252138 // We use these to indicate our intention to update metadata, placing the new block,
21262139 // and possibly removing a free list node.
......@@ -2140,7 +2153,7 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64,
21402153 // Is it enough that we could fit this new text block?
21412154 const sym = self.local_symbols.items[big_block.local_sym_index];
21422155 const capacity = big_block.capacity(self.*);
2143 const ideal_capacity = capacity * alloc_num / alloc_den;
2156 const ideal_capacity = padToIdeal(capacity);
21442157 const ideal_capacity_end_vaddr = sym.n_value + ideal_capacity;
21452158 const capacity_end_vaddr = sym.n_value + capacity;
21462159 const new_start_vaddr_unaligned = capacity_end_vaddr - new_block_ideal_capacity;
......@@ -2172,7 +2185,7 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64,
21722185 const last_symbol = self.local_symbols.items[last.local_sym_index];
21732186 // TODO We should pad out the excess capacity with NOPs. For executables,
21742187 // no padding seems to be OK, but it will probably not be for objects.
2175 const ideal_capacity = last.size * alloc_num / alloc_den;
2188 const ideal_capacity = padToIdeal(last.size);
21762189 const ideal_capacity_end_vaddr = last_symbol.n_value + ideal_capacity;
21772190 const new_start_vaddr = mem.alignForwardGeneric(u64, ideal_capacity_end_vaddr, alignment);
21782191 block_placement = last;
......@@ -2230,14 +2243,26 @@ pub fn makeStaticString(comptime bytes: []const u8) [16]u8 {
22302243}
22312244
22322245fn makeString(self: *MachO, bytes: []const u8) !u32 {
2246 if (self.string_table_directory.get(bytes)) |offset| {
2247 log.debug("reusing '{s}' from string table at offset 0x{x}", .{ bytes, offset });
2248 return offset;
2249 }
2250
22332251 try self.string_table.ensureCapacity(self.base.allocator, self.string_table.items.len + bytes.len + 1);
22342252 const offset = @intCast(u32, self.string_table.items.len);
2235 log.debug("writing '{s}' into the string table at offset 0x{x}", .{ bytes, offset });
2253 log.debug("writing new string '{s}' into string table at offset 0x{x}", .{ bytes, offset });
22362254 self.string_table.appendSliceAssumeCapacity(bytes);
22372255 self.string_table.appendAssumeCapacity(0);
2256 try self.string_table_directory.putNoClobber(
2257 self.base.allocator,
2258 try self.base.allocator.dupe(u8, bytes),
2259 offset,
2260 );
2261
22382262 self.string_table_dirty = true;
22392263 if (self.d_sym) |*ds|
22402264 ds.string_table_dirty = true;
2265
22412266 return offset;
22422267}
22432268
......@@ -2335,7 +2360,7 @@ fn allocatedSizeLinkedit(self: *MachO, start: u64) u64 {
23352360}
23362361
23372362inline fn checkForCollision(start: u64, end: u64, off: u64, size: u64) ?u64 {
2338 const increased_size = satMul(size, alloc_num) / alloc_den;
2363 const increased_size = padToIdeal(size);
23392364 const test_end = off + increased_size;
23402365 if (end > off and start < test_end) {
23412366 return test_end;
......@@ -2344,7 +2369,7 @@ inline fn checkForCollision(start: u64, end: u64, off: u64, size: u64) ?u64 {
23442369}
23452370
23462371fn detectAllocCollisionLinkedit(self: *MachO, start: u64, size: u64) ?u64 {
2347 const end = start + satMul(size, alloc_num) / alloc_den;
2372 const end = start + padToIdeal(size);
23482373
23492374 // __LINKEDIT is a weird segment where sections get their own load commands so we
23502375 // special-case it.
......@@ -2425,12 +2450,6 @@ fn findFreeSpaceLinkedit(self: *MachO, object_size: u64, min_alignment: u16, sta
24252450 return st;
24262451}
24272452
2428/// Saturating multiplication
2429pub fn satMul(a: anytype, b: anytype) @TypeOf(a, b) {
2430 const T = @TypeOf(a, b);
2431 return std.math.mul(T, a, b) catch std.math.maxInt(T);
2432}
2433
24342453fn writeOffsetTableEntry(self: *MachO, index: usize) !void {
24352454 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
24362455 const sect = &text_segment.sections.items[self.got_section_index.?];
......@@ -2445,8 +2464,8 @@ fn writeOffsetTableEntry(self: *MachO, index: usize) !void {
24452464 var code: [8]u8 = undefined;
24462465 switch (self.base.options.target.cpu.arch) {
24472466 .x86_64 => {
2448 const pos_symbol_off = @intCast(u31, vmaddr - self.offset_table.items[index] + 7);
2449 const symbol_off = @bitCast(u32, @intCast(i32, pos_symbol_off) * -1);
2467 const pos_symbol_off = try math.cast(u31, vmaddr - self.offset_table.items[index] + 7);
2468 const symbol_off = @bitCast(u32, @as(i32, pos_symbol_off) * -1);
24502469 // lea %rax, [rip - disp]
24512470 code[0] = 0x48;
24522471 code[1] = 0x8D;
......@@ -2456,8 +2475,8 @@ fn writeOffsetTableEntry(self: *MachO, index: usize) !void {
24562475 code[7] = 0xC3;
24572476 },
24582477 .aarch64 => {
2459 const pos_symbol_off = @intCast(u20, vmaddr - self.offset_table.items[index]);
2460 const symbol_off = @intCast(i21, pos_symbol_off) * -1;
2478 const pos_symbol_off = try math.cast(u20, vmaddr - self.offset_table.items[index]);
2479 const symbol_off = @as(i21, pos_symbol_off) * -1;
24612480 // adr x0, #-disp
24622481 mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.adr(.x0, symbol_off).toU32());
24632482 // ret x28
......@@ -2503,16 +2522,19 @@ fn writeStub(self: *MachO, index: u32) !void {
25032522 defer self.base.allocator.free(code);
25042523 switch (self.base.options.target.cpu.arch) {
25052524 .x86_64 => {
2506 const displacement = @intCast(u32, la_ptr_addr - stub_addr - stubs.reserved2);
2525 assert(la_ptr_addr >= stub_addr + stubs.reserved2);
2526 const displacement = try math.cast(u32, la_ptr_addr - stub_addr - stubs.reserved2);
25072527 // jmp
25082528 code[0] = 0xff;
25092529 code[1] = 0x25;
25102530 mem.writeIntLittle(u32, code[2..][0..4], displacement);
25112531 },
25122532 .aarch64 => {
2513 const displacement = la_ptr_addr - stub_addr;
2533 assert(la_ptr_addr >= stub_addr);
2534 const displacement = try math.divExact(u64, la_ptr_addr - stub_addr, 4);
2535 const literal = try math.cast(u19, displacement);
25142536 mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.ldr(.x16, .{
2515 .literal = @intCast(u19, displacement / 4),
2537 .literal = literal,
25162538 }).toU32());
25172539 mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.br(.x16).toU32());
25182540 },
......@@ -2535,7 +2557,10 @@ fn writeStubInStubHelper(self: *MachO, index: u32) !void {
25352557 defer self.base.allocator.free(code);
25362558 switch (self.base.options.target.cpu.arch) {
25372559 .x86_64 => {
2538 const displacement = @intCast(i32, @intCast(i64, stub_helper.offset) - @intCast(i64, stub_off) - stub_size);
2560 const displacement = try math.cast(
2561 i32,
2562 @intCast(i64, stub_helper.offset) - @intCast(i64, stub_off) - stub_size,
2563 );
25392564 // pushq
25402565 code[0] = 0x68;
25412566 mem.writeIntLittle(u32, code[1..][0..4], 0x0); // Just a placeholder populated in `populateLazyBindOffsetsInStubHelper`.
......@@ -2544,11 +2569,11 @@ fn writeStubInStubHelper(self: *MachO, index: u32) !void {
25442569 mem.writeIntLittle(u32, code[6..][0..4], @bitCast(u32, displacement));
25452570 },
25462571 .aarch64 => {
2547 const displacement = @intCast(i64, stub_helper.offset) - @intCast(i64, stub_off) - 4;
2572 const displacement = try math.cast(i28, @intCast(i64, stub_helper.offset) - @intCast(i64, stub_off) - 4);
25482573 mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.ldr(.w16, .{
2549 .literal = 0x2,
2574 .literal = @divExact(stub_size - @sizeOf(u32), 4),
25502575 }).toU32());
2551 mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.b(@intCast(i28, displacement)).toU32());
2576 mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.b(displacement).toU32());
25522577 mem.writeIntLittle(u32, code[8..12], 0x0); // Just a placeholder populated in `populateLazyBindOffsetsInStubHelper`.
25532578 },
25542579 else => unreachable,
......@@ -3239,3 +3264,9 @@ fn fixupInfoCommon(self: *MachO, buffer: []u8, dylib_ordinal: u32) !void {
32393264 }
32403265 }
32413266}
3267
3268pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {
3269 // TODO https://github.com/ziglang/zig/issues/1284
3270 return std.math.add(@TypeOf(actual_size), actual_size, actual_size / ideal_factor) catch
3271 std.math.maxInt(@TypeOf(actual_size));
3272}
src/link/MachO/DebugSymbols.zig+11-13
......@@ -18,9 +18,7 @@ const link = @import("../../link.zig");
1818const MachO = @import("../MachO.zig");
1919const SrcFn = MachO.SrcFn;
2020const TextBlock = MachO.TextBlock;
21const satMul = MachO.satMul;
22const alloc_num = MachO.alloc_num;
23const alloc_den = MachO.alloc_den;
21const padToIdeal = MachO.padToIdeal;
2422const makeStaticString = MachO.makeStaticString;
2523
2624usingnamespace @import("commands.zig");
......@@ -207,7 +205,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void
207205
208206 const linkedit = self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
209207 const ideal_size: u16 = 200 + 128 + 160 + 250;
210 const needed_size = mem.alignForwardGeneric(u64, satMul(ideal_size, alloc_num) / alloc_den, page_size);
208 const needed_size = mem.alignForwardGeneric(u64, padToIdeal(ideal_size), page_size);
211209 const off = linkedit.inner.fileoff + linkedit.inner.filesize;
212210 const vmaddr = linkedit.inner.vmaddr + linkedit.inner.vmsize;
213211
......@@ -804,7 +802,7 @@ fn allocatedSizeLinkedit(self: *DebugSymbols, start: u64) u64 {
804802}
805803
806804fn detectAllocCollisionLinkedit(self: *DebugSymbols, start: u64, size: u64) ?u64 {
807 const end = start + satMul(size, alloc_num) / alloc_den;
805 const end = start + padToIdeal(size);
808806
809807 if (self.symtab_cmd_index) |idx| outer: {
810808 if (self.load_commands.items.len == idx) break :outer;
......@@ -812,7 +810,7 @@ fn detectAllocCollisionLinkedit(self: *DebugSymbols, start: u64, size: u64) ?u64
812810 {
813811 // Symbol table
814812 const symsize = symtab.nsyms * @sizeOf(macho.nlist_64);
815 const increased_size = satMul(symsize, alloc_num) / alloc_den;
813 const increased_size = padToIdeal(symsize);
816814 const test_end = symtab.symoff + increased_size;
817815 if (end > symtab.symoff and start < test_end) {
818816 return test_end;
......@@ -820,7 +818,7 @@ fn detectAllocCollisionLinkedit(self: *DebugSymbols, start: u64, size: u64) ?u64
820818 }
821819 {
822820 // String table
823 const increased_size = satMul(symtab.strsize, alloc_num) / alloc_den;
821 const increased_size = padToIdeal(symtab.strsize);
824822 const test_end = symtab.stroff + increased_size;
825823 if (end > symtab.stroff and start < test_end) {
826824 return test_end;
......@@ -1099,7 +1097,7 @@ pub fn commitDeclDebugInfo(
10991097 last.next = src_fn;
11001098 self.dbg_line_fn_last = src_fn;
11011099
1102 src_fn.off = last.off + (last.len * alloc_num / alloc_den);
1100 src_fn.off = last.off + padToIdeal(last.len);
11031101 }
11041102 } else if (src_fn.prev == null) {
11051103 // Append new function.
......@@ -1108,14 +1106,14 @@ pub fn commitDeclDebugInfo(
11081106 last.next = src_fn;
11091107 self.dbg_line_fn_last = src_fn;
11101108
1111 src_fn.off = last.off + (last.len * alloc_num / alloc_den);
1109 src_fn.off = last.off + padToIdeal(last.len);
11121110 }
11131111 } else {
11141112 // This is the first function of the Line Number Program.
11151113 self.dbg_line_fn_first = src_fn;
11161114 self.dbg_line_fn_last = src_fn;
11171115
1118 src_fn.off = self.dbgLineNeededHeaderBytes(module) * alloc_num / alloc_den;
1116 src_fn.off = padToIdeal(self.dbgLineNeededHeaderBytes(module));
11191117 }
11201118
11211119 const last_src_fn = self.dbg_line_fn_last.?;
......@@ -1259,7 +1257,7 @@ fn updateDeclDebugInfoAllocation(
12591257 last.dbg_info_next = text_block;
12601258 self.dbg_info_decl_last = text_block;
12611259
1262 text_block.dbg_info_off = last.dbg_info_off + (last.dbg_info_len * alloc_num / alloc_den);
1260 text_block.dbg_info_off = last.dbg_info_off + padToIdeal(last.dbg_info_len);
12631261 }
12641262 } else if (text_block.dbg_info_prev == null) {
12651263 // Append new Decl.
......@@ -1268,14 +1266,14 @@ fn updateDeclDebugInfoAllocation(
12681266 last.dbg_info_next = text_block;
12691267 self.dbg_info_decl_last = text_block;
12701268
1271 text_block.dbg_info_off = last.dbg_info_off + (last.dbg_info_len * alloc_num / alloc_den);
1269 text_block.dbg_info_off = last.dbg_info_off + padToIdeal(last.dbg_info_len);
12721270 }
12731271 } else {
12741272 // This is the first Decl of the .debug_info
12751273 self.dbg_info_decl_first = text_block;
12761274 self.dbg_info_decl_last = text_block;
12771275
1278 text_block.dbg_info_off = self.dbgInfoNeededHeaderBytes() * alloc_num / alloc_den;
1276 text_block.dbg_info_off = padToIdeal(self.dbgInfoNeededHeaderBytes());
12791277 }
12801278}
12811279
src/link/MachO/commands.zig+3-5
......@@ -10,9 +10,7 @@ const assert = std.debug.assert;
1010const Allocator = std.mem.Allocator;
1111const MachO = @import("../MachO.zig");
1212const makeStaticString = MachO.makeStaticString;
13const satMul = MachO.satMul;
14const alloc_num = MachO.alloc_num;
15const alloc_den = MachO.alloc_den;
13const padToIdeal = MachO.padToIdeal;
1614
1715pub const LoadCommand = union(enum) {
1816 Segment: SegmentCommand,
......@@ -214,9 +212,9 @@ pub const SegmentCommand = struct {
214212 }
215213
216214 fn detectAllocCollision(self: SegmentCommand, start: u64, size: u64) ?u64 {
217 const end = start + satMul(size, alloc_num) / alloc_den;
215 const end = start + padToIdeal(size);
218216 for (self.sections.items) |section| {
219 const increased_size = satMul(section.size, alloc_num) / alloc_den;
217 const increased_size = padToIdeal(section.size);
220218 const test_end = section.offset + increased_size;
221219 if (end > section.offset and start < test_end) {
222220 return test_end;
test/stage2/aarch64.zig-110
......@@ -1,84 +1,12 @@
11const std = @import("std");
22const TestContext = @import("../../src/test.zig").TestContext;
33
4const macos_aarch64 = std.zig.CrossTarget{
5 .cpu_arch = .aarch64,
6 .os_tag = .macos,
7};
8
94const linux_aarch64 = std.zig.CrossTarget{
105 .cpu_arch = .aarch64,
116 .os_tag = .linux,
127};
138
149pub fn addCases(ctx: *TestContext) !void {
15 {
16 var case = ctx.exe("hello world with updates", macos_aarch64);
17
18 // Regular old hello world
19 case.addCompareOutput(
20 \\extern "c" fn write(usize, usize, usize) void;
21 \\extern "c" fn exit(usize) noreturn;
22 \\
23 \\export fn _start() noreturn {
24 \\ print();
25 \\
26 \\ exit(0);
27 \\}
28 \\
29 \\fn print() void {
30 \\ const msg = @ptrToInt("Hello, World!\n");
31 \\ const len = 14;
32 \\ write(1, msg, len);
33 \\}
34 ,
35 "Hello, World!\n",
36 );
37
38 // Now change the message only
39 case.addCompareOutput(
40 \\extern "c" fn write(usize, usize, usize) void;
41 \\extern "c" fn exit(usize) noreturn;
42 \\
43 \\export fn _start() noreturn {
44 \\ print();
45 \\
46 \\ exit(0);
47 \\}
48 \\
49 \\fn print() void {
50 \\ const msg = @ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n");
51 \\ const len = 104;
52 \\ write(1, msg, len);
53 \\}
54 ,
55 "What is up? This is a longer message that will force the data to be relocated in virtual address space.\n",
56 );
57
58 // Now we print it twice.
59 case.addCompareOutput(
60 \\extern "c" fn write(usize, usize, usize) void;
61 \\extern "c" fn exit(usize) noreturn;
62 \\
63 \\export fn _start() noreturn {
64 \\ print();
65 \\ print();
66 \\
67 \\ exit(0);
68 \\}
69 \\
70 \\fn print() void {
71 \\ const msg = @ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n");
72 \\ const len = 104;
73 \\ write(1, msg, len);
74 \\}
75 ,
76 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
77 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
78 \\
79 );
80 }
81
8210 {
8311 var case = ctx.exe("linux_aarch64 hello world", linux_aarch64);
8412 // Regular old hello world
......@@ -119,28 +47,6 @@ pub fn addCases(ctx: *TestContext) !void {
11947 );
12048 }
12149
122 {
123 var case = ctx.exe("exit fn taking argument", macos_aarch64);
124
125 case.addCompareOutput(
126 \\export fn _start() noreturn {
127 \\ exit(0);
128 \\}
129 \\
130 \\fn exit(ret: usize) noreturn {
131 \\ asm volatile ("svc #0x80"
132 \\ :
133 \\ : [number] "{x16}" (1),
134 \\ [arg1] "{x0}" (ret)
135 \\ : "memory"
136 \\ );
137 \\ unreachable;
138 \\}
139 ,
140 "",
141 );
142 }
143
14450 {
14551 var case = ctx.exe("exit fn taking argument", linux_aarch64);
14652
......@@ -162,20 +68,4 @@ pub fn addCases(ctx: *TestContext) !void {
16268 "",
16369 );
16470 }
165
166 {
167 var case = ctx.exe("only libc exit", macos_aarch64);
168
169 // This test case covers an infrequent scenarion where the string table *may* be relocated
170 // into the position preceeding the symbol table which results in a dyld error.
171 case.addCompareOutput(
172 \\extern "c" fn exit(usize) noreturn;
173 \\
174 \\export fn _start() noreturn {
175 \\ exit(0);
176 \\}
177 ,
178 "",
179 );
180 }
18171}
test/stage2/darwin.zig created+115
......@@ -0,0 +1,115 @@
1const std = @import("std");
2const TestContext = @import("../../src/test.zig").TestContext;
3
4const archs = [2]std.Target.Cpu.Arch{
5 .aarch64, .x86_64,
6};
7
8pub fn addCases(ctx: *TestContext) !void {
9 for (archs) |arch| {
10 const target: std.zig.CrossTarget = .{
11 .cpu_arch = arch,
12 .os_tag = .macos,
13 };
14 {
15 var case = ctx.exe("hello world with updates", target);
16 case.addError("", &[_][]const u8{"error: no entry point found"});
17
18 // Incorrect return type
19 case.addError(
20 \\export fn _start() noreturn {
21 \\}
22 , &[_][]const u8{":2:1: error: expected noreturn, found void"});
23
24 // Regular old hello world
25 case.addCompareOutput(
26 \\extern "c" fn write(usize, usize, usize) usize;
27 \\extern "c" fn exit(usize) noreturn;
28 \\
29 \\export fn _start() noreturn {
30 \\ print();
31 \\
32 \\ exit(0);
33 \\}
34 \\
35 \\fn print() void {
36 \\ const msg = @ptrToInt("Hello, World!\n");
37 \\ const len = 14;
38 \\ _ = write(1, msg, len);
39 \\}
40 ,
41 "Hello, World!\n",
42 );
43
44 // Now change the message only
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 \\
52 \\ exit(0);
53 \\}
54 \\
55 \\fn print() void {
56 \\ const msg = @ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n");
57 \\ const len = 104;
58 \\ _ = write(1, msg, len);
59 \\}
60 ,
61 "What is up? This is a longer message that will force the data to be relocated in virtual address space.\n",
62 );
63
64 // Now we print it twice.
65 case.addCompareOutput(
66 \\extern "c" fn write(usize, usize, usize) usize;
67 \\extern "c" fn exit(usize) noreturn;
68 \\
69 \\export fn _start() noreturn {
70 \\ print();
71 \\ print();
72 \\
73 \\ exit(0);
74 \\}
75 \\
76 \\fn print() void {
77 \\ const msg = @ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n");
78 \\ const len = 104;
79 \\ _ = write(1, msg, len);
80 \\}
81 ,
82 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
83 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
84 \\
85 );
86 }
87 {
88 var case = ctx.exe("corner case - update existing, singular TextBlock", target);
89
90 // This test case also covers an infrequent scenarion where the string table *may* be relocated
91 // into the position preceeding the symbol table which results in a dyld error.
92 case.addCompareOutput(
93 \\extern "c" fn exit(usize) noreturn;
94 \\
95 \\export fn _start() noreturn {
96 \\ exit(0);
97 \\}
98 ,
99 "",
100 );
101
102 case.addCompareOutput(
103 \\extern "c" fn exit(usize) noreturn;
104 \\extern "c" fn write(usize, usize, usize) usize;
105 \\
106 \\export fn _start() noreturn {
107 \\ _ = write(1, @ptrToInt("Hey!\n"), 5);
108 \\ exit(0);
109 \\}
110 ,
111 "Hey!\n",
112 );
113 }
114 }
115}
test/stage2/test.zig+1-110
......@@ -11,11 +11,6 @@ const linux_x64 = std.zig.CrossTarget{
1111 .os_tag = .linux,
1212};
1313
14const macos_x64 = std.zig.CrossTarget{
15 .cpu_arch = .x86_64,
16 .os_tag = .macos,
17};
18
1914const linux_riscv64 = std.zig.CrossTarget{
2015 .cpu_arch = .riscv64,
2116 .os_tag = .linux,
......@@ -28,6 +23,7 @@ pub fn addCases(ctx: *TestContext) !void {
2823 try @import("aarch64.zig").addCases(ctx);
2924 try @import("llvm.zig").addCases(ctx);
3025 try @import("wasm.zig").addCases(ctx);
26 try @import("darwin.zig").addCases(ctx);
3127
3228 {
3329 var case = ctx.exe("hello world with updates", linux_x64);
......@@ -141,95 +137,6 @@ pub fn addCases(ctx: *TestContext) !void {
141137 );
142138 }
143139
144 {
145 var case = ctx.exe("hello world with updates", macos_x64);
146 case.addError("", &[_][]const u8{"error: no entry point found"});
147
148 // Incorrect return type
149 case.addError(
150 \\export fn _start() noreturn {
151 \\}
152 , &[_][]const u8{":2:1: error: expected noreturn, found void"});
153
154 // Regular old hello world
155 case.addCompareOutput(
156 \\extern "c" fn write(usize, usize, usize) usize;
157 \\extern "c" fn exit(usize) noreturn;
158 \\
159 \\export fn _start() noreturn {
160 \\ print();
161 \\
162 \\ exit(0);
163 \\}
164 \\
165 \\fn print() void {
166 \\ const msg = @ptrToInt("Hello, World!\n");
167 \\ const len = 14;
168 \\ const nwritten = write(1, msg, len);
169 \\ assert(nwritten == len);
170 \\}
171 \\
172 \\fn assert(ok: bool) void {
173 \\ if (!ok) unreachable; // assertion failure
174 \\}
175 ,
176 "Hello, World!\n",
177 );
178
179 // Now change the message only
180 case.addCompareOutput(
181 \\extern "c" fn write(usize, usize, usize) usize;
182 \\extern "c" fn exit(usize) noreturn;
183 \\
184 \\export fn _start() noreturn {
185 \\ print();
186 \\
187 \\ exit(0);
188 \\}
189 \\
190 \\fn print() void {
191 \\ const msg = @ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n");
192 \\ const len = 104;
193 \\ const nwritten = write(1, msg, len);
194 \\ assert(nwritten == len);
195 \\}
196 \\
197 \\fn assert(ok: bool) void {
198 \\ if (!ok) unreachable; // assertion failure
199 \\}
200 ,
201 "What is up? This is a longer message that will force the data to be relocated in virtual address space.\n",
202 );
203
204 // Now we print it twice.
205 case.addCompareOutput(
206 \\extern "c" fn write(usize, usize, usize) usize;
207 \\extern "c" fn exit(usize) noreturn;
208 \\
209 \\export fn _start() noreturn {
210 \\ print();
211 \\ print();
212 \\
213 \\ exit(0);
214 \\}
215 \\
216 \\fn print() void {
217 \\ const msg = @ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n");
218 \\ const len = 104;
219 \\ const nwritten = write(1, msg, len);
220 \\ assert(nwritten == len);
221 \\}
222 \\
223 \\fn assert(ok: bool) void {
224 \\ if (!ok) unreachable; // assertion failure
225 \\}
226 ,
227 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
228 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
229 \\
230 );
231 }
232
233140 {
234141 var case = ctx.exe("riscv64 hello world", linux_riscv64);
235142 // Regular old hello world
......@@ -1446,22 +1353,6 @@ pub fn addCases(ctx: *TestContext) !void {
14461353 \\}
14471354 , &[_][]const u8{":8:10: error: evaluation exceeded 1000 backwards branches"});
14481355 }
1449
1450 {
1451 var case = ctx.exe("only libc exit", macos_x64);
1452
1453 // This test case covers an infrequent scenarion where the string table *may* be relocated
1454 // into the position preceeding the symbol table which results in a dyld error.
1455 case.addCompareOutput(
1456 \\extern "c" fn exit(usize) noreturn;
1457 \\
1458 \\export fn _start() noreturn {
1459 \\ exit(0);
1460 \\}
1461 ,
1462 "",
1463 );
1464 }
14651356 {
14661357 var case = ctx.exe("orelse at comptime", linux_x64);
14671358 case.addCompareOutput(