LD

The GNU linker (ld) combines one or more object files into a single executable or library. It resolves symbols (such as function and variable names), organizes code and data into appropriate memory sections, and applies relocations to ensure all references point to the correct memory addresses. ld is commonly used to turn assembled .o files into final executable binaries.

LINKING: BASICS

root@dev:~$ ld filename.o -o binaryFilename

 * add the -m elf_i386 to assemble a 32-bit binary

LINKING: W/ LIBC LIBRARY

When writing assembly programs on Linux, you can either call the kernel directly using syscalls or link against the C standard library (libc) to access higher-level functions like printf, scanf, or malloc. Direct syscalls require no external libraries, and the program is linked with just the object file, making it lightweight but low-level. By contrast, linking with libc lets you use convenient library routines, but this requires dynamic linking against libc.so and the system’s dynamic loader (e.g., /lib64/ld-linux-x86-64.so.2). The linker command typically looks like ld filename.o -o binaryFilename -lc --dynamic-linker /lib64/ld-linux-x86-64.so.2, which ensures the runtime can resolve the external functions. This method shifts some of the burden away from manually managing syscalls, but it introduces a dependency on the standard library and loader.

root@dev:~$ ld filename.o -o binaryFilename -lc --dynamic-linker /lib64/ld-linux-x86-64.so.2

 * -lc tells the linker to "Link against the C standard library (libc):
 * used when external libc library functions (e.g., printf) are imported 
   into the source code.

Last updated