ObjectFileInterface.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //===------ ObjectFileInterface.cpp - MU interface utils for objects ------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
  9. #include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
  10. #include "llvm/ExecutionEngine/Orc/MachOPlatform.h"
  11. #include "llvm/Object/ELFObjectFile.h"
  12. #include "llvm/Object/MachO.h"
  13. #include "llvm/Object/ObjectFile.h"
  14. #include "llvm/Support/Debug.h"
  15. #define DEBUG_TYPE "orc"
  16. namespace llvm {
  17. namespace orc {
  18. void addInitSymbol(MaterializationUnit::Interface &I, ExecutionSession &ES,
  19. StringRef ObjFileName) {
  20. assert(!I.InitSymbol && "I already has an init symbol");
  21. size_t Counter = 0;
  22. do {
  23. std::string InitSymString;
  24. raw_string_ostream(InitSymString)
  25. << "$." << ObjFileName << ".__inits." << Counter++;
  26. I.InitSymbol = ES.intern(InitSymString);
  27. } while (I.SymbolFlags.count(I.InitSymbol));
  28. I.SymbolFlags[I.InitSymbol] = JITSymbolFlags::MaterializationSideEffectsOnly;
  29. }
  30. static Expected<MaterializationUnit::Interface>
  31. getMachOObjectFileSymbolInfo(ExecutionSession &ES,
  32. const object::MachOObjectFile &Obj) {
  33. MaterializationUnit::Interface I;
  34. for (auto &Sym : Obj.symbols()) {
  35. Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
  36. if (!SymFlagsOrErr)
  37. // TODO: Test this error.
  38. return SymFlagsOrErr.takeError();
  39. // Skip symbols not defined in this object file.
  40. if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
  41. continue;
  42. // Skip symbols that are not global.
  43. if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
  44. continue;
  45. // Skip symbols that have type SF_File.
  46. if (auto SymType = Sym.getType()) {
  47. if (*SymType == object::SymbolRef::ST_File)
  48. continue;
  49. } else
  50. return SymType.takeError();
  51. auto Name = Sym.getName();
  52. if (!Name)
  53. return Name.takeError();
  54. auto InternedName = ES.intern(*Name);
  55. auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
  56. if (!SymFlags)
  57. return SymFlags.takeError();
  58. // Strip the 'exported' flag from MachO linker-private symbols.
  59. if (Name->startswith("l"))
  60. *SymFlags &= ~JITSymbolFlags::Exported;
  61. I.SymbolFlags[InternedName] = std::move(*SymFlags);
  62. }
  63. for (auto &Sec : Obj.sections()) {
  64. auto SecType = Obj.getSectionType(Sec);
  65. if ((SecType & MachO::SECTION_TYPE) == MachO::S_MOD_INIT_FUNC_POINTERS) {
  66. addInitSymbol(I, ES, Obj.getFileName());
  67. break;
  68. }
  69. auto SegName = Obj.getSectionFinalSegmentName(Sec.getRawDataRefImpl());
  70. auto SecName = cantFail(Obj.getSectionName(Sec.getRawDataRefImpl()));
  71. if (MachOPlatform::isInitializerSection(SegName, SecName)) {
  72. addInitSymbol(I, ES, Obj.getFileName());
  73. break;
  74. }
  75. }
  76. return I;
  77. }
  78. static Expected<MaterializationUnit::Interface>
  79. getELFObjectFileSymbolInfo(ExecutionSession &ES,
  80. const object::ELFObjectFileBase &Obj) {
  81. MaterializationUnit::Interface I;
  82. for (auto &Sym : Obj.symbols()) {
  83. Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
  84. if (!SymFlagsOrErr)
  85. // TODO: Test this error.
  86. return SymFlagsOrErr.takeError();
  87. // Skip symbols not defined in this object file.
  88. if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
  89. continue;
  90. // Skip symbols that are not global.
  91. if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
  92. continue;
  93. // Skip symbols that have type SF_File.
  94. if (auto SymType = Sym.getType()) {
  95. if (*SymType == object::SymbolRef::ST_File)
  96. continue;
  97. } else
  98. return SymType.takeError();
  99. auto Name = Sym.getName();
  100. if (!Name)
  101. return Name.takeError();
  102. auto InternedName = ES.intern(*Name);
  103. auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
  104. if (!SymFlags)
  105. return SymFlags.takeError();
  106. // ELF STB_GNU_UNIQUE should map to Weak for ORC.
  107. if (Sym.getBinding() == ELF::STB_GNU_UNIQUE)
  108. *SymFlags |= JITSymbolFlags::Weak;
  109. I.SymbolFlags[InternedName] = std::move(*SymFlags);
  110. }
  111. SymbolStringPtr InitSymbol;
  112. for (auto &Sec : Obj.sections()) {
  113. if (auto SecName = Sec.getName()) {
  114. if (ELFNixPlatform::isInitializerSection(*SecName)) {
  115. addInitSymbol(I, ES, Obj.getFileName());
  116. break;
  117. }
  118. }
  119. }
  120. return I;
  121. }
  122. Expected<MaterializationUnit::Interface>
  123. getGenericObjectFileSymbolInfo(ExecutionSession &ES,
  124. const object::ObjectFile &Obj) {
  125. MaterializationUnit::Interface I;
  126. for (auto &Sym : Obj.symbols()) {
  127. Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
  128. if (!SymFlagsOrErr)
  129. // TODO: Test this error.
  130. return SymFlagsOrErr.takeError();
  131. // Skip symbols not defined in this object file.
  132. if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
  133. continue;
  134. // Skip symbols that are not global.
  135. if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
  136. continue;
  137. // Skip symbols that have type SF_File.
  138. if (auto SymType = Sym.getType()) {
  139. if (*SymType == object::SymbolRef::ST_File)
  140. continue;
  141. } else
  142. return SymType.takeError();
  143. auto Name = Sym.getName();
  144. if (!Name)
  145. return Name.takeError();
  146. auto InternedName = ES.intern(*Name);
  147. auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
  148. if (!SymFlags)
  149. return SymFlags.takeError();
  150. I.SymbolFlags[InternedName] = std::move(*SymFlags);
  151. }
  152. return I;
  153. }
  154. Expected<MaterializationUnit::Interface>
  155. getObjectFileInterface(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
  156. auto Obj = object::ObjectFile::createObjectFile(ObjBuffer);
  157. if (!Obj)
  158. return Obj.takeError();
  159. if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(Obj->get()))
  160. return getMachOObjectFileSymbolInfo(ES, *MachOObj);
  161. else if (auto *ELFObj = dyn_cast<object::ELFObjectFileBase>(Obj->get()))
  162. return getELFObjectFileSymbolInfo(ES, *ELFObj);
  163. return getGenericObjectFileSymbolInfo(ES, **Obj);
  164. }
  165. } // End namespace orc.
  166. } // End namespace llvm.