From 3c8da0f727419e390b56601f5f818100e4b36ea1 Mon Sep 17 00:00:00 2001 From: "Andreas.Olofsson" Date: Thu, 5 Mar 2020 09:02:00 -0500 Subject: [PATCH] One line compile script --- scripts/c2elf | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100755 scripts/c2elf diff --git a/scripts/c2elf b/scripts/c2elf new file mode 100755 index 0000000..240cb54 --- /dev/null +++ b/scripts/c2elf @@ -0,0 +1,104 @@ +#!/usr/bin/env python3 +############################################################################# +# celf +# +# Uses gcc +# +# Simple utility for compiling c into an elf +# +# .globl _start +# _start: +# lui a0,0x1 +# +############################################################################# +import subprocess +import sys +import re +import os + +ARCH = sys.argv[1] +ASM = sys.argv[2] +ELF = sys.argv[3] + +############################################# +#SETUP + +LINKERSCRIPT="tmp.ld" + +if(ARCH=="rv32i"): + GCC="riscv64-unknown-elf-gcc -march=rv32i -mabi=ilp32" + GCCOPT=" -O2 -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles" + OUTPUT="riscv" + OBJDUMP="riscv64-unknown-elf-objdump" +elif(ARCH=="rv32gc"): + GCC="riscv64-unknown-elf-gcc -O2 -march=rv32gc -mabi=ilp32" + GCCOPT=" -O2 -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles" + OUTPUT="riscv" + OBJDUMP="riscv64-unknown-elf-objdump" +elif(ARCH=="rv64gc"): + GCC="riscv64-unknown-elf-gcc -O2 -march=rv64gc -mabi=lp64d" + GCCOPT=" -O2 -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles" + OUTPUT="riscv" + OBJDUMP="riscv64-unknown-elf-objdump" +else: + GCC="e-gcc" + GCCOPT=" -O2 -static -fvisibility=hidden -nostdlib -nostartfiles" + OUTPUT="epiphany" + OBJDUMP="e-objdump" + +############################################# +#PRINT LINKER FILE +f=open("tmp.ld",'w') +f.write("OUTPUT_ARCH( \"" + OUTPUT + "\" )\n") +if(ARCH=="rv32i"): + f.write("""ENTRY(_start) + SECTIONS + { + . = 0x80000000; + .text.trap : { *(.text.trap) } + + . = 0x00000000; + .text.init : { *(.text.init) } + + . = ALIGN(0x1000); + .tohost : { *(.tohost) } + . = ALIGN(0x1000); + .text : { *(.text) } + . = ALIGN(0x1000); + .data : { *(.data) } + .data.string : { *(.data.string)} + .bss : { *(.bss) } + _end = .; + }""") +else: + f.write("""ENTRY(_start) + SECTIONS + { + . = 0x00000000; + .text.init : { *(.text.init) } + . = ALIGN(0x1000); + .tohost : { *(.tohost) } + . = ALIGN(0x1000); + .text : { *(.text) } + . = ALIGN(0x1000); + .data : { *(.data) } + .bss : { *(.bss) } + _end = .; + }""") +f.close() + +############################################# +#RUN GCC +CMD = GCC + GCCOPT + " -T" + LINKERSCRIPT + " -o " + ELF + " " + ASM +print(CMD) +os.system(CMD) +#RUN OBJDUMP +CMD = OBJDUMP+ " -d " + ELF + " > " + ELF + ".objdump" +print(CMD) +os.system(CMD) + + + + + +