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