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