On Unix-like operating systems, ld is a linker. It combines many compiled object and archive files, relocates their data, and ties up symbol references. Usually, the last step in compiling a program is to run ld.
This page covers the GNU/Linux version of ld.
Description
ld accepts Linker Command Language files written in a superset of AT&T’s Link Editor Command Language syntax, to provide explicit and total control over the linking process.
- Description
- Syntax
- Examples
- Related commands
- Linux commands help
For full details on the ld command language, see the ld info page. Here, we’ll cover a broad overview of how to use GNU ld, which is used in most versions of Linux.
This version of ld uses the general purpose BFD libraries to operate on object files. This allows ld to read, combine, and write object files in many different formats: for example, COFF or “a.out”. Different formats may be linked together to produce any available kind of object file.
Aside from its flexibility, the GNU linker is more helpful than other linkers in providing diagnostic information. Many linkers abandon execution immediately upon encountering an error; whenever possible, ld continues executing, allowing you to identify other errors (or, in some cases, to get an output file in spite of the error).
The GNU linker ld covers a broad range of situations, and to be as compatible as possible with other linkers. As a result, you have many choices to control its behavior.
Syntax
ld [options] objfile …
Options
The linker supports many command-line options, but in actual practice few of them are used in any particular context. For instance, a frequent use of ld is to link standard Unix object files on a standard, supported Unix system. On such a system, to link a file “hello.o”:
ld -o
This tells ld to produce a file called output as the result of linking the file “/lib/crt0.o” with “hello.o” and the library “libc.a”, which comes from the standard search directories. See the -l option below.
Some of the command-line options to ld may be specified at any point in the command line. However, options which refer to files, such as -l or -T, cause the file to be read at the point at which the option appears in the command line, relative to the object files and other file options. Repeating non-file options with a different argument either have no further effect, or override prior occurrences (those further to the left on the command line) of that option. Options which may be meaningfully specified more than once are noted in the descriptions below.
Non-option arguments are object files or archives that are to be linked together. They may follow, precede, or be mixed in with command-line options, except that an object file argument may not be placed between an option and its argument.
Usually the linker is invoked with at least one object file, but you can specify other forms of binary input files using -l, -R, and the script command language. If no binary input files at all are specified, the linker does not produce any output, and issues the message “No input files”.
If the linker cannot recognize the format of an object file, it assumes that it is a linker script. A script specified in this way augments the main linker script used for the link (either the default linker script or the one specified using -T). This feature permits the linker to link against a file which appears to be an object or an archive, but actually merely defines some symbol values, or uses “INPUT” or “GROUP” to load other objects. Specifying a script in this way merely augments the main linker script, with the extra commands placed after the main script; use the -T option to replace the default linker script entirely, but note the effect of the “INSERT” command.
For options whose names are a single letter, option arguments must either follow the option letter without intervening whitespace, or be given as separate arguments immediately following the option that requires them.
For options whose names are multiple letters, either one dash or two can precede the option name; for example, -trace-symbol and –trace-symbol are equivalent. Note that there is one exception to this rule: Multiple letter options that start with a lowercase ‘o’ can only be preceded by two dashes. This is to reduce confusion with the -o option. So for example -omagic sets the output file name to magic whereas –omagic sets the NMAGIC flag on the output.
Arguments to multiple-letter options must either be separated from the option name by an equals sign, or be given as separate arguments immediately following the option that requires them. For example, –trace-symbol foo and –trace-symbol=foo are equivalent. Unique abbreviations of the names of multiple-letter options are accepted.
Note that if the linker is being invoked indirectly, via a compiler driver (e.g., gcc) then all the linker command line options should be prefixed by -Wl, (or whatever is appropriate for the particular compiler driver) like this:
gcc -Wl,–start-group foo.o bar.o -Wl,–end-group
This is important, because otherwise the compiler driver program may silently drop the linker options, resulting in a bad link. Confusion may also arise when passing options that require values through a driver, as the use of a space between option and argument acts as a separator, and causes the driver to pass only the option to the linker and the argument to the compiler. In this case, it is simplest to use the joined forms of both single- and multiple-letter options, such as:
gcc foo.o bar.o -Wl,-eENTRY -Wl,-Map=a.map
Here is a table of the generic command line switches accepted by the GNU linker:
The i386 PE linker supports the -shared option, which causes the output to be a DLL (dynamically linked library) instead of a normal executable. You should name the output “.dll” when you use this option. Also, the linker fully supports the standard “.def” files, which may be specified on the linker command line like an object file (in fact, it should precede archives it exports symbols from, to ensure that they get linked in, like a normal object file). In addition to the options common to all targets, the i386 PE linker support additional command line options that are specific to the i386 PE target. Options that take values may be separated from their values by either a space or an equals sign.
- Where object files are mapped into memory.How common symbols are allocated.All archive members included in the link, with a mention of the symbol that caused the archive member.The values assigned to symbols.
foo = 1 foo = foo * 4 foo = foo + 8
0x00000001 foo = 0x1 [0x0000000c] foo = (foo * 0x4) [0x0000000c] foo = (foo + 0x8)
A shared library specified at link time may not be the same as the one that is available at load time, so the symbol might actually be resolvable at load time.There are some operating systems, e.g., BeOS and HPPA, where undefined symbols in shared libraries are normal.
Any directories specified by -rpath-link options.Any directories specified by -rpath options. The difference between -rpath and -rpath-link is that directories specified by -rpath options are included in the executable and used at runtime, whereas the -rpath-link option is only effective at link time. Searching -rpath in this way is only supported by native linkers and cross linkers which are configured with the –with-sysroot option.On an ELF system, for native linkers, if the -rpath and -rpath-link options were not used, search the contents of the environment variable “LD_RUN_PATH”.On SunOS, if the -rpath option was not used, search any directories specified using -L options.For a native linker, the search the contents of the environment variable “LD_LIBRARY_PATH”.For a native ELF linker, the directories in “DT_RUNPATH” or “DT_RPATH” of a shared library are searched for shared libraries needed by it. The “DT_RPATH” entries are ignored if “DT_RUNPATH” entries exist.The default directories, normally /lib and /usr/lib.For a native linker on an ELF system, if the file /etc/ld.so.conf exists, the list of directories found in that file.
int i = 1;
extern int i;
int i;
void *__wrap_malloc (size_t c){ printf (“malloc called with %zu\n”, c); return __real_malloc (c);}
The C6X uClinux target uses a binary format called DSBT to support shared libraries. Each shared library in the system needs to have a unique index; all executables use an index of 0.
“variable ‘’ can’t be auto-imported. Please read the documentation for ld’s “–enable-auto-import” for details.”
extern type extern_array[]; extern_array[1] –> { volatile type *t=extern_array; t[1] }
extern type extern_array[]; extern_array[1] –> { volatile int t=1; extern_array[t] }
extern struct s extern_struct; extern_struct.field –> { volatile struct s *t=&extern_struct; t->field }
extern long long extern_ll; extern_ll –> { volatile long long * local_ll=&extern_ll; *local_ll }
extern int arr[];
#include “foo.h"void main(int argc, char **argv){ printf("%d\n”,arr[1]);}
#include “foo.h"void main(int argc, char *argv){ / This workaround is for win32 and cygwin; do not “optimize” */ volatile int *parr = arr; printf("%d\n”,parr[1]);}
The 68HC11 and 68HC12 linkers support specific options to control the memory bank switching mapping and trampoline code generation.
The following option is supported to control handling of GOT generation when linking for 68K targets.
Environment
You can change the behaviour of ld with the environment variables “GNUTARGET”, “LDEMULATION” and “COLLECT_NO_DEMANGLE”.
“GNUTARGET” determines the input-file object format if you don’t use -b (or its synonym –format). Its value should be one of the BFD names for an input format. If there is no “GNUTARGET” in the environment, ld uses the natural format of the target. If “GNUTARGET” is set to “default” then BFD attempts to discover the input format by examining binary input files; this method often succeeds, but there are potential ambiguities since there is no method of ensuring that the magic number used to specify object-file formats is unique. However, the configuration procedure for BFD on each system places the conventional format for that system first in the search-list, so ambiguities are resolved in favor of convention.
“LDEMULATION” determines the default emulation if you don’t use the -m option. The emulation can affect various aspects of linker behaviour, particularly the default linker script. You can list the available emulations with the –verbose or -V options. If the -m option is not used, and the “LDEMULATION” environment variable is not defined, the default emulation depends upon how the linker was configured.
Normally, the linker defaults to demangling symbols. However, if “COLLECT_NO_DEMANGLE” is set in the environment, then it defaults to not demangling symbols. This environment variable is used in a similar fashion by the gcc linker wrapper program. The default may be overridden by the –demangle and –no-demangle options.
Examples
ld -o output /lib/crt0.o hello.o -lc
Link the file /lib/crt0.o with the files hello.o and libc.a (which is found by searching the default library search directories), and output the linked file to the file output.
Related commands
as — An assembler.gprof — Display call graph profile data.pvs — Display the internal version information of dynamic objects within an ELF file.