authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-17 10:52:36+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-17 10:54:10+02:00
logc6ea181e75cc6822edbaceccb11bc3a39484d116
tree455da6a6cdae72ac5791fa644fbe8d9244298495
parent52f9b283a77e79f64d3a3b9faf31a07fe350fd8a

macho: dedup setting entry point logic


1 files changed, 22 insertions(+), 35 deletions(-)

src/link/MachO.zig+22-35
...@@ -860,46 +860,32 @@ pub fn flush(self: *MachO, comp: *Compilation) !void {...@@ -860,46 +860,32 @@ pub fn flush(self: *MachO, comp: *Compilation) !void {
860860
861pub fn flushModule(self: *MachO, comp: *Compilation) !void {861pub fn flushModule(self: *MachO, comp: *Compilation) !void {
862 _ = comp;862 _ = comp;
863
863 const tracy = trace(@src());864 const tracy = trace(@src());
864 defer tracy.end();865 defer tracy.end();
865866
866 const output_mode = self.base.options.output_mode;867 try self.setEntryPoint();
868 try self.writeRebaseInfoTable();
869 try self.writeBindInfoTable();
870 try self.writeLazyBindInfoTable();
871 try self.writeExportInfo();
872 try self.writeAllGlobalAndUndefSymbols();
873 try self.writeIndirectSymbolTable();
874 try self.writeStringTable();
875 try self.updateLinkeditSegmentSizes();
867876
868 switch (output_mode) {877 if (self.d_sym) |*ds| {
869 .Exe => {878 // Flush debug symbols bundle.
870 if (self.entry_addr) |addr| {879 try ds.flushModule(self.base.allocator, self.base.options);
871 // Update LC_MAIN with entry offset.880 }
872 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
873 const main_cmd = &self.load_commands.items[self.main_cmd_index.?].Main;
874 main_cmd.entryoff = addr - text_segment.inner.vmaddr;
875 main_cmd.stacksize = self.base.options.stack_size_override orelse 0;
876 self.load_commands_dirty = true;
877 }
878 try self.writeRebaseInfoTable();
879 try self.writeBindInfoTable();
880 try self.writeLazyBindInfoTable();
881 try self.writeExportInfo();
882 try self.writeAllGlobalAndUndefSymbols();
883 try self.writeIndirectSymbolTable();
884 try self.writeStringTable();
885 try self.updateLinkeditSegmentSizes();
886
887 if (self.d_sym) |*ds| {
888 // Flush debug symbols bundle.
889 try ds.flushModule(self.base.allocator, self.base.options);
890 }
891881
892 if (self.requires_adhoc_codesig) {882 if (self.requires_adhoc_codesig) {
893 // Preallocate space for the code signature.883 // Preallocate space for the code signature.
894 // We need to do this at this stage so that we have the load commands with proper values884 // We need to do this at this stage so that we have the load commands with proper values
895 // written out to the file.885 // written out to the file.
896 // The most important here is to have the correct vm and filesize of the __LINKEDIT segment886 // The most important here is to have the correct vm and filesize of the __LINKEDIT segment
897 // where the code signature goes into.887 // where the code signature goes into.
898 try self.writeCodeSignaturePadding();888 try self.writeCodeSignaturePadding();
899 }
900 },
901 .Obj => {},
902 .Lib => return error.TODOImplementWritingLibFiles,
903 }889 }
904890
905 try self.writeLoadCommands();891 try self.writeLoadCommands();
...@@ -2704,6 +2690,7 @@ fn setEntryPoint(self: *MachO) !void {...@@ -2704,6 +2690,7 @@ fn setEntryPoint(self: *MachO) !void {
2704 const ec = &self.load_commands.items[self.main_cmd_index.?].Main;2690 const ec = &self.load_commands.items[self.main_cmd_index.?].Main;
2705 ec.entryoff = @intCast(u32, sym.n_value - seg.inner.vmaddr);2691 ec.entryoff = @intCast(u32, sym.n_value - seg.inner.vmaddr);
2706 ec.stacksize = self.base.options.stack_size_override orelse 0;2692 ec.stacksize = self.base.options.stack_size_override orelse 0;
2693 self.load_commands_dirty = true;
2707}2694}
27082695
2709fn writeRebaseInfoTableZld(self: *MachO) !void {2696fn writeRebaseInfoTableZld(self: *MachO) !void {