Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from rui314:main #317

Merged
merged 169 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
169 commits
Select commit Hold shift + click to select a range
48e777a
Update man page
rui314 May 31, 2024
01bc0b5
Update mold.1 (automated commit)
rui314 May 31, 2024
8cd85aa
Make `--undefined=ignore-in-object-files` to override `-z defs`
rui314 May 31, 2024
ec04bfd
Bump mold version to 2.32.0
rui314 Jun 9, 2024
841a186
Fix corrupted .riscv.attributes ISA string (#1276)
yujincheng08 Jun 10, 2024
24b8941
Refactor
rui314 Jun 10, 2024
2a25c81
Refactor
rui314 Jun 11, 2024
768f1df
Attempt to fix CI
rui314 Jun 11, 2024
af6d356
Attempt to fix CI
rui314 Jun 11, 2024
3ad387f
Refactor
rui314 Jun 11, 2024
7669a3c
Attempt to fix CI
rui314 Jun 13, 2024
cff858b
Attempt to fix CI
rui314 Jun 13, 2024
d2774b0
Use $CXX to detect cxxflags
ziyao233 Jun 16, 2024
b83b195
Merge pull request #1281 from ziyao233/outgoing/fix-static-flag
rui314 Jun 16, 2024
8638930
Allow numbers other than 1 for MOLD_JOBS
rui314 Jun 15, 2024
1f94716
Attempt to fix CI
rui314 Jun 18, 2024
7cce297
Only define `STDERR_FILENO` when not defined
yujincheng08 Jun 19, 2024
cf9e599
Use `__has_include` instead
yujincheng08 Jun 19, 2024
f9a37e9
Remove dead code
rui314 Jun 20, 2024
bf05198
Merge pull request #1285 from yujincheng08/patch-2
rui314 Jun 20, 2024
50bf031
Support lto on mingw
yujincheng08 Jun 21, 2024
48ee8e8
Handle stackoverflow
yujincheng08 Jun 21, 2024
b1695c6
Merge pull request #1287 from yujincheng08/lto
rui314 Jun 21, 2024
0b5e5dc
Fix -Wunused-result
rui314 Jun 21, 2024
06b5926
Make weak undefined symbols to mark shared libraries as "needed"
rui314 Jun 21, 2024
5535e7a
Style change
rui314 Jun 22, 2024
4cc6136
Refactor
rui314 Jun 22, 2024
07b09a6
Attempt to fix CI
rui314 Jun 22, 2024
63f2670
Unbreak daily buildbot
rui314 Jun 23, 2024
8f9b59c
ci: add msys2
yujincheng08 Jun 23, 2024
ebfe4f1
fix mingw compiles
yujincheng08 Jun 23, 2024
c419dcd
Merge pull request #1289 from yujincheng08/main
rui314 Jun 24, 2024
f60fada
Refactor
rui314 Jun 23, 2024
35d67a3
Add a missing #include for getenv()
rui314 Jun 26, 2024
3dbe2ac
Do not write relocation addends for RISC-V
rui314 Jun 27, 2024
cf1c02d
Bump mold version to 2.32.1
rui314 Jun 27, 2024
a38be94
Simplify
rui314 Jun 27, 2024
091395d
Link with `-ldl` for dlopen()
rui314 Jun 28, 2024
34f4d65
Add an entry for AlmaLinnux
rui314 Jun 28, 2024
82fb10f
Fix a test
rui314 Jun 28, 2024
08f2a74
Improve --gdb-index test by using readelf --debug=gdb_index
marxin Jun 30, 2024
b145377
Merge pull request #1296 from marxin/improve-gdb-index-test
rui314 Jul 1, 2024
7305a5c
Fix cross-compile lto on MacOS
yujincheng08 Jul 3, 2024
55ca05b
Refactor
rui314 Jul 4, 2024
3936134
Refactor
rui314 Jul 4, 2024
c6b5453
Refactor
rui314 Jul 4, 2024
cb8cc1c
Refactor
rui314 Jul 5, 2024
4905f6d
Use macos-12 because macos-11 has been removed
yujincheng08 Jul 5, 2024
339f9c4
Attempt to fix CI
rui314 Jul 5, 2024
a63bbcd
Attempt to fix CI
rui314 Jul 5, 2024
aa939d7
Temporarily disable ASAN tests
rui314 Jul 5, 2024
5ab3708
Merge pull request #1298 from yujincheng08/patch-1
rui314 Jul 5, 2024
7089304
Simplify
rui314 Jul 5, 2024
8e3679e
Refactor
rui314 Jul 5, 2024
16ee9f4
Fix CI
rui314 Jul 7, 2024
5b43778
Fix CI
rui314 Jul 7, 2024
e78e12b
Refactor
rui314 Jul 6, 2024
19de40f
Refactor
rui314 Jul 7, 2024
cd3b817
Make notify_parent idempotent
rui314 Jul 7, 2024
596ffa9
Add --separate-debug-info
rui314 Jun 28, 2024
60760a8
Attempt to fix CI
rui314 Jul 8, 2024
f9e4cb1
Add a missing #include
rui314 Jul 8, 2024
97a1e21
Simplify crc32_solve()
rui314 Jul 8, 2024
18da5b6
Add --no-detach to write to a separate debug file in the foreground
rui314 Jul 9, 2024
02ca830
Refactor
rui314 Jul 9, 2024
315dfd3
Fix CI
rui314 Jul 9, 2024
8c1b25b
Obtain a file lock with flock() while creating a separate debug file
rui314 Jul 9, 2024
e03f74a
Fix CI
rui314 Jul 10, 2024
83ee496
Fix test flakiness for SH4
rui314 Jul 11, 2024
2fb3c9d
Update man page
rui314 Jul 11, 2024
162bcca
Update mold.1 (automated commit)
rui314 Jul 11, 2024
8426310
Revert "Allow numbers other than 1 for MOLD_JOBS"
rui314 Jul 11, 2024
0e2b6b0
Update mold.1 (automated commit)
rui314 Jul 11, 2024
8497b7e
Upgrade OneTBB in ./third-party to version 2021.13.0
rui314 Jul 11, 2024
d2870ac
Upgrade bundled mimalloc to 2.1.7
rui314 Jul 11, 2024
03a2105
Change how we resolve relocations referring to mergeable sections
rui314 Jul 13, 2024
4c569c8
Accept --library as a synonym for -l
rui314 Jul 12, 2024
0893840
Rename a variable
rui314 Jul 12, 2024
b2a1e4b
Move reader-related attributes to ReaderContext
rui314 Jul 13, 2024
4eeebf0
Define Script class and move linker script-related code there
rui314 Jul 14, 2024
1f444a2
Align Chuck, InputSection, and SectionFragment to at least 4 bytes
chewi Jul 14, 2024
0b1be96
Merge pull request #1304 from chewi/m68k-alignment
rui314 Jul 14, 2024
f180ac2
Fix build on non-m68k hosts
rui314 Jul 14, 2024
666b18b
Use __attribute__((aligned)) instead of a C macro
rui314 Jul 15, 2024
428a9c4
Skip a test on m68k
rui314 Jul 15, 2024
36bc198
Refactor
rui314 Jul 15, 2024
cdcb428
Remove dead code
rui314 Jul 15, 2024
baf9ae9
Fix a test on Debian
rui314 Jul 16, 2024
09f1008
Handle R_LARCH_TLS_LE_HI20_R and R_LARCH_TLS_LE_LO12_R
rui314 Jul 17, 2024
2acf634
Fix typo
rui314 Jul 17, 2024
cfe6099
Add --encoded-package-metadata
bdrung Jul 17, 2024
d7b079c
Refactor
rui314 Jul 17, 2024
a778f9c
Merge pull request #1308 from bdrung/encoded-package-metadata
rui314 Jul 17, 2024
0c34ec1
Refactor
rui314 Jul 17, 2024
9eacc63
Improve a test
rui314 Jul 18, 2024
aecf39d
Do not crash when an empty argument is given
rui314 Jul 18, 2024
9874a9d
Document --encoded-package-metadata
rui314 Jul 18, 2024
7b52f34
Make test scripts more verbose
rui314 Jul 18, 2024
9acda6f
Make update-manpage workflow runnable by hand
rui314 Jul 18, 2024
bae9598
Refactor
rui314 Jul 18, 2024
e67f5e6
Fix update-manpage workflow
rui314 Jul 18, 2024
5b7ae06
Update mold.1 (automated commit)
rui314 Jul 18, 2024
14d525c
Refactor
rui314 Jul 18, 2024
14a4b05
Always create a .eh_frame section even if it's empty
rui314 Jul 19, 2024
56a94d2
Fix a test on musl
rui314 Jul 19, 2024
70ea7ad
Do not use the extended regexp
rui314 Jul 19, 2024
2b4c365
Run tests on Alpine Linux with musl libc
rui314 Jul 19, 2024
d187dac
Install binary packages in Gentoo
rui314 Jul 19, 2024
6ee6410
Build and test on more Linux distros
rui314 Jul 19, 2024
b99ed6e
Refactor
rui314 Jul 20, 2024
1ccc77a
Refactor
rui314 Jul 20, 2024
bdca85e
Make Integer's constructor constexpr
rui314 Jul 20, 2024
6c4ca4e
Attempt to fix CI
rui314 Jul 20, 2024
39442db
Refactor
rui314 Jul 20, 2024
3001f02
Support --no-allow-shlib-undefined
rui314 Jul 21, 2024
1dc99aa
Fix CI
rui314 Jul 21, 2024
fa1237f
Document --no-allow-shlib-undefined
rui314 Jul 21, 2024
ed7414e
Update mold.1 (automated commit)
rui314 Jul 21, 2024
b919548
Refactor
rui314 Jul 21, 2024
5606087
Support `-z x86-64-v[234]`
rui314 Jul 21, 2024
bbfaa0b
Fix CI
rui314 Jul 21, 2024
b8556e3
Fix CI
rui314 Jul 21, 2024
e3876dd
Refactor
rui314 Jul 21, 2024
f5b2b82
Skip a test if LD_PRELOAD doesn't work
rui314 Jul 22, 2024
d829e42
Add a missing #include
rui314 Jul 22, 2024
a478db7
Do not obtain an unnecessary lock
rui314 Jul 22, 2024
2bbba15
Create canonical PLTs for PIEs
rui314 Jul 24, 2024
34ed39c
Refactor
rui314 Jul 26, 2024
98a7cff
Handle R_LARCH_RELAX
rui314 Jul 27, 2024
36e5b4b
Relax R_LARCH_TLS_LE_{HI20_R,ADD_R,LO12_R}
rui314 Jul 27, 2024
d1674d8
Add comments
rui314 Jul 27, 2024
464f765
Use `break` instead of `nop` as a filler
rui314 Jul 27, 2024
61af6f3
Report an error for an enormously large r_addend
rui314 Jul 27, 2024
766ce80
Update comments
rui314 Jul 28, 2024
3f7236d
Refactor
rui314 Jul 28, 2024
ed7eec5
Fix `-z rewrite-endbr`
rui314 Jul 28, 2024
1fb6dd3
Rename `-z rewrite-endbr` `--remove-landing-pads`
rui314 Jul 29, 2024
2c3dcc4
Document `--remove-landing-pads`
rui314 Jul 29, 2024
55e516d
Update mold.1 (automated commit)
rui314 Jul 29, 2024
bba20b3
Fix typos
rui314 Jul 29, 2024
35564d5
Update mold.1 (automated commit)
rui314 Jul 29, 2024
411dcfd
Fix comments
rui314 Jul 29, 2024
3691541
Run a binary under Intel SDE CPU emulator with CET enabled
rui314 Jul 29, 2024
9aa4fcc
Show skipped tests
rui314 Jul 29, 2024
197a74d
Add comments
rui314 Jul 29, 2024
74b359f
[LoongArch] Support relax of pcalau12i/addi.d->pcaddi (#1318)
ywgrit Jul 31, 2024
a4dc015
Update comments
rui314 Jul 29, 2024
b71bfcc
Improve pcalau12i+addi.d relaxation
rui314 Jul 31, 2024
96fda95
Add a test for LoongArch relaxation
rui314 Jul 31, 2024
bf79ae1
Refactor
rui314 Jul 31, 2024
2c6f379
Support R_LARCH_CALL36
rui314 Jul 31, 2024
4e6b4c4
Refactor
rui314 Jul 31, 2024
47c092a
Remove range extension support from LoongArch
rui314 Jul 31, 2024
db03248
Fix comments
rui314 Jul 31, 2024
bada67b
Handle R_LARCH_CALL36 as a function-calling relocation
rui314 Jul 31, 2024
218d89b
Rename a test
rui314 Jul 31, 2024
982f071
Refactor
rui314 Aug 1, 2024
dd8d971
dSupport --dynamic-list-data
rui314 Aug 1, 2024
22f85c1
Rename `--remove-landing-pad` `-z rewrite-endbr`
rui314 Aug 2, 2024
25b34bb
Update mold.1 (automated commit)
rui314 Aug 2, 2024
f7af61f
Update man page
rui314 Aug 3, 2024
06e8bf8
Update mold.1 (automated commit)
rui314 Aug 3, 2024
17a9481
Merge .sdata, .srodata and .sbss
rui314 Aug 3, 2024
121f917
LoongArch: support relaxation of pcalau12i/ld.d to pcalau12i/addi.d o…
ywgrit Aug 5, 2024
1aa966c
Refactor
rui314 Aug 5, 2024
893ec1b
Format
rui314 Aug 5, 2024
43d9536
Fix an assertion failure
rui314 Aug 5, 2024
0bf00a4
Remove `--encoded-package-metadata` from docs
rui314 Aug 6, 2024
573b8a4
Update mold.1 (automated commit)
rui314 Aug 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 52 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
env:
UBSAN_OPTIONS: print_stacktrace=1:halt_on_error=1
jobs:
Expand All @@ -13,7 +11,6 @@ jobs:
target:
# Disable PCH for the default configuration. This prevents relying on implicit includes.
- '-DCMAKE_DISABLE_PRECOMPILE_HEADERS=On'
- '-DMOLD_USE_ASAN=On'
- '-DMOLD_USE_TSAN=On'
runs-on: ubuntu-20.04
steps:
Expand Down Expand Up @@ -72,21 +69,22 @@ jobs:

# Install a LoongArch toolchain
mkdir /larch
wget -O- -q https://github.com/loongson/build-tools/releases/download/2023.08.08/CLFS-loongarch64-8.1-x86_64-cross-tools-gcc-glibc.tar.xz | tar -C /larch --strip-components=1 --xz -xf -
wget -O- -q https://github.com/loongson/build-tools/releases/download/2024.06.01/x86_64-cross-tools-loongarch64-binutils_2.42-gcc_14.1.0-glibc_2.39.tar.xz | tar -C /larch --strip-components=1 --xz -xf -

cp -r /larch/loongarch64-unknown-linux-gnu/lib/* /larch/target/lib64
ln -sf /larch/target /usr/loongarch64-linux-gnu
cp -r /larch/loongarch64-unknown-linux-gnu/lib/* /usr/loongarch64-linux-gnu/lib64/

for i in objdump objcopy strip; do
for i in gcc g++ objdump objcopy strip; do
ln -sf /larch/bin/loongarch64-unknown-linux-gnu-$i /usr/bin/loongarch64-linux-gnu-$i
done

echo '/larch/bin/loongarch64-unknown-linux-gnu-gcc -L/larch/loongarch64-unknown-linux-gnu "$@"' > /usr/bin/loongarch64-linux-gnu-gcc
echo '/larch/bin/loongarch64-unknown-linux-gnu-g++ -L/larch/loongarch64-unknown-linux-gnu "$@"' > /usr/bin/loongarch64-linux-gnu-g++
chmod 755 /usr/bin/loongarch64-linux-gnu-{gcc,g++}

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

# Install Intel SDE CPU emulator for CET-related tests
mkdir /sde
wget -O- -q https://downloadmirror.intel.com/813591/sde-external-9.33.0-2024-01-07-lin.tar.xz | tar -C /sde --strip-components=1 --xz -xf -
ln -s /sde/sde /usr/bin
- name: ccache
uses: hendrikmuhs/ccache-action@v1
- name: build
Expand All @@ -109,8 +107,33 @@ jobs:
build
!build/CMakeFiles

build-distros:
strategy:
matrix:
distro:
- alpine
- archlinux
- fedora
- gentoo/stage3
- opensuse/tumbleweed
runs-on: ubuntu-latest
container: ${{ matrix.distro }}
steps:
- uses: actions/checkout@v2
- run: ./install-build-deps.sh
- name: build
run: |
mkdir build
cd build
cmake ..
cmake --build . -j$(nproc)
- name: test
run: |
cd build
ctest -j$(nproc)

build-macos:
runs-on: macos-11
runs-on: macos-12
strategy:
matrix:
target:
Expand Down Expand Up @@ -139,3 +162,21 @@ jobs:
cd build
cmake -T clangcl ..
cmake --build . -j $Env:NUMBER_OF_PROCESSORS

build-msys:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- name: Setup MSYS2
uses: msys2/setup-msys2@v2
with:
msystem: UCRT64
update: true
pacboy: gcc-libs:p libwinpthread-git:p tbb:p zlib:p zstd:p dlfcn:p cc:p cmake:p ninja:p
- name: build
shell: msys2 {0}
run: |
mkdir build
cd build
cmake -GNinja -DMOLD_USE_MIMALLOC=OFF -DMOLD_USE_SYSTEM_TBB=ON -DMOLD_USE_MOLD=OFF ..
cmake --build . -j $(nproc)
5 changes: 2 additions & 3 deletions .github/workflows/update-manpage.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# This file is generated by ChatGPT

name: Update manpage

on:
Expand All @@ -8,6 +6,7 @@ on:
- 'docs/mold.md'
branches:
- main
workflow_dispatch:

jobs:
update-manpage:
Expand All @@ -18,7 +17,7 @@ jobs:
uses: actions/checkout@v2

- name: Install ronn
run: sudo apt-get install -y ronn
run: sudo apt-get update && sudo apt-get install -y ronn

- name: Generate mold.1 from mold.md
run: ronn --roff docs/mold.md
Expand Down
20 changes: 16 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
# features and behave exactly the same.

cmake_minimum_required(VERSION 3.14)
project(mold VERSION 2.31.0)
project(mold VERSION 2.32.1)

include(CMakeDependentOption)
include(CheckSymbolExists)
Expand All @@ -58,7 +58,12 @@ endif()

add_executable(mold)
target_compile_features(mold PRIVATE cxx_std_20)
target_link_libraries(mold PRIVATE ${CMAKE_DL_LIBS})

if(MINGW)
target_link_libraries(mold PRIVATE dl)
else()
target_link_libraries(mold PRIVATE ${CMAKE_DL_LIBS})
endif()

if(NOT "${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC")
target_compile_options(mold PRIVATE
Expand Down Expand Up @@ -317,17 +322,23 @@ list(APPEND MOLD_ELF_TEMPLATE_FILES
elf/output-chunks.cc
elf/passes.cc
elf/relocatable.cc
elf/subprocess.cc
elf/shrink-sections.cc
elf/thunks.cc
elf/tls.cc
)

if(WIN32)
if(WIN32 AND NOT MINGW)
list(APPEND MOLD_ELF_TEMPLATE_FILES elf/lto-win32.cc)
else()
list(APPEND MOLD_ELF_TEMPLATE_FILES elf/lto-unix.cc)
endif()

if(WIN32)
list(APPEND MOLD_ELF_TEMPLATE_FILES elf/subprocess-win32.cc)
else()
list(APPEND MOLD_ELF_TEMPLATE_FILES elf/subprocess-unix.cc)
endif()

function(mold_instantiate_templates SOURCE TARGET)
set(PATH ${CMAKE_BINARY_DIR}/${SOURCE}.${TARGET}.cc)
if(NOT EXISTS ${PATH})
Expand All @@ -348,6 +359,7 @@ endforeach()
# Add other non-template source files.
target_sources(mold PRIVATE
common/compress.cc
common/crc32.cc
common/demangle.cc
common/filepath.cc
common/glob.cc
Expand Down
40 changes: 25 additions & 15 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "integers.h"

#include <array>
#include <atomic>
#include <bit>
#include <bitset>
Expand Down Expand Up @@ -404,9 +405,9 @@ void update_maximum(std::atomic<T> &atomic, u64 new_val, Compare cmp = {}) {
std::memory_order_relaxed));
}

template <typename T, typename U>
inline void append(std::vector<T> &vec1, std::vector<U> vec2) {
vec1.insert(vec1.end(), vec2.begin(), vec2.end());
template <typename T>
inline void append(std::vector<T> &x, const auto &y) {
x.insert(x.end(), y.begin(), y.end());
}

template <typename T>
Expand Down Expand Up @@ -534,14 +535,6 @@ std::string_view save_string(Context &ctx, const std::string &str) {
return {(char *)buf, str.size()};
}

inline bool remove_prefix(std::string_view &s, std::string_view prefix) {
if (s.starts_with(prefix)) {
s = s.substr(prefix.size());
return true;
}
return false;
}

static inline void pause() {
#if defined(__x86_64__)
asm volatile("pause");
Expand Down Expand Up @@ -734,15 +727,15 @@ template <typename Context>
class OutputFile {
public:
static std::unique_ptr<OutputFile<Context>>
open(Context &ctx, std::string path, i64 filesize, i64 perm);
open(Context &ctx, std::string path, i64 filesize, int perm);

virtual void close(Context &ctx) = 0;
virtual ~OutputFile() = default;

u8 *buf = nullptr;
std::vector<u8> buf2;
std::string path;
i64 fd = -1;
int fd = -1;
i64 filesize = 0;
bool is_mmapped = false;
bool is_unmapped = false;
Expand All @@ -755,7 +748,7 @@ class OutputFile {
template <typename Context>
class MallocOutputFile : public OutputFile<Context> {
public:
MallocOutputFile(Context &ctx, std::string path, i64 filesize, i64 perm)
MallocOutputFile(Context &ctx, std::string path, i64 filesize, int perm)
: OutputFile<Context>(path, filesize, false), ptr(new u8[filesize]),
perm(perm) {
this->buf = ptr.get();
Expand Down Expand Up @@ -791,7 +784,15 @@ class MallocOutputFile : public OutputFile<Context> {

private:
std::unique_ptr<u8[]> ptr;
i64 perm;
int perm;
};

template <typename Context>
class LockingOutputFile : public OutputFile<Context> {
public:
LockingOutputFile(Context &ctx, std::string path, int perm);
void resize(Context &ctx, i64 filesize);
void close(Context &ctx) override;
};

//
Expand Down Expand Up @@ -899,6 +900,13 @@ std::optional<std::string_view> demangle_rust(std::string_view name);
void acquire_global_lock();
void release_global_lock();

//
// crc32.cc
//

u32 compute_crc32(u32 crc, u8 *buf, i64 len);
std::vector<u8> crc32_solve(u32 current, u32 desired);

//
// compress.cc
//
Expand Down Expand Up @@ -963,6 +971,8 @@ class MappedFile {
public:
~MappedFile() { unmap(); }
void unmap();
void close_fd();
void reopen_fd(const std::string &path);

template <typename Context>
MappedFile *slice(Context &ctx, std::string name, u64 start, u64 size) {
Expand Down
60 changes: 60 additions & 0 deletions common/crc32.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "common.h"

#include <tbb/parallel_for_each.h>
#include <zlib.h>

namespace mold {

// This function "forges" a CRC. That is, given the current and a desired
// CRC32 value, crc32_solve() returns a binary blob to add to the end of
// the original data to yield the desired CRC. Trailing garbage is ignored
// by many bianry file formats, so you can create a file with a desired
// CRC using crc32_solve(). We need it for --separate-debug-file.
std::vector<u8> crc32_solve(u32 current, u32 desired) {
constexpr u32 poly = 0xedb88320;
u32 x = ~desired;

// Each iteration computes x = (x * x^-1) mod poly.
for (i64 i = 0; i < 32; i++) {
x = std::rotl(x, 1);
x ^= (x & 1) * (poly << 1);
}

x ^= ~current;

std::vector<u8> out(4);
out[0] = x;
out[1] = x >> 8;
out[2] = x >> 16;
out[3] = x >> 24;
return out;
}

// Compute a CRC for given data in parallel
u32 compute_crc32(u32 crc, u8 *buf, i64 len) {
struct Shard {
u8 *buf;
i64 len;
u32 crc;
};

constexpr i64 shard_size = 1024 * 1024; // 1 MiB
std::vector<Shard> shards;

while (len > 0) {
i64 sz = std::min(len, shard_size);
shards.push_back({buf, sz, 0});
buf += sz;
len -= sz;
}

tbb::parallel_for_each(shards.begin(), shards.end(), [](Shard &shard) {
shard.crc = crc32_z(0, shard.buf, shard.len);
});

for (Shard &shard : shards)
crc = crc32_combine(crc, shard.crc, shard.len);
return crc;
}

} // namespace mold
11 changes: 3 additions & 8 deletions common/filepath.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,9 @@ std::filesystem::path to_abs_path(std::filesystem::path path) {

// Returns the path of the mold executable itself
std::string get_self_path() {
#if __APPLE__
char path[8192];
u32 size = sizeof(path);
if (_NSGetExecutablePath(path, &size)) {
std::cerr << "_NSGetExecutablePath failed\n";
exit(1);
}
return path;
#if __APPLE__ || _WIN32
fprintf(stderr, "mold: get_self_path is not supported");
exit(1);
#elif __FreeBSD__
// /proc may not be mounted on FreeBSD. The proper way to get the
// current executable's path is to use sysctl(2).
Expand Down
Loading
Loading