blob: 64005ae12cdb22d03bd151c53ad43131abc8125f [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 Ferris3f805ac2017-08-30 13:15:19 -070032Memory* MapInfo::GetFileMemory() {
33 std::unique_ptr<MemoryFileAtOffset> memory(new MemoryFileAtOffset);
34 if (offset == 0) {
35 if (memory->Init(name, 0)) {
36 return memory.release();
37 }
38 return nullptr;
39 }
40
41 // There are two possibilities when the offset is non-zero.
42 // - There is an elf file embedded in a file.
43 // - The whole file is an elf file, and the offset needs to be saved.
44 //
45 // Map in just the part of the file for the map. If this is not
46 // a valid elf, then reinit as if the whole file is an elf file.
47 // If the offset is a valid elf, then determine the size of the map
48 // and reinit to that size. This is needed because the dynamic linker
49 // only maps in a portion of the original elf, and never the symbol
50 // file data.
51 uint64_t map_size = end - start;
52 if (!memory->Init(name, offset, map_size)) {
53 return nullptr;
54 }
55
56 bool valid;
57 uint64_t max_size;
58 Elf::GetInfo(memory.get(), &valid, &max_size);
59 if (!valid) {
60 // Init as if the whole file is an elf.
61 if (memory->Init(name, 0)) {
62 elf_offset = offset;
63 return memory.release();
64 }
65 return nullptr;
66 }
67
68 if (max_size > map_size) {
69 if (memory->Init(name, offset, max_size)) {
70 return memory.release();
71 }
72 // Try to reinit using the default map_size.
73 if (memory->Init(name, offset, map_size)) {
74 return memory.release();
75 }
76 return nullptr;
77 }
78 return memory.release();
79}
80
Christopher Ferris5f118512017-09-01 11:17:16 -070081Memory* MapInfo::CreateMemory(const std::shared_ptr<Memory>& process_memory) {
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -070082 if (end <= start) {
83 return nullptr;
84 }
85
86 elf_offset = 0;
87
Christopher Ferris5f118512017-09-01 11:17:16 -070088 // Fail on device maps.
89 if (flags & MAPS_FLAGS_DEVICE_MAP) {
90 return nullptr;
91 }
92
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -070093 // First try and use the file associated with the info.
94 if (!name.empty()) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -070095 Memory* memory = GetFileMemory();
96 if (memory != nullptr) {
97 return memory;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -070098 }
99 }
100
Christopher Ferris5f118512017-09-01 11:17:16 -0700101 // If the map isn't readable, don't bother trying to read from process memory.
102 if (!(flags & PROT_READ)) {
103 return nullptr;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700104 }
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700105
106 // Need to verify that this elf is valid. It's possible that
107 // only part of the elf file to be mapped into memory is in the executable
108 // map. In this case, there will be another read-only map that includes the
109 // first part of the elf file. This is done if the linker rosegment
110 // option is used.
111 std::unique_ptr<MemoryRange> memory(new MemoryRange(process_memory, start, end - start, 0));
112 bool valid;
113 uint64_t max_size;
114 Elf::GetInfo(memory.get(), &valid, &max_size);
115 if (valid) {
116 // Valid elf, we are done.
117 return memory.release();
118 }
119
120 if (name.empty() || maps_ == nullptr) {
121 return nullptr;
122 }
123
124 // Find the read-only map that has the same name and has an offset closest
125 // to the current offset but less than the offset of the current map.
126 // For shared libraries, there should be a r-x map that has a non-zero
127 // offset and then a r-- map that has a zero offset.
128 // For shared libraries loaded from an apk, there should be a r-x map that
129 // has a non-zero offset and then a r-- map that has a non-zero offset less
130 // than the offset from the r-x map.
131 uint64_t closest_offset = 0;
132 MapInfo* ro_map_info = nullptr;
133 for (auto map_info : *maps_) {
134 if (map_info->flags == PROT_READ && map_info->name == name && map_info->offset < offset &&
135 map_info->offset >= closest_offset) {
136 ro_map_info = map_info;
137 closest_offset = ro_map_info->offset;
138 }
139 }
140
141 if (ro_map_info != nullptr) {
142 // Make sure that relative pc values are corrected properly.
143 elf_offset = offset - closest_offset;
144
145 MemoryRanges* ranges = new MemoryRanges;
146 ranges->Insert(new MemoryRange(process_memory, ro_map_info->start,
147 ro_map_info->end - ro_map_info->start, 0));
148 ranges->Insert(new MemoryRange(process_memory, start, end - start, elf_offset));
149
150 return ranges;
151 }
152 return nullptr;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700153}
154
Christopher Ferris5f118512017-09-01 11:17:16 -0700155Elf* MapInfo::GetElf(const std::shared_ptr<Memory>& process_memory, bool init_gnu_debugdata) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800156 // Make sure no other thread is trying to add the elf to this map.
157 std::lock_guard<std::mutex> guard(mutex_);
158
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800159 if (elf.get() != nullptr) {
160 return elf.get();
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700161 }
162
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800163 bool locked = false;
164 if (Elf::CachingEnabled() && !name.empty()) {
165 Elf::CacheLock();
166 locked = true;
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800167 if (Elf::CacheGet(this)) {
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800168 Elf::CacheUnlock();
169 return elf.get();
170 }
171 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700172
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800173 Memory* memory = CreateMemory(process_memory);
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800174 if (locked) {
175 if (Elf::CacheAfterCreateMemory(this)) {
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800176 delete memory;
177 Elf::CacheUnlock();
178 return elf.get();
179 }
180 }
181 elf.reset(new Elf(memory));
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700182 // If the init fails, keep the elf around as an invalid object so we
183 // don't try to reinit the object.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800184 elf->Init(init_gnu_debugdata);
185
186 if (locked) {
187 Elf::CacheAdd(this);
188 Elf::CacheUnlock();
189 }
190 return elf.get();
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700191}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700192
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800193uint64_t MapInfo::GetLoadBias(const std::shared_ptr<Memory>& process_memory) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800194 uint64_t cur_load_bias = load_bias.load();
195 if (cur_load_bias != static_cast<uint64_t>(-1)) {
196 return cur_load_bias;
197 }
198
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800199 {
200 // Make sure no other thread is trying to add the elf to this map.
201 std::lock_guard<std::mutex> guard(mutex_);
202 if (elf != nullptr) {
203 if (elf->valid()) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800204 cur_load_bias = elf->GetLoadBias();
205 load_bias = cur_load_bias;
206 return cur_load_bias;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800207 } else {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800208 load_bias = 0;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800209 return 0;
210 }
211 }
212 }
213
214 // Call lightweight static function that will only read enough of the
215 // elf data to get the load bias.
216 std::unique_ptr<Memory> memory(CreateMemory(process_memory));
Christopher Ferrise7b66242017-12-15 11:17:45 -0800217 cur_load_bias = Elf::GetLoadBias(memory.get());
218 load_bias = cur_load_bias;
219 return cur_load_bias;
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800220}
221
Christopher Ferrisd226a512017-07-14 10:37:19 -0700222} // namespace unwindstack