From 8de17e387a501e57dc6b3ba13f74a1049ffe5246 Mon Sep 17 00:00:00 2001 From: Richard Wheeler Date: Sun, 25 Oct 2026 09:57:12 -0400 Subject: [PATCH] tcc: count only file-backed pages in chained-fixups page_count (#29744) On macOS 27, TinyCC's chained-fixups writer sizes the __DATA segment's page_count from vmsize, so the zero-fill __bss pages are listed as file-backed. dyld then maps them from the file instead of zero-filling, which gives garbage globals. Add a patch that counts ceil(filesize / SEG_PAGE_SIZE) pages and skips segments with no file-backed pages. Apply it after checkout of TCC_COMMIT in the arm64 TCC build script. The script configures TinyCC with --config-new_macho=no, so the patch is compiled but inactive for this bundle. It is kept as defense in depth for builds that keep chained fixups. The recorded TCC commit hash does not reflect the applied patch. Co-Authored-By: Claude Haiku 5.5 --- .../patches/tccmacho-page-count-filesize.diff | 47 +++++++++++++++++++ .../thirdparty-macos-arm64_tcc.sh | 9 ++++ 2 files changed, 56 insertions(+) create mode 100644 thirdparty/build_scripts/patches/tccmacho-page-count-filesize.diff diff --git a/thirdparty/build_scripts/patches/tccmacho-page-count-filesize.diff b/thirdparty/build_scripts/patches/tccmacho-page-count-filesize.diff new file mode 100644 index 00000000000000..53f172e00a12fd --- /dev/null +++ b/thirdparty/build_scripts/patches/tccmacho-page-count-filesize.diff @@ -0,0 +1,47 @@ +diff --git a/tccmacho.c b/tccmacho.c +index cee09fd7..ecd467da 100644 +--- a/tccmacho.c ++++ b/tccmacho.c +@@ -1272,6 +1272,15 @@ const struct { + #define N_SEGMENT (sizeof(all_segment)/sizeof(all_segment[0])) + + #ifdef CONFIG_NEW_MACHO ++/* Only the pages of a segment that are backed by the file can hold fixups. ++ dyld's page-in linking maps every page listed in a segment's chained starts ++ from the file, so listing the zero-fill tail (__bss) as well would fill it ++ with the bytes that follow the segment in the file, instead of zeros. */ ++static int fixup_page_count(struct segment_command_64 *seg) ++{ ++ return (seg->filesize + SEG_PAGE_SIZE - 1) / SEG_PAGE_SIZE; ++} ++ + static void calc_fixup_size(TCCState *s1, struct macho *mo) + { + int i, size; +@@ -1279,7 +1288,9 @@ static void calc_fixup_size(TCCState *s1, struct macho *mo) + size = (sizeof(struct dyld_chained_fixups_header) + 7) & -8; + size += (sizeof(struct dyld_chained_starts_in_image) + (mo->nseg - 1) * sizeof(uint32_t) + 7) & -8; + for (i = (s1->output_type == TCC_OUTPUT_EXE); i < mo->nseg - 1; i++) { +- int page_count = (get_segment(mo, i)->vmsize + SEG_PAGE_SIZE - 1) / SEG_PAGE_SIZE; ++ int page_count = fixup_page_count(get_segment(mo, i)); ++ if (page_count == 0) ++ continue; + size += (sizeof(struct dyld_chained_starts_in_segment) + (page_count - 1) * sizeof(uint16_t) + 7) & -8; + } + size += mo->n_bind * sizeof (struct dyld_chained_import) + 1; +@@ -2059,10 +2070,13 @@ ST_FUNC void bind_rebase_import(TCCState *s1, struct macho *mo) + data += (size + 7) & -8; + image->seg_count = mo->nseg; + for (i = (s1->output_type == TCC_OUTPUT_EXE); i < mo->nseg - 1; i++) { ++ seg = get_segment(mo, i); ++ page_count = fixup_page_count(seg); ++ /* No file-backed pages, so no fixups: an offset of 0 says so. */ ++ if (page_count == 0) ++ continue; + image->seg_info_offset[i] = (data - mo->chained_fixups->data) - + header->starts_offset; +- seg = get_segment(mo, i); +- page_count = (seg->vmsize + SEG_PAGE_SIZE - 1) / SEG_PAGE_SIZE; + size = sizeof(struct dyld_chained_starts_in_segment) + + (page_count - 1) * sizeof(uint16_t); + segment = (struct dyld_chained_starts_in_segment *) data; diff --git a/thirdparty/build_scripts/thirdparty-macos-arm64_tcc.sh b/thirdparty/build_scripts/thirdparty-macos-arm64_tcc.sh index 109d42bb1d9988..930b0cd5af7782 100755 --- a/thirdparty/build_scripts/thirdparty-macos-arm64_tcc.sh +++ b/thirdparty/build_scripts/thirdparty-macos-arm64_tcc.sh @@ -184,6 +184,15 @@ cd "$TINYCC_SOURCE" git checkout $TCC_COMMIT export TCC_COMMIT_FULL_HASH=$(git rev-parse HEAD) +## vlang/v#29744: with chained fixups, TCC lists the zero-fill __bss pages in __DATA's +## page_count, and macOS 27 dyld maps them from the file, which gives garbage globals. +## The patch counts only file-backed pages. It only affects builds that keep chained fixups, +## so the configure step below (--config-new_macho=no) does not use it. See vlang/v#29744. +git apply "$V_REPO_ROOT/thirdparty/build_scripts/patches/tccmacho-page-count-filesize.diff" || { + echo "tccmacho page_count patch failed to apply" >&2 + exit 2 +} + ### NB: the symlinks below are needed, to ensure proper support for bootstrapping tcc, otherwise backtraces will be disabled . for i in include/*.h; do echo $i; ln -s $i $(basename $i); done