| ... | @@ -2298,9 +2298,16 @@ pub const Zld = struct { | ... | @@ -2298,9 +2298,16 @@ pub const Zld = struct { |
| 2298 | | 2298 | |
| 2299 | const asc_u64 = std.sort.asc(u64); | 2299 | const asc_u64 = std.sort.asc(u64); |
| 2300 | | 2300 | |
| | 2301 | fn addSymbolToFunctionStarts(self: *Zld, sym_loc: SymbolWithLoc, addresses: *std.ArrayList(u64)) !void { |
| | 2302 | const sym = self.getSymbol(sym_loc); |
| | 2303 | if (sym.n_strx == 0) return; |
| | 2304 | if (sym.n_desc == N_DEAD) return; |
| | 2305 | if (self.symbolIsTemp(sym_loc)) return; |
| | 2306 | try addresses.append(sym.n_value); |
| | 2307 | } |
| | 2308 | |
| 2301 | fn writeFunctionStarts(self: *Zld) !void { | 2309 | fn writeFunctionStarts(self: *Zld) !void { |
| 2302 | const text_seg_index = self.getSegmentByName("__TEXT") orelse return; | 2310 | const text_seg_index = self.getSegmentByName("__TEXT") orelse return; |
| 2303 | const text_sect_index = self.getSectionByName("__TEXT", "__text") orelse return; | | |
| 2304 | const text_seg = self.segments.items[text_seg_index]; | 2311 | const text_seg = self.segments.items[text_seg_index]; |
| 2305 | | 2312 | |
| 2306 | const gpa = self.gpa; | 2313 | const gpa = self.gpa; |
| ... | @@ -2308,17 +2315,18 @@ pub const Zld = struct { | ... | @@ -2308,17 +2315,18 @@ pub const Zld = struct { |
| 2308 | // We need to sort by address first | 2315 | // We need to sort by address first |
| 2309 | var addresses = std.ArrayList(u64).init(gpa); | 2316 | var addresses = std.ArrayList(u64).init(gpa); |
| 2310 | defer addresses.deinit(); | 2317 | defer addresses.deinit(); |
| 2311 | try addresses.ensureTotalCapacityPrecise(self.globals.items.len); | | |
| 2312 | | | |
| 2313 | for (self.globals.items) |global| { | | |
| 2314 | const sym = self.getSymbol(global); | | |
| 2315 | if (sym.undf()) continue; | | |
| 2316 | if (sym.n_desc == N_DEAD) continue; | | |
| 2317 | | 2318 | |
| 2318 | const sect_id = sym.n_sect - 1; | 2319 | for (self.objects.items) |object| { |
| 2319 | if (sect_id != text_sect_index) continue; | 2320 | for (object.exec_atoms.items) |atom_index| { |
| | 2321 | const atom = self.getAtom(atom_index); |
| | 2322 | const sym_loc = atom.getSymbolWithLoc(); |
| | 2323 | try self.addSymbolToFunctionStarts(sym_loc, &addresses); |
| 2320 | | 2324 | |
| 2321 | addresses.appendAssumeCapacity(sym.n_value); | 2325 | var it = Atom.getInnerSymbolsIterator(self, atom_index); |
| | 2326 | while (it.next()) |inner_sym_loc| { |
| | 2327 | try self.addSymbolToFunctionStarts(inner_sym_loc, &addresses); |
| | 2328 | } |
| | 2329 | } |
| 2322 | } | 2330 | } |
| 2323 | | 2331 | |
| 2324 | std.sort.sort(u64, addresses.items, {}, asc_u64); | 2332 | std.sort.sort(u64, addresses.items, {}, asc_u64); |
| ... | @@ -2457,6 +2465,7 @@ pub const Zld = struct { | ... | @@ -2457,6 +2465,7 @@ pub const Zld = struct { |
| 2457 | fn addLocalToSymtab(self: *Zld, sym_loc: SymbolWithLoc, locals: *std.ArrayList(macho.nlist_64)) !void { | 2465 | fn addLocalToSymtab(self: *Zld, sym_loc: SymbolWithLoc, locals: *std.ArrayList(macho.nlist_64)) !void { |
| 2458 | const sym = self.getSymbol(sym_loc); | 2466 | const sym = self.getSymbol(sym_loc); |
| 2459 | if (sym.n_strx == 0) return; // no name, skip | 2467 | if (sym.n_strx == 0) return; // no name, skip |
| | 2468 | if (sym.n_desc == N_DEAD) return; // garbage-collected, skip |
| 2460 | if (sym.ext()) return; // an export lands in its own symtab section, skip | 2469 | if (sym.ext()) return; // an export lands in its own symtab section, skip |
| 2461 | if (self.symbolIsTemp(sym_loc)) return; // local temp symbol, skip | 2470 | if (self.symbolIsTemp(sym_loc)) return; // local temp symbol, skip |
| 2462 | | 2471 | |