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