Skip to content

Commit

Permalink
Files for GDB-coreutils dem that illustrate ELF startup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chaiken committed Dec 24, 2017
1 parent 37dfcd9 commit e543d98
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
54 changes: 54 additions & 0 deletions compile_coreutils.sh
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions gdb-coreutils.txt
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit e543d98

Please sign in to comment.