authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-10 22:48:03+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-18 09:13:08+02:00
log18778e2a0aab02c88694bfed5623ef7c205e12c7
treeca532268ef107dbee3fad983f407b2c7a7498774
parent174de37cefde3b22b02fee5982e7bacead4d200f

macho: bring back parts of ar


5 files changed, 58 insertions(+), 49 deletions(-)

src/link/MachO.zig+1-1
......@@ -356,7 +356,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n
356356 if (comp.verbose_link) try self.dumpArgv(comp);
357357
358358 if (self.getZigObject()) |zo| try zo.flushModule(self, tid);
359 // if (self.base.isStaticLib()) return relocatable.flushStaticLib(self, comp, module_obj_path);
359 if (self.base.isStaticLib()) return relocatable.flushStaticLib(self, comp, module_obj_path);
360360 // if (self.base.isObject()) return relocatable.flushObject(self, comp, module_obj_path);
361361
362362 var positionals = std.ArrayList(Compilation.LinkObject).init(gpa);
src/link/MachO/Object.zig+2
......@@ -77,6 +77,8 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {
7777 const tracy = trace(@src());
7878 defer tracy.end();
7979
80 log.debug("parsing {}", .{self.fmtPath()});
81
8082 const gpa = macho_file.base.comp.gpa;
8183 const handle = macho_file.getFileHandle(self.file_handle);
8284 const cpu_arch = macho_file.getTarget().cpu.arch;
src/link/MachO/Relocation.zig+1-1
......@@ -41,7 +41,7 @@ pub fn getGotTargetAddress(rel: Relocation, atom: Atom, macho_file: *MachO) u64
4141}
4242
4343pub fn getZigGotTargetAddress(rel: Relocation, macho_file: *MachO) u64 {
44 const zo = macho_file.getZigObject().?;
44 const zo = macho_file.getZigObject() orelse return 0;
4545 return switch (rel.tag) {
4646 .local => 0,
4747 .@"extern" => {
src/link/MachO/file.zig+8-2
......@@ -205,6 +205,8 @@ pub const File = union(enum) {
205205 pub fn updateArSymtab(file: File, ar_symtab: *Archive.ArSymtab, macho_file: *MachO) error{OutOfMemory}!void {
206206 return switch (file) {
207207 .dylib, .internal => unreachable,
208 // TODO
209 .zig_object => unreachable,
208210 inline else => |x| x.updateArSymtab(ar_symtab, macho_file),
209211 };
210212 }
......@@ -212,7 +214,9 @@ pub const File = union(enum) {
212214 pub fn updateArSize(file: File, macho_file: *MachO) !void {
213215 return switch (file) {
214216 .dylib, .internal => unreachable,
215 .zig_object => |x| x.updateArSize(),
217 // TODO
218 .zig_object => unreachable,
219 // .zig_object => |x| x.updateArSize(),
216220 .object => |x| x.updateArSize(macho_file),
217221 };
218222 }
......@@ -220,7 +224,9 @@ pub const File = union(enum) {
220224 pub fn writeAr(file: File, ar_format: Archive.Format, macho_file: *MachO, writer: anytype) !void {
221225 return switch (file) {
222226 .dylib, .internal => unreachable,
223 .zig_object => |x| x.writeAr(ar_format, writer),
227 // TODO
228 .zig_object => unreachable,
229 // .zig_object => |x| x.writeAr(ar_format, writer),
224230 .object => |x| x.writeAr(ar_format, macho_file, writer),
225231 };
226232 }
src/link/MachO/relocatable.zig+46-45
......@@ -127,55 +127,56 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?
127127
128128 if (comp.link_errors.items.len > 0) return error.FlushFailure;
129129
130 // First, we flush relocatable object file generated with our backends.
131 if (macho_file.getZigObject()) |zo| {
132 zo.resolveSymbols(macho_file);
133 zo.asFile().markExportsRelocatable(macho_file);
134 zo.asFile().claimUnresolvedRelocatable(macho_file);
135 try macho_file.sortSections();
136 try macho_file.addAtomsToSections();
137 try calcSectionSizes(macho_file);
138 try createSegment(macho_file);
139 try allocateSections(macho_file);
140 allocateSegment(macho_file);
141
142 var off = off: {
143 const seg = macho_file.segments.items[0];
144 const off = math.cast(u32, seg.fileoff + seg.filesize) orelse return error.Overflow;
145 break :off mem.alignForward(u32, off, @alignOf(macho.relocation_info));
146 };
147 off = allocateSectionsRelocs(macho_file, off);
148
149 if (build_options.enable_logging) {
150 state_log.debug("{}", .{macho_file.dumpState()});
151 }
152
153 try macho_file.calcSymtabSize();
154 try writeAtoms(macho_file);
155
156 off = mem.alignForward(u32, off, @alignOf(u64));
157 off = try macho_file.writeDataInCode(0, off);
158 off = mem.alignForward(u32, off, @alignOf(u64));
159 off = try macho_file.writeSymtab(off);
160 off = mem.alignForward(u32, off, @alignOf(u64));
161 off = try macho_file.writeStrtab(off);
162
163 // In order to please Apple ld (and possibly other MachO linkers in the wild),
164 // we will now sanitize segment names of Zig-specific segments.
165 sanitizeZigSections(macho_file);
166
167 const ncmds, const sizeofcmds = try writeLoadCommands(macho_file);
168 try writeHeader(macho_file, ncmds, sizeofcmds);
169
170 // TODO we can avoid reading in the file contents we just wrote if we give the linker
171 // ability to write directly to a buffer.
172 try zo.readFileContents(off, macho_file);
173 }
130 // TODO re-enable
131 // // First, we flush relocatable object file generated with our backends.
132 // if (macho_file.getZigObject()) |zo| {
133 // zo.resolveSymbols(macho_file);
134 // zo.asFile().markExportsRelocatable(macho_file);
135 // zo.asFile().claimUnresolvedRelocatable(macho_file);
136 // try macho_file.sortSections();
137 // try macho_file.addAtomsToSections();
138 // try calcSectionSizes(macho_file);
139 // try createSegment(macho_file);
140 // try allocateSections(macho_file);
141 // allocateSegment(macho_file);
142
143 // var off = off: {
144 // const seg = macho_file.segments.items[0];
145 // const off = math.cast(u32, seg.fileoff + seg.filesize) orelse return error.Overflow;
146 // break :off mem.alignForward(u32, off, @alignOf(macho.relocation_info));
147 // };
148 // off = allocateSectionsRelocs(macho_file, off);
149
150 // if (build_options.enable_logging) {
151 // state_log.debug("{}", .{macho_file.dumpState()});
152 // }
153
154 // try macho_file.calcSymtabSize();
155 // try writeAtoms(macho_file);
156
157 // off = mem.alignForward(u32, off, @alignOf(u64));
158 // off = try macho_file.writeDataInCode(0, off);
159 // off = mem.alignForward(u32, off, @alignOf(u64));
160 // off = try macho_file.writeSymtab(off);
161 // off = mem.alignForward(u32, off, @alignOf(u64));
162 // off = try macho_file.writeStrtab(off);
163
164 // // In order to please Apple ld (and possibly other MachO linkers in the wild),
165 // // we will now sanitize segment names of Zig-specific segments.
166 // sanitizeZigSections(macho_file);
167
168 // const ncmds, const sizeofcmds = try writeLoadCommands(macho_file);
169 // try writeHeader(macho_file, ncmds, sizeofcmds);
170
171 // // TODO we can avoid reading in the file contents we just wrote if we give the linker
172 // // ability to write directly to a buffer.
173 // try zo.readFileContents(off, macho_file);
174 // }
174175
175176 var files = std.ArrayList(File.Index).init(gpa);
176177 defer files.deinit();
177178 try files.ensureTotalCapacityPrecise(macho_file.objects.items.len + 1);
178 if (macho_file.getZigObject()) |zo| files.appendAssumeCapacity(zo.index);
179 // if (macho_file.getZigObject()) |zo| files.appendAssumeCapacity(zo.index);
179180 for (macho_file.objects.items) |index| files.appendAssumeCapacity(index);
180181
181182 const format: Archive.Format = .p32;