elna/backend/target.cpp

231 lines
8.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"
#include <cstring>
2024-03-01 10:13:55 +01:00
2024-03-09 08:36:07 +01:00
namespace elna::riscv
2024-03-01 10:13:55 +01:00
{
2024-03-19 09:35:50 +01:00
elfio_section_writer::iterator::reference elfio_section_writer::iterator::operator*() const noexcept
2024-03-01 10:13:55 +01:00
{
return payload;
}
2024-03-19 09:35:50 +01:00
elfio_section_writer::iterator::pointer elfio_section_writer::iterator::operator->() const noexcept
{
return &payload;
}
2024-03-19 09:35:50 +01:00
elfio_section_writer::iterator& elfio_section_writer::iterator::operator++()
{
this->payload.data += *this->sizes;
this->payload.label = *(++this->labels);
this->payload.size = *(++this->sizes);
return *this;
}
2024-03-19 09:35:50 +01:00
elfio_section_writer::iterator& elfio_section_writer::iterator::operator++(int)
{
auto tmp = *this;
++(*this);
return *this;
}
2024-03-19 09:35:50 +01:00
bool elfio_section_writer::iterator::operator==(const iterator& that) const
{
return this->labels == that.labels;
}
2024-03-19 09:35:50 +01:00
bool elfio_section_writer::iterator::operator!=(const iterator& that) const
{
return !(*this == that);
}
2024-03-19 09:35:50 +01:00
elfio_section_writer::elfio_section_writer(ELFIO::section *section)
: m_section(section)
{
}
2024-03-19 09:35:50 +01:00
void elfio_section_writer::operator()(const std::string& label, const std::byte *data, std::size_t size)
{
2024-03-19 09:35:50 +01:00
labels.push_back(label);
sizes.push_back(size);
m_section->append_data(reinterpret_cast<const char *>(data), size);
}
2024-03-01 10:13:55 +01:00
2024-03-19 09:35:50 +01:00
std::pair<std::string_view, bool> elfio_section_writer::operator()(const std::byte *data, std::size_t size)
{
auto found = std::find_if(begin(), end(),
2024-03-19 09:35:50 +01:00
[data, size](elfio_section_writer::entry entry) {
return size == entry.size && std::memcmp(entry.data, data, size) == 0;
});
if (found == end())
{
2024-03-19 09:35:50 +01:00
(*this)(".CL" + std::to_string(labels.size()), data, size);
return std::pair<std::string_view, bool>(labels.back(), true);
}
2024-03-19 09:35:50 +01:00
return std::pair<std::string_view, bool>(found->label, false);
}
elfio_section_writer::iterator elfio_section_writer::begin() const
{
return elfio_section_writer::iterator(labels.cbegin(), sizes.cbegin(),
reinterpret_cast<const std::byte *>(m_section->get_data()));
}
elfio_section_writer::iterator elfio_section_writer::end() const
{
return elfio_section_writer::iterator(labels.cend(), sizes.cend());
}
2024-03-19 09:35:50 +01:00
ELFIO::section *elfio_section_writer::section() noexcept
{
2024-03-19 09:35:50 +01:00
return m_section;
}
2024-03-19 09:35:50 +01:00
elfio_writer::elfio_writer(ELFIO::section *text, ELFIO::section *read_only,
ELFIO::symbol_section_accessor symbol_accessor, ELFIO::string_section_accessor string_accessor)
: text(text), read_only(elfio_section_writer(read_only)),
symbol_accessor(symbol_accessor), string_accessor(string_accessor)
{
}
2024-03-19 09:35:50 +01:00
std::size_t elfio_writer::sink(const std::string& label, const std::byte *data, std::size_t size)
{
2024-03-19 09:35:50 +01:00
auto offset = text->get_size();
text->append_data(reinterpret_cast<const char *>(data), size);
2024-03-19 09:35:50 +01:00
this->symbol_accessor.add_symbol(this->string_accessor, label.data(), offset, label.size(),
ELFIO::STB_GLOBAL, ELFIO::STT_FUNC, 0, text->get_index());
return text->get_size();
}
std::string_view elfio_writer::sink(const std::byte *data, std::size_t size)
{
auto offset = read_only.section()->get_size();
auto [result, inserted] = read_only(data, size);
if (inserted)
{
2024-03-19 09:35:50 +01:00
this->symbol_accessor.add_symbol(this->string_accessor, result.data(), offset,
result.size(), ELFIO::STB_LOCAL, ELFIO::STT_NOTYPE, 0,
read_only.section()->get_index());
}
2024-03-19 09:35:50 +01:00
return result;
}
void elfio_writer::sink(const std::string& label)
{
this->symbol_accessor.add_symbol(this->string_accessor, "printf", 0x00000000, 0,
ELFIO::STB_GLOBAL, ELFIO::STT_NOTYPE, 0, ELFIO::SHN_UNDEF);
}
std::size_t elfio_writer::size() const
{
return this->text->get_size();
}
std::ptrdiff_t lookup(ELFIO::symbol_section_accessor symbol_accessor, const std::string& label)
{
for (ptrdiff_t j = 0; j < symbol_accessor.get_symbols_num(); ++j)
{
std::string name;
ELFIO::Elf64_Addr value;
ELFIO::Elf_Xword size;
unsigned char bind;
unsigned char type;
ELFIO::Elf_Half section_index;
unsigned char other;
symbol_accessor.get_symbol(j, name, value, size, bind, type, section_index, other);
if (name == label)
{
return j;
}
}
return -1;
}
void riscv32_elf(source::program *ast, std::shared_ptr<source::symbol_table> table,
const std::filesystem::path& out_file)
{
2024-03-01 10:13:55 +01:00
ELFIO::elfio writer;
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);
// 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 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-17 01:00:44 +01:00
// 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);
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-19 09:35:50 +01:00
auto _writer = std::make_shared<elfio_writer>(text_sec, ro_sec, syma, stra);
2024-03-17 01:00:44 +01:00
visitor _visitor{ _writer, table };
2024-03-19 09:35:50 +01:00
_visitor.visit(ast);
syma.arrange_local_symbols();
2024-03-11 10:43:26 +01:00
for (auto& reference : _visitor.references)
2024-03-11 10:43:26 +01:00
{
2024-03-20 17:56:38 +01:00
ELFIO::Elf_Word relocated_symbol = lookup(syma, reference.name);
2024-03-19 09:35:50 +01:00
2024-03-11 10:43:26 +01:00
switch (reference.target)
{
case address_t::high20:
2024-03-19 09:35:50 +01:00
rela.add_entry(reference.offset, relocated_symbol, 26 /* ELFIO::R_RISCV_HI20 */);
2024-04-12 00:07:46 +02:00
// rela.add_entry(reference.offset, relocated_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
2024-03-11 10:43:26 +01:00
break;
case address_t::lower12i:
2024-03-19 09:35:50 +01:00
rela.add_entry(reference.offset, relocated_symbol, 27 /* ELFIO::R_RISCV_LO12_I */);
2024-04-12 00:07:46 +02:00
// rela.add_entry(reference.offset, relocated_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
2024-03-11 10:43:26 +01:00
break;
case address_t::text:
2024-03-19 09:35:50 +01:00
rela.add_entry(reference.offset, relocated_symbol, 18 /* ELFIO::R_RISCV_CALL */);
2024-04-12 00:07:46 +02:00
// rela.add_entry(reference.offset, relocated_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
2024-03-11 10:43:26 +01:00
break;
}
}
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
}
}