Skip to content

Commit b18328a

Browse files
authored
Merge pull request #359 from orivej/build-debug
Support debug build
2 parents cb3f602 + 40e3368 commit b18328a

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

build.sh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66

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

9-
STATIC_BUILD=1
9+
STRIP="strip"
1010
INSTALL_DEPENDENCIES=1
11+
STATIC_BUILD=1
1112

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

138143
make XZ_SUPPORT=1 mksquashfs # LZ4_SUPPORT=1 did not build yet on CentOS 6
139-
strip mksquashfs
144+
$STRIP mksquashfs
140145

141146
cd ../../
142147

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

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

234-
strip digest
239+
$STRIP digest
235240

236241
# Compile and link validate tool
237242

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

246-
strip validate
251+
$STRIP validate
247252

248253
# AppRun
249254
$CC ../AppRun.c -o AppRun
@@ -286,7 +291,7 @@ cd ..
286291
# Strip and check size and dependencies
287292

288293
rm build/*.o build/1024_blank_bytes
289-
strip build/* 2>/dev/null
294+
$STRIP build/* 2>/dev/null
290295
chmod a+x build/*
291296
ls -lh build/*
292297
for FILE in $(ls build/*) ; do

elf.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ static uint64_t file64_to_cpu(uint64_t val)
4646
static unsigned long read_elf32(int fd)
4747
{
4848
Elf32_Ehdr ehdr32;
49+
Elf32_Shdr shdr32;
50+
off_t last_shdr_offset;
4951
ssize_t ret;
52+
unsigned long sht_end, last_section_end;
5053

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

62-
return(ehdr.e_shoff + (ehdr.e_shentsize * ehdr.e_shnum));
65+
last_shdr_offset = ehdr.e_shoff + (ehdr.e_shentsize * (ehdr.e_shnum - 1));
66+
ret = pread(fd, &shdr32, sizeof(shdr32), last_shdr_offset);
67+
if (ret < 0 || (size_t)ret != sizeof(shdr32)) {
68+
fprintf(stderr, "Read of ELF section header from %s failed: %s\n",
69+
fname, strerror(errno));
70+
exit(10);
71+
}
72+
73+
/* ELF ends either with the table of section headers (SHT) or with a section. */
74+
sht_end = ehdr.e_shoff + (ehdr.e_shentsize * ehdr.e_shnum);
75+
last_section_end = file64_to_cpu(shdr32.sh_offset) + file64_to_cpu(shdr32.sh_size);
76+
return sht_end > last_section_end ? sht_end : last_section_end;
6377
}
6478

6579
static unsigned long read_elf64(int fd)
6680
{
6781
Elf64_Ehdr ehdr64;
82+
Elf64_Shdr shdr64;
83+
off_t last_shdr_offset;
6884
ssize_t ret;
85+
unsigned long sht_end, last_section_end;
6986

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

81-
return(ehdr.e_shoff + (ehdr.e_shentsize * ehdr.e_shnum));
98+
last_shdr_offset = ehdr.e_shoff + (ehdr.e_shentsize * (ehdr.e_shnum - 1));
99+
ret = pread(fd, &shdr64, sizeof(shdr64), last_shdr_offset);
100+
if (ret < 0 || (size_t)ret != sizeof(shdr64)) {
101+
fprintf(stderr, "Read of ELF section header from %s failed: %s\n",
102+
fname, strerror(errno));
103+
exit(10);
104+
}
105+
106+
/* ELF ends either with the table of section headers (SHT) or with a section. */
107+
sht_end = ehdr.e_shoff + (ehdr.e_shentsize * ehdr.e_shnum);
108+
last_section_end = file64_to_cpu(shdr64.sh_offset) + file64_to_cpu(shdr64.sh_size);
109+
return sht_end > last_section_end ? sht_end : last_section_end;
82110
}
83111

84112
unsigned long get_elf_size(const char *fname)
85-
/* TODO, FIXME: This assumes that the section header table (SHT) is
86-
the last part of the ELF. This is usually the case but
87-
it could also be that the last section is the last part
88-
of the ELF. This should be checked for.
89-
*/
90113
{
91114
ssize_t ret;
92115
int fd;

0 commit comments

Comments
 (0)