Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build crtbegin.o/crtend.o from source code #85395

Merged
merged 1 commit into from
May 31, 2021
Merged

Conversation

12101111
Copy link
Contributor

@12101111 12101111 commented May 17, 2021

Build crtbengin.o/crtend.o from source code instead of copying from gcc.

The crtbegin and crtend implementation from llvm don't need crtbeginS.o for PIC. crtbegin{,S,T}.o is unified into one generic crtbegin.o. See the comments in https://reviews.llvm.org/D28791#1419436 and https://reviews.llvm.org/D28791#1420914

fix: #85310 , fix: #47551 , fix: #84033

@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 17, 2021
@12101111 12101111 changed the title Build crtbengin.o/crtend.o from source code Build crtbegin.o/crtend.o from source code May 19, 2021
@12101111
Copy link
Contributor Author

Test with the code from #47551 :

#![feature(backtrace)]
#[derive(Clone, Copy)]
struct Foo {
    array: [u64; 10240],
}

impl Foo {
    const fn new() -> Self {
        Self {
            array: [0x1122_3344_5566_7788; 10240]
        }
    }
}

static BAR: [Foo; 10240] = [Foo::new(); 10240];

fn main() {
    let bt = std::backtrace::Backtrace::force_capture();
    println!("Hello, world! {:?}", bt);
    println!("{:x}", BAR[0].array[0]);
}

The broken rustc that don't link to crtbeginS.o/crtendS.o:

> export RUSTFLAGS="-Z print-link-args -C link-args=-fuse-ld=bfd"
> cargo +stage2  run -vvv
   Compiling hello v0.1.0 (/tmp/hello)
     Running `CARGO=/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/bin/cargo CARGO_BIN_NAME=hello CARGO_CRATE_NAME=hello CARGO_MANIFEST_DIR=/tmp/hello CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=hello CARGO_PKG_REPOSITORY='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/usr/obj/rust/debug/deps:/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib:/home/han/.rustup/toolchains/stage2/lib' rustc --crate-name hello --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=45f8637d16581c8f -C extra-filename=-45f8637d16581c8f --out-dir /usr/obj/rust/debug/deps -C incremental=/usr/obj/rust/debug/incremental -L dependency=/usr/obj/rust/debug/deps -Z print-link-args -C link-args=-fuse-ld=bfd`
"cc" "-m64" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-Wl,--as-needed" "-nostartfiles" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/rcrt1.o" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crti.o" "-L" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib" "-L" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.13c46qjpqqmsvlt7.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.15v6bxaiph9x009s.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.1ink71ppmx2dzs7x.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.1uw2b3f6g5y40ue3.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.266s81kb0ncdbolo.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.2eue4xcls65wdpx8.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.2p4usy9q2qvnypt6.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.2ps5mydlcnk9396a.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.2skmjjaikvpkfupm.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.30dz11z0aofnesqr.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.3n7b5ehgwrintgct.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.3u88p76iye7ms53s.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.3wogw5s564ur4fuw.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.52b1zbprkak691s9.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.5a02jqibtvw5j173.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.7ufvqzpplts0u9c.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.cc1no8scl2oh3vx.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.hh14nni9hru6433.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.s3hwstby9sigjq.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.uaspq4z5q2q46ku.rcgu.o" "-o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.3xix89bijvttq8xf.rcgu.o" "-Wl,--gc-sections" "-static-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/usr/obj/rust/debug/deps" "-L" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-09863bbdf407aa03.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-1b63f9f92f3108f4.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libminiz_oxide-32d4417712748336.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libadler-6b51cf8ee6f6a418.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libobject-155010ea90fd90f7.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libaddr2line-e5d9a2520c52002b.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libgimli-cb8f304520dcee08.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd_detect-ecccbd55539888eb.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-aa6c405eaec4f6eb.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-e36d8e13091d3283.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-3fa5f334a8a4e7aa.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-c9316e944fa41c58.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-d2617c158ec95334.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-a59daf1420a022a8.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-eed919ff8c26abfe.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-66552530c797bb9f.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-e378844bf95f8386.rlib" "-Wl,--end-group" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-47286ef3f3775665.rlib" "-fuse-ld=bfd" "-Wl,-Bdynamic" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
    Finished dev [unoptimized + debuginfo] target(s) in 1.47s
     Running `/usr/obj/rust/debug/hello`
zsh: segmentation fault (core dumped)  cargo +stage2 run -vvv
> lldb /usr/obj/rust/debug/hello
(lldb) target create "/usr/obj/rust/debug/hello"
Current executable set to '/usr/obj/rust/debug/hello' (x86_64).
(lldb) r
Process 32683 launched: '/usr/obj/rust/debug/hello' (x86_64)
Process 32683 stopped
* thread #1, name = 'hello', stop reason = signal SIGSEGV: invalid address (fault address: 0x0)
    frame #0: 0x00007fffc5fd2301 hello`libunwind::CFI_Parser<libunwind::LocalAddressSpace>::findFDE(libunwind::LocalAddressSpace&, unsigned long, unsigned long, unsigned long, unsigned long, libunwind::CFI_Parser<libunwind::LocalAddressSpace>::FDE_Info*, libunwind::CFI_Parser<libunwind::LocalAddressSpace>::CIE_Info*) + 97
hello`libunwind::CFI_Parser<libunwind::LocalAddressSpace>::findFDE:
->  0x7fffc5fd2301 <+97>:  movl   (%rbp), %ebx
    0x7fffc5fd2304 <+100>: leaq   0x4(%rbp), %rsi
    0x7fffc5fd2308 <+104>: movq   %rsi, 0x38(%rsp)
    0x7fffc5fd230d <+109>: cmpq   %r14, %rbx
(lldb) bt
error: need to add support for DW_TAG_base_type '()' encoded with DW_ATE = 0x7, bit_size = 0
* thread #1, name = 'hello', stop reason = signal SIGSEGV: invalid address (fault address: 0x0)
  * frame #0: 0x00007fffc5fd2301 hello`libunwind::CFI_Parser<libunwind::LocalAddressSpace>::findFDE(libunwind::LocalAddressSpace&, unsigned long, unsigned long, unsigned long, unsigned long, libunwind::CFI_Parser<libunwind::LocalAddressSpace>::FDE_Info*, libunwind::CFI_Parser<libunwind::LocalAddressSpace>::CIE_Info*) + 97
    frame #1: 0x00007fffc5fd844e hello`libunwind::UnwindCursor<libunwind::LocalAddressSpace, libunwind::Registers_x86_64>::setInfoBasedOnIPRegister(bool) + 350
    frame #2: 0x00007fffc5fda7f5 hello`unw_step + 165
    frame #3: 0x00007fffc5fcdf3b hello`_Unwind_Backtrace + 107
    frame #4: 0x00007fffc5fb073c hello`std::backtrace::Backtrace::create::h99b62ff1676d9f60 + 140
    frame #5: 0x00007fffc5fb06a0 hello`std::backtrace::Backtrace::force_capture::h28ea90e80c6b235c + 16
    frame #6: 0x00007fffc5fa2e82 hello`hello::main::hc539c34270107337 at main.rs:18:14
    frame #7: 0x00007fffc5fa261b hello`core::ops::function::FnOnce::call_once::hf9d16316d26b5a58((null)=(hello`hello::main::hc539c34270107337 at main.rs:17), (null)=<unavailable>) at function.rs:227:5
    frame #8: 0x00007fffc5fa1cee hello`std::sys_common::backtrace::__rust_begin_short_backtrace::h6c47d048019b78ff(f=(hello`hello::main::hc539c34270107337 at main.rs:17)) at backtrace.rs:125:18
    frame #9: 0x00007fffc5fa1b81 hello`std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h65a46410314f2661 at rt.rs:49:18
    frame #10: 0x00007fffc5fb6f92 hello`std::rt::lang_start_internal::h74e26f259c28581a + 658
    frame #11: 0x00007fffc5fa1b60 hello`std::rt::lang_start::h71b37c822e80bcc1(main=(hello`hello::main::hc539c34270107337 at main.rs:17), argc=1, argv=0x00007fffffffba68) at rt.rs:48:5
    frame #12: 0x00007fffc5fa300c hello`main + 28
    frame #13: 0x00007fffc5fdac2b hello`libc_start_main_stage2 + 39
    frame #14: 0x00007fffc5fa1917 hello`_start + 22

rustc in this PR:

> cargo +stage2  run -vvv
   Compiling hello v0.1.0 (/tmp/hello)
     Running `CARGO=/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/bin/cargo CARGO_BIN_NAME=hello CARGO_CRATE_NAME=hello CARGO_MANIFEST_DIR=/tmp/hello CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=hello CARGO_PKG_REPOSITORY='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/usr/obj/rust/debug/deps:/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib:/home/han/.rustup/toolchains/stage2/lib' rustc --crate-name hello --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=45f8637d16581c8f -C extra-filename=-45f8637d16581c8f --out-dir /usr/obj/rust/debug/deps -C incremental=/usr/obj/rust/debug/incremental -L dependency=/usr/obj/rust/debug/deps -Z print-link-args -C link-args=-fuse-ld=bfd`
"cc" "-m64" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-Wl,--as-needed" "-nostartfiles" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/rcrt1.o" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crti.o" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtbeginS.o" "-L" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib" "-L" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.13c46qjpqqmsvlt7.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.15v6bxaiph9x009s.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.1ink71ppmx2dzs7x.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.1uw2b3f6g5y40ue3.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.266s81kb0ncdbolo.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.2eue4xcls65wdpx8.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.2p4usy9q2qvnypt6.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.2ps5mydlcnk9396a.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.2skmjjaikvpkfupm.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.30dz11z0aofnesqr.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.3n7b5ehgwrintgct.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.3u88p76iye7ms53s.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.3wogw5s564ur4fuw.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.52b1zbprkak691s9.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.5a02jqibtvw5j173.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.7ufvqzpplts0u9c.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.cc1no8scl2oh3vx.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.hh14nni9hru6433.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.s3hwstby9sigjq.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.uaspq4z5q2q46ku.rcgu.o" "-o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.3xix89bijvttq8xf.rcgu.o" "-Wl,--gc-sections" "-static-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/usr/obj/rust/debug/deps" "-L" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-09863bbdf407aa03.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-1b63f9f92f3108f4.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libminiz_oxide-32d4417712748336.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libadler-6b51cf8ee6f6a418.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libobject-155010ea90fd90f7.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libaddr2line-e5d9a2520c52002b.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libgimli-cb8f304520dcee08.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd_detect-ecccbd55539888eb.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-aa6c405eaec4f6eb.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-e36d8e13091d3283.rlib" "/home/han/rust
/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-3fa5f334a8a4e7aa.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-c9316e944fa41c58.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-d2617c158ec95334.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-a59daf1420a022a8.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage
2/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-eed919ff8c26abfe.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-66552530c797bb9f.rlib" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-e378844bf95f8386.rlib" "-Wl,--end-group" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-47286ef3f3775665.rlib" "-fuse-ld=bfd" "-Wl,-Bdynamic" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtendS.o" "/home/han/rust/obj/build/x86_64-unknown-linux-musl/stage2/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
    Finished dev [unoptimized + debuginfo] target(s) in 7.75s
     Running `/usr/obj/rust/debug/hello`
Hello, world! Backtrace [{ fn: "hello::main", file: "./src/main.rs", line: 18 }, { fn: "core::ops::function::FnOnce::call_once", file: "/checkout/library/core/src/ops/function.rs", line: 227 }, { fn: "std::sys_common::backtrace::__rust_begin_short_backtrace", file: "/checkout/library/std/src/sys_common/backtrace.rs", line: 125 }, { fn: "std::rt::lang_start::{{closure}}", file: "/checkout/library/std/src/rt.rs", line: 49 }, { fn: "std::rt::lang_start_internal" }, { fn: "std::rt::lang_start", file: "/checkout/library/std/src/rt.rs", line: 48 }, { fn: "main" }]
1122334455667788

@Mark-Simulacrum
Copy link
Member

r? @nagisa or @petrochenkov perhaps?

I don't know too much about our crtbegin self-contained objects story, and whether building them ourselves (vs. using the ones in system roots) makes sense.

@petrochenkov
Copy link
Contributor

@12101111
Why the previous strategy of copying the objects doesn't work?
#85310 appears to be caused by objects for a wrong target being copied, why objects for the right target cannot be copied instead?

@12101111
Copy link
Contributor Author

gcc's crtbegin.o/crtend.o is built when gcc bootstrap. Those target use musl-gcc which is a wrapper script, so there is no objects for those target.

And the current copy mechanism don't work well when using clang as C compiler.

@petrochenkov
Copy link
Contributor

Those target use musl-gcc which is a wrapper script, so there is no objects for those target.

musl-gcc will use crtbegin/crtend from gcc that it wraps (these objects belong to compiler rather than libc, unlike crti/crtn/etc), so they can be copied from there.

And the current copy mechanism don't work well when using clang as C compiler.

A working clang toolchain will still link these objects from somewhere, so they can be copied from the same place.

@12101111
Copy link
Contributor Author

musl-gcc will use crtbegin/crtend from gcc that it wraps (these objects belong to compiler rather than libc, unlike crti/crtn/etc), so they can be copied from there.

root@7a1d5318dbef:/build# /musl-armv5te/bin/musl-gcc --target=armv5te-unknown-linux-musleabi -print-file-name=crtbegin.o
arm-linux-gnueabi-gcc: error: unrecognized command line option '--target=armv5te-unknown-linux-musleabi'
/usr/lib/gcc-cross/arm-linux-gnueabi/9/crtbegin.o
root@7a1d5318dbef:/build# /musl-arm/bin/musl-gcc --target=arm-unknown-linux-musleabi -print-file-name=crtbegin.oarm-linux-gnueabi-gcc: error: unrecognized command line option '--target=arm-unknown-linux-musleabi'
/usr/lib/gcc-cross/arm-linux-gnueabi/9/crtbegin.o
root@7a1d5318dbef:/build# /musl-armhf/bin/musl-gcc --target=arm-unknown-linux-musleabihf -print-file-name=crtbegin.oarm-linux-gnueabihf-gcc: error: unrecognized command line option '--target=arm-unknown-linux-musleabihf'
/usr/lib/gcc-cross/arm-linux-gnueabihf/9/crtbegin.o
root@7a1d5318dbef:/build# /musl-armv7hf/bin/musl-gcc --target=armv7-unknown-linux-musleabihf -print-file-name=crtbegin.o
arm-linux-gnueabihf-gcc: error: unrecognized command line option '--target=armv7-unknown-linux-musleabihf'
/usr/lib/gcc-cross/arm-linux-gnueabihf/9/crtbegin.o
root@7a1d5318dbef:/build# readelf -A /usr/lib/gcc-cross/arm-linux-gnueabihf/9/crtbegin.o
Attribute Section: aeabi
File Attributes
  Tag_CPU_name: "7-A"
  Tag_CPU_arch: v7
  Tag_CPU_arch_profile: Application
  Tag_ARM_ISA_use: Yes
  Tag_THUMB_ISA_use: Thumb-2
  Tag_FP_arch: VFPv3-D16
  Tag_ABI_PCS_wchar_t: 4
  Tag_ABI_FP_denormal: Needed
  Tag_ABI_FP_exceptions: Needed
  Tag_ABI_FP_number_model: IEEE 754
  Tag_ABI_align_needed: 8-byte
  Tag_ABI_align_preserved: 8-byte, except leaf SP
  Tag_ABI_enum_size: int
  Tag_ABI_VFP_args: VFP registers
  Tag_ABI_optimization_goals: Aggressive Speed
  Tag_CPU_unaligned_access: v6

They are the same file. dist-various-1 builder use the same GCC arm cross compiler for 4 different arm target by setting CFLAGS.

RUN env \
CC=arm-linux-gnueabi-gcc CFLAGS="-march=armv5te -marm -mfloat-abi=soft" \
CXX=arm-linux-gnueabi-g++ CXXFLAGS="-march=armv5te -marm -mfloat-abi=soft" \
bash musl.sh armv5te && \
env \
CC=arm-linux-gnueabi-gcc CFLAGS="-march=armv6 -marm" \
CXX=arm-linux-gnueabi-g++ CXXFLAGS="-march=armv6 -marm" \
bash musl.sh arm && \
env \
CC=arm-linux-gnueabihf-gcc CFLAGS="-march=armv6 -marm -mfpu=vfp" \
CXX=arm-linux-gnueabihf-g++ CXXFLAGS="-march=armv6 -marm -mfpu=vfp" \
bash musl.sh armhf && \
env \
CC=arm-linux-gnueabihf-gcc CFLAGS="-march=armv7-a" \
CXX=arm-linux-gnueabihf-g++ CXXFLAGS="-march=armv7-a" \
bash musl.sh armv7hf && \

A working clang toolchain will still link these objects from somewhere, so they can be copied from the same place.

Clang don't provided a command line to get the location of compiler-rt. And compiler-rt is optional for clang.

https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/failed.20to.20bootstrap/near/237962952

@nagisa
Copy link
Member

nagisa commented May 26, 2021

r? @petrochenkov

@rust-highfive rust-highfive assigned petrochenkov and unassigned nagisa May 26, 2021
@mati865
Copy link
Contributor

mati865 commented May 26, 2021

Clang don't provided a command line to get the location of compiler-rt.

It does:

# This mingw-w64 toolchain uses compiler-rt by the default
$ /clang64/bin/clang -print-libgcc-file-name
D:/msys64/clang64/lib/clang/12.0.0/lib/windows/libclang_rt.builtins-x86_64.a


# This mingw-w64 toolchain uses libgcc by the default
$ /mingw64/bin/clang -print-libgcc-file-name
D:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/10.3.0/libgcc.a

$ /mingw64/bin/clang -print-libgcc-file-name -rtlib=compiler-rt
D:/msys64/mingw64/lib/clang/12.0.0/lib/windows/libclang_rt.builtins-x86_64.a

@12101111
Copy link
Contributor Author

This is intrinsics, not crtbegin.o/crtend.o. The logic in clang is at
https://github.com/llvm/llvm-project/blob/d28af7c654d8db0b68c175db5ce212d74fb5e9bc/clang/lib/Driver/Driver.cpp#L1829, and the source code shows there is no such option that print TC.getCompilerRT(C.getArgs(), "crtbegin");

A full clang installation should have crtbegin.o in /usr/lib/clang/12.0.0/lib/linux/clang_rt.crtbegin-x86_64.o.
But on my system ( Gentoo Linux ), compiler-rt is optional for clang, so this file may not exsit.
Alpine also don't list compiler-rt as dependence of clang : https://pkgs.alpinelinux.org/package/edge/main/x86/compiler-rt

@petrochenkov
Copy link
Contributor

But on my system ( Gentoo Linux ), compiler-rt is optional for clang, so this file may not exsit.

The files must always exist somewhere, otherwise the C toolchain will be non-functional.
Clang takes gcc's objects if it doesn't ship its own.

My main question is what is simpler, less error-prone and requires less support - implementing the object search for all platforms on which x.py dist is supposed to be run, or building the objects ourselves (with correct flags and defines).

src/bootstrap/native.rs Outdated Show resolved Hide resolved
src/bootstrap/native.rs Outdated Show resolved Hide resolved
@petrochenkov
Copy link
Contributor

This PR will need a backport to beta and stable (if one more dot release is produced), because #82534 landed exactly on the current stable.
Alternatively, a revert of #82534 can be backported (assuming that not linking crtbegin/end at all is better than linking incorrect objects).

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. beta-nominated Nominated for backporting to the compiler in the beta channel. stable-nominated Nominated for backporting to the compiler in the stable channel. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 29, 2021
@petrochenkov
Copy link
Contributor

@12101111
Could you add a test making sure that mixed C/Rust code linked by rustc and using some features requiring crtbegin/end (e.g. init fini arrays) works as expected?

@petrochenkov petrochenkov added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 30, 2021
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented May 30, 2021

📌 Commit 5b44522ff115b8a2ac82217c6a69233988aae84e has been approved by petrochenkov

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 30, 2021
@bors
Copy link
Contributor

bors commented May 30, 2021

⌛ Testing commit 5b44522ff115b8a2ac82217c6a69233988aae84e with merge 5168f9db7cf61a646190550e7fd0c1165898ca49...

@rust-log-analyzer

This comment has been minimized.

@bors

This comment has been minimized.

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels May 30, 2021
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented May 31, 2021

📌 Commit 61c1155 has been approved by petrochenkov

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 31, 2021
@bors
Copy link
Contributor

bors commented May 31, 2021

⌛ Testing commit 61c1155 with merge 7baa7af...

@bors
Copy link
Contributor

bors commented May 31, 2021

☀️ Test successful - checks-actions
Approved by: petrochenkov
Pushing 7baa7af to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 31, 2021
@bors bors merged commit 7baa7af into rust-lang:master May 31, 2021
@rustbot rustbot added this to the 1.54.0 milestone May 31, 2021
@apiraino
Copy link
Contributor

apiraino commented Jun 4, 2021

Beta backport accepted as per compiler team on Zulip

Stable backport declined (discussion on Zulip) since in 2 weeks from now it will land anyway on 1.53

@rustbot label +beta-accepted -stable-nominated

@rustbot rustbot added beta-accepted Accepted for backporting to the compiler in the beta channel. and removed stable-nominated Nominated for backporting to the compiler in the stable channel. labels Jun 4, 2021
@Mark-Simulacrum Mark-Simulacrum removed the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jun 11, 2021
@Mark-Simulacrum Mark-Simulacrum modified the milestones: 1.54.0, 1.53.0 Jun 11, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 11, 2021
…ulacrum

[beta] backports

* Disable the machine outliner by default rust-lang#86020
* Fix incorrect gating of nonterminals in key-value attributes rust-lang#85445
* Build crtbegin.o/crtend.o from source code rust-lang#85395
* Bring back x86_64-sun-solaris target to rustup rust-lang#85252
* Preserve SyntaxContext for invalid/dummy spans in crate metadata rust-lang#85211
* [beta] backport: Remove unsound TrustedRandomAccess implementations rust-lang#86222

r? `@Mark-Simulacrum`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta-accepted Accepted for backporting to the compiler in the beta channel. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
10 participants