1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-26 09:51:53 +03:00

temporary

This commit is contained in:
Rui Ueyama 2020-11-11 09:02:36 +09:00
parent 79793077b9
commit a5a91f1e85
3 changed files with 50 additions and 73 deletions

40
main.cc
View File

@ -449,6 +449,34 @@ 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));
@ -821,8 +849,7 @@ int main(int argc, char **argv) {
total_timer.startTimer();
if (!config.is_static) {
out::interp.path = "/lib64/ld-linux-x86-64.so.2";
out::interp.shdr.sh_size = out::interp.path.size() + 1;
out::interp.shdr.sh_size = config.dynamic_linker.size() + 1;
out::dynamic.shdr.sh_size = 1;
}
@ -972,6 +999,7 @@ int main(int argc, char **argv) {
output_chunks[i]->shndx = shndx++;
// Initialize synthetic section contents
out::ehdr.shdr.sh_size = sizeof(ELF64LE::Ehdr);
out::shdr.shdr.sh_size = create_shdr(output_chunks).size();
out::phdr.shdr.sh_size = create_phdr(output_chunks).size();
if (out::dynamic.shdr.sh_size)
@ -1038,8 +1066,16 @@ int main(int argc, char **argv) {
}
// 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(output_chunks));
write_vector(buf + out::phdr.shdr.sh_offset, create_phdr(output_chunks));
if (out::interp.shdr.sh_size) {
StringRef path = config.dynamic_linker;
memcpy(buf + out::interp.shdr.sh_offset, path.data(), path.size());
buf[out::interp.shdr.sh_offset + path.size()] = '\0';
}
if (out::dynamic.shdr.sh_size)
write_vector(buf + out::dynamic.shdr.sh_offset, create_dynamic_section());

58
mold.h
View File

@ -64,9 +64,10 @@ class OutputSection;
class MergedSection;
struct Config {
StringRef dynamic_linker = "/lib64/ld-linux-x86-64.so.2";
StringRef output;
bool print_map = false;
bool is_static = false;
bool print_map = false;
};
inline Config config;
@ -288,29 +289,10 @@ public:
ELF64LE::Shdr shdr = {};
};
// ELF header
class OutputEhdr : public OutputChunk {
// ELF, Section or Program header
class OutputHeader : 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;
};
// Section header
class OutputShdr : public OutputChunk {
public:
OutputShdr() : OutputChunk(HEADER) {
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
}
};
// Program header
class OutputPhdr : public OutputChunk {
public:
OutputPhdr() : OutputChunk(HEADER) {
OutputHeader() : OutputChunk(HEADER) {
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
}
};
@ -350,22 +332,6 @@ public:
}
};
class InterpSection : public OutputChunk {
public:
InterpSection() : OutputChunk(SYNTHETIC) {
name = ".interp";
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
shdr.sh_type = llvm::ELF::SHT_PROGBITS;
}
void copy_to(u8 *buf) override {
memcpy(buf + shdr.sh_offset, path.data(), path.size());
buf[shdr.sh_offset + path.size()] = '\0';
}
StringRef path;
};
class PltSection : public OutputChunk {
public:
PltSection() : OutputChunk(SYNTHETIC) {
@ -459,18 +425,18 @@ bool is_c_identifier(StringRef name);
namespace out {
using namespace llvm::ELF;
inline OutputEhdr ehdr;
inline OutputShdr shdr;
inline OutputPhdr phdr;
inline InterpSection interp;
inline OutputHeader ehdr;
inline OutputHeader shdr;
inline OutputHeader phdr;
inline SpecialSection interp(".interp", SHF_ALLOC, SHT_PROGBITS, 1, 0);
inline SpecialSection got(".got", SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 8, 0);
inline SpecialSection gotplt(".gotplt", SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 8, 0);
inline PltSection plt;
inline SpecialSection relplt(".rela.plt", SHF_ALLOC, SHT_RELA, 8, sizeof(ELF64LE::Rela));
inline SpecialSection dynamic(".dynamic", SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, 8, 0);
inline ShstrtabSection shstrtab;
inline SymtabSection symtab;
inline SpecialSection strtab(".strtab", 0, SHT_STRTAB, 1, 0);
inline ShstrtabSection shstrtab;
inline PltSection plt;
inline SymtabSection symtab;
inline u64 tls_end;

View File

@ -4,31 +4,6 @@
using namespace llvm::ELF;
void OutputEhdr::copy_to(u8 *buf) {
auto &hdr = *(ELF64LE::Ehdr *)buf;
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.",