authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-04-21 17:53:07+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-04-24 08:13:53+02:00
logb03983b4501031e6faed47bf886586b0ad2150aa
tree0f410332cc8949d148b4741e0e7c7135f8a92bfd
parent4f7765de7c0203ceed3a5acbe0d4c07b678557b5

zld: analyze static initializers


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

src/link/MachO/Object.zig+20-4
...@@ -14,6 +14,7 @@ const Allocator = mem.Allocator;...@@ -14,6 +14,7 @@ const Allocator = mem.Allocator;
14const Relocation = reloc.Relocation;14const Relocation = reloc.Relocation;
15const Symbol = @import("Symbol.zig");15const Symbol = @import("Symbol.zig");
16const parseName = @import("Zld.zig").parseName;16const parseName = @import("Zld.zig").parseName;
17const CppStatic = @import("Zld.zig").CppStatic;
1718
18usingnamespace @import("commands.zig");19usingnamespace @import("commands.zig");
1920
...@@ -216,6 +217,7 @@ pub fn parse(self: *Object) !void {...@@ -216,6 +217,7 @@ pub fn parse(self: *Object) !void {
216 try self.parseSections();217 try self.parseSections();
217 if (self.symtab_cmd_index != null) try self.parseSymtab();218 if (self.symtab_cmd_index != null) try self.parseSymtab();
218 if (self.data_in_code_cmd_index != null) try self.readDataInCode();219 if (self.data_in_code_cmd_index != null) try self.readDataInCode();
220 try self.parseInitializers();
219 try self.parseDebugInfo();221 try self.parseDebugInfo();
220}222}
221223
...@@ -298,28 +300,42 @@ pub fn parseSections(self: *Object) !void {...@@ -298,28 +300,42 @@ pub fn parseSections(self: *Object) !void {
298 var section = Section{300 var section = Section{
299 .inner = sect,301 .inner = sect,
300 .code = code,302 .code = code,
301 .relocs = undefined,303 .relocs = null,
302 };304 };
303305
304 // Parse relocations306 // Parse relocations
305 section.relocs = if (sect.nreloc > 0) relocs: {307 if (sect.nreloc > 0) {
306 var raw_relocs = try self.allocator.alloc(u8, @sizeOf(macho.relocation_info) * sect.nreloc);308 var raw_relocs = try self.allocator.alloc(u8, @sizeOf(macho.relocation_info) * sect.nreloc);
307 defer self.allocator.free(raw_relocs);309 defer self.allocator.free(raw_relocs);
308310
309 _ = try self.file.?.preadAll(raw_relocs, sect.reloff);311 _ = try self.file.?.preadAll(raw_relocs, sect.reloff);
310312
311 break :relocs try reloc.parse(313 section.relocs = try reloc.parse(
312 self.allocator,314 self.allocator,
313 self.arch.?,315 self.arch.?,
314 section.code,316 section.code,
315 mem.bytesAsSlice(macho.relocation_info, raw_relocs),317 mem.bytesAsSlice(macho.relocation_info, raw_relocs),
316 );318 );
317 } else null;319 }
318320
319 self.sections.appendAssumeCapacity(section);321 self.sections.appendAssumeCapacity(section);
320 }322 }
321}323}
322324
325pub 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
323pub fn parseSymtab(self: *Object) !void {339pub fn parseSymtab(self: *Object) !void {
324 const symtab_cmd = self.load_commands.items[self.symtab_cmd_index.?].Symtab;340 const symtab_cmd = self.load_commands.items[self.symtab_cmd_index.?].Symtab;
325341
src/link/MachO/Zld.zig+1-1
...@@ -90,7 +90,7 @@ stub_helper_stubs_start_off: ?u64 = null,...@@ -90,7 +90,7 @@ stub_helper_stubs_start_off: ?u64 = null,
90mappings: std.AutoHashMapUnmanaged(MappingKey, SectionMapping) = .{},90mappings: std.AutoHashMapUnmanaged(MappingKey, SectionMapping) = .{},
91unhandled_sections: std.AutoHashMapUnmanaged(MappingKey, u0) = .{},91unhandled_sections: std.AutoHashMapUnmanaged(MappingKey, u0) = .{},
9292
93const CppStatic = struct {93pub const CppStatic = struct {
94 index: u32,94 index: u32,
95 target_addr: u64,95 target_addr: u64,
96 file: u16,96 file: u16,