From 8a60ebbfbf8542964ef7335651183e3bd6261547 Mon Sep 17 00:00:00 2001 From: RoyR Date: Mon, 16 Mar 2026 11:47:38 -0400 Subject: [PATCH] Added a linker scripts, instead of using gcc directly. --- Makefile | 6 +++--- README.md | 1 + commonl | 11 +++++++++++ test_runner.c | 2 +- 4 files changed, 16 insertions(+), 4 deletions(-) create mode 100755 commonl diff --git a/Makefile b/Makefile index f2bb06e..4626efe 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,8 @@ CC = gcc CFLAGS = -std=c99 -Wall -O2 NASM = nasm NASMFLAGS = -f elf32 -LD = gcc -LDFLAGS = -m32 +LD = ./commonl +LDFLAGS = # Compiler COMPILER = common @@ -49,7 +49,7 @@ $(EXAMPLES): %: $(EXAMPLES_DIR)/%.cm $(COMPILER) @echo "Building $@..." ./$(COMPILER) $(EXAMPLES_DIR)/$@.cm $@.asm $(NASM) $(NASMFLAGS) $@.asm -o $@.o - $(LD) $(LDFLAGS) $@.o -o $@ -no-pie + $(LD) $(LDFLAGS) $@.o -o $@ @echo "Built $@ successfully" # Run all examples diff --git a/README.md b/README.md index 7bfff78..06bad37 100644 --- a/README.md +++ b/README.md @@ -302,6 +302,7 @@ gcc -m32 main.c common.o -o program ``` . ├── common.c # Compiler source (2000 LOC) +├── commonl # Linker ├── Makefile # Build automation ├── run_tests.sh # Quick test script │ diff --git a/commonl b/commonl new file mode 100755 index 0000000..c42b9d9 --- /dev/null +++ b/commonl @@ -0,0 +1,11 @@ +#!/bin/bash +# Public domain / CC0. Use freely for any purpose. RoyR 2026 + +GCC_NO_PIE=1 +echo 'int main() {}' | gcc -E -no-pie - -o /dev/null || GCC_NO_PIE=0 + +if [ $GCC_NO_PIE = 1 ];then + gcc -m32 -no-pie "$@" +else + gcc -m32 "$@" +fi diff --git a/test_runner.c b/test_runner.c index 34a68b5..c499687 100644 --- a/test_runner.c +++ b/test_runner.c @@ -72,7 +72,7 @@ static void run_test(Test *t) { } /* Link */ - if (run_command("gcc -m32 /tmp/test.o -o /tmp/test 2>/tmp/test.err -no-pie") != 0) { + if (run_command("./commonl /tmp/test.o -o /tmp/test 2>/tmp/test.err") != 0) { printf("FAIL (linker error)\n"); test_failed++; return;