From 7f64cab390e376806922e7878af2f7a313fb8caa Mon Sep 17 00:00:00 2001 From: Amine Alami <43780877+Alami-Amine@users.noreply.github.com> Date: Thu, 12 Sep 2024 13:43:01 +0200 Subject: [PATCH] [OSS-Fuzz] Fix static library linking needed for OSS-Fuzz (#35523) * getting transitive libraries for static linking * Fixing commit * integrating comments --- build/config/linux/pkg-config.py | 10 +++++++++- build/config/linux/pkg_config.gni | 13 +++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/build/config/linux/pkg-config.py b/build/config/linux/pkg-config.py index f44667242de807..73a33e4f0e8694 100755 --- a/build/config/linux/pkg-config.py +++ b/build/config/linux/pkg-config.py @@ -142,6 +142,8 @@ def main(): dest='dridriverdir') parser.add_option('--version-as-components', action='store_true', dest='version_as_components') + parser.add_option('--static', action='store_true', + dest='static') (options, args) = parser.parse_args() # Make a list of regular expressions to strip out. @@ -203,7 +205,13 @@ def main(): sys.stdout.write(dridriverdir.strip()) return - cmd = [options.pkg_config, "--cflags", "--libs"] + args + cmd = [options.pkg_config, "--cflags", "--libs"] + + if options.static: + cmd.append("--static") + + cmd.extend(args) + if options.debug: sys.stderr.write('Running: %s\n' % ' '.join(cmd)) diff --git a/build/config/linux/pkg_config.gni b/build/config/linux/pkg_config.gni index d6892d97fb976f..04419ecee24260 100644 --- a/build/config/linux/pkg_config.gni +++ b/build/config/linux/pkg_config.gni @@ -137,10 +137,19 @@ template("pkg_config") { # Link libraries statically for OSS-Fuzz fuzzer build if (oss_fuzz) { + # Output libs needed for static linking (direct + transitive/non-direct libs), we will re-execute the script to get those libs + args += [ "--static" ] + pkgresult_static = exec_script(pkg_config_script, args, "value") libs = [] ldflags = [ "-Wl,-Bstatic" ] - foreach(lib, pkgresult[3]) { - ldflags += [ "-l$lib" ] + foreach(lib, pkgresult_static[3]) { + # dl(dynamic loading) lib is not needed for linking statically and its presence triggers errors. + # example of errors: + # ld.lld: error: undefined symbol: __dlsym + # >>> referenced by dlsym.o:(dlsym) in archive /lib/x86_64-linux-gnu/libdl.a + if (lib != "dl") { + ldflags += [ "-l$lib" ] + } } ldflags += [ "-Wl,-Bdynamic" ] lib_dirs = pkgresult[4]