1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-27 10:23:41 +03:00

temporary

This commit is contained in:
Rui Ueyama 2020-10-16 16:38:03 +09:00
parent 961205a662
commit d4048d49db
4 changed files with 63 additions and 47 deletions

View File

@ -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<ELF64LE::Shdr> 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<ELF64LE::Phdr> 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<InputSection *> sections;
@ -216,6 +245,7 @@ public:
int priority;
bool is_alive = false;
std::unordered_set<ObjectFile *> liveness_edges;
ELFFile<ELF64LE> obj;
private:
MemoryBufferRef mb;

View File

@ -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<ELF64LE>::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<ELF64LE> obj = check(ELFFile<ELF64LE>::create(mb.getBuffer()));
ArrayRef<ELF64LE::Shdr> sections = CHECK(obj.sections(), this);
StringRef section_strtab = CHECK(obj.getSectionStringTable(sections), this);

View File

@ -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<uint8_t> data = check(file->obj.getSectionContents(*hdr));
memcpy(buf + offset, &data[0], data.size());
}

View File

@ -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);
}