blob: 5b30a4d7b791a9283e50b72b53e00c80a8d3e192 [file] [log] [blame]
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -07001/*
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 Ferris5f118512017-09-01 11:17:16 -070017#include <sys/mman.h>
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -070018#include <sys/types.h>
19#include <unistd.h>
20
21#include <memory>
Christopher Ferrisbe788d82017-11-27 14:50:38 -080022#include <mutex>
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -070023#include <string>
24
Christopher Ferrisb1c9c202019-01-25 14:28:13 -080025#include <android-base/stringprintf.h>
26
Christopher Ferrisd226a512017-07-14 10:37:19 -070027#include <unwindstack/Elf.h>
28#include <unwindstack/MapInfo.h>
29#include <unwindstack/Maps.h>
Casey Dahlin6b95a0e2019-03-12 17:50:52 -070030
31#include "MemoryFileAtOffset.h"
32#include "MemoryRange.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070033
34namespace unwindstack {
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -070035
Christopher Ferris01040b12018-12-10 11:13:23 -080036bool MapInfo::InitFileMemoryFromPreviousReadOnlyMap(MemoryFileAtOffset* memory) {
37 // One last attempt, see if the previous map is read-only with the
38 // same name and stretches across this map.
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080039 if (prev_map == nullptr || prev_map->flags != PROT_READ) {
40 return false;
Christopher Ferris01040b12018-12-10 11:13:23 -080041 }
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080042
43 uint64_t map_size = end - prev_map->end;
44 if (!memory->Init(name, prev_map->offset, map_size)) {
45 return false;
46 }
47
48 uint64_t max_size;
49 if (!Elf::GetInfo(memory, &max_size) || max_size < map_size) {
50 return false;
51 }
52
53 if (!memory->Init(name, prev_map->offset, max_size)) {
54 return false;
55 }
56
57 elf_offset = offset - prev_map->offset;
58 elf_start_offset = prev_map->offset;
59 return true;
Christopher Ferris01040b12018-12-10 11:13:23 -080060}
61
Christopher Ferris3f805ac2017-08-30 13:15:19 -070062Memory* MapInfo::GetFileMemory() {
63 std::unique_ptr<MemoryFileAtOffset> memory(new MemoryFileAtOffset);
64 if (offset == 0) {
65 if (memory->Init(name, 0)) {
66 return memory.release();
67 }
68 return nullptr;
69 }
70
Christopher Ferris01040b12018-12-10 11:13:23 -080071 // These are the possibilities when the offset is non-zero.
72 // - There is an elf file embedded in a file, and the offset is the
73 // the start of the elf in the file.
74 // - There is an elf file embedded in a file, and the offset is the
75 // the start of the executable part of the file. The actual start
76 // of the elf is in the read-only segment preceeding this map.
Christopher Ferris3f805ac2017-08-30 13:15:19 -070077 // - The whole file is an elf file, and the offset needs to be saved.
78 //
79 // Map in just the part of the file for the map. If this is not
80 // a valid elf, then reinit as if the whole file is an elf file.
81 // If the offset is a valid elf, then determine the size of the map
82 // and reinit to that size. This is needed because the dynamic linker
83 // only maps in a portion of the original elf, and never the symbol
84 // file data.
85 uint64_t map_size = end - start;
86 if (!memory->Init(name, offset, map_size)) {
87 return nullptr;
88 }
89
Christopher Ferris01040b12018-12-10 11:13:23 -080090 // Check if the start of this map is an embedded elf.
91 uint64_t max_size = 0;
Christopher Ferris01040b12018-12-10 11:13:23 -080092 if (Elf::GetInfo(memory.get(), &max_size)) {
Christopher Ferris86f2d9d2019-03-12 15:17:36 -070093 elf_start_offset = offset;
Christopher Ferris01040b12018-12-10 11:13:23 -080094 if (max_size > map_size) {
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080095 if (memory->Init(name, offset, max_size)) {
Christopher Ferris01040b12018-12-10 11:13:23 -080096 return memory.release();
97 }
98 // Try to reinit using the default map_size.
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080099 if (memory->Init(name, offset, map_size)) {
Christopher Ferris01040b12018-12-10 11:13:23 -0800100 return memory.release();
101 }
Christopher Ferris86f2d9d2019-03-12 15:17:36 -0700102 elf_start_offset = 0;
Christopher Ferris01040b12018-12-10 11:13:23 -0800103 return nullptr;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700104 }
Christopher Ferris01040b12018-12-10 11:13:23 -0800105 return memory.release();
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700106 }
107
Christopher Ferris01040b12018-12-10 11:13:23 -0800108 // No elf at offset, try to init as if the whole file is an elf.
109 if (memory->Init(name, 0) && Elf::IsValidElf(memory.get())) {
110 elf_offset = offset;
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800111 // Need to check how to set the elf start offset. If this map is not
112 // the r-x map of a r-- map, then use the real offset value. Otherwise,
113 // use 0.
114 if (prev_map == nullptr || prev_map->offset != 0 || prev_map->flags != PROT_READ ||
115 prev_map->name != name) {
116 elf_start_offset = offset;
117 }
Christopher Ferris01040b12018-12-10 11:13:23 -0800118 return memory.release();
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700119 }
Christopher Ferris01040b12018-12-10 11:13:23 -0800120
121 // See if the map previous to this one contains a read-only map
122 // that represents the real start of the elf data.
123 if (InitFileMemoryFromPreviousReadOnlyMap(memory.get())) {
124 return memory.release();
125 }
126
127 // Failed to find elf at start of file or at read-only map, return
128 // file object from the current map.
129 if (memory->Init(name, offset, map_size)) {
130 return memory.release();
131 }
132 return nullptr;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700133}
134
Christopher Ferris5f118512017-09-01 11:17:16 -0700135Memory* MapInfo::CreateMemory(const std::shared_ptr<Memory>& process_memory) {
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700136 if (end <= start) {
137 return nullptr;
138 }
139
140 elf_offset = 0;
141
Christopher Ferris5f118512017-09-01 11:17:16 -0700142 // Fail on device maps.
143 if (flags & MAPS_FLAGS_DEVICE_MAP) {
144 return nullptr;
145 }
146
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700147 // First try and use the file associated with the info.
148 if (!name.empty()) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700149 Memory* memory = GetFileMemory();
150 if (memory != nullptr) {
151 return memory;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700152 }
153 }
154
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800155 if (process_memory == nullptr) {
156 return nullptr;
157 }
158
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700159 // Need to verify that this elf is valid. It's possible that
160 // only part of the elf file to be mapped into memory is in the executable
161 // map. In this case, there will be another read-only map that includes the
162 // first part of the elf file. This is done if the linker rosegment
163 // option is used.
164 std::unique_ptr<MemoryRange> memory(new MemoryRange(process_memory, start, end - start, 0));
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700165 if (Elf::IsValidElf(memory.get())) {
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700166 memory_backed_elf = true;
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700167 return memory.release();
168 }
169
Christopher Ferris01040b12018-12-10 11:13:23 -0800170 // Find the read-only map by looking at the previous map. The linker
171 // doesn't guarantee that this invariant will always be true. However,
172 // if that changes, there is likely something else that will change and
173 // break something.
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800174 if (offset == 0 || name.empty() || prev_map == nullptr || prev_map->name != name ||
175 prev_map->offset >= offset) {
Christopher Ferris55659062018-11-15 14:06:26 -0800176 return nullptr;
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700177 }
Christopher Ferris55659062018-11-15 14:06:26 -0800178
179 // Make sure that relative pc values are corrected properly.
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800180 elf_offset = offset - prev_map->offset;
181 // Use this as the elf start offset, otherwise, you always get offsets into
182 // the r-x section, which is not quite the right information.
183 elf_start_offset = prev_map->offset;
Christopher Ferris55659062018-11-15 14:06:26 -0800184
185 MemoryRanges* ranges = new MemoryRanges;
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800186 ranges->Insert(
187 new MemoryRange(process_memory, prev_map->start, prev_map->end - prev_map->start, 0));
Christopher Ferris55659062018-11-15 14:06:26 -0800188 ranges->Insert(new MemoryRange(process_memory, start, end - start, elf_offset));
189
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700190 memory_backed_elf = true;
Christopher Ferris55659062018-11-15 14:06:26 -0800191 return ranges;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700192}
193
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700194Elf* MapInfo::GetElf(const std::shared_ptr<Memory>& process_memory, ArchEnum expected_arch) {
Christopher Ferris02a6c442019-03-11 14:43:33 -0700195 {
196 // Make sure no other thread is trying to add the elf to this map.
197 std::lock_guard<std::mutex> guard(mutex_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800198
Christopher Ferris02a6c442019-03-11 14:43:33 -0700199 if (elf.get() != nullptr) {
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800200 return elf.get();
201 }
Christopher Ferris02a6c442019-03-11 14:43:33 -0700202
203 bool locked = false;
204 if (Elf::CachingEnabled() && !name.empty()) {
205 Elf::CacheLock();
206 locked = true;
207 if (Elf::CacheGet(this)) {
208 Elf::CacheUnlock();
209 return elf.get();
210 }
211 }
212
213 Memory* memory = CreateMemory(process_memory);
214 if (locked) {
215 if (Elf::CacheAfterCreateMemory(this)) {
216 delete memory;
217 Elf::CacheUnlock();
218 return elf.get();
219 }
220 }
221 elf.reset(new Elf(memory));
222 // If the init fails, keep the elf around as an invalid object so we
223 // don't try to reinit the object.
224 elf->Init();
225 if (elf->valid() && expected_arch != elf->arch()) {
226 // Make the elf invalid, mismatch between arch and expected arch.
227 elf->Invalidate();
228 }
229
230 if (locked) {
231 Elf::CacheAdd(this);
232 Elf::CacheUnlock();
233 }
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800234 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700235
Christopher Ferrisd49499d2019-06-10 18:37:32 -0700236 if (!elf->valid()) {
237 elf_start_offset = offset;
238 } else if (prev_map != nullptr && elf_start_offset != offset &&
239 prev_map->offset == elf_start_offset && prev_map->name == name) {
240 // If there is a read-only map then a read-execute map that represents the
241 // same elf object, make sure the previous map is using the same elf
242 // object if it hasn't already been set.
Christopher Ferris02a6c442019-03-11 14:43:33 -0700243 std::lock_guard<std::mutex> guard(prev_map->mutex_);
244 if (prev_map->elf.get() == nullptr) {
245 prev_map->elf = elf;
Christopher Ferris4ae266c2019-04-03 09:27:12 -0700246 prev_map->memory_backed_elf = memory_backed_elf;
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800247 }
248 }
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800249 return elf.get();
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700250}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700251
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800252bool MapInfo::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
253 {
254 // Make sure no other thread is trying to update this elf object.
255 std::lock_guard<std::mutex> guard(mutex_);
256 if (elf == nullptr) {
257 return false;
258 }
259 }
260 // No longer need the lock, once the elf object is created, it is not deleted
261 // until this object is deleted.
262 return elf->GetFunctionName(addr, name, func_offset);
263}
264
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800265uint64_t MapInfo::GetLoadBias(const std::shared_ptr<Memory>& process_memory) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800266 uint64_t cur_load_bias = load_bias.load();
267 if (cur_load_bias != static_cast<uint64_t>(-1)) {
268 return cur_load_bias;
269 }
270
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800271 {
272 // Make sure no other thread is trying to add the elf to this map.
273 std::lock_guard<std::mutex> guard(mutex_);
274 if (elf != nullptr) {
275 if (elf->valid()) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800276 cur_load_bias = elf->GetLoadBias();
277 load_bias = cur_load_bias;
278 return cur_load_bias;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800279 } else {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800280 load_bias = 0;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800281 return 0;
282 }
283 }
284 }
285
286 // Call lightweight static function that will only read enough of the
287 // elf data to get the load bias.
288 std::unique_ptr<Memory> memory(CreateMemory(process_memory));
Christopher Ferrise7b66242017-12-15 11:17:45 -0800289 cur_load_bias = Elf::GetLoadBias(memory.get());
290 load_bias = cur_load_bias;
291 return cur_load_bias;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800292}
293
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800294MapInfo::~MapInfo() {
295 uintptr_t id = build_id.load();
296 if (id != 0) {
297 delete reinterpret_cast<std::string*>(id);
298 }
299}
300
301std::string MapInfo::GetBuildID() {
302 uintptr_t id = build_id.load();
Christopher Ferrisd1d973b2019-06-19 18:41:37 -0700303 if (id != 0) {
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800304 return *reinterpret_cast<std::string*>(id);
305 }
306
307 // No need to lock, at worst if multiple threads do this at the same
308 // time it should be detected and only one thread should win and
309 // save the data.
310 std::unique_ptr<std::string> cur_build_id(new std::string);
311
312 // Now need to see if the elf object exists.
313 // Make sure no other thread is trying to add the elf to this map.
314 mutex_.lock();
315 Elf* elf_obj = elf.get();
316 mutex_.unlock();
317 if (elf_obj != nullptr) {
318 *cur_build_id = elf_obj->GetBuildID();
319 } else {
320 // This will only work if we can get the file associated with this memory.
321 // If this is only available in memory, then the section name information
322 // is not present and we will not be able to find the build id info.
323 std::unique_ptr<Memory> memory(GetFileMemory());
324 if (memory != nullptr) {
325 *cur_build_id = Elf::GetBuildID(memory.get());
326 }
327 }
328
329 id = reinterpret_cast<uintptr_t>(cur_build_id.get());
330 uintptr_t expected_id = 0;
331 if (build_id.compare_exchange_weak(expected_id, id)) {
332 // Value saved, so make sure the memory is not freed.
333 cur_build_id.release();
334 }
335 return *reinterpret_cast<std::string*>(id);
336}
337
Christopher Ferrisb1c9c202019-01-25 14:28:13 -0800338std::string MapInfo::GetPrintableBuildID() {
339 std::string raw_build_id = GetBuildID();
340 if (raw_build_id.empty()) {
341 return "";
342 }
343 std::string printable_build_id;
344 for (const char& c : raw_build_id) {
Christopher Ferrisce34d622019-01-30 10:55:27 -0800345 // Use %hhx to avoid sign extension on abis that have signed chars.
346 printable_build_id += android::base::StringPrintf("%02hhx", c);
Christopher Ferrisb1c9c202019-01-25 14:28:13 -0800347 }
348 return printable_build_id;
349}
350
Christopher Ferrisd226a512017-07-14 10:37:19 -0700351} // namespace unwindstack