1 // merge.cc -- handle section merging for 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.
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
35 Output_merge_base::add_mapping(Relobj
* object
, unsigned int shndx
,
36 off_t offset
, off_t output_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
54 Output_merge_base::do_output_address(const Relobj
* object
, unsigned int shndx
,
56 uint64_t output_section_address
,
57 uint64_t* poutput
) const
59 gold_assert(output_section_address
== this->address());
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
69 if (p
->first
.object
!= object
70 || p
->first
.shndx
!= shndx
71 || p
->first
.offset
!= offset
)
73 if (p
== this->merge_map_
.begin())
78 if (p
->first
.object
!= object
|| p
->first
.shndx
!= shndx
)
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
);
87 // Compute the hash code for a fixed-size constant.
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;
108 size_t result
= 2166136261UL;
109 for (uint64_t i
= 0; i
< entsize
; ++i
)
111 result
^= (size_t) *p
++;
112 result
*= 16777619UL;
118 // Return whether one hash table key equals another.
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.
132 Output_merge_data::add_constant(const unsigned char* p
)
134 uint64_t entsize
= this->entsize();
135 if (this->len_
+ entsize
> this->alc_
)
138 this->alc_
= 128 * entsize
;
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
156 Output_merge_data::do_add_input_section(Relobj
* object
, unsigned int shndx
)
159 const unsigned char* p
= object
->section_contents(shndx
, &len
);
161 uint64_t entsize
= this->entsize();
163 if (len
% entsize
!= 0)
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
);
178 // Key was already present. Remove the copy we just added.
179 this->len_
-= entsize
;
183 // Record the offset of this constant in the output section.
184 this->add_mapping(object
, shndx
, i
, k
);
190 // Set the final data size in a merged output section with fixed size
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
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
>
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
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
>
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
>
241 Output_merge_string
<Char_type
>::do_add_input_section(Relobj
* object
,
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)
252 _("%s: %s: mergeable string section length not multiple of "
254 program_name
, object
->name().c_str());
257 len
/= sizeof(Char_type
);
263 for (const Char_type
* pl
= p
; *pl
!= 0; ++pl
)
269 _("%s: %s: entry in mergeable string section "
270 "not null terminated\n"),
271 program_name
, object
->name().c_str());
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
;
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
294 template<typename Char_type
>
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();
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());
310 this->hashtable_
.clear();
313 // Write out a merged string section.
315 template<typename Char_type
>
317 Output_merge_string
<Char_type
>::do_write(Output_file
* of
)
319 this->stringpool_
.write(of
, this->offset());
322 // Instantiate the templates we need.
325 class Output_merge_string
<char>;
328 class Output_merge_string
<uint16_t>;
331 class Output_merge_string
<uint32_t>;
333 } // End namespace gold.