Remove unused section writer

This commit is contained in:
2024-08-31 18:24:34 +02:00
parent 6d5cf23157
commit 7979c0cd23
6 changed files with 106 additions and 187 deletions

View File

@ -1,90 +1,14 @@
#include "elna/backend/target.hpp"
#include "elna/backend/riscv.hpp"
#include <cstring>
#include <boost/process/v2.hpp>
#include <boost/endian.hpp>
namespace elna::riscv
{
elfio_section_writer::iterator::reference elfio_section_writer::iterator::operator*() const noexcept
{
return payload;
}
elfio_section_writer::iterator::pointer elfio_section_writer::iterator::operator->() const noexcept
{
return &payload;
}
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;
}
elfio_section_writer::iterator& elfio_section_writer::iterator::operator++(int)
{
auto tmp = *this;
++(*this);
return *this;
}
bool elfio_section_writer::iterator::operator==(const iterator& that) const
{
return this->labels == that.labels;
}
bool elfio_section_writer::iterator::operator!=(const iterator& that) const
{
return !(*this == that);
}
elfio_section_writer::elfio_section_writer(ELFIO::section *section)
: m_section(section)
{
}
void elfio_section_writer::operator()(const std::string& label, const std::byte *data, std::size_t size)
{
labels.push_back(label);
sizes.push_back(size);
m_section->append_data(reinterpret_cast<const char *>(data), size);
}
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(),
[data, size](elfio_section_writer::entry entry) {
return size == entry.size && std::memcmp(entry.data, data, size) == 0;
});
if (found == end())
{
(*this)(".CL" + std::to_string(labels.size()), data, size);
return std::pair<std::string_view, bool>(labels.back(), true);
}
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());
}
ELFIO::section *elfio_section_writer::section() noexcept
{
return m_section;
}
elfio_writer::elfio_writer(ELFIO::section *text, ELFIO::section *read_only,
elfio_writer::elfio_writer(ELFIO::section *text,
ELFIO::symbol_section_accessor symbol_accessor, ELFIO::string_section_accessor string_accessor)
: text(text), read_only(elfio_section_writer(read_only)),
: text(text),
symbol_accessor(symbol_accessor), string_accessor(string_accessor)
{
}
@ -100,20 +24,6 @@ namespace elna::riscv
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)
{
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());
}
return result;
}
void elfio_writer::sink(const std::string& label)
{
this->symbol_accessor.add_symbol(this->string_accessor, "printf", 0x00000000, 0,
@ -125,6 +35,35 @@ namespace elna::riscv
return this->text->get_size();
}
process_writer::process_writer(boost::asio::io_context& context)
: pipe(context)
{
}
std::size_t process_writer::sink(const std::string& label, const std::byte *data, std::size_t size)
{
auto network_order_size = boost::endian::native_to_big(static_cast<std::uint16_t>(size));
auto written = boost::asio::write(this->pipe, boost::asio::buffer(label.data(), label.size() + 1));
boost::asio::write(this->pipe, boost::asio::buffer(&network_order_size, 2));
boost::asio::write(this->pipe, boost::asio::buffer(data, size));
this->text_size += written;
return written;
}
void process_writer::sink(const std::string& label)
{
std::uint16_t data_size{ 0 };
boost::asio::write(this->pipe, boost::asio::buffer(label.data(), label.size() + 1));
boost::asio::write(this->pipe, boost::asio::buffer(&data_size, 2));
}
std::size_t process_writer::size() const
{
return this->text_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)
@ -188,18 +127,21 @@ namespace elna::riscv
rel_sec->set_link(sym_sec->get_index());
rel_sec->set_flags(ELFIO::SHF_ALLOC);
// 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);
// Create symbol relocation table writers
ELFIO::symbol_section_accessor syma(writer, sym_sec);
ELFIO::relocation_section_accessor rela(writer, rel_sec);
auto _writer = std::make_shared<elfio_writer>(text_sec, ro_sec, syma, stra);
auto references = generate(intermediate_code_generator, table, _writer);
auto writer1 = std::make_shared<elfio_writer>(text_sec, syma, stra);
boost::asio::io_context ctx;
auto writer2 = std::make_shared<process_writer>(ctx);
boost::process::process child(ctx, "./build/hs/elna", {},
boost::process::process_stdio{ { writer2->pipe }, nullptr, nullptr });
auto references = generate(intermediate_code_generator, table, writer1);
generate(intermediate_code_generator, table, writer2);
writer2->pipe.close();
child.wait();
syma.arrange_local_symbols();