1*a9fa9459Szrj // stringpool.cc -- a string pool for gold 2*a9fa9459Szrj 3*a9fa9459Szrj // Copyright (C) 2006-2016 Free Software Foundation, Inc. 4*a9fa9459Szrj // Written by Ian Lance Taylor <iant@google.com>. 5*a9fa9459Szrj 6*a9fa9459Szrj // This file is part of gold. 7*a9fa9459Szrj 8*a9fa9459Szrj // This program is free software; you can redistribute it and/or modify 9*a9fa9459Szrj // it under the terms of the GNU General Public License as published by 10*a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or 11*a9fa9459Szrj // (at your option) any later version. 12*a9fa9459Szrj 13*a9fa9459Szrj // This program is distributed in the hope that it will be useful, 14*a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 15*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*a9fa9459Szrj // GNU General Public License for more details. 17*a9fa9459Szrj 18*a9fa9459Szrj // You should have received a copy of the GNU General Public License 19*a9fa9459Szrj // along with this program; if not, write to the Free Software 20*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21*a9fa9459Szrj // MA 02110-1301, USA. 22*a9fa9459Szrj 23*a9fa9459Szrj #include "gold.h" 24*a9fa9459Szrj 25*a9fa9459Szrj #include <cstring> 26*a9fa9459Szrj #include <algorithm> 27*a9fa9459Szrj #include <vector> 28*a9fa9459Szrj 29*a9fa9459Szrj #include "output.h" 30*a9fa9459Szrj #include "parameters.h" 31*a9fa9459Szrj #include "stringpool.h" 32*a9fa9459Szrj 33*a9fa9459Szrj namespace gold 34*a9fa9459Szrj { 35*a9fa9459Szrj 36*a9fa9459Szrj template<typename Stringpool_char> 37*a9fa9459Szrj Stringpool_template<Stringpool_char>::Stringpool_template(uint64_t addralign) 38*a9fa9459Szrj : string_set_(), key_to_offset_(), strings_(), strtab_size_(0), 39*a9fa9459Szrj zero_null_(true), optimize_(false), offset_(sizeof(Stringpool_char)), 40*a9fa9459Szrj addralign_(addralign) 41*a9fa9459Szrj { 42*a9fa9459Szrj if (parameters->options_valid() 43*a9fa9459Szrj && parameters->options().optimize() >= 2 44*a9fa9459Szrj && addralign <= sizeof(Stringpool_char)) 45*a9fa9459Szrj this->optimize_ = true; 46*a9fa9459Szrj } 47*a9fa9459Szrj 48*a9fa9459Szrj template<typename Stringpool_char> 49*a9fa9459Szrj void 50*a9fa9459Szrj Stringpool_template<Stringpool_char>::clear() 51*a9fa9459Szrj { 52*a9fa9459Szrj for (typename std::list<Stringdata*>::iterator p = this->strings_.begin(); 53*a9fa9459Szrj p != this->strings_.end(); 54*a9fa9459Szrj ++p) 55*a9fa9459Szrj delete[] reinterpret_cast<char*>(*p); 56*a9fa9459Szrj this->strings_.clear(); 57*a9fa9459Szrj this->key_to_offset_.clear(); 58*a9fa9459Szrj this->string_set_.clear(); 59*a9fa9459Szrj } 60*a9fa9459Szrj 61*a9fa9459Szrj template<typename Stringpool_char> 62*a9fa9459Szrj Stringpool_template<Stringpool_char>::~Stringpool_template() 63*a9fa9459Szrj { 64*a9fa9459Szrj this->clear(); 65*a9fa9459Szrj } 66*a9fa9459Szrj 67*a9fa9459Szrj // Resize the internal hashtable with the expectation we'll get n new 68*a9fa9459Szrj // elements. Note that the hashtable constructor takes a "number of 69*a9fa9459Szrj // buckets you'd like," rather than "number of elements you'd like," 70*a9fa9459Szrj // but that's the best we can do. 71*a9fa9459Szrj 72*a9fa9459Szrj template<typename Stringpool_char> 73*a9fa9459Szrj void 74*a9fa9459Szrj Stringpool_template<Stringpool_char>::reserve(unsigned int n) 75*a9fa9459Szrj { 76*a9fa9459Szrj this->key_to_offset_.reserve(n); 77*a9fa9459Szrj 78*a9fa9459Szrj #if defined(HAVE_UNORDERED_MAP) 79*a9fa9459Szrj this->string_set_.rehash(this->string_set_.size() + n); 80*a9fa9459Szrj return; 81*a9fa9459Szrj #elif defined(HAVE_TR1_UNORDERED_MAP) 82*a9fa9459Szrj // rehash() implementation is broken in gcc 4.0.3's stl 83*a9fa9459Szrj //this->string_set_.rehash(this->string_set_.size() + n); 84*a9fa9459Szrj //return; 85*a9fa9459Szrj #elif defined(HAVE_EXT_HASH_MAP) 86*a9fa9459Szrj this->string_set_.resize(this->string_set_.size() + n); 87*a9fa9459Szrj return; 88*a9fa9459Szrj #endif 89*a9fa9459Szrj 90*a9fa9459Szrj // This is the generic "reserve" code, if no #ifdef above triggers. 91*a9fa9459Szrj String_set_type new_string_set(this->string_set_.size() + n); 92*a9fa9459Szrj new_string_set.insert(this->string_set_.begin(), this->string_set_.end()); 93*a9fa9459Szrj this->string_set_.swap(new_string_set); 94*a9fa9459Szrj } 95*a9fa9459Szrj 96*a9fa9459Szrj // Compare two strings of arbitrary character type for equality. 97*a9fa9459Szrj 98*a9fa9459Szrj template<typename Stringpool_char> 99*a9fa9459Szrj bool 100*a9fa9459Szrj Stringpool_template<Stringpool_char>::string_equal(const Stringpool_char* s1, 101*a9fa9459Szrj const Stringpool_char* s2) 102*a9fa9459Szrj { 103*a9fa9459Szrj while (*s1 != 0) 104*a9fa9459Szrj if (*s1++ != *s2++) 105*a9fa9459Szrj return false; 106*a9fa9459Szrj return *s2 == 0; 107*a9fa9459Szrj } 108*a9fa9459Szrj 109*a9fa9459Szrj // Specialize string_equal for char. 110*a9fa9459Szrj 111*a9fa9459Szrj template<> 112*a9fa9459Szrj inline bool 113*a9fa9459Szrj Stringpool_template<char>::string_equal(const char* s1, const char* s2) 114*a9fa9459Szrj { 115*a9fa9459Szrj return strcmp(s1, s2) == 0; 116*a9fa9459Szrj } 117*a9fa9459Szrj 118*a9fa9459Szrj // Equality comparison function for the hash table. 119*a9fa9459Szrj 120*a9fa9459Szrj template<typename Stringpool_char> 121*a9fa9459Szrj inline bool 122*a9fa9459Szrj Stringpool_template<Stringpool_char>::Stringpool_eq::operator()( 123*a9fa9459Szrj const Hashkey& h1, 124*a9fa9459Szrj const Hashkey& h2) const 125*a9fa9459Szrj { 126*a9fa9459Szrj return (h1.hash_code == h2.hash_code 127*a9fa9459Szrj && h1.length == h2.length 128*a9fa9459Szrj && (h1.string == h2.string 129*a9fa9459Szrj || memcmp(h1.string, h2.string, 130*a9fa9459Szrj h1.length * sizeof(Stringpool_char)) == 0)); 131*a9fa9459Szrj } 132*a9fa9459Szrj 133*a9fa9459Szrj // Hash function. The length is in characters, not bytes. 134*a9fa9459Szrj 135*a9fa9459Szrj template<typename Stringpool_char> 136*a9fa9459Szrj size_t 137*a9fa9459Szrj Stringpool_template<Stringpool_char>::string_hash(const Stringpool_char* s, 138*a9fa9459Szrj size_t length) 139*a9fa9459Szrj { 140*a9fa9459Szrj return gold::string_hash<Stringpool_char>(s, length); 141*a9fa9459Szrj } 142*a9fa9459Szrj 143*a9fa9459Szrj // Add the string S to the list of canonical strings. Return a 144*a9fa9459Szrj // pointer to the canonical string. If PKEY is not NULL, set *PKEY to 145*a9fa9459Szrj // the key. LENGTH is the length of S in characters. Note that S may 146*a9fa9459Szrj // not be NUL terminated. 147*a9fa9459Szrj 148*a9fa9459Szrj template<typename Stringpool_char> 149*a9fa9459Szrj const Stringpool_char* 150*a9fa9459Szrj Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s, 151*a9fa9459Szrj size_t len) 152*a9fa9459Szrj { 153*a9fa9459Szrj // We are in trouble if we've already computed the string offsets. 154*a9fa9459Szrj gold_assert(this->strtab_size_ == 0); 155*a9fa9459Szrj 156*a9fa9459Szrj // The size we allocate for a new Stringdata. 157*a9fa9459Szrj const size_t buffer_size = 1000; 158*a9fa9459Szrj // The amount we multiply the Stringdata index when calculating the 159*a9fa9459Szrj // key. 160*a9fa9459Szrj const size_t key_mult = 1024; 161*a9fa9459Szrj gold_assert(key_mult >= buffer_size); 162*a9fa9459Szrj 163*a9fa9459Szrj // Convert len to the number of bytes we need to allocate, including 164*a9fa9459Szrj // the null character. 165*a9fa9459Szrj len = (len + 1) * sizeof(Stringpool_char); 166*a9fa9459Szrj 167*a9fa9459Szrj size_t alc; 168*a9fa9459Szrj bool front = true; 169*a9fa9459Szrj if (len > buffer_size) 170*a9fa9459Szrj { 171*a9fa9459Szrj alc = sizeof(Stringdata) + len; 172*a9fa9459Szrj front = false; 173*a9fa9459Szrj } 174*a9fa9459Szrj else if (this->strings_.empty()) 175*a9fa9459Szrj alc = sizeof(Stringdata) + buffer_size; 176*a9fa9459Szrj else 177*a9fa9459Szrj { 178*a9fa9459Szrj Stringdata* psd = this->strings_.front(); 179*a9fa9459Szrj if (len > psd->alc - psd->len) 180*a9fa9459Szrj alc = sizeof(Stringdata) + buffer_size; 181*a9fa9459Szrj else 182*a9fa9459Szrj { 183*a9fa9459Szrj char* ret = psd->data + psd->len; 184*a9fa9459Szrj memcpy(ret, s, len - sizeof(Stringpool_char)); 185*a9fa9459Szrj memset(ret + len - sizeof(Stringpool_char), 0, 186*a9fa9459Szrj sizeof(Stringpool_char)); 187*a9fa9459Szrj 188*a9fa9459Szrj psd->len += len; 189*a9fa9459Szrj 190*a9fa9459Szrj return reinterpret_cast<const Stringpool_char*>(ret); 191*a9fa9459Szrj } 192*a9fa9459Szrj } 193*a9fa9459Szrj 194*a9fa9459Szrj Stringdata* psd = reinterpret_cast<Stringdata*>(new char[alc]); 195*a9fa9459Szrj psd->alc = alc - sizeof(Stringdata); 196*a9fa9459Szrj memcpy(psd->data, s, len - sizeof(Stringpool_char)); 197*a9fa9459Szrj memset(psd->data + len - sizeof(Stringpool_char), 0, 198*a9fa9459Szrj sizeof(Stringpool_char)); 199*a9fa9459Szrj psd->len = len; 200*a9fa9459Szrj 201*a9fa9459Szrj if (front) 202*a9fa9459Szrj this->strings_.push_front(psd); 203*a9fa9459Szrj else 204*a9fa9459Szrj this->strings_.push_back(psd); 205*a9fa9459Szrj 206*a9fa9459Szrj return reinterpret_cast<const Stringpool_char*>(psd->data); 207*a9fa9459Szrj } 208*a9fa9459Szrj 209*a9fa9459Szrj // Add a string to a string pool. 210*a9fa9459Szrj 211*a9fa9459Szrj template<typename Stringpool_char> 212*a9fa9459Szrj const Stringpool_char* 213*a9fa9459Szrj Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, bool copy, 214*a9fa9459Szrj Key* pkey) 215*a9fa9459Szrj { 216*a9fa9459Szrj return this->add_with_length(s, string_length(s), copy, pkey); 217*a9fa9459Szrj } 218*a9fa9459Szrj 219*a9fa9459Szrj // Add a new key offset entry. 220*a9fa9459Szrj 221*a9fa9459Szrj template<typename Stringpool_char> 222*a9fa9459Szrj void 223*a9fa9459Szrj Stringpool_template<Stringpool_char>::new_key_offset(size_t length) 224*a9fa9459Szrj { 225*a9fa9459Szrj section_offset_type offset; 226*a9fa9459Szrj if (this->zero_null_ && length == 0) 227*a9fa9459Szrj offset = 0; 228*a9fa9459Szrj else 229*a9fa9459Szrj { 230*a9fa9459Szrj offset = this->offset_; 231*a9fa9459Szrj // Align strings. 232*a9fa9459Szrj offset = align_address(offset, this->addralign_); 233*a9fa9459Szrj this->offset_ = offset + (length + 1) * sizeof(Stringpool_char); 234*a9fa9459Szrj } 235*a9fa9459Szrj this->key_to_offset_.push_back(offset); 236*a9fa9459Szrj } 237*a9fa9459Szrj 238*a9fa9459Szrj template<typename Stringpool_char> 239*a9fa9459Szrj const Stringpool_char* 240*a9fa9459Szrj Stringpool_template<Stringpool_char>::add_with_length(const Stringpool_char* s, 241*a9fa9459Szrj size_t length, 242*a9fa9459Szrj bool copy, 243*a9fa9459Szrj Key* pkey) 244*a9fa9459Szrj { 245*a9fa9459Szrj typedef std::pair<typename String_set_type::iterator, bool> Insert_type; 246*a9fa9459Szrj 247*a9fa9459Szrj // We add 1 so that 0 is always invalid. 248*a9fa9459Szrj const Key k = this->key_to_offset_.size() + 1; 249*a9fa9459Szrj 250*a9fa9459Szrj if (!copy) 251*a9fa9459Szrj { 252*a9fa9459Szrj // When we don't need to copy the string, we can call insert 253*a9fa9459Szrj // directly. 254*a9fa9459Szrj 255*a9fa9459Szrj std::pair<Hashkey, Hashval> element(Hashkey(s, length), k); 256*a9fa9459Szrj 257*a9fa9459Szrj Insert_type ins = this->string_set_.insert(element); 258*a9fa9459Szrj 259*a9fa9459Szrj typename String_set_type::const_iterator p = ins.first; 260*a9fa9459Szrj 261*a9fa9459Szrj if (ins.second) 262*a9fa9459Szrj { 263*a9fa9459Szrj // We just added the string. The key value has now been 264*a9fa9459Szrj // used. 265*a9fa9459Szrj this->new_key_offset(length); 266*a9fa9459Szrj } 267*a9fa9459Szrj else 268*a9fa9459Szrj { 269*a9fa9459Szrj gold_assert(k != p->second); 270*a9fa9459Szrj } 271*a9fa9459Szrj 272*a9fa9459Szrj if (pkey != NULL) 273*a9fa9459Szrj *pkey = p->second; 274*a9fa9459Szrj return p->first.string; 275*a9fa9459Szrj } 276*a9fa9459Szrj 277*a9fa9459Szrj // When we have to copy the string, we look it up twice in the hash 278*a9fa9459Szrj // table. The problem is that we can't insert S before we 279*a9fa9459Szrj // canonicalize it by copying it into the canonical list. The hash 280*a9fa9459Szrj // code will only be computed once. 281*a9fa9459Szrj 282*a9fa9459Szrj Hashkey hk(s, length); 283*a9fa9459Szrj typename String_set_type::const_iterator p = this->string_set_.find(hk); 284*a9fa9459Szrj if (p != this->string_set_.end()) 285*a9fa9459Szrj { 286*a9fa9459Szrj if (pkey != NULL) 287*a9fa9459Szrj *pkey = p->second; 288*a9fa9459Szrj return p->first.string; 289*a9fa9459Szrj } 290*a9fa9459Szrj 291*a9fa9459Szrj this->new_key_offset(length); 292*a9fa9459Szrj 293*a9fa9459Szrj hk.string = this->add_string(s, length); 294*a9fa9459Szrj // The contents of the string stay the same, so we don't need to 295*a9fa9459Szrj // adjust hk.hash_code or hk.length. 296*a9fa9459Szrj 297*a9fa9459Szrj std::pair<Hashkey, Hashval> element(hk, k); 298*a9fa9459Szrj 299*a9fa9459Szrj Insert_type ins = this->string_set_.insert(element); 300*a9fa9459Szrj gold_assert(ins.second); 301*a9fa9459Szrj 302*a9fa9459Szrj if (pkey != NULL) 303*a9fa9459Szrj *pkey = k; 304*a9fa9459Szrj return hk.string; 305*a9fa9459Szrj } 306*a9fa9459Szrj 307*a9fa9459Szrj template<typename Stringpool_char> 308*a9fa9459Szrj const Stringpool_char* 309*a9fa9459Szrj Stringpool_template<Stringpool_char>::find(const Stringpool_char* s, 310*a9fa9459Szrj Key* pkey) const 311*a9fa9459Szrj { 312*a9fa9459Szrj Hashkey hk(s); 313*a9fa9459Szrj typename String_set_type::const_iterator p = this->string_set_.find(hk); 314*a9fa9459Szrj if (p == this->string_set_.end()) 315*a9fa9459Szrj return NULL; 316*a9fa9459Szrj 317*a9fa9459Szrj if (pkey != NULL) 318*a9fa9459Szrj *pkey = p->second; 319*a9fa9459Szrj 320*a9fa9459Szrj return p->first.string; 321*a9fa9459Szrj } 322*a9fa9459Szrj 323*a9fa9459Szrj // Comparison routine used when sorting into an ELF strtab. We want 324*a9fa9459Szrj // to sort this so that when one string is a suffix of another, we 325*a9fa9459Szrj // always see the shorter string immediately after the longer string. 326*a9fa9459Szrj // For example, we want to see these strings in this order: 327*a9fa9459Szrj // abcd 328*a9fa9459Szrj // cd 329*a9fa9459Szrj // d 330*a9fa9459Szrj // When strings are not suffixes, we don't care what order they are 331*a9fa9459Szrj // in, but we need to ensure that suffixes wind up next to each other. 332*a9fa9459Szrj // So we do a reversed lexicographic sort on the reversed string. 333*a9fa9459Szrj 334*a9fa9459Szrj template<typename Stringpool_char> 335*a9fa9459Szrj bool 336*a9fa9459Szrj Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()( 337*a9fa9459Szrj const Stringpool_sort_info& sort_info1, 338*a9fa9459Szrj const Stringpool_sort_info& sort_info2) const 339*a9fa9459Szrj { 340*a9fa9459Szrj const Hashkey& h1(sort_info1->first); 341*a9fa9459Szrj const Hashkey& h2(sort_info2->first); 342*a9fa9459Szrj const Stringpool_char* s1 = h1.string; 343*a9fa9459Szrj const Stringpool_char* s2 = h2.string; 344*a9fa9459Szrj const size_t len1 = h1.length; 345*a9fa9459Szrj const size_t len2 = h2.length; 346*a9fa9459Szrj const size_t minlen = len1 < len2 ? len1 : len2; 347*a9fa9459Szrj const Stringpool_char* p1 = s1 + len1 - 1; 348*a9fa9459Szrj const Stringpool_char* p2 = s2 + len2 - 1; 349*a9fa9459Szrj for (size_t i = minlen; i > 0; --i, --p1, --p2) 350*a9fa9459Szrj { 351*a9fa9459Szrj if (*p1 != *p2) 352*a9fa9459Szrj return *p1 > *p2; 353*a9fa9459Szrj } 354*a9fa9459Szrj return len1 > len2; 355*a9fa9459Szrj } 356*a9fa9459Szrj 357*a9fa9459Szrj // Return whether s1 is a suffix of s2. 358*a9fa9459Szrj 359*a9fa9459Szrj template<typename Stringpool_char> 360*a9fa9459Szrj bool 361*a9fa9459Szrj Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1, 362*a9fa9459Szrj size_t len1, 363*a9fa9459Szrj const Stringpool_char* s2, 364*a9fa9459Szrj size_t len2) 365*a9fa9459Szrj { 366*a9fa9459Szrj if (len1 > len2) 367*a9fa9459Szrj return false; 368*a9fa9459Szrj return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0; 369*a9fa9459Szrj } 370*a9fa9459Szrj 371*a9fa9459Szrj // Turn the stringpool into an ELF strtab: determine the offsets of 372*a9fa9459Szrj // each string in the table. 373*a9fa9459Szrj 374*a9fa9459Szrj template<typename Stringpool_char> 375*a9fa9459Szrj void 376*a9fa9459Szrj Stringpool_template<Stringpool_char>::set_string_offsets() 377*a9fa9459Szrj { 378*a9fa9459Szrj if (this->strtab_size_ != 0) 379*a9fa9459Szrj { 380*a9fa9459Szrj // We've already computed the offsets. 381*a9fa9459Szrj return; 382*a9fa9459Szrj } 383*a9fa9459Szrj 384*a9fa9459Szrj const size_t charsize = sizeof(Stringpool_char); 385*a9fa9459Szrj 386*a9fa9459Szrj // Offset 0 may be reserved for the empty string. 387*a9fa9459Szrj section_offset_type offset = this->zero_null_ ? charsize : 0; 388*a9fa9459Szrj 389*a9fa9459Szrj // Sorting to find suffixes can take over 25% of the total CPU time 390*a9fa9459Szrj // used by the linker. Since it's merely an optimization to reduce 391*a9fa9459Szrj // the strtab size, and gives a relatively small benefit (it's 392*a9fa9459Szrj // typically rare for a symbol to be a suffix of another), we only 393*a9fa9459Szrj // take the time to sort when the user asks for heavy optimization. 394*a9fa9459Szrj if (!this->optimize_) 395*a9fa9459Szrj { 396*a9fa9459Szrj // If we are not optimizing, the offsets are already assigned. 397*a9fa9459Szrj offset = this->offset_; 398*a9fa9459Szrj } 399*a9fa9459Szrj else 400*a9fa9459Szrj { 401*a9fa9459Szrj size_t count = this->string_set_.size(); 402*a9fa9459Szrj 403*a9fa9459Szrj std::vector<Stringpool_sort_info> v; 404*a9fa9459Szrj v.reserve(count); 405*a9fa9459Szrj 406*a9fa9459Szrj for (typename String_set_type::iterator p = this->string_set_.begin(); 407*a9fa9459Szrj p != this->string_set_.end(); 408*a9fa9459Szrj ++p) 409*a9fa9459Szrj v.push_back(Stringpool_sort_info(p)); 410*a9fa9459Szrj 411*a9fa9459Szrj std::sort(v.begin(), v.end(), Stringpool_sort_comparison()); 412*a9fa9459Szrj 413*a9fa9459Szrj section_offset_type last_offset = -1; 414*a9fa9459Szrj for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(), 415*a9fa9459Szrj curr = v.begin(); 416*a9fa9459Szrj curr != v.end(); 417*a9fa9459Szrj last = curr++) 418*a9fa9459Szrj { 419*a9fa9459Szrj section_offset_type this_offset; 420*a9fa9459Szrj if (this->zero_null_ && (*curr)->first.string[0] == 0) 421*a9fa9459Szrj this_offset = 0; 422*a9fa9459Szrj else if (last != v.end() 423*a9fa9459Szrj && ((((*curr)->first.length - (*last)->first.length) 424*a9fa9459Szrj % this->addralign_) == 0) 425*a9fa9459Szrj && is_suffix((*curr)->first.string, 426*a9fa9459Szrj (*curr)->first.length, 427*a9fa9459Szrj (*last)->first.string, 428*a9fa9459Szrj (*last)->first.length)) 429*a9fa9459Szrj this_offset = (last_offset 430*a9fa9459Szrj + (((*last)->first.length - (*curr)->first.length) 431*a9fa9459Szrj * charsize)); 432*a9fa9459Szrj else 433*a9fa9459Szrj { 434*a9fa9459Szrj this_offset = align_address(offset, this->addralign_); 435*a9fa9459Szrj offset = this_offset + ((*curr)->first.length + 1) * charsize; 436*a9fa9459Szrj } 437*a9fa9459Szrj this->key_to_offset_[(*curr)->second - 1] = this_offset; 438*a9fa9459Szrj last_offset = this_offset; 439*a9fa9459Szrj } 440*a9fa9459Szrj } 441*a9fa9459Szrj 442*a9fa9459Szrj this->strtab_size_ = offset; 443*a9fa9459Szrj } 444*a9fa9459Szrj 445*a9fa9459Szrj // Get the offset of a string in the ELF strtab. The string must 446*a9fa9459Szrj // exist. 447*a9fa9459Szrj 448*a9fa9459Szrj template<typename Stringpool_char> 449*a9fa9459Szrj section_offset_type 450*a9fa9459Szrj Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s) 451*a9fa9459Szrj const 452*a9fa9459Szrj { 453*a9fa9459Szrj return this->get_offset_with_length(s, string_length(s)); 454*a9fa9459Szrj } 455*a9fa9459Szrj 456*a9fa9459Szrj template<typename Stringpool_char> 457*a9fa9459Szrj section_offset_type 458*a9fa9459Szrj Stringpool_template<Stringpool_char>::get_offset_with_length( 459*a9fa9459Szrj const Stringpool_char* s, 460*a9fa9459Szrj size_t length) const 461*a9fa9459Szrj { 462*a9fa9459Szrj gold_assert(this->strtab_size_ != 0); 463*a9fa9459Szrj Hashkey hk(s, length); 464*a9fa9459Szrj typename String_set_type::const_iterator p = this->string_set_.find(hk); 465*a9fa9459Szrj if (p != this->string_set_.end()) 466*a9fa9459Szrj return this->key_to_offset_[p->second - 1]; 467*a9fa9459Szrj gold_unreachable(); 468*a9fa9459Szrj } 469*a9fa9459Szrj 470*a9fa9459Szrj // Write the ELF strtab into the buffer. 471*a9fa9459Szrj 472*a9fa9459Szrj template<typename Stringpool_char> 473*a9fa9459Szrj void 474*a9fa9459Szrj Stringpool_template<Stringpool_char>::write_to_buffer( 475*a9fa9459Szrj unsigned char* buffer, 476*a9fa9459Szrj section_size_type bufsize) 477*a9fa9459Szrj { 478*a9fa9459Szrj gold_assert(this->strtab_size_ != 0); 479*a9fa9459Szrj gold_assert(bufsize >= this->strtab_size_); 480*a9fa9459Szrj if (this->zero_null_) 481*a9fa9459Szrj buffer[0] = '\0'; 482*a9fa9459Szrj for (typename String_set_type::const_iterator p = this->string_set_.begin(); 483*a9fa9459Szrj p != this->string_set_.end(); 484*a9fa9459Szrj ++p) 485*a9fa9459Szrj { 486*a9fa9459Szrj const int len = (p->first.length + 1) * sizeof(Stringpool_char); 487*a9fa9459Szrj const section_offset_type offset = this->key_to_offset_[p->second - 1]; 488*a9fa9459Szrj gold_assert(static_cast<section_size_type>(offset) + len 489*a9fa9459Szrj <= this->strtab_size_); 490*a9fa9459Szrj memcpy(buffer + offset, p->first.string, len); 491*a9fa9459Szrj } 492*a9fa9459Szrj } 493*a9fa9459Szrj 494*a9fa9459Szrj // Write the ELF strtab into the output file at the specified offset. 495*a9fa9459Szrj 496*a9fa9459Szrj template<typename Stringpool_char> 497*a9fa9459Szrj void 498*a9fa9459Szrj Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset) 499*a9fa9459Szrj { 500*a9fa9459Szrj gold_assert(this->strtab_size_ != 0); 501*a9fa9459Szrj unsigned char* view = of->get_output_view(offset, this->strtab_size_); 502*a9fa9459Szrj this->write_to_buffer(view, this->strtab_size_); 503*a9fa9459Szrj of->write_output_view(offset, this->strtab_size_, view); 504*a9fa9459Szrj } 505*a9fa9459Szrj 506*a9fa9459Szrj // Print statistical information to stderr. This is used for --stats. 507*a9fa9459Szrj 508*a9fa9459Szrj template<typename Stringpool_char> 509*a9fa9459Szrj void 510*a9fa9459Szrj Stringpool_template<Stringpool_char>::print_stats(const char* name) const 511*a9fa9459Szrj { 512*a9fa9459Szrj #if defined(HAVE_UNORDERED_MAP) || defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP) 513*a9fa9459Szrj fprintf(stderr, _("%s: %s entries: %zu; buckets: %zu\n"), 514*a9fa9459Szrj program_name, name, this->string_set_.size(), 515*a9fa9459Szrj this->string_set_.bucket_count()); 516*a9fa9459Szrj #else 517*a9fa9459Szrj fprintf(stderr, _("%s: %s entries: %zu\n"), 518*a9fa9459Szrj program_name, name, this->table_.size()); 519*a9fa9459Szrj #endif 520*a9fa9459Szrj fprintf(stderr, _("%s: %s Stringdata structures: %zu\n"), 521*a9fa9459Szrj program_name, name, this->strings_.size()); 522*a9fa9459Szrj } 523*a9fa9459Szrj 524*a9fa9459Szrj // Instantiate the templates we need. 525*a9fa9459Szrj 526*a9fa9459Szrj template 527*a9fa9459Szrj class Stringpool_template<char>; 528*a9fa9459Szrj 529*a9fa9459Szrj template 530*a9fa9459Szrj class Stringpool_template<uint16_t>; 531*a9fa9459Szrj 532*a9fa9459Szrj template 533*a9fa9459Szrj class Stringpool_template<uint32_t>; 534*a9fa9459Szrj 535*a9fa9459Szrj } // End namespace gold. 536