blob: b5229b579c3bb9a8721e9a6153b9eb553c2d27c5 [file] [log] [blame]
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_ELF_FILE_H_
18#define ART_RUNTIME_ELF_FILE_H_
Brian Carlstrom700c8d32012-11-05 10:42:02 -080019
Ian Rogersd4c4d952014-10-16 20:31:53 -070020#include <memory>
Tong Shen62d1ca32014-09-03 17:24:56 -070021#include <string>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080022
Ian Rogersd4c4d952014-10-16 20:31:53 -070023#include "base/macros.h"
24// Explicitly include our own elf.h to avoid Linux and other dependencies.
25#include "./elf.h"
26#include "os.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080027
28namespace art {
David Srbecky533c2072015-04-22 12:20:22 +010029template <typename ElfTypes>
Ian Rogersd4c4d952014-10-16 20:31:53 -070030class ElfFileImpl;
31
32// Explicitly instantiated in elf_file.cc
David Srbecky533c2072015-04-22 12:20:22 +010033typedef ElfFileImpl<ElfTypes32> ElfFileImpl32;
34typedef ElfFileImpl<ElfTypes64> ElfFileImpl64;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080035
36// Used for compile time and runtime for ElfFile access. Because of
37// the need for use at runtime, cannot directly use LLVM classes such as
38// ELFObjectFile.
39class ElfFile {
40 public:
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080041 static ElfFile* Open(File* file,
42 bool writable,
43 bool program_header_only,
44 bool low_4gb,
45 std::string* error_msg,
Igor Murashkin46774762014-10-22 11:37:02 -070046 uint8_t* requested_base = nullptr); // TODO: move arg to before error_msg.
Alex Light3470ab42014-06-18 10:35:45 -070047 // Open with specific mmap flags, Always maps in the whole file, not just the
48 // program header sections.
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080049 static ElfFile* Open(File* file,
50 int mmap_prot,
51 int mmap_flags,
52 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053 ~ElfFile();
54
55 // Load segments into memory based on PT_LOAD program headers
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080056 bool Load(bool executable, bool low_4gb, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080057
Ian Rogers13735952014-10-08 12:43:28 -070058 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const;
Tong Shen62d1ca32014-09-03 17:24:56 -070059
60 size_t Size() const;
61
Igor Murashkin46774762014-10-22 11:37:02 -070062 // The start of the memory map address range for this ELF file.
Ian Rogers13735952014-10-08 12:43:28 -070063 uint8_t* Begin() const;
Tong Shen62d1ca32014-09-03 17:24:56 -070064
Igor Murashkin46774762014-10-22 11:37:02 -070065 // The end of the memory map address range for this ELF file.
Ian Rogers13735952014-10-08 12:43:28 -070066 uint8_t* End() const;
Tong Shen62d1ca32014-09-03 17:24:56 -070067
68 const File& GetFile() const;
69
Alex Light0eb76d22015-08-11 18:03:47 -070070 bool GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) const;
Tong Shen62d1ca32014-09-03 17:24:56 -070071
72 uint64_t FindSymbolAddress(unsigned section_type,
73 const std::string& symbol_name,
74 bool build_map);
75
Vladimir Marko3fc99032015-05-13 19:06:30 +010076 bool GetLoadedSize(size_t* size, std::string* error_msg) const;
Tong Shen62d1ca32014-09-03 17:24:56 -070077
78 // Strip an ELF file of unneeded debugging information.
79 // Returns true on success, false on failure.
80 static bool Strip(File* file, std::string* error_msg);
81
82 // Fixup an ELF file so that that oat header will be loaded at oat_begin.
83 // Returns true on success, false on failure.
Andreas Gampe3c54b002015-04-07 16:09:30 -070084 static bool Fixup(File* file, uint64_t oat_data_begin);
Tong Shen62d1ca32014-09-03 17:24:56 -070085
Andreas Gampe3c54b002015-04-07 16:09:30 -070086 bool Fixup(uint64_t base_address);
Tong Shen62d1ca32014-09-03 17:24:56 -070087
Ian Rogersd4c4d952014-10-16 20:31:53 -070088 bool Is64Bit() const {
89 return elf64_.get() != nullptr;
90 }
91
92 ElfFileImpl32* GetImpl32() const {
93 return elf32_.get();
94 }
95
96 ElfFileImpl64* GetImpl64() const {
97 return elf64_.get();
98 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070099
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800100 private:
Tong Shen62d1ca32014-09-03 17:24:56 -0700101 explicit ElfFile(ElfFileImpl32* elf32);
102 explicit ElfFile(ElfFileImpl64* elf64);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800103
Ian Rogersd4c4d952014-10-16 20:31:53 -0700104 const std::unique_ptr<ElfFileImpl32> elf32_;
105 const std::unique_ptr<ElfFileImpl64> elf64_;
106
107 DISALLOW_COPY_AND_ASSIGN(ElfFile);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800108};
109
110} // namespace art
111
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700112#endif // ART_RUNTIME_ELF_FILE_H_