7f08da6f94c82678fd7013289b4c371eadac0d45
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 // Add an input section to a merged string section.
213 template<typename Char_type
>
215 Output_merge_string
<Char_type
>::do_add_input_section(Relobj
* object
,
219 const unsigned char* pdata
= object
->section_contents(shndx
, &len
);
221 const Char_type
* p
= reinterpret_cast<const Char_type
*>(pdata
);
223 if (len
% sizeof(Char_type
) != 0)
226 _("%s: %s: mergeable string section length not multiple of "
228 program_name
, object
->name().c_str());
231 len
/= sizeof(Char_type
);
237 for (const Char_type
* pl
= p
; *pl
!= 0; ++pl
)
243 _("%s: %s: entry in mergeable string section "
244 "not null terminated\n"),
245 program_name
, object
->name().c_str());
250 const Char_type
* str
= this->stringpool_
.add(p
, NULL
);
252 this->merged_strings_
.push_back(Merged_string(object
, shndx
, i
, str
));
261 // Set the final data size of a merged string section. This is where
262 // we finalize the mappings from the input sections to the output
265 template<typename Char_type
>
267 Output_merge_string
<Char_type
>::do_set_address(uint64_t, off_t
)
269 this->stringpool_
.set_string_offsets();
271 for (typename
Merged_strings::const_iterator p
=
272 this->merged_strings_
.begin();
273 p
!= this->merged_strings_
.end();
275 this->add_mapping(p
->object
, p
->shndx
, p
->offset
,
276 this->stringpool_
.get_offset(p
->string
));
278 this->set_data_size(this->stringpool_
.get_strtab_size());
281 this->merged_strings_
.clear();
284 // Write out a merged string section.
286 template<typename Char_type
>
288 Output_merge_string
<Char_type
>::do_write(Output_file
* of
)
290 this->stringpool_
.write(of
, this->offset());
293 // Instantiate the templates we need.
296 class Output_merge_string
<char>;
299 class Output_merge_string
<uint16_t>;
302 class Output_merge_string
<uint32_t>;
304 } // End namespace gold.
This page took 0.036018 seconds and 4 git commands to generate.