| author | |
| committer | |
| log | 7f01b61679999bcd0ff644632a26e8c35e7541b6 |
| tree | 78560ee683f038776ec84e2959479e1a67498635 |
| parent | bdbb1dbe1535b727e542c80fe1f7d62a78e527fd |
3 files changed, 69 insertions(+), 39 deletions(-)
src/link/MachO.zig+2-2| ... | @@ -417,8 +417,8 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node | ... | @@ -417,8 +417,8 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node |
| 417 | if (comp.verbose_link) try self.dumpArgv(comp); | 417 | if (comp.verbose_link) try self.dumpArgv(comp); |
| 418 | 418 | ||
| 419 | if (self.getZigObject()) |zo| try zo.flushModule(self); | 419 | if (self.getZigObject()) |zo| try zo.flushModule(self); |
| 420 | if (self.base.isStaticLib()) return Archive.flush(self, comp, module_obj_path); | 420 | if (self.base.isStaticLib()) return relocatable.flushStaticLib(self, comp, module_obj_path); |
| 421 | if (self.base.isObject()) return relocatable.flush(self, comp, module_obj_path); | 421 | if (self.base.isObject()) return relocatable.flushObject(self, comp, module_obj_path); |
| 422 | 422 | ||
| 423 | var positionals = std.ArrayList(Compilation.LinkObject).init(gpa); | 423 | var positionals = std.ArrayList(Compilation.LinkObject).init(gpa); |
| 424 | defer positionals.deinit(); | 424 | defer positionals.deinit(); |
src/link/MachO/Archive.zig-36| ... | @@ -143,51 +143,15 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: | ... | @@ -143,51 +143,15 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: |
| 143 | } | 143 | } |
| 144 | } | 144 | } |
| 145 | 145 | ||
| 146 | pub fn flush(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u8) link.File.FlushError!void { | ||
| 147 | const gpa = comp.gpa; | ||
| 148 | |||
| 149 | var positionals = std.ArrayList(Compilation.LinkObject).init(gpa); | ||
| 150 | defer positionals.deinit(); | ||
| 151 | |||
| 152 | try positionals.ensureUnusedCapacity(comp.objects.len); | ||
| 153 | positionals.appendSliceAssumeCapacity(comp.objects); | ||
| 154 | |||
| 155 | for (comp.c_object_table.keys()) |key| { | ||
| 156 | try positionals.append(.{ .path = key.status.success.object_path }); | ||
| 157 | } | ||
| 158 | |||
| 159 | if (module_obj_path) |path| try positionals.append(.{ .path = path }); | ||
| 160 | |||
| 161 | for (positionals.items) |obj| { | ||
| 162 | // TODO: parse for archive meaning don't unpack objects | ||
| 163 | _ = obj; | ||
| 164 | } | ||
| 165 | |||
| 166 | if (comp.link_errors.items.len > 0) return error.FlushFailure; | ||
| 167 | |||
| 168 | // First, we flush relocatable object file generated with our backends. | ||
| 169 | if (macho_file.getZigObject()) |zo| { | ||
| 170 | zo.resolveSymbols(macho_file); | ||
| 171 | zo.asFile().claimUnresolvedRelocatable(macho_file); | ||
| 172 | } | ||
| 173 | |||
| 174 | var err = try macho_file.addErrorWithNotes(0); | ||
| 175 | try err.addMsg(macho_file, "TODO implement flushStaticLib", .{}); | ||
| 176 | |||
| 177 | return error.FlushFailure; | ||
| 178 | } | ||
| 179 | |||
| 180 | const fat = @import("fat.zig"); | 146 | const fat = @import("fat.zig"); |
| 181 | const link = @import("../../link.zig"); | 147 | const link = @import("../../link.zig"); |
| 182 | const log = std.log.scoped(.link); | 148 | const log = std.log.scoped(.link); |
| 183 | const macho = std.macho; | 149 | const macho = std.macho; |
| 184 | const mem = std.mem; | 150 | const mem = std.mem; |
| 185 | const relocatable = @import("relocatable.zig"); | ||
| 186 | const std = @import("std"); | 151 | const std = @import("std"); |
| 187 | 152 | ||
| 188 | const Allocator = mem.Allocator; | 153 | const Allocator = mem.Allocator; |
| 189 | const Archive = @This(); | 154 | const Archive = @This(); |
| 190 | const Compilation = @import("../../Compilation.zig"); | ||
| 191 | const File = @import("file.zig").File; | 155 | const File = @import("file.zig").File; |
| 192 | const MachO = @import("../MachO.zig"); | 156 | const MachO = @import("../MachO.zig"); |
| 193 | const Object = @import("Object.zig"); | 157 | const Object = @import("Object.zig"); |
src/link/MachO/relocatable.zig+67-1| ... | @@ -1,4 +1,4 @@ | ... | @@ -1,4 +1,4 @@ |
| 1 | pub fn flush(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u8) link.File.FlushError!void { | 1 | pub fn flushObject(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u8) link.File.FlushError!void { |
| 2 | const gpa = macho_file.base.comp.gpa; | 2 | const gpa = macho_file.base.comp.gpa; |
| 3 | 3 | ||
| 4 | var positionals = std.ArrayList(Compilation.LinkObject).init(gpa); | 4 | var positionals = std.ArrayList(Compilation.LinkObject).init(gpa); |
| ... | @@ -86,6 +86,72 @@ pub fn flush(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u | ... | @@ -86,6 +86,72 @@ pub fn flush(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u |
| 86 | try writeHeader(macho_file, ncmds, sizeofcmds); | 86 | try writeHeader(macho_file, ncmds, sizeofcmds); |
| 87 | } | 87 | } |
| 88 | 88 | ||
| 89 | pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u8) link.File.FlushError!void { | ||
| 90 | const gpa = comp.gpa; | ||
| 91 | |||
| 92 | var positionals = std.ArrayList(Compilation.LinkObject).init(gpa); | ||
| 93 | defer positionals.deinit(); | ||
| 94 | |||
| 95 | try positionals.ensureUnusedCapacity(comp.objects.len); | ||
| 96 | positionals.appendSliceAssumeCapacity(comp.objects); | ||
| 97 | |||
| 98 | for (comp.c_object_table.keys()) |key| { | ||
| 99 | try positionals.append(.{ .path = key.status.success.object_path }); | ||
| 100 | } | ||
| 101 | |||
| 102 | if (module_obj_path) |path| try positionals.append(.{ .path = path }); | ||
| 103 | |||
| 104 | for (positionals.items) |obj| { | ||
| 105 | // TODO: parse for archive meaning don't unpack objects | ||
| 106 | _ = obj; | ||
| 107 | } | ||
| 108 | |||
| 109 | if (comp.link_errors.items.len > 0) return error.FlushFailure; | ||
| 110 | |||
| 111 | // First, we flush relocatable object file generated with our backends. | ||
| 112 | if (macho_file.getZigObject()) |zo| { | ||
| 113 | zo.resolveSymbols(macho_file); | ||
| 114 | zo.asFile().claimUnresolvedRelocatable(macho_file); | ||
| 115 | try macho_file.sortSections(); | ||
| 116 | try macho_file.addAtomsToSections(); | ||
| 117 | try calcSectionSizes(macho_file); | ||
| 118 | try createSegment(macho_file); | ||
| 119 | try allocateSections(macho_file); | ||
| 120 | allocateSegment(macho_file); | ||
| 121 | |||
| 122 | var off = off: { | ||
| 123 | const seg = macho_file.segments.items[0]; | ||
| 124 | const off = math.cast(u32, seg.fileoff + seg.filesize) orelse return error.Overflow; | ||
| 125 | break :off mem.alignForward(u32, off, @alignOf(macho.relocation_info)); | ||
| 126 | }; | ||
| 127 | off = allocateSectionsRelocs(macho_file, off); | ||
| 128 | |||
| 129 | state_log.debug("{}", .{macho_file.dumpState()}); | ||
| 130 | |||
| 131 | try macho_file.calcSymtabSize(); | ||
| 132 | try writeAtoms(macho_file); | ||
| 133 | |||
| 134 | off = mem.alignForward(u32, off, @alignOf(u64)); | ||
| 135 | off = try macho_file.writeDataInCode(0, off); | ||
| 136 | off = mem.alignForward(u32, off, @alignOf(u64)); | ||
| 137 | off = try macho_file.writeSymtab(off); | ||
| 138 | off = mem.alignForward(u32, off, @alignOf(u64)); | ||
| 139 | off = try macho_file.writeStrtab(off); | ||
| 140 | |||
| 141 | // In order to please Apple ld (and possibly other MachO linkers in the wild), | ||
| 142 | // we will now sanitize segment names of Zig-specific segments. | ||
| 143 | sanitizeZigSections(macho_file); | ||
| 144 | |||
| 145 | const ncmds, const sizeofcmds = try writeLoadCommands(macho_file); | ||
| 146 | try writeHeader(macho_file, ncmds, sizeofcmds); | ||
| 147 | } | ||
| 148 | |||
| 149 | var err = try macho_file.addErrorWithNotes(0); | ||
| 150 | try err.addMsg(macho_file, "TODO implement flushStaticLib", .{}); | ||
| 151 | |||
| 152 | return error.FlushFailure; | ||
| 153 | } | ||
| 154 | |||
| 89 | fn markExports(macho_file: *MachO) void { | 155 | fn markExports(macho_file: *MachO) void { |
| 90 | if (macho_file.getZigObject()) |zo| { | 156 | if (macho_file.getZigObject()) |zo| { |
| 91 | zo.asFile().markExportsRelocatable(macho_file); | 157 | zo.asFile().markExportsRelocatable(macho_file); |