elf_fixup.cc revision 7934ac288acfb2552bb0b06ec1f61e5820d924a4
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
25namespace art {
26
27static const bool DEBUG_FIXUP = false;
28
29bool 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
82bool ElfFixup::FixupDynamic(ElfFile& elf_file, uintptr_t base_address) {
83  // TODO: C++0x auto.
84  for (::llvm::ELF::Elf32_Word i = 0; i < elf_file.GetDynamicNum(); i++) {
85    ::llvm::ELF::Elf32_Dyn& elf_dyn = elf_file.GetDynamic(i);
86    ::llvm::ELF::Elf32_Word d_tag = elf_dyn.d_tag;
87    bool elf_dyn_needs_fixup = false;
88    switch (d_tag) {
89      // case 1: well known d_tag values that imply Elf32_Dyn.d_un contains an address in d_ptr
90      case ::llvm::ELF::DT_PLTGOT:
91      case ::llvm::ELF::DT_HASH:
92      case ::llvm::ELF::DT_STRTAB:
93      case ::llvm::ELF::DT_SYMTAB:
94      case ::llvm::ELF::DT_RELA:
95      case ::llvm::ELF::DT_INIT:
96      case ::llvm::ELF::DT_FINI:
97      case ::llvm::ELF::DT_REL:
98      case ::llvm::ELF::DT_DEBUG:
99      case ::llvm::ELF::DT_JMPREL: {
100        elf_dyn_needs_fixup = true;
101        break;
102      }
103      // d_val or ignored values
104      case ::llvm::ELF::DT_NULL:
105      case ::llvm::ELF::DT_NEEDED:
106      case ::llvm::ELF::DT_PLTRELSZ:
107      case ::llvm::ELF::DT_RELASZ:
108      case ::llvm::ELF::DT_RELAENT:
109      case ::llvm::ELF::DT_STRSZ:
110      case ::llvm::ELF::DT_SYMENT:
111      case ::llvm::ELF::DT_SONAME:
112      case ::llvm::ELF::DT_RPATH:
113      case ::llvm::ELF::DT_SYMBOLIC:
114      case ::llvm::ELF::DT_RELSZ:
115      case ::llvm::ELF::DT_RELENT:
116      case ::llvm::ELF::DT_PLTREL:
117      case ::llvm::ELF::DT_TEXTREL:
118      case ::llvm::ELF::DT_BIND_NOW:
119      case ::llvm::ELF::DT_INIT_ARRAYSZ:
120      case ::llvm::ELF::DT_FINI_ARRAYSZ:
121      case ::llvm::ELF::DT_RUNPATH:
122      case ::llvm::ELF::DT_FLAGS: {
123        break;
124      }
125      // boundary values that should not be used
126      case ::llvm::ELF::DT_ENCODING:
127      case ::llvm::ELF::DT_LOOS:
128      case ::llvm::ELF::DT_HIOS:
129      case ::llvm::ELF::DT_LOPROC:
130      case ::llvm::ELF::DT_HIPROC: {
131        LOG(FATAL) << "Illegal d_tag value 0x" << std::hex << d_tag;
132        break;
133      }
134      default: {
135        // case 2: "regular" DT_* ranges where even d_tag values imply an address in d_ptr
136        if ((::llvm::ELF::DT_ENCODING  < d_tag && d_tag < ::llvm::ELF::DT_LOOS)
137            || (::llvm::ELF::DT_LOOS   < d_tag && d_tag < ::llvm::ELF::DT_HIOS)
138            || (::llvm::ELF::DT_LOPROC < d_tag && d_tag < ::llvm::ELF::DT_HIPROC)) {
139          // Special case for MIPS which breaks the regular rules between DT_LOPROC and DT_HIPROC
140          if (elf_file.GetHeader().e_machine == ::llvm::ELF::EM_MIPS) {
141            switch (d_tag) {
142              case DT_MIPS_RLD_VERSION:
143              case DT_MIPS_TIME_STAMP:
144              case DT_MIPS_ICHECKSUM:
145              case DT_MIPS_IVERSION:
146              case DT_MIPS_FLAGS:
147              case DT_MIPS_LOCAL_GOTNO:
148              case DT_MIPS_CONFLICTNO:
149              case DT_MIPS_LIBLISTNO:
150              case DT_MIPS_SYMTABNO:
151              case DT_MIPS_UNREFEXTNO:
152              case DT_MIPS_GOTSYM:
153              case DT_MIPS_HIPAGENO: {
154                break;
155              }
156              case DT_MIPS_BASE_ADDRESS:
157              case DT_MIPS_CONFLICT:
158              case DT_MIPS_LIBLIST:
159              case DT_MIPS_RLD_MAP: {
160                elf_dyn_needs_fixup = true;
161                break;
162              }
163              default: {
164                LOG(FATAL) << "Unknown MIPS d_tag value 0x" << std::hex << d_tag;
165                break;
166              }
167            }
168          } else if ((elf_dyn.d_tag % 2) == 0) {
169            elf_dyn_needs_fixup = true;
170          }
171        } else {
172          LOG(FATAL) << "Unknown d_tag value 0x" << std::hex << d_tag;
173        }
174        break;
175      }
176    }
177    if (elf_dyn_needs_fixup) {
178      uint32_t d_ptr = elf_dyn.d_un.d_ptr;
179      if (DEBUG_FIXUP) {
180        LOG(INFO) << StringPrintf("In %s moving Elf32_Dyn[%d] from 0x%08x to 0x%08x",
181                                  elf_file.GetFile().GetPath().c_str(), i,
182                                  d_ptr, d_ptr + base_address);
183      }
184      d_ptr += base_address;
185      elf_dyn.d_un.d_ptr = d_ptr;
186    }
187  }
188  return true;
189}
190
191bool ElfFixup::FixupSectionHeaders(ElfFile& elf_file, uintptr_t base_address) {
192  for (::llvm::ELF::Elf32_Word i = 0; i < elf_file.GetSectionHeaderNum(); i++) {
193    ::llvm::ELF::Elf32_Shdr& sh = elf_file.GetSectionHeader(i);
194    // 0 implies that the section will not exist in the memory of the process
195    if (sh.sh_addr == 0) {
196      continue;
197    }
198    if (DEBUG_FIXUP) {
199      LOG(INFO) << StringPrintf("In %s moving Elf32_Shdr[%d] from 0x%08x to 0x%08x",
200                                elf_file.GetFile().GetPath().c_str(), i,
201                                sh.sh_addr, sh.sh_addr + base_address);
202    }
203    sh.sh_addr += base_address;
204  }
205  return true;
206}
207
208bool ElfFixup::FixupProgramHeaders(ElfFile& elf_file, uintptr_t base_address) {
209  // TODO: ELFObjectFile doesn't have give to Elf32_Phdr, so we do that ourselves for now.
210  for (::llvm::ELF::Elf32_Word i = 0; i < elf_file.GetProgramHeaderNum(); i++) {
211    ::llvm::ELF::Elf32_Phdr& ph = elf_file.GetProgramHeader(i);
212    CHECK_EQ(ph.p_vaddr, ph.p_paddr) << elf_file.GetFile().GetPath() << " i=" << i;
213    CHECK((ph.p_align == 0) || (0 == ((ph.p_vaddr - ph.p_offset) & (ph.p_align - 1))))
214            << elf_file.GetFile().GetPath() << " i=" << i;
215    if (DEBUG_FIXUP) {
216      LOG(INFO) << StringPrintf("In %s moving Elf32_Phdr[%d] from 0x%08x to 0x%08x",
217                                elf_file.GetFile().GetPath().c_str(), i,
218                                ph.p_vaddr, ph.p_vaddr + base_address);
219    }
220    ph.p_vaddr += base_address;
221    ph.p_paddr += base_address;
222    CHECK((ph.p_align == 0) || (0 == ((ph.p_vaddr - ph.p_offset) & (ph.p_align - 1))))
223            << elf_file.GetFile().GetPath() << " i=" << i;
224  }
225  return true;
226}
227
228bool ElfFixup::FixupSymbols(ElfFile& elf_file, uintptr_t base_address, bool dynamic) {
229  ::llvm::ELF::Elf32_Word section_type = dynamic ? ::llvm::ELF::SHT_DYNSYM : ::llvm::ELF::SHT_SYMTAB;
230  // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
231  ::llvm::ELF::Elf32_Shdr* symbol_section = elf_file.FindSectionByType(section_type);
232  if (symbol_section == NULL) {
233    // file is missing optional .symtab
234    CHECK(!dynamic) << elf_file.GetFile().GetPath();
235    return true;
236  }
237  for (uint32_t i = 0; i < elf_file.GetSymbolNum(*symbol_section); i++) {
238    ::llvm::ELF::Elf32_Sym& symbol = elf_file.GetSymbol(section_type, i);
239    if (symbol.st_value != 0) {
240      if (DEBUG_FIXUP) {
241        LOG(INFO) << StringPrintf("In %s moving Elf32_Sym[%d] from 0x%08x to 0x%08x",
242                                  elf_file.GetFile().GetPath().c_str(), i,
243                                  symbol.st_value, symbol.st_value + base_address);
244      }
245      symbol.st_value += base_address;
246    }
247  }
248  return true;
249}
250
251bool ElfFixup::FixupRelocations(ElfFile& elf_file, uintptr_t base_address) {
252  for (llvm::ELF::Elf32_Word i = 0; i < elf_file.GetSectionHeaderNum(); i++) {
253    llvm::ELF::Elf32_Shdr& sh = elf_file.GetSectionHeader(i);
254    if (sh.sh_type == llvm::ELF::SHT_REL) {
255      for (uint32_t i = 0; i < elf_file.GetRelNum(sh); i++) {
256        llvm::ELF::Elf32_Rel& rel = elf_file.GetRel(sh, i);
257        if (DEBUG_FIXUP) {
258          LOG(INFO) << StringPrintf("In %s moving Elf32_Rel[%d] from 0x%08x to 0x%08x",
259                                    elf_file.GetFile().GetPath().c_str(), i,
260                                    rel.r_offset, rel.r_offset + base_address);
261        }
262        rel.r_offset += base_address;
263      }
264    } else if (sh.sh_type == llvm::ELF::SHT_RELA) {
265      for (uint32_t i = 0; i < elf_file.GetRelaNum(sh); i++) {
266        llvm::ELF::Elf32_Rela& rela = elf_file.GetRela(sh, i);
267        if (DEBUG_FIXUP) {
268          LOG(INFO) << StringPrintf("In %s moving Elf32_Rela[%d] from 0x%08x to 0x%08x",
269                                    elf_file.GetFile().GetPath().c_str(), i,
270                                    rela.r_offset, rela.r_offset + base_address);
271        }
272        rela.r_offset += base_address;
273      }
274    }
275  }
276  return true;
277}
278
279}  // namespace art
280