1*f8fb3368SJohn Marino /*- 2*f8fb3368SJohn Marino * Copyright (c) 2006,2008-2011 Joseph Koshy 3*f8fb3368SJohn Marino * All rights reserved. 4*f8fb3368SJohn Marino * 5*f8fb3368SJohn Marino * Redistribution and use in source and binary forms, with or without 6*f8fb3368SJohn Marino * modification, are permitted provided that the following conditions 7*f8fb3368SJohn Marino * are met: 8*f8fb3368SJohn Marino * 1. Redistributions of source code must retain the above copyright 9*f8fb3368SJohn Marino * notice, this list of conditions and the following disclaimer. 10*f8fb3368SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright 11*f8fb3368SJohn Marino * notice, this list of conditions and the following disclaimer in the 12*f8fb3368SJohn Marino * documentation and/or other materials provided with the distribution. 13*f8fb3368SJohn Marino * 14*f8fb3368SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15*f8fb3368SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16*f8fb3368SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17*f8fb3368SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18*f8fb3368SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19*f8fb3368SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20*f8fb3368SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*f8fb3368SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22*f8fb3368SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23*f8fb3368SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24*f8fb3368SJohn Marino * SUCH DAMAGE. 25*f8fb3368SJohn Marino * 26*f8fb3368SJohn Marino * $Id: _libelf.h 3174 2015-03-27 17:13:41Z emaste $ 27*f8fb3368SJohn Marino */ 28*f8fb3368SJohn Marino 29*f8fb3368SJohn Marino #ifndef __LIBELF_H_ 30*f8fb3368SJohn Marino #define __LIBELF_H_ 31*f8fb3368SJohn Marino 32*f8fb3368SJohn Marino #include <sys/queue.h> 33*f8fb3368SJohn Marino 34*f8fb3368SJohn Marino #include "_libelf_config.h" 35*f8fb3368SJohn Marino 36*f8fb3368SJohn Marino #include "_elftc.h" 37*f8fb3368SJohn Marino 38*f8fb3368SJohn Marino /* 39*f8fb3368SJohn Marino * Library-private data structures. 40*f8fb3368SJohn Marino */ 41*f8fb3368SJohn Marino 42*f8fb3368SJohn Marino #define LIBELF_MSG_SIZE 256 43*f8fb3368SJohn Marino 44*f8fb3368SJohn Marino struct _libelf_globals { 45*f8fb3368SJohn Marino int libelf_arch; 46*f8fb3368SJohn Marino unsigned int libelf_byteorder; 47*f8fb3368SJohn Marino int libelf_class; 48*f8fb3368SJohn Marino int libelf_error; 49*f8fb3368SJohn Marino int libelf_fillchar; 50*f8fb3368SJohn Marino unsigned int libelf_version; 51*f8fb3368SJohn Marino unsigned char libelf_msg[LIBELF_MSG_SIZE]; 52*f8fb3368SJohn Marino }; 53*f8fb3368SJohn Marino 54*f8fb3368SJohn Marino extern struct _libelf_globals _libelf; 55*f8fb3368SJohn Marino 56*f8fb3368SJohn Marino #define LIBELF_PRIVATE(N) (_libelf.libelf_##N) 57*f8fb3368SJohn Marino 58*f8fb3368SJohn Marino #define LIBELF_ELF_ERROR_MASK 0xFF 59*f8fb3368SJohn Marino #define LIBELF_OS_ERROR_SHIFT 8 60*f8fb3368SJohn Marino 61*f8fb3368SJohn Marino #define LIBELF_ERROR(E, O) (((E) & LIBELF_ELF_ERROR_MASK) | \ 62*f8fb3368SJohn Marino ((O) << LIBELF_OS_ERROR_SHIFT)) 63*f8fb3368SJohn Marino 64*f8fb3368SJohn Marino #define LIBELF_SET_ERROR(E, O) do { \ 65*f8fb3368SJohn Marino LIBELF_PRIVATE(error) = LIBELF_ERROR(ELF_E_##E, (O)); \ 66*f8fb3368SJohn Marino } while (0) 67*f8fb3368SJohn Marino 68*f8fb3368SJohn Marino #define LIBELF_ADJUST_AR_SIZE(S) (((S) + 1U) & ~1U) 69*f8fb3368SJohn Marino 70*f8fb3368SJohn Marino /* 71*f8fb3368SJohn Marino * Flags for library internal use. These use the upper 16 bits of the 72*f8fb3368SJohn Marino * `e_flags' field. 73*f8fb3368SJohn Marino */ 74*f8fb3368SJohn Marino #define LIBELF_F_API_MASK 0x00FFFFU /* Flags defined by the API. */ 75*f8fb3368SJohn Marino #define LIBELF_F_AR_HEADER 0x010000U /* translated header available */ 76*f8fb3368SJohn Marino #define LIBELF_F_AR_VARIANT_SVR4 0x020000U /* BSD style ar(1) archive */ 77*f8fb3368SJohn Marino #define LIBELF_F_DATA_MALLOCED 0x040000U /* whether data was malloc'ed */ 78*f8fb3368SJohn Marino #define LIBELF_F_RAWFILE_MALLOC 0x080000U /* whether e_rawfile was malloc'ed */ 79*f8fb3368SJohn Marino #define LIBELF_F_RAWFILE_MMAP 0x100000U /* whether e_rawfile was mmap'ed */ 80*f8fb3368SJohn Marino #define LIBELF_F_SHDRS_LOADED 0x200000U /* whether all shdrs were read in */ 81*f8fb3368SJohn Marino #define LIBELF_F_SPECIAL_FILE 0x400000U /* non-regular file */ 82*f8fb3368SJohn Marino 83*f8fb3368SJohn Marino struct _Elf { 84*f8fb3368SJohn Marino int e_activations; /* activation count */ 85*f8fb3368SJohn Marino unsigned int e_byteorder; /* ELFDATA* */ 86*f8fb3368SJohn Marino int e_class; /* ELFCLASS* */ 87*f8fb3368SJohn Marino Elf_Cmd e_cmd; /* ELF_C_* used at creation time */ 88*f8fb3368SJohn Marino int e_fd; /* associated file descriptor */ 89*f8fb3368SJohn Marino unsigned int e_flags; /* ELF_F_* & LIBELF_F_* flags */ 90*f8fb3368SJohn Marino Elf_Kind e_kind; /* ELF_K_* */ 91*f8fb3368SJohn Marino Elf *e_parent; /* non-NULL for archive members */ 92*f8fb3368SJohn Marino unsigned char *e_rawfile; /* uninterpreted bytes */ 93*f8fb3368SJohn Marino size_t e_rawsize; /* size of uninterpreted bytes */ 94*f8fb3368SJohn Marino unsigned int e_version; /* file version */ 95*f8fb3368SJohn Marino 96*f8fb3368SJohn Marino /* 97*f8fb3368SJohn Marino * Header information for archive members. See the 98*f8fb3368SJohn Marino * LIBELF_F_AR_HEADER flag. 99*f8fb3368SJohn Marino */ 100*f8fb3368SJohn Marino union { 101*f8fb3368SJohn Marino Elf_Arhdr *e_arhdr; /* translated header */ 102*f8fb3368SJohn Marino unsigned char *e_rawhdr; /* untranslated header */ 103*f8fb3368SJohn Marino } e_hdr; 104*f8fb3368SJohn Marino 105*f8fb3368SJohn Marino union { 106*f8fb3368SJohn Marino struct { /* ar(1) archives */ 107*f8fb3368SJohn Marino off_t e_next; /* set by elf_rand()/elf_next() */ 108*f8fb3368SJohn Marino int e_nchildren; 109*f8fb3368SJohn Marino unsigned char *e_rawstrtab; /* file name strings */ 110*f8fb3368SJohn Marino size_t e_rawstrtabsz; 111*f8fb3368SJohn Marino unsigned char *e_rawsymtab; /* symbol table */ 112*f8fb3368SJohn Marino size_t e_rawsymtabsz; 113*f8fb3368SJohn Marino Elf_Arsym *e_symtab; 114*f8fb3368SJohn Marino size_t e_symtabsz; 115*f8fb3368SJohn Marino } e_ar; 116*f8fb3368SJohn Marino struct { /* regular ELF files */ 117*f8fb3368SJohn Marino union { 118*f8fb3368SJohn Marino Elf32_Ehdr *e_ehdr32; 119*f8fb3368SJohn Marino Elf64_Ehdr *e_ehdr64; 120*f8fb3368SJohn Marino } e_ehdr; 121*f8fb3368SJohn Marino union { 122*f8fb3368SJohn Marino Elf32_Phdr *e_phdr32; 123*f8fb3368SJohn Marino Elf64_Phdr *e_phdr64; 124*f8fb3368SJohn Marino } e_phdr; 125*f8fb3368SJohn Marino STAILQ_HEAD(, _Elf_Scn) e_scn; /* section list */ 126*f8fb3368SJohn Marino size_t e_nphdr; /* number of Phdr entries */ 127*f8fb3368SJohn Marino size_t e_nscn; /* number of sections */ 128*f8fb3368SJohn Marino size_t e_strndx; /* string table section index */ 129*f8fb3368SJohn Marino } e_elf; 130*f8fb3368SJohn Marino } e_u; 131*f8fb3368SJohn Marino }; 132*f8fb3368SJohn Marino 133*f8fb3368SJohn Marino /* 134*f8fb3368SJohn Marino * The internal descriptor wrapping the "Elf_Data" type. 135*f8fb3368SJohn Marino */ 136*f8fb3368SJohn Marino struct _Libelf_Data { 137*f8fb3368SJohn Marino Elf_Data d_data; /* The exported descriptor. */ 138*f8fb3368SJohn Marino Elf_Scn *d_scn; /* The containing section */ 139*f8fb3368SJohn Marino unsigned int d_flags; 140*f8fb3368SJohn Marino STAILQ_ENTRY(_Libelf_Data) d_next; 141*f8fb3368SJohn Marino }; 142*f8fb3368SJohn Marino 143*f8fb3368SJohn Marino struct _Elf_Scn { 144*f8fb3368SJohn Marino union { 145*f8fb3368SJohn Marino Elf32_Shdr s_shdr32; 146*f8fb3368SJohn Marino Elf64_Shdr s_shdr64; 147*f8fb3368SJohn Marino } s_shdr; 148*f8fb3368SJohn Marino STAILQ_HEAD(, _Libelf_Data) s_data; /* translated data */ 149*f8fb3368SJohn Marino STAILQ_HEAD(, _Libelf_Data) s_rawdata; /* raw data */ 150*f8fb3368SJohn Marino STAILQ_ENTRY(_Elf_Scn) s_next; 151*f8fb3368SJohn Marino struct _Elf *s_elf; /* parent ELF descriptor */ 152*f8fb3368SJohn Marino unsigned int s_flags; /* flags for the section as a whole */ 153*f8fb3368SJohn Marino size_t s_ndx; /* index# for this section */ 154*f8fb3368SJohn Marino uint64_t s_offset; /* managed by elf_update() */ 155*f8fb3368SJohn Marino uint64_t s_rawoff; /* original offset in the file */ 156*f8fb3368SJohn Marino uint64_t s_size; /* managed by elf_update() */ 157*f8fb3368SJohn Marino }; 158*f8fb3368SJohn Marino 159*f8fb3368SJohn Marino 160*f8fb3368SJohn Marino enum { 161*f8fb3368SJohn Marino ELF_TOFILE, 162*f8fb3368SJohn Marino ELF_TOMEMORY 163*f8fb3368SJohn Marino }; 164*f8fb3368SJohn Marino 165*f8fb3368SJohn Marino 166*f8fb3368SJohn Marino /* 167*f8fb3368SJohn Marino * The LIBELF_COPY macros are used to copy fields from a GElf_* 168*f8fb3368SJohn Marino * structure to their 32-bit counterparts, while checking for out of 169*f8fb3368SJohn Marino * range values. 170*f8fb3368SJohn Marino * 171*f8fb3368SJohn Marino * - LIBELF_COPY_U32 :: copy an unsigned 32 bit field. 172*f8fb3368SJohn Marino * - LIBELF_COPY_S32 :: copy a signed 32 bit field. 173*f8fb3368SJohn Marino */ 174*f8fb3368SJohn Marino 175*f8fb3368SJohn Marino #define LIBELF_COPY_U32(DST, SRC, NAME) do { \ 176*f8fb3368SJohn Marino if ((SRC)->NAME > UINT32_MAX) { \ 177*f8fb3368SJohn Marino LIBELF_SET_ERROR(RANGE, 0); \ 178*f8fb3368SJohn Marino return (0); \ 179*f8fb3368SJohn Marino } \ 180*f8fb3368SJohn Marino (DST)->NAME = (SRC)->NAME & 0xFFFFFFFFU; \ 181*f8fb3368SJohn Marino } while (0) 182*f8fb3368SJohn Marino 183*f8fb3368SJohn Marino #define LIBELF_COPY_S32(DST, SRC, NAME) do { \ 184*f8fb3368SJohn Marino if ((SRC)->NAME > INT32_MAX || \ 185*f8fb3368SJohn Marino (SRC)->NAME < INT32_MIN) { \ 186*f8fb3368SJohn Marino LIBELF_SET_ERROR(RANGE, 0); \ 187*f8fb3368SJohn Marino return (0); \ 188*f8fb3368SJohn Marino } \ 189*f8fb3368SJohn Marino (DST)->NAME = (int32_t) (SRC)->NAME; \ 190*f8fb3368SJohn Marino } while (0) 191*f8fb3368SJohn Marino 192*f8fb3368SJohn Marino 193*f8fb3368SJohn Marino /* 194*f8fb3368SJohn Marino * Function Prototypes. 195*f8fb3368SJohn Marino */ 196*f8fb3368SJohn Marino 197*f8fb3368SJohn Marino #ifdef __cplusplus 198*f8fb3368SJohn Marino extern "C" { 199*f8fb3368SJohn Marino #endif 200*f8fb3368SJohn Marino struct _Libelf_Data *_libelf_allocate_data(Elf_Scn *_s); 201*f8fb3368SJohn Marino Elf *_libelf_allocate_elf(void); 202*f8fb3368SJohn Marino Elf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx); 203*f8fb3368SJohn Marino Elf_Arhdr *_libelf_ar_gethdr(Elf *_e); 204*f8fb3368SJohn Marino Elf *_libelf_ar_open(Elf *_e, int _reporterror); 205*f8fb3368SJohn Marino Elf *_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar); 206*f8fb3368SJohn Marino Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst); 207*f8fb3368SJohn Marino Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst); 208*f8fb3368SJohn Marino long _libelf_checksum(Elf *_e, int _elfclass); 209*f8fb3368SJohn Marino void *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate); 210*f8fb3368SJohn Marino unsigned int _libelf_falign(Elf_Type _t, int _elfclass); 211*f8fb3368SJohn Marino size_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version, 212*f8fb3368SJohn Marino size_t count); 213*f8fb3368SJohn Marino int (*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass)) 214*f8fb3368SJohn Marino (unsigned char *_dst, size_t dsz, unsigned char *_src, 215*f8fb3368SJohn Marino size_t _cnt, int _byteswap); 216*f8fb3368SJohn Marino void *_libelf_getphdr(Elf *_e, int _elfclass); 217*f8fb3368SJohn Marino void *_libelf_getshdr(Elf_Scn *_scn, int _elfclass); 218*f8fb3368SJohn Marino void _libelf_init_elf(Elf *_e, Elf_Kind _kind); 219*f8fb3368SJohn Marino int _libelf_load_section_headers(Elf *e, void *ehdr); 220*f8fb3368SJohn Marino unsigned int _libelf_malign(Elf_Type _t, int _elfclass); 221*f8fb3368SJohn Marino Elf *_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror); 222*f8fb3368SJohn Marino size_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version); 223*f8fb3368SJohn Marino void *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count); 224*f8fb3368SJohn Marino Elf *_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror); 225*f8fb3368SJohn Marino struct _Libelf_Data *_libelf_release_data(struct _Libelf_Data *_d); 226*f8fb3368SJohn Marino Elf *_libelf_release_elf(Elf *_e); 227*f8fb3368SJohn Marino Elf_Scn *_libelf_release_scn(Elf_Scn *_s); 228*f8fb3368SJohn Marino int _libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum); 229*f8fb3368SJohn Marino int _libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum); 230*f8fb3368SJohn Marino int _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass, 231*f8fb3368SJohn Marino size_t _shstrndx); 232*f8fb3368SJohn Marino Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s, 233*f8fb3368SJohn Marino unsigned int _encoding, int _elfclass, int _direction); 234*f8fb3368SJohn Marino int _libelf_xlate_shtype(uint32_t _sht); 235*f8fb3368SJohn Marino #ifdef __cplusplus 236*f8fb3368SJohn Marino } 237*f8fb3368SJohn Marino #endif 238*f8fb3368SJohn Marino 239*f8fb3368SJohn Marino #endif /* __LIBELF_H_ */ 240