Code coverage options?

Hi everybody,

I’m attempting to instrument wine programs.

I say attempting because ld.ldd has no -fprofile-instr-generate -fprofile-instr-use option.

How may I workaround this obstacle ?

Thank you for your lights.

ld.ldd doesn’t exist, but assuming you mean ld.lld, it’s a compiler driver flag (clang) not a linker flag.

Sorry for my mistake.

Could you comment this :
$ i686-w64-mingw32-gcc --verbose
clang version 16.0.0 (GitHub - didier31/llvm-project: The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Note: the repository does not accept github pull requests at this moment. Please submit your patches at http://reviews.llvm.org. fd837bf6afceed1cc8931f0301cc79b9a163768f)
Target: i686-w64-windows-gnu
Thread model: posix
InstalledDir: /home/didier/llvm-mingw/b

$ i686-w64-mingw32-gcc -c -o programs/start/i386-windows/start.o …/…/git/wine/programs/start/start.c -Iprograms/start -I…/…/git/wine/programs/start -Iinclude -I…/…/git/wine/include -I…/…/git/wine/include/msvcrt -D_UCRT -DWINESRC -D__WINE_PE_BUILD -Wall -target i686-w64-mingw32 -fuse-ld=lld -fno-strict-aliasing -Wdeclaration-after-statement -Wempty-body -Wignored-qualifiers -Winit-self -Wno-pragma-pack -Wstrict-prototypes -Wtype-limits -Wunused-but-set-parameter -Wvla -Wwrite-strings -Wpointer-arith -Wabsolute-value -fno-omit-frame-pointer -g -fdebug-macro -ggdb -glldb -fprofile-instr-generate

$ “/home/didier/llvm-mingw/bin/ld.lld” -m i386pe --subsystem console -Bdynamic -o programs/start/i386-windows/start.exe -L./dlls -L/home/didier/llvm-mingw/i686-w64-mingw32/lib -L/home/didier/llvm-mingw/i686-w64-mingw32/mingw/lib -L/usr/home/didier/llvm-mingw/lib/clang/16/lib/windows --disable-stdcall-fixup --file-alignment 0x1000 --nxcompat --entry _wmainCRTStartup tmp6467abc5/start-00000000.spec.o programs/start/i386-windows/start.o dlls/shell32/i386-windows/libshell32.a dlls/user32/i386-windows/libuser32.a dlls/winecrt0/i386-windows/libwinecrt0.a dlls/ucrtbase/i386-windows/libucrtbase.a dlls/kernel32/i386-windows/libkernel32.a dlls/ntdll/i386-windows/libntdll.a /usr/home/didier/llvm-mingw/lib/clang/16/lib/windows/libclang_rt.builtins-i386.a
ld.lld: error: undefined symbol: ___llvm_profile_runtime
>>> referenced by programs/start/i386-windows/start.o:(___llvm_profile_runtime_user)
clang-16: error: linker command failed with exit code 1 (use -v to see invocation)

If you invoke clang to link your program, clang should add libclang_rt.profile-i386.a to the link line automatically. If you’re invoking the linker directly, you might need to explicitly add it.

1 Like

I mean, this is kind of cursed, can you not invoke it as clang rather than gcc instead? Won’t make a difference to how it behaves, but makes it far less confusing… I initially thought you were compiling programs with GCC and then manually linking with LLD.

But yeah, as Eli said, you need libclang_rt.profile-i386.a if you want to do profiling. Ideally you’d use clang to link, not manually call the linker (I know lots of people do it, but it’s not a good idea in general, just because people do it doesn’t mean it’s right… I could write a long rant about that one), but if you really have to then add the library alongside the builtins.

1 Like

I mean the invocation of genuine command lines generated by wine build system (winegcc and winebuild).

That’s true I’ve tried to replace lld by clang but some lld options were not recognized by clang.

I’m thankful of your answers : it should unblock me.

Thank you, you two.

When you’re invoking the linker via the compiler driver (gcc or clang), you can prefix linker arguments with -Wl, to pass them directly to the linker, e.g. -Wl,--foo=bar would pass --foo=bar to the linker, or -Wl,--abc,--xyz would pass --abc --xyz to the linker.

2 Likes

I’ve added -Wl,-lclang_rt.profile-i386 -Wl,-lmsvcr120 -Wl,-lucrt -Wl,-lmingwex in order to link successfully.

Now, it appears instrumentation in start.exe is inactive. (i.e: instrumentation is not triggered during execution through wine) … :tired_face:

You don’t need -Wl,-l, just -l works. Only a subset of options need to be passed to the linker directly via -Wl.

1 Like

Also, like we said, -fprofile-instr-generate should automatically add the library if you specify that flag at link time; it’s not great practice to be manually specifying compiler-rt libraries like that.

__llvm_profile_initialize is the function that’s meant to be called, by virtue of llvm-project/InstrProfilingRuntime.cpp at 94063cac47fb2d07e92d6922761e182f1e657764 · llvm/llvm-project · GitHub. LLVM’s InstrProfiling pass is then supposed to make a reference to it (llvm-project/InstrProfiling.cpp at 94063cac47fb2d07e92d6922761e182f1e657764 · llvm/llvm-project · GitHub) in code compiled with profiling to ensure that file gets included and runs the function; this is what the ___llvm_profile_runtime symbol from your error before was referencing. I guess you should get out a debugger on your start.exe to see if the relevant functions are being called.

1 Like

I see but the build process of wine invokes linker, ld.lld through winegcc :

winegcc -o programs/start/i386-windows/start.exe --wine-objdir . --winebuild …/wine-8.8-win64-build//tools/winebuild/winebuild -b i686-w64-mingw32 -Wl,–wine-builtin -mconsole -municode programs/start/i386-windows/start.o programs/start/start.res dlls/shell32/i386-windows/libshell32.a dlls/user32/i386-windows/libuser32.a dlls/winecrt0/i386-windows/libwinecrt0.a dlls/ucrtbase/i386-windows/libucrtbase.a dlls/kernel32/i386-windows/libkernel32.a dlls/ntdll/i386-windows/libntdll.a -Wl,–disable-stdcall-fixup -Wl,-lclang_rt.profile-i386 -Wl,-lmsvcr120 -Wl,-lucrt -Wl,-lmingwex
:relieved:

Well I don’t know what winegcc does. You should probably go ask wine people for help with wine’s tooling, not LLVM people. We can only tell you how to use LLVM’s tools. It’s up to you and them to figure out how that fits into wine’s tooling.

1 Like

Sorry for disturbing but my initial question deals with llvm coverage options and ld.lld interoperability provided that the build system of wine makes use of ld.lld.

Thank you again.
Have a nice week-end.