Compare commits

...
88 Commits
Author SHA1 Message Date
Rui Ueyama 3d9b4d5c05 Bump mold version to 2.4.0 2023-11-29 17:58:00 +09:00
Rui Ueyama d06bfe0ac3 Update GitHub sponsor list 2023-11-29 17:48:11 +09:00
Rui Ueyama 6540294a81 Fix CI 2023-11-29 17:15:39 +09:00
Rui Ueyama 02a7640047 Fix build artifact pathnames 2023-11-29 16:59:23 +09:00
Rui Ueyama f4c5a8a42e Allow multiple .eh_frame sections in a single object file
A `.eh_frame` section contains data for exception handling. Usually,
an object file contains only one `.eh_frame` section, which explains
how to handle exceptions for all text sections in the same object file.

However, it appears that, in rare cases, we need to handle object
files containing multiple `.eh_frame` sections. An example of this is
the `/usr/lib/clang/17/lib/x86_64-redhat-linux-gnu/clang_rt.crtbegin.o`
file, which is provided by the `compiler-rt` package of Fedora 39.
Specifically, I'm using the `quay.io/fedora/fedora:39` Docker image.
The file contains two `.eh_frame` sections.

One `.eh_frame` in the file is of type `STT_X86_64_UNWIND` and the
other is of `STT_PROGBITS`. It's possible that the file was created
with `ld -r`, and the linker failed to merge the two incoming
`.eh_frame` sections into one output section due to the difference in
section types.

We did not expect such inputs and consequently produced corrupted
output files.

This commit improves our linker so that mold can handle multiple
`.eh_frame` sections in a single object file.

Fixes https://github.com/rui314/mold/issues/1157
2023-11-29 16:10:36 +09:00
Rui Ueyama 0fdbace9b2 Improve compatibility of precedences in version script patterns
Previously, symbol names or wildcard patterns in version script files
were processed strictly from top to bottom. When a single symbol
matches two or more patterns, the first one was given precedence.

However, GNU ld appears to treat exact matches differently, assigning
them higher precedence over wildcard patterns. This discrepancy led to
programs linked with Qt 6.6.1 using mold failing to launch.

Fixes https://github.com/rui314/mold/issues/1158
2023-11-28 15:43:48 +09:00
Rui Ueyama cab6014154 Merge pull request #1156 from ywgrit/main
Remove assertions to make errors exposed in a customized way
2023-11-26 18:55:40 +09:00
Rui Ueyama 000ce0ee52 Handle LLVM-generated TLSDESC code sequence
It had been assumed that the TLSDESC code seqeunce looks like this:

  lea    0(%rip), %rax
      R_X86_64_GOTPC32_TLSDESC    foo
  call   *(%rax)
      R_X86_64_TLSDESC_CALL       foo

However, LLVM seems to also emit something like this:

  lea    0(%rip), %reg
      R_X86_64_GOTPC32_TLSDESC    foo
  ...
  mov    %reg, %rax
  ...
  call   *(%rax)
      R_X86_64_TLSDESC_CALL       foo

That means when we rewrite the LEA instruction to relax the code
sequence, we need to handle destination registers other than %rax.

The wrong assumption caused a program crash as reported as
https://bugs.gentoo.org/914849.
2023-11-26 18:49:20 +09:00
ywgrit d720a2011c Remove assertions to make errors exposed in a customized way 2023-11-26 17:04:28 +08:00
Rui Ueyama c91055271b Simplify 2023-11-24 16:15:37 +09:00
Rui Ueyama 1478acd551 Do not save strings if not necessary 2023-11-24 11:48:19 +09:00
Rui Ueyama b3404c07d7 Add a timer prober 2023-11-24 10:44:18 +09:00
Rui Ueyama 94767dc0e7 Fix a crash bug 2023-11-24 10:14:00 +09:00
Rui Ueyama f957acfe11 Add an entry for Amazon Linux 2023-11-24 09:30:56 +09:00
Rui Ueyama 7fc43ccc3a Add comments 2023-11-24 08:40:57 +09:00
Rui Ueyama dc5b9508d9 Simplify 2023-11-24 06:39:27 +09:00
Rui Ueyama ef056d8ed1 Refactor 2023-11-24 06:38:29 +09:00
Rui Ueyama 6a63671ef5 Simplify 2023-11-23 12:22:14 +09:00
Rui Ueyama d3867766af Remove redundant check
`is_ld("ld")` returns true
2023-11-23 11:47:33 +09:00
Rui Ueyama 1ae4b9571b Merge pull request #1154 from andreas-schwab/main
mold-wrapper: also wrap posix_spawnp
2023-11-23 11:39:07 +09:00
Andreas Schwab 3fd1cec20e mold-wrapper: also wrap posix_spawnp
Fixes issue 1153.

Signed-off-by: Andreas Schwab <[email protected]>
2023-11-22 14:59:11 +01:00
Rui Ueyama 28b16eea19 Rename classes 2023-11-22 20:56:21 +09:00
Rui Ueyama 37f32a9008 Simplify 2023-11-22 14:12:37 +09:00
Rui Ueyama 8415252322 Do not use std::aligned_alloc
Fixes https://github.com/rui314/mold/issues/1152
2023-11-22 11:52:33 +09:00
Rui Ueyama b3984c9580 Simplify 2023-11-21 21:23:47 +09:00
Rui Ueyama 6616995804 Refactor 2023-11-21 12:23:49 +09:00
Rui Ueyama de7d37e1fc Do not count .tbss into account when creating PT_GNU_RELRO
Fixes https://github.com/rui314/mold/issues/1151
2023-11-21 11:44:52 +09:00
Rui Ueyama 3cb8a528a5 Make -z rewrite-endbr to work without -ffunction-sections
Now we can rewrite an endbr64 instruction even if it is not at the
beginning of a section.
2023-11-20 17:36:23 +09:00
Rui Ueyama 8bb01e1a0e Fix CI 2023-11-20 14:25:03 +09:00
Rui Ueyama 0416d7ed80 Add --nmagic
Fixes https://github.com/rui314/mold/issues/1144
2023-11-20 13:35:04 +09:00
Rui Ueyama c02c04f08a Improve dist.sh 2023-11-20 11:55:14 +09:00
Rui Ueyama efb952ef12 Use the release version of setup-mold GitHub action 2023-11-19 10:32:51 +09:00
Rui Ueyama 04b145d795 Add comments and fix SHA256 digest 2023-11-19 09:36:04 +09:00
Rui Ueyama 1830fa1d84 Pin docker images with sha256 digests for additional security 2023-11-18 12:25:37 +09:00
Rui Ueyama a2b300b863 Fix CI 2023-11-17 19:12:19 +09:00
Rui Ueyama 2d8297a562 Fix CI 2023-11-17 18:22:16 +09:00
Rui Ueyama deb7728b46 Fix CI 2023-11-17 18:08:13 +09:00
Rui Ueyama bbd6d86af4 Fix CI 2023-11-17 17:57:59 +09:00
Rui Ueyama b6361b8cad Refactor 2023-11-17 17:51:25 +09:00
Rui Ueyama 04fdae124f Refactor 2023-11-17 17:41:24 +09:00
Rui Ueyama 29df7093a8 Refactor 2023-11-17 17:18:53 +09:00
Rui Ueyama 18212a21ab Refactor 2023-11-17 17:04:43 +09:00
Rui Ueyama e13d28bfe6 Create non-x86 binaries on daily basis
Building non-native binaries using Qemu is extremely heavy.
It takes ~2 hours on a GitHub Actions runner. We don't want to
run it on each push.
2023-11-17 15:45:39 +09:00
Rui Ueyama 667204ac7c Make dist.sh to work on macOS 2023-11-17 15:09:31 +09:00
Rui Ueyama 76ea83e791 Create binary tarballs for non-x86-46 targets 2023-11-17 14:15:04 +09:00
Rui Ueyama 55daf7385d Cache Docker images for GitHub Actions 2023-11-17 13:42:54 +09:00
Rui Ueyama 0e66e4b7e9 Create distribution tarballs for each commit 2023-11-16 22:54:38 +09:00
Rui Ueyama fe485db9b4 Refactor 2023-11-16 18:30:31 +09:00
Rui Ueyama b3dc4879fd Remove a global variable 2023-11-16 18:00:41 +09:00
Rui Ueyama 25ce3738b9 Update workflow 2023-11-16 16:50:33 +09:00
Rui Ueyama 22b8ac8470 Update mold.1 2023-11-16 07:16:13 +00:00
Rui Ueyama 6a4e128ef8 Document --spare-program-headers 2023-11-16 16:15:28 +09:00
Rui Ueyama 66d68c8a98 Auto-generate docs/mold.1 2023-11-16 16:15:28 +09:00
Rui Ueyama 76867af090 Regenerate mold.1
This file is created by `ronn` command.
2023-11-16 12:54:49 +09:00
Rui Ueyama 5d02248a0b Sort entries and fix typos 2023-11-16 12:54:38 +09:00
Rui Ueyama eb6c213f2a Add --spare-program-headers
This is a new experimental flag to make room at the end of PHDR
so that post-processing tools can add more entries as needed.

Fixes https://github.com/rui314/mold/issues/1148
2023-11-16 12:07:13 +09:00
Rui Ueyama efdac9afdd Merge pull request #1147 from Saldivarcher/add-trace-symbol-help-msg 2023-11-16 05:45:29 +09:00
Miguel Saldivar c6bfff2a04 Add --trace-symbol to the help message 2023-11-14 14:24:41 -06:00
Rui Ueyama 49066ea329 Bump mold version to 2.3.3 2023-11-14 14:41:42 +09:00
Rui Ueyama 25ceab07f7 Build mold on Debian 8 (Jessie)
Debian 8 was released in April 2015, which is old enough for
binary distribution. Previously, we used to use Debian 10 (Bullseye),
which was released in July 2019.
2023-11-14 12:44:02 +09:00
Rui Ueyama 6e97393740 Fix tests 2023-11-14 12:44:02 +09:00
Rui Ueyama ff3d54d26f Fix a crash bug in --defsym
Fixes https://github.com/rui314/mold/issues/1108
2023-11-13 20:09:25 +09:00
Rui Ueyama 25c9cf3976 Refactor 2023-11-13 19:57:50 +09:00
Rui Ueyama 5f2fb08cfd Make dist.sh reproducible
dist.sh script should now produce bit-by-bit identical output files now.

Fixes https://github.com/rui314/mold/issues/1146
2023-11-13 17:14:37 +09:00
Rui Ueyama 60bc56848f Fix tests for Debian 9 2023-11-13 13:37:35 +09:00
Rui Ueyama 600ca381fd Reduce the size of Symbol by 2 bytes 2023-11-12 13:39:44 +09:00
Rui Ueyama da3f5dd4ec Fix --dynamic-list for DSOs
--dynamic-list, --export-dynamic-symbol and --export-dynamic-symbol-list
have different semantics for executables and DSOs. If the output is an
executable, they specify a list of symbols that are to be exported.
If the output is a shared object, they specify the list of symbols that
are to be interposable.

mold havne't implemented the latter semantics. This commit fixes that
issue.

Fixes https://github.com/rui314/mold/issues/1071
2023-11-12 13:00:24 +09:00
Rui Ueyama b59911b0c1 Refactor 2023-11-11 15:09:51 +09:00
Rui Ueyama 8131528305 Rename long .rodata section names 2023-11-10 15:07:19 +09:00
Rui Ueyama 6a71e7dd4b Refactor 2023-11-09 18:57:13 +09:00
Rui Ueyama a63b2f70b2 Fix CI 2023-11-09 15:47:25 +09:00
Rui Ueyama 3f88964527 Map .ctors/.dtors into .init_array/.fini_array
.init_array/.fini_array haven't completely replace .ctors/.dtors, so
we need to convert .ctors/.dtors to .init_array/.fini_array.

Fixes https://github.com/rui314/mold/issues/1113
2023-11-09 12:43:58 +09:00
Rui Ueyama e18dc70af0 Refactor 2023-11-08 22:04:14 +09:00
Rui Ueyama 0c6daad215 Add more tests 2023-11-07 15:20:31 +09:00
Rui Ueyama 8c35c80bb7 Enable qemu tests by default 2023-11-07 13:55:56 +09:00
Rui Ueyama d42ba3bae4 Fix CI 2023-11-07 13:48:48 +09:00
Rui Ueyama 8c337b25df Run power10 tests on qemu only if qemu and cross GCC suports power10 2023-11-07 12:04:22 +09:00
Rui Ueyama 8d1c08bd03 Simplify 2023-11-07 11:25:03 +09:00
Rui Ueyama 5599a6f24f Skip test only on qemu 2023-11-07 11:06:13 +09:00
Rui Ueyama ef9a610dd1 Run tests as long as qemu and cross compiler are available
So that we can catch bugs like e11d64b5c7
earlier.
2023-11-07 11:00:26 +09:00
Rui Ueyama 31c3b5397b [POWER10] Emit dynamic relocation for ifunc
Previously, we didn't emit a dynamic relocation for a statically-
initialized function pointer pointing to an ifunc on POWER10, e.g.

  int fn() __attribute__((ifunc("resolve_fn")));
  void *ptr = fn;

Fixes https://github.com/rui314/mold/issues/1142
2023-11-06 13:27:05 +09:00
Rui Ueyama e11d64b5c7 Do not run tests under qemu-i386 on i686 native host
Fixes https://github.com/rui314/mold/issues/1140
2023-11-06 12:44:28 +09:00
Rui Ueyama 9d91a2bc23 Revert "Do not crash if defsym'ed symbol didn't exist"
This reverts commit 5c7a75b3e4 and
8c41f21d3c because they broke CI.
2023-11-06 12:43:20 +09:00
Rui Ueyama 8c41f21d3c Fix CI 2023-11-05 22:30:43 +09:00
Rui Ueyama 5c7a75b3e4 Do not crash if defsym'ed symbol didn't exist
Fixes https://github.com/rui314/mold/issues/1108
2023-11-05 16:37:09 +09:00
Rui Ueyama 0804fbd095 Refactor 2023-11-05 16:11:27 +09:00
Rui Ueyama 0f78fa9c43 Attempt to fix tests on 32-bit x86
Fixes https://github.com/rui314/mold/issues/1140
2023-11-05 11:01:31 +09:00
Rui Ueyama d30e970eb8 Remove useless function call 2023-11-05 10:57:25 +09:00
76 changed files with 1728 additions and 694 deletions

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
name: Build all tarballs
on:
schedule:
- cron: '0 0 * * *'
release:
types: [ published ]
jobs:
build-tarballs:
runs-on: ubuntu-latest
strategy:
matrix:
target: [ x86_64, aarch64, arm, riscv64, ppc64le, s390x ]
permissions:
contents: read
packages: write
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build a tarball
run: ./dist.sh ${{ matrix.target }}
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: tarballs
path: mold-*.tar.gz
+36
View File
@@ -0,0 +1,36 @@
name: Build x86 tarball
on:
push:
branches: [ main ]
jobs:
build-tarball:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build a tarball
run: ./dist.sh
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: tarball
path: mold-*.tar.gz
+18 -21
View File
@@ -18,12 +18,12 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: rui314/setup-mold@staging
- uses: rui314/setup-mold@v1
- name: install-build-deps
run: sudo ./install-build-deps.sh update
run: sudo ./install-build-deps.sh
- name: ccache
uses: hendrikmuhs/ccache-action@v1
- name: build and test
- name: build
run: |
echo "/usr/lib/ccache:/usr/local/opt/ccache/libexec" >> $GITHUB_PATH
sudo apt-get install -y clang++-12
@@ -31,7 +31,10 @@ jobs:
cd build
cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++-12 ${{ matrix.target }} ..
cmake --build . -j$(nproc)
ctest . -j$(nproc)
- name: test
run: |
cd build
ctest -j$(nproc)
- name: archive test results
uses: actions/upload-artifact@v3
if: failure()
@@ -47,16 +50,11 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: install-build-deps
shell: bash
run: |
# Install cross toolchains
dpkg --add-architecture i386
./install-build-deps.sh update
apt-get install -y sudo qemu-user gdb zstd dwarfdump xz-utils {gcc,g++}-10-{i686,aarch64,riscv64,powerpc,powerpc64,powerpc64le,s390x,sparc64,m68k,sh4,alpha}-linux-gnu {gcc,g++}-10-arm-linux-gnueabihf
for i in {i686,aarch64,riscv64,powerpc,powerpc64,powerpc64le,s390x,sparc64,m68k,sh4,alpha}-linux-gnu arm-linux-gnueabihf; do
ln -sf /usr/bin/$i-gcc-10 /usr/bin/$i-gcc
ln -sf /usr/bin/$i-g++-10 /usr/bin/$i-g++
done
./install-build-deps.sh
./install-cross-tools.sh
# Install a RV32 toolchain from third party since it's not available
# as an Ubuntu package.
@@ -89,26 +87,25 @@ jobs:
wget -O/usr/local/bin/qemu-loongarch64 -q https://github.com/loongson/build-tools/releases/download/2023.08.08/qemu-loongarch64
chmod 755 /usr/local/bin/qemu-loongarch64
shell: bash
- name: ccache
uses: hendrikmuhs/ccache-action@v1
- name: build and test
- name: build
run: |
echo "/usr/lib/ccache:/usr/local/opt/ccache/libexec" >> $GITHUB_PATH
mkdir build
cd build
cmake -DMOLD_ENABLE_QEMU_TESTS=On -DMOLD_ENABLE_QEMU_TESTS_RV32=On -DMOLD_ENABLE_QEMU_TESTS_LOONGARCH=On ..
cmake ..
cmake --build . -j$(nproc)
ctest . -j$(nproc)
- name: test
run: |
cd build
ctest -j$(nproc)
- name: archive test results
uses: actions/upload-artifact@v3
if: failure()
with:
name: test-results-gcc
path: |
build/out
build/Testing
path: b/out b/Testing
build-macos:
runs-on: macos-11
@@ -122,7 +119,7 @@ jobs:
- uses: actions/checkout@v3
- name: ccache
uses: hendrikmuhs/ccache-action@v1
- name: build and test
- name: build
run: |
echo "/usr/lib/ccache:/usr/local/opt/ccache/libexec" >> $GITHUB_PATH
mkdir build
-15
View File
@@ -1,15 +0,0 @@
name: linux-packages
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- name: build
run: ./dist.sh x86_64
- name: upload
uses: actions/upload-artifact@v3
with:
path: mold-*-linux.tar.gz
+32
View File
@@ -0,0 +1,32 @@
# This file is generated by ChatGPT
name: Update manpage
on:
push:
paths:
- 'docs/mold.md'
jobs:
update-manpage:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: Install ronn
run: sudo apt-get install -y ronn
- name: Generate mold.1 from mold.md
run: ronn --roff docs/mold.md
- name: Configure Git
run: |
git config --global user.name 'Rui Ueyama'
git config --global user.email '[email protected]'
- name: Commit and push if mold.1 is updated
run: |
git add docs/mold.1
git diff --staged --quiet || (git commit -m "Update mold.1 (automated commit)" && git push)
+1 -1
View File
@@ -44,7 +44,7 @@
# features and behave exactly the same.
cmake_minimum_required(VERSION 3.14)
project(mold VERSION 2.3.2)
project(mold VERSION 2.4.0)
include(CMakeDependentOption)
include(GNUInstallDirs)
+5 -4
View File
@@ -71,7 +71,7 @@ necessary packages. You may need to run it as root.
git clone https://github.com/rui314/mold.git
mkdir mold/build
cd mold/build
git checkout v2.3.2
git checkout v2.4.0
../install-build-deps.sh
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=c++ ..
cmake --build . -j $(nproc)
@@ -255,13 +255,14 @@ following people and organizations who have sponsored $128/month or more:
<a href="https://www.emergetools.com"><img src="docs/emerge-tools-logo.png" align=center height=120 width=240 alt="Emerge Tools"></a><br>
- [Uber](https://uber.com)
- [G-Research](https://www.gresearch.co.uk)
- [Signal Slot Inc.](https://github.com/signal-slot)
- [GlareDB](https://github.com/GlareDB)
### Individual sponsors
- [300baud](https://github.com/300baud)
- [Johan Andersson](https://github.com/repi)
- [Wei Wu](https://github.com/lazyparser)
- [kyle-elliott](https://github.com/kyle-elliott)
- [Bryant Biggs](https://github.com/bryantbiggs)
- [kraptor23](https://github.com/kraptor23)
- [Jinkyu Yi](https://github.com/jincreator)
+9 -12
View File
@@ -565,7 +565,7 @@ public:
}
~ConcurrentMap() {
free(entries);
free(entries_buf);
}
// In order to avoid unnecessary cache-line false sharing, we want
@@ -579,19 +579,15 @@ public:
void resize(i64 nbuckets) {
this->nbuckets = std::max<i64>(MIN_NBUCKETS, bit_ceil(nbuckets));
free(entries_buf);
i64 sz = sizeof(Entry) * this->nbuckets;
free(entries);
#if _WIN32
// Even though std::aligned_alloc is defined in C++17, MSVC doesn't
// seem to provide that function.
entries = (Entry *)_aligned_malloc(sz, alignof(Entry));
#else
entries = (Entry *)std::aligned_alloc(alignof(Entry), sz);
#endif
memset(entries, 0, sz);
// seem to provide that function. C11's aligned_alloc may not be always
// avialalbe. Therefore, we'll align the buffer ourselves.
i64 size = sizeof(Entry) * this->nbuckets;
entries_buf = malloc(size + alignof(Entry) - 1);
entries = (Entry *)align_to((u64)entries_buf, alignof(Entry));
memset(entries, 0, size);
}
std::pair<T *, bool> insert(std::string_view key, u64 hash, const T &val) {
@@ -699,6 +695,7 @@ public:
static constexpr i64 NUM_SHARDS = 16;
static constexpr i64 MAX_RETRY = 128;
void *entries_buf = nullptr;
Entry *entries = nullptr;
i64 nbuckets = 0;
+5 -6
View File
@@ -11,16 +11,15 @@
namespace mold {
std::string_view demangle(std::string_view name) {
static thread_local char *p;
if (p)
free(p);
static thread_local char *buf;
free(buf);
// Try to demangle as a Rust symbol. Since legacy-style Rust symbols
// are also valid as a C++ mangled name, we need to call this before
// cpp_demangle.
p = rust_demangle(std::string(name).c_str(), 0);
if (p)
return p;
buf = rust_demangle(std::string(name).c_str(), 0);
if (buf)
return buf;
// Try to demangle as a C++ symbol.
if (std::optional<std::string_view> s = cpp_demangle(name))
+147 -56
View File
@@ -1,78 +1,169 @@
#!/bin/bash
#
# This script creates a mold binary distribution. The output is
# written in this directory as `mold-$version-$arch-linux.tar.gz`
# (e.g. `mold-1.0.3-x86_64-linux.tar.gz`).
# This script creates a mold binary distribution. The output is written in
# this directory as `mold-$version-$arch-linux.tar.gz` (e.g.,
# `mold-1.0.3-x86_64-linux.tar.gz`).
#
# The mold executable created by this script is statically-linked to
# libstdc++ and libcrypto but dynamically-linked to libc, libm, libz
# and librt, as they almost always exist on any Linux systems.
# The mold executable created by this script is statically linked to
# libstdc++ but dynamically linked to libc, libm, libz, and librt, as
# these libraries almost always exist on any Linux system. We can't
# statically link libc because doing so would disable dlopen(), which is
# necessary to open the LTO linker plugin.
#
# This script aims to produce reproducible outputs. That means if you run
# the script twice on the same git commit, it should produce bit-by-bit
# identical binary files. This property is crucial as a countermeasure
# against supply chain attacks. With this, you can verify that the binary
# files distributed on the GitHub release pages are created from the
# commit with release tags by rebuilding the binaries yourself.
#
# Debian provides snapshot.debian.org to host all historical binary
# packages. We use it to construct Docker images pinned to a
# particular timestamp.
#
# We aim to use a reasonably old Debian version because we'll dynamically
# link glibc to mold, and a binary linked against a newer version of glibc
# won't work on a system with an older version of glibc.
#
# We need GCC 10 or newer to build mold. If GCC 10 is not available on an
# old Debian version, we'll build it ourselves.
#
# You may need to run the following command to use Docker with Qemu:
#
# $ docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
set -e -x
cd "$(dirname $0)"
usage() {
echo "Usage: $0 [ x86_64 | aarch64 | arm | riscv64 | ppc64le | s390x ]"
exit 1
}
case $# in
0)
arch=$(uname -m)
[[ $arch = arm* ]] && arch=arm
if [ $arch = arm64 ]; then
arch=aarch64
elif [[ $arch = arm* ]]; then
arch=arm
fi
;;
1)
arch="$1"
;;
*)
echo "Usage: $0 [ x86_64 | aarch64 | arm | riscv64 | ppc64le | s390x ]"
exit 1
usage
esac
echo "$arch" | grep -Eq '^(x86_64|aarch64|arm|riscv64|ppc64le|s390x)$' || \
{ echo "Error: no docker image for $arch"; exit 1; }
echo "$arch" | grep -Eq '^(x86_64|aarch64|arm|riscv64|ppc64le|s390x)$' || usage
image=mold-builder-$arch:1
version=$(sed -n 's/^project(mold VERSION \(.*\))/\1/p' $(dirname $0)/CMakeLists.txt)
version=$(sed -n 's/^project(mold VERSION \(.*\))/\1/p' CMakeLists.txt)
dest=mold-$version-$arch-linux
# Create a Docker image if not exists.
#
# We want to use a reasonably old Linux distro because we'll dynamically
# link glibc to mold, and a binary linked against a newer version of
# glibc won't work on a system with a older version of glibc.
#
# We might want to instead statically-link everything, but that'll
# disable dlopen(), which means we can't use the linker plugin for LTO.
# So we don't want to do that.
if ! docker image ls $image | grep -q $image; then
if [ $arch = riscv64 ]; then
# Ubuntu 18 didn't support RISC-V, so use Ubuntu 20 instead
cat <<EOF | docker build --platform $arch -t $image -
FROM riscv64/ubuntu:20.04
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive TZ=UTC apt-get install -y --no-install-recommends build-essential gcc-10 g++-10 cmake && \
rm -rf /var/lib/apt/lists
EOF
else
cat <<EOF | docker build --platform $arch -t $image -
FROM ubuntu:18.04
RUN apt-get update && \
apt-get install -y --no-install-recommends software-properties-common && \
add-apt-repository -y ppa:ubuntu-toolchain-r/test && \
apt-get update && \
DEBIAN_FRONTEND=noninteractive TZ=UTC apt-get install -y --no-install-recommends build-essential wget libstdc++-11-dev gcc-10 g++-10 python3 libssl-dev && \
\
mkdir /cmake && cd /cmake && \
wget -O- -q https://github.com/Kitware/CMake/releases/download/v3.24.2/cmake-3.24.2.tar.gz | tar --strip-components=1 -xzf - && \
./bootstrap --parallel=$(nproc) && make -j$(nproc) && make -j$(nproc) install && \
rm -rf /var/lib/apt/lists /cmake
EOF
fi
if [ "$GITHUB_REPOSITORY" = '' ]; then
image=mold-builder-$arch
docker_build="docker build --platform linux/$arch -t $image -"
else
# If this script is running on GitHub Actions, we want to cache
# the created Docker image in GitHub's Docker repostiory.
image=ghcr.io/$GITHUB_REPOSITORY/mold-builder-$arch
docker_build="docker buildx build --platform linux/$arch -t $image --push --cache-to type=inline --cache-from type=registry,ref=ghcr.io/$GITHUB_REPOSITORY/mold-builder-$arch -"
fi
# Create a Docker image.
case $arch in
x86_64)
# Debian 8 (Jessie) released in April 2015
cat <<EOF | $docker_build
FROM debian:jessie-20210326@sha256:32ad5050caffb2c7e969dac873bce2c370015c2256ff984b70c1c08b3a2816a0
ENV DEBIAN_FRONTEND=noninteractive TZ=UTC
RUN sed -i -e '/^deb/d' -e 's/^# deb /deb [trusted=yes] /g' /etc/apt/sources.list && \
echo 'Acquire::Retries "10"; Acquire::http::timeout "10"; Acquire::Check-Valid-Until "false";' > /etc/apt/apt.conf.d/80-retries && \
apt-get update && \
apt-get install -y --no-install-recommends wget bzip2 file make autoconf gcc g++ libssl-dev && \
rm -rf /var/lib/apt/lists
# Build CMake 3.27
RUN mkdir -p /build/cmake && \
cd /build/cmake && \
wget -O- --no-check-certificate https://cmake.org/files/v3.27/cmake-3.27.7.tar.gz | tar xzf - --strip-components=1 && \
./bootstrap --parallel=\$(nproc) && \
make -j\$(nproc) && \
make install && \
rm -rf /build
# Build GCC 10
RUN mkdir -p /build/gcc && \
cd /build/gcc && \
wget -O- http://ftp.gnu.org/gnu/gcc/gcc-10.5.0/gcc-10.5.0.tar.gz | tar xzf - --strip-components=1 && \
mkdir isl gmp mpc mpfr && \
wget -O- http://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2 | tar xjf - --strip-components=1 -C isl && \
wget -O- http://ftp.gnu.org/gnu/gmp/gmp-6.1.2.tar.bz2 | tar xjf - --strip-components=1 -C gmp && \
wget -O- http://ftp.gnu.org/gnu/mpc/mpc-1.2.1.tar.gz | tar xzf - --strip-components=1 -C mpc && \
wget -O- http://ftp.gnu.org/gnu/mpfr/mpfr-4.1.0.tar.gz | tar xzf - --strip-components=1 -C mpfr && \
./configure --prefix=/usr --enable-languages=c,c++ --disable-bootstrap --disable-multilib && \
make -j\$(nproc) && \
make install && \
ln -sf /usr/lib64/libstdc++.so.6 /usr/lib/x86_64-linux-gnu/libstdc++.so.6 && \
rm -rf /build
EOF
;;
aarch64 | arm | ppc64le | s390x)
# Debian 10 (Bullseye) released in July 2019
#
# We don't want to build GCC for these targets with Qemu becuase
# that'd take extremely long time. Also I believe old build machines
# are usually x86-64.
[ $arch = aarch64 ] && digest=d5ed76c5265576982e6599b6f12392290d9b52b315b19b28b640aaba6e8af002
[ $arch = arm ] && digest=bede2623dae269454c5b6dd4af15a10810a5f4ef75963d4eb6531628f98bd633
[ $arch = ppc64le ] && digest=255f385e735469493b3465befad59a16f9d46f41d0b50e4fa6d5928c5ee7702a
[ $arch = s390x ] && digest=96fb9ce5d3ce7f3dab7c34c18edfee093904cbc7fc19162dbcca22b2cc273b9d
cat <<EOF | $docker_build
FROM debian:bullseye-20231030@sha256:$digest
ENV DEBIAN_FRONTEND=noninteractive TZ=UTC
RUN sed -i -e '/^deb/d' -e 's/^# deb /deb [trusted=yes] /g' /etc/apt/sources.list && \
echo 'Acquire::Retries "10"; Acquire::http::timeout "10"; Acquire::Check-Valid-Until "false";' > /etc/apt/apt.conf.d/80-retries && \
apt-get update && \
apt-get install -y --no-install-recommends build-essential gcc-10 g++-10 cmake && \
ln -sf /usr/bin/gcc-10 /usr/bin/cc && \
ln -sf /usr/bin/g++-10 /usr/bin/c++ && \
rm -rf /var/lib/apt/lists
EOF
;;
riscv64)
# snapshot.debian.org is not available for RISC-V binaries
cat <<EOF | $docker_build
FROM riscv64/debian:unstable-20231030@sha256:be1882409392c1f68f23e1e04bd965c49398f4a358b1a15f7b1820c0b39ede5b
ENV DEBIAN_FRONTEND=noninteractive TZ=UTC
RUN apt-get update && \
apt-get install -y --no-install-recommends build-essential gcc-12 g++-12 cmake && \
ln -sf /usr/bin/gcc-12 /usr/bin/cc && \
ln -sf /usr/bin/g++-12 /usr/bin/c++ && \
rm -rf /var/lib/apt/lists
EOF
;;
esac
# Source tarballs available on GitHub don't contain .git history.
# Clone the repo if missing.
[ -d .git ] || git clone --branch v$version --depth 1 --bare https://github.com/rui314/mold .git
# We use the timestamp of the last Git commit as the file timestamp
# for build artifacts.
timestamp="$(git log -1 --format=%ci)"
# Build mold in a container.
docker run --platform linux/$arch -i --rm -v "$(realpath $(dirname $0)):/mold" \
-e "OWNER=$(id -u):$(id -g)" $image \
bash -c "mkdir /build &&
cd /build &&
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc-10 -DCMAKE_CXX_COMPILER=g++-10 -DMOLD_MOSTLY_STATIC=On /mold &&
cmake --build . -j\$(nproc) &&
[ $arch = arm ] || ctest -j\$(nproc) &&
cmake --install . --prefix $dest --strip &&
tar czf /mold/$dest.tar.gz $dest &&
chown \$OWNER /mold/$dest.tar.gz"
docker run --platform linux/$arch -i --rm -v "$(pwd):/mold" $image bash -c "
set -e
mkdir -p /build/mold
cd /build/mold
cmake -DCMAKE_BUILD_TYPE=Release -DMOLD_MOSTLY_STATIC=On /mold
cmake --build . -j\$(nproc)
ctest -j\$(nproc)
cmake --install . --prefix $dest --strip
find $dest -print | xargs touch --no-dereference --date='$timestamp'
find $dest -print | sort | tar -cf - --no-recursion --files-from=- | gzip -9nc > /mold/$dest.tar.gz
chown $(id -u):$(id -g) /mold/$dest.tar.gz
"
+11 -5
View File
@@ -1,6 +1,6 @@
.\" generated with Ronn-NG/v0.9.1
.\" http://github.com/apjanke/ronn-ng/tree/0.9.1
.TH "MOLD" "1" "July 2023" ""
.TH "MOLD" "1" "November 2023" ""
.SH "NAME"
\fBmold\fR \- a modern linker
.SH "SYNOPSIS"
@@ -179,6 +179,9 @@ Omit \fB\.symtab\fR section from the output file\.
\fB\-u\fR \fIsymbol\fR, \fB\-\-undefined\fR=\fIsymbol\fR
If \fIsymbol\fR remains as an undefined symbol after reading all object files, and if there is a static archive that contains an object file defining \fIsymbol\fR, pull out the object file and link it so that the output file contains a definition of \fIsymbol\fR\.
.TP
\fB\-y\fR \fIsymbol\fR, \fB\-\-trace\-symbol\fR=\fIsymbol\fR
Trace references to \fIsymbol\fR\.
.TP
\fB\-\-Bdynamic\fR
Link against shared libraries\.
.TP
@@ -354,6 +357,9 @@ Create a share library\.
\fB\-\-spare\-dynamic\-tags\fR=\fInumber\fR
Reserve the given \fInumber\fR of tags in \fB\.dynamic\fR section\.
.TP
\fB\-\-spare\-program\-headers\fR=\fInumber\fR
Reserve the given \fInumber\fR of entries in the program header\.
.TP
\fB\-\-start\-lib\fR, \fB\-\-end\-lib\fR
Handle object files between \fB\-\-start\-lib\fR and \fB\-\-end\-lib\fR 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 ar(3)\.
.TP
@@ -475,13 +481,13 @@ Ignored
.SH "ENVIRONMENT VARIABLES"
.TP
\fBMOLD_JOBS\fR
If this variable is set to \fB1\fR, only one process of \fBmold\fR runs actively\. A mold process invoked while another active mold process is running will wait before doing anything until the active process exits\.
If this variable is set to \fB1\fR, only one \fBmold\fR process will run at a time\. If a new mold process is initiated while another is already active, the new process will wait until the active one completes before starting\.
.IP
The purpose of this environment variable is to reduce peak memory usage\. Since mold is highly parallelized, there's no point in running it simultaneously\. If you run N instances of mold in parallel, it would take N times more time and N times more memory\. If you run them serially, it would still take N times more to finish, but their peak memory usage is reduced to normal\.
The primary reason for this environment variable is to minimize peak memory usage\. Since mold is designed to operate with high parallelism, running multiple mold instances simultaneously may not be beneficial\. If you execute N instances of mold concurrently, it could require N times the time and N times the memory\. On the other hand, running them one after the other might still take N times longer, but the peak memory usage would be the same as running just a single instance\.
.IP
If your build system tends to invoke multiple linker processes simultaneously, you may want to try to set this environment variable to \fB1\fR to see if it could improve overall performance\.
If your build system invokes multiple linker processes simultaneously and some of them often get killed due to out\-of\-memory errors, you might consider setting this environment variable to \fB1\fR to see if it addresses the OOM issue\.
.IP
Currently, any value other than 1 is silently ignored\.
Currently, any value other than \fB1\fR is silently ignored\.
.TP
\fBMOLD_DEBUG\fR
If this variable is set to a non\-empty string, \fBmold\fR embeds its command\-line options in the output file's \fB\.comment\fR section\.
+6
View File
@@ -321,6 +321,9 @@ arguments.
_symbol_, pull out the object file and link it so that the output file
contains a definition of _symbol_.
* `-y` _symbol_, `--trace-symbol`=_symbol_:
Trace references to _symbol_.
* `--Bdynamic`:
Link against shared libraries.
@@ -581,6 +584,9 @@ arguments.
* `--spare-dynamic-tags`=_number_:
Reserve the given _number_ of tags in `.dynamic` section.
* `--spare-program-headers`=_number_:
Reserve the given _number_ of entries in the program header.
* `--start-lib`, `--end-lib`:
Handle object files between `--start-lib` and `--end-lib` as if they were in
an archive file. That means object files between them are linked only when
+1 -1
View File
@@ -647,7 +647,7 @@ void InputSection<E>::scan_relocations(Context<E> &ctx) {
}
template <>
void RangeExtensionThunk<E>::copy_buf(Context<E> &ctx) {
void Thunk<E>::copy_buf(Context<E> &ctx) {
// TLS trampoline code. ARM32's TLSDESC is designed so that this
// common piece of code is factored out from object files to reduce
// output size. Since no one provide, the linker has to synthesize it.
+1 -1
View File
@@ -602,7 +602,7 @@ void InputSection<E>::scan_relocations(Context<E> &ctx) {
}
template <>
void RangeExtensionThunk<E>::copy_buf(Context<E> &ctx) {
void Thunk<E>::copy_buf(Context<E> &ctx) {
static const ul32 insn[] = {
0x9000'0010, // adrp x16, 0 # R_AARCH64_ADR_PREL_PG_HI21
0x9100'0210, // add x16, x16 # R_AARCH64_ADD_ABS_LO12_NC
+47 -4
View File
@@ -254,6 +254,34 @@ static void relax_ld_to_le(u8 *loc, ElfRel<E> rel, u64 val) {
}
}
static u32 relax_tlsdesc_to_ie(u8 *loc) {
switch ((loc[0] << 8) | loc[1]) {
case 0x8d83: return 0x8b83; // lea 0(%ebx), %eax -> mov 0(%ebx), %eax
case 0x8d9b: return 0x8b9b; // lea 0(%ebx), %ebx -> mov 0(%ebx), %ebx
case 0x8d8b: return 0x8b8b; // lea 0(%ebx), %ecx -> mov 0(%ebx), %ecx
case 0x8d93: return 0x8b93; // lea 0(%ebx), %edx -> mov 0(%ebx), %edx
case 0x8db3: return 0x8bb3; // lea 0(%ebx), %esi -> mov 0(%ebx), %esi
case 0x8dbb: return 0x8bbb; // lea 0(%ebx), %edi -> mov 0(%ebx), %edi
case 0x8da3: return 0x8ba3; // lea 0(%ebx), %esp -> mov 0(%ebx), %esp
case 0x8dab: return 0x8bab; // lea 0(%ebx), %ebp -> mov 0(%ebx), %ebp
}
return 0;
}
static u32 relax_tlsdesc_to_le(u8 *loc) {
switch ((loc[0] << 8) | loc[1]) {
case 0x8d83: return 0x90b8; // lea 0(%ebx), %eax -> mov $0, %eax
case 0x8d9b: return 0x90bb; // lea 0(%ebx), %ebx -> mov $0, %ebx
case 0x8d8b: return 0x90b9; // lea 0(%ebx), %ecx -> mov $0, %ecx
case 0x8d93: return 0x90ba; // lea 0(%ebx), %edx -> mov $0, %edx
case 0x8db3: return 0x90be; // lea 0(%ebx), %esi -> mov $0, %esi
case 0x8dbb: return 0x90bf; // lea 0(%ebx), %edi -> mov $0, %edi
case 0x8da3: return 0x90bc; // lea 0(%ebx), %esp -> mov $0, %esp
case 0x8dab: return 0x90bd; // lea 0(%ebx), %ebp -> mov $0, %ebp
}
return 0;
}
template <>
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
@@ -373,15 +401,30 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
//
// mov $foo@TPOFF, %eax
// nop
//
// We allow the following alternative code sequence too because
// LLVM emits such code.
//
// lea 0(%ebx), %reg
// R_386_TLS_GOTDESC foo
// mov %reg, %eax
// call *(%eax)
// R_386_TLS_DESC_CALL foo
if (sym.has_tlsdesc(ctx)) {
*(ul32 *)loc = sym.get_tlsdesc_addr(ctx) + A - GOT;
} else if (sym.has_gottp(ctx)) {
loc[-2] = 0x8b;
loc[-1] = 0x83;
u32 insn = relax_tlsdesc_to_ie(loc - 2);
if (!insn)
Fatal(ctx) << *this << ": illegal instruction sequence for TLSDESC";
loc[-2] = insn >> 8;
loc[-1] = insn;
*(ul32 *)loc = sym.get_gottp_addr(ctx) + A - GOT;
} else {
loc[-2] = 0x90;
loc[-1] = 0xb8;
u32 insn = relax_tlsdesc_to_le(loc - 2);
if (!insn)
Fatal(ctx) << *this << ": illegal instruction sequence for TLSDESC";
loc[-2] = insn >> 8;
loc[-1] = insn;
*(ul32 *)loc = S + A - ctx.tp_addr;
}
break;
+1 -6
View File
@@ -569,11 +569,6 @@ void InputSection<E>::scan_relocations(Context<E> &ctx) {
Symbol<E> &sym = *file.symbols[rel.r_sym];
if (!sym.file) {
record_undef_error(ctx, rel);
continue;
}
if (sym.is_ifunc())
sym.flags |= NEEDS_GOT | NEEDS_PLT;
@@ -658,7 +653,7 @@ void InputSection<E>::scan_relocations(Context<E> &ctx) {
}
template <>
void RangeExtensionThunk<E>::copy_buf(Context<E> &ctx) {
void Thunk<E>::copy_buf(Context<E> &ctx) {
static const ul32 insn[] = {
0x1e00'000c, // pcaddu18i $t0, 0
0x4c00'0180, // jirl $zero, $t0, 0
+1 -1
View File
@@ -408,7 +408,7 @@ void InputSection<E>::scan_relocations(Context<E> &ctx) {
}
template <>
void RangeExtensionThunk<E>::copy_buf(Context<E> &ctx) {
void Thunk<E>::copy_buf(Context<E> &ctx) {
static const ub32 local_thunk[] = {
// Get this thunk's address
0x7c08'02a6, // mflr r0
+8 -9
View File
@@ -409,7 +409,7 @@ void InputSection<E>::scan_relocations(Context<E> &ctx) {
}
template <>
void RangeExtensionThunk<E>::copy_buf(Context<E> &ctx) {
void Thunk<E>::copy_buf(Context<E> &ctx) {
// If the destination is .plt.got, we save the current r2, read an
// address of a function descriptor from .got, restore %r2 and jump
// to the function.
@@ -638,15 +638,14 @@ void ppc64v1_scan_symbols(Context<E> &ctx) {
});
// Functions referenced by the ELF header also have to have .opd entries.
auto mark = [&](std::string_view name) {
if (!name.empty())
if (Symbol<E> &sym = *get_symbol(ctx, name); !sym.is_imported)
sym.flags |= NEEDS_PPC_OPD;
};
if (!ctx.arg.entry->is_imported)
ctx.arg.entry->flags |= NEEDS_PPC_OPD;
mark(ctx.arg.entry);
mark(ctx.arg.init);
mark(ctx.arg.fini);
if (!ctx.arg.init->is_imported)
ctx.arg.init->flags |= NEEDS_PPC_OPD;
if (!ctx.arg.fini->is_imported)
ctx.arg.fini->flags |= NEEDS_PPC_OPD;
}
void PPC64OpdSection::add_symbol(Context<E> &ctx, Symbol<E> *sym) {
+1 -1
View File
@@ -484,7 +484,7 @@ void InputSection<E>::scan_relocations(Context<E> &ctx) {
}
template <>
void RangeExtensionThunk<E>::copy_buf(Context<E> &ctx) {
void Thunk<E>::copy_buf(Context<E> &ctx) {
// If the destination is PLT, we read an address from .got.plt or .got
// and jump there.
static const ul32 plt_thunk[] = {
+65 -8
View File
@@ -178,6 +178,50 @@ static u32 relax_gottpoff(u8 *loc) {
return 0;
}
static u32 relax_tlsdesc_to_ie(u8 *loc) {
switch ((loc[0] << 16) | (loc[1] << 8) | loc[2]) {
case 0x488d05: return 0x488b05; // lea 0(%rip), %rax -> mov 0(%rip), %rax
case 0x488d0d: return 0x488b0d; // lea 0(%rip), %rcx -> mov 0(%rip), %rcx
case 0x488d15: return 0x488b15; // lea 0(%rip), %rdx -> mov 0(%rip), %rdx
case 0x488d1d: return 0x488b1d; // lea 0(%rip), %rbx -> mov 0(%rip), %rbx
case 0x488d25: return 0x488b25; // lea 0(%rip), %rsp -> mov 0(%rip), %rsp
case 0x488d2d: return 0x488b2d; // lea 0(%rip), %rbp -> mov 0(%rip), %rbp
case 0x488d35: return 0x488b35; // lea 0(%rip), %rsi -> mov 0(%rip), %rsi
case 0x488d3d: return 0x488b3d; // lea 0(%rip), %rdi -> mov 0(%rip), %rdi
case 0x4c8d05: return 0x4c8b05; // lea 0(%rip), %r8 -> mov 0(%rip), %r8
case 0x4c8d0d: return 0x4c8b0d; // lea 0(%rip), %r9 -> mov 0(%rip), %r9
case 0x4c8d15: return 0x4c8b15; // lea 0(%rip), %r10 -> mov 0(%rip), %r10
case 0x4c8d1d: return 0x4c8b1d; // lea 0(%rip), %r11 -> mov 0(%rip), %r11
case 0x4c8d25: return 0x4c8b25; // lea 0(%rip), %r12 -> mov 0(%rip), %r12
case 0x4c8d2d: return 0x4c8b2d; // lea 0(%rip), %r13 -> mov 0(%rip), %r13
case 0x4c8d35: return 0x4c8b35; // lea 0(%rip), %r14 -> mov 0(%rip), %r14
case 0x4c8d3d: return 0x4c8b3d; // lea 0(%rip), %r15 -> mov 0(%rip), %r15
}
return 0;
}
static u32 relax_tlsdesc_to_le(u8 *loc) {
switch ((loc[0] << 16) | (loc[1] << 8) | loc[2]) {
case 0x488d05: return 0x48c7c0; // lea 0(%rip), %rax -> mov $0, %rax
case 0x488d0d: return 0x48c7c1; // lea 0(%rip), %rcx -> mov $0, %rcx
case 0x488d15: return 0x48c7c2; // lea 0(%rip), %rdx -> mov $0, %rdx
case 0x488d1d: return 0x48c7c3; // lea 0(%rip), %rbx -> mov $0, %rbx
case 0x488d25: return 0x48c7c4; // lea 0(%rip), %rsp -> mov $0, %rsp
case 0x488d2d: return 0x48c7c5; // lea 0(%rip), %rbp -> mov $0, %rbp
case 0x488d35: return 0x48c7c6; // lea 0(%rip), %rsi -> mov $0, %rsi
case 0x488d3d: return 0x48c7c7; // lea 0(%rip), %rdi -> mov $0, %rdi
case 0x4c8d05: return 0x49c7c0; // lea 0(%rip), %r8 -> mov $0, %r8
case 0x4c8d0d: return 0x49c7c1; // lea 0(%rip), %r9 -> mov $0, %r9
case 0x4c8d15: return 0x49c7c2; // lea 0(%rip), %r10 -> mov $0, %r10
case 0x4c8d1d: return 0x49c7c3; // lea 0(%rip), %r11 -> mov $0, %r11
case 0x4c8d25: return 0x49c7c4; // lea 0(%rip), %r12 -> mov $0, %r12
case 0x4c8d2d: return 0x49c7c5; // lea 0(%rip), %r13 -> mov $0, %r13
case 0x4c8d35: return 0x49c7c6; // lea 0(%rip), %r14 -> mov $0, %r14
case 0x4c8d3d: return 0x49c7c7; // lea 0(%rip), %r15 -> mov $0, %r15
}
return 0;
}
// Rewrite a function call to __tls_get_addr to a cheaper instruction
// sequence. We can do this when we know the thread-local variable's TP-
// relative address at link-time.
@@ -501,19 +545,32 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
//
// mov $foo@TPOFF, %rax
// nop
//
// We allow the following alternative code sequence too because
// LLVM emits such code.
//
// lea 0(%rip), %reg
// R_X86_64_GOTPC32_TLSDESC foo
// mov %reg, %rax
// call *(%rax)
// R_X86_64_TLSDESC_CALL foo
if (sym.has_tlsdesc(ctx)) {
write32s(sym.get_tlsdesc_addr(ctx) + A - P);
} else if (sym.has_gottp(ctx)) {
// mov foo@gottpoff(%rip), %rax
loc[-3] = 0x48;
loc[-2] = 0x8b;
loc[-1] = 0x05;
u32 insn = relax_tlsdesc_to_ie(loc - 3);
if (!insn)
Fatal(ctx) << *this << ": illegal instruction sequence for TLSDESC";
loc[-3] = insn >> 16;
loc[-2] = insn >> 8;
loc[-1] = insn;
write32s(sym.get_gottp_addr(ctx) + A - P);
} else {
// mov $foo@tpoff, %rax
loc[-3] = 0x48;
loc[-2] = 0xc7;
loc[-1] = 0xc0;
u32 insn = relax_tlsdesc_to_le(loc - 3);
if (!insn)
Fatal(ctx) << *this << ": illegal instruction sequence for TLSDESC";
loc[-3] = insn >> 16;
loc[-2] = insn >> 8;
loc[-1] = insn;
write32s(S - ctx.tp_addr);
}
break;
+38 -21
View File
@@ -52,6 +52,8 @@ Options:
-s, --strip-all Strip .symtab section
-u SYMBOL, --undefined SYMBOL
Force to resolve SYMBOL
-y SYMBOL, --trace-symbol SYMBOL
Trace references to SYMBOL
--Bdynamic, --dy Link against shared libraries (default)
--Bstatic, --dn, --static Do not link against shared libraries
--Bsymbolic Bind global symbols locally
@@ -107,6 +109,8 @@ Options:
Allow merging non-executable sections with --icf
--image-base ADDR Set the base address to a given value
--init SYMBOL Call SYMBOL at load-time
--nmagic Do not page align sections
--no-nmagic
--no-undefined Report undefined symbols (even with --shared)
--noinhibit-exec Create an output file even if errors occur
--oformat=binary Omit ELF, section and program headers
@@ -140,7 +144,9 @@ Options:
--shuffle-sections[=SEED] Randomize the output by shuffling input sections
--sort-common Ignored
--sort-section Ignored
--spare-dynamic-tags NUMBER Reserve give number of tags in .dynamic section
--spare-dynamic-tags NUMBER Reserve given number of tags in .dynamic section
--spare-program-headers NUMBER
Reserve given number of slots in the program header
--start-lib Give following object files in-archive-file semantics
--end-lib End the effect of --start-lib
--stats Print input statistics
@@ -586,7 +592,7 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
ctx.arg.emit_relocs = true;
ctx.arg.discard_locals = false;
} else if (read_arg("e") || read_arg("entry")) {
ctx.arg.entry = arg;
ctx.arg.entry = get_symbol(ctx, arg);
} else if (read_arg("Map")) {
ctx.arg.Map = arg;
ctx.arg.print_map = true;
@@ -604,6 +610,9 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
ctx.arg.shared = true;
} else if (read_arg("spare-dynamic-tags")) {
ctx.arg.spare_dynamic_tags = parse_number(ctx, "spare-dynamic-tags", arg);
} else if (read_arg("spare-program-headers")) {
ctx.arg.spare_program_headers
= parse_number(ctx, "spare-program-headers", arg);
} else if (read_flag("start-lib")) {
remaining.push_back("--start-lib");
} else if (read_flag("start-stop")) {
@@ -660,13 +669,13 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
else
Fatal(ctx) << "unknown --unresolved-symbols argument: " << arg;
} else if (read_arg("undefined") || read_arg("u")) {
ctx.arg.undefined.push_back(arg);
ctx.arg.undefined.push_back(get_symbol(ctx, arg));
} else if (read_arg("require-defined")) {
ctx.arg.require_defined.push_back(arg);
ctx.arg.require_defined.push_back(get_symbol(ctx, arg));
} else if (read_arg("init")) {
ctx.arg.init = arg;
ctx.arg.init = get_symbol(ctx, arg);
} else if (read_arg("fini")) {
ctx.arg.fini = arg;
ctx.arg.fini = get_symbol(ctx, arg);
} else if (read_arg("hash-style")) {
if (arg == "sysv") {
ctx.arg.hash_style_sysv = true;
@@ -887,6 +896,10 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
ctx.arg.z_rewrite_endbr = true;
} else if (read_flag("no-undefined")) {
ctx.arg.z_defs = true;
} else if (read_flag("nmagic")) {
ctx.arg.nmagic = true;
} else if (read_flag("no-nmagic")) {
ctx.arg.nmagic = false;
} else if (read_flag("fatal-warnings")) {
ctx.arg.fatal_warnings = true;
} else if (read_flag("no-fatal-warnings")) {
@@ -1104,21 +1117,21 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
} else if (read_flag("no-keep-memory")) {
} else if (read_arg("max-cache-size")) {
} else if (read_arg("version-script")) {
// --version-script, --dynamic-list and --export-dynamic-symbol[-list]
// are treated as positional arguments even though they are actually not
// positional. This is because linker scripts (a positional argument)
// can also specify a version script, and it's better to consolidate
// parsing in read_input_files. In particular, version scripts can
// modify ctx.default_version which we initialize *after* parsing
// non-positional args, so the parsing cannot be done right here.
// --version-script is treated as positional arguments even though
// they are actually not positional. This is because linker scripts
// (a positional argument) can also specify a version script, and
// it's better to consolidate parsing in read_input_files. In
// particular, version scripts can modify ctx.default_version which
// we initialize *after* parsing non-positional args, so the parsing
// cannot be done right here.
remaining.push_back("--version-script=" + std::string(arg));
} else if (read_arg("dynamic-list")) {
ctx.arg.Bsymbolic = true;
remaining.push_back("--dynamic-list=" + std::string(arg));
append(ctx.dynamic_list_patterns, parse_dynamic_list(ctx, arg));
} else if (read_arg("export-dynamic-symbol")) {
remaining.push_back("--export-dynamic-symbol=" + std::string(arg));
ctx.dynamic_list_patterns.push_back({arg, "<command line>"});
} else if (read_arg("export-dynamic-symbol-list")) {
remaining.push_back("--export-dynamic-symbol-list=" + std::string(arg));
append(ctx.dynamic_list_patterns, parse_dynamic_list(ctx, arg));
} else if (read_flag("as-needed")) {
remaining.push_back("--as-needed");
} else if (read_flag("no-as-needed")) {
@@ -1200,6 +1213,9 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
else if (!ctx.arg.section_order.empty())
ctx.arg.z_relro = false;
if (ctx.arg.nmagic)
ctx.arg.z_relro = false;
if (!ctx.arg.shared) {
if (!ctx.arg.filter.empty())
Fatal(ctx) << "-filter may not be used without -shared";
@@ -1228,11 +1244,6 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
if (char *env = getenv("MOLD_REPRO"); env && env[0])
ctx.arg.repro = true;
if (ctx.arg.shared || ctx.arg.export_dynamic)
ctx.default_version = VER_NDX_GLOBAL;
else
ctx.default_version = VER_NDX_LOCAL;
if (ctx.arg.default_symver) {
std::string ver = ctx.arg.soname.empty() ?
filepath(ctx.arg.output).filename().string() : std::string(ctx.arg.soname);
@@ -1245,6 +1256,12 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
ctx.arg.undefined.push_back(ctx.arg.entry);
for (i64 i = 0; i < ctx.arg.defsyms.size(); i++) {
std::variant<Symbol<E> *, u64> &val = ctx.arg.defsyms[i].second;
if (Symbol<E> **sym = std::get_if<Symbol<E> *>(&val))
ctx.arg.undefined.push_back(*sym);
}
// --oformat=binary implies --strip-all because without a section
// header, there's no way to identify the locations of a symbol
// table in an output file in the first place.
+17 -16
View File
@@ -161,6 +161,7 @@ enum : u32 {
VER_NDX_LOCAL = 0,
VER_NDX_GLOBAL = 1,
VER_NDX_LAST_RESERVED = 1,
VER_NDX_UNSPECIFIED = 0xffff,
};
enum : u32 {
@@ -1868,7 +1869,7 @@ struct X86_64 {
static constexpr u32 plt_hdr_size = 32;
static constexpr u32 plt_size = 16;
static constexpr u32 pltgot_size = 8;
static constexpr u8 filler[] = { 0xcc };
static constexpr u8 filler[] = { 0xcc }; // int3
static constexpr u32 R_COPY = R_X86_64_COPY;
static constexpr u32 R_GLOB_DAT = R_X86_64_GLOB_DAT;
@@ -1893,7 +1894,7 @@ struct I386 {
static constexpr u32 plt_hdr_size = 16;
static constexpr u32 plt_size = 16;
static constexpr u32 pltgot_size = 16;
static constexpr u8 filler[] = { 0xcc };
static constexpr u8 filler[] = { 0xcc }; // int3
static constexpr u32 R_COPY = R_386_COPY;
static constexpr u32 R_GLOB_DAT = R_386_GLOB_DAT;
@@ -1920,7 +1921,7 @@ struct ARM64 {
static constexpr u32 pltgot_size = 16;
static constexpr u32 thunk_hdr_size = 0;
static constexpr u32 thunk_size = 16;
static constexpr u8 filler[] = { 0x00, 0x7d, 0x20, 0xd4 };
static constexpr u8 filler[] = { 0x00, 0x7d, 0x20, 0xd4 }; // brk
static constexpr u32 R_COPY = R_AARCH64_COPY;
static constexpr u32 R_GLOB_DAT = R_AARCH64_GLOB_DAT;
@@ -1947,7 +1948,7 @@ struct ARM32 {
static constexpr u32 pltgot_size = 16;
static constexpr u32 thunk_hdr_size = 16;
static constexpr u32 thunk_size = 16;
static constexpr u8 filler[] = { 0xff, 0xde };
static constexpr u8 filler[] = { 0xff, 0xde }; // udf
static constexpr u32 R_COPY = R_ARM_COPY;
static constexpr u32 R_GLOB_DAT = R_ARM_GLOB_DAT;
@@ -1973,7 +1974,7 @@ struct RV64 {
static constexpr u32 plt_hdr_size = 32;
static constexpr u32 plt_size = 16;
static constexpr u32 pltgot_size = 16;
static constexpr u8 filler[] = { 0x02, 0x90 };
static constexpr u8 filler[] = { 0x02, 0x90 }; // ebreak
static constexpr u32 R_COPY = R_RISCV_COPY;
static constexpr u32 R_GLOB_DAT = R_RISCV_64;
@@ -2006,7 +2007,7 @@ struct RV32 {
static constexpr u32 plt_hdr_size = 32;
static constexpr u32 plt_size = 16;
static constexpr u32 pltgot_size = 16;
static constexpr u8 filler[] = { 0x02, 0x90 };
static constexpr u8 filler[] = { 0x02, 0x90 }; // ebreak
static constexpr u32 R_COPY = R_RISCV_COPY;
static constexpr u32 R_GLOB_DAT = R_RISCV_32;
@@ -2043,7 +2044,7 @@ struct PPC32 {
static constexpr u32 pltgot_size = 36;
static constexpr u32 thunk_hdr_size = 0;
static constexpr u32 thunk_size = 36;
static constexpr u8 filler[] = { 0x7f, 0xe0, 0x00, 0x08 };
static constexpr u8 filler[] = { 0x7f, 0xe0, 0x00, 0x08 }; // trap
static constexpr u32 R_COPY = R_PPC_COPY;
static constexpr u32 R_GLOB_DAT = R_PPC_GLOB_DAT;
@@ -2085,7 +2086,7 @@ struct PPC64V1 : PPC64 {
static constexpr u32 pltgot_size = 0;
static constexpr u32 thunk_hdr_size = 0;
static constexpr u32 thunk_size = 28;
static constexpr u8 filler[] = { 0x7f, 0xe0, 0x00, 0x08 };
static constexpr u8 filler[] = { 0x7f, 0xe0, 0x00, 0x08 }; // trap
};
struct PPC64V2 : PPC64 {
@@ -2096,7 +2097,7 @@ struct PPC64V2 : PPC64 {
static constexpr u32 pltgot_size = 0;
static constexpr u32 thunk_hdr_size = 0;
static constexpr u32 thunk_size = 24;
static constexpr u8 filler[] = { 0x08, 0x00, 0xe0, 0x7f };
static constexpr u8 filler[] = { 0x08, 0x00, 0xe0, 0x7f }; // trap
};
struct S390X {
@@ -2109,7 +2110,7 @@ struct S390X {
static constexpr u32 plt_hdr_size = 48;
static constexpr u32 plt_size = 16;
static constexpr u32 pltgot_size = 16;
static constexpr u8 filler[] = { 0x07, 0x00 };
static constexpr u8 filler[] = { 0x07, 0x00 }; // nop
static constexpr u32 R_COPY = R_390_COPY;
static constexpr u32 R_GLOB_DAT = R_390_GLOB_DAT;
@@ -2133,7 +2134,7 @@ struct SPARC64 {
static constexpr u32 plt_hdr_size = 128;
static constexpr u32 plt_size = 32;
static constexpr u32 pltgot_size = 32;
static constexpr u8 filler[] = { 0x91, 0xd0, 0x20, 0x05 };
static constexpr u8 filler[] = { 0x91, 0xd0, 0x20, 0x05 }; // ta 5
static constexpr u32 R_COPY = R_SPARC_COPY;
static constexpr u32 R_GLOB_DAT = R_SPARC_GLOB_DAT;
@@ -2157,7 +2158,7 @@ struct M68K {
static constexpr u32 plt_hdr_size = 18;
static constexpr u32 plt_size = 14;
static constexpr u32 pltgot_size = 8;
static constexpr u8 filler[] = { 0x4a, 0xfc };
static constexpr u8 filler[] = { 0x4a, 0xfc }; // illegal
static constexpr u32 R_COPY = R_68K_COPY;
static constexpr u32 R_GLOB_DAT = R_68K_GLOB_DAT;
@@ -2180,7 +2181,7 @@ struct SH4 {
static constexpr u32 plt_hdr_size = 16;
static constexpr u32 plt_size = 16;
static constexpr u32 pltgot_size = 12;
static constexpr u8 filler[] = { 0x00, 0x00 };
static constexpr u8 filler[] = { 0x00, 0x90 }; // nop
static constexpr u32 R_COPY = R_SH_COPY;
static constexpr u32 R_GLOB_DAT = R_SH_GLOB_DAT;
@@ -2203,7 +2204,7 @@ struct ALPHA {
static constexpr u32 plt_hdr_size = 0;
static constexpr u32 plt_size = 0;
static constexpr u32 pltgot_size = 0;
static constexpr u8 filler[] = { 0x81, 0x00, 0x00, 0x00 };
static constexpr u8 filler[] = { 0x81, 0x00, 0x00, 0x00 }; // bugchk
static constexpr u32 R_COPY = R_ALPHA_COPY;
static constexpr u32 R_GLOB_DAT = R_ALPHA_GLOB_DAT;
@@ -2228,7 +2229,7 @@ struct LOONGARCH64 {
static constexpr u32 pltgot_size = 16;
static constexpr u32 thunk_hdr_size = 0;
static constexpr u32 thunk_size = 8;
static constexpr u8 filler[] = { 0x00, 0x00, 0x2a, 0x00 };
static constexpr u8 filler[] = { 0x00, 0x00, 0x2a, 0x00 }; // break 0
static constexpr u32 R_COPY = R_LARCH_COPY;
static constexpr u32 R_GLOB_DAT = R_LARCH_64;
@@ -2254,7 +2255,7 @@ struct LOONGARCH32 {
static constexpr u32 pltgot_size = 16;
static constexpr u32 thunk_hdr_size = 0;
static constexpr u32 thunk_size = 8;
static constexpr u8 filler[] = { 0x00, 0x00, 0x2a, 0x00 };
static constexpr u8 filler[] = { 0x00, 0x00, 0x2a, 0x00 }; // break 0
static constexpr u32 R_COPY = R_LARCH_COPY;
static constexpr u32 R_GLOB_DAT = R_LARCH_32;
+4 -6
View File
@@ -114,13 +114,11 @@ static void collect_root_set(Context<E> &ctx,
});
// Add sections referenced by root symbols.
enqueue_symbol(get_symbol(ctx, ctx.arg.entry));
for (Symbol<E> *sym : ctx.arg.undefined)
enqueue_symbol(sym);
for (std::string_view name : ctx.arg.undefined)
enqueue_symbol(get_symbol(ctx, name));
for (std::string_view name : ctx.arg.require_defined)
enqueue_symbol(get_symbol(ctx, name));
for (Symbol<E> *sym : ctx.arg.require_defined)
enqueue_symbol(sym);
// .eh_frame consists of variable-length records called CIE and FDE
// records, and they are a unit of inclusion or exclusion.
+72 -59
View File
@@ -63,7 +63,7 @@ void InputFile<E>::clear_symbols() {
sym->origin = 0;
sym->value = -1;
sym->sym_idx = -1;
sym->ver_idx = 0;
sym->ver_idx = VER_NDX_UNSPECIFIED;
sym->is_weak = false;
sym->is_imported = false;
sym->is_exported = false;
@@ -314,8 +314,17 @@ void ObjectFile<E>::initialize_sections(Context<E> &ctx) {
continue;
}
if (shdr.sh_type == SHT_INIT_ARRAY ||
shdr.sh_type == SHT_FINI_ARRAY ||
shdr.sh_type == SHT_PREINIT_ARRAY)
ctx.has_init_array = true;
if (name == ".ctors" || name.starts_with(".ctors.") ||
name == ".dtors" || name.starts_with(".dtors."))
ctx.has_ctors = true;
if (name == ".eh_frame")
eh_frame_section = this->sections[i].get();
eh_frame_sections.push_back(this->sections[i].get());
if constexpr (is_ppc32<E>)
if (name == ".got2")
@@ -407,78 +416,75 @@ void ObjectFile<E>::initialize_sections(Context<E> &ctx) {
// This function parses an input .eh_frame section.
template <typename E>
void ObjectFile<E>::parse_ehframe(Context<E> &ctx) {
if (!eh_frame_section)
return;
for (InputSection<E> *isec : eh_frame_sections) {
std::span<ElfRel<E>> rels = isec->get_rels(ctx);
i64 cies_begin = cies.size();
i64 fdes_begin = fdes.size();
InputSection<E> &isec = *eh_frame_section;
std::span<ElfRel<E>> rels = isec.get_rels(ctx);
i64 cies_begin = cies.size();
i64 fdes_begin = fdes.size();
// Read CIEs and FDEs until empty.
std::string_view contents = this->get_string(ctx, isec->shdr());
i64 rel_idx = 0;
// Read CIEs and FDEs until empty.
std::string_view contents = this->get_string(ctx, isec.shdr());
i64 rel_idx = 0;
for (std::string_view data = contents; !data.empty();) {
i64 size = *(U32<E> *)data.data();
if (size == 0)
break;
for (std::string_view data = contents; !data.empty();) {
i64 size = *(U32<E> *)data.data();
if (size == 0)
break;
i64 begin_offset = data.data() - contents.data();
i64 end_offset = begin_offset + size + 4;
i64 id = *(U32<E> *)(data.data() + 4);
data = data.substr(size + 4);
i64 begin_offset = data.data() - contents.data();
i64 end_offset = begin_offset + size + 4;
i64 id = *(U32<E> *)(data.data() + 4);
data = data.substr(size + 4);
i64 rel_begin = rel_idx;
while (rel_idx < rels.size() && rels[rel_idx].r_offset < end_offset)
rel_idx++;
assert(rel_idx == rels.size() || begin_offset <= rels[rel_begin].r_offset);
i64 rel_begin = rel_idx;
while (rel_idx < rels.size() && rels[rel_idx].r_offset < end_offset)
rel_idx++;
assert(rel_idx == rels.size() || begin_offset <= rels[rel_begin].r_offset);
if (id == 0) {
// This is CIE.
cies.emplace_back(ctx, *this, *isec, begin_offset, rels, rel_begin);
} else {
// This is FDE.
if (rel_begin == rel_idx || rels[rel_begin].r_sym == 0) {
// FDE has no valid relocation, which means FDE is dead from
// the beginning. Compilers usually don't create such FDE, but
// `ld -r` tend to generate such dead FDEs.
continue;
}
if (id == 0) {
// This is CIE.
cies.emplace_back(ctx, *this, isec, begin_offset, rels, rel_begin);
} else {
// This is FDE.
if (rel_begin == rel_idx || rels[rel_begin].r_sym == 0) {
// FDE has no valid relocation, which means FDE is dead from
// the beginning. Compilers usually don't create such FDE, but
// `ld -r` tend to generate such dead FDEs.
continue;
if (rels[rel_begin].r_offset - begin_offset != 8)
Fatal(ctx) << *isec << ": FDE's first relocation should have offset 8";
fdes.emplace_back(begin_offset, rel_begin);
}
}
if (rels[rel_begin].r_offset - begin_offset != 8)
Fatal(ctx) << isec << ": FDE's first relocation should have offset 8";
// Associate CIEs to FDEs.
auto find_cie = [&](i64 offset) {
for (i64 i = cies_begin; i < cies.size(); i++)
if (cies[i].input_offset == offset)
return i;
Fatal(ctx) << *isec << ": bad FDE pointer";
};
fdes.emplace_back(begin_offset, rel_begin);
for (i64 i = fdes_begin; i < fdes.size(); i++) {
i64 cie_offset = *(I32<E> *)(contents.data() + fdes[i].input_offset + 4);
fdes[i].cie_idx = find_cie(fdes[i].input_offset + 4 - cie_offset);
}
}
// Associate CIEs to FDEs.
auto find_cie = [&](i64 offset) {
for (i64 i = cies_begin; i < cies.size(); i++)
if (cies[i].input_offset == offset)
return i;
Fatal(ctx) << isec << ": bad FDE pointer";
};
for (i64 i = fdes_begin; i < fdes.size(); i++) {
i64 cie_offset = *(I32<E> *)(contents.data() + fdes[i].input_offset + 4);
fdes[i].cie_idx = find_cie(fdes[i].input_offset + 4 - cie_offset);
}
auto get_isec = [&](const FdeRecord<E> &fde) -> InputSection<E> * {
return get_section(this->elf_syms[rels[fde.rel_idx].r_sym]);
auto get_isec = [&](const FdeRecord<E> &fde) {
return get_section(this->elf_syms[fde.get_rels(*this)[0].r_sym]);
};
// We assume that FDEs for the same input sections are contiguous
// in `fdes` vector.
std::stable_sort(fdes.begin() + fdes_begin, fdes.end(),
[&](const FdeRecord<E> &a, const FdeRecord<E> &b) {
sort(fdes, [&](const FdeRecord<E> &a, const FdeRecord<E> &b) {
return get_isec(a)->get_priority() < get_isec(b)->get_priority();
});
// Associate FDEs to input sections.
for (i64 i = fdes_begin; i < fdes.size();) {
for (i64 i = 0; i < fdes.size();) {
InputSection<E> *isec = get_isec(fdes[i]);
assert(isec->fde_begin == -1);
isec->fde_begin = i++;
@@ -644,9 +650,13 @@ split_section(Context<E> &ctx, InputSection<E> &sec) {
if (entsize == 0)
return nullptr;
i64 addralign = shdr.sh_addralign;
if (addralign == 0)
addralign = 1;
std::unique_ptr<MergeableSection<E>> rec(new MergeableSection<E>);
rec->parent = MergedSection<E>::get_instance(ctx, sec.name(), shdr.sh_type,
shdr.sh_flags, entsize);
shdr.sh_flags, entsize, addralign);
rec->p2align = sec.p2align;
if (sec.sh_size == 0)
@@ -1277,17 +1287,17 @@ void SharedFile<E>::parse(Context<E> &ctx) {
continue;
std::string_view name = this->symbol_strtab.data() + esyms[i].st_name;
bool is_hidden = (!vers.empty() && (vers[i] & VERSYM_HIDDEN));
bool is_default = vers.empty() || !(vers[i] & VERSYM_HIDDEN);
this->elf_syms2.push_back(esyms[i]);
this->versyms.push_back(ver);
if (is_hidden) {
if (is_default) {
this->symbols.push_back(get_symbol(ctx, name));
} else {
std::string_view mangled_name = save_string(
ctx, std::string(name) + "@" + std::string(version_strings[ver]));
this->symbols.push_back(get_symbol(ctx, mangled_name, name));
} else {
this->symbols.push_back(get_symbol(ctx, name));
}
}
@@ -1342,6 +1352,9 @@ std::vector<std::string_view> SharedFile<E>::read_verdef(Context<E> &ctx) {
ElfVerdef<E> *ver = (ElfVerdef<E> *)verdef.data();
for (;;) {
if (ver->vd_ndx == VER_NDX_UNSPECIFIED)
Fatal(ctx) << *this << ": symbol version too large";
if (ret.size() <= ver->vd_ndx)
ret.resize(ver->vd_ndx + 1);
+1 -13
View File
@@ -274,7 +274,7 @@ static Action get_absrel_action(Context<E> &ctx, Symbol<E> &sym) {
template <typename E>
static Action get_dyn_absrel_action(Context<E> &ctx, Symbol<E> &sym) {
if (sym.is_ifunc())
return ctx.arg.pic ? IFUNC_DYNREL : NONE;
return sym.is_pde_ifunc(ctx) ? NONE : IFUNC_DYNREL;
// This is a decision table for absolute relocations for the pointer
// size data (e.g. R_X86_64_64). Unlike the absrel_table, we can emit
@@ -457,18 +457,6 @@ void InputSection<E>::write_to(Context<E> &ctx, u8 *buf) {
apply_reloc_alloc(ctx, buf);
else
apply_reloc_nonalloc(ctx, buf);
if constexpr (is_x86_64<E>) {
u8 endbr[] = {0xf3, 0x0f, 0x1e, 0xfa};
u8 nop[] = {0x0f, 0x1f, 0x40, 0x00};
// Rewrite the leading endbr instruction with a nop if the section
// is not address-taken.
if (ctx.arg.z_rewrite_endbr && (shdr().sh_flags & SHF_EXECINSTR) &&
!address_taken && sh_size >= 4 && memcmp(buf, endbr, 4) == 0) {
memcpy(buf, nop, 4);
}
}
}
}
+31 -29
View File
@@ -10,9 +10,6 @@
namespace mold::elf {
template <typename E>
static thread_local MappedFile<Context<E>> *current_file;
template <typename E>
void read_version_script(Context<E> &ctx, std::span<std::string_view> &tok);
@@ -37,7 +34,7 @@ template <typename E>
class SyntaxError {
public:
SyntaxError(Context<E> &ctx, std::string_view errpos) : out(ctx) {
std::string_view contents = current_file<E>->get_contents();
std::string_view contents = ctx.script_file->get_contents();
std::string_view line = get_line(contents, errpos.data());
i64 lineno = 1;
@@ -48,7 +45,7 @@ public:
i64 column = errpos.data() - line.data();
std::stringstream ss;
ss << current_file<E>->name << ":" << lineno << ": ";
ss << ctx.script_file->name << ":" << lineno << ": ";
i64 indent = (i64)ss.tellp() + strlen("mold: ");
ss << line << "\n" << std::setw(indent + column) << " " << "^ ";
out << ss.str();
@@ -118,7 +115,7 @@ template <typename E>
static std::span<std::string_view>
skip(Context<E> &ctx, std::span<std::string_view> tok, std::string_view str) {
if (tok.empty())
Fatal(ctx) << current_file<E>->name << ": expected '" << str
Fatal(ctx) << ctx.script_file->name << ": expected '" << str
<< "', but got EOF";
if (tok[0] != str)
SyntaxError(ctx, tok[0]) << "expected '" << str << "'";
@@ -140,7 +137,7 @@ read_output_format(Context<E> &ctx, std::span<std::string_view> tok) {
while (!tok.empty() && tok[0] != ")")
tok = tok.subspan(1);
if (tok.empty())
Fatal(ctx) << current_file<E>->name << ": expected ')', but got EOF";
Fatal(ctx) << ctx.script_file->name << ": expected ')', but got EOF";
return tok.subspan(1);
}
@@ -158,7 +155,7 @@ static MappedFile<Context<E>> *resolve_path(Context<E> &ctx, std::string_view to
// GNU ld prepends the sysroot if a pathname starts with '/' and the
// script being processed is in the sysroot. We do the same.
if (str.starts_with('/') && is_in_sysroot(ctx, current_file<E>->name))
if (str.starts_with('/') && is_in_sysroot(ctx, ctx.script_file->name))
return MappedFile<Context<E>>::must_open(ctx, ctx.arg.sysroot + str);
if (str.starts_with('=')) {
@@ -205,13 +202,13 @@ read_group(Context<E> &ctx, std::span<std::string_view> tok) {
}
if (tok.empty())
Fatal(ctx) << current_file<E>->name << ": expected ')', but got EOF";
Fatal(ctx) << ctx.script_file->name << ": expected ')', but got EOF";
return tok.subspan(1);
}
template <typename E>
void parse_linker_script(Context<E> &ctx, MappedFile<Context<E>> *mf) {
current_file<E> = mf;
ctx.script_file = mf;
std::vector<std::string_view> vec = tokenize(ctx, mf->get_contents());
std::span<std::string_view> tok = vec;
@@ -241,7 +238,7 @@ void parse_linker_script(Context<E> &ctx, MappedFile<Context<E>> *mf) {
template <typename E>
std::string_view
get_script_output_type(Context<E> &ctx, MappedFile<Context<E>> *mf) {
current_file<E> = mf;
ctx.script_file = mf;
std::vector<std::string_view> vec = tokenize(ctx, mf->get_contents());
std::span<std::string_view> tok = vec;
@@ -312,12 +309,11 @@ read_version_script_commands(Context<E> &ctx, std::span<std::string_view> &tok,
if (tok[0] == "*") {
ctx.default_version = (is_global ? ver_idx : (u32)VER_NDX_LOCAL);
ctx.default_version_from_version_script = true;
} else if (is_global) {
ctx.version_patterns.push_back({unquote(tok[0]), current_file<E>->name,
ctx.version_patterns.push_back({unquote(tok[0]), ctx.script_file->name,
ver_str, ver_idx, is_cpp});
} else {
ctx.version_patterns.push_back({unquote(tok[0]), current_file<E>->name,
ctx.version_patterns.push_back({unquote(tok[0]), ctx.script_file->name,
ver_str, VER_NDX_LOCAL, is_cpp});
}
@@ -358,7 +354,7 @@ void read_version_script(Context<E> &ctx, std::span<std::string_view> &tok) {
template <typename E>
void parse_version_script(Context<E> &ctx, MappedFile<Context<E>> *mf) {
current_file<E> = mf;
ctx.script_file = mf;
std::vector<std::string_view> vec = tokenize(ctx, mf->get_contents());
std::span<std::string_view> tok = vec;
read_version_script(ctx, tok);
@@ -367,7 +363,9 @@ void parse_version_script(Context<E> &ctx, MappedFile<Context<E>> *mf) {
}
template <typename E>
void read_dynamic_list_commands(Context<E> &ctx, std::span<std::string_view> &tok,
void read_dynamic_list_commands(Context<E> &ctx,
std::vector<DynamicPattern> &result,
std::span<std::string_view> &tok,
bool is_cpp) {
while (!tok.empty() && tok[0] != "}") {
if (tok[0] == "extern") {
@@ -376,11 +374,11 @@ void read_dynamic_list_commands(Context<E> &ctx, std::span<std::string_view> &to
if (!tok.empty() && tok[0] == "\"C\"") {
tok = tok.subspan(1);
tok = skip(ctx, tok, "{");
read_dynamic_list_commands(ctx, tok, false);
read_dynamic_list_commands(ctx, result, tok, false);
} else {
tok = skip(ctx, tok, "\"C++\"");
tok = skip(ctx, tok, "{");
read_dynamic_list_commands(ctx, tok, true);
read_dynamic_list_commands(ctx, result, tok, true);
}
tok = skip(ctx, tok, "}");
@@ -388,29 +386,32 @@ void read_dynamic_list_commands(Context<E> &ctx, std::span<std::string_view> &to
continue;
}
if (tok[0] == "*")
ctx.default_version = VER_NDX_GLOBAL;
else
ctx.version_patterns.push_back({unquote(tok[0]), current_file<E>->name,
"global", VER_NDX_GLOBAL, is_cpp});
result.push_back({unquote(tok[0]), "", is_cpp});
tok = skip(ctx, tok.subspan(1), ";");
}
}
template <typename E>
void parse_dynamic_list(Context<E> &ctx, MappedFile<Context<E>> *mf) {
current_file<E> = mf;
std::vector<std::string_view> vec = tokenize(ctx, mf->get_contents());
std::vector<DynamicPattern>
parse_dynamic_list(Context<E> &ctx, std::string_view path) {
std::string_view contents =
MappedFile<Context<E>>::must_open(ctx, std::string(path))->get_contents();
std::vector<std::string_view> vec = tokenize(ctx, contents);
std::span<std::string_view> tok = vec;
std::vector<DynamicPattern> result;
tok = skip(ctx, tok, "{");
read_dynamic_list_commands(ctx, tok, false);
read_dynamic_list_commands(ctx, result, tok, false);
tok = skip(ctx, tok, "}");
tok = skip(ctx, tok, ";");
if (!tok.empty())
SyntaxError(ctx, tok[0]) << "trailing garbage token";
for (DynamicPattern &p : result)
p.source = path;
return result;
}
using E = MOLD_TARGET;
@@ -418,6 +419,7 @@ using E = MOLD_TARGET;
template void parse_linker_script(Context<E> &, MappedFile<Context<E>> *);
template std::string_view get_script_output_type(Context<E> &, MappedFile<Context<E>> *);
template void parse_version_script(Context<E> &, MappedFile<Context<E>> *);
template void parse_dynamic_list(Context<E> &, MappedFile<Context<E>> *);
template std::vector<DynamicPattern> parse_dynamic_list(Context<E> &, std::string_view);
} // namespace mold::elf
+3 -2
View File
@@ -709,8 +709,9 @@ std::vector<ObjectFile<E> *> do_lto(Context<E> &ctx) {
get_symbol(ctx, y)->referenced_by_regular_obj = true;
}
// Handle --entry
get_symbol(ctx, ctx.arg.entry)->referenced_by_regular_obj = true;
// Keep some symbols
for (Symbol<E> *sym : ctx.arg.undefined)
sym->referenced_by_regular_obj = true;
// all_symbols_read_hook() calls add_input_file() and add_input_library()
LOG << "all symbols read\n";
+11 -19
View File
@@ -299,22 +299,6 @@ static void read_input_files(Context<E> &ctx, std::span<std::string> args) {
if (!mf)
Fatal(ctx) << "--version-script: file not found: " << arg;
parse_version_script(ctx, mf);
} else if (remove_prefix(arg, "--dynamic-list=")) {
MappedFile<Context<E>> *mf = find_from_search_paths(ctx, std::string(arg));
if (!mf)
Fatal(ctx) << "--dynamic-list: file not found: " << arg;
parse_dynamic_list(ctx, mf);
} else if (remove_prefix(arg, "--export-dynamic-symbol=")) {
if (arg == "*")
ctx.default_version = VER_NDX_GLOBAL;
else
ctx.version_patterns.push_back({arg, "--export-dynamic-symbol",
"global", VER_NDX_GLOBAL, false});
} else if (remove_prefix(arg, "--export-dynamic-symbol-list=")) {
MappedFile<Context<E>> *mf = find_from_search_paths(ctx, std::string(arg));
if (!mf)
Fatal(ctx) << "--export-dynamic-symbol-list: file not found: " << arg;
parse_dynamic_list(ctx, mf);
} else if (arg == "--push-state") {
state.push_back({ctx.as_needed, ctx.whole_archive, ctx.is_static,
ctx.in_lib});
@@ -523,9 +507,9 @@ int elf_main(int argc, char **argv) {
write_repro_file(ctx);
// Handle --require-defined
for (std::string_view name : ctx.arg.require_defined)
if (!get_symbol(ctx, name)->file)
Error(ctx) << "--require-defined: undefined symbol: " << name;
for (Symbol<E> *sym : ctx.arg.require_defined)
if (!sym->file)
Error(ctx) << "--require-defined: undefined symbol: " << *sym;
// .init_array and .fini_array contents have to be sorted by
// a special rule. Sort them.
@@ -535,6 +519,11 @@ int elf_main(int argc, char **argv) {
// because they are superceded by .init_array/.fini_array, though.
sort_ctor_dtor(ctx);
// If .ctors/.dtors are to be placed to .init_array/.fini_array,
// we need to reverse their contents.
if (ctx.has_init_array && ctx.has_ctors)
fixup_ctors_in_init_array(ctx);
// Handle --shuffle-sections
if (ctx.arg.shuffle_sections != SHUFFLE_SECTIONS_NONE)
shuffle_sections(ctx);
@@ -642,6 +631,9 @@ int elf_main(int argc, char **argv) {
// Copy input sections to the output file and apply relocations.
copy_chunks(ctx);
if (ctx.arg.z_rewrite_endbr)
rewrite_endbr(ctx);
// Dynamic linker works better with sorted .rela.dyn section,
// so we sort them.
ctx.reldyn->sort(ctx);
+11
View File
@@ -132,3 +132,14 @@ int posix_spawn(pid_t *pid, const char *path,
typeof(posix_spawn) *real = dlsym(RTLD_NEXT, "posix_spawn");
return real(pid, path, file_actions, attrp, argv, envp);
}
int posix_spawnp(pid_t *pid, const char *file,
const posix_spawn_file_actions_t *file_actions,
const posix_spawnattr_t *attrp,
char *const *argv, char *const *envp) {
debug_print("posix_spawnp %s\n", file);
if (is_ld(file))
file = get_mold_path();
typeof(posix_spawnp) *real = dlsym(RTLD_NEXT, "posix_spawnp");
return real(pid, file, file_actions, attrp, argv, envp);
}
+41 -23
View File
@@ -95,12 +95,12 @@ struct SymbolAux<PPC64V1> : SymbolAux<X86_64> {
//
template <typename E>
class RangeExtensionThunk {};
class Thunk {};
template <needs_thunk E>
class RangeExtensionThunk<E> {
class Thunk<E> {
public:
RangeExtensionThunk(OutputSection<E> &osec, i64 offset)
Thunk(OutputSection<E> &osec, i64 offset)
: output_section(osec), offset(offset) {}
i64 size() const { return E::thunk_hdr_size + symbols.size() * E::thunk_size; }
@@ -119,7 +119,7 @@ public:
std::vector<Symbol<E> *> symbols;
};
struct RangeExtensionRef {
struct ThunkRef {
i16 thunk_idx = -1;
i16 sym_idx = -1;
};
@@ -212,7 +212,7 @@ struct InputSectionExtras {};
template <needs_thunk E>
struct InputSectionExtras<E> {
std::vector<RangeExtensionRef> range_extn;
std::vector<ThunkRef> thunk_refs;
};
template <is_riscv E>
@@ -462,7 +462,7 @@ public:
void create_range_extension_thunks(Context<E> &ctx);
std::vector<InputSection<E> *> members;
std::vector<std::unique_ptr<RangeExtensionThunk<E>>> thunks;
std::vector<std::unique_ptr<Thunk<E>>> thunks;
std::unique_ptr<RelocSection<E>> reloc_sec;
};
@@ -768,7 +768,7 @@ class MergedSection : public Chunk<E> {
public:
static MergedSection<E> *
get_instance(Context<E> &ctx, std::string_view name, i64 type, i64 flags,
i64 entsize);
i64 entsize, i64 addralign);
SectionFragment<E> *insert(Context<E> &ctx, std::string_view data,
u64 hash, i64 p2align);
@@ -1183,7 +1183,7 @@ public:
std::vector<FdeRecord<E>> fdes;
BitVector has_symver;
std::vector<ComdatGroupRef<E>> comdat_groups;
InputSection<E> *eh_frame_section = nullptr;
std::vector<InputSection<E> *> eh_frame_sections;
bool exclude_libs = false;
std::map<u32, u32> gnu_properties;
bool is_lto_obj = false;
@@ -1281,8 +1281,15 @@ get_script_output_type(Context<E> &ctx, MappedFile<Context<E>> *mf);
template <typename E>
void parse_version_script(Context<E> &ctx, MappedFile<Context<E>> *mf);
struct DynamicPattern {
std::string_view pattern;
std::string_view source;
bool is_cpp = false;
};
template <typename E>
void parse_dynamic_list(Context<E> &ctx, MappedFile<Context<E>> *mf);
std::vector<DynamicPattern>
parse_dynamic_list(Context<E> &ctx, std::string_view path);
//
// lto.cc
@@ -1372,6 +1379,7 @@ template <typename E> void check_duplicate_symbols(Context<E> &);
template <typename E> void check_symbol_types(Context<E> &);
template <typename E> void sort_init_fini(Context<E> &);
template <typename E> void sort_ctor_dtor(Context<E> &);
template <typename E> void fixup_ctors_in_init_array(Context<E> &);
template <typename E> void shuffle_sections(Context<E> &);
template <typename E> void compute_section_sizes(Context<E> &);
template <typename E> void sort_output_sections(Context<E> &);
@@ -1382,6 +1390,7 @@ template <typename E> void create_output_symtab(Context<E> &);
template <typename E> void report_undef_errors(Context<E> &);
template <typename E> void create_reloc_sections(Context<E> &);
template <typename E> void copy_chunks(Context<E> &);
template <typename E> void rewrite_endbr(Context<E> &);
template <typename E> void apply_version_script(Context<E> &);
template <typename E> void parse_symbol_version(Context<E> &);
template <typename E> void compute_import_export(Context<E> &);
@@ -1599,7 +1608,12 @@ struct ContextExtras<ALPHA> {
// resource management, and other miscellaneous objects.
template <typename E>
struct Context {
Context() = default;
Context() {
arg.entry = get_symbol(*this, "_start");
arg.fini = get_symbol(*this, "_fini");
arg.init = get_symbol(*this, "_init");
}
Context(const Context<E> &) = delete;
void checkpoint() {
@@ -1616,6 +1630,9 @@ struct Context {
CompressKind compress_debug_sections = COMPRESS_NONE;
SeparateCodeKind z_separate_code = NOSEPARATE_CODE;
ShuffleSectionsKind shuffle_sections = SHUFFLE_SECTIONS_NONE;
Symbol<E> *entry = nullptr;
Symbol<E> *fini = nullptr;
Symbol<E> *init = nullptr;
UnresolvedKind unresolved_symbols = UNRESOLVED_ERROR;
bool Bsymbolic = false;
bool Bsymbolic_functions = false;
@@ -1642,6 +1659,7 @@ struct Context {
bool ignore_data_address_equality = false;
bool is_static = false;
bool lto_pass2 = false;
bool nmagic = false;
bool noinhibit_exec = false;
bool oformat_binary = false;
bool omagic = false;
@@ -1692,6 +1710,7 @@ struct Context {
bool z_text = false;
i64 filler = -1;
i64 spare_dynamic_tags = 5;
i64 spare_program_headers = 0;
i64 thread_count = 0;
i64 z_stack_size = 0;
u64 shuffle_sections_seed;
@@ -1703,9 +1722,6 @@ struct Context {
std::string dependency_file;
std::string directory;
std::string dynamic_linker;
std::string entry = "_start";
std::string fini = "_fini";
std::string init = "_init";
std::string output = "a.out";
std::string package_metadata;
std::string plugin;
@@ -1718,6 +1734,8 @@ struct Context {
std::unordered_set<std::string_view> ignore_ir_file;
std::unordered_set<std::string_view> wrap;
std::vector<SectionOrder> section_order;
std::vector<Symbol<E> *> require_defined;
std::vector<Symbol<E> *> undefined;
std::vector<std::pair<Symbol<E> *, std::variant<Symbol<E> *, u64>>> defsyms;
std::vector<std::string> library_paths;
std::vector<std::string> plugin_opt;
@@ -1725,31 +1743,30 @@ struct Context {
std::vector<std::string_view> auxiliary;
std::vector<std::string_view> exclude_libs;
std::vector<std::string_view> filter;
std::vector<std::string_view> require_defined;
std::vector<std::string_view> trace_symbol;
std::vector<std::string_view> undefined;
u64 image_base = 0x200000;
} arg;
std::vector<VersionPattern> version_patterns;
u16 default_version = VER_NDX_GLOBAL;
std::vector<DynamicPattern> dynamic_list_patterns;
i64 default_version = VER_NDX_UNSPECIFIED;
i64 page_size = E::page_size;
std::optional<int> global_lock_fd;
// true if default_version is set by a wildcard in version script.
bool default_version_from_version_script = false;
// Reader context
bool as_needed = false;
bool whole_archive = false;
bool is_static;
bool in_lib = false;
i64 file_priority = 10000;
MappedFile<Context<E>> *script_file = nullptr;
std::unordered_set<std::string_view> visited;
tbb::task_group tg;
bool has_error = false;
bool has_lto_object = false;
Atomic<bool> has_init_array = false;
Atomic<bool> has_ctors = false;
// Symbol table
tbb::concurrent_hash_map<std::string_view, Symbol<E>, HashCmp> symbol_map;
@@ -2031,7 +2048,7 @@ public:
i32 sym_idx = -1;
i32 aux_idx = -1;
u16 ver_idx = 0;
u16 ver_idx = VER_NDX_UNSPECIFIED;
// `flags` has NEEDS_ flags.
Atomic<u8> flags = 0;
@@ -2152,6 +2169,9 @@ public:
// opposed to IR object).
bool referenced_by_regular_obj : 1 = false;
// For `-z rewrite-endbr`
bool address_taken : 1 = false;
// Target-dependent extra members.
[[no_unique_address]] SymbolExtras<E> extra;
};
@@ -2299,7 +2319,7 @@ InputSection<E>::get_fragment(Context<E> &ctx, const ElfRel<E> &rel) {
template <typename E>
u64 InputSection<E>::get_thunk_addr(i64 idx) {
if constexpr (needs_thunk<E>) {
RangeExtensionRef ref = extra.range_extn[idx];
ThunkRef ref = extra.thunk_refs[idx];
assert(ref.thunk_idx != -1);
return output_section->thunks[ref.thunk_idx]->get_addr(ref.sym_idx);
}
@@ -2397,8 +2417,6 @@ InputFile<E>::get_string(Context<E> &ctx, const ElfShdr<E> &shdr) {
template <typename E>
inline std::string_view InputFile<E>::get_string(Context<E> &ctx, i64 idx) {
assert(idx < elf_sections.size());
if (elf_sections.size() <= idx)
Fatal(ctx) << *this << ": invalid section index: " << idx;
return this->get_string(ctx, elf_sections[idx]);
+101 -86
View File
@@ -50,10 +50,9 @@ void OutputEhdr<E>::copy_buf(Context<E> &ctx) {
if (ctx.arg.relocatable)
return 0;
if (!ctx.arg.entry.empty())
if (Symbol<E> *sym = get_symbol(ctx, ctx.arg.entry);
sym->file && !sym->file->is_dso)
return sym->get_addr(ctx);
if (Symbol<E> &sym = *ctx.arg.entry;
sym.file && !sym.file->is_dso)
return sym.get_addr(ctx);
if (OutputSection<E> *osec = find_section(ctx, ".text"))
return osec->shdr.sh_addr;
@@ -149,11 +148,11 @@ template <typename E>
static std::vector<ElfPhdr<E>> create_phdr(Context<E> &ctx) {
std::vector<ElfPhdr<E>> vec;
auto define = [&](u64 type, u64 flags, i64 min_align, Chunk<E> *chunk) {
auto define = [&](u64 type, u64 flags, Chunk<E> *chunk) {
ElfPhdr<E> phdr = {};
phdr.p_type = type;
phdr.p_flags = flags;
phdr.p_align = std::max<u64>(min_align, chunk->shdr.sh_addralign);
phdr.p_align = chunk->shdr.sh_addralign;
phdr.p_offset = chunk->shdr.sh_offset;
if (chunk->shdr.sh_type != SHT_NOBITS)
@@ -176,8 +175,7 @@ static std::vector<ElfPhdr<E>> create_phdr(Context<E> &ctx) {
};
auto is_bss = [](Chunk<E> *chunk) {
return chunk->shdr.sh_type == SHT_NOBITS &&
!(chunk->shdr.sh_flags & SHF_TLS);
return chunk->shdr.sh_type == SHT_NOBITS;
};
auto is_tbss = [](Chunk<E> *chunk) {
@@ -186,86 +184,86 @@ static std::vector<ElfPhdr<E>> create_phdr(Context<E> &ctx) {
};
auto is_note = [](Chunk<E> *chunk) {
ElfShdr<E> &shdr = chunk->shdr;
return (shdr.sh_type == SHT_NOTE) && (shdr.sh_flags & SHF_ALLOC);
return chunk->shdr.sh_type == SHT_NOTE;
};
// When we are creating PT_LOAD segments, we consider only
// the following chunks.
std::vector<Chunk<E> *> chunks;
for (Chunk<E> *chunk : ctx.chunks)
if ((chunk->shdr.sh_flags & SHF_ALLOC) && !is_tbss(chunk))
chunks.push_back(chunk);
// The ELF spec says that "loadable segment entries in the program
// header table appear in ascending order, sorted on the p_vaddr
// member".
sort(chunks, [](Chunk<E> *a, Chunk<E> *b) {
return a->shdr.sh_addr < b->shdr.sh_addr;
});
// Create a PT_PHDR for the program header itself.
if (ctx.phdr && (ctx.phdr->shdr.sh_flags & SHF_ALLOC))
define(PT_PHDR, PF_R, sizeof(Word<E>), ctx.phdr);
define(PT_PHDR, PF_R, ctx.phdr);
// Create a PT_INTERP.
if (ctx.interp)
define(PT_INTERP, PF_R, 1, ctx.interp);
define(PT_INTERP, PF_R, ctx.interp);
// Create a PT_NOTE for SHF_NOTE sections.
for (i64 i = 0, end = ctx.chunks.size(); i < end;) {
Chunk<E> *first = ctx.chunks[i++];
if (!is_note(first))
continue;
for (i64 i = 0; i < chunks.size();) {
Chunk<E> *first = chunks[i++];
if (is_note(first)) {
i64 flags = to_phdr_flags(ctx, first);
define(PT_NOTE, flags, first);
i64 flags = to_phdr_flags(ctx, first);
i64 alignment = first->shdr.sh_addralign;
define(PT_NOTE, flags, alignment, first);
while (i < end && is_note(ctx.chunks[i]) &&
to_phdr_flags(ctx, ctx.chunks[i]) == flags)
append(ctx.chunks[i++]);
while (i < chunks.size() &&
is_note(ctx.chunks[i]) &&
to_phdr_flags(ctx, ctx.chunks[i]) == flags)
append(ctx.chunks[i++]);
}
}
// Create PT_LOAD segments.
{
i64 idx = vec.size();
std::vector<Chunk<E> *> chunks = ctx.chunks;
std::erase_if(chunks, is_tbss);
for (i64 i = 0; i < chunks.size();) {
Chunk<E> *first = chunks[i++];
i64 flags = to_phdr_flags(ctx, first);
define(PT_LOAD, flags, first);
vec.back().p_align = std::max<u64>(ctx.page_size, vec.back().p_align);
for (i64 i = 0, end = chunks.size(); i < end;) {
Chunk<E> *first = chunks[i++];
if (!(first->shdr.sh_flags & SHF_ALLOC))
continue;
i64 flags = to_phdr_flags(ctx, first);
define(PT_LOAD, flags, ctx.page_size, first);
// Add contiguous ALLOC sections as long as they have the same
// section flags and there's no on-disk gap in between.
if (!is_bss(first))
while (i < end && !is_bss(chunks[i]) &&
to_phdr_flags(ctx, chunks[i]) == flags &&
chunks[i]->shdr.sh_offset - first->shdr.sh_offset ==
chunks[i]->shdr.sh_addr - first->shdr.sh_addr)
append(chunks[i++]);
while (i < end && is_bss(chunks[i]) &&
to_phdr_flags(ctx, chunks[i]) == flags)
// Add contiguous ALLOC sections as long as they have the same
// section flags and there's no on-disk gap in between.
if (!is_bss(first))
while (i < chunks.size() &&
!is_bss(chunks[i]) &&
to_phdr_flags(ctx, chunks[i]) == flags &&
chunks[i]->shdr.sh_offset - first->shdr.sh_offset ==
chunks[i]->shdr.sh_addr - first->shdr.sh_addr)
append(chunks[i++]);
}
// The ELF spec says that "loadable segment entries in the program
// header table appear in ascending order, sorted on the p_vaddr
// member".
std::stable_sort(vec.begin() + idx, vec.end(),
[](const ElfPhdr<E> &a, const ElfPhdr<E> &b) {
return a.p_vaddr < b.p_vaddr;
});
while (i < chunks.size() &&
is_bss(chunks[i]) &&
to_phdr_flags(ctx, chunks[i]) == flags)
append(chunks[i++]);
}
// Create a PT_TLS.
for (i64 i = 0; i < ctx.chunks.size(); i++) {
if (ctx.chunks[i]->shdr.sh_flags & SHF_TLS) {
define(PT_TLS, PF_R, 1, ctx.chunks[i++]);
while (i < ctx.chunks.size() && (ctx.chunks[i]->shdr.sh_flags & SHF_TLS))
for (i64 i = 0; i < ctx.chunks.size();) {
Chunk<E> *first = ctx.chunks[i++];
if (first->shdr.sh_flags & SHF_TLS) {
define(PT_TLS, PF_R, first);
while (i < ctx.chunks.size() &&
(ctx.chunks[i]->shdr.sh_flags & SHF_TLS))
append(ctx.chunks[i++]);
}
}
// Add PT_DYNAMIC
if (ctx.dynamic && ctx.dynamic->shdr.sh_size)
define(PT_DYNAMIC, PF_R | PF_W, 1, ctx.dynamic);
define(PT_DYNAMIC, PF_R | PF_W, ctx.dynamic);
// Add PT_GNU_EH_FRAME
if (ctx.eh_frame_hdr)
define(PT_GNU_EH_FRAME, PF_R, 1, ctx.eh_frame_hdr);
define(PT_GNU_EH_FRAME, PF_R, ctx.eh_frame_hdr);
// Add PT_GNU_STACK, which is a marker segment that doesn't really
// contain any segments. It controls executable bit of stack area.
@@ -280,31 +278,31 @@ static std::vector<ElfPhdr<E>> create_phdr(Context<E> &ctx) {
// Create a PT_GNU_RELRO.
if (ctx.arg.z_relro) {
for (i64 i = 0; i < ctx.chunks.size(); i++) {
if (!ctx.chunks[i]->is_relro)
continue;
define(PT_GNU_RELRO, PF_R, 1, ctx.chunks[i++]);
while (i < ctx.chunks.size() && ctx.chunks[i]->is_relro)
append(ctx.chunks[i++]);
vec.back().p_align = 1;
for (i64 i = 0; i < chunks.size();) {
Chunk<E> *first = chunks[i++];
if (first->is_relro) {
define(PT_GNU_RELRO, PF_R, first);
while (i < chunks.size() && chunks[i]->is_relro)
append(chunks[i++]);
vec.back().p_align = 1;
}
}
}
// Create a PT_ARM_EDXIDX
if constexpr (is_arm32<E>)
if (OutputSection<E> *osec = find_section(ctx, SHT_ARM_EXIDX))
define(PT_ARM_EXIDX, PF_R, 4, osec);
define(PT_ARM_EXIDX, PF_R, osec);
// Create a PT_RISCV_ATTRIBUTES
if constexpr (is_riscv<E>)
if (ctx.extra.riscv_attributes->shdr.sh_size)
define(PT_RISCV_ATTRIBUTES, PF_R, 1, ctx.extra.riscv_attributes);
define(PT_RISCV_ATTRIBUTES, PF_R, ctx.extra.riscv_attributes);
// Create a PT_OPENBSD_RANDOMIZE
for (Chunk<E> *chunk : ctx.chunks)
if (chunk->name == ".openbsd.randomdata")
define(PT_OPENBSD_RANDOMIZE, PF_R | PF_W, 1, chunk);
define(PT_OPENBSD_RANDOMIZE, PF_R | PF_W, chunk);
// Set p_paddr if --physical-image-base was given. --physical-image-base
// is typically used in embedded programming to specify the base address
@@ -352,6 +350,7 @@ static std::vector<ElfPhdr<E>> create_phdr(Context<E> &ctx) {
}
}
vec.resize(vec.size() + ctx.arg.spare_program_headers);
return vec;
}
@@ -763,13 +762,13 @@ static std::vector<Word<E>> create_dynamic_section(Context<E> &ctx) {
define(DT_VERDEFNUM, ctx.verdef->shdr.sh_info);
}
if (Symbol<E> *sym = get_symbol(ctx, ctx.arg.init);
sym->file && !sym->file->is_dso)
define(DT_INIT, sym->get_addr(ctx));
if (Symbol<E> &sym = *ctx.arg.init;
sym.file && !sym.file->is_dso)
define(DT_INIT, sym.get_addr(ctx));
if (Symbol<E> *sym = get_symbol(ctx, ctx.arg.fini);
sym->file && !sym->file->is_dso)
define(DT_FINI, sym->get_addr(ctx));
if (Symbol<E> &sym = *ctx.arg.fini;
sym.file && !sym.file->is_dso)
define(DT_FINI, sym.get_addr(ctx));
if (ctx.hash)
define(DT_HASH, ctx.hash->shdr.sh_addr);
@@ -925,8 +924,7 @@ void OutputSection<E>::write_to(Context<E> &ctx, u8 *buf) {
});
if constexpr (needs_thunk<E>) {
tbb::parallel_for_each(thunks,
[&](std::unique_ptr<RangeExtensionThunk<E>> &thunk) {
tbb::parallel_for_each(thunks, [&](std::unique_ptr<Thunk<E>> &thunk) {
thunk->copy_buf(ctx);
});
}
@@ -1019,7 +1017,7 @@ void OutputSection<E>::compute_symtab_size(Context<E> &ctx) {
this->strtab_size = 0;
this->num_local_symtab = 0;
for (std::unique_ptr<RangeExtensionThunk<E>> &thunk : thunks) {
for (std::unique_ptr<Thunk<E>> &thunk : thunks) {
// For ARM32, we emit additional symbol "$t", "$a" and "$d" for
// each thunk to mark the beginning of Thumb code, ARM code and
// data, respectively.
@@ -1058,7 +1056,7 @@ void OutputSection<E>::populate_symtab(Context<E> &ctx) {
esym++;
};
for (std::unique_ptr<RangeExtensionThunk<E>> &thunk : thunks) {
for (std::unique_ptr<Thunk<E>> &thunk : thunks) {
for (i64 i = 0; i < thunk->symbols.size(); i++) {
Symbol<E> &sym = *thunk->symbols[i];
u64 addr = thunk->get_addr(i);
@@ -1912,11 +1910,23 @@ void GnuHashSection<E>::copy_buf(Context<E> &ctx) {
template <typename E>
std::string_view
get_merged_output_name(Context<E> &ctx, std::string_view name, u64 flags) {
get_merged_output_name(Context<E> &ctx, std::string_view name, u64 flags,
i64 entsize, i64 addralign) {
if (ctx.arg.relocatable && !ctx.arg.relocatable_merge_sections)
return name;
if (ctx.arg.unique && ctx.arg.unique->match(name))
return name;
// GCC seems to create sections named ".rodata.strN.<mangled-symbol-name>.M".
// We want to eliminate the symbol name part from the section name.
if ((flags & SHF_STRINGS) && name.starts_with(".rodata.")) {
std::string name2 = ".rodata.str"s + std::to_string(entsize) +
"." + std::to_string(addralign);
if (name == name2)
return name;
return save_string(ctx, name2);
}
return name;
}
@@ -1932,8 +1942,9 @@ MergedSection<E>::MergedSection(std::string_view name, i64 flags, i64 type,
template <typename E>
MergedSection<E> *
MergedSection<E>::get_instance(Context<E> &ctx, std::string_view name,
i64 type, i64 flags, i64 entsize) {
name = get_merged_output_name(ctx, name, flags);
i64 type, i64 flags,
i64 entsize, i64 addralign) {
name = get_merged_output_name(ctx, name, flags, entsize, addralign);
flags = flags & ~(u64)SHF_GROUP & ~(u64)SHF_COMPRESSED;
auto find = [&]() -> MergedSection * {
@@ -2534,8 +2545,12 @@ void VerdefSection<E>::construct(Context<E> &ctx) {
for (std::string_view verstr : ctx.arg.version_definitions)
write(verstr, idx++, 0);
for (Symbol<E> *sym : std::span<Symbol<E> *>(ctx.dynsym->symbols).subspan(1))
ctx.versym->contents[sym->get_dynsym_idx(ctx)] = sym->ver_idx;
for (Symbol<E> *sym : std::span<Symbol<E> *>(ctx.dynsym->symbols).subspan(1)) {
i64 ver = sym->ver_idx;
if (ver == VER_NDX_UNSPECIFIED)
ver = VER_NDX_GLOBAL;
ctx.versym->contents[sym->get_dynsym_idx(ctx)] = ver;
}
}
template <typename E>
+336 -173
View File
@@ -189,15 +189,13 @@ void create_synthetic_sections(Context<E> &ctx) {
template <typename E>
static void mark_live_objects(Context<E> &ctx) {
auto mark_symbol = [&](std::string_view name) {
if (InputFile<E> *file = get_symbol(ctx, name)->file)
file->is_alive = true;
};
for (Symbol<E> *sym : ctx.arg.undefined)
if (sym->file)
sym->file->is_alive = true;
for (std::string_view name : ctx.arg.undefined)
mark_symbol(name);
for (std::string_view name : ctx.arg.require_defined)
mark_symbol(name);
for (Symbol<E> *sym : ctx.arg.require_defined)
if (sym->file)
sym->file->is_alive = true;
std::vector<InputFile<E> *> roots;
@@ -358,8 +356,8 @@ void kill_eh_frame_sections(Context<E> &ctx) {
Timer t(ctx, "kill_eh_frame_sections");
for (ObjectFile<E> *file : ctx.objs)
if (file->eh_frame_section)
file->eh_frame_section->is_alive = false;
for (InputSection<E> *sec : file->eh_frame_sections)
sec->is_alive = false;
}
template <typename E>
@@ -397,7 +395,7 @@ template <typename E>
void add_comment_string(Context<E> &ctx, std::string str) {
MergedSection<E> *sec =
MergedSection<E>::get_instance(ctx, ".comment", SHT_PROGBITS,
SHF_MERGE | SHF_STRINGS, 1);
SHF_MERGE | SHF_STRINGS, 1, 1);
std::string_view buf = save_string(ctx, str);
std::string_view data(buf.data(), buf.size() + 1);
@@ -510,9 +508,31 @@ get_output_name(Context<E> &ctx, std::string_view name, u64 flags) {
return name;
}
// Returns true if a given input section is a .ctors/.dtors that
// should be put into .init_array/.fini_array.
//
// CRT object files contain .ctors/.dtors sections without any
// relocations. They contain sentinel values, 0 and -1, to mark the
// beginning and the end of the initializer/finalizer pointer arrays. We
// do not place them into .init_array/.fini_array because such invalid
// pointer values would simply make the program to crash.
template <typename E>
static bool is_ctors_in_init_array(Context<E> &ctx, InputSection<E> &isec) {
std::string_view name = isec.name();
return ctx.has_init_array && !isec.get_rels(ctx).empty() &&
(name == ".ctors" || name.starts_with(".ctors.") ||
name == ".dtors" || name.starts_with(".dtors."));
}
template <typename E>
static OutputSectionKey
get_output_section_key(Context<E> &ctx, InputSection<E> &isec) {
if (is_ctors_in_init_array(ctx, isec)) {
if (isec.name().starts_with(".ctors"))
return {".init_array", SHT_INIT_ARRAY, SHF_ALLOC | SHF_WRITE};
return {".fini_array", SHT_FINI_ARRAY, SHF_ALLOC | SHF_WRITE};
}
const ElfShdr<E> &shdr = isec.shdr();
std::string_view name = get_output_name(ctx, isec.name(), shdr.sh_flags);
u64 type = canonicalize_type<E>(name, shdr.sh_type);
@@ -523,6 +543,7 @@ get_output_section_key(Context<E> &ctx, InputSection<E> &isec) {
// So is .fini_array.
if (type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY)
flags |= SHF_WRITE;
return {name, type, flags};
}
@@ -661,34 +682,9 @@ void create_internal_file(Context<E> &ctx) {
ctx.internal_esyms.push_back(esym);
};
auto add_undef = [&](Symbol<E> *sym) {
obj->symbols.push_back(sym);
sym->value = 0xdeadbeef;
ElfSym<E> esym;
memset(&esym, 0, sizeof(esym));
esym.st_type = STT_NOTYPE;
esym.st_shndx = SHN_UNDEF;
esym.st_bind = STB_GLOBAL;
esym.st_visibility = STV_DEFAULT;
ctx.internal_esyms.push_back(esym);
};
// Add --defsym symbols
for (i64 i = 0; i < ctx.arg.defsyms.size(); i++) {
Symbol<E> *sym = ctx.arg.defsyms[i].first;
std::variant<Symbol<E> *, u64> val = ctx.arg.defsyms[i].second;
add(sym);
if (Symbol<E> **ref = std::get_if<Symbol<E> *>(&val)) {
// Add an undefined symbol to keep a reference to the defsym target.
// This prevents elimination by e.g. LTO or gc-sections.
// The undefined symbol will never make to the final object file; we
// double-check that the defsym target is not undefined in
// fix_synthetic_symbols.
add_undef(*ref);
}
}
// Add --defsym'd symbols
for (i64 i = 0; i < ctx.arg.defsyms.size(); i++)
add(ctx.arg.defsyms[i].first);
// Add --section-order symbols
for (SectionOrder &ord : ctx.arg.section_order)
@@ -821,26 +817,27 @@ void add_synthetic_symbols(Context<E> &ctx) {
// Handle --defsym symbols.
for (i64 i = 0; i < ctx.arg.defsyms.size(); i++) {
Symbol<E> *sym = ctx.arg.defsyms[i].first;
Symbol<E> *sym1 = ctx.arg.defsyms[i].first;
std::variant<Symbol<E> *, u64> val = ctx.arg.defsyms[i].second;
Symbol<E> *target = nullptr;
if (Symbol<E> **ref = std::get_if<Symbol<E> *>(&val))
target = *ref;
if (Symbol<E> **ref = std::get_if<Symbol<E> *>(&val)) {
Symbol<E> *sym2 = *ref;
if (!sym2->file) {
Error(ctx) << "--defsym: undefined symbol: " << *sym2;
continue;
}
// If the alias refers another symobl, copy ELF symbol attributes.
if (target) {
ElfSym<E> &esym = obj.elf_syms[i + 1];
esym.st_type = target->esym().st_type;
esym.st_type = sym2->esym().st_type;
if constexpr (is_ppc64v2<E>)
esym.ppc_local_entry = target->esym().ppc_local_entry;
esym.ppc_local_entry = sym2->esym().ppc_local_entry;
if (sym2->is_absolute())
sym1->origin = 0;
} else {
sym1->origin = 0;
}
// Make the target absolute if necessary.
if (!target || target->is_absolute())
sym->origin = 0;
}
}
template <typename E>
@@ -1047,19 +1044,42 @@ void check_symbol_types(Context<E> &ctx) {
});
}
template <typename E>
static i64 get_init_fini_priority(InputSection<E> *isec) {
static std::regex re(R"(\.(\d+)$)", std::regex_constants::optimize);
std::string_view name = isec->name();
std::cmatch m;
if (std::regex_search(name.data(), name.data() + name.size(), m, re))
return std::stoi(m[1]);
return 65536;
}
template <typename E>
static i64 get_ctor_dtor_priority(InputSection<E> *isec) {
auto opts = std::regex_constants::optimize | std::regex_constants::ECMAScript;
static std::regex re1(R"((?:clang_rt\.)?crtbegin)", opts);
static std::regex re2(R"((?:clang_rt\.)?crtend)", opts);
static std::regex re3(R"(\.(\d+)$)", opts);
// crtbegin.o and crtend.o contain marker symbols such as
// __CTOR_LIST__ or __DTOR_LIST__. So they have to be at the
// beginning or end of the section.
std::smatch m;
if (std::regex_search(isec->file.filename, m, re1))
return -2;
if (std::regex_search(isec->file.filename, m, re2))
return 65536;
std::string name(isec->name());
if (std::regex_search(name, m, re3))
return std::stoi(m[1]);
return -1;
}
template <typename E>
void sort_init_fini(Context<E> &ctx) {
Timer t(ctx, "sort_init_fini");
auto get_priority = [](InputSection<E> *isec) {
static std::regex re(R"(\.(\d+)$)", std::regex_constants::optimize);
std::string_view name = isec->name();
std::cmatch m;
if (std::regex_search(name.data(), name.data() + name.size(), m, re))
return std::stoi(m[1]);
return 65536;
};
for (Chunk<E> *chunk : ctx.chunks) {
if (OutputSection<E> *osec = chunk->to_osec()) {
if (osec->name == ".init_array" || osec->name == ".preinit_array" ||
@@ -1068,8 +1088,14 @@ void sort_init_fini(Context<E> &ctx) {
std::reverse(osec->members.begin(), osec->members.end());
std::unordered_map<InputSection<E> *, i64> map;
for (InputSection<E> *isec : osec->members)
map.insert({isec, get_priority(isec)});
for (InputSection<E> *isec : osec->members) {
std::string_view name = isec->name();
if (name.starts_with(".ctors") || name.starts_with(".dtors"))
map.insert({isec, 65535 - get_ctor_dtor_priority(isec)});
else
map.insert({isec, get_init_fini_priority(isec)});
}
sort(osec->members, [&](InputSection<E> *a, InputSection<E> *b) {
return map[a] < map[b];
@@ -1083,27 +1109,6 @@ template <typename E>
void sort_ctor_dtor(Context<E> &ctx) {
Timer t(ctx, "sort_ctor_dtor");
auto get_priority = [](InputSection<E> *isec) {
auto opts = std::regex_constants::optimize | std::regex_constants::ECMAScript;
static std::regex re1(R"((?:clang_rt\.)?crtbegin)", opts);
static std::regex re2(R"((?:clang_rt\.)?crtend)", opts);
static std::regex re3(R"(\.(\d+)$)", opts);
// crtbegin.o and crtend.o contain marker symbols such as
// __CTOR_LIST__ or __DTOR_LIST__. So they have to be at the
// beginning or end of the section.
std::smatch m;
if (std::regex_search(isec->file.filename, m, re1))
return -2;
if (std::regex_search(isec->file.filename, m, re2))
return 65536;
std::string name(isec->name());
if (std::regex_search(name, m, re3))
return std::stoi(m[1]);
return -1;
};
for (Chunk<E> *chunk : ctx.chunks) {
if (OutputSection<E> *osec = chunk->to_osec()) {
if (osec->name == ".ctors" || osec->name == ".dtors") {
@@ -1112,7 +1117,7 @@ void sort_ctor_dtor(Context<E> &ctx) {
std::unordered_map<InputSection<E> *, i64> map;
for (InputSection<E> *isec : osec->members)
map.insert({isec, get_priority(isec)});
map.insert({isec, get_ctor_dtor_priority(isec)});
sort(osec->members, [&](InputSection<E> *a, InputSection<E> *b) {
return map[a] < map[b];
@@ -1122,6 +1127,45 @@ void sort_ctor_dtor(Context<E> &ctx) {
}
}
// .ctors/.dtors serves the same purpose as .init_array/.fini_array,
// albeit with very subtly differences. Both contain pointers to
// initializer/finalizer functions. The runtime executes them one by one
// but in the exact opposite order to one another. Therefore, if we are to
// place the contents of .ctors/.dtors into .init_array/.fini_array, we
// need to reverse them.
//
// It's unfortunate that we have both .ctors/.dtors and
// .init_array/.fini_array in ELF for historical reasons, but that's
// the reality we need to deal with.
template <typename E>
void fixup_ctors_in_init_array(Context<E> &ctx) {
Timer t(ctx, "fixup_ctors_in_init_array");
for (Chunk<E> *chunk : ctx.chunks) {
if (OutputSection<E> *osec = chunk->to_osec()) {
if (osec->name == ".init_array" || osec->name == ".fini_array") {
for (InputSection<E> *isec : osec->members) {
if (isec->name().starts_with(".ctors") ||
isec->name().starts_with(".dtors")) {
if (isec->sh_size % sizeof(Word<E>)) {
Error(ctx) << *isec << ": section corrupted";
continue;
}
u8 *buf = (u8 *)isec->contents.data();
std::reverse((Word<E> *)buf, (Word<E> *)(buf + isec->sh_size));
std::span<ElfRel<E>> rels = isec->get_rels(ctx);
for (ElfRel<E> &r : rels)
r.r_offset = isec->sh_size - r.r_offset - sizeof(Word<E>);
std::reverse(rels.begin(), rels.end());
}
}
}
}
}
}
template <typename T>
static void shuffle(std::vector<T> &vec, u64 seed) {
if (vec.empty())
@@ -1503,12 +1547,12 @@ void copy_chunks(Context<E> &ctx) {
// sections first. This is because REL-type relocation sections (as
// opposed to RELA-type) stores relocation addends to target sections.
tbb::parallel_for_each(ctx.chunks, [&](Chunk<E> *chunk) {
if (chunk->shdr.sh_type != (E::is_rela ? SHT_RELA : SHT_REL))
if (chunk->shdr.sh_type != SHT_REL)
copy(*chunk);
});
tbb::parallel_for_each(ctx.chunks, [&](Chunk<E> *chunk) {
if (chunk->shdr.sh_type == (E::is_rela ? SHT_RELA : SHT_REL))
if (chunk->shdr.sh_type == SHT_REL)
copy(*chunk);
});
@@ -1522,6 +1566,54 @@ void copy_chunks(Context<E> &ctx) {
fixup_arm_exidx_section(ctx);
}
// Rewrite the leading endbr64 instruction with a nop if a function
// symbol's address was not taken.
template <typename E>
void rewrite_endbr(Context<E> &ctx) {
Timer t(ctx, "rewrite_endbr");
assert(is_x86_64<E>);
// Compute address-taken bit for each symbol
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
for (std::unique_ptr<InputSection<E>> &isec : file->sections) {
if (isec && isec->is_alive && (isec->shdr().sh_flags & SHF_ALLOC)) {
for (const ElfRel<E> &rel : isec->get_rels(ctx)) {
Symbol<E> &sym = *file->symbols[rel.r_sym];
if (!is_func_call_rel(rel) && sym.esym().st_type == STT_FUNC) {
std::scoped_lock lock(sym.mu);
sym.address_taken = true;
}
}
}
}
});
// Some symbols are implicitly address-taken
ctx.arg.entry->address_taken = true;
ctx.arg.init->address_taken = true;
ctx.arg.fini->address_taken = true;
// Rewrite endbr64 with nop
u8 endbr64[] = {0xf3, 0x0f, 0x1e, 0xfa};
u8 nop[] = {0x0f, 0x1f, 0x40, 0x00};
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
for (Symbol<E> *sym : file->symbols) {
if (sym->file == file && sym->esym().st_type == STT_FUNC &&
!sym->address_taken) {
if (InputSection<E> *isec = sym->get_input_section()) {
if (OutputSection<E> *osec = isec->output_section) {
u8 *buf = ctx.buf + osec->shdr.sh_offset + isec->offset +
sym->value;
if (memcmp(buf, endbr64, 4) == 0)
memcpy(buf, nop, 4);
}
}
}
}
});
}
template <typename E>
void construct_relr(Context<E> &ctx) {
Timer t(ctx, "construct_relr");
@@ -1554,18 +1646,54 @@ template <typename E>
void apply_version_script(Context<E> &ctx) {
Timer t(ctx, "apply_version_script");
// If all patterns are simple (i.e. not containing any meta-
// characters and is not a C++ name), we can simply look up
// symbols.
auto is_simple = [&] {
for (VersionPattern &v : ctx.version_patterns)
if (v.is_cpp || v.pattern.find_first_of("*?[") != v.pattern.npos)
return false;
return true;
};
// Assign versions to symbols specified with `extern "C++"` or
// wildcard patterns first.
MultiGlob matcher;
MultiGlob cpp_matcher;
if (is_simple()) {
for (VersionPattern &v : ctx.version_patterns) {
for (i64 i = 0; i < ctx.version_patterns.size(); i++) {
VersionPattern &v = ctx.version_patterns[i];
if (v.is_cpp) {
if (!cpp_matcher.add(v.pattern, i))
Fatal(ctx) << "invalid version pattern: " << v.pattern;
} else if (v.pattern.find_first_of("*?[") != v.pattern.npos) {
if (!matcher.add(v.pattern, i))
Fatal(ctx) << "invalid version pattern: " << v.pattern;
}
}
if (!matcher.empty() || !cpp_matcher.empty()) {
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
for (Symbol<E> *sym : file->get_global_syms()) {
if (sym->file != file)
continue;
std::string_view name = sym->name();
i64 match = INT64_MAX;
if (std::optional<u32> idx = matcher.find(name))
match = std::min<i64>(match, *idx);
// Match non-mangled symbols against the C++ pattern as well.
// Weird, but required to match other linkers' behavior.
if (!cpp_matcher.empty()) {
if (std::optional<std::string_view> s = cpp_demangle(name))
name = *s;
if (std::optional<u32> idx = cpp_matcher.find(name))
match = std::min<i64>(match, *idx);
}
if (match != INT64_MAX)
sym->ver_idx = ctx.version_patterns[match].ver_idx;
}
});
}
// Next, assign versions to symbols specified by exact name.
// In other words, exact matches have higher precedence over
// wildcard or `extern "C++"` patterns.
for (VersionPattern &v : ctx.version_patterns) {
if (!v.is_cpp && v.pattern.find_first_of("*?[") == v.pattern.npos) {
Symbol<E> *sym = get_symbol(ctx, v.pattern);
if (!sym->file && !ctx.arg.undefined_version)
@@ -1575,48 +1703,7 @@ void apply_version_script(Context<E> &ctx) {
if (sym->file && !sym->file->is_dso)
sym->ver_idx = v.ver_idx;
}
return;
}
// Otherwise, use glob pattern matchers.
MultiGlob matcher;
MultiGlob cpp_matcher;
for (i64 i = 0; i < ctx.version_patterns.size(); i++) {
VersionPattern &v = ctx.version_patterns[i];
if (v.is_cpp) {
if (!cpp_matcher.add(v.pattern, i))
Fatal(ctx) << "invalid version pattern: " << v.pattern;
} else {
if (!matcher.add(v.pattern, i))
Fatal(ctx) << "invalid version pattern: " << v.pattern;
}
}
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
for (Symbol<E> *sym : file->get_global_syms()) {
if (sym->file != file)
continue;
std::string_view name = sym->name();
i64 match = INT64_MAX;
if (std::optional<u32> idx = matcher.find(name))
match = std::min<i64>(match, *idx);
// Match non-mangled symbols against the C++ pattern as well.
// Weird, but required to match other linkers' behavior.
if (!cpp_matcher.empty()) {
if (std::optional<std::string_view> s = cpp_demangle(name))
name = *s;
if (std::optional<u32> idx = cpp_matcher.find(name))
match = std::min<i64>(match, *idx);
}
if (match != INT64_MAX)
sym->ver_idx = ctx.version_patterns[match].ver_idx;
}
});
}
template <typename E>
@@ -1689,44 +1776,124 @@ void compute_import_export(Context<E> &ctx) {
if (!ctx.arg.shared) {
tbb::parallel_for_each(ctx.dsos, [&](SharedFile<E> *file) {
for (Symbol<E> *sym : file->symbols) {
if (sym->file && !sym->file->is_dso && sym->visibility != STV_HIDDEN) {
if (sym->ver_idx != VER_NDX_LOCAL ||
!ctx.default_version_from_version_script) {
std::scoped_lock lock(sym->mu);
sym->is_exported = true;
}
if (sym->file && !sym->file->is_dso && sym->visibility != STV_HIDDEN &&
sym->ver_idx != VER_NDX_LOCAL) {
std::scoped_lock lock(sym->mu);
sym->is_exported = true;
}
}
});
}
auto should_export = [&](Symbol<E> &sym) {
if (sym.visibility == STV_HIDDEN)
return false;
switch (sym.ver_idx) {
case VER_NDX_UNSPECIFIED:
if (ctx.arg.shared)
return !((ObjectFile<E> *)sym.file)->exclude_libs;
return ctx.arg.export_dynamic;
case VER_NDX_LOCAL:
return false;
default:
return true;
}
};
// Export symbols that are not hidden or marked as local.
// We also want to mark imported symbols as such.
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
for (Symbol<E> *sym : file->get_global_syms()) {
if (!sym->file || sym->visibility == STV_HIDDEN ||
sym->ver_idx == VER_NDX_LOCAL)
continue;
// If we are using a symbol in a DSO, we need to import it at runtime.
if (sym->file != file && sym->file->is_dso && !sym->is_absolute()) {
std::scoped_lock lock(sym->mu);
sym->is_imported = true;
// If we are using a symbol in a DSO, we need to import it.
if (sym->file && sym->file->is_dso) {
if (!sym->is_absolute()) {
std::scoped_lock lock(sym->mu);
sym->is_imported = true;
}
continue;
}
// If we are creating a DSO, all global symbols are exported by default.
if (sym->file == file) {
std::scoped_lock lock(sym->mu);
// If we have a definition of a symbol, we may want to export it.
if (sym->file == file && should_export(*sym)) {
sym->is_exported = true;
if (ctx.arg.shared && sym->visibility != STV_PROTECTED &&
// Exported symbols are marked as imported as well by default
// for DSOs.
if (ctx.arg.shared &&
sym->visibility != STV_PROTECTED &&
!ctx.arg.Bsymbolic &&
!(ctx.arg.Bsymbolic_functions && sym->get_type() == STT_FUNC))
sym->is_imported = true;
}
}
});
// Apply --dynamic-list, --export-dynamic-symbol and
// --export-dynamic-symbol-list options.
//
// The semantics of these options vary depending on whether we are
// creating an executalbe or a shared object.
//
// For executable, matched symbols are exported.
//
// For shared objects, matched symbols are imported if it is already
// exported so that they are interposable. In other words, symbols
// that did not match will be bound locally within the output file,
// effectively turning them into protected symbols.
MultiGlob matcher;
MultiGlob cpp_matcher;
auto handle_match = [&](Symbol<E> *sym) {
if (ctx.arg.shared) {
if (sym->is_exported)
sym->is_imported = true;
} else {
if (sym->file && !sym->file->is_dso && sym->visibility != STV_HIDDEN)
sym->is_exported = true;
}
};
for (DynamicPattern &p : ctx.dynamic_list_patterns) {
if (p.is_cpp) {
if (!cpp_matcher.add(p.pattern, 1))
Fatal(ctx) << p.source << ": invalid dynamic list entry: "
<< p.pattern;
continue;
}
if (p.pattern.find_first_of("*?[") != p.pattern.npos) {
if (!matcher.add(p.pattern, 1))
Fatal(ctx) << p.source << ": invalid dynamic list entry: "
<< p.pattern;
continue;
}
handle_match(get_symbol(ctx, p.pattern));
}
if (!matcher.empty() || !cpp_matcher.empty()) {
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
for (Symbol<E> *sym : file->get_global_syms()) {
if (sym->file != file)
continue;
if (ctx.arg.shared && !sym->is_exported)
continue;
std::string_view name = sym->name();
if (matcher.find(name)) {
handle_match(sym);
} else if (!cpp_matcher.empty()) {
if (std::optional<std::string_view> s = cpp_demangle(name))
name = *s;
if (cpp_matcher.find(name))
handle_match(sym);
}
}
});
}
}
// Compute the "address-taken" bit for each input section.
@@ -1783,9 +1950,9 @@ void compute_address_significance(Context<E> &ctx) {
};
// Some symbols' pointer values are leaked to the dynamic section.
mark(get_symbol(ctx, ctx.arg.entry));
mark(get_symbol(ctx, ctx.arg.init));
mark(get_symbol(ctx, ctx.arg.fini));
mark(ctx.arg.entry);
mark(ctx.arg.init);
mark(ctx.arg.fini);
// Exported symbols are conservatively considered address-taken.
if (ctx.dynsym)
@@ -2133,7 +2300,7 @@ static void set_virtual_addresses_regular(Context<E> &ctx) {
i64 flags1 = get_flags(chunks[i - 1]);
i64 flags2 = get_flags(chunks[i]);
if (flags1 != flags2) {
if (!ctx.arg.nmagic && flags1 != flags2) {
switch (ctx.arg.z_separate_code) {
case SEPARATE_LOADABLE_SEGMENTS:
addr = align_to(addr, ctx.page_size);
@@ -2592,18 +2759,12 @@ void fix_synthetic_symbols(Context<E> &ctx) {
if (u64 *addr = std::get_if<u64>(&val)) {
sym->origin = 0;
sym->value = *addr;
continue;
} else {
Symbol<E> *sym2 = std::get<Symbol<E> *>(val);
sym->value = sym2->value;
sym->origin = sym2->origin;
sym->visibility = sym2->visibility.load();
}
Symbol<E> *sym2 = std::get<Symbol<E> *>(val);
if (!sym2->file) {
Error(ctx) << "--defsym: undefined symbol: " << *sym2;
continue;
}
sym->value = sym2->value;
sym->origin = sym2->origin;
sym->visibility = sym2->visibility.load();
}
@@ -2726,7 +2887,7 @@ void show_stats(Context<E> &ctx) {
static Counter thunk_bytes("thunk_bytes");
for (Chunk<E> *chunk : ctx.chunks)
if (OutputSection<E> *osec = chunk->to_osec())
for (std::unique_ptr<RangeExtensionThunk<E>> &thunk : osec->thunks)
for (std::unique_ptr<Thunk<E>> &thunk : osec->thunks)
thunk_bytes += thunk->size();
}
@@ -2756,6 +2917,7 @@ template void check_duplicate_symbols(Context<E> &);
template void check_symbol_types(Context<E> &);
template void sort_init_fini(Context<E> &);
template void sort_ctor_dtor(Context<E> &);
template void fixup_ctors_in_init_array(Context<E> &);
template void shuffle_sections(Context<E> &);
template void compute_section_sizes(Context<E> &);
template void sort_output_sections(Context<E> &);
@@ -2764,6 +2926,7 @@ template void scan_relocations(Context<E> &);
template void report_undef_errors(Context<E> &);
template void create_reloc_sections(Context<E> &);
template void copy_chunks(Context<E> &);
template void rewrite_endbr(Context<E> &);
template void construct_relr(Context<E> &);
template void create_output_symtab(Context<E> &);
template void apply_version_script(Context<E> &);
+15 -18
View File
@@ -119,7 +119,7 @@ static bool is_reachable(Context<E> &ctx, InputSection<E> &isec,
return -max_distance() <= val && val < max_distance();
}
static void reset_thunk(RangeExtensionThunk<E> &thunk) {
static void reset_thunk(Thunk<E> &thunk) {
for (Symbol<E> *sym : thunk.symbols) {
sym->extra.thunk_idx = -1;
sym->extra.thunk_sym_idx = -1;
@@ -129,10 +129,10 @@ static void reset_thunk(RangeExtensionThunk<E> &thunk) {
// Scan relocations to collect symbols that need thunks.
static void scan_rels(Context<E> &ctx, InputSection<E> &isec,
RangeExtensionThunk<E> &thunk, i64 thunk_idx) {
Thunk<E> &thunk, i64 thunk_idx) {
std::span<const ElfRel<E>> rels = isec.get_rels(ctx);
std::vector<RangeExtensionRef> &range_extn = isec.extra.range_extn;
range_extn.resize(rels.size());
std::vector<ThunkRef> &thunk_refs = isec.extra.thunk_refs;
thunk_refs.resize(rels.size());
for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -151,15 +151,15 @@ static void scan_rels(Context<E> &ctx, InputSection<E> &isec,
// This relocation needs a thunk. If the symbol is already in a
// previous thunk, reuse it.
if (sym.extra.thunk_idx != -1) {
range_extn[i].thunk_idx = sym.extra.thunk_idx;
range_extn[i].sym_idx = sym.extra.thunk_sym_idx;
thunk_refs[i].thunk_idx = sym.extra.thunk_idx;
thunk_refs[i].sym_idx = sym.extra.thunk_sym_idx;
continue;
}
// Otherwise, add the symbol to the current thunk if it's not
// added already.
range_extn[i].thunk_idx = thunk_idx;
range_extn[i].sym_idx = -1;
thunk_refs[i].thunk_idx = thunk_idx;
thunk_refs[i].sym_idx = -1;
if (sym.flags.exchange(-1) == 0) {
std::scoped_lock lock(thunk.mu);
@@ -170,8 +170,6 @@ static void scan_rels(Context<E> &ctx, InputSection<E> &isec,
template <>
void OutputSection<E>::create_range_extension_thunks(Context<E> &ctx) {
using Thunk = RangeExtensionThunk<E>;
std::span<InputSection<E> *> m = members;
if (m.empty())
return;
@@ -179,9 +177,8 @@ void OutputSection<E>::create_range_extension_thunks(Context<E> &ctx) {
// Initialize input sections with a dummy offset so that we can
// distinguish sections that have got an address with the one who
// haven't.
tbb::parallel_for_each(m, [](InputSection<E> *isec) {
for (InputSection<E> *isec : m)
isec->offset = -1;
});
// We create thunks from the beginning of the section to the end.
// We manage progress using four offsets which increase monotonically.
@@ -215,7 +212,7 @@ void OutputSection<E>::create_range_extension_thunks(Context<E> &ctx) {
// Move D foward as far as we can jump from B to a thunk at D.
auto d_thunk_end = [&] {
u64 d_end = align_to(offset, 1 << m[d]->p2align) + m[d]->sh_size;
return align_to(d_end, Thunk::alignment) + max_thunk_size;
return align_to(d_end, Thunk<E>::alignment) + max_thunk_size;
};
while (d < m.size() &&
@@ -243,9 +240,9 @@ void OutputSection<E>::create_range_extension_thunks(Context<E> &ctx) {
reset_thunk(*thunks[t++]);
// Create a new thunk and place it at D.
offset = align_to(offset, Thunk::alignment);
offset = align_to(offset, Thunk<E>::alignment);
i64 thunk_idx = thunks.size();
Thunk *thunk = new Thunk(*this, offset);
Thunk<E> *thunk = new Thunk<E>(*this, offset);
thunks.emplace_back(thunk);
// Scan relocations between B and C to collect symbols that need
@@ -278,11 +275,11 @@ void OutputSection<E>::create_range_extension_thunks(Context<E> &ctx) {
[&](InputSection<E> *isec) {
std::span<Symbol<E> *> syms = isec->file.symbols;
std::span<const ElfRel<E>> rels = isec->get_rels(ctx);
std::span<RangeExtensionRef> range_extn = isec->extra.range_extn;
std::span<ThunkRef> thunk_refs = isec->extra.thunk_refs;
for (i64 i = 0; i < rels.size(); i++)
if (range_extn[i].thunk_idx == thunk_idx)
range_extn[i].sym_idx = syms[rels[i].r_sym]->extra.thunk_sym_idx;
if (thunk_refs[i].thunk_idx == thunk_idx)
thunk_refs[i].sym_idx = syms[rels[i].r_sym]->extra.thunk_sym_idx;
});
// Move B forward to point to the begining of the next batch.
+1 -1
View File
@@ -20,7 +20,7 @@ ubuntu-* | pop-* | linuxmint-* | debian-* | raspbian-*)
apt-get install -y cmake gcc g++
apt-get install -y file
;;
fedora-*)
fedora-* | amzn-*)
dnf install -y gcc-g++ cmake
dnf install -y glibc-static file libstdc++-static diffutils util-linux
;;
+130 -40
View File
@@ -1,7 +1,10 @@
option(MOLD_ENABLE_QEMU_TESTS "Enable tests on non-native targets" OFF)
option(MOLD_ENABLE_QEMU_TESTS "Enable tests on non-native targets" ON)
set(HOST ${CMAKE_HOST_SYSTEM_PROCESSOR})
if(EXISTS "/proc/cpuinfo")
file(READ "/proc/cpuinfo" CPUINFO)
endif()
# CMAKE_HOST_SYSTEM_PROCESSOR returns "aarch64" on ARM32 userland
# with ARM64 kernel. Here, we run `cc -dumpmachine` to determine the
# compiler's default target.
@@ -25,67 +28,154 @@ elseif(${HOST} STREQUAL "powerpc64le")
set(HOST ppc64le)
endif()
if(MOLD_ENABLE_QEMU_TESTS)
list(APPEND QEMU_ARCHS
x86_64 i386 arm aarch64 ppc ppc64 ppc64le sparc64 sh4 s390x
alpha riscv64 riscv32 m68k loongarch64)
LIST(APPEND TRIPLES
x86_64-linux-gnu
i686-linux-gnu
aarch64-linux-gnu
arm-linux-gnueabihf
riscv64-linux-gnu
powerpc-linux-gnu
powerpc64-linux-gnu
powerpc64le-linux-gnu
sparc64-linux-gnu
s390x-linux-gnu
sh4-linux-gnu
alpha-linux-gnu
riscv32-linux-gnu
m68k-linux-gnu
loongarch64-linux-gnu)
foreach(ARCH IN LISTS QEMU_ARCHS)
find_program(HAS_qemu-${ARCH} qemu-${ARCH})
endforeach()
foreach(TRIPLE IN LISTS TRIPLES)
find_program(HAS_${TRIPLE}-gcc ${TRIPLE}-gcc)
endforeach()
endif()
function(add_target ARCH TRIPLE)
set(CPU ${ARGV2})
if(${ARCH} STREQUAL ${HOST})
set(IS_NATIVE 1)
endif()
if(IS_NATIVE OR MOLD_ENABLE_QEMU_TESTS)
file(GLOB ALL_TESTS RELATIVE ${CMAKE_CURRENT_LIST_DIR} CONFIGURE_DEPENDS "*.sh")
file(GLOB ALL_TESTS RELATIVE ${CMAKE_CURRENT_LIST_DIR} CONFIGURE_DEPENDS
"*.sh")
list(FILTER ALL_TESTS EXCLUDE REGEX "_")
list(FILTER ALL_TESTS EXCLUDE REGEX "_")
file(GLOB TESTS RELATIVE ${CMAKE_CURRENT_LIST_DIR} CONFIGURE_DEPENDS "${ARCH}_*.sh")
file(GLOB TESTS RELATIVE ${CMAKE_CURRENT_LIST_DIR} CONFIGURE_DEPENDS
"${ARCH}_*.sh")
list(APPEND TESTS ${ALL_TESTS})
list(APPEND TESTS ${ALL_TESTS})
foreach(TEST IN LISTS TESTS)
foreach(TEST IN LISTS TESTS)
if(CPU)
string(REGEX REPLACE "\\.sh$" "" TESTNAME "${ARCH}_${CPU}-${TEST}")
else()
string(REGEX REPLACE "\\.sh$" "" TESTNAME "${ARCH}-${TEST}")
endif()
add_test(NAME ${TESTNAME}
COMMAND bash -x ${CMAKE_CURRENT_LIST_DIR}/${TEST}
WORKING_DIRECTORY ${mold_BINARY_DIR})
add_test(NAME ${TESTNAME}
COMMAND bash -x ${CMAKE_CURRENT_LIST_DIR}/${TEST}
WORKING_DIRECTORY ${mold_BINARY_DIR})
set_property(TEST ${TESTNAME} APPEND PROPERTY ENVIRONMENT "HOST=${HOST}")
set_property(TEST ${TESTNAME} APPEND PROPERTY ENVIRONMENT
"HOST=${HOST};CPU=${CPU}")
if(IS_NATIVE)
set_property(TEST ${TESTNAME} APPEND PROPERTY SKIP_REGULAR_EXPRESSION "skipped")
else()
set_property(TEST ${TESTNAME} APPEND PROPERTY ENVIRONMENT "TRIPLE=${TRIPLE}")
endif()
endforeach()
endif()
if(IS_NATIVE)
set_property(TEST ${TESTNAME} APPEND PROPERTY SKIP_REGULAR_EXPRESSION
"skipped")
else()
set_property(TEST ${TESTNAME} APPEND PROPERTY ENVIRONMENT
"TRIPLE=${TRIPLE}")
endif()
endforeach()
endfunction()
add_target(x86_64 x86_64-linux-gnu)
add_target(i686 i686-linux-gnu)
add_target(aarch64 aarch64-linux-gnu)
add_target(arm arm-linux-gnueabihf)
add_target(riscv64 riscv64-linux-gnu)
add_target(ppc powerpc-linux-gnu)
add_target(ppc64 powerpc64-linux-gnu)
add_target(ppc64le powerpc64le-linux-gnu)
add_target(sparc64 sparc64-linux-gnu)
add_target(s390x s390x-linux-gnu)
add_target(sh4 sh4-linux-gnu)
add_target(alpha alpha-linux-gnu)
if(${HOST} STREQUAL "x86_64" OR (HAS_qemu-x86_64 AND HAS_x86_64-linux-gnu-gcc))
add_target(x86_64 x86_64-linux-gnu)
endif()
option(MOLD_ENABLE_QEMU_TESTS_RV32 "Enable tests for RV32" OFF)
if(MOLD_ENABLE_QEMU_TESTS_RV32)
if(${HOST} STREQUAL "i686" OR (HAS_qemu-i386 AND HAS_i686-linux-gnu-gcc))
add_target(i686 i686-linux-gnu)
endif()
if(${HOST} STREQUAL "aarch64" OR (HAS_qemu-aarch64 AND HAS_aarch64-linux-gnu-gcc))
add_target(aarch64 aarch64-linux-gnu)
endif()
if(${HOST} STREQUAL "arm" OR (HAS_qemu-arm AND HAS_arm-linux-gnueabihf-gcc))
add_target(arm arm-linux-gnueabihf)
endif()
if(${HOST} STREQUAL "riscv64" OR (HAS_qemu-riscv64 AND HAS_riscv64-linux-gnu-gcc))
add_target(riscv64 riscv64-linux-gnu)
endif()
if(${HOST} STREQUAL "riscv32" OR (HAS_qemu-riscv32 AND HAS_riscv32-linux-gnu-gcc))
add_target(riscv32 riscv32-linux-gnu)
endif()
option(MOLD_ENABLE_QEMU_TESTS_POWER10 "Enable tests for Power10" OFF)
if(MOLD_ENABLE_QEMU_TESTS_POWER10)
add_target(ppc64le powerpc64le_power10-linux-gnu)
if(${HOST} STREQUAL "ppc" OR (HAS_qemu-ppc AND HAS_powerpc-linux-gnu-gcc))
add_target(ppc powerpc-linux-gnu)
endif()
option(MOLD_ENABLE_QEMU_TESTS_M68K "Enable tests for m68k" OFF)
if(MOLD_ENABLE_QEMU_TESTS_M68K)
if(${HOST} STREQUAL "ppc64" OR (HAS_qemu-ppc64 AND HAS_powerpc64-linux-gnu-gcc))
add_target(ppc64 powerpc64-linux-gnu)
endif()
if(${HOST} STREQUAL "ppc64le" OR (HAS_qemu-ppc64le AND HAS_powerpc64le-linux-gnu-gcc))
add_target(ppc64le powerpc64le-linux-gnu)
endif()
if(${HOST} STREQUAL "ppc64le" AND "${CPUINFO}" MATCHES "POWER10")
add_target(ppc64le powerpc64le-linux-gnu power10)
elseif(HAS_qemu-ppc64le AND HAS_powerpc64le-linux-gnu-gcc)
file(WRITE "${CMAKE_BINARY_DIR}/empty.c" "")
execute_process(
COMMAND powerpc64le-linux-gnu-gcc -mcpu=power10 -E
"${CMAKE_BINARY_DIR}/empty.c"
RESULT_VARIABLE GCC_EXIT_CODE
OUTPUT_QUIET ERROR_QUIET)
execute_process(
COMMAND qemu-ppc64le -cpu help
OUTPUT_VARIABLE QEMU_CPUS)
if("${GCC_EXIT_CODE}" EQUAL "0" AND "${QEMU_CPUS}" MATCHES power10_v2.0)
add_target(ppc64le powerpc64le-linux-gnu power10)
endif()
endif()
if(${HOST} STREQUAL "sparc64" OR (HAS_qemu-sparc64 AND HAS_sparc64-linux-gnu-gcc))
add_target(sparc64 sparc64-linux-gnu)
endif()
if(${HOST} STREQUAL "s390x" OR (HAS_qemu-s390x AND HAS_s390x-linux-gnu-gcc))
add_target(s390x s390x-linux-gnu)
endif()
if(${HOST} STREQUAL "sh4" OR (HAS_qemu-sh4 AND HAS_sh4-linux-gnu-gcc))
add_target(sh4 sh4-linux-gnu)
endif()
if(${HOST} STREQUAL "alpha" OR (HAS_qemu-alpha AND HAS_alpha-linux-gnu-gcc))
add_target(alpha alpha-linux-gnu)
endif()
if(${HOST} STREQUAL "m68k" OR (HAS_qemu-m68k AND HAS_m68k-linux-gnu-gcc))
add_target(m68k m68k-linux-gnu)
endif()
option(MOLD_ENABLE_QEMU_TESTS_LOONGARCH "Enable tests for LoongArch" OFF)
if(MOLD_ENABLE_QEMU_TESTS_LOONGARCH OR ${HOST} STREQUAL loongarch64)
if(${HOST} STREQUAL "loongarch64" OR (HAS_qemu-loongarch64 AND HAS_loongarch64-linux-gnu-gcc))
add_target(loongarch64 loongarch64-linux-gnu)
endif()
+1 -2
View File
@@ -7,8 +7,7 @@ $CC -B. -o $t/exe $t/a.c -Wl,-build-id
readelf -n $t/exe | grep -qv 'GNU.*0x00000010.*NT_GNU_BUILD_ID'
$CC -B. -o $t/exe $t/a.c -Wl,-build-id=uuid
readelf -nW $t/exe |
grep -Eq 'GNU.*0x00000010.*NT_GNU_BUILD_ID.*Build ID: ............4...[89abcdef]'
readelf -nW $t/exe | grep -Eq 'Build ID: ............4...[89abcdef]'
$CC -B. -o $t/exe $t/a.c -Wl,-build-id=md5
readelf -n $t/exe | grep -q 'GNU.*0x00000010.*NT_GNU_BUILD_ID'
+9 -12
View File
@@ -5,7 +5,7 @@ export LC_ALL=C
canonical_name() {
case $1 in
i?86) echo i386 ;;
i?86) echo i686 ;;
arm*) echo arm ;;
powerpc) echo ppc ;;
powerpc64) echo ppc64 ;;
@@ -30,8 +30,7 @@ if [ -z "$TRIPLE" ]; then
OBJCOPY=objcopy
STRIP=strip
QEMU=
elif [ "$TRIPLE" = powerpc64le_power10-linux-gnu ]; then
TRIPLE=powerpc64le-linux-gnu
elif [ "$TRIPLE" = powerpc64le-linux-gnu -a "$CPU" = power10 ]; then
MACHINE=ppc64le
TESTDIR=out/test/elf/ppc64le-power10
CC="${TEST_CC:-$TRIPLE-gcc} -mcpu=power10"
@@ -52,22 +51,20 @@ else
OBJDUMP="$TRIPLE-objdump"
OBJCOPY="$TRIPLE-objcopy"
STRIP="$TRIPLE-strip"
QEMU="qemu-$MACHINE -L /usr/$TRIPLE"
if [ $MACHINE = i686 ]; then
QEMU="qemu-i386 -L /usr/$TRIPLE"
else
QEMU="qemu-$MACHINE -L /usr/$TRIPLE"
fi
fi
if [ $MACHINE = x86_64 -o $MACHINE = i386 -o $MACHINE = arm ]; then
if [ $MACHINE = x86_64 -o $MACHINE = i686 -o $MACHINE = arm ]; then
tlsdesc_opt=-mtls-dialect=gnu2
elif [ $MACHINE = aarch64 ]; then
tlsdesc_opt=-mtls-dialect=desc
fi
if [ $MACHINE = ppc64le -a -e /proc/cpuinfo ] && grep -q POWER10 /proc/cpuinfo; then
CC="$CC -mcpu=power10"
CXX="$CXX -mcpu=power10"
GCC="$GCC -mcpu=power10"
GXX="$GXX -mcpu=power10"
fi
# Common functions
test_cflags() {
echo 'int main() {}' | $CC "$@" -o /dev/null -xc - >& /dev/null
+1 -2
View File
@@ -1,7 +1,6 @@
#!/bin/bash
. $(dirname $0)/common.inc
cat <<EOF | $CC -o $t/a.o -c -xc -fno-PIE -
#include <setjmp.h>
#include <signal.h>
@@ -32,7 +31,7 @@ int main() {
}
EOF
cat <<EOF | $CC -fPIC -shared -o $t/b.so -xc -
cat <<EOF | $CC -B. -fPIC -shared -o $t/b.so -xc -
__attribute__((section (".data.rel.ro"))) char readonly[100] = "abc";
char readwrite[100] = "abc";
EOF
+55
View File
@@ -0,0 +1,55 @@
#!/bin/bash
. $(dirname $0)/common.inc
cat <<EOF | $CC -o $t/a.o -c -xc -
#include <stdio.h>
static void ctor1() { printf("ctor1 "); }
static void ctor2() { printf("ctor2 "); }
static void ctor3() { printf("ctor3 "); }
static void ctor4() { printf("ctor4 "); }
static void dtor1() { printf("dtor1 "); }
static void dtor2() { printf("dtor2 "); }
static void dtor3() { printf("dtor3 "); }
static void dtor4() { printf("dtor4 "); }
__attribute__((aligned(sizeof(void *)), section(".ctors.65435")))
void (*ctors65435[])() = { ctor1 };
__attribute__((aligned(sizeof(void *)), section(".ctors.65433")))
void (*ctors65433[])() = { ctor2 };
__attribute__((aligned(sizeof(void *)), section(".ctors")))
void (*ctors[])() = { ctor4, ctor3 };
__attribute__((aligned(sizeof(void *)), section(".dtors")))
void (*dtors[])() = { dtor1, dtor2 };
__attribute__((aligned(sizeof(void *)), section(".dtors.65433")))
void (*dtors65433[])() = { dtor3 };
__attribute__((aligned(sizeof(void *)), section(".dtors.65435")))
void (*dtors65435[])() = { dtor4 };
EOF
cat <<EOF | $CC -o $t/b.o -c -xc -
#include <stdio.h>
__attribute__((constructor(101)))
static void init1() { printf("init1 "); }
__attribute__((constructor))
static void init2() { printf("init2 "); }
__attribute__((destructor(101)))
static void fini1() { printf("fini1 "); }
__attribute__((destructor))
static void fini2() { printf("fini2 "); }
int main() {}
EOF
$CC -B. -o $t/exe $t/a.o $t/b.o
$QEMU $t/exe | grep -q 'ctor1 init1 ctor2 ctor3 ctor4 init2 fini2 dtor1 dtor2 dtor3 fini1 dtor4'
+9
View File
@@ -0,0 +1,9 @@
#!/bin/bash
. $(dirname $0)/common.inc
cat <<EOF | $CC -o $t/a.o -c -xc -
int main() {}
EOF
! $CC -B. -o $t/exe $t/a.o -Wl,-defsym=foo=bar 2> $t/log
grep -q 'undefined symbol: bar' $t/log
+44
View File
@@ -0,0 +1,44 @@
#!/bin/bash
. $(dirname $0)/common.inc
cat <<EOF | $CC -o $t/a.o -c -xc - -fPIC
#include <stdio.h>
void foo() { printf("foo1 "); }
void bar() { printf("bar1 "); }
void baz() { printf("baz1 "); }
void print() {
foo();
bar();
baz();
printf("\n");
}
EOF
cat <<EOF > $t/dyn
{ foo; bar; };
EOF
$CC -B. -shared -o $t/b.so $t/a.o -Wl,--dynamic-list=$t/dyn
cat <<EOF | $CC -o $t/c.o -c -xc - -fPIC
#include <stdio.h>
void foo() { printf("foo2 "); }
void bar() { printf("bar2 "); }
void baz() { printf("baz2 "); }
EOF
$CC -B. -shared -o $t/d.so $t/c.o
cat <<EOF | $CC -o $t/e.o -c -xc -
#include <stdio.h>
void print();
int main() { print(); }
EOF
$CC -B. -o $t/exe1 $t/e.o -Wl,-push-state,-no-as-needed $t/b.so -Wl,-pop-state
$QEMU $t/exe1 | grep -q 'foo1 bar1 baz1'
$CC -B. -o $t/exe2 $t/e.o -Wl,-push-state,-no-as-needed $t/d.so $t/b.so -Wl,-pop-state
$QEMU $t/exe2 | grep -q 'foo2 bar2 baz1'
+1
View File
@@ -1,6 +1,7 @@
#!/bin/bash
. $(dirname $0)/common.inc
[ $MACHINE = m68k ] && skip
[ $MACHINE = sh4 ] && skip
cat <<EOF | $CXX -o $t/a.o -c -fPIC -xc++ -g -ffunction-sections -
+48
View File
@@ -0,0 +1,48 @@
#!/bin/bash
. $(dirname $0)/common.inc
nm mold | grep -q '__tsan_init' && skip
[ $MACHINE = m68k ] && skip
[ $MACHINE = sh4 ] && skip
cat <<EOF | $CXX -o $t/a.o -c -xc++ -
int foo() {
try {
throw 1;
} catch (int x) {
return x;
}
return 2;
}
EOF
cat <<EOF | $CXX -o $t/b.o -c -xc++ -
int bar() {
try {
throw 3;
} catch (int x) {
return x;
}
return 4;
}
EOF
$OBJCOPY --rename-section .eh_frame=.eh_frame2 $t/a.o
./mold -r -o $t/c.o $t/a.o $t/b.o
$OBJCOPY --rename-section .eh_frame2=.eh_frame $t/c.o
cat <<EOF | $CXX -o $t/d.o -c -xc++ -
#include <stdio.h>
int foo();
int bar();
int main() {
printf("%d %d\n", foo(), bar());
}
EOF
$CXX -B. -o $t/exe1 $t/d.o $t/c.o
$QEMU $t/exe1
$QEMU $t/exe1 | grep -q '^1 3$'
+1
View File
@@ -1,6 +1,7 @@
#!/bin/bash
. $(dirname $0)/common.inc
[ $MACHINE = m68k ] && skip
[ $MACHINE = sh4 ] && skip
static=
+1 -1
View File
@@ -20,7 +20,7 @@ GOT_ADDR=$($QEMU $t/exe)
# _GLOBAL_OFFSET_TABLE_ refers the end of .got only on x86.
# We assume .got is followed by .gotplt.
if [ $MACHINE = x86_64 -o $MACHINE = i386 ]; then
if [ $MACHINE = x86_64 -o $MACHINE = i686 ]; then
readelf -WS $t/exe | grep -q "\.got\.plt .*$GOT_ADDR "
else
readelf -WS $t/exe | grep -q "\.got .*$GOT_ADDR "
+1 -1
View File
@@ -18,5 +18,5 @@ int main() {
}
EOF
$CC -B. -o $t/exe $t/a.o $t/b.o
$CC -B. -o $t/exe $t/a.o $t/b.o -no-pie
$QEMU $t/exe | grep -q 'foo=5'
+1 -1
View File
@@ -16,5 +16,5 @@ int main() {
EOF
# Make sure that we do not copy .gnu.warning.* sections.
$CC -B. -o $t/exe $t/a.o
$CC -B. -o $t/exe $t/a.o -no-pie
$QEMU $t/exe | grep -q 'Hello world'
@@ -1,7 +1,7 @@
#!/bin/bash
. $(dirname $0)/common.inc
[ $MACHINE = i386 ] || skip
[ $MACHINE = i686 ] || skip
cat <<'EOF' | $CC -fPIC -o $t/a.o -c -xassembler -
.globl get_foo
+48
View File
@@ -0,0 +1,48 @@
#!/bin/bash
. $(dirname $0)/common.inc
supports_tlsdesc || skip
cat <<'EOF' | $GCC -c -o $t/a.o -xassembler -
.globl get_foo
.type get_foo, @function
get_foo:
pushl %ebx
call __x86.get_pc_thunk.bx
addl $_GLOBAL_OFFSET_TABLE_, %ebx
subl $8, %esp
leal foo@TLSDESC(%ebx), %ebx
movl %ebx, %eax
call *foo@TLSCALL(%eax)
movl %gs:(%eax), %eax
addl $8, %esp
popl %ebx
ret
EOF
cat <<EOF | $GCC -fPIC -c -o $t/b.o -xc - $tlsdesc_opt
#include <stdio.h>
_Thread_local int foo;
int get_foo();
int main() {
foo = 42;
printf("%d\n", get_foo());
}
EOF
$CC -B. -o $t/exe1 $t/a.o $t/b.o
$QEMU $t/exe1 | grep -q 42
$CC -B. -o $t/exe2 $t/a.o $t/b.o -Wl,-no-relax
$QEMU $t/exe2 | grep -q 42
$CC -B. -shared -o $t/c.so $t/a.o
$CC -B. -o $t/exe3 $t/b.o $t/c.so
$QEMU $t/exe3 | grep -q 42
$CC -B. -shared -o $t/c.so $t/a.o -Wl,-no-relax
$CC -B. -o $t/exe4 $t/b.o $t/c.so -Wl,-no-relax
$QEMU $t/exe4 | grep -q 42
+2 -2
View File
@@ -29,5 +29,5 @@ int main() {
}
EOF
$CC -B. -o $t/exe1 $t/c.o $t/b.so -no-pie
$QEMU $t/exe1 | grep -Eq '^(\S+) \1'
$CC -B. -o $t/exe $t/c.o $t/b.so -no-pie
$QEMU $t/exe | grep -Eq '^(\S+) \1'
+5 -2
View File
@@ -16,5 +16,8 @@ int main() {
}
EOF
$CC -B. -o $t/exe $t/a.o
$QEMU $t/exe | grep -Eq '^(\S+) \1$'
$CC -B. -o $t/exe1 $t/a.o -pie
$QEMU $t/exe1 | grep -Eq '^(\S+) \1$'
$CC -B. -o $t/exe2 $t/a.o -no-pie
$QEMU $t/exe2 | grep -Eq '^(\S+) \1$'
+5 -2
View File
@@ -33,5 +33,8 @@ int main() {
}
EOF
$CC -B. -o $t/exe $t/a.o $t/b.o $t/c.o
$QEMU $t/exe | grep -q '^3$'
$CC -B. -o $t/exe1 $t/a.o $t/b.o $t/c.o -pie
$QEMU $t/exe1 | grep -q '^3$'
$CC -B. -o $t/exe2 $t/a.o $t/b.o $t/c.o -no-pie
$QEMU $t/exe2 | grep -q '^3$'
+5 -2
View File
@@ -24,5 +24,8 @@ int main() {
}
EOF
$CC -B. -o $t/exe $t/a.o
$QEMU $t/exe | grep -q 'Hello world'
$CC -B. -o $t/exe1 $t/a.o -pie
$QEMU $t/exe1 | grep -q 'Hello world'
$CC -B. -o $t/exe2 $t/a.o -no-pie
$QEMU $t/exe2 | grep -q 'Hello world'
+1
View File
@@ -1,6 +1,7 @@
#!/bin/bash
. $(dirname $0)/common.inc
[ $MACHINE = m68k ] && skip
[ $MACHINE = sh4 ] && skip
cat <<EOF | $CXX -o $t/a.o -c -xc++ -
+1 -1
View File
@@ -1,7 +1,7 @@
#!/bin/bash
. $(dirname $0)/common.inc
[ $MACHINE = i386 ] && skip
[ $MACHINE = i686 ] && skip
cat <<EOF | $CC -o $t/a.o -c -xc - -ffunction-sections -fPIC
#include <stdio.h>
+1 -1
View File
@@ -1,7 +1,7 @@
#!/bin/bash
. $(dirname $0)/common.inc
[ $MACHINE = i386 ] && skip
[ $MACHINE = i686 ] && skip
cat <<EOF | $CC -o $t/a.o -c -xc - -ffunction-sections
#include <stdio.h>
+14
View File
@@ -0,0 +1,14 @@
#!/bin/bash
. $(dirname $0)/common.inc
cat <<EOF | $CC -o $t/a.o -c -xc -
void _start() {}
EOF
$CC -B. -o $t/exe1 $t/a.o -nostdlib -Wl,-nmagic
$CC -B. -o $t/exe2 $t/a.o -nostdlib
end1=$(nm $t/exe1 | grep ' end$' | cut -d' ' -f1)
end2=$(nm $t/exe2 | grep ' end$' | cut -d' ' -f1)
[ $((0x$end1)) -lt $((0x$end2)) ]
+1 -1
View File
@@ -1,7 +1,7 @@
#!/bin/bash
. $(dirname $0)/common.inc
[ $MACHINE = i386 ] && skip
[ $MACHINE = i686 ] && skip
[ $MACHINE = m68k ] && skip
[ $MACHINE = arm ] && skip
[ $MACHINE = ppc64 ] && skip
+5 -1
View File
@@ -2,12 +2,16 @@
. $(dirname $0)/common.inc
# Skip if 32 bits as we use very large addresses in this test.
[ $MACHINE = i386 ] && skip
[ $MACHINE = i686 ] && skip
[ $MACHINE = riscv32 ] && skip
# It looks like SPARC's runtime can't handle PLT if it's too far from GOT.
[ $MACHINE = sparc64 ] && skip
# qemu aborts with the "Unknown exception 0x5" error, although this
# test passes on a real POWER10 machine.
[ -n "$QEMU" -a "$CPU" = power10 ] && skip
cat <<EOF > $t/a.c
#include <stdio.h>
+2 -1
View File
@@ -4,7 +4,8 @@
# OneTBB isn't tsan-clean
nm mold | grep -q '__tsan_init' && skip
# Ubuntu 22.04 SH4 GCC is broken
# Ubuntu 22.04 GCC is broken
[ $MACHINE = m68k ] && skip
[ $MACHINE = sh4 ] && skip
cat <<EOF | $CXX -c -o $t/a.o -xc++ -
+1
View File
@@ -1,6 +1,7 @@
#!/bin/bash
. $(dirname $0)/common.inc
[ $MACHINE = m68k ] && skip
[ $MACHINE = sh4 ] && skip
# OneTBB isn't tsan-clean
+50
View File
@@ -0,0 +1,50 @@
#!/bin/bash
. $(dirname $0)/common.inc
# ARM assembler has a differnet grammar than the others.
# Concretely speaking, ARM as uses "@" as a start of a comment.
[ $MACHINE = arm ] && skip
cat <<'EOF' | $CC -c -o $t/a.o -x assembler -
.globl val1, val2, val3, val4, val5
.section .rodata.str1.1,"aMS",@progbits,1
val1:
.ascii "Hello \0"
.section .rodata.foo,"aMS",@progbits,4
.p2align 2
val2:
.ascii "world \0\0\0\0"
.section .rodata.x,"aMS",@progbits,1
val3:
.ascii "foobar\0"
.section .rodata.cst8,"aM",@progbits,8
.p2align 3
val4:
.ascii "abcdefgh"
.section .rodatabaz,"aMS",@progbits,1
val5:
.ascii "baz\0"
EOF
cat <<'EOF' | $CC -c -o $t/b.o -xc -
#include <stdio.h>
extern char val1, val2, val3, val4, val5;
int main() {
printf("%p %p %p %p %p\n", &val1, &val2, &val3, &val4, &val5);
}
EOF
$CC -B. -o $t/exe $t/a.o $t/b.o
readelf -p .rodata.str1.1 $t/exe | grep -q Hello
readelf -p .rodata.str4.4 $t/exe | grep -q world
readelf -p .rodata.str1.1 $t/exe | grep -q foobar
readelf -p .rodata.cst8 $t/exe | grep -q abcdefgh
readelf -p .rodatabaz $t/exe | grep -q baz
+25
View File
@@ -0,0 +1,25 @@
#!/bin/bash
. $(dirname $0)/common.inc
cat <<EOF | $CC -o $t/a.o -c -xc -
#include <stdio.h>
int main() {
printf("Hello world\n");
}
EOF
$CC -B. -o $t/exe1 $t/a.o
$QEMU $t/exe1 | grep -q 'Hello world'
[ "$(readelf -Wl $t/exe1 | grep NULL | wc -l)" -eq 0 ]
$CC -B. -o $t/exe2 $t/a.o -Wl,--spare-program-headers=0
$QEMU $t/exe2 | grep -q 'Hello world'
[ "$(readelf -Wl $t/exe2 | grep NULL | wc -l)" -eq 0 ]
$CC -B. -o $t/exe3 $t/a.o -Wl,--spare-program-headers=1
$QEMU $t/exe3 | grep -q 'Hello world'
[ "$(readelf -Wl $t/exe3 | grep NULL | wc -l)" -eq 1 ]
$CC -B. -o $t/exe4 $t/a.o -Wl,--spare-program-headers=5
$QEMU $t/exe4 | grep -q 'Hello world'
[ "$(readelf -Wl $t/exe4 | grep NULL | wc -l)" -eq 5 ]
+14
View File
@@ -0,0 +1,14 @@
#!/bin/bash
. $(dirname $0)/common.inc
cat <<EOF | $CC -o $t/a.o -c -xc -
__thread char foo;
__attribute__((section(".data.rel.ro.bar"), aligned(16*1024)))
char bar;
int main() {}
EOF
$CC -B. -o $t/exe $t/a.o
$QEMU $t/exe
+1 -1
View File
@@ -19,7 +19,7 @@ EOF
$CC -B. -shared -o $t/b.so $t/a.o
cat <<EOF | $GCC -c -o $t/c.o -xc -
cat <<EOF | $GCC -c -o $t/c.o -xc - -fPIC
#include <stdio.h>
_Thread_local int baz;
+1 -1
View File
@@ -23,5 +23,5 @@ cat <<EOF | $CC -xc -c -o $t/b.o -
__attribute__((tls_model("global-dynamic"))) _Thread_local int foo;
EOF
$CC -B. -o $t/exe $t/a.o $t/b.o
$CC -B. -o $t/exe $t/a.o $t/b.o -no-pie
$QEMU $t/exe | grep -q '3 5 3 5'
+19
View File
@@ -0,0 +1,19 @@
#!/bin/bash
. $(dirname $0)/common.inc
cat <<'EOF' > $t/a.ver
VER1 { foo*; };
VER2 { foo_x; };
EOF
cat <<EOF | $CC -fPIC -c -o $t/b.o -xc -
void foo_x() {}
void foo_y() {}
void foo_z() {}
EOF
$CC -B. -shared -Wl,--version-script=$t/a.ver -o $t/c.so $t/b.o
readelf -W --dyn-syms $t/c.so > $t/log
grep -Fq 'foo_x@@VER2' $t/log
grep -Fq 'foo_y@@VER1' $t/log
grep -Fq 'foo_z@@VER1' $t/log
+19
View File
@@ -0,0 +1,19 @@
#!/bin/bash
. $(dirname $0)/common.inc
cat <<'EOF' > $t/a.ver
VER1 { foo_x; };
VER2 { foo*; };
EOF
cat <<EOF | $CC -fPIC -c -o $t/b.o -xc -
void foo_x() {}
void foo_y() {}
void foo_z() {}
EOF
$CC -B. -shared -Wl,--version-script=$t/a.ver -o $t/c.so $t/b.o
readelf -W --dyn-syms $t/c.so > $t/log
grep -Fq 'foo_x@@VER1' $t/log
grep -Fq 'foo_y@@VER2' $t/log
grep -Fq 'foo_z@@VER2' $t/log
+2 -2
View File
@@ -9,10 +9,10 @@ EOF
cat <<EOF | $CXX -fPIC -c -o $t/b.o -xc -
int foo = 5;
int bar = 6;
int quux = 100;
EOF
$CC -B. -shared -Wl,--version-script=$t/a.ver \
-o $t/c.so $t/b.o
$CC -B. -shared -Wl,--version-script=$t/a.ver -o $t/c.so $t/b.o
cat <<'EOF' > $t/d.ver
VER_Y1 { local; *; };
+28
View File
@@ -0,0 +1,28 @@
#!/bin/bash
. $(dirname $0)/common.inc
[ $MACHINE = x86_64 ] || skip
test_cflags -fcf-protection || skip
cat <<EOF | $CC -o $t/a.o -c -xc - -fno-function-sections -O -fcf-protection
int foo() { return 3; }
int bar() { return foo(); }
EOF
cat <<EOF | $CC -o $t/b.o -c -xc - -fno-function-sections -O -fcf-protection
int main() {}
EOF
$CC -B. -o $t/exe1 $t/a.o $t/b.o
$OBJDUMP -dr $t/exe1 > $t/log1
grep -A1 '<foo>:' $t/log1 | grep -q endbr64
grep -A1 '<bar>:' $t/log1 | grep -q endbr64
grep -A1 '<main>:' $t/log1 | grep -q endbr64
$CC -B. -o $t/exe2 $t/a.o $t/b.o -Wl,-z,rewrite-endbr
$OBJDUMP -dr $t/exe2 > $t/log2
grep -A1 '<foo>:' $t/log2 | grep -q nop
grep -A1 '<bar>:' $t/log2 | grep -q nop
grep -A1 '<main>:' $t/log2 | grep -q endbr64
+1
View File
@@ -22,5 +22,6 @@ int main() {}
EOF
$CC -B. -o $t/exe $t/a.o $t/b.o $t/c.o
$OBJDUMP -d $t/exe >& /dev/null || skip
$OBJDUMP -d $t/exe | grep -A1 '<__x86.get_pc_thunk.bx>:' | \
grep -Fq 'puts$plt'
+2
View File
@@ -4,6 +4,8 @@
# Skip if target is not x86-64
[ $MACHINE = x86_64 ] || skip
echo endbr64 | $CC -o /dev/null -c -xassembler - 2> /dev/null || skip
$CC -fcf-protection=branch -c /dev/null -o /dev/null -xc 2> /dev/null || skip
cat <<EOF | $CC -fcf-protection=branch -c -o $t/a.o -xc -
+1
View File
@@ -3,6 +3,7 @@
# Skip if target is not x86-64
[ $MACHINE = x86_64 ] || skip
echo ret | cc -c -o /dev/null -xassembler -Wa,-mrelax-relocations=yes - 2> /dev/null || skip
cat <<EOF | $CC -o $t/a.o -c -x assembler -Wa,-mrelax-relocations=yes -
.globl bar
+47
View File
@@ -0,0 +1,47 @@
#!/bin/bash
. $(dirname $0)/common.inc
supports_tlsdesc || skip
cat <<EOF | $GCC -c -o $t/a.o -xassembler -
.globl get_foo
.type get_foo, @function
get_foo:
pushq %rbp
movq %rsp, %rbp
leaq foo@TLSDESC(%rip), %rbx
movq %rbx, %rax
call *foo@TLSCALL(%rax)
movq %fs:0, %rdx
addq %rdx, %rax
movl (%rax), %eax
popq %rbp
ret
EOF
cat <<EOF | $GCC -fPIC -c -o $t/b.o -xc - $tlsdesc_opt
#include <stdio.h>
_Thread_local int foo;
int get_foo();
int main() {
foo = 42;
printf("%d\n", get_foo());
}
EOF
$CC -B. -o $t/exe1 $t/a.o $t/b.o
$QEMU $t/exe1 | grep -q 42
$CC -B. -o $t/exe2 $t/a.o $t/b.o -Wl,-no-relax
$QEMU $t/exe2 | grep -q 42
$CC -B. -shared -o $t/c.so $t/a.o
$CC -B. -o $t/exe3 $t/b.o $t/c.so
$QEMU $t/exe3 | grep -q 42
$CC -B. -shared -o $t/c.so $t/a.o -Wl,-no-relax
$CC -B. -o $t/exe4 $t/b.o $t/c.so -Wl,-no-relax
$QEMU $t/exe4 | grep -q 42
+1
View File
@@ -2,6 +2,7 @@
. $(dirname $0)/common.inc
[ $MACHINE = x86_64 ] || skip
echo endbr64 | $CC -o /dev/null -c -xassembler - 2> /dev/null || skip
cat <<EOF | $CC -o $t/a.o -c -x assembler -
.globl main
+1
View File
@@ -2,6 +2,7 @@
. $(dirname $0)/common.inc
[ $MACHINE = x86_64 ] || skip
echo endbr64 | $CC -o /dev/null -c -xassembler - 2> /dev/null || skip
cat <<EOF | $CC -o $t/a.o -c -x assembler -
.globl main