gas/testsuite/
[binutils.git] / gold / merge.cc
blob0db62ef7c822f67cc682f7d46e1634bf46b9449f
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.
23 #include "gold.h"
25 #include <cstdlib>
27 #include "merge.h"
29 namespace gold
32 // Sort the entries in a merge mapping. The key is an input object, a
33 // section index in that object, and an offset in that section.
35 bool
36 Output_merge_base::Merge_key_less::operator()(const Merge_key& mk1,
37 const Merge_key& mk2) const
39 // The order of different objects and different sections doesn't
40 // matter. We want to get consistent results across links so we
41 // don't use pointer comparison.
42 if (mk1.object != mk2.object)
43 return mk1.object->name() < mk2.object->name();
44 if (mk1.shndx != mk2.shndx)
45 return mk1.shndx < mk2.shndx;
46 return mk1.offset < mk2.offset;
49 // Add a mapping from an OFFSET in input section SHNDX in object
50 // OBJECT to an OUTPUT_OFFSET in a merged output section. This
51 // manages the mapping used to resolve relocations against merged
52 // sections.
54 void
55 Output_merge_base::add_mapping(Relobj* object, unsigned int shndx,
56 off_t offset, off_t output_offset)
58 Merge_key mk;
59 mk.object = object;
60 mk.shndx = shndx;
61 mk.offset = offset;
62 std::pair<Merge_map::iterator, bool> ins =
63 this->merge_map_.insert(std::make_pair(mk, output_offset));
64 gold_assert(ins.second);
67 // Return the output address for an input address. The input address
68 // is at offset OFFSET in section SHNDX in OBJECT.
69 // OUTPUT_SECTION_ADDRESS is the address of the output section. If we
70 // know the address, set *POUTPUT and return true. Otherwise return
71 // false.
73 bool
74 Output_merge_base::do_output_address(const Relobj* object, unsigned int shndx,
75 off_t offset,
76 uint64_t output_section_address,
77 uint64_t* poutput) const
79 gold_assert(output_section_address == this->address());
81 Merge_key mk;
82 mk.object = object;
83 mk.shndx = shndx;
84 mk.offset = offset;
85 Merge_map::const_iterator p = this->merge_map_.lower_bound(mk);
87 // If MK is not in the map, lower_bound returns the next iterator
88 // larger than it.
89 if (p == this->merge_map_.end()
90 || p->first.object != object
91 || p->first.shndx != shndx
92 || p->first.offset != offset)
94 if (p == this->merge_map_.begin())
95 return false;
96 --p;
99 if (p->first.object != object || p->first.shndx != shndx)
100 return false;
102 // Any input section is fully mapped: we don't need to know the size
103 // of the range starting at P->FIRST.OFFSET.
104 *poutput = output_section_address + p->second + (offset - p->first.offset);
105 return true;
108 // Compute the hash code for a fixed-size constant.
110 size_t
111 Output_merge_data::Merge_data_hash::operator()(Merge_data_key k) const
113 const unsigned char* p = this->pomd_->constant(k);
114 uint64_t entsize = this->pomd_->entsize();
116 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
117 if (sizeof(size_t) == 8)
119 size_t result = static_cast<size_t>(14695981039346656037ULL);
120 for (uint64_t i = 0; i < entsize; ++i)
122 result &= (size_t) *p++;
123 result *= 1099511628211ULL;
125 return result;
127 else
129 size_t result = 2166136261UL;
130 for (uint64_t i = 0; i < entsize; ++i)
132 result ^= (size_t) *p++;
133 result *= 16777619UL;
135 return result;
139 // Return whether one hash table key equals another.
141 bool
142 Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1,
143 Merge_data_key k2) const
145 const unsigned char* p1 = this->pomd_->constant(k1);
146 const unsigned char* p2 = this->pomd_->constant(k2);
147 return memcmp(p1, p2, this->pomd_->entsize()) == 0;
150 // Add a constant to the end of the section contents.
152 void
153 Output_merge_data::add_constant(const unsigned char* p)
155 uint64_t entsize = this->entsize();
156 if (this->len_ + entsize > this->alc_)
158 if (this->alc_ == 0)
159 this->alc_ = 128 * entsize;
160 else
161 this->alc_ *= 2;
162 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_));
163 if (this->p_ == NULL)
164 gold_fatal("out of memory", true);
167 memcpy(this->p_ + this->len_, p, entsize);
168 this->len_ += entsize;
171 // Add the input section SHNDX in OBJECT to a merged output section
172 // which holds fixed length constants. Return whether we were able to
173 // handle the section; if not, it will be linked as usual without
174 // constant merging.
176 bool
177 Output_merge_data::do_add_input_section(Relobj* object, unsigned int shndx)
179 off_t len;
180 const unsigned char* p = object->section_contents(shndx, &len, false);
182 uint64_t entsize = this->entsize();
184 if (len % entsize != 0)
185 return false;
187 for (off_t i = 0; i < len; i += entsize, p += entsize)
189 // Add the constant to the section contents. If we find that it
190 // is already in the hash table, we will remove it again.
191 Merge_data_key k = this->len_;
192 this->add_constant(p);
194 std::pair<Merge_data_hashtable::iterator, bool> ins =
195 this->hashtable_.insert(k);
197 if (!ins.second)
199 // Key was already present. Remove the copy we just added.
200 this->len_ -= entsize;
201 k = *ins.first;
204 // Record the offset of this constant in the output section.
205 this->add_mapping(object, shndx, i, k);
208 return true;
211 // Set the final data size in a merged output section with fixed size
212 // constants.
214 void
215 Output_merge_data::do_set_address(uint64_t, off_t)
217 // Release the memory we don't need.
218 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->len_));
219 gold_assert(this->p_ != NULL);
220 this->set_data_size(this->len_);
223 // Write the data of a merged output section with fixed size constants
224 // to the file.
226 void
227 Output_merge_data::do_write(Output_file* of)
229 of->write(this->offset(), this->p_, this->len_);
232 // Add an input section to a merged string section.
234 template<typename Char_type>
235 bool
236 Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
237 unsigned int shndx)
239 off_t len;
240 const unsigned char* pdata = object->section_contents(shndx, &len, false);
242 const Char_type* p = reinterpret_cast<const Char_type*>(pdata);
244 if (len % sizeof(Char_type) != 0)
246 fprintf(stderr,
247 _("%s: %s: mergeable string section length not multiple of "
248 "character size\n"),
249 program_name, object->name().c_str());
250 gold_exit(false);
252 len /= sizeof(Char_type);
254 off_t i = 0;
255 while (i < len)
257 off_t plen = 0;
258 for (const Char_type* pl = p; *pl != 0; ++pl)
260 ++plen;
261 if (i + plen >= len)
263 fprintf(stderr,
264 _("%s: %s: entry in mergeable string section "
265 "not null terminated\n"),
266 program_name, object->name().c_str());
267 gold_exit(false);
271 const Char_type* str = this->stringpool_.add(p, NULL);
273 this->merged_strings_.push_back(Merged_string(object, shndx, i, str));
275 p += plen + 1;
276 i += plen + 1;
279 return true;
282 // Set the final data size of a merged string section. This is where
283 // we finalize the mappings from the input sections to the output
284 // section.
286 template<typename Char_type>
287 void
288 Output_merge_string<Char_type>::do_set_address(uint64_t, off_t)
290 this->stringpool_.set_string_offsets();
292 for (typename Merged_strings::const_iterator p =
293 this->merged_strings_.begin();
294 p != this->merged_strings_.end();
295 ++p)
296 this->add_mapping(p->object, p->shndx, p->offset,
297 this->stringpool_.get_offset(p->string));
299 this->set_data_size(this->stringpool_.get_strtab_size());
301 // Save some memory.
302 this->merged_strings_.clear();
305 // Write out a merged string section.
307 template<typename Char_type>
308 void
309 Output_merge_string<Char_type>::do_write(Output_file* of)
311 this->stringpool_.write(of, this->offset());
314 // Instantiate the templates we need.
316 template
317 class Output_merge_string<char>;
319 template
320 class Output_merge_string<uint16_t>;
322 template
323 class Output_merge_string<uint32_t>;
325 } // End namespace gold.