Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop gcc from passing -L with standard library paths to the linker

Tags:

c

path

gcc

linker

I have a script that needs to prevent gcc from passing -L with the standard library paths to ld. Using -nostdlib inhibits the -lc -lgcc etc. but not the -L. Using -Wl,-nostdlib prevents the linker from using its own standard path, but doesn't stop gcc from passing -L with the standard paths. Is there any way to ensure that gcc calls the linker with nothing in the library path expect the directories I explicitly write on the command line?

like image 241
R.. GitHub STOP HELPING ICE Avatar asked Dec 18 '10 20:12

R.. GitHub STOP HELPING ICE


1 Answers

I found a solution but it depends on gcc 4.4 or later for the -wrapper option (slightly updated version of the script):

inc=/path/to/alt/incl
lib=/path/to/alt/libs
crt=/path/to/alt/crt1.o
gcc -wrapper sh,-c,'
x= ; z= ; s= ; for i ; do
[ "$z" ] || set -- ; z=1
case "$i" in
-shared) s=1 ; set -- "$@" "$i" ;;
-Lxxxxxx) x=1 ;;
-xxxxxx) x= ; [ "$s" ] || set -- "$@" '"'$crt'"' ;;
*) [ "$x" ] || set -- "$@" "$i" ;;
esac
done
exec "$0" "$@"
' -nostdinc -nostdlib -isystem "$inc" -Wl,-xxxxxx "$@" -L"$lib" -Lxxxxxx -Wl,-nostdlib -lc -lgcc

My version of this wrapper is tuned to re-add alternate crt1.o and libc and libgcc files in place of the ones it prevents access to, but you could just as easily omit them if needed.

like image 89
R.. GitHub STOP HELPING ICE Avatar answered Oct 23 '22 20:10

R.. GitHub STOP HELPING ICE