| author | |
| committer | |
| log | 246e3e0c75c5f0201f3be6514360adf04669caab |
| tree | 3722581134332ef618139b483120bbc5bb10fce4 |
| parent | a70cbe779adb11faeb88ceffad0cfa729d2eec6e |
2 files changed, 47 insertions(+), 29 deletions(-)
src/link/MachO.zig+24-15| ... | @@ -3358,27 +3358,36 @@ fn writeDyldInfoData(self: *MachO) !void { | ... | @@ -3358,27 +3358,36 @@ fn writeDyldInfoData(self: *MachO) !void { |
| 3358 | try self.collectExportData(&trie); | 3358 | try self.collectExportData(&trie); |
| 3359 | 3359 | ||
| 3360 | const link_seg = self.getLinkeditSegmentPtr(); | 3360 | const link_seg = self.getLinkeditSegmentPtr(); |
| 3361 | const rebase_off = mem.alignForwardGeneric(u64, link_seg.fileoff, @alignOf(u64)); | 3361 | assert(mem.isAlignedGeneric(u64, link_seg.fileoff, @alignOf(u64))); |
| 3362 | assert(rebase_off == link_seg.fileoff); | 3362 | const rebase_off = link_seg.fileoff; |
| 3363 | const rebase_size = try bind.rebaseInfoSize(rebase_pointers.items); | 3363 | const rebase_size = try bind.rebaseInfoSize(rebase_pointers.items); |
| 3364 | log.debug("writing rebase info from 0x{x} to 0x{x}", .{ rebase_off, rebase_off + rebase_size }); | 3364 | const rebase_size_aligned = mem.alignForwardGeneric(u64, rebase_size, @alignOf(u64)); |
| 3365 | log.debug("writing rebase info from 0x{x} to 0x{x}", .{ rebase_off, rebase_off + rebase_size_aligned }); | ||
| 3365 | 3366 | ||
| 3366 | const bind_off = mem.alignForwardGeneric(u64, rebase_off + rebase_size, @alignOf(u64)); | 3367 | const bind_off = rebase_off + rebase_size_aligned; |
| 3367 | const bind_size = try bind.bindInfoSize(bind_pointers.items); | 3368 | const bind_size = try bind.bindInfoSize(bind_pointers.items); |
| 3368 | log.debug("writing bind info from 0x{x} to 0x{x}", .{ bind_off, bind_off + bind_size }); | 3369 | const bind_size_aligned = mem.alignForwardGeneric(u64, bind_size, @alignOf(u64)); |
| 3370 | log.debug("writing bind info from 0x{x} to 0x{x}", .{ bind_off, bind_off + bind_size_aligned }); | ||
| 3369 | 3371 | ||
| 3370 | const lazy_bind_off = mem.alignForwardGeneric(u64, bind_off + bind_size, @alignOf(u64)); | 3372 | const lazy_bind_off = bind_off + bind_size_aligned; |
| 3371 | const lazy_bind_size = try bind.lazyBindInfoSize(lazy_bind_pointers.items); | 3373 | const lazy_bind_size = try bind.lazyBindInfoSize(lazy_bind_pointers.items); |
| 3372 | log.debug("writing lazy bind info from 0x{x} to 0x{x}", .{ lazy_bind_off, lazy_bind_off + lazy_bind_size }); | 3374 | const lazy_bind_size_aligned = mem.alignForwardGeneric(u64, lazy_bind_size, @alignOf(u64)); |
| 3375 | log.debug("writing lazy bind info from 0x{x} to 0x{x}", .{ | ||
| 3376 | lazy_bind_off, | ||
| 3377 | lazy_bind_off + lazy_bind_size_aligned, | ||
| 3378 | }); | ||
| 3373 | 3379 | ||
| 3374 | const export_off = mem.alignForwardGeneric(u64, lazy_bind_off + lazy_bind_size, @alignOf(u64)); | 3380 | const export_off = lazy_bind_off + lazy_bind_size_aligned; |
| 3375 | const export_size = trie.size; | 3381 | const export_size = trie.size; |
| 3376 | log.debug("writing export trie from 0x{x} to 0x{x}", .{ export_off, export_off + export_size }); | 3382 | const export_size_aligned = mem.alignForwardGeneric(u64, export_size, @alignOf(u64)); |
| 3383 | log.debug("writing export trie from 0x{x} to 0x{x}", .{ export_off, export_off + export_size_aligned }); | ||
| 3377 | 3384 | ||
| 3378 | const needed_size = export_off + export_size - rebase_off; | 3385 | const needed_size = math.cast(usize, export_off + export_size_aligned - rebase_off) orelse |
| 3386 | return error.Overflow; | ||
| 3379 | link_seg.filesize = needed_size; | 3387 | link_seg.filesize = needed_size; |
| 3388 | assert(mem.isAlignedGeneric(u64, link_seg.fileoff + link_seg.filesize, @alignOf(u64))); | ||
| 3380 | 3389 | ||
| 3381 | var buffer = try gpa.alloc(u8, math.cast(usize, needed_size) orelse return error.Overflow); | 3390 | var buffer = try gpa.alloc(u8, needed_size); |
| 3382 | defer gpa.free(buffer); | 3391 | defer gpa.free(buffer); |
| 3383 | mem.set(u8, buffer, 0); | 3392 | mem.set(u8, buffer, 0); |
| 3384 | 3393 | ||
| ... | @@ -3407,13 +3416,13 @@ fn writeDyldInfoData(self: *MachO) !void { | ... | @@ -3407,13 +3416,13 @@ fn writeDyldInfoData(self: *MachO) !void { |
| 3407 | try self.populateLazyBindOffsetsInStubHelper(buffer[start..end]); | 3416 | try self.populateLazyBindOffsetsInStubHelper(buffer[start..end]); |
| 3408 | 3417 | ||
| 3409 | self.dyld_info_cmd.rebase_off = @intCast(u32, rebase_off); | 3418 | self.dyld_info_cmd.rebase_off = @intCast(u32, rebase_off); |
| 3410 | self.dyld_info_cmd.rebase_size = @intCast(u32, rebase_size); | 3419 | self.dyld_info_cmd.rebase_size = @intCast(u32, rebase_size_aligned); |
| 3411 | self.dyld_info_cmd.bind_off = @intCast(u32, bind_off); | 3420 | self.dyld_info_cmd.bind_off = @intCast(u32, bind_off); |
| 3412 | self.dyld_info_cmd.bind_size = @intCast(u32, bind_size); | 3421 | self.dyld_info_cmd.bind_size = @intCast(u32, bind_size_aligned); |
| 3413 | self.dyld_info_cmd.lazy_bind_off = @intCast(u32, lazy_bind_off); | 3422 | self.dyld_info_cmd.lazy_bind_off = @intCast(u32, lazy_bind_off); |
| 3414 | self.dyld_info_cmd.lazy_bind_size = @intCast(u32, lazy_bind_size); | 3423 | self.dyld_info_cmd.lazy_bind_size = @intCast(u32, lazy_bind_size_aligned); |
| 3415 | self.dyld_info_cmd.export_off = @intCast(u32, export_off); | 3424 | self.dyld_info_cmd.export_off = @intCast(u32, export_off); |
| 3416 | self.dyld_info_cmd.export_size = @intCast(u32, export_size); | 3425 | self.dyld_info_cmd.export_size = @intCast(u32, export_size_aligned); |
| 3417 | } | 3426 | } |
| 3418 | 3427 | ||
| 3419 | fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { | 3428 | fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { |
src/link/MachO/zld.zig+23-14| ... | @@ -2178,25 +2178,34 @@ pub const Zld = struct { | ... | @@ -2178,25 +2178,34 @@ pub const Zld = struct { |
| 2178 | try self.collectExportData(&trie); | 2178 | try self.collectExportData(&trie); |
| 2179 | 2179 | ||
| 2180 | const link_seg = self.getLinkeditSegmentPtr(); | 2180 | const link_seg = self.getLinkeditSegmentPtr(); |
| 2181 | const rebase_off = mem.alignForwardGeneric(u64, link_seg.fileoff, @alignOf(u64)); | 2181 | assert(mem.isAlignedGeneric(u64, link_seg.fileoff, @alignOf(u64))); |
| 2182 | assert(rebase_off == link_seg.fileoff); | 2182 | const rebase_off = link_seg.fileoff; |
| 2183 | const rebase_size = try bind.rebaseInfoSize(rebase_pointers.items); | 2183 | const rebase_size = try bind.rebaseInfoSize(rebase_pointers.items); |
| 2184 | log.debug("writing rebase info from 0x{x} to 0x{x}", .{ rebase_off, rebase_off + rebase_size }); | 2184 | const rebase_size_aligned = mem.alignForwardGeneric(u64, rebase_size, @alignOf(u64)); |
| 2185 | log.debug("writing rebase info from 0x{x} to 0x{x}", .{ rebase_off, rebase_off + rebase_size_aligned }); | ||
| 2185 | 2186 | ||
| 2186 | const bind_off = mem.alignForwardGeneric(u64, rebase_off + rebase_size, @alignOf(u64)); | 2187 | const bind_off = rebase_off + rebase_size_aligned; |
| 2187 | const bind_size = try bind.bindInfoSize(bind_pointers.items); | 2188 | const bind_size = try bind.bindInfoSize(bind_pointers.items); |
| 2188 | log.debug("writing bind info from 0x{x} to 0x{x}", .{ bind_off, bind_off + bind_size }); | 2189 | const bind_size_aligned = mem.alignForwardGeneric(u64, bind_size, @alignOf(u64)); |
| 2190 | log.debug("writing bind info from 0x{x} to 0x{x}", .{ bind_off, bind_off + bind_size_aligned }); | ||
| 2189 | 2191 | ||
| 2190 | const lazy_bind_off = mem.alignForwardGeneric(u64, bind_off + bind_size, @alignOf(u64)); | 2192 | const lazy_bind_off = bind_off + bind_size_aligned; |
| 2191 | const lazy_bind_size = try bind.lazyBindInfoSize(lazy_bind_pointers.items); | 2193 | const lazy_bind_size = try bind.lazyBindInfoSize(lazy_bind_pointers.items); |
| 2192 | log.debug("writing lazy bind info from 0x{x} to 0x{x}", .{ lazy_bind_off, lazy_bind_off + lazy_bind_size }); | 2194 | const lazy_bind_size_aligned = mem.alignForwardGeneric(u64, lazy_bind_size, @alignOf(u64)); |
| 2195 | log.debug("writing lazy bind info from 0x{x} to 0x{x}", .{ | ||
| 2196 | lazy_bind_off, | ||
| 2197 | lazy_bind_off + lazy_bind_size_aligned, | ||
| 2198 | }); | ||
| 2193 | 2199 | ||
| 2194 | const export_off = mem.alignForwardGeneric(u64, lazy_bind_off + lazy_bind_size, @alignOf(u64)); | 2200 | const export_off = lazy_bind_off + lazy_bind_size_aligned; |
| 2195 | const export_size = trie.size; | 2201 | const export_size = trie.size; |
| 2196 | log.debug("writing export trie from 0x{x} to 0x{x}", .{ export_off, export_off + export_size }); | 2202 | const export_size_aligned = mem.alignForwardGeneric(u64, export_size, @alignOf(u64)); |
| 2203 | log.debug("writing export trie from 0x{x} to 0x{x}", .{ export_off, export_off + export_size_aligned }); | ||
| 2197 | 2204 | ||
| 2198 | const needed_size = math.cast(usize, export_off + export_size - rebase_off) orelse return error.Overflow; | 2205 | const needed_size = math.cast(usize, export_off + export_size_aligned - rebase_off) orelse |
| 2206 | return error.Overflow; | ||
| 2199 | link_seg.filesize = needed_size; | 2207 | link_seg.filesize = needed_size; |
| 2208 | assert(mem.isAlignedGeneric(u64, link_seg.fileoff + link_seg.filesize, @alignOf(u64))); | ||
| 2200 | 2209 | ||
| 2201 | var buffer = try gpa.alloc(u8, needed_size); | 2210 | var buffer = try gpa.alloc(u8, needed_size); |
| 2202 | defer gpa.free(buffer); | 2211 | defer gpa.free(buffer); |
| ... | @@ -2228,13 +2237,13 @@ pub const Zld = struct { | ... | @@ -2228,13 +2237,13 @@ pub const Zld = struct { |
| 2228 | try self.populateLazyBindOffsetsInStubHelper(buffer[offset..][0..size]); | 2237 | try self.populateLazyBindOffsetsInStubHelper(buffer[offset..][0..size]); |
| 2229 | 2238 | ||
| 2230 | self.dyld_info_cmd.rebase_off = @intCast(u32, rebase_off); | 2239 | self.dyld_info_cmd.rebase_off = @intCast(u32, rebase_off); |
| 2231 | self.dyld_info_cmd.rebase_size = @intCast(u32, rebase_size); | 2240 | self.dyld_info_cmd.rebase_size = @intCast(u32, rebase_size_aligned); |
| 2232 | self.dyld_info_cmd.bind_off = @intCast(u32, bind_off); | 2241 | self.dyld_info_cmd.bind_off = @intCast(u32, bind_off); |
| 2233 | self.dyld_info_cmd.bind_size = @intCast(u32, bind_size); | 2242 | self.dyld_info_cmd.bind_size = @intCast(u32, bind_size_aligned); |
| 2234 | self.dyld_info_cmd.lazy_bind_off = @intCast(u32, lazy_bind_off); | 2243 | self.dyld_info_cmd.lazy_bind_off = @intCast(u32, lazy_bind_off); |
| 2235 | self.dyld_info_cmd.lazy_bind_size = @intCast(u32, lazy_bind_size); | 2244 | self.dyld_info_cmd.lazy_bind_size = @intCast(u32, lazy_bind_size_aligned); |
| 2236 | self.dyld_info_cmd.export_off = @intCast(u32, export_off); | 2245 | self.dyld_info_cmd.export_off = @intCast(u32, export_off); |
| 2237 | self.dyld_info_cmd.export_size = @intCast(u32, export_size); | 2246 | self.dyld_info_cmd.export_size = @intCast(u32, export_size_aligned); |
| 2238 | } | 2247 | } |
| 2239 | 2248 | ||
| 2240 | fn populateLazyBindOffsetsInStubHelper(self: *Zld, buffer: []const u8) !void { | 2249 | fn populateLazyBindOffsetsInStubHelper(self: *Zld, buffer: []const u8) !void { |