1#!/bin/bash -eu 2# Copyright 2019 Google Inc. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16################################################################################ 17 18# build project 19if [ "$SANITIZER" = undefined ]; then 20 export CFLAGS="$CFLAGS -fno-sanitize=unsigned-integer-overflow" 21 export CXXFLAGS="$CXXFLAGS -fno-sanitize=unsigned-integer-overflow" 22fi 23cd binutils-gdb 24 25# Comment out the lines of logging to stderror from elfcomm.c 26# This is to make it nicer to read the output of libfuzzer. 27cd binutils 28sed -i 's/vfprintf (stderr/\/\//' elfcomm.c 29sed -i 's/fprintf (stderr/\/\//' elfcomm.c 30cd ../ 31 32./configure --disable-gdb --disable-gdbserver --disable-gdbsupport \ 33 --disable-libdecnumber --disable-readline --disable-sim \ 34 --enable-targets=all --disable-werror 35make MAKEINFO=true && true 36 37# Make fuzzer directory 38mkdir fuzz 39cp ../fuzz_*.c fuzz/ 40cd fuzz 41 42for i in fuzz_disassemble fuzz_bfd; do 43 $CC $CFLAGS -I ../include -I ../bfd -I ../opcodes -c $i.c -o $i.o 44 $CXX $CXXFLAGS $i.o -o $OUT/$i $LIB_FUZZING_ENGINE ../opcodes/libopcodes.a ../bfd/libbfd.a ../libiberty/libiberty.a ../zlib/libz.a 45done 46# TODO build corpuses 47 48# Now compile the src/binutils fuzzers 49cd ../binutils 50 51# First copy the fuzzers, modify applications and copile object files 52for i in readelf; do 53 cp ../../fuzz_$i.c . 54 55 # Modify main functions so we dont have them anymore 56 sed 's/main (int argc/old_main (int argc, char **argv);\nint old_main (int argc/' $i.c >> $i.h 57 58 # Compile object file 59 $CC $CFLAGS -DHAVE_CONFIG_H -I. -I../bfd -I./../bfd -I./../include -I./../zlib -DLOCALEDIR="\"/usr/local/share/locale\"" -Dbin_dummy_emulation=bin_vanilla_emulation -W -Wall -MT fuzz_$i.o -MD -MP -c -o fuzz_$i.o fuzz_$i.c 60done 61 62# Link the files 63# Only link if they exist 64if ([ -f dwarf.o ] && [ -f elfcomm.o ] && [ -f version.o ]); then 65 ## Readelf 66 $CXX $CXXFLAGS $LIB_FUZZING_ENGINE -W -Wall -I./../zlib -o fuzz_readelf fuzz_readelf.o version.o unwind-ia64.o dwarf.o elfcomm.o ../libctf/.libs/libctf-nobfd.a -L/src/binutils-gdb/zlib -lz ../libiberty/libiberty.a 67 mv fuzz_readelf $OUT/fuzz_readelf 68 69 ### Set up seed corpus for readelf in the form of a single ELF file. 70 zip fuzz_readelf_seed_corpus.zip /src/fuzz_readelf_seed_corpus/simple_elf 71 mv fuzz_readelf_seed_corpus.zip $OUT/ 72 73 ## Copy over the options file 74 cp $SRC/fuzz_readelf.options $OUT/fuzz_readelf.options 75fi 76