Exercise: XOR Encryption — Build and Use a Library¶
Tools: GCC, Make, ar, file, ldd, nm
Goal¶
Build a static library and a shared library from the same source file, link a program against each, and follow a symbol from undefined to resolved.
This is a guided walkthrough: there is no code to write, only commands to run and output to read.
Everything you do here, you will do on your own in 03-stream-ciphers.
Background¶
xor_encrypt() (in xor.c / xor.h) XORs every byte of a buffer with the bytes of a repeating key.
XOR is its own inverse, so the same call with the same key also decrypts.
main.c encrypts its <text> argument, prints the result in hexadecimal (it can hold any byte value, 00 included), then decrypts it again.
A static library (.a) is an archive of object files; the linker copies what it needs out of it into the executable.
A shared library (.so) stays a separate file; the executable only records its name, and the loader maps it in each time the program starts.
To keep the two apart, the libraries have different names: libxorstatic.a and libxordyn.so.
Your Task¶
Run every command, and read every output before moving on.
-
Compile each source file to an object file, and look at the symbols in each:
Umeans undefined: used here, defined somewhere else.Tmeans defined here, in the code (text) section. Findxor_encryptin both outputs. -
Try to make an executable from
main.oalone, withgcc -o xor main.o, and read the error. Someone has to providexor_encrypt. -
Build the static library and look inside it:
-
Link
main.oagainst it, in three attempts:gcc -o xor-static-lib main.o -lxorstatic gcc -o xor-static-lib -L. -lxorstatic main.o gcc -o xor-static-lib main.o -L. -lxorstatic-lxorstaticasks for a file namedlibxorstatic.a(orlibxorstatic.so): the linker adds thelibprefix and the extension.-L.adds the current directory to the places the linker searches; without it, only the system directories are searched. The linker reads its arguments left to right and takes from a library only the symbols that are undefined so far. Explain why only the third attempt works, then run./xor-static-lib key "Hello, World!". -
Check where
xor_encryptended up, and what the executable depends on: -
Build the shared library — position-independent code (
-fPIC), linked with-shared— and look at it: -
Link against it, again with
-L.and with-lat the end, and run the result:Read the error message, and the
not foundin thelddoutput.-L.was for the linker, at build time; it told the loader nothing.LD_LIBRARY_PATHis the environment variable that adds directories to the loader's search, at run time: -
Check where
xor_encryptis now, and watch the loader resolve it: -
Read the
Makefile, match each rule to a command you typed, then rebuild everything with it:
Check Your Work¶
- Both executables print the same encrypted bytes for the same key and text, and both decrypt back to the text.
- For each of
xor.o,libxorstatic.a,libxordyn.so,xor-static-libandxor-dyn, you can say whatfilereports and why. - You can say which of the two executables
lddshows depending onlibxordyn.so, and why the other one does not need it. xor_encryptisUinmain.oand inxor-dyn, butTinxor-static-lib. Explain to the teaching assistant who resolves it in each case, and when: at link time, or at load time.- You can explain why
-lgoes after the object files, why-L.is needed, and whyLD_LIBRARY_PATHis needed forxor-dynbut not forxor-static-lib.