<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 - Fix stream interleaving caused by llvm-readelf stream usage"
   href="https://bugs.llvm.org/show_bug.cgi?id=42140">42140</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Fix stream interleaving caused by llvm-readelf stream usage
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>tools
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </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>llvm-readobj
          </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>jh7370.2008@my.bristol.ac.uk, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following test:

# RUN: yaml2obj %s -o %t
# RUN: llvm-readelf --dynamic-table --symbols %t | FileCheck %s

# CHECK: foo{{$}}

--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type:    ET_DYN
  Machine: EM_X86_64
Sections:
  - Name:    .dynamic
    Type:    SHT_DYNAMIC
    Entries:
      - Tag: DT_NULL
        Value: 0
Symbols:
  - Name: foo
ProgramHeaders:
  - Type: PT_DYNAMIC
    Sections:
      - Section: .dynamic

The output should print a symbol table, followed by a new line and then the
dynamic table like this:
Symbol table '.symtab' contains 2 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND foo
DynamicSection [ (1 entries)
  Tag                Type                 Name/Value
  0x0000000000000000 NULL                 0x0
]

This is what happens when I run the llvm-readelf command directly on the
command-line. However, when the output is redirected to a file or other process
(such as when run through lit), the output is as follows:

Symbol table '.symtab' contains 2 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND fooDynamicSection [
(1 entries)
  Tag                Type                 Name/Value
  0x0000000000000000 NULL                 0x0
]

Note that there is an extra new line at the end of the output, in addition to
no new line between the "foo" symbol name and "DynamicSection". As such the
above test will fail.

I did some investigation and the problem is that the GNUStyle dumper has its
own stream instance, which is buffered.

class GNUStyle {
  formatted_raw_ostream OS;
  ...
};

This stream wraps the ScopedPrinter's stream that ELFDumper is provided, and
adds the layer of buffering over the top. This is fine as long as all printing
is done exclusively through the GNUStyle's variable, or exclusively through
ELFDumper's stream variable. If the two are mixed, the following happens:

GNUStyle->OS prints some stuff, buffers the remainder
ELFDumper->OS prints some stuff
GNUStyle->OS prints the remainder of its buffer

This is bad, as it results in interleaving. The exact amount depends on what is
printed, because the GNUStyle buffer is sometimes flushed, but not always.</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>