elna/backend/target.cpp

94 lines
4.0 KiB
C++
Raw Normal View History

2024-03-07 09:15:11 +01:00
#include "elna/backend/target.hpp"
#include "elna/backend/riscv.hpp"
2024-03-01 10:13:55 +01:00
#include <elfio/elfio.hpp>
2024-03-09 08:36:07 +01:00
namespace elna::riscv
2024-03-01 10:13:55 +01:00
{
2024-03-07 09:15:11 +01:00
void riscv32_elf(source::block *ast, const std::filesystem::path& out_file)
2024-03-01 10:13:55 +01:00
{
2024-03-09 08:36:07 +01:00
auto _visitor = std::make_unique<visitor>();
_visitor->visit(ast);
2024-03-01 10:13:55 +01:00
ELFIO::elfio writer;
2024-03-09 08:36:07 +01:00
const ELFIO::Elf_Word instructions_size = _visitor->instructions.size() * sizeof(instruction);
2024-03-01 10:13:55 +01:00
writer.create(ELFIO::ELFCLASS32, ELFIO::ELFDATA2LSB);
writer.set_os_abi(ELFIO::ELFOSABI_NONE);
writer.set_type(ELFIO::ET_REL);
writer.set_machine(ELFIO::EM_RISCV);
// Create code section
ELFIO::section* text_sec = writer.sections.add(".text");
text_sec->set_type(ELFIO::SHT_PROGBITS);
text_sec->set_flags(ELFIO::SHF_ALLOC | ELFIO::SHF_EXECINSTR);
text_sec->set_addr_align(0x1);
2024-03-09 08:36:07 +01:00
text_sec->set_data(reinterpret_cast<const char *>(_visitor->instructions.data()),
2024-03-07 09:15:11 +01:00
instructions_size);
2024-03-01 10:13:55 +01:00
// Create string table section
ELFIO::section* str_sec = writer.sections.add(".strtab");
str_sec->set_type(ELFIO::SHT_STRTAB);
// Create string table writer
ELFIO::string_section_accessor stra(str_sec);
// Create read only data section
ELFIO::section* ro_sec = writer.sections.add(".rodata");
ro_sec->set_type(ELFIO::SHT_PROGBITS);
ro_sec->set_flags(ELFIO::SHF_ALLOC);
ro_sec->set_addr_align(0x4);
ro_sec->set_data("%d\n");
// Create symbol table section
ELFIO::section* sym_sec = writer.sections.add(".symtab");
sym_sec->set_type(ELFIO::SHT_SYMTAB);
sym_sec->set_info(2);
sym_sec->set_addr_align(0x4);
sym_sec->set_entry_size(writer.get_default_entry_size(ELFIO::SHT_SYMTAB));
sym_sec->set_link(str_sec->get_index());
// Create relocation table section
ELFIO::section* rel_sec = writer.sections.add(".rel.text");
rel_sec->set_type(ELFIO::SHT_REL);
rel_sec->set_info(text_sec->get_index());
rel_sec->set_addr_align(0x4);
rel_sec->set_entry_size(writer.get_default_entry_size(ELFIO::SHT_REL));
rel_sec->set_link(sym_sec->get_index());
rel_sec->set_flags(ELFIO::SHF_ALLOC);
2024-03-11 10:43:26 +01:00
// Create symbol relocation table writers
ELFIO::symbol_section_accessor syma(writer, sym_sec);
2024-03-01 10:13:55 +01:00
ELFIO::relocation_section_accessor rela(writer, rel_sec);
2024-03-11 10:43:26 +01:00
ELFIO::Elf_Word digit_symbol = syma.add_symbol(stra, ".CL0", 0x00000000, strlen("%d\n") + 1,
ELFIO::STB_LOCAL, ELFIO::STT_NOTYPE, 0, ro_sec->get_index());
ELFIO::Elf_Word printf_symbol = syma.add_symbol(stra, "printf", 0x00000000, 0,
ELFIO::STB_GLOBAL, ELFIO::STT_NOTYPE, 0, ELFIO::SHN_UNDEF);
for (auto& reference : _visitor->references)
{
// The loop assumes that address_t::lower12i always follows address_t::high20.
switch (reference.target)
{
case address_t::high20:
rela.add_entry(reference.offset, digit_symbol, 26 /* ELFIO::R_RISCV_HI20 */);
rela.add_entry(reference.offset, digit_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
break;
case address_t::lower12i:
rela.add_entry(reference.offset, digit_symbol, 27 /* ELFIO::R_RISCV_LO12_I */);
rela.add_entry(reference.offset, digit_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
break;
case address_t::text:
rela.add_entry(reference.offset, printf_symbol, 18 /* ELFIO::R_RISCV_CALL */);
rela.add_entry(reference.offset, printf_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
break;
}
}
syma.add_symbol(stra, "main", 0x00000000, instructions_size,
ELFIO::STB_GLOBAL, ELFIO::STT_FUNC, 0, text_sec->get_index());
2024-03-01 10:13:55 +01:00
// Create ELF object file
2024-03-03 13:11:39 +01:00
writer.save(out_file);
2024-03-01 10:13:55 +01:00
}
}