IRObjectFile.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===//
  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. // Part of the IRObjectFile class implementation.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Bitcode/ReaderWriter.h"
  14. #include "llvm/IR/LLVMContext.h"
  15. #include "llvm/IR/Mangler.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/Object/IRObjectFile.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace llvm;
  20. using namespace object;
  21. IRObjectFile::IRObjectFile(MemoryBuffer *Object, error_code &EC,
  22. LLVMContext &Context, bool BufferOwned)
  23. : SymbolicFile(Binary::ID_IR, Object, BufferOwned) {
  24. ErrorOr<Module*> MOrErr = parseBitcodeFile(Object, Context);
  25. if ((EC = MOrErr.getError()))
  26. return;
  27. M.reset(MOrErr.get());
  28. // If we have a DataLayout, setup a mangler.
  29. const DataLayout *DL = M->getDataLayout();
  30. if (!DL)
  31. return;
  32. Mang.reset(new Mangler(DL));
  33. }
  34. static const GlobalValue &getGV(DataRefImpl &Symb) {
  35. return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
  36. }
  37. static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
  38. if (I == M.alias_end())
  39. return 3;
  40. const GlobalValue *GV = &*I;
  41. return reinterpret_cast<uintptr_t>(GV) | 2;
  42. }
  43. static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
  44. if (I == M.global_end())
  45. return skipEmpty(M.alias_begin(), M);
  46. const GlobalValue *GV = &*I;
  47. return reinterpret_cast<uintptr_t>(GV) | 1;
  48. }
  49. static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
  50. if (I == M.end())
  51. return skipEmpty(M.global_begin(), M);
  52. const GlobalValue *GV = &*I;
  53. return reinterpret_cast<uintptr_t>(GV) | 0;
  54. }
  55. void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
  56. const GlobalValue *GV = &getGV(Symb);
  57. const Module &M = *GV->getParent();
  58. uintptr_t Res;
  59. switch (Symb.p & 3) {
  60. case 0: {
  61. Module::const_iterator Iter(static_cast<const Function*>(GV));
  62. ++Iter;
  63. Res = skipEmpty(Iter, M);
  64. break;
  65. }
  66. case 1: {
  67. Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
  68. ++Iter;
  69. Res = skipEmpty(Iter, M);
  70. break;
  71. }
  72. case 2: {
  73. Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
  74. ++Iter;
  75. Res = skipEmpty(Iter, M);
  76. break;
  77. }
  78. case 3:
  79. llvm_unreachable("Invalid symbol reference");
  80. }
  81. Symb.p = Res;
  82. }
  83. error_code IRObjectFile::printSymbolName(raw_ostream &OS,
  84. DataRefImpl Symb) const {
  85. const GlobalValue &GV = getGV(Symb);
  86. if (Mang)
  87. Mang->getNameWithPrefix(OS, &GV, false);
  88. else
  89. OS << GV.getName();
  90. return object_error::success;
  91. }
  92. uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
  93. const GlobalValue &GV = getGV(Symb);
  94. uint32_t Res = BasicSymbolRef::SF_None;
  95. if (GV.isDeclaration() || GV.hasAvailableExternallyLinkage())
  96. Res |= BasicSymbolRef::SF_Undefined;
  97. if (GV.hasPrivateLinkage())
  98. Res |= BasicSymbolRef::SF_FormatSpecific;
  99. if (!GV.hasLocalLinkage())
  100. Res |= BasicSymbolRef::SF_Global;
  101. if (GV.hasCommonLinkage())
  102. Res |= BasicSymbolRef::SF_Common;
  103. if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
  104. Res |= BasicSymbolRef::SF_Weak;
  105. return Res;
  106. }
  107. const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
  108. const GlobalValue &GV = getGV(Symb);
  109. return GV;
  110. }
  111. basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
  112. Module::const_iterator I = M->begin();
  113. DataRefImpl Ret;
  114. Ret.p = skipEmpty(I, *M);
  115. return basic_symbol_iterator(BasicSymbolRef(Ret, this));
  116. }
  117. basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
  118. DataRefImpl Ret;
  119. Ret.p = 3;
  120. return basic_symbol_iterator(BasicSymbolRef(Ret, this));
  121. }
  122. ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile(
  123. MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) {
  124. error_code EC;
  125. std::unique_ptr<IRObjectFile> Ret(
  126. new IRObjectFile(Object, EC, Context, BufferOwned));
  127. if (EC)
  128. return EC;
  129. return Ret.release();
  130. }