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