ld
Link object files to create executables
TLDR
Link a specific object file with no dependencies into an executable
Link two object files together
Dynamically link an x86_64 program to glibc (file paths change depending on the system)
SYNOPSIS
ld [options] objfile...
PARAMETERS
-o output
Specifies the name of the output file. If not specified, the default name is `a.out`.
-l library
Searches for the library named library when resolving references. The library is searched for in standard locations.
-L searchdir
Adds searchdir to the list of directories to search for libraries.
-static
Links against static libraries only.
-shared
Creates a shared library.
-T script
Uses script as the linker script.
-e entry
Sets the entry point symbol to entry.
-r
Creates a relocatable output; that is, a further ld run is needed.
--help
Displays help information.
--version
Displays version information.
DESCRIPTION
The ld command is the GNU linker. Its primary job is to combine object files (produced by compilers or assemblers) and archives into executable and shared objects. It resolves symbolic references between object files, relocates code and data, and performs other actions necessary to create a usable program. While often invoked indirectly through a compiler driver (like gcc or clang), understanding ld allows for fine-grained control over the linking process. It is a crucial part of the toolchain for building software on Linux and other Unix-like systems. The linker is highly configurable, allowing for customization of memory layouts, symbol management, and output formats.
ld supports various input and output formats including ELF, COFF, and others. It can also create shared libraries and perform dynamic linking. Many of its operations are controlled via command line options and linker scripts. These scripts allow for complex operations, such as defining custom memory sections, setting entry points, and handling symbol versioning.
CAVEATS
Direct use of ld is often discouraged unless specific linking control is required. Compiler drivers like gcc handle many common linking tasks automatically. Misconfigured linker scripts can lead to non-functional or unstable programs. Understanding the target architecture and its memory layout is crucial when writing linker scripts.
LINKER SCRIPTS
Linker scripts provide fine-grained control over the linking process. They define memory regions, section placement, symbol assignments, and other linking-related tasks. Learning how to write and use linker scripts is essential for advanced linking customization. ld reads its control information from this script, so precise control of section placement, memory layout, symbol assignment, and more is possible.
SYMBOL RESOLUTION
ld resolves symbolic references between object files. When a function in one object file calls a function in another, ld finds the definition of the called function and updates the calling code with the correct address. This is a fundamental process in linking.
DYNAMIC LINKING
ld can create dynamically linked executables, which rely on shared libraries loaded at runtime. This reduces the size of executables and allows for code reuse among multiple programs. Dynamic linking enables updating shared libraries without relinking the executables that use them.
HISTORY
ld has evolved alongside Unix and Linux, becoming an integral part of the GNU toolchain. Early linkers were relatively simple, primarily focused on resolving symbols and relocating code. Over time, as software became more complex, ld gained features like support for shared libraries, dynamic linking, and sophisticated linker scripts. The GNU linker, ld, has been continuously developed to support new architectures, object file formats, and linking techniques. Its capabilities have significantly expanded, making it a powerful tool for building modern software systems.