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 3174 2015-03-27 17:13:41Z emaste $
32 #include <sys/queue.h>
34 #include "_libelf_config.h"
39 * Library-private data structures.
42 #define LIBELF_MSG_SIZE 256
44 struct _libelf_globals {
46 unsigned int libelf_byteorder;
50 unsigned int libelf_version;
51 unsigned char libelf_msg[LIBELF_MSG_SIZE];
54 extern struct _libelf_globals _libelf;
56 #define LIBELF_PRIVATE(N) (_libelf.libelf_##N)
58 #define LIBELF_ELF_ERROR_MASK 0xFF
59 #define LIBELF_OS_ERROR_SHIFT 8
61 #define LIBELF_ERROR(E, O) (((E) & LIBELF_ELF_ERROR_MASK) | \
62 ((O) << LIBELF_OS_ERROR_SHIFT))
64 #define LIBELF_SET_ERROR(E, O) do { \
65 LIBELF_PRIVATE(error) = LIBELF_ERROR(ELF_E_##E, (O)); \
68 #define LIBELF_ADJUST_AR_SIZE(S) (((S) + 1U) & ~1U)
71 * Flags for library internal use. These use the upper 16 bits of the
74 #define LIBELF_F_API_MASK 0x00FFFFU /* Flags defined by the API. */
75 #define LIBELF_F_AR_HEADER 0x010000U /* translated header available */
76 #define LIBELF_F_AR_VARIANT_SVR4 0x020000U /* BSD style ar(1) archive */
77 #define LIBELF_F_DATA_MALLOCED 0x040000U /* whether data was malloc'ed */
78 #define LIBELF_F_RAWFILE_MALLOC 0x080000U /* whether e_rawfile was malloc'ed */
79 #define LIBELF_F_RAWFILE_MMAP 0x100000U /* whether e_rawfile was mmap'ed */
80 #define LIBELF_F_SHDRS_LOADED 0x200000U /* whether all shdrs were read in */
81 #define LIBELF_F_SPECIAL_FILE 0x400000U /* non-regular file */
84 int e_activations; /* activation count */
85 unsigned int e_byteorder; /* ELFDATA* */
86 int e_class; /* ELFCLASS* */
87 Elf_Cmd e_cmd; /* ELF_C_* used at creation time */
88 int e_fd; /* associated file descriptor */
89 unsigned int e_flags; /* ELF_F_* & LIBELF_F_* flags */
90 Elf_Kind e_kind; /* ELF_K_* */
91 Elf *e_parent; /* non-NULL for archive members */
92 unsigned char *e_rawfile; /* uninterpreted bytes */
93 size_t e_rawsize; /* size of uninterpreted bytes */
94 unsigned int e_version; /* file version */
97 * Header information for archive members. See the
98 * LIBELF_F_AR_HEADER flag.
101 Elf_Arhdr *e_arhdr; /* translated header */
102 unsigned char *e_rawhdr; /* untranslated header */
106 struct { /* ar(1) archives */
107 off_t e_next; /* set by elf_rand()/elf_next() */
109 unsigned char *e_rawstrtab; /* file name strings */
110 size_t e_rawstrtabsz;
111 unsigned char *e_rawsymtab; /* symbol table */
112 size_t e_rawsymtabsz;
116 struct { /* regular ELF files */
118 Elf32_Ehdr *e_ehdr32;
119 Elf64_Ehdr *e_ehdr64;
122 Elf32_Phdr *e_phdr32;
123 Elf64_Phdr *e_phdr64;
125 STAILQ_HEAD(, _Elf_Scn) e_scn; /* section list */
126 size_t e_nphdr; /* number of Phdr entries */
127 size_t e_nscn; /* number of sections */
128 size_t e_strndx; /* string table section index */
134 * The internal descriptor wrapping the "Elf_Data" type.
136 struct _Libelf_Data {
137 Elf_Data d_data; /* The exported descriptor. */
138 Elf_Scn *d_scn; /* The containing section */
139 unsigned int d_flags;
140 STAILQ_ENTRY(_Libelf_Data) d_next;
148 STAILQ_HEAD(, _Libelf_Data) s_data; /* translated data */
149 STAILQ_HEAD(, _Libelf_Data) s_rawdata; /* raw data */
150 STAILQ_ENTRY(_Elf_Scn) s_next;
151 struct _Elf *s_elf; /* parent ELF descriptor */
152 unsigned int s_flags; /* flags for the section as a whole */
153 size_t s_ndx; /* index# for this section */
154 uint64_t s_offset; /* managed by elf_update() */
155 uint64_t s_rawoff; /* original offset in the file */
156 uint64_t s_size; /* managed by elf_update() */
167 * The LIBELF_COPY macros are used to copy fields from a GElf_*
168 * structure to their 32-bit counterparts, while checking for out of
171 * - LIBELF_COPY_U32 :: copy an unsigned 32 bit field.
172 * - LIBELF_COPY_S32 :: copy a signed 32 bit field.
175 #define LIBELF_COPY_U32(DST, SRC, NAME) do { \
176 if ((SRC)->NAME > UINT32_MAX) { \
177 LIBELF_SET_ERROR(RANGE, 0); \
180 (DST)->NAME = (SRC)->NAME & 0xFFFFFFFFU; \
183 #define LIBELF_COPY_S32(DST, SRC, NAME) do { \
184 if ((SRC)->NAME > INT32_MAX || \
185 (SRC)->NAME < INT32_MIN) { \
186 LIBELF_SET_ERROR(RANGE, 0); \
189 (DST)->NAME = (int32_t) (SRC)->NAME; \
194 * Function Prototypes.
200 struct _Libelf_Data *_libelf_allocate_data(Elf_Scn *_s);
201 Elf *_libelf_allocate_elf(void);
202 Elf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx);
203 Elf_Arhdr *_libelf_ar_gethdr(Elf *_e);
204 Elf *_libelf_ar_open(Elf *_e, int _reporterror);
205 Elf *_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);
206 Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst);
207 Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst);
208 long _libelf_checksum(Elf *_e, int _elfclass);
209 void *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);
210 unsigned int _libelf_falign(Elf_Type _t, int _elfclass);
211 size_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version,
213 int (*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass))
214 (unsigned char *_dst, size_t dsz, unsigned char *_src,
215 size_t _cnt, int _byteswap);
216 void *_libelf_getphdr(Elf *_e, int _elfclass);
217 void *_libelf_getshdr(Elf_Scn *_scn, int _elfclass);
218 void _libelf_init_elf(Elf *_e, Elf_Kind _kind);
219 int _libelf_is_mips64el(Elf *e);
220 int _libelf_load_section_headers(Elf *e, void *ehdr);
221 unsigned int _libelf_malign(Elf_Type _t, int _elfclass);
222 Elf *_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror);
223 size_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);
224 void *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);
225 Elf *_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror);
226 Elf64_Xword _libelf_mips64el_r_info_tof(Elf64_Xword r_info);
227 Elf64_Xword _libelf_mips64el_r_info_tom(Elf64_Xword r_info);
228 struct _Libelf_Data *_libelf_release_data(struct _Libelf_Data *_d);
229 Elf *_libelf_release_elf(Elf *_e);
230 Elf_Scn *_libelf_release_scn(Elf_Scn *_s);
231 int _libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum);
232 int _libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum);
233 int _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass,
235 Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s,
236 unsigned int _encoding, int _elfclass, int _direction);
237 int _libelf_xlate_shtype(uint32_t _sht);
242 #endif /* __LIBELF_H_ */