2006-08-19 00:29:20 +02:00
|
|
|
// stringpool.h -- a string pool for gold -*- C++ -*-
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
// Stringpool
|
|
|
|
// Manage a pool of unique strings.
|
|
|
|
|
|
|
|
#ifndef GOLD_STRINGPOOL_H
|
|
|
|
#define GOLD_STRINGPOOL_H
|
|
|
|
|
|
|
|
namespace gold
|
|
|
|
{
|
|
|
|
|
2006-09-29 21:58:17 +02:00
|
|
|
class Output_file;
|
|
|
|
|
2006-08-19 00:29:20 +02:00
|
|
|
class Stringpool
|
|
|
|
{
|
|
|
|
public:
|
2006-11-07 05:40:46 +01:00
|
|
|
// The type of a key into the stringpool. A key value will always
|
|
|
|
// be the same during any run of the linker. The string pointers
|
|
|
|
// may change when using address space randomization. We use key
|
|
|
|
// values in order to get repeatable runs when the value is inserted
|
|
|
|
// into an unordered hash table. Zero is never a valid key.
|
|
|
|
typedef size_t Key;
|
|
|
|
|
2006-08-19 00:29:20 +02:00
|
|
|
Stringpool();
|
|
|
|
|
|
|
|
~Stringpool();
|
|
|
|
|
|
|
|
// Add a string to the pool. This returns a canonical permanent
|
2006-11-07 05:40:46 +01:00
|
|
|
// pointer to the string. If PKEY is not NULL, this sets *PKEY to
|
|
|
|
// the key for the string.
|
2006-09-29 21:58:17 +02:00
|
|
|
const char*
|
2006-11-07 05:40:46 +01:00
|
|
|
add(const char*, Key* pkey);
|
2006-08-19 00:29:20 +02:00
|
|
|
|
2006-09-29 21:58:17 +02:00
|
|
|
const char*
|
2006-11-07 05:40:46 +01:00
|
|
|
add(const std::string& s, Key* pkey)
|
|
|
|
{ return this->add(s.c_str(), pkey); }
|
2006-08-19 00:29:20 +02:00
|
|
|
|
|
|
|
// Add the prefix of a string to the pool.
|
2006-09-29 21:58:17 +02:00
|
|
|
const char*
|
2006-11-07 05:40:46 +01:00
|
|
|
add(const char *, size_t, Key* pkey);
|
2006-09-29 21:58:17 +02:00
|
|
|
|
|
|
|
// If a string is present, return the canonical string. Otherwise,
|
2006-11-07 05:40:46 +01:00
|
|
|
// return NULL. If PKEY is not NULL, set *PKEY to the key.
|
2006-09-29 21:58:17 +02:00
|
|
|
const char*
|
2006-11-07 05:40:46 +01:00
|
|
|
find(const char*, Key* pkey) const;
|
2006-09-29 21:58:17 +02:00
|
|
|
|
|
|
|
// Turn the stringpool into an ELF strtab: determine the offsets of
|
|
|
|
// all the strings.
|
|
|
|
void
|
|
|
|
set_string_offsets();
|
|
|
|
|
2006-11-07 05:40:46 +01:00
|
|
|
// Get the offset of a string in an ELF strtab.
|
2006-09-29 21:58:17 +02:00
|
|
|
off_t
|
|
|
|
get_offset(const char*) const;
|
|
|
|
|
|
|
|
off_t
|
|
|
|
get_offset(const std::string& s) const
|
|
|
|
{ return this->get_offset(s.c_str()); }
|
|
|
|
|
|
|
|
// Get the size of the ELF strtab.
|
|
|
|
off_t
|
|
|
|
get_strtab_size() const
|
2006-11-29 18:56:40 +01:00
|
|
|
{
|
|
|
|
gold_assert(this->strtab_size_ != 0);
|
|
|
|
return this->strtab_size_;
|
|
|
|
}
|
2006-09-29 21:58:17 +02:00
|
|
|
|
|
|
|
// Write the strtab into the output file at the specified offset.
|
|
|
|
void
|
|
|
|
write(Output_file*, off_t offset);
|
2006-08-19 00:29:20 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
Stringpool(const Stringpool&);
|
|
|
|
Stringpool& operator=(const Stringpool&);
|
|
|
|
|
2006-09-29 21:58:17 +02:00
|
|
|
// We store the actual data in a list of these buffers.
|
|
|
|
struct Stringdata
|
2006-08-19 00:29:20 +02:00
|
|
|
{
|
|
|
|
// Length of data in buffer.
|
|
|
|
size_t len;
|
|
|
|
// Allocated size of buffer.
|
|
|
|
size_t alc;
|
2006-11-07 05:40:46 +01:00
|
|
|
// Buffer index.
|
|
|
|
unsigned int index;
|
2006-08-19 00:29:20 +02:00
|
|
|
// Buffer.
|
|
|
|
char data[1];
|
|
|
|
};
|
|
|
|
|
2006-09-29 21:58:17 +02:00
|
|
|
// Copy a string into the buffers, returning a canonical string.
|
|
|
|
const char*
|
2006-11-07 05:40:46 +01:00
|
|
|
add_string(const char*, Key*);
|
2006-08-19 00:29:20 +02:00
|
|
|
|
|
|
|
struct Stringpool_hash
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const char*) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Stringpool_eq
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
operator()(const char* p1, const char* p2) const
|
|
|
|
{ return strcmp(p1, p2) == 0; }
|
|
|
|
};
|
|
|
|
|
2006-09-29 21:58:17 +02:00
|
|
|
// Return whether s1 is a suffix of s2.
|
|
|
|
static bool is_suffix(const char* s1, const char* s2);
|
|
|
|
|
2006-11-07 05:40:46 +01:00
|
|
|
// The hash table is a map from string names to a pair of Key and
|
|
|
|
// ELF strtab offsets. We only use the offsets if we turn this into
|
|
|
|
// an ELF strtab section.
|
|
|
|
|
|
|
|
typedef std::pair<Key, off_t> Val;
|
2006-09-29 21:58:17 +02:00
|
|
|
|
2006-09-26 23:50:25 +02:00
|
|
|
#ifdef HAVE_TR1_UNORDERED_SET
|
2006-11-07 05:40:46 +01:00
|
|
|
typedef Unordered_map<const char*, Val, Stringpool_hash,
|
2006-09-29 21:58:17 +02:00
|
|
|
Stringpool_eq,
|
2006-11-07 05:40:46 +01:00
|
|
|
std::allocator<std::pair<const char* const, Val> >,
|
2006-08-19 00:29:20 +02:00
|
|
|
true> String_set_type;
|
2006-09-26 23:50:25 +02:00
|
|
|
#else
|
2006-11-07 05:40:46 +01:00
|
|
|
typedef Unordered_map<const char*, Val, Stringpool_hash,
|
2006-09-29 21:58:17 +02:00
|
|
|
Stringpool_eq> String_set_type;
|
2006-09-26 23:50:25 +02:00
|
|
|
#endif
|
|
|
|
|
2006-09-29 21:58:17 +02:00
|
|
|
// Comparison routine used when sorting into an ELF strtab.
|
|
|
|
|
|
|
|
struct Stringpool_sort_comparison
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
operator()(String_set_type::iterator,
|
|
|
|
String_set_type::iterator) const;
|
|
|
|
};
|
|
|
|
|
2006-11-07 05:40:46 +01:00
|
|
|
// List of Stringdata structures.
|
|
|
|
typedef std::list<Stringdata*> Stringdata_list;
|
|
|
|
|
|
|
|
// Mapping from const char* to namepool entry.
|
2006-08-19 00:29:20 +02:00
|
|
|
String_set_type string_set_;
|
2006-11-07 05:40:46 +01:00
|
|
|
// List of buffers.
|
|
|
|
Stringdata_list strings_;
|
|
|
|
// Size of ELF strtab.
|
2006-09-29 21:58:17 +02:00
|
|
|
off_t strtab_size_;
|
2006-11-07 05:40:46 +01:00
|
|
|
// Next Stringdata index.
|
|
|
|
unsigned int next_index_;
|
2006-08-19 00:29:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End namespace gold.
|
|
|
|
|
|
|
|
#endif // !defined(GOLD_STRINGPOOL_H)
|