From d4048d49dbb02229af3ce28908083a95f5de8ecb Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 16 Oct 2020 16:38:03 +0900 Subject: [PATCH] temporary --- chibild.h | 54 +++++++++++++++++++++++++++++++++++----------- input_files.cc | 4 ++-- input_sections.cc | 4 +++- output_sections.cc | 48 ++++++++++++++--------------------------- 4 files changed, 63 insertions(+), 47 deletions(-) diff --git a/chibild.h b/chibild.h index 785cef7f..242027ce 100644 --- a/chibild.h +++ b/chibild.h @@ -128,7 +128,7 @@ private: class InputSection { public: InputSection(ObjectFile *file, const ELF64LE::Shdr *hdr, StringRef name); - void writeTo(uint8_t *buf); + void copy_to(uint8_t *buf); uint64_t get_size() const; StringRef name; @@ -148,7 +148,7 @@ class OutputChunk { public: virtual ~OutputChunk() {} - virtual void writeTo(uint8_t *buf) = 0; + virtual void copy_to(uint8_t *buf) = 0; virtual void set_offset(uint64_t off) { offset = off; } uint64_t get_offset() const { return offset; } virtual uint64_t get_size() const = 0; @@ -158,36 +158,65 @@ protected: int64_t size = -1; }; +// ELF header class OutputEhdr : public OutputChunk { public: - void writeTo(uint8_t *buf) override; - uint64_t get_size() const override; + OutputEhdr(); - ELF64LE::Ehdr hdr; + void copy_to(uint8_t *buf) override { + memcpy(buf + offset, &hdr, sizeof(hdr)); + } + + uint64_t get_size() const override { + return sizeof(hdr); + } + + ELF64LE::Ehdr hdr = {}; }; +// Section header class OutputShdr : public OutputChunk { public: - void writeTo(uint8_t *buf) override; - uint64_t get_size() const override; + void copy_to(uint8_t *buf) override { + memcpy(buf + offset, &hdr[0], get_size()); + } + + uint64_t get_size() const override { + return hdr.size() * sizeof(hdr[0]); + } std::vector hdr; }; +// Program header class OutputPhdr : public OutputChunk { public: - void writeTo(uint8_t *buf) override; - uint64_t get_size() const override; + void copy_to(uint8_t *buf) override { + memcpy(buf + offset, &hdr[0], get_size()); + } + + uint64_t get_size() const override { + return hdr.size() * sizeof(hdr[0]); + } std::vector hdr; }; +// Sections class OutputSection : public OutputChunk { public: - OutputSection(StringRef name); + OutputSection(StringRef name) : name(name) {} + + void copy_to(uint8_t *buf) override { + for (InputSection *sec : sections) + sec->copy_to(buf); + } + + uint64_t get_size() const override { + assert(size >= 0); + return size; + } - void writeTo(uint8_t *buf) override; - uint64_t get_size() const override; void set_offset(uint64_t off) override; std::vector sections; @@ -216,6 +245,7 @@ public: int priority; bool is_alive = false; std::unordered_set liveness_edges; + ELFFile obj; private: MemoryBufferRef mb; diff --git a/input_files.cc b/input_files.cc index b155de9f..b2eb3632 100644 --- a/input_files.cc +++ b/input_files.cc @@ -8,7 +8,8 @@ std::atomic_int num_undefined; std::atomic_int num_files; ObjectFile::ObjectFile(MemoryBufferRef mb, StringRef archive_name) - : mb(mb), archive_name(archive_name) {} + : mb(mb), archive_name(archive_name), + obj(check(ELFFile::create(mb.getBuffer()))) {} MemoryBufferRef readFile(StringRef path) { auto mbOrErr = MemoryBuffer::getFile(path, -1, false); @@ -33,7 +34,6 @@ void ObjectFile::parse() { num_files++; // Initialize sections. - ELFFile obj = check(ELFFile::create(mb.getBuffer())); ArrayRef sections = CHECK(obj.sections(), this); StringRef section_strtab = CHECK(obj.getSectionStringTable(sections), this); diff --git a/input_sections.cc b/input_sections.cc index f2c1a24f..a68fdc03 100644 --- a/input_sections.cc +++ b/input_sections.cc @@ -7,5 +7,7 @@ uint64_t InputSection::get_size() const { return hdr->sh_size; } -void InputSection::writeTo(uint8_t *buf) { +void InputSection::copy_to(uint8_t *buf) { + ArrayRef data = check(file->obj.getSectionContents(*hdr)); + memcpy(buf + offset, &data[0], data.size()); } diff --git a/output_sections.cc b/output_sections.cc index 4b8ea493..5cb86019 100644 --- a/output_sections.cc +++ b/output_sections.cc @@ -1,34 +1,23 @@ #include "chibild.h" -void OutputEhdr::writeTo(uint8_t *buf) { - memcpy(buf + offset, &hdr, sizeof(hdr)); -} +using namespace llvm::ELF; -uint64_t OutputEhdr::get_size() const { - return sizeof(hdr); -} +OutputEhdr::OutputEhdr() { + memcpy(&hdr.e_ident, "\177ELF", 4); -void OutputShdr::writeTo(uint8_t *buf) { - memcpy(buf + offset, &hdr[0], get_size()); -} - -uint64_t OutputShdr::get_size() const { - return hdr.size() * sizeof(hdr[0]); -} - -void OutputPhdr::writeTo(uint8_t *buf) { - memcpy(buf + offset, &hdr[0], get_size()); -} - -uint64_t OutputPhdr::get_size() const { - return hdr.size() * sizeof(hdr[0]); -} - -OutputSection::OutputSection(StringRef name) : name(name) {} - -uint64_t OutputSection::get_size() const { - assert(size >= 0); - return size; + hdr.e_ident[EI_CLASS] = ELFCLASS64; + hdr.e_ident[EI_DATA] = ELFDATA2LSB; + hdr.e_ident[EI_VERSION] = EV_CURRENT; + hdr.e_ident[EI_OSABI] = 0; + hdr.e_ident[EI_ABIVERSION] = 0; + hdr.e_machine = EM_X86_64; + hdr.e_version = EV_CURRENT; + hdr.e_flags = 0; + hdr.e_ehsize = sizeof(ELF64LE::Ehdr); + hdr.e_phnum = 0; + hdr.e_shentsize = sizeof(ELF64LE::Shdr); + hdr.e_phoff = sizeof(ELF64LE::Ehdr); + hdr.e_phentsize = sizeof(ELF64LE::Phdr); } void OutputSection::set_offset(uint64_t off) { @@ -39,8 +28,3 @@ void OutputSection::set_offset(uint64_t off) { } size = off - offset; } - -void OutputSection::writeTo(uint8_t *buf) { - for (InputSection *sec : sections) - sec->writeTo(buf); -}