| 1 | // strtab.h -- manage an ELF string table for gold -*- C++ -*- |
| 2 | |
| 3 | #ifndef GOLD_STRTAB_H |
| 4 | #define GOLD_STRTAB_H |
| 5 | |
| 6 | #include <cstring> |
| 7 | #include <string> |
| 8 | |
| 9 | namespace gold |
| 10 | { |
| 11 | |
| 12 | // This class holds an ELF string table. We keep a reference count |
| 13 | // for each string, which we use to determine which strings are |
| 14 | // actually required at the end. When all operations are done, the |
| 15 | // string table is finalized, which sets the offsets to use for each |
| 16 | // string. |
| 17 | |
| 18 | class Strtab |
| 19 | { |
| 20 | public: |
| 21 | Strtab(); |
| 22 | |
| 23 | ~Strtab(); |
| 24 | |
| 25 | Strtab_ref* add(const char*); |
| 26 | |
| 27 | Strtab_ref* add(const std::string& s) |
| 28 | { return this->add(s.c_str()); } |
| 29 | |
| 30 | private: |
| 31 | Strtab(const Strtab&); |
| 32 | Strtab& operator=(const Strtab&); |
| 33 | |
| 34 | struct strtab_hash |
| 35 | { |
| 36 | std::size_t |
| 37 | operator()(const char*p); |
| 38 | }; |
| 39 | |
| 40 | struct strtab_eq |
| 41 | { |
| 42 | bool |
| 43 | operator()(const char* p1, const char* p2) |
| 44 | { return strcmp(p1, p2) == 0; } |
| 45 | }; |
| 46 | |
| 47 | Unordered_map<const char*, Strtab_ref*, strtab_hash, strtab_eq, |
| 48 | std::allocator<std::pair<const char* const, Strtab_ref*> >, |
| 49 | true> strings_; |
| 50 | }; |
| 51 | |
| 52 | // Users of Strtab work with pointers to Strtab_ref structures. These |
| 53 | // are allocated via new and should be deleted if the string is no |
| 54 | // longer needed. |
| 55 | |
| 56 | class Strtab_ref |
| 57 | { |
| 58 | public: |
| 59 | ~Strtab_ref(); |
| 60 | |
| 61 | const char* |
| 62 | str() const; |
| 63 | |
| 64 | private: |
| 65 | Strtab_ref(const Strtab_ref&); |
| 66 | Strtab_ref& operator=(const Strtab_ref&); |
| 67 | |
| 68 | int refs_; |
| 69 | }; |
| 70 | |
| 71 | } // End namespace gold. |
| 72 | |
| 73 | #endif // !defined(GOLD_STRTAB_H) |