Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 6 Jun 2022 22:40:05 +0300
From:      Konstantin Belousov <kostikbel@gmail.com>
To:        Michael Yan Ka Chiu <nyan@myuji.xyz>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Help needed to get Rust dtrace USDT working on/with FreeBSD linker
Message-ID:  <Yp5YFVBB/UFnvnZM@kib.kiev.ua>
In-Reply-To: <51c74c5b-731a-445f-be96-0baf6cd0ebe2@www.fastmail.com>
References:  <f5c27ce0-0dc7-467f-b293-857c74125648@www.fastmail.com> <Ypz9gaGQ/Nqja5kF@kib.kiev.ua> <51c74c5b-731a-445f-be96-0baf6cd0ebe2@www.fastmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Jun 06, 2022 at 03:44:32AM +0800, Michael Yan Ka Chiu wrote:
> On Mon, Jun 6, 2022, at 3:01 AM, Konstantin Belousov wrote:
> > On Sun, Jun 05, 2022 at 04:26:37PM +0800, Michael Yan Ka Chiu wrote:
> > > Hi everyone,
> > > 
> > > I’m working on a PR to get the Rust Usdt crate working on FreeBSD. This crate
> > > basically allow adding DTrace probes to rust sources by compiling the probe during
> > > macro invocation and embed to a custom section (set_dtrace_probe) using inline 
> > > assembly.
> > > 
> > > The problem I am encountering is that the linker will optimize the compiled probes
> > > out in the custom section, the only workaround I have found is to force the linker to
> > > link all the dead code by invoking `-C link-dead-code=yes`. On Illumos, the workaround
> > > is to reference another section such that the Illumos linker will not throw the probes 
> > > away; however the same workaround does not work on FreeBSD.
> > > 
> > > I wonder if there're any tricks similar to the Illumos fix, by putting some inline asm there
> > > to trick the linker and not throw out the probes.
> > > 
> > > Thanks In advance,
> > > Michael
> > > 
> > > References:
> > > The PR: https://github.com/oxidecomputer/usdt/pull/63
> > > The illumos fix: https://github.com/oxidecomputer/usdt/blob/eac0fe5f03c3fbf23468ead5cb140f62d51ac3f3/usdt-impl/src/record.rs#L251
> > > 
> > >  
> > 
> > GNU as seems to gain support for the "R" flag for sections, which should
> > prevent them from linker GC.  Not sure if llvm toolchain has this, it
> > requires both as and lld to recognize the flag.
> > 
> > Anyway, try it?  See GNU as documentation for the .section directive,
> > ELF type flags.
> 
> Thanks! This seems to solve a big part of the issue.
> 
> If i pass “cargo:rustc-link-arg=-Xlinker” and “cargo:rustc-link-arg=—no-gc-sections” it does prevent the linker from removing the probes.
> 
> I am still looking for solutions that does not involve explicit involvement of the flags by the crate consumer, and maybe something not turning gc off entirely but this is a great progress.

Did you tried the "R" section flag, as I noted above?



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Yp5YFVBB/UFnvnZM>