Wed, 02 Jul 2008 12:55:16 -0700
6719955: Update copyright year
Summary: Update copyright year for files that have been modified in 2008
Reviewed-by: ohair, tbell
duke@435 | 1 | /* |
duke@435 | 2 | * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | #include "salibelf.h" |
duke@435 | 26 | #include <stdlib.h> |
duke@435 | 27 | #include <unistd.h> |
duke@435 | 28 | |
duke@435 | 29 | extern void print_debug(const char*,...); |
duke@435 | 30 | |
duke@435 | 31 | // ELF file parsing helpers. Note that we do *not* use libelf here. |
duke@435 | 32 | int read_elf_header(int fd, ELF_EHDR* ehdr) { |
duke@435 | 33 | if (pread(fd, ehdr, sizeof (ELF_EHDR), 0) != sizeof (ELF_EHDR) || |
duke@435 | 34 | memcmp(&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG) != 0 || |
duke@435 | 35 | ehdr->e_version != EV_CURRENT) { |
duke@435 | 36 | return 0; |
duke@435 | 37 | } |
duke@435 | 38 | return 1; |
duke@435 | 39 | } |
duke@435 | 40 | |
duke@435 | 41 | bool is_elf_file(int fd) { |
duke@435 | 42 | ELF_EHDR ehdr; |
duke@435 | 43 | return read_elf_header(fd, &ehdr); |
duke@435 | 44 | } |
duke@435 | 45 | |
duke@435 | 46 | // read program header table of an ELF file |
duke@435 | 47 | ELF_PHDR* read_program_header_table(int fd, ELF_EHDR* hdr) { |
duke@435 | 48 | ELF_PHDR* phbuf = 0; |
duke@435 | 49 | // allocate memory for program header table |
duke@435 | 50 | size_t nbytes = hdr->e_phnum * hdr->e_phentsize; |
duke@435 | 51 | |
duke@435 | 52 | if ((phbuf = (ELF_PHDR*) malloc(nbytes)) == NULL) { |
duke@435 | 53 | print_debug("can't allocate memory for reading program header table\n"); |
duke@435 | 54 | return NULL; |
duke@435 | 55 | } |
duke@435 | 56 | |
duke@435 | 57 | if (pread(fd, phbuf, nbytes, hdr->e_phoff) != nbytes) { |
duke@435 | 58 | print_debug("ELF file is truncated! can't read program header table\n"); |
duke@435 | 59 | free(phbuf); |
duke@435 | 60 | return NULL; |
duke@435 | 61 | } |
duke@435 | 62 | |
duke@435 | 63 | return phbuf; |
duke@435 | 64 | } |
duke@435 | 65 | |
duke@435 | 66 | // read section header table of an ELF file |
duke@435 | 67 | ELF_SHDR* read_section_header_table(int fd, ELF_EHDR* hdr) { |
duke@435 | 68 | ELF_SHDR* shbuf = 0; |
duke@435 | 69 | // allocate memory for section header table |
duke@435 | 70 | size_t nbytes = hdr->e_shnum * hdr->e_shentsize; |
duke@435 | 71 | |
duke@435 | 72 | if ((shbuf = (ELF_SHDR*) malloc(nbytes)) == NULL) { |
duke@435 | 73 | print_debug("can't allocate memory for reading section header table\n"); |
duke@435 | 74 | return NULL; |
duke@435 | 75 | } |
duke@435 | 76 | |
duke@435 | 77 | if (pread(fd, shbuf, nbytes, hdr->e_shoff) != nbytes) { |
duke@435 | 78 | print_debug("ELF file is truncated! can't read section header table\n"); |
duke@435 | 79 | free(shbuf); |
duke@435 | 80 | return NULL; |
duke@435 | 81 | } |
duke@435 | 82 | |
duke@435 | 83 | return shbuf; |
duke@435 | 84 | } |
duke@435 | 85 | |
duke@435 | 86 | // read a particular section's data |
duke@435 | 87 | void* read_section_data(int fd, ELF_EHDR* ehdr, ELF_SHDR* shdr) { |
duke@435 | 88 | void *buf = NULL; |
duke@435 | 89 | if (shdr->sh_type == SHT_NOBITS || shdr->sh_size == 0) { |
duke@435 | 90 | return buf; |
duke@435 | 91 | } |
duke@435 | 92 | if ((buf = calloc(shdr->sh_size, 1)) == NULL) { |
duke@435 | 93 | print_debug("can't allocate memory for reading section data\n"); |
duke@435 | 94 | return NULL; |
duke@435 | 95 | } |
duke@435 | 96 | if (pread(fd, buf, shdr->sh_size, shdr->sh_offset) != shdr->sh_size) { |
duke@435 | 97 | free(buf); |
duke@435 | 98 | print_debug("section data read failed\n"); |
duke@435 | 99 | return NULL; |
duke@435 | 100 | } |
duke@435 | 101 | return buf; |
duke@435 | 102 | } |
duke@435 | 103 | |
duke@435 | 104 | uintptr_t find_base_address(int fd, ELF_EHDR* ehdr) { |
duke@435 | 105 | uintptr_t baseaddr = (uintptr_t)-1; |
duke@435 | 106 | int cnt; |
duke@435 | 107 | ELF_PHDR *phbuf, *phdr; |
duke@435 | 108 | |
duke@435 | 109 | // read program header table |
duke@435 | 110 | if ((phbuf = read_program_header_table(fd, ehdr)) == NULL) { |
duke@435 | 111 | goto quit; |
duke@435 | 112 | } |
duke@435 | 113 | |
duke@435 | 114 | // the base address of a shared object is the lowest vaddr of |
duke@435 | 115 | // its loadable segments (PT_LOAD) |
duke@435 | 116 | for (phdr = phbuf, cnt = 0; cnt < ehdr->e_phnum; cnt++, phdr++) { |
duke@435 | 117 | if (phdr->p_type == PT_LOAD && phdr->p_vaddr < baseaddr) { |
duke@435 | 118 | baseaddr = phdr->p_vaddr; |
duke@435 | 119 | } |
duke@435 | 120 | } |
duke@435 | 121 | |
duke@435 | 122 | quit: |
duke@435 | 123 | if (phbuf) free(phbuf); |
duke@435 | 124 | return baseaddr; |
duke@435 | 125 | } |