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

3724 Commits

Author SHA1 Message Date
Rui Ueyama
825b918146 Do not use dlopen only when built with ./build-static.sh
This commit fixes a regression in f4a9b05114
that mold crashed when it tried to dlopen a linker plugin shared library.
2022-02-18 15:18:56 +09:00
Rui Ueyama
19587d43ee Rename a variable 2022-02-18 12:53:34 +09:00
Rui Ueyama
f4a9b05114 Do not call dloepn() if built with ./build-static.sh
It looks like setting __TBB_DYNAMIC_LOAD_ENABLED to 0 is not enough
to make TBB not to call dlopen(). We need to unset __TBB_WEAK_SYMBOLS_PRESENT
as well.

Fixes https://github.com/rui314/mold/issues/348
2022-02-18 12:17:24 +09:00
Rui Ueyama
7735cf5b95 Style change 2022-02-17 20:29:28 +09:00
Rui Ueyama
fcb28161b1
Merge pull request #349 from dmantipov/use-explicit-mimalloc-new-delete-overrides-if-system-mimalloc
Use explicit mimalloc new/delete overrides if SYSTEM_MIMALLOC
2022-02-17 20:26:27 +09:00
Rui Ueyama
b57da5413c [ELF][LTO] Restart mold if a LTO plugin does not support get_symbols v3 API 2022-02-17 19:14:05 +09:00
Dmitry Antipov
630b262d2b Use explicit mimalloc new/delete overrides if SYSTEM_MIMALLOC
When building with SYSTEM_MIMALLOC=1, linking with (assuming
shared) '-lmimalloc' is not enough to override C++ new/delete
with mimalloc versions, and 'mimalloc-new-delete.h' should be
included from one (and the only) translation unit as well.

Signed-off-by: Dmitry Antipov <dantipov@cloudlinux.com>
2022-02-17 10:44:58 +03:00
Rui Ueyama
8f0acbf413 [ELF][LTO] Move LTO to the symbol resolution pass
ELF object files returned by do_lto() may contain unforeseen undefined
symbols, so we can't call that function after the symbol resolution pass
because it can cause undefined symbol errors.
2022-02-17 16:14:11 +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
0b7edc07f4 Format 2022-02-17 10:30:20 +09:00
Rui Ueyama
da48efe63b Update the man page 2022-02-16 19:36:26 +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
444cb7a566 [ELF] Improve the man page 2022-02-16 18:54:08 +09:00
Rui Ueyama
889f9f3a36 Simplify 2022-02-16 16:53:23 +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
b94dd6dbeb Revert "[ELF] add method to check whether an input file is shared object"
This reverts commit ddcb7b4197. `is_dso`
is used by hot functions such as `Symbol::get_addr()`, so we want to
eliminate the cost of virtual function dispatch.
2022-02-16 09:51:07 +09:00
Rui Ueyama
a7333bdb4e
Merge pull request #343 from dmantipov/fix-annoying-unused-variable-warnings
[ELF] Fix -Wunused-variable warnings
2022-02-14 21:27:57 +09:00
Dmitry Antipov
881b55d691 [ELF] Fix -Wunused-variable warning
Fix the following -Wunused-variable warning as reported by gcc:

elf/input-sections.cc:116:8: warning: unused variable ‘is_code’
[-Wunused-variable]
  116 |   bool is_code = (shdr().sh_flags & SHF_EXECINSTR);

Signed-off-by: Dmitry Antipov <dantipov@cloudlinux.com>
2022-02-14 15:22:00 +03:00
Rui Ueyama
28236b1b34 [ELF][LTO] Report an error if an IR file wasn't claimed
And remove an unused variable. Found by Dmitry Antipov.
2022-02-14 20:59:32 +09:00
Rui Ueyama
7e62ce4bd8 Improve an error message 2022-02-14 13:59:01 +09:00
Rui Ueyama
b0d75f8957 [ELF] Document --start-lib and --end-lib
Also extends the description of --push-state and --pop-state.

Fixes https://github.com/rui314/mold/issues/339
2022-02-13 19:57:06 +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
e18eaac3eb Update README 2022-02-13 15:18:08 +09:00
Rui Ueyama
5f17da80d0 [ELF][LTO] Mark IR objects as alive 2022-02-13 12:57:28 +09:00
Rui Ueyama
1663bcf932 Rename test files 2022-02-13 12:22:18 +09:00
Rui Ueyama
16a3c9491f [ELF][LTO] Fix "too many open files" issue
read_lto_object is called on each archive member, so if an archive file
contains lots of members, it can exhaust the pool of file descriptors.
This patch fixes the issue by opening an archive only once.
2022-02-13 12:22:18 +09:00
Rui Ueyama
bab74c0c7e
Merge pull request #342 from sylvestre/wording
Readme: Improve the wording
2022-02-13 11:41:57 +09:00
Sylvestre Ledru
5c5d50929e Improve the wording
Patch by Josh Triplett
https://salsa.debian.org/pkg-llvm-team/mold/-/merge_requests/8/diffs

Signed-off-by: Sylvestre Ledru <sylvestre@debian.org>
2022-02-12 22:53:19 +01:00
Rui Ueyama
ce744aeca4 Refactor 2022-02-12 20:34:49 +09:00
Rui Ueyama
0222dc6f39 [ELF] Drop SHF_GROUP flag from section header
SHF_GROUP is sometimes set to comdat group members. Since we process
comdat groups and uniquify them, that flag doesn't make sense in the
output file.
2022-02-12 20:17:57 +09:00
Rui Ueyama
46f954d23d [ELF][LTO] Returns a more precise value from get_symbols callback 2022-02-12 20:06:44 +09:00
Rui Ueyama
42b3038463 [ELF][LTO] Factor out LTO type definitions to lto.h 2022-02-12 17:50:36 +09:00
Rui Ueyama
c9612d3120 Refactor 2022-02-12 17:36:38 +09:00
Rui Ueyama
60d0900496 [ELF][LTO] Fix "concurrent hash table full" error
Since compiled IR objects may contain mergeable string sections, we
don't know the estimated number of items in output mergeable sections.
Previously, we created the concurrent hash tables for output mergeable
sections before compiling IR objects, so there was a chance to see
the "concurrent hash table full" error.

This commit moves the code to add section pieces to output mergeable
sections after `do_lto`.
2022-02-12 16:53:36 +09:00
Rui Ueyama
c1963ce2ac [ELF][LTO] Recognize IR files generated by GCC 11 2022-02-12 16:28:38 +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
61eb265cf4 [ELF][LTO] Report an error if -plugin was not given 2022-02-12 10:17:21 +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
dd8002f092 [ELF] Detect GCC LTO object files early 2022-02-11 20:57:19 +09:00
Rui Ueyama
6ee5aea1bf Add a comment about how to build mold with AddressSanitizer 2022-02-11 18:16:20 +09:00
Rui Ueyama
81cf1171a9 Simplify 2022-02-10 18:23:59 +09:00
Rui Ueyama
955c6d1fe4 Fix ODR violation 2022-02-10 15:26:26 +09:00