Skip to content

Commit eb501b1

Browse files
committed
[llvm-objdump] Use a counter for llvm-objdump -h instead of the section index.
Summary: When listing the index in `llvm-objdump -h`, use a zero-based counter instead of the actual section index (e.g. shdr->sh_index for ELF). While this is effectively a noop for now (except one unit test for XCOFF), the index values will change in a future patch that filters certain sections out (e.g. symbol tables). See D68669 for more context. Note: the test case in `test/tools/llvm-objdump/X86/section-index.s` already covers the case of incrementing the section index counter when sections are skipped. Reviewers: grimar, jhenderson, espindola Reviewed By: grimar Subscribers: emaste, sbc100, arichardson, aheejin, arphaman, seiya, llvm-commits, MaskRay Tags: #llvm Differential Revision: https://reviews.llvm.org/D68848 llvm-svn: 374931
1 parent 621ce37 commit eb501b1

File tree

3 files changed

+63
-22
lines changed

3 files changed

+63
-22
lines changed

llvm/test/tools/llvm-objdump/xcoff-section-headers.test

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
# CHECK: xcoff-section-headers.o: file format aixcoff-rs6000
1414
# CHECK: Sections:
1515
# CHECK: Idx Name Size VMA Type
16-
# CHECK: 1 .text 00000080 00000000 TEXT
17-
# CHECK: 2 .data 00000024 00000080 DATA
18-
# CHECK: 3 .bss 00000004 000000a4 BSS
19-
# CHECK: 4 .tdata 00000008 00000000 DATA
20-
# CHECK: 5 .tbss 00000004 00000008 BSS
16+
# CHECK: 0 .text 00000080 00000000 TEXT
17+
# CHECK: 1 .data 00000024 00000080 DATA
18+
# CHECK: 2 .bss 00000004 000000a4 BSS
19+
# CHECK: 3 .tdata 00000008 00000000 DATA
20+
# CHECK: 4 .tbss 00000004 00000008 BSS
2121

2222
# xcoff-section-headers.o Compiled with IBM XL C/C++ for AIX, V16.1.0
2323
# test.c:
@@ -32,10 +32,10 @@
3232

3333
# LONG: xcoff-long-sec-names.o: file format aixcoff-rs6000
3434
# LONG: Sections:
35-
# LONG: Idx Name Size VMA Type
36-
# LONG: 1 .dwarnge 00000004 00000000
37-
# LONG: 2 .dwpbnms 00000004 00000000
38-
# LONG: 3 .dwpbtyp 00000004 00000000
35+
# LONG: Idx Name Size VMA Type
36+
# LONG: 0 .dwarnge 00000004 00000000
37+
# LONG: 1 .dwpbnms 00000004 00000000
38+
# LONG: 2 .dwpbtyp 00000004 00000000
3939

4040
# xcoff-long-sec-names.o was generated by assembling the following .s file:
4141
# .dwsect 0x30000 # .dwpbnms section

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -342,26 +342,54 @@ static StringRef ToolName;
342342

343343
typedef std::vector<std::tuple<uint64_t, StringRef, uint8_t>> SectionSymbolsTy;
344344

345-
static bool shouldKeep(object::SectionRef S) {
345+
namespace {
346+
struct FilterResult {
347+
// True if the section should not be skipped.
348+
bool Keep;
349+
350+
// True if the index counter should be incremented, even if the section should
351+
// be skipped. For example, sections may be skipped if they are not included
352+
// in the --section flag, but we still want those to count toward the section
353+
// count.
354+
bool IncrementIndex;
355+
};
356+
} // namespace
357+
358+
static FilterResult checkSectionFilter(object::SectionRef S) {
346359
if (FilterSections.empty())
347-
return true;
360+
return {/*Keep=*/true, /*IncrementIndex=*/true};
348361

349362
Expected<StringRef> SecNameOrErr = S.getName();
350363
if (!SecNameOrErr) {
351364
consumeError(SecNameOrErr.takeError());
352-
return false;
365+
return {/*Keep=*/false, /*IncrementIndex=*/false};
353366
}
354367
StringRef SecName = *SecNameOrErr;
355368

356369
// StringSet does not allow empty key so avoid adding sections with
357370
// no name (such as the section with index 0) here.
358371
if (!SecName.empty())
359372
FoundSectionSet.insert(SecName);
360-
return is_contained(FilterSections, SecName);
373+
374+
// Only show the section if it's in the FilterSections list, but always
375+
// increment so the indexing is stable.
376+
return {/*Keep=*/is_contained(FilterSections, SecName),
377+
/*IncrementIndex=*/true};
361378
}
362379

363-
SectionFilter ToolSectionFilter(object::ObjectFile const &O) {
364-
return SectionFilter([](object::SectionRef S) { return shouldKeep(S); }, O);
380+
SectionFilter ToolSectionFilter(object::ObjectFile const &O, uint64_t *Idx) {
381+
// Start at UINT64_MAX so that the first index returned after an increment is
382+
// zero (after the unsigned wrap).
383+
if (Idx)
384+
*Idx = UINT64_MAX;
385+
return SectionFilter(
386+
[Idx](object::SectionRef S) {
387+
FilterResult Result = checkSectionFilter(S);
388+
if (Idx != nullptr && Result.IncrementIndex)
389+
*Idx += 1;
390+
return Result.Keep;
391+
},
392+
O);
365393
}
366394

367395
std::string getFileNameForError(const object::Archive::Child &C,
@@ -967,7 +995,7 @@ getRelocsMap(object::ObjectFile const &Obj) {
967995
std::map<SectionRef, std::vector<RelocationRef>> Ret;
968996
for (SectionRef Sec : Obj.sections()) {
969997
section_iterator Relocated = Sec.getRelocatedSection();
970-
if (Relocated == Obj.section_end() || !shouldKeep(*Relocated))
998+
if (Relocated == Obj.section_end() || !checkSectionFilter(*Relocated).Keep)
971999
continue;
9721000
std::vector<RelocationRef> &V = Ret[*Relocated];
9731001
for (const RelocationRef &R : Sec.relocations())
@@ -1676,7 +1704,8 @@ void printSectionHeaders(const ObjectFile *Obj) {
16761704
<< left_justify("Name", NameWidth) << " Size "
16771705
<< left_justify("VMA", AddressWidth) << " Type\n";
16781706

1679-
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1707+
uint64_t Idx;
1708+
for (const SectionRef &Section : ToolSectionFilter(*Obj, &Idx)) {
16801709
StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName());
16811710
uint64_t VMA = Section.getAddress();
16821711
if (shouldAdjustVA(Section))
@@ -1691,14 +1720,14 @@ void printSectionHeaders(const ObjectFile *Obj) {
16911720
Type += Type.empty() ? "BSS" : " BSS";
16921721

16931722
if (HasLMAColumn)
1694-
outs() << format("%3d %-*s %08" PRIx64 " ", (unsigned)Section.getIndex(),
1695-
NameWidth, Name.str().c_str(), Size)
1723+
outs() << format("%3d %-*s %08" PRIx64 " ", Idx, NameWidth,
1724+
Name.str().c_str(), Size)
16961725
<< format_hex_no_prefix(VMA, AddressWidth) << " "
16971726
<< format_hex_no_prefix(getELFSectionLMA(Section), AddressWidth)
16981727
<< " " << Type << "\n";
16991728
else
1700-
outs() << format("%3d %-*s %08" PRIx64 " ", (unsigned)Section.getIndex(),
1701-
NameWidth, Name.str().c_str(), Size)
1729+
outs() << format("%3d %-*s %08" PRIx64 " ", Idx, NameWidth,
1730+
Name.str().c_str(), Size)
17021731
<< format_hex_no_prefix(VMA, AddressWidth) << " " << Type << "\n";
17031732
}
17041733
outs() << "\n";

llvm/tools/llvm-objdump/llvm-objdump.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ extern cl::opt<bool> Demangle;
3131

3232
typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
3333

34+
/// A filtered iterator for SectionRefs that skips sections based on some given
35+
/// predicate.
3436
class SectionFilterIterator {
3537
public:
3638
SectionFilterIterator(FilterPredicate P,
@@ -60,6 +62,8 @@ class SectionFilterIterator {
6062
llvm::object::section_iterator End;
6163
};
6264

65+
/// Creates an iterator range of SectionFilterIterators for a given Object and
66+
/// predicate.
6367
class SectionFilter {
6468
public:
6569
SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
@@ -79,7 +83,15 @@ class SectionFilter {
7983
};
8084

8185
// Various helper functions.
82-
SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O);
86+
87+
/// Creates a SectionFilter with a standard predicate that conditionally skips
88+
/// sections when the --section objdump flag is provided.
89+
///
90+
/// Idx is an optional output parameter that keeps track of which section index
91+
/// this is. This may be different than the actual section number, as some
92+
/// sections may be filtered (e.g. symbol tables).
93+
SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O,
94+
uint64_t *Idx = nullptr);
8395

8496
Error getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
8597
const object::RelocationRef &Rel,

0 commit comments

Comments
 (0)