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 // Class Merge_map::Merge_key_less.
35 // Sort the entries in a merge mapping. The key is an input object, a
36 // section index in that object, and an offset in that section.
39 Merge_map::Merge_key_less::operator()(const Merge_key& mk1,
40 const Merge_key& mk2) const
42 // The order of different objects and different sections doesn't
43 // matter. We want to get consistent results across links so we
44 // don't use pointer comparison.
45 if (mk1.object != mk2.object)
47 // Two different object files can have the same name: if foo.a
48 // includes both bar/qux.o and baz/qux.o, then both end up with
49 // the name foo.a(qux.o). But it's impossible for two different
50 // object files to have both the same name and the same offset.
51 if (mk1.object->offset() != mk2.object->offset())
52 return mk1.object->offset() < mk2.object->offset();
53 return mk1.object->name() < mk2.object->name();
55 if (mk1.shndx != mk2.shndx)
56 return mk1.shndx < mk2.shndx;
57 return mk1.offset < mk2.offset;
62 // Add a mapping for the bytes from OFFSET to OFFSET + LENGTH in input
63 // section SHNDX in object OBJECT to an OUTPUT_OFFSET in a merged
67 Merge_map::add_mapping(Relobj* object, unsigned int shndx,
68 off_t offset, off_t length, off_t output_offset)
77 mv.output_offset = output_offset;
79 std::pair<Merge_mapping::iterator, bool> ins =
80 this->merge_map_.insert(std::make_pair(mk, mv));
81 gold_assert(ins.second);
84 // Return the output offset for an input address. The input address
85 // is at offset OFFSET in section SHNDX in OBJECT. This sets
86 // *OUTPUT_OFFSET to the offset in the output section. This returns
87 // true if the mapping is known, false otherwise.
90 Merge_map::get_output_offset(const Relobj* object, unsigned int shndx,
91 off_t offset, off_t* output_offset) const
97 Merge_mapping::const_iterator p = this->merge_map_.lower_bound(mk);
99 // If MK is not in the map, lower_bound returns the next iterator
101 if (p == this->merge_map_.end()
102 || p->first.object != object
103 || p->first.shndx != shndx
104 || p->first.offset != offset)
106 if (p == this->merge_map_.begin())
111 if (p->first.object != object || p->first.shndx != shndx)
114 if (offset - p->first.offset >= p->second.length)
117 *output_offset = p->second.output_offset;
118 if (*output_offset != -1)
119 *output_offset += (offset - p->first.offset);
123 // Class Output_merge_base.
125 // Return the output offset for an input offset. The input address is
126 // at offset OFFSET in section SHNDX in OBJECT. If we know the
127 // offset, set *POUTPUT and return true. Otherwise return false.
130 Output_merge_base::do_output_offset(const Relobj* object,
133 off_t* poutput) const
135 return this->merge_map_.get_output_offset(object, shndx, offset, poutput);
138 // Class Output_merge_data.
140 // Compute the hash code for a fixed-size constant.
143 Output_merge_data::Merge_data_hash::operator()(Merge_data_key k) const
145 const unsigned char* p = this->pomd_->constant(k);
146 uint64_t entsize = this->pomd_->entsize();
148 // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
149 if (sizeof(size_t) == 8)
151 size_t result = static_cast<size_t>(14695981039346656037ULL);
152 for (uint64_t i = 0; i < entsize; ++i)
154 result &= (size_t) *p++;
155 result *= 1099511628211ULL;
161 size_t result = 2166136261UL;
162 for (uint64_t i = 0; i < entsize; ++i)
164 result ^= (size_t) *p++;
165 result *= 16777619UL;
171 // Return whether one hash table key equals another.
174 Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1,
175 Merge_data_key k2) const
177 const unsigned char* p1 = this->pomd_->constant(k1);
178 const unsigned char* p2 = this->pomd_->constant(k2);
179 return memcmp(p1, p2, this->pomd_->entsize()) == 0;
182 // Add a constant to the end of the section contents.
185 Output_merge_data::add_constant(const unsigned char* p)
187 uint64_t entsize = this->entsize();
188 uint64_t addsize = std::max(entsize, this->addralign());
189 if (this->len_ + addsize > this->alc_)
192 this->alc_ = 128 * addsize;
195 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_));
196 if (this->p_ == NULL)
200 memcpy(this->p_ + this->len_, p, entsize);
201 if (addsize > entsize)
202 memset(this->p_ + this->len_ + entsize, 0, addsize - entsize);
203 this->len_ += addsize;
206 // Add the input section SHNDX in OBJECT to a merged output section
207 // which holds fixed length constants. Return whether we were able to
208 // handle the section; if not, it will be linked as usual without
212 Output_merge_data::do_add_input_section(Relobj* object, unsigned int shndx)
215 const unsigned char* p = object->section_contents(shndx, &len, false);
217 uint64_t entsize = this->entsize();
219 if (len % entsize != 0)
222 for (off_t i = 0; i < len; i += entsize, p += entsize)
224 // Add the constant to the section contents. If we find that it
225 // is already in the hash table, we will remove it again.
226 Merge_data_key k = this->len_;
227 this->add_constant(p);
229 std::pair<Merge_data_hashtable::iterator, bool> ins =
230 this->hashtable_.insert(k);
234 // Key was already present. Remove the copy we just added.
235 this->len_ -= entsize;
239 // Record the offset of this constant in the output section.
240 this->add_mapping(object, shndx, i, entsize, k);
246 // Set the final data size in a merged output section with fixed size
250 Output_merge_data::do_set_address(uint64_t, off_t)
252 // Release the memory we don't need.
253 this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->len_));
254 gold_assert(this->p_ != NULL);
255 this->set_data_size(this->len_);
258 // Write the data of a merged output section with fixed size constants
262 Output_merge_data::do_write(Output_file* of)
264 of->write(this->offset(), this->p_, this->len_);
267 // Class Output_merge_string.
269 // Add an input section to a merged string section.
271 template<typename Char_type>
273 Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
277 const unsigned char* pdata = object->section_contents(shndx, &len, false);
279 const Char_type* p = reinterpret_cast<const Char_type*>(pdata);
281 if (len % sizeof(Char_type) != 0)
283 object->error(_("mergeable string section length not multiple of "
288 // The index I is in bytes, not characters.
293 for (const Char_type* pl = p; *pl != 0; ++pl)
295 // The length PLEN is in characters, not bytes.
297 if (i + plen * static_cast<off_t>(sizeof(Char_type)) >= len)
299 object->error(_("entry in mergeable string section "
300 "not null terminated"));
305 const Char_type* str = this->stringpool_.add(p, true, NULL);
307 off_t bytelen_with_null = (plen + 1) * sizeof(Char_type);
308 this->merged_strings_.push_back(Merged_string(object, shndx, i, str,
312 i += bytelen_with_null;
318 // Set the final data size of a merged string section. This is where
319 // we finalize the mappings from the input sections to the output
322 template<typename Char_type>
324 Output_merge_string<Char_type>::do_set_address(uint64_t, off_t)
326 this->stringpool_.set_string_offsets();
328 for (typename Merged_strings::const_iterator p =
329 this->merged_strings_.begin();
330 p != this->merged_strings_.end();
332 this->add_mapping(p->object, p->shndx, p->offset, p->length,
333 this->stringpool_.get_offset(p->string));
335 this->set_data_size(this->stringpool_.get_strtab_size());
338 this->merged_strings_.clear();
341 // Write out a merged string section.
343 template<typename Char_type>
345 Output_merge_string<Char_type>::do_write(Output_file* of)
347 this->stringpool_.write(of, this->offset());
350 // Instantiate the templates we need.
353 class Output_merge_string<char>;
356 class Output_merge_string<uint16_t>;
359 class Output_merge_string<uint32_t>;
361 } // End namespace gold.