|
| 1 | +// compiler-builtins weakly defines the f32/f64 math symbols (its `full_availability` list) so |
| 2 | +// `no_std` targets without a system libm still work. A staticlib that records `-lm` must omit |
| 3 | +// them so libm's strong definitions win at the final link (#142119); the no-`-lm` control keeps |
| 4 | +// its weak fallbacks. `only-gnu` because only gnu libm (glibc) provides these symbols. |
| 5 | + |
| 6 | +//@ only-gnu |
| 7 | +//@ ignore-cross-compile |
| 8 | + |
| 9 | +use std::collections::HashSet; |
| 10 | + |
| 11 | +use run_make_support::object::read::archive::ArchiveFile; |
| 12 | +use run_make_support::object::read::elf::{FileHeader as _, SectionHeader as _, Sym as _}; |
| 13 | +use run_make_support::object::{Endianness, elf}; |
| 14 | +use run_make_support::{rfs, rustc, static_lib_name}; |
| 15 | + |
| 16 | +// Keep in sync with `COMPILER_BUILTINS_LIBM_SYMBOLS` in rustc_codegen_ssa/src/back/link.rs. |
| 17 | +const LIBM_SYMBOLS: &[&str] = &[ |
| 18 | + "cbrtf", |
| 19 | + "ceilf", |
| 20 | + "copysignf", |
| 21 | + "fabsf", |
| 22 | + "fdimf", |
| 23 | + "floorf", |
| 24 | + "fmaf", |
| 25 | + "fmaxf", |
| 26 | + "fminf", |
| 27 | + "fmodf", |
| 28 | + "rintf", |
| 29 | + "roundf", |
| 30 | + "sqrtf", |
| 31 | + "truncf", |
| 32 | + "cbrt", |
| 33 | + "ceil", |
| 34 | + "copysign", |
| 35 | + "fabs", |
| 36 | + "fdim", |
| 37 | + "floor", |
| 38 | + "fma", |
| 39 | + "fmax", |
| 40 | + "fmin", |
| 41 | + "fmod", |
| 42 | + "rint", |
| 43 | + "round", |
| 44 | + "sqrt", |
| 45 | + "trunc", |
| 46 | +]; |
| 47 | + |
| 48 | +fn main() { |
| 49 | + rustc().input("no_libm.rs").crate_type("staticlib").panic("abort").run(); |
| 50 | + let no_libm = static_lib_name("no_libm"); |
| 51 | + let no_libm_defined = defined_global_symbols(&no_libm); |
| 52 | + for name in LIBM_SYMBOLS { |
| 53 | + assert!( |
| 54 | + no_libm_defined.contains(*name), |
| 55 | + "expected weak fallback `{name}` to be kept without `-lm` in `{}`", |
| 56 | + no_libm |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + rustc().input("lib.rs").crate_type("staticlib").panic("abort").run(); |
| 61 | + let with_libm = static_lib_name("lib"); |
| 62 | + let with_libm_defined = defined_global_symbols(&with_libm); |
| 63 | + for name in LIBM_SYMBOLS { |
| 64 | + assert!( |
| 65 | + !with_libm_defined.contains(*name), |
| 66 | + "weak definition `{name}` should be omitted when `-lm` is recorded in `{with_libm}`" |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + // Not a blanket strip: the f16/f128 and integer fallbacks survive. `ceilf16` also guards |
| 71 | + // against matching `ceilf` as a prefix of `ceilf16`. |
| 72 | + assert!( |
| 73 | + with_libm_defined.contains("ceilf16"), |
| 74 | + "compiler-builtins' f16 fallback must survive the omit in `{with_libm}`" |
| 75 | + ); |
| 76 | + assert!( |
| 77 | + with_libm_defined.contains("__floatdidf"), |
| 78 | + "compiler-builtins' integer fallback must survive the omit in `{with_libm}`" |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +/// Every defined global/weak symbol in the archive. The omitted symbols can still appear as |
| 83 | +/// undefined references (e.g. `use_mathf` calling `ceilf`), so filter `SHN_UNDEF` rather than |
| 84 | +/// checking mere presence. |
| 85 | +fn defined_global_symbols(archive_path: &str) -> HashSet<String> { |
| 86 | + let archive_data = rfs::read(archive_path); |
| 87 | + let archive = ArchiveFile::parse(archive_data.as_slice()).unwrap(); |
| 88 | + let mut defined = HashSet::new(); |
| 89 | + |
| 90 | + for member in archive.members() { |
| 91 | + let member = member.unwrap(); |
| 92 | + let data = member.data(archive_data.as_slice()).unwrap(); |
| 93 | + |
| 94 | + if let Ok(header) = elf::FileHeader64::<Endianness>::parse(data) { |
| 95 | + collect_elf_defined(header, data, &mut defined); |
| 96 | + } else if let Ok(header) = elf::FileHeader32::<Endianness>::parse(data) { |
| 97 | + collect_elf_defined(header, data, &mut defined); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + defined |
| 102 | +} |
| 103 | + |
| 104 | +fn collect_elf_defined< |
| 105 | + Elf: run_make_support::object::read::elf::FileHeader<Endian = Endianness>, |
| 106 | +>( |
| 107 | + header: &Elf, |
| 108 | + data: &[u8], |
| 109 | + defined: &mut HashSet<String>, |
| 110 | +) { |
| 111 | + let Ok(endian) = header.endian() else { return }; |
| 112 | + let Ok(sections) = header.sections(endian, data) else { return }; |
| 113 | + |
| 114 | + for (si, section) in sections.enumerate() { |
| 115 | + if section.sh_type(endian) != elf::SHT_SYMTAB { |
| 116 | + continue; |
| 117 | + } |
| 118 | + let Ok(symbols) = run_make_support::object::read::elf::SymbolTable::parse( |
| 119 | + endian, data, §ions, si, section, |
| 120 | + ) else { |
| 121 | + continue; |
| 122 | + }; |
| 123 | + let strtab = symbols.strings(); |
| 124 | + |
| 125 | + for symbol in symbols.symbols() { |
| 126 | + let bind = symbol.st_bind(); |
| 127 | + if bind != elf::STB_GLOBAL && bind != elf::STB_WEAK { |
| 128 | + continue; |
| 129 | + } |
| 130 | + if symbol.st_shndx(endian) == elf::SHN_UNDEF { |
| 131 | + continue; |
| 132 | + } |
| 133 | + let Ok(name_bytes) = symbol.name(endian, strtab) else { continue }; |
| 134 | + if let Ok(name) = str::from_utf8(name_bytes) { |
| 135 | + defined.insert(name.to_string()); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments