| 1 | ; Alpine 3.21 binutils source and patches, producing native Vinix ELF tools. |
| 2 | selected := get-environment-variable("VPAC_TARGET_ARCH") |
| 3 | host-arch := if memq(string->symbol("aarch64"), features()) { "aarch64" } else { "x86_64" } |
| 4 | arch := if string?(selected) { selected } else { host-arch } |
| 5 | libc := if string=?(arch, "aarch64") { "musl>=1.2.6-4" } else { "musl" } |
| 6 | |
| 7 | ; Libtool leaves a shell launcher in the build tree for dynamically linked |
| 8 | ; programs. Invoke the underlying target ELF with its freshly built DSOs. |
| 9 | tool := fn(dir, name) { |
| 10 | binary := string-append(dir, "/.libs/", name) |
| 11 | if file-exists?(binary) { binary } else { string-append(dir, "/", name) } |
| 12 | } |
| 13 | check-tool := fn(dir, name, args) { |
| 14 | apply(run, append(list("vinix-run", "--library-path", "bfd/.libs:opcodes/.libs:libctf/.libs:libsframe/.libs", tool(dir, name)), args)) |
| 15 | } |
| 16 | |
| 17 | defpkgbuild(binutils, |
| 18 | #:pkgname, "binutils", |
| 19 | #:pkgver, "2.43.1", |
| 20 | #:pkgrel, 5, |
| 21 | #:pkgdesc, "GNU assembler, linker, and binary utilities for Vinix", |
| 22 | #:arch, [#:x86-64, #:aarch64], |
| 23 | #:url, "https://www.gnu.org/software/binutils/", |
| 24 | #:license, ["GPL-2.0-or-later", "LGPL-2.1-or-later", "BSD-3-Clause"], |
| 25 | #:depends, list(libc, "zlib", "zstd", "jansson", "gcc-libs"), |
| 26 | #:source, ["https://distfiles.alpinelinux.org/distfiles/v3.21/binutils-2.43.1.tar.xz", |
| 27 | "https://raw.githubusercontent.com/alpinelinux/aports/a7de3e63cc8015474a4feadd8bb7c0ecc097b1fa/main/binutils/0001-Revert-PR25882-.gnu.attributes-are-not-checked-for-s.patch", |
| 28 | "https://raw.githubusercontent.com/alpinelinux/aports/a7de3e63cc8015474a4feadd8bb7c0ecc097b1fa/main/binutils/binutils-ld-fix-static-linking.patch", |
| 29 | "https://raw.githubusercontent.com/alpinelinux/aports/a7de3e63cc8015474a4feadd8bb7c0ecc097b1fa/main/binutils/CVE-2025-0840.patch", |
| 30 | "https://raw.githubusercontent.com/alpinelinux/aports/a7de3e63cc8015474a4feadd8bb7c0ecc097b1fa/main/binutils/CVE-2025-1153.patch", |
| 31 | "https://raw.githubusercontent.com/alpinelinux/aports/a7de3e63cc8015474a4feadd8bb7c0ecc097b1fa/main/binutils/CVE-2025-3198.patch"], |
| 32 | #:sha256sums, ["13f74202a3c4c51118b797a39ea4200d3f6cfbe224da6d1d95bb938480132dfd", |
| 33 | "faba7b07cf02de228fa383bcaca339f8e7b70dec10ee3a5e08af5a8b82bc2ae6", |
| 34 | "d5c5581d0ba04ef2e3690f6fb57435bf7ce343f2376fe972a2a693c5429eec9c", |
| 35 | "2ab7a15b40dc7da6b93da96d9ba28b65be89db3338d4481c056a4e98a6be7aa1", |
| 36 | "395450d49cc0059100d33622bc4e22f4c56f9313fd0c8a81c4facb6514536483", |
| 37 | "ffe8d968ef8bfc682421756d24ad465636a359bb0a10a66943647e726f780640"], |
| 38 | #:prepare, fn() { |
| 39 | if string=?(arch, "aarch64") { |
| 40 | ; The native Vinix AArch64 MMU uses 16 KiB pages. Set both BFD and |
| 41 | ; gold defaults so direct linker users produce loadable segments. |
| 42 | run("sed", "-i", "/^#define ELF_MAXPAGESIZE/s/0x10000/0x4000/; /^#define ELF_COMMONPAGESIZE/s/0x1000/0x4000/", |
| 43 | "binutils-2.43.1/bfd/elfnn-aarch64.c") |
| 44 | run("sed", "-i", "/abi_pagesize (overridable/s/0x10000/0x4000/; /common_pagesize (overridable/s/0x1000/0x4000/", |
| 45 | "binutils-2.43.1/gold/aarch64.cc") |
| 46 | } |
| 47 | for-each(fn(p) { run("patch", "--batch", "--fuzz=1", "-p1", "-d", "binutils-2.43.1", "-i", string-append("../", p)) }, |
| 48 | list("0001-Revert-PR25882-.gnu.attributes-are-not-checked-for-s.patch", |
| 49 | "binutils-ld-fix-static-linking.patch", "CVE-2025-0840.patch", "CVE-2025-1153.patch", "CVE-2025-3198.patch")) |
| 50 | }, |
| 51 | #:build, fn() { |
| 52 | prefix := get-environment-variable("CROSS_COMPILE") |
| 53 | tools := if string?(prefix) { prefix } else { "" } |
| 54 | ; When --target is omitted GNU configure uses --host, producing a |
| 55 | ; native assembler and linker for the requested Vinix architecture. |
| 56 | formats := if string=?(arch, "x86_64") { list("--enable-targets=x86_64-pep") } else { list() } |
| 57 | apply(run, append(list("env", string-append("AR=", tools, "ar"), string-append("RANLIB=", tools, "ranlib"), string-append("NM=", tools, "nm"), |
| 58 | string-append("AS=", tools, "as"), string-append("LD=", tools, "ld"), "CC_FOR_BUILD=gcc", "CFLAGS=-O2 -std=gnu17", |
| 59 | "CXXCPP=vinix-cc -E -x c++", "vinix-configure", "binutils-2.43.1/configure", |
| 60 | "--with-sysroot=/", "--with-system-zlib", "--with-zstd", "--enable-jansson", "--enable-gold", "--enable-ld=default", |
| 61 | "--disable-gdb", "--disable-gprofng", "--disable-sim", "--disable-nls", "--disable-werror", |
| 62 | "--disable-multilib", "--enable-libctf", "--enable-shared", "--enable-plugins", "--enable-install-libiberty", "--enable-64-bit-bfd", |
| 63 | "--enable-deterministic-archives", "--enable-colored-disassembly", "--enable-default-execstack=no", |
| 64 | "--enable-threads", "--with-pic", "--with-mmap", "--enable-new-dtags", "--enable-relro", "--enable-default-hash-style=gnu"), formats)) |
| 65 | ; Binutils configures its subdirectories during make. Use the same SDK |
| 66 | ; pkg-config paths that vinix-configure sets for the top-level script. |
| 67 | root := get-environment-variable("VPAC_MAKEPKG_ROOT") |
| 68 | run("env", string-append("PKG_CONFIG_SYSROOT_DIR=", root), |
| 69 | string-append("PKG_CONFIG_LIBDIR=", root, "/usr/lib/pkgconfig:", root, "/usr/share/pkgconfig"), |
| 70 | "PKG_CONFIG_PATH=", "make") |
| 71 | }, |
| 72 | #:check, fn() { |
| 73 | call-with-output-file("debug-fixture.c", fn(port) { display("struct record { int number; };\nint twice(struct record r) { return r.number * 2; }\n", port) }) |
| 74 | run("vinix-cc", "-gdwarf-4", "-gsplit-dwarf", "-c", "debug-fixture.c", "-o", "fixture.o") |
| 75 | check-tool("gold", "dwp", list("fixture.dwo", "-o", "fixture.dwp")) |
| 76 | check-tool("binutils", "objcopy", list("--dump-section", ".debug_cu_index=fixture.index", "fixture.dwp")) |
| 77 | run("test", "-s", "fixture.index") |
| 78 | run("vinix-cc", "-gctf", "-c", "debug-fixture.c", "-o", "ctf.o") |
| 79 | check-tool("binutils", "objdump", list("--ctf=.ctf", "ctf.o")) |
| 80 | check-tool("binutils", "objcopy", list("--compress-debug-sections=zstd", "fixture.o", "compressed.o")) |
| 81 | if string=?(arch, "x86_64") { |
| 82 | call-with-output-file("pe-start.s", fn(port) { display(".text\n.globl _start\n_start:\n xorl %eax, %eax\n ret\n", port) }) |
| 83 | for-each(fn(bits, format, emulation) { |
| 84 | check-tool("gas", "as-new", list(string-append("--", bits), "-o", "pe-start.o", "pe-start.s")) |
| 85 | check-tool("binutils", "objcopy", list("-O", format, "pe-start.o", "pe-start.obj")) |
| 86 | script := string-append("ld/ldscripts/", emulation, ".x") |
| 87 | check-tool("ld", "ld-new", list("-m", emulation, "-T", script, "--entry", "_start", "-o", "pe-start.exe", "pe-start.obj")) |
| 88 | check-tool("binutils", "objdump", list("-f", "pe-start.exe")) |
| 89 | }, list("32", "64"), list("pe-i386", "pe-x86-64"), list("i386pe", "i386pep")) |
| 90 | } else { |
| 91 | call-with-output-file("abi-start.s", fn(port) { display(".text\n.global _start\n_start:\n mov x0, #0\n mov x8, #93\n svc #0\n", port) }) |
| 92 | check-tool("gas", "as-new", list("-o", "abi-start.o", "abi-start.s")) |
| 93 | for-each(fn(dir, name) { |
| 94 | check-tool(dir, name, list("-o", "abi-start", "abi-start.o")) |
| 95 | run("vinix-elf-pages", "aarch64", "abi-start") |
| 96 | }, list("ld", "gold"), list("ld-new", "ld-new")) |
| 97 | } |
| 98 | }, |
| 99 | #:package, fn() { |
| 100 | run("make", string-append("DESTDIR=", pkgdir()), "install") |
| 101 | run("rm", "-f", string-append(pkgdir(), "/usr/share/info/dir")) |
| 102 | for-each(fn(la) { run("rm", la) }, glob(string-append(pkgdir(), "/usr/lib/*.la"))) |
| 103 | for-each(fn(lib) { run("test", "-f", string-append(pkgdir(), "/usr/lib/", lib)) }, |
| 104 | list("bfd-plugins/libdep.so", "libbfd-2.43.1.so", "libctf-nobfd.so.0", "libctf-nobfd.so.0.0.0", "libctf.so.0", "libctf.so.0.0.0", |
| 105 | "libopcodes-2.43.1.so", "libsframe.so.1", "libsframe.so.1.0.0")) |
| 106 | run("test", "-x", string-append(pkgdir(), "/usr/bin/dwp")) |
| 107 | if string=?(arch, "x86_64") { |
| 108 | for-each(fn(emulation) { |
| 109 | for-each(fn(suffix) { run("test", "-s", string-append(pkgdir(), "/usr/x86_64-linux-musl/lib/ldscripts/", emulation, suffix)) }, |
| 110 | list(".x", ".xa", ".xbn", ".xe", ".xer", ".xn", ".xr", ".xu")) |
| 111 | }, list("i386pe", "i386pep")) |
| 112 | } |
| 113 | run("install", "-Dm644", "binutils-2.43.1/COPYING3", string-append(pkgdir(), "/usr/share/licenses/binutils/COPYING3")) |
| 114 | run("find", string-append(pkgdir(), "/usr"), "-type", "d", "-exec", "chmod", "755", "{}", "+") |
| 115 | }) |
| 116 | |