blob: b9d8b3db7e7dc3bfb8117a573e4d13cffcb908e6 [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 Ferris9d5712c2018-10-01 21:01:09 -0700149 // 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 Ferrisa2f38f12018-10-09 18:49:11 -0700155 if (Elf::IsValidElf(memory.get())) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700156 return memory.release();
157 }
158
Christopher Ferris01040b12018-12-10 11:13:23 -0800159 // 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 Ferrisa09c4a62018-12-13 16:08:50 -0800163 if (offset == 0 || name.empty() || prev_map == nullptr || prev_map->name != name ||
164 prev_map->offset >= offset) {
Christopher Ferris55659062018-11-15 14:06:26 -0800165 return nullptr;
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700166 }
Christopher Ferris55659062018-11-15 14:06:26 -0800167
168 // Make sure that relative pc values are corrected properly.
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800169 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 Ferris55659062018-11-15 14:06:26 -0800173
174 MemoryRanges* ranges = new MemoryRanges;
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800175 ranges->Insert(
176 new MemoryRange(process_memory, prev_map->start, prev_map->end - prev_map->start, 0));
Christopher Ferris55659062018-11-15 14:06:26 -0800177 ranges->Insert(new MemoryRange(process_memory, start, end - start, elf_offset));
178
179 return ranges;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700180}
181
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700182Elf* MapInfo::GetElf(const std::shared_ptr<Memory>& process_memory, ArchEnum expected_arch) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800183 // Make sure no other thread is trying to add the elf to this map.
184 std::lock_guard<std::mutex> guard(mutex_);
185
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800186 if (elf.get() != nullptr) {
187 return elf.get();
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700188 }
189
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800190 bool locked = false;
191 if (Elf::CachingEnabled() && !name.empty()) {
192 Elf::CacheLock();
193 locked = true;
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800194 if (Elf::CacheGet(this)) {
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800195 Elf::CacheUnlock();
196 return elf.get();
197 }
198 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700199
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800200 Memory* memory = CreateMemory(process_memory);
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800201 if (locked) {
202 if (Elf::CacheAfterCreateMemory(this)) {
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800203 delete memory;
204 Elf::CacheUnlock();
205 return elf.get();
206 }
207 }
208 elf.reset(new Elf(memory));
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700209 // If the init fails, keep the elf around as an invalid object so we
210 // don't try to reinit the object.
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700211 elf->Init();
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700212 if (elf->valid() && expected_arch != elf->arch()) {
213 // Make the elf invalid, mismatch between arch and expected arch.
214 elf->Invalidate();
215 }
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800216
217 if (locked) {
218 Elf::CacheAdd(this);
219 Elf::CacheUnlock();
220 }
221 return elf.get();
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700222}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700223
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800224bool MapInfo::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
225 {
226 // Make sure no other thread is trying to update this elf object.
227 std::lock_guard<std::mutex> guard(mutex_);
228 if (elf == nullptr) {
229 return false;
230 }
231 }
232 // No longer need the lock, once the elf object is created, it is not deleted
233 // until this object is deleted.
234 return elf->GetFunctionName(addr, name, func_offset);
235}
236
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800237uint64_t MapInfo::GetLoadBias(const std::shared_ptr<Memory>& process_memory) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800238 uint64_t cur_load_bias = load_bias.load();
239 if (cur_load_bias != static_cast<uint64_t>(-1)) {
240 return cur_load_bias;
241 }
242
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800243 {
244 // Make sure no other thread is trying to add the elf to this map.
245 std::lock_guard<std::mutex> guard(mutex_);
246 if (elf != nullptr) {
247 if (elf->valid()) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800248 cur_load_bias = elf->GetLoadBias();
249 load_bias = cur_load_bias;
250 return cur_load_bias;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800251 } else {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800252 load_bias = 0;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800253 return 0;
254 }
255 }
256 }
257
258 // Call lightweight static function that will only read enough of the
259 // elf data to get the load bias.
260 std::unique_ptr<Memory> memory(CreateMemory(process_memory));
Christopher Ferrise7b66242017-12-15 11:17:45 -0800261 cur_load_bias = Elf::GetLoadBias(memory.get());
262 load_bias = cur_load_bias;
263 return cur_load_bias;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800264}
265
Christopher Ferrisd226a512017-07-14 10:37:19 -0700266} // namespace unwindstack