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