\input{configpres} \title{\lq (Embedded) LINUX Applikationsentwicklung\rq} \maketitle \begin{frame} \frametitle{Übersicht} \tableofcontents \end{frame} \subsection{Der GNU Compiler} \subsubsection{Hello world} \begin{frame}[fragile] \frametitle{Der GNU Compiler} \begin{lstlisting}[language=c] /* hello.c */ #include int main(void) { printf("Hello world\n"); return 0; } \end{lstlisting} \begin{lstlisting}[language=bash] # Uebersetzen des Testprogramms gcc -o hello hello.c \end{lstlisting} \end{frame} \subsubsection{Wichtige Optionen} \begin{frame}[fragile] \frametitle{Wichtige gcc Optionen} \begin{lstlisting}[language=bash] # Nur Objectfile erzeugen gcc -c -o hello.o hello.c # Uebersetzen mit Optimierungslevel 3 gcc -O3 -o hello hello.c # Ohne Optimierung und mit # Debugsymbolen uebersetzen gcc -O0 -g hello hello.c \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Wichtige gcc Optionen} \begin{lstlisting}[language=bash] # Gegen zusaetzliche Bibliothek linken (librt.so) gcc -lrt -o hello hello.c # Suchpfad fuer Bibliotheken hinzufuegen gcc -L /mypath -lrt -o hello hello.c # Suchpfad fuer Includes hinzufuegen gcc -I /mypath -o hello hello.c \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Wichtige gcc Optionen} \begin{lstlisting}[language=bash] # Alle Warnings ausgeben gcc -Wall -o hello hello.c # Warnings als Fehler behandeln gcc -Werror -o hello hello.c \end{lstlisting} \end{frame} \begin{frame}[containsverbatim] \frametitle{GCC: Nützliches} \begin{lstlisting}[language=bash] # Vordefinierte / Interne Makros ausgeben $ gcc -E -dM - < /dev/null | cut -c 9- | sort [...] __SIZEOF_DOUBLE__ 8 __SIZEOF_FLOAT__ 4 __SIZEOF_INT__ 4 __SIZEOF_LONG__ 8 [...] \end{lstlisting} \end{frame} \begin{frame}[containsverbatim] \frametitle{GCC: Nützliches} \begin{lstlisting}[language=bash] # Nur Pre-Prozessor ausfuehren $ gcc -C -E hello.c -o hello_pre # Woher kommt welches Symbol? $ gcc -Wl,-y,printf hello.c /lib/libc.so.6: definition of printf \end{lstlisting} Quelle und weitere nuetzliche Tipps: http://elinux.org/GCC\_Tips \end{frame} \subsection{Der Dynamic Loader} \begin{frame} \frametitle{Der Dynamic Loader: ld-linux.so} \begin{alertblock}{Was ist der Dynamic Loader?} Der Dynamic Loader: ld-linux.so lädt die dynamischen Bibliotheken, die von einem Programm benötigt werden. Er ist Bestandteil der C Bibliothek. \end{alertblock} \end{frame} \begin{frame}[containsverbatim] \frametitle{Der Dynamic Loader: ld-linux.so} \begin{lstlisting}[language=bash] # Entweder ./hello # oder /lib/ld-linux-x86-64.so.2 ./hello \end{lstlisting} \end{frame} \begin{frame}[containsverbatim] \frametitle{Der Dynamic Loader: Umgebungsvariablen} \begin{tabular}{|l|p{5cm}|} \hline \textbf{Umgebungsvariable} & \textbf{Abkürzung} \\ \hline LD\_LIBRARY\_PATH & Suchpfad für Bibliotheken \\ \hline LD\_PRELOAD & Liste von Bibliotheken, die vor allen anderen zu laden sind \\ \hline LD\_DEBUG & Debug Ausgaben \\ \hline \end{tabular} \end{frame} \begin{frame}[containsverbatim] \frametitle{Der Dynamic Loader: Umgebungsvariablen} \begin{lstlisting}[basicstyle=\ttfamily\fontsize{9}{9}\selectfont] $ 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{lstlisting} \end{frame} \begin{frame} \frametitle{Der Dynamic Loader: Suchreihenfolge} \begin{enumerate} \item DT\_RPATH dynamic subsection (ELF) \item LD\_LIBRARY\_PATH \item DT\_RUNPATH dynamic subsection (ELF) \item ld.so.cache \item /lib bzw. /usr/lib \end{enumerate} \end{frame} \subsection{Die Binutils} \subsubsection{Übersicht} \begin{frame} \frametitle{Die Binutils} Eine Sammlung von Programmen zum Erstellen / Bearbeiten von Binaries. Die wichtigsten Tools sind: \begin{itemize} \item Der GNU Linker (ld) \item Der GNU Assembler (as) \end{itemize} \end{frame} \begin{frame} \frametitle{Weitere Programme der Binutils} \begin{itemize} \item \textbf{addr2line}: Ordnet Adressen Zeilennummern zu \item \textbf{gprof}: Profiler \item \textbf{nm}: Listet Symbole in Objectfiles \item \textbf{objcopy}: Kopiert und konvertiert Objectfiles \item \textbf{objdump}: Listet Informationen zu Objectfiles \end{itemize} \end{frame} \begin{frame} \frametitle{Weitere Programme der Binutils} \begin{itemize} \item \textbf{ranlib}: Generiert den Index zu einem Archivinhalt \item \textbf{readelf}: Zeigt Informationen zu ELF Files \item \textbf{size}: Listet die Sektionsgrößen für Object- oder ELF Files \item \textbf{strip}: Entfernt Symbole \end{itemize} \end{frame} \subsubsection{objdump} \begin{frame}[containsverbatim] \frametitle{Untersuchen von Binaries mit Objdump} \begin{lstlisting}[language=bash] jan@hopfropf:~$ 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 librt.so.1 [...] \end{lstlisting} \end{frame} \begin{frame}[containsverbatim] \frametitle{Untersuchen von Binaries mit Objdump} \begin{lstlisting}[language=bash] 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{lstlisting} \end{frame} \begin{frame}[containsverbatim] \frametitle{Bibliotheksabhängigkeiten mit objdump bestimmen} \begin{lstlisting}[language=bash] jan@hopfropf:~$ objdump -x /bin/ls | grep NEEDED NEEDED librt.so.1 NEEDED libselinux.so.1 NEEDED libacl.so.1 NEEDED libc.so.6 \end{lstlisting} \end{frame} \subsubsection{objcopy} \begin{frame}[fragile] \frametitle{Binaries bearbeiten mit objcopy} \begin{lstlisting}[language=bash] # 64bit ELF ins SREC Format kopieren objcopy -I elf64-x86-64 -O srec hello hello.srec # SREC in 64 bit ELF kopieren objcopy -I srec -O elf64-x86-64 hello.srec hello \end{lstlisting} \end{frame} \subsubsection{addr2line} \begin{frame}[containsverbatim] \frametitle{Adressen zuordnen mit addr2line} \begin{lstlisting}[language=bash] objdump -D hello | less \end{lstlisting} \begin{verbatim} [...] 000000000040050c
: 40050c: 55 push %rbp 40050d: 48 89 e5 mov %rsp,%rbp [...] \end{verbatim} \begin{lstlisting}[language=bash] $ addr2line -e hello 40050c /home/jan/work/examples/hello.c:4 \end{lstlisting} \end{frame} \subsection{Buildprozess automatisieren} \subsubsection{Übersicht} \begin{frame} \frametitle{GNU make} \begin{alertblock}{Was ist GNU make?} GNU make kontrolliert und automatisiert Buildprozesse. \end{alertblock} \end{frame} \subsubsection{Anwendungsbeispiele} \begin{frame}[containsverbatim] \frametitle{Einfaches Beispiel} \begin{lstlisting}[language=make,showtabs=true,tabsize=4,tab=\rightarrowfill] % Makefile hello: hello.o gcc -o $@ $< hello.o: hello.c gcc -c -o $@ $< clean: rm -rf hello hello.o \end{lstlisting} \end{frame} \begin{frame} \frametitle{Aufgabe} \begin{itemize} \item Erstellen Sie ein Beispielprojekt mit einem Hallo Welt Programm und dem im Beispiel aufgeführten Makefile \item Übersetzen Sie das Programm \item Erstellen Sie eine Datei mit dem Namen clean und führen make clean aus (Was passiert?!) \end{itemize} \end{frame} \begin{frame}[containsverbatim] \frametitle{Problem} \begin{lstlisting}[language=bash] $ make gcc -c -o hello.o hello.c gcc -o hello hello.o $ touch clean $ make clean make: `clean' is up to date. \end{lstlisting} \end{frame} \begin{frame}[containsverbatim] \frametitle{PHONY targets} \begin{lstlisting}[language=make,showtabs=true,tabsize=4,tab=\rightarrowfill] hello: hello.o gcc -o $@ $< hello.o: hello.c gcc -c -o $@ $< .PHONY: clean clean: rm -rf hello hello.o \end{lstlisting} Aufgabe: Erweitern Sie Ihr Makefile um das PHONY target und führen Sie erneut make clean aus. \end{frame} \begin{frame}[containsverbatim] \frametitle{Regeln mit Pattern} \begin{lstlisting}[language=make,showtabs=true,tabsize=4,tab=\rightarrowfill] hello: hello.o gcc -o $@ $< %.o: %.c gcc -c -o $@ $< .PHONY: clean clean: rm -rf hello hello.o \end{lstlisting} \end{frame} \begin{frame}[containsverbatim] \frametitle{Variablen} \begin{lstlisting}[language=make,showtabs=true,tabsize=4,tab=\rightarrowfill] EXE = hello OBJ = $(EXE).o $(EXE): $(OBJ) gcc -o $@ $< %.o: %.c gcc -c -o $@ $< .PHONY: clean clean: rm -rf $(EXE) $(OBJ) \end{lstlisting} \end{frame} \begin{frame}[containsverbatim] \frametitle{Pattern substitution} \begin{lstlisting}[language=make,showtabs=true,tabsize=4,tab=\rightarrowfill] SRC = hello.c hello1.c OBJ = $(SRC:%.c=%.o) [...] \end{lstlisting} \end{frame} \input{tailpres}