Skip to content

Conversation

@xclaesse
Copy link
Member

@xclaesse xclaesse commented Nov 6, 2023

-l static= expects a filename and not a relative path, rustc already has -L flags to find the library. Weirdly passing a path to -l dylib= works fine, but better never do it.

Fixes: #12484

@xclaesse xclaesse requested a review from jpakkane as a code owner November 6, 2023 19:11
@xclaesse xclaesse force-pushed the rust-link-regress branch 2 times, most recently from 2b23c3d to f469bd4 Compare November 6, 2023 21:34
@gfxstrand
Copy link
Contributor

This fixes the Mesa build with Meson 1.3.0rc2.

@xclaesse xclaesse force-pushed the rust-link-regress branch 2 times, most recently from 55c1352 to b3aae77 Compare November 8, 2023 18:20
@xclaesse xclaesse force-pushed the rust-link-regress branch 2 times, most recently from 5244308 to c254af1 Compare November 8, 2023 21:39
CustomTarget and CustomTargetIndex often have to be special cased
because they are not subclass of BuildTarget. It's easier to introduce a
dummy base class.

This fix recursive functions over link targets that might encouters
libraries built as CustomTarget.
@xclaesse xclaesse force-pushed the rust-link-regress branch 2 times, most recently from 2ba6dc4 to 5170ef3 Compare November 9, 2023 19:35
Pass link arguments directly down to linker by using `-C link-args=`
instead of letting rustc/linker resolve `-l` arguments. This solves
problems with e.g. +verbatim not being portable. Note that we also pass
`-l` args as `-Clink-args=-l` because rustc would otherwise reorder
arguments and put `-lstdc++` before `-Clink-args=libfoo++.a`.

However, when building a rlib/staticlib we should still use `-l`
arguments because that allows rustc to bundle static libraries we
link-whole. In that case, since there is no platform specific dynamic
linker, +verbatim works.

This also fix installed staticlib that now bundle uninstalled static
libraries it links to (recursively). This is done by putting them all
into self.link_whole_targets instead of putting their objects into
self.objects, and let rustc do the bundling. This has the extra
advantage that rustc can bundle static libries built with CustomTarget.

Disable bundling in all other cases, otherwise we could end up with
duplicated objects in static libraries, in diamond dependency graph
case.

Fixes: mesonbuild#12484
CompilerArgs would otherwise dedup and reorder them.
@xclaesse xclaesse added this to the 1.3.0 milestone Nov 9, 2023
@jpakkane
Copy link
Member

It this now ready for merging?

@xclaesse
Copy link
Member Author

Yes

@jpakkane jpakkane merged commit 8f89ce8 into mesonbuild:master Nov 12, 2023
@jpakkane
Copy link
Member

Fails when building under Debian Sid pbuilder:

All failures:
  -> rust: 13 external c dependencies    (static=False method=cmake)
  -> rust: 13 external c dependencies    (static=False method=pkg-config)
  -> rust: 13 external c dependencies    (static=False method=system)
  -> rust: 13 external c dependencies    (static=True method=system)
  -> rust: 20 rust and cpp    (b_vscrt=from_buildtype)
  -> rust: 20 rust and cpp    (b_vscrt=md)
  -> rust: 20 rust and cpp    (b_vscrt=mdd)
  -> rust: 20 rust and cpp    (b_vscrt=mt)
  -> rust: 20 rust and cpp    (b_vscrt=mtd)
  -> rust: 20 rust and cpp    (b_vscrt=none)
  -> rust: 20 rust and cpp    (b_vscrt=static_from_buildtype)

One of the error messages:

rustc -C linker=cc --color=always -C debug-assertions=yes -C overflow-checks=no --crate-type bin -g --crate-name main --emit dep-info=main.d --em
it link=main --out-dir main.p -C metadata=main@exe -Clink-arg=libcpp.a -L. -Clink-arg=-L/usr/lib/gcc/i686-linux-gnu/13 -Clink-arg=-L/usr/lib/gcc/
i686-linux-gnu/13/../../../i386-linux-gnu -Clink-arg=-L/usr/lib/i386-linux-gnu -Clink-arg=-L/usr/lib/gcc/i686-linux-gnu/13/../../../../lib -Clink
-arg=-L/usr/lib -Clink-arg=-L/lib/i386-linux-gnu -Clink-arg=-L/lib/../lib -Clink-arg=-L/usr/lib/../lib -Clink-arg=-L/usr/lib/gcc/i686-linux-gnu/1
3/../../.. -Clink-arg=-L/lib -Clink-arg=-lstdc++ '../test cases/rust/20 rust and cpp/main.rs'
error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/usr/lib/rustlib/i686-unknown-linux-gnu/bin:/usr/sbin:/usr/bin:/sbin:/bin" VSLANG="1033" "cc" "-m32" "/tmp/rustcy68F2x
/symbols.o" "main.p/main.main.ffd924bf-cgu.0.rcgu.o" "main.p/main.main.ffd924bf-cgu.1.rcgu.o" "main.p/main.main.ffd924bf-cgu.10.rcgu.o" "main.p/m
ain.main.ffd924bf-cgu.2.rcgu.o" "main.p/main.main.ffd924bf-cgu.3.rcgu.o" "main.p/main.main.ffd924bf-cgu.4.rcgu.o" "main.p/main.main.ffd924bf-cgu.
5.rcgu.o" "main.p/main.main.ffd924bf-cgu.6.rcgu.o" "main.p/main.main.ffd924bf-cgu.7.rcgu.o" "main.p/main.main.ffd924bf-cgu.8.rcgu.o" "main.p/main
.main.ffd924bf-cgu.9.rcgu.o" "main.p/main.4ek1sc2eddz1nvoz.rcgu.o" "-Wl,--as-needed" "-L" "." "-L" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib"
"-Wl,-Bstatic" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libstd-9eb1d1dded28896e.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libpanic_u
nwind-b9975aa5c12eaccf.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libobject-7caf3a8d5b4f043b.rlib" "/usr/lib/rustlib/i686-unknown-linux-g
nu/lib/libmemchr-3b2966b4a4f5d67e.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libaddr2line-34da0594e576609d.rlib" "/usr/lib/rustlib/i686-u
nknown-linux-gnu/lib/libgimli-4e36f80f23111b27.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/librustc_demangle-f2ce595e59ab042e.rlib" "/usr/
lib/rustlib/i686-unknown-linux-gnu/lib/libstd_detect-41160129bd7723c4.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libhashbrown-7f7b27cd909
266e8.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libminiz_oxide-ca2cb570f5eebbb2.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libad
ler-61b1de8adc09be95.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/librustc_std_workspace_alloc-b244d37ff37bedd7.rlib" "/usr/lib/rustlib/i68
6-unknown-linux-gnu/lib/libunwind-b162b23e58e42aac.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libcfg_if-6d47de0badd76ebc.rlib" "/usr/lib/
rustlib/i686-unknown
-linux-gnu/lib/liblibc-abc97f6f160793e3.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/liballoc-909d2cf54addb281.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/librustc_std_workspace_core-2f4f74014cd40ee1.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libcore-c5dc2261107d87e4.rlib" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib/libcompiler_builtins-7ec1d5128420f267.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/usr/lib/rustlib/i686-unknown-linux-gnu/lib" "-o" "main" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "libcpp.a" "-L/usr/lib/gcc/i686-linux-gnu/13" "-L/usr/lib/gcc/i686-linux-gnu/13/../../../i386-linux-gnu" "-L/usr/lib/i386-linux-gnu" "-L/usr/lib/gcc/i686-linux-gnu/13/../../../../lib" "-L/usr/lib" "-L/lib/i386-linux-gnu" "-L/lib/../lib" "-L/usr/lib/../lib" "-L/usr/lib/gcc/i686-linux-gnu/13/../../.." "-L/lib" "-lstdc++"
  = note: /usr/bin/ld: libcpp.a.p/lib.cpp.o: in function `lib_length':
          ./b dccb8dea1f/../test cases/rust/20 rust and cpp/lib.cpp:18:(.text+0xfe): undefined reference to `__stack_chk_fail_local'
          /usr/bin/ld: main: hidden symbol `__stack_chk_fail_local' isn't defined
          /usr/bin/ld: final link failed: bad value
          collect2: error: ld returned 1 exit status

  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: aborting due to previous error

@eli-schwartz
Copy link
Member

That looks like the rust linker is not correctly handling system linking the way the system linker does, and neglects to load e.g. /usr/lib64/libc_nonshared.a

@xclaesse xclaesse deleted the rust-link-regress branch November 12, 2023 20:13
@xclaesse
Copy link
Member Author

@jpakkane It's weird, none of the flag -Clink-arg= seems to get passed down to linker command. Which version of rustc is that, and is there a way to reproducde?

@eli-schwartz
Copy link
Member

@xclaesse
Copy link
Member Author

I tried running ./run_project_tests.py --only rust in a debian sid docker and it passed.

@jpakkane
Copy link
Member

I tried that also now, but in this case test 13 fails. Rerunning the full packaging test.

@jpakkane
Copy link
Member

BTW this is on 32 bit x86, not x64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rust: Linking with C static libraries in subdirectories fails

5 participants