Object.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. /*===-- llvm-c/Object.h - Object Lib C Iface --------------------*- C++ -*-===*/
  7. /* */
  8. /* Part of the LLVM Project, under the Apache License v2.0 with LLVM */
  9. /* Exceptions. */
  10. /* See https://llvm.org/LICENSE.txt for license information. */
  11. /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
  12. /* */
  13. /*===----------------------------------------------------------------------===*/
  14. /* */
  15. /* This header declares the C interface to libLLVMObject.a, which */
  16. /* implements object file reading and writing. */
  17. /* */
  18. /* Many exotic languages can interoperate with C code but have a harder time */
  19. /* with C++ due to name mangling. So in addition to C, this interface enables */
  20. /* tools written in such languages. */
  21. /* */
  22. /*===----------------------------------------------------------------------===*/
  23. #ifndef LLVM_C_OBJECT_H
  24. #define LLVM_C_OBJECT_H
  25. #include "llvm-c/ExternC.h"
  26. #include "llvm-c/Types.h"
  27. #include "llvm/Config/llvm-config.h"
  28. LLVM_C_EXTERN_C_BEGIN
  29. /**
  30. * @defgroup LLVMCObject Object file reading and writing
  31. * @ingroup LLVMC
  32. *
  33. * @{
  34. */
  35. // Opaque type wrappers
  36. typedef struct LLVMOpaqueSectionIterator *LLVMSectionIteratorRef;
  37. typedef struct LLVMOpaqueSymbolIterator *LLVMSymbolIteratorRef;
  38. typedef struct LLVMOpaqueRelocationIterator *LLVMRelocationIteratorRef;
  39. typedef enum {
  40. LLVMBinaryTypeArchive, /**< Archive file. */
  41. LLVMBinaryTypeMachOUniversalBinary, /**< Mach-O Universal Binary file. */
  42. LLVMBinaryTypeCOFFImportFile, /**< COFF Import file. */
  43. LLVMBinaryTypeIR, /**< LLVM IR. */
  44. LLVMBinaryTypeWinRes, /**< Windows resource (.res) file. */
  45. LLVMBinaryTypeCOFF, /**< COFF Object file. */
  46. LLVMBinaryTypeELF32L, /**< ELF 32-bit, little endian. */
  47. LLVMBinaryTypeELF32B, /**< ELF 32-bit, big endian. */
  48. LLVMBinaryTypeELF64L, /**< ELF 64-bit, little endian. */
  49. LLVMBinaryTypeELF64B, /**< ELF 64-bit, big endian. */
  50. LLVMBinaryTypeMachO32L, /**< MachO 32-bit, little endian. */
  51. LLVMBinaryTypeMachO32B, /**< MachO 32-bit, big endian. */
  52. LLVMBinaryTypeMachO64L, /**< MachO 64-bit, little endian. */
  53. LLVMBinaryTypeMachO64B, /**< MachO 64-bit, big endian. */
  54. LLVMBinaryTypeWasm, /**< Web Assembly. */
  55. LLVMBinaryTypeOffload, /**< Offloading fatbinary. */
  56. } LLVMBinaryType;
  57. /**
  58. * Create a binary file from the given memory buffer.
  59. *
  60. * The exact type of the binary file will be inferred automatically, and the
  61. * appropriate implementation selected. The context may be NULL except if
  62. * the resulting file is an LLVM IR file.
  63. *
  64. * The memory buffer is not consumed by this function. It is the responsibilty
  65. * of the caller to free it with \c LLVMDisposeMemoryBuffer.
  66. *
  67. * If NULL is returned, the \p ErrorMessage parameter is populated with the
  68. * error's description. It is then the caller's responsibility to free this
  69. * message by calling \c LLVMDisposeMessage.
  70. *
  71. * @see llvm::object::createBinary
  72. */
  73. LLVMBinaryRef LLVMCreateBinary(LLVMMemoryBufferRef MemBuf,
  74. LLVMContextRef Context,
  75. char **ErrorMessage);
  76. /**
  77. * Dispose of a binary file.
  78. *
  79. * The binary file does not own its backing buffer. It is the responsibilty
  80. * of the caller to free it with \c LLVMDisposeMemoryBuffer.
  81. */
  82. void LLVMDisposeBinary(LLVMBinaryRef BR);
  83. /**
  84. * Retrieves a copy of the memory buffer associated with this object file.
  85. *
  86. * The returned buffer is merely a shallow copy and does not own the actual
  87. * backing buffer of the binary. Nevertheless, it is the responsibility of the
  88. * caller to free it with \c LLVMDisposeMemoryBuffer.
  89. *
  90. * @see llvm::object::getMemoryBufferRef
  91. */
  92. LLVMMemoryBufferRef LLVMBinaryCopyMemoryBuffer(LLVMBinaryRef BR);
  93. /**
  94. * Retrieve the specific type of a binary.
  95. *
  96. * @see llvm::object::Binary::getType
  97. */
  98. LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR);
  99. /*
  100. * For a Mach-O universal binary file, retrieves the object file corresponding
  101. * to the given architecture if it is present as a slice.
  102. *
  103. * If NULL is returned, the \p ErrorMessage parameter is populated with the
  104. * error's description. It is then the caller's responsibility to free this
  105. * message by calling \c LLVMDisposeMessage.
  106. *
  107. * It is the responsiblity of the caller to free the returned object file by
  108. * calling \c LLVMDisposeBinary.
  109. */
  110. LLVMBinaryRef LLVMMachOUniversalBinaryCopyObjectForArch(LLVMBinaryRef BR,
  111. const char *Arch,
  112. size_t ArchLen,
  113. char **ErrorMessage);
  114. /**
  115. * Retrieve a copy of the section iterator for this object file.
  116. *
  117. * If there are no sections, the result is NULL.
  118. *
  119. * The returned iterator is merely a shallow copy. Nevertheless, it is
  120. * the responsibility of the caller to free it with
  121. * \c LLVMDisposeSectionIterator.
  122. *
  123. * @see llvm::object::sections()
  124. */
  125. LLVMSectionIteratorRef LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR);
  126. /**
  127. * Returns whether the given section iterator is at the end.
  128. *
  129. * @see llvm::object::section_end
  130. */
  131. LLVMBool LLVMObjectFileIsSectionIteratorAtEnd(LLVMBinaryRef BR,
  132. LLVMSectionIteratorRef SI);
  133. /**
  134. * Retrieve a copy of the symbol iterator for this object file.
  135. *
  136. * If there are no symbols, the result is NULL.
  137. *
  138. * The returned iterator is merely a shallow copy. Nevertheless, it is
  139. * the responsibility of the caller to free it with
  140. * \c LLVMDisposeSymbolIterator.
  141. *
  142. * @see llvm::object::symbols()
  143. */
  144. LLVMSymbolIteratorRef LLVMObjectFileCopySymbolIterator(LLVMBinaryRef BR);
  145. /**
  146. * Returns whether the given symbol iterator is at the end.
  147. *
  148. * @see llvm::object::symbol_end
  149. */
  150. LLVMBool LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,
  151. LLVMSymbolIteratorRef SI);
  152. void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI);
  153. void LLVMMoveToNextSection(LLVMSectionIteratorRef SI);
  154. void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
  155. LLVMSymbolIteratorRef Sym);
  156. // ObjectFile Symbol iterators
  157. void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI);
  158. void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI);
  159. // SectionRef accessors
  160. const char *LLVMGetSectionName(LLVMSectionIteratorRef SI);
  161. uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI);
  162. const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI);
  163. uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI);
  164. LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
  165. LLVMSymbolIteratorRef Sym);
  166. // Section Relocation iterators
  167. LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section);
  168. void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef RI);
  169. LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
  170. LLVMRelocationIteratorRef RI);
  171. void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef RI);
  172. // SymbolRef accessors
  173. const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI);
  174. uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI);
  175. uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI);
  176. // RelocationRef accessors
  177. uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI);
  178. LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI);
  179. uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI);
  180. // NOTE: Caller takes ownership of returned string of the two
  181. // following functions.
  182. const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI);
  183. const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI);
  184. /** Deprecated: Use LLVMBinaryRef instead. */
  185. typedef struct LLVMOpaqueObjectFile *LLVMObjectFileRef;
  186. /** Deprecated: Use LLVMCreateBinary instead. */
  187. LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf);
  188. /** Deprecated: Use LLVMDisposeBinary instead. */
  189. void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile);
  190. /** Deprecated: Use LLVMObjectFileCopySectionIterator instead. */
  191. LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile);
  192. /** Deprecated: Use LLVMObjectFileIsSectionIteratorAtEnd instead. */
  193. LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
  194. LLVMSectionIteratorRef SI);
  195. /** Deprecated: Use LLVMObjectFileCopySymbolIterator instead. */
  196. LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile);
  197. /** Deprecated: Use LLVMObjectFileIsSymbolIteratorAtEnd instead. */
  198. LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
  199. LLVMSymbolIteratorRef SI);
  200. /**
  201. * @}
  202. */
  203. LLVM_C_EXTERN_C_END
  204. #endif
  205. #ifdef __GNUC__
  206. #pragma GCC diagnostic pop
  207. #endif