blob: 9e6bcd4e3d7859f14100e50788f02b09a39fdda9 [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>
30#include <unwindstack/Memory.h>
31
32namespace unwindstack {
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -070033
Christopher Ferris01040b12018-12-10 11:13:23 -080034bool MapInfo::InitFileMemoryFromPreviousReadOnlyMap(MemoryFileAtOffset* memory) {
35 // One last attempt, see if the previous map is read-only with the
36 // same name and stretches across this map.
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080037 if (prev_map == nullptr || prev_map->flags != PROT_READ) {
38 return false;
Christopher Ferris01040b12018-12-10 11:13:23 -080039 }
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080040
41 uint64_t map_size = end - prev_map->end;
42 if (!memory->Init(name, prev_map->offset, map_size)) {
43 return false;
44 }
45
46 uint64_t max_size;
47 if (!Elf::GetInfo(memory, &max_size) || max_size < map_size) {
48 return false;
49 }
50
51 if (!memory->Init(name, prev_map->offset, max_size)) {
52 return false;
53 }
54
55 elf_offset = offset - prev_map->offset;
56 elf_start_offset = prev_map->offset;
57 return true;
Christopher Ferris01040b12018-12-10 11:13:23 -080058}
59
Christopher Ferris3f805ac2017-08-30 13:15:19 -070060Memory* MapInfo::GetFileMemory() {
61 std::unique_ptr<MemoryFileAtOffset> memory(new MemoryFileAtOffset);
62 if (offset == 0) {
63 if (memory->Init(name, 0)) {
64 return memory.release();
65 }
66 return nullptr;
67 }
68
Christopher Ferris01040b12018-12-10 11:13:23 -080069 // These are the possibilities when the offset is non-zero.
70 // - There is an elf file embedded in a file, and the offset is the
71 // the start of the elf in the file.
72 // - There is an elf file embedded in a file, and the offset is the
73 // the start of the executable part of the file. The actual start
74 // of the elf is in the read-only segment preceeding this map.
Christopher Ferris3f805ac2017-08-30 13:15:19 -070075 // - The whole file is an elf file, and the offset needs to be saved.
76 //
77 // Map in just the part of the file for the map. If this is not
78 // a valid elf, then reinit as if the whole file is an elf file.
79 // If the offset is a valid elf, then determine the size of the map
80 // and reinit to that size. This is needed because the dynamic linker
81 // only maps in a portion of the original elf, and never the symbol
82 // file data.
83 uint64_t map_size = end - start;
84 if (!memory->Init(name, offset, map_size)) {
85 return nullptr;
86 }
87
Christopher Ferris01040b12018-12-10 11:13:23 -080088 // Check if the start of this map is an embedded elf.
89 uint64_t max_size = 0;
Christopher Ferris01040b12018-12-10 11:13:23 -080090 if (Elf::GetInfo(memory.get(), &max_size)) {
91 if (max_size > map_size) {
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080092 if (memory->Init(name, offset, max_size)) {
Christopher Ferris01040b12018-12-10 11:13:23 -080093 return memory.release();
94 }
95 // Try to reinit using the default map_size.
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080096 if (memory->Init(name, offset, map_size)) {
Christopher Ferris01040b12018-12-10 11:13:23 -080097 return memory.release();
98 }
99 return nullptr;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700100 }
Christopher Ferris01040b12018-12-10 11:13:23 -0800101 return memory.release();
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700102 }
103
Christopher Ferris01040b12018-12-10 11:13:23 -0800104 // No elf at offset, try to init as if the whole file is an elf.
105 if (memory->Init(name, 0) && Elf::IsValidElf(memory.get())) {
106 elf_offset = offset;
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800107 // Need to check how to set the elf start offset. If this map is not
108 // the r-x map of a r-- map, then use the real offset value. Otherwise,
109 // use 0.
110 if (prev_map == nullptr || prev_map->offset != 0 || prev_map->flags != PROT_READ ||
111 prev_map->name != name) {
112 elf_start_offset = offset;
113 }
Christopher Ferris01040b12018-12-10 11:13:23 -0800114 return memory.release();
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700115 }
Christopher Ferris01040b12018-12-10 11:13:23 -0800116
117 // See if the map previous to this one contains a read-only map
118 // that represents the real start of the elf data.
119 if (InitFileMemoryFromPreviousReadOnlyMap(memory.get())) {
120 return memory.release();
121 }
122
123 // Failed to find elf at start of file or at read-only map, return
124 // file object from the current map.
125 if (memory->Init(name, offset, map_size)) {
126 return memory.release();
127 }
128 return nullptr;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700129}
130
Christopher Ferris5f118512017-09-01 11:17:16 -0700131Memory* MapInfo::CreateMemory(const std::shared_ptr<Memory>& process_memory) {
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700132 if (end <= start) {
133 return nullptr;
134 }
135
136 elf_offset = 0;
137
Christopher Ferris5f118512017-09-01 11:17:16 -0700138 // Fail on device maps.
139 if (flags & MAPS_FLAGS_DEVICE_MAP) {
140 return nullptr;
141 }
142
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700143 // First try and use the file associated with the info.
144 if (!name.empty()) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700145 Memory* memory = GetFileMemory();
146 if (memory != nullptr) {
147 return memory;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700148 }
149 }
150
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800151 if (process_memory == nullptr) {
152 return nullptr;
153 }
154
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700155 // Need to verify that this elf is valid. It's possible that
156 // only part of the elf file to be mapped into memory is in the executable
157 // map. In this case, there will be another read-only map that includes the
158 // first part of the elf file. This is done if the linker rosegment
159 // option is used.
160 std::unique_ptr<MemoryRange> memory(new MemoryRange(process_memory, start, end - start, 0));
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700161 if (Elf::IsValidElf(memory.get())) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700162 return memory.release();
163 }
164
Christopher Ferris01040b12018-12-10 11:13:23 -0800165 // Find the read-only map by looking at the previous map. The linker
166 // doesn't guarantee that this invariant will always be true. However,
167 // if that changes, there is likely something else that will change and
168 // break something.
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800169 if (offset == 0 || name.empty() || prev_map == nullptr || prev_map->name != name ||
170 prev_map->offset >= offset) {
Christopher Ferris55659062018-11-15 14:06:26 -0800171 return nullptr;
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700172 }
Christopher Ferris55659062018-11-15 14:06:26 -0800173
174 // Make sure that relative pc values are corrected properly.
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800175 elf_offset = offset - prev_map->offset;
176 // Use this as the elf start offset, otherwise, you always get offsets into
177 // the r-x section, which is not quite the right information.
178 elf_start_offset = prev_map->offset;
Christopher Ferris55659062018-11-15 14:06:26 -0800179
180 MemoryRanges* ranges = new MemoryRanges;
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800181 ranges->Insert(
182 new MemoryRange(process_memory, prev_map->start, prev_map->end - prev_map->start, 0));
Christopher Ferris55659062018-11-15 14:06:26 -0800183 ranges->Insert(new MemoryRange(process_memory, start, end - start, elf_offset));
184
185 return ranges;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700186}
187
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700188Elf* MapInfo::GetElf(const std::shared_ptr<Memory>& process_memory, ArchEnum expected_arch) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800189 // Make sure no other thread is trying to add the elf to this map.
190 std::lock_guard<std::mutex> guard(mutex_);
191
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800192 if (elf.get() != nullptr) {
193 return elf.get();
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700194 }
195
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800196 bool locked = false;
197 if (Elf::CachingEnabled() && !name.empty()) {
198 Elf::CacheLock();
199 locked = true;
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800200 if (Elf::CacheGet(this)) {
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800201 Elf::CacheUnlock();
202 return elf.get();
203 }
204 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700205
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800206 Memory* memory = CreateMemory(process_memory);
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800207 if (locked) {
208 if (Elf::CacheAfterCreateMemory(this)) {
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800209 delete memory;
210 Elf::CacheUnlock();
211 return elf.get();
212 }
213 }
214 elf.reset(new Elf(memory));
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700215 // If the init fails, keep the elf around as an invalid object so we
216 // don't try to reinit the object.
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700217 elf->Init();
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700218 if (elf->valid() && expected_arch != elf->arch()) {
219 // Make the elf invalid, mismatch between arch and expected arch.
220 elf->Invalidate();
221 }
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800222
223 if (locked) {
224 Elf::CacheAdd(this);
225 Elf::CacheUnlock();
226 }
227 return elf.get();
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700228}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700229
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800230bool MapInfo::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
231 {
232 // Make sure no other thread is trying to update this elf object.
233 std::lock_guard<std::mutex> guard(mutex_);
234 if (elf == nullptr) {
235 return false;
236 }
237 }
238 // No longer need the lock, once the elf object is created, it is not deleted
239 // until this object is deleted.
240 return elf->GetFunctionName(addr, name, func_offset);
241}
242
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800243uint64_t MapInfo::GetLoadBias(const std::shared_ptr<Memory>& process_memory) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800244 uint64_t cur_load_bias = load_bias.load();
245 if (cur_load_bias != static_cast<uint64_t>(-1)) {
246 return cur_load_bias;
247 }
248
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800249 {
250 // Make sure no other thread is trying to add the elf to this map.
251 std::lock_guard<std::mutex> guard(mutex_);
252 if (elf != nullptr) {
253 if (elf->valid()) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800254 cur_load_bias = elf->GetLoadBias();
255 load_bias = cur_load_bias;
256 return cur_load_bias;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800257 } else {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800258 load_bias = 0;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800259 return 0;
260 }
261 }
262 }
263
264 // Call lightweight static function that will only read enough of the
265 // elf data to get the load bias.
266 std::unique_ptr<Memory> memory(CreateMemory(process_memory));
Christopher Ferrise7b66242017-12-15 11:17:45 -0800267 cur_load_bias = Elf::GetLoadBias(memory.get());
268 load_bias = cur_load_bias;
269 return cur_load_bias;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800270}
271
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800272MapInfo::~MapInfo() {
273 uintptr_t id = build_id.load();
274 if (id != 0) {
275 delete reinterpret_cast<std::string*>(id);
276 }
277}
278
279std::string MapInfo::GetBuildID() {
280 uintptr_t id = build_id.load();
281 if (build_id != 0) {
282 return *reinterpret_cast<std::string*>(id);
283 }
284
285 // No need to lock, at worst if multiple threads do this at the same
286 // time it should be detected and only one thread should win and
287 // save the data.
288 std::unique_ptr<std::string> cur_build_id(new std::string);
289
290 // Now need to see if the elf object exists.
291 // Make sure no other thread is trying to add the elf to this map.
292 mutex_.lock();
293 Elf* elf_obj = elf.get();
294 mutex_.unlock();
295 if (elf_obj != nullptr) {
296 *cur_build_id = elf_obj->GetBuildID();
297 } else {
298 // This will only work if we can get the file associated with this memory.
299 // If this is only available in memory, then the section name information
300 // is not present and we will not be able to find the build id info.
301 std::unique_ptr<Memory> memory(GetFileMemory());
302 if (memory != nullptr) {
303 *cur_build_id = Elf::GetBuildID(memory.get());
304 }
305 }
306
307 id = reinterpret_cast<uintptr_t>(cur_build_id.get());
308 uintptr_t expected_id = 0;
309 if (build_id.compare_exchange_weak(expected_id, id)) {
310 // Value saved, so make sure the memory is not freed.
311 cur_build_id.release();
312 }
313 return *reinterpret_cast<std::string*>(id);
314}
315
Christopher Ferrisb1c9c202019-01-25 14:28:13 -0800316std::string MapInfo::GetPrintableBuildID() {
317 std::string raw_build_id = GetBuildID();
318 if (raw_build_id.empty()) {
319 return "";
320 }
321 std::string printable_build_id;
322 for (const char& c : raw_build_id) {
323 printable_build_id += android::base::StringPrintf("%02x", c);
324 }
325 return printable_build_id;
326}
327
Christopher Ferrisd226a512017-07-14 10:37:19 -0700328} // namespace unwindstack