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