Skip to content

Commit 936b32a

Browse files
committed
Auto merge of rust-lang#33359 - tamird:consolidate-musl, r=alexcrichton
rustc_back: use a common musl base extracted from rust-lang#33327. cc rust-lang#33189. r? @alexcrichton
2 parents 0d61bb3 + 728cd03 commit 936b32a

File tree

4 files changed

+74
-73
lines changed

4 files changed

+74
-73
lines changed

src/librustc_back/target/i686_unknown_linux_musl.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// See x86_64_unknown_linux_musl for explanation of arguments
12-
1311
use target::Target;
1412

1513
pub fn target() -> Target {
16-
let mut base = super::linux_base::opts();
14+
let mut base = super::linux_musl_base::opts();
1715
base.cpu = "pentium4".to_string();
1816
base.pre_link_args.push("-m32".to_string());
1917
base.pre_link_args.push("-Wl,-melf_i386".to_string());
2018

21-
base.pre_link_args.push("-nostdlib".to_string());
22-
base.pre_link_args.push("-static".to_string());
23-
base.pre_link_args.push("-Wl,--eh-frame-hdr".to_string());
24-
25-
base.pre_link_args.push("-Wl,-(".to_string());
26-
base.post_link_args.push("-Wl,-)".to_string());
27-
28-
base.pre_link_objects_exe.push("crt1.o".to_string());
29-
base.pre_link_objects_exe.push("crti.o".to_string());
30-
base.post_link_objects.push("crtn.o".to_string());
31-
32-
base.dynamic_linking = false;
33-
base.has_rpath = false;
34-
base.position_independent_executables = false;
35-
3619
Target {
3720
llvm_target: "i686-unknown-linux-musl".to_string(),
3821
target_endian: "little".to_string(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use target::TargetOptions;
12+
13+
pub fn opts() -> TargetOptions {
14+
let mut base = super::linux_base::opts();
15+
16+
// Make sure that the linker/gcc really don't pull in anything, including
17+
// default objects, libs, etc.
18+
base.pre_link_args.push("-nostdlib".to_string());
19+
base.pre_link_args.push("-static".to_string());
20+
21+
// At least when this was tested, the linker would not add the
22+
// `GNU_EH_FRAME` program header to executables generated, which is required
23+
// when unwinding to locate the unwinding information. I'm not sure why this
24+
// argument is *not* necessary for normal builds, but it can't hurt!
25+
base.pre_link_args.push("-Wl,--eh-frame-hdr".to_string());
26+
27+
// There's a whole bunch of circular dependencies when dealing with MUSL
28+
// unfortunately. To put this in perspective libc is statically linked to
29+
// liblibc and libunwind is statically linked to libstd:
30+
//
31+
// * libcore depends on `fmod` which is in libc (transitively in liblibc).
32+
// liblibc, however, depends on libcore.
33+
// * compiler-rt has personality symbols that depend on libunwind, but
34+
// libunwind is in libstd which depends on compiler-rt.
35+
//
36+
// Recall that linkers discard libraries and object files as much as
37+
// possible, and with all the static linking and archives flying around with
38+
// MUSL the linker is super aggressively stripping out objects. For example
39+
// the first case has fmod stripped from liblibc (it's in its own object
40+
// file) so it's not there when libcore needs it. In the second example all
41+
// the unused symbols from libunwind are stripped (each is in its own object
42+
// file in libstd) before we end up linking compiler-rt which depends on
43+
// those symbols.
44+
//
45+
// To deal with these circular dependencies we just force the compiler to
46+
// link everything as a group, not stripping anything out until everything
47+
// is processed. The linker will still perform a pass to strip out object
48+
// files but it won't do so until all objects/archives have been processed.
49+
base.pre_link_args.push("-Wl,-(".to_string());
50+
base.post_link_args.push("-Wl,-)".to_string());
51+
52+
// When generating a statically linked executable there's generally some
53+
// small setup needed which is listed in these files. These are provided by
54+
// a musl toolchain and are linked by default by the `musl-gcc` script. Note
55+
// that `gcc` also does this by default, it just uses some different files.
56+
//
57+
// Each target directory for musl has these object files included in it so
58+
// they'll be included from there.
59+
base.pre_link_objects_exe.push("crt1.o".to_string());
60+
base.pre_link_objects_exe.push("crti.o".to_string());
61+
base.post_link_objects.push("crtn.o".to_string());
62+
63+
// MUSL support doesn't currently include dynamic linking, so there's no
64+
// need for dylibs or rpath business. Additionally `-pie` is incompatible
65+
// with `-static`, so we can't pass `-pie`.
66+
base.dynamic_linking = false;
67+
base.has_rpath = false;
68+
base.position_independent_executables = false;
69+
70+
base
71+
}

src/librustc_back/target/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ mod bitrig_base;
5656
mod dragonfly_base;
5757
mod freebsd_base;
5858
mod linux_base;
59+
mod linux_musl_base;
5960
mod openbsd_base;
6061
mod netbsd_base;
6162
mod solaris_base;

src/librustc_back/target/x86_64_unknown_linux_musl.rs

+1-55
Original file line numberDiff line numberDiff line change
@@ -11,64 +11,10 @@
1111
use target::Target;
1212

1313
pub fn target() -> Target {
14-
let mut base = super::linux_base::opts();
14+
let mut base = super::linux_musl_base::opts();
1515
base.cpu = "x86-64".to_string();
1616
base.pre_link_args.push("-m64".to_string());
1717

18-
// Make sure that the linker/gcc really don't pull in anything, including
19-
// default objects, libs, etc.
20-
base.pre_link_args.push("-nostdlib".to_string());
21-
base.pre_link_args.push("-static".to_string());
22-
23-
// At least when this was tested, the linker would not add the
24-
// `GNU_EH_FRAME` program header to executables generated, which is required
25-
// when unwinding to locate the unwinding information. I'm not sure why this
26-
// argument is *not* necessary for normal builds, but it can't hurt!
27-
base.pre_link_args.push("-Wl,--eh-frame-hdr".to_string());
28-
29-
// There's a whole bunch of circular dependencies when dealing with MUSL
30-
// unfortunately. To put this in perspective libc is statically linked to
31-
// liblibc and libunwind is statically linked to libstd:
32-
//
33-
// * libcore depends on `fmod` which is in libc (transitively in liblibc).
34-
// liblibc, however, depends on libcore.
35-
// * compiler-rt has personality symbols that depend on libunwind, but
36-
// libunwind is in libstd which depends on compiler-rt.
37-
//
38-
// Recall that linkers discard libraries and object files as much as
39-
// possible, and with all the static linking and archives flying around with
40-
// MUSL the linker is super aggressively stripping out objects. For example
41-
// the first case has fmod stripped from liblibc (it's in its own object
42-
// file) so it's not there when libcore needs it. In the second example all
43-
// the unused symbols from libunwind are stripped (each is in its own object
44-
// file in libstd) before we end up linking compiler-rt which depends on
45-
// those symbols.
46-
//
47-
// To deal with these circular dependencies we just force the compiler to
48-
// link everything as a group, not stripping anything out until everything
49-
// is processed. The linker will still perform a pass to strip out object
50-
// files but it won't do so until all objects/archives have been processed.
51-
base.pre_link_args.push("-Wl,-(".to_string());
52-
base.post_link_args.push("-Wl,-)".to_string());
53-
54-
// When generating a statically linked executable there's generally some
55-
// small setup needed which is listed in these files. These are provided by
56-
// a musl toolchain and are linked by default by the `musl-gcc` script. Note
57-
// that `gcc` also does this by default, it just uses some different files.
58-
//
59-
// Each target directory for musl has these object files included in it so
60-
// they'll be included from there.
61-
base.pre_link_objects_exe.push("crt1.o".to_string());
62-
base.pre_link_objects_exe.push("crti.o".to_string());
63-
base.post_link_objects.push("crtn.o".to_string());
64-
65-
// MUSL support doesn't currently include dynamic linking, so there's no
66-
// need for dylibs or rpath business. Additionally `-pie` is incompatible
67-
// with `-static`, so we can't pass `-pie`.
68-
base.dynamic_linking = false;
69-
base.has_rpath = false;
70-
base.position_independent_executables = false;
71-
7218
Target {
7319
llvm_target: "x86_64-unknown-linux-musl".to_string(),
7420
target_endian: "little".to_string(),

0 commit comments

Comments
 (0)