1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
|
\input{configpres}
\title{\lq (Embedded) Linux Application Development\rq}
\maketitle
\subsection{Toolchains}
\begin{frame}
\frametitle{What is a toolchain?}
\pause
\begin{enumerate}
\item compiler (gcc)
\item debugger (gdb)
\item binutils (as, ld, objdump, ...)
\item sysroot (ld-linux-x86-64.so.2, libc.so.6, ...)
\item sysroot-dev (libc.so, stdio.h, ...)
\end{enumerate}
\end{frame}
\subsection{The GNU Compiler}
\begin{frame}[fragile]
\frametitle{Build a Test Program}
Create a test program: Hello, world!
\begin{verbatim}
/* hello.c */
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
\end{verbatim}
Build the test program.
\begin{verbatim}
gcc -ohello hello.c
\end{verbatim}
\end{frame}
\begin{frame}
\frametitle{Stages of a Build}
\begin{enumerate}
\item pre-processor (evaluate macros)
\item compiler (convert C code to assembly)
\item assembler (convert assembly to machine code)
\item linker (add information for external symbols)
\end{enumerate}
\end{frame}
\begin{frame}[fragile]
\frametitle{Stages of a Build}
pre-process code (evaluate macros)
\begin{verbatim}
gcc -E -ohello_pre.c hello.c
\end{verbatim}
compile code (C to assembly)
\begin{verbatim}
gcc -S -ohello.S hello_pre.c
\end{verbatim}
assemble code (assembly to machine)
\begin{verbatim}
as -ohello.o hello.S
\end{verbatim}
link objects (objects to executable)
\begin{verbatim}
ld -I/lib64/ld-linux-x86-64.so.2 -ohello hello.o \
/usr/lib/x86_64-linux-gnu/crt[1in].o -lc
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{Stages of a Build}
stop after pre-processor stage
\begin{verbatim}
gcc -E hello.c
\end{verbatim}
stop after compile stage
\begin{verbatim}
gcc -S -ohello.S hello.c
\end{verbatim}
stop after assemble stage
\begin{verbatim}
gcc -c -ohello.o hello.c
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{Important gcc Options}
compile with optimization level 3
\begin{verbatim}
gcc -O3 -ohello hello.c
\end{verbatim}
compile without optimization and with debug symbols
\begin{verbatim}
gcc -O0 -g -ohello hello.c
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{Important gcc Options}
link against additional libraries (librt)
\begin{verbatim}
gcc -ohello hello.c -lrt
\end{verbatim}
add extra search paths for libraries
\begin{verbatim}
gcc -L/mypath -ohello hello.c -lrt
\end{verbatim}
add extra search paths for headers
\begin{verbatim}
gcc -I/mypath -ohello hello.c
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{Important gcc Options}
turn on (almost) all warnings
\begin{verbatim}
gcc -Wall -ohello hello.c
\end{verbatim}
treat warnings as errors
\begin{verbatim}
gcc -Werror -ohello hello.c
\end{verbatim}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{gcc: Useful Tips}
stop after pre-processing stage and include comments
\begin{verbatim}
gcc -E -C hello.c
\end{verbatim}
show pre-defined/internal macros
\begin{verbatim}
$ gcc -E -dM - < /dev/null | cut -c 9- | sort
[...]
__SIZEOF_DOUBLE__ 8
__SIZEOF_FLOAT__ 4
__SIZEOF_INT__ 4
__SIZEOF_LONG__ 8
[...]
\end{verbatim}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{gcc: Useful Tips}
Where does the symbol printf come from?
\begin{verbatim}
$ gcc -Wl,-y,printf -ohello hello.c
/lib/libc.so.6: definition of printf
\end{verbatim}
Source and further useful tips: http://elinux.org/GCC\_Tips
\end{frame}
\subsection{The Binutils}
\begin{frame}
\frametitle{What are the binutils?}
A collection of programs to create and modify binaries. These programs are not responsible for understanding source code.
The most important tools are:
\begin{itemize}
\item \textbf{as}: the GNU assembler
\item \textbf{ld}: the GNU linker
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Other binutils Programs}
\begin{itemize}
\item \textbf{addr2line}: matches binary addresses to source code line numbers
\item \textbf{gprof}: an execution profiler
\item \textbf{nm}: lists symbols within object files
\item \textbf{objcopy}: copies and converts object files
\item \textbf{objdump}: lists/decodes information within object files
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Other binutils Programs}
\begin{itemize}
\item \textbf{ar}: create, modify, extract archives
\item \textbf{ranlib}: generates the index for an archive file
\item \textbf{readelf}: displays information within ELF files
\item \textbf{size}: lists sizes of ELF file sections
\item \textbf{strip}: removes symbols
\end{itemize}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Investigating Binaries with objdump}
\begin{verbatim}
$ objdump -x /bin/ls
/bin/ls: file format elf64-x86-64
/bin/ls
architecture: i386:x86-64, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x0000000000402490
Program Header:
PHDR off 0x0000000000000040 vaddr [...]
filesz 0x00000000000001f8 memsz [...]
[...]
Dynamic Section:
NEEDED libselinux.so.1
[...]
\end{verbatim}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Investigating Binaries with objdump}
\begin{verbatim}
[...]
Version References:
required from librt.so.1:
0x09691a75 0x00 07 GLIBC_2.2.5
[...]
Sections:
Idx Name Size VMA [...]
0 .interp 0000001c 000000000040 [...]
CONTENTS, ALLOC, LOAD, [...]
1 .note.ABI-tag 00000020 000000000040 [...]
CONTENTS, ALLOC, LOAD, [...]
[...]
\end{verbatim}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Identifying Library Dependencies with objdump}
list library dependencies
\begin{verbatim}
$ objdump -x /bin/ls | grep NEEDED
NEEDED libselinux.so.1
NEEDED libacl.so.1
NEEDED libc.so.6
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{Modifying Binaries with objcopy}
convert 64-bit ELF to SREC format
\begin{verbatim}
objcopy -I elf64-x86-64 -O srec hello hello.srec
\end{verbatim}
convert SREC to 64-bit ELF format
\begin{verbatim}
objcopy -I srec -O elf64-x86-64 hello.srec hello
\end{verbatim}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Identifying Source Code Lines with addr2line}
disassemble program to see addresses
\begin{verbatim}
$ objdump -D hello | less
[...]
000000000040050c <main>:
40050c: 55 push %rbp
40050d: 48 89 e5 mov %rsp,%rbp
[...]
\end{verbatim}
identify source line number for an address
\begin{verbatim}
$ addr2line -e hello 40050c
/home/devel/work/hello.c:4
\end{verbatim}
\end{frame}
\subsection{Static Libraries}
\begin{frame}[fragile]
\frametitle{Using Static Libraries}
create static library
\begin{verbatim}
gcc -c file1.c file2.c
ar cr libhello.a file1.o file2.o
ranlib libhello.a
\end{verbatim}
link against static library
\begin{verbatim}
gcc -ohello hello.o libhello.a
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{Static Builds}
create a static executable
\begin{verbatim}
gcc -static -ohello hello.o -lhello
\end{verbatim}
the linker searches for a file libhello.a
\end{frame}
\subsection{Dynamic Libraries}
\begin{frame}
\frametitle{\textbf{P}osition \textbf{I}ndependent \textbf{C}ode}
The -fPIC compiler option tells the compiler to only generate instructions that are position independent (i.e. relative). This means the generated instrutions can be run from any virtual address.
\end{frame}
\begin{frame}[fragile]
\frametitle{Using Dynamic (i.e. Shared) Libraries}
create shared library
\begin{verbatim}
gcc -c -fPIC file1.c file2.c
gcc -shared -olibhello.so.0.0.1 file1.o file2.o
\end{verbatim}
link against shared library
\begin{verbatim}
gcc -ohello hello.c libhello.so.0.0.1
\end{verbatim}
Try to start the program. What happens?
\end{frame}
\begin{frame}
\frametitle{What is the Dynamic Loader?}
The dynamic loader (ld-linux) loads the shared libraries needed by a program and then starts that program. It is part of the C library.
\end{frame}
\begin{frame}[fragile]
\frametitle{Running Programs}
\begin{verbatim}
./hello
\end{verbatim}
or
\begin{verbatim}
/lib64/ld-linux-x86-64.so.2 ./hello
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{Investigating Which Dynamic Loader}
The dynamic loader is stored in the contents in the ELF .interp section.
\begin{verbatim}
$ objdump -s hello | less
hello: file format elf64-x86-64
Contents of section .interp:
400200 2f6c6962 36342f6c 642d6c69 6e75782d /lib64/ld-linux-
400210 7838362d 36342e73 6f2e3200 x86-64.so.2.
\end{verbatim}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{The Dynamic Loaded: Environment Variables}
\begin{tabular}{|l|p{5cm}|}
\hline
\textbf{Environment Variable} & \textbf{Description} \\
\hline
LD\_LIBRARY\_PATH & extra search path for libraries \\
\hline
LD\_PRELOAD & list of libraries to be loaded before all others \\
\hline
LD\_DEBUG & debug output \\
\hline
LD\_TRACE\_LOADED\_OBJECTS & list shared libraries and their load addresses \\
\hline
LD\_TRACE\_PRELINKING & list all load addresses \\
\hline
\end{tabular}
\end{frame}
\begin{frame}[fragile]
\frametitle{The Dynamic Loaded: Environment Variables}
\begin{verbatim}
$ LD_DEBUG=help ./hello
Valid options for the LD_DEBUG environment
variable are:
libs display library search paths
reloc display relocation processing
files display progress for input file
symbols display symbol table processing
bindings display information about symbol binding
versions display version dependencies
all all previous options combined
statistics display relocation statistics
unused determined unused DSOs
help display this help message and exit
\end{verbatim}
\end{frame}
\begin{frame}
\frametitle{The Dynamic Loaded: Search Order}
\begin{enumerate}
\item DT\_RPATH (ELF dynamic section)
\item LD\_LIBRARY\_PATH (environment variable)
\item DT\_RUNPATH (ELF dynamic section)
\item ld.so.cache (local file)
\item generic fallbacks: /lib, /usr/lib, etc.
\end{enumerate}
\end{frame}
\begin{frame}
\frametitle{Loader Terminology}
\begin{itemize}
\item \textbf{real name}: the filename of the shared library
\item \textbf{shared object name}: identifies the shared library to load (runtime)
\item \textbf{linker name}: points to the library to link against (build time, -l)
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{The Shared Object Name (soname)}
The -soname linker option specifies to the linker the shared object name to record in the shared library. It is stored as SONAME in the ELF dynamic section.
\begin{verbatim}
gcc -c -fPIC file1.c file2.c
gcc -shared -Wl,-soname,libhello.so.0 \
-olibhello.so.0.0.1 file1.o file2.o
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{The Shared Object Name (soname)}
A symbolic link with the shared object name pointing to the shared library (real name) must exist. This is what the dynamic loader is looking for.
\begin{verbatim}
libhello.so.0 -> libhello.so.0.0.1
\end{verbatim}
The symbolic link should point to the desired library version.
\end{frame}
\begin{frame}[fragile]
\frametitle{The Shared Object Name (soname)}
originally
\begin{verbatim}
libhello.so.0 -> libhello.so.0.0.1
\end{verbatim}
after minor version update
\begin{verbatim}
libhello.so.0 -> libhello.so.0.1.0
\end{verbatim}
after major version update
\begin{verbatim}
libhello.so.1 -> libhello.so.1.0.0
\end{verbatim}
The real name version is typically of the form: major, minor, build.
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{The Shared Object Name (soname)}
Library dependencies are stored as NEEDED in the ELF dynamic section.
\begin{verbatim}
$ objdump -x hello | grep NEEDED
NEEDED libhello.so.0
NEEDED libc.so.6
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{The Linker Name}
A symbolic link without any version number is usually created that points to the latest major release.
\begin{verbatim}
libhello.so -> libhello.so.1
\end{verbatim}
This is used by the linker (build time) to locate the correct library specified with -l.
\begin{verbatim}
gcc -ohello hello.c -lhello
\end{verbatim}
\end{frame}
\begin{frame}
\frametitle{Loader Terminology}
\begin{itemize}
\item \textbf{real name}: libhello.so.0.0.1
\item \textbf{shared object name}: libhello.so.0
\item \textbf{linker name}: libhello.so
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{DT\_RUNPATH and DT\_RPATH}
The -rpath and --enable-new-dtags linker options together specify to the linker a search path to record in the executable binary. It is stored as DT\_RUNPATH in the ELF dynamic section.
\begin{verbatim}
gcc -Wl,--enable-new-dtags -Wl,-rpath,/mypath \
-ohello hello.c -lhello
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{DT\_RUNPATH and DT\_RPATH}
The -rpath and --disable-new-dtags linker options together specify to the linker a search path to record in the executable binary. It is stored as DT\_RPATH in the ELF dynamic section.
\begin{verbatim}
gcc -Wl,--disable-new-dtags -Wl,-rpath,/mypath \
-ohello hello.c -lhello
\end{verbatim}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Identifying Hard-Coded Search Paths}
Hard-coded library search paths are are stored as DT\_RUNPATH or DT\_RPATH in the ELF dynamic section (depending on the linker options used).
\begin{verbatim}
$ objdump -x hello | grep PATH
RUNPATH /mypath
$ objdump -x hello2 | grep PATH
RPATH /mypath
\end{verbatim}
\end{frame}
\begin{frame}
\frametitle{The Dynamic Loader}
Awareness and understanding of the dynamic loader leads to:
\begin{enumerate}
\item control where files are located
\item control which libraries are loaded
\item faster program loading
\item cleaner system integration
\end{enumerate}
\end{frame}
\subsection{Position Independent Executables}
\begin{frame}[fragile]
\frametitle{\textbf{P}osition \textbf{I}ndependent \textbf{E}xecutables}
The -fPIE compiler option is like the -fPIC compiler option (only generate instructions that are position independent), but the compiled objects should be used for executables instead of dynamic libraries.
\begin{verbatim}
gcc -c -fPIE hello.c
\end{verbatim}
The -pie linker option tells the linker to create a position independent executable. Objects must be compiled with -fPIE for predictable results.
\begin{verbatim}
gcc -pie -ohello hello.o
\end{verbatim}
The -no-pie linker option tells the linker \textbf{not} to create a position indepdendent executable.
\end{frame}
\begin{frame}[fragile]
\frametitle{\textbf{A}ddress \textbf{S}pace \textbf{L}ayout \textbf{R}andomization}
With PIE, the executable is loaded to a randomized address on each run.
\begin{verbatim}
$ LD_TRACE_PRELINKING=1 ./hello | grep '=>'
./hello => ./hello (0x000055f721cde000, 0x000055f721cde000)
[...]
$ LD_TRACE_PRELINKING=1 ./hello | grep '=>'
./hello => ./hello (0x00005624239d2000, 0x00005624239d2000)
[...]
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{\textbf{A}ddress \textbf{S}pace \textbf{L}ayout \textbf{R}andomization}
ASLR can be disabled.
\begin{itemize}
\item \textbf{setarch -R}: program environment (local)
\item \textbf{norandmaps}: boot argument (global)
\item \textbf{kernel.randomize\_va\_space=0}: sysctl data (global)
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\textbf{A}ddress \textbf{S}pace \textbf{L}ayout \textbf{R}andomization}
disable ASLR for a single program
\begin{verbatim}
$ setarch `uname -m` -R ./hello
Hello, world!
\end{verbatim}
verify ASLR is disabled
\begin{verbatim}
$ setarch `uname -m` -R env LD_TRACE_PRELINKING=1 ./hello | grep '=>'
./hello => ./hello (0x0000555555554000, 0x0000555555554000)
[...]
$ setarch `uname -m` -R env LD_TRACE_PRELINKING=1 ./hello | grep '=>'
./hello => ./hello (0x0000555555554000, 0x0000555555554000)
[...]
\end{verbatim}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Identifying Source Code Lines with addr2line}
disassemble program to see \textbf{relative} addresses
\begin{verbatim}
$ objdump -D hello | less
[...]
00000000000006b0 <main>:
6b0: 55 push %rbp
6b1: 48 89 e5 mov %rsp,%rbp
[...]
\end{verbatim}
identify source line number for a \textbf{relative} address
\begin{verbatim}
$ addr2line -e hello 6b0
/home/devel/work/hello.c:4
\end{verbatim}
\end{frame}
\subsection{Toolchains (cont.)}
\begin{frame}
\frametitle{What is a cross-toolchain?}
\pause
A toolchain where the architecture of the build machine is different than the architecture of the target machine.
\end{frame}
\subsection{Automating the Build Process}
\begin{frame}
\frametitle{GNU make}
\begin{alertblock}{What is GNU make?}
GNU make automates and controls build processes.
\end{alertblock}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Simple Example}
\begin{verbatim}
# Makefile
hello: hello.o
gcc -o$@ $<
hello.o: hello.c
gcc -c -o$@ $<
clean:
rm -f hello hello.o
.PHONY: clean
\end{verbatim}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Rules with Patterns}
\begin{verbatim}
hello: hello.o
gcc -o$@ $<
%.o: %.c
gcc -c -o$@ $<
clean:
rm -f hello hello.o
.PHONY: clean
\end{verbatim}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Variables}
\begin{verbatim}
EXE = hello
OBJ = $(EXE).o
$(EXE): $(OBJ)
gcc -o$@ $<
%.o: %.c
gcc -c -o$@ $<
clean:
rm -f $(EXE) $(OBJ)
.PHONY: clean
\end{verbatim}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Pattern Substitution}
\begin{verbatim}
SRC = hello.c file1.c file2.c
OBJS = $(SRC:%.c=%.o)
$(EXE): $(OBJS)
gcc -o$@ $<
%.o: %.c
gcc -c -o$@ $<
clean:
rm -f $(EXE) $(OBJS)
.PHONY: clean
\end{verbatim}
\end{frame}
\input{tailpres}
|