src/hotspot/os/linux/decoder_linux.cpp
author zgu
Wed, 14 Feb 2018 17:20:59 -0500
changeset 48975 2c35fd3c5789
parent 47765 b7c7428eaab9
permissions -rw-r--r--
8193373: Cleanup ElfFile and family Summary: Cleanup elf decoder and added test Reviewed-by: minqi, coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     1
/*
48975
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
     2
 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     4
 *
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     8
 *
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    13
 * accompanied this code).
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    14
 *
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    18
 *
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    21
 * questions.
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    22
 *
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    23
 */
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    24
47765
b7c7428eaab9 8189610: Reconcile jvm.h and all jvm_md.h between java.base and hotspot
coleenp
parents: 47216
diff changeset
    25
#include "jvm.h"
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 7447
diff changeset
    26
#include "utilities/decoder_elf.hpp"
48975
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    27
#include "utilities/elfFile.hpp"
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    28
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    29
#include <cxxabi.h>
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    30
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 7447
diff changeset
    31
bool ElfDecoder::demangle(const char* symbol, char *buf, int buflen) {
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    32
  int   status;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    33
  char* result;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    34
  size_t size = (size_t)buflen;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    35
22857
2167396cfc83 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 13963
diff changeset
    36
#ifdef PPC64
2167396cfc83 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 13963
diff changeset
    37
  // On PPC64 ElfDecoder::decode() may return a dot (.) prefixed name
2167396cfc83 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 13963
diff changeset
    38
  // (see elfFuncDescTable.hpp for details)
2167396cfc83 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 13963
diff changeset
    39
  if (symbol && *symbol == '.') symbol += 1;
2167396cfc83 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 13963
diff changeset
    40
#endif
2167396cfc83 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 13963
diff changeset
    41
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    42
  // Don't pass buf to __cxa_demangle. In case of the 'buf' is too small,
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    43
  // __cxa_demangle will call system "realloc" for additional memory, which
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    44
  // may use different malloc/realloc mechanism that allocates 'buf'.
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    45
  if ((result = abi::__cxa_demangle(symbol, NULL, NULL, &status)) != NULL) {
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    46
    jio_snprintf(buf, buflen, "%s", result);
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    47
      // call c library's free
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    48
      ::free(result);
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    49
      return true;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    50
  }
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    51
  return false;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    52
}
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 7447
diff changeset
    53
48975
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    54
// Returns true if the elf file is marked NOT to require an executable stack,
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    55
// or if the file could not be opened.
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    56
// Returns false if the elf file requires an executable stack, the stack flag
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    57
// is not set at all, or if the file can not be read.
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    58
bool ElfFile::specifies_noexecstack(const char* filepath) {
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    59
  if (filepath == NULL) return true;
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    60
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    61
  FILE* file = fopen(filepath, "r");
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    62
  if (file == NULL)  return true;
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    63
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    64
  // AARCH64 defaults to noexecstack. All others default to execstack.
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    65
  bool result = AARCH64_ONLY(true) NOT_AARCH64(false);
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    66
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    67
  // Read file header
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    68
  Elf_Ehdr head;
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    69
  if (fread(&head, sizeof(Elf_Ehdr), 1, file) == 1 &&
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    70
      is_elf_file(head) &&
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    71
      fseek(file, head.e_phoff, SEEK_SET) == 0) {
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    72
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    73
    // Read program header table
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    74
    Elf_Phdr phdr;
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    75
    for (int index = 0; index < head.e_phnum; index ++) {
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    76
      if (fread((void*)&phdr, sizeof(Elf_Phdr), 1, file) != 1) {
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    77
        result = false;
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    78
        break;
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    79
      }
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    80
      if (phdr.p_type == PT_GNU_STACK) {
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    81
        result = (phdr.p_flags == (PF_R | PF_W));
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    82
        break;
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    83
      }
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    84
    }
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    85
  }
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    86
  fclose(file);
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    87
  return result;
2c35fd3c5789 8193373: Cleanup ElfFile and family
zgu
parents: 47765
diff changeset
    88
}