Skip to content

Instantly share code, notes, and snippets.

@darealshinji
Last active February 15, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darealshinji/0fabe0c8d471412aafaebc739f3b9d16 to your computer and use it in GitHub Desktop.
Save darealshinji/0fabe0c8d471412aafaebc739f3b9d16 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -e
set -x
mkdir -p build
cd build
# readelf
CC=cc
CFLAGS="-Os -Wall -ffunction-sections -fdata-sections"
LDFLAGS="-Wl,--gc-sections"
if [ ! -e elftoolchain-0.7.1/readelf/readelf ] ; then
wget -c https://ftp.heanet.ie/mirrors/OpenBSD/distfiles/elftoolchain-0.7.1.tgz
#wget -c https://sourceforge.net/projects/elftoolchain/files/Sources/elftoolchain-0.7.1/elftoolchain-0.7.1.tar.bz2
tar xf elftoolchain-0.7.1.tgz
cd elftoolchain-0.7.1
(cd common && ./native-elf-format > native-elf-format.h)
# libelf
cd libelf
for l in fsize msize convert ; do
m4 -DSRCDIR=. libelf_${l}.m4 > libelf_${l}.c
done
for c in *.c ; do
$CC $CFLAGS -I. -I../common -c $c
done
ar cr libelf.a *.o && ranlib libelf.a
cd -
# libdwarf
cd libdwarf
for l in pubnames pubtypes weaks funcs vars types pro_pubnames pro_weaks pro_funcs pro_types pro_vars ; do
m4 -DSRCDIR=. dwarf_${l}.m4 > dwarf_${l}.c
done
for c in *.c ; do
$CC $CFLAGS -I. -I../common -I../libelf -c $c
done
ar cr libdwarf.a *.o && ranlib libdwarf.a
cd -
# libelftc
cd libelftc
echo 'const char *elftc_version(){ return "elftoolchain 0.7.1"; }' > elftc_version.c
for c in *.c ; do
$CC $CFLAGS -D_GNU_SOURCE -I. -I../common -I../libelf -c $c
done
ar cr libelftc.a *.o && ranlib libelftc.a
cd -
# readelf
$CC $CFLAGS $LDFLAGS -I. -I./readelf -I./common -I./libdwarf -I./libelf -I./libelftc \
-o readelf/readelf readelf/readelf.c \
-L./libdwarf -ldwarf -L./libelftc -lelftc -L./libelf -lelf
cd ..
fi
mkdir lib
cp elftoolchain-0.7.1/readelf/readelf lib/
strip --strip-all lib/readelf
echo "int main(){return 0;}" | $CC -Os -s -o lib/testrt -xc - -Wl,--no-as-needed -lgcc_s -lstdc++
# libgcc, libstdc++
wget -c https://ftp.gnu.org/gnu/gcc/gcc-6.3.0/gcc-6.3.0.tar.bz2
wget -c http://www.mpfr.org/mpfr-3.1.5/mpfr-3.1.5.tar.xz
wget -c https://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz
wget -c https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz
wget -c http://isl.gforge.inria.fr/isl-0.18.tar.xz
tar xf gcc-6.3.0.tar.bz2
tar xf mpfr-3.1.5.tar.xz && mv mpfr-3.1.5 gcc-6.3.0/mpfr
tar xf mpc-1.0.3.tar.gz && mv mpc-1.0.3 gcc-6.3.0/mpc
tar xf gmp-6.1.2.tar.xz && mv gmp-6.1.2 gcc-6.3.0/gmp
tar xf isl-0.18.tar.xz && mv isl-0.18 gcc-6.3.0/isl
cd gcc-6.3.0
./configure --enable-languages=c,c++ --disable-bootstrap --disable-multilib
make -j`nproc`
cd -
cp gcc-6.3.0/$(./gcc-6.3.0/config.guess)/libgcc/libgcc_s.so.1 .
cp gcc-6.3.0/$(./gcc-6.3.0/config.guess)/libstdc++-v3/src/.libs/libstdc++.so.6 .
strip libgcc_s.so.1 libstdc++.so.6
chmod a-x libgcc_s.so.1 libstdc++.so.6
mkdir -p lib/libgcc_s lib/libstdc++
mv libgcc_s.so.1 lib/libgcc_s
mv libstdc++.so.6 lib/libstdc++
#!/bin/sh
LANG_OLD=$LANG
LANG=C
cxx_lib_system=$(ldd ./lib/testrt | grep libstdc++.so.6 | awk '{print $3}')
cxx_lib_bundle="./lib/libstdc++/libstdc++.so.6"
cxx_system=$(./lib/readelf -s $cxx_lib_system | grep -Eo '@GLIBCXX_[0-9.]{1,}' | sort --version-sort -r | sed -n 's/@GLIBCXX_//g; 1p')
cxx_bundle=$(./lib/readelf -s $cxx_lib_bundle | grep -Eo '@GLIBCXX_[0-9.]{1,}' | sort --version-sort -r | sed -n 's/@GLIBCXX_//g; 1p')
cxx_newer=$(echo "$cxx_bundle\n$cxx_system" | sort -V | tail -n1)
gcc_lib_system=$(ldd ./lib/testrt | grep libgcc_s.so.1 | awk '{print $3}')
gcc_lib_bundle="./lib/libgcc_s/libgcc_s.so.1"
gcc_system=$(./lib/readelf -s $cxx_lib_system | grep -Eo '@GCC_[0-9.]{1,}' | sort --version-sort -r | sed -n 's/@GCC_//g; 1p')
gcc_bundle=$(./lib/readelf -s $cxx_lib_bundle | grep -Eo '@GCC_[0-9.]{1,}' | sort --version-sort -r | sed -n 's/@GCC_//g; 1p')
gcc_newer=$(echo "$gcc_bundle\n$gcc_system" | sort -V | tail -n1)
if [ "x$DEBUG" = "x1" ]; then
echo "Bundled libraries:
libstdc++.so.6 => $cxx_bundle
libgcc_s.so.1 => $gcc_bundle
System libraries:
libstdc++.so.6 => $cxx_system
libgcc_s.so.1 => $gcc_system
"
fi
if [ $cxx_bundle = $cxx_newer ] && [ $cxx_system != $cxx_bundle ] ; then
LD_LIBRARY_PATH="`pwd`/lib/libstdc++:$LD_LIBRARY_PATH"
if [ "x$DEBUG" = "x1" ]; then
echo "adding bundled libstdc++.so.6 library to search path"
fi
fi
if [ $gcc_bundle = $gcc_newer ] && [ $gcc_system != $gcc_bundle ] ; then
LD_LIBRARY_PATH="`pwd`/lib/libgcc_s:$LD_LIBRARY_PATH"
if [ "x$DEBUG" = "x1" ]; then
echo "adding bundled libgcc_s.so.1 library to search path"
fi
fi
LANG=$LANG_OLD
export LD_LIBRARY_PATH
"$(basename "$0")-real"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment