1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-22 10:27:48 +03:00

temporary

This commit is contained in:
Rui Ueyama 2020-10-25 15:17:43 +09:00
parent 144e6c0c12
commit 6a27ea57e1
3 changed files with 36 additions and 14 deletions

22
main.cc
View File

@ -162,7 +162,6 @@ static void fill_shdrs(ArrayRef<OutputChunk *> output_chunks) {
for (OutputChunk *chunk : output_chunks) {
if (chunk->name.empty())
continue;
chunk->shdr.sh_offset = chunk->fileoff;
chunk->shdr.sh_size = chunk->get_filesz();
}
}
@ -314,13 +313,30 @@ int main(int argc, char **argv) {
MyTimer t("file_offset", before_copy);
for (OutputChunk *chunk : output_chunks) {
if (chunk->starts_segment)
filesize = align_to(filesize, SECTOR_SIZE);
filesize = align_to(filesize, PAGE_SIZE);
else
filesize = align_to(filesize, chunk->shdr.sh_addralign);
chunk->set_fileoff(filesize);
filesize += chunk->get_filesz();
}
}
}
#if 0
{
MyTimer t("vaddr", before_copy);
uint64_t vaddr = 0x200000;
for (OutputChunk *chunk : output_chunks) {
if (chunk->starts_segment) {
vaddr = align_to(vaddr, PAGE_SIZE);
vaddr += chunk->get_fileoff() % PAGE_SIZE;
}
chunk->vaddr = vaddr;
vaddr += chunk->shdr.sh_size;
}
}
#endif
// Fill section header.
fill_shdrs(output_chunks);

15
mold.h
View File

@ -226,11 +226,16 @@ public:
virtual void copy_to(uint8_t *buf) = 0;
virtual void relocate(uint8_t *buf) {}
virtual void set_fileoff(uint64_t off) { fileoff = off; }
virtual void set_fileoff(uint64_t off) { shdr.sh_offset = off; }
uint64_t get_fileoff() const { return shdr.sh_offset; }
virtual void set_vaddr(uint64_t va) { vaddr = va; }
virtual uint64_t get_filesz() const = 0;
StringRef name;
uint64_t fileoff;
uint64_t vaddr;
bool starts_segment = false;
ELF64LE::Shdr shdr = {};
};
@ -254,7 +259,7 @@ public:
OutputShdr() { shdr.sh_flags = llvm::ELF::SHF_ALLOC; }
void copy_to(uint8_t *buf) override {
auto *p = (ELF64LE::Shdr *)(buf + fileoff);
auto *p = (ELF64LE::Shdr *)(buf + get_fileoff());
for (ELF64LE::Shdr *ent : entries)
*p++ = *ent;
}
@ -330,7 +335,7 @@ public:
}
void copy_to(uint8_t *buf) override {
memcpy(buf + fileoff, path, sizeof(path));
memcpy(buf + get_fileoff(), path, sizeof(path));
}
uint64_t get_filesz() const override { return sizeof(path); }
@ -357,7 +362,7 @@ public:
}
void copy_to(uint8_t *buf) override {
memcpy(buf + fileoff, &contents[0], contents.size());
memcpy(buf + get_fileoff(), &contents[0], contents.size());
}
uint64_t get_filesz() const override { return contents.size(); }

View File

@ -30,8 +30,8 @@ void OutputEhdr::relocate(uint8_t *buf) {
hdr->e_machine = EM_X86_64;
hdr->e_version = EV_CURRENT;
hdr->e_entry = Symbol::intern("_entry")->addr;
hdr->e_phoff = out::phdr->fileoff;
hdr->e_shoff = out::shdr->fileoff;
hdr->e_phoff = out::phdr->get_fileoff();
hdr->e_shoff = out::shdr->get_fileoff();
hdr->e_flags = 0;
hdr->e_ehsize = sizeof(ELF64LE::Ehdr);
hdr->e_phentsize = sizeof(ELF64LE::Phdr);
@ -97,17 +97,18 @@ void OutputPhdr::copy_to(uint8_t *buf) {
OutputChunk *front = ent.members.front();
OutputChunk *back = ent.members.back();
ent.phdr.p_offset = front->fileoff;
ent.phdr.p_filesz = back->fileoff + back->get_filesz() - front->fileoff;
ent.phdr.p_offset = front->get_fileoff();
ent.phdr.p_filesz =
back->get_fileoff() + back->get_filesz() - front->shdr.sh_offset;
}
auto *p = (ELF64LE::Phdr *)(buf + fileoff);
auto *p = (ELF64LE::Phdr *)(buf + get_fileoff());
for (Phdr &ent : entries)
*p++ = ent.phdr;
}
void OutputSection::set_fileoff(uint64_t off) {
fileoff = off;
shdr.sh_offset = off;
for (int i = 0; i < chunks.size(); i++) {
chunks[i]->offset = off;
off += chunks[i]->get_size();