private.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * private.h - private definitions for libelf.
  3. * Copyright (C) 1995 - 2007 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: private.h,v 1.40 2009/11/01 13:04:19 michael Exp $ */
  20. #ifndef _PRIVATE_H
  21. #define _PRIVATE_H
  22. #define __LIBELF_INTERNAL__ 1
  23. #if HAVE_CONFIG_H
  24. # include <config.h>
  25. #endif /* HAVE_CONFIG_H */
  26. /*
  27. * Workaround for GLIBC bug:
  28. * include <stdint.h> before <sys/types.h>
  29. */
  30. #if HAVE_STDINT_H
  31. #include <stdint.h>
  32. #endif
  33. #include <sys/types.h>
  34. #if STDC_HEADERS
  35. # include <stdlib.h>
  36. # include <string.h>
  37. #else /* STDC_HEADERS */
  38. extern void *malloc(), *realloc();
  39. extern void free(), bcopy(), abort();
  40. extern int strcmp(), strncmp(), memcmp();
  41. extern void *memcpy(), *memmove(), *memset();
  42. #endif /* STDC_HEADERS */
  43. #if defined(_WIN32)
  44. #include <io.h>
  45. #else
  46. #if HAVE_UNISTD_H
  47. # include <unistd.h>
  48. #else /* HAVE_UNISTD_H */
  49. extern int read(), write(), close();
  50. extern off_t lseek();
  51. #if HAVE_FTRUNCATE
  52. extern int ftruncate();
  53. #endif /* HAVE_FTRUNCATE */
  54. #endif /* HAVE_UNISTD_H */
  55. #endif /* defined(_WIN32) */
  56. #ifndef SEEK_SET
  57. #define SEEK_SET 0
  58. #endif /* SEEK_SET */
  59. #ifndef SEEK_CUR
  60. #define SEEK_CUR 1
  61. #endif /* SEEK_CUR */
  62. #ifndef SEEK_END
  63. #define SEEK_END 2
  64. #endif /* SEEK_END */
  65. #if !HAVE_MEMCMP
  66. # define memcmp strncmp
  67. #endif /* !HAVE_MEMCMP */
  68. #if !HAVE_MEMCPY
  69. # define memcpy(d,s,n) bcopy(s,d,n)
  70. #endif /* !HAVE_MEMCPY */
  71. #if !HAVE_MEMMOVE
  72. # define memmove(d,s,n) bcopy(s,d,n)
  73. #endif /* !HAVE_MEMMOVE */
  74. #if !HAVE_MEMSET
  75. # define memset _elf_memset
  76. extern void *_elf_memset();
  77. #endif /* !HAVE_MEMSET */
  78. #if HAVE_STRUCT_NLIST_DECLARATION
  79. # define nlist __override_nlist_declaration
  80. #endif /* HAVE_STRUCT_NLIST_DECLARATION */
  81. #if __LIBELF_NEED_LINK_H
  82. # include <link.h>
  83. #elif __LIBELF_NEED_SYS_LINK_H
  84. # include <sys/link.h>
  85. #endif /* __LIBELF_NEED_LINK_H */
  86. #if HAVE_AR_H
  87. #include <ar.h>
  88. #else /* HAVE_AR_H */
  89. #define ARMAG "!<arch>\n"
  90. #define SARMAG 8
  91. struct ar_hdr {
  92. char ar_name[16];
  93. char ar_date[12];
  94. char ar_uid[6];
  95. char ar_gid[6];
  96. char ar_mode[8];
  97. char ar_size[10];
  98. char ar_fmag[2];
  99. };
  100. #define ARFMAG "`\n"
  101. #endif /* HAVE_AR_H */
  102. #include <libelf.h>
  103. #if HAVE_STRUCT_NLIST_DECLARATION
  104. # undef nlist
  105. #endif /* HAVE_STRUCT_NLIST_DECLARATION */
  106. #if __LIBELF64
  107. #include <gelf.h>
  108. #endif /* __LIBELF64 */
  109. typedef struct Scn_Data Scn_Data;
  110. /*
  111. * ELF descriptor
  112. */
  113. struct Elf {
  114. /* common */
  115. size_t e_size; /* file/member size */
  116. size_t e_dsize; /* size of memory image */
  117. Elf_Kind e_kind; /* kind of file */
  118. char* e_data; /* file/member data */
  119. char* e_rawdata; /* file/member raw data */
  120. size_t e_idlen; /* identifier size */
  121. int e_fd; /* file descriptor */
  122. unsigned e_count; /* activation count */
  123. /* archive members (still common) */
  124. Elf* e_parent; /* NULL if not an archive member */
  125. size_t e_next; /* 0 if not an archive member */
  126. size_t e_base; /* 0 if not an archive member */
  127. Elf* e_link; /* next archive member or NULL */
  128. Elf_Arhdr* e_arhdr; /* archive member header or NULL */
  129. /* archives */
  130. size_t e_off; /* current member offset (for elf_begin) */
  131. Elf* e_members; /* linked list of active archive members */
  132. char* e_symtab; /* archive symbol table */
  133. size_t e_symlen; /* length of archive symbol table */
  134. char* e_strtab; /* archive string table */
  135. size_t e_strlen; /* length of archive string table */
  136. /* ELF files */
  137. unsigned e_class; /* ELF class */
  138. unsigned e_encoding; /* ELF data encoding */
  139. unsigned e_version; /* ELF version */
  140. char* e_ehdr; /* ELF header */
  141. char* e_phdr; /* ELF program header table */
  142. size_t e_phnum; /* size of program header table */
  143. Elf_Scn* e_scn_1; /* first section */
  144. Elf_Scn* e_scn_n; /* last section */
  145. unsigned e_elf_flags; /* elf flags (ELF_F_*) */
  146. unsigned e_ehdr_flags; /* ehdr flags (ELF_F_*) */
  147. unsigned e_phdr_flags; /* phdr flags (ELF_F_*) */
  148. /* misc flags */
  149. unsigned e_readable : 1; /* file is readable */
  150. unsigned e_writable : 1; /* file is writable */
  151. unsigned e_disabled : 1; /* e_fd has been disabled */
  152. unsigned e_cooked : 1; /* e_data was modified */
  153. unsigned e_free_syms : 1; /* e_symtab is malloc'ed */
  154. unsigned e_unmap_data : 1; /* e_data is mmap'ed */
  155. unsigned e_memory : 1; /* created by elf_memory() */
  156. /* magic number for debugging */
  157. long e_magic;
  158. };
  159. #define ELF_MAGIC 0x012b649e
  160. #define INIT_ELF {\
  161. /* e_size */ 0,\
  162. /* e_dsize */ 0,\
  163. /* e_kind */ ELF_K_NONE,\
  164. /* e_data */ NULL,\
  165. /* e_rawdata */ NULL,\
  166. /* e_idlen */ 0,\
  167. /* e_fd */ -1,\
  168. /* e_count */ 1,\
  169. /* e_parent */ NULL,\
  170. /* e_next */ 0,\
  171. /* e_base */ 0,\
  172. /* e_link */ NULL,\
  173. /* e_arhdr */ NULL,\
  174. /* e_off */ 0,\
  175. /* e_members */ NULL,\
  176. /* e_symtab */ NULL,\
  177. /* e_symlen */ 0,\
  178. /* e_strtab */ NULL,\
  179. /* e_strlen */ 0,\
  180. /* e_class */ ELFCLASSNONE,\
  181. /* e_encoding */ ELFDATANONE,\
  182. /* e_version */ EV_NONE,\
  183. /* e_ehdr */ NULL,\
  184. /* e_phdr */ NULL,\
  185. /* e_phnum */ 0,\
  186. /* e_scn_1 */ NULL,\
  187. /* e_scn_n */ NULL,\
  188. /* e_elf_flags */ 0,\
  189. /* e_ehdr_flags */ 0,\
  190. /* e_phdr_flags */ 0,\
  191. /* e_readable */ 0,\
  192. /* e_writable */ 0,\
  193. /* e_disabled */ 0,\
  194. /* e_cooked */ 0,\
  195. /* e_free_syms */ 0,\
  196. /* e_unmap_data */ 0,\
  197. /* e_memory */ 0,\
  198. /* e_magic */ ELF_MAGIC\
  199. }
  200. /*
  201. * Section descriptor
  202. */
  203. struct Elf_Scn {
  204. Elf_Scn* s_link; /* pointer to next Elf_Scn */
  205. Elf* s_elf; /* pointer to elf descriptor */
  206. size_t s_index; /* number of this section */
  207. unsigned s_scn_flags; /* section flags (ELF_F_*) */
  208. unsigned s_shdr_flags; /* shdr flags (ELF_F_*) */
  209. Scn_Data* s_data_1; /* first data buffer */
  210. Scn_Data* s_data_n; /* last data buffer */
  211. Scn_Data* s_rawdata; /* raw data buffer */
  212. /* data copied from shdr */
  213. unsigned s_type; /* section type */
  214. size_t s_offset; /* section offset */
  215. size_t s_size; /* section size */
  216. /* misc flags */
  217. unsigned s_freeme : 1; /* this Elf_Scn was malloc'ed */
  218. /* section header */
  219. union {
  220. #if __LIBELF64
  221. Elf64_Shdr u_shdr64;
  222. #endif /* __LIBELF64 */
  223. Elf32_Shdr u_shdr32;
  224. } s_uhdr;
  225. /* magic number for debugging */
  226. long s_magic;
  227. };
  228. #define s_shdr32 s_uhdr.u_shdr32
  229. #define s_shdr64 s_uhdr.u_shdr64
  230. #define SCN_MAGIC 0x012c747d
  231. #define INIT_SCN {\
  232. /* s_link */ NULL,\
  233. /* s_elf */ NULL,\
  234. /* s_index */ 0,\
  235. /* s_scn_flags */ 0,\
  236. /* s_shdr_flags */ 0,\
  237. /* s_data_1 */ NULL,\
  238. /* s_data_n */ NULL,\
  239. /* s_rawdata */ NULL,\
  240. /* s_type */ SHT_NULL,\
  241. /* s_offset */ 0,\
  242. /* s_size */ 0,\
  243. /* s_freeme */ 0,\
  244. /* s_uhdr */ {{0,}},\
  245. /* s_magic */ SCN_MAGIC\
  246. }
  247. /*
  248. * Data descriptor
  249. */
  250. struct Scn_Data {
  251. Elf_Data sd_data; /* must be first! */
  252. Scn_Data* sd_link; /* pointer to next Scn_Data */
  253. Elf_Scn* sd_scn; /* pointer to section */
  254. char* sd_memdata; /* memory image of section */
  255. unsigned sd_data_flags; /* data flags (ELF_F_*) */
  256. /* misc flags */
  257. unsigned sd_freeme : 1; /* this Scn_Data was malloc'ed */
  258. unsigned sd_free_data : 1; /* sd_memdata is malloc'ed */
  259. /* magic number for debugging */
  260. long sd_magic;
  261. };
  262. #define DATA_MAGIC 0x01072639
  263. #define INIT_DATA {\
  264. {\
  265. /* d_buf */ NULL,\
  266. /* d_type */ ELF_T_BYTE,\
  267. /* d_size */ 0,\
  268. /* d_off */ 0,\
  269. /* d_align */ 0,\
  270. /* d_version */ EV_NONE\
  271. },\
  272. /* sd_link */ NULL,\
  273. /* sd_scn */ NULL,\
  274. /* sd_memdata */ NULL,\
  275. /* sd_data_flags */ 0,\
  276. /* sd_freeme */ 0,\
  277. /* sd_free_data */ 0,\
  278. /* sd_magic */ DATA_MAGIC\
  279. }
  280. /*
  281. * Private status variables
  282. */
  283. extern unsigned _elf_version;
  284. extern int _elf_errno;
  285. extern int _elf_fill;
  286. extern int _elf_sanity_checks;
  287. #define SANITY_CHECK_STRPTR (1u << 0)
  288. /*
  289. * Private functions
  290. */
  291. extern void *_elf_read __P((Elf*, void*, size_t, size_t));
  292. extern void *_elf_mmap __P((Elf*));
  293. extern int _elf_cook __P((Elf*));
  294. extern char *_elf_getehdr __P((Elf*, unsigned));
  295. extern char *_elf_getphdr __P((Elf*, unsigned));
  296. extern Elf_Data *_elf_xlatetom __P((const Elf*, Elf_Data*, const Elf_Data*));
  297. extern Elf_Type _elf_scn_type __P((unsigned));
  298. extern size_t _elf32_xltsize __P((const Elf_Data *__src, unsigned __dv, unsigned __encode, int __tof));
  299. extern size_t _elf64_xltsize __P((const Elf_Data *__src, unsigned __dv, unsigned __encode, int __tof));
  300. extern int _elf_update_shnum(Elf *__elf, size_t __shnum);
  301. extern Elf_Scn *_elf_first_scn(Elf *__elf);
  302. /*
  303. * Special translators
  304. */
  305. extern size_t _elf_verdef_32L11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
  306. extern size_t _elf_verdef_32L11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
  307. extern size_t _elf_verdef_32M11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
  308. extern size_t _elf_verdef_32M11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
  309. extern size_t _elf_verdef_64L11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
  310. extern size_t _elf_verdef_64L11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
  311. extern size_t _elf_verdef_64M11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
  312. extern size_t _elf_verdef_64M11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
  313. extern size_t _elf_verneed_32L11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
  314. extern size_t _elf_verneed_32L11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
  315. extern size_t _elf_verneed_32M11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
  316. extern size_t _elf_verneed_32M11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
  317. extern size_t _elf_verneed_64L11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
  318. extern size_t _elf_verneed_64L11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
  319. extern size_t _elf_verneed_64M11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
  320. extern size_t _elf_verneed_64M11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
  321. /*
  322. * Private data
  323. */
  324. extern const Elf_Scn _elf_scn_init;
  325. extern const Scn_Data _elf_data_init;
  326. extern const size_t _elf_fmsize[2][EV_CURRENT - EV_NONE][ELF_T_NUM][2];
  327. /*
  328. * Access macros for _elf_fmsize[]
  329. */
  330. #define _fmsize(c,v,t,w) \
  331. (_elf_fmsize[(c)-ELFCLASS32][(v)-EV_NONE-1][(t)-ELF_T_BYTE][(w)])
  332. #define _fsize(c,v,t) _fmsize((c),(v),(t),1)
  333. #define _msize(c,v,t) _fmsize((c),(v),(t),0)
  334. /*
  335. * Various checks
  336. */
  337. #define valid_class(c) ((c) >= ELFCLASS32 && (c) <= ELFCLASS64)
  338. #define valid_encoding(e) ((e) >= ELFDATA2LSB && (e) <= ELFDATA2MSB)
  339. #define valid_version(v) ((v) > EV_NONE && (v) <= EV_CURRENT)
  340. #define valid_type(t) ((unsigned)(t) < ELF_T_NUM)
  341. /*
  342. * Error codes
  343. */
  344. enum {
  345. #define __err__(a,b) a,
  346. #include <errors.h> /* include constants from errors.h */
  347. #undef __err__
  348. ERROR_NUM
  349. };
  350. #define seterr(err) (_elf_errno = (err))
  351. /*
  352. * Sizes of data types (external representation)
  353. * These definitions should be in <elf.h>, but...
  354. */
  355. #ifndef ELF32_FSZ_ADDR
  356. # define ELF32_FSZ_ADDR 4
  357. # define ELF32_FSZ_HALF 2
  358. # define ELF32_FSZ_OFF 4
  359. # define ELF32_FSZ_SWORD 4
  360. # define ELF32_FSZ_WORD 4
  361. #endif /* ELF32_FSZ_ADDR */
  362. #ifndef ELF64_FSZ_ADDR
  363. # define ELF64_FSZ_ADDR 8
  364. # define ELF64_FSZ_HALF 2
  365. # define ELF64_FSZ_OFF 8
  366. # define ELF64_FSZ_SWORD 4
  367. # define ELF64_FSZ_SXWORD 8
  368. # define ELF64_FSZ_WORD 4
  369. # define ELF64_FSZ_XWORD 8
  370. #endif /* ELF64_FSZ_ADDR */
  371. /*
  372. * More missing pieces, in no particular order
  373. */
  374. #ifndef SHT_SYMTAB_SHNDX
  375. #define SHT_SYMTAB_SHNDX 18
  376. #endif /* SHT_SYMTAB_SHNDX */
  377. #ifndef SHN_XINDEX
  378. #define SHN_XINDEX 0xffff
  379. #endif /* SHN_XINDEX */
  380. #ifndef PN_XNUM
  381. #define PN_XNUM 0xffff
  382. #endif /* PN_XNUM */
  383. /*
  384. * Debugging
  385. */
  386. #if ENABLE_DEBUG
  387. extern void __elf_assert __P((const char*, unsigned, const char*));
  388. # if (__STDC__ + 0)
  389. # define elf_assert(x) do{if(!(x))__elf_assert(__FILE__,__LINE__,#x);}while(0)
  390. # else /* __STDC__ */
  391. # define elf_assert(x) do{if(!(x))__elf_assert(__FILE__,__LINE__,"x");}while(0)
  392. # endif /* __STDC__ */
  393. #else /* ENABLE_DEBUG */
  394. # define elf_assert(x) do{}while(0)
  395. #endif /* ENABLE_DEBUG */
  396. /*
  397. * Return values for certain functions
  398. */
  399. #define LIBELF_SUCCESS 1
  400. #define LIBELF_FAILURE 0
  401. #endif /* _PRIVATE_H */