2007-08-01 Michael Snyder <msnyder@access-company.com>
[binutils.git] / gold / merge.cc
blob7af4faa03c92ea4937453caf0b36baa5d88f26e7
1 // merge.cc -- handle section merging for gold
3 #include "gold.h"
5 #include <cstdlib>
7 #include "merge.h"
9 namespace gold
12 // Sort the entries in a merge mapping. The key is an input object, a
13 // section index in that object, and an offset in that section.
15 bool
16 Output_merge_base::Merge_key_less::operator()(const Merge_key& mk1,
17 const Merge_key& mk2) const
19 // The order of different objects and different sections doesn't
20 // matter. We want to get consistent results across links so we
21 // don't use pointer comparison.
22 if (mk1.object != mk2.object)
23 return mk1.object->name() < mk2.object->name();
24 if (mk1.shndx != mk2.shndx)
25 return mk1.shndx < mk2.shndx;
26 return mk1.offset < mk2.offset;
29 // Add a mapping from an OFFSET in input section SHNDX in object
30 // OBJECT to an OUTPUT_OFFSET in a merged output section. This
31 // manages the mapping used to resolve relocations against merged
32 // sections.
34 void
35 Output_merge_base::add_mapping(Relobj* object, unsigned int shndx,
36 off_t offset, off_t output_offset)
38 Merge_key mk;
39 mk.object = object;
40 mk.shndx = shndx;
41 mk.offset = offset;
42 std::pair<Merge_map::iterator, bool> ins =
43 this->merge_map_.insert(std::make_pair(mk, output_offset));
44 gold_assert(ins.second);
47 // Return the output address for an input address. The input address
48 // is at offset OFFSET in section SHNDX in OBJECT.
49 // OUTPUT_SECTION_ADDRESS is the address of the output section. If we
50 // know the address, set *POUTPUT and return true. Otherwise return
51 // false.
53 bool
54 Output_merge_base::do_output_address(const Relobj* object, unsigned int shndx,
55 off_t offset,
56 uint64_t output_section_address,
57 uint64_t* poutput) const
59 gold_assert(output_section_address == this->address());
61 Merge_key mk;
62 mk.object = object;
63 mk.shndx = shndx;
64 mk.offset = offset;
65 Merge_map::const_iterator p = this->merge_map_.lower_bound(mk);
67 // If MK is not in the map, lower_bound returns the next iterator
68 // larger than it.
69 if (p->first.object != object
70 || p->first.shndx != shndx
71 || p->first.offset != offset)
73 if (p == this->merge_map_.begin())
74 return false;
75 --p;
78 if (p->first.object != object || p->first.shndx != shndx)
79 return false;
81 // Any input section is fully mapped: we don't need to know the size
82 // of the range starting at P->FIRST.OFFSET.
83 *poutput = output_section_address + p->second + (offset - p->first.offset);
84 return true;
87 // Compute the hash code for a fixed-size constant.
89 size_t
90 Output_merge_data::Merge_data_hash::operator()(Merge_data_key k) const
92 const unsigned char* p = this->pomd_->constant(k);
93 uint64_t entsize = this->pomd_->entsize();
95 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
96 if (sizeof(size_t) == 8)
98 size_t result = static_cast<size_t>(14695981039346656037ULL);
99 for (uint64_t i = 0; i < entsize; ++i)
101 result &= (size_t) *p++;
102 result *= 1099511628211ULL;
104 return result;
106 else
108 size_t result = 2166136261UL;
109 for (uint64_t i = 0; i < entsize; ++i)
111 result ^= (size_t) *p++;
112 result *= 16777619UL;
114 return result;
118 // Return whether one hash table key equals another.
120 bool
121 Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1,
122 Merge_data_key k2) const
124 const unsigned char* p1 = this->pomd_->constant(k1);
125 const unsigned char* p2 = this->pomd_->constant(k2);
126 return memcmp(p1, p2, this->pomd_->entsize()) == 0;
129 // Add a constant to the end of the section contents.
131 void
132 Output_merge_data::add_constant(const unsigned char* p)
134 uint64_t entsize = this->entsize();
135 if (this->len_ + entsize > this->alc_)
137 if (this->alc_ == 0)
138 this->alc_ = 128 * entsize;
139 else
140 this->alc_ *= 2;
141 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_));
142 if (this->p_ == NULL)
143 gold_fatal("out of memory", true);
146 memcpy(this->p_ + this->len_, p, entsize);
147 this->len_ += entsize;
150 // Add the input section SHNDX in OBJECT to a merged output section
151 // which holds fixed length constants. Return whether we were able to
152 // handle the section; if not, it will be linked as usual without
153 // constant merging.
155 bool
156 Output_merge_data::do_add_input_section(Relobj* object, unsigned int shndx)
158 off_t len;
159 const unsigned char* p = object->section_contents(shndx, &len);
161 uint64_t entsize = this->entsize();
163 if (len % entsize != 0)
164 return false;
166 for (off_t i = 0; i < len; i += entsize, p += entsize)
168 // Add the constant to the section contents. If we find that it
169 // is already in the hash table, we will remove it again.
170 Merge_data_key k = this->len_;
171 this->add_constant(p);
173 std::pair<Merge_data_hashtable::iterator, bool> ins =
174 this->hashtable_.insert(k);
176 if (!ins.second)
178 // Key was already present. Remove the copy we just added.
179 this->len_ -= entsize;
180 k = *ins.first;
183 // Record the offset of this constant in the output section.
184 this->add_mapping(object, shndx, i, k);
187 return true;
190 // Set the final data size in a merged output section with fixed size
191 // constants.
193 void
194 Output_merge_data::do_set_address(uint64_t, off_t)
196 // Release the memory we don't need.
197 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->len_));
198 gold_assert(this->p_ != NULL);
199 this->set_data_size(this->len_);
202 // Write the data of a merged output section with fixed size constants
203 // to the file.
205 void
206 Output_merge_data::do_write(Output_file* of)
208 of->write(this->offset(), this->p_, this->len_);
211 // Compute a hash code for a Merge_string_key, which is an object, a
212 // section index, and an offset.
214 template<typename Char_type>
215 size_t
216 Output_merge_string<Char_type>::Merge_string_key_hash::operator()(
217 const Merge_string_key& key) const
219 // This is a very simple minded hash code. Fix it if it we get too
220 // many collisions.
221 const std::string& oname(key.object->name());
222 return oname[0] + oname.length() + key.shndx + key.offset;
225 // Compare two Merge_string_keys for equality.
227 template<typename Char_type>
228 bool
229 Output_merge_string<Char_type>::Merge_string_key_eq::operator()(
230 const Merge_string_key& k1, const Merge_string_key& k2) const
232 return (k1.object == k2.object
233 && k1.shndx == k2.shndx
234 && k1.offset == k2.offset);
237 // Add an input section to a merged string section.
239 template<typename Char_type>
240 bool
241 Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
242 unsigned int shndx)
244 off_t len;
245 const unsigned char* pdata = object->section_contents(shndx, &len);
247 const Char_type* p = reinterpret_cast<const Char_type*>(pdata);
249 if (len % sizeof(Char_type) != 0)
251 fprintf(stderr,
252 _("%s: %s: mergeable string section length not multiple of "
253 "character size\n"),
254 program_name, object->name().c_str());
255 gold_exit(false);
257 len /= sizeof(Char_type);
259 off_t i = 0;
260 while (i < len)
262 off_t plen = 0;
263 for (const Char_type* pl = p; *pl != 0; ++pl)
265 ++plen;
266 if (i + plen >= len)
268 fprintf(stderr,
269 _("%s: %s: entry in mergeable string section "
270 "not null terminated\n"),
271 program_name, object->name().c_str());
272 gold_exit(false);
276 const Char_type* str = this->stringpool_.add(p, NULL);
278 Merge_string_key k(object, shndx, i);
279 typename Merge_string_hashtable::value_type v(k, str);
280 bool b = this->hashtable_.insert(v).second;
281 gold_assert(b);
283 p += plen + 1;
284 i += plen + 1;
287 return true;
290 // Set the final data size of a merged string section. This is where
291 // we finalize the mappings from the input sections to the output
292 // section.
294 template<typename Char_type>
295 void
296 Output_merge_string<Char_type>::do_set_address(uint64_t, off_t)
298 this->stringpool_.set_string_offsets();
300 for (typename Merge_string_hashtable::const_iterator p =
301 this->hashtable_.begin();
302 p != this->hashtable_.end();
303 ++p)
304 this->add_mapping(p->first.object, p->first.shndx, p->first.offset,
305 this->stringpool_.get_offset(p->second));
307 this->set_data_size(this->stringpool_.get_strtab_size());
309 // Save some memory.
310 this->hashtable_.clear();
313 // Write out a merged string section.
315 template<typename Char_type>
316 void
317 Output_merge_string<Char_type>::do_write(Output_file* of)
319 this->stringpool_.write(of, this->offset());
322 // Instantiate the templates we need.
324 template
325 class Output_merge_string<char>;
327 template
328 class Output_merge_string<uint16_t>;
330 template
331 class Output_merge_string<uint32_t>;
333 } // End namespace gold.