authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-02 23:42:38+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-03 09:28:34+01:00
logdc6db3b30911a09890d6b5674c72a9db5a52e050
tree244895b64fb362bef32aa64d9a57dc7508163580
parent9fc1685c1caa4c1d093fa8936a5602f54121dd50

macho: minor fixes and sanitize input *_zig segment/sections names


2 files changed, 20 insertions(+), 10 deletions(-)

src/link/MachO/Atom.zig+18-10
...@@ -119,9 +119,19 @@ pub fn getThunk(self: Atom, macho_file: *MachO) *Thunk {...@@ -119,9 +119,19 @@ pub fn getThunk(self: Atom, macho_file: *MachO) *Thunk {
119119
120pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {120pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {
121 const segname, const sectname, const flags = blk: {121 const segname, const sectname, const flags = blk: {
122 // Sanitize names produced by Zig self-hosted backends.
123 // TODO perhaps we simply should emit different names instead?
124 const segname = if (mem.indexOf(u8, sect.segName(), "_ZIG")) |idx|
125 sect.segName()[0..idx]
126 else
127 sect.segName();
128 const sectname = if (mem.indexOf(u8, sect.sectName(), "_zig")) |idx|
129 sect.sectName()[0..idx]
130 else
131 sect.sectName();
122 if (sect.isCode()) break :blk .{132 if (sect.isCode()) break :blk .{
123 "__TEXT",133 "__TEXT",
124 sect.sectName(),134 sectname,
125 macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,135 macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
126 };136 };
127137
...@@ -132,15 +142,15 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {...@@ -132,15 +142,15 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {
132 => break :blk .{ "__TEXT", "__const", macho.S_REGULAR },142 => break :blk .{ "__TEXT", "__const", macho.S_REGULAR },
133143
134 macho.S_CSTRING_LITERALS => {144 macho.S_CSTRING_LITERALS => {
135 if (mem.startsWith(u8, sect.sectName(), "__objc")) break :blk .{145 if (mem.startsWith(u8, sectname, "__objc")) break :blk .{
136 sect.segName(), sect.sectName(), macho.S_REGULAR,146 segname, sectname, macho.S_REGULAR,
137 };147 };
138 break :blk .{ "__TEXT", "__cstring", macho.S_CSTRING_LITERALS };148 break :blk .{ "__TEXT", "__cstring", macho.S_CSTRING_LITERALS };
139 },149 },
140150
141 macho.S_MOD_INIT_FUNC_POINTERS,151 macho.S_MOD_INIT_FUNC_POINTERS,
142 macho.S_MOD_TERM_FUNC_POINTERS,152 macho.S_MOD_TERM_FUNC_POINTERS,
143 => break :blk .{ "__DATA_CONST", sect.sectName(), sect.flags },153 => break :blk .{ "__DATA_CONST", sectname, sect.flags },
144154
145 macho.S_LITERAL_POINTERS,155 macho.S_LITERAL_POINTERS,
146 macho.S_ZEROFILL,156 macho.S_ZEROFILL,
...@@ -149,17 +159,15 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {...@@ -149,17 +159,15 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {
149 macho.S_THREAD_LOCAL_VARIABLE_POINTERS,159 macho.S_THREAD_LOCAL_VARIABLE_POINTERS,
150 macho.S_THREAD_LOCAL_REGULAR,160 macho.S_THREAD_LOCAL_REGULAR,
151 macho.S_THREAD_LOCAL_ZEROFILL,161 macho.S_THREAD_LOCAL_ZEROFILL,
152 => break :blk .{ sect.segName(), sect.sectName(), sect.flags },162 => break :blk .{ segname, sectname, sect.flags },
153163
154 macho.S_COALESCED => break :blk .{164 macho.S_COALESCED => break :blk .{
155 sect.segName(),165 segname,
156 sect.sectName(),166 sectname,
157 macho.S_REGULAR,167 macho.S_REGULAR,
158 },168 },
159169
160 macho.S_REGULAR => {170 macho.S_REGULAR => {
161 const segname = sect.segName();
162 const sectname = sect.sectName();
163 if (mem.eql(u8, segname, "__DATA")) {171 if (mem.eql(u8, segname, "__DATA")) {
164 if (mem.eql(u8, sectname, "__const") or172 if (mem.eql(u8, sectname, "__const") or
165 mem.eql(u8, sectname, "__cfstring") or173 mem.eql(u8, sectname, "__cfstring") or
...@@ -173,7 +181,7 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {...@@ -173,7 +181,7 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {
173 break :blk .{ segname, sectname, sect.flags };181 break :blk .{ segname, sectname, sect.flags };
174 },182 },
175183
176 else => break :blk .{ sect.segName(), sect.sectName(), sect.flags },184 else => break :blk .{ segname, sectname, sect.flags },
177 }185 }
178 };186 };
179 const osec = macho_file.getSectionByName(segname, sectname) orelse try macho_file.addSection(187 const osec = macho_file.getSectionByName(segname, sectname) orelse try macho_file.addSection(
src/link/MachO/ZigObject.zig+2
...@@ -196,8 +196,10 @@ pub fn resolveSymbols(self: *ZigObject, macho_file: *MachO) void {...@@ -196,8 +196,10 @@ pub fn resolveSymbols(self: *ZigObject, macho_file: *MachO) void {
196 const atom = macho_file.getAtom(atom_index).?;196 const atom = macho_file.getAtom(atom_index).?;
197 break :blk nlist.n_value - atom.getInputAddress(macho_file);197 break :blk nlist.n_value - atom.getInputAddress(macho_file);
198 } else nlist.n_value;198 } else nlist.n_value;
199 const out_n_sect = if (nlist.sect()) macho_file.getAtom(atom_index).?.out_n_sect else 0;
199 symbol.value = value;200 symbol.value = value;
200 symbol.atom = atom_index;201 symbol.atom = atom_index;
202 symbol.out_n_sect = out_n_sect;
201 symbol.nlist_idx = nlist_idx;203 symbol.nlist_idx = nlist_idx;
202 symbol.file = self.index;204 symbol.file = self.index;
203 symbol.flags.weak = nlist.weakDef();205 symbol.flags.weak = nlist.weakDef();