Commit 570660bb46

Jakub Konka <kubkon@jakubkonka.com>
2021-07-11 10:04:13
zld: ___dso_handle is regular at 0x100000000
which points at the start of the __TEXT segment. Also, ensure C++ initializers and terminators are rebased.
1 parent 9e051e3
Changed files (3)
src/link/MachO/Object.zig
@@ -530,7 +530,7 @@ pub fn parseTextBlocks(self: *Object, zld: *Zld) !void {
                     const reg = &sym.payload.regular;
                     if (reg.file) |file| {
                         if (file != self) {
-                            log.warn("deduping definition of {s} in {s}", .{ sym.name, self.name.? });
+                            log.debug("deduping definition of {s} in {s}", .{ sym.name, self.name.? });
                             block.deinit();
                             self.allocator.destroy(block);
                             continue;
src/link/MachO/reloc.zig
@@ -411,6 +411,8 @@ pub const Relocation = struct {
     };
 
     pub fn resolve(self: Relocation, zld: *Zld) !void {
+        log.debug("relocating {}", .{self});
+
         const source_addr = blk: {
             const sym = zld.locals.items[self.block.local_sym_index];
             break :blk sym.payload.regular.address + self.offset;
@@ -497,7 +499,6 @@ pub const Relocation = struct {
             }
         };
 
-        log.debug("relocating {}", .{self});
         log.debug("  | source_addr = 0x{x}", .{source_addr});
         log.debug("  | target_addr = 0x{x}", .{target_addr});
 
@@ -703,7 +704,9 @@ pub const Parser = struct {
 
                             if (!is_right_segment) break :rebase false;
                             if (sect_type != macho.S_LITERAL_POINTERS and
-                                sect_type != macho.S_REGULAR)
+                                sect_type != macho.S_REGULAR and
+                                sect_type != macho.S_MOD_INIT_FUNC_POINTERS and
+                                sect_type != macho.S_MOD_TERM_FUNC_POINTERS)
                             {
                                 break :rebase false;
                             }
src/link/MachO/Zld.zig
@@ -1686,10 +1686,16 @@ fn resolveSymbols(self: *Zld) !void {
     // Fourth pass, handle synthetic symbols and flag any undefined references.
     if (self.globals.get("___dso_handle")) |symbol| {
         if (symbol.payload == .undef) {
+            const seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
             symbol.payload = .{
-                .proxy = .{},
+                .regular = .{
+                    .linkage = .translation_unit,
+                    .address = seg.inner.vmaddr,
+                    .weak_ref = true,
+                    .local_sym_index = @intCast(u32, self.locals.items.len),
+                },
             };
-            try self.imports.append(self.allocator, symbol);
+            try self.locals.append(self.allocator, symbol);
         }
     }