aoqi@0: /* aoqi@0: * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "salibelf.h" aoqi@0: #include aoqi@0: #include aoqi@0: #include aoqi@0: aoqi@0: extern void print_debug(const char*,...); aoqi@0: aoqi@0: // ELF file parsing helpers. Note that we do *not* use libelf here. aoqi@0: int read_elf_header(int fd, ELF_EHDR* ehdr) { aoqi@0: if (pread(fd, ehdr, sizeof (ELF_EHDR), 0) != sizeof (ELF_EHDR) || aoqi@0: memcmp(&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG) != 0 || aoqi@0: ehdr->e_version != EV_CURRENT) { aoqi@0: return 0; aoqi@0: } aoqi@0: return 1; aoqi@0: } aoqi@0: aoqi@0: bool is_elf_file(int fd) { aoqi@0: ELF_EHDR ehdr; aoqi@0: return read_elf_header(fd, &ehdr); aoqi@0: } aoqi@0: aoqi@0: // read program header table of an ELF file aoqi@0: ELF_PHDR* read_program_header_table(int fd, ELF_EHDR* hdr) { aoqi@0: ELF_PHDR* phbuf = 0; aoqi@0: // allocate memory for program header table aoqi@0: size_t nbytes = hdr->e_phnum * hdr->e_phentsize; aoqi@0: aoqi@0: if ((phbuf = (ELF_PHDR*) malloc(nbytes)) == NULL) { aoqi@0: print_debug("can't allocate memory for reading program header table\n"); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: if (pread(fd, phbuf, nbytes, hdr->e_phoff) != nbytes) { aoqi@0: print_debug("ELF file is truncated! can't read program header table\n"); aoqi@0: free(phbuf); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: return phbuf; aoqi@0: } aoqi@0: aoqi@0: // read section header table of an ELF file aoqi@0: ELF_SHDR* read_section_header_table(int fd, ELF_EHDR* hdr) { aoqi@0: ELF_SHDR* shbuf = 0; aoqi@0: // allocate memory for section header table aoqi@0: size_t nbytes = hdr->e_shnum * hdr->e_shentsize; aoqi@0: aoqi@0: if ((shbuf = (ELF_SHDR*) malloc(nbytes)) == NULL) { aoqi@0: print_debug("can't allocate memory for reading section header table\n"); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: if (pread(fd, shbuf, nbytes, hdr->e_shoff) != nbytes) { aoqi@0: print_debug("ELF file is truncated! can't read section header table\n"); aoqi@0: free(shbuf); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: return shbuf; aoqi@0: } aoqi@0: aoqi@0: // read a particular section's data aoqi@0: void* read_section_data(int fd, ELF_EHDR* ehdr, ELF_SHDR* shdr) { aoqi@0: void *buf = NULL; aoqi@0: if (shdr->sh_type == SHT_NOBITS || shdr->sh_size == 0) { aoqi@0: return buf; aoqi@0: } aoqi@0: if ((buf = calloc(shdr->sh_size, 1)) == NULL) { aoqi@0: print_debug("can't allocate memory for reading section data\n"); aoqi@0: return NULL; aoqi@0: } aoqi@0: if (pread(fd, buf, shdr->sh_size, shdr->sh_offset) != shdr->sh_size) { aoqi@0: free(buf); aoqi@0: print_debug("section data read failed\n"); aoqi@0: return NULL; aoqi@0: } aoqi@0: return buf; aoqi@0: } aoqi@0: aoqi@0: uintptr_t find_base_address(int fd, ELF_EHDR* ehdr) { aoqi@0: uintptr_t baseaddr = (uintptr_t)-1; aoqi@0: int cnt; aoqi@0: ELF_PHDR *phbuf, *phdr; aoqi@0: aoqi@0: // read program header table aoqi@0: if ((phbuf = read_program_header_table(fd, ehdr)) == NULL) { aoqi@0: goto quit; aoqi@0: } aoqi@0: aoqi@0: // the base address of a shared object is the lowest vaddr of aoqi@0: // its loadable segments (PT_LOAD) aoqi@0: for (phdr = phbuf, cnt = 0; cnt < ehdr->e_phnum; cnt++, phdr++) { aoqi@0: if (phdr->p_type == PT_LOAD && phdr->p_vaddr < baseaddr) { aoqi@0: baseaddr = phdr->p_vaddr; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: quit: aoqi@0: if (phbuf) free(phbuf); aoqi@0: return baseaddr; aoqi@0: }