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

716 Commits

Author SHA1 Message Date
Rui Ueyama
d4bbf31db2 [ELF] Add the --noinhibit-exec option
Fixes https://github.com/rui314/mold/issues/370
2022-02-28 13:03:35 +09:00
Rui Ueyama
8f21cc386e [ELF] Merge --shuffle-sections-seed into --shuffle-sections 2022-02-27 14:25:10 +09:00
Rui Ueyama
41986279de [ELF] Do not create multiple .init_array output sections
Usually, init_array is readable and writable, but it looks like rustc
with `-C lto=fat` create a read-only .init_array. mold used to create
two output .init_array sections for read/write and read-only
init_array input sections. That caused one of them are not executed
on startup.

Now, mold create read/write .init_array output section even if an
input .init_array section is read-only.

Fixes https://github.com/rui314/mold/issues/363
2022-02-25 22:54:55 +09:00
Rui Ueyama
31a5cdf86f [ELF] Verify a relocation after TLSGD or TLSLD 2022-02-24 12:32:02 +09:00
Rui Ueyama
a44de89991 Fix CI
`-m32` doesn't work on CI for some reason, so I'll remove the test
file for now.
2022-02-24 11:32:00 +09:00
Rui Ueyama
30683645c2 [ELF] Do not corrupt compressed debug sections on i386
Previously, if an input debug section is compressed, mold produced
corrupted output debug section on i386.

Fixes https://github.com/rui314/mold/issues/361
2022-02-24 11:05:54 +09:00
Rui Ueyama
4aa4bfa4a6 [ELF] Fix TLSGD/TLSLD relaxation
When we relax R_X86_64_TLSGD or R_X86_64_TLSGD, we rewrite two
instructions (the one referred by TLSGD/TLSLD and the following one
referred by the following relocation). The second instruction is
usually 5 bytes, but if it can be longer than that, and if that's the
case, we need to emit a nop to fill the gap at the end of the longer
instruction.

We didn't do that. As a result, the remaining garbage of the second
instruction is executed and caused an unpredictable result (illegal
instruction or segv).

This patch fixes the issue.

Fixes https://github.com/rui314/mold/issues/360
2022-02-23 16:16:53 +09:00
Rui Ueyama
4667be6134 [ELF] Change the size of ibtplt from 24 bytes to 32 bytes 2022-02-22 16:14:27 +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
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
Rui Ueyama
a9cca00933 [ELF] Fix symbol visibility
This commit fixes a regression introduced in
ce5749ce2f.
2022-01-13 22:59:04 +09:00
Rui Ueyama
011a7d35d6 [ELF] Add a test
https://github.com/rui314/mold/issues/244
2022-01-13 13:06:37 +09:00
Rui Ueyama
9d27ee0839 [ELF] Skip incompatible files specified by linker script GROUP command
Fixes https://github.com/rui314/mold/issues/260
2022-01-13 10:42:13 +09:00
Rui Ueyama
74e0e4f6d0 [ELF] Resurrect a file that was deleted by accident 2022-01-12 22:40:04 +09:00
Rui Ueyama
60c134893c [ELF] Create DT_DEBUG dynamic entry only for executables
gdb uses this field only if it is an executable. Having an extra
dynamic entry is usually harmless, but unfortunately, RPM's `rpmdeps`
tool uses the presence of this entry to distinguish executables from
shared object files. So, it is better not to create a DT_DEBUG entry
for shared object files.

Fixes https://github.com/rui314/mold/issues/254
2022-01-12 22:13:13 +09:00
Rui Ueyama
d2c57e99ca Refactor 2022-01-12 20:35:41 +09:00
Rui Ueyama
bb18b29e13 [ELF] Fix --trace-symbol for DSO
Previously, --trace-symbol didn't work for shared object files.
2022-01-12 17:30:51 +09:00
Rui Ueyama
cea6a569af [ELF] Remove "GNU gold" from the --version string
That substring was introduced in de7ba9014f
to appease Gentoo's dev-libs/jansson-2.13.1-r1 package. But I think I
shouldn't done that from the beginning to avoid the sitaution of the
"User-Agent" string of the web browser, which everybody claims they
are Mozilla, AppleWebKit, Chrome and Safari simultaneously.
2022-01-12 16:38:04 +09:00
Rui Ueyama
9ca6a9dc5e [ELF] Add -z ibt
Fixes https://github.com/rui314/mold/issues/229
2022-01-09 12:38:38 +09:00
Rui Ueyama
31a43a7ba6 [ELF] Add -z cet-report
Fixes https://github.com/rui314/mold/issues/229
2022-01-09 12:34:32 +09:00
Rui Ueyama
e29bd8f42b [ELF] Add -z shstk
Fixes https://github.com/rui314/mold/issues/229
2022-01-09 12:34:24 +09:00
Rui Ueyama
013e6ea06e [ELF] Fix .got.plt for IBTPLT
Previously, the `bnd jmp` instruction in a IBTPLT entry jumps to the
middle of an instruction due to an incorrect GOTPLT value. That happened
to work because a value that was there after the jump can be interpreted
as an `add` instruction and the control falled through the next `pushq`.
2022-01-09 10:14:35 +09:00
Rui Ueyama
27d836180f [ELF] Make an undefined symbol to be resolved to a common symbol in an archive
Previously, mold ignored common symbols in an archive when resolving
symbols. As a result, even if an undefined symbol could be resolved
using a common symbol in an archive, mold would end up with an undefined
symbol error.

This commit changes the behavior. Now, an undefined symbol is resolved
to a common symbol in an archive if exists.

Fixes https://github.com/rui314/mold/issues/256
2022-01-08 20:44:22 +09:00
Rui Ueyama
0370e7f1b3 [ELF] Emit a compact PLT section if -z now is passed
Each entry in the regular PLT section takes 16 bytes.
This new PLT section reduces it to 8 bytes.
2022-01-08 14:57:50 +09:00
Rui Ueyama
fbfa01dcd1 [ELF] Implement -z ibtplt
https://github.com/rui314/mold/issues/229
2022-01-08 14:09:12 +09:00
Rui Ueyama
badc733488 [ELF] Report an error if an incompatible object file is given
Previously, mold would crash if an incompatible object file was given.
https://github.com/rui314/mold/issues/260
2022-01-07 23:04:08 +09:00
Rui Ueyama
3dd76cd953 Attempt to fix buildbots 2022-01-07 18:18:00 +09:00
Rui Ueyama
d86a774ebf Fix Mach-O tests 2022-01-07 18:12:40 +09:00
Rui Ueyama
e7decd2c77 Use relative paths in tests 2022-01-07 18:08:06 +09:00
Rui Ueyama
4324cb2998 Use cc and c++ instead of clang, gcc, clang++ or g++ in tests 2022-01-07 18:02:35 +09:00
Rui Ueyama
4348417dd8 [ELF] Do not convert .ctors/.dtors to .init_array/.fini_array
The .ctors section contains a list of pointers that have to be run
before `main`. It usually contains pointers to global constructors.
The .dtors are for global destructors.

The .init_array and .fini_array serve the same purpose as .ctors and
.dtors. The former sections are newer than the latter sections, and
most programs have already migrated to .{init,fini}_array.

.ctors/.dtors are very similar to .{init,fini}_array, but the order
in which pointers are executed is different. .ctors/.dtors are executed
from the end to the beginning, while .{init,fini}_array are executed
from the beginning to the end.

Previously, we converted .ctors/.dtors sections into .{init,fini}_array
sections. In order to do that, we not only merge .ctors/.dtors to
.{init,fini}_array but also reverse their section contents, so that
the initialization order remains the same.

However, there was a bug in that logic. We forgot to reverse the
locations of dynamic relocations for the converted sections.
So, .ctors/.dtors sections converted to .{init,fini}_array were executed
in a wrong order.

We have two choices to "fix" the issue: (1) reverse not only section
contents but also dynamic relocations, or (2) stop converting .ctors/.dtors.
In this patch, I chose (2).

Since LLVM lld doesn't convert .ctors/.dtors, it shouldn't cause an
issue for most programs.
2022-01-07 17:47:14 +09:00
Rui Ueyama
0e17dbeda8 [ELF] Make --defsym'ed symbols absolute
If a symbol is defined in the form of --defsym=foo=0x<hexvalue>,
it should be defined as an absolute symbol with the given value.
2022-01-07 16:28:14 +09:00
Rui Ueyama
3b33420fe5 [ELF] Support R_X86_64_PLTOFF64 2022-01-07 14:44:17 +09:00
Rui Ueyama
e075e622ba Fix buildbots 2022-01-07 13:24:42 +09:00
Rui Ueyama
092286fc1d [ELF] Fix tests so that they work if CC is clang 2022-01-07 12:45:20 +09:00
Rui Ueyama
2f86fef3a0 [ELF] Fix assertion failure
This patch fixes a regression introduced in
43fa021d48.

Fixes https://github.com/rui314/mold/issues/259
2022-01-06 22:01:56 +09:00
Rui Ueyama
daa88f2f06 [ELF] Handle '[]' in glob patterns
Previously, mold crashes due to an invalid regex pattern exception
when `[...]` is given as a version script pattern.

Fixes https://github.com/rui314/mold/issues/258
2022-01-06 20:45:46 +09:00
Rui Ueyama
404fa1288b [ELF] Add a test
https://github.com/rui314/mold/issues/258
2022-01-06 18:27:31 +09:00
Rui Ueyama
43fa021d48 [ELF] Do not place non-exported symbols into .gnu.hash
Previously, mold put all global symbols into .gnu.hash. Although I
believe it was not an error, it bloated the size of .gnu.hash because
.gnu.hash needs only exported symbols.

https://github.com/rui314/mold/issues/255
2022-01-06 17:47:19 +09:00
Christoph Erhardt
df865725ed Fix test failures on aarch64
Signed-off-by: Christoph Erhardt <github@sicherha.de>
2022-01-05 12:26:56 +01:00
Rui Ueyama
be49d673a5 [ELF] Deduce emulation from input files if -m is not given 2022-01-05 19:55:32 +09:00
Rui Ueyama
285c9e2c32 [ELF] Attempt to fix buildbots 2022-01-04 20:59:33 +09:00
Rui Ueyama
a5029d19a8 [ELF] Automatically fall back to ld.bfd or ld.lld if LTO is in use
This is very hacky but highly practical, so I couldn't resist to not
implement this. We should support LTO natively in the future. In the
meantime, this feature should work as a poor-man's replacement.

Fixes https://github.com/rui314/mold/issues/242
2022-01-04 20:50:16 +09:00
Rui Ueyama
9894b3173b [ELF] Add --default-symver
Fixes https://github.com/rui314/mold/issues/228
2022-01-03 20:29:33 +09:00
Rui Ueyama
4b2f10d009 [ELF] Improve test 2022-01-03 20:15:04 +09:00
Rui Ueyama
5618038463 [ELF] Show a better error message for the empty input file
Fixes https://github.com/rui314/mold/issues/227
2022-01-02 19:33:07 +09:00
Rui Ueyama
0a8231b59d [ELF] Support R_X86_64_GOTOFF64
Fixes https://github.com/rui314/mold/issues/224
2022-01-02 11:14:33 +09:00
Rui Ueyama
1f7727975c [ELF] Improve test 2022-01-02 11:13:12 +09:00
Rui Ueyama
b5172caeb5 Improve test
I think `-m32` means ARM32 if this test runs on ARM64.
2021-12-31 00:13:07 +09:00
Tobias Klauser
66c9659f5a
Fix skip check in i386-mergable-strings.sh
Check for the ability to build 32-bit binaries like in the other i386
test cases.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
2021-12-30 15:58:11 +01:00
Rui Ueyama
d49c50ddaf [ELF] Add --defsym
Fixes https://github.com/rui314/mold/issues/208
2021-12-30 14:48:39 +09:00
Rui Ueyama
44ae92c4ed Remove redundant "../../" from test script paths
So that it's easier to see the output of the test script running
with `bash -x` . This change was made using sed.
2021-12-30 11:11:27 +09:00
Christoph Erhardt
0dcc910223 Add lots of quotes to shell scripts
This makes it possible to build and test mold in a path that contains
whitespace characters - with the notable exception of the tests where
`LD_PRELOAD` is used. That's because `LD_PRELOAD` unconditionally treats
any whitespace as separator, regardless of quoting.

The following ShellCheck warnings are eliminated by this commit:
* SC2046: Quote this to prevent word splitting.
* SC2086: Double quote to prevent globbing and word splitting.

Signed-off-by: Christoph Erhardt <github@sicherha.de>
2021-12-29 22:18:19 +01:00
Rui Ueyama
f3766cda81 [ELF] Add -z {max,common}-page-size
Fixes https://github.com/rui314/mold/issues/203
2021-12-29 17:14:08 +09:00
Rui Ueyama
b151de6684 [ELF] Fix .tbss layout
Fixes https://github.com/rui314/mold/issues/126
2021-12-29 15:50:56 +09:00
Rui Ueyama
cfed33ee42 Revert "[ELF] Fix a layout issue for thread-local bss sections"
This reverts commit 5c35d2a41f
because it causes a regression https://github.com/rui314/mold/issues/201.

I'll fix https://github.com/rui314/mold/issues/126 in another patch.
2021-12-28 18:36:31 +09:00
Rui Ueyama
1228bf3865
Merge pull request #200 from jmglogow/fix_version_globbing
[ELF] Fix '?' for globbing + version scripts
2021-12-26 23:29:02 +09:00
Jan-Marek Glogowski
31b0248b05 [ELF] Fix '?' for globbing + version scripts
For globbing, '?' matches a single character, so must be replaced
with '.'. As a result, don't skip the later regex matching for
version scripts, if the pattern contains either '*' or '?'.

Signed-off-by: Jan-Marek Glogowski <glogow@fbihome.de>
2021-12-26 15:02:43 +01:00
Rui Ueyama
d1161137d9 [ELF] Fix Initial-Exec TLS variables
Previously, GOTTPOFF relocations against Initial-Exec TLS variables
got wrong value.

Fixes https://github.com/rui314/mold/issues/197
2021-12-26 20:51:37 +09:00
Rui Ueyama
5c35d2a41f [ELF] Fix a layout issue for thread-local bss sections
Previously, if there are more than one TLS BSS section (e.g. .tbss and
.tcommon), mold layout them in such a way that they overlap in memory.
That caused a mysterious program crash to the programs compiled with DMD,
a D compiler.

Fixes https://github.com/rui314/mold/issues/126
2021-12-25 20:32:33 +09:00
Rui Ueyama
6e290aab3e [ELF] Implement --color-diagnostics 2021-12-25 16:55:51 +09:00
Rui Ueyama
7aa5c393e4 [ELF] Support extern "C++" in dynamic lists
Fixes https://github.com/rui314/mold/issues/149
2021-12-25 15:10:24 +09:00
Rui Ueyama
7b3c640472 Fix --dynamic-list
Previously, symbols were not exported from an executable even if
they are specified so by a dynamic list.
2021-12-25 13:38:24 +09:00
Rui Ueyama
04ccd4dbdd [ELF] Export undef symbols if both -z defs and -warn-undefined-symbols are given
When we are creating a DSO, unresolved undefined symbols are promoted to
dynamic symbols by default so that they will get another chance to be
resolved at runtime.

You can suppress this behavior by passing `-z defs`. If that option is
given, remaining undefs are reported as errors instead of being silently
promoted.

It looks like `-warn-undefined-symbols` negates the effect of `-z defs`.
So, if both options are given, remaining undefs are promoted to dynamic
symbols. mold previously silently make such symbols absolute symbols with
value 0.

Fixed https://github.com/rui314/mold/issues/152
2021-12-24 21:13:15 +09:00
Rui Ueyama
5601cf4236 [ELF] Add -z separate-code, -z noseparate-code and -z separate-lodable-segments
Fixes https://github.com/rui314/mold/issues/172
2021-12-24 20:28:45 +09:00
Rui Ueyama
088912e2fc [ELF] Create less number of PT_NOTE segments 2021-12-24 20:15:56 +09:00
Rui Ueyama
29d956a7b0 [ELF] Fix handling of DSO symbols with default version
Fixes https://github.com/rui314/mold/issues/150
2021-12-24 16:10:01 +09:00
Rui Ueyama
1ba6cc9188 [ELF] Handle relocation referring 0th symbol correctly 2021-12-24 16:09:57 +09:00
Rui Ueyama
135f17c5aa [ELF] Handle --sysroot
Attempt to fix https://github.com/rui314/mold/issues/150
2021-12-23 21:23:00 +09:00
Rui Ueyama
8c86c28496 Add -z nodefaultlib
Fixes https://github.com/rui314/mold/issues/184
2021-12-23 15:01:57 +09:00
Rui Ueyama
76407a6646 Do not create a PT_NOTE segment for non-alloc .note sections
Fixes https://github.com/rui314/mold/issues/185
2021-12-23 14:04:08 +09:00
Rui Ueyama
4d5e4730a2 Fix typo 2021-12-22 20:44:29 +09:00
Rui Ueyama
24bf92c27c [ELF] Fix a crash bug 2021-12-22 20:41:11 +09:00
Rui Ueyama
8bc57363c8 [ELF] Issue a warning instead of error for an unknown -z option
Fixes https://github.com/rui314/mold/issues/180
2021-12-22 20:08:14 +09:00
Rui Ueyama
74fdf90d70 Fix a test for clang and clang-as
clang-as does not support --keep-locals, so use its synonym -L
which is supported both by GNU as and clang-as.

Fixes https://github.com/rui314/mold/issues/173
2021-12-22 11:42:29 +09:00
Rui Ueyama
f9ff04866e [ELF] Add --threads=N option
Fixes https://github.com/rui314/mold/issues/168
2021-12-20 16:29:22 +09:00
Rui Ueyama
1d6d0e2fe3
Merge pull request #158 from sylvestre/second-fix
test/pie.sh: extend the check to other output
2021-12-17 20:16:07 +09:00
Rui Ueyama
4f37c3d024 Fix tests in non-English environment
These test files are not only invoked by make by also directly by hand.
Unsetting `LANG` on each file so that they don't fail in a non-English
environment.

This change is mechanically created by the following command:

  sed -i '1 a export LANG=' test/*/*.sh
2021-12-17 20:06:50 +09:00
Sylvestre Ledru
fbee0ec588 test/pie.sh: extend the check to other output
On debian unstable, the output doesn't contain 'Shared object file'
Parsing a different output

Signed-off-by: Sylvestre Ledru <sylvestre@debian.org>
2021-12-17 10:02:21 +01:00
Rui Ueyama
36633895e5 [ELF] Fix undefined symbol versions
Previously, if the following conditions are met:

 - we are creating a shared object file,
 - there's an undefined symbol in an input object file that are
   to be promoted to a dynamic symbol, and
 - a version script defining a default version is given,

then the symbol gets the default version. But that's a wrong behavior
because version scripts should not affect symbols that are not defined
in an output file.

This change fixes the issue by not setting version to such symbol.

Fixes https://github.com/rui314/mold/issues/151
2021-12-17 17:07:36 +09:00
Rui Ueyama
27f72ac320 Fix tests 2021-12-15 20:42:27 +09:00
Rui Ueyama
c9a2fd1d7d Fix a test 2021-12-15 10:36:51 +09:00
Rui Ueyama
10a318d5ba [ELF] Fix -exclude_libs option 2021-12-10 20:45:57 +09:00
Rui Ueyama
22116629f1 [ELF] Add --start-lib and --end-lib
Fixes https://github.com/rui314/mold/issues/133
2021-12-06 20:09:46 +09:00
Rui Ueyama
0328f37b2a [Mach-O] Add -bundle 2021-12-05 16:52:35 +09:00
Rui Ueyama
c5f5e6153f [Mach-O] Add -headerpad_max_install_names 2021-12-05 15:53:41 +09:00
Rui Ueyama
6fae1d8555 [Mach-O] Report an error on -search_dylibs_first 2021-12-05 15:40:36 +09:00
Rui Ueyama
a3e0e9f197 wip 2021-12-04 16:41:51 +09:00
Rui Ueyama
6a3ddf1ab4 wip 2021-12-04 16:31:15 +09:00
Rui Ueyama
a415accbc3 wip 2021-12-04 12:55:21 +09:00
Rui Ueyama
d6a9005b14 wip 2021-12-04 12:10:18 +09:00
Rui Ueyama
bf7366438b wip 2021-12-03 23:06:07 +09:00
Rui Ueyama
4bd92c0d46 [Mach-O] Do not emit PAGEZERO segment for dylibs 2021-12-01 15:14:38 +09:00
Rui Ueyama
8ceaf53728 [Mach-O] Add -needed_framework 2021-12-01 13:45:25 +09:00
Rui Ueyama
ee05f3a65f [Mach-O] Add -dead_strip_dylibs 2021-11-30 19:20:29 +09:00
Rui Ueyama
7156dd265f [Mach-O] Add -needed-l 2021-11-30 18:05:28 +09:00
Rui Ueyama
9e7e41cfac [Mach-O] Fix -L 2021-11-30 15:23:00 +09:00
Rui Ueyama
4998a130e5 [Mach-O] Support -dylib 2021-11-29 18:12:48 +09:00
Rui Ueyama
e99727f87f [Mach-O] Add -Z 2021-11-28 10:05:23 +09:00
Rui Ueyama
1219d3250c [ELF] Fix a test for AArch64 2021-11-27 21:03:47 +09:00
Rui Ueyama
8ae43210e7 [Mach-O] Add -framework and -F 2021-11-27 20:03:12 +09:00
Rui Ueyama
72cea9a0bf [Mach-O] Improve -filelist 2021-11-21 22:18:55 +09:00
Rui Ueyama
5804d6b4b2 [Mach-O] Add -ObjC 2021-11-21 20:26:41 +09:00
Rui Ueyama
efb6e6729a [Mach-O] Handle response files correctly 2021-11-21 14:53:50 +09:00
Rui Ueyama
6087782660 [Mach-O] Add -filepath 2021-11-20 19:26:29 +09:00
Rui Ueyama
cee6412e3d [Mach-O] Add -rpath 2021-11-20 19:01:18 +09:00
Rui Ueyama
39676ef7a9 Define BINDIR, LIBDIR and MANDIR
So that it is easy to specify alternative install target paths.
2021-11-19 17:18:15 +09:00
Rui Ueyama
8b41bb923a [ELF] Fix canonical PLT entry
Previously, an executable linked by mold could fall into an infinite
loop between .plt.got and .got entries. This commit fixes the issue
by creating .plt entries instead of .plt.got.

Fixes https://github.com/rui314/mold/issues/129
2021-11-18 20:53:15 +09:00
Rui Ueyama
18cfe1f4ca [Mach-O] Add -pagezero_size option 2021-11-14 14:23:44 +09:00
Rui Ueyama
06bc828a9b [Mach-O] wip 2021-11-14 13:30:55 +09:00
Rui Ueyama
433666a470 [Mach-O] Implement -dead_strip 2021-11-13 15:21:52 +09:00
Rui Ueyama
fcd592e53d [Mach-O] Add -e option 2021-11-12 19:10:00 +09:00
Rui Ueyama
4a1ec53c24 [Mach-O] wip 2021-11-12 19:10:00 +09:00
Rui Ueyama
87c4c1df15 [Mach-O] Relax GOT/TLV load instructions 2021-11-09 19:09:07 +09:00
Rui Ueyama
92f56aa73f [Mach-O] Support thread-local variables 2021-11-09 16:23:43 +09:00
Rui Ueyama
3a24532d2a [Mach-O] wip 2021-11-08 20:36:27 +09:00
Rui Ueyama
72587e643f [Mach-O] wip 2021-11-07 23:57:43 +09:00
Rui Ueyama
ea6d310576 [Mach-O] wip 2021-11-07 20:01:36 +09:00
Rui Ueyama
6d709de396 [Mach-O] wip 2021-11-06 16:22:55 +09:00
Rui Ueyama
f232e857f0 [Mach-O] wip 2021-11-06 10:53:44 +09:00
Rui Ueyama
cbc990d459 [Mach-O] wip 2021-11-05 18:40:59 +09:00
Rui Ueyama
247655179f [Mach-O] Add -map option 2021-11-05 14:23:28 +09:00
Rui Ueyama
bfa50ac015 [Mach-O] Handle Universal binaries 2021-11-03 16:38:29 +09:00
Rui Ueyama
00e45a4ba1 [Mach-O] wip 2021-11-02 15:08:02 +09:00
Rui Ueyama
4de3bb9a78 [Mach-O] wip 2021-11-02 10:07:49 +09:00
Rui Ueyama
bfec2435da [Mach-O] wip 2021-11-01 21:06:33 +09:00
Rui Ueyama
3c297c2254 [Mach-O] wip 2021-11-01 15:13:28 +09:00
Rui Ueyama
0a6d3986cd [Mach-O] Align common symbols properly 2021-10-29 21:34:27 +09:00
Rui Ueyama
4afc68d74d [Mach-O] Handle common symbols 2021-10-29 16:19:05 +09:00
Rui Ueyama
c227286dd5 [Mach-O] Handle .bss 2021-10-29 13:28:52 +09:00
Rui Ueyama
e1ab83155a [Mach-O] wip 2021-10-28 18:51:54 +09:00
Rui Ueyama
58f6502a1c [Mach-O] Add static archive file support 2021-10-28 14:37:18 +09:00
Rui Ueyama
7236d14650 [Mach-O] Report errors for remaining undefined symbols 2021-10-27 21:46:57 +09:00
Rui Ueyama
6cdfd45134 [Mach-O] Create GOT entries for internal references 2021-10-27 15:57:31 +09:00
Rui Ueyama
5549a005f1 [Mach-O] Create GOT entries for external references 2021-10-26 17:44:57 +09:00
Rui Ueyama
5837fe75eb [Mach-O] wip 2021-10-26 16:38:44 +09:00
Rui Ueyama
d7fb95fa63 [Mach-O] wip 2021-10-26 15:26:08 +09:00
Rui Ueyama
8471f47abb [Mach-O] wip 2021-10-26 15:24:23 +09:00
Rui Ueyama
48e7616263 [Mach-O] Add the -platform_version option 2021-10-26 15:14:14 +09:00
Rui Ueyama
d6657a3cb0 [Mach-O] Add the -headerpad option 2021-10-25 20:14:52 +09:00
Rui Ueyama
5817265e91 [Mach-O] Fix ASAN issue 2021-10-25 20:14:32 +09:00
Rui Ueyama
a86ad2771a [Mach-O] Use clang as a linker frontend
Now that we can link a "hello world" program on macOS, we can
use clang as a linker frontend.
2021-10-25 19:09:18 +09:00
Rui Ueyama
4b6407dfc0 [Mach-O] wip 2021-10-23 17:45:45 +09:00
Rui Ueyama
e6c614d69c [ELF] Remove extra path separators 2021-10-12 18:32:20 +09:00
Rui Ueyama
ffb8e03c97 [ELF] Make .rela.dyn and .rela.plt contiguous in an output file
glibc 2.22 or prior have a bug that the dynamic linker (ld-linux.so.2)
crashes on `dlopen` if .rela.dyn and .rela.plt are not contiguous in
the `dlopen`-ed file and RTLD_NOW is specified. That bug was fixed in
fa19d5c48a.

glibc 2.23 which includes the fix for the bug was released on 2016-02-18.
So, unless you are using glibc that is more than 5 years old, mold-
generated shared object files work fine with `dlopen`. But it is easy
to make the sections contiguous in file, so I'll commit this change as a
workaround for the bug.

Fixes https://github.com/rui314/mold/issues/120
2021-10-12 17:10:32 +09:00
Rui Ueyama
a8b2f521f7 [ELF] Create .rodata.cst and .rodata.str sections
Previously, mold created two or more ".rodata" sections if input files
contain regular .rodata sections as well as .rodata.str* or .rodata.cst*
sections. This is because .rodata.str* and .rodata.cst* contains mergeable
section contents (i.e. string constants and constant pools, respectively),
and they are handled in a special way.

In ELF, section names are not significant, so nothing is wrong with
creating multiple .rodata. However, valgrind unfortunately doesn't like
such executable.

With this change, mold keeps ".rodata.str" and ".rodata.cst" prefixes
to avoid creating multiple ".rodata" sections.

Fixes https://github.com/rui314/mold/issues/118
2021-10-07 13:55:46 +09:00
Rui Ueyama
301d45b2fa Fix a test 2021-10-06 22:04:25 +09:00
Rui Ueyama
3c513a31cc [Mach-O] Apply relocations 2021-10-06 14:33:01 +09:00
Rui Ueyama
4932daadba [Mach-O] wip 2021-10-05 11:40:32 +09:00
Rui Ueyama
8965de697b [Mach-O] wip 2021-10-05 11:40:32 +09:00