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 {
119119
120120pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {
121121 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();
122132 if (sect.isCode()) break :blk .{
123133 "__TEXT",
124 sect.sectName(),
134 sectname,
125135 macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS,
126136 };
127137
......@@ -132,15 +142,15 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {
132142 => break :blk .{ "__TEXT", "__const", macho.S_REGULAR },
133143
134144 macho.S_CSTRING_LITERALS => {
135 if (mem.startsWith(u8, sect.sectName(), "__objc")) break :blk .{
136 sect.segName(), sect.sectName(), macho.S_REGULAR,
145 if (mem.startsWith(u8, sectname, "__objc")) break :blk .{
146 segname, sectname, macho.S_REGULAR,
137147 };
138148 break :blk .{ "__TEXT", "__cstring", macho.S_CSTRING_LITERALS };
139149 },
140150
141151 macho.S_MOD_INIT_FUNC_POINTERS,
142152 macho.S_MOD_TERM_FUNC_POINTERS,
143 => break :blk .{ "__DATA_CONST", sect.sectName(), sect.flags },
153 => break :blk .{ "__DATA_CONST", sectname, sect.flags },
144154
145155 macho.S_LITERAL_POINTERS,
146156 macho.S_ZEROFILL,
......@@ -149,17 +159,15 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {
149159 macho.S_THREAD_LOCAL_VARIABLE_POINTERS,
150160 macho.S_THREAD_LOCAL_REGULAR,
151161 macho.S_THREAD_LOCAL_ZEROFILL,
152 => break :blk .{ sect.segName(), sect.sectName(), sect.flags },
162 => break :blk .{ segname, sectname, sect.flags },
153163
154164 macho.S_COALESCED => break :blk .{
155 sect.segName(),
156 sect.sectName(),
165 segname,
166 sectname,
157167 macho.S_REGULAR,
158168 },
159169
160170 macho.S_REGULAR => {
161 const segname = sect.segName();
162 const sectname = sect.sectName();
163171 if (mem.eql(u8, segname, "__DATA")) {
164172 if (mem.eql(u8, sectname, "__const") or
165173 mem.eql(u8, sectname, "__cfstring") or
......@@ -173,7 +181,7 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {
173181 break :blk .{ segname, sectname, sect.flags };
174182 },
175183
176 else => break :blk .{ sect.segName(), sect.sectName(), sect.flags },
184 else => break :blk .{ segname, sectname, sect.flags },
177185 }
178186 };
179187 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 {
196196 const atom = macho_file.getAtom(atom_index).?;
197197 break :blk nlist.n_value - atom.getInputAddress(macho_file);
198198 } else nlist.n_value;
199 const out_n_sect = if (nlist.sect()) macho_file.getAtom(atom_index).?.out_n_sect else 0;
199200 symbol.value = value;
200201 symbol.atom = atom_index;
202 symbol.out_n_sect = out_n_sect;
201203 symbol.nlist_idx = nlist_idx;
202204 symbol.file = self.index;
203205 symbol.flags.weak = nlist.weakDef();