blob: 0c0609009d5f4c91196fcf7e69645e346e01ba26 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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
17#include "elf_writer.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "base/unix_file/fd_file.h"
21#include "class_linker.h"
22#include "dex_file-inl.h"
23#include "dex_method_iterator.h"
24#include "driver/compiler_driver.h"
25#include "elf_file.h"
26#include "invoke_type.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "mirror/object-inl.h"
28#include "oat.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070029#include "scoped_thread_state_change-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030
31namespace art {
32
Tong Shen62d1ca32014-09-03 17:24:56 -070033uintptr_t ElfWriter::GetOatDataAddress(ElfFile* elf_file) {
34 uintptr_t oatdata_address = elf_file->FindSymbolAddress(SHT_DYNSYM,
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000035 "oatdata",
36 false);
Brian Carlstrom7940e442013-07-12 13:46:57 -070037 CHECK_NE(0U, oatdata_address);
38 return oatdata_address;
39}
40
41void ElfWriter::GetOatElfInformation(File* file,
Vladimir Marko3fc99032015-05-13 19:06:30 +010042 size_t* oat_loaded_size,
43 size_t* oat_data_offset) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070044 std::string error_msg;
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080045 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file,
46 false,
47 false,
48 /*low_4gb*/false,
49 &error_msg));
Alex Light3470ab42014-06-18 10:35:45 -070050 CHECK(elf_file.get() != nullptr) << error_msg;
Brian Carlstrom7940e442013-07-12 13:46:57 -070051
Vladimir Marko3fc99032015-05-13 19:06:30 +010052 bool success = elf_file->GetLoadedSize(oat_loaded_size, &error_msg);
53 CHECK(success) << error_msg;
54 CHECK_NE(0U, *oat_loaded_size);
55 *oat_data_offset = GetOatDataAddress(elf_file.get());
56 CHECK_NE(0U, *oat_data_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070057}
58
Tong Shen62d1ca32014-09-03 17:24:56 -070059bool ElfWriter::Fixup(File* file, uintptr_t oat_data_begin) {
60 std::string error_msg;
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080061 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, /*low_4gb*/false, &error_msg));
Tong Shen62d1ca32014-09-03 17:24:56 -070062 CHECK(elf_file.get() != nullptr) << error_msg;
63
64 // Lookup "oatdata" symbol address.
65 uintptr_t oatdata_address = ElfWriter::GetOatDataAddress(elf_file.get());
66 uintptr_t base_address = oat_data_begin - oatdata_address;
67
68 return elf_file->Fixup(base_address);
69}
70
Brian Carlstrom7940e442013-07-12 13:46:57 -070071} // namespace art