]>
Commit | Line | Data |
---|---|---|
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 | |
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 | |
81 | symobls. 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 -dynamiclib, -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 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. | |
a61fdf0a A |
152 | .It Fl L Ns dir |
153 | Add | |
154 | .Ar dir | |
155 | to the list of directories in which to search for libraries. | |
156 | Directories specified with -L are searched in the order they appear on the command line | |
157 | and before the default search path. | |
158 | .It Fl Z | |
159 | Do not search the standard directories when searching for libraries and frameworks. | |
160 | .It Fl syslibroot Ar rootdir | |
161 | Prepend | |
162 | .Ar rootdir | |
163 | to all search paths when searching for libraries or frameworks. | |
164 | .It Fl search_paths_first | |
165 | By default the -lx and -weak-lx options first search for a file of the form `libx.dylib' in each directory | |
166 | in the library search path, then a file of the form `libx.a' is searched for in the library search paths. | |
167 | This option changes it so that in each path `libx.dylib' is searched for then `libx.a' before the | |
168 | next path in the library search path is searched. | |
169 | .It Fl framework Ar name[,suffix] | |
170 | This option tells the linker to search for `name.framework/name' the framework search path. | |
171 | If 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] |
174 | This is the same as the -framework name[,suffix] but forces the framework and all | |
175 | references to it to be marked as weak imports. | |
176 | .It Fl reexport_framework Ar name[,suffix] | |
177 | This is the same as the -framework name[,suffix] but also specifies that the | |
178 | all symbols in that framework should be available to clients linking to the library being created. | |
179 | This was previously done with a separate -sub_umbrella option. | |
2f2f92e4 A |
180 | .It Fl lazy_framework Ar name[,suffix] |
181 | This is the same as the -framework name[,suffix] except that the linker will | |
182 | construct glue code so that the framework is not | |
183 | loaded until the first function in it is called. You cannot directly access | |
184 | data or Objective-C classes in a frameworked linked this way. | |
a61fdf0a A |
185 | .It Fl F Ns dir |
186 | Add | |
187 | .Ar dir | |
188 | to the list of directories in which to search for frameworks. | |
189 | Directories specified with -F are searched in the order they appear on the command line | |
190 | and before the default search path. | |
191 | .It Fl all_load | |
74cfe461 | 192 | Loads all members of static archive libraries. |
a61fdf0a A |
193 | .It Fl ObjC |
194 | Loads 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 |
196 | Loads all members of the specified static archive library. Note: -all_load forces all members of all | |
197 | archives 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 | |
202 | The section | |
203 | .Ar sectname | |
204 | in the segment | |
205 | .Ar segname | |
206 | is created from the contents of file | |
207 | .Ar file. | |
208 |