<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 - DEFINED(foo) always evaluates to true if foo is also defined in the script"
   href="https://bugs.llvm.org/show_bug.cgi?id=40396">40396</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>DEFINED(foo) always evaluates to true if foo is also defined in the script
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>lld
          </td>
        </tr>

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

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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>ELF
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>pkmx.tw@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, peter.smith@linaro.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>$ cat test.ld
SECTIONS {
  foo = DEFINED(foo) ? foo : 0x1000;
  ASSERT ( foo == 0x1000 , "should not happen" )
}

$ echo | clang -xc - -c -o empty.o

$ ld.lld empty.o -Ttest.ld
ld.lld: error: should not happen

This is a documented usage in bfd's ld manual
(<a href="https://sourceware.org/binutils/docs/ld/Builtin-Functions.html#Builtin-Functions">https://sourceware.org/binutils/docs/ld/Builtin-Functions.html#Builtin-Functions</a>),
where foo should retain its value if it is already set prior to the statement
(via --defsym or other methods), or otherwise set to a default value 0x1000.
However, lld always evaluates it to 0.

The root cause seems to be rL323729 that adds all symbol assignments to the
symbol table as a dummy `Defined` absolute symbol of value 0, which causes the
DEFINED() builtin to always evaluate to true.</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>