agent/src/os/bsd/salibelf.c

Mon, 28 Jul 2014 15:06:38 -0700

author
fzhinkin
date
Mon, 28 Jul 2014 15:06:38 -0700
changeset 6997
dbb05f6d93c4
parent 0
f90c822e73f8
permissions
-rw-r--r--

8051344: JVM crashed in Compile::start() during method parsing w/ UseRTMDeopt turned on
Summary: call rtm_deopt() only if there were no compilation bailouts before.
Reviewed-by: kvn

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

mercurial