Skip to content

Commit ecf7dfc

Browse files
authored
Merge pull request #7273 from kubkon/lld-codesig-poc
lld+macho: patch lld output on Apple Silicon by calculating and embedding adhoc code signature
2 parents a2cd9dc + d3be499 commit ecf7dfc

File tree

6 files changed

+678
-286
lines changed

6 files changed

+678
-286
lines changed

src/codegen.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
17951795
if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
17961796
if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
17971797
const func = func_val.func;
1798-
const got = &macho_file.sections.items[macho_file.got_section_index.?];
1798+
const text_segment = &macho_file.load_commands.items[macho_file.text_segment_cmd_index.?].Segment;
1799+
const got = &text_segment.sections.items[macho_file.got_section_index.?];
17991800
const got_addr = got.addr + func.owner_decl.link.macho.offset_table_index * @sizeOf(u64);
18001801
switch (arch) {
18011802
.x86_64 => {
@@ -3196,7 +3197,8 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
31963197
return MCValue{ .memory = got_addr };
31973198
} else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
31983199
const decl = payload.decl;
3199-
const got = &macho_file.sections.items[macho_file.got_section_index.?];
3200+
const text_segment = &macho_file.load_commands.items[macho_file.text_segment_cmd_index.?].Segment;
3201+
const got = &text_segment.sections.items[macho_file.got_section_index.?];
32003202
const got_addr = got.addr + decl.link.macho.offset_table_index * ptr_bytes;
32013203
return MCValue{ .memory = got_addr };
32023204
} else if (self.bin_file.cast(link.File.Coff)) |coff_file| {

src/link.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ pub const File = struct {
238238
}
239239

240240
pub fn makeExecutable(base: *File) !void {
241+
switch (base.options.output_mode) {
242+
.Obj => return,
243+
.Lib => switch (base.options.link_mode) {
244+
.Static => return,
245+
.Dynamic => {},
246+
},
247+
.Exe => {},
248+
}
241249
switch (base.tag) {
242250
.macho => if (base.file) |f| {
243251
if (base.intermediary_basename != null) {

0 commit comments

Comments
 (0)