2014-10-07 02:42:18 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "BaseElf.h"
|
|
|
|
#include "Elfxx.h"
|
|
|
|
#include "Logging.h"
|
2016-12-15 00:22:49 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2014-10-07 02:42:18 +04:00
|
|
|
|
|
|
|
using namespace Elf;
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
unsigned long BaseElf::Hash(const char* symbol) {
|
|
|
|
const unsigned char* sym = reinterpret_cast<const unsigned char*>(symbol);
|
2014-10-07 02:42:18 +04:00
|
|
|
unsigned long h = 0, g;
|
|
|
|
while (*sym) {
|
|
|
|
h = (h << 4) + *sym++;
|
|
|
|
g = h & 0xf0000000;
|
|
|
|
h ^= g;
|
|
|
|
h ^= g >> 24;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void* BaseElf::GetSymbolPtr(const char* symbol) const {
|
2014-10-16 04:19:46 +04:00
|
|
|
return GetSymbolPtr(symbol, Hash(symbol));
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void* BaseElf::GetSymbolPtr(const char* symbol, unsigned long hash) const {
|
|
|
|
const Sym* sym = GetSymbol(symbol, hash);
|
|
|
|
void* ptr = nullptr;
|
2014-10-07 02:42:18 +04:00
|
|
|
if (sym && sym->st_shndx != SHN_UNDEF) ptr = GetPtr(sym->st_value);
|
|
|
|
DEBUG_LOG("BaseElf::GetSymbolPtr(%p [\"%s\"], \"%s\") = %p",
|
2019-05-01 11:47:10 +03:00
|
|
|
reinterpret_cast<const void*>(this), GetPath(), symbol, ptr);
|
2014-10-07 02:42:18 +04:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const Sym* BaseElf::GetSymbol(const char* symbol, unsigned long hash) const {
|
2014-10-07 02:42:18 +04:00
|
|
|
/* Search symbol with the buckets and chains tables.
|
|
|
|
* The hash computed from the symbol name gives an index in the buckets
|
|
|
|
* table. The corresponding value in the bucket table is an index in the
|
|
|
|
* symbols table and in the chains table.
|
|
|
|
* If the corresponding symbol in the symbols table matches, we're done.
|
|
|
|
* Otherwise, the corresponding value in the chains table is a new index
|
|
|
|
* in both tables, which corresponding symbol is tested and so on and so
|
|
|
|
* forth */
|
|
|
|
size_t bucket = hash % buckets.numElements();
|
|
|
|
for (size_t y = buckets[bucket]; y != STN_UNDEF; y = chains[y]) {
|
|
|
|
if (strcmp(symbol, strtab.GetStringAt(symtab[y].st_name))) continue;
|
|
|
|
return &symtab[y];
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-10-16 04:19:46 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
bool BaseElf::Contains(void* addr) const { return base.Contains(addr); }
|
2014-10-16 04:19:46 +04:00
|
|
|
|
|
|
|
#ifdef __ARM_EABI__
|
2019-05-01 11:47:10 +03:00
|
|
|
const void* BaseElf::FindExidx(int* pcount) const {
|
2014-10-16 04:19:46 +04:00
|
|
|
if (arm_exidx) {
|
|
|
|
*pcount = arm_exidx.numElements();
|
|
|
|
return arm_exidx;
|
|
|
|
}
|
|
|
|
*pcount = 0;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
2014-10-16 04:20:06 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
already_AddRefed<LibHandle> LoadedElf::Create(const char* path,
|
|
|
|
void* base_addr) {
|
2014-10-16 04:20:06 +04:00
|
|
|
DEBUG_LOG("LoadedElf::Create(\"%s\", %p) = ...", path, base_addr);
|
|
|
|
|
|
|
|
uint8_t mapped;
|
|
|
|
/* If the page is not mapped, mincore returns an error. If base_addr is
|
|
|
|
* nullptr, as would happen if the corresponding binary is prelinked with
|
|
|
|
* the prelink look (but not with the android apriori tool), no page being
|
|
|
|
* mapped there (right?), mincore returns an error, too, which makes
|
|
|
|
* prelinked libraries on glibc unsupported. This is not an interesting
|
|
|
|
* use case for now, so don't try supporting that case.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
if (mincore(const_cast<void*>(base_addr), PageSize(), &mapped))
|
2014-10-16 04:20:06 +04:00
|
|
|
return nullptr;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<LoadedElf> elf = new LoadedElf(path);
|
2014-10-16 04:20:06 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const Ehdr* ehdr = Ehdr::validate(base_addr);
|
2014-10-16 04:20:06 +04:00
|
|
|
if (!ehdr) return nullptr;
|
|
|
|
|
|
|
|
Addr min_vaddr = (Addr)-1; // We want to find the lowest and biggest
|
|
|
|
Addr max_vaddr = 0; // virtual address used by this Elf.
|
2019-05-01 11:47:10 +03:00
|
|
|
const Phdr* dyn = nullptr;
|
2014-10-16 04:20:06 +04:00
|
|
|
#ifdef __ARM_EABI__
|
2019-05-01 11:47:10 +03:00
|
|
|
const Phdr* arm_exidx_phdr = nullptr;
|
2014-10-16 04:20:06 +04:00
|
|
|
#endif
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Array<Phdr> phdrs(reinterpret_cast<const char*>(ehdr) + ehdr->e_phoff,
|
2014-10-16 04:20:06 +04:00
|
|
|
ehdr->e_phnum);
|
|
|
|
for (auto phdr = phdrs.begin(); phdr < phdrs.end(); ++phdr) {
|
|
|
|
switch (phdr->p_type) {
|
|
|
|
case PT_LOAD:
|
|
|
|
if (phdr->p_vaddr < min_vaddr) min_vaddr = phdr->p_vaddr;
|
|
|
|
if (max_vaddr < phdr->p_vaddr + phdr->p_memsz)
|
|
|
|
max_vaddr = phdr->p_vaddr + phdr->p_memsz;
|
|
|
|
break;
|
|
|
|
case PT_DYNAMIC:
|
|
|
|
dyn = &*phdr;
|
|
|
|
break;
|
|
|
|
#ifdef __ARM_EABI__
|
|
|
|
case PT_ARM_EXIDX:
|
|
|
|
/* We cannot initialize arm_exidx here
|
|
|
|
because we don't have a base yet */
|
|
|
|
arm_exidx_phdr = &*phdr;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the lowest PT_LOAD virtual address in headers is not 0, then the ELF
|
|
|
|
* is either prelinked or a non-PIE executable. The former case is not
|
|
|
|
* possible, because base_addr would be nullptr and the mincore test above
|
|
|
|
* would already have made us return.
|
|
|
|
* For a non-PIE executable, PT_LOADs contain absolute addresses, so in
|
|
|
|
* practice, this means min_vaddr should be equal to base_addr. max_vaddr
|
|
|
|
* can thus be adjusted accordingly.
|
|
|
|
*/
|
|
|
|
if (min_vaddr != 0) {
|
2019-05-01 11:47:10 +03:00
|
|
|
void* min_vaddr_ptr =
|
|
|
|
reinterpret_cast<void*>(static_cast<uintptr_t>(min_vaddr));
|
2014-10-16 04:20:06 +04:00
|
|
|
if (min_vaddr_ptr != base_addr) {
|
|
|
|
LOG("%s: %p != %p", elf->GetPath(), min_vaddr_ptr, base_addr);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
max_vaddr -= min_vaddr;
|
|
|
|
}
|
|
|
|
if (!dyn) {
|
|
|
|
LOG("%s: No PT_DYNAMIC segment found", elf->GetPath());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
elf->base.Assign(base_addr, max_vaddr);
|
|
|
|
|
|
|
|
if (!elf->InitDyn(dyn)) return nullptr;
|
|
|
|
|
|
|
|
#ifdef __ARM_EABI__
|
|
|
|
if (arm_exidx_phdr)
|
|
|
|
elf->arm_exidx.InitSize(elf->GetPtr(arm_exidx_phdr->p_vaddr),
|
|
|
|
arm_exidx_phdr->p_memsz);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DEBUG_LOG("LoadedElf::Create(\"%s\", %p) = %p", path, base_addr,
|
2019-05-01 11:47:10 +03:00
|
|
|
static_cast<void*>(elf));
|
2014-10-16 04:20:06 +04:00
|
|
|
|
|
|
|
ElfLoader::Singleton.Register(elf);
|
2015-05-01 16:14:16 +03:00
|
|
|
return elf.forget();
|
2014-10-16 04:20:06 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
bool LoadedElf::InitDyn(const Phdr* pt_dyn) {
|
2014-10-16 04:20:06 +04:00
|
|
|
Array<Dyn> dyns;
|
|
|
|
dyns.InitSize(GetPtr<Dyn>(pt_dyn->p_vaddr), pt_dyn->p_filesz);
|
|
|
|
|
|
|
|
size_t symnum = 0;
|
|
|
|
for (auto dyn = dyns.begin(); dyn < dyns.end() && dyn->d_tag; ++dyn) {
|
|
|
|
switch (dyn->d_tag) {
|
|
|
|
case DT_HASH: {
|
2017-05-17 20:06:22 +03:00
|
|
|
DEBUG_LOG("%s 0x%08" PRIxPTR, "DT_HASH", uintptr_t(dyn->d_un.d_val));
|
2019-05-01 11:47:10 +03:00
|
|
|
const Elf::Word* hash_table_header = GetPtr<Elf::Word>(dyn->d_un.d_ptr);
|
2014-10-16 04:20:06 +04:00
|
|
|
symnum = hash_table_header[1];
|
|
|
|
buckets.Init(&hash_table_header[2], hash_table_header[0]);
|
|
|
|
chains.Init(&*buckets.end());
|
|
|
|
} break;
|
|
|
|
case DT_STRTAB:
|
2017-05-17 20:06:22 +03:00
|
|
|
DEBUG_LOG("%s 0x%08" PRIxPTR, "DT_STRTAB", uintptr_t(dyn->d_un.d_val));
|
2014-10-16 04:20:06 +04:00
|
|
|
strtab.Init(GetPtr(dyn->d_un.d_ptr));
|
|
|
|
break;
|
|
|
|
case DT_SYMTAB:
|
2017-05-17 20:06:22 +03:00
|
|
|
DEBUG_LOG("%s 0x%08" PRIxPTR, "DT_SYMTAB", uintptr_t(dyn->d_un.d_val));
|
2014-10-16 04:20:06 +04:00
|
|
|
symtab.Init(GetPtr(dyn->d_un.d_ptr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!buckets || !symnum) {
|
|
|
|
ERROR("%s: Missing or broken DT_HASH", GetPath());
|
|
|
|
} else if (!strtab) {
|
|
|
|
ERROR("%s: Missing DT_STRTAB", GetPath());
|
|
|
|
} else if (!symtab) {
|
|
|
|
ERROR("%s: Missing DT_SYMTAB", GetPath());
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|