<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 - Linker defined variable used in asm() call results in incorrect value for 32-bit code"
   href="https://bugs.llvm.org/show_bug.cgi?id=48461">48461</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Linker defined variable used in asm() call results in incorrect value for 32-bit code
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>10.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 Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>Jared.candelaria@intel.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=24256" name="attach_24256" title="Repro consisting of C source and linker script with build script">attachment 24256</a> <a href="attachment.cgi?id=24256&action=edit" title="Repro consisting of C source and linker script with build script">[details]</a></span>
Repro consisting of C source and linker script with build script

I'm compiling 32-bit x86 code that uses inline assembly, linker defined
variables, and some arithmetic. Clang generates unexpected code around this
compared to gcc.

For example, given the following C:
extern uint8_t BASE[]; // This is the linker defined variable (0x2000).
#define BASE_ADDRESS                        ((uint32_t) BASE)
#define TOP                                 (0xFF100000UL)
#define OFFSET                              (0x000000a5UL)
#define REGISTERS_BASE                      (TOP - BASE_ADDRESS)
#define WRITE_CTL_REG(Register, Value)  WRITE_REG(REGISTERS_BASE + (Register),
Value)
#define WRITE_REG(Register, Value)      \
   __asm__(                             \
      "movl %1, %%fs:(%0) \n\t"         \
      :                                 \
      : "ir"(Register), "ir"(Value)     \
      : "memory"                        \
      )                                 \

    WRITE_CTL_REG(OFFSET, 0xdead);

Clang generates the corresponding:
64 c7 05 a5 20 f0 00    movl   $0xdead,%fs:0xf020a5

While gcc generates:
b8 00 20 00 00          mov    $0x2000,%eax
ba a5 00 10 ff          mov    $0xff1000a5,%edx
29 c2                   sub    %eax,%edx
89 d0                   mov    %edx,%eax
64 c7 00 ad de 00 00    movl   $0xdead,%fs:(%eax)

Clang generates the expected code when compiling as 64-bit.

I tested this on Ubuntu clang version
10.0.1-++20201013091236+ef32c611aa2-1~exp1~20201013191834.199. Attached is a
small repro that compiles, links, and disassembles the problem spot. You can
uncomment a gcc invocation in build.sh to see what gcc will do in the same
situation. Also attached is example.txt that does this for you.</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>