# HG changeset patch
# User Emmanuel Ackaouy <ack@xxxxxxxxxxxxx>
# Date 1169763412 0
# Node ID db3d03dfe92fee126e473ac1c2e97d02c0cb79e2
# Parent b111908dd70b82edda1a0e61de6a269a9bff890d
add libelf: an ELF binary parser library.
This patch adds a library with a small collection of helper functions
to parse and load elf binaries. The library handles endianess and
elfsize at runtime.
The patch also shuffles around the include files a bit. Now there is
*one* include file holding all the elf structures
(xen/include/public/elfstructs.h) which is included by everyone who
needs them.
It's dead code with this patch only, putting the code into use happens
in followup patches.
Signed-off-by: Gerd Hoffmann <kraxel@xxxxxxx>
---
tools/libxc/xc_elf.h | 525 -----------------------------------
xen/arch/x86/boot/mkelf32.c | 2
xen/common/Makefile | 2
xen/common/libelf/Makefile | 4
xen/common/libelf/README | 1
xen/common/libelf/libelf-dominfo.c | 420 ++++++++++++++++++++++++++++
xen/common/libelf/libelf-loader.c | 156 ++++++++++
xen/common/libelf/libelf-private.h | 51 +++
xen/common/libelf/libelf-relocate.c | 345 +++++++++++++++++++++++
xen/common/libelf/libelf-tools.c | 225 +++++++++++++++
xen/include/public/elfstructs.h | 527 ++++++++++++++++++++++++++++++++++++
xen/include/public/libelf.h | 238 ++++++++++++++++
xen/include/xen/elf.h | 490 ---------------------------------
13 files changed, 1972 insertions(+), 1014 deletions(-)
diff -r b111908dd70b -r db3d03dfe92f tools/libxc/xc_elf.h
--- a/tools/libxc/xc_elf.h Thu Jan 25 18:54:25 2007 +0000
+++ b/tools/libxc/xc_elf.h Thu Jan 25 22:16:52 2007 +0000
@@ -1,524 +1,1 @@
-/*
- * Copyright (c) 1995, 1996 Erik Theisen. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- * derived from this software without specific prior written permission
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-typedef uint8_t Elf_Byte;
-
-typedef uint32_t Elf32_Addr; /* Unsigned program address */
-typedef uint32_t Elf32_Off; /* Unsigned file offset */
-typedef int32_t Elf32_Sword; /* Signed large integer */
-typedef uint32_t Elf32_Word; /* Unsigned large integer */
-typedef uint16_t Elf32_Half; /* Unsigned medium integer */
-
-typedef uint64_t Elf64_Addr;
-typedef uint64_t Elf64_Off;
-typedef int32_t Elf64_Shalf;
-
-typedef int32_t Elf64_Sword;
-typedef uint32_t Elf64_Word;
-
-typedef int64_t Elf64_Sxword;
-typedef uint64_t Elf64_Xword;
-
-typedef uint32_t Elf64_Half;
-typedef uint16_t Elf64_Quarter;
-
-/*
- * e_ident[] identification indexes
- * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html
- */
-#define EI_MAG0 0 /* file ID */
-#define EI_MAG1 1 /* file ID */
-#define EI_MAG2 2 /* file ID */
-#define EI_MAG3 3 /* file ID */
-#define EI_CLASS 4 /* file class */
-#define EI_DATA 5 /* data encoding */
-#define EI_VERSION 6 /* ELF header version */
-#define EI_OSABI 7 /* OS/ABI ID */
-#define EI_ABIVERSION 8 /* ABI version */
-#define EI_PAD 9 /* start of pad bytes */
-#define EI_NIDENT 16 /* Size of e_ident[] */
-
-/* e_ident[] magic number */
-#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */
-#define ELFMAG1 'E' /* e_ident[EI_MAG1] */
-#define ELFMAG2 'L' /* e_ident[EI_MAG2] */
-#define ELFMAG3 'F' /* e_ident[EI_MAG3] */
-#define ELFMAG "\177ELF" /* magic */
-#define SELFMAG 4 /* size of magic */
-
-/* e_ident[] file class */
-#define ELFCLASSNONE 0 /* invalid */
-#define ELFCLASS32 1 /* 32-bit objs */
-#define ELFCLASS64 2 /* 64-bit objs */
-#define ELFCLASSNUM 3 /* number of classes */
-
-/* e_ident[] data encoding */
-#define ELFDATANONE 0 /* invalid */
-#define ELFDATA2LSB 1 /* Little-Endian */
-#define ELFDATA2MSB 2 /* Big-Endian */
-#define ELFDATANUM 3 /* number of data encode defines */
-
-/* e_ident[] Operating System/ABI */
-#define ELFOSABI_SYSV 0 /* UNIX System V ABI */
-#define ELFOSABI_HPUX 1 /* HP-UX operating system */
-#define ELFOSABI_NETBSD 2 /* NetBSD */
-#define ELFOSABI_LINUX 3 /* GNU/Linux */
-#define ELFOSABI_HURD 4 /* GNU/Hurd */
-#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */
-#define ELFOSABI_SOLARIS 6 /* Solaris */
-#define ELFOSABI_MONTEREY 7 /* Monterey */
-#define ELFOSABI_IRIX 8 /* IRIX */
-#define ELFOSABI_FREEBSD 9 /* FreeBSD */
-#define ELFOSABI_TRU64 10 /* TRU64 UNIX */
-#define ELFOSABI_MODESTO 11 /* Novell Modesto */
-#define ELFOSABI_OPENBSD 12 /* OpenBSD */
-#define ELFOSABI_ARM 97 /* ARM */
-#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
-
-/* e_ident */
-#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
- (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
- (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
- (ehdr).e_ident[EI_MAG3] == ELFMAG3)
-
-/* ELF Header */
-typedef struct elfhdr {
- unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
- Elf32_Half e_type; /* object file type */
- Elf32_Half e_machine; /* machine */
- Elf32_Word e_version; /* object file version */
- Elf32_Addr e_entry; /* virtual entry point */
- Elf32_Off e_phoff; /* program header table offset */
- Elf32_Off e_shoff; /* section header table offset */
- Elf32_Word e_flags; /* processor-specific flags */
- Elf32_Half e_ehsize; /* ELF header size */
- Elf32_Half e_phentsize; /* program header entry size */
- Elf32_Half e_phnum; /* number of program header entries */
- Elf32_Half e_shentsize; /* section header entry size */
- Elf32_Half e_shnum; /* number of section header entries */
- Elf32_Half e_shstrndx; /* section header table's "section
- header string table" entry offset */
-} Elf32_Ehdr;
-
-typedef struct {
- unsigned char e_ident[EI_NIDENT]; /* Id bytes */
- Elf64_Quarter e_type; /* file type */
- Elf64_Quarter e_machine; /* machine type */
- Elf64_Half e_version; /* version number */
- Elf64_Addr e_entry; /* entry point */
- Elf64_Off e_phoff; /* Program hdr offset */
- Elf64_Off e_shoff; /* Section hdr offset */
- Elf64_Half e_flags; /* Processor flags */
- Elf64_Quarter e_ehsize; /* sizeof ehdr */
- Elf64_Quarter e_phentsize; /* Program header entry size */
- Elf64_Quarter e_phnum; /* Number of program headers */
- Elf64_Quarter e_shentsize; /* Section header entry size */
- Elf64_Quarter e_shnum; /* Number of section headers */
- Elf64_Quarter e_shstrndx; /* String table index */
-} Elf64_Ehdr;
-
-/* e_type */
-#define ET_NONE 0 /* No file type */
-#define ET_REL 1 /* relocatable file */
-#define ET_EXEC 2 /* executable file */
-#define ET_DYN 3 /* shared object file */
-#define ET_CORE 4 /* core file */
-#define ET_NUM 5 /* number of types */
-#define ET_LOPROC 0xff00 /* reserved range for processor */
-#define ET_HIPROC 0xffff /* specific e_type */
-
-/* e_machine */
-#define EM_NONE 0 /* No Machine */
-#define EM_M32 1 /* AT&T WE 32100 */
-#define EM_SPARC 2 /* SPARC */
-#define EM_386 3 /* Intel 80386 */
-#define EM_68K 4 /* Motorola 68000 */
-#define EM_88K 5 /* Motorola 88000 */
-#define EM_486 6 /* Intel 80486 - unused? */
-#define EM_860 7 /* Intel 80860 */
-#define EM_MIPS 8 /* MIPS R3000 Big-Endian only */
-/*
- * Don't know if EM_MIPS_RS4_BE,
- * EM_SPARC64, EM_PARISC,
- * or EM_PPC are ABI compliant
- */
-#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */
-#define EM_SPARC64 11 /* SPARC v9 64-bit unoffical */
-#define EM_PARISC 15 /* HPPA */
-#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */
-#define EM_PPC 20 /* PowerPC */
-#define EM_PPC64 21 /* PowerPC 64-bit */
-#define EM_ARM 40 /* Advanced RISC Machines ARM */
-#define EM_ALPHA 41 /* DEC ALPHA */
-#define EM_SPARCV9 43 /* SPARC version 9 */
-#define EM_ALPHA_EXP 0x9026 /* DEC ALPHA */
-#define EM_IA_64 50 /* Intel Merced */
-#define EM_X86_64 62 /* AMD x86-64 architecture */
-#define EM_VAX 75 /* DEC VAX */
-
-/* Version */
-#define EV_NONE 0 /* Invalid */
-#define EV_CURRENT 1 /* Current */
-#define EV_NUM 2 /* number of versions */
-
-/* Section Header */
-typedef struct {
- Elf32_Word sh_name; /* name - index into section header
- string table section */
- Elf32_Word sh_type; /* type */
- Elf32_Word sh_flags; /* flags */
- Elf32_Addr sh_addr; /* address */
- Elf32_Off sh_offset; /* file offset */
- Elf32_Word sh_size; /* section size */
- Elf32_Word sh_link; /* section header table index link */
- Elf32_Word sh_info; /* extra information */
- Elf32_Word sh_addralign; /* address alignment */
- Elf32_Word sh_entsize; /* section entry size */
-} Elf32_Shdr;
-
-typedef struct {
- Elf64_Half sh_name; /* section name */
- Elf64_Half sh_type; /* section type */
- Elf64_Xword sh_flags; /* section flags */
- Elf64_Addr sh_addr; /* virtual address */
- Elf64_Off sh_offset; /* file offset */
- Elf64_Xword sh_size; /* section size */
- Elf64_Half sh_link; /* link to another */
- Elf64_Half sh_info; /* misc info */
- Elf64_Xword sh_addralign; /* memory alignment */
- Elf64_Xword sh_entsize; /* table entry size */
-} Elf64_Shdr;
-
-/* Special Section Indexes */
-#define SHN_UNDEF 0 /* undefined */
-#define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */
-#define SHN_LOPROC 0xff00 /* reserved range for processor */
-#define SHN_HIPROC 0xff1f /* specific section indexes */
-#define SHN_ABS 0xfff1 /* absolute value */
-#define SHN_COMMON 0xfff2 /* common symbol */
-#define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */
-
-/* sh_type */
-#define SHT_NULL 0 /* inactive */
-#define SHT_PROGBITS 1 /* program defined information */
-#define SHT_SYMTAB 2 /* symbol table section */
-#define SHT_STRTAB 3 /* string table section */
-#define SHT_RELA 4 /* relocation section with addends*/
-#define SHT_HASH 5 /* symbol hash table section */
-#define SHT_DYNAMIC 6 /* dynamic section */
-#define SHT_NOTE 7 /* note section */
-#define SHT_NOBITS 8 /* no space section */
-#define SHT_REL 9 /* relation section without
addends */
-#define SHT_SHLIB 10 /* reserved - purpose unknown */
-#define SHT_DYNSYM 11 /* dynamic symbol table section */
-#define SHT_NUM 12 /* number of section types */
-#define SHT_LOPROC 0x70000000 /* reserved range for processor */
-#define SHT_HIPROC 0x7fffffff /* specific section header types */
-#define SHT_LOUSER 0x80000000 /* reserved range for application */
-#define SHT_HIUSER 0xffffffff /* specific indexes */
-
-/* Section names */
-#define ELF_BSS ".bss" /* uninitialized data */
-#define ELF_DATA ".data" /* initialized data */
-#define ELF_DEBUG ".debug" /* debug */
-#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */
-#define ELF_DYNSTR ".dynstr" /* dynamic string table */
-#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */
-#define ELF_FINI ".fini" /* termination code */
-#define ELF_GOT ".got" /* global offset table */
-#define ELF_HASH ".hash" /* symbol hash table */
-#define ELF_INIT ".init" /* initialization code */
-#define ELF_REL_DATA ".rel.data" /* relocation data */
-#define ELF_REL_FINI ".rel.fini" /* relocation termination code */
-#define ELF_REL_INIT ".rel.init" /* relocation initialization code */
-#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */
-#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */
-#define ELF_REL_TEXT ".rel.text" /* relocation code */
-#define ELF_RODATA ".rodata" /* read-only data */
-#define ELF_SHSTRTAB ".shstrtab" /* section header string table */
-#define ELF_STRTAB ".strtab" /* string table */
-#define ELF_SYMTAB ".symtab" /* symbol table */
-#define ELF_TEXT ".text" /* code */
-
-
-/* Section Attribute Flags - sh_flags */
-#define SHF_WRITE 0x1 /* Writable */
-#define SHF_ALLOC 0x2 /* occupies memory */
-#define SHF_EXECINSTR 0x4 /* executable */
-#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
- /* specific section attributes */
-
-/* Symbol Table Entry */
-typedef struct elf32_sym {
- Elf32_Word st_name; /* name - index into string table */
- Elf32_Addr st_value; /* symbol value */
- Elf32_Word st_size; /* symbol size */
- unsigned char st_info; /* type and binding */
- unsigned char st_other; /* 0 - no defined meaning */
- Elf32_Half st_shndx; /* section header index */
-} Elf32_Sym;
-
-typedef struct {
- Elf64_Half st_name; /* Symbol name index in str table */
- Elf_Byte st_info; /* type / binding attrs */
- Elf_Byte st_other; /* unused */
- Elf64_Quarter st_shndx; /* section index of symbol */
- Elf64_Xword st_value; /* value of symbol */
- Elf64_Xword st_size; /* size of symbol */
-} Elf64_Sym;
-
-/* Symbol table index */
-#define STN_UNDEF 0 /* undefined */
-
-/* Extract symbol info - st_info */
-#define ELF32_ST_BIND(x) ((x) >> 4)
-#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf)
-#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf))
-
-#define ELF64_ST_BIND(x) ((x) >> 4)
-#define ELF64_ST_TYPE(x) (((unsigned int) x) & 0xf)
-#define ELF64_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf))
-
-/* Symbol Binding - ELF32_ST_BIND - st_info */
-#define STB_LOCAL 0 /* Local symbol */
-#define STB_GLOBAL 1 /* Global symbol */
-#define STB_WEAK 2 /* like global - lower precedence */
-#define STB_NUM 3 /* number of symbol bindings */
-#define STB_LOPROC 13 /* reserved range for processor */
-#define STB_HIPROC 15 /* specific symbol bindings */
-
-/* Symbol type - ELF32_ST_TYPE - st_info */
-#define STT_NOTYPE 0 /* not specified */
-#define STT_OBJECT 1 /* data object */
-#define STT_FUNC 2 /* function */
-#define STT_SECTION 3 /* section */
-#define STT_FILE 4 /* file */
-#define STT_NUM 5 /* number of symbol types */
-#define STT_LOPROC 13 /* reserved range for processor */
-#define STT_HIPROC 15 /* specific symbol types */
-
-/* Relocation entry with implicit addend */
-typedef struct {
- Elf32_Addr r_offset; /* offset of relocation */
- Elf32_Word r_info; /* symbol table index and type */
-} Elf32_Rel;
-
-/* Relocation entry with explicit addend */
-typedef struct {
- Elf32_Addr r_offset; /* offset of relocation */
- Elf32_Word r_info; /* symbol table index and type */
- Elf32_Sword r_addend;
-} Elf32_Rela;
-
-/* Extract relocation info - r_info */
-#define ELF32_R_SYM(i) ((i) >> 8)
-#define ELF32_R_TYPE(i) ((unsigned char) (i))
-#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
-
-typedef struct {
- Elf64_Xword r_offset; /* where to do it */
- Elf64_Xword r_info; /* index & type of relocation */
-} Elf64_Rel;
-
-typedef struct {
- Elf64_Xword r_offset; /* where to do it */
- Elf64_Xword r_info; /* index & type of relocation */
- Elf64_Sxword r_addend; /* adjustment value */
-} Elf64_Rela;
-
-#define ELF64_R_SYM(info) ((info) >> 32)
-#define ELF64_R_TYPE(info) ((info) & 0xFFFFFFFF)
-#define ELF64_R_INFO(s,t) (((s) << 32) + (u_int32_t)(t))
-
-/* Program Header */
-typedef struct {
- Elf32_Word p_type; /* segment type */
- Elf32_Off p_offset; /* segment offset */
- Elf32_Addr p_vaddr; /* virtual address of segment */
- Elf32_Addr p_paddr; /* physical address - ignored? */
- Elf32_Word p_filesz; /* number of bytes in file for seg. */
- Elf32_Word p_memsz; /* number of bytes in mem. for seg. */
- Elf32_Word p_flags; /* flags */
- Elf32_Word p_align; /* memory alignment */
-} Elf32_Phdr;
-
-typedef struct {
- Elf64_Half p_type; /* entry type */
- Elf64_Half p_flags; /* flags */
- Elf64_Off p_offset; /* offset */
- Elf64_Addr p_vaddr; /* virtual address */
- Elf64_Addr p_paddr; /* physical address */
- Elf64_Xword p_filesz; /* file size */
- Elf64_Xword p_memsz; /* memory size */
- Elf64_Xword p_align; /* memory & file alignment */
-} Elf64_Phdr;
-
-/* Segment types - p_type */
-#define PT_NULL 0 /* unused */
-#define PT_LOAD 1 /* loadable segment */
-#define PT_DYNAMIC 2 /* dynamic linking section */
-#define PT_INTERP 3 /* the RTLD */
-#define PT_NOTE 4 /* auxiliary information */
-#define PT_SHLIB 5 /* reserved - purpose undefined */
-#define PT_PHDR 6 /* program header */
-#define PT_NUM 7 /* Number of segment types */
-#define PT_LOPROC 0x70000000 /* reserved range for processor */
-#define PT_HIPROC 0x7fffffff /* specific segment types */
-
-/* Segment flags - p_flags */
-#define PF_X 0x1 /* Executable */
-#define PF_W 0x2 /* Writable */
-#define PF_R 0x4 /* Readable */
-#define PF_MASKPROC 0xf0000000 /* reserved bits for processor */
- /* specific segment flags */
-
-/* Dynamic structure */
-typedef struct {
- Elf32_Sword d_tag; /* controls meaning of d_val */
- union {
- Elf32_Word d_val; /* Multiple meanings - see d_tag */
- Elf32_Addr d_ptr; /* program virtual address */
- } d_un;
-} Elf32_Dyn;
-
-typedef struct {
- Elf64_Xword d_tag; /* controls meaning of d_val */
- union {
- Elf64_Addr d_ptr;
- Elf64_Xword d_val;
- } d_un;
-} Elf64_Dyn;
-
-/* Dynamic Array Tags - d_tag */
-#define DT_NULL 0 /* marks end of _DYNAMIC array
*/
-#define DT_NEEDED 1 /* string table offset of needed lib */
-#define DT_PLTRELSZ 2 /* size of relocation entries in PLT */
-#define DT_PLTGOT 3 /* address PLT/GOT */
-#define DT_HASH 4 /* address of symbol hash table
*/
-#define DT_STRTAB 5 /* address of string table */
-#define DT_SYMTAB 6 /* address of symbol table */
-#define DT_RELA 7 /* address of relocation table
*/
-#define DT_RELASZ 8 /* size of relocation table */
-#define DT_RELAENT 9 /* size of relocation entry */
-#define DT_STRSZ 10 /* size of string table */
-#define DT_SYMENT 11 /* size of symbol table entry */
-#define DT_INIT 12 /* address of initialization
func. */
-#define DT_FINI 13 /* address of termination
function */
-#define DT_SONAME 14 /* string table offset of shared obj */
-#define DT_RPATH 15 /* string table offset of library
- search path */
-#define DT_SYMBOLIC 16 /* start sym search in shared obj. */
-#define DT_REL 17 /* address of rel. tbl. w addends */
-#define DT_RELSZ 18 /* size of DT_REL relocation table */
-#define DT_RELENT 19 /* size of DT_REL relocation entry */
-#define DT_PLTREL 20 /* PLT referenced relocation entry */
-#define DT_DEBUG 21 /* bugger */
-#define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */
-#define DT_JMPREL 23 /* add. of PLT's relocation entries */
-#define DT_BIND_NOW 24 /* Bind now regardless of env setting */
-#define DT_NUM 25 /* Number used. */
-#define DT_LOPROC 0x70000000 /* reserved range for processor */
-#define DT_HIPROC 0x7fffffff /* specific dynamic array tags */
-
-/* Standard ELF hashing function */
-unsigned int elf_hash(const unsigned char *name);
-
-/*
- * Note Definitions
- */
-typedef struct {
- Elf32_Word namesz;
- Elf32_Word descsz;
- Elf32_Word type;
-} Elf32_Note;
-
-typedef struct {
- Elf64_Half namesz;
- Elf64_Half descsz;
- Elf64_Half type;
-} Elf64_Note;
-
-
-#if defined(ELFSIZE)
-#define CONCAT(x,y) __CONCAT(x,y)
-#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
-#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
-#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
-#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
-#endif
-
-#if defined(ELFSIZE) && (ELFSIZE == 32)
-#define Elf_Ehdr Elf32_Ehdr
-#define Elf_Phdr Elf32_Phdr
-#define Elf_Shdr Elf32_Shdr
-#define Elf_Sym Elf32_Sym
-#define Elf_Rel Elf32_Rel
-#define Elf_RelA Elf32_Rela
-#define Elf_Dyn Elf32_Dyn
-#define Elf_Word Elf32_Word
-#define Elf_Sword Elf32_Sword
-#define Elf_Addr Elf32_Addr
-#define Elf_Off Elf32_Off
-#define Elf_Nhdr Elf32_Nhdr
-#define Elf_Note Elf32_Note
-
-#define ELF_R_SYM ELF32_R_SYM
-#define ELF_R_TYPE ELF32_R_TYPE
-#define ELF_R_INFO ELF32_R_INFO
-#define ELFCLASS ELFCLASS32
-
-#define ELF_ST_BIND ELF32_ST_BIND
-#define ELF_ST_TYPE ELF32_ST_TYPE
-#define ELF_ST_INFO ELF32_ST_INFO
-
-#define AuxInfo Aux32Info
-#elif defined(ELFSIZE) && (ELFSIZE == 64)
-#define Elf_Ehdr Elf64_Ehdr
-#define Elf_Phdr Elf64_Phdr
-#define Elf_Shdr Elf64_Shdr
-#define Elf_Sym Elf64_Sym
-#define Elf_Rel Elf64_Rel
-#define Elf_RelA Elf64_Rela
-#define Elf_Dyn Elf64_Dyn
-#define Elf_Word Elf64_Word
-#define Elf_Sword Elf64_Sword
-#define Elf_Addr Elf64_Addr
-#define Elf_Off Elf64_Off
-#define Elf_Nhdr Elf64_Nhdr
-#define Elf_Note Elf64_Note
-
-#define ELF_R_SYM ELF64_R_SYM
-#define ELF_R_TYPE ELF64_R_TYPE
-#define ELF_R_INFO ELF64_R_INFO
-#define ELFCLASS ELFCLASS64
-
-#define ELF_ST_BIND ELF64_ST_BIND
-#define ELF_ST_TYPE ELF64_ST_TYPE
-#define ELF_ST_INFO ELF64_ST_INFO
-
-#define AuxInfo Aux64Info
-#endif
-
+#include <xen/elfstructs.h>
diff -r b111908dd70b -r db3d03dfe92f xen/arch/x86/boot/mkelf32.c
--- a/xen/arch/x86/boot/mkelf32.c Thu Jan 25 18:54:25 2007 +0000
+++ b/xen/arch/x86/boot/mkelf32.c Thu Jan 25 22:16:52 2007 +0000
@@ -25,7 +25,7 @@
#define s16 int16_t
#define s32 int32_t
#define s64 int64_t
-#include "../../../include/xen/elf.h"
+#include "../../../include/public/elfstructs.h"
#define DYNAMICALLY_FILLED 0
#define RAW_OFFSET 128
diff -r b111908dd70b -r db3d03dfe92f xen/common/Makefile
--- a/xen/common/Makefile Thu Jan 25 18:54:25 2007 +0000
+++ b/xen/common/Makefile Thu Jan 25 22:16:52 2007 +0000
@@ -37,6 +37,8 @@ obj-$(CONFIG_XENCOMM) += xencomm.o
subdir-$(CONFIG_COMPAT) += compat
+subdir-y += libelf
+
# Object file contains changeset and compiler information.
version.o: $(BASEDIR)/include/xen/compile.h
diff -r b111908dd70b -r db3d03dfe92f xen/common/libelf/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/xen/common/libelf/Makefile Thu Jan 25 22:16:52 2007 +0000
@@ -0,0 +1,4 @@
+obj-y += libelf-tools.o
+obj-y += libelf-loader.o
+obj-y += libelf-dominfo.o
+#obj-y += libelf-relocate.o
diff -r b111908dd70b -r db3d03dfe92f xen/common/libelf/README
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/xen/common/libelf/README Thu Jan 25 22:16:52 2007 +0000
@@ -0,0 +1,1 @@
+Take care, this code is used by both xen and tools ...
diff -r b111908dd70b -r db3d03dfe92f xen/common/libelf/libelf-dominfo.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/xen/common/libelf/libelf-dominfo.c Thu Jan 25 22:16:52 2007 +0000
@@ -0,0 +1,420 @@
+/*
+ * parse xen-specific informations out of elf kernel binaries.
+ */
+
+#include "libelf-private.h"
+
+/* ------------------------------------------------------------------------ */
+/* xen features */
+
+const char *elf_xen_feature_names[] = {
+ [XENFEAT_writable_page_tables] = "writable_page_tables",
+ [XENFEAT_writable_descriptor_tables] = "writable_descriptor_tables",
+ [XENFEAT_auto_translated_physmap] = "auto_translated_physmap",
+ [XENFEAT_supervisor_mode_kernel] = "supervisor_mode_kernel",
+ [XENFEAT_pae_pgdir_above_4gb] = "pae_pgdir_above_4gb"
+};
+const int elf_xen_features =
+ sizeof(elf_xen_feature_names) / sizeof(elf_xen_feature_names[0]);
+
+int elf_xen_parse_features(const char *features,
+ uint32_t *supported,
+ uint32_t *required)
+{
+ char feature[64];
+ int pos, len, i;
+
+ if (NULL == features)
+ return 0;
+ for (pos = 0; features[pos] != '\0'; pos += len)
+ {
+ memset(feature, 0, sizeof(feature));
+ for (len = 0;; len++)
+ {
+ if (len >= sizeof(feature)-1)
+ break;
+ if (features[pos + len] == '\0')
+ break;
+ if (features[pos + len] == '|')
+ {
+ len++;
+ break;
+ }
+ feature[len] = features[pos + len];
+ }
+
+ for (i = 0; i < elf_xen_features; i++)
+ {
+ if (!elf_xen_feature_names[i])
+ continue;
+ if (NULL != required && feature[0] == '!')
+ {
+ /* required */
+ if (0 == strcmp(feature + 1, elf_xen_feature_names[i]))
+ {
+ elf_xen_feature_set(i, supported);
+ elf_xen_feature_set(i, required);
+ break;
+ }
+ }
+ else
+ {
+ /* supported */
+ if (0 == strcmp(feature, elf_xen_feature_names[i]))
+ {
+ elf_xen_feature_set(i, supported);
+ break;
+ }
+ }
+ }
+ if (i == elf_xen_features)
+ return -1;
+ }
+ return 0;
+}
+
+/* ------------------------------------------------------------------------ */
+/* xen elf notes */
+
+int elf_xen_parse_note(struct elf_binary *elf,
+ struct elf_dom_parms *parms,
+ const elf_note *note)
+{
+/* *INDENT-OFF* */
+ static const struct {
+ char *name;
+ int str;
+ } note_desc[] = {
+ [XEN_ELFNOTE_ENTRY] = { "ENTRY", 0},
+ [XEN_ELFNOTE_HYPERCALL_PAGE] = { "HYPERCALL_PAGE", 0},
+ [XEN_ELFNOTE_VIRT_BASE] = { "VIRT_BASE", 0},
+ [XEN_ELFNOTE_PADDR_OFFSET] = { "PADDR_OFFSET", 0},
+ [XEN_ELFNOTE_HV_START_LOW] = { "HV_START_LOW", 0},
+ [XEN_ELFNOTE_XEN_VERSION] = { "XEN_VERSION", 1},
+ [XEN_ELFNOTE_GUEST_OS] = { "GUEST_OS", 1},
+ [XEN_ELFNOTE_GUEST_VERSION] = { "GUEST_VERSION", 1},
+ [XEN_ELFNOTE_LOADER] = { "LOADER", 1},
+ [XEN_ELFNOTE_PAE_MODE] = { "PAE_MODE", 1},
+ [XEN_ELFNOTE_FEATURES] = { "FEATURES", 1},
+ [XEN_ELFNOTE_BSD_SYMTAB] = { "BSD_SYMTAB", 1},
+ };
+/* *INDENT-ON* */
+
+ const char *str = NULL;
+ uint64_t val = 0;
+ int type = elf_uval(elf, note, type);
+
+ if ((type >= sizeof(note_desc) / sizeof(note_desc[0])) ||
+ (NULL == note_desc[type].name))
+ {
+ elf_err(elf, "%s: unknown xen elf note (0x%x)\n",
+ __FUNCTION__, type);
+ return -1;
+ }
+
+ if (note_desc[type].str)
+ {
+ str = elf_note_desc(elf, note);
+ elf_msg(elf, "%s: %s = \"%s\"\n", __FUNCTION__,
+ note_desc[type].name, str);
+ }
+ else
+ {
+ val = elf_note_numeric(elf, note);
+ elf_msg(elf, "%s: %s = 0x%" PRIx64 "\n", __FUNCTION__,
+ note_desc[type].name, val);
+ }
+
+ switch (type)
+ {
+ case XEN_ELFNOTE_LOADER:
+ strncpy(parms->loader, str, sizeof(parms->loader));
+ break;
+ case XEN_ELFNOTE_GUEST_OS:
+ strncpy(parms->guest_os, str, sizeof(parms->guest_os));
+ break;
+ case XEN_ELFNOTE_GUEST_VERSION:
+ strncpy(parms->guest_ver, str, sizeof(parms->guest_ver));
+ break;
+ case XEN_ELFNOTE_XEN_VERSION:
+ strncpy(parms->xen_ver, str, sizeof(parms->xen_ver));
+ break;
+ case XEN_ELFNOTE_PAE_MODE:
+ if (0 == strcmp(str, "yes"))
+ parms->pae = 2 /* extended_cr3 */;
+ if (strstr(str, "bimodal"))
+ parms->pae = 3 /* bimodal */;
+ break;
+ case XEN_ELFNOTE_BSD_SYMTAB:
+ if (0 == strcmp(str, "yes"))
+ parms->bsd_symtab = 1;
+ break;
+
+ case XEN_ELFNOTE_VIRT_BASE:
+ parms->virt_base = val;
+ break;
+ case XEN_ELFNOTE_ENTRY:
+ parms->virt_entry = val;
+ break;
+ case XEN_ELFNOTE_PADDR_OFFSET:
+ parms->elf_paddr_offset = val;
+ break;
+ case XEN_ELFNOTE_HYPERCALL_PAGE:
+ parms->virt_hypercall = val;
+ break;
+ case XEN_ELFNOTE_HV_START_LOW:
+ parms->virt_hv_start_low = val;
+ break;
+
+ case XEN_ELFNOTE_FEATURES:
+ if (0 != elf_xen_parse_features(str, parms->f_supported,
+ parms->f_required))
+ return -1;
+ break;
+
+ }
+ return 0;
+}
+
+/* ------------------------------------------------------------------------ */
+/* __xen_guest section */
+
+int elf_xen_parse_guest_info(struct elf_binary *elf,
+ struct elf_dom_parms *parms)
+{
+ const char *h;
+ char name[32], value[128];
+ int len;
+
+ h = parms->guest_info;
+ while (*h)
+ {
+ memset(name, 0, sizeof(name));
+ memset(value, 0, sizeof(value));
+ for (len = 0;; len++, h++) {
+ if (len >= sizeof(name)-1)
+ break;
+ if (*h == '\0')
+ break;
+ if (*h == ',')
+ {
+ h++;
+ break;
+ }
+ if (*h == '=')
+ {
+ h++;
+ for (len = 0;; len++, h++) {
+ if (len >= sizeof(value)-1)
+ break;
+ if (*h == '\0')
+ break;
+ if (*h == ',')
+ {
+ h++;
+ break;
+ }
+ value[len] = *h;
+ }
+ break;
+ }
+ name[len] = *h;
+ }
+ elf_msg(elf, "%s: %s=\"%s\"\n", __FUNCTION__, name, value);
+
+ /* strings */
+ if (0 == strcmp(name, "LOADER"))
+ strncpy(parms->loader, value, sizeof(parms->loader));
+ if (0 == strcmp(name, "GUEST_OS"))
+ strncpy(parms->guest_os, value, sizeof(parms->guest_os));
+ if (0 == strcmp(name, "GUEST_VER"))
+ strncpy(parms->guest_ver, value, sizeof(parms->guest_ver));
+ if (0 == strcmp(name, "XEN_VER"))
+ strncpy(parms->xen_ver, value, sizeof(parms->xen_ver));
+ if (0 == strcmp(name, "PAE"))
+ {
+ if (0 == strcmp(value, "yes[extended-cr3]"))
+ parms->pae = 2 /* extended_cr3 */;
+ else if (0 == strncmp(value, "yes", 3))
+ parms->pae = 1 /* yes */;
+ }
+ if (0 == strcmp(name, "BSD_SYMTAB"))
+ parms->bsd_symtab = 1;
+
+ /* longs */
+ if (0 == strcmp(name, "VIRT_BASE"))
+ parms->virt_base = strtoull(value, NULL, 0);
+ if (0 == strcmp(name, "VIRT_ENTRY"))
+ parms->virt_entry = strtoull(value, NULL, 0);
+ if (0 == strcmp(name, "ELF_PADDR_OFFSET"))
+ parms->elf_paddr_offset = strtoull(value, NULL, 0);
+ if (0 == strcmp(name, "HYPERCALL_PAGE"))
+ parms->virt_hypercall = (strtoull(value, NULL, 0) << 12) +
+ parms->virt_base;
+
+ /* other */
+ if (0 == strcmp(name, "FEATURES"))
+ if (0 != elf_xen_parse_features(value, parms->f_supported,
+ parms->f_required))
+ return -1;
+ }
+ return 0;
+}
+
+/* ------------------------------------------------------------------------ */
+/* sanity checks */
+
+static int elf_xen_note_check(struct elf_binary *elf,
+ struct elf_dom_parms *parms)
+{
+ if (NULL == parms->elf_note_start && NULL == parms->guest_info) {
+ int machine = elf_uval(elf, elf->ehdr, e_machine);
+ if (EM_386 == machine || EM_X86_64 == machine) {
+ elf_err(elf, "%s: ERROR: Not a Xen-ELF image: "
+ "No ELF notes or '__xen_guest' section found.\n",
+ __FUNCTION__);
+ return -1;
+ }
+ return 0;
+ }
+
+ /* Check the contents of the Xen notes or guest string. */
+ if ( ( 0 == strlen(parms->loader) || strncmp(parms->loader, "generic", 7)
) &&
+ ( 0 == strlen(parms->guest_os) || strncmp(parms->guest_os, "linux", 5)
) )
+ {
+ elf_err(elf, "%s: ERROR: Will only load images built for the generic "
+ "loader or Linux images", __FUNCTION__);
+ return -1;
+ }
+
+ if ( 0 == strlen(parms->xen_ver) || strncmp(parms->xen_ver, "xen-3.0", 7) )
+ {
+ elf_err(elf, "%s: ERROR: Xen will only load images built for Xen
v3.0\n",
+ __FUNCTION__);
+ return -1;
+ }
+ return 0;
+}
+
+static int elf_xen_addr_calc_check(struct elf_binary *elf,
+ struct elf_dom_parms *parms)
+{
+ if (UNSET_ADDR != parms->elf_paddr_offset &&
+ UNSET_ADDR == parms->virt_base )
+ {
+ elf_err(elf, "%s: ERROR: ELF_PADDR_OFFSET set, VIRT_BASE unset\n",
+ __FUNCTION__);
+ return -1;
+ }
+
+ /* Initial guess for virt_base is 0 if it is not explicitly defined. */
+ if (UNSET_ADDR == parms->virt_base)
+ {
+ parms->virt_base = 0;
+ elf_msg(elf, "%s: VIRT_BASE unset, using 0x%" PRIx64 "\n",
+ __FUNCTION__, parms->virt_base);
+ }
+
+ /*
+ * If we are using the legacy __xen_guest section then elf_pa_off
+ * defaults to v_start in order to maintain compatibility with
+ * older hypervisors which set padd in the ELF header to
+ * virt_base.
+ *
+ * If we are using the modern ELF notes interface then the default
+ * is 0.
+ */
+ if (UNSET_ADDR == parms->elf_paddr_offset)
+ {
+ if (parms->elf_note_start)
+ parms->elf_paddr_offset = 0;
+ else
+ parms->elf_paddr_offset = parms->virt_base;
+ elf_msg(elf, "%s: ELF_PADDR_OFFSET unset, using 0x%" PRIx64 "\n",
+ __FUNCTION__, parms->elf_paddr_offset);
+ }
+
+ parms->virt_offset = parms->virt_base - parms->elf_paddr_offset;
+ parms->virt_kstart = elf->pstart + parms->virt_offset;
+ parms->virt_kend = elf->pend + parms->virt_offset;
+
+ if (UNSET_ADDR == parms->virt_entry)
+ parms->virt_entry = elf_uval(elf, elf->ehdr, e_entry);
+
+ elf_msg(elf, "%s: addresses:\n", __FUNCTION__);
+ elf_msg(elf, " virt_base = 0x%" PRIx64 "\n", parms->virt_base);
+ elf_msg(elf, " elf_paddr_offset = 0x%" PRIx64 "\n",
parms->elf_paddr_offset);
+ elf_msg(elf, " virt_offset = 0x%" PRIx64 "\n", parms->virt_offset);
+ elf_msg(elf, " virt_kstart = 0x%" PRIx64 "\n", parms->virt_kstart);
+ elf_msg(elf, " virt_kend = 0x%" PRIx64 "\n", parms->virt_kend);
+ elf_msg(elf, " virt_entry = 0x%" PRIx64 "\n", parms->virt_entry);
+
+ if ( (parms->virt_kstart > parms->virt_kend) ||
+ (parms->virt_entry < parms->virt_kstart) ||
+ (parms->virt_entry > parms->virt_kend) ||
+ (parms->virt_base > parms->virt_kstart) )
+ {
+ elf_err(elf, "%s: ERROR: ELF start or entries are out of bounds.\n",
+ __FUNCTION__);
+ return -1;
+ }
+
+ return 0;
+}
+
+/* ------------------------------------------------------------------------ */
+/* glue it all together ... */
+
+int elf_xen_parse(struct elf_binary *elf,
+ struct elf_dom_parms *parms)
+{
+ const elf_note *note;
+ const elf_shdr *shdr;
+ int xen_elfnotes = 0;
+ int i, count;
+
+ memset(parms, 0, sizeof(*parms));
+ parms->virt_base = UNSET_ADDR;
+ parms->virt_entry = UNSET_ADDR;
+ parms->virt_hypercall = UNSET_ADDR;
+ parms->virt_hv_start_low = UNSET_ADDR;
+ parms->elf_paddr_offset = UNSET_ADDR;
+
+ /* find and parse elf notes */
+ count = elf_shdr_count(elf);
+ for (i = 0; i < count; i++)
+ {
+ shdr = elf_shdr_by_index(elf, i);
+ if (0 == strcmp(elf_section_name(elf, shdr), "__xen_guest"))
+ parms->guest_info = elf_section_start(elf, shdr);
+ if (elf_uval(elf, shdr, sh_type) != SHT_NOTE)
+ continue;
+ parms->elf_note_start = elf_section_start(elf, shdr);
+ parms->elf_note_end = elf_section_end(elf, shdr);
+ for (note = parms->elf_note_start;
+ (void *)note < parms->elf_note_end;
+ note = elf_note_next(elf, note))
+ {
+ if (0 != strcmp(elf_note_name(elf, note), "Xen"))
+ continue;
+ if (0 != elf_xen_parse_note(elf, parms, note))
+ return -1;
+ xen_elfnotes++;
+ }
+ }
+
+ if (!xen_elfnotes && parms->guest_info)
+ {
+ parms->elf_note_start = NULL;
+ parms->elf_note_end = NULL;
+ elf_msg(elf, "%s: __xen_guest: \"%s\"\n", __FUNCTION__,
+ parms->guest_info);
+ elf_xen_parse_guest_info(elf, parms);
+ }
+
+ if (0 != elf_xen_note_check(elf, parms))
+ return -1;
+ if (0 != elf_xen_addr_calc_check(elf, parms))
+ return -1;
+ return 0;
+}
diff -r b111908dd70b -r db3d03dfe92f xen/common/libelf/libelf-loader.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/xen/common/libelf/libelf-loader.c Thu Jan 25 22:16:52 2007 +0000
@@ -0,0 +1,156 @@
+/*
+ * parse and load elf binaries
+ */
+
+#include "libelf-private.h"
+
+/* ------------------------------------------------------------------------ */
+
+int elf_init(struct elf_binary *elf, const char *image, size_t size)
+{
+ const elf_shdr *shdr;
+ uint64_t i, count, section, offset;
+
+ if (!elf_is_elfbinary(image))
+ {
+ elf_err(elf, "%s: not an ELF binary\n", __FUNCTION__);
+ return -1;
+ }
+
+ memset(elf, 0, sizeof(*elf));
+ elf->image = image;
+ elf->size = size;
+ elf->ehdr = (elf_ehdr *) image;
+ elf->class = elf->ehdr->e32.e_ident[EI_CLASS];
+ elf->data = elf->ehdr->e32.e_ident[EI_DATA];
+
+ /* sanity check phdr */
+ offset = elf_uval(elf, elf->ehdr, e_phoff) +
+ elf_uval(elf, elf->ehdr, e_phentsize) * elf_phdr_count(elf);
+ if (offset > elf->size)
+ {
+ elf_err(elf, "%s: phdr overflow (off %" PRIx64 " > size %lx)\n",
+ __FUNCTION__, offset, (unsigned long)elf->size);
+ return -1;
+ }
+
+ /* sanity check shdr */
+ offset = elf_uval(elf, elf->ehdr, e_shoff) +
+ elf_uval(elf, elf->ehdr, e_shentsize) * elf_shdr_count(elf);
+ if (offset > elf->size)
+ {
+ elf_err(elf, "%s: shdr overflow (off %" PRIx64 " > size %lx)\n",
+ __FUNCTION__, offset, (unsigned long)elf->size);
+ return -1;
+ }
+
+ /* find section string table */
+ section = elf_uval(elf, elf->ehdr, e_shstrndx);
+ shdr = elf_shdr_by_index(elf, section);
+ if (NULL != shdr)
+ elf->sec_strtab = elf_section_start(elf, shdr);
+
+ /* find symbol table, symbol string table */
+ count = elf_shdr_count(elf);
+ for (i = 0; i < count; i++)
+ {
+ shdr = elf_shdr_by_index(elf, i);
+ if (elf_uval(elf, shdr, sh_type) != SHT_SYMTAB)
+ continue;
+ elf->sym_tab = shdr;
+ shdr = elf_shdr_by_index(elf, elf_uval(elf, shdr, sh_link));
+ if (NULL == shdr)
+ {
+ elf->sym_tab = NULL;
+ continue;
+ }
+ elf->sym_strtab = elf_section_start(elf, shdr);
+ break;
+ }
+ return 0;
+}
+
+#ifndef __XEN__
+void elf_set_logfile(struct elf_binary *elf, FILE * log, int verbose)
+{
+ elf->log = log;
+ elf->verbose = verbose;
+}
+#else
+void elf_set_verbose(struct elf_binary *elf)
+{
+ elf->verbose = 1;
+}
+#endif
+
+void elf_parse_binary(struct elf_binary *elf)
+{
+ const elf_phdr *phdr;
+ uint64_t low = -1;
+ uint64_t high = 0;
+ uint64_t i, count, paddr, memsz;
+
+ count = elf_uval(elf, elf->ehdr, e_phnum);
+ for (i = 0; i < count; i++)
+ {
+ phdr = elf_phdr_by_index(elf, i);
+ if (!elf_phdr_is_loadable(elf, phdr))
+ continue;
+ paddr = elf_uval(elf, phdr, p_paddr);
+ memsz = elf_uval(elf, phdr, p_memsz);
+ elf_msg(elf, "%s: phdr: paddr=0x%" PRIx64
+ " memsz=0x%" PRIx64 "\n", __FUNCTION__, paddr, memsz);
+ if (low > paddr)
+ low = paddr;
+ if (high < paddr + memsz)
+ high = paddr + memsz;
+ }
+ elf->pstart = low;
+ elf->pend = high;
+ elf_msg(elf, "%s: memory: 0x%" PRIx64 " -> 0x%" PRIx64 "\n",
+ __FUNCTION__, elf->pstart, elf->pend);
+}
+
+void elf_load_binary(struct elf_binary *elf)
+{
+ const elf_phdr *phdr;
+ uint64_t i, count, paddr, offset, filesz, memsz;
+ char *dest;
+
+ count = elf_uval(elf, elf->ehdr, e_phnum);
+ for (i = 0; i < count; i++)
+ {
+ phdr = elf_phdr_by_index(elf, i);
+ if (!elf_phdr_is_loadable(elf, phdr))
+ continue;
+ paddr = elf_uval(elf, phdr, p_paddr);
+ offset = elf_uval(elf, phdr, p_offset);
+ filesz = elf_uval(elf, phdr, p_filesz);
+ memsz = elf_uval(elf, phdr, p_memsz);
+ dest = elf_get_ptr(elf, paddr);
+ memcpy(dest, elf->image + offset, filesz);
+ memset(dest + filesz, 0, memsz - filesz);
+ }
+}
+
+void *elf_get_ptr(struct elf_binary *elf, unsigned long addr)
+{
+ return elf->dest + addr - elf->pstart;
+}
+
+uint64_t elf_lookup_addr(struct elf_binary * elf, const char *symbol)
+{
+ const elf_sym *sym;
+ uint64_t value;
+
+ sym = elf_sym_by_name(elf, symbol);
+ if (NULL == sym)
+ {
+ elf_err(elf, "%s: not found: %s\n", __FUNCTION__, symbol);
+ return -1;
+ }
+ value = elf_uval(elf, sym, st_value);
+ elf_msg(elf, "%s: symbol \"%s\" at 0x%" PRIx64 "\n", __FUNCTION__,
+ symbol, value);
+ return value;
+}
diff -r b111908dd70b -r db3d03dfe92f xen/common/libelf/libelf-private.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/xen/common/libelf/libelf-private.h Thu Jan 25 22:16:52 2007 +0000
@@ -0,0 +1,51 @@
+#ifdef __XEN__
+
+#include <xen/string.h>
+#include <xen/lib.h>
+#include <public/elfnote.h>
+#include <public/libelf.h>
+
+#define elf_msg(elf, fmt, args ... ) \
+ if (elf->verbose) printk(fmt, ## args )
+#define elf_err(elf, fmt, args ... ) \
+ printk(fmt, ## args )
+
+#define strtoull(str, end, base) simple_strtoull(str, end, base)
+#define bswap_16(x) \
+ ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
+#define bswap_32(x) \
+ ( (((x) & 0xff000000) >> 24) \
+ | (((x) & 0x00ff0000) >> 8) \
+ | (((x) & 0x0000ff00) << 8) \
+ | (((x) & 0x000000ff) << 24))
+#define bswap_64(x) \
+ ( (((x) & 0xff00000000000000ull) >> 56) \
+ | (((x) & 0x00ff000000000000ull) >> 40) \
+ | (((x) & 0x0000ff0000000000ull) >> 24) \
+ | (((x) & 0x000000ff00000000ull) >> 8) \
+ | (((x) & 0x00000000ff000000ull) << 8) \
+ | (((x) & 0x0000000000ff0000ull) << 24) \
+ | (((x) & 0x000000000000ff00ull) << 40) \
+ | (((x) & 0x00000000000000ffull) << 56))
+
+#else /* !__XEN__ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <byteswap.h>
+#include <xen/elfnote.h>
+#include <xen/libelf.h>
+
+#include "xenctrl.h"
+#include "xc_private.h"
+
+#define elf_msg(elf, fmt, args ... ) \
+ if (elf->log && elf->verbose) fprintf(elf->log, fmt , ## args )
+#define elf_err(elf, fmt, args ... ) do { \
+ if (elf->log) \
+ fprintf(elf->log, fmt , ## args ); \
+ xc_set_error(XC_INVALID_KERNEL, fmt , ## args ); \
+ } while (0)
+
+#endif
diff -r b111908dd70b -r db3d03dfe92f xen/common/libelf/libelf-relocate.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/xen/common/libelf/libelf-relocate.c Thu Jan 25 22:16:52 2007 +0000
@@ -0,0 +1,345 @@
+/*
+ * ELF relocation code (not used by xen kernel right now).
+ */
+
+#include "libelf-private.h"
+
+/* ------------------------------------------------------------------------ */
+
+static const char *rel_names_i386[] = {
+ "R_386_NONE",
+ "R_386_32",
+ "R_386_PC32",
+ "R_386_GOT32",
+ "R_386_PLT32",
+ "R_386_COPY",
+ "R_386_GLOB_DAT",
+ "R_386_JMP_SLOT",
+ "R_386_RELATIVE",
+ "R_386_GOTOFF",
+ "R_386_GOTPC",
+ "R_386_32PLT",
+ "R_386_TLS_TPOFF",
+ "R_386_TLS_IE",
+ "R_386_TLS_GOTIE",
+ "R_386_TLS_LE",
+ "R_386_TLS_GD",
+ "R_386_TLS_LDM",
+ "R_386_16",
+ "R_386_PC16",
+ "R_386_8",
+ "R_386_PC8",
+ "R_386_TLS_GD_32",
+ "R_386_TLS_GD_PUSH",
+ "R_386_TLS_GD_CALL",
+ "R_386_TLS_GD_POP",
+ "R_386_TLS_LDM_32",
+ "R_386_TLS_LDM_PUSH",
+ "R_386_TLS_LDM_CALL",
+ "R_386_TLS_LDM_POP",
+ "R_386_TLS_LDO_32",
+ "R_386_TLS_IE_32",
+ "R_386_TLS_LE_32",
+ "R_386_TLS_DTPMOD32",
+ "R_386_TLS_DTPOFF32",
+ "R_386_TLS_TPOFF32",
+};
+
+static int elf_reloc_i386(struct elf_binary *elf, int type,
+ uint64_t addr, uint64_t value)
+{
+ void *ptr = elf_get_ptr(elf, addr);
+ uint32_t *u32;
+
+ switch (type)
+ {
+ case 1 /* R_386_32 */ :
+ u32 = ptr;
+ *u32 += elf->reloc_offset;
+ break;
+ case 2 /* R_386_PC32 */ :
+ /* nothing */
+ break;
+ default:
+ return -1;
+ }
+ return 0;
+}
+
+/* ------------------------------------------------------------------------ */
+
+static const char *rel_names_x86_64[] = {
+ "R_X86_64_NONE",
+ "R_X86_64_64",
+ "R_X86_64_PC32",
+ "R_X86_64_GOT32",
+ "R_X86_64_PLT32",
+ "R_X86_64_COPY",
+ "R_X86_64_GLOB_DAT",
+ "R_X86_64_JUMP_SLOT",
+ "R_X86_64_RELATIVE",
+ "R_X86_64_GOTPCREL",
+ "R_X86_64_32",
+ "R_X86_64_32S",
+ "R_X86_64_16",
+ "R_X86_64_PC16",
+ "R_X86_64_8",
+ "R_X86_64_PC8",
+ "R_X86_64_DTPMOD64",
+ "R_X86_64_DTPOFF64",
+ "R_X86_64_TPOFF64",
+ "R_X86_64_TLSGD",
+ "R_X86_64_TLSLD",
+ "R_X86_64_DTPOFF32",
+ "R_X86_64_GOTTPOFF",
+ "R_X86_64_TPOFF32",
+};
+
+static int elf_reloc_x86_64(struct elf_binary *elf, int type,
+ uint64_t addr, uint64_t value)
+{
+ void *ptr = elf_get_ptr(elf, addr);
+ uint64_t *u64;
+ uint32_t *u32;
+ int32_t *s32;
+
+ switch (type)
+ {
+ case 1 /* R_X86_64_64 */ :
+ u64 = ptr;
+ value += elf->reloc_offset;
+ *u64 = value;
+ break;
+ case 2 /* R_X86_64_PC32 */ :
+ u32 = ptr;
+ *u32 = value - addr;
+ if (*u32 != (uint32_t) (value - addr))
+ {
+ elf_err(elf, "R_X86_64_PC32 overflow: 0x%" PRIx32 " != 0x%" PRIx32
"\n",
+ *u32, (uint32_t) (value - addr));
+ return -1;
+ }
+ break;
+ case 10 /* R_X86_64_32 */ :
+ u32 = ptr;
+ value += elf->reloc_offset;
+ *u32 = value;
+ if (*u32 != value)
+ {
+ elf_err(elf, "R_X86_64_32 overflow: 0x%" PRIx32 " != 0x%" PRIx64
"\n",
+ *u32, value);
+ return -1;
+ }
+ break;
+ case 11 /* R_X86_64_32S */ :
+ s32 = ptr;
+ value += elf->reloc_offset;
+ *s32 = value;
+ if (*s32 != (int64_t) value)
+ {
+ elf_err(elf, "R_X86_64_32S overflow: 0x%" PRIx32 " != 0x%" PRIx64
"\n",
+ *s32, (int64_t) value);
+ return -1;
+ }
+ break;
+ default:
+ return -1;
+ }
+ return 0;
+}
+
+/* ------------------------------------------------------------------------ */
+
+static struct relocs {
+ const char **names;
+ int count;
+ int (*func) (struct elf_binary * elf, int type, uint64_t addr,
+ uint64_t value);
+} relocs[] =
+/* *INDENT-OFF* */
+{
+ [EM_386] = {
+ .names = rel_names_i386,
+ .count = sizeof(rel_names_i386) / sizeof(rel_names_i386[0]),
+ .func = elf_reloc_i386,
+ },
+ [EM_X86_64] = {
+ .names = rel_names_x86_64,
+ .count = sizeof(rel_names_x86_64) / sizeof(rel_names_x86_64[0]),
+ .func = elf_reloc_x86_64,
+ }
+};
+/* *INDENT-ON* */
+
+/* ------------------------------------------------------------------------ */
+
+static const char *rela_name(int machine, int type)
+{
+ if (machine > sizeof(relocs) / sizeof(relocs[0]))
+ return "unknown mach";
+ if (!relocs[machine].names)
+ return "unknown mach";
+ if (type > relocs[machine].count)
+ return "unknown rela";
+ return relocs[machine].names[type];
+}
+
+static int elf_reloc_section(struct elf_binary *elf,
+ const elf_shdr * rels,
+ const elf_shdr * sect, const elf_shdr * syms)
+{
+ const void *ptr, *end;
+ const elf_shdr *shdr;
+ const elf_rela *rela;
+ const elf_rel *rel;
+ const elf_sym *sym;
+ uint64_t s_type;
+ uint64_t r_offset;
+ uint64_t r_info;
+ uint64_t r_addend;
+ int r_type, r_sym;
+ size_t rsize;
+ uint64_t shndx, sbase, addr, value;
+ const char *sname;
+ int machine;
+
+ machine = elf_uval(elf, elf->ehdr, e_machine);
+ if (machine >= sizeof(relocs) / sizeof(relocs[0]) ||
+ NULL == relocs[machine].func)
+ {
+ elf_err(elf, "%s: can't handle machine %d\n",
+ __FUNCTION__, machine);
+ return -1;
+ }
+ if (elf_swap(elf))
+ {
+ elf_err(elf, "%s: non-native byte order, relocation not supported\n",
+ __FUNCTION__);
+ return -1;
+ }
+
+ s_type = elf_uval(elf, rels, sh_type);
+ rsize = (SHT_REL == s_type) ? elf_size(elf, rel) : elf_size(elf, rela);
+ ptr = elf_section_start(elf, rels);
+ end = elf_section_end(elf, rels);
+
+ for (; ptr < end; ptr += rsize)
+ {
+ switch (s_type)
+ {
+ case SHT_REL:
+ rel = ptr;
+ r_offset = elf_uval(elf, rel, r_offset);
+ r_info = elf_uval(elf, rel, r_info);
+ r_addend = 0;
+ break;
+ case SHT_RELA:
+ rela = ptr;
+ r_offset = elf_uval(elf, rela, r_offset);
+ r_info = elf_uval(elf, rela, r_info);
+ r_addend = elf_uval(elf, rela, r_addend);
+ break;
+ default:
+ /* can't happen */
+ return -1;
+ }
+ if (elf_64bit(elf))
+ {
+ r_type = ELF64_R_TYPE(r_info);
+ r_sym = ELF64_R_SYM(r_info);
+ }
+ else
+ {
+ r_type = ELF32_R_TYPE(r_info);
+ r_sym = ELF32_R_SYM(r_info);
+ }
+
+ sym = elf_sym_by_index(elf, r_sym);
+ shndx = elf_uval(elf, sym, st_shndx);
+ switch (shndx)
+ {
+ case SHN_UNDEF:
+ sname = "*UNDEF*";
+ sbase = 0;
+ break;
+ case SHN_COMMON:
+ elf_err(elf, "%s: invalid section: %" PRId64 "\n",
+ __FUNCTION__, shndx);
+ return -1;
+ case SHN_ABS:
+ sname = "*ABS*";
+ sbase = 0;
+ break;
+ default:
+ shdr = elf_shdr_by_index(elf, shndx);
+ if (NULL == shdr)
+ {
+ elf_err(elf, "%s: invalid section: %" PRId64 "\n",
+ __FUNCTION__, shndx);
+ return -1;
+ }
+ sname = elf_section_name(elf, shdr);
+ sbase = elf_uval(elf, shdr, sh_addr);
+ }
+
+ addr = r_offset;
+ value = elf_uval(elf, sym, st_value);
+ value += r_addend;
+
+ if (elf->log && elf->verbose > 1)
+ {
+ uint64_t st_name = elf_uval(elf, sym, st_name);
+ const char *name = st_name ? elf->sym_strtab + st_name : "*NONE*";
+
+ elf_msg(elf,
+ "%s: type %s [%d], off 0x%" PRIx64 ", add 0x%" PRIx64 ","
+ " sym %s [0x%" PRIx64 "], sec %s [0x%" PRIx64 "]"
+ " -> addr 0x%" PRIx64 " value 0x%" PRIx64 "\n",
+ __FUNCTION__, rela_name(machine, r_type), r_type, r_offset,
+ r_addend, name, elf_uval(elf, sym, st_value), sname, sbase,
+ addr, value);
+ }
+
+ if (-1 == relocs[machine].func(elf, r_type, addr, value))
+ {
+ elf_err(elf, "%s: unknown/unsupported reloc type %s [%d]\n",
+ __FUNCTION__, rela_name(machine, r_type), r_type);
+ return -1;
+ }
+ }
+ return 0;
+}
+
+int elf_reloc(struct elf_binary *elf)
+{
+ const elf_shdr *rels, *sect, *syms;
+ uint64_t i, count, type;
+
+ count = elf_shdr_count(elf);
+ for (i = 0; i < count; i++)
+ {
+ rels = elf_shdr_by_index(elf, i);
+ type = elf_uval(elf, rels, sh_type);
+ if (type != SHT_REL && type != SHT_RELA)
+ continue;
+
+ sect = elf_shdr_by_index(elf, elf_uval(elf, rels, sh_info));
+ syms = elf_shdr_by_index(elf, elf_uval(elf, rels, sh_link));
+ if (NULL == sect || NULL == syms)
+ continue;
+
+ if (!(elf_uval(elf, sect, sh_flags) & SHF_ALLOC))
+ {
+ elf_msg(elf, "%s: relocations for %s, skipping\n",
+ __FUNCTION__, elf_section_name(elf, sect));
+ continue;
+ }
+
+ elf_msg(elf, "%s: relocations for %s @ 0x%" PRIx64 "\n",
+ __FUNCTION__, elf_section_name(elf, sect),
+ elf_uval(elf, sect, sh_addr));
+ if (0 != elf_reloc_section(elf, rels, sect, syms))
+ return -1;
+ }
+ return 0;
+}
diff -r b111908dd70b -r db3d03dfe92f xen/common/libelf/libelf-tools.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/xen/common/libelf/libelf-tools.c Thu Jan 25 22:16:52 2007 +0000
@@ -0,0 +1,225 @@
+/*
+ * various helper functions to access elf structures
+ */
+
+#include "libelf-private.h"
+
+/* ------------------------------------------------------------------------ */
+
+uint64_t elf_access_unsigned(struct elf_binary * elf, const void *ptr,
+ off_t offset, size_t size)
+{
+ int need_swap = elf_swap(elf);
+ const uint8_t *u8;
+ const uint16_t *u16;
+ const uint32_t *u32;
+ const uint64_t *u64;
+
+ switch (size)
+ {
+ case 1:
+ u8 = ptr + offset;
+ return *u8;
+ case 2:
+ u16 = ptr + offset;
+ return need_swap ? bswap_16(*u16) : *u16;
+ case 4:
+ u32 = ptr + offset;
+ return need_swap ? bswap_32(*u32) : *u32;
+ case 8:
+ u64 = ptr + offset;
+ return need_swap ? bswap_64(*u64) : *u64;
+ default:
+ return 0;
+ }
+}
+
+int64_t elf_access_signed(struct elf_binary *elf, const void *ptr,
+ off_t offset, size_t size)
+{
+ int need_swap = elf_swap(elf);
+ const int8_t *s8;
+ const int16_t *s16;
+ const int32_t *s32;
+ const int64_t *s64;
+
+ switch (size)
+ {
+ case 1:
+ s8 = ptr + offset;
+ return *s8;
+ case 2:
+ s16 = ptr + offset;
+ return need_swap ? bswap_16(*s16) : *s16;
+ case 4:
+ s32 = ptr + offset;
+ return need_swap ? bswap_32(*s32) : *s32;
+ case 8:
+ s64 = ptr + offset;
+ return need_swap ? bswap_64(*s64) : *s64;
+ default:
+ return 0;
+ }
+}
+
+uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr)
+{
+ int elf_round = (elf_64bit(elf) ? 8 : 4) - 1;
+
+ return (addr + elf_round) & ~elf_round;
+}
+
+/* ------------------------------------------------------------------------ */
+
+int elf_shdr_count(struct elf_binary *elf)
+{
+ return elf_uval(elf, elf->ehdr, e_shnum);
+}
+
+int elf_phdr_count(struct elf_binary *elf)
+{
+ return elf_uval(elf, elf->ehdr, e_phnum);
+}
+
+const elf_shdr *elf_shdr_by_name(struct elf_binary *elf, const char *name)
+{
+ uint64_t count = elf_shdr_count(elf);
+ const elf_shdr *shdr;
+ const char *sname;
+ int i;
+
+ for (i = 0; i < count; i++) {
+ shdr = elf_shdr_by_index(elf, i);
+ sname = elf_section_name(elf, shdr);
+ if (sname && 0 == strcmp(sname, name))
+ return shdr;
+ }
+ return NULL;
+}
+
+const elf_shdr *elf_shdr_by_index(struct elf_binary *elf, int index)
+{
+ uint64_t count = elf_shdr_count(elf);
+ const void *ptr = NULL;
+
+ if (index < count)
+ ptr = elf->image
+ + elf_uval(elf, elf->ehdr, e_shoff)
+ + elf_uval(elf, elf->ehdr, e_shentsize) * index;
+ return ptr;
+}
+
+const elf_phdr *elf_phdr_by_index(struct elf_binary *elf, int index)
+{
+ uint64_t count = elf_uval(elf, elf->ehdr, e_phnum);
+ const void *ptr = NULL;
+
+ if (index < count)
+ ptr = elf->image
+ + elf_uval(elf, elf->ehdr, e_phoff)
+ + elf_uval(elf, elf->ehdr, e_phentsize) * index;
+ return ptr;
+}
+
+const char *elf_section_name(struct elf_binary *elf, const elf_shdr * shdr)
+{
+ if (NULL == elf->sec_strtab)
+ return "unknown";
+ return elf->sec_strtab + elf_uval(elf, shdr, sh_name);
+}
+
+const void *elf_section_start(struct elf_binary *elf, const elf_shdr * shdr)
+{
+ return elf->image + elf_uval(elf, shdr, sh_offset);
+}
+
+const void *elf_section_end(struct elf_binary *elf, const elf_shdr * shdr)
+{
+ return elf->image
+ + elf_uval(elf, shdr, sh_offset) + elf_uval(elf, shdr, sh_size);
+}
+
+const elf_sym *elf_sym_by_name(struct elf_binary *elf, const char *symbol)
+{
+ const void *ptr = elf_section_start(elf, elf->sym_tab);
+ const void *end = elf_section_end(elf, elf->sym_tab);
+ const elf_sym *sym;
+ uint64_t info, name;
+
+ for (; ptr < end; ptr += elf_size(elf, sym))
+ {
+ sym = ptr;
+ info = elf_uval(elf, sym, st_info);
+ name = elf_uval(elf, sym, st_name);
+ if (ELF32_ST_BIND(info) != STB_GLOBAL)
+ continue;
+ if (strcmp(elf->sym_strtab + name, symbol) != 0)
+ continue;
+ return sym;
+ }
+ return NULL;
+}
+
+const elf_sym *elf_sym_by_index(struct elf_binary *elf, int index)
+{
+ const void *ptr = elf_section_start(elf, elf->sym_tab);
+ const elf_sym *sym;
+
+ sym = ptr + index * elf_size(elf, sym);
+ return sym;
+}
+
+const char *elf_note_name(struct elf_binary *elf, const elf_note * note)
+{
+ return (void *)note + elf_size(elf, note);
+}
+
+const void *elf_note_desc(struct elf_binary *elf, const elf_note * note)
+{
+ int namesz = (elf_uval(elf, note, namesz) + 3) & ~3;
+
+ return (void *)note + elf_size(elf, note) + namesz;
+}
+
+uint64_t elf_note_numeric(struct elf_binary *elf, const elf_note * note)
+{
+ const void *desc = elf_note_desc(elf, note);
+ int descsz = elf_uval(elf, note, descsz);
+
+ switch (descsz)
+ {
+ case 1:
+ case 2:
+ case 4:
+ case 8:
+ return elf_access_unsigned(elf, desc, 0, descsz);
+ default:
+ return 0;
+ }
+}
+const elf_note *elf_note_next(struct elf_binary *elf, const elf_note * note)
+{
+ int namesz = (elf_uval(elf, note, namesz) + 3) & ~3;
+ int descsz = (elf_uval(elf, note, descsz) + 3) & ~3;
+
+ return (void *)note + elf_size(elf, note) + namesz + descsz;
+}
+
+/* ------------------------------------------------------------------------ */
+
+int elf_is_elfbinary(const void *image)
+{
+ const Elf32_Ehdr *ehdr = image;
+
+ if (IS_ELF(*ehdr))
+ return 1;
+ return 0;
+}
+
+int elf_phdr_is_loadable(struct elf_binary *elf, const elf_phdr * phdr)
+{
+ uint64_t p_type = elf_uval(elf, phdr, p_type);
+ uint64_t p_flags = elf_uval(elf, phdr, p_flags);
+
+ return ((p_type == PT_LOAD) && (p_flags & (PF_W | PF_X)) != 0);
+}
diff -r b111908dd70b -r db3d03dfe92f xen/include/public/elfstructs.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/xen/include/public/elfstructs.h Thu Jan 25 22:16:52 2007 +0000
@@ -0,0 +1,527 @@
+#ifndef __XEN_PUBLIC_ELFSTRUCTS_H__
+#define __XEN_PUBLIC_ELFSTRUCTS_H__ 1
+/*
+ * Copyright (c) 1995, 1996 Erik Theisen. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+typedef uint8_t Elf_Byte;
+
+typedef uint32_t Elf32_Addr; /* Unsigned program address */
+typedef uint32_t Elf32_Off; /* Unsigned file offset */
+typedef int32_t Elf32_Sword; /* Signed large integer */
+typedef uint32_t Elf32_Word; /* Unsigned large integer */
+typedef uint16_t Elf32_Half; /* Unsigned medium integer */
+
+typedef uint64_t Elf64_Addr;
+typedef uint64_t Elf64_Off;
+typedef int32_t Elf64_Shalf;
+
+typedef int32_t Elf64_Sword;
+typedef uint32_t Elf64_Word;
+
+typedef int64_t Elf64_Sxword;
+typedef uint64_t Elf64_Xword;
+
+typedef uint32_t Elf64_Half;
+typedef uint16_t Elf64_Quarter;
+
+/*
+ * e_ident[] identification indexes
+ * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html
+ */
+#define EI_MAG0 0 /* file ID */
+#define EI_MAG1 1 /* file ID */
+#define EI_MAG2 2 /* file ID */
+#define EI_MAG3 3 /* file ID */
+#define EI_CLASS 4 /* file class */
+#define EI_DATA 5 /* data encoding */
+#define EI_VERSION 6 /* ELF header version */
+#define EI_OSABI 7 /* OS/ABI ID */
+#define EI_ABIVERSION 8 /* ABI version */
+#define EI_PAD 9 /* start of pad bytes */
+#define EI_NIDENT 16 /* Size of e_ident[] */
+
+/* e_ident[] magic number */
+#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */
+#define ELFMAG1 'E' /* e_ident[EI_MAG1] */
+#define ELFMAG2 'L' /* e_ident[EI_MAG2] */
+#define ELFMAG3 'F' /* e_ident[EI_MAG3] */
+#define ELFMAG "\177ELF" /* magic */
+#define SELFMAG 4 /* size of magic */
+
+/* e_ident[] file class */
+#define ELFCLASSNONE 0 /* invalid */
+#define ELFCLASS32 1 /* 32-bit objs */
+#define ELFCLASS64 2 /* 64-bit objs */
+#define ELFCLASSNUM 3 /* number of classes */
+
+/* e_ident[] data encoding */
+#define ELFDATANONE 0 /* invalid */
+#define ELFDATA2LSB 1 /* Little-Endian */
+#define ELFDATA2MSB 2 /* Big-Endian */
+#define ELFDATANUM 3 /* number of data encode defines */
+
+/* e_ident[] Operating System/ABI */
+#define ELFOSABI_SYSV 0 /* UNIX System V ABI */
+#define ELFOSABI_HPUX 1 /* HP-UX operating system */
+#define ELFOSABI_NETBSD 2 /* NetBSD */
+#define ELFOSABI_LINUX 3 /* GNU/Linux */
+#define ELFOSABI_HURD 4 /* GNU/Hurd */
+#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */
+#define ELFOSABI_SOLARIS 6 /* Solaris */
+#define ELFOSABI_MONTEREY 7 /* Monterey */
+#define ELFOSABI_IRIX 8 /* IRIX */
+#define ELFOSABI_FREEBSD 9 /* FreeBSD */
+#define ELFOSABI_TRU64 10 /* TRU64 UNIX */
+#define ELFOSABI_MODESTO 11 /* Novell Modesto */
+#define ELFOSABI_OPENBSD 12 /* OpenBSD */
+#define ELFOSABI_ARM 97 /* ARM */
+#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
+
+/* e_ident */
+#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
+ (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
+ (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
+ (ehdr).e_ident[EI_MAG3] == ELFMAG3)
+
+/* ELF Header */
+typedef struct elfhdr {
+ unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
+ Elf32_Half e_type; /* object file type */
+ Elf32_Half e_machine; /* machine */
+ Elf32_Word e_version; /* object file version */
+ Elf32_Addr e_entry; /* virtual entry point */
+ Elf32_Off e_phoff; /* program header table offset */
+ Elf32_Off e_shoff; /* section header table offset */
+ Elf32_Word e_flags; /* processor-specific flags */
+ Elf32_Half e_ehsize; /* ELF header size */
+ Elf32_Half e_phentsize; /* program header entry size */
+ Elf32_Half e_phnum; /* number of program header entries */
+ Elf32_Half e_shentsize; /* section header entry size */
+ Elf32_Half e_shnum; /* number of section header entries */
+ Elf32_Half e_shstrndx; /* section header table's "section
+ header string table" entry offset */
+} Elf32_Ehdr;
+
+typedef struct {
+ unsigned char e_ident[EI_NIDENT]; /* Id bytes */
+ Elf64_Quarter e_type; /* file type */
+ Elf64_Quarter e_machine; /* machine type */
+ Elf64_Half e_version; /* version number */
+ Elf64_Addr e_entry; /* entry point */
+ Elf64_Off e_phoff; /* Program hdr offset */
+ Elf64_Off e_shoff; /* Section hdr offset */
+ Elf64_Half e_flags; /* Processor flags */
+ Elf64_Quarter e_ehsize; /* sizeof ehdr */
+ Elf64_Quarter e_phentsize; /* Program header entry size */
+ Elf64_Quarter e_phnum; /* Number of program headers */
+ Elf64_Quarter e_shentsize; /* Section header entry size */
+ Elf64_Quarter e_shnum; /* Number of section headers */
+ Elf64_Quarter e_shstrndx; /* String table index */
+} Elf64_Ehdr;
+
+/* e_type */
+#define ET_NONE 0 /* No file type */
+#define ET_REL 1 /* relocatable file */
+#define ET_EXEC 2 /* executable file */
+#define ET_DYN 3 /* shared object file */
+#define ET_CORE 4 /* core file */
+#define ET_NUM 5 /* number of types */
+#define ET_LOPROC 0xff00 /* reserved range for processor */
+#define ET_HIPROC 0xffff /* specific e_type */
+
+/* e_machine */
+#define EM_NONE 0 /* No Machine */
+#define EM_M32 1 /* AT&T WE 32100 */
+#define EM_SPARC 2 /* SPARC */
+#define EM_386 3 /* Intel 80386 */
+#define EM_68K 4 /* Motorola 68000 */
+#define EM_88K 5 /* Motorola 88000 */
+#define EM_486 6 /* Intel 80486 - unused? */
+#define EM_860 7 /* Intel 80860 */
+#define EM_MIPS 8 /* MIPS R3000 Big-Endian only */
+/*
+ * Don't know if EM_MIPS_RS4_BE,
+ * EM_SPARC64, EM_PARISC,
+ * or EM_PPC are ABI compliant
+ */
+#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */
+#define EM_SPARC64 11 /* SPARC v9 64-bit unoffical */
+#define EM_PARISC 15 /* HPPA */
+#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */
+#define EM_PPC 20 /* PowerPC */
+#define EM_PPC64 21 /* PowerPC 64-bit */
+#define EM_ARM 40 /* Advanced RISC Machines ARM */
+#define EM_ALPHA 41 /* DEC ALPHA */
+#define EM_SPARCV9 43 /* SPARC version 9 */
+#define EM_ALPHA_EXP 0x9026 /* DEC ALPHA */
+#define EM_IA_64 50 /* Intel Merced */
+#define EM_X86_64 62 /* AMD x86-64 architecture */
+#define EM_VAX 75 /* DEC VAX */
+
+/* Version */
+#define EV_NONE 0 /* Invalid */
+#define EV_CURRENT 1 /* Current */
+#define EV_NUM 2 /* number of versions */
+
+/* Section Header */
+typedef struct {
+ Elf32_Word sh_name; /* name - index into section header
+ string table section */
+ Elf32_Word sh_type; /* type */
+ Elf32_Word sh_flags; /* flags */
+ Elf32_Addr sh_addr; /* address */
+ Elf32_Off sh_offset; /* file offset */
+ Elf32_Word sh_size; /* section size */
+ Elf32_Word sh_link; /* section header table index link */
+ Elf32_Word sh_info; /* extra information */
+ Elf32_Word sh_addralign; /* address alignment */
+ Elf32_Word sh_entsize; /* section entry size */
+} Elf32_Shdr;
+
+typedef struct {
+ Elf64_Half sh_name; /* section name */
+ Elf64_Half sh_type; /* section type */
+ Elf64_Xword sh_flags; /* section flags */
+ Elf64_Addr sh_addr; /* virtual address */
+ Elf64_Off sh_offset; /* file offset */
+ Elf64_Xword sh_size; /* section size */
+ Elf64_Half sh_link; /* link to another */
+ Elf64_Half sh_info; /* misc info */
+ Elf64_Xword sh_addralign; /* memory alignment */
+ Elf64_Xword sh_entsize; /* table entry size */
+} Elf64_Shdr;
+
+/* Special Section Indexes */
+#define SHN_UNDEF 0 /* undefined */
+#define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */
+#define SHN_LOPROC 0xff00 /* reserved range for processor */
+#define SHN_HIPROC 0xff1f /* specific section indexes */
+#define SHN_ABS 0xfff1 /* absolute value */
+#define SHN_COMMON 0xfff2 /* common symbol */
+#define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */
+
+/* sh_type */
+#define SHT_NULL 0 /* inactive */
+#define SHT_PROGBITS 1 /* program defined information */
+#define SHT_SYMTAB 2 /* symbol table section */
+#define SHT_STRTAB 3 /* string table section */
+#define SHT_RELA 4 /* relocation section with addends*/
+#define SHT_HASH 5 /* symbol hash table section */
+#define SHT_DYNAMIC 6 /* dynamic section */
+#define SHT_NOTE 7 /* note section */
+#define SHT_NOBITS 8 /* no space section */
+#define SHT_REL 9 /* relation section without
addends */
+#define SHT_SHLIB 10 /* reserved - purpose unknown */
+#define SHT_DYNSYM 11 /* dynamic symbol table section */
+#define SHT_NUM 12 /* number of section types */
+#define SHT_LOPROC 0x70000000 /* reserved range for processor */
+#define SHT_HIPROC 0x7fffffff /* specific section header types */
+#define SHT_LOUSER 0x80000000 /* reserved range for application */
+#define SHT_HIUSER 0xffffffff /* specific indexes */
+
+/* Section names */
+#define ELF_BSS ".bss" /* uninitialized data */
+#define ELF_DATA ".data" /* initialized data */
+#define ELF_DEBUG ".debug" /* debug */
+#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */
+#define ELF_DYNSTR ".dynstr" /* dynamic string table */
+#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */
+#define ELF_FINI ".fini" /* termination code */
+#define ELF_GOT ".got" /* global offset table */
+#define ELF_HASH ".hash" /* symbol hash table */
+#define ELF_INIT ".init" /* initialization code */
+#define ELF_REL_DATA ".rel.data" /* relocation data */
+#define ELF_REL_FINI ".rel.fini" /* relocation termination code */
+#define ELF_REL_INIT ".rel.init" /* relocation initialization code */
+#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */
+#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */
+#define ELF_REL_TEXT ".rel.text" /* relocation code */
+#define ELF_RODATA ".rodata" /* read-only data */
+#define ELF_SHSTRTAB ".shstrtab" /* section header string table */
+#define ELF_STRTAB ".strtab" /* string table */
+#define ELF_SYMTAB ".symtab" /* symbol table */
+#define ELF_TEXT ".text" /* code */
+
+
+/* Section Attribute Flags - sh_flags */
+#define SHF_WRITE 0x1 /* Writable */
+#define SHF_ALLOC 0x2 /* occupies memory */
+#define SHF_EXECINSTR 0x4 /* executable */
+#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
+ /* specific section attributes */
+
+/* Symbol Table Entry */
+typedef struct elf32_sym {
+ Elf32_Word st_name; /* name - index into string table */
+ Elf32_Addr st_value; /* symbol value */
+ Elf32_Word st_size; /* symbol size */
+ unsigned char st_info; /* type and binding */
+ unsigned char st_other; /* 0 - no defined meaning */
+ Elf32_Half st_shndx; /* section header index */
+} Elf32_Sym;
+
+typedef struct {
+ Elf64_Half st_name; /* Symbol name index in str table */
+ Elf_Byte st_info; /* type / binding attrs */
+ Elf_Byte st_other; /* unused */
+ Elf64_Quarter st_shndx; /* section index of symbol */
+ Elf64_Xword st_value; /* value of symbol */
+ Elf64_Xword st_size; /* size of symbol */
+} Elf64_Sym;
+
+/* Symbol table index */
+#define STN_UNDEF 0 /* undefined */
+
+/* Extract symbol info - st_info */
+#define ELF32_ST_BIND(x) ((x) >> 4)
+#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf)
+#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf))
+
+#define ELF64_ST_BIND(x) ((x) >> 4)
+#define ELF64_ST_TYPE(x) (((unsigned int) x) & 0xf)
+#define ELF64_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf))
+
+/* Symbol Binding - ELF32_ST_BIND - st_info */
+#define STB_LOCAL 0 /* Local symbol */
+#define STB_GLOBAL 1 /* Global symbol */
+#define STB_WEAK 2 /* like global - lower precedence */
+#define STB_NUM 3 /* number of symbol bindings */
+#define STB_LOPROC 13 /* reserved range for processor */
+#define STB_HIPROC 15 /* specific symbol bindings */
+
+/* Symbol type - ELF32_ST_TYPE - st_info */
+#define STT_NOTYPE 0 /* not specified */
+#define STT_OBJECT 1 /* data object */
+#define STT_FUNC 2 /* function */
+#define STT_SECTION 3 /* section */
+#define STT_FILE 4 /* file */
+#define STT_NUM 5 /* number of symbol types */
+#define STT_LOPROC 13 /* reserved range for processor */
+#define STT_HIPROC 15 /* specific symbol types */
+
+/* Relocation entry with implicit addend */
+typedef struct {
+ Elf32_Addr r_offset; /* offset of relocation */
+ Elf32_Word r_info; /* symbol table index and type */
+} Elf32_Rel;
+
+/* Relocation entry with explicit addend */
+typedef struct {
+ Elf32_Addr r_offset; /* offset of relocation */
+ Elf32_Word r_info; /* symbol table index and type */
+ Elf32_Sword r_addend;
+} Elf32_Rela;
+
+/* Extract relocation info - r_info */
+#define ELF32_R_SYM(i) ((i) >> 8)
+#define ELF32_R_TYPE(i) ((unsigned char) (i))
+#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
+
+typedef struct {
+ Elf64_Xword r_offset; /* where to do it */
+ Elf64_Xword r_info; /* index & type of relocation */
+} Elf64_Rel;
+
+typedef struct {
+ Elf64_Xword r_offset; /* where to do it */
+ Elf64_Xword r_info; /* index & type of relocation */
+ Elf64_Sxword r_addend; /* adjustment value */
+} Elf64_Rela;
+
+#define ELF64_R_SYM(info) ((info) >> 32)
+#define ELF64_R_TYPE(info) ((info) & 0xFFFFFFFF)
+#define ELF64_R_INFO(s,t) (((s) << 32) + (u_int32_t)(t))
+
+/* Program Header */
+typedef struct {
+ Elf32_Word p_type; /* segment type */
+ Elf32_Off p_offset; /* segment offset */
+ Elf32_Addr p_vaddr; /* virtual address of segment */
+ Elf32_Addr p_paddr; /* physical address - ignored? */
+ Elf32_Word p_filesz; /* number of bytes in file for seg. */
+ Elf32_Word p_memsz; /* number of bytes in mem. for seg. */
+ Elf32_Word p_flags; /* flags */
+ Elf32_Word p_align; /* memory alignment */
+} Elf32_Phdr;
+
+typedef struct {
+ Elf64_Half p_type; /* entry type */
+ Elf64_Half p_flags; /* flags */
+ Elf64_Off p_offset; /* offset */
+ Elf64_Addr p_vaddr; /* virtual address */
+ Elf64_Addr p_paddr; /* physical address */
+ Elf64_Xword p_filesz; /* file size */
+ Elf64_Xword p_memsz; /* memory size */
+ Elf64_Xword p_align; /* memory & file alignment */
+} Elf64_Phdr;
+
+/* Segment types - p_type */
+#define PT_NULL 0 /* unused */
+#define PT_LOAD 1 /* loadable segment */
+#define PT_DYNAMIC 2 /* dynamic linking section */
+#define PT_INTERP 3 /* the RTLD */
+#define PT_NOTE 4 /* auxiliary information */
+#define PT_SHLIB 5 /* reserved - purpose undefined */
+#define PT_PHDR 6 /* program header */
+#define PT_NUM 7 /* Number of segment types */
+#define PT_LOPROC 0x70000000 /* reserved range for processor */
+#define PT_HIPROC 0x7fffffff /* specific segment types */
+
+/* Segment flags - p_flags */
+#define PF_X 0x1 /* Executable */
+#define PF_W 0x2 /* Writable */
+#define PF_R 0x4 /* Readable */
+#define PF_MASKPROC 0xf0000000 /* reserved bits for processor */
+ /* specific segment flags */
+
+/* Dynamic structure */
+typedef struct {
+ Elf32_Sword d_tag; /* controls meaning of d_val */
+ union {
+ Elf32_Word d_val; /* Multiple meanings - see d_tag */
+ Elf32_Addr d_ptr; /* program virtual address */
+ } d_un;
+} Elf32_Dyn;
+
+typedef struct {
+ Elf64_Xword d_tag; /* controls meaning of d_val */
+ union {
+ Elf64_Addr d_ptr;
+ Elf64_Xword d_val;
+ } d_un;
+} Elf64_Dyn;
+
+/* Dynamic Array Tags - d_tag */
+#define DT_NULL 0 /* marks end of _DYNAMIC array
*/
+#define DT_NEEDED 1 /* string table offset of needed lib */
+#define DT_PLTRELSZ 2 /* size of relocation entries in PLT */
+#define DT_PLTGOT 3 /* address PLT/GOT */
+#define DT_HASH 4 /* address of symbol hash table
*/
+#define DT_STRTAB 5 /* address of string table */
+#define DT_SYMTAB 6 /* address of symbol table */
+#define DT_RELA 7 /* address of relocation table
*/
+#define DT_RELASZ 8 /* size of relocation table */
+#define DT_RELAENT 9 /* size of relocation entry */
+#define DT_STRSZ 10 /* size of string table */
+#define DT_SYMENT 11 /* size of symbol table entry */
+#define DT_INIT 12 /* address of initialization
func. */
+#define DT_FINI 13 /* address of termination
function */
+#define DT_SONAME 14 /* string table offset of shared obj */
+#define DT_RPATH 15 /* string table offset of library
+ search path */
+#define DT_SYMBOLIC 16 /* start sym search in shared obj. */
+#define DT_REL 17 /* address of rel. tbl. w addends */
+#define DT_RELSZ 18 /* size of DT_REL relocation table */
+#define DT_RELENT 19 /* size of DT_REL relocation entry */
+#define DT_PLTREL 20 /* PLT referenced relocation entry */
+#define DT_DEBUG 21 /* bugger */
+#define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */
+#define DT_JMPREL 23 /* add. of PLT's relocation entries */
+#define DT_BIND_NOW 24 /* Bind now regardless of env setting */
+#define DT_NUM 25 /* Number used. */
+#define DT_LOPROC 0x70000000 /* reserved range for processor */
+#define DT_HIPROC 0x7fffffff /* specific dynamic array tags */
+
+/* Standard ELF hashing function */
+unsigned int elf_hash(const unsigned char *name);
+
+/*
+ * Note Definitions
+ */
+typedef struct {
+ Elf32_Word namesz;
+ Elf32_Word descsz;
+ Elf32_Word type;
+} Elf32_Note;
+
+typedef struct {
+ Elf64_Half namesz;
+ Elf64_Half descsz;
+ Elf64_Half type;
+} Elf64_Note;
+
+
+#if defined(ELFSIZE)
+#define CONCAT(x,y) __CONCAT(x,y)
+#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
+#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
+#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
+#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
+#endif
+
+#if defined(ELFSIZE) && (ELFSIZE == 32)
+#define Elf_Ehdr Elf32_Ehdr
+#define Elf_Phdr Elf32_Phdr
+#define Elf_Shdr Elf32_Shdr
+#define Elf_Sym Elf32_Sym
+#define Elf_Rel Elf32_Rel
+#define Elf_RelA Elf32_Rela
+#define Elf_Dyn Elf32_Dyn
+#define Elf_Word Elf32_Word
+#define Elf_Sword Elf32_Sword
+#define Elf_Addr Elf32_Addr
+#define Elf_Off Elf32_Off
+#define Elf_Nhdr Elf32_Nhdr
+#define Elf_Note Elf32_Note
+
+#define ELF_R_SYM ELF32_R_SYM
+#define ELF_R_TYPE ELF32_R_TYPE
+#define ELF_R_INFO ELF32_R_INFO
+#define ELFCLASS ELFCLASS32
+
+#define ELF_ST_BIND ELF32_ST_BIND
+#define ELF_ST_TYPE ELF32_ST_TYPE
+#define ELF_ST_INFO ELF32_ST_INFO
+
+#define AuxInfo Aux32Info
+#elif defined(ELFSIZE) && (ELFSIZE == 64)
+#define Elf_Ehdr Elf64_Ehdr
+#define Elf_Phdr Elf64_Phdr
+#define Elf_Shdr Elf64_Shdr
+#define Elf_Sym Elf64_Sym
+#define Elf_Rel Elf64_Rel
+#define Elf_RelA Elf64_Rela
+#define Elf_Dyn Elf64_Dyn
+#define Elf_Word Elf64_Word
+#define Elf_Sword Elf64_Sword
+#define Elf_Addr Elf64_Addr
+#define Elf_Off Elf64_Off
+#define Elf_Nhdr Elf64_Nhdr
+#define Elf_Note Elf64_Note
+
+#define ELF_R_SYM ELF64_R_SYM
+#define ELF_R_TYPE ELF64_R_TYPE
+#define ELF_R_INFO ELF64_R_INFO
+#define ELFCLASS ELFCLASS64
+
+#define ELF_ST_BIND ELF64_ST_BIND
+#define ELF_ST_TYPE ELF64_ST_TYPE
+#define ELF_ST_INFO ELF64_ST_INFO
+
+#define AuxInfo Aux64Info
+#endif
+
+#endif /* __XEN_PUBLIC_ELFSTRUCTS_H__ */
diff -r b111908dd70b -r db3d03dfe92f xen/include/public/libelf.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/xen/include/public/libelf.h Thu Jan 25 22:16:52 2007 +0000
@@ -0,0 +1,238 @@
+#ifndef __XC_LIBELF__
+#define __XC_LIBELF__ 1
+
+#ifdef __XEN__
+
+#include <xen/inttypes.h>
+#include <public/features.h>
+typedef uint64_t off_t;
+
+#define LITTLE_ENDIAN 1234
+#define BIG_ENDIAN 4321
+#if defined(__i386__) || defined(__x86_64) || defined(__ia64__)
+#define BYTE_ORDER LITTLE_ENDIAN
+#elif defined(__powerpc__)
+#define BYTE_ORDER BIG_ENDIAN
+#endif
+
+#else /* !__XEN__ */
+
+#include <stddef.h>
+#include <inttypes.h>
+
+#endif
+
+#undef ELFSIZE
+#include "elfstructs.h"
+#include "features.h"
+
+/* ------------------------------------------------------------------------ */
+
+typedef union {
+ Elf32_Ehdr e32;
+ Elf64_Ehdr e64;
+} elf_ehdr;
+
+typedef union {
+ Elf32_Phdr e32;
+ Elf64_Phdr e64;
+} elf_phdr;
+
+typedef union {
+ Elf32_Shdr e32;
+ Elf64_Shdr e64;
+} elf_shdr;
+
+typedef union {
+ Elf32_Sym e32;
+ Elf64_Sym e64;
+} elf_sym;
+
+typedef union {
+ Elf32_Rel e32;
+ Elf64_Rel e64;
+} elf_rel;
+
+typedef union {
+ Elf32_Rela e32;
+ Elf64_Rela e64;
+} elf_rela;
+
+typedef union {
+ Elf32_Note e32;
+ Elf64_Note e64;
+} elf_note;
+
+struct elf_binary {
+ /* elf binary */
+ const char *image;
+ size_t size;
+ char class;
+ char data;
+
+ const elf_ehdr *ehdr;
+ const char *sec_strtab;
+ const elf_shdr *sym_tab;
+ const char *sym_strtab;
+
+ /* loaded to */
+ char *dest;
+ uint64_t pstart;
+ uint64_t pend;
+ uint64_t reloc_offset;
+
+#ifndef __XEN__
+ /* misc */
+ FILE *log;
+#endif
+ int verbose;
+};
+
+/* ------------------------------------------------------------------------ */
+/* accessing elf header fields */
+
+#if !defined(BYTE_ORDER)
+# error BYTE_ORDER not defined
+#elif BYTE_ORDER == BIG_ENDIAN
+# define NATIVE_ELFDATA ELFDATA2MSB
+#elif BYTE_ORDER == LITTLE_ENDIAN
+# define NATIVE_ELFDATA ELFDATA2LSB
+#else
+# error BYTE_ORDER unknown
+#endif
+
+#define elf_32bit(elf) (ELFCLASS32 == (elf)->class)
+#define elf_64bit(elf) (ELFCLASS64 == (elf)->class)
+#define elf_msb(elf) (ELFDATA2MSB == (elf)->data)
+#define elf_lsb(elf) (ELFDATA2LSB == (elf)->data)
+#define elf_swap(elf) (NATIVE_ELFDATA != (elf)->data)
+
+#define elf_uval(elf, str, elem) \
+ ((ELFCLASS64 == (elf)->class) \
+ ? elf_access_unsigned((elf), (str), \
+ offsetof(typeof(*(str)),e64.elem), \
+ sizeof((str)->e64.elem)) \
+ : elf_access_unsigned((elf), (str), \
+ offsetof(typeof(*(str)),e32.elem), \
+ sizeof((str)->e32.elem)))
+
+#define elf_sval(elf, str, elem) \
+ ((ELFCLASS64 == (elf)->class) \
+ ? elf_access_signed((elf), (str), \
+ offsetof(typeof(*(str)),e64.elem), \
+ sizeof((str)->e64.elem)) \
+ : elf_access_signed((elf), (str), \
+ offsetof(typeof(*(str)),e32.elem), \
+ sizeof((str)->e32.elem)))
+
+#define elf_size(elf, str) \
+ ((ELFCLASS64 == (elf)->class) \
+ ? sizeof((str)->e64) \
+ : sizeof((str)->e32))
+
+uint64_t elf_access_unsigned(struct elf_binary *elf, const void *ptr,
+ off_t offset, size_t size);
+int64_t elf_access_signed(struct elf_binary *elf, const void *ptr,
+ off_t offset, size_t size);
+
+uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr);
+
+/* ------------------------------------------------------------------------ */
+/* xc_libelf_tools.c */
+
+int elf_shdr_count(struct elf_binary *elf);
+int elf_phdr_count(struct elf_binary *elf);
+
+const elf_shdr *elf_shdr_by_name(struct elf_binary *elf, const char *name);
+const elf_shdr *elf_shdr_by_index(struct elf_binary *elf, int index);
+const elf_phdr *elf_phdr_by_index(struct elf_binary *elf, int index);
+
+const char *elf_section_name(struct elf_binary *elf, const elf_shdr * shdr);
+const void *elf_section_start(struct elf_binary *elf, const elf_shdr * shdr);
+const void *elf_section_end(struct elf_binary *elf, const elf_shdr * shdr);
+
+const elf_sym *elf_sym_by_name(struct elf_binary *elf, const char *symbol);
+const elf_sym *elf_sym_by_index(struct elf_binary *elf, int index);
+
+const char *elf_note_name(struct elf_binary *elf, const elf_note * note);
+const void *elf_note_desc(struct elf_binary *elf, const elf_note * note);
+uint64_t elf_note_numeric(struct elf_binary *elf, const elf_note * note);
+const elf_note *elf_note_next(struct elf_binary *elf, const elf_note * note);
+
+int elf_is_elfbinary(const void *image);
+int elf_phdr_is_loadable(struct elf_binary *elf, const elf_phdr * phdr);
+
+/* ------------------------------------------------------------------------ */
+/* xc_libelf_loader.c */
+
+int elf_init(struct elf_binary *elf, const char *image, size_t size);
+#ifdef __XEN__
+void elf_set_verbose(struct elf_binary *elf);
+#else
+void elf_set_logfile(struct elf_binary *elf, FILE * log, int verbose);
+#endif
+
+void elf_parse_binary(struct elf_binary *elf);
+void elf_load_binary(struct elf_binary *elf);
+
+void *elf_get_ptr(struct elf_binary *elf, unsigned long addr);
+uint64_t elf_lookup_addr(struct elf_binary *elf, const char *symbol);
+
+/* ------------------------------------------------------------------------ */
+/* xc_libelf_relocate.c */
+
+int elf_reloc(struct elf_binary *elf);
+
+/* ------------------------------------------------------------------------ */
+/* xc_libelf_dominfo.c */
+
+#define UNSET_ADDR ((uint64_t)-1)
+
+struct elf_dom_parms {
+ /* raw */
+ const char *guest_info;
+ const void *elf_note_start;
+ const void *elf_note_end;
+
+ /* parsed */
+ char guest_os[16];
+ char guest_ver[16];
+ char xen_ver[16];
+ char loader[16];
+ int pae;
+ int bsd_symtab;
+ uint64_t virt_base;
+ uint64_t virt_entry;
+ uint64_t virt_hypercall;
+ uint64_t virt_hv_start_low;
+ uint64_t elf_paddr_offset;
+ uint32_t f_supported[XENFEAT_NR_SUBMAPS];
+ uint32_t f_required[XENFEAT_NR_SUBMAPS];
+
+ /* calculated */
+ uint64_t virt_offset;
+ uint64_t virt_kstart;
+ uint64_t virt_kend;
+};
+
+static inline void elf_xen_feature_set(int nr, uint32_t * addr)
+{
+ addr[nr >> 5] |= 1 << (nr & 31);
+}
+static inline int elf_xen_feature_get(int nr, uint32_t * addr)
+{
+ return !!(addr[nr >> 5] & (1 << (nr & 31)));
+}
+
+int elf_xen_parse_features(const char *features,
+ uint32_t *supported,
+ uint32_t *required);
+int elf_xen_parse_note(struct elf_binary *elf,
+ struct elf_dom_parms *parms,
+ const elf_note *note);
+int elf_xen_parse_guest_info(struct elf_binary *elf,
+ struct elf_dom_parms *parms);
+int elf_xen_parse(struct elf_binary *elf,
+ struct elf_dom_parms *parms);
+
+#endif /* __XC_LIBELF__ */
diff -r b111908dd70b -r db3d03dfe92f xen/include/xen/elf.h
--- a/xen/include/xen/elf.h Thu Jan 25 18:54:25 2007 +0000
+++ b/xen/include/xen/elf.h Thu Jan 25 22:16:52 2007 +0000
@@ -27,495 +27,7 @@
#ifndef __XEN_ELF_H__
#define __XEN_ELF_H__
-typedef u8 Elf_Byte;
-
-typedef u32 Elf32_Addr; /* Unsigned program address */
-typedef u32 Elf32_Off; /* Unsigned file offset */
-typedef s32 Elf32_Sword; /* Signed large integer */
-typedef u32 Elf32_Word; /* Unsigned large integer */
-typedef u16 Elf32_Half; /* Unsigned medium integer */
-
-typedef u64 Elf64_Addr;
-typedef u64 Elf64_Off;
-typedef s32 Elf64_Shalf;
-
-typedef s32 Elf64_Sword;
-typedef u32 Elf64_Word;
-
-typedef s64 Elf64_Sxword;
-typedef u64 Elf64_Xword;
-
-typedef u32 Elf64_Half;
-typedef u16 Elf64_Quarter;
-
-/*
- * e_ident[] identification indexes
- * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html
- */
-#define EI_MAG0 0 /* file ID */
-#define EI_MAG1 1 /* file ID */
-#define EI_MAG2 2 /* file ID */
-#define EI_MAG3 3 /* file ID */
-#define EI_CLASS 4 /* file class */
-#define EI_DATA 5 /* data encoding */
-#define EI_VERSION 6 /* ELF header version */
-#define EI_OSABI 7 /* OS/ABI ID */
-#define EI_ABIVERSION 8 /* ABI version */
-#define EI_PAD 9 /* start of pad bytes */
-#define EI_NIDENT 16 /* Size of e_ident[] */
-
-/* e_ident[] magic number */
-#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */
-#define ELFMAG1 'E' /* e_ident[EI_MAG1] */
-#define ELFMAG2 'L' /* e_ident[EI_MAG2] */
-#define ELFMAG3 'F' /* e_ident[EI_MAG3] */
-#define ELFMAG "\177ELF" /* magic */
-#define SELFMAG 4 /* size of magic */
-
-/* e_ident[] file class */
-#define ELFCLASSNONE 0 /* invalid */
-#define ELFCLASS32 1 /* 32-bit objs */
-#define ELFCLASS64 2 /* 64-bit objs */
-#define ELFCLASSNUM 3 /* number of classes */
-
-/* e_ident[] data encoding */
-#define ELFDATANONE 0 /* invalid */
-#define ELFDATA2LSB 1 /* Little-Endian */
-#define ELFDATA2MSB 2 /* Big-Endian */
-#define ELFDATANUM 3 /* number of data encode defines */
-
-/* e_ident[] Operating System/ABI */
-#define ELFOSABI_SYSV 0 /* UNIX System V ABI */
-#define ELFOSABI_HPUX 1 /* HP-UX operating system */
-#define ELFOSABI_NETBSD 2 /* NetBSD */
-#define ELFOSABI_LINUX 3 /* GNU/Linux */
-#define ELFOSABI_HURD 4 /* GNU/Hurd */
-#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */
-#define ELFOSABI_SOLARIS 6 /* Solaris */
-#define ELFOSABI_MONTEREY 7 /* Monterey */
-#define ELFOSABI_IRIX 8 /* IRIX */
-#define ELFOSABI_FREEBSD 9 /* FreeBSD */
-#define ELFOSABI_TRU64 10 /* TRU64 UNIX */
-#define ELFOSABI_MODESTO 11 /* Novell Modesto */
-#define ELFOSABI_OPENBSD 12 /* OpenBSD */
-#define ELFOSABI_ARM 97 /* ARM */
-#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
-
-/* e_ident */
-#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
- (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
- (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
- (ehdr).e_ident[EI_MAG3] == ELFMAG3)
-
-/* ELF Header */
-typedef struct elfhdr {
- unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
- Elf32_Half e_type; /* object file type */
- Elf32_Half e_machine; /* machine */
- Elf32_Word e_version; /* object file version */
- Elf32_Addr e_entry; /* virtual entry point */
- Elf32_Off e_phoff; /* program header table offset */
- Elf32_Off e_shoff; /* section header table offset */
- Elf32_Word e_flags; /* processor-specific flags */
- Elf32_Half e_ehsize; /* ELF header size */
- Elf32_Half e_phentsize; /* program header entry size */
- Elf32_Half e_phnum; /* number of program header entries */
- Elf32_Half e_shentsize; /* section header entry size */
- Elf32_Half e_shnum; /* number of section header entries */
- Elf32_Half e_shstrndx; /* section header table's "section
- header string table" entry offset */
-} Elf32_Ehdr;
-
-typedef struct {
- unsigned char e_ident[EI_NIDENT]; /* Id bytes */
- Elf64_Quarter e_type; /* file type */
- Elf64_Quarter e_machine; /* machine type */
- Elf64_Half e_version; /* version number */
- Elf64_Addr e_entry; /* entry point */
- Elf64_Off e_phoff; /* Program hdr offset */
- Elf64_Off e_shoff; /* Section hdr offset */
- Elf64_Half e_flags; /* Processor flags */
- Elf64_Quarter e_ehsize; /* sizeof ehdr */
- Elf64_Quarter e_phentsize; /* Program header entry size */
- Elf64_Quarter e_phnum; /* Number of program headers */
- Elf64_Quarter e_shentsize; /* Section header entry size */
- Elf64_Quarter e_shnum; /* Number of section headers */
- Elf64_Quarter e_shstrndx; /* String table index */
-} Elf64_Ehdr;
-
-/* e_type */
-#define ET_NONE 0 /* No file type */
-#define ET_REL 1 /* relocatable file */
-#define ET_EXEC 2 /* executable file */
-#define ET_DYN 3 /* shared object file */
-#define ET_CORE 4 /* core file */
-#define ET_NUM 5 /* number of types */
-#define ET_LOPROC 0xff00 /* reserved range for processor */
-#define ET_HIPROC 0xffff /* specific e_type */
-
-/* e_machine */
-#define EM_NONE 0 /* No Machine */
-#define EM_M32 1 /* AT&T WE 32100 */
-#define EM_SPARC 2 /* SPARC */
-#define EM_386 3 /* Intel 80386 */
-#define EM_68K 4 /* Motorola 68000 */
-#define EM_88K 5 /* Motorola 88000 */
-#define EM_486 6 /* Intel 80486 - unused? */
-#define EM_860 7 /* Intel 80860 */
-#define EM_MIPS 8 /* MIPS R3000 Big-Endian only */
-/*
- * Don't know if EM_MIPS_RS4_BE,
- * EM_SPARC64, EM_PARISC,
- * or EM_PPC are ABI compliant
- */
-#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */
-#define EM_SPARC64 11 /* SPARC v9 64-bit unoffical */
-#define EM_PARISC 15 /* HPPA */
-#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */
-#define EM_PPC 20 /* PowerPC */
-#define EM_PPC64 21 /* PowerPC 64-bit */
-#define EM_ARM 40 /* Advanced RISC Machines ARM */
-#define EM_ALPHA 41 /* DEC ALPHA */
-#define EM_SPARCV9 43 /* SPARC version 9 */
-#define EM_ALPHA_EXP 0x9026 /* DEC ALPHA */
-#define EM_IA_64 50 /* Intel Merced */
-#define EM_X86_64 62 /* AMD x86-64 architecture */
-#define EM_VAX 75 /* DEC VAX */
-
-/* Version */
-#define EV_NONE 0 /* Invalid */
-#define EV_CURRENT 1 /* Current */
-#define EV_NUM 2 /* number of versions */
-
-/* Section Header */
-typedef struct {
- Elf32_Word sh_name; /* name - index into section header
- string table section */
- Elf32_Word sh_type; /* type */
- Elf32_Word sh_flags; /* flags */
- Elf32_Addr sh_addr; /* address */
- Elf32_Off sh_offset; /* file offset */
- Elf32_Word sh_size; /* section size */
- Elf32_Word sh_link; /* section header table index link */
- Elf32_Word sh_info; /* extra information */
- Elf32_Word sh_addralign; /* address alignment */
- Elf32_Word sh_entsize; /* section entry size */
-} Elf32_Shdr;
-
-typedef struct {
- Elf64_Half sh_name; /* section name */
- Elf64_Half sh_type; /* section type */
- Elf64_Xword sh_flags; /* section flags */
- Elf64_Addr sh_addr; /* virtual address */
- Elf64_Off sh_offset; /* file offset */
- Elf64_Xword sh_size; /* section size */
- Elf64_Half sh_link; /* link to another */
- Elf64_Half sh_info; /* misc info */
- Elf64_Xword sh_addralign; /* memory alignment */
- Elf64_Xword sh_entsize; /* table entry size */
-} Elf64_Shdr;
-
-/* Special Section Indexes */
-#define SHN_UNDEF 0 /* undefined */
-#define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */
-#define SHN_LOPROC 0xff00 /* reserved range for processor */
-#define SHN_HIPROC 0xff1f /* specific section indexes */
-#define SHN_ABS 0xfff1 /* absolute value */
-#define SHN_COMMON 0xfff2 /* common symbol */
-#define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */
-
-/* sh_type */
-#define SHT_NULL 0 /* inactive */
-#define SHT_PROGBITS 1 /* program defined information */
-#define SHT_SYMTAB 2 /* symbol table section */
-#define SHT_STRTAB 3 /* string table section */
-#define SHT_RELA 4 /* relocation section with addends*/
-#define SHT_HASH 5 /* symbol hash table section */
-#define SHT_DYNAMIC 6 /* dynamic section */
-#define SHT_NOTE 7 /* note section */
-#define SHT_NOBITS 8 /* no space section */
-#define SHT_REL 9 /* relation section without
addends */
-#define SHT_SHLIB 10 /* reserved - purpose unknown */
-#define SHT_DYNSYM 11 /* dynamic symbol table section */
-#define SHT_NUM 12 /* number of section types */
-#define SHT_LOPROC 0x70000000 /* reserved range for processor */
-#define SHT_HIPROC 0x7fffffff /* specific section header types */
-#define SHT_LOUSER 0x80000000 /* reserved range for application */
-#define SHT_HIUSER 0xffffffff /* specific indexes */
-
-/* Section names */
-#define ELF_BSS ".bss" /* uninitialized data */
-#define ELF_DATA ".data" /* initialized data */
-#define ELF_DEBUG ".debug" /* debug */
-#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */
-#define ELF_DYNSTR ".dynstr" /* dynamic string table */
-#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */
-#define ELF_FINI ".fini" /* termination code */
-#define ELF_GOT ".got" /* global offset table */
-#define ELF_HASH ".hash" /* symbol hash table */
-#define ELF_INIT ".init" /* initialization code */
-#define ELF_REL_DATA ".rel.data" /* relocation data */
-#define ELF_REL_FINI ".rel.fini" /* relocation termination code */
-#define ELF_REL_INIT ".rel.init" /* relocation initialization code */
-#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */
-#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */
-#define ELF_REL_TEXT ".rel.text" /* relocation code */
-#define ELF_RODATA ".rodata" /* read-only data */
-#define ELF_SHSTRTAB ".shstrtab" /* section header string table */
-#define ELF_STRTAB ".strtab" /* string table */
-#define ELF_SYMTAB ".symtab" /* symbol table */
-#define ELF_TEXT ".text" /* code */
-
-
-/* Section Attribute Flags - sh_flags */
-#define SHF_WRITE 0x1 /* Writable */
-#define SHF_ALLOC 0x2 /* occupies memory */
-#define SHF_EXECINSTR 0x4 /* executable */
-#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
- /* specific section attributes */
-
-/* Symbol Table Entry */
-typedef struct elf32_sym {
- Elf32_Word st_name; /* name - index into string table */
- Elf32_Addr st_value; /* symbol value */
- Elf32_Word st_size; /* symbol size */
- unsigned char st_info; /* type and binding */
- unsigned char st_other; /* 0 - no defined meaning */
- Elf32_Half st_shndx; /* section header index */
-} Elf32_Sym;
-
-typedef struct {
- Elf64_Half st_name; /* Symbol name index in str table */
- Elf_Byte st_info; /* type / binding attrs */
- Elf_Byte st_other; /* unused */
- Elf64_Quarter st_shndx; /* section index of symbol */
- Elf64_Xword st_value; /* value of symbol */
- Elf64_Xword st_size; /* size of symbol */
-} Elf64_Sym;
-
-/* Symbol table index */
-#define STN_UNDEF 0 /* undefined */
-
-/* Extract symbol info - st_info */
-#define ELF32_ST_BIND(x) ((x) >> 4)
-#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf)
-#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf))
-
-#define ELF64_ST_BIND(x) ((x) >> 4)
-#define ELF64_ST_TYPE(x) (((unsigned int) x) & 0xf)
-#define ELF64_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf))
-
-/* Symbol Binding - ELF32_ST_BIND - st_info */
-#define STB_LOCAL 0 /* Local symbol */
-#define STB_GLOBAL 1 /* Global symbol */
-#define STB_WEAK 2 /* like global - lower precedence */
-#define STB_NUM 3 /* number of symbol bindings */
-#define STB_LOPROC 13 /* reserved range for processor */
-#define STB_HIPROC 15 /* specific symbol bindings */
-
-/* Symbol type - ELF32_ST_TYPE - st_info */
-#define STT_NOTYPE 0 /* not specified */
-#define STT_OBJECT 1 /* data object */
-#define STT_FUNC 2 /* function */
-#define STT_SECTION 3 /* section */
-#define STT_FILE 4 /* file */
-#define STT_NUM 5 /* number of symbol types */
-#define STT_LOPROC 13 /* reserved range for processor */
-#define STT_HIPROC 15 /* specific symbol types */
-
-/* Relocation entry with implicit addend */
-typedef struct {
- Elf32_Addr r_offset; /* offset of relocation */
- Elf32_Word r_info; /* symbol table index and type */
-} Elf32_Rel;
-
-/* Relocation entry with explicit addend */
-typedef struct {
- Elf32_Addr r_offset; /* offset of relocation */
- Elf32_Word r_info; /* symbol table index and type */
- Elf32_Sword r_addend;
-} Elf32_Rela;
-
-/* Extract relocation info - r_info */
-#define ELF32_R_SYM(i) ((i) >> 8)
-#define ELF32_R_TYPE(i) ((unsigned char) (i))
-#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
-
-typedef struct {
- Elf64_Xword r_offset; /* where to do it */
- Elf64_Xword r_info; /* index & type of relocation */
-} Elf64_Rel;
-
-typedef struct {
- Elf64_Xword r_offset; /* where to do it */
- Elf64_Xword r_info; /* index & type of relocation */
- Elf64_Sxword r_addend; /* adjustment value */
-} Elf64_Rela;
-
-#define ELF64_R_SYM(info) ((info) >> 32)
-#define ELF64_R_TYPE(info) ((info) & 0xFFFFFFFF)
-#define ELF64_R_INFO(s,t) (((s) << 32) + (u32)(t))
-
-/* Program Header */
-typedef struct {
- Elf32_Word p_type; /* segment type */
- Elf32_Off p_offset; /* segment offset */
- Elf32_Addr p_vaddr; /* virtual address of segment */
- Elf32_Addr p_paddr; /* physical address - ignored? */
- Elf32_Word p_filesz; /* number of bytes in file for seg. */
- Elf32_Word p_memsz; /* number of bytes in mem. for seg. */
- Elf32_Word p_flags; /* flags */
- Elf32_Word p_align; /* memory alignment */
-} Elf32_Phdr;
-
-typedef struct {
- Elf64_Half p_type; /* entry type */
- Elf64_Half p_flags; /* flags */
- Elf64_Off p_offset; /* offset */
- Elf64_Addr p_vaddr; /* virtual address */
- Elf64_Addr p_paddr; /* physical address */
- Elf64_Xword p_filesz; /* file size */
- Elf64_Xword p_memsz; /* memory size */
- Elf64_Xword p_align; /* memory & file alignment */
-} Elf64_Phdr;
-
-/* Segment types - p_type */
-#define PT_NULL 0 /* unused */
-#define PT_LOAD 1 /* loadable segment */
-#define PT_DYNAMIC 2 /* dynamic linking section */
-#define PT_INTERP 3 /* the RTLD */
-#define PT_NOTE 4 /* auxiliary information */
-#define PT_SHLIB 5 /* reserved - purpose undefined */
-#define PT_PHDR 6 /* program header */
-#define PT_NUM 7 /* Number of segment types */
-#define PT_LOPROC 0x70000000 /* reserved range for processor */
-#define PT_HIPROC 0x7fffffff /* specific segment types */
-
-/* Segment flags - p_flags */
-#define PF_X 0x1 /* Executable */
-#define PF_W 0x2 /* Writable */
-#define PF_R 0x4 /* Readable */
-#define PF_MASKPROC 0xf0000000 /* reserved bits for processor */
- /* specific segment flags */
-
-/* Dynamic structure */
-typedef struct {
- Elf32_Sword d_tag; /* controls meaning of d_val */
- union {
- Elf32_Word d_val; /* Multiple meanings - see d_tag */
- Elf32_Addr d_ptr; /* program virtual address */
- } d_un;
-} Elf32_Dyn;
-
-typedef struct {
- Elf64_Xword d_tag; /* controls meaning of d_val */
- union {
- Elf64_Addr d_ptr;
- Elf64_Xword d_val;
- } d_un;
-} Elf64_Dyn;
-
-/* Dynamic Array Tags - d_tag */
-#define DT_NULL 0 /* marks end of _DYNAMIC array
*/
-#define DT_NEEDED 1 /* string table offset of needed lib */
-#define DT_PLTRELSZ 2 /* size of relocation entries in PLT */
-#define DT_PLTGOT 3 /* address PLT/GOT */
-#define DT_HASH 4 /* address of symbol hash table
*/
-#define DT_STRTAB 5 /* address of string table */
-#define DT_SYMTAB 6 /* address of symbol table */
-#define DT_RELA 7 /* address of relocation table
*/
-#define DT_RELASZ 8 /* size of relocation table */
-#define DT_RELAENT 9 /* size of relocation entry */
-#define DT_STRSZ 10 /* size of string table */
-#define DT_SYMENT 11 /* size of symbol table entry */
-#define DT_INIT 12 /* address of initialization
func. */
-#define DT_FINI 13 /* address of termination
function */
-#define DT_SONAME 14 /* string table offset of shared obj */
-#define DT_RPATH 15 /* string table offset of library
- search path */
-#define DT_SYMBOLIC 16 /* start sym search in shared obj. */
-#define DT_REL 17 /* address of rel. tbl. w addends */
-#define DT_RELSZ 18 /* size of DT_REL relocation table */
-#define DT_RELENT 19 /* size of DT_REL relocation entry */
-#define DT_PLTREL 20 /* PLT referenced relocation entry */
-#define DT_DEBUG 21 /* bugger */
-#define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */
-#define DT_JMPREL 23 /* add. of PLT's relocation entries */
-#define DT_BIND_NOW 24 /* Bind now regardless of env setting */
-#define DT_NUM 25 /* Number used. */
-#define DT_LOPROC 0x70000000 /* reserved range for processor */
-#define DT_HIPROC 0x7fffffff /* specific dynamic array tags */
-
-/* Standard ELF hashing function */
-unsigned int elf_hash(const unsigned char *name);
-
-/*
- * Note Definitions
- */
-
-typedef struct {
- u32 namesz;
- u32 descsz;
- u32 type;
-} Elf_Note; /* same format for both 32-bit and 64-bit ELF */
-
-#if defined(ELFSIZE)
-#define CONCAT(x,y) __CONCAT(x,y)
-#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
-#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
-#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
-#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
-#endif
-
-#if defined(ELFSIZE) && (ELFSIZE == 32)
-#define Elf_Ehdr Elf32_Ehdr
-#define Elf_Phdr Elf32_Phdr
-#define Elf_Shdr Elf32_Shdr
-#define Elf_Sym Elf32_Sym
-#define Elf_Rel Elf32_Rel
-#define Elf_RelA Elf32_Rela
-#define Elf_Dyn Elf32_Dyn
-#define Elf_Word Elf32_Word
-#define Elf_Sword Elf32_Sword
-#define Elf_Addr Elf32_Addr
-#define Elf_Off Elf32_Off
-#define Elf_Nhdr Elf32_Nhdr
-
-#define ELF_R_SYM ELF32_R_SYM
-#define ELF_R_TYPE ELF32_R_TYPE
-#define ELF_R_INFO ELF32_R_INFO
-#define ELFCLASS ELFCLASS32
-
-#define ELF_ST_BIND ELF32_ST_BIND
-#define ELF_ST_TYPE ELF32_ST_TYPE
-#define ELF_ST_INFO ELF32_ST_INFO
-
-#define AuxInfo Aux32Info
-#elif defined(ELFSIZE) && (ELFSIZE == 64)
-#define Elf_Ehdr Elf64_Ehdr
-#define Elf_Phdr Elf64_Phdr
-#define Elf_Shdr Elf64_Shdr
-#define Elf_Sym Elf64_Sym
-#define Elf_Rel Elf64_Rel
-#define Elf_RelA Elf64_Rela
-#define Elf_Dyn Elf64_Dyn
-#define Elf_Word Elf64_Word
-#define Elf_Sword Elf64_Sword
-#define Elf_Addr Elf64_Addr
-#define Elf_Off Elf64_Off
-#define Elf_Nhdr Elf64_Nhdr
-
-#define ELF_R_SYM ELF64_R_SYM
-#define ELF_R_TYPE ELF64_R_TYPE
-#define ELF_R_INFO ELF64_R_INFO
-#define ELFCLASS ELFCLASS64
-
-#define ELF_ST_BIND ELF64_ST_BIND
-#define ELF_ST_TYPE ELF64_ST_TYPE
-#define ELF_ST_INFO ELF64_ST_INFO
-
-#define AuxInfo Aux64Info
-#endif
+#include <public/elfstructs.h>
#define ELFNOTE_ALIGN(_n_) (((_n_)+3)&~3)
#define ELFNOTE_NAME(_n_) ((char*)(_n_) + sizeof(*(_n_)))
_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog
|