Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 17 | #include <sys/mman.h> |
Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 18 | #include <sys/types.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <memory> |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 22 | #include <mutex> |
Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 25 | #include <unwindstack/Elf.h> |
| 26 | #include <unwindstack/MapInfo.h> |
| 27 | #include <unwindstack/Maps.h> |
| 28 | #include <unwindstack/Memory.h> |
| 29 | |
| 30 | namespace unwindstack { |
Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 31 | |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 32 | Memory* MapInfo::GetFileMemory() { |
| 33 | std::unique_ptr<MemoryFileAtOffset> memory(new MemoryFileAtOffset); |
| 34 | if (offset == 0) { |
| 35 | if (memory->Init(name, 0)) { |
| 36 | return memory.release(); |
| 37 | } |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
| 41 | // There are two possibilities when the offset is non-zero. |
| 42 | // - There is an elf file embedded in a file. |
| 43 | // - The whole file is an elf file, and the offset needs to be saved. |
| 44 | // |
| 45 | // Map in just the part of the file for the map. If this is not |
| 46 | // a valid elf, then reinit as if the whole file is an elf file. |
| 47 | // If the offset is a valid elf, then determine the size of the map |
| 48 | // and reinit to that size. This is needed because the dynamic linker |
| 49 | // only maps in a portion of the original elf, and never the symbol |
| 50 | // file data. |
| 51 | uint64_t map_size = end - start; |
| 52 | if (!memory->Init(name, offset, map_size)) { |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
| 56 | bool valid; |
| 57 | uint64_t max_size; |
| 58 | Elf::GetInfo(memory.get(), &valid, &max_size); |
| 59 | if (!valid) { |
| 60 | // Init as if the whole file is an elf. |
| 61 | if (memory->Init(name, 0)) { |
| 62 | elf_offset = offset; |
| 63 | return memory.release(); |
| 64 | } |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
| 68 | if (max_size > map_size) { |
| 69 | if (memory->Init(name, offset, max_size)) { |
| 70 | return memory.release(); |
| 71 | } |
| 72 | // Try to reinit using the default map_size. |
| 73 | if (memory->Init(name, offset, map_size)) { |
| 74 | return memory.release(); |
| 75 | } |
| 76 | return nullptr; |
| 77 | } |
| 78 | return memory.release(); |
| 79 | } |
| 80 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 81 | Memory* MapInfo::CreateMemory(const std::shared_ptr<Memory>& process_memory) { |
Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 82 | if (end <= start) { |
| 83 | return nullptr; |
| 84 | } |
| 85 | |
| 86 | elf_offset = 0; |
| 87 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 88 | // Fail on device maps. |
| 89 | if (flags & MAPS_FLAGS_DEVICE_MAP) { |
| 90 | return nullptr; |
| 91 | } |
| 92 | |
Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 93 | // First try and use the file associated with the info. |
| 94 | if (!name.empty()) { |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 95 | Memory* memory = GetFileMemory(); |
| 96 | if (memory != nullptr) { |
| 97 | return memory; |
Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 101 | // If the map isn't readable, don't bother trying to read from process memory. |
| 102 | if (!(flags & PROT_READ)) { |
| 103 | return nullptr; |
Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 104 | } |
Josh Gao | 29c5378 | 2017-09-26 14:11:37 -0700 | [diff] [blame] | 105 | return new MemoryRange(process_memory, start, end - start, 0); |
Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 108 | Elf* MapInfo::GetElf(const std::shared_ptr<Memory>& process_memory, bool init_gnu_debugdata) { |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 109 | // Make sure no other thread is trying to add the elf to this map. |
| 110 | std::lock_guard<std::mutex> guard(mutex_); |
| 111 | |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame^] | 112 | if (elf.get() != nullptr) { |
| 113 | return elf.get(); |
Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame^] | 116 | bool locked = false; |
| 117 | if (Elf::CachingEnabled() && !name.empty()) { |
| 118 | Elf::CacheLock(); |
| 119 | locked = true; |
| 120 | if (offset != 0) { |
| 121 | std::string hash(name + ':' + std::to_string(offset)); |
| 122 | if (Elf::CacheGet(hash, &elf)) { |
| 123 | Elf::CacheUnlock(); |
| 124 | return elf.get(); |
| 125 | } |
| 126 | } else if (Elf::CacheGet(name, &elf)) { |
| 127 | Elf::CacheUnlock(); |
| 128 | return elf.get(); |
| 129 | } |
| 130 | } |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 131 | |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame^] | 132 | Memory* memory = CreateMemory(process_memory); |
| 133 | if (locked && offset != 0 && elf_offset != 0) { |
| 134 | // In this case, the whole file is the elf, need to see if the elf |
| 135 | // data was cached. |
| 136 | if (Elf::CacheGet(name, &elf)) { |
| 137 | delete memory; |
| 138 | Elf::CacheUnlock(); |
| 139 | return elf.get(); |
| 140 | } |
| 141 | } |
| 142 | elf.reset(new Elf(memory)); |
Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 143 | // If the init fails, keep the elf around as an invalid object so we |
| 144 | // don't try to reinit the object. |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame^] | 145 | elf->Init(init_gnu_debugdata); |
| 146 | |
| 147 | if (locked) { |
| 148 | Elf::CacheAdd(this); |
| 149 | Elf::CacheUnlock(); |
| 150 | } |
| 151 | return elf.get(); |
Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 152 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 153 | |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 154 | uint64_t MapInfo::GetLoadBias(const std::shared_ptr<Memory>& process_memory) { |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 155 | uint64_t cur_load_bias = load_bias.load(); |
| 156 | if (cur_load_bias != static_cast<uint64_t>(-1)) { |
| 157 | return cur_load_bias; |
| 158 | } |
| 159 | |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 160 | { |
| 161 | // Make sure no other thread is trying to add the elf to this map. |
| 162 | std::lock_guard<std::mutex> guard(mutex_); |
| 163 | if (elf != nullptr) { |
| 164 | if (elf->valid()) { |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 165 | cur_load_bias = elf->GetLoadBias(); |
| 166 | load_bias = cur_load_bias; |
| 167 | return cur_load_bias; |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 168 | } else { |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 169 | load_bias = 0; |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Call lightweight static function that will only read enough of the |
| 176 | // elf data to get the load bias. |
| 177 | std::unique_ptr<Memory> memory(CreateMemory(process_memory)); |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 178 | cur_load_bias = Elf::GetLoadBias(memory.get()); |
| 179 | load_bias = cur_load_bias; |
| 180 | return cur_load_bias; |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 183 | } // namespace unwindstack |