_libelf.h revision 276371
1260684Skaiw/*- 2260684Skaiw * Copyright (c) 2006,2008-2011 Joseph Koshy 3260684Skaiw * All rights reserved. 4260684Skaiw * 5260684Skaiw * Redistribution and use in source and binary forms, with or without 6260684Skaiw * modification, are permitted provided that the following conditions 7260684Skaiw * are met: 8260684Skaiw * 1. Redistributions of source code must retain the above copyright 9260684Skaiw * notice, this list of conditions and the following disclaimer. 10260684Skaiw * 2. Redistributions in binary form must reproduce the above copyright 11260684Skaiw * notice, this list of conditions and the following disclaimer in the 12260684Skaiw * documentation and/or other materials provided with the distribution. 13260684Skaiw * 14260684Skaiw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15260684Skaiw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16260684Skaiw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17260684Skaiw * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18260684Skaiw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19260684Skaiw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20260684Skaiw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21260684Skaiw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22260684Skaiw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23260684Skaiw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24260684Skaiw * SUCH DAMAGE. 25260684Skaiw * 26276371Semaste * $Id: _libelf.h 3011 2014-03-23 03:32:42Z jkoshy $ 27260684Skaiw */ 28260684Skaiw 29260684Skaiw#ifndef __LIBELF_H_ 30260684Skaiw#define __LIBELF_H_ 31260684Skaiw 32260684Skaiw#include <sys/queue.h> 33260684Skaiw 34260684Skaiw#include "_libelf_config.h" 35260684Skaiw 36260684Skaiw#include "_elftc.h" 37260684Skaiw 38260684Skaiw/* 39260684Skaiw * Library-private data structures. 40260684Skaiw */ 41260684Skaiw 42260684Skaiw#define LIBELF_MSG_SIZE 256 43260684Skaiw 44260684Skaiwstruct _libelf_globals { 45260684Skaiw int libelf_arch; 46260684Skaiw unsigned int libelf_byteorder; 47260684Skaiw int libelf_class; 48260684Skaiw int libelf_error; 49260684Skaiw int libelf_fillchar; 50260684Skaiw unsigned int libelf_version; 51276371Semaste unsigned char libelf_msg[LIBELF_MSG_SIZE]; 52260684Skaiw}; 53260684Skaiw 54260684Skaiwextern struct _libelf_globals _libelf; 55260684Skaiw 56260684Skaiw#define LIBELF_PRIVATE(N) (_libelf.libelf_##N) 57260684Skaiw 58260684Skaiw#define LIBELF_ELF_ERROR_MASK 0xFF 59260684Skaiw#define LIBELF_OS_ERROR_SHIFT 8 60260684Skaiw 61260684Skaiw#define LIBELF_ERROR(E, O) (((E) & LIBELF_ELF_ERROR_MASK) | \ 62260684Skaiw ((O) << LIBELF_OS_ERROR_SHIFT)) 63260684Skaiw 64260684Skaiw#define LIBELF_SET_ERROR(E, O) do { \ 65260684Skaiw LIBELF_PRIVATE(error) = LIBELF_ERROR(ELF_E_##E, (O)); \ 66260684Skaiw } while (0) 67260684Skaiw 68260684Skaiw#define LIBELF_ADJUST_AR_SIZE(S) (((S) + 1U) & ~1U) 69260684Skaiw 70260684Skaiw/* 71260684Skaiw * Flags for library internal use. These use the upper 16 bits of the 72260684Skaiw * `e_flags' field. 73260684Skaiw */ 74276371Semaste#define LIBELF_F_API_MASK 0x00FFFFU /* Flags defined by the API. */ 75276371Semaste#define LIBELF_F_AR_HEADER 0x010000U /* translated header available */ 76276371Semaste#define LIBELF_F_AR_VARIANT_SVR4 0x020000U /* BSD style ar(1) archive */ 77276371Semaste#define LIBELF_F_DATA_MALLOCED 0x040000U /* whether data was malloc'ed */ 78276371Semaste#define LIBELF_F_RAWFILE_MALLOC 0x080000U /* whether e_rawfile was malloc'ed */ 79276371Semaste#define LIBELF_F_RAWFILE_MMAP 0x100000U /* whether e_rawfile was mmap'ed */ 80276371Semaste#define LIBELF_F_SHDRS_LOADED 0x200000U /* whether all shdrs were read in */ 81276371Semaste#define LIBELF_F_SPECIAL_FILE 0x400000U /* non-regular file */ 82260684Skaiw 83260684Skaiwstruct _Elf { 84260684Skaiw int e_activations; /* activation count */ 85260684Skaiw unsigned int e_byteorder; /* ELFDATA* */ 86260684Skaiw int e_class; /* ELFCLASS* */ 87260684Skaiw Elf_Cmd e_cmd; /* ELF_C_* used at creation time */ 88260684Skaiw int e_fd; /* associated file descriptor */ 89260684Skaiw unsigned int e_flags; /* ELF_F_* & LIBELF_F_* flags */ 90260684Skaiw Elf_Kind e_kind; /* ELF_K_* */ 91260684Skaiw Elf *e_parent; /* non-NULL for archive members */ 92276371Semaste unsigned char *e_rawfile; /* uninterpreted bytes */ 93260684Skaiw size_t e_rawsize; /* size of uninterpreted bytes */ 94260684Skaiw unsigned int e_version; /* file version */ 95260684Skaiw 96260684Skaiw /* 97260684Skaiw * Header information for archive members. See the 98260684Skaiw * LIBELF_F_AR_HEADER flag. 99260684Skaiw */ 100260684Skaiw union { 101260684Skaiw Elf_Arhdr *e_arhdr; /* translated header */ 102276371Semaste unsigned char *e_rawhdr; /* untranslated header */ 103260684Skaiw } e_hdr; 104260684Skaiw 105260684Skaiw union { 106260684Skaiw struct { /* ar(1) archives */ 107260684Skaiw off_t e_next; /* set by elf_rand()/elf_next() */ 108260684Skaiw int e_nchildren; 109276371Semaste unsigned char *e_rawstrtab; /* file name strings */ 110260684Skaiw size_t e_rawstrtabsz; 111276371Semaste unsigned char *e_rawsymtab; /* symbol table */ 112260684Skaiw size_t e_rawsymtabsz; 113260684Skaiw Elf_Arsym *e_symtab; 114260684Skaiw size_t e_symtabsz; 115260684Skaiw } e_ar; 116260684Skaiw struct { /* regular ELF files */ 117260684Skaiw union { 118260684Skaiw Elf32_Ehdr *e_ehdr32; 119260684Skaiw Elf64_Ehdr *e_ehdr64; 120260684Skaiw } e_ehdr; 121260684Skaiw union { 122260684Skaiw Elf32_Phdr *e_phdr32; 123260684Skaiw Elf64_Phdr *e_phdr64; 124260684Skaiw } e_phdr; 125260684Skaiw STAILQ_HEAD(, _Elf_Scn) e_scn; /* section list */ 126260684Skaiw size_t e_nphdr; /* number of Phdr entries */ 127260684Skaiw size_t e_nscn; /* number of sections */ 128260684Skaiw size_t e_strndx; /* string table section index */ 129260684Skaiw } e_elf; 130260684Skaiw } e_u; 131260684Skaiw}; 132260684Skaiw 133260684Skaiw/* 134260684Skaiw * The internal descriptor wrapping the "Elf_Data" type. 135260684Skaiw */ 136260684Skaiwstruct _Libelf_Data { 137260684Skaiw Elf_Data d_data; /* The exported descriptor. */ 138260684Skaiw Elf_Scn *d_scn; /* The containing section */ 139260684Skaiw unsigned int d_flags; 140260684Skaiw STAILQ_ENTRY(_Libelf_Data) d_next; 141260684Skaiw}; 142260684Skaiw 143260684Skaiwstruct _Elf_Scn { 144260684Skaiw union { 145260684Skaiw Elf32_Shdr s_shdr32; 146260684Skaiw Elf64_Shdr s_shdr64; 147260684Skaiw } s_shdr; 148260684Skaiw STAILQ_HEAD(, _Libelf_Data) s_data; /* translated data */ 149260684Skaiw STAILQ_HEAD(, _Libelf_Data) s_rawdata; /* raw data */ 150260684Skaiw STAILQ_ENTRY(_Elf_Scn) s_next; 151260684Skaiw struct _Elf *s_elf; /* parent ELF descriptor */ 152260684Skaiw unsigned int s_flags; /* flags for the section as a whole */ 153260684Skaiw size_t s_ndx; /* index# for this section */ 154260684Skaiw uint64_t s_offset; /* managed by elf_update() */ 155260684Skaiw uint64_t s_rawoff; /* original offset in the file */ 156260684Skaiw uint64_t s_size; /* managed by elf_update() */ 157260684Skaiw}; 158260684Skaiw 159260684Skaiw 160260684Skaiwenum { 161260684Skaiw ELF_TOFILE, 162260684Skaiw ELF_TOMEMORY 163260684Skaiw}; 164260684Skaiw 165276371Semaste 166276371Semaste/* 167276371Semaste * The LIBELF_COPY macros are used to copy fields from a GElf_* 168276371Semaste * structure to their 32-bit counterparts, while checking for out of 169276371Semaste * range values. 170276371Semaste * 171276371Semaste * - LIBELF_COPY_U32 :: copy an unsigned 32 bit field. 172276371Semaste * - LIBELF_COPY_S32 :: copy a signed 32 bit field. 173276371Semaste */ 174276371Semaste 175276371Semaste#define LIBELF_COPY_U32(DST, SRC, NAME) do { \ 176276371Semaste if ((SRC)->NAME > UINT32_MAX) { \ 177276371Semaste LIBELF_SET_ERROR(RANGE, 0); \ 178276371Semaste return (0); \ 179276371Semaste } \ 180276371Semaste (DST)->NAME = (SRC)->NAME & 0xFFFFFFFFU; \ 181260684Skaiw } while (0) 182260684Skaiw 183276371Semaste#define LIBELF_COPY_S32(DST, SRC, NAME) do { \ 184276371Semaste if ((SRC)->NAME > INT32_MAX || \ 185276371Semaste (SRC)->NAME < INT32_MIN) { \ 186276371Semaste LIBELF_SET_ERROR(RANGE, 0); \ 187276371Semaste return (0); \ 188276371Semaste } \ 189276371Semaste (DST)->NAME = (int32_t) (SRC)->NAME; \ 190260684Skaiw } while (0) 191260684Skaiw 192260684Skaiw 193260684Skaiw/* 194260684Skaiw * Function Prototypes. 195260684Skaiw */ 196260684Skaiw 197260684Skaiw__BEGIN_DECLS 198260684Skaiwstruct _Libelf_Data *_libelf_allocate_data(Elf_Scn *_s); 199260684SkaiwElf *_libelf_allocate_elf(void); 200260684SkaiwElf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx); 201260684SkaiwElf_Arhdr *_libelf_ar_gethdr(Elf *_e); 202260684SkaiwElf *_libelf_ar_open(Elf *_e, int _reporterror); 203260684SkaiwElf *_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar); 204260684SkaiwElf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst); 205260684SkaiwElf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst); 206276371Semastelong _libelf_checksum(Elf *_e, int _elfclass); 207260684Skaiwvoid *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate); 208276371Semasteunsigned int _libelf_falign(Elf_Type _t, int _elfclass); 209260684Skaiwsize_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version, 210260684Skaiw size_t count); 211260684Skaiwint (*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass)) 212276371Semaste (unsigned char *_dst, size_t dsz, unsigned char *_src, 213276371Semaste size_t _cnt, int _byteswap); 214260684Skaiwvoid *_libelf_getphdr(Elf *_e, int _elfclass); 215260684Skaiwvoid *_libelf_getshdr(Elf_Scn *_scn, int _elfclass); 216260684Skaiwvoid _libelf_init_elf(Elf *_e, Elf_Kind _kind); 217260684Skaiwint _libelf_load_section_headers(Elf *e, void *ehdr); 218276371Semasteunsigned int _libelf_malign(Elf_Type _t, int _elfclass); 219276371SemasteElf *_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror); 220260684Skaiwsize_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version); 221260684Skaiwvoid *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count); 222260684SkaiwElf *_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror); 223260684Skaiwstruct _Libelf_Data *_libelf_release_data(struct _Libelf_Data *_d); 224260684SkaiwElf *_libelf_release_elf(Elf *_e); 225260684SkaiwElf_Scn *_libelf_release_scn(Elf_Scn *_s); 226260684Skaiwint _libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum); 227260684Skaiwint _libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum); 228260684Skaiwint _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass, 229260684Skaiw size_t _shstrndx); 230260684SkaiwElf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s, 231260684Skaiw unsigned int _encoding, int _elfclass, int _direction); 232260684Skaiwint _libelf_xlate_shtype(uint32_t _sht); 233260684Skaiw__END_DECLS 234260684Skaiw 235260684Skaiw#endif /* __LIBELF_H_ */ 236