<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [obj2yaml][yaml2obj] Cannot round-trip clang-produced object file"
   href="https://bugs.llvm.org/show_bug.cgi?id=50506">50506</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[obj2yaml][yaml2obj] Cannot round-trip clang-produced object file
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>jh7370.2008@my.bristol.ac.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>