annotate src/share/vm/utilities/elfFile.cpp @ 20543:e7d0505c8a30

8059758: Footprint regressions with JDK-8038423 Summary: Changes in JDK-8038423 always initialize (zero out) virtual memory used for auxiliary data structures. This causes a footprint regression for G1 in startup benchmarks. This is because they do not touch that memory at all, so the operating system does not actually commit these pages. The fix is to, if the initialization value of the data structures matches the default value of just committed memory (=0), do not do anything. Reviewed-by: jwilhelm, brutisso
author tschatzl
date Fri, 10 Oct 2014 15:51:58 +0200
parents 31e80afe3fed
children 92aa6797d639 52b4284cb496
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
1 /*
14441
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2022
2d4762ec74af 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.
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
4 *
2d4762ec74af 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
2d4762ec74af 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
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
7 * published by the Free Software Foundation.
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
8 *
2d4762ec74af 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
2d4762ec74af 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
2d4762ec74af 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
2d4762ec74af 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
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
13 * accompanied this code).
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
14 *
2d4762ec74af 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
2d4762ec74af 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,
2d4762ec74af 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.
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
18 *
2d4762ec74af 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
2d4762ec74af 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
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
21 * questions.
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
22 *
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
23 */
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
24
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
25 #include "precompiled.hpp"
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
26
3960
f08d439fab8c 7089790: integrate bsd-port changes
never
parents: 3302
diff changeset
27 #if !defined(_WINDOWS) && !defined(__APPLE__)
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
28
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
29 #include <string.h>
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
30 #include <stdio.h>
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
31 #include <limits.h>
3302
2a3da7eaf4a6 7036747: 7017009 reappeared, problem with ElfStringTable
zgu
parents: 2022
diff changeset
32 #include <new>
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
33
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
34 #include "memory/allocation.inline.hpp"
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
35 #include "utilities/decoder.hpp"
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
36 #include "utilities/elfFile.hpp"
14441
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
37 #include "utilities/elfFuncDescTable.hpp"
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
38 #include "utilities/elfStringTable.hpp"
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
39 #include "utilities/elfSymbolTable.hpp"
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
40
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
41
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
42 ElfFile::ElfFile(const char* filepath) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
43 assert(filepath, "null file path");
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
44 memset(&m_elfHdr, 0, sizeof(m_elfHdr));
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
45 m_string_tables = NULL;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
46 m_symbol_tables = NULL;
14441
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
47 m_funcDesc_table = NULL;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
48 m_next = NULL;
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
49 m_status = NullDecoder::no_error;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
50
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
51 int len = strlen(filepath) + 1;
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 4803
diff changeset
52 m_filepath = (const char*)os::malloc(len * sizeof(char), mtInternal);
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
53 if (m_filepath != NULL) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
54 strcpy((char*)m_filepath, filepath);
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
55 m_file = fopen(filepath, "r");
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
56 if (m_file != NULL) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
57 load_tables();
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
58 } else {
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
59 m_status = NullDecoder::file_not_found;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
60 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
61 } else {
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
62 m_status = NullDecoder::out_of_memory;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
63 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
64 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
65
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
66 ElfFile::~ElfFile() {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
67 if (m_string_tables != NULL) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
68 delete m_string_tables;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
69 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
70
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
71 if (m_symbol_tables != NULL) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
72 delete m_symbol_tables;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
73 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
74
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
75 if (m_file != NULL) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
76 fclose(m_file);
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
77 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
78
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
79 if (m_filepath != NULL) {
3302
2a3da7eaf4a6 7036747: 7017009 reappeared, problem with ElfStringTable
zgu
parents: 2022
diff changeset
80 os::free((void*)m_filepath);
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
81 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
82
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
83 if (m_next != NULL) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
84 delete m_next;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
85 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
86 };
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
87
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
88
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
89 //Check elf header to ensure the file is valid.
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
90 bool ElfFile::is_elf_file(Elf_Ehdr& hdr) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
91 return (ELFMAG0 == hdr.e_ident[EI_MAG0] &&
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
92 ELFMAG1 == hdr.e_ident[EI_MAG1] &&
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
93 ELFMAG2 == hdr.e_ident[EI_MAG2] &&
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
94 ELFMAG3 == hdr.e_ident[EI_MAG3] &&
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
95 ELFCLASSNONE != hdr.e_ident[EI_CLASS] &&
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
96 ELFDATANONE != hdr.e_ident[EI_DATA]);
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
97 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
98
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
99 bool ElfFile::load_tables() {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
100 assert(m_file, "file not open");
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
101 assert(!NullDecoder::is_error(m_status), "already in error");
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
102
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
103 // read elf file header
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
104 if (fread(&m_elfHdr, sizeof(m_elfHdr), 1, m_file) != 1) {
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
105 m_status = NullDecoder::file_invalid;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
106 return false;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
107 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
108
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
109 if (!is_elf_file(m_elfHdr)) {
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
110 m_status = NullDecoder::file_invalid;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
111 return false;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
112 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
113
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
114 // walk elf file's section headers, and load string tables
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
115 Elf_Shdr shdr;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
116 if (!fseek(m_file, m_elfHdr.e_shoff, SEEK_SET)) {
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
117 if (NullDecoder::is_error(m_status)) return false;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
118
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
119 for (int index = 0; index < m_elfHdr.e_shnum; index ++) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
120 if (fread((void*)&shdr, sizeof(Elf_Shdr), 1, m_file) != 1) {
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
121 m_status = NullDecoder::file_invalid;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
122 return false;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
123 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
124 if (shdr.sh_type == SHT_STRTAB) {
14441
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
125 // string tables
3302
2a3da7eaf4a6 7036747: 7017009 reappeared, problem with ElfStringTable
zgu
parents: 2022
diff changeset
126 ElfStringTable* table = new (std::nothrow) ElfStringTable(m_file, shdr, index);
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
127 if (table == NULL) {
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
128 m_status = NullDecoder::out_of_memory;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
129 return false;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
130 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
131 add_string_table(table);
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
132 } else if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
14441
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
133 // symbol tables
3302
2a3da7eaf4a6 7036747: 7017009 reappeared, problem with ElfStringTable
zgu
parents: 2022
diff changeset
134 ElfSymbolTable* table = new (std::nothrow) ElfSymbolTable(m_file, shdr);
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
135 if (table == NULL) {
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
136 m_status = NullDecoder::out_of_memory;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
137 return false;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
138 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
139 add_symbol_table(table);
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
140 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
141 }
14441
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
142
17803
31e80afe3fed 8035647: PPC64: Support for elf v2 abi.
goetz
parents: 17795
diff changeset
143 #if defined(PPC64) && !defined(ABI_ELFv2)
14441
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
144 // Now read the .opd section wich contains the PPC64 function descriptor table.
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
145 // The .opd section is only available on PPC64 (see for example:
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
146 // http://refspecs.linuxfoundation.org/LSB_3.1.1/LSB-Core-PPC64/LSB-Core-PPC64/specialsections.html)
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
147 // so this code should do no harm on other platforms but because of performance reasons we only
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
148 // execute it on PPC64 platforms.
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
149 // Notice that we can only find the .opd section after we have successfully read in the string
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
150 // tables in the previous loop, because we need to query the name of each section which is
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
151 // contained in one of the string tables (i.e. the one with the index m_elfHdr.e_shstrndx).
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
152
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
153 // Reset the file pointer
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
154 if (fseek(m_file, m_elfHdr.e_shoff, SEEK_SET)) {
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
155 m_status = NullDecoder::file_invalid;
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
156 return false;
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
157 }
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
158 for (int index = 0; index < m_elfHdr.e_shnum; index ++) {
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
159 if (fread((void*)&shdr, sizeof(Elf_Shdr), 1, m_file) != 1) {
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
160 m_status = NullDecoder::file_invalid;
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
161 return false;
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
162 }
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
163 if (m_elfHdr.e_shstrndx != SHN_UNDEF && shdr.sh_type == SHT_PROGBITS) {
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
164 ElfStringTable* string_table = get_string_table(m_elfHdr.e_shstrndx);
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
165 if (string_table == NULL) {
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
166 m_status = NullDecoder::file_invalid;
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
167 return false;
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
168 }
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
169 char buf[8]; // '8' is enough because we only want to read ".opd"
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
170 if (string_table->string_at(shdr.sh_name, buf, sizeof(buf)) && !strncmp(".opd", buf, 4)) {
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
171 m_funcDesc_table = new (std::nothrow) ElfFuncDescTable(m_file, shdr, index);
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
172 if (m_funcDesc_table == NULL) {
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
173 m_status = NullDecoder::out_of_memory;
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
174 return false;
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
175 }
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
176 break;
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
177 }
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
178 }
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
179 }
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
180 #endif
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
181
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
182 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
183 return true;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
184 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
185
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
186 bool ElfFile::decode(address addr, char* buf, int buflen, int* offset) {
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
187 // something already went wrong, just give up
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
188 if (NullDecoder::is_error(m_status)) {
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
189 return false;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
190 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
191 ElfSymbolTable* symbol_table = m_symbol_tables;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
192 int string_table_index;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
193 int pos_in_string_table;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
194 int off = INT_MAX;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
195 bool found_symbol = false;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
196 while (symbol_table != NULL) {
14441
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
197 if (symbol_table->lookup(addr, &string_table_index, &pos_in_string_table, &off, m_funcDesc_table)) {
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
198 found_symbol = true;
14441
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
199 break;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
200 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
201 symbol_table = symbol_table->m_next;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
202 }
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
203 if (!found_symbol) return false;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
204
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
205 ElfStringTable* string_table = get_string_table(string_table_index);
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
206
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
207 if (string_table == NULL) {
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
208 m_status = NullDecoder::file_invalid;
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
209 return false;
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
210 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
211 if (offset) *offset = off;
4803
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
212
d7e3846464d0 7071311: Decoder enhancement
zgu
parents: 3960
diff changeset
213 return string_table->string_at(pos_in_string_table, buf, buflen);
2022
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
214 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
215
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
216
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
217 void ElfFile::add_symbol_table(ElfSymbolTable* table) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
218 if (m_symbol_tables == NULL) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
219 m_symbol_tables = table;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
220 } else {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
221 table->m_next = m_symbol_tables;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
222 m_symbol_tables = table;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
223 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
224 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
225
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
226 void ElfFile::add_string_table(ElfStringTable* table) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
227 if (m_string_tables == NULL) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
228 m_string_tables = table;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
229 } else {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
230 table->m_next = m_string_tables;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
231 m_string_tables = table;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
232 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
233 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
234
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
235 ElfStringTable* ElfFile::get_string_table(int index) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
236 ElfStringTable* p = m_string_tables;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
237 while (p != NULL) {
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
238 if (p->index() == index) return p;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
239 p = p->m_next;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
240 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
241 return NULL;
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
242 }
2d4762ec74af 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
243
8710
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
244 #ifdef LINUX
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
245 bool ElfFile::specifies_noexecstack() {
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
246 Elf_Phdr phdr;
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
247 if (!m_file) return true;
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
248
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
249 if (!fseek(m_file, m_elfHdr.e_phoff, SEEK_SET)) {
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
250 for (int index = 0; index < m_elfHdr.e_phnum; index ++) {
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
251 if (fread((void*)&phdr, sizeof(Elf_Phdr), 1, m_file) != 1) {
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
252 m_status = NullDecoder::file_invalid;
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
253 return false;
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
254 }
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
255 if (phdr.p_type == PT_GNU_STACK) {
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
256 if (phdr.p_flags == (PF_R | PF_W)) {
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
257 return true;
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
258 } else {
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
259 return false;
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
260 }
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
261 }
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
262 }
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
263 }
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
264 return false;
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
265 }
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
266 #endif
9058789475af 7107135: Stack guard pages are no more protected after loading a shared library with executable stack
iklam
parents: 6842
diff changeset
267
14441
e7cbc95179c4 8019929: PPC64 (part 107): Extend ELF-decoder to support PPC64 function descriptor tables
simonis
parents: 8710
diff changeset
268 #endif // !_WINDOWS && !__APPLE__