flightlessmango / MangoHud

A Vulkan and OpenGL overlay for monitoring FPS, temperatures, CPU/GPU load and more. Discord: https://discordapp.com/invite/Gj5YmBb
MIT License
6.28k stars 272 forks source link

clang + lld build issues #1240

Open AdelKS opened 6 months ago

AdelKS commented 6 months ago

Hello,

When building with clang + lld linking issues arise

CXX=clang++ CC=clang CXX_LD=lld C_LD=lld meson setup build -Dwith_xnvctrl=disabled
cd build
meson compile

we get these kind of linking errors

[99/101] Linking target src/libMangoHud_dlsym.so
FAILED: src/libMangoHud_dlsym.so 
clang++  -o src/libMangoHud_dlsym.so src/libMangoHud_dlsym.so.p/elfhacks.cpp.o src/libMangoHud_dlsym.so.p/real_dlsym.cpp.o src/libMangoHud_dlsym.so.p/hook_dlsym.cpp.o -Wl,--as-needed -Wl,--no-undefined -fuse-ld=lld -Wl,-O1 -shared -fPIC -Wl,--start-group -Wl,-soname,libMangoHud_dlsym.so src/libMangoHud.a subprojects/imgui-1.89.9/libimgui.a subprojects/spdlog-1.13.0/src/libspdlog.a subprojects/implot-0.16/libimplot.a -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,--exclude-libs,ALL -lGL -Wl,--end-group -static-libstdc++ -Wl,--version-script,/home/adel/Documents/Gethings/MangoHud/src/mangohud.version -pthread
ld.lld: error: version script assignment of 'global' to symbol 'mangohud_find_glx_ptr' failed: symbol not defined
ld.lld: error: version script assignment of 'global' to symbol 'mangohud_find_egl_ptr' failed: symbol not defined
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
[100/101] Linking target src/libMangoHud.so
FAILED: src/libMangoHud.so 
clang++  -o src/libMangoHud.so src/libMangoHud.a.p/vulkan.cpp.o src/libMangoHud.a.p/mesa_util_os_socket.c.o src/libMangoHud.a.p/mesa_util_os_time.c.o src/libMangoHud.a.p/hud_elements.cpp.o src/libMangoHud.a.p/overlay.cpp.o src/libMangoHud.a.p/overlay_params.cpp.o src/libMangoHud.a.p/font.cpp.o src/libMangoHud.a.p/keybinds.cpp.o src/libMangoHud.a.p/font_unispace.c.o src/libMangoHud.a.p/logging.cpp.o src/libMangoHud.a.p/config.cpp.o src/libMangoHud.a.p/gpu.cpp.o src/libMangoHud.a.p/blacklist.cpp.o src/libMangoHud.a.p/file_utils.cpp.o src/libMangoHud.a.p/cpu.cpp.o src/libMangoHud.a.p/memory.cpp.o src/libMangoHud.a.p/iostats.cpp.o src/libMangoHud.a.p/notify.cpp.o src/libMangoHud.a.p/elfhacks.cpp.o src/libMangoHud.a.p/real_dlsym.cpp.o src/libMangoHud.a.p/pci_ids.cpp.o src/libMangoHud.a.p/battery.cpp.o src/libMangoHud.a.p/control.cpp.o src/libMangoHud.a.p/device.cpp.o src/libMangoHud.a.p/amdgpu.cpp.o src/libMangoHud.a.p/intel.cpp.o src/libMangoHud.a.p/msm.cpp.o src/libMangoHud.a.p/nvml.cpp.o src/libMangoHud.a.p/loaders_loader_nvml.cpp.o src/libMangoHud.a.p/loaders_loader_x11.cpp.o src/libMangoHud.a.p/shared_x11.cpp.o src/libMangoHud.a.p/dbus.cpp.o src/libMangoHud.a.p/loaders_loader_dbus.cpp.o src/libMangoHud.a.p/meson-generated_.._.._vk_enum_to_str.c.o -Wl,--as-needed -Wl,--no-undefined -fuse-ld=lld -Wl,-O1 -shared -fPIC -Wl,--start-group -Wl,-soname,libMangoHud.so src/libMangoHud.a subprojects/imgui-1.89.9/libimgui.a subprojects/spdlog-1.13.0/src/libspdlog.a subprojects/implot-0.16/libimplot.a -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,--exclude-libs,ALL -lGL -Wl,--end-group -static-libstdc++ -Wl,--version-script,/home/adel/Documents/Gethings/MangoHud/src/mangohud.version -pthread
ld.lld: error: version script assignment of 'global' to symbol 'dlsym' failed: symbol not defined
ld.lld: error: version script assignment of 'global' to symbol 'mangohud_find_glx_ptr' failed: symbol not defined
ld.lld: error: version script assignment of 'global' to symbol 'mangohud_find_egl_ptr' failed: symbol not defined
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
[101/101] Linking target src/libMangoHud_opengl.so
FAILED: src/libMangoHud_opengl.so 
clang++  -o src/libMangoHud_opengl.so src/libMangoHud_opengl.so.p/gl_glad.c.o src/libMangoHud_opengl.so.p/gl_gl_renderer.cpp.o src/libMangoHud_opengl.so.p/gl_gl_hud.cpp.o src/libMangoHud_opengl.so.p/gl_inject_egl.cpp.o src/libMangoHud_opengl.so.p/loaders_loader_glx.cpp.o src/libMangoHud_opengl.so.p/gl_inject_glx.cpp.o src/libMangoHud_opengl.so.p/hud_elements.cpp.o src/libMangoHud_opengl.so.p/overlay.cpp.o src/libMangoHud_opengl.so.p/overlay_params.cpp.o src/libMangoHud_opengl.so.p/font.cpp.o src/libMangoHud_opengl.so.p/keybinds.cpp.o src/libMangoHud_opengl.so.p/font_unispace.c.o src/libMangoHud_opengl.so.p/logging.cpp.o src/libMangoHud_opengl.so.p/config.cpp.o src/libMangoHud_opengl.so.p/gpu.cpp.o src/libMangoHud_opengl.so.p/blacklist.cpp.o src/libMangoHud_opengl.so.p/file_utils.cpp.o src/libMangoHud_opengl.so.p/cpu.cpp.o src/libMangoHud_opengl.so.p/memory.cpp.o src/libMangoHud_opengl.so.p/iostats.cpp.o src/libMangoHud_opengl.so.p/notify.cpp.o src/libMangoHud_opengl.so.p/elfhacks.cpp.o src/libMangoHud_opengl.so.p/real_dlsym.cpp.o src/libMangoHud_opengl.so.p/pci_ids.cpp.o src/libMangoHud_opengl.so.p/battery.cpp.o src/libMangoHud_opengl.so.p/control.cpp.o src/libMangoHud_opengl.so.p/device.cpp.o src/libMangoHud_opengl.so.p/amdgpu.cpp.o src/libMangoHud_opengl.so.p/intel.cpp.o src/libMangoHud_opengl.so.p/msm.cpp.o src/libMangoHud_opengl.so.p/nvml.cpp.o src/libMangoHud_opengl.so.p/loaders_loader_nvml.cpp.o src/libMangoHud_opengl.so.p/loaders_loader_x11.cpp.o src/libMangoHud_opengl.so.p/shared_x11.cpp.o src/libMangoHud_opengl.so.p/dbus.cpp.o src/libMangoHud_opengl.so.p/loaders_loader_dbus.cpp.o src/libMangoHud_opengl.so.p/mesa_util_os_socket.c.o src/libMangoHud_opengl.so.p/mesa_util_os_time.c.o -Wl,--as-needed -Wl,--no-undefined -fuse-ld=lld -Wl,-O1 -shared -fPIC -Wl,--start-group -Wl,-soname,libMangoHud_opengl.so src/libMangoHud.a subprojects/imgui-1.89.9/libimgui.a subprojects/spdlog-1.13.0/src/libspdlog.a subprojects/implot-0.16/libimplot.a -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,--exclude-libs,ALL -lGL -Wl,--end-group -static-libstdc++ -Wl,--version-script,/home/adel/Documents/Gethings/MangoHud/src/mangohud.version -pthread
ld.lld: error: version script assignment of 'global' to symbol 'dlsym' failed: symbol not defined
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

All these errors seem to come from mangohud.version

# in base
{
  global:
    overlay_GetInstanceProcAddr;
    overlay_GetDeviceProcAddr;
    glX*;
    egl*;
    dlsym;
    mangohud_find_glx_ptr;
    mangohud_find_egl_ptr;
  local: *;
};

I will try to look into it more, the git blame seems to says that @jackun could be the right person to ask

I tried to bisect but I couldn't find a commit that builds without issues for the past several versions.

jackun commented 6 months ago

Try just commenting out version-script or maybe add -Wl,--undefined-version [1]

[1] https://issues.chromium.org/issues/40242425

AdelKS commented 6 months ago

Yeah both suggestions work ! Is there a way to make the symbols visible to the linker though ?

I see that hook_dlsym.cpp is part of the input sources, and it should define the symbol :thinking: Or maybe I am misunderstanding everything here

mangohud_dlsym = shared_library(
    'MangoHud_dlsym',
    files(
      'elfhacks.cpp',
      'real_dlsym.cpp',
      'hook_dlsym.cpp',
    ),
    c_args : [
      pre_args,
      ],
    cpp_args : [
      pre_args,
      ],
    gnu_symbol_visibility : 'hidden',
    dependencies : [dep_dl],
    include_directories : [inc_common],
    link_args : link_args,
    link_with: mangohud_static_lib,
    install_dir : libdir_mangohud,
    install : true
  )
AdelKS commented 6 months ago

Ah okay, I think I am on the right path, I should be able to figure it out

AdelKS commented 6 months ago

Okay, I am not acquainted enough with the code base to understand what's the intent behind the use of the version script and I've never heard of it before x)

This is something that fixes the build issue: basically not use the version script linker arguments for the shared libraries MangoHud, MangoHud_opengl and MangoHud_dlsym

diff --git a/src/meson.build b/src/meson.build
index c776ca3..d53c132 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -159,9 +159,9 @@ if is_unixy
   endif
 endif

-link_args = cc.get_supported_link_arguments(['-Wl,-Bsymbolic-functions', '-Wl,-z,relro', '-Wl,--exclude-libs,ALL', '-lGL', '-static-libstdc++'])
+base_link_args = cc.get_supported_link_arguments(['-Wl,-Bsymbolic-functions', '-Wl,-z,relro', '-Wl,--exclude-libs,ALL', '-lGL', '-static-libstdc++'])
 # meson fails to check version-script so just force add
-link_args += '-Wl,--version-script,@0@'.format(join_paths(meson.current_source_dir(), 'mangohud.version'))
+link_args = base_link_args + '-Wl,--version-script,@0@'.format(join_paths(meson.current_source_dir(), 'mangohud.version'))

 mangohud_static_lib = static_library(
   'MangoHud',
@@ -203,7 +203,7 @@ mangohud_shared_lib = shared_library(
   'MangoHud',
   objects: mangohud_static_lib.extract_all_objects(),
   link_with: mangohud_static_lib,
-  link_args : link_args,
+  link_args : base_link_args,
   install_dir : libdir_mangohud,
   install: true
 )
@@ -236,7 +236,7 @@ mangohud_opengl_shared_lib = shared_library(
     json_dep,
     implot_dep],
   include_directories : [inc_common],
-  link_args : link_args,
+  link_args : base_link_args,
   link_with: mangohud_static_lib,
   install_dir : libdir_mangohud,
   install: true
@@ -259,7 +259,7 @@ if is_unixy
     gnu_symbol_visibility : 'hidden',
     dependencies : [dep_dl],
     include_directories : [inc_common],
-    link_args : link_args,
+    link_args : base_link_args,
     link_with: mangohud_static_lib,
     install_dir : libdir_mangohud,
     install : true
flightlessmango commented 4 months ago

Not sure what potential consequences this would have, does it build fine with gcc and mingw still?

ThisNekoGuy commented 4 months ago

does it build fine with gcc and mingw still

Not on LLVM toolchain systems (LLVM Gentoo; haven't tried mingw because I've never messed with it before but I have tried GCC) but I haven't tried this patch, either, for a similar reason as you. (Also haven't personally tried the patch because the Gentoo GURU/community ebuild for it busted anyway)

AdelKS commented 4 months ago

It builds fine with GCC yeah, Mingw never tried, but since it's GCC it should be fine.

Also haven't personally tried the patch because the Gentoo GURU/community ebuild for it busted anyway

@ThisNekoGuy I don't think it's busted, try again please.

ThisNekoGuy commented 4 months ago

(Also haven't personally tried the patch because the Gentoo GURU/community ebuild for it busted anyway)

Like I said, I would but can't because the only means of doing so for my distro right now is barely maintained.

Edit: Ignore that, that link was a case of user-error; recompiling yields these errors: emerge_mangohud-guru_LLVM-build.log

AdelKS commented 3 months ago
../mangohud-9999/src/hud_elements.h:147:14: error: no template named 'map' in namespace 'std'; did you mean 'max'?

@ThisNekoGuy something is definitely wrong with your build environment if STL classes cannot be found

Here's my build log without the patch

mangohud-build.log

Here's the the log with the patch

mangohud-build-with-patch.log

ThisNekoGuy commented 3 months ago

@AdelKS Don't know how you can tell me its the build environment exactly if there's not much I can actually be doing to change how it behaves besides using Gentoo-provided toolchain USE flags to cause that result. The only difference I can imagine that would be causing this is that you're using libstdc++ which on LLVM profiles I think is still the default(? for some reason on LLVM profiles) when actually going fully LLVM would mean using libc++ (the default-libcxx use flag).

EDIT: Actually, yes, that's exactly what the issue is; found this in your log: -static-libstdc++

AdelKS commented 3 months ago

Okay I understand better now, you are trying to build with libc++, this bug report is specifically without using libc++ which is another beast. I tried building with libc++ and I get the same errors as you.