Object.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //===- Object.cpp - C bindings to the object file library--------*- C++ -*-===//
  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. //
  9. // This file defines the C bindings to the file-format-independent object
  10. // library.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm-c/Object.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/Object/ObjectFile.h"
  17. #include "llvm/Object/MachOUniversal.h"
  18. #include "llvm/Support/MemAlloc.h"
  19. using namespace llvm;
  20. using namespace object;
  21. inline OwningBinary<ObjectFile> *unwrap(LLVMObjectFileRef OF) {
  22. return reinterpret_cast<OwningBinary<ObjectFile> *>(OF);
  23. }
  24. inline LLVMObjectFileRef wrap(const OwningBinary<ObjectFile> *OF) {
  25. return reinterpret_cast<LLVMObjectFileRef>(
  26. const_cast<OwningBinary<ObjectFile> *>(OF));
  27. }
  28. inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
  29. return reinterpret_cast<section_iterator*>(SI);
  30. }
  31. inline LLVMSectionIteratorRef
  32. wrap(const section_iterator *SI) {
  33. return reinterpret_cast<LLVMSectionIteratorRef>
  34. (const_cast<section_iterator*>(SI));
  35. }
  36. inline symbol_iterator *unwrap(LLVMSymbolIteratorRef SI) {
  37. return reinterpret_cast<symbol_iterator*>(SI);
  38. }
  39. inline LLVMSymbolIteratorRef
  40. wrap(const symbol_iterator *SI) {
  41. return reinterpret_cast<LLVMSymbolIteratorRef>
  42. (const_cast<symbol_iterator*>(SI));
  43. }
  44. inline relocation_iterator *unwrap(LLVMRelocationIteratorRef SI) {
  45. return reinterpret_cast<relocation_iterator*>(SI);
  46. }
  47. inline LLVMRelocationIteratorRef
  48. wrap(const relocation_iterator *SI) {
  49. return reinterpret_cast<LLVMRelocationIteratorRef>
  50. (const_cast<relocation_iterator*>(SI));
  51. }
  52. /*--.. Operations on binary files ..........................................--*/
  53. LLVMBinaryRef LLVMCreateBinary(LLVMMemoryBufferRef MemBuf,
  54. LLVMContextRef Context,
  55. char **ErrorMessage) {
  56. auto maybeContext = Context ? unwrap(Context) : nullptr;
  57. Expected<std::unique_ptr<Binary>> ObjOrErr(
  58. createBinary(unwrap(MemBuf)->getMemBufferRef(), maybeContext));
  59. if (!ObjOrErr) {
  60. *ErrorMessage = strdup(toString(ObjOrErr.takeError()).c_str());
  61. return nullptr;
  62. }
  63. return wrap(ObjOrErr.get().release());
  64. }
  65. LLVMMemoryBufferRef LLVMBinaryCopyMemoryBuffer(LLVMBinaryRef BR) {
  66. auto Buf = unwrap(BR)->getMemoryBufferRef();
  67. return wrap(llvm::MemoryBuffer::getMemBuffer(
  68. Buf.getBuffer(), Buf.getBufferIdentifier(),
  69. /*RequiresNullTerminator*/false).release());
  70. }
  71. void LLVMDisposeBinary(LLVMBinaryRef BR) {
  72. delete unwrap(BR);
  73. }
  74. LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR) {
  75. class BinaryTypeMapper final : public Binary {
  76. public:
  77. static LLVMBinaryType mapBinaryTypeToLLVMBinaryType(unsigned Kind) {
  78. switch (Kind) {
  79. case ID_Archive:
  80. return LLVMBinaryTypeArchive;
  81. case ID_MachOUniversalBinary:
  82. return LLVMBinaryTypeMachOUniversalBinary;
  83. case ID_COFFImportFile:
  84. return LLVMBinaryTypeCOFFImportFile;
  85. case ID_IR:
  86. return LLVMBinaryTypeIR;
  87. case ID_WinRes:
  88. return LLVMBinaryTypeWinRes;
  89. case ID_COFF:
  90. return LLVMBinaryTypeCOFF;
  91. case ID_ELF32L:
  92. return LLVMBinaryTypeELF32L;
  93. case ID_ELF32B:
  94. return LLVMBinaryTypeELF32B;
  95. case ID_ELF64L:
  96. return LLVMBinaryTypeELF64L;
  97. case ID_ELF64B:
  98. return LLVMBinaryTypeELF64B;
  99. case ID_MachO32L:
  100. return LLVMBinaryTypeMachO32L;
  101. case ID_MachO32B:
  102. return LLVMBinaryTypeMachO32B;
  103. case ID_MachO64L:
  104. return LLVMBinaryTypeMachO64L;
  105. case ID_MachO64B:
  106. return LLVMBinaryTypeMachO64B;
  107. case ID_Wasm:
  108. return LLVMBinaryTypeWasm;
  109. case ID_StartObjects:
  110. case ID_EndObjects:
  111. llvm_unreachable("Marker types are not valid binary kinds!");
  112. default:
  113. llvm_unreachable("Unknown binary kind!");
  114. }
  115. }
  116. };
  117. return BinaryTypeMapper::mapBinaryTypeToLLVMBinaryType(unwrap(BR)->getType());
  118. }
  119. LLVMBinaryRef LLVMMachOUniversalBinaryCopyObjectForArch(LLVMBinaryRef BR,
  120. const char *Arch,
  121. size_t ArchLen,
  122. char **ErrorMessage) {
  123. auto universal = cast<MachOUniversalBinary>(unwrap(BR));
  124. Expected<std::unique_ptr<ObjectFile>> ObjOrErr(
  125. universal->getMachOObjectForArch({Arch, ArchLen}));
  126. if (!ObjOrErr) {
  127. *ErrorMessage = strdup(toString(ObjOrErr.takeError()).c_str());
  128. return nullptr;
  129. }
  130. return wrap(ObjOrErr.get().release());
  131. }
  132. LLVMSectionIteratorRef LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR) {
  133. auto OF = cast<ObjectFile>(unwrap(BR));
  134. auto sections = OF->sections();
  135. if (sections.begin() == sections.end())
  136. return nullptr;
  137. return wrap(new section_iterator(sections.begin()));
  138. }
  139. LLVMBool LLVMObjectFileIsSectionIteratorAtEnd(LLVMBinaryRef BR,
  140. LLVMSectionIteratorRef SI) {
  141. auto OF = cast<ObjectFile>(unwrap(BR));
  142. return (*unwrap(SI) == OF->section_end()) ? 1 : 0;
  143. }
  144. LLVMSymbolIteratorRef LLVMObjectFileCopySymbolIterator(LLVMBinaryRef BR) {
  145. auto OF = cast<ObjectFile>(unwrap(BR));
  146. auto symbols = OF->symbols();
  147. if (symbols.begin() == symbols.end())
  148. return nullptr;
  149. return wrap(new symbol_iterator(symbols.begin()));
  150. }
  151. LLVMBool LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,
  152. LLVMSymbolIteratorRef SI) {
  153. auto OF = cast<ObjectFile>(unwrap(BR));
  154. return (*unwrap(SI) == OF->symbol_end()) ? 1 : 0;
  155. }
  156. // ObjectFile creation
  157. LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
  158. std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
  159. Expected<std::unique_ptr<ObjectFile>> ObjOrErr(
  160. ObjectFile::createObjectFile(Buf->getMemBufferRef()));
  161. std::unique_ptr<ObjectFile> Obj;
  162. if (!ObjOrErr) {
  163. // TODO: Actually report errors helpfully.
  164. consumeError(ObjOrErr.takeError());
  165. return nullptr;
  166. }
  167. auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
  168. return wrap(Ret);
  169. }
  170. void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
  171. delete unwrap(ObjectFile);
  172. }
  173. // ObjectFile Section iterators
  174. LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF) {
  175. OwningBinary<ObjectFile> *OB = unwrap(OF);
  176. section_iterator SI = OB->getBinary()->section_begin();
  177. return wrap(new section_iterator(SI));
  178. }
  179. void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
  180. delete unwrap(SI);
  181. }
  182. LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF,
  183. LLVMSectionIteratorRef SI) {
  184. OwningBinary<ObjectFile> *OB = unwrap(OF);
  185. return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0;
  186. }
  187. void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
  188. ++(*unwrap(SI));
  189. }
  190. void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
  191. LLVMSymbolIteratorRef Sym) {
  192. Expected<section_iterator> SecOrErr = (*unwrap(Sym))->getSection();
  193. if (!SecOrErr) {
  194. std::string Buf;
  195. raw_string_ostream OS(Buf);
  196. logAllUnhandledErrors(SecOrErr.takeError(), OS);
  197. report_fatal_error(Twine(OS.str()));
  198. }
  199. *unwrap(Sect) = *SecOrErr;
  200. }
  201. // ObjectFile Symbol iterators
  202. LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF) {
  203. OwningBinary<ObjectFile> *OB = unwrap(OF);
  204. symbol_iterator SI = OB->getBinary()->symbol_begin();
  205. return wrap(new symbol_iterator(SI));
  206. }
  207. void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
  208. delete unwrap(SI);
  209. }
  210. LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF,
  211. LLVMSymbolIteratorRef SI) {
  212. OwningBinary<ObjectFile> *OB = unwrap(OF);
  213. return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0;
  214. }
  215. void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
  216. ++(*unwrap(SI));
  217. }
  218. // SectionRef accessors
  219. const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
  220. auto NameOrErr = (*unwrap(SI))->getName();
  221. if (!NameOrErr)
  222. report_fatal_error(NameOrErr.takeError());
  223. return NameOrErr->data();
  224. }
  225. uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
  226. return (*unwrap(SI))->getSize();
  227. }
  228. const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
  229. if (Expected<StringRef> E = (*unwrap(SI))->getContents())
  230. return E->data();
  231. else
  232. report_fatal_error(E.takeError());
  233. }
  234. uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
  235. return (*unwrap(SI))->getAddress();
  236. }
  237. LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
  238. LLVMSymbolIteratorRef Sym) {
  239. return (*unwrap(SI))->containsSymbol(**unwrap(Sym));
  240. }
  241. // Section Relocation iterators
  242. LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
  243. relocation_iterator SI = (*unwrap(Section))->relocation_begin();
  244. return wrap(new relocation_iterator(SI));
  245. }
  246. void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
  247. delete unwrap(SI);
  248. }
  249. LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
  250. LLVMRelocationIteratorRef SI) {
  251. return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
  252. }
  253. void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
  254. ++(*unwrap(SI));
  255. }
  256. // SymbolRef accessors
  257. const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
  258. Expected<StringRef> Ret = (*unwrap(SI))->getName();
  259. if (!Ret) {
  260. std::string Buf;
  261. raw_string_ostream OS(Buf);
  262. logAllUnhandledErrors(Ret.takeError(), OS);
  263. report_fatal_error(Twine(OS.str()));
  264. }
  265. return Ret->data();
  266. }
  267. uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
  268. Expected<uint64_t> Ret = (*unwrap(SI))->getAddress();
  269. if (!Ret) {
  270. std::string Buf;
  271. raw_string_ostream OS(Buf);
  272. logAllUnhandledErrors(Ret.takeError(), OS);
  273. report_fatal_error(Twine(OS.str()));
  274. }
  275. return *Ret;
  276. }
  277. uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
  278. return (*unwrap(SI))->getCommonSize();
  279. }
  280. // RelocationRef accessors
  281. uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
  282. return (*unwrap(RI))->getOffset();
  283. }
  284. LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
  285. symbol_iterator ret = (*unwrap(RI))->getSymbol();
  286. return wrap(new symbol_iterator(ret));
  287. }
  288. uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
  289. return (*unwrap(RI))->getType();
  290. }
  291. // NOTE: Caller takes ownership of returned string.
  292. const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
  293. SmallVector<char, 0> ret;
  294. (*unwrap(RI))->getTypeName(ret);
  295. char *str = static_cast<char*>(safe_malloc(ret.size()));
  296. llvm::copy(ret, str);
  297. return str;
  298. }
  299. // NOTE: Caller takes ownership of returned string.
  300. const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
  301. return strdup("");
  302. }