tools/elf4rom/libs/libelf-0.8.10/lib/private.h
changeset 34 92d87f2e53c2
equal deleted inserted replaced
33:1af5c1be89f8 34:92d87f2e53c2
       
     1 /*
       
     2  * private.h - private definitions for libelf.
       
     3  * Copyright (C) 1995 - 2007 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18  */
       
    19 
       
    20 /* @(#) $Id: private.h,v 1.38 2007/09/07 12:07:59 michael Exp $ */
       
    21 
       
    22 #ifndef _PRIVATE_H
       
    23 #define _PRIVATE_H
       
    24 
       
    25 #define __LIBELF_INTERNAL__ 1
       
    26 
       
    27 #if HAVE_CONFIG_H
       
    28 # include <config.h>
       
    29 #endif /* HAVE_CONFIG_H */
       
    30 
       
    31 /*
       
    32  * Workaround for GLIBC bug:
       
    33  * include <stdint.h> before <sys/types.h>
       
    34  */
       
    35 #if HAVE_STDINT_H
       
    36 #include <stdint.h>
       
    37 #endif
       
    38 #include <sys/types.h>
       
    39 
       
    40 #if STDC_HEADERS
       
    41 # include <stdlib.h>
       
    42 # include <string.h>
       
    43 #else /* STDC_HEADERS */
       
    44 extern void *malloc(), *realloc();
       
    45 extern void free(), bcopy(), abort();
       
    46 extern int strcmp(), strncmp(), memcmp();
       
    47 extern void *memcpy(), *memmove(), *memset();
       
    48 #endif /* STDC_HEADERS */
       
    49 
       
    50 #if defined(_WIN32)
       
    51 #include <io.h>
       
    52 #else
       
    53 #if HAVE_UNISTD_H
       
    54 # include <unistd.h>
       
    55 #else /* HAVE_UNISTD_H */
       
    56 extern int read(), write(), close();
       
    57 extern off_t lseek();
       
    58 #if HAVE_FTRUNCATE
       
    59 extern int ftruncate();
       
    60 #endif /* HAVE_FTRUNCATE */
       
    61 #endif /* HAVE_UNISTD_H */
       
    62 #endif /* defined(_WIN32) */
       
    63 
       
    64 #ifndef SEEK_SET
       
    65 #define SEEK_SET	0
       
    66 #endif /* SEEK_SET */
       
    67 #ifndef SEEK_CUR
       
    68 #define SEEK_CUR	1
       
    69 #endif /* SEEK_CUR */
       
    70 #ifndef SEEK_END
       
    71 #define SEEK_END	2
       
    72 #endif /* SEEK_END */
       
    73 
       
    74 #if !HAVE_MEMCMP
       
    75 # define memcmp	strncmp
       
    76 #endif /* !HAVE_MEMCMP */
       
    77 #if !HAVE_MEMCPY
       
    78 # define memcpy(d,s,n)	bcopy(s,d,n)
       
    79 #endif /* !HAVE_MEMCPY */
       
    80 #if !HAVE_MEMMOVE
       
    81 # define memmove(d,s,n)	bcopy(s,d,n)
       
    82 #endif /* !HAVE_MEMMOVE */
       
    83 
       
    84 #if !HAVE_MEMSET
       
    85 # define memset _elf_memset
       
    86 extern void *_elf_memset();
       
    87 #endif /* !HAVE_MEMSET */
       
    88 
       
    89 #if HAVE_STRUCT_NLIST_DECLARATION
       
    90 # define nlist __override_nlist_declaration
       
    91 #endif /* HAVE_STRUCT_NLIST_DECLARATION */
       
    92 
       
    93 #if __LIBELF_NEED_LINK_H
       
    94 # include <link.h>
       
    95 #elif __LIBELF_NEED_SYS_LINK_H
       
    96 # include <sys/link.h>
       
    97 #endif /* __LIBELF_NEED_LINK_H */
       
    98 
       
    99 #include <libelf.h>
       
   100 
       
   101 #if HAVE_STRUCT_NLIST_DECLARATION
       
   102 # undef nlist
       
   103 #endif /* HAVE_STRUCT_NLIST_DECLARATION */
       
   104 
       
   105 #if __LIBELF64
       
   106 #include <gelf.h>
       
   107 #endif /* __LIBELF64 */
       
   108 
       
   109 typedef struct Scn_Data Scn_Data;
       
   110 
       
   111 /*
       
   112  * ELF descriptor
       
   113  */
       
   114 struct Elf {
       
   115     /* common */
       
   116     size_t	e_size;			/* file/member size */
       
   117     size_t	e_dsize;		/* size of memory image */
       
   118     Elf_Kind	e_kind;			/* kind of file */
       
   119     char*	e_data;			/* file/member data */
       
   120     char*	e_rawdata;		/* file/member raw data */
       
   121     size_t	e_idlen;		/* identifier size */
       
   122     int		e_fd;			/* file descriptor */
       
   123     unsigned	e_count;		/* activation count */
       
   124     /* archive members (still common) */
       
   125     Elf*	e_parent;		/* NULL if not an archive member */
       
   126     size_t	e_next;			/* 0 if not an archive member */
       
   127     size_t	e_base;			/* 0 if not an archive member */
       
   128     Elf*	e_link;			/* next archive member or NULL */
       
   129     Elf_Arhdr*	e_arhdr;		/* archive member header or NULL */
       
   130     /* archives */
       
   131     size_t	e_off;			/* current member offset (for elf_begin) */
       
   132     Elf*	e_members;		/* linked list of active archive members */
       
   133     char*	e_symtab;		/* archive symbol table */
       
   134     size_t	e_symlen;		/* length of archive symbol table */
       
   135     char*	e_strtab;		/* archive string table */
       
   136     size_t	e_strlen;		/* length of archive string table */
       
   137     /* ELF files */
       
   138     unsigned	e_class;		/* ELF class */
       
   139     unsigned	e_encoding;		/* ELF data encoding */
       
   140     unsigned	e_version;		/* ELF version */
       
   141     char*	e_ehdr;			/* ELF header */
       
   142     char*	e_phdr;			/* ELF program header table */
       
   143     size_t	e_phnum;		/* size of program header table */
       
   144     Elf_Scn*	e_scn_1;		/* first section */
       
   145     Elf_Scn*	e_scn_n;		/* last section */
       
   146     unsigned	e_elf_flags;		/* elf flags (ELF_F_*) */
       
   147     unsigned	e_ehdr_flags;		/* ehdr flags (ELF_F_*) */
       
   148     unsigned	e_phdr_flags;		/* phdr flags (ELF_F_*) */
       
   149     /* misc flags */
       
   150     unsigned	e_readable : 1;		/* file is readable */
       
   151     unsigned	e_writable : 1;		/* file is writable */
       
   152     unsigned	e_disabled : 1;		/* e_fd has been disabled */
       
   153     unsigned	e_cooked : 1;		/* e_data was modified */
       
   154     unsigned	e_free_syms : 1;	/* e_symtab is malloc'ed */
       
   155     unsigned	e_unmap_data : 1;	/* e_data is mmap'ed */
       
   156     unsigned	e_memory : 1;		/* created by elf_memory() */
       
   157     /* magic number for debugging */
       
   158     long	e_magic;
       
   159 };
       
   160 
       
   161 #define ELF_MAGIC	0x012b649e
       
   162 
       
   163 #define INIT_ELF	{\
       
   164     /* e_size */	0,\
       
   165     /* e_dsize */	0,\
       
   166     /* e_kind */	ELF_K_NONE,\
       
   167     /* e_data */	NULL,\
       
   168     /* e_rawdata */	NULL,\
       
   169     /* e_idlen */	0,\
       
   170     /* e_fd */		-1,\
       
   171     /* e_count */	1,\
       
   172     /* e_parent */	NULL,\
       
   173     /* e_next */	0,\
       
   174     /* e_base */	0,\
       
   175     /* e_link */	NULL,\
       
   176     /* e_arhdr */	NULL,\
       
   177     /* e_off */		0,\
       
   178     /* e_members */	NULL,\
       
   179     /* e_symtab */	NULL,\
       
   180     /* e_symlen */	0,\
       
   181     /* e_strtab */	NULL,\
       
   182     /* e_strlen */	0,\
       
   183     /* e_class */	ELFCLASSNONE,\
       
   184     /* e_encoding */	ELFDATANONE,\
       
   185     /* e_version */	EV_NONE,\
       
   186     /* e_ehdr */	NULL,\
       
   187     /* e_phdr */	NULL,\
       
   188     /* e_phnum */	0,\
       
   189     /* e_scn_1 */	NULL,\
       
   190     /* e_scn_n */	NULL,\
       
   191     /* e_elf_flags */	0,\
       
   192     /* e_ehdr_flags */	0,\
       
   193     /* e_phdr_flags */	0,\
       
   194     /* e_readable */	0,\
       
   195     /* e_writable */	0,\
       
   196     /* e_disabled */	0,\
       
   197     /* e_cooked */	0,\
       
   198     /* e_free_syms */	0,\
       
   199     /* e_unmap_data */	0,\
       
   200     /* e_memory */	0,\
       
   201     /* e_magic */	ELF_MAGIC\
       
   202 }
       
   203 
       
   204 /*
       
   205  * Section descriptor
       
   206  */
       
   207 struct Elf_Scn {
       
   208     Elf_Scn*	s_link;			/* pointer to next Elf_Scn */
       
   209     Elf*	s_elf;			/* pointer to elf descriptor */
       
   210     size_t	s_index;		/* number of this section */
       
   211     unsigned	s_scn_flags;		/* section flags (ELF_F_*) */
       
   212     unsigned	s_shdr_flags;		/* shdr flags (ELF_F_*) */
       
   213     Scn_Data*	s_data_1;		/* first data buffer */
       
   214     Scn_Data*	s_data_n;		/* last data buffer */
       
   215     Scn_Data*	s_rawdata;		/* raw data buffer */
       
   216     /* data copied from shdr */
       
   217     unsigned	s_type;			/* section type */
       
   218     size_t	s_offset;		/* section offset */
       
   219     size_t	s_size;			/* section size */
       
   220     /* misc flags */
       
   221     unsigned	s_freeme : 1;		/* this Elf_Scn was malloc'ed */
       
   222     /* section header */
       
   223     union {
       
   224 #if __LIBELF64
       
   225 	Elf64_Shdr	u_shdr64;
       
   226 #endif /* __LIBELF64 */
       
   227 	Elf32_Shdr	u_shdr32;
       
   228     }		s_uhdr;
       
   229     /* magic number for debugging */
       
   230     long	s_magic;
       
   231 };
       
   232 #define s_shdr32	s_uhdr.u_shdr32
       
   233 #define s_shdr64	s_uhdr.u_shdr64
       
   234 
       
   235 #define SCN_MAGIC	0x012c747d
       
   236 
       
   237 #define INIT_SCN	{\
       
   238     /* s_link */	NULL,\
       
   239     /* s_elf */		NULL,\
       
   240     /* s_index */	0,\
       
   241     /* s_scn_flags */	0,\
       
   242     /* s_shdr_flags */	0,\
       
   243     /* s_data_1 */	NULL,\
       
   244     /* s_data_n */	NULL,\
       
   245     /* s_rawdata */	NULL,\
       
   246     /* s_type */	SHT_NULL,\
       
   247     /* s_offset */	0,\
       
   248     /* s_size */	0,\
       
   249     /* s_freeme */	0,\
       
   250     /* s_uhdr */	{{0,}},\
       
   251     /* s_magic */	SCN_MAGIC\
       
   252 }
       
   253 
       
   254 /*
       
   255  * Data descriptor
       
   256  */
       
   257 struct Scn_Data {
       
   258     Elf_Data	sd_data;		/* must be first! */
       
   259     Scn_Data*	sd_link;		/* pointer to next Scn_Data */
       
   260     Elf_Scn*	sd_scn;			/* pointer to section */
       
   261     char*	sd_memdata;		/* memory image of section */
       
   262     unsigned	sd_data_flags;		/* data flags (ELF_F_*) */
       
   263     /* misc flags */
       
   264     unsigned	sd_freeme : 1;		/* this Scn_Data was malloc'ed */
       
   265     unsigned	sd_free_data : 1;	/* sd_memdata is malloc'ed */
       
   266     /* magic number for debugging */
       
   267     long	sd_magic;
       
   268 };
       
   269 
       
   270 #define DATA_MAGIC	0x01072639
       
   271 
       
   272 #define INIT_DATA	{\
       
   273     {\
       
   274     /* d_buf */		NULL,\
       
   275     /* d_type */	ELF_T_BYTE,\
       
   276     /* d_size */	0,\
       
   277     /* d_off */		0,\
       
   278     /* d_align */	0,\
       
   279     /* d_version */	EV_NONE\
       
   280     },\
       
   281     /* sd_link */	NULL,\
       
   282     /* sd_scn */	NULL,\
       
   283     /* sd_memdata */	NULL,\
       
   284     /* sd_data_flags */	0,\
       
   285     /* sd_freeme */	0,\
       
   286     /* sd_free_data */	0,\
       
   287     /* sd_magic */	DATA_MAGIC\
       
   288 }
       
   289 
       
   290 /*
       
   291  * Private status variables
       
   292  */
       
   293 extern unsigned _elf_version;
       
   294 extern int _elf_errno;
       
   295 extern int _elf_fill;
       
   296 extern int _elf_sanity_checks;
       
   297 #define SANITY_CHECK_STRPTR	(1u << 0)
       
   298 
       
   299 /*
       
   300  * Private functions
       
   301  */
       
   302 extern void *_elf_read __P((Elf*, void*, size_t, size_t));
       
   303 extern void *_elf_mmap __P((Elf*));
       
   304 extern int _elf_cook __P((Elf*));
       
   305 extern char *_elf_getehdr __P((Elf*, unsigned));
       
   306 extern char *_elf_getphdr __P((Elf*, unsigned));
       
   307 extern Elf_Data *_elf_xlatetom __P((const Elf*, Elf_Data*, const Elf_Data*));
       
   308 extern Elf_Type _elf_scn_type __P((unsigned));
       
   309 extern size_t _elf32_xltsize __P((const Elf_Data *__src, unsigned __dv, unsigned __encode, int __tof));
       
   310 extern size_t _elf64_xltsize __P((const Elf_Data *__src, unsigned __dv, unsigned __encode, int __tof));
       
   311 extern int _elf_update_shnum(Elf *__elf, size_t __shnum);
       
   312 extern Elf_Scn *_elf_first_scn(Elf *__elf);
       
   313 
       
   314 /*
       
   315  * Special translators
       
   316  */
       
   317 extern size_t _elf_verdef_32L11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   318 extern size_t _elf_verdef_32L11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   319 extern size_t _elf_verdef_32M11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   320 extern size_t _elf_verdef_32M11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   321 extern size_t _elf_verdef_64L11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   322 extern size_t _elf_verdef_64L11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   323 extern size_t _elf_verdef_64M11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   324 extern size_t _elf_verdef_64M11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   325 extern size_t _elf_verneed_32L11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   326 extern size_t _elf_verneed_32L11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   327 extern size_t _elf_verneed_32M11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   328 extern size_t _elf_verneed_32M11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   329 extern size_t _elf_verneed_64L11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   330 extern size_t _elf_verneed_64L11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   331 extern size_t _elf_verneed_64M11_tof __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   332 extern size_t _elf_verneed_64M11_tom __P((unsigned char *dst, const unsigned char *src, size_t n));
       
   333 
       
   334 /*
       
   335  * Private data
       
   336  */
       
   337 extern const Elf_Scn _elf_scn_init;
       
   338 extern const Scn_Data _elf_data_init;
       
   339 extern const size_t _elf_fmsize[2][EV_CURRENT - EV_NONE][ELF_T_NUM][2];
       
   340 
       
   341 /*
       
   342  * Access macros for _elf_fmsize[]
       
   343  */
       
   344 #define _fmsize(c,v,t,w)	\
       
   345 	(_elf_fmsize[(c)-ELFCLASS32][(v)-EV_NONE-1][(t)-ELF_T_BYTE][(w)])
       
   346 #define _fsize(c,v,t)		_fmsize((c),(v),(t),1)
       
   347 #define _msize(c,v,t)		_fmsize((c),(v),(t),0)
       
   348 
       
   349 /*
       
   350  * Various checks
       
   351  */
       
   352 #define valid_class(c)		((c) >= ELFCLASS32 && (c) <= ELFCLASS64)
       
   353 #define valid_encoding(e)	((e) >= ELFDATA2LSB && (e) <= ELFDATA2MSB)
       
   354 #define valid_version(v)	((v) > EV_NONE && (v) <= EV_CURRENT)
       
   355 #define valid_type(t)		((unsigned)(t) < ELF_T_NUM)
       
   356 
       
   357 /*
       
   358  * Error codes
       
   359  */
       
   360 enum {
       
   361 #define __err__(a,b)	a,
       
   362 #include <errors.h>		/* include constants from errors.h */
       
   363 #undef __err__
       
   364 ERROR_NUM
       
   365 };
       
   366 
       
   367 #define seterr(err)	(_elf_errno = (err))
       
   368 
       
   369 /*
       
   370  * Sizes of data types (external representation)
       
   371  * These definitions should be in <elf.h>, but...
       
   372  */
       
   373 #ifndef ELF32_FSZ_ADDR
       
   374 # define ELF32_FSZ_ADDR		4
       
   375 # define ELF32_FSZ_HALF		2
       
   376 # define ELF32_FSZ_OFF		4
       
   377 # define ELF32_FSZ_SWORD	4
       
   378 # define ELF32_FSZ_WORD		4
       
   379 #endif /* ELF32_FSZ_ADDR */
       
   380 #ifndef ELF64_FSZ_ADDR
       
   381 # define ELF64_FSZ_ADDR		8
       
   382 # define ELF64_FSZ_HALF		2
       
   383 # define ELF64_FSZ_OFF		8
       
   384 # define ELF64_FSZ_SWORD	4
       
   385 # define ELF64_FSZ_SXWORD	8
       
   386 # define ELF64_FSZ_WORD		4
       
   387 # define ELF64_FSZ_XWORD	8
       
   388 #endif /* ELF64_FSZ_ADDR */
       
   389 
       
   390 /*
       
   391  * More missing pieces, in no particular order
       
   392  */
       
   393 #ifndef SHT_SYMTAB_SHNDX
       
   394 #define SHT_SYMTAB_SHNDX	18
       
   395 #endif /* SHT_SYMTAB_SHNDX */
       
   396 
       
   397 #ifndef SHN_XINDEX
       
   398 #define SHN_XINDEX		0xffff
       
   399 #endif /* SHN_XINDEX */
       
   400 
       
   401 #ifndef PN_XNUM
       
   402 #define PN_XNUM			0xffff
       
   403 #endif /* PN_XNUM */
       
   404 
       
   405 /*
       
   406  * Debugging
       
   407  */
       
   408 #if ENABLE_DEBUG
       
   409 extern void __elf_assert __P((const char*, unsigned, const char*));
       
   410 # if (__STDC__ + 0)
       
   411 #  define elf_assert(x)	do{if(!(x))__elf_assert(__FILE__,__LINE__,#x);}while(0)
       
   412 # else /* __STDC__ */
       
   413 #  define elf_assert(x)	do{if(!(x))__elf_assert(__FILE__,__LINE__,"x");}while(0)
       
   414 # endif /* __STDC__ */
       
   415 #else /* ENABLE_DEBUG */
       
   416 # define elf_assert(x)	do{}while(0)
       
   417 #endif /* ENABLE_DEBUG */
       
   418 
       
   419 /*
       
   420  * Return values for certain functions
       
   421  */
       
   422 #define LIBELF_SUCCESS	1
       
   423 #define LIBELF_FAILURE	0
       
   424 
       
   425 #endif /* _PRIVATE_H */