authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-19 22:03:29+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-24 14:45:45+02:00
log4854c96bc55d73797abe042c02d1f714945d1c25
treece0c84d8fd4414933494cb506742f341d4303cd3
parent96a0479db2d876bc1b3136b26cff4004a212ba6a

zld: create a synthetic ___dso_handle symbol self-referenced


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

src/link/MachO/Symbol.zig+2-1
...@@ -85,7 +85,8 @@ pub const Proxy = struct {...@@ -85,7 +85,8 @@ pub const Proxy = struct {
85 base: Symbol,85 base: Symbol,
8686
87 /// Dylib where to locate this symbol.87 /// Dylib where to locate this symbol.
88 dylib: *Dylib,88 /// null means self-reference.
89 dylib: ?*Dylib = null,
8990
90 pub const base_type: Symbol.Type = .proxy;91 pub const base_type: Symbol.Type = .proxy;
91};92};
src/link/MachO/Zld.zig+23-4
...@@ -1919,6 +1919,24 @@ fn resolveSymbols(self: *Zld) !void {...@@ -1919,6 +1919,24 @@ fn resolveSymbols(self: *Zld) !void {
1919 }1919 }
19201920
1921 if (!found) {1921 if (!found) {
1922 if (mem.eql(u8, undef.name, "___dso_handle")) {
1923 const proxy = self.imports.get(undef.name) orelse blk: {
1924 const name = try self.allocator.dupe(u8, undef.name);
1925 const proxy = try self.allocator.create(Symbol.Proxy);
1926 errdefer self.allocator.destroy(proxy);
1927 proxy.* = .{
1928 .base = .{
1929 .@"type" = .proxy,
1930 .name = name,
1931 },
1932 };
1933 try self.imports.putNoClobber(self.allocator, name, &proxy.base);
1934 break :blk &proxy.base;
1935 };
1936 undef.alias = proxy;
1937 continue;
1938 }
1939
1922 log.err("undefined reference to symbol '{s}'", .{undef.name});1940 log.err("undefined reference to symbol '{s}'", .{undef.name});
1923 log.err(" | referenced in {s}", .{1941 log.err(" | referenced in {s}", .{
1924 undef.cast(Symbol.Unresolved).?.file.name.?,1942 undef.cast(Symbol.Unresolved).?.file.name.?,
...@@ -2796,7 +2814,7 @@ fn writeBindInfoTable(self: *Zld) !void {...@@ -2796,7 +2814,7 @@ fn writeBindInfoTable(self: *Zld) !void {
2796 try pointers.append(.{2814 try pointers.append(.{
2797 .offset = base_offset + proxy.base.got_index.? * @sizeOf(u64),2815 .offset = base_offset + proxy.base.got_index.? * @sizeOf(u64),
2798 .segment_id = segment_id,2816 .segment_id = segment_id,
2799 .dylib_ordinal = proxy.dylib.ordinal.?,2817 .dylib_ordinal = if (proxy.dylib) |dylib| dylib.ordinal.? else 0,
2800 .name = proxy.base.name,2818 .name = proxy.base.name,
2801 });2819 });
2802 }2820 }
...@@ -2815,7 +2833,7 @@ fn writeBindInfoTable(self: *Zld) !void {...@@ -2815,7 +2833,7 @@ fn writeBindInfoTable(self: *Zld) !void {
2815 try pointers.append(.{2833 try pointers.append(.{
2816 .offset = base_offset,2834 .offset = base_offset,
2817 .segment_id = segment_id,2835 .segment_id = segment_id,
2818 .dylib_ordinal = proxy.dylib.ordinal.?,2836 .dylib_ordinal = if (proxy.dylib) |dylib| dylib.ordinal.? else 0,
2819 .name = proxy.base.name,2837 .name = proxy.base.name,
2820 });2838 });
2821 }2839 }
...@@ -2855,7 +2873,7 @@ fn writeLazyBindInfoTable(self: *Zld) !void {...@@ -2855,7 +2873,7 @@ fn writeLazyBindInfoTable(self: *Zld) !void {
2855 pointers.appendAssumeCapacity(.{2873 pointers.appendAssumeCapacity(.{
2856 .offset = base_offset + sym.stubs_index.? * @sizeOf(u64),2874 .offset = base_offset + sym.stubs_index.? * @sizeOf(u64),
2857 .segment_id = segment_id,2875 .segment_id = segment_id,
2858 .dylib_ordinal = proxy.dylib.ordinal.?,2876 .dylib_ordinal = if (proxy.dylib) |dylib| dylib.ordinal.? else 0,
2859 .name = sym.name,2877 .name = sym.name,
2860 });2878 });
2861 }2879 }
...@@ -3163,11 +3181,12 @@ fn writeSymbolTable(self: *Zld) !void {...@@ -3163,11 +3181,12 @@ fn writeSymbolTable(self: *Zld) !void {
31633181
3164 for (self.imports.values()) |sym| {3182 for (self.imports.values()) |sym| {
3165 const proxy = sym.cast(Symbol.Proxy) orelse unreachable;3183 const proxy = sym.cast(Symbol.Proxy) orelse unreachable;
3184 const dylib_ordinal = if (proxy.dylib) |dylib| dylib.ordinal.? else 0;
3166 try undefs.append(.{3185 try undefs.append(.{
3167 .n_strx = try self.makeString(sym.name),3186 .n_strx = try self.makeString(sym.name),
3168 .n_type = macho.N_UNDF | macho.N_EXT,3187 .n_type = macho.N_UNDF | macho.N_EXT,
3169 .n_sect = 0,3188 .n_sect = 0,
3170 .n_desc = (proxy.dylib.ordinal.? * macho.N_SYMBOL_RESOLVER) | macho.REFERENCE_FLAG_UNDEFINED_NON_LAZY,3189 .n_desc = (dylib_ordinal * macho.N_SYMBOL_RESOLVER) | macho.REFERENCE_FLAG_UNDEFINED_NON_LAZY,
3171 .n_value = 0,3190 .n_value = 0,
3172 });3191 });
3173 }3192 }