1/*
2 * Copyright (c) 1997, 2019, 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 */
24
25#ifndef SHARE_UTILITIES_ELFFILE_HPP
26#define SHARE_UTILITIES_ELFFILE_HPP
27
28#if !defined(_WINDOWS) && !defined(__APPLE__) && !defined(_AIX)
29
30#if defined(__OpenBSD__)
31#include <sys/exec_elf.h>
32#else
33#include <elf.h>
34#endif
35#include <stdio.h>
36
37#ifdef _LP64
38
39typedef Elf64_Half Elf_Half;
40typedef Elf64_Word Elf_Word;
41typedef Elf64_Off Elf_Off;
42typedef Elf64_Addr Elf_Addr;
43
44typedef Elf64_Ehdr Elf_Ehdr;
45typedef Elf64_Shdr Elf_Shdr;
46typedef Elf64_Phdr Elf_Phdr;
47typedef Elf64_Sym Elf_Sym;
48
49#if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)
50#define ELF_ST_TYPE ELF64_ST_TYPE
51#endif
52
53#else
54
55typedef Elf32_Half Elf_Half;
56typedef Elf32_Word Elf_Word;
57typedef Elf32_Off Elf_Off;
58typedef Elf32_Addr Elf_Addr;
59
60typedef Elf32_Ehdr Elf_Ehdr;
61typedef Elf32_Shdr Elf_Shdr;
62typedef Elf32_Phdr Elf_Phdr;
63typedef Elf32_Sym Elf_Sym;
64
65#if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)
66#define ELF_ST_TYPE ELF32_ST_TYPE
67#endif
68#endif
69
70#include "globalDefinitions.hpp"
71#include "memory/allocation.hpp"
72#include "utilities/decoder.hpp"
73
74class ElfStringTable;
75class ElfSymbolTable;
76class ElfFuncDescTable;
77
78// ELF section, may or may not have cached data
79class ElfSection {
80private:
81 Elf_Shdr _section_hdr;
82 void* _section_data;
83 NullDecoder::decoder_status _stat;
84public:
85 ElfSection(FILE* fd, const Elf_Shdr& hdr);
86 ~ElfSection();
87
88 NullDecoder::decoder_status status() const { return _stat; }
89
90 const Elf_Shdr* section_header() const { return &_section_hdr; }
91 const void* section_data() const { return (const void*)_section_data; }
92private:
93 // load this section.
94 // it return no_error, when it fails to cache the section data due to lack of memory
95 NullDecoder::decoder_status load_section(FILE* const file, const Elf_Shdr& hdr);
96};
97
98class FileReader : public StackObj {
99protected:
100 FILE* const _fd;
101public:
102 FileReader(FILE* const fd) : _fd(fd) {};
103 bool read(void* buf, size_t size);
104 int read_buffer(void* buf, size_t size);
105 bool set_position(long offset);
106};
107
108// Mark current position, so we can get back to it after
109// reads.
110class MarkedFileReader : public FileReader {
111private:
112 long _marked_pos;
113public:
114 MarkedFileReader(FILE* const fd);
115 ~MarkedFileReader();
116
117 bool has_mark() const { return _marked_pos >= 0; }
118};
119
120// ElfFile is basically an elf file parser, which can lookup the symbol
121// that is the nearest to the given address.
122// Beware, this code is called from vm error reporting code, when vm is already
123// in "error" state, so there are scenarios, lookup will fail. We want this
124// part of code to be very defensive, and bait out if anything went wrong.
125class ElfFile: public CHeapObj<mtInternal> {
126 friend class ElfDecoder;
127
128private:
129 // link ElfFiles
130 ElfFile* _next;
131
132 // Elf file
133 char* _filepath;
134 FILE* _file;
135
136 // Elf header
137 Elf_Ehdr _elfHdr;
138
139 // symbol tables
140 ElfSymbolTable* _symbol_tables;
141
142 // regular string tables
143 ElfStringTable* _string_tables;
144
145 // section header string table, used for finding section name
146 ElfStringTable* _shdr_string_table;
147
148 // function descriptors table
149 ElfFuncDescTable* _funcDesc_table;
150
151 NullDecoder::decoder_status _status;
152
153public:
154 ElfFile(const char* filepath);
155 ~ElfFile();
156
157 bool decode(address addr, char* buf, int buflen, int* offset);
158
159 const char* filepath() const {
160 return _filepath;
161 }
162
163 bool same_elf_file(const char* filepath) const {
164 assert(filepath != NULL, "null file path");
165 return (_filepath != NULL && !strcmp(filepath, _filepath));
166 }
167
168 NullDecoder::decoder_status get_status() const {
169 return _status;
170 }
171
172 // Returns true if the elf file is marked NOT to require an executable stack,
173 // or if the file could not be opened.
174 // Returns false if the elf file requires an executable stack, the stack flag
175 // is not set at all, or if the file can not be read.
176 // On systems other than linux it always returns false.
177 static bool specifies_noexecstack(const char* filepath) NOT_LINUX({ return false; });
178private:
179 // sanity check, if the file is a real elf file
180 static bool is_elf_file(Elf_Ehdr&);
181
182 // parse this elf file
183 NullDecoder::decoder_status parse_elf(const char* filename);
184
185 // load string, symbol and function descriptor tables from the elf file
186 NullDecoder::decoder_status load_tables();
187
188 ElfFile* next() const { return _next; }
189 void set_next(ElfFile* file) { _next = file; }
190
191 // find a section by name, return section index
192 // if there is no such section, return -1
193 int section_by_name(const char* name, Elf_Shdr& hdr);
194
195 // string tables are stored in a linked list
196 void add_string_table(ElfStringTable* table);
197
198 // symbol tables are stored in a linked list
199 void add_symbol_table(ElfSymbolTable* table);
200
201 // return a string table at specified section index
202 ElfStringTable* get_string_table(int index);
203
204
205 FILE* const fd() const { return _file; }
206
207 // Cleanup string, symbol and function descriptor tables
208 void cleanup_tables();
209
210public:
211 // For whitebox test
212 static bool _do_not_cache_elf_section;
213};
214
215#endif // !_WINDOWS && !__APPLE__
216
217#endif // SHARE_UTILITIES_ELFFILE_HPP
218