Skip to content

Commit 507e7e2

Browse files
author
Mark Wielaard
committed
readelf: Add optional --symbols[=SECTION] argument to select section name.
Make it possible to display just the symbols from a named symbol section instead of always displaying all symbol sections. https://bugzilla.redhat.com/show_bug.cgi?id=1396092 Signed-off-by: Mark Wielaard <[email protected]>
1 parent d5afff8 commit 507e7e2

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

src/ChangeLog

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2016-11-17 Mark Wielaard <[email protected]>
2+
3+
* readelf.c (options): Add optional arg SECTION for symbols.
4+
(symbol_table_section): New static variable.
5+
(parse_opt): Set symbol_table_section on 's'.
6+
(print_symtab): Filter on symbol_table_section name is set.
7+
18
2016-11-10 Mark Wielaard <[email protected]>
29

310
* ar.c (write_member): Make sure tmpbuf is large enough to contain

src/readelf.c

+19-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ static const struct argp_option options[] =
8585
{ "relocs", 'r', NULL, 0, N_("Display relocations"), 0 },
8686
{ "section-headers", 'S', NULL, 0, N_("Display the sections' headers"), 0 },
8787
{ "sections", 'S', NULL, OPTION_ALIAS | OPTION_HIDDEN, NULL, 0 },
88-
{ "symbols", 's', NULL, 0, N_("Display the symbol table"), 0 },
88+
{ "symbols", 's', "SECTION", OPTION_ARG_OPTIONAL,
89+
N_("Display the symbol table sections"), 0 },
8990
{ "version-info", 'V', NULL, 0, N_("Display versioning information"), 0 },
9091
{ "notes", 'n', NULL, 0, N_("Display the ELF notes"), 0 },
9192
{ "arch-specific", 'A', NULL, 0,
@@ -157,6 +158,9 @@ static bool print_section_header;
157158
/* True if the symbol table should be printed. */
158159
static bool print_symbol_table;
159160

161+
/* A specific section name, or NULL to print all symbol tables. */
162+
static char *symbol_table_section;
163+
160164
/* True if the version information should be printed. */
161165
static bool print_version_info;
162166

@@ -389,6 +393,7 @@ parse_opt (int key, char *arg,
389393
case 's':
390394
print_symbol_table = true;
391395
any_control_option = true;
396+
symbol_table_section = arg;
392397
break;
393398
case 'V':
394399
print_version_info = true;
@@ -2236,6 +2241,19 @@ print_symtab (Ebl *ebl, int type)
22362241

22372242
if (shdr != NULL && shdr->sh_type == (GElf_Word) type)
22382243
{
2244+
if (symbol_table_section != NULL)
2245+
{
2246+
/* Get the section header string table index. */
2247+
size_t shstrndx;
2248+
const char *sname;
2249+
if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
2250+
error (EXIT_FAILURE, 0,
2251+
gettext ("cannot get section header string table index"));
2252+
sname = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
2253+
if (sname == NULL || strcmp (sname, symbol_table_section) != 0)
2254+
continue;
2255+
}
2256+
22392257
if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
22402258
{
22412259
if (elf_compress (scn, 0, 0) < 0)

tests/ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2016-11-17 Mark Wielaard <[email protected]>
2+
3+
* run-readelf-s.sh: Add --symbols=.dynsym and --symbols=.symtab tests.
4+
15
2016-11-02 Mark Wielaard <[email protected]>
26

37
* backtrace-data.c (thread_callback): Add explicit break after error.

tests/run-readelf-s.sh

+11
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,20 @@ Symbol table [28] '.symtab' contains 40 entries:
273273
39: 0000000000000680 0 FUNC GLOBAL DEFAULT 11 _init
274274
EOF
275275

276+
# Display all symbol tables.
276277
cat testfile.dynsym.in testfile.symtab.in \
277278
| testrun_compare ${abs_top_builddir}/src/readelf -s testfilebaztab
278279

280+
# Display just .dynsym
281+
cat testfile.dynsym.in \
282+
| testrun_compare ${abs_top_builddir}/src/readelf \
283+
--symbols=.dynsym testfilebaztab
284+
285+
# Display just .symtab
286+
cat testfile.symtab.in \
287+
| testrun_compare ${abs_top_builddir}/src/readelf \
288+
--symbols=.symtab testfilebaztab
289+
279290
cat testfile.dynsym.in \
280291
| testrun_compare ${abs_top_builddir}/src/readelf -s testfilebazdbg
281292

0 commit comments

Comments
 (0)