Skip to content

Support immediate binding option - #335

Open
DrXiao wants to merge 3 commits into
sysprog21:masterfrom
DrXiao:support-immediate-binding
Open

DrXiao wants to merge 3 commits into
sysprog21:masterfrom
DrXiao:support-immediate-binding

Conversation

@DrXiao

@DrXiao DrXiao commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Since the immediate binding has already been implemented for the x86-64 and AArch64 architectures, the proposed changes extend its use by introducing a new "-z" command-line option, allowing the Arm32 and RV32 targets to also generate dynamically linked executables with immediate binding.

For the two 64-bit targets, immediate binding is always used under the dynamic linking mode as is.

The update usage:

# Support the "-z" option
$ qemu-arm -L /usr/arm-linux-gnueabihf/ out/shecc-stage2.elf
Usage: shecc [-o output] [+m] [--dot] [--dump-ir] [--no-libc] [--dynlink] [-z <lazy | now>] [-E] <input.c>
[Error]: Missing source file

# The build system can use the new 'BINDING' variable to determine whether to use immediate binding.
$ make ARCH=arm DYNLINK=1 BINDING=now

TODO:

  • Update the GitHub Actions workflow to validate builds when using immediate binding.

Summary by cubic

Adds a -z option to control dynamic binding: lazy (default) or now (immediate). It replaces the hardcoded immediate binding on x86-64 and AArch64 and lets Arm32 and RV32 dynamically linked executables use immediate binding.

  • The Makefile gains a BINDING variable, so builds can pass -z now via make DYNLINK=1 BINDING=now; the ABI and driver tests now take the binding to cover both modes.
  • x86-64 and AArch64 still force now in dynamic mode, so -z lazy is ignored there.

Written for commit 3c3998d. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 7 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread Makefile
Comment thread src/main.c
Since the original codebase already implemented the immediate binding
flag for the x86-64 and AArch64 architectures, this commit introduces a
"-z" option so that the Arm32 and RV32 targets can also use immediate
binding.

When the command-line arguments contain "-z now", the compiler sets its
internal flag (imm_binding) to true and generates the
Elf32_Dyn/Elf64_Dyn objects to enable exectuables to perform immediate
binding at runtime.

For 64-bit targets, immediate binding is always used under the dynamic
linking mode as is.
Since the compiler supports the "-z" option for lazy or immediate
binding, a new "BINDING" variable has benn added to the Makefile so that
users can choose which binding mode to use.

For example, users can build dynamically linked compilers with immediate
binding as follows:
$ make DYNLINK=1 BINDING=now
Since the compiler has implemented dynamic linking with immediate
binding, the test suites have been enhance to validate immediate binding
mode. The approach is to add an additional command-line argument to
determine which mode, lazy or immediate, is enabled during the tests.
@DrXiao
DrXiao force-pushed the support-immediate-binding branch from 0d1e98b to 3c3998d Compare September 15, 2026 14:02

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 7 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/riscv-abi.sh">

<violation number="1" location="tests/riscv-abi.sh:149">
P2: An unvalidated `binding` argument is inserted into a command string executed by `eval`, so invoking this test script with a crafted third argument executes arbitrary shell commands. Validate the value against `lazy|now` or shell-escape it before constructing the command.</violation>
</file>

<file name="tests/arm-abi.sh">

<violation number="1" location="tests/arm-abi.sh:149">
P2: When the binding argument contains shell syntax, this line inserts it into an `eval` string, so the ABI test executes it as a separate command. Escape the value or execute the compiler through an argv array.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread tests/riscv-abi.sh
local compile_cmd="$SHECC"
if [[ "$DYNLINK" == "1" ]]; then
compile_cmd="$compile_cmd --dynlink"
compile_cmd="$compile_cmd --dynlink -z $BINDING"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: An unvalidated binding argument is inserted into a command string executed by eval, so invoking this test script with a crafted third argument executes arbitrary shell commands. Validate the value against lazy|now or shell-escape it before constructing the command.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/riscv-abi.sh, line 149:

<comment>An unvalidated `binding` argument is inserted into a command string executed by `eval`, so invoking this test script with a crafted third argument executes arbitrary shell commands. Validate the value against `lazy|now` or shell-escape it before constructing the command.</comment>

<file context>
@@ -144,7 +146,7 @@ run_abi_test()
     local compile_cmd="$SHECC"
     if [[ "$DYNLINK" == "1" ]]; then
-        compile_cmd="$compile_cmd --dynlink"
+        compile_cmd="$compile_cmd --dynlink -z $BINDING"
     fi
     compile_cmd="$compile_cmd -o /tmp/shecc_abi_test_$$.elf $test_file"
</file context>
Suggested change
compile_cmd="$compile_cmd --dynlink -z $BINDING"
compile_cmd="$compile_cmd --dynlink -z $(printf '%q' "$BINDING")"

Comment thread tests/arm-abi.sh
local compile_cmd="$SHECC"
if [[ "$DYNLINK" == "1" ]]; then
compile_cmd="$compile_cmd --dynlink"
compile_cmd="$compile_cmd --dynlink -z $BINDING"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: When the binding argument contains shell syntax, this line inserts it into an eval string, so the ABI test executes it as a separate command. Escape the value or execute the compiler through an argv array.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/arm-abi.sh, line 149:

<comment>When the binding argument contains shell syntax, this line inserts it into an `eval` string, so the ABI test executes it as a separate command. Escape the value or execute the compiler through an argv array.</comment>

<file context>
@@ -144,7 +146,7 @@ run_abi_test()
     local compile_cmd="$SHECC"
     if [[ "$DYNLINK" == "1" ]]; then
-        compile_cmd="$compile_cmd --dynlink"
+        compile_cmd="$compile_cmd --dynlink -z $BINDING"
     fi
     compile_cmd="$compile_cmd -o /tmp/shecc_abi_test_$$.elf $test_file"
</file context>
Suggested change
compile_cmd="$compile_cmd --dynlink -z $BINDING"
compile_cmd="$compile_cmd --dynlink -z $(printf '%q' "$BINDING")"

@jserv jserv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebase latest master branch and resolve conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants