_libelf.h revision 256281
1171568Sscottl/*- 2211095Sdes * Copyright (c) 2006 Joseph Koshy 3171568Sscottl * All rights reserved. 4171568Sscottl * 5171568Sscottl * Redistribution and use in source and binary forms, with or without 6171568Sscottl * modification, are permitted provided that the following conditions 7171568Sscottl * are met: 8171568Sscottl * 1. Redistributions of source code must retain the above copyright 9171568Sscottl * notice, this list of conditions and the following disclaimer. 10171568Sscottl * 2. Redistributions in binary form must reproduce the above copyright 11171568Sscottl * notice, this list of conditions and the following disclaimer in the 12171568Sscottl * documentation and/or other materials provided with the distribution. 13171568Sscottl * 14171568Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15171568Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16171568Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17171568Sscottl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18171568Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19171568Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20171568Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21171568Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22171568Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23171568Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24171568Sscottl * SUCH DAMAGE. 25171568Sscottl * 26171568Sscottl * $FreeBSD: stable/10/lib/libelf/_libelf.h 210348 2010-07-21 12:54:34Z kaiw $ 27171568Sscottl */ 28171568Sscottl 29171568Sscottl#ifndef __LIBELF_H_ 30171568Sscottl#define __LIBELF_H_ 31171568Sscottl 32171568Sscottl#include <sys/queue.h> 33171568Sscottl 34171568Sscottl#ifndef NULL 35171568Sscottl#define NULL ((void *) 0) 36171568Sscottl#endif 37171568Sscottl 38171568Sscottl/* 39171568Sscottl * Library-private data structures. 40171568Sscottl */ 41171568Sscottl 42171568Sscottl#define LIBELF_MSG_SIZE 256 43171568Sscottl 44171568Sscottlstruct _libelf_globals { 45171568Sscottl int libelf_arch; 46171568Sscottl unsigned int libelf_byteorder; 47171568Sscottl int libelf_class; 48171568Sscottl int libelf_error; 49171568Sscottl int libelf_fillchar; 50171568Sscottl unsigned int libelf_version; 51171568Sscottl char libelf_msg[LIBELF_MSG_SIZE]; 52171568Sscottl}; 53171568Sscottl 54171568Sscottlextern struct _libelf_globals _libelf; 55211095Sdes 56171568Sscottl#define LIBELF_PRIVATE(N) (_libelf.libelf_##N) 57171568Sscottl 58171568Sscottl#define LIBELF_ELF_ERROR_MASK 0xFF 59171568Sscottl#define LIBELF_OS_ERROR_SHIFT 8 60171568Sscottl 61171568Sscottl#define LIBELF_SET_ERROR(E, O) do { \ 62171568Sscottl LIBELF_PRIVATE(error) = ((ELF_E_##E & LIBELF_ELF_ERROR_MASK)| \ 63171568Sscottl ((O) << LIBELF_OS_ERROR_SHIFT)); \ 64171568Sscottl } while (0) 65171568Sscottl 66171568Sscottl#define LIBELF_ADJUST_AR_SIZE(S) (((S) + 1U) & ~1U) 67171568Sscottl 68171568Sscottl/* 69171568Sscottl * Flags for library internal use. These use the upper 16 bits of a 70171568Sscottl * flags field. 71171568Sscottl */ 72171568Sscottl#define LIBELF_F_MALLOCED 0x010000 /* whether data was malloc'ed */ 73171568Sscottl#define LIBELF_F_MMAP 0x020000 /* whether e_rawfile was mmap'ed */ 74171568Sscottl#define LIBELF_F_SHDRS_LOADED 0x040000 /* whether all shdrs were read in */ 75171568Sscottl 76171568Sscottlstruct _Elf { 77171568Sscottl int e_activations; /* activation count */ 78171568Sscottl Elf_Arhdr *e_arhdr; /* header for archive members */ 79171568Sscottl unsigned int e_byteorder; /* ELFDATA* */ 80171568Sscottl int e_class; /* ELFCLASS* */ 81171568Sscottl Elf_Cmd e_cmd; /* ELF_C_* used at creation time */ 82171568Sscottl int e_fd; /* associated file descriptor */ 83171568Sscottl unsigned int e_flags; /* ELF_F_*, LIBELF_F_* flags */ 84171568Sscottl Elf_Kind e_kind; /* ELF_K_* */ 85171568Sscottl Elf *e_parent; /* non-NULL for archive members */ 86171568Sscottl char *e_rawfile; /* uninterpreted bytes */ 87171568Sscottl size_t e_rawsize; /* size of uninterpreted bytes */ 88171568Sscottl unsigned int e_version; /* file version */ 89171568Sscottl 90171568Sscottl union { 91171568Sscottl struct { /* ar(1) archives */ 92171568Sscottl off_t e_next; /* set by elf_rand()/elf_next() */ 93171568Sscottl int e_nchildren; 94171568Sscottl char *e_rawstrtab; /* file name strings */ 95171568Sscottl size_t e_rawstrtabsz; 96171568Sscottl char *e_rawsymtab; /* symbol table */ 97171568Sscottl size_t e_rawsymtabsz; 98171568Sscottl Elf_Arsym *e_symtab; 99171568Sscottl size_t e_symtabsz; 100171568Sscottl } e_ar; 101171568Sscottl struct { /* regular ELF files */ 102171568Sscottl union { 103171568Sscottl Elf32_Ehdr *e_ehdr32; 104171568Sscottl Elf64_Ehdr *e_ehdr64; 105171568Sscottl } e_ehdr; 106171568Sscottl union { 107171568Sscottl Elf32_Phdr *e_phdr32; 108171568Sscottl Elf64_Phdr *e_phdr64; 109171568Sscottl } e_phdr; 110171568Sscottl STAILQ_HEAD(, _Elf_Scn) e_scn; /* section list */ 111171568Sscottl size_t e_nphdr; /* number of Phdr entries */ 112171568Sscottl size_t e_nscn; /* number of sections */ 113171568Sscottl size_t e_strndx; /* string table section index */ 114171568Sscottl } e_elf; 115171568Sscottl } e_u; 116171568Sscottl}; 117171568Sscottl 118171568Sscottlstruct _Elf_Scn { 119171568Sscottl union { 120171568Sscottl Elf32_Shdr s_shdr32; 121171568Sscottl Elf64_Shdr s_shdr64; 122171568Sscottl } s_shdr; 123171568Sscottl STAILQ_HEAD(, _Elf_Data) s_data; /* list of Elf_Data descriptors */ 124171568Sscottl STAILQ_HEAD(, _Elf_Data) s_rawdata; /* raw data for this section */ 125171568Sscottl STAILQ_ENTRY(_Elf_Scn) s_next; 126171568Sscottl struct _Elf *s_elf; /* parent ELF descriptor */ 127171568Sscottl unsigned int s_flags; /* flags for the section as a whole */ 128171568Sscottl size_t s_ndx; /* index# for this section */ 129171568Sscottl uint64_t s_offset; /* managed by elf_update() */ 130171568Sscottl uint64_t s_rawoff; /* original offset in the file */ 131171568Sscottl uint64_t s_size; /* managed by elf_update() */ 132171568Sscottl}; 133171568Sscottl 134171568Sscottl 135171568Sscottlenum { 136171568Sscottl ELF_TOFILE, 137171568Sscottl ELF_TOMEMORY 138171568Sscottl}; 139171568Sscottl 140171568Sscottl#define LIBELF_COPY_U32(DST,SRC,NAME) do { \ 141171568Sscottl if ((SRC)->NAME > UINT_MAX) { \ 142171568Sscottl LIBELF_SET_ERROR(RANGE, 0); \ 143171568Sscottl return (0); \ 144171568Sscottl } \ 145171568Sscottl (DST)->NAME = (SRC)->NAME; \ 146171568Sscottl } while (0) 147171568Sscottl 148171568Sscottl#define LIBELF_COPY_S32(DST,SRC,NAME) do { \ 149171568Sscottl if ((SRC)->NAME > INT_MAX || \ 150171568Sscottl (SRC)->NAME < INT_MIN) { \ 151171568Sscottl LIBELF_SET_ERROR(RANGE, 0); \ 152171568Sscottl return (0); \ 153171568Sscottl } \ 154171568Sscottl (DST)->NAME = (SRC)->NAME; \ 155211095Sdes } while (0) 156171568Sscottl 157171568Sscottl 158171568Sscottl/* 159171568Sscottl * Prototypes 160171568Sscottl */ 161171568Sscottl 162171568SscottlElf_Data *_libelf_allocate_data(Elf_Scn *_s); 163171568SscottlElf *_libelf_allocate_elf(void); 164171568SscottlElf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx); 165171568SscottlElf_Arhdr *_libelf_ar_gethdr(Elf *_e); 166171568SscottlElf *_libelf_ar_open(Elf *_e); 167171568SscottlElf *_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar); 168171568Sscottlint _libelf_ar_get_member(char *_s, size_t _sz, int _base, size_t *_ret); 169171568Sscottlchar *_libelf_ar_get_string(const char *_buf, size_t _sz, int _rawname); 170171568Sscottlchar *_libelf_ar_get_name(char *_buf, size_t _sz, Elf *_e); 171171568Sscottlint _libelf_ar_get_number(char *_buf, size_t _sz, int _base, size_t *_ret); 172171568SscottlElf_Arsym *_libelf_ar_process_symtab(Elf *_ar, size_t *_dst); 173171568Sscottlunsigned long _libelf_checksum(Elf *_e, int _elfclass); 174171568Sscottlvoid *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate); 175171568Sscottlint _libelf_falign(Elf_Type _t, int _elfclass); 176171568Sscottlsize_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version, 177171568Sscottl size_t count); 178171568Sscottlint (*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass)) 179171568Sscottl (char *_dst, size_t dsz, char *_src, size_t _cnt, int _byteswap); 180171568Sscottlvoid *_libelf_getphdr(Elf *_e, int _elfclass); 181171568Sscottlvoid *_libelf_getshdr(Elf_Scn *_scn, int _elfclass); 182171568Sscottlvoid _libelf_init_elf(Elf *_e, Elf_Kind _kind); 183171568Sscottlint _libelf_load_scn(Elf *e, void *ehdr); 184171568Sscottlint _libelf_malign(Elf_Type _t, int _elfclass); 185171568Sscottlsize_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version); 186171568Sscottlvoid *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count); 187171568SscottlElf_Data *_libelf_release_data(Elf_Data *_d); 188171568SscottlElf *_libelf_release_elf(Elf *_e); 189171568SscottlElf_Scn *_libelf_release_scn(Elf_Scn *_s); 190171568Sscottlint _libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum); 191171568Sscottlint _libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum); 192171568Sscottlint _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass, 193171568Sscottl size_t _shstrndx); 194171568SscottlElf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s, 195171568Sscottl unsigned int _encoding, int _elfclass, int _direction); 196171568Sscottlint _libelf_xlate_shtype(uint32_t _sht); 197171568Sscottl 198171568Sscottl#endif /* __LIBELF_H_ */ 199171568Sscottl