Skip to content

Commit c02e6ca

Browse files
[lldb][AIX] Added 32-bit XCOFF Executable support (#139875)
This PR is in reference to porting LLDB on AIX. Link to discussions on llvm discourse and github: 1. https://discourse.llvm.org/t/port-lldb-to-ibm-aix/80640 2. #101657 The complete changes for porting are present in this draft PR: #102601 **Description:** Adding support for XCOFF 32 bit file format as well in lldb, up to the point where 64-bit support is implemented. Added a new test case for the same. This is an incremental PR on top of the previous couple of XCOFF support commits.
1 parent 38e0f98 commit c02e6ca

File tree

3 files changed

+162
-17
lines changed

3 files changed

+162
-17
lines changed

lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ using namespace lldb;
3939
using namespace lldb_private;
4040

4141
LLDB_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

144144
static 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

171171
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;
174175
}
175176

176177
ByteOrder ObjectFileXCOFF::GetByteOrder() const { return eByteOrderBig; }
177178

178179
bool ObjectFileXCOFF::IsExecutable() const { return true; }
179180

180181
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;
183185
}
184186

185187
AddressClass ObjectFileXCOFF::GetAddressClass(addr_t file_addr) {
@@ -191,20 +193,37 @@ void ObjectFileXCOFF::ParseSymtab(Symtab &lldb_symtab) {}
191193
bool ObjectFileXCOFF::IsStripped() { return false; }
192194

193195
void 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 &section :
207-
m_binary->sections64()) {
225+
for (const typename T::SectionHeader &section :
226+
GetSections<T>(m_binary.get())) {
208227

209228
ConstString const_sect_name(section.Name);
210229

@@ -253,9 +272,13 @@ UUID ObjectFileXCOFF::GetUUID() { return UUID(); }
253272
uint32_t ObjectFileXCOFF::GetDependentModules(FileSpecList &files) { return 0; }
254273

255274
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)
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
}

lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ class ObjectFileXCOFF : public lldb_private::ObjectFile {
104104

105105
private:
106106
bool CreateBinary();
107+
template <typename T>
108+
void
109+
CreateSectionsWithBitness(lldb_private::SectionList &unified_section_list);
110+
111+
struct XCOFF32 {
112+
using SectionHeader = llvm::object::XCOFFSectionHeader32;
113+
static constexpr bool Is64Bit = false;
114+
};
115+
struct XCOFF64 {
116+
using SectionHeader = llvm::object::XCOFFSectionHeader64;
117+
static constexpr bool Is64Bit = true;
118+
};
107119

108120
std::unique_ptr<llvm::object::XCOFFObjectFile> m_binary;
109121
};
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# RUN: yaml2obj %s -o %t
2+
# RUN: lldb-test object-file %t | FileCheck %s
3+
4+
# CHECK: Plugin name: xcoff
5+
# CHECK: Architecture: powerpc64-ibm-aix
6+
# CHECK: Executable: true
7+
# CHECK: Stripped: false
8+
# CHECK: Type: executable
9+
# CHECK: Strata: unknown
10+
# CHECK: Name: .text
11+
# CHECK-NEXT: Type: code
12+
# CHECK-NEXT: Permissions: r-x
13+
# CHECK: Name: .data
14+
# CHECK-NEXT: Type: data
15+
# CHECK-NEXT: Permissions: rw-
16+
# CHECK: Name: .bss
17+
# CHECK-NEXT: Type: zero-fill
18+
# CHECK-NEXT: Permissions: rw-
19+
# CHECK: Name: .loader
20+
# CHECK-NEXT: Type: regular
21+
# CHECK-NEXT: Permissions: r--
22+
# CHECK: Name: .dwline
23+
# CHECK-NEXT: Type: dwarf-line
24+
# CHECK-NEXT: Permissions: r--
25+
# CHECK: Name: .dwinfo
26+
# CHECK-NEXT: Type: dwarf-info
27+
# CHECK-NEXT: Permissions: r--
28+
# CHECK: Name: .dwabrev
29+
# CHECK-NEXT: Type: dwarf-abbrev
30+
# CHECK-NEXT: Permissions: r--
31+
32+
--- !XCOFF
33+
FileHeader:
34+
MagicNumber: 0x1DF
35+
NumberOfSections: 7
36+
CreationTime: 000000000
37+
Flags: 0x1002
38+
Sections:
39+
- Name: .text
40+
Address: 0x10000268
41+
Size: 0x512
42+
FileOffsetToData: 0x268
43+
FileOffsetToRelocations: 0xECC
44+
FileOffsetToLineNumbers: 0x0
45+
NumberOfRelocations: 0x24
46+
NumberOfLineNumbers: 0x0
47+
Flags: [ STYP_TEXT ]
48+
SectionData: 80C20000
49+
- Name: .data
50+
Address: 0x2000077A
51+
Size: 0x242
52+
FileOffsetToData: 0x77A
53+
FileOffsetToRelocations: 0x1034
54+
FileOffsetToLineNumbers: 0x0
55+
NumberOfRelocations: 0x25
56+
NumberOfLineNumbers: 0x0
57+
Flags: [ STYP_DATA ]
58+
SectionData: ''
59+
- Name: .bss
60+
Address: 0x200009BC
61+
Size: 0x10
62+
FileOffsetToData: 0x0
63+
FileOffsetToRelocations: 0x0
64+
FileOffsetToLineNumbers: 0x0
65+
NumberOfRelocations: 0x0
66+
NumberOfLineNumbers: 0x0
67+
Flags: [ STYP_BSS ]
68+
SectionData: ''
69+
- Name: .loader
70+
Address: 0x0
71+
Size: 0x3A4
72+
FileOffsetToData: 0x9BC
73+
FileOffsetToRelocations: 0x0
74+
FileOffsetToLineNumbers: 0x0
75+
NumberOfRelocations: 0x0
76+
NumberOfLineNumbers: 0x0
77+
Flags: [ STYP_LOADER ]
78+
SectionData: 00000001
79+
- Name: .dwline
80+
Address: 0x0
81+
Size: 0x73
82+
FileOffsetToData: 0xD60
83+
FileOffsetToRelocations: 0x11A6
84+
FileOffsetToLineNumbers: 0x0
85+
NumberOfRelocations: 0x5
86+
NumberOfLineNumbers: 0x0
87+
Flags: [ STYP_DWARF ]
88+
SectionData: FFFFFFFF
89+
- Name: .dwinfo
90+
Address: 0x0
91+
Size: 0xB4
92+
FileOffsetToData: 0xDD4
93+
FileOffsetToRelocations: 0x11D8
94+
FileOffsetToLineNumbers: 0x0
95+
NumberOfRelocations: 0x6
96+
NumberOfLineNumbers: 0x0
97+
Flags: [ STYP_DWARF ]
98+
SectionData: FFFFFFFF
99+
- Name: .dwabrev
100+
Address: 0x0
101+
Size: 0x43
102+
FileOffsetToData: 0xE88
103+
FileOffsetToRelocations: 0x0
104+
FileOffsetToLineNumbers: 0x0
105+
NumberOfRelocations: 0x0
106+
NumberOfLineNumbers: 0x0
107+
Flags: [ STYP_DWARF ]
108+
SectionData: 01110125
109+
StringTable: {}
110+
...

0 commit comments

Comments
 (0)