2 * Copyright (c) 2006,2008-2011 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $Id: _libelf.h 3632 2018-10-10 21:12:43Z jkoshy $
32 #include <sys/queue.h>
35 #include "_libelf_config.h"
40 * Library-private data structures.
43 #define LIBELF_MSG_SIZE 256
45 struct _libelf_globals {
47 unsigned int libelf_byteorder;
51 unsigned int libelf_version;
52 unsigned char libelf_msg[LIBELF_MSG_SIZE];
55 extern struct _libelf_globals _libelf;
57 #define LIBELF_PRIVATE(N) (_libelf.libelf_##N)
59 #define LIBELF_ELF_ERROR_MASK 0xFF
60 #define LIBELF_OS_ERROR_SHIFT 8
62 #define LIBELF_ERROR(E, O) (((E) & LIBELF_ELF_ERROR_MASK) | \
63 ((O) << LIBELF_OS_ERROR_SHIFT))
65 #define LIBELF_SET_ERROR(E, O) do { \
66 LIBELF_PRIVATE(error) = LIBELF_ERROR(ELF_E_##E, (O)); \
69 #define LIBELF_ADJUST_AR_SIZE(S) (((S) + 1U) & ~1U)
72 * Flags for library internal use. These use the upper 16 bits of the
75 #define LIBELF_F_API_MASK 0x00FFFFU /* Flags defined by the API. */
76 #define LIBELF_F_AR_HEADER 0x010000U /* translated header available */
77 #define LIBELF_F_AR_VARIANT_SVR4 0x020000U /* BSD style ar(1) archive */
78 #define LIBELF_F_DATA_MALLOCED 0x040000U /* whether data was malloc'ed */
79 #define LIBELF_F_RAWFILE_MALLOC 0x080000U /* whether e_rawfile was malloc'ed */
80 #define LIBELF_F_RAWFILE_MMAP 0x100000U /* whether e_rawfile was mmap'ed */
81 #define LIBELF_F_SHDRS_LOADED 0x200000U /* whether all shdrs were read in */
82 #define LIBELF_F_SPECIAL_FILE 0x400000U /* non-regular file */
84 RB_HEAD(scntree, _Elf_Scn);
85 RB_PROTOTYPE(scntree, _Elf_Scn, e_scn, elfscn_cmp);
88 int e_activations; /* activation count */
89 unsigned int e_byteorder; /* ELFDATA* */
90 int e_class; /* ELFCLASS* */
91 Elf_Cmd e_cmd; /* ELF_C_* used at creation time */
92 int e_fd; /* associated file descriptor */
93 unsigned int e_flags; /* ELF_F_* & LIBELF_F_* flags */
94 Elf_Kind e_kind; /* ELF_K_* */
95 Elf *e_parent; /* non-NULL for archive members */
96 unsigned char *e_rawfile; /* uninterpreted bytes */
97 size_t e_rawsize; /* size of uninterpreted bytes */
98 unsigned int e_version; /* file version */
101 * Header information for archive members. See the
102 * LIBELF_F_AR_HEADER flag.
105 Elf_Arhdr *e_arhdr; /* translated header */
106 unsigned char *e_rawhdr; /* untranslated header */
110 struct { /* ar(1) archives */
111 off_t e_next; /* set by elf_rand()/elf_next() */
113 unsigned char *e_rawstrtab; /* file name strings */
114 size_t e_rawstrtabsz;
115 unsigned char *e_rawsymtab; /* symbol table */
116 size_t e_rawsymtabsz;
120 struct { /* regular ELF files */
122 Elf32_Ehdr *e_ehdr32;
123 Elf64_Ehdr *e_ehdr64;
126 Elf32_Phdr *e_phdr32;
127 Elf64_Phdr *e_phdr64;
129 struct scntree e_scn; /* sections */
130 size_t e_nphdr; /* number of Phdr entries */
131 size_t e_nscn; /* number of sections */
132 size_t e_strndx; /* string table section index */
138 * The internal descriptor wrapping the "Elf_Data" type.
140 struct _Libelf_Data {
141 Elf_Data d_data; /* The exported descriptor. */
142 Elf_Scn *d_scn; /* The containing section */
143 unsigned int d_flags;
144 STAILQ_ENTRY(_Libelf_Data) d_next;
152 STAILQ_HEAD(, _Libelf_Data) s_data; /* translated data */
153 STAILQ_HEAD(, _Libelf_Data) s_rawdata; /* raw data */
154 RB_ENTRY(_Elf_Scn) s_tree;
155 struct _Elf *s_elf; /* parent ELF descriptor */
156 unsigned int s_flags; /* flags for the section as a whole */
157 size_t s_ndx; /* index# for this section */
158 uint64_t s_offset; /* managed by elf_update() */
159 uint64_t s_rawoff; /* original offset in the file */
160 uint64_t s_size; /* managed by elf_update() */
171 * The LIBELF_COPY macros are used to copy fields from a GElf_*
172 * structure to their 32-bit counterparts, while checking for out of
175 * - LIBELF_COPY_U32 :: copy an unsigned 32 bit field.
176 * - LIBELF_COPY_S32 :: copy a signed 32 bit field.
179 #define LIBELF_COPY_U32(DST, SRC, NAME) do { \
180 if ((SRC)->NAME > UINT32_MAX) { \
181 LIBELF_SET_ERROR(RANGE, 0); \
184 (DST)->NAME = (SRC)->NAME & 0xFFFFFFFFU; \
187 #define LIBELF_COPY_S32(DST, SRC, NAME) do { \
188 if ((SRC)->NAME > INT32_MAX || \
189 (SRC)->NAME < INT32_MIN) { \
190 LIBELF_SET_ERROR(RANGE, 0); \
193 (DST)->NAME = (int32_t) (SRC)->NAME; \
198 * Function Prototypes.
201 typedef int _libelf_translator_function(unsigned char *_dst, size_t dsz,
202 unsigned char *_src, size_t _cnt, int _byteswap);
207 struct _Libelf_Data *_libelf_allocate_data(Elf_Scn *_s);
208 Elf *_libelf_allocate_elf(void);
209 Elf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx);
210 Elf_Arhdr *_libelf_ar_gethdr(Elf *_e);
211 Elf *_libelf_ar_open(Elf *_e, int _reporterror);
212 Elf *_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);
213 Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst);
214 Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst);
215 long _libelf_checksum(Elf *_e, int _elfclass);
216 void *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);
217 int _libelf_elfmachine(Elf *_e);
218 unsigned int _libelf_falign(Elf_Type _t, int _elfclass);
219 size_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version,
221 _libelf_translator_function *_libelf_get_translator(Elf_Type _t,
222 int _direction, int _elfclass, int _elfmachine);
223 void *_libelf_getphdr(Elf *_e, int _elfclass);
224 void *_libelf_getshdr(Elf_Scn *_scn, int _elfclass);
225 void _libelf_init_elf(Elf *_e, Elf_Kind _kind);
226 int _libelf_is_mips64el(Elf *e);
227 int _libelf_load_section_headers(Elf *e, void *ehdr);
228 unsigned int _libelf_malign(Elf_Type _t, int _elfclass);
229 Elf *_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror);
230 size_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);
231 void *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);
232 Elf *_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror);
233 Elf64_Xword _libelf_mips64el_r_info_tof(Elf64_Xword r_info);
234 Elf64_Xword _libelf_mips64el_r_info_tom(Elf64_Xword r_info);
235 struct _Libelf_Data *_libelf_release_data(struct _Libelf_Data *_d);
236 Elf *_libelf_release_elf(Elf *_e);
237 Elf_Scn *_libelf_release_scn(Elf_Scn *_s);
238 int _libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum);
239 int _libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum);
240 int _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass,
242 Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s,
243 unsigned int _encoding, int _elfclass, int _elfmachine, int _direction);
244 int _libelf_xlate_shtype(uint32_t _sht);
249 #endif /* __LIBELF_H_ */