From 5e24b5048eb3ddee61ee240540644d091e43ad3c Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Mon, 26 Oct 2020 10:26:51 +0900 Subject: [PATCH] temporary --- main.cc | 62 ++++-------------------------------------------- mold.h | 11 +++++---- output_chunks.cc | 18 +++++++------- 3 files changed, 19 insertions(+), 72 deletions(-) diff --git a/main.cc b/main.cc index d25dbb20..040e2da2 100644 --- a/main.cc +++ b/main.cc @@ -315,65 +315,13 @@ int main(int argc, char **argv) { { MyTimer t("file_offset", before_copy); - std::vector> slices; - - int i = 0; - while (i < output_chunks.size() && !output_chunks[i]->is_bss()) { - int j = i + 1; - while (j < output_chunks.size() && !output_chunks[j]->starts_new_ptload && - !output_chunks[j]->is_bss()) - j++; - - slices.push_back(ArrayRef(output_chunks).slice(i, j - i)); - i = j; - } - - if (i != output_chunks.size()) - slices.push_back(ArrayRef(output_chunks).slice(i)); - - for (ArrayRef slice : slices) { - uint64_t vaddr = 0; - uint64_t fileoff = 0; - - for (OutputChunk *chunk : slice) { - vaddr = align_to(vaddr, chunk->shdr.sh_addralign); - if (!chunk->is_bss()) - fileoff = align_to(fileoff, chunk->shdr.sh_addralign); - - chunk->set_offset(vaddr, fileoff); - - vaddr += chunk->get_filesz(); - if (!chunk->is_bss()) - fileoff += chunk->get_filesz(); - } - } - - uint64_t vaddr = 0x200000; uint64_t fileoff = 0; - - for (ArrayRef slice : slices) { - vaddr = align_to(vaddr, PAGE_SIZE); - fileoff = align_to(fileoff, PAGE_SIZE); - - for (OutputChunk *chunk : slice) { - chunk->shdr.sh_addr += vaddr; - chunk->shdr.sh_offset += fileoff; - } - - OutputChunk *last = slice.back(); - vaddr = last->shdr.sh_addr + last->shdr.sh_size; - - if (last->is_bss()) - fileoff = last->shdr.sh_offset; - else - fileoff = last->shdr.sh_offset + last->get_filesz(); - } - for (OutputChunk *chunk : output_chunks) { - llvm::outs() << chunk->name - << " vaddr=" << chunk->shdr.sh_addr - << " offset=" << chunk->shdr.sh_offset - << "\n"; + if (!chunk->is_bss()) + fileoff = align_to(fileoff, chunk->shdr.sh_addralign); + chunk->set_offset(fileoff); + if (!chunk->is_bss()) + fileoff = chunk->get_filesz(); } } diff --git a/mold.h b/mold.h index 95d24f20..555a8eb3 100644 --- a/mold.h +++ b/mold.h @@ -222,8 +222,7 @@ public: virtual void copy_to(uint8_t *buf) = 0; virtual void relocate(uint8_t *buf) {} - virtual void set_offset(uint64_t vaddr, uint64_t fileoff) { - shdr.sh_addr = vaddr; + virtual void set_offset(uint64_t fileoff) { shdr.sh_offset = fileoff; } @@ -304,11 +303,13 @@ public: } void copy_to(uint8_t *buf) override { - for_each(chunks, [&](InputSection *isec) { isec->copy_to(buf); }); + if (!is_bss()) + for_each(chunks, [&](InputSection *isec) { isec->copy_to(buf); }); } void relocate(uint8_t *buf) override { - for_each(chunks, [&](InputSection *isec) { isec->relocate(buf); }); + if (!is_bss()) + for_each(chunks, [&](InputSection *isec) { isec->relocate(buf); }); } uint64_t get_filesz() const override { @@ -317,7 +318,7 @@ public: return shdr.sh_size; } - void set_offset(uint64_t vaddr, uint64_t fileoff) override; + void set_offset(uint64_t fileoff) override; void set_alignment(uint32_t align) { shdr.sh_addralign = align; } std::vector chunks; diff --git a/output_chunks.cc b/output_chunks.cc index dfbb3fae..3b78fd39 100644 --- a/output_chunks.cc +++ b/output_chunks.cc @@ -122,20 +122,18 @@ void OutputPhdr::copy_to(uint8_t *buf) { *p++ = ent.phdr; } -void OutputSection::set_offset(uint64_t vaddr, uint64_t fileoff) { - shdr.sh_addr = vaddr; +void OutputSection::set_offset(uint64_t fileoff) { shdr.sh_offset = fileoff; - if ((shdr.sh_flags & SHF_ALLOC) && !(shdr.sh_type & SHT_NOBITS)) - assert(vaddr == fileoff); + if (!is_bss()) { + uint64_t off = 0; - for (InputSection *isec : chunks) { - vaddr = align_to(vaddr, isec->shdr.sh_addralign); - isec->offset = vaddr; - vaddr += isec->shdr.sh_size; + for (InputSection *isec : chunks) { + off = align_to(off, isec->shdr.sh_addralign); + isec->offset = off; + off += isec->shdr.sh_size; + } } - - shdr.sh_size = vaddr - shdr.sh_addr; } static StringRef get_output_name(StringRef name) {