aboutsummaryrefslogtreecommitdiff
path: root/src/share/vm/utilities/elfFile.cpp
blob: b0d57e13ae2f1ecd4dc3805aef2d3dc3be0d197b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#include "precompiled.hpp"

#if !defined(_WINDOWS) && !defined(__APPLE__)

#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <new>

#include "memory/allocation.inline.hpp"
#include "utilities/decoder.hpp"
#include "utilities/elfFile.hpp"
#include "utilities/elfFuncDescTable.hpp"
#include "utilities/elfStringTable.hpp"
#include "utilities/elfSymbolTable.hpp"


ElfFile::ElfFile(const char* filepath) {
  assert(filepath, "null file path");
  memset(&m_elfHdr, 0, sizeof(m_elfHdr));
  m_string_tables = NULL;
  m_symbol_tables = NULL;
  m_funcDesc_table = NULL;
  m_next = NULL;
  m_status = NullDecoder::no_error;

  int len = strlen(filepath) + 1;
  m_filepath = (const char*)os::malloc(len * sizeof(char), mtInternal);
  if (m_filepath != NULL) {
    strcpy((char*)m_filepath, filepath);
    m_file = fopen(filepath, "r");
    if (m_file != NULL) {
      load_tables();
    } else {
      m_status = NullDecoder::file_not_found;
    }
  } else {
    m_status = NullDecoder::out_of_memory;
  }
}

ElfFile::~ElfFile() {
  if (m_string_tables != NULL) {
    delete m_string_tables;
  }

  if (m_symbol_tables != NULL) {
    delete m_symbol_tables;
  }

  if (m_file != NULL) {
    fclose(m_file);
  }

  if (m_filepath != NULL) {
    os::free((void*)m_filepath);
  }

  if (m_next != NULL) {
    delete m_next;
  }
};


//Check elf header to ensure the file is valid.
bool ElfFile::is_elf_file(Elf_Ehdr& hdr) {
  return (ELFMAG0 == hdr.e_ident[EI_MAG0] &&
      ELFMAG1 == hdr.e_ident[EI_MAG1] &&
      ELFMAG2 == hdr.e_ident[EI_MAG2] &&
      ELFMAG3 == hdr.e_ident[EI_MAG3] &&
      ELFCLASSNONE != hdr.e_ident[EI_CLASS] &&
      ELFDATANONE != hdr.e_ident[EI_DATA]);
}

bool ElfFile::load_tables() {
  assert(m_file, "file not open");
  assert(!NullDecoder::is_error(m_status), "already in error");

  // read elf file header
  if (fread(&m_elfHdr, sizeof(m_elfHdr), 1, m_file) != 1) {
    m_status = NullDecoder::file_invalid;
    return false;
  }

  if (!is_elf_file(m_elfHdr)) {
    m_status = NullDecoder::file_invalid;
    return false;
  }

  // walk elf file's section headers, and load string tables
  Elf_Shdr shdr;
  if (!fseek(m_file, m_elfHdr.e_shoff, SEEK_SET)) {
    if (NullDecoder::is_error(m_status)) return false;

    for (int index = 0; index < m_elfHdr.e_shnum; index ++) {
      if (fread((void*)&shdr, sizeof(Elf_Shdr), 1, m_file) != 1) {
        m_status = NullDecoder::file_invalid;
        return false;
      }
      if (shdr.sh_type == SHT_STRTAB) {
        // string tables
        ElfStringTable* table = new (std::nothrow) ElfStringTable(m_file, shdr, index);
        if (table == NULL) {
          m_status = NullDecoder::out_of_memory;
          return false;
        }
        add_string_table(table);
      } else if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
        // symbol tables
        ElfSymbolTable* table = new (std::nothrow) ElfSymbolTable(m_file, shdr);
        if (table == NULL) {
          m_status = NullDecoder::out_of_memory;
          return false;
        }
        add_symbol_table(table);
      }
    }

#if defined(PPC64) && !defined(ABI_ELFv2)
    // Now read the .opd section wich contains the PPC64 function descriptor table.
    // The .opd section is only available on PPC64 (see for example:
    // http://refspecs.linuxfoundation.org/LSB_3.1.1/LSB-Core-PPC64/LSB-Core-PPC64/specialsections.html)
    // so this code should do no harm on other platforms but because of performance reasons we only
    // execute it on PPC64 platforms.
    // Notice that we can only find the .opd section after we have successfully read in the string
    // tables in the previous loop, because we need to query the name of each section which is
    // contained in one of the string tables (i.e. the one with the index m_elfHdr.e_shstrndx).

    // Reset the file pointer
    if (fseek(m_file, m_elfHdr.e_shoff, SEEK_SET)) {
      m_status = NullDecoder::file_invalid;
      return false;
    }
    for (int index = 0; index < m_elfHdr.e_shnum; index ++) {
      if (fread((void*)&shdr, sizeof(Elf_Shdr), 1, m_file) != 1) {
        m_status = NullDecoder::file_invalid;
        return false;
      }
      if (m_elfHdr.e_shstrndx != SHN_UNDEF && shdr.sh_type == SHT_PROGBITS) {
        ElfStringTable* string_table = get_string_table(m_elfHdr.e_shstrndx);
        if (string_table == NULL) {
          m_status = NullDecoder::file_invalid;
          return false;
        }
        char buf[8]; // '8' is enough because we only want to read ".opd"
        if (string_table->string_at(shdr.sh_name, buf, sizeof(buf)) && !strncmp(".opd", buf, 4)) {
          m_funcDesc_table = new (std::nothrow) ElfFuncDescTable(m_file, shdr, index);
          if (m_funcDesc_table == NULL) {
            m_status = NullDecoder::out_of_memory;
            return false;
          }
          break;
        }
      }
    }
#endif

  }
  return true;
}

bool ElfFile::decode(address addr, char* buf, int buflen, int* offset) {
  // something already went wrong, just give up
  if (NullDecoder::is_error(m_status)) {
    return false;
  }
  ElfSymbolTable* symbol_table = m_symbol_tables;
  int string_table_index;
  int pos_in_string_table;
  int off = INT_MAX;
  bool found_symbol = false;
  while (symbol_table != NULL) {
    if (symbol_table->lookup(addr, &string_table_index, &pos_in_string_table, &off, m_funcDesc_table)) {
      found_symbol = true;
      break;
    }
    symbol_table = symbol_table->m_next;
  }
  if (!found_symbol) return false;

  ElfStringTable* string_table = get_string_table(string_table_index);

  if (string_table == NULL) {
    m_status = NullDecoder::file_invalid;
    return false;
  }
  if (offset) *offset = off;

  return string_table->string_at(pos_in_string_table, buf, buflen);
}


void ElfFile::add_symbol_table(ElfSymbolTable* table) {
  if (m_symbol_tables == NULL) {
    m_symbol_tables = table;
  } else {
    table->m_next = m_symbol_tables;
    m_symbol_tables = table;
  }
}

void ElfFile::add_string_table(ElfStringTable* table) {
  if (m_string_tables == NULL) {
    m_string_tables = table;
  } else {
    table->m_next = m_string_tables;
    m_string_tables = table;
  }
}

ElfStringTable* ElfFile::get_string_table(int index) {
  ElfStringTable* p = m_string_tables;
  while (p != NULL) {
    if (p->index() == index) return p;
    p = p->m_next;
  }
  return NULL;
}

#ifdef LINUX
bool ElfFile::specifies_noexecstack() {
  Elf_Phdr phdr;
  if (!m_file)  return true;

  if (!fseek(m_file, m_elfHdr.e_phoff, SEEK_SET)) {
    for (int index = 0; index < m_elfHdr.e_phnum; index ++) {
      if (fread((void*)&phdr, sizeof(Elf_Phdr), 1, m_file) != 1) {
        m_status = NullDecoder::file_invalid;
        return false;
      }
      if (phdr.p_type == PT_GNU_STACK) {
        if (phdr.p_flags == (PF_R | PF_W))  {
          return true;
        } else {
          return false;
        }
      }
    }
  }
// x86 defaults to execstack, AARCH64 defaults to noexecstack
#ifdef AARCH64
  return true;
#else
  return false;
#endif
}
#endif

#endif // !_WINDOWS && !__APPLE__