authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-07-05 07:39:49+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-07-15 18:49:47+02:00
log5649242025cd885a6a2f0607d96f54b1926b0a5a
tree749d2f72120be59d1d564dabf150887872853978
parent5b3c4691e628cb288e6595974781ffbadb717c28

zld: draft up final format of TextBlock


2 files changed, 70 insertions(+), 5 deletions(-)

src/link/MachO/Object.zig+49-1
......@@ -343,7 +343,7 @@ pub fn parseSections(self: *Object) !void {
343343 }
344344}
345345
346pub fn parseTextBlocks(self: *Object, zld: *Zld) !void {
346pub fn parseTextBlocks(self: *Object, zld: *Zld) !*TextBlock {
347347 const seg = self.load_commands.items[self.segment_cmd_index.?].Segment;
348348
349349 log.warn("analysing {s}", .{self.name.?});
......@@ -503,6 +503,54 @@ pub fn parseTextBlocks(self: *Object, zld: *Zld) !void {
503503 }
504504}
505505
506const SectionAsTextBlocksArgs = struct {
507 sect: macho.section_64,
508 code: []u8,
509 subsections_via_symbols: bool = false,
510 relocs: ?[]macho.relocation_info = null,
511 segment_id: u16 = 0,
512 section_id: u16 = 0,
513};
514
515fn sectionAsTextBlocks(self: *Object, args: SectionAsTextBlocksArgs) !*TextBlock {
516 const sect = args.sect;
517
518 log.warn("putting section '{s},{s}' as a TextBlock", .{ segmentName(sect), sectionName(sect) });
519
520 // Section alignment will be the assumed alignment per symbol.
521 const alignment = sect.@"align";
522
523 const first_block: *TextBlock = blk: {
524 if (args.subsections_via_symbols) {
525 return error.TODO;
526 } else {
527 const block = try self.allocator.create(TextBlock);
528 errdefer self.allocator.destroy(block);
529
530 block.* = .{
531 .ref = .{
532 .section = undefined, // Will be populated when we allocated final sections.
533 },
534 .code = args.code,
535 .relocs = null,
536 .size = sect.size,
537 .alignment = alignment,
538 .segment_id = args.segment_id,
539 .section_id = args.section_id,
540 };
541
542 // TODO parse relocs
543 if (args.relocs) |relocs| {
544 block.relocs = try reloc.parse(self.allocator, self.arch.?, args.code, relocs, symbols);
545 }
546
547 break :blk block;
548 }
549 };
550
551 return first_block;
552}
553
506554pub fn parseInitializers(self: *Object) !void {
507555 const index = self.mod_init_func_section_index orelse return;
508556 const section = self.sections.items[index];
src/link/MachO/Zld.zig+21-4
......@@ -135,13 +135,30 @@ const TlvOffset = struct {
135135};
136136
137137pub const TextBlock = struct {
138 local_sym_index: ?u32 = null,
138 allocator: *Allocator,
139 local_sym_index: u32,
140 aliases: std.ArrayList(u32),
141 references: std.ArrayList(u32),
142 code: []u8,
143 relocs: ?std.ArrayList(*Relocation) = null,
139144 size: u64,
140145 alignment: u32,
141 code: []u8,
142 relocs: []*Relocation,
143146 segment_id: u16,
144147 section_id: u16,
148 next: ?*TextBlock = null,
149 prev: ?*TextBlock = null,
150
151 pub fn deinit(block: *TextBlock, allocator: *Allocator) void {
152 block.aliases.deinit();
153 block.references.deinit();
154 if (block.relocs) |relocs| {
155 for (relocs.items) |reloc| {
156 allocator.destroy(reloc);
157 }
158 relocs.deinit();
159 }
160 allocator.free(code);
161 }
145162};
146163
147164/// Default path to dyld
......@@ -1669,7 +1686,7 @@ fn resolveSymbols(self: *Zld) !void {
16691686
16701687fn parseTextBlocks(self: *Zld) !void {
16711688 for (self.objects.items) |object| {
1672 try object.parseTextBlocks(self);
1689 _ = try object.parseTextBlocks(self);
16731690 }
16741691}
16751692