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