LLDB mainline
NativeProcessELF.cpp
Go to the documentation of this file.
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
12#include <optional>
13
14namespace lldb_private {
15
16std::optional<uint64_t>
18 if (m_aux_vector == nullptr) {
19 auto buffer_or_error = GetAuxvData();
20 if (!buffer_or_error)
21 return std::nullopt;
22 DataExtractor auxv_data(buffer_or_error.get()->getBufferStart(),
23 buffer_or_error.get()->getBufferSize(),
25 m_aux_vector = std::make_unique<AuxVector>(auxv_data);
26 }
27
28 return m_aux_vector->GetAuxValue(type);
29}
30
33 if (GetAddressByteSize() == 8)
35 GetELFImageInfoAddress<llvm::ELF::Elf64_Ehdr, llvm::ELF::Elf64_Phdr,
36 llvm::ELF::Elf64_Dyn>();
37 else
39 GetELFImageInfoAddress<llvm::ELF::Elf32_Ehdr, llvm::ELF::Elf32_Phdr,
40 llvm::ELF::Elf32_Dyn>();
41 }
42
44}
45
46template <typename ELF_EHDR, typename ELF_PHDR, typename ELF_DYN>
48 std::optional<uint64_t> maybe_phdr_addr =
50 std::optional<uint64_t> maybe_phdr_entry_size =
52 std::optional<uint64_t> maybe_phdr_num_entries =
54 if (!maybe_phdr_addr || !maybe_phdr_entry_size || !maybe_phdr_num_entries)
56 lldb::addr_t phdr_addr = *maybe_phdr_addr;
57 size_t phdr_entry_size = *maybe_phdr_entry_size;
58 size_t phdr_num_entries = *maybe_phdr_num_entries;
59
60 // Find the PT_DYNAMIC segment (.dynamic section) in the program header and
61 // what the load bias by calculating the difference of the program header
62 // load address and its virtual address.
63 lldb::offset_t load_bias;
64 bool found_load_bias = false;
65 lldb::addr_t dynamic_section_addr = 0;
66 uint64_t dynamic_section_size = 0;
67 bool found_dynamic_section = false;
68 ELF_PHDR phdr_entry;
69 for (size_t i = 0; i < phdr_num_entries; i++) {
70 size_t bytes_read;
71 auto error = ReadMemory(phdr_addr + i * phdr_entry_size, &phdr_entry,
72 sizeof(phdr_entry), bytes_read);
73 if (!error.Success())
75 if (phdr_entry.p_type == llvm::ELF::PT_PHDR) {
76 load_bias = phdr_addr - phdr_entry.p_vaddr;
77 found_load_bias = true;
78 }
79
80 if (phdr_entry.p_type == llvm::ELF::PT_DYNAMIC) {
81 dynamic_section_addr = phdr_entry.p_vaddr;
82 dynamic_section_size = phdr_entry.p_memsz;
83 found_dynamic_section = true;
84 }
85 }
86
87 if (!found_load_bias || !found_dynamic_section)
89
90 // Find the DT_DEBUG entry in the .dynamic section
91 dynamic_section_addr += load_bias;
92 ELF_DYN dynamic_entry;
93 size_t dynamic_num_entries = dynamic_section_size / sizeof(dynamic_entry);
94 for (size_t i = 0; i < dynamic_num_entries; i++) {
95 size_t bytes_read;
96 auto error = ReadMemory(dynamic_section_addr + i * sizeof(dynamic_entry),
97 &dynamic_entry, sizeof(dynamic_entry), bytes_read);
98 if (!error.Success())
100 // Return the &DT_DEBUG->d_ptr which points to r_debug which contains the
101 // link_map.
102 if (dynamic_entry.d_tag == llvm::ELF::DT_DEBUG) {
103 return dynamic_section_addr + i * sizeof(dynamic_entry) +
104 sizeof(dynamic_entry.d_tag);
105 }
106 }
107
109}
110
112 llvm::ELF::Elf32_Ehdr, llvm::ELF::Elf32_Phdr, llvm::ELF::Elf32_Dyn>();
114 llvm::ELF::Elf64_Ehdr, llvm::ELF::Elf64_Phdr, llvm::ELF::Elf64_Dyn>();
115
116template <typename T>
117llvm::Expected<SVR4LibraryInfo>
119 ELFLinkMap<T> link_map;
120 size_t bytes_read;
121 auto error =
122 ReadMemory(link_map_addr, &link_map, sizeof(link_map), bytes_read);
123 if (!error.Success())
124 return error.ToError();
125
126 char name_buffer[PATH_MAX];
127 llvm::Expected<llvm::StringRef> string_or_error = ReadCStringFromMemory(
128 link_map.l_name, &name_buffer[0], sizeof(name_buffer), bytes_read);
129 if (!string_or_error)
130 return string_or_error.takeError();
131
132 SVR4LibraryInfo info;
133 info.name = string_or_error->str();
134 info.link_map = link_map_addr;
135 info.base_addr = link_map.l_addr;
136 info.ld_addr = link_map.l_ld;
137 info.next = link_map.l_next;
138
139 return info;
140}
141
142llvm::Expected<std::vector<SVR4LibraryInfo>>
144 // Address of DT_DEBUG.d_ptr which points to r_debug
146 if (info_address == LLDB_INVALID_ADDRESS)
147 return llvm::createStringError(llvm::inconvertibleErrorCode(),
148 "Invalid shared library info address");
149 // Address of r_debug
150 lldb::addr_t address = 0;
151 size_t bytes_read;
152 auto status =
153 ReadMemory(info_address, &address, GetAddressByteSize(), bytes_read);
154 if (!status.Success())
155 return status.ToError();
156 if (address == 0)
157 return llvm::createStringError(llvm::inconvertibleErrorCode(),
158 "Invalid r_debug address");
159 // Read r_debug.r_map
160 lldb::addr_t link_map = 0;
161 status = ReadMemory(address + GetAddressByteSize(), &link_map,
162 GetAddressByteSize(), bytes_read);
163 if (!status.Success())
164 return status.ToError();
165 if (link_map == 0)
166 return llvm::createStringError(llvm::inconvertibleErrorCode(),
167 "Invalid link_map address");
168
169 std::vector<SVR4LibraryInfo> library_list;
170 while (link_map) {
171 llvm::Expected<SVR4LibraryInfo> info =
172 GetAddressByteSize() == 8 ? ReadSVR4LibraryInfo<uint64_t>(link_map)
173 : ReadSVR4LibraryInfo<uint32_t>(link_map);
174 if (!info)
175 return info.takeError();
176 if (!info->name.empty() && info->base_addr != 0)
177 library_list.push_back(*info);
178 link_map = info->next;
179 }
180
181 return library_list;
182}
183
187}
188
189} // namespace lldb_private
static llvm::raw_ostream & error(Stream &strm)
EntryType
Constants describing the type of entry.
Definition: AuxVector.h:26
@ AUXV_AT_PHNUM
Number of program headers.
Definition: AuxVector.h:32
@ AUXV_AT_PHDR
Program headers.
Definition: AuxVector.h:30
@ AUXV_AT_PHENT
Size of program header.
Definition: AuxVector.h:31
An data extractor class.
Definition: DataExtractor.h:48
llvm::Expected< SVR4LibraryInfo > ReadSVR4LibraryInfo(lldb::addr_t link_map_addr)
std::unique_ptr< AuxVector > m_aux_vector
llvm::Expected< std::vector< SVR4LibraryInfo > > GetLoadedSVR4Libraries() override
lldb::addr_t GetSharedLibraryInfoAddress() override
std::optional< uint64_t > GetAuxValue(enum AuxVector::EntryType type)
void NotifyDidExec() override
Notify the delegate that an exec occurred.
std::optional< lldb::addr_t > m_shared_library_info_addr
llvm::Expected< llvm::StringRef > ReadCStringFromMemory(lldb::addr_t addr, char *buffer, size_t max_size, size_t &total_bytes_read)
Reads a null terminated string from memory.
virtual Status ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read)=0
virtual void NotifyDidExec()
Notify the delegate that an exec occurred.
virtual llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > GetAuxvData() const =0
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
uint64_t offset_t
Definition: lldb-types.h:85
uint64_t addr_t
Definition: lldb-types.h:80
#define PATH_MAX