Object.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. } LLVMBinaryType;
  56. /**
  57. * Create a binary file from the given memory buffer.
  58. *
  59. * The exact type of the binary file will be inferred automatically, and the
  60. * appropriate implementation selected. The context may be NULL except if
  61. * the resulting file is an LLVM IR file.
  62. *
  63. * The memory buffer is not consumed by this function. It is the responsibilty
  64. * of the caller to free it with \c LLVMDisposeMemoryBuffer.
  65. *
  66. * If NULL is returned, the \p ErrorMessage parameter is populated with the
  67. * error's description. It is then the caller's responsibility to free this
  68. * message by calling \c LLVMDisposeMessage.
  69. *
  70. * @see llvm::object::createBinary
  71. */
  72. LLVMBinaryRef LLVMCreateBinary(LLVMMemoryBufferRef MemBuf,
  73. LLVMContextRef Context,
  74. char **ErrorMessage);
  75. /**
  76. * Dispose of a binary file.
  77. *
  78. * The binary file does not own its backing buffer. It is the responsibilty
  79. * of the caller to free it with \c LLVMDisposeMemoryBuffer.
  80. */
  81. void LLVMDisposeBinary(LLVMBinaryRef BR);
  82. /**
  83. * Retrieves a copy of the memory buffer associated with this object file.
  84. *
  85. * The returned buffer is merely a shallow copy and does not own the actual
  86. * backing buffer of the binary. Nevertheless, it is the responsibility of the
  87. * caller to free it with \c LLVMDisposeMemoryBuffer.
  88. *
  89. * @see llvm::object::getMemoryBufferRef
  90. */
  91. LLVMMemoryBufferRef LLVMBinaryCopyMemoryBuffer(LLVMBinaryRef BR);
  92. /**
  93. * Retrieve the specific type of a binary.
  94. *
  95. * @see llvm::object::Binary::getType
  96. */
  97. LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR);
  98. /*
  99. * For a Mach-O universal binary file, retrieves the object file corresponding
  100. * to the given architecture if it is present as a slice.
  101. *
  102. * If NULL is returned, the \p ErrorMessage parameter is populated with the
  103. * error's description. It is then the caller's responsibility to free this
  104. * message by calling \c LLVMDisposeMessage.
  105. *
  106. * It is the responsiblity of the caller to free the returned object file by
  107. * calling \c LLVMDisposeBinary.
  108. */
  109. LLVMBinaryRef LLVMMachOUniversalBinaryCopyObjectForArch(LLVMBinaryRef BR,
  110. const char *Arch,
  111. size_t ArchLen,
  112. char **ErrorMessage);
  113. /**
  114. * Retrieve a copy of the section iterator for this object file.
  115. *
  116. * If there are no sections, the result is NULL.
  117. *
  118. * The returned iterator is merely a shallow copy. Nevertheless, it is
  119. * the responsibility of the caller to free it with
  120. * \c LLVMDisposeSectionIterator.
  121. *
  122. * @see llvm::object::sections()
  123. */
  124. LLVMSectionIteratorRef LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR);
  125. /**
  126. * Returns whether the given section iterator is at the end.
  127. *
  128. * @see llvm::object::section_end
  129. */
  130. LLVMBool LLVMObjectFileIsSectionIteratorAtEnd(LLVMBinaryRef BR,
  131. LLVMSectionIteratorRef SI);
  132. /**
  133. * Retrieve a copy of the symbol iterator for this object file.
  134. *
  135. * If there are no symbols, the result is NULL.
  136. *
  137. * The returned iterator is merely a shallow copy. Nevertheless, it is
  138. * the responsibility of the caller to free it with
  139. * \c LLVMDisposeSymbolIterator.
  140. *
  141. * @see llvm::object::symbols()
  142. */
  143. LLVMSymbolIteratorRef LLVMObjectFileCopySymbolIterator(LLVMBinaryRef BR);
  144. /**
  145. * Returns whether the given symbol iterator is at the end.
  146. *
  147. * @see llvm::object::symbol_end
  148. */
  149. LLVMBool LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,
  150. LLVMSymbolIteratorRef SI);
  151. void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI);
  152. void LLVMMoveToNextSection(LLVMSectionIteratorRef SI);
  153. void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
  154. LLVMSymbolIteratorRef Sym);
  155. // ObjectFile Symbol iterators
  156. void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI);
  157. void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI);
  158. // SectionRef accessors
  159. const char *LLVMGetSectionName(LLVMSectionIteratorRef SI);
  160. uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI);
  161. const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI);
  162. uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI);
  163. LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
  164. LLVMSymbolIteratorRef Sym);
  165. // Section Relocation iterators
  166. LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section);
  167. void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef RI);
  168. LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
  169. LLVMRelocationIteratorRef RI);
  170. void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef RI);
  171. // SymbolRef accessors
  172. const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI);
  173. uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI);
  174. uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI);
  175. // RelocationRef accessors
  176. uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI);
  177. LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI);
  178. uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI);
  179. // NOTE: Caller takes ownership of returned string of the two
  180. // following functions.
  181. const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI);
  182. const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI);
  183. /** Deprecated: Use LLVMBinaryRef instead. */
  184. typedef struct LLVMOpaqueObjectFile *LLVMObjectFileRef;
  185. /** Deprecated: Use LLVMCreateBinary instead. */
  186. LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf);
  187. /** Deprecated: Use LLVMDisposeBinary instead. */
  188. void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile);
  189. /** Deprecated: Use LLVMObjectFileCopySectionIterator instead. */
  190. LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile);
  191. /** Deprecated: Use LLVMObjectFileIsSectionIteratorAtEnd instead. */
  192. LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
  193. LLVMSectionIteratorRef SI);
  194. /** Deprecated: Use LLVMObjectFileCopySymbolIterator instead. */
  195. LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile);
  196. /** Deprecated: Use LLVMObjectFileIsSymbolIteratorAtEnd instead. */
  197. LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
  198. LLVMSymbolIteratorRef SI);
  199. /**
  200. * @}
  201. */
  202. LLVM_C_EXTERN_C_END
  203. #endif
  204. #ifdef __GNUC__
  205. #pragma GCC diagnostic pop
  206. #endif