• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------- JITSymbol.cpp - JITSymbol class implementation -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // JITSymbol class implementation plus helper functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ExecutionEngine/JITSymbol.h"
15 #include "llvm/IR/GlobalValue.h"
16 #include "llvm/Object/SymbolicFile.h"
17 
18 using namespace llvm;
19 
fromGlobalValue(const GlobalValue & GV)20 JITSymbolFlags llvm::JITSymbolFlags::fromGlobalValue(const GlobalValue &GV) {
21   JITSymbolFlags Flags = JITSymbolFlags::None;
22   if (GV.hasWeakLinkage() || GV.hasLinkOnceLinkage())
23     Flags |= JITSymbolFlags::Weak;
24   if (GV.hasCommonLinkage())
25     Flags |= JITSymbolFlags::Common;
26   if (!GV.hasLocalLinkage() && !GV.hasHiddenVisibility())
27     Flags |= JITSymbolFlags::Exported;
28   return Flags;
29 }
30 
31 JITSymbolFlags
fromObjectSymbol(const object::BasicSymbolRef & Symbol)32 llvm::JITSymbolFlags::fromObjectSymbol(const object::BasicSymbolRef &Symbol) {
33   JITSymbolFlags Flags = JITSymbolFlags::None;
34   if (Symbol.getFlags() & object::BasicSymbolRef::SF_Weak)
35     Flags |= JITSymbolFlags::Weak;
36   if (Symbol.getFlags() & object::BasicSymbolRef::SF_Common)
37     Flags |= JITSymbolFlags::Common;
38   if (Symbol.getFlags() & object::BasicSymbolRef::SF_Exported)
39     Flags |= JITSymbolFlags::Exported;
40   return Flags;
41 }
42 
fromObjectSymbol(const object::BasicSymbolRef & Symbol)43 ARMJITSymbolFlags llvm::ARMJITSymbolFlags::fromObjectSymbol(
44                                          const object::BasicSymbolRef &Symbol) {
45   ARMJITSymbolFlags Flags;
46   if (Symbol.getFlags() & object::BasicSymbolRef::SF_Thumb)
47     Flags |= ARMJITSymbolFlags::Thumb;
48   return Flags;
49 }
50 
51 /// Performs lookup by, for each symbol, first calling
52 ///        findSymbolInLogicalDylib and if that fails calling
53 ///        findSymbol.
54 Expected<JITSymbolResolver::LookupResult>
lookup(const LookupSet & Symbols)55 LegacyJITSymbolResolver::lookup(const LookupSet &Symbols) {
56   JITSymbolResolver::LookupResult Result;
57   for (auto &Symbol : Symbols) {
58     std::string SymName = Symbol.str();
59     if (auto Sym = findSymbolInLogicalDylib(SymName)) {
60       if (auto AddrOrErr = Sym.getAddress())
61         Result[Symbol] = JITEvaluatedSymbol(*AddrOrErr, Sym.getFlags());
62       else
63         return AddrOrErr.takeError();
64     } else if (auto Err = Sym.takeError())
65       return std::move(Err);
66     else {
67       // findSymbolInLogicalDylib failed. Lets try findSymbol.
68       if (auto Sym = findSymbol(SymName)) {
69         if (auto AddrOrErr = Sym.getAddress())
70           Result[Symbol] = JITEvaluatedSymbol(*AddrOrErr, Sym.getFlags());
71         else
72           return AddrOrErr.takeError();
73       } else if (auto Err = Sym.takeError())
74         return std::move(Err);
75       else
76         return make_error<StringError>("Symbol not found: " + Symbol,
77                                        inconvertibleErrorCode());
78     }
79   }
80 
81   return std::move(Result);
82 }
83 
84 /// Performs flags lookup by calling findSymbolInLogicalDylib and
85 ///        returning the flags value for that symbol.
86 Expected<JITSymbolResolver::LookupFlagsResult>
lookupFlags(const LookupSet & Symbols)87 LegacyJITSymbolResolver::lookupFlags(const LookupSet &Symbols) {
88   JITSymbolResolver::LookupFlagsResult Result;
89 
90   for (auto &Symbol : Symbols) {
91     std::string SymName = Symbol.str();
92     if (auto Sym = findSymbolInLogicalDylib(SymName))
93       Result[Symbol] = Sym.getFlags();
94     else if (auto Err = Sym.takeError())
95       return std::move(Err);
96   }
97 
98   return std::move(Result);
99 }
100