Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 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_fixup.h" |
| 18 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 19 | #include <inttypes.h> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 21 | |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 22 | #include "base/logging.h" |
| 23 | #include "base/stringprintf.h" |
| 24 | #include "elf_file.h" |
| 25 | #include "elf_writer.h" |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | static const bool DEBUG_FIXUP = false; |
| 30 | |
| 31 | bool ElfFixup::Fixup(File* file, uintptr_t oat_data_begin) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 32 | std::string error_msg; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 33 | std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 34 | CHECK(elf_file.get() != nullptr) << error_msg; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 35 | |
| 36 | // Lookup "oatdata" symbol address. |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 37 | Elf32_Addr oatdata_address = ElfWriter::GetOatDataAddress(elf_file.get()); |
| 38 | Elf32_Off base_address = oat_data_begin - oatdata_address; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 39 | |
| 40 | if (!FixupDynamic(*elf_file.get(), base_address)) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 41 | LOG(WARNING) << "Failed to fixup .dynamic in " << file->GetPath(); |
| 42 | return false; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 43 | } |
| 44 | if (!FixupSectionHeaders(*elf_file.get(), base_address)) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 45 | LOG(WARNING) << "Failed to fixup section headers in " << file->GetPath(); |
| 46 | return false; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 47 | } |
| 48 | if (!FixupProgramHeaders(*elf_file.get(), base_address)) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 49 | LOG(WARNING) << "Failed to fixup program headers in " << file->GetPath(); |
| 50 | return false; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 51 | } |
| 52 | if (!FixupSymbols(*elf_file.get(), base_address, true)) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 53 | LOG(WARNING) << "Failed to fixup .dynsym in " << file->GetPath(); |
| 54 | return false; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 55 | } |
| 56 | if (!FixupSymbols(*elf_file.get(), base_address, false)) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 57 | LOG(WARNING) << "Failed to fixup .symtab in " << file->GetPath(); |
| 58 | return false; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 59 | } |
| 60 | if (!FixupRelocations(*elf_file.get(), base_address)) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 61 | LOG(WARNING) << "Failed to fixup .rel.dyn in " << file->GetPath(); |
| 62 | return false; |
| 63 | } |
| 64 | if (!elf_file->FixupDebugSections(base_address)) { |
| 65 | LOG(WARNING) << "Failed to fixup debug sections in " << file->GetPath(); |
| 66 | return false; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 71 | |
| 72 | bool ElfFixup::FixupDynamic(ElfFile& elf_file, uintptr_t base_address) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 73 | for (Elf32_Word i = 0; i < elf_file.GetDynamicNum(); i++) { |
| 74 | Elf32_Dyn& elf_dyn = elf_file.GetDynamic(i); |
| 75 | Elf32_Word d_tag = elf_dyn.d_tag; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 76 | if (IsDynamicSectionPointer(d_tag, elf_file.GetHeader().e_machine)) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 77 | uint32_t d_ptr = elf_dyn.d_un.d_ptr; |
| 78 | if (DEBUG_FIXUP) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 79 | LOG(INFO) << StringPrintf("In %s moving Elf32_Dyn[%d] from 0x%08x to 0x%08" PRIxPTR, |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 80 | elf_file.GetFile().GetPath().c_str(), i, |
| 81 | d_ptr, d_ptr + base_address); |
| 82 | } |
| 83 | d_ptr += base_address; |
| 84 | elf_dyn.d_un.d_ptr = d_ptr; |
| 85 | } |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool ElfFixup::FixupSectionHeaders(ElfFile& elf_file, uintptr_t base_address) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 91 | for (Elf32_Word i = 0; i < elf_file.GetSectionHeaderNum(); i++) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 92 | Elf32_Shdr* sh = elf_file.GetSectionHeader(i); |
| 93 | CHECK(sh != nullptr); |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 94 | // 0 implies that the section will not exist in the memory of the process |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 95 | if (sh->sh_addr == 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 96 | continue; |
| 97 | } |
| 98 | if (DEBUG_FIXUP) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 99 | LOG(INFO) << StringPrintf("In %s moving Elf32_Shdr[%d] from 0x%08x to 0x%08" PRIxPTR, |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 100 | elf_file.GetFile().GetPath().c_str(), i, |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 101 | sh->sh_addr, sh->sh_addr + base_address); |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 102 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 103 | sh->sh_addr += base_address; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool ElfFixup::FixupProgramHeaders(ElfFile& elf_file, uintptr_t base_address) { |
| 109 | // TODO: ELFObjectFile doesn't have give to Elf32_Phdr, so we do that ourselves for now. |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 110 | for (Elf32_Word i = 0; i < elf_file.GetProgramHeaderNum(); i++) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 111 | Elf32_Phdr* ph = elf_file.GetProgramHeader(i); |
| 112 | CHECK(ph != nullptr); |
| 113 | CHECK_EQ(ph->p_vaddr, ph->p_paddr) << elf_file.GetFile().GetPath() << " i=" << i; |
| 114 | CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)))) |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 115 | << elf_file.GetFile().GetPath() << " i=" << i; |
| 116 | if (DEBUG_FIXUP) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 117 | LOG(INFO) << StringPrintf("In %s moving Elf32_Phdr[%d] from 0x%08x to 0x%08" PRIxPTR, |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 118 | elf_file.GetFile().GetPath().c_str(), i, |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 119 | ph->p_vaddr, ph->p_vaddr + base_address); |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 120 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 121 | ph->p_vaddr += base_address; |
| 122 | ph->p_paddr += base_address; |
| 123 | CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)))) |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 124 | << elf_file.GetFile().GetPath() << " i=" << i; |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | bool ElfFixup::FixupSymbols(ElfFile& elf_file, uintptr_t base_address, bool dynamic) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 130 | Elf32_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 131 | // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 132 | Elf32_Shdr* symbol_section = elf_file.FindSectionByType(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 133 | if (symbol_section == nullptr) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 134 | // file is missing optional .symtab |
| 135 | CHECK(!dynamic) << elf_file.GetFile().GetPath(); |
| 136 | return true; |
| 137 | } |
| 138 | for (uint32_t i = 0; i < elf_file.GetSymbolNum(*symbol_section); i++) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 139 | Elf32_Sym* symbol = elf_file.GetSymbol(section_type, i); |
| 140 | CHECK(symbol != nullptr); |
| 141 | if (symbol->st_value != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 142 | if (DEBUG_FIXUP) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 143 | LOG(INFO) << StringPrintf("In %s moving Elf32_Sym[%d] from 0x%08x to 0x%08" PRIxPTR, |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 144 | elf_file.GetFile().GetPath().c_str(), i, |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 145 | symbol->st_value, symbol->st_value + base_address); |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 146 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 147 | symbol->st_value += base_address; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | bool ElfFixup::FixupRelocations(ElfFile& elf_file, uintptr_t base_address) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 154 | for (Elf32_Word i = 0; i < elf_file.GetSectionHeaderNum(); i++) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 155 | Elf32_Shdr* sh = elf_file.GetSectionHeader(i); |
| 156 | CHECK(sh != nullptr); |
| 157 | if (sh->sh_type == SHT_REL) { |
| 158 | for (uint32_t i = 0; i < elf_file.GetRelNum(*sh); i++) { |
| 159 | Elf32_Rel& rel = elf_file.GetRel(*sh, i); |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 160 | if (DEBUG_FIXUP) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 161 | LOG(INFO) << StringPrintf("In %s moving Elf32_Rel[%d] from 0x%08x to 0x%08" PRIxPTR, |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 162 | elf_file.GetFile().GetPath().c_str(), i, |
| 163 | rel.r_offset, rel.r_offset + base_address); |
| 164 | } |
| 165 | rel.r_offset += base_address; |
| 166 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 167 | } else if (sh->sh_type == SHT_RELA) { |
| 168 | for (uint32_t i = 0; i < elf_file.GetRelaNum(*sh); i++) { |
| 169 | Elf32_Rela& rela = elf_file.GetRela(*sh, i); |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 170 | if (DEBUG_FIXUP) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 171 | LOG(INFO) << StringPrintf("In %s moving Elf32_Rela[%d] from 0x%08x to 0x%08" PRIxPTR, |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 172 | elf_file.GetFile().GetPath().c_str(), i, |
| 173 | rela.r_offset, rela.r_offset + base_address); |
| 174 | } |
| 175 | rela.r_offset += base_address; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | } // namespace art |