[PATCH] D28560: [Object/ELF] - Allow to parse files with e_shstrndx set to SHN_UNDEF

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 11 07:49:45 PST 2017


We already have

  uint32_t Offset = Section->sh_name;
  if (Offset == 0)
    return StringRef();

It seems broken to not have a string table and yet have non zero
sh_name. Where did you find this file?

Cheers,
Rafael


George Rimar via Phabricator <reviews at reviews.llvm.org> writes:

> grimar created this revision.
> grimar added reviewers: rafael, davide.
> grimar added subscribers: llvm-commits, grimar, evgeny777.
>
> According to specification,
>
> e_shstrndx
> The section header table index of the entry that is associated with the section name string table. If the file has no section name string table, this member holds the value SHN_UNDEF.
>
> Previously objdump just reported error on such files. Though gnu readelf is able to parse them, below
> is sample output from readelf:
>
>   Section Headers:
>     [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
>     [ 0] <no-name>         NULL            0000000000000000 000000 000000 00      0   0  0
>     [ 1] <no-name>         PROGBITS        0000000000000000 001000 00000e 00  AX  0   0  4
>     [ 2] <no-name>         PROGBITS        000000000000000e 00100e 000020 00  WA  0   0  1
>     [ 3] <no-name>         PROGBITS        000000000000002e 00102e 000003 00  WA  0   0  1
>     [ 4] <no-name>         NOBITS          0000000000000031 001031 000002 00  WA  0   0  1
>     [ 5] <no-name>         PROGBITS        0000000000000000 001031 000008 01  MS  0   0  1
>     [ 6] <no-name>         SYMTAB          0000000000000000 001040 000030 18      7   1  8
>     [ 7] <no-name>         STRTAB          0000000000000000 001070 000008 00      0   0  1
>
>
> https://reviews.llvm.org/D28560
>
> Files:
>   include/llvm/Object/ELF.h
>   test/Object/Inputs/elf-no-section-header-table.elf-x86-64
>   test/Object/elf-no-section-header-table.test
>
>
> Index: test/Object/elf-no-section-header-table.test
> ===================================================================
> --- test/Object/elf-no-section-header-table.test
> +++ test/Object/elf-no-section-header-table.test
> @@ -0,0 +1,8 @@
> +## elf-no-section-header-table.elf-x86-64 is a simple ELF with e_shstrndx filed set
> +## to SHN_UNDEF, what means file has no section name string table.
> +# RUN: llvm-objdump -section-headers \
> +# RUN:   %p/Inputs/elf-no-section-header-table.elf-x86-64 2>&1 | FileCheck %s
> +
> +# CHECK:      Sections:
> +# CHECK-NEXT:  Idx Name          Size      Address          Type
> +# CHECK-NEXT:    0 <no-name>     00000000 0000000000000000
> Index: include/llvm/Object/ELF.h
> ===================================================================
> --- include/llvm/Object/ELF.h
> +++ include/llvm/Object/ELF.h
> @@ -327,7 +327,7 @@
>    if (Index == ELF::SHN_XINDEX)
>      Index = Sections[0].sh_link;
>  
> -  if (!Index) // no section string table.
> +  if (Index == ELF::SHN_UNDEF) // no section string table.
>      return "";
>    if (Index >= Sections.size())
>      return createError("invalid section index");
> @@ -491,6 +491,8 @@
>    auto Table = getSectionStringTable(*SectionsOrErr);
>    if (!Table)
>      return Table.takeError();
> +  if (Table->empty())
> +    return "<no-name>";
>    return getSectionName(Section, *Table);
>  }
>  
>
>
> Index: test/Object/elf-no-section-header-table.test
> ===================================================================
> --- test/Object/elf-no-section-header-table.test
> +++ test/Object/elf-no-section-header-table.test
> @@ -0,0 +1,8 @@
> +## elf-no-section-header-table.elf-x86-64 is a simple ELF with e_shstrndx filed set
> +## to SHN_UNDEF, what means file has no section name string table.
> +# RUN: llvm-objdump -section-headers \
> +# RUN:   %p/Inputs/elf-no-section-header-table.elf-x86-64 2>&1 | FileCheck %s
> +
> +# CHECK:      Sections:
> +# CHECK-NEXT:  Idx Name          Size      Address          Type
> +# CHECK-NEXT:    0 <no-name>     00000000 0000000000000000
> Index: include/llvm/Object/ELF.h
> ===================================================================
> --- include/llvm/Object/ELF.h
> +++ include/llvm/Object/ELF.h
> @@ -327,7 +327,7 @@
>    if (Index == ELF::SHN_XINDEX)
>      Index = Sections[0].sh_link;
>  
> -  if (!Index) // no section string table.
> +  if (Index == ELF::SHN_UNDEF) // no section string table.
>      return "";
>    if (Index >= Sections.size())
>      return createError("invalid section index");
> @@ -491,6 +491,8 @@
>    auto Table = getSectionStringTable(*SectionsOrErr);
>    if (!Table)
>      return Table.takeError();
> +  if (Table->empty())
> +    return "<no-name>";
>    return getSectionName(Section, *Table);
>  }
>  


More information about the llvm-commits mailing list