From: Shankar Easwaran Date: Fri, 1 Feb 2013 18:06:15 +0000 (+0000) Subject: remove duplicate strings from the string table X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a47495898c87d7e5802891695c5984380da2692c;p=platform%2Fupstream%2Fllvm.git remove duplicate strings from the string table llvm-svn: 174200 --- diff --git a/lld/lib/ReaderWriter/ELF/SectionChunks.h b/lld/lib/ReaderWriter/ELF/SectionChunks.h index 6fea1bc..f9dbd13 100644 --- a/lld/lib/ReaderWriter/ELF/SectionChunks.h +++ b/lld/lib/ReaderWriter/ELF/SectionChunks.h @@ -20,7 +20,8 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/OwningPtr.h" -#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/Object/ELF.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/Debug.h" @@ -531,11 +532,26 @@ public: private: std::vector _strings; + + struct StringRefMappingInfo { + static StringRef getEmptyKey() { return StringRef(); } + static StringRef getTombstoneKey() { return StringRef(" ", 0); } + static unsigned getHashValue(StringRef const val) { + return llvm::HashString(val); + } + static bool isEqual(StringRef const lhs, StringRef const rhs) { + return lhs.equals(rhs); + } + }; + typedef typename llvm::DenseMap StringMapT; + typedef typename StringMapT::iterator StringMapTIter; + StringMapT _stringMap; }; template StringTable::StringTable(const ELFTargetInfo &ti, const char *str, - int32_t order) + int32_t order) : Section(ti, str, llvm::ELF::SHT_STRTAB, DefinedAtom::perm___, order, Section::K_StringTable) { // the string table has a NULL entry for which @@ -547,15 +563,23 @@ StringTable::StringTable(const ELFTargetInfo &ti, const char *str, } template uint64_t StringTable::addString(StringRef symname) { - _strings.push_back(symname); - uint64_t offset = this->_fsize; - this->_fsize += symname.size() + 1; - return offset; + + if (symname.size() == 0) + return 0; + StringMapTIter stringIter = _stringMap.find(symname); + if (stringIter == _stringMap.end()) { + _strings.push_back(symname); + uint64_t offset = this->_fsize; + this->_fsize += symname.size() + 1; + _stringMap[symname] = offset; + return offset; + } + return stringIter->second; } template void StringTable::write(ELFWriter *writer, - llvm::FileOutputBuffer &buffer) { + llvm::FileOutputBuffer &buffer) { uint8_t *chunkBuffer = buffer.getBufferStart(); uint8_t *dest = chunkBuffer + this->fileOffset(); for (auto si : _strings) {