Object.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //===- Object.cpp - C bindings to the object file library--------*- 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. // This file defines the C bindings to the file-format-independent object
  11. // library.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm-c/Object.h"
  16. #include "llvm/Object/ObjectFile.h"
  17. using namespace llvm;
  18. using namespace object;
  19. inline OwningBinary<ObjectFile> *unwrap(LLVMObjectFileRef OF) {
  20. return reinterpret_cast<OwningBinary<ObjectFile> *>(OF);
  21. }
  22. inline LLVMObjectFileRef wrap(const OwningBinary<ObjectFile> *OF) {
  23. return reinterpret_cast<LLVMObjectFileRef>(
  24. const_cast<OwningBinary<ObjectFile> *>(OF));
  25. }
  26. inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
  27. return reinterpret_cast<section_iterator*>(SI);
  28. }
  29. inline LLVMSectionIteratorRef
  30. wrap(const section_iterator *SI) {
  31. return reinterpret_cast<LLVMSectionIteratorRef>
  32. (const_cast<section_iterator*>(SI));
  33. }
  34. inline symbol_iterator *unwrap(LLVMSymbolIteratorRef SI) {
  35. return reinterpret_cast<symbol_iterator*>(SI);
  36. }
  37. inline LLVMSymbolIteratorRef
  38. wrap(const symbol_iterator *SI) {
  39. return reinterpret_cast<LLVMSymbolIteratorRef>
  40. (const_cast<symbol_iterator*>(SI));
  41. }
  42. inline relocation_iterator *unwrap(LLVMRelocationIteratorRef SI) {
  43. return reinterpret_cast<relocation_iterator*>(SI);
  44. }
  45. inline LLVMRelocationIteratorRef
  46. wrap(const relocation_iterator *SI) {
  47. return reinterpret_cast<LLVMRelocationIteratorRef>
  48. (const_cast<relocation_iterator*>(SI));
  49. }
  50. // ObjectFile creation
  51. LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
  52. std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
  53. ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr(
  54. ObjectFile::createObjectFile(Buf->getMemBufferRef()));
  55. std::unique_ptr<ObjectFile> Obj;
  56. if (!ObjOrErr)
  57. return nullptr;
  58. auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
  59. return wrap(Ret);
  60. }
  61. void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
  62. delete unwrap(ObjectFile);
  63. }
  64. // ObjectFile Section iterators
  65. LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF) {
  66. OwningBinary<ObjectFile> *OB = unwrap(OF);
  67. section_iterator SI = OB->getBinary()->section_begin();
  68. return wrap(new section_iterator(SI));
  69. }
  70. void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
  71. delete unwrap(SI);
  72. }
  73. LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF,
  74. LLVMSectionIteratorRef SI) {
  75. OwningBinary<ObjectFile> *OB = unwrap(OF);
  76. return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0;
  77. }
  78. void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
  79. ++(*unwrap(SI));
  80. }
  81. void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
  82. LLVMSymbolIteratorRef Sym) {
  83. if (std::error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
  84. report_fatal_error(ec.message());
  85. }
  86. // ObjectFile Symbol iterators
  87. LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF) {
  88. OwningBinary<ObjectFile> *OB = unwrap(OF);
  89. symbol_iterator SI = OB->getBinary()->symbol_begin();
  90. return wrap(new symbol_iterator(SI));
  91. }
  92. void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
  93. delete unwrap(SI);
  94. }
  95. LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF,
  96. LLVMSymbolIteratorRef SI) {
  97. OwningBinary<ObjectFile> *OB = unwrap(OF);
  98. return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0;
  99. }
  100. void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
  101. ++(*unwrap(SI));
  102. }
  103. // SectionRef accessors
  104. const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
  105. StringRef ret;
  106. if (std::error_code ec = (*unwrap(SI))->getName(ret))
  107. report_fatal_error(ec.message());
  108. return ret.data();
  109. }
  110. uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
  111. return (*unwrap(SI))->getSize();
  112. }
  113. const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
  114. StringRef ret;
  115. if (std::error_code ec = (*unwrap(SI))->getContents(ret))
  116. report_fatal_error(ec.message());
  117. return ret.data();
  118. }
  119. uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
  120. return (*unwrap(SI))->getAddress();
  121. }
  122. LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
  123. LLVMSymbolIteratorRef Sym) {
  124. return (*unwrap(SI))->containsSymbol(**unwrap(Sym));
  125. }
  126. // Section Relocation iterators
  127. LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
  128. relocation_iterator SI = (*unwrap(Section))->relocation_begin();
  129. return wrap(new relocation_iterator(SI));
  130. }
  131. void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
  132. delete unwrap(SI);
  133. }
  134. LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
  135. LLVMRelocationIteratorRef SI) {
  136. return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
  137. }
  138. void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
  139. ++(*unwrap(SI));
  140. }
  141. // SymbolRef accessors
  142. const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
  143. ErrorOr<StringRef> Ret = (*unwrap(SI))->getName();
  144. if (std::error_code EC = Ret.getError())
  145. report_fatal_error(EC.message());
  146. return Ret->data();
  147. }
  148. uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
  149. ErrorOr<uint64_t> Ret = (*unwrap(SI))->getAddress();
  150. if (std::error_code EC = Ret.getError())
  151. report_fatal_error(EC.message());
  152. return *Ret;
  153. }
  154. uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
  155. return (*unwrap(SI))->getCommonSize();
  156. }
  157. // RelocationRef accessors
  158. uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
  159. return (*unwrap(RI))->getOffset();
  160. }
  161. LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
  162. symbol_iterator ret = (*unwrap(RI))->getSymbol();
  163. return wrap(new symbol_iterator(ret));
  164. }
  165. uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
  166. return (*unwrap(RI))->getType();
  167. }
  168. // NOTE: Caller takes ownership of returned string.
  169. const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
  170. SmallVector<char, 0> ret;
  171. (*unwrap(RI))->getTypeName(ret);
  172. char *str = static_cast<char*>(malloc(ret.size()));
  173. std::copy(ret.begin(), ret.end(), str);
  174. return str;
  175. }
  176. // NOTE: Caller takes ownership of returned string.
  177. const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
  178. return strdup("");
  179. }