mirror of
https://github.com/rui314/mold.git
synced 2026-11-09 13:01:26 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bda68f94a4 | ||
|
|
601b9e691c |
No files matched your search
@@ -1,13 +0,0 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
github: [rui314] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
||||
patreon: # Replace with a single Patreon username
|
||||
open_collective: # Replace with a single Open Collective username
|
||||
ko_fi: # Replace with a single Ko-fi username
|
||||
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||
liberapay: # Replace with a single Liberapay username
|
||||
issuehunt: # Replace with a single IssueHunt username
|
||||
otechie: # Replace with a single Otechie username
|
||||
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
|
||||
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
||||
@@ -1,51 +0,0 @@
|
||||
name: Build all tarballs
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 0 * * *'
|
||||
release:
|
||||
types: [ published ]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build-tarballs:
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- { target: x86_64, os: ubuntu-24.04 }
|
||||
- { target: aarch64, os: ubuntu-24.04-arm }
|
||||
- { target: arm, os: ubuntu-24.04-arm }
|
||||
- { target: riscv64, os: ubuntu-24.04 }
|
||||
- { target: ppc64le, os: ubuntu-24.04 }
|
||||
- { target: s390x, os: ubuntu-24.04 }
|
||||
- { target: loongarch64, os: ubuntu-24.04 }
|
||||
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Podman
|
||||
run: sudo apt-get update && sudo apt-get install -y podman qemu-user-static
|
||||
|
||||
- name: Login to GitHub Container Registry
|
||||
uses: redhat-actions/podman-login@v1
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.repository_owner }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Build a tarball
|
||||
run: ./dist.sh ${{ matrix.target }}
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: ${{ matrix.target }}
|
||||
path: dist/mold-*.tar.gz
|
||||
compression-level: 0
|
||||
@@ -1,44 +0,0 @@
|
||||
name: Build native tarballs
|
||||
|
||||
on:
|
||||
push:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build-tarballs:
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- { target: x86_64, os: ubuntu-24.04 }
|
||||
- { target: aarch64, os: ubuntu-24.04-arm }
|
||||
- { target: arm, os: ubuntu-24.04-arm }
|
||||
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Podman
|
||||
run: sudo apt-get update && sudo apt-get install -y podman qemu-user-static
|
||||
|
||||
- name: Login to GitHub Container Registry
|
||||
uses: redhat-actions/podman-login@v1
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.repository_owner }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Build a tarball
|
||||
run: ./dist.sh ${{ matrix.target }}
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: ${{ matrix.target }}
|
||||
path: dist/mold-*.tar.gz
|
||||
compression-level: 0
|
||||
+42
-135
@@ -1,149 +1,56 @@
|
||||
name: CI
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
env:
|
||||
UBSAN_OPTIONS: print_stacktrace=1:halt_on_error=1
|
||||
branches: [ main ]
|
||||
jobs:
|
||||
build-sanitizers:
|
||||
strategy:
|
||||
matrix:
|
||||
target:
|
||||
- ''
|
||||
- '-DMOLD_USE_ASAN=On'
|
||||
- '-DMOLD_USE_TSAN=On'
|
||||
runs-on: ubuntu-24.04
|
||||
build-clang:
|
||||
runs-on: ubuntu-20.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: rui314/setup-mold@staging
|
||||
- run: sudo ./install-build-deps.sh
|
||||
- name: build
|
||||
- uses: actions/checkout@v2
|
||||
- name: apt-get
|
||||
run: sudo dpkg --add-architecture i386 && sudo apt-get update && sudo apt-get install build-essential libstdc++-10-dev cmake clang libssl-dev zlib1g-dev libxxhash-dev libtbb-dev git bsdmainutils dwarfdump libc6-dev:i386 lib32gcc-10-dev libstdc++-10-dev-arm64-cross gcc-aarch64-linux-gnu g++-aarch64-linux-gnu qemu-user
|
||||
- name: ccache
|
||||
uses: hendrikmuhs/ccache-action@v1
|
||||
- name: make
|
||||
run: |
|
||||
sudo apt-get install -y clang-18 clang gcc-multilib gdb dwarfdump zstd
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang-18 -DCMAKE_CXX_COMPILER=clang++-18 ${{ matrix.target }} ..
|
||||
cmake --build . -j$(nproc)
|
||||
- run: cd build; ctest --output-on-failure -j$(nproc)
|
||||
- name: archive test results
|
||||
uses: actions/upload-artifact@v4
|
||||
if: failure()
|
||||
with:
|
||||
name: test-results-clang
|
||||
path: |
|
||||
build
|
||||
!build/CMakeFiles
|
||||
|
||||
build-multi-archs:
|
||||
runs-on: ubuntu-24.04
|
||||
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
|
||||
make CC=clang CXX=clang++ DEBUG=1 -j$(nproc)
|
||||
- name: make test
|
||||
run: |
|
||||
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
|
||||
make test -j$(nproc)
|
||||
build-gcc11:
|
||||
runs-on: ubuntu-20.04
|
||||
container: gcc:11.1.0
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: install-build-deps
|
||||
- uses: actions/checkout@v2
|
||||
- name: apt-get
|
||||
run: dpkg --add-architecture i386 && apt-get update && apt-get install -y sudo libssl-dev zlib1g-dev libxxhash-dev cmake clang dwarfdump bsdextrautils libgcc-9-dev gcc-aarch64-linux-gnu g++-aarch64-linux-gnu libc6-dev:i386 lib32gcc-10-dev qemu-user
|
||||
- name: ccache
|
||||
uses: hendrikmuhs/ccache-action@v1
|
||||
- name: make
|
||||
run: |
|
||||
sudo ./install-build-deps.sh
|
||||
sudo ./install-cross-tools.sh
|
||||
sudo .github/workflows/install-extras.sh
|
||||
- name: build
|
||||
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
|
||||
make CC=gcc CXX=g++ DEBUG=1 -j$(nproc)
|
||||
- name: make test
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake --build . -j$(nproc)
|
||||
- run: cd build; ctest --output-on-failure -j$(nproc)
|
||||
- name: archive test results
|
||||
uses: actions/upload-artifact@v4
|
||||
if: failure()
|
||||
with:
|
||||
name: test-results-multi-archs
|
||||
path: |
|
||||
build
|
||||
!build/CMakeFiles
|
||||
|
||||
build-distros:
|
||||
strategy:
|
||||
matrix:
|
||||
distro:
|
||||
- alpine
|
||||
- archlinux
|
||||
- debian:11 # GCC 10 and CMake 3.18 - the minimum supported by mold
|
||||
- fedora
|
||||
- gentoo/stage3
|
||||
- opensuse/tumbleweed
|
||||
- ubuntu:22.04
|
||||
- ubuntu:25.04
|
||||
runs-on: ubuntu-latest
|
||||
container: ${{ matrix.distro }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- run: ./install-build-deps.sh
|
||||
- name: build
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake --build . -j$(nproc)
|
||||
- run: cd build; ctest --output-on-failure -j$(nproc)
|
||||
- name: archive test results
|
||||
uses: actions/upload-artifact@v4
|
||||
if: failure()
|
||||
with:
|
||||
name: test-results-${{ matrix.distro }}
|
||||
path: |
|
||||
build
|
||||
!build/CMakeFiles
|
||||
|
||||
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
|
||||
make test -j$(nproc)
|
||||
build-macos:
|
||||
runs-on: macos-latest
|
||||
runs-on: macos-11
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: build
|
||||
- uses: actions/checkout@v2
|
||||
- name: brew
|
||||
run: brew install xxhash
|
||||
- name: ccache
|
||||
uses: hendrikmuhs/ccache-action@v1
|
||||
- name: make
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake --build . -j$(sysctl -n hw.physicalcpu)
|
||||
|
||||
build-windows:
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: build
|
||||
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
|
||||
make DEBUG=1 -j$(sysctl -n hw.logicalcpu)
|
||||
- name: make test
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -T clangcl ..
|
||||
cmake --build . -j $Env:NUMBER_OF_PROCESSORS
|
||||
|
||||
build-msys:
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- 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 ..
|
||||
cmake --build . -j $(nproc)
|
||||
|
||||
build-freebsd:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Build and test
|
||||
uses: vmactions/freebsd-vm@v1
|
||||
with:
|
||||
usesh: true
|
||||
run: |
|
||||
./install-build-deps.sh
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake --build . -j$(nproc)
|
||||
ctest --output-on-failure -j$(nproc)
|
||||
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
|
||||
make test -j$(sysctl -n hw.logicalcpu)
|
||||
@@ -1,61 +0,0 @@
|
||||
#!/bin/bash -x
|
||||
|
||||
apt-get update
|
||||
apt-get install -y wget xz-utils
|
||||
|
||||
# Install a 32-bit RISC-V toolchain
|
||||
mkdir /rv32
|
||||
wget -O- -q https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2025.01.20/riscv32-glibc-ubuntu-22.04-gcc-nightly-2025.01.20-nightly.tar.xz | tar -C /rv32 --strip-components=1 --xz -xf -
|
||||
|
||||
ln -sf /rv32/sysroot /usr/riscv32-linux-gnu
|
||||
echo '/rv32/bin/riscv32-unknown-linux-gnu-gcc -L/usr/riscv32-linux-gnu "$@"' > /usr/bin/riscv32-linux-gnu-gcc
|
||||
echo '/rv32/bin/riscv32-unknown-linux-gnu-g++ -L/usr/riscv32-linux-gnu "$@"' > /usr/bin/riscv32-linux-gnu-g++
|
||||
chmod 755 /usr/bin/riscv32-linux-gnu-{gcc,g++}
|
||||
|
||||
for i in objdump objcopy strip; do
|
||||
ln -sf /rv32/bin/riscv32-unknown-linux-gnu-$i /usr/bin/riscv32-linux-gnu-$i
|
||||
done
|
||||
|
||||
# Install a LoongArch toolchain
|
||||
mkdir /larch
|
||||
wget -O- -q https://github.com/loongson/build-tools/releases/download/2025.02.21/x86_64-cross-tools-loongarch64-binutils_2.44-gcc_14.2.0-glibc_2.41.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
|
||||
|
||||
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
|
||||
|
||||
wget -O /usr/local/bin/qemu-loongarch64 -q https://github.com/loongson/build-tools/releases/download/2024.11.01/qemu-loongarch64
|
||||
chmod 755 /usr/local/bin/qemu-loongarch64
|
||||
|
||||
# Install ARM64 big-endian toolchain
|
||||
mkdir /aarch64be
|
||||
wget -O- -q https://sources.buildroot.net/toolchain-external-arm-aarch64-be/arm-gnu-toolchain-13.3.rel1-x86_64-aarch64_be-none-linux-gnu.tar.xz | tar -C /aarch64be --strip-components=1 --xz -xf -
|
||||
|
||||
ln -sf /aarch64be/aarch64_be-none-linux-gnu/libc /usr/aarch64_be-linux-gnu
|
||||
echo '/aarch64be/bin/aarch64_be-none-linux-gnu-gcc -L/usr/aarch64_be-linux-gnu "$@"' > /usr/bin/aarch64_be-linux-gnu-gcc
|
||||
echo '/aarch64be/bin/aarch64_be-none-linux-gnu-g++ -L/usr/aarch64_be-linux-gnu "$@"' > /usr/bin/aarch64_be-linux-gnu-g++
|
||||
chmod 755 /usr/bin/aarch64_be-linux-gnu-{gcc,g++}
|
||||
|
||||
for i in objdump objcopy strip; do
|
||||
ln -sf /aarch64be/bin/aarch64_be-none-linux-gnu-$i /usr/bin/aarch64_be-linux-gnu-$i
|
||||
done
|
||||
|
||||
# Install SH4 big-endian toolchain
|
||||
mkdir /sh4aeb
|
||||
wget -O- -q https://toolchains.bootlin.com/downloads/releases/toolchains/sh-sh4aeb/tarballs/sh-sh4aeb--glibc--stable-2024.05-1.tar.xz | tar -C /sh4aeb --strip-components=1 --xz -xf -
|
||||
ln -sf /sh4aeb/sh4aeb-buildroot-linux-gnu/sysroot /usr/sh4aeb-linux-gnu
|
||||
echo '/sh4aeb/bin/sh4aeb-linux-gcc -L/usr/sh4aeb-linux-gnu "$@"' > /usr/bin/sh4aeb-linux-gnu-gcc
|
||||
echo '/sh4aeb/bin/sh4aeb-linux-g++ -L/usr/sh4aeb-linux-gnu "$@"' > /usr/bin/sh4aeb-linux-gnu-g++
|
||||
chmod 755 /usr/bin/sh4aeb-linux-gnu-{gcc,g++}
|
||||
|
||||
for i in objdump objcopy strip; do
|
||||
ln -sf /sh4aeb/bin/sh4aeb-linux-$i /usr/bin/sh4aeb-linux-gnu-$i
|
||||
done
|
||||
|
||||
# Install Intel SDE CPU emulator for CET-related tests
|
||||
mkdir /sde
|
||||
wget -O- -q https://downloadmirror.intel.com/843185/sde-external-9.48.0-2024-11-25-lin.tar.xz | tar -C /sde --strip-components=1 --xz -xf -
|
||||
ln -s /sde/sde64 /usr/bin
|
||||
@@ -1,33 +0,0 @@
|
||||
name: Update manpage
|
||||
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- 'docs/mold.md'
|
||||
branches:
|
||||
- main
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
update-manpage:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install 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
|
||||
|
||||
- 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)
|
||||
+8
-6
@@ -1,11 +1,13 @@
|
||||
*.o
|
||||
*.so
|
||||
*~
|
||||
**/.DS_Store
|
||||
.cache
|
||||
core
|
||||
gentoo
|
||||
/mold-*.tar.gz
|
||||
/build*
|
||||
compile_commands.json
|
||||
/mold
|
||||
/dist
|
||||
/test/tmp
|
||||
options.inc
|
||||
core
|
||||
ld
|
||||
ld64.mold
|
||||
gentoo
|
||||
out
|
||||
-458
@@ -1,458 +0,0 @@
|
||||
# You can customize a build by specifying CMake options. An option may be
|
||||
# given in the -Dvariable=value form. For a boolean variable, `ON` or `1`
|
||||
# means true, while `OFF` or `0` means false.
|
||||
#
|
||||
# Here are a couple of common cmake options:
|
||||
#
|
||||
# -DCMAKE_C_COMPILER=<command-name>
|
||||
#
|
||||
# Specifies the C compiler name to use. The default value is `cc`.
|
||||
#
|
||||
# -DCMAKE_CXX_COMPILER=<command-name>
|
||||
#
|
||||
# Specifies the C++ compiler name to use. The default value is `c++`.
|
||||
#
|
||||
# -DCMAKE_INSTALL_PREFIX=<directory>
|
||||
#
|
||||
# Specifies the install target directory. The default value is `/usr/local`.
|
||||
#
|
||||
# -DCMAKE_BUILD_TYPE=[Debug | Release | RelWithDebInfo | MinSizeRel]
|
||||
#
|
||||
# Specifies the build type. The default is `Release`, which is the right
|
||||
# option unless you are debugging mold.
|
||||
#
|
||||
# An example of a cmake command line is shown below:
|
||||
#
|
||||
# $ cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_CXX_COMPILER=g++-12 ..
|
||||
#
|
||||
# where `..` refers to this directory.
|
||||
#
|
||||
# With cmake, you may run `cmake --install .` instead of `make install` to
|
||||
# install build artifacts to system directories. If you want to install
|
||||
# artifacts to a temporary target directory, run `cmake --install . --prefix
|
||||
# <dir>`.
|
||||
#
|
||||
# You can see the current cmake variables and their values by running
|
||||
# `cmake -N -L .` in a build directory.
|
||||
#
|
||||
# Note that in this file, we provide various dials and knobs to configure
|
||||
# how to build mold. However, as a policy, we do not provide a way to
|
||||
# enable/disable any individual mold's feature. In other words, we do not
|
||||
# provide options like `--enable-foo` or `--disable-foo`. The motivation
|
||||
# behind this is build reproducibility. We want to guarantee that all builds
|
||||
# of the mold linker of the same version will have the exact same set of
|
||||
# features and behave exactly the same.
|
||||
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(mold VERSION 2.37.1)
|
||||
|
||||
include(CMakeDependentOption)
|
||||
include(CheckSymbolExists)
|
||||
include(GNUInstallDirs)
|
||||
|
||||
add_executable(mold)
|
||||
target_compile_features(mold PRIVATE cxx_std_20)
|
||||
|
||||
if(MINGW)
|
||||
target_link_libraries(mold PRIVATE dl)
|
||||
else()
|
||||
target_link_libraries(mold PRIVATE ${CMAKE_DL_LIBS})
|
||||
endif()
|
||||
|
||||
# Build mold itself using mold if -DMOLD_USE_MOLD=ON
|
||||
option(MOLD_USE_MOLD "Use mold to build mold" OFF)
|
||||
if(MOLD_USE_MOLD)
|
||||
target_link_options(mold PRIVATE -fuse-ld=mold -Wl,--gdb-index)
|
||||
|
||||
if(CMAKE_BUILD_TYPE MATCHES "^Rel")
|
||||
target_link_options(mold PRIVATE -Wl,--gc-sections,--icf=safe)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(NOT "${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC")
|
||||
target_compile_options(mold PRIVATE
|
||||
-fno-exceptions
|
||||
-fno-unwind-tables
|
||||
-fno-asynchronous-unwind-tables
|
||||
-ffunction-sections
|
||||
-fdata-sections
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wno-sign-compare
|
||||
-Wno-unused-function
|
||||
-Wno-unused-parameter
|
||||
-Wno-missing-field-initializers
|
||||
-ggnu-pubnames)
|
||||
endif()
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
target_compile_options(mold PRIVATE -D_GLIBCXX_ASSERTIONS)
|
||||
endif()
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
|
||||
set(OPENBSD ON)
|
||||
endif()
|
||||
|
||||
# Build mold with -flto if -DMOLD_LTO=ON
|
||||
option(MOLD_LTO "Build mold with link-time optimization enabled")
|
||||
if(MOLD_LTO)
|
||||
set_property(TARGET mold PROPERTY INTERPROCEDURAL_OPTIMIZATION ON)
|
||||
endif()
|
||||
|
||||
# Enable AddressSanitizer if -DMOLD_USE_ASAN=ON
|
||||
option(MOLD_USE_ASAN "Build mold with AddressSanitizer" OFF)
|
||||
if(MOLD_USE_ASAN)
|
||||
target_compile_options(mold PRIVATE -fsanitize=address -fsanitize=undefined)
|
||||
target_link_options(mold PRIVATE -fsanitize=address -fsanitize=undefined)
|
||||
endif()
|
||||
|
||||
# Enabled ThreadSanitizer if -DMOLD_USE_TSAN=ON
|
||||
option(MOLD_USE_TSAN "Build mold with ThreadSanitizer" OFF)
|
||||
if(MOLD_USE_TSAN)
|
||||
target_compile_options(mold PRIVATE -fsanitize=thread)
|
||||
target_link_options(mold PRIVATE -fsanitize=thread)
|
||||
endif()
|
||||
|
||||
# Statically-link libstdc++ if -DMOLD_MOSTLY_STATIC=ON.
|
||||
#
|
||||
# This option is intended to be used by `./dist.sh` script to create a
|
||||
# mold binary that works on various Linux distros. You probably don't
|
||||
# need nor want to set this to ON.
|
||||
option(MOLD_MOSTLY_STATIC "Statically link libstdc++ and some other libraries" OFF)
|
||||
if(MOLD_MOSTLY_STATIC)
|
||||
target_link_options(mold PRIVATE -static-libstdc++)
|
||||
endif()
|
||||
|
||||
# Find zlib. If libz.so is not found, we compile a bundled one and
|
||||
# statically-link it to mold.
|
||||
find_package(ZLIB QUIET)
|
||||
if(ZLIB_FOUND AND NOT MOLD_MOSTLY_STATIC)
|
||||
target_link_libraries(mold PRIVATE ZLIB::ZLIB)
|
||||
else()
|
||||
set(ZLIB_BUILD_EXAMPLES OFF CACHE INTERNAL "")
|
||||
add_subdirectory(third-party/zlib EXCLUDE_FROM_ALL)
|
||||
target_include_directories(zlibstatic INTERFACE third-party/zlib
|
||||
$<TARGET_PROPERTY:zlibstatic,BINARY_DIR>)
|
||||
target_link_libraries(mold PRIVATE zlibstatic)
|
||||
endif()
|
||||
|
||||
# Find BLAKE3 cryptographic hash library. Just like zlib, if libblkae3.so
|
||||
# is not found, we compile a bundled one and statically-link it to mold.
|
||||
find_package(BLAKE3 QUIET)
|
||||
if(BLAKE3_FOUND AND NOT MOLD_MOSTLY_STATIC)
|
||||
target_link_libraries(mold PRIVATE BLAKE3::blake3)
|
||||
else()
|
||||
function(mold_add_blake3)
|
||||
set(BUILD_SHARED_LIBS OFF)
|
||||
add_subdirectory(third-party/blake3/c EXCLUDE_FROM_ALL)
|
||||
target_link_libraries(mold PRIVATE blake3)
|
||||
target_include_directories(mold PUBLIC third-party/blake3/c)
|
||||
endfunction()
|
||||
|
||||
mold_add_blake3()
|
||||
endif()
|
||||
|
||||
# Find zstd compression library. If zstd.h is not found, we compile a
|
||||
# bundled one and statically-link it to mold.
|
||||
include(CheckIncludeFile)
|
||||
check_include_file(zstd.h HAVE_ZSTD_H)
|
||||
|
||||
if(HAVE_ZSTD_H AND NOT MOLD_MOSTLY_STATIC)
|
||||
target_link_libraries(mold PRIVATE zstd)
|
||||
else()
|
||||
add_subdirectory(third-party/zstd/build/cmake EXCLUDE_FROM_ALL)
|
||||
target_compile_definitions(libzstd_static PRIVATE
|
||||
ZSTD_BUILD_STATIC=1
|
||||
ZSTD_BUILD_SHARED=0
|
||||
ZSTD_BUILD_PROGRAMS=0
|
||||
ZSTD_MULTITHREAD_SUPPORT=0
|
||||
ZSTD_BUILD_TESTS=0)
|
||||
target_include_directories(mold PUBLIC third-party/zstd/lib)
|
||||
target_link_libraries(mold PRIVATE libzstd_static)
|
||||
endif()
|
||||
|
||||
# Find mimalloc. mimalloc is an alternative malloc implementation
|
||||
# optimized for multi-threaded applications.
|
||||
#
|
||||
# If you want to use the usual libc's malloc, pass -DMOLD_USE_MIMALLOC=OFF.
|
||||
#
|
||||
# We enable mimalloc by default for 64-bit targets. It doesn't seem to
|
||||
# be stable on 32-bit targets.
|
||||
cmake_dependent_option(
|
||||
MOLD_USE_MIMALLOC "Use mimalloc" ON
|
||||
"CMAKE_SIZEOF_VOID_P EQUAL 8; NOT APPLE; NOT ANDROID; NOT OPENBSD; NOT MOLD_USE_ASAN; NOT MOLD_USE_TSAN" OFF)
|
||||
|
||||
cmake_dependent_option(
|
||||
MOLD_USE_SYSTEM_MIMALLOC "Use system or vendored mimalloc" OFF
|
||||
MOLD_USE_MIMALLOC OFF)
|
||||
|
||||
# By default, we build a bundled mimalloc and statically-link it to
|
||||
# mold. If you want to dynamically link to the system's
|
||||
# libmimalloc.so, pass -DMOLD_USE_SYSTEM_MIMALLOC=ON.
|
||||
if(MOLD_USE_MIMALLOC)
|
||||
if(MOLD_USE_SYSTEM_MIMALLOC)
|
||||
find_package(mimalloc REQUIRED)
|
||||
target_link_libraries(mold PRIVATE mimalloc)
|
||||
else()
|
||||
function(mold_add_mimalloc)
|
||||
set(MI_BUILD_STATIC ON CACHE INTERNAL "")
|
||||
set(MI_BUILD_TESTS OFF CACHE INTERNAL "")
|
||||
add_subdirectory(third-party/mimalloc EXCLUDE_FROM_ALL)
|
||||
target_compile_definitions(mimalloc-static PRIVATE MI_USE_ENVIRON=0)
|
||||
target_link_libraries(mold PRIVATE mimalloc-static)
|
||||
endfunction()
|
||||
|
||||
mold_add_mimalloc()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Find TBB. TBB (OneTBB or Intel TBB) is a high-level threading library.
|
||||
# Use of this library is mandatory.
|
||||
#
|
||||
# By default, we build a bundled one and statically-link the library
|
||||
# to mold. If you want to link to the system's libtbb2.so, pass
|
||||
# -DMOLD_USE_SYSTEM_TBB=ON.
|
||||
option(MOLD_USE_SYSTEM_TBB "Use system or vendored TBB" OFF)
|
||||
if(MOLD_USE_SYSTEM_TBB)
|
||||
find_package(TBB REQUIRED)
|
||||
target_link_libraries(mold PRIVATE TBB::tbb)
|
||||
else()
|
||||
function(mold_add_tbb)
|
||||
set(BUILD_SHARED_LIBS OFF)
|
||||
set(TBB_TEST OFF CACHE INTERNAL "")
|
||||
set(TBB_STRICT OFF CACHE INTERNAL "")
|
||||
add_subdirectory(third-party/tbb EXCLUDE_FROM_ALL)
|
||||
target_compile_definitions(tbb PRIVATE __TBB_DYNAMIC_LOAD_ENABLED=0)
|
||||
target_link_libraries(mold PRIVATE TBB::tbb)
|
||||
endfunction()
|
||||
|
||||
mold_add_tbb()
|
||||
endif()
|
||||
|
||||
# We always use Clang to build mold on Windows. MSVC can't compile mold.
|
||||
if(WIN32)
|
||||
if(MSVC AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
||||
message(FATAL_ERROR
|
||||
"Your compiler is not supported; install Clang from Visual Studio Installer and re-run cmake with '-T clangcl'")
|
||||
endif()
|
||||
|
||||
target_compile_definitions(mold PRIVATE NOGDI NOMINMAX)
|
||||
if(MINGW)
|
||||
target_compile_definitions(mold PRIVATE _WIN32_WINNT=0xA00)
|
||||
target_link_libraries(mold PRIVATE bcrypt)
|
||||
endif()
|
||||
else()
|
||||
include(CheckLibraryExists)
|
||||
check_library_exists(m pow "" LIBM_FOUND)
|
||||
if(LIBM_FOUND)
|
||||
target_link_libraries(mold PRIVATE m)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Build mold-wrapper.so
|
||||
if(NOT APPLE AND NOT WIN32)
|
||||
add_library(mold-wrapper SHARED)
|
||||
install(TARGETS mold-wrapper DESTINATION ${CMAKE_INSTALL_LIBDIR}/mold)
|
||||
|
||||
# Remove the default `lib` prefix
|
||||
set_target_properties(mold-wrapper PROPERTIES PREFIX "")
|
||||
target_link_libraries(mold-wrapper PRIVATE ${CMAKE_DL_LIBS})
|
||||
target_sources(mold-wrapper PRIVATE src/mold-wrapper.c)
|
||||
endif()
|
||||
|
||||
# If atomics doesn't work by default, add -latomic.
|
||||
# We need the flag on riscv, armv6 and m68k.
|
||||
include(CheckCXXSourceCompiles)
|
||||
check_cxx_source_compiles("#include <atomic>
|
||||
int main() {
|
||||
std::atomic_uint8_t a;
|
||||
std::atomic_uint16_t b;
|
||||
std::atomic_uint32_t c;
|
||||
std::atomic_uint64_t d;
|
||||
return ++a + ++b + ++c + ++d;
|
||||
}" HAVE_FULL_ATOMIC_SUPPORT)
|
||||
|
||||
if(NOT HAVE_FULL_ATOMIC_SUPPORT)
|
||||
target_link_libraries(mold PRIVATE atomic)
|
||||
endif()
|
||||
|
||||
# Add -pthread
|
||||
if(NOT APPLE AND NOT MSVC)
|
||||
target_compile_options(mold PRIVATE -pthread)
|
||||
target_link_options(mold PRIVATE -pthread)
|
||||
endif()
|
||||
|
||||
check_symbol_exists(madvise sys/mman.h HAVE_MADVISE)
|
||||
check_symbol_exists(uname sys/utsname.h HAVE_UNAME)
|
||||
|
||||
# Create a .cc file containing the current git hash for `mold --version`.
|
||||
add_custom_target(git_hash
|
||||
COMMAND ${CMAKE_COMMAND}
|
||||
-DSOURCE_DIR=${CMAKE_SOURCE_DIR}
|
||||
-DOUTPUT_FILE=${CMAKE_BINARY_DIR}/git-hash.cc
|
||||
-P ${CMAKE_SOURCE_DIR}/lib/update-git-hash.cmake
|
||||
DEPENDS lib/update-git-hash.cmake
|
||||
BYPRODUCTS git-hash.cc
|
||||
VERBATIM)
|
||||
|
||||
add_dependencies(mold git_hash)
|
||||
|
||||
# Create config.h file
|
||||
configure_file(lib/config.h.in config.h)
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
# Almost all functions are template in mold which take a target type
|
||||
# (e.g. X86_64) as its type parameter. Since we suport more than 10
|
||||
# targets, compiling a single source file for all the targets is very
|
||||
# slow.
|
||||
#
|
||||
# As a workaround, we create a .cc file for each target and spawn many
|
||||
# compiler instances. This is hacky but greatly reduces compile time
|
||||
# on a multicore machine.
|
||||
list(APPEND MOLD_ELF_TARGETS
|
||||
X86_64 I386 ARM64LE ARM64BE ARM32 RV32LE RV32BE RV64LE RV64BE PPC32
|
||||
PPC64V1 PPC64V2 S390X SPARC64 M68K SH4LE SH4BE LOONGARCH32 LOONGARCH64 ARC)
|
||||
|
||||
list(APPEND MOLD_ELF_TEMPLATE_FILES
|
||||
src/arch-arm64.cc
|
||||
src/arch-loongarch.cc
|
||||
src/arch-riscv.cc
|
||||
src/arch-sh4.cc
|
||||
src/cmdline.cc
|
||||
src/filetype.cc
|
||||
src/gc-sections.cc
|
||||
src/gdb-index.cc
|
||||
src/icf.cc
|
||||
src/input-files.cc
|
||||
src/input-sections.cc
|
||||
src/linker-script.cc
|
||||
src/main.cc
|
||||
src/mapfile.cc
|
||||
src/output-chunks.cc
|
||||
src/passes.cc
|
||||
src/relocatable.cc
|
||||
src/shrink-sections.cc
|
||||
src/thunks.cc
|
||||
src/tls.cc
|
||||
)
|
||||
|
||||
if(WIN32 AND NOT MINGW)
|
||||
list(APPEND MOLD_ELF_TEMPLATE_FILES src/lto-win32.cc)
|
||||
else()
|
||||
list(APPEND MOLD_ELF_TEMPLATE_FILES src/lto-unix.cc)
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
list(APPEND MOLD_ELF_TEMPLATE_FILES
|
||||
src/output-file-win32.cc
|
||||
src/subprocess-win32.cc
|
||||
)
|
||||
else()
|
||||
list(APPEND MOLD_ELF_TEMPLATE_FILES
|
||||
src/output-file-unix.cc
|
||||
src/subprocess-unix.cc
|
||||
)
|
||||
endif()
|
||||
|
||||
function(mold_instantiate_templates SOURCE TARGET)
|
||||
set(PATH ${CMAKE_BINARY_DIR}/${SOURCE}.${TARGET}.cc)
|
||||
if(NOT EXISTS ${PATH})
|
||||
file(WRITE ${PATH} "#define MOLD_${TARGET} 1
|
||||
#define MOLD_TARGET ${TARGET}
|
||||
#include \"${CMAKE_SOURCE_DIR}/${SOURCE}\"
|
||||
")
|
||||
endif()
|
||||
target_sources(mold PRIVATE ${PATH})
|
||||
endfunction()
|
||||
|
||||
foreach (SOURCE IN LISTS MOLD_ELF_TEMPLATE_FILES)
|
||||
foreach(TARGET IN LISTS MOLD_ELF_TARGETS)
|
||||
mold_instantiate_templates(${SOURCE} ${TARGET})
|
||||
endforeach()
|
||||
endforeach()
|
||||
|
||||
# Add other non-template source files.
|
||||
target_sources(mold PRIVATE
|
||||
git-hash.cc
|
||||
lib/compress.cc
|
||||
lib/crc32.cc
|
||||
lib/demangle.cc
|
||||
lib/filepath.cc
|
||||
lib/glob.cc
|
||||
lib/hyperloglog.cc
|
||||
lib/mimalloc.cc
|
||||
lib/multi-glob.cc
|
||||
lib/perf.cc
|
||||
lib/random.cc
|
||||
lib/tar.cc
|
||||
src/arch-arc.cc
|
||||
src/arch-arm32.cc
|
||||
src/arch-i386.cc
|
||||
src/arch-m68k.cc
|
||||
src/arch-ppc32.cc
|
||||
src/arch-ppc64v1.cc
|
||||
src/arch-ppc64v2.cc
|
||||
src/arch-s390x.cc
|
||||
src/arch-sparc64.cc
|
||||
src/arch-x86-64.cc
|
||||
src/config.cc
|
||||
src/elf.cc
|
||||
third-party/rust-demangle/rust-demangle.c
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
target_sources(mold PRIVATE
|
||||
lib/jobs-win32.cc
|
||||
lib/mapped-file-win32.cc
|
||||
lib/signal-win32.cc
|
||||
)
|
||||
else()
|
||||
target_sources(mold PRIVATE
|
||||
lib/jobs-unix.cc
|
||||
lib/mapped-file-unix.cc
|
||||
lib/signal-unix.cc
|
||||
)
|
||||
endif()
|
||||
|
||||
include(CTest)
|
||||
|
||||
if(BUILD_TESTING)
|
||||
# Create the ld symlinks required for testing
|
||||
if(NOT WIN32)
|
||||
add_custom_command(
|
||||
TARGET mold POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E create_symlink mold ld
|
||||
BYPRODUCTS ld
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
VERBATIM)
|
||||
endif()
|
||||
|
||||
if(${UNIX})
|
||||
add_subdirectory(test)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(NOT CMAKE_SKIP_INSTALL_RULES)
|
||||
install(TARGETS mold RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
install(FILES docs/mold.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1/)
|
||||
install(FILES LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR})
|
||||
|
||||
function(mold_install_relative_symlink OLD NEW)
|
||||
install(CODE "
|
||||
get_filename_component(PREFIX_ABS \${CMAKE_INSTALL_PREFIX}/ ABSOLUTE)
|
||||
get_filename_component(OLD_ABS ${OLD} ABSOLUTE BASE_DIR \${PREFIX_ABS})
|
||||
get_filename_component(NEW_ABS ${NEW} ABSOLUTE BASE_DIR \${PREFIX_ABS})
|
||||
get_filename_component(NEW_DIR \${NEW_ABS} DIRECTORY)
|
||||
file(RELATIVE_PATH OLD_REL \${NEW_DIR} \${OLD_ABS})
|
||||
message(STATUS \"Installing symlink: \$ENV{DESTDIR}\${NEW_ABS} -> \${OLD_REL}\")
|
||||
file(MAKE_DIRECTORY \$ENV{DESTDIR}\${NEW_DIR})
|
||||
file(CREATE_LINK \${OLD_REL} \$ENV{DESTDIR}\${NEW_ABS} SYMBOLIC)")
|
||||
endfunction()
|
||||
|
||||
mold_install_relative_symlink(${CMAKE_INSTALL_BINDIR}/mold${CMAKE_EXECUTABLE_SUFFIX}
|
||||
${CMAKE_INSTALL_LIBEXECDIR}/mold/ld${CMAKE_EXECUTABLE_SUFFIX})
|
||||
mold_install_relative_symlink(${CMAKE_INSTALL_BINDIR}/mold${CMAKE_EXECUTABLE_SUFFIX}
|
||||
${CMAKE_INSTALL_BINDIR}/ld.mold${CMAKE_EXECUTABLE_SUFFIX})
|
||||
mold_install_relative_symlink(${CMAKE_INSTALL_MANDIR}/man1/mold.1
|
||||
${CMAKE_INSTALL_MANDIR}/man1/ld.mold.1)
|
||||
endif()
|
||||
@@ -0,0 +1,54 @@
|
||||
# Contributing to mold
|
||||
|
||||
Summary: When you send a patch, you must agree that the patch is
|
||||
released under the dual license of the GNU AGPLv3 and the MIT license.
|
||||
As an indication of agreement, please add a `Signed-off-by` line to
|
||||
your commit message using `git commit -s`.
|
||||
|
||||
Here is the long official version:
|
||||
|
||||
mold is currently licensed under the GNU AGPLv3, but I want to relicense
|
||||
it under a more liberal license in the future. To make this possible,
|
||||
I want all patches to be submitted under the dual license of the GNU
|
||||
AGPLv3 and the MIT license (the "Dual License") from the beginning.
|
||||
|
||||
To make it clear that contributors have agreed to release their
|
||||
patches under the Dual License, I introduced a sign-off procedure just
|
||||
like the Linux kernel project did. The sign-off is a simple line at
|
||||
the end of the git commit message, which certifies that you wrote it
|
||||
or otherwise have the right to pass it on as an open-source patch. The
|
||||
rules are pretty simple: if you can certify the below:
|
||||
|
||||
> ## Developer's Certificate of Origin
|
||||
>
|
||||
> By making a contribution to this project, I certify that:
|
||||
>
|
||||
> 1. The contribution was created in whole or in part by me and I
|
||||
> have the right to submit it under the Dual License; or
|
||||
>
|
||||
> 2. The contribution is based upon previous work that, to the best
|
||||
> of my knowledge, is covered under the the Dual License and I
|
||||
> have the right under the Dual License to submit that work with
|
||||
> modifications, whether created in whole or in part by me, under
|
||||
> the Dual License (unless I am permitted to submit under
|
||||
> different licenses), as indicated in the file; or
|
||||
>
|
||||
> 3. The contribution was provided directly to me by some other
|
||||
> person who certified (1), (2) or (3) and I have not modified it.
|
||||
>
|
||||
> 4. I understand and agree that this project and the contribution
|
||||
> are public and that a record of the contribution (including all
|
||||
> personal information I submit with it, including my sign-off) is
|
||||
> maintained indefinitely and may be redistributed consistent with
|
||||
> this project or the open source license(s) involved.
|
||||
|
||||
then you just add a line saying:
|
||||
|
||||
```
|
||||
Signed-off-by: Random J Developer <[email protected]>
|
||||
```
|
||||
|
||||
using your real name (sorry, no pseudonyms or anonymous contributions.)
|
||||
This will be done for you automatically if you use ``git commit -s``.
|
||||
Reverts should also include "Signed-off-by". ``git revert -s`` does that
|
||||
for you.
|
||||
@@ -1,21 +1,671 @@
|
||||
MIT License
|
||||
Note: I'm looking for a sponsor who wants to purchase the copyright
|
||||
of this work and relicense it under a more liberal license such as
|
||||
the MIT license. For now, mold is released under the GNU AGPL v3.
|
||||
|
||||
Copyright (c) 2023 Rui Ueyama
|
||||
If you just want to use mold but do not want to use AGPL software
|
||||
in your organization, a commercial license is available. Please
|
||||
contact [email protected].
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
--
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
Version 3, 19 November 2007
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU Affero General Public License is a free, copyleft license for
|
||||
software and other kinds of works, specifically designed to ensure
|
||||
cooperation with the community in the case of network server software.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
our General Public Licenses are intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
Developers that use our General Public Licenses protect your rights
|
||||
with two steps: (1) assert copyright on the software, and (2) offer
|
||||
you this License which gives you legal permission to copy, distribute
|
||||
and/or modify the software.
|
||||
|
||||
A secondary benefit of defending all users' freedom is that
|
||||
improvements made in alternate versions of the program, if they
|
||||
receive widespread use, become available for other developers to
|
||||
incorporate. Many developers of free software are heartened and
|
||||
encouraged by the resulting cooperation. However, in the case of
|
||||
software used on network servers, this result may fail to come about.
|
||||
The GNU General Public License permits making a modified version and
|
||||
letting the public access it on a server without ever releasing its
|
||||
source code to the public.
|
||||
|
||||
The GNU Affero General Public License is designed specifically to
|
||||
ensure that, in such cases, the modified source code becomes available
|
||||
to the community. It requires the operator of a network server to
|
||||
provide the source code of the modified version running there to the
|
||||
users of that server. Therefore, public use of a modified version, on
|
||||
a publicly accessible server, gives the public access to the source
|
||||
code of the modified version.
|
||||
|
||||
An older license, called the Affero General Public License and
|
||||
published by Affero, was designed to accomplish similar goals. This is
|
||||
a different license, not a version of the Affero GPL, but Affero has
|
||||
released a new version of the Affero GPL which permits relicensing under
|
||||
this license.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, if you modify the
|
||||
Program, your modified version must prominently offer all users
|
||||
interacting with it remotely through a computer network (if your version
|
||||
supports such interaction) an opportunity to receive the Corresponding
|
||||
Source of your version by providing access to the Corresponding Source
|
||||
from a network server at no charge, through some standard or customary
|
||||
means of facilitating copying of software. This Corresponding Source
|
||||
shall include the Corresponding Source for any work covered by version 3
|
||||
of the GNU General Public License that is incorporated pursuant to the
|
||||
following paragraph.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the work with which it is combined will remain governed by version
|
||||
3 of the GNU General Public License.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU Affero General Public License from time to time. Such new versions
|
||||
will be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU Affero General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU Affero General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU Affero General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If your software can interact with users remotely through a computer
|
||||
network, you should also make sure that it provides a way for users to
|
||||
get its source. For example, if your program is a web application, its
|
||||
interface could display a "Source" link that leads users to an archive
|
||||
of the code. There are many ways you could offer source, and different
|
||||
solutions will be better for different programs; see section 13 for the
|
||||
specific requirements.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
@@ -0,0 +1,197 @@
|
||||
PREFIX = /usr/local
|
||||
BINDIR = $(PREFIX)/bin
|
||||
LIBDIR = $(PREFIX)/lib
|
||||
LIBEXECDIR = $(PREFIX)/libexec
|
||||
MANDIR = $(PREFIX)/share/man
|
||||
|
||||
INSTALL = install
|
||||
INSTALL_PROGRAM = $(INSTALL)
|
||||
INSTALL_DATA = $(INSTALL) -m 644
|
||||
|
||||
D = $(DESTDIR)
|
||||
|
||||
# CXX defaults to `g++`. Rewrite it with a vendor-neutral compiler
|
||||
# name `c++`.
|
||||
ifeq ($(origin CXX), default)
|
||||
CXX = c++
|
||||
endif
|
||||
|
||||
# If you want to keep symbols in the installed binary, run make with
|
||||
# `STRIP=true` to run /bin/true instead of the strip command.
|
||||
STRIP = strip
|
||||
|
||||
SRCS=$(wildcard *.cc elf/*.cc macho/*.cc)
|
||||
HEADERS=$(wildcard *.h elf/*.h macho/*.h)
|
||||
OBJS=$(SRCS:%.cc=out/%.o)
|
||||
|
||||
OS = $(shell uname -s)
|
||||
ARCH = $(shell uname -m)
|
||||
|
||||
IS_ANDROID = 0
|
||||
ifneq ($(findstring -android,$(shell $(CC) -dumpmachine)),)
|
||||
IS_ANDROID = 1
|
||||
endif
|
||||
|
||||
# If you want to compile mold for debugging, invoke make as
|
||||
# `make CXXFLAGS=-g`.
|
||||
CFLAGS = -O2
|
||||
CXXFLAGS = -O2
|
||||
|
||||
MOLD_CXXFLAGS = -std=c++20 -fno-exceptions -fno-unwind-tables \
|
||||
-fno-asynchronous-unwind-tables -DMOLD_VERSION=\"1.0.3\" \
|
||||
-DLIBDIR="\"$(LIBDIR)\""
|
||||
|
||||
MOLD_LDFLAGS = -pthread -lz -lm
|
||||
|
||||
GIT_HASH = $(shell [ -d .git ] && git rev-parse HEAD)
|
||||
ifneq ($(GIT_HASH),)
|
||||
MOLD_CXXFLAGS += -DGIT_HASH=\"$(GIT_HASH)\"
|
||||
endif
|
||||
|
||||
LTO = 0
|
||||
|
||||
ifeq ($(LTO), 1)
|
||||
CXXFLAGS += -flto -O3
|
||||
LDFLAGS += -flto
|
||||
endif
|
||||
|
||||
# By default, we want to use mimalloc as a memory allocator. mimalloc
|
||||
# is disabled on macOS and Android because it didn't work on those hosts.
|
||||
USE_MIMALLOC = 1
|
||||
ifeq ($(OS), Darwin)
|
||||
USE_MIMALLOC = 0
|
||||
else ifeq ($(IS_ANDROID), 1)
|
||||
USE_MIMALLOC = 0
|
||||
endif
|
||||
|
||||
ifeq ($(USE_MIMALLOC), 1)
|
||||
ifdef SYSTEM_MIMALLOC
|
||||
MOLD_LDFLAGS += -lmimalloc
|
||||
else
|
||||
MIMALLOC_LIB = out/mimalloc/libmimalloc.a
|
||||
MOLD_CXXFLAGS += -Ithird-party/mimalloc/include
|
||||
MOLD_LDFLAGS += -Wl,-whole-archive $(MIMALLOC_LIB) -Wl,-no-whole-archive
|
||||
endif
|
||||
endif
|
||||
|
||||
ifdef SYSTEM_TBB
|
||||
MOLD_LDFLAGS += -ltbb
|
||||
else
|
||||
TBB_LIB = out/tbb/libs/libtbb.a
|
||||
MOLD_LDFLAGS += $(TBB_LIB)
|
||||
MOLD_CXXFLAGS += -Ithird-party/tbb/include
|
||||
endif
|
||||
|
||||
ifdef SYSTEM_XXHASH
|
||||
MOLD_LDFLAGS += -lxxhash
|
||||
else
|
||||
XXHASH_LIB = third-party/xxhash/libxxhash.a
|
||||
MOLD_LDFLAGS += $(XXHASH_LIB)
|
||||
MOLD_CXXFLAGS += -Ithird-party/xxhash
|
||||
endif
|
||||
|
||||
ifeq ($(OS), Linux)
|
||||
ifeq ($(IS_ANDROID), 0)
|
||||
# glibc before 2.17 need librt for clock_gettime
|
||||
MOLD_LDFLAGS += -lrt
|
||||
endif
|
||||
endif
|
||||
|
||||
# Use pkg-config to know where libcrypto resides.
|
||||
ifneq ($(OS), Darwin)
|
||||
MOLD_CXXFLAGS += $(shell pkg-config --cflags-only-I openssl)
|
||||
MOLD_LDFLAGS += $(shell pkg-config --libs-only-L openssl) -lcrypto
|
||||
endif
|
||||
|
||||
# '-latomic' flag is needed building on riscv64 system
|
||||
# RV32 system doesn't tested yet
|
||||
# seems like '-atomic' would be better but not working.
|
||||
ifeq ($(ARCH), riscv64)
|
||||
MOLD_LDFLAGS += -latomic
|
||||
endif
|
||||
|
||||
# -Wc++11-narrowing is a fatal error on Android, so disable it.
|
||||
ifeq ($(IS_ANDROID), 1)
|
||||
MOLD_CXXFLAGS += -Wno-c++11-narrowing
|
||||
endif
|
||||
|
||||
ifeq ($(OS), Linux)
|
||||
MOLD_WRAPPER_LDFLAGS = -Wl,-push-state -Wl,-no-as-needed -ldl -Wl,-pop-state
|
||||
endif
|
||||
|
||||
all: mold mold-wrapper.so
|
||||
|
||||
mold: $(OBJS) $(MIMALLOC_LIB) $(TBB_LIB) $(XXHASH_LIB)
|
||||
$(CXX) $(OBJS) -o $@ $(MOLD_LDFLAGS) $(LDFLAGS)
|
||||
ln -sf mold ld
|
||||
ln -sf mold ld64.mold
|
||||
|
||||
mold-wrapper.so: elf/mold-wrapper.c Makefile
|
||||
$(CC) $(CFLAGS) -fPIC -shared -o $@ $< $(MOLD_WRAPPER_LDFLAGS) $(LDFLAGS)
|
||||
|
||||
out/%.o: %.cc $(HEADERS) Makefile out/elf/.keep out/macho/.keep
|
||||
$(CXX) $(MOLD_CXXFLAGS) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
out/elf/.keep:
|
||||
mkdir -p out/elf
|
||||
touch $@
|
||||
|
||||
out/macho/.keep:
|
||||
mkdir -p out/macho
|
||||
touch $@
|
||||
|
||||
$(MIMALLOC_LIB):
|
||||
mkdir -p out/mimalloc
|
||||
(cd out/mimalloc; CFLAGS=-DMI_USE_ENVIRON=0 cmake -G'Unix Makefiles' ../../third-party/mimalloc)
|
||||
$(MAKE) -C out/mimalloc mimalloc-static
|
||||
|
||||
$(TBB_LIB):
|
||||
mkdir -p out/tbb
|
||||
(cd out/tbb; cmake -G'Unix Makefiles' -DBUILD_SHARED_LIBS=OFF -DTBB_TEST=OFF -DCMAKE_CXX_FLAGS=-D__TBB_DYNAMIC_LOAD_ENABLED=0 -DTBB_STRICT=OFF ../../third-party/tbb)
|
||||
$(MAKE) -C out/tbb tbb
|
||||
(cd out/tbb; ln -sf *_relwithdebinfo libs)
|
||||
|
||||
$(XXHASH_LIB):
|
||||
$(MAKE) -C third-party/xxhash libxxhash.a
|
||||
|
||||
test tests check: all
|
||||
ifeq ($(OS), Darwin)
|
||||
$(MAKE) -C test -f Makefile.darwin --no-print-directory
|
||||
else
|
||||
$(MAKE) -C test -f Makefile.linux --no-print-directory --output-sync
|
||||
endif
|
||||
|
||||
@if test -t 1; then \
|
||||
printf '\e[32mPassed all tests\e[0m\n'; \
|
||||
else \
|
||||
echo 'Passed all tests'; \
|
||||
fi
|
||||
|
||||
install: all
|
||||
$(INSTALL) -d $D$(BINDIR)
|
||||
$(INSTALL_PROGRAM) mold $D$(BINDIR)
|
||||
$(STRIP) $D$(BINDIR)/mold
|
||||
|
||||
$(INSTALL) -d $D$(LIBDIR)/mold
|
||||
$(INSTALL_DATA) mold-wrapper.so $D$(LIBDIR)/mold
|
||||
$(STRIP) $D$(LIBDIR)/mold/mold-wrapper.so
|
||||
|
||||
$(INSTALL) -d $D$(LIBEXECDIR)/mold
|
||||
ln -sf $(BINDIR)/mold $D$(LIBEXECDIR)/mold/ld
|
||||
|
||||
$(INSTALL) -d $D$(MANDIR)/man1
|
||||
$(INSTALL_DATA) docs/mold.1 $D$(MANDIR)/man1
|
||||
|
||||
ln -sf mold $D$(BINDIR)/ld.mold
|
||||
ln -sf mold $D$(BINDIR)/ld64.mold
|
||||
|
||||
uninstall:
|
||||
rm -f $D$(BINDIR)/mold $D$(BINDIR)/ld.mold $D$(BINDIR)/ld64.mold
|
||||
rm -f $D$(MANDIR)/man1/mold.1
|
||||
rm -rf $D$(LIBDIR)/mold
|
||||
|
||||
clean:
|
||||
rm -rf *~ mold mold-wrapper.so out ld ld64.mold
|
||||
$(MAKE) -C third-party/xxhash clean
|
||||
|
||||
.PHONY: all test tests check clean
|
||||
@@ -1,117 +1,133 @@
|
||||
# mold: A Modern Linker
|
||||
|
||||
mold is a faster drop-in replacement for existing Unix linkers. It is several
|
||||
times quicker than the LLVM lld linker, the second-fastest open-source linker,
|
||||
which I initially developed a few years ago. mold aims to enhance developer
|
||||
productivity by minimizing build time, particularly in rapid
|
||||
debug-edit-rebuild cycles.
|
||||
mold is a faster drop-in replacement for existing Unix linkers.
|
||||
It is several times faster than LLVM lld linker, the second-fastest
|
||||
open-source linker which I originally created a few years ago.
|
||||
mold is created for increasing developer productivity by reducing
|
||||
build time especially in rapid debug-edit-rebuild cycles.
|
||||
|
||||
Here is a performance comparison of GNU ld, GNU gold, LLVM lld, and
|
||||
mold when linking final debuginfo-enabled executables for major large
|
||||
programs on a simulated 16-core, 32-thread machine.
|
||||
Here is a performance comparison of GNU gold, LLVM lld, and mold for
|
||||
linking final debuginfo-enabled executables of major large programs
|
||||
on a simulated 8-core 16-threads machine.
|
||||
|
||||

|
||||

|
||||
|
||||
| Program (linker output size) | GNU ld | GNU gold | LLVM lld | mold
|
||||
|-------------------------------|--------|----------|----------|------
|
||||
| MySQL 8.3 (0.47 GiB) | 10.84s | 7.47s | 1.64s | 0.46s
|
||||
| Clang 19 (1.56 GiB) | 42.07s | 33.13s | 5.20s | 1.35s
|
||||
| Chromium 124 (1.35 GiB) | N/A | 27.40s | 6.10s | 1.52s
|
||||
| Program (linker output size) | GNU gold | LLVM lld | mold
|
||||
|-------------------------------|----------|----------|--------
|
||||
| Chrome 96 (1.89 GiB) | 53.86s | 11.74s | 2.21s
|
||||
| Clang 13 (3.18 GiB) | 64.12s | 5.82s | 2.90s
|
||||
| Firefox 89 libxul (1.64 GiB) | 32.95s | 6.80s | 1.42s
|
||||
|
||||
mold is so fast that it is only 2x _slower_ than the `cp` command on the same
|
||||
machine. If you find that mold is not faster than other linkers, feel
|
||||
free to [file a bug report](https://github.com/rui314/mold/issues).
|
||||
mold is so fast that it is only 2x _slower_ than `cp` on the same
|
||||
machine. Feel free to [file a bug](https://github.com/rui314/mold/issues)
|
||||
if you find mold is not faster than other linkers.
|
||||
|
||||
mold supports x86-64, i386, ARM64, ARM32, 64-bit/32-bit little/big-endian
|
||||
RISC-V, 32-bit PowerPC, 64-bit big-endian PowerPC ELFv1, 64-bit little-endian
|
||||
PowerPC ELFv2, s390x, 64-bit/32-bit LoongArch, SPARC64, m68k, and SH-4.
|
||||
mold currently supports x86-64, i386 and ARM64.
|
||||
|
||||
## Why does linking speed matter?
|
||||
## Why does the speed of linking matter?
|
||||
|
||||
If you are using a compiled language such as C, C++, or Rust, a build consists
|
||||
of two phases. In the first phase, a compiler compiles source files into
|
||||
object files (`.o` files). In the second phase, a linker takes all object
|
||||
files and combines them into a single executable or shared library file.
|
||||
If you are using a compiled language such as C, C++ or Rust, a build
|
||||
consists of two phases. In the first phase, a compiler compiles a
|
||||
source file into an object file (`.o` files). In the second phase,
|
||||
a linker takes all object files to combine them into a single executable
|
||||
or a shared library file.
|
||||
|
||||
The second phase can be time-consuming if your build output is large. mold can
|
||||
speed up this process, saving you time and preventing distractions while
|
||||
waiting for a lengthy build to finish. The difference is most noticeable
|
||||
during rapid debug-edit-rebuild cycles.
|
||||
The second phase takes a long time if your build output is large.
|
||||
mold can make it faster, saving your time and keeping you from being
|
||||
distracted while waiting for a long build to finish. The difference is
|
||||
most noticeable when you are in rapid debug-edit-rebuild cycles.
|
||||
|
||||
## Installation
|
||||
## Install
|
||||
|
||||
Binary packages for the following systems are currently available:
|
||||
Binary packages for the following distros are currently available.
|
||||
|
||||
[](https://repology.org/project/mold/versions)
|
||||
|
||||
## How to Build
|
||||
## How to build
|
||||
|
||||
mold is written in C++20, so if you build mold yourself, you will need a
|
||||
recent version of a C++ compiler and a C++ standard library. We recommend GCC
|
||||
10.2 or Clang 12.0.0 (or later) and libstdc++ 10 or libc++ 7 (or later).
|
||||
mold is written in C++20, so if you build mold yourself, you need a
|
||||
recent version of a C++ compiler and a C++ standard library. GCC 10.2
|
||||
or Clang 12.0.0 as well as libstdc++ 10 or libc++ 7 are recommended.
|
||||
|
||||
### Install Dependencies
|
||||
I'm using Ubuntu 20.04 as a development platform. In that environment,
|
||||
you can build mold by the following commands.
|
||||
|
||||
To install build dependencies, run `./install-build-deps.sh` in this
|
||||
directory. It will detect your Linux distribution and attempt to install the
|
||||
necessary packages. You may need to run it as root.
|
||||
### Install dependencies
|
||||
|
||||
#### Ubuntu 20.04 and later / Debian 11 and later
|
||||
|
||||
```shell
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y build-essential git clang cmake libstdc++-10-dev libssl-dev libxxhash-dev zlib1g-dev pkg-config
|
||||
```
|
||||
|
||||
#### Fedora 34 and later
|
||||
|
||||
```shell
|
||||
sudo dnf install -y git clang cmake openssl-devel xxhash-devel zlib-devel libstdc++-devel
|
||||
```
|
||||
|
||||
### Compile mold
|
||||
|
||||
```shell
|
||||
git clone --branch stable https://github.com/rui314/mold.git
|
||||
git clone https://github.com/rui314/mold.git
|
||||
cd mold
|
||||
./install-build-deps.sh
|
||||
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=c++ -B build
|
||||
cmake --build build -j$(nproc)
|
||||
sudo cmake --build build --target install
|
||||
git checkout v1.0.3
|
||||
make -j$(nproc) CXX=clang++
|
||||
sudo make install
|
||||
```
|
||||
|
||||
You might need to pass a C++20 compiler command name to `cmake`. In the
|
||||
example above, `c++` is passed. If that doesn't work for you, try a specific
|
||||
version of a compiler, such as `g++-10` or `clang++-12`.
|
||||
You may need to pass a C++20 compiler command name to `make`.
|
||||
In the above case, `clang++` is passed. If it doesn't work for you,
|
||||
try a specific version of a compiler such as `g++-10` or `clang++-12`.
|
||||
|
||||
By default, `mold` is installed to `/usr/local/bin`. You can change the
|
||||
installation location by passing `-DCMAKE_INSTALL_PREFIX=<directory>`.
|
||||
For other cmake options, see the comments in `CMakeLists.txt`.
|
||||
By default, `mold` is installed to `/usr/local/bin`.
|
||||
|
||||
If you are not using a recent enough Linux distribution, or if `cmake` does
|
||||
not work for you for any reason, you can use Podman to build mold in a
|
||||
container. To do so, run `./dist.sh` in this directory instead of using
|
||||
`cmake`. The shell script will pull a container image, build mold and auxiliary
|
||||
files inside it, and package them into a single tar file named
|
||||
`dist/mold-$version-$arch-linux.tar.gz`. You can extract the tar file anywhere
|
||||
and use the mold executable in it.
|
||||
If you don't use a recent enough Linux distribution, or if for any reason `make`
|
||||
in the above commands doesn't work for you, you can use Docker to build it in
|
||||
a Docker environment. To do so, just run `./build-static.sh` in this
|
||||
directory instead of running `make -j$(nproc)`. The shell script creates a
|
||||
Ubuntu 20.04 Docker image, installs necessary tools and libraries to it,
|
||||
and builds mold as a statically-linked executable.
|
||||
|
||||
`make test` depends on a few more packages. To install, run the following commands:
|
||||
|
||||
```shell
|
||||
sudo dpkg --add-architecture i386
|
||||
sudo apt update
|
||||
sudo apt-get install bsdmainutils dwarfdump libc6-dev:i386 lib32gcc-10-dev libstdc++-10-dev-arm64-cross gcc-10-aarch64-linux-gnu g++-10-aarch64-linux-gnu
|
||||
```
|
||||
|
||||
## How to use
|
||||
|
||||
<details><summary>A classic way to use mold</summary>
|
||||
|
||||
On Unix, the linker command (usually `/usr/bin/ld`) is indirectly invoked by
|
||||
the compiler driver (typically `cc`, `gcc`, or `clang`), which is in turn
|
||||
indirectly invoked by `make` or other build system commands.
|
||||
On Unix, the linker command (which is usually `/usr/bin/ld`) is
|
||||
invoked indirectly by the compiler driver (which is usually `cc`,
|
||||
`gcc` or `clang`), which is typically in turn indirectly invoked by
|
||||
`make` or some other build system command.
|
||||
|
||||
If you can specify an additional command line option for your compiler driver
|
||||
by modifying the build system's config files, add one of the following flags
|
||||
to use mold instead of `/usr/bin/ld`:
|
||||
If you can specify an additional command line option to your compiler
|
||||
driver by modifying build system's config files, add one of the
|
||||
following flags to use `mold` instead of `/usr/bin/ld`:
|
||||
|
||||
- For Clang: pass `-fuse-ld=mold`
|
||||
- Clang: pass `-fuse-ld=mold`
|
||||
|
||||
- For GCC 12.1.0 or later: pass `-fuse-ld=mold`
|
||||
- GCC 12.1.0 (upcoming version) or later: pass `-fuse-ld=mold`
|
||||
|
||||
- For GCC before 12.1.0: the `-fuse-ld` option does not accept `mold` as a
|
||||
valid argument, so you need to use the `-B` option instead. The `-B` option
|
||||
tells GCC where to look for external commands like `ld`.
|
||||
- GCC before 12.1.0: `-fuse-ld` does not accept `mold` as a valid
|
||||
argument, so you need to use `-B` option instead. `-B` is an option
|
||||
to tell GCC where to look for external commands such as `ld`.
|
||||
|
||||
If you have installed mold with `make install`, there should be a directory
|
||||
named `/usr/libexec/mold` (or `/usr/local/libexec/mold`, depending on your
|
||||
`$PREFIX`), and the `ld` command should be there. The `ld` is actually a
|
||||
symlink to `mold`. So, all you need is to pass `-B/usr/libexec/mold` (or
|
||||
`-B/usr/local/libexec/mold`) to GCC.
|
||||
If you have installed mold with `make install`, there should be a
|
||||
directory named `/usr/libexec/mold` (or `/usr/local/libexec/mold`,
|
||||
depending on your $PREFIX), and `ld` command should be there. The
|
||||
`ld` is actually a symlink to `mold`. So, all you need is to pass
|
||||
`-B/usr/libexec/mold` (or `-B/usr/local/libexec/mold`) to GCC.
|
||||
|
||||
If you haven't installed `ld.mold` to any `$PATH`, you can still pass
|
||||
`-fuse-ld=/absolute/path/to/mold` to clang to use mold. However, GCC does not
|
||||
accept an absolute path as an argument for `-fuse-ld`.
|
||||
If you haven't installed `mold` to any `$PATH`, you can still pass
|
||||
`-fuse-ld=/absolute/path/to/mold` to clang to use mold. GCC does not
|
||||
take an absolute path as an argument for `-fuse-ld` though.
|
||||
|
||||
</details>
|
||||
|
||||
@@ -119,157 +135,77 @@ accept an absolute path as an argument for `-fuse-ld`.
|
||||
|
||||
Create `.cargo/config.toml` in your project directory with the following:
|
||||
|
||||
```toml
|
||||
```
|
||||
[target.x86_64-unknown-linux-gnu]
|
||||
linker = "clang"
|
||||
rustflags = ["-C", "link-arg=-fuse-ld=/path/to/mold"]
|
||||
```
|
||||
|
||||
where `/path/to/mold` is an absolute path to the mold executable. In the
|
||||
example above, we use `clang` as a linker driver since it always accepts the
|
||||
`-fuse-ld` option. If your GCC is recent enough to recognize the option, you
|
||||
may be able to remove the `linker = "clang"` line.
|
||||
where `/path/to/mold` is an absolute path to `mold` exectuable.
|
||||
|
||||
```toml
|
||||
[target.x86_64-unknown-linux-gnu]
|
||||
rustflags = ["-C", "link-arg=-fuse-ld=mold"]
|
||||
```
|
||||
|
||||
If you want to use mold for all projects, add the above snippet to
|
||||
If you want to use mold for all projects, put the above snippet to
|
||||
`~/.cargo/config.toml`.
|
||||
|
||||
</details>
|
||||
|
||||
<details><summary>If you are using Nim</summary>
|
||||
|
||||
Create `config.nims` in your project directory with the following:
|
||||
|
||||
```nim
|
||||
when findExe("mold").len > 0 and defined(linux):
|
||||
switch("passL", "-fuse-ld=mold")
|
||||
```
|
||||
|
||||
where `mold` must be included in the `PATH` environment variable. In this
|
||||
example, `gcc` is used as the linker driver. Use the `-fuse-ld` option if your
|
||||
GCC is recent enough to recognize this option.
|
||||
|
||||
If you want to use mold for all projects, add the above snippet to
|
||||
`~/.config/config.nims`.
|
||||
|
||||
</details>
|
||||
|
||||
<details><summary>If you are using Conan package manager</summary>
|
||||
|
||||
You can configure [Conan](https://github.com/conan-io) to download the latest
|
||||
version of `mold` and use it as the linker when building your dependencies and
|
||||
projects from source. Please see the instructions [here](https://conan.io/center/recipes/mold).
|
||||
|
||||
</details>
|
||||
|
||||
<details><summary>mold -run</summary>
|
||||
|
||||
It is sometimes very hard to pass an appropriate command line option to `cc`
|
||||
to specify an alternative linker. To address this situation, mold has a
|
||||
feature to intercept all invocations of `ld`, `ld.bfd`, `ld.lld`, or `ld.gold`
|
||||
and redirect them to itself. To use this feature, run `make` (or another build
|
||||
command) as a subcommand of mold as follows:
|
||||
It is sometimes very hard to pass an appropriate command line option
|
||||
to `cc` to specify an alternative linker. To deal with the situation,
|
||||
mold has a feature to intercept all invocations of `ld`, `ld.lld` or
|
||||
`ld.gold` and redirect it to itself. To use the feature, run `make`
|
||||
(or another build command) as a subcommand of mold as follows:
|
||||
|
||||
```shell
|
||||
mold -run make <make-options-if-any>
|
||||
```
|
||||
|
||||
Internally, mold invokes a given command with the `LD_PRELOAD` environment
|
||||
variable set to its companion shared object file. The shared object file
|
||||
intercepts all function calls to `exec(3)`-family functions to replace
|
||||
`argv[0]` with `mold` if it is `ld`, `ld.bf`, `ld.gold`, or `ld.lld`.
|
||||
Here's an example showing how to link Rust code when using the
|
||||
cargo package manager:
|
||||
|
||||
```shell
|
||||
mold -run cargo build
|
||||
```
|
||||
|
||||
Internally, mold invokes a given command with `LD_PRELOAD` environment
|
||||
variable set to its companion shared object file. The shared object
|
||||
file intercepts all function calls to `exec(3)`-family functions to
|
||||
replace `argv[0]` with `mold` if it is `ld`, `ld.gold` or `ld.lld`.
|
||||
|
||||
</details>
|
||||
|
||||
<details><summary>GitHub Actions</summary>
|
||||
|
||||
You can use our [setup-mold](https://github.com/rui314/setup-mold) GitHub
|
||||
Action to speed up GitHub-hosted continuous builds. Although GitHub Actions
|
||||
run on a 4 core machine, mold is still significantly faster than the default
|
||||
GNU linker, especially when linking large programs.
|
||||
|
||||
</details>
|
||||
|
||||
<details><summary>Verify that you are using mold</summary>
|
||||
|
||||
mold leaves its identification string in the `.comment` section of an output
|
||||
mold leaves its identification string in `.comment` section in an output
|
||||
file. You can print it out to verify that you are actually using mold.
|
||||
|
||||
```shell
|
||||
$ readelf -p .comment <executable-file>
|
||||
readelf -p .comment <executable-file>
|
||||
|
||||
String dump of section '.comment':
|
||||
[ 0] GCC: (Ubuntu 10.2.0-5ubuntu1~20.04) 10.2.0
|
||||
[ 2b] mold 9a1679b47d9b22012ec7dfbda97c8983956716f7
|
||||
```
|
||||
|
||||
If `mold` is present in the `.comment` section, the file was created by mold.
|
||||
|
||||
</details>
|
||||
|
||||
<details><summary>Online manual</summary>
|
||||
|
||||
Since mold is a drop-in replacement, you should be able to use it without
|
||||
reading its manual. However, if you need it, [mold's man page](docs/mold.md)
|
||||
is available online. You can read the same manual by running `man mold`.
|
||||
|
||||
</details>
|
||||
If `mold` is in `.comment`, the file is created by mold.
|
||||
|
||||
## Why is mold so fast?
|
||||
|
||||
One reason is that it utilizes faster algorithms and more efficient data
|
||||
structures compared to other linkers. Another reason is that mold is highly
|
||||
parallelized.
|
||||
One reason is because it simply uses faster algorithms and efficient
|
||||
data structures than other linkers do. The other reason is that the
|
||||
new linker is highly parallelized.
|
||||
|
||||
Here is a side-by-side comparison of per-core CPU usage for lld (left) and
|
||||
mold (right), linking the same program, a Chromium executable.
|
||||
Here is a side-by-side comparison of per-core CPU usage of lld (left)
|
||||
and mold (right). They are linking the same program, Chromium
|
||||
executable.
|
||||
|
||||

|
||||
|
||||
As you can see, mold uses all available cores throughout its execution and
|
||||
finishes quickly. In contrast, lld fails to utilize available cores most of
|
||||
the time. In this demo, the maximum parallelism is artificially capped at 16,
|
||||
so that the bars fit in the GIF.
|
||||
As you can see, mold uses all available cores throughout its execution
|
||||
and finishes quickly. On the other hand, lld failed to use available
|
||||
cores most of the time. In this demo, the maximum parallelism is
|
||||
artificially capped to 16 so that the bars fit in the GIF.
|
||||
|
||||
For details, please see the [design notes](docs/design.md).
|
||||
For details, please read [design notes](docs/design.md).
|
||||
|
||||
## Sponsors
|
||||
# Logo
|
||||
|
||||
It is taken for granted nowadays that compiler toolchains can be easily
|
||||
installed and used for free, and people may not think too much about the
|
||||
individuals behind these "free tools". mold supports many projects, but it
|
||||
is essentially a one-person project. This situation is similar to the one
|
||||
depicted in the following xkcd illustration.
|
||||
|
||||
[](https://xkcd.com/2347)
|
||||
|
||||
If you think that the "Nebraska guy" should be rewarded, please consider
|
||||
becoming our [GitHub sponsor](https://github.com/sponsors/rui314)!
|
||||
|
||||
We thank everyone who sponsors our project. In particular, we'd like to acknowledge
|
||||
the following people and organizations who have sponsored $128/month or more:
|
||||
|
||||
### Corporate sponsors
|
||||
|
||||
<a href="https://mercury.com"><img src="docs/mercury-logo.png" align=center height=120 width=400 alt=Mercury></a>
|
||||
|
||||
<a href="https://cybozu-global.com"><img src="docs/cyboze-logo.png" align=center height=120 width=133 alt=Cybozu></a>
|
||||
|
||||
<a href="https://www.emergetools.com"><img src="docs/emerge-tools-logo.png" align=center height=120 width=240 alt="Emerge Tools"></a><br>
|
||||
|
||||
- [G-Research](https://www.gresearch.co.uk)
|
||||
- [Signal Slot Inc.](https://github.com/signal-slot)
|
||||
- [GlareDB](https://github.com/GlareDB)
|
||||
|
||||
### Individual sponsors
|
||||
|
||||
- [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)
|
||||
- [Pedro Navarro](https://github.com/pedronavf)
|
||||
-
|
||||
@@ -1,29 +0,0 @@
|
||||
#!/bin/bash -x
|
||||
set -e
|
||||
apt-get install -y git bzip2 ninja-build pkg-config libglib2.0-dev python3-distutils-extra libpixman-1-dev
|
||||
|
||||
cd /
|
||||
git clone --depth=1 https://github.com/foss-for-synopsys-dwc-arc-processors/qemu.git
|
||||
mkdir /qemu/build
|
||||
cd /qemu/build
|
||||
../configure --static --extra-cflags=-Wno-error
|
||||
ninja
|
||||
ln -s /qemu/build/qemu-arc /qemu/build/qemu-arc64 /usr/local/bin
|
||||
|
||||
|
||||
apt-get install -y libgmp-dev libmpfr-dev texinfo bison yacc flex
|
||||
cd /
|
||||
git clone --depth=1 https://github.com/foss-for-synopsys-dwc-arc-processors/binutils-gdb.git
|
||||
cd /binutils-gdb
|
||||
mkdir build
|
||||
cd build
|
||||
../configure --target=arc-linux
|
||||
make -j$(nproc)
|
||||
|
||||
|
||||
apt-get install -y mold
|
||||
mkdir /build
|
||||
cd /build
|
||||
cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DMOLD_USE_MOLD=1 /mold
|
||||
ninja
|
||||
TRIPLE=arc-linux-gnu bash -x /mold/test/hello-dynamic.sh
|
||||
@@ -1,31 +1,7 @@
|
||||
// This file contains functions to read an archive file (.a file).
|
||||
// An archive file is just a bundle of object files. It's similar to
|
||||
// tar or zip, but the contents are not compressed.
|
||||
//
|
||||
// An archive file is either "regular" or "thin". A regular archive
|
||||
// contains object files directly, while a thin archive contains only
|
||||
// pathnames. In the latter case, actual file contents have to be read
|
||||
// from given pathnames. A regular archive is sometimes called "fat"
|
||||
// archive as opposed to "thin".
|
||||
//
|
||||
// If an archive file is given to the linker, the linker pulls out
|
||||
// object files that are needed to resolve undefined symbols. So,
|
||||
// bunding object files as an archive and giving that archive to the
|
||||
// linker has a different meaning than directly giving the same set of
|
||||
// object files to the linker. The former links only needed object
|
||||
// files, while the latter links all the given object files.
|
||||
//
|
||||
// Therefore, if you link libc.a for example, not all the libc
|
||||
// functions are linked to your binary. Instead, only object files
|
||||
// that provides functions and variables used in your program get
|
||||
// linked. To make this efficient, static library functions are
|
||||
// usually separated to each object file in an archive file. You can
|
||||
// see the contents of libc.a by running `ar t
|
||||
// /usr/lib/x86_64-linux-gnu/libc.a`.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
#include "mold.h"
|
||||
#include "filetype.h"
|
||||
|
||||
namespace mold {
|
||||
|
||||
@@ -75,12 +51,12 @@ struct ArHdr {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Context>
|
||||
std::vector<MappedFile *>
|
||||
read_thin_archive_members(Context &ctx, MappedFile *mf) {
|
||||
template <typename C>
|
||||
std::vector<MappedFile<C> *>
|
||||
read_thin_archive_members(C &ctx, MappedFile<C> *mf) {
|
||||
u8 *begin = mf->data;
|
||||
u8 *data = begin + 8;
|
||||
std::vector<MappedFile *> vec;
|
||||
std::vector<MappedFile<C> *> vec;
|
||||
std::string_view strtab;
|
||||
|
||||
while (data < begin + mf->size) {
|
||||
@@ -115,19 +91,19 @@ read_thin_archive_members(Context &ctx, MappedFile *mf) {
|
||||
continue;
|
||||
|
||||
std::string path = name.starts_with('/') ?
|
||||
name : (path_dirname(mf->name) / name).string();
|
||||
vec.push_back(must_open_file(ctx, path));
|
||||
vec.back()->thin_parent = mf;
|
||||
name : (filepath(mf->name).parent_path() / name).string();
|
||||
vec.push_back(MappedFile<C>::must_open(ctx, path));
|
||||
data = body;
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <typename Context>
|
||||
std::vector<MappedFile *> read_fat_archive_members(Context &ctx, MappedFile *mf) {
|
||||
template <typename C>
|
||||
std::vector<MappedFile<C> *>
|
||||
read_fat_archive_members(C &ctx, MappedFile<C> *mf) {
|
||||
u8 *begin = mf->data;
|
||||
u8 *data = begin + 8;
|
||||
std::vector<MappedFile *> vec;
|
||||
std::vector<MappedFile<C> *> vec;
|
||||
std::string_view strtab;
|
||||
|
||||
while (begin + mf->size - data >= 2) {
|
||||
@@ -161,13 +137,17 @@ std::vector<MappedFile *> read_fat_archive_members(Context &ctx, MappedFile *mf)
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <typename Context>
|
||||
std::vector<MappedFile *> read_archive_members(Context &ctx, MappedFile *mf) {
|
||||
std::string_view str = mf->get_contents();
|
||||
if (str.starts_with("!<arch>\n"))
|
||||
template <typename C>
|
||||
std::vector<MappedFile<C> *>
|
||||
read_archive_members(C &ctx, MappedFile<C> *mf) {
|
||||
switch (get_file_type(mf)) {
|
||||
case FileType::AR:
|
||||
return read_fat_archive_members(ctx, mf);
|
||||
assert(str.starts_with("!<thin>\n"));
|
||||
return read_thin_archive_members(ctx, mf);
|
||||
case FileType::THIN_AR:
|
||||
return read_thin_archive_members(ctx, mf);
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
Executable
+44
@@ -0,0 +1,44 @@
|
||||
#!/bin/bash -x
|
||||
# This is a shell script to build a statically-linked mold
|
||||
# executable using Docker, so that it is easy to build mold
|
||||
# on non-Ubuntu 20.04 machines.
|
||||
#
|
||||
# A docker image used for building mold is persistent.
|
||||
# Run `docker image rm mold-build-ubuntu20` to remove the image
|
||||
# from disk.
|
||||
|
||||
set -e
|
||||
|
||||
# If the existing file is not statically-linked, remove it.
|
||||
[ -f mold ] && ! ldd mold 2>&1 | grep -q 'not a dynamic executable' && \
|
||||
rm mold
|
||||
|
||||
cat <<EOF | docker build -t mold-build-ubuntu20 -
|
||||
FROM ubuntu:20.04
|
||||
RUN apt-get update && \
|
||||
TZ=Europe/London apt-get install -y tzdata && \
|
||||
apt-get install -y --no-install-recommends build-essential clang lld \
|
||||
bsdmainutils file gcc-multilib git pkg-config \
|
||||
cmake libstdc++-10-dev zlib1g-dev libssl-dev && \
|
||||
apt clean && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
EOF
|
||||
|
||||
LDFLAGS='-fuse-ld=lld -static'
|
||||
|
||||
# libstdc++'s `std::__glibcxx_rwlock_rdlock` refers these symbols
|
||||
# as weak symbols, although they need to be defined. Otherwise,
|
||||
# the program crashes after juping to address 0.
|
||||
# So, we force loading symbols as a workaround.
|
||||
LDFLAGS="$LDFLAGS -Wl,-u,pthread_rwlock_rdlock"
|
||||
LDFLAGS="$LDFLAGS -Wl,-u,pthread_rwlock_unlock"
|
||||
LDFLAGS="$LDFLAGS -Wl,-u,pthread_rwlock_wrlock"
|
||||
|
||||
docker_args=(-v "`pwd`:/mold:Z" -u "$(id -u)":"$(id -g)")
|
||||
if docker --version | grep -q podman; then
|
||||
docker_args+=(--userns=keep-id)
|
||||
fi
|
||||
|
||||
docker run -it --rm "${docker_args[@]}" \
|
||||
mold-build-ubuntu20 \
|
||||
make -C /mold -j"$(nproc)" CC=clang CXX=clang++ LDFLAGS="$LDFLAGS" "$@"
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace mold {
|
||||
|
||||
template <typename T>
|
||||
class BigEndian {
|
||||
public:
|
||||
BigEndian() : BigEndian(0) {}
|
||||
|
||||
BigEndian(T x) {
|
||||
*this = x;
|
||||
}
|
||||
|
||||
operator T() const {
|
||||
// We don't need to optimize this code because compilers are
|
||||
// usually smart enough to compile this loop into a single
|
||||
// byte-swap instruction such as x86's bswap.
|
||||
T ret = 0;
|
||||
for (int i = 0; i < sizeof(T); i++)
|
||||
ret = (ret << 8) | val[i];
|
||||
return ret;
|
||||
}
|
||||
|
||||
BigEndian &operator=(T x) {
|
||||
for (int i = 0; i < sizeof(T); i++)
|
||||
val[sizeof(T) - 1 - i] = x >> (i * 8);
|
||||
return *this;
|
||||
}
|
||||
|
||||
BigEndian &operator++() {
|
||||
return *this = *this + 1;
|
||||
}
|
||||
|
||||
BigEndian operator++(int) {
|
||||
T ret = *this;
|
||||
*this = *this + 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
BigEndian &operator--() {
|
||||
return *this = *this - 1;
|
||||
}
|
||||
|
||||
BigEndian operator--(int) {
|
||||
T ret = *this;
|
||||
*this = *this - 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
BigEndian &operator+=(T x) {
|
||||
return *this = *this + x;
|
||||
}
|
||||
|
||||
BigEndian &operator&=(T x) {
|
||||
return *this = *this & x;
|
||||
}
|
||||
|
||||
BigEndian &operator|=(T x) {
|
||||
return *this = *this | x;
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t val[sizeof(T)];
|
||||
};
|
||||
|
||||
using ibig16 = BigEndian<int16_t>;
|
||||
using ibig32 = BigEndian<int32_t>;
|
||||
using ibig64 = BigEndian<int64_t>;
|
||||
using ubig16 = BigEndian<uint16_t>;
|
||||
using ubig32 = BigEndian<uint32_t>;
|
||||
using ubig64 = BigEndian<uint64_t>;
|
||||
|
||||
} // namespace mold
|
||||
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include "mold.h"
|
||||
|
||||
namespace mold {
|
||||
|
||||
template <typename C>
|
||||
std::vector<std::string_view>
|
||||
read_response_file(C &ctx, std::string_view path) {
|
||||
std::vector<std::string_view> vec;
|
||||
MappedFile<C> *mf = MappedFile<C>::must_open(ctx, std::string(path));
|
||||
u8 *data = mf->data;
|
||||
|
||||
auto read_quoted = [&](i64 i, char quote) {
|
||||
std::string buf;
|
||||
while (i < mf->size && data[i] != quote) {
|
||||
if (data[i] == '\\') {
|
||||
buf.append(1, data[i + 1]);
|
||||
i += 2;
|
||||
} else {
|
||||
buf.append(1, data[i++]);
|
||||
}
|
||||
}
|
||||
if (i >= mf->size)
|
||||
Fatal(ctx) << path << ": premature end of input";
|
||||
vec.push_back(save_string(ctx, buf));
|
||||
return i + 1;
|
||||
};
|
||||
|
||||
auto read_unquoted = [&](i64 i) {
|
||||
std::string buf;
|
||||
while (i < mf->size && !isspace(data[i]))
|
||||
buf.append(1, data[i++]);
|
||||
vec.push_back(save_string(ctx, buf));
|
||||
return i;
|
||||
};
|
||||
|
||||
for (i64 i = 0; i < mf->size;) {
|
||||
if (isspace(data[i]))
|
||||
i++;
|
||||
else if (data[i] == '\'')
|
||||
i = read_quoted(i + 1, '\'');
|
||||
else if (data[i] == '\"')
|
||||
i = read_quoted(i + 1, '\"');
|
||||
else
|
||||
i = read_unquoted(i);
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
std::vector<std::string_view> expand_response_files(C &ctx, char **argv) {
|
||||
std::vector<std::string_view> vec;
|
||||
for (i64 i = 0; argv[i]; i++) {
|
||||
if (argv[i][0] == '@')
|
||||
append(vec, read_response_file(ctx, argv[i] + 1));
|
||||
else
|
||||
vec.push_back(argv[i]);
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
// This file implements a multi-threaded zlib compression routine.
|
||||
//
|
||||
// Multiple pieces of raw compressed data in zlib-format can be merged
|
||||
// just by concatenation as long as each zlib stream is flushed with
|
||||
// Z_SYNC_FLUSH. In this file, we split input data into multiple
|
||||
// shards, compress them individually and concatenate them. We then
|
||||
// append a header, a trailer and a checksum so that the concatenated
|
||||
// data is valid zlib-format data.
|
||||
//
|
||||
// Using threads to compress data has a downside. Since the dictionary
|
||||
// is reset on boundaries of shards, compression ratio is sacrificed
|
||||
// a little bit. However, if a shard size is large enough, that loss
|
||||
// is negligible in practice.
|
||||
|
||||
#include "mold.h"
|
||||
|
||||
#include <tbb/parallel_for_each.h>
|
||||
#include <zlib.h>
|
||||
|
||||
namespace mold {
|
||||
|
||||
static constexpr i64 SHARD_SIZE = 1024 * 1024;
|
||||
|
||||
static std::vector<std::string_view> split(std::string_view input) {
|
||||
std::vector<std::string_view> shards;
|
||||
|
||||
while (input.size() >= SHARD_SIZE) {
|
||||
shards.push_back(input.substr(0, SHARD_SIZE));
|
||||
input = input.substr(SHARD_SIZE);
|
||||
}
|
||||
if (!input.empty())
|
||||
shards.push_back(input);
|
||||
return shards;
|
||||
}
|
||||
|
||||
static std::vector<u8> do_compress(std::string_view input) {
|
||||
// Initialize zlib stream. Since debug info is generally compressed
|
||||
// pretty well, we chose compression level 3.
|
||||
z_stream strm;
|
||||
strm.zalloc = Z_NULL;
|
||||
strm.zfree = Z_NULL;
|
||||
strm.opaque = Z_NULL;
|
||||
int r = deflateInit2(&strm, 3, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
|
||||
assert(r == Z_OK);
|
||||
|
||||
// Set an input buffer
|
||||
strm.avail_in = input.size();
|
||||
strm.next_in = (u8 *)input.data();
|
||||
|
||||
// Set an output buffer. deflateBound() returns an upper bound
|
||||
// on the compression size. +16 for Z_SYNC_FLUSH.
|
||||
std::vector<u8> buf(deflateBound(&strm, strm.avail_in) + 16);
|
||||
|
||||
strm.avail_out = buf.size();
|
||||
strm.next_out = buf.data();
|
||||
|
||||
r = deflate(&strm, Z_SYNC_FLUSH);
|
||||
assert(r == Z_OK);
|
||||
assert(strm.avail_out > 0);
|
||||
|
||||
buf.resize(buf.size() - strm.avail_out);
|
||||
deflateEnd(&strm);
|
||||
return buf;
|
||||
}
|
||||
|
||||
ZlibCompressor::ZlibCompressor(std::string_view input) {
|
||||
std::vector<std::string_view> inputs = split(input);
|
||||
std::vector<u64> adlers(inputs.size());
|
||||
shards.resize(inputs.size());
|
||||
|
||||
// Compress each shard
|
||||
tbb::parallel_for((i64)0, (i64)inputs.size(), [&](i64 i) {
|
||||
adlers[i] = adler32(1, (u8 *)inputs[i].data(), inputs[i].size());
|
||||
shards[i] = do_compress(inputs[i]);
|
||||
});
|
||||
|
||||
// Combine checksums
|
||||
checksum = adlers[0];
|
||||
for (i64 i = 1; i < inputs.size(); i++)
|
||||
checksum = adler32_combine(checksum, adlers[i], inputs[i].size());
|
||||
}
|
||||
|
||||
i64 ZlibCompressor::size() const {
|
||||
i64 size = 2; // +2 for header
|
||||
for (const std::vector<u8> &shard : shards)
|
||||
size += shard.size();
|
||||
return size + 6; // +6 for trailer and checksum
|
||||
}
|
||||
|
||||
void ZlibCompressor::write_to(u8 *buf) {
|
||||
// Write a zlib-format header
|
||||
buf[0] = 0x78;
|
||||
buf[1] = 0x9c;
|
||||
|
||||
// Copy compressed data
|
||||
std::vector<i64> offsets(shards.size());
|
||||
offsets[0] = 2; // +2 for header
|
||||
for (i64 i = 1; i < shards.size(); i++)
|
||||
offsets[i] = offsets[i - 1] + shards[i - 1].size();
|
||||
|
||||
tbb::parallel_for((i64)0, (i64)shards.size(), [&](i64 i) {
|
||||
memcpy(&buf[offsets[i]], shards[i].data(), shards[i].size());
|
||||
});
|
||||
|
||||
// Write a trailer
|
||||
u8 *end = buf + size();
|
||||
end[-6] = 3;
|
||||
end[-5] = 0;
|
||||
|
||||
// Write a checksum
|
||||
*(ubig32 *)(end - 4) = checksum;
|
||||
}
|
||||
|
||||
GzipCompressor::GzipCompressor(std::string_view input) {
|
||||
std::vector<std::string_view> inputs = split(input);
|
||||
std::vector<u32> crc(inputs.size());
|
||||
shards.resize(inputs.size());
|
||||
|
||||
// Compress each shard
|
||||
tbb::parallel_for((i64)0, (i64)inputs.size(), [&](i64 i) {
|
||||
crc[i] = crc32(0, (u8 *)inputs[i].data(), inputs[i].size());
|
||||
shards[i] = do_compress(inputs[i]);
|
||||
});
|
||||
|
||||
// Combine checksums
|
||||
checksum = crc[0];
|
||||
for (i64 i = 1; i < inputs.size(); i++)
|
||||
checksum = crc32_combine(checksum, crc[i], inputs[i].size());
|
||||
|
||||
uncompressed_size = input.size();
|
||||
}
|
||||
|
||||
i64 GzipCompressor::size() const {
|
||||
i64 size = 10; // +10 for header
|
||||
for (const std::vector<u8> &shard : shards)
|
||||
size += shard.size();
|
||||
return size + 10; // +10 for trailer and checksum
|
||||
}
|
||||
|
||||
void GzipCompressor::write_to(u8 *buf) {
|
||||
// Write a zlib-format header
|
||||
memset(buf, 0, 10);
|
||||
buf[0] = 0x1f; // magic
|
||||
buf[1] = 0x8b; // magic
|
||||
buf[2] = 0x08; // compression method is zlib
|
||||
buf[9] = 0xff; // made on unknown OS
|
||||
|
||||
// Copy compressed data
|
||||
std::vector<i64> offsets(shards.size());
|
||||
offsets[0] = 10; // +10 for header
|
||||
for (i64 i = 1; i < shards.size(); i++)
|
||||
offsets[i] = offsets[i - 1] + shards[i - 1].size();
|
||||
|
||||
tbb::parallel_for((i64)0, (i64)shards.size(), [&](i64 i) {
|
||||
memcpy(&buf[offsets[i]], shards[i].data(), shards[i].size());
|
||||
});
|
||||
|
||||
// Write a trailer
|
||||
u8 *end = buf + size();
|
||||
end[-10] = 0x3; // two-byte zlib stream terminator
|
||||
end[-9] = 0;
|
||||
*(u32 *)(end - 8) = checksum;
|
||||
*(u32 *)(end - 4) = uncompressed_size;
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cxxabi.h>
|
||||
|
||||
namespace mold {
|
||||
|
||||
std::string_view demangle(std::string_view name) {
|
||||
if (name.starts_with("_Z")) {
|
||||
static thread_local char *buf;
|
||||
static thread_local size_t buflen;
|
||||
|
||||
int status;
|
||||
char *p = abi::__cxa_demangle(std::string(name).c_str(), buf, &buflen, &status);
|
||||
if (status == 0) {
|
||||
buf = p;
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,219 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# This script creates a mold binary distribution. The output is written in
|
||||
# `dist` 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++ 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 Podman 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 want to build mold with Clang rather than GCC because mold's
|
||||
# Identical Code Folding works best with the LLVM address significance
|
||||
# table (.llvm_addrsig). Building a release binary with GCC yields a
|
||||
# slightly larger binary than Clang's.
|
||||
#
|
||||
# We need a recent version of Clang to build mold. If it's not available
|
||||
# via apt-get, we'll build it ourselves.
|
||||
#
|
||||
# You may need to install qemu-user-static package to build non-native
|
||||
# binaries.
|
||||
|
||||
set -e -x
|
||||
cd "$(dirname $0)"
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [ x86_64 | aarch64 | arm | riscv64 | ppc64le | s390x | loongarch64 ]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
case $# in
|
||||
0)
|
||||
arch=$(uname -m)
|
||||
if [ $arch = arm64 ]; then
|
||||
arch=aarch64
|
||||
elif [[ $arch = arm* ]]; then
|
||||
arch=arm
|
||||
fi
|
||||
;;
|
||||
1)
|
||||
arch="$1"
|
||||
;;
|
||||
*)
|
||||
usage
|
||||
esac
|
||||
|
||||
# Create a Podman image.
|
||||
if [ "$GITHUB_REPOSITORY" = '' ]; then
|
||||
image=mold-builder-$arch
|
||||
image_build="podman build --arch $arch -t $image -"
|
||||
else
|
||||
# If this script is running on GitHub Actions, we want to cache
|
||||
# the created container image in GitHub's container repostiory.
|
||||
image=ghcr.io/$GITHUB_REPOSITORY/mold-builder-$arch
|
||||
image_build="podman build --arch $arch -t $image --output=type=registry --layers --cache-to $image --cache-from $image -"
|
||||
fi
|
||||
|
||||
case $arch in
|
||||
x86_64)
|
||||
# Debian 9 (Stretch) released in June 2017.
|
||||
# We use a Google-provided mirror (gcr.io) of the official Docker hub
|
||||
# (docker.io) because docker.io has a strict rate limit policy.
|
||||
cat <<EOF | $image_build
|
||||
FROM mirror.gcr.io/library/debian:stretch@sha256:c5c5200ff1e9c73ffbf188b4a67eb1c91531b644856b4aefe86a58d2f0cb05be
|
||||
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 file make gcc g++ zlib1g-dev libssl-dev && \
|
||||
rm -rf /var/lib/apt/lists
|
||||
|
||||
# Build CMake 3.27
|
||||
RUN mkdir /build && \
|
||||
cd /build && \
|
||||
wget -O- --no-check-certificate --progress=dot:mega 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 14
|
||||
RUN mkdir /build && \
|
||||
cd /build && \
|
||||
wget -O- --no-check-certificate --progress=dot:mega https://ftpmirror.gnu.org/gcc/gcc-14.2.0/gcc-14.2.0.tar.gz | tar xzf - --strip-components=1 && \
|
||||
mkdir gmp mpc mpfr && \
|
||||
wget -O- --no-check-certificate --progress=dot:mega https://ftpmirror.gnu.org/gmp/gmp-6.3.0.tar.gz | tar xzf - --strip-components=1 -C gmp && \
|
||||
wget -O- --no-check-certificate --progress=dot:mega https://ftpmirror.gnu.org/mpc/mpc-1.3.1.tar.gz | tar xzf - --strip-components=1 -C mpc && \
|
||||
wget -O- --no-check-certificate --progress=dot:mega https://ftpmirror.gnu.org/mpfr/mpfr-4.2.1.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
|
||||
|
||||
# Build GNU binutils 2.43
|
||||
RUN mkdir /build && \
|
||||
cd /build && \
|
||||
wget -O- --no-check-certificate --progress=dot:mega https://ftpmirror.gnu.org/binutils/binutils-2.43.tar.gz | tar xzf - --strip-components=1 && \
|
||||
./configure --prefix=/usr && \
|
||||
make -j\$(nproc) && \
|
||||
make install && \
|
||||
rm -fr /build
|
||||
|
||||
# Build Python 3.12.7
|
||||
RUN mkdir /build && \
|
||||
cd /build && \
|
||||
wget -O- --no-check-certificate --progress=dot:mega https://www.python.org/ftp/python/3.12.7/Python-3.12.7.tgz | tar xzf - --strip-components=1 && \
|
||||
./configure && \
|
||||
make -j\$(nproc) && \
|
||||
make install && \
|
||||
rm -rf /build
|
||||
|
||||
# Build LLVM 19
|
||||
RUN mkdir /build && \
|
||||
cd /build && \
|
||||
wget -O- --no-check-certificate --progress=dot:mega https://github.com/llvm/llvm-project/archive/refs/tags/llvmorg-19.1.7.tar.gz | tar xzf - --strip-components=1 && \
|
||||
mkdir b && \
|
||||
cd b && \
|
||||
cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=clang ../llvm && \
|
||||
cmake --build . -j\$(nproc) && \
|
||||
cmake --install . --strip && \
|
||||
rm -rf /build
|
||||
EOF
|
||||
;;
|
||||
aarch64 | arm | ppc64le | s390x)
|
||||
# Debian 11 (Bullseye) released in August 2021
|
||||
#
|
||||
# We don't want to build Clang for these targets with Qemu becuase
|
||||
# that'd take extremely long time. Also I believe old build machines
|
||||
# are usually x86-64.
|
||||
cat <<EOF | $image_build
|
||||
FROM mirror.gcr.io/library/debian:bullseye-20240904@sha256:8ccc486c29a3ad02ad5af7f1156e2152dff3ba5634eec9be375269ef123457d8
|
||||
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 clang-16 cmake && \
|
||||
ln -sf /usr/bin/clang-16 /usr/bin/clang && \
|
||||
ln -sf /usr/bin/clang++-16 /usr/bin/clang++ && \
|
||||
rm -rf /var/lib/apt/lists
|
||||
EOF
|
||||
;;
|
||||
riscv64)
|
||||
cat <<EOF | $image_build
|
||||
FROM mirror.gcr.io/riscv64/debian:unstable-20240926@sha256:25654919c2926f38952cdd14b3300d83d13f2d820715f78c9f4b7a1d9399bf48
|
||||
ENV DEBIAN_FRONTEND=noninteractive TZ=UTC
|
||||
RUN sed -i -e '/^URIs/d' -e 's/^# http/URIs: http/' /etc/apt/sources.list.d/debian.sources && \
|
||||
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-14 g++-14 clang-18 cmake && \
|
||||
ln -sf /usr/bin/clang-18 /usr/bin/clang && \
|
||||
ln -sf /usr/bin/clang++-18 /usr/bin/clang++ && \
|
||||
rm -rf /var/lib/apt/lists
|
||||
EOF
|
||||
;;
|
||||
loongarch64)
|
||||
# LoongArch build is not reproducible yet
|
||||
cat <<EOF | $image_build
|
||||
FROM mirror.gcr.io/loongarch64/debian:sid
|
||||
ENV DEBIAN_FRONTEND=noninteractive TZ=UTC
|
||||
RUN apt-get update && \
|
||||
apt-get install -y --no-install-recommends build-essential gcc-14 g++-14 clang-19 cmake && \
|
||||
ln -sf /usr/bin/clang-19 /usr/bin/clang && \
|
||||
ln -sf /usr/bin/clang++-19 /usr/bin/clang++ && \
|
||||
rm -rf /var/lib/apt/lists
|
||||
EOF
|
||||
;;
|
||||
*)
|
||||
usage
|
||||
;;
|
||||
esac
|
||||
|
||||
version=$(sed -n 's/^project(mold VERSION \(.*\))/\1/p' CMakeLists.txt)
|
||||
dest=mold-$version-$arch-linux
|
||||
|
||||
# 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.
|
||||
mkdir -p dist
|
||||
|
||||
podman run --arch $arch -it --rm --userns=host --pids-limit=-1 --network=none \
|
||||
-v "$(pwd):/mold:ro" -v "$(pwd)/dist:/dist" $image bash -c "
|
||||
set -e
|
||||
mkdir /build
|
||||
cd /build
|
||||
cmake -DCMAKE_BUILD_TYPE=Release -DMOLD_MOSTLY_STATIC=1 -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ /mold
|
||||
cmake --build . -j\$(nproc)
|
||||
cmake --install .
|
||||
cmake -DMOLD_USE_MOLD=1 .
|
||||
cmake --build . -j\$(nproc)
|
||||
ctest --output-on-failure -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 > /dist/$dest.tar.gz
|
||||
cp mold /dist
|
||||
sha256sum /dist/$dest.tar.gz
|
||||
"
|
||||
@@ -1 +0,0 @@
|
||||
<svg version="1.1" viewBox="0.0 0.0 600.0 371.0" fill="none" stroke="none" stroke-linecap="square" stroke-miterlimit="10" width="600" height="371" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg"><path fill="#ffffff" d="M0 0L600.0 0L600.0 371.0L0 371.0L0 0Z" fill-rule="nonzero"/><path stroke="#333333" stroke-width="1.0" stroke-linecap="butt" d="M82.5 275.5L581.5 275.5" fill-rule="nonzero"/><path stroke="#cccccc" stroke-width="1.0" stroke-linecap="butt" d="M82.5 231.5L581.5 231.5" fill-rule="nonzero"/><path stroke="#cccccc" stroke-width="1.0" stroke-linecap="butt" d="M82.5 186.5L581.5 186.5" fill-rule="nonzero"/><path stroke="#cccccc" stroke-width="1.0" stroke-linecap="butt" d="M82.5 142.5L581.5 142.5" fill-rule="nonzero"/><path stroke="#cccccc" stroke-width="1.0" stroke-linecap="butt" d="M82.5 98.5L581.5 98.5" fill-rule="nonzero"/><path stroke="#cccccc" stroke-width="1.0" stroke-linecap="butt" d="M82.5 53.5L581.5 53.5" fill-rule="nonzero"/><clipPath id="id_0"><path d="M82.55 53.916668L581.45 53.916668L581.45 275.45L82.55 275.45L82.55 53.916668Z" clip-rule="nonzero"/></clipPath><path stroke="#000000" stroke-width="2.0" stroke-linecap="butt" stroke-opacity="0.0" clip-path="url(#id_0)" d="M158.0 275.0L136.0 275.0L136.0 229.0C136.0 227.89543 136.89543 227.0 138.0 227.0L156.0 227.0C157.10457 227.0 158.0 227.89543 158.0 229.0Z" fill-rule="nonzero"/><path fill="#4285f4" clip-path="url(#id_0)" d="M158.0 275.0L136.0 275.0L136.0 229.0C136.0 227.89543 136.89543 227.0 138.0 227.0L156.0 227.0C157.10457 227.0 158.0 227.89543 158.0 229.0Z" fill-rule="nonzero"/><path stroke="#000000" stroke-width="2.0" stroke-linecap="butt" stroke-opacity="0.0" clip-path="url(#id_0)" d="M307.0 275.0L285.0 275.0L285.0 91.0C285.0 89.89543 285.89542 89.0 287.0 89.0L305.0 89.0C306.10458 89.0 307.0 89.89543 307.0 91.0Z" fill-rule="nonzero"/><path fill="#4285f4" clip-path="url(#id_0)" d="M307.0 275.0L285.0 275.0L285.0 91.0C285.0 89.89543 285.89542 89.0 287.0 89.0L305.0 89.0C306.10458 89.0 307.0 89.89543 307.0 91.0Z" fill-rule="nonzero"/><path stroke="#000000" stroke-width="2.0" stroke-linecap="butt" stroke-opacity="0.0" clip-path="url(#id_0)" d="M182.0 275.0L160.0 275.0L160.0 244.0C160.0 242.89543 160.89543 242.0 162.0 242.0L180.0 242.0C181.10457 242.0 182.0 242.89543 182.0 244.0Z" fill-rule="nonzero"/><path fill="#ea4335" clip-path="url(#id_0)" d="M182.0 275.0L160.0 275.0L160.0 244.0C160.0 242.89543 160.89543 242.0 162.0 242.0L180.0 242.0C181.10457 242.0 182.0 242.89543 182.0 244.0Z" fill-rule="nonzero"/><path stroke="#000000" stroke-width="2.0" stroke-linecap="butt" stroke-opacity="0.0" clip-path="url(#id_0)" d="M331.0 275.0L309.0 275.0L309.0 131.0C309.0 129.89543 309.89542 129.0 311.0 129.0L329.0 129.0C330.10458 129.0 331.0 129.89543 331.0 131.0Z" fill-rule="nonzero"/><path fill="#ea4335" clip-path="url(#id_0)" d="M331.0 275.0L309.0 275.0L309.0 131.0C309.0 129.89543 309.89542 129.0 311.0 129.0L329.0 129.0C330.10458 129.0 331.0 129.89543 331.0 131.0Z" fill-rule="nonzero"/><path stroke="#000000" stroke-width="2.0" stroke-linecap="butt" stroke-opacity="0.0" clip-path="url(#id_0)" d="M480.0 275.0L458.0 275.0L458.0 156.0C458.0 154.89543 458.89542 154.0 460.0 154.0L478.0 154.0C479.10458 154.0 480.0 154.89543 480.0 156.0Z" fill-rule="nonzero"/><path fill="#ea4335" clip-path="url(#id_0)" d="M480.0 275.0L458.0 275.0L458.0 156.0C458.0 154.89543 458.89542 154.0 460.0 154.0L478.0 154.0C479.10458 154.0 480.0 154.89543 480.0 156.0Z" fill-rule="nonzero"/><path stroke="#000000" stroke-width="2.0" stroke-linecap="butt" stroke-opacity="0.0" clip-path="url(#id_0)" d="M206.0 275.0L184.0 275.0L184.0 270.0C184.0 268.89542 184.89543 268.0 186.0 268.0L204.0 268.0C205.10457 268.0 206.0 268.89542 206.0 270.0Z" fill-rule="nonzero"/><path fill="#fbbc04" clip-path="url(#id_0)" d="M206.0 275.0L184.0 275.0L184.0 270.0C184.0 268.89542 184.89543 268.0 186.0 268.0L204.0 268.0C205.10457 268.0 206.0 268.89542 206.0 270.0Z" fill-rule="nonzero"/><path stroke="#000000" stroke-width="2.0" stroke-linecap="butt" stroke-opacity="0.0" clip-path="url(#id_0)" d="M355.0 275.0L333.0 275.0L333.0 254.0C333.0 252.89543 333.89542 252.0 335.0 252.0L353.0 252.0C354.10458 252.0 355.0 252.89543 355.0 254.0Z" fill-rule="nonzero"/><path fill="#fbbc04" clip-path="url(#id_0)" d="M355.0 275.0L333.0 275.0L333.0 254.0C333.0 252.89543 333.89542 252.0 335.0 252.0L353.0 252.0C354.10458 252.0 355.0 252.89543 355.0 254.0Z" fill-rule="nonzero"/><path stroke="#000000" stroke-width="2.0" stroke-linecap="butt" stroke-opacity="0.0" clip-path="url(#id_0)" d="M504.0 275.0L482.0 275.0L482.0 250.0C482.0 248.89543 482.89542 248.0 484.0 248.0L502.0 248.0C503.10458 248.0 504.0 248.89543 504.0 250.0Z" fill-rule="nonzero"/><path fill="#fbbc04" clip-path="url(#id_0)" d="M504.0 275.0L482.0 275.0L482.0 250.0C482.0 248.89543 482.89542 248.0 484.0 248.0L502.0 248.0C503.10458 248.0 504.0 248.89543 504.0 250.0Z" fill-rule="nonzero"/><path stroke="#000000" stroke-width="2.0" stroke-linecap="butt" stroLine truncated
|
||||
|
Before Width: | Height: | Size: 99 KiB |
@@ -1,46 +0,0 @@
|
||||
# Coding Guidelines
|
||||
|
||||
mold is written in C++20, but as is the case with every C++ project,
|
||||
it has local coding rules. In this document, I'll explain some of them
|
||||
and try to give justifications for why I chose such rules.
|
||||
|
||||
## DOs
|
||||
|
||||
- Always use `i64` (which is a type alias for `int64_t` in mold) for
|
||||
integers unless you have a reason not to. For example, even if you know
|
||||
that a loop counter won't exceed 100, you should stop thinking about it
|
||||
and just use `i64`.
|
||||
|
||||
Justification: Local variables are usually on CPU registers, so on
|
||||
64-bit CPUs, there's no performance peanlty on choosing `i64` over
|
||||
`i32`. Even if a compiler has to spill register values to the stack,
|
||||
I don't think there's an observable difference between `i32` and
|
||||
`i64`. Therefore, extra 32 bits are essentially free. On 32-bit CPUs,
|
||||
they are not free, but that's OK because we are writing mold for modern
|
||||
computers. mold will still run on 32-bit computers but a bit slowly.
|
||||
|
||||
By always using `i64`, we can eliminate the need to think about the
|
||||
"right" size for each variable. It also reduces the risk of integer
|
||||
overflow.
|
||||
|
||||
Exceptions: If you have to allocate a very large number (e.g. millions)
|
||||
of the same object, its size matters. In that case, use a smaller type.
|
||||
|
||||
## DON'Ts
|
||||
|
||||
- Don't use `auto` unless its actual type is obvious in the very narrow
|
||||
context. Currently, we use `auto` only for lambdas.
|
||||
|
||||
Justification: I think `auto` makes code writing easier but code reading
|
||||
harder, because readers have to make a guess as to what is the actual
|
||||
type of `auto`. If you are already familiar with the existing codebase,
|
||||
you may be able to guess it easily, but that's not always the case.
|
||||
I want to keep the mold codebase friendly to first-time readers.
|
||||
|
||||
- Don't over-use inheritance. In mold, most classes don't have parents,
|
||||
and even if they do, their class hierarchy is very shallow. Currently
|
||||
its height is just two (i.e. abstract classes and their implementations).
|
||||
|
||||
Justification: Designing class hierarchies is fun as it feels like
|
||||
taxonomy, but I don't think that always help writing code. It looks like
|
||||
simpler class hierarchy makes its code simpler.
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 70 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 23 KiB |
@@ -1,8 +1,3 @@
|
||||
[This document was written in 2020, and the contents are outdated.
|
||||
Specifically, we no longer believe that object preloading is a good
|
||||
idea. That being said, most of the points in this document still hold
|
||||
even today. Therefore, I'll keep this document as-is.]
|
||||
|
||||
## Design and implementation of mold
|
||||
|
||||
For the rest of this documentation, I'll explain the design and the
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 51 KiB |
@@ -1,55 +0,0 @@
|
||||
This page explains the following warning message and how to fix it.
|
||||
mold emits this message when it sees an object file that may not be
|
||||
compatible with mold.
|
||||
|
||||
|
||||
```
|
||||
mold: warning: foo.o: this file may cause a segmentation fault because it requires an executable stack. See https://github.com/rui314/mold/tree/main/docs/execstack.md for more info.
|
||||
```
|
||||
|
||||
# Background
|
||||
|
||||
On modern computers, the stack area (to which local variables are
|
||||
stored) cannot contain executable code. If the control reaches the
|
||||
stack area, the CPU refuses to execute any code there and the program
|
||||
is usually terminated due to segmentation fault.
|
||||
|
||||
This is a security measure. The stack area used to be executable (old
|
||||
CPUs generally execute any code as long as it is in a readable memory
|
||||
region), but that provided an easy attack vector to a malicious user.
|
||||
They wrote executable code to the stack area using some buffer
|
||||
overflow bug and jumped there to run arbitrary code in a remote server
|
||||
process.
|
||||
|
||||
To prevent this type of attack, the stack area is no longer executable
|
||||
since the early 2000s. On Linux, the stack's executable-ness is
|
||||
controlled by a bit in an executable, and the loader respects that
|
||||
bit. The bit is set by the linker.
|
||||
|
||||
GCC had (and still has) a feature that depends on the executable
|
||||
stack, so they invented a way to tell the linker to mark the stack
|
||||
executable. Specifically, if an object file contains a
|
||||
`.note.GNU-stack` section with the `SHF_EXECSTACK` bit, GNU linker
|
||||
silently makes the stack of an output file executable.
|
||||
|
||||
But the GNU linker's behavior is dangerous. If you accidentally link
|
||||
an object file that has that marker section, the entire stack area
|
||||
silently becomes executable, disabling the security mechanism.
|
||||
|
||||
Therefore, mold simply ignores that marker section. If you are using
|
||||
mold, you need to explicitly pass `-z execstack` to the linker to make
|
||||
the stack executable.
|
||||
|
||||
# What caused this issue?
|
||||
|
||||
You are likely to use GCC's [Nested
|
||||
Functions](https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html)
|
||||
feature which still depends on the executable stack.
|
||||
|
||||
# How to fix it?
|
||||
|
||||
If you know what you are doing, pass `-z execstack` to mold. Beware
|
||||
that this will significantly weaken your program's security.
|
||||
|
||||
If you don't want to pass `-z execstack`, rewrite your code so that
|
||||
your code does not depend on the executable stack.
|
||||
@@ -1,158 +0,0 @@
|
||||
The very concept of linking is simple: a compiler compiles a piece of
|
||||
source code into an object file (a file containing machine code), and
|
||||
a linker combines object files into a single executable or a shared
|
||||
library file. However, the actual implementation of the linker for
|
||||
modern systems is much more complicated because hardware, operating
|
||||
system, compiler and linker all have many more features.
|
||||
|
||||
In this file, I'll explain random topics in the glossary format that
|
||||
you need to understand to read mold code.
|
||||
|
||||
## DSO
|
||||
|
||||
A .so file. Short for Dynamic Shared Object. Often called as a
|
||||
shared library, a dynamic libray or a shared object as well.
|
||||
|
||||
An DSO contains common functions and data that are used by multiple
|
||||
executables and/or other DSOs. At runtime, a DSO is loaded to a
|
||||
contiguous region in the virtual address.
|
||||
|
||||
## Object file
|
||||
|
||||
A .o file. An object file contains machine code and data, but it
|
||||
cannot be executed because it's not self-contained. For example,
|
||||
if you compile a C source file containing a call of `printf`,
|
||||
the actual function code of `printf` is not included in the resulting
|
||||
object file. You include `stdio.h`, but that teaches the compiler
|
||||
only about `printf`'s type, and the compiler still don't know what
|
||||
`printf` actually does. Therefore, it cannot emit code for `printf`.
|
||||
|
||||
You need to link an object file with other object file or a shared
|
||||
library to make it exectuable.
|
||||
|
||||
## Virtual address space
|
||||
|
||||
A pointer has a value like 0x803020 which is an address of the
|
||||
pointee. But it doesn't mean that the pointee resides at the
|
||||
physical memory address 0x803020 on the computer. Modern CPUs
|
||||
contains so-called Mmeory Management Unit (MMU), and all access to
|
||||
the memory are first translated by MMU to the physical address.
|
||||
The address before translation is called the "virtual address".
|
||||
Unless you are doing the kernel programming, all addresses you
|
||||
handle are virtual addresses.
|
||||
|
||||
The OS kernel controls the MMU so that each process owns the entire
|
||||
virtual address space. So, even if two process uses the same virtual
|
||||
address, they don't conflict. They are mapped to different physical
|
||||
addresses.
|
||||
|
||||
The existence of MMU has several implications to the linker. First,
|
||||
we can link the main executable to a specific address. On process
|
||||
startup, there's no code or data in the virtual address space, so
|
||||
the mapping of the main executable always succeed. However, it's not
|
||||
true to DSOs because they are loaded after the main executable and
|
||||
possibly other DSOs. Therefore, shared libraries must be linked in a
|
||||
way that they can be loaded to any address in the virtual address
|
||||
space.
|
||||
|
||||
## Relocation
|
||||
|
||||
A piece of information for the linker as to how to link object files
|
||||
or a dynamic objects.
|
||||
|
||||
Object files can refer functions or data in other object files. For
|
||||
example, if you compile a function which calls a non-local function
|
||||
`foo`, the resulting code contains something like this:
|
||||
|
||||
```
|
||||
26: e8 00 00 00 00 callq 2b <bar+0xb>
|
||||
27: R_X86_64_PLT32 foo-0x4
|
||||
```
|
||||
|
||||
The above `callq` is the instruction to call a function at the
|
||||
machine code level. It's opcode is `0xe8` in x86-64, so the
|
||||
instruction begins with `0xe8`. The following four bytes are
|
||||
displacement; that is, the address of the branch target relative to
|
||||
the end of this `callq` instruction. Notice that the displacement is
|
||||
0. The compiler couldn't fill the displacement because it has no
|
||||
idea as to where `foo` will be at runtime. So, the compiler write 0
|
||||
as a placeholder and instead write a relocation `R_X86_64_PLT32`
|
||||
with `foo` as its associated symbol. The linker reads this
|
||||
relocation, computes the offsets between this call instruction and
|
||||
function `foo` and overwrite the placeholder value 0 with an actual
|
||||
displacement.
|
||||
|
||||
There are many different types of relocations. For example, if you
|
||||
want to fix up not with a displacement but with an absolute address
|
||||
of a symbol, you need to use `R_X86_64_ABS64` instead.
|
||||
|
||||
## Static library
|
||||
|
||||
A .a file. Often called as an archive file or just archive as well.
|
||||
|
||||
A static library is a container just like tar or zip. Actually,
|
||||
there's no technical reason to not use tar or (uncompressed) zip,
|
||||
but traditionally the .a file format is used by the linker.
|
||||
|
||||
A static library contains object files and can be passed to the
|
||||
linker along with other object files and/or archives.
|
||||
|
||||
A linker pulls out object files from an archive only if it is needed
|
||||
to resolve undefined symbols. In other words, object files in an
|
||||
archive are not linked by default and used as a complement to supply
|
||||
missing definitions. This is ideal for a library because you don't
|
||||
want to link library functions unless you are actually using them.
|
||||
|
||||
Contrary to archive files, object files directly given to a linker
|
||||
are always linked to the output.
|
||||
|
||||
To maximize the benefit of archive files, a library often used as a
|
||||
static library is broken down to small files to separate each
|
||||
function individually (for example, look at
|
||||
https://git.musl-libc.org/cgit/musl/tree/src/stdio). By doing this,
|
||||
you import only used functions.
|
||||
|
||||
A static file is created by `ar`, whose command line arguments are
|
||||
similar to `tar`. A static library contains the symbol table which
|
||||
offers a quick way to look up an object file for a defined symbol,
|
||||
but mold does not use the static library's symbol table. mold
|
||||
doesdn't need a symbol table to exist in an archive, and if exists,
|
||||
mold just ignores it.
|
||||
|
||||
See also: DSO (dynamic library)
|
||||
|
||||
## Symbol
|
||||
|
||||
A symbol is a label assigned to a specific location in an input file
|
||||
or an output file. For example, if you define function `foo` and
|
||||
compile it, the resulting object file contains a symbol `foo`
|
||||
pointing to the beginning of the machine code for `foo`.
|
||||
|
||||
Usually, a symbol name is a function or a variable name. If an
|
||||
object is anonymous (such the one for a string literal), a compiler
|
||||
generated a unique symbol, which often starts with `.` to avoid
|
||||
conflict with user-defined symbols.
|
||||
|
||||
For C++, symbol name is a complex "mangled" name. We need to mangle
|
||||
identifiers because a simple name such as `foo` cannot be uniquely
|
||||
identify a function or a data in C++, because for example `foo` may
|
||||
be in a namespace or defined as a static member in some class. If
|
||||
`foo` is an overloaded function, we need to distinguish different
|
||||
`foo`s by its type. Therefore, C++ compiler mangles an identifier by
|
||||
appending nmaepsace names, type information and such so that
|
||||
different things get different names.
|
||||
|
||||
For example, a function `int foo(int)` in a namespace `bar` is
|
||||
mangled as `_ZN3bar3fooEi`.
|
||||
|
||||
A symbol can be either defined or undefined. A defined symbol points
|
||||
to some location in a file which may contain the function's machine
|
||||
code or the variable's initial value. An undefined symbol does not
|
||||
point to anywhere. It needs to be merged with a defined symbol with
|
||||
the same name at link-time. This merging process is called "name
|
||||
resolution".
|
||||
|
||||
For example, if your program is using `printf`, it usually contains
|
||||
`printf` as an undefined symbol. You need to link it with `libc.a`
|
||||
or `libc.so`, which contain a defined symbol of `printf`, to make a
|
||||
complete program.
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 13 KiB |
+950
-552
File diff suppressed because it is too large.
Load diff
Binary file not shown.
|
After Width: | Height: | Size: 95 KiB |
-935
@@ -1,935 +0,0 @@
|
||||
mold(1) -- a modern linker
|
||||
==========================
|
||||
|
||||
## SYNOPSIS
|
||||
|
||||
`mold` [_option_...] _file_...
|
||||
|
||||
## DESCRIPTION
|
||||
`mold` is a faster drop-in replacement for the default GNU ld(1).
|
||||
|
||||
### How to use
|
||||
See <https://github.com/rui314/mold#how-to-use>.
|
||||
|
||||
### Compatibility
|
||||
|
||||
`mold` is designed to be a drop-in replacement for the GNU linkers for
|
||||
linking user-land programs. If your user-land program cannot be built due to
|
||||
missing command-line options, please file a bug at
|
||||
<https://github.com/rui314/mold/issues>.
|
||||
|
||||
`mold` supports a very limited set of linker script features, which is just
|
||||
sufficient to read `/usr/lib/x86_64-linux-gnu/libc.so` on Linux systems (on
|
||||
Linux, that file is contrary to its name not a shared library but an ASCII
|
||||
linker script that loads a real `libc.so` file.)
|
||||
|
||||
Beyond that, we have no plan to support any additional linker script features.
|
||||
The linker script is an ad-hoc, over-designed, complex language which we
|
||||
believe needs to be replaced by a simpler mechanism. We have a plan to add a
|
||||
replacement for the linker script to `mold` instead.
|
||||
|
||||
### Archive symbol resolution
|
||||
|
||||
Traditionally, Unix linkers are sensitive to the order in which input files
|
||||
appear on the command line. They process input files from the first (leftmost)
|
||||
file to the last (rightmost) file one-by-one. While reading input files, they
|
||||
maintain sets of defined and undefined symbols. When visiting an archive file
|
||||
(`.a` files), they pull out object files to resolve as many undefined symbols
|
||||
as possible and move on to the next input file. Object files that weren't
|
||||
pulled out will never have a chance for a second look.
|
||||
|
||||
Due to this behavior, you usually have to add archive files at the end of a
|
||||
command line, so that when a linker reaches archive files, it knows what
|
||||
symbols remain as undefined.
|
||||
|
||||
If you put archive files at the beginning of a command line, a linker doesn't
|
||||
have any undefined symbols, and thus no object files will be pulled out from
|
||||
archives. You can change the processing order by using the `--start-group` and
|
||||
`--end-group` options, though they make a linker slower.
|
||||
|
||||
`mold`, as well as the LLVM lld(1) linker, takes a different approach. They
|
||||
remember which symbols can be resolved from archive files instead of
|
||||
forgetting them after processing each archive. Therefore, `mold` and lld(1)
|
||||
can "go back" in a command line to pull out object files from archives if they
|
||||
are needed to resolve remaining undefined symbols. They are not sensitive to
|
||||
the input file order.
|
||||
|
||||
`--start-group` and `--end-group` are still accepted by `mold` and lld(1)
|
||||
for compatibility with traditional linkers, but they are silently ignored.
|
||||
|
||||
### Dynamic symbol resolution
|
||||
|
||||
Some Unix linker features are difficult to understand without comprehending
|
||||
the semantics of dynamic symbol resolution. Therefore, even though it's not
|
||||
specific to `mold`, we'll explain it here.
|
||||
|
||||
We use "ELF module" or just "module" as a collective term to refer to an
|
||||
executable or a shared library file in the ELF format.
|
||||
|
||||
An ELF module may have lists of imported symbols and exported symbols, as well
|
||||
as a list of shared library names from which imported symbols should be
|
||||
imported. The point is that imported symbols are not bound to any specific
|
||||
shared library until runtime.
|
||||
|
||||
Here is how the Unix dynamic linker resolves dynamic symbols. Upon the start
|
||||
of an ELF program, the dynamic linker constructs a list of ELF modules which,
|
||||
as a whole, consist of a complete program. The executable file is always at
|
||||
the beginning of the list followed by its dependent shared libraries. An
|
||||
imported symbol is searched from the beginning of the list to the end. If two
|
||||
or more modules define the same symbol, the one that appears first in the list
|
||||
takes precedence over the others.
|
||||
|
||||
This Unix semantics are contrary to systems such as Windows that have a
|
||||
two-level namespace for dynamic symbols. On Windows, for example, dynamic
|
||||
symbols are represented as a tuple of (`symbol-name`, `shared-library-name`),
|
||||
so that each dynamic symbol is guaranteed to be resolved from some specific
|
||||
library.
|
||||
|
||||
Typically, an ELF module that exports a symbol also imports the same symbol.
|
||||
Such a symbol is usually resolved to itself, but that's not the case if a
|
||||
module that appears before it in the symbol search list provides another
|
||||
definition of the same symbol.
|
||||
|
||||
Let's take `malloc` as an example. Assume that you define your version of
|
||||
`malloc` in your main executable file. Then, all `malloc` calls from any
|
||||
module are resolved to your function instead of the one in libc, because the
|
||||
executable is always at the beginning of the dynamic symbol search list. Note
|
||||
that even `malloc` calls within libc are resolved to your definition since
|
||||
libc exports and imports `malloc`. Therefore, by defining `malloc` yourself,
|
||||
you can overwrite a library function, and the `malloc` in libc becomes dead
|
||||
code.
|
||||
|
||||
These Unix semantics are tricky and sometimes considered harmful. For example,
|
||||
assume that you accidentally define `atoi` as a global function in your
|
||||
executable that behaves completely differently from the one in the C standard.
|
||||
Then, all `atoi` function calls from any modules (even function calls within
|
||||
libc) are redirected to your function instead of the one in libc, which will
|
||||
very likely cause a problem. That is a somewhat surprising consequence for an
|
||||
accidental name conflict. On the other hand, this semantic is sometimes useful
|
||||
because it allows users to override library functions without rebuilding
|
||||
modules containing them.
|
||||
|
||||
Whether good or bad, you should keep these semantics in mind to understand
|
||||
Unix linkers' behaviors.
|
||||
|
||||
### Build reproducibility
|
||||
|
||||
`mold`'s output is deterministic. That is, if you pass the same object files
|
||||
and the same command-line options to the same version of `mold`, it is
|
||||
guaranteed that `mold` produces the bit-for-bit identical output. The linker's
|
||||
internal randomness, such as the timing of thread scheduling or iteration
|
||||
orders of hash tables, doesn't affect the output.
|
||||
|
||||
`mold` does not have any host-specific default settings. This is contrary to the
|
||||
GNU linkers, for which some configurable values, such as system-dependent
|
||||
library search paths, are hard-coded. `mold` depends only on its command-line
|
||||
arguments.
|
||||
|
||||
## OPTION NOTATIONS
|
||||
|
||||
Multi-letter long options may precede either a single dash or double dashes,
|
||||
except for those starting with the letter "o". For historical reasons, long
|
||||
options beginning with "o" must precede double dashes.
|
||||
|
||||
For example, you can spell `--as-needed` as `-as-needed`, but `--omagic` must
|
||||
not be spelled as `-omagic`. `-omagic` will be interpreted not as `--omagic`
|
||||
but as `-o magic`.
|
||||
|
||||
## MOLD-SPECIFIC OPTIONS
|
||||
|
||||
* `--chroot`=_dir_:
|
||||
Set _dir_ as the root directory.
|
||||
|
||||
* `--color-diagnostics`=[ _auto_ | _always_ | _never_ ]:
|
||||
Show diagnostic messages in color using ANSI escape sequences. `auto` means
|
||||
that `mold` prints out messages in color only if the standard output is
|
||||
connected to a TTY. Default is `auto`.
|
||||
|
||||
* `--color-diagnostics`:
|
||||
Synonym for `--color-diagnostics=auto`.
|
||||
|
||||
* `--no-color-diagnostics`:
|
||||
Synonym for `--color-diagnostics=never`.
|
||||
|
||||
* `--detach`, `--no-detach`:
|
||||
Permit or do not permit mold to create a debug info file in the background.
|
||||
|
||||
* `--fork`, `--no-fork`:
|
||||
Spawn a child process and let it do the actual linking. When linking a large
|
||||
program, the OS kernel can take a few hundred milliseconds to terminate a
|
||||
`mold` process. `--fork` hides that latency. By default, it does fork.
|
||||
|
||||
* `--perf`:
|
||||
Print performance statistics.
|
||||
|
||||
* `--print-dependencies`:
|
||||
Print out dependency information for input files.
|
||||
|
||||
Each line of the output for this option shows which file depends on which
|
||||
file to use a specific symbol. This option is useful for debugging why some
|
||||
object file in a static archive got linked or why some shared library is
|
||||
kept in an output file's dependency list even with `--as-needed`.
|
||||
|
||||
* `--relocatable-merge-sections`:
|
||||
By default, `mold` doesn't merge input sections by name when merging input
|
||||
object files into a single output object file for `-r`. For example,
|
||||
`.text.foo` and `.text.bar` aren't merged for `-r` even though they are
|
||||
merged into `.text` based on the default section merging rules.
|
||||
|
||||
This option changes the behavior so that `mold` merges input sections by
|
||||
name by the default section merging rules.
|
||||
|
||||
* `--repro`:
|
||||
Archive input files, as well as a text file containing command line options,
|
||||
in a tar file so that you can run `mold` with the exact same inputs again.
|
||||
This is useful for reporting a bug with a reproducer. The output filename is
|
||||
`path/to/output.tar`, where `path/to/output` is an output filename specified
|
||||
by `-o`.
|
||||
|
||||
* `--reverse-sections`:
|
||||
Reverse the order of input sections before assigning them the offsets in the
|
||||
output file.
|
||||
|
||||
This option is useful for finding bugs that depend on the initialization
|
||||
order of global objects. In C++, constructors of global objects in a single
|
||||
source file are guaranteed to be executed in the source order, but there's
|
||||
no such guarantee across compilation units. Usually, constructors are
|
||||
executed in the order given to the linker, but depending on it is a mistake.
|
||||
|
||||
By reversing the order of input sections using `--reverse-sections`, you can
|
||||
easily test that your program works in the reversed initialization order.
|
||||
|
||||
* `--run` _command_ _arg_...:
|
||||
Run _command_ with `mold` as `/usr/bin/ld`. Specifically, `mold` runs a
|
||||
given command with the `LD_PRELOAD` environment set to intercept exec(3)
|
||||
family functions and replaces `argv[0]` with itself if it is `ld`,
|
||||
`ld.gold`, or `ld.lld`.
|
||||
|
||||
* `--separate-debug-file`, `--separate-debug-file`=_file_:
|
||||
Bundle debug info sections into a separate file instead of embedding them in
|
||||
an output executable or a shared library. mold creates a debug info file in
|
||||
the background by default, so that you can start running your executable as
|
||||
soon as possible.
|
||||
|
||||
By default, the debug info file is created in the same directory as is the
|
||||
output file, with the `.dbg` file extension. That filename is embedded into
|
||||
the output file so that `gdb` can automatically find the debug info file for
|
||||
the output file. For more info about gdb features related to separate debug
|
||||
files, see
|
||||
<https://sourceware.org/gdb/current/onlinedocs/gdb.html/Separate-Debug-Files.html>.
|
||||
|
||||
mold holds a file lock with flock(2) while creating a debug info file in the
|
||||
background.
|
||||
|
||||
If you don't want to create a debug info file in the background, pass the
|
||||
`--no-detach` option.
|
||||
|
||||
* `--shuffle-sections`, `--shuffle-sections`=_number_:
|
||||
Randomize the output by shuffling the order of input sections before
|
||||
assigning them the offsets in the output file. If a _number_ is given, it's
|
||||
used as a seed for the random number generator, so that the linker produces
|
||||
the same output for the same seed. If no seed is given, a random number is
|
||||
used as a seed.
|
||||
|
||||
This option is useful for benchmarking. Modern CPUs are sensitive to a
|
||||
program's memory layout. A seemingly benign change in program layout, such
|
||||
as a small size increase of a function in the middle of a program, can
|
||||
affect the program's performance. Therefore, even if you write new code and
|
||||
get a good benchmark result, it is hard to say whether the new code improves
|
||||
the program's performance; it is possible that the new memory layout happens
|
||||
to perform better.
|
||||
|
||||
By running a benchmark multiple times with randomized memory layouts using
|
||||
`--shuffle-sections`, you can isolate your program's real performance number
|
||||
from the randomness caused by memory layout changes.
|
||||
|
||||
* `--spare-program-headers`=_number_:
|
||||
Append the given number of `PT_NULL` entries to the end of the program
|
||||
header, so that post-link processing tools can easily add new segments by
|
||||
overwriting the null entries.
|
||||
|
||||
Note that ELF requires all `PT_LOAD` segments to be sorted by `p_vaddr`.
|
||||
Therefore, if you add a new LOAD segment, you may need to sort the entire
|
||||
program header.
|
||||
|
||||
* `--stats`:
|
||||
Print input statistics.
|
||||
|
||||
* `--thread-count`=_count_:
|
||||
Use _count_ number of threads.
|
||||
|
||||
* `--threads`, `--no-threads`:
|
||||
Use multiple threads. By default, `mold` uses as many threads as the number of
|
||||
cores or 32, whichever is smaller. The reason it is capped at 32 is because
|
||||
`mold` doesn't scale well beyond that point. To use only one thread, pass
|
||||
`--no-threads` or `--thread-count=1`.
|
||||
|
||||
* `--quick-exit`, `--no-quick-exit`:
|
||||
Use or do not use `quick_exit` to exit.
|
||||
|
||||
* `-z rewrite-endbr`, `-z norewrite-endbr`:
|
||||
As a security measure, some CPU instruction sets have recently gained a
|
||||
feature to protect control flow integrity by disallowing indirect branches
|
||||
by default. If the feature is enabled, the instruction that is executed
|
||||
immediately after an indirect branch must be an branch target marker
|
||||
instruction, or a CPU-level fault will raise. The marker instruction is also
|
||||
known as "landing pad" instruction, to which indirect branches can land.
|
||||
This feature makes ROP attacks harder to conduct.
|
||||
|
||||
To use the feature, a function whose pointer is taken needs to begin with a
|
||||
landing pad because a function call via a function pointer is compiled to an
|
||||
indirect branch. On the other hand, if a function is called only directly
|
||||
(i.e. referred to only by _direct_ branch instructions), it doesn't have to
|
||||
begin with it.
|
||||
|
||||
By default, the compiler always emits a landing pad at the beginning of each
|
||||
global function because it doesn't know whether or not the function's
|
||||
pointer is taken in another translation unit. As a result, the resulting
|
||||
binary has more attack surface than necessary.
|
||||
|
||||
If `--rewrite-endbr` is given, mold conducts a whole program analysis
|
||||
to identify functions whose addresses are actually taken and rewrites
|
||||
landing pads with no-ops for non-address-taken functions, reducing the
|
||||
attack surface.
|
||||
|
||||
This feature is currently available only on x86-64.
|
||||
|
||||
## GNU-COMPATIBLE OPTIONS
|
||||
|
||||
* `--help`:
|
||||
Report usage information to stdout and exit.
|
||||
|
||||
* `-v`, `--version`:
|
||||
Report version information to stdout.
|
||||
|
||||
* `-V`:
|
||||
Report version and target information to stdout.
|
||||
|
||||
* `-E`, `--export-dynamic`, `--no-export-dynamic`:
|
||||
When creating an executable, using the `-E` option causes all global symbols
|
||||
to be put into the dynamic symbol table, so that the symbols are visible
|
||||
from other ELF modules at runtime.
|
||||
|
||||
By default, or if `--no-export-dynamic` is given, only symbols that are
|
||||
referenced by DSOs at link-time are exported from an executable.
|
||||
|
||||
* `-F` _libname_, `--filter`=_libname_:
|
||||
Set the `DT_FILTER` dynamic section field to _libname_.
|
||||
|
||||
* `-I` _file_, `--dynamic-linker`=_file_, `--no-dynamic-linker`:
|
||||
Set the dynamic linker path to _file_. If no `-I` option is given, or if
|
||||
`--no-dynamic-linker` is given, no dynamic linker path is set to an output
|
||||
file. This is contrary to the GNU linkers which set a default dynamic linker
|
||||
path in that case. This difference doesn't usually make any difference
|
||||
because the compiler driver always passes `-I` to the linker.
|
||||
|
||||
* `-L` _dir_, `--library-path`=_dir_:
|
||||
Add _dir_ to the list of library search paths from which `mold` searches
|
||||
libraries for the `-l` option.
|
||||
|
||||
Unlike the GNU linkers, `mold` does not have default search paths. This
|
||||
difference doesn't usually make any difference because the compiler driver
|
||||
always passes all necessary search paths to the linker.
|
||||
|
||||
* `-M`, `--print-map`:
|
||||
Write a map file to stdout.
|
||||
|
||||
* `-N`, `--omagic`, `--no-omagic`:
|
||||
Force `mold` to emit an output file with an old-fashioned memory layout.
|
||||
First, it makes the first data segment not aligned to a page boundary.
|
||||
Second, text segments are marked as writable if the option is given.
|
||||
|
||||
* `-S`, `--strip-debug`:
|
||||
Omit `.debug_*` sections from the output file.
|
||||
|
||||
* `-T` _file_, `--script`=_file_:
|
||||
Read linker script from _file_.
|
||||
|
||||
* `-X`, `--discard-locals`:
|
||||
Discard temporary local symbols to reduce the sizes of the symbol table and
|
||||
the string table. Temporary local symbols are local symbols starting with
|
||||
`.L`. Compilers usually generate such symbols for unnamed program elements
|
||||
such as string literals or floating-point literals.
|
||||
|
||||
* `-e` _symbol_, `--entry`=_symbol_:
|
||||
|
||||
Use _symbol_ as the entry point symbol instead of the default entry
|
||||
point symbol _start.
|
||||
|
||||
* `-f` _shlib_, `--auxiliary`=_shlib_:
|
||||
Set the `DT_AUXILIARY` dynamic section field to _shlib_.
|
||||
|
||||
* `-h` _libname_, `--soname`=_libname_:
|
||||
Set the `DT_SONAME` dynamic section field to _libname_. This option is used
|
||||
when creating a shared object file. Typically, when you create `libfoo.so`,
|
||||
you want to pass `--soname=foo` to a linker.
|
||||
|
||||
* `-l` _libname_:
|
||||
Search for `lib`_libname_`.so` or `lib`_libname_`.a` from library search
|
||||
paths.
|
||||
|
||||
* `-m` _target_:
|
||||
Choose a _target_.
|
||||
|
||||
* `-o` _file_, `--output`=_file_:
|
||||
Use _file_ as the output file name instead of the default name `a.out`.
|
||||
|
||||
* `-r`, `--relocatable`:
|
||||
Instead of generating an executable or a shared object file, combine input
|
||||
object files to generate another object file that can be used as an input to
|
||||
a linker.
|
||||
|
||||
* `-s`, `--strip-all`:
|
||||
Omit `.symtab` section from the output file.
|
||||
|
||||
* `-u` _symbol_, `--undefined`=_symbol_:
|
||||
If _symbol_ remains as an undefined symbol after reading all object files,
|
||||
and if there is a static archive that contains an object file defining
|
||||
_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.
|
||||
|
||||
* `--Bstatic`:
|
||||
Do not link against shared libraries.
|
||||
|
||||
* `--Bsymbolic`:
|
||||
When creating a shared library, make global symbols export-only (i.e. do not
|
||||
import the same symbol). As a result, references within a shared library are
|
||||
always resolved locally, negating symbol override at runtime. See "Dynamic
|
||||
symbol resolution" for more information about symbol imports and exports.
|
||||
|
||||
* `--Bsymbolic-functions`:
|
||||
This option has the same effect as `--Bsymbolic` but works only for function
|
||||
symbols. Data symbols remain being both imported and exported.
|
||||
|
||||
* `--Bsymbolic-non-weak`:
|
||||
This option has the same effect as `--Bsymbolic` but works only for non-weak
|
||||
symbols. Weak symbols remain being both imported and exported.
|
||||
|
||||
* `--Bsymbolic-non-weak-functions`:
|
||||
This option has the same effect as `--Bsymbolic` but works only for non-weak
|
||||
function symbols. Data symbols and weak function symbols remain being both
|
||||
imported and exported.
|
||||
|
||||
* `--Bno-symbolic`:
|
||||
Cancel `--Bsymbolic`, `--Bsymbolic-functions`, `--Bsymbolic-non-weak` and
|
||||
`--Bsymbolic-non-weak-functions`.
|
||||
|
||||
* `--Map`=_file_:
|
||||
Write map file to _file_.
|
||||
|
||||
* `--Tbss`=_address_:
|
||||
Alias for `--section-start=.bss=`_address_.
|
||||
|
||||
* `--Tdata`=_address_:
|
||||
Alias for `--section-start=.data=`_address_.
|
||||
|
||||
* `--Ttext`=_address_:
|
||||
Alias for `--section-start=.text=`_address_.
|
||||
|
||||
* `--allow-multiple-definition`:
|
||||
Normally, the linker reports an error if there are more than one definition
|
||||
of a symbol. This option changes the default behavior so that it doesn't
|
||||
report an error for duplicate definitions and instead use the first
|
||||
definition.
|
||||
|
||||
* `--allow-shlib-undefined`, `--no-allow-shlib-undefined`:
|
||||
Even if mold succeeds in linking a main executable without undefined symbol
|
||||
errors, you may still encounter symbol lookup errors at runtime because the
|
||||
dynamic linker cannot find some symbols in shared libraries in any ELF
|
||||
module. This occurs because mold ignores undefined symbols in shared
|
||||
libraries by default.
|
||||
|
||||
If you pass `--no-allow-shlib-undefined`, mold verifies that undefined
|
||||
symbols in shared libraries given to the linker can be resolved at
|
||||
link-time. In other words, this converts the runtime error to a link-time
|
||||
error.
|
||||
|
||||
Note that you need to pass all shared libraries, including indirectly
|
||||
dependent ones, to the linker as arguments for `-l`. If a shared library
|
||||
depends on a library that's not passed to the linker, the verification will
|
||||
be skipped for that file.
|
||||
|
||||
* `--as-needed`, `--no-as-needed`:
|
||||
By default, shared libraries given to the linker are unconditionally added
|
||||
to the list of required libraries in an output file. However, shared
|
||||
libraries after `--as-needed` are added to the list only when at least one
|
||||
symbol is actually used by the output file. In other words, shared libraries
|
||||
after `--as-needed` are not added to the list of needed libraries if they
|
||||
are not needed by a program.
|
||||
|
||||
The `--no-as-needed` option restores the default behavior for subsequent
|
||||
files.
|
||||
|
||||
* `--build-id`=[ `md5` | `sha1` | `sha256` | `fast` | `uuid` | `0x`_hexstring_ | `none` ]:
|
||||
Create a `.note.gnu.build-id` section containing a byte string to uniquely
|
||||
identify an output file. `sha256` compute a 256-bit cryptographic hash of an
|
||||
output file and set it to build-id. `md5` and `sha1` compute the same hash
|
||||
but truncate it to 128 and 160 bits, respectively, before setting it to
|
||||
build-id. `uuid` sets a random 128-bit UUID. `0x`_hexstring_ sets
|
||||
_hexstring_. `fast` is a synonym for `sha256`.
|
||||
|
||||
* `--build-id`:
|
||||
Synonym for `--build-id=sha256`.
|
||||
|
||||
* `--no-build-id`:
|
||||
Synonym for `--build-id=none`.
|
||||
|
||||
* `--compress-debug-sections`=[ `zlib` | `zlib-gabi` | `zstd` | `none` ]:
|
||||
Compress DWARF debug info (`.debug_*` sections) using the zlib or zstd
|
||||
compression algorithm. `zlib-gabi` is an alias for `zlib`.
|
||||
|
||||
* `--defsym`=_symbol_=_value_:
|
||||
Define _symbol_ as an alias for _value_.
|
||||
|
||||
_value_ is either an integer (in decimal or hexadecimal with `0x` prefix) or
|
||||
a symbol name. If an integer is given as a value, _symbol_ is defined as an
|
||||
absolute symbol with the given value.
|
||||
|
||||
* `--default-symver`:
|
||||
Use soname as a symbol version and append that version to all symbols.
|
||||
|
||||
* `--demangle`, `--no-demangle`:
|
||||
Demangle C++ and Rust symbols in log messages.
|
||||
|
||||
* `--dependency-file`=_file_:
|
||||
Write a dependency file to _file_. The contents of the written file is
|
||||
readable by make(1), which defines only one rule with the linker's output
|
||||
file as a target and all input files as its prerequisites. Users are
|
||||
expected to include the generated dependency file into a Makefile to
|
||||
automate the dependency management. This option is analogous to the
|
||||
compiler's `-MM -MF` options.
|
||||
|
||||
* `--dynamic-list`=_file_:
|
||||
Read a list of dynamic symbols from _file_. Same as
|
||||
`--export-dynamic-symbol-list`, except that it implies `--Bsymbolic`. If
|
||||
_file_ does not exist in the current directory, it is searched from library
|
||||
search paths for the sake of compatibility with GNU ld.
|
||||
|
||||
* `--eh-frame-hdr`, `--no-eh-frame-hdr`:
|
||||
Create `.eh_frame_hdr` section.
|
||||
|
||||
* `--emit-relocs`:
|
||||
The linker usually "consumes" relocation sections. That is, the linker
|
||||
applies relocations to other sections, and relocation sections themselves
|
||||
are discarded.
|
||||
|
||||
The `--emit-relocs` instructs the linker to leave relocation sections in the
|
||||
output file. Some post-link binary analysis or optimization tools such as
|
||||
LLVM Bolt need them.
|
||||
|
||||
* `--enable-new-dtags`, `--disable-new-dtags`:
|
||||
By default, `mold` emits `DT_RUNPATH` for `--rpath`. If you pass
|
||||
`--disable-new-dtags`, `mold` emits `DT_RPATH` for `--rpath` instead.
|
||||
|
||||
* `--execute-only`:
|
||||
|
||||
Traditionally, setting the executable bit to 1 for a memory page implies
|
||||
that the page also become readable, which allows machine code to be read
|
||||
as data at runtime. That is actually what an attacker often does after
|
||||
gaining a limited control of a process to find pieces of machine code
|
||||
they can use to gain the full control of the process. As a mitigation,
|
||||
recent processors including some ARM64 ones allows "execute-only" pages.
|
||||
If a page is execute-only, you can call a function there as long as you
|
||||
know its address but can't read it as data.
|
||||
|
||||
This option marks text segments as execute-only by setting just the "X"
|
||||
bit instead of "RX". Note that on most systems, the absence of the "R"
|
||||
bit in the text segment serves just as a hint. If you run a program
|
||||
linked with `--execute-only` on a processor that doesn't support
|
||||
execute-only pages, your executable will likely still function normally,
|
||||
but the text segment will remain readable.
|
||||
|
||||
* `--exclude-libs`=_libraries_ ...:
|
||||
Mark all symbols in the given _libraries_ hidden.
|
||||
|
||||
* `--export-dynamic-symbol`=_symbol_:
|
||||
Put symbols matching _symbol_ in the dynamic symbol table. _symbol_ may be a
|
||||
glob pattern in the same syntax as for the `--export-dynamic-symbol-list` or
|
||||
`--version-script` options.
|
||||
|
||||
* `--export-dynamic-symbol-list`=_file_:
|
||||
Read a list of dynamic symbols from _file_.
|
||||
|
||||
* `--fatal-warnings`, `--no-fatal-warnings`:
|
||||
Treat warnings as errors.
|
||||
|
||||
* `--fini`=_symbol_:
|
||||
Call _symbol_ at unload-time.
|
||||
|
||||
* `--gc-sections`, `--no-gc-sections`:
|
||||
Remove unreferenced sections.
|
||||
|
||||
* `--gdb-index`:
|
||||
Create a `.gdb_index` section to speed up GNU debugger. To use this, you
|
||||
need to compile source files with the `-ggnu-pubnames` compiler flag.
|
||||
|
||||
* `--hash-style`=[ `sysv` | `gnu` | `both` | `none` ]:
|
||||
Set hash style.
|
||||
|
||||
* `--icf`=[ `safe` | `all` | `none` ], `--no-icf`:
|
||||
It is not uncommon for a program to contain many identical functions that
|
||||
differ only in name. For example, a C++ template `std::vector` is very
|
||||
likely to be instantiated to the identical code for `std::vector<int>` and
|
||||
`std::vector<unsigned>` because the container cares only about the size of
|
||||
the parameter type. Identical Code Folding (ICF) is a size optimization to
|
||||
identify and merge such identical functions.
|
||||
|
||||
If `--icf=all` is given, `mold` tries to merge all identical functions. This
|
||||
reduces the size of the output most, but it is not a "safe" optimization. It
|
||||
is guaranteed in C and C++ that two pointers pointing two different
|
||||
functions will never be equal, but `--icf=all` breaks that assumption as two
|
||||
identical functions have the same address after merging. So a care must be
|
||||
taken when you use this flag that your program does not depend on the
|
||||
function pointer uniqueness.
|
||||
|
||||
`--icf=safe` is a flag to merge functions only when it is safe to do so.
|
||||
That is, if a program does not take an address of a function, it is safe to
|
||||
merge that function with other function, as you cannot compare a function
|
||||
pointer with something else without taking an address of a function.
|
||||
|
||||
`--icf=safe` needs to be used with a compiler that supports `.llvm_addrsig`
|
||||
section which contains the information as to what symbols are address-taken.
|
||||
LLVM/Clang supports that section by default. Since GCC does not support it
|
||||
yet, you cannot use `--icf=safe` with GCC (it doesn't do any harm but can't
|
||||
optimize at all.)
|
||||
|
||||
`--icf=none` and `--no-icf` disables ICF.
|
||||
|
||||
* `--ignore-data-address-equality`:
|
||||
Make ICF to merge not only functions but also data. This option should be
|
||||
used in combination with `--icf=all`.
|
||||
|
||||
* `--image-base`=_addr_:
|
||||
Set the base address to _addr_.
|
||||
|
||||
* `--init`=_symbol_:
|
||||
Call _symbol_ at load-time.
|
||||
|
||||
* `--no-undefined`:
|
||||
Report undefined symbols (even with `--shared`).
|
||||
|
||||
* `--noinhibit-exec`:
|
||||
Create an output file even if errors occur.
|
||||
|
||||
* `--package-metadata`=_percent-encoded-string_:
|
||||
Embed a specified string into the `.note.package` section. This option
|
||||
is designed for build scripts that generate binary packages, such as
|
||||
`.rpm` or `.deb`, to include package metadata in each executable. It
|
||||
simplifies the process of identifying the corresponding package for a
|
||||
given executable or core file.
|
||||
|
||||
An argument to this option is treated as percent-encoded and decoded
|
||||
before being inserted into the section, allowing you to avoid the use of
|
||||
the comma (`,`) character in the argument. This is useful because the
|
||||
compiler replaces all occurrences of commas in `-Wl,` with spaces before
|
||||
forwarding them to the linker. Note that `mold` always interprets the
|
||||
argument as percent-encoded, so you also need to escape all occurrences
|
||||
of `%` as `%25`.
|
||||
|
||||
* `--pack-dyn-relocs`=[ `relr` | `none` ]:
|
||||
If `relr` is specified, all `R_*_RELATIVE` relocations are put into
|
||||
`.relr.dyn` section instead of `.rel.dyn` or `.rela.dyn` section. Since
|
||||
`.relr.dyn` section uses a space-efficient encoding scheme, specifying this
|
||||
flag can reduce the size of the output. This is typically most effective for
|
||||
position-independent executable.
|
||||
|
||||
Note that a runtime loader has to support `.relr.dyn` to run executables or
|
||||
shared libraries linked with `--pack-dyn-relocs=relr`. As of 2022, only
|
||||
ChromeOS, Android and Fuchsia support it.
|
||||
|
||||
* `--pie`, `--pic-executable`, `--no-pie`, `--no-pic-executable`:
|
||||
Create a position-independent executable.
|
||||
|
||||
* `--print-gc-sections`, `--no-print-gc-sections`:
|
||||
Print removed unreferenced sections.
|
||||
|
||||
* `--print-icf-sections`, `--no-print-icf-sections`:
|
||||
Print folded identical sections.
|
||||
|
||||
* `--push-state`, `--pop-state`:
|
||||
`--push-state` saves the current values of `--as-needed`, `--whole-archive`,
|
||||
`--static`, and `--start-lib`. The saved values can be restored by
|
||||
pop-state.
|
||||
|
||||
`--push-state` and `--pop-state` pairs can nest.
|
||||
|
||||
These options are useful when you want to construct linker command line
|
||||
options programmatically. For example, if you want to link `libfoo.so` by
|
||||
as-needed basis but don't want to change the global state of `--as-needed`,
|
||||
you can append `--push-state --as-needed -lfoo --pop-state` to the linker
|
||||
command line options.
|
||||
|
||||
* `--relax, --no-relax`:
|
||||
Rewrite machine instructions with more efficient ones for some relocations.
|
||||
The feature is enabled by default.
|
||||
|
||||
* `--require-defined`=_symbol_:
|
||||
Like `--undefined`, except the new symbol must be defined by the end of the
|
||||
link.
|
||||
|
||||
* `--retain-symbols-file`=_file_:
|
||||
Keep only symbols listed in _file_. _file_ is a text file containing a
|
||||
symbol name on each line. `mold` discards all local symbols as well as
|
||||
global symbol that are not in _file_. Note that this option removes symbols
|
||||
only from `.symtab` section and does not affect `.dynsym` section, which is
|
||||
used for dynamic linking.
|
||||
|
||||
* `--rpath`=_dir_:
|
||||
Add _dir_ to runtime search paths.
|
||||
|
||||
* `--section-start`=_section_=_address_:
|
||||
Set _address_ to section. _address_ is a hexadecimal number that may start
|
||||
with an optional `0x`.
|
||||
|
||||
* `--shared`, `--Bshareable`:
|
||||
Create a share library.
|
||||
|
||||
* `--spare-dynamic-tags`=_number_:
|
||||
Append the given number of `DT_NULL` entries to the end of the `.dynamic`
|
||||
section, so that post-link processing tools can easily add new dynamic tags
|
||||
by overwriting the null entries.
|
||||
|
||||
* `--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
|
||||
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).
|
||||
|
||||
* `--static`:
|
||||
Do not link against shared libraries.
|
||||
|
||||
* `--sysroot`=_dir_:
|
||||
Set target system root directory to _dir_.
|
||||
|
||||
* `--trace`:
|
||||
Print name of each input file.
|
||||
|
||||
* `--undefined-glob`=_pattern_:
|
||||
Synonym for `--undefined`, except that `--undefined-glob` takes a glob
|
||||
pattern instead of just a single symbol name.
|
||||
|
||||
* `--undefined-version`, `--no-undefined-version`:
|
||||
By default, `mold` warns on a symbol specified by a version script or by
|
||||
`--export-dynamic-symbol` if it is not defined. You can silence the warning
|
||||
by `--undefined-version`.
|
||||
|
||||
* `--unique`=_pattern_:
|
||||
Don't merge input sections that match the given glob pattern _pattern_.
|
||||
|
||||
* `--unresolved-symbols`=[ `report-all` | `ignore-all` | `ignore-in-object-files` | `ignore-in-shared-libs` ]:
|
||||
How to handle undefined symbols.
|
||||
|
||||
* `--version-script`=_file_:
|
||||
Read version script from _file_. If _file_ does not exist in the current
|
||||
directory, it is searched from library search paths for the sake of
|
||||
compatibility with GNU ld.
|
||||
|
||||
* `--warn-common`, `--no-warn-common`:
|
||||
Warn about common symbols.
|
||||
|
||||
* `--warn-once`:
|
||||
Only warn once for each undefined symbol instead of warn for each relocation
|
||||
referring an undefined symbol.
|
||||
|
||||
* `--warn-unresolved-symbols`, `--error-unresolved-symbols`:
|
||||
Normally, the linker reports an error for unresolved symbols.
|
||||
`--warn-unresolved-symbols` option turns it into a warning.
|
||||
`--error-unresolved-symbols` option restores the default behavior.
|
||||
|
||||
* `--whole-archive`, `--no-whole-archive`:
|
||||
When archive files (`.a` files) are given to the linker, only object files
|
||||
that are needed to resolve undefined symbols are extracted from them and
|
||||
linked to an output file. `--whole-archive` changes that behavior for
|
||||
subsequent archives so that the linker extracts all object files and links
|
||||
them to an output. For example, if you are creating a shared object file and
|
||||
you want to include all archive members to the output, you should pass
|
||||
`--whole-archive`. `--no-whole-archive` restores the default behavior for
|
||||
subsequent archives.
|
||||
|
||||
* `--wrap`=_symbol_:
|
||||
Make _symbol_ be resolved to `__wrap_`_symbol_. The original symbol can be
|
||||
resolved as `__real_`_symbol_. This option is typically used for wrapping an
|
||||
existing function.
|
||||
|
||||
* `-z cet-report`=[ `warning` | `error` | `none` ]:
|
||||
Intel Control-flow Enforcement Technology (CET) is a new x86 feature
|
||||
available since Tiger Lake which is released in 2020. It defines new
|
||||
instructions to harden security to protect programs from control hijacking
|
||||
attacks. You can tell the compiler to use the feature by specifying the
|
||||
`-fcf-protection` flag.
|
||||
|
||||
`-z cet-report` flag is used to make sure that all object files were
|
||||
compiled with a correct `-fcf-protection` flag. If `warning` or `error` are
|
||||
given, `mold` prints out a warning or an error message if an object file was
|
||||
not compiled with the compiler flag.
|
||||
|
||||
`mold` looks for `GNU_PROPERTY_X86_FEATURE_1_IBT` bit and
|
||||
`GNU_PROPERTY_X86_FEATURE_1_SHSTK` bit in `.note.gnu.property` section to
|
||||
determine whether or not an object file was compiled with `-fcf-protection`.
|
||||
|
||||
* `-z now`, `-z lazy`:
|
||||
By default, functions referring to other ELF modules are resolved by the
|
||||
dynamic linker when they are called for the first time. `-z now` marks an
|
||||
executable or a shared library file so that all dynamic symbols are resolved
|
||||
when a file is loaded to memory. `-z lazy` restores the default behavior.
|
||||
|
||||
* `-z origin`:
|
||||
Mark object requiring immediate `$ORIGIN` processing at runtime.
|
||||
|
||||
* `-z ibt`:
|
||||
Turn on `GNU_PROPERTY_X86_FEATURE_1_IBT` bit in `.note.gnu.property` section
|
||||
to indicate that the output uses IBT-enabled PLT. This option implies `-z
|
||||
ibtplt`.
|
||||
|
||||
* `-z ibtplt`:
|
||||
Generate Intel Branch Tracking (IBT)-enabled PLT which is the default on
|
||||
x86-64. This is the default.
|
||||
|
||||
* `-z execstack`, `-z noexecstack`:
|
||||
By default, the pages for the stack area (i.e. the pages where local
|
||||
variables reside) are not executable for security reasons. `-z execstack`
|
||||
makes it executable. `-z noexecstack` restores the default behavior.
|
||||
|
||||
* `-z keep-text-section-prefix`, `-z nokeep-text-section-prefix`:
|
||||
Keep `.text.hot`, `.text.unknown`, `.text.unlikely`, `.text.startup`, and
|
||||
`.text.exit` as separate sections in the final binary instead of merging
|
||||
them as `.text`.
|
||||
|
||||
* `-z rodynamic`:
|
||||
Make the `.dynamic` section read-only.
|
||||
|
||||
* `-z relro`, `-z norelro`:
|
||||
Some sections such as `.dynamic` have to be writable only during a module is
|
||||
being loaded to memory. Once the dynamic linker finishes its job, such
|
||||
sections won't be mutated by anyone. As a security mitigation, it is
|
||||
preferred to make such segments read-only during program execution.
|
||||
|
||||
`-z relro` puts such sections into a special segment called `relro`. The
|
||||
dynamic linker makes a relro segment read-only after it finishes its job.
|
||||
|
||||
By default, `mold` generates a relro segment. `-z norelro` disables the
|
||||
feature.
|
||||
|
||||
* `-z sectionheader`, `-z nosectionheader`:
|
||||
`-z nosectionheader` tell the linker to omit the section header.
|
||||
By default, the linker does not omit the section header.
|
||||
|
||||
* `-z separate-loadable-segments`, `-z separate-code`, `-z noseparate-code`:
|
||||
If one memory page contains multiple segments, the page protection bits are
|
||||
set in such a way that the needed attributes (writable or executable) are
|
||||
satisfied for all segments. This usually happens at a boundary of two
|
||||
segments with two different attributes.
|
||||
|
||||
`separate-loadable-segments` adds paddings between segments with different
|
||||
attributes so that they do not share the same page. This is the default.
|
||||
|
||||
`separate-code` adds paddings only between executable and non-executable
|
||||
segments.
|
||||
|
||||
`noseparate-code` does not add any paddings between segments.
|
||||
|
||||
* `-z defs`, `-z nodefs`:
|
||||
Report undefined symbols (even with `--shared`).
|
||||
|
||||
* `-z shstk`:
|
||||
Enforce shadow stack by turning `GNU_PROPERTY_X86_FEATURE_1_SHSTK` bit in
|
||||
`.note.gnu.property` output section. Shadow stack is part of Intel
|
||||
Control-flow Enforcement Technology (CET), which is available since Tiger
|
||||
Lake (2020).
|
||||
|
||||
* `-z start_stop_visibility`=[ `hidden` | `protected` ]:
|
||||
If a section name is valid as a C identifier (i.e., it matches
|
||||
`/^[_a-zA-Z][_a-zA-Z0-9]*$/`), mold creates `__start_SECNAME` and
|
||||
`__stop_SECNAME` symbols to mark the beginning and end of the section,
|
||||
where `SECNAME` is the section name.
|
||||
|
||||
You can make these marker symbols visible from other ELF modules by passing
|
||||
`-z start_stop_visibility=protected`. Default is `hidden`.
|
||||
|
||||
* `-z text`, `-z notext`, `-z textoff`:
|
||||
`mold` by default reports an error if dynamic relocations are created in
|
||||
read-only sections. If `-z notext` or `-z textoff` are given, `mold` creates
|
||||
such dynamic relocations without reporting an error. `-z text` restores the
|
||||
default behavior.
|
||||
|
||||
* `-z max-page-size`=_number_:
|
||||
Some CPU ISAs support multiple memory page sizes. This option specifies the
|
||||
maximum page size that an output binary can run on. In general, binaries
|
||||
built for a larger page size can run on a system with a smaller page size,
|
||||
but not vice versa. The default value is 4 KiB for i386, x86-64, and RISC-V,
|
||||
and 64 KiB for ARM64.
|
||||
|
||||
* `-z nodefaultlib`:
|
||||
Make the dynamic loader ignore default search paths.
|
||||
|
||||
* `-z nodelete`:
|
||||
Mark DSO non-deletable at runtime.
|
||||
|
||||
* `-z nodlopen`:
|
||||
Mark DSO not available to dlopen(3). This option makes it possible for the
|
||||
linker to optimize thread-local variable accesses by rewriting instructions
|
||||
for some targets.
|
||||
|
||||
* `-z nodump`:
|
||||
Mark DSO not available to dldump(3).
|
||||
|
||||
* `-z nocopyreloc`:
|
||||
Do not create copy relocations.
|
||||
|
||||
* `-z initfirst`:
|
||||
Mark DSO to be initialized first at runtime.
|
||||
|
||||
* `-z interpose`:
|
||||
Mark object to interpose all DSOs but executable.
|
||||
|
||||
* `-(`, `-)`, `-EL`, `-O`_number_, `--dc`, `--dp`, `--end-group`, `--no-add-needed`, `--no-copy-dt-needed-entries`, `--nostdlib`, `--rpath-link=Ar dir`, `--sort-common`, `--sort-section`, `--start-group`, `--warn-constructors`, `--warn-once`, `--fix-cortex-a53-835769`, `--fix-cortex-a53-843419`, `-z combreloc`, `-z common-page-size`, `-z nocombreloc`:
|
||||
Ignored
|
||||
|
||||
## ENVIRONMENT VARIABLES
|
||||
|
||||
* `MOLD_JOBS`:
|
||||
If this variable is set to `1`, only one `mold` 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.
|
||||
|
||||
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.
|
||||
|
||||
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 `1` to see if it addresses the
|
||||
OOM issue.
|
||||
|
||||
Currently, any value other than `1` is silently ignored.
|
||||
|
||||
* `MOLD_DEBUG`:
|
||||
If this variable is set to a non-empty string, `mold` embeds its
|
||||
command-line options in the output file's `.comment` section.
|
||||
|
||||
* `MOLD_REPRO`:
|
||||
Setting this variable to a non-empty string has the same effect as passing
|
||||
the `--repro` option.
|
||||
|
||||
## SEE ALSO
|
||||
|
||||
gold(1), ld(1), elf(5), ld.so(8)
|
||||
|
||||
## AUTHOR
|
||||
|
||||
Rui Ueyama <ruiu@cs.stanford.edu>
|
||||
|
||||
## BUGS
|
||||
|
||||
Report bugs to <https://github.com/rui314/mold/issues>.
|
||||
@@ -0,0 +1,794 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include <tbb/parallel_for.h>
|
||||
#include <tbb/parallel_for_each.h>
|
||||
|
||||
namespace mold::elf {
|
||||
|
||||
using E = ARM64;
|
||||
|
||||
static void write_adr(u8 *buf, u64 val) {
|
||||
u32 hi = (val & 0x1ffffc) << 3;
|
||||
u32 lo = (val & 3) << 29;
|
||||
*(u32 *)buf = (*(u32 *)buf & 0x9f00001f) | hi | lo;
|
||||
}
|
||||
|
||||
// Returns [hi:lo] bits of val.
|
||||
static u64 bits(u64 val, u64 hi, u64 lo) {
|
||||
return (val >> lo) & (((u64)1 << (hi - lo + 1)) - 1);
|
||||
}
|
||||
|
||||
static u64 page(u64 val) {
|
||||
return val & ~(u64)0xfff;
|
||||
}
|
||||
|
||||
template <>
|
||||
void GotPltSection<E>::copy_buf(Context<E> &ctx) {
|
||||
u64 *buf = (u64 *)(ctx.buf + this->shdr.sh_offset);
|
||||
|
||||
// The first slot of .got.plt points to _DYNAMIC.
|
||||
buf[0] = ctx.dynamic ? ctx.dynamic->shdr.sh_addr : 0;
|
||||
buf[1] = 0;
|
||||
buf[2] = 0;
|
||||
|
||||
for (Symbol<E> *sym : ctx.plt->symbols)
|
||||
buf[sym->get_gotplt_idx(ctx)] = ctx.plt->shdr.sh_addr;
|
||||
}
|
||||
|
||||
static void write_plt_header(Context<E> &ctx, u8 *buf) {
|
||||
// Write PLT header
|
||||
static const u8 plt0[] = {
|
||||
0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
|
||||
0x10, 0x00, 0x00, 0x90, // adrp x16, .got.plt[2]
|
||||
0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, .got.plt[2]]
|
||||
0x10, 0x02, 0x00, 0x91, // add x16, x16, .got.plt[2]
|
||||
0x20, 0x02, 0x1f, 0xd6, // br x17
|
||||
0x1f, 0x20, 0x03, 0xd5, // nop
|
||||
0x1f, 0x20, 0x03, 0xd5, // nop
|
||||
0x1f, 0x20, 0x03, 0xd5, // nop
|
||||
};
|
||||
|
||||
u64 gotplt = ctx.gotplt->shdr.sh_addr + 16;
|
||||
u64 plt = ctx.plt->shdr.sh_addr;
|
||||
|
||||
memcpy(buf, plt0, sizeof(plt0));
|
||||
write_adr(buf + 4, bits(page(gotplt) - page(plt + 4), 32, 12));
|
||||
*(u32 *)(buf + 8) |= bits(gotplt, 11, 3) << 10;
|
||||
*(u32 *)(buf + 12) |= ((gotplt) & 0xfff) << 10;
|
||||
}
|
||||
|
||||
static void write_plt_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
|
||||
u8 *ent = buf + ctx.plt_hdr_size + sym.get_plt_idx(ctx) * ctx.plt_size;
|
||||
|
||||
static const u8 data[] = {
|
||||
0x10, 0x00, 0x00, 0x90, // adrp x16, .got.plt[n]
|
||||
0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, .got.plt[n]]
|
||||
0x10, 0x02, 0x00, 0x91, // add x16, x16, .got.plt[n]
|
||||
0x20, 0x02, 0x1f, 0xd6, // br x17
|
||||
};
|
||||
|
||||
u64 gotplt = sym.get_gotplt_addr(ctx);
|
||||
u64 plt = sym.get_plt_addr(ctx);
|
||||
|
||||
memcpy(ent, data, sizeof(data));
|
||||
write_adr(ent, bits(page(gotplt) - page(plt), 32, 12));
|
||||
*(u32 *)(ent + 4) |= bits(gotplt, 11, 3) << 10;
|
||||
*(u32 *)(ent + 8) |= (gotplt & 0xfff) << 10;
|
||||
}
|
||||
|
||||
template <>
|
||||
void PltSection<E>::copy_buf(Context<E> &ctx) {
|
||||
u8 *buf = ctx.buf + this->shdr.sh_offset;
|
||||
write_plt_header(ctx, buf);
|
||||
for (Symbol<E> *sym : symbols)
|
||||
write_plt_entry(ctx, buf, *sym);
|
||||
}
|
||||
|
||||
template <>
|
||||
void PltGotSection<E>::copy_buf(Context<E> &ctx) {
|
||||
u8 *buf = ctx.buf + this->shdr.sh_offset;
|
||||
|
||||
for (Symbol<E> *sym : symbols) {
|
||||
u8 *ent = buf + sym->get_pltgot_idx(ctx) * ARM64::pltgot_size;
|
||||
|
||||
static const u8 data[] = {
|
||||
0x10, 0x00, 0x00, 0x90, // adrp x16, GOT[n]
|
||||
0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, GOT[n]]
|
||||
0x20, 0x02, 0x1f, 0xd6, // br x17
|
||||
0x1f, 0x20, 0x03, 0xd5, // nop
|
||||
};
|
||||
|
||||
u64 got = sym->get_got_addr(ctx);
|
||||
u64 plt = sym->get_plt_addr(ctx);
|
||||
|
||||
memcpy(ent, data, sizeof(data));
|
||||
write_adr(ent, bits(page(got) - page(plt), 32, 12));
|
||||
*(u32 *)(ent + 4) |= bits(got, 11, 3) << 10;
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void EhFrameSection<E>::apply_reloc(Context<E> &ctx, ElfRel<E> &rel,
|
||||
u64 loc, u64 val) {
|
||||
u8 *base = ctx.buf + this->shdr.sh_offset;
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_AARCH64_ABS64:
|
||||
*(u64 *)(base + loc) = val;
|
||||
return;
|
||||
case R_AARCH64_PREL32:
|
||||
*(u32 *)(base + loc) = val - this->shdr.sh_addr - loc;
|
||||
return;
|
||||
case R_AARCH64_PREL64:
|
||||
*(u64 *)(base + loc) = val - this->shdr.sh_addr - loc;
|
||||
return;
|
||||
}
|
||||
Fatal(ctx) << "unsupported relocation in .eh_frame: " << rel;
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
|
||||
ElfRel<E> *dynrel = nullptr;
|
||||
std::span<ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
i64 frag_idx = 0;
|
||||
|
||||
if (ctx.reldyn)
|
||||
dynrel = (ElfRel<E> *)(ctx.buf + ctx.reldyn->shdr.sh_offset +
|
||||
file.reldyn_offset + this->reldyn_offset);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_AARCH64_NONE)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
const SectionFragmentRef<E> *frag_ref = nullptr;
|
||||
if (rel_fragments && rel_fragments[frag_idx].idx == i)
|
||||
frag_ref = &rel_fragments[frag_idx++];
|
||||
|
||||
auto overflow_check = [&](i64 val, i64 lo, i64 hi) {
|
||||
if (val < lo || hi <= val)
|
||||
Error(ctx) << *this << ": relocation " << rel << " against "
|
||||
<< sym << " out of range: " << val << " is not in ["
|
||||
<< lo << ", " << hi << ")";
|
||||
};
|
||||
|
||||
#define S (frag_ref ? frag_ref->frag->get_addr(ctx) : sym.get_addr(ctx))
|
||||
#define A (frag_ref ? frag_ref->addend : rel.r_addend)
|
||||
#define P (output_section->shdr.sh_addr + offset + rel.r_offset)
|
||||
#define G (sym.get_got_addr(ctx) - ctx.got->shdr.sh_addr)
|
||||
#define GOT ctx.got->shdr.sh_addr
|
||||
|
||||
if (needs_dynrel[i]) {
|
||||
*dynrel++ = {P, R_AARCH64_ABS64, (u32)sym.get_dynsym_idx(ctx), A};
|
||||
*(u64 *)loc = A;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (needs_baserel[i]) {
|
||||
if (!is_relr_reloc(ctx, rel))
|
||||
*dynrel++ = {P, R_AARCH64_RELATIVE, 0, (i64)(S + A)};
|
||||
*(u64 *)loc = S + A;
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_AARCH64_ABS64:
|
||||
*(u64 *)loc = S + A;
|
||||
continue;
|
||||
case R_AARCH64_LDST8_ABS_LO12_NC:
|
||||
*(u32 *)loc |= bits(S + A, 11, 0) << 10;
|
||||
continue;
|
||||
case R_AARCH64_LDST16_ABS_LO12_NC:
|
||||
*(u32 *)loc |= bits(S + A, 11, 1) << 10;
|
||||
continue;
|
||||
case R_AARCH64_LDST32_ABS_LO12_NC:
|
||||
*(u32 *)loc |= bits(S + A, 11, 2) << 10;
|
||||
continue;
|
||||
case R_AARCH64_LDST64_ABS_LO12_NC:
|
||||
*(u32 *)loc |= bits(S + A, 11, 3) << 10;
|
||||
continue;
|
||||
case R_AARCH64_LDST128_ABS_LO12_NC:
|
||||
*(u32 *)loc |= bits(S + A, 11, 4) << 10;
|
||||
continue;
|
||||
case R_AARCH64_ADD_ABS_LO12_NC:
|
||||
*(u32 *)loc |= bits(S + A, 11, 0) << 10;
|
||||
continue;
|
||||
case R_AARCH64_MOVW_UABS_G0_NC:
|
||||
*(u32 *)loc |= bits(S + A, 15, 0) << 5;
|
||||
continue;
|
||||
case R_AARCH64_MOVW_UABS_G1_NC:
|
||||
*(u32 *)loc |= bits(S + A, 31, 16) << 5;
|
||||
continue;
|
||||
case R_AARCH64_MOVW_UABS_G2_NC:
|
||||
*(u32 *)loc |= bits(S + A, 47, 32) << 5;
|
||||
continue;
|
||||
case R_AARCH64_MOVW_UABS_G3:
|
||||
*(u32 *)loc |= bits(S + A, 63, 48) << 5;
|
||||
continue;
|
||||
case R_AARCH64_ADR_GOT_PAGE: {
|
||||
i64 val = page(G + GOT + A) - page(P);
|
||||
overflow_check(val, -((i64)1 << 32), (i64)1 << 32);
|
||||
write_adr(loc, bits(val, 32, 12));
|
||||
continue;
|
||||
}
|
||||
case R_AARCH64_ADR_PREL_PG_HI21: {
|
||||
i64 val = page(S + A) - page(P);
|
||||
overflow_check(val, -((i64)1 << 32), (i64)1 << 32);
|
||||
write_adr(loc, bits(val, 32, 12));
|
||||
continue;
|
||||
}
|
||||
case R_AARCH64_ADR_PREL_LO21: {
|
||||
i64 val = S + A - P;
|
||||
overflow_check(val, -((i64)1 << 20), (i64)1 << 20);
|
||||
write_adr(loc, val);
|
||||
continue;
|
||||
}
|
||||
case R_AARCH64_CALL26:
|
||||
case R_AARCH64_JUMP26: {
|
||||
if (sym.esym().is_undef_weak()) {
|
||||
// On ARM, calling an weak undefined symbol jumps to the
|
||||
// next instruction.
|
||||
*(u32 *)loc |= 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
i64 lo = -(1 << 27);
|
||||
i64 hi = 1 << 27;
|
||||
i64 val = S + A - P;
|
||||
|
||||
if (val < lo || hi <= val) {
|
||||
RangeExtensionRef ref = range_extn[i];
|
||||
val = output_section->thunks[ref.thunk_idx]->get_addr(ref.sym_idx) + A - P;
|
||||
assert(lo <= val && val < hi);
|
||||
}
|
||||
|
||||
*(u32 *)loc |= (val >> 2) & 0x3ffffff;
|
||||
continue;
|
||||
}
|
||||
case R_AARCH64_CONDBR19: {
|
||||
i64 val = S + A - P;
|
||||
overflow_check(val, -((i64)1 << 20), (i64)1 << 20);
|
||||
*(u32 *)loc |= bits(val, 20, 2) << 5;
|
||||
continue;
|
||||
}
|
||||
case R_AARCH64_PREL16: {
|
||||
i64 val = S + A - P;
|
||||
overflow_check(val, -((i64)1 << 15), (i64)1 << 15);
|
||||
*(u16 *)loc = val;
|
||||
continue;
|
||||
}
|
||||
case R_AARCH64_PREL32: {
|
||||
i64 val = S + A - P;
|
||||
overflow_check(val, -((i64)1 << 31), (i64)1 << 32);
|
||||
*(u32 *)loc = val;
|
||||
continue;
|
||||
}
|
||||
case R_AARCH64_PREL64:
|
||||
*(u64 *)loc = S + A - P;
|
||||
continue;
|
||||
case R_AARCH64_LD64_GOT_LO12_NC:
|
||||
*(u32 *)loc |= bits(G + GOT + A, 11, 3) << 10;
|
||||
continue;
|
||||
case R_AARCH64_LD64_GOTPAGE_LO15: {
|
||||
i64 val = G + GOT + A - page(GOT);
|
||||
overflow_check(val, 0, 1 << 15);
|
||||
*(u32 *)loc |= bits(val, 14, 3) << 10;
|
||||
continue;
|
||||
}
|
||||
case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
|
||||
i64 val = page(sym.get_gottp_addr(ctx) + A) - page(P);
|
||||
overflow_check(val, -((i64)1 << 32), (i64)1 << 32);
|
||||
write_adr(loc, bits(val, 32, 12));
|
||||
continue;
|
||||
}
|
||||
case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
|
||||
*(u32 *)loc |= bits(sym.get_gottp_addr(ctx) + A, 11, 3) << 10;
|
||||
continue;
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
|
||||
i64 val = S + A - ctx.tls_begin + 16;
|
||||
overflow_check(val, 0, (i64)1 << 24);
|
||||
*(u32 *)loc |= bits(val, 23, 12) << 10;
|
||||
continue;
|
||||
}
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
|
||||
*(u32 *)loc |= bits(S + A - ctx.tls_begin + 16, 11, 0) << 10;
|
||||
continue;
|
||||
case R_AARCH64_TLSGD_ADR_PAGE21: {
|
||||
i64 val = page(sym.get_tlsgd_addr(ctx) + A) - page(P);
|
||||
overflow_check(val, -((i64)1 << 32), (i64)1 << 32);
|
||||
write_adr(loc, bits(val, 32, 12));
|
||||
continue;
|
||||
}
|
||||
case R_AARCH64_TLSGD_ADD_LO12_NC:
|
||||
*(u32 *)loc |= bits(sym.get_tlsgd_addr(ctx) + A, 11, 0) << 10;
|
||||
continue;
|
||||
case R_AARCH64_TLSDESC_ADR_PAGE21: {
|
||||
if (ctx.relax_tlsdesc && !sym.is_imported) {
|
||||
// adrp x0, 0 -> movz x0, #tls_ofset_hi, lsl #16
|
||||
i64 val = (S + A - ctx.tls_begin + 16);
|
||||
overflow_check(val, -((i64)1 << 32), (i64)1 << 32);
|
||||
*(u32 *)loc = 0xd2a00000 | (bits(val, 32, 16) << 5);
|
||||
} else {
|
||||
i64 val = page(sym.get_tlsdesc_addr(ctx) + A) - page(P);
|
||||
overflow_check(val, -((i64)1 << 32), (i64)1 << 32);
|
||||
write_adr(loc, bits(val, 32, 12));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
case R_AARCH64_TLSDESC_LD64_LO12:
|
||||
if (ctx.relax_tlsdesc && !sym.is_imported) {
|
||||
// ldr x2, [x0] -> movk x0, #tls_ofset_lo
|
||||
u32 offset_lo = (S + A - ctx.tls_begin + 16) & 0xffff;
|
||||
*(u32 *)loc = 0xf2800000 | (offset_lo << 5);
|
||||
} else {
|
||||
*(u32 *)loc |= bits(sym.get_tlsdesc_addr(ctx) + A, 11, 3) << 10;
|
||||
}
|
||||
continue;
|
||||
case R_AARCH64_TLSDESC_ADD_LO12:
|
||||
if (ctx.relax_tlsdesc && !sym.is_imported) {
|
||||
// add x0, x0, #0 -> nop
|
||||
*(u32 *)loc = 0xd503201f;
|
||||
} else {
|
||||
*(u32 *)loc |= bits(sym.get_tlsdesc_addr(ctx) + A, 11, 0) << 10;
|
||||
}
|
||||
continue;
|
||||
case R_AARCH64_TLSDESC_CALL:
|
||||
if (ctx.relax_tlsdesc && !sym.is_imported) {
|
||||
// blr x2 -> nop
|
||||
*(u32 *)loc = 0xd503201f;
|
||||
}
|
||||
continue;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
|
||||
#undef S
|
||||
#undef A
|
||||
#undef P
|
||||
#undef G
|
||||
#undef GOT
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
|
||||
std::span<ElfRel<E>> rels = get_rels(ctx);
|
||||
i64 frag_idx = 0;
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_AARCH64_NONE)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
if (!sym.file) {
|
||||
report_undef(ctx, sym);
|
||||
continue;
|
||||
}
|
||||
|
||||
SectionFragment<E> *frag;
|
||||
i64 addend;
|
||||
std::tie(frag, addend) = get_fragment(ctx, rel);
|
||||
|
||||
#define S (frag ? frag->get_addr(ctx) : sym.get_addr(ctx))
|
||||
#define A (frag ? addend : rel.r_addend)
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_AARCH64_ABS64:
|
||||
*(u64 *)loc = S + A;
|
||||
continue;
|
||||
case R_AARCH64_ABS32:
|
||||
*(u32 *)loc = S + A;
|
||||
continue;
|
||||
default:
|
||||
Fatal(ctx) << *this << ": invalid relocation for non-allocated sections: "
|
||||
<< rel;
|
||||
break;
|
||||
}
|
||||
|
||||
#undef S
|
||||
#undef A
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::scan_relocations(Context<E> &ctx) {
|
||||
assert(shdr.sh_flags & SHF_ALLOC);
|
||||
|
||||
this->reldyn_offset = file.num_dynrel * sizeof(ElfRel<E>);
|
||||
std::span<ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
// Scan relocations
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_AARCH64_NONE)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
|
||||
if (!sym.file) {
|
||||
report_undef(ctx, sym);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sym.get_type() == STT_GNU_IFUNC) {
|
||||
sym.flags |= NEEDS_GOT;
|
||||
sym.flags |= NEEDS_PLT;
|
||||
}
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_AARCH64_ABS64: {
|
||||
Action table[][4] = {
|
||||
// Absolute Local Imported data Imported code
|
||||
{ NONE, BASEREL, DYNREL, DYNREL }, // DSO
|
||||
{ NONE, BASEREL, DYNREL, DYNREL }, // PIE
|
||||
{ NONE, NONE, COPYREL, PLT }, // PDE
|
||||
};
|
||||
dispatch(ctx, table, i, rel, sym);
|
||||
break;
|
||||
}
|
||||
case R_AARCH64_ADR_GOT_PAGE:
|
||||
case R_AARCH64_LD64_GOT_LO12_NC:
|
||||
case R_AARCH64_LD64_GOTPAGE_LO15:
|
||||
sym.flags |= NEEDS_GOT;
|
||||
break;
|
||||
case R_AARCH64_CALL26:
|
||||
case R_AARCH64_JUMP26:
|
||||
if (sym.is_imported)
|
||||
sym.flags |= NEEDS_PLT;
|
||||
break;
|
||||
case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
|
||||
case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
|
||||
sym.flags |= NEEDS_GOTTP;
|
||||
break;
|
||||
case R_AARCH64_ADR_PREL_PG_HI21: {
|
||||
Action table[][4] = {
|
||||
// Absolute Local Imported data Imported code
|
||||
{ NONE, NONE, ERROR, ERROR }, // DSO
|
||||
{ NONE, NONE, COPYREL, PLT }, // PIE
|
||||
{ NONE, NONE, COPYREL, PLT }, // PDE
|
||||
};
|
||||
dispatch(ctx, table, i, rel, sym);
|
||||
break;
|
||||
}
|
||||
case R_AARCH64_TLSGD_ADR_PAGE21:
|
||||
sym.flags |= NEEDS_TLSGD;
|
||||
break;
|
||||
case R_AARCH64_TLSDESC_ADR_PAGE21:
|
||||
case R_AARCH64_TLSDESC_LD64_LO12:
|
||||
case R_AARCH64_TLSDESC_ADD_LO12:
|
||||
if (!ctx.relax_tlsdesc || sym.is_imported)
|
||||
sym.flags |= NEEDS_TLSDESC;
|
||||
break;
|
||||
case R_AARCH64_ADD_ABS_LO12_NC:
|
||||
case R_AARCH64_ADR_PREL_LO21:
|
||||
case R_AARCH64_CONDBR19:
|
||||
case R_AARCH64_LDST16_ABS_LO12_NC:
|
||||
case R_AARCH64_LDST32_ABS_LO12_NC:
|
||||
case R_AARCH64_LDST64_ABS_LO12_NC:
|
||||
case R_AARCH64_LDST128_ABS_LO12_NC:
|
||||
case R_AARCH64_LDST8_ABS_LO12_NC:
|
||||
case R_AARCH64_MOVW_UABS_G0_NC:
|
||||
case R_AARCH64_MOVW_UABS_G1_NC:
|
||||
case R_AARCH64_MOVW_UABS_G2_NC:
|
||||
case R_AARCH64_MOVW_UABS_G3:
|
||||
case R_AARCH64_PREL16:
|
||||
case R_AARCH64_PREL32:
|
||||
case R_AARCH64_PREL64:
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_HI12:
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
|
||||
case R_AARCH64_TLSGD_ADD_LO12_NC:
|
||||
case R_AARCH64_TLSDESC_CALL:
|
||||
break;
|
||||
default:
|
||||
Error(ctx) << *this << ": unknown relocation: " << rel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void reset_thunk(RangeExtensionThunk<E> &thunk) {
|
||||
for (Symbol<E> *sym : thunk.symbols) {
|
||||
sym->thunk_idx = -1;
|
||||
sym->thunk_sym_idx = -1;
|
||||
sym->flags &= (u8)~NEEDS_THUNK;
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_reachable(Context<E> &ctx, Symbol<E> &sym,
|
||||
InputSection<E> &isec, const ElfRel<E> &rel) {
|
||||
// We always create a thunk for an absolute symbol conservatively
|
||||
// because `shrink_sections` may increase a distance between a
|
||||
// branch instruction and an absolute symbol. Branching to an
|
||||
// absolute location is extremely rare in real code, though.
|
||||
if (sym.is_absolute())
|
||||
return false;
|
||||
|
||||
// Compute a distance between the relocated place and the symbol
|
||||
// and check if they are within reach.
|
||||
i64 S = sym.get_addr(ctx);
|
||||
i64 A = rel.r_addend;
|
||||
i64 P = isec.get_addr() + rel.r_offset;
|
||||
i64 val = S + A - P;
|
||||
return -(1 << 27) <= val && val < (1 << 27);
|
||||
}
|
||||
|
||||
// We create a thunk no further than 100 MiB from any section.
|
||||
constexpr i64 MAX_DISTANCE = 100 * 1024 * 1024;
|
||||
|
||||
// We create a thunk for each 10 MiB input sections.
|
||||
static constexpr i64 GROUP_SIZE = 10 * 1024 * 1024;
|
||||
|
||||
static void create_thunks(Context<E> &ctx, OutputSection<E> &osec) {
|
||||
std::span<InputSection<E> *> members = osec.members;
|
||||
members[0]->offset = 0;
|
||||
|
||||
// Initialize input sections with very large dummy offsets so that
|
||||
// sections that have got real offsets are separated from the ones
|
||||
// without in the virtual address space.
|
||||
tbb::parallel_for((i64)1, (i64)members.size(), [&](i64 i) {
|
||||
members[i]->offset = 1 << 31;
|
||||
});
|
||||
|
||||
// We create thunks from the beginning of the section to the end.
|
||||
// We manage progress using four offsets which increase monotonically.
|
||||
// The locations they point to are always A <= B <= C <= D.
|
||||
i64 a = 0;
|
||||
i64 b = 0;
|
||||
i64 c = 0;
|
||||
i64 d = 0;
|
||||
i64 offset = 0;
|
||||
|
||||
while (b < members.size()) {
|
||||
// Move D foward as far as we can jump from B to D.
|
||||
while (d < members.size() && offset - members[b]->offset < MAX_DISTANCE) {
|
||||
offset = align_to(offset, members[d]->shdr.sh_addralign);
|
||||
members[d]->offset = offset;
|
||||
offset += members[d]->shdr.sh_size;
|
||||
d++;
|
||||
}
|
||||
|
||||
// Move C forward so that C is apart from B by GROUP_SIZE.
|
||||
while (c < members.size() &&
|
||||
members[c]->offset - members[b]->offset < GROUP_SIZE)
|
||||
c++;
|
||||
|
||||
// Move A forward so that A is reachable from C.
|
||||
if (c > 0) {
|
||||
i64 c_end = members[c - 1]->offset + members[c - 1]->shdr.sh_size;
|
||||
while (a < osec.thunks.size() &&
|
||||
osec.thunks[a]->offset < c_end - MAX_DISTANCE)
|
||||
reset_thunk(*osec.thunks[a++]);
|
||||
}
|
||||
|
||||
// Create a thunk for input sections between B and C and place it at D.
|
||||
osec.thunks.emplace_back(new RangeExtensionThunk<E>{osec});
|
||||
|
||||
RangeExtensionThunk<E> &thunk = *osec.thunks.back();
|
||||
thunk.thunk_idx = osec.thunks.size() - 1;
|
||||
thunk.offset = offset;
|
||||
|
||||
// Scan relocations between B and C to collect symbols that need thunks.
|
||||
tbb::parallel_for_each(&members[b], &members[c], [&](InputSection<E> *isec) {
|
||||
std::span<ElfRel<E>> rels = isec->get_rels(ctx);
|
||||
isec->range_extn.resize(rels.size());
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type != R_AARCH64_CALL26 && rel.r_type != R_AARCH64_JUMP26)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *isec->file.symbols[rel.r_sym];
|
||||
|
||||
// Skip if the destination is within reach.
|
||||
if (is_reachable(ctx, sym, *isec, rel))
|
||||
continue;
|
||||
|
||||
// If the symbol is already in another thunk, reuse it.
|
||||
if (sym.thunk_idx != -1) {
|
||||
isec->range_extn[i] = {sym.thunk_idx, sym.thunk_sym_idx};
|
||||
continue;
|
||||
}
|
||||
|
||||
// Otherwise, add the symbol to this thunk if it's not added already.
|
||||
isec->range_extn[i] = {thunk.thunk_idx, -1};
|
||||
|
||||
if (!(sym.flags.fetch_or(NEEDS_THUNK) & NEEDS_THUNK)) {
|
||||
std::scoped_lock lock(thunk.mu);
|
||||
thunk.symbols.push_back(&sym);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Now that we know the number of symbols in the thunk, we can compute
|
||||
// its size.
|
||||
offset += thunk.size();
|
||||
|
||||
// Sort symbols added to the thunk to make the output deterministic.
|
||||
sort(thunk.symbols, [](Symbol<E> *a, Symbol<E> *b) { return *a < *b; });
|
||||
|
||||
// Assign offsets within the thunk to the symbols.
|
||||
for (i64 i = 0; Symbol<E> *sym : thunk.symbols) {
|
||||
sym->thunk_idx = thunk.thunk_idx;
|
||||
sym->thunk_sym_idx = i++;
|
||||
}
|
||||
|
||||
// Scan relocations again to fix symbol offsets in the last thunk.
|
||||
tbb::parallel_for_each(&members[b], &members[c], [&](InputSection<E> *isec) {
|
||||
std::span<ElfRel<E>> rels = isec->get_rels(ctx);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
RangeExtensionRef &ref = isec->range_extn[i];
|
||||
|
||||
if (ref.thunk_idx == thunk.thunk_idx) {
|
||||
Symbol<E> &sym = *isec->file.symbols[rels[i].r_sym];
|
||||
ref.sym_idx = sym.thunk_sym_idx;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Move B forward to point to the begining of the next group.
|
||||
b = c;
|
||||
}
|
||||
|
||||
while (a < osec.thunks.size())
|
||||
reset_thunk(*osec.thunks[a++]);
|
||||
|
||||
osec.shdr.sh_size = offset;
|
||||
}
|
||||
|
||||
static void mark_thunk_symbols(Context<E> &ctx, OutputSection<E> &osec) {
|
||||
for (std::unique_ptr<RangeExtensionThunk<E>> &thunk : osec.thunks) {
|
||||
i64 sz = thunk->symbols.size();
|
||||
thunk->symbol_map.resize(sz);
|
||||
thunk->used.reset(new std::atomic_bool[sz]{});
|
||||
}
|
||||
|
||||
// Mark referenced thunk symbols
|
||||
tbb::parallel_for_each(osec.members, [&](InputSection<E> *isec) {
|
||||
std::span<ElfRel<E>> rels = isec->get_rels(ctx);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
RangeExtensionRef &ref = isec->range_extn[i];
|
||||
if (ref.thunk_idx == -1)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *isec->file.symbols[rels[i].r_sym];
|
||||
if (!is_reachable(ctx, sym, *isec, rels[i]))
|
||||
osec.thunks[ref.thunk_idx]->used[ref.sym_idx] = true;
|
||||
}
|
||||
});
|
||||
|
||||
// Remove unreferenced thunk symbols
|
||||
tbb::parallel_for_each(osec.thunks,
|
||||
[&](std::unique_ptr<RangeExtensionThunk<E>> &thunk) {
|
||||
i64 i = 0;
|
||||
for (i64 j = 0; j < thunk->symbols.size(); j++) {
|
||||
if (thunk->used[j]) {
|
||||
thunk->symbol_map[j] = i;
|
||||
thunk->symbols[i] = thunk->symbols[j];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
thunk->symbols.resize(i);
|
||||
});
|
||||
}
|
||||
|
||||
static void shrink_section(Context<E> &ctx, OutputSection<E> &osec) {
|
||||
std::span<std::unique_ptr<RangeExtensionThunk<E>>> thunks = osec.thunks;
|
||||
std::span<InputSection<E> *> members = osec.members;
|
||||
|
||||
i64 offset = 0;
|
||||
|
||||
auto add_thunk = [&]() {
|
||||
thunks[0]->offset = offset;
|
||||
offset += thunks[0]->size();
|
||||
thunks = thunks.subspan(1);
|
||||
};
|
||||
|
||||
auto add_isec = [&]() {
|
||||
offset = align_to(offset, members[0]->shdr.sh_addralign);
|
||||
members[0]->offset = offset;
|
||||
offset += members[0]->shdr.sh_size;
|
||||
members = members.subspan(1);
|
||||
};
|
||||
|
||||
while (!thunks.empty() && !members.empty()) {
|
||||
if (thunks[0]->offset < members[0]->offset)
|
||||
add_thunk();
|
||||
else
|
||||
add_isec();
|
||||
}
|
||||
|
||||
while (!thunks.empty())
|
||||
add_thunk();
|
||||
while (!members.empty())
|
||||
add_isec();
|
||||
|
||||
assert(offset <= osec.shdr.sh_size);
|
||||
osec.shdr.sh_size = offset;
|
||||
}
|
||||
|
||||
// ARM64's call/jump instructions take 27 bits displacement, so they
|
||||
// can refer only up to ±128 MiB. If a branch target is further than
|
||||
// that, we need to let it branch to a linker-synthesized code
|
||||
// sequence that construct a full 32 bit address in a register and
|
||||
// jump there. That linker-synthesized code is called "thunk".
|
||||
i64 create_range_extension_thunks(Context<E> &ctx) {
|
||||
Timer t(ctx, "create_range_extension_thunks");
|
||||
|
||||
// First, we create thunks with a pessimistic assumption that all
|
||||
// out-of-section relocations would need thunks. To do so, we start
|
||||
// with an initial layout in which output sections are separated far
|
||||
// apart.
|
||||
for (i64 i = 0; Chunk<E> *chunk : ctx.chunks)
|
||||
chunk->shdr.sh_addr = i++ << 31;
|
||||
|
||||
std::vector<OutputSection<E> *> sections;
|
||||
for (std::unique_ptr<OutputSection<E>> &osec : ctx.output_sections)
|
||||
if (!osec->members.empty() && (osec->shdr.sh_flags & SHF_EXECINSTR))
|
||||
sections.push_back(osec.get());
|
||||
|
||||
for (OutputSection<E> *osec : sections)
|
||||
create_thunks(ctx, *osec);
|
||||
|
||||
// Recompute file layout.
|
||||
set_osec_offsets(ctx);
|
||||
|
||||
// Based on the current file layout, remove thunk symbols that turned
|
||||
// to be unnecessary.
|
||||
tbb::parallel_for_each(sections, [&](OutputSection<E> *osec) {
|
||||
mark_thunk_symbols(ctx, *osec);
|
||||
});
|
||||
|
||||
// Recompute output section sizes that contain thunks. New section
|
||||
// sizes must be equal to or smaller than previous values, so all
|
||||
// relocations that were previously reachable will still be reachable
|
||||
// after this step.
|
||||
for (OutputSection<E> *osec : sections)
|
||||
shrink_section(ctx, *osec);
|
||||
|
||||
// Compute the final layout.
|
||||
return set_osec_offsets(ctx);
|
||||
}
|
||||
|
||||
void RangeExtensionThunk<E>::copy_buf(Context<E> &ctx) {
|
||||
u8 *buf = ctx.buf + output_section.shdr.sh_offset + offset;
|
||||
|
||||
static const u8 insn[] = {
|
||||
0x10, 0x00, 0x00, 0x90, // adrp x16, 0 # R_AARCH64_ADR_PREL_PG_HI21
|
||||
0x10, 0x02, 0x00, 0x91, // add x16, x16 # R_AARCH64_ADD_ABS_LO12_NC
|
||||
0x00, 0x02, 0x1f, 0xd6, // br x16
|
||||
};
|
||||
|
||||
static_assert(ENTRY_SIZE == sizeof(insn));
|
||||
|
||||
for (i64 i = 0; i < symbols.size(); i++) {
|
||||
u64 S = symbols[i]->get_addr(ctx);
|
||||
u64 P = output_section.shdr.sh_addr + offset + i * ENTRY_SIZE;
|
||||
|
||||
u8 *loc = buf + i * ENTRY_SIZE;
|
||||
memcpy(loc , insn, sizeof(insn));
|
||||
write_adr(loc, bits(page(S) - page(P), 32, 12));
|
||||
*(u32 *)(loc + 4) |= bits(S, 11, 0) << 10;
|
||||
}
|
||||
}
|
||||
|
||||
void write_thunks(Context<E> &ctx) {
|
||||
Timer t(ctx, "write_thunks");
|
||||
|
||||
tbb::parallel_for_each(ctx.output_sections,
|
||||
[&](std::unique_ptr<OutputSection<E>> &osec) {
|
||||
tbb::parallel_for_each(osec->thunks,
|
||||
[&](std::unique_ptr<RangeExtensionThunk<E>> &thunk) {
|
||||
thunk->copy_buf(ctx);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace mold::elf
|
||||
@@ -0,0 +1,473 @@
|
||||
#include "mold.h"
|
||||
|
||||
namespace mold::elf {
|
||||
|
||||
using E = I386;
|
||||
|
||||
template <>
|
||||
void GotPltSection<E>::copy_buf(Context<E> &ctx) {
|
||||
u32 *buf = (u32 *)(ctx.buf + this->shdr.sh_offset);
|
||||
|
||||
// The first slot of .got.plt points to _DYNAMIC.
|
||||
buf[0] = ctx.dynamic ? ctx.dynamic->shdr.sh_addr : 0;
|
||||
buf[1] = 0;
|
||||
buf[2] = 0;
|
||||
|
||||
for (Symbol<E> *sym : ctx.plt->symbols)
|
||||
buf[sym->get_gotplt_idx(ctx)] = sym->get_plt_addr(ctx) + 6;
|
||||
}
|
||||
|
||||
static void write_plt_header(Context<E> &ctx, u8 *buf) {
|
||||
if (ctx.arg.pic) {
|
||||
static const u8 plt0[] = {
|
||||
0xff, 0xb3, 0x04, 0, 0, 0, // pushl 4(%ebx)
|
||||
0xff, 0xa3, 0x08, 0, 0, 0, // jmp *8(%ebx)
|
||||
0x90, 0x90, 0x90, 0x90, // nop
|
||||
};
|
||||
memcpy(buf, plt0, sizeof(plt0));
|
||||
} else {
|
||||
static const u8 plt0[] = {
|
||||
0xff, 0x35, 0, 0, 0, 0, // pushl (GOTPLT+4)
|
||||
0xff, 0x25, 0, 0, 0, 0, // jmp *(GOTPLT+8)
|
||||
0x90, 0x90, 0x90, 0x90, // nop
|
||||
};
|
||||
memcpy(buf, plt0, sizeof(plt0));
|
||||
*(u32 *)(buf + 2) = ctx.gotplt->shdr.sh_addr + 4;
|
||||
*(u32 *)(buf + 8) = ctx.gotplt->shdr.sh_addr + 8;
|
||||
}
|
||||
}
|
||||
|
||||
static void write_plt_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym,
|
||||
i64 idx) {
|
||||
u8 *ent = buf + ctx.plt_hdr_size + sym.get_plt_idx(ctx) * ctx.plt_size;
|
||||
|
||||
if (ctx.arg.pic) {
|
||||
static const u8 data[] = {
|
||||
0xff, 0xa3, 0, 0, 0, 0, // jmp *foo@GOT(%ebx)
|
||||
0x68, 0, 0, 0, 0, // pushl $reloc_offset
|
||||
0xe9, 0, 0, 0, 0, // jmp .PLT0@PC
|
||||
};
|
||||
memcpy(ent, data, sizeof(data));
|
||||
*(u32 *)(ent + 2) = sym.get_gotplt_addr(ctx) - ctx.gotplt->shdr.sh_addr;
|
||||
} else {
|
||||
static const u8 data[] = {
|
||||
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
|
||||
0x68, 0, 0, 0, 0, // pushl $reloc_offset
|
||||
0xe9, 0, 0, 0, 0, // jmp .PLT0@PC
|
||||
};
|
||||
memcpy(ent, data, sizeof(data));
|
||||
*(u32 *)(ent + 2) = sym.get_gotplt_addr(ctx);
|
||||
}
|
||||
|
||||
*(u32 *)(ent + 7) = idx * sizeof(ElfRel<E>);
|
||||
*(u32 *)(ent + 12) = ctx.plt->shdr.sh_addr - sym.get_plt_addr(ctx) - 16;
|
||||
}
|
||||
|
||||
template <>
|
||||
void PltSection<E>::copy_buf(Context<E> &ctx) {
|
||||
u8 *buf = ctx.buf + this->shdr.sh_offset;
|
||||
write_plt_header(ctx, buf);
|
||||
|
||||
for (i64 i = 0; i < symbols.size(); i++)
|
||||
write_plt_entry(ctx, buf, *symbols[i], i);
|
||||
}
|
||||
|
||||
template <>
|
||||
void PltGotSection<E>::copy_buf(Context<E> &ctx) {
|
||||
u8 *buf = ctx.buf + this->shdr.sh_offset;
|
||||
|
||||
if (ctx.arg.pic) {
|
||||
static const u8 data[] = {
|
||||
0xff, 0xa3, 0, 0, 0, 0, // jmp *foo@GOT(%ebx)
|
||||
0x66, 0x90, // nop
|
||||
};
|
||||
|
||||
for (i64 i = 0; i < symbols.size(); i++) {
|
||||
u8 *ent = buf + i * sizeof(data);
|
||||
memcpy(ent, data, sizeof(data));
|
||||
*(u32 *)(ent + 2) = symbols[i]->get_got_addr(ctx) - ctx.gotplt->shdr.sh_addr;
|
||||
}
|
||||
} else {
|
||||
static const u8 data[] = {
|
||||
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
|
||||
0x66, 0x90, // nop
|
||||
};
|
||||
|
||||
for (i64 i = 0; i < symbols.size(); i++) {
|
||||
u8 *ent = buf + i * sizeof(data);
|
||||
memcpy(ent, data, sizeof(data));
|
||||
*(u32 *)(ent + 2) = symbols[i]->get_got_addr(ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void EhFrameSection<E>::apply_reloc(Context<E> &ctx, ElfRel<E> &rel,
|
||||
u64 loc, u64 val) {
|
||||
u8 *base = ctx.buf + this->shdr.sh_offset;
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_386_NONE:
|
||||
return;
|
||||
case R_386_32:
|
||||
*(u32 *)(base + loc) = val;
|
||||
return;
|
||||
case R_386_PC32:
|
||||
*(u32 *)(base + loc) = val - this->shdr.sh_addr - loc;
|
||||
return;
|
||||
}
|
||||
unreachable();
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
|
||||
ElfRel<E> *dynrel = nullptr;
|
||||
std::span<ElfRel<E>> rels = get_rels(ctx);
|
||||
i64 frag_idx = 0;
|
||||
|
||||
if (ctx.reldyn)
|
||||
dynrel = (ElfRel<E> *)(ctx.buf + ctx.reldyn->shdr.sh_offset +
|
||||
file.reldyn_offset + this->reldyn_offset);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_386_NONE)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
const SectionFragmentRef<E> *frag_ref = nullptr;
|
||||
if (rel_fragments && rel_fragments[frag_idx].idx == i)
|
||||
frag_ref = &rel_fragments[frag_idx++];
|
||||
|
||||
auto overflow_check = [&](i64 val, i64 lo, i64 hi) {
|
||||
if (val < lo || hi <= val)
|
||||
Error(ctx) << *this << ": relocation " << rel << " against "
|
||||
<< sym << " out of range: " << val << " is not in ["
|
||||
<< lo << ", " << hi << ")";
|
||||
};
|
||||
|
||||
auto write8 = [&](u64 val) {
|
||||
overflow_check(val, 0, 1 << 8);
|
||||
*loc = val;
|
||||
};
|
||||
|
||||
auto write8s = [&](u64 val) {
|
||||
overflow_check(val, -(1 << 7), 1 << 7);
|
||||
*loc = val;
|
||||
};
|
||||
|
||||
auto write16 = [&](u64 val) {
|
||||
overflow_check(val, 0, 1 << 16);
|
||||
*(u16 *)loc = val;
|
||||
};
|
||||
|
||||
auto write16s = [&](u64 val) {
|
||||
overflow_check(val, -(1 << 15), 1 << 15);
|
||||
*(u16 *)loc = val;
|
||||
};
|
||||
|
||||
auto write32 = [&](u64 val) {
|
||||
*(u32 *)loc = val;
|
||||
};
|
||||
|
||||
#define S (frag_ref ? frag_ref->frag->get_addr(ctx) : sym.get_addr(ctx))
|
||||
#define A (frag_ref ? frag_ref->addend : this->get_addend(rel))
|
||||
#define P (output_section->shdr.sh_addr + offset + rel.r_offset)
|
||||
#define G (sym.get_got_addr(ctx) - ctx.got->shdr.sh_addr)
|
||||
#define GOTPLT ctx.gotplt->shdr.sh_addr
|
||||
|
||||
if (needs_dynrel[i]) {
|
||||
*dynrel++ = {P, R_386_32, (u32)sym.get_dynsym_idx(ctx)};
|
||||
write32(A);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (needs_baserel[i]) {
|
||||
if (!is_relr_reloc(ctx, rel))
|
||||
*dynrel++ = {P, R_386_RELATIVE, 0};
|
||||
write32(S + A);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_386_8:
|
||||
write8(S + A);
|
||||
continue;
|
||||
case R_386_16:
|
||||
write16(S + A);
|
||||
continue;
|
||||
case R_386_32:
|
||||
write32(S + A);
|
||||
continue;
|
||||
case R_386_PC8:
|
||||
write8s(S + A);
|
||||
continue;
|
||||
case R_386_PC16:
|
||||
write16s(S + A);
|
||||
continue;
|
||||
case R_386_PC32:
|
||||
case R_386_PLT32:
|
||||
write32(S + A - P);
|
||||
continue;
|
||||
case R_386_GOT32:
|
||||
case R_386_GOT32X:
|
||||
write32(sym.get_got_addr(ctx) + A - GOTPLT);
|
||||
continue;
|
||||
case R_386_GOTOFF:
|
||||
write32(S + A - GOTPLT);
|
||||
continue;
|
||||
case R_386_GOTPC:
|
||||
write32(GOTPLT + A - P);
|
||||
continue;
|
||||
case R_386_TLS_GOTIE:
|
||||
write32(sym.get_gottp_addr(ctx) + A - GOTPLT);
|
||||
continue;
|
||||
case R_386_TLS_LE:
|
||||
write32(S + A - ctx.tls_end);
|
||||
continue;
|
||||
case R_386_TLS_IE:
|
||||
write32(sym.get_gottp_addr(ctx) + A);
|
||||
continue;
|
||||
case R_386_TLS_GD:
|
||||
write32(sym.get_tlsgd_addr(ctx) + A - GOTPLT);
|
||||
continue;
|
||||
case R_386_TLS_LDM:
|
||||
write32(ctx.got->get_tlsld_addr(ctx) + A - GOTPLT);
|
||||
continue;
|
||||
case R_386_TLS_LDO_32:
|
||||
write32(S + A - ctx.tls_begin);
|
||||
continue;
|
||||
case R_386_SIZE32:
|
||||
write32(sym.esym().st_size + A);
|
||||
continue;
|
||||
case R_386_TLS_GOTDESC:
|
||||
if (sym.get_tlsdesc_idx(ctx) == -1) {
|
||||
static const u8 insn[] = {
|
||||
0x8d, 0x05, 0, 0, 0, 0, // lea 0, %eax
|
||||
};
|
||||
memcpy(loc - 2, insn, sizeof(insn));
|
||||
write32(S + A - ctx.tls_end);
|
||||
} else {
|
||||
write32(sym.get_tlsdesc_addr(ctx) + A - GOTPLT);
|
||||
}
|
||||
continue;
|
||||
case R_386_TLS_DESC_CALL:
|
||||
if (ctx.arg.relax && !ctx.arg.shared) {
|
||||
// call *(%rax) -> nop
|
||||
loc[0] = 0x66;
|
||||
loc[1] = 0x90;
|
||||
}
|
||||
continue;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
|
||||
#undef S
|
||||
#undef A
|
||||
#undef P
|
||||
#undef G
|
||||
#undef GOTPLT
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
|
||||
std::span<ElfRel<E>> rels = get_rels(ctx);
|
||||
i64 frag_idx = 0;
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_386_NONE)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
if (!sym.file) {
|
||||
report_undef(ctx, sym);
|
||||
continue;
|
||||
}
|
||||
|
||||
SectionFragment<E> *frag;
|
||||
i64 addend;
|
||||
std::tie(frag, addend) = get_fragment(ctx, rel);
|
||||
|
||||
auto overflow_check = [&](i64 val, i64 lo, i64 hi) {
|
||||
if (val < lo || hi <= val)
|
||||
Error(ctx) << *this << ": relocation " << rel << " against "
|
||||
<< sym << " out of range: " << val << " is not in ["
|
||||
<< lo << ", " << hi << ")";
|
||||
};
|
||||
|
||||
auto write8 = [&](u64 val) {
|
||||
overflow_check(val, 0, 1 << 8);
|
||||
*loc = val;
|
||||
};
|
||||
|
||||
auto write8s = [&](u64 val) {
|
||||
overflow_check(val, -(1 << 7), 1 << 7);
|
||||
*loc = val;
|
||||
};
|
||||
|
||||
auto write16 = [&](u64 val) {
|
||||
overflow_check(val, 0, 1 << 16);
|
||||
*(u16 *)loc = val;
|
||||
};
|
||||
|
||||
auto write16s = [&](u64 val) {
|
||||
overflow_check(val, -(1 << 15), 1 << 15);
|
||||
*(u16 *)loc = val;
|
||||
};
|
||||
|
||||
#define S (frag ? frag->get_addr(ctx) : sym.get_addr(ctx))
|
||||
#define A (frag ? addend : this->get_addend(rel))
|
||||
#define G (sym.get_got_addr(ctx) - ctx.got->shdr.sh_addr)
|
||||
#define GOTPLT ctx.gotplt->shdr.sh_addr
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_386_8:
|
||||
write8(S + A);
|
||||
continue;
|
||||
case R_386_16:
|
||||
write16(S + A);
|
||||
continue;
|
||||
case R_386_32:
|
||||
*(u32 *)loc = S + A;
|
||||
continue;
|
||||
case R_386_PC8:
|
||||
write8s(S + A);
|
||||
continue;
|
||||
case R_386_PC16:
|
||||
write16s(S + A);
|
||||
continue;
|
||||
case R_386_PC32:
|
||||
*(u32 *)loc = S + A;
|
||||
continue;
|
||||
case R_386_GOTPC:
|
||||
*(u32 *)loc = GOTPLT + A;
|
||||
continue;
|
||||
case R_386_GOTOFF:
|
||||
*(u32 *)loc = S + A - GOTPLT;
|
||||
continue;
|
||||
case R_386_TLS_LDO_32:
|
||||
*(u32 *)loc = S + A - ctx.tls_begin;
|
||||
continue;
|
||||
case R_386_SIZE32:
|
||||
*(u32 *)loc = sym.esym().st_size + A;
|
||||
continue;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
|
||||
#undef S
|
||||
#undef A
|
||||
#undef GOTPLT
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::scan_relocations(Context<E> &ctx) {
|
||||
assert(shdr.sh_flags & SHF_ALLOC);
|
||||
|
||||
this->reldyn_offset = file.num_dynrel * sizeof(ElfRel<E>);
|
||||
std::span<ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
// Scan relocations
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_386_NONE)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
|
||||
if (!sym.file) {
|
||||
report_undef(ctx, sym);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sym.get_type() == STT_GNU_IFUNC) {
|
||||
sym.flags |= NEEDS_GOT;
|
||||
sym.flags |= NEEDS_PLT;
|
||||
}
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_386_8:
|
||||
case R_386_16: {
|
||||
Action table[][4] = {
|
||||
// Absolute Local Imported data Imported code
|
||||
{ NONE, ERROR, ERROR, ERROR }, // DSO
|
||||
{ NONE, ERROR, ERROR, ERROR }, // PIE
|
||||
{ NONE, NONE, COPYREL, PLT }, // PDE
|
||||
};
|
||||
dispatch(ctx, table, i, rel, sym);
|
||||
break;
|
||||
}
|
||||
case R_386_32: {
|
||||
Action table[][4] = {
|
||||
// Absolute Local Imported data Imported code
|
||||
{ NONE, BASEREL, DYNREL, DYNREL }, // DSO
|
||||
{ NONE, BASEREL, DYNREL, DYNREL }, // PIE
|
||||
{ NONE, NONE, COPYREL, PLT }, // PDE
|
||||
};
|
||||
dispatch(ctx, table, i, rel, sym);
|
||||
break;
|
||||
}
|
||||
case R_386_PC8:
|
||||
case R_386_PC16: {
|
||||
Action table[][4] = {
|
||||
// Absolute Local Imported data Imported code
|
||||
{ ERROR, NONE, ERROR, ERROR }, // DSO
|
||||
{ ERROR, NONE, COPYREL, PLT }, // PIE
|
||||
{ NONE, NONE, COPYREL, PLT }, // PDE
|
||||
};
|
||||
dispatch(ctx, table, i, rel, sym);
|
||||
break;
|
||||
}
|
||||
case R_386_PC32: {
|
||||
Action table[][4] = {
|
||||
// Absolute Local Imported data Imported code
|
||||
{ BASEREL, NONE, DYNREL, DYNREL }, // DSO
|
||||
{ BASEREL, NONE, COPYREL, PLT }, // PIE
|
||||
{ NONE, NONE, COPYREL, PLT }, // PDE
|
||||
};
|
||||
dispatch(ctx, table, i, rel, sym);
|
||||
break;
|
||||
}
|
||||
case R_386_GOT32:
|
||||
case R_386_GOT32X:
|
||||
case R_386_GOTPC:
|
||||
sym.flags |= NEEDS_GOT;
|
||||
break;
|
||||
case R_386_PLT32:
|
||||
if (sym.is_imported)
|
||||
sym.flags |= NEEDS_PLT;
|
||||
break;
|
||||
case R_386_TLS_GOTIE:
|
||||
case R_386_TLS_LE:
|
||||
case R_386_TLS_IE:
|
||||
sym.flags |= NEEDS_GOTTP;
|
||||
break;
|
||||
case R_386_TLS_GD:
|
||||
sym.flags |= NEEDS_TLSGD;
|
||||
break;
|
||||
case R_386_TLS_LDM:
|
||||
sym.flags |= NEEDS_TLSLD;
|
||||
break;
|
||||
case R_386_TLS_GOTDESC:
|
||||
if (!ctx.arg.relax || ctx.arg.shared)
|
||||
sym.flags |= NEEDS_TLSDESC;
|
||||
break;
|
||||
case R_386_GOTOFF:
|
||||
case R_386_TLS_LDO_32:
|
||||
case R_386_SIZE32:
|
||||
case R_386_TLS_DESC_CALL:
|
||||
break;
|
||||
default:
|
||||
Error(ctx) << *this << ": unknown relocation: " << rel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mold::elf
|
||||
@@ -0,0 +1,749 @@
|
||||
#include "mold.h"
|
||||
|
||||
namespace mold::elf {
|
||||
|
||||
using E = X86_64;
|
||||
|
||||
template <>
|
||||
void GotPltSection<E>::copy_buf(Context<E> &ctx) {
|
||||
u64 *buf = (u64 *)(ctx.buf + this->shdr.sh_offset);
|
||||
|
||||
// The first slot of .got.plt points to _DYNAMIC, as requested by
|
||||
// the x86-64 psABI. The second and the third slots are reserved by
|
||||
// the psABI.
|
||||
buf[0] = ctx.dynamic ? ctx.dynamic->shdr.sh_addr : 0;
|
||||
buf[1] = 0;
|
||||
buf[2] = 0;
|
||||
|
||||
for (Symbol<E> *sym : ctx.plt->symbols) {
|
||||
if (ctx.arg.z_ibtplt)
|
||||
buf[sym->get_gotplt_idx(ctx)] = sym->get_plt_addr(ctx) + 10;
|
||||
else
|
||||
buf[sym->get_gotplt_idx(ctx)] = sym->get_plt_addr(ctx) + 6;
|
||||
}
|
||||
}
|
||||
|
||||
// The compact PLT format is used when `-z now` is given. If the flag
|
||||
// is given, all PLT symbols are resolved eagerly on startup, so we
|
||||
// can omit code for lazy symbol resolution from PLT in that case.
|
||||
static void write_compact_plt(Context<E> &ctx) {
|
||||
u8 *buf = ctx.buf + ctx.plt->shdr.sh_offset;
|
||||
|
||||
static const u8 data[] = {
|
||||
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
|
||||
0x66, 0x90, // nop
|
||||
};
|
||||
|
||||
for (Symbol<E> *sym : ctx.plt->symbols) {
|
||||
u8 *ent = buf + sym->get_plt_idx(ctx) * ctx.plt_size;
|
||||
memcpy(ent, data, sizeof(data));
|
||||
*(u32 *)(ent + 2) = sym->get_gotplt_addr(ctx) - sym->get_plt_addr(ctx) - 6;
|
||||
}
|
||||
}
|
||||
|
||||
// The IBTPLT is a security-enhanced version of the regular PLT.
|
||||
// It uses Indirect Branch Tracking (IBT) feature which is part of
|
||||
// Intel Control-Flow Enforcement (CET). IBTPLT is slightly larger
|
||||
// than the regular PLT (24 bytes vs 16 bytes for each entry).
|
||||
//
|
||||
// Note that our IBTPLT instruction sequence is different from the one
|
||||
// used in GNU ld. GNU's IBTPLT implementation uses two separate
|
||||
// sections (.plt and .plt.sec) in which one PLT entry takes 32 bytes
|
||||
// in total.
|
||||
static void write_ibtplt(Context<E> &ctx) {
|
||||
u8 *buf = ctx.buf + ctx.plt->shdr.sh_offset;
|
||||
|
||||
// Write PLT header
|
||||
static const u8 plt0[] = {
|
||||
0xff, 0x35, 0, 0, 0, 0, // push GOTPLT+8(%rip)
|
||||
0xff, 0x25, 0, 0, 0, 0, // jmp *GOTPLT+16(%rip)
|
||||
0x0f, 0x1f, 0x40, 0x00, // nop
|
||||
};
|
||||
|
||||
memcpy(buf, plt0, sizeof(plt0));
|
||||
*(u32 *)(buf + 2) = ctx.gotplt->shdr.sh_addr - ctx.plt->shdr.sh_addr + 2;
|
||||
*(u32 *)(buf + 8) = ctx.gotplt->shdr.sh_addr - ctx.plt->shdr.sh_addr + 4;
|
||||
|
||||
// Write PLT entries
|
||||
i64 relplt_idx = 0;
|
||||
|
||||
static const u8 data[] = {
|
||||
0xf3, 0x0f, 0x1e, 0xfa, // endbr64
|
||||
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOTPLT
|
||||
0x68, 0, 0, 0, 0, // push $index_in_relplt
|
||||
0xf2, 0xe9, 0, 0, 0, 0, // jmp PLT[0]
|
||||
0x0f, 0x1f, 0x00, // nop
|
||||
};
|
||||
|
||||
for (Symbol<E> *sym : ctx.plt->symbols) {
|
||||
u8 *ent = buf + ctx.plt_hdr_size + sym->get_plt_idx(ctx) * ctx.plt_size;
|
||||
memcpy(ent, data, sizeof(data));
|
||||
*(u32 *)(ent + 6) = sym->get_gotplt_addr(ctx) - sym->get_plt_addr(ctx) - 10;
|
||||
*(u32 *)(ent + 11) = relplt_idx++;
|
||||
*(u32 *)(ent + 17) = ctx.plt->shdr.sh_addr - sym->get_plt_addr(ctx) - 21;
|
||||
}
|
||||
}
|
||||
|
||||
// The regular PLT.
|
||||
static void write_plt(Context<E> &ctx) {
|
||||
u8 *buf = ctx.buf + ctx.plt->shdr.sh_offset;
|
||||
|
||||
// Write PLT header
|
||||
static const u8 plt0[] = {
|
||||
0xff, 0x35, 0, 0, 0, 0, // pushq GOTPLT+8(%rip)
|
||||
0xff, 0x25, 0, 0, 0, 0, // jmp *GOTPLT+16(%rip)
|
||||
0x0f, 0x1f, 0x40, 0x00, // nop
|
||||
};
|
||||
|
||||
memcpy(buf, plt0, sizeof(plt0));
|
||||
*(u32 *)(buf + 2) = ctx.gotplt->shdr.sh_addr - ctx.plt->shdr.sh_addr + 2;
|
||||
*(u32 *)(buf + 8) = ctx.gotplt->shdr.sh_addr - ctx.plt->shdr.sh_addr + 4;
|
||||
|
||||
// Write PLT entries
|
||||
i64 relplt_idx = 0;
|
||||
|
||||
static const u8 data[] = {
|
||||
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOTPLT
|
||||
0x68, 0, 0, 0, 0, // push $index_in_relplt
|
||||
0xe9, 0, 0, 0, 0, // jmp PLT[0]
|
||||
};
|
||||
|
||||
for (Symbol<E> *sym : ctx.plt->symbols) {
|
||||
u8 *ent = buf + ctx.plt_hdr_size + sym->get_plt_idx(ctx) * ctx.plt_size;
|
||||
memcpy(ent, data, sizeof(data));
|
||||
*(u32 *)(ent + 2) = sym->get_gotplt_addr(ctx) - sym->get_plt_addr(ctx) - 6;
|
||||
*(u32 *)(ent + 7) = relplt_idx++;
|
||||
*(u32 *)(ent + 12) = ctx.plt->shdr.sh_addr - sym->get_plt_addr(ctx) - 16;
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void PltSection<E>::copy_buf(Context<E> &ctx) {
|
||||
if (ctx.arg.z_now)
|
||||
write_compact_plt(ctx);
|
||||
else if (ctx.arg.z_ibtplt)
|
||||
write_ibtplt(ctx);
|
||||
else
|
||||
write_plt(ctx);
|
||||
}
|
||||
|
||||
template <>
|
||||
void PltGotSection<E>::copy_buf(Context<E> &ctx) {
|
||||
u8 *buf = ctx.buf + this->shdr.sh_offset;
|
||||
|
||||
static const u8 data[] = {
|
||||
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
|
||||
0x66, 0x90, // nop
|
||||
};
|
||||
|
||||
for (Symbol<E> *sym : symbols) {
|
||||
u8 *ent = buf + sym->get_pltgot_idx(ctx) * X86_64::pltgot_size;
|
||||
memcpy(ent, data, sizeof(data));
|
||||
*(u32 *)(ent + 2) = sym->get_got_addr(ctx) - sym->get_plt_addr(ctx) - 6;
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void EhFrameSection<E>::apply_reloc(Context<E> &ctx, ElfRel<E> &rel,
|
||||
u64 loc, u64 val) {
|
||||
u8 *base = ctx.buf + this->shdr.sh_offset;
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_X86_64_NONE:
|
||||
return;
|
||||
case R_X86_64_32:
|
||||
*(u32 *)(base + loc) = val;
|
||||
return;
|
||||
case R_X86_64_64:
|
||||
*(u64 *)(base + loc) = val;
|
||||
return;
|
||||
case R_X86_64_PC32:
|
||||
*(u32 *)(base + loc) = val - this->shdr.sh_addr - loc;
|
||||
return;
|
||||
case R_X86_64_PC64:
|
||||
*(u64 *)(base + loc) = val - this->shdr.sh_addr - loc;
|
||||
return;
|
||||
}
|
||||
unreachable();
|
||||
}
|
||||
|
||||
static u32 relax_gotpcrelx(u8 *loc) {
|
||||
switch ((loc[0] << 8) | loc[1]) {
|
||||
case 0xff15: return 0x90e8; // call *0(%rip) -> call 0
|
||||
case 0xff25: return 0x90e9; // jmp *0(%rip) -> jmp 0
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static u32 relax_rex_gotpcrelx(u8 *loc) {
|
||||
switch ((loc[0] << 16) | (loc[1] << 8) | loc[2]) {
|
||||
case 0x488b05: return 0x488d05; // mov 0(%rip), %rax -> lea 0(%rip), %rax
|
||||
case 0x488b0d: return 0x488d0d; // mov 0(%rip), %rcx -> lea 0(%rip), %rcx
|
||||
case 0x488b15: return 0x488d15; // mov 0(%rip), %rdx -> lea 0(%rip), %rdx
|
||||
case 0x488b1d: return 0x488d1d; // mov 0(%rip), %rbx -> lea 0(%rip), %rbx
|
||||
case 0x488b25: return 0x488d25; // mov 0(%rip), %rsp -> lea 0(%rip), %rsp
|
||||
case 0x488b2d: return 0x488d2d; // mov 0(%rip), %rbp -> lea 0(%rip), %rbp
|
||||
case 0x488b35: return 0x488d35; // mov 0(%rip), %rsi -> lea 0(%rip), %rsi
|
||||
case 0x488b3d: return 0x488d3d; // mov 0(%rip), %rdi -> lea 0(%rip), %rdi
|
||||
case 0x4c8b05: return 0x4c8d05; // mov 0(%rip), %r8 -> lea 0(%rip), %r8
|
||||
case 0x4c8b0d: return 0x4c8d0d; // mov 0(%rip), %r9 -> lea 0(%rip), %r9
|
||||
case 0x4c8b15: return 0x4c8d15; // mov 0(%rip), %r10 -> lea 0(%rip), %r10
|
||||
case 0x4c8b1d: return 0x4c8d1d; // mov 0(%rip), %r11 -> lea 0(%rip), %r11
|
||||
case 0x4c8b25: return 0x4c8d25; // mov 0(%rip), %r12 -> lea 0(%rip), %r12
|
||||
case 0x4c8b2d: return 0x4c8d2d; // mov 0(%rip), %r13 -> lea 0(%rip), %r13
|
||||
case 0x4c8b35: return 0x4c8d35; // mov 0(%rip), %r14 -> lea 0(%rip), %r14
|
||||
case 0x4c8b3d: return 0x4c8d3d; // mov 0(%rip), %r15 -> lea 0(%rip), %r15
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static u32 relax_gottpoff(u8 *loc) {
|
||||
switch ((loc[0] << 16) | (loc[1] << 8) | loc[2]) {
|
||||
case 0x488b05: return 0x48c7c0; // mov 0(%rip), %rax -> mov $0, %rax
|
||||
case 0x488b0d: return 0x48c7c1; // mov 0(%rip), %rcx -> mov $0, %rcx
|
||||
case 0x488b15: return 0x48c7c2; // mov 0(%rip), %rdx -> mov $0, %rdx
|
||||
case 0x488b1d: return 0x48c7c3; // mov 0(%rip), %rbx -> mov $0, %rbx
|
||||
case 0x488b25: return 0x48c7c4; // mov 0(%rip), %rsp -> mov $0, %rsp
|
||||
case 0x488b2d: return 0x48c7c5; // mov 0(%rip), %rbp -> mov $0, %rbp
|
||||
case 0x488b35: return 0x48c7c6; // mov 0(%rip), %rsi -> mov $0, %rsi
|
||||
case 0x488b3d: return 0x48c7c7; // mov 0(%rip), %rdi -> mov $0, %rdi
|
||||
case 0x4c8b05: return 0x49c7c0; // mov 0(%rip), %r8 -> mov $0, %r8
|
||||
case 0x4c8b0d: return 0x49c7c1; // mov 0(%rip), %r9 -> mov $0, %r9
|
||||
case 0x4c8b15: return 0x49c7c2; // mov 0(%rip), %r10 -> mov $0, %r10
|
||||
case 0x4c8b1d: return 0x49c7c3; // mov 0(%rip), %r11 -> mov $0, %r11
|
||||
case 0x4c8b25: return 0x49c7c4; // mov 0(%rip), %r12 -> mov $0, %r12
|
||||
case 0x4c8b2d: return 0x49c7c5; // mov 0(%rip), %r13 -> mov $0, %r13
|
||||
case 0x4c8b35: return 0x49c7c6; // mov 0(%rip), %r14 -> mov $0, %r14
|
||||
case 0x4c8b3d: return 0x49c7c7; // mov 0(%rip), %r15 -> mov $0, %r15
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Apply relocations to SHF_ALLOC sections (i.e. sections that are
|
||||
// mapped to memory at runtime) based on the result of
|
||||
// scan_relocations().
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
|
||||
ElfRel<E> *dynrel = nullptr;
|
||||
std::span<ElfRel<E>> rels = get_rels(ctx);
|
||||
i64 frag_idx = 0;
|
||||
|
||||
if (ctx.reldyn)
|
||||
dynrel = (ElfRel<E> *)(ctx.buf + ctx.reldyn->shdr.sh_offset +
|
||||
file.reldyn_offset + this->reldyn_offset);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_X86_64_NONE)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
const SectionFragmentRef<E> *frag_ref = nullptr;
|
||||
if (rel_fragments && rel_fragments[frag_idx].idx == i)
|
||||
frag_ref = &rel_fragments[frag_idx++];
|
||||
|
||||
auto overflow_check = [&](i64 val, i64 lo, i64 hi) {
|
||||
if (val < lo || hi <= val)
|
||||
Error(ctx) << *this << ": relocation " << rel << " against "
|
||||
<< sym << " out of range: " << val << " is not in ["
|
||||
<< lo << ", " << hi << ")";
|
||||
};
|
||||
|
||||
auto write8 = [&](u64 val) {
|
||||
overflow_check(val, 0, 1 << 8);
|
||||
*loc = val;
|
||||
};
|
||||
|
||||
auto write8s = [&](u64 val) {
|
||||
overflow_check(val, -(1 << 7), 1 << 7);
|
||||
*loc = val;
|
||||
};
|
||||
|
||||
auto write16 = [&](u64 val) {
|
||||
overflow_check(val, 0, 1 << 16);
|
||||
*(u16 *)loc = val;
|
||||
};
|
||||
|
||||
auto write16s = [&](u64 val) {
|
||||
overflow_check(val, -(1 << 15), 1 << 15);
|
||||
*(u16 *)loc = val;
|
||||
};
|
||||
|
||||
auto write32 = [&](u64 val) {
|
||||
overflow_check(val, 0, (i64)1 << 32);
|
||||
*(u32 *)loc = val;
|
||||
};
|
||||
|
||||
auto write32s = [&](u64 val) {
|
||||
overflow_check(val, -((i64)1 << 31), (i64)1 << 31);
|
||||
*(u32 *)loc = val;
|
||||
};
|
||||
|
||||
auto write64 = [&](u64 val) {
|
||||
*(u64 *)loc = val;
|
||||
};
|
||||
|
||||
#define S (frag_ref ? frag_ref->frag->get_addr(ctx) : sym.get_addr(ctx))
|
||||
#define A (frag_ref ? frag_ref->addend : rel.r_addend)
|
||||
#define P (output_section->shdr.sh_addr + offset + rel.r_offset)
|
||||
#define G (sym.get_got_addr(ctx) - ctx.gotplt->shdr.sh_addr)
|
||||
#define GOT ctx.gotplt->shdr.sh_addr
|
||||
|
||||
if (needs_dynrel[i]) {
|
||||
*dynrel++ = {P, R_X86_64_64, (u32)sym.get_dynsym_idx(ctx), A};
|
||||
write64(A);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (needs_baserel[i]) {
|
||||
if (!is_relr_reloc(ctx, rel))
|
||||
*dynrel++ = {P, R_X86_64_RELATIVE, 0, (i64)(S + A)};
|
||||
write64(S + A);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_X86_64_8:
|
||||
write8(S + A);
|
||||
continue;
|
||||
case R_X86_64_16:
|
||||
write16(S + A);
|
||||
continue;
|
||||
case R_X86_64_32:
|
||||
write32(S + A);
|
||||
continue;
|
||||
case R_X86_64_32S:
|
||||
write32s(S + A);
|
||||
continue;
|
||||
case R_X86_64_64:
|
||||
write64(S + A);
|
||||
continue;
|
||||
case R_X86_64_PC8:
|
||||
write8s(S + A - P);
|
||||
continue;
|
||||
case R_X86_64_PC16:
|
||||
write16s(S + A - P);
|
||||
continue;
|
||||
case R_X86_64_PC32:
|
||||
write32s(S + A - P);
|
||||
continue;
|
||||
case R_X86_64_PC64:
|
||||
write64(S + A - P);
|
||||
continue;
|
||||
case R_X86_64_PLT32:
|
||||
write32s(S + A - P);
|
||||
continue;
|
||||
case R_X86_64_PLTOFF64:
|
||||
write64(S + A - GOT);
|
||||
break;
|
||||
case R_X86_64_GOT32:
|
||||
write32s(G + A);
|
||||
continue;
|
||||
case R_X86_64_GOT64:
|
||||
write64(G + A);
|
||||
continue;
|
||||
case R_X86_64_GOTOFF64:
|
||||
write64(S + A - GOT);
|
||||
continue;
|
||||
case R_X86_64_GOTPC32:
|
||||
write32s(GOT + A - P);
|
||||
continue;
|
||||
case R_X86_64_GOTPC64:
|
||||
write64(GOT + A - P);
|
||||
continue;
|
||||
case R_X86_64_GOTPCREL:
|
||||
write32s(G + GOT + A - P);
|
||||
continue;
|
||||
case R_X86_64_GOTPCREL64:
|
||||
write64(G + GOT + A - P);
|
||||
continue;
|
||||
case R_X86_64_GOTPCRELX:
|
||||
if (sym.get_got_idx(ctx) == -1) {
|
||||
u32 insn = relax_gotpcrelx(loc - 2);
|
||||
loc[-2] = insn >> 8;
|
||||
loc[-1] = insn;
|
||||
write32s(S + A - P);
|
||||
} else {
|
||||
write32s(G + GOT + A - P);
|
||||
}
|
||||
continue;
|
||||
case R_X86_64_REX_GOTPCRELX:
|
||||
if (sym.get_got_idx(ctx) == -1) {
|
||||
u32 insn = relax_rex_gotpcrelx(loc - 3);
|
||||
loc[-3] = insn >> 16;
|
||||
loc[-2] = insn >> 8;
|
||||
loc[-1] = insn;
|
||||
write32s(S + A - P);
|
||||
} else {
|
||||
write32s(G + GOT + A - P);
|
||||
}
|
||||
continue;
|
||||
case R_X86_64_TLSGD:
|
||||
if (sym.get_tlsgd_idx(ctx) == -1) {
|
||||
// Relax GD to LE
|
||||
static const u8 insn[] = {
|
||||
0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // mov %fs:0, %rax
|
||||
0x48, 0x8d, 0x80, 0, 0, 0, 0, // lea 0(%rax), %rax
|
||||
};
|
||||
memcpy(loc - 4, insn, sizeof(insn));
|
||||
|
||||
i64 val = S - ctx.tls_end + A + 4;
|
||||
overflow_check(val, -((i64)1 << 31), (i64)1 << 31);
|
||||
*(u32 *)(loc + 8) = val;
|
||||
i++;
|
||||
} else {
|
||||
write32s(sym.get_tlsgd_addr(ctx) + A - P);
|
||||
}
|
||||
continue;
|
||||
case R_X86_64_TLSLD:
|
||||
if (ctx.got->tlsld_idx == -1) {
|
||||
// Relax LD to LE
|
||||
static const u8 insn[] = {
|
||||
0x66, 0x66, 0x66, // (padding)
|
||||
0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // mov %fs:0, %rax
|
||||
};
|
||||
memcpy(loc - 3, insn, sizeof(insn));
|
||||
i++;
|
||||
} else {
|
||||
write32s(ctx.got->get_tlsld_addr(ctx) + A - P);
|
||||
}
|
||||
continue;
|
||||
case R_X86_64_DTPOFF32:
|
||||
if (ctx.arg.relax && !ctx.arg.shared)
|
||||
write32s(S + A - ctx.tls_end);
|
||||
else
|
||||
write32s(S + A - ctx.tls_begin);
|
||||
continue;
|
||||
case R_X86_64_DTPOFF64:
|
||||
if (ctx.arg.relax && !ctx.arg.shared)
|
||||
write64(S + A - ctx.tls_end);
|
||||
else
|
||||
write64(S + A - ctx.tls_begin);
|
||||
continue;
|
||||
case R_X86_64_TPOFF32:
|
||||
write32s(S + A - ctx.tls_end);
|
||||
continue;
|
||||
case R_X86_64_TPOFF64:
|
||||
write64(S + A - ctx.tls_end);
|
||||
continue;
|
||||
case R_X86_64_GOTTPOFF:
|
||||
if (sym.get_gottp_idx(ctx) == -1) {
|
||||
u32 insn = relax_gottpoff(loc - 3);
|
||||
loc[-3] = insn >> 16;
|
||||
loc[-2] = insn >> 8;
|
||||
loc[-1] = insn;
|
||||
write32s(S + A - ctx.tls_end + 4);
|
||||
} else {
|
||||
write32s(sym.get_gottp_addr(ctx) + A - P);
|
||||
}
|
||||
continue;
|
||||
case R_X86_64_GOTPC32_TLSDESC:
|
||||
if (sym.get_tlsdesc_idx(ctx) == -1) {
|
||||
static const u8 insn[] = {
|
||||
0x48, 0xc7, 0xc0, 0, 0, 0, 0, // mov $0, %rax
|
||||
};
|
||||
memcpy(loc - 3, insn, sizeof(insn));
|
||||
write32s(S + A - ctx.tls_end + 4);
|
||||
} else {
|
||||
write32s(sym.get_tlsdesc_addr(ctx) + A - P);
|
||||
}
|
||||
continue;
|
||||
case R_X86_64_SIZE32:
|
||||
write32(sym.esym().st_size + A);
|
||||
continue;
|
||||
case R_X86_64_SIZE64:
|
||||
write64(sym.esym().st_size + A);
|
||||
continue;
|
||||
case R_X86_64_TLSDESC_CALL:
|
||||
if (sym.get_tlsdesc_idx(ctx) == -1) {
|
||||
// call *(%rax) -> nop
|
||||
loc[0] = 0x66;
|
||||
loc[1] = 0x90;
|
||||
}
|
||||
continue;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
|
||||
#undef S
|
||||
#undef A
|
||||
#undef P
|
||||
#undef G
|
||||
#undef GOT
|
||||
}
|
||||
}
|
||||
|
||||
// This function is responsible for applying relocations against
|
||||
// non-SHF_ALLOC sections (i.e. sections that are not mapped to memory
|
||||
// at runtime).
|
||||
//
|
||||
// Relocations against non-SHF_ALLOC sections are much easier to
|
||||
// handle than that against SHF_ALLOC sections. It is because, since
|
||||
// they are not mapped to memory, they don't contain any variable or
|
||||
// function and never need PLT or GOT. Non-SHF_ALLOC sections are
|
||||
// mostly debug info sections.
|
||||
//
|
||||
// Relocations against non-SHF_ALLOC sections are not scanned by
|
||||
// scan_relocations.
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
|
||||
std::span<ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_X86_64_NONE)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
if (!sym.file) {
|
||||
report_undef(ctx, sym);
|
||||
continue;
|
||||
}
|
||||
|
||||
SectionFragment<E> *frag;
|
||||
i64 addend;
|
||||
std::tie(frag, addend) = get_fragment(ctx, rel);
|
||||
|
||||
auto overflow_check = [&](i64 val, i64 lo, i64 hi) {
|
||||
if (val < lo || hi <= val)
|
||||
Error(ctx) << *this << ": relocation " << rel << " against "
|
||||
<< sym << " out of range: " << val << " is not in ["
|
||||
<< lo << ", " << hi << ")";
|
||||
};
|
||||
|
||||
auto write8 = [&](u64 val) {
|
||||
overflow_check(val, 0, 1 << 8);
|
||||
*loc = val;
|
||||
};
|
||||
|
||||
auto write16 = [&](u64 val) {
|
||||
overflow_check(val, 0, 1 << 16);
|
||||
*(u16 *)loc = val;
|
||||
};
|
||||
|
||||
auto write32 = [&](u64 val) {
|
||||
overflow_check(val, 0, (i64)1 << 32);
|
||||
*(u32 *)loc = val;
|
||||
};
|
||||
|
||||
auto write32s = [&](u64 val) {
|
||||
overflow_check(val, -((i64)1 << 31), (i64)1 << 31);
|
||||
*(u32 *)loc = val;
|
||||
};
|
||||
|
||||
#define S (frag ? frag->get_addr(ctx) : sym.get_addr(ctx))
|
||||
#define A (frag ? addend : rel.r_addend)
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_X86_64_8:
|
||||
write8(S + A);
|
||||
break;
|
||||
case R_X86_64_16:
|
||||
write16(S + A);
|
||||
break;
|
||||
case R_X86_64_32:
|
||||
write32(S + A);
|
||||
break;
|
||||
case R_X86_64_32S:
|
||||
write32s(S + A);
|
||||
break;
|
||||
case R_X86_64_64:
|
||||
*(u64 *)loc = S + A;
|
||||
break;
|
||||
case R_X86_64_DTPOFF32:
|
||||
write32s(S + A - ctx.tls_begin);
|
||||
break;
|
||||
case R_X86_64_DTPOFF64:
|
||||
*(u64 *)loc = S + A - ctx.tls_begin;
|
||||
break;
|
||||
case R_X86_64_SIZE32:
|
||||
write32(sym.esym().st_size + A);
|
||||
break;
|
||||
case R_X86_64_SIZE64:
|
||||
*(u64 *)loc = sym.esym().st_size + A;
|
||||
break;
|
||||
default:
|
||||
Fatal(ctx) << *this << ": invalid relocation for non-allocated sections: "
|
||||
<< rel;
|
||||
break;
|
||||
}
|
||||
|
||||
#undef S
|
||||
#undef A
|
||||
}
|
||||
}
|
||||
|
||||
// Linker has to create data structures in an output file to apply
|
||||
// some type of relocations. For example, if a relocation refers a GOT
|
||||
// or a PLT entry of a symbol, linker has to create an entry in .got
|
||||
// or in .plt for that symbol. In order to fix the file layout, we
|
||||
// need to scan relocations.
|
||||
template <>
|
||||
void InputSection<E>::scan_relocations(Context<E> &ctx) {
|
||||
assert(shdr.sh_flags & SHF_ALLOC);
|
||||
|
||||
this->reldyn_offset = file.num_dynrel * sizeof(ElfRel<E>);
|
||||
std::span<ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
// Scan relocations
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_X86_64_NONE)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = (u8 *)(contents.data() + rel.r_offset);
|
||||
|
||||
if (!sym.file) {
|
||||
report_undef(ctx, sym);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sym.get_type() == STT_GNU_IFUNC) {
|
||||
sym.flags |= NEEDS_GOT;
|
||||
sym.flags |= NEEDS_PLT;
|
||||
}
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_X86_64_8:
|
||||
case R_X86_64_16:
|
||||
case R_X86_64_32:
|
||||
case R_X86_64_32S: {
|
||||
// Dynamic linker does not support 8, 16 or 32-bit dynamic
|
||||
// relocations for these types of relocations. We report an
|
||||
// error if we cannot relocate them even at load-time.
|
||||
Action table[][4] = {
|
||||
// Absolute Local Imported data Imported code
|
||||
{ NONE, ERROR, ERROR, ERROR }, // DSO
|
||||
{ NONE, ERROR, ERROR, ERROR }, // PIE
|
||||
{ NONE, NONE, COPYREL, PLT }, // PDE
|
||||
};
|
||||
dispatch(ctx, table, i, rel, sym);
|
||||
break;
|
||||
}
|
||||
case R_X86_64_64: {
|
||||
// Unlike the above, we can use R_X86_64_RELATIVE and R_86_64_64
|
||||
// relocations.
|
||||
Action table[][4] = {
|
||||
// Absolute Local Imported data Imported code
|
||||
{ NONE, BASEREL, DYNREL, DYNREL }, // DSO
|
||||
{ NONE, BASEREL, DYNREL, DYNREL }, // PIE
|
||||
{ NONE, NONE, COPYREL, PLT }, // PDE
|
||||
};
|
||||
dispatch(ctx, table, i, rel, sym);
|
||||
break;
|
||||
}
|
||||
case R_X86_64_PC8:
|
||||
case R_X86_64_PC16:
|
||||
case R_X86_64_PC32: {
|
||||
Action table[][4] = {
|
||||
// Absolute Local Imported data Imported code
|
||||
{ ERROR, NONE, ERROR, ERROR }, // DSO
|
||||
{ ERROR, NONE, COPYREL, PLT }, // PIE
|
||||
{ NONE, NONE, COPYREL, PLT }, // PDE
|
||||
};
|
||||
dispatch(ctx, table, i, rel, sym);
|
||||
break;
|
||||
}
|
||||
case R_X86_64_PC64: {
|
||||
Action table[][4] = {
|
||||
// Absolute Local Imported data Imported code
|
||||
{ BASEREL, NONE, DYNREL, DYNREL }, // DSO
|
||||
{ BASEREL, NONE, COPYREL, PLT }, // PIE
|
||||
{ NONE, NONE, COPYREL, PLT }, // PDE
|
||||
};
|
||||
dispatch(ctx, table, i, rel, sym);
|
||||
break;
|
||||
}
|
||||
case R_X86_64_GOT32:
|
||||
case R_X86_64_GOT64:
|
||||
case R_X86_64_GOTPC32:
|
||||
case R_X86_64_GOTPC64:
|
||||
case R_X86_64_GOTPCREL:
|
||||
case R_X86_64_GOTPCREL64:
|
||||
sym.flags |= NEEDS_GOT;
|
||||
break;
|
||||
case R_X86_64_GOTPCRELX: {
|
||||
if (rel.r_addend != -4)
|
||||
Fatal(ctx) << *this << ": bad r_addend for R_X86_64_GOTPCRELX";
|
||||
|
||||
bool do_relax = ctx.arg.relax && !sym.is_imported &&
|
||||
sym.is_relative() && relax_gotpcrelx(loc - 2);
|
||||
if (!do_relax)
|
||||
sym.flags |= NEEDS_GOT;
|
||||
break;
|
||||
}
|
||||
case R_X86_64_REX_GOTPCRELX: {
|
||||
if (rel.r_addend != -4)
|
||||
Fatal(ctx) << *this << ": bad r_addend for R_X86_64_REX_GOTPCRELX";
|
||||
|
||||
bool do_relax = ctx.arg.relax && !sym.is_imported &&
|
||||
sym.is_relative() && relax_rex_gotpcrelx(loc - 3);
|
||||
if (!do_relax)
|
||||
sym.flags |= NEEDS_GOT;
|
||||
break;
|
||||
}
|
||||
case R_X86_64_PLT32:
|
||||
case R_X86_64_PLTOFF64:
|
||||
if (sym.is_imported)
|
||||
sym.flags |= NEEDS_PLT;
|
||||
break;
|
||||
case R_X86_64_TLSGD:
|
||||
if (i + 1 == rels.size())
|
||||
Fatal(ctx) << *this
|
||||
<< ": TLSGD reloc must be followed by PLT32 or GOTPCREL";
|
||||
|
||||
if (ctx.arg.relax && !ctx.arg.shared && !sym.is_imported)
|
||||
i++;
|
||||
else
|
||||
sym.flags |= NEEDS_TLSGD;
|
||||
break;
|
||||
case R_X86_64_TLSLD:
|
||||
if (i + 1 == rels.size())
|
||||
Fatal(ctx) << *this
|
||||
<< ": TLSLD reloc must be followed by PLT32 or GOTPCREL";
|
||||
|
||||
if (ctx.arg.relax && !ctx.arg.shared)
|
||||
i++;
|
||||
else
|
||||
sym.flags |= NEEDS_TLSLD;
|
||||
break;
|
||||
case R_X86_64_GOTTPOFF: {
|
||||
ctx.has_gottp_rel = true;
|
||||
|
||||
bool do_relax = ctx.arg.relax && !ctx.arg.shared &&
|
||||
!sym.is_imported && relax_gottpoff(loc - 3);
|
||||
if (!do_relax)
|
||||
sym.flags |= NEEDS_GOTTP;
|
||||
break;
|
||||
}
|
||||
case R_X86_64_GOTPC32_TLSDESC: {
|
||||
if (memcmp(loc - 3, "\x48\x8d\x05", 3))
|
||||
Fatal(ctx) << *this << ": GOTPC32_TLSDESC relocation is used"
|
||||
<< " against an invalid code sequence";
|
||||
|
||||
bool do_relax = ctx.relax_tlsdesc && !sym.is_imported;
|
||||
if (!do_relax)
|
||||
sym.flags |= NEEDS_TLSDESC;
|
||||
break;
|
||||
}
|
||||
case R_X86_64_GOTOFF64:
|
||||
case R_X86_64_DTPOFF32:
|
||||
case R_X86_64_DTPOFF64:
|
||||
case R_X86_64_TPOFF32:
|
||||
case R_X86_64_TPOFF64:
|
||||
case R_X86_64_SIZE32:
|
||||
case R_X86_64_SIZE64:
|
||||
case R_X86_64_TLSDESC_CALL:
|
||||
break;
|
||||
default:
|
||||
Error(ctx) << *this << ": unknown relocation: " << rel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mold::elf
|
||||
+973
@@ -0,0 +1,973 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace mold::elf {
|
||||
|
||||
static const char helpmsg[] = R"(
|
||||
Options:
|
||||
--help Report usage information
|
||||
-v, --version Report version information
|
||||
-V Report version and target information
|
||||
-(, --start-group Ignored
|
||||
-), --end-group Ignored
|
||||
-C DIR, --directory DIR Change to DIR before doing anything
|
||||
-E, --export-dynamic Put symbols in the dynamic symbol table
|
||||
--no-export-dynamic
|
||||
-F LIBNAME, --filter LIBNAME
|
||||
Set DT_FILTER to the specified value
|
||||
-I FILE, --dynamic-linker FILE
|
||||
Set dynamic linker path
|
||||
--no-dynamic-linker
|
||||
-L DIR, --library-path DIR Add DIR to library search path
|
||||
-M, --print-map Write map file to stdout
|
||||
-N, --omagic Do not page align data, do not make text readonly
|
||||
--no-omagic
|
||||
-O NUMBER Ignored
|
||||
-S, --strip-debug Strip .debug_* sections
|
||||
-T FILE, --script FILE Read linker script
|
||||
-X, --discard-locals Discard temporary local symbols
|
||||
-e SYMBOL, --entry SYMBOL Set program entry point
|
||||
-f SHLIB, --auxiliary SHLIB Set DT_AUXILIARY to the specified value
|
||||
-h LIBNAME, --soname LIBNAME
|
||||
Set shared library name
|
||||
-l LIBNAME Search for a given library
|
||||
-m TARGET Set target
|
||||
-o FILE, --output FILE Set output filename
|
||||
-r, --relocatable Generate relocatable output
|
||||
-s, --strip-all Strip .symtab section
|
||||
-u SYMBOL, --undefined SYMBOL
|
||||
Force to resolve SYMBOL
|
||||
--Bdynamic Link against shared libraries (default)
|
||||
--Bstatic Do not link against shared libraries
|
||||
--Bsymbolic Bind global symbols locally
|
||||
--Bsymbolic-functions Bind global functions locally
|
||||
--Bno-symbolic Cancel --Bsymbolic and --Bsymbolic-functions
|
||||
--Map FILE Write map file to a given file
|
||||
--allow-multiple-definition Allow multiple definitions
|
||||
--as-needed Only set DT_NEEDED if used
|
||||
--no-as-needed
|
||||
--build-id [none,md5,sha1,sha256,uuid,HEXSTRING]
|
||||
Generate build ID
|
||||
--no-build-id
|
||||
--chroot DIR Set a given path to root directory
|
||||
--color-diagnostics=[auto,always,never]
|
||||
Use colors in diagnostics
|
||||
--color-diagnostics Alias for --color-diagnostics=always
|
||||
--compress-debug-sections [none,zlib,zlib-gabi,zlib-gnu]
|
||||
Compress .debug_* sections
|
||||
--dc Ignored
|
||||
--defsym=SYMBOL=VALUE Define a symbol alias
|
||||
--demangle Demangle C++ symbols in log messages (default)
|
||||
--no-demangle
|
||||
--disable-new-dtags Ignored
|
||||
--dp Ignored
|
||||
--dynamic-list Read a list of dynamic symbols
|
||||
--eh-frame-hdr Create .eh_frame_hdr section
|
||||
--no-eh-frame-hdr
|
||||
--enable-new-dtags Ignored
|
||||
--exclude-libs LIB,LIB,.. Mark all symbols in given libraries hidden
|
||||
--fatal-warnings Ignored
|
||||
--no-fatal-warnings Ignored
|
||||
--fini SYMBOL Call SYMBOL at unload-time
|
||||
--fork Spawn a child process (default)
|
||||
--no-fork
|
||||
--gc-sections Remove unreferenced sections
|
||||
--no-gc-sections
|
||||
--gdb-index Ignored
|
||||
--hash-style [sysv,gnu,both]
|
||||
Set hash style
|
||||
--icf Fold identical code
|
||||
--no-icf
|
||||
--image-base ADDR Set the base address to a given value
|
||||
--init SYMBOL Call SYMBOl at load-time
|
||||
--no-undefined Report undefined symbols (even with --shared)
|
||||
--pack-dyn-relocs=[relr,none]
|
||||
Pack dynamic relocations
|
||||
--perf Print performance statistics
|
||||
--pie, --pic-executable Create a position independent executable
|
||||
--no-pie, --no-pic-executable
|
||||
--plugin Ignored
|
||||
--plugin-opt Ignored
|
||||
--pop-state Pop state of flags governing input file handling
|
||||
--preload
|
||||
--no-preload
|
||||
--print-gc-sections Print removed unreferenced sections
|
||||
--no-print-gc-sections
|
||||
--print-icf-sections Print folded identical sections
|
||||
--no-print-icf-sections
|
||||
--push-state Pop state of flags governing input file handling
|
||||
--quick-exit Use quick_exit to exit (default)
|
||||
--no-quick-exit
|
||||
--relax Optimize instructions (default)
|
||||
--no-relax
|
||||
--repro Embed input files to .repro section
|
||||
--require-defined SYMBOL Require SYMBOL be defined in the final output
|
||||
--retain-symbols-file FILE Keep only symbols listed in FILE
|
||||
--rpath DIR Add DIR to runtime search path
|
||||
--rpath-link DIR Ignored
|
||||
--run COMMAND ARG... Run COMMAND with mold as /usr/bin/ld
|
||||
--shared, --Bshareable Create a share library
|
||||
--sort-common Ignored
|
||||
--sort-section Ignored
|
||||
--spare-dynamic-tags NUMBER Reserve give number of tags in .dynamic section
|
||||
--start-lib Give following object files in-archive-file semantics
|
||||
--end-lib End the effect of --start-lib
|
||||
--static Do not link against shared libraries
|
||||
--stats Print input statistics
|
||||
--sysroot DIR Set target system root directory
|
||||
--thread-count COUNT, --threads=COUNT
|
||||
Use COUNT number of threads
|
||||
--threads Use multiple threads (default)
|
||||
--no-threads
|
||||
--trace Print name of each input file
|
||||
--unique PATTERN Don't merge input sections that match a given pattern
|
||||
--unresolved-symbols [report-all,ignore-all,ignore-in-object-files,ignore-in-shared-libs]
|
||||
How to handle unresolved symbols
|
||||
--version-script FILE Read version script
|
||||
--warn-common Warn about common symbols
|
||||
--no-warn-common
|
||||
--warn-unresolved-symbols Report unresolved symbols as warnings
|
||||
--error-unresolved-symbols
|
||||
Report unresolved symbols as errors (default)
|
||||
--whole-archive Include all objects from static archives
|
||||
--no-whole-archive
|
||||
--wrap SYMBOL Use wrapper function for a given symbol
|
||||
-z defs Report undefined symbols (even with --shared)
|
||||
-z nodefs
|
||||
-z common-page-size=VALUE Ignored
|
||||
-z execstack Require executable stack
|
||||
-z noexecstack
|
||||
-z initfirst Mark DSO to be initialized first at runtime
|
||||
-z interpose Mark object to interpose all DSOs but executable
|
||||
-z keep-text-section-prefix Keep .text.{hot,unknown,unlikely,startup,exit} as separate sections in the final binary
|
||||
-z nokeep-text-section-prefix
|
||||
-z lazy Enable lazy function resolution (default)
|
||||
-z max-page-size=VALUE Use VALUE as the memory page size
|
||||
-z nocopyreloc Do not create copy relocations
|
||||
-z nodefaultlib Make the dynamic loader to ignore default search paths
|
||||
-z nodelete Mark DSO non-deletable at runtime
|
||||
-z nodlopen Mark DSO not available to dlopen
|
||||
-z nodump Mark DSO not available to dldump
|
||||
-z now Disable lazy function resolution
|
||||
-z origin Mark object requiring immediate $ORIGIN processing at runtime
|
||||
-z separate-loadable-segments
|
||||
Separate all loadable segments to different pages
|
||||
-z separate-code Separate code and data into different pages
|
||||
-z noseparate-code Allow overlap in pages
|
||||
-z relro Make some sections read-only after relocation (default)
|
||||
-z norelro
|
||||
-z text Report error if DT_TEXTREL is set
|
||||
-z notext
|
||||
-z textoff
|
||||
|
||||
mold: supported targets: elf32-i386 elf64-x86-64 elf64-littleaarch64
|
||||
mold: supported emulations: elf_i386 elf_x86_64 aarch64linux aarch64elf)";
|
||||
|
||||
static std::vector<std::string> add_dashes(std::string name) {
|
||||
// Multi-letter linker options can be preceded by either a single
|
||||
// dash or double dashes except ones starting with "o", which must
|
||||
// be preceded by double dashes. For example, "-omagic" is
|
||||
// interpreted as "-o magic". If you really want to specify the
|
||||
// "omagic" option, you have to pass "--omagic".
|
||||
if (name[0] == 'o')
|
||||
return {"--" + name};
|
||||
return {"-" + name, "--" + name};
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
bool read_arg(Context<E> &ctx, std::span<std::string_view> &args,
|
||||
std::string_view &arg, std::string name) {
|
||||
if (name.size() == 1) {
|
||||
if (args[0] == "-" + name) {
|
||||
if (args.size() == 1)
|
||||
Fatal(ctx) << "option -" << name << ": argument missing";
|
||||
arg = args[1];
|
||||
args = args.subspan(2);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].starts_with("-" + name)) {
|
||||
arg = args[0].substr(name.size() + 1);
|
||||
args = args.subspan(1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
for (std::string opt : add_dashes(name)) {
|
||||
if (args[0] == opt) {
|
||||
if (args.size() == 1)
|
||||
Fatal(ctx) << "option -" << name << ": argument missing";
|
||||
arg = args[1];
|
||||
args = args.subspan(2);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].starts_with(opt + "=")) {
|
||||
arg = args[0].substr(opt.size() + 1);
|
||||
args = args.subspan(1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool read_flag(std::span<std::string_view> &args, std::string name) {
|
||||
for (std::string opt : add_dashes(name)) {
|
||||
if (args[0] == opt) {
|
||||
args = args.subspan(1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool read_z_flag(std::span<std::string_view> &args, std::string name) {
|
||||
if (args.size() >= 2 && args[0] == "-z" && args[1] == name) {
|
||||
args = args.subspan(2);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!args.empty() && args[0] == "-z" + name) {
|
||||
args = args.subspan(1);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
bool read_z_arg(Context<E> &ctx, std::span<std::string_view> &args,
|
||||
std::string_view &arg, std::string name) {
|
||||
if (args.size() >= 2 && args[0] == "-z" && args[1].starts_with(name + "=")) {
|
||||
arg = args[1].substr(name.size() + 1);
|
||||
args = args.subspan(2);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!args.empty() && args[0].starts_with("-z" + name + "=")) {
|
||||
arg = args[0].substr(name.size() + 3);
|
||||
args = args.subspan(1);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
std::string create_response_file(Context<E> &ctx) {
|
||||
std::string buf;
|
||||
std::stringstream out;
|
||||
|
||||
std::string cwd = std::filesystem::current_path();
|
||||
out << "-C " << cwd.substr(1) << "\n";
|
||||
|
||||
if (cwd != "/") {
|
||||
out << "--chroot ..";
|
||||
i64 depth = std::count(cwd.begin(), cwd.end(), '/');
|
||||
for (i64 i = 1; i < depth; i++)
|
||||
out << "/..";
|
||||
out << "\n";
|
||||
}
|
||||
|
||||
for (i64 i = 1; i < ctx.cmdline_args.size(); i++) {
|
||||
std::string_view arg = ctx.cmdline_args[i];
|
||||
|
||||
if (arg == "-repro" || arg == "--repro") {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
out << arg << "\n";
|
||||
}
|
||||
|
||||
return out.str();
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static i64 parse_hex(Context<E> &ctx, std::string opt, std::string_view value) {
|
||||
if (!value.starts_with("0x") && !value.starts_with("0X"))
|
||||
Fatal(ctx) << "option -" << opt << ": not a hexadecimal number";
|
||||
value = value.substr(2);
|
||||
if (value.find_first_not_of("0123456789abcdefABCDEF") != value.npos)
|
||||
Fatal(ctx) << "option -" << opt << ": not a hexadecimal number";
|
||||
return std::stol(std::string(value), nullptr, 16);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static i64 parse_number(Context<E> &ctx, std::string opt,
|
||||
std::string_view value) {
|
||||
size_t nread;
|
||||
i64 ret = std::stol(std::string(value), &nread, 0);
|
||||
if (value.size() != nread)
|
||||
Fatal(ctx) << "option -" << opt << ": not a number: " << value;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static std::vector<u8> parse_hex_build_id(Context<E> &ctx,
|
||||
std::string_view arg) {
|
||||
assert(arg.starts_with("0x") || arg.starts_with("0X"));
|
||||
|
||||
if (arg.size() % 2)
|
||||
Fatal(ctx) << "invalid build-id: " << arg;
|
||||
if (arg.substr(2).find_first_not_of("0123456789abcdefABCDEF") != arg.npos)
|
||||
Fatal(ctx) << "invalid build-id: " << arg;
|
||||
|
||||
arg = arg.substr(2);
|
||||
|
||||
auto fn = [](char c) {
|
||||
if ('0' <= c && c <= '9')
|
||||
return c - '0';
|
||||
if ('a' <= c && c <= 'f')
|
||||
return c - 'a' + 10;
|
||||
assert('A' <= c && c <= 'F');
|
||||
return c - 'A' + 10;
|
||||
};
|
||||
|
||||
std::vector<u8> vec(arg.size() / 2);
|
||||
for (i64 i = 0; i < vec.size(); i++)
|
||||
vec[i] = (fn(arg[i * 2]) << 4) | fn(arg[i * 2 + 1]);
|
||||
return vec;
|
||||
}
|
||||
|
||||
static std::vector<std::string_view>
|
||||
split_by_comma_or_colon(std::string_view str) {
|
||||
std::vector<std::string_view> vec;
|
||||
|
||||
for (;;) {
|
||||
i64 pos = str.find_first_of(",:");
|
||||
if (pos == str.npos) {
|
||||
vec.push_back(str);
|
||||
break;
|
||||
}
|
||||
vec.push_back(str.substr(0, pos));
|
||||
str = str.substr(pos);
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
static std::string_view trim(std::string_view str) {
|
||||
size_t pos = str.find_first_not_of(" \t");
|
||||
if (pos == str.npos)
|
||||
return "";
|
||||
str = str.substr(pos);
|
||||
|
||||
pos = str.find_last_not_of(" \t");
|
||||
if (pos == str.npos)
|
||||
return str;
|
||||
return str.substr(0, pos + 1);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void read_retain_symbols_file(Context<E> &ctx, std::string_view path) {
|
||||
MappedFile<Context<E>> *mf =
|
||||
MappedFile<Context<E>>::must_open(ctx, std::string(path));
|
||||
std::string_view data((char *)mf->data, mf->size);
|
||||
|
||||
ctx.arg.retain_symbols_file.reset(new std::unordered_set<std::string_view>);
|
||||
|
||||
while (!data.empty()) {
|
||||
size_t pos = data.find('\n');
|
||||
std::string_view name;
|
||||
|
||||
if (pos == data.npos) {
|
||||
name = data;
|
||||
data = "";
|
||||
} else {
|
||||
name = data.substr(0, pos);
|
||||
data = data.substr(pos + 1);
|
||||
}
|
||||
|
||||
name = trim(name);
|
||||
if (!name.empty())
|
||||
ctx.arg.retain_symbols_file->insert(name);
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_file(std::string_view path) {
|
||||
struct stat st;
|
||||
return stat(std::string(path).c_str(), &st) == 0 &&
|
||||
(st.st_mode & S_IFMT) != S_IFDIR;
|
||||
}
|
||||
|
||||
// Returns a PLT header size and a PLT entry size.
|
||||
template <typename E>
|
||||
static std::pair<i64, i64> get_plt_size(Context<E> &ctx) {
|
||||
switch (E::e_machine) {
|
||||
case EM_X86_64:
|
||||
if (ctx.arg.z_now)
|
||||
return {0, 8};
|
||||
if (ctx.arg.z_ibtplt)
|
||||
return {16, 24};
|
||||
return {16, 16};
|
||||
case EM_386:
|
||||
return {16, 16};
|
||||
case EM_AARCH64:
|
||||
return {32, 16};
|
||||
}
|
||||
unreachable();
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void parse_nonpositional_args(Context<E> &ctx,
|
||||
std::vector<std::string_view> &remaining) {
|
||||
std::span<std::string_view> args = ctx.cmdline_args;
|
||||
args = args.subspan(1);
|
||||
|
||||
ctx.arg.color_diagnostics = isatty(STDERR_FILENO);
|
||||
ctx.page_size = E::page_size;
|
||||
|
||||
bool version_shown = false;
|
||||
|
||||
while (!args.empty()) {
|
||||
std::string_view arg;
|
||||
|
||||
if (read_flag(args, "help")) {
|
||||
SyncOut(ctx) << "Usage: " << ctx.cmdline_args[0]
|
||||
<< " [options] file...\n" << helpmsg;
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (read_arg(ctx, args, arg, "o") || read_arg(ctx, args, arg, "output")) {
|
||||
ctx.arg.output = arg;
|
||||
} else if (read_arg(ctx, args, arg, "dynamic-linker") ||
|
||||
read_arg(ctx, args, arg, "I")) {
|
||||
ctx.arg.dynamic_linker = arg;
|
||||
} else if (read_flag(args, "no-dynamic-linker")) {
|
||||
ctx.arg.dynamic_linker = "";
|
||||
} else if (read_flag(args, "v")) {
|
||||
SyncOut(ctx) << mold_version;
|
||||
version_shown = true;
|
||||
} else if (read_flag(args, "version")) {
|
||||
SyncOut(ctx) << mold_version;
|
||||
exit(0);
|
||||
} else if (read_flag(args, "V")) {
|
||||
SyncOut(ctx) << mold_version
|
||||
<< "\n Supported emulations:\n elf_x86_64\n elf_i386";
|
||||
version_shown = true;
|
||||
} else if (read_arg(ctx, args, arg, "m")) {
|
||||
if (arg == "elf_x86_64") {
|
||||
ctx.arg.emulation = EM_X86_64;
|
||||
} else if (arg == "elf_i386") {
|
||||
ctx.arg.emulation = EM_386;
|
||||
} else if (arg == "aarch64linux") {
|
||||
ctx.arg.emulation = EM_AARCH64;
|
||||
} else {
|
||||
Fatal(ctx) << "unknown -m argument: " << arg;
|
||||
}
|
||||
} else if (read_flag(args, "end-lib")) {
|
||||
remaining.push_back("-end-lib");
|
||||
} else if (read_flag(args, "export-dynamic") || read_flag(args, "E")) {
|
||||
ctx.arg.export_dynamic = true;
|
||||
} else if (read_flag(args, "no-export-dynamic")) {
|
||||
ctx.arg.export_dynamic = false;
|
||||
} else if (read_flag(args, "Bsymbolic")) {
|
||||
ctx.arg.Bsymbolic = true;
|
||||
} else if (read_flag(args, "Bsymbolic-functions")) {
|
||||
ctx.arg.Bsymbolic_functions = true;
|
||||
} else if (read_flag(args, "Bno-symbolic")) {
|
||||
ctx.arg.Bsymbolic = false;
|
||||
ctx.arg.Bsymbolic_functions = false;
|
||||
} else if (read_arg(ctx, args, arg, "exclude-libs")) {
|
||||
append(ctx.arg.exclude_libs, split_by_comma_or_colon(arg));
|
||||
} else if (read_arg(ctx, args, arg, "e") ||
|
||||
read_arg(ctx, args, arg, "entry")) {
|
||||
ctx.arg.entry = arg;
|
||||
} else if (read_arg(ctx, args, arg, "Map")) {
|
||||
ctx.arg.Map = arg;
|
||||
ctx.arg.print_map = true;
|
||||
} else if (read_flag(args, "print-map") || read_flag(args, "M")) {
|
||||
ctx.arg.print_map = true;
|
||||
} else if (read_flag(args, "static") || read_flag(args, "Bstatic")) {
|
||||
ctx.arg.is_static = true;
|
||||
remaining.push_back("-Bstatic");
|
||||
} else if (read_flag(args, "Bdynamic")) {
|
||||
ctx.arg.is_static = false;
|
||||
remaining.push_back("-Bdynamic");
|
||||
} else if (read_flag(args, "shared") || read_flag(args, "Bshareable")) {
|
||||
ctx.arg.shared = true;
|
||||
} else if (read_arg(ctx, args, arg, "spare-dynamic-tags")) {
|
||||
ctx.arg.spare_dynamic_tags = parse_number(ctx, "spare-dynamic-tags", arg);
|
||||
} else if (read_flag(args, "start-lib")) {
|
||||
remaining.push_back("-start-lib");
|
||||
} else if (read_arg(ctx, args, arg, "defsym")) {
|
||||
size_t pos = arg.find('=');
|
||||
if (pos == arg.npos || pos == arg.size() - 1)
|
||||
Fatal(ctx) << "-defsym: syntax error: " << arg;
|
||||
ctx.arg.defsyms.push_back({arg.substr(0, pos), arg.substr(pos + 1)});
|
||||
} else if (read_flag(args, "demangle")) {
|
||||
ctx.arg.demangle = true;
|
||||
} else if (read_flag(args, "no-demangle")) {
|
||||
ctx.arg.demangle = false;
|
||||
} else if (read_flag(args, "default-symver")) {
|
||||
ctx.arg.default_symver = true;
|
||||
} else if (read_arg(ctx, args, arg, "y") ||
|
||||
read_arg(ctx, args, arg, "trace-symbol")) {
|
||||
ctx.arg.trace_symbol.push_back(arg);
|
||||
} else if (read_arg(ctx, args, arg, "filler")) {
|
||||
ctx.arg.filler = parse_hex(ctx, "filler", arg);
|
||||
} else if (read_arg(ctx, args, arg, "L") ||
|
||||
read_arg(ctx, args, arg, "library-path")) {
|
||||
ctx.arg.library_paths.push_back(std::string(arg));
|
||||
} else if (read_arg(ctx, args, arg, "sysroot")) {
|
||||
ctx.arg.sysroot = arg;
|
||||
} else if (read_arg(ctx, args, arg, "unique")) {
|
||||
std::optional<GlobPattern> pat = GlobPattern::compile(arg);
|
||||
if (!pat)
|
||||
Fatal(ctx) << "-unique: invalid glob pattern: " << arg;
|
||||
ctx.arg.unique = std::move(*pat);
|
||||
} else if (read_arg(ctx, args, arg, "unresolved-symbols")) {
|
||||
if (arg == "report-all" || arg == "ignore-in-shared-libs")
|
||||
ctx.arg.unresolved_symbols = UNRESOLVED_ERROR;
|
||||
else if (arg == "ignore-all" || arg == "ignore-in-object-files")
|
||||
ctx.arg.unresolved_symbols = UNRESOLVED_IGNORE;
|
||||
else
|
||||
Fatal(ctx) << "unknown --unresolved-symbols argument: " << arg;
|
||||
} else if (read_arg(ctx, args, arg, "u") ||
|
||||
read_arg(ctx, args, arg, "undefined")) {
|
||||
ctx.arg.undefined.push_back(arg);
|
||||
} else if (read_arg(ctx, args, arg, "require-defined")) {
|
||||
ctx.arg.require_defined.push_back(arg);
|
||||
} else if (read_arg(ctx, args, arg, "init")) {
|
||||
ctx.arg.init = arg;
|
||||
} else if (read_arg(ctx, args, arg, "fini")) {
|
||||
ctx.arg.fini = arg;
|
||||
} else if (read_arg(ctx, args, arg, "hash-style")) {
|
||||
if (arg == "sysv") {
|
||||
ctx.arg.hash_style_sysv = true;
|
||||
ctx.arg.hash_style_gnu = false;
|
||||
} else if (arg == "gnu") {
|
||||
ctx.arg.hash_style_sysv = false;
|
||||
ctx.arg.hash_style_gnu = true;
|
||||
} else if (arg == "both") {
|
||||
ctx.arg.hash_style_sysv = true;
|
||||
ctx.arg.hash_style_gnu = true;
|
||||
} else {
|
||||
Fatal(ctx) << "invalid --hash-style argument: " << arg;
|
||||
}
|
||||
} else if (read_arg(ctx, args, arg, "soname") ||
|
||||
read_arg(ctx, args, arg, "h")) {
|
||||
ctx.arg.soname = arg;
|
||||
} else if (read_flag(args, "allow-multiple-definition")) {
|
||||
ctx.arg.allow_multiple_definition = true;
|
||||
} else if (read_flag(args, "trace")) {
|
||||
ctx.arg.trace = true;
|
||||
} else if (read_flag(args, "eh-frame-hdr")) {
|
||||
ctx.arg.eh_frame_hdr = true;
|
||||
} else if (read_flag(args, "no-eh-frame-hdr")) {
|
||||
ctx.arg.eh_frame_hdr = false;
|
||||
} else if (read_flag(args, "pie") || read_flag(args, "pic-executable")) {
|
||||
ctx.arg.pic = true;
|
||||
ctx.arg.pie = true;
|
||||
} else if (read_flag(args, "no-pie") ||
|
||||
read_flag(args, "no-pic-executable")) {
|
||||
ctx.arg.pic = false;
|
||||
ctx.arg.pie = false;
|
||||
} else if (read_flag(args, "relax")) {
|
||||
ctx.arg.relax = true;
|
||||
} else if (read_flag(args, "no-relax")) {
|
||||
ctx.arg.relax = false;
|
||||
} else if (read_flag(args, "r") || read_flag(args, "relocatable")) {
|
||||
ctx.arg.relocatable = true;
|
||||
} else if (read_flag(args, "perf")) {
|
||||
ctx.arg.perf = true;
|
||||
} else if (read_flag(args, "pack-dyn-relocs=relr")) {
|
||||
ctx.arg.pack_dyn_relocs_relr = true;
|
||||
} else if (read_flag(args, "pack-dyn-relocs=none")) {
|
||||
ctx.arg.pack_dyn_relocs_relr = false;
|
||||
} else if (read_flag(args, "stats")) {
|
||||
ctx.arg.stats = true;
|
||||
Counter::enabled = true;
|
||||
} else if (read_arg(ctx, args, arg, "C") ||
|
||||
read_arg(ctx, args, arg, "directory")) {
|
||||
ctx.arg.directory = arg;
|
||||
} else if (read_arg(ctx, args, arg, "chroot")) {
|
||||
ctx.arg.chroot = arg;
|
||||
} else if (args[0] == "-color-diagnostics=auto" ||
|
||||
args[0] == "--color-diagnostics=auto") {
|
||||
ctx.arg.color_diagnostics = isatty(STDERR_FILENO);
|
||||
args = args.subspan(1);
|
||||
} else if (args[0] == "-color-diagnostics=always" ||
|
||||
args[0] == "--color-diagnostics=always") {
|
||||
ctx.arg.color_diagnostics = true;
|
||||
args = args.subspan(1);
|
||||
} else if (args[0] == "-color-diagnostics=never" ||
|
||||
args[0] == "--color-diagnostics=never") {
|
||||
ctx.arg.color_diagnostics = false;
|
||||
args = args.subspan(1);
|
||||
} else if (read_flag(args, "color-diagnostics")) {
|
||||
ctx.arg.color_diagnostics = true;
|
||||
} else if (read_flag(args, "warn-common")) {
|
||||
ctx.arg.warn_common = true;
|
||||
} else if (read_flag(args, "no-warn-common")) {
|
||||
ctx.arg.warn_common = false;
|
||||
} else if (read_arg(ctx, args, arg, "compress-debug-sections")) {
|
||||
if (arg == "zlib" || arg == "zlib-gabi")
|
||||
ctx.arg.compress_debug_sections = COMPRESS_GABI;
|
||||
else if (arg == "zlib-gnu")
|
||||
ctx.arg.compress_debug_sections = COMPRESS_GNU;
|
||||
else if (arg == "none")
|
||||
ctx.arg.compress_debug_sections = COMPRESS_NONE;
|
||||
else
|
||||
Fatal(ctx) << "invalid --compress-debug-sections argument: " << arg;
|
||||
} else if (read_arg(ctx, args, arg, "wrap")) {
|
||||
ctx.arg.wrap.insert(arg);
|
||||
} else if (read_flag(args, "omagic") || read_flag(args, "N")) {
|
||||
ctx.arg.omagic = true;
|
||||
ctx.arg.is_static = true;
|
||||
} else if (read_flag(args, "no-omagic")) {
|
||||
ctx.arg.omagic = false;
|
||||
} else if (read_arg(ctx, args, arg, "retain-symbols-file")) {
|
||||
read_retain_symbols_file(ctx, arg);
|
||||
} else if (read_flag(args, "repro")) {
|
||||
ctx.arg.repro = true;
|
||||
} else if (read_z_flag(args, "now")) {
|
||||
ctx.arg.z_now = true;
|
||||
} else if (read_z_flag(args, "lazy")) {
|
||||
ctx.arg.z_now = false;
|
||||
} else if (read_z_flag(args, "cet-report=none")) {
|
||||
ctx.arg.z_cet_report = CET_REPORT_NONE;
|
||||
} else if (read_z_flag(args, "cet-report=warning")) {
|
||||
ctx.arg.z_cet_report = CET_REPORT_WARNING;
|
||||
} else if (read_z_flag(args, "cet-report=error")) {
|
||||
ctx.arg.z_cet_report = CET_REPORT_ERROR;
|
||||
} else if (read_z_flag(args, "execstack")) {
|
||||
ctx.arg.z_execstack = true;
|
||||
} else if (read_z_arg(ctx, args, arg, "max-page-size")) {
|
||||
ctx.page_size = parse_number(ctx, "-z max-page-size", arg);
|
||||
if (__builtin_popcountll(ctx.page_size) != 1)
|
||||
Fatal(ctx) << "-z max-page-size " << arg << ": value must be a power of 2";
|
||||
} else if (read_z_flag(args, "noexecstack")) {
|
||||
ctx.arg.z_execstack = false;
|
||||
} else if (read_z_flag(args, "relro")) {
|
||||
ctx.arg.z_relro = true;
|
||||
} else if (read_z_flag(args, "norelro")) {
|
||||
ctx.arg.z_relro = false;
|
||||
} else if (read_z_flag(args, "defs")) {
|
||||
ctx.arg.z_defs = true;
|
||||
} else if (read_z_flag(args, "nodefs")) {
|
||||
ctx.arg.z_defs = false;
|
||||
} else if (read_z_flag(args, "nodlopen")) {
|
||||
ctx.arg.z_dlopen = false;
|
||||
} else if (read_z_flag(args, "nodelete")) {
|
||||
ctx.arg.z_delete = false;
|
||||
} else if (read_z_flag(args, "nocopyreloc")) {
|
||||
ctx.arg.z_copyreloc = false;
|
||||
} else if (read_z_flag(args, "nodump")) {
|
||||
ctx.arg.z_dump = false;
|
||||
} else if (read_z_flag(args, "initfirst")) {
|
||||
ctx.arg.z_initfirst = true;
|
||||
} else if (read_z_flag(args, "interpose")) {
|
||||
ctx.arg.z_interpose = true;
|
||||
} else if (read_z_flag(args, "ibt")) {
|
||||
ctx.arg.z_ibt = true;
|
||||
ctx.arg.z_ibtplt = true;
|
||||
} else if (read_z_flag(args, "ibtplt")) {
|
||||
ctx.arg.z_ibtplt = true;
|
||||
} else if (read_z_flag(args, "muldefs")) {
|
||||
ctx.arg.allow_multiple_definition = true;
|
||||
} else if (read_z_flag(args, "keep-text-section-prefix")) {
|
||||
ctx.arg.z_keep_text_section_prefix = true;
|
||||
} else if (read_z_flag(args, "nokeep-text-section-prefix")) {
|
||||
ctx.arg.z_keep_text_section_prefix = false;
|
||||
} else if (read_z_flag(args, "shstk")) {
|
||||
ctx.arg.z_shstk = true;
|
||||
} else if (read_z_flag(args, "text")) {
|
||||
ctx.arg.z_text = true;
|
||||
} else if (read_z_flag(args, "notext") || read_z_flag(args, "textoff")) {
|
||||
ctx.arg.z_text = false;
|
||||
} else if (read_z_flag(args, "origin")) {
|
||||
ctx.arg.z_origin = true;
|
||||
} else if (read_z_flag(args, "nodefaultlib")) {
|
||||
ctx.arg.z_nodefaultlib = true;
|
||||
} else if (read_z_flag(args, "separate-loadable-segments")) {
|
||||
ctx.arg.z_separate_code = SEPARATE_LOADABLE_SEGMENTS;
|
||||
} else if (read_z_flag(args, "separate-code")) {
|
||||
ctx.arg.z_separate_code = SEPARATE_CODE;
|
||||
} else if (read_z_flag(args, "noseparate-code")) {
|
||||
ctx.arg.z_separate_code = NOSEPARATE_CODE;
|
||||
} else if (read_flag(args, "no-undefined")) {
|
||||
ctx.arg.z_defs = true;
|
||||
} else if (read_flag(args, "fatal-warnings")) {
|
||||
ctx.arg.fatal_warnings = true;
|
||||
} else if (read_flag(args, "no-fatal-warnings")) {
|
||||
ctx.arg.fatal_warnings = false;
|
||||
} else if (read_flag(args, "fork")) {
|
||||
ctx.arg.fork = true;
|
||||
} else if (read_flag(args, "no-fork")) {
|
||||
ctx.arg.fork = false;
|
||||
} else if (read_flag(args, "gc-sections")) {
|
||||
ctx.arg.gc_sections = true;
|
||||
} else if (read_flag(args, "no-gc-sections")) {
|
||||
ctx.arg.gc_sections = false;
|
||||
} else if (read_flag(args, "print-gc-sections")) {
|
||||
ctx.arg.print_gc_sections = true;
|
||||
} else if (read_flag(args, "no-print-gc-sections")) {
|
||||
ctx.arg.print_gc_sections = false;
|
||||
} else if (read_arg(ctx, args, arg, "icf")) {
|
||||
if (arg == "all")
|
||||
ctx.arg.icf = true;
|
||||
else if (arg == "none")
|
||||
ctx.arg.icf = false;
|
||||
else
|
||||
Fatal(ctx) << "unknown --icf argument: " << arg;
|
||||
} else if (read_flag(args, "no-icf")) {
|
||||
ctx.arg.icf = false;
|
||||
} else if (read_arg(ctx, args, arg, "image-base")) {
|
||||
ctx.arg.image_base = parse_number(ctx, "image-base", arg);
|
||||
} else if (read_flag(args, "print-icf-sections")) {
|
||||
ctx.arg.print_icf_sections = true;
|
||||
} else if (read_flag(args, "no-print-icf-sections")) {
|
||||
ctx.arg.print_icf_sections = false;
|
||||
} else if (read_flag(args, "quick-exit")) {
|
||||
ctx.arg.quick_exit = true;
|
||||
} else if (read_flag(args, "no-quick-exit")) {
|
||||
ctx.arg.quick_exit = false;
|
||||
} else if (read_arg(ctx, args, arg, "thread-count")) {
|
||||
ctx.arg.thread_count = parse_number(ctx, "thread-count", arg);
|
||||
} else if (read_flag(args, "threads")) {
|
||||
ctx.arg.thread_count = 0;
|
||||
} else if (read_flag(args, "no-threads")) {
|
||||
ctx.arg.thread_count = 1;
|
||||
} else if (args[0].starts_with("-threads=")) {
|
||||
ctx.arg.thread_count = parse_number(ctx, "threads=", args[0].substr(9));
|
||||
args = args.subspan(1);
|
||||
} else if (args[0].starts_with("--threads=")) {
|
||||
ctx.arg.thread_count = parse_number(ctx, "threads=", args[0].substr(10));
|
||||
args = args.subspan(1);
|
||||
} else if (read_flag(args, "discard-all") || read_flag(args, "x")) {
|
||||
ctx.arg.discard_all = true;
|
||||
} else if (read_flag(args, "discard-locals") || read_flag(args, "X")) {
|
||||
ctx.arg.discard_locals = true;
|
||||
} else if (read_flag(args, "strip-all") || read_flag(args, "s")) {
|
||||
ctx.arg.strip_all = true;
|
||||
} else if (read_flag(args, "strip-debug") || read_flag(args, "S")) {
|
||||
ctx.arg.strip_all = true;
|
||||
} else if (read_flag(args, "warn-unresolved-symbols")) {
|
||||
ctx.arg.unresolved_symbols = UNRESOLVED_WARN;
|
||||
} else if (read_flag(args, "error-unresolved-symbols")) {
|
||||
ctx.arg.unresolved_symbols = UNRESOLVED_ERROR;
|
||||
} else if (read_arg(ctx, args, arg, "rpath")) {
|
||||
if (!ctx.arg.rpaths.empty())
|
||||
ctx.arg.rpaths += ":";
|
||||
ctx.arg.rpaths += arg;
|
||||
} else if (read_arg(ctx, args, arg, "R")) {
|
||||
if (is_file(arg))
|
||||
Fatal(ctx) << "-R" << arg
|
||||
<< ": -R as an alias for --just-symbols is not supported";
|
||||
|
||||
if (!ctx.arg.rpaths.empty())
|
||||
ctx.arg.rpaths += ":";
|
||||
ctx.arg.rpaths += arg;
|
||||
} else if (read_flag(args, "build-id")) {
|
||||
ctx.arg.build_id.kind = BuildId::HASH;
|
||||
ctx.arg.build_id.hash_size = 20;
|
||||
} else if (read_arg(ctx, args, arg, "build-id")) {
|
||||
if (arg == "none") {
|
||||
ctx.arg.build_id.kind = BuildId::NONE;
|
||||
} else if (arg == "uuid") {
|
||||
ctx.arg.build_id.kind = BuildId::UUID;
|
||||
} else if (arg == "md5") {
|
||||
ctx.arg.build_id.kind = BuildId::HASH;
|
||||
ctx.arg.build_id.hash_size = 16;
|
||||
} else if (arg == "sha1") {
|
||||
ctx.arg.build_id.kind = BuildId::HASH;
|
||||
ctx.arg.build_id.hash_size = 20;
|
||||
} else if (arg == "sha256") {
|
||||
ctx.arg.build_id.kind = BuildId::HASH;
|
||||
ctx.arg.build_id.hash_size = 32;
|
||||
} else if (arg.starts_with("0x") || arg.starts_with("0X")) {
|
||||
ctx.arg.build_id.kind = BuildId::HEX;
|
||||
ctx.arg.build_id.value = parse_hex_build_id(ctx, arg);
|
||||
} else {
|
||||
Fatal(ctx) << "invalid --build-id argument: " << arg;
|
||||
}
|
||||
} else if (read_flag(args, "no-build-id")) {
|
||||
ctx.arg.build_id.kind = BuildId::NONE;
|
||||
} else if (read_arg(ctx, args, arg, "format") ||
|
||||
read_arg(ctx, args, arg, "b")) {
|
||||
if (arg == "binary")
|
||||
Fatal(ctx)
|
||||
<< "mold does not suppor `-b binary`. If you want to convert a binary"
|
||||
" file into an object file, use `objcopy -I binary -O elf64-x86-64"
|
||||
<< " <input-file> <output-file.o>` instead.";
|
||||
Fatal(ctx) << "unknown command line option: -b " << arg;
|
||||
} else if (read_arg(ctx, args, arg, "auxiliary") ||
|
||||
read_arg(ctx, args, arg, "f")) {
|
||||
ctx.arg.auxiliary.push_back(arg);
|
||||
} else if (read_arg(ctx, args, arg, "filter") ||
|
||||
read_arg(ctx, args, arg, "F")) {
|
||||
ctx.arg.filter.push_back(arg);
|
||||
} else if (read_flag(args, "preload")) {
|
||||
ctx.arg.preload = true;
|
||||
} else if (read_flag(args, "no-preload")) {
|
||||
ctx.arg.preload = false;
|
||||
} else if (read_flag(args, "apply-dynamic-relocs")) {
|
||||
} else if (read_arg(ctx, args, arg, "O")) {
|
||||
} else if (read_flag(args, "O0")) {
|
||||
} else if (read_flag(args, "O1")) {
|
||||
} else if (read_flag(args, "O2")) {
|
||||
} else if (read_flag(args, "verbose")) {
|
||||
} else if (read_arg(ctx, args, arg, "plugin")) {
|
||||
} else if (read_arg(ctx, args, arg, "plugin-opt")) {
|
||||
} else if (read_flag(args, "color-diagnostics")) {
|
||||
} else if (read_flag(args, "gdb-index")) {
|
||||
} else if (read_flag(args, "eh-frame-hdr")) {
|
||||
} else if (read_flag(args, "start-group")) {
|
||||
} else if (read_flag(args, "end-group")) {
|
||||
} else if (read_flag(args, "(")) {
|
||||
} else if (read_flag(args, ")")) {
|
||||
} else if (read_flag(args, "fatal-warnings")) {
|
||||
} else if (read_flag(args, "enable-new-dtags")) {
|
||||
} else if (read_flag(args, "disable-new-dtags")) {
|
||||
} else if (read_flag(args, "nostdlib")) {
|
||||
} else if (read_flag(args, "allow-shlib-undefined")) {
|
||||
} else if (read_flag(args, "no-allow-shlib-undefined")) {
|
||||
} else if (read_flag(args, "no-add-needed")) {
|
||||
} else if (read_flag(args, "no-call-graph-profile-sort")) {
|
||||
} else if (read_flag(args, "no-copy-dt-needed-entries")) {
|
||||
} else if (read_flag(args, "no-undefined-version")) {
|
||||
} else if (read_arg(ctx, args, arg, "sort-section")) {
|
||||
} else if (read_flag(args, "sort-common")) {
|
||||
} else if (read_flag(args, "dc")) {
|
||||
} else if (read_flag(args, "dp")) {
|
||||
} else if (read_flag(args, "fix-cortex-a53-835769")) {
|
||||
} else if (read_flag(args, "fix-cortex-a53-843419")) {
|
||||
} else if (read_flag(args, "EL")) {
|
||||
} else if (read_flag(args, "warn-once")) {
|
||||
} else if (read_flag(args, "nodefaultlibs")) {
|
||||
} else if (read_flag(args, "warn-constructors")) {
|
||||
} else if (read_flag(args, "warn-execstack")) {
|
||||
} else if (read_flag(args, "no-warn-execstack")) {
|
||||
} else if (read_arg(ctx, args, arg, "rpath-link")) {
|
||||
} else if (read_z_flag(args, "combreloc")) {
|
||||
} else if (read_z_flag(args, "nocombreloc")) {
|
||||
} else if (read_z_arg(ctx, args, arg, "common-page-size")) {
|
||||
} else if (read_arg(ctx, args, arg, "version-script")) {
|
||||
remaining.push_back("--version-script");
|
||||
remaining.push_back(arg);
|
||||
} else if (read_arg(ctx, args, arg, "dynamic-list")) {
|
||||
remaining.push_back("--dynamic-list");
|
||||
remaining.push_back(arg);
|
||||
} else if (read_flag(args, "as-needed")) {
|
||||
remaining.push_back("-as-needed");
|
||||
} else if (read_flag(args, "no-as-needed")) {
|
||||
remaining.push_back("-no-as-needed");
|
||||
} else if (read_flag(args, "whole-archive")) {
|
||||
remaining.push_back("-whole-archive");
|
||||
} else if (read_flag(args, "no-whole-archive")) {
|
||||
remaining.push_back("-no-whole-archive");
|
||||
} else if (read_arg(ctx, args, arg, "l")) {
|
||||
remaining.push_back("-l");
|
||||
remaining.push_back(arg);
|
||||
} else if (read_arg(ctx, args, arg, "script") ||
|
||||
read_arg(ctx, args, arg, "T")) {
|
||||
remaining.push_back(arg);
|
||||
} else if (read_flag(args, "push-state")) {
|
||||
remaining.push_back("-push-state");
|
||||
} else if (read_flag(args, "pop-state")) {
|
||||
remaining.push_back("-pop-state");
|
||||
} else if (args[0].starts_with("-z") && args[0].size() > 2) {
|
||||
Warn(ctx) << "unknown command line option: " << args[0];
|
||||
args = args.subspan(1);
|
||||
} else if (args[0] == "-z" && args.size() >= 2) {
|
||||
Warn(ctx) << "unknown command line option: -z " << args[1];
|
||||
args = args.subspan(2);
|
||||
} else {
|
||||
if (args[0][0] == '-')
|
||||
Fatal(ctx) << "unknown command line option: " << args[0];
|
||||
remaining.push_back(args[0]);
|
||||
args = args.subspan(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!ctx.arg.sysroot.empty()) {
|
||||
for (std::string &path : ctx.arg.library_paths) {
|
||||
if (std::string_view(path).starts_with('='))
|
||||
path = ctx.arg.sysroot + path.substr(1);
|
||||
else if (std::string_view(path).starts_with("$SYSROOT"))
|
||||
path = ctx.arg.sysroot + path.substr(8);
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx.arg.shared) {
|
||||
ctx.arg.pic = true;
|
||||
ctx.arg.dynamic_linker = "";
|
||||
}
|
||||
|
||||
if (ctx.arg.pic)
|
||||
ctx.arg.image_base = 0;
|
||||
|
||||
if (ctx.arg.retain_symbols_file) {
|
||||
ctx.arg.strip_all = false;
|
||||
ctx.arg.discard_all = false;
|
||||
}
|
||||
|
||||
if (ctx.arg.relocatable)
|
||||
ctx.arg.is_static = true;
|
||||
|
||||
if (!ctx.arg.shared) {
|
||||
if (!ctx.arg.filter.empty())
|
||||
Fatal(ctx) << "-filter may not be used without -shared";
|
||||
if (!ctx.arg.auxiliary.empty())
|
||||
Fatal(ctx) << "-auxiliary may not be used without -shared";
|
||||
}
|
||||
|
||||
if (ctx.arg.image_base % ctx.page_size)
|
||||
Fatal(ctx) << "-image-base msut be a multiple of -max-page-size";
|
||||
|
||||
if (char *env = getenv("MOLD_REPRO"); env && env[0])
|
||||
ctx.arg.repro = true;
|
||||
|
||||
if (ctx.arg.output.empty())
|
||||
ctx.arg.output = "a.out";
|
||||
|
||||
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);
|
||||
ctx.arg.version_definitions.push_back(ver);
|
||||
ctx.default_version = VER_NDX_LAST_RESERVED + 1;
|
||||
}
|
||||
|
||||
std::tie(ctx.plt_hdr_size, ctx.plt_size) = get_plt_size(ctx);
|
||||
|
||||
ctx.arg.undefined.push_back(ctx.arg.entry);
|
||||
|
||||
// TLSDESC relocs must be always relaxed for statically-linked
|
||||
// executables even if -no-relax is given. It is because a
|
||||
// statically-linked executable doesn't contain a tranpoline
|
||||
// function needed for TLSDESC.
|
||||
ctx.relax_tlsdesc = ctx.arg.is_static || (ctx.arg.relax && !ctx.arg.shared);
|
||||
|
||||
if (version_shown && remaining.empty())
|
||||
exit(0);
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template \
|
||||
bool read_arg(Context<E> &ctx, std::span<std::string_view> &args, \
|
||||
std::string_view &arg, \
|
||||
std::string name); \
|
||||
\
|
||||
template std::string create_response_file(Context<E> &ctx); \
|
||||
\
|
||||
template \
|
||||
void parse_nonpositional_args(Context<E> &ctx, \
|
||||
std::vector<std::string_view> &remaining)
|
||||
|
||||
INSTANTIATE(X86_64);
|
||||
INSTANTIATE(I386);
|
||||
INSTANTIATE(ARM64);
|
||||
|
||||
} // namespace mold::elf
|
||||
@@ -0,0 +1,953 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
#ifdef __BIG_ENDIAN__
|
||||
#error "mold does not support big-endian hosts"
|
||||
#endif
|
||||
|
||||
namespace mold::elf {
|
||||
|
||||
typedef uint8_t u8;
|
||||
typedef uint16_t u16;
|
||||
typedef uint32_t u32;
|
||||
typedef uint64_t u64;
|
||||
|
||||
typedef int8_t i8;
|
||||
typedef int16_t i16;
|
||||
typedef int32_t i32;
|
||||
typedef int64_t i64;
|
||||
|
||||
struct X86_64;
|
||||
struct I386;
|
||||
struct ARM64;
|
||||
|
||||
template <typename E> struct ElfSym;
|
||||
template <typename E> struct ElfShdr;
|
||||
template <typename E> struct ElfEhdr;
|
||||
template <typename E> struct ElfPhdr;
|
||||
template <typename E> struct ElfRel;
|
||||
template <typename E> struct ElfDyn;
|
||||
template <typename E> struct ElfChdr;
|
||||
|
||||
template <typename E>
|
||||
std::string rel_to_string(u32 r_type);
|
||||
|
||||
template <typename E>
|
||||
std::ostream &operator<<(std::ostream &out, const ElfRel<E> &rel) {
|
||||
out << rel_to_string<E>(rel.r_type);
|
||||
return out;
|
||||
}
|
||||
|
||||
static constexpr u32 SHN_UNDEF = 0;
|
||||
static constexpr u32 SHN_ABS = 0xfff1;
|
||||
static constexpr u32 SHN_COMMON = 0xfff2;
|
||||
static constexpr u32 SHN_XINDEX = 0xffff;
|
||||
|
||||
static constexpr u32 SHT_NULL = 0;
|
||||
static constexpr u32 SHT_PROGBITS = 1;
|
||||
static constexpr u32 SHT_SYMTAB = 2;
|
||||
static constexpr u32 SHT_STRTAB = 3;
|
||||
static constexpr u32 SHT_RELA = 4;
|
||||
static constexpr u32 SHT_HASH = 5;
|
||||
static constexpr u32 SHT_DYNAMIC = 6;
|
||||
static constexpr u32 SHT_NOTE = 7;
|
||||
static constexpr u32 SHT_NOBITS = 8;
|
||||
static constexpr u32 SHT_REL = 9;
|
||||
static constexpr u32 SHT_SHLIB = 10;
|
||||
static constexpr u32 SHT_DYNSYM = 11;
|
||||
static constexpr u32 SHT_INIT_ARRAY = 14;
|
||||
static constexpr u32 SHT_FINI_ARRAY = 15;
|
||||
static constexpr u32 SHT_PREINIT_ARRAY = 16;
|
||||
static constexpr u32 SHT_GROUP = 17;
|
||||
static constexpr u32 SHT_SYMTAB_SHNDX = 18;
|
||||
static constexpr u32 SHT_RELR = 19;
|
||||
static constexpr u32 SHT_GNU_HASH = 0x6ffffff6;
|
||||
static constexpr u32 SHT_GNU_VERDEF = 0x6ffffffd;
|
||||
static constexpr u32 SHT_GNU_VERNEED = 0x6ffffffe;
|
||||
static constexpr u32 SHT_GNU_VERSYM = 0x6fffffff;
|
||||
static constexpr u32 SHT_X86_64_UNWIND = 0x70000001;
|
||||
|
||||
static constexpr u32 SHF_WRITE = 0x1;
|
||||
static constexpr u32 SHF_ALLOC = 0x2;
|
||||
static constexpr u32 SHF_EXECINSTR = 0x4;
|
||||
static constexpr u32 SHF_MERGE = 0x10;
|
||||
static constexpr u32 SHF_STRINGS = 0x20;
|
||||
static constexpr u32 SHF_INFO_LINK = 0x40;
|
||||
static constexpr u32 SHF_LINK_ORDER = 0x80;
|
||||
static constexpr u32 SHF_GROUP = 0x200;
|
||||
static constexpr u32 SHF_TLS = 0x400;
|
||||
static constexpr u32 SHF_COMPRESSED = 0x800;
|
||||
static constexpr u32 SHF_EXCLUDE = 0x80000000;
|
||||
|
||||
static constexpr u32 GRP_COMDAT = 1;
|
||||
|
||||
static constexpr u32 STT_NOTYPE = 0;
|
||||
static constexpr u32 STT_OBJECT = 1;
|
||||
static constexpr u32 STT_FUNC = 2;
|
||||
static constexpr u32 STT_SECTION = 3;
|
||||
static constexpr u32 STT_FILE = 4;
|
||||
static constexpr u32 STT_COMMON = 5;
|
||||
static constexpr u32 STT_TLS = 6;
|
||||
static constexpr u32 STT_GNU_IFUNC = 10;
|
||||
|
||||
static constexpr u32 STB_LOCAL = 0;
|
||||
static constexpr u32 STB_GLOBAL = 1;
|
||||
static constexpr u32 STB_WEAK = 2;
|
||||
static constexpr u32 STB_GNU_UNIQUE = 10;
|
||||
|
||||
static constexpr u32 STV_DEFAULT = 0;
|
||||
static constexpr u32 STV_INTERNAL = 1;
|
||||
static constexpr u32 STV_HIDDEN = 2;
|
||||
static constexpr u32 STV_PROTECTED = 3;
|
||||
|
||||
static constexpr u32 VER_NDX_LOCAL = 0;
|
||||
static constexpr u32 VER_NDX_GLOBAL = 1;
|
||||
static constexpr u32 VER_NDX_LAST_RESERVED = 1;
|
||||
|
||||
static constexpr u32 VER_FLG_BASE = 1;
|
||||
static constexpr u32 VER_FLG_WEAK = 2;
|
||||
static constexpr u32 VER_FLG_INFO = 4;
|
||||
|
||||
static constexpr u32 VERSYM_HIDDEN = 0x8000;
|
||||
|
||||
static constexpr u32 PT_NULL = 0;
|
||||
static constexpr u32 PT_LOAD = 1;
|
||||
static constexpr u32 PT_DYNAMIC = 2;
|
||||
static constexpr u32 PT_INTERP = 3;
|
||||
static constexpr u32 PT_NOTE = 4;
|
||||
static constexpr u32 PT_SHLIB = 5;
|
||||
static constexpr u32 PT_PHDR = 6;
|
||||
static constexpr u32 PT_TLS = 7;
|
||||
static constexpr u32 PT_GNU_EH_FRAME = 0x6474e550;
|
||||
static constexpr u32 PT_GNU_STACK = 0x6474e551;
|
||||
static constexpr u32 PT_GNU_RELRO = 0x6474e552;
|
||||
|
||||
static constexpr u32 PF_X = 1;
|
||||
static constexpr u32 PF_W = 2;
|
||||
static constexpr u32 PF_R = 4;
|
||||
|
||||
static constexpr u32 ET_NONE = 0;
|
||||
static constexpr u32 ET_REL = 1;
|
||||
static constexpr u32 ET_EXEC = 2;
|
||||
static constexpr u32 ET_DYN = 3;
|
||||
|
||||
static constexpr u32 ELFDATA2LSB = 1;
|
||||
static constexpr u32 ELFDATA2MSB = 2;
|
||||
|
||||
static constexpr u32 ELFCLASS32 = 1;
|
||||
static constexpr u32 ELFCLASS64 = 2;
|
||||
|
||||
static constexpr u32 EV_CURRENT = 1;
|
||||
|
||||
static constexpr u32 EM_386 = 3;
|
||||
static constexpr u32 EM_X86_64 = 62;
|
||||
static constexpr u32 EM_AARCH64 = 183;
|
||||
|
||||
static constexpr u32 EI_CLASS = 4;
|
||||
static constexpr u32 EI_DATA = 5;
|
||||
static constexpr u32 EI_VERSION = 6;
|
||||
static constexpr u32 EI_OSABI = 7;
|
||||
static constexpr u32 EI_ABIVERSION = 8;
|
||||
|
||||
static constexpr u32 ELFOSABI_NONE = 0;
|
||||
static constexpr u32 ELFOSABI_GNU = 3;
|
||||
|
||||
static constexpr u32 DT_NULL = 0;
|
||||
static constexpr u32 DT_NEEDED = 1;
|
||||
static constexpr u32 DT_PLTRELSZ = 2;
|
||||
static constexpr u32 DT_PLTGOT = 3;
|
||||
static constexpr u32 DT_HASH = 4;
|
||||
static constexpr u32 DT_STRTAB = 5;
|
||||
static constexpr u32 DT_SYMTAB = 6;
|
||||
static constexpr u32 DT_RELA = 7;
|
||||
static constexpr u32 DT_RELASZ = 8;
|
||||
static constexpr u32 DT_RELAENT = 9;
|
||||
static constexpr u32 DT_STRSZ = 10;
|
||||
static constexpr u32 DT_SYMENT = 11;
|
||||
static constexpr u32 DT_INIT = 12;
|
||||
static constexpr u32 DT_FINI = 13;
|
||||
static constexpr u32 DT_SONAME = 14;
|
||||
static constexpr u32 DT_RPATH = 15;
|
||||
static constexpr u32 DT_SYMBOLIC = 16;
|
||||
static constexpr u32 DT_REL = 17;
|
||||
static constexpr u32 DT_RELSZ = 18;
|
||||
static constexpr u32 DT_RELENT = 19;
|
||||
static constexpr u32 DT_PLTREL = 20;
|
||||
static constexpr u32 DT_DEBUG = 21;
|
||||
static constexpr u32 DT_TEXTREL = 22;
|
||||
static constexpr u32 DT_JMPREL = 23;
|
||||
static constexpr u32 DT_BIND_NOW = 24;
|
||||
static constexpr u32 DT_INIT_ARRAY = 25;
|
||||
static constexpr u32 DT_FINI_ARRAY = 26;
|
||||
static constexpr u32 DT_INIT_ARRAYSZ = 27;
|
||||
static constexpr u32 DT_FINI_ARRAYSZ = 28;
|
||||
static constexpr u32 DT_RUNPATH = 29;
|
||||
static constexpr u32 DT_FLAGS = 30;
|
||||
static constexpr u32 DT_RELRSZ = 35;
|
||||
static constexpr u32 DT_RELR = 36;
|
||||
static constexpr u32 DT_RELRENT = 37;
|
||||
static constexpr u32 DT_GNU_HASH = 0x6ffffef5;
|
||||
static constexpr u32 DT_VERSYM = 0x6ffffff0;
|
||||
static constexpr u32 DT_RELACOUNT = 0x6ffffff9;
|
||||
static constexpr u32 DT_RELCOUNT = 0x6ffffffa;
|
||||
static constexpr u32 DT_FLAGS_1 = 0x6ffffffb;
|
||||
static constexpr u32 DT_VERDEF = 0x6ffffffc;
|
||||
static constexpr u32 DT_VERDEFNUM = 0x6ffffffd;
|
||||
static constexpr u32 DT_VERNEED = 0x6ffffffe;
|
||||
static constexpr u32 DT_VERNEEDNUM = 0x6fffffff;
|
||||
static constexpr u32 DT_AUXILIARY = 0x7ffffffd;
|
||||
static constexpr u32 DT_FILTER = 0x7fffffff;
|
||||
|
||||
static constexpr u32 DF_ORIGIN = 0x01;
|
||||
static constexpr u32 DF_SYMBOLIC = 0x02;
|
||||
static constexpr u32 DF_TEXTREL = 0x04;
|
||||
static constexpr u32 DF_BIND_NOW = 0x08;
|
||||
static constexpr u32 DF_STATIC_TLS = 0x10;
|
||||
|
||||
static constexpr u32 DF_1_NOW = 0x00000001;
|
||||
static constexpr u32 DF_1_NODELETE = 0x00000008;
|
||||
static constexpr u32 DF_1_INITFIRST = 0x00000020;
|
||||
static constexpr u32 DF_1_NOOPEN = 0x00000040;
|
||||
static constexpr u32 DF_1_ORIGIN = 0x00000080;
|
||||
static constexpr u32 DF_1_INTERPOSE = 0x00000400;
|
||||
static constexpr u32 DF_1_NODEFLIB = 0x00000800;
|
||||
static constexpr u32 DF_1_NODUMP = 0x00001000;
|
||||
static constexpr u32 DF_1_PIE = 0x08000000;
|
||||
|
||||
static constexpr u32 NT_GNU_ABI_TAG = 1;
|
||||
static constexpr u32 NT_GNU_HWCAP = 2;
|
||||
static constexpr u32 NT_GNU_BUILD_ID = 3;
|
||||
static constexpr u32 NT_GNU_GOLD_VERSION = 4;
|
||||
static constexpr u32 NT_GNU_PROPERTY_TYPE_0 = 5;
|
||||
|
||||
static constexpr u32 GNU_PROPERTY_AARCH64_FEATURE_1_AND = 0xc0000000;
|
||||
static constexpr u32 GNU_PROPERTY_X86_FEATURE_1_AND = 0xc0000002;
|
||||
|
||||
static constexpr u32 GNU_PROPERTY_X86_FEATURE_1_IBT = 1;
|
||||
static constexpr u32 GNU_PROPERTY_X86_FEATURE_1_SHSTK = 2;
|
||||
|
||||
static constexpr u32 ELFCOMPRESS_ZLIB = 1;
|
||||
|
||||
static constexpr u32 R_X86_64_NONE = 0;
|
||||
static constexpr u32 R_X86_64_64 = 1;
|
||||
static constexpr u32 R_X86_64_PC32 = 2;
|
||||
static constexpr u32 R_X86_64_GOT32 = 3;
|
||||
static constexpr u32 R_X86_64_PLT32 = 4;
|
||||
static constexpr u32 R_X86_64_COPY = 5;
|
||||
static constexpr u32 R_X86_64_GLOB_DAT = 6;
|
||||
static constexpr u32 R_X86_64_JUMP_SLOT = 7;
|
||||
static constexpr u32 R_X86_64_RELATIVE = 8;
|
||||
static constexpr u32 R_X86_64_GOTPCREL = 9;
|
||||
static constexpr u32 R_X86_64_32 = 10;
|
||||
static constexpr u32 R_X86_64_32S = 11;
|
||||
static constexpr u32 R_X86_64_16 = 12;
|
||||
static constexpr u32 R_X86_64_PC16 = 13;
|
||||
static constexpr u32 R_X86_64_8 = 14;
|
||||
static constexpr u32 R_X86_64_PC8 = 15;
|
||||
static constexpr u32 R_X86_64_DTPMOD64 = 16;
|
||||
static constexpr u32 R_X86_64_DTPOFF64 = 17;
|
||||
static constexpr u32 R_X86_64_TPOFF64 = 18;
|
||||
static constexpr u32 R_X86_64_TLSGD = 19;
|
||||
static constexpr u32 R_X86_64_TLSLD = 20;
|
||||
static constexpr u32 R_X86_64_DTPOFF32 = 21;
|
||||
static constexpr u32 R_X86_64_GOTTPOFF = 22;
|
||||
static constexpr u32 R_X86_64_TPOFF32 = 23;
|
||||
static constexpr u32 R_X86_64_PC64 = 24;
|
||||
static constexpr u32 R_X86_64_GOTOFF64 = 25;
|
||||
static constexpr u32 R_X86_64_GOTPC32 = 26;
|
||||
static constexpr u32 R_X86_64_GOT64 = 27;
|
||||
static constexpr u32 R_X86_64_GOTPCREL64 = 28;
|
||||
static constexpr u32 R_X86_64_GOTPC64 = 29;
|
||||
static constexpr u32 R_X86_64_GOTPLT64 = 30;
|
||||
static constexpr u32 R_X86_64_PLTOFF64 = 31;
|
||||
static constexpr u32 R_X86_64_SIZE32 = 32;
|
||||
static constexpr u32 R_X86_64_SIZE64 = 33;
|
||||
static constexpr u32 R_X86_64_GOTPC32_TLSDESC = 34;
|
||||
static constexpr u32 R_X86_64_TLSDESC_CALL = 35;
|
||||
static constexpr u32 R_X86_64_TLSDESC = 36;
|
||||
static constexpr u32 R_X86_64_IRELATIVE = 37;
|
||||
static constexpr u32 R_X86_64_GOTPCRELX = 41;
|
||||
static constexpr u32 R_X86_64_REX_GOTPCRELX = 42;
|
||||
|
||||
template <>
|
||||
inline std::string rel_to_string<X86_64>(u32 r_type) {
|
||||
switch (r_type) {
|
||||
case R_X86_64_NONE: return "R_X86_64_NONE";
|
||||
case R_X86_64_64: return "R_X86_64_64";
|
||||
case R_X86_64_PC32: return "R_X86_64_PC32";
|
||||
case R_X86_64_GOT32: return "R_X86_64_GOT32";
|
||||
case R_X86_64_PLT32: return "R_X86_64_PLT32";
|
||||
case R_X86_64_COPY: return "R_X86_64_COPY";
|
||||
case R_X86_64_GLOB_DAT: return "R_X86_64_GLOB_DAT";
|
||||
case R_X86_64_JUMP_SLOT: return "R_X86_64_JUMP_SLOT";
|
||||
case R_X86_64_RELATIVE: return "R_X86_64_RELATIVE";
|
||||
case R_X86_64_GOTPCREL: return "R_X86_64_GOTPCREL";
|
||||
case R_X86_64_32: return "R_X86_64_32";
|
||||
case R_X86_64_32S: return "R_X86_64_32S";
|
||||
case R_X86_64_16: return "R_X86_64_16";
|
||||
case R_X86_64_PC16: return "R_X86_64_PC16";
|
||||
case R_X86_64_8: return "R_X86_64_8";
|
||||
case R_X86_64_PC8: return "R_X86_64_PC8";
|
||||
case R_X86_64_DTPMOD64: return "R_X86_64_DTPMOD64";
|
||||
case R_X86_64_DTPOFF64: return "R_X86_64_DTPOFF64";
|
||||
case R_X86_64_TPOFF64: return "R_X86_64_TPOFF64";
|
||||
case R_X86_64_TLSGD: return "R_X86_64_TLSGD";
|
||||
case R_X86_64_TLSLD: return "R_X86_64_TLSLD";
|
||||
case R_X86_64_DTPOFF32: return "R_X86_64_DTPOFF32";
|
||||
case R_X86_64_GOTTPOFF: return "R_X86_64_GOTTPOFF";
|
||||
case R_X86_64_TPOFF32: return "R_X86_64_TPOFF32";
|
||||
case R_X86_64_PC64: return "R_X86_64_PC64";
|
||||
case R_X86_64_GOTOFF64: return "R_X86_64_GOTOFF64";
|
||||
case R_X86_64_GOTPC32: return "R_X86_64_GOTPC32";
|
||||
case R_X86_64_GOT64: return "R_X86_64_GOT64";
|
||||
case R_X86_64_GOTPCREL64: return "R_X86_64_GOTPCREL64";
|
||||
case R_X86_64_GOTPC64: return "R_X86_64_GOTPC64";
|
||||
case R_X86_64_GOTPLT64: return "R_X86_64_GOTPLT64";
|
||||
case R_X86_64_PLTOFF64: return "R_X86_64_PLTOFF64";
|
||||
case R_X86_64_SIZE32: return "R_X86_64_SIZE32";
|
||||
case R_X86_64_SIZE64: return "R_X86_64_SIZE64";
|
||||
case R_X86_64_GOTPC32_TLSDESC: return "R_X86_64_GOTPC32_TLSDESC";
|
||||
case R_X86_64_TLSDESC_CALL: return "R_X86_64_TLSDESC_CALL";
|
||||
case R_X86_64_TLSDESC: return "R_X86_64_TLSDESC";
|
||||
case R_X86_64_IRELATIVE: return "R_X86_64_IRELATIVE";
|
||||
case R_X86_64_GOTPCRELX: return "R_X86_64_GOTPCRELX";
|
||||
case R_X86_64_REX_GOTPCRELX: return "R_X86_64_REX_GOTPCRELX";
|
||||
}
|
||||
return "unknown (" + std::to_string(r_type) + ")";
|
||||
}
|
||||
|
||||
static constexpr u32 R_386_NONE = 0;
|
||||
static constexpr u32 R_386_32 = 1;
|
||||
static constexpr u32 R_386_PC32 = 2;
|
||||
static constexpr u32 R_386_GOT32 = 3;
|
||||
static constexpr u32 R_386_PLT32 = 4;
|
||||
static constexpr u32 R_386_COPY = 5;
|
||||
static constexpr u32 R_386_GLOB_DAT = 6;
|
||||
static constexpr u32 R_386_JUMP_SLOT = 7;
|
||||
static constexpr u32 R_386_RELATIVE = 8;
|
||||
static constexpr u32 R_386_GOTOFF = 9;
|
||||
static constexpr u32 R_386_GOTPC = 10;
|
||||
static constexpr u32 R_386_32PLT = 11;
|
||||
static constexpr u32 R_386_TLS_TPOFF = 14;
|
||||
static constexpr u32 R_386_TLS_IE = 15;
|
||||
static constexpr u32 R_386_TLS_GOTIE = 16;
|
||||
static constexpr u32 R_386_TLS_LE = 17;
|
||||
static constexpr u32 R_386_TLS_GD = 18;
|
||||
static constexpr u32 R_386_TLS_LDM = 19;
|
||||
static constexpr u32 R_386_16 = 20;
|
||||
static constexpr u32 R_386_PC16 = 21;
|
||||
static constexpr u32 R_386_8 = 22;
|
||||
static constexpr u32 R_386_PC8 = 23;
|
||||
static constexpr u32 R_386_TLS_GD_32 = 24;
|
||||
static constexpr u32 R_386_TLS_GD_PUSH = 25;
|
||||
static constexpr u32 R_386_TLS_GD_CALL = 26;
|
||||
static constexpr u32 R_386_TLS_GD_POP = 27;
|
||||
static constexpr u32 R_386_TLS_LDM_32 = 28;
|
||||
static constexpr u32 R_386_TLS_LDM_PUSH = 29;
|
||||
static constexpr u32 R_386_TLS_LDM_CALL = 30;
|
||||
static constexpr u32 R_386_TLS_LDM_POP = 31;
|
||||
static constexpr u32 R_386_TLS_LDO_32 = 32;
|
||||
static constexpr u32 R_386_TLS_IE_32 = 33;
|
||||
static constexpr u32 R_386_TLS_LE_32 = 34;
|
||||
static constexpr u32 R_386_TLS_DTPMOD32 = 35;
|
||||
static constexpr u32 R_386_TLS_DTPOFF32 = 36;
|
||||
static constexpr u32 R_386_TLS_TPOFF32 = 37;
|
||||
static constexpr u32 R_386_SIZE32 = 38;
|
||||
static constexpr u32 R_386_TLS_GOTDESC = 39;
|
||||
static constexpr u32 R_386_TLS_DESC_CALL = 40;
|
||||
static constexpr u32 R_386_TLS_DESC = 41;
|
||||
static constexpr u32 R_386_IRELATIVE = 42;
|
||||
static constexpr u32 R_386_GOT32X = 43;
|
||||
|
||||
template <>
|
||||
inline std::string rel_to_string<I386>(u32 r_type) {
|
||||
switch (r_type) {
|
||||
case R_386_NONE: return "R_386_NONE";
|
||||
case R_386_32: return "R_386_32";
|
||||
case R_386_PC32: return "R_386_PC32";
|
||||
case R_386_GOT32: return "R_386_GOT32";
|
||||
case R_386_PLT32: return "R_386_PLT32";
|
||||
case R_386_COPY: return "R_386_COPY";
|
||||
case R_386_GLOB_DAT: return "R_386_GLOB_DAT";
|
||||
case R_386_JUMP_SLOT: return "R_386_JUMP_SLOT";
|
||||
case R_386_RELATIVE: return "R_386_RELATIVE";
|
||||
case R_386_GOTOFF: return "R_386_GOTOFF";
|
||||
case R_386_GOTPC: return "R_386_GOTPC";
|
||||
case R_386_32PLT: return "R_386_32PLT";
|
||||
case R_386_TLS_TPOFF: return "R_386_TLS_TPOFF";
|
||||
case R_386_TLS_IE: return "R_386_TLS_IE";
|
||||
case R_386_TLS_GOTIE: return "R_386_TLS_GOTIE";
|
||||
case R_386_TLS_LE: return "R_386_TLS_LE";
|
||||
case R_386_TLS_GD: return "R_386_TLS_GD";
|
||||
case R_386_TLS_LDM: return "R_386_TLS_LDM";
|
||||
case R_386_16: return "R_386_16";
|
||||
case R_386_PC16: return "R_386_PC16";
|
||||
case R_386_8: return "R_386_8";
|
||||
case R_386_PC8: return "R_386_PC8";
|
||||
case R_386_TLS_GD_32: return "R_386_TLS_GD_32";
|
||||
case R_386_TLS_GD_PUSH: return "R_386_TLS_GD_PUSH";
|
||||
case R_386_TLS_GD_CALL: return "R_386_TLS_GD_CALL";
|
||||
case R_386_TLS_GD_POP: return "R_386_TLS_GD_POP";
|
||||
case R_386_TLS_LDM_32: return "R_386_TLS_LDM_32";
|
||||
case R_386_TLS_LDM_PUSH: return "R_386_TLS_LDM_PUSH";
|
||||
case R_386_TLS_LDM_CALL: return "R_386_TLS_LDM_CALL";
|
||||
case R_386_TLS_LDM_POP: return "R_386_TLS_LDM_POP";
|
||||
case R_386_TLS_LDO_32: return "R_386_TLS_LDO_32";
|
||||
case R_386_TLS_IE_32: return "R_386_TLS_IE_32";
|
||||
case R_386_TLS_LE_32: return "R_386_TLS_LE_32";
|
||||
case R_386_TLS_DTPMOD32: return "R_386_TLS_DTPMOD32";
|
||||
case R_386_TLS_DTPOFF32: return "R_386_TLS_DTPOFF32";
|
||||
case R_386_TLS_TPOFF32: return "R_386_TLS_TPOFF32";
|
||||
case R_386_SIZE32: return "R_386_SIZE32";
|
||||
case R_386_TLS_GOTDESC: return "R_386_TLS_GOTDESC";
|
||||
case R_386_TLS_DESC_CALL: return "R_386_TLS_DESC_CALL";
|
||||
case R_386_TLS_DESC: return "R_386_TLS_DESC";
|
||||
case R_386_IRELATIVE: return "R_386_IRELATIVE";
|
||||
case R_386_GOT32X: return "R_386_GOT32X";
|
||||
}
|
||||
return "unknown (" + std::to_string(r_type) + ")";
|
||||
}
|
||||
|
||||
static constexpr u32 R_AARCH64_NONE = 0;
|
||||
static constexpr u32 R_AARCH64_ABS64 = 0x101;
|
||||
static constexpr u32 R_AARCH64_ABS32 = 0x102;
|
||||
static constexpr u32 R_AARCH64_ABS16 = 0x103;
|
||||
static constexpr u32 R_AARCH64_PREL64 = 0x104;
|
||||
static constexpr u32 R_AARCH64_PREL32 = 0x105;
|
||||
static constexpr u32 R_AARCH64_PREL16 = 0x106;
|
||||
static constexpr u32 R_AARCH64_MOVW_UABS_G0 = 0x107;
|
||||
static constexpr u32 R_AARCH64_MOVW_UABS_G0_NC = 0x108;
|
||||
static constexpr u32 R_AARCH64_MOVW_UABS_G1 = 0x109;
|
||||
static constexpr u32 R_AARCH64_MOVW_UABS_G1_NC = 0x10a;
|
||||
static constexpr u32 R_AARCH64_MOVW_UABS_G2 = 0x10b;
|
||||
static constexpr u32 R_AARCH64_MOVW_UABS_G2_NC = 0x10c;
|
||||
static constexpr u32 R_AARCH64_MOVW_UABS_G3 = 0x10d;
|
||||
static constexpr u32 R_AARCH64_MOVW_SABS_G0 = 0x10e;
|
||||
static constexpr u32 R_AARCH64_MOVW_SABS_G1 = 0x10f;
|
||||
static constexpr u32 R_AARCH64_MOVW_SABS_G2 = 0x110;
|
||||
static constexpr u32 R_AARCH64_LD_PREL_LO19 = 0x111;
|
||||
static constexpr u32 R_AARCH64_ADR_PREL_LO21 = 0x112;
|
||||
static constexpr u32 R_AARCH64_ADR_PREL_PG_HI21 = 0x113;
|
||||
static constexpr u32 R_AARCH64_ADR_PREL_PG_HI21_NC = 0x114;
|
||||
static constexpr u32 R_AARCH64_ADD_ABS_LO12_NC = 0x115;
|
||||
static constexpr u32 R_AARCH64_LDST8_ABS_LO12_NC = 0x116;
|
||||
static constexpr u32 R_AARCH64_TSTBR14 = 0x117;
|
||||
static constexpr u32 R_AARCH64_CONDBR19 = 0x118;
|
||||
static constexpr u32 R_AARCH64_JUMP26 = 0x11a;
|
||||
static constexpr u32 R_AARCH64_CALL26 = 0x11b;
|
||||
static constexpr u32 R_AARCH64_LDST16_ABS_LO12_NC = 0x11c;
|
||||
static constexpr u32 R_AARCH64_LDST32_ABS_LO12_NC = 0x11d;
|
||||
static constexpr u32 R_AARCH64_LDST64_ABS_LO12_NC = 0x11e;
|
||||
static constexpr u32 R_AARCH64_MOVW_PREL_G0 = 0x11f;
|
||||
static constexpr u32 R_AARCH64_MOVW_PREL_G0_NC = 0x120;
|
||||
static constexpr u32 R_AARCH64_MOVW_PREL_G1 = 0x121;
|
||||
static constexpr u32 R_AARCH64_MOVW_PREL_G1_NC = 0x122;
|
||||
static constexpr u32 R_AARCH64_MOVW_PREL_G2 = 0x123;
|
||||
static constexpr u32 R_AARCH64_MOVW_PREL_G2_NC = 0x124;
|
||||
static constexpr u32 R_AARCH64_MOVW_PREL_G3 = 0x125;
|
||||
static constexpr u32 R_AARCH64_LDST128_ABS_LO12_NC = 0x12b;
|
||||
static constexpr u32 R_AARCH64_ADR_GOT_PAGE = 0x137;
|
||||
static constexpr u32 R_AARCH64_LD64_GOT_LO12_NC = 0x138;
|
||||
static constexpr u32 R_AARCH64_LD64_GOTPAGE_LO15 = 0x139;
|
||||
static constexpr u32 R_AARCH64_PLT32 = 0x13a;
|
||||
static constexpr u32 R_AARCH64_TLSGD_ADR_PREL21 = 0x200;
|
||||
static constexpr u32 R_AARCH64_TLSGD_ADR_PAGE21 = 0x201;
|
||||
static constexpr u32 R_AARCH64_TLSGD_ADD_LO12_NC = 0x202;
|
||||
static constexpr u32 R_AARCH64_TLSGD_MOVW_G1 = 0x203;
|
||||
static constexpr u32 R_AARCH64_TLSGD_MOVW_G0_NC = 0x204;
|
||||
static constexpr u32 R_AARCH64_TLSLD_ADR_PREL21 = 0x205;
|
||||
static constexpr u32 R_AARCH64_TLSLD_ADR_PAGE21 = 0x206;
|
||||
static constexpr u32 R_AARCH64_TLSLD_ADD_LO12_NC = 0x207;
|
||||
static constexpr u32 R_AARCH64_TLSLD_MOVW_G1 = 0x208;
|
||||
static constexpr u32 R_AARCH64_TLSLD_MOVW_G0_NC = 0x209;
|
||||
static constexpr u32 R_AARCH64_TLSLD_LD_PREL19 = 0x20a;
|
||||
static constexpr u32 R_AARCH64_TLSLD_MOVW_DTPREL_G2 = 0x20b;
|
||||
static constexpr u32 R_AARCH64_TLSLD_MOVW_DTPREL_G1 = 0x20c;
|
||||
static constexpr u32 R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC = 0x20d;
|
||||
static constexpr u32 R_AARCH64_TLSLD_MOVW_DTPREL_G0 = 0x20e;
|
||||
static constexpr u32 R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC = 0x20f;
|
||||
static constexpr u32 R_AARCH64_TLSLD_ADD_DTPREL_HI12 = 0x210;
|
||||
static constexpr u32 R_AARCH64_TLSLD_ADD_DTPREL_LO12 = 0x211;
|
||||
static constexpr u32 R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC = 0x212;
|
||||
static constexpr u32 R_AARCH64_TLSLD_LDST8_DTPREL_LO12 = 0x213;
|
||||
static constexpr u32 R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC = 0x214;
|
||||
static constexpr u32 R_AARCH64_TLSLD_LDST16_DTPREL_LO12 = 0x215;
|
||||
static constexpr u32 R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC = 0x216;
|
||||
static constexpr u32 R_AARCH64_TLSLD_LDST32_DTPREL_LO12 = 0x217;
|
||||
static constexpr u32 R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC = 0x218;
|
||||
static constexpr u32 R_AARCH64_TLSLD_LDST64_DTPREL_LO12 = 0x219;
|
||||
static constexpr u32 R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC = 0x21a;
|
||||
static constexpr u32 R_AARCH64_TLSIE_MOVW_GOTTPREL_G1 = 0x21b;
|
||||
static constexpr u32 R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC = 0x21c;
|
||||
static constexpr u32 R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 = 0x21d;
|
||||
static constexpr u32 R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC = 0x21e;
|
||||
static constexpr u32 R_AARCH64_TLSIE_LD_GOTTPREL_PREL19 = 0x21f;
|
||||
static constexpr u32 R_AARCH64_TLSLE_MOVW_TPREL_G2 = 0x220;
|
||||
static constexpr u32 R_AARCH64_TLSLE_MOVW_TPREL_G1 = 0x221;
|
||||
static constexpr u32 R_AARCH64_TLSLE_MOVW_TPREL_G1_NC = 0x222;
|
||||
static constexpr u32 R_AARCH64_TLSLE_MOVW_TPREL_G0 = 0x223;
|
||||
static constexpr u32 R_AARCH64_TLSLE_MOVW_TPREL_G0_NC = 0x224;
|
||||
static constexpr u32 R_AARCH64_TLSLE_ADD_TPREL_HI12 = 0x225;
|
||||
static constexpr u32 R_AARCH64_TLSLE_ADD_TPREL_LO12 = 0x226;
|
||||
static constexpr u32 R_AARCH64_TLSLE_ADD_TPREL_LO12_NC = 0x227;
|
||||
static constexpr u32 R_AARCH64_TLSLE_LDST8_TPREL_LO12 = 0x228;
|
||||
static constexpr u32 R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC = 0x229;
|
||||
static constexpr u32 R_AARCH64_TLSLE_LDST16_TPREL_LO12 = 0x22a;
|
||||
static constexpr u32 R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC = 0x22b;
|
||||
static constexpr u32 R_AARCH64_TLSLE_LDST32_TPREL_LO12 = 0x22c;
|
||||
static constexpr u32 R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC = 0x22d;
|
||||
static constexpr u32 R_AARCH64_TLSLE_LDST64_TPREL_LO12 = 0x22e;
|
||||
static constexpr u32 R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC = 0x22f;
|
||||
static constexpr u32 R_AARCH64_TLSDESC_ADR_PAGE21 = 0x232;
|
||||
static constexpr u32 R_AARCH64_TLSDESC_LD64_LO12 = 0x233;
|
||||
static constexpr u32 R_AARCH64_TLSDESC_ADD_LO12 = 0x234;
|
||||
static constexpr u32 R_AARCH64_TLSDESC_CALL = 0x239;
|
||||
static constexpr u32 R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC = 0x23b;
|
||||
static constexpr u32 R_AARCH64_COPY = 0x400;
|
||||
static constexpr u32 R_AARCH64_GLOB_DAT = 0x401;
|
||||
static constexpr u32 R_AARCH64_JUMP_SLOT = 0x402;
|
||||
static constexpr u32 R_AARCH64_RELATIVE = 0x403;
|
||||
static constexpr u32 R_AARCH64_TLS_DTPMOD64 = 0x404;
|
||||
static constexpr u32 R_AARCH64_TLS_DTPREL64 = 0x405;
|
||||
static constexpr u32 R_AARCH64_TLS_TPREL64 = 0x406;
|
||||
static constexpr u32 R_AARCH64_TLSDESC = 0x407;
|
||||
static constexpr u32 R_AARCH64_IRELATIVE = 0x408;
|
||||
|
||||
template <>
|
||||
inline std::string rel_to_string<ARM64>(u32 r_type) {
|
||||
switch (r_type) {
|
||||
case R_AARCH64_NONE: return "R_AARCH64_NONE";
|
||||
case R_AARCH64_ABS64: return "R_AARCH64_ABS64";
|
||||
case R_AARCH64_ABS32: return "R_AARCH64_ABS32";
|
||||
case R_AARCH64_ABS16: return "R_AARCH64_ABS16";
|
||||
case R_AARCH64_PREL64: return "R_AARCH64_PREL64";
|
||||
case R_AARCH64_PREL32: return "R_AARCH64_PREL32";
|
||||
case R_AARCH64_PREL16: return "R_AARCH64_PREL16";
|
||||
case R_AARCH64_MOVW_UABS_G0: return "R_AARCH64_MOVW_UABS_G0";
|
||||
case R_AARCH64_MOVW_UABS_G0_NC: return "R_AARCH64_MOVW_UABS_G0_NC";
|
||||
case R_AARCH64_MOVW_UABS_G1: return "R_AARCH64_MOVW_UABS_G1";
|
||||
case R_AARCH64_MOVW_UABS_G1_NC: return "R_AARCH64_MOVW_UABS_G1_NC";
|
||||
case R_AARCH64_MOVW_UABS_G2: return "R_AARCH64_MOVW_UABS_G2";
|
||||
case R_AARCH64_MOVW_UABS_G2_NC: return "R_AARCH64_MOVW_UABS_G2_NC";
|
||||
case R_AARCH64_MOVW_UABS_G3: return "R_AARCH64_MOVW_UABS_G3";
|
||||
case R_AARCH64_MOVW_SABS_G0: return "R_AARCH64_MOVW_SABS_G0";
|
||||
case R_AARCH64_MOVW_SABS_G1: return "R_AARCH64_MOVW_SABS_G1";
|
||||
case R_AARCH64_MOVW_SABS_G2: return "R_AARCH64_MOVW_SABS_G2";
|
||||
case R_AARCH64_LD_PREL_LO19: return "R_AARCH64_LD_PREL_LO19";
|
||||
case R_AARCH64_ADR_PREL_LO21: return "R_AARCH64_ADR_PREL_LO21";
|
||||
case R_AARCH64_ADR_PREL_PG_HI21: return "R_AARCH64_ADR_PREL_PG_HI21";
|
||||
case R_AARCH64_ADR_PREL_PG_HI21_NC: return "R_AARCH64_ADR_PREL_PG_HI21_NC";
|
||||
case R_AARCH64_ADD_ABS_LO12_NC: return "R_AARCH64_ADD_ABS_LO12_NC";
|
||||
case R_AARCH64_LDST8_ABS_LO12_NC: return "R_AARCH64_LDST8_ABS_LO12_NC";
|
||||
case R_AARCH64_TSTBR14: return "R_AARCH64_TSTBR14";
|
||||
case R_AARCH64_CONDBR19: return "R_AARCH64_CONDBR19";
|
||||
case R_AARCH64_JUMP26: return "R_AARCH64_JUMP26";
|
||||
case R_AARCH64_CALL26: return "R_AARCH64_CALL26";
|
||||
case R_AARCH64_LDST16_ABS_LO12_NC: return "R_AARCH64_LDST16_ABS_LO12_NC";
|
||||
case R_AARCH64_LDST32_ABS_LO12_NC: return "R_AARCH64_LDST32_ABS_LO12_NC";
|
||||
case R_AARCH64_LDST64_ABS_LO12_NC: return "R_AARCH64_LDST64_ABS_LO12_NC";
|
||||
case R_AARCH64_MOVW_PREL_G0: return "R_AARCH64_MOVW_PREL_G0";
|
||||
case R_AARCH64_MOVW_PREL_G0_NC: return "R_AARCH64_MOVW_PREL_G0_NC";
|
||||
case R_AARCH64_MOVW_PREL_G1: return "R_AARCH64_MOVW_PREL_G1";
|
||||
case R_AARCH64_MOVW_PREL_G1_NC: return "R_AARCH64_MOVW_PREL_G1_NC";
|
||||
case R_AARCH64_MOVW_PREL_G2: return "R_AARCH64_MOVW_PREL_G2";
|
||||
case R_AARCH64_MOVW_PREL_G2_NC: return "R_AARCH64_MOVW_PREL_G2_NC";
|
||||
case R_AARCH64_MOVW_PREL_G3: return "R_AARCH64_MOVW_PREL_G3";
|
||||
case R_AARCH64_LDST128_ABS_LO12_NC: return "R_AARCH64_LDST128_ABS_LO12_NC";
|
||||
case R_AARCH64_ADR_GOT_PAGE: return "R_AARCH64_ADR_GOT_PAGE";
|
||||
case R_AARCH64_LD64_GOT_LO12_NC: return "R_AARCH64_LD64_GOT_LO12_NC";
|
||||
case R_AARCH64_LD64_GOTPAGE_LO15: return "R_AARCH64_LD64_GOTPAGE_LO15";
|
||||
case R_AARCH64_PLT32: return "R_AARCH64_PLT32";
|
||||
case R_AARCH64_TLSGD_ADR_PREL21: return "R_AARCH64_TLSGD_ADR_PREL21";
|
||||
case R_AARCH64_TLSGD_ADR_PAGE21: return "R_AARCH64_TLSGD_ADR_PAGE21";
|
||||
case R_AARCH64_TLSGD_ADD_LO12_NC: return "R_AARCH64_TLSGD_ADD_LO12_NC";
|
||||
case R_AARCH64_TLSGD_MOVW_G1: return "R_AARCH64_TLSGD_MOVW_G1";
|
||||
case R_AARCH64_TLSGD_MOVW_G0_NC: return "R_AARCH64_TLSGD_MOVW_G0_NC";
|
||||
case R_AARCH64_TLSLD_ADR_PREL21: return "R_AARCH64_TLSLD_ADR_PREL21";
|
||||
case R_AARCH64_TLSLD_ADR_PAGE21: return "R_AARCH64_TLSLD_ADR_PAGE21";
|
||||
case R_AARCH64_TLSLD_ADD_LO12_NC: return "R_AARCH64_TLSLD_ADD_LO12_NC";
|
||||
case R_AARCH64_TLSLD_MOVW_G1: return "R_AARCH64_TLSLD_MOVW_G1";
|
||||
case R_AARCH64_TLSLD_MOVW_G0_NC: return "R_AARCH64_TLSLD_MOVW_G0_NC";
|
||||
case R_AARCH64_TLSLD_LD_PREL19: return "R_AARCH64_TLSLD_LD_PREL19";
|
||||
case R_AARCH64_TLSLD_MOVW_DTPREL_G2: return "R_AARCH64_TLSLD_MOVW_DTPREL_G2";
|
||||
case R_AARCH64_TLSLD_MOVW_DTPREL_G1: return "R_AARCH64_TLSLD_MOVW_DTPREL_G1";
|
||||
case R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC: return "R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC";
|
||||
case R_AARCH64_TLSLD_MOVW_DTPREL_G0: return "R_AARCH64_TLSLD_MOVW_DTPREL_G0";
|
||||
case R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC: return "R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC";
|
||||
case R_AARCH64_TLSLD_ADD_DTPREL_HI12: return "R_AARCH64_TLSLD_ADD_DTPREL_HI12";
|
||||
case R_AARCH64_TLSLD_ADD_DTPREL_LO12: return "R_AARCH64_TLSLD_ADD_DTPREL_LO12";
|
||||
case R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: return "R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC";
|
||||
case R_AARCH64_TLSLD_LDST8_DTPREL_LO12: return "R_AARCH64_TLSLD_LDST8_DTPREL_LO12";
|
||||
case R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC: return "R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC";
|
||||
case R_AARCH64_TLSLD_LDST16_DTPREL_LO12: return "R_AARCH64_TLSLD_LDST16_DTPREL_LO12";
|
||||
case R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC: return "R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC";
|
||||
case R_AARCH64_TLSLD_LDST32_DTPREL_LO12: return "R_AARCH64_TLSLD_LDST32_DTPREL_LO12";
|
||||
case R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC: return "R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC";
|
||||
case R_AARCH64_TLSLD_LDST64_DTPREL_LO12: return "R_AARCH64_TLSLD_LDST64_DTPREL_LO12";
|
||||
case R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC: return "R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC";
|
||||
case R_AARCH64_TLSIE_MOVW_GOTTPREL_G1: return "R_AARCH64_TLSIE_MOVW_GOTTPREL_G1";
|
||||
case R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC: return "R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC";
|
||||
case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: return "R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21";
|
||||
case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: return "R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC";
|
||||
case R_AARCH64_TLSIE_LD_GOTTPREL_PREL19: return "R_AARCH64_TLSIE_LD_GOTTPREL_PREL19";
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G2: return "R_AARCH64_TLSLE_MOVW_TPREL_G2";
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G1: return "R_AARCH64_TLSLE_MOVW_TPREL_G1";
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC: return "R_AARCH64_TLSLE_MOVW_TPREL_G1_NC";
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G0: return "R_AARCH64_TLSLE_MOVW_TPREL_G0";
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC: return "R_AARCH64_TLSLE_MOVW_TPREL_G0_NC";
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_HI12: return "R_AARCH64_TLSLE_ADD_TPREL_HI12";
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_LO12: return "R_AARCH64_TLSLE_ADD_TPREL_LO12";
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: return "R_AARCH64_TLSLE_ADD_TPREL_LO12_NC";
|
||||
case R_AARCH64_TLSLE_LDST8_TPREL_LO12: return "R_AARCH64_TLSLE_LDST8_TPREL_LO12";
|
||||
case R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC: return "R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC";
|
||||
case R_AARCH64_TLSLE_LDST16_TPREL_LO12: return "R_AARCH64_TLSLE_LDST16_TPREL_LO12";
|
||||
case R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC: return "R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC";
|
||||
case R_AARCH64_TLSLE_LDST32_TPREL_LO12: return "R_AARCH64_TLSLE_LDST32_TPREL_LO12";
|
||||
case R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC: return "R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC";
|
||||
case R_AARCH64_TLSLE_LDST64_TPREL_LO12: return "R_AARCH64_TLSLE_LDST64_TPREL_LO12";
|
||||
case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC: return "R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC";
|
||||
case R_AARCH64_TLSDESC_ADR_PAGE21: return "R_AARCH64_TLSDESC_ADR_PAGE21";
|
||||
case R_AARCH64_TLSDESC_LD64_LO12: return "R_AARCH64_TLSDESC_LD64_LO12";
|
||||
case R_AARCH64_TLSDESC_ADD_LO12: return "R_AARCH64_TLSDESC_ADD_LO12";
|
||||
case R_AARCH64_TLSDESC_CALL: return "R_AARCH64_TLSDESC_CALL";
|
||||
case R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC: return "R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC";
|
||||
case R_AARCH64_COPY: return "R_AARCH64_COPY";
|
||||
case R_AARCH64_GLOB_DAT: return "R_AARCH64_GLOB_DAT";
|
||||
case R_AARCH64_JUMP_SLOT: return "R_AARCH64_JUMP_SLOT";
|
||||
case R_AARCH64_RELATIVE: return "R_AARCH64_RELATIVE";
|
||||
case R_AARCH64_TLS_DTPMOD64: return "R_AARCH64_TLS_DTPMOD64";
|
||||
case R_AARCH64_TLS_DTPREL64: return "R_AARCH64_TLS_DTPREL64";
|
||||
case R_AARCH64_TLS_TPREL64: return "R_AARCH64_TLS_TPREL64";
|
||||
case R_AARCH64_TLSDESC: return "R_AARCH64_TLSDESC";
|
||||
case R_AARCH64_IRELATIVE: return "R_AARCH64_IRELATIVE";
|
||||
}
|
||||
return "unknown (" + std::to_string(r_type) + ")";
|
||||
}
|
||||
|
||||
static constexpr u32 DW_EH_PE_absptr = 0;
|
||||
static constexpr u32 DW_EH_PE_omit = 0xff;
|
||||
static constexpr u32 DW_EH_PE_uleb128 = 0x01;
|
||||
static constexpr u32 DW_EH_PE_udata2 = 0x02;
|
||||
static constexpr u32 DW_EH_PE_udata4 = 0x03;
|
||||
static constexpr u32 DW_EH_PE_udata8 = 0x04;
|
||||
static constexpr u32 DW_EH_PE_signed = 0x08;
|
||||
static constexpr u32 DW_EH_PE_sleb128 = 0x09;
|
||||
static constexpr u32 DW_EH_PE_sdata2 = 0x0a;
|
||||
static constexpr u32 DW_EH_PE_sdata4 = 0x0b;
|
||||
static constexpr u32 DW_EH_PE_sdata8 = 0x0c;
|
||||
static constexpr u32 DW_EH_PE_pcrel = 0x10;
|
||||
static constexpr u32 DW_EH_PE_textrel = 0x20;
|
||||
static constexpr u32 DW_EH_PE_datarel = 0x30;
|
||||
static constexpr u32 DW_EH_PE_funcrel = 0x40;
|
||||
static constexpr u32 DW_EH_PE_aligned = 0x50;
|
||||
|
||||
struct Elf64Sym {
|
||||
bool is_defined() const { return !is_undef(); }
|
||||
bool is_undef() const { return st_shndx == SHN_UNDEF; }
|
||||
bool is_abs() const { return st_shndx == SHN_ABS; }
|
||||
bool is_common() const { return st_shndx == SHN_COMMON; }
|
||||
bool is_weak() const { return st_bind == STB_WEAK; }
|
||||
|
||||
bool is_undef_strong() const {
|
||||
return st_shndx == SHN_UNDEF && st_bind != STB_WEAK;
|
||||
}
|
||||
|
||||
bool is_undef_weak() const {
|
||||
return st_shndx == SHN_UNDEF && st_bind == STB_WEAK;
|
||||
}
|
||||
|
||||
u32 st_name;
|
||||
u8 st_type : 4;
|
||||
u8 st_bind : 4;
|
||||
u8 st_visibility : 2;
|
||||
u16 st_shndx;
|
||||
u64 st_value;
|
||||
u64 st_size;
|
||||
};
|
||||
|
||||
struct Elf32Sym {
|
||||
bool is_defined() const { return !is_undef(); }
|
||||
bool is_undef() const { return st_shndx == SHN_UNDEF; }
|
||||
bool is_abs() const { return st_shndx == SHN_ABS; }
|
||||
bool is_common() const { return st_shndx == SHN_COMMON; }
|
||||
bool is_weak() const { return st_bind == STB_WEAK; }
|
||||
|
||||
bool is_undef_strong() const {
|
||||
return st_shndx == SHN_UNDEF && st_bind != STB_WEAK;
|
||||
}
|
||||
|
||||
bool is_undef_weak() const {
|
||||
return st_shndx == SHN_UNDEF && st_bind == STB_WEAK;
|
||||
}
|
||||
|
||||
u32 st_name;
|
||||
u32 st_value;
|
||||
u32 st_size;
|
||||
u8 st_type : 4;
|
||||
u8 st_bind : 4;
|
||||
u8 st_visibility : 2;
|
||||
u16 st_shndx;
|
||||
};
|
||||
|
||||
struct Elf64Shdr {
|
||||
u32 sh_name;
|
||||
u32 sh_type;
|
||||
u64 sh_flags;
|
||||
u64 sh_addr;
|
||||
u64 sh_offset;
|
||||
u64 sh_size;
|
||||
u32 sh_link;
|
||||
u32 sh_info;
|
||||
u64 sh_addralign;
|
||||
u64 sh_entsize;
|
||||
};
|
||||
|
||||
struct Elf32Shdr {
|
||||
u32 sh_name;
|
||||
u32 sh_type;
|
||||
u32 sh_flags;
|
||||
u32 sh_addr;
|
||||
u32 sh_offset;
|
||||
u32 sh_size;
|
||||
u32 sh_link;
|
||||
u32 sh_info;
|
||||
u32 sh_addralign;
|
||||
u32 sh_entsize;
|
||||
};
|
||||
|
||||
struct Elf64Ehdr {
|
||||
u8 e_ident[16];
|
||||
u16 e_type;
|
||||
u16 e_machine;
|
||||
u32 e_version;
|
||||
u64 e_entry;
|
||||
u64 e_phoff;
|
||||
u64 e_shoff;
|
||||
u32 e_flags;
|
||||
u16 e_ehsize;
|
||||
u16 e_phentsize;
|
||||
u16 e_phnum;
|
||||
u16 e_shentsize;
|
||||
u16 e_shnum;
|
||||
u16 e_shstrndx;
|
||||
};
|
||||
|
||||
struct Elf32Ehdr {
|
||||
u8 e_ident[16];
|
||||
u16 e_type;
|
||||
u16 e_machine;
|
||||
u32 e_version;
|
||||
u32 e_entry;
|
||||
u32 e_phoff;
|
||||
u32 e_shoff;
|
||||
u32 e_flags;
|
||||
u16 e_ehsize;
|
||||
u16 e_phentsize;
|
||||
u16 e_phnum;
|
||||
u16 e_shentsize;
|
||||
u16 e_shnum;
|
||||
u16 e_shstrndx;
|
||||
};
|
||||
|
||||
struct Elf64Phdr {
|
||||
u32 p_type;
|
||||
u32 p_flags;
|
||||
u64 p_offset;
|
||||
u64 p_vaddr;
|
||||
u64 p_paddr;
|
||||
u64 p_filesz;
|
||||
u64 p_memsz;
|
||||
u64 p_align;
|
||||
};
|
||||
|
||||
struct Elf32Phdr {
|
||||
u32 p_type;
|
||||
u32 p_offset;
|
||||
u32 p_vaddr;
|
||||
u32 p_paddr;
|
||||
u32 p_filesz;
|
||||
u32 p_memsz;
|
||||
u32 p_flags;
|
||||
u32 p_align;
|
||||
};
|
||||
|
||||
struct Elf64Rel {
|
||||
u64 r_offset;
|
||||
u32 r_type;
|
||||
u32 r_sym;
|
||||
};
|
||||
|
||||
struct Elf32Rel {
|
||||
u32 r_offset;
|
||||
u32 r_type : 8;
|
||||
u32 r_sym : 24;
|
||||
};
|
||||
|
||||
struct Elf64Rela {
|
||||
u64 r_offset;
|
||||
u32 r_type;
|
||||
u32 r_sym;
|
||||
i64 r_addend;
|
||||
};
|
||||
|
||||
struct Elf32Rela {
|
||||
u32 r_offset;
|
||||
u32 r_type : 8;
|
||||
u32 r_sym : 24;
|
||||
i32 r_addend;
|
||||
};
|
||||
|
||||
struct Elf64Dyn {
|
||||
u64 d_tag;
|
||||
u64 d_val;
|
||||
};
|
||||
|
||||
struct Elf32Dyn {
|
||||
u32 d_tag;
|
||||
u32 d_val;
|
||||
};
|
||||
|
||||
struct ElfVerneed {
|
||||
u16 vn_version;
|
||||
u16 vn_cnt;
|
||||
u32 vn_file;
|
||||
u32 vn_aux;
|
||||
u32 vn_next;
|
||||
};
|
||||
|
||||
struct ElfVernaux {
|
||||
u32 vna_hash;
|
||||
u16 vna_flags;
|
||||
u16 vna_other;
|
||||
u32 vna_name;
|
||||
u32 vna_next;
|
||||
};
|
||||
|
||||
struct ElfVerdef {
|
||||
u16 vd_version;
|
||||
u16 vd_flags;
|
||||
u16 vd_ndx;
|
||||
u16 vd_cnt;
|
||||
u32 vd_hash;
|
||||
u32 vd_aux;
|
||||
u32 vd_next;
|
||||
};
|
||||
|
||||
struct ElfVerdaux {
|
||||
u32 vda_name;
|
||||
u32 vda_next;
|
||||
};
|
||||
|
||||
struct Elf64Chdr {
|
||||
u32 ch_type;
|
||||
u32 ch_reserved;
|
||||
u64 ch_size;
|
||||
u64 ch_addralign;
|
||||
};
|
||||
|
||||
struct Elf32Chdr {
|
||||
u32 ch_type;
|
||||
u32 ch_size;
|
||||
u32 ch_addralign;
|
||||
};
|
||||
|
||||
struct ElfNhdr {
|
||||
u32 n_namesz;
|
||||
u32 n_descsz;
|
||||
u32 n_type;
|
||||
};
|
||||
|
||||
struct X86_64 {
|
||||
using WordTy = u64;
|
||||
|
||||
static constexpr u32 R_NONE = R_X86_64_NONE;
|
||||
static constexpr u32 R_COPY = R_X86_64_COPY;
|
||||
static constexpr u32 R_GLOB_DAT = R_X86_64_GLOB_DAT;
|
||||
static constexpr u32 R_JUMP_SLOT = R_X86_64_JUMP_SLOT;
|
||||
static constexpr u32 R_RELATIVE = R_X86_64_RELATIVE;
|
||||
static constexpr u32 R_IRELATIVE = R_X86_64_IRELATIVE;
|
||||
static constexpr u32 R_DTPOFF = R_X86_64_DTPOFF64;
|
||||
static constexpr u32 R_TPOFF = R_X86_64_TPOFF64;
|
||||
static constexpr u32 R_DTPMOD = R_X86_64_DTPMOD64;
|
||||
static constexpr u32 R_TLSDESC = R_X86_64_TLSDESC;
|
||||
|
||||
static constexpr u32 word_size = 8;
|
||||
static constexpr u32 page_size = 4096;
|
||||
static constexpr u32 e_machine = EM_X86_64;
|
||||
static constexpr u32 pltgot_size = 8;
|
||||
static constexpr bool is_rel = false;
|
||||
static constexpr bool is_le = true;
|
||||
};
|
||||
|
||||
template <> struct ElfSym<X86_64> : public Elf64Sym {};
|
||||
template <> struct ElfShdr<X86_64> : public Elf64Shdr {};
|
||||
template <> struct ElfEhdr<X86_64> : public Elf64Ehdr {};
|
||||
template <> struct ElfPhdr<X86_64> : public Elf64Phdr {};
|
||||
template <> struct ElfRel<X86_64> : public Elf64Rela {};
|
||||
template <> struct ElfDyn<X86_64> : public Elf64Dyn {};
|
||||
template <> struct ElfChdr<X86_64> : public Elf64Chdr {};
|
||||
|
||||
struct I386 {
|
||||
using WordTy = u32;
|
||||
|
||||
static constexpr u32 R_NONE = R_386_NONE;
|
||||
static constexpr u32 R_COPY = R_386_COPY;
|
||||
static constexpr u32 R_GLOB_DAT = R_386_GLOB_DAT;
|
||||
static constexpr u32 R_JUMP_SLOT = R_386_JUMP_SLOT;
|
||||
static constexpr u32 R_RELATIVE = R_386_RELATIVE;
|
||||
static constexpr u32 R_IRELATIVE = R_386_IRELATIVE;
|
||||
static constexpr u32 R_DTPOFF = R_386_TLS_DTPOFF32;
|
||||
static constexpr u32 R_TPOFF = R_386_TLS_TPOFF;
|
||||
static constexpr u32 R_DTPMOD = R_386_TLS_DTPMOD32;
|
||||
static constexpr u32 R_TLSDESC = R_386_TLS_DESC;
|
||||
|
||||
static constexpr u32 word_size = 4;
|
||||
static constexpr u32 page_size = 4096;
|
||||
static constexpr u32 e_machine = EM_386;
|
||||
static constexpr u32 pltgot_size = 8;
|
||||
static constexpr bool is_rel = true;
|
||||
static constexpr bool is_le = true;
|
||||
};
|
||||
|
||||
template <> struct ElfSym<I386> : public Elf32Sym {};
|
||||
template <> struct ElfShdr<I386> : public Elf32Shdr {};
|
||||
template <> struct ElfEhdr<I386> : public Elf32Ehdr {};
|
||||
template <> struct ElfPhdr<I386> : public Elf32Phdr {};
|
||||
template <> struct ElfRel<I386> : public Elf32Rel {};
|
||||
template <> struct ElfDyn<I386> : public Elf32Dyn {};
|
||||
template <> struct ElfChdr<I386> : public Elf32Chdr {};
|
||||
|
||||
struct ARM64 {
|
||||
using WordTy = u64;
|
||||
|
||||
static constexpr u32 R_NONE = R_AARCH64_NONE;
|
||||
static constexpr u32 R_COPY = R_AARCH64_COPY;
|
||||
static constexpr u32 R_GLOB_DAT = R_AARCH64_GLOB_DAT;
|
||||
static constexpr u32 R_JUMP_SLOT = R_AARCH64_JUMP_SLOT;
|
||||
static constexpr u32 R_RELATIVE = R_AARCH64_RELATIVE;
|
||||
static constexpr u32 R_IRELATIVE = R_AARCH64_IRELATIVE;
|
||||
static constexpr u32 R_DTPOFF = R_AARCH64_TLS_DTPREL64;
|
||||
static constexpr u32 R_TPOFF = R_AARCH64_TLS_TPREL64;
|
||||
static constexpr u32 R_DTPMOD = R_AARCH64_TLS_DTPMOD64;
|
||||
static constexpr u32 R_TLSDESC = R_AARCH64_TLSDESC;
|
||||
|
||||
static constexpr u32 word_size = 8;
|
||||
static constexpr u32 page_size = 65536;
|
||||
static constexpr u32 e_machine = EM_AARCH64;
|
||||
static constexpr u32 pltgot_size = 16;
|
||||
static constexpr bool is_rel = false;
|
||||
static constexpr bool is_le = true;
|
||||
};
|
||||
|
||||
template <> struct ElfSym<ARM64> : public Elf64Sym {};
|
||||
template <> struct ElfShdr<ARM64> : public Elf64Shdr {};
|
||||
template <> struct ElfEhdr<ARM64> : public Elf64Ehdr {};
|
||||
template <> struct ElfPhdr<ARM64> : public Elf64Phdr {};
|
||||
template <> struct ElfRel<ARM64> : public Elf64Rela {};
|
||||
template <> struct ElfDyn<ARM64> : public Elf64Dyn {};
|
||||
template <> struct ElfChdr<ARM64> : public Elf64Chdr {};
|
||||
|
||||
} // namespace mold::elf
|
||||
@@ -7,33 +7,65 @@
|
||||
#include <tbb/concurrent_vector.h>
|
||||
#include <tbb/parallel_for_each.h>
|
||||
|
||||
namespace mold {
|
||||
namespace mold::elf {
|
||||
|
||||
template <typename E>
|
||||
static bool should_keep(const InputSection<E> &isec) {
|
||||
u32 type = isec.shdr().sh_type;
|
||||
u32 flags = isec.shdr().sh_flags;
|
||||
std::string_view name = isec.name();
|
||||
|
||||
if constexpr (is_ppc32<E>)
|
||||
if (name == ".got2")
|
||||
return true;
|
||||
|
||||
return (flags & SHF_GNU_RETAIN) ||
|
||||
type == SHT_NOTE ||
|
||||
type == SHT_INIT_ARRAY ||
|
||||
type == SHT_FINI_ARRAY ||
|
||||
type == SHT_PREINIT_ARRAY ||
|
||||
name.starts_with(".ctors") ||
|
||||
name.starts_with(".dtors") ||
|
||||
name.starts_with(".init") ||
|
||||
name.starts_with(".fini") ||
|
||||
is_c_identifier(name);
|
||||
static bool is_init_fini(const InputSection<E> &isec) {
|
||||
return isec.shdr.sh_type == SHT_INIT_ARRAY ||
|
||||
isec.shdr.sh_type == SHT_FINI_ARRAY ||
|
||||
isec.shdr.sh_type == SHT_PREINIT_ARRAY ||
|
||||
isec.name().starts_with(".ctors") ||
|
||||
isec.name().starts_with(".dtors") ||
|
||||
isec.name().starts_with(".init") ||
|
||||
isec.name().starts_with(".fini");
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static bool mark_section(InputSection<E> *isec) {
|
||||
return isec && isec->is_alive && !isec->is_visited.test_and_set();
|
||||
return isec && isec->is_alive && !isec->is_visited.exchange(true);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void visit(Context<E> &ctx, InputSection<E> *isec,
|
||||
tbb::feeder<InputSection<E> *> &feeder, i64 depth) {
|
||||
assert(isec->is_visited);
|
||||
|
||||
// A relocation can refer either a section fragment (i.e. a piece of
|
||||
// string in a mergeable string section) or a symbol. Mark all
|
||||
// section fragments as alive.
|
||||
if (SectionFragmentRef<E> *refs = isec->rel_fragments.get())
|
||||
for (i64 i = 0; refs[i].idx >= 0; i++)
|
||||
refs[i].frag->is_alive.store(true, std::memory_order_relaxed);
|
||||
|
||||
// If this is a text section, .eh_frame may contain records
|
||||
// describing how to handle exceptions for that function.
|
||||
// We want to keep associated .eh_frame records.
|
||||
for (FdeRecord<E> &fde : isec->get_fdes())
|
||||
for (ElfRel<E> &rel : fde.get_rels().subspan(1))
|
||||
if (Symbol<E> *sym = isec->file.symbols[rel.r_sym])
|
||||
if (mark_section(sym->input_section))
|
||||
feeder.add(sym->input_section);
|
||||
|
||||
for (ElfRel<E> &rel : isec->get_rels(ctx)) {
|
||||
Symbol<E> &sym = *isec->file.symbols[rel.r_sym];
|
||||
|
||||
// Symbol can refer either a section fragment or an input section.
|
||||
// Mark a fragment as alive.
|
||||
if (SectionFragment<E> *frag = sym.get_frag()) {
|
||||
frag->is_alive.store(true, std::memory_order_relaxed);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!mark_section(sym.input_section))
|
||||
continue;
|
||||
|
||||
// Mark a section alive. For better performacne, we don't call
|
||||
// `feeder.add` too often.
|
||||
if (depth < 3)
|
||||
visit(ctx, sym.input_section, feeder, depth + 1);
|
||||
else
|
||||
feeder.add(sym.input_section);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
@@ -50,9 +82,9 @@ collect_root_set(Context<E> &ctx) {
|
||||
auto enqueue_symbol = [&](Symbol<E> *sym) {
|
||||
if (sym) {
|
||||
if (SectionFragment<E> *frag = sym->get_frag())
|
||||
frag->is_alive = true;
|
||||
frag->is_alive.store(true, std::memory_order_relaxed);
|
||||
else
|
||||
enqueue_section(sym->get_input_section());
|
||||
enqueue_section(sym->input_section);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -62,75 +94,47 @@ collect_root_set(Context<E> &ctx) {
|
||||
if (!isec || !isec->is_alive)
|
||||
continue;
|
||||
|
||||
// --gc-sections discards only SHF_ALLOC sections. If you want to
|
||||
// -gc-sections discards only SHF_ALLOC sections. If you want to
|
||||
// reduce the amount of non-memory-mapped segments, you should
|
||||
// use `strip` command, compile without debug info or use
|
||||
// --strip-all linker option.
|
||||
u32 flags = isec->shdr().sh_flags;
|
||||
if (!(flags & SHF_ALLOC))
|
||||
// -strip-all linker option.
|
||||
if (!(isec->shdr.sh_flags & SHF_ALLOC))
|
||||
isec->is_visited = true;
|
||||
|
||||
if (should_keep(*isec))
|
||||
if (is_init_fini(*isec) || is_c_identifier(isec->name()) ||
|
||||
isec->shdr.sh_type == SHT_NOTE)
|
||||
enqueue_section(isec.get());
|
||||
}
|
||||
});
|
||||
|
||||
// Add sections containing gc root or exported symbols
|
||||
// Add sections containing exported symbols
|
||||
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
||||
for (Symbol<E> *sym : file->symbols)
|
||||
if (sym->file == file && (sym->gc_root || sym->is_exported))
|
||||
if (sym->file == file && sym->is_exported)
|
||||
enqueue_symbol(sym);
|
||||
});
|
||||
|
||||
// Add sections referenced by root symbols.
|
||||
enqueue_symbol(get_symbol(ctx, ctx.arg.entry));
|
||||
|
||||
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));
|
||||
|
||||
// .eh_frame consists of variable-length records called CIE and FDE
|
||||
// records, and they are a unit of inclusion or exclusion.
|
||||
// We just keep all CIEs and everything that are referenced by them.
|
||||
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
||||
for (CieRecord<E> &cie : file->cies)
|
||||
for (const ElfRel<E> &rel : cie.get_rels())
|
||||
for (ElfRel<E> &rel : cie.get_rels())
|
||||
enqueue_symbol(file->symbols[rel.r_sym]);
|
||||
});
|
||||
|
||||
return rootset;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void visit(Context<E> &ctx, InputSection<E> *isec,
|
||||
tbb::feeder<InputSection<E> *> &feeder, i64 depth) {
|
||||
assert(isec->is_visited);
|
||||
|
||||
// Mark a section alive. For better performacne, we don't call
|
||||
// `feeder.add` too often.
|
||||
auto mark = [&](InputSection<E> *sec) {
|
||||
if (mark_section(sec)) {
|
||||
if (depth < 3)
|
||||
visit(ctx, sec, feeder, depth + 1);
|
||||
else
|
||||
feeder.add(sec);
|
||||
}
|
||||
};
|
||||
|
||||
// If this is a text section, .eh_frame may contain records
|
||||
// describing how to handle exceptions for that function.
|
||||
// We want to keep associated .eh_frame records.
|
||||
for (FdeRecord<E> &fde : isec->get_fdes())
|
||||
for (const ElfRel<E> &rel : fde.get_rels(isec->file).subspan(1))
|
||||
if (Symbol<E> *sym = isec->file.symbols[rel.r_sym])
|
||||
mark(sym->get_input_section());
|
||||
|
||||
for (const ElfRel<E> &rel : isec->get_rels(ctx)) {
|
||||
// Symbol can refer to either a section fragment or an input section.
|
||||
Symbol<E> &sym = *isec->file.symbols[rel.r_sym];
|
||||
if (SectionFragment<E> *frag = sym.get_frag())
|
||||
frag->is_alive = true;
|
||||
else
|
||||
mark(sym.get_input_section());
|
||||
}
|
||||
|
||||
if constexpr (is_arm32<E>)
|
||||
mark(isec->extra.exidx);
|
||||
}
|
||||
|
||||
// Mark all reachable sections
|
||||
template <typename E>
|
||||
static void mark(Context<E> &ctx,
|
||||
@@ -138,7 +142,7 @@ static void mark(Context<E> &ctx,
|
||||
Timer t(ctx, "mark");
|
||||
|
||||
tbb::parallel_for_each(rootset, [&](InputSection<E> *isec,
|
||||
tbb::feeder<InputSection<E> *> &feeder) {
|
||||
tbb::feeder<InputSection<E> *> &feeder) {
|
||||
visit(ctx, isec, feeder, 0);
|
||||
});
|
||||
}
|
||||
@@ -153,7 +157,7 @@ static void sweep(Context<E> &ctx) {
|
||||
for (std::unique_ptr<InputSection<E>> &isec : file->sections) {
|
||||
if (isec && isec->is_alive && !isec->is_visited) {
|
||||
if (ctx.arg.print_gc_sections)
|
||||
Out(ctx) << "removing unused section " << *isec;
|
||||
SyncOut(ctx) << "removing unused section " << *isec;
|
||||
isec->kill();
|
||||
counter++;
|
||||
}
|
||||
@@ -161,16 +165,37 @@ static void sweep(Context<E> &ctx) {
|
||||
});
|
||||
}
|
||||
|
||||
// Non-alloc section fragments are not subject of garbage collection.
|
||||
// This function marks such fragments.
|
||||
template <typename E>
|
||||
static void mark_nonalloc_fragments(Context<E> &ctx) {
|
||||
Timer t(ctx, "mark_nonalloc_fragments");
|
||||
|
||||
tbb::parallel_for_each(ctx.objs, [](ObjectFile<E> *file) {
|
||||
for (std::unique_ptr<MergeableSection<E>> &m : file->mergeable_sections)
|
||||
if (m)
|
||||
for (SectionFragment<E> *frag : m->fragments)
|
||||
if (!(frag->output_section.shdr.sh_flags & SHF_ALLOC))
|
||||
frag->is_alive.store(true, std::memory_order_relaxed);
|
||||
});
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void gc_sections(Context<E> &ctx) {
|
||||
Timer t(ctx, "gc");
|
||||
|
||||
mark_nonalloc_fragments(ctx);
|
||||
|
||||
tbb::concurrent_vector<InputSection<E> *> rootset = collect_root_set(ctx);
|
||||
mark(ctx, rootset);
|
||||
sweep(ctx);
|
||||
}
|
||||
|
||||
using E = MOLD_TARGET;
|
||||
#define INSTANTIATE(E) \
|
||||
template void gc_sections(Context<E> &ctx);
|
||||
|
||||
template void gc_sections(Context<E> &ctx);
|
||||
INSTANTIATE(X86_64);
|
||||
INSTANTIATE(I386);
|
||||
INSTANTIATE(ARM64);
|
||||
|
||||
} // namespace mold
|
||||
} // namespace mold::elf
|
||||
+21
-26
@@ -1,10 +1,10 @@
|
||||
#include "common.h"
|
||||
#include "mold.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace mold {
|
||||
namespace mold::elf {
|
||||
|
||||
std::optional<Glob> Glob::compile(std::string_view pat) {
|
||||
std::optional<GlobPattern> GlobPattern::compile(std::string_view pat) {
|
||||
std::vector<Element> vec;
|
||||
|
||||
while (!pat.empty()) {
|
||||
@@ -19,8 +19,9 @@ std::optional<Glob> Glob::compile(std::string_view pat) {
|
||||
// [$\]!]: $, ] or !
|
||||
// [a-czg-i]: a, b, c, z, g, h, or i
|
||||
// [^a-z]: Any character except lowercase letters
|
||||
vec.emplace_back(BRACKET);
|
||||
std::bitset<256> &bitset = vec.back().bitset;
|
||||
vec.push_back({BRACKET});
|
||||
std::vector<bool> &bitmap = vec.back().bitmap;
|
||||
bitmap.resize(256);
|
||||
|
||||
bool negate = false;
|
||||
if (!pat.empty() && pat[0] == '^') {
|
||||
@@ -59,9 +60,9 @@ std::optional<Glob> Glob::compile(std::string_view pat) {
|
||||
return {};
|
||||
|
||||
for (i64 i = start; i <= end; i++)
|
||||
bitset[i] = true;
|
||||
bitmap[i] = true;
|
||||
} else {
|
||||
bitset[(u8)pat[0]] = true;
|
||||
bitmap[(u8)pat[0]] = true;
|
||||
pat = pat.substr(1);
|
||||
}
|
||||
}
|
||||
@@ -70,46 +71,39 @@ std::optional<Glob> Glob::compile(std::string_view pat) {
|
||||
return {};
|
||||
|
||||
if (negate)
|
||||
bitset.flip();
|
||||
for (i64 i = 0; i < 256; i++)
|
||||
bitmap[i] = !bitmap[i];
|
||||
break;
|
||||
}
|
||||
case '?':
|
||||
vec.emplace_back(QUESTION);
|
||||
vec.push_back({QUESTION});
|
||||
break;
|
||||
case '*':
|
||||
vec.emplace_back(STAR);
|
||||
break;
|
||||
case '\\':
|
||||
if (pat.empty())
|
||||
return {};
|
||||
if (vec.empty() || vec.back().kind != STRING)
|
||||
vec.emplace_back(STRING);
|
||||
vec.back().str += pat[0];
|
||||
pat = pat.substr(1);
|
||||
vec.push_back({STAR});
|
||||
break;
|
||||
default:
|
||||
if (vec.empty() || vec.back().kind != STRING)
|
||||
vec.emplace_back(STRING);
|
||||
vec.push_back({STRING});
|
||||
vec.back().str += c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return {Glob{std::move(vec)}};
|
||||
return {GlobPattern{std::move(vec)}};
|
||||
}
|
||||
|
||||
bool Glob::match(std::string_view str) {
|
||||
bool GlobPattern::match(std::string_view str) {
|
||||
return do_match(str, elements);
|
||||
}
|
||||
|
||||
bool Glob::do_match(std::string_view str, std::span<Element> elements) {
|
||||
bool GlobPattern::do_match(std::string_view str, std::span<Element> elements) {
|
||||
while (!elements.empty()) {
|
||||
Element &e = elements[0];
|
||||
elements = elements.subspan(1);
|
||||
|
||||
switch (e.kind) {
|
||||
case STRING:
|
||||
if (!str.starts_with(e.str))
|
||||
if (str.empty() || !str.starts_with(e.str))
|
||||
return false;
|
||||
str = str.substr(e.str.size());
|
||||
break;
|
||||
@@ -124,12 +118,13 @@ bool Glob::do_match(std::string_view str, std::span<Element> elements) {
|
||||
for (;;) {
|
||||
size_t pos = str.find(elements[0].str);
|
||||
if (pos == str.npos)
|
||||
return false;
|
||||
break;
|
||||
if (do_match(str.substr(pos + elements[0].str.size()),
|
||||
elements.subspan(1)))
|
||||
return true;
|
||||
str = str.substr(pos + 1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Other cases are handled here.
|
||||
@@ -143,7 +138,7 @@ bool Glob::do_match(std::string_view str, std::span<Element> elements) {
|
||||
str = str.substr(1);
|
||||
break;
|
||||
case BRACKET:
|
||||
if (str.empty() || !e.bitset[str[0]])
|
||||
if (str.empty() || !e.bitmap[str[0]])
|
||||
return false;
|
||||
str = str.substr(1);
|
||||
break;
|
||||
@@ -153,4 +148,4 @@ bool Glob::do_match(std::string_view str, std::span<Element> elements) {
|
||||
return str.empty();
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
} // namespace mold::elf
|
||||
+144
-175
@@ -40,35 +40,21 @@
|
||||
// regular trees are equal. Given this formulation, we want to find as
|
||||
// many identical vertices as possible.
|
||||
//
|
||||
// Just like a lot of problems with graph, this problem doesn't have a
|
||||
// straightforward "optimal" solution, and we need to resort to heuristics.
|
||||
// Solving such problem is computationally intensive, but mold is quite fast.
|
||||
// For Chromium, mold's ICF finishes in less than 1 second with 20 threads.
|
||||
// This is contrary to lld and gold, which take about 5 and 50 seconds to
|
||||
// run ICF under the same condition, respectively.
|
||||
//
|
||||
// mold approaches this problem by hashing program trees with increasing depth
|
||||
// on each iteration.
|
||||
// For example, when we start, we only hash individual functions with
|
||||
// their call into other functions omitted. From the second iteration, we
|
||||
// put the function they call into the hash by appending the hash of those
|
||||
// functions from the previous iteration. This means that the nth iteration
|
||||
// hashes call chain up to (n-1) levels deep.
|
||||
// We use a cryptographic hash function, so the unique number of hashes will
|
||||
// only monotonically increase as we take into account of deeper trees with
|
||||
// iterations (otherwise, that means we have found a hash collision). We stop
|
||||
// when the unique number of hashes stop increasing; this is based on the fact
|
||||
// that once we observe an iteration with the same amount of unique hashes as
|
||||
// the previous iteration, it will remain unchanged for further iterations.
|
||||
// This is provable, but here we omit the proof for brevity.
|
||||
//
|
||||
// When compared to other approaches, mold's approach has a relatively cheaper
|
||||
// cost per iteration, and as a bonus, is highly parallelizable.
|
||||
// For Chromium, mold's ICF finishes in less than 1 second with 20 threads,
|
||||
// whereas lld takes 5 seconds and gold takes 50 seconds under the same
|
||||
// conditions.
|
||||
// mold's ICF is faster because we are using a better algorithm.
|
||||
// It's actually me who developed and implemented the lld's ICF algorithm,
|
||||
// and I can say that mold's algorithm is better than that in all aspects.
|
||||
// It scales better for number of available cores, require less overall
|
||||
// computation, and has a smaller working set. So, it's better with a single
|
||||
// thread and even better with multiple threads.
|
||||
|
||||
#include "mold.h"
|
||||
#include "../lib/siphash.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <tbb/concurrent_unordered_map.h>
|
||||
#include <tbb/concurrent_vector.h>
|
||||
#include <tbb/enumerable_thread_specific.h>
|
||||
@@ -76,66 +62,75 @@
|
||||
#include <tbb/parallel_for_each.h>
|
||||
#include <tbb/parallel_sort.h>
|
||||
|
||||
#ifdef __APPLE__
|
||||
# define COMMON_DIGEST_FOR_OPENSSL
|
||||
# include <CommonCrypto/CommonDigest.h>
|
||||
#else
|
||||
# include <openssl/sha.h>
|
||||
#endif
|
||||
|
||||
static constexpr int64_t HASH_SIZE = 16;
|
||||
|
||||
using Digest = std::array<uint8_t, HASH_SIZE>;
|
||||
typedef std::array<uint8_t, HASH_SIZE> Digest;
|
||||
|
||||
namespace std {
|
||||
template <> struct hash<Digest> {
|
||||
template<> struct hash<Digest> {
|
||||
size_t operator()(const Digest &k) const {
|
||||
static_assert(sizeof(size_t) <= HASH_SIZE);
|
||||
size_t val;
|
||||
memcpy(&val, k.data(), sizeof(size_t));
|
||||
return val;
|
||||
return *(int64_t *)&k[0];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace mold {
|
||||
|
||||
static u8 hmac_key[16];
|
||||
namespace mold::elf {
|
||||
|
||||
template <typename E>
|
||||
static void uniquify_cies(Context<E> &ctx) {
|
||||
Timer t(ctx, "uniquify_cies");
|
||||
std::vector<CieRecord<E> *> cies;
|
||||
|
||||
auto find = [&](CieRecord<E> &cie) -> i64 {
|
||||
for (i64 i = 0; i < cies.size(); i++)
|
||||
if (cie_equals(cie, *cies[i]))
|
||||
return i;
|
||||
return -1;
|
||||
};
|
||||
|
||||
for (ObjectFile<E> *file : ctx.objs) {
|
||||
for (CieRecord<E> &cie : file->cies) {
|
||||
if (i64 idx = find(cie); idx != -1) {
|
||||
cie.icf_idx = idx;
|
||||
} else {
|
||||
cie.icf_idx = cies.size();
|
||||
cies.push_back(&cie);
|
||||
for (i64 i = 0; i < cies.size(); i++) {
|
||||
if (cie.equals(*cies[i])) {
|
||||
cie.icf_idx = i;
|
||||
goto found;
|
||||
}
|
||||
}
|
||||
cie.icf_idx = cies.size();
|
||||
cies.push_back(&cie);
|
||||
found:;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static bool is_eligible(Context<E> &ctx, InputSection<E> &isec) {
|
||||
const ElfShdr<E> &shdr = isec.shdr();
|
||||
static bool is_eligible(InputSection<E> &isec) {
|
||||
const ElfShdr<E> &shdr = isec.shdr;
|
||||
std::string_view name = isec.name();
|
||||
|
||||
if (shdr.sh_size == 0 || !(shdr.sh_flags & SHF_ALLOC) ||
|
||||
shdr.sh_type == SHT_NOBITS || is_c_identifier(name))
|
||||
return false;
|
||||
bool is_alloc = (shdr.sh_flags & SHF_ALLOC);
|
||||
bool is_executable = (shdr.sh_flags & SHF_EXECINSTR);
|
||||
bool is_relro = (name == ".data.rel.ro" ||
|
||||
name.starts_with(".data.rel.ro."));
|
||||
bool is_readonly = !(shdr.sh_flags & SHF_WRITE) || is_relro;
|
||||
bool is_bss = (shdr.sh_type == SHT_NOBITS);
|
||||
bool is_empty = (shdr.sh_size == 0);
|
||||
bool is_init = (shdr.sh_type == SHT_INIT_ARRAY || name == ".init");
|
||||
bool is_fini = (shdr.sh_type == SHT_FINI_ARRAY || name == ".fini");
|
||||
bool is_enumerable = is_c_identifier(name);
|
||||
|
||||
if (shdr.sh_flags & SHF_EXECINSTR)
|
||||
return (ctx.arg.icf_all || !isec.address_taken) &&
|
||||
name != ".init" && name != ".fini";
|
||||
return is_alloc && is_executable && is_readonly && !is_bss &&
|
||||
!is_empty && !is_init && !is_fini && !is_enumerable;
|
||||
}
|
||||
|
||||
bool is_readonly = !(shdr.sh_flags & SHF_WRITE);
|
||||
bool is_relro = isec.name().starts_with(".data.rel.ro");
|
||||
return (ctx.arg.ignore_data_address_equality || !isec.address_taken) &&
|
||||
(is_readonly || is_relro);
|
||||
static Digest digest_final(SHA256_CTX &sha) {
|
||||
u8 buf[SHA256_SIZE];
|
||||
int res = SHA256_Final(buf, &sha);
|
||||
assert(res == 1);
|
||||
|
||||
Digest digest;
|
||||
memcpy(digest.data(), buf, HASH_SIZE);
|
||||
return digest;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
@@ -144,18 +139,22 @@ static bool is_leaf(Context<E> &ctx, InputSection<E> &isec) {
|
||||
return false;
|
||||
|
||||
for (FdeRecord<E> &fde : isec.get_fdes())
|
||||
if (fde.get_rels(isec.file).size() > 1)
|
||||
if (fde.get_rels().size() > 1)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static u64 combine_hash(u64 a, u64 b) {
|
||||
return a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2));
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
struct LeafHasher {
|
||||
size_t operator()(InputSection<E> *isec) const {
|
||||
size_t operator()(const InputSection<E> *isec) const {
|
||||
u64 h = hash_string(isec->contents);
|
||||
for (FdeRecord<E> &fde : isec->get_fdes()) {
|
||||
u64 h2 = hash_string(fde.get_contents(isec->file).substr(8));
|
||||
u64 h2 = hash_string(fde.get_contents().substr(8));
|
||||
h = combine_hash(h, h2);
|
||||
}
|
||||
return h;
|
||||
@@ -164,7 +163,7 @@ struct LeafHasher {
|
||||
|
||||
template <typename E>
|
||||
struct LeafEq {
|
||||
bool operator()(InputSection<E> *a, InputSection<E> *b) const {
|
||||
bool operator()(const InputSection<E> *a, const InputSection<E> *b) const {
|
||||
if (a->contents != b->contents)
|
||||
return false;
|
||||
|
||||
@@ -175,15 +174,12 @@ struct LeafEq {
|
||||
return false;
|
||||
|
||||
for (i64 i = 0; i < x.size(); i++)
|
||||
if (x[i].get_contents(a->file).substr(8) !=
|
||||
y[i].get_contents(b->file).substr(8))
|
||||
if (x[i].get_contents().substr(8) != y[i].get_contents().substr(8))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// Early merge of leaf nodes, which can be processed without constructing the
|
||||
// entire graph. This reduces the vertex count and improves memory efficiency.
|
||||
template <typename E>
|
||||
static void merge_leaf_nodes(Context<E> &ctx) {
|
||||
Timer t(ctx, "merge_leaf_nodes");
|
||||
@@ -192,7 +188,7 @@ static void merge_leaf_nodes(Context<E> &ctx) {
|
||||
static Counter non_eligible("icf_non_eligibles");
|
||||
static Counter leaf("icf_leaf_nodes");
|
||||
|
||||
tbb::concurrent_unordered_map<InputSection<E> *, Atomic<InputSection<E> *>,
|
||||
tbb::concurrent_unordered_map<InputSection<E> *, InputSection<E> *,
|
||||
LeafHasher<E>, LeafEq<E>> map;
|
||||
|
||||
tbb::parallel_for((i64)0, (i64)ctx.objs.size(), [&](i64 i) {
|
||||
@@ -200,7 +196,7 @@ static void merge_leaf_nodes(Context<E> &ctx) {
|
||||
if (!isec || !isec->is_alive)
|
||||
continue;
|
||||
|
||||
if (!is_eligible(ctx, *isec)) {
|
||||
if (!is_eligible(*isec)) {
|
||||
non_eligible++;
|
||||
continue;
|
||||
}
|
||||
@@ -209,11 +205,8 @@ static void merge_leaf_nodes(Context<E> &ctx) {
|
||||
leaf++;
|
||||
isec->icf_leaf = true;
|
||||
auto [it, inserted] = map.insert({isec.get(), isec.get()});
|
||||
if (!inserted) {
|
||||
InputSection<E> *isec2 = it->second.load();
|
||||
while (isec->get_priority() < isec2->get_priority() &&
|
||||
!it->second.compare_exchange_strong(isec2, isec.get()));
|
||||
}
|
||||
if (!inserted && isec->get_priority() < it->second->get_priority())
|
||||
it->second = isec.get();
|
||||
} else {
|
||||
eligible++;
|
||||
isec->icf_eligible = true;
|
||||
@@ -234,19 +227,20 @@ static void merge_leaf_nodes(Context<E> &ctx) {
|
||||
|
||||
template <typename E>
|
||||
static Digest compute_digest(Context<E> &ctx, InputSection<E> &isec) {
|
||||
SipHash13_128 hasher(hmac_key);
|
||||
SHA256_CTX sha;
|
||||
SHA256_Init(&sha);
|
||||
|
||||
auto hash = [&](auto val) {
|
||||
hasher.update((u8 *)&val, sizeof(val));
|
||||
SHA256_Update(&sha, &val, sizeof(val));
|
||||
};
|
||||
|
||||
auto hash_string = [&](std::string_view str) {
|
||||
hash(str.size());
|
||||
hasher.update((u8 *)str.data(), str.size());
|
||||
SHA256_Update(&sha, str.data(), str.size());
|
||||
};
|
||||
|
||||
auto hash_symbol = [&](Symbol<E> &sym) {
|
||||
InputSection<E> *isec = sym.get_input_section();
|
||||
InputSection<E> *isec = sym.input_section;
|
||||
|
||||
if (!sym.file) {
|
||||
hash('1');
|
||||
@@ -269,38 +263,46 @@ static Digest compute_digest(Context<E> &ctx, InputSection<E> &isec) {
|
||||
};
|
||||
|
||||
hash_string(isec.contents);
|
||||
hash(isec.shdr().sh_flags);
|
||||
hash(isec.shdr.sh_flags);
|
||||
hash(isec.get_fdes().size());
|
||||
hash(isec.get_rels(ctx).size());
|
||||
|
||||
for (FdeRecord<E> &fde : isec.get_fdes()) {
|
||||
hash(isec.file.cies[fde.cie_idx].icf_idx);
|
||||
hash(fde.cie->icf_idx);
|
||||
|
||||
// Bytes 0 to 4 contain the length of this record, and
|
||||
// bytes 4 to 8 contain an offset to CIE.
|
||||
hash_string(fde.get_contents(isec.file).substr(8));
|
||||
hash_string(fde.get_contents().substr(8));
|
||||
|
||||
hash(fde.get_rels(isec.file).size());
|
||||
hash(fde.get_rels().size());
|
||||
|
||||
for (const ElfRel<E> &rel : fde.get_rels(isec.file).subspan(1)) {
|
||||
for (ElfRel<E> &rel : fde.get_rels().subspan(1)) {
|
||||
hash_symbol(*isec.file.symbols[rel.r_sym]);
|
||||
hash(rel.r_type);
|
||||
hash(rel.r_offset - fde.input_offset);
|
||||
hash(get_addend(isec.file.cies[fde.cie_idx].input_section, rel));
|
||||
hash(fde.cie->input_section.get_addend(rel));
|
||||
}
|
||||
}
|
||||
|
||||
i64 frag_idx = 0;
|
||||
|
||||
for (i64 i = 0; i < isec.get_rels(ctx).size(); i++) {
|
||||
const ElfRel<E> &rel = isec.get_rels(ctx)[i];
|
||||
ElfRel<E> &rel = isec.get_rels(ctx)[i];
|
||||
hash(rel.r_offset);
|
||||
hash(rel.r_type);
|
||||
hash(get_addend(isec, rel));
|
||||
hash_symbol(*isec.file.symbols[rel.r_sym]);
|
||||
hash(isec.get_addend(rel));
|
||||
|
||||
if (isec.rel_fragments && isec.rel_fragments[frag_idx].idx == i) {
|
||||
SectionFragmentRef<E> &ref = isec.rel_fragments[frag_idx++];
|
||||
hash('a');
|
||||
isec.get_addend(rel);
|
||||
hash((u64)ref.frag);
|
||||
} else {
|
||||
hash_symbol(*isec.file.symbols[rel.r_sym]);
|
||||
}
|
||||
}
|
||||
|
||||
Digest digest;
|
||||
hasher.finish(digest.data());
|
||||
return digest;
|
||||
return digest_final(sha);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
@@ -350,9 +352,6 @@ compute_digests(Context<E> &ctx, std::span<InputSection<E> *> sections) {
|
||||
return digests;
|
||||
}
|
||||
|
||||
// Build a graph, treating every function as a vertex and every function call
|
||||
// as an edge. See the description at the top for a more detailed formulation.
|
||||
// We use u32 indices here to improve cache locality.
|
||||
template <typename E>
|
||||
static void gather_edges(Context<E> &ctx,
|
||||
std::span<InputSection<E> *> sections,
|
||||
@@ -360,23 +359,24 @@ static void gather_edges(Context<E> &ctx,
|
||||
std::vector<u32> &edge_indices) {
|
||||
Timer t(ctx, "gather_edges");
|
||||
|
||||
if (sections.empty())
|
||||
return;
|
||||
|
||||
std::vector<i64> num_edges(sections.size());
|
||||
edge_indices.resize(sections.size());
|
||||
|
||||
tbb::parallel_for((i64)0, (i64)sections.size(), [&](i64 i) {
|
||||
InputSection<E> &isec = *sections[i];
|
||||
assert(isec.icf_eligible);
|
||||
i64 frag_idx = 0;
|
||||
|
||||
for (i64 j = 0; j < isec.get_rels(ctx).size(); j++) {
|
||||
const ElfRel<E> &rel = isec.get_rels(ctx)[j];
|
||||
Symbol<E> &sym = *isec.file.symbols[rel.r_sym];
|
||||
if (!sym.get_frag())
|
||||
if (InputSection<E> *isec = sym.get_input_section())
|
||||
if (isec->icf_eligible)
|
||||
num_edges[i]++;
|
||||
if (isec.rel_fragments && isec.rel_fragments[frag_idx].idx == j) {
|
||||
frag_idx++;
|
||||
} else {
|
||||
ElfRel<E> &rel = isec.get_rels(ctx)[j];
|
||||
Symbol<E> &sym = *isec.file.symbols[rel.r_sym];
|
||||
if (!sym.get_frag() && sym.input_section &&
|
||||
sym.input_section->icf_eligible)
|
||||
num_edges[i]++;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -387,22 +387,26 @@ static void gather_edges(Context<E> &ctx,
|
||||
|
||||
tbb::parallel_for((i64)0, (i64)num_edges.size(), [&](i64 i) {
|
||||
InputSection<E> &isec = *sections[i];
|
||||
i64 frag_idx = 0;
|
||||
i64 idx = edge_indices[i];
|
||||
|
||||
for (ElfRel<E> &rel : isec.get_rels(ctx)) {
|
||||
Symbol<E> &sym = *isec.file.symbols[rel.r_sym];
|
||||
if (InputSection<E> *isec = sym.get_input_section())
|
||||
if (isec->icf_eligible)
|
||||
edges[idx++] = isec->icf_idx;
|
||||
}
|
||||
for (i64 j = 0; j < isec.get_rels(ctx).size(); j++) {
|
||||
if (isec.rel_fragments && isec.rel_fragments[frag_idx].idx == j) {
|
||||
frag_idx++;
|
||||
ElfRel<E> &rel = isec.get_rels(ctx)[j];
|
||||
Symbol<E> &sym = *isec.file.symbols[rel.r_sym];
|
||||
if (!sym.get_frag() && sym.input_section &&
|
||||
sym.input_section->icf_eligible)
|
||||
edges[idx++] = sym.input_section->icf_idx;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static i64 propagate(std::span<std::vector<Digest>> digests,
|
||||
std::span<u32> edges, std::span<u32> edge_indices,
|
||||
bool &slot, std::span<u8> converged,
|
||||
tbb::affinity_partitioner &ap) {
|
||||
bool &slot, tbb::affinity_partitioner &ap) {
|
||||
static Counter round("icf_round");
|
||||
round++;
|
||||
|
||||
@@ -410,27 +414,23 @@ static i64 propagate(std::span<std::vector<Digest>> digests,
|
||||
tbb::enumerable_thread_specific<i64> changed;
|
||||
|
||||
tbb::parallel_for((i64)0, num_digests, [&](i64 i) {
|
||||
if (converged[i])
|
||||
if (digests[slot][i] == digests[!slot][i])
|
||||
return;
|
||||
|
||||
SipHash13_128 hasher(hmac_key);
|
||||
hasher.update(digests[2][i].data(), HASH_SIZE);
|
||||
SHA256_CTX sha;
|
||||
SHA256_Init(&sha);
|
||||
SHA256_Update(&sha, digests[2][i].data(), HASH_SIZE);
|
||||
|
||||
i64 begin = edge_indices[i];
|
||||
i64 end = (i + 1 == num_digests) ? edges.size() : edge_indices[i + 1];
|
||||
|
||||
for (i64 j : edges.subspan(begin, end - begin))
|
||||
hasher.update(digests[slot][j].data(), HASH_SIZE);
|
||||
SHA256_Update(&sha, digests[slot][j].data(), HASH_SIZE);
|
||||
|
||||
hasher.finish(digests[!slot][i].data());
|
||||
digests[!slot][i] = digest_final(sha);
|
||||
|
||||
if (digests[slot][i] == digests[!slot][i]) {
|
||||
// This node has converged. Skip further iterations as it will
|
||||
// yield the same hash.
|
||||
converged[i] = true;
|
||||
} else {
|
||||
if (digests[slot][i] != digests[!slot][i])
|
||||
changed.local()++;
|
||||
}
|
||||
}, ap);
|
||||
|
||||
slot = !slot;
|
||||
@@ -468,7 +468,7 @@ static void print_icf_sections(Context<E> &ctx) {
|
||||
});
|
||||
|
||||
tbb::parallel_sort(leaders.begin(), leaders.end(),
|
||||
[](InputSection<E> *a, InputSection<E> *b) {
|
||||
[&](InputSection<E> *a, InputSection<E> *b) {
|
||||
return a->get_priority() < b->get_priority();
|
||||
});
|
||||
|
||||
@@ -479,26 +479,22 @@ static void print_icf_sections(Context<E> &ctx) {
|
||||
if (begin == end)
|
||||
continue;
|
||||
|
||||
Out(ctx) << "selected section " << *leader;
|
||||
SyncOut(ctx) << "selected section " << *leader;
|
||||
|
||||
i64 n = 0;
|
||||
for (auto it = begin; it != end; it++) {
|
||||
Out(ctx) << " removing identical section " << *it->second;
|
||||
SyncOut(ctx) << " removing identical section " << *it->second;
|
||||
n++;
|
||||
}
|
||||
saved_bytes += leader->contents.size() * n;
|
||||
}
|
||||
|
||||
Out(ctx) << "ICF saved " << saved_bytes << " bytes";
|
||||
SyncOut(ctx) << "ICF saved " << saved_bytes << " bytes";
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void icf_sections(Context<E> &ctx) {
|
||||
Timer t(ctx, "icf");
|
||||
if (ctx.objs.empty())
|
||||
return;
|
||||
|
||||
get_random_bytes(hmac_key, sizeof(hmac_key));
|
||||
|
||||
uniquify_cies(ctx);
|
||||
merge_leaf_nodes(ctx);
|
||||
@@ -506,15 +502,6 @@ void icf_sections(Context<E> &ctx) {
|
||||
// Prepare for the propagation rounds.
|
||||
std::vector<InputSection<E> *> sections = gather_sections(ctx);
|
||||
|
||||
// We allocate 3 arrays to store hashes for each vertex.
|
||||
//
|
||||
// Index 0 and 1 are used for tree hashes from the previous
|
||||
// iteration and the current iteration. They switch roles every
|
||||
// iteration. See `slot` below.
|
||||
//
|
||||
// Index 2 stores the initial, single-vertex hash. This is combined
|
||||
// with hashes from the connected vertices to form the tree hash
|
||||
// described above.
|
||||
std::vector<std::vector<Digest>> digests(3);
|
||||
digests[0] = compute_digests<E>(ctx, sections);
|
||||
digests[1].resize(digests[0].size());
|
||||
@@ -524,7 +511,6 @@ void icf_sections(Context<E> &ctx) {
|
||||
std::vector<u32> edge_indices;
|
||||
gather_edges<E>(ctx, sections, edges, edge_indices);
|
||||
|
||||
std::vector<u8> converged(digests[0].size());
|
||||
bool slot = 0;
|
||||
|
||||
// Execute the propagation rounds until convergence is obtained.
|
||||
@@ -532,33 +518,18 @@ void icf_sections(Context<E> &ctx) {
|
||||
Timer t(ctx, "propagate");
|
||||
tbb::affinity_partitioner ap;
|
||||
|
||||
// A cheap test that the graph hasn't converged yet.
|
||||
// The loop after this one uses a strict condition, but it's expensive
|
||||
// as it requires sorting the entire hash collection.
|
||||
//
|
||||
// For nodes that have a cycle in downstream (i.e. recursive
|
||||
// functions and functions that calls recursive functions) will always
|
||||
// change with the iterations. Nodes that doesn't (i.e. non-recursive
|
||||
// functions) will stop changing as soon as the propagation depth reaches
|
||||
// the call tree depth.
|
||||
// Here, we test whether we have reached sufficient depth for the latter,
|
||||
// which is a necessary (but not sufficient) condition for convergence.
|
||||
i64 num_changed = -1;
|
||||
for (;;) {
|
||||
i64 n = propagate<E>(digests, edges, edge_indices, slot, converged, ap);
|
||||
i64 n = propagate<E>(digests, edges, edge_indices, slot, ap);
|
||||
if (n == num_changed)
|
||||
break;
|
||||
num_changed = n;
|
||||
}
|
||||
|
||||
// Run the pass until the unique number of hashes stop increasing, at which
|
||||
// point we have achieved convergence (proof omitted for brevity).
|
||||
i64 num_classes = -1;
|
||||
for (;;) {
|
||||
// count_num_classes requires sorting which is O(n log n), so do a little
|
||||
// more work beforehand to amortize that log factor.
|
||||
for (i64 i = 0; i < 10; i++)
|
||||
propagate<E>(digests, edges, edge_indices, slot, converged, ap);
|
||||
propagate<E>(digests, edges, edge_indices, slot, ap);
|
||||
|
||||
i64 n = count_num_classes<E>(digests[slot], ap);
|
||||
if (n == num_classes)
|
||||
@@ -567,23 +538,18 @@ void icf_sections(Context<E> &ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
// Group sections by hash values.
|
||||
// Group sections by SHA digest.
|
||||
{
|
||||
Timer t(ctx, "group");
|
||||
|
||||
auto *map =
|
||||
new tbb::concurrent_unordered_map<Digest, Atomic<InputSection<E> *>>;
|
||||
|
||||
auto *map = new tbb::concurrent_unordered_map<Digest, InputSection<E> *>;
|
||||
std::span<Digest> digest = digests[slot];
|
||||
|
||||
tbb::parallel_for((i64)0, (i64)sections.size(), [&](i64 i) {
|
||||
InputSection<E> *isec = sections[i];
|
||||
auto [it, inserted] = map->insert({digest[i], isec});
|
||||
if (!inserted) {
|
||||
InputSection<E> *isec2 = it->second.load();
|
||||
while (isec->get_priority() < isec2->get_priority() &&
|
||||
!it->second.compare_exchange_strong(isec2, isec));
|
||||
}
|
||||
if (!inserted && isec->get_priority() < it->second->get_priority())
|
||||
it->second = isec;
|
||||
});
|
||||
|
||||
tbb::parallel_for((i64)0, (i64)sections.size(), [&](i64 i) {
|
||||
@@ -593,31 +559,34 @@ void icf_sections(Context<E> &ctx) {
|
||||
});
|
||||
|
||||
// Since free'ing the map is slow, postpone it.
|
||||
ctx.on_exit.push_back([=] { delete map; });
|
||||
ctx.on_exit.push_back([=]() { delete map; });
|
||||
}
|
||||
|
||||
if (ctx.arg.print_icf_sections)
|
||||
print_icf_sections(ctx);
|
||||
|
||||
// Eliminate duplicate sections.
|
||||
// Symbols pointing to eliminated sections will be redirected on the fly when
|
||||
// exporting to the symtab.
|
||||
// Re-assign input sections to symbols.
|
||||
{
|
||||
Timer t(ctx, "sweep");
|
||||
static Counter eliminated("icf_eliminated");
|
||||
Timer t(ctx, "reassign");
|
||||
tbb::parallel_for_each(ctx.objs, [](ObjectFile<E> *file) {
|
||||
for (std::unique_ptr<InputSection<E>> &isec : file->sections) {
|
||||
if (isec && isec->is_alive && isec->icf_removed()) {
|
||||
for (Symbol<E> *sym : file->symbols) {
|
||||
if (sym->file != file)
|
||||
continue;
|
||||
InputSection<E> *isec = sym->input_section;
|
||||
if (isec && isec->leader && isec->leader != isec) {
|
||||
sym->input_section = isec->leader;
|
||||
isec->kill();
|
||||
eliminated++;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
using E = MOLD_TARGET;
|
||||
#define INSTANTIATE(E) \
|
||||
template void icf_sections(Context<E> &ctx);
|
||||
|
||||
template void icf_sections(Context<E> &ctx);
|
||||
INSTANTIATE(X86_64);
|
||||
INSTANTIATE(I386);
|
||||
INSTANTIATE(ARM64);
|
||||
|
||||
} // namespace mold
|
||||
} // namespace mold::elf
|
||||
+1394
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,162 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace mold::elf {
|
||||
|
||||
template <typename E>
|
||||
bool CieRecord<E>::equals(const CieRecord<E> &other) const {
|
||||
if (get_contents() != other.get_contents())
|
||||
return false;
|
||||
|
||||
std::span<ElfRel<E>> x = get_rels();
|
||||
std::span<ElfRel<E>> y = other.get_rels();
|
||||
if (x.size() != y.size())
|
||||
return false;
|
||||
|
||||
for (i64 i = 0; i < x.size(); i++) {
|
||||
if (x[i].r_offset - input_offset != y[i].r_offset - other.input_offset ||
|
||||
x[i].r_type != y[i].r_type ||
|
||||
file.symbols[x[i].r_sym] != other.file.symbols[y[i].r_sym] ||
|
||||
input_section.get_addend(x[i]) != other.input_section.get_addend(y[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
InputSection<E>::InputSection(Context<E> &ctx, ObjectFile<E> &file,
|
||||
const ElfShdr<E> &shdr, std::string_view name,
|
||||
std::string_view contents, i64 section_idx)
|
||||
: file(file), shdr(shdr), contents(contents), nameptr(name.data()),
|
||||
namelen(name.size()), section_idx(section_idx) {
|
||||
output_section =
|
||||
OutputSection<E>::get_instance(ctx, name, shdr.sh_type, shdr.sh_flags);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void InputSection<E>::write_to(Context<E> &ctx, u8 *buf) {
|
||||
if (shdr.sh_type == SHT_NOBITS || shdr.sh_size == 0)
|
||||
return;
|
||||
|
||||
// Copy data
|
||||
memcpy(buf, contents.data(), contents.size());
|
||||
|
||||
// Apply relocations
|
||||
if (shdr.sh_flags & SHF_ALLOC)
|
||||
apply_reloc_alloc(ctx, buf);
|
||||
else
|
||||
apply_reloc_nonalloc(ctx, buf);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static i64 get_output_type(Context<E> &ctx) {
|
||||
if (ctx.arg.shared)
|
||||
return 0;
|
||||
if (ctx.arg.pie)
|
||||
return 1;
|
||||
return 2;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static i64 get_sym_type(Symbol<E> &sym) {
|
||||
if (sym.is_absolute())
|
||||
return 0;
|
||||
if (!sym.is_imported)
|
||||
return 1;
|
||||
if (sym.get_type() != STT_FUNC)
|
||||
return 2;
|
||||
return 3;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void InputSection<E>::dispatch(Context<E> &ctx, Action table[3][4], i64 i,
|
||||
const ElfRel<E> &rel, Symbol<E> &sym) {
|
||||
Action action = table[get_output_type(ctx)][get_sym_type(sym)];
|
||||
bool is_code = (shdr.sh_flags & SHF_EXECINSTR);
|
||||
bool is_writable = (shdr.sh_flags & SHF_WRITE);
|
||||
|
||||
auto error = [&]() {
|
||||
Error(ctx) << *this << ": " << rel << " relocation against symbol `"
|
||||
<< sym << "' can not be used; recompile with -fPIC";
|
||||
};
|
||||
|
||||
switch (action) {
|
||||
case NONE:
|
||||
return;
|
||||
case ERROR:
|
||||
error();
|
||||
return;
|
||||
case COPYREL:
|
||||
if (!ctx.arg.z_copyreloc) {
|
||||
error();
|
||||
return;
|
||||
}
|
||||
|
||||
if (sym.esym().st_visibility == STV_PROTECTED) {
|
||||
Error(ctx) << *this
|
||||
<< ": cannot make copy relocation for protected symbol '"
|
||||
<< sym << "', defined in " << *sym.file
|
||||
<< "; recompile with -fPIC";
|
||||
return;
|
||||
}
|
||||
|
||||
sym.flags |= NEEDS_COPYREL;
|
||||
return;
|
||||
case PLT:
|
||||
sym.flags |= NEEDS_PLT;
|
||||
return;
|
||||
case DYNREL:
|
||||
if (!is_writable) {
|
||||
if (!is_code || ctx.arg.z_text) {
|
||||
error();
|
||||
return;
|
||||
}
|
||||
ctx.has_textrel = true;
|
||||
}
|
||||
|
||||
assert(sym.is_imported);
|
||||
needs_dynrel[i] = true;
|
||||
file.num_dynrel++;
|
||||
return;
|
||||
case BASEREL:
|
||||
if (!is_writable) {
|
||||
if (!is_code || ctx.arg.z_text) {
|
||||
error();
|
||||
return;
|
||||
}
|
||||
ctx.has_textrel = true;
|
||||
}
|
||||
|
||||
needs_baserel[i] = true;
|
||||
if (!is_relr_reloc(ctx, rel))
|
||||
file.num_dynrel++;
|
||||
return;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void InputSection<E>::report_undef(Context<E> &ctx, Symbol<E> &sym) {
|
||||
switch (ctx.arg.unresolved_symbols) {
|
||||
case UNRESOLVED_ERROR:
|
||||
Error(ctx) << "undefined symbol: " << file << ": " << sym;
|
||||
break;
|
||||
case UNRESOLVED_WARN:
|
||||
Warn(ctx) << "undefined symbol: " << file << ": " << sym;
|
||||
break;
|
||||
case UNRESOLVED_IGNORE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template struct CieRecord<E>; \
|
||||
template class InputSection<E>;
|
||||
|
||||
INSTANTIATE(X86_64);
|
||||
INSTANTIATE(I386);
|
||||
INSTANTIATE(ARM64);
|
||||
|
||||
} // namespace mold::elf
|
||||
@@ -0,0 +1,412 @@
|
||||
// On Linux, /usr/lib/x86_64-linux-gnu/libc.so is not actually
|
||||
// a shared object file but an ASCII text file containing a linker
|
||||
// script to include a "real" libc.so file. Therefore, we need to
|
||||
// support a (very limited) subset of the linker script language.
|
||||
|
||||
#include "mold.h"
|
||||
|
||||
#include <cctype>
|
||||
#include <iomanip>
|
||||
|
||||
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);
|
||||
|
||||
static std::string_view get_line(std::string_view input, const char *pos) {
|
||||
assert(input.data() <= pos);
|
||||
assert(pos < input.data() + input.size());
|
||||
|
||||
i64 start = input.rfind('\n', pos - input.data());
|
||||
if (start == input.npos)
|
||||
start = 0;
|
||||
else
|
||||
start++;
|
||||
|
||||
i64 end = input.find('\n', pos - input.data());
|
||||
if (end == input.npos)
|
||||
end = input.size();
|
||||
|
||||
return input.substr(start, end - start);
|
||||
}
|
||||
|
||||
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 line = get_line(contents, errpos.data());
|
||||
|
||||
i64 lineno = 1;
|
||||
for (i64 i = 0; contents.data() + i < line.data(); i++)
|
||||
if (contents[i] == '\n')
|
||||
lineno++;
|
||||
|
||||
i64 column = errpos.data() - line.data();
|
||||
|
||||
std::stringstream ss;
|
||||
ss << current_file<E>->name << ":" << lineno << ": ";
|
||||
i64 indent = (i64)ss.tellp() + strlen("mold: ");
|
||||
ss << line << "\n" << std::setw(indent + column) << " " << "^ ";
|
||||
out << ss.str();
|
||||
}
|
||||
|
||||
template <class T> SyntaxError &operator<<(T &&val) {
|
||||
out << std::forward<T>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[noreturn]] ~SyntaxError() = default;
|
||||
|
||||
Fatal<Context<E>> out;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
static std::vector<std::string_view>
|
||||
tokenize(Context<E> &ctx, std::string_view input) {
|
||||
std::vector<std::string_view> vec;
|
||||
while (!input.empty()) {
|
||||
if (isspace(input[0])) {
|
||||
input = input.substr(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (input.starts_with("/*")) {
|
||||
i64 pos = input.find("*/", 2);
|
||||
if (pos == std::string_view::npos)
|
||||
SyntaxError(ctx, input) << "unclosed comment";
|
||||
input = input.substr(pos + 2);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (input[0] == '#') {
|
||||
i64 pos = input.find("\n", 1);
|
||||
if (pos == std::string_view::npos)
|
||||
break;
|
||||
input = input.substr(pos + 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (input[0] == '"') {
|
||||
i64 pos = input.find('"', 1);
|
||||
if (pos == std::string_view::npos)
|
||||
SyntaxError(ctx, input) << "unclosed string literal";
|
||||
vec.push_back(input.substr(0, pos + 1));
|
||||
input = input.substr(pos + 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
i64 pos = input.find_first_not_of(
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789_.$/\\~=+[]*?-!^:");
|
||||
|
||||
if (pos == 0)
|
||||
pos = 1;
|
||||
else if (pos == input.npos)
|
||||
pos = input.size();
|
||||
|
||||
vec.push_back(input.substr(0, pos));
|
||||
input = input.substr(pos);
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
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
|
||||
<< "', but got EOF";
|
||||
if (tok[0] != str)
|
||||
SyntaxError(ctx, tok[0]) << "expected '" << str << "'";
|
||||
return tok.subspan(1);
|
||||
}
|
||||
|
||||
static std::string_view unquote(std::string_view s) {
|
||||
if (s.size() > 0 && s[0] == '"') {
|
||||
assert(s[s.size() - 1] == '"');
|
||||
return s.substr(1, s.size() - 2);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static std::span<std::string_view>
|
||||
read_output_format(Context<E> &ctx, std::span<std::string_view> tok) {
|
||||
tok = skip(ctx, tok, "(");
|
||||
while (!tok.empty() && tok[0] != ")")
|
||||
tok = tok.subspan(1);
|
||||
if (tok.empty())
|
||||
Fatal(ctx) << current_file<E>->name << ": expected ')', but got EOF";
|
||||
return tok.subspan(1);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static bool is_in_sysroot(Context<E> &ctx, std::string path) {
|
||||
std::string rel =
|
||||
to_abs_path(path).lexically_relative(to_abs_path(ctx.arg.sysroot));
|
||||
return rel != "." && !rel.starts_with("../");
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static MappedFile<Context<E>> *resolve_path(Context<E> &ctx, std::string_view tok) {
|
||||
std::string str(unquote(tok));
|
||||
|
||||
// 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))
|
||||
return MappedFile<Context<E>>::must_open(ctx, ctx.arg.sysroot + str);
|
||||
|
||||
if (str.starts_with('=')) {
|
||||
std::string path;
|
||||
if (ctx.arg.sysroot.empty())
|
||||
path = str.substr(1);
|
||||
else
|
||||
path = ctx.arg.sysroot + str.substr(1);
|
||||
return MappedFile<Context<E>>::must_open(ctx, path);
|
||||
}
|
||||
|
||||
if (str.starts_with("-l"))
|
||||
return find_library(ctx, str.substr(2));
|
||||
|
||||
if (MappedFile<Context<E>> *mf = open_library(ctx, str))
|
||||
return mf;
|
||||
|
||||
for (std::string_view dir : ctx.arg.library_paths) {
|
||||
std::string path = std::string(dir) + "/" + str;
|
||||
if (MappedFile<Context<E>> *mf = open_library(ctx, path))
|
||||
return mf;
|
||||
}
|
||||
|
||||
SyntaxError(ctx, tok) << "library not found: " << str;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static std::span<std::string_view>
|
||||
read_group(Context<E> &ctx, std::span<std::string_view> tok) {
|
||||
tok = skip(ctx, tok, "(");
|
||||
|
||||
while (!tok.empty() && tok[0] != ")") {
|
||||
if (tok[0] == "AS_NEEDED") {
|
||||
bool orig = ctx.as_needed;
|
||||
ctx.as_needed = true;
|
||||
tok = read_group(ctx, tok.subspan(1));
|
||||
ctx.as_needed = orig;
|
||||
continue;
|
||||
}
|
||||
|
||||
MappedFile<Context<E>> *mf = resolve_path(ctx, tok[0]);
|
||||
read_file(ctx, mf);
|
||||
tok = tok.subspan(1);
|
||||
}
|
||||
|
||||
if (tok.empty())
|
||||
Fatal(ctx) << current_file<E>->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;
|
||||
|
||||
std::vector<std::string_view> vec = tokenize(ctx, mf->get_contents());
|
||||
std::span<std::string_view> tok = vec;
|
||||
|
||||
while (!tok.empty()) {
|
||||
if (tok[0] == "OUTPUT_FORMAT") {
|
||||
tok = read_output_format(ctx, tok.subspan(1));
|
||||
} else if (tok[0] == "INPUT" || tok[0] == "GROUP") {
|
||||
tok = read_group(ctx, tok.subspan(1));
|
||||
} else if (tok[0] == "VERSION") {
|
||||
tok = tok.subspan(1);
|
||||
tok = skip(ctx, tok, "{");
|
||||
read_version_script(ctx, tok);
|
||||
tok = skip(ctx, tok, "}");
|
||||
} else if (tok[0] == ";") {
|
||||
tok = tok.subspan(1);
|
||||
} else {
|
||||
SyntaxError(ctx, tok[0]) << "unknown linker script token";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
i64 get_script_output_type(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
current_file<E> = mf;
|
||||
|
||||
std::vector<std::string_view> vec = tokenize(ctx, mf->get_contents());
|
||||
std::span<std::string_view> tok = vec;
|
||||
|
||||
if (tok.size() >= 3 && tok[0] == "OUTPUT_FORMAT" && tok[1] == "(") {
|
||||
if (tok[2] == "elf64-x86-64")
|
||||
return EM_X86_64;
|
||||
if (tok[2] == "elf32-i386")
|
||||
return EM_386;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static bool read_label(std::span<std::string_view> &tok,
|
||||
std::string label) {
|
||||
if (tok.size() >= 1 && tok[0] == label + ":") {
|
||||
tok = tok.subspan(1);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (tok.size() >= 2 && tok[0] == label && tok[1] == ":") {
|
||||
tok = tok.subspan(2);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void
|
||||
read_version_script_commands(Context<E> &ctx, std::span<std::string_view> &tok,
|
||||
u16 ver_idx, bool is_cpp) {
|
||||
bool is_global = true;
|
||||
|
||||
while (!tok.empty() && tok[0] != "}") {
|
||||
if (read_label(tok, "global")) {
|
||||
is_global = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (read_label(tok, "local")) {
|
||||
is_global = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tok[0] == "extern") {
|
||||
tok = tok.subspan(1);
|
||||
|
||||
if (!tok.empty() && tok[0] == "\"C\"") {
|
||||
tok = tok.subspan(1);
|
||||
tok = skip(ctx, tok, "{");
|
||||
read_version_script_commands( ctx, tok, ver_idx, false);
|
||||
} else {
|
||||
tok = skip(ctx, tok, "\"C++\"");
|
||||
tok = skip(ctx, tok, "{");
|
||||
read_version_script_commands(ctx, tok, ver_idx, true);
|
||||
}
|
||||
|
||||
tok = skip(ctx, tok, "}");
|
||||
tok = skip(ctx, tok, ";");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tok[0] == "*") {
|
||||
ctx.default_version = (is_global ? ver_idx : VER_NDX_LOCAL);
|
||||
} else if (is_global) {
|
||||
ctx.version_patterns.push_back({unquote(tok[0]), ver_idx, is_cpp});
|
||||
} else {
|
||||
ctx.version_patterns.push_back({unquote(tok[0]), VER_NDX_LOCAL, is_cpp});
|
||||
}
|
||||
|
||||
tok = tok.subspan(1);
|
||||
|
||||
if (!tok.empty() && tok[0] == "}")
|
||||
return;
|
||||
tok = skip(ctx, tok, ";");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void read_version_script(Context<E> &ctx, std::span<std::string_view> &tok) {
|
||||
u16 next_ver = VER_NDX_LAST_RESERVED + ctx.arg.version_definitions.size() + 1;
|
||||
|
||||
while (!tok.empty() && tok[0] != "}") {
|
||||
u16 ver_idx;
|
||||
if (tok[0] == "{") {
|
||||
ver_idx = VER_NDX_GLOBAL;
|
||||
} else {
|
||||
ver_idx = next_ver++;
|
||||
ctx.arg.version_definitions.push_back(std::string(tok[0]));
|
||||
tok = tok.subspan(1);
|
||||
}
|
||||
|
||||
tok = skip(ctx, tok, "{");
|
||||
read_version_script_commands(ctx, tok, ver_idx, false);
|
||||
tok = skip(ctx, tok, "}");
|
||||
if (!tok.empty() && tok[0] != ";")
|
||||
tok = tok.subspan(1);
|
||||
tok = skip(ctx, tok, ";");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void parse_version_script(Context<E> &ctx, std::string path) {
|
||||
current_file<E> = MappedFile<Context<E>>::must_open(ctx, path);
|
||||
std::vector<std::string_view> vec =
|
||||
tokenize(ctx, current_file<E>->get_contents());
|
||||
std::span<std::string_view> tok = vec;
|
||||
read_version_script(ctx, tok);
|
||||
if (!tok.empty())
|
||||
SyntaxError(ctx, tok[0]) << "trailing garbage token";
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void read_dynamic_list_commands(Context<E> &ctx, std::span<std::string_view> &tok,
|
||||
bool is_cpp) {
|
||||
while (!tok.empty() && tok[0] != "}") {
|
||||
if (tok[0] == "extern") {
|
||||
tok = tok.subspan(1);
|
||||
|
||||
if (!tok.empty() && tok[0] == "\"C\"") {
|
||||
tok = tok.subspan(1);
|
||||
tok = skip(ctx, tok, "{");
|
||||
read_dynamic_list_commands(ctx, tok, false);
|
||||
} else {
|
||||
tok = skip(ctx, tok, "\"C++\"");
|
||||
tok = skip(ctx, tok, "{");
|
||||
read_dynamic_list_commands(ctx, tok, true);
|
||||
}
|
||||
|
||||
tok = skip(ctx, tok, "}");
|
||||
tok = skip(ctx, tok, ";");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tok[0] == "*")
|
||||
ctx.default_version = VER_NDX_GLOBAL;
|
||||
else
|
||||
ctx.version_patterns.push_back({unquote(tok[0]), VER_NDX_GLOBAL, is_cpp});
|
||||
|
||||
tok = skip(ctx, tok.subspan(1), ";");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void parse_dynamic_list(Context<E> &ctx, std::string path) {
|
||||
current_file<E> = MappedFile<Context<E>>::must_open(ctx, path);
|
||||
std::vector<std::string_view> vec =
|
||||
tokenize(ctx, current_file<E>->get_contents());
|
||||
|
||||
std::span<std::string_view> tok = vec;
|
||||
tok = skip(ctx, tok, "{");
|
||||
read_dynamic_list_commands(ctx, tok, false);
|
||||
tok = skip(ctx, tok, "}");
|
||||
tok = skip(ctx, tok, ";");
|
||||
|
||||
if (!tok.empty())
|
||||
SyntaxError(ctx, tok[0]) << "trailing garbage token";
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template \
|
||||
void parse_linker_script(Context<E> &ctx, MappedFile<Context<E>> *mf); \
|
||||
template \
|
||||
i64 get_script_output_type(Context<E> &ctx, MappedFile<Context<E>> *mf); \
|
||||
template \
|
||||
void parse_version_script(Context<E> &ctx, std::string path); \
|
||||
template \
|
||||
void parse_dynamic_list(Context<E> &ctx, std::string path)
|
||||
|
||||
INSTANTIATE(X86_64);
|
||||
INSTANTIATE(I386);
|
||||
INSTANTIATE(ARM64);
|
||||
|
||||
} // namespace mold::elf
|
||||
+731
@@ -0,0 +1,731 @@
|
||||
#include "mold.h"
|
||||
#include "../archive-file.h"
|
||||
#include "../cmdline.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <iomanip>
|
||||
#include <map>
|
||||
#include <signal.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <tbb/global_control.h>
|
||||
#include <tbb/parallel_for_each.h>
|
||||
#include <unistd.h>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace mold::elf {
|
||||
|
||||
template <typename E>
|
||||
static ObjectFile<E> *new_object_file(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
std::string archive_name) {
|
||||
if (i64 type = ((ElfEhdr<E> *)mf->data)->e_machine; type != E::e_machine)
|
||||
Fatal(ctx) << mf->name << ": incompatible file type: " << type;
|
||||
|
||||
static Counter count("parsed_objs");
|
||||
count++;
|
||||
|
||||
bool in_lib = ctx.in_lib || (!archive_name.empty() && !ctx.whole_archive);
|
||||
ObjectFile<E> *file = ObjectFile<E>::create(ctx, mf, archive_name, in_lib);
|
||||
file->priority = ctx.file_priority++;
|
||||
ctx.tg.run([file, &ctx]() { file->parse(ctx); });
|
||||
if (ctx.arg.trace)
|
||||
SyncOut(ctx) << "trace: " << *file;
|
||||
return file;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static SharedFile<E> *
|
||||
new_shared_file(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
if (i64 type = ((ElfEhdr<E> *)mf->data)->e_machine; type != E::e_machine)
|
||||
Fatal(ctx) << mf->name << ": incompatible file type: " << type;
|
||||
|
||||
SharedFile<E> *file = SharedFile<E>::create(ctx, mf);
|
||||
file->priority = ctx.file_priority++;
|
||||
ctx.tg.run([file, &ctx]() { file->parse(ctx); });
|
||||
if (ctx.arg.trace)
|
||||
SyncOut(ctx) << "trace: " << *file;
|
||||
return file;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void read_file(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
if (ctx.visited.contains(mf->name))
|
||||
return;
|
||||
|
||||
FileType type = get_file_type(mf);
|
||||
switch (type) {
|
||||
case FileType::ELF_OBJ:
|
||||
ctx.objs.push_back(new_object_file(ctx, mf, ""));
|
||||
return;
|
||||
case FileType::ELF_DSO:
|
||||
ctx.dsos.push_back(new_shared_file(ctx, mf));
|
||||
ctx.visited.insert(mf->name);
|
||||
return;
|
||||
case FileType::AR:
|
||||
case FileType::THIN_AR:
|
||||
for (MappedFile<Context<E>> *child : read_archive_members(ctx, mf))
|
||||
if (get_file_type(child) == FileType::ELF_OBJ)
|
||||
ctx.objs.push_back(new_object_file(ctx, child, mf->name));
|
||||
ctx.visited.insert(mf->name);
|
||||
return;
|
||||
case FileType::TEXT:
|
||||
parse_linker_script(ctx, mf);
|
||||
return;
|
||||
case FileType::LLVM_BITCODE:
|
||||
Warn(ctx) << mf->name << ": looks like this is an LLVM bitcode, "
|
||||
<< "but mold does not support LTO";
|
||||
ctx.llvm_lto = true;
|
||||
return;
|
||||
default:
|
||||
Fatal(ctx) << mf->name << ": unknown file type: " << type;
|
||||
}
|
||||
}
|
||||
|
||||
// Read the beginning of a given file and returns its machine type
|
||||
// (e.g. EM_X86_64 or EM_386). Return -1 if unknown.
|
||||
template <typename E>
|
||||
static i64 get_machine_type(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
switch (get_file_type(mf)) {
|
||||
case FileType::ELF_OBJ:
|
||||
case FileType::ELF_DSO:
|
||||
return ((ElfEhdr<E> *)mf->data)->e_machine;
|
||||
case FileType::AR:
|
||||
for (MappedFile<Context<E>> *child : read_fat_archive_members(ctx, mf))
|
||||
if (get_file_type(child) == FileType::ELF_OBJ)
|
||||
return ((ElfEhdr<E> *)child->data)->e_machine;
|
||||
return -1;
|
||||
case FileType::THIN_AR:
|
||||
for (MappedFile<Context<E>> *child : read_thin_archive_members(ctx, mf))
|
||||
if (get_file_type(child) == FileType::ELF_OBJ)
|
||||
return ((ElfEhdr<E> *)child->data)->e_machine;
|
||||
return -1;
|
||||
case FileType::TEXT:
|
||||
return get_script_output_type(ctx, mf);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static i64
|
||||
deduce_machine_type(Context<E> &ctx, std::span<std::string_view> args) {
|
||||
for (std::string_view arg : args)
|
||||
if (!arg.starts_with('-'))
|
||||
if (auto *mf = MappedFile<Context<E>>::open(ctx, std::string(arg)))
|
||||
if (i64 type = get_machine_type(ctx, mf); type != -1)
|
||||
return type;
|
||||
Fatal(ctx) << "-m option is missing";
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
MappedFile<Context<E>> *open_library(Context<E> &ctx, std::string path) {
|
||||
MappedFile<Context<E>> *mf = MappedFile<Context<E>>::open(ctx, path);
|
||||
if (!mf)
|
||||
return nullptr;
|
||||
|
||||
i64 type = get_machine_type(ctx, mf);
|
||||
if (type == -1 || type == E::e_machine)
|
||||
return mf;
|
||||
Warn(ctx) << path << ": skipping incompatible file " << (int)type
|
||||
<< " " << (int)E::e_machine;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
MappedFile<Context<E>> *find_library(Context<E> &ctx, std::string name) {
|
||||
if (name.starts_with(':')) {
|
||||
for (std::string_view dir : ctx.arg.library_paths) {
|
||||
std::string path = std::string(dir) + "/" + name.substr(1);
|
||||
if (MappedFile<Context<E>> *mf = open_library(ctx, path))
|
||||
return mf;
|
||||
}
|
||||
Fatal(ctx) << "library not found: " << name;
|
||||
}
|
||||
|
||||
for (std::string_view dir : ctx.arg.library_paths) {
|
||||
std::string stem = std::string(dir) + "/lib" + name;
|
||||
if (!ctx.is_static)
|
||||
if (MappedFile<Context<E>> *mf = open_library(ctx, stem + ".so"))
|
||||
return mf;
|
||||
if (MappedFile<Context<E>> *mf = open_library(ctx, stem + ".a"))
|
||||
return mf;
|
||||
}
|
||||
Fatal(ctx) << "library not found: " << name;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void read_input_files(Context<E> &ctx, std::span<std::string_view> args) {
|
||||
Timer t(ctx, "read_input_files");
|
||||
|
||||
std::vector<std::tuple<bool, bool, bool, bool>> state;
|
||||
ctx.is_static = ctx.arg.is_static;
|
||||
|
||||
while (!args.empty()) {
|
||||
std::string_view arg;
|
||||
|
||||
if (read_flag(args, "as-needed")) {
|
||||
ctx.as_needed = true;
|
||||
} else if (read_flag(args, "no-as-needed")) {
|
||||
ctx.as_needed = false;
|
||||
} else if (read_flag(args, "whole-archive")) {
|
||||
ctx.whole_archive = true;
|
||||
} else if (read_flag(args, "no-whole-archive")) {
|
||||
ctx.whole_archive = false;
|
||||
} else if (read_flag(args, "Bstatic")) {
|
||||
ctx.is_static = true;
|
||||
} else if (read_flag(args, "Bdynamic")) {
|
||||
ctx.is_static = false;
|
||||
} else if (read_flag(args, "start-lib")) {
|
||||
ctx.in_lib = true;
|
||||
} else if (read_flag(args, "end-lib")) {
|
||||
ctx.in_lib = false;
|
||||
} else if (read_arg(ctx, args, arg, "version-script")) {
|
||||
parse_version_script(ctx, std::string(arg));
|
||||
} else if (read_arg(ctx, args, arg, "dynamic-list")) {
|
||||
parse_dynamic_list(ctx, std::string(arg));
|
||||
} else if (read_flag(args, "push-state")) {
|
||||
state.push_back({ctx.as_needed, ctx.whole_archive, ctx.is_static,
|
||||
ctx.in_lib});
|
||||
} else if (read_flag(args, "pop-state")) {
|
||||
if (state.empty())
|
||||
Fatal(ctx) << "no state pushed before popping";
|
||||
std::tie(ctx.as_needed, ctx.whole_archive, ctx.is_static, ctx.in_lib) =
|
||||
state.back();
|
||||
state.pop_back();
|
||||
} else if (read_arg(ctx, args, arg, "l")) {
|
||||
MappedFile<Context<E>> *mf = find_library(ctx, std::string(arg));
|
||||
mf->given_fullpath = false;
|
||||
read_file(ctx, mf);
|
||||
} else {
|
||||
read_file(ctx, MappedFile<Context<E>>::must_open(ctx, std::string(args[0])));
|
||||
args = args.subspan(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx.objs.empty())
|
||||
Fatal(ctx) << "no input files";
|
||||
|
||||
ctx.tg.wait();
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static i64 get_mtime(Context<E> &ctx, std::string path) {
|
||||
struct stat st;
|
||||
if (stat(path.c_str(), &st) < 0)
|
||||
Fatal(ctx) << path << ": stat failed: " << errno_string();
|
||||
return st.st_mtime;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static bool reload_input_files(Context<E> &ctx) {
|
||||
Timer t(ctx, "reload_input_files");
|
||||
|
||||
std::vector<ObjectFile<E> *> objs;
|
||||
std::vector<SharedFile<E> *> dsos;
|
||||
|
||||
// Reload updated .o files
|
||||
for (ObjectFile<E> *file : ctx.objs) {
|
||||
if (file->mf->parent) {
|
||||
if (get_mtime(ctx, file->mf->parent->name) != file->mf->parent->mtime)
|
||||
return false;
|
||||
objs.push_back(file);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (get_mtime(ctx, file->mf->name) == file->mf->mtime) {
|
||||
objs.push_back(file);
|
||||
continue;
|
||||
}
|
||||
|
||||
MappedFile<Context<E>> *mf =
|
||||
MappedFile<Context<E>>::must_open(ctx, file->mf->name);
|
||||
objs.push_back(new_object_file(ctx, mf, file->mf->name));
|
||||
}
|
||||
|
||||
// Reload updated .so files
|
||||
for (SharedFile<E> *file : ctx.dsos) {
|
||||
if (get_mtime(ctx, file->mf->name) == file->mf->mtime) {
|
||||
dsos.push_back(file);
|
||||
} else {
|
||||
MappedFile<Context<E>> *mf =
|
||||
MappedFile<Context<E>>::must_open(ctx, file->mf->name);
|
||||
dsos.push_back(new_shared_file(ctx, mf));
|
||||
}
|
||||
}
|
||||
|
||||
ctx.objs = objs;
|
||||
ctx.dsos = dsos;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void show_stats(Context<E> &ctx) {
|
||||
for (ObjectFile<E> *obj : ctx.objs) {
|
||||
static Counter defined("defined_syms");
|
||||
defined += obj->first_global - 1;
|
||||
|
||||
static Counter undefined("undefined_syms");
|
||||
undefined += obj->symbols.size() - obj->first_global;
|
||||
|
||||
for (std::unique_ptr<InputSection<E>> &sec : obj->sections) {
|
||||
if (!sec || !sec->is_alive)
|
||||
continue;
|
||||
|
||||
static Counter alloc("reloc_alloc");
|
||||
static Counter nonalloc("reloc_nonalloc");
|
||||
|
||||
if (sec->shdr.sh_flags & SHF_ALLOC)
|
||||
alloc += sec->get_rels(ctx).size();
|
||||
else
|
||||
nonalloc += sec->get_rels(ctx).size();
|
||||
}
|
||||
|
||||
static Counter comdats("comdats");
|
||||
comdats += obj->comdat_groups.size();
|
||||
|
||||
static Counter removed_comdats("removed_comdat_mem");
|
||||
for (auto &pair : obj->comdat_groups)
|
||||
if (ComdatGroup *group = pair.first; group->owner != obj->priority)
|
||||
removed_comdats += pair.second.size();
|
||||
|
||||
static Counter num_cies("num_cies");
|
||||
num_cies += obj->cies.size();
|
||||
|
||||
static Counter num_unique_cies("num_unique_cies");
|
||||
for (CieRecord<E> &cie : obj->cies)
|
||||
if (cie.is_leader)
|
||||
num_unique_cies++;
|
||||
|
||||
static Counter num_fdes("num_fdes");
|
||||
num_fdes += obj->fdes.size();
|
||||
}
|
||||
|
||||
static Counter num_bytes("total_input_bytes");
|
||||
for (std::unique_ptr<MappedFile<Context<E>>> &mf : ctx.mf_pool)
|
||||
num_bytes += mf->size;
|
||||
|
||||
static Counter num_input_sections("input_sections");
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
num_input_sections += file->sections.size();
|
||||
|
||||
static Counter num_output_chunks("output_chunks", ctx.chunks.size());
|
||||
static Counter num_objs("num_objs", ctx.objs.size());
|
||||
static Counter num_dsos("num_dsos", ctx.dsos.size());
|
||||
|
||||
if constexpr (E::e_machine == EM_AARCH64) {
|
||||
static Counter num_thunks("num_thunks");
|
||||
for (std::unique_ptr<OutputSection<E>> &osec : ctx.output_sections)
|
||||
for (std::unique_ptr<RangeExtensionThunk<E>> &thunk : osec->thunks)
|
||||
num_thunks += thunk->symbols.size();
|
||||
}
|
||||
|
||||
Counter::print();
|
||||
}
|
||||
|
||||
static i64 get_default_thread_count() {
|
||||
// mold doesn't scale above 32 threads.
|
||||
int n = tbb::global_control::active_value(
|
||||
tbb::global_control::max_allowed_parallelism);
|
||||
return std::min(n, 32);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static int elf_main(int argc, char **argv) {
|
||||
Context<E> ctx;
|
||||
|
||||
// Process -run option first. process_run_subcommand() does not return.
|
||||
if (argc >= 2)
|
||||
if (argv[1] == "-run"sv || argv[1] == "--run"sv)
|
||||
process_run_subcommand(ctx, argc, argv);
|
||||
|
||||
// Parse non-positional command line options
|
||||
ctx.cmdline_args = expand_response_files(ctx, argv);
|
||||
std::vector<std::string_view> file_args;
|
||||
parse_nonpositional_args(ctx, file_args);
|
||||
|
||||
// If no -m option is given, deduce it from input files.
|
||||
if (ctx.arg.emulation == -1)
|
||||
ctx.arg.emulation = deduce_machine_type(ctx, file_args);
|
||||
|
||||
// Redo if -m is not x86-64.
|
||||
if (ctx.arg.emulation != E::e_machine) {
|
||||
switch (ctx.arg.emulation) {
|
||||
case EM_386:
|
||||
return elf_main<I386>(argc, argv);
|
||||
case EM_AARCH64:
|
||||
return elf_main<ARM64>(argc, argv);
|
||||
}
|
||||
unreachable();
|
||||
}
|
||||
|
||||
Timer t_all(ctx, "all");
|
||||
|
||||
if (ctx.arg.relocatable) {
|
||||
combine_objects(ctx, file_args);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!ctx.arg.preload)
|
||||
try_resume_daemon(ctx);
|
||||
|
||||
i64 thread_count = ctx.arg.thread_count;
|
||||
if (thread_count == 0)
|
||||
thread_count = get_default_thread_count();
|
||||
tbb::global_control tbb_cont(tbb::global_control::max_allowed_parallelism,
|
||||
thread_count);
|
||||
|
||||
install_signal_handler();
|
||||
|
||||
if (!ctx.arg.directory.empty())
|
||||
if (chdir(ctx.arg.directory.c_str()) == -1)
|
||||
Fatal(ctx) << "chdir failed: " << ctx.arg.directory
|
||||
<< ": " << errno_string();
|
||||
|
||||
// Handle --wrap options if any.
|
||||
for (std::string_view name : ctx.arg.wrap)
|
||||
get_symbol(ctx, name)->wrap = true;
|
||||
|
||||
// Handle --retain-symbols-file options if any.
|
||||
if (ctx.arg.retain_symbols_file)
|
||||
for (std::string_view name : *ctx.arg.retain_symbols_file)
|
||||
get_symbol(ctx, name)->write_to_symtab = true;
|
||||
|
||||
// Preload input files
|
||||
std::function<void()> on_complete;
|
||||
std::function<void()> wait_for_client;
|
||||
|
||||
if (ctx.arg.preload)
|
||||
daemonize(ctx, &wait_for_client, &on_complete);
|
||||
else if (ctx.arg.fork)
|
||||
on_complete = fork_child();
|
||||
|
||||
for (std::string_view arg : ctx.arg.trace_symbol)
|
||||
get_symbol(ctx, arg)->traced = true;
|
||||
|
||||
// Parse input files
|
||||
read_input_files(ctx, file_args);
|
||||
|
||||
if (ctx.arg.preload) {
|
||||
wait_for_client();
|
||||
if (!reload_input_files(ctx)) {
|
||||
std::vector<char *> args(argv, argv + argc);
|
||||
args.push_back((char *)"--no-preload");
|
||||
args.push_back(nullptr);
|
||||
return elf_main<E>(argc + 1, args.data());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
Timer t(ctx, "register_section_pieces");
|
||||
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
||||
file->register_section_pieces(ctx);
|
||||
});
|
||||
}
|
||||
|
||||
// Uniquify shared object files by soname
|
||||
{
|
||||
std::unordered_set<std::string_view> seen;
|
||||
std::erase_if(ctx.dsos, [&](SharedFile<E> *file) {
|
||||
return !seen.insert(file->soname).second;
|
||||
});
|
||||
}
|
||||
|
||||
Timer t_total(ctx, "total");
|
||||
Timer t_before_copy(ctx, "before_copy");
|
||||
|
||||
// Apply -exclude-libs
|
||||
apply_exclude_libs(ctx);
|
||||
|
||||
// Create instances of linker-synthesized sections such as
|
||||
// .got or .plt.
|
||||
create_synthetic_sections(ctx);
|
||||
|
||||
// Resolve symbols and fix the set of object files that are
|
||||
// included to the final output.
|
||||
resolve_symbols(ctx);
|
||||
|
||||
// We currently do not natively support LTO.
|
||||
// If LLVM LTO is in use, fallback to the lld linker by invoking
|
||||
// it with the exact same command line arguments as we got.
|
||||
if (ctx.llvm_lto) {
|
||||
Warn(ctx) << "LLVM LTO is detected, so falling back to ld.lld";
|
||||
argv[0] = (char *)"ld.lld";
|
||||
execvp("ld.lld", argv);
|
||||
Fatal(ctx) << "execvp failed: ld.lld: " << errno_string();
|
||||
}
|
||||
|
||||
// Do the same for GCC LTO.
|
||||
if (Symbol<E> *sym = get_symbol(ctx, "__gnu_lto_slim"); sym->file) {
|
||||
Warn(ctx) << *sym->file
|
||||
<< "GCC LTO is detected, so falling back to ld.bfd";
|
||||
execvp("ld.bfd", argv);
|
||||
Fatal(ctx) << "execvp failed: ld.bfd: " << errno_string();
|
||||
}
|
||||
|
||||
// Remove redundant comdat sections (e.g. duplicate inline functions).
|
||||
eliminate_comdats(ctx);
|
||||
|
||||
// Create .bss sections for common symbols.
|
||||
convert_common_symbols(ctx);
|
||||
|
||||
// Apply version scripts.
|
||||
apply_version_script(ctx);
|
||||
|
||||
// Parse symbol version suffixes (e.g. "foo@ver1").
|
||||
parse_symbol_version(ctx);
|
||||
|
||||
// Set is_import and is_export bits for each symbol.
|
||||
compute_import_export(ctx);
|
||||
|
||||
// Garbage-collect unreachable sections.
|
||||
if (ctx.arg.gc_sections)
|
||||
gc_sections(ctx);
|
||||
|
||||
// Merge identical read-only sections.
|
||||
if (ctx.arg.icf)
|
||||
icf_sections(ctx);
|
||||
|
||||
// Compute sizes of sections containing mergeable strings.
|
||||
compute_merged_section_sizes(ctx);
|
||||
|
||||
// Bin input sections into output sections.
|
||||
bin_sections(ctx);
|
||||
|
||||
// Get a list of output sections.
|
||||
append(ctx.chunks, collect_output_sections(ctx));
|
||||
|
||||
// Create a dummy file containing linker-synthesized symbols
|
||||
// (e.g. `__bss_start`).
|
||||
ctx.internal_obj = create_internal_file(ctx);
|
||||
ctx.internal_obj->resolve_symbols(ctx);
|
||||
ctx.objs.push_back(ctx.internal_obj);
|
||||
|
||||
// Beyond this point, no new files will be added to ctx.objs
|
||||
// or ctx.dsos.
|
||||
|
||||
// Handle `-z cet-report`.
|
||||
if (ctx.arg.z_cet_report != CET_REPORT_NONE)
|
||||
check_cet_errors(ctx);
|
||||
|
||||
// If we are linking a .so file, remaining undefined symbols does
|
||||
// not cause a linker error. Instead, they are treated as if they
|
||||
// were imported symbols.
|
||||
//
|
||||
// If we are linking an executable, weak undefs are converted to
|
||||
// weakly imported symbol so that they'll have another chance to be
|
||||
// resolved.
|
||||
claim_unresolved_symbols(ctx);
|
||||
|
||||
// Beyond this point, no new symbols will be added to the result.
|
||||
|
||||
// Make sure that all symbols have been resolved.
|
||||
if (!ctx.arg.allow_multiple_definition)
|
||||
check_duplicate_symbols(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;
|
||||
|
||||
// .init_array and .fini_array contents have to be sorted by
|
||||
// a special rule. Sort them.
|
||||
sort_init_fini(ctx);
|
||||
|
||||
// Compute sizes of output sections while assigning offsets
|
||||
// within an output section to input sections.
|
||||
compute_section_sizes(ctx);
|
||||
|
||||
// Sort sections by section attributes so that we'll have to
|
||||
// create as few segments as possible.
|
||||
sort(ctx.chunks, [&](Chunk<E> *a, Chunk<E> *b) {
|
||||
return get_section_rank(ctx, a) < get_section_rank(ctx, b);
|
||||
});
|
||||
|
||||
// Copy string referred by .dynamic to .dynstr.
|
||||
for (SharedFile<E> *file : ctx.dsos)
|
||||
ctx.dynstr->add_string(file->soname);
|
||||
for (std::string_view str : ctx.arg.auxiliary)
|
||||
ctx.dynstr->add_string(str);
|
||||
for (std::string_view str : ctx.arg.filter)
|
||||
ctx.dynstr->add_string(str);
|
||||
if (!ctx.arg.rpaths.empty())
|
||||
ctx.dynstr->add_string(ctx.arg.rpaths);
|
||||
if (!ctx.arg.soname.empty())
|
||||
ctx.dynstr->add_string(ctx.arg.soname);
|
||||
|
||||
// Scan relocations to find symbols that need entries in .got, .plt,
|
||||
// .got.plt, .dynsym, .dynstr, etc.
|
||||
scan_rels(ctx);
|
||||
|
||||
// If --packed_dyn_relocs=relr was given, base relocations are stored
|
||||
// to a .relr.dyn section in a compressed form. Construct a compressed
|
||||
// relocations now so that we can fix section sizes and file layout.
|
||||
if (ctx.arg.pack_dyn_relocs_relr)
|
||||
construct_relr(ctx);
|
||||
|
||||
// Reserve a space for dynamic symbol strings in .dynstr and sort
|
||||
// .dynsym contents if necessary. Beyond this point, no symbol will
|
||||
// be added to .dynsym.
|
||||
ctx.dynsym->finalize(ctx);
|
||||
|
||||
// Fill .gnu.version_d section contents.
|
||||
ctx.verdef->construct(ctx);
|
||||
|
||||
// Fill .gnu.version_r section contents.
|
||||
ctx.verneed->construct(ctx);
|
||||
|
||||
// Compute .symtab and .strtab sizes for each file.
|
||||
{
|
||||
Timer t(ctx, "compute_symtab");
|
||||
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
||||
file->compute_symtab(ctx);
|
||||
});
|
||||
}
|
||||
|
||||
// .eh_frame is a special section from the linker's point of view,
|
||||
// as its contents are parsed and reconstructed by the linker,
|
||||
// unlike other sections that are regarded as opaque bytes.
|
||||
// Here, we transplant .eh_frame sections from a regular output
|
||||
// section to the special EHFrameSection.
|
||||
{
|
||||
Timer t(ctx, "eh_frame");
|
||||
std::erase_if(ctx.chunks, [](Chunk<E> *chunk) {
|
||||
return chunk->kind == Chunk<E>::REGULAR && chunk->name == ".eh_frame";
|
||||
});
|
||||
ctx.eh_frame->construct(ctx);
|
||||
}
|
||||
|
||||
// Update shdr.sh_size for each chunk and remove empty ones.
|
||||
for (Chunk<E> *chunk : ctx.chunks)
|
||||
chunk->update_shdr(ctx);
|
||||
|
||||
std::erase_if(ctx.chunks, [](Chunk<E> *chunk) {
|
||||
return chunk->kind == Chunk<E>::SYNTHETIC && chunk->shdr.sh_size == 0;
|
||||
});
|
||||
|
||||
// Set section indices.
|
||||
for (i64 i = 0, shndx = 1; i < ctx.chunks.size(); i++)
|
||||
if (ctx.chunks[i]->kind != Chunk<E>::HEADER)
|
||||
ctx.chunks[i]->shndx = shndx++;
|
||||
|
||||
// Some types of section header refer other section by index.
|
||||
// Recompute the section header to fill such fields with correct values.
|
||||
for (Chunk<E> *chunk : ctx.chunks)
|
||||
chunk->update_shdr(ctx);
|
||||
|
||||
// Assign offsets to output sections
|
||||
i64 filesize = set_osec_offsets(ctx);
|
||||
|
||||
// On ARM64, we may need to create so-called "range extension thunks"
|
||||
// to extend branch instructions reach, as they can jump only to
|
||||
// ±128 MiB.
|
||||
if constexpr (E::e_machine == EM_AARCH64)
|
||||
filesize = create_range_extension_thunks(ctx);
|
||||
|
||||
// Fix linker-synthesized symbol addresses.
|
||||
fix_synthetic_symbols(ctx);
|
||||
|
||||
// If --compress-debug-sections is given, compress .debug_* sections
|
||||
// using zlib.
|
||||
if (ctx.arg.compress_debug_sections != COMPRESS_NONE) {
|
||||
compress_debug_sections(ctx);
|
||||
filesize = set_osec_offsets(ctx);
|
||||
}
|
||||
|
||||
// At this point, file layout is fixed.
|
||||
// Beyond this, you can assume that symbol addresses including their
|
||||
// GOT or PLT addresses have a correct final value.
|
||||
|
||||
// Some types of TLS relocations are defined relative to the beginning
|
||||
// or the end of the TLS segment address. Find these addresses now.
|
||||
for (ElfPhdr<E> phdr : create_phdr(ctx)) {
|
||||
if (phdr.p_type == PT_TLS) {
|
||||
ctx.tls_begin = phdr.p_vaddr;
|
||||
ctx.tls_end = align_to(phdr.p_vaddr + phdr.p_memsz, phdr.p_align);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
t_before_copy.stop();
|
||||
|
||||
// Create an output file
|
||||
ctx.output_file = OutputFile<E>::open(ctx, ctx.arg.output, filesize, 0777);
|
||||
ctx.buf = ctx.output_file->buf;
|
||||
|
||||
Timer t_copy(ctx, "copy");
|
||||
|
||||
// Copy input sections to the output file
|
||||
{
|
||||
Timer t(ctx, "copy_buf");
|
||||
|
||||
tbb::parallel_for_each(ctx.chunks, [&](Chunk<E> *chunk) {
|
||||
std::string name(chunk->name);
|
||||
if (name.empty())
|
||||
name = "(header)";
|
||||
Timer t2(ctx, name, &t);
|
||||
|
||||
chunk->copy_buf(ctx);
|
||||
});
|
||||
|
||||
ctx.checkpoint();
|
||||
}
|
||||
|
||||
if constexpr (E::e_machine == EM_AARCH64)
|
||||
write_thunks(ctx);
|
||||
|
||||
// Dynamic linker works better with sorted .rela.dyn section,
|
||||
// so we sort them.
|
||||
ctx.reldyn->sort(ctx);
|
||||
|
||||
// Zero-clear paddings between sections
|
||||
clear_padding(ctx);
|
||||
|
||||
if (ctx.buildid) {
|
||||
Timer t(ctx, "build_id");
|
||||
ctx.buildid->write_buildid(ctx);
|
||||
}
|
||||
|
||||
t_copy.stop();
|
||||
|
||||
// Close the output file. This is the end of the linker's main job.
|
||||
ctx.output_file->close(ctx);
|
||||
|
||||
t_total.stop();
|
||||
t_all.stop();
|
||||
|
||||
if (ctx.arg.print_map)
|
||||
print_map(ctx);
|
||||
|
||||
// Show stats numbers
|
||||
if (ctx.arg.stats)
|
||||
show_stats(ctx);
|
||||
|
||||
if (ctx.arg.perf)
|
||||
print_timer_records(ctx.timer_records);
|
||||
|
||||
std::cout << std::flush;
|
||||
std::cerr << std::flush;
|
||||
if (on_complete)
|
||||
on_complete();
|
||||
|
||||
if (ctx.arg.quick_exit)
|
||||
_exit(0);
|
||||
|
||||
for (std::function<void()> &fn : ctx.on_exit)
|
||||
fn();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
return elf_main<X86_64>(argc, argv);
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template void read_file(Context<E> &, MappedFile<Context<E>> *);
|
||||
|
||||
INSTANTIATE(X86_64);
|
||||
INSTANTIATE(I386);
|
||||
INSTANTIATE(ARM64);
|
||||
|
||||
} // namespace mold::elf
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <ios>
|
||||
#include <sstream>
|
||||
#include <tbb/parallel_for_each.h>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace mold::elf {
|
||||
|
||||
template <typename E>
|
||||
using Map =
|
||||
tbb::concurrent_hash_map<InputSection<E> *, std::vector<Symbol<E> *>>;
|
||||
|
||||
template <typename E>
|
||||
static std::unique_ptr<std::ofstream> open_output_file(Context<E> &ctx) {
|
||||
std::unique_ptr<std::ofstream> file(new std::ofstream);
|
||||
file->open(ctx.arg.Map.c_str());
|
||||
if (!file->is_open())
|
||||
Fatal(ctx) << "cannot open " << ctx.arg.Map << ": " << errno_string();
|
||||
return file;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static Map<E> get_map(Context<E> &ctx) {
|
||||
Map<E> map;
|
||||
|
||||
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
||||
for (Symbol<E> *sym : file->symbols) {
|
||||
if (sym->file == file && sym->input_section &&
|
||||
sym->get_type() != STT_SECTION) {
|
||||
assert(file == &sym->input_section->file);
|
||||
|
||||
typename Map<E>::accessor acc;
|
||||
map.insert(acc, {sym->input_section, {}});
|
||||
acc->second.push_back(sym);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tbb::parallel_for(map.range(), [](const typename Map<E>::range_type &range) {
|
||||
for (auto it = range.begin(); it != range.end(); it++) {
|
||||
std::vector<Symbol<E> *> &vec = it->second;
|
||||
sort(vec, [](Symbol<E> *a, Symbol<E> *b) { return a->value < b->value; });
|
||||
}
|
||||
});
|
||||
return map;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void print_map(Context<E> &ctx) {
|
||||
std::ostream *out = &std::cout;
|
||||
std::unique_ptr<std::ofstream> file;
|
||||
|
||||
if (!ctx.arg.Map.empty()) {
|
||||
file = open_output_file(ctx);
|
||||
out = file.get();
|
||||
}
|
||||
|
||||
// Construct a section-to-symbol map.
|
||||
Map<E> map = get_map(ctx);
|
||||
|
||||
// Print a mapfile.
|
||||
*out << " VMA Size Align Out In Symbol\n";
|
||||
|
||||
for (Chunk<E> *osec : ctx.chunks) {
|
||||
*out << std::setw(16) << (u64)osec->shdr.sh_addr
|
||||
<< std::setw(11) << (u64)osec->shdr.sh_size
|
||||
<< std::setw(6) << (u64)osec->shdr.sh_addralign
|
||||
<< " " << osec->name << "\n";
|
||||
|
||||
if (osec->kind != Chunk<E>::REGULAR)
|
||||
continue;
|
||||
|
||||
std::span<InputSection<E> *> members = ((OutputSection<E> *)osec)->members;
|
||||
std::vector<std::string> bufs(members.size());
|
||||
|
||||
tbb::parallel_for((i64)0, (i64)members.size(), [&](i64 i) {
|
||||
InputSection<E> *mem = members[i];
|
||||
std::ostringstream ss;
|
||||
opt_demangle = ctx.arg.demangle;
|
||||
|
||||
ss << std::setw(16) << (osec->shdr.sh_addr + mem->offset)
|
||||
<< std::setw(11) << (u64)mem->shdr.sh_size
|
||||
<< std::setw(6) << (u64)mem->shdr.sh_addralign
|
||||
<< " " << *mem << "\n";
|
||||
|
||||
typename Map<E>::const_accessor acc;
|
||||
if (map.find(acc, mem))
|
||||
for (Symbol<E> *sym : acc->second)
|
||||
ss << std::setw(16) << sym->get_addr(ctx)
|
||||
<< " 0 0 "
|
||||
<< *sym << "\n";
|
||||
|
||||
bufs[i] = ss.str();
|
||||
});
|
||||
|
||||
for (std::string &str : bufs)
|
||||
*out << str;
|
||||
}
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template void print_map(Context<E> &ctx);
|
||||
|
||||
INSTANTIATE(X86_64);
|
||||
INSTANTIATE(I386);
|
||||
INSTANTIATE(ARM64);
|
||||
|
||||
} // namespace mold::elf
|
||||
@@ -9,10 +9,6 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if __has_include(<alloca.h>)
|
||||
# include <alloca.h>
|
||||
#endif
|
||||
|
||||
extern char **environ;
|
||||
|
||||
static char *get_mold_path() {
|
||||
@@ -23,7 +19,7 @@ static char *get_mold_path() {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static void debug_print(const char *fmt, ...) {
|
||||
static void debug_print(char *fmt, ...) {
|
||||
if (!getenv("MOLD_WRAPPER_DEBUG"))
|
||||
return;
|
||||
|
||||
@@ -35,25 +31,13 @@ static void debug_print(const char *fmt, ...) {
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
static int count_args(va_list *ap) {
|
||||
va_list aq;
|
||||
va_copy(aq, *ap);
|
||||
|
||||
int i = 0;
|
||||
while (va_arg(aq, char *))
|
||||
i++;
|
||||
va_end(aq);
|
||||
return i;
|
||||
}
|
||||
|
||||
static void copy_args(char **argv, const char *arg0, va_list *ap) {
|
||||
int i = 1;
|
||||
char *arg;
|
||||
while ((arg = va_arg(*ap, char *)))
|
||||
argv[i++] = arg;
|
||||
|
||||
((const char **)argv)[0] = arg0;
|
||||
((const char **)argv)[i] = NULL;
|
||||
static void get_args(va_list *ap, int argc, char **argv) {
|
||||
for (int i = 1; i < argc - 1; i++) {
|
||||
char *arg = va_arg(*ap, char *);
|
||||
if (!arg)
|
||||
break;
|
||||
argv[i] = arg;
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_ld(const char *path) {
|
||||
@@ -62,15 +46,16 @@ static bool is_ld(const char *path) {
|
||||
ptr--;
|
||||
|
||||
return !strcmp(ptr, "ld") || !strcmp(ptr, "ld.lld") ||
|
||||
!strcmp(ptr, "ld.gold") || !strcmp(ptr, "ld.bfd") ||
|
||||
!strcmp(ptr, "ld.mold");
|
||||
!strcmp(ptr, "ld.gold");
|
||||
}
|
||||
|
||||
int execvpe(const char *file, char *const *argv, char *const *envp) {
|
||||
debug_print("execvpe %s\n", file);
|
||||
|
||||
if (!strcmp(file, "ld") || is_ld(file))
|
||||
if (!strcmp(file, "ld") || is_ld(file)) {
|
||||
file = get_mold_path();
|
||||
((const char **)argv)[0] = file;
|
||||
}
|
||||
|
||||
for (int i = 0; envp[i]; i++)
|
||||
putenv(envp[i]);
|
||||
@@ -81,8 +66,12 @@ int execvpe(const char *file, char *const *argv, char *const *envp) {
|
||||
|
||||
int execve(const char *path, char *const *argv, char *const *envp) {
|
||||
debug_print("execve %s\n", path);
|
||||
if (is_ld(path))
|
||||
|
||||
if (is_ld(path)) {
|
||||
path = get_mold_path();
|
||||
((const char **)argv)[0] = path;
|
||||
}
|
||||
|
||||
typeof(execve) *real = dlsym(RTLD_NEXT, "execve");
|
||||
return real(path, argv, envp);
|
||||
}
|
||||
@@ -90,8 +79,8 @@ int execve(const char *path, char *const *argv, char *const *envp) {
|
||||
int execl(const char *path, const char *arg0, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, arg0);
|
||||
char **argv = alloca((count_args(&ap) + 2) * sizeof(char *));
|
||||
copy_args(argv, arg0, &ap);
|
||||
char *argv[4096] = {(char *)arg0};
|
||||
get_args(&ap, 4096, argv);
|
||||
va_end(ap);
|
||||
return execve(path, argv, environ);
|
||||
}
|
||||
@@ -99,8 +88,8 @@ int execl(const char *path, const char *arg0, ...) {
|
||||
int execlp(const char *file, const char *arg0, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, arg0);
|
||||
char **argv = alloca((count_args(&ap) + 2) * sizeof(char *));
|
||||
copy_args(argv, arg0, &ap);
|
||||
char *argv[4096] = {(char *)arg0};
|
||||
get_args(&ap, 4096, argv);
|
||||
va_end(ap);
|
||||
return execvpe(file, argv, environ);
|
||||
}
|
||||
@@ -108,8 +97,8 @@ int execlp(const char *file, const char *arg0, ...) {
|
||||
int execle(const char *path, const char *arg0, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, arg0);
|
||||
char **argv = alloca((count_args(&ap) + 2) * sizeof(char *));
|
||||
copy_args(argv, arg0, &ap);
|
||||
char *argv[4096] = {(char *)arg0};
|
||||
get_args(&ap, 4096, argv);
|
||||
char **env = va_arg(ap, char **);
|
||||
va_end(ap);
|
||||
return execve(path, argv, env);
|
||||
@@ -128,19 +117,12 @@ int posix_spawn(pid_t *pid, const char *path,
|
||||
const posix_spawnattr_t *attrp,
|
||||
char *const *argv, char *const *envp) {
|
||||
debug_print("posix_spawn %s\n", path);
|
||||
if (is_ld(path))
|
||||
|
||||
if (is_ld(path)) {
|
||||
path = get_mold_path();
|
||||
((const char **)argv)[0] = 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);
|
||||
}
|
||||
+2195
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,142 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace mold::elf {
|
||||
|
||||
inline u32 get_umask() {
|
||||
u32 orig_umask = umask(0);
|
||||
umask(orig_umask);
|
||||
return orig_umask;
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
static std::pair<i64, char *>
|
||||
open_or_create_file(C &ctx, std::string path, i64 filesize, i64 perm) {
|
||||
std::string tmpl = filepath(path).parent_path() / ".mold-XXXXXX";
|
||||
char *path2 = (char *)save_string(ctx, tmpl).data();
|
||||
|
||||
i64 fd = mkstemp(path2);
|
||||
if (fd == -1)
|
||||
Fatal(ctx) << "cannot open " << path2 << ": " << errno_string();
|
||||
|
||||
// Reuse an existing file if exists and writable.
|
||||
if (rename(path.c_str(), path2) == 0) {
|
||||
::close(fd);
|
||||
fd = ::open(path2, O_RDWR | O_CREAT, perm);
|
||||
if (fd != -1 && !ftruncate(fd, filesize) && !fchmod(fd, perm & ~get_umask()))
|
||||
return {fd, path2};
|
||||
|
||||
unlink(path2);
|
||||
fd = ::open(path2, O_RDWR | O_CREAT, perm);
|
||||
if (fd == -1)
|
||||
Fatal(ctx) << "cannot open " << path2 << ": " << errno_string();
|
||||
}
|
||||
|
||||
if (ftruncate(fd, filesize))
|
||||
Fatal(ctx) << "ftruncate failed: " << errno_string();
|
||||
|
||||
if (fchmod(fd, (perm & ~get_umask())) == -1)
|
||||
Fatal(ctx) << "fchmod failed: " << errno_string();
|
||||
return {fd, path2};
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
class MemoryMappedOutputFile : public OutputFile<E> {
|
||||
public:
|
||||
MemoryMappedOutputFile(Context<E> &ctx, std::string path, i64 filesize, i64 perm)
|
||||
: OutputFile<E>(path, filesize, true) {
|
||||
i64 fd;
|
||||
std::tie(fd, output_tmpfile) = open_or_create_file(ctx, path, filesize, perm);
|
||||
|
||||
this->buf = (u8 *)mmap(nullptr, filesize, PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED, fd, 0);
|
||||
if (this->buf == MAP_FAILED)
|
||||
Fatal(ctx) << path << ": mmap failed: " << errno_string();
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
void close(Context<E> &ctx) override {
|
||||
Timer t(ctx, "close_file");
|
||||
|
||||
if (!this->is_unmapped)
|
||||
munmap(this->buf, this->filesize);
|
||||
|
||||
if (rename(output_tmpfile, this->path.c_str()) == -1)
|
||||
Fatal(ctx) << this->path << ": rename failed: " << errno_string();
|
||||
output_tmpfile = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class MallocOutputFile : public OutputFile<E> {
|
||||
public:
|
||||
MallocOutputFile(Context<E> &ctx, std::string path, i64 filesize, i64 perm)
|
||||
: OutputFile<E>(path, filesize, false), perm(perm) {
|
||||
this->buf = (u8 *)mmap(NULL, filesize, PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||
if (this->buf == MAP_FAILED)
|
||||
Fatal(ctx) << "mmap failed: " << errno_string();
|
||||
}
|
||||
|
||||
void close(Context<E> &ctx) override {
|
||||
Timer t(ctx, "close_file");
|
||||
|
||||
if (this->path == "-") {
|
||||
fwrite(this->buf, this->filesize, 1, stdout);
|
||||
fclose(stdout);
|
||||
return;
|
||||
}
|
||||
|
||||
i64 fd = ::open(this->path.c_str(), O_RDWR | O_CREAT, perm);
|
||||
if (fd == -1)
|
||||
Fatal(ctx) << "cannot open " << this->path << ": " << errno_string();
|
||||
|
||||
FILE *fp = fdopen(fd, "w");
|
||||
fwrite(this->buf, this->filesize, 1, fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
private:
|
||||
i64 perm;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
std::unique_ptr<OutputFile<E>>
|
||||
OutputFile<E>::open(Context<E> &ctx, std::string path, i64 filesize, i64 perm) {
|
||||
Timer t(ctx, "open_file");
|
||||
|
||||
if (path.starts_with('/') && !ctx.arg.chroot.empty())
|
||||
path = ctx.arg.chroot + "/" + path_clean(path);
|
||||
|
||||
bool is_special = false;
|
||||
if (path == "-") {
|
||||
is_special = true;
|
||||
} else {
|
||||
struct stat st;
|
||||
if (stat(path.c_str(), &st) == 0 && (st.st_mode & S_IFMT) != S_IFREG)
|
||||
is_special = true;
|
||||
}
|
||||
|
||||
std::unique_ptr<OutputFile<E>> file;
|
||||
if (is_special)
|
||||
file = std::make_unique<MallocOutputFile<E>>(ctx, path, filesize, perm);
|
||||
else
|
||||
file = std::make_unique<MemoryMappedOutputFile<E>>(ctx, path, filesize, perm);
|
||||
|
||||
if (ctx.arg.filler != -1)
|
||||
memset(file->buf, ctx.arg.filler, filesize);
|
||||
return file;
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template class OutputFile<E>;
|
||||
|
||||
INSTANTIATE(X86_64);
|
||||
INSTANTIATE(I386);
|
||||
INSTANTIATE(ARM64);
|
||||
|
||||
} // namespace mold::elf
|
||||
+1120
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,595 @@
|
||||
// This file implements the -r or ---relocatable option. If the option
|
||||
// is given to a linker, the linker creates not an executable nor a
|
||||
// shared library file but instead create another object file by
|
||||
// combining input object files.
|
||||
//
|
||||
// The behavior of a linker with -r is pretty different from the one
|
||||
// without -r, and adding code for -r to the regular code path could
|
||||
// severely complicate it. Therefore, we implement -r as an
|
||||
// independent feature from others. That's why this file share only a
|
||||
// small amount of code with other files.
|
||||
//
|
||||
// Since the -r option is a minor feature, we don't aim for speed in
|
||||
// this file. This is a "better than nothing" implementation of the -r
|
||||
// option.
|
||||
//
|
||||
// Here is the strategy as to how to combine multiple object files
|
||||
// into one:
|
||||
//
|
||||
// - Regular sections containing opaque data (e.g. ".text" or
|
||||
// ".data") are just copied as-is without merging.
|
||||
//
|
||||
// - .symtab, .strtab and .shstrtab are merged.
|
||||
//
|
||||
// - COMDAT groups are uniquified.
|
||||
//
|
||||
// - Relocation sections are copied one by one, but we need to fix
|
||||
// symbol indices.
|
||||
|
||||
#include "mold.h"
|
||||
#include "../archive-file.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace mold::elf {
|
||||
|
||||
template <typename E> class RObjectFile;
|
||||
|
||||
template <typename E>
|
||||
class RChunk {
|
||||
public:
|
||||
RChunk() {
|
||||
out_shdr.sh_addralign = 1;
|
||||
}
|
||||
|
||||
virtual ~RChunk() = default;
|
||||
virtual void update_shdr(Context<E> &ctx) {}
|
||||
virtual void write_to(Context<E> &ctx) = 0;
|
||||
|
||||
std::string_view name;
|
||||
i64 shndx = 0;
|
||||
ElfShdr<E> in_shdr = {};
|
||||
ElfShdr<E> out_shdr = {};
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class RInputSection : public RChunk<E> {
|
||||
public:
|
||||
RInputSection(Context<E> &ctx, RObjectFile<E> &file, const ElfShdr<E> &shdr);
|
||||
void update_shdr(Context<E> &ctx) override;
|
||||
void write_to(Context<E> &ctx) override;
|
||||
|
||||
RObjectFile<E> &file;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class RSymtabSection : public RChunk<E> {
|
||||
public:
|
||||
RSymtabSection() {
|
||||
this->name = ".symtab";
|
||||
this->out_shdr.sh_type = SHT_SYMTAB;
|
||||
this->out_shdr.sh_entsize = sizeof(ElfSym<E>);
|
||||
this->out_shdr.sh_addralign = E::word_size;
|
||||
}
|
||||
|
||||
void add_local_symbol(Context<E> &ctx, RObjectFile<E> &file, i64 idx);
|
||||
void add_global_symbol(Context<E> &ctx, RObjectFile<E> &file, i64 idx);
|
||||
void update_shdr(Context<E> &ctx) override;
|
||||
void write_to(Context<E> &ctx) override;
|
||||
|
||||
std::unordered_map<std::string_view, i64> sym_map;
|
||||
std::vector<ElfSym<E>> syms{1};
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class RStrtabSection : public RChunk<E> {
|
||||
public:
|
||||
RStrtabSection(std::string_view name) {
|
||||
this->name = name;
|
||||
this->out_shdr.sh_type = SHT_STRTAB;
|
||||
this->out_shdr.sh_size = 1;
|
||||
}
|
||||
|
||||
i64 add_string(std::string_view str);
|
||||
void write_to(Context<E> &ctx) override;
|
||||
|
||||
std::unordered_map<std::string_view, i64> strings;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class ROutputEhdr : public RChunk<E> {
|
||||
public:
|
||||
ROutputEhdr() {
|
||||
this->out_shdr.sh_size = sizeof(ElfEhdr<E>);
|
||||
}
|
||||
|
||||
void write_to(Context<E> &ctx) override;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class ROutputShdr : public RChunk<E> {
|
||||
public:
|
||||
ROutputShdr() {
|
||||
this->out_shdr.sh_size = sizeof(ElfShdr<E>);
|
||||
}
|
||||
|
||||
void update_shdr(Context<E> &ctx) override;
|
||||
void write_to(Context<E> &ctx) override;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class RObjectFile {
|
||||
public:
|
||||
RObjectFile(Context<E> &ctx, MappedFile<Context<E>> &mf, bool is_alive);
|
||||
|
||||
void remove_comdats(Context<E> &ctx,
|
||||
std::unordered_set<std::string_view> &groups);
|
||||
|
||||
template <typename T>
|
||||
std::span<T> get_data(Context<E> &ctx, const ElfShdr<E> &shdr);
|
||||
|
||||
MappedFile<Context<E>> &mf;
|
||||
std::span<ElfShdr<E>> elf_sections;
|
||||
std::vector<std::unique_ptr<RInputSection<E>>> sections;
|
||||
std::span<const ElfSym<E>> syms;
|
||||
std::vector<i64> symidx;
|
||||
std::unordered_set<std::string_view> defined_syms;
|
||||
std::unordered_set<std::string_view> undef_syms;
|
||||
i64 symtab_shndx = 0;
|
||||
i64 first_global = 0;
|
||||
bool is_alive;
|
||||
const char *strtab = nullptr;
|
||||
const char *shstrtab = nullptr;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
void RSymtabSection<E>::add_local_symbol(Context<E> &ctx, RObjectFile<E> &file,
|
||||
i64 idx) {
|
||||
ElfSym<E> sym = file.syms[idx];
|
||||
assert(sym.st_bind == STB_LOCAL);
|
||||
|
||||
if (!sym.is_undef() && !sym.is_abs() && !sym.is_common()) {
|
||||
if (!file.sections[sym.st_shndx])
|
||||
return;
|
||||
sym.st_shndx = file.sections[sym.st_shndx]->shndx;
|
||||
}
|
||||
|
||||
std::string_view name = file.strtab + sym.st_name;
|
||||
sym.st_name = ctx.r_strtab->add_string(name);
|
||||
|
||||
file.symidx[idx] = syms.size();
|
||||
syms.push_back(sym);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void RSymtabSection<E>::add_global_symbol(Context<E> &ctx, RObjectFile<E> &file,
|
||||
i64 idx) {
|
||||
ElfSym<E> sym = file.syms[idx];
|
||||
assert(sym.st_bind != STB_LOCAL);
|
||||
|
||||
std::string_view name = file.strtab + sym.st_name;
|
||||
auto [it, inserted] = sym_map.insert({name, syms.size()});
|
||||
|
||||
if (inserted) {
|
||||
if (!sym.is_undef() && !sym.is_abs() && !sym.is_common())
|
||||
sym.st_shndx = file.sections[sym.st_shndx]->shndx;
|
||||
sym.st_name = ctx.r_strtab->add_string(name);
|
||||
file.symidx[idx] = syms.size();
|
||||
syms.push_back(sym);
|
||||
return;
|
||||
}
|
||||
|
||||
file.symidx[idx] = it->second;
|
||||
|
||||
ElfSym<E> &existing = syms[it->second];
|
||||
if (existing.is_undef() && !sym.is_undef()) {
|
||||
if (!sym.is_abs() && !sym.is_common())
|
||||
sym.st_shndx = file.sections[sym.st_shndx]->shndx;
|
||||
sym.st_name = existing.st_name;
|
||||
existing = sym;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void RSymtabSection<E>::update_shdr(Context<E> &ctx) {
|
||||
this->out_shdr.sh_size = syms.size() * sizeof(ElfSym<E>);
|
||||
this->out_shdr.sh_link = ctx.r_strtab->shndx;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void RSymtabSection<E>::write_to(Context<E> &ctx) {
|
||||
ElfSym<E> *buf = (ElfSym<E> *)(ctx.buf + this->out_shdr.sh_offset);
|
||||
for (i64 i = 1; i < syms.size(); i++)
|
||||
buf[i] = syms[i];
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
RInputSection<E>::RInputSection(Context<E> &ctx, RObjectFile<E> &file,
|
||||
const ElfShdr<E> &shdr)
|
||||
: file(file) {
|
||||
this->name = file.shstrtab + shdr.sh_name;
|
||||
this->in_shdr = shdr;
|
||||
this->out_shdr = shdr;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void RInputSection<E>::update_shdr(Context<E> &ctx) {
|
||||
switch (this->in_shdr.sh_type) {
|
||||
case SHT_GROUP:
|
||||
this->out_shdr.sh_link = ctx.r_symtab->shndx;
|
||||
this->out_shdr.sh_info = file.symidx[this->in_shdr.sh_info];
|
||||
break;
|
||||
case SHT_REL:
|
||||
case SHT_RELA:
|
||||
this->out_shdr.sh_link = ctx.r_symtab->shndx;
|
||||
this->out_shdr.sh_info = file.sections[this->in_shdr.sh_info]->shndx;
|
||||
break;
|
||||
default:
|
||||
if (this->in_shdr.sh_link) {
|
||||
std::unique_ptr<RInputSection<E>> &sec =
|
||||
file.sections[this->in_shdr.sh_info];
|
||||
|
||||
if (sec)
|
||||
this->out_shdr.sh_link = sec->shndx;
|
||||
else if (this->in_shdr.sh_link == file.symtab_shndx)
|
||||
this->out_shdr.sh_link = ctx.r_symtab->shndx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void RInputSection<E>::write_to(Context<E> &ctx) {
|
||||
if (this->in_shdr.sh_type == SHT_NOBITS)
|
||||
return;
|
||||
|
||||
std::span<u8> contents = file.template get_data<u8>(ctx, this->in_shdr);
|
||||
memcpy(ctx.buf + this->out_shdr.sh_offset, contents.data(), contents.size());
|
||||
|
||||
switch (this->in_shdr.sh_type) {
|
||||
case SHT_GROUP: {
|
||||
u32 *mem = (u32 *)(ctx.buf + this->out_shdr.sh_offset);
|
||||
for (i64 i = 1; i < this->out_shdr.sh_size / sizeof(u32); i++)
|
||||
mem[i] = file.sections[mem[i]]->shndx;
|
||||
break;
|
||||
}
|
||||
case SHT_REL:
|
||||
case SHT_RELA: {
|
||||
ElfRel<E> *rel = (ElfRel<E> *)(ctx.buf + this->out_shdr.sh_offset);
|
||||
i64 size = this->out_shdr.sh_size / sizeof(ElfRel<E>);
|
||||
|
||||
for (i64 i = 0; i < size; i++) {
|
||||
const ElfSym<E> &sym = file.syms[rel[i].r_sym];
|
||||
if (sym.is_undef() || sym.is_abs() || sym.is_common() ||
|
||||
file.sections[sym.st_shndx])
|
||||
rel[i].r_sym = file.symidx[rel[i].r_sym];
|
||||
else
|
||||
memset(rel + i, 0, sizeof(ElfRel<E>));
|
||||
}
|
||||
|
||||
i64 i = 0;
|
||||
i64 j = 0;
|
||||
for (; j < size; j++)
|
||||
if (rel[j].r_type)
|
||||
rel[i++] = rel[j];
|
||||
for (; i < size; i++)
|
||||
memset(rel + i, 0, sizeof(ElfRel<E>));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
i64 RStrtabSection<E>::add_string(std::string_view str) {
|
||||
auto [it, inserted] = strings.insert({str, this->out_shdr.sh_size});
|
||||
if (inserted)
|
||||
this->out_shdr.sh_size += str.size() + 1;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void RStrtabSection<E>::write_to(Context<E> &ctx) {
|
||||
for (auto [str, offset] : strings)
|
||||
memcpy(ctx.buf + this->out_shdr.sh_offset + offset, str.data(), str.size());
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ROutputEhdr<E>::write_to(Context<E> &ctx) {
|
||||
ElfEhdr<E> &hdr = *(ElfEhdr<E> *)(ctx.buf + this->out_shdr.sh_offset);
|
||||
memcpy(&hdr.e_ident, "\177ELF", 4);
|
||||
hdr.e_ident[EI_CLASS] = (E::word_size == 8) ? ELFCLASS64 : ELFCLASS32;
|
||||
hdr.e_ident[EI_DATA] = E::is_le ? ELFDATA2LSB : ELFDATA2MSB;
|
||||
hdr.e_ident[EI_VERSION] = EV_CURRENT;
|
||||
hdr.e_type = ET_REL;
|
||||
hdr.e_machine = E::e_machine;
|
||||
hdr.e_version = EV_CURRENT;
|
||||
hdr.e_shoff = ctx.r_shdr->out_shdr.sh_offset;
|
||||
hdr.e_ehsize = sizeof(ElfEhdr<E>);
|
||||
hdr.e_shentsize = sizeof(ElfShdr<E>);
|
||||
hdr.e_shstrndx = ctx.r_shstrtab->shndx;
|
||||
hdr.e_shnum = ctx.r_shdr->out_shdr.sh_size / sizeof(ElfShdr<E>);
|
||||
hdr.e_shstrndx = ctx.r_shstrtab->shndx;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ROutputShdr<E>::update_shdr(Context<E> &ctx) {
|
||||
for (RChunk<E> *chunk : ctx.r_chunks)
|
||||
if (chunk->shndx)
|
||||
this->out_shdr.sh_size += sizeof(ElfShdr<E>);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ROutputShdr<E>::write_to(Context<E> &ctx) {
|
||||
ElfShdr<E> *hdr = (ElfShdr<E> *)(ctx.buf + this->out_shdr.sh_offset);
|
||||
for (RChunk<E> *chunk : ctx.r_chunks)
|
||||
if (chunk->shndx)
|
||||
hdr[chunk->shndx] = chunk->out_shdr;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
RObjectFile<E>::RObjectFile(Context<E> &ctx, MappedFile<Context<E>> &mf,
|
||||
bool is_alive)
|
||||
: mf(mf), is_alive(is_alive) {
|
||||
// Read ELF header and section header
|
||||
ElfEhdr<E> &ehdr = *(ElfEhdr<E> *)mf.data;
|
||||
ElfShdr<E> *sh_begin = (ElfShdr<E> *)(mf.data + ehdr.e_shoff);
|
||||
i64 num_sections = (ehdr.e_shnum == 0) ? sh_begin->sh_size : ehdr.e_shnum;
|
||||
elf_sections = {sh_begin, sh_begin + num_sections};
|
||||
sections.resize(num_sections);
|
||||
|
||||
// Read .shstrtab
|
||||
i64 shstrtab_idx = (ehdr.e_shstrndx == SHN_XINDEX)
|
||||
? sh_begin->sh_link : ehdr.e_shstrndx;
|
||||
shstrtab = (char *)(mf.data + elf_sections[shstrtab_idx].sh_offset);
|
||||
|
||||
// Read .symtab
|
||||
for (i64 i = 1; i < elf_sections.size(); i++) {
|
||||
ElfShdr<E> &shdr = elf_sections[i];
|
||||
if (shdr.sh_type == SHT_SYMTAB) {
|
||||
syms = get_data<const ElfSym<E>>(ctx, shdr);
|
||||
strtab = (char *)(mf.data + elf_sections[shdr.sh_link].sh_offset);
|
||||
symtab_shndx = i;
|
||||
first_global = shdr.sh_info;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
symidx.resize(syms.size());
|
||||
|
||||
// Read sections
|
||||
for (i64 i = 1; i < elf_sections.size(); i++) {
|
||||
ElfShdr<E> &shdr = elf_sections[i];
|
||||
switch (shdr.sh_type) {
|
||||
case SHT_NULL:
|
||||
case SHT_SYMTAB:
|
||||
case SHT_STRTAB:
|
||||
break;
|
||||
default:
|
||||
sections[i].reset(new RInputSection(ctx, *this, shdr));
|
||||
}
|
||||
}
|
||||
|
||||
// Read global symbols
|
||||
for (i64 i = first_global; i < syms.size(); i++) {
|
||||
std::string_view name = strtab + syms[i].st_name;
|
||||
if (syms[i].is_defined())
|
||||
defined_syms.insert(name);
|
||||
else
|
||||
undef_syms.insert(name);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove duplicate comdat groups
|
||||
template <typename E>
|
||||
void RObjectFile<E>::remove_comdats(Context<E> &ctx,
|
||||
std::unordered_set<std::string_view> &groups) {
|
||||
for (i64 i = 1; i < sections.size(); i++) {
|
||||
if (!sections[i])
|
||||
continue;
|
||||
ElfShdr<E> &shdr = sections[i]->in_shdr;
|
||||
if (shdr.sh_type != SHT_GROUP)
|
||||
continue;
|
||||
|
||||
// Get a comdat group signature and insert it into a set.
|
||||
const ElfSym<E> &sym = syms[shdr.sh_info];
|
||||
std::string_view signature = strtab + sym.st_name;
|
||||
if (groups.insert(signature).second)
|
||||
continue;
|
||||
|
||||
// If it is a duplicate, remove it and its members.
|
||||
for (i64 j : this->template get_data<u32>(ctx, shdr).subspan(1))
|
||||
sections[j] = nullptr;
|
||||
sections[i] = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
template <typename T>
|
||||
std::span<T> RObjectFile<E>::get_data(Context<E> &ctx, const ElfShdr<E> &shdr) {
|
||||
T *begin = (T *)(mf.data + shdr.sh_offset);
|
||||
T *end = (T *)(mf.data + shdr.sh_offset + shdr.sh_size);
|
||||
return {begin, end};
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static std::vector<std::unique_ptr<RObjectFile<E>>>
|
||||
open_files(Context<E> &ctx, std::span<std::string_view> args) {
|
||||
std::vector<std::unique_ptr<RObjectFile<E>>> files;
|
||||
bool whole_archive = false;
|
||||
|
||||
while (!args.empty()) {
|
||||
if (read_flag(args, "whole-archive")) {
|
||||
whole_archive = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (read_flag(args, "no-whole-archive")) {
|
||||
whole_archive = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string_view arg;
|
||||
if (read_arg(ctx, args, arg, "version-script") ||
|
||||
read_arg(ctx, args, arg, "dynamic-list"))
|
||||
continue;
|
||||
|
||||
MappedFile<Context<E>> *mf = nullptr;
|
||||
|
||||
if (read_arg(ctx, args, arg, "l")) {
|
||||
mf = find_library(ctx, std::string(arg));
|
||||
} else {
|
||||
if (arg.starts_with('-'))
|
||||
continue;
|
||||
arg = args[0];
|
||||
args = args.subspan(1);
|
||||
mf = MappedFile<Context<E>>::must_open(ctx, std::string(arg));
|
||||
}
|
||||
|
||||
switch (get_file_type(mf)) {
|
||||
case FileType::ELF_OBJ:
|
||||
files.emplace_back(new RObjectFile<E>(ctx, *mf, true));
|
||||
break;
|
||||
case FileType::AR:
|
||||
case FileType::THIN_AR:
|
||||
for (MappedFile<Context<E>> *child : read_archive_members(ctx, mf))
|
||||
if (get_file_type(child) == FileType::ELF_OBJ)
|
||||
files.emplace_back(new RObjectFile<E>(ctx, *child, whole_archive));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static i64 assign_offsets(Context<E> &ctx) {
|
||||
i64 offset = 0;
|
||||
for (RChunk<E> *chunk : ctx.r_chunks) {
|
||||
offset = align_to(offset, chunk->out_shdr.sh_addralign);
|
||||
chunk->out_shdr.sh_offset = offset;
|
||||
offset += chunk->out_shdr.sh_size;
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
static bool contains(std::unordered_set<std::string_view> &a,
|
||||
std::unordered_set<std::string_view> &b) {
|
||||
for (std::string_view x : b)
|
||||
if (a.contains(x))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void combine_objects(Context<E> &ctx, std::span<std::string_view> file_args) {
|
||||
// Read object files
|
||||
std::vector<std::unique_ptr<RObjectFile<E>>> files = open_files(ctx, file_args);
|
||||
|
||||
// Identify needed objects
|
||||
std::unordered_set<std::string_view> undef_syms;
|
||||
auto add_syms = [&](RObjectFile<E> &file) {
|
||||
undef_syms.insert(file.undef_syms.begin(), file.undef_syms.end());
|
||||
for (std::string_view name : file.defined_syms)
|
||||
undef_syms.erase(name);
|
||||
file.is_alive = true;
|
||||
};
|
||||
|
||||
for (std::unique_ptr<RObjectFile<E>> &file : files)
|
||||
if (file->is_alive)
|
||||
add_syms(*file);
|
||||
|
||||
for (;;) {
|
||||
bool added = false;
|
||||
for (std::unique_ptr<RObjectFile<E>> &file : files) {
|
||||
if (!file->is_alive && contains(undef_syms, file->defined_syms)) {
|
||||
add_syms(*file);
|
||||
added = true;
|
||||
}
|
||||
}
|
||||
if (!added)
|
||||
break;
|
||||
}
|
||||
|
||||
std::erase_if(files, [](std::unique_ptr<RObjectFile<E>> &file) {
|
||||
return !file->is_alive;
|
||||
});
|
||||
|
||||
// Remove duplicate comdat groups
|
||||
std::unordered_set<std::string_view> comdat_groups;
|
||||
for (std::unique_ptr<RObjectFile<E>> &file : files)
|
||||
file->remove_comdats(ctx, comdat_groups);
|
||||
|
||||
// Create headers and linker-synthesized sections
|
||||
ROutputEhdr<E> ehdr;
|
||||
ROutputShdr<E> shdr;
|
||||
RSymtabSection<E> symtab;
|
||||
RStrtabSection<E> shstrtab(".shstrtab");
|
||||
RStrtabSection<E> strtab(".strtab");
|
||||
|
||||
ctx.r_chunks.push_back(&ehdr);
|
||||
ctx.r_chunks.push_back(&shstrtab);
|
||||
ctx.r_chunks.push_back(&strtab);
|
||||
|
||||
ctx.r_ehdr = &ehdr;
|
||||
ctx.r_shdr = &shdr;
|
||||
ctx.r_shstrtab = &shstrtab;
|
||||
ctx.r_strtab = &strtab;
|
||||
ctx.r_symtab = &symtab;
|
||||
|
||||
// Add input sections to output sections
|
||||
for (std::unique_ptr<RObjectFile<E>> &file : files)
|
||||
for (std::unique_ptr<RInputSection<E>> &sec : file->sections)
|
||||
if (sec)
|
||||
ctx.r_chunks.push_back(sec.get());
|
||||
|
||||
ctx.r_chunks.push_back(&symtab);
|
||||
ctx.r_chunks.push_back(&shdr);
|
||||
|
||||
// Assign output section indices
|
||||
i64 shndx = 1;
|
||||
for (RChunk<E> *chunk : ctx.r_chunks)
|
||||
if (chunk != &ehdr && chunk != &shdr)
|
||||
chunk->shndx = shndx++;
|
||||
|
||||
// Add section names to .shstrtab
|
||||
for (RChunk<E> *chunk : ctx.r_chunks)
|
||||
if (chunk->shndx)
|
||||
chunk->out_shdr.sh_name = shstrtab.add_string(chunk->name);
|
||||
|
||||
// Copy symbols from input objects to an output object
|
||||
for (std::unique_ptr<RObjectFile<E>> &file : files)
|
||||
for (i64 i = 1; i < file->first_global; i++)
|
||||
symtab.add_local_symbol(ctx, *file, i);
|
||||
|
||||
symtab.out_shdr.sh_info = symtab.syms.size();
|
||||
|
||||
for (std::unique_ptr<RObjectFile<E>> &file : files)
|
||||
for (i64 i = file->first_global; i < file->syms.size(); i++)
|
||||
symtab.add_global_symbol(ctx, *file, i);
|
||||
|
||||
// Finalize section header
|
||||
for (RChunk<E> *chunk : ctx.r_chunks)
|
||||
chunk->update_shdr(ctx);
|
||||
|
||||
// Open an output file
|
||||
i64 filesize = assign_offsets(ctx);
|
||||
std::unique_ptr<OutputFile<E>> out =
|
||||
OutputFile<E>::open(ctx, ctx.arg.output, filesize, 0666);
|
||||
memset(out->buf, 0, filesize);
|
||||
ctx.buf = out->buf;
|
||||
|
||||
// Write to the output file
|
||||
for (RChunk<E> *chunk : ctx.r_chunks)
|
||||
chunk->write_to(ctx);
|
||||
out->close(ctx);
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template void combine_objects(Context<E> &, std::span<std::string_view>);
|
||||
|
||||
INSTANTIATE(X86_64);
|
||||
INSTANTIATE(I386);
|
||||
INSTANTIATE(ARM64);
|
||||
|
||||
} // namespace mold::elf
|
||||
@@ -0,0 +1,325 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <signal.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef __APPLE__
|
||||
# define COMMON_DIGEST_FOR_OPENSSL
|
||||
# include <CommonCrypto/CommonDigest.h>
|
||||
#else
|
||||
# include <openssl/sha.h>
|
||||
#endif
|
||||
|
||||
#define DAEMON_TIMEOUT 30
|
||||
|
||||
namespace mold::elf {
|
||||
|
||||
// Exiting from a program with large memory usage is slow --
|
||||
// it may take a few hundred milliseconds. To hide the latency,
|
||||
// we fork a child and let it do the actual linking work.
|
||||
std::function<void()> fork_child() {
|
||||
int pipefd[2];
|
||||
if (pipe(pipefd) == -1) {
|
||||
perror("pipe");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid == -1) {
|
||||
perror("fork");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (pid > 0) {
|
||||
// Parent
|
||||
close(pipefd[1]);
|
||||
|
||||
char buf[1];
|
||||
if (read(pipefd[0], buf, 1) == 1)
|
||||
_exit(0);
|
||||
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
|
||||
if (WIFEXITED(status))
|
||||
_exit(WEXITSTATUS(status));
|
||||
if (WIFSIGNALED(status))
|
||||
raise(WTERMSIG(status));
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
// Child
|
||||
close(pipefd[0]);
|
||||
|
||||
return [=]() {
|
||||
char buf[] = {1};
|
||||
int n = write(pipefd[1], buf, 1);
|
||||
assert(n == 1);
|
||||
};
|
||||
}
|
||||
|
||||
static std::string base64(u8 *data, u64 size) {
|
||||
static const char chars[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_";
|
||||
|
||||
std::ostringstream out;
|
||||
|
||||
auto encode = [&](u32 x) {
|
||||
out << chars[x & 0b111111]
|
||||
<< chars[(x >> 6) & 0b111111]
|
||||
<< chars[(x >> 12) & 0b111111]
|
||||
<< chars[(x >> 18) & 0b111111];
|
||||
};
|
||||
|
||||
i64 i = 0;
|
||||
for (; i < size - 3; i += 3)
|
||||
encode((data[i + 2] << 16) | (data[i + 1] << 8) | data[i]);
|
||||
|
||||
if (i == size - 1)
|
||||
encode(data[i]);
|
||||
else if (i == size - 2)
|
||||
encode((data[i + 1] << 8) | data[i]);
|
||||
return out.str();
|
||||
}
|
||||
|
||||
static std::string compute_sha256(std::span<std::string_view> argv) {
|
||||
SHA256_CTX sha;
|
||||
SHA256_Init(&sha);
|
||||
|
||||
for (std::string_view arg : argv) {
|
||||
if (arg != "-preload" && arg != "--preload") {
|
||||
SHA256_Update(&sha, arg.data(), arg.size());
|
||||
char buf[] = {0};
|
||||
SHA256_Update(&sha, buf, 1);
|
||||
}
|
||||
}
|
||||
|
||||
u8 digest[SHA256_SIZE];
|
||||
SHA256_Final(digest, &sha);
|
||||
return base64(digest, SHA256_SIZE);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void send_fd(Context<E> &ctx, i64 conn, i64 fd) {
|
||||
struct iovec iov;
|
||||
char dummy = '1';
|
||||
iov.iov_base = &dummy;
|
||||
iov.iov_len = 1;
|
||||
|
||||
struct msghdr msg = {};
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
|
||||
char buf[CMSG_SPACE(sizeof(int))];
|
||||
msg.msg_control = buf;
|
||||
msg.msg_controllen = CMSG_LEN(sizeof(int));
|
||||
|
||||
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SCM_RIGHTS;
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
|
||||
*(int *)CMSG_DATA(cmsg) = fd;
|
||||
|
||||
if (sendmsg(conn, &msg, 0) == -1)
|
||||
Fatal(ctx) << "sendmsg failed: " << errno_string();
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static i64 recv_fd(Context<E> &ctx, i64 conn) {
|
||||
struct iovec iov;
|
||||
char buf[1];
|
||||
iov.iov_base = buf;
|
||||
iov.iov_len = sizeof(buf);
|
||||
|
||||
struct msghdr msg = {};
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
|
||||
char cmsgbuf[CMSG_SPACE(sizeof(int))];
|
||||
msg.msg_control = (caddr_t)cmsgbuf;
|
||||
msg.msg_controllen = sizeof(cmsgbuf);
|
||||
|
||||
i64 len = recvmsg(conn, &msg, 0);
|
||||
if (len <= 0)
|
||||
Fatal(ctx) << "recvmsg failed: " << errno_string();
|
||||
|
||||
struct cmsghdr *cmsg;
|
||||
cmsg = CMSG_FIRSTHDR(&msg);
|
||||
return *(int *)CMSG_DATA(cmsg);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void try_resume_daemon(Context<E> &ctx) {
|
||||
i64 conn = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (conn == -1)
|
||||
Fatal(ctx) << "socket failed: " << errno_string();
|
||||
|
||||
std::string path = "/tmp/mold-" + compute_sha256(ctx.cmdline_args);
|
||||
|
||||
struct sockaddr_un name = {};
|
||||
name.sun_family = AF_UNIX;
|
||||
memcpy(name.sun_path, path.data(), path.size());
|
||||
|
||||
if (connect(conn, (struct sockaddr *)&name, sizeof(name)) != 0) {
|
||||
close(conn);
|
||||
return;
|
||||
}
|
||||
|
||||
send_fd(ctx, conn, STDOUT_FILENO);
|
||||
send_fd(ctx, conn, STDERR_FILENO);
|
||||
|
||||
char buf[1];
|
||||
i64 r = read(conn, buf, 1);
|
||||
close(conn);
|
||||
if (r == 1)
|
||||
exit(0);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void daemonize(Context<E> &ctx, std::function<void()> *wait_for_client,
|
||||
std::function<void()> *on_complete) {
|
||||
if (daemon(1, 0) == -1)
|
||||
Fatal(ctx) << "daemon failed: " << errno_string();
|
||||
|
||||
i64 sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sock == -1)
|
||||
Fatal(ctx) << "socket failed: " << errno_string();
|
||||
|
||||
socket_tmpfile =
|
||||
strdup(("/tmp/mold-" + compute_sha256(ctx.cmdline_args)).c_str());
|
||||
|
||||
struct sockaddr_un name = {};
|
||||
name.sun_family = AF_UNIX;
|
||||
strcpy(name.sun_path, socket_tmpfile);
|
||||
|
||||
u32 orig_mask = umask(0177);
|
||||
|
||||
if (bind(sock, (struct sockaddr *)&name, sizeof(name)) == -1) {
|
||||
if (errno != EADDRINUSE)
|
||||
Fatal(ctx) << "bind failed: " << errno_string();
|
||||
|
||||
unlink(socket_tmpfile);
|
||||
if (bind(sock, (struct sockaddr *)&name, sizeof(name)) == -1)
|
||||
Fatal(ctx) << "bind failed: " << errno_string();
|
||||
}
|
||||
|
||||
umask(orig_mask);
|
||||
|
||||
if (listen(sock, 0) == -1)
|
||||
Fatal(ctx) << "listen failed: " << errno_string();
|
||||
|
||||
static i64 conn = -1;
|
||||
|
||||
*wait_for_client = [=, &ctx]() {
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(sock, &rfds);
|
||||
|
||||
struct timeval tv;
|
||||
tv.tv_sec = DAEMON_TIMEOUT;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
i64 res = select(sock + 1, &rfds, NULL, NULL, &tv);
|
||||
if (res == -1)
|
||||
Fatal(ctx) << "select failed: " << errno_string();
|
||||
|
||||
if (res == 0) {
|
||||
std::cout << "timeout\n";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
conn = accept(sock, NULL, NULL);
|
||||
if (conn == -1)
|
||||
Fatal(ctx) << "accept failed: " << errno_string();
|
||||
unlink(socket_tmpfile);
|
||||
|
||||
dup2(recv_fd(ctx, conn), STDOUT_FILENO);
|
||||
dup2(recv_fd(ctx, conn), STDERR_FILENO);
|
||||
};
|
||||
|
||||
*on_complete = [=]() {
|
||||
char buf[] = {1};
|
||||
int n = write(conn, buf, 1);
|
||||
assert(n == 1);
|
||||
};
|
||||
}
|
||||
|
||||
static std::string get_self_path() {
|
||||
return std::filesystem::read_symlink("/proc/self/exe");
|
||||
}
|
||||
|
||||
static bool is_regular_file(const std::string &path) {
|
||||
struct stat st;
|
||||
return !stat(path.c_str(), &st) && (st.st_mode & S_IFMT) == S_IFREG;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
std::string find_dso(Context<E> &ctx, std::filesystem::path self) {
|
||||
// Look for mold-wrapper.so from the same directory as the executable is.
|
||||
std::filesystem::path path = self.parent_path() / "mold-wrapper.so";
|
||||
std::error_code ec;
|
||||
if (std::filesystem::is_regular_file(path, ec) && !ec)
|
||||
return path;
|
||||
|
||||
#ifdef LIBDIR
|
||||
// If not found, search $(LIBDIR)/mold, which is /usr/local/lib/mold
|
||||
// by default.
|
||||
path = LIBDIR "/mold/mold-wrapper.so";
|
||||
if (std::filesystem::is_regular_file(path, ec) && !ec)
|
||||
return path;
|
||||
#endif
|
||||
|
||||
Fatal(ctx) << "mold-wrapper.so is missing";
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
[[noreturn]]
|
||||
void process_run_subcommand(Context<E> &ctx, int argc, char **argv) {
|
||||
std::string_view arg1 = argv[1];
|
||||
assert(arg1 == "-run" || arg1 == "--run");
|
||||
if (!argv[2])
|
||||
Fatal(ctx) << "-run: argument missing";
|
||||
|
||||
// Get the mold-wrapper.so path
|
||||
std::string self = get_self_path();
|
||||
std::string dso_path = find_dso(ctx, self);
|
||||
|
||||
// Set environment variables
|
||||
putenv(strdup(("LD_PRELOAD=" + dso_path).c_str()));
|
||||
putenv(strdup(("MOLD_PATH=" + self).c_str()));
|
||||
|
||||
// If ld, ld.lld or ld.gold is specified, run mold itself
|
||||
if (std::string cmd = filepath(argv[2]).filename();
|
||||
cmd == "ld" || cmd == "ld.lld" || cmd == "ld.gold") {
|
||||
std::vector<char *> args;
|
||||
args.push_back(argv[0]);
|
||||
args.insert(args.end(), argv + 3, argv + argc);
|
||||
args.push_back(nullptr);
|
||||
execv(self.c_str(), args.data());
|
||||
Fatal(ctx) << "mold -run failed: " << self << ": " << errno_string();
|
||||
}
|
||||
|
||||
// Execute a given command
|
||||
execvp(argv[2], argv + 2);
|
||||
Fatal(ctx) << "mold -run failed: " << argv[2] << ": " << errno_string();
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template void try_resume_daemon(Context<E> &); \
|
||||
template void daemonize(Context<E> &, std::function<void()> *, \
|
||||
std::function<void()> *); \
|
||||
template void process_run_subcommand(Context<E> &, int, char **)
|
||||
|
||||
INSTANTIATE(X86_64);
|
||||
INSTANTIATE(I386);
|
||||
INSTANTIATE(ARM64);
|
||||
|
||||
} // namespace mold::elf
|
||||
@@ -1,75 +1,65 @@
|
||||
// This file implements the Aho-Corasick algorithm to match multiple
|
||||
// glob patterns to symbol strings as quickly as possible.
|
||||
// This file implements the Aho-Corasick algorithm to match version
|
||||
// script patterns to symbol strings as quickly as possible.
|
||||
//
|
||||
// Here are some examples of glob patterns:
|
||||
// Here are some examples of version script patterns:
|
||||
//
|
||||
// qt_private_api_tag*
|
||||
// *16QAccessibleCache*
|
||||
// *32QAbstractFileIconProviderPrivate*
|
||||
// *17QPixmapIconEngine*
|
||||
//
|
||||
// `*` is a wildcard that matches any substring. We sometimes have
|
||||
// hundreds of glob patterns and have to match them against millions
|
||||
// of symbol strings.
|
||||
// The pattern is a glob pattern, so `*` is a wildcard that matches
|
||||
// any substring. We sometimes have hundreds of version script
|
||||
// patterns and have to match them against millions of symbol strings.
|
||||
//
|
||||
// Aho-Corasick cannot handle complex patterns such as `*foo*bar*`.
|
||||
// We handle such patterns with the Glob class. Glob is relatively
|
||||
// slow, but complex patterns are rare in practice, so it should be
|
||||
// OK.
|
||||
// We handle such patterns with GlobPattern. GlobPattern is relatively
|
||||
// slow, but complex patterns are rare in practice, so it should be OK.
|
||||
|
||||
#include "common.h"
|
||||
#include "mold.h"
|
||||
|
||||
#include <queue>
|
||||
#include <regex>
|
||||
|
||||
namespace mold {
|
||||
namespace mold::elf {
|
||||
|
||||
std::optional<i64> MultiGlob::find(std::string_view str) {
|
||||
std::call_once(once, [&] { compile(); });
|
||||
i64 val = -1;
|
||||
std::optional<u16> VersionMatcher::find(std::string_view str) {
|
||||
u32 idx = UINT32_MAX;
|
||||
|
||||
// Match against simple glob patterns
|
||||
if (root)
|
||||
val = find_aho_corasick(str);
|
||||
if (root) {
|
||||
std::call_once(once_flag, [&]() { compile(); });
|
||||
|
||||
// Match against complex glob patterns
|
||||
for (std::pair<Glob, i64> &glob : globs)
|
||||
if (glob.first.match(str))
|
||||
val = std::max(val, glob.second);
|
||||
// Match against simple glob patterns
|
||||
TrieNode *node = root.get();
|
||||
|
||||
if (val == -1)
|
||||
return {};
|
||||
return val;
|
||||
}
|
||||
auto walk = [&](u8 c) {
|
||||
for (;;) {
|
||||
if (node->children[c]) {
|
||||
node = node->children[c].get();
|
||||
idx = std::min(idx, node->value);
|
||||
return;
|
||||
}
|
||||
|
||||
i64 MultiGlob::find_aho_corasick(std::string_view str) {
|
||||
TrieNode *node = root.get();
|
||||
i64 val = -1;
|
||||
|
||||
auto walk = [&](u8 c) {
|
||||
for (;;) {
|
||||
if (node->children[c]) {
|
||||
node = node->children[c].get();
|
||||
val = std::max(val, node->value);
|
||||
return;
|
||||
if (!node->suffix_link)
|
||||
return;
|
||||
node = node->suffix_link;
|
||||
}
|
||||
};
|
||||
|
||||
if (!node->suffix_link)
|
||||
return;
|
||||
node = node->suffix_link;
|
||||
}
|
||||
};
|
||||
|
||||
walk('\0');
|
||||
|
||||
for (u8 c : str) {
|
||||
if (prefix_match && node == root.get())
|
||||
return val;
|
||||
walk(c);
|
||||
walk('\0');
|
||||
for (u8 c : str)
|
||||
walk(c);
|
||||
walk('\0');
|
||||
}
|
||||
|
||||
walk('\0');
|
||||
return val;
|
||||
// Match against complex glob patterns
|
||||
for (std::pair<GlobPattern, u32> &glob : globs)
|
||||
if (glob.first.match(str))
|
||||
idx = std::min(idx, glob.second);
|
||||
|
||||
if (idx == UINT32_MAX)
|
||||
return {};
|
||||
return versions[idx];
|
||||
}
|
||||
|
||||
static bool is_simple_pattern(std::string_view pat) {
|
||||
@@ -92,16 +82,18 @@ static std::string handle_stars(std::string_view pat) {
|
||||
return "\0"s + str + "\0"s;
|
||||
}
|
||||
|
||||
bool MultiGlob::add(std::string_view pat, i64 val) {
|
||||
assert(!is_compiled);
|
||||
bool VersionMatcher::add(std::string_view pat, u16 ver) {
|
||||
assert(!compiled);
|
||||
assert(!pat.empty());
|
||||
|
||||
strings.emplace_back(pat);
|
||||
u32 idx = strings.size();
|
||||
strings.push_back(std::string(pat));
|
||||
versions.push_back(ver);
|
||||
|
||||
// Complex glob pattern
|
||||
if (!is_simple_pattern(pat)) {
|
||||
if (std::optional<Glob> glob = Glob::compile(pat)) {
|
||||
globs.emplace_back(std::move(*glob), val);
|
||||
if (std::optional<GlobPattern> glob = GlobPattern::compile(pat)) {
|
||||
globs.push_back({std::move(*glob), idx});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -118,29 +110,17 @@ bool MultiGlob::add(std::string_view pat, i64 val) {
|
||||
node = node->children[c].get();
|
||||
}
|
||||
|
||||
node->value = std::max(node->value, val);
|
||||
node->value = std::min(node->value, idx);
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultiGlob::compile() {
|
||||
is_compiled = true;
|
||||
if (root) {
|
||||
fix_suffix_links(*root);
|
||||
fix_values();
|
||||
|
||||
// If no pattern starts with '*', set prefix_match to true.
|
||||
// We'll use this flag for optimization.
|
||||
prefix_match = true;
|
||||
for (i64 i = 1; i < 256; i++) {
|
||||
if (root->children[i]) {
|
||||
prefix_match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
void VersionMatcher::compile() {
|
||||
fix_suffix_links(*root);
|
||||
fix_values();
|
||||
compiled = true;
|
||||
}
|
||||
|
||||
void MultiGlob::fix_suffix_links(TrieNode &node) {
|
||||
void VersionMatcher::fix_suffix_links(TrieNode &node) {
|
||||
for (i64 i = 0; i < 256; i++) {
|
||||
if (!node.children[i])
|
||||
continue;
|
||||
@@ -166,7 +146,7 @@ void MultiGlob::fix_suffix_links(TrieNode &node) {
|
||||
}
|
||||
}
|
||||
|
||||
void MultiGlob::fix_values() {
|
||||
void VersionMatcher::fix_values() {
|
||||
std::queue<TrieNode *> queue;
|
||||
queue.push(root.get());
|
||||
|
||||
@@ -177,10 +157,10 @@ void MultiGlob::fix_values() {
|
||||
for (std::unique_ptr<TrieNode> &child : node->children) {
|
||||
if (!child)
|
||||
continue;
|
||||
child->value = std::max(child->value, child->suffix_link->value);
|
||||
child->value = std::min(child->value, child->suffix_link->value);
|
||||
queue.push(child.get());
|
||||
}
|
||||
} while (!queue.empty());
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
} // namespace mold::elf
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace mold {
|
||||
|
||||
std::string get_realpath(std::string_view path) {
|
||||
std::error_code ec;
|
||||
std::string ret = std::filesystem::canonical(path, ec);
|
||||
return ec ? std::string(path) : ret;
|
||||
}
|
||||
|
||||
// Removes redundant '/..' or '/.' from a given path.
|
||||
// The transformation is done purely by lexical processing.
|
||||
// This function does not access file system.
|
||||
std::string path_clean(std::string_view path) {
|
||||
return filepath(path).lexically_normal();
|
||||
}
|
||||
|
||||
std::filesystem::path to_abs_path(std::filesystem::path path) {
|
||||
if (path.is_absolute())
|
||||
return path;
|
||||
return (std::filesystem::current_path() / path).lexically_normal();
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
#pragma once
|
||||
|
||||
#include "mold.h"
|
||||
|
||||
namespace mold {
|
||||
|
||||
enum class FileType {
|
||||
UNKNOWN,
|
||||
EMPTY,
|
||||
ELF_OBJ,
|
||||
ELF_DSO,
|
||||
MACH_OBJ,
|
||||
MACH_DYLIB,
|
||||
MACH_UNIVERSAL,
|
||||
AR,
|
||||
THIN_AR,
|
||||
TAPI,
|
||||
TEXT,
|
||||
LLVM_BITCODE,
|
||||
};
|
||||
|
||||
template <typename C>
|
||||
bool is_text_file(MappedFile<C> *mf) {
|
||||
u8 *data = mf->data;
|
||||
return mf->size >= 4 && isprint(data[0]) && isprint(data[1]) &&
|
||||
isprint(data[2]) && isprint(data[3]);
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
FileType get_file_type(MappedFile<C> *mf) {
|
||||
std::string_view data = mf->get_contents();
|
||||
|
||||
if (data.empty())
|
||||
return FileType::EMPTY;
|
||||
|
||||
if (data.starts_with("\177ELF")) {
|
||||
switch (*(u16 *)(data.data() + 16)) {
|
||||
case 1: // ET_REL
|
||||
return FileType::ELF_OBJ;
|
||||
case 3: // ET_DYN
|
||||
return FileType::ELF_DSO;
|
||||
}
|
||||
return FileType::UNKNOWN;
|
||||
}
|
||||
|
||||
if (data.starts_with("\xcf\xfa\xed\xfe")) {
|
||||
switch (*(u32 *)(data.data() + 12)) {
|
||||
case 1: // MH_OBJECT
|
||||
return FileType::MACH_OBJ;
|
||||
case 6: // MH_DYLIB
|
||||
return FileType::MACH_DYLIB;
|
||||
}
|
||||
return FileType::UNKNOWN;
|
||||
}
|
||||
|
||||
if (data.starts_with("!<arch>\n"))
|
||||
return FileType::AR;
|
||||
if (data.starts_with("!<thin>\n"))
|
||||
return FileType::THIN_AR;
|
||||
if (data.starts_with("--- !tapi-tbd"))
|
||||
return FileType::TAPI;
|
||||
if (data.starts_with("\xca\xfe\xba\xbe"))
|
||||
return FileType::MACH_UNIVERSAL;
|
||||
if (is_text_file(mf))
|
||||
return FileType::TEXT;
|
||||
if (data.starts_with("\xde\xc0\x17\x0b"))
|
||||
return FileType::LLVM_BITCODE;
|
||||
if (data.starts_with("BC\xc0\xde"))
|
||||
return FileType::LLVM_BITCODE;
|
||||
return FileType::UNKNOWN;
|
||||
}
|
||||
|
||||
inline std::string filetype_to_string(FileType type) {
|
||||
switch (type) {
|
||||
case FileType::UNKNOWN: return "UNKNOWN";
|
||||
case FileType::EMPTY: return "EMPTY";
|
||||
case FileType::ELF_OBJ: return "ELF_OBJ";
|
||||
case FileType::ELF_DSO: return "ELF_DSO";
|
||||
case FileType::MACH_OBJ: return "MACH_OBJ";
|
||||
case FileType::MACH_DYLIB: return "MACH_DYLIB";
|
||||
case FileType::MACH_UNIVERSAL: return "MACH_UNIVERSAL";
|
||||
case FileType::AR: return "AR";
|
||||
case FileType::THIN_AR: return "THIN_AR";
|
||||
case FileType::TAPI: return "TAPI";
|
||||
case FileType::TEXT: return "TEXT";
|
||||
case FileType::LLVM_BITCODE: return "LLVM_BITCODE";
|
||||
}
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &out, FileType type) {
|
||||
out << filetype_to_string(type);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -4,7 +4,7 @@
|
||||
// For more info, read
|
||||
// https://engineering.fb.com/2018/12/13/data-infrastructure/hyperloglog
|
||||
|
||||
#include "common.h"
|
||||
#include "mold.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace mold {
|
||||
i64 HyperLogLog::get_cardinality() const {
|
||||
double z = 0;
|
||||
for (i64 val : buckets)
|
||||
z += std::ldexp(1.0, -val);
|
||||
z += pow(2, -val);
|
||||
return ALPHA * NBUCKETS * NBUCKETS / z;
|
||||
}
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
#!/bin/sh
|
||||
# This script installs binary packages needed to build mold.
|
||||
# Feel free to send me a PR if your OS is not on this list.
|
||||
|
||||
set -e
|
||||
. /etc/os-release
|
||||
|
||||
set -x
|
||||
|
||||
case "$ID" in
|
||||
ubuntu | pop | linuxmint | debian | raspbian | neon)
|
||||
apt-get update
|
||||
apt-get install -y cmake gcc g++ clang gdb
|
||||
;;
|
||||
fedora | amzn | rhel | centos)
|
||||
dnf install -y gcc-g++ cmake glibc-static libstdc++-static diffutils util-linux tar
|
||||
;;
|
||||
rocky)
|
||||
dnf install -y gcc-g++ cmake diffutils util-linux
|
||||
;;
|
||||
opensuse-*)
|
||||
zypper install -y make cmake gcc-c++ glibc-devel-static tar diffutils util-linux
|
||||
;;
|
||||
gentoo)
|
||||
emerge-webrsync
|
||||
FEATURES='getbinpkg binpkg-request-signature' emerge dev-build/cmake
|
||||
;;
|
||||
arch | archarm | artix | endeavouros)
|
||||
pacman -Sy --needed --noconfirm base-devel cmake util-linux
|
||||
;;
|
||||
void)
|
||||
xbps-install -Sy xbps bash make cmake gcc tar diffutils util-linux
|
||||
;;
|
||||
alpine)
|
||||
apk update
|
||||
apk add bash make linux-headers cmake gcc g++
|
||||
;;
|
||||
clear-linux-os)
|
||||
swupd update
|
||||
swupd bundle-add c-basic diffutils
|
||||
;;
|
||||
almalinux)
|
||||
dnf install -y gcc-toolset-13-gcc-c++ gcc-toolset-13-libstdc++-devel cmake diffutils
|
||||
;;
|
||||
freebsd)
|
||||
pkg update
|
||||
pkg install -y cmake bash binutils gcc
|
||||
;;
|
||||
*)
|
||||
echo "Error: don't know anything about build dependencies on $ID-$VERSION_ID"
|
||||
exit 1
|
||||
esac
|
||||
@@ -1,19 +0,0 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
. /etc/os-release
|
||||
|
||||
set -x
|
||||
|
||||
# This script install packages for -DMOLD_ENABLE_QEMU_TESTS=1
|
||||
# to enable cross-target tests.
|
||||
#
|
||||
# Feel free to send me a PR if your OS is not on this list.
|
||||
|
||||
case "$ID" in
|
||||
ubuntu | pop | linuxmint | debian | raspbian)
|
||||
apt-get install -y qemu-user {gcc,g++}-{i686,aarch64,riscv64,powerpc,powerpc64,powerpc64le,s390x,sparc64,m68k,sh4,arc}-linux-gnu {gcc,g++}-arm-linux-gnueabihf
|
||||
;;
|
||||
*)
|
||||
echo "Error: don't know anything about build dependencies on $ID-$VERSION_ID"
|
||||
exit 1
|
||||
esac
|
||||
-979
@@ -1,979 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "config.h"
|
||||
#include "integers.h"
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <bit>
|
||||
#include <bitset>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <tbb/concurrent_vector.h>
|
||||
#include <tbb/enumerable_thread_specific.h>
|
||||
#include <tbb/parallel_for.h>
|
||||
#include <vector>
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <io.h>
|
||||
#else
|
||||
# include <sys/mman.h>
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#define XXH_INLINE_ALL 1
|
||||
#include "../third-party/xxhash/xxhash.h"
|
||||
|
||||
#ifdef NDEBUG
|
||||
# define unreachable() __builtin_unreachable()
|
||||
#else
|
||||
# define unreachable() assert(0 && "unreachable")
|
||||
#endif
|
||||
|
||||
inline uint64_t hash_string(std::string_view str) {
|
||||
return XXH3_64bits(str.data(), str.size());
|
||||
}
|
||||
|
||||
class HashCmp {
|
||||
public:
|
||||
static size_t hash(const std::string_view &k) {
|
||||
return hash_string(k);
|
||||
}
|
||||
|
||||
static bool equal(const std::string_view &k1, const std::string_view &k2) {
|
||||
return k1 == k2;
|
||||
}
|
||||
};
|
||||
|
||||
namespace mold {
|
||||
|
||||
namespace ranges = std::ranges;
|
||||
using namespace std::literals::string_literals;
|
||||
using namespace std::literals::string_view_literals;
|
||||
|
||||
inline char *output_tmpfile;
|
||||
|
||||
inline u8 *output_buffer_start = nullptr;
|
||||
inline u8 *output_buffer_end = nullptr;
|
||||
|
||||
extern std::string mold_git_hash;
|
||||
|
||||
std::string errno_string();
|
||||
void cleanup();
|
||||
void install_signal_handler();
|
||||
|
||||
inline u64 combine_hash(u64 a, u64 b) {
|
||||
return a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2));
|
||||
}
|
||||
|
||||
//
|
||||
// Error output
|
||||
//
|
||||
|
||||
// Some C++ stdlibs don't support std::osyncstream even though
|
||||
// it's is in the C++20 standard. So we implement it ourselves.
|
||||
class SyncStream {
|
||||
public:
|
||||
SyncStream(std::ostream &out) : out(out) {}
|
||||
|
||||
~SyncStream() {
|
||||
emit();
|
||||
}
|
||||
|
||||
template <typename T> SyncStream &operator<<(T &&val) {
|
||||
ss << std::forward<T>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void emit() {
|
||||
if (emitted)
|
||||
return;
|
||||
|
||||
std::scoped_lock lock(mu);
|
||||
out << ss.str() << '\n';
|
||||
emitted = true;
|
||||
}
|
||||
|
||||
private:
|
||||
std::ostream &out;
|
||||
std::stringstream ss;
|
||||
bool emitted = false;
|
||||
static inline std::mutex mu;
|
||||
};
|
||||
|
||||
template <typename Context>
|
||||
class Out {
|
||||
public:
|
||||
Out(Context &ctx) {}
|
||||
|
||||
template <typename T> Out &operator<<(T &&val) {
|
||||
out << std::forward<T>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
SyncStream out{std::cout};
|
||||
};
|
||||
|
||||
static std::string_view fatal_mono = "mold: fatal: ";
|
||||
static std::string_view fatal_color = "mold: \033[0;1;31mfatal:\033[0m ";
|
||||
static std::string_view error_mono = "mold: error: ";
|
||||
static std::string_view error_color = "mold: \033[0;1;31merror:\033[0m ";
|
||||
static std::string_view warning_mono = "mold: warning: ";
|
||||
static std::string_view warning_color = "mold: \033[0;1;35mwarning:\033[0m ";
|
||||
|
||||
template <typename Context>
|
||||
class Fatal {
|
||||
public:
|
||||
Fatal(Context &ctx) {
|
||||
out << (ctx.arg.color_diagnostics ? fatal_color : fatal_mono);
|
||||
}
|
||||
|
||||
[[noreturn]] ~Fatal() {
|
||||
out.emit();
|
||||
cleanup();
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
template <typename T> Fatal &operator<<(T &&val) {
|
||||
out << std::forward<T>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
SyncStream out{std::cerr};
|
||||
};
|
||||
|
||||
template <typename Context>
|
||||
class Error {
|
||||
public:
|
||||
Error(Context &ctx) {
|
||||
if (ctx.arg.noinhibit_exec) {
|
||||
out << (ctx.arg.color_diagnostics ? warning_color : warning_mono);
|
||||
} else {
|
||||
out << (ctx.arg.color_diagnostics ? error_color : error_mono);
|
||||
ctx.has_error = true;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T> Error &operator<<(T &&val) {
|
||||
out << std::forward<T>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
SyncStream out{std::cerr};
|
||||
};
|
||||
|
||||
template <typename Context>
|
||||
class Warn {
|
||||
public:
|
||||
Warn(Context &ctx) {
|
||||
if (ctx.arg.suppress_warnings)
|
||||
return;
|
||||
|
||||
out.emplace(std::cerr);
|
||||
|
||||
if (ctx.arg.fatal_warnings) {
|
||||
*out << (ctx.arg.color_diagnostics ? error_color : error_mono);
|
||||
ctx.has_error = true;
|
||||
} else {
|
||||
*out << (ctx.arg.color_diagnostics ? warning_color : warning_mono);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T> Warn &operator<<(T &&val) {
|
||||
if (out)
|
||||
*out << std::forward<T>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
std::optional<SyncStream> out;
|
||||
};
|
||||
|
||||
//
|
||||
// Atomics
|
||||
//
|
||||
|
||||
// This is the same as std::atomic except that the default memory
|
||||
// order is relaxed instead of sequential consistency.
|
||||
template <typename T>
|
||||
struct Atomic : std::atomic<T> {
|
||||
static constexpr std::memory_order relaxed = std::memory_order_relaxed;
|
||||
|
||||
using std::atomic<T>::atomic;
|
||||
|
||||
Atomic(const Atomic<T> &other) : std::atomic<T>(other.load()) {}
|
||||
|
||||
Atomic<T> &operator=(const Atomic<T> &other) {
|
||||
store(other.load());
|
||||
return *this;
|
||||
}
|
||||
|
||||
void operator=(T val) { store(val); }
|
||||
operator T() const { return load(); }
|
||||
|
||||
void store(T val, std::memory_order order = relaxed) {
|
||||
std::atomic<T>::store(val, order);
|
||||
}
|
||||
|
||||
T load(std::memory_order order = relaxed) const {
|
||||
return std::atomic<T>::load(order);
|
||||
}
|
||||
|
||||
T exchange(T val) { return std::atomic<T>::exchange(val, relaxed); }
|
||||
T operator|=(T val) { return std::atomic<T>::fetch_or(val, relaxed); }
|
||||
T operator++() { return std::atomic<T>::fetch_add(1, relaxed) + 1; }
|
||||
T operator--() { return std::atomic<T>::fetch_sub(1, relaxed) - 1; }
|
||||
T operator++(int) { return std::atomic<T>::fetch_add(1, relaxed); }
|
||||
T operator--(int) { return std::atomic<T>::fetch_sub(1, relaxed); }
|
||||
|
||||
bool test_and_set() {
|
||||
// A relaxed load + branch (assuming miss) takes only around 20 cycles,
|
||||
// while an atomic RMW can easily take hundreds on x86. We note that it's
|
||||
// common that another thread beat us in marking, so doing an optimistic
|
||||
// early test tends to improve performance in the ~20% ballpark.
|
||||
return load() || exchange(true);
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// mimalloc.cc
|
||||
//
|
||||
|
||||
void set_mimalloc_options();
|
||||
|
||||
//
|
||||
// perf.cc
|
||||
//
|
||||
|
||||
// Counter is used to collect statistics numbers.
|
||||
class Counter {
|
||||
public:
|
||||
Counter(std::string_view name, i64 value = 0) : name(name), values(value) {
|
||||
static std::mutex mu;
|
||||
std::scoped_lock lock(mu);
|
||||
instances.push_back(this);
|
||||
}
|
||||
|
||||
Counter &operator++(int) {
|
||||
if (enabled) [[unlikely]]
|
||||
values.local()++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Counter &operator+=(int delta) {
|
||||
if (enabled) [[unlikely]]
|
||||
values.local() += delta;
|
||||
return *this;
|
||||
}
|
||||
|
||||
static void print();
|
||||
|
||||
static inline bool enabled = false;
|
||||
|
||||
private:
|
||||
i64 get_value();
|
||||
|
||||
std::string_view name;
|
||||
tbb::enumerable_thread_specific<i64> values;
|
||||
|
||||
static inline std::vector<Counter *> instances;
|
||||
};
|
||||
|
||||
// Timer and TimeRecord records elapsed time (wall clock time)
|
||||
// used by each pass of the linker.
|
||||
struct TimerRecord {
|
||||
TimerRecord(std::string name, TimerRecord *parent = nullptr);
|
||||
void stop();
|
||||
|
||||
std::string name;
|
||||
TimerRecord *parent;
|
||||
tbb::concurrent_vector<TimerRecord *> children;
|
||||
i64 start;
|
||||
i64 end;
|
||||
i64 user;
|
||||
i64 sys;
|
||||
bool stopped = false;
|
||||
};
|
||||
|
||||
void
|
||||
print_timer_records(tbb::concurrent_vector<std::unique_ptr<TimerRecord>> &);
|
||||
|
||||
template <typename Context>
|
||||
class Timer {
|
||||
public:
|
||||
Timer(Context &ctx, std::string name, Timer *parent = nullptr) {
|
||||
record = new TimerRecord(name, parent ? parent->record : nullptr);
|
||||
ctx.timer_records.emplace_back(record);
|
||||
}
|
||||
|
||||
Timer(const Timer &) = delete;
|
||||
|
||||
~Timer() {
|
||||
record->stop();
|
||||
}
|
||||
|
||||
void stop() {
|
||||
record->stop();
|
||||
}
|
||||
|
||||
private:
|
||||
TimerRecord *record;
|
||||
};
|
||||
|
||||
//
|
||||
// Utility functions
|
||||
//
|
||||
|
||||
// Some C++ libraries haven't implemented std::has_single_bit yet.
|
||||
inline bool has_single_bit(u64 val) {
|
||||
return std::popcount(val) == 1;
|
||||
}
|
||||
|
||||
// Some C++ libraries haven't implemented std::bit_ceil yet.
|
||||
inline u64 bit_ceil(u64 val) {
|
||||
if (has_single_bit(val))
|
||||
return val;
|
||||
return 1LL << (64 - std::countl_zero(val));
|
||||
}
|
||||
|
||||
inline u64 align_to(u64 val, u64 align) {
|
||||
if (align == 0)
|
||||
return val;
|
||||
assert(has_single_bit(align));
|
||||
return (val + align - 1) & ~(align - 1);
|
||||
}
|
||||
|
||||
inline u64 align_down(u64 val, u64 align) {
|
||||
assert(has_single_bit(align));
|
||||
return val & ~(align - 1);
|
||||
}
|
||||
|
||||
inline u64 bit(u64 val, i64 pos) {
|
||||
return (val >> pos) & 1;
|
||||
};
|
||||
|
||||
// Returns [hi:lo] bits of val.
|
||||
inline u64 bits(u64 val, u64 hi, u64 lo) {
|
||||
return (val >> lo) & ((1LL << (hi - lo + 1)) - 1);
|
||||
}
|
||||
|
||||
// Cast val to a signed N bit integer.
|
||||
// For example, sign_extend(x, 32) == (i32)x for any integer x.
|
||||
inline i64 sign_extend(u64 val, i64 n) {
|
||||
return (i64)(val << (64 - n)) >> (64 - n);
|
||||
}
|
||||
|
||||
inline bool is_int(u64 val, i64 n) {
|
||||
return sign_extend(val, n) == val;
|
||||
}
|
||||
|
||||
template <typename T, typename Compare = std::less<T>>
|
||||
void update_minimum(std::atomic<T> &atomic, u64 new_val, Compare cmp = {}) {
|
||||
T old_val = atomic.load(std::memory_order_relaxed);
|
||||
while (cmp(new_val, old_val) &&
|
||||
!atomic.compare_exchange_weak(old_val, new_val,
|
||||
std::memory_order_relaxed));
|
||||
}
|
||||
|
||||
template <typename T, typename Compare = std::less<T>>
|
||||
void update_maximum(std::atomic<T> &atomic, u64 new_val, Compare cmp = {}) {
|
||||
T old_val = atomic.load(std::memory_order_relaxed);
|
||||
while (cmp(old_val, new_val) &&
|
||||
!atomic.compare_exchange_weak(old_val, new_val,
|
||||
std::memory_order_relaxed));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void append(std::vector<T> &x, const auto &y) {
|
||||
x.insert(x.end(), y.begin(), y.end());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline std::vector<T> flatten(std::vector<std::vector<T>> &vec) {
|
||||
i64 size = 0;
|
||||
for (std::vector<T> &v : vec)
|
||||
size += v.size();
|
||||
|
||||
std::vector<T> ret;
|
||||
ret.reserve(size);
|
||||
for (std::vector<T> &v : vec)
|
||||
append(ret, v);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void remove_duplicates(std::vector<T> &vec) {
|
||||
vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
|
||||
}
|
||||
|
||||
inline i64 write_string(void *buf, std::string_view str) {
|
||||
memcpy(buf, str.data(), str.size());
|
||||
*((u8 *)buf + str.size()) = '\0';
|
||||
return str.size() + 1;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void write_vector(void *buf, const std::vector<T> &vec) {
|
||||
if (!vec.empty())
|
||||
memcpy(buf, vec.data(), vec.size() * sizeof(T));
|
||||
}
|
||||
|
||||
inline void encode_uleb(std::vector<u8> &vec, u64 val) {
|
||||
do {
|
||||
u8 byte = val & 0x7f;
|
||||
val >>= 7;
|
||||
vec.push_back(val ? (byte | 0x80) : byte);
|
||||
} while (val);
|
||||
}
|
||||
|
||||
inline void encode_sleb(std::vector<u8> &vec, i64 val) {
|
||||
for (;;) {
|
||||
u8 byte = val & 0x7f;
|
||||
val >>= 7;
|
||||
|
||||
bool neg = (byte & 0x40);
|
||||
if ((val == 0 && !neg) || (val == -1 && neg)) {
|
||||
vec.push_back(byte);
|
||||
break;
|
||||
}
|
||||
vec.push_back(byte | 0x80);
|
||||
}
|
||||
}
|
||||
|
||||
inline i64 write_uleb(u8 *buf, u64 val) {
|
||||
i64 i = 0;
|
||||
do {
|
||||
u8 byte = val & 0x7f;
|
||||
val >>= 7;
|
||||
buf[i++] = val ? (byte | 0x80) : byte;
|
||||
} while (val);
|
||||
return i;
|
||||
}
|
||||
|
||||
inline u64 read_uleb(u8 **buf) {
|
||||
u64 val = 0;
|
||||
u8 shift = 0;
|
||||
u8 byte;
|
||||
do {
|
||||
byte = *(*buf)++;
|
||||
val |= (byte & 0x7f) << shift;
|
||||
shift += 7;
|
||||
} while (byte & 0x80);
|
||||
return val;
|
||||
}
|
||||
|
||||
inline u64 read_uleb(u8 *buf) {
|
||||
u8 *tmp = buf;
|
||||
return read_uleb(&tmp);
|
||||
}
|
||||
|
||||
inline i64 read_sleb(u8 **buf) {
|
||||
u64 val = 0;
|
||||
u8 shift = 0;
|
||||
u8 byte;
|
||||
do {
|
||||
byte = *(*buf)++;
|
||||
val |= (byte & 0x7f) << shift;
|
||||
shift += 7;
|
||||
} while (byte & 0x80);
|
||||
return sign_extend(val, shift);
|
||||
}
|
||||
|
||||
inline i64 read_sleb(u8 *buf) {
|
||||
u8 *tmp = buf;
|
||||
return read_sleb(&tmp);
|
||||
}
|
||||
|
||||
inline u64 read_uleb(std::string_view *str) {
|
||||
u8 *start = (u8 *)str->data();
|
||||
u8 *ptr = start;
|
||||
u64 val = read_uleb(&ptr);
|
||||
*str = str->substr(ptr - start);
|
||||
return val;
|
||||
}
|
||||
|
||||
inline u64 read_uleb(std::string_view str) {
|
||||
std::string_view tmp = str;
|
||||
return read_uleb(&tmp);
|
||||
}
|
||||
|
||||
inline i64 uleb_size(u64 val) {
|
||||
for (int i = 1; i < 9; i++)
|
||||
if (val < (1LL << (7 * i)))
|
||||
return i;
|
||||
return 9;
|
||||
}
|
||||
|
||||
inline void overwrite_uleb(u8 *loc, u64 val) {
|
||||
while (*loc & 0b1000'0000) {
|
||||
*loc++ = 0b1000'0000 | (val & 0b0111'1111);
|
||||
val >>= 7;
|
||||
}
|
||||
*loc = val & 0b0111'1111;
|
||||
}
|
||||
|
||||
static inline void pause() {
|
||||
#if defined(__x86_64__)
|
||||
asm volatile("pause");
|
||||
#elif defined(__aarch64__)
|
||||
asm volatile("yield");
|
||||
#elif defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_8A__)
|
||||
asm volatile("yield");
|
||||
#endif
|
||||
}
|
||||
|
||||
//
|
||||
// Concurrent Map
|
||||
//
|
||||
|
||||
// This is an implementation of a fast concurrent hash map. Unlike
|
||||
// ordinary hash tables, this impl just aborts if it becomes full.
|
||||
// So you need to give a correct estimation of the final size before
|
||||
// using it. We use this hash map to uniquify pieces of data in
|
||||
// mergeable sections.
|
||||
//
|
||||
// We've implemented this ourselves because the performance of
|
||||
// conrurent hash map is critical for our linker.
|
||||
template <typename T>
|
||||
class ConcurrentMap {
|
||||
public:
|
||||
ConcurrentMap() = default;
|
||||
|
||||
ConcurrentMap(i64 nbuckets) {
|
||||
resize(nbuckets);
|
||||
}
|
||||
|
||||
~ConcurrentMap() {
|
||||
if (entries) {
|
||||
#ifdef _WIN32
|
||||
_aligned_free(entries);
|
||||
#else
|
||||
munmap(entries, sizeof(Entry) * nbuckets);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// In order to avoid unnecessary cache-line false sharing, we want
|
||||
// to make this object to be aligned to a reasonably large
|
||||
// power-of-two address.
|
||||
struct alignas(32) Entry {
|
||||
Atomic<const char *> key;
|
||||
u32 keylen;
|
||||
T value;
|
||||
};
|
||||
|
||||
void resize(i64 nbuckets) {
|
||||
assert(!entries);
|
||||
this->nbuckets = std::max<i64>(MIN_NBUCKETS, bit_ceil(nbuckets));
|
||||
i64 bufsize = sizeof(Entry) * this->nbuckets;
|
||||
|
||||
// Allocate a zero-initialized buffer. We use mmap() if available
|
||||
// because it's faster than malloc() and memset().
|
||||
#ifdef _WIN32
|
||||
entries = (Entry *)_aligned_malloc(bufsize, alignof(Entry));
|
||||
memset((void *)entries, 0, bufsize);
|
||||
#else
|
||||
entries = (Entry *)mmap(nullptr, bufsize, PROT_READ | PROT_WRITE,
|
||||
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
std::pair<T *, bool> insert(std::string_view key, u64 hash, const T &val) {
|
||||
assert(has_single_bit(nbuckets));
|
||||
|
||||
u64 begin = hash & (nbuckets - 1);
|
||||
u64 mask = nbuckets / NUM_SHARDS - 1;
|
||||
|
||||
for (i64 i = 0; i < MAX_RETRY; i++) {
|
||||
u64 idx = (begin & ~mask) | ((begin + i) & mask);
|
||||
Entry &ent = entries[idx];
|
||||
|
||||
// It seems avoiding compare-and-swap is faster overall at least
|
||||
// on my Zen4 machine, so do it.
|
||||
if (const char *ptr = ent.key.load(std::memory_order_acquire);
|
||||
ptr != nullptr && ptr != (char *)-1) {
|
||||
if (key == std::string_view(ptr, ent.keylen))
|
||||
return {&ent.value, false};
|
||||
continue;
|
||||
}
|
||||
|
||||
// Otherwise, use CAS to atomically claim the ownership of the slot.
|
||||
const char *ptr = nullptr;
|
||||
bool claimed = ent.key.compare_exchange_strong(ptr, (char *)-1,
|
||||
std::memory_order_acquire);
|
||||
|
||||
// If we successfully claimed the ownership of the slot,
|
||||
// copy values to it.
|
||||
if (claimed) {
|
||||
new (&ent.value) T(val);
|
||||
ent.keylen = key.size();
|
||||
ent.key.store(key.data(), std::memory_order_release);
|
||||
return {&ent.value, true};
|
||||
}
|
||||
|
||||
// If someone is copying values to the slot, do busy wait.
|
||||
while (ptr == (char *)-1) {
|
||||
pause();
|
||||
ptr = ent.key.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
// If the same key is already present, this is the slot we are
|
||||
// looking for.
|
||||
if (key == std::string_view(ptr, ent.keylen))
|
||||
return {&ent.value, false};
|
||||
}
|
||||
|
||||
std::cerr << "ConcurrentMap is full\n";
|
||||
abort();
|
||||
}
|
||||
|
||||
i64 get_idx(T *value) const {
|
||||
uintptr_t addr = (uintptr_t)value - (uintptr_t)value % sizeof(Entry);
|
||||
return (Entry *)addr - entries;
|
||||
}
|
||||
|
||||
// Return a list of map entries sorted in a deterministic order.
|
||||
std::vector<Entry *> get_sorted_entries(i64 shard_idx) {
|
||||
if (nbuckets == 0)
|
||||
return {};
|
||||
|
||||
i64 shard_size = nbuckets / NUM_SHARDS;
|
||||
i64 begin = shard_idx * shard_size;
|
||||
i64 end = begin + shard_size;
|
||||
|
||||
i64 sz = 0;
|
||||
for (i64 i = begin; i < end; i++)
|
||||
if (entries[i].key)
|
||||
sz++;
|
||||
|
||||
std::vector<Entry *> vec;
|
||||
vec.reserve(sz);
|
||||
|
||||
// Since the shard is circular, we need to handle the last entries
|
||||
// as if they were next to the first entries.
|
||||
while (entries[end - 1].key)
|
||||
vec.push_back(entries + --end);
|
||||
|
||||
// Find entries contiguous in the buckets and sort them.
|
||||
i64 last = 0;
|
||||
for (i64 i = begin; i < end;) {
|
||||
while (i < end && entries[i].key)
|
||||
vec.push_back(entries + i++);
|
||||
|
||||
std::sort(vec.begin() + last, vec.end(), [](Entry *a, Entry *b) {
|
||||
if (a->keylen != b->keylen)
|
||||
return a->keylen < b->keylen;
|
||||
return memcmp(a->key, b->key, a->keylen) < 0;
|
||||
});
|
||||
|
||||
last = vec.size();
|
||||
|
||||
while (i < end && !entries[i].key)
|
||||
i++;
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
std::vector<Entry *> get_sorted_entries_all() {
|
||||
std::vector<std::vector<Entry *>> vec(NUM_SHARDS);
|
||||
tbb::parallel_for((i64)0, NUM_SHARDS, [&](i64 i) {
|
||||
vec[i] = get_sorted_entries(i);
|
||||
});
|
||||
return flatten(vec);
|
||||
}
|
||||
|
||||
static constexpr i64 MIN_NBUCKETS = 4096;
|
||||
static constexpr i64 NUM_SHARDS = 16;
|
||||
static constexpr i64 MAX_RETRY = 256;
|
||||
|
||||
Entry *entries = nullptr;
|
||||
u64 nbuckets = 0;
|
||||
};
|
||||
|
||||
//
|
||||
// random.cc
|
||||
//
|
||||
|
||||
void get_random_bytes(u8 *buf, i64 size);
|
||||
|
||||
//
|
||||
// hyperloglog.cc
|
||||
//
|
||||
|
||||
class HyperLogLog {
|
||||
public:
|
||||
void insert(u64 hash) {
|
||||
update_maximum(buckets[hash & (NBUCKETS - 1)], std::countl_zero(hash) + 1);
|
||||
}
|
||||
|
||||
i64 get_cardinality() const;
|
||||
|
||||
void merge(const HyperLogLog &other) {
|
||||
for (i64 i = 0; i < NBUCKETS; i++)
|
||||
update_maximum(buckets[i], other.buckets[i]);
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr i64 NBUCKETS = 2048;
|
||||
static constexpr double ALPHA = 0.79402;
|
||||
|
||||
Atomic<u8> buckets[NBUCKETS];
|
||||
};
|
||||
|
||||
//
|
||||
// glob.cc
|
||||
//
|
||||
|
||||
class Glob {
|
||||
typedef enum { STRING, STAR, QUESTION, BRACKET } Kind;
|
||||
|
||||
struct Element {
|
||||
Element(Kind k) : kind(k) {}
|
||||
Kind kind;
|
||||
std::string str;
|
||||
std::bitset<256> bitset;
|
||||
};
|
||||
|
||||
public:
|
||||
static std::optional<Glob> compile(std::string_view pat);
|
||||
bool match(std::string_view str);
|
||||
|
||||
private:
|
||||
Glob(std::vector<Element> &&vec) : elements(vec) {}
|
||||
static bool do_match(std::string_view str, std::span<Element> elements);
|
||||
|
||||
std::vector<Element> elements;
|
||||
};
|
||||
|
||||
//
|
||||
// multi-glob.cc
|
||||
//
|
||||
|
||||
class MultiGlob {
|
||||
public:
|
||||
bool add(std::string_view pat, i64 val);
|
||||
bool empty() const { return strings.empty(); }
|
||||
std::optional<i64> find(std::string_view str);
|
||||
|
||||
private:
|
||||
struct TrieNode {
|
||||
i64 value = -1;
|
||||
TrieNode *suffix_link = nullptr;
|
||||
std::unique_ptr<TrieNode> children[256];
|
||||
};
|
||||
|
||||
void compile();
|
||||
void fix_suffix_links(TrieNode &node);
|
||||
void fix_values();
|
||||
i64 find_aho_corasick(std::string_view str);
|
||||
|
||||
std::vector<std::string> strings;
|
||||
std::unique_ptr<TrieNode> root;
|
||||
std::vector<std::pair<Glob, i64>> globs;
|
||||
std::once_flag once;
|
||||
bool is_compiled = false;
|
||||
bool prefix_match = false;
|
||||
};
|
||||
|
||||
//
|
||||
// filepath.cc
|
||||
//
|
||||
|
||||
inline std::filesystem::path path_dirname(std::string_view path) {
|
||||
return std::filesystem::path(path).parent_path();
|
||||
}
|
||||
|
||||
inline std::string path_filename(std::string_view path) {
|
||||
return std::filesystem::path(path).filename().string();
|
||||
}
|
||||
|
||||
inline std::string path_clean(std::string_view path) {
|
||||
return std::filesystem::path(path).lexically_normal().string();
|
||||
}
|
||||
|
||||
std::string get_self_path();
|
||||
|
||||
//
|
||||
// demangle.cc
|
||||
//
|
||||
|
||||
std::optional<std::string_view> demangle_cpp(std::string_view name);
|
||||
std::optional<std::string_view> demangle_rust(std::string_view name);
|
||||
|
||||
//
|
||||
// jbos.cc
|
||||
//
|
||||
|
||||
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
|
||||
//
|
||||
|
||||
class Compressor {
|
||||
public:
|
||||
virtual void write_to(u8 *buf) = 0;
|
||||
virtual ~Compressor() {}
|
||||
i64 compressed_size = 0;
|
||||
};
|
||||
|
||||
class ZlibCompressor : public Compressor {
|
||||
public:
|
||||
ZlibCompressor(u8 *buf, i64 size);
|
||||
void write_to(u8 *buf) override;
|
||||
|
||||
private:
|
||||
std::vector<std::vector<u8>> shards;
|
||||
u64 checksum = 0;
|
||||
};
|
||||
|
||||
class ZstdCompressor : public Compressor {
|
||||
public:
|
||||
ZstdCompressor(u8 *buf, i64 size);
|
||||
void write_to(u8 *buf) override;
|
||||
|
||||
private:
|
||||
std::vector<std::vector<u8>> shards;
|
||||
};
|
||||
|
||||
//
|
||||
// tar.cc
|
||||
//
|
||||
|
||||
// TarFile is a class to create a tar file.
|
||||
//
|
||||
// If you pass `--repro` to mold, mold collects all input files and
|
||||
// put them into `<output-file-path>.repro.tar`, so that it is easy to
|
||||
// run the same command with the same command line arguments.
|
||||
class TarWriter {
|
||||
public:
|
||||
static std::unique_ptr<TarWriter>
|
||||
open(std::string output_path, std::string basedir);
|
||||
|
||||
~TarWriter();
|
||||
void append(std::string path, std::string_view data);
|
||||
|
||||
private:
|
||||
TarWriter(FILE *out, std::string basedir) : out(out), basedir(basedir) {}
|
||||
|
||||
FILE *out = nullptr;
|
||||
std::string basedir;
|
||||
};
|
||||
|
||||
//
|
||||
// Memory-mapped file
|
||||
//
|
||||
|
||||
// MappedFile represents an mmap'ed input file.
|
||||
// mold uses mmap-IO only.
|
||||
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) {
|
||||
MappedFile *mf = new MappedFile;
|
||||
mf->name = name;
|
||||
mf->data = data + start;
|
||||
mf->size = size;
|
||||
mf->parent = this;
|
||||
|
||||
ctx.mf_pool.emplace_back(mf);
|
||||
return mf;
|
||||
}
|
||||
|
||||
std::string_view get_contents() {
|
||||
return std::string_view((char *)data, size);
|
||||
}
|
||||
|
||||
i64 get_offset() const {
|
||||
return parent ? (data - parent->data + parent->get_offset()) : 0;
|
||||
}
|
||||
|
||||
// Returns a string that uniquely identify a file that is possibly
|
||||
// in an archive.
|
||||
std::string get_identifier() const {
|
||||
if (parent) {
|
||||
// We use the file offset within an archive as an identifier
|
||||
// because archive members may have the same name.
|
||||
return parent->name + ":" + std::to_string(get_offset());
|
||||
}
|
||||
|
||||
if (thin_parent) {
|
||||
// If this is a thin archive member, the filename part is
|
||||
// guaranteed to be unique.
|
||||
return thin_parent->name + ":" + name;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
std::string name;
|
||||
u8 *data = nullptr;
|
||||
i64 size = 0;
|
||||
bool given_fullpath = true;
|
||||
MappedFile *parent = nullptr;
|
||||
MappedFile *thin_parent = nullptr;
|
||||
|
||||
// For --dependency-file
|
||||
bool is_dependency = true;
|
||||
|
||||
#ifdef _WIN32
|
||||
HANDLE fd = INVALID_HANDLE_VALUE;
|
||||
#else
|
||||
int fd = -1;
|
||||
#endif
|
||||
};
|
||||
|
||||
MappedFile *open_file_impl(const std::string &path, std::string &error);
|
||||
|
||||
template <typename Context>
|
||||
MappedFile *open_file(Context &ctx, std::string path) {
|
||||
if (path.starts_with('/') && !ctx.arg.chroot.empty())
|
||||
path = ctx.arg.chroot + "/" + path_clean(path);
|
||||
|
||||
std::string error;
|
||||
MappedFile *mf = open_file_impl(path, error);
|
||||
if (!error.empty())
|
||||
Fatal(ctx) << error;
|
||||
|
||||
if (mf)
|
||||
ctx.mf_pool.emplace_back(mf);
|
||||
return mf;
|
||||
}
|
||||
|
||||
template <typename Context>
|
||||
MappedFile *must_open_file(Context &ctx, std::string path) {
|
||||
MappedFile *mf = open_file(ctx, path);
|
||||
if (!mf)
|
||||
Fatal(ctx) << "cannot open " << path << ": " << errno_string();
|
||||
return mf;
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
-185
@@ -1,185 +0,0 @@
|
||||
// This file implements a multi-threaded zlib and zstd compression
|
||||
// routine.
|
||||
//
|
||||
// zlib-compressed data can be merged just by concatenation as long as
|
||||
// each piece of data is flushed with Z_SYNC_FLUSH. In this file, we
|
||||
// split input data into multiple shards, compress them individually
|
||||
// and concatenate them. We then append a header, a trailer and a
|
||||
// checksum so that the concatenated data is valid zlib-format data.
|
||||
//
|
||||
// zstd-compressed data can be merged in the same way.
|
||||
//
|
||||
// Using threads to compress data has a downside. Since the dictionary
|
||||
// is reset on boundaries of shards, compression ratio is sacrificed
|
||||
// a little bit. However, if a shard size is large enough, that loss
|
||||
// is negligible in practice.
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <tbb/parallel_for_each.h>
|
||||
#include <zlib.h>
|
||||
#include <zstd.h>
|
||||
|
||||
#define CHECK(fn) \
|
||||
do { \
|
||||
[[maybe_unused]] int r = (fn); \
|
||||
assert(r == Z_OK); \
|
||||
} while (0)
|
||||
|
||||
namespace mold {
|
||||
|
||||
static constexpr i64 SHARD_SIZE = 1024 * 1024;
|
||||
|
||||
static std::vector<std::string_view> split(std::string_view input) {
|
||||
std::vector<std::string_view> shards;
|
||||
|
||||
while (input.size() >= SHARD_SIZE) {
|
||||
shards.push_back(input.substr(0, SHARD_SIZE));
|
||||
input = input.substr(SHARD_SIZE);
|
||||
}
|
||||
if (!input.empty())
|
||||
shards.push_back(input);
|
||||
return shards;
|
||||
}
|
||||
|
||||
static std::vector<u8> zlib_compress(std::string_view input) {
|
||||
// Initialize zlib stream. Since debug info is generally compressed
|
||||
// pretty well with lower compression levels, we chose compression
|
||||
// level 1.
|
||||
z_stream strm;
|
||||
strm.zalloc = Z_NULL;
|
||||
strm.zfree = Z_NULL;
|
||||
strm.opaque = Z_NULL;
|
||||
|
||||
CHECK(deflateInit2(&strm, 1, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY));
|
||||
|
||||
// Set an input buffer
|
||||
strm.avail_in = input.size();
|
||||
strm.next_in = (u8 *)input.data();
|
||||
|
||||
// Set an output buffer. deflateBound() returns an upper bound
|
||||
// on the compression size. +16 for Z_SYNC_FLUSH.
|
||||
std::vector<u8> buf(deflateBound(&strm, strm.avail_in) + 16);
|
||||
|
||||
// Compress data. It writes all compressed bytes except the last
|
||||
// partial byte, so up to 7 bits can be held to be written to the
|
||||
// buffer.
|
||||
strm.avail_out = buf.size();
|
||||
strm.next_out = buf.data();
|
||||
CHECK(deflate(&strm, Z_BLOCK));
|
||||
|
||||
// This is a workaround for libbacktrace before 2022-04-06.
|
||||
//
|
||||
// Zlib is a bit stream, and what Z_SYNC_FLUSH does is to write a
|
||||
// three bit value indicating the start of an uncompressed data
|
||||
// block followed by four byte data 00 00 ff ff which indicates that
|
||||
// the length of the block is zero. libbacktrace uses its own zlib
|
||||
// inflate routine, and it had a bug that if that particular three
|
||||
// bit value happens to end at a byte boundary, it accidentally
|
||||
// skipped the next byte.
|
||||
//
|
||||
// In order to avoid triggering that bug, we should avoid calling
|
||||
// deflate() with Z_SYNC_FLUSH if the current bit position is 5.
|
||||
// If it's 5, we insert an empty block consisting of 10 bits so
|
||||
// that the bit position is 7 in the next byte.
|
||||
//
|
||||
// https://github.com/ianlancetaylor/libbacktrace/pull/87
|
||||
int nbits;
|
||||
deflatePending(&strm, Z_NULL, &nbits);
|
||||
if (nbits == 5)
|
||||
CHECK(deflatePrime(&strm, 10, 2));
|
||||
CHECK(deflate(&strm, Z_SYNC_FLUSH));
|
||||
|
||||
assert(strm.avail_out > 0);
|
||||
buf.resize(buf.size() - strm.avail_out);
|
||||
buf.shrink_to_fit();
|
||||
deflateEnd(&strm);
|
||||
return buf;
|
||||
}
|
||||
|
||||
ZlibCompressor::ZlibCompressor(u8 *buf, i64 size) {
|
||||
std::string_view input{(char *)buf, (size_t)size};
|
||||
std::vector<std::string_view> inputs = split(input);
|
||||
std::vector<u64> adlers(inputs.size());
|
||||
shards.resize(inputs.size());
|
||||
|
||||
// Compress each shard
|
||||
tbb::parallel_for((i64)0, (i64)inputs.size(), [&](i64 i) {
|
||||
adlers[i] = adler32(1, (u8 *)inputs[i].data(), inputs[i].size());
|
||||
shards[i] = zlib_compress(inputs[i]);
|
||||
});
|
||||
|
||||
// Combine checksums
|
||||
checksum = adlers[0];
|
||||
for (i64 i = 1; i < inputs.size(); i++)
|
||||
checksum = adler32_combine(checksum, adlers[i], inputs[i].size());
|
||||
|
||||
// Comput the total size
|
||||
compressed_size = 8; // the header and the trailer
|
||||
for (std::vector<u8> &shard : shards)
|
||||
compressed_size += shard.size();
|
||||
}
|
||||
|
||||
void ZlibCompressor::write_to(u8 *buf) {
|
||||
// Write a zlib-format header
|
||||
buf[0] = 0x78;
|
||||
buf[1] = 0x9c;
|
||||
|
||||
// Copy compressed data
|
||||
std::vector<i64> offsets(shards.size());
|
||||
offsets[0] = 2; // +2 for header
|
||||
for (i64 i = 1; i < shards.size(); i++)
|
||||
offsets[i] = offsets[i - 1] + shards[i - 1].size();
|
||||
|
||||
tbb::parallel_for((i64)0, (i64)shards.size(), [&](i64 i) {
|
||||
memcpy(buf + offsets[i], shards[i].data(), shards[i].size());
|
||||
});
|
||||
|
||||
// Write a trailer
|
||||
u8 *end = buf + compressed_size;
|
||||
end[-6] = 3;
|
||||
end[-5] = 0;
|
||||
|
||||
// Write a checksum
|
||||
*(ub32 *)(end - 4) = checksum;
|
||||
}
|
||||
|
||||
static std::vector<u8> zstd_compress(std::string_view input) {
|
||||
std::vector<u8> buf(ZSTD_COMPRESSBOUND(input.size()));
|
||||
constexpr int LEVEL = 3; // compression level; must be between 1 to 22
|
||||
|
||||
size_t sz = ZSTD_compress(buf.data(), buf.size(), input.data(), input.size(),
|
||||
LEVEL);
|
||||
assert(!ZSTD_isError(sz));
|
||||
buf.resize(sz);
|
||||
buf.shrink_to_fit();
|
||||
return buf;
|
||||
}
|
||||
|
||||
ZstdCompressor::ZstdCompressor(u8 *buf, i64 size) {
|
||||
std::string_view input{(char *)buf, (size_t)size};
|
||||
std::vector<std::string_view> inputs = split(input);
|
||||
shards.resize(inputs.size());
|
||||
|
||||
// Compress each shard
|
||||
tbb::parallel_for((i64)0, (i64)inputs.size(), [&](i64 i) {
|
||||
shards[i] = zstd_compress(inputs[i]);
|
||||
});
|
||||
|
||||
compressed_size = 0;
|
||||
for (std::vector<u8> &shard : shards)
|
||||
compressed_size += shard.size();
|
||||
}
|
||||
|
||||
void ZstdCompressor::write_to(u8 *buf) {
|
||||
// Copy compressed data
|
||||
std::vector<i64> offsets(shards.size());
|
||||
for (i64 i = 1; i < shards.size(); i++)
|
||||
offsets[i] = offsets[i - 1] + shards[i - 1].size();
|
||||
|
||||
tbb::parallel_for((i64)0, (i64)shards.size(), [&](i64 i) {
|
||||
memcpy(buf + offsets[i], shards[i].data(), shards[i].size());
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,7 +0,0 @@
|
||||
#define MOLD_VERSION "@mold_VERSION@"
|
||||
#define MOLD_LIBDIR "@CMAKE_INSTALL_FULL_LIBDIR@"
|
||||
|
||||
#cmakedefine HAVE_MADVISE 1
|
||||
#cmakedefine HAVE_UNAME 1
|
||||
#cmakedefine MOLD_USE_MIMALLOC 1
|
||||
#cmakedefine MOLD_USE_SYSTEM_MIMALLOC 1
|
||||
@@ -1,60 +0,0 @@
|
||||
#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, [](Shard &shard) {
|
||||
shard.crc = crc32(0, shard.buf, shard.len);
|
||||
});
|
||||
|
||||
for (Shard &shard : shards)
|
||||
crc = crc32_combine(crc, shard.crc, shard.len);
|
||||
return crc;
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,42 +0,0 @@
|
||||
#include "common.h"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <cxxabi.h>
|
||||
#endif
|
||||
|
||||
#include "../third-party/rust-demangle/rust-demangle.h"
|
||||
|
||||
namespace mold {
|
||||
|
||||
std::optional<std::string_view> demangle_cpp(std::string_view name) {
|
||||
static thread_local char *buf;
|
||||
static thread_local size_t buflen;
|
||||
|
||||
// TODO(cwasser): Actually demangle Symbols on Windows using e.g.
|
||||
// `UnDecorateSymbolName` from Dbghelp, maybe even Itanium symbols?
|
||||
#ifndef _WIN32
|
||||
if (name.starts_with("_Z")) {
|
||||
int status;
|
||||
char *p = abi::__cxa_demangle(std::string(name).c_str(), buf, &buflen, &status);
|
||||
if (status == 0) {
|
||||
buf = p;
|
||||
return p;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<std::string_view> demangle_rust(std::string_view name) {
|
||||
static thread_local char *buf;
|
||||
free(buf);
|
||||
buf = rust_demangle(std::string(name).c_str(), 0);
|
||||
if (buf)
|
||||
return buf;
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,42 +0,0 @@
|
||||
#include "common.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifdef __APPLE__
|
||||
# include <mach-o/dyld.h>
|
||||
#endif
|
||||
|
||||
#ifdef __FreeBSD__
|
||||
# include <sys/sysctl.h>
|
||||
#endif
|
||||
|
||||
namespace mold {
|
||||
|
||||
// Returns the path of the mold executable itself
|
||||
std::string get_self_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).
|
||||
int mib[4];
|
||||
mib[0] = CTL_KERN;
|
||||
mib[1] = KERN_PROC;
|
||||
mib[2] = KERN_PROC_PATHNAME;
|
||||
mib[3] = -1;
|
||||
|
||||
size_t size;
|
||||
sysctl(mib, 4, NULL, &size, NULL, 0);
|
||||
|
||||
std::string path;
|
||||
path.resize(size);
|
||||
sysctl(mib, 4, path.data(), &size, NULL, 0);
|
||||
return path;
|
||||
#else
|
||||
return std::filesystem::read_symlink("/proc/self/exe").string();
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,69 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# This test script takes a Gentoo package name and tries to build it
|
||||
# using mold in a Podman environment. We chose Gentoo Linux as a test
|
||||
# target, because its source-based package allows us to build programs
|
||||
# locally and run their test suites without any hassle.
|
||||
#
|
||||
# You can get a complete list of Gentoo packages availalbe for testing
|
||||
# with the following command:
|
||||
#
|
||||
# podman run --rm mold-gentoo emerge --color n -s '' | \
|
||||
# perl -ne 'next unless m!^\*\s+(\S+/\S+)!; print "$1\n"'
|
||||
|
||||
package="$1"
|
||||
|
||||
if [ "$package" = "" ]; then
|
||||
echo "Usage: $0 gentoo-package-name"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
set -x
|
||||
|
||||
# Create a Podman image
|
||||
if ! podman image ls mold-gentoo | grep -q mold-gentoo; then
|
||||
set -e
|
||||
cat <<EOF | podman build -t mold-gentoo -
|
||||
FROM docker.io/gentoo/stage3
|
||||
RUN emerge-webrsync
|
||||
RUN echo 'USE="X ssl elogind -systemd corefonts truetype jpeg jpeg2k tiff zstd static-libs binary -perl"' >> /etc/portage/make.conf && \
|
||||
echo 'ACCEPT_KEYWORDS="~amd64"' >> /etc/portage/make.conf && \
|
||||
echo 'ACCEPT_LICENSE="* -@EULA"' >> /etc/portage/make.conf && \
|
||||
echo 'FEATURES="\${FEATURE} noclean nostrip ccache -ipc-sandbox -network-sandbox -pid-sandbox -sandbox"' >> /etc/portage/make.conf && \
|
||||
echo 'CCACHE_DIR="/ccache"' >> /etc/portage/make.conf && \
|
||||
FEATURES='getbinpkg binpkg-request-signature' emerge gdb lld llvm-core/clang vim emacs strace ccache xeyes dev-build/cmake dev-vcs/git && \
|
||||
rm -rf /var/tmp/portage
|
||||
EOF
|
||||
set +e
|
||||
fi
|
||||
|
||||
git_hash=$(./dist/mold --version | perl -ne '/\((\w+)/; print $1;')
|
||||
|
||||
if [ "$package" = dev-libs/concurrencykit ]; then
|
||||
echo "Skipping known broken package: $package"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Build a given package in Podman
|
||||
cmd1='(cd /usr/bin; ln -sf /mold/dist/mold $(realpath ld))'
|
||||
cmd2="MAKEOPTS=-'j$(nproc) --load-average=100' emerge --onlydeps $package"
|
||||
cmd3="MAKEOPTS='-j$(nproc) --load-average=100' FEATURES=test emerge $package"
|
||||
filename=`echo "$package" | sed 's!/!_!g'`
|
||||
podman="podman run --rm --pids-limit=-1 --cap-add=SYS_PTRACE -v `pwd`:/mold:ro -v /var/cache/ccache-gentoo:/ccache mold-gentoo timeout -v -k 15s 3h"
|
||||
dir=gentoo/$git_hash
|
||||
|
||||
mkdir -p "$dir"/success "$dir"/failure
|
||||
|
||||
$podman nice -n 19 bash -c "$cmd1 && $cmd2 && $cmd3" >& "$dir"/"$filename".mold
|
||||
if [ $? = 0 ]; then
|
||||
mv "$dir"/"$filename".mold "$dir"/success
|
||||
else
|
||||
mv "$dir"/"$filename".mold "$dir"/failure
|
||||
fi
|
||||
|
||||
$podman nice -n 19 bash -c "$cmd2 && $cmd3" >& "$dir"/"$filename".ld
|
||||
if [ $? = 0 ]; then
|
||||
mv "$dir"/"$filename".ld "$dir"/success
|
||||
else
|
||||
mv "$dir"/"$filename".ld "$dir"/failure
|
||||
fi
|
||||
-144
@@ -1,144 +0,0 @@
|
||||
// This file defines integral types for file input/output. We need to use
|
||||
// these types instead of the plain integers (such as uint32_t or int32_t)
|
||||
// when reading from/writing to an mmap'ed file area for the following
|
||||
// reasons:
|
||||
//
|
||||
// 1. mold is always a cross linker and should not depend on what host it
|
||||
// is running on. For example, users should be able to run mold on a
|
||||
// big-endian SPARC machine to create a little-endian RV64 binary.
|
||||
//
|
||||
// 2. Even though data members in all ELF data strucutres are naturally
|
||||
// aligned, they are not guaranteed to be aligned on memory because of
|
||||
// archive files. Archive files (.a files) align each member only to a
|
||||
// 2 byte boundary, so anything larger than 2 bytes may be misaligned
|
||||
// in an mmap'ed memory. Misaligned access is an undefined behavior in
|
||||
// C/C++, so we shouldn't cast an arbitrary pointer to a uint32_t, for
|
||||
// example, to read a 32 bit value.
|
||||
//
|
||||
// The data types defined in this file don't depend on host byte order and
|
||||
// don't do unaligned access. Note that modern compilers are smart enough
|
||||
// to recognize shift and bitwise OR patterns and compile them into a
|
||||
// single load instruction.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
|
||||
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
# define __LITTLE_ENDIAN__ 1
|
||||
# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||
# define __BIG_ENDIAN__ 1
|
||||
# else
|
||||
# error "unknown host byte order"
|
||||
# endif
|
||||
#endif
|
||||
|
||||
namespace mold {
|
||||
|
||||
typedef uint8_t u8;
|
||||
typedef uint16_t u16;
|
||||
typedef uint32_t u32;
|
||||
typedef uint64_t u64;
|
||||
|
||||
typedef int8_t i8;
|
||||
typedef int16_t i16;
|
||||
typedef int32_t i32;
|
||||
typedef int64_t i64;
|
||||
|
||||
template <typename T, bool is_le, int size = sizeof(T)>
|
||||
class Integer {
|
||||
public:
|
||||
constexpr Integer() = default;
|
||||
|
||||
constexpr Integer(T x) requires (is_le && size == 2)
|
||||
: buf{(u8)x, (u8)(x >> 8)} {}
|
||||
|
||||
constexpr Integer(T x) requires (is_le && size == 3)
|
||||
: buf{(u8)x, (u8)(x >> 8), (u8)(x >> 16)} {}
|
||||
|
||||
constexpr Integer(T x) requires (is_le && size == 4)
|
||||
: buf{(u8)x, (u8)(x >> 8), (u8)(x >> 16), (u8)(x >> 24)} {}
|
||||
|
||||
constexpr Integer(T x) requires (is_le && size == 8)
|
||||
: buf{(u8)x, (u8)(x >> 8), (u8)(x >> 16), (u8)(x >> 24),
|
||||
(u8)(x >> 32), (u8)(x >> 40), (u8)(x >> 48), (u8)(x >> 56)} {}
|
||||
|
||||
constexpr Integer(T x) requires (!is_le && size == 2)
|
||||
: buf{(u8)(x >> 8), (u8)x} {}
|
||||
|
||||
constexpr Integer(T x) requires (!is_le && size == 3)
|
||||
: buf{(u8)(x >> 16), (u8)(x >> 8), (u8)x} {}
|
||||
|
||||
constexpr Integer(T x) requires (!is_le && size == 4)
|
||||
: buf{(u8)(x >> 24), (u8)(x >> 16), (u8)(x >> 8), (u8)x} {}
|
||||
|
||||
constexpr Integer(T x) requires (!is_le && size == 8)
|
||||
: buf{(u8)(x >> 56), (u8)(x >> 48), (u8)(x >> 40), (u8)(x >> 32),
|
||||
(u8)(x >> 24), (u8)(x >> 16), (u8)(x >> 8), (u8)x} {}
|
||||
|
||||
operator T() const {
|
||||
if constexpr (is_le) {
|
||||
if constexpr (size == 2)
|
||||
return buf[1] << 8 | buf[0];
|
||||
else if constexpr (size == 3)
|
||||
return buf[2] << 16 | buf[1] << 8 | buf[0];
|
||||
else if constexpr (size == 4)
|
||||
return buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
|
||||
else
|
||||
return (u64)buf[7] << 56 | (u64)buf[6] << 48 |
|
||||
(u64)buf[5] << 40 | (u64)buf[4] << 32 |
|
||||
(u64)buf[3] << 24 | (u64)buf[2] << 16 |
|
||||
(u64)buf[1] << 8 | (u64)buf[0];
|
||||
} else {
|
||||
if constexpr (size == 2)
|
||||
return buf[0] << 8 | buf[1];
|
||||
else if constexpr (size == 3)
|
||||
return buf[0] << 16 | buf[1] << 8 | buf[2];
|
||||
else if constexpr (size == 4)
|
||||
return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
|
||||
else
|
||||
return (u64)buf[0] << 56 | (u64)buf[1] << 48 |
|
||||
(u64)buf[2] << 40 | (u64)buf[3] << 32 |
|
||||
(u64)buf[4] << 24 | (u64)buf[5] << 16 |
|
||||
(u64)buf[6] << 8 | (u64)buf[7];
|
||||
}
|
||||
}
|
||||
|
||||
Integer &operator=(T x) {
|
||||
new (this) Integer(x);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Integer &operator++() { return *this = *this + 1; }
|
||||
Integer operator++(int) { return ++*this - 1; }
|
||||
Integer &operator--() { return *this = *this - 1; }
|
||||
Integer operator--(int) { return --*this + 1; }
|
||||
Integer &operator+=(T x) { return *this = *this + x; }
|
||||
Integer &operator-=(T x) { return *this = *this - x; }
|
||||
Integer &operator&=(T x) { return *this = *this & x; }
|
||||
Integer &operator|=(T x) { return *this = *this | x; }
|
||||
|
||||
private:
|
||||
u8 buf[size];
|
||||
};
|
||||
|
||||
using il16 = Integer<i16, true>;
|
||||
using il32 = Integer<i32, true>;
|
||||
using il64 = Integer<i64, true>;
|
||||
|
||||
using ul16 = Integer<u16, true>;
|
||||
using ul24 = Integer<u32, true, 3>;
|
||||
using ul32 = Integer<u32, true>;
|
||||
using ul64 = Integer<u64, true>;
|
||||
|
||||
using ib16 = Integer<i16, false>;
|
||||
using ib32 = Integer<i32, false>;
|
||||
using ib64 = Integer<i64, false>;
|
||||
|
||||
using ub16 = Integer<u16, false>;
|
||||
using ub24 = Integer<u32, false, 3>;
|
||||
using ub32 = Integer<u32, false>;
|
||||
using ub64 = Integer<u64, false>;
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,50 +0,0 @@
|
||||
// Many build systems attempt to invoke as many linker processes as there
|
||||
// are cores, based on the assumption that the linker is single-threaded.
|
||||
// However, since mold is multi-threaded, such build systems' behavior is
|
||||
// not beneficial and just increases the overall peak memory usage.
|
||||
// On machines with limited memory, this could lead to an out-of-memory
|
||||
// error.
|
||||
//
|
||||
// This file implements a feature that limits the number of concurrent
|
||||
// mold processes to just 1 for each user. It is intended to be used as
|
||||
// `MOLD_JOBS=1 ninja` or `MOLD_JOBS=1 make -j$(nproc)`.
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <fcntl.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace mold {
|
||||
|
||||
static int lock_fd = -1;
|
||||
|
||||
void acquire_global_lock() {
|
||||
char *jobs = getenv("MOLD_JOBS");
|
||||
if (!jobs || jobs != "1"s)
|
||||
return;
|
||||
|
||||
std::string path;
|
||||
if (char *dir = getenv("XDG_RUNTIME_DIR"))
|
||||
path = dir + "/mold-lock"s;
|
||||
else
|
||||
path = "/tmp/mold-lock-"s + getpwuid(getuid())->pw_name;
|
||||
|
||||
int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, 0600);
|
||||
if (fd == -1)
|
||||
return;
|
||||
|
||||
if (lockf(fd, F_LOCK, 0) == -1)
|
||||
return;
|
||||
lock_fd = fd;
|
||||
}
|
||||
|
||||
void release_global_lock() {
|
||||
if (lock_fd != -1)
|
||||
close(lock_fd);
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace mold {
|
||||
|
||||
void acquire_global_lock() {}
|
||||
void release_global_lock() {}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,51 +0,0 @@
|
||||
#include "common.h"
|
||||
|
||||
namespace mold {
|
||||
|
||||
MappedFile *open_file_impl(const std::string &path, std::string &error) {
|
||||
i64 fd = ::open(path.c_str(), O_RDONLY);
|
||||
if (fd == -1) {
|
||||
if (errno != ENOENT)
|
||||
error = "opening " + path + " failed: " + errno_string();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) == -1)
|
||||
error = path + ": fstat failed: " + errno_string();
|
||||
|
||||
MappedFile *mf = new MappedFile;
|
||||
mf->name = path;
|
||||
mf->size = st.st_size;
|
||||
|
||||
if (st.st_size > 0) {
|
||||
mf->data = (u8 *)mmap(nullptr, st.st_size, PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE, fd, 0);
|
||||
if (mf->data == MAP_FAILED)
|
||||
error = path + ": mmap failed: " + errno_string();
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return mf;
|
||||
}
|
||||
|
||||
void MappedFile::unmap() {
|
||||
if (size == 0 || parent || !data)
|
||||
return;
|
||||
munmap(data, size);
|
||||
data = nullptr;
|
||||
}
|
||||
|
||||
void MappedFile::close_fd() {
|
||||
if (fd == -1)
|
||||
return;
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
|
||||
void MappedFile::reopen_fd(const std::string &path) {
|
||||
if (fd == -1)
|
||||
fd = open(path.c_str(), O_RDONLY);
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,81 +0,0 @@
|
||||
#include "common.h"
|
||||
|
||||
namespace mold {
|
||||
|
||||
MappedFile *open_file_impl(const std::string &path, std::string &error) {
|
||||
HANDLE fd = CreateFileA(path.c_str(), GENERIC_READ,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
|
||||
if (fd == INVALID_HANDLE_VALUE) {
|
||||
auto err = GetLastError();
|
||||
if (err != ERROR_FILE_NOT_FOUND)
|
||||
error = "opening " + path + " failed: " + errno_string();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (GetFileType(fd) != FILE_TYPE_DISK) {
|
||||
CloseHandle(fd);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
DWORD size_hi;
|
||||
DWORD size_lo = GetFileSize(fd, &size_hi);
|
||||
|
||||
if (size_lo == INVALID_FILE_SIZE) {
|
||||
error = path + ": GetFileSize failed: " + errno_string();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
u64 size = ((u64)size_hi << 32) + size_lo;
|
||||
|
||||
MappedFile *mf = new MappedFile;
|
||||
mf->name = path;
|
||||
mf->size = size;
|
||||
mf->fd = fd;
|
||||
|
||||
if (size > 0) {
|
||||
HANDLE h = CreateFileMapping(fd, nullptr, PAGE_READONLY, 0, size, nullptr);
|
||||
if (!h) {
|
||||
error = path + ": CreateFileMapping failed: " + errno_string();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
mf->data = (u8 *)MapViewOfFile(h, FILE_MAP_COPY, 0, 0, size);
|
||||
CloseHandle(h);
|
||||
|
||||
if (!mf->data) {
|
||||
error = path + ": MapViewOfFile failed: " + errno_string();
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return mf;
|
||||
}
|
||||
|
||||
void MappedFile::unmap() {
|
||||
if (size == 0 || parent || !data)
|
||||
return;
|
||||
|
||||
UnmapViewOfFile(data);
|
||||
if (fd != INVALID_HANDLE_VALUE)
|
||||
CloseHandle(fd);
|
||||
|
||||
data = nullptr;
|
||||
}
|
||||
|
||||
void MappedFile::close_fd() {
|
||||
if (fd == INVALID_HANDLE_VALUE)
|
||||
return;
|
||||
CloseHandle(fd);
|
||||
fd = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
|
||||
void MappedFile::reopen_fd(const std::string &path) {
|
||||
if (fd == INVALID_HANDLE_VALUE)
|
||||
fd = CreateFileA(path.c_str(), GENERIC_READ,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,24 +0,0 @@
|
||||
#include "common.h"
|
||||
|
||||
// Including mimalloc-new-delete.h overrides new/delete operators.
|
||||
// We need it only when we are using mimalloc as a dynamic library.
|
||||
#if MOLD_USE_SYSTEM_MIMALLOC
|
||||
# include <mimalloc-new-delete.h>
|
||||
#endif
|
||||
|
||||
// Silence mimalloc warning messages that users can just ignore.
|
||||
#if MOLD_USE_MIMALLOC
|
||||
# include <mimalloc.h>
|
||||
|
||||
namespace mold {
|
||||
void set_mimalloc_options() {
|
||||
mi_option_disable(mi_option_verbose);
|
||||
mi_option_disable(mi_option_show_errors);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
namespace mold {
|
||||
void set_mimalloc_options() {}
|
||||
}
|
||||
#endif
|
||||
@@ -1,20 +0,0 @@
|
||||
#include "common.h"
|
||||
|
||||
#include <random>
|
||||
|
||||
namespace mold {
|
||||
|
||||
void get_random_bytes(u8 *buf, i64 size) {
|
||||
std::random_device rand;
|
||||
i64 i = 0;
|
||||
|
||||
for (; i < size - 4; i += 4) {
|
||||
u32 val = rand();
|
||||
memcpy(buf + i, &val, 4);
|
||||
}
|
||||
|
||||
u32 val = rand();
|
||||
memcpy(buf + i, &val, size - i);
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,88 +0,0 @@
|
||||
#include "common.h"
|
||||
|
||||
#include <csignal>
|
||||
#include <tbb/version.h>
|
||||
|
||||
#ifdef __FreeBSD__
|
||||
# include <sys/sysctl.h>
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace mold {
|
||||
|
||||
std::string errno_string() {
|
||||
// strerror is not thread-safe, so guard it with a lock.
|
||||
static std::mutex mu;
|
||||
std::scoped_lock lock(mu);
|
||||
return strerror(errno);
|
||||
}
|
||||
|
||||
void cleanup() {
|
||||
if (output_tmpfile)
|
||||
unlink(output_tmpfile);
|
||||
}
|
||||
|
||||
// mold mmap's an output file, and the mmap succeeds even if there's
|
||||
// no enough space left on the filesystem. The actual disk blocks are
|
||||
// not allocated on the mmap call but when the program writes to it
|
||||
// for the first time.
|
||||
//
|
||||
// If a disk becomes full as a result of a write to an mmap'ed memory
|
||||
// region, the failure of the write is reported as a SIGBUS or structured
|
||||
// exeption with code EXCEPTION_IN_PAGE_ERROR on Windows. This
|
||||
// signal handler catches that signal and prints out a user-friendly
|
||||
// error message. Without this, it is very hard to realize that the
|
||||
// disk might be full.
|
||||
static std::string sigabrt_msg;
|
||||
|
||||
static void sighandler(int signo, siginfo_t *info, void *ucontext) {
|
||||
static std::mutex mu;
|
||||
std::scoped_lock lock{mu};
|
||||
|
||||
// Handle disk full error
|
||||
switch (signo) {
|
||||
case SIGSEGV:
|
||||
case SIGBUS:
|
||||
if (output_buffer_start <= info->si_addr &&
|
||||
info->si_addr < output_buffer_end) {
|
||||
const char msg[] = "mold: failed to write to an output file. Disk full?\n";
|
||||
(void)!write(STDERR_FILENO, msg, sizeof(msg) - 1);
|
||||
}
|
||||
break;
|
||||
case SIGABRT: {
|
||||
(void)!write(STDERR_FILENO, &sigabrt_msg[0], sigabrt_msg.size());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Re-throw the signal
|
||||
signal(SIGSEGV, SIG_DFL);
|
||||
signal(SIGBUS, SIG_DFL);
|
||||
signal(SIGABRT, SIG_DFL);
|
||||
|
||||
cleanup();
|
||||
raise(signo);
|
||||
}
|
||||
|
||||
void install_signal_handler() {
|
||||
struct sigaction action;
|
||||
action.sa_sigaction = sighandler;
|
||||
sigemptyset(&action.sa_mask);
|
||||
action.sa_flags = SA_SIGINFO;
|
||||
|
||||
sigaction(SIGSEGV, &action, NULL);
|
||||
sigaction(SIGBUS, &action, NULL);
|
||||
|
||||
// OneTBB 2021.9.0 has the interface version 12090.
|
||||
if (TBB_runtime_interface_version() < 12090) {
|
||||
sigabrt_msg = "mold: aborted\n"
|
||||
"mold: mold with libtbb version 2021.9.0 or older is known to be unstable "
|
||||
"under heavy load. Your libtbb version is " +
|
||||
std::string(TBB_runtime_version()) +
|
||||
". Please upgrade your libtbb library and try again.\n";
|
||||
|
||||
sigaction(SIGABRT, &action, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,55 +0,0 @@
|
||||
#include "common.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
namespace mold {
|
||||
|
||||
void cleanup() {
|
||||
if (output_tmpfile)
|
||||
_unlink(output_tmpfile);
|
||||
}
|
||||
|
||||
std::string errno_string() {
|
||||
LPVOID buf;
|
||||
DWORD dw = GetLastError();
|
||||
|
||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
nullptr, dw,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPTSTR)&buf, 0, nullptr);
|
||||
|
||||
std::string ret = (char *)buf;
|
||||
LocalFree(buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static LONG WINAPI vectored_handler(_EXCEPTION_POINTERS *exception_info) {
|
||||
static std::mutex mu;
|
||||
std::scoped_lock lock{mu};
|
||||
|
||||
PEXCEPTION_RECORD rec = exception_info->ExceptionRecord;
|
||||
ULONG_PTR *p = rec->ExceptionInformation;
|
||||
|
||||
if (rec->ExceptionCode == EXCEPTION_IN_PAGE_ERROR &&
|
||||
(ULONG_PTR)output_buffer_start <= p[1] &&
|
||||
p[1] < (ULONG_PTR)output_buffer_end) {
|
||||
static const char msg[] =
|
||||
"mold: failed to write to an output file. Disk full?\n";
|
||||
(void)!_write(_fileno(stderr), msg, sizeof(msg) - 1);
|
||||
} else if (rec->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
|
||||
static const char msg[] =
|
||||
"mold: stack overflow\n";
|
||||
(void)!_write(_fileno(stderr), msg, sizeof(msg) - 1);
|
||||
}
|
||||
|
||||
cleanup();
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
void install_signal_handler() {
|
||||
AddVectoredExceptionHandler(0, vectored_handler);
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
-144
@@ -1,144 +0,0 @@
|
||||
// This is a header-only C++20 implementation of SipHash based on the
|
||||
// reference implementation. To use, just copy this header file into
|
||||
// your project and #include it.
|
||||
//
|
||||
// https://github.com/rui314/siphash/blob/main/siphash.h
|
||||
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
template <int C_ROUNDS, int D_ROUNDS, int OUTLEN>
|
||||
class SipHashTmpl {
|
||||
public:
|
||||
static_assert(OUTLEN == 64 || OUTLEN == 128);
|
||||
|
||||
SipHashTmpl(void *key) {
|
||||
uint64_t k0 = read64(key);
|
||||
uint64_t k1 = read64((char *)key + 8);
|
||||
|
||||
v0 = 0x736f6d6570736575 ^ k0;
|
||||
v1 = 0x646f72616e646f6d ^ k1;
|
||||
v2 = 0x6c7967656e657261 ^ k0;
|
||||
v3 = 0x7465646279746573 ^ k1;
|
||||
|
||||
if (OUTLEN == 128)
|
||||
v1 ^= 0xee;
|
||||
}
|
||||
|
||||
void update(void *msgp, int64_t msglen) {
|
||||
char *msg = (char *)msgp;
|
||||
sum += msglen;
|
||||
|
||||
if (buflen) {
|
||||
if (buflen + msglen < 8) {
|
||||
memcpy(buf + buflen, msg, msglen);
|
||||
buflen += msglen;
|
||||
return;
|
||||
}
|
||||
|
||||
int j = 8 - buflen;
|
||||
memcpy(buf + buflen, msg, j);
|
||||
compress(read64(buf));
|
||||
|
||||
msg += j;
|
||||
msglen -= j;
|
||||
buflen = 0;
|
||||
}
|
||||
|
||||
while (msglen >= 8) {
|
||||
compress(read64(msg));
|
||||
msg += 8;
|
||||
msglen -= 8;
|
||||
}
|
||||
|
||||
memcpy(buf, msg, msglen);
|
||||
buflen = msglen;
|
||||
}
|
||||
|
||||
void finish(void *out) {
|
||||
memset(buf + buflen, 0, 8 - buflen);
|
||||
compress(((uint64_t)sum << 56) | read64(buf));
|
||||
|
||||
v2 ^= (OUTLEN == 128) ? 0xee : 0xff;
|
||||
finalize();
|
||||
write64(out, v0 ^ v1 ^ v2 ^ v3);
|
||||
|
||||
if (OUTLEN == 128) {
|
||||
v1 ^= 0xdd;
|
||||
finalize();
|
||||
write64((char *)out + 8, v0 ^ v1 ^ v2 ^ v3);
|
||||
}
|
||||
}
|
||||
|
||||
static void hash(void *out, void *key, void *in, int inlen) {
|
||||
SipHashTmpl<C_ROUNDS, D_ROUNDS, OUTLEN> h(key);
|
||||
h.update(in, inlen);
|
||||
h.finish(out);
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t v0, v1, v2, v3;
|
||||
uint8_t buf[8];
|
||||
uint8_t buflen = 0;
|
||||
uint8_t sum = 0;
|
||||
|
||||
uint64_t read64(void *loc) {
|
||||
uint64_t val;
|
||||
memcpy(&val, loc, 8);
|
||||
if (std::endian::native == std::endian::big)
|
||||
val = bswap(val);
|
||||
return val;
|
||||
}
|
||||
|
||||
void write64(void *loc, uint64_t val) {
|
||||
if (std::endian::native == std::endian::big)
|
||||
val = bswap(val);
|
||||
memcpy(loc, &val, 8);
|
||||
}
|
||||
|
||||
uint64_t bswap(uint64_t val) {
|
||||
return ((val << 56) & 0xff00000000000000) |
|
||||
((val << 40) & 0x00ff000000000000) |
|
||||
((val << 24) & 0x0000ff0000000000) |
|
||||
((val << 8) & 0x000000ff00000000) |
|
||||
((val >> 8) & 0x00000000ff000000) |
|
||||
((val >> 24) & 0x0000000000ff0000) |
|
||||
((val >> 40) & 0x000000000000ff00) |
|
||||
((val >> 56) & 0x00000000000000ff);
|
||||
}
|
||||
|
||||
void round() {
|
||||
v0 += v1;
|
||||
v1 = std::rotl(v1, 13);
|
||||
v1 ^= v0;
|
||||
v0 = std::rotl(v0, 32);
|
||||
v2 += v3;
|
||||
v3 = std::rotl(v3, 16);
|
||||
v3 ^= v2;
|
||||
v0 += v3;
|
||||
v3 = std::rotl(v3, 21);
|
||||
v3 ^= v0;
|
||||
v2 += v1;
|
||||
v1 = std::rotl(v1, 17);
|
||||
v1 ^= v2;
|
||||
v2 = std::rotl(v2, 32);
|
||||
}
|
||||
|
||||
void compress(uint64_t m) {
|
||||
v3 ^= m;
|
||||
for (int i = 0; i < C_ROUNDS; i++)
|
||||
round();
|
||||
v0 ^= m;
|
||||
}
|
||||
|
||||
void finalize() {
|
||||
for (int i = 0; i < D_ROUNDS; i++)
|
||||
round();
|
||||
}
|
||||
};
|
||||
|
||||
using SipHash = SipHashTmpl<2, 4, 64>;
|
||||
using SipHash128 = SipHashTmpl<2, 4, 128>;
|
||||
using SipHash13 = SipHashTmpl<1, 3, 64>;
|
||||
using SipHash13_128 = SipHashTmpl<1, 3, 128>;
|
||||
-114
@@ -1,114 +0,0 @@
|
||||
// This file contains functions to create a tar file.
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
# define ftruncate _chsize_s
|
||||
#endif
|
||||
|
||||
namespace mold {
|
||||
|
||||
static constexpr i64 BLOCK_SIZE = 512;
|
||||
|
||||
// A tar file consists of one or more Ustar header followed by data.
|
||||
// Each Ustar header represents a single file in an archive.
|
||||
//
|
||||
// tar is an old file format, and its `name` field is only 100 bytes long.
|
||||
// If `name` is longer than 100 bytes, we can emit a PAX header before a
|
||||
// Ustar header to store a long filename.
|
||||
//
|
||||
// For simplicity, we always emit a PAX header even for a short filename.
|
||||
struct UstarHeader {
|
||||
char name[100];
|
||||
char mode[8];
|
||||
char uid[8];
|
||||
char gid[8];
|
||||
char size[12];
|
||||
char mtime[12];
|
||||
char checksum[8];
|
||||
char typeflag[1];
|
||||
char linkname[100];
|
||||
char magic[6];
|
||||
char version[2];
|
||||
char uname[32];
|
||||
char gname[32];
|
||||
char devmajor[8];
|
||||
char devminor[8];
|
||||
char prefix[155];
|
||||
char pad[12];
|
||||
};
|
||||
|
||||
static_assert(sizeof(UstarHeader) == BLOCK_SIZE);
|
||||
|
||||
static void finalize(UstarHeader &hdr) {
|
||||
memset(hdr.checksum, ' ', sizeof(hdr.checksum));
|
||||
memcpy(hdr.magic, "ustar", 5);
|
||||
memcpy(hdr.version, "00", 2);
|
||||
|
||||
// Compute checksum
|
||||
int sum = 0;
|
||||
for (i64 i = 0; i < sizeof(hdr); i++)
|
||||
sum += ((u8 *)&hdr)[i];
|
||||
|
||||
// We need to convince the compiler that sum isn't too big to silence
|
||||
// -Werror=format-truncation.
|
||||
if (sum >= 01'000'000)
|
||||
unreachable();
|
||||
snprintf(hdr.checksum, sizeof(hdr.checksum), "%06o", sum);
|
||||
}
|
||||
|
||||
static std::string encode_path(std::string basedir, std::string path) {
|
||||
path = path_clean(basedir + "/" + path);
|
||||
|
||||
// Construct a string which contains something like
|
||||
// "16 path=foo/bar\n" where 16 is the size of the string
|
||||
// including the size string itself.
|
||||
i64 len = std::string(" path=\n").size() + path.size();
|
||||
i64 total = std::to_string(len).size() + len;
|
||||
total = std::to_string(total).size() + len;
|
||||
return std::to_string(total) + " path=" + path + "\n";
|
||||
}
|
||||
|
||||
std::unique_ptr<TarWriter>
|
||||
TarWriter::open(std::string output_path, std::string basedir) {
|
||||
FILE *out = fopen(output_path.c_str(), "w");
|
||||
if (!out)
|
||||
return nullptr;
|
||||
return std::unique_ptr<TarWriter>(new TarWriter(out, basedir));
|
||||
}
|
||||
|
||||
TarWriter::~TarWriter() {
|
||||
fclose(out);
|
||||
}
|
||||
|
||||
void TarWriter::append(std::string path, std::string_view data) {
|
||||
// Write PAX header
|
||||
UstarHeader pax = {};
|
||||
|
||||
std::string attr = encode_path(basedir, path);
|
||||
snprintf(pax.size, sizeof(pax.size), "%011zo", attr.size());
|
||||
pax.name[0] = '/';
|
||||
pax.typeflag[0] = 'x';
|
||||
finalize(pax);
|
||||
fwrite(&pax, sizeof(pax), 1, out);
|
||||
|
||||
// Write pathname
|
||||
fwrite(attr.data(), attr.size(), 1, out);
|
||||
fseek(out, align_to(ftell(out), BLOCK_SIZE), SEEK_SET);
|
||||
|
||||
// Write Ustar header
|
||||
UstarHeader ustar = {};
|
||||
memcpy(ustar.mode, "0000664", 8);
|
||||
snprintf(ustar.size, sizeof(ustar.size), "%011zo", data.size());
|
||||
finalize(ustar);
|
||||
fwrite(&ustar, sizeof(ustar), 1, out);
|
||||
|
||||
// Write file contents
|
||||
fwrite(data.data(), data.size(), 1, out);
|
||||
fseek(out, align_to(ftell(out), BLOCK_SIZE), SEEK_SET);
|
||||
|
||||
// A tar file must ends with two empty blocks
|
||||
(void)!ftruncate(fileno(out), ftell(out) + BLOCK_SIZE * 2);
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,33 +0,0 @@
|
||||
# Get a git hash value. We do not want to use git command here
|
||||
# because we don't want to make git a build-time dependency.
|
||||
if(EXISTS "${SOURCE_DIR}/.git/HEAD")
|
||||
file(READ "${SOURCE_DIR}/.git/HEAD" HASH)
|
||||
string(STRIP "${HASH}" HASH)
|
||||
|
||||
if(HASH MATCHES "^ref: (.*)")
|
||||
set(HEAD "${CMAKE_MATCH_1}")
|
||||
if(EXISTS "${SOURCE_DIR}/.git/${HEAD}")
|
||||
file(READ "${SOURCE_DIR}/.git/${HEAD}" HASH)
|
||||
string(STRIP "${HASH}" HASH)
|
||||
else()
|
||||
file(READ "${SOURCE_DIR}/.git/packed-refs" PACKED_REFS)
|
||||
string(REGEX REPLACE ".*\n([0-9a-f]+) ${HEAD}\n.*" "\\1" HASH "\n${PACKED_REFS}")
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Create new file contents and update a given file if necessary.
|
||||
set(NEW_CONTENTS "#include <string>
|
||||
namespace mold {
|
||||
std::string mold_git_hash = \"${HASH}\";
|
||||
}
|
||||
")
|
||||
|
||||
if(EXISTS "${OUTPUT_FILE}")
|
||||
file(READ "${OUTPUT_FILE}" OLD_CONTENTS)
|
||||
if(NOT "${NEW_CONTENTS}" STREQUAL "${OLD_CONTENTS}")
|
||||
file(WRITE "${OUTPUT_FILE}" "${NEW_CONTENTS}")
|
||||
endif()
|
||||
else()
|
||||
file(WRITE "${OUTPUT_FILE}" "${NEW_CONTENTS}")
|
||||
endif()
|
||||
@@ -0,0 +1,258 @@
|
||||
#include "mold.h"
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
// Returns [hi:lo] bits of val.
|
||||
static u64 bits(u64 val, u64 hi, u64 lo) {
|
||||
return (val >> lo) & (((u64)1 << (hi - lo + 1)) - 1);
|
||||
}
|
||||
|
||||
static u64 page(u64 val) {
|
||||
return val & 0xffff'ffff'ffff'f000;
|
||||
}
|
||||
|
||||
static u64 encode_page(u64 val) {
|
||||
return (bits(val, 13, 12) << 29) | (bits(val, 32, 14) << 5);
|
||||
}
|
||||
|
||||
template <>
|
||||
void StubsSection<ARM64>::copy_buf(Context<ARM64> &ctx) {
|
||||
u32 *buf = (u32 *)(ctx.buf + this->hdr.offset);
|
||||
|
||||
for (i64 i = 0; i < syms.size(); i++) {
|
||||
static const u32 insn[] = {
|
||||
0x90000010, // adrp x16, $ptr@PAGE
|
||||
0xf9400210, // ldr x16, [x16, $ptr@PAGEOFF]
|
||||
0xd61f0200, // br x16
|
||||
};
|
||||
|
||||
static_assert(sizeof(insn) == ARM64::stub_size);
|
||||
|
||||
u64 la_addr = ctx.lazy_symbol_ptr.hdr.addr + ARM64::word_size * i;
|
||||
u64 this_addr = this->hdr.addr + ARM64::stub_size * i;
|
||||
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
buf[0] |= encode_page(page(la_addr) - page(this_addr));
|
||||
buf[1] |= bits(la_addr, 11, 3) << 10;
|
||||
buf += 3;
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void StubHelperSection<ARM64>::copy_buf(Context<ARM64> &ctx) {
|
||||
u32 *start = (u32 *)(ctx.buf + this->hdr.offset);
|
||||
u32 *buf = start;
|
||||
|
||||
static const u32 insn0[] = {
|
||||
0x90000011, // adrp x17, $__dyld_private@PAGE
|
||||
0x91000231, // add x17, x17, $__dyld_private@PAGEOFF
|
||||
0xa9bf47f0, // stp x16, x17, [sp, #-16]!
|
||||
0x90000010, // adrp x16, $dyld_stub_binder@PAGE
|
||||
0xf9400210, // ldr x16, [x16, $dyld_stub_binder@PAGEOFF]
|
||||
0xd61f0200, // br x16
|
||||
};
|
||||
|
||||
static_assert(sizeof(insn0) == ARM64::stub_helper_hdr_size);
|
||||
memcpy(buf, insn0, sizeof(insn0));
|
||||
|
||||
u64 dyld_private = get_symbol(ctx, "__dyld_private")->get_addr(ctx);
|
||||
buf[0] |= encode_page(page(dyld_private) - page(this->hdr.addr));
|
||||
buf[1] |= bits(dyld_private, 11, 0) << 10;
|
||||
|
||||
u64 stub_binder = get_symbol(ctx, "dyld_stub_binder")->get_addr(ctx);
|
||||
buf[3] |= encode_page(page(stub_binder) - page(this->hdr.addr - 12));
|
||||
buf[4] |= bits(stub_binder, 11, 0) << 10;
|
||||
|
||||
buf += 6;
|
||||
|
||||
for (i64 i = 0; i < ctx.stubs.syms.size(); i++) {
|
||||
static const u32 insn[] = {
|
||||
0x18000050, // ldr w16, addr
|
||||
0x14000000, // b stubHelperHeader
|
||||
0x00000000, // addr: .long <idx>
|
||||
};
|
||||
|
||||
static_assert(sizeof(insn) == ARM64::stub_helper_size);
|
||||
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
buf[1] |= bits((start - buf - 1) * 4, 27, 2);
|
||||
buf[2] = ctx.stubs.bind_offsets[i];
|
||||
buf += 3;
|
||||
}
|
||||
}
|
||||
|
||||
static i64 read_addend(u8 *buf, const MachRel &r) {
|
||||
if (r.p2size == 2)
|
||||
return *(i32 *)(buf + r.offset);
|
||||
if (r.p2size == 3)
|
||||
return *(i64 *)(buf + r.offset);
|
||||
unreachable();
|
||||
}
|
||||
|
||||
static Relocation<ARM64>
|
||||
read_reloc(Context<ARM64> &ctx, ObjectFile<ARM64> &file,
|
||||
const MachSection &hdr, MachRel *rels, i64 &idx) {
|
||||
i64 addend = 0;
|
||||
|
||||
switch (rels[idx].type) {
|
||||
case ARM64_RELOC_UNSIGNED:
|
||||
case ARM64_RELOC_SUBTRACTOR:
|
||||
addend = read_addend((u8 *)file.mf->data + hdr.offset, rels[idx]);
|
||||
break;
|
||||
case ARM64_RELOC_ADDEND:
|
||||
addend = rels[idx++].offset;
|
||||
break;
|
||||
}
|
||||
|
||||
MachRel &r = rels[idx];
|
||||
Relocation<ARM64> rel{r.offset, (u8)r.type, (u8)r.p2size, (bool)r.is_pcrel};
|
||||
|
||||
if (r.is_extern) {
|
||||
rel.sym = file.syms[r.idx];
|
||||
rel.addend = addend;
|
||||
return rel;
|
||||
}
|
||||
|
||||
u32 addr;
|
||||
if (r.is_pcrel)
|
||||
addr = hdr.addr + r.offset + addend;
|
||||
else
|
||||
addr = addend;
|
||||
|
||||
Subsection<ARM64> *target = file.find_subsection(ctx, addr);
|
||||
if (!target)
|
||||
Fatal(ctx) << file << ": bad relocation: " << r.offset;
|
||||
|
||||
rel.subsec = target;
|
||||
rel.addend = addr - target->input_addr;
|
||||
return rel;
|
||||
}
|
||||
|
||||
template <>
|
||||
std::vector<Relocation<ARM64>>
|
||||
read_relocations(Context<ARM64> &ctx, ObjectFile<ARM64> &file,
|
||||
const MachSection &hdr) {
|
||||
std::vector<Relocation<ARM64>> vec;
|
||||
|
||||
MachRel *rels = (MachRel *)(file.mf->data + hdr.reloff);
|
||||
for (i64 i = 0; i < hdr.nreloc; i++)
|
||||
vec.push_back(read_reloc(ctx, file, hdr, rels, i));
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <>
|
||||
void Subsection<ARM64>::scan_relocations(Context<ARM64> &ctx) {
|
||||
for (Relocation<ARM64> &r : get_rels()) {
|
||||
Symbol<ARM64> *sym = r.sym;
|
||||
if (!sym)
|
||||
continue;
|
||||
|
||||
switch (r.type) {
|
||||
case ARM64_RELOC_GOT_LOAD_PAGE21:
|
||||
case ARM64_RELOC_GOT_LOAD_PAGEOFF12:
|
||||
case ARM64_RELOC_POINTER_TO_GOT:
|
||||
sym->flags |= NEEDS_GOT;
|
||||
break;
|
||||
case ARM64_RELOC_TLVP_LOAD_PAGE21:
|
||||
case ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
|
||||
sym->flags |= NEEDS_THREAD_PTR;
|
||||
break;
|
||||
}
|
||||
|
||||
if (sym->file && sym->file->is_dylib) {
|
||||
sym->flags |= NEEDS_STUB;
|
||||
((DylibFile<ARM64> *)sym->file)->is_needed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void Subsection<ARM64>::apply_reloc(Context<ARM64> &ctx, u8 *buf) {
|
||||
std::span<Relocation<ARM64>> rels = get_rels();
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
Relocation<ARM64> &r = rels[i];
|
||||
|
||||
if (r.sym && !r.sym->file) {
|
||||
Error(ctx) << "undefined symbol: " << isec.file << ": " << *r.sym;
|
||||
continue;
|
||||
}
|
||||
|
||||
u64 val = 0;
|
||||
|
||||
switch (r.type) {
|
||||
case ARM64_RELOC_UNSIGNED:
|
||||
case ARM64_RELOC_BRANCH26:
|
||||
case ARM64_RELOC_PAGE21:
|
||||
case ARM64_RELOC_PAGEOFF12:
|
||||
val = r.sym ? r.sym->get_addr(ctx) : r.subsec->get_addr(ctx);
|
||||
break;
|
||||
case ARM64_RELOC_SUBTRACTOR: {
|
||||
Relocation<ARM64> s = rels[++i];
|
||||
assert(s.type == ARM64_RELOC_UNSIGNED);
|
||||
u64 val1 = r.sym ? r.sym->get_addr(ctx) : r.subsec->get_addr(ctx);
|
||||
u64 val2 = s.sym ? s.sym->get_addr(ctx) : s.subsec->get_addr(ctx);
|
||||
val = val2 - val1;
|
||||
break;
|
||||
}
|
||||
case ARM64_RELOC_GOT_LOAD_PAGE21:
|
||||
case ARM64_RELOC_GOT_LOAD_PAGEOFF12:
|
||||
case ARM64_RELOC_POINTER_TO_GOT:
|
||||
val = r.sym->get_got_addr(ctx);
|
||||
break;
|
||||
case ARM64_RELOC_TLVP_LOAD_PAGE21:
|
||||
case ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
|
||||
val = r.sym->get_tlv_addr(ctx);
|
||||
break;
|
||||
default:
|
||||
Fatal(ctx) << isec << ": unknown reloc: " << (int)r.type;
|
||||
}
|
||||
|
||||
val += r.addend;
|
||||
|
||||
switch (r.type) {
|
||||
case ARM64_RELOC_UNSIGNED:
|
||||
case ARM64_RELOC_SUBTRACTOR:
|
||||
case ARM64_RELOC_POINTER_TO_GOT:
|
||||
if (r.is_pcrel)
|
||||
val -= get_addr(ctx) + r.offset;
|
||||
|
||||
if (r.p2size == 2)
|
||||
*(i32 *)(buf + r.offset) = val;
|
||||
else if (r.p2size == 3)
|
||||
*(i64 *)(buf + r.offset) = val;
|
||||
else
|
||||
unreachable();
|
||||
break;
|
||||
case ARM64_RELOC_BRANCH26:
|
||||
if (r.is_pcrel)
|
||||
val -= get_addr(ctx) + r.offset;
|
||||
*(u32 *)(buf + r.offset) |= bits(val, 27, 2);
|
||||
break;
|
||||
case ARM64_RELOC_PAGE21:
|
||||
case ARM64_RELOC_GOT_LOAD_PAGE21:
|
||||
case ARM64_RELOC_TLVP_LOAD_PAGE21:
|
||||
assert(r.is_pcrel);
|
||||
*(u32 *)(buf + r.offset) |=
|
||||
encode_page(page(val) - page(get_addr(ctx) + r.offset));
|
||||
break;
|
||||
case ARM64_RELOC_PAGEOFF12:
|
||||
case ARM64_RELOC_GOT_LOAD_PAGEOFF12:
|
||||
case ARM64_RELOC_TLVP_LOAD_PAGEOFF12: {
|
||||
if (r.is_pcrel)
|
||||
val -= get_addr(ctx) + r.offset;
|
||||
|
||||
u32 insn = *(u32 *)(buf + r.offset);
|
||||
i64 scale = 0;
|
||||
if ((insn & 0x3b000000) == 0x39000000)
|
||||
scale = insn >> 30;
|
||||
*(u32 *)(buf + r.offset) |= bits(val, 11, scale) << 10;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
Fatal(ctx) << isec << ": unknown reloc: " << (int)r.type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mold::macho
|
||||
@@ -0,0 +1,222 @@
|
||||
#include "mold.h"
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
template <>
|
||||
void StubsSection<X86_64>::copy_buf(Context<X86_64> &ctx) {
|
||||
u8 *buf = ctx.buf + this->hdr.offset;
|
||||
|
||||
for (i64 i = 0; i < syms.size(); i++) {
|
||||
// `ff 25 xx xx xx xx` is a RIP-relative indirect jump instruction,
|
||||
// i.e., `jmp *IMM(%rip)`. It loads an address from la_symbol_ptr
|
||||
// and jump there.
|
||||
static_assert(X86_64::stub_size == 6);
|
||||
buf[i * 6] = 0xff;
|
||||
buf[i * 6 + 1] = 0x25;
|
||||
*(u32 *)(buf + i * 6 + 2) =
|
||||
ctx.lazy_symbol_ptr.hdr.addr + i * X86_64::word_size -
|
||||
(this->hdr.addr + i * 6 + 6);
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void StubHelperSection<X86_64>::copy_buf(Context<X86_64> &ctx) {
|
||||
u8 *start = ctx.buf + this->hdr.offset;
|
||||
u8 *buf = start;
|
||||
|
||||
static const u8 insn0[] = {
|
||||
0x4c, 0x8d, 0x1d, 0, 0, 0, 0, // lea $__dyld_private(%rip), %r11
|
||||
0x41, 0x53, // push %r11
|
||||
0xff, 0x25, 0, 0, 0, 0, // jmp *$dyld_stub_binder@GOT(%rip)
|
||||
0x90, // nop
|
||||
};
|
||||
|
||||
static_assert(sizeof(insn0) == X86_64::stub_helper_hdr_size);
|
||||
|
||||
memcpy(buf, insn0, sizeof(insn0));
|
||||
*(u32 *)(buf + 3) =
|
||||
get_symbol(ctx, "__dyld_private")->get_addr(ctx) - this->hdr.addr - 7;
|
||||
*(u32 *)(buf + 11) =
|
||||
get_symbol(ctx, "dyld_stub_binder")->get_got_addr(ctx) - this->hdr.addr - 15;
|
||||
|
||||
buf += 16;
|
||||
|
||||
for (i64 i = 0; i < ctx.stubs.syms.size(); i++) {
|
||||
u8 insn[] = {
|
||||
0x68, 0, 0, 0, 0, // push $bind_offset
|
||||
0xe9, 0, 0, 0, 0, // jmp $__stub_helper
|
||||
};
|
||||
|
||||
static_assert(sizeof(insn) == X86_64::stub_helper_size);
|
||||
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
*(u32 *)(buf + 1) = ctx.stubs.bind_offsets[i];
|
||||
*(u32 *)(buf + 6) = start - buf - 10;
|
||||
buf += 10;
|
||||
}
|
||||
}
|
||||
|
||||
static i64 get_reloc_addend(u32 type) {
|
||||
switch (type) {
|
||||
case X86_64_RELOC_SIGNED_1:
|
||||
return 1;
|
||||
case X86_64_RELOC_SIGNED_2:
|
||||
return 2;
|
||||
case X86_64_RELOC_SIGNED_4:
|
||||
return 4;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static i64 read_addend(u8 *buf, const MachRel &r) {
|
||||
if (r.p2size == 2)
|
||||
return *(i32 *)(buf + r.offset) + get_reloc_addend(r.type);
|
||||
if (r.p2size == 3)
|
||||
return *(i64 *)(buf + r.offset) + get_reloc_addend(r.type);
|
||||
unreachable();
|
||||
}
|
||||
|
||||
static Relocation<X86_64>
|
||||
read_reloc(Context<X86_64> &ctx, ObjectFile<X86_64> &file,
|
||||
const MachSection &hdr, MachRel &r) {
|
||||
if (r.p2size != 2 && r.p2size != 3)
|
||||
Fatal(ctx) << file << ": invalid r.p2size: " << (u32)r.p2size;
|
||||
|
||||
if (r.is_pcrel) {
|
||||
if (r.p2size != 2)
|
||||
Fatal(ctx) << file << ": invalid PC-relative reloc: " << r.offset;
|
||||
} else {
|
||||
if (r.p2size != 3)
|
||||
Fatal(ctx) << file << ": invalid non-PC-relative reloc: " << r.offset;
|
||||
}
|
||||
|
||||
u8 *buf = (u8 *)file.mf->data + hdr.offset;
|
||||
Relocation<X86_64> rel{r.offset, (u8)r.type, (u8)r.p2size, (bool)r.is_pcrel};
|
||||
i64 addend = read_addend(buf, r);
|
||||
|
||||
if (r.is_extern) {
|
||||
rel.sym = file.syms[r.idx];
|
||||
rel.addend = addend;
|
||||
return rel;
|
||||
}
|
||||
|
||||
u32 addr;
|
||||
if (r.is_pcrel)
|
||||
addr = hdr.addr + r.offset + 4 + addend;
|
||||
else
|
||||
addr = addend;
|
||||
|
||||
Subsection<X86_64> *target = file.find_subsection(ctx, addr);
|
||||
if (!target)
|
||||
Fatal(ctx) << file << ": bad relocation: " << r.offset;
|
||||
|
||||
rel.subsec = target;
|
||||
rel.addend = addr - target->input_addr;
|
||||
return rel;
|
||||
}
|
||||
|
||||
template <>
|
||||
std::vector<Relocation<X86_64>>
|
||||
read_relocations(Context<X86_64> &ctx, ObjectFile<X86_64> &file,
|
||||
const MachSection &hdr) {
|
||||
std::vector<Relocation<X86_64>> vec;
|
||||
vec.reserve(hdr.nreloc);
|
||||
|
||||
MachRel *rels = (MachRel *)(file.mf->data + hdr.reloff);
|
||||
for (i64 i = 0; i < hdr.nreloc; i++)
|
||||
vec.push_back(read_reloc(ctx, file, hdr, rels[i]));
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <>
|
||||
void Subsection<X86_64>::scan_relocations(Context<X86_64> &ctx) {
|
||||
for (Relocation<X86_64> &r : get_rels()) {
|
||||
Symbol<X86_64> *sym = r.sym;
|
||||
if (!sym)
|
||||
continue;
|
||||
|
||||
switch (r.type) {
|
||||
case X86_64_RELOC_GOT_LOAD:
|
||||
if (sym->file && sym->file->is_dylib)
|
||||
sym->flags |= NEEDS_GOT;
|
||||
break;
|
||||
case X86_64_RELOC_GOT:
|
||||
sym->flags |= NEEDS_GOT;
|
||||
break;
|
||||
case X86_64_RELOC_TLV:
|
||||
if (sym->file && sym->file->is_dylib)
|
||||
sym->flags |= NEEDS_THREAD_PTR;
|
||||
break;
|
||||
}
|
||||
|
||||
if (sym->file && sym->file->is_dylib) {
|
||||
sym->flags |= NEEDS_STUB;
|
||||
((DylibFile<X86_64> *)sym->file)->is_needed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void Subsection<X86_64>::apply_reloc(Context<X86_64> &ctx, u8 *buf) {
|
||||
for (const Relocation<X86_64> &r : get_rels()) {
|
||||
if (r.sym && !r.sym->file) {
|
||||
Error(ctx) << "undefined symbol: " << isec.file << ": " << *r.sym;
|
||||
continue;
|
||||
}
|
||||
|
||||
u64 val = 0;
|
||||
|
||||
switch (r.type) {
|
||||
case X86_64_RELOC_UNSIGNED:
|
||||
case X86_64_RELOC_SIGNED:
|
||||
case X86_64_RELOC_BRANCH:
|
||||
case X86_64_RELOC_SIGNED_1:
|
||||
case X86_64_RELOC_SIGNED_2:
|
||||
case X86_64_RELOC_SIGNED_4:
|
||||
val = r.sym ? r.sym->get_addr(ctx) : r.subsec->get_addr(ctx);
|
||||
break;
|
||||
case X86_64_RELOC_GOT_LOAD:
|
||||
if (r.sym->got_idx != -1) {
|
||||
val = r.sym->get_got_addr(ctx);
|
||||
} else {
|
||||
// Relax MOVQ into LEAQ
|
||||
if (buf[r.offset - 2] != 0x8b)
|
||||
Error(ctx) << isec << ": invalid GOT_LOAD relocation";
|
||||
buf[r.offset - 2] = 0x8d;
|
||||
val = r.sym->get_addr(ctx);
|
||||
}
|
||||
break;
|
||||
case X86_64_RELOC_GOT:
|
||||
val = r.sym->get_got_addr(ctx);
|
||||
break;
|
||||
case X86_64_RELOC_TLV:
|
||||
if (r.sym->tlv_idx != -1) {
|
||||
val = r.sym->get_tlv_addr(ctx);
|
||||
} else {
|
||||
// Relax MOVQ into LEAQ
|
||||
if (buf[r.offset - 2] != 0x8b)
|
||||
Error(ctx) << isec << ": invalid TLV relocation";
|
||||
buf[r.offset - 2] = 0x8d;
|
||||
val = r.sym->get_addr(ctx);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Fatal(ctx) << isec << ": unknown reloc: " << (int)r.type;
|
||||
}
|
||||
|
||||
val += r.addend;
|
||||
|
||||
if (r.is_pcrel)
|
||||
val -= get_addr(ctx) + r.offset + 4 + get_reloc_addend(r.type);
|
||||
|
||||
if (r.p2size == 2)
|
||||
*(u32 *)(buf + r.offset) = val;
|
||||
else if (r.p2size == 3)
|
||||
*(u64 *)(buf + r.offset) = val;
|
||||
else
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mold::macho
|
||||
@@ -0,0 +1,322 @@
|
||||
#include "mold.h"
|
||||
#include "../cmdline.h"
|
||||
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <regex>
|
||||
#include <unistd.h>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
static const char helpmsg[] = R"(
|
||||
Options:
|
||||
-F<PATH> Add DIR to framework search path
|
||||
-L<PATH> Add DIR to library search path
|
||||
-Z Do not search the standard directories when
|
||||
searching for libraries and frameworks
|
||||
-ObjC Load all static archive members that implement
|
||||
an Objective-C class or category
|
||||
-adhoc_codesign Add ad-hoc code signature to the output file
|
||||
-no_adhoc_codesign
|
||||
-arch <ARCH_NAME> Specify target architecture
|
||||
-bundle Produce a mach-o bundle
|
||||
-dead_strip Remove unreachable functions and data
|
||||
-dead_strip_dylibs Remove unreachable dylibs from dependencies
|
||||
-demangle Demangle C++ symbols in log messages (default)
|
||||
-dylib Produce a dynamic library
|
||||
-dynamic Link against dylibs (default)
|
||||
-e <SYMBOL> Specify the entry point of a main executable
|
||||
-execute Produce an executable (default)
|
||||
-filelist <FILE>[,<DIR>] Specify the list of input file names
|
||||
-framework <NAME>,[,<SUFFIX>]
|
||||
Search for a given framework
|
||||
-headerpad <SIZE> Allocate the size of padding after load commands
|
||||
-headerpad_max_install_names
|
||||
Allocate MAXPATHLEN byte padding after load commands
|
||||
-help Report usage information
|
||||
-l<LIB> Search for a given library
|
||||
-lto_library <FILE> Ignored
|
||||
-map <FILE> Write map file to a given file
|
||||
-needed-l<LIB> Search for a given library
|
||||
-needed-framework <NAME>[,<SUFFIX>]
|
||||
Search for a given framework
|
||||
-no_deduplicate Ignored
|
||||
-o <FILE> Set output filename
|
||||
-pagezero_size <SIZE> Specify the size of the __PAGEZERO segment
|
||||
-platform_version <PLATFORM> <MIN_VERSION> <SDK_VERSION>
|
||||
Set platform, platform version and SDK version
|
||||
-rpath <PATH> Add PATH to the runpath search path list
|
||||
-syslibroot <DIR> Prepend DIR to library search paths
|
||||
-t Print out each file the linker loads
|
||||
-v Report version information)";
|
||||
|
||||
template <typename E>
|
||||
static i64 parse_platform(Context<E> &ctx, std::string_view arg) {
|
||||
static std::regex re(R"(\d+)", std::regex_constants::ECMAScript);
|
||||
if (std::regex_match(arg.begin(), arg.end(), re))
|
||||
return stoi(std::string(arg));
|
||||
|
||||
if (arg == "macos")
|
||||
return PLATFORM_MACOS;
|
||||
if (arg == "ios")
|
||||
return PLATFORM_IOS;
|
||||
if (arg == "tvos")
|
||||
return PLATFORM_TVOS;
|
||||
if (arg == "watchos")
|
||||
return PLATFORM_WATCHOS;
|
||||
if (arg == "bridgeos")
|
||||
return PLATFORM_BRIDGEOS;
|
||||
if (arg == "mac-catalyst")
|
||||
return PLATFORM_MACCATALYST;
|
||||
if (arg == "ios-simulator")
|
||||
return PLATFORM_IOSSIMULATOR;
|
||||
if (arg == "tvos-simulator")
|
||||
return PLATFORM_TVOSSIMULATOR;
|
||||
if (arg == "watchos-simulator")
|
||||
return PLATFORM_WATCHOSSIMULATOR;
|
||||
if (arg == "driverkit")
|
||||
return PLATFORM_DRIVERKIT;
|
||||
Fatal(ctx) << "unknown -platform_version name: " << arg;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static i64 parse_version(Context<E> &ctx, std::string_view arg) {
|
||||
static std::regex re(R"((\d+)(?:\.(\d+))?(?:\.(\d+))?)",
|
||||
std::regex_constants::ECMAScript);
|
||||
std::cmatch m;
|
||||
if (!std::regex_match(arg.begin(), arg.end(), m, re))
|
||||
Fatal(ctx) << "malformed version number: " << arg;
|
||||
|
||||
i64 major = (m[1].length() == 0) ? 0 : stoi(m[1]);
|
||||
i64 minor = (m[2].length() == 0) ? 0 : stoi(m[2]);
|
||||
i64 patch = (m[3].length() == 0) ? 0 : stoi(m[3]);
|
||||
return (major << 16) | (minor << 8) | patch;
|
||||
}
|
||||
|
||||
static bool is_directory(std::filesystem::path path) {
|
||||
std::error_code ec;
|
||||
return std::filesystem::is_directory(path, ec) && !ec;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void parse_nonpositional_args(Context<E> &ctx,
|
||||
std::vector<std::string> &remaining) {
|
||||
std::vector<std::string_view> &args = ctx.cmdline_args;
|
||||
i64 i = 1;
|
||||
|
||||
std::vector<std::string> framework_paths;
|
||||
std::vector<std::string> library_paths;
|
||||
bool nostdlib = false;
|
||||
std::optional<i64> pagezero_size;
|
||||
|
||||
while (i < args.size()) {
|
||||
std::string_view arg;
|
||||
std::string_view arg2;
|
||||
std::string_view arg3;
|
||||
|
||||
auto read_arg = [&](std::string name) {
|
||||
if (args[i] == name) {
|
||||
if (args.size() <= i + 1)
|
||||
Fatal(ctx) << "option -" << name << ": argument missing";
|
||||
arg = args[i + 1];
|
||||
i += 2;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
auto read_arg3 = [&](std::string name) {
|
||||
if (args[i] == name) {
|
||||
if (args.size() <= i + 3)
|
||||
Fatal(ctx) << "option -" << name << ": argument missing";
|
||||
arg = args[i + 1];
|
||||
arg2 = args[i + 2];
|
||||
arg3 = args[i + 3];
|
||||
i += 4;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
auto read_joined = [&](std::string name) {
|
||||
if (read_arg(name))
|
||||
return true;
|
||||
if (args[i].starts_with(name)) {
|
||||
arg = args[i].substr(name.size());
|
||||
i++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
auto read_flag = [&](std::string name) {
|
||||
if (args[i] == name) {
|
||||
i++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
if (args[i].starts_with('@')) {
|
||||
std::vector<std::string_view> vec =
|
||||
read_response_file(ctx, args[i].substr(1));
|
||||
args.erase(args.begin() + i);
|
||||
args.insert(args.begin() + i, vec.begin(), vec.end());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (read_flag("-help") || read_flag("--help")) {
|
||||
SyncOut(ctx) << "Usage: " << ctx.cmdline_args[i]
|
||||
<< " [options] file...\n" << helpmsg;
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (read_joined("-F")) {
|
||||
framework_paths.push_back(std::string(arg));
|
||||
} else if (read_joined("-L")) {
|
||||
library_paths.push_back(std::string(arg));
|
||||
} else if (read_flag("-Z")) {
|
||||
nostdlib = true;
|
||||
} else if (read_flag("-ObjC")) {
|
||||
ctx.arg.ObjC = true;
|
||||
} else if (read_flag("-adhoc_codesign")) {
|
||||
ctx.arg.adhoc_codesign = true;
|
||||
} else if (read_flag("-no_adhoc_codesign")) {
|
||||
ctx.arg.adhoc_codesign = false;
|
||||
} else if (read_arg("-arch")) {
|
||||
if (arg == "x86_64")
|
||||
ctx.arg.arch = CPU_TYPE_X86_64;
|
||||
else if (arg == "arm64")
|
||||
ctx.arg.arch = CPU_TYPE_ARM64;
|
||||
else
|
||||
Fatal(ctx) << "unknown -arch: " << arg;
|
||||
} else if (read_flag("-bundle")) {
|
||||
ctx.output_type = MH_BUNDLE;
|
||||
} else if (read_flag("-color-diagnostics") ||
|
||||
read_flag("--color-diagnostics")) {
|
||||
} else if (read_flag("-dead_strip")) {
|
||||
ctx.arg.dead_strip = true;
|
||||
} else if (read_flag("-dead_strip_dylibs")) {
|
||||
ctx.arg.dead_strip_dylibs = true;
|
||||
} else if (read_flag("-demangle")) {
|
||||
ctx.arg.demangle = true;
|
||||
} else if (read_flag("-dylib")) {
|
||||
ctx.output_type = MH_DYLIB;
|
||||
} else if (read_arg("-headerpad")) {
|
||||
size_t pos;
|
||||
ctx.arg.headerpad = std::stol(std::string(arg), &pos, 16);
|
||||
if (pos != arg.size())
|
||||
Fatal(ctx) << "malformed -headerpad: " << arg;
|
||||
} else if (read_flag("-headerpad_max_install_names")) {
|
||||
ctx.arg.headerpad = 1024;
|
||||
} else if (read_flag("-dynamic")) {
|
||||
ctx.arg.dynamic = true;
|
||||
} else if (read_arg("-e")) {
|
||||
ctx.arg.entry = arg;
|
||||
} else if (read_flag("-execute")) {
|
||||
ctx.output_type = MH_EXECUTE;
|
||||
} else if (read_arg("-fatal_warnings")) {
|
||||
} else if (read_arg("-filelist")) {
|
||||
remaining.push_back("-filelist");
|
||||
remaining.push_back(std::string(arg));
|
||||
} else if (read_arg("-framework")) {
|
||||
remaining.push_back("-framework");
|
||||
remaining.push_back(std::string(arg));
|
||||
} else if (read_arg("-lto_library")) {
|
||||
} else if (read_joined("-l")) {
|
||||
remaining.push_back("-l");
|
||||
remaining.push_back(std::string(arg));
|
||||
} else if (read_arg("-map")) {
|
||||
ctx.arg.map = arg;
|
||||
} else if (read_joined("-needed-l")) {
|
||||
remaining.push_back("-needed-l");
|
||||
remaining.push_back(std::string(arg));
|
||||
} else if (read_arg("-needed_framework")) {
|
||||
remaining.push_back("-needed_framework");
|
||||
remaining.push_back(std::string(arg));
|
||||
} else if (read_flag("-no_deduplicate")) {
|
||||
} else if (read_arg("-o")) {
|
||||
ctx.arg.output = arg;
|
||||
} else if (read_arg("-pagezero_size")) {
|
||||
size_t pos;
|
||||
pagezero_size = std::stol(std::string(arg), &pos, 16);
|
||||
if (pos != arg.size())
|
||||
Fatal(ctx) << "malformed -pagezero_size: " << arg;
|
||||
} else if (read_arg3("-platform_version")) {
|
||||
ctx.arg.platform = parse_platform(ctx, arg);
|
||||
ctx.arg.platform_min_version = parse_version(ctx, arg2);
|
||||
ctx.arg.platform_sdk_version = parse_version(ctx, arg3);
|
||||
} else if (read_arg("-rpath")) {
|
||||
ctx.arg.rpath.push_back(std::string(arg));
|
||||
} else if (read_flag("-search_dylibs_first")) {
|
||||
Fatal(ctx) << "-search_dylibs_first is not supported";
|
||||
} else if (read_flag("-search_paths_first")) {
|
||||
} else if (read_arg("-syslibroot")) {
|
||||
ctx.arg.syslibroot.push_back(std::string(arg));
|
||||
} else if (read_flag("-t")) {
|
||||
ctx.arg.trace = true;
|
||||
} else if (read_flag("-v")) {
|
||||
SyncOut(ctx) << mold_version;
|
||||
} else {
|
||||
if (args[i][0] == '-')
|
||||
Fatal(ctx) << "unknown command line option: " << args[i];
|
||||
remaining.push_back(std::string(args[i]));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
auto add_search_path = [&](std::vector<std::string> &vec, std::string path) {
|
||||
if (!path.starts_with('/') || ctx.arg.syslibroot.empty()) {
|
||||
if (is_directory(path))
|
||||
vec.push_back(path);
|
||||
return;
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
for (std::string &dir : ctx.arg.syslibroot) {
|
||||
std::string str = path_clean(dir + "/" + path);
|
||||
if (is_directory(str)) {
|
||||
vec.push_back(str);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found && is_directory(path))
|
||||
vec.push_back(path);
|
||||
};
|
||||
|
||||
for (std::string &path : library_paths)
|
||||
add_search_path(ctx.arg.library_paths, path);
|
||||
|
||||
if (!nostdlib) {
|
||||
add_search_path(ctx.arg.library_paths, "/usr/lib");
|
||||
add_search_path(ctx.arg.library_paths, "/usr/local/lib");
|
||||
}
|
||||
|
||||
for (std::string &path : framework_paths)
|
||||
add_search_path(ctx.arg.framework_paths, path);
|
||||
|
||||
if (!nostdlib) {
|
||||
add_search_path(ctx.arg.framework_paths, "/Library/Frameworks");
|
||||
add_search_path(ctx.arg.framework_paths, "/System/Library/Frameworks");
|
||||
}
|
||||
|
||||
if (pagezero_size.has_value()) {
|
||||
if (ctx.output_type != MH_EXECUTE)
|
||||
Error(ctx) << "-pagezero_size option can only be used when"
|
||||
<< " linking a main executable";
|
||||
ctx.arg.pagezero_size = *pagezero_size;
|
||||
} else {
|
||||
ctx.arg.pagezero_size = (ctx.output_type == MH_EXECUTE) ? 0x100000000 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template void parse_nonpositional_args(Context<E> &, std::vector<std::string> &)
|
||||
|
||||
INSTANTIATE(ARM64);
|
||||
INSTANTIATE(X86_64);
|
||||
|
||||
} // namespace mold::macho
|
||||
@@ -0,0 +1,112 @@
|
||||
#include "mold.h"
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
template <typename E>
|
||||
static std::vector<Subsection<E> *> collect_root_set(Context<E> &ctx) {
|
||||
std::vector<Subsection<E> *> rootset;
|
||||
|
||||
auto mark = [&](Symbol<E> *sym) {
|
||||
if (sym && sym->subsec)
|
||||
rootset.push_back(sym->subsec);
|
||||
};
|
||||
|
||||
mark(get_symbol(ctx, ctx.arg.entry));
|
||||
|
||||
if (ctx.output_type == MH_DYLIB || ctx.output_type == MH_BUNDLE)
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
for (Symbol<E> *sym : file->syms)
|
||||
if (sym->file == file && sym->is_extern)
|
||||
mark(sym);
|
||||
|
||||
return rootset;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void visit(Context<E> &ctx, Subsection<E> &subsec) {
|
||||
if (subsec.is_alive.exchange(true))
|
||||
return;
|
||||
|
||||
for (Relocation<E> &rel : subsec.get_rels()) {
|
||||
if (rel.sym) {
|
||||
if (rel.sym->subsec)
|
||||
visit(ctx, *rel.sym->subsec);
|
||||
} else {
|
||||
visit(ctx, *rel.subsec);
|
||||
}
|
||||
}
|
||||
|
||||
for (UnwindRecord<E> &rec : subsec.get_unwind_records()) {
|
||||
rec.is_alive = true;
|
||||
visit(ctx, *rec.subsec);
|
||||
if (rec.lsda)
|
||||
visit(ctx, *rec.lsda);
|
||||
if (Symbol<E> *sym = rec.personality; sym && sym->subsec)
|
||||
visit(ctx, *sym->subsec);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static bool refers_live_subsection(Subsection<E> &subsec) {
|
||||
for (Relocation<E> &rel : subsec.get_rels()) {
|
||||
if (rel.sym) {
|
||||
if (!rel.sym->subsec || rel.sym->subsec->is_alive)
|
||||
return true;
|
||||
} else {
|
||||
if (rel.subsec->is_alive)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void mark(Context<E> &ctx, const std::vector<Subsection<E> *> &rootset) {
|
||||
for (Subsection<E> *subsec : rootset)
|
||||
visit(ctx, *subsec);
|
||||
|
||||
bool repeat;
|
||||
do {
|
||||
repeat = false;
|
||||
for (ObjectFile<E> *file : ctx.objs) {
|
||||
for (std::unique_ptr<Subsection<E>> &subsec : file->subsections) {
|
||||
if ((subsec->isec.hdr.attr & S_ATTR_LIVE_SUPPORT) &&
|
||||
!subsec->is_alive &&
|
||||
refers_live_subsection(*subsec)) {
|
||||
visit(ctx, *subsec);
|
||||
repeat = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (repeat);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void sweep(Context<E> &ctx) {
|
||||
for (ObjectFile<E> *file : ctx.objs) {
|
||||
std::erase_if(file->subsections,
|
||||
[](const std::unique_ptr<Subsection<E>> &subsec) {
|
||||
return !subsec->is_alive;
|
||||
});
|
||||
}
|
||||
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
for (Symbol<E> *&sym : file->syms)
|
||||
if (sym->file == file && sym->subsec && !sym->subsec->is_alive)
|
||||
sym = nullptr;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void dead_strip(Context<E> &ctx) {
|
||||
std::vector<Subsection<E> *> rootset = collect_root_set(ctx);
|
||||
mark(ctx, rootset);
|
||||
sweep(ctx);
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template void dead_strip(Context<E> &)
|
||||
|
||||
INSTANTIATE(ARM64);
|
||||
INSTANTIATE(X86_64);
|
||||
|
||||
} // namespace mold::macho
|
||||
+511
@@ -0,0 +1,511 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <fcntl.h>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
static u8 *open_file(std::string path) {
|
||||
i64 fd = ::open(path.c_str(), O_RDONLY);
|
||||
if (fd == -1)
|
||||
return nullptr;
|
||||
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) == -1) {
|
||||
std::cerr << path << ": fstat failed: " << errno_string();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void *ptr = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (ptr == MAP_FAILED) {
|
||||
std::cerr << path << ": mmap failed: " << errno_string();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return (u8 *)ptr;
|
||||
}
|
||||
|
||||
static void print_bytes(u8 *buf, i64 size) {
|
||||
if (size == 0) {
|
||||
std::cout << "[]\n";
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "[" << std::setw(2) << std::setfill('0') << (u32)buf[0];
|
||||
for (i64 i = 1; i < size; i++)
|
||||
std::cout << " " << std::setw(2) << std::setfill('0') << (u32)buf[i];
|
||||
std::cout << "]\n";
|
||||
}
|
||||
|
||||
struct ExportEntry {
|
||||
std::string name;
|
||||
u32 flags;
|
||||
u64 addr;
|
||||
};
|
||||
|
||||
static void read_trie(std::vector<ExportEntry> &vec, u8 *start, i64 offset = 0,
|
||||
const std::string &prefix = "") {
|
||||
u8 *buf = start + offset;
|
||||
|
||||
if (read_uleb(buf)) {
|
||||
ExportEntry ent;
|
||||
ent.name = prefix;
|
||||
ent.flags = read_uleb(buf);
|
||||
ent.addr = read_uleb(buf);
|
||||
vec.push_back(std::move(ent));
|
||||
}
|
||||
|
||||
for (i64 i = 0, n = read_uleb(buf); i < n; i++) {
|
||||
std::string suffix((char *)buf);
|
||||
buf += suffix.size() + 1;
|
||||
i64 off = read_uleb(buf);
|
||||
read_trie(vec, start, off, prefix + suffix);
|
||||
}
|
||||
}
|
||||
|
||||
void dump_unwind_info(u8 *buf, MachSection &sec) {
|
||||
UnwindSectionHeader &hdr = *(UnwindSectionHeader *)(buf + sec.offset);
|
||||
|
||||
std::cout << std::hex << " Unwind info:"
|
||||
<< "\n version: 0x" << hdr.version
|
||||
<< "\n encoding_offset: 0x" << hdr.encoding_offset
|
||||
<< "\n encoding_count: 0x" << hdr.encoding_count
|
||||
<< "\n personality_offset: 0x" << hdr.personality_offset
|
||||
<< "\n personality_count: 0x" << hdr.personality_count
|
||||
<< "\n page_offset: 0x" << hdr.page_offset
|
||||
<< "\n page_count: 0x" << hdr.page_count;
|
||||
|
||||
std::cout << "\n encoding:";
|
||||
u32 *enc = (u32 *)(buf + sec.offset + hdr.encoding_offset);
|
||||
for (i64 i = 0; i < hdr.encoding_count; i++)
|
||||
std::cout << std::hex << "\n 0x" << enc[i];
|
||||
|
||||
std::cout << "\n personality:";
|
||||
u32 *pers = (u32 *)(buf + sec.offset + hdr.personality_offset);
|
||||
for (i64 i = 0; i < hdr.personality_count; i++)
|
||||
std::cout << std::hex << "\n 0x" << pers[i];
|
||||
|
||||
UnwindFirstLevelPage *ent =
|
||||
(UnwindFirstLevelPage *)(buf + sec.offset + hdr.page_offset);
|
||||
|
||||
for (i64 i = 0; i < hdr.page_count; i++) {
|
||||
std::cout << std::hex << "\n function:"
|
||||
<< "\n func_addr: 0x" << ent[i].func_addr
|
||||
<< "\n page_offset: 0x" << ent[i].page_offset
|
||||
<< "\n lsda_offset: 0x" << ent[i].lsda_offset;
|
||||
|
||||
if (i != hdr.page_count - 1) {
|
||||
UnwindLsdaEntry *lsda =
|
||||
(UnwindLsdaEntry *)(buf + sec.offset + ent[i].lsda_offset);
|
||||
i64 lsda_size = ent[i + 1].lsda_offset - ent[i].lsda_offset;
|
||||
for (i64 j = 0; j < lsda_size / sizeof(UnwindLsdaEntry); j++)
|
||||
std::cout << std::hex
|
||||
<< "\n lsda:"
|
||||
<< "\n func_addr: 0x" << lsda[j].func_addr
|
||||
<< "\n lsda_addr: 0x" << lsda[j].lsda_addr;
|
||||
}
|
||||
|
||||
if (ent[i].page_offset == 0)
|
||||
break;
|
||||
|
||||
u8 *addr = buf + sec.offset + ent[i].page_offset;
|
||||
|
||||
switch (u32 kind = *addr; kind) {
|
||||
case UNWIND_SECOND_LEVEL_REGULAR: {
|
||||
std::cout << "\n UNWIND_SECOND_LEVEL_REGULAR:" ;
|
||||
break;
|
||||
}
|
||||
case UNWIND_SECOND_LEVEL_COMPRESSED: {
|
||||
std::cout << "\n UNWIND_SECOND_LEVEL_COMPRESSED" ;
|
||||
UnwindSecondLevelPage &hdr2 = *(UnwindSecondLevelPage *)addr;
|
||||
std::cout << std::hex
|
||||
<< "\n page_offset: 0x" << hdr2.page_offset
|
||||
<< "\n page_count: 0x" << hdr2.page_count
|
||||
<< "\n encoding_offset: 0x" << hdr2.encoding_offset
|
||||
<< "\n encoding_count: 0x" << hdr2.encoding_count;
|
||||
|
||||
UnwindPageEntry *ent2 = (UnwindPageEntry *)(addr + hdr2.page_offset);
|
||||
for (i64 j = 0; j < hdr2.page_count; j++)
|
||||
std::cout << std::hex << "\n ent 0x"
|
||||
<< (ent[i].func_addr + ent2[j].func_addr)
|
||||
<< " 0x" << ent2[j].encoding;
|
||||
|
||||
u32 *enc = (u32 *)(addr + hdr2.encoding_offset);
|
||||
for (i64 j = 0; j < hdr2.encoding_count; j++)
|
||||
std::cout << std::hex << "\n 0x" << enc[j];
|
||||
break;
|
||||
}
|
||||
default:
|
||||
std::cout << "\n bad 2nd-level unwind info header: " << kind;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
void dump_compact_unwind(u8 *buf, MachSection &sec) {
|
||||
CompactUnwindEntry *ent = (CompactUnwindEntry *)(buf + sec.offset);
|
||||
i64 nentry = sec.size / sizeof(CompactUnwindEntry);
|
||||
|
||||
std::cout << " Compact unwind:"
|
||||
<< "\n num_entry: " << nentry;
|
||||
|
||||
for (i64 i = 0; i < nentry; i++) {
|
||||
std::cout << std::hex
|
||||
<< "\n entry: 0x" << (i * sizeof(CompactUnwindEntry))
|
||||
<< "\n code_start: 0x" << ent[i].code_start
|
||||
<< "\n code_len: 0x" << ent[i].code_len
|
||||
<< "\n encoding: 0x" << ent[i].encoding
|
||||
<< "\n personality: 0x" << ent[i].personality
|
||||
<< "\n lsda: 0x" << ent[i].lsda;
|
||||
}
|
||||
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
void dump_file(std::string path) {
|
||||
u8 *buf = open_file(path);
|
||||
if (!buf) {
|
||||
std::cerr << "cannot open " << path << "\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
MachHeader &hdr = *(MachHeader *)buf;
|
||||
std::cout << "magic: 0x" << std::hex << hdr.magic
|
||||
<< "\ncputype: 0x" << hdr.cputype
|
||||
<< "\ncpusubtype: 0x" << hdr.cpusubtype
|
||||
<< "\nfiletype: 0x" << hdr.filetype
|
||||
<< "\nncmds: 0x" << hdr.ncmds
|
||||
<< "\nsizeofcmds: 0x" << hdr.sizeofcmds
|
||||
<< "\nflags: 0x" << hdr.flags
|
||||
<< "\n\n";
|
||||
|
||||
u8 *p = buf + sizeof(MachHeader);
|
||||
|
||||
for (i64 i = 0; i < hdr.ncmds; i++) {
|
||||
std::cout << "fileoff: 0x" << std::hex << (p - buf) << "\n";
|
||||
|
||||
LoadCommand &lc = *(LoadCommand *)p;
|
||||
p += lc.cmdsize;
|
||||
|
||||
auto print_dylib = [&]() {
|
||||
DylibCommand &cmd = *(DylibCommand *)&lc;
|
||||
std::cout << " cmdsize: 0x" << cmd.cmdsize
|
||||
<< "\n nameoff: 0x" << cmd.nameoff
|
||||
<< "\n timestamp: 0x" << cmd.timestamp
|
||||
<< "\n current_version: 0x" << cmd.current_version
|
||||
<< "\n compatibility_version: 0x" << cmd.compatibility_version
|
||||
<< "\n data: " << (char *)((char *)&cmd + cmd.nameoff)
|
||||
<< "\n";
|
||||
};
|
||||
|
||||
switch (lc.cmd) {
|
||||
case LC_SYMTAB: {
|
||||
std::cout << "LC_SYMTAB\n";
|
||||
SymtabCommand &cmd = *(SymtabCommand *)&lc;
|
||||
std::cout << " cmdsize: " << cmd.cmdsize
|
||||
<< "\n symoff: 0x" << std::hex << cmd.symoff
|
||||
<< "\n nsyms: " << std::dec << cmd.nsyms
|
||||
<< "\n stroff: 0x" << std::hex << cmd.stroff
|
||||
<< "\n strsize: 0x" << cmd.strsize
|
||||
<< "\n symdata: ";
|
||||
print_bytes(buf + cmd.symoff, cmd.nsyms * sizeof(MachSym));
|
||||
|
||||
std::cout << " strdata: ";
|
||||
print_bytes(buf + cmd.stroff, cmd.strsize);
|
||||
|
||||
MachSym *syms = (MachSym *)(buf + cmd.symoff);
|
||||
for (i64 j = 0; j < cmd.nsyms; j++) {
|
||||
std::cout << " symbol:"
|
||||
<< "\n name: " << (char *)(buf + cmd.stroff + syms[j].stroff)
|
||||
<< "\n stub: " << (u32)syms[j].stub
|
||||
<< "\n pext: " << (u32)syms[j].pext
|
||||
<< "\n type: " << (u32)syms[j].type
|
||||
<< "\n ext: " << (u32)syms[j].ext
|
||||
<< "\n sect: 0x" << (u32)syms[j].sect
|
||||
<< "\n desc: 0x" << (u32)syms[j].desc
|
||||
<< "\n value: 0x" << syms[j].value
|
||||
<< "\n";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LC_DYSYMTAB: {
|
||||
std::cout << "LC_DYSYMTAB\n";
|
||||
DysymtabCommand &cmd = *(DysymtabCommand *)&lc;
|
||||
std::cout << " cmdsize: 0x" << cmd.cmdsize
|
||||
<< "\n ilocalsym: 0x" << cmd.ilocalsym
|
||||
<< "\n nlocalsym: 0x" << cmd.nlocalsym
|
||||
<< "\n iextdefsym: 0x" << cmd.iextdefsym
|
||||
<< "\n nextdefsym: 0x" << cmd.nextdefsym
|
||||
<< "\n iundefsym: 0x" << cmd.iundefsym
|
||||
<< "\n nundefsym: 0x" << cmd.nundefsym
|
||||
<< "\n tocoff: 0x" << cmd.tocoff
|
||||
<< "\n ntoc: 0x" << cmd.ntoc
|
||||
<< "\n modtaboff: 0x" << cmd.modtaboff
|
||||
<< "\n nmodtab: 0x" << cmd.nmodtab
|
||||
<< "\n extrefsymoff: 0x" << cmd.extrefsymoff
|
||||
<< "\n nextrefsyms: 0x" << cmd.nextrefsyms
|
||||
<< "\n indirectsymoff: 0x" << cmd.indirectsymoff
|
||||
<< "\n nindirectsyms: 0x" << cmd.nindirectsyms
|
||||
<< "\n extreloff: 0x" << cmd.extreloff
|
||||
<< "\n nextrel: 0x" << cmd.nextrel
|
||||
<< "\n locreloff: 0x" << cmd.locreloff
|
||||
<< "\n nlocrel: 0x" << cmd.nlocrel
|
||||
<< "\n";
|
||||
|
||||
if (cmd.indirectsymoff) {
|
||||
std::cout << " indirectsymdata: ";
|
||||
print_bytes(buf + cmd.indirectsymoff, 4 * cmd.nindirectsyms);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LC_LOAD_DYLIB:
|
||||
std::cout << "LC_LOAD_DYLIB\n";
|
||||
print_dylib();
|
||||
break;
|
||||
case LC_LOAD_WEAK_DYLIB:
|
||||
std::cout << "LC_LOAD_WEAK_DYLIB\n";
|
||||
print_dylib();
|
||||
break;
|
||||
case LC_ID_DYLIB:
|
||||
std::cout << "LC_ID_DYLIB\n";
|
||||
print_dylib();
|
||||
break;
|
||||
case LC_LOAD_DYLINKER: {
|
||||
std::cout << "LC_LOAD_DYLINKER\n";
|
||||
DylinkerCommand &cmd = *(DylinkerCommand *)&lc;
|
||||
std::cout << " cmdsize: 0x" << cmd.cmdsize
|
||||
<< "\n nameoff: 0x" << cmd.nameoff
|
||||
<< "\n data: " << (char *)((char *)&cmd + cmd.nameoff)
|
||||
<< "\n";
|
||||
break;
|
||||
}
|
||||
case LC_SEGMENT_64: {
|
||||
std::cout << "LC_SEGMENT_64\n";
|
||||
SegmentCommand &cmd = *(SegmentCommand *)&lc;
|
||||
std::cout << " cmdsize: " << cmd.cmdsize
|
||||
<< "\n segname: " << cmd.get_segname()
|
||||
<< "\n vmaddr: 0x" << std::hex << cmd.vmaddr
|
||||
<< "\n vmsize: 0x" << cmd.vmsize
|
||||
<< "\n fileoff: 0x" << cmd.fileoff
|
||||
<< "\n filesize: 0x" << cmd.filesize
|
||||
<< "\n maxprot: " << std::dec << cmd.maxprot
|
||||
<< "\n initprot: " << cmd.initprot
|
||||
<< "\n nsects: " << cmd.nsects
|
||||
<< "\n flags: 0x" << std::hex << cmd.flags
|
||||
<< "\n";
|
||||
|
||||
MachSection *sec = (MachSection *)((u8 *)&lc + sizeof(cmd));
|
||||
for (i64 j = 0; j < cmd.nsects; j++) {
|
||||
std::cout << " section:\n sectname: " << sec[j].get_sectname()
|
||||
<< "\n segname: " << sec[j].get_segname()
|
||||
<< "\n addr: 0x" << std::hex << sec[j].addr
|
||||
<< "\n size: 0x" << sec[j].size
|
||||
<< "\n offset: 0x" << sec[j].offset
|
||||
<< "\n p2align: " << std::dec << sec[j].p2align
|
||||
<< "\n reloff: " << std::hex << sec[j].reloff
|
||||
<< "\n nreloc: " << std::dec << sec[j].nreloc
|
||||
<< "\n type: 0x" << std::hex << sec[j].type
|
||||
<< "\n attr: 0x" << std::hex << sec[j].attr
|
||||
<< "\n";
|
||||
|
||||
if (sec[j].type != S_ZEROFILL) {
|
||||
std::cout << " contents: ";
|
||||
print_bytes(buf + sec[j].offset, sec[j].size);
|
||||
}
|
||||
|
||||
if (sec[j].reloff) {
|
||||
MachRel *rel = (MachRel *)(buf + sec[j].reloff);
|
||||
for (i64 k = 0; k < sec[j].nreloc; k++) {
|
||||
std::cout << " reloc: "
|
||||
<< "\n offset: 0x" << rel[k].offset
|
||||
<< "\n idx: 0x" << rel[k].idx
|
||||
<< "\n is_pcrel: " << rel[k].is_pcrel
|
||||
<< "\n p2size: 0x" << rel[k].p2size
|
||||
<< "\n is_extern: " << rel[k].is_extern
|
||||
<< "\n type: " << rel[k].type
|
||||
<< "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (sec[j].match("__TEXT", "__unwind_info"))
|
||||
dump_unwind_info(buf, sec[j]);
|
||||
|
||||
if (sec[j].match("__LD", "__compact_unwind"))
|
||||
dump_compact_unwind(buf, sec[j]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LC_UUID: {
|
||||
std::cout << "LC_UUID\n";
|
||||
UUIDCommand &cmd = *(UUIDCommand *)&lc;
|
||||
std::cout << " data: ";
|
||||
print_bytes(cmd.uuid, sizeof(cmd.uuid));
|
||||
break;
|
||||
}
|
||||
case LC_DYLD_INFO_ONLY: {
|
||||
std::cout << "LC_DYLD_INFO_ONLY\n";
|
||||
DyldInfoCommand &cmd = *(DyldInfoCommand *)&lc;
|
||||
|
||||
if (cmd.rebase_off) {
|
||||
std::cout << " rebase: ";
|
||||
print_bytes(buf + cmd.rebase_off, cmd.rebase_size);
|
||||
std::cout << " rebase_off: 0x" << std::hex << cmd.rebase_off
|
||||
<< "\n rebase_size: 0x" << cmd.rebase_size
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
if (cmd.bind_off) {
|
||||
std::cout << " bind: ";
|
||||
print_bytes(buf + cmd.bind_off, cmd.bind_size);
|
||||
std::cout << " bind_off: 0x" << std::hex << cmd.bind_off
|
||||
<< "\n bind_size: 0x" << cmd.bind_size
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
if (cmd.weak_bind_off) {
|
||||
std::cout << " weak_bind: ";
|
||||
print_bytes(buf + cmd.weak_bind_off, cmd.weak_bind_size);
|
||||
std::cout << " weak_bind_off: 0x" << std::hex << cmd.weak_bind_off
|
||||
<< "\n weak_bind_size: 0x" << cmd.weak_bind_size
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
if (cmd.lazy_bind_off) {
|
||||
std::cout << " lazy_bind: ";
|
||||
print_bytes(buf + cmd.lazy_bind_off, cmd.lazy_bind_size);
|
||||
std::cout << " lazy_bind_off: 0x" << std::hex << cmd.lazy_bind_off
|
||||
<< "\n lazy_bind_size: 0x" << cmd.lazy_bind_size
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
if (cmd.export_off) {
|
||||
std::cout << " export: ";
|
||||
print_bytes(buf + cmd.export_off, cmd.export_size);
|
||||
std::cout << " export_off: 0x" << std::hex << cmd.export_off
|
||||
<< "\n export_size: 0x" << cmd.export_size
|
||||
<< "\n";
|
||||
|
||||
std::vector<ExportEntry> vec;
|
||||
read_trie(vec, buf + cmd.export_off);
|
||||
for (ExportEntry &ent : vec)
|
||||
std::cout << " export_sym: " << ent.name << " 0x" << ent.addr << "\n";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LC_FUNCTION_STARTS: {
|
||||
std::cout << "LC_FUNCTION_STARTS\n";
|
||||
LinkEditDataCommand &cmd = *(LinkEditDataCommand *)&lc;
|
||||
std::cout << " dataoff: 0x" << cmd.dataoff
|
||||
<< "\n datasize: 0x" << cmd.datasize
|
||||
<< "\n data:";
|
||||
|
||||
u8 *p = buf + cmd.dataoff;
|
||||
u64 addr = 0;
|
||||
for (;;) {
|
||||
u64 delta = read_uleb(p);
|
||||
if (!delta)
|
||||
break;
|
||||
addr += delta;
|
||||
std::cout << std::hex << " 0x" << addr;
|
||||
}
|
||||
std::cout << "\n";
|
||||
break;
|
||||
}
|
||||
case LC_MAIN: {
|
||||
std::cout << "LC_MAIN\n";
|
||||
EntryPointCommand &cmd = *(EntryPointCommand *)&lc;
|
||||
std::cout << " cmdsize: 0x" << cmd.cmdsize
|
||||
<< "\n entryoff: 0x" << cmd.entryoff
|
||||
<< "\n stacksize: 0x" << cmd.stacksize
|
||||
<< "\n";
|
||||
break;
|
||||
}
|
||||
case LC_DATA_IN_CODE: {
|
||||
std::cout << "LC_DATA_IN_CODE\n";
|
||||
LinkEditDataCommand &cmd = *(LinkEditDataCommand *)&lc;
|
||||
std::cout << " dataoff: 0x" << cmd.dataoff
|
||||
<< "\n datasize: 0x" << cmd.datasize
|
||||
<< "\n";
|
||||
break;
|
||||
}
|
||||
case LC_SOURCE_VERSION: {
|
||||
std::cout << "LC_SOURCE_VERSION\n";
|
||||
SourceVersionCommand &cmd = *(SourceVersionCommand *)&lc;
|
||||
std::cout << " version: 0x" << cmd.version
|
||||
<< "\n";
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case LC_BUILD_VERSION: {
|
||||
std::cout << "LC_BUILD_VERSION\n";
|
||||
BuildVersionCommand &cmd = *(BuildVersionCommand *)&lc;
|
||||
std::cout << " cmdsize: 0x" << cmd.cmdsize
|
||||
<< "\n platform: 0x" << cmd.platform
|
||||
<< "\n minos: 0x" << cmd.minos
|
||||
<< "\n sdk: 0x" << cmd.sdk
|
||||
<< "\n ntools: 0x" << cmd.ntools
|
||||
<< "\n";
|
||||
|
||||
BuildToolVersion *tools =
|
||||
(BuildToolVersion *)((u8 *)&lc + sizeof(BuildVersionCommand));
|
||||
for (i64 i = 0; i < cmd.ntools; i++)
|
||||
std::cout << " tool: 0x" << tools[i].tool
|
||||
<< "\n version: 0x" << tools[i].version
|
||||
<< "\n";
|
||||
break;
|
||||
}
|
||||
case LC_VERSION_MIN_MACOSX: {
|
||||
std::cout << "LC_VERSION_MIN_MACOSX\n";
|
||||
VersionMinCommand &cmd = *(VersionMinCommand *)&lc;
|
||||
std::cout << " version: " << (int)cmd.version
|
||||
<< "\n sdk: " << (int)cmd.sdk
|
||||
<< "\n";
|
||||
break;
|
||||
}
|
||||
case LC_CODE_SIGNATURE: {
|
||||
std::cout << "LC_CODE_SIGNATURE\n";
|
||||
LinkEditDataCommand &cmd = *(LinkEditDataCommand *)&lc;
|
||||
|
||||
CodeSignatureHeader &sig = *(CodeSignatureHeader *)(buf + cmd.dataoff);
|
||||
std::cout << " magic: " << sig.magic
|
||||
<< "\n length: " << sig.length
|
||||
<< "\n count: " << sig.count
|
||||
<< "\n";
|
||||
|
||||
for (i64 i = 0; i < sig.count; i++) {
|
||||
CodeSignatureBlobIndex &idx = ((CodeSignatureBlobIndex *)(&sig + 1))[i];
|
||||
std::cout << " idx type: " << idx.type
|
||||
<< "\n idx offset: " << idx.offset
|
||||
<< "\n";
|
||||
|
||||
CodeSignatureDirectory &dir =
|
||||
*(CodeSignatureDirectory *)((u8 *)&sig + idx.offset);
|
||||
std::cout << std::hex
|
||||
<< " magic: 0x" << dir.magic
|
||||
<< "\n version: 0x" << dir.version
|
||||
<< "\n flags: 0x" << dir.flags
|
||||
<< "\n hash_offset: 0x" << dir.hash_offset
|
||||
<< "\n n_code_slots: 0x" << dir.n_code_slots
|
||||
<< "\n hash_size: 0x" << (u32)dir.hash_size
|
||||
<< "\n hash_type: 0x" << (u32)dir.hash_type
|
||||
<< "\n page_size: 0x" << (1 << dir.page_size)
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
std::cout << "UNKNOWN (0x" << std::hex << lc.cmd << ")\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mold::macho
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "mold.h"
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
template <typename E>
|
||||
InputSection<E>::InputSection(Context<E> &ctx, ObjectFile<E> &file,
|
||||
const MachSection &hdr)
|
||||
: file(file), hdr(hdr),
|
||||
osec(*OutputSection<E>::get_instance(ctx, hdr.get_segname(),
|
||||
hdr.get_sectname())) {
|
||||
if (hdr.type != S_ZEROFILL)
|
||||
contents = file.mf->get_contents().substr(hdr.offset, hdr.size);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void InputSection<E>::parse_relocations(Context<E> &ctx) {
|
||||
rels.reserve(hdr.nreloc);
|
||||
|
||||
// Parse mach-o relocations to fill `rels` vector
|
||||
rels = read_relocations(ctx, file, hdr);
|
||||
|
||||
// Sort `rels` vector
|
||||
sort(rels, [](const Relocation<E> &a, const Relocation<E> &b) {
|
||||
return a.offset < b.offset;
|
||||
});
|
||||
|
||||
// Find subsections for this section
|
||||
auto begin = std::lower_bound(
|
||||
file.subsections.begin(), file.subsections.end(), hdr.addr,
|
||||
[](std::unique_ptr<Subsection<E>> &subsec, u32 addr) {
|
||||
return subsec->input_addr < addr;
|
||||
});
|
||||
|
||||
auto end = std::lower_bound(
|
||||
begin, file.subsections.end(), hdr.addr + hdr.size,
|
||||
[](std::unique_ptr<Subsection<E>> &subsec, u32 addr) {
|
||||
return subsec->input_addr < addr;
|
||||
});
|
||||
|
||||
// Assign each subsection a group of relocations
|
||||
i64 i = 0;
|
||||
for (auto it = begin; it < end; it++) {
|
||||
Subsection<E> &subsec = **it;
|
||||
subsec.rel_offset = i;
|
||||
while (i < rels.size() &&
|
||||
rels[i].offset < subsec.input_offset + subsec.input_size) {
|
||||
rels[i].offset -= subsec.input_offset;
|
||||
i++;
|
||||
}
|
||||
subsec.nrels = i - subsec.rel_offset;
|
||||
}
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template class InputSection<E>
|
||||
|
||||
INSTANTIATE(ARM64);
|
||||
INSTANTIATE(X86_64);
|
||||
|
||||
} // namespace mold::macho
|
||||
+693
@@ -0,0 +1,693 @@
|
||||
#pragma once
|
||||
|
||||
#include "../byteorder.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string_view>
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
typedef uint8_t u8;
|
||||
typedef uint16_t u16;
|
||||
typedef uint32_t u32;
|
||||
typedef uint64_t u64;
|
||||
|
||||
typedef int8_t i8;
|
||||
typedef int16_t i16;
|
||||
typedef int32_t i32;
|
||||
typedef int64_t i64;
|
||||
|
||||
static constexpr u32 FAT_MAGIC = 0xcafebabe;
|
||||
|
||||
static constexpr u32 MH_OBJECT = 0x1;
|
||||
static constexpr u32 MH_EXECUTE = 0x2;
|
||||
static constexpr u32 MH_FVMLIB = 0x3;
|
||||
static constexpr u32 MH_CORE = 0x4;
|
||||
static constexpr u32 MH_PRELOAD = 0x5;
|
||||
static constexpr u32 MH_DYLIB = 0x6;
|
||||
static constexpr u32 MH_DYLINKER = 0x7;
|
||||
static constexpr u32 MH_BUNDLE = 0x8;
|
||||
static constexpr u32 MH_DYLIB_STUB = 0x9;
|
||||
static constexpr u32 MH_DSYM = 0xa;
|
||||
static constexpr u32 MH_KEXT_BUNDLE = 0xb;
|
||||
|
||||
static constexpr u32 MH_NOUNDEFS = 0x1;
|
||||
static constexpr u32 MH_INCRLINK = 0x2;
|
||||
static constexpr u32 MH_DYLDLINK = 0x4;
|
||||
static constexpr u32 MH_BINDATLOAD = 0x8;
|
||||
static constexpr u32 MH_PREBOUND = 0x10;
|
||||
static constexpr u32 MH_SPLIT_SEGS = 0x20;
|
||||
static constexpr u32 MH_LAZY_INIT = 0x40;
|
||||
static constexpr u32 MH_TWOLEVEL = 0x80;
|
||||
static constexpr u32 MH_FORCE_FLAT = 0x100;
|
||||
static constexpr u32 MH_NOMULTIDEFS = 0x200;
|
||||
static constexpr u32 MH_NOFIXPREBINDING = 0x400;
|
||||
static constexpr u32 MH_PREBINDABLE = 0x800;
|
||||
static constexpr u32 MH_ALLMODSBOUND = 0x1000;
|
||||
static constexpr u32 MH_SUBSECTIONS_VIA_SYMBOLS = 0x2000;
|
||||
static constexpr u32 MH_CANONICAL = 0x4000;
|
||||
static constexpr u32 MH_WEAK_DEFINES = 0x8000;
|
||||
static constexpr u32 MH_BINDS_TO_WEAK = 0x10000;
|
||||
static constexpr u32 MH_ALLOW_STACK_EXECUTION = 0x20000;
|
||||
static constexpr u32 MH_ROOT_SAFE = 0x40000;
|
||||
static constexpr u32 MH_SETUID_SAFE = 0x80000;
|
||||
static constexpr u32 MH_NO_REEXPORTED_DYLIBS = 0x100000;
|
||||
static constexpr u32 MH_PIE = 0x200000;
|
||||
static constexpr u32 MH_DEAD_STRIPPABLE_DYLIB = 0x400000;
|
||||
static constexpr u32 MH_HAS_TLV_DESCRIPTORS = 0x800000;
|
||||
static constexpr u32 MH_NO_HEAP_EXECUTION = 0x1000000;
|
||||
static constexpr u32 MH_APP_EXTENSION_SAFE = 0x02000000;
|
||||
static constexpr u32 MH_NLIST_OUTOFSYNC_WITH_DYLDINFO = 0x04000000;
|
||||
static constexpr u32 MH_SIM_SUPPORT = 0x08000000;
|
||||
|
||||
static constexpr u32 VM_PROT_READ = 0x1;
|
||||
static constexpr u32 VM_PROT_WRITE = 0x2;
|
||||
static constexpr u32 VM_PROT_EXECUTE = 0x4;
|
||||
static constexpr u32 VM_PROT_NO_CHANGE = 0x8;
|
||||
static constexpr u32 VM_PROT_COPY = 0x10;
|
||||
static constexpr u32 VM_PROT_WANTS_COPY = 0x10;
|
||||
|
||||
static constexpr u32 LC_REQ_DYLD = 0x80000000;
|
||||
|
||||
static constexpr u32 LC_SEGMENT = 0x1;
|
||||
static constexpr u32 LC_SYMTAB = 0x2;
|
||||
static constexpr u32 LC_SYMSEG = 0x3;
|
||||
static constexpr u32 LC_THREAD = 0x4;
|
||||
static constexpr u32 LC_UNIXTHREAD = 0x5;
|
||||
static constexpr u32 LC_LOADFVMLIB = 0x6;
|
||||
static constexpr u32 LC_IDFVMLIB = 0x7;
|
||||
static constexpr u32 LC_IDENT = 0x8;
|
||||
static constexpr u32 LC_FVMFILE = 0x9;
|
||||
static constexpr u32 LC_PREPAGE = 0xa;
|
||||
static constexpr u32 LC_DYSYMTAB = 0xb;
|
||||
static constexpr u32 LC_LOAD_DYLIB = 0xc;
|
||||
static constexpr u32 LC_ID_DYLIB = 0xd;
|
||||
static constexpr u32 LC_LOAD_DYLINKER = 0xe;
|
||||
static constexpr u32 LC_ID_DYLINKER = 0xf;
|
||||
static constexpr u32 LC_PREBOUND_DYLIB = 0x10;
|
||||
static constexpr u32 LC_ROUTINES = 0x11;
|
||||
static constexpr u32 LC_SUB_FRAMEWORK = 0x12;
|
||||
static constexpr u32 LC_SUB_UMBRELLA = 0x13;
|
||||
static constexpr u32 LC_SUB_CLIENT = 0x14;
|
||||
static constexpr u32 LC_SUB_LIBRARY = 0x15;
|
||||
static constexpr u32 LC_TWOLEVEL_HINTS = 0x16;
|
||||
static constexpr u32 LC_PREBIND_CKSUM = 0x17;
|
||||
static constexpr u32 LC_LOAD_WEAK_DYLIB = (0x18 | LC_REQ_DYLD);
|
||||
static constexpr u32 LC_SEGMENT_64 = 0x19;
|
||||
static constexpr u32 LC_ROUTINES_64 = 0x1a;
|
||||
static constexpr u32 LC_UUID = 0x1b;
|
||||
static constexpr u32 LC_RPATH = (0x1c | LC_REQ_DYLD);
|
||||
static constexpr u32 LC_CODE_SIGNATURE = 0x1d;
|
||||
static constexpr u32 LC_SEGMENT_SPLIT_INFO = 0x1e;
|
||||
static constexpr u32 LC_REEXPORT_DYLIB = (0x1f | LC_REQ_DYLD);
|
||||
static constexpr u32 LC_LAZY_LOAD_DYLIB = 0x20;
|
||||
static constexpr u32 LC_ENCRYPTION_INFO = 0x21;
|
||||
static constexpr u32 LC_DYLD_INFO = 0x22;
|
||||
static constexpr u32 LC_DYLD_INFO_ONLY = (0x22 | LC_REQ_DYLD);
|
||||
static constexpr u32 LC_LOAD_UPWARD_DYLIB = (0x23 | LC_REQ_DYLD);
|
||||
static constexpr u32 LC_VERSION_MIN_MACOSX = 0x24;
|
||||
static constexpr u32 LC_VERSION_MIN_IPHONEOS = 0x25;
|
||||
static constexpr u32 LC_FUNCTION_STARTS = 0x26;
|
||||
static constexpr u32 LC_DYLD_ENVIRONMENT = 0x27;
|
||||
static constexpr u32 LC_MAIN = (0x28 | LC_REQ_DYLD);
|
||||
static constexpr u32 LC_DATA_IN_CODE = 0x29;
|
||||
static constexpr u32 LC_SOURCE_VERSION = 0x2A;
|
||||
static constexpr u32 LC_DYLIB_CODE_SIGN_DRS = 0x2B;
|
||||
static constexpr u32 LC_ENCRYPTION_INFO_64 = 0x2C;
|
||||
static constexpr u32 LC_LINKER_OPTION = 0x2D;
|
||||
static constexpr u32 LC_LINKER_OPTIMIZATION_HINT = 0x2E;
|
||||
static constexpr u32 LC_VERSION_MIN_TVOS = 0x2F;
|
||||
static constexpr u32 LC_VERSION_MIN_WATCHOS = 0x30;
|
||||
static constexpr u32 LC_NOTE = 0x31;
|
||||
static constexpr u32 LC_BUILD_VERSION = 0x32;
|
||||
static constexpr u32 LC_DYLD_EXPORTS_TRIE = (0x33 | LC_REQ_DYLD);
|
||||
static constexpr u32 LC_DYLD_CHAINED_FIXUPS = (0x34 | LC_REQ_DYLD);
|
||||
|
||||
static constexpr u32 SG_HIGHVM = 0x1;
|
||||
static constexpr u32 SG_FVMLIB = 0x2;
|
||||
static constexpr u32 SG_NORELOC = 0x4;
|
||||
static constexpr u32 SG_PROTECTED_VERSION_1 = 0x8;
|
||||
static constexpr u32 SG_READ_ONLY = 0x10;
|
||||
|
||||
static constexpr u32 S_REGULAR = 0x0;
|
||||
static constexpr u32 S_ZEROFILL = 0x1;
|
||||
static constexpr u32 S_CSTRING_LITERALS = 0x2;
|
||||
static constexpr u32 S_4BYTE_LITERALS = 0x3;
|
||||
static constexpr u32 S_8BYTE_LITERALS = 0x4;
|
||||
static constexpr u32 S_LITERAL_POINTERS = 0x5;
|
||||
static constexpr u32 S_NON_LAZY_SYMBOL_POINTERS = 0x6;
|
||||
static constexpr u32 S_LAZY_SYMBOL_POINTERS = 0x7;
|
||||
static constexpr u32 S_SYMBOL_STUBS = 0x8;
|
||||
static constexpr u32 S_MOD_INIT_FUNC_POINTERS = 0x9;
|
||||
static constexpr u32 S_MOD_TERM_FUNC_POINTERS = 0xa;
|
||||
static constexpr u32 S_COALESCED = 0xb;
|
||||
static constexpr u32 S_GB_ZEROFILL = 0xc;
|
||||
static constexpr u32 S_INTERPOSING = 0xd;
|
||||
static constexpr u32 S_16BYTE_LITERALS = 0xe;
|
||||
static constexpr u32 S_DTRACE_DOF = 0xf;
|
||||
static constexpr u32 S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10;
|
||||
static constexpr u32 S_THREAD_LOCAL_REGULAR = 0x11;
|
||||
static constexpr u32 S_THREAD_LOCAL_ZEROFILL = 0x12;
|
||||
static constexpr u32 S_THREAD_LOCAL_VARIABLES = 0x13;
|
||||
static constexpr u32 S_THREAD_LOCAL_VARIABLE_POINTERS = 0x14;
|
||||
static constexpr u32 S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15;
|
||||
static constexpr u32 S_INIT_FUNC_OFFSETS = 0x16;
|
||||
|
||||
static constexpr u32 S_ATTR_LOC_RELOC = 0x000001;
|
||||
static constexpr u32 S_ATTR_EXT_RELOC = 0x000002;
|
||||
static constexpr u32 S_ATTR_SOME_INSTRUCTIONS = 0x000004;
|
||||
|
||||
static constexpr u32 S_ATTR_DEBUG = 0x020000;
|
||||
static constexpr u32 S_ATTR_SELF_MODIFYING_CODE = 0x040000;
|
||||
static constexpr u32 S_ATTR_LIVE_SUPPORT = 0x080000;
|
||||
static constexpr u32 S_ATTR_NO_DEAD_STRIP = 0x100000;
|
||||
static constexpr u32 S_ATTR_STRIP_STATIC_SYMS = 0x200000;
|
||||
static constexpr u32 S_ATTR_NO_TOC = 0x400000;
|
||||
static constexpr u32 S_ATTR_PURE_INSTRUCTIONS = 0x800000;
|
||||
|
||||
static constexpr u32 CPU_TYPE_X86_64 = 0x1000007;
|
||||
static constexpr u32 CPU_TYPE_ARM64 = 0x100000c;
|
||||
|
||||
static constexpr u32 CPU_SUBTYPE_X86_64_ALL = 3;
|
||||
static constexpr u32 CPU_SUBTYPE_ARM64_ALL = 0;
|
||||
|
||||
static constexpr u32 REBASE_TYPE_POINTER = 1;
|
||||
static constexpr u32 REBASE_TYPE_TEXT_ABSOLUTE32 = 2;
|
||||
static constexpr u32 REBASE_TYPE_TEXT_PCREL32 = 3;
|
||||
|
||||
static constexpr u32 REBASE_OPCODE_MASK = 0xf0;
|
||||
static constexpr u32 REBASE_IMMEDIATE_MASK = 0x0f;
|
||||
static constexpr u32 REBASE_OPCODE_DONE = 0x00;
|
||||
static constexpr u32 REBASE_OPCODE_SET_TYPE_IMM = 0x10;
|
||||
static constexpr u32 REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB = 0x20;
|
||||
static constexpr u32 REBASE_OPCODE_ADD_ADDR_ULEB = 0x30;
|
||||
static constexpr u32 REBASE_OPCODE_ADD_ADDR_IMM_SCALED = 0x40;
|
||||
static constexpr u32 REBASE_OPCODE_DO_REBASE_IMM_TIMES = 0x50;
|
||||
static constexpr u32 REBASE_OPCODE_DO_REBASE_ULEB_TIMES = 0x60;
|
||||
static constexpr u32 REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB = 0x70;
|
||||
static constexpr u32 REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80;
|
||||
|
||||
static constexpr u32 BIND_TYPE_POINTER = 1;
|
||||
static constexpr u32 BIND_TYPE_TEXT_ABSOLUTE32 = 2;
|
||||
static constexpr u32 BIND_TYPE_TEXT_PCREL32 = 3;
|
||||
|
||||
static constexpr u32 BIND_SPECIAL_DYLIB_SELF = 0;
|
||||
static constexpr u32 BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE = -1;
|
||||
static constexpr u32 BIND_SPECIAL_DYLIB_FLAT_LOOKUP = -2;
|
||||
static constexpr u32 BIND_SPECIAL_DYLIB_WEAK_LOOKUP = -3;
|
||||
|
||||
static constexpr u32 BIND_SYMBOL_FLAGS_WEAK_IMPORT = 0x1;
|
||||
static constexpr u32 BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION = 0x8;
|
||||
|
||||
static constexpr u32 BIND_OPCODE_MASK = 0xF0;
|
||||
static constexpr u32 BIND_IMMEDIATE_MASK = 0x0F;
|
||||
static constexpr u32 BIND_OPCODE_DONE = 0x00;
|
||||
static constexpr u32 BIND_OPCODE_SET_DYLIB_ORDINAL_IMM = 0x10;
|
||||
static constexpr u32 BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB = 0x20;
|
||||
static constexpr u32 BIND_OPCODE_SET_DYLIB_SPECIAL_IMM = 0x30;
|
||||
static constexpr u32 BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM = 0x40;
|
||||
static constexpr u32 BIND_OPCODE_SET_TYPE_IMM = 0x50;
|
||||
static constexpr u32 BIND_OPCODE_SET_ADDEND_SLEB = 0x60;
|
||||
static constexpr u32 BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB = 0x70;
|
||||
static constexpr u32 BIND_OPCODE_ADD_ADDR_ULEB = 0x80;
|
||||
static constexpr u32 BIND_OPCODE_DO_BIND = 0x90;
|
||||
static constexpr u32 BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB = 0xA0;
|
||||
static constexpr u32 BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED = 0xB0;
|
||||
static constexpr u32 BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0;
|
||||
static constexpr u32 BIND_OPCODE_THREADED = 0xD0;
|
||||
static constexpr u32 BIND_SUBOPCODE_THREADED_SET_BIND_ORDINAL_TABLE_SIZE_ULEB = 0x00;
|
||||
static constexpr u32 BIND_SUBOPCODE_THREADED_APPLY = 0x01;
|
||||
|
||||
static constexpr u32 EXPORT_SYMBOL_FLAGS_KIND_MASK = 0x03;
|
||||
static constexpr u32 EXPORT_SYMBOL_FLAGS_KIND_REGULAR = 0x00;
|
||||
static constexpr u32 EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL = 0x01;
|
||||
static constexpr u32 EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE = 0x02;
|
||||
static constexpr u32 EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION = 0x04;
|
||||
static constexpr u32 EXPORT_SYMBOL_FLAGS_REEXPORT = 0x08;
|
||||
static constexpr u32 EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER = 0x10;
|
||||
|
||||
static constexpr u32 DICE_KIND_DATA = 1;
|
||||
static constexpr u32 DICE_KIND_JUMP_TABLE8 = 2;
|
||||
static constexpr u32 DICE_KIND_JUMP_TABLE16 = 3;
|
||||
static constexpr u32 DICE_KIND_JUMP_TABLE32 = 4;
|
||||
static constexpr u32 DICE_KIND_ABS_JUMP_TABLE32 = 5;
|
||||
|
||||
static constexpr u32 N_UNDF = 0;
|
||||
static constexpr u32 N_ABS = 1;
|
||||
static constexpr u32 N_INDR = 5;
|
||||
static constexpr u32 N_PBUD = 6;
|
||||
static constexpr u32 N_SECT = 7;
|
||||
|
||||
static constexpr u32 N_GSYM = 0x20;
|
||||
static constexpr u32 N_FNAME = 0x22;
|
||||
static constexpr u32 N_FUN = 0x24;
|
||||
static constexpr u32 N_STSYM = 0x26;
|
||||
static constexpr u32 N_LCSYM = 0x28;
|
||||
static constexpr u32 N_BNSYM = 0x2e;
|
||||
static constexpr u32 N_AST = 0x32;
|
||||
static constexpr u32 N_OPT = 0x3c;
|
||||
static constexpr u32 N_RSYM = 0x40;
|
||||
static constexpr u32 N_SLINE = 0x44;
|
||||
static constexpr u32 N_ENSYM = 0x4e;
|
||||
static constexpr u32 N_SSYM = 0x60;
|
||||
static constexpr u32 N_SO = 0x64;
|
||||
static constexpr u32 N_OSO = 0x66;
|
||||
static constexpr u32 N_LSYM = 0x80;
|
||||
static constexpr u32 N_BINCL = 0x82;
|
||||
static constexpr u32 N_SOL = 0x84;
|
||||
static constexpr u32 N_PARAMS = 0x86;
|
||||
static constexpr u32 N_VERSION = 0x88;
|
||||
static constexpr u32 N_OLEVEL = 0x8A;
|
||||
static constexpr u32 N_PSYM = 0xa0;
|
||||
static constexpr u32 N_EINCL = 0xa2;
|
||||
static constexpr u32 N_ENTRY = 0xa4;
|
||||
static constexpr u32 N_LBRAC = 0xc0;
|
||||
static constexpr u32 N_EXCL = 0xc2;
|
||||
static constexpr u32 N_RBRAC = 0xe0;
|
||||
static constexpr u32 N_BCOMM = 0xe2;
|
||||
static constexpr u32 N_ECOMM = 0xe4;
|
||||
static constexpr u32 N_ECOML = 0xe8;
|
||||
static constexpr u32 N_LENG = 0xfe;
|
||||
static constexpr u32 N_PC = 0x30;
|
||||
|
||||
static constexpr u32 REFERENCE_TYPE = 0xf;
|
||||
static constexpr u32 REFERENCE_FLAG_UNDEFINED_NON_LAZY = 0;
|
||||
static constexpr u32 REFERENCE_FLAG_UNDEFINED_LAZY = 1;
|
||||
static constexpr u32 REFERENCE_FLAG_DEFINED = 2;
|
||||
static constexpr u32 REFERENCE_FLAG_PRIVATE_DEFINED = 3;
|
||||
static constexpr u32 REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4;
|
||||
static constexpr u32 REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY = 5;
|
||||
|
||||
static constexpr u32 REFERENCED_DYNAMICALLY = 0x0010;
|
||||
|
||||
static constexpr u32 N_NO_DEAD_STRIP = 0x0020;
|
||||
static constexpr u32 N_DESC_DISCARDED = 0x0020;
|
||||
static constexpr u32 N_WEAK_REF = 0x0040;
|
||||
static constexpr u32 N_WEAK_DEF = 0x0080;
|
||||
static constexpr u32 N_REF_TO_WEAK = 0x0080;
|
||||
static constexpr u32 N_ARM_THUMB_DEF = 0x0008;
|
||||
static constexpr u32 N_SYMBOL_RESOLVER = 0x0100;
|
||||
static constexpr u32 N_ALT_ENTRY = 0x0200;
|
||||
|
||||
static constexpr u32 PLATFORM_MACOS = 1;
|
||||
static constexpr u32 PLATFORM_IOS = 2;
|
||||
static constexpr u32 PLATFORM_TVOS = 3;
|
||||
static constexpr u32 PLATFORM_WATCHOS = 4;
|
||||
static constexpr u32 PLATFORM_BRIDGEOS = 5;
|
||||
static constexpr u32 PLATFORM_MACCATALYST = 6;
|
||||
static constexpr u32 PLATFORM_IOSSIMULATOR = 7;
|
||||
static constexpr u32 PLATFORM_TVOSSIMULATOR = 8;
|
||||
static constexpr u32 PLATFORM_WATCHOSSIMULATOR = 9;
|
||||
static constexpr u32 PLATFORM_DRIVERKIT = 10;
|
||||
|
||||
static constexpr u32 TOOL_CLANG = 1;
|
||||
static constexpr u32 TOOL_SWIFT = 2;
|
||||
static constexpr u32 TOOL_LD = 3;
|
||||
|
||||
static constexpr u32 ARM64_RELOC_UNSIGNED = 0;
|
||||
static constexpr u32 ARM64_RELOC_SUBTRACTOR = 1;
|
||||
static constexpr u32 ARM64_RELOC_BRANCH26 = 2;
|
||||
static constexpr u32 ARM64_RELOC_PAGE21 = 3;
|
||||
static constexpr u32 ARM64_RELOC_PAGEOFF12 = 4;
|
||||
static constexpr u32 ARM64_RELOC_GOT_LOAD_PAGE21 = 5;
|
||||
static constexpr u32 ARM64_RELOC_GOT_LOAD_PAGEOFF12 = 6;
|
||||
static constexpr u32 ARM64_RELOC_POINTER_TO_GOT = 7;
|
||||
static constexpr u32 ARM64_RELOC_TLVP_LOAD_PAGE21 = 8;
|
||||
static constexpr u32 ARM64_RELOC_TLVP_LOAD_PAGEOFF12 = 9;
|
||||
static constexpr u32 ARM64_RELOC_ADDEND = 10;
|
||||
|
||||
static constexpr u32 X86_64_RELOC_UNSIGNED = 0;
|
||||
static constexpr u32 X86_64_RELOC_SIGNED = 1;
|
||||
static constexpr u32 X86_64_RELOC_BRANCH = 2;
|
||||
static constexpr u32 X86_64_RELOC_GOT_LOAD = 3;
|
||||
static constexpr u32 X86_64_RELOC_GOT = 4;
|
||||
static constexpr u32 X86_64_RELOC_SUBTRACTOR = 5;
|
||||
static constexpr u32 X86_64_RELOC_SIGNED_1 = 6;
|
||||
static constexpr u32 X86_64_RELOC_SIGNED_2 = 7;
|
||||
static constexpr u32 X86_64_RELOC_SIGNED_4 = 8;
|
||||
static constexpr u32 X86_64_RELOC_TLV = 9;
|
||||
|
||||
struct FatHeader {
|
||||
ubig32 magic;
|
||||
ubig32 nfat_arch;
|
||||
};
|
||||
|
||||
struct FatArch {
|
||||
ubig32 cputype;
|
||||
ubig32 cpusubtype;
|
||||
ubig32 offset;
|
||||
ubig32 size;
|
||||
ubig32 align;
|
||||
};
|
||||
|
||||
struct MachHeader {
|
||||
u32 magic;
|
||||
u32 cputype;
|
||||
u32 cpusubtype;
|
||||
u32 filetype;
|
||||
u32 ncmds;
|
||||
u32 sizeofcmds;
|
||||
u32 flags;
|
||||
u32 reserved;
|
||||
};
|
||||
|
||||
struct LoadCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
};
|
||||
|
||||
struct SegmentCommand {
|
||||
std::string_view get_segname() const {
|
||||
return {segname, strnlen(segname, sizeof(segname))};
|
||||
}
|
||||
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
char segname[16];
|
||||
u64 vmaddr;
|
||||
u64 vmsize;
|
||||
u64 fileoff;
|
||||
u64 filesize;
|
||||
u32 maxprot;
|
||||
u32 initprot;
|
||||
u32 nsects;
|
||||
u32 flags;
|
||||
};
|
||||
|
||||
struct MachSection {
|
||||
void set_segname(std::string_view name) {
|
||||
assert(name.size() <= sizeof(segname));
|
||||
memcpy(segname, name.data(), name.size());
|
||||
}
|
||||
|
||||
std::string_view get_segname() const {
|
||||
return {segname, strnlen(segname, sizeof(segname))};
|
||||
}
|
||||
|
||||
void set_sectname(std::string_view name) {
|
||||
assert(name.size() <= sizeof(sectname));
|
||||
memcpy(sectname, name.data(), name.size());
|
||||
}
|
||||
|
||||
std::string_view get_sectname() const {
|
||||
return {sectname, strnlen(sectname, sizeof(sectname))};
|
||||
}
|
||||
|
||||
bool match(std::string_view segname, std::string_view sectname) const {
|
||||
return get_segname() == segname && get_sectname() == sectname;
|
||||
}
|
||||
|
||||
char sectname[16];
|
||||
char segname[16];
|
||||
u64 addr;
|
||||
u64 size;
|
||||
u32 offset;
|
||||
u32 p2align;
|
||||
u32 reloff;
|
||||
u32 nreloc;
|
||||
u32 type : 8;
|
||||
u32 attr : 24;
|
||||
u32 reserved1;
|
||||
u32 reserved2;
|
||||
u32 reserved3;
|
||||
};
|
||||
|
||||
struct DylibCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
u32 nameoff;
|
||||
u32 timestamp;
|
||||
u32 current_version;
|
||||
u32 compatibility_version;
|
||||
};
|
||||
|
||||
struct DylinkerCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
u32 nameoff;
|
||||
};
|
||||
|
||||
struct SymtabCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
u32 symoff;
|
||||
u32 nsyms;
|
||||
u32 stroff;
|
||||
u32 strsize;
|
||||
};
|
||||
|
||||
struct DysymtabCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
u32 ilocalsym;
|
||||
u32 nlocalsym;
|
||||
u32 iextdefsym;
|
||||
u32 nextdefsym;
|
||||
u32 iundefsym;
|
||||
u32 nundefsym;
|
||||
u32 tocoff;
|
||||
u32 ntoc;
|
||||
u32 modtaboff;
|
||||
u32 nmodtab;
|
||||
u32 extrefsymoff;
|
||||
u32 nextrefsyms;
|
||||
u32 indirectsymoff;
|
||||
u32 nindirectsyms;
|
||||
u32 extreloff;
|
||||
u32 nextrel;
|
||||
u32 locreloff;
|
||||
u32 nlocrel;
|
||||
};
|
||||
|
||||
struct VersionMinCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
u32 version;
|
||||
u32 sdk;
|
||||
};
|
||||
|
||||
struct DyldInfoCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
u32 rebase_off;
|
||||
u32 rebase_size;
|
||||
u32 bind_off;
|
||||
u32 bind_size;
|
||||
u32 weak_bind_off;
|
||||
u32 weak_bind_size;
|
||||
u32 lazy_bind_off;
|
||||
u32 lazy_bind_size;
|
||||
u32 export_off;
|
||||
u32 export_size;
|
||||
};
|
||||
|
||||
struct UUIDCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
u8 uuid[16];
|
||||
};
|
||||
|
||||
struct RpathCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
u32 path_off;
|
||||
};
|
||||
|
||||
struct LinkEditDataCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
u32 dataoff;
|
||||
u32 datasize;
|
||||
};
|
||||
|
||||
struct BuildToolVersion {
|
||||
u32 tool;
|
||||
u32 version;
|
||||
};
|
||||
|
||||
struct BuildVersionCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
u32 platform;
|
||||
u32 minos;
|
||||
u32 sdk;
|
||||
u32 ntools;
|
||||
};
|
||||
|
||||
struct EntryPointCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
u64 entryoff;
|
||||
u64 stacksize;
|
||||
};
|
||||
|
||||
struct SourceVersionCommand {
|
||||
u32 cmd;
|
||||
u32 cmdsize;
|
||||
u64 version;
|
||||
};
|
||||
|
||||
struct DataInCodeEntry {
|
||||
u32 offset;
|
||||
u16 length;
|
||||
u16 kind;
|
||||
};
|
||||
|
||||
// This struct is named `n_list` on BSD and macOS.
|
||||
struct MachSym {
|
||||
bool is_undef() const {
|
||||
return type == N_UNDF && !is_common();
|
||||
}
|
||||
|
||||
bool is_common() const {
|
||||
return type == N_UNDF && ext && value;
|
||||
}
|
||||
|
||||
u32 stroff;
|
||||
u8 ext : 1;
|
||||
u8 type : 3;
|
||||
u8 pext : 1;
|
||||
u8 stub : 3;
|
||||
u8 sect;
|
||||
union {
|
||||
u16 desc;
|
||||
struct {
|
||||
u16 : 8;
|
||||
u16 p2align : 4;
|
||||
u16 : 4;
|
||||
};
|
||||
};
|
||||
u64 value;
|
||||
};
|
||||
|
||||
// This struct is named `relocation_info` on BSD and macOS.
|
||||
struct MachRel {
|
||||
u32 offset;
|
||||
u32 idx : 24;
|
||||
u32 is_pcrel : 1;
|
||||
u32 p2size : 2;
|
||||
u32 is_extern : 1;
|
||||
u32 type : 4;
|
||||
};
|
||||
|
||||
// __TEXT,__unwind_info section contents
|
||||
|
||||
static constexpr u32 UNWIND_SECTION_VERSION = 1;
|
||||
static constexpr u32 UNWIND_SECOND_LEVEL_REGULAR = 2;
|
||||
static constexpr u32 UNWIND_SECOND_LEVEL_COMPRESSED = 3;
|
||||
static constexpr u32 UNWIND_PERSONALITY_MASK = 0x30000000;
|
||||
|
||||
struct UnwindSectionHeader {
|
||||
u32 version;
|
||||
u32 encoding_offset;
|
||||
u32 encoding_count;
|
||||
u32 personality_offset;
|
||||
u32 personality_count;
|
||||
u32 page_offset;
|
||||
u32 page_count;
|
||||
};
|
||||
|
||||
struct UnwindFirstLevelPage {
|
||||
u32 func_addr;
|
||||
u32 page_offset;
|
||||
u32 lsda_offset;
|
||||
};
|
||||
|
||||
struct UnwindSecondLevelPage {
|
||||
u32 kind;
|
||||
u16 page_offset;
|
||||
u16 page_count;
|
||||
u16 encoding_offset;
|
||||
u16 encoding_count;
|
||||
};
|
||||
|
||||
struct UnwindLsdaEntry {
|
||||
u32 func_addr;
|
||||
u32 lsda_addr;
|
||||
};
|
||||
|
||||
struct UnwindPageEntry {
|
||||
u32 func_addr : 24;
|
||||
u32 encoding : 8;
|
||||
};
|
||||
|
||||
// __LD,__compact_unwind section contents
|
||||
|
||||
struct CompactUnwindEntry {
|
||||
u64 code_start;
|
||||
u32 code_len;
|
||||
u32 encoding;
|
||||
u64 personality;
|
||||
u64 lsda;
|
||||
};
|
||||
|
||||
// __LINKEDIT,__code_signature
|
||||
|
||||
static constexpr u32 CSMAGIC_EMBEDDED_SIGNATURE = 0xfade0cc0;
|
||||
static constexpr u32 CS_SUPPORTSEXECSEG = 0x20400;
|
||||
static constexpr u32 CSMAGIC_CODEDIRECTORY = 0xfade0c02;
|
||||
static constexpr u32 CSSLOT_CODEDIRECTORY = 0;
|
||||
static constexpr u32 CS_ADHOC = 0x00000002;
|
||||
static constexpr u32 CS_LINKER_SIGNED = 0x00020000;
|
||||
static constexpr u32 CS_EXECSEG_MAIN_BINARY = 1;
|
||||
static constexpr u32 CS_HASHTYPE_SHA256 = 2;
|
||||
|
||||
struct CodeSignatureHeader {
|
||||
ubig32 magic;
|
||||
ubig32 length;
|
||||
ubig32 count;
|
||||
};
|
||||
|
||||
struct CodeSignatureBlobIndex {
|
||||
ubig32 type;
|
||||
ubig32 offset;
|
||||
u32 padding;
|
||||
};
|
||||
|
||||
struct CodeSignatureDirectory {
|
||||
ubig32 magic;
|
||||
ubig32 length;
|
||||
ubig32 version;
|
||||
ubig32 flags;
|
||||
ubig32 hash_offset;
|
||||
ubig32 ident_offset;
|
||||
ubig32 n_special_slots;
|
||||
ubig32 n_code_slots;
|
||||
ubig32 code_limit;
|
||||
u8 hash_size;
|
||||
u8 hash_type;
|
||||
u8 platform;
|
||||
u8 page_size;
|
||||
ubig32 spare2;
|
||||
ubig32 scatter_offset;
|
||||
ubig32 team_offset;
|
||||
ubig32 spare3;
|
||||
ubig64 code_limit64;
|
||||
ubig64 exec_seg_base;
|
||||
ubig64 exec_seg_limit;
|
||||
ubig64 exec_seg_flags;
|
||||
};
|
||||
|
||||
struct ARM64 {
|
||||
static constexpr u32 cputype = CPU_TYPE_ARM64;
|
||||
static constexpr u32 cpusubtype = CPU_SUBTYPE_ARM64_ALL;
|
||||
static constexpr u32 abs_rel = ARM64_RELOC_UNSIGNED;
|
||||
static constexpr u32 word_size = 8;
|
||||
static constexpr u32 stub_size = 12;
|
||||
static constexpr u32 stub_helper_hdr_size = 24;
|
||||
static constexpr u32 stub_helper_size = 12;
|
||||
};
|
||||
|
||||
struct X86_64 {
|
||||
static constexpr u32 cputype = CPU_TYPE_X86_64;
|
||||
static constexpr u32 cpusubtype = CPU_SUBTYPE_X86_64_ALL;
|
||||
static constexpr u32 abs_rel = X86_64_RELOC_UNSIGNED;
|
||||
static constexpr u32 word_size = 8;
|
||||
static constexpr u32 stub_size = 6;
|
||||
static constexpr u32 stub_helper_hdr_size = 16;
|
||||
static constexpr u32 stub_helper_size = 10;
|
||||
};
|
||||
|
||||
} // namespace mold::macho
|
||||
+480
@@ -0,0 +1,480 @@
|
||||
#include "mold.h"
|
||||
#include "../archive-file.h"
|
||||
#include "../cmdline.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <fcntl.h>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
static std::pair<std::string_view, std::string_view>
|
||||
split_string(std::string_view str, char sep) {
|
||||
size_t pos = str.find(sep);
|
||||
if (pos == str.npos)
|
||||
return {str, ""};
|
||||
return {str.substr(0, pos), str.substr(pos + 1)};
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void create_internal_file(Context<E> &ctx) {
|
||||
ObjectFile<E> *obj = new ObjectFile<E>;
|
||||
ctx.obj_pool.push_back(std::unique_ptr<ObjectFile<E>>(obj));
|
||||
ctx.objs.push_back(obj);
|
||||
|
||||
auto add = [&](std::string_view name) {
|
||||
Symbol<E> *sym = get_symbol(ctx, name);
|
||||
sym->file = obj;
|
||||
obj->syms.push_back(sym);
|
||||
return sym;
|
||||
};
|
||||
|
||||
add("__dyld_private");
|
||||
|
||||
switch (ctx.output_type) {
|
||||
case MH_EXECUTE: {
|
||||
Symbol<E> *sym = add("__mh_execute_header");
|
||||
sym->is_extern = true;
|
||||
sym->referenced_dynamically = true;
|
||||
sym->value = ctx.arg.pagezero_size;
|
||||
break;
|
||||
}
|
||||
case MH_DYLIB:
|
||||
add("__mh_dylib_header");
|
||||
break;
|
||||
case MH_BUNDLE:
|
||||
add("__mh_bundle_header");
|
||||
break;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static bool compare_segments(const std::unique_ptr<OutputSegment<E>> &a,
|
||||
const std::unique_ptr<OutputSegment<E>> &b) {
|
||||
// We want to sort output segments in the following order:
|
||||
// __TEXT, __DATA_CONST, __DATA, <other segments>, __LINKEDIT
|
||||
auto get_rank = [](std::string_view name) {
|
||||
if (name == "__TEXT")
|
||||
return 0;
|
||||
if (name == "__DATA_CONST")
|
||||
return 1;
|
||||
if (name == "__DATA")
|
||||
return 2;
|
||||
return INT_MAX;
|
||||
};
|
||||
|
||||
std::string_view na = a->cmd.get_segname();
|
||||
std::string_view nb = b->cmd.get_segname();
|
||||
i64 ra = get_rank(na);
|
||||
i64 rb = get_rank(nb);
|
||||
if (ra != INT_MAX || rb != INT_MAX)
|
||||
return ra < rb;
|
||||
|
||||
if (na == "__LINKEDIT" || nb == "__LINKEDIT")
|
||||
return na != "__LINKEDIT";
|
||||
return na < nb;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static bool compare_chunks(const Chunk<E> *a, const Chunk<E> *b) {
|
||||
assert(a->hdr.get_segname() == b->hdr.get_segname());
|
||||
|
||||
if ((a->hdr.type == S_ZEROFILL) != (b->hdr.type == S_ZEROFILL))
|
||||
return a->hdr.type != S_ZEROFILL;
|
||||
|
||||
static const std::string_view rank[] = {
|
||||
// __TEXT
|
||||
"__mach_header",
|
||||
"__text",
|
||||
"__stubs",
|
||||
"__stub_helper",
|
||||
"__gcc_except_tab",
|
||||
"__cstring",
|
||||
"__unwind_info",
|
||||
// __DATA_CONST
|
||||
"__got",
|
||||
// __DATA
|
||||
"__la_symbol_ptr",
|
||||
"__data",
|
||||
"__thread_ptr",
|
||||
"__thread_data",
|
||||
"__thread_vars",
|
||||
"__thread_bss",
|
||||
"__common",
|
||||
"__bss",
|
||||
// __LINKEDIT
|
||||
"__rebase",
|
||||
"__binding",
|
||||
"__weak_binding",
|
||||
"__lazy_binding",
|
||||
"__export",
|
||||
"__func_starts",
|
||||
"__data_in_code",
|
||||
"__symbol_table",
|
||||
"__ind_sym_tab",
|
||||
"__string_table",
|
||||
"__code_signature",
|
||||
};
|
||||
|
||||
auto get_rank = [](const Chunk<E> *chunk) -> i64 {
|
||||
std::string_view name = chunk->hdr.get_sectname();
|
||||
i64 i = 0;
|
||||
for (; i < sizeof(rank) / sizeof(rank[0]); i++)
|
||||
if (name == rank[i])
|
||||
return i;
|
||||
return INT_MAX;
|
||||
};
|
||||
|
||||
i64 ra = get_rank(a);
|
||||
i64 rb = get_rank(b);
|
||||
if (ra == INT_MAX && rb == INT_MAX)
|
||||
return a->hdr.get_sectname() < b->hdr.get_sectname();
|
||||
return ra < rb;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void create_synthetic_chunks(Context<E> &ctx) {
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
for (std::unique_ptr<Subsection<E>> &subsec : file->subsections)
|
||||
subsec->isec.osec.add_subsec(subsec.get());
|
||||
|
||||
for (Chunk<E> *chunk : ctx.chunks) {
|
||||
if (chunk != ctx.data && chunk->is_regular &&
|
||||
((OutputSection<E> *)chunk)->members.empty())
|
||||
continue;
|
||||
|
||||
OutputSegment<E> *seg =
|
||||
OutputSegment<E>::get_instance(ctx, chunk->hdr.get_segname());
|
||||
seg->chunks.push_back(chunk);
|
||||
}
|
||||
|
||||
sort(ctx.segments, compare_segments<E>);
|
||||
|
||||
for (std::unique_ptr<OutputSegment<E>> &seg : ctx.segments)
|
||||
sort(seg->chunks, compare_chunks<E>);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void scan_unwind_info(Context<E> &ctx) {
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
for (UnwindRecord<E> &rec : file->unwind_records)
|
||||
if (!ctx.arg.dead_strip || rec.is_alive)
|
||||
if (rec.personality)
|
||||
rec.personality->flags |= NEEDS_GOT;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void export_symbols(Context<E> &ctx) {
|
||||
ctx.got.add(ctx, get_symbol(ctx, "dyld_stub_binder"));
|
||||
|
||||
for (ObjectFile<E> *file : ctx.objs) {
|
||||
for (Symbol<E> *sym : file->syms) {
|
||||
if (sym && sym->file == file) {
|
||||
if (sym->flags & NEEDS_GOT)
|
||||
ctx.got.add(ctx, sym);
|
||||
if (sym->flags & NEEDS_THREAD_PTR)
|
||||
ctx.thread_ptrs.add(ctx, sym);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (DylibFile<E> *file : ctx.dylibs) {
|
||||
for (Symbol<E> *sym : file->syms) {
|
||||
if (sym && sym->file == file) {
|
||||
if (sym->flags & NEEDS_STUB)
|
||||
ctx.stubs.add(ctx, sym);
|
||||
if (sym->flags & NEEDS_GOT)
|
||||
ctx.got.add(ctx, sym);
|
||||
if (sym->flags & NEEDS_THREAD_PTR)
|
||||
ctx.thread_ptrs.add(ctx, sym);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static i64 assign_offsets(Context<E> &ctx) {
|
||||
i64 sect_idx = 1;
|
||||
for (std::unique_ptr<OutputSegment<E>> &seg : ctx.segments)
|
||||
for (Chunk<E> *chunk : seg->chunks)
|
||||
if (!chunk->is_hidden)
|
||||
chunk->sect_idx = sect_idx++;
|
||||
|
||||
i64 fileoff = 0;
|
||||
i64 vmaddr = ctx.arg.pagezero_size;
|
||||
|
||||
for (std::unique_ptr<OutputSegment<E>> &seg : ctx.segments) {
|
||||
seg->set_offset(ctx, fileoff, vmaddr);
|
||||
fileoff += seg->cmd.filesize;
|
||||
vmaddr += seg->cmd.vmsize;
|
||||
}
|
||||
return fileoff;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void fix_synthetic_symbol_values(Context<E> &ctx) {
|
||||
get_symbol(ctx, "__dyld_private")->value = ctx.data->hdr.addr;
|
||||
get_symbol(ctx, "__mh_dylib_header")->value = ctx.data->hdr.addr;
|
||||
get_symbol(ctx, "__mh_bundle_header")->value = ctx.data->hdr.addr;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
MappedFile<Context<E>> *find_framework(Context<E> &ctx, std::string name) {
|
||||
std::string suffix;
|
||||
std::tie(name, suffix) = split_string(name, ',');
|
||||
|
||||
for (std::string path : ctx.arg.framework_paths) {
|
||||
path = get_realpath(path + "/" + name + ".framework/" + name);
|
||||
|
||||
if (!suffix.empty())
|
||||
if (auto *mf = MappedFile<Context<E>>::open(ctx, path + suffix))
|
||||
return mf;
|
||||
|
||||
if (auto *mf = MappedFile<Context<E>>::open(ctx, path + ".tbd"))
|
||||
return mf;
|
||||
|
||||
if (auto *mf = MappedFile<Context<E>>::open(ctx, path))
|
||||
return mf;
|
||||
}
|
||||
Fatal(ctx) << "-framework not found: " << name;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
MappedFile<Context<E>> *find_library(Context<E> &ctx, std::string name) {
|
||||
for (std::string dir : ctx.arg.library_paths) {
|
||||
for (std::string ext : {".tbd", ".dylib", ".a"}) {
|
||||
std::string path = dir + "/lib" + name + ext;
|
||||
if (MappedFile<Context<E>> *mf = MappedFile<Context<E>>::open(ctx, path))
|
||||
return mf;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static MappedFile<Context<E>> *
|
||||
strip_universal_header(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
FatHeader &hdr = *(FatHeader *)mf->data;
|
||||
assert(hdr.magic == FAT_MAGIC);
|
||||
|
||||
FatArch *arch = (FatArch *)(mf->data + sizeof(hdr));
|
||||
for (i64 i = 0; i < hdr.nfat_arch; i++)
|
||||
if (arch[i].cputype == E::cputype)
|
||||
return mf->slice(ctx, mf->name, arch[i].offset, arch[i].size);
|
||||
Fatal(ctx) << mf->name << ": fat file contains no matching file";
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void read_file(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
bool is_needed = false) {
|
||||
if (get_file_type(mf) == FileType::MACH_UNIVERSAL)
|
||||
mf = strip_universal_header(ctx, mf);
|
||||
|
||||
switch (get_file_type(mf)) {
|
||||
case FileType::TAPI:
|
||||
case FileType::MACH_DYLIB:
|
||||
ctx.dylibs.push_back(DylibFile<E>::create(ctx, mf));
|
||||
if (is_needed || !ctx.arg.dead_strip_dylibs)
|
||||
ctx.dylibs.back()->is_needed = true;
|
||||
break;
|
||||
case FileType::MACH_OBJ:
|
||||
ctx.objs.push_back(ObjectFile<E>::create(ctx, mf, ""));
|
||||
break;
|
||||
case FileType::AR:
|
||||
for (MappedFile<Context<E>> *child : read_archive_members(ctx, mf))
|
||||
if (get_file_type(child) == FileType::MACH_OBJ)
|
||||
ctx.objs.push_back(ObjectFile<E>::create(ctx, child, mf->name));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
// Fatal(ctx) << mf->name << ": unknown file type";
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static std::vector<std::string>
|
||||
read_filelist(Context<E> &ctx, std::string arg) {
|
||||
std::string path;
|
||||
std::string dir;
|
||||
|
||||
if (size_t pos = arg.find(','); pos != arg.npos) {
|
||||
path = arg.substr(0, pos);
|
||||
dir = arg.substr(pos + 1) + "/";
|
||||
} else {
|
||||
path = arg;
|
||||
}
|
||||
|
||||
MappedFile<Context<E>> *mf = MappedFile<Context<E>>::open(ctx, path);
|
||||
if (!mf)
|
||||
Fatal(ctx) << "-filepath: cannot open " << path;
|
||||
|
||||
std::vector<std::string> vec;
|
||||
for (std::string_view str = mf->get_contents(); !str.empty();) {
|
||||
std::string_view path;
|
||||
std::tie(path, str) = split_string(str, '\n');
|
||||
vec.push_back(path_clean(dir + std::string(path)));
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void read_input_files(Context<E> &ctx, std::span<std::string> args) {
|
||||
while (!args.empty()) {
|
||||
if (args[0].starts_with("-filelist")) {
|
||||
for (std::string &path : read_filelist(ctx, args[1])) {
|
||||
MappedFile<Context<E>> *mf = MappedFile<Context<E>>::open(ctx, path);
|
||||
if (!mf)
|
||||
Fatal(ctx) << "-filepath " << args[1] << ": cannot open file: " << path;
|
||||
read_file(ctx, mf);
|
||||
}
|
||||
args = args.subspan(2);
|
||||
} else if (args[0] == "-framework" || args[0] == "-needed_framework") {
|
||||
bool needed = (args[0] == "-needed_framework");
|
||||
read_file(ctx, find_framework(ctx, args[1]), needed);
|
||||
args = args.subspan(2);
|
||||
} else if (args[0] == "-l" || args[0] == "-needed-l") {
|
||||
MappedFile<Context<E>> *mf = find_library(ctx, args[1]);
|
||||
if (!mf)
|
||||
Fatal(ctx) << "library not found: -l" << args[1];
|
||||
bool needed = (args[0] == "-needed-l");
|
||||
read_file(ctx, mf, needed);
|
||||
args = args.subspan(2);
|
||||
} else {
|
||||
read_file(ctx, MappedFile<Context<E>>::must_open(ctx, args[0]));
|
||||
args = args.subspan(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx.objs.empty())
|
||||
Fatal(ctx) << "no input files";
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static int do_main(int argc, char **argv) {
|
||||
Context<E> ctx;
|
||||
|
||||
if (argc > 1 && std::string_view(argv[1]) == "-dump") {
|
||||
if (argc != 3)
|
||||
Fatal(ctx) << "usage: ld64.mold -dump <executable-name>\n";
|
||||
dump_file(argv[2]);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
for (i64 i = 0; i < argc; i++)
|
||||
ctx.cmdline_args.push_back(argv[i]);
|
||||
|
||||
std::vector<std::string> file_args;
|
||||
parse_nonpositional_args(ctx, file_args);
|
||||
|
||||
if (ctx.arg.arch != E::cputype) {
|
||||
if (ctx.arg.arch == CPU_TYPE_X86_64)
|
||||
return do_main<X86_64>(argc, argv);
|
||||
}
|
||||
|
||||
read_input_files(ctx, file_args);
|
||||
|
||||
i64 priority = 1;
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
file->priority = priority++;
|
||||
for (DylibFile<E> *dylib : ctx.dylibs)
|
||||
dylib->priority = priority++;
|
||||
|
||||
for (i64 i = 0; i < ctx.dylibs.size(); i++)
|
||||
ctx.dylibs[i]->dylib_idx = i + 1;
|
||||
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
file->parse(ctx);
|
||||
for (DylibFile<E> *dylib : ctx.dylibs)
|
||||
dylib->parse(ctx);
|
||||
|
||||
if (ctx.arg.ObjC)
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
if (!file->archive_name.empty() && file->is_objc_object(ctx))
|
||||
file->is_alive = true;
|
||||
|
||||
for (ObjectFile<E> *file : ctx.objs) {
|
||||
if (file->is_alive)
|
||||
file->resolve_regular_symbols(ctx);
|
||||
else
|
||||
file->resolve_lazy_symbols(ctx);
|
||||
}
|
||||
|
||||
std::vector<ObjectFile<E> *> live_objs;
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
if (file->is_alive)
|
||||
live_objs.push_back(file);
|
||||
|
||||
for (i64 i = 0; i < live_objs.size(); i++)
|
||||
append(live_objs, live_objs[i]->mark_live_objects(ctx));
|
||||
|
||||
for (DylibFile<E> *dylib : ctx.dylibs)
|
||||
dylib->resolve_symbols(ctx);
|
||||
|
||||
if (ctx.output_type == MH_EXECUTE && !get_symbol(ctx, ctx.arg.entry)->file)
|
||||
Error(ctx) << "undefined entry point symbol: " << ctx.arg.entry;
|
||||
|
||||
create_internal_file(ctx);
|
||||
|
||||
std::erase_if(ctx.objs, [](ObjectFile<E> *file) { return !file->is_alive; });
|
||||
std::erase_if(ctx.dylibs, [](DylibFile<E> *file) { return !file->is_alive; });
|
||||
|
||||
if (ctx.arg.trace) {
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
SyncOut(ctx) << *file;
|
||||
for (DylibFile<E> *file : ctx.dylibs)
|
||||
SyncOut(ctx) << *file;
|
||||
}
|
||||
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
file->convert_common_symbols(ctx);
|
||||
|
||||
if (ctx.arg.dead_strip)
|
||||
dead_strip(ctx);
|
||||
|
||||
create_synthetic_chunks(ctx);
|
||||
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
file->check_duplicate_symbols(ctx);
|
||||
|
||||
for (i64 i = 0; i < ctx.segments.size(); i++)
|
||||
ctx.segments[i]->seg_idx = i + 1;
|
||||
|
||||
for (ObjectFile<E> *file : ctx.objs)
|
||||
for (std::unique_ptr<Subsection<E> > &subsec : file->subsections)
|
||||
subsec->scan_relocations(ctx);
|
||||
|
||||
scan_unwind_info(ctx);
|
||||
|
||||
if (ctx.arg.dead_strip_dylibs)
|
||||
std::erase_if(ctx.dylibs, [](DylibFile<E> *file) { return !file->is_needed; });
|
||||
|
||||
export_symbols(ctx);
|
||||
i64 output_size = assign_offsets(ctx);
|
||||
fix_synthetic_symbol_values(ctx);
|
||||
|
||||
ctx.output_file = OutputFile<E>::open(ctx, ctx.arg.output, output_size, 0777);
|
||||
ctx.buf = ctx.output_file->buf;
|
||||
|
||||
for (std::unique_ptr<OutputSegment<E>> &seg : ctx.segments)
|
||||
seg->copy_buf(ctx);
|
||||
ctx.code_sig.write_signature(ctx);
|
||||
|
||||
ctx.output_file->close(ctx);
|
||||
ctx.checkpoint();
|
||||
|
||||
if (!ctx.arg.map.empty())
|
||||
print_map(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
return do_main<ARM64>(argc, argv);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include <iomanip>
|
||||
#include <fstream>
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
struct Sym {
|
||||
u64 addr;
|
||||
u32 size;
|
||||
u32 fileidx;
|
||||
std::string_view name;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
void print_map(Context<E> &ctx) {
|
||||
std::ofstream out(ctx.arg.map.c_str());
|
||||
if (!out.is_open())
|
||||
Fatal(ctx) << "cannot open " << ctx.arg.map << ": " << errno_string();
|
||||
|
||||
out << "# Path: " << ctx.arg.output << "\n"
|
||||
<< "# Arch: x86-64\n"
|
||||
<< "# Object files:\n";
|
||||
|
||||
std::vector<Sym> syms;
|
||||
|
||||
for (i64 i = 0; i < ctx.objs.size(); i++){
|
||||
ObjectFile<E> &file = *ctx.objs[i];
|
||||
|
||||
if (file.is_alive) {
|
||||
out << "[" << std::setw(3) << i << "] " << file << "\n";
|
||||
for (Symbol<E> *sym : file.syms)
|
||||
if (sym->file == &file)
|
||||
syms.push_back({sym->get_addr(ctx), 0, (u32)i, sym->name});
|
||||
}
|
||||
}
|
||||
|
||||
sort(syms, [](const Sym &a, const Sym &b) {
|
||||
return a.addr < b.addr;
|
||||
});
|
||||
|
||||
out << "# Sections:\n"
|
||||
<< "# Address Size Segment Section\n";
|
||||
|
||||
for (std::unique_ptr<OutputSegment<E>> &seg : ctx.segments) {
|
||||
for (Chunk<E> *chunk : seg->chunks) {
|
||||
if (!chunk->is_hidden) {
|
||||
out << "0x" << std::hex
|
||||
<< std::setw(8) << std::setfill('0') << chunk->hdr.addr
|
||||
<< " 0x"
|
||||
<< std::setw(8) << std::setfill('0') << chunk->hdr.size
|
||||
<< " " << std::setw(7) << std::left << std::setfill(' ')
|
||||
<< chunk->hdr.get_segname()
|
||||
<< " " << chunk->hdr.get_sectname() << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out << "# Symbols:\n"
|
||||
<< "# Address Size File Name\n";
|
||||
|
||||
for (Sym &sym : syms) {
|
||||
out << "0x" << std::hex
|
||||
<< std::setw(8) << std::setfill('0') << sym.addr
|
||||
<< " 0x"
|
||||
<< std::setw(8) << std::setfill('0') << 0
|
||||
<< " ["
|
||||
<< std::setw(3) << std::right << std::setfill(' ') << std::dec
|
||||
<< sym.fileidx
|
||||
<< "] " << sym.name << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template void print_map(Context<E> &)
|
||||
|
||||
INSTANTIATE(ARM64);
|
||||
INSTANTIATE(X86_64);
|
||||
|
||||
} // namespace mold::macho
|
||||
+914
@@ -0,0 +1,914 @@
|
||||
#pragma once
|
||||
|
||||
#include "macho.h"
|
||||
#include "../mold.h"
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <tbb/concurrent_hash_map.h>
|
||||
#include <tbb/spin_mutex.h>
|
||||
#include <unordered_map>
|
||||
#include <variant>
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
static constexpr i64 COMMON_PAGE_SIZE = 0x4000;
|
||||
static constexpr i64 SHA256_SIZE = 32;
|
||||
|
||||
template <typename E> class Chunk;
|
||||
template <typename E> class InputSection;
|
||||
template <typename E> class OutputSection;
|
||||
template <typename E> class Subsection;
|
||||
template <typename E> struct Context;
|
||||
template <typename E> struct Symbol;
|
||||
|
||||
//
|
||||
// object-file.cc
|
||||
//
|
||||
|
||||
template <typename E>
|
||||
struct Relocation {
|
||||
u32 offset = 0;
|
||||
u8 type = -1;
|
||||
u8 p2size = 0;
|
||||
bool is_pcrel = false;
|
||||
i64 addend = 0;
|
||||
Symbol<E> *sym = nullptr;
|
||||
Subsection<E> *subsec = nullptr;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
struct UnwindRecord {
|
||||
UnwindRecord(u32 len, u32 enc) : code_len(len), encoding(enc) {}
|
||||
|
||||
inline u64 get_func_raddr(Context<E> &ctx) const;
|
||||
|
||||
Subsection<E> *subsec = nullptr;
|
||||
u32 offset = 0;
|
||||
u32 code_len;
|
||||
u32 encoding;
|
||||
Symbol<E> *personality = nullptr;
|
||||
Subsection<E> *lsda = nullptr;
|
||||
u32 lsda_offset = 0;
|
||||
bool is_alive = false;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class InputFile {
|
||||
public:
|
||||
MappedFile<Context<E>> *mf = nullptr;
|
||||
std::vector<Symbol<E> *> syms;
|
||||
i64 priority = 0;
|
||||
bool is_dylib = false;
|
||||
std::atomic_bool is_alive = false;
|
||||
std::string archive_name;
|
||||
|
||||
protected:
|
||||
InputFile() = default;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class ObjectFile : public InputFile<E> {
|
||||
public:
|
||||
ObjectFile() = default;
|
||||
|
||||
static ObjectFile *create(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
std::string archive_name);
|
||||
void parse(Context<E> &ctx);
|
||||
Subsection<E> *find_subsection(Context<E> &ctx, u32 addr);
|
||||
void parse_compact_unwind(Context<E> &ctx, MachSection &hdr);
|
||||
void resolve_regular_symbols(Context<E> &ctx);
|
||||
void resolve_lazy_symbols(Context<E> &ctx);
|
||||
bool is_objc_object(Context<E> &ctx);
|
||||
std::vector<ObjectFile *> mark_live_objects(Context<E> &ctx);
|
||||
void convert_common_symbols(Context<E> &ctx);
|
||||
void check_duplicate_symbols(Context<E> &ctx);
|
||||
|
||||
Relocation<E> read_reloc(Context<E> &ctx, const MachSection &hdr, MachRel r);
|
||||
|
||||
std::vector<std::unique_ptr<InputSection<E>>> sections;
|
||||
std::vector<std::unique_ptr<Subsection<E>>> subsections;
|
||||
std::vector<u32> sym_to_subsec;
|
||||
std::span<MachSym> mach_syms;
|
||||
std::vector<Symbol<E>> local_syms;
|
||||
std::vector<UnwindRecord<E>> unwind_records;
|
||||
std::span<DataInCodeEntry> data_in_code_entries;
|
||||
|
||||
private:
|
||||
void parse_sections(Context<E> &ctx);
|
||||
void parse_symtab(Context<E> &ctx);
|
||||
void split_subsections(Context<E> &ctx);
|
||||
void parse_data_in_code(Context<E> &ctx);
|
||||
LoadCommand *find_load_command(Context<E> &ctx, u32 type);
|
||||
i64 find_subsection_idx(Context<E> &ctx, u32 addr);
|
||||
void override_symbol(Context<E> &ctx, i64 symidx);
|
||||
InputSection<E> *get_common_sec(Context<E> &ctx);
|
||||
|
||||
MachSection *unwind_sec = nullptr;
|
||||
std::unique_ptr<MachSection> common_hdr;
|
||||
InputSection<E> *common_sec = nullptr;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class DylibFile : public InputFile<E> {
|
||||
public:
|
||||
static DylibFile *create(Context<E> &ctx, MappedFile<Context<E>> *mf);
|
||||
void parse(Context<E> &ctx);
|
||||
void resolve_symbols(Context<E> &ctx);
|
||||
|
||||
std::string_view install_name;
|
||||
i64 dylib_idx = 0;
|
||||
std::atomic_bool is_needed = false;
|
||||
|
||||
private:
|
||||
void parse_dylib(Context<E> &ctx);
|
||||
void read_trie(Context<E> &ctx, u8 *start, i64 offset = 0,
|
||||
const std::string &prefix = "");
|
||||
|
||||
DylibFile() {
|
||||
this->is_dylib = true;
|
||||
this->is_alive = true;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
std::ostream &operator<<(std::ostream &out, const InputFile<E> &file);
|
||||
|
||||
//
|
||||
// input-sections.cc
|
||||
//
|
||||
|
||||
template <typename E>
|
||||
class InputSection {
|
||||
public:
|
||||
InputSection(Context<E> &ctx, ObjectFile<E> &file, const MachSection &hdr);
|
||||
void parse_relocations(Context<E> &ctx);
|
||||
|
||||
ObjectFile<E> &file;
|
||||
const MachSection &hdr;
|
||||
OutputSection<E> &osec;
|
||||
std::string_view contents;
|
||||
std::vector<Symbol<E> *> syms;
|
||||
std::vector<Relocation<E>> rels;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
std::ostream &operator<<(std::ostream &out, const InputSection<E> &sec);
|
||||
|
||||
template <typename E>
|
||||
class Subsection {
|
||||
public:
|
||||
inline u64 get_addr(Context<E> &ctx) const;
|
||||
|
||||
std::string_view get_contents() {
|
||||
assert(isec.hdr.type != S_ZEROFILL);
|
||||
return isec.contents.substr(input_offset, input_size);
|
||||
}
|
||||
|
||||
std::span<UnwindRecord<E>> get_unwind_records() {
|
||||
return std::span<UnwindRecord<E>>(isec.file.unwind_records)
|
||||
.subspan(unwind_offset, nunwind);
|
||||
}
|
||||
|
||||
std::span<Relocation<E>> get_rels() const {
|
||||
return std::span<Relocation<E>>(isec.rels).subspan(rel_offset, nrels);
|
||||
}
|
||||
|
||||
void scan_relocations(Context<E> &ctx);
|
||||
void apply_reloc(Context<E> &ctx, u8 *buf);
|
||||
|
||||
InputSection<E> &isec;
|
||||
u32 input_offset = 0;
|
||||
u32 input_size = 0;
|
||||
u32 input_addr = 0;
|
||||
u32 rel_offset = 0;
|
||||
u32 nrels = 0;
|
||||
u32 unwind_offset = 0;
|
||||
u32 nunwind = 0;
|
||||
u32 raddr = -1;
|
||||
u16 p2align = 0;
|
||||
std::atomic_bool is_alive = false;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
std::vector<Relocation<E>>
|
||||
read_relocations(Context<E> &ctx, ObjectFile<E> &file, const MachSection &hdr);
|
||||
|
||||
//
|
||||
// Symbol
|
||||
//
|
||||
|
||||
enum {
|
||||
NEEDS_GOT = 1 << 0,
|
||||
NEEDS_STUB = 1 << 1,
|
||||
NEEDS_THREAD_PTR = 1 << 2,
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
struct Symbol {
|
||||
Symbol() = default;
|
||||
Symbol(std::string_view name) : name(name) {}
|
||||
Symbol(const Symbol<E> &other) : name(other.name) {}
|
||||
|
||||
std::string_view name;
|
||||
InputFile<E> *file = nullptr;
|
||||
Subsection<E> *subsec = nullptr;
|
||||
u64 value = 0;
|
||||
|
||||
i32 stub_idx = -1;
|
||||
i32 got_idx = -1;
|
||||
i32 tlv_idx = -1;
|
||||
|
||||
tbb::spin_mutex mu;
|
||||
|
||||
std::atomic_uint8_t flags = 0;
|
||||
|
||||
u8 is_extern : 1 = false;
|
||||
u8 is_lazy : 1 = false;
|
||||
u8 is_common : 1 = false;
|
||||
u8 referenced_dynamically : 1 = false;
|
||||
|
||||
inline u64 get_addr(Context<E> &ctx) const;
|
||||
inline u64 get_got_addr(Context<E> &ctx) const;
|
||||
inline u64 get_tlv_addr(Context<E> &ctx) const;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
std::ostream &operator<<(std::ostream &out, const Symbol<E> &sym);
|
||||
|
||||
//
|
||||
// output-chunks.cc
|
||||
//
|
||||
|
||||
template <typename E>
|
||||
class OutputSegment {
|
||||
public:
|
||||
static OutputSegment<E> *
|
||||
get_instance(Context<E> &ctx, std::string_view name);
|
||||
|
||||
void set_offset(Context<E> &ctx, i64 fileoff, u64 vmaddr);
|
||||
void copy_buf(Context<E> &ctx);
|
||||
|
||||
std::string_view name;
|
||||
SegmentCommand cmd = {};
|
||||
i32 seg_idx = -1;
|
||||
std::vector<Chunk<E> *> chunks;
|
||||
|
||||
private:
|
||||
OutputSegment(std::string_view name);
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class Chunk {
|
||||
public:
|
||||
inline Chunk(Context<E> &ctx, std::string_view segname,
|
||||
std::string_view sectname);
|
||||
|
||||
virtual ~Chunk() = default;
|
||||
virtual void compute_size(Context<E> &ctx) {};
|
||||
virtual void copy_buf(Context<E> &ctx) {}
|
||||
|
||||
MachSection hdr = {};
|
||||
u32 sect_idx = 0;
|
||||
bool is_hidden = false;
|
||||
bool is_regular = false;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
std::ostream &operator<<(std::ostream &out, const Chunk<E> &chunk);
|
||||
|
||||
template <typename E>
|
||||
class OutputMachHeader : public Chunk<E> {
|
||||
public:
|
||||
OutputMachHeader(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__TEXT", "__mach_header") {
|
||||
this->is_hidden = true;
|
||||
}
|
||||
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class OutputSection : public Chunk<E> {
|
||||
public:
|
||||
static OutputSection<E> *
|
||||
get_instance(Context<E> &ctx, std::string_view segname,
|
||||
std::string_view sectname);
|
||||
|
||||
OutputSection(Context<E> &ctx, std::string_view segname,
|
||||
std::string_view sectname)
|
||||
: Chunk<E>(ctx, segname, sectname) {
|
||||
this->is_regular = true;
|
||||
}
|
||||
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
void add_subsec(Subsection<E> *subsec) {
|
||||
members.push_back(subsec);
|
||||
this->hdr.p2align = std::max<u32>(this->hdr.p2align, subsec->p2align);
|
||||
this->hdr.attr |= subsec->isec.hdr.attr;
|
||||
this->hdr.type = subsec->isec.hdr.type;
|
||||
}
|
||||
|
||||
std::vector<Subsection<E> *> members;
|
||||
};
|
||||
|
||||
class RebaseEncoder {
|
||||
public:
|
||||
RebaseEncoder();
|
||||
void add(i64 seg_idx, i64 offset);
|
||||
void flush();
|
||||
void finish();
|
||||
|
||||
std::vector<u8> buf;
|
||||
|
||||
private:
|
||||
i64 cur_seg = -1;
|
||||
i64 cur_off = 0;
|
||||
i64 times = 0;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class OutputRebaseSection : public Chunk<E> {
|
||||
public:
|
||||
OutputRebaseSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__LINKEDIT", "__rebase") {
|
||||
this->is_hidden = true;
|
||||
}
|
||||
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
std::vector<u8> contents;
|
||||
};
|
||||
|
||||
class BindEncoder {
|
||||
public:
|
||||
BindEncoder();
|
||||
void add(i64 dylib_idx, std::string_view sym, i64 flags, i64 seg_idx,
|
||||
i64 offset);
|
||||
void finish();
|
||||
|
||||
std::vector<u8> buf;
|
||||
|
||||
private:
|
||||
std::string_view last_sym;
|
||||
i64 last_flags = -1;
|
||||
i64 last_dylib = -1;
|
||||
i64 last_seg = -1;
|
||||
i64 last_off = -1;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class OutputBindSection : public Chunk<E> {
|
||||
public:
|
||||
OutputBindSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__LINKEDIT", "__binding") {
|
||||
this->is_hidden = true;
|
||||
}
|
||||
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
std::vector<u8> contents;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class OutputLazyBindSection : public Chunk<E> {
|
||||
public:
|
||||
OutputLazyBindSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__LINKEDIT", "__lazy_binding") {
|
||||
this->is_hidden = true;
|
||||
this->hdr.p2align = __builtin_ctz(8);
|
||||
}
|
||||
|
||||
void add(Context<E> &ctx, Symbol<E> &sym, i64 flags);
|
||||
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
std::vector<u8> contents;
|
||||
};
|
||||
|
||||
class ExportEncoder {
|
||||
public:
|
||||
void add(std::string_view name, u32 flags, u64 addr);
|
||||
i64 finish();
|
||||
|
||||
void write_trie(u8 *buf) {
|
||||
write_trie(buf, root);
|
||||
}
|
||||
|
||||
private:
|
||||
struct Entry {
|
||||
std::string_view name;
|
||||
u32 flags;
|
||||
u64 addr;
|
||||
};
|
||||
|
||||
struct TrieNode {
|
||||
std::string_view prefix;
|
||||
bool is_leaf = false;
|
||||
u32 flags = 0;
|
||||
u64 addr = 0;
|
||||
u32 offset = -1;
|
||||
std::vector<TrieNode> children;
|
||||
};
|
||||
|
||||
static i64 common_prefix_len(std::span<Entry> entries, i64 len);
|
||||
static TrieNode construct_trie(std::span<Entry> entries, i64 len);
|
||||
static i64 set_offset(TrieNode &node, i64 offset);
|
||||
void write_trie(u8 *buf, TrieNode &node);
|
||||
|
||||
TrieNode root;
|
||||
std::vector<Entry> entries;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class OutputExportSection : public Chunk<E> {
|
||||
public:
|
||||
OutputExportSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__LINKEDIT", "__export") {
|
||||
this->is_hidden = true;
|
||||
}
|
||||
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
private:
|
||||
ExportEncoder enc;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class OutputFunctionStartsSection : public Chunk<E> {
|
||||
public:
|
||||
OutputFunctionStartsSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__LINKEDIT", "__func_starts") {
|
||||
this->is_hidden = true;
|
||||
}
|
||||
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
std::vector<u8> contents;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class OutputSymtabSection : public Chunk<E> {
|
||||
public:
|
||||
OutputSymtabSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__LINKEDIT", "__symbol_table") {
|
||||
this->is_hidden = true;
|
||||
this->hdr.p2align = __builtin_ctz(8);
|
||||
}
|
||||
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
struct Entry {
|
||||
Symbol<E> *sym;
|
||||
i64 stroff;
|
||||
};
|
||||
|
||||
std::vector<Entry> locals;
|
||||
std::vector<Entry> globals;
|
||||
std::vector<Entry> undefs;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class OutputStrtabSection : public Chunk<E> {
|
||||
public:
|
||||
OutputStrtabSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__LINKEDIT", "__string_table") {
|
||||
this->is_hidden = true;
|
||||
this->hdr.p2align = __builtin_ctz(8);
|
||||
}
|
||||
|
||||
i64 add_string(std::string_view str);
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
std::string contents{1, '\0'};
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class OutputIndirectSymtabSection : public Chunk<E> {
|
||||
public:
|
||||
OutputIndirectSymtabSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__LINKEDIT", "__ind_sym_tab") {
|
||||
this->is_hidden = true;
|
||||
}
|
||||
|
||||
static constexpr i64 ENTRY_SIZE = 4;
|
||||
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
struct Entry {
|
||||
Symbol<E> *sym;
|
||||
i64 symtab_idx;
|
||||
};
|
||||
|
||||
std::vector<Entry> stubs;
|
||||
std::vector<Entry> gots;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class CodeSignatureSection : public Chunk<E> {
|
||||
public:
|
||||
CodeSignatureSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__LINKEDIT", "__code_signature") {
|
||||
this->is_hidden = true;
|
||||
this->hdr.p2align = __builtin_ctz(16);
|
||||
}
|
||||
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void write_signature(Context<E> &ctx);
|
||||
|
||||
static constexpr i64 BLOCK_SIZE = 4096;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class DataInCodeSection : public Chunk<E> {
|
||||
public:
|
||||
DataInCodeSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__LINKEDIT", "__data_in_code") {
|
||||
this->is_hidden = true;
|
||||
this->hdr.p2align = __builtin_ctz(alignof(DataInCodeEntry));
|
||||
}
|
||||
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
std::vector<DataInCodeEntry> contents;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class StubsSection : public Chunk<E> {
|
||||
public:
|
||||
StubsSection(Context<E> &ctx) : Chunk<E>(ctx, "__TEXT", "__stubs") {
|
||||
this->hdr.p2align = __builtin_ctz(2);
|
||||
this->hdr.type = S_SYMBOL_STUBS;
|
||||
this->hdr.attr = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
|
||||
this->hdr.reserved2 = E::stub_size;
|
||||
}
|
||||
|
||||
void add(Context<E> &ctx, Symbol<E> *sym);
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
std::vector<Symbol<E> *> syms;
|
||||
std::vector<u32> bind_offsets;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class StubHelperSection : public Chunk<E> {
|
||||
public:
|
||||
StubHelperSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__TEXT", "__stub_helper") {
|
||||
this->hdr.p2align = __builtin_ctz(4);
|
||||
this->hdr.attr = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
|
||||
}
|
||||
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class UnwindEncoder {
|
||||
public:
|
||||
std::vector<u8> encode(Context<E> &ctx, std::span<UnwindRecord<E>> records);
|
||||
|
||||
private:
|
||||
u32 encode_personality(Context<E> &ctx, Symbol<E> *sym);
|
||||
|
||||
std::vector<std::span<UnwindRecord<E>>>
|
||||
split_records(Context<E> &ctx, std::span<UnwindRecord<E>>);
|
||||
|
||||
std::vector<Symbol<E> *> personalities;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class UnwindInfoSection : public Chunk<E> {
|
||||
public:
|
||||
UnwindInfoSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__TEXT", "__unwind_info") {
|
||||
this->hdr.p2align = __builtin_ctz(4);
|
||||
}
|
||||
|
||||
void compute_size(Context<E> &ctx) override;
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
std::vector<u8> contents;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class GotSection : public Chunk<E> {
|
||||
public:
|
||||
GotSection(Context<E> &ctx) : Chunk<E>(ctx, "__DATA_CONST", "__got") {
|
||||
this->hdr.p2align = __builtin_ctz(8);
|
||||
this->hdr.type = S_NON_LAZY_SYMBOL_POINTERS;
|
||||
}
|
||||
|
||||
void add(Context<E> &ctx, Symbol<E> *sym);
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
std::vector<Symbol<E> *> syms;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class LazySymbolPtrSection : public Chunk<E> {
|
||||
public:
|
||||
LazySymbolPtrSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__DATA", "__la_symbol_ptr") {
|
||||
this->hdr.p2align = __builtin_ctz(8);
|
||||
this->hdr.type = S_LAZY_SYMBOL_POINTERS;
|
||||
}
|
||||
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class ThreadPtrsSection : public Chunk<E> {
|
||||
public:
|
||||
ThreadPtrsSection(Context<E> &ctx)
|
||||
: Chunk<E>(ctx, "__DATA", "__thread_ptrs") {
|
||||
this->hdr.p2align = __builtin_ctz(8);
|
||||
this->hdr.type = S_THREAD_LOCAL_VARIABLE_POINTERS;
|
||||
}
|
||||
|
||||
void add(Context<E> &ctx, Symbol<E> *sym);
|
||||
void copy_buf(Context<E> &ctx) override;
|
||||
|
||||
std::vector<Symbol<E> *> syms;
|
||||
};
|
||||
|
||||
//
|
||||
// mapfile.cc
|
||||
//
|
||||
|
||||
template <typename E>
|
||||
void print_map(Context<E> &ctx);
|
||||
|
||||
//
|
||||
// dumper.cc
|
||||
//
|
||||
|
||||
void dump_file(std::string path);
|
||||
|
||||
//
|
||||
// output-file.cc
|
||||
//
|
||||
|
||||
template <typename E>
|
||||
class OutputFile {
|
||||
public:
|
||||
static std::unique_ptr<OutputFile>
|
||||
open(Context<E> &ctx, std::string path, i64 filesize, i64 perm);
|
||||
|
||||
virtual void close(Context<E> &ctx) = 0;
|
||||
virtual ~OutputFile() {}
|
||||
|
||||
u8 *buf = nullptr;
|
||||
std::string path;
|
||||
i64 filesize;
|
||||
bool is_mmapped;
|
||||
bool is_unmapped = false;
|
||||
|
||||
protected:
|
||||
OutputFile(std::string path, i64 filesize, bool is_mmapped)
|
||||
: path(path), filesize(filesize), is_mmapped(is_mmapped) {}
|
||||
};
|
||||
|
||||
//
|
||||
// yaml.cc
|
||||
//
|
||||
|
||||
struct YamlNode {
|
||||
std::variant<std::string_view,
|
||||
std::vector<YamlNode>,
|
||||
std::map<std::string_view, YamlNode>> data;
|
||||
};
|
||||
|
||||
struct YamlError {
|
||||
std::string msg;
|
||||
i64 pos;
|
||||
};
|
||||
|
||||
std::variant<std::vector<YamlNode>, YamlError>
|
||||
parse_yaml(std::string_view str);
|
||||
|
||||
//
|
||||
// tapi.cc
|
||||
//
|
||||
|
||||
struct TextDylib {
|
||||
std::string_view uuid;
|
||||
std::string_view install_name;
|
||||
std::string_view current_version = "1.0";
|
||||
std::string_view parent_umbrella;
|
||||
std::vector<std::string_view> reexported_libs;
|
||||
std::vector<std::string_view> exports;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
TextDylib parse_tbd(Context<E> &ctx, MappedFile<Context<E>> *mf);
|
||||
|
||||
//
|
||||
// cmdline.cc
|
||||
//
|
||||
|
||||
template <typename E>
|
||||
void parse_nonpositional_args(Context<E> &ctx,
|
||||
std::vector<std::string> &remaining);
|
||||
|
||||
//
|
||||
// dead-strip.cc
|
||||
//
|
||||
|
||||
template <typename E>
|
||||
void dead_strip(Context<E> &ctx);
|
||||
|
||||
//
|
||||
// main.cc
|
||||
//
|
||||
|
||||
template <typename E>
|
||||
struct Context {
|
||||
Context() {
|
||||
text_seg = OutputSegment<E>::get_instance(*this, "__TEXT");
|
||||
data_const_seg = OutputSegment<E>::get_instance(*this, "__DATA_CONST");
|
||||
data_seg = OutputSegment<E>::get_instance(*this, "__DATA");
|
||||
linkedit_seg = OutputSegment<E>::get_instance(*this, "__LINKEDIT");
|
||||
|
||||
text = OutputSection<E>::get_instance(*this, "__TEXT", "__text");
|
||||
data = OutputSection<E>::get_instance(*this, "__DATA", "__data");
|
||||
bss = OutputSection<E>::get_instance(*this, "__DATA", "__bss");
|
||||
cstring = OutputSection<E>::get_instance(*this, "__TEXT", "__cstring");
|
||||
common = OutputSection<E>::get_instance(*this, "__DATA", "__common");
|
||||
|
||||
bss->hdr.type = S_ZEROFILL;
|
||||
cstring->hdr.type = S_CSTRING_LITERALS;
|
||||
common->hdr.type = S_ZEROFILL;
|
||||
}
|
||||
|
||||
Context(const Context<E> &) = delete;
|
||||
|
||||
void checkpoint() {
|
||||
if (has_error) {
|
||||
cleanup();
|
||||
_exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Command-line arguments
|
||||
struct {
|
||||
bool ObjC = false;
|
||||
bool adhoc_codesign = true;
|
||||
bool color_diagnostics = false;
|
||||
bool dead_strip = true;
|
||||
bool dead_strip_dylibs = false;
|
||||
bool deduplicate = true;
|
||||
bool demangle = false;
|
||||
bool dylib = false;
|
||||
bool dynamic = true;
|
||||
bool fatal_warnings = false;
|
||||
bool trace = false;
|
||||
i64 arch = CPU_TYPE_ARM64;
|
||||
i64 headerpad = 256;
|
||||
i64 pagezero_size = 0;
|
||||
i64 platform = PLATFORM_MACOS;
|
||||
i64 platform_min_version = 0;
|
||||
i64 platform_sdk_version = 0;
|
||||
std::string chroot;
|
||||
std::string entry = "_main";
|
||||
std::string map;
|
||||
std::string output = "a.out";
|
||||
std::vector<std::string> framework_paths;
|
||||
std::vector<std::string> library_paths;
|
||||
std::vector<std::string> rpath;
|
||||
std::vector<std::string> syslibroot;
|
||||
} arg;
|
||||
|
||||
std::vector<std::string_view> cmdline_args;
|
||||
u32 output_type = MH_EXECUTE;
|
||||
|
||||
bool has_error = false;
|
||||
|
||||
tbb::concurrent_hash_map<std::string_view, Symbol<E>> symbol_map;
|
||||
|
||||
std::unique_ptr<OutputFile<E>> output_file;
|
||||
u8 *buf;
|
||||
|
||||
tbb::concurrent_vector<std::unique_ptr<ObjectFile<E>>> obj_pool;
|
||||
tbb::concurrent_vector<std::unique_ptr<DylibFile<E>>> dylib_pool;
|
||||
tbb::concurrent_vector<std::unique_ptr<u8[]>> string_pool;
|
||||
tbb::concurrent_vector<std::unique_ptr<MappedFile<Context<E>>>> mf_pool;
|
||||
std::vector<std::unique_ptr<OutputSection<E>>> osec_pool;
|
||||
|
||||
tbb::concurrent_vector<std::unique_ptr<TimerRecord>> timer_records;
|
||||
|
||||
std::vector<ObjectFile<E> *> objs;
|
||||
std::vector<DylibFile<E> *> dylibs;
|
||||
|
||||
OutputSegment<E> *text_seg = nullptr;
|
||||
OutputSegment<E> *data_const_seg = nullptr;
|
||||
OutputSegment<E> *data_seg = nullptr;
|
||||
OutputSegment<E> *linkedit_seg = nullptr;
|
||||
|
||||
std::vector<std::unique_ptr<OutputSegment<E>>> segments;
|
||||
std::vector<Chunk<E> *> chunks;
|
||||
|
||||
OutputMachHeader<E> mach_hdr{*this};
|
||||
StubsSection<E> stubs{*this};
|
||||
StubHelperSection<E> stub_helper{*this};
|
||||
UnwindInfoSection<E> unwind_info{*this};
|
||||
GotSection<E> got{*this};
|
||||
LazySymbolPtrSection<E> lazy_symbol_ptr{*this};
|
||||
CodeSignatureSection<E> code_sig{*this};
|
||||
DataInCodeSection<E> data_in_code{*this};
|
||||
ThreadPtrsSection<E> thread_ptrs{*this};
|
||||
|
||||
OutputRebaseSection<E> rebase{*this};
|
||||
OutputBindSection<E> bind{*this};
|
||||
OutputLazyBindSection<E> lazy_bind{*this};
|
||||
OutputExportSection<E> export_{*this};
|
||||
OutputFunctionStartsSection<E> function_starts{*this};
|
||||
OutputSymtabSection<E> symtab{*this};
|
||||
OutputIndirectSymtabSection<E> indir_symtab{*this};
|
||||
OutputStrtabSection<E> strtab{*this};
|
||||
|
||||
OutputSection<E> *text = nullptr;
|
||||
OutputSection<E> *data = nullptr;
|
||||
OutputSection<E> *bss = nullptr;
|
||||
OutputSection<E> *cstring = nullptr;
|
||||
OutputSection<E> *common = nullptr;
|
||||
};
|
||||
|
||||
int main(int argc, char **argv);
|
||||
|
||||
//
|
||||
// Inline functions
|
||||
//
|
||||
|
||||
template <typename E>
|
||||
std::ostream &operator<<(std::ostream &out, const InputSection<E> &sec) {
|
||||
out << sec.file << "(" << sec.hdr.get_segname() << ","
|
||||
<< sec.hdr.get_sectname() << ")";
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
u64 Subsection<E>::get_addr(Context<E> &ctx) const {
|
||||
return ctx.arg.pagezero_size + raddr;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
u64 Symbol<E>::get_addr(Context<E> &ctx) const {
|
||||
if (subsec)
|
||||
return subsec->get_addr(ctx) + value;
|
||||
if (stub_idx != -1)
|
||||
return ctx.stubs.hdr.addr + stub_idx * E::stub_size;
|
||||
return value;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
u64 Symbol<E>::get_got_addr(Context<E> &ctx) const {
|
||||
assert(got_idx != -1);
|
||||
return ctx.got.hdr.addr + got_idx * E::word_size;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
u64 Symbol<E>::get_tlv_addr(Context<E> &ctx) const {
|
||||
assert(tlv_idx != -1);
|
||||
return ctx.thread_ptrs.hdr.addr + tlv_idx * E::word_size;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
inline Symbol<E> *get_symbol(Context<E> &ctx, std::string_view name) {
|
||||
typename decltype(ctx.symbol_map)::const_accessor acc;
|
||||
ctx.symbol_map.insert(acc, {name, Symbol<E>(name)});
|
||||
return (Symbol<E> *)(&acc->second);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
inline std::ostream &operator<<(std::ostream &out, const Symbol<E> &sym) {
|
||||
out << sym.name;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
Chunk<E>::Chunk(Context<E> &ctx, std::string_view segname,
|
||||
std::string_view sectname) {
|
||||
ctx.chunks.push_back(this);
|
||||
hdr.set_segname(segname);
|
||||
hdr.set_sectname(sectname);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
u64 UnwindRecord<E>::get_func_raddr(Context<E> &ctx) const {
|
||||
return subsec->raddr + offset;
|
||||
}
|
||||
|
||||
} // namespace mold::macho
|
||||
@@ -0,0 +1,621 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include "../archive-file.h"
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
template <typename E>
|
||||
std::ostream &operator<<(std::ostream &out, const InputFile<E> &file) {
|
||||
if (file.archive_name.empty())
|
||||
out << path_clean(file.mf->name);
|
||||
else
|
||||
out << path_clean(file.archive_name) << "(" << path_clean(file.mf->name) + ")";
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
ObjectFile<E> *
|
||||
ObjectFile<E>::create(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
std::string archive_name) {
|
||||
ObjectFile<E> *obj = new ObjectFile<E>;
|
||||
obj->mf = mf;
|
||||
obj->archive_name = archive_name;
|
||||
obj->is_alive = archive_name.empty();
|
||||
ctx.obj_pool.push_back(std::unique_ptr<ObjectFile<E>>(obj));
|
||||
return obj;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
void ObjectFile<E>::parse(Context<E> &ctx) {
|
||||
parse_sections(ctx);
|
||||
parse_symtab(ctx);
|
||||
split_subsections(ctx);
|
||||
parse_data_in_code(ctx);
|
||||
|
||||
for (std::unique_ptr<InputSection<E>> &isec : sections)
|
||||
if (isec)
|
||||
isec->parse_relocations(ctx);
|
||||
|
||||
if (unwind_sec)
|
||||
parse_compact_unwind(ctx, *unwind_sec);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ObjectFile<E>::parse_sections(Context<E> &ctx) {
|
||||
MachHeader &hdr = *(MachHeader *)this->mf->data;
|
||||
u8 *p = this->mf->data + sizeof(hdr);
|
||||
|
||||
// Read all but a symtab section
|
||||
for (i64 i = 0; i < hdr.ncmds; i++) {
|
||||
LoadCommand &lc = *(LoadCommand *)p;
|
||||
p += lc.cmdsize;
|
||||
if (lc.cmd != LC_SEGMENT_64)
|
||||
continue;
|
||||
|
||||
SegmentCommand &cmd = *(SegmentCommand *)&lc;
|
||||
MachSection *mach_sec = (MachSection *)((u8 *)&lc + sizeof(cmd));
|
||||
|
||||
for (MachSection &msec : std::span(mach_sec, mach_sec + cmd.nsects)) {
|
||||
sections.push_back(nullptr);
|
||||
|
||||
if (msec.match("__LD", "__compact_unwind")) {
|
||||
unwind_sec = &msec;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (msec.attr & S_ATTR_DEBUG)
|
||||
continue;
|
||||
|
||||
sections.back().reset(new InputSection<E>(ctx, *this, msec));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ObjectFile<E>::parse_symtab(Context<E> &ctx) {
|
||||
SymtabCommand *cmd = (SymtabCommand *)find_load_command(ctx, LC_SYMTAB);
|
||||
if (!cmd)
|
||||
return;
|
||||
|
||||
mach_syms = {(MachSym *)(this->mf->data + cmd->symoff), cmd->nsyms};
|
||||
this->syms.reserve(mach_syms.size());
|
||||
|
||||
i64 nlocal = 0;
|
||||
for (MachSym &msym : mach_syms)
|
||||
if (!msym.ext)
|
||||
nlocal++;
|
||||
local_syms.reserve(nlocal);
|
||||
|
||||
for (MachSym &msym : mach_syms) {
|
||||
std::string_view name = (char *)(this->mf->data + cmd->stroff + msym.stroff);
|
||||
|
||||
if (msym.ext) {
|
||||
this->syms.push_back(get_symbol(ctx, name));
|
||||
} else {
|
||||
local_syms.emplace_back(name);
|
||||
this->syms.push_back(&local_syms.back());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SplitRegion {
|
||||
u32 offset;
|
||||
u32 size;
|
||||
u32 symidx;
|
||||
bool is_alt_entry;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
struct SplitInfo {
|
||||
InputSection<E> *isec = nullptr;
|
||||
std::vector<SplitRegion> regions;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
static std::vector<SplitInfo<E>> split(Context<E> &ctx, ObjectFile<E> &file) {
|
||||
std::vector<SplitInfo<E>> vec;
|
||||
|
||||
for (std::unique_ptr<InputSection<E>> &isec : file.sections)
|
||||
vec.push_back({isec.get()});
|
||||
|
||||
for (i64 i = 0; i < file.mach_syms.size(); i++) {
|
||||
MachSym &msym = file.mach_syms[i];
|
||||
if (msym.type == N_SECT && file.sections[msym.sect - 1]) {
|
||||
SplitRegion r;
|
||||
r.offset = msym.value - file.sections[msym.sect - 1]->hdr.addr;
|
||||
r.symidx = i;
|
||||
r.is_alt_entry = (msym.desc & N_ALT_ENTRY);
|
||||
vec[msym.sect - 1].regions.push_back(r);
|
||||
}
|
||||
}
|
||||
|
||||
std::erase_if(vec, [](const SplitInfo<E> &info) { return !info.isec; });
|
||||
|
||||
sort(vec, [](const SplitInfo<E> &a, const SplitInfo<E> &b) {
|
||||
return a.isec->hdr.addr < b.isec->hdr.addr;
|
||||
});
|
||||
|
||||
for (SplitInfo<E> &info : vec) {
|
||||
std::vector<SplitRegion> &r = info.regions;
|
||||
|
||||
if (r.empty()) {
|
||||
r.push_back({0, (u32)info.isec->hdr.size, (u32)-1, false});
|
||||
continue;
|
||||
}
|
||||
|
||||
sort(r, [](const SplitRegion &a, const SplitRegion &b) {
|
||||
return a.offset < b.offset;
|
||||
});
|
||||
|
||||
if (r[0].offset > 0)
|
||||
r.insert(r.begin(), {0, r[0].offset, (u32)-1, false});
|
||||
|
||||
for (i64 i = 1; i < r.size(); i++)
|
||||
if (r[i - 1].offset == r[i].offset)
|
||||
r[i++].is_alt_entry = true;
|
||||
|
||||
i64 last = -1;
|
||||
for (i64 i = 0; i < r.size(); i++) {
|
||||
if (!r[i].is_alt_entry) {
|
||||
if (last != -1)
|
||||
r[last].size = r[i].offset - r[last].offset;
|
||||
last = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (last != -1)
|
||||
r[last].size = info.isec->hdr.size - r[last].offset;
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ObjectFile<E>::split_subsections(Context<E> &ctx) {
|
||||
sym_to_subsec.resize(mach_syms.size());
|
||||
|
||||
for (SplitInfo<E> &info : split(ctx, *this)) {
|
||||
InputSection<E> &isec = *info.isec;
|
||||
|
||||
for (SplitRegion &r : info.regions) {
|
||||
if (!r.is_alt_entry) {
|
||||
Subsection<E> *subsec = new Subsection<E>{
|
||||
.isec = isec,
|
||||
.input_offset = r.offset,
|
||||
.input_size = r.size,
|
||||
.input_addr = (u32)(isec.hdr.addr + r.offset),
|
||||
.p2align = (u8)isec.hdr.p2align,
|
||||
};
|
||||
subsections.push_back(std::unique_ptr<Subsection<E>>(subsec));
|
||||
}
|
||||
|
||||
if (r.symidx != -1)
|
||||
sym_to_subsec[r.symidx] = subsections.size() - 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (i64 i = 0; i < mach_syms.size(); i++)
|
||||
if (!mach_syms[i].ext)
|
||||
override_symbol(ctx, i);
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ObjectFile<E>::parse_data_in_code(Context<E> &ctx) {
|
||||
if (auto *cmd = (LinkEditDataCommand *)find_load_command(ctx, LC_DATA_IN_CODE))
|
||||
data_in_code_entries = {
|
||||
(DataInCodeEntry *)(this->mf->data + cmd->dataoff),
|
||||
cmd->datasize / sizeof(DataInCodeEntry)
|
||||
};
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
LoadCommand *ObjectFile<E>::find_load_command(Context<E> &ctx, u32 type) {
|
||||
MachHeader &hdr = *(MachHeader *)this->mf->data;
|
||||
u8 *p = this->mf->data + sizeof(hdr);
|
||||
|
||||
for (i64 i = 0; i < hdr.ncmds; i++) {
|
||||
LoadCommand &lc = *(LoadCommand *)p;
|
||||
if (lc.cmd == type)
|
||||
return &lc;
|
||||
p += lc.cmdsize;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
i64 ObjectFile<E>::find_subsection_idx(Context<E> &ctx, u32 addr) {
|
||||
auto it = std::upper_bound(
|
||||
subsections.begin(), subsections.end(), addr,
|
||||
[&](u32 addr, const std::unique_ptr<Subsection<E>> &subsec) {
|
||||
return addr < subsec->input_addr;
|
||||
});
|
||||
|
||||
if (it == subsections.begin())
|
||||
return -1;
|
||||
return it - subsections.begin() - 1;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
Subsection<E> *ObjectFile<E>::find_subsection(Context<E> &ctx, u32 addr) {
|
||||
i64 i = find_subsection_idx(ctx, addr);
|
||||
return (i == -1) ? nullptr : subsections[i].get();
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ObjectFile<E>::parse_compact_unwind(Context<E> &ctx, MachSection &hdr) {
|
||||
if (hdr.size % sizeof(CompactUnwindEntry))
|
||||
Fatal(ctx) << *this << ": invalid __compact_unwind section size";
|
||||
|
||||
i64 num_entries = hdr.size / sizeof(CompactUnwindEntry);
|
||||
unwind_records.reserve(num_entries);
|
||||
|
||||
CompactUnwindEntry *src = (CompactUnwindEntry *)(this->mf->data + hdr.offset);
|
||||
|
||||
// Read compact unwind entries
|
||||
for (i64 i = 0; i < num_entries; i++)
|
||||
unwind_records.emplace_back(src[i].code_len, src[i].encoding);
|
||||
|
||||
// Read relocations
|
||||
MachRel *mach_rels = (MachRel *)(this->mf->data + hdr.reloff);
|
||||
for (i64 i = 0; i < hdr.nreloc; i++) {
|
||||
MachRel &r = mach_rels[i];
|
||||
if (r.offset >= hdr.size)
|
||||
Fatal(ctx) << *this << ": relocation offset too large: " << i;
|
||||
|
||||
i64 idx = r.offset / sizeof(CompactUnwindEntry);
|
||||
UnwindRecord<E> &dst = unwind_records[idx];
|
||||
|
||||
auto error = [&]() {
|
||||
Fatal(ctx) << *this << ": __compact_unwind: unsupported relocation: " << i;
|
||||
};
|
||||
|
||||
switch (r.offset % sizeof(CompactUnwindEntry)) {
|
||||
case offsetof(CompactUnwindEntry, code_start): {
|
||||
if (r.is_pcrel || r.p2size != 3 || r.is_extern || r.type)
|
||||
error();
|
||||
|
||||
Subsection<E> *target = find_subsection(ctx, src[idx].code_start);
|
||||
if (!target)
|
||||
error();
|
||||
dst.subsec = target;
|
||||
dst.offset = src[idx].code_start - target->input_addr;
|
||||
break;
|
||||
}
|
||||
case offsetof(CompactUnwindEntry, personality):
|
||||
if (r.is_pcrel || r.p2size != 3 || !r.is_extern || r.type)
|
||||
error();
|
||||
dst.personality = this->syms[r.idx];
|
||||
break;
|
||||
case offsetof(CompactUnwindEntry, lsda): {
|
||||
if (r.is_pcrel || r.p2size != 3 || r.is_extern || r.type)
|
||||
error();
|
||||
|
||||
i32 addr = *(i32 *)((u8 *)this->mf->data + hdr.offset + r.offset);
|
||||
Subsection<E> *target = find_subsection(ctx, addr);
|
||||
if (!target)
|
||||
error();
|
||||
dst.lsda = target;
|
||||
dst.lsda_offset = addr - target->input_addr;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
Fatal(ctx) << *this << ": __compact_unwind: unsupported relocation: " << i;
|
||||
}
|
||||
}
|
||||
|
||||
for (i64 i = 0; i < num_entries; i++)
|
||||
if (!unwind_records[i].subsec)
|
||||
Fatal(ctx) << ": __compact_unwind: missing relocation at " << i;
|
||||
|
||||
// Sort unwind entries by offset
|
||||
sort(unwind_records, [](const UnwindRecord<E> &a, const UnwindRecord<E> &b) {
|
||||
return std::tuple(a.subsec->input_addr, a.offset) <
|
||||
std::tuple(b.subsec->input_addr, b.offset);
|
||||
});
|
||||
|
||||
// Associate unwind entries to subsections
|
||||
for (i64 i = 0; i < num_entries;) {
|
||||
Subsection<E> &subsec = *unwind_records[i].subsec;
|
||||
subsec.unwind_offset = i;
|
||||
|
||||
i64 j = i + 1;
|
||||
while (j < num_entries && unwind_records[j].subsec == &subsec)
|
||||
j++;
|
||||
subsec.nunwind = j - i;
|
||||
i = j;
|
||||
}
|
||||
}
|
||||
|
||||
// Symbols with higher priorities overwrites symbols with lower priorities.
|
||||
// Here is the list of priorities, from the highest to the lowest.
|
||||
//
|
||||
// 1. Strong defined symbol
|
||||
// 2. Weak defined symbol
|
||||
// 3. Strong defined symbol in a DSO
|
||||
// 4. Weak defined symbol in a DSO
|
||||
// 5. Strong or weak defined symbol in an archive
|
||||
// 6. Common symbol
|
||||
// 7. Unclaimed (nonexistent) symbol
|
||||
//
|
||||
// Ties are broken by file priority.
|
||||
template <typename E>
|
||||
static u64 get_rank(InputFile<E> *file, MachSym &msym, bool is_lazy) {
|
||||
if (msym.is_common())
|
||||
return (6 << 24) + file->priority;
|
||||
if (is_lazy)
|
||||
return (5 << 24) + file->priority;
|
||||
if (file->is_dylib)
|
||||
return (3 << 24) + file->priority;
|
||||
return (1 << 24) + file->priority;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static u64 get_rank(Symbol<E> &sym) {
|
||||
InputFile<E> *file = sym.file;
|
||||
if (!file)
|
||||
return 7 << 24;
|
||||
if (sym.is_common)
|
||||
return (6 << 24) + file->priority;
|
||||
if (!file->archive_name.empty())
|
||||
return (5 << 24) + file->priority;
|
||||
if (file->is_dylib)
|
||||
return (3 << 24) + file->priority;
|
||||
return (1 << 24) + file->priority;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ObjectFile<E>::override_symbol(Context<E> &ctx, i64 idx) {
|
||||
Symbol<E> &sym = *this->syms[idx];
|
||||
MachSym &msym = mach_syms[idx];
|
||||
|
||||
sym.file = this;
|
||||
sym.is_extern = msym.ext;
|
||||
sym.is_lazy = false;
|
||||
|
||||
switch (msym.type) {
|
||||
case N_UNDF:
|
||||
assert(msym.is_common());
|
||||
sym.subsec = nullptr;
|
||||
sym.value = msym.value;
|
||||
sym.is_common = true;
|
||||
break;
|
||||
case N_ABS:
|
||||
sym.subsec = nullptr;
|
||||
sym.value = msym.value;
|
||||
sym.is_common = false;
|
||||
break;
|
||||
case N_SECT:
|
||||
sym.subsec = subsections[sym_to_subsec[idx]].get();
|
||||
sym.value = msym.value - sym.subsec->input_addr;
|
||||
sym.is_common = false;
|
||||
break;
|
||||
default:
|
||||
Fatal(ctx) << sym << ": unknown symbol type: " << (u64)msym.type;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ObjectFile<E>::resolve_regular_symbols(Context<E> &ctx) {
|
||||
for (i64 i = 0; i < this->syms.size(); i++) {
|
||||
MachSym &msym = mach_syms[i];
|
||||
if (!msym.ext || msym.is_undef())
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *this->syms[i];
|
||||
std::scoped_lock lock(sym.mu);
|
||||
if (get_rank(this, msym, false) < get_rank(sym))
|
||||
override_symbol(ctx, i);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ObjectFile<E>::resolve_lazy_symbols(Context<E> &ctx) {
|
||||
for (i64 i = 0; i < this->syms.size(); i++) {
|
||||
MachSym &msym = mach_syms[i];
|
||||
if (!msym.ext || msym.is_undef() || msym.is_common())
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *this->syms[i];
|
||||
std::scoped_lock lock(sym.mu);
|
||||
|
||||
if (get_rank(this, msym, true) < get_rank(sym)) {
|
||||
sym.file = this;
|
||||
sym.subsec = nullptr;
|
||||
sym.value = 0;
|
||||
sym.is_extern = false;
|
||||
sym.is_lazy = true;
|
||||
sym.is_common = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
bool ObjectFile<E>::is_objc_object(Context<E> &ctx) {
|
||||
for (std::unique_ptr<InputSection<E>> &isec : sections)
|
||||
if (isec->hdr.match("__DATA", "__objc_catlist") ||
|
||||
isec->hdr.match("__TEXT", "__swift"))
|
||||
return true;
|
||||
|
||||
for (i64 i = 0; i < this->syms.size(); i++)
|
||||
if (!mach_syms[i].is_undef() && mach_syms[i].ext &&
|
||||
this->syms[i]->name.starts_with("_OBJC_CLASS_$_"))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
std::vector<ObjectFile<E> *> ObjectFile<E>::mark_live_objects(Context<E> &ctx) {
|
||||
std::vector<ObjectFile<E> *> vec;
|
||||
assert(this->is_alive);
|
||||
|
||||
for (i64 i = 0; i < this->syms.size(); i++) {
|
||||
MachSym &msym = mach_syms[i];
|
||||
if (!msym.ext)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *this->syms[i];
|
||||
std::scoped_lock lock(sym.mu);
|
||||
|
||||
if (msym.is_undef()) {
|
||||
if (sym.file && !sym.file->is_alive.exchange(true))
|
||||
vec.push_back((ObjectFile<E> *)sym.file);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (get_rank(this, msym, false) < get_rank(sym))
|
||||
override_symbol(ctx, i);
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ObjectFile<E>::convert_common_symbols(Context<E> &ctx) {
|
||||
for (i64 i = 0; i < this->syms.size(); i++) {
|
||||
Symbol<E> &sym = *this->syms[i];
|
||||
MachSym &msym = mach_syms[i];
|
||||
|
||||
if (sym.file == this && sym.is_common) {
|
||||
InputSection<E> *isec = get_common_sec(ctx);
|
||||
Subsection<E> *subsec = new Subsection<E>{
|
||||
.isec = *isec,
|
||||
.input_size = (u32)msym.value,
|
||||
.p2align = (u8)msym.p2align,
|
||||
};
|
||||
|
||||
subsections.push_back(std::unique_ptr<Subsection<E>>(subsec));
|
||||
|
||||
sym.subsec = subsec;
|
||||
sym.value = 0;
|
||||
sym.is_common = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void ObjectFile<E>::check_duplicate_symbols(Context<E> &ctx) {
|
||||
for (i64 i = 0; i < this->syms.size(); i++) {
|
||||
Symbol<E> *sym = this->syms[i];
|
||||
MachSym &msym = mach_syms[i];
|
||||
if (sym && !msym.is_undef() && !msym.is_common() && sym->file != this)
|
||||
Error(ctx) << "duplicate symbol: " << *this << ": " << *sym->file
|
||||
<< ": " << *sym;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
InputSection<E> *ObjectFile<E>::get_common_sec(Context<E> &ctx) {
|
||||
if (!common_sec) {
|
||||
MachSection *hdr = new MachSection;
|
||||
common_hdr.reset(hdr);
|
||||
|
||||
memset(hdr, 0, sizeof(*hdr));
|
||||
hdr->set_segname("__DATA");
|
||||
hdr->set_sectname("__common");
|
||||
hdr->type = S_ZEROFILL;
|
||||
|
||||
common_sec = new InputSection<E>(ctx, *this, *hdr);
|
||||
sections.push_back(std::unique_ptr<InputSection<E>>(common_sec));
|
||||
}
|
||||
return common_sec;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
DylibFile<E> *DylibFile<E>::create(Context<E> &ctx, MappedFile<Context<E>> *mf) {
|
||||
DylibFile<E> *dylib = new DylibFile<E>;
|
||||
dylib->mf = mf;
|
||||
ctx.dylib_pool.push_back(std::unique_ptr<DylibFile<E>>(dylib));
|
||||
return dylib;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
void DylibFile<E>::read_trie(Context<E> &ctx, u8 *start, i64 offset,
|
||||
const std::string &prefix) {
|
||||
u8 *buf = start + offset;
|
||||
|
||||
if (*buf) {
|
||||
read_uleb(buf); // size
|
||||
read_uleb(buf); // flags
|
||||
read_uleb(buf); // addr
|
||||
this->syms.push_back(get_symbol(ctx, prefix));
|
||||
} else {
|
||||
buf++;
|
||||
}
|
||||
|
||||
i64 nchild = *buf++;
|
||||
|
||||
for (i64 i = 0; i < nchild; i++) {
|
||||
std::string suffix((char *)buf);
|
||||
buf += suffix.size() + 1;
|
||||
i64 off = read_uleb(buf);
|
||||
read_trie(ctx, start, off, prefix + suffix);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void DylibFile<E>::parse_dylib(Context<E> &ctx) {
|
||||
MachHeader &hdr = *(MachHeader *)this->mf->data;
|
||||
u8 *p = this->mf->data + sizeof(hdr);
|
||||
|
||||
for (i64 i = 0; i < hdr.ncmds; i++) {
|
||||
LoadCommand &lc = *(LoadCommand *)p;
|
||||
|
||||
switch (lc.cmd) {
|
||||
case LC_ID_DYLIB: {
|
||||
DylibCommand &cmd = *(DylibCommand *)p;
|
||||
install_name = (char *)p + cmd.nameoff;
|
||||
break;
|
||||
}
|
||||
case LC_DYLD_INFO_ONLY: {
|
||||
DyldInfoCommand &cmd = *(DyldInfoCommand *)p;
|
||||
if (cmd.export_off)
|
||||
read_trie(ctx, this->mf->data + cmd.export_off);
|
||||
break;
|
||||
}
|
||||
case LC_DYLD_EXPORTS_TRIE: {
|
||||
LinkEditDataCommand &cmd = *(LinkEditDataCommand *)p;
|
||||
read_trie(ctx, this->mf->data + cmd.dataoff);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
p += lc.cmdsize;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void DylibFile<E>::parse(Context<E> &ctx) {
|
||||
switch (get_file_type(this->mf)) {
|
||||
case FileType::TAPI: {
|
||||
TextDylib tbd = parse_tbd(ctx, this->mf);
|
||||
for (std::string_view sym : tbd.exports)
|
||||
this->syms.push_back(get_symbol(ctx, sym));
|
||||
install_name = tbd.install_name;
|
||||
break;
|
||||
}
|
||||
case FileType::MACH_DYLIB:
|
||||
parse_dylib(ctx);
|
||||
break;
|
||||
default:
|
||||
Fatal(ctx) << this->mf->name << ": is not a dylib";
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
void DylibFile<E>::resolve_symbols(Context<E> &ctx) {
|
||||
for (Symbol<E> *sym : this->syms) {
|
||||
std::scoped_lock lock(sym->mu);
|
||||
if (sym->file && sym->file->priority < this->priority)
|
||||
continue;
|
||||
sym->file = this;
|
||||
sym->is_extern = true;
|
||||
}
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template class ObjectFile<E>; \
|
||||
template class DylibFile<E>; \
|
||||
template std::ostream &operator<<(std::ostream &, const InputFile<E> &)
|
||||
|
||||
INSTANTIATE(ARM64);
|
||||
INSTANTIATE(X86_64);
|
||||
|
||||
} // namespace mold::macho
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,120 @@
|
||||
// This file implements the same functionality as elf/output-file.cc.
|
||||
// We have two different implemetntations for ELF and Mach-O because
|
||||
// we implemented a special optimization for ELF or Linux.
|
||||
//
|
||||
// On Linux, it is faster to write to an existing file than creating a
|
||||
// new file and writing to it. So, we overwrite an existing executable
|
||||
// if exists.
|
||||
//
|
||||
// On macOS, it looks like the system does not assume that an executable
|
||||
// is mutated once it is created. Due to some code signing mechanism or
|
||||
// something, we always have to create a fresh file.
|
||||
|
||||
#include "mold.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
template <typename E>
|
||||
class MemoryMappedOutputFile : public OutputFile<E> {
|
||||
public:
|
||||
MemoryMappedOutputFile(Context<E> &ctx, std::string path, i64 filesize, i64 perm)
|
||||
: OutputFile<E>(path, filesize, true) {
|
||||
std::string tmp_path = filepath(path).parent_path() / ".mold-XXXXXX";
|
||||
output_tmpfile = (char *)save_string(ctx, tmp_path).data();
|
||||
|
||||
i64 fd = mkstemp(output_tmpfile);
|
||||
if (fd == -1)
|
||||
Fatal(ctx) << "cannot open " << output_tmpfile << ": " << errno_string();
|
||||
|
||||
if (ftruncate(fd, filesize))
|
||||
Fatal(ctx) << "ftruncate failed: " << errno_string();
|
||||
if (fchmod(fd, perm) == -1)
|
||||
Fatal(ctx) << "fchmod failed: " << errno_string();
|
||||
|
||||
this->buf = (u8 *)mmap(nullptr, filesize, PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED, fd, 0);
|
||||
if (this->buf == MAP_FAILED)
|
||||
Fatal(ctx) << path << ": mmap failed: " << errno_string();
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
void close(Context<E> &ctx) override {
|
||||
Timer t(ctx, "close_file");
|
||||
|
||||
munmap(this->buf, this->filesize);
|
||||
if (rename(output_tmpfile, this->path.c_str()) == -1)
|
||||
Fatal(ctx) << this->path << ": rename failed: " << errno_string();
|
||||
output_tmpfile = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
class MallocOutputFile : public OutputFile<E> {
|
||||
public:
|
||||
MallocOutputFile(Context<E> &ctx, std::string path, i64 filesize, i64 perm)
|
||||
: OutputFile<E>(path, filesize, false), perm(perm) {
|
||||
this->buf = (u8 *)mmap(NULL, filesize, PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||
if (this->buf == MAP_FAILED)
|
||||
Fatal(ctx) << "mmap failed: " << errno_string();
|
||||
}
|
||||
|
||||
void close(Context<E> &ctx) override {
|
||||
Timer t(ctx, "close_file");
|
||||
|
||||
if (this->path == "-") {
|
||||
fwrite(this->buf, this->filesize, 1, stdout);
|
||||
fclose(stdout);
|
||||
return;
|
||||
}
|
||||
|
||||
i64 fd = ::open(this->path.c_str(), O_RDWR | O_CREAT, perm);
|
||||
if (fd == -1)
|
||||
Fatal(ctx) << "cannot open " << this->path << ": " << errno_string();
|
||||
|
||||
FILE *fp = fdopen(fd, "w");
|
||||
fwrite(this->buf, this->filesize, 1, fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
private:
|
||||
i64 perm;
|
||||
};
|
||||
|
||||
template <typename E>
|
||||
std::unique_ptr<OutputFile<E>>
|
||||
OutputFile<E>::open(Context<E> &ctx, std::string path, i64 filesize, i64 perm) {
|
||||
Timer t(ctx, "open_file");
|
||||
|
||||
if (path.starts_with('/') && !ctx.arg.chroot.empty())
|
||||
path = ctx.arg.chroot + "/" + path_clean(path);
|
||||
|
||||
bool is_special = false;
|
||||
if (path == "-") {
|
||||
is_special = true;
|
||||
} else {
|
||||
struct stat st;
|
||||
if (stat(path.c_str(), &st) == 0 && (st.st_mode & S_IFMT) != S_IFREG)
|
||||
is_special = true;
|
||||
}
|
||||
|
||||
std::unique_ptr<OutputFile> file;
|
||||
if (is_special)
|
||||
file = std::make_unique<MallocOutputFile<E>>(ctx, path, filesize, perm);
|
||||
else
|
||||
file = std::make_unique<MemoryMappedOutputFile<E>>(ctx, path, filesize, perm);
|
||||
return file;
|
||||
}
|
||||
|
||||
#define INSTANTIATE(E) \
|
||||
template class OutputFile<E>
|
||||
|
||||
INSTANTIATE(ARM64);
|
||||
INSTANTIATE(X86_64);
|
||||
|
||||
} // namespace mold::macho
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include <optional>
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
static std::vector<YamlNode>
|
||||
get_vector(YamlNode &node, std::string_view key) {
|
||||
if (auto *map = std::get_if<std::map<std::string_view, YamlNode>>(&node.data))
|
||||
if (auto it = map->find(key); it != map->end())
|
||||
if (auto *vec = std::get_if<std::vector<YamlNode>>(&it->second.data))
|
||||
return *vec;
|
||||
return {};
|
||||
}
|
||||
|
||||
static std::optional<std::string_view>
|
||||
get_string(YamlNode &node, std::string_view key) {
|
||||
if (auto *map = std::get_if<std::map<std::string_view, YamlNode>>(&node.data))
|
||||
if (auto it = map->find(key); it != map->end())
|
||||
if (auto *str = std::get_if<std::string_view>(&it->second.data))
|
||||
return *str;
|
||||
return {};
|
||||
}
|
||||
|
||||
static bool contains(const std::vector<YamlNode> &vec, std::string_view key) {
|
||||
for (const YamlNode &mem : vec)
|
||||
if (const std::string_view *s = std::get_if<std::string_view>(&mem.data))
|
||||
if (*s == key)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static std::optional<TextDylib> to_tbd(YamlNode &node, std::string_view arch) {
|
||||
if (!contains(get_vector(node, "targets"), arch))
|
||||
return {};
|
||||
|
||||
TextDylib tbd;
|
||||
|
||||
for (YamlNode &mem : get_vector(node, "uuids"))
|
||||
if (auto target = get_string(mem, "target"))
|
||||
if (*target == arch)
|
||||
if (auto value = get_string(mem, "value"))
|
||||
tbd.uuid = *value;
|
||||
|
||||
if (auto val = get_string(node, "install-name"))
|
||||
tbd.install_name = *val;
|
||||
|
||||
if (auto val = get_string(node, "current-version"))
|
||||
tbd.current_version = *val;
|
||||
|
||||
for (YamlNode &mem : get_vector(node, "parent-umbrella"))
|
||||
if (contains(get_vector(mem, "targets"), arch))
|
||||
if (auto val = get_string(mem, "umbrella"))
|
||||
tbd.parent_umbrella = *val;
|
||||
|
||||
for (YamlNode &mem : get_vector(node, "reexported-libraries"))
|
||||
if (contains(get_vector(mem, "targets"), arch))
|
||||
for (YamlNode &mem : get_vector(mem, "libraries"))
|
||||
if (auto *lib = std::get_if<std::string_view>(&mem.data))
|
||||
tbd.reexported_libs.push_back(*lib);
|
||||
|
||||
for (std::string_view key : {"exports", "reexports"})
|
||||
for (YamlNode &mem : get_vector(node, key))
|
||||
if (contains(get_vector(mem, "targets"), arch))
|
||||
for (YamlNode &mem : get_vector(mem, "symbols"))
|
||||
if (auto *sym = std::get_if<std::string_view>(&mem.data))
|
||||
tbd.exports.push_back(*sym);
|
||||
|
||||
return tbd;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static TextDylib squash(Context<E> &ctx, std::span<TextDylib> tbds) {
|
||||
std::unordered_map<std::string_view, TextDylib> map;
|
||||
|
||||
TextDylib main = std::move(tbds[0]);
|
||||
for (TextDylib &tbd : tbds.subspan(1))
|
||||
map[tbd.install_name] = std::move(tbd);
|
||||
|
||||
std::vector<std::string_view> libs;
|
||||
|
||||
for (std::string_view lib : main.reexported_libs) {
|
||||
auto it = map.find(lib);
|
||||
if (it != map.end())
|
||||
append(main.exports, it->second.exports);
|
||||
else
|
||||
libs.push_back(lib);
|
||||
}
|
||||
|
||||
main.reexported_libs = std::move(libs);
|
||||
return main;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static TextDylib parse(Context<E> &ctx, MappedFile<Context<E>> *mf,
|
||||
std::string_view arch) {
|
||||
std::string_view contents = mf->get_contents();
|
||||
std::variant<std::vector<YamlNode>, YamlError> res = parse_yaml(contents);
|
||||
|
||||
if (YamlError *err = std::get_if<YamlError>(&res)) {
|
||||
i64 lineno = std::count(contents.begin(), contents.begin() + err->pos, '\n');
|
||||
Fatal(ctx) << mf->name << ":" << (lineno + 1)
|
||||
<< ": YAML parse error: " << err->msg;
|
||||
}
|
||||
|
||||
std::vector<YamlNode> &nodes = std::get<std::vector<YamlNode>>(res);
|
||||
if (nodes.empty())
|
||||
Fatal(ctx) << mf->name << ": malformed TBD file";
|
||||
|
||||
std::vector<TextDylib> vec;
|
||||
for (YamlNode &node : nodes)
|
||||
if (std::optional<TextDylib> dylib = to_tbd(node, arch))
|
||||
vec.push_back(*dylib);
|
||||
return squash(ctx, vec);
|
||||
}
|
||||
|
||||
template <>
|
||||
TextDylib parse_tbd(Context<ARM64> &ctx, MappedFile<Context<ARM64>> *mf) {
|
||||
return parse(ctx, mf, "arm64-macos");
|
||||
}
|
||||
|
||||
template <>
|
||||
TextDylib parse_tbd(Context<X86_64> &ctx, MappedFile<Context<X86_64>> *mf) {
|
||||
return parse(ctx, mf, "x86_64-macos");
|
||||
}
|
||||
|
||||
} // namespace mold::macho
|
||||
+360
@@ -0,0 +1,360 @@
|
||||
#include "mold.h"
|
||||
|
||||
#include <optional>
|
||||
|
||||
namespace mold::macho {
|
||||
|
||||
struct Token {
|
||||
enum { STRING = 1, INDENT, DEDENT, END };
|
||||
|
||||
u8 kind = 0;
|
||||
std::string_view str;
|
||||
};
|
||||
|
||||
class YamlParser {
|
||||
public:
|
||||
YamlParser(std::string_view input) : input(input) {}
|
||||
|
||||
std::variant<std::vector<YamlNode>, YamlError> parse();
|
||||
|
||||
private:
|
||||
std::optional<YamlError> tokenize();
|
||||
|
||||
void tokenize_bare_string(std::string_view &str);
|
||||
|
||||
std::optional<YamlError> tokenize_list(std::string_view &str);
|
||||
std::optional<YamlError> tokenize_string(std::string_view &str, char end);
|
||||
|
||||
std::variant<YamlNode, YamlError> parse_element(std::span<Token> &tok);
|
||||
std::variant<YamlNode, YamlError> parse_list(std::span<Token> &tok);
|
||||
std::variant<YamlNode, YamlError> parse_map(std::span<Token> &tok);
|
||||
std::variant<YamlNode, YamlError> parse_flow_element(std::span<Token> &tok);
|
||||
std::variant<YamlNode, YamlError> parse_flow_list(std::span<Token> &tok);
|
||||
|
||||
std::string_view input;
|
||||
std::vector<Token> tokens;
|
||||
};
|
||||
|
||||
std::optional<YamlError> YamlParser::tokenize() {
|
||||
std::vector<i64> indents = {0};
|
||||
|
||||
auto indent = [&](std::string_view str, i64 depth) {
|
||||
tokens.push_back({Token::INDENT, str});
|
||||
indents.push_back(depth);
|
||||
};
|
||||
|
||||
auto dedent = [&](std::string_view str) {
|
||||
assert(indents.size() > 1);
|
||||
tokens.push_back({Token::DEDENT, str});
|
||||
indents.pop_back();
|
||||
};
|
||||
|
||||
auto skip_line = [](std::string_view &str) {
|
||||
size_t pos = str.find('\n');
|
||||
if (pos == str.npos)
|
||||
str = str.substr(str.size());
|
||||
else
|
||||
str = str.substr(pos + 1);
|
||||
};
|
||||
|
||||
auto tokenize_line = [&](std::string_view &str) -> std::optional<YamlError> {
|
||||
const char *start = str.data();
|
||||
|
||||
if (str.starts_with("---")) {
|
||||
while (indents.size() > 1)
|
||||
dedent(str);
|
||||
tokens.push_back({Token::END, str.substr(0, 3)});
|
||||
skip_line(str);
|
||||
return {};
|
||||
}
|
||||
|
||||
if (str.starts_with("...")) {
|
||||
while (indents.size() > 1)
|
||||
dedent(str);
|
||||
tokens.push_back({Token::END, str.substr(0, 3)});
|
||||
str = str.substr(str.size());
|
||||
return {};
|
||||
}
|
||||
|
||||
size_t pos = str.find_first_not_of(" \t");
|
||||
if (pos == str.npos || str[pos] == '#' || str[pos] == '\n') {
|
||||
skip_line(str);
|
||||
return {};
|
||||
}
|
||||
|
||||
if (indents.back() != pos) {
|
||||
if (indents.back() < pos) {
|
||||
indent(str, pos);
|
||||
} else {
|
||||
while (indents.back() != pos) {
|
||||
if (pos < indents.back())
|
||||
dedent(str);
|
||||
else
|
||||
return YamlError{"bad indentation", start - input.data()};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
str = str.substr(pos);
|
||||
|
||||
while (!str.empty()) {
|
||||
if (str[0] == '\n') {
|
||||
str = str.substr(1);
|
||||
return {};
|
||||
}
|
||||
|
||||
if (str.starts_with("- ")) {
|
||||
tokens.push_back({'-', str.substr(0, 1)});
|
||||
|
||||
size_t pos = str.find_first_not_of(" \t", 1);
|
||||
if (pos == str.npos || str[pos] == '\n') {
|
||||
skip_line(str);
|
||||
return {};
|
||||
}
|
||||
|
||||
str = str.substr(pos);
|
||||
indent(str, str.data() - start);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (str.starts_with('['))
|
||||
return tokenize_list(str);
|
||||
|
||||
if (str.starts_with('\'')) {
|
||||
if (std::optional<YamlError> err = tokenize_string(str, '\''))
|
||||
return err;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (str.starts_with('"')) {
|
||||
if (std::optional<YamlError> err = tokenize_string(str, '"'))
|
||||
return err;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (str.starts_with('#')) {
|
||||
skip_line(str);
|
||||
return {};
|
||||
}
|
||||
|
||||
if (str.starts_with(':')) {
|
||||
tokens.push_back({':', str.substr(0, 1)});
|
||||
|
||||
size_t pos = str.find_first_not_of(" \t", 1);
|
||||
if (pos == str.npos || str[pos] == '\n') {
|
||||
skip_line(str);
|
||||
return {};
|
||||
}
|
||||
|
||||
str = str.substr(pos);
|
||||
continue;
|
||||
}
|
||||
|
||||
tokenize_bare_string(str);
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
std::string_view str = input;
|
||||
while (!str.empty())
|
||||
if (std::optional<YamlError> err = tokenize_line(str))
|
||||
return err;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<YamlError> YamlParser::tokenize_list(std::string_view &str) {
|
||||
const char *start = str.data();
|
||||
|
||||
tokens.push_back({'[', str.substr(0, 1)});
|
||||
str = str.substr(1);
|
||||
|
||||
while (!str.empty() && str[0] != ']') {
|
||||
if (size_t pos = str.find_first_not_of(" \t\n"); pos) {
|
||||
str = str.substr(pos);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (str.starts_with('\'')) {
|
||||
if (std::optional<YamlError> err = tokenize_string(str, '\''))
|
||||
return err;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (str.starts_with('"')) {
|
||||
if (std::optional<YamlError> err = tokenize_string(str, '"'))
|
||||
return err;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (str.starts_with(',')) {
|
||||
tokens.push_back({',', str.substr(0, 1)});
|
||||
str = str.substr(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
tokenize_bare_string(str);
|
||||
}
|
||||
|
||||
if (str.empty())
|
||||
return YamlError{"unclosed list", start - input.data()};
|
||||
|
||||
const char *bracket = str.data();
|
||||
tokens.push_back({']', str.substr(0, 1)});
|
||||
str = str.substr(1);
|
||||
|
||||
while (!str.empty() && (str[0] == ' ' || str[0] == '\t'))
|
||||
str = str.substr(1);
|
||||
if (str.empty() || str[0] != '\n')
|
||||
return YamlError{"no newline after ']'", bracket - input.data()};
|
||||
str = str.substr(1);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<YamlError>
|
||||
YamlParser::tokenize_string(std::string_view &str, char end) {
|
||||
const char *start = str.data();
|
||||
size_t pos = str.find(end, 1);
|
||||
if (pos == str.npos)
|
||||
return YamlError{"unterminated string literal", start - input.data()};
|
||||
|
||||
tokens.push_back({Token::STRING, str.substr(1, pos - 1)});
|
||||
str = str.substr(pos + 1);
|
||||
return {};
|
||||
}
|
||||
|
||||
void
|
||||
YamlParser::tokenize_bare_string(std::string_view &str) {
|
||||
size_t pos = str.find_first_not_of(
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-/.");
|
||||
if (pos == str.npos)
|
||||
pos = str.size();
|
||||
tokens.push_back({Token::STRING, str.substr(0, pos)});
|
||||
str = str.substr(pos);
|
||||
}
|
||||
|
||||
std::variant<std::vector<YamlNode>, YamlError> YamlParser::parse() {
|
||||
if (std::optional<YamlError> err = tokenize())
|
||||
return *err;
|
||||
|
||||
std::span<Token> tok(tokens);
|
||||
std::vector<YamlNode> vec;
|
||||
|
||||
while (!tok.empty()) {
|
||||
if (tok[0].kind == Token::END) {
|
||||
tok = tok.subspan(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
std::variant<YamlNode, YamlError> elem = parse_element(tok);
|
||||
if (YamlError *err = std::get_if<YamlError>(&elem))
|
||||
return *err;
|
||||
vec.push_back(std::get<YamlNode>(elem));
|
||||
|
||||
if (tok[0].kind != Token::END)
|
||||
return YamlError{"stray token", tok[0].str.data() - input.data()};
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
std::variant<YamlNode, YamlError>
|
||||
YamlParser::parse_element(std::span<Token> &tok) {
|
||||
if (tok[0].kind == Token::INDENT) {
|
||||
tok = tok.subspan(1);
|
||||
|
||||
std::variant<YamlNode, YamlError> elem = parse_element(tok);
|
||||
assert(tok[0].kind == Token::DEDENT);
|
||||
tok = tok.subspan(1);
|
||||
return elem;
|
||||
}
|
||||
|
||||
if (tok[0].kind == '-')
|
||||
return parse_list(tok);
|
||||
|
||||
if (tok.size() > 2 && tok[0].kind == Token::STRING && tok[1].kind == ':')
|
||||
return parse_map(tok);
|
||||
|
||||
return parse_flow_element(tok);
|
||||
}
|
||||
|
||||
std::variant<YamlNode, YamlError>
|
||||
YamlParser::parse_list(std::span<Token> &tok) {
|
||||
std::vector<YamlNode> vec;
|
||||
|
||||
while (tok[0].kind != Token::END && tok[0].kind != Token::DEDENT) {
|
||||
if (tok[0].kind != '-')
|
||||
return YamlError{"list element expected", tok[0].str.data() - input.data()};
|
||||
tok = tok.subspan(1);
|
||||
|
||||
std::variant<YamlNode, YamlError> elem = parse_element(tok);
|
||||
if (YamlError *err = std::get_if<YamlError>(&elem))
|
||||
return *err;
|
||||
vec.push_back(std::get<YamlNode>(elem));
|
||||
}
|
||||
return YamlNode{vec};
|
||||
}
|
||||
|
||||
std::variant<YamlNode, YamlError>
|
||||
YamlParser::parse_map(std::span<Token> &tok) {
|
||||
std::map<std::string_view, YamlNode> map;
|
||||
|
||||
while (tok[0].kind != Token::END && tok[0].kind != Token::DEDENT) {
|
||||
if (tok.size() < 2 || tok[0].kind != Token::STRING || tok[1].kind != ':')
|
||||
return YamlError{"map key expected", tok[0].str.data() - input.data()};
|
||||
|
||||
std::string_view key = tok[0].str;
|
||||
tok = tok.subspan(2);
|
||||
|
||||
std::variant<YamlNode, YamlError> elem = parse_element(tok);
|
||||
if (YamlError *err = std::get_if<YamlError>(&elem))
|
||||
return *err;
|
||||
map[key] = std::get<YamlNode>(elem);
|
||||
}
|
||||
return YamlNode{map};
|
||||
}
|
||||
|
||||
std::variant<YamlNode, YamlError>
|
||||
YamlParser::parse_flow_element(std::span<Token> &tok) {
|
||||
if (tok[0].kind == '[') {
|
||||
tok = tok.subspan(1);
|
||||
return parse_flow_list(tok);
|
||||
}
|
||||
|
||||
if (tok[0].kind != Token::STRING)
|
||||
return YamlError{"scalar expected", tok[0].str.data() - input.data()};
|
||||
|
||||
std::string_view val = tok[0].str;
|
||||
tok = tok.subspan(1);
|
||||
return YamlNode{val};
|
||||
}
|
||||
|
||||
std::variant<YamlNode, YamlError>
|
||||
YamlParser::parse_flow_list(std::span<Token> &tok) {
|
||||
std::vector<YamlNode> vec;
|
||||
const char *start = tok[0].str.data();
|
||||
|
||||
while (tok[0].kind != ']' && tok[0].kind != Token::END) {
|
||||
std::variant<YamlNode, YamlError> elem = parse_flow_element(tok);
|
||||
if (YamlError *err = std::get_if<YamlError>(&elem))
|
||||
return *err;
|
||||
vec.push_back(std::get<YamlNode>(elem));
|
||||
|
||||
if (tok[0].kind == ']')
|
||||
break;
|
||||
if (tok[0].kind != ',')
|
||||
return YamlError{"comma expected", tok[0].str.data() - input.data()};
|
||||
tok = tok.subspan(1);
|
||||
}
|
||||
|
||||
if (tok[0].kind == Token::END)
|
||||
return YamlError{"unterminated flow list", start - input.data()};
|
||||
|
||||
tok = tok.subspan(1);
|
||||
return YamlNode{vec};
|
||||
}
|
||||
|
||||
std::variant<std::vector<YamlNode>, YamlError>
|
||||
parse_yaml(std::string_view str) {
|
||||
return YamlParser(str).parse();
|
||||
}
|
||||
|
||||
} // namespace mold::macho
|
||||
@@ -0,0 +1,43 @@
|
||||
#include "elf/mold.h"
|
||||
#include "macho/mold.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <signal.h>
|
||||
|
||||
namespace mold {
|
||||
|
||||
#ifdef GIT_HASH
|
||||
const std::string mold_version =
|
||||
"mold " MOLD_VERSION " (" GIT_HASH "; compatible with GNU ld)";
|
||||
#else
|
||||
const std::string mold_version =
|
||||
"mold " MOLD_VERSION " (compatible with GNU ld)";
|
||||
#endif
|
||||
|
||||
void cleanup() {
|
||||
if (output_tmpfile)
|
||||
unlink(output_tmpfile);
|
||||
if (socket_tmpfile)
|
||||
unlink(socket_tmpfile);
|
||||
}
|
||||
|
||||
static void signal_handler(int) {
|
||||
cleanup();
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
void install_signal_handler() {
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
std::string cmd = mold::filepath(argv[0]).filename();
|
||||
|
||||
if (cmd == "ld64" || cmd == "ld64.mold")
|
||||
return mold::macho::main(argc, argv);
|
||||
|
||||
return mold::elf::main(argc, argv);
|
||||
}
|
||||
@@ -0,0 +1,684 @@
|
||||
#pragma once
|
||||
|
||||
#include "byteorder.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <span>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <tbb/concurrent_vector.h>
|
||||
#include <tbb/enumerable_thread_specific.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
#ifdef NDEBUG
|
||||
# define unreachable() __builtin_unreachable()
|
||||
#else
|
||||
# define unreachable() assert(0 && "unreachable")
|
||||
#endif
|
||||
|
||||
namespace mold {
|
||||
|
||||
using namespace std::literals::string_literals;
|
||||
using namespace std::literals::string_view_literals;
|
||||
|
||||
typedef uint8_t u8;
|
||||
typedef uint16_t u16;
|
||||
typedef uint32_t u32;
|
||||
typedef uint64_t u64;
|
||||
|
||||
typedef int8_t i8;
|
||||
typedef int16_t i16;
|
||||
typedef int32_t i32;
|
||||
typedef int64_t i64;
|
||||
|
||||
template <typename C> class OutputFile;
|
||||
|
||||
inline char *output_tmpfile;
|
||||
inline char *socket_tmpfile;
|
||||
inline thread_local bool opt_demangle;
|
||||
|
||||
extern const std::string mold_version;
|
||||
|
||||
std::string_view errno_string();
|
||||
void cleanup();
|
||||
void install_signal_handler();
|
||||
|
||||
//
|
||||
// Error output
|
||||
//
|
||||
|
||||
template <typename C>
|
||||
class SyncOut {
|
||||
public:
|
||||
SyncOut(C &ctx, std::ostream &out = std::cout) : out(out) {
|
||||
opt_demangle = ctx.arg.demangle;
|
||||
}
|
||||
|
||||
~SyncOut() {
|
||||
std::scoped_lock lock(mu);
|
||||
out << ss.str() << "\n";
|
||||
}
|
||||
|
||||
template <class T> SyncOut &operator<<(T &&val) {
|
||||
ss << std::forward<T>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
static inline std::mutex mu;
|
||||
|
||||
private:
|
||||
std::ostream &out;
|
||||
std::stringstream ss;
|
||||
};
|
||||
|
||||
template <typename C>
|
||||
class Fatal {
|
||||
public:
|
||||
Fatal(C &ctx) : out(ctx, std::cerr) {
|
||||
out << "mold: ";
|
||||
}
|
||||
|
||||
[[noreturn]] ~Fatal() {
|
||||
out.~SyncOut();
|
||||
cleanup();
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
template <class T> Fatal &operator<<(T &&val) {
|
||||
out << std::forward<T>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
SyncOut<C> out;
|
||||
};
|
||||
|
||||
template <typename C>
|
||||
class Error {
|
||||
public:
|
||||
Error(C &ctx) : out(ctx, std::cerr) {
|
||||
if (ctx.arg.color_diagnostics)
|
||||
out << "mold: \033[0;1;31merror:\033[0m ";
|
||||
else
|
||||
out << "mold: error: ";
|
||||
|
||||
ctx.has_error = true;
|
||||
}
|
||||
|
||||
template <class T> Error &operator<<(T &&val) {
|
||||
out << std::forward<T>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
SyncOut<C> out;
|
||||
};
|
||||
|
||||
template <typename C>
|
||||
class Warn {
|
||||
public:
|
||||
Warn(C &ctx) : out(ctx, std::cerr) {
|
||||
if (ctx.arg.fatal_warnings) {
|
||||
if (ctx.arg.color_diagnostics)
|
||||
out << "mold: \033[0;1;31merror:\033[0m ";
|
||||
else
|
||||
out << "mold: error: ";
|
||||
ctx.has_error = true;
|
||||
} else {
|
||||
if (ctx.arg.color_diagnostics)
|
||||
out << "mold: \033[0;1;35mwarning:\033[0m ";
|
||||
else
|
||||
out << "mold: warning: ";
|
||||
}
|
||||
}
|
||||
|
||||
template <class T> Warn &operator<<(T &&val) {
|
||||
out << std::forward<T>(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
SyncOut<C> out;
|
||||
};
|
||||
|
||||
//
|
||||
// Utility functions
|
||||
//
|
||||
|
||||
inline u64 align_to(u64 val, u64 align) {
|
||||
if (align == 0)
|
||||
return val;
|
||||
assert(__builtin_popcount(align) == 1);
|
||||
return (val + align - 1) & ~(align - 1);
|
||||
}
|
||||
|
||||
inline u64 align_down(u64 val, u64 align) {
|
||||
assert(__builtin_popcount(align) == 1);
|
||||
return val & ~(align - 1);
|
||||
}
|
||||
|
||||
inline u64 next_power_of_two(u64 val) {
|
||||
assert(val >> 63 == 0);
|
||||
if (val == 0 || val == 1)
|
||||
return 1;
|
||||
return (u64)1 << (64 - __builtin_clzl(val - 1));
|
||||
}
|
||||
|
||||
template <typename T, typename Compare = std::less<T>>
|
||||
void update_minimum(std::atomic<T> &atomic, u64 new_val,
|
||||
Compare cmp = {}) {
|
||||
T old_val = atomic;
|
||||
while (cmp(new_val, old_val) &&
|
||||
!atomic.compare_exchange_weak(old_val, new_val));
|
||||
}
|
||||
|
||||
template <typename T, typename Compare = std::less<T>>
|
||||
void update_maximum(std::atomic<T> &atomic, u64 new_val,
|
||||
Compare cmp = {}) {
|
||||
T old_val = atomic;
|
||||
while (cmp(old_val, new_val) &&
|
||||
!atomic.compare_exchange_weak(old_val, new_val));
|
||||
}
|
||||
|
||||
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 std::vector<T> flatten(std::vector<std::vector<T>> &vec) {
|
||||
std::vector<T> ret;
|
||||
for (std::vector<T> &v : vec)
|
||||
append(ret, v);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void sort(T &vec) {
|
||||
std::stable_sort(vec.begin(), vec.end());
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
inline void sort(T &vec, U less) {
|
||||
std::stable_sort(vec.begin(), vec.end(), less);
|
||||
}
|
||||
|
||||
inline i64 write_string(u8 *buf, std::string_view str) {
|
||||
memcpy(buf, str.data(), str.size());
|
||||
buf[str.size()] = '\0';
|
||||
return str.size() + 1;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline i64 write_vector(u8 *buf, const std::vector<T> &vec) {
|
||||
i64 sz = vec.size() * sizeof(T);
|
||||
memcpy(buf, vec.data(), sz);
|
||||
return sz;
|
||||
}
|
||||
|
||||
inline void encode_uleb(std::vector<u8> &vec, u64 val) {
|
||||
do {
|
||||
u8 byte = val & 0x7f;
|
||||
val >>= 7;
|
||||
vec.push_back(val ? (byte | 0x80) : byte);
|
||||
} while (val);
|
||||
}
|
||||
|
||||
inline i64 write_uleb(u8 *buf, u64 val) {
|
||||
i64 i = 0;
|
||||
do {
|
||||
u8 byte = val & 0x7f;
|
||||
val >>= 7;
|
||||
buf[i++] = val ? (byte | 0x80) : byte;
|
||||
} while (val);
|
||||
return i;
|
||||
}
|
||||
|
||||
inline u64 read_uleb(u8 *&buf) {
|
||||
u64 val = 0;
|
||||
u8 shift = 0;
|
||||
u8 byte;
|
||||
do {
|
||||
byte = *buf++;
|
||||
val |= (byte & 0x7f) << shift;
|
||||
shift += 7;
|
||||
} while (byte & 0x80);
|
||||
return val;
|
||||
}
|
||||
|
||||
inline i64 uleb_size(u64 val) {
|
||||
i64 i = 0;
|
||||
do {
|
||||
i++;
|
||||
val >>= 7;
|
||||
} while (val);
|
||||
return i;
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
std::string_view save_string(C &ctx, const std::string &str) {
|
||||
u8 *buf = new u8[str.size() + 1];
|
||||
memcpy(buf, str.data(), str.size());
|
||||
buf[str.size()] = '\0';
|
||||
ctx.string_pool.push_back(std::unique_ptr<u8[]>(buf));
|
||||
return {(char *)buf, str.size()};
|
||||
}
|
||||
|
||||
//
|
||||
// Concurrent Map
|
||||
//
|
||||
|
||||
template <typename T>
|
||||
class ConcurrentMap {
|
||||
public:
|
||||
ConcurrentMap() {}
|
||||
|
||||
ConcurrentMap(i64 nbuckets) {
|
||||
resize(nbuckets);
|
||||
}
|
||||
|
||||
~ConcurrentMap() {
|
||||
if (keys) {
|
||||
free((void *)keys);
|
||||
free((void *)sizes);
|
||||
free((void *)values);
|
||||
}
|
||||
}
|
||||
|
||||
void resize(i64 nbuckets) {
|
||||
this->~ConcurrentMap();
|
||||
|
||||
nbuckets = std::max<i64>(MIN_NBUCKETS, next_power_of_two(nbuckets));
|
||||
|
||||
this->nbuckets = nbuckets;
|
||||
keys = (std::atomic<const char *> *)calloc(nbuckets, sizeof(keys[0]));
|
||||
sizes = (u32 *)calloc(nbuckets, sizeof(sizes[0]));
|
||||
values = (T *)calloc(nbuckets, sizeof(values[0]));
|
||||
}
|
||||
|
||||
std::pair<T *, bool> insert(std::string_view key, u64 hash, const T &val) {
|
||||
if (!keys)
|
||||
return {nullptr, false};
|
||||
|
||||
assert(__builtin_popcount(nbuckets) == 1);
|
||||
i64 idx = hash & (nbuckets - 1);
|
||||
i64 retry = 0;
|
||||
|
||||
while (retry < MAX_RETRY) {
|
||||
const char *ptr = keys[idx];
|
||||
if (ptr == locked) {
|
||||
#ifdef __x86_64__
|
||||
asm volatile("pause" ::: "memory");
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ptr == nullptr) {
|
||||
if (!keys[idx].compare_exchange_weak(ptr, locked))
|
||||
continue;
|
||||
new (values + idx) T(val);
|
||||
sizes[idx] = key.size();
|
||||
keys[idx] = key.data();
|
||||
return {values + idx, true};
|
||||
}
|
||||
|
||||
if (key.size() == sizes[idx] && memcmp(ptr, key.data(), sizes[idx]) == 0)
|
||||
return {values + idx, false};
|
||||
|
||||
u64 mask = nbuckets / NUM_SHARDS - 1;
|
||||
idx = (idx & ~mask) | ((idx + 1) & mask);
|
||||
retry++;
|
||||
}
|
||||
|
||||
assert(false && "ConcurrentMap is full");
|
||||
return {nullptr, false};
|
||||
}
|
||||
|
||||
bool has_key(i64 idx) {
|
||||
return keys[idx];
|
||||
}
|
||||
|
||||
static constexpr i64 MIN_NBUCKETS = 2048;
|
||||
static constexpr i64 NUM_SHARDS = 16;
|
||||
static constexpr i64 MAX_RETRY = 128;
|
||||
|
||||
i64 nbuckets = 0;
|
||||
std::atomic<const char *> *keys = nullptr;
|
||||
u32 *sizes = nullptr;
|
||||
T *values = nullptr;
|
||||
|
||||
private:
|
||||
static constexpr const char *locked = "marker";
|
||||
};
|
||||
|
||||
//
|
||||
// Bit vector
|
||||
//
|
||||
|
||||
class BitVector {
|
||||
class BitRef {
|
||||
public:
|
||||
BitRef(u8 &byte, u8 bitpos) : byte(byte), bitpos(bitpos) {}
|
||||
|
||||
BitRef &operator=(bool val) {
|
||||
if (val)
|
||||
byte |= (1 << bitpos);
|
||||
else
|
||||
byte &= ~(1 << bitpos);
|
||||
return *this;
|
||||
}
|
||||
|
||||
BitRef &operator=(const BitRef &other) {
|
||||
*this = (bool)other;
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator bool() const {
|
||||
return byte & (1 << bitpos);
|
||||
}
|
||||
|
||||
private:
|
||||
u8 &byte;
|
||||
u8 bitpos;
|
||||
};
|
||||
|
||||
public:
|
||||
void resize(i64 size) {
|
||||
vec.reset(new u8[(size + 7) / 8]);
|
||||
memset(vec.get(), 0, (size + 7) / 8);
|
||||
}
|
||||
|
||||
BitRef operator[](i64 i) {
|
||||
return BitRef(vec[i / 8], i % 8);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<u8[]> vec = nullptr;
|
||||
};
|
||||
|
||||
//
|
||||
// threads.cc
|
||||
//
|
||||
|
||||
void set_thread_count(i64 n);
|
||||
|
||||
//
|
||||
// hyperloglog.cc
|
||||
//
|
||||
|
||||
class HyperLogLog {
|
||||
public:
|
||||
HyperLogLog() : buckets(NBUCKETS) {}
|
||||
|
||||
void insert(u32 hash) {
|
||||
update_maximum(buckets[hash & (NBUCKETS - 1)], __builtin_clz(hash) + 1);
|
||||
}
|
||||
|
||||
i64 get_cardinality() const;
|
||||
|
||||
void merge(const HyperLogLog &other) {
|
||||
for (i64 i = 0; i < NBUCKETS; i++)
|
||||
update_maximum(buckets[i], other.buckets[i]);
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr i64 NBUCKETS = 2048;
|
||||
static constexpr double ALPHA = 0.79402;
|
||||
|
||||
std::vector<std::atomic_uint8_t> buckets;
|
||||
};
|
||||
|
||||
//
|
||||
// filepath.cc
|
||||
//
|
||||
|
||||
template <typename T>
|
||||
std::filesystem::path filepath(const T &path) {
|
||||
return {path, std::filesystem::path::format::generic_format};
|
||||
}
|
||||
|
||||
std::string get_realpath(std::string_view path);
|
||||
std::string path_clean(std::string_view path);
|
||||
std::filesystem::path to_abs_path(std::filesystem::path path);
|
||||
|
||||
//
|
||||
// demangle.cc
|
||||
//
|
||||
|
||||
std::string_view demangle(std::string_view name);
|
||||
|
||||
//
|
||||
// compress.cc
|
||||
//
|
||||
|
||||
class ZlibCompressor {
|
||||
public:
|
||||
ZlibCompressor(std::string_view input);
|
||||
void write_to(u8 *buf);
|
||||
i64 size() const;
|
||||
|
||||
private:
|
||||
std::vector<std::vector<u8>> shards;
|
||||
u64 checksum = 0;
|
||||
};
|
||||
|
||||
class GzipCompressor {
|
||||
public:
|
||||
GzipCompressor(std::string_view input);
|
||||
void write_to(u8 *buf);
|
||||
i64 size() const;
|
||||
|
||||
private:
|
||||
std::vector<std::vector<u8>> shards;
|
||||
u32 checksum = 0;
|
||||
u32 uncompressed_size = 0;
|
||||
};
|
||||
|
||||
//
|
||||
// perf.cc
|
||||
//
|
||||
|
||||
// Counter is used to collect statistics numbers.
|
||||
class Counter {
|
||||
public:
|
||||
Counter(std::string_view name, i64 value = 0) : name(name), values(value) {
|
||||
static std::mutex mu;
|
||||
std::scoped_lock lock(mu);
|
||||
instances.push_back(this);
|
||||
}
|
||||
|
||||
Counter &operator++(int) {
|
||||
if (enabled)
|
||||
values.local()++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Counter &operator+=(int delta) {
|
||||
if (enabled)
|
||||
values.local() += delta;
|
||||
return *this;
|
||||
}
|
||||
|
||||
static void print();
|
||||
|
||||
static inline bool enabled = false;
|
||||
|
||||
private:
|
||||
i64 get_value();
|
||||
|
||||
std::string_view name;
|
||||
tbb::enumerable_thread_specific<i64> values;
|
||||
|
||||
static inline std::vector<Counter *> instances;
|
||||
};
|
||||
|
||||
// Timer and TimeRecord records elapsed time (wall clock time)
|
||||
// used by each pass of the linker.
|
||||
struct TimerRecord {
|
||||
TimerRecord(std::string name, TimerRecord *parent = nullptr);
|
||||
void stop();
|
||||
|
||||
std::string name;
|
||||
TimerRecord *parent;
|
||||
tbb::concurrent_vector<TimerRecord *> children;
|
||||
i64 start;
|
||||
i64 end;
|
||||
i64 user;
|
||||
i64 sys;
|
||||
bool stopped = false;
|
||||
};
|
||||
|
||||
void
|
||||
print_timer_records(tbb::concurrent_vector<std::unique_ptr<TimerRecord>> &);
|
||||
|
||||
template <typename C>
|
||||
class Timer {
|
||||
public:
|
||||
Timer(C &ctx, std::string name, Timer *parent = nullptr) {
|
||||
record = new TimerRecord(name, parent ? parent->record : nullptr);
|
||||
ctx.timer_records.push_back(std::unique_ptr<TimerRecord>(record));
|
||||
}
|
||||
|
||||
Timer(const Timer &) = delete;
|
||||
|
||||
~Timer() {
|
||||
record->stop();
|
||||
}
|
||||
|
||||
void stop() {
|
||||
record->stop();
|
||||
}
|
||||
|
||||
private:
|
||||
TimerRecord *record;
|
||||
};
|
||||
|
||||
//
|
||||
// tar.cc
|
||||
//
|
||||
|
||||
// TarFile is a class to create a tar file.
|
||||
//
|
||||
// If you pass `--reproduce=repro.tar` to mold, mold collects all
|
||||
// input files and put them into `repro.tar`, so that it is easy to
|
||||
// run the same command with the same command line arguments.
|
||||
class TarFile {
|
||||
public:
|
||||
TarFile(std::string basedir) : basedir(basedir) {}
|
||||
void append(std::string path, std::string_view data);
|
||||
void write_to(u8 *buf);
|
||||
i64 size() const { return size_; }
|
||||
|
||||
private:
|
||||
static constexpr i64 BLOCK_SIZE = 512;
|
||||
|
||||
std::string encode_path(std::string path);
|
||||
|
||||
std::string basedir;
|
||||
std::vector<std::pair<std::string, std::string_view>> contents;
|
||||
i64 size_ = BLOCK_SIZE * 2;
|
||||
};
|
||||
|
||||
//
|
||||
// Memory-mapped file
|
||||
//
|
||||
|
||||
// MappedFile represents an mmap'ed input file.
|
||||
// mold uses mmap-IO only.
|
||||
template <typename C>
|
||||
class MappedFile {
|
||||
public:
|
||||
static MappedFile *open(C &ctx, std::string path);
|
||||
static MappedFile *must_open(C &ctx, std::string path);
|
||||
|
||||
~MappedFile();
|
||||
|
||||
MappedFile *slice(C &ctx, std::string name, u64 start, u64 size);
|
||||
|
||||
std::string_view get_contents() {
|
||||
return std::string_view((char *)data, size);
|
||||
}
|
||||
|
||||
std::string name;
|
||||
u8 *data = nullptr;
|
||||
i64 size = 0;
|
||||
i64 mtime = 0;
|
||||
bool given_fullpath = true;
|
||||
MappedFile *parent = nullptr;
|
||||
};
|
||||
|
||||
template <typename C>
|
||||
MappedFile<C> *MappedFile<C>::open(C &ctx, std::string path) {
|
||||
MappedFile *mf = new MappedFile;
|
||||
mf->name = path;
|
||||
|
||||
ctx.mf_pool.push_back(std::unique_ptr<MappedFile>(mf));
|
||||
|
||||
if (path.starts_with('/') && !ctx.arg.chroot.empty())
|
||||
path = ctx.arg.chroot + "/" + path_clean(path);
|
||||
|
||||
i64 fd = ::open(path.c_str(), O_RDONLY);
|
||||
if (fd == -1)
|
||||
return nullptr;
|
||||
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) == -1)
|
||||
Fatal(ctx) << path << ": fstat failed: " << errno_string();
|
||||
|
||||
mf->size = st.st_size;
|
||||
|
||||
#ifdef __APPLE__
|
||||
mf->mtime = (u64)st.st_mtimespec.tv_sec * 1000000000 + st.st_mtimespec.tv_nsec;
|
||||
#else
|
||||
mf->mtime = (u64)st.st_mtim.tv_sec * 1000000000 + st.st_mtim.tv_nsec;
|
||||
#endif
|
||||
|
||||
if (st.st_size > 0) {
|
||||
mf->data = (u8 *)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (mf->data == MAP_FAILED)
|
||||
Fatal(ctx) << path << ": mmap failed: " << errno_string();
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return mf;
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
MappedFile<C> *MappedFile<C>::must_open(C &ctx, std::string path) {
|
||||
if (MappedFile *mf = MappedFile::open(ctx, path))
|
||||
return mf;
|
||||
Fatal(ctx) << "cannot open " << path;
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
MappedFile<C> *
|
||||
MappedFile<C>::slice(C &ctx, std::string name, u64 start, u64 size) {
|
||||
MappedFile *mf = new MappedFile<C>;
|
||||
mf->name = name;
|
||||
mf->data = data + start;
|
||||
mf->size = size;
|
||||
mf->parent = this;
|
||||
|
||||
ctx.mf_pool.push_back(std::unique_ptr<MappedFile>(mf));
|
||||
return mf;
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
MappedFile<C>::~MappedFile() {
|
||||
if (size && !parent)
|
||||
munmap(data, size);
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
+25
-35
@@ -1,14 +1,10 @@
|
||||
#include "common.h"
|
||||
#include "mold.h"
|
||||
|
||||
#include <functional>
|
||||
#include <iomanip>
|
||||
#include <ios>
|
||||
#include <tbb/concurrent_vector.h>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/resource.h>
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
namespace mold {
|
||||
|
||||
@@ -17,7 +13,9 @@ i64 Counter::get_value() {
|
||||
}
|
||||
|
||||
void Counter::print() {
|
||||
ranges::stable_sort(instances, {}, [](Counter *x) { return x->get_value(); });
|
||||
sort(instances, [](Counter *a, Counter *b) {
|
||||
return a->get_value() > b->get_value();
|
||||
});
|
||||
|
||||
for (Counter *c : instances)
|
||||
std::cout << std::setw(20) << std::right << c->name
|
||||
@@ -25,33 +23,24 @@ void Counter::print() {
|
||||
}
|
||||
|
||||
static i64 now_nsec() {
|
||||
return (i64)std::chrono::steady_clock::now().time_since_epoch().count();
|
||||
struct timespec t;
|
||||
clock_gettime(CLOCK_MONOTONIC, &t);
|
||||
return (i64)t.tv_sec * 1000000000 + t.tv_nsec;
|
||||
}
|
||||
|
||||
static std::pair<i64, i64> get_usage() {
|
||||
#ifdef _WIN32
|
||||
auto to_nsec = [](FILETIME t) -> i64 {
|
||||
return (((u64)t.dwHighDateTime << 32) + (u64)t.dwLowDateTime) * 100;
|
||||
};
|
||||
|
||||
FILETIME creation, exit, kernel, user;
|
||||
GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user);
|
||||
return {to_nsec(user), to_nsec(kernel)};
|
||||
#else
|
||||
auto to_nsec = [](struct timeval t) -> i64 {
|
||||
return (i64)t.tv_sec * 1'000'000'000 + t.tv_usec * 1'000;
|
||||
};
|
||||
|
||||
struct rusage ru;
|
||||
getrusage(RUSAGE_SELF, &ru);
|
||||
return {to_nsec(ru.ru_utime), to_nsec(ru.ru_stime)};
|
||||
#endif
|
||||
static i64 to_nsec(struct timeval t) {
|
||||
return (i64)t.tv_sec * 1000000000 + t.tv_usec * 1000;
|
||||
}
|
||||
|
||||
TimerRecord::TimerRecord(std::string name, TimerRecord *parent)
|
||||
: name(name), parent(parent) {
|
||||
struct rusage usage;
|
||||
getrusage(RUSAGE_SELF, &usage);
|
||||
|
||||
start = now_nsec();
|
||||
std::tie(user, sys) = get_usage();
|
||||
user = to_nsec(usage.ru_utime);
|
||||
sys = to_nsec(usage.ru_stime);
|
||||
|
||||
if (parent)
|
||||
parent->children.push_back(this);
|
||||
}
|
||||
@@ -61,24 +50,25 @@ void TimerRecord::stop() {
|
||||
return;
|
||||
stopped = true;
|
||||
|
||||
i64 user2;
|
||||
i64 sys2;
|
||||
std::tie(user2, sys2) = get_usage();
|
||||
struct rusage usage;
|
||||
getrusage(RUSAGE_SELF, &usage);
|
||||
|
||||
end = now_nsec();
|
||||
user = user2 - user;
|
||||
sys = sys2 - sys;
|
||||
user = to_nsec(usage.ru_utime) - user;
|
||||
sys = to_nsec(usage.ru_stime) - sys;
|
||||
}
|
||||
|
||||
static void print_rec(TimerRecord &rec, i64 indent) {
|
||||
printf(" % 8.3f % 8.3f % 8.3f %s%s\n",
|
||||
((double)rec.user / 1'000'000'000),
|
||||
((double)rec.sys / 1'000'000'000),
|
||||
(((double)rec.end - rec.start) / 1'000'000'000),
|
||||
((double)rec.user / 1000000000),
|
||||
((double)rec.sys / 1000000000),
|
||||
(((double)rec.end - rec.start) / 1000000000),
|
||||
std::string(indent * 2, ' ').c_str(),
|
||||
rec.name.c_str());
|
||||
|
||||
ranges::stable_sort(rec.children, {}, &TimerRecord::start);
|
||||
sort(rec.children, [](TimerRecord *a, TimerRecord *b) {
|
||||
return a->start < b->start;
|
||||
});
|
||||
|
||||
for (TimerRecord *child : rec.children)
|
||||
print_rec(*child, indent + 1);
|
||||
-285
@@ -1,285 +0,0 @@
|
||||
// r31 is blink. r30 is I believe a scratch register.
|
||||
//
|
||||
// Programs might use one of several branch, jump, and link instructions
|
||||
// to control execution flow through direct and indirect function calls
|
||||
// and branching. For function calling, the conditional branch-and-link
|
||||
// instruction has a maximum branch range of ±1 MiB, and the target
|
||||
// address is 32-bit-aligned. The unconditional branch-and-link format has
|
||||
// a maximum branch range of ±16 MiB.
|
||||
//
|
||||
// https://github.com/foss-for-synopsys-dwc-arc-processors/arc-ABI-manual/blob/master/ARCv2_ABI.pdf
|
||||
|
||||
#include "mold.h"
|
||||
|
||||
namespace mold {
|
||||
|
||||
using E = ARC;
|
||||
|
||||
// An integer-like class for the mixed-endian encoding
|
||||
class M32 {
|
||||
public:
|
||||
operator u32() const {
|
||||
return (buf[1] << 24) | (buf[0] << 16) | (buf[3] << 8) | buf[2];
|
||||
}
|
||||
|
||||
M32 &operator=(u32 x) {
|
||||
buf[0] = x >> 16;
|
||||
buf[1] = x >> 24;
|
||||
buf[2] = x;
|
||||
buf[3] = x >> 8;
|
||||
return *this;
|
||||
}
|
||||
|
||||
M32 &operator&=(u32 x) { return *this = *this & x; }
|
||||
M32 &operator|=(u32 x) { return *this = *this | x; }
|
||||
u8 buf[4];
|
||||
};
|
||||
|
||||
static void write_disp7u(u8 *loc, u32 val) {
|
||||
*(ul16 *)loc |= (bits(val, 6, 3) << 4) | bits(val, 2, 0);
|
||||
}
|
||||
|
||||
static void write_disp9(u8 *loc, u32 val) {
|
||||
*(M32 *)loc |= bits(val, 8, 0);
|
||||
}
|
||||
|
||||
static void write_disp9ls(u8 *loc, u32 val) {
|
||||
*(M32 *)loc |= (bits(val, 7, 0) << 16) | (bit(val, 8) << 15);
|
||||
}
|
||||
|
||||
static void write_disp9s(u8 *loc, u32 val) {
|
||||
*(ul16 *)loc |= bits(val, 8, 0);
|
||||
}
|
||||
|
||||
static void write_disp10u(u8 *loc, u32 val) {
|
||||
*(ul16 *)loc |= bits(val, 9, 0);
|
||||
}
|
||||
|
||||
static void write_disp13s(u8 *loc, u32 val) {
|
||||
*(ul16 *)loc |= bits(val, 12, 0);
|
||||
}
|
||||
|
||||
static void write_disp21h(u8 *loc, u32 val) {
|
||||
*(M32 *)loc |= (bits(val, 10, 1) << 17) | (bits(val, 20, 11) << 6);
|
||||
}
|
||||
|
||||
static void write_disp21w(u8 *loc, u32 val) {
|
||||
*(M32 *)loc |= (bits(val, 10, 2) << 18) | (bits(val, 20, 11) << 6);
|
||||
}
|
||||
|
||||
static void write_disp25h(u8 *loc, u32 val) {
|
||||
*(M32 *)loc |= (bits(val, 10, 1) << 17) | (bits(val, 20, 11) << 6) |
|
||||
bits(val, 24, 21);
|
||||
}
|
||||
|
||||
static void write_disp25w(u8 *loc, u32 val) {
|
||||
*(M32 *)loc |= (bits(val, 10, 2) << 18) | (bits(val, 20, 11) << 6) |
|
||||
bits(val, 24, 21);
|
||||
}
|
||||
|
||||
static void write_disps9(u8 *loc, u32 val) {
|
||||
*(ul16 *)loc |= bits(val, 10, 2);
|
||||
}
|
||||
|
||||
static void write_disps12(u8 *loc, u32 val) {
|
||||
*(M32 *)loc |= (bits(val, 5, 0) << 6) | bits(val, 11, 6);
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_plt_header(Context<E> &ctx, u8 *buf) {
|
||||
static const ul16 insn[] = {
|
||||
0x2730, 0x7f8b, 0, 0, // ld r11, [pcl,0]
|
||||
0x2730, 0x7f8a, 0, 0, // ld r10, [pcl,0]
|
||||
0x2020, 0x0280, // j [r10]
|
||||
0, 0, // (address of GOTPLT)
|
||||
};
|
||||
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
*(M32 *)(buf + 4) = ctx.gotplt->shdr.sh_addr - ctx.plt->shdr.sh_addr + 4;
|
||||
*(M32 *)(buf + 12) = ctx.gotplt->shdr.sh_addr - ctx.plt->shdr.sh_addr;
|
||||
*(M32 *)(buf + 20) = ctx.gotplt->shdr.sh_addr;
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_plt_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
|
||||
static const ul16 insn[] = {
|
||||
0x2730, 0x7f8c, 0, 0, // ld r12, [pcl,0]
|
||||
0x2021, 0x0300, // j.d [r12]
|
||||
0x240a, 0x1fc0, // mov r12,pcl
|
||||
};
|
||||
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
*(M32 *)(buf + 4) = sym.get_gotplt_addr(ctx) - sym.get_plt_addr(ctx);
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_pltgot_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
|
||||
static const ul16 insn[] = {
|
||||
0x2730, 0x7f8c, 0, 0, // ld r12, [pcl,0]
|
||||
0x2021, 0x0300, // j.d [r12]
|
||||
0x240a, 0x1fc0, // mov r12,pcl
|
||||
};
|
||||
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
*(M32 *)(buf + 4) = sym.get_got_pltgot_addr(ctx) - sym.get_plt_addr(ctx);
|
||||
}
|
||||
|
||||
template <>
|
||||
void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
|
||||
u64 offset, u64 val) {
|
||||
u8 *loc = ctx.buf + this->shdr.sh_offset + offset;
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_NONE:
|
||||
break;
|
||||
case R_ARC_32:
|
||||
*(ul32 *)loc = val;
|
||||
break;
|
||||
case R_ARC_32_PCREL:
|
||||
*(ul32 *)loc = val - this->shdr.sh_addr - offset;
|
||||
break;
|
||||
default:
|
||||
Fatal(ctx) << "unsupported relocation in .eh_frame: " << rel;
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
|
||||
std::span<const ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_NONE)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
u64 S = sym.get_addr(ctx);
|
||||
u64 A = rel.r_addend;
|
||||
u64 P = get_addr() + rel.r_offset;
|
||||
u64 G = sym.get_got_idx(ctx) * sizeof(Word<E>);
|
||||
u64 GOT = ctx.got->shdr.sh_addr;
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_ARC_32:
|
||||
*(ul32 *)loc = S + A;
|
||||
break;
|
||||
case R_ARC_32_ME:
|
||||
*(M32 *)loc = S + A;
|
||||
break;
|
||||
case R_ARC_S25H_PCREL:
|
||||
case R_ARC_S25H_PCREL_PLT:
|
||||
write_disp25h(loc, align_to(S + A - P, 2));
|
||||
break;
|
||||
case R_ARC_S25W_PCREL:
|
||||
case R_ARC_S25W_PCREL_PLT:
|
||||
write_disp25w(loc, align_to(S + A - P, 4));
|
||||
break;
|
||||
case R_ARC_PC32:
|
||||
*(M32 *)loc = S + A - P;
|
||||
break;
|
||||
case R_ARC_32_PCREL:
|
||||
*(ul32 *)loc = S + A - P;
|
||||
break;
|
||||
case R_ARC_GOTPC32:
|
||||
*(M32 *)loc = GOT + G + A - P;
|
||||
break;
|
||||
case R_ARC_TLS_LE_32:
|
||||
*(M32 *)loc = S + A - ctx.tp_addr;
|
||||
break;
|
||||
case R_ARC_TLS_IE_GOT:
|
||||
*(M32 *)loc = sym.get_gottp_addr(ctx) + A - P;
|
||||
break;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
|
||||
std::span<const ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_NONE || record_undef_error(ctx, rel))
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
SectionFragment<E> *frag;
|
||||
i64 frag_addend;
|
||||
std::tie(frag, frag_addend) = get_fragment(ctx, rel);
|
||||
|
||||
u64 S = frag ? frag->get_addr(ctx) : sym.get_addr(ctx);
|
||||
u64 A = frag ? frag_addend : (i64)rel.r_addend;
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_ARC_32:
|
||||
if (std::optional<u64> val = get_tombstone(sym, frag))
|
||||
*(ul32 *)loc = *val;
|
||||
else
|
||||
*(ul32 *)loc = S + A;
|
||||
break;
|
||||
default:
|
||||
Fatal(ctx) << *this << ": invalid relocation for non-allocated sections: "
|
||||
<< rel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::scan_relocations(Context<E> &ctx) {
|
||||
assert(shdr().sh_flags & SHF_ALLOC);
|
||||
std::span<const ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_NONE || record_undef_error(ctx, rel))
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
|
||||
if (sym.is_ifunc())
|
||||
Error(ctx) << sym << ": GNU ifunc symbol is not supported on m68k";
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_ARC_32:
|
||||
break;
|
||||
case R_ARC_32_ME:
|
||||
scan_absrel(ctx, sym, rel);
|
||||
break;
|
||||
case R_ARC_S25H_PCREL:
|
||||
case R_ARC_S25W_PCREL:
|
||||
case R_ARC_32_PCREL:
|
||||
case R_ARC_PC32:
|
||||
scan_pcrel(ctx, sym, rel);
|
||||
break;
|
||||
case R_ARC_S25H_PCREL_PLT:
|
||||
case R_ARC_S25W_PCREL_PLT:
|
||||
if (sym.is_imported)
|
||||
sym.flags |= NEEDS_PLT;
|
||||
break;
|
||||
case R_ARC_GOTPC32:
|
||||
sym.flags |= NEEDS_GOT;
|
||||
break;
|
||||
case R_ARC_TLS_LE_32:
|
||||
check_tlsle(ctx, sym, rel);
|
||||
break;
|
||||
case R_ARC_TLS_IE_GOT:
|
||||
sym.flags |= NEEDS_GOTTP;
|
||||
break;
|
||||
default:
|
||||
Error(ctx) << *this << ": unknown relocation: " << rel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
u64 get_eflags(Context<E> &ctx) {
|
||||
return 0x406;
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,849 +0,0 @@
|
||||
// ARM32 is a bit special from the linker's viewpoint because ARM
|
||||
// processors support two different instruction encodings: Thumb and
|
||||
// ARM (in a narrower sense). Thumb instructions are either 16 bits or
|
||||
// 32 bits, while ARM instructions are all 32 bits. Feature-wise,
|
||||
// Thumb is a subset of ARM, so not all ARM instructions are
|
||||
// representable in Thumb.
|
||||
//
|
||||
// ARM processors originally supported only ARM instructions. Thumb
|
||||
// instructions were later added to increase code density.
|
||||
//
|
||||
// ARM processors runs in either ARM mode or Thumb mode. The mode can
|
||||
// be switched using BX (branch and mode exchange)-family instructions.
|
||||
// We need to use that instructions to, for example, call a function
|
||||
// encoded in Thumb from a function encoded in ARM. Sometimes, the
|
||||
// linker even has to emit interworking thunk code to switch mode.
|
||||
//
|
||||
// ARM instructions are aligned to 4 byte boundaries. Thumb are to 2
|
||||
// byte boundaries. So the least significant bit of a function address
|
||||
// is always 0.
|
||||
//
|
||||
// To distinguish Thumb functions from ARM fucntions, the LSB of a
|
||||
// function address is repurposed as a boolean flag. If the LSB is 0,
|
||||
// the function referred to by the address is encoded in ARM;
|
||||
// otherwise, Thumb.
|
||||
//
|
||||
// For example, if a symbol `foo` is of type STT_FUNC and has value
|
||||
// 0x2001, `foo` is a function using Thumb instructions whose address
|
||||
// is 0x2000 (not 0x2001, as Thumb instructions are always 2-byte
|
||||
// aligned). Likewise, if a function pointer has value 0x2001, it
|
||||
// refers a Thumb function at 0x2000.
|
||||
//
|
||||
// https://github.com/ARM-software/abi-aa/blob/main/aaelf32/aaelf32.rst
|
||||
|
||||
#include "mold.h"
|
||||
|
||||
#include <tbb/parallel_for.h>
|
||||
|
||||
namespace mold {
|
||||
|
||||
using E = ARM32;
|
||||
|
||||
template <>
|
||||
i64 get_addend(u8 *loc, const ElfRel<E> &rel) {
|
||||
ul32 *arm = (ul32 *)loc;
|
||||
ul16 *thm = (ul16 *)loc;
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_ARM_ABS32:
|
||||
case R_ARM_REL32:
|
||||
case R_ARM_BASE_PREL:
|
||||
case R_ARM_GOTOFF32:
|
||||
case R_ARM_GOT_PREL:
|
||||
case R_ARM_GOT_BREL:
|
||||
case R_ARM_TLS_GD32:
|
||||
case R_ARM_TLS_LDM32:
|
||||
case R_ARM_TLS_LDO32:
|
||||
case R_ARM_TLS_IE32:
|
||||
case R_ARM_TLS_LE32:
|
||||
case R_ARM_TLS_GOTDESC:
|
||||
case R_ARM_TARGET1:
|
||||
case R_ARM_TARGET2:
|
||||
return (il32)*arm;
|
||||
case R_ARM_THM_JUMP8:
|
||||
return sign_extend(thm[0], 8) << 1;
|
||||
case R_ARM_THM_JUMP11:
|
||||
return sign_extend(thm[0], 11) << 1;
|
||||
case R_ARM_THM_JUMP19: {
|
||||
// https://developer.arm.com/documentation/ddi0597/2024-12/Base-Instructions/B--Branch-
|
||||
u32 S = bit(thm[0], 10);
|
||||
u32 J2 = bit(thm[1], 11);
|
||||
u32 J1 = bit(thm[1], 13);
|
||||
u32 imm6 = bits(thm[0], 5, 0);
|
||||
u32 imm11 = bits(thm[1], 10, 0);
|
||||
u32 val = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
|
||||
return sign_extend(val, 21);
|
||||
}
|
||||
case R_ARM_THM_CALL:
|
||||
case R_ARM_THM_JUMP24:
|
||||
case R_ARM_THM_TLS_CALL: {
|
||||
// https://developer.arm.com/documentation/ddi0597/2024-12/Base-Instructions/BL--BLX--immediate---Branch-with-Link-and-optional-Exchange--immediate--
|
||||
u32 S = bit(thm[0], 10);
|
||||
u32 J1 = bit(thm[1], 13);
|
||||
u32 J2 = bit(thm[1], 11);
|
||||
u32 I1 = !(J1 ^ S);
|
||||
u32 I2 = !(J2 ^ S);
|
||||
u32 imm10 = bits(thm[0], 9, 0);
|
||||
u32 imm11 = bits(thm[1], 10, 0);
|
||||
u32 val = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
|
||||
return sign_extend(val, 25);
|
||||
}
|
||||
case R_ARM_CALL:
|
||||
case R_ARM_JUMP24:
|
||||
case R_ARM_PLT32:
|
||||
case R_ARM_TLS_CALL:
|
||||
return sign_extend(*arm, 24) << 2;
|
||||
case R_ARM_MOVW_PREL_NC:
|
||||
case R_ARM_MOVW_ABS_NC:
|
||||
case R_ARM_MOVT_PREL:
|
||||
case R_ARM_MOVT_ABS: {
|
||||
// https://developer.arm.com/documentation/ddi0597/2024-12/Base-Instructions/MOV--MOVS--immediate---Move--immediate--
|
||||
u32 imm4 = bits(*arm, 19, 16);
|
||||
u32 imm12 = bits(*arm, 11, 0);
|
||||
u32 val = (imm4 << 12) | imm12;
|
||||
return sign_extend(val, 16);
|
||||
}
|
||||
case R_ARM_PREL31:
|
||||
return sign_extend(*arm, 31);
|
||||
case R_ARM_THM_MOVW_PREL_NC:
|
||||
case R_ARM_THM_MOVW_ABS_NC:
|
||||
case R_ARM_THM_MOVT_PREL:
|
||||
case R_ARM_THM_MOVT_ABS: {
|
||||
// https://developer.arm.com/documentation/ddi0597/2024-12/Base-Instructions/MOVT--Move-Top-
|
||||
u32 imm4 = bits(thm[0], 3, 0);
|
||||
u32 i = bit(thm[0], 10);
|
||||
u32 imm3 = bits(thm[1], 14, 12);
|
||||
u32 imm8 = bits(thm[1], 7, 0);
|
||||
u32 val = (imm4 << 12) | (i << 11) | (imm3 << 8) | imm8;
|
||||
return sign_extend(val, 16);
|
||||
}
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void write_arm_mov(u8 *loc, u32 val) {
|
||||
u32 imm12 = bits(val, 11, 0);
|
||||
u32 imm4 = bits(val, 15, 12);
|
||||
*(ul32 *)loc = (*(ul32 *)loc & 0xfff0'f000) | (imm4 << 16) | imm12;
|
||||
}
|
||||
|
||||
static void write_thm_b21(u8 *loc, u32 val) {
|
||||
u32 S = bit(val, 20);
|
||||
u32 J2 = bit(val, 19);
|
||||
u32 J1 = bit(val, 18);
|
||||
u32 imm6 = bits(val, 17, 12);
|
||||
u32 imm11 = bits(val, 11, 1);
|
||||
|
||||
ul16 *buf = (ul16 *)loc;
|
||||
buf[0] = (buf[0] & 0b1111'1011'1100'0000) | (S << 10) | imm6;
|
||||
buf[1] = (buf[1] & 0b1101'0000'0000'0000) | (J1 << 13) | (J2 << 11) | imm11;
|
||||
}
|
||||
|
||||
static void write_thm_b25(u8 *loc, u32 val) {
|
||||
u32 S = bit(val, 24);
|
||||
u32 I1 = bit(val, 23);
|
||||
u32 I2 = bit(val, 22);
|
||||
u32 J1 = !I1 ^ S;
|
||||
u32 J2 = !I2 ^ S;
|
||||
u32 imm10 = bits(val, 21, 12);
|
||||
u32 imm11 = bits(val, 11, 1);
|
||||
|
||||
ul16 *buf = (ul16 *)loc;
|
||||
buf[0] = (buf[0] & 0b1111'1000'0000'0000) | (S << 10) | imm10;
|
||||
buf[1] = (buf[1] & 0b1101'0000'0000'0000) | (J1 << 13) | (J2 << 11) | imm11;
|
||||
}
|
||||
|
||||
static void write_thm_mov(u8 *loc, u32 val) {
|
||||
u32 imm4 = bits(val, 15, 12);
|
||||
u32 i = bit(val, 11);
|
||||
u32 imm3 = bits(val, 10, 8);
|
||||
u32 imm8 = bits(val, 7, 0);
|
||||
|
||||
ul16 *buf = (ul16 *)loc;
|
||||
buf[0] = (buf[0] & 0b1111'1011'1111'0000) | (i << 10) | imm4;
|
||||
buf[1] = (buf[1] & 0b1000'1111'0000'0000) | (imm3 << 12) | imm8;
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_addend(u8 *loc, i64 val, const ElfRel<E> &rel) {
|
||||
switch (rel.r_type) {
|
||||
case R_ARM_NONE:
|
||||
break;
|
||||
case R_ARM_ABS32:
|
||||
case R_ARM_REL32:
|
||||
case R_ARM_BASE_PREL:
|
||||
case R_ARM_GOTOFF32:
|
||||
case R_ARM_GOT_PREL:
|
||||
case R_ARM_GOT_BREL:
|
||||
case R_ARM_TLS_GD32:
|
||||
case R_ARM_TLS_LDM32:
|
||||
case R_ARM_TLS_LDO32:
|
||||
case R_ARM_TLS_IE32:
|
||||
case R_ARM_TLS_LE32:
|
||||
case R_ARM_TLS_GOTDESC:
|
||||
case R_ARM_TARGET1:
|
||||
case R_ARM_TARGET2:
|
||||
*(ul32 *)loc = val;
|
||||
break;
|
||||
case R_ARM_THM_JUMP8:
|
||||
*(ul16 *)loc = (*(ul16 *)loc & 0xff00) | bits(val, 8, 1);
|
||||
break;
|
||||
case R_ARM_THM_JUMP11:
|
||||
*(ul16 *)loc = (*(ul16 *)loc & 0xf800) | bits(val, 11, 1);
|
||||
break;
|
||||
case R_ARM_THM_CALL:
|
||||
case R_ARM_THM_JUMP24:
|
||||
case R_ARM_THM_TLS_CALL:
|
||||
write_thm_b25(loc, val);
|
||||
break;
|
||||
case R_ARM_CALL:
|
||||
case R_ARM_JUMP24:
|
||||
case R_ARM_PLT32:
|
||||
*(ul32 *)loc = (*(ul32 *)loc & 0xff00'0000) | bits(val, 25, 2);
|
||||
break;
|
||||
case R_ARM_MOVW_PREL_NC:
|
||||
case R_ARM_MOVW_ABS_NC:
|
||||
case R_ARM_MOVT_PREL:
|
||||
case R_ARM_MOVT_ABS:
|
||||
write_arm_mov(loc, val);
|
||||
break;
|
||||
case R_ARM_PREL31:
|
||||
*(ul32 *)loc = (*(ul32 *)loc & 0x8000'0000) | (val & 0x7fff'ffff);
|
||||
break;
|
||||
case R_ARM_THM_MOVW_PREL_NC:
|
||||
case R_ARM_THM_MOVW_ABS_NC:
|
||||
case R_ARM_THM_MOVT_PREL:
|
||||
case R_ARM_THM_MOVT_ABS:
|
||||
write_thm_mov(loc, val);
|
||||
break;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_plt_header(Context<E> &ctx, u8 *buf) {
|
||||
constexpr ul32 insn[] = {
|
||||
0xe52d'e004, // push {lr}
|
||||
0xe59f'e004, // ldr lr, 2f
|
||||
0xe08f'e00e, // 1: add lr, pc, lr
|
||||
0xe5be'f008, // ldr pc, [lr, #8]!
|
||||
0x0000'0000, // 2: .word .got.plt - 1b - 8
|
||||
0x0000'0000, // (padding)
|
||||
0x0000'0000, // (padding)
|
||||
0x0000'0000, // (padding)
|
||||
};
|
||||
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
*(ul32 *)(buf + 16) = ctx.gotplt->shdr.sh_addr - ctx.plt->shdr.sh_addr - 16;
|
||||
}
|
||||
|
||||
static constexpr ul32 plt_entry[] = {
|
||||
0xe59f'c004, // 1: ldr ip, 2f
|
||||
0xe08c'c00f, // add ip, ip, pc
|
||||
0xe59c'f000, // ldr pc, [ip]
|
||||
0x0000'0000, // 2: .word sym@GOT - 1b
|
||||
};
|
||||
|
||||
template <>
|
||||
void write_plt_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
|
||||
memcpy(buf, plt_entry, sizeof(plt_entry));
|
||||
*(ul32 *)(buf + 12) = sym.get_gotplt_addr(ctx) - sym.get_plt_addr(ctx) - 12;
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_pltgot_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
|
||||
memcpy(buf, plt_entry, sizeof(plt_entry));
|
||||
*(ul32 *)(buf + 12) = sym.get_got_pltgot_addr(ctx) - sym.get_plt_addr(ctx) - 12;
|
||||
}
|
||||
|
||||
template <>
|
||||
void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
|
||||
u64 offset, u64 val) {
|
||||
u8 *loc = ctx.buf + this->shdr.sh_offset + offset;
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_NONE:
|
||||
break;
|
||||
case R_ARM_ABS32:
|
||||
*(ul32 *)loc = val;
|
||||
break;
|
||||
case R_ARM_REL32:
|
||||
*(ul32 *)loc = val - this->shdr.sh_addr - offset;
|
||||
break;
|
||||
default:
|
||||
Fatal(ctx) << "unsupported relocation in .eh_frame: " << rel;
|
||||
}
|
||||
}
|
||||
|
||||
static Thunk<E> &get_reachable_thunk(OutputSection<E> &osec, u64 addr) {
|
||||
auto it = std::upper_bound(osec.thunks.begin(), osec.thunks.end(), addr,
|
||||
[](u64 addr, std::unique_ptr<Thunk<E>> &thunk) {
|
||||
return addr < thunk->get_addr();
|
||||
});
|
||||
return **it;
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
|
||||
std::span<const ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_NONE || rel.r_type == R_ARM_V4BX)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
u64 S = sym.get_addr(ctx);
|
||||
u64 A = get_addend(*this, rel);
|
||||
u64 P = get_addr() + rel.r_offset;
|
||||
u64 T = S & 1;
|
||||
u64 G = sym.get_got_idx(ctx) * sizeof(Word<E>);
|
||||
u64 GOT = ctx.got->shdr.sh_addr;
|
||||
|
||||
auto check = [&](i64 val, i64 lo, i64 hi) {
|
||||
check_range(ctx, i, val, lo, hi);
|
||||
};
|
||||
|
||||
auto get_thumb_thunk_addr = [&] { return sym.get_thunk_addr(ctx, P); };
|
||||
auto get_arm_thunk_addr = [&] { return sym.get_thunk_addr(ctx, P) + 4; };
|
||||
|
||||
auto get_tlsdesc_trampoline_addr = [&] {
|
||||
return get_reachable_thunk(*output_section, P).get_addr();
|
||||
};
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_ARM_ABS32:
|
||||
case R_ARM_TARGET1:
|
||||
break;
|
||||
case R_ARM_REL32:
|
||||
*(ul32 *)loc = S + A - P;
|
||||
break;
|
||||
case R_ARM_THM_CALL: {
|
||||
if (sym.is_remaining_undef_weak()) {
|
||||
// On ARM, calling an weak undefined symbol jumps to the
|
||||
// next instruction.
|
||||
*(ul32 *)loc = 0x8000'f3af; // NOP.W
|
||||
break;
|
||||
}
|
||||
|
||||
// THM_CALL relocation refers to either BL or BLX instruction.
|
||||
// They are different in only one bit. We need to use BL if
|
||||
// the jump target is Thumb. Otherwise, use BLX.
|
||||
i64 val1 = S + A - P;
|
||||
i64 val2 = align_to(S + A - P, 4);
|
||||
|
||||
if (T && is_int(val1, 25)) {
|
||||
*(ul16 *)(loc + 2) |= 0x1000; // BL
|
||||
write_thm_b25(loc, val1);
|
||||
} else if (!T && is_int(val2, 25)) {
|
||||
*(ul16 *)(loc + 2) &= ~0x1000; // BLX
|
||||
write_thm_b25(loc, val2);
|
||||
} else {
|
||||
*(ul16 *)(loc + 2) |= 0x1000; // BL
|
||||
write_thm_b25(loc, get_thumb_thunk_addr() + A - P);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case R_ARM_BASE_PREL:
|
||||
*(ul32 *)loc = GOT + A - P;
|
||||
break;
|
||||
case R_ARM_GOTOFF32:
|
||||
*(ul32 *)loc = ((S + A) | T) - GOT;
|
||||
break;
|
||||
case R_ARM_GOT_PREL:
|
||||
case R_ARM_TARGET2:
|
||||
*(ul32 *)loc = GOT + G + A - P;
|
||||
break;
|
||||
case R_ARM_GOT_BREL:
|
||||
*(ul32 *)loc = G + A;
|
||||
break;
|
||||
case R_ARM_CALL: {
|
||||
if (sym.is_remaining_undef_weak()) {
|
||||
*(ul32 *)loc = 0xe320'f000; // NOP
|
||||
break;
|
||||
}
|
||||
|
||||
// Just like THM_CALL, ARM_CALL relocation refers to either BL or
|
||||
// BLX instruction. We may need to rewrite BL → BLX or BLX → BL.
|
||||
bool is_bl = ((*(ul32 *)loc & 0xff00'0000) == 0xeb00'0000);
|
||||
bool is_blx = ((*(ul32 *)loc & 0xfe00'0000) == 0xfa00'0000);
|
||||
if (!is_bl && !is_blx)
|
||||
Fatal(ctx) << *this << ": R_ARM_CALL refers to neither BL nor BLX";
|
||||
|
||||
i64 val = S + A - P;
|
||||
if (is_int(val, 26)) {
|
||||
if (T) {
|
||||
*(ul32 *)loc = 0xfa00'0000; // BLX
|
||||
*(ul32 *)loc |= (bit(val, 1) << 24) | bits(val, 25, 2);
|
||||
} else {
|
||||
*(ul32 *)loc = 0xeb00'0000; // BL
|
||||
*(ul32 *)loc |= bits(val, 25, 2);
|
||||
}
|
||||
} else {
|
||||
*(ul32 *)loc = 0xeb00'0000; // BL
|
||||
*(ul32 *)loc |= bits(get_arm_thunk_addr() + A - P, 25, 2);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case R_ARM_JUMP24: {
|
||||
if (sym.is_remaining_undef_weak()) {
|
||||
*(ul32 *)loc = 0xe320'f000; // NOP
|
||||
break;
|
||||
}
|
||||
|
||||
// These relocs refers to a B (unconditional branch) instruction.
|
||||
// Unlike BL or BLX, we can't rewrite B to BX in place when the
|
||||
// processor mode switch is required because BX doesn't takes an
|
||||
// immediate; it takes only a register. So if mode switch is
|
||||
// required, we jump to a linker-synthesized thunk which does the
|
||||
// job with a longer code sequence.
|
||||
i64 val = S + A - P;
|
||||
if (T || !is_int(val, 26))
|
||||
val = get_arm_thunk_addr() + A - P;
|
||||
*(ul32 *)loc = (*(ul32 *)loc & 0xff00'0000) | bits(val, 25, 2);
|
||||
break;
|
||||
}
|
||||
case R_ARM_PLT32:
|
||||
if (sym.is_remaining_undef_weak()) {
|
||||
*(ul32 *)loc = 0xe320'f000; // NOP
|
||||
} else {
|
||||
u64 val = (T ? get_arm_thunk_addr() : S) + A - P;
|
||||
*(ul32 *)loc = (*(ul32 *)loc & 0xff00'0000) | bits(val, 25, 2);
|
||||
}
|
||||
break;
|
||||
case R_ARM_THM_JUMP8:
|
||||
check(S + A - P, -(1 << 8), 1 << 8);
|
||||
*(ul16 *)loc &= 0xff00;
|
||||
*(ul16 *)loc |= bits(S + A - P, 8, 1);
|
||||
break;
|
||||
case R_ARM_THM_JUMP11:
|
||||
check(S + A - P, -(1 << 11), 1 << 11);
|
||||
*(ul16 *)loc &= 0xf800;
|
||||
*(ul16 *)loc |= bits(S + A - P, 11, 1);
|
||||
break;
|
||||
case R_ARM_THM_JUMP19:
|
||||
check(S + A - P, -(1 << 20), 1 << 20);
|
||||
write_thm_b21(loc, S + A - P);
|
||||
break;
|
||||
case R_ARM_THM_JUMP24: {
|
||||
if (sym.is_remaining_undef_weak()) {
|
||||
*(ul32 *)loc = 0x8000'f3af; // NOP
|
||||
break;
|
||||
}
|
||||
|
||||
// Just like R_ARM_JUMP24, we need to jump to a thunk if we need to
|
||||
// switch processor mode.
|
||||
i64 val = S + A - P;
|
||||
if (!T || !is_int(val, 25))
|
||||
val = get_thumb_thunk_addr() + A - P;
|
||||
write_thm_b25(loc, val);
|
||||
break;
|
||||
}
|
||||
case R_ARM_MOVW_PREL_NC:
|
||||
write_arm_mov(loc, ((S + A) | T) - P);
|
||||
break;
|
||||
case R_ARM_MOVW_ABS_NC:
|
||||
write_arm_mov(loc, (S + A) | T);
|
||||
break;
|
||||
case R_ARM_THM_MOVW_PREL_NC:
|
||||
write_thm_mov(loc, ((S + A) | T) - P);
|
||||
break;
|
||||
case R_ARM_PREL31:
|
||||
check(S + A - P, -(1LL << 30), 1LL << 30);
|
||||
*(ul32 *)loc &= 0x8000'0000;
|
||||
*(ul32 *)loc |= (S + A - P) & 0x7fff'ffff;
|
||||
break;
|
||||
case R_ARM_THM_MOVW_ABS_NC:
|
||||
write_thm_mov(loc, (S + A) | T);
|
||||
break;
|
||||
case R_ARM_MOVT_PREL:
|
||||
write_arm_mov(loc, (S + A - P) >> 16);
|
||||
break;
|
||||
case R_ARM_THM_MOVT_PREL:
|
||||
write_thm_mov(loc, (S + A - P) >> 16);
|
||||
break;
|
||||
case R_ARM_MOVT_ABS:
|
||||
write_arm_mov(loc, (S + A) >> 16);
|
||||
break;
|
||||
case R_ARM_THM_MOVT_ABS:
|
||||
write_thm_mov(loc, (S + A) >> 16);
|
||||
break;
|
||||
case R_ARM_TLS_GD32:
|
||||
*(ul32 *)loc = sym.get_tlsgd_addr(ctx) + A - P;
|
||||
break;
|
||||
case R_ARM_TLS_LDM32:
|
||||
*(ul32 *)loc = ctx.got->get_tlsld_addr(ctx) + A - P;
|
||||
break;
|
||||
case R_ARM_TLS_LDO32:
|
||||
*(ul32 *)loc = S + A - ctx.dtp_addr;
|
||||
break;
|
||||
case R_ARM_TLS_IE32:
|
||||
*(ul32 *)loc = sym.get_gottp_addr(ctx) + A - P;
|
||||
break;
|
||||
case R_ARM_TLS_LE32:
|
||||
*(ul32 *)loc = S + A - ctx.tp_addr;
|
||||
break;
|
||||
case R_ARM_TLS_GOTDESC:
|
||||
// ARM32 TLSDESC uses the following code sequence to materialize
|
||||
// a TP-relative address in r0.
|
||||
//
|
||||
// ldr r0, .L2
|
||||
// .L1: bl foo
|
||||
// R_ARM_TLS_CALL
|
||||
// .L2: .word foo + . - .L1
|
||||
// R_ARM_TLS_GOTDESC
|
||||
//
|
||||
// We may relax the instructions to the following if its TP-relative
|
||||
// address is known at link-time
|
||||
//
|
||||
// ldr r0, .L2
|
||||
// .L1: nop
|
||||
// ...
|
||||
// .L2: .word foo(tpoff)
|
||||
//
|
||||
// or to the following if the TP-relative address is known at
|
||||
// process startup time.
|
||||
//
|
||||
// ldr r0, .L2
|
||||
// .L1: ldr r0, [pc, r0]
|
||||
// ...
|
||||
// .L2: .word foo(gottpoff) + . - .L1
|
||||
if (sym.has_tlsdesc(ctx)) {
|
||||
// A is odd if the corresponding TLS_CALL is Thumb.
|
||||
*(ul32 *)loc = sym.get_tlsdesc_addr(ctx) - P + A - ((A & 1) ? 6 : 4);
|
||||
} else if (sym.has_gottp(ctx)) {
|
||||
*(ul32 *)loc = sym.get_gottp_addr(ctx) - P + A - ((A & 1) ? 5 : 8);
|
||||
} else {
|
||||
*(ul32 *)loc = S - ctx.tp_addr;
|
||||
}
|
||||
break;
|
||||
case R_ARM_TLS_CALL:
|
||||
if (sym.has_tlsdesc(ctx)) {
|
||||
*(ul32 *)loc = 0xeb00'0000; // bl 0
|
||||
*(ul32 *)loc |= bits(get_tlsdesc_trampoline_addr() - P - 8, 25, 2);
|
||||
} else if (sym.has_gottp(ctx)) {
|
||||
*(ul32 *)loc = 0xe79f'0000; // ldr r0, [pc, r0]
|
||||
} else {
|
||||
*(ul32 *)loc = 0xe320'f000; // nop
|
||||
}
|
||||
break;
|
||||
case R_ARM_THM_TLS_CALL:
|
||||
if (sym.has_tlsdesc(ctx)) {
|
||||
u64 val = align_to(get_tlsdesc_trampoline_addr() - P - 4, 4);
|
||||
write_thm_b25(loc, val);
|
||||
*(ul16 *)(loc + 2) &= ~0x1000; // rewrite BL with BLX
|
||||
} else if (sym.has_gottp(ctx)) {
|
||||
// Since `ldr r0, [pc, r0]` is not representable in Thumb,
|
||||
// we use two instructions instead.
|
||||
*(ul16 *)loc = 0x4478; // add r0, pc
|
||||
*(ul16 *)(loc + 2) = 0x6800; // ldr r0, [r0]
|
||||
} else {
|
||||
*(ul32 *)loc = 0x8000'f3af; // nop.w
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Error(ctx) << *this << ": unknown relocation: " << rel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
|
||||
std::span<const ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_NONE || record_undef_error(ctx, rel))
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
SectionFragment<E> *frag;
|
||||
i64 frag_addend;
|
||||
std::tie(frag, frag_addend) = get_fragment(ctx, rel);
|
||||
|
||||
u64 S = frag ? frag->get_addr(ctx) : sym.get_addr(ctx);
|
||||
u64 A = frag ? frag_addend : get_addend(*this, rel);
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_ARM_ABS32:
|
||||
if (std::optional<u64> val = get_tombstone(sym, frag))
|
||||
*(ul32 *)loc = *val;
|
||||
else
|
||||
*(ul32 *)loc = S + A;
|
||||
break;
|
||||
case R_ARM_TLS_LDO32:
|
||||
if (std::optional<u64> val = get_tombstone(sym, frag))
|
||||
*(ul32 *)loc = *val;
|
||||
else
|
||||
*(ul32 *)loc = S + A - ctx.dtp_addr;
|
||||
break;
|
||||
default:
|
||||
Fatal(ctx) << *this << ": invalid relocation for non-allocated sections: "
|
||||
<< rel;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::scan_relocations(Context<E> &ctx) {
|
||||
assert(shdr().sh_flags & SHF_ALLOC);
|
||||
std::span<const ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
// Scan relocations
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_NONE || record_undef_error(ctx, rel))
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
|
||||
if (sym.is_ifunc())
|
||||
sym.flags |= NEEDS_GOT | NEEDS_PLT;
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_ARM_MOVW_ABS_NC:
|
||||
case R_ARM_THM_MOVW_ABS_NC:
|
||||
scan_absrel(ctx, sym, rel);
|
||||
break;
|
||||
case R_ARM_THM_CALL:
|
||||
case R_ARM_CALL:
|
||||
case R_ARM_JUMP24:
|
||||
case R_ARM_PLT32:
|
||||
case R_ARM_THM_JUMP24:
|
||||
if (sym.is_imported)
|
||||
sym.flags |= NEEDS_PLT;
|
||||
break;
|
||||
case R_ARM_GOT_PREL:
|
||||
case R_ARM_GOT_BREL:
|
||||
case R_ARM_TARGET2:
|
||||
sym.flags |= NEEDS_GOT;
|
||||
break;
|
||||
case R_ARM_MOVT_PREL:
|
||||
case R_ARM_THM_MOVT_PREL:
|
||||
case R_ARM_PREL31:
|
||||
scan_pcrel(ctx, sym, rel);
|
||||
break;
|
||||
case R_ARM_TLS_GD32:
|
||||
sym.flags |= NEEDS_TLSGD;
|
||||
break;
|
||||
case R_ARM_TLS_LDM32:
|
||||
ctx.needs_tlsld = true;
|
||||
break;
|
||||
case R_ARM_TLS_IE32:
|
||||
sym.flags |= NEEDS_GOTTP;
|
||||
break;
|
||||
case R_ARM_TLS_CALL:
|
||||
case R_ARM_THM_TLS_CALL:
|
||||
scan_tlsdesc(ctx, sym);
|
||||
break;
|
||||
case R_ARM_TLS_LE32:
|
||||
check_tlsle(ctx, sym, rel);
|
||||
break;
|
||||
case R_ARM_ABS32:
|
||||
case R_ARM_TARGET1:
|
||||
case R_ARM_MOVT_ABS:
|
||||
case R_ARM_THM_MOVT_ABS:
|
||||
case R_ARM_REL32:
|
||||
case R_ARM_BASE_PREL:
|
||||
case R_ARM_GOTOFF32:
|
||||
case R_ARM_THM_JUMP8:
|
||||
case R_ARM_THM_JUMP11:
|
||||
case R_ARM_THM_JUMP19:
|
||||
case R_ARM_MOVW_PREL_NC:
|
||||
case R_ARM_THM_MOVW_PREL_NC:
|
||||
case R_ARM_TLS_LDO32:
|
||||
case R_ARM_V4BX:
|
||||
case R_ARM_TLS_GOTDESC:
|
||||
break;
|
||||
default:
|
||||
Error(ctx) << *this << ": unknown relocation: " << rel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
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.
|
||||
constexpr ul32 hdr[] = {
|
||||
0xe08e'0000, // add r0, lr, r0
|
||||
0xe590'1004, // ldr r1, [r0, #4]
|
||||
0xe12f'ff11, // bx r1
|
||||
0xe320'f000, // nop
|
||||
};
|
||||
|
||||
// This is a range extension and mode switch thunk.
|
||||
// It has two entry points: +0 for Thumb and +4 for ARM.
|
||||
constexpr u8 entry[] = {
|
||||
// .thumb
|
||||
0x78, 0x47, // bx pc # jumps to 1f
|
||||
0xc0, 0x46, // nop
|
||||
// .arm
|
||||
0x00, 0xc0, 0x9f, 0xe5, // 1: ldr ip, 3f
|
||||
0x0f, 0xf0, 0x8c, 0xe0, // 2: add pc, ip, pc
|
||||
0x00, 0x00, 0x00, 0x00, // 3: .word sym - 2b
|
||||
};
|
||||
|
||||
static_assert(E::thunk_hdr_size == sizeof(hdr));
|
||||
static_assert(E::thunk_size == sizeof(entry));
|
||||
|
||||
u8 *buf = ctx.buf + output_section.shdr.sh_offset + offset;
|
||||
memcpy(buf, hdr, sizeof(hdr));
|
||||
buf += sizeof(hdr);
|
||||
|
||||
u64 P = output_section.shdr.sh_addr + offset + sizeof(hdr);
|
||||
|
||||
for (Symbol<E> *sym : symbols) {
|
||||
memcpy(buf, entry, sizeof(entry));
|
||||
*(ul32 *)(buf + 12) = sym->get_addr(ctx) - P - 16;
|
||||
|
||||
buf += sizeof(entry);
|
||||
P += sizeof(entry);
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
u64 get_eflags(Context<E> &ctx) {
|
||||
return EF_ARM_EABI_VER5;
|
||||
}
|
||||
|
||||
void create_arm_exidx_section(Context<E> &ctx) {
|
||||
for (i64 i = 0; i < ctx.chunks.size(); i++) {
|
||||
OutputSection<E> *osec = ctx.chunks[i]->to_osec();
|
||||
|
||||
if (osec && osec->shdr.sh_type == SHT_ARM_EXIDX) {
|
||||
auto *sec = new Arm32ExidxSection(*osec);
|
||||
ctx.extra.exidx = sec;
|
||||
ctx.chunks[i] = sec;
|
||||
ctx.chunk_pool.emplace_back(sec);
|
||||
|
||||
for (InputSection<E> *isec : osec->members)
|
||||
isec->is_alive = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Arm32ExidxSection::compute_section_size(Context<E> &ctx) {
|
||||
output_section.compute_section_size(ctx);
|
||||
this->shdr.sh_size = output_section.shdr.sh_size + 8; // +8 for sentinel
|
||||
}
|
||||
|
||||
void Arm32ExidxSection::update_shdr(Context<E> &ctx) {
|
||||
// .ARM.exidx's sh_link should be set to the .text section index.
|
||||
// Runtime doesn't care about it, but the binutils's strip command does.
|
||||
if (Chunk<E> *chunk = find_chunk(ctx, ".text"))
|
||||
this->shdr.sh_link = chunk->shndx;
|
||||
}
|
||||
|
||||
void Arm32ExidxSection::remove_duplicate_entries(Context<E> &ctx) {
|
||||
this->shdr.sh_size = get_contents(ctx).size();
|
||||
}
|
||||
|
||||
void Arm32ExidxSection::copy_buf(Context<E> &ctx) {
|
||||
std::vector<u8> contents = get_contents(ctx);
|
||||
assert(this->shdr.sh_size == contents.size());
|
||||
write_vector(ctx.buf + this->shdr.sh_offset, contents);
|
||||
}
|
||||
|
||||
// Returns the end of the text segment
|
||||
static u64 get_text_end(Context<E> &ctx) {
|
||||
u64 ret = 0;
|
||||
for (Chunk<E> *chunk : ctx.chunks)
|
||||
if (chunk->shdr.sh_flags & SHF_EXECINSTR)
|
||||
ret = std::max<u64>(ret, chunk->shdr.sh_addr + chunk->shdr.sh_size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// ARM executables use an .ARM.exidx section to look up an exception
|
||||
// handling record for the current instruction pointer. The table needs
|
||||
// to be sorted by their addresses.
|
||||
//
|
||||
// Other target uses .eh_frame_hdr instead for the same purpose.
|
||||
// I don't know why only ARM uses the different mechanism, but it's
|
||||
// likely that it's due to some historical reason.
|
||||
//
|
||||
// This function returns contents of .ARM.exidx.
|
||||
std::vector<u8> Arm32ExidxSection::get_contents(Context<E> &ctx) {
|
||||
// .ARM.exidx records consists of a signed 31-bit relative address
|
||||
// and a 32-bit value. The relative address indicates the start
|
||||
// address of a function that the record covers. The value is one of
|
||||
// the followings:
|
||||
//
|
||||
// 1. CANTUNWIND indicating that there's no unwinding info for the function,
|
||||
// 2. a compact unwinding record encoded into a 32-bit value, or
|
||||
// 3. a 31-bit relative address which points to a larger record in
|
||||
// the .ARM.extab section.
|
||||
//
|
||||
// CANTUNWIND is value 1. The most significant bit is set in (2) but
|
||||
// not in (3). So we can distinguished them just by looking at a value.
|
||||
const u32 CANTUNWIND = 1;
|
||||
|
||||
struct Entry {
|
||||
ul32 addr;
|
||||
ul32 val;
|
||||
};
|
||||
|
||||
// We reserve one extra slot for the sentinel
|
||||
i64 num_entries = output_section.shdr.sh_size / sizeof(Entry) + 1;
|
||||
std::vector<u8> buf(num_entries * sizeof(Entry));
|
||||
Entry *ent = (Entry *)buf.data();
|
||||
|
||||
// Write section contents to the buffer
|
||||
output_section.shdr.sh_addr = this->shdr.sh_addr;
|
||||
output_section.write_to(ctx, buf.data());
|
||||
|
||||
// Fill in sentinel fields
|
||||
u64 sentinel_addr = this->shdr.sh_addr + sizeof(Entry) * (num_entries - 1);
|
||||
ent[num_entries - 1].addr = get_text_end(ctx) - sentinel_addr;
|
||||
ent[num_entries - 1].val = CANTUNWIND;
|
||||
|
||||
// Entry's addresses are relative to themselves. In order to sort
|
||||
// records by address, we first translate them so that the addresses
|
||||
// are relative to the beginning of the section.
|
||||
auto is_relative = [](u32 val) {
|
||||
return val != CANTUNWIND && !(val & 0x8000'0000);
|
||||
};
|
||||
|
||||
tbb::parallel_for((i64)0, num_entries, [&](i64 i) {
|
||||
i64 offset = sizeof(Entry) * i;
|
||||
ent[i].addr = sign_extend(ent[i].addr, 31) + offset;
|
||||
if (is_relative(ent[i].val))
|
||||
ent[i].val = 0x7fff'ffff & (ent[i].val + offset);
|
||||
});
|
||||
|
||||
std::sort(ent, ent + num_entries, [](const Entry &a, const Entry &b) {
|
||||
return a.addr < b.addr;
|
||||
});
|
||||
|
||||
// Remove duplicate adjacent entries. That is, if two adjacent functions
|
||||
// have the same compact unwind info or are both CANTUNWIND, we can
|
||||
// merge them into a single address range.
|
||||
auto it = std::unique(ent, ent + num_entries,
|
||||
[](const Entry &a, const Entry &b) {
|
||||
return a.val == b.val;
|
||||
});
|
||||
|
||||
num_entries = it - ent;
|
||||
buf.resize(num_entries * sizeof(Entry));
|
||||
|
||||
// Make addresses relative to themselves.
|
||||
tbb::parallel_for((i64)0, num_entries, [&](i64 i) {
|
||||
i64 offset = sizeof(Entry) * i;
|
||||
ent[i].addr = 0x7fff'ffff & (ent[i].addr - offset);
|
||||
if (is_relative(ent[i].val))
|
||||
ent[i].val = 0x7fff'ffff & (ent[i].val - offset);
|
||||
});
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
@@ -1,617 +0,0 @@
|
||||
// This file contains ARM64-specific code. Being new, the ARM64's ELF
|
||||
// psABI doesn't have anything peculiar. ARM64 is a clean RISC
|
||||
// instruction set that supports PC-relative load/store instructions.
|
||||
//
|
||||
// Unlike ARM32, instructions length doesn't vary. All ARM64
|
||||
// instructions are 4 bytes long.
|
||||
//
|
||||
// Branch instructions used for function call can jump within ±128 MiB.
|
||||
// We need to create range extension thunks to support binaries whose
|
||||
// .text is larger than that.
|
||||
//
|
||||
// Unlike most other targets, the TLSDESC access model is used by default
|
||||
// for -fPIC to access thread-local variables instead of the less
|
||||
// efficient GD model. You can still enable GD but it needs the
|
||||
// -mtls-dialect=trad flag. Since GD is used rarely, we don't need to
|
||||
// implement GD → LE relaxation.
|
||||
//
|
||||
// https://github.com/ARM-software/abi-aa/blob/main/aaelf64/aaelf64.rst
|
||||
|
||||
#if MOLD_ARM64LE || MOLD_ARM64BE
|
||||
|
||||
#include "mold.h"
|
||||
|
||||
namespace mold {
|
||||
|
||||
using E = MOLD_TARGET;
|
||||
|
||||
static void write_adrp(u8 *buf, u64 val) {
|
||||
*(ul32 *)buf |= (bits(val, 13, 12) << 29) | (bits(val, 32, 14) << 5);
|
||||
}
|
||||
|
||||
static void write_adr(u8 *buf, u64 val) {
|
||||
*(ul32 *)buf |= (bits(val, 1, 0) << 29) | (bits(val, 20, 2) << 5);
|
||||
}
|
||||
|
||||
static void write_movn_movz(u8 *buf, i64 val) {
|
||||
*(ul32 *)buf &= 0b0000'0000'0110'0000'0000'0000'0001'1111;
|
||||
|
||||
if (val >= 0)
|
||||
*(ul32 *)buf |= 0xd280'0000 | (bits(val, 15, 0) << 5); // rewrite to movz
|
||||
else
|
||||
*(ul32 *)buf |= 0x9280'0000 | (bits(~val, 15, 0) << 5); // rewrite to movn
|
||||
}
|
||||
|
||||
static u64 page(u64 val) {
|
||||
return val & 0xffff'ffff'ffff'f000;
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_plt_header(Context<E> &ctx, u8 *buf) {
|
||||
constexpr ul32 insn[] = {
|
||||
0xa9bf'7bf0, // stp x16, x30, [sp,#-16]!
|
||||
0x9000'0010, // adrp x16, .got.plt[2]
|
||||
0xf940'0211, // ldr x17, [x16, .got.plt[2]]
|
||||
0x9100'0210, // add x16, x16, .got.plt[2]
|
||||
0xd61f'0220, // br x17
|
||||
0xd420'7d00, // brk
|
||||
0xd420'7d00, // brk
|
||||
0xd420'7d00, // brk
|
||||
};
|
||||
|
||||
u64 gotplt = ctx.gotplt->shdr.sh_addr + 16;
|
||||
u64 plt = ctx.plt->shdr.sh_addr;
|
||||
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
write_adrp(buf + 4, page(gotplt) - page(plt + 4));
|
||||
*(ul32 *)(buf + 8) |= bits(gotplt, 11, 3) << 10;
|
||||
*(ul32 *)(buf + 12) |= (gotplt & 0xfff) << 10;
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_plt_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
|
||||
constexpr ul32 insn[] = {
|
||||
0x9000'0010, // adrp x16, .got.plt[n]
|
||||
0xf940'0211, // ldr x17, [x16, .got.plt[n]]
|
||||
0x9100'0210, // add x16, x16, .got.plt[n]
|
||||
0xd61f'0220, // br x17
|
||||
};
|
||||
|
||||
u64 gotplt = sym.get_gotplt_addr(ctx);
|
||||
u64 plt = sym.get_plt_addr(ctx);
|
||||
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
write_adrp(buf, page(gotplt) - page(plt));
|
||||
*(ul32 *)(buf + 4) |= bits(gotplt, 11, 3) << 10;
|
||||
*(ul32 *)(buf + 8) |= (gotplt & 0xfff) << 10;
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_pltgot_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
|
||||
constexpr ul32 insn[] = {
|
||||
0x9000'0010, // adrp x16, GOT[n]
|
||||
0xf940'0211, // ldr x17, [x16, GOT[n]]
|
||||
0xd61f'0220, // br x17
|
||||
0xd420'7d00, // brk
|
||||
};
|
||||
|
||||
u64 got = sym.get_got_pltgot_addr(ctx);
|
||||
u64 plt = sym.get_plt_addr(ctx);
|
||||
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
write_adrp(buf, page(got) - page(plt));
|
||||
*(ul32 *)(buf + 4) |= bits(got, 11, 3) << 10;
|
||||
}
|
||||
|
||||
template <>
|
||||
void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
|
||||
u64 offset, u64 val) {
|
||||
u8 *loc = ctx.buf + this->shdr.sh_offset + offset;
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_NONE:
|
||||
break;
|
||||
case R_AARCH64_ABS64:
|
||||
*(U64<E> *)loc = val;
|
||||
break;
|
||||
case R_AARCH64_PREL32:
|
||||
*(U32<E> *)loc = val - this->shdr.sh_addr - offset;
|
||||
break;
|
||||
case R_AARCH64_PREL64:
|
||||
*(U64<E> *)loc = val - this->shdr.sh_addr - offset;
|
||||
break;
|
||||
default:
|
||||
Fatal(ctx) << "unsupported relocation in .eh_frame: " << rel;
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_adrp(u8 *loc) {
|
||||
// https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/ADRP--Form-PC-relative-address-to-4KB-page-
|
||||
u32 insn = *(ul32 *)loc;
|
||||
return (bits(insn, 31, 24) & 0b1001'1111) == 0b1001'0000;
|
||||
}
|
||||
|
||||
static bool is_ldr(u8 *loc) {
|
||||
// https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/LDR--immediate---Load-Register--immediate--
|
||||
u32 insn = *(ul32 *)loc;
|
||||
return (bits(insn, 31, 20) & 0b1111'1111'1100) == 0b1111'1001'0100;
|
||||
}
|
||||
|
||||
static bool is_add(u8 *loc) {
|
||||
// https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/ADD--immediate---Add--immediate--
|
||||
u32 insn = *(ul32 *)loc;
|
||||
return (bits(insn, 31, 20) & 0b1111'1111'1100) == 0b1001'0001'0000;
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
|
||||
std::span<const ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_NONE)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
u64 S = sym.get_addr(ctx);
|
||||
u64 A = rel.r_addend;
|
||||
u64 P = get_addr() + rel.r_offset;
|
||||
u64 G = sym.get_got_idx(ctx) * sizeof(Word<E>);
|
||||
u64 GOT = ctx.got->shdr.sh_addr;
|
||||
|
||||
auto check = [&](i64 val, i64 lo, i64 hi) {
|
||||
check_range(ctx, i, val, lo, hi);
|
||||
};
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_AARCH64_ABS64:
|
||||
break;
|
||||
case R_AARCH64_LDST8_ABS_LO12_NC:
|
||||
case R_AARCH64_ADD_ABS_LO12_NC:
|
||||
*(ul32 *)loc |= bits(S + A, 11, 0) << 10;
|
||||
break;
|
||||
case R_AARCH64_LDST16_ABS_LO12_NC:
|
||||
*(ul32 *)loc |= bits(S + A, 11, 1) << 10;
|
||||
break;
|
||||
case R_AARCH64_LDST32_ABS_LO12_NC:
|
||||
*(ul32 *)loc |= bits(S + A, 11, 2) << 10;
|
||||
break;
|
||||
case R_AARCH64_LDST64_ABS_LO12_NC:
|
||||
*(ul32 *)loc |= bits(S + A, 11, 3) << 10;
|
||||
break;
|
||||
case R_AARCH64_LDST128_ABS_LO12_NC:
|
||||
*(ul32 *)loc |= bits(S + A, 11, 4) << 10;
|
||||
break;
|
||||
case R_AARCH64_MOVW_UABS_G0:
|
||||
check(S + A, 0, 1 << 16);
|
||||
*(ul32 *)loc |= bits(S + A, 15, 0) << 5;
|
||||
break;
|
||||
case R_AARCH64_MOVW_UABS_G0_NC:
|
||||
*(ul32 *)loc |= bits(S + A, 15, 0) << 5;
|
||||
break;
|
||||
case R_AARCH64_MOVW_UABS_G1:
|
||||
check(S + A, 0, 1LL << 32);
|
||||
*(ul32 *)loc |= bits(S + A, 31, 16) << 5;
|
||||
break;
|
||||
case R_AARCH64_MOVW_UABS_G1_NC:
|
||||
*(ul32 *)loc |= bits(S + A, 31, 16) << 5;
|
||||
break;
|
||||
case R_AARCH64_MOVW_UABS_G2:
|
||||
check(S + A, 0, 1LL << 48);
|
||||
*(ul32 *)loc |= bits(S + A, 47, 32) << 5;
|
||||
break;
|
||||
case R_AARCH64_MOVW_UABS_G2_NC:
|
||||
*(ul32 *)loc |= bits(S + A, 47, 32) << 5;
|
||||
break;
|
||||
case R_AARCH64_MOVW_UABS_G3:
|
||||
*(ul32 *)loc |= bits(S + A, 63, 48) << 5;
|
||||
break;
|
||||
case R_AARCH64_ADR_GOT_PAGE:
|
||||
if (sym.has_got(ctx)) {
|
||||
i64 val = page(G + GOT + A) - page(P);
|
||||
check(val, -(1LL << 32), 1LL << 32);
|
||||
write_adrp(loc, val);
|
||||
} else {
|
||||
// Relax GOT-loading ADRP+LDR to an immediate ADRP+ADD
|
||||
i64 val = page(S + A) - page(P);
|
||||
check(val, -(1LL << 32), 1LL << 32);
|
||||
write_adrp(loc, val);
|
||||
|
||||
u32 reg = bits(*(ul32 *)loc, 4, 0);
|
||||
*(ul32 *)(loc + 4) = 0x9100'0000 | (reg << 5) | reg; // ADD
|
||||
*(ul32 *)(loc + 4) |= bits(S + A, 11, 0) << 10;
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
case R_AARCH64_ADR_PREL_PG_HI21:
|
||||
case R_AARCH64_ADR_PREL_PG_HI21_NC: {
|
||||
// The ARM64 psABI defines that an `ADRP x0, foo` and `ADD x0, x0,
|
||||
// :lo12: foo` instruction pair to materialize a PC-relative address
|
||||
// in a register can be relaxed to `NOP` followed by `ADR x0, foo`
|
||||
// if foo is in PC ± 1 MiB.
|
||||
if (ctx.arg.relax && sym.is_pcrel_linktime_const(ctx) &&
|
||||
i + 1 < rels.size()) {
|
||||
i64 val = S + A - P - 4;
|
||||
const ElfRel<E> &rel2 = rels[i + 1];
|
||||
if (is_int(val, 21) &&
|
||||
rel2.r_type == R_AARCH64_ADD_ABS_LO12_NC &&
|
||||
rel2.r_sym == rel.r_sym &&
|
||||
rel2.r_offset == rel.r_offset + 4 &&
|
||||
rel2.r_addend == rel.r_addend &&
|
||||
is_adrp(loc) &&
|
||||
is_add(loc + 4)) {
|
||||
u32 reg1 = bits(*(ul32 *)loc, 4, 0);
|
||||
u32 reg2 = bits(*(ul32 *)(loc + 4), 4, 0);
|
||||
if (reg1 == reg2) {
|
||||
*(ul32 *)loc = 0xd503'201f; // nop
|
||||
*(ul32 *)(loc + 4) = 0x1000'0000 | reg1; // adr
|
||||
write_adr(loc + 4, val);
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
i64 val = page(S + A) - page(P);
|
||||
if (rel.r_type == R_AARCH64_ADR_PREL_PG_HI21)
|
||||
check(val, -(1LL << 32), 1LL << 32);
|
||||
write_adrp(loc, val);
|
||||
break;
|
||||
}
|
||||
case R_AARCH64_ADR_PREL_LO21:
|
||||
check(S + A - P, -(1LL << 20), 1LL << 20);
|
||||
write_adr(loc, S + A - P);
|
||||
break;
|
||||
case R_AARCH64_CALL26:
|
||||
case R_AARCH64_JUMP26: {
|
||||
if (sym.is_remaining_undef_weak()) {
|
||||
// On ARM, calling an weak undefined symbol jumps to the
|
||||
// next instruction.
|
||||
*(ul32 *)loc = 0xd503'201f; // nop
|
||||
break;
|
||||
}
|
||||
|
||||
i64 val = S + A - P;
|
||||
if (!is_int(val, 28))
|
||||
val = sym.get_thunk_addr(ctx, P) + A - P;
|
||||
*(ul32 *)loc |= bits(val, 27, 2);
|
||||
break;
|
||||
}
|
||||
case R_AARCH64_PLT32:
|
||||
check(S + A - P, -(1LL << 31), 1LL << 31);
|
||||
*(U32<E> *)loc = S + A - P;
|
||||
break;
|
||||
case R_AARCH64_CONDBR19:
|
||||
case R_AARCH64_LD_PREL_LO19:
|
||||
check(S + A - P, -(1LL << 20), 1LL << 20);
|
||||
*(ul32 *)loc |= bits(S + A - P, 20, 2) << 5;
|
||||
break;
|
||||
case R_AARCH64_PREL16:
|
||||
check(S + A - P, -(1LL << 15), 1LL << 16);
|
||||
*(U16<E> *)loc = S + A - P;
|
||||
break;
|
||||
case R_AARCH64_PREL32:
|
||||
check(S + A - P, -(1LL << 31), 1LL << 32);
|
||||
*(U32<E> *)loc = S + A - P;
|
||||
break;
|
||||
case R_AARCH64_PREL64:
|
||||
*(U64<E> *)loc = S + A - P;
|
||||
break;
|
||||
case R_AARCH64_LD64_GOT_LO12_NC:
|
||||
*(ul32 *)loc |= bits(G + GOT + A, 11, 3) << 10;
|
||||
break;
|
||||
case R_AARCH64_LD64_GOTPAGE_LO15: {
|
||||
i64 val = G + GOT + A - page(GOT);
|
||||
check(val, 0, 1 << 15);
|
||||
*(ul32 *)loc |= bits(val, 14, 3) << 10;
|
||||
break;
|
||||
}
|
||||
case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
|
||||
i64 val = page(sym.get_gottp_addr(ctx) + A) - page(P);
|
||||
check(val, -(1LL << 32), 1LL << 32);
|
||||
write_adrp(loc, val);
|
||||
break;
|
||||
}
|
||||
case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
|
||||
*(ul32 *)loc |= bits(sym.get_gottp_addr(ctx) + A, 11, 3) << 10;
|
||||
break;
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G0: {
|
||||
i64 val = S + A - ctx.tp_addr;
|
||||
check(val, -(1 << 15), 1 << 15);
|
||||
write_movn_movz(loc, val);
|
||||
break;
|
||||
}
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
|
||||
*(ul32 *)loc |= bits(S + A - ctx.tp_addr, 15, 0) << 5;
|
||||
break;
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G1: {
|
||||
i64 val = S + A - ctx.tp_addr;
|
||||
check(val, -(1LL << 31), 1LL << 31);
|
||||
write_movn_movz(loc, val >> 16);
|
||||
break;
|
||||
}
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
|
||||
*(ul32 *)loc |= bits(S + A - ctx.tp_addr, 31, 16) << 5;
|
||||
break;
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G2: {
|
||||
i64 val = S + A - ctx.tp_addr;
|
||||
check(val, -(1LL << 47), 1LL << 47);
|
||||
write_movn_movz(loc, val >> 32);
|
||||
break;
|
||||
}
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
|
||||
i64 val = S + A - ctx.tp_addr;
|
||||
check(val, 0, 1LL << 24);
|
||||
*(ul32 *)loc |= bits(val, 23, 12) << 10;
|
||||
break;
|
||||
}
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_LO12:
|
||||
check(S + A - ctx.tp_addr, 0, 1 << 12);
|
||||
*(ul32 *)loc |= bits(S + A - ctx.tp_addr, 11, 0) << 10;
|
||||
break;
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
|
||||
*(ul32 *)loc |= bits(S + A - ctx.tp_addr, 11, 0) << 10;
|
||||
break;
|
||||
case R_AARCH64_TLSGD_ADR_PAGE21: {
|
||||
i64 val = page(sym.get_tlsgd_addr(ctx) + A) - page(P);
|
||||
check(val, -(1LL << 32), 1LL << 32);
|
||||
write_adrp(loc, val);
|
||||
break;
|
||||
}
|
||||
case R_AARCH64_TLSGD_ADD_LO12_NC:
|
||||
*(ul32 *)loc |= bits(sym.get_tlsgd_addr(ctx) + A, 11, 0) << 10;
|
||||
break;
|
||||
case R_AARCH64_TLSDESC_ADR_PAGE21:
|
||||
// ARM64 TLSDESC uses the following code sequence to materialize
|
||||
// a TP-relative address in x0.
|
||||
//
|
||||
// adrp x0, 0
|
||||
// R_AARCH64_TLSDESC_ADR_PAGE21 foo
|
||||
// ldr x1, [x0]
|
||||
// R_AARCH64_TLSDESC_LD64_LO12 foo
|
||||
// add x0, x0, #0
|
||||
// R_AARCH64_TLSDESC_ADD_LO12 foo
|
||||
// blr x1
|
||||
// R_AARCH64_TLSDESC_CALL foo
|
||||
//
|
||||
// We may relax the instructions to the following if its TP-relative
|
||||
// address is known at link-time
|
||||
//
|
||||
// nop
|
||||
// nop
|
||||
// movz x0, :tls_offset_hi:foo, lsl #16
|
||||
// movk x0, :tls_offset_lo:foo
|
||||
//
|
||||
// or to the following if the TP-relative address is known at
|
||||
// process startup time.
|
||||
//
|
||||
// nop
|
||||
// nop
|
||||
// adrp x0, :gottprel:foo
|
||||
// ldr x0, [x0, :gottprel_lo12:foo]
|
||||
if (sym.has_tlsdesc(ctx)) {
|
||||
i64 val = page(sym.get_tlsdesc_addr(ctx) + A) - page(P);
|
||||
check(val, -(1LL << 32), 1LL << 32);
|
||||
write_adrp(loc, val);
|
||||
} else {
|
||||
*(ul32 *)loc = 0xd503'201f; // nop
|
||||
}
|
||||
break;
|
||||
case R_AARCH64_TLSDESC_LD64_LO12:
|
||||
if (sym.has_tlsdesc(ctx))
|
||||
*(ul32 *)loc |= bits(sym.get_tlsdesc_addr(ctx) + A, 11, 3) << 10;
|
||||
else
|
||||
*(ul32 *)loc = 0xd503'201f; // nop
|
||||
break;
|
||||
case R_AARCH64_TLSDESC_ADD_LO12:
|
||||
if (sym.has_tlsdesc(ctx)) {
|
||||
*(ul32 *)loc |= bits(sym.get_tlsdesc_addr(ctx) + A, 11, 0) << 10;
|
||||
} else if (sym.has_gottp(ctx)) {
|
||||
*(ul32 *)loc = 0x9000'0000; // adrp x0, 0
|
||||
write_adrp(loc, page(sym.get_gottp_addr(ctx) + A) - page(P));
|
||||
} else {
|
||||
*(ul32 *)loc = 0xd2a0'0000; // movz x0, 0, lsl #16
|
||||
*(ul32 *)loc |= bits(S + A - ctx.tp_addr, 32, 16) << 5;
|
||||
}
|
||||
break;
|
||||
case R_AARCH64_TLSDESC_CALL:
|
||||
if (sym.has_tlsdesc(ctx)) {
|
||||
// Do nothing
|
||||
} else if (sym.has_gottp(ctx)) {
|
||||
*(ul32 *)loc = 0xf940'0000; // ldr x0, [x0, 0]
|
||||
*(ul32 *)loc |= bits(sym.get_gottp_addr(ctx) + A, 11, 3) << 10;
|
||||
} else {
|
||||
*(ul32 *)loc = 0xf280'0000; // movk x0, 0
|
||||
*(ul32 *)loc |= bits(S + A - ctx.tp_addr, 15, 0) << 5;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
|
||||
std::span<const ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_NONE || record_undef_error(ctx, rel))
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
SectionFragment<E> *frag;
|
||||
i64 frag_addend;
|
||||
std::tie(frag, frag_addend) = get_fragment(ctx, rel);
|
||||
|
||||
u64 S = frag ? frag->get_addr(ctx) : sym.get_addr(ctx);
|
||||
u64 A = frag ? frag_addend : (i64)rel.r_addend;
|
||||
|
||||
auto check = [&](i64 val, i64 lo, i64 hi) {
|
||||
check_range(ctx, val, i, lo, hi);
|
||||
};
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_AARCH64_ABS64:
|
||||
if (std::optional<u64> val = get_tombstone(sym, frag))
|
||||
*(U64<E> *)loc = *val;
|
||||
else
|
||||
*(U64<E> *)loc = S + A;
|
||||
break;
|
||||
case R_AARCH64_ABS32:
|
||||
check(S + A, 0, 1LL << 32);
|
||||
*(U32<E> *)loc = S + A;
|
||||
break;
|
||||
default:
|
||||
Fatal(ctx) << *this << ": invalid relocation for non-allocated sections: "
|
||||
<< rel;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::scan_relocations(Context<E> &ctx) {
|
||||
assert(shdr().sh_flags & SHF_ALLOC);
|
||||
std::span<const ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
// Scan relocations
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_NONE || record_undef_error(ctx, rel))
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = (u8 *)(contents.data() + rel.r_offset);
|
||||
|
||||
if (sym.is_ifunc())
|
||||
sym.flags |= NEEDS_GOT | NEEDS_PLT;
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_AARCH64_MOVW_UABS_G3:
|
||||
scan_absrel(ctx, sym, rel);
|
||||
break;
|
||||
case R_AARCH64_ADR_GOT_PAGE:
|
||||
// An ADR_GOT_PAGE and GOT_LO12_NC relocation pair is used to load a
|
||||
// symbol's address from GOT. If the GOT value is a link-time
|
||||
// constant, we may be able to rewrite the ADRP+LDR instruction pair
|
||||
// with an ADRP+ADD, eliminating a GOT memory load.
|
||||
if (ctx.arg.relax && sym.is_pcrel_linktime_const(ctx) &&
|
||||
i + 1 < rels.size()) {
|
||||
// ADRP+LDR must be consecutive and use the same register to relax.
|
||||
const ElfRel<E> &rel2 = rels[i + 1];
|
||||
if (rel2.r_type == R_AARCH64_LD64_GOT_LO12_NC &&
|
||||
rel2.r_offset == rel.r_offset + 4 &&
|
||||
rel2.r_sym == rel.r_sym &&
|
||||
rel.r_addend == 0 &&
|
||||
rel2.r_addend == 0 &&
|
||||
is_adrp(loc) &&
|
||||
is_ldr(loc + 4)) {
|
||||
u32 rd = bits(*(ul32 *)loc, 4, 0);
|
||||
u32 rn = bits(*(ul32 *)(loc + 4), 9, 5);
|
||||
u32 rt = bits(*(ul32 *)(loc + 4), 4, 0);
|
||||
if (rd == rn && rn == rt) {
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
sym.flags |= NEEDS_GOT;
|
||||
break;
|
||||
case R_AARCH64_LD64_GOT_LO12_NC:
|
||||
case R_AARCH64_LD64_GOTPAGE_LO15:
|
||||
sym.flags |= NEEDS_GOT;
|
||||
break;
|
||||
case R_AARCH64_CALL26:
|
||||
case R_AARCH64_JUMP26:
|
||||
case R_AARCH64_PLT32:
|
||||
if (sym.is_imported)
|
||||
sym.flags |= NEEDS_PLT;
|
||||
break;
|
||||
case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
|
||||
case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
|
||||
sym.flags |= NEEDS_GOTTP;
|
||||
break;
|
||||
case R_AARCH64_ADR_PREL_PG_HI21:
|
||||
case R_AARCH64_ADR_PREL_PG_HI21_NC:
|
||||
scan_pcrel(ctx, sym, rel);
|
||||
break;
|
||||
case R_AARCH64_TLSGD_ADR_PAGE21:
|
||||
sym.flags |= NEEDS_TLSGD;
|
||||
break;
|
||||
case R_AARCH64_TLSDESC_CALL:
|
||||
scan_tlsdesc(ctx, sym);
|
||||
break;
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G2:
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_LO12:
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
|
||||
check_tlsle(ctx, sym, rel);
|
||||
break;
|
||||
case R_AARCH64_ABS64:
|
||||
case R_AARCH64_ADD_ABS_LO12_NC:
|
||||
case R_AARCH64_ADR_PREL_LO21:
|
||||
case R_AARCH64_CONDBR19:
|
||||
case R_AARCH64_LD_PREL_LO19:
|
||||
case R_AARCH64_LDST16_ABS_LO12_NC:
|
||||
case R_AARCH64_LDST32_ABS_LO12_NC:
|
||||
case R_AARCH64_LDST64_ABS_LO12_NC:
|
||||
case R_AARCH64_LDST128_ABS_LO12_NC:
|
||||
case R_AARCH64_LDST8_ABS_LO12_NC:
|
||||
case R_AARCH64_MOVW_UABS_G0:
|
||||
case R_AARCH64_MOVW_UABS_G0_NC:
|
||||
case R_AARCH64_MOVW_UABS_G1:
|
||||
case R_AARCH64_MOVW_UABS_G1_NC:
|
||||
case R_AARCH64_MOVW_UABS_G2:
|
||||
case R_AARCH64_MOVW_UABS_G2_NC:
|
||||
case R_AARCH64_PREL16:
|
||||
case R_AARCH64_PREL32:
|
||||
case R_AARCH64_PREL64:
|
||||
case R_AARCH64_TLSGD_ADD_LO12_NC:
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G0:
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G1:
|
||||
case R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
|
||||
case R_AARCH64_TLSLE_ADD_TPREL_HI12:
|
||||
case R_AARCH64_TLSDESC_ADR_PAGE21:
|
||||
case R_AARCH64_TLSDESC_LD64_LO12:
|
||||
case R_AARCH64_TLSDESC_ADD_LO12:
|
||||
break;
|
||||
default:
|
||||
Error(ctx) << *this << ": unknown relocation: " << rel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void Thunk<E>::copy_buf(Context<E> &ctx) {
|
||||
constexpr ul32 insn[] = {
|
||||
0x9000'0010, // adrp x16, 0 # R_AARCH64_ADR_PREL_PG_HI21
|
||||
0x9100'0210, // add x16, x16 # R_AARCH64_ADD_ABS_LO12_NC
|
||||
0xd61f'0200, // br x16
|
||||
0xd420'7d00, // brk
|
||||
};
|
||||
|
||||
static_assert(E::thunk_size == sizeof(insn));
|
||||
|
||||
u8 *buf = ctx.buf + output_section.shdr.sh_offset + offset;
|
||||
u64 P = output_section.shdr.sh_addr + offset;
|
||||
|
||||
for (Symbol<E> *sym : symbols) {
|
||||
u64 S = sym->get_addr(ctx);
|
||||
memcpy(buf, insn, E::thunk_size);
|
||||
write_adrp(buf, page(S) - page(P));
|
||||
*(ul32 *)(buf + 4) |= bits(S, 11, 0) << 10;
|
||||
|
||||
buf += E::thunk_size;
|
||||
P += E::thunk_size;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
|
||||
#endif
|
||||
@@ -1,598 +0,0 @@
|
||||
// i386 is similar to x86-64 but lacks PC-relative memory access
|
||||
// instructions. So it's not straightforward to support position-
|
||||
// independent code (PIC) on that target.
|
||||
//
|
||||
// If an object file is compiled with -fPIC, a function that needs to load
|
||||
// a value from memory first obtains its own address with the following
|
||||
// code
|
||||
//
|
||||
// call __x86.get_pc_thunk.bx
|
||||
//
|
||||
// where __x86.get_pc_thunk.bx is defined as
|
||||
//
|
||||
// __x86.get_pc_thunk.bx:
|
||||
// mov (%esp), %ebx # move the return address to %ebx
|
||||
// ret
|
||||
//
|
||||
// . With the function's own address (or, more precisely, the address
|
||||
// immediately after the call instruction), the function can compute an
|
||||
// absolute address of a variable with its address + link-time constant.
|
||||
//
|
||||
// Executing call-mov-ret isn't very cheap, and allocating one register to
|
||||
// store PC isn't cheap too, especially given that i386 has only 8
|
||||
// general-purpose registers. But that's the cost of PIC on i386. You need
|
||||
// to pay it when creating a .so and a position-independent executable.
|
||||
//
|
||||
// When a position-independent function calls another function, it sets
|
||||
// %ebx to the address of .got. Position-independent PLT entries use that
|
||||
// register to load values from .got.plt/.got.
|
||||
//
|
||||
// If we are creating a position-dependent executable (PDE), we can't
|
||||
// assume that %ebx is set to .got. For PDE, we need to create position-
|
||||
// dependent PLT entries which don't use %ebx.
|
||||
//
|
||||
// https://github.com/rui314/psabi/blob/main/i386.pdf
|
||||
|
||||
#include "mold.h"
|
||||
|
||||
namespace mold {
|
||||
|
||||
using E = I386;
|
||||
|
||||
template <>
|
||||
i64 get_addend(u8 *loc, const ElfRel<E> &rel) {
|
||||
switch (rel.r_type) {
|
||||
case R_386_8:
|
||||
case R_386_PC8:
|
||||
return *loc;
|
||||
case R_386_16:
|
||||
case R_386_PC16:
|
||||
return *(ul16 *)loc;
|
||||
case R_386_32:
|
||||
case R_386_PC32:
|
||||
case R_386_GOT32:
|
||||
case R_386_GOT32X:
|
||||
case R_386_PLT32:
|
||||
case R_386_GOTOFF:
|
||||
case R_386_GOTPC:
|
||||
case R_386_TLS_LDM:
|
||||
case R_386_TLS_GOTIE:
|
||||
case R_386_TLS_LE:
|
||||
case R_386_TLS_IE:
|
||||
case R_386_TLS_GD:
|
||||
case R_386_TLS_LDO_32:
|
||||
case R_386_SIZE32:
|
||||
case R_386_TLS_GOTDESC:
|
||||
return *(ul32 *)loc;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_addend(u8 *loc, i64 val, const ElfRel<E> &rel) {
|
||||
switch (rel.r_type) {
|
||||
case R_386_NONE:
|
||||
break;
|
||||
case R_386_8:
|
||||
case R_386_PC8:
|
||||
*loc = val;
|
||||
break;
|
||||
case R_386_16:
|
||||
case R_386_PC16:
|
||||
*(ul16 *)loc = val;
|
||||
break;
|
||||
case R_386_32:
|
||||
case R_386_PC32:
|
||||
case R_386_GOT32:
|
||||
case R_386_GOT32X:
|
||||
case R_386_PLT32:
|
||||
case R_386_GOTOFF:
|
||||
case R_386_GOTPC:
|
||||
case R_386_TLS_LDM:
|
||||
case R_386_TLS_GOTIE:
|
||||
case R_386_TLS_LE:
|
||||
case R_386_TLS_IE:
|
||||
case R_386_TLS_GD:
|
||||
case R_386_TLS_LDO_32:
|
||||
case R_386_SIZE32:
|
||||
case R_386_TLS_GOTDESC:
|
||||
*(ul32 *)loc = val;
|
||||
break;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_plt_header(Context<E> &ctx, u8 *buf) {
|
||||
if (ctx.arg.pic) {
|
||||
static const u8 insn[] = {
|
||||
0x51, // push %ecx
|
||||
0x8d, 0x8b, 0, 0, 0, 0, // lea GOTPLT+4(%ebx), %ecx
|
||||
0xff, 0x31, // push (%ecx)
|
||||
0xff, 0x61, 0x04, // jmp *0x4(%ecx)
|
||||
0xcc, 0xcc, 0xcc, 0xcc, // (padding)
|
||||
};
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
*(ul32 *)(buf + 3) = ctx.gotplt->shdr.sh_addr - ctx.got->shdr.sh_addr + 4;
|
||||
} else {
|
||||
static const u8 insn[] = {
|
||||
0x51, // push %ecx
|
||||
0xb9, 0, 0, 0, 0, // mov GOTPLT+4, %ecx
|
||||
0xff, 0x31, // push (%ecx)
|
||||
0xff, 0x61, 0x04, // jmp *0x4(%ecx)
|
||||
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // (padding)
|
||||
};
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
*(ul32 *)(buf + 2) = ctx.gotplt->shdr.sh_addr + 4;
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_plt_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
|
||||
if (ctx.arg.pic) {
|
||||
static const u8 insn[] = {
|
||||
0xb9, 0, 0, 0, 0, // mov $reloc_offset, %ecx
|
||||
0xff, 0xa3, 0, 0, 0, 0, // jmp *foo@GOT(%ebx)
|
||||
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // (padding)
|
||||
};
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
*(ul32 *)(buf + 1) = sym.get_plt_idx(ctx) * sizeof(ElfRel<E>);
|
||||
*(ul32 *)(buf + 7) = sym.get_gotplt_addr(ctx) - ctx.got->shdr.sh_addr;
|
||||
} else {
|
||||
static const u8 insn[] = {
|
||||
0xb9, 0, 0, 0, 0, // mov $reloc_offset, %ecx
|
||||
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
|
||||
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // (padding)
|
||||
};
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
*(ul32 *)(buf + 1) = sym.get_plt_idx(ctx) * sizeof(ElfRel<E>);
|
||||
*(ul32 *)(buf + 7) = sym.get_gotplt_addr(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void write_pltgot_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym) {
|
||||
if (ctx.arg.pic) {
|
||||
static const u8 insn[] = {
|
||||
0xff, 0xa3, 0, 0, 0, 0, // jmp *foo@GOT(%ebx)
|
||||
0xcc, 0xcc, // (padding)
|
||||
};
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
*(ul32 *)(buf + 2) = sym.get_got_pltgot_addr(ctx) - ctx.got->shdr.sh_addr;
|
||||
} else {
|
||||
static const u8 insn[] = {
|
||||
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
|
||||
0xcc, 0xcc, // (padding)
|
||||
};
|
||||
memcpy(buf, insn, sizeof(insn));
|
||||
*(ul32 *)(buf + 2) = sym.get_got_pltgot_addr(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
|
||||
u64 offset, u64 val) {
|
||||
u8 *loc = ctx.buf + this->shdr.sh_offset + offset;
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_NONE:
|
||||
break;
|
||||
case R_386_32:
|
||||
*(ul32 *)loc = val;
|
||||
break;
|
||||
case R_386_PC32:
|
||||
*(ul32 *)loc = val - this->shdr.sh_addr - offset;
|
||||
break;
|
||||
default:
|
||||
Fatal(ctx) << "unsupported relocation in .eh_frame: " << rel;
|
||||
}
|
||||
}
|
||||
|
||||
static u32 relax_got32x(u8 *loc) {
|
||||
// mov imm(%reg1), %reg2 -> lea imm(%reg1), %reg2
|
||||
if (loc[0] == 0x8b)
|
||||
return 0x8d00 | loc[1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Relax GD to LE
|
||||
static void relax_gd_to_le(u8 *loc, ElfRel<E> rel, u64 val) {
|
||||
static const u8 insn[] = {
|
||||
0x65, 0xa1, 0, 0, 0, 0, // mov %gs:0, %eax
|
||||
0x81, 0xc0, 0, 0, 0, 0, // add $tp_offset, %eax
|
||||
};
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_386_PLT32:
|
||||
case R_386_PC32:
|
||||
memcpy(loc - 3, insn, sizeof(insn));
|
||||
*(ul32 *)(loc + 5) = val;
|
||||
break;
|
||||
case R_386_GOT32:
|
||||
case R_386_GOT32X:
|
||||
memcpy(loc - 2, insn, sizeof(insn));
|
||||
*(ul32 *)(loc + 6) = val;
|
||||
break;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
// Relax LD to LE
|
||||
static void relax_ld_to_le(u8 *loc, ElfRel<E> rel, u64 tls_size) {
|
||||
switch (rel.r_type) {
|
||||
case R_386_PLT32:
|
||||
case R_386_PC32: {
|
||||
static const u8 insn[] = {
|
||||
0x65, 0xa1, 0, 0, 0, 0, // mov %gs:0, %eax
|
||||
0x2d, 0, 0, 0, 0, // sub $tls_size, %eax
|
||||
};
|
||||
memcpy(loc - 2, insn, sizeof(insn));
|
||||
*(ul32 *)(loc + 5) = tls_size;
|
||||
break;
|
||||
}
|
||||
case R_386_GOT32:
|
||||
case R_386_GOT32X: {
|
||||
static const u8 insn[] = {
|
||||
0x65, 0xa1, 0, 0, 0, 0, // mov %gs:0, %eax
|
||||
0x81, 0xe8, 0, 0, 0, 0, // sub $tls_size, %eax
|
||||
};
|
||||
memcpy(loc - 2, insn, sizeof(insn));
|
||||
*(ul32 *)(loc + 6) = tls_size;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_NONE)
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
u64 S = sym.get_addr(ctx);
|
||||
u64 A = get_addend(*this, rel);
|
||||
u64 P = get_addr() + rel.r_offset;
|
||||
u64 G = sym.get_got_idx(ctx) * sizeof(Word<E>);
|
||||
u64 GOT = ctx.got->shdr.sh_addr;
|
||||
|
||||
auto check = [&](i64 val, i64 lo, i64 hi) {
|
||||
check_range(ctx, i, val, lo, hi);
|
||||
};
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_386_8:
|
||||
check(S + A, 0, 1 << 8);
|
||||
*loc = S + A;
|
||||
break;
|
||||
case R_386_16:
|
||||
check(S + A, 0, 1 << 16);
|
||||
*(ul16 *)loc = S + A;
|
||||
break;
|
||||
case R_386_32:
|
||||
break;
|
||||
case R_386_PC8:
|
||||
check(S + A - P, -(1 << 7), 1 << 7);
|
||||
*loc = S + A - P;
|
||||
break;
|
||||
case R_386_PC16:
|
||||
check(S + A - P, -(1 << 15), 1 << 15);
|
||||
*(ul16 *)loc = S + A - P;
|
||||
break;
|
||||
case R_386_PC32:
|
||||
case R_386_PLT32:
|
||||
*(ul32 *)loc = S + A - P;
|
||||
break;
|
||||
case R_386_GOT32:
|
||||
*(ul32 *)loc = G + A;
|
||||
break;
|
||||
case R_386_GOT32X:
|
||||
if (sym.has_got(ctx)) {
|
||||
*(ul32 *)loc = G + A;
|
||||
} else {
|
||||
u32 insn = relax_got32x(loc - 2);
|
||||
assert(insn);
|
||||
loc[-2] = insn >> 8;
|
||||
loc[-1] = insn;
|
||||
*(ul32 *)loc = S + A - GOT;
|
||||
}
|
||||
break;
|
||||
case R_386_GOTOFF:
|
||||
*(ul32 *)loc = S + A - GOT;
|
||||
break;
|
||||
case R_386_GOTPC:
|
||||
*(ul32 *)loc = GOT + A - P;
|
||||
break;
|
||||
case R_386_TLS_GOTIE:
|
||||
*(ul32 *)loc = sym.get_gottp_addr(ctx) + A - GOT;
|
||||
break;
|
||||
case R_386_TLS_LE:
|
||||
*(ul32 *)loc = S + A - ctx.tp_addr;
|
||||
break;
|
||||
case R_386_TLS_IE:
|
||||
*(ul32 *)loc = sym.get_gottp_addr(ctx) + A;
|
||||
break;
|
||||
case R_386_TLS_GD:
|
||||
if (sym.has_tlsgd(ctx))
|
||||
*(ul32 *)loc = sym.get_tlsgd_addr(ctx) + A - GOT;
|
||||
else
|
||||
relax_gd_to_le(loc, rels[++i], S - ctx.tp_addr);
|
||||
break;
|
||||
case R_386_TLS_LDM:
|
||||
if (ctx.got->has_tlsld(ctx))
|
||||
*(ul32 *)loc = ctx.got->get_tlsld_addr(ctx) + A - GOT;
|
||||
else
|
||||
relax_ld_to_le(loc, rels[++i], ctx.tp_addr - ctx.tls_begin);
|
||||
break;
|
||||
case R_386_TLS_LDO_32:
|
||||
*(ul32 *)loc = S + A - ctx.dtp_addr;
|
||||
break;
|
||||
case R_386_SIZE32:
|
||||
*(ul32 *)loc = sym.esym().st_size + A;
|
||||
break;
|
||||
case R_386_TLS_GOTDESC:
|
||||
// i386 TLSDESC uses the following code sequence to materialize
|
||||
// a TP-relative address in %eax.
|
||||
//
|
||||
// lea 0(%ebx), %eax
|
||||
// R_386_TLS_GOTDESC foo
|
||||
// call *(%eax)
|
||||
// R_386_TLS_DESC_CALL foo
|
||||
//
|
||||
// We may relax the instructions to the following if its TP-relative
|
||||
// address is known at link-time
|
||||
//
|
||||
// mov $foo@TPOFF, %eax
|
||||
// nop
|
||||
//
|
||||
// or to the following if the TP-relative address is known at
|
||||
// process startup time.
|
||||
//
|
||||
// mov foo@GOTTPOFF(%ebx), %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
|
||||
//
|
||||
// Note that the compiler always uses the local-exec TLS model
|
||||
// for -fno-pic, so TLSDESC code is always PIC (i.e. uses %ebx to
|
||||
// store the address of GOT.)
|
||||
if (sym.has_tlsdesc(ctx)) {
|
||||
*(ul32 *)loc = sym.get_tlsdesc_addr(ctx) + A - GOT;
|
||||
} else if (sym.has_gottp(ctx)) {
|
||||
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 {
|
||||
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;
|
||||
case R_386_TLS_DESC_CALL:
|
||||
if (!sym.has_tlsdesc(ctx)) {
|
||||
// call *(%eax) -> nop
|
||||
loc[0] = 0x66;
|
||||
loc[1] = 0x90;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
|
||||
std::span<const ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_NONE || record_undef_error(ctx, rel))
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = base + rel.r_offset;
|
||||
|
||||
SectionFragment<E> *frag;
|
||||
i64 frag_addend;
|
||||
std::tie(frag, frag_addend) = get_fragment(ctx, rel);
|
||||
|
||||
u64 S = frag ? frag->get_addr(ctx) : sym.get_addr(ctx);
|
||||
u64 A = frag ? frag_addend : get_addend(*this, rel);
|
||||
u64 GOT = ctx.got->shdr.sh_addr;
|
||||
|
||||
auto check = [&](i64 val, i64 lo, i64 hi) {
|
||||
check_range(ctx, val, i, lo, hi);
|
||||
};
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_386_8:
|
||||
check(S + A, 0, 1 << 8);
|
||||
*loc = S + A;
|
||||
break;
|
||||
case R_386_16:
|
||||
check(S + A, 0, 1 << 16);
|
||||
*(ul16 *)loc = S + A;
|
||||
break;
|
||||
case R_386_32:
|
||||
if (std::optional<u64> val = get_tombstone(sym, frag))
|
||||
*(ul32 *)loc = *val;
|
||||
else
|
||||
*(ul32 *)loc = S + A;
|
||||
break;
|
||||
case R_386_PC8:
|
||||
check(S + A, -(1 << 7), 1 << 7);
|
||||
*loc = S + A;
|
||||
break;
|
||||
case R_386_PC16:
|
||||
check(S + A, -(1 << 15), 1 << 15);
|
||||
*(ul16 *)loc = S + A;
|
||||
break;
|
||||
case R_386_PC32:
|
||||
*(ul32 *)loc = S + A;
|
||||
break;
|
||||
case R_386_GOTPC:
|
||||
*(ul32 *)loc = GOT + A;
|
||||
break;
|
||||
case R_386_GOTOFF:
|
||||
*(ul32 *)loc = S + A - GOT;
|
||||
break;
|
||||
case R_386_TLS_LDO_32:
|
||||
if (std::optional<u64> val = get_tombstone(sym, frag))
|
||||
*(ul32 *)loc = *val;
|
||||
else
|
||||
*(ul32 *)loc = S + A - ctx.dtp_addr;
|
||||
break;
|
||||
case R_386_SIZE32:
|
||||
*(ul32 *)loc = sym.esym().st_size + A;
|
||||
break;
|
||||
default:
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void InputSection<E>::scan_relocations(Context<E> &ctx) {
|
||||
assert(shdr().sh_flags & SHF_ALLOC);
|
||||
std::span<const ElfRel<E>> rels = get_rels(ctx);
|
||||
|
||||
// Scan relocations
|
||||
for (i64 i = 0; i < rels.size(); i++) {
|
||||
const ElfRel<E> &rel = rels[i];
|
||||
if (rel.r_type == R_NONE || record_undef_error(ctx, rel))
|
||||
continue;
|
||||
|
||||
Symbol<E> &sym = *file.symbols[rel.r_sym];
|
||||
u8 *loc = (u8 *)(contents.data() + rel.r_offset);
|
||||
|
||||
if (sym.is_ifunc())
|
||||
sym.flags |= NEEDS_GOT | NEEDS_PLT;
|
||||
|
||||
if (rel.r_type == R_386_TLS_GD || rel.r_type == R_386_TLS_LDM) {
|
||||
if (i + 1 == rels.size())
|
||||
Fatal(ctx) << *this << ": " << rel << " must be followed by PLT or GOT32";
|
||||
|
||||
if (u32 ty = rels[i + 1].r_type;
|
||||
ty != R_386_PLT32 && ty != R_386_PC32 &&
|
||||
ty != R_386_GOT32 && ty != R_386_GOT32X)
|
||||
Fatal(ctx) << *this << ": " << rel << " must be followed by PLT or GOT32";
|
||||
}
|
||||
|
||||
switch (rel.r_type) {
|
||||
case R_386_8:
|
||||
case R_386_16:
|
||||
scan_absrel(ctx, sym, rel);
|
||||
break;
|
||||
case R_386_PC8:
|
||||
case R_386_PC16:
|
||||
case R_386_PC32:
|
||||
scan_pcrel(ctx, sym, rel);
|
||||
break;
|
||||
case R_386_GOT32:
|
||||
case R_386_GOTPC:
|
||||
sym.flags |= NEEDS_GOT;
|
||||
break;
|
||||
case R_386_GOT32X:
|
||||
// We always want to relax GOT32X even if --no-relax is given
|
||||
// because static PIE doesn't work without it.
|
||||
if (sym.is_pcrel_linktime_const(ctx) && relax_got32x(loc - 2)) {
|
||||
// Do nothing
|
||||
} else {
|
||||
sym.flags |= NEEDS_GOT;
|
||||
}
|
||||
break;
|
||||
case R_386_PLT32:
|
||||
if (sym.is_imported)
|
||||
sym.flags |= NEEDS_PLT;
|
||||
break;
|
||||
case R_386_TLS_GOTIE:
|
||||
case R_386_TLS_IE:
|
||||
sym.flags |= NEEDS_GOTTP;
|
||||
break;
|
||||
case R_386_TLS_GD:
|
||||
// We always relax if -static because libc.a doesn't contain
|
||||
// __tls_get_addr().
|
||||
if (ctx.arg.static_ || (ctx.arg.relax && sym.is_tprel_linktime_const(ctx)))
|
||||
i++;
|
||||
else
|
||||
sym.flags |= NEEDS_TLSGD;
|
||||
break;
|
||||
case R_386_TLS_LDM:
|
||||
// We always relax if -static because libc.a doesn't contain
|
||||
// __tls_get_addr().
|
||||
if (ctx.arg.static_ || (ctx.arg.relax && !ctx.arg.shared))
|
||||
i++;
|
||||
else
|
||||
ctx.needs_tlsld = true;
|
||||
break;
|
||||
case R_386_TLS_GOTDESC:
|
||||
scan_tlsdesc(ctx, sym);
|
||||
break;
|
||||
case R_386_TLS_LE:
|
||||
check_tlsle(ctx, sym, rel);
|
||||
break;
|
||||
case R_386_32:
|
||||
case R_386_GOTOFF:
|
||||
case R_386_TLS_LDO_32:
|
||||
case R_386_SIZE32:
|
||||
case R_386_TLS_DESC_CALL:
|
||||
break;
|
||||
default:
|
||||
Error(ctx) << *this << ": unknown relocation: " << rel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mold
|
||||
File diff suppressed because it is too large.
Load diff
Loaded 100 of 2632 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user