Skip to content

Commit

Permalink
Merge pull request #359 from orivej/build-debug
Browse files Browse the repository at this point in the history
Support debug build
  • Loading branch information
probonopd authored Feb 19, 2017
2 parents cb3f602 + 40e3368 commit b18328a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
17 changes: 11 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

CC="cc -O2 -Wall -Wno-deprecated-declarations -Wno-unused-result"

STATIC_BUILD=1
STRIP="strip"
INSTALL_DEPENDENCIES=1
STATIC_BUILD=1

while [ $1 ]; do
case $1 in
'--debug' | '-d' )
STRIP="true"
;;
'--no-dependencies' | '-n' )
INSTALL_DEPENDENCIES=0
;;
Expand All @@ -29,6 +33,7 @@ while [ $1 ]; do
echo
echo 'OPTIONS:'
echo ' -h, --help: Show this help screen'
echo ' -d, --debug: Build with debug info.'
echo ' -n, --no-dependencies: Do not try to install distro specific build dependencies.'
echo ' -s, --use-shared-libs: Use distro provided shared versions of inotify-tools and openssl.'
echo ' -c, --clean: Clean all artifacts generated by the build.'
Expand Down Expand Up @@ -136,7 +141,7 @@ sed -i "s|CFLAGS += -DXZ_SUPPORT|CFLAGS += -DXZ_SUPPORT -I../../xz-5.2.3/build/i
sed -i "s|LIBS += -llzma|LIBS += -Bstatic -llzma -L../../xz-5.2.3/build/lib|g" Makefile

make XZ_SUPPORT=1 mksquashfs # LZ4_SUPPORT=1 did not build yet on CentOS 6
strip mksquashfs
$STRIP mksquashfs

cd ../../

Expand Down Expand Up @@ -165,7 +170,7 @@ objcopy --add-section .sha256_sig=1024_blank_bytes \
$CC -o runtime ../elf.c ../notify.c ../getsection.c runtime3.o \
../squashfuse/.libs/libsquashfuse_ll.a ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
-L../xz-5.2.3/build/lib -Wl,-Bdynamic -lfuse -lpthread -lz -Wl,-Bstatic -llzma -Wl,-Bdynamic -ldl
strip runtime
$STRIP runtime

# Test if we can read it back
readelf -x .upd_info runtime # hexdump
Expand Down Expand Up @@ -231,7 +236,7 @@ else
$CC -o digest ../getsection.c ../digest.c -Wl,-Bdynamic -lssl -lcrypto -lz -ldl
fi

strip digest
$STRIP digest

# Compile and link validate tool

Expand All @@ -243,7 +248,7 @@ else
-Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -ldl
fi

strip validate
$STRIP validate

# AppRun
$CC ../AppRun.c -o AppRun
Expand Down Expand Up @@ -286,7 +291,7 @@ cd ..
# Strip and check size and dependencies

rm build/*.o build/1024_blank_bytes
strip build/* 2>/dev/null
$STRIP build/* 2>/dev/null
chmod a+x build/*
ls -lh build/*
for FILE in $(ls build/*) ; do
Expand Down
39 changes: 31 additions & 8 deletions elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ static uint64_t file64_to_cpu(uint64_t val)
static unsigned long read_elf32(int fd)
{
Elf32_Ehdr ehdr32;
Elf32_Shdr shdr32;
off_t last_shdr_offset;
ssize_t ret;
unsigned long sht_end, last_section_end;

ret = pread(fd, &ehdr32, sizeof(ehdr32), 0);
if (ret < 0 || (size_t)ret != sizeof(ehdr32)) {
Expand All @@ -59,16 +62,30 @@ static unsigned long read_elf32(int fd)
ehdr.e_shentsize = file16_to_cpu(ehdr32.e_shentsize);
ehdr.e_shnum = file16_to_cpu(ehdr32.e_shnum);

return(ehdr.e_shoff + (ehdr.e_shentsize * ehdr.e_shnum));
last_shdr_offset = ehdr.e_shoff + (ehdr.e_shentsize * (ehdr.e_shnum - 1));
ret = pread(fd, &shdr32, sizeof(shdr32), last_shdr_offset);
if (ret < 0 || (size_t)ret != sizeof(shdr32)) {
fprintf(stderr, "Read of ELF section header from %s failed: %s\n",
fname, strerror(errno));
exit(10);
}

/* ELF ends either with the table of section headers (SHT) or with a section. */
sht_end = ehdr.e_shoff + (ehdr.e_shentsize * ehdr.e_shnum);
last_section_end = file64_to_cpu(shdr32.sh_offset) + file64_to_cpu(shdr32.sh_size);
return sht_end > last_section_end ? sht_end : last_section_end;
}

static unsigned long read_elf64(int fd)
{
Elf64_Ehdr ehdr64;
Elf64_Shdr shdr64;
off_t last_shdr_offset;
ssize_t ret;
unsigned long sht_end, last_section_end;

ret = pread(fd, &ehdr64, sizeof(ehdr64), 0);
if (ret < 0 || (size_t)ret != sizeof(ehdr)) {
if (ret < 0 || (size_t)ret != sizeof(ehdr64)) {
fprintf(stderr, "Read of ELF header from %s failed: %s\n",
fname, strerror(errno));
exit(10);
Expand All @@ -78,15 +95,21 @@ static unsigned long read_elf64(int fd)
ehdr.e_shentsize = file16_to_cpu(ehdr64.e_shentsize);
ehdr.e_shnum = file16_to_cpu(ehdr64.e_shnum);

return(ehdr.e_shoff + (ehdr.e_shentsize * ehdr.e_shnum));
last_shdr_offset = ehdr.e_shoff + (ehdr.e_shentsize * (ehdr.e_shnum - 1));
ret = pread(fd, &shdr64, sizeof(shdr64), last_shdr_offset);
if (ret < 0 || (size_t)ret != sizeof(shdr64)) {
fprintf(stderr, "Read of ELF section header from %s failed: %s\n",
fname, strerror(errno));
exit(10);
}

/* ELF ends either with the table of section headers (SHT) or with a section. */
sht_end = ehdr.e_shoff + (ehdr.e_shentsize * ehdr.e_shnum);
last_section_end = file64_to_cpu(shdr64.sh_offset) + file64_to_cpu(shdr64.sh_size);
return sht_end > last_section_end ? sht_end : last_section_end;
}

unsigned long get_elf_size(const char *fname)
/* TODO, FIXME: This assumes that the section header table (SHT) is
the last part of the ELF. This is usually the case but
it could also be that the last section is the last part
of the ELF. This should be checked for.
*/
{
ssize_t ret;
int fd;
Expand Down

0 comments on commit b18328a

Please sign in to comment.