blob: a7f3056cae2c42a487c0c754577eff936551779d [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
Tong Shen62d1ca32014-09-03 17:24:56 -070020#include <string>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080021
Brian Carlstrom700c8d32012-11-05 10:42:02 -080022#include "base/unix_file/fd_file.h"
Tong Shen62d1ca32014-09-03 17:24:56 -070023#include "elf_file_impl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080024
25namespace art {
26
27// Used for compile time and runtime for ElfFile access. Because of
28// the need for use at runtime, cannot directly use LLVM classes such as
29// ELFObjectFile.
30class ElfFile {
31 public:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070032 static ElfFile* Open(File* file, bool writable, bool program_header_only, std::string* error_msg);
Alex Light3470ab42014-06-18 10:35:45 -070033 // Open with specific mmap flags, Always maps in the whole file, not just the
34 // program header sections.
35 static ElfFile* Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080036 ~ElfFile();
37
Tong Shen62d1ca32014-09-03 17:24:56 -070038 const bool is_elf64_;
39
Brian Carlstrom700c8d32012-11-05 10:42:02 -080040 // Load segments into memory based on PT_LOAD program headers
Ian Rogers8d31bbd2013-10-13 10:44:14 -070041 bool Load(bool executable, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080042
Ian Rogers13735952014-10-08 12:43:28 -070043 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const;
Tong Shen62d1ca32014-09-03 17:24:56 -070044
45 size_t Size() const;
46
Ian Rogers13735952014-10-08 12:43:28 -070047 uint8_t* Begin() const;
Tong Shen62d1ca32014-09-03 17:24:56 -070048
Ian Rogers13735952014-10-08 12:43:28 -070049 uint8_t* End() const;
Tong Shen62d1ca32014-09-03 17:24:56 -070050
51 const File& GetFile() const;
52
53 bool GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size);
54
55 uint64_t FindSymbolAddress(unsigned section_type,
56 const std::string& symbol_name,
57 bool build_map);
58
59 size_t GetLoadedSize() const;
60
61 // Strip an ELF file of unneeded debugging information.
62 // Returns true on success, false on failure.
63 static bool Strip(File* file, std::string* error_msg);
64
65 // Fixup an ELF file so that that oat header will be loaded at oat_begin.
66 // Returns true on success, false on failure.
67 static bool Fixup(File* file, uintptr_t oat_data_begin);
68
69 bool Fixup(uintptr_t base_address);
70
71 ElfFileImpl32* GetImpl32() const;
72 ElfFileImpl64* GetImpl64() const;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070073
Brian Carlstrom700c8d32012-11-05 10:42:02 -080074 private:
Tong Shen62d1ca32014-09-03 17:24:56 -070075 explicit ElfFile(ElfFileImpl32* elf32);
76 explicit ElfFile(ElfFileImpl64* elf64);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080077
Tong Shen62d1ca32014-09-03 17:24:56 -070078 union ElfFileContainer {
79 ElfFileImpl32* elf32_;
80 ElfFileImpl64* elf64_;
81 } elf_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080082};
83
84} // namespace art
85
Brian Carlstromfc0e3212013-07-17 14:40:12 -070086#endif // ART_RUNTIME_ELF_FILE_H_