1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-05 09:07:10 +03:00
Commit Graph

147 Commits

Author SHA1 Message Date
Rui Ueyama
4caadefac6 Refactor 2022-02-27 15:21:06 +09:00
Rui Ueyama
e413433752 [ELF][LTO] Add lld-compatible LTO-related command line options
Fixes https://github.com/rui314/mold/issues/367
2022-02-27 09:28:51 +09:00
Rui Ueyama
3f9868238b [ELF] Add the --shuffle-sections-seed=NUMBER option 2022-02-22 15:41:51 +09:00
Rui Ueyama
3e8c519d4e [ELF] Add the --reverse-sections option
This option reverse input sections before assigning offsets to them.
This is useful to detect an unintended dependency between static
initializers in different translation units. C++ only guarantees that
static initializers are invoked from top to bottom in a single
translation unit, and it doesn't guaratee anything about the order of
initialization between translation units.

By (partially) reversing the contents of .init_array using
--reverse-sections, you can run your program with a completely
reversed initialization order.
2022-02-22 15:41:51 +09:00
Rui Ueyama
a054bcd4ff [ELF] Add the --dependency-file=FILE option 2022-02-22 13:18:04 +09:00
Rui Ueyama
d258140d54 Inline a default constructor 2022-02-20 16:04:00 +09:00
Rui Ueyama
6ce142d73a Remove redundant default destructors 2022-02-20 16:02:05 +09:00
Rui Ueyama
15fa05eff6 [ELF][LTO] Clear symbols after LTO
This commit ensure that all symbol resolution results are cleared
after LTO, so that the following second symbol resolution will not
be affected by the previous results.
2022-02-18 22:05:14 +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
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
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
46f954d23d [ELF][LTO] Returns a more precise value from get_symbols callback 2022-02-12 20:06:44 +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
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
dd8002f092 [ELF] Detect GCC LTO object files early 2022-02-11 20:57:19 +09:00
Rui Ueyama
955c6d1fe4 Fix ODR violation 2022-02-10 15:26:26 +09:00
Rui Ueyama
7546b44111 [ELF] Add --print-dependencies=full
This is an experimental feature. We haven't decided as to whether we'll
include this flag into the next release.

This is an example output of the flag when linking mold with mold:
https://gist.github.com/rui314/3f5d226814bb1188c535f1f86706cea5
2022-02-09 13:52:37 +09:00
Rui Ueyama
249c331117 Refactor 2022-02-08 16:30:41 +09:00
Dmitry Antipov
ddcb7b4197 [ELF] add method to check whether an input file is shared object
Prefer pure virtual 'mold:🧝:InputFile::is_dso()' (actually
implemented in 'mold:🧝:ObjectFile' and 'mold:🧝:SharedFile')
over the field of the same name, adjust related code.

Signed-off-by: Dmitry Antipov dantipov@cloudlinux.com
2022-02-08 10:19:56 +03:00
Rui Ueyama
5850f237fe [ELF] Do not set .copyrel.rel.ro to NOBITS if it's small 2022-02-08 10:09:57 +09:00
Rui Ueyama
0f8bf23bcf [ELF] Rename .dynbss -> .copyrel
Currently, the sections we create for copy relocations are named .dynbss
and .dynbss.rel.ro. Section names shouldn't be significant in ELF
executables, so we should be able to name them as we want.

However, in practice, valgrind treat a section with the name .dynbss
in a special way and crashes if an executable contains both .bss and
.dynbss due to the following assertion failure:

  $ valgrind ./prog
  ==1960252== Memcheck, a memory error detector
  ==1960252== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
  ==1960252== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
  ==1960252== Command: ./prog
  ==1960252==

  valgrind: m_debuginfo/readelf.c:2271 (vgModuleLocal_read_elf_debug_info): Assertion 'di->bss_svma + di->bss_size == svma' failed.
  Segmentation fault (core dumped)

This commit is a workaround for the valgrind issue. We simply rename
dynbss and .dynbss.rel.ro to .copyrel and .copyrel.rel.ro, respectively.

Fixes https://github.com/rui314/mold/issues/159
2022-02-08 10:09:54 +09:00
Rui Ueyama
a2fa0934d0 Refactor 2022-02-08 07:06:44 +09:00
Rui Ueyama
214c35e05d Refactor 2022-02-08 07:02:30 +09:00
Rui Ueyama
e5f4b967ed Do not link against libxxhash.so at all
Ensure that all xxhash functions are inlined. Since it eliminates
external dependencies, we no longer have to link against libxxhash.a
or libxxhash.so at all.

`SYSTEM_XXHASH` Makefile variable has been removed because it is
no longer relevant.
2022-02-07 20:06:59 +09:00
Rui Ueyama
a1287c26b9 [ELF] Add --print-dependencies
This is a mold-only experimental feature. If the flag is given,
mold prints out dependencies between input files as tab-separated
tuples of (`source-file`, `dest-file`, `symbol`). Each line of the
output means `source-file` depends on `dest-file` to use `symbol`.

With the dependency information, you can analyze for example why
some archive file member is included in a linker's output.
You can also for example know what symbols have to be removed
to eliminate the dependency to some shared library file.

I'm not sure if I'll include this flag in the next release or
document this feature, but it looks promising so far. If you like
or dislike this feature, please leave a comment on GitHub.

This is an example of the output when linking mold with mold:
https://gist.github.com/rui314/e1d70bbdebfabb881be7167314b58f48
2022-02-07 11:53:38 +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
f24b997501 [ELF} Add --warn-once 2022-02-05 11:39:59 +09:00
Rui Ueyama
579fafc04b Refactor 2022-02-05 09:31:43 +09:00
Rui Ueyama
77716836be Refactor 2022-02-05 08:58:20 +09:00
Rui Ueyama
afca9d55aa [ELF] Fix RISC-V code for -O2 2022-02-04 20:11:50 +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
dca8d58b51 Refactor 2022-02-04 18:26:41 +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
0542a2254b Refactor 2022-02-04 17:05:05 +09:00
Rui Ueyama
d0ab7d9e17 Refactor 2022-02-04 16:14:00 +09:00
Rui Ueyama
5c123e2c68 [ELF] Reduce the size of Symbol<E> from 64 to 54 bytes 2022-02-03 20:12:19 +09:00
Rui Ueyama
9b90f642a1 [ELF][RISC-V] Support relocation relaxation.
RISC-V psABI defines a mechanism for a linker to shrink a section
by rewriting instructions in it. This patch implements that feature.
2022-02-02 20:40:17 +09:00
Rui Ueyama
0acd4b9d2f [ELF] Reduce the size of InputSection by 8 bytes 2022-02-02 15:03:22 +09:00
Rui Ueyama
7fcff98f29 [ELF] Directly uncompress debug sections into the output mmap'ed buffer
Previously, we always uncompress compressed debug sections to
in-memory buffer before copying them to the final output buffer.
This patch eliminates that redundant copy whenever possible.
2022-02-02 14:18:48 +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
d3140a25f6 Refactor 2022-01-31 20:36:43 +09:00
Dmitry Antipov
35c0d4ae9a Fix -Wmissing-field-initializers
Add 'mold:🧝:GlobPattern::Element' and 'mold::macho::SplitInfo'
trivial constructors to fix all '-Wmissing-field-initializers' as
reported both by g++ and clang++.

Signed-off-by: Dmitry Antipov <dantipov@cloudlinux.com>
2022-01-31 12:50:01 +03:00
Christoph Grüninger
d301b8a3fe Add missing destructor to InputFile
Fixes Clang's delete-non-abstract-non-virtual-dtor warning

Signed-off-by: Christoph Grüninger <foss@grueninger.de>
2022-01-30 23:08:24 +01:00
Christoph Grüninger
3f442402f8 Fix typos in comments
Signed-off-by: Christoph Grüninger <foss@grueninger.de>
2022-01-30 22:56:43 +01:00
Rui Ueyama
f7b2ce278b [ELF] Reduce the size of InputSection class
This commit reduces the size of InputSection class from 144 bytes
to 120 bytes. Since we create lots of instance of the class, the
size of it matters.
2022-01-30 20:25:20 +09:00
Rui Ueyama
5c89a6d08c [ELF][RISC-V] Set ELF e_flags field 2022-01-29 16:26:24 +09:00