blob: 41c54bce7b027aeb4ee5f43ef28ec83326d82940 [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 {
Ian Rogersd4c4d952014-10-16 20:31:53 -070029template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
30 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
31 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
32class ElfFileImpl;
33
34// Explicitly instantiated in elf_file.cc
35typedef ElfFileImpl<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Word, Elf32_Sword,
36 Elf32_Addr, Elf32_Sym, Elf32_Rel, Elf32_Rela, Elf32_Dyn, Elf32_Off> ElfFileImpl32;
37typedef ElfFileImpl<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Word, Elf64_Sword,
38 Elf64_Addr, Elf64_Sym, Elf64_Rel, Elf64_Rela, Elf64_Dyn, Elf64_Off> ElfFileImpl64;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080039
40// Used for compile time and runtime for ElfFile access. Because of
41// the need for use at runtime, cannot directly use LLVM classes such as
42// ELFObjectFile.
43class ElfFile {
44 public:
Igor Murashkin46774762014-10-22 11:37:02 -070045 static ElfFile* Open(File* file, bool writable, bool program_header_only, std::string* error_msg,
46 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.
49 static ElfFile* Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080050 ~ElfFile();
51
52 // Load segments into memory based on PT_LOAD program headers
Ian Rogers8d31bbd2013-10-13 10:44:14 -070053 bool Load(bool executable, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080054
Ian Rogers13735952014-10-08 12:43:28 -070055 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const;
Tong Shen62d1ca32014-09-03 17:24:56 -070056
57 size_t Size() const;
58
Igor Murashkin46774762014-10-22 11:37:02 -070059 // The start of the memory map address range for this ELF file.
Ian Rogers13735952014-10-08 12:43:28 -070060 uint8_t* Begin() const;
Tong Shen62d1ca32014-09-03 17:24:56 -070061
Igor Murashkin46774762014-10-22 11:37:02 -070062 // The end of the memory map address range for this ELF file.
Ian Rogers13735952014-10-08 12:43:28 -070063 uint8_t* End() const;
Tong Shen62d1ca32014-09-03 17:24:56 -070064
65 const File& GetFile() const;
66
67 bool GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size);
68
69 uint64_t FindSymbolAddress(unsigned section_type,
70 const std::string& symbol_name,
71 bool build_map);
72
73 size_t GetLoadedSize() const;
74
75 // Strip an ELF file of unneeded debugging information.
76 // Returns true on success, false on failure.
77 static bool Strip(File* file, std::string* error_msg);
78
79 // Fixup an ELF file so that that oat header will be loaded at oat_begin.
80 // Returns true on success, false on failure.
81 static bool Fixup(File* file, uintptr_t oat_data_begin);
82
83 bool Fixup(uintptr_t base_address);
84
Ian Rogersd4c4d952014-10-16 20:31:53 -070085 bool Is64Bit() const {
86 return elf64_.get() != nullptr;
87 }
88
89 ElfFileImpl32* GetImpl32() const {
90 return elf32_.get();
91 }
92
93 ElfFileImpl64* GetImpl64() const {
94 return elf64_.get();
95 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070096
Brian Carlstrom700c8d32012-11-05 10:42:02 -080097 private:
Tong Shen62d1ca32014-09-03 17:24:56 -070098 explicit ElfFile(ElfFileImpl32* elf32);
99 explicit ElfFile(ElfFileImpl64* elf64);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800100
Ian Rogersd4c4d952014-10-16 20:31:53 -0700101 const std::unique_ptr<ElfFileImpl32> elf32_;
102 const std::unique_ptr<ElfFileImpl64> elf64_;
103
104 DISALLOW_COPY_AND_ASSIGN(ElfFile);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800105};
106
107} // namespace art
108
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700109#endif // ART_RUNTIME_ELF_FILE_H_