blob: 32722154a26c3bd34f48d7f234869d5e5825a2d0 [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
17#include <sys/types.h>
18#include <unistd.h>
19
20#include <memory>
21#include <string>
22
Christopher Ferrisd226a512017-07-14 10:37:19 -070023#include <unwindstack/Elf.h>
24#include <unwindstack/MapInfo.h>
25#include <unwindstack/Maps.h>
26#include <unwindstack/Memory.h>
27
28namespace unwindstack {
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -070029
Christopher Ferris3f805ac2017-08-30 13:15:19 -070030Memory* MapInfo::GetFileMemory() {
31 std::unique_ptr<MemoryFileAtOffset> memory(new MemoryFileAtOffset);
32 if (offset == 0) {
33 if (memory->Init(name, 0)) {
34 return memory.release();
35 }
36 return nullptr;
37 }
38
39 // There are two possibilities when the offset is non-zero.
40 // - There is an elf file embedded in a file.
41 // - The whole file is an elf file, and the offset needs to be saved.
42 //
43 // Map in just the part of the file for the map. If this is not
44 // a valid elf, then reinit as if the whole file is an elf file.
45 // If the offset is a valid elf, then determine the size of the map
46 // and reinit to that size. This is needed because the dynamic linker
47 // only maps in a portion of the original elf, and never the symbol
48 // file data.
49 uint64_t map_size = end - start;
50 if (!memory->Init(name, offset, map_size)) {
51 return nullptr;
52 }
53
54 bool valid;
55 uint64_t max_size;
56 Elf::GetInfo(memory.get(), &valid, &max_size);
57 if (!valid) {
58 // Init as if the whole file is an elf.
59 if (memory->Init(name, 0)) {
60 elf_offset = offset;
61 return memory.release();
62 }
63 return nullptr;
64 }
65
66 if (max_size > map_size) {
67 if (memory->Init(name, offset, max_size)) {
68 return memory.release();
69 }
70 // Try to reinit using the default map_size.
71 if (memory->Init(name, offset, map_size)) {
72 return memory.release();
73 }
74 return nullptr;
75 }
76 return memory.release();
77}
78
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -070079Memory* MapInfo::CreateMemory(pid_t pid) {
80 if (end <= start) {
81 return nullptr;
82 }
83
84 elf_offset = 0;
85
86 // First try and use the file associated with the info.
87 if (!name.empty()) {
88 // Fail on device maps.
89 if (flags & MAPS_FLAGS_DEVICE_MAP) {
90 return nullptr;
91 }
Christopher Ferris3f805ac2017-08-30 13:15:19 -070092 Memory* memory = GetFileMemory();
93 if (memory != nullptr) {
94 return memory;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -070095 }
96 }
97
Christopher Ferris3f805ac2017-08-30 13:15:19 -070098 Memory* memory;
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -070099 if (pid == getpid()) {
100 memory = new MemoryLocal();
101 } else {
102 memory = new MemoryRemote(pid);
103 }
104 return new MemoryRange(memory, start, end);
105}
106
Christopher Ferris570b76f2017-06-30 17:18:16 -0700107Elf* MapInfo::GetElf(pid_t pid, bool init_gnu_debugdata) {
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700108 if (elf) {
109 return elf;
110 }
111
112 elf = new Elf(CreateMemory(pid));
Christopher Ferris570b76f2017-06-30 17:18:16 -0700113 if (elf->Init() && init_gnu_debugdata) {
114 elf->InitGnuDebugdata();
115 }
Christopher Ferris0d7cf3e2017-04-19 15:42:19 -0700116 // If the init fails, keep the elf around as an invalid object so we
117 // don't try to reinit the object.
118 return elf;
119}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700120
121} // namespace unwindstack