blob: 60f76efed0eb7278dd72c041ebfe255b26b99184 [file] [log] [blame]
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -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_fixup.h"
18
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000019#include <inttypes.h>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000021
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070022#include "base/logging.h"
23#include "base/stringprintf.h"
24#include "elf_file.h"
25#include "elf_writer.h"
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070026
27namespace art {
28
29static const bool DEBUG_FIXUP = false;
30
31bool ElfFixup::Fixup(File* file, uintptr_t oat_data_begin) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070032 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070033 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070034 CHECK(elf_file.get() != nullptr) << error_msg;
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070035
36 // Lookup "oatdata" symbol address.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000037 Elf32_Addr oatdata_address = ElfWriter::GetOatDataAddress(elf_file.get());
38 Elf32_Off base_address = oat_data_begin - oatdata_address;
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070039
40 if (!FixupDynamic(*elf_file.get(), base_address)) {
41 LOG(WARNING) << "Failed fo fixup .dynamic in " << file->GetPath();
42 return false;
43 }
44 if (!FixupSectionHeaders(*elf_file.get(), base_address)) {
45 LOG(WARNING) << "Failed fo fixup section headers in " << file->GetPath();
46 return false;
47 }
48 if (!FixupProgramHeaders(*elf_file.get(), base_address)) {
49 LOG(WARNING) << "Failed fo fixup program headers in " << file->GetPath();
50 return false;
51 }
52 if (!FixupSymbols(*elf_file.get(), base_address, true)) {
53 LOG(WARNING) << "Failed fo fixup .dynsym in " << file->GetPath();
54 return false;
55 }
56 if (!FixupSymbols(*elf_file.get(), base_address, false)) {
57 LOG(WARNING) << "Failed fo fixup .symtab in " << file->GetPath();
58 return false;
59 }
60 if (!FixupRelocations(*elf_file.get(), base_address)) {
61 LOG(WARNING) << "Failed fo fixup .rel.dyn in " << file->GetPath();
62 return false;
63 }
64 return true;
65}
66
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070067
68bool ElfFixup::FixupDynamic(ElfFile& elf_file, uintptr_t base_address) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000069 for (Elf32_Word i = 0; i < elf_file.GetDynamicNum(); i++) {
70 Elf32_Dyn& elf_dyn = elf_file.GetDynamic(i);
71 Elf32_Word d_tag = elf_dyn.d_tag;
Alex Light53cb16b2014-06-12 11:26:29 -070072 if (IsDynamicSectionPointer(d_tag, elf_file.GetHeader().e_machine)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070073 uint32_t d_ptr = elf_dyn.d_un.d_ptr;
74 if (DEBUG_FIXUP) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 LOG(INFO) << StringPrintf("In %s moving Elf32_Dyn[%d] from 0x%08x to 0x%08" PRIxPTR,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070076 elf_file.GetFile().GetPath().c_str(), i,
77 d_ptr, d_ptr + base_address);
78 }
79 d_ptr += base_address;
80 elf_dyn.d_un.d_ptr = d_ptr;
81 }
82 }
83 return true;
84}
85
86bool ElfFixup::FixupSectionHeaders(ElfFile& elf_file, uintptr_t base_address) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000087 for (Elf32_Word i = 0; i < elf_file.GetSectionHeaderNum(); i++) {
88 Elf32_Shdr& sh = elf_file.GetSectionHeader(i);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070089 // 0 implies that the section will not exist in the memory of the process
90 if (sh.sh_addr == 0) {
91 continue;
92 }
93 if (DEBUG_FIXUP) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080094 LOG(INFO) << StringPrintf("In %s moving Elf32_Shdr[%d] from 0x%08x to 0x%08" PRIxPTR,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070095 elf_file.GetFile().GetPath().c_str(), i,
96 sh.sh_addr, sh.sh_addr + base_address);
97 }
98 sh.sh_addr += base_address;
99 }
100 return true;
101}
102
103bool ElfFixup::FixupProgramHeaders(ElfFile& elf_file, uintptr_t base_address) {
104 // TODO: ELFObjectFile doesn't have give to Elf32_Phdr, so we do that ourselves for now.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000105 for (Elf32_Word i = 0; i < elf_file.GetProgramHeaderNum(); i++) {
106 Elf32_Phdr& ph = elf_file.GetProgramHeader(i);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700107 CHECK_EQ(ph.p_vaddr, ph.p_paddr) << elf_file.GetFile().GetPath() << " i=" << i;
108 CHECK((ph.p_align == 0) || (0 == ((ph.p_vaddr - ph.p_offset) & (ph.p_align - 1))))
109 << elf_file.GetFile().GetPath() << " i=" << i;
110 if (DEBUG_FIXUP) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800111 LOG(INFO) << StringPrintf("In %s moving Elf32_Phdr[%d] from 0x%08x to 0x%08" PRIxPTR,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700112 elf_file.GetFile().GetPath().c_str(), i,
113 ph.p_vaddr, ph.p_vaddr + base_address);
114 }
115 ph.p_vaddr += base_address;
116 ph.p_paddr += base_address;
117 CHECK((ph.p_align == 0) || (0 == ((ph.p_vaddr - ph.p_offset) & (ph.p_align - 1))))
118 << elf_file.GetFile().GetPath() << " i=" << i;
119 }
120 return true;
121}
122
123bool ElfFixup::FixupSymbols(ElfFile& elf_file, uintptr_t base_address, bool dynamic) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000124 Elf32_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700125 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000126 Elf32_Shdr* symbol_section = elf_file.FindSectionByType(section_type);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700127 if (symbol_section == NULL) {
128 // file is missing optional .symtab
129 CHECK(!dynamic) << elf_file.GetFile().GetPath();
130 return true;
131 }
132 for (uint32_t i = 0; i < elf_file.GetSymbolNum(*symbol_section); i++) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000133 Elf32_Sym& symbol = elf_file.GetSymbol(section_type, i);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700134 if (symbol.st_value != 0) {
135 if (DEBUG_FIXUP) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800136 LOG(INFO) << StringPrintf("In %s moving Elf32_Sym[%d] from 0x%08x to 0x%08" PRIxPTR,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700137 elf_file.GetFile().GetPath().c_str(), i,
138 symbol.st_value, symbol.st_value + base_address);
139 }
140 symbol.st_value += base_address;
141 }
142 }
143 return true;
144}
145
146bool ElfFixup::FixupRelocations(ElfFile& elf_file, uintptr_t base_address) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000147 for (Elf32_Word i = 0; i < elf_file.GetSectionHeaderNum(); i++) {
148 Elf32_Shdr& sh = elf_file.GetSectionHeader(i);
149 if (sh.sh_type == SHT_REL) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700150 for (uint32_t i = 0; i < elf_file.GetRelNum(sh); i++) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000151 Elf32_Rel& rel = elf_file.GetRel(sh, i);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700152 if (DEBUG_FIXUP) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800153 LOG(INFO) << StringPrintf("In %s moving Elf32_Rel[%d] from 0x%08x to 0x%08" PRIxPTR,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700154 elf_file.GetFile().GetPath().c_str(), i,
155 rel.r_offset, rel.r_offset + base_address);
156 }
157 rel.r_offset += base_address;
158 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000159 } else if (sh.sh_type == SHT_RELA) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700160 for (uint32_t i = 0; i < elf_file.GetRelaNum(sh); i++) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000161 Elf32_Rela& rela = elf_file.GetRela(sh, i);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700162 if (DEBUG_FIXUP) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800163 LOG(INFO) << StringPrintf("In %s moving Elf32_Rela[%d] from 0x%08x to 0x%08" PRIxPTR,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700164 elf_file.GetFile().GetPath().c_str(), i,
165 rela.r_offset, rela.r_offset + base_address);
166 }
167 rela.r_offset += base_address;
168 }
169 }
170 }
171 return true;
172}
173
174} // namespace art