crashpad/snapshot/elf/elf_symbol_table_reader.cc
Scott Graham 1f1657d573 Read either DT_HASH or DT_GNU_HASH to determine the size of DT_SYMTAB
Without the section headers for the symbol table, there's no direct way
to calculate the number of entries in the table.

DT_HASH and DT_GNU_HASH are auxiliary tables that are designed to make
symbol lookup faster. DT_HASH is the original and is theoretically
mandatory. DT_GNU_HASH is the new-and-improved, but is more complex.

In practice, however, an Android build (at least vs. API 16) has only
DT_HASH, and not DT_GNU_HASH, and a Fuchsia build has only DT_GNU_HASH
but not DT_HASH. So, both are tried.

This change does not actually use the data in these tables to improve
the speed of symbol lookup, but instead only uses them to correctly
terminate the linear search.

DT_HASH contains the total number of symbols in the symbol table fairly
directly because there is an entry for each symbol table entry in the
hash table, so the number is the same.

DT_GNU_HASH regrettably does not. Instead, it's necessary to walk the
buckets and chain structure to find the largest entry.

DT_GNU_HASH doesn't appear in any "real" documentation that I'm aware
of, other than the binutils code (at least as far as I know). Some
more-and-less-useful references:
- https://flapenguin.me/2017/04/24/elf-lookup-dt-hash/
- https://flapenguin.me/2017/05/10/elf-lookup-dt-gnu-hash/
- http://deroko.phearless.org/dt_gnu_hash.txt
- https://sourceware.org/ml/binutils/2006-10/msg00377.html

Change-Id: I7cfc4372f29efc37446f0931d22a1f790e44076f
Bug: crashpad:213, crashpad:196
Reviewed-on: https://chromium-review.googlesource.com/876879
Commit-Queue: Scott Graham <scottmg@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
2018-01-30 22:40:17 +00:00

95 lines
2.8 KiB
C++

// Copyright 2017 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "snapshot/elf/elf_symbol_table_reader.h"
#include <elf.h>
#include "base/logging.h"
#include "snapshot/elf/elf_image_reader.h"
namespace crashpad {
namespace {
uint8_t GetBinding(const Elf32_Sym& sym) {
return ELF32_ST_BIND(sym.st_info);
}
uint8_t GetBinding(const Elf64_Sym& sym) {
return ELF64_ST_BIND(sym.st_info);
}
uint8_t GetType(const Elf32_Sym& sym) {
return ELF32_ST_TYPE(sym.st_info);
}
uint8_t GetType(const Elf64_Sym& sym) {
return ELF64_ST_TYPE(sym.st_info);
}
uint8_t GetVisibility(const Elf32_Sym& sym) {
return ELF32_ST_VISIBILITY(sym.st_other);
}
uint8_t GetVisibility(const Elf64_Sym& sym) {
return ELF64_ST_VISIBILITY(sym.st_other);
}
} // namespace
ElfSymbolTableReader::ElfSymbolTableReader(const ProcessMemoryRange* memory,
ElfImageReader* elf_reader,
VMAddress address,
VMSize num_entries)
: memory_(memory),
elf_reader_(elf_reader),
base_address_(address),
num_entries_(num_entries) {}
ElfSymbolTableReader::~ElfSymbolTableReader() {}
bool ElfSymbolTableReader::GetSymbol(const std::string& name,
SymbolInformation* info) {
return memory_->Is64Bit() ? ScanSymbolTable<Elf64_Sym>(name, info)
: ScanSymbolTable<Elf32_Sym>(name, info);
}
template <typename SymEnt>
bool ElfSymbolTableReader::ScanSymbolTable(const std::string& name,
SymbolInformation* info_out) {
VMAddress address = base_address_;
SymEnt entry;
std::string string;
size_t i = 0;
while (i < num_entries_ && memory_->Read(address, sizeof(entry), &entry)) {
if (elf_reader_->ReadDynamicStringTableAtOffset(entry.st_name, &string) &&
string == name) {
info_out->address = entry.st_value;
info_out->size = entry.st_size;
info_out->shndx = entry.st_shndx;
info_out->binding = GetBinding(entry);
info_out->type = GetType(entry);
info_out->visibility = GetVisibility(entry);
return true;
}
// TODO(scottmg): This should respect DT_SYMENT if present.
address += sizeof(entry);
++i;
}
return false;
}
} // namespace crashpad