1 /* 2 * Copyright (C) 2016 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 #ifndef _LIBUNWINDSTACK_MAP_INFO_H 18 #define _LIBUNWINDSTACK_MAP_INFO_H 19 20 #include <stdint.h> 21 22 #include <atomic> 23 #include <memory> 24 #include <mutex> 25 #include <string> 26 27 #include <unwindstack/Elf.h> 28 #include <unwindstack/Memory.h> 29 30 namespace unwindstack { 31 32 struct MapInfo { MapInfoMapInfo33 MapInfo(MapInfo* map_info, uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, 34 const char* name) 35 : start(start), 36 end(end), 37 offset(offset), 38 flags(flags), 39 name(name), 40 prev_map(map_info), 41 load_bias(static_cast<uint64_t>(-1)), 42 build_id(0) {} MapInfoMapInfo43 MapInfo(MapInfo* map_info, uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, 44 const std::string& name) 45 : start(start), 46 end(end), 47 offset(offset), 48 flags(flags), 49 name(name), 50 prev_map(map_info), 51 load_bias(static_cast<uint64_t>(-1)), 52 build_id(0) {} 53 ~MapInfo(); 54 55 uint64_t start = 0; 56 uint64_t end = 0; 57 uint64_t offset = 0; 58 uint16_t flags = 0; 59 std::string name; 60 std::shared_ptr<Elf> elf; 61 // This value is only non-zero if the offset is non-zero but there is 62 // no elf signature found at that offset. 63 uint64_t elf_offset = 0; 64 // This value is the offset from the map in memory that is the start 65 // of the elf. This is not equal to offset when the linker splits 66 // shared libraries into a read-only and read-execute map. 67 uint64_t elf_start_offset = 0; 68 69 MapInfo* prev_map = nullptr; 70 71 std::atomic_uint64_t load_bias; 72 73 // This is a pointer to a new'd std::string. 74 // Using an atomic value means that we don't need to lock and will 75 // make it easier to move to a fine grained lock in the future. 76 std::atomic_uintptr_t build_id; 77 78 // Set to true if the elf file data is coming from memory. 79 bool memory_backed_elf = false; 80 81 // This function guarantees it will never return nullptr. 82 Elf* GetElf(const std::shared_ptr<Memory>& process_memory, ArchEnum expected_arch); 83 84 uint64_t GetLoadBias(const std::shared_ptr<Memory>& process_memory); 85 86 Memory* CreateMemory(const std::shared_ptr<Memory>& process_memory); 87 88 bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset); 89 90 // Returns the raw build id read from the elf data. 91 std::string GetBuildID(); 92 93 // Returns the printable version of the build id (hex dump of raw data). 94 std::string GetPrintableBuildID(); 95 96 private: 97 MapInfo(const MapInfo&) = delete; 98 void operator=(const MapInfo&) = delete; 99 100 Memory* GetFileMemory(); 101 bool InitFileMemoryFromPreviousReadOnlyMap(MemoryFileAtOffset* memory); 102 103 // Protect the creation of the elf object. 104 std::mutex mutex_; 105 }; 106 107 } // namespace unwindstack 108 109 #endif // _LIBUNWINDSTACK_MAP_INFO_H 110