Skip to content

Commit e4c83b7

Browse files
[lldb][NFC] Change ObjectFile argument type (#171574)
The ObjectFile plugin interface accepts an optional DataBufferSP argument. If the caller has the contents of the binary, it can provide this in that DataBufferSP. The ObjectFile subclasses in their CreateInstance methods will fill in the DataBufferSP with the actual binary contents if it is not set. ObjectFile base class creates an ivar DataExtractor from the DataBufferSP passed in. My next patch will be a caller that creates a VirtualDataExtractor with the binary data, and needs to pass that in to the ObjectFile plugin, instead of the bag-of-bytes DataBufferSP. It builds on the previous patch changing ObjectFile's ivar from DataExtractor to DataExtractorSP so I could pass in a subclass in the shared ptr. And it will be using the VirtualDataExtractor that Jonas added in #168802 No behavior is changed by the patch; we're simply moving the creation of the DataExtractor to the caller, instead of a DataBuffer that is immediately used to set up the ObjectFile DataExtractor. The patch is a bit complicated because all of the ObjectFile subclasses have to initialize their DataExtractor to pass in to the base class. I ran the testsuite on macOS and on AArch64 Ubutnu. (btw David, I ran it under qemu on my M4 mac with SME-no-SVE again, Ubuntu 25.10, checked lshw(1) cpu capabilities, and qemu doesn't seem to be virtualizing the SME, that explains why the testsuite passes) rdar://148939795 --------- Co-authored-by: Jonas Devlieghere <jonas@devlieghere.com>
1 parent 3142e3a commit e4c83b7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+476
-343
lines changed

lldb/include/lldb/Expression/ObjectFileJIT.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ class ObjectFileJIT : public ObjectFile {
4949
}
5050

5151
static lldb_private::ObjectFile *
52-
CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
53-
lldb::offset_t data_offset, const lldb_private::FileSpec *file,
54-
lldb::offset_t file_offset, lldb::offset_t length);
52+
CreateInstance(const lldb::ModuleSP &module_sp,
53+
lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset,
54+
const lldb_private::FileSpec *file, lldb::offset_t file_offset,
55+
lldb::offset_t length);
5556

5657
static lldb_private::ObjectFile *CreateMemoryInstance(
5758
const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,

lldb/include/lldb/Symbol/ObjectContainer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ class ObjectContainer : public PluginInterface, public ModuleChild {
9797
/// Attempts to parse the object header.
9898
///
9999
/// This function is used as a test to see if a given plug-in instance can
100-
/// parse the header data already contained in ObjectContainer::m_data. If
101-
/// an object file parser does not recognize that magic bytes in a header,
102-
/// false should be returned and the next plug-in can attempt to parse an
103-
/// object file.
100+
/// parse the header data already contained in
101+
/// ObjectContainer::m_extractor_sp. If an object file parser does not
102+
/// recognize that magic bytes in a header, false should be returned and the
103+
/// next plug-in can attempt to parse an object file.
104104
///
105105
/// \return
106106
/// Returns \b true if the header was parsed successfully, \b
@@ -140,7 +140,7 @@ class ObjectContainer : public PluginInterface, public ModuleChild {
140140
lldb::addr_t m_length;
141141

142142
/// The data for this object file so things can be parsed lazily.
143-
DataExtractor m_data;
143+
lldb::DataExtractorSP m_extractor_sp;
144144

145145
private:
146146
ObjectContainer(const ObjectContainer &) = delete;

lldb/include/lldb/Symbol/ObjectFile.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
105105
/// more than one architecture or object.
106106
ObjectFile(const lldb::ModuleSP &module_sp, const FileSpec *file_spec_ptr,
107107
lldb::offset_t file_offset, lldb::offset_t length,
108-
lldb::DataBufferSP data_sp, lldb::offset_t data_offset);
108+
lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset);
109109

110110
ObjectFile(const lldb::ModuleSP &module_sp, const lldb::ProcessSP &process_sp,
111-
lldb::addr_t header_addr, lldb::DataBufferSP data_sp);
111+
lldb::addr_t header_addr, lldb::DataExtractorSP extractor_sp);
112112

113113
/// Destructor.
114114
///
@@ -152,7 +152,7 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
152152
static lldb::ObjectFileSP
153153
FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file_spec,
154154
lldb::offset_t file_offset, lldb::offset_t file_size,
155-
lldb::DataBufferSP &data_sp, lldb::offset_t &data_offset);
155+
lldb::DataExtractorSP extractor_sp, lldb::offset_t &data_offset);
156156

157157
/// Find a ObjectFile plug-in that can parse a file in memory.
158158
///

lldb/include/lldb/Utility/DataExtractor.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLDB_UTILITY_DATAEXTRACTOR_H
1010
#define LLDB_UTILITY_DATAEXTRACTOR_H
1111

12+
#include "lldb/Utility/DataBuffer.h"
1213
#include "lldb/Utility/Endian.h"
1314
#include "lldb/lldb-defines.h"
1415
#include "lldb/lldb-enumerations.h"
@@ -90,10 +91,10 @@ class DataExtractor {
9091

9192
/// Construct with shared data.
9293
///
93-
/// Copies the data shared pointer which adds a reference to the contained
94-
/// in \a data_sp. The shared data reference is reference counted to ensure
95-
/// the data lives as long as anyone still has a valid shared pointer to the
96-
/// data in \a data_sp.
94+
/// Copies the data shared pointer which adds a reference to the data
95+
/// contained in \a data_sp. The shared data reference is reference counted to
96+
/// ensure the data lives as long as anyone still has a valid shared pointer
97+
/// to the data in \a data_sp.
9798
///
9899
/// \param[in] data_sp
99100
/// A shared pointer to data.
@@ -109,6 +110,18 @@ class DataExtractor {
109110
DataExtractor(const lldb::DataBufferSP &data_sp, lldb::ByteOrder byte_order,
110111
uint32_t addr_size, uint32_t target_byte_size = 1);
111112

113+
/// Construct with shared data, but byte-order & addr-size are unspecified.
114+
///
115+
/// Copies the data shared pointer which adds a reference to the data
116+
/// contained in \a data_sp. The shared data reference is reference counted to
117+
/// ensure the data lives as long as anyone still has a valid shared pointer
118+
/// to the data in \a data_sp.
119+
///
120+
/// \param[in] data_sp
121+
/// A shared pointer to data.
122+
DataExtractor(const lldb::DataBufferSP &data_sp,
123+
uint32_t target_byte_size = 1);
124+
112125
/// Construct with a subset of \a data.
113126
///
114127
/// Initialize this object with a subset of the data bytes in \a data. If \a
@@ -807,6 +820,8 @@ class DataExtractor {
807820

808821
lldb::DataBufferSP &GetSharedDataBuffer() { return m_data_sp; }
809822

823+
bool HasData() { return m_start && m_end && m_end - m_start > 0; }
824+
810825
/// Peek at a C string at \a offset.
811826
///
812827
/// Peeks at a string in the contained data. No verification is done to make

lldb/include/lldb/lldb-private-interfaces.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ typedef size_t (*ObjectFileGetModuleSpecifications)(
4949
const FileSpec &file, lldb::DataBufferSP &data_sp,
5050
lldb::offset_t data_offset, lldb::offset_t file_offset,
5151
lldb::offset_t length, ModuleSpecList &module_specs);
52-
typedef ObjectFile *(*ObjectFileCreateInstance)(const lldb::ModuleSP &module_sp,
53-
lldb::DataBufferSP data_sp,
54-
lldb::offset_t data_offset,
55-
const FileSpec *file,
56-
lldb::offset_t file_offset,
57-
lldb::offset_t length);
52+
typedef ObjectFile *(*ObjectFileCreateInstance)(
53+
const lldb::ModuleSP &module_sp, lldb::DataExtractorSP extractor_sp,
54+
lldb::offset_t data_offset, const FileSpec *file,
55+
lldb::offset_t file_offset, lldb::offset_t length);
5856
typedef ObjectFile *(*ObjectFileCreateMemoryInstance)(
5957
const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
6058
const lldb::ProcessSP &process_sp, lldb::addr_t offset);

lldb/source/Core/Module.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,10 +1206,12 @@ ObjectFile *Module::GetObjectFile() {
12061206
m_did_load_objfile = true;
12071207
// FindPlugin will modify its data_sp argument. Do not let it
12081208
// modify our m_data_sp member.
1209-
auto data_sp = m_data_sp;
1209+
DataExtractorSP extractor_sp;
1210+
if (m_data_sp)
1211+
extractor_sp = std::make_shared<DataExtractor>(m_data_sp);
12101212
m_objfile_sp = ObjectFile::FindPlugin(
12111213
shared_from_this(), &m_file, m_object_offset,
1212-
file_size - m_object_offset, data_sp, data_offset);
1214+
file_size - m_object_offset, extractor_sp, data_offset);
12131215
if (m_objfile_sp) {
12141216
// Once we get the object file, update our module with the object
12151217
// file's architecture since it might differ in vendor/os if some

lldb/source/Expression/ObjectFileJIT.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void ObjectFileJIT::Terminate() {
4141
}
4242

4343
ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
44-
DataBufferSP data_sp,
44+
DataExtractorSP extractor_sp,
4545
lldb::offset_t data_offset,
4646
const FileSpec *file,
4747
lldb::offset_t file_offset,
@@ -70,7 +70,8 @@ size_t ObjectFileJIT::GetModuleSpecifications(
7070

7171
ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp,
7272
const ObjectFileJITDelegateSP &delegate_sp)
73-
: ObjectFile(module_sp, nullptr, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
73+
: ObjectFile(module_sp, nullptr, 0, 0, DataExtractorSP(), 0),
74+
m_delegate_wp() {
7475
if (delegate_sp) {
7576
m_delegate_wp = delegate_sp;
7677
m_data_nsp->SetByteOrder(delegate_sp->GetByteOrder());

lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,19 @@ void ObjectContainerBSDArchive::Object::Dump() const {
6868
ObjectContainerBSDArchive::Archive::Archive(const lldb_private::ArchSpec &arch,
6969
const llvm::sys::TimePoint<> &time,
7070
lldb::offset_t file_offset,
71-
lldb_private::DataExtractor &data,
71+
lldb::DataExtractorSP extractor_sp,
7272
ArchiveType archive_type)
7373
: m_arch(arch), m_modification_time(time), m_file_offset(file_offset),
74-
m_objects(), m_data(data), m_archive_type(archive_type) {}
74+
m_objects(), m_extractor_sp(extractor_sp), m_archive_type(archive_type) {}
7575

7676
Log *l = GetLog(LLDBLog::Object);
7777
ObjectContainerBSDArchive::Archive::~Archive() = default;
7878

7979
size_t ObjectContainerBSDArchive::Archive::ParseObjects() {
80-
DataExtractor &data = m_data;
81-
8280
std::unique_ptr<llvm::MemoryBuffer> mem_buffer =
8381
llvm::MemoryBuffer::getMemBuffer(
84-
llvm::StringRef((const char *)data.GetDataStart(),
85-
data.GetByteSize()),
82+
llvm::StringRef((const char *)m_extractor_sp->GetDataStart(),
83+
m_extractor_sp->GetByteSize()),
8684
llvm::StringRef(),
8785
/*RequiresNullTerminator=*/false);
8886

@@ -221,9 +219,9 @@ ObjectContainerBSDArchive::Archive::shared_ptr
221219
ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile(
222220
const FileSpec &file, const ArchSpec &arch,
223221
const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset,
224-
DataExtractor &data, ArchiveType archive_type) {
222+
DataExtractorSP extractor_sp, ArchiveType archive_type) {
225223
shared_ptr archive_sp(
226-
new Archive(arch, time, file_offset, data, archive_type));
224+
new Archive(arch, time, file_offset, extractor_sp, archive_type));
227225
if (archive_sp) {
228226
const size_t num_objects = archive_sp->ParseObjects();
229227
if (num_objects > 0) {
@@ -368,16 +366,17 @@ ObjectContainerBSDArchive::~ObjectContainerBSDArchive() = default;
368366

369367
bool ObjectContainerBSDArchive::ParseHeader() {
370368
if (m_archive_sp.get() == nullptr) {
371-
if (m_data.GetByteSize() > 0) {
369+
if (m_extractor_sp->GetByteSize() > 0) {
372370
ModuleSP module_sp(GetModule());
373371
if (module_sp) {
374372
m_archive_sp = Archive::ParseAndCacheArchiveForFile(
375373
m_file, module_sp->GetArchitecture(),
376-
module_sp->GetModificationTime(), m_offset, m_data, m_archive_type);
374+
module_sp->GetModificationTime(), m_offset, m_extractor_sp,
375+
m_archive_type);
377376
}
378-
// Clear the m_data that contains the entire archive data and let our
379-
// m_archive_sp hold onto the data.
380-
m_data.Clear();
377+
// Clear the m_extractor_sp that contains the entire archive data and let
378+
// our m_archive_sp hold onto the data.
379+
m_extractor_sp = std::make_shared<DataExtractor>();
381380
}
382381
}
383382
return m_archive_sp.get() != nullptr;
@@ -416,14 +415,18 @@ ObjectFileSP ObjectContainerBSDArchive::GetObjectFile(const FileSpec *file) {
416415
child_data_sp->GetByteSize() != object->file_size)
417416
return ObjectFileSP();
418417
lldb::offset_t data_offset = 0;
418+
DataExtractorSP extractor_sp =
419+
std::make_shared<DataExtractor>(child_data_sp);
419420
return ObjectFile::FindPlugin(
420421
module_sp, &child, m_offset + object->file_offset,
421-
object->file_size, child_data_sp, data_offset);
422+
object->file_size, extractor_sp, data_offset);
422423
}
423424
lldb::offset_t data_offset = object->file_offset;
425+
DataExtractorSP extractor_sp =
426+
std::make_shared<DataExtractor>(m_archive_sp->GetData());
424427
return ObjectFile::FindPlugin(
425428
module_sp, file, m_offset + object->file_offset, object->file_size,
426-
m_archive_sp->GetData().GetSharedDataBuffer(), data_offset);
429+
extractor_sp, data_offset);
427430
}
428431
}
429432
}
@@ -438,9 +441,10 @@ size_t ObjectContainerBSDArchive::GetModuleSpecifications(
438441
// We have data, which means this is the first 512 bytes of the file Check to
439442
// see if the magic bytes match and if they do, read the entire table of
440443
// contents for the archive and cache it
441-
DataExtractor data;
442-
data.SetData(data_sp, data_offset, data_sp->GetByteSize());
443-
ArchiveType archive_type = ObjectContainerBSDArchive::MagicBytesMatch(data);
444+
DataExtractorSP extractor_sp = std::make_shared<DataExtractor>();
445+
extractor_sp->SetData(data_sp, data_offset, data_sp->GetByteSize());
446+
ArchiveType archive_type =
447+
ObjectContainerBSDArchive::MagicBytesMatch(*extractor_sp.get());
444448
if (!file || !data_sp || archive_type == ArchiveType::Invalid)
445449
return 0;
446450

@@ -455,9 +459,10 @@ size_t ObjectContainerBSDArchive::GetModuleSpecifications(
455459
data_sp =
456460
FileSystem::Instance().CreateDataBuffer(file, file_size, file_offset);
457461
if (data_sp) {
458-
data.SetData(data_sp, 0, data_sp->GetByteSize());
462+
extractor_sp->SetData(data_sp);
459463
archive_sp = Archive::ParseAndCacheArchiveForFile(
460-
file, ArchSpec(), file_mod_time, file_offset, data, archive_type);
464+
file, ArchSpec(), file_mod_time, file_offset, extractor_sp,
465+
archive_type);
461466
}
462467
}
463468

lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#include "lldb/Symbol/ObjectContainer.h"
1414
#include "lldb/Utility/ArchSpec.h"
1515
#include "lldb/Utility/ConstString.h"
16+
#include "lldb/Utility/DataExtractor.h"
1617
#include "lldb/Utility/FileSpec.h"
18+
#include "lldb/Utility/NonNullSharedPtr.h"
1719

1820
#include "llvm/Object/Archive.h"
1921
#include "llvm/Support/Chrono.h"
@@ -59,7 +61,8 @@ class ObjectContainerBSDArchive : public lldb_private::ObjectContainer {
5961
lldb::offset_t length,
6062
lldb_private::ModuleSpecList &specs);
6163

62-
static ArchiveType MagicBytesMatch(const lldb_private::DataExtractor &data);
64+
static ArchiveType
65+
MagicBytesMatch(const lldb_private::DataExtractor &extractor);
6366

6467
// Member Functions
6568
bool ParseHeader() override;
@@ -81,11 +84,11 @@ class ObjectContainerBSDArchive : public lldb_private::ObjectContainer {
8184

8285
void Clear();
8386

84-
lldb::offset_t ExtractFromThin(const lldb_private::DataExtractor &data,
87+
lldb::offset_t ExtractFromThin(const lldb_private::DataExtractor &extractor,
8588
lldb::offset_t offset,
8689
llvm::StringRef stringTable);
8790

88-
lldb::offset_t Extract(const lldb_private::DataExtractor &data,
91+
lldb::offset_t Extract(const lldb_private::DataExtractor &extractor,
8992
lldb::offset_t offset);
9093
/// Object name in the archive.
9194
lldb_private::ConstString ar_name;
@@ -112,7 +115,7 @@ class ObjectContainerBSDArchive : public lldb_private::ObjectContainer {
112115

113116
Archive(const lldb_private::ArchSpec &arch,
114117
const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,
115-
lldb_private::DataExtractor &data, ArchiveType archive_type);
118+
lldb::DataExtractorSP extractor_sp, ArchiveType archive_type);
116119

117120
~Archive();
118121

@@ -127,7 +130,7 @@ class ObjectContainerBSDArchive : public lldb_private::ObjectContainer {
127130
static Archive::shared_ptr ParseAndCacheArchiveForFile(
128131
const lldb_private::FileSpec &file, const lldb_private::ArchSpec &arch,
129132
const llvm::sys::TimePoint<> &mod_time, lldb::offset_t file_offset,
130-
lldb_private::DataExtractor &data, ArchiveType archive_type);
133+
lldb::DataExtractorSP extractor_sp, ArchiveType archive_type);
131134

132135
size_t GetNumObjects() const { return m_objects.size(); }
133136

@@ -154,7 +157,8 @@ class ObjectContainerBSDArchive : public lldb_private::ObjectContainer {
154157

155158
bool HasNoExternalReferences() const;
156159

157-
lldb_private::DataExtractor &GetData() { return m_data; }
160+
lldb_private::DataExtractor &GetData() { return *m_extractor_sp.get(); }
161+
lldb::DataExtractorSP &GetDataSP() { return m_extractor_sp; }
158162

159163
ArchiveType GetArchiveType() { return m_archive_type; }
160164

@@ -166,9 +170,9 @@ class ObjectContainerBSDArchive : public lldb_private::ObjectContainer {
166170
lldb::offset_t m_file_offset;
167171
std::vector<Object> m_objects;
168172
ObjectNameToIndexMap m_object_name_to_index_map;
169-
lldb_private::DataExtractor m_data; ///< The data for this object container
170-
///so we don't lose data if the .a files
171-
///gets modified
173+
/// The data for this object container so we don't lose data if the .a files
174+
/// gets modified.
175+
lldb::DataExtractorSP m_extractor_sp;
172176
ArchiveType m_archive_type;
173177
};
174178

0 commit comments

Comments
 (0)