Skip to main content

8  Sharing Functions with Code Libraries

Published

November 24, 2026

By now you should realize that the computer has to do a lot of work even for simple tasks. Because of that, you have to do a lot of work to write the code for a computer to even do simple tasks. In addition, programming tasks are usually not very simple. Therefore, we need a way to make this process easier on ourselves. There are several ways to do this, including:

All three of these are usually used to some degree in any given project. The first option will be explored further in Chapter 11. High-Level Languages. The second option is useful but it suffers from some drawbacks, including:

Therefore, the second option is usually used sparingly. It is usually only used in cases where you copy and paste skeleton code for a specific type of task, and add in your program-specific details. The third option is the one that is used the most often. The third option includes having a central repository of shared code. Then, instead of each program wasting space storing the same copies of functions, they can simply point to the dynamic libraries which contain the functions they need. If a bug is found in one of these functions, it only has to be fixed within the single function library file, and all applications which use it are automatically updated. The main drawback with this approach is that it creates some dependency problems, including:

These problems are what lead to what is known as "DLL hell". However, it is generally assumed that the advantages outweigh the disadvantages.

In programming, these shared code files are referred to as shared libraries, dynamic libraries, shared objects, dynamic-link libraries, DLLs, or .so files.1 We will refer to all of these as dynamic libraries.

Using a Dynamic Library

The program we will examine here is simple - it writes the characters hello world to the screen and exits. The regular program, 008-01-hello-world-nolib.s, looks like this:

    # Assemble with `as --32` and `ld -m elf_i386`.
    #
    .include "006-01-linux.s"      # Linux Definitions.

    # PURPOSE:
    #     This program writes the message
    #     "hello world" and exits.
    #
    .section .data
        helloworld:
            .ascii "hello world\n"

        helloworld_end:
            .equ length, helloworld_end - helloworld

    .section .text
        .globl _start

_start:
    movl  $STDOUT, %ebx
    movl  $helloworld, %ecx
    movl  $length, %edx
    movl  $SYS_WRITE, %eax
    int   $LINUX_SYSCALL

    movl  $0, %ebx
    movl  $SYS_EXIT, %eax
    int   $LINUX_SYSCALL

That's not too long. However, take a look at how short 008-01-hello-world-lib is which uses a library:

    # Assemble with `as --32` and `ld -m elf_i386`.
    #
    # PURPOSE:
    #     This program writes the message "hello world"
    #     and exits.
    #
    .section .data
        helloworld:
            .ascii "hello world\n\0"

    .section .text
        .globl _start

_start:
    pushl $helloworld
    call  printf

    pushl $0
    call  exit

It's even shorter!

Now, building programs which use dynamic libraries is a little different than normal. You can build the first program normally by doing this:

as -o 008-01-hello-world-nolib.o  --32 \
                                  --gstabs+ \
                                  008-01-hello-world-nolib.s

ld -o 008-01-hello-world-nolib    -m elf_i386 \
                                  008-01-hello-world-nolib.o

However, in order to build the second program, you have to do this:

as -o 008-01-hello-world-lib.o  --32 \
                                --gstabs+ \
                                008-01-hello-world-lib.s

ld -o 008-01-hello-world-lib    -m elf_i386 \
                                008-01-hello-world-lib.o \
            --library        c \
            --library-path   /usr/lib32/ \
            -dynamic-linker  /usr/lib32/ld-linux.so.2

Run both programs and you will get the classic message:

./008-01-hello-world-lib
./008-01-hello-world-nolib

Remember, the backslash in the first line simply means that the command continues on the next line. The option -dynamic-linker /usr/lib32/ld-linux.so.2 allows our program to be linked to libraries. This builds the executable so that before executing, the operating system will load the program /usr/lib32/ld-linux.so.2 to load in external libraries and link them with the program. This program is known as a dynamic linker.

The -l c or --library c option says to link to the C library, named libc.so on GNU/Linux systems. Given a library name, c in this case (usually library names are longer than a single letter), the GNU/Linux linker prepends the string lib to the beginning of the library name and appends .so to the end of it to form the library's filename. This library contains many functions to automate all types of tasks. The two we are using are printf, which prints strings, and exit, which exits the program.

Notice that the symbols printf and exit are simply referred to by name within the program. In previous chapters, the linker would resolve all of the names to physical memory addresses, and the names would be thrown away. When using dynamic linking, the name itself resides within the executable, and is resolved by the dynamic linker when it is run. When the program is run by the user, the dynamic linker loads the dynamic libraries listed in our link statement, and then finds all of the function and variable names that were named by our program but not found at link time, and matches them up with corresponding entries in the shared libraries it loads. It then replaces all of the names with the addresses which they are loaded at. This sounds time-consuming. It is to a small degree, but it only happens once - at program startup time.

How Dynamic Libraries Work

In our first programs, all of the code was contained within the source file. Such programs are called statically-linked executables, because they contained all of the necessary functionality for the program that wasn't handled by the kernel. In the programs we wrote in Chapter 6. Reading and Writing Simple Records, we used both our main program file and files containing routines used by multiple programs. In these cases, we combined all of the code together using the linker at link-time, so it was still statically-linked. However, in the 008-01-hello-world-lib program, we started using dynamic libraries. When you use dynamic libraries, your program is then dynamically-linked, which means that not all of the code needed to run the program is actually contained within the program file itself, but in external libraries.

When we put the --library c on the command to link the 008-01-hello-world-lib program, it told the linker to use the C library (libc.so) to look up any symbols that weren't already defined in 008-01-hello-world-lib.o. However, it doesn't actually add any code to our program, it just notes in the program where to look. When the 008-01-hello-world-lib program begins, the file /usr/lib32/ld-linux.so.2 is loaded first. This is the dynamic linker. This looks at our 008-01-hello-world-lib program and sees that it needs the C library to run. So, it searches for a file called libc.so in the standard places (listed in /etc/ld.so.conf and in the contents of the LD_LIBRARY_PATH environment variable), then looks in it for all the needed symbols (printf and exit in this case), and then loads the library into the program's virtual memory. Finally, it replaces all instances of printf in the program with the actual location of printf in the library.

Run the following command:

ldd ./008-01-hello-world-nolib

It should report back not a dynamic executable. This is just like we said - 008-01-hello-world-nolib is a statically-linked executable. However, try this:

ldd ./008-01-hello-world-lib

It will report back something like:

linux-gate.so.1 (0xf7f17000)
libc.so.6 => /usr/lib32/libc.so.6 (0xf7ce5000)
/usr/lib32/ld-linux.so.2 => /usr/lib/ld-linux.so.2 (0xf7f19000)

The numbers in parentheses may be different on your system. This means that the program 008-01-hello-world-lib is linked to libc.so.6 (the .6 is the version number), which is found at /usr/lib32/, and ld-linux.so.2 is found at /usr/lib32/. These libraries have to be loaded before the program can be run. If you are interested, run the ldd program on various programs that are on your Linux distribution, and see what libraries they rely on.

Finding Information about Libraries

Okay, so now that you know about libraries, the question is, how do you find out what libraries you have on your system and what they do? Well, let's skip that question for a minute and ask another question: How do programmers describe functions to each other in their documentation? Let's take a look at the function printf. Its calling interface (usually referred to as a prototype) looks like this:

int printf(char *string, ...);

In Linux, functions are described in the C programming language. In fact, most Linux programs are written in C. That is why most documentation and binary compatibility is defined using the C language. The interface to the printf function above is described using the C programming language.

This definition means that there is a function printf. The things inside the parenthesis are the function's parameters or arguments. The first parameter here is char *string. This means there is a parameter named string (the name isn't important, except to use for talking about it), which has a type char *. char means that it wants a single-byte character. The * after it means that it doesn't actually want a character as an argument, but instead it wants the address of a character or sequence of characters. If you look back at our 008-01-hello-world-lib program, you will notice that the function call looked like this:

    pushl $helloworld
    call  printf

So, we pushed the address of the hello string, rather than the actual characters. You might notice that we didn't push the length of the string. The way that printf found the end of the string was because we ended it with a null character (\0). Many functions work that way, especially C language functions. The int before the function definition tell what type of value the function will return in %eax when it returns. printf will return an int when it's through. Now, after the char *string, we have a series of periods, .... This means that it can take an indefinite number of additional arguments after the string. Most functions can only take a specified number of arguments. printf, however, can take many. It will look into the string parameter, and everywhere it sees the characters %s, it will look for another string from the stack to insert, and everywhere it sees %d it will look for a number from the stack to insert. This is best described using an example 008-02-printf-example.s:

    # Assemble with `as --32` and `ld -m elf_i386`.
    #
    # PURPOSE:
    #    This program is to demonstrate how to call
    #    printf.
    #
    .section .data
        # This string is called the format string.  It
        # is the first parameter, and printf uses it to
        # find out how many parameters it was given,
        # and what kind they are.
        #
        first_string:
            .ascii "Hi! %s is the %s number #%d.\n\0"

        name_string:
            .ascii "Jonathan\0"

        person_string:
            .ascii "person\0"

        # This could also have been an .equ, but we
        # decided to give it a real memory location
        # just for kicks.
        #
        number_loved:
            .long 3

    .section .text
        .globl _start

_start:
    # Note that the parameters are passed in the
    # reverse order that they are listed in the
    # function prototype.
    #
    pushl number_loved      # This is the %d.
    pushl $person_string    # This is the second %s.
    pushl $name_string      # This is the first %s.
    pushl $first_string     # This is the format string
                            # in the prototype.
    call  printf

    pushl $0
    call  exit

Type it in with the filename 008-02-printf-example.s, and then do the following commands:

as -o 008-02-printf-example.o  --32 \
                               --gstabs+ \
                               008-02-printf-example.s

ld -o 008-02-printf-example    -m elf_i386 \
                               008-02-printf-example.o \
            --library-path   /usr/lib32/ \
            --library        c \
            -dynamic-linker  /usr/lib32/ld-linux.so.2

Then run the program with ./008-02-printf-example, and it should say this:

Hi! Jonathan is the person number #3.

Now, if you look at the code, you'll see that we actually push the format string last, even though it's the first parameter listed. You always push a functions parameters in reverse order.2 You may be wondering how the printf function knows how many parameters there are. Well, it searches through your string, and counts how many %ds and %ss it finds, and then grabs that number of parameters from the stack. If the parameter matches a %d, it treats it as a number, and if it matches a %s, it treats it as a pointer to a null-terminated string. printf has many more features than this, but these are the most-used ones. So, as you can see, printf can make output a lot easier, but it also has a lot of overhead, because it has to count the number of characters in the string, look through it for all of the control characters it needs to replace, pull them off the stack, convert them to a suitable representation (numbers have to be converted to strings, etc), and stick them all together appropriately.

We've seen how to use the C programming language prototypes to call library functions. To use them effectively, however, you need to know several more of the possible data types for reading functions. Here are the main ones:

int:

An int is an integer number. The size of 4 bytes (32 bits ~ 2^32) on x86 processor provide a maximum value of 4,294,967,295; negative values between -2,147,483,648 and 2,147,483,647.

long:

A long is also an integer number. The size of 4 bytes (32 bits ~ 2^32) on x86 processor provide a maximum value of 4,294,967,295; negative values between -2,147,483,648 and 2,147,483,647.

long long:

A long long is an integer number that's larger than a long. The size of 8 bytes (64 bits ~ 2^64) on x86 processor provide a maximum value of 18,446,744,073,709,551,615; negative values between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.

short:

A short is an integer number that's shorter than an int. The size of 2 bytes (16 bits ~ 2^16) on x86 processor provide a maximum value of 65,535; negative values between -32,768 and 32,767.

char:

A char is a single-byte integer number. The size of 1 byte (8 bits ~ 2^8) on x86 processor provide a maximum value of 255; negative values between -128 and 127. This is mostly used for storing character data, since ASCII strings usually are represented with one byte per character.

float:

A float is a floating-point number. (4 bytes on an x86 processor). Floating-point numbers will be explained in more depth in Floating-point Numbers.

double:

A double is a floating-point number that is larger than a float. (8 bytes on an x86 processor).

unsigned:

unsigned is a modifier used for any of the above types which keeps them from being used as signed quantities. The difference between signed and unsigned numbers will be discussed in Chapter 10. Counting Like a Computer.

*:

An asterisk (*) is used to denote that the data isn't an actual value, but instead is a pointer to a location holding the given value (4 bytes on an x86 processor). So, let's say in memory location my_location you have the number 20 stored. If the prototype said to pass an int, you would use direct addressing mode and do pushl my_location. However, if the prototype said to pass an int *, you would do pushl $my_location - an immediate mode addressing push of the address that the value resides in. In addition to indicating the address of a single value, pointers can also be used to pass a sequence of consecutive locations, starting with the one pointed to by the given value. This is called an array.

struct:

A struct is a set of data items that have been put together under a name. For example you could declare:

And any time you ran into struct teststruct you would know that it is actually two words right next to each other, the first being an integer, and the second a pointer to a character or group of characters. You never see structs passed as arguments to functions. Instead, you usually see pointers to structs passed as arguments. This is because passing structs to functions is fairly complicated, since they can take up so many storage locations.

struct teststruct {
    int a;
    char *b;
};
typedef:

A typedef basically allows you to rename a type. For example, I can do typedef int myowntype; in a C program, and any time I typed myowntype, it would be just as if I typed int. This can get kind of annoying, because you have to look up what all of the typedefs and structs in a function prototype really mean. However, typedefs are useful for giving types more meaningful and descriptive names.

NOTE.

Compatibility: The listed sizes are for intel-compatible (x86) machines. Other machines will have different sizes. Also, even when parameters shorter than a word are passed to functions, they are passed as longs on the stack.

That's how to read function documentation. Now, let's get back to the question of how to find out about libraries. Most of your system libraries are in /usr/lib or /lib. If you want to just see what symbols they define, just run objdump -R FILENAME where FILENAME is the full path to the library. The output of that isn't too helpful, though, for finding an interface that you might need. Usually, you have to know what library you want at the beginning, and then just read the documentation. Most libraries have manuals or man pages for their functions. The web is the best source of documentation for libraries. Most libraries from the GNU project also have info pages on them, which are a little more thorough than man pages.

Let's see the objdump of the last program:

objdump -R  008-02-printf-example-bin

It should display something like this:

008-02-printf-example-bin:     file format elf32-i386

DYNAMIC RELOCATION RECORDS
OFFSET   TYPE              VALUE
0804b00c R_386_JUMP_SLOT   printf@GLIBC_2.0
0804b010 R_386_JUMP_SLOT   exit@GLIBC_2.0

Useful Functions

Several useful functions you will want to be aware of from the C library include:

  • size_t strlen (const char *s) calculates the size of null-terminated strings.

  • int strcmp (const char *s1, const char *s2) compares two strings alphabetically.

  • char * strdup (const char *s) takes the pointer to a string, and creates a new copy in a new location, and returns the new location.

  • FILE * fopen (const char *filename, const char *opentype) opens a managed, buffered file (allows easier reading and writing than using file descriptors directly).3 4

  • int fclose (FILE *stream) closes a file opened with fopen.

  • char * fgets (char *s, int count, FILE *stream) fetches a line of characters into string s.

  • int fputs (const char *s, FILE *stream) writes a string to the given open file.

  • int fprintf (FILE *stream, const char *template, ...) is just like printf, but it uses an open file rather than defaulting to using standard output.

You can find the complete manual on this library by going to The GNU C Library manual

Building a Dynamic Library

Let's say that we wanted to take all of our shared code from Chapter 6. Reading and Writing Simple Records and build it into a dynamic library to use in our programs. The first thing we would do is assemble them like normal:

as -o 006-01-write-record.o  --32 \
                             --gstabs+ \
                             006-01-write-record.s

as -o 006-01-read-record.o   --32 \
                             --gstabs+ \
                             006-01-read-record.s

Now, instead of linking them into a program, we want to link them into a dynamic library. This changes our linker command to this:

ld -o librecord.so  -m elf_i386 \
                    -shared \
                    006-01-write-record.o \
                    006-01-read-record.o

This links both of these files together into a dynamic library called librecord.so. This file can now be used for multiple programs. If we need to update the functions contained within it, we can just update this one file and not have to worry about which programs use it.

Let's look at how we would link against this library. To link the 008-03-write-records program, we would do the following:

as -o 006-01-write-records.o  --32 \
                              --gstabs+ \
                              006-01-write-records.s

ld -o 008-03-write-records    -m elf_i386 \
                              006-01-write-records.o \
                --library        record \
                --library-path   . \
                -dynamic-linker  /lib32/ld-linux.so.2

In this command, -L . or --library-path . told the linker to look for libraries in the current directory (it usually only searches /lib directory, /usr/lib directory, and a few others). As we've seen, the option -dynamic-linker /usr/lib32/ld-linux.so.2 specified the dynamic linker. The option --library record tells the linker to search for functions in the file named librecord.so.

Now the 008-03-write-records program is built, but it will not run. If we try it, we will get an error like the following:

`./008-03-write-records`: error while loading shared libraries:
librecord.so: cannot open shared object file: No such file or directory

This is because, by default, the dynamic linker only searches /lib, /usr/lib, and whatever directories are listed in /etc/ld.so.conf for libraries. In order to run the program, you either need to move the library to one of these directories, or execute the following command:

LD_LIBRARY_PATH=.  ./008-03-write-records

The next solution store the library directory in the session, it keeps alive until the session is done or the terminal/console is closed:

LD_LIBRARY_PATH=.
export LD_LIBRARY_PATH

Alternatively, if that gives you an error, do this instead:

setenv LD_LIBRARY_PATH .

Now, you can run 008-03-write-records normally by typing ./008-03-write-records. Setting LD_LIBRARY_PATH tells the linker to add whatever paths you give it to the library search path for dynamic libraries.

Let's print the shared object dependencies for ./008-03-write-records. It is a shared library. Try this:

LD_LIBRARY_PATH=.  ldd ./008-03-write-records

It will report back something like:

    linux-gate.so.1 (0xf7fa9000)
    librecord.so => ./librecord.so (0xf7fa0000)

And it’s the objdump result:

LD_LIBRARY_PATH=.  objdump -R ./008-03-write-records
./008-03-write-records-bin:     file format elf32-i386

DYNAMIC RELOCATION RECORDS
OFFSET   TYPE              VALUE
0804b00c R_386_JUMP_SLOT   _write_record

For further information about dynamic linking, see the following sources on the Internet:

Review

Know the Concepts

  • What are the advantages and disadvantages of shared libraries?

  • Given a library named 'foo', what would the library's filename be?

  • What does the ldd command do?

  • Let's say we had the files foo.o and bar.o, and you wanted to link them together, and dynamically link them to the library 'kramer'. What would the linking command be to generate the final executable?

  • What is typedef for?

  • What are structs for?

  • What is the difference between a data element of type int and int *? How would you access them differently in your program?

  • If you had an object file called foo.o, what would be the command to create a shared library called 'bar'?

  • What is the purpose of LD_LIBRARY_PATH?

Use the Concepts

  • Rewrite one or more of the programs from the previous chapters to print their results to the screen using printf rather than returning the result as the exit status code. Also, make the exit status code be 0.

  • Use the _factorial function you developed in Recursive Functions to make a shared library. Then re-write the main program so that it links with the library dynamically.

  • Rewrite the program above so that it also links with the 'C' library. Use the 'C' library's printf function to display the result of the _factorial call.

  • Rewrite the 005-01-toupper program so that it uses the C library functions for files rather than system calls.

Going Further

  • Make a list of all the environment variables used by the GNU/Linux dynamic linker.

  • Research the different types of executable file formats in use today and in the history of computing. Tell the strengths and weaknesses of each.

  • What kinds of programming are you interested in (graphics, databases, science, etc.)? Find a library for working in that area, and write a program that makes some basic use of that library.

  • Research the use of LD_PRELOAD. What is it used for? Try building a shared library that contained the exit function, and have it write a message to STDERR before exiting. Use LD_PRELOAD and run various programs with it. What are the results?


  1. Each of these terms have slightly different meanings, but most people use them interchangeably anyway. Specifically, this chapter will cover dynamic libraries, but not shared libraries. Shared libraries are dynamic libraries which are built using position-independent code (often abbreviated PIC) which is outside the scope of this book. However, shared libraries and dynamic libraries are used in the same way by users and programs; the linker just links them differently.↩︎

  2. The reason that parameters are pushed in the reverse order is because of functions which take a variable number of parameters like printf. The parameters pushed in last will be in a known position relative to the top of the stack. The program can then use these parameters to determine where on the stack the additional arguments are, and what type they are. For example, printf uses the format string to determine how many other parameters are being sent. If we pushed the known arguments first, you wouldn't be able to tell where they were on the stack.↩︎

  3. stdin, stdout, and stderr (all lower case) can be used in these programs to refer to the files of their corresponding file descriptors.↩︎

  4. FILE is a struct. You don't need to know its contents to use it. You only have to store the pointer and pass it to the relevant other functions.↩︎