authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-01-21 16:53:46+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-01-21 16:53:46+01:00
log983e37340913383647727a7118c2824baf23d2ea
treed1a4688a0b4dea90597022bb1ea39275ae61abc2
parent02db9933a4a0a99836f33757932f07263945166a

macho: fix sorting symbols by seniority


2 files changed, 20 insertions(+), 14 deletions(-)

src/link/MachO/Object.zig+18-14
......@@ -242,6 +242,17 @@ const SymbolAtIndex = struct {
242242 return mem.sliceTo(@ptrCast([*:0]const u8, ctx.in_strtab.?.ptr + off), 0);
243243 }
244244
245 fn getSymbolSeniority(self: SymbolAtIndex, ctx: Context) u2 {
246 const sym = self.getSymbol(ctx);
247 if (!sym.ext()) {
248 const sym_name = self.getSymbolName(ctx);
249 if (mem.startsWith(u8, sym_name, "l") or mem.startsWith(u8, sym_name, "L")) return 0;
250 return 1;
251 }
252 if (sym.weakDef() or sym.pext()) return 2;
253 return 3;
254 }
255
245256 /// Performs lexicographic-like check.
246257 /// * lhs and rhs defined
247258 /// * if lhs == rhs
......@@ -256,23 +267,15 @@ const SymbolAtIndex = struct {
256267 if (lhs.sect() and rhs.sect()) {
257268 if (lhs.n_value == rhs.n_value) {
258269 if (lhs.n_sect == rhs.n_sect) {
259 if (lhs.ext() and rhs.ext()) {
260 if ((lhs.pext() or lhs.weakDef()) and (rhs.pext() or rhs.weakDef())) {
261 return false;
262 } else return rhs.pext() or rhs.weakDef();
263 } else {
264 const lhs_name = lhs_index.getSymbolName(ctx);
265 const lhs_temp = mem.startsWith(u8, lhs_name, "l") or mem.startsWith(u8, lhs_name, "L");
266 const rhs_name = rhs_index.getSymbolName(ctx);
267 const rhs_temp = mem.startsWith(u8, rhs_name, "l") or mem.startsWith(u8, rhs_name, "L");
268 if (lhs_temp and rhs_temp) {
269 return false;
270 } else return rhs_temp;
271 }
270 const lhs_senior = lhs_index.getSymbolSeniority(ctx);
271 const rhs_senior = rhs_index.getSymbolSeniority(ctx);
272 if (lhs_senior == rhs_senior) {
273 return lessThanByNStrx(ctx, lhs_index, rhs_index);
274 } else return lhs_senior < rhs_senior;
272275 } else return lhs.n_sect < rhs.n_sect;
273276 } else return lhs.n_value < rhs.n_value;
274277 } else if (lhs.undf() and rhs.undf()) {
275 return false;
278 return lessThanByNStrx(ctx, lhs_index, rhs_index);
276279 } else return rhs.undf();
277280 }
278281
......@@ -786,6 +789,7 @@ fn parseUnwindInfo(self: *Object, zld: *Zld, object_id: u32) !void {
786789 mem.asBytes(&record),
787790 @intCast(i32, offset),
788791 );
792 log.debug("unwind record {d} tracks {s}", .{ record_id, zld.getSymbolName(target) });
789793 if (target.getFile() != object_id) {
790794 self.unwind_relocs_lookup[record_id].dead = true;
791795 } else {
test/link/macho/weak_library/build.zig+2
......@@ -31,6 +31,8 @@ pub fn build(b: *Builder) void {
3131
3232 check.checkInSymtab();
3333 check.checkNext("(undefined) weak external _a (from liba)");
34
35 check.checkInSymtab();
3436 check.checkNext("(undefined) weak external _asStr (from liba)");
3537
3638 const run_cmd = check.runAndCompare();