/*1* Copyright (c) Christos Zoulas 2003.2* 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 immediately at the beginning of the file, without modification,9* this list of conditions, and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR18* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/26/*27* @(#)Id: readelf.h,v 1.9 2002/05/16 18:45:56 christos Exp28*29* Provide elf data structures for non-elf machines, allowing file30* non-elf hosts to determine if an elf binary is stripped.31* Note: cobbled from the linux header file, with modifications32*/33#ifndef __fake_elf_h__34#define __fake_elf_h__3536#if HAVE_STDINT_H37#include <stdint.h>38#endif3940typedef uint32_t Elf32_Addr;41typedef uint32_t Elf32_Off;42typedef uint16_t Elf32_Half;43typedef uint32_t Elf32_Word;44typedef uint8_t Elf32_Char;4546typedef uint64_t Elf64_Addr;47typedef uint64_t Elf64_Off;48typedef uint64_t Elf64_Xword;49typedef uint16_t Elf64_Half;50typedef uint32_t Elf64_Word;51typedef uint8_t Elf64_Char;5253#define EI_NIDENT 165455typedef struct {56Elf32_Word a_type; /* 32-bit id */57Elf32_Word a_v; /* 32-bit id */58} Aux32Info;5960typedef struct {61Elf64_Xword a_type; /* 64-bit id */62Elf64_Xword a_v; /* 64-bit id */63} Aux64Info;6465#define AT_NULL 0 /* end of vector */66#define AT_IGNORE 1 /* entry should be ignored */67#define AT_EXECFD 2 /* file descriptor of program */68#define AT_PHDR 3 /* program headers for program */69#define AT_PHENT 4 /* size of program header entry */70#define AT_PHNUM 5 /* number of program headers */71#define AT_PAGESZ 6 /* system page size */72#define AT_BASE 7 /* base address of interpreter */73#define AT_FLAGS 8 /* flags */74#define AT_ENTRY 9 /* entry point of program */75#define AT_LINUX_NOTELF 10 /* program is not ELF */76#define AT_LINUX_UID 11 /* real uid */77#define AT_LINUX_EUID 12 /* effective uid */78#define AT_LINUX_GID 13 /* real gid */79#define AT_LINUX_EGID 14 /* effective gid */80#define AT_LINUX_PLATFORM 15 /* string identifying CPU for optimizations */81#define AT_LINUX_HWCAP 16 /* arch dependent hints at CPU capabilities */82#define AT_LINUX_CLKTCK 17 /* frequency at which times() increments */83/* AT_* values 18 through 22 are reserved */84#define AT_LINUX_SECURE 23 /* secure mode boolean */85#define AT_LINUX_BASE_PLATFORM 24 /* string identifying real platform, may86* differ from AT_PLATFORM. */87#define AT_LINUX_RANDOM 25 /* address of 16 random bytes */88#define AT_LINUX_HWCAP2 26 /* extension of AT_HWCAP */89#define AT_LINUX_EXECFN 31 /* filename of program */9091typedef struct {92Elf32_Char e_ident[EI_NIDENT];93Elf32_Half e_type;94Elf32_Half e_machine;95Elf32_Word e_version;96Elf32_Addr e_entry; /* Entry point */97Elf32_Off e_phoff;98Elf32_Off e_shoff;99Elf32_Word e_flags;100Elf32_Half e_ehsize;101Elf32_Half e_phentsize;102Elf32_Half e_phnum;103Elf32_Half e_shentsize;104Elf32_Half e_shnum;105Elf32_Half e_shstrndx;106} Elf32_Ehdr;107108typedef struct {109Elf64_Char e_ident[EI_NIDENT];110Elf64_Half e_type;111Elf64_Half e_machine;112Elf64_Word e_version;113Elf64_Addr e_entry; /* Entry point */114Elf64_Off e_phoff;115Elf64_Off e_shoff;116Elf64_Word e_flags;117Elf64_Half e_ehsize;118Elf64_Half e_phentsize;119Elf64_Half e_phnum;120Elf64_Half e_shentsize;121Elf64_Half e_shnum;122Elf64_Half e_shstrndx;123} Elf64_Ehdr;124125/* e_type */126#define ET_REL 1127#define ET_EXEC 2128#define ET_DYN 3129#define ET_CORE 4130131/* e_machine (used only for SunOS 5.x hardware capabilities) */132#define EM_SPARC 2133#define EM_386 3134#define EM_SPARC32PLUS 18135#define EM_SPARCV9 43136#define EM_IA_64 50137#define EM_AMD64 62138139/* sh_type */140#define SHT_SYMTAB 2141#define SHT_NOTE 7142#define SHT_DYNSYM 11143#define SHT_SUNW_cap 0x6ffffff5 /* SunOS 5.x hw/sw capabilities */144145/* elf type */146#define ELFDATANONE 0 /* e_ident[EI_DATA] */147#define ELFDATA2LSB 1148#define ELFDATA2MSB 2149150/* elf class */151#define ELFCLASSNONE 0152#define ELFCLASS32 1153#define ELFCLASS64 2154155/* magic number */156#define EI_MAG0 0 /* e_ident[] indexes */157#define EI_MAG1 1158#define EI_MAG2 2159#define EI_MAG3 3160#define EI_CLASS 4161#define EI_DATA 5162#define EI_VERSION 6163#define EI_PAD 7164165#define ELFMAG0 0x7f /* EI_MAG */166#define ELFMAG1 'E'167#define ELFMAG2 'L'168#define ELFMAG3 'F'169#define ELFMAG "\177ELF"170171#define OLFMAG1 'O'172#define OLFMAG "\177OLF"173174typedef struct {175Elf32_Word p_type;176Elf32_Off p_offset;177Elf32_Addr p_vaddr;178Elf32_Addr p_paddr;179Elf32_Word p_filesz;180Elf32_Word p_memsz;181Elf32_Word p_flags;182Elf32_Word p_align;183} Elf32_Phdr;184185typedef struct {186Elf64_Word p_type;187Elf64_Word p_flags;188Elf64_Off p_offset;189Elf64_Addr p_vaddr;190Elf64_Addr p_paddr;191Elf64_Xword p_filesz;192Elf64_Xword p_memsz;193Elf64_Xword p_align;194} Elf64_Phdr;195196#define PT_NULL 0 /* p_type */197#define PT_LOAD 1198#define PT_DYNAMIC 2199#define PT_INTERP 3200#define PT_NOTE 4201#define PT_SHLIB 5202#define PT_PHDR 6203#define PT_NUM 7204205typedef struct {206Elf32_Word sh_name;207Elf32_Word sh_type;208Elf32_Word sh_flags;209Elf32_Addr sh_addr;210Elf32_Off sh_offset;211Elf32_Word sh_size;212Elf32_Word sh_link;213Elf32_Word sh_info;214Elf32_Word sh_addralign;215Elf32_Word sh_entsize;216} Elf32_Shdr;217218typedef struct {219Elf64_Word sh_name;220Elf64_Word sh_type;221Elf64_Off sh_flags;222Elf64_Addr sh_addr;223Elf64_Off sh_offset;224Elf64_Off sh_size;225Elf64_Word sh_link;226Elf64_Word sh_info;227Elf64_Off sh_addralign;228Elf64_Off sh_entsize;229} Elf64_Shdr;230231#define NT_NETBSD_CORE_PROCINFO 1232#define NT_NETBSD_CORE_AUXV 2233234struct NetBSD_elfcore_procinfo {235/* Version 1 fields start here. */236uint32_t cpi_version; /* our version */237uint32_t cpi_cpisize; /* sizeof(this struct) */238uint32_t cpi_signo; /* killing signal */239uint32_t cpi_sigcode; /* signal code */240uint32_t cpi_sigpend[4]; /* pending signals */241uint32_t cpi_sigmask[4]; /* blocked signals */242uint32_t cpi_sigignore[4]; /* ignored signals */243uint32_t cpi_sigcatch[4]; /* caught signals */244int32_t cpi_pid; /* process ID */245int32_t cpi_ppid; /* parent process ID */246int32_t cpi_pgrp; /* process group ID */247int32_t cpi_sid; /* session ID */248uint32_t cpi_ruid; /* real user ID */249uint32_t cpi_euid; /* effective user ID */250uint32_t cpi_svuid; /* saved user ID */251uint32_t cpi_rgid; /* real group ID */252uint32_t cpi_egid; /* effective group ID */253uint32_t cpi_svgid; /* saved group ID */254uint32_t cpi_nlwps; /* number of LWPs */255int8_t cpi_name[32]; /* copy of p->p_comm */256/* Add version 2 fields below here. */257int32_t cpi_siglwp; /* LWP target of killing signal */258};259260/* Note header in a PT_NOTE section */261typedef struct elf_note {262Elf32_Word n_namesz; /* Name size */263Elf32_Word n_descsz; /* Content size */264Elf32_Word n_type; /* Content type */265} Elf32_Nhdr;266267typedef struct {268Elf64_Word n_namesz;269Elf64_Word n_descsz;270Elf64_Word n_type;271} Elf64_Nhdr;272273/* Notes used in ET_CORE */274#define NT_PRSTATUS 1275#define NT_PRFPREG 2276#define NT_PRPSINFO 3277#define NT_PRXREG 4278#define NT_TASKSTRUCT 4279#define NT_PLATFORM 5280#define NT_AUXV 6281282/* Note types used in executables */283/* NetBSD executables (name = "NetBSD") */284#define NT_NETBSD_VERSION 1285#define NT_NETBSD_EMULATION 2286#define NT_FREEBSD_VERSION 1287#define NT_OPENBSD_VERSION 1288#define NT_DRAGONFLY_VERSION 1289/*290* GNU executables (name = "GNU")291* word[0]: GNU OS tags292* word[1]: major version293* word[2]: minor version294* word[3]: tiny version295*/296#define NT_GNU_VERSION 1297298/* GNU OS tags */299#define GNU_OS_LINUX 0300#define GNU_OS_HURD 1301#define GNU_OS_SOLARIS 2302#define GNU_OS_KFREEBSD 3303#define GNU_OS_KNETBSD 4304305/*306* GNU Hardware capability information307* word[0]: Number of entries308* word[1]: Bitmask of enabled entries309* Followed by a byte id, and a NUL terminated string per entry310*/311#define NT_GNU_HWCAP 2312313/*314* GNU Build ID generated by ld315* 160 bit SHA1 [default]316* 128 bit md5 or uuid317*/318#define NT_GNU_BUILD_ID 3319320/*321* NetBSD-specific note type: PaX.322* There should be 1 NOTE per executable.323* name: PaX\0324* namesz: 4325* desc:326* word[0]: capability bitmask327* descsz: 4328*/329#define NT_NETBSD_PAX 3330#define NT_NETBSD_PAX_MPROTECT 0x01 /* Force enable Mprotect */331#define NT_NETBSD_PAX_NOMPROTECT 0x02 /* Force disable Mprotect */332#define NT_NETBSD_PAX_GUARD 0x04 /* Force enable Segvguard */333#define NT_NETBSD_PAX_NOGUARD 0x08 /* Force disable Servguard */334#define NT_NETBSD_PAX_ASLR 0x10 /* Force enable ASLR */335#define NT_NETBSD_PAX_NOASLR 0x20 /* Force disable ASLR */336337/*338* NetBSD-specific note type: MACHINE_ARCH.339* There should be 1 NOTE per executable.340* name: NetBSD\0341* namesz: 7342* desc: string343* descsz: variable344*/345#define NT_NETBSD_MARCH 5346347/*348* NetBSD-specific note type: COMPILER MODEL.349* There should be 1 NOTE per executable.350* name: NetBSD\0351* namesz: 7352* desc: string353* descsz: variable354*/355#define NT_NETBSD_CMODEL 6356357/*358* Golang-specific note type359* name: Go\0\0360* namesz: 4361* desc: base-64 build id.362* descsz: < 128363*/364#define NT_GO_BUILD_ID 4365366/*367* Android-specific note type: ident368* name: Android\0369* namesz: 8370* desc:371* uint32_t: api_version372* string[64]: NDK Version373* string[64]: version detail374* descsz: < 4+64+64 (4 for < r14)375*/376#define NT_ANDROID_VERSION 1377378#define NT_ANDROID_KUSER 3379380/*381* Android-specific note type: memory tag382* name: Android\0383* namesz: 8384* desc:385* uint32_t: bitmask386* descsz: 4387*/388#define NT_ANDROID_MEMTAG 4389390#define NT_ANDROID_MEMTAG_LEVEL_NONE 0391#define NT_ANDROID_MEMTAG_LEVEL_ASYNC 1392#define NT_ANDROID_MEMTAG_LEVEL_SYNC 2393#define NT_ANDROID_MEMTAG_LEVEL_MASK 3394#define NT_ANDROID_MEMTAG_HEAP 4395#define NT_ANDROID_MEMTAG_STACK 8396397/*398* FreeBSD specific notes399*/400#define NT_FREEBSD_PROCSTAT_AUXV 16401402#if !defined(ELFSIZE) && defined(ARCH_ELFSIZE)403#define ELFSIZE ARCH_ELFSIZE404#endif405/* SunOS 5.x hardware/software capabilities */406typedef struct {407Elf32_Word c_tag;408union {409Elf32_Word c_val;410Elf32_Addr c_ptr;411} c_un;412} Elf32_Cap;413414typedef struct {415Elf64_Xword c_tag;416union {417Elf64_Xword c_val;418Elf64_Addr c_ptr;419} c_un;420} Elf64_Cap;421422/* SunOS 5.x hardware/software capability tags */423#define CA_SUNW_NULL 0424#define CA_SUNW_HW_1 1425#define CA_SUNW_SF_1 2426427/* SunOS 5.x software capabilities */428#define SF1_SUNW_FPKNWN 0x01429#define SF1_SUNW_FPUSED 0x02430#define SF1_SUNW_MASK 0x03431432/* SunOS 5.x hardware capabilities: sparc */433#define AV_SPARC_MUL32 0x0001434#define AV_SPARC_DIV32 0x0002435#define AV_SPARC_FSMULD 0x0004436#define AV_SPARC_V8PLUS 0x0008437#define AV_SPARC_POPC 0x0010438#define AV_SPARC_VIS 0x0020439#define AV_SPARC_VIS2 0x0040440#define AV_SPARC_ASI_BLK_INIT 0x0080441#define AV_SPARC_FMAF 0x0100442#define AV_SPARC_FJFMAU 0x4000443#define AV_SPARC_IMA 0x8000444445/* SunOS 5.x hardware capabilities: 386 */446#define AV_386_FPU 0x00000001447#define AV_386_TSC 0x00000002448#define AV_386_CX8 0x00000004449#define AV_386_SEP 0x00000008450#define AV_386_AMD_SYSC 0x00000010451#define AV_386_CMOV 0x00000020452#define AV_386_MMX 0x00000040453#define AV_386_AMD_MMX 0x00000080454#define AV_386_AMD_3DNow 0x00000100455#define AV_386_AMD_3DNowx 0x00000200456#define AV_386_FXSR 0x00000400457#define AV_386_SSE 0x00000800458#define AV_386_SSE2 0x00001000459#define AV_386_PAUSE 0x00002000460#define AV_386_SSE3 0x00004000461#define AV_386_MON 0x00008000462#define AV_386_CX16 0x00010000463#define AV_386_AHF 0x00020000464#define AV_386_TSCP 0x00040000465#define AV_386_AMD_SSE4A 0x00080000466#define AV_386_POPCNT 0x00100000467#define AV_386_AMD_LZCNT 0x00200000468#define AV_386_SSSE3 0x00400000469#define AV_386_SSE4_1 0x00800000470#define AV_386_SSE4_2 0x01000000471472/*473* Dynamic Section structure array474*/475typedef struct {476Elf32_Word d_tag; /* entry tag value */477union {478Elf32_Addr d_ptr;479Elf32_Word d_val;480} d_un;481} Elf32_Dyn;482483typedef struct {484Elf64_Xword d_tag; /* entry tag value */485union {486Elf64_Addr d_ptr;487Elf64_Xword d_val;488} d_un;489} Elf64_Dyn;490491/* d_tag */492#define DT_NULL 0 /* Marks end of dynamic array */493#define DT_NEEDED 1 /* Name of needed library (DT_STRTAB offset) */494#define DT_PLTRELSZ 2 /* Size, in bytes, of relocations in PLT */495#define DT_PLTGOT 3 /* Address of PLT and/or GOT */496#define DT_HASH 4 /* Address of symbol hash table */497#define DT_STRTAB 5 /* Address of string table */498#define DT_SYMTAB 6 /* Address of symbol table */499#define DT_RELA 7 /* Address of Rela relocation table */500#define DT_RELASZ 8 /* Size, in bytes, of DT_RELA table */501#define DT_RELAENT 9 /* Size, in bytes, of one DT_RELA entry */502#define DT_STRSZ 10 /* Size, in bytes, of DT_STRTAB table */503#define DT_SYMENT 11 /* Size, in bytes, of one DT_SYMTAB entry */504#define DT_INIT 12 /* Address of initialization function */505#define DT_FINI 13 /* Address of termination function */506#define DT_SONAME 14 /* Shared object name (DT_STRTAB offset) */507#define DT_RPATH 15 /* Library search path (DT_STRTAB offset) */508#define DT_SYMBOLIC 16 /* Start symbol search within local object */509#define DT_REL 17 /* Address of Rel relocation table */510#define DT_RELSZ 18 /* Size, in bytes, of DT_REL table */511#define DT_RELENT 19 /* Size, in bytes, of one DT_REL entry */512#define DT_PLTREL 20 /* Type of PLT relocation entries */513#define DT_DEBUG 21 /* Used for debugging; unspecified */514#define DT_TEXTREL 22 /* Relocations might modify non-writable seg */515#define DT_JMPREL 23 /* Address of relocations associated with PLT */516#define DT_BIND_NOW 24 /* Process all relocations at load-time */517#define DT_INIT_ARRAY 25 /* Address of initialization function array */518#define DT_FINI_ARRAY 26 /* Size, in bytes, of DT_INIT_ARRAY array */519#define DT_INIT_ARRAYSZ 27 /* Address of termination function array */520#define DT_FINI_ARRAYSZ 28 /* Size, in bytes, of DT_FINI_ARRAY array*/521#define DT_RUNPATH 29 /* overrides DT_RPATH */522#define DT_FLAGS 30 /* Encodes ORIGIN, SYMBOLIC, TEXTREL, BIND_NOW, STATIC_TLS */523#define DT_ENCODING 31 /* ??? */524#define DT_PREINIT_ARRAY 32 /* Address of pre-init function array */525#define DT_PREINIT_ARRAYSZ 33 /* Size, in bytes, of DT_PREINIT_ARRAY array */526#define DT_NUM 34527528#define DT_LOOS 0x60000000 /* Operating system specific range */529#define DT_VERSYM 0x6ffffff0 /* Symbol versions */530#define DT_FLAGS_1 0x6ffffffb /* ELF dynamic flags */531#define DT_VERDEF 0x6ffffffc /* Versions defined by file */532#define DT_VERDEFNUM 0x6ffffffd /* Number of versions defined by file */533#define DT_VERNEED 0x6ffffffe /* Versions needed by file */534#define DT_VERNEEDNUM 0x6fffffff /* Number of versions needed by file */535#define DT_HIOS 0x6fffffff536#define DT_LOPROC 0x70000000 /* Processor-specific range */537#define DT_HIPROC 0x7fffffff538539/* Flag values for DT_FLAGS */540#define DF_ORIGIN 0x00000001 /* uses $ORIGIN */541#define DF_SYMBOLIC 0x00000002 /* */542#define DF_TEXTREL 0x00000004 /* */543#define DF_BIND_NOW 0x00000008 /* */544#define DF_STATIC_TLS 0x00000010 /* */545546/* Flag values for DT_FLAGS_1 */547#define DF_1_NOW 0x00000001 /* Same as DF_BIND_NOW */548#define DF_1_GLOBAL 0x00000002 /* Unused */549#define DF_1_GROUP 0x00000004 /* Is member of group */550#define DF_1_NODELETE 0x00000008 /* Cannot be deleted from process */551#define DF_1_LOADFLTR 0x00000010 /* Immediate loading of filters */552#define DF_1_INITFIRST 0x00000020 /* init/fini takes priority */553#define DF_1_NOOPEN 0x00000040 /* Do not allow loading on dlopen() */554#define DF_1_ORIGIN 0x00000080 /* Require $ORIGIN processing */555#define DF_1_DIRECT 0x00000100 /* Enable direct bindings */556#define DF_1_INTERPOSE 0x00000400 /* Is an interposer */557#define DF_1_NODEFLIB 0x00000800 /* Ignore default library search path */558#define DF_1_NODUMP 0x00001000 /* Cannot be dumped with dldump(3C) */559#define DF_1_CONFALT 0x00002000 /* Configuration alternative */560#define DF_1_ENDFILTEE 0x00004000 /* Filtee ends filter's search */561#define DF_1_DISPRELDNE 0x00008000 /* Did displacement relocation */562#define DF_1_DISPRELPND 0x00010000 /* Pending displacement relocation */563#define DF_1_NODIRECT 0x00020000 /* Has non-direct bindings */564#define DF_1_IGNMULDEF 0x00040000 /* Used internally */565#define DF_1_NOKSYMS 0x00080000 /* Used internally */566#define DF_1_NOHDR 0x00100000 /* Used internally */567#define DF_1_EDITED 0x00200000 /* Has been modified since build */568#define DF_1_NORELOC 0x00400000 /* Used internally */569#define DF_1_SYMINTPOSE 0x00800000 /* Has individual symbol interposers */570#define DF_1_GLOBAUDIT 0x01000000 /* Require global auditing */571#define DF_1_SINGLETON 0x02000000 /* Has singleton symbols */572#define DF_1_STUB 0x04000000 /* Stub */573#define DF_1_PIE 0x08000000 /* Position Independent Executable */574575#endif576577578