1 // merge.cc -- handle section merging for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
33 // For each object with merge sections, we store an Object_merge_map.
34 // This is used to map locations in input sections to a merged output
35 // section. The output section itself is not recorded here--it can be
36 // found in the map_to_output_ field of the Object.
38 class Object_merge_map
42 : first_shnum_(-1U), first_map_(),
43 second_shnum_(-1U), second_map_(),
49 // Add a mapping for MERGE_MAP, for the bytes from OFFSET to OFFSET
50 // + LENGTH in the input section SHNDX to OUTPUT_OFFSET in the
51 // output section. An OUTPUT_OFFSET of -1 means that the bytes are
54 add_mapping(const Merge_map*, unsigned int shndx, off_t offset, off_t length,
57 // Get the output offset for an input address in MERGE_MAP. The
58 // input address is at offset OFFSET in section SHNDX. This sets
59 // *OUTPUT_OFFSET to the offset in the output section; this will be
60 // -1 if the bytes are not being copied to the output. This returns
61 // true if the mapping is known, false otherwise.
63 get_output_offset(const Merge_map*, unsigned int shndx, off_t offset,
64 off_t *output_offset);
67 // Map input section offsets to a length and an output section
68 // offset. An output section offset of -1 means that this part of
69 // the input section is being discarded.
70 struct Input_merge_entry
72 // The offset in the input section.
76 // The offset in the output section.
80 // A less-than comparison routine for Input_merge_entry.
81 struct Input_merge_compare
84 operator()(const Input_merge_entry& i1, const Input_merge_entry& i2) const
85 { return i1.input_offset < i2.input_offset; }
88 // A list of entries for a particular section.
89 struct Input_merge_map
91 // The Merge_map for this section.
92 const Merge_map* merge_map;
93 // The list of mappings.
94 std::vector<Input_merge_entry> entries;
95 // Whether the ENTRIES field is sorted by input_offset.
99 : merge_map(NULL), entries(), sorted(true)
103 // Map input section indices to merge maps.
104 typedef std::map<unsigned int, Input_merge_map*> Section_merge_maps;
106 // Return a pointer to the Input_merge_map to use for the input
107 // section SHNDX, or NULL.
109 get_input_merge_map(unsigned int shndx);
111 // Get or make the the Input_merge_map to use for the section SHNDX
114 get_or_make_input_merge_map(const Merge_map* merge_map, unsigned int shndx);
116 // Any given object file will normally only have a couple of input
117 // sections with mergeable contents. So we keep the first two input
118 // section numbers inline, and push any further ones into a map. A
119 // value of -1U in first_shnum_ or second_shnum_ means that we don't
120 // have a corresponding entry.
121 unsigned int first_shnum_;
122 Input_merge_map first_map_;
123 unsigned int second_shnum_;
124 Input_merge_map second_map_;
125 Section_merge_maps section_merge_maps_;
130 Object_merge_map::~Object_merge_map()
132 for (Section_merge_maps::iterator p = this->section_merge_maps_.begin();
133 p != this->section_merge_maps_.end();
138 // Get the Input_merge_map to use for an input section, or NULL.
140 Object_merge_map::Input_merge_map*
141 Object_merge_map::get_input_merge_map(unsigned int shndx)
143 gold_assert(shndx != -1U);
144 if (shndx == this->first_shnum_)
145 return &this->first_map_;
146 if (shndx == this->second_shnum_)
147 return &this->second_map_;
148 Section_merge_maps::const_iterator p = this->section_merge_maps_.find(shndx);
149 if (p != this->section_merge_maps_.end())
154 // Get or create the Input_merge_map to use for an input section.
156 Object_merge_map::Input_merge_map*
157 Object_merge_map::get_or_make_input_merge_map(const Merge_map* merge_map,
160 Input_merge_map* map = this->get_input_merge_map(shndx);
163 // For a given input section in a given object, every mapping
164 // must be donw with the same Merge_map.
165 gold_assert(map->merge_map == merge_map);
169 // We need to create a new entry.
170 if (this->first_shnum_ == -1U)
172 this->first_shnum_ = shndx;
173 this->first_map_.merge_map = merge_map;
174 return &this->first_map_;
176 if (this->second_shnum_ == -1U)
178 this->second_shnum_ = shndx;
179 this->second_map_.merge_map = merge_map;
180 return &this->second_map_;
183 Input_merge_map* new_map = new Input_merge_map;
184 new_map->merge_map = merge_map;
185 this->section_merge_maps_[shndx] = new_map;
192 Object_merge_map::add_mapping(const Merge_map* merge_map, unsigned int shndx,
193 off_t input_offset, off_t length,
196 Input_merge_map* map = this->get_or_make_input_merge_map(merge_map, shndx);
198 // Try to merge the new entry in the last one we saw.
199 if (!map->entries.empty())
201 Input_merge_entry& entry(map->entries.back());
203 // If this entry is not in order, we need to sort the vector
204 // before looking anything up.
205 if (input_offset < entry.input_offset + entry.length)
207 gold_assert(input_offset < entry.input_offset
208 && input_offset + length <= entry.input_offset);
211 else if (entry.input_offset + entry.length == input_offset
212 && (output_offset == -1
213 ? entry.output_offset == -1
214 : entry.output_offset + entry.length == output_offset))
216 entry.length += length;
221 Input_merge_entry entry;
222 entry.input_offset = input_offset;
223 entry.length = length;
224 entry.output_offset = output_offset;
225 map->entries.push_back(entry);
228 // Get the output offset for an input address.
231 Object_merge_map::get_output_offset(const Merge_map* merge_map,
232 unsigned int shndx, off_t input_offset,
233 off_t *output_offset)
235 Input_merge_map* map = this->get_input_merge_map(shndx);
236 if (map == NULL || map->merge_map != merge_map)
241 std::sort(map->entries.begin(), map->entries.end(),
242 Input_merge_compare());
246 Input_merge_entry entry;
247 entry.input_offset = input_offset;
248 std::vector<Input_merge_entry>::const_iterator p =
249 std::lower_bound(map->entries.begin(), map->entries.end(),
250 entry, Input_merge_compare());
251 if (p == map->entries.end() || p->input_offset > input_offset)
253 if (p == map->entries.begin())
256 gold_assert(p->input_offset <= input_offset);
259 if (input_offset - p->input_offset >= p->length)
262 *output_offset = p->output_offset;
263 if (*output_offset != -1)
264 *output_offset += (input_offset - p->input_offset);
270 // Add a mapping for the bytes from OFFSET to OFFSET + LENGTH in input
271 // section SHNDX in object OBJECT to an OUTPUT_OFFSET in a merged
275 Merge_map::add_mapping(Relobj* object, unsigned int shndx,
276 off_t offset, off_t length, off_t output_offset)
278 Object_merge_map* object_merge_map = object->merge_map();
279 if (object_merge_map == NULL)
281 object_merge_map = new Object_merge_map();
282 object->set_merge_map(object_merge_map);
285 object_merge_map->add_mapping(this, shndx, offset, length, output_offset);
288 // Return the output offset for an input address. The input address
289 // is at offset OFFSET in section SHNDX in OBJECT. This sets
290 // *OUTPUT_OFFSET to the offset in the output section. This returns
291 // true if the mapping is known, false otherwise.
294 Merge_map::get_output_offset(const Relobj* object, unsigned int shndx,
295 off_t offset, off_t* output_offset) const
297 Object_merge_map* object_merge_map = object->merge_map();
298 if (object_merge_map == NULL)
300 return object_merge_map->get_output_offset(this, shndx, offset,
304 // Class Output_merge_base.
306 // Return the output offset for an input offset. The input address is
307 // at offset OFFSET in section SHNDX in OBJECT. If we know the
308 // offset, set *POUTPUT and return true. Otherwise return false.
311 Output_merge_base::do_output_offset(const Relobj* object,
314 off_t* poutput) const
316 return this->merge_map_.get_output_offset(object, shndx, offset, poutput);
319 // Class Output_merge_data.
321 // Compute the hash code for a fixed-size constant.
324 Output_merge_data::Merge_data_hash::operator()(Merge_data_key k) const
326 const unsigned char* p = this->pomd_->constant(k);
327 uint64_t entsize = this->pomd_->entsize();
329 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
330 if (sizeof(size_t) == 8)
332 size_t result = static_cast<size_t>(14695981039346656037ULL);
333 for (uint64_t i = 0; i < entsize; ++i)
335 result &= (size_t) *p++;
336 result *= 1099511628211ULL;
342 size_t result = 2166136261UL;
343 for (uint64_t i = 0; i < entsize; ++i)
345 result ^= (size_t) *p++;
346 result *= 16777619UL;
352 // Return whether one hash table key equals another.
355 Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1,
356 Merge_data_key k2) const
358 const unsigned char* p1 = this->pomd_->constant(k1);
359 const unsigned char* p2 = this->pomd_->constant(k2);
360 return memcmp(p1, p2, this->pomd_->entsize()) == 0;
363 // Add a constant to the end of the section contents.
366 Output_merge_data::add_constant(const unsigned char* p)
368 uint64_t entsize = this->entsize();
369 uint64_t addsize = std::max(entsize, this->addralign());
370 if (this->len_ + addsize > this->alc_)
373 this->alc_ = 128 * addsize;
376 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_));
377 if (this->p_ == NULL)
381 memcpy(this->p_ + this->len_, p, entsize);
382 if (addsize > entsize)
383 memset(this->p_ + this->len_ + entsize, 0, addsize - entsize);
384 this->len_ += addsize;
387 // Add the input section SHNDX in OBJECT to a merged output section
388 // which holds fixed length constants. Return whether we were able to
389 // handle the section; if not, it will be linked as usual without
393 Output_merge_data::do_add_input_section(Relobj* object, unsigned int shndx)
396 const unsigned char* p = object->section_contents(shndx, &len, false);
398 uint64_t entsize = this->entsize();
400 if (len % entsize != 0)
403 for (off_t i = 0; i < len; i += entsize, p += entsize)
405 // Add the constant to the section contents. If we find that it
406 // is already in the hash table, we will remove it again.
407 Merge_data_key k = this->len_;
408 this->add_constant(p);
410 std::pair<Merge_data_hashtable::iterator, bool> ins =
411 this->hashtable_.insert(k);
415 // Key was already present. Remove the copy we just added.
416 this->len_ -= entsize;
420 // Record the offset of this constant in the output section.
421 this->add_mapping(object, shndx, i, entsize, k);
427 // Set the final data size in a merged output section with fixed size
431 Output_merge_data::do_set_address(uint64_t, off_t)
433 // Release the memory we don't need.
434 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->len_));
435 gold_assert(this->p_ != NULL);
436 this->set_data_size(this->len_);
439 // Write the data of a merged output section with fixed size constants
443 Output_merge_data::do_write(Output_file* of)
445 of->write(this->offset(), this->p_, this->len_);
448 // Class Output_merge_string.
450 // Add an input section to a merged string section.
452 template<typename Char_type>
454 Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
458 const unsigned char* pdata = object->section_contents(shndx, &len, false);
460 const Char_type* p = reinterpret_cast<const Char_type*>(pdata);
462 if (len % sizeof(Char_type) != 0)
464 object->error(_("mergeable string section length not multiple of "
469 // The index I is in bytes, not characters.
474 for (const Char_type* pl = p; *pl != 0; ++pl)
476 // The length PLEN is in characters, not bytes.
478 if (i + plen * static_cast<off_t>(sizeof(Char_type)) >= len)
480 object->error(_("entry in mergeable string section "
481 "not null terminated"));
486 const Char_type* str = this->stringpool_.add(p, true, NULL);
488 off_t bytelen_with_null = (plen + 1) * sizeof(Char_type);
489 this->merged_strings_.push_back(Merged_string(object, shndx, i, str,
493 i += bytelen_with_null;
499 // Set the final data size of a merged string section. This is where
500 // we finalize the mappings from the input sections to the output
503 template<typename Char_type>
505 Output_merge_string<Char_type>::do_set_address(uint64_t, off_t)
507 this->stringpool_.set_string_offsets();
509 for (typename Merged_strings::const_iterator p =
510 this->merged_strings_.begin();
511 p != this->merged_strings_.end();
513 this->add_mapping(p->object, p->shndx, p->offset, p->length,
514 this->stringpool_.get_offset(p->string));
516 this->set_data_size(this->stringpool_.get_strtab_size());
519 this->merged_strings_.clear();
522 // Write out a merged string section.
524 template<typename Char_type>
526 Output_merge_string<Char_type>::do_write(Output_file* of)
528 this->stringpool_.write(of, this->offset());
531 // Instantiate the templates we need.
534 class Output_merge_string<char>;
537 class Output_merge_string<uint16_t>;
540 class Output_merge_string<uint32_t>;
542 } // End namespace gold.