[boring] Document and check the glibc requirement of the prebuilt Linux libraries

The prebuilt Linux libraries are built on Ubuntu 22.04, the oldest Ubuntu
that GitHub Actions has, so they require glibc 2.34, for the pthread functions
that moved into libc in 2.34. The README didn't say so, and the libraries are
picked by OS and architecture only, so they failed to load on older
distributions and on other C libraries, such as musl on Alpine Linux.

- List the requirement in the README.
- In the `fetch` build mode, when building for the Linux machine the hook runs
  on, check its C library with gnu_get_libc_version(), and fail with a hint to
  use `buildMode: checkout` on musl or glibc older than 2.34, instead of
  bundling a library that fails to load.
- Tighten the ABI check of the binaries workflow from 2.35 to 2.34, what the
  libraries need today, so the requirement can't silently grow.
diff --git a/.github/workflows/boring_binaries.yaml b/.github/workflows/boring_binaries.yaml
index 8ed7bda..33a220f 100644
--- a/.github/workflows/boring_binaries.yaml
+++ b/.github/workflows/boring_binaries.yaml
@@ -61,15 +61,17 @@
         # tool/precompile_binaries.dart.
         include:
           # The oldest available Ubuntu, so the libraries only require glibc
-          # 2.35.
+          # 2.34, for the pthread functions that moved into libc in 2.34. Keep
+          # `max-glibc` in sync with `prebuiltLinuxGlibc` in
+          # lib/src/hook_helpers/libc.dart.
           - os: ubuntu-22.04
             target-os: linux
             target-arch: x64
-            max-glibc: '2.35'
+            max-glibc: '2.34'
           - os: ubuntu-22.04-arm
             target-os: linux
             target-arch: arm64
-            max-glibc: '2.35'
+            max-glibc: '2.34'
           - os: macos-15
             target-os: macos
             target-arch: arm64
diff --git a/pkgs/boring/CHANGELOG.md b/pkgs/boring/CHANGELOG.md
index e0ed898..19df87a 100644
--- a/pkgs/boring/CHANGELOG.md
+++ b/pkgs/boring/CHANGELOG.md
@@ -1,5 +1,10 @@
 ## 0.5.1-wip
 
+- On Linux, the `fetch` build mode checks that the machine has glibc 2.34 or
+  later, which the prebuilt libraries require, and otherwise fails with a hint
+  to use `buildMode: checkout`, instead of bundling a library that fails to
+  load. This includes distributions with another C library, such as musl on
+  Alpine Linux. The README now lists the requirement.
 - When the Dart runtime uses a sanitizer (`asan`, `msan`, or `tsan`), the
   `fetch` and `checkout` build modes compile BoringSSL with it, using Clang.
   The prebuilt libraries aren't instrumented, so `fetch` builds from source.
diff --git a/pkgs/boring/README.md b/pkgs/boring/README.md
index 46e17c5..8b9e6c0 100644
--- a/pkgs/boring/README.md
+++ b/pkgs/boring/README.md
@@ -66,6 +66,12 @@
 `buildMode: checkout`, which requires CMake, Ninja, and a C toolchain (Clang or
 GCC, Xcode, MSVC, or the Android NDK), plus NASM on Windows.
 
+The prebuilt Linux libraries require glibc 2.34 or later, for example Ubuntu
+22.04, Debian 12, RHEL 9, Fedora 35, or Amazon Linux 2023. On older
+distributions, and on distributions with another C library, such as musl on
+Alpine Linux, the `fetch` build mode fails with a hint to use `buildMode:
+checkout` instead, which compiles BoringSSL for the C library of the machine.
+
 When the Dart runtime uses a sanitizer, for example with `dart test -p
 vm-asan`, the `fetch` build mode compiles BoringSSL from source with that
 sanitizer and Clang instead, because the prebuilt libraries aren't
diff --git a/pkgs/boring/lib/src/hook_helpers/fetch.dart b/pkgs/boring/lib/src/hook_helpers/fetch.dart
index 85c4407..94e9eb4 100644
--- a/pkgs/boring/lib/src/hook_helpers/fetch.dart
+++ b/pkgs/boring/lib/src/hook_helpers/fetch.dart
@@ -11,6 +11,7 @@
 import 'package:hooks/hooks.dart';
 
 import 'hashes.dart' show fileHashes, releaseRepository, releaseTag;
+import 'libc.dart' show currentGlibcVersion, unsupportedLinuxLibcReason;
 import 'targets.dart'
     show libraryFileName, releaseAssetName, releaseAssetUrl, targetTripleFor;
 
@@ -58,6 +59,18 @@
     );
   }
 
+  // The prebuilt libraries would fail to load on another C library. Only the
+  // C library of this machine is known, so this only checks builds for it, as
+  // in `dart run` and `dart test`.
+  if (targetOS == OS.linux &&
+      OS.current == OS.linux &&
+      targetArch == Architecture.current) {
+    final reason = unsupportedLinuxLibcReason(currentGlibcVersion());
+    if (reason != null) {
+      throw BuildError(message: '$reason\n$_checkoutHint');
+    }
+  }
+
   final fileName = libraryFileName(targetOS, static: static);
   final cachedFile = File.fromUri(
     input.outputDirectoryShared
diff --git a/pkgs/boring/lib/src/hook_helpers/libc.dart b/pkgs/boring/lib/src/hook_helpers/libc.dart
new file mode 100644
index 0000000..25ff678
--- /dev/null
+++ b/pkgs/boring/lib/src/hook_helpers/libc.dart
@@ -0,0 +1,58 @@
+// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:ffi';
+
+/// The oldest glibc that the prebuilt Linux libraries run on.
+///
+/// They're built on the oldest Ubuntu that GitHub Actions has, 22.04, and the
+/// pthread functions they use moved into libc in glibc 2.34. Keep in sync with
+/// `max-glibc` of the Linux targets in `.github/workflows/boring_binaries.yaml`,
+/// which checks the libraries, and with README.md.
+const prebuiltLinuxGlibc = (2, 34);
+
+/// The version of the GNU C library (glibc) of this process, or `null` if the
+/// process doesn't use glibc, for example on Alpine Linux, which uses musl.
+(int, int)? currentGlibcVersion() {
+  final process = DynamicLibrary.process();
+  if (!process.providesSymbol('gnu_get_libc_version')) {
+    return null;
+  }
+  final gnuGetLibcVersion = process
+      .lookupFunction<Pointer<Uint8> Function(), Pointer<Uint8> Function()>(
+        'gnu_get_libc_version',
+      );
+  final version = gnuGetLibcVersion();
+  final bytes = <int>[];
+  for (var i = 0; version[i] != 0; i++) {
+    bytes.add(version[i]);
+  }
+  return parseGlibcVersion(String.fromCharCodes(bytes));
+}
+
+/// Parses the major and minor version from a glibc version string, such as
+/// `2.35` or `2.39.9000`.
+(int, int)? parseGlibcVersion(String version) {
+  final match = RegExp(r'^(\d+)\.(\d+)').firstMatch(version);
+  if (match == null) {
+    return null;
+  }
+  return (int.parse(match[1]!), int.parse(match[2]!));
+}
+
+/// Why the prebuilt Linux libraries can't run with the C library
+/// [glibcVersion] (see [currentGlibcVersion]), or `null` if they can.
+String? unsupportedLinuxLibcReason((int, int)? glibcVersion) {
+  final (minMajor, minMinor) = prebuiltLinuxGlibc;
+  if (glibcVersion == null) {
+    return 'The prebuilt Linux libraries require glibc, but this system uses '
+        'another C library, such as musl on Alpine Linux.';
+  }
+  final (major, minor) = glibcVersion;
+  if (major < minMajor || (major == minMajor && minor < minMinor)) {
+    return 'The prebuilt Linux libraries require glibc $minMajor.$minMinor or '
+        'newer, but this system has glibc $major.$minor.';
+  }
+  return null;
+}
diff --git a/pkgs/boring/test/hook_helpers_test.dart b/pkgs/boring/test/hook_helpers_test.dart
index f906482..0984f34 100644
--- a/pkgs/boring/test/hook_helpers_test.dart
+++ b/pkgs/boring/test/hook_helpers_test.dart
@@ -7,6 +7,7 @@
 import 'package:boring/src/hook_helpers/build_options.dart';
 import 'package:boring/src/hook_helpers/fetch.dart';
 import 'package:boring/src/hook_helpers/hashes.dart';
+import 'package:boring/src/hook_helpers/libc.dart';
 import 'package:boring/src/hook_helpers/targets.dart';
 import 'package:code_assets/code_assets.dart';
 import 'package:hooks/hooks.dart';
@@ -136,6 +137,41 @@
     });
   });
 
+  group('libc.dart', () {
+    test('parses glibc versions', () {
+      expect(parseGlibcVersion('2.35'), equals((2, 35)));
+      expect(parseGlibcVersion('2.39.9000'), equals((2, 39)));
+      expect(parseGlibcVersion('unknown'), isNull);
+    });
+
+    test('accepts glibc from prebuiltLinuxGlibc on', () {
+      final (major, minor) = prebuiltLinuxGlibc;
+      expect(unsupportedLinuxLibcReason((major, minor)), isNull);
+      expect(unsupportedLinuxLibcReason((major, minor + 7)), isNull);
+      expect(unsupportedLinuxLibcReason((major + 1, 0)), isNull);
+    });
+
+    test('rejects older glibc and other C libraries', () {
+      final (major, minor) = prebuiltLinuxGlibc;
+      expect(
+        unsupportedLinuxLibcReason((major, minor - 1)),
+        contains('require glibc $major.$minor or newer'),
+      );
+      expect(unsupportedLinuxLibcReason(null), contains('musl'));
+    });
+
+    test('finds the glibc of this process', () {
+      final version = currentGlibcVersion();
+      if (Platform.isLinux) {
+        // The CI runners use glibc.
+        expect(version, isNotNull);
+        expect(unsupportedLinuxLibcReason(version), isNull);
+      } else {
+        expect(version, isNull);
+      }
+    });
+  });
+
   group('fetchPrebuiltLibrary', () {
     test('throws a BuildError when no prebuilt binary exists for target', () {
       final builder = BuildInputBuilder()