]> git.saurik.com Git - apple/ld64.git/blame - doc/man/man1/ld.1
ld64-97.14.tar.gz
[apple/ld64.git] / doc / man / man1 / ld.1
CommitLineData
55e3d2f6 1.Dd December 15, 2008
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
81symobls. The alternative is to use the -flat_namespace option. With flat namespace,
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
117The default. Implied by -dynamiclib, -bundle, or -execute
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.
a61fdf0a
A
152.It Fl L Ns dir
153Add
154.Ar dir
155to the list of directories in which to search for libraries.
156Directories specified with -L are searched in the order they appear on the command line
157and before the default search path.
158.It Fl Z
159Do not search the standard directories when searching for libraries and frameworks.
160.It Fl syslibroot Ar rootdir
161Prepend
162.Ar rootdir
163to all search paths when searching for libraries or frameworks.
164.It Fl search_paths_first
165By default the -lx and -weak-lx options first search for a file of the form `libx.dylib' in each directory
166in the library search path, then a file of the form `libx.a' is searched for in the library search paths.
167This option changes it so that in each path `libx.dylib' is searched for then `libx.a' before the
168next path in the library search path is searched.
169.It Fl framework Ar name[,suffix]
170This option tells the linker to search for `name.framework/name' the framework search path.
171If the optional suffix is specified the framework is first searched for the name with the suffix and then without
2f2f92e4 172(e.g. look for `name.framework/name_suffix' first, if not there try `name.framework/name').
a61fdf0a
A
173.It Fl weak_framework Ar name[,suffix]
174This is the same as the -framework name[,suffix] but forces the framework and all
175references to it to be marked as weak imports.
176.It Fl reexport_framework Ar name[,suffix]
177This is the same as the -framework name[,suffix] but also specifies that the
178all symbols in that framework should be available to clients linking to the library being created.
179This was previously done with a separate -sub_umbrella option.
2f2f92e4
A
180.It Fl lazy_framework Ar name[,suffix]
181This is the same as the -framework name[,suffix] except that the linker will
182construct glue code so that the framework is not
183loaded until the first function in it is called. You cannot directly access
184data or Objective-C classes in a frameworked linked this way.
a61fdf0a
A
185.It Fl F Ns dir
186Add
187.Ar dir
188to the list of directories in which to search for frameworks.
189Directories specified with -F are searched in the order they appear on the command line
190and before the default search path.
191.It Fl all_load
74cfe461 192Loads all members of static archive libraries.
a61fdf0a
A
193.It Fl ObjC
194Loads all members of static archive libraries that implement an Objective-C class or category.
55e3d2f6
A
195.It Fl force_load Ar path_to_archive
196Loads all members of the specified static archive library. Note: -all_load forces all members of all
197archives to be loaded. This option allows you to target a specific archive.
a61fdf0a
A
198.El
199.Ss Options that control additional content
200.Bl -tag
201.It Fl sectcreate Ar segname sectname file
202The section
203.Ar sectname
204in the segment
205.Ar segname
206is created from the contents of file
207.Ar file.
208