Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Interface for libelf.
2 Copyright (C) 1998-2010, 2015 Red Hat, Inc.
3 This file is part of elfutils.
4
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
7
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
11
12 or
13
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
17
18 or both in parallel, as here.
19
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
24
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
28
29#ifndef _LIBELF_H
30#define _LIBELF_H 1
31
32#include <stdint.h>
33#include <sys/types.h>
34
35/* Get the ELF types. */
36#include <elf.h>
37
38#ifndef SHF_COMPRESSED
39 /* Older glibc elf.h might not yet define the ELF compression types. */
40 #define SHF_COMPRESSED (1 << 11) /* Section with compressed data. */
41
42 /* Section compression header. Used when SHF_COMPRESSED is set. */
43
44 typedef struct
45 {
46 Elf32_Word ch_type; /* Compression format. */
47 Elf32_Word ch_size; /* Uncompressed data size. */
48 Elf32_Word ch_addralign; /* Uncompressed data alignment. */
49 } Elf32_Chdr;
50
51 typedef struct
52 {
53 Elf64_Word ch_type; /* Compression format. */
54 Elf64_Word ch_reserved;
55 Elf64_Xword ch_size; /* Uncompressed data size. */
56 Elf64_Xword ch_addralign; /* Uncompressed data alignment. */
57 } Elf64_Chdr;
58
59 /* Legal values for ch_type (compression algorithm). */
60 #define ELFCOMPRESS_ZLIB 1 /* ZLIB/DEFLATE algorithm. */
61 #define ELFCOMPRESS_LOOS 0x60000000 /* Start of OS-specific. */
62 #define ELFCOMPRESS_HIOS 0x6fffffff /* End of OS-specific. */
63 #define ELFCOMPRESS_LOPROC 0x70000000 /* Start of processor-specific. */
64 #define ELFCOMPRESS_HIPROC 0x7fffffff /* End of processor-specific. */
65#endif
66
67#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
68# define __nonnull_attribute__(...) __attribute__ ((__nonnull__ (__VA_ARGS__)))
69# define __deprecated_attribute__ __attribute__ ((__deprecated__))
70# define __pure_attribute__ __attribute__ ((__pure__))
71# define __const_attribute__ __attribute__ ((__const__))
72#else
73# define __nonnull_attribute__(...)
74# define __deprecated_attribute__
75# define __pure_attribute__
76# define __const_attribute__
77#endif
78
79#if __GNUC__ < 4
80#define __noreturn_attribute__
81#else
82#define __noreturn_attribute__ __attribute__ ((noreturn))
83#endif
84
85#ifdef __GNUC_STDC_INLINE__
86# define __libdw_extern_inline extern __inline __attribute__ ((__gnu_inline__))
87#else
88# define __libdw_extern_inline extern __inline
89#endif
90
91/* Known translation types. */
92typedef enum
93{
94 ELF_T_BYTE, /* unsigned char */
95 ELF_T_ADDR, /* Elf32_Addr, Elf64_Addr, ... */
96 ELF_T_DYN, /* Dynamic section record. */
97 ELF_T_EHDR, /* ELF header. */
98 ELF_T_HALF, /* Elf32_Half, Elf64_Half, ... */
99 ELF_T_OFF, /* Elf32_Off, Elf64_Off, ... */
100 ELF_T_PHDR, /* Program header. */
101 ELF_T_RELA, /* Relocation entry with addend. */
102 ELF_T_REL, /* Relocation entry. */
103 ELF_T_SHDR, /* Section header. */
104 ELF_T_SWORD, /* Elf32_Sword, Elf64_Sword, ... */
105 ELF_T_SYM, /* Symbol record. */
106 ELF_T_WORD, /* Elf32_Word, Elf64_Word, ... */
107 ELF_T_XWORD, /* Elf32_Xword, Elf64_Xword, ... */
108 ELF_T_SXWORD, /* Elf32_Sxword, Elf64_Sxword, ... */
109 ELF_T_VDEF, /* Elf32_Verdef, Elf64_Verdef, ... */
110 ELF_T_VDAUX, /* Elf32_Verdaux, Elf64_Verdaux, ... */
111 ELF_T_VNEED, /* Elf32_Verneed, Elf64_Verneed, ... */
112 ELF_T_VNAUX, /* Elf32_Vernaux, Elf64_Vernaux, ... */
113 ELF_T_NHDR, /* Elf32_Nhdr, Elf64_Nhdr, ... */
114 ELF_T_SYMINFO, /* Elf32_Syminfo, Elf64_Syminfo, ... */
115 ELF_T_MOVE, /* Elf32_Move, Elf64_Move, ... */
116 ELF_T_LIB, /* Elf32_Lib, Elf64_Lib, ... */
117 ELF_T_GNUHASH, /* GNU-style hash section. */
118 ELF_T_AUXV, /* Elf32_auxv_t, Elf64_auxv_t, ... */
119 ELF_T_CHDR, /* Compressed, Elf32_Chdr, Elf64_Chdr, ... */
120 /* Keep this the last entry. */
121 ELF_T_NUM
122} Elf_Type;
123
124/* Descriptor for data to be converted to or from memory format. */
125typedef struct
126{
127 void *d_buf; /* Pointer to the actual data. */
128 Elf_Type d_type; /* Type of this piece of data. */
129 unsigned int d_version; /* ELF version. */
130 size_t d_size; /* Size in bytes. */
131 int64_t d_off; /* Offset into section. */
132 size_t d_align; /* Alignment in section. */
133} Elf_Data;
134
135
136/* Commands for `...'. */
137typedef enum
138{
139 ELF_C_NULL, /* Nothing, terminate, or compute only. */
140 ELF_C_READ, /* Read .. */
141 ELF_C_RDWR, /* Read and write .. */
142 ELF_C_WRITE, /* Write .. */
143 ELF_C_CLR, /* Clear flag. */
144 ELF_C_SET, /* Set flag. */
145 ELF_C_FDDONE, /* Signal that file descriptor will not be
146 used anymore. */
147 ELF_C_FDREAD, /* Read rest of data so that file descriptor
148 is not used anymore. */
149 /* The following are extensions. */
150 ELF_C_READ_MMAP, /* Read, but mmap the file if possible. */
151 ELF_C_RDWR_MMAP, /* Read and write, with mmap. */
152 ELF_C_WRITE_MMAP, /* Write, with mmap. */
153 ELF_C_READ_MMAP_PRIVATE, /* Read, but memory is writable, results are
154 not written to the file. */
155 ELF_C_EMPTY, /* Copy basic file data but not the content. */
156 /* Keep this the last entry. */
157 ELF_C_NUM
158} Elf_Cmd;
159
160
161/* Flags for the ELF structures. */
162enum
163{
164 ELF_F_DIRTY = 0x1,
165#define ELF_F_DIRTY ELF_F_DIRTY
166 ELF_F_LAYOUT = 0x4,
167#define ELF_F_LAYOUT ELF_F_LAYOUT
168 ELF_F_PERMISSIVE = 0x8
169#define ELF_F_PERMISSIVE ELF_F_PERMISSIVE
170};
171
172/* Flags for elf_compress[_gnu]. */
173enum
174{
175 ELF_CHF_FORCE = 0x1
176#define ELF_CHF_FORCE ELF_CHF_FORCE
177};
178
179/* Identification values for recognized object files. */
180typedef enum
181{
182 ELF_K_NONE, /* Unknown. */
183 ELF_K_AR, /* Archive. */
184 ELF_K_COFF, /* Stupid old COFF. */
185 ELF_K_ELF, /* ELF file. */
186 /* Keep this the last entry. */
187 ELF_K_NUM
188} Elf_Kind;
189
190
191/* Archive member header. */
192typedef struct
193{
194 char *ar_name; /* Name of archive member. */
195 time_t ar_date; /* File date. */
196 uid_t ar_uid; /* User ID. */
197 gid_t ar_gid; /* Group ID. */
198 mode_t ar_mode; /* File mode. */
199 int64_t ar_size; /* File size. */
200 char *ar_rawname; /* Original name of archive member. */
201} Elf_Arhdr;
202
203
204/* Archive symbol table entry. */
205typedef struct
206{
207 char *as_name; /* Symbol name. */
208 size_t as_off; /* Offset for this file in the archive. */
209 unsigned long int as_hash; /* Hash value of the name. */
210} Elf_Arsym;
211
212
213/* Descriptor for the ELF file. */
214typedef struct Elf Elf;
215
216/* Descriptor for ELF file section. */
217typedef struct Elf_Scn Elf_Scn;
218
219
220#ifdef __cplusplus
221extern "C" {
222#endif
223
224/* Return descriptor for ELF file to work according to CMD. */
225extern Elf *elf_begin (int __fildes, Elf_Cmd __cmd, Elf *__ref);
226
227/* Create a clone of an existing ELF descriptor. */
228 extern Elf *elf_clone (Elf *__elf, Elf_Cmd __cmd);
229
230/* Create descriptor for memory region. */
231extern Elf *elf_memory (char *__image, size_t __size);
232
233/* Advance archive descriptor to next element. */
234extern Elf_Cmd elf_next (Elf *__elf);
235
236/* Free resources allocated for ELF. */
237extern int elf_end (Elf *__elf);
238
239/* Update ELF descriptor and write file to disk. */
240extern int64_t elf_update (Elf *__elf, Elf_Cmd __cmd);
241
242/* Determine what kind of file is associated with ELF. */
243extern Elf_Kind elf_kind (Elf *__elf) __pure_attribute__;
244
245/* Get the base offset for an object file. */
246extern int64_t elf_getbase (Elf *__elf);
247
248
249/* Retrieve file identification data. */
250extern char *elf_getident (Elf *__elf, size_t *__nbytes);
251
252/* Retrieve class-dependent object file header. */
253extern Elf32_Ehdr *elf32_getehdr (Elf *__elf);
254/* Similar but this time the binary calls is ELFCLASS64. */
255extern Elf64_Ehdr *elf64_getehdr (Elf *__elf);
256
257/* Create ELF header if none exists. */
258extern Elf32_Ehdr *elf32_newehdr (Elf *__elf);
259/* Similar but this time the binary calls is ELFCLASS64. */
260extern Elf64_Ehdr *elf64_newehdr (Elf *__elf);
261
262/* Get the number of program headers in the ELF file. If the file uses
263 more headers than can be represented in the e_phnum field of the ELF
264 header the information from the sh_info field in the zeroth section
265 header is used. */
266extern int elf_getphdrnum (Elf *__elf, size_t *__dst);
267
268/* Retrieve class-dependent program header table. */
269extern Elf32_Phdr *elf32_getphdr (Elf *__elf);
270/* Similar but this time the binary calls is ELFCLASS64. */
271extern Elf64_Phdr *elf64_getphdr (Elf *__elf);
272
273/* Create ELF program header. */
274extern Elf32_Phdr *elf32_newphdr (Elf *__elf, size_t __cnt);
275/* Similar but this time the binary calls is ELFCLASS64. */
276extern Elf64_Phdr *elf64_newphdr (Elf *__elf, size_t __cnt);
277
278
279/* Get section at INDEX. */
280extern Elf_Scn *elf_getscn (Elf *__elf, size_t __index);
281
282/* Get section at OFFSET. */
283extern Elf_Scn *elf32_offscn (Elf *__elf, Elf32_Off __offset);
284/* Similar bug this time the binary calls is ELFCLASS64. */
285extern Elf_Scn *elf64_offscn (Elf *__elf, Elf64_Off __offset);
286
287/* Get index of section. */
288extern size_t elf_ndxscn (Elf_Scn *__scn);
289
290/* Get section with next section index. */
291extern Elf_Scn *elf_nextscn (Elf *__elf, Elf_Scn *__scn);
292
293/* Create a new section and append it at the end of the table. */
294extern Elf_Scn *elf_newscn (Elf *__elf);
295
296/* Get the section index of the extended section index table for the
297 given symbol table. */
298extern int elf_scnshndx (Elf_Scn *__scn);
299
300/* Get the number of sections in the ELF file. If the file uses more
301 sections than can be represented in the e_shnum field of the ELF
302 header the information from the sh_size field in the zeroth section
303 header is used. */
304extern int elf_getshdrnum (Elf *__elf, size_t *__dst);
305/* Sun messed up the implementation of 'elf_getshnum' in their implementation.
306 It was agreed to make the same functionality available under a different
307 name and obsolete the old name. */
308extern int elf_getshnum (Elf *__elf, size_t *__dst)
309 __deprecated_attribute__;
310
311
312/* Get the section index of the section header string table in the ELF
313 file. If the index cannot be represented in the e_shstrndx field of
314 the ELF header the information from the sh_link field in the zeroth
315 section header is used. */
316extern int elf_getshdrstrndx (Elf *__elf, size_t *__dst);
317/* Sun messed up the implementation of 'elf_getshstrndx' in their
318 implementation. It was agreed to make the same functionality available
319 under a different name and obsolete the old name. */
320extern int elf_getshstrndx (Elf *__elf, size_t *__dst)
321 __deprecated_attribute__;
322
323
324/* Retrieve section header of ELFCLASS32 binary. */
325extern Elf32_Shdr *elf32_getshdr (Elf_Scn *__scn);
326/* Similar for ELFCLASS64. */
327extern Elf64_Shdr *elf64_getshdr (Elf_Scn *__scn);
328
329/* Returns compression header for a section if section data is
330 compressed. Returns NULL and sets elf_errno if the section isn't
331 compressed or an error occurred. */
332extern Elf32_Chdr *elf32_getchdr (Elf_Scn *__scn);
333extern Elf64_Chdr *elf64_getchdr (Elf_Scn *__scn);
334
335/* Compress or decompress the data of a section and adjust the section
336 header.
337
338 elf_compress works by setting or clearing the SHF_COMPRESS flag
339 from the section Shdr and will encode or decode a Elf32_Chdr or
340 Elf64_Chdr at the start of the section data. elf_compress_gnu will
341 encode or decode any section, but is traditionally only used for
342 sections that have a name starting with ".debug" when
343 uncompressed or ".zdebug" when compressed and stores just the
344 uncompressed size. The GNU compression method is deprecated and
345 should only be used for legacy support.
346
347 elf_compress takes a compression type that should be either zero to
348 decompress or an ELFCOMPRESS algorithm to use for compression.
349 Currently only ELFCOMPRESS_ZLIB is supported. elf_compress_gnu
350 will compress in the traditional GNU compression format when
351 compress is one and decompress the section data when compress is
352 zero.
353
354 The FLAGS argument can be zero or ELF_CHF_FORCE. If FLAGS contains
355 ELF_CHF_FORCE then it will always compress the section, even if
356 that would not reduce the size of the data section (including the
357 header). Otherwise elf_compress and elf_compress_gnu will compress
358 the section only if the total data size is reduced.
359
360 On successful compression or decompression the function returns
361 one. If (not forced) compression is requested and the data section
362 would not actually reduce in size, the section is not actually
363 compressed and zero is returned. Otherwise -1 is returned and
364 elf_errno is set.
365
366 It is an error to request compression for a section that already
367 has SHF_COMPRESSED set, or (for elf_compress) to request
368 decompression for an section that doesn't have SHF_COMPRESSED set.
369 If a section has SHF_COMPRESSED set then calling elf_compress_gnu
370 will result in an error. The section has to be decompressed first
371 using elf_compress. Calling elf_compress on a section compressed
372 with elf_compress_gnu is fine, but probably useless.
373
374 It is always an error to call these functions on SHT_NOBITS
375 sections or if the section has the SHF_ALLOC flag set.
376 elf_compress_gnu will not check whether the section name starts
377 with ".debug" or .zdebug". It is the responsibilty of the caller
378 to make sure the deprecated GNU compression method is only called
379 on correctly named sections (and to change the name of the section
380 when using elf_compress_gnu).
381
382 All previous returned Shdrs and Elf_Data buffers are invalidated by
383 this call and should no longer be accessed.
384
385 Note that although this changes the header and data returned it
386 doesn't mark the section as dirty. To keep the changes when
387 calling elf_update the section has to be flagged ELF_F_DIRTY. */
388extern int elf_compress (Elf_Scn *scn, int type, unsigned int flags);
389extern int elf_compress_gnu (Elf_Scn *scn, int compress, unsigned int flags);
390
391/* Set or clear flags for ELF file. */
392extern unsigned int elf_flagelf (Elf *__elf, Elf_Cmd __cmd,
393 unsigned int __flags);
394/* Similarly for the ELF header. */
395extern unsigned int elf_flagehdr (Elf *__elf, Elf_Cmd __cmd,
396 unsigned int __flags);
397/* Similarly for the ELF program header. */
398extern unsigned int elf_flagphdr (Elf *__elf, Elf_Cmd __cmd,
399 unsigned int __flags);
400/* Similarly for the given ELF section. */
401extern unsigned int elf_flagscn (Elf_Scn *__scn, Elf_Cmd __cmd,
402 unsigned int __flags);
403/* Similarly for the given ELF data. */
404extern unsigned int elf_flagdata (Elf_Data *__data, Elf_Cmd __cmd,
405 unsigned int __flags);
406/* Similarly for the given ELF section header. */
407extern unsigned int elf_flagshdr (Elf_Scn *__scn, Elf_Cmd __cmd,
408 unsigned int __flags);
409
410
411/* Get data from section while translating from file representation to
412 memory representation. The Elf_Data d_type is set based on the
413 section type if known. Otherwise d_type is set to ELF_T_BYTE. If
414 the section contains compressed data then d_type is always set to
415 ELF_T_CHDR. */
416extern Elf_Data *elf_getdata (Elf_Scn *__scn, Elf_Data *__data);
417
418/* Get uninterpreted section content. */
419extern Elf_Data *elf_rawdata (Elf_Scn *__scn, Elf_Data *__data);
420
421/* Create new data descriptor for section SCN. */
422extern Elf_Data *elf_newdata (Elf_Scn *__scn);
423
424/* Get data translated from a chunk of the file contents as section data
425 would be for TYPE. The resulting Elf_Data pointer is valid until
426 elf_end (ELF) is called. */
427extern Elf_Data *elf_getdata_rawchunk (Elf *__elf,
428 int64_t __offset, size_t __size,
429 Elf_Type __type);
430
431
432/* Return pointer to string at OFFSET in section INDEX. */
433extern char *elf_strptr (Elf *__elf, size_t __index, size_t __offset);
434
435
436/* Return header of archive. */
437extern Elf_Arhdr *elf_getarhdr (Elf *__elf);
438
439/* Return offset in archive for current file ELF. */
440extern int64_t elf_getaroff (Elf *__elf);
441
442/* Select archive element at OFFSET. */
443extern size_t elf_rand (Elf *__elf, size_t __offset);
444
445/* Get symbol table of archive. */
446extern Elf_Arsym *elf_getarsym (Elf *__elf, size_t *__narsyms);
447
448
449/* Control ELF descriptor. */
450extern int elf_cntl (Elf *__elf, Elf_Cmd __cmd);
451
452/* Retrieve uninterpreted file contents. */
453extern char *elf_rawfile (Elf *__elf, size_t *__nbytes);
454
455
456/* Return size of array of COUNT elements of the type denoted by TYPE
457 in the external representation. The binary class is taken from ELF.
458 The result is based on version VERSION of the ELF standard. */
459extern size_t elf32_fsize (Elf_Type __type, size_t __count,
460 unsigned int __version)
461 __const_attribute__;
462/* Similar but this time the binary calls is ELFCLASS64. */
463extern size_t elf64_fsize (Elf_Type __type, size_t __count,
464 unsigned int __version)
465 __const_attribute__;
466
467
468/* Convert data structure from the representation in the file represented
469 by ELF to their memory representation. */
470extern Elf_Data *elf32_xlatetom (Elf_Data *__dest, const Elf_Data *__src,
471 unsigned int __encode);
472/* Same for 64 bit class. */
473extern Elf_Data *elf64_xlatetom (Elf_Data *__dest, const Elf_Data *__src,
474 unsigned int __encode);
475
476/* Convert data structure from to the representation in memory
477 represented by ELF file representation. */
478extern Elf_Data *elf32_xlatetof (Elf_Data *__dest, const Elf_Data *__src,
479 unsigned int __encode);
480/* Same for 64 bit class. */
481extern Elf_Data *elf64_xlatetof (Elf_Data *__dest, const Elf_Data *__src,
482 unsigned int __encode);
483
484
485/* Return error code of last failing function call. This value is kept
486 separately for each thread. */
487extern int elf_errno (void);
488
489/* Return error string for ERROR. If ERROR is zero, return error string
490 for most recent error or NULL is none occurred. If ERROR is -1 the
491 behaviour is similar to the last case except that not NULL but a legal
492 string is returned. */
493extern const char *elf_errmsg (int __error);
494
495
496/* Coordinate ELF library and application versions. */
497extern unsigned int elf_version (unsigned int __version);
498
499/* Set fill bytes used to fill holes in data structures. */
500extern void elf_fill (int __fill);
501
502/* Compute hash value. */
503extern unsigned long int elf_hash (const char *__string)
504 __pure_attribute__;
505
506/* Compute hash value using the GNU-specific hash function. */
507extern unsigned long int elf_gnu_hash (const char *__string)
508 __pure_attribute__;
509
510
511/* Compute simple checksum from permanent parts of the ELF file. */
512extern long int elf32_checksum (Elf *__elf);
513/* Similar but this time the binary calls is ELFCLASS64. */
514extern long int elf64_checksum (Elf *__elf);
515
516#ifdef __cplusplus
517}
518#endif
519
520#endif /* libelf.h */
521

Warning: That file was not part of the compilation database. It may have many parsing errors.