@@ -39,7 +39,6 @@ using namespace lldb;
3939using namespace lldb_private ;
4040
4141LLDB_PLUGIN_DEFINE (ObjectFileXCOFF)
42-
4342// FIXME: target 64bit at this moment.
4443
4544// Static methods.
@@ -95,10 +94,11 @@ bool ObjectFileXCOFF::CreateBinary() {
9594
9695 Log *log = GetLog (LLDBLog::Object);
9796
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);
102102 if (!binary) {
103103 LLDB_LOG_ERROR (log, binary.takeError (),
104104 " Failed to create binary for file ({1}): {0}" , m_file);
@@ -143,9 +143,9 @@ size_t ObjectFileXCOFF::GetModuleSpecifications(
143143
144144static uint32_t XCOFFHeaderSizeFromMagic (uint32_t magic) {
145145 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 ;
149149 case XCOFF::XCOFF64:
150150 return sizeof (struct llvm ::object::XCOFFFileHeader64);
151151 break ;
@@ -169,17 +169,19 @@ bool ObjectFileXCOFF::MagicBytesMatch(DataBufferSP &data_sp,
169169}
170170
171171bool 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;
174175}
175176
176177ByteOrder ObjectFileXCOFF::GetByteOrder () const { return eByteOrderBig; }
177178
178179bool ObjectFileXCOFF::IsExecutable () const { return true ; }
179180
180181uint32_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 ;
183185}
184186
185187AddressClass ObjectFileXCOFF::GetAddressClass (addr_t file_addr) {
@@ -191,20 +193,37 @@ void ObjectFileXCOFF::ParseSymtab(Symtab &lldb_symtab) {}
191193bool ObjectFileXCOFF::IsStripped () { return false ; }
192194
193195void ObjectFileXCOFF::CreateSections (SectionList &unified_section_list) {
196+
194197 if (m_sections_up)
195198 return ;
196199
197200 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+ }
199206
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 ());
200219 if (!module_sp)
201220 return ;
202221
203222 std::lock_guard<std::recursive_mutex> guard (module_sp->GetMutex ());
204223
205224 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 () )) {
208227
209228 ConstString const_sect_name (section.Name );
210229
@@ -253,9 +272,13 @@ UUID ObjectFileXCOFF::GetUUID() { return UUID(); }
253272uint32_t ObjectFileXCOFF::GetDependentModules (FileSpecList &files) { return 0 ; }
254273
255274ObjectFile::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)
257280 return eTypeExecutable;
258- else if (m_binary-> fileHeader64 ()-> Flags & XCOFF::F_SHROBJ)
281+ else if (flags & XCOFF::F_SHROBJ)
259282 return eTypeSharedLibrary;
260283 return eTypeUnknown;
261284}
0 commit comments