]>
Commit | Line | Data |
---|---|---|
1 | .Dd March 7, 2011 | |
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 | |
13 | The | |
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 | |
45 | The object files are loaded in the order in which they are specified on the | |
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. | |
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). | |
75 | The -F option will add 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 | |
81 | symbols. The alternative is to use the -flat_namespace option. With flat namespace, | |
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 | |
117 | The default. Implied by -dylib, -bundle, or -execute | |
118 | .It Fl static | |
119 | Produces a mach-o file that does not use the dyld. Only used building the kernel. | |
120 | .It Fl preload | |
121 | Produces a mach-o file in which the mach_header, load commands, and symbol table are | |
122 | not in any segment. This output type is used for firmware or embedded development | |
123 | where the segments are copied out of the mach-o into ROM/Flash. | |
124 | .It Fl arch Ar arch_name | |
125 | Specifies which architecture (e.g. ppc, ppc64, i386, x86_64) the output file should be. | |
126 | .It Fl o Ar path | |
127 | Specifies the name and location of the output file. If not specified, `a.out' is used. | |
128 | .El | |
129 | .Ss Options that control libraries | |
130 | .Bl -tag | |
131 | .It Fl l Ns x | |
132 | This option tells the linker to search for libx.dylib or libx.a in the library search path. | |
133 | If string x is of the form y.o, then that file is searched for in the same places, but without | |
134 | prepending `lib' or appending `.a' or `.dylib' to the filename. | |
135 | .It Fl weak-l Ns Ar x | |
136 | This is the same as the -lx but forces the library and all references to it to be marked as weak imports. | |
137 | That is, the library is allowed to be missing at runtime. | |
138 | .It Fl weak_library Ar path_to_library | |
139 | This is the same as listing a file name path to a library on the link line except that it forces the | |
140 | library and all references to it to be marked as weak imports. | |
141 | .It Fl reexport-l Ns Ar x | |
142 | This is the same as the -lx but specifies that the all symbols in library x should be available to | |
143 | clients linking to the library being created. This was previously done with a separate -sub_library option. | |
144 | .It Fl reexport_library Ar path_to_library | |
145 | This is the same as listing a file name path to a library on the link line and it specifies that the | |
146 | all symbols in library path should be available to clients linking to the library being created. | |
147 | This was previously done with a separate -sub_library option. | |
148 | .It Fl lazy-l Ns Ar x | |
149 | This is the same as the -lx but it is only for shared libraries and the linker | |
150 | will construct glue code so that the shared library is not loaded until | |
151 | the first function in it is called. | |
152 | .It Fl lazy_library Ar path_to_library | |
153 | This is the same as listing a file name path to a shared library on the link line | |
154 | except that the linker will construct glue code so that the shared library is not | |
155 | loaded until the first function in it is called. | |
156 | .It Fl upward-l Ns Ar x | |
157 | This is the same as the -lx but specifies that the dylib is an upward dependency. | |
158 | .It Fl upward_library Ar path_to_library | |
159 | This is the same as listing a file name path to a library on the link line but also marks | |
160 | the dylib as an upward dependency. | |
161 | .It Fl L Ns dir | |
162 | Add | |
163 | .Ar dir | |
164 | to the list of directories in which to search for libraries. | |
165 | Directories specified with -L are searched in the order they appear on the command line | |
166 | and before the default search path. In Xcode4 and later, there can be a space between | |
167 | the -L and directory. | |
168 | .It Fl Z | |
169 | Do not search the standard directories when searching for libraries and frameworks. | |
170 | .It Fl syslibroot Ar rootdir | |
171 | Prepend | |
172 | .Ar rootdir | |
173 | to all search paths when searching for libraries or frameworks. | |
174 | .It Fl search_paths_first | |
175 | This is now the default (in Xcode4 tools). When processing -lx the linker now searches each directory | |
176 | in its library search paths for `libx.dylib' then `libx.a' before the moving on to the next path | |
177 | in the library search path. | |
178 | .It Fl search_dylibs_first | |
179 | Changes the searching behavior for libraries. The default is that when processing -lx the linker | |
180 | searches each directory in its library search paths for `libx.dylib' then `libx.a'. | |
181 | This option changes the behavior to first search for a file of the form `libx.dylib' in each directory | |
182 | in the library search path, then a file of the form `libx.a' is searched for in the library search paths. | |
183 | This option restores the search behavior of the linker prior to Xcode4. | |
184 | .It Fl framework Ar name[,suffix] | |
185 | This option tells the linker to search for `name.framework/name' the framework search path. | |
186 | If the optional suffix is specified the framework is first searched for the name with the suffix and then without | |
187 | (e.g. look for `name.framework/name_suffix' first, if not there try `name.framework/name'). | |
188 | .It Fl weak_framework Ar name[,suffix] | |
189 | This is the same as the -framework name[,suffix] but forces the framework and all | |
190 | references to it to be marked as weak imports. | |
191 | .It Fl reexport_framework Ar name[,suffix] | |
192 | This is the same as the -framework name[,suffix] but also specifies that the | |
193 | all symbols in that framework should be available to clients linking to the library being created. | |
194 | This was previously done with a separate -sub_umbrella option. | |
195 | .It Fl lazy_framework Ar name[,suffix] | |
196 | This is the same as the -framework name[,suffix] except that the linker will | |
197 | construct glue code so that the framework is not | |
198 | loaded until the first function in it is called. You cannot directly access | |
199 | data or Objective-C classes in a framework linked this way. | |
200 | .It Fl upward_framework Ar name[,suffix] | |
201 | This is the same as the -framework name[,suffix] but also specifies that the | |
202 | framework is an upward dependency. | |
203 | .It Fl F Ns dir | |
204 | Add | |
205 | .Ar dir | |
206 | to the list of directories in which to search for frameworks. | |
207 | Directories specified with -F are searched in the order they appear on the command line | |
208 | and before the default search path. In Xcode4 and later, there can be a space between | |
209 | the -F and directory. | |
210 | .It Fl all_load | |
211 | Loads all members of static archive libraries. | |
212 | .It Fl ObjC | |
213 | Loads all members of static archive libraries that implement an Objective-C class or category. | |
214 | .It Fl force_load Ar path_to_archive | |
215 | Loads all members of the specified static archive library. Note: -all_load forces all members of all | |
216 | archives to be loaded. This option allows you to target a specific archive. | |
217 | .El | |
218 | .Ss Options that control additional content | |
219 | .Bl -tag | |
220 | .It Fl sectcreate Ar segname sectname file | |
221 | The section | |
222 | .Ar sectname | |
223 | in the segment | |
224 | .Ar segname | |
225 | is created from the contents of file | |
226 | .Ar file. | |
227 |