2006-08-19 00:29:20 +02:00
|
|
|
// stringpool.h -- a string pool for gold -*- C++ -*-
|
|
|
|
|
2008-03-13 22:04:21 +01:00
|
|
|
// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
|
2007-09-22 23:02:10 +02:00
|
|
|
// Written by Ian Lance Taylor <iant@google.com>.
|
|
|
|
|
|
|
|
// This file is part of gold.
|
|
|
|
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
|
|
|
|
// MA 02110-1301, USA.
|
|
|
|
|
2006-08-19 00:29:20 +02:00
|
|
|
#include <string>
|
|
|
|
#include <list>
|
2007-12-19 02:23:46 +01:00
|
|
|
#include <vector>
|
2006-08-19 00:29:20 +02:00
|
|
|
|
|
|
|
#ifndef GOLD_STRINGPOOL_H
|
|
|
|
#define GOLD_STRINGPOOL_H
|
|
|
|
|
|
|
|
namespace gold
|
|
|
|
{
|
|
|
|
|
2006-09-29 21:58:17 +02:00
|
|
|
class Output_file;
|
|
|
|
|
2007-09-19 19:38:00 +02:00
|
|
|
// A Stringpool is a pool of unique strings. It provides the
|
|
|
|
// following features:
|
|
|
|
|
|
|
|
// Every string in the pool is unique. Thus, if you have two strings
|
|
|
|
// in the Stringpool, you can compare them for equality by using
|
|
|
|
// pointer comparison rather than string comparison.
|
|
|
|
|
|
|
|
// There is a key associated with every string in the pool. If you
|
|
|
|
// add strings to the Stringpool in the same order, then the key for
|
|
|
|
// each string will always be the same for any run of the linker.
|
|
|
|
// This is not true of the string pointers themselves, as they may
|
|
|
|
// change due to address space randomization. Some parts of the
|
|
|
|
// linker (e.g., the symbol table) use the key value instead of the
|
|
|
|
// string pointer so that repeated runs of the linker will generate
|
|
|
|
// precisely the same output.
|
|
|
|
|
2007-10-12 08:06:34 +02:00
|
|
|
// When you add a string to a Stringpool, Stringpool will optionally
|
|
|
|
// make a copy of it. Thus there is no requirement to keep a copy
|
|
|
|
// elsewhere.
|
2007-09-19 22:44:15 +02:00
|
|
|
|
2007-09-19 19:38:00 +02:00
|
|
|
// A Stringpool can be turned into a string table, a sequential series
|
|
|
|
// of null terminated strings. The first string may optionally be a
|
|
|
|
// single zero byte, as required for SHT_STRTAB sections. This
|
|
|
|
// conversion is only permitted after all strings have been added to
|
|
|
|
// the Stringpool. After doing this conversion, you can ask for the
|
2007-12-19 02:23:46 +01:00
|
|
|
// offset of any string (or any key) in the stringpool in the string
|
|
|
|
// table, and you can write the resulting string table to an output
|
|
|
|
// file.
|
2007-09-19 19:38:00 +02:00
|
|
|
|
|
|
|
// When a Stringpool is turned into a string table, then as an
|
|
|
|
// optimization it will reuse string suffixes to avoid duplicating
|
|
|
|
// strings. That is, given the strings "abc" and "bc", only the
|
|
|
|
// string "abc" will be stored, and "bc" will be represented by an
|
|
|
|
// offset into the middle of the string "abc".
|
|
|
|
|
2007-12-19 02:23:46 +01:00
|
|
|
|
|
|
|
// A simple chunked vector class--this is a subset of std::vector
|
|
|
|
// which stores memory in chunks. We don't provide iterators, because
|
|
|
|
// we don't need them.
|
|
|
|
|
|
|
|
template<typename Element>
|
|
|
|
class Chunked_vector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Chunked_vector()
|
2010-03-16 02:26:15 +01:00
|
|
|
: chunks_(), size_(0)
|
2007-12-19 02:23:46 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
// Clear the elements.
|
|
|
|
void
|
|
|
|
clear()
|
2010-03-16 02:26:15 +01:00
|
|
|
{
|
|
|
|
this->chunks_.clear();
|
|
|
|
this->size_ = 0;
|
|
|
|
}
|
2007-12-19 02:23:46 +01:00
|
|
|
|
|
|
|
// Reserve elements.
|
|
|
|
void
|
|
|
|
reserve(unsigned int n)
|
|
|
|
{
|
2010-03-16 02:26:15 +01:00
|
|
|
if (n > this->chunks_.size() * chunk_size)
|
2007-12-19 02:23:46 +01:00
|
|
|
{
|
2010-03-16 02:26:15 +01:00
|
|
|
this->chunks_.resize((n + chunk_size - 1) / chunk_size);
|
|
|
|
// We need to call reserve() of all chunks since changing
|
|
|
|
// this->chunks_ casues Element_vectors to be copied. The
|
|
|
|
// reserved capacity of an Element_vector may be lost in copying.
|
|
|
|
for (size_t i = 0; i < this->chunks_.size(); ++i)
|
|
|
|
this->chunks_[i].reserve(chunk_size);
|
2007-12-19 02:23:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the number of elements.
|
|
|
|
size_t
|
|
|
|
size() const
|
2010-03-16 02:26:15 +01:00
|
|
|
{ return this->size_; }
|
2007-12-19 02:23:46 +01:00
|
|
|
|
|
|
|
// Push a new element on the back of the vector.
|
|
|
|
void
|
|
|
|
push_back(const Element& element)
|
|
|
|
{
|
2010-03-16 02:26:15 +01:00
|
|
|
size_t chunk_index = this->size_ / chunk_size;
|
|
|
|
if (chunk_index >= this->chunks_.size())
|
2007-12-19 02:23:46 +01:00
|
|
|
{
|
|
|
|
this->chunks_.push_back(Element_vector());
|
|
|
|
this->chunks_.back().reserve(chunk_size);
|
2010-03-16 02:26:15 +01:00
|
|
|
gold_assert(chunk_index < this->chunks_.size());
|
2007-12-19 02:23:46 +01:00
|
|
|
}
|
2010-03-16 02:26:15 +01:00
|
|
|
this->chunks_[chunk_index].push_back(element);
|
|
|
|
this->size_++;
|
2007-12-19 02:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a reference to an entry in the vector.
|
|
|
|
Element&
|
|
|
|
operator[](size_t i)
|
|
|
|
{ return this->chunks_[i / chunk_size][i % chunk_size]; }
|
|
|
|
|
|
|
|
const Element&
|
|
|
|
operator[](size_t i) const
|
|
|
|
{ return this->chunks_[i / chunk_size][i % chunk_size]; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const unsigned int chunk_size = 8192;
|
|
|
|
|
|
|
|
typedef std::vector<Element> Element_vector;
|
|
|
|
typedef std::vector<Element_vector> Chunk_vector;
|
|
|
|
|
|
|
|
Chunk_vector chunks_;
|
2010-03-16 02:26:15 +01:00
|
|
|
size_t size_;
|
2007-12-19 02:23:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-09-19 19:38:00 +02:00
|
|
|
// Stringpools are implemented in terms of Stringpool_template, which
|
|
|
|
// is generalized on the type of character used for the strings. Most
|
|
|
|
// uses will want the Stringpool type which uses char. Other cases
|
|
|
|
// are used for merging wide string constants.
|
|
|
|
|
2007-05-16 19:42:48 +02:00
|
|
|
template<typename Stringpool_char>
|
|
|
|
class Stringpool_template
|
2006-08-19 00:29:20 +02:00
|
|
|
{
|
|
|
|
public:
|
2007-09-19 19:38:00 +02:00
|
|
|
// The type of a key into the stringpool. As described above, a key
|
|
|
|
// value will always be the same during any run of the linker. Zero
|
|
|
|
// is never a valid key value.
|
2006-11-07 05:40:46 +01:00
|
|
|
typedef size_t Key;
|
|
|
|
|
2007-09-21 08:12:32 +02:00
|
|
|
// Create a Stringpool.
|
|
|
|
Stringpool_template();
|
2006-08-19 00:29:20 +02:00
|
|
|
|
2007-05-16 19:42:48 +02:00
|
|
|
~Stringpool_template();
|
2006-08-19 00:29:20 +02:00
|
|
|
|
2007-11-30 01:35:27 +01:00
|
|
|
// Clear all the data from the stringpool.
|
|
|
|
void
|
|
|
|
clear();
|
|
|
|
|
2007-12-14 06:24:17 +01:00
|
|
|
// Hint to the stringpool class that you intend to insert n additional
|
|
|
|
// elements. The stringpool class can use this info however it likes;
|
|
|
|
// in practice it will resize its internal hashtables to make room.
|
|
|
|
void
|
|
|
|
reserve(unsigned int n);
|
|
|
|
|
2007-09-21 08:12:32 +02:00
|
|
|
// Indicate that we should not reserve offset 0 to hold the empty
|
|
|
|
// string when converting the stringpool to a string table. This
|
|
|
|
// should not be called for a proper ELF SHT_STRTAB section.
|
|
|
|
void
|
|
|
|
set_no_zero_null()
|
2010-03-16 02:26:15 +01:00
|
|
|
{
|
2010-03-19 20:11:21 +01:00
|
|
|
gold_assert(this->string_set_.empty()
|
|
|
|
&& this->offset_ == sizeof(Stringpool_char));
|
2010-03-16 02:26:15 +01:00
|
|
|
this->zero_null_ = false;
|
2010-03-19 20:11:21 +01:00
|
|
|
this->offset_ = 0;
|
2010-03-16 02:26:15 +01:00
|
|
|
}
|
2007-09-21 08:12:32 +02:00
|
|
|
|
2009-06-23 09:04:10 +02:00
|
|
|
// Indicate that this string pool should be optimized, even if not
|
|
|
|
// running with -O2.
|
|
|
|
void
|
|
|
|
set_optimize()
|
|
|
|
{ this->optimize_ = true; }
|
|
|
|
|
2007-09-19 19:38:00 +02:00
|
|
|
// Add the string S to the pool. This returns a canonical permanent
|
2007-10-12 08:06:34 +02:00
|
|
|
// pointer to the string in the pool. If COPY is true, the string
|
|
|
|
// is copied into permanent storage. If PKEY is not NULL, this sets
|
|
|
|
// *PKEY to the key for the string.
|
2007-05-16 19:42:48 +02:00
|
|
|
const Stringpool_char*
|
2007-10-12 08:06:34 +02:00
|
|
|
add(const Stringpool_char* s, bool copy, Key* pkey);
|
2006-08-19 00:29:20 +02:00
|
|
|
|
2007-12-19 01:29:28 +01:00
|
|
|
// Add string S of length LEN characters to the pool. If COPY is
|
|
|
|
// true, S need not be null terminated.
|
2007-05-16 19:42:48 +02:00
|
|
|
const Stringpool_char*
|
2007-12-19 01:29:28 +01:00
|
|
|
add_with_length(const Stringpool_char* s, size_t len, bool copy, Key* pkey);
|
2006-09-29 21:58:17 +02:00
|
|
|
|
2007-09-19 19:38:00 +02:00
|
|
|
// If the string S is present in the pool, return the canonical
|
|
|
|
// string pointer. Otherwise, return NULL. If PKEY is not NULL,
|
|
|
|
// set *PKEY to the key.
|
2007-05-16 19:42:48 +02:00
|
|
|
const Stringpool_char*
|
2007-09-19 19:38:00 +02:00
|
|
|
find(const Stringpool_char* s, Key* pkey) const;
|
2006-09-29 21:58:17 +02:00
|
|
|
|
2007-09-19 19:38:00 +02:00
|
|
|
// Turn the stringpool into a string table: determine the offsets of
|
|
|
|
// all the strings. After this is called, no more strings may be
|
|
|
|
// added to the stringpool.
|
2006-09-29 21:58:17 +02:00
|
|
|
void
|
|
|
|
set_string_offsets();
|
|
|
|
|
2007-09-19 19:38:00 +02:00
|
|
|
// Get the offset of the string S in the string table. This returns
|
|
|
|
// the offset in bytes, not in units of Stringpool_char. This may
|
|
|
|
// only be called after set_string_offsets has been called.
|
2007-12-18 01:48:04 +01:00
|
|
|
section_offset_type
|
2007-09-19 19:38:00 +02:00
|
|
|
get_offset(const Stringpool_char* s) const;
|
2006-09-29 21:58:17 +02:00
|
|
|
|
2007-09-19 19:38:00 +02:00
|
|
|
// Get the offset of the string S in the string table.
|
2007-12-18 01:48:04 +01:00
|
|
|
section_offset_type
|
2007-05-16 19:42:48 +02:00
|
|
|
get_offset(const std::basic_string<Stringpool_char>& s) const
|
2007-12-19 01:29:28 +01:00
|
|
|
{ return this->get_offset_with_length(s.c_str(), s.size()); }
|
|
|
|
|
|
|
|
// Get the offset of string S, with length LENGTH characters, in the
|
|
|
|
// string table.
|
|
|
|
section_offset_type
|
|
|
|
get_offset_with_length(const Stringpool_char* s, size_t length) const;
|
2006-09-29 21:58:17 +02:00
|
|
|
|
2007-12-19 02:23:46 +01:00
|
|
|
// Get the offset of the string with key K.
|
|
|
|
section_offset_type
|
|
|
|
get_offset_from_key(Key k) const
|
|
|
|
{
|
2008-07-24 09:23:20 +02:00
|
|
|
gold_assert(k <= this->key_to_offset_.size());
|
|
|
|
return this->key_to_offset_[k - 1];
|
2007-12-19 02:23:46 +01:00
|
|
|
}
|
|
|
|
|
2007-09-19 19:38:00 +02:00
|
|
|
// Get the size of the string table. This returns the number of
|
|
|
|
// bytes, not in units of Stringpool_char.
|
2007-12-18 01:48:04 +01:00
|
|
|
section_size_type
|
2006-09-29 21:58:17 +02:00
|
|
|
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
|
|
|
|
2007-09-19 19:38:00 +02:00
|
|
|
// Write the string table into the output file at the specified
|
|
|
|
// offset.
|
2006-09-29 21:58:17 +02:00
|
|
|
void
|
|
|
|
write(Output_file*, off_t offset);
|
2006-08-19 00:29:20 +02:00
|
|
|
|
2007-11-30 01:35:27 +01:00
|
|
|
// Write the string table into the specified buffer, of the
|
|
|
|
// specified size. buffer_size should be at least
|
|
|
|
// get_strtab_size().
|
|
|
|
void
|
2007-12-18 01:48:04 +01:00
|
|
|
write_to_buffer(unsigned char* buffer, section_size_type buffer_size);
|
2007-11-30 01:35:27 +01:00
|
|
|
|
2007-12-05 01:48:49 +01:00
|
|
|
// Dump statistical information to stderr.
|
|
|
|
void
|
|
|
|
print_stats(const char*) const;
|
|
|
|
|
2006-08-19 00:29:20 +02:00
|
|
|
private:
|
2007-05-16 19:42:48 +02:00
|
|
|
Stringpool_template(const Stringpool_template&);
|
|
|
|
Stringpool_template& operator=(const Stringpool_template&);
|
|
|
|
|
2007-09-19 19:38:00 +02:00
|
|
|
// Return the length of a string in units of Stringpool_char.
|
2007-05-16 19:42:48 +02:00
|
|
|
static size_t
|
|
|
|
string_length(const Stringpool_char*);
|
2006-08-19 00:29:20 +02:00
|
|
|
|
2007-12-05 23:56:51 +01:00
|
|
|
// Return whether two strings are equal.
|
|
|
|
static bool
|
|
|
|
string_equal(const Stringpool_char*, const Stringpool_char*);
|
|
|
|
|
|
|
|
// Compute a hash code for a string. LENGTH is the length of the
|
|
|
|
// string in characters.
|
|
|
|
static size_t
|
|
|
|
string_hash(const Stringpool_char*, size_t length);
|
|
|
|
|
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;
|
|
|
|
// Buffer.
|
|
|
|
char data[1];
|
|
|
|
};
|
|
|
|
|
2010-03-16 02:26:15 +01:00
|
|
|
// Add a new key offset entry.
|
|
|
|
void
|
|
|
|
new_key_offset(size_t);
|
|
|
|
|
2006-09-29 21:58:17 +02:00
|
|
|
// Copy a string into the buffers, returning a canonical string.
|
2007-05-16 19:42:48 +02:00
|
|
|
const Stringpool_char*
|
2007-12-19 02:23:46 +01:00
|
|
|
add_string(const Stringpool_char*, size_t);
|
2007-12-05 23:56:51 +01:00
|
|
|
|
|
|
|
// Return whether s1 is a suffix of s2.
|
|
|
|
static bool
|
|
|
|
is_suffix(const Stringpool_char* s1, size_t len1,
|
|
|
|
const Stringpool_char* s2, size_t len2);
|
|
|
|
|
|
|
|
// The hash table key includes the string, the length of the string,
|
|
|
|
// and the hash code for the string. We put the hash code
|
|
|
|
// explicitly into the key so that we can do a find()/insert()
|
|
|
|
// sequence without having to recompute the hash. Computing the
|
|
|
|
// hash code is a significant user of CPU time in the linker.
|
|
|
|
struct Hashkey
|
|
|
|
{
|
|
|
|
const Stringpool_char* string;
|
|
|
|
// Length is in characters, not bytes.
|
|
|
|
size_t length;
|
|
|
|
size_t hash_code;
|
|
|
|
|
|
|
|
// This goes in an STL container, so we need a default
|
|
|
|
// constructor.
|
|
|
|
Hashkey()
|
|
|
|
: string(NULL), length(0), hash_code(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
// Note that these constructors are relatively expensive, because
|
|
|
|
// they compute the hash code.
|
2007-12-19 01:29:28 +01:00
|
|
|
explicit Hashkey(const Stringpool_char* s)
|
2007-12-05 23:56:51 +01:00
|
|
|
: string(s), length(string_length(s)), hash_code(string_hash(s, length))
|
|
|
|
{ }
|
|
|
|
|
|
|
|
Hashkey(const Stringpool_char* s, size_t len)
|
|
|
|
: string(s), length(len), hash_code(string_hash(s, len))
|
|
|
|
{ }
|
|
|
|
};
|
2006-08-19 00:29:20 +02:00
|
|
|
|
2007-12-05 23:56:51 +01:00
|
|
|
// Hash function. This is trivial, since we have already computed
|
|
|
|
// the hash.
|
2006-08-19 00:29:20 +02:00
|
|
|
struct Stringpool_hash
|
|
|
|
{
|
|
|
|
size_t
|
2007-12-05 23:56:51 +01:00
|
|
|
operator()(const Hashkey& hk) const
|
|
|
|
{ return hk.hash_code; }
|
2006-08-19 00:29:20 +02:00
|
|
|
};
|
|
|
|
|
2007-09-19 19:38:00 +02:00
|
|
|
// Equality comparison function for hash table.
|
2006-08-19 00:29:20 +02:00
|
|
|
struct Stringpool_eq
|
|
|
|
{
|
|
|
|
bool
|
2007-12-05 23:56:51 +01:00
|
|
|
operator()(const Hashkey&, const Hashkey&) const;
|
2006-08-19 00:29:20 +02:00
|
|
|
};
|
|
|
|
|
2007-12-19 02:23:46 +01:00
|
|
|
// The hash table is a map from strings to Keys.
|
2006-11-07 05:40:46 +01:00
|
|
|
|
2007-12-19 02:23:46 +01:00
|
|
|
typedef Key Hashval;
|
2006-09-29 21:58:17 +02:00
|
|
|
|
2007-12-05 23:56:51 +01:00
|
|
|
typedef Unordered_map<Hashkey, Hashval, Stringpool_hash,
|
2006-09-29 21:58:17 +02:00
|
|
|
Stringpool_eq> String_set_type;
|
2006-09-26 23:50:25 +02:00
|
|
|
|
2007-12-05 23:56:51 +01:00
|
|
|
// Comparison routine used when sorting into a string table.
|
|
|
|
|
|
|
|
typedef typename String_set_type::iterator Stringpool_sort_info;
|
2006-09-29 21:58:17 +02:00
|
|
|
|
|
|
|
struct Stringpool_sort_comparison
|
|
|
|
{
|
|
|
|
bool
|
2007-09-18 07:16:39 +02:00
|
|
|
operator()(const Stringpool_sort_info&, const Stringpool_sort_info&) const;
|
2006-09-29 21:58:17 +02:00
|
|
|
};
|
|
|
|
|
2007-12-19 02:23:46 +01:00
|
|
|
// Keys map to offsets via a Chunked_vector. We only use the
|
|
|
|
// offsets if we turn this into an string table section.
|
|
|
|
typedef Chunked_vector<section_offset_type> Key_to_offset;
|
|
|
|
|
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_;
|
2007-12-19 02:23:46 +01:00
|
|
|
// Mapping from Key to string table offset.
|
|
|
|
Key_to_offset key_to_offset_;
|
2006-11-07 05:40:46 +01:00
|
|
|
// List of buffers.
|
|
|
|
Stringdata_list strings_;
|
2007-09-19 19:38:00 +02:00
|
|
|
// Size of string table.
|
2007-12-18 01:48:04 +01:00
|
|
|
section_size_type strtab_size_;
|
2007-05-16 19:42:48 +02:00
|
|
|
// Whether to reserve offset 0 to hold the null string.
|
|
|
|
bool zero_null_;
|
2009-06-23 09:04:10 +02:00
|
|
|
// Whether to optimize the string table.
|
|
|
|
bool optimize_;
|
2010-03-16 02:26:15 +01:00
|
|
|
// offset of the next string.
|
|
|
|
section_offset_type offset_;
|
2006-08-19 00:29:20 +02:00
|
|
|
};
|
|
|
|
|
2007-05-16 19:42:48 +02:00
|
|
|
// The most common type of Stringpool.
|
|
|
|
typedef Stringpool_template<char> Stringpool;
|
|
|
|
|
2006-08-19 00:29:20 +02:00
|
|
|
} // End namespace gold.
|
|
|
|
|
|
|
|
#endif // !defined(GOLD_STRINGPOOL_H)
|