2006-08-19 06:29:20 +08:00
|
|
|
// stringpool.h -- a string pool for gold -*- C++ -*-
|
|
|
|
|
2007-09-23 05:02:10 +08:00
|
|
|
// Copyright 2006, 2007 Free Software Foundation, Inc.
|
|
|
|
// 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 06:29:20 +08:00
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
#ifndef GOLD_STRINGPOOL_H
|
|
|
|
#define GOLD_STRINGPOOL_H
|
|
|
|
|
|
|
|
namespace gold
|
|
|
|
{
|
|
|
|
|
2006-09-30 03:58:17 +08:00
|
|
|
class Output_file;
|
|
|
|
|
2007-09-20 01:38:00 +08: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-09-20 04:44:15 +08:00
|
|
|
// When you add a string to a Stringpool, Stringpool will make a copy
|
|
|
|
// of it. Thus there is no need to keep a copy elsewhere.
|
|
|
|
|
2007-09-20 01:38:00 +08: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
|
|
|
|
// offset of any string in the stringpool in the string table, and you
|
|
|
|
// can write the resulting string table to an output file.
|
|
|
|
|
|
|
|
// 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".
|
|
|
|
|
|
|
|
// 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-17 01:42:48 +08:00
|
|
|
template<typename Stringpool_char>
|
|
|
|
class Stringpool_template
|
2006-08-19 06:29:20 +08:00
|
|
|
{
|
|
|
|
public:
|
2007-09-20 01:38:00 +08: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 12:40:46 +08:00
|
|
|
typedef size_t Key;
|
|
|
|
|
2007-09-21 14:12:32 +08:00
|
|
|
// Create a Stringpool.
|
|
|
|
Stringpool_template();
|
2006-08-19 06:29:20 +08:00
|
|
|
|
2007-05-17 01:42:48 +08:00
|
|
|
~Stringpool_template();
|
2006-08-19 06:29:20 +08:00
|
|
|
|
2007-09-21 14:12:32 +08: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()
|
|
|
|
{ this->zero_null_ = false; }
|
|
|
|
|
2007-09-20 01:38:00 +08:00
|
|
|
// Add the string S to the pool. This returns a canonical permanent
|
|
|
|
// pointer to the string in the pool. If PKEY is not NULL, this
|
|
|
|
// sets *PKEY to the key for the string.
|
2007-05-17 01:42:48 +08:00
|
|
|
const Stringpool_char*
|
2007-09-20 01:38:00 +08:00
|
|
|
add(const Stringpool_char* s, Key* pkey);
|
2006-08-19 06:29:20 +08:00
|
|
|
|
2007-09-20 01:38:00 +08:00
|
|
|
// Add the string S to the pool.
|
2007-05-17 01:42:48 +08:00
|
|
|
const Stringpool_char*
|
|
|
|
add(const std::basic_string<Stringpool_char>& s, Key* pkey)
|
2006-11-07 12:40:46 +08:00
|
|
|
{ return this->add(s.c_str(), pkey); }
|
2006-08-19 06:29:20 +08:00
|
|
|
|
2007-09-20 01:38:00 +08:00
|
|
|
// Add the prefix of length LEN of string S to the pool.
|
2007-05-17 01:42:48 +08:00
|
|
|
const Stringpool_char*
|
2007-09-20 01:38:00 +08:00
|
|
|
add(const Stringpool_char* s, size_t len, Key* pkey);
|
2006-09-30 03:58:17 +08:00
|
|
|
|
2007-09-20 01:38:00 +08: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-17 01:42:48 +08:00
|
|
|
const Stringpool_char*
|
2007-09-20 01:38:00 +08:00
|
|
|
find(const Stringpool_char* s, Key* pkey) const;
|
2006-09-30 03:58:17 +08:00
|
|
|
|
2007-09-20 01:38:00 +08: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-30 03:58:17 +08:00
|
|
|
void
|
|
|
|
set_string_offsets();
|
|
|
|
|
2007-09-20 01:38:00 +08: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.
|
2006-09-30 03:58:17 +08:00
|
|
|
off_t
|
2007-09-20 01:38:00 +08:00
|
|
|
get_offset(const Stringpool_char* s) const;
|
2006-09-30 03:58:17 +08:00
|
|
|
|
2007-09-20 01:38:00 +08:00
|
|
|
// Get the offset of the string S in the string table.
|
2006-09-30 03:58:17 +08:00
|
|
|
off_t
|
2007-05-17 01:42:48 +08:00
|
|
|
get_offset(const std::basic_string<Stringpool_char>& s) const
|
2006-09-30 03:58:17 +08:00
|
|
|
{ return this->get_offset(s.c_str()); }
|
|
|
|
|
2007-09-20 01:38:00 +08:00
|
|
|
// Get the size of the string table. This returns the number of
|
|
|
|
// bytes, not in units of Stringpool_char.
|
2006-09-30 03:58:17 +08:00
|
|
|
off_t
|
|
|
|
get_strtab_size() const
|
2006-11-30 01:56:40 +08:00
|
|
|
{
|
|
|
|
gold_assert(this->strtab_size_ != 0);
|
|
|
|
return this->strtab_size_;
|
|
|
|
}
|
2006-09-30 03:58:17 +08:00
|
|
|
|
2007-09-20 01:38:00 +08:00
|
|
|
// Write the string table into the output file at the specified
|
|
|
|
// offset.
|
2006-09-30 03:58:17 +08:00
|
|
|
void
|
|
|
|
write(Output_file*, off_t offset);
|
2006-08-19 06:29:20 +08:00
|
|
|
|
|
|
|
private:
|
2007-05-17 01:42:48 +08:00
|
|
|
Stringpool_template(const Stringpool_template&);
|
|
|
|
Stringpool_template& operator=(const Stringpool_template&);
|
|
|
|
|
2007-09-20 01:38:00 +08:00
|
|
|
// Return the length of a string in units of Stringpool_char.
|
2007-05-17 01:42:48 +08:00
|
|
|
static size_t
|
|
|
|
string_length(const Stringpool_char*);
|
2006-08-19 06:29:20 +08:00
|
|
|
|
2006-09-30 03:58:17 +08:00
|
|
|
// We store the actual data in a list of these buffers.
|
|
|
|
struct Stringdata
|
2006-08-19 06:29:20 +08:00
|
|
|
{
|
|
|
|
// Length of data in buffer.
|
|
|
|
size_t len;
|
|
|
|
// Allocated size of buffer.
|
|
|
|
size_t alc;
|
2006-11-07 12:40:46 +08:00
|
|
|
// Buffer index.
|
|
|
|
unsigned int index;
|
2006-08-19 06:29:20 +08:00
|
|
|
// Buffer.
|
|
|
|
char data[1];
|
|
|
|
};
|
|
|
|
|
2006-09-30 03:58:17 +08:00
|
|
|
// Copy a string into the buffers, returning a canonical string.
|
2007-05-17 01:42:48 +08:00
|
|
|
const Stringpool_char*
|
|
|
|
add_string(const Stringpool_char*, Key*);
|
2006-08-19 06:29:20 +08:00
|
|
|
|
2007-09-20 01:38:00 +08:00
|
|
|
// Hash function.
|
2006-08-19 06:29:20 +08:00
|
|
|
struct Stringpool_hash
|
|
|
|
{
|
|
|
|
size_t
|
2007-05-17 01:42:48 +08:00
|
|
|
operator()(const Stringpool_char*) const;
|
2006-08-19 06:29:20 +08:00
|
|
|
};
|
|
|
|
|
2007-09-20 01:38:00 +08:00
|
|
|
// Equality comparison function for hash table.
|
2006-08-19 06:29:20 +08:00
|
|
|
struct Stringpool_eq
|
|
|
|
{
|
|
|
|
bool
|
2007-05-17 01:42:48 +08:00
|
|
|
operator()(const Stringpool_char* p1, const Stringpool_char* p2) const;
|
2006-08-19 06:29:20 +08:00
|
|
|
};
|
|
|
|
|
2006-09-30 03:58:17 +08:00
|
|
|
// Return whether s1 is a suffix of s2.
|
2007-05-17 01:42:48 +08:00
|
|
|
static bool
|
2007-09-18 13:16:39 +08:00
|
|
|
is_suffix(const Stringpool_char* s1, size_t len1,
|
|
|
|
const Stringpool_char* s2, size_t len2);
|
2006-09-30 03:58:17 +08:00
|
|
|
|
2006-11-07 12:40:46 +08:00
|
|
|
// The hash table is a map from string names to a pair of Key and
|
2007-09-20 01:38:00 +08:00
|
|
|
// string table offsets. We only use the offsets if we turn this
|
|
|
|
// into an string table section.
|
2006-11-07 12:40:46 +08:00
|
|
|
|
|
|
|
typedef std::pair<Key, off_t> Val;
|
2006-09-30 03:58:17 +08:00
|
|
|
|
2006-09-27 05:50:25 +08:00
|
|
|
#ifdef HAVE_TR1_UNORDERED_SET
|
2007-05-17 01:42:48 +08:00
|
|
|
typedef Unordered_map<const Stringpool_char*, Val, Stringpool_hash,
|
2006-09-30 03:58:17 +08:00
|
|
|
Stringpool_eq,
|
2007-05-17 01:42:48 +08:00
|
|
|
std::allocator<std::pair<const Stringpool_char* const,
|
|
|
|
Val> >,
|
2006-08-19 06:29:20 +08:00
|
|
|
true> String_set_type;
|
2006-09-27 05:50:25 +08:00
|
|
|
#else
|
2007-05-17 01:42:48 +08:00
|
|
|
typedef Unordered_map<const Stringpool_char*, Val, Stringpool_hash,
|
2006-09-30 03:58:17 +08:00
|
|
|
Stringpool_eq> String_set_type;
|
2006-09-27 05:50:25 +08:00
|
|
|
#endif
|
|
|
|
|
2007-09-20 01:38:00 +08:00
|
|
|
// Comparison routine used when sorting into a string table. We
|
2007-09-18 13:16:39 +08:00
|
|
|
// store string-sizes in the sort-vector so we don't have to
|
|
|
|
// recompute them log(n) times as we sort.
|
|
|
|
struct Stringpool_sort_info
|
|
|
|
{
|
|
|
|
typename String_set_type::iterator it;
|
|
|
|
size_t string_length;
|
|
|
|
Stringpool_sort_info(typename String_set_type::iterator i, size_t s)
|
|
|
|
: it(i), string_length(s)
|
|
|
|
{ }
|
|
|
|
};
|
2006-09-30 03:58:17 +08:00
|
|
|
|
|
|
|
struct Stringpool_sort_comparison
|
|
|
|
{
|
|
|
|
bool
|
2007-09-18 13:16:39 +08:00
|
|
|
operator()(const Stringpool_sort_info&, const Stringpool_sort_info&) const;
|
2006-09-30 03:58:17 +08:00
|
|
|
};
|
|
|
|
|
2006-11-07 12:40:46 +08:00
|
|
|
// List of Stringdata structures.
|
|
|
|
typedef std::list<Stringdata*> Stringdata_list;
|
|
|
|
|
|
|
|
// Mapping from const char* to namepool entry.
|
2006-08-19 06:29:20 +08:00
|
|
|
String_set_type string_set_;
|
2006-11-07 12:40:46 +08:00
|
|
|
// List of buffers.
|
|
|
|
Stringdata_list strings_;
|
2007-09-20 01:38:00 +08:00
|
|
|
// Size of string table.
|
2006-09-30 03:58:17 +08:00
|
|
|
off_t strtab_size_;
|
2006-11-07 12:40:46 +08:00
|
|
|
// Next Stringdata index.
|
|
|
|
unsigned int next_index_;
|
2007-05-17 01:42:48 +08:00
|
|
|
// Whether to reserve offset 0 to hold the null string.
|
|
|
|
bool zero_null_;
|
2006-08-19 06:29:20 +08:00
|
|
|
};
|
|
|
|
|
2007-05-17 01:42:48 +08:00
|
|
|
// The most common type of Stringpool.
|
|
|
|
typedef Stringpool_template<char> Stringpool;
|
|
|
|
|
2006-08-19 06:29:20 +08:00
|
|
|
} // End namespace gold.
|
|
|
|
|
|
|
|
#endif // !defined(GOLD_STRINGPOOL_H)
|