2017-04-20 00:42:19 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* 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-01 20:17:16 +02:00
|
|
|
#include <sys/mman.h>
|
2017-04-20 00:42:19 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <memory>
|
2017-11-27 23:50:38 +01:00
|
|
|
#include <mutex>
|
2017-04-20 00:42:19 +02:00
|
|
|
#include <string>
|
|
|
|
|
2017-07-14 19:37:19 +02:00
|
|
|
#include <unwindstack/Elf.h>
|
|
|
|
#include <unwindstack/MapInfo.h>
|
|
|
|
#include <unwindstack/Maps.h>
|
|
|
|
#include <unwindstack/Memory.h>
|
|
|
|
|
|
|
|
namespace unwindstack {
|
2017-04-20 00:42:19 +02:00
|
|
|
|
2018-12-10 20:13:23 +01:00
|
|
|
bool MapInfo::InitFileMemoryFromPreviousReadOnlyMap(MemoryFileAtOffset* memory) {
|
|
|
|
// One last attempt, see if the previous map is read-only with the
|
|
|
|
// same name and stretches across this map.
|
2018-12-14 01:08:50 +01:00
|
|
|
if (prev_map == nullptr || prev_map->flags != PROT_READ) {
|
|
|
|
return false;
|
2018-12-10 20:13:23 +01:00
|
|
|
}
|
2018-12-14 01:08:50 +01:00
|
|
|
|
|
|
|
uint64_t map_size = end - prev_map->end;
|
|
|
|
if (!memory->Init(name, prev_map->offset, map_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t max_size;
|
|
|
|
if (!Elf::GetInfo(memory, &max_size) || max_size < map_size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!memory->Init(name, prev_map->offset, max_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
elf_offset = offset - prev_map->offset;
|
|
|
|
elf_start_offset = prev_map->offset;
|
|
|
|
return true;
|
2018-12-10 20:13:23 +01:00
|
|
|
}
|
|
|
|
|
2017-08-30 22:15:19 +02:00
|
|
|
Memory* MapInfo::GetFileMemory() {
|
|
|
|
std::unique_ptr<MemoryFileAtOffset> memory(new MemoryFileAtOffset);
|
|
|
|
if (offset == 0) {
|
|
|
|
if (memory->Init(name, 0)) {
|
|
|
|
return memory.release();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-10 20:13:23 +01:00
|
|
|
// These are the possibilities when the offset is non-zero.
|
|
|
|
// - There is an elf file embedded in a file, and the offset is the
|
|
|
|
// the start of the elf in the file.
|
|
|
|
// - There is an elf file embedded in a file, and the offset is the
|
|
|
|
// the start of the executable part of the file. The actual start
|
|
|
|
// of the elf is in the read-only segment preceeding this map.
|
2017-08-30 22:15:19 +02:00
|
|
|
// - The whole file is an elf file, and the offset needs to be saved.
|
|
|
|
//
|
|
|
|
// Map in just the part of the file for the map. If this is not
|
|
|
|
// a valid elf, then reinit as if the whole file is an elf file.
|
|
|
|
// If the offset is a valid elf, then determine the size of the map
|
|
|
|
// and reinit to that size. This is needed because the dynamic linker
|
|
|
|
// only maps in a portion of the original elf, and never the symbol
|
|
|
|
// file data.
|
|
|
|
uint64_t map_size = end - start;
|
|
|
|
if (!memory->Init(name, offset, map_size)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-10 20:13:23 +01:00
|
|
|
// Check if the start of this map is an embedded elf.
|
|
|
|
uint64_t max_size = 0;
|
|
|
|
if (Elf::GetInfo(memory.get(), &max_size)) {
|
|
|
|
if (max_size > map_size) {
|
2018-12-14 01:08:50 +01:00
|
|
|
if (memory->Init(name, offset, max_size)) {
|
2018-12-10 20:13:23 +01:00
|
|
|
return memory.release();
|
|
|
|
}
|
|
|
|
// Try to reinit using the default map_size.
|
2018-12-14 01:08:50 +01:00
|
|
|
if (memory->Init(name, offset, map_size)) {
|
2018-12-10 20:13:23 +01:00
|
|
|
return memory.release();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2017-08-30 22:15:19 +02:00
|
|
|
}
|
2018-12-10 20:13:23 +01:00
|
|
|
return memory.release();
|
2017-08-30 22:15:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 20:13:23 +01:00
|
|
|
// No elf at offset, try to init as if the whole file is an elf.
|
|
|
|
if (memory->Init(name, 0) && Elf::IsValidElf(memory.get())) {
|
|
|
|
elf_offset = offset;
|
2018-12-14 01:08:50 +01:00
|
|
|
// Need to check how to set the elf start offset. If this map is not
|
|
|
|
// the r-x map of a r-- map, then use the real offset value. Otherwise,
|
|
|
|
// use 0.
|
|
|
|
if (prev_map == nullptr || prev_map->offset != 0 || prev_map->flags != PROT_READ ||
|
|
|
|
prev_map->name != name) {
|
|
|
|
elf_start_offset = offset;
|
|
|
|
}
|
2018-12-10 20:13:23 +01:00
|
|
|
return memory.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if the map previous to this one contains a read-only map
|
|
|
|
// that represents the real start of the elf data.
|
|
|
|
if (InitFileMemoryFromPreviousReadOnlyMap(memory.get())) {
|
|
|
|
return memory.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failed to find elf at start of file or at read-only map, return
|
|
|
|
// file object from the current map.
|
|
|
|
if (memory->Init(name, offset, map_size)) {
|
|
|
|
return memory.release();
|
2017-08-30 22:15:19 +02:00
|
|
|
}
|
2018-12-10 20:13:23 +01:00
|
|
|
return nullptr;
|
2017-08-30 22:15:19 +02:00
|
|
|
}
|
|
|
|
|
2017-09-01 20:17:16 +02:00
|
|
|
Memory* MapInfo::CreateMemory(const std::shared_ptr<Memory>& process_memory) {
|
2017-04-20 00:42:19 +02:00
|
|
|
if (end <= start) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
elf_offset = 0;
|
|
|
|
|
2017-09-01 20:17:16 +02:00
|
|
|
// Fail on device maps.
|
|
|
|
if (flags & MAPS_FLAGS_DEVICE_MAP) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:42:19 +02:00
|
|
|
// First try and use the file associated with the info.
|
|
|
|
if (!name.empty()) {
|
2017-08-30 22:15:19 +02:00
|
|
|
Memory* memory = GetFileMemory();
|
|
|
|
if (memory != nullptr) {
|
|
|
|
return memory;
|
2017-04-20 00:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 02:23:39 +01:00
|
|
|
if (process_memory == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-02 06:01:09 +02:00
|
|
|
// Need to verify that this elf is valid. It's possible that
|
|
|
|
// only part of the elf file to be mapped into memory is in the executable
|
|
|
|
// map. In this case, there will be another read-only map that includes the
|
|
|
|
// first part of the elf file. This is done if the linker rosegment
|
|
|
|
// option is used.
|
|
|
|
std::unique_ptr<MemoryRange> memory(new MemoryRange(process_memory, start, end - start, 0));
|
2018-10-10 03:49:11 +02:00
|
|
|
if (Elf::IsValidElf(memory.get())) {
|
2018-10-02 06:01:09 +02:00
|
|
|
return memory.release();
|
|
|
|
}
|
|
|
|
|
2018-12-10 20:13:23 +01:00
|
|
|
// Find the read-only map by looking at the previous map. The linker
|
|
|
|
// doesn't guarantee that this invariant will always be true. However,
|
|
|
|
// if that changes, there is likely something else that will change and
|
|
|
|
// break something.
|
2018-12-14 01:08:50 +01:00
|
|
|
if (offset == 0 || name.empty() || prev_map == nullptr || prev_map->name != name ||
|
|
|
|
prev_map->offset >= offset) {
|
2018-11-15 23:06:26 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-02 06:01:09 +02:00
|
|
|
|
2018-11-15 23:06:26 +01:00
|
|
|
// Make sure that relative pc values are corrected properly.
|
2018-12-14 01:08:50 +01:00
|
|
|
elf_offset = offset - prev_map->offset;
|
|
|
|
// Use this as the elf start offset, otherwise, you always get offsets into
|
|
|
|
// the r-x section, which is not quite the right information.
|
|
|
|
elf_start_offset = prev_map->offset;
|
2018-10-02 06:01:09 +02:00
|
|
|
|
2018-11-15 23:06:26 +01:00
|
|
|
MemoryRanges* ranges = new MemoryRanges;
|
2018-12-14 01:08:50 +01:00
|
|
|
ranges->Insert(
|
|
|
|
new MemoryRange(process_memory, prev_map->start, prev_map->end - prev_map->start, 0));
|
2018-11-15 23:06:26 +01:00
|
|
|
ranges->Insert(new MemoryRange(process_memory, start, end - start, elf_offset));
|
|
|
|
|
|
|
|
return ranges;
|
2017-04-20 00:42:19 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 02:42:41 +02:00
|
|
|
Elf* MapInfo::GetElf(const std::shared_ptr<Memory>& process_memory, ArchEnum expected_arch) {
|
2017-11-27 23:50:38 +01:00
|
|
|
// Make sure no other thread is trying to add the elf to this map.
|
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
|
|
|
|
2018-01-25 21:15:56 +01:00
|
|
|
if (elf.get() != nullptr) {
|
|
|
|
return elf.get();
|
2017-04-20 00:42:19 +02:00
|
|
|
}
|
|
|
|
|
2018-01-25 21:15:56 +01:00
|
|
|
bool locked = false;
|
|
|
|
if (Elf::CachingEnabled() && !name.empty()) {
|
|
|
|
Elf::CacheLock();
|
|
|
|
locked = true;
|
2018-02-16 22:48:19 +01:00
|
|
|
if (Elf::CacheGet(this)) {
|
2018-01-25 21:15:56 +01:00
|
|
|
Elf::CacheUnlock();
|
|
|
|
return elf.get();
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 01:08:58 +02:00
|
|
|
|
2018-01-25 21:15:56 +01:00
|
|
|
Memory* memory = CreateMemory(process_memory);
|
2018-02-16 22:48:19 +01:00
|
|
|
if (locked) {
|
|
|
|
if (Elf::CacheAfterCreateMemory(this)) {
|
2018-01-25 21:15:56 +01:00
|
|
|
delete memory;
|
|
|
|
Elf::CacheUnlock();
|
|
|
|
return elf.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elf.reset(new Elf(memory));
|
2017-04-20 00:42:19 +02:00
|
|
|
// If the init fails, keep the elf around as an invalid object so we
|
|
|
|
// don't try to reinit the object.
|
2018-10-23 21:04:26 +02:00
|
|
|
elf->Init();
|
2018-10-24 02:42:41 +02:00
|
|
|
if (elf->valid() && expected_arch != elf->arch()) {
|
|
|
|
// Make the elf invalid, mismatch between arch and expected arch.
|
|
|
|
elf->Invalidate();
|
|
|
|
}
|
2018-01-25 21:15:56 +01:00
|
|
|
|
|
|
|
if (locked) {
|
|
|
|
Elf::CacheAdd(this);
|
|
|
|
Elf::CacheUnlock();
|
|
|
|
}
|
|
|
|
return elf.get();
|
2017-04-20 00:42:19 +02:00
|
|
|
}
|
2017-07-14 19:37:19 +02:00
|
|
|
|
2018-12-06 00:57:02 +01:00
|
|
|
bool MapInfo::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
|
|
|
|
{
|
|
|
|
// Make sure no other thread is trying to update this elf object.
|
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
|
|
|
if (elf == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No longer need the lock, once the elf object is created, it is not deleted
|
|
|
|
// until this object is deleted.
|
|
|
|
return elf->GetFunctionName(addr, name, func_offset);
|
|
|
|
}
|
|
|
|
|
2017-12-02 06:37:37 +01:00
|
|
|
uint64_t MapInfo::GetLoadBias(const std::shared_ptr<Memory>& process_memory) {
|
2017-12-15 20:17:45 +01:00
|
|
|
uint64_t cur_load_bias = load_bias.load();
|
|
|
|
if (cur_load_bias != static_cast<uint64_t>(-1)) {
|
|
|
|
return cur_load_bias;
|
|
|
|
}
|
|
|
|
|
2017-12-02 06:37:37 +01:00
|
|
|
{
|
|
|
|
// Make sure no other thread is trying to add the elf to this map.
|
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
|
|
|
if (elf != nullptr) {
|
|
|
|
if (elf->valid()) {
|
2017-12-15 20:17:45 +01:00
|
|
|
cur_load_bias = elf->GetLoadBias();
|
|
|
|
load_bias = cur_load_bias;
|
|
|
|
return cur_load_bias;
|
2017-12-02 06:37:37 +01:00
|
|
|
} else {
|
2017-12-15 20:17:45 +01:00
|
|
|
load_bias = 0;
|
2017-12-02 06:37:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call lightweight static function that will only read enough of the
|
|
|
|
// elf data to get the load bias.
|
|
|
|
std::unique_ptr<Memory> memory(CreateMemory(process_memory));
|
2017-12-15 20:17:45 +01:00
|
|
|
cur_load_bias = Elf::GetLoadBias(memory.get());
|
|
|
|
load_bias = cur_load_bias;
|
|
|
|
return cur_load_bias;
|
2017-12-02 06:37:37 +01:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:23:39 +01:00
|
|
|
MapInfo::~MapInfo() {
|
|
|
|
uintptr_t id = build_id.load();
|
|
|
|
if (id != 0) {
|
|
|
|
delete reinterpret_cast<std::string*>(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MapInfo::GetBuildID() {
|
|
|
|
uintptr_t id = build_id.load();
|
|
|
|
if (build_id != 0) {
|
|
|
|
return *reinterpret_cast<std::string*>(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to lock, at worst if multiple threads do this at the same
|
|
|
|
// time it should be detected and only one thread should win and
|
|
|
|
// save the data.
|
|
|
|
std::unique_ptr<std::string> cur_build_id(new std::string);
|
|
|
|
|
|
|
|
// Now need to see if the elf object exists.
|
|
|
|
// Make sure no other thread is trying to add the elf to this map.
|
|
|
|
mutex_.lock();
|
|
|
|
Elf* elf_obj = elf.get();
|
|
|
|
mutex_.unlock();
|
|
|
|
if (elf_obj != nullptr) {
|
|
|
|
*cur_build_id = elf_obj->GetBuildID();
|
|
|
|
} else {
|
|
|
|
// This will only work if we can get the file associated with this memory.
|
|
|
|
// If this is only available in memory, then the section name information
|
|
|
|
// is not present and we will not be able to find the build id info.
|
|
|
|
std::unique_ptr<Memory> memory(GetFileMemory());
|
|
|
|
if (memory != nullptr) {
|
|
|
|
*cur_build_id = Elf::GetBuildID(memory.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
id = reinterpret_cast<uintptr_t>(cur_build_id.get());
|
|
|
|
uintptr_t expected_id = 0;
|
|
|
|
if (build_id.compare_exchange_weak(expected_id, id)) {
|
|
|
|
// Value saved, so make sure the memory is not freed.
|
|
|
|
cur_build_id.release();
|
|
|
|
}
|
|
|
|
return *reinterpret_cast<std::string*>(id);
|
|
|
|
}
|
|
|
|
|
2017-07-14 19:37:19 +02:00
|
|
|
} // namespace unwindstack
|