Date: Tue, 07 Jun 2022 08:52:40 +0800 From: "Michael Yan Ka Chiu" <nyan@myuji.xyz> To: "Konstantin Belousov" <kostikbel@gmail.com> Cc: freebsd-hackers@freebsd.org Subject: Re: Help needed to get Rust dtrace USDT working on/with FreeBSD linker Message-ID: <717e9669-425a-4aae-9178-99d031d606f2@www.fastmail.com> In-Reply-To: <Yp5YFVBB/UFnvnZM@kib.kiev.ua> References: <f5c27ce0-0dc7-467f-b293-857c74125648@www.fastmail.com> <Ypz9gaGQ/Nqja5kF@kib.kiev.ua> <51c74c5b-731a-445f-be96-0baf6cd0ebe2@www.fastmail.com> <Yp5YFVBB/UFnvnZM@kib.kiev.ua>
next in thread | previous in thread | raw e-mail | index | archive | help
--852b2e739d824269a0a618ba8ae1a5db Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable On Tue, Jun 7, 2022, at 3:40 AM, Konstantin Belousov wrote: > 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 wrot= e: > > > > Hi everyone, > > > >=20 > > > > I=E2=80=99m working on a PR to get the Rust Usdt crate working o= n FreeBSD. This crate > > > > basically allow adding DTrace probes to rust sources by compilin= g the probe during > > > > macro invocation and embed to a custom section (set_dtrace_probe= ) using inline=20 > > > > assembly. > > > >=20 > > > > The problem I am encountering is that the linker will optimize t= he compiled probes > > > > out in the custom section, the only workaround I have found is t= o force the linker to > > > > link all the dead code by invoking `-C link-dead-code=3Dyes`. On= Illumos, the workaround > > > > is to reference another section such that the Illumos linker wil= l not throw the probes=20 > > > > away; however the same workaround does not work on FreeBSD. > > > >=20 > > > > I wonder if there're any tricks similar to the Illumos fix, by p= utting some inline asm there > > > > to trick the linker and not throw out the probes. > > > >=20 > > > > Thanks In advance, > > > > Michael > > > >=20 > > > > References: > > > > The PR: https://github.com/oxidecomputer/usdt/pull/63 > > > > The illumos fix: https://github.com/oxidecomputer/usdt/blob/eac0= fe5f03c3fbf23468ead5cb140f62d51ac3f3/usdt-impl/src/record.rs#L251 > > > >=20 > > > > =20 > > >=20 > > > 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. > > >=20 > > > Anyway, try it? See GNU as documentation for the .section directi= ve, > > > ELF type flags. > >=20 > > Thanks! This seems to solve a big part of the issue. > >=20 > > If i pass =E2=80=9Ccargo:rustc-link-arg=3D-Xlinker=E2=80=9D and =E2=80= =9Ccargo:rustc-link-arg=3D=E2=80=94no-gc-sections=E2=80=9D it does preve= nt the linker from removing the probes. > >=20 > > I am still looking for solutions that does not involve explicit invo= lvement of the flags by the crate consumer, and maybe something not turn= ing gc off entirely but this is a great progress. >=20 > Did you tried the "R" section flag, as I noted above? Yes sir, I am using the =E2=80=9CR=E2=80=9D flag and it works amazingly = well. Sorry I misread your email the first time. --852b2e739d824269a0a618ba8ae1a5db Content-Type: text/html;charset=utf-8 Content-Transfer-Encoding: quoted-printable <!DOCTYPE html><html><head><title></title><style type=3D"text/css">p.Mso= Normal,p.MsoNoSpacing{margin:0}</style></head><body><div><br></div><div>= On Tue, Jun 7, 2022, at 3:40 AM, Konstantin Belousov wrote:<br></div><bl= ockquote type=3D"cite" id=3D"qt" style=3D""><div>On Mon, Jun 06, 2022 at= 03:44:32AM +0800, Michael Yan Ka Chiu wrote:<br></div><div>> On Mon,= Jun 6, 2022, at 3:01 AM, Konstantin Belousov wrote:<br></div><div>> = > On Sun, Jun 05, 2022 at 04:26:37PM +0800, Michael Yan Ka Chiu wrote= :<br></div><div>> > > Hi everyone,<br></div><div>> > >= <br></div><div>> > > I=E2=80=99m working on a PR to get t= he Rust Usdt crate working on FreeBSD. This crate<br></div><div>> >= ; > basically allow adding DTrace probes to rust sources by compiling= the probe during<br></div><div>> > > macro invocation and embe= d to a custom section (set_dtrace_probe) using inline <br></div><di= v>> > > assembly.<br></div><div>> > > <br></div><= div>> > > The problem I am encountering is that the linker will= optimize the compiled probes<br></div><div>> > > out in the cu= stom section, the only workaround I have found is to force the linker to= <br></div><div>> > > link all the dead code by invoking `-C lin= k-dead-code=3Dyes`. On Illumos, the workaround<br></div><div>> > &= gt; is to reference another section such that the Illumos linker will no= t throw the probes <br></div><div>> > > away; however the = same workaround does not work on FreeBSD.<br></div><div>> > >&n= bsp;<br></div><div>> > > I wonder if there're any tricks simila= r to the Illumos fix, by putting some inline asm there<br></div><div>>= ; > > to trick the linker and not throw out the probes.<br></div><= div>> > > <br></div><div>> > > Thanks In advance,= <br></div><div>> > > Michael<br></div><div>> > > = <br></div><div>> > > References:<br></div><div>> > > T= he PR: <a href=3D"https://github.com/oxidecomputer/usdt/pull/63">ht= tps://github.com/oxidecomputer/usdt/pull/63</a><br></div><div>> > = > The illumos fix: <a href=3D"https://github.com/oxidecomputer/u= sdt/blob/eac0fe5f03c3fbf23468ead5cb140f62d51ac3f3/usdt-impl/src/record.r= s#L251">https://github.com/oxidecomputer/usdt/blob/eac0fe5f03c3fbf23468e= ad5cb140f62d51ac3f3/usdt-impl/src/record.rs#L251</a><br></div><div>> = > > <br></div><div>> > > <br></div><div>&= gt; > <br></div><div>> > GNU as seems to gain support for = the "R" flag for sections, which should<br></div><div>> > prevent = them from linker GC. Not sure if llvm toolchain has this, it<br></= div><div>> > requires both as and lld to recognize the flag.<br></= div><div>> > <br></div><div>> > Anyway, try it? S= ee GNU as documentation for the .section directive,<br></div><div>> &= gt; ELF type flags.<br></div><div>> <br></div><div>> Thanks! = This seems to solve a big part of the issue.<br></div><div>> <br= ></div><div>> If i pass =E2=80=9Ccargo:rustc-link-arg=3D-Xlinker=E2=80= =9D and =E2=80=9Ccargo:rustc-link-arg=3D=E2=80=94no-gc-sections=E2=80=9D= it does prevent the linker from removing the probes.<br></div><div>>= <br></div><div>> I am still looking for solutions that does not= involve explicit involvement of the flags by the crate consumer, and ma= ybe something not turning gc off entirely but this is a great progress.<= br></div><div><br></div><div>Did you tried the "R" section flag, as I no= ted above?<br></div></blockquote><div><br></div><div>Yes sir, I am using= the =E2=80=9CR=E2=80=9D flag and it works amazingly well. Sorry I misre= ad your email the first time.</div></body></html> --852b2e739d824269a0a618ba8ae1a5db--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?717e9669-425a-4aae-9178-99d031d606f2>