From e543d9812058f2dd65f6aed45b09dda886c5fd4e Mon Sep 17 00:00:00 2001 From: Alison Chaiken Date: Sun, 24 Dec 2017 12:07:59 -0800 Subject: [PATCH] Files for GDB-coreutils dem that illustrate ELF startup Copying compile_coreutils.sh and gdb-coreutils.txt to the same directory and then executing the script downloads the glibc and coreutils source, compiles coreutils, and illustrates ELF startup with GDB. Inspired by https://0xax.gitbooks.io/linux-insides/content/Misc/program_startup.html, which is now a bit out of date. --- compile_coreutils.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++ gdb-coreutils.txt | 14 ++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 compile_coreutils.sh create mode 100644 gdb-coreutils.txt diff --git a/compile_coreutils.sh b/compile_coreutils.sh new file mode 100644 index 0000000..7ca51f4 --- /dev/null +++ b/compile_coreutils.sh @@ -0,0 +1,54 @@ +#!/bin/bash +# +set -u +set -e + +if [[ ! -n $(which gcc) || ! -n $(which gdb) ]]; then + echo "Install gcc and gdb to proceed." + exit 1 +else + echo "Found GCC and GDB, proceeding." +fi +# This demo would also likely work on derivatives like Mint or Ubuntu. +if [[ ! -f /etc/debian_version ]]; then + echo "Sorry, this is a Debian demo." +else + echo "Confirmed Debian, proceeding." +fi +readonly GDBFILE="$(readlink -f gdb-coreutils.txt)" +if [[ ! -n "$GDBFILE" ]]; then + echo "GDB commands file is missing." + exit 1 +else + echo "Found GDB commands file." +fi + +readonly TESTDIR=./coreutils +mkdir "$TESTDIR" + +# Install glibc sources. +cd "$TESTDIR" +echo "Getting glibc source." +apt-get -q source glibc +# du -hcs glibc-2.24/ +# 225M glibc-2.24/ +readonly GLIBCDIR="$(find . -name "glibc*" -type d)" + +# Compile coreutils. +echo "Getting coreutils source." +apt-get -q source coreutils +TARBALL="$(ls coreutils*orig.tar.xz)" +tar xfJ "$TARBALL" +readonly COREUTILSDIR="$(find . -name "coreutils-*" -type d)" + +# Build coreutils. +cd "$COREUTILSDIR" +echo "Configuring coreutils" +# Semicolon matters; 'configure' seems to eat the next line otherwise. +./configure -q; echo "Compiling coreutils" +make -s + +# Run coreutils under GDB. +ln -s "$(readlink -f ../"$GLIBCDIR"/sysdeps)" ../sysdeps +ln -s "$(readlink -f ../"$GLIBCDIR"/csu)" ../csu +gdb --command="$GDBFILE" src/date diff --git a/gdb-coreutils.txt b/gdb-coreutils.txt new file mode 100644 index 0000000..017213c --- /dev/null +++ b/gdb-coreutils.txt @@ -0,0 +1,14 @@ +# GDB commands to examine the start-up of an ELF binary with glibc. +# Inspired by https://0xax.gitbooks.io/linux-insides/content/Misc/program_startup.html, +# which is a bit out-of-date. +run +info functions +b _init +run +l 40,100 +# Also interesting: +# info files +# Then break at "Entry point": +# b *(0x????) +# or, "info address _start" +