Object.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_Offload:
  108. return LLVMBinaryTypeOffload;
  109. case ID_Wasm:
  110. return LLVMBinaryTypeWasm;
  111. case ID_StartObjects:
  112. case ID_EndObjects:
  113. llvm_unreachable("Marker types are not valid binary kinds!");
  114. default:
  115. llvm_unreachable("Unknown binary kind!");
  116. }
  117. }
  118. };
  119. return BinaryTypeMapper::mapBinaryTypeToLLVMBinaryType(unwrap(BR)->getType());
  120. }
  121. LLVMBinaryRef LLVMMachOUniversalBinaryCopyObjectForArch(LLVMBinaryRef BR,
  122. const char *Arch,
  123. size_t ArchLen,
  124. char **ErrorMessage) {
  125. auto universal = cast<MachOUniversalBinary>(unwrap(BR));
  126. Expected<std::unique_ptr<ObjectFile>> ObjOrErr(
  127. universal->getMachOObjectForArch({Arch, ArchLen}));
  128. if (!ObjOrErr) {
  129. *ErrorMessage = strdup(toString(ObjOrErr.takeError()).c_str());
  130. return nullptr;
  131. }
  132. return wrap(ObjOrErr.get().release());
  133. }
  134. LLVMSectionIteratorRef LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR) {
  135. auto OF = cast<ObjectFile>(unwrap(BR));
  136. auto sections = OF->sections();
  137. if (sections.begin() == sections.end())
  138. return nullptr;
  139. return wrap(new section_iterator(sections.begin()));
  140. }
  141. LLVMBool LLVMObjectFileIsSectionIteratorAtEnd(LLVMBinaryRef BR,
  142. LLVMSectionIteratorRef SI) {
  143. auto OF = cast<ObjectFile>(unwrap(BR));
  144. return (*unwrap(SI) == OF->section_end()) ? 1 : 0;
  145. }
  146. LLVMSymbolIteratorRef LLVMObjectFileCopySymbolIterator(LLVMBinaryRef BR) {
  147. auto OF = cast<ObjectFile>(unwrap(BR));
  148. auto symbols = OF->symbols();
  149. if (symbols.begin() == symbols.end())
  150. return nullptr;
  151. return wrap(new symbol_iterator(symbols.begin()));
  152. }
  153. LLVMBool LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,
  154. LLVMSymbolIteratorRef SI) {
  155. auto OF = cast<ObjectFile>(unwrap(BR));
  156. return (*unwrap(SI) == OF->symbol_end()) ? 1 : 0;
  157. }
  158. // ObjectFile creation
  159. LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
  160. std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
  161. Expected<std::unique_ptr<ObjectFile>> ObjOrErr(
  162. ObjectFile::createObjectFile(Buf->getMemBufferRef()));
  163. std::unique_ptr<ObjectFile> Obj;
  164. if (!ObjOrErr) {
  165. // TODO: Actually report errors helpfully.
  166. consumeError(ObjOrErr.takeError());
  167. return nullptr;
  168. }
  169. auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
  170. return wrap(Ret);
  171. }
  172. void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
  173. delete unwrap(ObjectFile);
  174. }
  175. // ObjectFile Section iterators
  176. LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF) {
  177. OwningBinary<ObjectFile> *OB = unwrap(OF);
  178. section_iterator SI = OB->getBinary()->section_begin();
  179. return wrap(new section_iterator(SI));
  180. }
  181. void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
  182. delete unwrap(SI);
  183. }
  184. LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF,
  185. LLVMSectionIteratorRef SI) {
  186. OwningBinary<ObjectFile> *OB = unwrap(OF);
  187. return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0;
  188. }
  189. void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
  190. ++(*unwrap(SI));
  191. }
  192. void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
  193. LLVMSymbolIteratorRef Sym) {
  194. Expected<section_iterator> SecOrErr = (*unwrap(Sym))->getSection();
  195. if (!SecOrErr) {
  196. std::string Buf;
  197. raw_string_ostream OS(Buf);
  198. logAllUnhandledErrors(SecOrErr.takeError(), OS);
  199. report_fatal_error(Twine(OS.str()));
  200. }
  201. *unwrap(Sect) = *SecOrErr;
  202. }
  203. // ObjectFile Symbol iterators
  204. LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF) {
  205. OwningBinary<ObjectFile> *OB = unwrap(OF);
  206. symbol_iterator SI = OB->getBinary()->symbol_begin();
  207. return wrap(new symbol_iterator(SI));
  208. }
  209. void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
  210. delete unwrap(SI);
  211. }
  212. LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF,
  213. LLVMSymbolIteratorRef SI) {
  214. OwningBinary<ObjectFile> *OB = unwrap(OF);
  215. return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0;
  216. }
  217. void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
  218. ++(*unwrap(SI));
  219. }
  220. // SectionRef accessors
  221. const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
  222. auto NameOrErr = (*unwrap(SI))->getName();
  223. if (!NameOrErr)
  224. report_fatal_error(NameOrErr.takeError());
  225. return NameOrErr->data();
  226. }
  227. uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
  228. return (*unwrap(SI))->getSize();
  229. }
  230. const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
  231. if (Expected<StringRef> E = (*unwrap(SI))->getContents())
  232. return E->data();
  233. else
  234. report_fatal_error(E.takeError());
  235. }
  236. uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
  237. return (*unwrap(SI))->getAddress();
  238. }
  239. LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
  240. LLVMSymbolIteratorRef Sym) {
  241. return (*unwrap(SI))->containsSymbol(**unwrap(Sym));
  242. }
  243. // Section Relocation iterators
  244. LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
  245. relocation_iterator SI = (*unwrap(Section))->relocation_begin();
  246. return wrap(new relocation_iterator(SI));
  247. }
  248. void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
  249. delete unwrap(SI);
  250. }
  251. LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
  252. LLVMRelocationIteratorRef SI) {
  253. return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
  254. }
  255. void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
  256. ++(*unwrap(SI));
  257. }
  258. // SymbolRef accessors
  259. const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
  260. Expected<StringRef> Ret = (*unwrap(SI))->getName();
  261. if (!Ret) {
  262. std::string Buf;
  263. raw_string_ostream OS(Buf);
  264. logAllUnhandledErrors(Ret.takeError(), OS);
  265. report_fatal_error(Twine(OS.str()));
  266. }
  267. return Ret->data();
  268. }
  269. uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
  270. Expected<uint64_t> Ret = (*unwrap(SI))->getAddress();
  271. if (!Ret) {
  272. std::string Buf;
  273. raw_string_ostream OS(Buf);
  274. logAllUnhandledErrors(Ret.takeError(), OS);
  275. report_fatal_error(Twine(OS.str()));
  276. }
  277. return *Ret;
  278. }
  279. uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
  280. return (*unwrap(SI))->getCommonSize();
  281. }
  282. // RelocationRef accessors
  283. uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
  284. return (*unwrap(RI))->getOffset();
  285. }
  286. LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
  287. symbol_iterator ret = (*unwrap(RI))->getSymbol();
  288. return wrap(new symbol_iterator(ret));
  289. }
  290. uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
  291. return (*unwrap(RI))->getType();
  292. }
  293. // NOTE: Caller takes ownership of returned string.
  294. const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
  295. SmallVector<char, 0> ret;
  296. (*unwrap(RI))->getTypeName(ret);
  297. char *str = static_cast<char*>(safe_malloc(ret.size()));
  298. llvm::copy(ret, str);
  299. return str;
  300. }
  301. // NOTE: Caller takes ownership of returned string.
  302. const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
  303. return strdup("");
  304. }