From 5635365d394cbd29812a06bd5f7d1b0b53de5d0f Mon Sep 17 00:00:00 2001 From: QinlinChen Date: Mon, 5 Jul 2021 21:55:07 +0800 Subject: [PATCH] afl-clang-fast: modify edit_params() to handle '-r' parameters for partial linking There is a case that AFL cannot compile busybox. It is because the building system of busybox uses the partial linking feature of ld while the afl-clang-fast cannot handle such case. More specifically, the building system of busybox first merges several relocatable object file into a new relocatable file using the '-r' option, where the afl-llvm-rt.o added by afl-clang-fast is also merged, so the new relocatable file contains the symbols from afl-llvm-rt.o. At the final linking step, the new relocatable file is linked with afl-llvm-rt.o (added again by afl-clang-fast) into the executable file, but both files contain the definitions of the symbols from afl-llvm-rt.o. As a result, the compiler complains that and stop the building process. I fix this by restraining afl-clang-fast from adding the 'afl-llvm-rt.o' parameter when seeing the '-r', '-Wl,-r', or '-Wl,-i' in the cmd line. --- llvm_mode/afl-clang-fast.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/llvm_mode/afl-clang-fast.c b/llvm_mode/afl-clang-fast.c index 2104a1244..fdfc54f4d 100644 --- a/llvm_mode/afl-clang-fast.c +++ b/llvm_mode/afl-clang-fast.c @@ -103,7 +103,7 @@ static void find_obj(u8* argv0) { static void edit_params(u32 argc, char** argv) { - u8 fortify_set = 0, asan_set = 0, x_set = 0, bit_mode = 0; + u8 fortify_set = 0, asan_set = 0, x_set = 0, bit_mode = 0, partial_linking = 0; u8 *name; cc_params = ck_alloc((argc + 128) * sizeof(u8*)); @@ -148,6 +148,10 @@ static void edit_params(u32 argc, char** argv) { if (!strcmp(cur, "armv7a-linux-androideabi")) bit_mode = 32; if (!strcmp(cur, "-m64")) bit_mode = 64; + if (!strcmp(cur, "-Wl,-r") || + !strcmp(cur, "-Wl,-i") || + !strcmp(cur, "-r")) partial_linking = 1; + if (!strcmp(cur, "-x")) x_set = 1; if (!strcmp(cur, "-fsanitize=address") || @@ -278,28 +282,30 @@ static void edit_params(u32 argc, char** argv) { } #ifndef __ANDROID__ - switch (bit_mode) { + if (!partial_linking) { + switch (bit_mode) { - case 0: - cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-rt.o", obj_path); - break; + case 0: + cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-rt.o", obj_path); + break; - case 32: - cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-rt-32.o", obj_path); + case 32: + cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-rt-32.o", obj_path); - if (access(cc_params[cc_par_cnt - 1], R_OK)) - FATAL("-m32 is not supported by your compiler"); + if (access(cc_params[cc_par_cnt - 1], R_OK)) + FATAL("-m32 is not supported by your compiler"); - break; + break; - case 64: - cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-rt-64.o", obj_path); + case 64: + cc_params[cc_par_cnt++] = alloc_printf("%s/afl-llvm-rt-64.o", obj_path); - if (access(cc_params[cc_par_cnt - 1], R_OK)) - FATAL("-m64 is not supported by your compiler"); + if (access(cc_params[cc_par_cnt - 1], R_OK)) + FATAL("-m64 is not supported by your compiler"); - break; + break; + } } #endif