Object.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 ObjectFile *unwrap(LLVMObjectFileRef OF) {
  20. return reinterpret_cast<ObjectFile*>(OF);
  21. }
  22. inline LLVMObjectFileRef wrap(const ObjectFile *OF) {
  23. return reinterpret_cast<LLVMObjectFileRef>(const_cast<ObjectFile*>(OF));
  24. }
  25. inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
  26. return reinterpret_cast<section_iterator*>(SI);
  27. }
  28. inline LLVMSectionIteratorRef
  29. wrap(const section_iterator *SI) {
  30. return reinterpret_cast<LLVMSectionIteratorRef>
  31. (const_cast<section_iterator*>(SI));
  32. }
  33. inline symbol_iterator *unwrap(LLVMSymbolIteratorRef SI) {
  34. return reinterpret_cast<symbol_iterator*>(SI);
  35. }
  36. inline LLVMSymbolIteratorRef
  37. wrap(const symbol_iterator *SI) {
  38. return reinterpret_cast<LLVMSymbolIteratorRef>
  39. (const_cast<symbol_iterator*>(SI));
  40. }
  41. inline relocation_iterator *unwrap(LLVMRelocationIteratorRef SI) {
  42. return reinterpret_cast<relocation_iterator*>(SI);
  43. }
  44. inline LLVMRelocationIteratorRef
  45. wrap(const relocation_iterator *SI) {
  46. return reinterpret_cast<LLVMRelocationIteratorRef>
  47. (const_cast<relocation_iterator*>(SI));
  48. }
  49. // ObjectFile creation
  50. LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
  51. ErrorOr<ObjectFile*> ObjOrErr(ObjectFile::createObjectFile(unwrap(MemBuf)));
  52. ObjectFile *Obj = ObjOrErr ? ObjOrErr.get() : nullptr;
  53. return wrap(Obj);
  54. }
  55. void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
  56. delete unwrap(ObjectFile);
  57. }
  58. // ObjectFile Section iterators
  59. LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
  60. section_iterator SI = unwrap(ObjectFile)->section_begin();
  61. return wrap(new section_iterator(SI));
  62. }
  63. void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
  64. delete unwrap(SI);
  65. }
  66. LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
  67. LLVMSectionIteratorRef SI) {
  68. return (*unwrap(SI) == unwrap(ObjectFile)->section_end()) ? 1 : 0;
  69. }
  70. void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
  71. ++(*unwrap(SI));
  72. }
  73. void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
  74. LLVMSymbolIteratorRef Sym) {
  75. if (error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
  76. report_fatal_error(ec.message());
  77. }
  78. // ObjectFile Symbol iterators
  79. LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) {
  80. symbol_iterator SI = unwrap(ObjectFile)->symbol_begin();
  81. return wrap(new symbol_iterator(SI));
  82. }
  83. void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
  84. delete unwrap(SI);
  85. }
  86. LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
  87. LLVMSymbolIteratorRef SI) {
  88. return (*unwrap(SI) == unwrap(ObjectFile)->symbol_end()) ? 1 : 0;
  89. }
  90. void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
  91. ++(*unwrap(SI));
  92. }
  93. // SectionRef accessors
  94. const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
  95. StringRef ret;
  96. if (error_code ec = (*unwrap(SI))->getName(ret))
  97. report_fatal_error(ec.message());
  98. return ret.data();
  99. }
  100. uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
  101. uint64_t ret;
  102. if (error_code ec = (*unwrap(SI))->getSize(ret))
  103. report_fatal_error(ec.message());
  104. return ret;
  105. }
  106. const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
  107. StringRef ret;
  108. if (error_code ec = (*unwrap(SI))->getContents(ret))
  109. report_fatal_error(ec.message());
  110. return ret.data();
  111. }
  112. uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
  113. uint64_t ret;
  114. if (error_code ec = (*unwrap(SI))->getAddress(ret))
  115. report_fatal_error(ec.message());
  116. return ret;
  117. }
  118. LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
  119. LLVMSymbolIteratorRef Sym) {
  120. bool ret;
  121. if (error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
  122. report_fatal_error(ec.message());
  123. return ret;
  124. }
  125. // Section Relocation iterators
  126. LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
  127. relocation_iterator SI = (*unwrap(Section))->relocation_begin();
  128. return wrap(new relocation_iterator(SI));
  129. }
  130. void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
  131. delete unwrap(SI);
  132. }
  133. LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
  134. LLVMRelocationIteratorRef SI) {
  135. return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
  136. }
  137. void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
  138. ++(*unwrap(SI));
  139. }
  140. // SymbolRef accessors
  141. const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
  142. StringRef ret;
  143. if (error_code ec = (*unwrap(SI))->getName(ret))
  144. report_fatal_error(ec.message());
  145. return ret.data();
  146. }
  147. uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
  148. uint64_t ret;
  149. if (error_code ec = (*unwrap(SI))->getAddress(ret))
  150. report_fatal_error(ec.message());
  151. return ret;
  152. }
  153. uint64_t LLVMGetSymbolFileOffset(LLVMSymbolIteratorRef SI) {
  154. uint64_t ret;
  155. if (error_code ec = (*unwrap(SI))->getFileOffset(ret))
  156. report_fatal_error(ec.message());
  157. return ret;
  158. }
  159. uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
  160. uint64_t ret;
  161. if (error_code ec = (*unwrap(SI))->getSize(ret))
  162. report_fatal_error(ec.message());
  163. return ret;
  164. }
  165. // RelocationRef accessors
  166. uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
  167. uint64_t ret;
  168. if (error_code ec = (*unwrap(RI))->getAddress(ret))
  169. report_fatal_error(ec.message());
  170. return ret;
  171. }
  172. uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
  173. uint64_t ret;
  174. if (error_code ec = (*unwrap(RI))->getOffset(ret))
  175. report_fatal_error(ec.message());
  176. return ret;
  177. }
  178. LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
  179. symbol_iterator ret = (*unwrap(RI))->getSymbol();
  180. return wrap(new symbol_iterator(ret));
  181. }
  182. uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
  183. uint64_t ret;
  184. if (error_code ec = (*unwrap(RI))->getType(ret))
  185. report_fatal_error(ec.message());
  186. return ret;
  187. }
  188. // NOTE: Caller takes ownership of returned string.
  189. const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
  190. SmallVector<char, 0> ret;
  191. if (error_code ec = (*unwrap(RI))->getTypeName(ret))
  192. report_fatal_error(ec.message());
  193. char *str = static_cast<char*>(malloc(ret.size()));
  194. std::copy(ret.begin(), ret.end(), str);
  195. return str;
  196. }
  197. // NOTE: Caller takes ownership of returned string.
  198. const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
  199. SmallVector<char, 0> ret;
  200. if (error_code ec = (*unwrap(RI))->getValueString(ret))
  201. report_fatal_error(ec.message());
  202. char *str = static_cast<char*>(malloc(ret.size()));
  203. std::copy(ret.begin(), ret.end(), str);
  204. return str;
  205. }