LLDB mainline
ObjectFileJSON.cpp
Go to the documentation of this file.
1//===-- ObjectFileJSON.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
10#include "lldb/Core/Module.h"
13#include "lldb/Core/Section.h"
14#include "lldb/Symbol/Symbol.h"
15#include "lldb/Target/Target.h"
17#include "lldb/Utility/Log.h"
18#include "llvm/ADT/DenseSet.h"
19#include <optional>
20
21using namespace llvm;
22using namespace lldb;
23using namespace lldb_private;
24
26
28
34
38
40 DataExtractorSP extractor_sp,
41 offset_t data_offset,
42 const FileSpec *file,
43 offset_t file_offset,
44 offset_t length) {
45 if (!extractor_sp || !extractor_sp->HasData()) {
46 DataBufferSP data_sp = MapFileData(*file, length, file_offset);
47 if (!data_sp)
48 return nullptr;
49 extractor_sp = std::make_shared<DataExtractor>(data_sp);
50 data_offset = 0;
51 }
52
53 if (!MagicBytesMatch(extractor_sp->GetSharedDataBuffer(), 0,
54 extractor_sp->GetByteSize()))
55 return nullptr;
56
57 // Update the data to contain the entire file if it doesn't already.
58 if (extractor_sp->GetByteSize() < length) {
59 DataBufferSP data_sp = MapFileData(*file, length, file_offset);
60 if (!data_sp)
61 return nullptr;
62 extractor_sp->SetData(data_sp);
63 data_offset = 0;
64 }
65
67
68 auto text = llvm::StringRef(reinterpret_cast<const char *>(
69 extractor_sp->GetSharedDataBuffer()->GetBytes()));
70
71 Expected<json::Value> json = json::parse(text);
72 if (!json) {
73 LLDB_LOG_ERROR(log, json.takeError(),
74 "failed to parse JSON object file: {0}");
75 return nullptr;
76 }
77
78 json::Path::Root root;
79 Header header;
80 if (!fromJSON(*json, header, root)) {
81 LLDB_LOG_ERROR(log, root.getError(),
82 "failed to parse JSON object file header: {0}");
83 return nullptr;
84 }
85
86 ArchSpec arch(header.triple);
87 UUID uuid;
88 uuid.SetFromStringRef(header.uuid);
89 Type type = header.type.value_or(eTypeDebugInfo);
90
91 Body body;
92 if (!fromJSON(*json, body, root)) {
93 LLDB_LOG_ERROR(log, root.getError(),
94 "failed to parse JSON object file body: {0}");
95 return nullptr;
96 }
97
98 return new ObjectFileJSON(module_sp, extractor_sp, data_offset, file,
99 file_offset, length, std::move(arch),
100 std::move(uuid), type, std::move(body.symbols),
101 std::move(body.sections));
102}
103
105 WritableDataBufferSP data_sp,
106 const ProcessSP &process_sp,
107 addr_t header_addr) {
108 return nullptr;
109}
110
112 const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,
113 offset_t file_offset, offset_t length, ModuleSpecList &specs) {
114 if (!MagicBytesMatch(data_sp, data_offset, data_sp->GetByteSize()))
115 return 0;
116
117 // Update the data to contain the entire file if it doesn't already.
118 if (data_sp->GetByteSize() < length) {
119 data_sp = MapFileData(file, length, file_offset);
120 if (!data_sp)
121 return 0;
122 data_offset = 0;
123 }
124
126
127 auto text =
128 llvm::StringRef(reinterpret_cast<const char *>(data_sp->GetBytes()));
129
130 Expected<json::Value> json = json::parse(text);
131 if (!json) {
132 LLDB_LOG_ERROR(log, json.takeError(),
133 "failed to parse JSON object file: {0}");
134 return 0;
135 }
136
137 json::Path::Root root;
138 Header header;
139 if (!fromJSON(*json, header, root)) {
140 LLDB_LOG_ERROR(log, root.getError(),
141 "failed to parse JSON object file header: {0}");
142 return 0;
143 }
144
145 ArchSpec arch(header.triple);
146 UUID uuid;
147 uuid.SetFromStringRef(header.uuid);
148
149 ModuleSpec spec(file, std::move(arch));
150 spec.GetUUID() = std::move(uuid);
151 specs.Append(spec);
152 return 1;
153}
154
156 DataExtractorSP extractor_sp,
157 offset_t data_offset, const FileSpec *file,
158 offset_t offset, offset_t length, ArchSpec arch,
159 UUID uuid, Type type,
160 std::vector<JSONSymbol> symbols,
161 std::vector<JSONSection> sections)
162 : ObjectFile(module_sp, file, offset, length, extractor_sp, data_offset),
163 m_arch(std::move(arch)), m_uuid(std::move(uuid)), m_type(type),
164 m_symbols(std::move(symbols)), m_sections(std::move(sections)) {}
165
167 // We already parsed the header during initialization.
168 return true;
169}
170
173 SectionList *section_list = GetModule()->GetSectionList();
174 for (JSONSymbol json_symbol : m_symbols) {
175 llvm::Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, section_list);
176 if (!symbol) {
177 LLDB_LOG_ERROR(log, symbol.takeError(), "invalid symbol: {0}");
178 continue;
179 }
180 symtab.AddSymbol(*symbol);
181 }
182 symtab.Finalize();
183}
184
185void ObjectFileJSON::CreateSections(SectionList &unified_section_list) {
186 if (m_sections_up)
187 return;
188 m_sections_up = std::make_unique<SectionList>();
189
190 lldb::user_id_t id = 0;
191 for (const auto &json_section : m_sections) {
192 auto make_section = [this, &id](const JSONSection &section,
193 SectionSP parent_section_sp =
194 nullptr) -> SectionSP {
195 SectionSP section_sp;
196 auto sect_id = section.user_id.value_or(id + 1);
197 if (!section.user_id.has_value())
198 ++id;
199 const auto name = ConstString(section.name);
200 const auto sect_type = section.type.value_or(eSectionTypeCode);
201 const auto vm_addr = section.address.value_or(0);
202 const auto vm_size = section.size.value_or(0);
203 const auto file_offset = section.file_offset.value_or(0);
204 const auto file_size = section.file_size.value_or(0);
205 const auto log2align = section.log2align.value_or(0);
206 const auto flags = section.flags.value_or(0);
207 if (parent_section_sp) {
208 section_sp = std::make_shared<Section>(
209 parent_section_sp, GetModule(), this, sect_id, name, sect_type,
210 vm_addr - parent_section_sp->GetFileAddress(), vm_size, file_offset,
211 file_size, log2align, flags);
212
213 } else {
214 section_sp = std::make_shared<Section>(
215 GetModule(), this, sect_id, name, sect_type, vm_addr, vm_size,
216 file_offset, file_size, log2align, flags);
217 }
218 // Set permissions
219 uint32_t permissions = 0;
220 if (section.read.value_or(0))
221 permissions |= lldb::ePermissionsReadable;
222 if (section.write.value_or(0))
223 permissions |= lldb::ePermissionsWritable;
224 if (section.execute.value_or(0))
225 permissions |= lldb::ePermissionsExecutable;
226 if (permissions)
227 section_sp->SetPermissions(permissions);
228 section_sp->SetIsFake(section.fake.value_or(false));
229 section_sp->SetIsEncrypted(section.encrypted.value_or(false));
230 section_sp->SetIsThreadSpecific(section.thread_specific.value_or(false));
231 return section_sp;
232 };
233 auto section_sp = make_section(json_section);
234 for (const auto &subsection : json_section.subsections) {
235 SectionSP subsection_sp = make_section(subsection, section_sp);
236 section_sp->GetChildren().AddSection(subsection_sp);
237 }
238
239 m_sections_up->AddSection(section_sp);
240 unified_section_list.AddSection(section_sp);
241 }
242}
243
245 bool value_is_offset) {
247 if (!m_sections_up)
248 return true;
249
250 addr_t slide = value;
251 if (!value_is_offset) {
252 addr_t lowest_addr = LLDB_INVALID_ADDRESS;
253 for (const SectionSP &section_sp : *m_sections_up) {
254 addr_t section_load_addr = section_sp->GetFileAddress();
255 lowest_addr = std::min(lowest_addr, section_load_addr);
256 }
257 if (lowest_addr == LLDB_INVALID_ADDRESS)
258 return false;
259 slide = value - lowest_addr;
260 }
261
262 // Apply slide to each section's file address.
263 for (const SectionSP &section_sp : *m_sections_up) {
264 addr_t section_load_addr = section_sp->GetFileAddress();
265 if (section_load_addr != LLDB_INVALID_ADDRESS) {
266 LLDB_LOGF(
267 log,
268 "ObjectFileJSON::SetLoadAddress section %s to load addr 0x%" PRIx64,
269 section_sp->GetName().AsCString(), section_load_addr + slide);
270 target.SetSectionLoadAddress(section_sp, section_load_addr + slide,
271 /*warn_multiple=*/true);
272 }
273 }
274
275 return true;
276}
277
279 lldb::addr_t data_offset,
280 lldb::addr_t data_length) {
281 DataExtractor data;
282 data.SetData(data_sp, data_offset, data_length);
283 lldb::offset_t offset = 0;
284 uint32_t magic = data.GetU8(&offset);
285 return magic == '{';
286}
287
288namespace lldb_private {
289
290bool fromJSON(const json::Value &value, ObjectFileJSON::Header &header,
291 json::Path path) {
292 json::ObjectMapper o(value, path);
293 return o && o.map("triple", header.triple) && o.map("uuid", header.uuid) &&
294 o.map("type", header.type);
295}
296
297bool fromJSON(const json::Value &value, ObjectFileJSON::Body &body,
298 json::Path path) {
299 json::ObjectMapper o(value, path);
300 return o && o.mapOptional("symbols", body.symbols) &&
301 o.mapOptional("sections", body.sections);
302}
303
304} // namespace lldb_private
#define LLDB_LOGF(log,...)
Definition Log.h:376
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:392
#define LLDB_PLUGIN_DEFINE(PluginName)
An architecture specification class.
Definition ArchSpec.h:31
A uniqued constant string class.
Definition ConstString.h:40
An data extractor class.
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
A file utility class.
Definition FileSpec.h:57
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
void Append(const ModuleSpec &spec)
Definition ModuleSpec.h:326
std::vector< JSONSection > m_sections
bool SetLoadAddress(Target &target, lldb::addr_t value, bool value_is_offset) override
Sets the load address for an entire module, assuming a rigid slide of sections, if possible in the im...
ObjectFileJSON(const lldb::ModuleSP &module_sp, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset, const FileSpec *file, lldb::offset_t offset, lldb::offset_t length, ArchSpec arch, UUID uuid, Type type, std::vector< JSONSymbol > symbols, std::vector< JSONSection > sections)
void ParseSymtab(lldb_private::Symtab &symtab) override
Parse the symbol table into the provides symbol table object.
static bool MagicBytesMatch(lldb::DataBufferSP data_sp, lldb::addr_t offset, lldb::addr_t length)
static const char * GetPluginDescriptionStatic()
static size_t GetModuleSpecifications(const FileSpec &file, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, ModuleSpecList &specs)
void CreateSections(SectionList &unified_section_list) override
bool ParseHeader() override
Attempts to parse the object header.
static ObjectFile * CreateMemoryInstance(const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
std::vector< JSONSymbol > m_symbols
static ObjectFile * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset, const FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
static llvm::StringRef GetPluginNameStatic()
std::unique_ptr< lldb_private::SectionList > m_sections_up
Definition ObjectFile.h:799
static lldb::DataBufferSP MapFileData(const FileSpec &file, uint64_t Size, uint64_t Offset)
@ eTypeDebugInfo
An object file that contains only debug information.
Definition ObjectFile.h:57
ObjectFile(const lldb::ModuleSP &module_sp, const FileSpec *file_spec_ptr, lldb::offset_t file_offset, lldb::offset_t length, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset)
Construct with a parent module, offset, and header data.
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
size_t AddSection(const lldb::SectionSP &section_sp)
Definition Section.cpp:488
static llvm::Expected< Symbol > FromJSON(const JSONSymbol &symbol, SectionList *section_list)
Definition Symbol.cpp:101
uint32_t AddSymbol(const Symbol &symbol)
Definition Symtab.cpp:64
bool SetSectionLoadAddress(const lldb::SectionSP &section, lldb::addr_t load_addr, bool warn_multiple=false)
Definition Target.cpp:3334
Represents UUID's of various sizes.
Definition UUID.h:27
bool SetFromStringRef(llvm::StringRef str)
Definition UUID.cpp:101
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
bool fromJSON(const llvm::json::Value &value, TraceSupportedResponse &info, llvm::json::Path path)
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
uint64_t user_id_t
Definition lldb-types.h:82
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::Section > SectionSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::Module > ModuleSP
std::optional< lldb::user_id_t > user_id
Definition Section.h:117
std::optional< uint64_t > log2align
Definition Section.h:124
std::optional< uint64_t > size
Definition Section.h:121
std::optional< uint64_t > flags
Definition Section.h:125
std::optional< bool > encrypted
Definition Section.h:133
std::optional< bool > fake
Definition Section.h:132
std::optional< lldb::SectionType > type
Definition Section.h:119
std::optional< bool > write
Definition Section.h:129
std::optional< bool > execute
Definition Section.h:130
std::optional< uint64_t > file_offset
Definition Section.h:122
std::optional< uint64_t > address
Definition Section.h:120
std::optional< bool > thread_specific
Definition Section.h:134
std::optional< uint64_t > file_size
Definition Section.h:123
std::optional< bool > read
Definition Section.h:128
std::vector< JSONSymbol > symbols
std::vector< JSONSection > sections
std::optional< ObjectFile::Type > type