1 /* Internal interfaces for libelf.
2 Copyright (C) 1998-2010, 2015, 2016 Red Hat, Inc.
3 This file is part of elfutils.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 1998.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
44 /* Helper Macros to write 32 bit and 64 bit functions. */
45 #define __elfw2_(Bits, Name) __elf##Bits##_##Name
46 #define elfw2_(Bits, Name) elf##Bits##_##Name
47 #define ElfW2_(Bits, Name) Elf##Bits##_##Name
48 #define ELFW2_(Bits, Name) ELF##Bits##_##Name
49 #define ELFW_(Name, Bits) Name##Bits
50 #define __elfw2(Bits, Name) __elfw2_(Bits, Name)
51 #define elfw2(Bits, Name) elfw2_(Bits, Name)
52 #define ElfW2(Bits, Name) ElfW2_(Bits, Name)
53 #define ELFW2(Bits, Name) ELFW2_(Bits, Name)
54 #define ELFW(Name, Bits) ELFW_(Name, Bits)
57 /* Sizes of the external types, for 32 bits objects. */
58 #define ELF32_FSZ_ADDR 4
59 #define ELF32_FSZ_OFF 4
60 #define ELF32_FSZ_HALF 2
61 #define ELF32_FSZ_WORD 4
62 #define ELF32_FSZ_SWORD 4
63 #define ELF32_FSZ_XWORD 8
64 #define ELF32_FSZ_SXWORD 8
66 /* Same for 64 bits objects. */
67 #define ELF64_FSZ_ADDR 8
68 #define ELF64_FSZ_OFF 8
69 #define ELF64_FSZ_HALF 2
70 #define ELF64_FSZ_WORD 4
71 #define ELF64_FSZ_SWORD 4
72 #define ELF64_FSZ_XWORD 8
73 #define ELF64_FSZ_SXWORD 8
76 /* This is an extension of the ELF_F_* enumeration. The values here are
77 not part of the library interface, they are only used internally. */
81 ELF_F_MALLOCED = 0x80,
82 ELF_F_FILEDATA = 0x100
86 /* Get definition of all the external types. */
95 ELF_E_UNKNOWN_VERSION,
100 ELF_E_INVALID_ENCODING,
109 ELF_E_INVALID_ARCHIVE,
116 ELF_E_INVALID_OPERAND,
117 ELF_E_INVALID_SECTION,
118 ELF_E_INVALID_COMMAND,
119 ELF_E_WRONG_ORDER_EHDR,
123 ELF_E_NOT_NUL_SECTION,
125 ELF_E_INVALID_SECTION_HEADER,
128 ELF_E_SECTION_TOO_SMALL,
130 ELF_E_INVALID_SHENTSIZE,
136 ELF_E_INVALID_OFFSET,
137 ELF_E_INVALID_SECTION_TYPE,
138 ELF_E_INVALID_SECTION_FLAGS,
139 ELF_E_NOT_COMPRESSED,
140 ELF_E_ALREADY_COMPRESSED,
141 ELF_E_UNKNOWN_COMPRESSION_TYPE,
142 ELF_E_COMPRESS_ERROR,
143 ELF_E_DECOMPRESS_ERROR,
144 /* Keep this as the last entry. */
149 /* The visible `Elf_Data' type is not sufficient for some operations due
150 to a misdesigned interface. Extend it for internal purposes. */
158 /* List of `Elf_Data' descriptors. This is what makes up the section
160 typedef struct Elf_Data_List
162 /* `data' *must* be the first element in the struct. */
164 struct Elf_Data_List *next;
169 /* Descriptor for ELF section. */
172 /* We have to distinguish several different situations:
174 1. the section is user created. Therefore there is no file or memory
175 region to read the data from. Here we have two different subcases:
177 a) data was not yet added (before the first `elf_newdata' call)
179 b) at least one data set is available
181 2. this is a section from a file/memory region. We have to read the
182 current content in one data block if we have to. But we don't
183 read the data until it is necessary. So we have the subcases:
185 a) the section in the file has size zero (for whatever reason)
187 b) the data of the file is not (yet) read
189 c) the data is read and available.
191 In addition to this we have different data sets, the raw and the converted
192 data. This distinction only exists for the data read from the file.
193 All user-added data set (all but the first when read from the file or
194 all of them for user-create sections) are the same in both formats.
195 We don't create the converted data before it is necessary.
197 The `data_read' element signals whether data is available in the
200 If there is data from the file/memory region or if read one data
201 set is added the `rawdata_list_read' pointer in non-NULL and points
202 to the last filled data set. `raw_datalist_rear' is therefore NULL
203 only if there is no data set at all.
205 This so far allows to distinguish all but two cases (given that the
206 `rawdata_list' and `data_list' entries are initialized to zero) is
207 between not yet loaded data from the file/memory region and a section
208 with zero size and type ELF_T_BYTE. */
209 Elf_Data_List data_list; /* List of data buffers. */
210 Elf_Data_List *data_list_rear; /* Pointer to the rear of the data list. */
212 Elf_Data_Scn rawdata; /* Uninterpreted data of the section. */
214 int data_read; /* Nonzero if the section was created by the
215 user or if the data from the file/memory
217 int shndx_index; /* Index of the extended section index
218 table for this symbol table (if this
219 section is a symbol table). */
221 size_t index; /* Index of this section. */
222 struct Elf *elf; /* The underlying ELF file. */
226 Elf32_Shdr *e32; /* Pointer to 32bit section header. */
227 Elf64_Shdr *e64; /* Pointer to 64bit section header. */
230 unsigned int shdr_flags; /* Section header modified? */
231 unsigned int flags; /* Section changed in size?
232 ELF_F_MALLOCED for a Elf_Data_Chunk
233 dummy_scn means the rawchunks
234 data.d.d_buf was malloced. For normal
235 sections it means rawdata_base was
236 malloced (by elf_compress) even if
237 the Elf was mmapped. */
239 char *rawdata_base; /* The unmodified data of the section. */
240 char *data_base; /* The converted data of the section. */
242 char *zdata_base; /* The uncompressed data of the section. */
243 size_t zdata_size; /* If zdata_base != NULL, the size of data. */
244 size_t zdata_align; /* If zdata_base != NULL, the addralign. */
246 struct Elf_ScnList *list; /* Pointer to the section list element the
251 /* List of section. */
252 typedef struct Elf_ScnList
254 unsigned int cnt; /* Number of elements of 'data' used. */
255 unsigned int max; /* Number of elements of 'data' allocated. */
256 struct Elf_ScnList *next; /* Next block of sections. */
257 struct Elf_Scn data[0]; /* Section data. */
261 /* elf_getdata_rawchunk result. */
262 typedef struct Elf_Data_Chunk
268 struct Elf_Data_Chunk *next;
270 int64_t offset; /* The original raw offset in the Elf image. */
274 /* The ELF descriptor. */
277 /* Address to which the file was mapped. NULL if not mapped. */
280 /* When created for an archive member this points to the descriptor
283 Elf *next; /* Used in list of archive descriptors. */
285 /* What kind of file is underneath (ELF file, archive...). */
288 /* Command used to create this descriptor. */
291 /* The binary class. */
294 /* The used file descriptor. -1 if not available anymore. */
297 /* Offset in the archive this file starts or zero. */
298 int64_t start_offset;
300 /* Size of the file in the archive or the entire file size, or ~0
301 for an (yet) unknown size. */
304 /* Describes the way the memory was allocated and if the dirty bit is
305 signalled it means that the whole file has to be rewritten since
306 the layout changed. */
309 /* Reference counting for the descriptor. */
312 /* Lock to handle multithreaded programs. */
313 rwlock_define (,lock);
319 /* The next fields are only useful when testing for ==/!= NULL. */
324 Elf_ScnList *scns_last; /* Last element in the section list.
325 If NULL the data has not yet been
326 read from the file. */
327 Elf_Data_Chunk *rawchunks; /* List of elf_getdata_rawchunk results. */
328 unsigned int scnincr; /* Number of sections allocate the last
330 int ehdr_flags; /* Flags (dirty) for ELF header. */
331 int phdr_flags; /* Flags (dirty|malloc) for program header. */
332 int shdr_malloced; /* Nonzero if shdr array was allocated. */
333 off_t sizestr_offset; /* Offset of the size string in the parent
334 if this is an archive member. */
339 Elf32_Ehdr *ehdr; /* Pointer to the ELF header. This is
341 Elf32_Shdr *shdr; /* Used when reading from a file. */
342 Elf32_Phdr *phdr; /* Pointer to the program header array. */
343 Elf_ScnList *scns_last; /* Last element in the section list.
344 If NULL the data has not yet been
345 read from the file. */
346 Elf_Data_Chunk *rawchunks; /* List of elf_getdata_rawchunk results. */
347 unsigned int scnincr; /* Number of sections allocate the last
349 int ehdr_flags; /* Flags (dirty) for ELF header. */
350 int phdr_flags; /* Flags (dirty|malloc) for program header. */
351 int shdr_malloced; /* Nonzero if shdr array was allocated. */
352 int64_t sizestr_offset; /* Offset of the size string in the parent
353 if this is an archive member. */
354 Elf32_Ehdr ehdr_mem; /* Memory used for ELF header when not
356 char __e32scnspad[sizeof (Elf64_Ehdr) - sizeof (Elf32_Ehdr)];
358 /* The section array. */
364 Elf64_Ehdr *ehdr; /* Pointer to the ELF header. This is
366 Elf64_Shdr *shdr; /* Used when reading from a file. */
367 Elf64_Phdr *phdr; /* Pointer to the program header array. */
368 Elf_ScnList *scns_last; /* Last element in the section list.
369 If NULL the data has not yet been
370 read from the file. */
371 Elf_Data_Chunk *rawchunks; /* List of elf_getdata_rawchunk results. */
372 unsigned int scnincr; /* Number of sections allocate the last
374 int ehdr_flags; /* Flags (dirty) for ELF header. */
375 int phdr_flags; /* Flags (dirty|malloc) for program header. */
376 int shdr_malloced; /* Nonzero if shdr array was allocated. */
377 int64_t sizestr_offset; /* Offset of the size string in the parent
378 if this is an archive member. */
379 Elf64_Ehdr ehdr_mem; /* Memory used for ELF header when not
382 /* The section array. */
388 Elf *children; /* List of all descriptors for this archive. */
389 Elf_Arsym *ar_sym; /* Symbol table returned by elf_getarsym. */
390 size_t ar_sym_num; /* Number of entries in `ar_sym'. */
391 char *long_names; /* If no index is available but long names
392 are used this elements points to the data.*/
393 size_t long_names_len; /* Length of the long name table. */
394 int64_t offset; /* Offset in file we are currently at.
395 elf_next() advances this to the next
396 member of the archive. */
397 Elf_Arhdr elf_ar_hdr; /* Structure returned by 'elf_getarhdr'. */
398 struct ar_hdr ar_hdr; /* Header read from file. */
399 char ar_name[16]; /* NUL terminated ar_name of elf_ar_hdr. */
400 char raw_name[17]; /* This is a buffer for the NUL terminated
401 named raw_name used in the elf_ar_hdr. */
405 /* There absolutely never must be anything following the union. */
408 /* Type of the conversion functions. These functions will convert the
410 typedef void (*xfct_t) (void *, const void *, size_t, int);
412 /* The table with the function pointers. */
413 extern const xfct_t __elf_xfctstom[ELFCLASSNUM - 1][ELF_T_NUM]
417 /* Array with sizes of the external types indexed by ELF version, binary
419 extern const size_t __libelf_type_sizes[ELFCLASSNUM - 1][ELF_T_NUM]
421 /* We often have to access the size for a type in the current version. */
422 # define elf_typesize(class,type,n) \
423 (__libelf_type_sizes[ELFW(ELFCLASS,class) - 1][type] * n)
425 /* The byte value used for filling gaps. */
426 extern int __libelf_fill_byte attribute_hidden;
428 /* EV_CURRENT if the version was set, EV_NONE otherwise. */
429 extern unsigned int __libelf_version attribute_hidden;
431 /* Array with alignment requirements of the internal types indexed by
432 binary class, and type. */
433 extern const uint_fast8_t __libelf_type_aligns[ELFCLASSNUM - 1][ELF_T_NUM]
435 # define __libelf_type_align(class, type) \
436 (__libelf_type_aligns[class - 1][type] ?: 1)
438 /* Given an GElf_Ehdr handle and a section type returns the Elf_Data d_type.
439 Should not be called when SHF_COMPRESSED is set, the d_type should
441 extern Elf_Type __libelf_data_type (GElf_Ehdr *ehdr,
442 int sh_type, GElf_Xword align)
446 /* Create Elf descriptor from memory image. */
447 extern Elf *__libelf_read_mmaped_file (int fildes, void *map_address,
448 int64_t offset, size_t maxsize,
449 Elf_Cmd cmd, Elf *parent)
452 /* Set error value. */
453 extern void __libelf_seterrno (int value) internal_function;
455 /* Get the next archive header. */
456 extern int __libelf_next_arhdr_wrlock (Elf *elf) internal_function;
458 /* Read all of the file associated with the descriptor. */
459 extern char *__libelf_readall (Elf *elf) internal_function;
461 /* Read the complete section table and convert the byte order if necessary. */
462 extern int __libelf_readsections (Elf *elf) internal_function;
464 /* Store the information for the raw data in the `rawdata_list' element. */
465 extern int __libelf_set_rawdata (Elf_Scn *scn) internal_function;
466 extern int __libelf_set_rawdata_wrlock (Elf_Scn *scn) internal_function;
469 /* Helper functions for elf_update. */
470 extern int64_t __elf32_updatenull_wrlock (Elf *elf, int *change_bop,
471 size_t shnum) internal_function;
472 extern int64_t __elf64_updatenull_wrlock (Elf *elf, int *change_bop,
473 size_t shnum) internal_function;
475 extern int __elf32_updatemmap (Elf *elf, int change_bo, size_t shnum)
477 extern int __elf64_updatemmap (Elf *elf, int change_bo, size_t shnum)
479 extern int __elf32_updatefile (Elf *elf, int change_bo, size_t shnum)
481 extern int __elf64_updatefile (Elf *elf, int change_bo, size_t shnum)
485 /* Alias for exported functions to avoid PLT entries, and
486 rdlock/wrlock variants of these functions. */
487 extern int __elf_end_internal (Elf *__elf) attribute_hidden;
488 extern Elf *__elf_begin_internal (int __fildes, Elf_Cmd __cmd, Elf *__ref)
490 extern Elf32_Ehdr *__elf32_getehdr_wrlock (Elf *__elf) internal_function;
491 extern Elf64_Ehdr *__elf64_getehdr_wrlock (Elf *__elf) internal_function;
492 extern Elf32_Ehdr *__elf32_newehdr_internal (Elf *__elf) attribute_hidden;
493 extern Elf64_Ehdr *__elf64_newehdr_internal (Elf *__elf) attribute_hidden;
494 extern Elf32_Phdr *__elf32_getphdr_internal (Elf *__elf) attribute_hidden;
495 extern Elf64_Phdr *__elf64_getphdr_internal (Elf *__elf) attribute_hidden;
496 extern Elf32_Phdr *__elf32_getphdr_wrlock (Elf *__elf) attribute_hidden;
497 extern Elf64_Phdr *__elf64_getphdr_wrlock (Elf *__elf) attribute_hidden;
498 extern Elf32_Phdr *__elf32_newphdr_internal (Elf *__elf, size_t __cnt)
500 extern Elf64_Phdr *__elf64_newphdr_internal (Elf *__elf, size_t __cnt)
502 extern Elf_Scn *__elf32_offscn_internal (Elf *__elf, Elf32_Off __offset)
504 extern Elf_Scn *__elf64_offscn_internal (Elf *__elf, Elf64_Off __offset)
506 extern int __elf_getphdrnum_rdlock (Elf *__elf, size_t *__dst)
508 extern int __elf_getphdrnum_chk_rdlock (Elf *__elf, size_t *__dst)
510 extern int __elf_getshdrnum_rdlock (Elf *__elf, size_t *__dst)
512 extern int __elf_getshdrstrndx_internal (Elf *__elf, size_t *__dst)
514 extern Elf32_Shdr *__elf32_getshdr_rdlock (Elf_Scn *__scn) internal_function;
515 extern Elf64_Shdr *__elf64_getshdr_rdlock (Elf_Scn *__scn) internal_function;
516 extern Elf32_Shdr *__elf32_getshdr_wrlock (Elf_Scn *__scn) internal_function;
517 extern Elf64_Shdr *__elf64_getshdr_wrlock (Elf_Scn *__scn) internal_function;
518 extern Elf_Scn *__elf_getscn_internal (Elf *__elf, size_t __index)
520 extern Elf_Scn *__elf_nextscn_internal (Elf *__elf, Elf_Scn *__scn)
522 extern int __elf_scnshndx_internal (Elf_Scn *__scn) attribute_hidden;
523 extern Elf_Data *__elf_getdata_internal (Elf_Scn *__scn, Elf_Data *__data)
525 extern Elf_Data *__elf_getdata_rdlock (Elf_Scn *__scn, Elf_Data *__data)
527 extern Elf_Data *__elf_rawdata_internal (Elf_Scn *__scn, Elf_Data *__data)
529 /* Should be called to setup first section data element if
530 data_list_rear is NULL and we know data_read is set and there is
531 raw data available. Might upgrade the ELF lock from a read to a
532 write lock. If the lock is already a write lock set wrlocked. */
533 extern void __libelf_set_data_list_rdlock (Elf_Scn *scn, int wrlocked)
535 extern char *__elf_strptr_internal (Elf *__elf, size_t __index,
536 size_t __offset) attribute_hidden;
537 extern Elf_Data *__elf32_xlatetom_internal (Elf_Data *__dest,
538 const Elf_Data *__src,
539 unsigned int __encode)
541 extern Elf_Data *__elf64_xlatetom_internal (Elf_Data *__dest,
542 const Elf_Data *__src,
543 unsigned int __encode)
545 extern Elf_Data *__elf32_xlatetof_internal (Elf_Data *__dest,
546 const Elf_Data *__src,
547 unsigned int __encode)
549 extern Elf_Data *__elf64_xlatetof_internal (Elf_Data *__dest,
550 const Elf_Data *__src,
551 unsigned int __encode)
553 extern unsigned int __elf_version_internal (unsigned int __version)
555 extern unsigned long int __elf_hash_internal (const char *__string)
556 __attribute__ ((__pure__)) attribute_hidden;
557 extern long int __elf32_checksum_internal (Elf *__elf) attribute_hidden;
558 extern long int __elf64_checksum_internal (Elf *__elf) attribute_hidden;
561 extern GElf_Ehdr *__gelf_getehdr_rdlock (Elf *__elf, GElf_Ehdr *__dest)
563 extern size_t __gelf_fsize_internal (Elf *__elf, Elf_Type __type,
564 size_t __count, unsigned int __version)
566 extern GElf_Shdr *__gelf_getshdr_internal (Elf_Scn *__scn, GElf_Shdr *__dst)
568 extern GElf_Sym *__gelf_getsym_internal (Elf_Data *__data, int __ndx,
569 GElf_Sym *__dst) attribute_hidden;
572 extern uint32_t __libelf_crc32 (uint32_t crc, unsigned char *buf, size_t len)
575 extern void * __libelf_compress (Elf_Scn *scn, size_t hsize, int ei_data,
576 size_t *orig_size, size_t *orig_addralign,
577 size_t *size, bool force, bool use_zstd)
580 extern void * __libelf_decompress (int chtype, void *buf_in, size_t size_in,
581 size_t size_out) internal_function;
582 extern void * __libelf_decompress_elf (Elf_Scn *scn,
583 size_t *size_out, size_t *addralign)
587 extern void __libelf_reset_rawdata (Elf_Scn *scn, void *buf, size_t size,
588 size_t align, Elf_Type type)
592 /* We often have to update a flag iff a value changed. Make this
594 #define update_if_changed(var, exp, flag) \
596 __typeof__ (var) *_var = &(var); \
597 __typeof__ (exp) _exp = (exp); \
601 (flag) |= ELF_F_DIRTY; \
605 /* Align offset to 4 bytes as needed for note name and descriptor data.
606 This is almost always used, except for GNU Property notes, which use
608 #define NOTE_ALIGN4(n) (((n) + 3) & -4UL)
610 /* Special note padding rule for GNU Property notes. */
611 #define NOTE_ALIGN8(n) (((n) + 7) & -8UL)
613 /* Convenience macro. */
614 #define INVALID_NDX(ndx, type, data) \
615 unlikely ((data)->d_size / sizeof (type) <= (unsigned int) (ndx))
617 #endif /* libelfP.h */