elftoolchain

This commit is contained in:
cowmonk 2025-11-15 13:08:46 -07:00
parent 58d098dfae
commit a9a3077633
16 changed files with 260 additions and 14 deletions

4
.gitignore vendored
View file

@ -1,2 +1,2 @@
./sources/ /sources/
./build/ /build/

View file

@ -1,13 +1,15 @@
IS_MAKE=1
include config.mk include config.mk
all: all: build
pull: pull:
@if [ ! -d ./sources ]; then \ @if [ ! -d ./sources ]; then \
echo "Sources Directory Not Found!"; \ echo "Sources Directory Not Found!"; \
mkdir -p ./sources; \ mkdir -p ./sources; \
echo "Pulling Sources..."; \ echo "Pulling Sources..."; \
xargs -a ${SOURCES_FILE} -n1 -P4 -- curl -L -S -O --output-dir ./sources || { echo "curl failed"; exit 1; }; \ xargs -a ${SOURCES_FILE} -n1 -P4 -- curl -f -L -S -O --output-dir ./sources || { echo "curl failed"; exit 1; }; \
echo "done"; \ echo "done"; \
else \ else \
./scripts/check.sh; \ ./scripts/check.sh; \
@ -15,15 +17,32 @@ pull:
build: init build: init
./scripts/musl-cross.sh ./scripts/musl-cross.sh
./scripts/elftoolchain-cross.sh
init: init:
@if [ ! -d ./build/${FROOT} ]; then \ @if [ ! -d ./build/${FROOT} ]; then \
echo "Making toolchain directory..."; \ @echo "Making toolchain directory..."; \
mkdir -p ./build/${FROOT}; \ mkdir -p ./build/${FROOT}; \
@echo "Making cross directory"; \
mkdir -p ./build/cross; \
fi fi
@echo "updating configuration based on config.mk..."
cp -r config.mk ./scripts/config.sh
sed -i 's/shell //g' ./scripts/config.sh
clean: clean:
rm -rf ./sources rm -rf ./sources
rm -rf ./build rm -rf ./build
.PHONY: all pull build clean init help:
@echo "musllvm"
@echo "-----------------------"
@echo "commands:"
@echo " all - make all (duh)"
@echo " pull - pull all sources (on first run), second run will check indivdually"
@echo " build - build will build to toolchain"
@echo " clean - clean up all sources and build artifacts"
@echo " init - sets up everything, run also every time you make changes to config.mk"
@echo " help - shows this menu"
.PHONY: all pull build clean init help

View file

@ -8,8 +8,10 @@ Here are some dependencies required to build this toolchain:
- a C compiler (preferably clang) - a C compiler (preferably clang)
- curl - curl
- make - make
- bmake
- POSIX Unix utilities (GNU utilities are acceptable) - POSIX Unix utilities (GNU utilities are acceptable)
# Credits # Credits
* [Chimera linux](https://chimera-linux.org) - Majority of patches are sourced from chimera linux. * [Chimera linux](https://chimera-linux.org) - Majority of patches are sourced from chimera linux.
* [CMLFS](https://github.com/dslm4515/CMLFS) - Many of the build scripts were taken as reference for the musllvm toolchain itself * [CMLFS](https://github.com/dslm4515/CMLFS) - Many of the build scripts were taken as reference for the musllvm toolchain itself
* [Mussel](https://github.com/firasuke/mussel) - Best, shortest, fastest script that builds musl cross-compiler

View file

@ -1,5 +1,15 @@
# musllvm default flags # musllvm default flags
# --------------------------------
# feel free to change these however you like
#
SOURCES_FILE=./sources.list #
# these are shared between the Makefile and scripts
#
ARCH=$(shell uname -m) ARCH=$(shell uname -m)
SOURCES_FILE=./sources.list
FROOT="${ARCH}-musllvm" FROOT="${ARCH}-musllvm"
CROSS="./build/cross" # cross directory
# MAKEFLAGS to parse the amounto of cores used
MAKEFLAGS="-j$(shell nproc) -l$(shell nproc)"

View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2021 Daniel Kolesa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* This file is a part of Chimera Linux. It provides a version of the
* file that does not conflict with system-wide elf.h.
*/
#ifndef _SYS_ELFDEFINITIONS_H_
#define _SYS_ELFDEFINITIONS_H_
#include <elf.h>
typedef Elf32_auxv_t Elf32_Cap;
typedef Elf64_auxv_t Elf64_Cap;
#endif

View file

@ -0,0 +1,25 @@
From b287a2c2b779758a31dc3902a7daf63cc0e6727f Mon Sep 17 00:00:00 2001
From: Daniel Kolesa <daniel@octaforge.org>
Date: Wed, 23 Jun 2021 01:40:50 +0200
Subject: [PATCH 1/2] disable ld build
---
Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
index e8713982..cbfd9c59 100644
--- a/Makefile
+++ b/Makefile
@@ -31,7 +31,7 @@ SUBDIR += cxxfilt
SUBDIR += elfcopy
SUBDIR += elfdump
SUBDIR += findtextrel
-SUBDIR += ld
+#SUBDIR += ld
SUBDIR += nm
SUBDIR += readelf
SUBDIR += size
--
2.32.0

View file

@ -0,0 +1,26 @@
From fe335f0f01aabe08df740092306ecc107ff5d96d Mon Sep 17 00:00:00 2001
From: Michael Forney <mforney@mforney.org>
Date: Mon, 28 Jun 2021 18:23:22 -0700
Subject: [PATCH] Allow empty Elf_Data
---
libelf/elf_update.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/libelf/elf_update.c b/libelf/elf_update.c
index 3e19b78a..62f64fc1 100644
--- a/libelf/elf_update.c
+++ b/libelf/elf_update.c
@@ -815,6 +815,9 @@ _libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex)
LIBELF_PRIVATE(fillchar),
(size_t) (sh_off + d->d_off - (uint64_t) rc));
+ if (d->d_size == 0)
+ continue;
+
rc = (off_t) (sh_off + d->d_off);
assert(d->d_buf != NULL);
--
2.32.0

View file

@ -0,0 +1,14 @@
Using --as-needed here eliminates the libelf linkage,
which results in undefined symbols at runtime.
--- a/cxxfilt/Makefile
+++ b/cxxfilt/Makefile
@@ -8,7 +8,7 @@ SRCS= cxxfilt.c
WARNS?= 6
DPADD= ${LIBELFTC} ${LIBELF}
-LDADD= -lelftc -lelf
+LDADD= -Wl,--no-as-needed -lelftc -lelf
MAN1= c++filt.1

View file

@ -0,0 +1,11 @@
--- elftoolchain-f27fcce314b91b3dece6bee90949183f7a1e18b3/libelftc/make-toolchain-version 2022-05-06 06:22:42.000000000 -0500
+++ elftoolchain-0.7.1/libelftc/make-toolchain-version 2023-10-07 19:26:54.758190500 -0500
@@ -42,7 +42,7 @@
# - Otherwise, we use `git --describe'.
get_revision_string()
{
- v="unknown:unknown"
+ v="0.7.1:svn-20230416"
if [ -d CVS ]; then # Look for CVS (NetBSD).
v="cvs:unknown"
elif [ -d .svn ]; then # An SVN checkout (SourceForge or FreeBSD).

View file

@ -0,0 +1,39 @@
From 974e9f2d745c4c20a6f68b54e7f9c1d38078434c Mon Sep 17 00:00:00 2001
From: Daniel Kolesa <daniel@octaforge.org>
Date: Fri, 5 Nov 2021 22:21:54 +0100
Subject: [PATCH] use symlinks for binaries
---
ar/Makefile | 2 +-
elfcopy/Makefile | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/ar/Makefile b/ar/Makefile
index cfbaac3..ae49132 100644
--- a/ar/Makefile
+++ b/ar/Makefile
@@ -14,7 +14,7 @@ LDADD= -larchive -lelftc -lelf -lz
CFLAGS+=-I. -I${.CURDIR}
-LINKS= ${BINDIR}/ar ${BINDIR}/ranlib
+SYMLINKS= ${BINDIR}/ar ${BINDIR}/ranlib
EXTRA_TARGETS= ranlib
diff --git a/elfcopy/Makefile b/elfcopy/Makefile
index a73515a..ca6e03c 100644
--- a/elfcopy/Makefile
+++ b/elfcopy/Makefile
@@ -31,7 +31,7 @@ MLINKS= elfcopy.1 objcopy.1
NO_SHARED?= yes
-LINKS= ${BINDIR}/elfcopy ${BINDIR}/mcs \
+SYMLINKS= ${BINDIR}/elfcopy ${BINDIR}/mcs \
${BINDIR}/elfcopy ${BINDIR}/objcopy \
${BINDIR}/elfcopy ${BINDIR}/strip
--
2.33.1

View file

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
set -eu set -eu
SOURCES_FILE=./sources.list source ./scripts/config.sh
while IFS= read -r url; do \ while IFS= read -r url; do \
[ -z "$url" ] && continue; \ [ -z "$url" ] && continue; \
@ -10,6 +10,6 @@ if [ -e "./sources/$fname" ]; then \
printf "SKIP: %s (already exists)\n" "$fname"; \ printf "SKIP: %s (already exists)\n" "$fname"; \
else \ else \
printf "GET: %s -> %s\n" "$url" "$fname"; \ printf "GET: %s -> %s\n" "$url" "$fname"; \
curl -o "./sources/$fname" "$url" || { printf "ERROR: failed to download %s\n" "$url"; exit 1; }; \ curl -f -L -O -S --output-dir "./sources/" "$url" || { printf "ERROR: failed to download %s\n" "$url"; exit 1; }; \
fi; \ fi; \
done < "$SOURCES_FILE"; \ done < "$SOURCES_FILE"; \

15
scripts/config.sh Executable file
View file

@ -0,0 +1,15 @@
# musllvm default flags
# --------------------------------
# feel free to change these however you like
#
#
# these are shared between the Makefile and scripts
#
ARCH=$(uname -m)
SOURCES_FILE=./sources.list
FROOT="${ARCH}-musllvm"
CROSS="./build/cross" # cross directory
# MAKEFLAGS to parse the amounto of cores used
MAKEFLAGS="-j$(nproc) -l$(nproc)"

42
scripts/elftoolchain-cross.sh Executable file
View file

@ -0,0 +1,42 @@
#!/bin/sh
set -eu
source ./scripts/config.sh
ELF_TAR=$(ls ./sources/elftoolchain-*.tar.* 2>/dev/null | head -n 1 | xargs basename)
if [ -z "$ELF_TAR" ]; then
echo "ERR: No Musl tarball found in sources directory"
exit 1
fi
ELF_SOURCE="${ELF_TAR%%.tar.*}"
if command -v "clang" >/dev/null 2>&1 && command -v "lld" >/dev/null 2>&1; then
export CC="clang"
else
export CC="cc"
fi
if [ ! -d ./build/"$ELF_SOURCE" ]; then
echo "Extracting Elftoolchain tarball to ./build/"
tar -xpf ./sources/"$ELF_TAR" -C ./build
else
echo "Elftoolchain source already extracted."
fi
echo "Buliding musl headers"
if [ -f "./build/$ELF_SOURCE" ]; then
echo "ELFTOOLCHAIN already built, skipping..."
exit
fi
cd ./build/"$ELF_SOURCE"
echo "patching elftoolchain-cross..."
patch -Np1 < ../../patches/elftoolchain/*
echo "Installing elftoolchain-cross..."
bmake WITH_ADDITIONAL_DOCUMENTATION=no \
WITH_TESTS=no \
MANTARGET=man

View file

@ -1,8 +1,8 @@
#!/bin/sh #!/bin/sh
set -eu set -eu
ARCH=$(uname -m) source ./scripts/config.sh
FROOT="$ARCH-musllvm"
LINUX_TAR=$(ls ./sources/linux-*.tar.* 2>/dev/null | head -n 1 | xargs basename) LINUX_TAR=$(ls ./sources/linux-*.tar.* 2>/dev/null | head -n 1 | xargs basename)
if [ -z "$LINUX_TAR" ]; then if [ -z "$LINUX_TAR" ]; then
echo "ERR: No Linux tarball found in sources directory" echo "ERR: No Linux tarball found in sources directory"

View file

@ -1,8 +1,8 @@
#!/bin/sh #!/bin/sh
set -eu set -eu
ARCH=$(uname -m) source ./scripts/config.sh
FROOT="../$ARCH-musllvm" # make install in source dir
MUSL_TAR=$(ls ./sources/musl-*.tar.* 2>/dev/null | head -n 1 | xargs basename) MUSL_TAR=$(ls ./sources/musl-*.tar.* 2>/dev/null | head -n 1 | xargs basename)
if [ -z "$MUSL_TAR" ]; then if [ -z "$MUSL_TAR" ]; then
echo "ERR: No Musl tarball found in sources directory" echo "ERR: No Musl tarball found in sources directory"
@ -32,4 +32,7 @@ fi
cd ./build/"$MUSL_SOURCE" cd ./build/"$MUSL_SOURCE"
echo "Installing musl headers..." echo "Installing musl headers..."
DESTDIR=$FROOT make ARCH=$(uname -m) install-headers ./configure \
--prefix=/
DESTDIR=../../$CROSS make ARCH=$(uname -m) install-headers

View file

@ -2,6 +2,7 @@ https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.17.7.tar.xz
https://musl.libc.org/releases/musl-1.2.5.tar.gz https://musl.libc.org/releases/musl-1.2.5.tar.gz
https://pub.sortix.org/libz/libz-1.2.8.2025.03.07.tar.gz https://pub.sortix.org/libz/libz-1.2.8.2025.03.07.tar.gz
https://github.com/chimera-linux/libatomic-chimera/archive/refs/tags/v0.90.0.tar.gz https://github.com/chimera-linux/libatomic-chimera/archive/refs/tags/v0.90.0.tar.gz
https://sourceforge.net/projects/elftoolchain/files/Sources/elftoolchain-0.7.1/elftoolchain-0.7.1.tar.bz2
https://github.com/llvm/llvm-project/releases/download/llvmorg-21.1.2/llvm-21.1.2.src.tar.xz https://github.com/llvm/llvm-project/releases/download/llvmorg-21.1.2/llvm-21.1.2.src.tar.xz
https://github.com/llvm/llvm-project/releases/download/llvmorg-21.1.2/cmake-21.1.2.src.tar.xz https://github.com/llvm/llvm-project/releases/download/llvmorg-21.1.2/cmake-21.1.2.src.tar.xz
https://github.com/llvm/llvm-project/releases/download/llvmorg-21.1.2/third-party-21.1.2.src.tar.xz https://github.com/llvm/llvm-project/releases/download/llvmorg-21.1.2/third-party-21.1.2.src.tar.xz