blob: f3199711b338c83ce5af1149d7cd0a636e24ca6a [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 Ferrisd226a512017-07-14 10:37:19 -070025#include <unwindstack/Elf.h>
26#include <unwindstack/MapInfo.h>
27#include <unwindstack/Maps.h>
28#include <unwindstack/Memory.h>
29
30namespace unwindstack {
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -070031
Christopher Ferris01040b12018-12-10 11:13:23 -080032bool 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 Ferrisa09c4a62018-12-13 16:08:50 -080035 if (prev_map == nullptr || prev_map->flags != PROT_READ) {
36 return false;
Christopher Ferris01040b12018-12-10 11:13:23 -080037 }
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080038
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 Ferris01040b12018-12-10 11:13:23 -080056}
57
Christopher Ferris3f805ac2017-08-30 13:15:19 -070058Memory* 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 Ferris01040b12018-12-10 11:13:23 -080067 // 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 Ferris3f805ac2017-08-30 13:15:19 -070073 // - 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 Ferris01040b12018-12-10 11:13:23 -080086 // Check if the start of this map is an embedded elf.
87 uint64_t max_size = 0;
Christopher Ferris01040b12018-12-10 11:13:23 -080088 if (Elf::GetInfo(memory.get(), &max_size)) {
89 if (max_size > map_size) {
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080090 if (memory->Init(name, offset, max_size)) {
Christopher Ferris01040b12018-12-10 11:13:23 -080091 return memory.release();
92 }
93 // Try to reinit using the default map_size.
Christopher Ferrisa09c4a62018-12-13 16:08:50 -080094 if (memory->Init(name, offset, map_size)) {
Christopher Ferris01040b12018-12-10 11:13:23 -080095 return memory.release();
96 }
97 return nullptr;
Christopher Ferris3f805ac2017-08-30 13:15:19 -070098 }
Christopher Ferris01040b12018-12-10 11:13:23 -080099 return memory.release();
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700100 }
101
Christopher Ferris01040b12018-12-10 11:13:23 -0800102 // 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 Ferrisa09c4a62018-12-13 16:08:50 -0800105 // 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 Ferris01040b12018-12-10 11:13:23 -0800112 return memory.release();
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700113 }
Christopher Ferris01040b12018-12-10 11:13:23 -0800114
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 Ferris3f805ac2017-08-30 13:15:19 -0700127}
128
Christopher Ferris5f118512017-09-01 11:17:16 -0700129Memory* MapInfo::CreateMemory(const std::shared_ptr<Memory>& process_memory) {
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700130 if (end <= start) {
131 return nullptr;
132 }
133
134 elf_offset = 0;
135
Christopher Ferris5f118512017-09-01 11:17:16 -0700136 // Fail on device maps.
137 if (flags & MAPS_FLAGS_DEVICE_MAP) {
138 return nullptr;
139 }
140
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700141 // First try and use the file associated with the info.
142 if (!name.empty()) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700143 Memory* memory = GetFileMemory();
144 if (memory != nullptr) {
145 return memory;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700146 }
147 }
148
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800149 if (process_memory == nullptr) {
150 return nullptr;
151 }
152
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700153 // Need to verify that this elf is valid. It's possible that
154 // only part of the elf file to be mapped into memory is in the executable
155 // map. In this case, there will be another read-only map that includes the
156 // first part of the elf file. This is done if the linker rosegment
157 // option is used.
158 std::unique_ptr<MemoryRange> memory(new MemoryRange(process_memory, start, end - start, 0));
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700159 if (Elf::IsValidElf(memory.get())) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700160 return memory.release();
161 }
162
Christopher Ferris01040b12018-12-10 11:13:23 -0800163 // 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 Ferrisa09c4a62018-12-13 16:08:50 -0800167 if (offset == 0 || name.empty() || prev_map == nullptr || prev_map->name != name ||
168 prev_map->offset >= offset) {
Christopher Ferris55659062018-11-15 14:06:26 -0800169 return nullptr;
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700170 }
Christopher Ferris55659062018-11-15 14:06:26 -0800171
172 // Make sure that relative pc values are corrected properly.
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800173 elf_offset = offset - prev_map->offset;
174 // Use this as the elf start offset, otherwise, you always get offsets into
175 // the r-x section, which is not quite the right information.
176 elf_start_offset = prev_map->offset;
Christopher Ferris55659062018-11-15 14:06:26 -0800177
178 MemoryRanges* ranges = new MemoryRanges;
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800179 ranges->Insert(
180 new MemoryRange(process_memory, prev_map->start, prev_map->end - prev_map->start, 0));
Christopher Ferris55659062018-11-15 14:06:26 -0800181 ranges->Insert(new MemoryRange(process_memory, start, end - start, elf_offset));
182
183 return ranges;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700184}
185
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700186Elf* MapInfo::GetElf(const std::shared_ptr<Memory>& process_memory, ArchEnum expected_arch) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800187 // Make sure no other thread is trying to add the elf to this map.
188 std::lock_guard<std::mutex> guard(mutex_);
189
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800190 if (elf.get() != nullptr) {
191 return elf.get();
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700192 }
193
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800194 bool locked = false;
195 if (Elf::CachingEnabled() && !name.empty()) {
196 Elf::CacheLock();
197 locked = true;
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800198 if (Elf::CacheGet(this)) {
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800199 Elf::CacheUnlock();
200 return elf.get();
201 }
202 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700203
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800204 Memory* memory = CreateMemory(process_memory);
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800205 if (locked) {
206 if (Elf::CacheAfterCreateMemory(this)) {
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800207 delete memory;
208 Elf::CacheUnlock();
209 return elf.get();
210 }
211 }
212 elf.reset(new Elf(memory));
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700213 // If the init fails, keep the elf around as an invalid object so we
214 // don't try to reinit the object.
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700215 elf->Init();
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700216 if (elf->valid() && expected_arch != elf->arch()) {
217 // Make the elf invalid, mismatch between arch and expected arch.
218 elf->Invalidate();
219 }
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800220
221 if (locked) {
222 Elf::CacheAdd(this);
223 Elf::CacheUnlock();
224 }
225 return elf.get();
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700226}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700227
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800228bool MapInfo::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
229 {
230 // Make sure no other thread is trying to update this elf object.
231 std::lock_guard<std::mutex> guard(mutex_);
232 if (elf == nullptr) {
233 return false;
234 }
235 }
236 // No longer need the lock, once the elf object is created, it is not deleted
237 // until this object is deleted.
238 return elf->GetFunctionName(addr, name, func_offset);
239}
240
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800241uint64_t MapInfo::GetLoadBias(const std::shared_ptr<Memory>& process_memory) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800242 uint64_t cur_load_bias = load_bias.load();
243 if (cur_load_bias != static_cast<uint64_t>(-1)) {
244 return cur_load_bias;
245 }
246
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800247 {
248 // Make sure no other thread is trying to add the elf to this map.
249 std::lock_guard<std::mutex> guard(mutex_);
250 if (elf != nullptr) {
251 if (elf->valid()) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800252 cur_load_bias = elf->GetLoadBias();
253 load_bias = cur_load_bias;
254 return cur_load_bias;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800255 } else {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800256 load_bias = 0;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800257 return 0;
258 }
259 }
260 }
261
262 // Call lightweight static function that will only read enough of the
263 // elf data to get the load bias.
264 std::unique_ptr<Memory> memory(CreateMemory(process_memory));
Christopher Ferrise7b66242017-12-15 11:17:45 -0800265 cur_load_bias = Elf::GetLoadBias(memory.get());
266 load_bias = cur_load_bias;
267 return cur_load_bias;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800268}
269
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800270MapInfo::~MapInfo() {
271 uintptr_t id = build_id.load();
272 if (id != 0) {
273 delete reinterpret_cast<std::string*>(id);
274 }
275}
276
277std::string MapInfo::GetBuildID() {
278 uintptr_t id = build_id.load();
279 if (build_id != 0) {
280 return *reinterpret_cast<std::string*>(id);
281 }
282
283 // No need to lock, at worst if multiple threads do this at the same
284 // time it should be detected and only one thread should win and
285 // save the data.
286 std::unique_ptr<std::string> cur_build_id(new std::string);
287
288 // Now need to see if the elf object exists.
289 // Make sure no other thread is trying to add the elf to this map.
290 mutex_.lock();
291 Elf* elf_obj = elf.get();
292 mutex_.unlock();
293 if (elf_obj != nullptr) {
294 *cur_build_id = elf_obj->GetBuildID();
295 } else {
296 // This will only work if we can get the file associated with this memory.
297 // If this is only available in memory, then the section name information
298 // is not present and we will not be able to find the build id info.
299 std::unique_ptr<Memory> memory(GetFileMemory());
300 if (memory != nullptr) {
301 *cur_build_id = Elf::GetBuildID(memory.get());
302 }
303 }
304
305 id = reinterpret_cast<uintptr_t>(cur_build_id.get());
306 uintptr_t expected_id = 0;
307 if (build_id.compare_exchange_weak(expected_id, id)) {
308 // Value saved, so make sure the memory is not freed.
309 cur_build_id.release();
310 }
311 return *reinterpret_cast<std::string*>(id);
312}
313
Christopher Ferrisd226a512017-07-14 10:37:19 -0700314} // namespace unwindstack