1
0
mirror of https://github.com/aolofsson/oh.git synced 2025-01-17 20:02:53 +08:00
oh/scripts/c2elf
2020-03-05 09:02:00 -05:00

105 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
#############################################################################
# celf <arch> <inputfile> <outputfile>
#
# 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)