Skip to content

Instantly share code, notes, and snippets.

@rui314
Created January 20, 2016 04:12
Show Gist options
  • Save rui314/283099fd4e5214264d9e to your computer and use it in GitHub Desktop.
Save rui314/283099fd4e5214264d9e to your computer and use it in GitHub Desktop.
diff --git a/ELF/OutputSections.cpp b/ELF/OutputSections.cpp
index b885601..1a6fbc1 100644
--- a/ELF/OutputSections.cpp
+++ b/ELF/OutputSections.cpp
@@ -1385,18 +1385,22 @@ StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
// we obtained in the former function can be written in the latter.
// This design eliminated that need.
template <class ELFT> void StringTableSection<ELFT>::reserve(StringRef S) {
- Reserved += S.size() + 1; // +1 for NUL
+ if (Seen.insert(S).second)
+ Reserved += S.size() + 1; // +1 for NUL
}
// Adds a string to the string table. You must call reserve() with the
// same string before calling addString().
template <class ELFT> size_t StringTableSection<ELFT>::addString(StringRef S) {
- size_t Pos = Used;
+ size_t &Off = Offsets[S];
+ if (Off)
+ return Off;
+ Off = Used;
Strings.push_back(S);
Used += S.size() + 1;
Reserved -= S.size() + 1;
assert((int64_t)Reserved >= 0);
- return Pos;
+ return Off;
}
template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
diff --git a/ELF/OutputSections.h b/ELF/OutputSections.h
index 7132413..173b91b 100644
--- a/ELF/OutputSections.h
+++ b/ELF/OutputSections.h
@@ -19,6 +19,8 @@
#include "Config.h"
#include <type_traits>
+#include <set>
+#include <map>
namespace lld {
namespace elf2 {
@@ -339,6 +341,8 @@ public:
bool isDynamic() const { return Dynamic; }
private:
+ std::set<std::string> Seen;
+ std::map<std::string, size_t> Offsets;
const bool Dynamic;
std::vector<StringRef> Strings;
size_t Used = 1; // ELF string tables start with a NUL byte, so 1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment