From a4c381a26a186b4e83ef4e435850c0340ae05b3f Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Sun, 24 Jan 2016 20:46:11 +0000 Subject: [PATCH] [COFF] Simplify SetSectionName Consolidate the code which handles string table offsets less than 999999 with the code for offsets less than 9999999. While we are here, simplify the code by not using sprintf to generate the string. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@258664 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/MC/WinCOFFObjectWriter.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/MC/WinCOFFObjectWriter.cpp b/lib/MC/WinCOFFObjectWriter.cpp index 36b317eebb9..825e488755b 100644 --- a/lib/MC/WinCOFFObjectWriter.cpp +++ b/lib/MC/WinCOFFObjectWriter.cpp @@ -426,9 +426,8 @@ void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol, } // Maximum offsets for different string table entry encodings. -static const unsigned Max6DecimalOffset = 999999; -static const unsigned Max7DecimalOffset = 9999999; -static const uint64_t MaxBase64Offset = 0xFFFFFFFFFULL; // 64^6, including 0 +enum : unsigned { Max7DecimalOffset = 9999999U }; +enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0 // Encode a string table entry offset in base 64, padded to 6 chars, and // prefixed with a double slash: '//AAAAAA', '//AAAAAB', ... @@ -456,22 +455,21 @@ void WinCOFFObjectWriter::SetSectionName(COFFSection &S) { if (S.Name.size() > COFF::NameSize) { uint64_t StringTableEntry = Strings.getOffset(S.Name); - if (StringTableEntry <= Max6DecimalOffset) { - std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry)); - } else if (StringTableEntry <= Max7DecimalOffset) { - // With seven digits, we have to skip the terminating null. Because - // sprintf always appends it, we use a larger temporary buffer. - char buffer[9] = {}; - std::sprintf(buffer, "/%d", unsigned(StringTableEntry)); - std::memcpy(S.Header.Name, buffer, 8); + if (StringTableEntry <= Max7DecimalOffset) { + SmallVector Buffer; + Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer); + assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2); + + std::memcpy(S.Header.Name, Buffer.data(), Buffer.size()); } else if (StringTableEntry <= MaxBase64Offset) { // Starting with 10,000,000, offsets are encoded as base64. encodeBase64StringEntry(S.Header.Name, StringTableEntry); } else { report_fatal_error("COFF string table is greater than 64 GB."); } - } else + } else { std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size()); + } } void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {