2017-07-05 18:23:32 -07:00
|
|
|
// 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.
|
|
|
|
|
2017-09-08 12:49:55 -07:00
|
|
|
#include "snapshot/elf/elf_dynamic_array_reader.h"
|
2017-07-05 18:23:32 -07:00
|
|
|
|
|
|
|
#include <elf.h>
|
|
|
|
|
2018-01-08 15:10:31 -08:00
|
|
|
#include <type_traits>
|
|
|
|
|
2017-07-05 18:23:32 -07:00
|
|
|
#include "util/stdlib/map_insert.h"
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <typename DynType>
|
|
|
|
bool Read(const ProcessMemoryRange& memory,
|
2017-09-12 16:49:35 -07:00
|
|
|
VMAddress address,
|
|
|
|
VMSize size,
|
2017-07-05 18:23:32 -07:00
|
|
|
std::map<uint64_t, uint64_t>* values) {
|
|
|
|
std::map<uint64_t, uint64_t> local_values;
|
|
|
|
|
|
|
|
while (size > 0) {
|
|
|
|
DynType entry;
|
|
|
|
if (!memory.Read(address, sizeof(entry), &entry)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size -= sizeof(entry);
|
|
|
|
address += sizeof(entry);
|
|
|
|
|
|
|
|
switch (entry.d_tag) {
|
|
|
|
case DT_NULL:
|
|
|
|
values->swap(local_values);
|
|
|
|
if (size != 0) {
|
|
|
|
LOG(WARNING) << size << " trailing bytes not read";
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case DT_NEEDED:
|
|
|
|
// Skip these entries for now.
|
|
|
|
break;
|
|
|
|
default:
|
2018-01-08 15:10:31 -08:00
|
|
|
static_assert(std::is_unsigned<decltype(entry.d_un.d_ptr)>::value,
|
|
|
|
"type must be unsigned");
|
|
|
|
static_assert(static_cast<void*>(&entry.d_un.d_ptr) ==
|
|
|
|
static_cast<void*>(&entry.d_un.d_val) &&
|
|
|
|
sizeof(entry.d_un.d_ptr) == sizeof(entry.d_un.d_val),
|
|
|
|
"d_ptr and d_val must be aliases");
|
2017-07-05 18:23:32 -07:00
|
|
|
if (!MapInsertOrReplace(
|
2018-01-08 15:10:31 -08:00
|
|
|
&local_values, entry.d_tag, entry.d_un.d_ptr, nullptr)) {
|
2017-07-05 18:23:32 -07:00
|
|
|
LOG(ERROR) << "duplicate dynamic array entry";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG(ERROR) << "missing DT_NULL";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ElfDynamicArrayReader::ElfDynamicArrayReader() : values_() {}
|
|
|
|
|
|
|
|
ElfDynamicArrayReader::~ElfDynamicArrayReader() {}
|
|
|
|
|
|
|
|
bool ElfDynamicArrayReader::Initialize(const ProcessMemoryRange& memory,
|
2017-09-12 16:49:35 -07:00
|
|
|
VMAddress address,
|
|
|
|
VMSize size) {
|
2017-07-05 18:23:32 -07:00
|
|
|
return memory.Is64Bit() ? Read<Elf64_Dyn>(memory, address, size, &values_)
|
|
|
|
: Read<Elf32_Dyn>(memory, address, size, &values_);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace crashpad
|