[SPCA] finish linking

This commit is contained in:
RobinB27
2026-01-13 16:53:01 +01:00
parent 222eb95e4c
commit 42d368525f
4 changed files with 61 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
#include <stdio.h>
#include <dlfcn.h> // 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;
}

View File

@@ -53,3 +53,31 @@ in \texttt{C}, function symbols can explicitly be declared weak using:
The second step during Linking is Relocation. 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) 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

Binary file not shown.

View File

@@ -1,6 +1,6 @@
\documentclass{article} \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{\authorTitle}{Robin Bacher, Janis Hutz\\\url{https://github.com/janishutz/eth-summaries}}
\renewcommand{\authorHeaders}{Robin Bacher, Janis Hutz} \renewcommand{\authorHeaders}{Robin Bacher, Janis Hutz}