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"
16#include "lldb/Utility/Log.h"
17#include "llvm/ADT/DenseSet.h"
18#include <optional>
19
20using namespace llvm;
21using namespace lldb;
22using namespace lldb_private;
23
25
27
32}
33
36}
37
40 offset_t data_offset, const FileSpec *file,
41 offset_t file_offset, offset_t length) {
42 if (!data_sp) {
43 data_sp = MapFileData(*file, length, file_offset);
44 if (!data_sp)
45 return nullptr;
46 data_offset = 0;
47 }
48
49 if (!MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
50 return nullptr;
51
52 // Update the data to contain the entire file if it doesn't already.
53 if (data_sp->GetByteSize() < length) {
54 data_sp = MapFileData(*file, length, file_offset);
55 if (!data_sp)
56 return nullptr;
57 data_offset = 0;
58 }
59
61
62 auto text =
63 llvm::StringRef(reinterpret_cast<const char *>(data_sp->GetBytes()));
64
65 Expected<json::Value> json = json::parse(text);
66 if (!json) {
67 LLDB_LOG_ERROR(log, json.takeError(),
68 "failed to parse JSON object file: {0}");
69 return nullptr;
70 }
71
72 json::Path::Root root;
73 Header header;
74 if (!fromJSON(*json, header, root)) {
75 LLDB_LOG_ERROR(log, root.getError(),
76 "failed to parse JSON object file header: {0}");
77 return nullptr;
78 }
79
80 ArchSpec arch(header.triple);
81 UUID uuid;
82 uuid.SetFromStringRef(header.uuid);
83 Type type = header.type.value_or(eTypeDebugInfo);
84
85 Body body;
86 if (!fromJSON(*json, body, root)) {
87 LLDB_LOG_ERROR(log, root.getError(),
88 "failed to parse JSON object file body: {0}");
89 return nullptr;
90 }
91
92 return new ObjectFileJSON(module_sp, data_sp, data_offset, file, file_offset,
93 length, std::move(arch), std::move(uuid), type,
94 std::move(body.symbols), std::move(body.sections));
95}
96
99 const ProcessSP &process_sp,
100 addr_t header_addr) {
101 return nullptr;
102}
103
105 const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,
106 offset_t file_offset, offset_t length, ModuleSpecList &specs) {
107 if (!MagicBytesMatch(data_sp, data_offset, data_sp->GetByteSize()))
108 return 0;
109
110 // Update the data to contain the entire file if it doesn't already.
111 if (data_sp->GetByteSize() < length) {
112 data_sp = MapFileData(file, length, file_offset);
113 if (!data_sp)
114 return 0;
115 data_offset = 0;
116 }
117
119
120 auto text =
121 llvm::StringRef(reinterpret_cast<const char *>(data_sp->GetBytes()));
122
123 Expected<json::Value> json = json::parse(text);
124 if (!json) {
125 LLDB_LOG_ERROR(log, json.takeError(),
126 "failed to parse JSON object file: {0}");
127 return 0;
128 }
129
130 json::Path::Root root;
131 Header header;
132 if (!fromJSON(*json, header, root)) {
133 LLDB_LOG_ERROR(log, root.getError(),
134 "failed to parse JSON object file header: {0}");
135 return 0;
136 }
137
138 ArchSpec arch(header.triple);
139 UUID uuid;
140 uuid.SetFromStringRef(header.uuid);
141
142 ModuleSpec spec(file, std::move(arch));
143 spec.GetUUID() = std::move(uuid);
144 specs.Append(spec);
145 return 1;
146}
147
149 offset_t data_offset, const FileSpec *file,
150 offset_t offset, offset_t length, ArchSpec arch,
151 UUID uuid, Type type,
152 std::vector<JSONSymbol> symbols,
153 std::vector<JSONSection> sections)
154 : ObjectFile(module_sp, file, offset, length, data_sp, data_offset),
155 m_arch(std::move(arch)), m_uuid(std::move(uuid)), m_type(type),
156 m_symbols(std::move(symbols)), m_sections(std::move(sections)) {}
157
159 // We already parsed the header during initialization.
160 return true;
161}
162
165 SectionList *section_list = GetModule()->GetSectionList();
166 for (JSONSymbol json_symbol : m_symbols) {
167 llvm::Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, section_list);
168 if (!symbol) {
169 LLDB_LOG_ERROR(log, symbol.takeError(), "invalid symbol: {0}");
170 continue;
171 }
172 symtab.AddSymbol(*symbol);
173 }
174 symtab.Finalize();
175}
176
177void ObjectFileJSON::CreateSections(SectionList &unified_section_list) {
178 if (m_sections_up)
179 return;
180 m_sections_up = std::make_unique<SectionList>();
181
182 lldb::user_id_t id = 1;
183 for (const auto &section : m_sections) {
184 auto section_sp = std::make_shared<Section>(
185 GetModule(), this, id++, ConstString(section.name),
186 section.type.value_or(eSectionTypeCode), 0, section.size.value_or(0), 0,
187 section.size.value_or(0), /*log2align*/ 0, /*flags*/ 0);
188 m_sections_up->AddSection(section_sp);
189 unified_section_list.AddSection(section_sp);
190 }
191}
192
194 lldb::addr_t data_offset,
195 lldb::addr_t data_length) {
196 DataExtractor data;
197 data.SetData(data_sp, data_offset, data_length);
198 lldb::offset_t offset = 0;
199 uint32_t magic = data.GetU8(&offset);
200 return magic == '{';
201}
202
203namespace lldb_private {
204
205bool fromJSON(const json::Value &value, ObjectFileJSON::Header &header,
206 json::Path path) {
207 json::ObjectMapper o(value, path);
208 return o && o.map("triple", header.triple) && o.map("uuid", header.uuid) &&
209 o.map("type", header.type);
210}
211
212bool fromJSON(const json::Value &value, ObjectFileJSON::Body &body,
213 json::Path path) {
214 json::ObjectMapper o(value, path);
215 return o && o.mapOptional("symbols", body.symbols) &&
216 o.mapOptional("sections", body.sections);
217}
218
219} // namespace lldb_private
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:382
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:32
An architecture specification class.
Definition: ArchSpec.h:31
A uniqued constant string class.
Definition: ConstString.h:40
An data extractor class.
Definition: DataExtractor.h:48
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:56
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:24
void Append(const ModuleSpec &spec)
Definition: ModuleSpec.h:308
std::vector< JSONSection > m_sections
static ObjectFile * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp, lldb::offset_t data_offset, const FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
ObjectFileJSON(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_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 llvm::StringRef GetPluginNameStatic()
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:44
std::unique_ptr< lldb_private::SectionList > m_sections_up
Definition: ObjectFile.h:762
static lldb::DataBufferSP MapFileData(const FileSpec &file, uint64_t Size, uint64_t Offset)
Definition: ObjectFile.cpp:671
@ eTypeDebugInfo
An object file that contains only debug information.
Definition: ObjectFile.h:55
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:475
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 SetFromStringRef(llvm::StringRef str)
Definition: UUID.cpp:97
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:331
bool fromJSON(const llvm::json::Value &value, TraceSupportedResponse &info, llvm::json::Path path)
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:387
uint64_t user_id_t
Definition: lldb-types.h:82
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
Definition: lldb-forward.h:334
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
Definition: lldb-forward.h:335
uint64_t addr_t
Definition: lldb-types.h:80
@ eSectionTypeCode
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:371
Definition: Debugger.h:54
std::vector< JSONSymbol > symbols
std::vector< JSONSection > sections
std::optional< ObjectFile::Type > type