From c9519ee8b6cb1ba54b7df1001f7f39f07218d514 Mon Sep 17 00:00:00 2001 From: Romain Malmain Date: Tue, 16 Apr 2024 10:12:31 +0200 Subject: [PATCH] Add RPATH to linker_interceptor.py (#61) * Add check for __LIBAFL_QEMU_CONFIGURE in configure script. * Use regex in linker_interceptor.py to detect shared libraries * Add a rpath section to linkinfo.json * Update configure --- configure | 9 +++++++++ linker_interceptor.py | 27 +++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/configure b/configure index 798f94d85f3..40b22747906 100755 --- a/configure +++ b/configure @@ -1742,6 +1742,15 @@ if test "$tcg" = "enabled"; then fi ) +#### --- Begin LibAFL code --- + +# Remove LibAFL config signature if building manually +if [ -z ${__LIBAFL_QEMU_CONFIGURE+x} ]; then + rm -f libafl_config +fi + +#### --- End LibAFL code --- + if test "$skip_meson" = no; then cross="config-meson.cross.new" meson_quote() { diff --git a/linker_interceptor.py b/linker_interceptor.py index 9285cd7b0a6..4caebaffc08 100755 --- a/linker_interceptor.py +++ b/linker_interceptor.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import subprocess, shutil, json, sys, os +import subprocess, shutil, json, sys, os, re FILTER = ['-shared'] @@ -18,8 +18,14 @@ out_args = [] shareds = [] search = [] +rpath = [] + is_linking_qemu = False +shared_library_pattern = r"^[^-].*/lib(.*)\.so(\.[0-9].*)?(?!rsp)$" +rpath_pattern = r"^'.*,-rpath,(.*)'$" +rpath_link_pattern = r"^.*,-rpath-link,(.*)$" + def process_args(args): global out_args, shareds, search, is_linking_qemu prev_o = False @@ -32,10 +38,18 @@ def process_args(args): continue elif args[i] in FILTER: continue - elif args[i].endswith('.so') and not args[i].startswith('-'): - name = os.path.basename(args[i])[3:-3] # remove prefix and suffix + elif (res := re.match(shared_library_pattern, args[i])) is not None: + name = res.group(1) shareds.append(name) continue + elif (res := re.match(rpath_link_pattern, args[i])) is not None: + rpath_link_path = res.group(1) + search.append(rpath_link_path) + continue + elif (res := re.match(rpath_pattern, args[i])) is not None: + rpath_path = res.group(1) + rpath.append(rpath_path) + continue elif args[i] == '-o': prev_o = True continue @@ -57,9 +71,10 @@ def process_args(args): if is_linking_qemu: with open(OUT, 'w') as f: json.dump({ - 'cmd': out_args, - 'libs': shareds, - 'search': search, + 'cmd': out_args, + 'libs': shareds, + 'search': search, + 'rpath': rpath, }, f, indent=2) r = subprocess.run([cc] + args)