<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - clang reorders module level asm statements"
   href="https://llvm.org/bugs/show_bug.cgi?id=27688">27688</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang reorders module level asm statements
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

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

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>rjones@redhat.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=16335" name="attach_16335" title="asmorder.c">attachment 16335</a> <a href="attachment.cgi?id=16335&action=edit" title="asmorder.c">[details]</a></span>
asmorder.c

Firstly, I realize here that I'm operating well outside any C standards or
guarantees of any sort.  But this stuff is useful for some people.

If you want to write a PC BIOS option ROM in C, you can do it in
gcc using asm statements at the module level of the C file, at the
start and end.  These asm statements contain the option ROM header
and trailing and effectively "bracket" the C code in the middle.
(See attached example).

This can be compiled using:

  gcc -m16 -c asmorder.c
  ld  -m elf_i386 -Ttext 0 -e _start -s -o asmorder.img asmorder.o
  objcopy -O binary -j .text asmorder.img asmorder.bin

to produce the option ROM (I missed out a step for computing the
checksum of the ROM but it's not relevant for this discussion).

If you disassemble the object file, you'll see that the order of
the start & end asm statements is preserved, and the size of the
file in the header is computed correctly:

  objdump -d asmorder.o

However this does not work if you substitute clang for gcc.

The problem is that clang reorders the module level asm statements,
putting them all at the start of the file, and so the option ROM
format isn't recognized.

In short, gcc converts:

  asm("some stuff");
  some_C_functions;
  asm("more stuff");

into:

  some stuff
  assembler of some_C_functions
  more stuff

but clang turns it into:

  some stuff
  more stuff
  assembler of some_C_functions

Note I'm not using any optimization.

It would be nice if clang didn't do that, at least for asm statements
appearing at the beginning and end of the file.

gcc-6.0.0-0.20.fc24.x86_64 (also works in gcc 4 & 5)
clang-3.8.0-1.fc24.x86_64</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>