ld(1) | General Commands Manual | ld(1) |
ld
—
linker
ld |
files... [options] [-o
outputfile] |
The ld
command combines several object
files and libraries, resolves references, and produces an output file.
ld
can produce a final linked image (executable,
dylib, or bundle), or with the -r option, produce another object file. If
the -o option is not used, the output file produced is named
"a.out".
The linker accepts universal (multiple-architecture) input files,
but always creates a "thin" (single-architecture), standard Mach-O
output file. The architecture for the output file is specified using the
-arch option. If this option is not used, ld
attempts to determine the output architecture by examining the object files
in command line order. The first "thin" architecture determines
that of the output file. If no input object file is a "thin" file,
the native 32-bit architecture for the host is used.
Usually, ld
is not used directly. Instead
the compiler driver invokes ld.
The compiler driver
can be passed multiple -arch options and it will create a universal final
linked image by invoking ld
multiple times and then
running lipo(1) merge the outputs into a universal
file.
The object files are loaded in the order in which they are specified on the command line. The segments and the sections in those segments will appear in the output file in the order they are encountered in the object files being linked. All zero fill sections will appear after all non-zero fill sections in their segments.
A static library (aka static archive) is a collection of .o files
with a table of contents that lists the global symbols in the .o files.
ld
will only pull .o files out of a static library
if needed to resolve some symbol reference. Unlike traditional linkers,
ld
will continually search a static library while
linking. There is no need to specify a static library multiple times on the
command line.
A dynamic library (aka dylib or framework) is a final linked image. Putting a dynamic library on the command line causes two things: 1) The generated final linked image will have encoded that it depends on that dynamic library. 2) Exported symbols from the dynamic library are used to resolve references.
Both dynamic and static libraries are searched as they appear on the command line.
ld
maintains a list of directories to
search for a library or framework to use. The default library search path is
/usr/lib then /usr/local/lib. The -L option will add a new library search
path. The default framework search path is /Library/Frameworks then
/System/Library/Frameworks. (Note: previously, /Network/Library/Frameworks
was at the end of the default path. If you need that functionality, you need
to explicitly add -F/Network/Library/Frameworks). The -F option will add a
new framework search path. The -Z option will remove the standard search
paths. The -syslibroot option will prepend a prefix to all search paths.
By default all references resolved to a dynamic library record the library to which they were resolved. At runtime, dyld uses that information to directly resolve symbols. The alternative is to use the -flat_namespace option. With flat namespace, the library is not recorded. At runtime, dyld will search each dynamic library in load order when resolving symbols. This is slower, but more like how other operating systems resolve symbols.
If the command line specifies to link against dylib A, and when
dylib A was built it linked against dylib B, then B is considered an
indirect dylib. When linking for two-level namespace, ld does not look at
indirect dylibs, except when re-exported by a direct dylibs. On the other
hand when linking for flat namespace, ld does load all indirect dylibs and
uses them to resolve references. Even though indirect dylibs are specified
via a full path, ld
first uses the specified search
paths to locate each indirect dylib. If one cannot be found using the search
paths, the full path is used.
When linking for two-level namespace, ld
does not verify that undefines in dylibs actually exist. But when linking
for flat namespace, ld
does check that all undefines
from all loaded dylibs have a matching definition. This is sometimes used to
force selected functions to be loaded from a static library.
-execute
-dylib
-bundle
-r
-dylinker
-dynamic
-static
-preload
-arch
arch_name-o
path-l
x-needed-l
x-reexport-l
x-upward-l
x-weak-l
x-assert-weak-l
x-delay-l
x-needed_library
path_to_dylib-reexport_library
path_to_library-upward_library
path_to_library-weak_library
path_to_library-assert_weak_library
path_to_library-delay_library
path_to_library-L
dir-Z
-syslibroot
rootdir-search_paths_first
-search_dylibs_first
-framework
name[,suffix]-needed_framework
name[,suffix]-weak_framework
name[,suffix]-assert_weak_framework
name[,suffix]-reexport_framework
name[,suffix]-upward_framework
name[,suffix]-delay_framework
name[,suffix]-F
dir-all_load
-ObjC
-force_load
path_to_archive-image_suffix
suffix-sectcreate
segname sectname file-add_empty_section
segname sectname-add_ast_path
file-filelist
file[,dirname]-dtrace
file-dead_strip
-order_file
file-no_order_inits
-platform_version
platform min_version
sdk_version-macos_version_min
version-ios_version_min
version-image_base
address-no_implicit_dylibs
-no_zero_fill_sections
-merge_zero_fill_sections
-no_branch_islands
-O0
-reproducible
ld
to create a reproducible output binary by
ignoring certain input properties or using alternative algorithms.-install_name
name-compatibility_version
number-current_version
number-pie
-no_pie
-pagezero_size
size-stack_size
size-allow_stack_execute
-export_dynamic
-bundle_loader
executable-keep_private_externs
-d
-exported_symbols_list
filename-exported_symbol
symbol-no_exported_symbols
-unexported_symbols_list
file-unexported_symbol
symbol-reexported_symbols_list
file-alias
symbol_name
alternate_symbol_name-alias_list
filename-flat_namespace
-u
symbol_name-U
symbol_name-undefined
treatment-rpath
path-commons
treatment-why_load
-why_live
symbol_name-print_statistics
-t
-order_file_statistics
-map
map_file_path-S
-x
-non_global_symbols_strip_list
filename-non_global_symbols_no_strip_list
filename-oso_prefix
prefix-path-bitcode_bundle
-bitcode_hide_symbols
-bitcode_symbol_map
path-v
-adhoc_codesign
-no_adhoc_codesign
-data_const
-no_data_const
-const_selrefs
-no_const_selrefs
-version_details
-no_weak_imports
-no_deduplicate
-verbose_deduplicate
-no_inits
-no_warn_inits
-warn_duplicate_libraries
-no_warn_duplicate_libraries
-debug_variant
-unaligned_pointers
treatment-dirty_data_list
filename-max_default_common_align
value-move_to_rw_segment
segment_name filename-move_to_ro_segment
segment_name filename-rename_section
orgSegment orgSection newSegment newSection-rename_segment
orgSegment newSegment-trace_symbol_layout
-section_order
segname
colon_separated_section_list-segment_order
colon_separated_segment_list-allow_heap_execute
-application_extension
-no_application_extension
-fatal_warnings
-no_eh_labels
-warn_compact_unwind
-warn_weak_exports
-no_weak_exports
-warn_unused_dylibs
-no_warn_unused_dylibs
-dead_strip_dylibs
-allow_sub_type_mismatches
-no_uuid
-random_uuid
-root_safe
-setuid_safe
-interposable
-init
symbol_name-sub_library
library_name-sub_umbrella
framework_name-allowable_client
name-client_name
name-umbrella
framework_name-headerpad
size-headerpad_max_install_names
-bind_at_load
-force_flat_namespace
-sectalign
segname sectname
value-stack_addr
address-segprot
segname max_prot
init_prot-seg_addr_table
filename-segs_read_write_addr
address-segs_read_only_addr
address-segaddr
name address-seg_page_size
name size-dylib_file
install_name:file_name-prebind
-weak_reference_mismatches
treatment-read_only_relocs
treatment-force_cpusubtype_ALL
-dylinker_install_name
path-no_arch_warnings
-arch_errors_fatal
-e
symbol_name-w
-final_output
name-arch_multiple
-twolevel_namespace_hints
-dot
path-keep_relocs
-warn_stabs
-warn_commons
-read_only_stubs
-slow_stubs
-interposable_list
filename-no_function_starts
-no_objc_category_merging
-objc_relative_method_lists
-no_objc_relative_method_lists
-object_path_lto
filename-lto_library
path-cache_path_lto
path-prune_interval_lto
seconds-prune_after_lto
seconds-max_relative_cache_size_lto
percent-fixup_chains_section
-fixup_chains_section_vm
-threaded_starts_section
-page_align_data_atoms
-search_in_sparse_frameworks
-ld_classic
ld-classic
to link the binary. This is
incompatible with options such as -merge*, used to build/merge
libraries.-ld_new
ld
to link the binary. This is incompatible with
older architectures such as armv7k and i386.-make_mergeable
-merge-l
x-merge_library
path_to_library-merge_framework
name[,suffix]-no_merged_libraries_hook
ld
automatically
adds a hook to redirect bundle resource lookups from mergeable frameworks
into the merged binary. Use this option to disable the hook.
The hook requires a minimum deployment version of iOS 12, you can use the option to disable the hook with a lower deployment target if your frameworks don't require bundle resource lookups. Disabling the hook might also improve launch time performance, so it's good to disable it regardless of the deployment target if it's not required.
-segalign
value-seglinkedit
-noseglinkedit
-fvmlib
-sectobjectsymbols
segname sectname-nofixprebinding
-noprebind_all_twolevel_modules
-prebind_all_twolevel_modules
-prebind_allow_overlap
-noprebind
-sect_diff_relocs
treatment-run_init_lazily
-single_module
-multi_module
-no_dead_strip_inits_and_terms
-A
basefile-b
-Sn
-Si
-Sp
-X
-s
-m
-y
symbol-Y
number-nomultidefs
-multiply_defined_unused
treatment-multiply_defined
treatment-private_bundle
-noall_load
-seg_addr_table_filename
path-sectorder
segname sectname orderfile-sectorder_detail
-poison_symbol
symbolName-poison_symbols_list
filePathld-classic(1), as(1), ar(1), cc(1), dyld_info(1), nm(1), otool(1) lipo(1), arch(3), dyld(3), Mach-O(5), strip(1), rebase(1)
June 21, 2023 | Darwin |