@@ -744,14 +744,11 @@ static void sendOperandDataMetadata(FILE *out, const InstrInfo *I,
744744}
745745
746746/*
747- * Emits instructions to translate an address into a static address, if
748- * necessary. This essentially just subtracts the ELF base address.
747+ * Emits instructions to translate to/from static and dynamic addresses.
749748 */
750- static void sendTranslateToStaticAddress (FILE *out, const InstrInfo *I,
751- CallInfo &info, bool _static , int regno)
749+ static void sendTranslateAddress (FILE *out, const InstrInfo *I, CallInfo &info ,
750+ bool neg , int regno)
752751{
753- if (!_static || !info.pic )
754- return ;
755752 Register exclude[] = {getReg (regno), REGISTER_INVALID};
756753 Register rscratch = info.getScratch (exclude);
757754 bool save_rax = false ;
@@ -764,27 +761,45 @@ static void sendTranslateToStaticAddress(FILE *out, const InstrInfo *I,
764761 int regno_1 = getRegIdx (rscratch);
765762 sendLeaFromPCRelToR64 (out, " {\" rel32\" :0}" , regno_1);
766763
767- // The not+lea implement %arg -= %base without affecting %rflags:
768-
769- // not %reg
770- const uint8_t REX[] =
771- {0x48 , 0x48 , 0x48 , 0x48 , 0x49 , 0x49 , 0x00 ,
772- 0x48 , 0x49 , 0x49 , 0x48 , 0x48 , 0x49 , 0x49 , 0x49 , 0x49 , 0x48 };
773- const uint8_t MODRM[] =
774- {0xd7 , 0xd6 , 0xd2 , 0xd1 , 0xd0 , 0xd1 , 0x00 ,
775- 0xd0 , 0xd2 , 0xd3 , 0xd3 , 0xd5 , 0xd4 , 0xd5 , 0xd6 , 0xd7 , 0xd4 };
776- fprintf (out, " %u,%u,%u," , REX[regno_1], 0xf7 , MODRM[regno_1]);
764+ int32_t disp = 0x0 ;
765+ if (neg)
766+ {
767+ // The not+lea implement %arg -= %base without affecting %rflags:
768+ // not %reg
769+ const uint8_t REX[] =
770+ {0x48 , 0x48 , 0x48 , 0x48 , 0x49 , 0x49 , 0x00 ,
771+ 0x48 , 0x49 , 0x49 , 0x48 , 0x48 , 0x49 , 0x49 , 0x49 , 0x49 , 0x48 };
772+ const uint8_t MODRM[] =
773+ {0xd7 , 0xd6 , 0xd2 , 0xd1 , 0xd0 , 0xd1 , 0x00 ,
774+ 0xd0 , 0xd2 , 0xd3 , 0xd3 , 0xd5 , 0xd4 , 0xd5 , 0xd6 , 0xd7 , 0xd4 };
775+ fprintf (out, " %u,%u,%u," , REX[regno_1], 0xf7 , MODRM[regno_1]);
776+ disp = 0x1 ;
777+ }
777778
778779 // lea 0x1(%arg,%reg,1),%arg
779780 sendLoadFromMemOpToR64 (out, I, info, /* size=*/ 8 , /* seg=*/ REGISTER_NONE,
780- /* disp= */ 0x1 , /* base=*/ getReg (regno), /* index=*/ rscratch,
781+ disp, /* base=*/ getReg (regno), /* index=*/ rscratch,
781782 /* scale=*/ 1 , /* lea=*/ true , regno, /* asis=*/ true );
782783
783784 if (save_rax)
784785 fprintf (out, " %u," , 0x58 ); // pop %rax
785786 else
786787 info.clobber (rscratch);
787788}
789+ static void sendTranslateToStaticAddress (FILE *out, const InstrInfo *I,
790+ CallInfo &info, bool _static, int regno)
791+ {
792+ if (!_static || !info.pic )
793+ return ;
794+ sendTranslateAddress (out, I, info, /* neg=*/ true , regno);
795+ }
796+ static void sendTranslateToDynamicAddress (FILE *out, const InstrInfo *I,
797+ CallInfo &info, bool _static, int regno)
798+ {
799+ if (_static || !info.pic )
800+ return ;
801+ sendTranslateAddress (out, I, info, /* neg=*/ false , regno);
802+ }
788803
789804/*
790805 * Emits instructions to load the jump/call/return target into the
@@ -1138,8 +1153,21 @@ static Type sendLoadArgumentMetadata(FILE *out, CallInfo &info,
11381153 t = TYPE_CONST_VOID_PTR;
11391154 break ;
11401155 case ARGUMENT_CONFIG:
1141- sendLoadPointerMetadata (out, info, _static, 0x0 ,
1142- " {\" rel32\" :\" .Lconfig\" }" , regno);
1156+ // ELF = "config anywhere", PE = "config close".
1157+ switch (elf->type )
1158+ {
1159+ case BINARY_TYPE_ELF_EXE: case BINARY_TYPE_ELF_DSO:
1160+ case BINARY_TYPE_ELF_PIE:
1161+ sendMovFromI64ToR64 (out, " {\" int64\" :\" .Lconfig\" }" ,
1162+ regno);
1163+ sendTranslateToDynamicAddress (out, I, info,
1164+ /* static=*/ false , regno);
1165+ break ;
1166+ case BINARY_TYPE_PE_EXE: case BINARY_TYPE_PE_DLL:
1167+ sendLeaFromPCRelToR64 (out, " {\" rel32\" :\" .Lconfig\" }" ,
1168+ regno);
1169+ break ;
1170+ }
11431171 t = TYPE_CONST_VOID_PTR;
11441172 break ;
11451173 case ARGUMENT_ASM:
0 commit comments