Bug 30684 - readelf -s: support an option to display section names
Summary: readelf -s: support an option to display section names
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Nick Clifton
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-07-25 18:23 UTC by Fangrui Song
Modified: 2023-09-05 10:11 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
Proposed patch (5.25 KB, patch)
2023-07-26 10:47 UTC, Nick Clifton
Details | Diff
Proposed patch (16.34 KB, patch)
2023-07-27 13:49 UTC, Nick Clifton
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2023-07-25 18:23:54 UTC
I almost always prefer readelf -Ws to objdump -t for ELF symbol information as the former presents all information without distortion. Certain symbol types have strange output for objdump -t output, e.g. STT_SECTION=>'d', STT_NOTYPE=>no particular character.

There is, however, one feature that objdump -t is better: it displays the section name for a symbol defined relative to a section. readelf -Ws just prints st_shndx, which is sometimes less readable.

I wonder whether readelf can add an option to change the "Ndx" column to display section names instead.
Comment 1 Nick Clifton 2023-07-26 08:46:08 UTC
Hi Fanguri,

  I like the idea, although I am not sure of the format the output should take.
  I tried a small experiment and the output looked like this (pruned to just 
  show some interesting cases):

     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND    
     1: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 .text
     2: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crt1.o
     3: 0000000000405fa6     0 NOTYPE  LOCAL  HIDDEN    14 (.text) .annobin_abi_note.c
    12: 000000000040039c    32 OBJECT  LOCAL  DEFAULT    4 (.note.ABI-tag) __abi_tag
    40: 0000000000423ea0    19 OBJECT  LOCAL  DEFAULT   16 (.rodata) __PRETTY_FUNCTION__.1
 
  The problem is that I wanted to associate the section names with the section
  indexes, where the index is relevant, but this results in the symbol names
  being mis-indented and the output looking messy.

  Do you have any suggestions for a better format ?

Cheers
  Nick
Comment 2 Nick Clifton 2023-07-26 10:47:20 UTC
Created attachment 15012 [details]
Proposed patch

Have a play with this patch and let me know what you think.

The patch is incomplete - it does not document the new behaviour and I have not added any new tests or tweaks to existing tests - but it does show how I think that the feature might work.

The new behaviour is only enabled when the --wide option is enabled.

In addition to decoding the symbol indicies, the patch also changes readelf's current behaviour slightly in that any non-visibility information in a symbols st_other field is now only displayed when --wide is active.  I made this change because displaying the "other" information disrupts the layout of the table being displayed and I want to keep the non-wide display as uniform as possible.

You may also wonder about/hate the display of "==>" for section symbols.  I did not want to display the section name twice, but I did want a way to indicate that the section index does have a string associated with it.
Comment 3 Nick Clifton 2023-07-27 13:49:32 UTC
Created attachment 15016 [details]
Proposed patch

And here is a full patch.

After some more thought, I decided that the new behaviour needed to be gated by a command line option, so I have added --extra-sym-info to do this.

I have also moved the display of non-visibility related bits in the st_other field to this new option.  This means that by default the symbol table displayed by readelf should be completely uniform.

I have discarded the "==>" for section symbols - it looked silly.  Instead I just filled the gap with spaces.

Any comments ?
Comment 4 Fangrui Song 2023-07-28 05:47:41 UTC
(In reply to Nick Clifton from comment #3)
> Created attachment 15016 [details]
> Proposed patch
> 
> And here is a full patch.
> 
> After some more thought, I decided that the new behaviour needed to be gated
> by a command line option, so I have added --extra-sym-info to do this.

Thank you for implementing this! Yes, I think quite a few projects rely on the output of readelf -Ws. Using an opt-in option is necessary. The option name -X/--extra-sym-info looks good to me. Another long option name choice is --symbol-details to be similar to --section-details.

> I have also moved the display of non-visibility related bits in the st_other
> field to this new option.  This means that by default the symbol table
> displayed by readelf should be completely uniform.

Like [<localentry>: 8] in the [Other] column for ppc64 ELFv2? Looks good to me, but I'd like to hear from Alan.

> I have discarded the "==>" for section symbols - it looked silly.  Instead I
> just filled the gap with spaces.

objdump -t shows the section name twice for STT_SECTION symbols as well. I think readelf -sX displaying the section name twice should be as well, to not make STT_SECTION special.
Comment 5 Sourceware Commits 2023-09-05 10:09:14 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b6ac461ace19ba19aaf135a028df4e67e47e21d7

commit b6ac461ace19ba19aaf135a028df4e67e47e21d7
Author: Nick Clifton <nickc@redhat.com>
Date:   Tue Sep 5 11:08:23 2023 +0100

    readelf: Add option to display the names of sections referenced by symbols.
    
      PR 30684
      * readelf.c (extra_sym_info): New variable. (section_name_valid): Also check for filedata being NULL. (section_name_print): Delete. (section_index_real): New function.  Returns true if the given section index references a real section. (print_symbol): Rename to print_sumbol_name. (printable_section_name): Use a rotating array of static buffers for the return string. (printable_section_name_from_index): Merge code from dump_relocations and get_symbol_index_type into here. (long_option_values): Add OPTION_NO_EXTRA_SYM_INFO. (options): Add "extra-sym-info" and "no-extra-sym-info". (usage): Mention new options. (parse_args): Parse new options. (get_symbol_index_type): Delete. (print_dynamic_symbol_size): Rename to print_symbol_size. (print_dynamic_symbol): Rename to print_symbol. (print_symbol_table_heading): New function. (process_symbol_table): Use new function.
      * doc/binutils.texi: Document the new option.
      * NEWS: Mention the new feature.
Comment 6 Nick Clifton 2023-09-05 10:11:02 UTC
Right, sorry for the delay in responding.

I have gone ahead and applied a variation on the patch which adds the -X option to display the section names, but which does not change when/where the information held in a symbol's st_other field is displayed.  That change caused too many disruptions to the testsuites and was bound to upset some users.