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...@@ -356,7 +356,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n
356 if (comp.verbose_link) try self.dumpArgv(comp);356 if (comp.verbose_link) try self.dumpArgv(comp);
357357
358 if (self.getZigObject()) |zo| try zo.flushModule(self, tid);358 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);
360 // if (self.base.isObject()) return relocatable.flushObject(self, comp, module_obj_path);360 // if (self.base.isObject()) return relocatable.flushObject(self, comp, module_obj_path);
361361
362 var positionals = std.ArrayList(Compilation.LinkObject).init(gpa);362 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 {...@@ -77,6 +77,8 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {
77 const tracy = trace(@src());77 const tracy = trace(@src());
78 defer tracy.end();78 defer tracy.end();
7979
80 log.debug("parsing {}", .{self.fmtPath()});
81
80 const gpa = macho_file.base.comp.gpa;82 const gpa = macho_file.base.comp.gpa;
81 const handle = macho_file.getFileHandle(self.file_handle);83 const handle = macho_file.getFileHandle(self.file_handle);
82 const cpu_arch = macho_file.getTarget().cpu.arch;84 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...@@ -41,7 +41,7 @@ pub fn getGotTargetAddress(rel: Relocation, atom: Atom, macho_file: *MachO) u64
41}41}
4242
43pub fn getZigGotTargetAddress(rel: Relocation, macho_file: *MachO) u64 {43pub fn getZigGotTargetAddress(rel: Relocation, macho_file: *MachO) u64 {
44 const zo = macho_file.getZigObject().?;44 const zo = macho_file.getZigObject() orelse return 0;
45 return switch (rel.tag) {45 return switch (rel.tag) {
46 .local => 0,46 .local => 0,
47 .@"extern" => {47 .@"extern" => {
src/link/MachO/file.zig+8-2
...@@ -205,6 +205,8 @@ pub const File = union(enum) {...@@ -205,6 +205,8 @@ pub const File = union(enum) {
205 pub fn updateArSymtab(file: File, ar_symtab: *Archive.ArSymtab, macho_file: *MachO) error{OutOfMemory}!void {205 pub fn updateArSymtab(file: File, ar_symtab: *Archive.ArSymtab, macho_file: *MachO) error{OutOfMemory}!void {
206 return switch (file) {206 return switch (file) {
207 .dylib, .internal => unreachable,207 .dylib, .internal => unreachable,
208 // TODO
209 .zig_object => unreachable,
208 inline else => |x| x.updateArSymtab(ar_symtab, macho_file),210 inline else => |x| x.updateArSymtab(ar_symtab, macho_file),
209 };211 };
210 }212 }
...@@ -212,7 +214,9 @@ pub const File = union(enum) {...@@ -212,7 +214,9 @@ pub const File = union(enum) {
212 pub fn updateArSize(file: File, macho_file: *MachO) !void {214 pub fn updateArSize(file: File, macho_file: *MachO) !void {
213 return switch (file) {215 return switch (file) {
214 .dylib, .internal => unreachable,216 .dylib, .internal => unreachable,
215 .zig_object => |x| x.updateArSize(),217 // TODO
218 .zig_object => unreachable,
219 // .zig_object => |x| x.updateArSize(),
216 .object => |x| x.updateArSize(macho_file),220 .object => |x| x.updateArSize(macho_file),
217 };221 };
218 }222 }
...@@ -220,7 +224,9 @@ pub const File = union(enum) {...@@ -220,7 +224,9 @@ pub const File = union(enum) {
220 pub fn writeAr(file: File, ar_format: Archive.Format, macho_file: *MachO, writer: anytype) !void {224 pub fn writeAr(file: File, ar_format: Archive.Format, macho_file: *MachO, writer: anytype) !void {
221 return switch (file) {225 return switch (file) {
222 .dylib, .internal => unreachable,226 .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),
224 .object => |x| x.writeAr(ar_format, macho_file, writer),230 .object => |x| x.writeAr(ar_format, macho_file, writer),
225 };231 };
226 }232 }
src/link/MachO/relocatable.zig+46-45
...@@ -127,55 +127,56 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?...@@ -127,55 +127,56 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?
127127
128 if (comp.link_errors.items.len > 0) return error.FlushFailure;128 if (comp.link_errors.items.len > 0) return error.FlushFailure;
129129
130 // First, we flush relocatable object file generated with our backends.130 // TODO re-enable
131 if (macho_file.getZigObject()) |zo| {131 // // First, we flush relocatable object file generated with our backends.
132 zo.resolveSymbols(macho_file);132 // if (macho_file.getZigObject()) |zo| {
133 zo.asFile().markExportsRelocatable(macho_file);133 // zo.resolveSymbols(macho_file);
134 zo.asFile().claimUnresolvedRelocatable(macho_file);134 // zo.asFile().markExportsRelocatable(macho_file);
135 try macho_file.sortSections();135 // zo.asFile().claimUnresolvedRelocatable(macho_file);
136 try macho_file.addAtomsToSections();136 // try macho_file.sortSections();
137 try calcSectionSizes(macho_file);137 // try macho_file.addAtomsToSections();
138 try createSegment(macho_file);138 // try calcSectionSizes(macho_file);
139 try allocateSections(macho_file);139 // try createSegment(macho_file);
140 allocateSegment(macho_file);140 // try allocateSections(macho_file);
141141 // allocateSegment(macho_file);
142 var off = off: {142
143 const seg = macho_file.segments.items[0];143 // var off = off: {
144 const off = math.cast(u32, seg.fileoff + seg.filesize) orelse return error.Overflow;144 // const seg = macho_file.segments.items[0];
145 break :off mem.alignForward(u32, off, @alignOf(macho.relocation_info));145 // const off = math.cast(u32, seg.fileoff + seg.filesize) orelse return error.Overflow;
146 };146 // break :off mem.alignForward(u32, off, @alignOf(macho.relocation_info));
147 off = allocateSectionsRelocs(macho_file, off);147 // };
148148 // off = allocateSectionsRelocs(macho_file, off);
149 if (build_options.enable_logging) {149
150 state_log.debug("{}", .{macho_file.dumpState()});150 // if (build_options.enable_logging) {
151 }151 // state_log.debug("{}", .{macho_file.dumpState()});
152152 // }
153 try macho_file.calcSymtabSize();153
154 try writeAtoms(macho_file);154 // try macho_file.calcSymtabSize();
155155 // try writeAtoms(macho_file);
156 off = mem.alignForward(u32, off, @alignOf(u64));156
157 off = try macho_file.writeDataInCode(0, off);157 // off = mem.alignForward(u32, off, @alignOf(u64));
158 off = mem.alignForward(u32, off, @alignOf(u64));158 // off = try macho_file.writeDataInCode(0, off);
159 off = try macho_file.writeSymtab(off);159 // off = mem.alignForward(u32, off, @alignOf(u64));
160 off = mem.alignForward(u32, off, @alignOf(u64));160 // off = try macho_file.writeSymtab(off);
161 off = try macho_file.writeStrtab(off);161 // off = mem.alignForward(u32, off, @alignOf(u64));
162162 // off = try macho_file.writeStrtab(off);
163 // In order to please Apple ld (and possibly other MachO linkers in the wild),163
164 // we will now sanitize segment names of Zig-specific segments.164 // // In order to please Apple ld (and possibly other MachO linkers in the wild),
165 sanitizeZigSections(macho_file);165 // // we will now sanitize segment names of Zig-specific segments.
166166 // sanitizeZigSections(macho_file);
167 const ncmds, const sizeofcmds = try writeLoadCommands(macho_file);167
168 try writeHeader(macho_file, ncmds, sizeofcmds);168 // const ncmds, const sizeofcmds = try writeLoadCommands(macho_file);
169169 // try writeHeader(macho_file, ncmds, sizeofcmds);
170 // TODO we can avoid reading in the file contents we just wrote if we give the linker170
171 // ability to write directly to a buffer.171 // // TODO we can avoid reading in the file contents we just wrote if we give the linker
172 try zo.readFileContents(off, macho_file);172 // // ability to write directly to a buffer.
173 }173 // try zo.readFileContents(off, macho_file);
174 // }
174175
175 var files = std.ArrayList(File.Index).init(gpa);176 var files = std.ArrayList(File.Index).init(gpa);
176 defer files.deinit();177 defer files.deinit();
177 try files.ensureTotalCapacityPrecise(macho_file.objects.items.len + 1);178 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);
179 for (macho_file.objects.items) |index| files.appendAssumeCapacity(index);180 for (macho_file.objects.items) |index| files.appendAssumeCapacity(index);
180181
181 const format: Archive.Format = .p32;182 const format: Archive.Format = .p32;