1 //===-- NativeProcessELF.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "NativeProcessELF.h"
10
11 #include "lldb/Utility/DataExtractor.h"
12
13 namespace lldb_private {
14
15 llvm::Optional<uint64_t>
GetAuxValue(enum AuxVector::EntryType type)16 NativeProcessELF::GetAuxValue(enum AuxVector::EntryType type) {
17 if (m_aux_vector == nullptr) {
18 auto buffer_or_error = GetAuxvData();
19 if (!buffer_or_error)
20 return llvm::None;
21 DataExtractor auxv_data(buffer_or_error.get()->getBufferStart(),
22 buffer_or_error.get()->getBufferSize(),
23 GetByteOrder(), GetAddressByteSize());
24 m_aux_vector = std::make_unique<AuxVector>(auxv_data);
25 }
26
27 return m_aux_vector->GetAuxValue(type);
28 }
29
GetSharedLibraryInfoAddress()30 lldb::addr_t NativeProcessELF::GetSharedLibraryInfoAddress() {
31 if (!m_shared_library_info_addr.hasValue()) {
32 if (GetAddressByteSize() == 8)
33 m_shared_library_info_addr =
34 GetELFImageInfoAddress<llvm::ELF::Elf64_Ehdr, llvm::ELF::Elf64_Phdr,
35 llvm::ELF::Elf64_Dyn>();
36 else
37 m_shared_library_info_addr =
38 GetELFImageInfoAddress<llvm::ELF::Elf32_Ehdr, llvm::ELF::Elf32_Phdr,
39 llvm::ELF::Elf32_Dyn>();
40 }
41
42 return m_shared_library_info_addr.getValue();
43 }
44
45 template <typename ELF_EHDR, typename ELF_PHDR, typename ELF_DYN>
GetELFImageInfoAddress()46 lldb::addr_t NativeProcessELF::GetELFImageInfoAddress() {
47 llvm::Optional<uint64_t> maybe_phdr_addr =
48 GetAuxValue(AuxVector::AUXV_AT_PHDR);
49 llvm::Optional<uint64_t> maybe_phdr_entry_size =
50 GetAuxValue(AuxVector::AUXV_AT_PHENT);
51 llvm::Optional<uint64_t> maybe_phdr_num_entries =
52 GetAuxValue(AuxVector::AUXV_AT_PHNUM);
53 if (!maybe_phdr_addr || !maybe_phdr_entry_size || !maybe_phdr_num_entries)
54 return LLDB_INVALID_ADDRESS;
55 lldb::addr_t phdr_addr = *maybe_phdr_addr;
56 size_t phdr_entry_size = *maybe_phdr_entry_size;
57 size_t phdr_num_entries = *maybe_phdr_num_entries;
58
59 // Find the PT_DYNAMIC segment (.dynamic section) in the program header and
60 // what the load bias by calculating the difference of the program header
61 // load address and its virtual address.
62 lldb::offset_t load_bias;
63 bool found_load_bias = false;
64 lldb::addr_t dynamic_section_addr = 0;
65 uint64_t dynamic_section_size = 0;
66 bool found_dynamic_section = false;
67 ELF_PHDR phdr_entry;
68 for (size_t i = 0; i < phdr_num_entries; i++) {
69 size_t bytes_read;
70 auto error = ReadMemory(phdr_addr + i * phdr_entry_size, &phdr_entry,
71 sizeof(phdr_entry), bytes_read);
72 if (!error.Success())
73 return LLDB_INVALID_ADDRESS;
74 if (phdr_entry.p_type == llvm::ELF::PT_PHDR) {
75 load_bias = phdr_addr - phdr_entry.p_vaddr;
76 found_load_bias = true;
77 }
78
79 if (phdr_entry.p_type == llvm::ELF::PT_DYNAMIC) {
80 dynamic_section_addr = phdr_entry.p_vaddr;
81 dynamic_section_size = phdr_entry.p_memsz;
82 found_dynamic_section = true;
83 }
84 }
85
86 if (!found_load_bias || !found_dynamic_section)
87 return LLDB_INVALID_ADDRESS;
88
89 // Find the DT_DEBUG entry in the .dynamic section
90 dynamic_section_addr += load_bias;
91 ELF_DYN dynamic_entry;
92 size_t dynamic_num_entries = dynamic_section_size / sizeof(dynamic_entry);
93 for (size_t i = 0; i < dynamic_num_entries; i++) {
94 size_t bytes_read;
95 auto error = ReadMemory(dynamic_section_addr + i * sizeof(dynamic_entry),
96 &dynamic_entry, sizeof(dynamic_entry), bytes_read);
97 if (!error.Success())
98 return LLDB_INVALID_ADDRESS;
99 // Return the &DT_DEBUG->d_ptr which points to r_debug which contains the
100 // link_map.
101 if (dynamic_entry.d_tag == llvm::ELF::DT_DEBUG) {
102 return dynamic_section_addr + i * sizeof(dynamic_entry) +
103 sizeof(dynamic_entry.d_tag);
104 }
105 }
106
107 return LLDB_INVALID_ADDRESS;
108 }
109
110 template lldb::addr_t NativeProcessELF::GetELFImageInfoAddress<
111 llvm::ELF::Elf32_Ehdr, llvm::ELF::Elf32_Phdr, llvm::ELF::Elf32_Dyn>();
112 template lldb::addr_t NativeProcessELF::GetELFImageInfoAddress<
113 llvm::ELF::Elf64_Ehdr, llvm::ELF::Elf64_Phdr, llvm::ELF::Elf64_Dyn>();
114
115 template <typename T>
116 llvm::Expected<SVR4LibraryInfo>
ReadSVR4LibraryInfo(lldb::addr_t link_map_addr)117 NativeProcessELF::ReadSVR4LibraryInfo(lldb::addr_t link_map_addr) {
118 ELFLinkMap<T> link_map;
119 size_t bytes_read;
120 auto error =
121 ReadMemory(link_map_addr, &link_map, sizeof(link_map), bytes_read);
122 if (!error.Success())
123 return error.ToError();
124
125 char name_buffer[PATH_MAX];
126 llvm::Expected<llvm::StringRef> string_or_error = ReadCStringFromMemory(
127 link_map.l_name, &name_buffer[0], sizeof(name_buffer), bytes_read);
128 if (!string_or_error)
129 return string_or_error.takeError();
130
131 SVR4LibraryInfo info;
132 info.name = string_or_error->str();
133 info.link_map = link_map_addr;
134 info.base_addr = link_map.l_addr;
135 info.ld_addr = link_map.l_ld;
136 info.next = link_map.l_next;
137
138 return info;
139 }
140
141 llvm::Expected<std::vector<SVR4LibraryInfo>>
GetLoadedSVR4Libraries()142 NativeProcessELF::GetLoadedSVR4Libraries() {
143 // Address of DT_DEBUG.d_ptr which points to r_debug
144 lldb::addr_t info_address = GetSharedLibraryInfoAddress();
145 if (info_address == LLDB_INVALID_ADDRESS)
146 return llvm::createStringError(llvm::inconvertibleErrorCode(),
147 "Invalid shared library info address");
148 // Address of r_debug
149 lldb::addr_t address = 0;
150 size_t bytes_read;
151 auto status =
152 ReadMemory(info_address, &address, GetAddressByteSize(), bytes_read);
153 if (!status.Success())
154 return status.ToError();
155 if (address == 0)
156 return llvm::createStringError(llvm::inconvertibleErrorCode(),
157 "Invalid r_debug address");
158 // Read r_debug.r_map
159 lldb::addr_t link_map = 0;
160 status = ReadMemory(address + GetAddressByteSize(), &link_map,
161 GetAddressByteSize(), bytes_read);
162 if (!status.Success())
163 return status.ToError();
164 if (address == 0)
165 return llvm::createStringError(llvm::inconvertibleErrorCode(),
166 "Invalid link_map address");
167
168 std::vector<SVR4LibraryInfo> library_list;
169 while (link_map) {
170 llvm::Expected<SVR4LibraryInfo> info =
171 GetAddressByteSize() == 8 ? ReadSVR4LibraryInfo<uint64_t>(link_map)
172 : ReadSVR4LibraryInfo<uint32_t>(link_map);
173 if (!info)
174 return info.takeError();
175 if (!info->name.empty() && info->base_addr != 0)
176 library_list.push_back(*info);
177 link_map = info->next;
178 }
179
180 return library_list;
181 }
182
183 } // namespace lldb_private
184