1 | /*- |
---|
2 | * Copyright (c) 2006,2008-2011 Joseph Koshy |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
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. |
---|
13 | * |
---|
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 |
---|
24 | * SUCH DAMAGE. |
---|
25 | * |
---|
26 | * $Id: _libelf.h 1921 2011-09-23 08:04:02Z jkoshy $ |
---|
27 | */ |
---|
28 | |
---|
29 | #ifndef __LIBELF_H_ |
---|
30 | #define __LIBELF_H_ |
---|
31 | |
---|
32 | #include <sys/queue.h> |
---|
33 | |
---|
34 | #include "_libelf_config.h" |
---|
35 | |
---|
36 | #include "_elftc.h" |
---|
37 | |
---|
38 | /* |
---|
39 | * Library-private data structures. |
---|
40 | */ |
---|
41 | |
---|
42 | #define LIBELF_MSG_SIZE 256 |
---|
43 | |
---|
44 | struct _libelf_globals { |
---|
45 | int libelf_arch; |
---|
46 | unsigned int libelf_byteorder; |
---|
47 | int libelf_class; |
---|
48 | int libelf_error; |
---|
49 | int libelf_fillchar; |
---|
50 | unsigned int libelf_version; |
---|
51 | char libelf_msg[LIBELF_MSG_SIZE]; |
---|
52 | }; |
---|
53 | |
---|
54 | extern struct _libelf_globals _libelf; |
---|
55 | |
---|
56 | #define LIBELF_PRIVATE(N) (_libelf.libelf_##N) |
---|
57 | |
---|
58 | #define LIBELF_ELF_ERROR_MASK 0xFF |
---|
59 | #define LIBELF_OS_ERROR_SHIFT 8 |
---|
60 | |
---|
61 | #define LIBELF_SET_ERROR(E, O) do { \ |
---|
62 | LIBELF_PRIVATE(error) = ((ELF_E_##E & LIBELF_ELF_ERROR_MASK)| \ |
---|
63 | ((O) << LIBELF_OS_ERROR_SHIFT)); \ |
---|
64 | } while (0) |
---|
65 | |
---|
66 | #define LIBELF_ADJUST_AR_SIZE(S) (((S) + 1U) & ~1U) |
---|
67 | |
---|
68 | /* |
---|
69 | * Flags for library internal use. These use the upper 16 bits of the |
---|
70 | * `e_flags' field. |
---|
71 | */ |
---|
72 | #define LIBELF_F_API_MASK 0x00FFFF /* Flags defined by the API. */ |
---|
73 | #define LIBELF_F_AR_HEADER 0x010000 /* translated header available */ |
---|
74 | #define LIBELF_F_AR_VARIANT_SVR4 0x020000 /* BSD style ar(1) archive */ |
---|
75 | #define LIBELF_F_DATA_MALLOCED 0x040000 /* whether data was malloc'ed */ |
---|
76 | #define LIBELF_F_RAWFILE_MALLOC 0x080000 /* whether e_rawfile was malloc'ed */ |
---|
77 | #define LIBELF_F_RAWFILE_MMAP 0x100000 /* whether e_rawfile was mmap'ed */ |
---|
78 | #define LIBELF_F_SHDRS_LOADED 0x200000 /* whether all shdrs were read in */ |
---|
79 | #define LIBELF_F_SPECIAL_FILE 0x400000 /* non-regular file */ |
---|
80 | |
---|
81 | struct _Elf { |
---|
82 | int e_activations; /* activation count */ |
---|
83 | unsigned int e_byteorder; /* ELFDATA* */ |
---|
84 | int e_class; /* ELFCLASS* */ |
---|
85 | Elf_Cmd e_cmd; /* ELF_C_* used at creation time */ |
---|
86 | int e_fd; /* associated file descriptor */ |
---|
87 | unsigned int e_flags; /* ELF_F_* & LIBELF_F_* flags */ |
---|
88 | Elf_Kind e_kind; /* ELF_K_* */ |
---|
89 | Elf *e_parent; /* non-NULL for archive members */ |
---|
90 | char *e_rawfile; /* uninterpreted bytes */ |
---|
91 | size_t e_rawsize; /* size of uninterpreted bytes */ |
---|
92 | unsigned int e_version; /* file version */ |
---|
93 | |
---|
94 | /* |
---|
95 | * Header information for archive members. See the |
---|
96 | * LIBELF_F_AR_HEADER flag. |
---|
97 | */ |
---|
98 | union { |
---|
99 | Elf_Arhdr *e_arhdr; /* translated header */ |
---|
100 | char *e_rawhdr; /* untranslated header */ |
---|
101 | } e_hdr; |
---|
102 | |
---|
103 | union { |
---|
104 | struct { /* ar(1) archives */ |
---|
105 | off_t e_next; /* set by elf_rand()/elf_next() */ |
---|
106 | int e_nchildren; |
---|
107 | char *e_rawstrtab; /* file name strings */ |
---|
108 | size_t e_rawstrtabsz; |
---|
109 | char *e_rawsymtab; /* symbol table */ |
---|
110 | size_t e_rawsymtabsz; |
---|
111 | Elf_Arsym *e_symtab; |
---|
112 | size_t e_symtabsz; |
---|
113 | } e_ar; |
---|
114 | struct { /* regular ELF files */ |
---|
115 | union { |
---|
116 | Elf32_Ehdr *e_ehdr32; |
---|
117 | Elf64_Ehdr *e_ehdr64; |
---|
118 | } e_ehdr; |
---|
119 | union { |
---|
120 | Elf32_Phdr *e_phdr32; |
---|
121 | Elf64_Phdr *e_phdr64; |
---|
122 | } e_phdr; |
---|
123 | STAILQ_HEAD(, _Elf_Scn) e_scn; /* section list */ |
---|
124 | size_t e_nphdr; /* number of Phdr entries */ |
---|
125 | size_t e_nscn; /* number of sections */ |
---|
126 | size_t e_strndx; /* string table section index */ |
---|
127 | } e_elf; |
---|
128 | } e_u; |
---|
129 | }; |
---|
130 | |
---|
131 | struct _Elf_Scn { |
---|
132 | union { |
---|
133 | Elf32_Shdr s_shdr32; |
---|
134 | Elf64_Shdr s_shdr64; |
---|
135 | } s_shdr; |
---|
136 | STAILQ_HEAD(, _Elf_Data) s_data; /* list of Elf_Data descriptors */ |
---|
137 | STAILQ_HEAD(, _Elf_Data) s_rawdata; /* raw data for this section */ |
---|
138 | STAILQ_ENTRY(_Elf_Scn) s_next; |
---|
139 | struct _Elf *s_elf; /* parent ELF descriptor */ |
---|
140 | unsigned int s_flags; /* flags for the section as a whole */ |
---|
141 | size_t s_ndx; /* index# for this section */ |
---|
142 | uint64_t s_offset; /* managed by elf_update() */ |
---|
143 | uint64_t s_rawoff; /* original offset in the file */ |
---|
144 | uint64_t s_size; /* managed by elf_update() */ |
---|
145 | }; |
---|
146 | |
---|
147 | |
---|
148 | enum { |
---|
149 | ELF_TOFILE, |
---|
150 | ELF_TOMEMORY |
---|
151 | }; |
---|
152 | |
---|
153 | #define LIBELF_COPY_U32(DST,SRC,NAME) do { \ |
---|
154 | if ((SRC)->NAME > UINT_MAX) { \ |
---|
155 | LIBELF_SET_ERROR(RANGE, 0); \ |
---|
156 | return (0); \ |
---|
157 | } \ |
---|
158 | (DST)->NAME = (SRC)->NAME; \ |
---|
159 | } while (0) |
---|
160 | |
---|
161 | #define LIBELF_COPY_S32(DST,SRC,NAME) do { \ |
---|
162 | if ((SRC)->NAME > INT_MAX || \ |
---|
163 | (SRC)->NAME < INT_MIN) { \ |
---|
164 | LIBELF_SET_ERROR(RANGE, 0); \ |
---|
165 | return (0); \ |
---|
166 | } \ |
---|
167 | (DST)->NAME = (SRC)->NAME; \ |
---|
168 | } while (0) |
---|
169 | |
---|
170 | |
---|
171 | /* |
---|
172 | * Function Prototypes. |
---|
173 | */ |
---|
174 | |
---|
175 | __BEGIN_DECLS |
---|
176 | Elf_Data *_libelf_allocate_data(Elf_Scn *_s); |
---|
177 | Elf *_libelf_allocate_elf(void); |
---|
178 | Elf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx); |
---|
179 | Elf_Arhdr *_libelf_ar_gethdr(Elf *_e); |
---|
180 | Elf *_libelf_ar_open(Elf *_e); |
---|
181 | Elf *_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar); |
---|
182 | int _libelf_ar_get_member(char *_s, size_t _sz, int _base, size_t *_ret); |
---|
183 | Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst); |
---|
184 | Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst); |
---|
185 | unsigned long _libelf_checksum(Elf *_e, int _elfclass); |
---|
186 | void *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate); |
---|
187 | int _libelf_falign(Elf_Type _t, int _elfclass); |
---|
188 | size_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version, |
---|
189 | size_t count); |
---|
190 | int (*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass)) |
---|
191 | (char *_dst, size_t dsz, char *_src, size_t _cnt, int _byteswap); |
---|
192 | void *_libelf_getphdr(Elf *_e, int _elfclass); |
---|
193 | void *_libelf_getshdr(Elf_Scn *_scn, int _elfclass); |
---|
194 | void _libelf_init_elf(Elf *_e, Elf_Kind _kind); |
---|
195 | int _libelf_load_section_headers(Elf *e, void *ehdr); |
---|
196 | int _libelf_malign(Elf_Type _t, int _elfclass); |
---|
197 | size_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version); |
---|
198 | void *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count); |
---|
199 | Elf_Data *_libelf_release_data(Elf_Data *_d); |
---|
200 | Elf *_libelf_release_elf(Elf *_e); |
---|
201 | Elf_Scn *_libelf_release_scn(Elf_Scn *_s); |
---|
202 | int _libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum); |
---|
203 | int _libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum); |
---|
204 | int _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass, |
---|
205 | size_t _shstrndx); |
---|
206 | Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s, |
---|
207 | unsigned int _encoding, int _elfclass, int _direction); |
---|
208 | int _libelf_xlate_shtype(uint32_t _sht); |
---|
209 | __END_DECLS |
---|
210 | |
---|
211 | #endif /* __LIBELF_H_ */ |
---|