Skip to content

Commit e324a3b

Browse files
Added comments and tests
1 parent 018b7dd commit e324a3b

File tree

2 files changed

+141
-24
lines changed

2 files changed

+141
-24
lines changed

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

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -188,78 +188,102 @@ AddressClass ObjectFileXCOFF::GetAddressClass(addr_t file_addr) {
188188
return AddressClass::eUnknown;
189189
}
190190

191-
lldb::SymbolType MapSymbolType(llvm::object::SymbolRef::Type sym_type) {
192-
if (sym_type == llvm::object::SymbolRef::ST_Function)
191+
static lldb::SymbolType MapSymbolType(llvm::object::SymbolRef::Type sym_type) {
192+
switch (sym_type) {
193+
case llvm::object::SymbolRef::ST_Function:
193194
return lldb::eSymbolTypeCode;
194-
else if (sym_type == llvm::object::SymbolRef::ST_Data)
195+
case llvm::object::SymbolRef::ST_Data:
195196
return lldb::eSymbolTypeData;
196-
else if (sym_type == llvm::object::SymbolRef::ST_File)
197+
case llvm::object::SymbolRef::ST_File:
197198
return lldb::eSymbolTypeSourceFile;
198-
return lldb::eSymbolTypeInvalid;
199+
default:
200+
return lldb::eSymbolTypeInvalid;
201+
}
199202
}
200203

201204
void ObjectFileXCOFF::ParseSymtab(Symtab &lldb_symtab) {
205+
Log *log = GetLog(LLDBLog::Object);
202206
SectionList *sectionList = GetSectionList();
203207

204208
for (const auto &symbol_ref : m_binary->symbols()) {
205209
llvm::object::XCOFFSymbolRef xcoff_sym_ref(symbol_ref);
210+
206211
llvm::Expected<llvm::StringRef> name_or_err = xcoff_sym_ref.getName();
207212
if (!name_or_err) {
208213
LLDB_LOG_ERROR(log, name_or_err.takeError(),
209214
"Unable to extract name from the xcoff symbol ref object");
210215
continue;
211216
}
217+
212218
llvm::StringRef symbolName = name_or_err.get();
213-
// Remove the dot prefix from symbol names.
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.
214223
llvm::StringRef name_no_dot =
215224
symbolName.starts_with(".") ? symbolName.drop_front() : symbolName;
216225
auto storageClass = xcoff_sym_ref.getStorageClass();
217-
// If its the hidden ext TOC symbol, add it directly,
218-
// for all other C_HIDEXT symbols, proceed to other checks.
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
219228
if (storageClass == XCOFF::C_HIDEXT && symbolName != "TOC") {
220-
// We do not need to add entries with 0 or >1 auxiliary data
229+
230+
// Zero or muliple aux entries may suggest ambiguous data
221231
if (xcoff_sym_ref.getNumberOfAuxEntries() != 1)
222232
continue;
233+
223234
auto aux_csect_or_err = xcoff_sym_ref.getXCOFFCsectAuxRef();
224235
if (!aux_csect_or_err) {
225236
LLDB_LOG_ERROR(log, aux_csect_or_err.takeError(),
226237
"Unable to access xcoff csect aux ref object");
227238
continue;
228239
}
240+
229241
const llvm::object::XCOFFCsectAuxRef csect_aux = aux_csect_or_err.get();
242+
230243
// Only add hidden ext entries which come under Program Code, skip others
244+
// as they are not useful as debugging data.
231245
if (csect_aux.getStorageMappingClass() != XCOFF::XMC_PR)
232246
continue;
247+
233248
// This does not apply to 32-bit,
234-
// Only add csect symbols identified by the aux entry, skip others
235-
if (m_binary->is64Bit() ? (csect_aux.getAuxType64() != XCOFF::AUX_CSECT))
236-
continue;
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;
237254
}
238255

239256
Symbol symbol;
240257
symbol.GetMangled().SetValue(ConstString(name_no_dot));
241258

242259
int16_t sectionNumber = xcoff_sym_ref.getSectionNumber();
260+
// Note that XCOFF section headers are numbered from 1 and not 0.
243261
size_t sectionIndex = static_cast<size_t>(sectionNumber - 1);
244-
if (sectionNumber > 0 && sectionIndex < sectionList->GetSize()) {
245-
lldb::SectionSP section_sp =
246-
sectionList->GetSectionAtIndex(sectionNumber - 1);
247-
if (!section_sp || section_sp->GetFileAddress() == LLDB_INVALID_ADDRESS)
248-
continue;
249-
lldb::addr_t file_addr = section_sp->GetFileAddress();
250-
lldb::addr_t symbolValue = xcoff_sym_ref.getValue();
251-
if (symbolValue < file_addr)
252-
continue;
253-
symbol.GetAddressRef() = Address(section_sp, symbolValue - file_addr);
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+
}
254277
}
255278

256279
Expected<llvm::object::SymbolRef::Type> sym_type_or_err =
257280
symbol_ref.getType();
258281
if (!sym_type_or_err) {
259-
LLDB_LOG_ERROR(log, aux_csect_or_err.takeError(),
282+
LLDB_LOG_ERROR(log, sym_type_or_err.takeError(),
260283
"Unable to access xcoff symbol type");
261284
continue;
262285
}
286+
263287
symbol.SetType(MapSymbolType(sym_type_or_err.get()));
264288

265289
lldb_symtab.AddSymbol(symbol);
@@ -315,7 +339,6 @@ void ObjectFileXCOFF::CreateSectionsWithBitness(
315339
.Case(".dwinfo", eSectionTypeDWARFDebugInfo)
316340
.Case(".dwline", eSectionTypeDWARFDebugLine)
317341
.Case(".dwabrev", eSectionTypeDWARFDebugAbbrev)
318-
.Case(".dwrnges", eSectionTypeDWARFDebugRanges)
319342
.Default(eSectionTypeInvalid);
320343
}
321344

lldb/test/Shell/ObjectFile/XCOFF/basic-info.yaml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
# CHECK-NEXT: Type: dwarf-abbrev
3030
# CHECK-NEXT: Permissions: r--
3131

32+
# RUN: %lldb %t -o "image dump symtab" -o exit | FileCheck %s --check-prefix=CHECK-SYMBOL
33+
# CHECK-SYMBOL:Index UserID DSX Type File Address/Value Load Address Size Flags Name
34+
# CHECK-SYMBOL:[ 0] 4294967295 Invalid 0xffffffffffffffff 0x0000000000000000 0x00000000 errno
35+
# CHECK-SYMBOL:[ 1] 4294967295 Code 0x0000000100000500 0x0000000000000398 0x00000000 __threads_init
36+
# CHECK-SYMBOL:[ 2] 4294967295 Data 0x0000000110000a70 0x0000000000000060 0x00000000 __threads_init
37+
# CHECK-SYMBOL:[ 3] 4294967295 Invalid 0x0000000110000ad0 0x00000000000000b0 0x00000000 TOC
38+
# CHECK-SYMBOL:[ 4] 4294967295 Invalid 0x0000000100000898 0x00000000100001d8 0x00000000 text
39+
# CHECK-SYMBOL:[ 5] 4294967295 Code 0x0000000100000898 0x00000000100001d8 0x00000000 main
40+
3241
--- !XCOFF
3342
FileHeader:
3443
MagicNumber: 0x1F7
@@ -104,5 +113,90 @@ Sections:
104113
NumberOfLineNumbers: 0x0
105114
Flags: [ STYP_DWARF ]
106115
SectionData: 01110125
116+
Symbols:
117+
- Name: errno
118+
Value: 0x0
119+
Section: N_UNDEF
120+
Type: 0x0
121+
StorageClass: C_EXT
122+
NumberOfAuxEntries: 1
123+
AuxEntries:
124+
- Type: AUX_CSECT
125+
ParameterHashIndex: 0
126+
TypeChkSectNum: 0
127+
SymbolAlignmentAndType: 0
128+
StorageMappingClass: XMC_RW
129+
SectionOrLengthLo: 0
130+
SectionOrLengthHi: 0
131+
- Name: .__threads_init
132+
Value: 0x100000500
133+
Section: .text
134+
Type: 0x20
135+
StorageClass: C_EXT
136+
NumberOfAuxEntries: 1
137+
AuxEntries:
138+
- Type: AUX_CSECT
139+
ParameterHashIndex: 0
140+
TypeChkSectNum: 0
141+
SymbolAlignmentAndType: 2
142+
StorageMappingClass: XMC_PR
143+
SectionOrLengthLo: 80
144+
SectionOrLengthHi: 0
145+
- Name: __threads_init
146+
Value: 0x110000A70
147+
Section: .data
148+
Type: 0x0
149+
StorageClass: C_EXT
150+
NumberOfAuxEntries: 1
151+
AuxEntries:
152+
- Type: AUX_CSECT
153+
ParameterHashIndex: 0
154+
TypeChkSectNum: 0
155+
SymbolAlignmentAndType: 25
156+
StorageMappingClass: XMC_DS
157+
SectionOrLengthLo: 24
158+
SectionOrLengthHi: 0
159+
- Name: TOC
160+
Value: 0x110000AD0
161+
Section: .data
162+
Type: 0x0
163+
StorageClass: C_HIDEXT
164+
NumberOfAuxEntries: 1
165+
AuxEntries:
166+
- Type: AUX_CSECT
167+
ParameterHashIndex: 0
168+
TypeChkSectNum: 0
169+
SymbolAlignmentAndType: 25
170+
StorageMappingClass: XMC_TC0
171+
SectionOrLengthLo: 0
172+
SectionOrLengthHi: 0
173+
- Name: .text
174+
Value: 0x100000898
175+
Section: .text
176+
Type: 0x0
177+
StorageClass: C_HIDEXT
178+
NumberOfAuxEntries: 1
179+
AuxEntries:
180+
- Type: AUX_CSECT
181+
ParameterHashIndex: 0
182+
TypeChkSectNum: 0
183+
SymbolAlignmentAndType: 17
184+
StorageMappingClass: XMC_PR
185+
SectionOrLengthLo: 58
186+
SectionOrLengthHi: 0
187+
- Name: .main
188+
Value: 0x100000898
189+
Section: .text
190+
Type: 0x0
191+
StorageClass: C_EXT
192+
NumberOfAuxEntries: 1
193+
AuxEntries:
194+
- Type: AUX_CSECT
195+
ParameterHashIndex: 0
196+
TypeChkSectNum: 0
197+
SymbolAlignmentAndType: 2
198+
StorageMappingClass: XMC_PR
199+
SectionOrLengthLo: 135
200+
SectionOrLengthHi: 0
107201
StringTable: {}
108202
...

0 commit comments

Comments
 (0)