debuggers.hg
changeset 13655:db3d03dfe92f
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@suse.de>
---
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(-)
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@suse.de>
---
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(-)
author | Emmanuel Ackaouy <ack@xensource.com> |
---|---|
date | Thu Jan 25 22:16:52 2007 +0000 (2007-01-25) |
parents | b111908dd70b |
children | 3c9926aadec5 |
files | tools/libxc/xc_elf.h xen/arch/x86/boot/mkelf32.c xen/common/Makefile xen/common/libelf/Makefile xen/common/libelf/README xen/common/libelf/libelf-dominfo.c xen/common/libelf/libelf-loader.c xen/common/libelf/libelf-private.h xen/common/libelf/libelf-relocate.c xen/common/libelf/libelf-tools.c xen/include/public/elfstructs.h xen/include/public/libelf.h xen/include/xen/elf.h |
line diff
1.1 --- a/tools/libxc/xc_elf.h Thu Jan 25 18:54:25 2007 +0000 1.2 +++ b/tools/libxc/xc_elf.h Thu Jan 25 22:16:52 2007 +0000 1.3 @@ -1,524 +1,1 @@ 1.4 -/* 1.5 - * Copyright (c) 1995, 1996 Erik Theisen. All rights reserved. 1.6 - * 1.7 - * Redistribution and use in source and binary forms, with or without 1.8 - * modification, are permitted provided that the following conditions 1.9 - * are met: 1.10 - * 1. Redistributions of source code must retain the above copyright 1.11 - * notice, this list of conditions and the following disclaimer. 1.12 - * 2. Redistributions in binary form must reproduce the above copyright 1.13 - * notice, this list of conditions and the following disclaimer in the 1.14 - * documentation and/or other materials provided with the distribution. 1.15 - * 3. The name of the author may not be used to endorse or promote products 1.16 - * derived from this software without specific prior written permission 1.17 - * 1.18 - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1.19 - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1.20 - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1.21 - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1.22 - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1.23 - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.24 - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.25 - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.26 - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.27 - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.28 - */ 1.29 - 1.30 -typedef uint8_t Elf_Byte; 1.31 - 1.32 -typedef uint32_t Elf32_Addr; /* Unsigned program address */ 1.33 -typedef uint32_t Elf32_Off; /* Unsigned file offset */ 1.34 -typedef int32_t Elf32_Sword; /* Signed large integer */ 1.35 -typedef uint32_t Elf32_Word; /* Unsigned large integer */ 1.36 -typedef uint16_t Elf32_Half; /* Unsigned medium integer */ 1.37 - 1.38 -typedef uint64_t Elf64_Addr; 1.39 -typedef uint64_t Elf64_Off; 1.40 -typedef int32_t Elf64_Shalf; 1.41 - 1.42 -typedef int32_t Elf64_Sword; 1.43 -typedef uint32_t Elf64_Word; 1.44 - 1.45 -typedef int64_t Elf64_Sxword; 1.46 -typedef uint64_t Elf64_Xword; 1.47 - 1.48 -typedef uint32_t Elf64_Half; 1.49 -typedef uint16_t Elf64_Quarter; 1.50 - 1.51 -/* 1.52 - * e_ident[] identification indexes 1.53 - * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html 1.54 - */ 1.55 -#define EI_MAG0 0 /* file ID */ 1.56 -#define EI_MAG1 1 /* file ID */ 1.57 -#define EI_MAG2 2 /* file ID */ 1.58 -#define EI_MAG3 3 /* file ID */ 1.59 -#define EI_CLASS 4 /* file class */ 1.60 -#define EI_DATA 5 /* data encoding */ 1.61 -#define EI_VERSION 6 /* ELF header version */ 1.62 -#define EI_OSABI 7 /* OS/ABI ID */ 1.63 -#define EI_ABIVERSION 8 /* ABI version */ 1.64 -#define EI_PAD 9 /* start of pad bytes */ 1.65 -#define EI_NIDENT 16 /* Size of e_ident[] */ 1.66 - 1.67 -/* e_ident[] magic number */ 1.68 -#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */ 1.69 -#define ELFMAG1 'E' /* e_ident[EI_MAG1] */ 1.70 -#define ELFMAG2 'L' /* e_ident[EI_MAG2] */ 1.71 -#define ELFMAG3 'F' /* e_ident[EI_MAG3] */ 1.72 -#define ELFMAG "\177ELF" /* magic */ 1.73 -#define SELFMAG 4 /* size of magic */ 1.74 - 1.75 -/* e_ident[] file class */ 1.76 -#define ELFCLASSNONE 0 /* invalid */ 1.77 -#define ELFCLASS32 1 /* 32-bit objs */ 1.78 -#define ELFCLASS64 2 /* 64-bit objs */ 1.79 -#define ELFCLASSNUM 3 /* number of classes */ 1.80 - 1.81 -/* e_ident[] data encoding */ 1.82 -#define ELFDATANONE 0 /* invalid */ 1.83 -#define ELFDATA2LSB 1 /* Little-Endian */ 1.84 -#define ELFDATA2MSB 2 /* Big-Endian */ 1.85 -#define ELFDATANUM 3 /* number of data encode defines */ 1.86 - 1.87 -/* e_ident[] Operating System/ABI */ 1.88 -#define ELFOSABI_SYSV 0 /* UNIX System V ABI */ 1.89 -#define ELFOSABI_HPUX 1 /* HP-UX operating system */ 1.90 -#define ELFOSABI_NETBSD 2 /* NetBSD */ 1.91 -#define ELFOSABI_LINUX 3 /* GNU/Linux */ 1.92 -#define ELFOSABI_HURD 4 /* GNU/Hurd */ 1.93 -#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */ 1.94 -#define ELFOSABI_SOLARIS 6 /* Solaris */ 1.95 -#define ELFOSABI_MONTEREY 7 /* Monterey */ 1.96 -#define ELFOSABI_IRIX 8 /* IRIX */ 1.97 -#define ELFOSABI_FREEBSD 9 /* FreeBSD */ 1.98 -#define ELFOSABI_TRU64 10 /* TRU64 UNIX */ 1.99 -#define ELFOSABI_MODESTO 11 /* Novell Modesto */ 1.100 -#define ELFOSABI_OPENBSD 12 /* OpenBSD */ 1.101 -#define ELFOSABI_ARM 97 /* ARM */ 1.102 -#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ 1.103 - 1.104 -/* e_ident */ 1.105 -#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \ 1.106 - (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \ 1.107 - (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \ 1.108 - (ehdr).e_ident[EI_MAG3] == ELFMAG3) 1.109 - 1.110 -/* ELF Header */ 1.111 -typedef struct elfhdr { 1.112 - unsigned char e_ident[EI_NIDENT]; /* ELF Identification */ 1.113 - Elf32_Half e_type; /* object file type */ 1.114 - Elf32_Half e_machine; /* machine */ 1.115 - Elf32_Word e_version; /* object file version */ 1.116 - Elf32_Addr e_entry; /* virtual entry point */ 1.117 - Elf32_Off e_phoff; /* program header table offset */ 1.118 - Elf32_Off e_shoff; /* section header table offset */ 1.119 - Elf32_Word e_flags; /* processor-specific flags */ 1.120 - Elf32_Half e_ehsize; /* ELF header size */ 1.121 - Elf32_Half e_phentsize; /* program header entry size */ 1.122 - Elf32_Half e_phnum; /* number of program header entries */ 1.123 - Elf32_Half e_shentsize; /* section header entry size */ 1.124 - Elf32_Half e_shnum; /* number of section header entries */ 1.125 - Elf32_Half e_shstrndx; /* section header table's "section 1.126 - header string table" entry offset */ 1.127 -} Elf32_Ehdr; 1.128 - 1.129 -typedef struct { 1.130 - unsigned char e_ident[EI_NIDENT]; /* Id bytes */ 1.131 - Elf64_Quarter e_type; /* file type */ 1.132 - Elf64_Quarter e_machine; /* machine type */ 1.133 - Elf64_Half e_version; /* version number */ 1.134 - Elf64_Addr e_entry; /* entry point */ 1.135 - Elf64_Off e_phoff; /* Program hdr offset */ 1.136 - Elf64_Off e_shoff; /* Section hdr offset */ 1.137 - Elf64_Half e_flags; /* Processor flags */ 1.138 - Elf64_Quarter e_ehsize; /* sizeof ehdr */ 1.139 - Elf64_Quarter e_phentsize; /* Program header entry size */ 1.140 - Elf64_Quarter e_phnum; /* Number of program headers */ 1.141 - Elf64_Quarter e_shentsize; /* Section header entry size */ 1.142 - Elf64_Quarter e_shnum; /* Number of section headers */ 1.143 - Elf64_Quarter e_shstrndx; /* String table index */ 1.144 -} Elf64_Ehdr; 1.145 - 1.146 -/* e_type */ 1.147 -#define ET_NONE 0 /* No file type */ 1.148 -#define ET_REL 1 /* relocatable file */ 1.149 -#define ET_EXEC 2 /* executable file */ 1.150 -#define ET_DYN 3 /* shared object file */ 1.151 -#define ET_CORE 4 /* core file */ 1.152 -#define ET_NUM 5 /* number of types */ 1.153 -#define ET_LOPROC 0xff00 /* reserved range for processor */ 1.154 -#define ET_HIPROC 0xffff /* specific e_type */ 1.155 - 1.156 -/* e_machine */ 1.157 -#define EM_NONE 0 /* No Machine */ 1.158 -#define EM_M32 1 /* AT&T WE 32100 */ 1.159 -#define EM_SPARC 2 /* SPARC */ 1.160 -#define EM_386 3 /* Intel 80386 */ 1.161 -#define EM_68K 4 /* Motorola 68000 */ 1.162 -#define EM_88K 5 /* Motorola 88000 */ 1.163 -#define EM_486 6 /* Intel 80486 - unused? */ 1.164 -#define EM_860 7 /* Intel 80860 */ 1.165 -#define EM_MIPS 8 /* MIPS R3000 Big-Endian only */ 1.166 -/* 1.167 - * Don't know if EM_MIPS_RS4_BE, 1.168 - * EM_SPARC64, EM_PARISC, 1.169 - * or EM_PPC are ABI compliant 1.170 - */ 1.171 -#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */ 1.172 -#define EM_SPARC64 11 /* SPARC v9 64-bit unoffical */ 1.173 -#define EM_PARISC 15 /* HPPA */ 1.174 -#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */ 1.175 -#define EM_PPC 20 /* PowerPC */ 1.176 -#define EM_PPC64 21 /* PowerPC 64-bit */ 1.177 -#define EM_ARM 40 /* Advanced RISC Machines ARM */ 1.178 -#define EM_ALPHA 41 /* DEC ALPHA */ 1.179 -#define EM_SPARCV9 43 /* SPARC version 9 */ 1.180 -#define EM_ALPHA_EXP 0x9026 /* DEC ALPHA */ 1.181 -#define EM_IA_64 50 /* Intel Merced */ 1.182 -#define EM_X86_64 62 /* AMD x86-64 architecture */ 1.183 -#define EM_VAX 75 /* DEC VAX */ 1.184 - 1.185 -/* Version */ 1.186 -#define EV_NONE 0 /* Invalid */ 1.187 -#define EV_CURRENT 1 /* Current */ 1.188 -#define EV_NUM 2 /* number of versions */ 1.189 - 1.190 -/* Section Header */ 1.191 -typedef struct { 1.192 - Elf32_Word sh_name; /* name - index into section header 1.193 - string table section */ 1.194 - Elf32_Word sh_type; /* type */ 1.195 - Elf32_Word sh_flags; /* flags */ 1.196 - Elf32_Addr sh_addr; /* address */ 1.197 - Elf32_Off sh_offset; /* file offset */ 1.198 - Elf32_Word sh_size; /* section size */ 1.199 - Elf32_Word sh_link; /* section header table index link */ 1.200 - Elf32_Word sh_info; /* extra information */ 1.201 - Elf32_Word sh_addralign; /* address alignment */ 1.202 - Elf32_Word sh_entsize; /* section entry size */ 1.203 -} Elf32_Shdr; 1.204 - 1.205 -typedef struct { 1.206 - Elf64_Half sh_name; /* section name */ 1.207 - Elf64_Half sh_type; /* section type */ 1.208 - Elf64_Xword sh_flags; /* section flags */ 1.209 - Elf64_Addr sh_addr; /* virtual address */ 1.210 - Elf64_Off sh_offset; /* file offset */ 1.211 - Elf64_Xword sh_size; /* section size */ 1.212 - Elf64_Half sh_link; /* link to another */ 1.213 - Elf64_Half sh_info; /* misc info */ 1.214 - Elf64_Xword sh_addralign; /* memory alignment */ 1.215 - Elf64_Xword sh_entsize; /* table entry size */ 1.216 -} Elf64_Shdr; 1.217 - 1.218 -/* Special Section Indexes */ 1.219 -#define SHN_UNDEF 0 /* undefined */ 1.220 -#define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */ 1.221 -#define SHN_LOPROC 0xff00 /* reserved range for processor */ 1.222 -#define SHN_HIPROC 0xff1f /* specific section indexes */ 1.223 -#define SHN_ABS 0xfff1 /* absolute value */ 1.224 -#define SHN_COMMON 0xfff2 /* common symbol */ 1.225 -#define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */ 1.226 - 1.227 -/* sh_type */ 1.228 -#define SHT_NULL 0 /* inactive */ 1.229 -#define SHT_PROGBITS 1 /* program defined information */ 1.230 -#define SHT_SYMTAB 2 /* symbol table section */ 1.231 -#define SHT_STRTAB 3 /* string table section */ 1.232 -#define SHT_RELA 4 /* relocation section with addends*/ 1.233 -#define SHT_HASH 5 /* symbol hash table section */ 1.234 -#define SHT_DYNAMIC 6 /* dynamic section */ 1.235 -#define SHT_NOTE 7 /* note section */ 1.236 -#define SHT_NOBITS 8 /* no space section */ 1.237 -#define SHT_REL 9 /* relation section without addends */ 1.238 -#define SHT_SHLIB 10 /* reserved - purpose unknown */ 1.239 -#define SHT_DYNSYM 11 /* dynamic symbol table section */ 1.240 -#define SHT_NUM 12 /* number of section types */ 1.241 -#define SHT_LOPROC 0x70000000 /* reserved range for processor */ 1.242 -#define SHT_HIPROC 0x7fffffff /* specific section header types */ 1.243 -#define SHT_LOUSER 0x80000000 /* reserved range for application */ 1.244 -#define SHT_HIUSER 0xffffffff /* specific indexes */ 1.245 - 1.246 -/* Section names */ 1.247 -#define ELF_BSS ".bss" /* uninitialized data */ 1.248 -#define ELF_DATA ".data" /* initialized data */ 1.249 -#define ELF_DEBUG ".debug" /* debug */ 1.250 -#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */ 1.251 -#define ELF_DYNSTR ".dynstr" /* dynamic string table */ 1.252 -#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */ 1.253 -#define ELF_FINI ".fini" /* termination code */ 1.254 -#define ELF_GOT ".got" /* global offset table */ 1.255 -#define ELF_HASH ".hash" /* symbol hash table */ 1.256 -#define ELF_INIT ".init" /* initialization code */ 1.257 -#define ELF_REL_DATA ".rel.data" /* relocation data */ 1.258 -#define ELF_REL_FINI ".rel.fini" /* relocation termination code */ 1.259 -#define ELF_REL_INIT ".rel.init" /* relocation initialization code */ 1.260 -#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */ 1.261 -#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */ 1.262 -#define ELF_REL_TEXT ".rel.text" /* relocation code */ 1.263 -#define ELF_RODATA ".rodata" /* read-only data */ 1.264 -#define ELF_SHSTRTAB ".shstrtab" /* section header string table */ 1.265 -#define ELF_STRTAB ".strtab" /* string table */ 1.266 -#define ELF_SYMTAB ".symtab" /* symbol table */ 1.267 -#define ELF_TEXT ".text" /* code */ 1.268 - 1.269 - 1.270 -/* Section Attribute Flags - sh_flags */ 1.271 -#define SHF_WRITE 0x1 /* Writable */ 1.272 -#define SHF_ALLOC 0x2 /* occupies memory */ 1.273 -#define SHF_EXECINSTR 0x4 /* executable */ 1.274 -#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */ 1.275 - /* specific section attributes */ 1.276 - 1.277 -/* Symbol Table Entry */ 1.278 -typedef struct elf32_sym { 1.279 - Elf32_Word st_name; /* name - index into string table */ 1.280 - Elf32_Addr st_value; /* symbol value */ 1.281 - Elf32_Word st_size; /* symbol size */ 1.282 - unsigned char st_info; /* type and binding */ 1.283 - unsigned char st_other; /* 0 - no defined meaning */ 1.284 - Elf32_Half st_shndx; /* section header index */ 1.285 -} Elf32_Sym; 1.286 - 1.287 -typedef struct { 1.288 - Elf64_Half st_name; /* Symbol name index in str table */ 1.289 - Elf_Byte st_info; /* type / binding attrs */ 1.290 - Elf_Byte st_other; /* unused */ 1.291 - Elf64_Quarter st_shndx; /* section index of symbol */ 1.292 - Elf64_Xword st_value; /* value of symbol */ 1.293 - Elf64_Xword st_size; /* size of symbol */ 1.294 -} Elf64_Sym; 1.295 - 1.296 -/* Symbol table index */ 1.297 -#define STN_UNDEF 0 /* undefined */ 1.298 - 1.299 -/* Extract symbol info - st_info */ 1.300 -#define ELF32_ST_BIND(x) ((x) >> 4) 1.301 -#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf) 1.302 -#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) 1.303 - 1.304 -#define ELF64_ST_BIND(x) ((x) >> 4) 1.305 -#define ELF64_ST_TYPE(x) (((unsigned int) x) & 0xf) 1.306 -#define ELF64_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) 1.307 - 1.308 -/* Symbol Binding - ELF32_ST_BIND - st_info */ 1.309 -#define STB_LOCAL 0 /* Local symbol */ 1.310 -#define STB_GLOBAL 1 /* Global symbol */ 1.311 -#define STB_WEAK 2 /* like global - lower precedence */ 1.312 -#define STB_NUM 3 /* number of symbol bindings */ 1.313 -#define STB_LOPROC 13 /* reserved range for processor */ 1.314 -#define STB_HIPROC 15 /* specific symbol bindings */ 1.315 - 1.316 -/* Symbol type - ELF32_ST_TYPE - st_info */ 1.317 -#define STT_NOTYPE 0 /* not specified */ 1.318 -#define STT_OBJECT 1 /* data object */ 1.319 -#define STT_FUNC 2 /* function */ 1.320 -#define STT_SECTION 3 /* section */ 1.321 -#define STT_FILE 4 /* file */ 1.322 -#define STT_NUM 5 /* number of symbol types */ 1.323 -#define STT_LOPROC 13 /* reserved range for processor */ 1.324 -#define STT_HIPROC 15 /* specific symbol types */ 1.325 - 1.326 -/* Relocation entry with implicit addend */ 1.327 -typedef struct { 1.328 - Elf32_Addr r_offset; /* offset of relocation */ 1.329 - Elf32_Word r_info; /* symbol table index and type */ 1.330 -} Elf32_Rel; 1.331 - 1.332 -/* Relocation entry with explicit addend */ 1.333 -typedef struct { 1.334 - Elf32_Addr r_offset; /* offset of relocation */ 1.335 - Elf32_Word r_info; /* symbol table index and type */ 1.336 - Elf32_Sword r_addend; 1.337 -} Elf32_Rela; 1.338 - 1.339 -/* Extract relocation info - r_info */ 1.340 -#define ELF32_R_SYM(i) ((i) >> 8) 1.341 -#define ELF32_R_TYPE(i) ((unsigned char) (i)) 1.342 -#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t)) 1.343 - 1.344 -typedef struct { 1.345 - Elf64_Xword r_offset; /* where to do it */ 1.346 - Elf64_Xword r_info; /* index & type of relocation */ 1.347 -} Elf64_Rel; 1.348 - 1.349 -typedef struct { 1.350 - Elf64_Xword r_offset; /* where to do it */ 1.351 - Elf64_Xword r_info; /* index & type of relocation */ 1.352 - Elf64_Sxword r_addend; /* adjustment value */ 1.353 -} Elf64_Rela; 1.354 - 1.355 -#define ELF64_R_SYM(info) ((info) >> 32) 1.356 -#define ELF64_R_TYPE(info) ((info) & 0xFFFFFFFF) 1.357 -#define ELF64_R_INFO(s,t) (((s) << 32) + (u_int32_t)(t)) 1.358 - 1.359 -/* Program Header */ 1.360 -typedef struct { 1.361 - Elf32_Word p_type; /* segment type */ 1.362 - Elf32_Off p_offset; /* segment offset */ 1.363 - Elf32_Addr p_vaddr; /* virtual address of segment */ 1.364 - Elf32_Addr p_paddr; /* physical address - ignored? */ 1.365 - Elf32_Word p_filesz; /* number of bytes in file for seg. */ 1.366 - Elf32_Word p_memsz; /* number of bytes in mem. for seg. */ 1.367 - Elf32_Word p_flags; /* flags */ 1.368 - Elf32_Word p_align; /* memory alignment */ 1.369 -} Elf32_Phdr; 1.370 - 1.371 -typedef struct { 1.372 - Elf64_Half p_type; /* entry type */ 1.373 - Elf64_Half p_flags; /* flags */ 1.374 - Elf64_Off p_offset; /* offset */ 1.375 - Elf64_Addr p_vaddr; /* virtual address */ 1.376 - Elf64_Addr p_paddr; /* physical address */ 1.377 - Elf64_Xword p_filesz; /* file size */ 1.378 - Elf64_Xword p_memsz; /* memory size */ 1.379 - Elf64_Xword p_align; /* memory & file alignment */ 1.380 -} Elf64_Phdr; 1.381 - 1.382 -/* Segment types - p_type */ 1.383 -#define PT_NULL 0 /* unused */ 1.384 -#define PT_LOAD 1 /* loadable segment */ 1.385 -#define PT_DYNAMIC 2 /* dynamic linking section */ 1.386 -#define PT_INTERP 3 /* the RTLD */ 1.387 -#define PT_NOTE 4 /* auxiliary information */ 1.388 -#define PT_SHLIB 5 /* reserved - purpose undefined */ 1.389 -#define PT_PHDR 6 /* program header */ 1.390 -#define PT_NUM 7 /* Number of segment types */ 1.391 -#define PT_LOPROC 0x70000000 /* reserved range for processor */ 1.392 -#define PT_HIPROC 0x7fffffff /* specific segment types */ 1.393 - 1.394 -/* Segment flags - p_flags */ 1.395 -#define PF_X 0x1 /* Executable */ 1.396 -#define PF_W 0x2 /* Writable */ 1.397 -#define PF_R 0x4 /* Readable */ 1.398 -#define PF_MASKPROC 0xf0000000 /* reserved bits for processor */ 1.399 - /* specific segment flags */ 1.400 - 1.401 -/* Dynamic structure */ 1.402 -typedef struct { 1.403 - Elf32_Sword d_tag; /* controls meaning of d_val */ 1.404 - union { 1.405 - Elf32_Word d_val; /* Multiple meanings - see d_tag */ 1.406 - Elf32_Addr d_ptr; /* program virtual address */ 1.407 - } d_un; 1.408 -} Elf32_Dyn; 1.409 - 1.410 -typedef struct { 1.411 - Elf64_Xword d_tag; /* controls meaning of d_val */ 1.412 - union { 1.413 - Elf64_Addr d_ptr; 1.414 - Elf64_Xword d_val; 1.415 - } d_un; 1.416 -} Elf64_Dyn; 1.417 - 1.418 -/* Dynamic Array Tags - d_tag */ 1.419 -#define DT_NULL 0 /* marks end of _DYNAMIC array */ 1.420 -#define DT_NEEDED 1 /* string table offset of needed lib */ 1.421 -#define DT_PLTRELSZ 2 /* size of relocation entries in PLT */ 1.422 -#define DT_PLTGOT 3 /* address PLT/GOT */ 1.423 -#define DT_HASH 4 /* address of symbol hash table */ 1.424 -#define DT_STRTAB 5 /* address of string table */ 1.425 -#define DT_SYMTAB 6 /* address of symbol table */ 1.426 -#define DT_RELA 7 /* address of relocation table */ 1.427 -#define DT_RELASZ 8 /* size of relocation table */ 1.428 -#define DT_RELAENT 9 /* size of relocation entry */ 1.429 -#define DT_STRSZ 10 /* size of string table */ 1.430 -#define DT_SYMENT 11 /* size of symbol table entry */ 1.431 -#define DT_INIT 12 /* address of initialization func. */ 1.432 -#define DT_FINI 13 /* address of termination function */ 1.433 -#define DT_SONAME 14 /* string table offset of shared obj */ 1.434 -#define DT_RPATH 15 /* string table offset of library 1.435 - search path */ 1.436 -#define DT_SYMBOLIC 16 /* start sym search in shared obj. */ 1.437 -#define DT_REL 17 /* address of rel. tbl. w addends */ 1.438 -#define DT_RELSZ 18 /* size of DT_REL relocation table */ 1.439 -#define DT_RELENT 19 /* size of DT_REL relocation entry */ 1.440 -#define DT_PLTREL 20 /* PLT referenced relocation entry */ 1.441 -#define DT_DEBUG 21 /* bugger */ 1.442 -#define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */ 1.443 -#define DT_JMPREL 23 /* add. of PLT's relocation entries */ 1.444 -#define DT_BIND_NOW 24 /* Bind now regardless of env setting */ 1.445 -#define DT_NUM 25 /* Number used. */ 1.446 -#define DT_LOPROC 0x70000000 /* reserved range for processor */ 1.447 -#define DT_HIPROC 0x7fffffff /* specific dynamic array tags */ 1.448 - 1.449 -/* Standard ELF hashing function */ 1.450 -unsigned int elf_hash(const unsigned char *name); 1.451 - 1.452 -/* 1.453 - * Note Definitions 1.454 - */ 1.455 -typedef struct { 1.456 - Elf32_Word namesz; 1.457 - Elf32_Word descsz; 1.458 - Elf32_Word type; 1.459 -} Elf32_Note; 1.460 - 1.461 -typedef struct { 1.462 - Elf64_Half namesz; 1.463 - Elf64_Half descsz; 1.464 - Elf64_Half type; 1.465 -} Elf64_Note; 1.466 - 1.467 - 1.468 -#if defined(ELFSIZE) 1.469 -#define CONCAT(x,y) __CONCAT(x,y) 1.470 -#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x))) 1.471 -#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y)))) 1.472 -#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE)) 1.473 -#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x))) 1.474 -#endif 1.475 - 1.476 -#if defined(ELFSIZE) && (ELFSIZE == 32) 1.477 -#define Elf_Ehdr Elf32_Ehdr 1.478 -#define Elf_Phdr Elf32_Phdr 1.479 -#define Elf_Shdr Elf32_Shdr 1.480 -#define Elf_Sym Elf32_Sym 1.481 -#define Elf_Rel Elf32_Rel 1.482 -#define Elf_RelA Elf32_Rela 1.483 -#define Elf_Dyn Elf32_Dyn 1.484 -#define Elf_Word Elf32_Word 1.485 -#define Elf_Sword Elf32_Sword 1.486 -#define Elf_Addr Elf32_Addr 1.487 -#define Elf_Off Elf32_Off 1.488 -#define Elf_Nhdr Elf32_Nhdr 1.489 -#define Elf_Note Elf32_Note 1.490 - 1.491 -#define ELF_R_SYM ELF32_R_SYM 1.492 -#define ELF_R_TYPE ELF32_R_TYPE 1.493 -#define ELF_R_INFO ELF32_R_INFO 1.494 -#define ELFCLASS ELFCLASS32 1.495 - 1.496 -#define ELF_ST_BIND ELF32_ST_BIND 1.497 -#define ELF_ST_TYPE ELF32_ST_TYPE 1.498 -#define ELF_ST_INFO ELF32_ST_INFO 1.499 - 1.500 -#define AuxInfo Aux32Info 1.501 -#elif defined(ELFSIZE) && (ELFSIZE == 64) 1.502 -#define Elf_Ehdr Elf64_Ehdr 1.503 -#define Elf_Phdr Elf64_Phdr 1.504 -#define Elf_Shdr Elf64_Shdr 1.505 -#define Elf_Sym Elf64_Sym 1.506 -#define Elf_Rel Elf64_Rel 1.507 -#define Elf_RelA Elf64_Rela 1.508 -#define Elf_Dyn Elf64_Dyn 1.509 -#define Elf_Word Elf64_Word 1.510 -#define Elf_Sword Elf64_Sword 1.511 -#define Elf_Addr Elf64_Addr 1.512 -#define Elf_Off Elf64_Off 1.513 -#define Elf_Nhdr Elf64_Nhdr 1.514 -#define Elf_Note Elf64_Note 1.515 - 1.516 -#define ELF_R_SYM ELF64_R_SYM 1.517 -#define ELF_R_TYPE ELF64_R_TYPE 1.518 -#define ELF_R_INFO ELF64_R_INFO 1.519 -#define ELFCLASS ELFCLASS64 1.520 - 1.521 -#define ELF_ST_BIND ELF64_ST_BIND 1.522 -#define ELF_ST_TYPE ELF64_ST_TYPE 1.523 -#define ELF_ST_INFO ELF64_ST_INFO 1.524 - 1.525 -#define AuxInfo Aux64Info 1.526 -#endif 1.527 - 1.528 +#include <xen/elfstructs.h>
2.1 --- a/xen/arch/x86/boot/mkelf32.c Thu Jan 25 18:54:25 2007 +0000 2.2 +++ b/xen/arch/x86/boot/mkelf32.c Thu Jan 25 22:16:52 2007 +0000 2.3 @@ -25,7 +25,7 @@ 2.4 #define s16 int16_t 2.5 #define s32 int32_t 2.6 #define s64 int64_t 2.7 -#include "../../../include/xen/elf.h" 2.8 +#include "../../../include/public/elfstructs.h" 2.9 2.10 #define DYNAMICALLY_FILLED 0 2.11 #define RAW_OFFSET 128
3.1 --- a/xen/common/Makefile Thu Jan 25 18:54:25 2007 +0000 3.2 +++ b/xen/common/Makefile Thu Jan 25 22:16:52 2007 +0000 3.3 @@ -37,6 +37,8 @@ obj-$(CONFIG_XENCOMM) += xencomm.o 3.4 3.5 subdir-$(CONFIG_COMPAT) += compat 3.6 3.7 +subdir-y += libelf 3.8 + 3.9 # Object file contains changeset and compiler information. 3.10 version.o: $(BASEDIR)/include/xen/compile.h 3.11
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/xen/common/libelf/Makefile Thu Jan 25 22:16:52 2007 +0000 4.3 @@ -0,0 +1,4 @@ 4.4 +obj-y += libelf-tools.o 4.5 +obj-y += libelf-loader.o 4.6 +obj-y += libelf-dominfo.o 4.7 +#obj-y += libelf-relocate.o
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/xen/common/libelf/README Thu Jan 25 22:16:52 2007 +0000 5.3 @@ -0,0 +1,1 @@ 5.4 +Take care, this code is used by both xen and tools ...
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/xen/common/libelf/libelf-dominfo.c Thu Jan 25 22:16:52 2007 +0000 6.3 @@ -0,0 +1,420 @@ 6.4 +/* 6.5 + * parse xen-specific informations out of elf kernel binaries. 6.6 + */ 6.7 + 6.8 +#include "libelf-private.h" 6.9 + 6.10 +/* ------------------------------------------------------------------------ */ 6.11 +/* xen features */ 6.12 + 6.13 +const char *elf_xen_feature_names[] = { 6.14 + [XENFEAT_writable_page_tables] = "writable_page_tables", 6.15 + [XENFEAT_writable_descriptor_tables] = "writable_descriptor_tables", 6.16 + [XENFEAT_auto_translated_physmap] = "auto_translated_physmap", 6.17 + [XENFEAT_supervisor_mode_kernel] = "supervisor_mode_kernel", 6.18 + [XENFEAT_pae_pgdir_above_4gb] = "pae_pgdir_above_4gb" 6.19 +}; 6.20 +const int elf_xen_features = 6.21 + sizeof(elf_xen_feature_names) / sizeof(elf_xen_feature_names[0]); 6.22 + 6.23 +int elf_xen_parse_features(const char *features, 6.24 + uint32_t *supported, 6.25 + uint32_t *required) 6.26 +{ 6.27 + char feature[64]; 6.28 + int pos, len, i; 6.29 + 6.30 + if (NULL == features) 6.31 + return 0; 6.32 + for (pos = 0; features[pos] != '\0'; pos += len) 6.33 + { 6.34 + memset(feature, 0, sizeof(feature)); 6.35 + for (len = 0;; len++) 6.36 + { 6.37 + if (len >= sizeof(feature)-1) 6.38 + break; 6.39 + if (features[pos + len] == '\0') 6.40 + break; 6.41 + if (features[pos + len] == '|') 6.42 + { 6.43 + len++; 6.44 + break; 6.45 + } 6.46 + feature[len] = features[pos + len]; 6.47 + } 6.48 + 6.49 + for (i = 0; i < elf_xen_features; i++) 6.50 + { 6.51 + if (!elf_xen_feature_names[i]) 6.52 + continue; 6.53 + if (NULL != required && feature[0] == '!') 6.54 + { 6.55 + /* required */ 6.56 + if (0 == strcmp(feature + 1, elf_xen_feature_names[i])) 6.57 + { 6.58 + elf_xen_feature_set(i, supported); 6.59 + elf_xen_feature_set(i, required); 6.60 + break; 6.61 + } 6.62 + } 6.63 + else 6.64 + { 6.65 + /* supported */ 6.66 + if (0 == strcmp(feature, elf_xen_feature_names[i])) 6.67 + { 6.68 + elf_xen_feature_set(i, supported); 6.69 + break; 6.70 + } 6.71 + } 6.72 + } 6.73 + if (i == elf_xen_features) 6.74 + return -1; 6.75 + } 6.76 + return 0; 6.77 +} 6.78 + 6.79 +/* ------------------------------------------------------------------------ */ 6.80 +/* xen elf notes */ 6.81 + 6.82 +int elf_xen_parse_note(struct elf_binary *elf, 6.83 + struct elf_dom_parms *parms, 6.84 + const elf_note *note) 6.85 +{ 6.86 +/* *INDENT-OFF* */ 6.87 + static const struct { 6.88 + char *name; 6.89 + int str; 6.90 + } note_desc[] = { 6.91 + [XEN_ELFNOTE_ENTRY] = { "ENTRY", 0}, 6.92 + [XEN_ELFNOTE_HYPERCALL_PAGE] = { "HYPERCALL_PAGE", 0}, 6.93 + [XEN_ELFNOTE_VIRT_BASE] = { "VIRT_BASE", 0}, 6.94 + [XEN_ELFNOTE_PADDR_OFFSET] = { "PADDR_OFFSET", 0}, 6.95 + [XEN_ELFNOTE_HV_START_LOW] = { "HV_START_LOW", 0}, 6.96 + [XEN_ELFNOTE_XEN_VERSION] = { "XEN_VERSION", 1}, 6.97 + [XEN_ELFNOTE_GUEST_OS] = { "GUEST_OS", 1}, 6.98 + [XEN_ELFNOTE_GUEST_VERSION] = { "GUEST_VERSION", 1}, 6.99 + [XEN_ELFNOTE_LOADER] = { "LOADER", 1}, 6.100 + [XEN_ELFNOTE_PAE_MODE] = { "PAE_MODE", 1}, 6.101 + [XEN_ELFNOTE_FEATURES] = { "FEATURES", 1}, 6.102 + [XEN_ELFNOTE_BSD_SYMTAB] = { "BSD_SYMTAB", 1}, 6.103 + }; 6.104 +/* *INDENT-ON* */ 6.105 + 6.106 + const char *str = NULL; 6.107 + uint64_t val = 0; 6.108 + int type = elf_uval(elf, note, type); 6.109 + 6.110 + if ((type >= sizeof(note_desc) / sizeof(note_desc[0])) || 6.111 + (NULL == note_desc[type].name)) 6.112 + { 6.113 + elf_err(elf, "%s: unknown xen elf note (0x%x)\n", 6.114 + __FUNCTION__, type); 6.115 + return -1; 6.116 + } 6.117 + 6.118 + if (note_desc[type].str) 6.119 + { 6.120 + str = elf_note_desc(elf, note); 6.121 + elf_msg(elf, "%s: %s = \"%s\"\n", __FUNCTION__, 6.122 + note_desc[type].name, str); 6.123 + } 6.124 + else 6.125 + { 6.126 + val = elf_note_numeric(elf, note); 6.127 + elf_msg(elf, "%s: %s = 0x%" PRIx64 "\n", __FUNCTION__, 6.128 + note_desc[type].name, val); 6.129 + } 6.130 + 6.131 + switch (type) 6.132 + { 6.133 + case XEN_ELFNOTE_LOADER: 6.134 + strncpy(parms->loader, str, sizeof(parms->loader)); 6.135 + break; 6.136 + case XEN_ELFNOTE_GUEST_OS: 6.137 + strncpy(parms->guest_os, str, sizeof(parms->guest_os)); 6.138 + break; 6.139 + case XEN_ELFNOTE_GUEST_VERSION: 6.140 + strncpy(parms->guest_ver, str, sizeof(parms->guest_ver)); 6.141 + break; 6.142 + case XEN_ELFNOTE_XEN_VERSION: 6.143 + strncpy(parms->xen_ver, str, sizeof(parms->xen_ver)); 6.144 + break; 6.145 + case XEN_ELFNOTE_PAE_MODE: 6.146 + if (0 == strcmp(str, "yes")) 6.147 + parms->pae = 2 /* extended_cr3 */; 6.148 + if (strstr(str, "bimodal")) 6.149 + parms->pae = 3 /* bimodal */; 6.150 + break; 6.151 + case XEN_ELFNOTE_BSD_SYMTAB: 6.152 + if (0 == strcmp(str, "yes")) 6.153 + parms->bsd_symtab = 1; 6.154 + break; 6.155 + 6.156 + case XEN_ELFNOTE_VIRT_BASE: 6.157 + parms->virt_base = val; 6.158 + break; 6.159 + case XEN_ELFNOTE_ENTRY: 6.160 + parms->virt_entry = val; 6.161 + break; 6.162 + case XEN_ELFNOTE_PADDR_OFFSET: 6.163 + parms->elf_paddr_offset = val; 6.164 + break; 6.165 + case XEN_ELFNOTE_HYPERCALL_PAGE: 6.166 + parms->virt_hypercall = val; 6.167 + break; 6.168 + case XEN_ELFNOTE_HV_START_LOW: 6.169 + parms->virt_hv_start_low = val; 6.170 + break; 6.171 + 6.172 + case XEN_ELFNOTE_FEATURES: 6.173 + if (0 != elf_xen_parse_features(str, parms->f_supported, 6.174 + parms->f_required)) 6.175 + return -1; 6.176 + break; 6.177 + 6.178 + } 6.179 + return 0; 6.180 +} 6.181 + 6.182 +/* ------------------------------------------------------------------------ */ 6.183 +/* __xen_guest section */ 6.184 + 6.185 +int elf_xen_parse_guest_info(struct elf_binary *elf, 6.186 + struct elf_dom_parms *parms) 6.187 +{ 6.188 + const char *h; 6.189 + char name[32], value[128]; 6.190 + int len; 6.191 + 6.192 + h = parms->guest_info; 6.193 + while (*h) 6.194 + { 6.195 + memset(name, 0, sizeof(name)); 6.196 + memset(value, 0, sizeof(value)); 6.197 + for (len = 0;; len++, h++) { 6.198 + if (len >= sizeof(name)-1) 6.199 + break; 6.200 + if (*h == '\0') 6.201 + break; 6.202 + if (*h == ',') 6.203 + { 6.204 + h++; 6.205 + break; 6.206 + } 6.207 + if (*h == '=') 6.208 + { 6.209 + h++; 6.210 + for (len = 0;; len++, h++) { 6.211 + if (len >= sizeof(value)-1) 6.212 + break; 6.213 + if (*h == '\0') 6.214 + break; 6.215 + if (*h == ',') 6.216 + { 6.217 + h++; 6.218 + break; 6.219 + } 6.220 + value[len] = *h; 6.221 + } 6.222 + break; 6.223 + } 6.224 + name[len] = *h; 6.225 + } 6.226 + elf_msg(elf, "%s: %s=\"%s\"\n", __FUNCTION__, name, value); 6.227 + 6.228 + /* strings */ 6.229 + if (0 == strcmp(name, "LOADER")) 6.230 + strncpy(parms->loader, value, sizeof(parms->loader)); 6.231 + if (0 == strcmp(name, "GUEST_OS")) 6.232 + strncpy(parms->guest_os, value, sizeof(parms->guest_os)); 6.233 + if (0 == strcmp(name, "GUEST_VER")) 6.234 + strncpy(parms->guest_ver, value, sizeof(parms->guest_ver)); 6.235 + if (0 == strcmp(name, "XEN_VER")) 6.236 + strncpy(parms->xen_ver, value, sizeof(parms->xen_ver)); 6.237 + if (0 == strcmp(name, "PAE")) 6.238 + { 6.239 + if (0 == strcmp(value, "yes[extended-cr3]")) 6.240 + parms->pae = 2 /* extended_cr3 */; 6.241 + else if (0 == strncmp(value, "yes", 3)) 6.242 + parms->pae = 1 /* yes */; 6.243 + } 6.244 + if (0 == strcmp(name, "BSD_SYMTAB")) 6.245 + parms->bsd_symtab = 1; 6.246 + 6.247 + /* longs */ 6.248 + if (0 == strcmp(name, "VIRT_BASE")) 6.249 + parms->virt_base = strtoull(value, NULL, 0); 6.250 + if (0 == strcmp(name, "VIRT_ENTRY")) 6.251 + parms->virt_entry = strtoull(value, NULL, 0); 6.252 + if (0 == strcmp(name, "ELF_PADDR_OFFSET")) 6.253 + parms->elf_paddr_offset = strtoull(value, NULL, 0); 6.254 + if (0 == strcmp(name, "HYPERCALL_PAGE")) 6.255 + parms->virt_hypercall = (strtoull(value, NULL, 0) << 12) + 6.256 + parms->virt_base; 6.257 + 6.258 + /* other */ 6.259 + if (0 == strcmp(name, "FEATURES")) 6.260 + if (0 != elf_xen_parse_features(value, parms->f_supported, 6.261 + parms->f_required)) 6.262 + return -1; 6.263 + } 6.264 + return 0; 6.265 +} 6.266 + 6.267 +/* ------------------------------------------------------------------------ */ 6.268 +/* sanity checks */ 6.269 + 6.270 +static int elf_xen_note_check(struct elf_binary *elf, 6.271 + struct elf_dom_parms *parms) 6.272 +{ 6.273 + if (NULL == parms->elf_note_start && NULL == parms->guest_info) { 6.274 + int machine = elf_uval(elf, elf->ehdr, e_machine); 6.275 + if (EM_386 == machine || EM_X86_64 == machine) { 6.276 + elf_err(elf, "%s: ERROR: Not a Xen-ELF image: " 6.277 + "No ELF notes or '__xen_guest' section found.\n", 6.278 + __FUNCTION__); 6.279 + return -1; 6.280 + } 6.281 + return 0; 6.282 + } 6.283 + 6.284 + /* Check the contents of the Xen notes or guest string. */ 6.285 + if ( ( 0 == strlen(parms->loader) || strncmp(parms->loader, "generic", 7) ) && 6.286 + ( 0 == strlen(parms->guest_os) || strncmp(parms->guest_os, "linux", 5) ) ) 6.287 + { 6.288 + elf_err(elf, "%s: ERROR: Will only load images built for the generic " 6.289 + "loader or Linux images", __FUNCTION__); 6.290 + return -1; 6.291 + } 6.292 + 6.293 + if ( 0 == strlen(parms->xen_ver) || strncmp(parms->xen_ver, "xen-3.0", 7) ) 6.294 + { 6.295 + elf_err(elf, "%s: ERROR: Xen will only load images built for Xen v3.0\n", 6.296 + __FUNCTION__); 6.297 + return -1; 6.298 + } 6.299 + return 0; 6.300 +} 6.301 + 6.302 +static int elf_xen_addr_calc_check(struct elf_binary *elf, 6.303 + struct elf_dom_parms *parms) 6.304 +{ 6.305 + if (UNSET_ADDR != parms->elf_paddr_offset && 6.306 + UNSET_ADDR == parms->virt_base ) 6.307 + { 6.308 + elf_err(elf, "%s: ERROR: ELF_PADDR_OFFSET set, VIRT_BASE unset\n", 6.309 + __FUNCTION__); 6.310 + return -1; 6.311 + } 6.312 + 6.313 + /* Initial guess for virt_base is 0 if it is not explicitly defined. */ 6.314 + if (UNSET_ADDR == parms->virt_base) 6.315 + { 6.316 + parms->virt_base = 0; 6.317 + elf_msg(elf, "%s: VIRT_BASE unset, using 0x%" PRIx64 "\n", 6.318 + __FUNCTION__, parms->virt_base); 6.319 + } 6.320 + 6.321 + /* 6.322 + * If we are using the legacy __xen_guest section then elf_pa_off 6.323 + * defaults to v_start in order to maintain compatibility with 6.324 + * older hypervisors which set padd in the ELF header to 6.325 + * virt_base. 6.326 + * 6.327 + * If we are using the modern ELF notes interface then the default 6.328 + * is 0. 6.329 + */ 6.330 + if (UNSET_ADDR == parms->elf_paddr_offset) 6.331 + { 6.332 + if (parms->elf_note_start) 6.333 + parms->elf_paddr_offset = 0; 6.334 + else 6.335 + parms->elf_paddr_offset = parms->virt_base; 6.336 + elf_msg(elf, "%s: ELF_PADDR_OFFSET unset, using 0x%" PRIx64 "\n", 6.337 + __FUNCTION__, parms->elf_paddr_offset); 6.338 + } 6.339 + 6.340 + parms->virt_offset = parms->virt_base - parms->elf_paddr_offset; 6.341 + parms->virt_kstart = elf->pstart + parms->virt_offset; 6.342 + parms->virt_kend = elf->pend + parms->virt_offset; 6.343 + 6.344 + if (UNSET_ADDR == parms->virt_entry) 6.345 + parms->virt_entry = elf_uval(elf, elf->ehdr, e_entry); 6.346 + 6.347 + elf_msg(elf, "%s: addresses:\n", __FUNCTION__); 6.348 + elf_msg(elf, " virt_base = 0x%" PRIx64 "\n", parms->virt_base); 6.349 + elf_msg(elf, " elf_paddr_offset = 0x%" PRIx64 "\n", parms->elf_paddr_offset); 6.350 + elf_msg(elf, " virt_offset = 0x%" PRIx64 "\n", parms->virt_offset); 6.351 + elf_msg(elf, " virt_kstart = 0x%" PRIx64 "\n", parms->virt_kstart); 6.352 + elf_msg(elf, " virt_kend = 0x%" PRIx64 "\n", parms->virt_kend); 6.353 + elf_msg(elf, " virt_entry = 0x%" PRIx64 "\n", parms->virt_entry); 6.354 + 6.355 + if ( (parms->virt_kstart > parms->virt_kend) || 6.356 + (parms->virt_entry < parms->virt_kstart) || 6.357 + (parms->virt_entry > parms->virt_kend) || 6.358 + (parms->virt_base > parms->virt_kstart) ) 6.359 + { 6.360 + elf_err(elf, "%s: ERROR: ELF start or entries are out of bounds.\n", 6.361 + __FUNCTION__); 6.362 + return -1; 6.363 + } 6.364 + 6.365 + return 0; 6.366 +} 6.367 + 6.368 +/* ------------------------------------------------------------------------ */ 6.369 +/* glue it all together ... */ 6.370 + 6.371 +int elf_xen_parse(struct elf_binary *elf, 6.372 + struct elf_dom_parms *parms) 6.373 +{ 6.374 + const elf_note *note; 6.375 + const elf_shdr *shdr; 6.376 + int xen_elfnotes = 0; 6.377 + int i, count; 6.378 + 6.379 + memset(parms, 0, sizeof(*parms)); 6.380 + parms->virt_base = UNSET_ADDR; 6.381 + parms->virt_entry = UNSET_ADDR; 6.382 + parms->virt_hypercall = UNSET_ADDR; 6.383 + parms->virt_hv_start_low = UNSET_ADDR; 6.384 + parms->elf_paddr_offset = UNSET_ADDR; 6.385 + 6.386 + /* find and parse elf notes */ 6.387 + count = elf_shdr_count(elf); 6.388 + for (i = 0; i < count; i++) 6.389 + { 6.390 + shdr = elf_shdr_by_index(elf, i); 6.391 + if (0 == strcmp(elf_section_name(elf, shdr), "__xen_guest")) 6.392 + parms->guest_info = elf_section_start(elf, shdr); 6.393 + if (elf_uval(elf, shdr, sh_type) != SHT_NOTE) 6.394 + continue; 6.395 + parms->elf_note_start = elf_section_start(elf, shdr); 6.396 + parms->elf_note_end = elf_section_end(elf, shdr); 6.397 + for (note = parms->elf_note_start; 6.398 + (void *)note < parms->elf_note_end; 6.399 + note = elf_note_next(elf, note)) 6.400 + { 6.401 + if (0 != strcmp(elf_note_name(elf, note), "Xen")) 6.402 + continue; 6.403 + if (0 != elf_xen_parse_note(elf, parms, note)) 6.404 + return -1; 6.405 + xen_elfnotes++; 6.406 + } 6.407 + } 6.408 + 6.409 + if (!xen_elfnotes && parms->guest_info) 6.410 + { 6.411 + parms->elf_note_start = NULL; 6.412 + parms->elf_note_end = NULL; 6.413 + elf_msg(elf, "%s: __xen_guest: \"%s\"\n", __FUNCTION__, 6.414 + parms->guest_info); 6.415 + elf_xen_parse_guest_info(elf, parms); 6.416 + } 6.417 + 6.418 + if (0 != elf_xen_note_check(elf, parms)) 6.419 + return -1; 6.420 + if (0 != elf_xen_addr_calc_check(elf, parms)) 6.421 + return -1; 6.422 + return 0; 6.423 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/xen/common/libelf/libelf-loader.c Thu Jan 25 22:16:52 2007 +0000 7.3 @@ -0,0 +1,156 @@ 7.4 +/* 7.5 + * parse and load elf binaries 7.6 + */ 7.7 + 7.8 +#include "libelf-private.h" 7.9 + 7.10 +/* ------------------------------------------------------------------------ */ 7.11 + 7.12 +int elf_init(struct elf_binary *elf, const char *image, size_t size) 7.13 +{ 7.14 + const elf_shdr *shdr; 7.15 + uint64_t i, count, section, offset; 7.16 + 7.17 + if (!elf_is_elfbinary(image)) 7.18 + { 7.19 + elf_err(elf, "%s: not an ELF binary\n", __FUNCTION__); 7.20 + return -1; 7.21 + } 7.22 + 7.23 + memset(elf, 0, sizeof(*elf)); 7.24 + elf->image = image; 7.25 + elf->size = size; 7.26 + elf->ehdr = (elf_ehdr *) image; 7.27 + elf->class = elf->ehdr->e32.e_ident[EI_CLASS]; 7.28 + elf->data = elf->ehdr->e32.e_ident[EI_DATA]; 7.29 + 7.30 + /* sanity check phdr */ 7.31 + offset = elf_uval(elf, elf->ehdr, e_phoff) + 7.32 + elf_uval(elf, elf->ehdr, e_phentsize) * elf_phdr_count(elf); 7.33 + if (offset > elf->size) 7.34 + { 7.35 + elf_err(elf, "%s: phdr overflow (off %" PRIx64 " > size %lx)\n", 7.36 + __FUNCTION__, offset, (unsigned long)elf->size); 7.37 + return -1; 7.38 + } 7.39 + 7.40 + /* sanity check shdr */ 7.41 + offset = elf_uval(elf, elf->ehdr, e_shoff) + 7.42 + elf_uval(elf, elf->ehdr, e_shentsize) * elf_shdr_count(elf); 7.43 + if (offset > elf->size) 7.44 + { 7.45 + elf_err(elf, "%s: shdr overflow (off %" PRIx64 " > size %lx)\n", 7.46 + __FUNCTION__, offset, (unsigned long)elf->size); 7.47 + return -1; 7.48 + } 7.49 + 7.50 + /* find section string table */ 7.51 + section = elf_uval(elf, elf->ehdr, e_shstrndx); 7.52 + shdr = elf_shdr_by_index(elf, section); 7.53 + if (NULL != shdr) 7.54 + elf->sec_strtab = elf_section_start(elf, shdr); 7.55 + 7.56 + /* find symbol table, symbol string table */ 7.57 + count = elf_shdr_count(elf); 7.58 + for (i = 0; i < count; i++) 7.59 + { 7.60 + shdr = elf_shdr_by_index(elf, i); 7.61 + if (elf_uval(elf, shdr, sh_type) != SHT_SYMTAB) 7.62 + continue; 7.63 + elf->sym_tab = shdr; 7.64 + shdr = elf_shdr_by_index(elf, elf_uval(elf, shdr, sh_link)); 7.65 + if (NULL == shdr) 7.66 + { 7.67 + elf->sym_tab = NULL; 7.68 + continue; 7.69 + } 7.70 + elf->sym_strtab = elf_section_start(elf, shdr); 7.71 + break; 7.72 + } 7.73 + return 0; 7.74 +} 7.75 + 7.76 +#ifndef __XEN__ 7.77 +void elf_set_logfile(struct elf_binary *elf, FILE * log, int verbose) 7.78 +{ 7.79 + elf->log = log; 7.80 + elf->verbose = verbose; 7.81 +} 7.82 +#else 7.83 +void elf_set_verbose(struct elf_binary *elf) 7.84 +{ 7.85 + elf->verbose = 1; 7.86 +} 7.87 +#endif 7.88 + 7.89 +void elf_parse_binary(struct elf_binary *elf) 7.90 +{ 7.91 + const elf_phdr *phdr; 7.92 + uint64_t low = -1; 7.93 + uint64_t high = 0; 7.94 + uint64_t i, count, paddr, memsz; 7.95 + 7.96 + count = elf_uval(elf, elf->ehdr, e_phnum); 7.97 + for (i = 0; i < count; i++) 7.98 + { 7.99 + phdr = elf_phdr_by_index(elf, i); 7.100 + if (!elf_phdr_is_loadable(elf, phdr)) 7.101 + continue; 7.102 + paddr = elf_uval(elf, phdr, p_paddr); 7.103 + memsz = elf_uval(elf, phdr, p_memsz); 7.104 + elf_msg(elf, "%s: phdr: paddr=0x%" PRIx64 7.105 + " memsz=0x%" PRIx64 "\n", __FUNCTION__, paddr, memsz); 7.106 + if (low > paddr) 7.107 + low = paddr; 7.108 + if (high < paddr + memsz) 7.109 + high = paddr + memsz; 7.110 + } 7.111 + elf->pstart = low; 7.112 + elf->pend = high; 7.113 + elf_msg(elf, "%s: memory: 0x%" PRIx64 " -> 0x%" PRIx64 "\n", 7.114 + __FUNCTION__, elf->pstart, elf->pend); 7.115 +} 7.116 + 7.117 +void elf_load_binary(struct elf_binary *elf) 7.118 +{ 7.119 + const elf_phdr *phdr; 7.120 + uint64_t i, count, paddr, offset, filesz, memsz; 7.121 + char *dest; 7.122 + 7.123 + count = elf_uval(elf, elf->ehdr, e_phnum); 7.124 + for (i = 0; i < count; i++) 7.125 + { 7.126 + phdr = elf_phdr_by_index(elf, i); 7.127 + if (!elf_phdr_is_loadable(elf, phdr)) 7.128 + continue; 7.129 + paddr = elf_uval(elf, phdr, p_paddr); 7.130 + offset = elf_uval(elf, phdr, p_offset); 7.131 + filesz = elf_uval(elf, phdr, p_filesz); 7.132 + memsz = elf_uval(elf, phdr, p_memsz); 7.133 + dest = elf_get_ptr(elf, paddr); 7.134 + memcpy(dest, elf->image + offset, filesz); 7.135 + memset(dest + filesz, 0, memsz - filesz); 7.136 + } 7.137 +} 7.138 + 7.139 +void *elf_get_ptr(struct elf_binary *elf, unsigned long addr) 7.140 +{ 7.141 + return elf->dest + addr - elf->pstart; 7.142 +} 7.143 + 7.144 +uint64_t elf_lookup_addr(struct elf_binary * elf, const char *symbol) 7.145 +{ 7.146 + const elf_sym *sym; 7.147 + uint64_t value; 7.148 + 7.149 + sym = elf_sym_by_name(elf, symbol); 7.150 + if (NULL == sym) 7.151 + { 7.152 + elf_err(elf, "%s: not found: %s\n", __FUNCTION__, symbol); 7.153 + return -1; 7.154 + } 7.155 + value = elf_uval(elf, sym, st_value); 7.156 + elf_msg(elf, "%s: symbol \"%s\" at 0x%" PRIx64 "\n", __FUNCTION__, 7.157 + symbol, value); 7.158 + return value; 7.159 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/xen/common/libelf/libelf-private.h Thu Jan 25 22:16:52 2007 +0000 8.3 @@ -0,0 +1,51 @@ 8.4 +#ifdef __XEN__ 8.5 + 8.6 +#include <xen/string.h> 8.7 +#include <xen/lib.h> 8.8 +#include <public/elfnote.h> 8.9 +#include <public/libelf.h> 8.10 + 8.11 +#define elf_msg(elf, fmt, args ... ) \ 8.12 + if (elf->verbose) printk(fmt, ## args ) 8.13 +#define elf_err(elf, fmt, args ... ) \ 8.14 + printk(fmt, ## args ) 8.15 + 8.16 +#define strtoull(str, end, base) simple_strtoull(str, end, base) 8.17 +#define bswap_16(x) \ 8.18 + ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)) 8.19 +#define bswap_32(x) \ 8.20 + ( (((x) & 0xff000000) >> 24) \ 8.21 + | (((x) & 0x00ff0000) >> 8) \ 8.22 + | (((x) & 0x0000ff00) << 8) \ 8.23 + | (((x) & 0x000000ff) << 24)) 8.24 +#define bswap_64(x) \ 8.25 + ( (((x) & 0xff00000000000000ull) >> 56) \ 8.26 + | (((x) & 0x00ff000000000000ull) >> 40) \ 8.27 + | (((x) & 0x0000ff0000000000ull) >> 24) \ 8.28 + | (((x) & 0x000000ff00000000ull) >> 8) \ 8.29 + | (((x) & 0x00000000ff000000ull) << 8) \ 8.30 + | (((x) & 0x0000000000ff0000ull) << 24) \ 8.31 + | (((x) & 0x000000000000ff00ull) << 40) \ 8.32 + | (((x) & 0x00000000000000ffull) << 56)) 8.33 + 8.34 +#else /* !__XEN__ */ 8.35 + 8.36 +#include <stdio.h> 8.37 +#include <stdlib.h> 8.38 +#include <string.h> 8.39 +#include <byteswap.h> 8.40 +#include <xen/elfnote.h> 8.41 +#include <xen/libelf.h> 8.42 + 8.43 +#include "xenctrl.h" 8.44 +#include "xc_private.h" 8.45 + 8.46 +#define elf_msg(elf, fmt, args ... ) \ 8.47 + if (elf->log && elf->verbose) fprintf(elf->log, fmt , ## args ) 8.48 +#define elf_err(elf, fmt, args ... ) do { \ 8.49 + if (elf->log) \ 8.50 + fprintf(elf->log, fmt , ## args ); \ 8.51 + xc_set_error(XC_INVALID_KERNEL, fmt , ## args ); \ 8.52 + } while (0) 8.53 + 8.54 +#endif
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/xen/common/libelf/libelf-relocate.c Thu Jan 25 22:16:52 2007 +0000 9.3 @@ -0,0 +1,345 @@ 9.4 +/* 9.5 + * ELF relocation code (not used by xen kernel right now). 9.6 + */ 9.7 + 9.8 +#include "libelf-private.h" 9.9 + 9.10 +/* ------------------------------------------------------------------------ */ 9.11 + 9.12 +static const char *rel_names_i386[] = { 9.13 + "R_386_NONE", 9.14 + "R_386_32", 9.15 + "R_386_PC32", 9.16 + "R_386_GOT32", 9.17 + "R_386_PLT32", 9.18 + "R_386_COPY", 9.19 + "R_386_GLOB_DAT", 9.20 + "R_386_JMP_SLOT", 9.21 + "R_386_RELATIVE", 9.22 + "R_386_GOTOFF", 9.23 + "R_386_GOTPC", 9.24 + "R_386_32PLT", 9.25 + "R_386_TLS_TPOFF", 9.26 + "R_386_TLS_IE", 9.27 + "R_386_TLS_GOTIE", 9.28 + "R_386_TLS_LE", 9.29 + "R_386_TLS_GD", 9.30 + "R_386_TLS_LDM", 9.31 + "R_386_16", 9.32 + "R_386_PC16", 9.33 + "R_386_8", 9.34 + "R_386_PC8", 9.35 + "R_386_TLS_GD_32", 9.36 + "R_386_TLS_GD_PUSH", 9.37 + "R_386_TLS_GD_CALL", 9.38 + "R_386_TLS_GD_POP", 9.39 + "R_386_TLS_LDM_32", 9.40 + "R_386_TLS_LDM_PUSH", 9.41 + "R_386_TLS_LDM_CALL", 9.42 + "R_386_TLS_LDM_POP", 9.43 + "R_386_TLS_LDO_32", 9.44 + "R_386_TLS_IE_32", 9.45 + "R_386_TLS_LE_32", 9.46 + "R_386_TLS_DTPMOD32", 9.47 + "R_386_TLS_DTPOFF32", 9.48 + "R_386_TLS_TPOFF32", 9.49 +}; 9.50 + 9.51 +static int elf_reloc_i386(struct elf_binary *elf, int type, 9.52 + uint64_t addr, uint64_t value) 9.53 +{ 9.54 + void *ptr = elf_get_ptr(elf, addr); 9.55 + uint32_t *u32; 9.56 + 9.57 + switch (type) 9.58 + { 9.59 + case 1 /* R_386_32 */ : 9.60 + u32 = ptr; 9.61 + *u32 += elf->reloc_offset; 9.62 + break; 9.63 + case 2 /* R_386_PC32 */ : 9.64 + /* nothing */ 9.65 + break; 9.66 + default: 9.67 + return -1; 9.68 + } 9.69 + return 0; 9.70 +} 9.71 + 9.72 +/* ------------------------------------------------------------------------ */ 9.73 + 9.74 +static const char *rel_names_x86_64[] = { 9.75 + "R_X86_64_NONE", 9.76 + "R_X86_64_64", 9.77 + "R_X86_64_PC32", 9.78 + "R_X86_64_GOT32", 9.79 + "R_X86_64_PLT32", 9.80 + "R_X86_64_COPY", 9.81 + "R_X86_64_GLOB_DAT", 9.82 + "R_X86_64_JUMP_SLOT", 9.83 + "R_X86_64_RELATIVE", 9.84 + "R_X86_64_GOTPCREL", 9.85 + "R_X86_64_32", 9.86 + "R_X86_64_32S", 9.87 + "R_X86_64_16", 9.88 + "R_X86_64_PC16", 9.89 + "R_X86_64_8", 9.90 + "R_X86_64_PC8", 9.91 + "R_X86_64_DTPMOD64", 9.92 + "R_X86_64_DTPOFF64", 9.93 + "R_X86_64_TPOFF64", 9.94 + "R_X86_64_TLSGD", 9.95 + "R_X86_64_TLSLD", 9.96 + "R_X86_64_DTPOFF32", 9.97 + "R_X86_64_GOTTPOFF", 9.98 + "R_X86_64_TPOFF32", 9.99 +}; 9.100 + 9.101 +static int elf_reloc_x86_64(struct elf_binary *elf, int type, 9.102 + uint64_t addr, uint64_t value) 9.103 +{ 9.104 + void *ptr = elf_get_ptr(elf, addr); 9.105 + uint64_t *u64; 9.106 + uint32_t *u32; 9.107 + int32_t *s32; 9.108 + 9.109 + switch (type) 9.110 + { 9.111 + case 1 /* R_X86_64_64 */ : 9.112 + u64 = ptr; 9.113 + value += elf->reloc_offset; 9.114 + *u64 = value; 9.115 + break; 9.116 + case 2 /* R_X86_64_PC32 */ : 9.117 + u32 = ptr; 9.118 + *u32 = value - addr; 9.119 + if (*u32 != (uint32_t) (value - addr)) 9.120 + { 9.121 + elf_err(elf, "R_X86_64_PC32 overflow: 0x%" PRIx32 " != 0x%" PRIx32 "\n", 9.122 + *u32, (uint32_t) (value - addr)); 9.123 + return -1; 9.124 + } 9.125 + break; 9.126 + case 10 /* R_X86_64_32 */ : 9.127 + u32 = ptr; 9.128 + value += elf->reloc_offset; 9.129 + *u32 = value; 9.130 + if (*u32 != value) 9.131 + { 9.132 + elf_err(elf, "R_X86_64_32 overflow: 0x%" PRIx32 " != 0x%" PRIx64 "\n", 9.133 + *u32, value); 9.134 + return -1; 9.135 + } 9.136 + break; 9.137 + case 11 /* R_X86_64_32S */ : 9.138 + s32 = ptr; 9.139 + value += elf->reloc_offset; 9.140 + *s32 = value; 9.141 + if (*s32 != (int64_t) value) 9.142 + { 9.143 + elf_err(elf, "R_X86_64_32S overflow: 0x%" PRIx32 " != 0x%" PRIx64 "\n", 9.144 + *s32, (int64_t) value); 9.145 + return -1; 9.146 + } 9.147 + break; 9.148 + default: 9.149 + return -1; 9.150 + } 9.151 + return 0; 9.152 +} 9.153 + 9.154 +/* ------------------------------------------------------------------------ */ 9.155 + 9.156 +static struct relocs { 9.157 + const char **names; 9.158 + int count; 9.159 + int (*func) (struct elf_binary * elf, int type, uint64_t addr, 9.160 + uint64_t value); 9.161 +} relocs[] = 9.162 +/* *INDENT-OFF* */ 9.163 +{ 9.164 + [EM_386] = { 9.165 + .names = rel_names_i386, 9.166 + .count = sizeof(rel_names_i386) / sizeof(rel_names_i386[0]), 9.167 + .func = elf_reloc_i386, 9.168 + }, 9.169 + [EM_X86_64] = { 9.170 + .names = rel_names_x86_64, 9.171 + .count = sizeof(rel_names_x86_64) / sizeof(rel_names_x86_64[0]), 9.172 + .func = elf_reloc_x86_64, 9.173 + } 9.174 +}; 9.175 +/* *INDENT-ON* */ 9.176 + 9.177 +/* ------------------------------------------------------------------------ */ 9.178 + 9.179 +static const char *rela_name(int machine, int type) 9.180 +{ 9.181 + if (machine > sizeof(relocs) / sizeof(relocs[0])) 9.182 + return "unknown mach"; 9.183 + if (!relocs[machine].names) 9.184 + return "unknown mach"; 9.185 + if (type > relocs[machine].count) 9.186 + return "unknown rela"; 9.187 + return relocs[machine].names[type]; 9.188 +} 9.189 + 9.190 +static int elf_reloc_section(struct elf_binary *elf, 9.191 + const elf_shdr * rels, 9.192 + const elf_shdr * sect, const elf_shdr * syms) 9.193 +{ 9.194 + const void *ptr, *end; 9.195 + const elf_shdr *shdr; 9.196 + const elf_rela *rela; 9.197 + const elf_rel *rel; 9.198 + const elf_sym *sym; 9.199 + uint64_t s_type; 9.200 + uint64_t r_offset; 9.201 + uint64_t r_info; 9.202 + uint64_t r_addend; 9.203 + int r_type, r_sym; 9.204 + size_t rsize; 9.205 + uint64_t shndx, sbase, addr, value; 9.206 + const char *sname; 9.207 + int machine; 9.208 + 9.209 + machine = elf_uval(elf, elf->ehdr, e_machine); 9.210 + if (machine >= sizeof(relocs) / sizeof(relocs[0]) || 9.211 + NULL == relocs[machine].func) 9.212 + { 9.213 + elf_err(elf, "%s: can't handle machine %d\n", 9.214 + __FUNCTION__, machine); 9.215 + return -1; 9.216 + } 9.217 + if (elf_swap(elf)) 9.218 + { 9.219 + elf_err(elf, "%s: non-native byte order, relocation not supported\n", 9.220 + __FUNCTION__); 9.221 + return -1; 9.222 + } 9.223 + 9.224 + s_type = elf_uval(elf, rels, sh_type); 9.225 + rsize = (SHT_REL == s_type) ? elf_size(elf, rel) : elf_size(elf, rela); 9.226 + ptr = elf_section_start(elf, rels); 9.227 + end = elf_section_end(elf, rels); 9.228 + 9.229 + for (; ptr < end; ptr += rsize) 9.230 + { 9.231 + switch (s_type) 9.232 + { 9.233 + case SHT_REL: 9.234 + rel = ptr; 9.235 + r_offset = elf_uval(elf, rel, r_offset); 9.236 + r_info = elf_uval(elf, rel, r_info); 9.237 + r_addend = 0; 9.238 + break; 9.239 + case SHT_RELA: 9.240 + rela = ptr; 9.241 + r_offset = elf_uval(elf, rela, r_offset); 9.242 + r_info = elf_uval(elf, rela, r_info); 9.243 + r_addend = elf_uval(elf, rela, r_addend); 9.244 + break; 9.245 + default: 9.246 + /* can't happen */ 9.247 + return -1; 9.248 + } 9.249 + if (elf_64bit(elf)) 9.250 + { 9.251 + r_type = ELF64_R_TYPE(r_info); 9.252 + r_sym = ELF64_R_SYM(r_info); 9.253 + } 9.254 + else 9.255 + { 9.256 + r_type = ELF32_R_TYPE(r_info); 9.257 + r_sym = ELF32_R_SYM(r_info); 9.258 + } 9.259 + 9.260 + sym = elf_sym_by_index(elf, r_sym); 9.261 + shndx = elf_uval(elf, sym, st_shndx); 9.262 + switch (shndx) 9.263 + { 9.264 + case SHN_UNDEF: 9.265 + sname = "*UNDEF*"; 9.266 + sbase = 0; 9.267 + break; 9.268 + case SHN_COMMON: 9.269 + elf_err(elf, "%s: invalid section: %" PRId64 "\n", 9.270 + __FUNCTION__, shndx); 9.271 + return -1; 9.272 + case SHN_ABS: 9.273 + sname = "*ABS*"; 9.274 + sbase = 0; 9.275 + break; 9.276 + default: 9.277 + shdr = elf_shdr_by_index(elf, shndx); 9.278 + if (NULL == shdr) 9.279 + { 9.280 + elf_err(elf, "%s: invalid section: %" PRId64 "\n", 9.281 + __FUNCTION__, shndx); 9.282 + return -1; 9.283 + } 9.284 + sname = elf_section_name(elf, shdr); 9.285 + sbase = elf_uval(elf, shdr, sh_addr); 9.286 + } 9.287 + 9.288 + addr = r_offset; 9.289 + value = elf_uval(elf, sym, st_value); 9.290 + value += r_addend; 9.291 + 9.292 + if (elf->log && elf->verbose > 1) 9.293 + { 9.294 + uint64_t st_name = elf_uval(elf, sym, st_name); 9.295 + const char *name = st_name ? elf->sym_strtab + st_name : "*NONE*"; 9.296 + 9.297 + elf_msg(elf, 9.298 + "%s: type %s [%d], off 0x%" PRIx64 ", add 0x%" PRIx64 "," 9.299 + " sym %s [0x%" PRIx64 "], sec %s [0x%" PRIx64 "]" 9.300 + " -> addr 0x%" PRIx64 " value 0x%" PRIx64 "\n", 9.301 + __FUNCTION__, rela_name(machine, r_type), r_type, r_offset, 9.302 + r_addend, name, elf_uval(elf, sym, st_value), sname, sbase, 9.303 + addr, value); 9.304 + } 9.305 + 9.306 + if (-1 == relocs[machine].func(elf, r_type, addr, value)) 9.307 + { 9.308 + elf_err(elf, "%s: unknown/unsupported reloc type %s [%d]\n", 9.309 + __FUNCTION__, rela_name(machine, r_type), r_type); 9.310 + return -1; 9.311 + } 9.312 + } 9.313 + return 0; 9.314 +} 9.315 + 9.316 +int elf_reloc(struct elf_binary *elf) 9.317 +{ 9.318 + const elf_shdr *rels, *sect, *syms; 9.319 + uint64_t i, count, type; 9.320 + 9.321 + count = elf_shdr_count(elf); 9.322 + for (i = 0; i < count; i++) 9.323 + { 9.324 + rels = elf_shdr_by_index(elf, i); 9.325 + type = elf_uval(elf, rels, sh_type); 9.326 + if (type != SHT_REL && type != SHT_RELA) 9.327 + continue; 9.328 + 9.329 + sect = elf_shdr_by_index(elf, elf_uval(elf, rels, sh_info)); 9.330 + syms = elf_shdr_by_index(elf, elf_uval(elf, rels, sh_link)); 9.331 + if (NULL == sect || NULL == syms) 9.332 + continue; 9.333 + 9.334 + if (!(elf_uval(elf, sect, sh_flags) & SHF_ALLOC)) 9.335 + { 9.336 + elf_msg(elf, "%s: relocations for %s, skipping\n", 9.337 + __FUNCTION__, elf_section_name(elf, sect)); 9.338 + continue; 9.339 + } 9.340 + 9.341 + elf_msg(elf, "%s: relocations for %s @ 0x%" PRIx64 "\n", 9.342 + __FUNCTION__, elf_section_name(elf, sect), 9.343 + elf_uval(elf, sect, sh_addr)); 9.344 + if (0 != elf_reloc_section(elf, rels, sect, syms)) 9.345 + return -1; 9.346 + } 9.347 + return 0; 9.348 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/xen/common/libelf/libelf-tools.c Thu Jan 25 22:16:52 2007 +0000 10.3 @@ -0,0 +1,225 @@ 10.4 +/* 10.5 + * various helper functions to access elf structures 10.6 + */ 10.7 + 10.8 +#include "libelf-private.h" 10.9 + 10.10 +/* ------------------------------------------------------------------------ */ 10.11 + 10.12 +uint64_t elf_access_unsigned(struct elf_binary * elf, const void *ptr, 10.13 + off_t offset, size_t size) 10.14 +{ 10.15 + int need_swap = elf_swap(elf); 10.16 + const uint8_t *u8; 10.17 + const uint16_t *u16; 10.18 + const uint32_t *u32; 10.19 + const uint64_t *u64; 10.20 + 10.21 + switch (size) 10.22 + { 10.23 + case 1: 10.24 + u8 = ptr + offset; 10.25 + return *u8; 10.26 + case 2: 10.27 + u16 = ptr + offset; 10.28 + return need_swap ? bswap_16(*u16) : *u16; 10.29 + case 4: 10.30 + u32 = ptr + offset; 10.31 + return need_swap ? bswap_32(*u32) : *u32; 10.32 + case 8: 10.33 + u64 = ptr + offset; 10.34 + return need_swap ? bswap_64(*u64) : *u64; 10.35 + default: 10.36 + return 0; 10.37 + } 10.38 +} 10.39 + 10.40 +int64_t elf_access_signed(struct elf_binary *elf, const void *ptr, 10.41 + off_t offset, size_t size) 10.42 +{ 10.43 + int need_swap = elf_swap(elf); 10.44 + const int8_t *s8; 10.45 + const int16_t *s16; 10.46 + const int32_t *s32; 10.47 + const int64_t *s64; 10.48 + 10.49 + switch (size) 10.50 + { 10.51 + case 1: 10.52 + s8 = ptr + offset; 10.53 + return *s8; 10.54 + case 2: 10.55 + s16 = ptr + offset; 10.56 + return need_swap ? bswap_16(*s16) : *s16; 10.57 + case 4: 10.58 + s32 = ptr + offset; 10.59 + return need_swap ? bswap_32(*s32) : *s32; 10.60 + case 8: 10.61 + s64 = ptr + offset; 10.62 + return need_swap ? bswap_64(*s64) : *s64; 10.63 + default: 10.64 + return 0; 10.65 + } 10.66 +} 10.67 + 10.68 +uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr) 10.69 +{ 10.70 + int elf_round = (elf_64bit(elf) ? 8 : 4) - 1; 10.71 + 10.72 + return (addr + elf_round) & ~elf_round; 10.73 +} 10.74 + 10.75 +/* ------------------------------------------------------------------------ */ 10.76 + 10.77 +int elf_shdr_count(struct elf_binary *elf) 10.78 +{ 10.79 + return elf_uval(elf, elf->ehdr, e_shnum); 10.80 +} 10.81 + 10.82 +int elf_phdr_count(struct elf_binary *elf) 10.83 +{ 10.84 + return elf_uval(elf, elf->ehdr, e_phnum); 10.85 +} 10.86 + 10.87 +const elf_shdr *elf_shdr_by_name(struct elf_binary *elf, const char *name) 10.88 +{ 10.89 + uint64_t count = elf_shdr_count(elf); 10.90 + const elf_shdr *shdr; 10.91 + const char *sname; 10.92 + int i; 10.93 + 10.94 + for (i = 0; i < count; i++) { 10.95 + shdr = elf_shdr_by_index(elf, i); 10.96 + sname = elf_section_name(elf, shdr); 10.97 + if (sname && 0 == strcmp(sname, name)) 10.98 + return shdr; 10.99 + } 10.100 + return NULL; 10.101 +} 10.102 + 10.103 +const elf_shdr *elf_shdr_by_index(struct elf_binary *elf, int index) 10.104 +{ 10.105 + uint64_t count = elf_shdr_count(elf); 10.106 + const void *ptr = NULL; 10.107 + 10.108 + if (index < count) 10.109 + ptr = elf->image 10.110 + + elf_uval(elf, elf->ehdr, e_shoff) 10.111 + + elf_uval(elf, elf->ehdr, e_shentsize) * index; 10.112 + return ptr; 10.113 +} 10.114 + 10.115 +const elf_phdr *elf_phdr_by_index(struct elf_binary *elf, int index) 10.116 +{ 10.117 + uint64_t count = elf_uval(elf, elf->ehdr, e_phnum); 10.118 + const void *ptr = NULL; 10.119 + 10.120 + if (index < count) 10.121 + ptr = elf->image 10.122 + + elf_uval(elf, elf->ehdr, e_phoff) 10.123 + + elf_uval(elf, elf->ehdr, e_phentsize) * index; 10.124 + return ptr; 10.125 +} 10.126 + 10.127 +const char *elf_section_name(struct elf_binary *elf, const elf_shdr * shdr) 10.128 +{ 10.129 + if (NULL == elf->sec_strtab) 10.130 + return "unknown"; 10.131 + return elf->sec_strtab + elf_uval(elf, shdr, sh_name); 10.132 +} 10.133 + 10.134 +const void *elf_section_start(struct elf_binary *elf, const elf_shdr * shdr) 10.135 +{ 10.136 + return elf->image + elf_uval(elf, shdr, sh_offset); 10.137 +} 10.138 + 10.139 +const void *elf_section_end(struct elf_binary *elf, const elf_shdr * shdr) 10.140 +{ 10.141 + return elf->image 10.142 + + elf_uval(elf, shdr, sh_offset) + elf_uval(elf, shdr, sh_size); 10.143 +} 10.144 + 10.145 +const elf_sym *elf_sym_by_name(struct elf_binary *elf, const char *symbol) 10.146 +{ 10.147 + const void *ptr = elf_section_start(elf, elf->sym_tab); 10.148 + const void *end = elf_section_end(elf, elf->sym_tab); 10.149 + const elf_sym *sym; 10.150 + uint64_t info, name; 10.151 + 10.152 + for (; ptr < end; ptr += elf_size(elf, sym)) 10.153 + { 10.154 + sym = ptr; 10.155 + info = elf_uval(elf, sym, st_info); 10.156 + name = elf_uval(elf, sym, st_name); 10.157 + if (ELF32_ST_BIND(info) != STB_GLOBAL) 10.158 + continue; 10.159 + if (strcmp(elf->sym_strtab + name, symbol) != 0) 10.160 + continue; 10.161 + return sym; 10.162 + } 10.163 + return NULL; 10.164 +} 10.165 + 10.166 +const elf_sym *elf_sym_by_index(struct elf_binary *elf, int index) 10.167 +{ 10.168 + const void *ptr = elf_section_start(elf, elf->sym_tab); 10.169 + const elf_sym *sym; 10.170 + 10.171 + sym = ptr + index * elf_size(elf, sym); 10.172 + return sym; 10.173 +} 10.174 + 10.175 +const char *elf_note_name(struct elf_binary *elf, const elf_note * note) 10.176 +{ 10.177 + return (void *)note + elf_size(elf, note); 10.178 +} 10.179 + 10.180 +const void *elf_note_desc(struct elf_binary *elf, const elf_note * note) 10.181 +{ 10.182 + int namesz = (elf_uval(elf, note, namesz) + 3) & ~3; 10.183 + 10.184 + return (void *)note + elf_size(elf, note) + namesz; 10.185 +} 10.186 + 10.187 +uint64_t elf_note_numeric(struct elf_binary *elf, const elf_note * note) 10.188 +{ 10.189 + const void *desc = elf_note_desc(elf, note); 10.190 + int descsz = elf_uval(elf, note, descsz); 10.191 + 10.192 + switch (descsz) 10.193 + { 10.194 + case 1: 10.195 + case 2: 10.196 + case 4: 10.197 + case 8: 10.198 + return elf_access_unsigned(elf, desc, 0, descsz); 10.199 + default: 10.200 + return 0; 10.201 + } 10.202 +} 10.203 +const elf_note *elf_note_next(struct elf_binary *elf, const elf_note * note) 10.204 +{ 10.205 + int namesz = (elf_uval(elf, note, namesz) + 3) & ~3; 10.206 + int descsz = (elf_uval(elf, note, descsz) + 3) & ~3; 10.207 + 10.208 + return (void *)note + elf_size(elf, note) + namesz + descsz; 10.209 +} 10.210 + 10.211 +/* ------------------------------------------------------------------------ */ 10.212 + 10.213 +int elf_is_elfbinary(const void *image) 10.214 +{ 10.215 + const Elf32_Ehdr *ehdr = image; 10.216 + 10.217 + if (IS_ELF(*ehdr)) 10.218 + return 1; 10.219 + return 0; 10.220 +} 10.221 + 10.222 +int elf_phdr_is_loadable(struct elf_binary *elf, const elf_phdr * phdr) 10.223 +{ 10.224 + uint64_t p_type = elf_uval(elf, phdr, p_type); 10.225 + uint64_t p_flags = elf_uval(elf, phdr, p_flags); 10.226 + 10.227 + return ((p_type == PT_LOAD) && (p_flags & (PF_W | PF_X)) != 0); 10.228 +}
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/xen/include/public/elfstructs.h Thu Jan 25 22:16:52 2007 +0000 11.3 @@ -0,0 +1,527 @@ 11.4 +#ifndef __XEN_PUBLIC_ELFSTRUCTS_H__ 11.5 +#define __XEN_PUBLIC_ELFSTRUCTS_H__ 1 11.6 +/* 11.7 + * Copyright (c) 1995, 1996 Erik Theisen. All rights reserved. 11.8 + * 11.9 + * Redistribution and use in source and binary forms, with or without 11.10 + * modification, are permitted provided that the following conditions 11.11 + * are met: 11.12 + * 1. Redistributions of source code must retain the above copyright 11.13 + * notice, this list of conditions and the following disclaimer. 11.14 + * 2. Redistributions in binary form must reproduce the above copyright 11.15 + * notice, this list of conditions and the following disclaimer in the 11.16 + * documentation and/or other materials provided with the distribution. 11.17 + * 3. The name of the author may not be used to endorse or promote products 11.18 + * derived from this software without specific prior written permission 11.19 + * 11.20 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 11.21 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 11.22 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 11.23 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 11.24 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 11.25 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 11.26 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 11.27 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 11.28 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 11.29 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 11.30 + */ 11.31 + 11.32 +typedef uint8_t Elf_Byte; 11.33 + 11.34 +typedef uint32_t Elf32_Addr; /* Unsigned program address */ 11.35 +typedef uint32_t Elf32_Off; /* Unsigned file offset */ 11.36 +typedef int32_t Elf32_Sword; /* Signed large integer */ 11.37 +typedef uint32_t Elf32_Word; /* Unsigned large integer */ 11.38 +typedef uint16_t Elf32_Half; /* Unsigned medium integer */ 11.39 + 11.40 +typedef uint64_t Elf64_Addr; 11.41 +typedef uint64_t Elf64_Off; 11.42 +typedef int32_t Elf64_Shalf; 11.43 + 11.44 +typedef int32_t Elf64_Sword; 11.45 +typedef uint32_t Elf64_Word; 11.46 + 11.47 +typedef int64_t Elf64_Sxword; 11.48 +typedef uint64_t Elf64_Xword; 11.49 + 11.50 +typedef uint32_t Elf64_Half; 11.51 +typedef uint16_t Elf64_Quarter; 11.52 + 11.53 +/* 11.54 + * e_ident[] identification indexes 11.55 + * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html 11.56 + */ 11.57 +#define EI_MAG0 0 /* file ID */ 11.58 +#define EI_MAG1 1 /* file ID */ 11.59 +#define EI_MAG2 2 /* file ID */ 11.60 +#define EI_MAG3 3 /* file ID */ 11.61 +#define EI_CLASS 4 /* file class */ 11.62 +#define EI_DATA 5 /* data encoding */ 11.63 +#define EI_VERSION 6 /* ELF header version */ 11.64 +#define EI_OSABI 7 /* OS/ABI ID */ 11.65 +#define EI_ABIVERSION 8 /* ABI version */ 11.66 +#define EI_PAD 9 /* start of pad bytes */ 11.67 +#define EI_NIDENT 16 /* Size of e_ident[] */ 11.68 + 11.69 +/* e_ident[] magic number */ 11.70 +#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */ 11.71 +#define ELFMAG1 'E' /* e_ident[EI_MAG1] */ 11.72 +#define ELFMAG2 'L' /* e_ident[EI_MAG2] */ 11.73 +#define ELFMAG3 'F' /* e_ident[EI_MAG3] */ 11.74 +#define ELFMAG "\177ELF" /* magic */ 11.75 +#define SELFMAG 4 /* size of magic */ 11.76 + 11.77 +/* e_ident[] file class */ 11.78 +#define ELFCLASSNONE 0 /* invalid */ 11.79 +#define ELFCLASS32 1 /* 32-bit objs */ 11.80 +#define ELFCLASS64 2 /* 64-bit objs */ 11.81 +#define ELFCLASSNUM 3 /* number of classes */ 11.82 + 11.83 +/* e_ident[] data encoding */ 11.84 +#define ELFDATANONE 0 /* invalid */ 11.85 +#define ELFDATA2LSB 1 /* Little-Endian */ 11.86 +#define ELFDATA2MSB 2 /* Big-Endian */ 11.87 +#define ELFDATANUM 3 /* number of data encode defines */ 11.88 + 11.89 +/* e_ident[] Operating System/ABI */ 11.90 +#define ELFOSABI_SYSV 0 /* UNIX System V ABI */ 11.91 +#define ELFOSABI_HPUX 1 /* HP-UX operating system */ 11.92 +#define ELFOSABI_NETBSD 2 /* NetBSD */ 11.93 +#define ELFOSABI_LINUX 3 /* GNU/Linux */ 11.94 +#define ELFOSABI_HURD 4 /* GNU/Hurd */ 11.95 +#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */ 11.96 +#define ELFOSABI_SOLARIS 6 /* Solaris */ 11.97 +#define ELFOSABI_MONTEREY 7 /* Monterey */ 11.98 +#define ELFOSABI_IRIX 8 /* IRIX */ 11.99 +#define ELFOSABI_FREEBSD 9 /* FreeBSD */ 11.100 +#define ELFOSABI_TRU64 10 /* TRU64 UNIX */ 11.101 +#define ELFOSABI_MODESTO 11 /* Novell Modesto */ 11.102 +#define ELFOSABI_OPENBSD 12 /* OpenBSD */ 11.103 +#define ELFOSABI_ARM 97 /* ARM */ 11.104 +#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ 11.105 + 11.106 +/* e_ident */ 11.107 +#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \ 11.108 + (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \ 11.109 + (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \ 11.110 + (ehdr).e_ident[EI_MAG3] == ELFMAG3) 11.111 + 11.112 +/* ELF Header */ 11.113 +typedef struct elfhdr { 11.114 + unsigned char e_ident[EI_NIDENT]; /* ELF Identification */ 11.115 + Elf32_Half e_type; /* object file type */ 11.116 + Elf32_Half e_machine; /* machine */ 11.117 + Elf32_Word e_version; /* object file version */ 11.118 + Elf32_Addr e_entry; /* virtual entry point */ 11.119 + Elf32_Off e_phoff; /* program header table offset */ 11.120 + Elf32_Off e_shoff; /* section header table offset */ 11.121 + Elf32_Word e_flags; /* processor-specific flags */ 11.122 + Elf32_Half e_ehsize; /* ELF header size */ 11.123 + Elf32_Half e_phentsize; /* program header entry size */ 11.124 + Elf32_Half e_phnum; /* number of program header entries */ 11.125 + Elf32_Half e_shentsize; /* section header entry size */ 11.126 + Elf32_Half e_shnum; /* number of section header entries */ 11.127 + Elf32_Half e_shstrndx; /* section header table's "section 11.128 + header string table" entry offset */ 11.129 +} Elf32_Ehdr; 11.130 + 11.131 +typedef struct { 11.132 + unsigned char e_ident[EI_NIDENT]; /* Id bytes */ 11.133 + Elf64_Quarter e_type; /* file type */ 11.134 + Elf64_Quarter e_machine; /* machine type */ 11.135 + Elf64_Half e_version; /* version number */ 11.136 + Elf64_Addr e_entry; /* entry point */ 11.137 + Elf64_Off e_phoff; /* Program hdr offset */ 11.138 + Elf64_Off e_shoff; /* Section hdr offset */ 11.139 + Elf64_Half e_flags; /* Processor flags */ 11.140 + Elf64_Quarter e_ehsize; /* sizeof ehdr */ 11.141 + Elf64_Quarter e_phentsize; /* Program header entry size */ 11.142 + Elf64_Quarter e_phnum; /* Number of program headers */ 11.143 + Elf64_Quarter e_shentsize; /* Section header entry size */ 11.144 + Elf64_Quarter e_shnum; /* Number of section headers */ 11.145 + Elf64_Quarter e_shstrndx; /* String table index */ 11.146 +} Elf64_Ehdr; 11.147 + 11.148 +/* e_type */ 11.149 +#define ET_NONE 0 /* No file type */ 11.150 +#define ET_REL 1 /* relocatable file */ 11.151 +#define ET_EXEC 2 /* executable file */ 11.152 +#define ET_DYN 3 /* shared object file */ 11.153 +#define ET_CORE 4 /* core file */ 11.154 +#define ET_NUM 5 /* number of types */ 11.155 +#define ET_LOPROC 0xff00 /* reserved range for processor */ 11.156 +#define ET_HIPROC 0xffff /* specific e_type */ 11.157 + 11.158 +/* e_machine */ 11.159 +#define EM_NONE 0 /* No Machine */ 11.160 +#define EM_M32 1 /* AT&T WE 32100 */ 11.161 +#define EM_SPARC 2 /* SPARC */ 11.162 +#define EM_386 3 /* Intel 80386 */ 11.163 +#define EM_68K 4 /* Motorola 68000 */ 11.164 +#define EM_88K 5 /* Motorola 88000 */ 11.165 +#define EM_486 6 /* Intel 80486 - unused? */ 11.166 +#define EM_860 7 /* Intel 80860 */ 11.167 +#define EM_MIPS 8 /* MIPS R3000 Big-Endian only */ 11.168 +/* 11.169 + * Don't know if EM_MIPS_RS4_BE, 11.170 + * EM_SPARC64, EM_PARISC, 11.171 + * or EM_PPC are ABI compliant 11.172 + */ 11.173 +#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */ 11.174 +#define EM_SPARC64 11 /* SPARC v9 64-bit unoffical */ 11.175 +#define EM_PARISC 15 /* HPPA */ 11.176 +#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */ 11.177 +#define EM_PPC 20 /* PowerPC */ 11.178 +#define EM_PPC64 21 /* PowerPC 64-bit */ 11.179 +#define EM_ARM 40 /* Advanced RISC Machines ARM */ 11.180 +#define EM_ALPHA 41 /* DEC ALPHA */ 11.181 +#define EM_SPARCV9 43 /* SPARC version 9 */ 11.182 +#define EM_ALPHA_EXP 0x9026 /* DEC ALPHA */ 11.183 +#define EM_IA_64 50 /* Intel Merced */ 11.184 +#define EM_X86_64 62 /* AMD x86-64 architecture */ 11.185 +#define EM_VAX 75 /* DEC VAX */ 11.186 + 11.187 +/* Version */ 11.188 +#define EV_NONE 0 /* Invalid */ 11.189 +#define EV_CURRENT 1 /* Current */ 11.190 +#define EV_NUM 2 /* number of versions */ 11.191 + 11.192 +/* Section Header */ 11.193 +typedef struct { 11.194 + Elf32_Word sh_name; /* name - index into section header 11.195 + string table section */ 11.196 + Elf32_Word sh_type; /* type */ 11.197 + Elf32_Word sh_flags; /* flags */ 11.198 + Elf32_Addr sh_addr; /* address */ 11.199 + Elf32_Off sh_offset; /* file offset */ 11.200 + Elf32_Word sh_size; /* section size */ 11.201 + Elf32_Word sh_link; /* section header table index link */ 11.202 + Elf32_Word sh_info; /* extra information */ 11.203 + Elf32_Word sh_addralign; /* address alignment */ 11.204 + Elf32_Word sh_entsize; /* section entry size */ 11.205 +} Elf32_Shdr; 11.206 + 11.207 +typedef struct { 11.208 + Elf64_Half sh_name; /* section name */ 11.209 + Elf64_Half sh_type; /* section type */ 11.210 + Elf64_Xword sh_flags; /* section flags */ 11.211 + Elf64_Addr sh_addr; /* virtual address */ 11.212 + Elf64_Off sh_offset; /* file offset */ 11.213 + Elf64_Xword sh_size; /* section size */ 11.214 + Elf64_Half sh_link; /* link to another */ 11.215 + Elf64_Half sh_info; /* misc info */ 11.216 + Elf64_Xword sh_addralign; /* memory alignment */ 11.217 + Elf64_Xword sh_entsize; /* table entry size */ 11.218 +} Elf64_Shdr; 11.219 + 11.220 +/* Special Section Indexes */ 11.221 +#define SHN_UNDEF 0 /* undefined */ 11.222 +#define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */ 11.223 +#define SHN_LOPROC 0xff00 /* reserved range for processor */ 11.224 +#define SHN_HIPROC 0xff1f /* specific section indexes */ 11.225 +#define SHN_ABS 0xfff1 /* absolute value */ 11.226 +#define SHN_COMMON 0xfff2 /* common symbol */ 11.227 +#define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */ 11.228 + 11.229 +/* sh_type */ 11.230 +#define SHT_NULL 0 /* inactive */ 11.231 +#define SHT_PROGBITS 1 /* program defined information */ 11.232 +#define SHT_SYMTAB 2 /* symbol table section */ 11.233 +#define SHT_STRTAB 3 /* string table section */ 11.234 +#define SHT_RELA 4 /* relocation section with addends*/ 11.235 +#define SHT_HASH 5 /* symbol hash table section */ 11.236 +#define SHT_DYNAMIC 6 /* dynamic section */ 11.237 +#define SHT_NOTE 7 /* note section */ 11.238 +#define SHT_NOBITS 8 /* no space section */ 11.239 +#define SHT_REL 9 /* relation section without addends */ 11.240 +#define SHT_SHLIB 10 /* reserved - purpose unknown */ 11.241 +#define SHT_DYNSYM 11 /* dynamic symbol table section */ 11.242 +#define SHT_NUM 12 /* number of section types */ 11.243 +#define SHT_LOPROC 0x70000000 /* reserved range for processor */ 11.244 +#define SHT_HIPROC 0x7fffffff /* specific section header types */ 11.245 +#define SHT_LOUSER 0x80000000 /* reserved range for application */ 11.246 +#define SHT_HIUSER 0xffffffff /* specific indexes */ 11.247 + 11.248 +/* Section names */ 11.249 +#define ELF_BSS ".bss" /* uninitialized data */ 11.250 +#define ELF_DATA ".data" /* initialized data */ 11.251 +#define ELF_DEBUG ".debug" /* debug */ 11.252 +#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */ 11.253 +#define ELF_DYNSTR ".dynstr" /* dynamic string table */ 11.254 +#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */ 11.255 +#define ELF_FINI ".fini" /* termination code */ 11.256 +#define ELF_GOT ".got" /* global offset table */ 11.257 +#define ELF_HASH ".hash" /* symbol hash table */ 11.258 +#define ELF_INIT ".init" /* initialization code */ 11.259 +#define ELF_REL_DATA ".rel.data" /* relocation data */ 11.260 +#define ELF_REL_FINI ".rel.fini" /* relocation termination code */ 11.261 +#define ELF_REL_INIT ".rel.init" /* relocation initialization code */ 11.262 +#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */ 11.263 +#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */ 11.264 +#define ELF_REL_TEXT ".rel.text" /* relocation code */ 11.265 +#define ELF_RODATA ".rodata" /* read-only data */ 11.266 +#define ELF_SHSTRTAB ".shstrtab" /* section header string table */ 11.267 +#define ELF_STRTAB ".strtab" /* string table */ 11.268 +#define ELF_SYMTAB ".symtab" /* symbol table */ 11.269 +#define ELF_TEXT ".text" /* code */ 11.270 + 11.271 + 11.272 +/* Section Attribute Flags - sh_flags */ 11.273 +#define SHF_WRITE 0x1 /* Writable */ 11.274 +#define SHF_ALLOC 0x2 /* occupies memory */ 11.275 +#define SHF_EXECINSTR 0x4 /* executable */ 11.276 +#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */ 11.277 + /* specific section attributes */ 11.278 + 11.279 +/* Symbol Table Entry */ 11.280 +typedef struct elf32_sym { 11.281 + Elf32_Word st_name; /* name - index into string table */ 11.282 + Elf32_Addr st_value; /* symbol value */ 11.283 + Elf32_Word st_size; /* symbol size */ 11.284 + unsigned char st_info; /* type and binding */ 11.285 + unsigned char st_other; /* 0 - no defined meaning */ 11.286 + Elf32_Half st_shndx; /* section header index */ 11.287 +} Elf32_Sym; 11.288 + 11.289 +typedef struct { 11.290 + Elf64_Half st_name; /* Symbol name index in str table */ 11.291 + Elf_Byte st_info; /* type / binding attrs */ 11.292 + Elf_Byte st_other; /* unused */ 11.293 + Elf64_Quarter st_shndx; /* section index of symbol */ 11.294 + Elf64_Xword st_value; /* value of symbol */ 11.295 + Elf64_Xword st_size; /* size of symbol */ 11.296 +} Elf64_Sym; 11.297 + 11.298 +/* Symbol table index */ 11.299 +#define STN_UNDEF 0 /* undefined */ 11.300 + 11.301 +/* Extract symbol info - st_info */ 11.302 +#define ELF32_ST_BIND(x) ((x) >> 4) 11.303 +#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf) 11.304 +#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) 11.305 + 11.306 +#define ELF64_ST_BIND(x) ((x) >> 4) 11.307 +#define ELF64_ST_TYPE(x) (((unsigned int) x) & 0xf) 11.308 +#define ELF64_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) 11.309 + 11.310 +/* Symbol Binding - ELF32_ST_BIND - st_info */ 11.311 +#define STB_LOCAL 0 /* Local symbol */ 11.312 +#define STB_GLOBAL 1 /* Global symbol */ 11.313 +#define STB_WEAK 2 /* like global - lower precedence */ 11.314 +#define STB_NUM 3 /* number of symbol bindings */ 11.315 +#define STB_LOPROC 13 /* reserved range for processor */ 11.316 +#define STB_HIPROC 15 /* specific symbol bindings */ 11.317 + 11.318 +/* Symbol type - ELF32_ST_TYPE - st_info */ 11.319 +#define STT_NOTYPE 0 /* not specified */ 11.320 +#define STT_OBJECT 1 /* data object */ 11.321 +#define STT_FUNC 2 /* function */ 11.322 +#define STT_SECTION 3 /* section */ 11.323 +#define STT_FILE 4 /* file */ 11.324 +#define STT_NUM 5 /* number of symbol types */ 11.325 +#define STT_LOPROC 13 /* reserved range for processor */ 11.326 +#define STT_HIPROC 15 /* specific symbol types */ 11.327 + 11.328 +/* Relocation entry with implicit addend */ 11.329 +typedef struct { 11.330 + Elf32_Addr r_offset; /* offset of relocation */ 11.331 + Elf32_Word r_info; /* symbol table index and type */ 11.332 +} Elf32_Rel; 11.333 + 11.334 +/* Relocation entry with explicit addend */ 11.335 +typedef struct { 11.336 + Elf32_Addr r_offset; /* offset of relocation */ 11.337 + Elf32_Word r_info; /* symbol table index and type */ 11.338 + Elf32_Sword r_addend; 11.339 +} Elf32_Rela; 11.340 + 11.341 +/* Extract relocation info - r_info */ 11.342 +#define ELF32_R_SYM(i) ((i) >> 8) 11.343 +#define ELF32_R_TYPE(i) ((unsigned char) (i)) 11.344 +#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t)) 11.345 + 11.346 +typedef struct { 11.347 + Elf64_Xword r_offset; /* where to do it */ 11.348 + Elf64_Xword r_info; /* index & type of relocation */ 11.349 +} Elf64_Rel; 11.350 + 11.351 +typedef struct { 11.352 + Elf64_Xword r_offset; /* where to do it */ 11.353 + Elf64_Xword r_info; /* index & type of relocation */ 11.354 + Elf64_Sxword r_addend; /* adjustment value */ 11.355 +} Elf64_Rela; 11.356 + 11.357 +#define ELF64_R_SYM(info) ((info) >> 32) 11.358 +#define ELF64_R_TYPE(info) ((info) & 0xFFFFFFFF) 11.359 +#define ELF64_R_INFO(s,t) (((s) << 32) + (u_int32_t)(t)) 11.360 + 11.361 +/* Program Header */ 11.362 +typedef struct { 11.363 + Elf32_Word p_type; /* segment type */ 11.364 + Elf32_Off p_offset; /* segment offset */ 11.365 + Elf32_Addr p_vaddr; /* virtual address of segment */ 11.366 + Elf32_Addr p_paddr; /* physical address - ignored? */ 11.367 + Elf32_Word p_filesz; /* number of bytes in file for seg. */ 11.368 + Elf32_Word p_memsz; /* number of bytes in mem. for seg. */ 11.369 + Elf32_Word p_flags; /* flags */ 11.370 + Elf32_Word p_align; /* memory alignment */ 11.371 +} Elf32_Phdr; 11.372 + 11.373 +typedef struct { 11.374 + Elf64_Half p_type; /* entry type */ 11.375 + Elf64_Half p_flags; /* flags */ 11.376 + Elf64_Off p_offset; /* offset */ 11.377 + Elf64_Addr p_vaddr; /* virtual address */ 11.378 + Elf64_Addr p_paddr; /* physical address */ 11.379 + Elf64_Xword p_filesz; /* file size */ 11.380 + Elf64_Xword p_memsz; /* memory size */ 11.381 + Elf64_Xword p_align; /* memory & file alignment */ 11.382 +} Elf64_Phdr; 11.383 + 11.384 +/* Segment types - p_type */ 11.385 +#define PT_NULL 0 /* unused */ 11.386 +#define PT_LOAD 1 /* loadable segment */ 11.387 +#define PT_DYNAMIC 2 /* dynamic linking section */ 11.388 +#define PT_INTERP 3 /* the RTLD */ 11.389 +#define PT_NOTE 4 /* auxiliary information */ 11.390 +#define PT_SHLIB 5 /* reserved - purpose undefined */ 11.391 +#define PT_PHDR 6 /* program header */ 11.392 +#define PT_NUM 7 /* Number of segment types */ 11.393 +#define PT_LOPROC 0x70000000 /* reserved range for processor */ 11.394 +#define PT_HIPROC 0x7fffffff /* specific segment types */ 11.395 + 11.396 +/* Segment flags - p_flags */ 11.397 +#define PF_X 0x1 /* Executable */ 11.398 +#define PF_W 0x2 /* Writable */ 11.399 +#define PF_R 0x4 /* Readable */ 11.400 +#define PF_MASKPROC 0xf0000000 /* reserved bits for processor */ 11.401 + /* specific segment flags */ 11.402 + 11.403 +/* Dynamic structure */ 11.404 +typedef struct { 11.405 + Elf32_Sword d_tag; /* controls meaning of d_val */ 11.406 + union { 11.407 + Elf32_Word d_val; /* Multiple meanings - see d_tag */ 11.408 + Elf32_Addr d_ptr; /* program virtual address */ 11.409 + } d_un; 11.410 +} Elf32_Dyn; 11.411 + 11.412 +typedef struct { 11.413 + Elf64_Xword d_tag; /* controls meaning of d_val */ 11.414 + union { 11.415 + Elf64_Addr d_ptr; 11.416 + Elf64_Xword d_val; 11.417 + } d_un; 11.418 +} Elf64_Dyn; 11.419 + 11.420 +/* Dynamic Array Tags - d_tag */ 11.421 +#define DT_NULL 0 /* marks end of _DYNAMIC array */ 11.422 +#define DT_NEEDED 1 /* string table offset of needed lib */ 11.423 +#define DT_PLTRELSZ 2 /* size of relocation entries in PLT */ 11.424 +#define DT_PLTGOT 3 /* address PLT/GOT */ 11.425 +#define DT_HASH 4 /* address of symbol hash table */ 11.426 +#define DT_STRTAB 5 /* address of string table */ 11.427 +#define DT_SYMTAB 6 /* address of symbol table */ 11.428 +#define DT_RELA 7 /* address of relocation table */ 11.429 +#define DT_RELASZ 8 /* size of relocation table */ 11.430 +#define DT_RELAENT 9 /* size of relocation entry */ 11.431 +#define DT_STRSZ 10 /* size of string table */ 11.432 +#define DT_SYMENT 11 /* size of symbol table entry */ 11.433 +#define DT_INIT 12 /* address of initialization func. */ 11.434 +#define DT_FINI 13 /* address of termination function */ 11.435 +#define DT_SONAME 14 /* string table offset of shared obj */ 11.436 +#define DT_RPATH 15 /* string table offset of library 11.437 + search path */ 11.438 +#define DT_SYMBOLIC 16 /* start sym search in shared obj. */ 11.439 +#define DT_REL 17 /* address of rel. tbl. w addends */ 11.440 +#define DT_RELSZ 18 /* size of DT_REL relocation table */ 11.441 +#define DT_RELENT 19 /* size of DT_REL relocation entry */ 11.442 +#define DT_PLTREL 20 /* PLT referenced relocation entry */ 11.443 +#define DT_DEBUG 21 /* bugger */ 11.444 +#define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */ 11.445 +#define DT_JMPREL 23 /* add. of PLT's relocation entries */ 11.446 +#define DT_BIND_NOW 24 /* Bind now regardless of env setting */ 11.447 +#define DT_NUM 25 /* Number used. */ 11.448 +#define DT_LOPROC 0x70000000 /* reserved range for processor */ 11.449 +#define DT_HIPROC 0x7fffffff /* specific dynamic array tags */ 11.450 + 11.451 +/* Standard ELF hashing function */ 11.452 +unsigned int elf_hash(const unsigned char *name); 11.453 + 11.454 +/* 11.455 + * Note Definitions 11.456 + */ 11.457 +typedef struct { 11.458 + Elf32_Word namesz; 11.459 + Elf32_Word descsz; 11.460 + Elf32_Word type; 11.461 +} Elf32_Note; 11.462 + 11.463 +typedef struct { 11.464 + Elf64_Half namesz; 11.465 + Elf64_Half descsz; 11.466 + Elf64_Half type; 11.467 +} Elf64_Note; 11.468 + 11.469 + 11.470 +#if defined(ELFSIZE) 11.471 +#define CONCAT(x,y) __CONCAT(x,y) 11.472 +#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x))) 11.473 +#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y)))) 11.474 +#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE)) 11.475 +#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x))) 11.476 +#endif 11.477 + 11.478 +#if defined(ELFSIZE) && (ELFSIZE == 32) 11.479 +#define Elf_Ehdr Elf32_Ehdr 11.480 +#define Elf_Phdr Elf32_Phdr 11.481 +#define Elf_Shdr Elf32_Shdr 11.482 +#define Elf_Sym Elf32_Sym 11.483 +#define Elf_Rel Elf32_Rel 11.484 +#define Elf_RelA Elf32_Rela 11.485 +#define Elf_Dyn Elf32_Dyn 11.486 +#define Elf_Word Elf32_Word 11.487 +#define Elf_Sword Elf32_Sword 11.488 +#define Elf_Addr Elf32_Addr 11.489 +#define Elf_Off Elf32_Off 11.490 +#define Elf_Nhdr Elf32_Nhdr 11.491 +#define Elf_Note Elf32_Note 11.492 + 11.493 +#define ELF_R_SYM ELF32_R_SYM 11.494 +#define ELF_R_TYPE ELF32_R_TYPE 11.495 +#define ELF_R_INFO ELF32_R_INFO 11.496 +#define ELFCLASS ELFCLASS32 11.497 + 11.498 +#define ELF_ST_BIND ELF32_ST_BIND 11.499 +#define ELF_ST_TYPE ELF32_ST_TYPE 11.500 +#define ELF_ST_INFO ELF32_ST_INFO 11.501 + 11.502 +#define AuxInfo Aux32Info 11.503 +#elif defined(ELFSIZE) && (ELFSIZE == 64) 11.504 +#define Elf_Ehdr Elf64_Ehdr 11.505 +#define Elf_Phdr Elf64_Phdr 11.506 +#define Elf_Shdr Elf64_Shdr 11.507 +#define Elf_Sym Elf64_Sym 11.508 +#define Elf_Rel Elf64_Rel 11.509 +#define Elf_RelA Elf64_Rela 11.510 +#define Elf_Dyn Elf64_Dyn 11.511 +#define Elf_Word Elf64_Word 11.512 +#define Elf_Sword Elf64_Sword 11.513 +#define Elf_Addr Elf64_Addr 11.514 +#define Elf_Off Elf64_Off 11.515 +#define Elf_Nhdr Elf64_Nhdr 11.516 +#define Elf_Note Elf64_Note 11.517 + 11.518 +#define ELF_R_SYM ELF64_R_SYM 11.519 +#define ELF_R_TYPE ELF64_R_TYPE 11.520 +#define ELF_R_INFO ELF64_R_INFO 11.521 +#define ELFCLASS ELFCLASS64 11.522 + 11.523 +#define ELF_ST_BIND ELF64_ST_BIND 11.524 +#define ELF_ST_TYPE ELF64_ST_TYPE 11.525 +#define ELF_ST_INFO ELF64_ST_INFO 11.526 + 11.527 +#define AuxInfo Aux64Info 11.528 +#endif 11.529 + 11.530 +#endif /* __XEN_PUBLIC_ELFSTRUCTS_H__ */
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/xen/include/public/libelf.h Thu Jan 25 22:16:52 2007 +0000 12.3 @@ -0,0 +1,238 @@ 12.4 +#ifndef __XC_LIBELF__ 12.5 +#define __XC_LIBELF__ 1 12.6 + 12.7 +#ifdef __XEN__ 12.8 + 12.9 +#include <xen/inttypes.h> 12.10 +#include <public/features.h> 12.11 +typedef uint64_t off_t; 12.12 + 12.13 +#define LITTLE_ENDIAN 1234 12.14 +#define BIG_ENDIAN 4321 12.15 +#if defined(__i386__) || defined(__x86_64) || defined(__ia64__) 12.16 +#define BYTE_ORDER LITTLE_ENDIAN 12.17 +#elif defined(__powerpc__) 12.18 +#define BYTE_ORDER BIG_ENDIAN 12.19 +#endif 12.20 + 12.21 +#else /* !__XEN__ */ 12.22 + 12.23 +#include <stddef.h> 12.24 +#include <inttypes.h> 12.25 + 12.26 +#endif 12.27 + 12.28 +#undef ELFSIZE 12.29 +#include "elfstructs.h" 12.30 +#include "features.h" 12.31 + 12.32 +/* ------------------------------------------------------------------------ */ 12.33 + 12.34 +typedef union { 12.35 + Elf32_Ehdr e32; 12.36 + Elf64_Ehdr e64; 12.37 +} elf_ehdr; 12.38 + 12.39 +typedef union { 12.40 + Elf32_Phdr e32; 12.41 + Elf64_Phdr e64; 12.42 +} elf_phdr; 12.43 + 12.44 +typedef union { 12.45 + Elf32_Shdr e32; 12.46 + Elf64_Shdr e64; 12.47 +} elf_shdr; 12.48 + 12.49 +typedef union { 12.50 + Elf32_Sym e32; 12.51 + Elf64_Sym e64; 12.52 +} elf_sym; 12.53 + 12.54 +typedef union { 12.55 + Elf32_Rel e32; 12.56 + Elf64_Rel e64; 12.57 +} elf_rel; 12.58 + 12.59 +typedef union { 12.60 + Elf32_Rela e32; 12.61 + Elf64_Rela e64; 12.62 +} elf_rela; 12.63 + 12.64 +typedef union { 12.65 + Elf32_Note e32; 12.66 + Elf64_Note e64; 12.67 +} elf_note; 12.68 + 12.69 +struct elf_binary { 12.70 + /* elf binary */ 12.71 + const char *image; 12.72 + size_t size; 12.73 + char class; 12.74 + char data; 12.75 + 12.76 + const elf_ehdr *ehdr; 12.77 + const char *sec_strtab; 12.78 + const elf_shdr *sym_tab; 12.79 + const char *sym_strtab; 12.80 + 12.81 + /* loaded to */ 12.82 + char *dest; 12.83 + uint64_t pstart; 12.84 + uint64_t pend; 12.85 + uint64_t reloc_offset; 12.86 + 12.87 +#ifndef __XEN__ 12.88 + /* misc */ 12.89 + FILE *log; 12.90 +#endif 12.91 + int verbose; 12.92 +}; 12.93 + 12.94 +/* ------------------------------------------------------------------------ */ 12.95 +/* accessing elf header fields */ 12.96 + 12.97 +#if !defined(BYTE_ORDER) 12.98 +# error BYTE_ORDER not defined 12.99 +#elif BYTE_ORDER == BIG_ENDIAN 12.100 +# define NATIVE_ELFDATA ELFDATA2MSB 12.101 +#elif BYTE_ORDER == LITTLE_ENDIAN 12.102 +# define NATIVE_ELFDATA ELFDATA2LSB 12.103 +#else 12.104 +# error BYTE_ORDER unknown 12.105 +#endif 12.106 + 12.107 +#define elf_32bit(elf) (ELFCLASS32 == (elf)->class) 12.108 +#define elf_64bit(elf) (ELFCLASS64 == (elf)->class) 12.109 +#define elf_msb(elf) (ELFDATA2MSB == (elf)->data) 12.110 +#define elf_lsb(elf) (ELFDATA2LSB == (elf)->data) 12.111 +#define elf_swap(elf) (NATIVE_ELFDATA != (elf)->data) 12.112 + 12.113 +#define elf_uval(elf, str, elem) \ 12.114 + ((ELFCLASS64 == (elf)->class) \ 12.115 + ? elf_access_unsigned((elf), (str), \ 12.116 + offsetof(typeof(*(str)),e64.elem), \ 12.117 + sizeof((str)->e64.elem)) \ 12.118 + : elf_access_unsigned((elf), (str), \ 12.119 + offsetof(typeof(*(str)),e32.elem), \ 12.120 + sizeof((str)->e32.elem))) 12.121 + 12.122 +#define elf_sval(elf, str, elem) \ 12.123 + ((ELFCLASS64 == (elf)->class) \ 12.124 + ? elf_access_signed((elf), (str), \ 12.125 + offsetof(typeof(*(str)),e64.elem), \ 12.126 + sizeof((str)->e64.elem)) \ 12.127 + : elf_access_signed((elf), (str), \ 12.128 + offsetof(typeof(*(str)),e32.elem), \ 12.129 + sizeof((str)->e32.elem))) 12.130 + 12.131 +#define elf_size(elf, str) \ 12.132 + ((ELFCLASS64 == (elf)->class) \ 12.133 + ? sizeof((str)->e64) \ 12.134 + : sizeof((str)->e32)) 12.135 + 12.136 +uint64_t elf_access_unsigned(struct elf_binary *elf, const void *ptr, 12.137 + off_t offset, size_t size); 12.138 +int64_t elf_access_signed(struct elf_binary *elf, const void *ptr, 12.139 + off_t offset, size_t size); 12.140 + 12.141 +uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr); 12.142 + 12.143 +/* ------------------------------------------------------------------------ */ 12.144 +/* xc_libelf_tools.c */ 12.145 + 12.146 +int elf_shdr_count(struct elf_binary *elf); 12.147 +int elf_phdr_count(struct elf_binary *elf); 12.148 + 12.149 +const elf_shdr *elf_shdr_by_name(struct elf_binary *elf, const char *name); 12.150 +const elf_shdr *elf_shdr_by_index(struct elf_binary *elf, int index); 12.151 +const elf_phdr *elf_phdr_by_index(struct elf_binary *elf, int index); 12.152 + 12.153 +const char *elf_section_name(struct elf_binary *elf, const elf_shdr * shdr); 12.154 +const void *elf_section_start(struct elf_binary *elf, const elf_shdr * shdr); 12.155 +const void *elf_section_end(struct elf_binary *elf, const elf_shdr * shdr); 12.156 + 12.157 +const elf_sym *elf_sym_by_name(struct elf_binary *elf, const char *symbol); 12.158 +const elf_sym *elf_sym_by_index(struct elf_binary *elf, int index); 12.159 + 12.160 +const char *elf_note_name(struct elf_binary *elf, const elf_note * note); 12.161 +const void *elf_note_desc(struct elf_binary *elf, const elf_note * note); 12.162 +uint64_t elf_note_numeric(struct elf_binary *elf, const elf_note * note); 12.163 +const elf_note *elf_note_next(struct elf_binary *elf, const elf_note * note); 12.164 + 12.165 +int elf_is_elfbinary(const void *image); 12.166 +int elf_phdr_is_loadable(struct elf_binary *elf, const elf_phdr * phdr); 12.167 + 12.168 +/* ------------------------------------------------------------------------ */ 12.169 +/* xc_libelf_loader.c */ 12.170 + 12.171 +int elf_init(struct elf_binary *elf, const char *image, size_t size); 12.172 +#ifdef __XEN__ 12.173 +void elf_set_verbose(struct elf_binary *elf); 12.174 +#else 12.175 +void elf_set_logfile(struct elf_binary *elf, FILE * log, int verbose); 12.176 +#endif 12.177 + 12.178 +void elf_parse_binary(struct elf_binary *elf); 12.179 +void elf_load_binary(struct elf_binary *elf); 12.180 + 12.181 +void *elf_get_ptr(struct elf_binary *elf, unsigned long addr); 12.182 +uint64_t elf_lookup_addr(struct elf_binary *elf, const char *symbol); 12.183 + 12.184 +/* ------------------------------------------------------------------------ */ 12.185 +/* xc_libelf_relocate.c */ 12.186 + 12.187 +int elf_reloc(struct elf_binary *elf); 12.188 + 12.189 +/* ------------------------------------------------------------------------ */ 12.190 +/* xc_libelf_dominfo.c */ 12.191 + 12.192 +#define UNSET_ADDR ((uint64_t)-1) 12.193 + 12.194 +struct elf_dom_parms { 12.195 + /* raw */ 12.196 + const char *guest_info; 12.197 + const void *elf_note_start; 12.198 + const void *elf_note_end; 12.199 + 12.200 + /* parsed */ 12.201 + char guest_os[16]; 12.202 + char guest_ver[16]; 12.203 + char xen_ver[16]; 12.204 + char loader[16]; 12.205 + int pae; 12.206 + int bsd_symtab; 12.207 + uint64_t virt_base; 12.208 + uint64_t virt_entry; 12.209 + uint64_t virt_hypercall; 12.210 + uint64_t virt_hv_start_low; 12.211 + uint64_t elf_paddr_offset; 12.212 + uint32_t f_supported[XENFEAT_NR_SUBMAPS]; 12.213 + uint32_t f_required[XENFEAT_NR_SUBMAPS]; 12.214 + 12.215 + /* calculated */ 12.216 + uint64_t virt_offset; 12.217 + uint64_t virt_kstart; 12.218 + uint64_t virt_kend; 12.219 +}; 12.220 + 12.221 +static inline void elf_xen_feature_set(int nr, uint32_t * addr) 12.222 +{ 12.223 + addr[nr >> 5] |= 1 << (nr & 31); 12.224 +} 12.225 +static inline int elf_xen_feature_get(int nr, uint32_t * addr) 12.226 +{ 12.227 + return !!(addr[nr >> 5] & (1 << (nr & 31))); 12.228 +} 12.229 + 12.230 +int elf_xen_parse_features(const char *features, 12.231 + uint32_t *supported, 12.232 + uint32_t *required); 12.233 +int elf_xen_parse_note(struct elf_binary *elf, 12.234 + struct elf_dom_parms *parms, 12.235 + const elf_note *note); 12.236 +int elf_xen_parse_guest_info(struct elf_binary *elf, 12.237 + struct elf_dom_parms *parms); 12.238 +int elf_xen_parse(struct elf_binary *elf, 12.239 + struct elf_dom_parms *parms); 12.240 + 12.241 +#endif /* __XC_LIBELF__ */
13.1 --- a/xen/include/xen/elf.h Thu Jan 25 18:54:25 2007 +0000 13.2 +++ b/xen/include/xen/elf.h Thu Jan 25 22:16:52 2007 +0000 13.3 @@ -27,495 +27,7 @@ 13.4 #ifndef __XEN_ELF_H__ 13.5 #define __XEN_ELF_H__ 13.6 13.7 -typedef u8 Elf_Byte; 13.8 - 13.9 -typedef u32 Elf32_Addr; /* Unsigned program address */ 13.10 -typedef u32 Elf32_Off; /* Unsigned file offset */ 13.11 -typedef s32 Elf32_Sword; /* Signed large integer */ 13.12 -typedef u32 Elf32_Word; /* Unsigned large integer */ 13.13 -typedef u16 Elf32_Half; /* Unsigned medium integer */ 13.14 - 13.15 -typedef u64 Elf64_Addr; 13.16 -typedef u64 Elf64_Off; 13.17 -typedef s32 Elf64_Shalf; 13.18 - 13.19 -typedef s32 Elf64_Sword; 13.20 -typedef u32 Elf64_Word; 13.21 - 13.22 -typedef s64 Elf64_Sxword; 13.23 -typedef u64 Elf64_Xword; 13.24 - 13.25 -typedef u32 Elf64_Half; 13.26 -typedef u16 Elf64_Quarter; 13.27 - 13.28 -/* 13.29 - * e_ident[] identification indexes 13.30 - * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html 13.31 - */ 13.32 -#define EI_MAG0 0 /* file ID */ 13.33 -#define EI_MAG1 1 /* file ID */ 13.34 -#define EI_MAG2 2 /* file ID */ 13.35 -#define EI_MAG3 3 /* file ID */ 13.36 -#define EI_CLASS 4 /* file class */ 13.37 -#define EI_DATA 5 /* data encoding */ 13.38 -#define EI_VERSION 6 /* ELF header version */ 13.39 -#define EI_OSABI 7 /* OS/ABI ID */ 13.40 -#define EI_ABIVERSION 8 /* ABI version */ 13.41 -#define EI_PAD 9 /* start of pad bytes */ 13.42 -#define EI_NIDENT 16 /* Size of e_ident[] */ 13.43 - 13.44 -/* e_ident[] magic number */ 13.45 -#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */ 13.46 -#define ELFMAG1 'E' /* e_ident[EI_MAG1] */ 13.47 -#define ELFMAG2 'L' /* e_ident[EI_MAG2] */ 13.48 -#define ELFMAG3 'F' /* e_ident[EI_MAG3] */ 13.49 -#define ELFMAG "\177ELF" /* magic */ 13.50 -#define SELFMAG 4 /* size of magic */ 13.51 - 13.52 -/* e_ident[] file class */ 13.53 -#define ELFCLASSNONE 0 /* invalid */ 13.54 -#define ELFCLASS32 1 /* 32-bit objs */ 13.55 -#define ELFCLASS64 2 /* 64-bit objs */ 13.56 -#define ELFCLASSNUM 3 /* number of classes */ 13.57 - 13.58 -/* e_ident[] data encoding */ 13.59 -#define ELFDATANONE 0 /* invalid */ 13.60 -#define ELFDATA2LSB 1 /* Little-Endian */ 13.61 -#define ELFDATA2MSB 2 /* Big-Endian */ 13.62 -#define ELFDATANUM 3 /* number of data encode defines */ 13.63 - 13.64 -/* e_ident[] Operating System/ABI */ 13.65 -#define ELFOSABI_SYSV 0 /* UNIX System V ABI */ 13.66 -#define ELFOSABI_HPUX 1 /* HP-UX operating system */ 13.67 -#define ELFOSABI_NETBSD 2 /* NetBSD */ 13.68 -#define ELFOSABI_LINUX 3 /* GNU/Linux */ 13.69 -#define ELFOSABI_HURD 4 /* GNU/Hurd */ 13.70 -#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */ 13.71 -#define ELFOSABI_SOLARIS 6 /* Solaris */ 13.72 -#define ELFOSABI_MONTEREY 7 /* Monterey */ 13.73 -#define ELFOSABI_IRIX 8 /* IRIX */ 13.74 -#define ELFOSABI_FREEBSD 9 /* FreeBSD */ 13.75 -#define ELFOSABI_TRU64 10 /* TRU64 UNIX */ 13.76 -#define ELFOSABI_MODESTO 11 /* Novell Modesto */ 13.77 -#define ELFOSABI_OPENBSD 12 /* OpenBSD */ 13.78 -#define ELFOSABI_ARM 97 /* ARM */ 13.79 -#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ 13.80 - 13.81 -/* e_ident */ 13.82 -#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \ 13.83 - (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \ 13.84 - (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \ 13.85 - (ehdr).e_ident[EI_MAG3] == ELFMAG3) 13.86 - 13.87 -/* ELF Header */ 13.88 -typedef struct elfhdr { 13.89 - unsigned char e_ident[EI_NIDENT]; /* ELF Identification */ 13.90 - Elf32_Half e_type; /* object file type */ 13.91 - Elf32_Half e_machine; /* machine */ 13.92 - Elf32_Word e_version; /* object file version */ 13.93 - Elf32_Addr e_entry; /* virtual entry point */ 13.94 - Elf32_Off e_phoff; /* program header table offset */ 13.95 - Elf32_Off e_shoff; /* section header table offset */ 13.96 - Elf32_Word e_flags; /* processor-specific flags */ 13.97 - Elf32_Half e_ehsize; /* ELF header size */ 13.98 - Elf32_Half e_phentsize; /* program header entry size */ 13.99 - Elf32_Half e_phnum; /* number of program header entries */ 13.100 - Elf32_Half e_shentsize; /* section header entry size */ 13.101 - Elf32_Half e_shnum; /* number of section header entries */ 13.102 - Elf32_Half e_shstrndx; /* section header table's "section 13.103 - header string table" entry offset */ 13.104 -} Elf32_Ehdr; 13.105 - 13.106 -typedef struct { 13.107 - unsigned char e_ident[EI_NIDENT]; /* Id bytes */ 13.108 - Elf64_Quarter e_type; /* file type */ 13.109 - Elf64_Quarter e_machine; /* machine type */ 13.110 - Elf64_Half e_version; /* version number */ 13.111 - Elf64_Addr e_entry; /* entry point */ 13.112 - Elf64_Off e_phoff; /* Program hdr offset */ 13.113 - Elf64_Off e_shoff; /* Section hdr offset */ 13.114 - Elf64_Half e_flags; /* Processor flags */ 13.115 - Elf64_Quarter e_ehsize; /* sizeof ehdr */ 13.116 - Elf64_Quarter e_phentsize; /* Program header entry size */ 13.117 - Elf64_Quarter e_phnum; /* Number of program headers */ 13.118 - Elf64_Quarter e_shentsize; /* Section header entry size */ 13.119 - Elf64_Quarter e_shnum; /* Number of section headers */ 13.120 - Elf64_Quarter e_shstrndx; /* String table index */ 13.121 -} Elf64_Ehdr; 13.122 - 13.123 -/* e_type */ 13.124 -#define ET_NONE 0 /* No file type */ 13.125 -#define ET_REL 1 /* relocatable file */ 13.126 -#define ET_EXEC 2 /* executable file */ 13.127 -#define ET_DYN 3 /* shared object file */ 13.128 -#define ET_CORE 4 /* core file */ 13.129 -#define ET_NUM 5 /* number of types */ 13.130 -#define ET_LOPROC 0xff00 /* reserved range for processor */ 13.131 -#define ET_HIPROC 0xffff /* specific e_type */ 13.132 - 13.133 -/* e_machine */ 13.134 -#define EM_NONE 0 /* No Machine */ 13.135 -#define EM_M32 1 /* AT&T WE 32100 */ 13.136 -#define EM_SPARC 2 /* SPARC */ 13.137 -#define EM_386 3 /* Intel 80386 */ 13.138 -#define EM_68K 4 /* Motorola 68000 */ 13.139 -#define EM_88K 5 /* Motorola 88000 */ 13.140 -#define EM_486 6 /* Intel 80486 - unused? */ 13.141 -#define EM_860 7 /* Intel 80860 */ 13.142 -#define EM_MIPS 8 /* MIPS R3000 Big-Endian only */ 13.143 -/* 13.144 - * Don't know if EM_MIPS_RS4_BE, 13.145 - * EM_SPARC64, EM_PARISC, 13.146 - * or EM_PPC are ABI compliant 13.147 - */ 13.148 -#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */ 13.149 -#define EM_SPARC64 11 /* SPARC v9 64-bit unoffical */ 13.150 -#define EM_PARISC 15 /* HPPA */ 13.151 -#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */ 13.152 -#define EM_PPC 20 /* PowerPC */ 13.153 -#define EM_PPC64 21 /* PowerPC 64-bit */ 13.154 -#define EM_ARM 40 /* Advanced RISC Machines ARM */ 13.155 -#define EM_ALPHA 41 /* DEC ALPHA */ 13.156 -#define EM_SPARCV9 43 /* SPARC version 9 */ 13.157 -#define EM_ALPHA_EXP 0x9026 /* DEC ALPHA */ 13.158 -#define EM_IA_64 50 /* Intel Merced */ 13.159 -#define EM_X86_64 62 /* AMD x86-64 architecture */ 13.160 -#define EM_VAX 75 /* DEC VAX */ 13.161 - 13.162 -/* Version */ 13.163 -#define EV_NONE 0 /* Invalid */ 13.164 -#define EV_CURRENT 1 /* Current */ 13.165 -#define EV_NUM 2 /* number of versions */ 13.166 - 13.167 -/* Section Header */ 13.168 -typedef struct { 13.169 - Elf32_Word sh_name; /* name - index into section header 13.170 - string table section */ 13.171 - Elf32_Word sh_type; /* type */ 13.172 - Elf32_Word sh_flags; /* flags */ 13.173 - Elf32_Addr sh_addr; /* address */ 13.174 - Elf32_Off sh_offset; /* file offset */ 13.175 - Elf32_Word sh_size; /* section size */ 13.176 - Elf32_Word sh_link; /* section header table index link */ 13.177 - Elf32_Word sh_info; /* extra information */ 13.178 - Elf32_Word sh_addralign; /* address alignment */ 13.179 - Elf32_Word sh_entsize; /* section entry size */ 13.180 -} Elf32_Shdr; 13.181 - 13.182 -typedef struct { 13.183 - Elf64_Half sh_name; /* section name */ 13.184 - Elf64_Half sh_type; /* section type */ 13.185 - Elf64_Xword sh_flags; /* section flags */ 13.186 - Elf64_Addr sh_addr; /* virtual address */ 13.187 - Elf64_Off sh_offset; /* file offset */ 13.188 - Elf64_Xword sh_size; /* section size */ 13.189 - Elf64_Half sh_link; /* link to another */ 13.190 - Elf64_Half sh_info; /* misc info */ 13.191 - Elf64_Xword sh_addralign; /* memory alignment */ 13.192 - Elf64_Xword sh_entsize; /* table entry size */ 13.193 -} Elf64_Shdr; 13.194 - 13.195 -/* Special Section Indexes */ 13.196 -#define SHN_UNDEF 0 /* undefined */ 13.197 -#define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */ 13.198 -#define SHN_LOPROC 0xff00 /* reserved range for processor */ 13.199 -#define SHN_HIPROC 0xff1f /* specific section indexes */ 13.200 -#define SHN_ABS 0xfff1 /* absolute value */ 13.201 -#define SHN_COMMON 0xfff2 /* common symbol */ 13.202 -#define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */ 13.203 - 13.204 -/* sh_type */ 13.205 -#define SHT_NULL 0 /* inactive */ 13.206 -#define SHT_PROGBITS 1 /* program defined information */ 13.207 -#define SHT_SYMTAB 2 /* symbol table section */ 13.208 -#define SHT_STRTAB 3 /* string table section */ 13.209 -#define SHT_RELA 4 /* relocation section with addends*/ 13.210 -#define SHT_HASH 5 /* symbol hash table section */ 13.211 -#define SHT_DYNAMIC 6 /* dynamic section */ 13.212 -#define SHT_NOTE 7 /* note section */ 13.213 -#define SHT_NOBITS 8 /* no space section */ 13.214 -#define SHT_REL 9 /* relation section without addends */ 13.215 -#define SHT_SHLIB 10 /* reserved - purpose unknown */ 13.216 -#define SHT_DYNSYM 11 /* dynamic symbol table section */ 13.217 -#define SHT_NUM 12 /* number of section types */ 13.218 -#define SHT_LOPROC 0x70000000 /* reserved range for processor */ 13.219 -#define SHT_HIPROC 0x7fffffff /* specific section header types */ 13.220 -#define SHT_LOUSER 0x80000000 /* reserved range for application */ 13.221 -#define SHT_HIUSER 0xffffffff /* specific indexes */ 13.222 - 13.223 -/* Section names */ 13.224 -#define ELF_BSS ".bss" /* uninitialized data */ 13.225 -#define ELF_DATA ".data" /* initialized data */ 13.226 -#define ELF_DEBUG ".debug" /* debug */ 13.227 -#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */ 13.228 -#define ELF_DYNSTR ".dynstr" /* dynamic string table */ 13.229 -#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */ 13.230 -#define ELF_FINI ".fini" /* termination code */ 13.231 -#define ELF_GOT ".got" /* global offset table */ 13.232 -#define ELF_HASH ".hash" /* symbol hash table */ 13.233 -#define ELF_INIT ".init" /* initialization code */ 13.234 -#define ELF_REL_DATA ".rel.data" /* relocation data */ 13.235 -#define ELF_REL_FINI ".rel.fini" /* relocation termination code */ 13.236 -#define ELF_REL_INIT ".rel.init" /* relocation initialization code */ 13.237 -#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */ 13.238 -#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */ 13.239 -#define ELF_REL_TEXT ".rel.text" /* relocation code */ 13.240 -#define ELF_RODATA ".rodata" /* read-only data */ 13.241 -#define ELF_SHSTRTAB ".shstrtab" /* section header string table */ 13.242 -#define ELF_STRTAB ".strtab" /* string table */ 13.243 -#define ELF_SYMTAB ".symtab" /* symbol table */ 13.244 -#define ELF_TEXT ".text" /* code */ 13.245 - 13.246 - 13.247 -/* Section Attribute Flags - sh_flags */ 13.248 -#define SHF_WRITE 0x1 /* Writable */ 13.249 -#define SHF_ALLOC 0x2 /* occupies memory */ 13.250 -#define SHF_EXECINSTR 0x4 /* executable */ 13.251 -#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */ 13.252 - /* specific section attributes */ 13.253 - 13.254 -/* Symbol Table Entry */ 13.255 -typedef struct elf32_sym { 13.256 - Elf32_Word st_name; /* name - index into string table */ 13.257 - Elf32_Addr st_value; /* symbol value */ 13.258 - Elf32_Word st_size; /* symbol size */ 13.259 - unsigned char st_info; /* type and binding */ 13.260 - unsigned char st_other; /* 0 - no defined meaning */ 13.261 - Elf32_Half st_shndx; /* section header index */ 13.262 -} Elf32_Sym; 13.263 - 13.264 -typedef struct { 13.265 - Elf64_Half st_name; /* Symbol name index in str table */ 13.266 - Elf_Byte st_info; /* type / binding attrs */ 13.267 - Elf_Byte st_other; /* unused */ 13.268 - Elf64_Quarter st_shndx; /* section index of symbol */ 13.269 - Elf64_Xword st_value; /* value of symbol */ 13.270 - Elf64_Xword st_size; /* size of symbol */ 13.271 -} Elf64_Sym; 13.272 - 13.273 -/* Symbol table index */ 13.274 -#define STN_UNDEF 0 /* undefined */ 13.275 - 13.276 -/* Extract symbol info - st_info */ 13.277 -#define ELF32_ST_BIND(x) ((x) >> 4) 13.278 -#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf) 13.279 -#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) 13.280 - 13.281 -#define ELF64_ST_BIND(x) ((x) >> 4) 13.282 -#define ELF64_ST_TYPE(x) (((unsigned int) x) & 0xf) 13.283 -#define ELF64_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) 13.284 - 13.285 -/* Symbol Binding - ELF32_ST_BIND - st_info */ 13.286 -#define STB_LOCAL 0 /* Local symbol */ 13.287 -#define STB_GLOBAL 1 /* Global symbol */ 13.288 -#define STB_WEAK 2 /* like global - lower precedence */ 13.289 -#define STB_NUM 3 /* number of symbol bindings */ 13.290 -#define STB_LOPROC 13 /* reserved range for processor */ 13.291 -#define STB_HIPROC 15 /* specific symbol bindings */ 13.292 - 13.293 -/* Symbol type - ELF32_ST_TYPE - st_info */ 13.294 -#define STT_NOTYPE 0 /* not specified */ 13.295 -#define STT_OBJECT 1 /* data object */ 13.296 -#define STT_FUNC 2 /* function */ 13.297 -#define STT_SECTION 3 /* section */ 13.298 -#define STT_FILE 4 /* file */ 13.299 -#define STT_NUM 5 /* number of symbol types */ 13.300 -#define STT_LOPROC 13 /* reserved range for processor */ 13.301 -#define STT_HIPROC 15 /* specific symbol types */ 13.302 - 13.303 -/* Relocation entry with implicit addend */ 13.304 -typedef struct { 13.305 - Elf32_Addr r_offset; /* offset of relocation */ 13.306 - Elf32_Word r_info; /* symbol table index and type */ 13.307 -} Elf32_Rel; 13.308 - 13.309 -/* Relocation entry with explicit addend */ 13.310 -typedef struct { 13.311 - Elf32_Addr r_offset; /* offset of relocation */ 13.312 - Elf32_Word r_info; /* symbol table index and type */ 13.313 - Elf32_Sword r_addend; 13.314 -} Elf32_Rela; 13.315 - 13.316 -/* Extract relocation info - r_info */ 13.317 -#define ELF32_R_SYM(i) ((i) >> 8) 13.318 -#define ELF32_R_TYPE(i) ((unsigned char) (i)) 13.319 -#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t)) 13.320 - 13.321 -typedef struct { 13.322 - Elf64_Xword r_offset; /* where to do it */ 13.323 - Elf64_Xword r_info; /* index & type of relocation */ 13.324 -} Elf64_Rel; 13.325 - 13.326 -typedef struct { 13.327 - Elf64_Xword r_offset; /* where to do it */ 13.328 - Elf64_Xword r_info; /* index & type of relocation */ 13.329 - Elf64_Sxword r_addend; /* adjustment value */ 13.330 -} Elf64_Rela; 13.331 - 13.332 -#define ELF64_R_SYM(info) ((info) >> 32) 13.333 -#define ELF64_R_TYPE(info) ((info) & 0xFFFFFFFF) 13.334 -#define ELF64_R_INFO(s,t) (((s) << 32) + (u32)(t)) 13.335 - 13.336 -/* Program Header */ 13.337 -typedef struct { 13.338 - Elf32_Word p_type; /* segment type */ 13.339 - Elf32_Off p_offset; /* segment offset */ 13.340 - Elf32_Addr p_vaddr; /* virtual address of segment */ 13.341 - Elf32_Addr p_paddr; /* physical address - ignored? */ 13.342 - Elf32_Word p_filesz; /* number of bytes in file for seg. */ 13.343 - Elf32_Word p_memsz; /* number of bytes in mem. for seg. */ 13.344 - Elf32_Word p_flags; /* flags */ 13.345 - Elf32_Word p_align; /* memory alignment */ 13.346 -} Elf32_Phdr; 13.347 - 13.348 -typedef struct { 13.349 - Elf64_Half p_type; /* entry type */ 13.350 - Elf64_Half p_flags; /* flags */ 13.351 - Elf64_Off p_offset; /* offset */ 13.352 - Elf64_Addr p_vaddr; /* virtual address */ 13.353 - Elf64_Addr p_paddr; /* physical address */ 13.354 - Elf64_Xword p_filesz; /* file size */ 13.355 - Elf64_Xword p_memsz; /* memory size */ 13.356 - Elf64_Xword p_align; /* memory & file alignment */ 13.357 -} Elf64_Phdr; 13.358 - 13.359 -/* Segment types - p_type */ 13.360 -#define PT_NULL 0 /* unused */ 13.361 -#define PT_LOAD 1 /* loadable segment */ 13.362 -#define PT_DYNAMIC 2 /* dynamic linking section */ 13.363 -#define PT_INTERP 3 /* the RTLD */ 13.364 -#define PT_NOTE 4 /* auxiliary information */ 13.365 -#define PT_SHLIB 5 /* reserved - purpose undefined */ 13.366 -#define PT_PHDR 6 /* program header */ 13.367 -#define PT_NUM 7 /* Number of segment types */ 13.368 -#define PT_LOPROC 0x70000000 /* reserved range for processor */ 13.369 -#define PT_HIPROC 0x7fffffff /* specific segment types */ 13.370 - 13.371 -/* Segment flags - p_flags */ 13.372 -#define PF_X 0x1 /* Executable */ 13.373 -#define PF_W 0x2 /* Writable */ 13.374 -#define PF_R 0x4 /* Readable */ 13.375 -#define PF_MASKPROC 0xf0000000 /* reserved bits for processor */ 13.376 - /* specific segment flags */ 13.377 - 13.378 -/* Dynamic structure */ 13.379 -typedef struct { 13.380 - Elf32_Sword d_tag; /* controls meaning of d_val */ 13.381 - union { 13.382 - Elf32_Word d_val; /* Multiple meanings - see d_tag */ 13.383 - Elf32_Addr d_ptr; /* program virtual address */ 13.384 - } d_un; 13.385 -} Elf32_Dyn; 13.386 - 13.387 -typedef struct { 13.388 - Elf64_Xword d_tag; /* controls meaning of d_val */ 13.389 - union { 13.390 - Elf64_Addr d_ptr; 13.391 - Elf64_Xword d_val; 13.392 - } d_un; 13.393 -} Elf64_Dyn; 13.394 - 13.395 -/* Dynamic Array Tags - d_tag */ 13.396 -#define DT_NULL 0 /* marks end of _DYNAMIC array */ 13.397 -#define DT_NEEDED 1 /* string table offset of needed lib */ 13.398 -#define DT_PLTRELSZ 2 /* size of relocation entries in PLT */ 13.399 -#define DT_PLTGOT 3 /* address PLT/GOT */ 13.400 -#define DT_HASH 4 /* address of symbol hash table */ 13.401 -#define DT_STRTAB 5 /* address of string table */ 13.402 -#define DT_SYMTAB 6 /* address of symbol table */ 13.403 -#define DT_RELA 7 /* address of relocation table */ 13.404 -#define DT_RELASZ 8 /* size of relocation table */ 13.405 -#define DT_RELAENT 9 /* size of relocation entry */ 13.406 -#define DT_STRSZ 10 /* size of string table */ 13.407 -#define DT_SYMENT 11 /* size of symbol table entry */ 13.408 -#define DT_INIT 12 /* address of initialization func. */ 13.409 -#define DT_FINI 13 /* address of termination function */ 13.410 -#define DT_SONAME 14 /* string table offset of shared obj */ 13.411 -#define DT_RPATH 15 /* string table offset of library 13.412 - search path */ 13.413 -#define DT_SYMBOLIC 16 /* start sym search in shared obj. */ 13.414 -#define DT_REL 17 /* address of rel. tbl. w addends */ 13.415 -#define DT_RELSZ 18 /* size of DT_REL relocation table */ 13.416 -#define DT_RELENT 19 /* size of DT_REL relocation entry */ 13.417 -#define DT_PLTREL 20 /* PLT referenced relocation entry */ 13.418 -#define DT_DEBUG 21 /* bugger */ 13.419 -#define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */ 13.420 -#define DT_JMPREL 23 /* add. of PLT's relocation entries */ 13.421 -#define DT_BIND_NOW 24 /* Bind now regardless of env setting */ 13.422 -#define DT_NUM 25 /* Number used. */ 13.423 -#define DT_LOPROC 0x70000000 /* reserved range for processor */ 13.424 -#define DT_HIPROC 0x7fffffff /* specific dynamic array tags */ 13.425 - 13.426 -/* Standard ELF hashing function */ 13.427 -unsigned int elf_hash(const unsigned char *name); 13.428 - 13.429 -/* 13.430 - * Note Definitions 13.431 - */ 13.432 - 13.433 -typedef struct { 13.434 - u32 namesz; 13.435 - u32 descsz; 13.436 - u32 type; 13.437 -} Elf_Note; /* same format for both 32-bit and 64-bit ELF */ 13.438 - 13.439 -#if defined(ELFSIZE) 13.440 -#define CONCAT(x,y) __CONCAT(x,y) 13.441 -#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x))) 13.442 -#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y)))) 13.443 -#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE)) 13.444 -#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x))) 13.445 -#endif 13.446 - 13.447 -#if defined(ELFSIZE) && (ELFSIZE == 32) 13.448 -#define Elf_Ehdr Elf32_Ehdr 13.449 -#define Elf_Phdr Elf32_Phdr 13.450 -#define Elf_Shdr Elf32_Shdr 13.451 -#define Elf_Sym Elf32_Sym 13.452 -#define Elf_Rel Elf32_Rel 13.453 -#define Elf_RelA Elf32_Rela 13.454 -#define Elf_Dyn Elf32_Dyn 13.455 -#define Elf_Word Elf32_Word 13.456 -#define Elf_Sword Elf32_Sword 13.457 -#define Elf_Addr Elf32_Addr 13.458 -#define Elf_Off Elf32_Off 13.459 -#define Elf_Nhdr Elf32_Nhdr 13.460 - 13.461 -#define ELF_R_SYM ELF32_R_SYM 13.462 -#define ELF_R_TYPE ELF32_R_TYPE 13.463 -#define ELF_R_INFO ELF32_R_INFO 13.464 -#define ELFCLASS ELFCLASS32 13.465 - 13.466 -#define ELF_ST_BIND ELF32_ST_BIND 13.467 -#define ELF_ST_TYPE ELF32_ST_TYPE 13.468 -#define ELF_ST_INFO ELF32_ST_INFO 13.469 - 13.470 -#define AuxInfo Aux32Info 13.471 -#elif defined(ELFSIZE) && (ELFSIZE == 64) 13.472 -#define Elf_Ehdr Elf64_Ehdr 13.473 -#define Elf_Phdr Elf64_Phdr 13.474 -#define Elf_Shdr Elf64_Shdr 13.475 -#define Elf_Sym Elf64_Sym 13.476 -#define Elf_Rel Elf64_Rel 13.477 -#define Elf_RelA Elf64_Rela 13.478 -#define Elf_Dyn Elf64_Dyn 13.479 -#define Elf_Word Elf64_Word 13.480 -#define Elf_Sword Elf64_Sword 13.481 -#define Elf_Addr Elf64_Addr 13.482 -#define Elf_Off Elf64_Off 13.483 -#define Elf_Nhdr Elf64_Nhdr 13.484 - 13.485 -#define ELF_R_SYM ELF64_R_SYM 13.486 -#define ELF_R_TYPE ELF64_R_TYPE 13.487 -#define ELF_R_INFO ELF64_R_INFO 13.488 -#define ELFCLASS ELFCLASS64 13.489 - 13.490 -#define ELF_ST_BIND ELF64_ST_BIND 13.491 -#define ELF_ST_TYPE ELF64_ST_TYPE 13.492 -#define ELF_ST_INFO ELF64_ST_INFO 13.493 - 13.494 -#define AuxInfo Aux64Info 13.495 -#endif 13.496 +#include <public/elfstructs.h> 13.497 13.498 #define ELFNOTE_ALIGN(_n_) (((_n_)+3)&~3) 13.499 #define ELFNOTE_NAME(_n_) ((char*)(_n_) + sizeof(*(_n_)))