| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 17 | #include <sys/mman.h> | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 18 | #include <sys/types.h> | 
|  | 19 | #include <unistd.h> | 
|  | 20 |  | 
|  | 21 | #include <memory> | 
|  | 22 | #include <string> | 
|  | 23 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 24 | #include <unwindstack/Elf.h> | 
|  | 25 | #include <unwindstack/MapInfo.h> | 
|  | 26 | #include <unwindstack/Maps.h> | 
|  | 27 | #include <unwindstack/Memory.h> | 
|  | 28 |  | 
|  | 29 | namespace unwindstack { | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 30 |  | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 31 | Memory* MapInfo::GetFileMemory() { | 
|  | 32 | std::unique_ptr<MemoryFileAtOffset> memory(new MemoryFileAtOffset); | 
|  | 33 | if (offset == 0) { | 
|  | 34 | if (memory->Init(name, 0)) { | 
|  | 35 | return memory.release(); | 
|  | 36 | } | 
|  | 37 | return nullptr; | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | // There are two possibilities when the offset is non-zero. | 
|  | 41 | // - There is an elf file embedded in a file. | 
|  | 42 | // - The whole file is an elf file, and the offset needs to be saved. | 
|  | 43 | // | 
|  | 44 | // Map in just the part of the file for the map. If this is not | 
|  | 45 | // a valid elf, then reinit as if the whole file is an elf file. | 
|  | 46 | // If the offset is a valid elf, then determine the size of the map | 
|  | 47 | // and reinit to that size. This is needed because the dynamic linker | 
|  | 48 | // only maps in a portion of the original elf, and never the symbol | 
|  | 49 | // file data. | 
|  | 50 | uint64_t map_size = end - start; | 
|  | 51 | if (!memory->Init(name, offset, map_size)) { | 
|  | 52 | return nullptr; | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | bool valid; | 
|  | 56 | uint64_t max_size; | 
|  | 57 | Elf::GetInfo(memory.get(), &valid, &max_size); | 
|  | 58 | if (!valid) { | 
|  | 59 | // Init as if the whole file is an elf. | 
|  | 60 | if (memory->Init(name, 0)) { | 
|  | 61 | elf_offset = offset; | 
|  | 62 | return memory.release(); | 
|  | 63 | } | 
|  | 64 | return nullptr; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | if (max_size > map_size) { | 
|  | 68 | if (memory->Init(name, offset, max_size)) { | 
|  | 69 | return memory.release(); | 
|  | 70 | } | 
|  | 71 | // Try to reinit using the default map_size. | 
|  | 72 | if (memory->Init(name, offset, map_size)) { | 
|  | 73 | return memory.release(); | 
|  | 74 | } | 
|  | 75 | return nullptr; | 
|  | 76 | } | 
|  | 77 | return memory.release(); | 
|  | 78 | } | 
|  | 79 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 80 | Memory* MapInfo::CreateMemory(const std::shared_ptr<Memory>& process_memory) { | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 81 | if (end <= start) { | 
|  | 82 | return nullptr; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | elf_offset = 0; | 
|  | 86 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 87 | // Fail on device maps. | 
|  | 88 | if (flags & MAPS_FLAGS_DEVICE_MAP) { | 
|  | 89 | return nullptr; | 
|  | 90 | } | 
|  | 91 |  | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 92 | // First try and use the file associated with the info. | 
|  | 93 | if (!name.empty()) { | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 94 | Memory* memory = GetFileMemory(); | 
|  | 95 | if (memory != nullptr) { | 
|  | 96 | return memory; | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 97 | } | 
|  | 98 | } | 
|  | 99 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 100 | // If the map isn't readable, don't bother trying to read from process memory. | 
|  | 101 | if (!(flags & PROT_READ)) { | 
|  | 102 | return nullptr; | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 103 | } | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 104 | return new MemoryRange(process_memory, start, end); | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 107 | Elf* MapInfo::GetElf(const std::shared_ptr<Memory>& process_memory, bool init_gnu_debugdata) { | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 108 | if (elf) { | 
|  | 109 | return elf; | 
|  | 110 | } | 
|  | 111 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 112 | elf = new Elf(CreateMemory(process_memory)); | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 113 | elf->Init(init_gnu_debugdata); | 
|  | 114 |  | 
| Christopher Ferris | 0d7cf3e | 2017-04-19 15:42:19 -0700 | [diff] [blame] | 115 | // If the init fails, keep the elf around as an invalid object so we | 
|  | 116 | // don't try to reinit the object. | 
|  | 117 | return elf; | 
|  | 118 | } | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 119 |  | 
|  | 120 | }  // namespace unwindstack |