1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
|
.\"
.\" This manpage is written in mdoc(7).
.\"
.\" * Language reference:
.\" https://man.openbsd.org/mdoc.7
.\"
.\" * Atom editor support:
.\" https://atom.io/packages/language-roff
.\"
.\" * Linting changes:
.\" mandoc -Wall -Tlint /path/to/this.file # BSD
.\" groff -w all -z /path/to/this.file # GNU/Linux, macOS
.\"
.\"
.\" When making changes, please keep the following in mind:
.\"
.\" * In Roff, each new sentence should begin on a new line. This gives
.\" the Roff formatter better control over text-spacing, line-wrapping,
.\" and paragraph justification.
.\"
.\" * If a line exceeds the maximum length enforced by a project's \
.\" coding style, prefer line-continuation instead of hard-wrapping; \
.\" that is, end each incomplete (physical) line with a backslash, \
.\" like in this paragraph.
.\"
.\" * Do not leave blank lines in the markup. If whitespace is desired
.\" for readability, put a dot in the first column to indicate a null/empty
.\" command. Comments and horizontal whitespace may optionally follow: each
.\" of these lines are an example of a null command immediately followed by
.\" a comment.
.\"
.\"=============================================================================
.
.Dd $Mdocdate$
.Dt MOLD 1
.Os
.Sh NAME
.Nm mold
.Nd a modern linker
.
.\"=============================================================================
.Sh SYNOPSIS
.Nm
.Op Fl options
.Ar objfile ...
.
.\"=============================================================================
.Sh DESCRIPTION
.Nm
is a faster drop-in replacement for the default GNU
.Xr ld 1 .
.
.\"-----------------------------------------------------------------------------
.Ss How to use Nm
See
.Lk https://github.com/rui314/mold#how-to-use .
.\"-----------------------------------------------------------------------------
.Ss Compatibility
.Nm
is designed to be a drop-in replacement for the GNU linkers for linking user\
-land programs.
If your user-land program cannot be built due to missing command-line options, \
please file a bug at
.Lk https://github.com/rui314/mold/issues .
.
.Pp
.Nm
supports a very limited set of linker script features,
which is just sufficient to read
.Pa /usr/lib/x86_64-linux-gnu/libc.so
on Linux systems (on Linux, that file is despite its name not a shared \
library but an ASCII linker script that loads a real
.Pa libc.so
file.)
Beyond that, we have no plan to support any linker script features.
The linker script is an ad-hoc, over-designed, complex language which \
we believe needs to be disrupted by a simpler mechanism.
We have a plan to add a replacement for the linker script to
.Nm
instead.
.
.\"-----------------------------------------------------------------------------
.Ss Archive symbol resolution
Traditionally, Unix linkers are sensitive to the order in which input files \
appear on command line.
They process input files from the first (left-most) file to the \
last (right-most) file one-by-one.
While reading input files, they maintain sets of defined and \
undefined symbols.
When visiting an archive file
.Pf ( Li \.a
files), they pull out object files to resolve as many undefined symbols as \
possible and go on to the next input file.
Object files that weren't pulled out will never have a chance for a second look.
.
.Pp
Due to this semantics, you usually have to add archive files at the end of a \
command line, so that when a linker reaches archive files, it knows what \
symbols are remain undefined.
If you put archive files at the beginning of a command line, a linker doesn't \
have any undefined symbol, and thus no object files will be pulled out from \
archives.
.
.Pp
You can change the processing order by
.Fl -start-group
and
.Fl -end-group
options, though they make a linker slower.
.
.Pp
.Nm
as well as LLVM
.Xr lld 1
linker take a different approach.
They memorize what symbols can be resolved from archive files instead of \
forgetting it after processing each archive.
Therefore,
.Nm
and
.Xr lld 1
can "go back" in a command line to pull out object files from archives,
if they are needed to resolve remaining undefined symbols.
They are not sensitive to the input file order.
.
.Pp
.Fl -start-group
and
.Fl -end-group
are still accepted by
.Nm
and
.Xr lld 1
for compatibility with traditional linkers,
but they are silently ignored.
.
.\"-----------------------------------------------------------------------------
.Ss Dynamic symbol resolution
Some Unix linker features are unable to be understood without understanding \
the semantics of dynamic symbol resolution.
Therefore, even though that's not specific to
.Nm ,
we'll explain it here.
.Pp
We use "ELF module" or just "module" as a collective term to refer an
executable or a shared library file in the ELF format.
.Pp
An ELF module may have lists of imported symbols and exported symbols,
as well as a list of shared library names from which imported symbols
should be imported.
The point is that imported symbols are not bound to any specific shared \
library until runtime.
.Pp
Here is how the Unix dynamic linker resolves dynamic symbols.
Upon the start of an ELF program, the dynamic linker construct a list of ELF \
modules which as a whole consist of a complete program.
The executable file is always at the beginning of the list followed \
by its depending shared libraries.
An imported symbol is searched from the beginning of the list to the end.
If two or more modules define the same symbol, the one that appears first in \
the list takes precedence over the others.
.Pp
This Unix semantics are contrary to systems such as Windows that have the \
two-level namespace for dynamic symbols.
On Windows, for example, dynamic symbols are represented as a tuple of
.Pq Sy symbol-name , shared-library-name ,
so that each dynamic symbol is guaranteed to be resolved from some specific \
library.
.Pp
Typically, an ELF module that exports a symbol also imports the same symbol.
Such a symbol is usually resolved to itself, but that's not the case if a \
module that appears before in the symbol search list provides another \
definition of the same symbol.
.Pp
Let me take
.Xr malloc 3
as an example.
Assume that you define your version of
.Xr malloc 3
in your main executable file.
Then, all
.Sy malloc
calls from any module are resolved to your function instead of that in libc,
because the executable is always at the beginning of the dynamic symbol \
search list. Note that even
.Xr malloc 3
calls within libc are resolved to your definition since libc exports and imports
.Sy malloc .
Therefore, by defining
.Sy malloc
yourself, you can overwrite a library function, and the
.Xr malloc 3
in libc becomes dead code.
.Pp
These Unix semantics are tricky and sometimes considered harmful.
For example, assume that you accidentally define
.Xr atoi 3
as a global function in your executable that behaves completely differently \
from the one in the C standard.
Then, all
.Sy atoi
function calls from any modules (even function calls within libc) are \
redirected to your function instead of the one in libc which obviously causes \
a problem.
That is a somewhat surprising consequence for an accidental name conflict.
On the other hand, this semantic is sometimes considered useful because it \
allows users to overwrite library functions without recompiling modules \
containing them.
Whether good or bad, you should keep this semantic in mind to understand the \
Unix linkers behaviors.
.
.\"-----------------------------------------------------------------------------
.Ss Build reproducibility
.Nm Ap s
output is deterministic.
That is, if you pass the same object files and the same command-line options to
the same version of
.Nm ,
it is guaranteed to always produce the same output.
The linker's internal randomness, such as the timing of thread scheduling or \
iteration orders of hash tables, doesn't affect the output.
.
.Pp
.Nm
does not have any host-specific default settings.
This is contrary to the GNU linkers to which some configurable values, \
such as system-dependent library search paths, are hard-coded.
.Nm
depends only on its command-line arguments.
.
.\"=============================================================================
.Sh MOLD-SPECIFIC OPTIONS
.Bl -tag -width 6n -compact
.Pp
.It Fl -chroot Ns = Ns Ar dir
Set
.Ar dir
to root directory.
.Pp
.It Fl -color-diagnostics Ns = Ns Op Sy auto | always | never
.It Fl -color-diagnostics
.It Fl -no-color-diagnostics
.Pp
Show diagnostics messages in color using ANSI escape sequences.
.Ar auto
means that
.Nm
prints out messages in color only if the standard output is connected to a TTY. \
Default is
.Ar auto .
.Pp
.It Fl -fork
.It Fl -no-fork
Spawn a child process and let it do the actual linking. \
When linking a large program, the OS kernel can take a few hundred \
milliseconds to terminate a
.Nm
process.
.Fl -fork
hides that latency. By default, it does fork.
.Pp
.It Fl -perf
Print performance statistics.
.Pp
.It Fl -print-dependencies
Print out dependency information for input files.
.Pp
Each line of the output for this option shows that which file depends on \
which file to use which symbol. This option is useful to debug why some \
object file in a static archive got linked or why some shared library is \
kept in an output file's dependency list even with
.Fl -as-needed .
.Pp
.It Fl N , Fl -omagic
.It Fl -no-omagic
Force
.Nm
to emit an output file with an old-fashioned memory layout.
First, it makes the first data segment to not be aligned to a page boundary.
Second, text segments are marked as writable if the option is given.
.Pp
.It Fl -repro
Archive input files as a tar file.
.Pp
.It Fl -reverse-sections
Reverses the order of input sections before assigning them the offsets \
in the output file.
.Pp
This option is useful for finding a bug that depends on an initialization \
order of global objects. In C++, constructors of global objects in a single \
source file are guaranteed to be executed in the source order, but \
there's no such guarantee across compilation units. Usually, constructors \
are executed in the order given to the linker, but depending on it is a mistake.
.Pp
By reversing the order of input sections using
.Fl -reverse-sections ,
you can easily test that your program works in the reversed initialization order.
.Pp
.It Fl -run Cm command Ar arg Ar
Run
.Cm command
with
.Nm
as
.Pa /usr/bin/ld .
Specifically,
.Nm
runs a given command with the LD_PRELOAD environment set to intercept
.Xr exec 3
family functions and replaces argv[0] with itself if it is
.Pa ld,
.Pa ld.gold
or
.Pa ld.lld .
.Pp
.It Fl -shuffle-sections
.It Fl -shuffle-sections Ns = Ns Ar number
Randomizes the output by shuffling the order of input sections before \
assigning them the offsets in the output file. If
.Ar number
is given, it's used as a seed for the random number generator, so that \
the linker produces the same output as for the same seed. If no seed \
is given, a random number is used as a seed.
.Pp
This option is useful for benchmarking. Modern CPUs are sensitive to program's \
memory layout. A seeming benign change in program layout (such as a small \
size increase of a function in the middle of a program) can affect program's \
performance. Therefore, \
even if you write new code and get a good benchmark result, it is hard \
to say whether or not the new code improves the programs performance. \
It is possible that the new memory layout happens to perform better.
.Pp
By running a benchmark multiple time with shuffling memory layout using
.Fl -shuffle-sections ,
you can isolate your program's real performance number from the randomness \
caused by memory layout changes.
.Pp
.It Fl -stats
Print input statistics.
.Pp
.It Fl -thread-count Ns = Ns Ar count
Use
.Ar count
number of threads.
.Pp
.It Fl -threads
.It Fl -no-threads
Use multiple threads.
By default,
.Nm
uses as many threads as the number of cores or 32, whichever is the smallest.
The reason why it is capped to 32 is because
.Nm
doesn't scale well beyond that point.
To use only one thread, pass
.Fl -no-threads
or
.Fl -thread-count Ns = Ns Sy 1 .
.Pp
.Pp
.It Fl -quick-exit
.It Fl -no-quick-exit
Use or do not use
.Dv quick_exit
to exit.
.
.El \" End of options list
.
.\"-----------------------------------------------------------------------------
.Sh GNU-COMPATIBLE OPTIONS
.Bl -tag -width 6n -compact
.It Fl -help
Report usage information to stdout and exit.
.Pp
.It Fl v , Fl -version
Report version information to stdout.
.Pp
.It Fl V
Report version and target information to stdout.
.Pp
.It Fl C Ar dir , Fl -directory Ar dir
Change to
.Ar dir
before doing anything.
.Pp
.It Fl E , Fl -export-dynamic
.It Fl -no-export-dynamic
When creating an executable, using the
.Fl E
option causes all global symbols to be put into the dynamic symbol table,
so that the symbols are visible from other ELF modules at runtime.
.Pp
By default, or if
.Fl -no-export-dynamic
is given, only symbols
that are referenced by DSOs at link-time are exported from an executable.
.Pp
.It Fl F Ar libname , Fl -filter Ns = Ns Ar libname
Set the
.Dv DT_FILTER
dynamic section field to
.Ar libname .
.Pp
.It Fl I Ns Ar file , Fl -dynamic-linker Ns = Ns Ar file
.It Fl -no-dynamic-linker
Set the dynamic linker path to
.Ar file .
If no
.Fl I
option is given, or if
.Fl -no-dynamic-linker
is given, no dynamic linker path is set to an output file.
This is contrary to the GNU linkers which sets a default dynamic linker path \
in that case.
However, this difference doesn't usually make any difference because the \
compiler driver always passes
.Fl I
to a linker.
.Pp
.It Fl L Ns Ar dir , Fl -library-path Ns = Ns Ar dir
Add
.Ar dir
to the list of library search paths from which
.Nm
searches libraries for the \fB-l\fR option.
.Pp
Unlike the GNU linkers,
.Nm
does not have the default search paths.
This difference doesn't usually make any difference because the
compiler driver always passes all necessary search paths to a linker.
.Pp
.It Fl M , Fl -print-map
Write a map file to stdout.
.Pp
.It Fl N , Fl -omagic
.It Fl -no-omagic
Force
.Nm
to emit an output file with an old-fashioned memory layout.
First, it makes the first data segment to not be aligned to a page boundary.
Second, text segments are marked as writable if the option is given.
.Pp
.It Fl S , Fl -strip-debug
Omit
.Li \.debug_*
sections from the output file.
.Pp
.It Fl T Ar file , Fl -script Ns = Ns Ar file
Read linker script from
.Ar file .
.Pp
.It Fl X , Fl -discard-locals
Discard temporary local symbols to reduce the sizes of the \
symbol table and the string table.
Temporary local symbols are local symbols starting with
.Li \.L .
Compilers usually generate such symbols for unnamed program elements such as \
string literals or floating-point literals.
.Pp
.It Fl e Ar symbol , Fl -entry Ns = Ns Ar symbol
Use
.Ar symbol
as the entry point symbol instead of the default
entry point symbol
.Sy _start .
.Pp
.It Fl f Ar shlib , Fl -auxiliary Ns = Ns Ar shlib
Set the
.Dv DT_AUXILIARY
dynamic section field to
.Ar shlib .
.Pp
.It Fl h Ar libname , Fl -soname Ns = Ns Ar libname
Set the
.Dv DT_SONAME
dynamic section field to
.Ar libname .
This option is used when creating a shared object file.
Typically, when you create
.Pf Sy XXX lib Ar foo Ns Sy .so ,
you want to pass
.Fl -soname Ns = Ns Ar foo
to a linker.
.Pp
.It Fl l Ns Ar libname
Search for
.Pf Sy lib Ar libname Ns Sy \.so
or
.Pf Sy lib Ar libname Ns Sy \.a
from library search paths.
.Pp
.It Fl m Op Ar target
Choose a target.
.Pp
.It Fl o Ar file , Fl -output Ns = Ns Ar file
Use
.Ar file
as the output file name instead of the default name
.Sy a.out .
.Pp
.It Fl r , Fl -relocatable
Instead of generating an executable or a shared object file, combine
input object files to generate another object file that can be used as
an input to a linker.
.Pp
.It Fl -relocatable-merge-sections
By default,
.Nm
doesn't merge input sections by name when merging input object files into a \
single output object file for
.Fl r .
For example,
.Ar .text.foo
and
.Ar .text.bar
aren't merged for
.Fl r
even though they are merged into
.Ar .text
according to the default section merging rules.
.Pp
This option changes the behavior so that
.Nm
merges input sections by name by the default section merging rules.
.Pp
.It Fl s , Fl -strip-all
Omit
.Li \.symtab
section from the output file.
.Pp
.It Fl u Ar symbol , Fl -undefined Ns = Ns Ar symbol
If
.Ar symbol
remains as an undefined symbol after reading all object files,
and if there is an static archive that contains an object file defining
.Ar symbol ,
pull out the object file and link it so that the \
output file contains a definition of
.Ar symbol .
.Pp
.It Fl -Bdynamic
Link against shared libraries.
.Pp
.It Fl -Bstatic
Do not link against shared libraries.
.Pp
.It Fl -Bsymbolic
When creating a shared library, make global symbols export-only
(i.e. do not import the same symbol).
As a result, references within a shared library is always resolved locally, \
negating symbol override at runtime.
See
.Sx Dynamic symbol resolution
for more information about symbol imports and exports.
.Pp
.It Fl -Bsymbolic-functions
Have the same effect as
.Fl -Bsymbolic
but works only for function symbols.
Data symbols remains being both imported and exported.
.Pp
.It Fl -Bno-symbolic
Cancel
.Fl -Bsymbolic
and
.Fl -Bsymbolic-functions .
.Pp
.It Fl -Map Ns = Ns Ar file
Write map file to
.Ar file .
.Pp
.It Fl -Tbss Ns = Ns Ar address
Alias for
.Fl -section-start=.bss Ns = Ns Ar address .
.Pp
.It Fl -Tdata Ns = Ns Ar address
Alias for
.Fl -section-start=.data Ns = Ns Ar address .
.Pp
.It Fl -Ttext Ns = Ns Ar address
Alias for
.Fl -section-start=.text Ns = Ns Ar address .
.Pp
.It Fl -allow-multiple-definition
Normally, the linker reports an error if there are more than one \
definition of a symbol.
This option changes the default behavior so that it doesn't report an error \
for duplicate definitions and instead use the first definition.
.Pp
.It Fl -as-needed
.It Fl -no-as-needed
By default, shared libraries given to a linker are unconditionally added to \
the list of required libraries in an output file.
However, shared libraries after
.Fl -as-needed
are added to the list only when at least one symbol is actually used by an \
object file.
In other words, shared libraries after
.Fl -as-needed
are not added to the list of needed libraries if they are not needed by a program.
.Pp
The
.Fl -no-as-needed
option restores the default behavior for subsequent files.
.Pp
.It Fl -build-id
.It Fl -build-id Ns = Ns Op Sy none | md5 | sha1 | sha256 | uuid | 0x Ns Ar hexstring
.It Fl -no-build-id
Create a
.Li .note.gnu.build-id
section containing a byte string to
uniquely identify an output file.
.Fl -build-id
and
.Fl -build-id Ns = Ns Sy sha256
compute a 256-bit cryptographic hash of an output file and set it to build-id.
.Sy md5
and
.Sy sha1
compute the same hash but truncate it to 128 and 160 bits, respectively, \
before setting it to build-id.
.Sy uuid
sets a random 128-bit UUID.
.Sy 0x Ns Ar hexstring
sets
.Ar hexstring .
.Pp
.It Fl -defsym Ns = Ns Ar symbol Ns = Ns Ar value
.Pp
.It Fl -compress-debug-sections Ns = Ns Op Sy none | zlib | zlib-gabi | zstd
Compress DWARF debug info
.Pf ( Sy .debug_*
sections) using the zlib or zstd compression algorithm.
.Fl zlib-gabi
is an alias for
.Fl zlib .
.Pp
.It Fl -defsym Ns = Ns Ar symbol Ns = Ns Ar value
Define
.Ar symbol
as an alias for
.Ar value .
.Pp
.Ar value
is either
an integer (in decimal or hexadecimal with
.Sq 0x
prefix) or a symbol name.
If an integer is given as a value,
.Ar symbol
is defined as an absolute symbol with the given value.
.Pp
.It Fl -default-symver
Use soname as a symbol version and append that version to all symbols.
.Pp
.It Fl -demangle
.It Fl -no-demangle
Demangle C++ symbols in log messages.
.Pp
.It Fl -dependency-file Ns = Ns Ar file
Write a dependency file to
.Ar file .
The contents of the written file is readable by
.Cm make ,
which defines only one rule with the linker's output file as a target \
and all input files as its prerequisite. Users are expected to include \
the generated dependency file into a Makefile to automate the \
dependency management. This option is analogous to the compiler's
.Fl MM Fl MF
options.
.Pp
.It Fl -dynamic-list Ns = Ns Ar file
Read a list of dynamic symbols from
.Ar file .
Same as
.Fl -export-dynamic-symbol-list ,
except that it implies
.Fl -Bsymbolic .
.Pp
If
.Ar file
does not exist in the current directory, it is searched from library \
search paths for the sake of compatibility with GNU ld.
.Pp
.It Fl -eh-frame-hdr
.It Fl -no-eh-frame-hdr
Create
.Li .eh_frame_hdr
section.
.Pp
.It Fl -emit-relocs
A linker usually "consumes" relocation sections. That is, a linker \
applies relocations to other sections, and relocation sections themselves \
are discarded.
.Pp
The
.Fl -emit-relocs
instructs the linker to leave relocation sections in the output file. \
Some post-link binary analysis or optimization tools such as LLVM Bolt \
need them.
.Pp
.It Fl -enable-new-dtags
.It Fl -disable-new-dtags
By default,
.Nm
emits DT_RUNPATH for
.Fl -rpath .
If you pass
.Fl -disable-new-dtags,
mold emits DT_RPATH for
.Fl -rpath
instead.
.Pp
.It Fl -execute-only
Traditionally, most processors require both executable and readable bits to 1 \
to make the page executable, which allows machine code to read itself as data
at runtime. This is actually what an attacker often does after gaining a limited \
control of a process to find pieces of machine code they can use to gain the \
full control of the process.
.Pp
As a mitigation, some recent processors allows "execute-only" pages. \
If a page is execute-only, you can call a function there as long as you know \
its address but can't read it as data.
.Pp
This option marks text segments execute-only. This option currently works only \
on some ARM64 processors.
.Pp
.It Fl -exclude-libs Ns = Ns Ar libraries Ns ...
Mark all symbols in the given
.Ar libraries
hidden.
.Pp
.It Fl -export-dynamic-symbol Ns = Ns Ar sym
Put symbols matching
.Ar sym
in the dynamic symbol table.
.Ar sym
may be a glob, with the same syntax as the globs used in
.Fl -export-dynamic-symbol-list
or
.Fl -version-script .
.Pp
.It Fl -export-dynamic-symbol-list Ns = Ns Ar file
Read a list of dynamic symbols from
.Ar file .
.Pp
.It Fl -fatal-warnings
.It Fl -no-fatal-warnings
Treat warnings as errors.
.Pp
.It Fl -fini Ns = Ns Ar symbol
Call
.Ar symbol
at unload-time.
.Pp
.It Fl -gc-sections
.It Fl -no-gc-sections
Remove unreferenced sections.
.Pp
.It Fl -gdb-index
Create a
.Li .gdb_index
section to speed up GNU debugger. To use this, you need to compile source files \
with the
.Fl ggnu-pubnames
compiler flag.
.Pp
.It Fl -hash-style Ns = Ns Op Sy sysv | gnu | both
Set hash style.
.Pp
.It Fl -icf Ns = Ns Op Sy none | safe | all
.It Fl -no-icf
It is not uncommon for a program to contain many identical functions that differ \
only in name. For example, a C++ template
.Sy std::vector
is very likely to be instantiated to the identical code for
.Sy std::vector<int>
and
.Sy std::vector<unsigned>
because the container cares only about the size of the parameter type. \
Identical Code Folding (ICF) is a size optimization to identify and merge \
such identical functions.
.Pp
If
.Fl -icf=all
is given,
.Nm
tries to merge all identical functions. This reduces the size of the output \
most, but it is not
.Dq safe
optimization. It is guaranteed in C and C++ that two pointers pointing two \
different functions will never be equal, but
.Fl -icf=all
breaks that assumption as two functions have the same address after merging. \
So a care must be taken when you use that flag that your program does not \
depend on the function pointer uniqueness.
.Pp
.Fl -icf=safe
is a flag to merge functions only when it is safe to do so. That is, if a \
program does not take an address of a function, it is safe to merge that \
function with other function, as you cannot compare a function pointer \
with something else without taking an address of a function.
.FL -icf=safe
needs to be used with a compiler that supports
.Sy .llvm_addrsig
section which contains the information as to what symbols are address-taken. \
LLVM/Clang supports that section by default. Since GCC does not support it \
yet, you cannot use
.Fl -icf=safe
with GCC (it doesn't do any harm but can't optimize at all.)
.Pp
.Fl -icf=none
and
.Fl -no-icf
disables ICF.
.Pp
.It Fl -ignore-data-address-equality
Make ICF to merge not only functions but also data. This option should be \
used in combination with
.Fl -icf=all .
.Pp
.It Fl -image-base Ns = Ns Ar addr
Set the base address to
.Ar addr .
.Pp
.It Fl -init Ns = Ns Ar symbol
Call
.Ar symbol
at load-time.
.Pp
.It Fl -no-undefined
Report undefined symbols (even with
.Fl -shared ) .
.Pp
.It Fl -noinhibit-exec
Create an output file even if errors occur.
.Pp
.It Fl -pack-dyn-relocs Ns = Ns Op Sy none | relr
If
.Sy relr
is specified, all
.Li R_*_RELATIVE
relocations are put into
.Li .relr.dyn
section instead of
.Li .rel.dyn
or
.Li .rela.dyn
section. Since
.Li .relr.dyn
section uses a space-efficient encoding scheme, specifying this flag \
can reduce the size of the output. This is typically most effective \
for position-independent executable.
.Pp
Note that a runtime loader has to support
.Li .relr.dyn
to run executables or shared libraries linked with
.Fl -pack-dyn-relocs=relr ,
and only ChromeOS, Android and Fuchsia support it as of now in 2022.
.Pp
.It Fl -package-metadata Ns = Ns Ar string
Embed
.Ar string
to a .note.package section. This option in intended to be used by a \
package management command such as
.Cm rpm
to embed metadata regarding a package to each executable file.
.Pp
.It Fl -pie , -pic-executable
.It Fl -no-pie , -no-pic-executable
Create a position-independent executable.
.Pp
.It Fl -print-gc-sections
.It Fl -no-print-gc-sections
Print removed unreferenced sections.
.Pp
.It Fl -print-icf-sections
.It Fl -no-print-icf-sections
Print folded identical sections.
.Pp
.It Fl -push-state
.It Fl -pop-state
.Fl -push-state
saves the current values of
.Fl -as-needed ,
.Fl -whole-archive ,
.Fl -static ,
and
.Fl -start-lib .
The saved values can be restored by
.Fl -pop-state .
.Pp
.Fl -push-state
and
.Fl -pop-state
pairs can nest.
.Pp
These options are useful when you want to construct linker command line \
options programmatically. For example, if you want to link
.Ar libfoo.so
by as-needed basis but don't want to change the global state of
.Fl -as-needed ,
you can append "--push-state --as-needed -lfoo --pop-state" to the \
linker command line options.
.Pp
.It Fl -relax
.It Fl -no-relax
Rewrite machine instructions with more efficient ones for some relocations.
The feature is enabled by default.
.Pp
.It Fl -require-defined Ns = Ns Ar symbol
Like
.Fl -undefined ,
except the new symbol must be defined by the end of the link.
.Pp
.It Fl -retain-symbols-file Ns = Ns Ar file
Keep only symbols listed in
.Ar file .
.Pp
.Ar file
is a text file
containing a symbol name on each line.
.Nm
discards all local
symbols as well as global symbol that are not in
.Ar file .
Note that this option removes symbols only from
.Dv .symtab
section and does not affect
.Dv .dynsym
section, which is used for dynamic linking.
.Pp
.It Fl -rpath Ns = Ns Ar dir
Add
.Ar dir
to runtime search path.
.Pp
.It Fl -section-start Ns = Ns Ar section Ns = Ns Ar address
Set
.Ar address
to
.Ar section .
.Ar address
is a hexadecimal number that may start with an optional
.Sq 0x .
.Pp
.It Fl -shared , -Bshareable
Create a share library.
.Pp
.It Fl -spare-dynamic-tags Ns = Ns Ar number
Reserve given
.Ar number
of tags in
.Dv .dynamic
section.
.Pp
.It Fl -start-lib
.It Fl -end-lib
Handle object files between
.Fl -start-lib
and
.Fl -end-lib
as if they were in an archive file. That means object files between them \
are linked only when they are needed to resolve undefined symbols. \
The options are useful if you want to link object files only when they are \
needed but want to avoid the overhead of running
.Xr ar 3 .
.Pp
.It Fl -static
Do not link against shared libraries.
.Pp
.It Fl -sysroot Ns = Ns Ar dir
Set target system root directory to
.Ar dir .
.It Fl -trace
Print name of each input file.
.Pp
.It Fl -undefined-version
.It Fl -no-undefined-version
By default,
.Nm
warns on a symbol specified by a version script or by
.Fl -export-dynamic-symbol
if it is not defined. You can silence the warning by
.Fl -undefined-version .
.Pp
.It Fl -unique Ns = Ns Ar pattern
Don't merge input sections that match
.Ar pattern .
.Pp
.It Fl -unresolved-symbols Ns = Ns Op Sy \
report-all | ignore-all | ignore-in-object-files | ignore-in-shared-libs
How to handle undefined symbols.
.Pp
.It Fl -version-script Ns = Ns Ar file
Read version script from
.Ar file .
If
.Ar file
does not exist in the current directory, it is searched from library search paths \
for the sake of compatibility with GNU ld.
.Pp
.It Fl -warn-common
.It Fl -no-warn-common
Warn about common symbols.
.Pp
.It Fl -warn-once
Only warn once for each undefined symbol instead of warn for each relocation
referring an undefined symbol.
.Pp
.It Fl -warn-unresolved-symbols
.It Fl -error-unresolved-symbols
Normally, the linker reports an error for unresolved symbols.
.Fl -warn-unresolved-symbols
option turns it into a warning.
.Fl -error-unresolved-symbols
option restores the default behavior.
.Pp
.It Fl -whole-archive
.It Fl -no-whole-archive
When archive files
.Pf ( Sy .a
files) are given to a linker, only object
files that are needed to resolve undefined symbols are extracted from
them and linked to an output file.
.Fl -whole-archive
changes that behavior for subsequent archives so that a linker extracts all
object files and link them to an output.
For example, if you are creating a shared object file and you want to include \
all archive members to the output, you should pass
.Fl -whole-archive .
.Fl -no-whole-archive
restores the default behavior for subsequent archives.
.Pp
.It Fl -wrap Ns = Ns Ar symbol
Make
.Ar symbol
to be resolved to
.Sy __wrap_ Ns Ar symbol .
The original symbol can be resolved as
.Sy __real_ Ns Ar symbol .
This option is typically used for wrapping an existing function.
.Pp
.It Fl z Cm cet-report Ns = Ns Op Sy none | warning | error
Intel Control-flow Enforcement Technology (CET) is a new x86 feature \
available since Tiger Lake which is released in 2020.
It defines new instructions to harden security to protect programs from \
control hijacking attacks. You can tell compiler to use the feature by \
specifying the
.Fl fcf-protection
flag.
.Pp
.Fl z Cm cet-report
flag is used to make sure that all object files were compiled with a correct
.Fl fcf-protection
flag. If
.Sy warning
or
.Sy error
are given,
.Nm
prints out a warning or an error message if an object file was not compiled \
with the compiler flag.
.Pp
.Nm
looks for
.Li GNU_PROPERTY_X86_FEATURE_1_IBT
bit and
.Li GNU_PROPERTY_X86_FEATURE_1_SHSTK
bit in
.Li .note.gnu.property
section to determine whether or not an object file was compiled with
.Fl fcf-protection .
.Pp
.It Fl z Cm now
.It Fl z Cm lazy
By default, functions referring other ELF modules are resolved by the
dynamic linker when they are called for the first time.
.Fl z Cm now
marks an executable or a shared library file so that all dynamic
symbols are loaded when a file is loaded to memory.
.Fl z Cm lazy
restores the default behavior.
.Pp
.It Fl z Cm origin
Mark object requiring immediate
.Dv $ORIGIN
processing at runtime.
.Pp
.It Fl z Cm ibt
Turn on
.Li GNU_PROPERTY_X86_FEATURE_1_IBT
bit in
.Li .note.gnu.property
section to indicate that the output uses IBT-enabled PLT. This option implies
.Fl z Cm ibtplt .
.Pp
.It Fl z Cm ibtplt
Generate Intel Branch Tracking (IBT)-enabled PLT which is the default on x86-64.
.Pp
.It Fl z Cm execstack
.It Fl z Cm noexecstack
By default, the pages for the stack area (i.e. the pages where local
variables reside) are not executable for security reasons.
.Fl z Cm execstack
makes it executable.
.Fl z Cm noexecstack
restores the default behavior.
.Pp
.It Fl z Cm keep-text-section-prefix
.It Fl z Cm nokeep-text-section-prefix
Keep
.Dv .text.hot ,
.Dv .text.unknown ,
.Dv .text.unlikely ,
.Dv .text.startup
and
.Dv .text.exit
as separate sections in the final binary.
.Pp
.It Fl z Cm relro
.It Fl z Cm norelro
Some sections such as
.Dv .dynamic
have to be writable only during an executable or \
a shared library file is being loaded to memory.
Once the dynamic linker finishes its job,
such sections won't be mutated by anyone.
As a security mitigation,
it is preferred to make such segments read-only during program execution.
.Pp
.Fl z Cm relro
puts such sections into a special segment called
.Dv relro .
The dynamic linker make a relro segment read-only after it finishes its job.
.Pp
By default,
.Nm
generates a relro segment.
.Fl z Cm norelro
disables the feature.
.Pp
.It Fl z Cm separate-loadable-segments
.It Fl z Cm separate-code
.It Fl z Cm noseparate-code
If one memory page contains multiple segments,
the page protection bits are set in such a way that needed attributes \
(writable or executable) are satisfied for all segments.
This usually happens at a boundary of two segments with two different \
attributes.
.Pp
.Cm separate-loadable-segments
adds paddings between segments with different attributes so that they \
do not share the same page.
This is the default.
.Pp
.Cm separate-code
adds paddings only between executable and non-executable segments.
.Pp
.Cm noseparate-code
does not add any paddings between segments.
.Pp
.It Fl z Cm defs
.It Fl z Cm nodefs
Report undefined symbols (even with
.Fl -shared ) .
.Pp
.It Fl z Cm shstk
Enforce shadow stack by turning GNU_PROPERTY_X86_FEATURE_1_SHSTK bit in
.Li .note.gnu.property
output section. Shadow stack is part of Intel Control-flow Enforcement \
Technology (CET), which is available since Tiger Lake (2020).
.Pp
.It Fl z Cm text
.It Fl z Cm notext , Fl z Cm textoff
.Nm
by default reports an error if dynamic relocations are created in read-only \
sections.
If
.Fl z Cm notext
or
.Fl z Cm textoff
are given,
.Nm
creates such dynamic relocations without reporting an error.
.Fl z Cm text
restores the default behavior.
.Pp
.It Fl z Cm max-page-size
Some CPU ISAs support multiple different memory page sizes.
This option specifies the maximum page size that an output binary can run on.
If you specify a large value, the output can run on both large and small page \
systems, but it wastes a bit of memory at page boundaries on systems with \
small pages.
.Pp
The default value is 4 KiB for i386, x86-64 and RISC-V, and 64 KiB for ARM64.
.Pp
.It Fl z Cm nodefaultlib
Make the dynamic loader to ignore default search paths.
.Pp
.It Fl z Cm nodelete
Mark DSO non-deletable at runtime.
.Pp
.It Fl z Cm nodlopen
Mark DSO not available to
.Xr dlopen 3 .
This option makes it possible for the linker to optimize thread-local \
variable accesses by rewriting instructions for some targets.
.Pp
.It Fl z Cm nodump
Mark DSO not available to
.Xr dldump 3 .
.Pp
.It Fl z Cm nocopyreloc
Do not create copy relocations.
.Pp
.It Fl z Cm initfirst
Mark DSO to be initialized first at runtime.
.Pp
.It Fl z Cm interpose
Mark object to interpose all DSOs but executable.
.Pp
.ig
.It Fl (
.It Fl )
.It Fl EL
.It Fl O Ns Ar number
.It Fl -allow-shlib-undefined
.It Fl -dc
.It Fl -dp
.It Fl -end-group
.It Fl -no-add-needed
.It Fl -no-allow-shlib-undefined
.It Fl -no-copy-dt-needed-entries
.It Fl -no-fatal-warnings
.It Fl -nostdlib
.It Fl -rpath-link Ns = Ns Ar dir
.It Fl -sort-common
.It Fl -sort-section
.It Fl -start-group
.It Fl -warn-constructors
.It Fl -warn-once
.It Fl fix-cortex-a53-835769
.It Fl fix-cortex-a53-843419
.It Fl z combreloc
.It Fl z common-page-size
.It Fl z nocombreloc
Ignored
..
.
.El \" End of options list
.
.\"=============================================================================
.Sh SEE ALSO
.Xr gold 1 ,
.Xr ld 1 ,
.Xr elf 5 ,
.Xr ld.so 8
.
.\"=============================================================================
.Sh AUTHORS
.An Rui Ueyama Aq Mt ruiu@cs.stanford.edu
.
.\"=============================================================================
.Sh BUGS
Report bugs to
.Lk https://github.com/rui314/mold/issues .
|