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