1*a9fa9459Szrj // merge.cc -- handle section merging 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 <cstdlib>
26*a9fa9459Szrj #include <algorithm>
27*a9fa9459Szrj
28*a9fa9459Szrj #include "merge.h"
29*a9fa9459Szrj #include "compressed_output.h"
30*a9fa9459Szrj
31*a9fa9459Szrj namespace gold
32*a9fa9459Szrj {
33*a9fa9459Szrj
34*a9fa9459Szrj // Class Object_merge_map.
35*a9fa9459Szrj
36*a9fa9459Szrj // Destructor.
37*a9fa9459Szrj
~Object_merge_map()38*a9fa9459Szrj Object_merge_map::~Object_merge_map()
39*a9fa9459Szrj {
40*a9fa9459Szrj for (Section_merge_maps::iterator p = this->section_merge_maps_.begin();
41*a9fa9459Szrj p != this->section_merge_maps_.end();
42*a9fa9459Szrj ++p)
43*a9fa9459Szrj delete p->second;
44*a9fa9459Szrj }
45*a9fa9459Szrj
46*a9fa9459Szrj // Get the Input_merge_map to use for an input section, or NULL.
47*a9fa9459Szrj
48*a9fa9459Szrj const Object_merge_map::Input_merge_map*
get_input_merge_map(unsigned int shndx) const49*a9fa9459Szrj Object_merge_map::get_input_merge_map(unsigned int shndx) const
50*a9fa9459Szrj {
51*a9fa9459Szrj gold_assert(shndx != -1U);
52*a9fa9459Szrj const Section_merge_maps &maps = this->section_merge_maps_;
53*a9fa9459Szrj for (Section_merge_maps::const_iterator i = maps.begin(), e = maps.end();
54*a9fa9459Szrj i != e; ++i)
55*a9fa9459Szrj {
56*a9fa9459Szrj if (i->first == shndx)
57*a9fa9459Szrj return i->second;
58*a9fa9459Szrj }
59*a9fa9459Szrj return NULL;
60*a9fa9459Szrj }
61*a9fa9459Szrj
62*a9fa9459Szrj // Get or create the Input_merge_map to use for an input section.
63*a9fa9459Szrj
64*a9fa9459Szrj Object_merge_map::Input_merge_map*
get_or_make_input_merge_map(const Output_section_data * output_data,unsigned int shndx)65*a9fa9459Szrj Object_merge_map::get_or_make_input_merge_map(
66*a9fa9459Szrj const Output_section_data* output_data, unsigned int shndx) {
67*a9fa9459Szrj Input_merge_map* map = this->get_input_merge_map(shndx);
68*a9fa9459Szrj if (map != NULL)
69*a9fa9459Szrj {
70*a9fa9459Szrj // For a given input section in a given object, every mapping
71*a9fa9459Szrj // must be done with the same Merge_map.
72*a9fa9459Szrj gold_assert(map->output_data == output_data);
73*a9fa9459Szrj return map;
74*a9fa9459Szrj }
75*a9fa9459Szrj
76*a9fa9459Szrj Input_merge_map* new_map = new Input_merge_map;
77*a9fa9459Szrj new_map->output_data = output_data;
78*a9fa9459Szrj Section_merge_maps &maps = this->section_merge_maps_;
79*a9fa9459Szrj maps.push_back(std::make_pair(shndx, new_map));
80*a9fa9459Szrj return new_map;
81*a9fa9459Szrj }
82*a9fa9459Szrj
83*a9fa9459Szrj // Add a mapping.
84*a9fa9459Szrj
85*a9fa9459Szrj void
add_mapping(const Output_section_data * output_data,unsigned int shndx,section_offset_type input_offset,section_size_type length,section_offset_type output_offset)86*a9fa9459Szrj Object_merge_map::add_mapping(const Output_section_data* output_data,
87*a9fa9459Szrj unsigned int shndx,
88*a9fa9459Szrj section_offset_type input_offset,
89*a9fa9459Szrj section_size_type length,
90*a9fa9459Szrj section_offset_type output_offset)
91*a9fa9459Szrj {
92*a9fa9459Szrj Input_merge_map* map = this->get_or_make_input_merge_map(output_data, shndx);
93*a9fa9459Szrj map->add_mapping(input_offset, length, output_offset);
94*a9fa9459Szrj }
95*a9fa9459Szrj
96*a9fa9459Szrj void
add_mapping(section_offset_type input_offset,section_size_type length,section_offset_type output_offset)97*a9fa9459Szrj Object_merge_map::Input_merge_map::add_mapping(
98*a9fa9459Szrj section_offset_type input_offset, section_size_type length,
99*a9fa9459Szrj section_offset_type output_offset) {
100*a9fa9459Szrj // Try to merge the new entry in the last one we saw.
101*a9fa9459Szrj if (!this->entries.empty())
102*a9fa9459Szrj {
103*a9fa9459Szrj Input_merge_entry& entry(this->entries.back());
104*a9fa9459Szrj
105*a9fa9459Szrj // Use section_size_type to avoid signed/unsigned warnings.
106*a9fa9459Szrj section_size_type input_offset_u = input_offset;
107*a9fa9459Szrj section_size_type output_offset_u = output_offset;
108*a9fa9459Szrj
109*a9fa9459Szrj // If this entry is not in order, we need to sort the vector
110*a9fa9459Szrj // before looking anything up.
111*a9fa9459Szrj if (input_offset_u < entry.input_offset + entry.length)
112*a9fa9459Szrj {
113*a9fa9459Szrj gold_assert(input_offset < entry.input_offset);
114*a9fa9459Szrj gold_assert(input_offset_u + length
115*a9fa9459Szrj <= static_cast<section_size_type>(entry.input_offset));
116*a9fa9459Szrj this->sorted = false;
117*a9fa9459Szrj }
118*a9fa9459Szrj else if (entry.input_offset + entry.length == input_offset_u
119*a9fa9459Szrj && (output_offset == -1
120*a9fa9459Szrj ? entry.output_offset == -1
121*a9fa9459Szrj : entry.output_offset + entry.length == output_offset_u))
122*a9fa9459Szrj {
123*a9fa9459Szrj entry.length += length;
124*a9fa9459Szrj return;
125*a9fa9459Szrj }
126*a9fa9459Szrj }
127*a9fa9459Szrj
128*a9fa9459Szrj Input_merge_entry entry;
129*a9fa9459Szrj entry.input_offset = input_offset;
130*a9fa9459Szrj entry.length = length;
131*a9fa9459Szrj entry.output_offset = output_offset;
132*a9fa9459Szrj this->entries.push_back(entry);
133*a9fa9459Szrj }
134*a9fa9459Szrj
135*a9fa9459Szrj // Get the output offset for an input address.
136*a9fa9459Szrj
137*a9fa9459Szrj bool
get_output_offset(unsigned int shndx,section_offset_type input_offset,section_offset_type * output_offset)138*a9fa9459Szrj Object_merge_map::get_output_offset(unsigned int shndx,
139*a9fa9459Szrj section_offset_type input_offset,
140*a9fa9459Szrj section_offset_type* output_offset)
141*a9fa9459Szrj {
142*a9fa9459Szrj Input_merge_map* map = this->get_input_merge_map(shndx);
143*a9fa9459Szrj if (map == NULL)
144*a9fa9459Szrj return false;
145*a9fa9459Szrj
146*a9fa9459Szrj if (!map->sorted)
147*a9fa9459Szrj {
148*a9fa9459Szrj std::sort(map->entries.begin(), map->entries.end(),
149*a9fa9459Szrj Input_merge_compare());
150*a9fa9459Szrj map->sorted = true;
151*a9fa9459Szrj }
152*a9fa9459Szrj
153*a9fa9459Szrj Input_merge_entry entry;
154*a9fa9459Szrj entry.input_offset = input_offset;
155*a9fa9459Szrj std::vector<Input_merge_entry>::const_iterator p =
156*a9fa9459Szrj std::upper_bound(map->entries.begin(), map->entries.end(),
157*a9fa9459Szrj entry, Input_merge_compare());
158*a9fa9459Szrj if (p == map->entries.begin())
159*a9fa9459Szrj return false;
160*a9fa9459Szrj --p;
161*a9fa9459Szrj gold_assert(p->input_offset <= input_offset);
162*a9fa9459Szrj
163*a9fa9459Szrj if (input_offset - p->input_offset
164*a9fa9459Szrj >= static_cast<section_offset_type>(p->length))
165*a9fa9459Szrj return false;
166*a9fa9459Szrj
167*a9fa9459Szrj *output_offset = p->output_offset;
168*a9fa9459Szrj if (*output_offset != -1)
169*a9fa9459Szrj *output_offset += (input_offset - p->input_offset);
170*a9fa9459Szrj return true;
171*a9fa9459Szrj }
172*a9fa9459Szrj
173*a9fa9459Szrj // Return whether this is the merge map for section SHNDX.
174*a9fa9459Szrj
175*a9fa9459Szrj const Output_section_data*
find_merge_section(unsigned int shndx) const176*a9fa9459Szrj Object_merge_map::find_merge_section(unsigned int shndx) const {
177*a9fa9459Szrj const Object_merge_map::Input_merge_map* map =
178*a9fa9459Szrj this->get_input_merge_map(shndx);
179*a9fa9459Szrj if (map == NULL)
180*a9fa9459Szrj return NULL;
181*a9fa9459Szrj return map->output_data;
182*a9fa9459Szrj }
183*a9fa9459Szrj
184*a9fa9459Szrj // Initialize a mapping from input offsets to output addresses.
185*a9fa9459Szrj
186*a9fa9459Szrj template<int size>
187*a9fa9459Szrj void
initialize_input_to_output_map(unsigned int shndx,typename elfcpp::Elf_types<size>::Elf_Addr starting_address,Unordered_map<section_offset_type,typename elfcpp::Elf_types<size>::Elf_Addr> * initialize_map)188*a9fa9459Szrj Object_merge_map::initialize_input_to_output_map(
189*a9fa9459Szrj unsigned int shndx,
190*a9fa9459Szrj typename elfcpp::Elf_types<size>::Elf_Addr starting_address,
191*a9fa9459Szrj Unordered_map<section_offset_type,
192*a9fa9459Szrj typename elfcpp::Elf_types<size>::Elf_Addr>* initialize_map)
193*a9fa9459Szrj {
194*a9fa9459Szrj Input_merge_map* map = this->get_input_merge_map(shndx);
195*a9fa9459Szrj gold_assert(map != NULL);
196*a9fa9459Szrj
197*a9fa9459Szrj gold_assert(initialize_map->empty());
198*a9fa9459Szrj // We know how many entries we are going to add.
199*a9fa9459Szrj // reserve_unordered_map takes an expected count of buckets, not a
200*a9fa9459Szrj // count of elements, so double it to try to reduce collisions.
201*a9fa9459Szrj reserve_unordered_map(initialize_map, map->entries.size() * 2);
202*a9fa9459Szrj
203*a9fa9459Szrj for (Input_merge_map::Entries::const_iterator p = map->entries.begin();
204*a9fa9459Szrj p != map->entries.end();
205*a9fa9459Szrj ++p)
206*a9fa9459Szrj {
207*a9fa9459Szrj section_offset_type output_offset = p->output_offset;
208*a9fa9459Szrj if (output_offset != -1)
209*a9fa9459Szrj output_offset += starting_address;
210*a9fa9459Szrj else
211*a9fa9459Szrj {
212*a9fa9459Szrj // If we see a relocation against an address we have chosen
213*a9fa9459Szrj // to discard, we relocate to zero. FIXME: We could also
214*a9fa9459Szrj // issue a warning in this case; that would require
215*a9fa9459Szrj // reporting this somehow and checking it in the routines in
216*a9fa9459Szrj // reloc.h.
217*a9fa9459Szrj output_offset = 0;
218*a9fa9459Szrj }
219*a9fa9459Szrj initialize_map->insert(std::make_pair(p->input_offset, output_offset));
220*a9fa9459Szrj }
221*a9fa9459Szrj }
222*a9fa9459Szrj
223*a9fa9459Szrj // Class Output_merge_base.
224*a9fa9459Szrj
225*a9fa9459Szrj // Return the output offset for an input offset. The input address is
226*a9fa9459Szrj // at offset OFFSET in section SHNDX in OBJECT. If we know the
227*a9fa9459Szrj // offset, set *POUTPUT and return true. Otherwise return false.
228*a9fa9459Szrj
229*a9fa9459Szrj bool
do_output_offset(const Relobj * object,unsigned int shndx,section_offset_type offset,section_offset_type * poutput) const230*a9fa9459Szrj Output_merge_base::do_output_offset(const Relobj* object,
231*a9fa9459Szrj unsigned int shndx,
232*a9fa9459Szrj section_offset_type offset,
233*a9fa9459Szrj section_offset_type* poutput) const
234*a9fa9459Szrj {
235*a9fa9459Szrj return object->merge_output_offset(shndx, offset, poutput);
236*a9fa9459Szrj }
237*a9fa9459Szrj
238*a9fa9459Szrj // Record a merged input section for script processing.
239*a9fa9459Szrj
240*a9fa9459Szrj void
record_input_section(Relobj * relobj,unsigned int shndx)241*a9fa9459Szrj Output_merge_base::record_input_section(Relobj* relobj, unsigned int shndx)
242*a9fa9459Szrj {
243*a9fa9459Szrj gold_assert(this->keeps_input_sections_ && relobj != NULL);
244*a9fa9459Szrj // If this is the first input section, record it. We need do this because
245*a9fa9459Szrj // this->input_sections_ is unordered.
246*a9fa9459Szrj if (this->first_relobj_ == NULL)
247*a9fa9459Szrj {
248*a9fa9459Szrj this->first_relobj_ = relobj;
249*a9fa9459Szrj this->first_shndx_ = shndx;
250*a9fa9459Szrj }
251*a9fa9459Szrj
252*a9fa9459Szrj std::pair<Input_sections::iterator, bool> result =
253*a9fa9459Szrj this->input_sections_.insert(Section_id(relobj, shndx));
254*a9fa9459Szrj // We should insert a merge section once only.
255*a9fa9459Szrj gold_assert(result.second);
256*a9fa9459Szrj }
257*a9fa9459Szrj
258*a9fa9459Szrj // Class Output_merge_data.
259*a9fa9459Szrj
260*a9fa9459Szrj // Compute the hash code for a fixed-size constant.
261*a9fa9459Szrj
262*a9fa9459Szrj size_t
operator ()(Merge_data_key k) const263*a9fa9459Szrj Output_merge_data::Merge_data_hash::operator()(Merge_data_key k) const
264*a9fa9459Szrj {
265*a9fa9459Szrj const unsigned char* p = this->pomd_->constant(k);
266*a9fa9459Szrj section_size_type entsize =
267*a9fa9459Szrj convert_to_section_size_type(this->pomd_->entsize());
268*a9fa9459Szrj
269*a9fa9459Szrj // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
270*a9fa9459Szrj if (sizeof(size_t) == 8)
271*a9fa9459Szrj {
272*a9fa9459Szrj size_t result = static_cast<size_t>(14695981039346656037ULL);
273*a9fa9459Szrj for (section_size_type i = 0; i < entsize; ++i)
274*a9fa9459Szrj {
275*a9fa9459Szrj result &= (size_t) *p++;
276*a9fa9459Szrj result *= 1099511628211ULL;
277*a9fa9459Szrj }
278*a9fa9459Szrj return result;
279*a9fa9459Szrj }
280*a9fa9459Szrj else
281*a9fa9459Szrj {
282*a9fa9459Szrj size_t result = 2166136261UL;
283*a9fa9459Szrj for (section_size_type i = 0; i < entsize; ++i)
284*a9fa9459Szrj {
285*a9fa9459Szrj result ^= (size_t) *p++;
286*a9fa9459Szrj result *= 16777619UL;
287*a9fa9459Szrj }
288*a9fa9459Szrj return result;
289*a9fa9459Szrj }
290*a9fa9459Szrj }
291*a9fa9459Szrj
292*a9fa9459Szrj // Return whether one hash table key equals another.
293*a9fa9459Szrj
294*a9fa9459Szrj bool
operator ()(Merge_data_key k1,Merge_data_key k2) const295*a9fa9459Szrj Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1,
296*a9fa9459Szrj Merge_data_key k2) const
297*a9fa9459Szrj {
298*a9fa9459Szrj const unsigned char* p1 = this->pomd_->constant(k1);
299*a9fa9459Szrj const unsigned char* p2 = this->pomd_->constant(k2);
300*a9fa9459Szrj return memcmp(p1, p2, this->pomd_->entsize()) == 0;
301*a9fa9459Szrj }
302*a9fa9459Szrj
303*a9fa9459Szrj // Add a constant to the end of the section contents.
304*a9fa9459Szrj
305*a9fa9459Szrj void
add_constant(const unsigned char * p)306*a9fa9459Szrj Output_merge_data::add_constant(const unsigned char* p)
307*a9fa9459Szrj {
308*a9fa9459Szrj section_size_type entsize = convert_to_section_size_type(this->entsize());
309*a9fa9459Szrj section_size_type addralign =
310*a9fa9459Szrj convert_to_section_size_type(this->addralign());
311*a9fa9459Szrj section_size_type addsize = std::max(entsize, addralign);
312*a9fa9459Szrj if (this->len_ + addsize > this->alc_)
313*a9fa9459Szrj {
314*a9fa9459Szrj if (this->alc_ == 0)
315*a9fa9459Szrj this->alc_ = 128 * addsize;
316*a9fa9459Szrj else
317*a9fa9459Szrj this->alc_ *= 2;
318*a9fa9459Szrj this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_));
319*a9fa9459Szrj if (this->p_ == NULL)
320*a9fa9459Szrj gold_nomem();
321*a9fa9459Szrj }
322*a9fa9459Szrj
323*a9fa9459Szrj memcpy(this->p_ + this->len_, p, entsize);
324*a9fa9459Szrj if (addsize > entsize)
325*a9fa9459Szrj memset(this->p_ + this->len_ + entsize, 0, addsize - entsize);
326*a9fa9459Szrj this->len_ += addsize;
327*a9fa9459Szrj }
328*a9fa9459Szrj
329*a9fa9459Szrj // Add the input section SHNDX in OBJECT to a merged output section
330*a9fa9459Szrj // which holds fixed length constants. Return whether we were able to
331*a9fa9459Szrj // handle the section; if not, it will be linked as usual without
332*a9fa9459Szrj // constant merging.
333*a9fa9459Szrj
334*a9fa9459Szrj bool
do_add_input_section(Relobj * object,unsigned int shndx)335*a9fa9459Szrj Output_merge_data::do_add_input_section(Relobj* object, unsigned int shndx)
336*a9fa9459Szrj {
337*a9fa9459Szrj section_size_type len;
338*a9fa9459Szrj bool is_new;
339*a9fa9459Szrj const unsigned char* p = object->decompressed_section_contents(shndx, &len,
340*a9fa9459Szrj &is_new);
341*a9fa9459Szrj
342*a9fa9459Szrj section_size_type entsize = convert_to_section_size_type(this->entsize());
343*a9fa9459Szrj
344*a9fa9459Szrj if (len % entsize != 0)
345*a9fa9459Szrj {
346*a9fa9459Szrj if (is_new)
347*a9fa9459Szrj delete[] p;
348*a9fa9459Szrj return false;
349*a9fa9459Szrj }
350*a9fa9459Szrj
351*a9fa9459Szrj this->input_count_ += len / entsize;
352*a9fa9459Szrj
353*a9fa9459Szrj Object_merge_map* merge_map = object->get_or_create_merge_map();
354*a9fa9459Szrj Object_merge_map::Input_merge_map* input_merge_map =
355*a9fa9459Szrj merge_map->get_or_make_input_merge_map(this, shndx);
356*a9fa9459Szrj
357*a9fa9459Szrj for (section_size_type i = 0; i < len; i += entsize, p += entsize)
358*a9fa9459Szrj {
359*a9fa9459Szrj // Add the constant to the section contents. If we find that it
360*a9fa9459Szrj // is already in the hash table, we will remove it again.
361*a9fa9459Szrj Merge_data_key k = this->len_;
362*a9fa9459Szrj this->add_constant(p);
363*a9fa9459Szrj
364*a9fa9459Szrj std::pair<Merge_data_hashtable::iterator, bool> ins =
365*a9fa9459Szrj this->hashtable_.insert(k);
366*a9fa9459Szrj
367*a9fa9459Szrj if (!ins.second)
368*a9fa9459Szrj {
369*a9fa9459Szrj // Key was already present. Remove the copy we just added.
370*a9fa9459Szrj this->len_ -= entsize;
371*a9fa9459Szrj k = *ins.first;
372*a9fa9459Szrj }
373*a9fa9459Szrj
374*a9fa9459Szrj // Record the offset of this constant in the output section.
375*a9fa9459Szrj input_merge_map->add_mapping(i, entsize, k);
376*a9fa9459Szrj }
377*a9fa9459Szrj
378*a9fa9459Szrj // For script processing, we keep the input sections.
379*a9fa9459Szrj if (this->keeps_input_sections())
380*a9fa9459Szrj record_input_section(object, shndx);
381*a9fa9459Szrj
382*a9fa9459Szrj if (is_new)
383*a9fa9459Szrj delete[] p;
384*a9fa9459Szrj
385*a9fa9459Szrj return true;
386*a9fa9459Szrj }
387*a9fa9459Szrj
388*a9fa9459Szrj // Set the final data size in a merged output section with fixed size
389*a9fa9459Szrj // constants.
390*a9fa9459Szrj
391*a9fa9459Szrj void
set_final_data_size()392*a9fa9459Szrj Output_merge_data::set_final_data_size()
393*a9fa9459Szrj {
394*a9fa9459Szrj // Release the memory we don't need.
395*a9fa9459Szrj this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->len_));
396*a9fa9459Szrj // An Output_merge_data object may be empty and realloc is allowed
397*a9fa9459Szrj // to return a NULL pointer in this case. An Output_merge_data is empty
398*a9fa9459Szrj // if all its input sections have sizes that are not multiples of entsize.
399*a9fa9459Szrj gold_assert(this->p_ != NULL || this->len_ == 0);
400*a9fa9459Szrj this->set_data_size(this->len_);
401*a9fa9459Szrj }
402*a9fa9459Szrj
403*a9fa9459Szrj // Write the data of a merged output section with fixed size constants
404*a9fa9459Szrj // to the file.
405*a9fa9459Szrj
406*a9fa9459Szrj void
do_write(Output_file * of)407*a9fa9459Szrj Output_merge_data::do_write(Output_file* of)
408*a9fa9459Szrj {
409*a9fa9459Szrj of->write(this->offset(), this->p_, this->len_);
410*a9fa9459Szrj }
411*a9fa9459Szrj
412*a9fa9459Szrj // Write the data to a buffer.
413*a9fa9459Szrj
414*a9fa9459Szrj void
do_write_to_buffer(unsigned char * buffer)415*a9fa9459Szrj Output_merge_data::do_write_to_buffer(unsigned char* buffer)
416*a9fa9459Szrj {
417*a9fa9459Szrj memcpy(buffer, this->p_, this->len_);
418*a9fa9459Szrj }
419*a9fa9459Szrj
420*a9fa9459Szrj // Print merge stats to stderr.
421*a9fa9459Szrj
422*a9fa9459Szrj void
do_print_merge_stats(const char * section_name)423*a9fa9459Szrj Output_merge_data::do_print_merge_stats(const char* section_name)
424*a9fa9459Szrj {
425*a9fa9459Szrj fprintf(stderr,
426*a9fa9459Szrj _("%s: %s merged constants size: %lu; input: %zu; output: %zu\n"),
427*a9fa9459Szrj program_name, section_name,
428*a9fa9459Szrj static_cast<unsigned long>(this->entsize()),
429*a9fa9459Szrj this->input_count_, this->hashtable_.size());
430*a9fa9459Szrj }
431*a9fa9459Szrj
432*a9fa9459Szrj // Class Output_merge_string.
433*a9fa9459Szrj
434*a9fa9459Szrj // Add an input section to a merged string section.
435*a9fa9459Szrj
436*a9fa9459Szrj template<typename Char_type>
437*a9fa9459Szrj bool
do_add_input_section(Relobj * object,unsigned int shndx)438*a9fa9459Szrj Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
439*a9fa9459Szrj unsigned int shndx)
440*a9fa9459Szrj {
441*a9fa9459Szrj section_size_type sec_len;
442*a9fa9459Szrj bool is_new;
443*a9fa9459Szrj const unsigned char* pdata = object->decompressed_section_contents(shndx,
444*a9fa9459Szrj &sec_len,
445*a9fa9459Szrj &is_new);
446*a9fa9459Szrj
447*a9fa9459Szrj const Char_type* p = reinterpret_cast<const Char_type*>(pdata);
448*a9fa9459Szrj const Char_type* pend = p + sec_len / sizeof(Char_type);
449*a9fa9459Szrj const Char_type* pend0 = pend;
450*a9fa9459Szrj
451*a9fa9459Szrj if (sec_len % sizeof(Char_type) != 0)
452*a9fa9459Szrj {
453*a9fa9459Szrj object->error(_("mergeable string section length not multiple of "
454*a9fa9459Szrj "character size"));
455*a9fa9459Szrj if (is_new)
456*a9fa9459Szrj delete[] pdata;
457*a9fa9459Szrj return false;
458*a9fa9459Szrj }
459*a9fa9459Szrj
460*a9fa9459Szrj if (pend[-1] != 0)
461*a9fa9459Szrj {
462*a9fa9459Szrj gold_warning(_("%s: last entry in mergeable string section '%s' "
463*a9fa9459Szrj "not null terminated"),
464*a9fa9459Szrj object->name().c_str(),
465*a9fa9459Szrj object->section_name(shndx).c_str());
466*a9fa9459Szrj // Find the end of the last NULL-terminated string in the buffer.
467*a9fa9459Szrj while (pend0 > p && pend0[-1] != 0)
468*a9fa9459Szrj --pend0;
469*a9fa9459Szrj }
470*a9fa9459Szrj
471*a9fa9459Szrj Merged_strings_list* merged_strings_list =
472*a9fa9459Szrj new Merged_strings_list(object, shndx);
473*a9fa9459Szrj this->merged_strings_lists_.push_back(merged_strings_list);
474*a9fa9459Szrj Merged_strings& merged_strings = merged_strings_list->merged_strings;
475*a9fa9459Szrj
476*a9fa9459Szrj // Count the number of non-null strings in the section and size the list.
477*a9fa9459Szrj size_t count = 0;
478*a9fa9459Szrj const Char_type* pt = p;
479*a9fa9459Szrj while (pt < pend0)
480*a9fa9459Szrj {
481*a9fa9459Szrj size_t len = string_length(pt);
482*a9fa9459Szrj if (len != 0)
483*a9fa9459Szrj ++count;
484*a9fa9459Szrj pt += len + 1;
485*a9fa9459Szrj }
486*a9fa9459Szrj if (pend0 < pend)
487*a9fa9459Szrj ++count;
488*a9fa9459Szrj merged_strings.reserve(count + 1);
489*a9fa9459Szrj
490*a9fa9459Szrj // The index I is in bytes, not characters.
491*a9fa9459Szrj section_size_type i = 0;
492*a9fa9459Szrj
493*a9fa9459Szrj // We assume here that the beginning of the section is correctly
494*a9fa9459Szrj // aligned, so each string within the section must retain the same
495*a9fa9459Szrj // modulo.
496*a9fa9459Szrj uintptr_t init_align_modulo = (reinterpret_cast<uintptr_t>(pdata)
497*a9fa9459Szrj & (this->addralign() - 1));
498*a9fa9459Szrj bool has_misaligned_strings = false;
499*a9fa9459Szrj
500*a9fa9459Szrj while (p < pend)
501*a9fa9459Szrj {
502*a9fa9459Szrj size_t len = p < pend0 ? string_length(p) : pend - p;
503*a9fa9459Szrj
504*a9fa9459Szrj // Within merge input section each string must be aligned.
505*a9fa9459Szrj if (len != 0
506*a9fa9459Szrj && ((reinterpret_cast<uintptr_t>(p) & (this->addralign() - 1))
507*a9fa9459Szrj != init_align_modulo))
508*a9fa9459Szrj has_misaligned_strings = true;
509*a9fa9459Szrj
510*a9fa9459Szrj Stringpool::Key key;
511*a9fa9459Szrj this->stringpool_.add_with_length(p, len, true, &key);
512*a9fa9459Szrj
513*a9fa9459Szrj merged_strings.push_back(Merged_string(i, key));
514*a9fa9459Szrj p += len + 1;
515*a9fa9459Szrj i += (len + 1) * sizeof(Char_type);
516*a9fa9459Szrj }
517*a9fa9459Szrj
518*a9fa9459Szrj // Record the last offset in the input section so that we can
519*a9fa9459Szrj // compute the length of the last string.
520*a9fa9459Szrj merged_strings.push_back(Merged_string(i, 0));
521*a9fa9459Szrj
522*a9fa9459Szrj this->input_count_ += count;
523*a9fa9459Szrj this->input_size_ += i;
524*a9fa9459Szrj
525*a9fa9459Szrj if (has_misaligned_strings)
526*a9fa9459Szrj gold_warning(_("%s: section %s contains incorrectly aligned strings;"
527*a9fa9459Szrj " the alignment of those strings won't be preserved"),
528*a9fa9459Szrj object->name().c_str(),
529*a9fa9459Szrj object->section_name(shndx).c_str());
530*a9fa9459Szrj
531*a9fa9459Szrj // For script processing, we keep the input sections.
532*a9fa9459Szrj if (this->keeps_input_sections())
533*a9fa9459Szrj record_input_section(object, shndx);
534*a9fa9459Szrj
535*a9fa9459Szrj if (is_new)
536*a9fa9459Szrj delete[] pdata;
537*a9fa9459Szrj
538*a9fa9459Szrj return true;
539*a9fa9459Szrj }
540*a9fa9459Szrj
541*a9fa9459Szrj // Finalize the mappings from the input sections to the output
542*a9fa9459Szrj // section, and return the final data size.
543*a9fa9459Szrj
544*a9fa9459Szrj template<typename Char_type>
545*a9fa9459Szrj section_size_type
finalize_merged_data()546*a9fa9459Szrj Output_merge_string<Char_type>::finalize_merged_data()
547*a9fa9459Szrj {
548*a9fa9459Szrj this->stringpool_.set_string_offsets();
549*a9fa9459Szrj
550*a9fa9459Szrj for (typename Merged_strings_lists::const_iterator l =
551*a9fa9459Szrj this->merged_strings_lists_.begin();
552*a9fa9459Szrj l != this->merged_strings_lists_.end();
553*a9fa9459Szrj ++l)
554*a9fa9459Szrj {
555*a9fa9459Szrj section_offset_type last_input_offset = 0;
556*a9fa9459Szrj section_offset_type last_output_offset = 0;
557*a9fa9459Szrj Relobj *object = (*l)->object;
558*a9fa9459Szrj Object_merge_map* merge_map = object->get_or_create_merge_map();
559*a9fa9459Szrj Object_merge_map::Input_merge_map* input_merge_map =
560*a9fa9459Szrj merge_map->get_or_make_input_merge_map(this, (*l)->shndx);
561*a9fa9459Szrj
562*a9fa9459Szrj for (typename Merged_strings::const_iterator p =
563*a9fa9459Szrj (*l)->merged_strings.begin();
564*a9fa9459Szrj p != (*l)->merged_strings.end();
565*a9fa9459Szrj ++p)
566*a9fa9459Szrj {
567*a9fa9459Szrj section_size_type length = p->offset - last_input_offset;
568*a9fa9459Szrj if (length > 0)
569*a9fa9459Szrj input_merge_map->add_mapping(last_input_offset, length,
570*a9fa9459Szrj last_output_offset);
571*a9fa9459Szrj last_input_offset = p->offset;
572*a9fa9459Szrj if (p->stringpool_key != 0)
573*a9fa9459Szrj last_output_offset =
574*a9fa9459Szrj this->stringpool_.get_offset_from_key(p->stringpool_key);
575*a9fa9459Szrj }
576*a9fa9459Szrj delete *l;
577*a9fa9459Szrj }
578*a9fa9459Szrj
579*a9fa9459Szrj // Save some memory. This also ensures that this function will work
580*a9fa9459Szrj // if called twice, as may happen if Layout::set_segment_offsets
581*a9fa9459Szrj // finds a better alignment.
582*a9fa9459Szrj this->merged_strings_lists_.clear();
583*a9fa9459Szrj
584*a9fa9459Szrj return this->stringpool_.get_strtab_size();
585*a9fa9459Szrj }
586*a9fa9459Szrj
587*a9fa9459Szrj template<typename Char_type>
588*a9fa9459Szrj void
set_final_data_size()589*a9fa9459Szrj Output_merge_string<Char_type>::set_final_data_size()
590*a9fa9459Szrj {
591*a9fa9459Szrj const off_t final_data_size = this->finalize_merged_data();
592*a9fa9459Szrj this->set_data_size(final_data_size);
593*a9fa9459Szrj }
594*a9fa9459Szrj
595*a9fa9459Szrj // Write out a merged string section.
596*a9fa9459Szrj
597*a9fa9459Szrj template<typename Char_type>
598*a9fa9459Szrj void
do_write(Output_file * of)599*a9fa9459Szrj Output_merge_string<Char_type>::do_write(Output_file* of)
600*a9fa9459Szrj {
601*a9fa9459Szrj this->stringpool_.write(of, this->offset());
602*a9fa9459Szrj }
603*a9fa9459Szrj
604*a9fa9459Szrj // Write a merged string section to a buffer.
605*a9fa9459Szrj
606*a9fa9459Szrj template<typename Char_type>
607*a9fa9459Szrj void
do_write_to_buffer(unsigned char * buffer)608*a9fa9459Szrj Output_merge_string<Char_type>::do_write_to_buffer(unsigned char* buffer)
609*a9fa9459Szrj {
610*a9fa9459Szrj this->stringpool_.write_to_buffer(buffer, this->data_size());
611*a9fa9459Szrj }
612*a9fa9459Szrj
613*a9fa9459Szrj // Return the name of the types of string to use with
614*a9fa9459Szrj // do_print_merge_stats.
615*a9fa9459Szrj
616*a9fa9459Szrj template<typename Char_type>
617*a9fa9459Szrj const char*
string_name()618*a9fa9459Szrj Output_merge_string<Char_type>::string_name()
619*a9fa9459Szrj {
620*a9fa9459Szrj gold_unreachable();
621*a9fa9459Szrj return NULL;
622*a9fa9459Szrj }
623*a9fa9459Szrj
624*a9fa9459Szrj template<>
625*a9fa9459Szrj const char*
string_name()626*a9fa9459Szrj Output_merge_string<char>::string_name()
627*a9fa9459Szrj {
628*a9fa9459Szrj return "strings";
629*a9fa9459Szrj }
630*a9fa9459Szrj
631*a9fa9459Szrj template<>
632*a9fa9459Szrj const char*
string_name()633*a9fa9459Szrj Output_merge_string<uint16_t>::string_name()
634*a9fa9459Szrj {
635*a9fa9459Szrj return "16-bit strings";
636*a9fa9459Szrj }
637*a9fa9459Szrj
638*a9fa9459Szrj template<>
639*a9fa9459Szrj const char*
string_name()640*a9fa9459Szrj Output_merge_string<uint32_t>::string_name()
641*a9fa9459Szrj {
642*a9fa9459Szrj return "32-bit strings";
643*a9fa9459Szrj }
644*a9fa9459Szrj
645*a9fa9459Szrj // Print merge stats to stderr.
646*a9fa9459Szrj
647*a9fa9459Szrj template<typename Char_type>
648*a9fa9459Szrj void
do_print_merge_stats(const char * section_name)649*a9fa9459Szrj Output_merge_string<Char_type>::do_print_merge_stats(const char* section_name)
650*a9fa9459Szrj {
651*a9fa9459Szrj char buf[200];
652*a9fa9459Szrj snprintf(buf, sizeof buf, "%s merged %s", section_name, this->string_name());
653*a9fa9459Szrj fprintf(stderr, _("%s: %s input bytes: %zu\n"),
654*a9fa9459Szrj program_name, buf, this->input_size_);
655*a9fa9459Szrj fprintf(stderr, _("%s: %s input strings: %zu\n"),
656*a9fa9459Szrj program_name, buf, this->input_count_);
657*a9fa9459Szrj this->stringpool_.print_stats(buf);
658*a9fa9459Szrj }
659*a9fa9459Szrj
660*a9fa9459Szrj // Instantiate the templates we need.
661*a9fa9459Szrj
662*a9fa9459Szrj template
663*a9fa9459Szrj class Output_merge_string<char>;
664*a9fa9459Szrj
665*a9fa9459Szrj template
666*a9fa9459Szrj class Output_merge_string<uint16_t>;
667*a9fa9459Szrj
668*a9fa9459Szrj template
669*a9fa9459Szrj class Output_merge_string<uint32_t>;
670*a9fa9459Szrj
671*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
672*a9fa9459Szrj template
673*a9fa9459Szrj void
674*a9fa9459Szrj Object_merge_map::initialize_input_to_output_map<32>(
675*a9fa9459Szrj unsigned int shndx,
676*a9fa9459Szrj elfcpp::Elf_types<32>::Elf_Addr starting_address,
677*a9fa9459Szrj Unordered_map<section_offset_type, elfcpp::Elf_types<32>::Elf_Addr>*);
678*a9fa9459Szrj #endif
679*a9fa9459Szrj
680*a9fa9459Szrj #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
681*a9fa9459Szrj template
682*a9fa9459Szrj void
683*a9fa9459Szrj Object_merge_map::initialize_input_to_output_map<64>(
684*a9fa9459Szrj unsigned int shndx,
685*a9fa9459Szrj elfcpp::Elf_types<64>::Elf_Addr starting_address,
686*a9fa9459Szrj Unordered_map<section_offset_type, elfcpp::Elf_types<64>::Elf_Addr>*);
687*a9fa9459Szrj #endif
688*a9fa9459Szrj
689*a9fa9459Szrj } // End namespace gold.
690