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 19 #include "base/logging.h" 20 #include "base/stringprintf.h" 21 #include "elf_file.h" 22 #include "elf_writer.h" 23 #include "UniquePtr.h" 24 25 namespace art { 26 27 static const bool DEBUG_FIXUP = false; 28 29 bool ElfFixup::Fixup(File* file, uintptr_t oat_data_begin) { 30 UniquePtr<ElfFile> elf_file(ElfFile::Open(file, true, false)); 31 CHECK(elf_file.get() != NULL); 32 33 // Lookup "oatdata" symbol address. 34 ::llvm::ELF::Elf32_Addr oatdata_address = ElfWriter::GetOatDataAddress(elf_file.get()); 35 ::llvm::ELF::Elf32_Off base_address = oat_data_begin - oatdata_address; 36 37 if (!FixupDynamic(*elf_file.get(), base_address)) { 38 LOG(WARNING) << "Failed fo fixup .dynamic in " << file->GetPath(); 39 return false; 40 } 41 if (!FixupSectionHeaders(*elf_file.get(), base_address)) { 42 LOG(WARNING) << "Failed fo fixup section headers in " << file->GetPath(); 43 return false; 44 } 45 if (!FixupProgramHeaders(*elf_file.get(), base_address)) { 46 LOG(WARNING) << "Failed fo fixup program headers in " << file->GetPath(); 47 return false; 48 } 49 if (!FixupSymbols(*elf_file.get(), base_address, true)) { 50 LOG(WARNING) << "Failed fo fixup .dynsym in " << file->GetPath(); 51 return false; 52 } 53 if (!FixupSymbols(*elf_file.get(), base_address, false)) { 54 LOG(WARNING) << "Failed fo fixup .symtab in " << file->GetPath(); 55 return false; 56 } 57 if (!FixupRelocations(*elf_file.get(), base_address)) { 58 LOG(WARNING) << "Failed fo fixup .rel.dyn in " << file->GetPath(); 59 return false; 60 } 61 return true; 62 } 63 64 // MIPS seems to break the rules d_val vs d_ptr even though their values are between DT_LOPROC and DT_HIPROC 65 #define DT_MIPS_RLD_VERSION 0x70000001 // d_val 66 #define DT_MIPS_TIME_STAMP 0x70000002 // d_val 67 #define DT_MIPS_ICHECKSUM 0x70000003 // d_val 68 #define DT_MIPS_IVERSION 0x70000004 // d_val 69 #define DT_MIPS_FLAGS 0x70000005 // d_val 70 #define DT_MIPS_BASE_ADDRESS 0x70000006 // d_ptr 71 #define DT_MIPS_CONFLICT 0x70000008 // d_ptr 72 #define DT_MIPS_LIBLIST 0x70000009 // d_ptr 73 #define DT_MIPS_LOCAL_GOTNO 0x7000000A // d_val 74 #define DT_MIPS_CONFLICTNO 0x7000000B // d_val 75 #define DT_MIPS_LIBLISTNO 0x70000010 // d_val 76 #define DT_MIPS_SYMTABNO 0x70000011 // d_val 77 #define DT_MIPS_UNREFEXTNO 0x70000012 // d_val 78 #define DT_MIPS_GOTSYM 0x70000013 // d_val 79 #define DT_MIPS_HIPAGENO 0x70000014 // d_val 80 #define DT_MIPS_RLD_MAP 0x70000016 // d_ptr 81 82 bool ElfFixup::FixupDynamic(ElfFile& elf_file, uintptr_t base_address) { 83 for (::llvm::ELF::Elf32_Word i = 0; i < elf_file.GetDynamicNum(); i++) { 84 ::llvm::ELF::Elf32_Dyn& elf_dyn = elf_file.GetDynamic(i); 85 ::llvm::ELF::Elf32_Word d_tag = elf_dyn.d_tag; 86 bool elf_dyn_needs_fixup = false; 87 switch (d_tag) { 88 // case 1: well known d_tag values that imply Elf32_Dyn.d_un contains an address in d_ptr 89 case ::llvm::ELF::DT_PLTGOT: 90 case ::llvm::ELF::DT_HASH: 91 case ::llvm::ELF::DT_STRTAB: 92 case ::llvm::ELF::DT_SYMTAB: 93 case ::llvm::ELF::DT_RELA: 94 case ::llvm::ELF::DT_INIT: 95 case ::llvm::ELF::DT_FINI: 96 case ::llvm::ELF::DT_REL: 97 case ::llvm::ELF::DT_DEBUG: 98 case ::llvm::ELF::DT_JMPREL: { 99 elf_dyn_needs_fixup = true; 100 break; 101 } 102 // d_val or ignored values 103 case ::llvm::ELF::DT_NULL: 104 case ::llvm::ELF::DT_NEEDED: 105 case ::llvm::ELF::DT_PLTRELSZ: 106 case ::llvm::ELF::DT_RELASZ: 107 case ::llvm::ELF::DT_RELAENT: 108 case ::llvm::ELF::DT_STRSZ: 109 case ::llvm::ELF::DT_SYMENT: 110 case ::llvm::ELF::DT_SONAME: 111 case ::llvm::ELF::DT_RPATH: 112 case ::llvm::ELF::DT_SYMBOLIC: 113 case ::llvm::ELF::DT_RELSZ: 114 case ::llvm::ELF::DT_RELENT: 115 case ::llvm::ELF::DT_PLTREL: 116 case ::llvm::ELF::DT_TEXTREL: 117 case ::llvm::ELF::DT_BIND_NOW: 118 case ::llvm::ELF::DT_INIT_ARRAYSZ: 119 case ::llvm::ELF::DT_FINI_ARRAYSZ: 120 case ::llvm::ELF::DT_RUNPATH: 121 case ::llvm::ELF::DT_FLAGS: { 122 break; 123 } 124 // boundary values that should not be used 125 case ::llvm::ELF::DT_ENCODING: 126 case ::llvm::ELF::DT_LOOS: 127 case ::llvm::ELF::DT_HIOS: 128 case ::llvm::ELF::DT_LOPROC: 129 case ::llvm::ELF::DT_HIPROC: { 130 LOG(FATAL) << "Illegal d_tag value 0x" << std::hex << d_tag; 131 break; 132 } 133 default: { 134 // case 2: "regular" DT_* ranges where even d_tag values imply an address in d_ptr 135 if ((::llvm::ELF::DT_ENCODING < d_tag && d_tag < ::llvm::ELF::DT_LOOS) 136 || (::llvm::ELF::DT_LOOS < d_tag && d_tag < ::llvm::ELF::DT_HIOS) 137 || (::llvm::ELF::DT_LOPROC < d_tag && d_tag < ::llvm::ELF::DT_HIPROC)) { 138 // Special case for MIPS which breaks the regular rules between DT_LOPROC and DT_HIPROC 139 if (elf_file.GetHeader().e_machine == ::llvm::ELF::EM_MIPS) { 140 switch (d_tag) { 141 case DT_MIPS_RLD_VERSION: 142 case DT_MIPS_TIME_STAMP: 143 case DT_MIPS_ICHECKSUM: 144 case DT_MIPS_IVERSION: 145 case DT_MIPS_FLAGS: 146 case DT_MIPS_LOCAL_GOTNO: 147 case DT_MIPS_CONFLICTNO: 148 case DT_MIPS_LIBLISTNO: 149 case DT_MIPS_SYMTABNO: 150 case DT_MIPS_UNREFEXTNO: 151 case DT_MIPS_GOTSYM: 152 case DT_MIPS_HIPAGENO: { 153 break; 154 } 155 case DT_MIPS_BASE_ADDRESS: 156 case DT_MIPS_CONFLICT: 157 case DT_MIPS_LIBLIST: 158 case DT_MIPS_RLD_MAP: { 159 elf_dyn_needs_fixup = true; 160 break; 161 } 162 default: { 163 LOG(FATAL) << "Unknown MIPS d_tag value 0x" << std::hex << d_tag; 164 break; 165 } 166 } 167 } else if ((elf_dyn.d_tag % 2) == 0) { 168 elf_dyn_needs_fixup = true; 169 } 170 } else { 171 LOG(FATAL) << "Unknown d_tag value 0x" << std::hex << d_tag; 172 } 173 break; 174 } 175 } 176 if (elf_dyn_needs_fixup) { 177 uint32_t d_ptr = elf_dyn.d_un.d_ptr; 178 if (DEBUG_FIXUP) { 179 LOG(INFO) << StringPrintf("In %s moving Elf32_Dyn[%d] from 0x%08x to 0x%08x", 180 elf_file.GetFile().GetPath().c_str(), i, 181 d_ptr, d_ptr + base_address); 182 } 183 d_ptr += base_address; 184 elf_dyn.d_un.d_ptr = d_ptr; 185 } 186 } 187 return true; 188 } 189 190 bool ElfFixup::FixupSectionHeaders(ElfFile& elf_file, uintptr_t base_address) { 191 for (::llvm::ELF::Elf32_Word i = 0; i < elf_file.GetSectionHeaderNum(); i++) { 192 ::llvm::ELF::Elf32_Shdr& sh = elf_file.GetSectionHeader(i); 193 // 0 implies that the section will not exist in the memory of the process 194 if (sh.sh_addr == 0) { 195 continue; 196 } 197 if (DEBUG_FIXUP) { 198 LOG(INFO) << StringPrintf("In %s moving Elf32_Shdr[%d] from 0x%08x to 0x%08x", 199 elf_file.GetFile().GetPath().c_str(), i, 200 sh.sh_addr, sh.sh_addr + base_address); 201 } 202 sh.sh_addr += base_address; 203 } 204 return true; 205 } 206 207 bool ElfFixup::FixupProgramHeaders(ElfFile& elf_file, uintptr_t base_address) { 208 // TODO: ELFObjectFile doesn't have give to Elf32_Phdr, so we do that ourselves for now. 209 for (::llvm::ELF::Elf32_Word i = 0; i < elf_file.GetProgramHeaderNum(); i++) { 210 ::llvm::ELF::Elf32_Phdr& ph = elf_file.GetProgramHeader(i); 211 CHECK_EQ(ph.p_vaddr, ph.p_paddr) << elf_file.GetFile().GetPath() << " i=" << i; 212 CHECK((ph.p_align == 0) || (0 == ((ph.p_vaddr - ph.p_offset) & (ph.p_align - 1)))) 213 << elf_file.GetFile().GetPath() << " i=" << i; 214 if (DEBUG_FIXUP) { 215 LOG(INFO) << StringPrintf("In %s moving Elf32_Phdr[%d] from 0x%08x to 0x%08x", 216 elf_file.GetFile().GetPath().c_str(), i, 217 ph.p_vaddr, ph.p_vaddr + base_address); 218 } 219 ph.p_vaddr += base_address; 220 ph.p_paddr += base_address; 221 CHECK((ph.p_align == 0) || (0 == ((ph.p_vaddr - ph.p_offset) & (ph.p_align - 1)))) 222 << elf_file.GetFile().GetPath() << " i=" << i; 223 } 224 return true; 225 } 226 227 bool ElfFixup::FixupSymbols(ElfFile& elf_file, uintptr_t base_address, bool dynamic) { 228 ::llvm::ELF::Elf32_Word section_type = dynamic ? ::llvm::ELF::SHT_DYNSYM : ::llvm::ELF::SHT_SYMTAB; 229 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile 230 ::llvm::ELF::Elf32_Shdr* symbol_section = elf_file.FindSectionByType(section_type); 231 if (symbol_section == NULL) { 232 // file is missing optional .symtab 233 CHECK(!dynamic) << elf_file.GetFile().GetPath(); 234 return true; 235 } 236 for (uint32_t i = 0; i < elf_file.GetSymbolNum(*symbol_section); i++) { 237 ::llvm::ELF::Elf32_Sym& symbol = elf_file.GetSymbol(section_type, i); 238 if (symbol.st_value != 0) { 239 if (DEBUG_FIXUP) { 240 LOG(INFO) << StringPrintf("In %s moving Elf32_Sym[%d] from 0x%08x to 0x%08x", 241 elf_file.GetFile().GetPath().c_str(), i, 242 symbol.st_value, symbol.st_value + base_address); 243 } 244 symbol.st_value += base_address; 245 } 246 } 247 return true; 248 } 249 250 bool ElfFixup::FixupRelocations(ElfFile& elf_file, uintptr_t base_address) { 251 for (llvm::ELF::Elf32_Word i = 0; i < elf_file.GetSectionHeaderNum(); i++) { 252 llvm::ELF::Elf32_Shdr& sh = elf_file.GetSectionHeader(i); 253 if (sh.sh_type == llvm::ELF::SHT_REL) { 254 for (uint32_t i = 0; i < elf_file.GetRelNum(sh); i++) { 255 llvm::ELF::Elf32_Rel& rel = elf_file.GetRel(sh, i); 256 if (DEBUG_FIXUP) { 257 LOG(INFO) << StringPrintf("In %s moving Elf32_Rel[%d] from 0x%08x to 0x%08x", 258 elf_file.GetFile().GetPath().c_str(), i, 259 rel.r_offset, rel.r_offset + base_address); 260 } 261 rel.r_offset += base_address; 262 } 263 } else if (sh.sh_type == llvm::ELF::SHT_RELA) { 264 for (uint32_t i = 0; i < elf_file.GetRelaNum(sh); i++) { 265 llvm::ELF::Elf32_Rela& rela = elf_file.GetRela(sh, i); 266 if (DEBUG_FIXUP) { 267 LOG(INFO) << StringPrintf("In %s moving Elf32_Rela[%d] from 0x%08x to 0x%08x", 268 elf_file.GetFile().GetPath().c_str(), i, 269 rela.r_offset, rela.r_offset + base_address); 270 } 271 rela.r_offset += base_address; 272 } 273 } 274 } 275 return true; 276 } 277 278 } // namespace art 279