Home | History | Annotate | Download | only in libelf
      1 /* Interface for libelf.
      2    Copyright (C) 1998, 1999, 2000, 2002, 2004, 2005, 2006, 2007 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4 
      5    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.
      8 
      9    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.
     13 
     14    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,
     16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     17 
     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>.  */
     49 
     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.  */
     60 typedef 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   ELF_T_GNUHASH,		/* GNU-style hash section.  */
     86   ELF_T_AUXV,			/* Elf32_auxv_t, Elf64_auxv_t, ... */
     87   /* Keep this the last entry.  */
     88   ELF_T_NUM
     89 } Elf_Type;
     90 
     91 /* Descriptor for data to be converted to or from memory format.  */
     92 typedef struct
     93 {
     94   void *d_buf;			/* Pointer to the actual data.  */
     95   Elf_Type d_type;		/* Type of this piece of data.  */
     96   unsigned int d_version;	/* ELF version.  */
     97   size_t d_size;		/* Size in bytes.  */
     98   /* ANDROID_CHANGE_BEGIN */
     99 #if 0
    100   loff_t d_off;			/* Offset into section.  */
    101 #else
    102   off_t d_off;			/* Offset into section.  */
    103 #endif
    104   /* ANDROID_CHANGE_END */
    105   size_t d_align;		/* Alignment in section.  */
    106 } Elf_Data;
    107 
    108 
    109 /* Commands for `...'.  */
    110 typedef enum
    111 {
    112   ELF_C_NULL,			/* Nothing, terminate, or compute only.  */
    113   ELF_C_READ,			/* Read .. */
    114   ELF_C_RDWR,			/* Read and write .. */
    115   ELF_C_WRITE,			/* Write .. */
    116   ELF_C_CLR,			/* Clear flag.  */
    117   ELF_C_SET,			/* Set flag.  */
    118   ELF_C_FDDONE,			/* Signal that file descriptor will not be
    119 				   used anymore.  */
    120   ELF_C_FDREAD,			/* Read rest of data so that file descriptor
    121 				   is not used anymore.  */
    122   /* The following are extensions.  */
    123   ELF_C_READ_MMAP,		/* Read, but mmap the file if possible.  */
    124   ELF_C_RDWR_MMAP,		/* Read and write, with mmap.  */
    125   ELF_C_WRITE_MMAP,		/* Write, with mmap.  */
    126   ELF_C_READ_MMAP_PRIVATE,	/* Read, but memory is writable, results are
    127 				   not written to the file.  */
    128   ELF_C_EMPTY,			/* Copy basic file data but not the content. */
    129   /* Keep this the last entry.  */
    130   ELF_C_NUM
    131 } Elf_Cmd;
    132 
    133 
    134 /* Flags for the ELF structures.  */
    135 enum
    136 {
    137   ELF_F_DIRTY = 0x1,
    138 #define ELF_F_DIRTY		ELF_F_DIRTY
    139   ELF_F_LAYOUT = 0x4,
    140 #define ELF_F_LAYOUT		ELF_F_LAYOUT
    141   ELF_F_PERMISSIVE = 0x8
    142 #define ELF_F_PERMISSIVE	ELF_F_PERMISSIVE
    143 };
    144 
    145 
    146 /* Identification values for recognized object files.  */
    147 typedef enum
    148 {
    149   ELF_K_NONE,			/* Unknown.  */
    150   ELF_K_AR,			/* Archive.  */
    151   ELF_K_COFF,			/* Stupid old COFF.  */
    152   ELF_K_ELF,			/* ELF file.  */
    153   /* Keep this the last entry.  */
    154   ELF_K_NUM
    155 } Elf_Kind;
    156 
    157 
    158 /* Archive member header.  */
    159 typedef struct
    160 {
    161   char *ar_name;		/* Name of archive member.  */
    162   time_t ar_date;		/* File date.  */
    163   uid_t ar_uid;			/* User ID.  */
    164   gid_t ar_gid;			/* Group ID.  */
    165   mode_t ar_mode;		/* File mode.  */
    166   /* ANDROID_CHANGE_BEGIN */
    167 #if 0
    168   loff_t ar_size;		/* File size.  */
    169 #else
    170   off_t ar_size;		/* File size.  */
    171 #endif
    172   /* ANDROID_CHANGE_END */
    173   char *ar_rawname;		/* Original name of archive member.  */
    174 } Elf_Arhdr;
    175 
    176 
    177 /* Archive symbol table entry.  */
    178 typedef struct
    179 {
    180   char *as_name;		/* Symbol name.  */
    181   size_t as_off;		/* Offset for this file in the archive.  */
    182   unsigned long int as_hash;	/* Hash value of the name.  */
    183 } Elf_Arsym;
    184 
    185 
    186 /* Descriptor for the ELF file.  */
    187 typedef struct Elf Elf;
    188 
    189 /* Descriptor for ELF file section.  */
    190 typedef struct Elf_Scn Elf_Scn;
    191 
    192 
    193 #ifdef __cplusplus
    194 extern "C" {
    195 #endif
    196 
    197 /* Return descriptor for ELF file to work according to CMD.  */
    198 extern Elf *elf_begin (int __fildes, Elf_Cmd __cmd, Elf *__ref);
    199 
    200 /* Create a clone of an existing ELF descriptor.  */
    201   extern Elf *elf_clone (Elf *__elf, Elf_Cmd __cmd);
    202 
    203 /* Create descriptor for memory region.  */
    204 extern Elf *elf_memory (char *__image, size_t __size);
    205 
    206 /* Advance archive descriptor to next element.  */
    207 extern Elf_Cmd elf_next (Elf *__elf);
    208 
    209 /* Free resources allocated for ELF.  */
    210 extern int elf_end (Elf *__elf);
    211 
    212 /* Update ELF descriptor and write file to disk.  */
    213 /* ANDROID_CHANGE_BEGIN */
    214 #if 0
    215 extern off_t elf_update (Elf *__elf, Elf_Cmd __cmd);
    216 #else
    217 #endif
    218 /* ANDROID_CHANGE_END */
    219 
    220 
    221 /* Determine what kind of file is associated with ELF.  */
    222 extern Elf_Kind elf_kind (Elf *__elf) __attribute__ ((__pure__));
    223 
    224 /* Get the base offset for an object file.  */
    225 /* ANDROID_CHANGE_BEGIN */
    226 #if 0
    227 extern loff_t elf_getbase (Elf *__elf);
    228 #else
    229 extern off_t elf_getbase (Elf *__elf);
    230 #endif
    231 /* ANDROID_CHANGE_END */
    232 
    233 
    234 /* Retrieve file identification data.  */
    235 extern char *elf_getident (Elf *__elf, size_t *__nbytes);
    236 
    237 /* Retrieve class-dependent object file header.  */
    238 extern Elf32_Ehdr *elf32_getehdr (Elf *__elf);
    239 /* Similar but this time the binary calls is ELFCLASS64.  */
    240 extern Elf64_Ehdr *elf64_getehdr (Elf *__elf);
    241 
    242 /* Create ELF header if none exists.  */
    243 extern Elf32_Ehdr *elf32_newehdr (Elf *__elf);
    244 /* Similar but this time the binary calls is ELFCLASS64.  */
    245 extern Elf64_Ehdr *elf64_newehdr (Elf *__elf);
    246 
    247 /* Retrieve class-dependent program header table.  */
    248 extern Elf32_Phdr *elf32_getphdr (Elf *__elf);
    249 /* Similar but this time the binary calls is ELFCLASS64.  */
    250 extern Elf64_Phdr *elf64_getphdr (Elf *__elf);
    251 
    252 /* Create ELF program header.  */
    253 extern Elf32_Phdr *elf32_newphdr (Elf *__elf, size_t __cnt);
    254 /* Similar but this time the binary calls is ELFCLASS64.  */
    255 extern Elf64_Phdr *elf64_newphdr (Elf *__elf, size_t __cnt);
    256 
    257 
    258 /* Get section at INDEX.  */
    259 extern Elf_Scn *elf_getscn (Elf *__elf, size_t __index);
    260 
    261 /* Get section at OFFSET.  */
    262 extern Elf_Scn *elf32_offscn (Elf *__elf, Elf32_Off __offset);
    263 /* Similar bug this time the binary calls is ELFCLASS64.  */
    264 extern Elf_Scn *elf64_offscn (Elf *__elf, Elf64_Off __offset);
    265 
    266 /* Get index of section.  */
    267 extern size_t elf_ndxscn (Elf_Scn *__scn);
    268 
    269 /* Get section with next section index.  */
    270 extern Elf_Scn *elf_nextscn (Elf *__elf, Elf_Scn *__scn);
    271 
    272 /* Create a new section and append it at the end of the table.  */
    273 extern Elf_Scn *elf_newscn (Elf *__elf);
    274 
    275 /* Get the section index of the extended section index table for the
    276    given symbol table.  */
    277 extern int elf_scnshndx (Elf_Scn *__scn);
    278 
    279 /* Get the number of sections in the ELF file.  If the file uses more
    280    sections than can be represented in the e_shnum field of the ELF
    281    header the information from the sh_size field in the zeroth section
    282    header is used.  */
    283 extern int elf_getshnum (Elf *__elf, size_t *__dst);
    284 
    285 
    286 /* Get the section index of the section header string table in the ELF
    287    file.  If the index cannot be represented in the e_shnum field of
    288    the ELF header the information from the sh_link field in the zeroth
    289    section header is used.  */
    290 extern int elf_getshstrndx (Elf *__elf, size_t *__dst);
    291 
    292 
    293 /* Retrieve section header of ELFCLASS32 binary.  */
    294 extern Elf32_Shdr *elf32_getshdr (Elf_Scn *__scn);
    295 /* Similar for ELFCLASS64.  */
    296 extern Elf64_Shdr *elf64_getshdr (Elf_Scn *__scn);
    297 
    298 
    299 /* Set or clear flags for ELF file.  */
    300 extern unsigned int elf_flagelf (Elf *__elf, Elf_Cmd __cmd,
    301 				 unsigned int __flags);
    302 /* Similarly for the ELF header.  */
    303 extern unsigned int elf_flagehdr (Elf *__elf, Elf_Cmd __cmd,
    304 				  unsigned int __flags);
    305 /* Similarly for the ELF program header.  */
    306 extern unsigned int elf_flagphdr (Elf *__elf, Elf_Cmd __cmd,
    307 				  unsigned int __flags);
    308 /* Similarly for the given ELF section.  */
    309 extern unsigned int elf_flagscn (Elf_Scn *__scn, Elf_Cmd __cmd,
    310 				 unsigned int __flags);
    311 /* Similarly for the given ELF data.  */
    312 extern unsigned int elf_flagdata (Elf_Data *__data, Elf_Cmd __cmd,
    313 				  unsigned int __flags);
    314 /* Similarly for the given ELF section header.  */
    315 extern unsigned int elf_flagshdr (Elf_Scn *__scn, Elf_Cmd __cmd,
    316 				  unsigned int __flags);
    317 
    318 
    319 /* Get data from section while translating from file representation
    320    to memory representation.  */
    321 extern Elf_Data *elf_getdata (Elf_Scn *__scn, Elf_Data *__data);
    322 
    323 /* Get uninterpreted section content.  */
    324 extern Elf_Data *elf_rawdata (Elf_Scn *__scn, Elf_Data *__data);
    325 
    326 /* Create new data descriptor for section SCN.  */
    327 extern Elf_Data *elf_newdata (Elf_Scn *__scn);
    328 
    329 /* Get data translated from a chunk of the file contents as section data
    330    would be for TYPE.  The resulting Elf_Data pointer is valid until
    331    elf_end (ELF) is called.  */
    332 /* ANDROID_CHANGE_BEGIN */
    333 #if 0
    334 extern Elf_Data *elf_getdata_rawchunk (Elf *__elf,
    335 				       loff_t __offset, size_t __size,
    336 				       Elf_Type __type);
    337 #else
    338 extern Elf_Data *elf_getdata_rawchunk (Elf *__elf,
    339 				       off_t __offset, size_t __size,
    340 				       Elf_Type __type);
    341 #endif
    342 /* ANDROID_CHANGE_END */
    343 
    344 
    345 /* Return pointer to string at OFFSET in section INDEX.  */
    346 extern char *elf_strptr (Elf *__elf, size_t __index, size_t __offset);
    347 
    348 
    349 /* Return header of archive.  */
    350 extern Elf_Arhdr *elf_getarhdr (Elf *__elf);
    351 
    352 /* Return offset in archive for current file ELF.  */
    353 /* ANDROID_CHANGE_BEGIN */
    354 #if 0
    355 extern loff_t elf_getaroff (Elf *__elf);
    356 #else
    357 extern off_t elf_getaroff (Elf *__elf);
    358 #endif
    359 /* ANDROID_CHANGE_END */
    360 
    361 /* Select archive element at OFFSET.  */
    362 extern size_t elf_rand (Elf *__elf, size_t __offset);
    363 
    364 /* Get symbol table of archive.  */
    365 extern Elf_Arsym *elf_getarsym (Elf *__elf, size_t *__narsyms);
    366 
    367 
    368 /* Control ELF descriptor.  */
    369 extern int elf_cntl (Elf *__elf, Elf_Cmd __cmd);
    370 
    371 /* Retrieve uninterpreted file contents.  */
    372 extern char *elf_rawfile (Elf *__elf, size_t *__nbytes);
    373 
    374 
    375 /* Return size of array of COUNT elements of the type denoted by TYPE
    376    in the external representation.  The binary class is taken from ELF.
    377    The result is based on version VERSION of the ELF standard.  */
    378 extern size_t elf32_fsize (Elf_Type __type, size_t __count,
    379 			   unsigned int __version)
    380        __attribute__ ((__const__));
    381 /* Similar but this time the binary calls is ELFCLASS64.  */
    382 extern size_t elf64_fsize (Elf_Type __type, size_t __count,
    383 			   unsigned int __version)
    384        __attribute__ ((__const__));
    385 
    386 
    387 /* Convert data structure from the representation in the file represented
    388    by ELF to their memory representation.  */
    389 extern Elf_Data *elf32_xlatetom (Elf_Data *__dest, const Elf_Data *__src,
    390 				 unsigned int __encode);
    391 /* Same for 64 bit class.  */
    392 extern Elf_Data *elf64_xlatetom (Elf_Data *__dest, const Elf_Data *__src,
    393 				 unsigned int __encode);
    394 
    395 /* Convert data structure from to the representation in memory
    396    represented by ELF file representation.  */
    397 extern Elf_Data *elf32_xlatetof (Elf_Data *__dest, const Elf_Data *__src,
    398 				 unsigned int __encode);
    399 /* Same for 64 bit class.  */
    400 extern Elf_Data *elf64_xlatetof (Elf_Data *__dest, const Elf_Data *__src,
    401 				 unsigned int __encode);
    402 
    403 
    404 /* Return error code of last failing function call.  This value is kept
    405    separately for each thread.  */
    406 extern int elf_errno (void);
    407 
    408 /* Return error string for ERROR.  If ERROR is zero, return error string
    409    for most recent error or NULL is none occurred.  If ERROR is -1 the
    410    behaviour is similar to the last case except that not NULL but a legal
    411    string is returned.  */
    412 extern const char *elf_errmsg (int __error);
    413 
    414 
    415 /* Coordinate ELF library and application versions.  */
    416 extern unsigned int elf_version (unsigned int __version);
    417 
    418 /* Set fill bytes used to fill holes in data structures.  */
    419 extern void elf_fill (int __fill);
    420 
    421 /* Compute hash value.  */
    422 extern unsigned long int elf_hash (const char *__string)
    423        __attribute__ ((__pure__));
    424 
    425 /* Compute hash value using the GNU-specific hash function.  */
    426 extern unsigned long int elf_gnu_hash (const char *__string)
    427        __attribute__ ((__pure__));
    428 
    429 
    430 /* Compute simple checksum from permanent parts of the ELF file.  */
    431 extern long int elf32_checksum (Elf *__elf);
    432 /* Similar but this time the binary calls is ELFCLASS64.  */
    433 extern long int elf64_checksum (Elf *__elf);
    434 
    435 #ifdef __cplusplus
    436 }
    437 #endif
    438 
    439 #endif  /* libelf.h */
    440