diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 33ae89538d5645552d32de40a8d0b9c3974ef343..4e368aadf62a557fcd1438001783bec4c3051861 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -4070,13 +4070,11 @@ void AdltSection::finalizeContents() { ADLT_HASH_TYPE_GNU_HASH, // .stringHashType getBlobStartOffset(), // .blobStart estimateBlobSize(), // .blobSize - 0, // .overallMappedSize + 0, // .overallMappedSize, known on writeTo {0, 0}, // .relaDynSegs, filled in writeTo {0, 0}, // .relaPltSegs, filled in writeTo }; - // TODO: estimate and fill overallMappedSize - buildSonameIndex(); linkInternalDtNeeded(); extractInitFiniArray(); @@ -4133,6 +4131,15 @@ void AdltSection::extractInitFiniArray() { } } +template +size_t AdltSection::estimateOverallMappedSize() { + size_t totalMemsz = 0; + for (PhdrEntry* ph : mainPart->phdrs) + if (ph->p_type == PT_LOAD) + totalMemsz += ph->p_memsz; + return totalMemsz; +} + template typename AdltSection::CommonData AdltSection::makeCommonData() { @@ -4265,6 +4272,12 @@ adlt_blob_array_t AdltSection::writeDtNeeded( return writeArray(buff, offset, needIndexes); } +template +void AdltSection::finalizeOnWrite() { + // require Writer::setPhdrs previously been called + header.overallMappedSize = estimateOverallMappedSize(); +} + template void AdltSection::writeTo(uint8_t* buf) { // TODO: take care of endianness, use write32 / write64 etc. @@ -4277,6 +4290,8 @@ void AdltSection::writeTo(uint8_t* buf) { psods.push_back(psod); } + finalizeOnWrite(); + // serialize blob data { uint8_t* const blobBuf = buf + header.blobStart; diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index c69b95704406e06fcdcad81c394021cff0bcc911..a93ff052cfe362624439003531fc2b36047ff3cb 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1282,15 +1282,16 @@ private: void buildSonameIndex(); void linkInternalDtNeeded(); void extractInitFiniArray(); + size_t estimateOverallMappedSize(); + Elf64_Xword calculateHash(StringRef str) const; CommonData makeCommonData(); SoData makeSoData(const SharedFileExtended*); - - Elf64_Xword calculateHash(StringRef str) const; adlt_psod_t serialize(const SoData&) const; - size_t estimateBlobSize() const; + void finalizeOnWrite(); + template adlt_blob_array_t writeArray(uint8_t* buff, size_t offset, const SmallVector& data);