• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
5 
6    This file is free software; you can redistribute it and/or modify
7    it under the terms of either
8 
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
12 
13    or
14 
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
18 
19    or both in parallel, as here.
20 
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.
25 
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/>.  */
29 
30 #ifndef _LIBELFP_H
31 #define _LIBELFP_H 1
32 
33 #include <ar.h>
34 #include <gelf.h>
35 
36 #include <errno.h>
37 #include <stdbool.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <string.h>
41 
42 
43 /* Helper Macros to write 32 bit and 64 bit functions.  */
44 #define __elfw2_(Bits, Name) __elf##Bits##_##Name
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 ELFW_(Name, Bits) Name##Bits
49 #define __elfw2(Bits, Name) __elfw2_(Bits, Name)
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 ELFW(Name, Bits)  ELFW_(Name, Bits)
54 
55 
56 /* Sizes of the external types, for 32 bits objects.  */
57 #define ELF32_FSZ_ADDR   4
58 #define ELF32_FSZ_OFF    4
59 #define ELF32_FSZ_HALF   2
60 #define ELF32_FSZ_WORD   4
61 #define ELF32_FSZ_SWORD  4
62 #define ELF32_FSZ_XWORD  8
63 #define ELF32_FSZ_SXWORD 8
64 
65 /* Same for 64 bits objects.  */
66 #define ELF64_FSZ_ADDR   8
67 #define ELF64_FSZ_OFF    8
68 #define ELF64_FSZ_HALF   2
69 #define ELF64_FSZ_WORD   4
70 #define ELF64_FSZ_SWORD  4
71 #define ELF64_FSZ_XWORD  8
72 #define ELF64_FSZ_SXWORD 8
73 
74 
75 /* This is an extension of the ELF_F_* enumeration.  The values here are
76    not part of the library interface, they are only used internally.  */
77 enum
78 {
79   ELF_F_MMAPPED = 0x40,
80   ELF_F_MALLOCED = 0x80,
81   ELF_F_FILEDATA = 0x100
82 };
83 
84 
85 /* Get definition of all the external types.  */
86 #include "exttypes.h"
87 
88 
89 /* Error values.  */
90 enum
91 {
92   ELF_E_NOERROR = 0,
93   ELF_E_UNKNOWN_ERROR,
94   ELF_E_UNKNOWN_VERSION,
95   ELF_E_UNKNOWN_TYPE,
96   ELF_E_INVALID_HANDLE,
97   ELF_E_SOURCE_SIZE,
98   ELF_E_DEST_SIZE,
99   ELF_E_INVALID_ENCODING,
100   ELF_E_NOMEM,
101   ELF_E_INVALID_FILE,
102   ELF_E_INVALID_ELF,
103   ELF_E_INVALID_OP,
104   ELF_E_NO_VERSION,
105   ELF_E_INVALID_CMD,
106   ELF_E_RANGE,
107   ELF_E_ARCHIVE_FMAG,
108   ELF_E_INVALID_ARCHIVE,
109   ELF_E_NO_ARCHIVE,
110   ELF_E_NO_INDEX,
111   ELF_E_READ_ERROR,
112   ELF_E_WRITE_ERROR,
113   ELF_E_INVALID_CLASS,
114   ELF_E_INVALID_INDEX,
115   ELF_E_INVALID_OPERAND,
116   ELF_E_INVALID_SECTION,
117   ELF_E_INVALID_COMMAND,
118   ELF_E_WRONG_ORDER_EHDR,
119   ELF_E_FD_DISABLED,
120   ELF_E_FD_MISMATCH,
121   ELF_E_OFFSET_RANGE,
122   ELF_E_NOT_NUL_SECTION,
123   ELF_E_DATA_MISMATCH,
124   ELF_E_INVALID_SECTION_HEADER,
125   ELF_E_INVALID_DATA,
126   ELF_E_DATA_ENCODING,
127   ELF_E_SECTION_TOO_SMALL,
128   ELF_E_INVALID_ALIGN,
129   ELF_E_INVALID_SHENTSIZE,
130   ELF_E_UPDATE_RO,
131   ELF_E_NOFILE,
132   ELF_E_GROUP_NOT_REL,
133   ELF_E_INVALID_PHDR,
134   ELF_E_NO_PHDR,
135   ELF_E_INVALID_OFFSET,
136   ELF_E_INVALID_SECTION_TYPE,
137   ELF_E_INVALID_SECTION_FLAGS,
138   ELF_E_NOT_COMPRESSED,
139   ELF_E_ALREADY_COMPRESSED,
140   ELF_E_UNKNOWN_COMPRESSION_TYPE,
141   ELF_E_COMPRESS_ERROR,
142   ELF_E_DECOMPRESS_ERROR,
143   /* Keep this as the last entry.  */
144   ELF_E_NUM
145 };
146 
147 
148 /* The visible `Elf_Data' type is not sufficient for some operations due
149    to a misdesigned interface.  Extend it for internal purposes.  */
150 typedef struct
151 {
152   Elf_Data d;
153   Elf_Scn *s;
154 } Elf_Data_Scn;
155 
156 
157 /* List of `Elf_Data' descriptors.  This is what makes up the section
158    contents.  */
159 typedef struct Elf_Data_List
160 {
161   /* `data' *must* be the first element in the struct.  */
162   Elf_Data_Scn data;
163   struct Elf_Data_List *next;
164   int flags;
165 } Elf_Data_List;
166 
167 
168 /* Descriptor for ELF section.  */
169 struct Elf_Scn
170 {
171   /* We have to distinguish several different situations:
172 
173      1. the section is user created.  Therefore there is no file or memory
174         region to read the data from.  Here we have two different subcases:
175 
176         a) data was not yet added (before the first `elf_newdata' call)
177 
178         b) at least one data set is available
179 
180      2. this is a section from a file/memory region.  We have to read the
181         current content in one data block if we have to.  But we don't
182         read the data until it is necessary.  So we have the subcases:
183 
184         a) the section in the file has size zero (for whatever reason)
185 
186         b) the data of the file is not (yet) read
187 
188         c) the data is read and available.
189 
190      In addition to this we have different data sets, the raw and the converted
191      data.  This distinction only exists for the data read from the file.
192      All user-added data set (all but the first when read from the file or
193      all of them for user-create sections) are the same in both formats.
194      We don't create the converted data before it is necessary.
195 
196      The `data_read' element signals whether data is available in the
197      raw format.
198 
199      If there is data from the file/memory region or if read one data
200      set is added the `rawdata_list_read' pointer in non-NULL and points
201      to the last filled data set.  `raw_datalist_rear' is therefore NULL
202      only if there is no data set at all.
203 
204      This so far allows to distinguish all but two cases (given that the
205      `rawdata_list' and `data_list' entries are initialized to zero) is
206      between not yet loaded data from the file/memory region and a section
207      with zero size and type ELF_T_BYTE.   */
208   Elf_Data_List data_list;	/* List of data buffers.  */
209   Elf_Data_List *data_list_rear; /* Pointer to the rear of the data list. */
210 
211   Elf_Data_Scn rawdata;		/* Uninterpreted data of the section.  */
212 
213   int data_read;		/* Nonzero if the section was created by the
214 				   user or if the data from the file/memory
215 				   is read.  */
216   int shndx_index;		/* Index of the extended section index
217 				   table for this symbol table (if this
218 				   section is a symbol table).  */
219 
220   size_t index;			/* Index of this section.  */
221   struct Elf *elf;		/* The underlying ELF file.  */
222 
223   union
224   {
225     Elf32_Shdr *e32;		/* Pointer to 32bit section header.  */
226     Elf64_Shdr *e64;		/* Pointer to 64bit section header.  */
227   } shdr;
228 
229   unsigned int shdr_flags;	/* Section header modified?  */
230   unsigned int flags;		/* Section changed in size?
231 				   ELF_F_MALLOCED for a Elf_Data_Chunk
232 				   dummy_scn means the rawchunks
233 				   data.d.d_buf was malloced. For normal
234 				   sections it means rawdata_base was
235 				   malloced (by elf_compress) even if
236 				   the Elf was mmapped.  */
237 
238   char *rawdata_base;		/* The unmodified data of the section.  */
239   char *data_base;		/* The converted data of the section.  */
240 
241   char *zdata_base;		/* The uncompressed data of the section.  */
242   size_t zdata_size;		/* If zdata_base != NULL, the size of data.  */
243   size_t zdata_align;		/* If zdata_base != NULL, the addralign.  */
244 
245   struct Elf_ScnList *list;	/* Pointer to the section list element the
246 				   data is in.  */
247 };
248 
249 
250 /* List of section.  */
251 typedef struct Elf_ScnList
252 {
253   unsigned int cnt;		/* Number of elements of 'data' used.  */
254   unsigned int max;		/* Number of elements of 'data' allocated.  */
255   struct Elf_ScnList *next;	/* Next block of sections.  */
256   struct Elf_Scn data[0];	/* Section data.  */
257 } Elf_ScnList;
258 
259 
260 /* elf_getdata_rawchunk result.  */
261 typedef struct Elf_Data_Chunk
262 {
263   Elf_Data_Scn data;
264   union
265   {
266     Elf_Scn dummy_scn;
267     struct Elf_Data_Chunk *next;
268   };
269 } Elf_Data_Chunk;
270 
271 
272 /* The ELF descriptor.  */
273 struct Elf
274 {
275   /* Address to which the file was mapped.  NULL if not mapped.  */
276   void *map_address;
277 
278   /* When created for an archive member this points to the descriptor
279      for the archive. */
280   Elf *parent;
281   Elf *next;             /* Used in list of archive descriptors.  */
282 
283   /* What kind of file is underneath (ELF file, archive...).  */
284   Elf_Kind kind;
285 
286   /* Command used to create this descriptor.  */
287   Elf_Cmd cmd;
288 
289   /* The binary class.  */
290   unsigned int class;
291 
292   /* The used file descriptor.  -1 if not available anymore.  */
293   int fildes;
294 
295   /* Offset in the archive this file starts or zero.  */
296   int64_t start_offset;
297 
298   /* Size of the file in the archive or the entire file size, or ~0
299      for an (yet) unknown size.  */
300   size_t maximum_size;
301 
302   /* Describes the way the memory was allocated and if the dirty bit is
303      signalled it means that the whole file has to be rewritten since
304      the layout changed.  */
305   int flags;
306 
307   /* Reference counting for the descriptor.  */
308   int ref_count;
309 
310   /* Lock to handle multithreaded programs.  */
311   rwlock_define (,lock);
312 
313   union
314   {
315     struct
316     {
317       /* The next fields are only useful when testing for ==/!= NULL.  */
318       void *ehdr;
319       void *shdr;
320       void *phdr;
321 
322       Elf_ScnList *scns_last;	/* Last element in the section list.
323 				   If NULL the data has not yet been
324 				   read from the file.  */
325       Elf_Data_Chunk *rawchunks; /* List of elf_getdata_rawchunk results.  */
326       unsigned int scnincr;	/* Number of sections allocate the last
327 				   time.  */
328       int ehdr_flags;		/* Flags (dirty) for ELF header.  */
329       int phdr_flags;		/* Flags (dirty|malloc) for program header.  */
330       int shdr_malloced;	/* Nonzero if shdr array was allocated.  */
331       off_t sizestr_offset;	/* Offset of the size string in the parent
332 				   if this is an archive member.  */
333     } elf;
334 
335     struct
336     {
337       Elf32_Ehdr *ehdr;		/* Pointer to the ELF header.  This is
338 				   never malloced.  */
339       Elf32_Shdr *shdr;		/* Used when reading from a file.  */
340       Elf32_Phdr *phdr;		/* Pointer to the program header array.  */
341       Elf_ScnList *scns_last;	/* Last element in the section list.
342 				   If NULL the data has not yet been
343 				   read from the file.  */
344       Elf_Data_Chunk *rawchunks; /* List of elf_getdata_rawchunk results.  */
345       unsigned int scnincr;	/* Number of sections allocate the last
346 				   time.  */
347       int ehdr_flags;		/* Flags (dirty) for ELF header.  */
348       int phdr_flags;		/* Flags (dirty|malloc) for program header.  */
349       int shdr_malloced;	/* Nonzero if shdr array was allocated.  */
350       int64_t sizestr_offset;	/* Offset of the size string in the parent
351 				   if this is an archive member.  */
352       Elf32_Ehdr ehdr_mem;	/* Memory used for ELF header when not
353 				   mmaped.  */
354       char __e32scnspad[sizeof (Elf64_Ehdr) - sizeof (Elf32_Ehdr)];
355 
356       /* The section array.  */
357       Elf_ScnList scns;
358     } elf32;
359 
360     struct
361     {
362       Elf64_Ehdr *ehdr;		/* Pointer to the ELF header.  This is
363 				   never malloced.  */
364       Elf64_Shdr *shdr;		/* Used when reading from a file.  */
365       Elf64_Phdr *phdr;		/* Pointer to the program header array.  */
366       Elf_ScnList *scns_last;	/* Last element in the section list.
367 				   If NULL the data has not yet been
368 				   read from the file.  */
369       Elf_Data_Chunk *rawchunks; /* List of elf_getdata_rawchunk results.  */
370       unsigned int scnincr;	/* Number of sections allocate the last
371 				   time.  */
372       int ehdr_flags;		/* Flags (dirty) for ELF header.  */
373       int phdr_flags;		/* Flags (dirty|malloc) for program header.  */
374       int shdr_malloced;	/* Nonzero if shdr array was allocated.  */
375       int64_t sizestr_offset;	/* Offset of the size string in the parent
376 				   if this is an archive member.  */
377       Elf64_Ehdr ehdr_mem;	/* Memory used for ELF header when not
378 				   mmaped.  */
379 
380       /* The section array.  */
381       Elf_ScnList scns;
382     } elf64;
383 
384     struct
385     {
386       Elf *children;		/* List of all descriptors for this archive. */
387       Elf_Arsym *ar_sym;	/* Symbol table returned by elf_getarsym.  */
388       size_t ar_sym_num;	/* Number of entries in `ar_sym'.  */
389       char *long_names;		/* If no index is available but long names
390 				   are used this elements points to the data.*/
391       size_t long_names_len;	/* Length of the long name table.  */
392       int64_t offset;		/* Offset in file we are currently at.
393 				   elf_next() advances this to the next
394 				   member of the archive.  */
395       Elf_Arhdr elf_ar_hdr;	/* Structure returned by 'elf_getarhdr'.  */
396       struct ar_hdr ar_hdr;	/* Header read from file.  */
397       char ar_name[16];		/* NUL terminated ar_name of elf_ar_hdr.  */
398       char raw_name[17];	/* This is a buffer for the NUL terminated
399 				   named raw_name used in the elf_ar_hdr.  */
400     } ar;
401   } state;
402 
403   /* There absolutely never must be anything following the union.  */
404 };
405 
406 /* Type of the conversion functions.  These functions will convert the
407    byte order.  */
408 typedef void (*xfct_t) (void *, const void *, size_t, int);
409 
410 /* The table with the function pointers.  */
411 extern const xfct_t __elf_xfctstom[ELFCLASSNUM - 1][ELF_T_NUM]
412   attribute_hidden;
413 
414 
415 /* Array with sizes of the external types indexed by ELF version, binary
416    class, and type. */
417 extern const size_t __libelf_type_sizes[ELFCLASSNUM - 1][ELF_T_NUM]
418   attribute_hidden;
419 /* We often have to access the size for a type in the current version.  */
420 # define elf_typesize(class,type,n) \
421   (__libelf_type_sizes[ELFW(ELFCLASS,class) - 1][type] * n)
422 
423 /* The byte value used for filling gaps.  */
424 extern int __libelf_fill_byte attribute_hidden;
425 
426 /* EV_CURRENT if the version was set, EV_NONE otherwise.  */
427 extern unsigned int __libelf_version attribute_hidden;
428 
429 /* Array with alignment requirements of the internal types indexed by
430    binary class, and type. */
431 extern const uint_fast8_t __libelf_type_aligns[ELFCLASSNUM - 1][ELF_T_NUM]
432   attribute_hidden;
433 # define __libelf_type_align(class, type)	\
434     (__libelf_type_aligns[class - 1][type] ?: 1)
435 
436 /* Given an GElf_Ehdr handle and a section type returns the Elf_Data d_type.
437    Should not be called when SHF_COMPRESSED is set, the d_type should
438    be ELF_T_BYTE.  */
439 extern Elf_Type __libelf_data_type (GElf_Ehdr *ehdr,
440 				    int sh_type, GElf_Xword align)
441   internal_function;
442 
443 
444 /* Create Elf descriptor from memory image.  */
445 extern Elf *__libelf_read_mmaped_file (int fildes, void *map_address,
446 				       int64_t offset, size_t maxsize,
447 				       Elf_Cmd cmd, Elf *parent)
448      internal_function;
449 
450 /* Set error value.  */
451 extern void __libelf_seterrno (int value) internal_function;
452 
453 /* Get the next archive header.  */
454 extern int __libelf_next_arhdr_wrlock (Elf *elf) internal_function;
455 
456 /* Read all of the file associated with the descriptor.  */
457 extern char *__libelf_readall (Elf *elf) internal_function;
458 
459 /* Read the complete section table and convert the byte order if necessary.  */
460 extern int __libelf_readsections (Elf *elf) internal_function;
461 
462 /* Store the information for the raw data in the `rawdata_list' element.  */
463 extern int __libelf_set_rawdata (Elf_Scn *scn) internal_function;
464 extern int __libelf_set_rawdata_wrlock (Elf_Scn *scn) internal_function;
465 
466 
467 /* Helper functions for elf_update.  */
468 extern int64_t __elf32_updatenull_wrlock (Elf *elf, int *change_bop,
469 					  size_t shnum) internal_function;
470 extern int64_t __elf64_updatenull_wrlock (Elf *elf, int *change_bop,
471 					  size_t shnum) internal_function;
472 
473 extern int __elf32_updatemmap (Elf *elf, int change_bo, size_t shnum)
474      internal_function;
475 extern int __elf64_updatemmap (Elf *elf, int change_bo, size_t shnum)
476      internal_function;
477 extern int __elf32_updatefile (Elf *elf, int change_bo, size_t shnum)
478      internal_function;
479 extern int __elf64_updatefile (Elf *elf, int change_bo, size_t shnum)
480      internal_function;
481 
482 
483 /* Alias for exported functions to avoid PLT entries, and
484    rdlock/wrlock variants of these functions.  */
485 extern int __elf_end_internal (Elf *__elf) attribute_hidden;
486 extern Elf *__elf_begin_internal (int __fildes, Elf_Cmd __cmd, Elf *__ref)
487      attribute_hidden;
488 extern Elf32_Ehdr *__elf32_getehdr_wrlock (Elf *__elf) internal_function;
489 extern Elf64_Ehdr *__elf64_getehdr_wrlock (Elf *__elf) internal_function;
490 extern Elf32_Ehdr *__elf32_newehdr_internal (Elf *__elf) attribute_hidden;
491 extern Elf64_Ehdr *__elf64_newehdr_internal (Elf *__elf) attribute_hidden;
492 extern Elf32_Phdr *__elf32_getphdr_internal (Elf *__elf) attribute_hidden;
493 extern Elf64_Phdr *__elf64_getphdr_internal (Elf *__elf) attribute_hidden;
494 extern Elf32_Phdr *__elf32_getphdr_wrlock (Elf *__elf) attribute_hidden;
495 extern Elf64_Phdr *__elf64_getphdr_wrlock (Elf *__elf) attribute_hidden;
496 extern Elf32_Phdr *__elf32_newphdr_internal (Elf *__elf, size_t __cnt)
497      attribute_hidden;
498 extern Elf64_Phdr *__elf64_newphdr_internal (Elf *__elf, size_t __cnt)
499      attribute_hidden;
500 extern Elf_Scn *__elf32_offscn_internal (Elf *__elf, Elf32_Off __offset)
501      attribute_hidden;
502 extern Elf_Scn *__elf64_offscn_internal (Elf *__elf, Elf64_Off __offset)
503      attribute_hidden;
504 extern int __elf_getphdrnum_rdlock (Elf *__elf, size_t *__dst)
505      internal_function;
506 extern int __elf_getphdrnum_chk_rdlock (Elf *__elf, size_t *__dst)
507      internal_function;
508 extern int __elf_getshdrnum_rdlock (Elf *__elf, size_t *__dst)
509      internal_function;
510 extern int __elf_getshdrstrndx_internal (Elf *__elf, size_t *__dst)
511      attribute_hidden;
512 extern Elf32_Shdr *__elf32_getshdr_rdlock (Elf_Scn *__scn) internal_function;
513 extern Elf64_Shdr *__elf64_getshdr_rdlock (Elf_Scn *__scn) internal_function;
514 extern Elf32_Shdr *__elf32_getshdr_wrlock (Elf_Scn *__scn) internal_function;
515 extern Elf64_Shdr *__elf64_getshdr_wrlock (Elf_Scn *__scn) internal_function;
516 extern Elf_Scn *__elf_getscn_internal (Elf *__elf, size_t __index)
517      attribute_hidden;
518 extern Elf_Scn *__elf_nextscn_internal (Elf *__elf, Elf_Scn *__scn)
519      attribute_hidden;
520 extern int __elf_scnshndx_internal (Elf_Scn *__scn) attribute_hidden;
521 extern Elf_Data *__elf_getdata_internal (Elf_Scn *__scn, Elf_Data *__data)
522      attribute_hidden;
523 extern Elf_Data *__elf_getdata_rdlock (Elf_Scn *__scn, Elf_Data *__data)
524      internal_function;
525 extern Elf_Data *__elf_rawdata_internal (Elf_Scn *__scn, Elf_Data *__data)
526      attribute_hidden;
527 /* Should be called to setup first section data element if
528    data_list_rear is NULL and we know data_read is set and there is
529    raw data available.  Might upgrade the ELF lock from a read to a
530    write lock.  If the lock is already a write lock set wrlocked.  */
531 extern void __libelf_set_data_list_rdlock (Elf_Scn *scn, int wrlocked)
532      internal_function;
533 extern char *__elf_strptr_internal (Elf *__elf, size_t __index,
534 				    size_t __offset) attribute_hidden;
535 extern Elf_Data *__elf32_xlatetom_internal (Elf_Data *__dest,
536 					    const Elf_Data *__src,
537 					    unsigned int __encode)
538      attribute_hidden;
539 extern Elf_Data *__elf64_xlatetom_internal (Elf_Data *__dest,
540 					    const Elf_Data *__src,
541 					    unsigned int __encode)
542      attribute_hidden;
543 extern Elf_Data *__elf32_xlatetof_internal (Elf_Data *__dest,
544 					    const Elf_Data *__src,
545 					    unsigned int __encode)
546      attribute_hidden;
547 extern Elf_Data *__elf64_xlatetof_internal (Elf_Data *__dest,
548 					    const Elf_Data *__src,
549 					    unsigned int __encode)
550      attribute_hidden;
551 extern unsigned int __elf_version_internal (unsigned int __version)
552      attribute_hidden;
553 extern unsigned long int __elf_hash_internal (const char *__string)
554        __attribute__ ((__pure__)) attribute_hidden;
555 extern long int __elf32_checksum_internal (Elf *__elf) attribute_hidden;
556 extern long int __elf64_checksum_internal (Elf *__elf) attribute_hidden;
557 
558 
559 extern GElf_Ehdr *__gelf_getehdr_rdlock (Elf *__elf, GElf_Ehdr *__dest)
560      internal_function;
561 extern size_t __gelf_fsize_internal (Elf *__elf, Elf_Type __type,
562 				     size_t __count, unsigned int __version)
563      attribute_hidden;
564 extern GElf_Shdr *__gelf_getshdr_internal (Elf_Scn *__scn, GElf_Shdr *__dst)
565      attribute_hidden;
566 extern GElf_Sym *__gelf_getsym_internal (Elf_Data *__data, int __ndx,
567 					 GElf_Sym *__dst) attribute_hidden;
568 
569 
570 extern uint32_t __libelf_crc32 (uint32_t crc, unsigned char *buf, size_t len)
571      attribute_hidden;
572 
573 extern void * __libelf_compress (Elf_Scn *scn, size_t hsize, int ei_data,
574 				 size_t *orig_size, size_t *orig_addralign,
575 				 size_t *size, bool force)
576      internal_function;
577 
578 extern void * __libelf_decompress (void *buf_in, size_t size_in,
579 				   size_t size_out) internal_function;
580 extern void * __libelf_decompress_elf (Elf_Scn *scn,
581 				       size_t *size_out, size_t *addralign)
582      internal_function;
583 
584 
585 extern void __libelf_reset_rawdata (Elf_Scn *scn, void *buf, size_t size,
586 				    size_t align, Elf_Type type)
587      internal_function;
588 
589 
590 /* We often have to update a flag iff a value changed.  Make this
591    convenient.  */
592 #define update_if_changed(var, exp, flag) \
593   do {									      \
594     __typeof__ (var) *_var = &(var);					      \
595     __typeof__ (exp) _exp = (exp);					      \
596     if (*_var != _exp)							      \
597       {									      \
598 	*_var = _exp;							      \
599 	(flag) |= ELF_F_DIRTY;						      \
600       }									      \
601   } while (0)
602 
603 /* Align offset to 4 bytes as needed for note name and descriptor data.
604    This is almost always used, except for GNU Property notes, which use
605    8 byte padding...  */
606 #define NOTE_ALIGN4(n)	(((n) + 3) & -4U)
607 
608 /* Special note padding rule for GNU Property notes.  */
609 #define NOTE_ALIGN8(n)	(((n) + 7) & -8U)
610 
611 /* Convenience macro.  */
612 #define INVALID_NDX(ndx, type, data) \
613   unlikely ((data)->d_size / sizeof (type) <= (unsigned int) (ndx))
614 
615 #endif  /* libelfP.h */
616