1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-26 18:02:30 +03:00

temporary

This commit is contained in:
Rui Ueyama 2020-11-17 00:17:01 +09:00
parent 8f78590cbe
commit a4255b68e5
3 changed files with 42 additions and 35 deletions

36
main.cc
View File

@ -576,34 +576,6 @@ static int get_section_rank(const ELF64LE::Shdr &shdr) {
static void sort_output_chunks(std::vector<OutputChunk *> &chunks) {
}
static std::vector<u8> create_ehdr() {
ELF64LE::Ehdr hdr = {};
memcpy(&hdr.e_ident, "\177ELF", 4);
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_type = ET_EXEC;
hdr.e_machine = EM_X86_64;
hdr.e_version = EV_CURRENT;
hdr.e_entry = Symbol::intern("_start")->get_addr();
hdr.e_phoff = out::phdr->shdr.sh_offset;
hdr.e_shoff = out::shdr->shdr.sh_offset;
hdr.e_flags = 0;
hdr.e_ehsize = sizeof(ELF64LE::Ehdr);
hdr.e_phentsize = sizeof(ELF64LE::Phdr);
hdr.e_phnum = out::phdr->shdr.sh_size / sizeof(ELF64LE::Phdr);
hdr.e_shentsize = sizeof(ELF64LE::Shdr);
hdr.e_shnum = out::shdr->shdr.sh_size / sizeof(ELF64LE::Shdr);
hdr.e_shstrndx = out::shstrtab->shndx;
std::vector<u8> ret(sizeof(hdr));
memcpy(ret.data(), &hdr, sizeof(hdr));
return ret;
}
template<typename T>
static std::vector<u8> to_u8vector(const std::vector<T> &vec) {
std::vector<u8> ret(vec.size() * sizeof(T));
@ -984,9 +956,9 @@ int main(int argc, char **argv) {
Timer total_timer("total", "total");
total_timer.startTimer();
out::ehdr = new OutputHeader;
out::shdr = new OutputHeader;
out::phdr = new OutputHeader;
out::ehdr = new OutputEhdr;
out::shdr = new OutputHeader(0);
out::phdr = new OutputHeader(0);
out::got = new SpecialSection(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE, 8);
out::gotplt = new GotPltSection;
out::relplt = new SpecialSection(".rela.plt", SHT_RELA, SHF_ALLOC,
@ -1142,7 +1114,6 @@ int main(int argc, char **argv) {
chunks[i]->shndx = shndx++;
// Initialize synthetic section contents
out::ehdr->shdr.sh_size = sizeof(ELF64LE::Ehdr);
out::shdr->shdr.sh_size = create_shdr(chunks).size();
out::phdr->shdr.sh_size = create_phdr(chunks).size();
if (out::dynamic)
@ -1220,7 +1191,6 @@ int main(int argc, char **argv) {
write_merged_strings(buf, files);
// Write headers and synthetic sections.
write_vector(buf + out::ehdr->shdr.sh_offset, create_ehdr());
write_vector(buf + out::shdr->shdr.sh_offset, create_shdr(chunks));
write_vector(buf + out::phdr->shdr.sh_offset, create_phdr(chunks));

16
mold.h
View File

@ -321,11 +321,23 @@ public:
ELF64LE::Shdr shdr = {};
};
// ELF header
class OutputEhdr : public OutputChunk {
public:
OutputEhdr() : OutputChunk(HEADER) {
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
shdr.sh_size = sizeof(ELF64LE::Ehdr);
}
void copy_to(u8 *buf) override;
};
// ELF, Section or Program header
class OutputHeader : public OutputChunk {
public:
OutputHeader() : OutputChunk(HEADER) {
OutputHeader(u32 size) : OutputChunk(HEADER) {
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
shdr.sh_size = size;
}
};
@ -497,7 +509,7 @@ bool is_c_identifier(StringRef name);
namespace out {
using namespace llvm::ELF;
inline OutputHeader *ehdr;
inline OutputEhdr *ehdr;
inline OutputHeader *shdr;
inline OutputHeader *phdr;
inline SpecialSection *interp;

View File

@ -4,6 +4,31 @@
using namespace llvm::ELF;
void OutputEhdr::copy_to(u8 *buf) {
auto &hdr = *(ELF64LE::Ehdr *)(buf + shdr.sh_offset);
memset(&hdr, 0, sizeof(hdr));
memcpy(&hdr.e_ident, "\177ELF", 4);
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_type = ET_EXEC;
hdr.e_machine = EM_X86_64;
hdr.e_version = EV_CURRENT;
hdr.e_entry = Symbol::intern("_start")->get_addr();
hdr.e_phoff = out::phdr->shdr.sh_offset;
hdr.e_shoff = out::shdr->shdr.sh_offset;
hdr.e_flags = 0;
hdr.e_ehsize = sizeof(ELF64LE::Ehdr);
hdr.e_phentsize = sizeof(ELF64LE::Phdr);
hdr.e_phnum = out::phdr->shdr.sh_size / sizeof(ELF64LE::Phdr);
hdr.e_shentsize = sizeof(ELF64LE::Shdr);
hdr.e_shnum = out::shdr->shdr.sh_size / sizeof(ELF64LE::Shdr);
hdr.e_shstrndx = out::shstrtab->shndx;
}
static StringRef get_output_name(StringRef name) {
static StringRef common_names[] = {
".text.", ".data.rel.ro.", ".data.", ".rodata.", ".bss.rel.ro.",