168349Sobrien/* 2133359Sobrien * Copyright (c) Christos Zoulas 2003. 3133359Sobrien * All Rights Reserved. 4133359Sobrien * 5133359Sobrien * Redistribution and use in source and binary forms, with or without 6133359Sobrien * modification, are permitted provided that the following conditions 7133359Sobrien * are met: 8133359Sobrien * 1. Redistributions of source code must retain the above copyright 9133359Sobrien * notice immediately at the beginning of the file, without modification, 10133359Sobrien * this list of conditions, and the following disclaimer. 11133359Sobrien * 2. Redistributions in binary form must reproduce the above copyright 12133359Sobrien * notice, this list of conditions and the following disclaimer in the 13133359Sobrien * documentation and/or other materials provided with the distribution. 14133359Sobrien * 15133359Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16133359Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18133359Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19133359Sobrien * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20133359Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25133359Sobrien * SUCH DAMAGE. 26133359Sobrien */ 27133359Sobrien/* 28133359Sobrien * @(#)Id: readelf.h,v 1.9 2002/05/16 18:45:56 christos Exp 2968349Sobrien * 3068349Sobrien * Provide elf data structures for non-elf machines, allowing file 3168349Sobrien * non-elf hosts to determine if an elf binary is stripped. 3268349Sobrien * Note: cobbled from the linux header file, with modifications 3368349Sobrien */ 3468349Sobrien#ifndef __fake_elf_h__ 35226048Sobrien#define __fake_elf_h__ 3668349Sobrien 37103373Sobrien#if HAVE_STDINT_H 38103373Sobrien#include <stdint.h> 39103373Sobrien#endif 40103373Sobrien 4168349Sobrientypedef uint32_t Elf32_Addr; 4268349Sobrientypedef uint32_t Elf32_Off; 4368349Sobrientypedef uint16_t Elf32_Half; 4468349Sobrientypedef uint32_t Elf32_Word; 4568349Sobrientypedef uint8_t Elf32_Char; 4668349Sobrien 4768349Sobrientypedef uint64_t Elf64_Addr; 4868349Sobrientypedef uint64_t Elf64_Off; 4968349Sobrientypedef uint64_t Elf64_Xword; 5068349Sobrientypedef uint16_t Elf64_Half; 5168349Sobrientypedef uint32_t Elf64_Word; 5268349Sobrientypedef uint8_t Elf64_Char; 5368349Sobrien 54226048Sobrien#define EI_NIDENT 16 5568349Sobrien 5668349Sobrientypedef struct { 57330569Sgordon Elf32_Word a_type; /* 32-bit id */ 58330569Sgordon Elf32_Word a_v; /* 32-bit id */ 59330569Sgordon} Aux32Info; 60330569Sgordon 61330569Sgordontypedef struct { 62330569Sgordon Elf64_Xword a_type; /* 64-bit id */ 63330569Sgordon Elf64_Xword a_v; /* 64-bit id */ 64330569Sgordon} Aux64Info; 65330569Sgordon 66330569Sgordon#define AT_NULL 0 /* end of vector */ 67330569Sgordon#define AT_IGNORE 1 /* entry should be ignored */ 68330569Sgordon#define AT_EXECFD 2 /* file descriptor of program */ 69330569Sgordon#define AT_PHDR 3 /* program headers for program */ 70330569Sgordon#define AT_PHENT 4 /* size of program header entry */ 71330569Sgordon#define AT_PHNUM 5 /* number of program headers */ 72330569Sgordon#define AT_PAGESZ 6 /* system page size */ 73330569Sgordon#define AT_BASE 7 /* base address of interpreter */ 74330569Sgordon#define AT_FLAGS 8 /* flags */ 75330569Sgordon#define AT_ENTRY 9 /* entry point of program */ 76330569Sgordon#define AT_LINUX_NOTELF 10 /* program is not ELF */ 77330569Sgordon#define AT_LINUX_UID 11 /* real uid */ 78330569Sgordon#define AT_LINUX_EUID 12 /* effective uid */ 79330569Sgordon#define AT_LINUX_GID 13 /* real gid */ 80330569Sgordon#define AT_LINUX_EGID 14 /* effective gid */ 81330569Sgordon#define AT_LINUX_PLATFORM 15 /* string identifying CPU for optimizations */ 82330569Sgordon#define AT_LINUX_HWCAP 16 /* arch dependent hints at CPU capabilities */ 83330569Sgordon#define AT_LINUX_CLKTCK 17 /* frequency at which times() increments */ 84330569Sgordon/* AT_* values 18 through 22 are reserved */ 85330569Sgordon#define AT_LINUX_SECURE 23 /* secure mode boolean */ 86330569Sgordon#define AT_LINUX_BASE_PLATFORM 24 /* string identifying real platform, may 87330569Sgordon * differ from AT_PLATFORM. */ 88330569Sgordon#define AT_LINUX_RANDOM 25 /* address of 16 random bytes */ 89330569Sgordon#define AT_LINUX_HWCAP2 26 /* extension of AT_HWCAP */ 90330569Sgordon#define AT_LINUX_EXECFN 31 /* filename of program */ 91330569Sgordon 92330569Sgordontypedef struct { 9368349Sobrien Elf32_Char e_ident[EI_NIDENT]; 9468349Sobrien Elf32_Half e_type; 9568349Sobrien Elf32_Half e_machine; 9668349Sobrien Elf32_Word e_version; 9768349Sobrien Elf32_Addr e_entry; /* Entry point */ 9868349Sobrien Elf32_Off e_phoff; 9968349Sobrien Elf32_Off e_shoff; 10068349Sobrien Elf32_Word e_flags; 10168349Sobrien Elf32_Half e_ehsize; 10268349Sobrien Elf32_Half e_phentsize; 10368349Sobrien Elf32_Half e_phnum; 10468349Sobrien Elf32_Half e_shentsize; 10568349Sobrien Elf32_Half e_shnum; 10668349Sobrien Elf32_Half e_shstrndx; 10768349Sobrien} Elf32_Ehdr; 10868349Sobrien 10968349Sobrientypedef struct { 11068349Sobrien Elf64_Char e_ident[EI_NIDENT]; 11168349Sobrien Elf64_Half e_type; 11268349Sobrien Elf64_Half e_machine; 11368349Sobrien Elf64_Word e_version; 11468349Sobrien Elf64_Addr e_entry; /* Entry point */ 11568349Sobrien Elf64_Off e_phoff; 11668349Sobrien Elf64_Off e_shoff; 11768349Sobrien Elf64_Word e_flags; 11868349Sobrien Elf64_Half e_ehsize; 11968349Sobrien Elf64_Half e_phentsize; 12068349Sobrien Elf64_Half e_phnum; 12168349Sobrien Elf64_Half e_shentsize; 12268349Sobrien Elf64_Half e_shnum; 12368349Sobrien Elf64_Half e_shstrndx; 12468349Sobrien} Elf64_Ehdr; 12568349Sobrien 12668349Sobrien/* e_type */ 127226048Sobrien#define ET_REL 1 128226048Sobrien#define ET_EXEC 2 129226048Sobrien#define ET_DYN 3 130226048Sobrien#define ET_CORE 4 13168349Sobrien 132186690Sobrien/* e_machine (used only for SunOS 5.x hardware capabilities) */ 133186690Sobrien#define EM_SPARC 2 134186690Sobrien#define EM_386 3 135186690Sobrien#define EM_SPARC32PLUS 18 136186690Sobrien#define EM_SPARCV9 43 137186690Sobrien#define EM_IA_64 50 138186690Sobrien#define EM_AMD64 62 139186690Sobrien 14068349Sobrien/* sh_type */ 141226048Sobrien#define SHT_SYMTAB 2 142226048Sobrien#define SHT_NOTE 7 143226048Sobrien#define SHT_DYNSYM 11 144330569Sgordon#define SHT_SUNW_cap 0x6ffffff5 /* SunOS 5.x hw/sw capabilities */ 14568349Sobrien 14668349Sobrien/* elf type */ 147226048Sobrien#define ELFDATANONE 0 /* e_ident[EI_DATA] */ 148226048Sobrien#define ELFDATA2LSB 1 149226048Sobrien#define ELFDATA2MSB 2 15068349Sobrien 15168349Sobrien/* elf class */ 152226048Sobrien#define ELFCLASSNONE 0 153226048Sobrien#define ELFCLASS32 1 154226048Sobrien#define ELFCLASS64 2 15568349Sobrien 15668349Sobrien/* magic number */ 15768349Sobrien#define EI_MAG0 0 /* e_ident[] indexes */ 15868349Sobrien#define EI_MAG1 1 15968349Sobrien#define EI_MAG2 2 16068349Sobrien#define EI_MAG3 3 16168349Sobrien#define EI_CLASS 4 16268349Sobrien#define EI_DATA 5 16368349Sobrien#define EI_VERSION 6 16468349Sobrien#define EI_PAD 7 16568349Sobrien 16668349Sobrien#define ELFMAG0 0x7f /* EI_MAG */ 16768349Sobrien#define ELFMAG1 'E' 16868349Sobrien#define ELFMAG2 'L' 16968349Sobrien#define ELFMAG3 'F' 17068349Sobrien#define ELFMAG "\177ELF" 17168349Sobrien 17268349Sobrien#define OLFMAG1 'O' 17368349Sobrien#define OLFMAG "\177OLF" 17468349Sobrien 17568349Sobrientypedef struct { 17668349Sobrien Elf32_Word p_type; 17768349Sobrien Elf32_Off p_offset; 17868349Sobrien Elf32_Addr p_vaddr; 17968349Sobrien Elf32_Addr p_paddr; 18068349Sobrien Elf32_Word p_filesz; 18168349Sobrien Elf32_Word p_memsz; 18268349Sobrien Elf32_Word p_flags; 18368349Sobrien Elf32_Word p_align; 18468349Sobrien} Elf32_Phdr; 18568349Sobrien 18668349Sobrientypedef struct { 18768349Sobrien Elf64_Word p_type; 18868349Sobrien Elf64_Word p_flags; 18968349Sobrien Elf64_Off p_offset; 19068349Sobrien Elf64_Addr p_vaddr; 19168349Sobrien Elf64_Addr p_paddr; 19268349Sobrien Elf64_Xword p_filesz; 19368349Sobrien Elf64_Xword p_memsz; 19468349Sobrien Elf64_Xword p_align; 19568349Sobrien} Elf64_Phdr; 19668349Sobrien 19768349Sobrien#define PT_NULL 0 /* p_type */ 19868349Sobrien#define PT_LOAD 1 19968349Sobrien#define PT_DYNAMIC 2 20068349Sobrien#define PT_INTERP 3 20168349Sobrien#define PT_NOTE 4 20268349Sobrien#define PT_SHLIB 5 20368349Sobrien#define PT_PHDR 6 20468349Sobrien#define PT_NUM 7 20568349Sobrien 20668349Sobrientypedef struct { 20768349Sobrien Elf32_Word sh_name; 20868349Sobrien Elf32_Word sh_type; 20968349Sobrien Elf32_Word sh_flags; 21068349Sobrien Elf32_Addr sh_addr; 21168349Sobrien Elf32_Off sh_offset; 21268349Sobrien Elf32_Word sh_size; 21368349Sobrien Elf32_Word sh_link; 21468349Sobrien Elf32_Word sh_info; 21568349Sobrien Elf32_Word sh_addralign; 21668349Sobrien Elf32_Word sh_entsize; 21768349Sobrien} Elf32_Shdr; 21868349Sobrien 21968349Sobrientypedef struct { 22068349Sobrien Elf64_Word sh_name; 22168349Sobrien Elf64_Word sh_type; 22268349Sobrien Elf64_Off sh_flags; 22368349Sobrien Elf64_Addr sh_addr; 22468349Sobrien Elf64_Off sh_offset; 22568349Sobrien Elf64_Off sh_size; 22668349Sobrien Elf64_Word sh_link; 22768349Sobrien Elf64_Word sh_info; 22868349Sobrien Elf64_Off sh_addralign; 22968349Sobrien Elf64_Off sh_entsize; 23068349Sobrien} Elf64_Shdr; 23168349Sobrien 232103373Sobrien#define NT_NETBSD_CORE_PROCINFO 1 233330569Sgordon#define NT_NETBSD_CORE_AUXV 2 234103373Sobrien 235330569Sgordonstruct NetBSD_elfcore_procinfo { 236330569Sgordon /* Version 1 fields start here. */ 237330569Sgordon uint32_t cpi_version; /* our version */ 238330569Sgordon uint32_t cpi_cpisize; /* sizeof(this struct) */ 239330569Sgordon uint32_t cpi_signo; /* killing signal */ 240330569Sgordon uint32_t cpi_sigcode; /* signal code */ 241330569Sgordon uint32_t cpi_sigpend[4]; /* pending signals */ 242330569Sgordon uint32_t cpi_sigmask[4]; /* blocked signals */ 243330569Sgordon uint32_t cpi_sigignore[4]; /* ignored signals */ 244330569Sgordon uint32_t cpi_sigcatch[4]; /* caught signals */ 245330569Sgordon int32_t cpi_pid; /* process ID */ 246330569Sgordon int32_t cpi_ppid; /* parent process ID */ 247330569Sgordon int32_t cpi_pgrp; /* process group ID */ 248330569Sgordon int32_t cpi_sid; /* session ID */ 249330569Sgordon uint32_t cpi_ruid; /* real user ID */ 250330569Sgordon uint32_t cpi_euid; /* effective user ID */ 251330569Sgordon uint32_t cpi_svuid; /* saved user ID */ 252330569Sgordon uint32_t cpi_rgid; /* real group ID */ 253330569Sgordon uint32_t cpi_egid; /* effective group ID */ 254330569Sgordon uint32_t cpi_svgid; /* saved group ID */ 255330569Sgordon uint32_t cpi_nlwps; /* number of LWPs */ 256330569Sgordon int8_t cpi_name[32]; /* copy of p->p_comm */ 257330569Sgordon /* Add version 2 fields below here. */ 258330569Sgordon int32_t cpi_siglwp; /* LWP target of killing signal */ 259330569Sgordon}; 260330569Sgordon 26168349Sobrien/* Note header in a PT_NOTE section */ 26268349Sobrientypedef struct elf_note { 26368349Sobrien Elf32_Word n_namesz; /* Name size */ 26468349Sobrien Elf32_Word n_descsz; /* Content size */ 26568349Sobrien Elf32_Word n_type; /* Content type */ 26668349Sobrien} Elf32_Nhdr; 26768349Sobrien 26868349Sobrientypedef struct { 26968349Sobrien Elf64_Word n_namesz; 27068349Sobrien Elf64_Word n_descsz; 27168349Sobrien Elf64_Word n_type; 27268349Sobrien} Elf64_Nhdr; 27368349Sobrien 274175296Sobrien/* Notes used in ET_CORE */ 27568349Sobrien#define NT_PRSTATUS 1 27668349Sobrien#define NT_PRFPREG 2 27768349Sobrien#define NT_PRPSINFO 3 27868349Sobrien#define NT_PRXREG 4 279226048Sobrien#define NT_TASKSTRUCT 4 28068349Sobrien#define NT_PLATFORM 5 28168349Sobrien#define NT_AUXV 6 28268349Sobrien 283103373Sobrien/* Note types used in executables */ 284103373Sobrien/* NetBSD executables (name = "NetBSD") */ 285226048Sobrien#define NT_NETBSD_VERSION 1 286226048Sobrien#define NT_NETBSD_EMULATION 2 287226048Sobrien#define NT_FREEBSD_VERSION 1 288226048Sobrien#define NT_OPENBSD_VERSION 1 289226048Sobrien#define NT_DRAGONFLY_VERSION 1 290226048Sobrien/* 291226048Sobrien * GNU executables (name = "GNU") 292226048Sobrien * word[0]: GNU OS tags 293226048Sobrien * word[1]: major version 294226048Sobrien * word[2]: minor version 295226048Sobrien * word[3]: tiny version 296226048Sobrien */ 297226048Sobrien#define NT_GNU_VERSION 1 298103373Sobrien 299103373Sobrien/* GNU OS tags */ 300226048Sobrien#define GNU_OS_LINUX 0 301226048Sobrien#define GNU_OS_HURD 1 302226048Sobrien#define GNU_OS_SOLARIS 2 303226048Sobrien#define GNU_OS_KFREEBSD 3 304226048Sobrien#define GNU_OS_KNETBSD 4 305103373Sobrien 306226048Sobrien/* 307226048Sobrien * GNU Hardware capability information 308226048Sobrien * word[0]: Number of entries 309226048Sobrien * word[1]: Bitmask of enabled entries 310226048Sobrien * Followed by a byte id, and a NUL terminated string per entry 311226048Sobrien */ 312226048Sobrien#define NT_GNU_HWCAP 2 313226048Sobrien 314226048Sobrien/* 315226048Sobrien * GNU Build ID generated by ld 316226048Sobrien * 160 bit SHA1 [default] 317226048Sobrien * 128 bit md5 or uuid 318226048Sobrien */ 319226048Sobrien#define NT_GNU_BUILD_ID 3 320226048Sobrien 321267843Sdelphij/* 322267843Sdelphij * NetBSD-specific note type: PaX. 323267843Sdelphij * There should be 1 NOTE per executable. 324267843Sdelphij * name: PaX\0 325267843Sdelphij * namesz: 4 326267843Sdelphij * desc: 327267843Sdelphij * word[0]: capability bitmask 328267843Sdelphij * descsz: 4 329267843Sdelphij */ 330267843Sdelphij#define NT_NETBSD_PAX 3 331267843Sdelphij#define NT_NETBSD_PAX_MPROTECT 0x01 /* Force enable Mprotect */ 332267843Sdelphij#define NT_NETBSD_PAX_NOMPROTECT 0x02 /* Force disable Mprotect */ 333267843Sdelphij#define NT_NETBSD_PAX_GUARD 0x04 /* Force enable Segvguard */ 334267843Sdelphij#define NT_NETBSD_PAX_NOGUARD 0x08 /* Force disable Servguard */ 335267843Sdelphij#define NT_NETBSD_PAX_ASLR 0x10 /* Force enable ASLR */ 336267843Sdelphij#define NT_NETBSD_PAX_NOASLR 0x20 /* Force disable ASLR */ 337267843Sdelphij 338267843Sdelphij/* 339267843Sdelphij * NetBSD-specific note type: MACHINE_ARCH. 340267843Sdelphij * There should be 1 NOTE per executable. 341267843Sdelphij * name: NetBSD\0 342267843Sdelphij * namesz: 7 343267843Sdelphij * desc: string 344267843Sdelphij * descsz: variable 345267843Sdelphij */ 346267843Sdelphij#define NT_NETBSD_MARCH 5 347267843Sdelphij 348267843Sdelphij/* 349267843Sdelphij * NetBSD-specific note type: COMPILER MODEL. 350267843Sdelphij * There should be 1 NOTE per executable. 351267843Sdelphij * name: NetBSD\0 352267843Sdelphij * namesz: 7 353267843Sdelphij * desc: string 354267843Sdelphij * descsz: variable 355267843Sdelphij */ 356267843Sdelphij#define NT_NETBSD_CMODEL 6 357267843Sdelphij 358330569Sgordon/* 359330569Sgordon * FreeBSD specific notes 360330569Sgordon */ 361330569Sgordon#define NT_FREEBSD_PROCSTAT_AUXV 16 362330569Sgordon 363267843Sdelphij#if !defined(ELFSIZE) && defined(ARCH_ELFSIZE) 364267843Sdelphij#define ELFSIZE ARCH_ELFSIZE 365267843Sdelphij#endif 366186690Sobrien/* SunOS 5.x hardware/software capabilities */ 367186690Sobrientypedef struct { 368186690Sobrien Elf32_Word c_tag; 369186690Sobrien union { 370186690Sobrien Elf32_Word c_val; 371186690Sobrien Elf32_Addr c_ptr; 372186690Sobrien } c_un; 373186690Sobrien} Elf32_Cap; 374186690Sobrien 375186690Sobrientypedef struct { 376186690Sobrien Elf64_Xword c_tag; 377186690Sobrien union { 378186690Sobrien Elf64_Xword c_val; 379186690Sobrien Elf64_Addr c_ptr; 380186690Sobrien } c_un; 381186690Sobrien} Elf64_Cap; 382186690Sobrien 383186690Sobrien/* SunOS 5.x hardware/software capability tags */ 384186690Sobrien#define CA_SUNW_NULL 0 385186690Sobrien#define CA_SUNW_HW_1 1 386186690Sobrien#define CA_SUNW_SF_1 2 387186690Sobrien 388186690Sobrien/* SunOS 5.x software capabilities */ 389186690Sobrien#define SF1_SUNW_FPKNWN 0x01 390186690Sobrien#define SF1_SUNW_FPUSED 0x02 391186690Sobrien#define SF1_SUNW_MASK 0x03 392186690Sobrien 393186690Sobrien/* SunOS 5.x hardware capabilities: sparc */ 394186690Sobrien#define AV_SPARC_MUL32 0x0001 395186690Sobrien#define AV_SPARC_DIV32 0x0002 396186690Sobrien#define AV_SPARC_FSMULD 0x0004 397186690Sobrien#define AV_SPARC_V8PLUS 0x0008 398186690Sobrien#define AV_SPARC_POPC 0x0010 399186690Sobrien#define AV_SPARC_VIS 0x0020 400186690Sobrien#define AV_SPARC_VIS2 0x0040 401186690Sobrien#define AV_SPARC_ASI_BLK_INIT 0x0080 402186690Sobrien#define AV_SPARC_FMAF 0x0100 403186690Sobrien#define AV_SPARC_FJFMAU 0x4000 404186690Sobrien#define AV_SPARC_IMA 0x8000 405186690Sobrien 406186690Sobrien/* SunOS 5.x hardware capabilities: 386 */ 407186690Sobrien#define AV_386_FPU 0x00000001 408186690Sobrien#define AV_386_TSC 0x00000002 409186690Sobrien#define AV_386_CX8 0x00000004 410186690Sobrien#define AV_386_SEP 0x00000008 411186690Sobrien#define AV_386_AMD_SYSC 0x00000010 412186690Sobrien#define AV_386_CMOV 0x00000020 413186690Sobrien#define AV_386_MMX 0x00000040 414186690Sobrien#define AV_386_AMD_MMX 0x00000080 415186690Sobrien#define AV_386_AMD_3DNow 0x00000100 416186690Sobrien#define AV_386_AMD_3DNowx 0x00000200 417186690Sobrien#define AV_386_FXSR 0x00000400 418186690Sobrien#define AV_386_SSE 0x00000800 419186690Sobrien#define AV_386_SSE2 0x00001000 420186690Sobrien#define AV_386_PAUSE 0x00002000 421186690Sobrien#define AV_386_SSE3 0x00004000 422186690Sobrien#define AV_386_MON 0x00008000 423186690Sobrien#define AV_386_CX16 0x00010000 424186690Sobrien#define AV_386_AHF 0x00020000 425186690Sobrien#define AV_386_TSCP 0x00040000 426186690Sobrien#define AV_386_AMD_SSE4A 0x00080000 427186690Sobrien#define AV_386_POPCNT 0x00100000 428186690Sobrien#define AV_386_AMD_LZCNT 0x00200000 429186690Sobrien#define AV_386_SSSE3 0x00400000 430186690Sobrien#define AV_386_SSE4_1 0x00800000 431186690Sobrien#define AV_386_SSE4_2 0x01000000 432186690Sobrien 43368349Sobrien#endif 434