From 87edc8b08c3f769071d77f576592f661d173055b Mon Sep 17 00:00:00 2001 From: Khomutov Nikita Date: Thu, 11 Apr 2024 10:04:43 +0300 Subject: [PATCH] [ADLT] write overall mapped image size to .adlt Signed-off-by: Khomutov Nikita Change-Id: I83b40245dc6700f0714a0910fd8f4878cdbceeb1 --- lld/ELF/SyntheticSections.cpp | 21 ++++++++++++++++++--- lld/ELF/SyntheticSections.h | 7 ++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 33ae89538d56..4e368aadf62a 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 c69b95704406..a93ff052cfe3 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); -- Gitee