]> git.saurik.com Git - apple/ld64.git/blame - doc/man/man1/ld.1
ld64-123.2.tar.gz
[apple/ld64.git] / doc / man / man1 / ld.1
CommitLineData
a645023d 1.Dd Sept 2, 2010
a61fdf0a
A
2.Dt ld 1
3.Os Darwin
4.Sh NAME
5.Nm ld
6.Nd "linker"
7.Sh SYNOPSIS
8.Nm
9files...
10.Op options
11.Op Fl o Ar outputfile
12.Sh DESCRIPTION
74cfe461 13The
a61fdf0a
A
14.Nm ld
15command combines several object files and libraries, resolves references, and
16produces an ouput file.
17.Nm ld
18can produce a final linked image (executable, dylib, or bundle), or with the -r
19option, produce another object file. If the -o option is not used, the output
20file produced is named "a.out".
21.Ss Universal
22The linker accepts universal (multiple-architecture) input files, but
23always creates a "thin" (single-architecture), standard Mach-O output file.
24The architecture for the output file is specified using the -arch option.
25If this option is not used,
26.Nm ld
27attempts to determine the output architecture by examining the object
28files in command line order. The first "thin"
29architecture determines that of the output file. If no input
30object file is a "thin" file, the native 32-bit architecture for the host is used.
31.Pp
32Usually,
33.Nm ld
34is not used directly. Instead the
35.Xr gcc(1)
36compiler driver invokes
37.Nm ld.
38The compiler driver can be passed multiple -arch options and it will create a
39universal final linked image by invoking
40.Nm ld
41multiple times and then running
42.Xr lipo(1)
43merge the outputs into a universal file.
44.Ss Layout
74cfe461 45The object files are loaded in the order in which they are specified on the
a61fdf0a
A
46command line. The segments and the sections in those segments will appear in
47the output file in the order they are encountered in the object files being linked.
48All zero fill sections will appear after all non-zero fill sections in their segments.
49Sections created from files with the -sectcreate option will be laid out at after
50sections from .o files. The use of the -order_file option will alter the layout
51rules above, and move the symbols specified to start of their section.
52.Ss Libraries
53A static library (aka static archive) is a collection of .o files with a table of contents
54that lists the global symbols in the .o files.
55.Nm ld
56will only pull .o files out of a static library if needed to resolve some symbol reference.
57Unlike traditional linkers,
58.Nm ld
59will continually search a static library while linking. There is no need to specify a static
60library multiple times on the command line.
61.Pp
62A dynamic library (aka dylib or framework) is a final linked image. Putting a dynamic
63library on the command line causes two things: 1) The generated final linked image
64will have encoded that it depends on that dynamic library. 2) Exported symbols from the
65dynamic library are used to resolve references.
66.Pp
67Both dynamic and static libraries are searched as they appear on the command line.
68.Ss Search paths
69.Nm ld
70maintains a list of directories to search for a library or framework to use. The default
71library search path is /usr/lib then /usr/local/lib. The -L option will add a new library search
72path. The default framework search path is /Library/Frameworks then /System/Library/Frameworks.
2f2f92e4
A
73(Note: previously, /Network/Library/Frameworks was at the end of the default path. If you need
74that functionality, you need to explicitly add -F/Network/Library/Frameworks).
a61fdf0a
A
75The -F option will a new framework search path. The -Z option will remove
76the standard search paths. The -syslibroot option will prepend a prefix to all search
77paths.
78.Ss Two-level namespace
79By default all references resolved to a dynamic library record the library to which
80they were resolved. At runtime, dyld uses that information to directly resolve
a645023d 81symbols. The alternative is to use the -flat_namespace option. With flat namespace,
a61fdf0a
A
82the library is not recorded. At runtime, dyld will search each dynamic library in load
83order when resolving symbols. This is slower, but more like how other operating systems
84resolve symbols.
85.Ss Indirect dynamic libraries
86If the command line specifies to link against dylib A, and when dylib A was built it linked
87against dylib B, then B is considered an indirect dylib.
88When linking for two-level namespace, ld does not look at indirect dylibs, except when
89re-exported by a direct dylibs. On the other hand when linking for flat namespace,
90ld does load all indirect dylibs and uses them to resolve references.
91Even though indirect dylibs are specified via a full path,
92.Nm ld
93first uses the specified search paths to locate each indirect dylib. If one cannot
94be found using the search paths, the full path is used.
95.Ss Dynamic libraries undefines
96When linking for two-level namespace,
97.Nm ld
98does not verify that undefines in dylibs actually
99exist. But when linking for flat namespace,
100.Nm ld
101does check that all undefines from all loaded dylibs have a matching definition.
102This is sometimes used to force selected functions to be loaded from a static library.
103.Sh OPTIONS
104.Ss Options that control the kind of output
105.Bl -tag
106.It Fl execute
107The default. Produce a mach-o main executable that has file type MH_EXECUTE.
108.It Fl dylib
109Produce a mach-o shared library that has file type MH_DYLIB.
110.It Fl bundle
111Produce a mach-o bundle that has file type MH_BUNDLE.
112.It Fl r
113Merges object files to produce another mach-o object file with file type MH_OBJECT.
114.It Fl dylinker
115Produce a mach-o dylinker that has file type MH_DYLINKER. Only used when building dyld.
116.It Fl dynamic
a645023d 117The default. Implied by -dylib, -bundle, or -execute
a61fdf0a
A
118.It Fl static
119Produces a mach-o file that does not use the dyld. Only used building the kernel.
120.It Fl arch Ar arch_name
121Specifies which architecture (e.g. ppc, ppc64, i386, x86_64) the output file should be.
122.It Fl o Ar path
123Specifies the name and location of the output file. If not specified, `a.out' is used.
124.El
125.Ss Options that control libraries
126.Bl -tag
127.It Fl l Ns x
128This option tells the linker to search for libx.dylib or libx.a in the library search path.
129If string x is of the form y.o, then that file is searched for in the same places, but without
130prepending `lib' or appending `.a' or `.dylib' to the filename.
131.It Fl weak-l Ns Ar x
132This is the same as the -lx but forces the library and all references to it to be marked as weak imports.
133That is, the library is allowed to be missing at runtime.
134.It Fl weak_library Ar path_to_library
135This is the same as listing a file name path to a library on the link line except that it forces the
136library and all references to it to be marked as weak imports.
137.It Fl reexport-l Ns Ar x
138This is the same as the -lx but specifies that the all symbols in library x should be available to
139clients linking to the library being created. This was previously done with a separate -sub_library option.
140.It Fl reexport_library Ar path_to_library
141This is the same as listing a file name path to a library on the link line and it specifies that the
142all symbols in library path should be available to clients linking to the library being created.
143This was previously done with a separate -sub_library option.
2f2f92e4
A
144.It Fl lazy-l Ns Ar x
145This is the same as the -lx but it is only for shared libraries and the linker
146will construct glue code so that the shared library is not loaded until
147the first function in it is called.
148.It Fl lazy_library Ar path_to_library
149This is the same as listing a file name path to a shared library on the link line
150except that the linker will construct glue code so that the shared library is not
151loaded until the first function in it is called.
a645023d
A
152.It Fl upward-l Ns Ar x
153This is the same as the -lx but specifies that the dylib is an upward dependency.
154.It Fl upward_library Ar path_to_library
155This is the same as listing a file name path to a library on the link line but also marks
156the dylib as an upward dependency.
a61fdf0a
A
157.It Fl L Ns dir
158Add
159.Ar dir
160to the list of directories in which to search for libraries.
161Directories specified with -L are searched in the order they appear on the command line
a645023d
A
162and before the default search path. In Xcode4 and later, there can be a space between
163the -L and directory.
a61fdf0a
A
164.It Fl Z
165Do not search the standard directories when searching for libraries and frameworks.
166.It Fl syslibroot Ar rootdir
167Prepend
168.Ar rootdir
169to all search paths when searching for libraries or frameworks.
170.It Fl search_paths_first
a645023d
A
171This is now the default (in Xcode4 tools). When processing -lx the linker now searches each directory
172in its library search paths for `libx.dylib' then `libx.a' before the moving on to the next path
173in the library search path.
174.It Fl search_dylibs_first
175Changes the searching behavior for libraries. The default is that when processing -lx the linker
176searches each directory in its library search paths for `libx.dylib' then `libx.a'.
177This option changes the behavior to first search for a file of the form `libx.dylib' in each directory
a61fdf0a 178in the library search path, then a file of the form `libx.a' is searched for in the library search paths.
a645023d 179This option restores the search behavior of the linker prior to Xcode4.
a61fdf0a
A
180.It Fl framework Ar name[,suffix]
181This option tells the linker to search for `name.framework/name' the framework search path.
182If the optional suffix is specified the framework is first searched for the name with the suffix and then without
2f2f92e4 183(e.g. look for `name.framework/name_suffix' first, if not there try `name.framework/name').
a61fdf0a
A
184.It Fl weak_framework Ar name[,suffix]
185This is the same as the -framework name[,suffix] but forces the framework and all
186references to it to be marked as weak imports.
187.It Fl reexport_framework Ar name[,suffix]
188This is the same as the -framework name[,suffix] but also specifies that the
189all symbols in that framework should be available to clients linking to the library being created.
190This was previously done with a separate -sub_umbrella option.
2f2f92e4
A
191.It Fl lazy_framework Ar name[,suffix]
192This is the same as the -framework name[,suffix] except that the linker will
193construct glue code so that the framework is not
194loaded until the first function in it is called. You cannot directly access
a645023d
A
195data or Objective-C classes in a framework linked this way.
196.It Fl upward_framework Ar name[,suffix]
197This is the same as the -framework name[,suffix] but also specifies that the
198framework is an upward dependency.
a61fdf0a
A
199.It Fl F Ns dir
200Add
201.Ar dir
202to the list of directories in which to search for frameworks.
203Directories specified with -F are searched in the order they appear on the command line
a645023d
A
204and before the default search path. In Xcode4 and later, there can be a space between
205the -F and directory.
a61fdf0a 206.It Fl all_load
74cfe461 207Loads all members of static archive libraries.
a61fdf0a
A
208.It Fl ObjC
209Loads all members of static archive libraries that implement an Objective-C class or category.
55e3d2f6
A
210.It Fl force_load Ar path_to_archive
211Loads all members of the specified static archive library. Note: -all_load forces all members of all
212archives to be loaded. This option allows you to target a specific archive.
a61fdf0a
A
213.El
214.Ss Options that control additional content
215.Bl -tag
216.It Fl sectcreate Ar segname sectname file
217The section
218.Ar sectname
219in the segment
220.Ar segname
221is created from the contents of file
222.Ar file.
223