diff --git a/semester3/spca/code-examples/00_c/04_toolchain/01_dynamic_linking.c b/semester3/spca/code-examples/00_c/04_toolchain/01_dynamic_linking.c new file mode 100644 index 0000000..ce5fd59 --- /dev/null +++ b/semester3/spca/code-examples/00_c/04_toolchain/01_dynamic_linking.c @@ -0,0 +1,32 @@ +#include +#include // contains addvec + +int x[2] = {1, 2}; int y[2] = {3, 4}; int z[2]; + +int main(int argc, char *argv[]) +{ + void *handle; + void (*addvec)(int *, int *, int *, int); // Declaration + char *error; + + handle = dlopen("./libvector.so", RTLD_LAZY); // Load .so, makes addvec usable + if (!handle) { + fprintf(stderr, "%s\n", dlerror()); + exit(1); + } + + addvec = dlsym(handle, "addvec"); // get a pointer to advec + if ((error = dlerror()) != NULL) { + fprintf(stderr, "%s\n", error); + exit(1); + } + + addvec(x, y, z, 2); // Now callable like any other function + printf("z = [%d %d]\n", z[0], z[1]); + + if (dlclose(handle) < 0) { // Unload shared library + fprintf(stderr, "%s\n", dlerror()); + exit(1); + } + return 0; +} \ No newline at end of file diff --git a/semester3/spca/parts/02_toolchain/01_linking.tex b/semester3/spca/parts/02_toolchain/01_linking.tex index cccc7e8..96f3276 100644 --- a/semester3/spca/parts/02_toolchain/01_linking.tex +++ b/semester3/spca/parts/02_toolchain/01_linking.tex @@ -53,3 +53,31 @@ in \texttt{C}, function symbols can explicitly be declared weak using: The second step during Linking is Relocation. Code and data sections of separate sources are combined, and symbols are relocated from relative locations (in \texttt{.o} files) to absolute locations (in \texttt{.exe} files) + +\textbf{Command line order matters} for this, since the Linker will scan \texttt{.o} and \texttt{.a} files in this order. In general, libraries should therefore be linked \textit{last}. + +\newpage +\subsubsection{Packaging Libraries} + +Using just the Linker, there are only 2 inconvenient ways to package libraries: + +\begin{enumerate} + \item All functions into 1 file $\mapsto$ linking unnecessarily big objects. + \item One function per file $\mapsto$ Requires linking a lot of files, annoying for programmer. +\end{enumerate} + +\textbf{Static Libraries} solve this: The linker looks for functions inside the static library, and only links matching archive \textit{members} into the executable. However, these come with issues too: + +\begin{enumerate} + \item Duplication in stored executables (e.g. \texttt{libc.a} functions) + \item Duplication in running executables + \item Any fix in a library requires importing applications to explicitly relink +\end{enumerate} + +\textbf{Shared Libraries} solve this: These are linked at load-time or during run-time. Another advantage is that \textit{multiple} processes can use the same shared library simultaneously. This is how, for example, \texttt{libc} is packaged. + +During runtime, shared libraries can be loaded using \texttt{dlopen}: + +\inputcodewithfilename{gas}{code-examples/00_c/04_toolchain/}{01_dynamic_linking.c} + +\newpage \ No newline at end of file diff --git a/semester3/spca/spca-summary.pdf b/semester3/spca/spca-summary.pdf index 180e044..bb0a44e 100644 Binary files a/semester3/spca/spca-summary.pdf and b/semester3/spca/spca-summary.pdf differ diff --git a/semester3/spca/spca-summary.tex b/semester3/spca/spca-summary.tex index a5e1ed2..b84ed6f 100644 --- a/semester3/spca/spca-summary.tex +++ b/semester3/spca/spca-summary.tex @@ -1,6 +1,6 @@ \documentclass{article} -\input{C:/projects/latex/dist/full.tex} +\input{~/projects/latex/dist/full.tex} \renewcommand{\authorTitle}{Robin Bacher, Janis Hutz\\\url{https://github.com/janishutz/eth-summaries}} \renewcommand{\authorHeaders}{Robin Bacher, Janis Hutz}