authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-07-03 10:11:58+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-07-22 16:58:20+02:00
logb81c48d9856141a4a380bd8569825919e5bd8cfc
tree065f0f2e75d192255e00e2ce2b22c67675bd1825
parent7ec9a4f382fb73950e4b4c5f7e005c154f6ec294

macho: read the entire file contents into memory at once


4 files changed, 105 insertions(+), 125 deletions(-)

src/link/MachO.zig+2-2
......@@ -2895,7 +2895,7 @@ fn resolveSymbolsInObject(self: *MachO, object_id: u16) !void {
28952895
28962896 log.debug("resolving symbols in '{s}'", .{object.name});
28972897
2898 for (object.symtab.items) |sym, id| {
2898 for (object.symtab) |sym, id| {
28992899 const sym_id = @intCast(u32, id);
29002900 const sym_name = object.getString(sym.n_strx);
29012901
......@@ -6600,7 +6600,7 @@ pub fn symbolIsTemp(sym: macho.nlist_64, sym_name: []const u8) bool {
66006600 return mem.startsWith(u8, sym_name, "l") or mem.startsWith(u8, sym_name, "L");
66016601}
66026602
6603pub fn findFirst(comptime T: type, haystack: []T, start: usize, predicate: anytype) usize {
6603pub fn findFirst(comptime T: type, haystack: []const T, start: usize, predicate: anytype) usize {
66046604 if (!@hasDecl(@TypeOf(predicate), "predicate"))
66056605 @compileError("Predicate is required to define fn predicate(@This(), T) bool");
66066606
src/link/MachO/Atom.zig+3-3
......@@ -241,7 +241,7 @@ const RelocContext = struct {
241241 macho_file: *MachO,
242242};
243243
244pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocContext) !void {
244pub fn parseRelocs(self: *Atom, relocs: []const macho.relocation_info, context: RelocContext) !void {
245245 const tracy = trace(@src());
246246 defer tracy.end();
247247
......@@ -284,7 +284,7 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC
284284 }
285285
286286 assert(subtractor == null);
287 const sym = context.object.symtab.items[rel.r_symbolnum];
287 const sym = context.object.symtab[rel.r_symbolnum];
288288 if (sym.sect() and !sym.ext()) {
289289 subtractor = context.object.symbol_mapping.get(rel.r_symbolnum).?;
290290 } else {
......@@ -350,7 +350,7 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC
350350 break :target Relocation.Target{ .local = local_sym_index };
351351 }
352352
353 const sym = context.object.symtab.items[rel.r_symbolnum];
353 const sym = context.object.symtab[rel.r_symbolnum];
354354 const sym_name = context.object.getString(sym.n_strx);
355355
356356 if (sym.sect() and !sym.ext()) {
src/link/MachO/Object.zig+99-119
......@@ -20,9 +20,14 @@ const MachO = @import("../MachO.zig");
2020file: fs.File,
2121name: []const u8,
2222
23/// Data contents of the file. Includes sections, and data of load commands.
24/// Excludes the backing memory for the header and load commands.
25/// Initialized in `parse`.
26contents: []const u8 = undefined,
27
2328file_offset: ?u32 = null,
2429
25header: ?macho.mach_header_64 = null,
30header: macho.mach_header_64 = undefined,
2631
2732load_commands: std.ArrayListUnmanaged(macho.LoadCommand) = .{},
2833
......@@ -41,9 +46,9 @@ dwarf_debug_line_index: ?u16 = null,
4146dwarf_debug_line_str_index: ?u16 = null,
4247dwarf_debug_ranges_index: ?u16 = null,
4348
44symtab: std.ArrayListUnmanaged(macho.nlist_64) = .{},
45strtab: std.ArrayListUnmanaged(u8) = .{},
46data_in_code_entries: std.ArrayListUnmanaged(macho.data_in_code_entry) = .{},
49symtab: []const macho.nlist_64 = &.{},
50strtab: []const u8 = &.{},
51data_in_code_entries: []const macho.data_in_code_entry = &.{},
4752
4853// Debug info
4954debug_info: ?DebugInfo = null,
......@@ -65,41 +70,41 @@ analyzed: bool = false,
6570
6671const DebugInfo = struct {
6772 inner: dwarf.DwarfInfo,
68 debug_info: []u8,
69 debug_abbrev: []u8,
70 debug_str: []u8,
71 debug_line: []u8,
72 debug_line_str: []u8,
73 debug_ranges: []u8,
73 debug_info: []const u8,
74 debug_abbrev: []const u8,
75 debug_str: []const u8,
76 debug_line: []const u8,
77 debug_line_str: []const u8,
78 debug_ranges: []const u8,
7479
7580 pub fn parseFromObject(allocator: Allocator, object: *const Object) !?DebugInfo {
7681 var debug_info = blk: {
7782 const index = object.dwarf_debug_info_index orelse return null;
78 break :blk try object.readSection(allocator, index);
83 break :blk object.getSectionContents(index);
7984 };
8085 var debug_abbrev = blk: {
8186 const index = object.dwarf_debug_abbrev_index orelse return null;
82 break :blk try object.readSection(allocator, index);
87 break :blk object.getSectionContents(index);
8388 };
8489 var debug_str = blk: {
8590 const index = object.dwarf_debug_str_index orelse return null;
86 break :blk try object.readSection(allocator, index);
91 break :blk object.getSectionContents(index);
8792 };
8893 var debug_line = blk: {
8994 const index = object.dwarf_debug_line_index orelse return null;
90 break :blk try object.readSection(allocator, index);
95 break :blk object.getSectionContents(index);
9196 };
9297 var debug_line_str = blk: {
9398 if (object.dwarf_debug_line_str_index) |ind| {
94 break :blk try object.readSection(allocator, ind);
99 break :blk object.getSectionContents(ind);
95100 }
96 break :blk try allocator.alloc(u8, 0);
101 break :blk &[0]u8{};
97102 };
98103 var debug_ranges = blk: {
99104 if (object.dwarf_debug_ranges_index) |ind| {
100 break :blk try object.readSection(allocator, ind);
105 break :blk object.getSectionContents(ind);
101106 }
102 break :blk try allocator.alloc(u8, 0);
107 break :blk &[0]u8{};
103108 };
104109
105110 var inner: dwarf.DwarfInfo = .{
......@@ -125,12 +130,6 @@ const DebugInfo = struct {
125130 }
126131
127132 pub fn deinit(self: *DebugInfo, allocator: Allocator) void {
128 allocator.free(self.debug_info);
129 allocator.free(self.debug_abbrev);
130 allocator.free(self.debug_str);
131 allocator.free(self.debug_line);
132 allocator.free(self.debug_line_str);
133 allocator.free(self.debug_ranges);
134133 self.inner.deinit(allocator);
135134 }
136135};
......@@ -140,9 +139,7 @@ pub fn deinit(self: *Object, allocator: Allocator) void {
140139 lc.deinit(allocator);
141140 }
142141 self.load_commands.deinit(allocator);
143 self.data_in_code_entries.deinit(allocator);
144 self.symtab.deinit(allocator);
145 self.strtab.deinit(allocator);
142 allocator.free(self.contents);
146143 self.sections_as_symbols.deinit(allocator);
147144 self.symbol_mapping.deinit(allocator);
148145 self.reverse_symbol_mapping.deinit(allocator);
......@@ -155,14 +152,6 @@ pub fn deinit(self: *Object, allocator: Allocator) void {
155152 if (self.debug_info) |*db| {
156153 db.deinit(allocator);
157154 }
158
159 if (self.tu_name) |n| {
160 allocator.free(n);
161 }
162
163 if (self.tu_comp_dir) |n| {
164 allocator.free(n);
165 }
166155}
167156
168157pub fn free(self: *Object, allocator: Allocator, macho_file: *MachO) void {
......@@ -233,21 +222,28 @@ fn freeAtoms(self: *Object, macho_file: *MachO) void {
233222}
234223
235224pub fn parse(self: *Object, allocator: Allocator, target: std.Target) !void {
236 const reader = self.file.reader();
237 if (self.file_offset) |offset| {
238 try reader.context.seekTo(offset);
225 const file_stat = try self.file.stat();
226 const file_size = math.cast(usize, file_stat.size) orelse return error.Overflow;
227 self.contents = try self.file.readToEndAlloc(allocator, file_size);
228
229 var stream = std.io.fixedBufferStream(self.contents);
230 const reader = stream.reader();
231
232 const file_offset = self.file_offset orelse 0;
233 if (file_offset > 0) {
234 try reader.context.seekTo(file_offset);
239235 }
240236
241 const header = try reader.readStruct(macho.mach_header_64);
242 if (header.filetype != macho.MH_OBJECT) {
237 self.header = try reader.readStruct(macho.mach_header_64);
238 if (self.header.filetype != macho.MH_OBJECT) {
243239 log.debug("invalid filetype: expected 0x{x}, found 0x{x}", .{
244240 macho.MH_OBJECT,
245 header.filetype,
241 self.header.filetype,
246242 });
247243 return error.NotObject;
248244 }
249245
250 const this_arch: std.Target.Cpu.Arch = switch (header.cputype) {
246 const this_arch: std.Target.Cpu.Arch = switch (self.header.cputype) {
251247 macho.CPU_TYPE_ARM64 => .aarch64,
252248 macho.CPU_TYPE_X86_64 => .x86_64,
253249 else => |value| {
......@@ -260,22 +256,10 @@ pub fn parse(self: *Object, allocator: Allocator, target: std.Target) !void {
260256 return error.MismatchedCpuArchitecture;
261257 }
262258
263 self.header = header;
264
265 try self.readLoadCommands(allocator, reader);
266 try self.parseSymtab(allocator);
267 try self.parseDataInCode(allocator);
268 try self.parseDebugInfo(allocator);
269}
270
271pub fn readLoadCommands(self: *Object, allocator: Allocator, reader: anytype) !void {
272 const header = self.header orelse unreachable; // Unreachable here signifies a fatal unexplored condition.
273 const offset = self.file_offset orelse 0;
274
275 try self.load_commands.ensureUnusedCapacity(allocator, header.ncmds);
259 try self.load_commands.ensureUnusedCapacity(allocator, self.header.ncmds);
276260
277261 var i: u16 = 0;
278 while (i < header.ncmds) : (i += 1) {
262 while (i < self.header.ncmds) : (i += 1) {
279263 var cmd = try macho.LoadCommand.read(allocator, reader);
280264 switch (cmd.cmd()) {
281265 .SEGMENT_64 => {
......@@ -305,18 +289,18 @@ pub fn readLoadCommands(self: *Object, allocator: Allocator, reader: anytype) !v
305289 }
306290 }
307291
308 sect.offset += offset;
292 sect.offset += file_offset;
309293 if (sect.reloff > 0) {
310 sect.reloff += offset;
294 sect.reloff += file_offset;
311295 }
312296 }
313297
314 seg.inner.fileoff += offset;
298 seg.inner.fileoff += file_offset;
315299 },
316300 .SYMTAB => {
317301 self.symtab_cmd_index = i;
318 cmd.symtab.symoff += offset;
319 cmd.symtab.stroff += offset;
302 cmd.symtab.symoff += file_offset;
303 cmd.symtab.stroff += file_offset;
320304 },
321305 .DYSYMTAB => {
322306 self.dysymtab_cmd_index = i;
......@@ -326,7 +310,7 @@ pub fn readLoadCommands(self: *Object, allocator: Allocator, reader: anytype) !v
326310 },
327311 .DATA_IN_CODE => {
328312 self.data_in_code_cmd_index = i;
329 cmd.linkedit_data.dataoff += offset;
313 cmd.linkedit_data.dataoff += file_offset;
330314 },
331315 else => {
332316 log.debug("Unknown load command detected: 0x{x}.", .{cmd.cmd()});
......@@ -334,6 +318,10 @@ pub fn readLoadCommands(self: *Object, allocator: Allocator, reader: anytype) !v
334318 }
335319 self.load_commands.appendAssumeCapacity(cmd);
336320 }
321
322 self.parseSymtab();
323 self.parseDataInCode();
324 try self.parseDebugInfo(allocator);
337325}
338326
339327const NlistWithIndex = struct {
......@@ -373,7 +361,11 @@ const NlistWithIndex = struct {
373361 }
374362};
375363
376fn filterDice(dices: []macho.data_in_code_entry, start_addr: u64, end_addr: u64) []macho.data_in_code_entry {
364fn filterDice(
365 dices: []const macho.data_in_code_entry,
366 start_addr: u64,
367 end_addr: u64,
368) []const macho.data_in_code_entry {
377369 const Predicate = struct {
378370 addr: u64,
379371
......@@ -400,10 +392,10 @@ pub fn parseIntoAtoms(self: *Object, allocator: Allocator, macho_file: *MachO) !
400392 // local < extern defined < undefined. Unfortunately, this is not guaranteed! For instance,
401393 // the GO compiler does not necessarily respect that therefore we sort immediately by type
402394 // and address within.
403 var sorted_all_nlists = try std.ArrayList(NlistWithIndex).initCapacity(allocator, self.symtab.items.len);
395 var sorted_all_nlists = try std.ArrayList(NlistWithIndex).initCapacity(allocator, self.symtab.len);
404396 defer sorted_all_nlists.deinit();
405397
406 for (self.symtab.items) |nlist, index| {
398 for (self.symtab) |nlist, index| {
407399 sorted_all_nlists.appendAssumeCapacity(.{
408400 .nlist = nlist,
409401 .index = @intCast(u32, index),
......@@ -439,16 +431,20 @@ pub fn parseIntoAtoms(self: *Object, allocator: Allocator, macho_file: *MachO) !
439431 continue;
440432 };
441433
434 const is_zerofill = blk: {
435 const section_type = sect.type_();
436 break :blk section_type == macho.S_ZEROFILL or section_type == macho.S_THREAD_LOCAL_ZEROFILL;
437 };
438
442439 // Read section's code
443 var code = try allocator.alloc(u8, @intCast(usize, sect.size));
444 defer allocator.free(code);
445 _ = try self.file.preadAll(code, sect.offset);
440 const code: ?[]const u8 = if (!is_zerofill) self.getSectionContents(sect_id) else null;
446441
447442 // Read section's list of relocations
448 var raw_relocs = try allocator.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info));
449 defer allocator.free(raw_relocs);
450 _ = try self.file.preadAll(raw_relocs, sect.reloff);
451 const relocs = mem.bytesAsSlice(macho.relocation_info, raw_relocs);
443 const raw_relocs = self.contents[sect.reloff..][0 .. sect.nreloc * @sizeOf(macho.relocation_info)];
444 const relocs = mem.bytesAsSlice(
445 macho.relocation_info,
446 @alignCast(@alignOf(macho.relocation_info), raw_relocs),
447 );
452448
453449 // Symbols within this section only.
454450 const filtered_nlists = NlistWithIndex.filterInSection(sorted_nlists, sect);
......@@ -456,7 +452,7 @@ pub fn parseIntoAtoms(self: *Object, allocator: Allocator, macho_file: *MachO) !
456452 macho_file.has_dices = macho_file.has_dices or blk: {
457453 if (self.text_section_index) |index| {
458454 if (index != id) break :blk false;
459 if (self.data_in_code_entries.items.len == 0) break :blk false;
455 if (self.data_in_code_entries.len == 0) break :blk false;
460456 break :blk true;
461457 }
462458 break :blk false;
......@@ -482,16 +478,12 @@ pub fn parseIntoAtoms(self: *Object, allocator: Allocator, macho_file: *MachO) !
482478 const aligned_size = mem.alignForwardGeneric(u64, sect.size, alignment);
483479 const atom = try macho_file.createEmptyAtom(atom_local_sym_index, aligned_size, sect.@"align");
484480
485 const is_zerofill = blk: {
486 const section_type = sect.type_();
487 break :blk section_type == macho.S_ZEROFILL or section_type == macho.S_THREAD_LOCAL_ZEROFILL;
488 };
489 if (!is_zerofill) {
490 mem.copy(u8, atom.code.items, code);
481 if (code) |cc| {
482 assert(!is_zerofill);
483 mem.copy(u8, atom.code.items, cc);
491484 }
492485
493 // TODO stage2 bug: @alignCast shouldn't be needed
494 try atom.parseRelocs(@alignCast(@alignOf(macho.relocation_info), relocs), .{
486 try atom.parseRelocs(relocs, .{
495487 .base_addr = sect.addr,
496488 .allocator = allocator,
497489 .object = self,
......@@ -499,7 +491,7 @@ pub fn parseIntoAtoms(self: *Object, allocator: Allocator, macho_file: *MachO) !
499491 });
500492
501493 if (macho_file.has_dices) {
502 const dices = filterDice(self.data_in_code_entries.items, sect.addr, sect.addr + sect.size);
494 const dices = filterDice(self.data_in_code_entries, sect.addr, sect.addr + sect.size);
503495 try atom.dices.ensureTotalCapacity(allocator, dices.len);
504496
505497 for (dices) |dice| {
......@@ -562,20 +554,13 @@ pub fn parseIntoAtoms(self: *Object, allocator: Allocator, macho_file: *MachO) !
562554 }
563555}
564556
565fn parseSymtab(self: *Object, allocator: Allocator) !void {
557fn parseSymtab(self: *Object) void {
566558 const index = self.symtab_cmd_index orelse return;
567 const symtab_cmd = self.load_commands.items[index].symtab;
568
569 var symtab = try allocator.alloc(u8, @sizeOf(macho.nlist_64) * symtab_cmd.nsyms);
570 defer allocator.free(symtab);
571 _ = try self.file.preadAll(symtab, symtab_cmd.symoff);
572 const slice = @alignCast(@alignOf(macho.nlist_64), mem.bytesAsSlice(macho.nlist_64, symtab));
573 try self.symtab.appendSlice(allocator, slice);
574
575 var strtab = try allocator.alloc(u8, symtab_cmd.strsize);
576 defer allocator.free(strtab);
577 _ = try self.file.preadAll(strtab, symtab_cmd.stroff);
578 try self.strtab.appendSlice(allocator, strtab);
559 const symtab = self.load_commands.items[index].symtab;
560 const symtab_size = @sizeOf(macho.nlist_64) * symtab.nsyms;
561 const raw_symtab = self.contents[symtab.symoff..][0..symtab_size];
562 self.symtab = mem.bytesAsSlice(macho.nlist_64, @alignCast(@alignOf(macho.nlist_64), raw_symtab));
563 self.strtab = self.contents[symtab.stroff..][0..symtab.strsize];
579564}
580565
581566pub fn parseDebugInfo(self: *Object, allocator: Allocator) !void {
......@@ -599,8 +584,8 @@ pub fn parseDebugInfo(self: *Object, allocator: Allocator) !void {
599584 const comp_dir = try compile_unit.die.getAttrString(&debug_info.inner, dwarf.AT.comp_dir);
600585
601586 self.debug_info = debug_info;
602 self.tu_name = try allocator.dupe(u8, name);
603 self.tu_comp_dir = try allocator.dupe(u8, comp_dir);
587 self.tu_name = name;
588 self.tu_comp_dir = comp_dir;
604589
605590 if (self.mtime == null) {
606591 self.mtime = mtime: {
......@@ -610,34 +595,29 @@ pub fn parseDebugInfo(self: *Object, allocator: Allocator) !void {
610595 }
611596}
612597
613pub fn parseDataInCode(self: *Object, allocator: Allocator) !void {
598pub fn parseDataInCode(self: *Object) void {
614599 const index = self.data_in_code_cmd_index orelse return;
615600 const data_in_code = self.load_commands.items[index].linkedit_data;
616
617 var buffer = try allocator.alloc(u8, data_in_code.datasize);
618 defer allocator.free(buffer);
619
620 _ = try self.file.preadAll(buffer, data_in_code.dataoff);
621
622 var stream = io.fixedBufferStream(buffer);
623 var reader = stream.reader();
624 while (true) {
625 const dice = reader.readStruct(macho.data_in_code_entry) catch |err| switch (err) {
626 error.EndOfStream => break,
627 };
628 try self.data_in_code_entries.append(allocator, dice);
629 }
601 const raw_dice = self.contents[data_in_code.dataoff..][0..data_in_code.datasize];
602 self.data_in_code_entries = mem.bytesAsSlice(
603 macho.data_in_code_entry,
604 @alignCast(@alignOf(macho.data_in_code_entry), raw_dice),
605 );
630606}
631607
632fn readSection(self: Object, allocator: Allocator, index: u16) ![]u8 {
608fn getSectionContents(self: Object, sect_id: u16) []const u8 {
633609 const seg = self.load_commands.items[self.segment_cmd_index.?].segment;
634 const sect = seg.sections.items[index];
635 var buffer = try allocator.alloc(u8, @intCast(usize, sect.size));
636 _ = try self.file.preadAll(buffer, sect.offset);
637 return buffer;
610 const sect = seg.sections.items[sect_id];
611 log.debug("getting {s},{s} data at 0x{x} - 0x{x}", .{
612 sect.segName(),
613 sect.sectName(),
614 sect.offset,
615 sect.offset + sect.size,
616 });
617 return self.contents[sect.offset..][0..sect.size];
638618}
639619
640620pub fn getString(self: Object, off: u32) []const u8 {
641 assert(off < self.strtab.items.len);
642 return mem.sliceTo(@ptrCast([*:0]const u8, self.strtab.items.ptr + off), 0);
621 assert(off < self.strtab.len);
622 return mem.sliceTo(@ptrCast([*:0]const u8, self.strtab.ptr + off), 0);
643623}
test/cases/recursive_inline_function.0.zig+1-1
......@@ -9,5 +9,5 @@ inline fn fibonacci(n: usize) usize {
99}
1010
1111// run
12// target=x86_64-linux,arm-linux,x86_64-macos,wasm32-wasi
12// target=x86_64-linux,arm-linux,wasm32-wasi
1313//