Added a linker scripts, instead of using gcc directly.
This commit is contained in:
6
Makefile
6
Makefile
@@ -4,8 +4,8 @@ CC = gcc
|
|||||||
CFLAGS = -std=c99 -Wall -O2
|
CFLAGS = -std=c99 -Wall -O2
|
||||||
NASM = nasm
|
NASM = nasm
|
||||||
NASMFLAGS = -f elf32
|
NASMFLAGS = -f elf32
|
||||||
LD = gcc
|
LD = ./commonl
|
||||||
LDFLAGS = -m32
|
LDFLAGS =
|
||||||
|
|
||||||
# Compiler
|
# Compiler
|
||||||
COMPILER = common
|
COMPILER = common
|
||||||
@@ -49,7 +49,7 @@ $(EXAMPLES): %: $(EXAMPLES_DIR)/%.cm $(COMPILER)
|
|||||||
@echo "Building $@..."
|
@echo "Building $@..."
|
||||||
./$(COMPILER) $(EXAMPLES_DIR)/$@.cm $@.asm
|
./$(COMPILER) $(EXAMPLES_DIR)/$@.cm $@.asm
|
||||||
$(NASM) $(NASMFLAGS) $@.asm -o $@.o
|
$(NASM) $(NASMFLAGS) $@.asm -o $@.o
|
||||||
$(LD) $(LDFLAGS) $@.o -o $@ -no-pie
|
$(LD) $(LDFLAGS) $@.o -o $@
|
||||||
@echo "Built $@ successfully"
|
@echo "Built $@ successfully"
|
||||||
|
|
||||||
# Run all examples
|
# Run all examples
|
||||||
|
|||||||
@@ -302,6 +302,7 @@ gcc -m32 main.c common.o -o program
|
|||||||
```
|
```
|
||||||
.
|
.
|
||||||
├── common.c # Compiler source (2000 LOC)
|
├── common.c # Compiler source (2000 LOC)
|
||||||
|
├── commonl # Linker
|
||||||
├── Makefile # Build automation
|
├── Makefile # Build automation
|
||||||
├── run_tests.sh # Quick test script
|
├── run_tests.sh # Quick test script
|
||||||
│
|
│
|
||||||
|
|||||||
11
commonl
Executable file
11
commonl
Executable file
@@ -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
|
||||||
@@ -72,7 +72,7 @@ static void run_test(Test *t) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Link */
|
/* 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");
|
printf("FAIL (linker error)\n");
|
||||||
test_failed++;
|
test_failed++;
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user