Skip to content

Commit 181cc6e

Browse files
WalterBrightthewilsonator
authored andcommitted
some corrected ARM64 fixups
1 parent 2be1b55 commit 181cc6e

2 files changed

Lines changed: 22 additions & 43 deletions

File tree

compiler/src/dmd/backend/mach.d

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module dmd.backend.mach;
99
// Online documentation: https://dlang.org/phobos/dmd_backend_mach.html
1010
// location of system .h files on Mac: xcrun --show-sdk-path
1111
// location of machine.h: usr/include/mach
12+
// https://alexdremov.me/mystery-of-mach-o-object-file-builders/
1213

1314
@safe:
1415

@@ -423,16 +424,17 @@ enum
423424
ARM64_RELOC_AUTHENTICATED_POINTER = 11,
424425
}
425426

426-
struct relocation_info
427+
struct relocation_info // https://developer.apple.com/documentation/kernel/relocation_info
427428
{
428-
int r_address;
429+
int r_address; // offset from the section start to the item to be relocated
429430

430431
/* LITTLE_ENDIAN for x86
431-
* uint r_symbolnum:24,
432-
* r_pcrel :1,
433-
* r_length :2,
434-
* r_extern :1,
435-
* r_type :4;
432+
* uint r_symbolnum:24, // if r_extern is 1, then index into the symbol table
433+
* // if r_extern is 0, then ordinal number of the section
434+
* r_pcrel :1, // one means PC-relative, 0 means absolute
435+
* r_length :2, // 0: 1 byte, 1: 2 bytes, 2: sizeof(long) bytes, 3: 8 bytes
436+
* r_extern :1, // see r_symbolnum
437+
* r_type :4; // ARM64_RELOC_xxxxx, X86_64_RELOC_xxxxx
436438
*/
437439
uint xxx;
438440
nothrow:

compiler/src/dmd/backend/machobj.d

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,13 @@ int mach_seg_data_isCode(const ref seg_data sd)
219219
if (&sd == SegData[CDATA])
220220
return false;
221221

222-
if (I64)
222+
if (machobj.AArch64)
223223
{
224224
//printf("SDshtidx = %d, x%x\n", SDshtidx, SecHdrTab64[sd.SDshtidx].flags);
225-
//return SecHdrTab64[sd.SDshtidx].flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS);
225+
return SecHdrTab64[sd.SDshtidx].flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS);
226+
}
227+
else if (I64)
228+
{
226229
return strcmp(SecHdrTab64[sd.SDshtidx].segname.ptr, "__TEXT") == 0;
227230
}
228231
else
@@ -907,7 +910,7 @@ void MachObj_term(const(char)[] objfilename)
907910
//printf("%d:x%04llx : targseg %d targsym %s REL%s flag %d\n", seg, r.offset, r.targseg, s ? s.Sident.ptr : "0", rs, r.flag);
908911
// bool isPersonality = strcmp(s.Sident.ptr, "__dmd_personality_v0") == 0 ||
909912
// strcmp(s.Sident.ptr, "___dmd_personality_v0") == 0; // temporary scaffolding
910-
//symbol_print(*s);
913+
// symbol_print(*s);
911914
relocation_info rel;
912915
scattered_relocation_info srel;
913916
if (machobj.AArch64)
@@ -918,6 +921,7 @@ void MachObj_term(const(char)[] objfilename)
918921
//symbol_print(*s);
919922
if (r.flag == 1) // emit SUBTRACTOR/UNSIGNED pair
920923
{
924+
//printf("rel1\n");
921925
rel.r_type = ARM64_RELOC_SUBTRACTOR;
922926
rel.r_address = cast(int)r.offset;
923927
rel.r_symbolnum = r.funcsym.Sxtrnnum;
@@ -941,6 +945,7 @@ void MachObj_term(const(char)[] objfilename)
941945
}
942946
else if (pseg.isCode())
943947
{
948+
//printf("rel2\n");
944949
//printf("AArch64\n");
945950
//symbol_print(*s);
946951
switch (s.Sclass)
@@ -1014,6 +1019,7 @@ void MachObj_term(const(char)[] objfilename)
10141019
}
10151020
else
10161021
{
1022+
//printf("rel3\n");
10171023
if (s.Sclass == SC.locstat && s.Sfl == FL.tlsdata)
10181024
{
10191025
rel.r_address = cast(int)r.offset;
@@ -1034,57 +1040,28 @@ void MachObj_term(const(char)[] objfilename)
10341040
s.Sclass == SC.comdef ||
10351041
s.Sclass == SC.comdat)
10361042
{
1037-
assert(!machobj.AArch64);
10381043
rel.r_address = cast(int)r.offset;
10391044
rel.r_symbolnum = s.Sxtrnnum;
10401045
rel.r_pcrel = 0;
1041-
rel.r_length = 2;
1046+
rel.r_length = 3;
10421047
rel.r_extern = 1;
1043-
rel.r_type = GENERIC_RELOC_VANILLA;
1044-
if (I64)
1045-
{
1046-
rel.r_type = X86_64_RELOC_UNSIGNED;
1047-
rel.r_length = 3;
1048-
}
1048+
rel.r_type = ARM64_RELOC_UNSIGNED; // RELOC_POINTER_TO_GOT?
10491049
machobj.fobjbuf.write(&rel, rel.sizeof);
10501050
foffset += rel.sizeof;
10511051
nreloc++;
10521052
continue;
10531053
}
10541054
else
10551055
{
1056-
assert(!machobj.AArch64);
10571056
rel.r_address = cast(int)r.offset;
10581057
rel.r_symbolnum = s.Sseg;
10591058
rel.r_pcrel = 0;
1060-
rel.r_length = 2;
1059+
rel.r_length = 2; // 3?
10611060
rel.r_extern = 0;
1062-
rel.r_type = GENERIC_RELOC_VANILLA;
1063-
if (I64)
1064-
{
1065-
rel.r_type = X86_64_RELOC_UNSIGNED;
1066-
rel.r_length = 3;
1067-
if (0 && s.Sseg != seg)
1068-
rel.r_type = X86_64_RELOC_BRANCH;
1069-
}
1061+
rel.r_type = ARM64_RELOC_UNSIGNED;
10701062
machobj.fobjbuf.write(&rel, rel.sizeof);
10711063
foffset += rel.sizeof;
10721064
nreloc++;
1073-
if (I64)
1074-
{
1075-
rel.r_length = 3;
1076-
int32_t* p = patchAddr64(seg, r.offset);
1077-
// Absolute address; add in addr of start of targ seg
1078-
*p += SecHdrTab64[SegData[s.Sseg].SDshtidx].addr + s.Soffset;
1079-
//patch(pseg, r.offset, s.Sseg, s.Soffset);
1080-
}
1081-
else
1082-
{
1083-
int32_t* p = patchAddr(seg, r.offset);
1084-
// Absolute address; add in addr of start of targ seg
1085-
*p += SecHdrTab[SegData[s.Sseg].SDshtidx].addr + s.Soffset;
1086-
//patch(pseg, r.offset, s.Sseg, s.Soffset);
1087-
}
10881065
continue;
10891066
}
10901067
}

0 commit comments

Comments
 (0)