blob: 8516b5107749f09117536b8fdabb9fa1cff33daf [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"
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/os.h"
David Srbecky50928112019-03-22 17:06:28 +000025#include "elf/elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080026
27namespace art {
Vladimir Markoc09cd052018-08-23 16:36:36 +010028
29class MemMap;
30
David Srbecky533c2072015-04-22 12:20:22 +010031template <typename ElfTypes>
Ian Rogersd4c4d952014-10-16 20:31:53 -070032class ElfFileImpl;
33
34// Explicitly instantiated in elf_file.cc
Vladimir Marko4f990712021-07-14 12:45:13 +010035using ElfFileImpl32 = ElfFileImpl<ElfTypes32>;
36using ElfFileImpl64 = ElfFileImpl<ElfTypes64>;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080037
38// Used for compile time and runtime for ElfFile access. Because of
39// the need for use at runtime, cannot directly use LLVM classes such as
40// ELFObjectFile.
41class ElfFile {
42 public:
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080043 static ElfFile* Open(File* file,
44 bool writable,
45 bool program_header_only,
46 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +010047 /*out*/std::string* error_msg);
Alex Light3470ab42014-06-18 10:35:45 -070048 // Open with specific mmap flags, Always maps in the whole file, not just the
49 // program header sections.
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080050 static ElfFile* Open(File* file,
51 int mmap_prot,
52 int mmap_flags,
Vladimir Markoc09cd052018-08-23 16:36:36 +010053 /*out*/std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080054 ~ElfFile();
55
56 // Load segments into memory based on PT_LOAD program headers
Vladimir Markoc09cd052018-08-23 16:36:36 +010057 bool Load(File* file,
58 bool executable,
59 bool low_4gb,
60 /*inout*/MemMap* reservation,
61 /*out*/std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080062
Ian Rogers13735952014-10-08 12:43:28 -070063 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const;
Tong Shen62d1ca32014-09-03 17:24:56 -070064
65 size_t Size() const;
66
Igor Murashkin46774762014-10-22 11:37:02 -070067 // The start of the memory map address range for this ELF file.
Ian Rogers13735952014-10-08 12:43:28 -070068 uint8_t* Begin() const;
Tong Shen62d1ca32014-09-03 17:24:56 -070069
Igor Murashkin46774762014-10-22 11:37:02 -070070 // The end of the memory map address range for this ELF file.
Ian Rogers13735952014-10-08 12:43:28 -070071 uint8_t* End() const;
Tong Shen62d1ca32014-09-03 17:24:56 -070072
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070073 const std::string& GetFilePath() const;
Tong Shen62d1ca32014-09-03 17:24:56 -070074
Alex Light0eb76d22015-08-11 18:03:47 -070075 bool GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) const;
Tong Shen62d1ca32014-09-03 17:24:56 -070076
Jeff Hao24ec0282016-03-17 21:32:45 -070077 bool HasSection(const std::string& name) const;
78
Tong Shen62d1ca32014-09-03 17:24:56 -070079 uint64_t FindSymbolAddress(unsigned section_type,
80 const std::string& symbol_name,
81 bool build_map);
82
Vladimir Marko3fc99032015-05-13 19:06:30 +010083 bool GetLoadedSize(size_t* size, std::string* error_msg) const;
Tong Shen62d1ca32014-09-03 17:24:56 -070084
85 // Strip an ELF file of unneeded debugging information.
86 // Returns true on success, false on failure.
87 static bool Strip(File* file, std::string* error_msg);
88
Ian Rogersd4c4d952014-10-16 20:31:53 -070089 bool Is64Bit() const {
90 return elf64_.get() != nullptr;
91 }
92
93 ElfFileImpl32* GetImpl32() const {
94 return elf32_.get();
95 }
96
97 ElfFileImpl64* GetImpl64() const {
98 return elf64_.get();
99 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700100
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800101 private:
Tong Shen62d1ca32014-09-03 17:24:56 -0700102 explicit ElfFile(ElfFileImpl32* elf32);
103 explicit ElfFile(ElfFileImpl64* elf64);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800104
Ian Rogersd4c4d952014-10-16 20:31:53 -0700105 const std::unique_ptr<ElfFileImpl32> elf32_;
106 const std::unique_ptr<ElfFileImpl64> elf64_;
107
108 DISALLOW_COPY_AND_ASSIGN(ElfFile);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800109};
110
111} // namespace art
112
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700113#endif // ART_RUNTIME_ELF_FILE_H_