| 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 | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 32 | bool MapInfo::InitFileMemoryFromPreviousReadOnlyMap(MemoryFileAtOffset* memory) { | 
|  | 33 | // One last attempt, see if the previous map is read-only with the | 
|  | 34 | // same name and stretches across this map. | 
|  | 35 | for (auto iter = maps_->begin(); iter != maps_->end(); ++iter) { | 
|  | 36 | if (*iter == this) { | 
|  | 37 | if (iter == maps_->begin()) { | 
|  | 38 | return false; | 
|  | 39 | } | 
|  | 40 | --iter; | 
|  | 41 | MapInfo* prev_map = *iter; | 
|  | 42 | // Make sure this is a read-only map. | 
|  | 43 | if (prev_map->flags != PROT_READ) { | 
|  | 44 | return false; | 
|  | 45 | } | 
|  | 46 | uint64_t map_size = end - prev_map->end; | 
|  | 47 | if (!memory->Init(name, prev_map->offset, map_size)) { | 
|  | 48 | return false; | 
|  | 49 | } | 
|  | 50 | uint64_t max_size; | 
|  | 51 | if (!Elf::GetInfo(memory, &max_size) || max_size < map_size) { | 
|  | 52 | return false; | 
|  | 53 | } | 
|  | 54 | if (!memory->Init(name, prev_map->offset, max_size)) { | 
|  | 55 | return false; | 
|  | 56 | } | 
|  | 57 | elf_offset = offset - prev_map->offset; | 
|  | 58 | return true; | 
|  | 59 | } | 
|  | 60 | } | 
|  | 61 | return false; | 
|  | 62 | } | 
|  | 63 |  | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 64 | Memory* MapInfo::GetFileMemory() { | 
|  | 65 | std::unique_ptr<MemoryFileAtOffset> memory(new MemoryFileAtOffset); | 
|  | 66 | if (offset == 0) { | 
|  | 67 | if (memory->Init(name, 0)) { | 
|  | 68 | return memory.release(); | 
|  | 69 | } | 
|  | 70 | return nullptr; | 
|  | 71 | } | 
|  | 72 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 73 | // These are the possibilities when the offset is non-zero. | 
|  | 74 | // - There is an elf file embedded in a file, and the offset is the | 
|  | 75 | //   the start of the elf in the file. | 
|  | 76 | // - There is an elf file embedded in a file, and the offset is the | 
|  | 77 | //   the start of the executable part of the file. The actual start | 
|  | 78 | //   of the elf is in the read-only segment preceeding this map. | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 79 | // - The whole file is an elf file, and the offset needs to be saved. | 
|  | 80 | // | 
|  | 81 | // Map in just the part of the file for the map. If this is not | 
|  | 82 | // a valid elf, then reinit as if the whole file is an elf file. | 
|  | 83 | // If the offset is a valid elf, then determine the size of the map | 
|  | 84 | // and reinit to that size. This is needed because the dynamic linker | 
|  | 85 | // only maps in a portion of the original elf, and never the symbol | 
|  | 86 | // file data. | 
|  | 87 | uint64_t map_size = end - start; | 
|  | 88 | if (!memory->Init(name, offset, map_size)) { | 
|  | 89 | return nullptr; | 
|  | 90 | } | 
|  | 91 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 92 | // Check if the start of this map is an embedded elf. | 
|  | 93 | uint64_t max_size = 0; | 
|  | 94 | uint64_t file_offset = offset; | 
|  | 95 | if (Elf::GetInfo(memory.get(), &max_size)) { | 
|  | 96 | if (max_size > map_size) { | 
|  | 97 | if (memory->Init(name, file_offset, max_size)) { | 
|  | 98 | return memory.release(); | 
|  | 99 | } | 
|  | 100 | // Try to reinit using the default map_size. | 
|  | 101 | if (memory->Init(name, file_offset, map_size)) { | 
|  | 102 | return memory.release(); | 
|  | 103 | } | 
|  | 104 | return nullptr; | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 105 | } | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 106 | return memory.release(); | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 109 | // No elf at offset, try to init as if the whole file is an elf. | 
|  | 110 | if (memory->Init(name, 0) && Elf::IsValidElf(memory.get())) { | 
|  | 111 | elf_offset = offset; | 
|  | 112 | return memory.release(); | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 113 | } | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 114 |  | 
|  | 115 | // See if the map previous to this one contains a read-only map | 
|  | 116 | // that represents the real start of the elf data. | 
|  | 117 | if (InitFileMemoryFromPreviousReadOnlyMap(memory.get())) { | 
|  | 118 | return memory.release(); | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | // Failed to find elf at start of file or at read-only map, return | 
|  | 122 | // file object from the current map. | 
|  | 123 | if (memory->Init(name, offset, map_size)) { | 
|  | 124 | return memory.release(); | 
|  | 125 | } | 
|  | 126 | return nullptr; | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 127 | } | 
|  | 128 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 129 | Memory* MapInfo::CreateMemory(const std::shared_ptr<Memory>& process_memory) { | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 130 | if (end <= start) { | 
|  | 131 | return nullptr; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | elf_offset = 0; | 
|  | 135 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 136 | // Fail on device maps. | 
|  | 137 | if (flags & MAPS_FLAGS_DEVICE_MAP) { | 
|  | 138 | return nullptr; | 
|  | 139 | } | 
|  | 140 |  | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 141 | // First try and use the file associated with the info. | 
|  | 142 | if (!name.empty()) { | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 143 | Memory* memory = GetFileMemory(); | 
|  | 144 | if (memory != nullptr) { | 
|  | 145 | return memory; | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 146 | } | 
|  | 147 | } | 
|  | 148 |  | 
| Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 149 | // Need to verify that this elf is valid. It's possible that | 
|  | 150 | // only part of the elf file to be mapped into memory is in the executable | 
|  | 151 | // map. In this case, there will be another read-only map that includes the | 
|  | 152 | // first part of the elf file. This is done if the linker rosegment | 
|  | 153 | // option is used. | 
|  | 154 | std::unique_ptr<MemoryRange> memory(new MemoryRange(process_memory, start, end - start, 0)); | 
| Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 155 | if (Elf::IsValidElf(memory.get())) { | 
| Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 156 | return memory.release(); | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | if (name.empty() || maps_ == nullptr) { | 
|  | 160 | return nullptr; | 
|  | 161 | } | 
|  | 162 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 163 | // Find the read-only map by looking at the previous map. The linker | 
|  | 164 | // doesn't guarantee that this invariant will always be true. However, | 
|  | 165 | // if that changes, there is likely something else that will change and | 
|  | 166 | // break something. | 
| Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 167 | MapInfo* ro_map_info = nullptr; | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 168 | for (auto iter = maps_->begin(); iter != maps_->end(); ++iter) { | 
|  | 169 | if (*iter == this) { | 
|  | 170 | if (iter != maps_->begin()) { | 
|  | 171 | --iter; | 
|  | 172 | ro_map_info = *iter; | 
|  | 173 | } | 
|  | 174 | break; | 
| Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 175 | } | 
|  | 176 | } | 
|  | 177 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 178 | if (ro_map_info == nullptr || ro_map_info->name != name || ro_map_info->offset >= offset) { | 
| Christopher Ferris | 5565906 | 2018-11-15 14:06:26 -0800 | [diff] [blame] | 179 | return nullptr; | 
| Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 180 | } | 
| Christopher Ferris | 5565906 | 2018-11-15 14:06:26 -0800 | [diff] [blame] | 181 |  | 
|  | 182 | // Make sure that relative pc values are corrected properly. | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 183 | elf_offset = offset - ro_map_info->offset; | 
| Christopher Ferris | 5565906 | 2018-11-15 14:06:26 -0800 | [diff] [blame] | 184 |  | 
|  | 185 | MemoryRanges* ranges = new MemoryRanges; | 
|  | 186 | ranges->Insert(new MemoryRange(process_memory, ro_map_info->start, | 
|  | 187 | ro_map_info->end - ro_map_info->start, 0)); | 
|  | 188 | ranges->Insert(new MemoryRange(process_memory, start, end - start, elf_offset)); | 
|  | 189 |  | 
|  | 190 | return ranges; | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 191 | } | 
|  | 192 |  | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 193 | Elf* MapInfo::GetElf(const std::shared_ptr<Memory>& process_memory, ArchEnum expected_arch) { | 
| Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 194 | // Make sure no other thread is trying to add the elf to this map. | 
|  | 195 | std::lock_guard<std::mutex> guard(mutex_); | 
|  | 196 |  | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 197 | if (elf.get() != nullptr) { | 
|  | 198 | return elf.get(); | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 201 | bool locked = false; | 
|  | 202 | if (Elf::CachingEnabled() && !name.empty()) { | 
|  | 203 | Elf::CacheLock(); | 
|  | 204 | locked = true; | 
| Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 205 | if (Elf::CacheGet(this)) { | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 206 | Elf::CacheUnlock(); | 
|  | 207 | return elf.get(); | 
|  | 208 | } | 
|  | 209 | } | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 210 |  | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 211 | Memory* memory = CreateMemory(process_memory); | 
| Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 212 | if (locked) { | 
|  | 213 | if (Elf::CacheAfterCreateMemory(this)) { | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 214 | delete memory; | 
|  | 215 | Elf::CacheUnlock(); | 
|  | 216 | return elf.get(); | 
|  | 217 | } | 
|  | 218 | } | 
|  | 219 | elf.reset(new Elf(memory)); | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 220 | // If the init fails, keep the elf around as an invalid object so we | 
|  | 221 | // don't try to reinit the object. | 
| Christopher Ferris | e8c4ecf | 2018-10-23 12:04:26 -0700 | [diff] [blame] | 222 | elf->Init(); | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 223 | if (elf->valid() && expected_arch != elf->arch()) { | 
|  | 224 | // Make the elf invalid, mismatch between arch and expected arch. | 
|  | 225 | elf->Invalidate(); | 
|  | 226 | } | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 227 |  | 
|  | 228 | if (locked) { | 
|  | 229 | Elf::CacheAdd(this); | 
|  | 230 | Elf::CacheUnlock(); | 
|  | 231 | } | 
|  | 232 | return elf.get(); | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 233 | } | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 234 |  | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 235 | uint64_t MapInfo::GetLoadBias(const std::shared_ptr<Memory>& process_memory) { | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 236 | uint64_t cur_load_bias = load_bias.load(); | 
|  | 237 | if (cur_load_bias != static_cast<uint64_t>(-1)) { | 
|  | 238 | return cur_load_bias; | 
|  | 239 | } | 
|  | 240 |  | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 241 | { | 
|  | 242 | // Make sure no other thread is trying to add the elf to this map. | 
|  | 243 | std::lock_guard<std::mutex> guard(mutex_); | 
|  | 244 | if (elf != nullptr) { | 
|  | 245 | if (elf->valid()) { | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 246 | cur_load_bias = elf->GetLoadBias(); | 
|  | 247 | load_bias = cur_load_bias; | 
|  | 248 | return cur_load_bias; | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 249 | } else { | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 250 | load_bias = 0; | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 251 | return 0; | 
|  | 252 | } | 
|  | 253 | } | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | // Call lightweight static function that will only read enough of the | 
|  | 257 | // elf data to get the load bias. | 
|  | 258 | std::unique_ptr<Memory> memory(CreateMemory(process_memory)); | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 259 | cur_load_bias = Elf::GetLoadBias(memory.get()); | 
|  | 260 | load_bias = cur_load_bias; | 
|  | 261 | return cur_load_bias; | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 264 | }  // namespace unwindstack |