<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 --- - [Rust] Executable linked with -pie segfaults"
   href="https://llvm.org/bugs/show_bug.cgi?id=30434">30434</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[Rust] Executable linked with -pie segfaults
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>lld
          </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>ELF
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>japaricious@gmail.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>Trying to link a hello world program written in Rust succeeds but running the
executable produces a
segfault. If I remove the `-pie` flag from the arguments `rustc` passes to
`lld` then the link
also succeeds but I get an executable that works correctly this time.

### Steps To Reproduce

As a bash script but I've also attached the .cpio file generated by these
commands:

``` bash
main() {
    install_rust

    # test program
    echo 'fn main() { println!("Hello, world!"); }' > hello.rs

    # compile only, we'll link manually
    rm -f hello.o
    rustc --emit=obj hello.rs

    # store the path to the sysroot in a variable to make the following
commands shorter
    local sysroot=$(rustc --print sysroot)

    # link 
    # NOTE The arguments used here are what `rustc` (and `cc`) would ultimately
pass to `ld` when
    # directly building an executable via the `rustc hello.rs` command. You can
see what flags
    # `rustc` passes to `cc` with the `rustc -Z print-link-args hello.rs`
command.
    # NOTE(2) You will have to update the library search paths and paths to
startup objects
    ld.lld \
        --build-id \
        --eh-frame-hdr \
        --hash-style=gnu \
        -m \
        elf_x86_64 \
        -dynamic-linker \
        /lib64/ld-linux-x86-64.so.2 \
        -pie \
        -o \
        hello \
        /usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../lib/Scrt1.o \
        /usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../lib/crti.o \
        /usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/crtbeginS.o \
        -L$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib \
        -L$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib \
        -L/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1 \
        -L/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../lib \
        -L/lib/../lib \
        -L/usr/lib/../lib \
        -L/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../.. \
        --as-needed \
        -z \
        noexecstack \
        hello.o \
        --gc-sections \
        -Bstatic \
        -Bdynamic \
        $sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-411f48d3.rlib
\
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-411f48d3.rlib
\
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-411f48d3.rlib \
        $sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/librand-411f48d3.rlib
\
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcollections-411f48d3.rlib
\
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_unicode-411f48d3.rlib
\
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-411f48d3.rlib \
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_jemalloc-411f48d3.rlib
\
        $sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-411f48d3.rlib
\
        $sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-411f48d3.rlib
\
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-411f48d3.rlib
\
        -ldl \
        -lpthread \
        -lgcc_s \
        -lpthread \
        -lc \
        -lm \
        -lrt \
        -lutil \
        /usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/crtendS.o \
        /usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../lib/crtn.o \
        --reproduce pie

    # segfaults
    ./hello

    # NOTE some link command as above but without the -pie flag
    ld.lld \
        --build-id \
        --eh-frame-hdr \
        --hash-style=gnu \
        -m \
        elf_x86_64 \
        -dynamic-linker \
        /lib64/ld-linux-x86-64.so.2 \
        -o \
        hello \
        /usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../lib/Scrt1.o \
        /usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../lib/crti.o \
        /usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/crtbeginS.o \
        -L$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib \
        -L$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib \
        -L/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1 \
        -L/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../lib \
        -L/lib/../lib \
        -L/usr/lib/../lib \
        -L/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../.. \
        --as-needed \
        -z \
        noexecstack \
        hello.o \
        --gc-sections \
        -Bstatic \
        -Bdynamic \
        $sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-411f48d3.rlib
\
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-411f48d3.rlib
\
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-411f48d3.rlib \
        $sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/librand-411f48d3.rlib
\
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcollections-411f48d3.rlib
\
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_unicode-411f48d3.rlib
\
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-411f48d3.rlib \
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_jemalloc-411f48d3.rlib
\
        $sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-411f48d3.rlib
\
        $sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-411f48d3.rlib
\
       
$sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-411f48d3.rlib
\
        -ldl \
        -lpthread \
        -lgcc_s \
        -lpthread \
        -lc \
        -lm \
        -lrt \
        -lutil \
        /usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/crtendS.o \
        /usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../lib/crtn.o

    # OK
    ./hello

    uninstall_rust
}

install_rust() {
    curl <a href="https://sh.rustup.rs">https://sh.rustup.rs</a> -sSf | sh -s -- --default-toolchain
nightly-2016-09-15 --no-modify-path -y
    source ~/.cargo/env
}

uninstall_rust() {
    rustup self uninstall
}

main
```

### Meta

```
$ ld.lld --version
LLD 4.0 (git://github.com/llvm-mirror/lld.git
a499b2da2436789a9136f046f193caad0801ed93)
```

```
$ rustc -Vv
rustc 1.13.0-nightly (6ffdda1ba 2016-09-14)
binary: rustc
commit-hash: 6ffdda1ba183c981d57e63b59c88184be449eee4
commit-date: 2016-09-14
host: x86_64-unknown-linux-gnu
release: 1.13.0-nightly
```</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>