>From eac69c817f628e153d3bcd40aa12bc4ab5bb4bfa Mon Sep 17 00:00:00 2001
From: Robert Dubner <[email protected]>
Date: Wed, 18 Nov 2026 20:17:51 -0500
Subject: [PATCH] cobol: Fix FUNCTION CHAR-NATIONAL; fix UNSTRING pointer
handling
The function __gg__char_national needed to be added to the export map.
It was also being told to assign its result to a numeric variable;
that's been changed to a national alphanumeric.
The UNSTRING function, when given a bad POINTER, was improperly changing
that value to 1. It is now being left alone.
gcc/cobol/ChangeLog:
* parse.y: CHAR-NATIONAL now assigns the result of
__gg__char_national to an alphanumeric value.
libgcobol/ChangeLog:
* libgcobol.map: __gg__char_national has been added to the map.
* unstring.cc (unstring_fixed): A bad input pointer is left
unchanged; the OVERFLOW flag is still being set.
* testsuite/perform-stack.cc: New test.
* testsuite/runpath.py: New test.
---
gcc/cobol/parse.y | 7 +-
libgcobol/libgcobol.map | 1 +
libgcobol/testsuite/perform-stack.cc | 168 +++++++++++++++++++++++++++
libgcobol/testsuite/runpath.py | 165 ++++++++++++++++++++++++++
libgcobol/unstring.cc | 9 +-
5 files changed, 343 insertions(+), 7 deletions(-)
create mode 100644 libgcobol/testsuite/perform-stack.cc
create mode 100644 libgcobol/testsuite/runpath.py
diff --git a/gcc/cobol/parse.y b/gcc/cobol/parse.y
index f14547c2070..ac30bae53ac 100644
--- a/gcc/cobol/parse.y
+++ b/gcc/cobol/parse.y
@@ -11516,6 +11516,11 @@ intrinsic: function_udf
$$ = new_alphanumeric("CHAR");
if( ! intrinsic_call_1($$, CHAR, $r1, @r1)) YYERROR;
}
+ | CHAR_NATIONAL '(' cexpr[r1] ')' {
+ location_set(@1);
+ $$ = new_alphanumeric("CHAR_NATIONAL",
current_encoding('N'));
+ if( ! intrinsic_call_1($$, CHAR, $r1, @r1)) YYERROR;
+ }
/* convert formulations:
* 1. ANY to ALNUM HEX, or NAT HEX
* 2. HEX to BYTE
@@ -12263,7 +12268,7 @@ intrinsic0: CURRENT_DATE {
intrinsic_I: BOOLEAN_OF_INTEGER { $$ = BOOLEAN_OF_INTEGER;
cbl_unimplemented("BOOLEAN-OF-INTEGER");
}
- | CHAR_NATIONAL { $$ = CHAR_NATIONAL; }
+// | CHAR_NATIONAL { $$ = CHAR_NATIONAL; }
| DATE_OF_INTEGER { $$ = DATE_OF_INTEGER; }
| DAY_OF_INTEGER { $$ = DAY_OF_INTEGER; }
| FACTORIAL { $$ = FACTORIAL; }
diff --git a/libgcobol/libgcobol.map b/libgcobol/libgcobol.map
index 599dfdb9007..859c683ea0f 100644
--- a/libgcobol/libgcobol.map
+++ b/libgcobol/libgcobol.map
@@ -25,6 +25,7 @@ GCOBOL_17.1
__gg__call_parameter_signature;
__gg__call_warning_message;
__gg__char;
+ __gg__char_national;
__gg__check_fatal_exception;
__gg__classify;
__gg__clear_exception;
diff --git a/libgcobol/testsuite/perform-stack.cc
b/libgcobol/testsuite/perform-stack.cc
new file mode 100644
index 00000000000..7f84cc42c93
--- /dev/null
+++ b/libgcobol/testsuite/perform-stack.cc
@@ -0,0 +1,168 @@
+/* Copyright (C) 2026 Free Software Foundation, Inc.
+ This file is part of GCC, distributed under the GNU General Public
License,
+ version 3 or (at your option) any later version. See COPYING3. */
+
+#include "../perform-stack.h"
+#include <functional>
+#include "../encodings.h"
+#undef NDEBUG
+#include <cassert>
+#include <cstdlib>
+#include <cstdint>
+#include <cstdio>
+#include <new>
+
+extern "C" void __gg__pseudo_return_push(void *, size_t);
+extern "C" size_t __gg__pseudo_return_pop();
+extern "C" void __gg__pseudo_return_bookmark();
+extern "C" size_t __gg__debug_pseudo_return_bookmark();
+extern "C" void __gg__pseudo_return_flush();
+extern "C" void __gg__init_program_state(cbl_encoding_t, cbl_encoding_t);
+extern "C" size_t __gg__unique_prog_id;
+extern "C" void *__gg__exit_address;
+extern "C" void __gg__module_name_push(const char *);
+extern "C" void __gg__push_program_state();
+
+
+/* Fail the second vector allocation once, after the signature push
succeeds.
+ The executable's replacement operator also serves allocations in
libgcobol. */
+static int fail_after = -1;
+void *operator new(size_t size)
+ {
+ if( fail_after == 0 )
+ {
+ fail_after = -1;
+ throw std::bad_alloc();
+ }
+ if( fail_after > 0 )
+ {
+ fail_after -= 1;
+ }
+ if( void *memory = std::malloc(size ? size : 1) )
+ {
+ return memory;
+ }
+ throw std::bad_alloc();
+ }
+void operator delete(void *memory) noexcept { std::free(memory); }
+void operator delete(void *memory, size_t) noexcept { std::free(memory);
}
+
+static void
+check(size_t depth, size_t invocations, uint32_t generation)
+ {
+ const auto &view = *__gg__debug_perform_stack;
+ assert(view.version == 1);
+ assert(view.byte_size == sizeof(view));
+ assert(view.word_size == sizeof(uintptr_t));
+ assert(view.generation == generation && !(generation & 1));
+ assert(view.signature_count == depth && view.index_count == depth);
+ assert(view.bookmark_count == invocations);
+ assert(!depth || (view.signatures && view.indexes));
+ assert(!invocations || view.bookmarks);
+ }
+
+int
+main()
+ {
+ uint32_t generation = __gg__debug_perform_stack->generation;
+ check(0, 0, generation);
+ assert(__gg__debug_pseudo_return_bookmark() == 1);
+ check(0, 1, generation += 2);
+ fail_after = 1;
+ bool failed = false;
+ try
+ {
+ __gg__pseudo_return_push(reinterpret_cast<void *>(0x1000), 7);
+ }
+ catch(const std::bad_alloc &)
+ {
+ failed = true;
+ }
+ assert(failed);
+ check(0, 1, generation += 2);
+
+ for( size_t i = 0; i < 2048; i += 1 )
+ {
+ __gg__pseudo_return_push(reinterpret_cast<void *>(0x1000 + i * 16),
i);
+ check(i + 1, 1, generation += 2);
+ auto *signatures = reinterpret_cast<void *const
*>(__gg__debug_perform_stack->signatures);
+ auto *indexes = reinterpret_cast<const size_t
*>(__gg__debug_perform_stack->indexes);
+ assert(signatures[i] == reinterpret_cast<void *>(0x1000 + i * 16));
+ assert(indexes[i] == i);
+ }
+ for( size_t i = 0; i < 512; i += 1 )
+ {
+ assert(__gg__debug_pseudo_return_bookmark() == i + 2);
+ check(2048, i + 2, generation += 2);
+ auto *bookmarks = reinterpret_cast<const size_t
*>(__gg__debug_perform_stack->bookmarks);
+ assert(bookmarks[i + 1] == 2048);
+ }
+ for( size_t i = 512; i > 0; i -= 1 )
+ {
+ __gg__pseudo_return_flush();
+ check(2048, i, generation += 2);
+ }
+ for( size_t i = 2048; i > 0; i -= 1 )
+ {
+ assert(__gg__pseudo_return_pop() == i - 1);
+ check(i - 1, 1, generation += 2);
+ }
+ __gg__pseudo_return_push(reinterpret_cast<void *>(0x9000), 99);
+ check(1, 1, generation += 2);
+ __gg__pseudo_return_bookmark();
+ check(1, 2, generation += 2);
+ __gg__pseudo_return_push(reinterpret_cast<void *>(0xa000), 100);
+ check(2, 2, generation += 2);
+ __gg__pseudo_return_flush();
+ check(1, 1, generation += 2);
+ assert(__gg__pseudo_return_pop() == 99);
+ check(0, 1, generation += 2);
+ __gg__pseudo_return_flush();
+ check(0, 0, generation += 2);
+
+ const_cast<gg_debug_perform_stack_v1
*>(__gg__debug_perform_stack)->generation = UINT32_MAX - 1;
+ __gg__pseudo_return_bookmark();
+ check(0, 1, 0);
+ __gg__pseudo_return_flush();
+ check(0, 0, 2);
+ __gg__init_program_state(iconv_CP1252_e, iconv_UTF_16LE_e);
+ int main_counter = 1;
+ int child_counter = 0;
+ __gg__unique_prog_id = 1;
+ assert(__gg__debug_pseudo_return_bookmark() == 1);
+ __gg__module_name_push("Tmain");
+ __gg__pseudo_return_push(reinterpret_cast<void *>(0x1000), 1);
+ __gg__push_program_state();
+ __gg__unique_prog_id += 1;
+ assert(__gg__debug_pseudo_return_bookmark() == 2);
+ __gg__module_name_push("Tchild");
+ __gg__pseudo_return_push(reinterpret_cast<void *>(0x2000), 2);
+ generation = __gg__debug_perform_stack->generation;
+ assert(__gg__debug_perform_unwind(0, 0, 0) == 1);
+ assert(__gg__debug_perform_unwind(3, 0, 0) == 1);
+ assert(__gg__debug_perform_unwind(1, 2, 0) == 1);
+ assert(__gg__debug_perform_unwind(1, 1, 1) == 1);
+ check(2, 2, generation);
+ assert(__gg__unique_prog_id == 2 && main_counter == 1);
+ assert(__gg__debug_perform_unwind(1, 0, 0, &child_counter) == 0);
+ check(0, 1, generation += 2);
+ assert(__gg__unique_prog_id == 1 && main_counter == 1);
+ assert(__gg__exit_address == nullptr);
+ __gg__pseudo_return_push(reinterpret_cast<void *>(0x3000), 3);
+ assert(__gg__debug_perform_unwind(1, 0, 1, &main_counter) == 0);
+ check(0, 0, generation += 4);
+ assert(__gg__unique_prog_id == 0 && main_counter == 0);
+ assert(__gg__exit_address == nullptr);
+
+ /* Invocation identities are reusable after forced return. An
unavailable
+ compiler counter rejects cleanup without changing published state.
*/
+ __gg__unique_prog_id = 1;
+ __gg__pseudo_return_bookmark();
+ __gg__module_name_push("Told");
+ generation = __gg__debug_perform_stack->generation;
+ assert(__gg__debug_perform_unwind(1, 0, 1, static_cast<int *>(nullptr))
== 2);
+ check(0, 1, generation);
+ __gg__pseudo_return_flush();
+ __gg__unique_prog_id = 0;
+ std::puts("PASS: PERFORM stack publication, relocation, rollback,
wraparound and forced return");
+ }
diff --git a/libgcobol/testsuite/runpath.py
b/libgcobol/testsuite/runpath.py
new file mode 100644
index 00000000000..ebe25768146
--- /dev/null
+++ b/libgcobol/testsuite/runpath.py
@@ -0,0 +1,165 @@
+#!/usr/bin/env python3
+# Copyright (C) 2026 Free Software Foundation, Inc.
+# This file is part of GCC, distributed under the GNU General Public
License,
+# version 3 or (at your option) any later version. See COPYING3.
+"""Native Linux RUNPATH tests; no third-party Python packages required.
+
+Without --gcobol, test the spec generator and ELF dependency semantics
with
+the system C compiler. With --gcobol, also test an installed patched
GCobol.
+Run installed tests in a clean container without a GCobol ld.so.conf
entry.
+"""
+
+import argparse
+import os
+from pathlib import Path
+import re
+import shutil
+import subprocess
+import tempfile
+
+
+def run(args, *, input=None, ok=True, env=None):
+ result = subprocess.run([str(a) for a in args], input=input,
text=True,
+ stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
+ env=env or clean_env)
+ if ok and result.returncode:
+ raise AssertionError(f"{args}\n{result.stdout}{result.stderr}")
+ return result
+
+
+def dynamic(path):
+ return run([args.readelf, "-d", path]).stdout
+
+
+def check_runpath(path, expected):
+ tags = dynamic(path)
+ assert "(RUNPATH)" in tags, tags
+ assert "(RPATH)" not in tags, tags
+ actual = re.search(r"\(RUNPATH\).*\[(.*)\]",
tags).group(1).split(":")
+ assert os.path.normpath(expected) in [os.path.normpath(p) for p in
actual], (expected, actual)
+
+
+def mechanism(root):
+ """Exercise real GCC spec expansion, GNU ELF tags and the dynamic
loader."""
+ # Include characters which have special meanings to specs and sed.
+ lib = root / "runtime space%&"
+ lib.mkdir()
+ source = root / "test.c"
+ source.write_text("int main(void) { return 0; }\n")
+ template = (src / "libgcobol.spec.in").read_text()
+ # Use the production configure expression, rather than a second copy.
+ config = (src / "configure.ac").read_text()
+ expression = re.search(r"LIBGCOBOL_RPATH_SPEC='([^']*)'",
config).group(1)
+ template = template.replace("@LIBGCOBOL_RPATH_SPEC@", expression)
+ template = template.replace("@LIBQUADSPEC@", "").replace("@LIBM@",
"")
+ spec = root / "test.spec"
+ spec.write_text(run(["sh", src / "mk-rpath-spec.sh", lib],
+ input=template).stdout)
+ exe = root / "hello"
+ run([args.cc, f"-specs={spec}", source, "-o", exe])
+ check_runpath(exe, lib)
+ run([exe])
+ print("PASS: generated specs preserve the runtime path and emit
DT_RUNPATH")
+
+ for option in ("-static", "-static-pie", "-nostdlib",
"-nodefaultlibs",
+ "-c", "-S", "-E", "-r"):
+ command = run([args.cc, f"-specs={spec}", option, "-###",
source])
+ assert "--enable-new-dtags" not in command.stderr, command.stderr
+ print("PASS: specs omit RUNPATH for
suppressed/default-library-free/static links")
+
+ # A unique library name prevents the system cache from masking a
missing
+ # transitive RUNPATH. Only the parent is on the executable's NEEDED
list.
+ leaf = root / "leaf.c"
+ leaf.write_text("int gcobol_runpath_leaf(void) { return 42; }\n")
+ parent = root / "parent.c"
+ parent.write_text("extern int gcobol_runpath_leaf(void);\n"
+ "int gcobol_runpath_parent(void) { return
gcobol_runpath_leaf(); }\n")
+ source.write_text("extern int gcobol_runpath_parent(void);\n"
+ "int main(void) { return gcobol_runpath_parent() !=
42; }\n")
+ run([args.cc, "-shared", "-fPIC", leaf, "-o", lib /
"libgcobol_rpath_leaf.so"])
+ parent_so = lib / "libgcobol_rpath_parent.so"
+ link_parent = [args.cc, "-shared", "-fPIC", parent, f"-L{lib}",
+ "-lgcobol_rpath_leaf", "-o", parent_so]
+ run(link_parent)
+ run([args.cc, f"-specs={spec}", source, f"-L{lib}",
+ "-lgcobol_rpath_parent", "-Xlinker", "-rpath-link", "-Xlinker",
lib,
+ "-o", exe])
+ assert "[libgcobol_rpath_leaf.so]" not in dynamic(exe)
+ result = run([exe], ok=False)
+ assert result.returncode != 0 and "libgcobol_rpath_leaf.so" in
result.stderr
+ run(link_parent + ["-Wl,--enable-new-dtags", "-Xlinker", "-rpath",
+ "-Xlinker", lib])
+ check_runpath(parent_so, lib)
+ run([exe])
+ print("PASS: a transitive private dependency needs and uses its
parent's RUNPATH")
+
+ # Environment overrides remain possible with RUNPATH (unlike old
RPATH).
+ override = root / "override"
+ override.mkdir()
+ leaf.write_text("int gcobol_runpath_leaf(void) { return 13; }\n")
+ run([args.cc, "-shared", "-fPIC", leaf, "-o", override /
"libgcobol_rpath_leaf.so"])
+ env = dict(clean_env, LD_LIBRARY_PATH=str(override))
+ assert run([exe], ok=False, env=env).returncode == 1
+ print("PASS: LD_LIBRARY_PATH can deliberately override DT_RUNPATH")
+
+
+def installed(root):
+ compiler = shutil.which(args.gcobol) or
str(Path(args.gcobol).resolve())
+ runtime = Path(args.runtime_dir).resolve()
+ source = root / "hello.cob"
+ source.write_text(' identification division.\n'
+ ' program-id. hello.\n'
+ ' procedure division.\n'
+ ' display "RUNPATH-OK"\n'
+ ' stop run.\n')
+ exe = root / "hello-cobol"
+ for options in ([], ["-lgcobol"], ["-dialect", "mf"],
["-static-libgcobol"]):
+ run([compiler, source, *options, "-o", exe])
+ check_runpath(exe, runtime)
+ assert run([exe]).stdout.strip() == "RUNPATH-OK"
+ print(f"PASS: installed compiler links and runs with {options}")
+ obj = root / "hello.o"
+ run([compiler, "-c", "-main", source, "-o", obj])
+ run([compiler, obj, "-o", exe])
+ check_runpath(exe, runtime)
+ assert run([exe]).stdout.strip() == "RUNPATH-OK"
+ print("PASS: separate compilation and object-only linking")
+ run([compiler, "-shared", "-fPIC", source, "-o", root / "hello.so"])
+ check_runpath(root / "hello.so", runtime)
+ print("PASS: generated shared libraries have their own RUNPATH")
+ for option in ("-c", "-S", "-E", "-r", "-nostdlib", "-nodefaultlibs",
+ "-static", "-static-pie"):
+ command = run([compiler, option, "-###", source])
+ assert "--enable-new-dtags" not in command.stderr, command.stderr
+ print("PASS: installed driver suppression and static-link cases")
+ for name in ("gcobol", "gcobol_posix", "gcobol_compat_gnu"):
+ check_runpath(runtime / f"lib{name}.so", runtime)
+ print("PASS: all three installed runtime DSOs have DT_RUNPATH")
+ # The ELF interpreter reports actual paths without executing the
program.
+ headers = run([args.readelf, "-l", exe]).stdout
+ loader = re.search(r"Requesting program interpreter: (.*?)\]",
headers).group(1)
+ dependencies = run([loader, "--inhibit-cache", "--list", exe]).stdout
+ assert "not found" not in dependencies, dependencies
+ run([loader, "--inhibit-cache", exe])
+ print("PASS: execution also succeeds with the system library cache
inhibited")
+ print(dependencies.rstrip())
+
+
+parser = argparse.ArgumentParser(description=__doc__)
+parser.add_argument("--cc", default="gcc")
+parser.add_argument("--readelf", default="readelf")
+parser.add_argument("--gcobol", help="installed patched native Linux
driver")
+parser.add_argument("--runtime-dir", help="configured toolexeclibdir")
+args = parser.parse_args()
+if bool(args.gcobol) != bool(args.runtime_dir):
+ parser.error("--gcobol and --runtime-dir must be used together")
+src = Path(__file__).resolve().parents[1]
+clean_env = {k: v for k, v in os.environ.items()
+ if k not in ("LD_LIBRARY_PATH", "LD_PRELOAD", "LD_AUDIT",
+ "LD_RUN_PATH", "LIBRARY_PATH",
"GCC_EXEC_PREFIX",
+ "COMPILER_PATH")}
+with tempfile.TemporaryDirectory(prefix="gcobol-runpath-") as temporary:
+ root = Path(temporary)
+ mechanism(root)
+ if args.gcobol:
+ installed(root)
diff --git a/libgcobol/unstring.cc b/libgcobol/unstring.cc
index f9bbcf4394f..aa03aea44f1 100644
--- a/libgcobol/unstring.cc
+++ b/libgcobol/unstring.cc
@@ -641,8 +641,8 @@ unstring_fixed( const cblc_referlet_t *id2,
|| static_cast<unsigned __int128>(p)
>
std::numeric_limits<size_t>::max() )
{
- overflow = 1;
- goto done;
+ // No characters were examined; leave all operands unchanged.
+ return 1;
}
pointer = static_cast<size_t>(p);
}
@@ -850,10 +850,7 @@ unstring_fixed( const cblc_referlet_t *id2,
// Update the state variables:
tally += 1;
nreceiver += 1;
- if( best_delimiter != ndelimiteds )
- {
- pointer = left+1 ;
- }
+ pointer = left+1 ;
}
done:
--
2.34.1