<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 - llvm-nm yields incorrect result for Webassembly objects"
   href="https://bugs.llvm.org/show_bug.cgi?id=34392">34392</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>llvm-nm yields incorrect result for Webassembly objects
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>5.0
          </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>llvm-nm
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>mvogelsang@rocketmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dschuff@google.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=19067" name="attach_19067" title="test.c">attachment 19067</a> <a href="attachment.cgi?id=19067&action=edit" title="test.c">[details]</a></span>
test.c

llvm-nm is able to process binary wasm object files and produce well-formatted
results, but the results aren't sensible or useful. I'll outline the expected
behaviour and actual behaviour.

If we have a file `test.c` with the contents
'''
char first[0xF];
char second[0xFF];
char third[0x9];
char fourth[0xA];
'''

and compile it to ARM using a clang that is set to target
`aarch64-unknown-linux-gnu` with the command `$CC -c test.c -o armtest.o` we
can look at the GNU nm output with `nm -P armtest.o` with the following
results.
'''
$d.0 n 0000000000000000 
first C 000000000000000f 000000000000000f
fourth C 000000000000000a 000000000000000a
second C 00000000000000ff 00000000000000ff
third C 0000000000000009 0000000000000009
'''

llvm-nm yields a similar enough result with `llvm-nm -P armtest.o`
'''
$d.0 n 0 0
first C f 0
fourth C a 0
second C ff 0
third C 9 0
'''

Now, we can compile test.c to wasm using a clang set to target
`wasm32-unknown-unknown-wasm` with the command `$CC -c test.c -o wasmtest.o`.
GNU nm, of course, just errors if you try to run it on wasmtest.o. However
`llvm-nm -P wasmtest.o` yields the following.
'''
first D 0 0
fourth D 3 0
second D 1 0
third D 2 0
'''

It kinda just spits out the symbols with a number that indicates the relative
declaration order.
It is worthwhile to take a look at the wasm-dis of wasmtest.o.
'''
(module
 (global $global$0 i32 (i32.const 0))
 (global $global$1 i32 (i32.const 16))
 (global $global$2 i32 (i32.const 271))
 (global $global$3 i32 (i32.const 280))
 (table 0 anyfunc)
 (memory $0 1)
 (data (i32.const 0)
"\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
 (export "first" (global $global$0))
 (export "second" (global $global$1))
 (export "third" (global $global$2))
 (export "fourth" (global $global$3))
 ;; custom section "linking", size 15
)
'''

Notice, lines 2-5 seem to indicate that the information necessary to determine
symbol values/sizes and offsets is available in wasmtest.o, the file just isn't
getting processed correctly. Somehow, for each symbol we're getting the 'x' out
of it's '$global$x'. 

test.c, armtest.o, wasmtest.o, and the wasm-dis are attached. 

Sorry to preemptively ping you on this Derek. I couldn't file this under
llvm-nm and something wasm related, so I figured I should at least tag someone
on the wasm team to make you all aware. 

GHC's build system actually relies on using nm at one point. Our current
workaround is to compile with -flto and parse the llvm-dis of the necessary
object files.</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>