blob: 44ec5c1d274abbad1d2f6a7434d07cae117b11d2 [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.
35 for (auto iter = maps_->begin(); iter != maps_->end(); ++iter) {
36 if (*iter == this) {
37 if (iter == maps_->begin()) {
38 return false;
39 }
40 --iter;
41 MapInfo* prev_map = *iter;
42 // Make sure this is a read-only map.
43 if (prev_map->flags != PROT_READ) {
44 return false;
45 }
46 uint64_t map_size = end - prev_map->end;
47 if (!memory->Init(name, prev_map->offset, map_size)) {
48 return false;
49 }
50 uint64_t max_size;
51 if (!Elf::GetInfo(memory, &max_size) || max_size < map_size) {
52 return false;
53 }
54 if (!memory->Init(name, prev_map->offset, max_size)) {
55 return false;
56 }
57 elf_offset = offset - prev_map->offset;
58 return true;
59 }
60 }
61 return false;
62}
63
Christopher Ferris3f805ac2017-08-30 13:15:19 -070064Memory* MapInfo::GetFileMemory() {
65 std::unique_ptr<MemoryFileAtOffset> memory(new MemoryFileAtOffset);
66 if (offset == 0) {
67 if (memory->Init(name, 0)) {
68 return memory.release();
69 }
70 return nullptr;
71 }
72
Christopher Ferris01040b12018-12-10 11:13:23 -080073 // These are the possibilities when the offset is non-zero.
74 // - There is an elf file embedded in a file, and the offset is the
75 // the start of the elf in the file.
76 // - There is an elf file embedded in a file, and the offset is the
77 // the start of the executable part of the file. The actual start
78 // of the elf is in the read-only segment preceeding this map.
Christopher Ferris3f805ac2017-08-30 13:15:19 -070079 // - The whole file is an elf file, and the offset needs to be saved.
80 //
81 // Map in just the part of the file for the map. If this is not
82 // a valid elf, then reinit as if the whole file is an elf file.
83 // If the offset is a valid elf, then determine the size of the map
84 // and reinit to that size. This is needed because the dynamic linker
85 // only maps in a portion of the original elf, and never the symbol
86 // file data.
87 uint64_t map_size = end - start;
88 if (!memory->Init(name, offset, map_size)) {
89 return nullptr;
90 }
91
Christopher Ferris01040b12018-12-10 11:13:23 -080092 // Check if the start of this map is an embedded elf.
93 uint64_t max_size = 0;
94 uint64_t file_offset = offset;
95 if (Elf::GetInfo(memory.get(), &max_size)) {
96 if (max_size > map_size) {
97 if (memory->Init(name, file_offset, max_size)) {
98 return memory.release();
99 }
100 // Try to reinit using the default map_size.
101 if (memory->Init(name, file_offset, map_size)) {
102 return memory.release();
103 }
104 return nullptr;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700105 }
Christopher Ferris01040b12018-12-10 11:13:23 -0800106 return memory.release();
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700107 }
108
Christopher Ferris01040b12018-12-10 11:13:23 -0800109 // No elf at offset, try to init as if the whole file is an elf.
110 if (memory->Init(name, 0) && Elf::IsValidElf(memory.get())) {
111 elf_offset = offset;
112 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
159 if (name.empty() || maps_ == nullptr) {
160 return nullptr;
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 Ferris9d5712c2018-10-01 21:01:09 -0700167 MapInfo* ro_map_info = nullptr;
Christopher Ferris01040b12018-12-10 11:13:23 -0800168 for (auto iter = maps_->begin(); iter != maps_->end(); ++iter) {
169 if (*iter == this) {
170 if (iter != maps_->begin()) {
171 --iter;
172 ro_map_info = *iter;
173 }
174 break;
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700175 }
176 }
177
Christopher Ferris01040b12018-12-10 11:13:23 -0800178 if (ro_map_info == nullptr || ro_map_info->name != name || ro_map_info->offset >= offset) {
Christopher Ferris55659062018-11-15 14:06:26 -0800179 return nullptr;
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700180 }
Christopher Ferris55659062018-11-15 14:06:26 -0800181
182 // Make sure that relative pc values are corrected properly.
Christopher Ferris01040b12018-12-10 11:13:23 -0800183 elf_offset = offset - ro_map_info->offset;
Christopher Ferris55659062018-11-15 14:06:26 -0800184
185 MemoryRanges* ranges = new MemoryRanges;
186 ranges->Insert(new MemoryRange(process_memory, ro_map_info->start,
187 ro_map_info->end - ro_map_info->start, 0));
188 ranges->Insert(new MemoryRange(process_memory, start, end - start, elf_offset));
189
190 return ranges;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700191}
192
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700193Elf* MapInfo::GetElf(const std::shared_ptr<Memory>& process_memory, ArchEnum expected_arch) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800194 // Make sure no other thread is trying to add the elf to this map.
195 std::lock_guard<std::mutex> guard(mutex_);
196
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800197 if (elf.get() != nullptr) {
198 return elf.get();
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700199 }
200
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800201 bool locked = false;
202 if (Elf::CachingEnabled() && !name.empty()) {
203 Elf::CacheLock();
204 locked = true;
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800205 if (Elf::CacheGet(this)) {
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800206 Elf::CacheUnlock();
207 return elf.get();
208 }
209 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700210
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800211 Memory* memory = CreateMemory(process_memory);
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800212 if (locked) {
213 if (Elf::CacheAfterCreateMemory(this)) {
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800214 delete memory;
215 Elf::CacheUnlock();
216 return elf.get();
217 }
218 }
219 elf.reset(new Elf(memory));
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700220 // If the init fails, keep the elf around as an invalid object so we
221 // don't try to reinit the object.
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700222 elf->Init();
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700223 if (elf->valid() && expected_arch != elf->arch()) {
224 // Make the elf invalid, mismatch between arch and expected arch.
225 elf->Invalidate();
226 }
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800227
228 if (locked) {
229 Elf::CacheAdd(this);
230 Elf::CacheUnlock();
231 }
232 return elf.get();
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700233}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700234
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800235uint64_t MapInfo::GetLoadBias(const std::shared_ptr<Memory>& process_memory) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800236 uint64_t cur_load_bias = load_bias.load();
237 if (cur_load_bias != static_cast<uint64_t>(-1)) {
238 return cur_load_bias;
239 }
240
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800241 {
242 // Make sure no other thread is trying to add the elf to this map.
243 std::lock_guard<std::mutex> guard(mutex_);
244 if (elf != nullptr) {
245 if (elf->valid()) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800246 cur_load_bias = elf->GetLoadBias();
247 load_bias = cur_load_bias;
248 return cur_load_bias;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800249 } else {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800250 load_bias = 0;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800251 return 0;
252 }
253 }
254 }
255
256 // Call lightweight static function that will only read enough of the
257 // elf data to get the load bias.
258 std::unique_ptr<Memory> memory(CreateMemory(process_memory));
Christopher Ferrise7b66242017-12-15 11:17:45 -0800259 cur_load_bias = Elf::GetLoadBias(memory.get());
260 load_bias = cur_load_bias;
261 return cur_load_bias;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800262}
263
Christopher Ferrisd226a512017-07-14 10:37:19 -0700264} // namespace unwindstack