1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/agent/src/os/bsd/salibelf.c Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,126 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "salibelf.h" 1.29 +#include <stdlib.h> 1.30 +#include <unistd.h> 1.31 +#include <string.h> 1.32 + 1.33 +extern void print_debug(const char*,...); 1.34 + 1.35 +// ELF file parsing helpers. Note that we do *not* use libelf here. 1.36 +int read_elf_header(int fd, ELF_EHDR* ehdr) { 1.37 + if (pread(fd, ehdr, sizeof (ELF_EHDR), 0) != sizeof (ELF_EHDR) || 1.38 + memcmp(&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG) != 0 || 1.39 + ehdr->e_version != EV_CURRENT) { 1.40 + return 0; 1.41 + } 1.42 + return 1; 1.43 +} 1.44 + 1.45 +bool is_elf_file(int fd) { 1.46 + ELF_EHDR ehdr; 1.47 + return read_elf_header(fd, &ehdr); 1.48 +} 1.49 + 1.50 +// read program header table of an ELF file 1.51 +ELF_PHDR* read_program_header_table(int fd, ELF_EHDR* hdr) { 1.52 + ELF_PHDR* phbuf = 0; 1.53 + // allocate memory for program header table 1.54 + size_t nbytes = hdr->e_phnum * hdr->e_phentsize; 1.55 + 1.56 + if ((phbuf = (ELF_PHDR*) malloc(nbytes)) == NULL) { 1.57 + print_debug("can't allocate memory for reading program header table\n"); 1.58 + return NULL; 1.59 + } 1.60 + 1.61 + if (pread(fd, phbuf, nbytes, hdr->e_phoff) != nbytes) { 1.62 + print_debug("ELF file is truncated! can't read program header table\n"); 1.63 + free(phbuf); 1.64 + return NULL; 1.65 + } 1.66 + 1.67 + return phbuf; 1.68 +} 1.69 + 1.70 +// read section header table of an ELF file 1.71 +ELF_SHDR* read_section_header_table(int fd, ELF_EHDR* hdr) { 1.72 + ELF_SHDR* shbuf = 0; 1.73 + // allocate memory for section header table 1.74 + size_t nbytes = hdr->e_shnum * hdr->e_shentsize; 1.75 + 1.76 + if ((shbuf = (ELF_SHDR*) malloc(nbytes)) == NULL) { 1.77 + print_debug("can't allocate memory for reading section header table\n"); 1.78 + return NULL; 1.79 + } 1.80 + 1.81 + if (pread(fd, shbuf, nbytes, hdr->e_shoff) != nbytes) { 1.82 + print_debug("ELF file is truncated! can't read section header table\n"); 1.83 + free(shbuf); 1.84 + return NULL; 1.85 + } 1.86 + 1.87 + return shbuf; 1.88 +} 1.89 + 1.90 +// read a particular section's data 1.91 +void* read_section_data(int fd, ELF_EHDR* ehdr, ELF_SHDR* shdr) { 1.92 + void *buf = NULL; 1.93 + if (shdr->sh_type == SHT_NOBITS || shdr->sh_size == 0) { 1.94 + return buf; 1.95 + } 1.96 + if ((buf = calloc(shdr->sh_size, 1)) == NULL) { 1.97 + print_debug("can't allocate memory for reading section data\n"); 1.98 + return NULL; 1.99 + } 1.100 + if (pread(fd, buf, shdr->sh_size, shdr->sh_offset) != shdr->sh_size) { 1.101 + free(buf); 1.102 + print_debug("section data read failed\n"); 1.103 + return NULL; 1.104 + } 1.105 + return buf; 1.106 +} 1.107 + 1.108 +uintptr_t find_base_address(int fd, ELF_EHDR* ehdr) { 1.109 + uintptr_t baseaddr = (uintptr_t)-1; 1.110 + int cnt; 1.111 + ELF_PHDR *phbuf, *phdr; 1.112 + 1.113 + // read program header table 1.114 + if ((phbuf = read_program_header_table(fd, ehdr)) == NULL) { 1.115 + goto quit; 1.116 + } 1.117 + 1.118 + // the base address of a shared object is the lowest vaddr of 1.119 + // its loadable segments (PT_LOAD) 1.120 + for (phdr = phbuf, cnt = 0; cnt < ehdr->e_phnum; cnt++, phdr++) { 1.121 + if (phdr->p_type == PT_LOAD && phdr->p_vaddr < baseaddr) { 1.122 + baseaddr = phdr->p_vaddr; 1.123 + } 1.124 + } 1.125 + 1.126 +quit: 1.127 + if (phbuf) free(phbuf); 1.128 + return baseaddr; 1.129 +}