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 outfile
Specify output file name (default: a.out)
-lname
Search for library libname.a or libname.so
-Ldir
Add directory dir to library search path
-r
Generate relocatable output (partial link)
-shared
Create a shared object (DLL)
--static
Do not link shared libraries (static link)
-s
Strip all symbol/debug info from output
-S
Omit debugger symbol information
-Map=file
Create a map file listing symbols and sections
-T scriptfile
Use linker script scriptfile for layout
-e symbol
Set entry point symbol
--verbose
Print verbose linking details
-u symbol
Force undefined symbol symbol
-z keyword
Set ELF dynamic tag (e.g., nodefaultlib)
-soname=name
Set shared object name in dynamic section
DESCRIPTION
The ld command is the GNU linker, a core utility from the Binutils package used to combine multiple object files (.o), archive libraries (.a), and shared libraries (.so) into a single executable program, shared object, or static library. It performs critical tasks such as symbol resolution, relocation of addresses, layout of sections in memory, and generation of the final binary format (e.g., ELF on Linux).
Invoked typically by compilers like gcc or g++, ld reads input files, matches undefined symbols with definitions, discards unused sections if requested, and applies linker scripts for custom control over output layout. It supports multiple architectures and targets, making it essential for cross-compilation. Advanced features include garbage collection of unreachable code, version scripting for shared libraries, and plugin support for custom processing.
Direct use requires understanding of object file formats and linker scripts, but it's powerful for embedded systems, custom loaders, and build systems like Make or CMake.
CAVEATS
Direct invocation requires precise options; prefer compiler drivers like gcc. Many options are architecture-specific. Linker scripts can be complex and error-prone. Large inputs may need high memory.
COMMON USAGE
ld -o program main.o utils.o -lc
Links main.o, utils.o with C library into program.
LINKER SCRIPTS
Custom scripts control sections:
MEMORY { RAM : ORIGIN = 0x1000, LENGTH = 1M }
SECTIONS { .text : { *(.text) } }
HISTORY
Developed in 1987-1988 as part of GNU Binutils by the Free Software Foundation. Maintained alongside GCC; current versions (2.40+) support modern features like LTO and IFUNC.


