1 /*
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
19 #include "base/unix_file/fd_file.h"
20 #include "elf_file.h"
21
22 namespace art {
23
GetOatDataAddress(ElfFile * elf_file)24 uintptr_t ElfWriter::GetOatDataAddress(ElfFile* elf_file) {
25 uintptr_t oatdata_address = elf_file->FindSymbolAddress(SHT_DYNSYM,
26 "oatdata",
27 false);
28 CHECK_NE(0U, oatdata_address);
29 return oatdata_address;
30 }
31
GetOatElfInformation(File * file,size_t * oat_loaded_size,size_t * oat_data_offset)32 void ElfWriter::GetOatElfInformation(File* file,
33 size_t* oat_loaded_size,
34 size_t* oat_data_offset) {
35 std::string error_msg;
36 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file,
37 false,
38 false,
39 /*low_4gb*/false,
40 &error_msg));
41 CHECK(elf_file.get() != nullptr) << error_msg;
42
43 bool success = elf_file->GetLoadedSize(oat_loaded_size, &error_msg);
44 CHECK(success) << error_msg;
45 CHECK_NE(0U, *oat_loaded_size);
46 *oat_data_offset = GetOatDataAddress(elf_file.get());
47 CHECK_NE(0U, *oat_data_offset);
48 }
49
Fixup(File * file,uintptr_t oat_data_begin)50 bool ElfWriter::Fixup(File* file, uintptr_t oat_data_begin) {
51 std::string error_msg;
52 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, /*low_4gb*/false, &error_msg));
53 CHECK(elf_file.get() != nullptr) << error_msg;
54
55 // Lookup "oatdata" symbol address.
56 uintptr_t oatdata_address = ElfWriter::GetOatDataAddress(elf_file.get());
57 uintptr_t base_address = oat_data_begin - oatdata_address;
58
59 return elf_file->Fixup(base_address);
60 }
61
62 } // namespace art
63