Path: blob/master/src/hotspot/share/utilities/elfFile.hpp
40949 views
/*1* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_UTILITIES_ELFFILE_HPP25#define SHARE_UTILITIES_ELFFILE_HPP2627#if !defined(_WINDOWS) && !defined(__APPLE__) && !defined(_AIX)2829#if defined(__OpenBSD__)30#include <sys/exec_elf.h>31#else32#include <elf.h>33#endif34#include <stdio.h>3536#ifdef _LP643738typedef Elf64_Half Elf_Half;39typedef Elf64_Word Elf_Word;40typedef Elf64_Off Elf_Off;41typedef Elf64_Addr Elf_Addr;4243typedef Elf64_Ehdr Elf_Ehdr;44typedef Elf64_Shdr Elf_Shdr;45typedef Elf64_Phdr Elf_Phdr;46typedef Elf64_Sym Elf_Sym;4748#if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)49#ifndef ELF_ST_TYPE50#define ELF_ST_TYPE ELF64_ST_TYPE51#endif52#endif5354#else5556typedef Elf32_Half Elf_Half;57typedef Elf32_Word Elf_Word;58typedef Elf32_Off Elf_Off;59typedef Elf32_Addr Elf_Addr;6061typedef Elf32_Ehdr Elf_Ehdr;62typedef Elf32_Shdr Elf_Shdr;63typedef Elf32_Phdr Elf_Phdr;64typedef Elf32_Sym Elf_Sym;6566#if !defined(_ALLBSD_SOURCE) || defined(__APPLE__)67#ifndef ELF_ST_TYPE68#define ELF_ST_TYPE ELF32_ST_TYPE69#endif70#endif71#endif7273#include "globalDefinitions.hpp"74#include "memory/allocation.hpp"75#include "utilities/decoder.hpp"7677class ElfStringTable;78class ElfSymbolTable;79class ElfFuncDescTable;8081// ELF section, may or may not have cached data82class ElfSection {83private:84Elf_Shdr _section_hdr;85void* _section_data;86NullDecoder::decoder_status _stat;87public:88ElfSection(FILE* fd, const Elf_Shdr& hdr);89~ElfSection();9091NullDecoder::decoder_status status() const { return _stat; }9293const Elf_Shdr* section_header() const { return &_section_hdr; }94const void* section_data() const { return (const void*)_section_data; }95private:96// load this section.97// it return no_error, when it fails to cache the section data due to lack of memory98NullDecoder::decoder_status load_section(FILE* const file, const Elf_Shdr& hdr);99};100101class FileReader : public StackObj {102protected:103FILE* const _fd;104public:105FileReader(FILE* const fd) : _fd(fd) {};106bool read(void* buf, size_t size);107int read_buffer(void* buf, size_t size);108bool set_position(long offset);109};110111// Mark current position, so we can get back to it after112// reads.113class MarkedFileReader : public FileReader {114private:115long _marked_pos;116public:117MarkedFileReader(FILE* const fd);118~MarkedFileReader();119120bool has_mark() const { return _marked_pos >= 0; }121};122123// ElfFile is basically an elf file parser, which can lookup the symbol124// that is the nearest to the given address.125// Beware, this code is called from vm error reporting code, when vm is already126// in "error" state, so there are scenarios, lookup will fail. We want this127// part of code to be very defensive, and bait out if anything went wrong.128class ElfFile: public CHeapObj<mtInternal> {129friend class ElfDecoder;130131private:132// link ElfFiles133ElfFile* _next;134135// Elf file136char* _filepath;137FILE* _file;138139// Elf header140Elf_Ehdr _elfHdr;141142// symbol tables143ElfSymbolTable* _symbol_tables;144145// regular string tables146ElfStringTable* _string_tables;147148// section header string table, used for finding section name149ElfStringTable* _shdr_string_table;150151// function descriptors table152ElfFuncDescTable* _funcDesc_table;153154NullDecoder::decoder_status _status;155156public:157ElfFile(const char* filepath);158~ElfFile();159160bool decode(address addr, char* buf, int buflen, int* offset);161162const char* filepath() const {163return _filepath;164}165166bool same_elf_file(const char* filepath) const {167assert(filepath != NULL, "null file path");168return (_filepath != NULL && !strcmp(filepath, _filepath));169}170171NullDecoder::decoder_status get_status() const {172return _status;173}174175// Returns true if the elf file is marked NOT to require an executable stack,176// or if the file could not be opened.177// Returns false if the elf file requires an executable stack, the stack flag178// is not set at all, or if the file can not be read.179// On systems other than linux it always returns false.180static bool specifies_noexecstack(const char* filepath) NOT_LINUX({ return false; });181private:182// sanity check, if the file is a real elf file183static bool is_elf_file(Elf_Ehdr&);184185// parse this elf file186NullDecoder::decoder_status parse_elf(const char* filename);187188// load string, symbol and function descriptor tables from the elf file189NullDecoder::decoder_status load_tables();190191ElfFile* next() const { return _next; }192void set_next(ElfFile* file) { _next = file; }193194// find a section by name, return section index195// if there is no such section, return -1196int section_by_name(const char* name, Elf_Shdr& hdr);197198// string tables are stored in a linked list199void add_string_table(ElfStringTable* table);200201// symbol tables are stored in a linked list202void add_symbol_table(ElfSymbolTable* table);203204// return a string table at specified section index205ElfStringTable* get_string_table(int index);206207208FILE* const fd() const { return _file; }209210// Cleanup string, symbol and function descriptor tables211void cleanup_tables();212213public:214// For whitebox test215static bool _do_not_cache_elf_section;216};217218#endif // !_WINDOWS && !__APPLE__219220#endif // SHARE_UTILITIES_ELFFILE_HPP221222223