libelf.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * libelf.h - public header file for libelf.
  3. * Copyright (C) 1995 - 2008 Michael Riepe
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. */
  19. /* @(#) $Id: libelf.h,v 1.29 2009/07/07 17:57:43 michael Exp $ */
  20. #ifndef _LIBELF_H
  21. #define _LIBELF_H
  22. #include <stddef.h> /* for size_t */
  23. #include <sys/types.h>
  24. #if __LIBELF_INTERNAL__
  25. #include <sys_elf.h>
  26. #else /* __LIBELF_INTERNAL__ */
  27. #include <libelf/sys_elf.h>
  28. #endif /* __LIBELF_INTERNAL__ */
  29. #if defined __GNUC__ && !defined __cplusplus
  30. #define DEPRECATED __attribute__((deprecated))
  31. #else
  32. #define DEPRECATED /* nothing */
  33. #endif
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif /* __cplusplus */
  37. #ifndef __P
  38. # if (__STDC__ + 0) || defined(__cplusplus) || defined(_WIN32)
  39. # define __P(args) args
  40. # else /* __STDC__ || defined(__cplusplus) */
  41. # define __P(args) ()
  42. # endif /* __STDC__ || defined(__cplusplus) */
  43. #endif /* __P */
  44. /*
  45. * Commands
  46. */
  47. typedef enum {
  48. ELF_C_NULL = 0, /* must be first, 0 */
  49. ELF_C_READ,
  50. ELF_C_WRITE,
  51. ELF_C_CLR,
  52. ELF_C_SET,
  53. ELF_C_FDDONE,
  54. ELF_C_FDREAD,
  55. ELF_C_RDWR,
  56. ELF_C_NUM /* must be last */
  57. } Elf_Cmd;
  58. /*
  59. * Flags
  60. */
  61. #define ELF_F_DIRTY 0x1
  62. #define ELF_F_LAYOUT 0x4
  63. /*
  64. * Allow sections to overlap when ELF_F_LAYOUT is in effect.
  65. * Note that this flag ist NOT portable, and that it may render
  66. * the output file unusable. Use with extreme caution!
  67. */
  68. #define ELF_F_LAYOUT_OVERLAP 0x10000000
  69. /*
  70. * File types
  71. */
  72. typedef enum {
  73. ELF_K_NONE = 0, /* must be first, 0 */
  74. ELF_K_AR,
  75. ELF_K_COFF,
  76. ELF_K_ELF,
  77. ELF_K_NUM /* must be last */
  78. } Elf_Kind;
  79. /*
  80. * Data types
  81. */
  82. typedef enum {
  83. ELF_T_BYTE = 0, /* must be first, 0 */
  84. ELF_T_ADDR,
  85. ELF_T_DYN,
  86. ELF_T_EHDR,
  87. ELF_T_HALF,
  88. ELF_T_OFF,
  89. ELF_T_PHDR,
  90. ELF_T_RELA,
  91. ELF_T_REL,
  92. ELF_T_SHDR,
  93. ELF_T_SWORD,
  94. ELF_T_SYM,
  95. ELF_T_WORD,
  96. /*
  97. * New stuff for 64-bit.
  98. *
  99. * Most implementations add ELF_T_SXWORD after ELF_T_SWORD
  100. * which breaks binary compatibility with earlier versions.
  101. * If this causes problems for you, contact me.
  102. */
  103. ELF_T_SXWORD,
  104. ELF_T_XWORD,
  105. /*
  106. * Symbol versioning. Sun broke binary compatibility (again!),
  107. * but I won't.
  108. */
  109. ELF_T_VDEF,
  110. ELF_T_VNEED,
  111. ELF_T_NUM /* must be last */
  112. } Elf_Type;
  113. /*
  114. * Elf descriptor
  115. */
  116. typedef struct Elf Elf;
  117. /*
  118. * Section descriptor
  119. */
  120. typedef struct Elf_Scn Elf_Scn;
  121. /*
  122. * Archive member header
  123. */
  124. typedef struct {
  125. char* ar_name;
  126. time_t ar_date;
  127. long ar_uid;
  128. long ar_gid;
  129. unsigned long ar_mode;
  130. off_t ar_size;
  131. char* ar_rawname;
  132. } Elf_Arhdr;
  133. /*
  134. * Archive symbol table
  135. */
  136. typedef struct {
  137. char* as_name;
  138. size_t as_off;
  139. unsigned long as_hash;
  140. } Elf_Arsym;
  141. /*
  142. * Data descriptor
  143. */
  144. typedef struct {
  145. void* d_buf;
  146. Elf_Type d_type;
  147. size_t d_size;
  148. off_t d_off;
  149. size_t d_align;
  150. unsigned d_version;
  151. } Elf_Data;
  152. /*
  153. * Function declarations
  154. */
  155. extern Elf *elf_begin __P((int __fd, Elf_Cmd __cmd, Elf *__ref));
  156. extern Elf *elf_memory __P((char *__image, size_t __size));
  157. extern int elf_cntl __P((Elf *__elf, Elf_Cmd __cmd));
  158. extern int elf_end __P((Elf *__elf));
  159. extern const char *elf_errmsg __P((int __err));
  160. extern int elf_errno __P((void));
  161. extern void elf_fill __P((int __fill));
  162. extern unsigned elf_flagdata __P((Elf_Data *__data, Elf_Cmd __cmd,
  163. unsigned __flags));
  164. extern unsigned elf_flagehdr __P((Elf *__elf, Elf_Cmd __cmd,
  165. unsigned __flags));
  166. extern unsigned elf_flagelf __P((Elf *__elf, Elf_Cmd __cmd,
  167. unsigned __flags));
  168. extern unsigned elf_flagphdr __P((Elf *__elf, Elf_Cmd __cmd,
  169. unsigned __flags));
  170. extern unsigned elf_flagscn __P((Elf_Scn *__scn, Elf_Cmd __cmd,
  171. unsigned __flags));
  172. extern unsigned elf_flagshdr __P((Elf_Scn *__scn, Elf_Cmd __cmd,
  173. unsigned __flags));
  174. extern size_t elf32_fsize __P((Elf_Type __type, size_t __count,
  175. unsigned __ver));
  176. extern Elf_Arhdr *elf_getarhdr __P((Elf *__elf));
  177. extern Elf_Arsym *elf_getarsym __P((Elf *__elf, size_t *__ptr));
  178. extern off_t elf_getbase __P((Elf *__elf));
  179. extern Elf_Data *elf_getdata __P((Elf_Scn *__scn, Elf_Data *__data));
  180. extern Elf32_Ehdr *elf32_getehdr __P((Elf *__elf));
  181. extern char *elf_getident __P((Elf *__elf, size_t *__ptr));
  182. extern Elf32_Phdr *elf32_getphdr __P((Elf *__elf));
  183. extern Elf_Scn *elf_getscn __P((Elf *__elf, size_t __index));
  184. extern Elf32_Shdr *elf32_getshdr __P((Elf_Scn *__scn));
  185. extern unsigned long elf_hash __P((const unsigned char *__name));
  186. extern Elf_Kind elf_kind __P((Elf *__elf));
  187. extern size_t elf_ndxscn __P((Elf_Scn *__scn));
  188. extern Elf_Data *elf_newdata __P((Elf_Scn *__scn));
  189. extern Elf32_Ehdr *elf32_newehdr __P((Elf *__elf));
  190. extern Elf32_Phdr *elf32_newphdr __P((Elf *__elf, size_t __count));
  191. extern Elf_Scn *elf_newscn __P((Elf *__elf));
  192. extern Elf_Cmd elf_next __P((Elf *__elf));
  193. extern Elf_Scn *elf_nextscn __P((Elf *__elf, Elf_Scn *__scn));
  194. extern size_t elf_rand __P((Elf *__elf, size_t __offset));
  195. extern Elf_Data *elf_rawdata __P((Elf_Scn *__scn, Elf_Data *__data));
  196. extern char *elf_rawfile __P((Elf *__elf, size_t *__ptr));
  197. extern char *elf_strptr __P((Elf *__elf, size_t __section, size_t __offset));
  198. extern off_t elf_update __P((Elf *__elf, Elf_Cmd __cmd));
  199. extern unsigned elf_version __P((unsigned __ver));
  200. extern Elf_Data *elf32_xlatetof __P((Elf_Data *__dst, const Elf_Data *__src,
  201. unsigned __encode));
  202. extern Elf_Data *elf32_xlatetom __P((Elf_Data *__dst, const Elf_Data *__src,
  203. unsigned __encode));
  204. /*
  205. * Additional functions found on Solaris
  206. */
  207. extern long elf32_checksum __P((Elf *__elf));
  208. #if __LIBELF64
  209. /*
  210. * 64-bit ELF functions
  211. * Not available on all platforms
  212. */
  213. extern Elf64_Ehdr *elf64_getehdr __P((Elf *__elf));
  214. extern Elf64_Ehdr *elf64_newehdr __P((Elf *__elf));
  215. extern Elf64_Phdr *elf64_getphdr __P((Elf *__elf));
  216. extern Elf64_Phdr *elf64_newphdr __P((Elf *__elf, size_t __count));
  217. extern Elf64_Shdr *elf64_getshdr __P((Elf_Scn *__scn));
  218. extern size_t elf64_fsize __P((Elf_Type __type, size_t __count,
  219. unsigned __ver));
  220. extern Elf_Data *elf64_xlatetof __P((Elf_Data *__dst, const Elf_Data *__src,
  221. unsigned __encode));
  222. extern Elf_Data *elf64_xlatetom __P((Elf_Data *__dst, const Elf_Data *__src,
  223. unsigned __encode));
  224. /*
  225. * Additional functions found on Solaris
  226. */
  227. extern long elf64_checksum __P((Elf *__elf));
  228. #endif /* __LIBELF64 */
  229. /*
  230. * ELF format extensions
  231. *
  232. * These functions return 0 on failure, 1 on success. Since other
  233. * implementations of libelf may behave differently (there was quite
  234. * some confusion about the correct values), they are now officially
  235. * deprecated and should be replaced with the three new functions below.
  236. */
  237. DEPRECATED extern int elf_getphnum __P((Elf *__elf, size_t *__resultp));
  238. DEPRECATED extern int elf_getshnum __P((Elf *__elf, size_t *__resultp));
  239. DEPRECATED extern int elf_getshstrndx __P((Elf *__elf, size_t *__resultp));
  240. /*
  241. * Replacement functions (return -1 on failure, 0 on success).
  242. */
  243. extern int elf_getphdrnum __P((Elf *__elf, size_t *__resultp));
  244. extern int elf_getshdrnum __P((Elf *__elf, size_t *__resultp));
  245. extern int elf_getshdrstrndx __P((Elf *__elf, size_t *__resultp));
  246. /*
  247. * Convenience functions
  248. *
  249. * elfx_update_shstrndx is elf_getshstrndx's counterpart.
  250. * It should be used to set the e_shstrndx member.
  251. * There is no update function for e_shnum or e_phnum
  252. * because libelf handles them internally.
  253. */
  254. extern int elfx_update_shstrndx __P((Elf *__elf, size_t __index));
  255. /*
  256. * Experimental extensions:
  257. *
  258. * elfx_movscn() moves section `__scn' directly after section `__after'.
  259. * elfx_remscn() removes section `__scn'. Both functions update
  260. * the section indices; elfx_remscn() also adjusts the ELF header's
  261. * e_shnum member. The application is responsible for updating other
  262. * data (in particular, e_shstrndx and the section headers' sh_link and
  263. * sh_info members).
  264. *
  265. * elfx_movscn() returns the new index of the moved section.
  266. * elfx_remscn() returns the original index of the removed section.
  267. * A return value of zero indicates an error.
  268. */
  269. extern size_t elfx_movscn __P((Elf *__elf, Elf_Scn *__scn, Elf_Scn *__after));
  270. extern size_t elfx_remscn __P((Elf *__elf, Elf_Scn *__scn));
  271. /*
  272. * elf_delscn() is obsolete. Please use elfx_remscn() instead.
  273. */
  274. extern size_t elf_delscn __P((Elf *__elf, Elf_Scn *__scn));
  275. #ifdef __cplusplus
  276. }
  277. #endif /* __cplusplus */
  278. #endif /* _LIBELF_H */