This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objdump] Use a counter for llvm-objdump -h instead of the section index.
ClosedPublic

Authored by rupprecht on Oct 10 2019, 6:12 PM.

Details

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.

Event Timeline

rupprecht created this revision.Oct 10 2019, 6:12 PM
rupprecht updated this revision to Diff 224519.Oct 10 2019, 6:15 PM

Rebase against D68730

Harbormaster completed remote builds in B39385: Diff 224519.

A few ideas/suggestions.

llvm/tools/llvm-objdump/llvm-objdump.cpp
345–358

It's common to wrap such things (helper types) into anonymous namespaces:
I think you can also add a helper function too:

namespace {
struct FilterResult {
  bool Keep;
  bool IncrementIndex;
};

FilterResult checkSectionFilter(object::SectionRef S) {
...
}
};
347

I'd comment these fields. It wasn't obvious to me what they are used for.

360

I think we use a full variable name usually, i.e. Increment -> IncrementIndex

383

nit: if (Idx) would be shorer.

387

Please avoid using auto for return types that are not obvious.

1708

Looking at this,
should ToolSectionFilter just return [(SectionRef&)Ref, (uint64_t )Index] struct/pair instead? It seems could make the whole logic simper.

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

I think this needs a full comment (it does not give an information about what this helper avtually do atm):

I.e. would be nice to see something like:

// Function is used to ...
// Idx is a optional output parameter that ...
rupprecht marked 9 inline comments as done.
  • Add/update several comments
  • Use simplified pointer check
  • Avoid auto as the returned type is not obvious

Rebase against D68730 again

rupprecht added inline comments.Oct 11 2019, 10:41 AM
llvm/tools/llvm-objdump/llvm-objdump.cpp
345–358
360

Sorry, I originally had the variable named Increment but forgot to update these comments.

1708

That's one of the paths I considered while writing this patch. A couple thoughts:

  1. This is the only place that needs this counter value, and loop iteration in every other case (7 other places in this file, 1 in the MachO dumper) would now be a little more complicated even when callers don't care about the index, e.g. it would now be:
for (const SomeWrapperType &Foo : ToolSectionFilter(*Obj)) {
  const SectionRef &Section = Foo.Section;
  1. llvm has make_filter_range which probably did not exist at the time this code was first written, and it would be nice to completely remove SectionFilter and SectionFilterIterator from llvm-objdump.h in favor of those standard llvm libraries, but AIUI in order to use that, the return type would need to be SectionRef, not some wrapper type. (I'm trying to do that in a separate branch, but I'm dealing with template woes).
  2. But on the plus side, it does avoid out parameters which would be very nice.

I think 1&2 outweigh 3, so I'm leaning towards this approach. But I'm still exploring alternatives.

Harbormaster completed remote builds in B39427: Diff 224633.
grimar added inline comments.Oct 12 2019, 5:20 AM
llvm/tools/llvm-objdump/llvm-objdump.cpp
376

Can we have a test for this logic? I.e. for a case when Keep=false, IncrementIndex=true.
(Doesn't seem we have it)

1708

I see. I have no much better/different ideas atm. Perhaps the current approach is OK for now.

rupprecht marked 3 inline comments as done.Oct 14 2019, 10:00 AM
rupprecht added inline comments.
llvm/tools/llvm-objdump/llvm-objdump.cpp
376

This is already covered by llvm/test/tools/llvm-objdump/X86/section-index.s:

# RUN: llvm-objdump -section-headers %t | FileCheck %s
...
# CHECK-NEXT: 4  .bar

# RUN: llvm-objdump -section-headers -section=.bar %t \
# RUN:   | FileCheck %s --check-prefix=BAR
...
# BAR-NEXT:  4  .bar

I'll mention this in the patch description.

rupprecht edited the summary of this revision. (Show Details)Oct 14 2019, 10:02 AM
grimar accepted this revision.Oct 15 2019, 12:16 AM

LGTM

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

Ah, OK.

This revision is now accepted and ready to land.Oct 15 2019, 12:16 AM
rupprecht marked an inline comment as done.Oct 15 2019, 10:07 AM

Thanks for the review!

This revision was automatically updated to reflect the committed changes.