Skip to content

Commit 47005a3

Browse files
DhruvSrivastavaXJaddyen
authored andcommitted
[lldb][AIX] Added XCOFF ParseSymtab handling (llvm#141577)
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. llvm#101657 The complete changes for porting are present in this draft PR: llvm#102601 **Description:** Adding ParseSymtab logic after creating sections. It is able to handle both 32 and 64 bit symbols, without the need to add template logic. This is an incremental PR on top of my previous couple of XCOFF support commits.
1 parent 4159474 commit 47005a3

File tree

3 files changed

+346
-1
lines changed

3 files changed

+346
-1
lines changed

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

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,107 @@ AddressClass ObjectFileXCOFF::GetAddressClass(addr_t file_addr) {
188188
return AddressClass::eUnknown;
189189
}
190190

191-
void ObjectFileXCOFF::ParseSymtab(Symtab &lldb_symtab) {}
191+
static lldb::SymbolType MapSymbolType(llvm::object::SymbolRef::Type sym_type) {
192+
switch (sym_type) {
193+
case llvm::object::SymbolRef::ST_Function:
194+
return lldb::eSymbolTypeCode;
195+
case llvm::object::SymbolRef::ST_Data:
196+
return lldb::eSymbolTypeData;
197+
case llvm::object::SymbolRef::ST_File:
198+
return lldb::eSymbolTypeSourceFile;
199+
default:
200+
return lldb::eSymbolTypeInvalid;
201+
}
202+
}
203+
204+
void ObjectFileXCOFF::ParseSymtab(Symtab &lldb_symtab) {
205+
Log *log = GetLog(LLDBLog::Object);
206+
SectionList *sectionList = GetSectionList();
207+
208+
for (const auto &symbol_ref : m_binary->symbols()) {
209+
llvm::object::XCOFFSymbolRef xcoff_sym_ref(symbol_ref);
210+
211+
llvm::Expected<llvm::StringRef> name_or_err = xcoff_sym_ref.getName();
212+
if (!name_or_err) {
213+
LLDB_LOG_ERROR(log, name_or_err.takeError(),
214+
"Unable to extract name from the xcoff symbol ref object");
215+
continue;
216+
}
217+
218+
llvm::StringRef symbolName = name_or_err.get();
219+
// Remove the . prefix added during compilation. This prefix is usually
220+
// added to differentiate between reference to the code and function
221+
// descriptor. For instance, Adding .func will only allow user to put bp on
222+
// .func, which is not known to the user, instead of func.
223+
llvm::StringRef name_no_dot =
224+
symbolName.starts_with(".") ? symbolName.drop_front() : symbolName;
225+
auto storageClass = xcoff_sym_ref.getStorageClass();
226+
// C_HIDEXT symbols are not needed to be exposed, with the exception of TOC
227+
// which is responsible for storing references to global data
228+
if (storageClass == XCOFF::C_HIDEXT && symbolName != "TOC") {
229+
230+
// Zero or muliple aux entries may suggest ambiguous data
231+
if (xcoff_sym_ref.getNumberOfAuxEntries() != 1)
232+
continue;
233+
234+
auto aux_csect_or_err = xcoff_sym_ref.getXCOFFCsectAuxRef();
235+
if (!aux_csect_or_err) {
236+
LLDB_LOG_ERROR(log, aux_csect_or_err.takeError(),
237+
"Unable to access xcoff csect aux ref object");
238+
continue;
239+
}
240+
241+
const llvm::object::XCOFFCsectAuxRef csect_aux = aux_csect_or_err.get();
242+
243+
// Only add hidden ext entries which come under Program Code, skip others
244+
// as they are not useful as debugging data.
245+
if (csect_aux.getStorageMappingClass() != XCOFF::XMC_PR)
246+
continue;
247+
248+
// This does not apply to 32-bit,
249+
// Only add csect symbols identified by the aux entry, as they are
250+
// needed to reference section information. Skip others
251+
if (m_binary->is64Bit())
252+
if (csect_aux.getAuxType64() != XCOFF::AUX_CSECT)
253+
continue;
254+
}
255+
256+
Symbol symbol;
257+
symbol.GetMangled().SetValue(ConstString(name_no_dot));
258+
259+
int16_t sectionNumber = xcoff_sym_ref.getSectionNumber();
260+
// Note that XCOFF section headers are numbered from 1 and not 0.
261+
size_t sectionIndex = static_cast<size_t>(sectionNumber - 1);
262+
if (sectionNumber > 0) {
263+
if (sectionIndex < sectionList->GetSize()) {
264+
265+
lldb::SectionSP section_sp =
266+
sectionList->GetSectionAtIndex(sectionIndex);
267+
if (!section_sp || section_sp->GetFileAddress() == LLDB_INVALID_ADDRESS)
268+
continue;
269+
270+
lldb::addr_t file_addr = section_sp->GetFileAddress();
271+
lldb::addr_t symbolValue = xcoff_sym_ref.getValue();
272+
if (symbolValue < file_addr)
273+
continue;
274+
275+
symbol.GetAddressRef() = Address(section_sp, symbolValue - file_addr);
276+
}
277+
}
278+
279+
Expected<llvm::object::SymbolRef::Type> sym_type_or_err =
280+
symbol_ref.getType();
281+
if (!sym_type_or_err) {
282+
LLDB_LOG_ERROR(log, sym_type_or_err.takeError(),
283+
"Unable to access xcoff symbol type");
284+
continue;
285+
}
286+
287+
symbol.SetType(MapSymbolType(sym_type_or_err.get()));
288+
289+
lldb_symtab.AddSymbol(symbol);
290+
}
291+
}
192292

193293
bool ObjectFileXCOFF::IsStripped() { return false; }
194294

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# RUN: yaml2obj %s -o %t
2+
# RUN: %lldb %t -o "image dump symtab" -o exit | FileCheck %s
3+
# CHECK: Index UserID DSX Type File Address/Value Load Address Size Flags Name
4+
# CHECK: [ 0] 4294967295 Invalid 0xffffffffffffffff 0x0000000000000000 0x00000000 errno
5+
# CHECK: [ 1] 4294967295 Code 0x0000000100000500 0x0000000000000398 0x00000000 __threads_init
6+
# CHECK: [ 2] 4294967295 Data 0x0000000110000a70 0x0000000000000060 0x00000000 __threads_init
7+
# CHECK: [ 3] 4294967295 Invalid 0x0000000110000ad0 0x00000000000000b0 0x00000000 TOC
8+
# CHECK: [ 4] 4294967295 Invalid 0x0000000100000898 0x00000000100001d8 0x00000000 text
9+
# CHECK: [ 5] 4294967295 Code 0x0000000100000898 0x00000000100001d8 0x00000000 main
10+
11+
--- !XCOFF
12+
FileHeader:
13+
MagicNumber: 0x1F7
14+
NumberOfSections: 2
15+
CreationTime: 000000000
16+
Flags: 0x0002
17+
Sections:
18+
- Name: .text
19+
Address: 0x100000438
20+
Size: 0x38
21+
FileOffsetToData: 0x0
22+
FileOffsetToLineNumbers: 0x0
23+
NumberOfLineNumbers: 0x0
24+
Flags: [ STYP_TEXT ]
25+
SectionData: E8C20000
26+
- Name: .data
27+
Address: 0x1100008D2
28+
Size: 0x2AE
29+
FileOffsetToData: 0x8D2
30+
FileOffsetToRelocations: 0x132E
31+
FileOffsetToLineNumbers: 0x0
32+
NumberOfRelocations: 0x22
33+
NumberOfLineNumbers: 0x0
34+
Flags: [ STYP_DATA ]
35+
SectionData: ''
36+
Symbols:
37+
- Name: errno
38+
Value: 0x0
39+
Section: N_UNDEF
40+
Type: 0x0
41+
StorageClass: C_EXT
42+
NumberOfAuxEntries: 1
43+
AuxEntries:
44+
- Type: AUX_CSECT
45+
ParameterHashIndex: 0
46+
TypeChkSectNum: 0
47+
SymbolAlignmentAndType: 0
48+
StorageMappingClass: XMC_RW
49+
SectionOrLengthLo: 0
50+
SectionOrLengthHi: 0
51+
- Name: .__threads_init
52+
Value: 0x100000500
53+
Section: .text
54+
Type: 0x20
55+
StorageClass: C_EXT
56+
NumberOfAuxEntries: 1
57+
AuxEntries:
58+
- Type: AUX_CSECT
59+
ParameterHashIndex: 0
60+
TypeChkSectNum: 0
61+
SymbolAlignmentAndType: 2
62+
StorageMappingClass: XMC_PR
63+
SectionOrLengthLo: 80
64+
SectionOrLengthHi: 0
65+
- Name: __threads_init
66+
Value: 0x110000A70
67+
Section: .data
68+
Type: 0x0
69+
StorageClass: C_EXT
70+
NumberOfAuxEntries: 1
71+
AuxEntries:
72+
- Type: AUX_CSECT
73+
ParameterHashIndex: 0
74+
TypeChkSectNum: 0
75+
SymbolAlignmentAndType: 25
76+
StorageMappingClass: XMC_DS
77+
SectionOrLengthLo: 24
78+
SectionOrLengthHi: 0
79+
- Name: TOC
80+
Value: 0x110000AD0
81+
Section: .data
82+
Type: 0x0
83+
StorageClass: C_HIDEXT
84+
NumberOfAuxEntries: 1
85+
AuxEntries:
86+
- Type: AUX_CSECT
87+
ParameterHashIndex: 0
88+
TypeChkSectNum: 0
89+
SymbolAlignmentAndType: 25
90+
StorageMappingClass: XMC_TC0
91+
SectionOrLengthLo: 0
92+
SectionOrLengthHi: 0
93+
- Name: .text
94+
Value: 0x100000898
95+
Section: .text
96+
Type: 0x0
97+
StorageClass: C_HIDEXT
98+
NumberOfAuxEntries: 1
99+
AuxEntries:
100+
- Type: AUX_CSECT
101+
ParameterHashIndex: 0
102+
TypeChkSectNum: 0
103+
SymbolAlignmentAndType: 17
104+
StorageMappingClass: XMC_PR
105+
SectionOrLengthLo: 58
106+
SectionOrLengthHi: 0
107+
- Name: .main
108+
Value: 0x100000898
109+
Section: .text
110+
Type: 0x0
111+
StorageClass: C_EXT
112+
NumberOfAuxEntries: 1
113+
AuxEntries:
114+
- Type: AUX_CSECT
115+
ParameterHashIndex: 0
116+
TypeChkSectNum: 0
117+
SymbolAlignmentAndType: 2
118+
StorageMappingClass: XMC_PR
119+
SectionOrLengthLo: 135
120+
SectionOrLengthHi: 0
121+
...
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# RUN: yaml2obj %s -o %t
2+
# RUN: %lldb %t -o "image dump symtab" -o exit | FileCheck %s
3+
# CHECK: Index UserID DSX Type File Address/Value Load Address Size Flags Name
4+
# CHECK: [ 0] 4294967295 Invalid 0xffffffffffffffff 0x0000000000000000 0x00000000 errno
5+
# CHECK: [ 1] 4294967295 Code 0x0000000010000320 0x0000000000000420 0x00000000 __threads_init
6+
# CHECK: [ 2] 4294967295 Data 0x0000000020000920 0x000000000000003c 0x00000000 __threads_init
7+
# CHECK: [ 3] 4294967295 Invalid 0x000000002000095c 0x0000000000000060 0x00000000 TOC
8+
# CHECK: [ 4] 4294967295 Invalid 0x0000000010000740 0x000000000000003a 0x00000000 text
9+
# CHECK: [ 5] 4294967295 Invalid 0x0000000010000740 0x000000000000003a 0x00000000 main
10+
11+
--- !XCOFF
12+
FileHeader:
13+
MagicNumber: 0x1DF
14+
NumberOfSections: 2
15+
CreationTime: 000000000
16+
Flags: 0x1002
17+
Sections:
18+
- Name: .text
19+
Address: 0x10000268
20+
Size: 0x512
21+
FileOffsetToData: 0x268
22+
FileOffsetToRelocations: 0xECC
23+
FileOffsetToLineNumbers: 0x0
24+
NumberOfRelocations: 0x24
25+
NumberOfLineNumbers: 0x0
26+
Flags: [ STYP_TEXT ]
27+
SectionData: 80C20000
28+
- Name: .data
29+
Address: 0x2000077A
30+
Size: 0x242
31+
FileOffsetToData: 0x77A
32+
FileOffsetToRelocations: 0x1034
33+
FileOffsetToLineNumbers: 0x0
34+
NumberOfRelocations: 0x25
35+
NumberOfLineNumbers: 0x0
36+
Flags: [ STYP_DATA ]
37+
SectionData: ''
38+
Symbols:
39+
- Name: errno
40+
Value: 0x0
41+
Section: N_UNDEF
42+
Type: 0x0
43+
StorageClass: C_EXT
44+
NumberOfAuxEntries: 1
45+
AuxEntries:
46+
- Type: AUX_CSECT
47+
ParameterHashIndex: 0
48+
TypeChkSectNum: 0
49+
StorageMappingClass: XMC_RW
50+
SectionOrLength: 0
51+
StabInfoIndex: 0
52+
StabSectNum: 0
53+
- Name: .__threads_init
54+
Value: 0x10000320
55+
Section: .text
56+
Type: 0x20
57+
StorageClass: C_EXT
58+
NumberOfAuxEntries: 1
59+
AuxEntries:
60+
- Type: AUX_CSECT
61+
ParameterHashIndex: 0
62+
TypeChkSectNum: 0
63+
StorageMappingClass: XMC_PR
64+
SectionOrLength: 84
65+
StabInfoIndex: 0
66+
StabSectNum: 0
67+
- Name: __threads_init
68+
Value: 0x20000920
69+
Section: .data
70+
Type: 0x0
71+
StorageClass: C_EXT
72+
NumberOfAuxEntries: 1
73+
AuxEntries:
74+
- Type: AUX_CSECT
75+
ParameterHashIndex: 0
76+
TypeChkSectNum: 0
77+
StorageMappingClass: XMC_DS
78+
SectionOrLength: 12
79+
StabInfoIndex: 0
80+
StabSectNum: 0
81+
- Name: TOC
82+
Value: 0x2000095C
83+
Section: .data
84+
Type: 0x0
85+
StorageClass: C_HIDEXT
86+
NumberOfAuxEntries: 1
87+
AuxEntries:
88+
- Type: AUX_CSECT
89+
ParameterHashIndex: 0
90+
TypeChkSectNum: 0
91+
StorageMappingClass: XMC_TC0
92+
SectionOrLength: 0
93+
StabInfoIndex: 0
94+
StabSectNum: 0
95+
- Name: .text
96+
Value: 0x10000740
97+
Section: .text
98+
Type: 0x0
99+
StorageClass: C_HIDEXT
100+
NumberOfAuxEntries: 1
101+
AuxEntries:
102+
- Type: AUX_CSECT
103+
ParameterHashIndex: 0
104+
TypeChkSectNum: 0
105+
StorageMappingClass: XMC_PR
106+
SectionOrLength: 58
107+
StabInfoIndex: 0
108+
StabSectNum: 0
109+
- Name: .main
110+
Value: 0x10000740
111+
Section: .text
112+
Type: 0x0
113+
StorageClass: C_EXT
114+
NumberOfAuxEntries: 1
115+
AuxEntries:
116+
- Type: AUX_CSECT
117+
ParameterHashIndex: 0
118+
TypeChkSectNum: 0
119+
StorageMappingClass: XMC_PR
120+
SectionOrLength: 137
121+
StabInfoIndex: 0
122+
StabSectNum: 0
123+
124+
...

0 commit comments

Comments
 (0)