[llvm-bugs] [Bug 50506] New: [obj2yaml][yaml2obj] Cannot round-trip clang-produced object file

via llvm-bugs llvm-bugs at lists.llvm.org
Thu May 27 06:37:35 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=50506

            Bug ID: 50506
           Summary: [obj2yaml][yaml2obj] Cannot round-trip clang-produced
                    object file
           Product: new-bugs
           Version: unspecified
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: jh7370.2008 at my.bristol.ac.uk
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org

If you take a trivial clang object file, run it through obj2yaml and then the
output through yaml2obj, the latter tool reports an error:

C:\Work\TempWork> touch test.cpp
C:\Work\TempWork> C:\llvm\build\Debug\bin\clang.exe -c test.cpp
C:\Work\TempWork> C:\llvm\build\Debug\bin\obj2yaml.exe test.o -o test.yaml
C:\Work\TempWork> C:\llvm\build\Debug\bin\yaml2obj test.yaml -o test2.o
yaml2obj: error: section '.shstrtab' should be present in the 'Sections' or
'Excluded' lists

The intermediate YAML file looks like this:
--- !ELF
FileHeader:
  Class:           ELFCLASS64
  Data:            ELFDATA2LSB
  Type:            ET_REL
  Machine:         EM_X86_64
Sections:
  - Name:            .text
    Type:            SHT_PROGBITS
    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
    AddressAlign:    0x4
  - Name:            .comment
    Type:            SHT_PROGBITS
    Flags:           [ SHF_MERGE, SHF_STRINGS ]
    AddressAlign:    0x1
    EntSize:         0x1
    Content:         00636C616E672076657273696F6E2031332E302E3000
  - Name:            .note.GNU-stack
    Type:            SHT_PROGBITS
    AddressAlign:    0x1
  - Name:            .llvm_addrsig
    Type:            SHT_LLVM_ADDRSIG
    Flags:           [ SHF_EXCLUDE ]
    Link:            .symtab
    AddressAlign:    0x1
    Offset:          0x88
    Symbols:         [  ]
  - Type:            SectionHeaderTable
    Sections:
      - Name:            .strtab
      - Name:            .text
      - Name:            .comment
      - Name:            .note.GNU-stack
      - Name:            .llvm_addrsig
      - Name:            .symtab
Symbols:
  - Name:            test.cpp
    Type:            STT_FILE
    Index:           SHN_ABS
...

The problem is the presence of the SectionHeaderTable key. Deleting it causes
the error to go away. The SectionHeaderTable key however is necessary to
faithfully represent the input object file: the section data in that object
file is not in the same order as the section header table. Additionally, clang
quite reasonably produces a single combined string table for the symbol and
section names (the .strtab) section. This single section is represented in the
SectionHeaderTable. yaml2obj implicitly generates separate sections for these
strings however(i.e .strtab and .shstrtab), and when a SectionHeaderTable
element is present, it expects all sections to be listed (either in the
Sections list, or the Exclude list). Since .shstrtab wasn't present in the
object file, it doesn't appear in the YAML, which causes yaml2obj to reject it.

I can think of a couple of different possible fixes:
1) Add yaml2obj implicitly created sections to the end of the output object
file, if not mentioned in the SectionHeaderTable.
2) Add a key to obj2yaml/yaml2obj which indicates which section to use for the
section header table and/or symbol string table, or which indicates the two
tables should be merged. yaml2obj would then generate one or two string tables
as requesteed.
3) Change the LLVM code to emit sections in the section header table in the
order they appear in the output. That would mean obj2yaml doesn't need to
generate a SectionHeaderKey in this instance.

I think 2) most faithfully represents the input object, and should therefore be
our preferred approach.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210527/e70314e8/attachment.html>


More information about the llvm-bugs mailing list