| 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. | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 35 | if (prev_map == nullptr || prev_map->flags != PROT_READ) { | 
|  | 36 | return false; | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 37 | } | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | uint64_t map_size = end - prev_map->end; | 
|  | 40 | if (!memory->Init(name, prev_map->offset, map_size)) { | 
|  | 41 | return false; | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | uint64_t max_size; | 
|  | 45 | if (!Elf::GetInfo(memory, &max_size) || max_size < map_size) { | 
|  | 46 | return false; | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | if (!memory->Init(name, prev_map->offset, max_size)) { | 
|  | 50 | return false; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | elf_offset = offset - prev_map->offset; | 
|  | 54 | elf_start_offset = prev_map->offset; | 
|  | 55 | return true; | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 58 | Memory* MapInfo::GetFileMemory() { | 
|  | 59 | std::unique_ptr<MemoryFileAtOffset> memory(new MemoryFileAtOffset); | 
|  | 60 | if (offset == 0) { | 
|  | 61 | if (memory->Init(name, 0)) { | 
|  | 62 | return memory.release(); | 
|  | 63 | } | 
|  | 64 | return nullptr; | 
|  | 65 | } | 
|  | 66 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 67 | // These are the possibilities when the offset is non-zero. | 
|  | 68 | // - There is an elf file embedded in a file, and the offset is the | 
|  | 69 | //   the start of the elf in the file. | 
|  | 70 | // - There is an elf file embedded in a file, and the offset is the | 
|  | 71 | //   the start of the executable part of the file. The actual start | 
|  | 72 | //   of the elf is in the read-only segment preceeding this map. | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 73 | // - The whole file is an elf file, and the offset needs to be saved. | 
|  | 74 | // | 
|  | 75 | // Map in just the part of the file for the map. If this is not | 
|  | 76 | // a valid elf, then reinit as if the whole file is an elf file. | 
|  | 77 | // If the offset is a valid elf, then determine the size of the map | 
|  | 78 | // and reinit to that size. This is needed because the dynamic linker | 
|  | 79 | // only maps in a portion of the original elf, and never the symbol | 
|  | 80 | // file data. | 
|  | 81 | uint64_t map_size = end - start; | 
|  | 82 | if (!memory->Init(name, offset, map_size)) { | 
|  | 83 | return nullptr; | 
|  | 84 | } | 
|  | 85 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 86 | // Check if the start of this map is an embedded elf. | 
|  | 87 | uint64_t max_size = 0; | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 88 | if (Elf::GetInfo(memory.get(), &max_size)) { | 
|  | 89 | if (max_size > map_size) { | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 90 | if (memory->Init(name, offset, max_size)) { | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 91 | return memory.release(); | 
|  | 92 | } | 
|  | 93 | // Try to reinit using the default map_size. | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 94 | if (memory->Init(name, offset, map_size)) { | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 95 | return memory.release(); | 
|  | 96 | } | 
|  | 97 | return nullptr; | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 98 | } | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 99 | return memory.release(); | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 102 | // No elf at offset, try to init as if the whole file is an elf. | 
|  | 103 | if (memory->Init(name, 0) && Elf::IsValidElf(memory.get())) { | 
|  | 104 | elf_offset = offset; | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 105 | // Need to check how to set the elf start offset. If this map is not | 
|  | 106 | // the r-x map of a r-- map, then use the real offset value. Otherwise, | 
|  | 107 | // use 0. | 
|  | 108 | if (prev_map == nullptr || prev_map->offset != 0 || prev_map->flags != PROT_READ || | 
|  | 109 | prev_map->name != name) { | 
|  | 110 | elf_start_offset = offset; | 
|  | 111 | } | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 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 |  | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 159 | // Find the read-only map by looking at the previous map. The linker | 
|  | 160 | // doesn't guarantee that this invariant will always be true. However, | 
|  | 161 | // if that changes, there is likely something else that will change and | 
|  | 162 | // break something. | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 163 | if (offset == 0 || name.empty() || prev_map == nullptr || prev_map->name != name || | 
|  | 164 | prev_map->offset >= offset) { | 
| Christopher Ferris | 5565906 | 2018-11-15 14:06:26 -0800 | [diff] [blame] | 165 | return nullptr; | 
| Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 166 | } | 
| Christopher Ferris | 5565906 | 2018-11-15 14:06:26 -0800 | [diff] [blame] | 167 |  | 
|  | 168 | // Make sure that relative pc values are corrected properly. | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 169 | elf_offset = offset - prev_map->offset; | 
|  | 170 | // Use this as the elf start offset, otherwise, you always get offsets into | 
|  | 171 | // the r-x section, which is not quite the right information. | 
|  | 172 | elf_start_offset = prev_map->offset; | 
| Christopher Ferris | 5565906 | 2018-11-15 14:06:26 -0800 | [diff] [blame] | 173 |  | 
|  | 174 | MemoryRanges* ranges = new MemoryRanges; | 
| Christopher Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 175 | ranges->Insert( | 
|  | 176 | new MemoryRange(process_memory, prev_map->start, prev_map->end - prev_map->start, 0)); | 
| Christopher Ferris | 5565906 | 2018-11-15 14:06:26 -0800 | [diff] [blame] | 177 | ranges->Insert(new MemoryRange(process_memory, start, end - start, elf_offset)); | 
|  | 178 |  | 
|  | 179 | return ranges; | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 182 | 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] | 183 | // Make sure no other thread is trying to add the elf to this map. | 
|  | 184 | std::lock_guard<std::mutex> guard(mutex_); | 
|  | 185 |  | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 186 | if (elf.get() != nullptr) { | 
|  | 187 | return elf.get(); | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 190 | bool locked = false; | 
|  | 191 | if (Elf::CachingEnabled() && !name.empty()) { | 
|  | 192 | Elf::CacheLock(); | 
|  | 193 | locked = true; | 
| Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 194 | if (Elf::CacheGet(this)) { | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 195 | Elf::CacheUnlock(); | 
|  | 196 | return elf.get(); | 
|  | 197 | } | 
|  | 198 | } | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 199 |  | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 200 | Memory* memory = CreateMemory(process_memory); | 
| Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 201 | if (locked) { | 
|  | 202 | if (Elf::CacheAfterCreateMemory(this)) { | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 203 | delete memory; | 
|  | 204 | Elf::CacheUnlock(); | 
|  | 205 | return elf.get(); | 
|  | 206 | } | 
|  | 207 | } | 
|  | 208 | elf.reset(new Elf(memory)); | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 209 | // If the init fails, keep the elf around as an invalid object so we | 
|  | 210 | // don't try to reinit the object. | 
| Christopher Ferris | e8c4ecf | 2018-10-23 12:04:26 -0700 | [diff] [blame] | 211 | elf->Init(); | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 212 | if (elf->valid() && expected_arch != elf->arch()) { | 
|  | 213 | // Make the elf invalid, mismatch between arch and expected arch. | 
|  | 214 | elf->Invalidate(); | 
|  | 215 | } | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 216 |  | 
|  | 217 | if (locked) { | 
|  | 218 | Elf::CacheAdd(this); | 
|  | 219 | Elf::CacheUnlock(); | 
|  | 220 | } | 
|  | 221 | return elf.get(); | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 222 | } | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 223 |  | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 224 | uint64_t MapInfo::GetLoadBias(const std::shared_ptr<Memory>& process_memory) { | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 225 | uint64_t cur_load_bias = load_bias.load(); | 
|  | 226 | if (cur_load_bias != static_cast<uint64_t>(-1)) { | 
|  | 227 | return cur_load_bias; | 
|  | 228 | } | 
|  | 229 |  | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 230 | { | 
|  | 231 | // Make sure no other thread is trying to add the elf to this map. | 
|  | 232 | std::lock_guard<std::mutex> guard(mutex_); | 
|  | 233 | if (elf != nullptr) { | 
|  | 234 | if (elf->valid()) { | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 235 | cur_load_bias = elf->GetLoadBias(); | 
|  | 236 | load_bias = cur_load_bias; | 
|  | 237 | return cur_load_bias; | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 238 | } else { | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 239 | load_bias = 0; | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 240 | return 0; | 
|  | 241 | } | 
|  | 242 | } | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | // Call lightweight static function that will only read enough of the | 
|  | 246 | // elf data to get the load bias. | 
|  | 247 | std::unique_ptr<Memory> memory(CreateMemory(process_memory)); | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 248 | cur_load_bias = Elf::GetLoadBias(memory.get()); | 
|  | 249 | load_bias = cur_load_bias; | 
|  | 250 | return cur_load_bias; | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 253 | }  // namespace unwindstack |