1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-04 16:48:04 +03:00
Commit Graph

556 Commits

Author SHA1 Message Date
Christoph Erhardt
3cbc57f9af Skip reloc-rodata test on aarch64
The test expects an error when an object compiled with `-fno-PIC` is
linked with `-pie`, but that doesn't happen on aarch64.

Signed-off-by: Christoph Erhardt <github@sicherha.de>
2022-02-22 06:41:01 +01:00
Rui Ueyama
a054bcd4ff [ELF] Add the --dependency-file=FILE option 2022-02-22 13:18:04 +09:00
Rui Ueyama
4a6e71096c Fix Gentoo build script as build-static.sh is gone 2022-02-19 21:57:36 +09:00
Rui Ueyama
a3f2a5c7ec Format 2022-02-17 13:54:15 +09:00
Rui Ueyama
b5db63f4bb [ELF] Allow relative relocs against absolute symbols
In the previous commit (c325cc039a),
I argued that PC-relative relocations are not representable if they
refer absolute symbols, and I made a change so that such relocations
are handled as errors.

Even though what I did is technically correct, that results in a failure
of an important use case of the weak undefined symbol. Here is why.

If you have an weak undefined symbol `foo`, you would use it as follows:

  if (foo)
    foo();

If `foo` is defined (i.e. has an address other than 0), `foo` is called.

If `foo` is undefined (i.e. has the address 0), the subsequent CALL
instruction will have an offset from the CALL instruction to address 0.
That displacement is computed at link-time. If the output is position-
independent, the call instruction may not have a correct displacement
from address 0 to the instruction at runtime due to base relocation.

However, that's not a problem in practice, because the function call
is guarded by `if (foo)`, and that will always evaluated to false
if `foo` is undefined. Therefore, the faulty `CALL` instruction will
never be executed.

So, that means we need to relocate PC-relative relocations against
absolute symbols even if doing so results in an incorrect result.
This patch implement that.
2022-02-17 12:07:48 +09:00
Rui Ueyama
c325cc039a [ELF] Report an error for an unrepresentable reloc against abs symbol
PC-relative relocations against absolute symbols are not representable
in position-independent code. In order to support such relocations,
someone has to compute `S + A - P` or subtract `B` as a base relocation,
but such dynamic relocations don't exist.

This is contrary to non-PC-relative relocations against non-absolute
symbols. In this case, we can simply use the base relocation (e.g.
R_X86_64_RELATIVE).

https://github.com/rui314/mold/issues/348
2022-02-17 11:21:52 +09:00
Rui Ueyama
3f1b071ae5 [ELF] Remove --warn-shared-textrel
It looks like the option is supported only by gold, and it doesn't
make much sense to me to have this option along with more generic
--warn-textrel. So I want to remove this before the next release.
2022-02-16 19:36:26 +09:00
Rui Ueyama
e4c4ab053e [ELF] Remove --shuffle-sections=N
This patch removes the --shuffle-sections option that takes an
argument as I'm still thinking about a better name for the option.

You can still use `--shuffle-sections` that doesn't take an argument.
2022-02-16 15:55:47 +09:00
Rui Ueyama
10ff2d705c Fix CI 2022-02-16 15:26:39 +09:00
Rui Ueyama
31f7427859 Rename a test file 2022-02-16 15:04:09 +09:00
Rui Ueyama
c338540885 [ELF] Handle relocations with r_sym == 0 correctly
If a relocation's r_sym value is 0 (i.e. it refers the undefined symbol
at the beginning of the symbol table), it has to handled as if S were 0.
We used to handle as if S were -1.

In addition to that, even if a relocation needs a dynamic base relocation,
it doesn't mean its expression is always S+A. For example, calling an
absolute address needs a dynamic relocation but its expression is S+A-P.

Fixes https://github.com/rui314/mold/issues/348
2022-02-16 13:18:22 +09:00
Rui Ueyama
d908d4c61c [ELF] Handle global symbols in mergeable sections correctly
Prior to this patch, mold computeed a wrong address for a global
symbol in a mergeable section. believe it was a regression in
60d0900496.

Fixes https://github.com/rui314/mold/issues/347
2022-02-16 12:05:40 +09:00
Rui Ueyama
68a35bfa03 Fix CI 2022-02-13 16:52:54 +09:00
Rui Ueyama
70ec56f8bb [ELF][LTO] Export symbols from DSOs correctly
With this change, mold can now build Clang with LTO and all Clang tests pass.
2022-02-13 15:34:37 +09:00
Rui Ueyama
1663bcf932 Rename test files 2022-02-13 12:22:18 +09:00
Rui Ueyama
1dcd20b401 Fix CI 2022-02-12 15:39:28 +09:00
Rui Ueyama
7e91897154 [ELF] Add the --shuffle-sections option
If the --shuffle-sections is given, mold now randomize the output by
shuffling input sections randomly. This feature is compatible with lld's
--shuffle-sections=SEED option introduced in
https://reviews.llvm.org/D74791.

This feature is useful when you want to equalize the conditions of
benchmarks. That is, some particular memory layout can produce a very
good benchmark number due to hardware-level cache hit rate or something
like that. Therefore, even if you get a good benchmark number after
changing code, there's a chance that that's caused by the layout change
and not by the new code itself. With --shuffle-sections, you can isolate
that.

The other use case I can think of is to enhance security. If you build
your program as a position-independent executable, the kernel
automatically enables ASLR (Address Space Layout Randomization), but ASLR
only shift the entire program image in memory by some random offset;
Relative offsets between sections remain the same. If you compile programs
from source, by using --shuffle-sections, you can make the offsets
unpredictable to attackers.
2022-02-12 15:32:37 +09:00
Rui Ueyama
46995bcfc3 [ELF] Support LTO plugin API
The LTO plugin API support is still in progress, but with this change,
mold can link itself with `-flto` with both GCC and Clang.

Since mold now supports LTO natively, I removed the fallback mechanism
to ld.bfd or ld.lld that I implemented in
a5029d19a8.

Fixes https://github.com/rui314/mold/issues/181
2022-02-11 21:09:28 +09:00
Rui Ueyama
ebc2bfafdd [ELF] Fix ICF crash bug
`-icf=all` couldn't handle very small programs that has no sections
to process in the main loop. This commit fixes that crash bug.

Fixes https://github.com/rui314/mold/issues/332
2022-02-08 16:34:25 +09:00
Rui Ueyama
73159e241d [ELF] Add .got.plt to PT_GNU_RELRO if -z now
If external symbols are resolved eagerly on process startup, .got.plt
can be read-only and therefore can be in the PT_GNU_RELRO segment.
2022-02-07 17:12:53 +09:00
Rui Ueyama
e65c5d2a1c [ELF] Handle ABI incompatibility between -fgnu-unique and -fno-gnu-unique
GCC creates symbols in comdat groups as STB_GNU_UNIQUE instead of STB_WEAK
if it was configured to do so at build time or the -fgnu-unique option was
given. If mold is given two object files with and without STB_GNU_UNIQUE,
it could end up selecting a sybmol that is in a de-duplicated comdat group.

This is arguably just an ABI incompatibility. Two comdat groups must
contain the same contents if their identifiers are the same. But we
can't handle it as an error because it is not uncommon to link object
files compiled using Clang (or GCC without -fgnu-unique) to static
libraries built with GCC that produces STB_GNU_UNIQUE symbols.

This patch gives the same priority to STB_GNU_UNIQUE as STB_WEAK so that
mold won't select symbols in discarded comdat groups.

Frankly, the situation around STB_GNU_UNIQUE is a mess. That GNU extension
shouldn't have been added to the GNU toolchain in the first place.
It looks like GCC shipped with Linux distros are nowadays do not produce
STB_GNU_UNIQUE symbols by default, but we still need to handle them.

Fixes https://github.com/rui314/mold/issues/324
2022-02-07 07:53:58 +09:00
Rui Ueyama
27c50e8158 [ELF] Report an error for an unresolved hidden undef correctly
Previously, if a protected/hidden undef symbol is resolved to a DSO
symbol,  mold didn't report a symbol undefined error.

Fixes https://github.com/rui314/mold/issues/329
2022-02-06 13:49:17 +09:00
Rui Ueyama
6ffcae4286 [ELF] Add --warn-textrel 2022-02-05 13:26:38 +09:00
Rui Ueyama
03e0cef726 [ELF] Add --warn-shared-textrel 2022-02-05 12:15:30 +09:00
Rui Ueyama
a4d9bc7550 Fix CI 2022-02-05 12:15:17 +09:00
Rui Ueyama
f24b997501 [ELF} Add --warn-once 2022-02-05 11:39:59 +09:00
Rui Ueyama
26fe71d64c [ELF] Support --emit-relocs
This commit adds the `--emit-relocs` option to mold. If the flag is
given, mold copies relocation sections from input files to an output
file. This option is used by some binary post-processing tools such
as Facebook's Bolt.

It looks like the relocations emitted by `--emit-relocs` are correct,
but I didn't do extensive testing. If you find any problem, please
let me know.

Fixes https://github.com/rui314/mold/issues/177
2022-02-04 18:42:08 +09:00
Rui Ueyama
354d1b7d2f [ELF] Fix CI 2022-02-04 18:34:57 +09:00
Rui Ueyama
1550b5afff [ELF] Copy symbols from DSOs to an output file
Previously, mold didn't copy symbols from .so input files to the
output .symtab.
2022-02-04 18:03:17 +09:00
Rui Ueyama
e4c03c238e [ELF] Write section symbols to .symtab 2022-02-04 17:05:06 +09:00
Rui Ueyama
d0ab7d9e17 Refactor 2022-02-04 16:14:00 +09:00
Rui Ueyama
e98aab7ea9 [ELF] Reimplement --repro
`--repro` is a hidden command flag for debugging.

Previously, if the flag was given, mold would create a `.repro`
section in an output file with all input files as its contents.
The flaw of the design is that when mold fails to create an output
file, no .repro section would be created.

So I changed the behavior in this commit. Now, the tar file is
created as an independent file.
2022-02-01 13:47:44 +09:00
Rui Ueyama
00203dbc7c [ELF][RISC-V] Implement TLS_GOT_HI20 and TLS_GD_HI20
Now all test passed on my SiFive Unmatched RISC-V PC.
2022-01-29 22:35:04 +09:00
Rui Ueyama
e061e3ca70 [ELF][RISC-V] Add a test for dynamically-linked executable 2022-01-29 14:24:46 +09:00
Rui Ueyama
e76f7c0d47 Add an initial support of RISC-V
With this patch, mold can now produce a statically-link "Hello wrold"
program for RISC-V.
2022-01-28 18:08:42 +09:00
Rui Ueyama
b4fa51e981 Fix tests for musl libc 2022-01-24 10:29:31 +09:00
Rui Ueyama
dc7432a6dd Avoid GNU extensions in tests 2022-01-24 10:21:08 +09:00
Rui Ueyama
0a0f9b3ad5 [ELF] Protect the last page of a RELRO segment
PT_GNU_RELRO works on page granularity. We always align the begining
of a RELRO segment to a page boundary, but the end was not. Since the
runtime conservatively align _down_ it to a page boundary, the last
page weren't be marked as read-only.

This patch makes the size of a RELRO always a multiple of the page size.
2022-01-24 09:40:21 +09:00
Rui Ueyama
f8e7b157c7 Fix a test 2022-01-23 11:41:19 +09:00
Rui Ueyama
5cf2328a8b [ELF] Support negation and range in glob pattern 2022-01-22 11:01:55 +09:00
Rui Ueyama
e9a5d1cee3 [ELF] Handle glob patterns directly instead of converting them to regex
Converting a glob pattern to a regex is fragile, and std::regex is slow.
So we should handle glob patterns ourselves.
2022-01-21 23:32:27 +09:00
Rui Ueyama
4ee3062054 [ELF] Fix version pattern matching 2022-01-21 21:27:49 +09:00
Rui Ueyama
d0c1c4db19 [ELF] Use Aho-Corasick algo to handle version script patternsC
To process version scriots, we have to match glob patterns against
symbol strings. Sometimes, we have hundreds or thousands of glob
patterns and have to match them against millions of mangled long
C++ symbol names. This step can be very slow.

In this patch, I implemented the Aho-Corasick algorithm to match glob
patterns to symbol strings as quickly as possible. For the details
of the algorithm, see https://en.wikipedia.org/wiki/Glob_(programming).

This patch improves mold's performance for programs that uses large
version scripts. For example, linking libQt6Gui.so.6.3.0 reduced from
1.10s to 0.05s with this patch.

This patch also changes how symbol versions are applied if two or more
version patterns match to a single symbol string. Previously, the last
one in a script file took precedence. Now, the first one takes
precedence. I believe the new behavior is compatible with GNU ld.

Fixes https://github.com/rui314/mold/issues/156
Fixed https://github.com/rui314/mold/issues/287
2022-01-21 20:30:05 +09:00
Rui Ueyama
8b1905453d [ELF] Do not use BND prefix
BND prefix is defined by Intel MPX extension which is now deprecated by Intel.
2022-01-20 13:39:31 +09:00
Rui Ueyama
1b01448f01
Merge pull request #286 from tklauser/test-exception-aarch64
[ELF] Pass -fno-PIC along with -mcmodel=large on aarch64
2022-01-20 09:46:42 +09:00
Rui Ueyama
809652d740 [ELF] Handle glob patterns other than * for local: in version scripts
Previously, we didn't handle version scripts like this correctly:

  ver {
    global: *;
    local: foo*;
  }

We didn't handle `local:` part correctly except for `*`.

Fixes https://github.com/rui314/mold/issues/277
2022-01-20 08:51:04 +09:00
Tobias Klauser
fbd4e559e6
[ELF] Pass -fno-PIC along with -mcmodel=large on aarch64
The -mcmodel=large option is incompatible with -fPIC on aarch64, see
https://gcc.gnu.org/onlinedocs/gcc/AArch64-Options.html#index-mcmodel_003dlarge

This makes test/elf/exception.sh pass on linux/arm64.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2022-01-19 21:36:53 +01:00
Rui Ueyama
9453488424 [ELF] Fix test for ARM64 2022-01-18 13:28:57 +09:00
Rui Ueyama
3411511a8b [ELF] Ignore i386-specific linkonce symbols
This is a follow-up to the previously commit patch
0c19046721. We needed to ignore symbols
whose address is in a discarded .gnu.linkonce sections.

Fixes https://github.com/rui314/mold/issues/270
2022-01-15 14:28:34 +09:00
Rui Ueyama
bd6afa1b23 [ELF] Add -pack-dyn-relocs=relr
.relr.dyn is a new section that has been implemented in other linkers
recently. That section contains only the RELATIVE-type dynamic
relocations (i.e. base relocations). Compared to the regular
.rela.dyn, a .relr.dyn's size is typically less than 1/10 because the
section is compressed.

Since PIEs (position-independent executables) tend to contain lots of
RELATIVE-type relocations and PIEs are now the default on many Linux
distributions for security reasons, .relr.dyn is more effective than
it was. It can reduce binary size by a few percent or more.

Note that the runtime support is catching up, so binaries built with
`-pack-dyn-relocs=relr` may not work on your system unless you are
running a very recent version of Linux.
2022-01-14 20:54:37 +09:00