Path: blob/master/src/jdk.hotspot.agent/linux/native/libsaproc/salibelf.c
41149 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "salibelf.h"25#include <stdlib.h>26#include <unistd.h>27#include <string.h>2829extern void print_debug(const char*,...);3031// ELF file parsing helpers. Note that we do *not* use libelf here.32int read_elf_header(int fd, ELF_EHDR* ehdr) {33if (pread(fd, ehdr, sizeof (ELF_EHDR), 0) != sizeof (ELF_EHDR) ||34memcmp(&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG) != 0 ||35ehdr->e_version != EV_CURRENT) {36return 0;37}38return 1;39}4041bool is_elf_file(int fd) {42ELF_EHDR ehdr;43return read_elf_header(fd, &ehdr);44}4546// read program header table of an ELF file47ELF_PHDR* read_program_header_table(int fd, ELF_EHDR* hdr) {48ELF_PHDR* phbuf = 0;49// allocate memory for program header table50size_t nbytes = hdr->e_phnum * hdr->e_phentsize;5152if ((phbuf = (ELF_PHDR*) malloc(nbytes)) == NULL) {53print_debug("can't allocate memory for reading program header table\n");54return NULL;55}5657if (pread(fd, phbuf, nbytes, hdr->e_phoff) != nbytes) {58print_debug("ELF file is truncated! can't read program header table\n");59free(phbuf);60return NULL;61}6263return phbuf;64}6566// read section header table of an ELF file67ELF_SHDR* read_section_header_table(int fd, ELF_EHDR* hdr) {68ELF_SHDR* shbuf = 0;69// allocate memory for section header table70size_t nbytes = hdr->e_shnum * hdr->e_shentsize;7172if ((shbuf = (ELF_SHDR*) malloc(nbytes)) == NULL) {73print_debug("can't allocate memory for reading section header table\n");74return NULL;75}7677if (pread(fd, shbuf, nbytes, hdr->e_shoff) != nbytes) {78print_debug("ELF file is truncated! can't read section header table\n");79free(shbuf);80return NULL;81}8283return shbuf;84}8586// read a particular section's data87void* read_section_data(int fd, ELF_EHDR* ehdr, ELF_SHDR* shdr) {88void *buf = NULL;89if (shdr->sh_type == SHT_NOBITS || shdr->sh_size == 0) {90return buf;91}92if ((buf = calloc(shdr->sh_size, 1)) == NULL) {93print_debug("can't allocate memory for reading section data\n");94return NULL;95}96if (pread(fd, buf, shdr->sh_size, shdr->sh_offset) != shdr->sh_size) {97free(buf);98print_debug("section data read failed\n");99return NULL;100}101return buf;102}103104uintptr_t find_base_address(int fd, ELF_EHDR* ehdr) {105uintptr_t baseaddr = (uintptr_t)-1;106int cnt;107ELF_PHDR *phbuf, *phdr;108109// read program header table110if ((phbuf = read_program_header_table(fd, ehdr)) == NULL) {111goto quit;112}113114// the base address of a shared object is the lowest vaddr of115// its loadable segments (PT_LOAD)116for (phdr = phbuf, cnt = 0; cnt < ehdr->e_phnum; cnt++, phdr++) {117if (phdr->p_type == PT_LOAD && phdr->p_vaddr < baseaddr) {118baseaddr = phdr->p_vaddr;119}120}121122quit:123if (phbuf) free(phbuf);124return baseaddr;125}126127struct elf_section *find_section_by_name(char *name,128int fd,129ELF_EHDR *ehdr,130struct elf_section *scn_cache) {131char *strtab;132int cnt;133int strtab_size;134135// Section cache have to already contain data for e_shstrndx section.136// If it's not true - elf file is broken, so just bail out137if (scn_cache[ehdr->e_shstrndx].c_data == NULL) {138return NULL;139}140141strtab = scn_cache[ehdr->e_shstrndx].c_data;142strtab_size = scn_cache[ehdr->e_shstrndx].c_shdr->sh_size;143144for (cnt = 0; cnt < ehdr->e_shnum; ++cnt) {145if (scn_cache[cnt].c_shdr->sh_name < strtab_size) {146if (strcmp(scn_cache[cnt].c_shdr->sh_name + strtab, name) == 0) {147scn_cache[cnt].c_data = read_section_data(fd, ehdr, scn_cache[cnt].c_shdr);148return &scn_cache[cnt];149}150}151}152153return NULL;154}155156157