Path: blob/main/contrib/elftoolchain/libelf/_libelf.h
39483 views
/*-1* Copyright (c) 2006,2008-2011 Joseph Koshy2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25* $Id: _libelf.h 3738 2019-05-05 21:49:06Z jkoshy $26*/2728#ifndef __LIBELF_H_29#define __LIBELF_H_3031#include <sys/queue.h>32#include <sys/tree.h>3334#include "_libelf_config.h"3536#include "_elftc.h"3738/*39* Library-private data structures.40*/4142#define LIBELF_MSG_SIZE 2564344struct _libelf_globals {45int libelf_arch;46unsigned int libelf_byteorder;47int libelf_class;48int libelf_error;49int libelf_fillchar;50unsigned int libelf_version;51unsigned char libelf_msg[LIBELF_MSG_SIZE];52};5354extern struct _libelf_globals _libelf;5556#define LIBELF_PRIVATE(N) (_libelf.libelf_##N)5758#define LIBELF_ELF_ERROR_MASK 0xFF59#define LIBELF_OS_ERROR_SHIFT 86061#define LIBELF_ERROR(E, O) (((E) & LIBELF_ELF_ERROR_MASK) | \62((O) << LIBELF_OS_ERROR_SHIFT))6364#define LIBELF_SET_ERROR(E, O) do { \65LIBELF_PRIVATE(error) = LIBELF_ERROR(ELF_E_##E, (O)); \66} while (0)6768#define LIBELF_ADJUST_AR_SIZE(S) (((S) + 1U) & ~1U)6970/*71* Flags for library internal use. These use the upper 16 bits of the72* `e_flags' field.73*/74#define LIBELF_F_API_MASK 0x00FFFFU /* Flags defined by the API. */75#define LIBELF_F_AR_HEADER 0x010000U /* translated header available */76#define LIBELF_F_AR_VARIANT_SVR4 0x020000U /* BSD style ar(1) archive */77#define LIBELF_F_DATA_MALLOCED 0x040000U /* whether data was malloc'ed */78#define LIBELF_F_RAWFILE_MALLOC 0x080000U /* whether e_rawfile was malloc'ed */79#define LIBELF_F_RAWFILE_MMAP 0x100000U /* whether e_rawfile was mmap'ed */80#define LIBELF_F_SHDRS_LOADED 0x200000U /* whether all shdrs were read in */81#define LIBELF_F_SPECIAL_FILE 0x400000U /* non-regular file */8283RB_HEAD(scntree, _Elf_Scn);84RB_PROTOTYPE(scntree, _Elf_Scn, e_scn, elfscn_cmp);8586struct _Elf {87int e_activations; /* activation count */88unsigned int e_byteorder; /* ELFDATA* */89int e_class; /* ELFCLASS* */90Elf_Cmd e_cmd; /* ELF_C_* used at creation time */91int e_fd; /* associated file descriptor */92unsigned int e_flags; /* ELF_F_* & LIBELF_F_* flags */93Elf_Kind e_kind; /* ELF_K_* */94Elf *e_parent; /* non-NULL for archive members */95unsigned char *e_rawfile; /* uninterpreted bytes */96off_t e_rawsize; /* size of uninterpreted bytes */97unsigned int e_version; /* file version */9899/*100* Header information for archive members. See the101* LIBELF_F_AR_HEADER flag.102*/103union {104Elf_Arhdr *e_arhdr; /* translated header */105unsigned char *e_rawhdr; /* untranslated header */106} e_hdr;107108union {109struct { /* ar(1) archives */110off_t e_next; /* set by elf_rand()/elf_next() */111int e_nchildren;112unsigned char *e_rawstrtab; /* file name strings */113size_t e_rawstrtabsz;114unsigned char *e_rawsymtab; /* symbol table */115size_t e_rawsymtabsz;116Elf_Arsym *e_symtab;117size_t e_symtabsz;118} e_ar;119struct { /* regular ELF files */120union {121Elf32_Ehdr *e_ehdr32;122Elf64_Ehdr *e_ehdr64;123} e_ehdr;124union {125Elf32_Phdr *e_phdr32;126Elf64_Phdr *e_phdr64;127} e_phdr;128struct scntree e_scn; /* sections */129size_t e_nphdr; /* number of Phdr entries */130size_t e_nscn; /* number of sections */131size_t e_strndx; /* string table section index */132} e_elf;133} e_u;134};135136/*137* The internal descriptor wrapping the "Elf_Data" type.138*/139struct _Libelf_Data {140Elf_Data d_data; /* The exported descriptor. */141Elf_Scn *d_scn; /* The containing section */142unsigned int d_flags;143STAILQ_ENTRY(_Libelf_Data) d_next;144};145146struct _Elf_Scn {147union {148Elf32_Shdr s_shdr32;149Elf64_Shdr s_shdr64;150} s_shdr;151STAILQ_HEAD(, _Libelf_Data) s_data; /* translated data */152STAILQ_HEAD(, _Libelf_Data) s_rawdata; /* raw data */153RB_ENTRY(_Elf_Scn) s_tree;154struct _Elf *s_elf; /* parent ELF descriptor */155unsigned int s_flags; /* flags for the section as a whole */156size_t s_ndx; /* index# for this section */157uint64_t s_offset; /* managed by elf_update() */158uint64_t s_rawoff; /* original offset in the file */159uint64_t s_size; /* managed by elf_update() */160};161162163enum {164ELF_TOFILE,165ELF_TOMEMORY166};167168169/*170* The LIBELF_COPY macros are used to copy fields from a GElf_*171* structure to their 32-bit counterparts, while checking for out of172* range values.173*174* - LIBELF_COPY_U32 :: copy an unsigned 32 bit field.175* - LIBELF_COPY_S32 :: copy a signed 32 bit field.176*/177178#define LIBELF_COPY_U32(DST, SRC, NAME) do { \179if ((SRC)->NAME > UINT32_MAX) { \180LIBELF_SET_ERROR(RANGE, 0); \181return (0); \182} \183(DST)->NAME = (SRC)->NAME & 0xFFFFFFFFU; \184} while (0)185186#define LIBELF_COPY_S32(DST, SRC, NAME) do { \187if ((SRC)->NAME > INT32_MAX || \188(SRC)->NAME < INT32_MIN) { \189LIBELF_SET_ERROR(RANGE, 0); \190return (0); \191} \192(DST)->NAME = (int32_t) (SRC)->NAME; \193} while (0)194195196/*197* Function Prototypes.198*/199200typedef int _libelf_translator_function(unsigned char *_dst, size_t dsz,201unsigned char *_src, size_t _cnt, int _byteswap);202203#ifdef __cplusplus204extern "C" {205#endif206struct _Libelf_Data *_libelf_allocate_data(Elf_Scn *_s);207Elf *_libelf_allocate_elf(void);208Elf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx);209Elf_Arhdr *_libelf_ar_gethdr(Elf *_e);210Elf *_libelf_ar_open(Elf *_e, int _reporterror);211Elf *_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);212Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst);213Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst);214long _libelf_checksum(Elf *_e, int _elfclass);215void *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);216int _libelf_elfmachine(Elf *_e);217unsigned int _libelf_falign(Elf_Type _t, int _elfclass);218size_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version,219size_t count);220_libelf_translator_function *_libelf_get_translator(Elf_Type _t,221int _direction, int _elfclass, int _elfmachine);222void *_libelf_getchdr(Elf_Scn *_e, int _elfclass);223void *_libelf_getphdr(Elf *_e, int _elfclass);224void *_libelf_getshdr(Elf_Scn *_scn, int _elfclass);225void _libelf_init_elf(Elf *_e, Elf_Kind _kind);226int _libelf_is_mips64el(Elf *e);227int _libelf_load_section_headers(Elf *e, void *ehdr);228unsigned int _libelf_malign(Elf_Type _t, int _elfclass);229Elf *_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror);230size_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);231void *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);232Elf *_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror);233Elf64_Xword _libelf_mips64el_r_info_tof(Elf64_Xword r_info);234Elf64_Xword _libelf_mips64el_r_info_tom(Elf64_Xword r_info);235struct _Libelf_Data *_libelf_release_data(struct _Libelf_Data *_d);236void _libelf_release_elf(Elf *_e);237Elf_Scn *_libelf_release_scn(Elf_Scn *_s);238int _libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum);239int _libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum);240int _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass,241size_t _shstrndx);242Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s,243unsigned int _encoding, int _elfclass, int _elfmachine, int _direction);244int _libelf_xlate_shtype(uint32_t _sht);245#ifdef __cplusplus246}247#endif248249#endif /* __LIBELF_H_ */250251252