| ... | ... | @@ -14,6 +14,7 @@ const Allocator = mem.Allocator; |
| 14 | 14 | const Relocation = reloc.Relocation; |
| 15 | 15 | const Symbol = @import("Symbol.zig"); |
| 16 | 16 | const parseName = @import("Zld.zig").parseName; |
| 17 | const CppStatic = @import("Zld.zig").CppStatic; |
| 17 | 18 | |
| 18 | 19 | usingnamespace @import("commands.zig"); |
| 19 | 20 | |
| ... | ... | @@ -216,6 +217,7 @@ pub fn parse(self: *Object) !void { |
| 216 | 217 | try self.parseSections(); |
| 217 | 218 | if (self.symtab_cmd_index != null) try self.parseSymtab(); |
| 218 | 219 | if (self.data_in_code_cmd_index != null) try self.readDataInCode(); |
| 220 | try self.parseInitializers(); |
| 219 | 221 | try self.parseDebugInfo(); |
| 220 | 222 | } |
| 221 | 223 | |
| ... | ... | @@ -298,28 +300,42 @@ pub fn parseSections(self: *Object) !void { |
| 298 | 300 | var section = Section{ |
| 299 | 301 | .inner = sect, |
| 300 | 302 | .code = code, |
| 301 | | .relocs = undefined, |
| 303 | .relocs = null, |
| 302 | 304 | }; |
| 303 | 305 | |
| 304 | 306 | // Parse relocations |
| 305 | | section.relocs = if (sect.nreloc > 0) relocs: { |
| 307 | if (sect.nreloc > 0) { |
| 306 | 308 | var raw_relocs = try self.allocator.alloc(u8, @sizeOf(macho.relocation_info) * sect.nreloc); |
| 307 | 309 | defer self.allocator.free(raw_relocs); |
| 308 | 310 | |
| 309 | 311 | _ = try self.file.?.preadAll(raw_relocs, sect.reloff); |
| 310 | 312 | |
| 311 | | break :relocs try reloc.parse( |
| 313 | section.relocs = try reloc.parse( |
| 312 | 314 | self.allocator, |
| 313 | 315 | self.arch.?, |
| 314 | 316 | section.code, |
| 315 | 317 | mem.bytesAsSlice(macho.relocation_info, raw_relocs), |
| 316 | 318 | ); |
| 317 | | } else null; |
| 319 | } |
| 318 | 320 | |
| 319 | 321 | self.sections.appendAssumeCapacity(section); |
| 320 | 322 | } |
| 321 | 323 | } |
| 322 | 324 | |
| 325 | pub fn parseInitializers(self: *Object) !void { |
| 326 | for (self.sections.items) |section| { |
| 327 | if (section.inner.flags != macho.S_MOD_INIT_FUNC_POINTERS) continue; |
| 328 | log.warn("parsing initializers in {s}", .{self.name.?}); |
| 329 | // Parse C++ initializers |
| 330 | const relocs = section.relocs orelse unreachable; |
| 331 | for (relocs) |rel| { |
| 332 | const sym = self.symtab.items[rel.target.symbol]; |
| 333 | const sym_name = self.getString(sym.n_strx); |
| 334 | log.warn(" | {s}", .{sym_name}); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 323 | 339 | pub fn parseSymtab(self: *Object) !void { |
| 324 | 340 | const symtab_cmd = self.load_commands.items[self.symtab_cmd_index.?].Symtab; |
| 325 | 341 | |