| ... | @@ -171,46 +171,52 @@ pub const TextBlock = struct { | ... | @@ -171,46 +171,52 @@ pub const TextBlock = struct { |
| 171 | } | 171 | } |
| 172 | } | 172 | } |
| 173 | | 173 | |
| 174 | pub fn print_this(self: *const TextBlock, zld: *Zld) void { | 174 | pub fn format(self: *const TextBlock, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 175 | log.warn("TextBlock", .{}); | 175 | _ = fmt; |
| 176 | log.warn(" | {}: {}", .{ self.local_sym_index, zld.locals.items[self.local_sym_index] }); | 176 | _ = options; |
| | 177 | try std.fmt.format(writer, "TextBlock {{\n", .{}); |
| | 178 | try std.fmt.format(writer, " {}: {}\n", .{ self.local_sym_index, zld.locals.items[self.local_sym_index] }); |
| 177 | if (self.aliases) |aliases| { | 179 | if (self.aliases) |aliases| { |
| 178 | log.warn(" | aliases:", .{}); | 180 | try std.fmt.format(writer, " aliases:\n", .{}); |
| 179 | for (aliases) |index| { | 181 | for (aliases) |index| { |
| 180 | log.warn(" | {}: {}", .{ index, zld.locals.items[index] }); | 182 | try std.fmt.format(writer, " {}: {}\n", .{ index, zld.locals.items[index] }); |
| 181 | } | 183 | } |
| 182 | } | 184 | } |
| 183 | if (self.references.count() > 0) { | 185 | if (self.references.count() > 0) { |
| 184 | log.warn(" | references:", .{}); | 186 | try std.fmt.format(writer, " references:\n", .{}); |
| 185 | for (self.references.keys()) |index| { | 187 | for (self.references.keys()) |index| { |
| 186 | log.warn(" | {}: {}", .{ index, zld.locals.items[index] }); | 188 | try std.fmt.format(writer, " {}: {}\n", .{ index, zld.locals.items[index] }); |
| 187 | } | 189 | } |
| 188 | } | 190 | } |
| 189 | if (self.contained) |contained| { | 191 | if (self.contained) |contained| { |
| 190 | log.warn(" | contained symbols:", .{}); | 192 | try std.fmt.format(writer, " contained symbols:\n", .{}); |
| 191 | for (contained) |sym_at_off| { | 193 | for (contained) |sym_at_off| { |
| 192 | log.warn(" | {}: {}", .{ sym_at_off.offset, zld.locals.items[sym_at_off.local_sym_index] }); | 194 | try std.fmt.format(writer, " {}: {}\n", .{ |
| | 195 | sym_at_off.offset, |
| | 196 | zld.locals.items[sym_at_off.local_sym_index], |
| | 197 | }); |
| 193 | } | 198 | } |
| 194 | } | 199 | } |
| 195 | log.warn(" | code.len = {}", .{self.code.len}); | 200 | try std.fmt.format(writer, " code.len = {}\n", .{self.code.len}); |
| 196 | if (self.relocs.items.len > 0) { | 201 | if (self.relocs.items.len > 0) { |
| 197 | log.warn(" | relocations:", .{}); | 202 | try std.fmt.format(writer, " relocations:\n", .{}); |
| 198 | for (self.relocs.items) |rel| { | 203 | for (self.relocs.items) |rel| { |
| 199 | log.warn(" | {}", .{rel}); | 204 | try std.fmt.format(writer, " {}\n", .{rel}); |
| 200 | } | 205 | } |
| 201 | } | 206 | } |
| 202 | if (self.rebases.items.len > 0) { | 207 | if (self.rebases.items.len > 0) { |
| 203 | log.warn(" | rebases: {any}", .{self.rebases.items}); | 208 | try std.fmt.format(writer, " rebases: {any}\n", .{self.rebases.items}); |
| 204 | } | 209 | } |
| 205 | log.warn(" | size = {}", .{self.size}); | 210 | try std.fmt.format(writer, " size = {}\n", .{self.size}); |
| 206 | log.warn(" | align = {}", .{self.alignment}); | 211 | try std.fmt.format(writer, " align = {}\n", .{self.alignment}); |
| | 212 | try std.fmt.format(writer, "}}", .{}); |
| 207 | } | 213 | } |
| 208 | | 214 | |
| 209 | pub fn print(self: *const TextBlock, zld: *Zld) void { | 215 | pub fn print(self: *const TextBlock, zld: *Zld) void { |
| 210 | if (self.prev) |prev| { | 216 | if (self.prev) |prev| { |
| 211 | prev.print(zld); | 217 | prev.print(zld); |
| 212 | } | 218 | } |
| 213 | self.print_this(zld); | 219 | log.warn("{}\n", .{self}); |
| 214 | } | 220 | } |
| 215 | }; | 221 | }; |
| 216 | | 222 | |
| ... | @@ -317,15 +323,15 @@ pub fn link(self: *Zld, files: []const []const u8, output: Output, args: LinkArg | ... | @@ -317,15 +323,15 @@ pub fn link(self: *Zld, files: []const []const u8, output: Output, args: LinkArg |
| 317 | self.allocateLinkeditSegment(); | 323 | self.allocateLinkeditSegment(); |
| 318 | try self.allocateTextBlocks(); | 324 | try self.allocateTextBlocks(); |
| 319 | | 325 | |
| 320 | var it = self.blocks.iterator(); | 326 | // var it = self.blocks.iterator(); |
| 321 | while (it.next()) |entry| { | 327 | // while (it.next()) |entry| { |
| 322 | const seg = self.load_commands.items[entry.key_ptr.seg].Segment; | 328 | // const seg = self.load_commands.items[entry.key_ptr.seg].Segment; |
| 323 | const sect = seg.sections.items[entry.key_ptr.sect]; | 329 | // const sect = seg.sections.items[entry.key_ptr.sect]; |
| 324 | | 330 | |
| 325 | log.warn("\n\n{s},{s} contents:", .{ segmentName(sect), sectionName(sect) }); | 331 | // log.warn("\n\n{s},{s} contents:", .{ segmentName(sect), sectionName(sect) }); |
| 326 | log.warn(" {}", .{sect}); | 332 | // log.warn(" {}", .{sect}); |
| 327 | entry.value_ptr.*.print(self); | 333 | // entry.value_ptr.*.print(self); |
| 328 | } | 334 | // } |
| 329 | | 335 | |
| 330 | try self.flush(); | 336 | try self.flush(); |
| 331 | } | 337 | } |
| ... | @@ -1036,7 +1042,7 @@ fn allocateSegment(self: *Zld, index: u16, offset: u64) !void { | ... | @@ -1036,7 +1042,7 @@ fn allocateSegment(self: *Zld, index: u16, offset: u64) !void { |
| 1036 | } | 1042 | } |
| 1037 | | 1043 | |
| 1038 | fn allocateTextBlocks(self: *Zld) !void { | 1044 | fn allocateTextBlocks(self: *Zld) !void { |
| 1039 | log.warn("allocating text blocks", .{}); | 1045 | log.debug("allocating text blocks", .{}); |
| 1040 | | 1046 | |
| 1041 | var it = self.blocks.iterator(); | 1047 | var it = self.blocks.iterator(); |
| 1042 | while (it.next()) |entry| { | 1048 | while (it.next()) |entry| { |
| ... | @@ -1047,8 +1053,8 @@ fn allocateTextBlocks(self: *Zld) !void { | ... | @@ -1047,8 +1053,8 @@ fn allocateTextBlocks(self: *Zld) !void { |
| 1047 | const sect = seg.sections.items[match.sect]; | 1053 | const sect = seg.sections.items[match.sect]; |
| 1048 | var base_addr: u64 = sect.addr + sect.size; | 1054 | var base_addr: u64 = sect.addr + sect.size; |
| 1049 | | 1055 | |
| 1050 | log.warn(" within section {s},{s}", .{ segmentName(sect), sectionName(sect) }); | 1056 | log.debug(" within section {s},{s}", .{ segmentName(sect), sectionName(sect) }); |
| 1051 | log.warn(" {}", .{sect}); | 1057 | log.debug(" {}", .{sect}); |
| 1052 | | 1058 | |
| 1053 | while (true) { | 1059 | while (true) { |
| 1054 | const block_alignment = try math.powi(u32, 2, block.alignment); | 1060 | const block_alignment = try math.powi(u32, 2, block.alignment); |
| ... | @@ -1058,7 +1064,7 @@ fn allocateTextBlocks(self: *Zld) !void { | ... | @@ -1058,7 +1064,7 @@ fn allocateTextBlocks(self: *Zld) !void { |
| 1058 | assert(sym.payload == .regular); | 1064 | assert(sym.payload == .regular); |
| 1059 | sym.payload.regular.address = base_addr; | 1065 | sym.payload.regular.address = base_addr; |
| 1060 | | 1066 | |
| 1061 | log.warn(" {s}: start=0x{x}, end=0x{x}, size={}, align={}", .{ | 1067 | log.debug(" {s}: start=0x{x}, end=0x{x}, size={}, align={}", .{ |
| 1062 | sym.name, | 1068 | sym.name, |
| 1063 | base_addr, | 1069 | base_addr, |
| 1064 | base_addr + block.size, | 1070 | base_addr + block.size, |
| ... | @@ -1092,7 +1098,7 @@ fn allocateTextBlocks(self: *Zld) !void { | ... | @@ -1092,7 +1098,7 @@ fn allocateTextBlocks(self: *Zld) !void { |
| 1092 | } | 1098 | } |
| 1093 | | 1099 | |
| 1094 | fn writeTextBlocks(self: *Zld) !void { | 1100 | fn writeTextBlocks(self: *Zld) !void { |
| 1095 | log.warn("writing text blocks", .{}); | 1101 | log.debug("writing text blocks", .{}); |
| 1096 | | 1102 | |
| 1097 | var it = self.blocks.iterator(); | 1103 | var it = self.blocks.iterator(); |
| 1098 | while (it.next()) |entry| { | 1104 | while (it.next()) |entry| { |
| ... | @@ -1103,8 +1109,8 @@ fn writeTextBlocks(self: *Zld) !void { | ... | @@ -1103,8 +1109,8 @@ fn writeTextBlocks(self: *Zld) !void { |
| 1103 | const sect = seg.sections.items[match.sect]; | 1109 | const sect = seg.sections.items[match.sect]; |
| 1104 | const sect_type = sectionType(sect); | 1110 | const sect_type = sectionType(sect); |
| 1105 | | 1111 | |
| 1106 | log.warn(" for section {s},{s}", .{ segmentName(sect), sectionName(sect) }); | 1112 | log.debug(" for section {s},{s}", .{ segmentName(sect), sectionName(sect) }); |
| 1107 | log.warn(" {}", .{sect}); | 1113 | log.debug(" {}", .{sect}); |
| 1108 | | 1114 | |
| 1109 | var code = try self.allocator.alloc(u8, sect.size); | 1115 | var code = try self.allocator.alloc(u8, sect.size); |
| 1110 | defer self.allocator.free(code); | 1116 | defer self.allocator.free(code); |
| ... | @@ -1120,7 +1126,7 @@ fn writeTextBlocks(self: *Zld) !void { | ... | @@ -1120,7 +1126,7 @@ fn writeTextBlocks(self: *Zld) !void { |
| 1120 | const aligned_base_off = mem.alignBackwardGeneric(u64, unaligned_base_off, block_alignment); | 1126 | const aligned_base_off = mem.alignBackwardGeneric(u64, unaligned_base_off, block_alignment); |
| 1121 | | 1127 | |
| 1122 | const sym = self.locals.items[block.local_sym_index]; | 1128 | const sym = self.locals.items[block.local_sym_index]; |
| 1123 | log.warn(" {s}: start=0x{x}, end=0x{x}, size={}, align={}", .{ | 1129 | log.debug(" {s}: start=0x{x}, end=0x{x}, size={}, align={}", .{ |
| 1124 | sym.name, | 1130 | sym.name, |
| 1125 | aligned_base_off, | 1131 | aligned_base_off, |
| 1126 | aligned_base_off + block.size, | 1132 | aligned_base_off + block.size, |