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