From 6eafc2ebc2d844a7dd57346c7e413bd30d1e25b3 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Wed, 21 Jun 2017 18:23:19 +0000 Subject: [PATCH] Object: Have the irsymtab builder take a string table builder. NFCI. This will be needed in order to share the irsymtab string table with the bitcode string table. Differential Revision: https://reviews.llvm.org/D33971 llvm-svn: 305937 --- include/llvm/Object/IRSymtab.h | 6 ++++-- lib/Object/IRSymtab.cpp | 36 ++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/include/llvm/Object/IRSymtab.h b/include/llvm/Object/IRSymtab.h index 5b832141a86..502f133d307 100644 --- a/include/llvm/Object/IRSymtab.h +++ b/include/llvm/Object/IRSymtab.h @@ -38,6 +38,7 @@ namespace llvm { struct BitcodeFileContents; +class StringTableBuilder; namespace irsymtab { @@ -136,9 +137,10 @@ struct Header { } // end namespace storage -/// Fills in Symtab and Strtab with a valid symbol and string table for Mods. +/// Fills in Symtab and StrtabBuilder with a valid symbol and string table for +/// Mods. Error build(ArrayRef Mods, SmallVector &Symtab, - SmallVector &Strtab); + StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc); /// This represents a symbol that has been read from a storage::Symbol and /// possibly a storage::Uncommon. diff --git a/lib/Object/IRSymtab.cpp b/lib/Object/IRSymtab.cpp index a6cd5dda12d..7bca032a7be 100644 --- a/lib/Object/IRSymtab.cpp +++ b/lib/Object/IRSymtab.cpp @@ -46,15 +46,15 @@ namespace { /// Stores the temporary state that is required to build an IR symbol table. struct Builder { SmallVector &Symtab; - SmallVector &Strtab; + StringTableBuilder &StrtabBuilder; + StringSaver Saver; - Builder(SmallVector &Symtab, SmallVector &Strtab) - : Symtab(Symtab), Strtab(Strtab) {} - - StringTableBuilder StrtabBuilder{StringTableBuilder::RAW}; - - BumpPtrAllocator Alloc; - StringSaver Saver{Alloc}; + // This ctor initializes a StringSaver using the passed in BumpPtrAllocator. + // The StringTableBuilder does not create a copy of any strings added to it, + // so this provides somewhere to store any strings that we create. + Builder(SmallVector &Symtab, StringTableBuilder &StrtabBuilder, + BumpPtrAllocator &Alloc) + : Symtab(Symtab), StrtabBuilder(StrtabBuilder), Saver(Alloc) {} DenseMap ComdatMap; Mangler Mang; @@ -240,7 +240,7 @@ Error Builder::build(ArrayRef IRMods) { return Err; COFFLinkerOptsOS.flush(); - setStr(Hdr.COFFLinkerOpts, COFFLinkerOpts); + setStr(Hdr.COFFLinkerOpts, Saver.save(COFFLinkerOpts)); // We are about to fill in the header's range fields, so reserve space for it // and copy it in afterwards. @@ -251,19 +251,15 @@ Error Builder::build(ArrayRef IRMods) { writeRange(Hdr.Uncommons, Uncommons); *reinterpret_cast(Symtab.data()) = Hdr; - - raw_svector_ostream OS(Strtab); - StrtabBuilder.finalizeInOrder(); - StrtabBuilder.write(OS); - return Error::success(); } } // end anonymous namespace Error irsymtab::build(ArrayRef Mods, SmallVector &Symtab, - SmallVector &Strtab) { - return Builder(Symtab, Strtab).build(Mods); + StringTableBuilder &StrtabBuilder, + BumpPtrAllocator &Alloc) { + return Builder(Symtab, StrtabBuilder, Alloc).build(Mods); } // Upgrade a vector of bitcode modules created by an old version of LLVM by @@ -285,9 +281,15 @@ static Expected upgrade(ArrayRef BMs) { OwnedMods.push_back(std::move(*MOrErr)); } - if (Error E = build(Mods, FC.Symtab, FC.Strtab)) + StringTableBuilder StrtabBuilder(StringTableBuilder::RAW); + BumpPtrAllocator Alloc; + if (Error E = build(Mods, FC.Symtab, StrtabBuilder, Alloc)) return std::move(E); + StrtabBuilder.finalizeInOrder(); + FC.Strtab.resize(StrtabBuilder.getSize()); + StrtabBuilder.write((uint8_t *)FC.Strtab.data()); + FC.TheReader = {{FC.Symtab.data(), FC.Symtab.size()}, {FC.Strtab.data(), FC.Strtab.size()}}; return std::move(FC);