10
10
#include " RuntimeDyldCheckerImpl.h"
11
11
#include " llvm/ADT/STLExtras.h"
12
12
#include " llvm/ADT/StringExtras.h"
13
- #include " llvm/MC/MCAsmInfo.h"
14
13
#include " llvm/MC/MCContext.h"
15
14
#include " llvm/MC/MCDisassembler/MCDisassembler.h"
16
15
#include " llvm/MC/MCInst.h"
17
- #include " llvm/MC/MCInstPrinter.h"
18
- #include " llvm/MC/MCInstrInfo.h"
19
- #include " llvm/MC/MCRegisterInfo.h"
20
- #include " llvm/MC/MCSubtargetInfo.h"
21
- #include " llvm/MC/MCTargetOptions.h"
22
- #include " llvm/MC/TargetRegistry.h"
23
16
#include " llvm/Support/Endian.h"
24
17
#include " llvm/Support/MSVCErrorWorkarounds.h"
25
18
#include " llvm/Support/MemoryBuffer.h"
32
25
33
26
using namespace llvm ;
34
27
35
- namespace {
36
- struct TargetInfo {
37
- const Target *TheTarget;
38
- std::unique_ptr<MCSubtargetInfo> STI;
39
- std::unique_ptr<MCRegisterInfo> MRI;
40
- std::unique_ptr<MCAsmInfo> MAI;
41
- std::unique_ptr<MCContext> Ctx;
42
- std::unique_ptr<MCDisassembler> Disassembler;
43
- std::unique_ptr<MCInstrInfo> MII;
44
- std::unique_ptr<MCInstPrinter> InstPrinter;
45
- };
46
- } // anonymous namespace
47
-
48
28
namespace llvm {
49
29
50
30
// Helper class that implements the language evaluated by RuntimeDyldChecker.
@@ -296,20 +276,6 @@ class RuntimeDyldCheckerExprEval {
296
276
" " );
297
277
298
278
unsigned OpIdx = OpIdxExpr.getValue ();
299
-
300
- auto printInst = [this ](StringRef Symbol, MCInst Inst,
301
- raw_string_ostream &ErrMsgStream) {
302
- auto TI = getTargetInfo (
303
- Checker.getTripleFromTargetFlag (Checker.getTargetFlag (Symbol)));
304
- if (auto E = TI.takeError ()) {
305
- errs () << " Error obtaining instruction printer: "
306
- << toString (std::move (E)) << " \n " ;
307
- return std::make_pair (EvalResult (ErrMsgStream.str ()), " " );
308
- }
309
- Inst.dump_pretty (ErrMsgStream, TI->InstPrinter .get ());
310
- return std::make_pair (EvalResult (ErrMsgStream.str ()), " " );
311
- };
312
-
313
279
if (OpIdx >= Inst.getNumOperands ()) {
314
280
std::string ErrMsg;
315
281
raw_string_ostream ErrMsgStream (ErrMsg);
@@ -318,8 +284,8 @@ class RuntimeDyldCheckerExprEval {
318
284
<< " '. Instruction has only "
319
285
<< format (" %i" , Inst.getNumOperands ())
320
286
<< " operands.\n Instruction is:\n " ;
321
-
322
- return printInst (Symbol, Inst, ErrMsgStream );
287
+ Inst. dump_pretty (ErrMsgStream, Checker. InstPrinter );
288
+ return std::make_pair ( EvalResult (ErrMsgStream. str ()), " " );
323
289
}
324
290
325
291
const MCOperand &Op = Inst.getOperand (OpIdx);
@@ -328,8 +294,9 @@ class RuntimeDyldCheckerExprEval {
328
294
raw_string_ostream ErrMsgStream (ErrMsg);
329
295
ErrMsgStream << " Operand '" << format (" %i" , OpIdx) << " ' of instruction '"
330
296
<< Symbol << " ' is not an immediate.\n Instruction is:\n " ;
297
+ Inst.dump_pretty (ErrMsgStream, Checker.InstPrinter );
331
298
332
- return printInst (Symbol, Inst, ErrMsgStream );
299
+ return std::make_pair ( EvalResult (ErrMsgStream. str ()), " " );
333
300
}
334
301
335
302
return std::make_pair (EvalResult (Op.getImm ()), RemainingExpr);
@@ -720,101 +687,31 @@ class RuntimeDyldCheckerExprEval {
720
687
721
688
bool decodeInst (StringRef Symbol, MCInst &Inst, uint64_t &Size ,
722
689
int64_t Offset) const {
723
- auto TI = getTargetInfo (
724
- Checker.getTripleFromTargetFlag (Checker.getTargetFlag (Symbol)));
725
-
726
- if (auto E = TI.takeError ()) {
727
- errs () << " Error obtaining disassembler: " << toString (std::move (E))
728
- << " \n " ;
729
- return false ;
730
- }
731
-
690
+ MCDisassembler *Dis = Checker.Disassembler ;
732
691
StringRef SymbolMem = Checker.getSymbolContent (Symbol);
733
692
ArrayRef<uint8_t > SymbolBytes (SymbolMem.bytes_begin () + Offset,
734
693
SymbolMem.size () - Offset);
735
694
736
695
MCDisassembler::DecodeStatus S =
737
- TI-> Disassembler ->getInstruction (Inst, Size , SymbolBytes, 0 , nulls ());
696
+ Dis ->getInstruction (Inst, Size , SymbolBytes, 0 , nulls ());
738
697
739
698
return (S == MCDisassembler::Success);
740
699
}
741
-
742
- Expected<TargetInfo>
743
- getTargetInfo (const Triple &TT,
744
- const SubtargetFeatures &TF = SubtargetFeatures()) const {
745
-
746
- auto TripleName = TT.str ();
747
- std::string ErrorStr;
748
- const Target *TheTarget =
749
- TargetRegistry::lookupTarget (TripleName, ErrorStr);
750
- if (!TheTarget)
751
- return make_error<StringError>(" Error accessing target '" + TripleName +
752
- " ': " + ErrorStr,
753
- inconvertibleErrorCode ());
754
-
755
- std::unique_ptr<MCSubtargetInfo> STI (
756
- TheTarget->createMCSubtargetInfo (TripleName, " " , TF.getString ()));
757
- if (!STI)
758
- return make_error<StringError>(" Unable to create subtarget for " +
759
- TripleName,
760
- inconvertibleErrorCode ());
761
-
762
- std::unique_ptr<MCRegisterInfo> MRI (TheTarget->createMCRegInfo (TripleName));
763
- if (!MRI)
764
- return make_error<StringError>(" Unable to create target register info "
765
- " for " +
766
- TripleName,
767
- inconvertibleErrorCode ());
768
-
769
- MCTargetOptions MCOptions;
770
- std::unique_ptr<MCAsmInfo> MAI (
771
- TheTarget->createMCAsmInfo (*MRI, TripleName, MCOptions));
772
- if (!MAI)
773
- return make_error<StringError>(" Unable to create target asm info " +
774
- TripleName,
775
- inconvertibleErrorCode ());
776
-
777
- auto Ctx = std::make_unique<MCContext>(Triple (TripleName), MAI.get (),
778
- MRI.get (), STI.get ());
779
-
780
- std::unique_ptr<MCDisassembler> Disassembler (
781
- TheTarget->createMCDisassembler (*STI, *Ctx));
782
- if (!Disassembler)
783
- return make_error<StringError>(" Unable to create disassembler for " +
784
- TripleName,
785
- inconvertibleErrorCode ());
786
-
787
- std::unique_ptr<MCInstrInfo> MII (TheTarget->createMCInstrInfo ());
788
- if (!MII)
789
- return make_error<StringError>(" Unable to create instruction info for" +
790
- TripleName,
791
- inconvertibleErrorCode ());
792
-
793
- std::unique_ptr<MCInstPrinter> InstPrinter (TheTarget->createMCInstPrinter (
794
- Triple (TripleName), 0 , *MAI, *MII, *MRI));
795
- if (!InstPrinter)
796
- return make_error<StringError>(
797
- " Unable to create instruction printer for" + TripleName,
798
- inconvertibleErrorCode ());
799
-
800
- return TargetInfo ({TheTarget, std::move (STI), std::move (MRI),
801
- std::move (MAI), std::move (Ctx), std::move (Disassembler),
802
- std::move (MII), std::move (InstPrinter)});
803
- }
804
700
};
805
701
} // namespace llvm
806
702
807
703
RuntimeDyldCheckerImpl::RuntimeDyldCheckerImpl (
808
704
IsSymbolValidFunction IsSymbolValid, GetSymbolInfoFunction GetSymbolInfo,
809
705
GetSectionInfoFunction GetSectionInfo, GetStubInfoFunction GetStubInfo,
810
- GetGOTInfoFunction GetGOTInfo, support::endianness Endianness, Triple TT,
811
- SubtargetFeatures TF, raw_ostream &ErrStream)
706
+ GetGOTInfoFunction GetGOTInfo, support::endianness Endianness,
707
+ MCDisassembler *Disassembler, MCInstPrinter *InstPrinter,
708
+ raw_ostream &ErrStream)
812
709
: IsSymbolValid(std::move(IsSymbolValid)),
813
710
GetSymbolInfo(std::move(GetSymbolInfo)),
814
711
GetSectionInfo(std::move(GetSectionInfo)),
815
712
GetStubInfo(std::move(GetStubInfo)), GetGOTInfo(std::move(GetGOTInfo)),
816
- Endianness(Endianness), TT(std::move(TT)), TF(std::move(TF) ),
817
- ErrStream(ErrStream) {}
713
+ Endianness(Endianness), Disassembler(Disassembler ),
714
+ InstPrinter(InstPrinter), ErrStream(ErrStream) {}
818
715
819
716
bool RuntimeDyldCheckerImpl::check (StringRef CheckExpr) const {
820
717
CheckExpr = CheckExpr.trim ();
@@ -925,36 +822,6 @@ StringRef RuntimeDyldCheckerImpl::getSymbolContent(StringRef Symbol) const {
925
822
return {SymInfo->getContent ().data (), SymInfo->getContent ().size ()};
926
823
}
927
824
928
- TargetFlagsType RuntimeDyldCheckerImpl::getTargetFlag (StringRef Symbol) const {
929
- auto SymInfo = GetSymbolInfo (Symbol);
930
- if (!SymInfo) {
931
- logAllUnhandledErrors (SymInfo.takeError (), errs (), " RTDyldChecker: " );
932
- return TargetFlagsType{};
933
- }
934
- return SymInfo->getTargetFlags ();
935
- }
936
-
937
- Triple
938
- RuntimeDyldCheckerImpl::getTripleFromTargetFlag (TargetFlagsType Flag) const {
939
- Triple TheTriple = TT;
940
-
941
- switch (TT.getArch ()) {
942
- case Triple::ArchType::arm:
943
- if (~Flag & 0x1 )
944
- return TT;
945
- TheTriple.setArchName ((Twine (" thumb" ) + TT.getArchName ().substr (3 )).str ());
946
- return TheTriple;
947
- case Triple::ArchType::thumb:
948
- if (Flag & 0x1 )
949
- return TT;
950
- TheTriple.setArchName ((Twine (" arm" ) + TT.getArchName ().substr (5 )).str ());
951
- return TheTriple;
952
-
953
- default :
954
- return TT;
955
- }
956
- }
957
-
958
825
std::pair<uint64_t , std::string> RuntimeDyldCheckerImpl::getSectionAddr (
959
826
StringRef FileName, StringRef SectionName, bool IsInsideLoad) const {
960
827
@@ -1017,12 +884,13 @@ std::pair<uint64_t, std::string> RuntimeDyldCheckerImpl::getStubOrGOTAddrFor(
1017
884
RuntimeDyldChecker::RuntimeDyldChecker (
1018
885
IsSymbolValidFunction IsSymbolValid, GetSymbolInfoFunction GetSymbolInfo,
1019
886
GetSectionInfoFunction GetSectionInfo, GetStubInfoFunction GetStubInfo,
1020
- GetGOTInfoFunction GetGOTInfo, support::endianness Endianness, Triple TT,
1021
- SubtargetFeatures TF, raw_ostream &ErrStream)
887
+ GetGOTInfoFunction GetGOTInfo, support::endianness Endianness,
888
+ MCDisassembler *Disassembler, MCInstPrinter *InstPrinter,
889
+ raw_ostream &ErrStream)
1022
890
: Impl(::std::make_unique<RuntimeDyldCheckerImpl>(
1023
891
std::move (IsSymbolValid), std::move(GetSymbolInfo),
1024
892
std::move(GetSectionInfo), std::move(GetStubInfo),
1025
- std::move(GetGOTInfo), Endianness, std::move(TT), std::move(TF) ,
893
+ std::move(GetGOTInfo), Endianness, Disassembler, InstPrinter ,
1026
894
ErrStream)) {}
1027
895
1028
896
RuntimeDyldChecker::~RuntimeDyldChecker () = default ;
0 commit comments