@@ -2816,24 +2816,21 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
2816
2816
if (self .air .value (callee )) | func_value | {
2817
2817
if (func_value .castTag (.function )) | func_payload | {
2818
2818
const func = func_payload .data ;
2819
- const got_addr = blk : {
2820
- const seg = macho_file .load_commands .items [macho_file .data_const_segment_cmd_index .? ].Segment ;
2821
- const got = seg .sections .items [macho_file .got_section_index .? ];
2822
- const got_index = macho_file .got_entries_map .get (.{
2823
- .where = .local ,
2824
- .where_index = func .owner_decl .link .macho .local_sym_index ,
2825
- }) orelse unreachable ;
2826
- break :blk got .addr + got_index * @sizeOf (u64 );
2827
- };
2819
+ // TODO I'm hacking my way through here by repurposing .memory for storing
2820
+ // index to the GOT target symbol index.
2828
2821
switch (arch ) {
2829
2822
.x86_64 = > {
2830
- try self .genSetReg (Type .initTag (.u64 ), .rax , .{ .memory = got_addr });
2823
+ try self .genSetReg (Type .initTag (.u64 ), .rax , .{
2824
+ .memory = func .owner_decl .link .macho .local_sym_index ,
2825
+ });
2831
2826
// callq *%rax
2832
2827
try self .code .ensureCapacity (self .code .items .len + 2 );
2833
2828
self .code .appendSliceAssumeCapacity (&[2 ]u8 { 0xff , 0xd0 });
2834
2829
},
2835
2830
.aarch64 = > {
2836
- try self .genSetReg (Type .initTag (.u64 ), .x30 , .{ .memory = got_addr });
2831
+ try self .genSetReg (Type .initTag (.u64 ), .x30 , .{
2832
+ .memory = func .owner_decl .link .macho .local_sym_index ,
2833
+ });
2837
2834
// blr x30
2838
2835
writeInt (u32 , try self .code .addManyAsArray (4 ), Instruction .blr (.x30 ).toU32 ());
2839
2836
},
@@ -4345,29 +4342,20 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
4345
4342
}).toU32 ());
4346
4343
4347
4344
if (self .bin_file .cast (link .File .MachO )) | macho_file | {
4348
- // TODO this is super awkward. We are reversing the address of the GOT entry here.
4349
- // We should probably have it cached or move the reloc adding somewhere else.
4350
- const got_addr = blk : {
4351
- const seg = macho_file .load_commands .items [macho_file .data_const_segment_cmd_index .? ].Segment ;
4352
- const got = seg .sections .items [macho_file .got_section_index .? ];
4353
- break :blk got .addr ;
4354
- };
4355
- const where_index = blk : for (macho_file .got_entries .items ) | key , id | {
4356
- if (got_addr + id * @sizeOf (u64 ) == addr ) break :blk key .where_index ;
4357
- } else unreachable ;
4345
+ // TODO I think the reloc might be in the wrong place.
4358
4346
const decl = macho_file .active_decl .? ;
4359
4347
// Page reloc for adrp instruction.
4360
4348
try decl .link .macho .relocs .append (self .bin_file .allocator , .{
4361
4349
.offset = offset ,
4362
4350
.where = .local ,
4363
- .where_index = where_index ,
4351
+ .where_index = @intCast ( u32 , addr ) ,
4364
4352
.payload = .{ .page = .{ .kind = .got } },
4365
4353
});
4366
4354
// Pageoff reloc for adrp instruction.
4367
4355
try decl .link .macho .relocs .append (self .bin_file .allocator , .{
4368
4356
.offset = offset + 4 ,
4369
4357
.where = .local ,
4370
- .where_index = where_index ,
4358
+ .where_index = @intCast ( u32 , addr ) ,
4371
4359
.payload = .{ .page_off = .{ .kind = .got } },
4372
4360
});
4373
4361
} else {
@@ -4628,22 +4616,13 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
4628
4616
const offset = @intCast (u32 , self .code .items .len );
4629
4617
4630
4618
if (self .bin_file .cast (link .File .MachO )) | macho_file | {
4631
- // TODO this is super awkward. We are reversing the address of the GOT entry here.
4632
- // We should probably have it cached or move the reloc adding somewhere else.
4633
- const got_addr = blk : {
4634
- const seg = macho_file .load_commands .items [macho_file .data_const_segment_cmd_index .? ].Segment ;
4635
- const got = seg .sections .items [macho_file .got_section_index .? ];
4636
- break :blk got .addr ;
4637
- };
4638
- const where_index = blk : for (macho_file .got_entries .items ) | key , id | {
4639
- if (got_addr + id * @sizeOf (u64 ) == x ) break :blk key .where_index ;
4640
- } else unreachable ;
4619
+ // TODO I think the reloc might be in the wrong place.
4641
4620
const decl = macho_file .active_decl .? ;
4642
4621
// Load reloc for LEA instruction.
4643
4622
try decl .link .macho .relocs .append (self .bin_file .allocator , .{
4644
4623
.offset = offset - 4 ,
4645
4624
.where = .local ,
4646
- .where_index = where_index ,
4625
+ .where_index = @intCast ( u32 , x ) ,
4647
4626
.payload = .{ .load = .{ .kind = .got } },
4648
4627
});
4649
4628
} else {
@@ -4869,17 +4848,10 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
4869
4848
const got = & elf_file .program_headers .items [elf_file .phdr_got_index .? ];
4870
4849
const got_addr = got .p_vaddr + decl .link .elf .offset_table_index * ptr_bytes ;
4871
4850
return MCValue { .memory = got_addr };
4872
- } else if (self .bin_file .cast (link .File .MachO )) | macho_file | {
4873
- const got_addr = blk : {
4874
- const seg = macho_file .load_commands .items [macho_file .data_const_segment_cmd_index .? ].Segment ;
4875
- const got = seg .sections .items [macho_file .got_section_index .? ];
4876
- const got_index = macho_file .got_entries_map .get (.{
4877
- .where = .local ,
4878
- .where_index = decl .link .macho .local_sym_index ,
4879
- }) orelse unreachable ;
4880
- break :blk got .addr + got_index * ptr_bytes ;
4881
- };
4882
- return MCValue { .memory = got_addr };
4851
+ } else if (self .bin_file .cast (link .File .MachO )) | _ | {
4852
+ // TODO I'm hacking my way through here by repurposing .memory for storing
4853
+ // index to the GOT target symbol index.
4854
+ return MCValue { .memory = decl .link .macho .local_sym_index };
4883
4855
} else if (self .bin_file .cast (link .File .Coff )) | coff_file | {
4884
4856
const got_addr = coff_file .offset_table_virtual_address + decl .link .coff .offset_table_index * ptr_bytes ;
4885
4857
return MCValue { .memory = got_addr };
0 commit comments