blob: d8d8487c17032af39da5b672f42ee11dd97fad0e [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Interface for libelf.
Roland McGrath1d3a64d2006-06-15 22:38:05 +00002 Copyright (C) 1998, 1999, 2000, 2002, 2004, 2005, 2006 Red Hat, Inc.
Ulrich Drepper361df7d2006-04-04 21:38:57 +00003 This file is part of Red Hat elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004
Ulrich Drepper361df7d2006-04-04 21:38:57 +00005 Red Hat elfutils is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by the
7 Free Software Foundation; version 2 of the License.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00008
Ulrich Drepper361df7d2006-04-04 21:38:57 +00009 Red Hat elfutils is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000013
Ulrich Drepper361df7d2006-04-04 21:38:57 +000014 You should have received a copy of the GNU General Public License along
15 with Red Hat elfutils; if not, write to the Free Software Foundation,
Ulrich Drepper1e9ef502006-04-04 22:29:06 +000016 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000017
18 In addition, as a special exception, Red Hat, Inc. gives You the
19 additional right to link the code of Red Hat elfutils with code licensed
20 under any Open Source Initiative certified open source license
21 (http://www.opensource.org/licenses/index.php) which requires the
22 distribution of source code with any binary distribution and to
23 distribute linked combinations of the two. Non-GPL Code permitted under
24 this exception must only link to the code of Red Hat elfutils through
25 those well defined interfaces identified in the file named EXCEPTION
26 found in the source code files (the "Approved Interfaces"). The files
27 of Non-GPL Code may instantiate templates or use macros or inline
28 functions from the Approved Interfaces without causing the resulting
29 work to be covered by the GNU General Public License. Only Red Hat,
30 Inc. may make changes or additions to the list of Approved Interfaces.
31 Red Hat's grant of this exception is conditioned upon your not adding
32 any new exceptions. If you wish to add a new Approved Interface or
33 exception, please contact Red Hat. You must obey the GNU General Public
34 License in all respects for all of the Red Hat elfutils code and other
35 code used in conjunction with Red Hat elfutils except the Non-GPL Code
36 covered by this exception. If you modify this file, you may extend this
37 exception to your version of the file, but you are not obligated to do
38 so. If you do not wish to provide this exception without modification,
39 you must delete this exception statement from your version and license
40 this file solely under the GPL without exception.
41
42 Red Hat elfutils is an included package of the Open Invention Network.
43 An included package of the Open Invention Network is a package for which
44 Open Invention Network licensees cross-license their patents. No patent
45 license is granted, either expressly or impliedly, by designation as an
46 included package. Should you wish to participate in the Open Invention
47 Network licensing program, please visit www.openinventionnetwork.com
48 <http://www.openinventionnetwork.com>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000049
50#ifndef _LIBELF_H
51#define _LIBELF_H 1
52
53#include <sys/types.h>
54
55/* Get the ELF types. */
56#include <elf.h>
57
58
59/* Known translation types. */
60typedef enum
61{
62 ELF_T_BYTE, /* unsigned char */
63 ELF_T_ADDR, /* Elf32_Addr, Elf64_Addr, ... */
64 ELF_T_DYN, /* Dynamic section record. */
65 ELF_T_EHDR, /* ELF header. */
66 ELF_T_HALF, /* Elf32_Half, Elf64_Half, ... */
67 ELF_T_OFF, /* Elf32_Off, Elf64_Off, ... */
68 ELF_T_PHDR, /* Program header. */
69 ELF_T_RELA, /* Relocation entry with addend. */
70 ELF_T_REL, /* Relocation entry. */
71 ELF_T_SHDR, /* Section header. */
72 ELF_T_SWORD, /* Elf32_Sword, Elf64_Sword, ... */
73 ELF_T_SYM, /* Symbol record. */
74 ELF_T_WORD, /* Elf32_Word, Elf64_Word, ... */
75 ELF_T_XWORD, /* Elf32_Xword, Elf64_Xword, ... */
76 ELF_T_SXWORD, /* Elf32_Sxword, Elf64_Sxword, ... */
77 ELF_T_VDEF, /* Elf32_Verdef, Elf64_Verdef, ... */
78 ELF_T_VDAUX, /* Elf32_Verdaux, Elf64_Verdaux, ... */
79 ELF_T_VNEED, /* Elf32_Verneed, Elf64_Verneed, ... */
80 ELF_T_VNAUX, /* Elf32_Vernaux, Elf64_Vernaux, ... */
81 ELF_T_NHDR, /* Elf32_Nhdr, Elf64_Nhdr, ... */
82 ELF_T_SYMINFO, /* Elf32_Syminfo, Elf64_Syminfo, ... */
83 ELF_T_MOVE, /* Elf32_Move, Elf64_Move, ... */
84 ELF_T_LIB, /* Elf32_Lib, Elf64_Lib, ... */
85 /* Keep this the last entry. */
86 ELF_T_NUM
87} Elf_Type;
88
89/* Descriptor for data to be converted to or from memory format. */
90typedef struct
91{
92 void *d_buf; /* Pointer to the actual data. */
93 Elf_Type d_type; /* Type of this piece of data. */
94 unsigned int d_version; /* ELF version. */
95 size_t d_size; /* Size in bytes. */
96 off_t d_off; /* Offset into section. */
97 size_t d_align; /* Alignment in section. */
98} Elf_Data;
99
100
101/* Commands for `...'. */
102typedef enum
103{
104 ELF_C_NULL, /* Nothing, terminate, or compute only. */
105 ELF_C_READ, /* Read .. */
106 ELF_C_RDWR, /* Read and write .. */
107 ELF_C_WRITE, /* Write .. */
108 ELF_C_CLR, /* Clear flag. */
109 ELF_C_SET, /* Set flag. */
110 ELF_C_FDDONE, /* Signal that file descriptor will not be
111 used anymore. */
112 ELF_C_FDREAD, /* Read rest of data so that file descriptor
113 is not used anymore. */
114 /* The following are extensions. */
115 ELF_C_READ_MMAP, /* Read, but mmap the file if possible. */
116 ELF_C_RDWR_MMAP, /* Read and write, with mmap. */
117 ELF_C_WRITE_MMAP, /* Write, with mmap. */
118 ELF_C_READ_MMAP_PRIVATE, /* Read, but memory is writable, results are
119 not written to the file. */
120 ELF_C_EMPTY, /* Copy basic file data but not the content. */
121 /* Keep this the last entry. */
122 ELF_C_NUM
123} Elf_Cmd;
124
125
126/* Flags for the ELF structures. */
127enum
128{
129 ELF_F_DIRTY = 0x1,
130#define ELF_F_DIRTY ELF_F_DIRTY
131 ELF_F_LAYOUT = 0x4,
132#define ELF_F_LAYOUT ELF_F_LAYOUT
133 ELF_F_PERMISSIVE = 0x8
134#define ELF_F_PERMISSIVE ELF_F_PERMISSIVE
135};
136
137
138/* Identification values for recognized object files. */
139typedef enum
140{
141 ELF_K_NONE, /* Unknown. */
142 ELF_K_AR, /* Archive. */
143 ELF_K_COFF, /* Stupid old COFF. */
144 ELF_K_ELF, /* ELF file. */
145 /* Keep this the last entry. */
146 ELF_K_NUM
147} Elf_Kind;
148
149
150/* Archive member header. */
151typedef struct
152{
153 char *ar_name; /* Name of archive member. */
154 time_t ar_date; /* File date. */
155 uid_t ar_uid; /* User ID. */
156 gid_t ar_gid; /* Group ID. */
157 mode_t ar_mode; /* File mode. */
158 off_t ar_size; /* File size. */
159 char *ar_rawname; /* Original name of archive member. */
160} Elf_Arhdr;
161
162
163/* Archive symbol table entry. */
164typedef struct
165{
166 char *as_name; /* Symbol name. */
167 size_t as_off; /* Offset for this file in the archive. */
168 unsigned long int as_hash; /* Hash value of the name. */
169} Elf_Arsym;
170
171
172/* Descriptor for the ELF file. */
173typedef struct Elf Elf;
174
175/* Descriptor for ELF file section. */
176typedef struct Elf_Scn Elf_Scn;
177
178
179#ifdef __cplusplus
180extern "C" {
181#endif
182
183/* Return descriptor for ELF file to work according to CMD. */
184extern Elf *elf_begin (int __fildes, Elf_Cmd __cmd, Elf *__ref);
185
186/* Create a clone of an existing ELF descriptor. */
187 extern Elf *elf_clone (Elf *__elf, Elf_Cmd __cmd);
188
189/* Create descriptor for memory region. */
190extern Elf *elf_memory (char *__image, size_t __size);
191
192/* Advance archive descriptor to next element. */
193extern Elf_Cmd elf_next (Elf *__elf);
194
195/* Free resources allocated for ELF. */
196extern int elf_end (Elf *__elf);
197
198/* Update ELF descriptor and write file to disk. */
199extern off_t elf_update (Elf *__elf, Elf_Cmd __cmd);
200
201/* Determine what kind of file is associated with ELF. */
202extern Elf_Kind elf_kind (Elf *__elf) __attribute__ ((__pure__));
203
204/* Get the base offset for an object file. */
205extern off_t elf_getbase (Elf *__elf);
206
207
208/* Retrieve file identification data. */
Roland McGrath1d3a64d2006-06-15 22:38:05 +0000209extern char *elf_getident (Elf *__elf, size_t *__nbytes);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000210
211/* Retrieve class-dependent object file header. */
212extern Elf32_Ehdr *elf32_getehdr (Elf *__elf);
213/* Similar but this time the binary calls is ELFCLASS64. */
214extern Elf64_Ehdr *elf64_getehdr (Elf *__elf);
215
216/* Create ELF header if none exists. */
217extern Elf32_Ehdr *elf32_newehdr (Elf *__elf);
218/* Similar but this time the binary calls is ELFCLASS64. */
219extern Elf64_Ehdr *elf64_newehdr (Elf *__elf);
220
221/* Retrieve class-dependent program header table. */
222extern Elf32_Phdr *elf32_getphdr (Elf *__elf);
223/* Similar but this time the binary calls is ELFCLASS64. */
224extern Elf64_Phdr *elf64_getphdr (Elf *__elf);
225
226/* Create ELF program header. */
227extern Elf32_Phdr *elf32_newphdr (Elf *__elf, size_t __cnt);
228/* Similar but this time the binary calls is ELFCLASS64. */
229extern Elf64_Phdr *elf64_newphdr (Elf *__elf, size_t __cnt);
230
231
232/* Get section at INDEX. */
233extern Elf_Scn *elf_getscn (Elf *__elf, size_t __index);
234
Ulrich Drepper41de4882005-08-03 00:02:56 +0000235/* Get section at OFFSET. */
236extern Elf_Scn *elf32_offscn (Elf *__elf, Elf32_Off __offset);
237/* Similar bug this time the binary calls is ELFCLASS64. */
238extern Elf_Scn *elf64_offscn (Elf *__elf, Elf64_Off __offset);
239
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000240/* Get index of section. */
241extern size_t elf_ndxscn (Elf_Scn *__scn);
242
243/* Get section with next section index. */
244extern Elf_Scn *elf_nextscn (Elf *__elf, Elf_Scn *__scn);
245
246/* Create a new section and append it at the end of the table. */
247extern Elf_Scn *elf_newscn (Elf *__elf);
248
249/* Get the number of sections in the ELF file. If the file uses more
250 sections than can be represented in the e_shnum field of the ELF
251 header the information from the sh_size field in the zeroth section
252 header is used. */
253extern int elf_getshnum (Elf *__elf, size_t *__dst);
254
255
256/* Get the section index of the section header string table in the ELF
257 file. If the index cannot be represented in the e_shnum field of
258 the ELF header the information from the sh_link field in the zeroth
259 section header is used. */
260extern int elf_getshstrndx (Elf *__elf, size_t *__dst);
261
262
263/* Retrieve section header of ELFCLASS32 binary. */
264extern Elf32_Shdr *elf32_getshdr (Elf_Scn *__scn);
265/* Similar for ELFCLASS64. */
266extern Elf64_Shdr *elf64_getshdr (Elf_Scn *__scn);
267
268
269/* Set or clear flags for ELF file. */
270extern unsigned int elf_flagelf (Elf *__elf, Elf_Cmd __cmd,
271 unsigned int __flags);
272/* Similarly for the ELF header. */
273extern unsigned int elf_flagehdr (Elf *__elf, Elf_Cmd __cmd,
274 unsigned int __flags);
275/* Similarly for the ELF program header. */
276extern unsigned int elf_flagphdr (Elf *__elf, Elf_Cmd __cmd,
277 unsigned int __flags);
278/* Similarly for the given ELF section. */
279extern unsigned int elf_flagscn (Elf_Scn *__scn, Elf_Cmd __cmd,
280 unsigned int __flags);
281/* Similarly for the given ELF data. */
282extern unsigned int elf_flagdata (Elf_Data *__data, Elf_Cmd __cmd,
283 unsigned int __flags);
284/* Similarly for the given ELF section header. */
285extern unsigned int elf_flagshdr (Elf_Scn *__scn, Elf_Cmd __cmd,
286 unsigned int __flags);
287
288
289/* Get data from section while translating from file representation
290 to memory representation. */
291extern Elf_Data *elf_getdata (Elf_Scn *__scn, Elf_Data *__data);
292
293/* Get uninterpreted section content. */
294extern Elf_Data *elf_rawdata (Elf_Scn *__scn, Elf_Data *__data);
295
296/* Create new data descriptor for section SCN. */
297extern Elf_Data *elf_newdata (Elf_Scn *__scn);
298
299
300/* Return pointer to string at OFFSET in section INDEX. */
301extern char *elf_strptr (Elf *__elf, size_t __index, size_t __offset);
302
303
304/* Return header of archive. */
305extern Elf_Arhdr *elf_getarhdr (Elf *__elf);
306
Ulrich Drepper5d832292005-08-15 21:36:27 +0000307/* Return offset in archive for current file ELF. */
308extern off_t elf_getaroff (Elf *__elf);
309
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000310/* Select archive element at OFFSET. */
311extern size_t elf_rand (Elf *__elf, size_t __offset);
312
Roland McGrath1d3a64d2006-06-15 22:38:05 +0000313/* Get symbol table of archive. */
314extern Elf_Arsym *elf_getarsym (Elf *__elf, size_t *__narsyms);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000315
316
317/* Control ELF descriptor. */
318extern int elf_cntl (Elf *__elf, Elf_Cmd __cmd);
319
320/* Retrieve uninterpreted file contents. */
Roland McGrath1d3a64d2006-06-15 22:38:05 +0000321extern char *elf_rawfile (Elf *__elf, size_t *__nbytes);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000322
323
324/* Return size of array of COUNT elements of the type denoted by TYPE
325 in the external representation. The binary class is taken from ELF.
326 The result is based on version VERSION of the ELF standard. */
327extern size_t elf32_fsize (Elf_Type __type, size_t __count,
328 unsigned int __version)
329 __attribute__ ((__const__));
330/* Similar but this time the binary calls is ELFCLASS64. */
331extern size_t elf64_fsize (Elf_Type __type, size_t __count,
332 unsigned int __version)
333 __attribute__ ((__const__));
334
335
336/* Convert data structure from the representation in the file represented
337 by ELF to their memory representation. */
338extern Elf_Data *elf32_xlatetom (Elf_Data *__dest, const Elf_Data *__src,
339 unsigned int __encode);
340/* Same for 64 bit class. */
341extern Elf_Data *elf64_xlatetom (Elf_Data *__dest, const Elf_Data *__src,
342 unsigned int __encode);
343
344/* Convert data structure from to the representation in memory
345 represented by ELF file representation. */
346extern Elf_Data *elf32_xlatetof (Elf_Data *__dest, const Elf_Data *__src,
347 unsigned int __encode);
348/* Same for 64 bit class. */
349extern Elf_Data *elf64_xlatetof (Elf_Data *__dest, const Elf_Data *__src,
350 unsigned int __encode);
351
352
353/* Return error code of last failing function call. This value is kept
354 separately for each thread. */
355extern int elf_errno (void);
356
357/* Return error string for ERROR. If ERROR is zero, return error string
358 for most recent error or NULL is none occurred. If ERROR is -1 the
359 behaviour is similar to the last case except that not NULL but a legal
360 string is returned. */
361extern const char *elf_errmsg (int __error);
362
363
364/* Coordinate ELF library and application versions. */
365extern unsigned int elf_version (unsigned int __version);
366
367/* Set fill bytes used to fill holes in data structures. */
368extern void elf_fill (int __fill);
369
370/* Compute hash value. */
371extern unsigned long int elf_hash (const char *__string)
372 __attribute__ ((__pure__));
373
Ulrich Drepper28ed8952006-07-07 03:43:47 +0000374/* Compute hash value using the GNU-specific hash function. */
375extern unsigned long int elf_gnu_hash (const char *__string)
376 __attribute__ ((__pure__));
377
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000378
379/* Compute simple checksum from permanent parts of the ELF file. */
380extern long int elf32_checksum (Elf *__elf);
381/* Similar but this time the binary calls is ELFCLASS64. */
382extern long int elf64_checksum (Elf *__elf);
383
384#ifdef __cplusplus
385}
386#endif
387
388#endif /* libelf.h */