@@ -39,7 +39,6 @@ using namespace lldb;
39
39
using namespace lldb_private ;
40
40
41
41
LLDB_PLUGIN_DEFINE (ObjectFileXCOFF)
42
-
43
42
// FIXME: target 64bit at this moment.
44
43
45
44
// Static methods.
@@ -95,10 +94,11 @@ bool ObjectFileXCOFF::CreateBinary() {
95
94
96
95
Log *log = GetLog (LLDBLog::Object);
97
96
98
- auto binary = llvm::object::ObjectFile::createObjectFile (
99
- llvm::MemoryBufferRef (toStringRef (m_data.GetData ()),
100
- m_file.GetFilename ().GetStringRef ()),
101
- file_magic::xcoff_object_64);
97
+ auto memory_ref = llvm::MemoryBufferRef (toStringRef (m_data.GetData ()),
98
+ m_file.GetFilename ().GetStringRef ());
99
+ llvm::file_magic magic = llvm::identify_magic (memory_ref.getBuffer ());
100
+
101
+ auto binary = llvm::object::ObjectFile::createObjectFile (memory_ref, magic);
102
102
if (!binary) {
103
103
LLDB_LOG_ERROR (log , binary.takeError (),
104
104
" Failed to create binary for file ({1}): {0}" , m_file);
@@ -143,9 +143,9 @@ size_t ObjectFileXCOFF::GetModuleSpecifications(
143
143
144
144
static uint32_t XCOFFHeaderSizeFromMagic (uint32_t magic) {
145
145
switch (magic) {
146
- // TODO: 32bit not supported.
147
- // case XCOFF::XCOFF32:
148
- // return sizeof(struct llvm::object::XCOFFFileHeader32) ;
146
+ case XCOFF::XCOFF32:
147
+ return sizeof ( struct llvm ::object::XCOFFFileHeader32);
148
+ break ;
149
149
case XCOFF::XCOFF64:
150
150
return sizeof (struct llvm ::object::XCOFFFileHeader64);
151
151
break ;
@@ -169,17 +169,19 @@ bool ObjectFileXCOFF::MagicBytesMatch(DataBufferSP &data_sp,
169
169
}
170
170
171
171
bool ObjectFileXCOFF::ParseHeader () {
172
- // Only 64-bit is supported for now
173
- return m_binary->fileHeader64 ()->Magic == XCOFF::XCOFF64;
172
+ if (m_binary->is64Bit ())
173
+ return m_binary->fileHeader64 ()->Magic == XCOFF::XCOFF64;
174
+ return m_binary->fileHeader32 ()->Magic == XCOFF::XCOFF32;
174
175
}
175
176
176
177
ByteOrder ObjectFileXCOFF::GetByteOrder () const { return eByteOrderBig; }
177
178
178
179
bool ObjectFileXCOFF::IsExecutable () const { return true ; }
179
180
180
181
uint32_t ObjectFileXCOFF::GetAddressByteSize () const {
181
- // 32-bit not supported. return 8 for 64-bit XCOFF::XCOFF64
182
- return 8 ;
182
+ if (m_binary->is64Bit ())
183
+ return 8 ;
184
+ return 4 ;
183
185
}
184
186
185
187
AddressClass ObjectFileXCOFF::GetAddressClass (addr_t file_addr) {
@@ -191,20 +193,37 @@ void ObjectFileXCOFF::ParseSymtab(Symtab &lldb_symtab) {}
191
193
bool ObjectFileXCOFF::IsStripped () { return false ; }
192
194
193
195
void ObjectFileXCOFF::CreateSections (SectionList &unified_section_list) {
196
+
194
197
if (m_sections_up)
195
198
return ;
196
199
197
200
m_sections_up = std::make_unique<SectionList>();
198
- ModuleSP module_sp (GetModule ());
201
+ if (m_binary->is64Bit ())
202
+ CreateSectionsWithBitness<XCOFF64>(unified_section_list);
203
+ else
204
+ CreateSectionsWithBitness<XCOFF32>(unified_section_list);
205
+ }
199
206
207
+ template <typename T>
208
+ static auto GetSections (llvm::object::XCOFFObjectFile *binary) {
209
+ if constexpr (T::Is64Bit)
210
+ return binary->sections64 ();
211
+ else
212
+ return binary->sections32 ();
213
+ }
214
+
215
+ template <typename T>
216
+ void ObjectFileXCOFF::CreateSectionsWithBitness (
217
+ SectionList &unified_section_list) {
218
+ ModuleSP module_sp (GetModule ());
200
219
if (!module_sp)
201
220
return ;
202
221
203
222
std::lock_guard<std::recursive_mutex> guard (module_sp->GetMutex ());
204
223
205
224
int idx = 0 ;
206
- for (const llvm::object::XCOFFSectionHeader64 §ion :
207
- m_binary-> sections64 ( )) {
225
+ for (const typename T::SectionHeader §ion :
226
+ GetSections<T>(m_binary. get () )) {
208
227
209
228
ConstString const_sect_name (section.Name );
210
229
@@ -253,9 +272,13 @@ UUID ObjectFileXCOFF::GetUUID() { return UUID(); }
253
272
uint32_t ObjectFileXCOFF::GetDependentModules (FileSpecList &files) { return 0 ; }
254
273
255
274
ObjectFile::Type ObjectFileXCOFF::CalculateType () {
256
- if (m_binary->fileHeader64 ()->Flags & XCOFF::F_EXEC)
275
+
276
+ const auto flags = m_binary->is64Bit () ? m_binary->fileHeader64 ()->Flags
277
+ : m_binary->fileHeader32 ()->Flags ;
278
+
279
+ if (flags & XCOFF::F_EXEC)
257
280
return eTypeExecutable;
258
- else if (m_binary-> fileHeader64 ()-> Flags & XCOFF::F_SHROBJ)
281
+ else if (flags & XCOFF::F_SHROBJ)
259
282
return eTypeSharedLibrary;
260
283
return eTypeUnknown;
261
284
}
0 commit comments