]>
Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // reloc.cc -- relocate input files for gold. |
2 | ||
b578bd7d | 3 | // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <[email protected]>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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. | |
22 | ||
61ba1cf9 ILT |
23 | #include "gold.h" |
24 | ||
cb295612 ILT |
25 | #include <algorithm> |
26 | ||
61ba1cf9 | 27 | #include "workqueue.h" |
09ec0418 | 28 | #include "layout.h" |
5a6f7e2d | 29 | #include "symtab.h" |
61ba1cf9 | 30 | #include "output.h" |
a9a60db6 ILT |
31 | #include "merge.h" |
32 | #include "object.h" | |
7019cd25 | 33 | #include "target-reloc.h" |
61ba1cf9 | 34 | #include "reloc.h" |
032ce4e9 | 35 | #include "icf.h" |
a2e47362 | 36 | #include "compressed_output.h" |
09ec0418 | 37 | #include "incremental.h" |
61ba1cf9 ILT |
38 | |
39 | namespace gold | |
40 | { | |
41 | ||
92e059d8 ILT |
42 | // Read_relocs methods. |
43 | ||
44 | // These tasks just read the relocation information from the file. | |
45 | // After reading it, the start another task to process the | |
46 | // information. These tasks requires access to the file. | |
47 | ||
17a1d0a9 ILT |
48 | Task_token* |
49 | Read_relocs::is_runnable() | |
92e059d8 | 50 | { |
17a1d0a9 | 51 | return this->object_->is_locked() ? this->object_->token() : NULL; |
92e059d8 ILT |
52 | } |
53 | ||
54 | // Lock the file. | |
55 | ||
17a1d0a9 ILT |
56 | void |
57 | Read_relocs::locks(Task_locker* tl) | |
92e059d8 | 58 | { |
cdc29364 CC |
59 | Task_token* token = this->object_->token(); |
60 | if (token != NULL) | |
61 | tl->add(this, token); | |
92e059d8 ILT |
62 | } |
63 | ||
64 | // Read the relocations and then start a Scan_relocs_task. | |
65 | ||
66 | void | |
67 | Read_relocs::run(Workqueue* workqueue) | |
68 | { | |
ca09d69a | 69 | Read_relocs_data* rd = new Read_relocs_data; |
92e059d8 | 70 | this->object_->read_relocs(rd); |
6d03d481 | 71 | this->object_->set_relocs_data(rd); |
17a1d0a9 ILT |
72 | this->object_->release(); |
73 | ||
ef15dade ST |
74 | // If garbage collection or identical comdat folding is desired, we |
75 | // process the relocs first before scanning them. Scanning of relocs is | |
76 | // done only after garbage or identical sections is identified. | |
032ce4e9 ST |
77 | if (parameters->options().gc_sections() |
78 | || parameters->options().icf_enabled()) | |
ef15dade | 79 | { |
ad0f2072 | 80 | workqueue->queue_next(new Gc_process_relocs(this->symtab_, |
6d03d481 ST |
81 | this->layout_, |
82 | this->object_, rd, | |
93ceb764 ILT |
83 | this->this_blocker_, |
84 | this->next_blocker_)); | |
6d03d481 ST |
85 | } |
86 | else | |
87 | { | |
ad0f2072 ILT |
88 | workqueue->queue_next(new Scan_relocs(this->symtab_, this->layout_, |
89 | this->object_, rd, | |
93ceb764 ILT |
90 | this->this_blocker_, |
91 | this->next_blocker_)); | |
6d03d481 | 92 | } |
92e059d8 ILT |
93 | } |
94 | ||
c7912668 ILT |
95 | // Return a debugging name for the task. |
96 | ||
97 | std::string | |
98 | Read_relocs::get_name() const | |
99 | { | |
100 | return "Read_relocs " + this->object_->name(); | |
101 | } | |
102 | ||
6d03d481 ST |
103 | // Gc_process_relocs methods. |
104 | ||
93ceb764 ILT |
105 | Gc_process_relocs::~Gc_process_relocs() |
106 | { | |
107 | if (this->this_blocker_ != NULL) | |
108 | delete this->this_blocker_; | |
109 | } | |
110 | ||
111 | // These tasks process the relocations read by Read_relocs and | |
6d03d481 | 112 | // determine which sections are referenced and which are garbage. |
93ceb764 ILT |
113 | // This task is done only when --gc-sections is used. This is blocked |
114 | // by THIS_BLOCKER_. It unblocks NEXT_BLOCKER_. | |
6d03d481 ST |
115 | |
116 | Task_token* | |
117 | Gc_process_relocs::is_runnable() | |
118 | { | |
93ceb764 ILT |
119 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) |
120 | return this->this_blocker_; | |
6d03d481 ST |
121 | if (this->object_->is_locked()) |
122 | return this->object_->token(); | |
123 | return NULL; | |
124 | } | |
125 | ||
126 | void | |
127 | Gc_process_relocs::locks(Task_locker* tl) | |
128 | { | |
129 | tl->add(this, this->object_->token()); | |
93ceb764 | 130 | tl->add(this, this->next_blocker_); |
6d03d481 ST |
131 | } |
132 | ||
133 | void | |
134 | Gc_process_relocs::run(Workqueue*) | |
135 | { | |
ad0f2072 | 136 | this->object_->gc_process_relocs(this->symtab_, this->layout_, this->rd_); |
6d03d481 ST |
137 | this->object_->release(); |
138 | } | |
139 | ||
140 | // Return a debugging name for the task. | |
141 | ||
142 | std::string | |
143 | Gc_process_relocs::get_name() const | |
144 | { | |
145 | return "Gc_process_relocs " + this->object_->name(); | |
146 | } | |
147 | ||
92e059d8 ILT |
148 | // Scan_relocs methods. |
149 | ||
93ceb764 ILT |
150 | Scan_relocs::~Scan_relocs() |
151 | { | |
152 | if (this->this_blocker_ != NULL) | |
153 | delete this->this_blocker_; | |
154 | } | |
155 | ||
92e059d8 ILT |
156 | // These tasks scan the relocations read by Read_relocs and mark up |
157 | // the symbol table to indicate which relocations are required. We | |
158 | // use a lock on the symbol table to keep them from interfering with | |
159 | // each other. | |
160 | ||
17a1d0a9 ILT |
161 | Task_token* |
162 | Scan_relocs::is_runnable() | |
92e059d8 | 163 | { |
93ceb764 ILT |
164 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) |
165 | return this->this_blocker_; | |
17a1d0a9 ILT |
166 | if (this->object_->is_locked()) |
167 | return this->object_->token(); | |
168 | return NULL; | |
92e059d8 ILT |
169 | } |
170 | ||
171 | // Return the locks we hold: one on the file, one on the symbol table | |
172 | // and one blocker. | |
173 | ||
17a1d0a9 ILT |
174 | void |
175 | Scan_relocs::locks(Task_locker* tl) | |
92e059d8 | 176 | { |
cdc29364 CC |
177 | Task_token* token = this->object_->token(); |
178 | if (token != NULL) | |
179 | tl->add(this, token); | |
93ceb764 | 180 | tl->add(this, this->next_blocker_); |
92e059d8 ILT |
181 | } |
182 | ||
183 | // Scan the relocs. | |
184 | ||
185 | void | |
186 | Scan_relocs::run(Workqueue*) | |
187 | { | |
ad0f2072 | 188 | this->object_->scan_relocs(this->symtab_, this->layout_, this->rd_); |
92e059d8 ILT |
189 | delete this->rd_; |
190 | this->rd_ = NULL; | |
a2a5469e | 191 | this->object_->release(); |
92e059d8 ILT |
192 | } |
193 | ||
c7912668 ILT |
194 | // Return a debugging name for the task. |
195 | ||
196 | std::string | |
197 | Scan_relocs::get_name() const | |
198 | { | |
199 | return "Scan_relocs " + this->object_->name(); | |
200 | } | |
201 | ||
61ba1cf9 ILT |
202 | // Relocate_task methods. |
203 | ||
730cdc88 | 204 | // We may have to wait for the output sections to be written. |
61ba1cf9 | 205 | |
17a1d0a9 ILT |
206 | Task_token* |
207 | Relocate_task::is_runnable() | |
61ba1cf9 | 208 | { |
730cdc88 ILT |
209 | if (this->object_->relocs_must_follow_section_writes() |
210 | && this->output_sections_blocker_->is_blocked()) | |
17a1d0a9 | 211 | return this->output_sections_blocker_; |
730cdc88 | 212 | |
c7912668 | 213 | if (this->object_->is_locked()) |
17a1d0a9 | 214 | return this->object_->token(); |
c7912668 | 215 | |
17a1d0a9 | 216 | return NULL; |
61ba1cf9 ILT |
217 | } |
218 | ||
219 | // We want to lock the file while we run. We want to unblock | |
730cdc88 | 220 | // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done. |
17a1d0a9 | 221 | // INPUT_SECTIONS_BLOCKER may be NULL. |
61ba1cf9 | 222 | |
17a1d0a9 ILT |
223 | void |
224 | Relocate_task::locks(Task_locker* tl) | |
61ba1cf9 | 225 | { |
17a1d0a9 ILT |
226 | if (this->input_sections_blocker_ != NULL) |
227 | tl->add(this, this->input_sections_blocker_); | |
228 | tl->add(this, this->final_blocker_); | |
cdc29364 CC |
229 | Task_token* token = this->object_->token(); |
230 | if (token != NULL) | |
231 | tl->add(this, token); | |
61ba1cf9 ILT |
232 | } |
233 | ||
234 | // Run the task. | |
235 | ||
236 | void | |
237 | Relocate_task::run(Workqueue*) | |
238 | { | |
ad0f2072 | 239 | this->object_->relocate(this->symtab_, this->layout_, this->of_); |
cb295612 ILT |
240 | |
241 | // This is normally the last thing we will do with an object, so | |
242 | // uncache all views. | |
243 | this->object_->clear_view_cache_marks(); | |
244 | ||
17a1d0a9 | 245 | this->object_->release(); |
61ba1cf9 ILT |
246 | } |
247 | ||
c7912668 ILT |
248 | // Return a debugging name for the task. |
249 | ||
250 | std::string | |
251 | Relocate_task::get_name() const | |
252 | { | |
253 | return "Relocate_task " + this->object_->name(); | |
254 | } | |
255 | ||
92e059d8 ILT |
256 | // Read the relocs and local symbols from the object file and store |
257 | // the information in RD. | |
258 | ||
259 | template<int size, bool big_endian> | |
260 | void | |
6fa2a40b | 261 | Sized_relobj_file<size, big_endian>::do_read_relocs(Read_relocs_data* rd) |
92e059d8 ILT |
262 | { |
263 | rd->relocs.clear(); | |
264 | ||
2ea97941 ILT |
265 | unsigned int shnum = this->shnum(); |
266 | if (shnum == 0) | |
92e059d8 ILT |
267 | return; |
268 | ||
2ea97941 | 269 | rd->relocs.reserve(shnum / 2); |
92e059d8 | 270 | |
ef9beddf | 271 | const Output_sections& out_sections(this->output_sections()); |
6fa2a40b | 272 | const std::vector<Address>& out_offsets(this->section_offsets()); |
730cdc88 | 273 | |
ca09d69a | 274 | const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(), |
2ea97941 | 275 | shnum * This::shdr_size, |
39d0cb0e | 276 | true, true); |
92e059d8 | 277 | // Skip the first, dummy, section. |
ca09d69a | 278 | const unsigned char* ps = pshdrs + This::shdr_size; |
2ea97941 | 279 | for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size) |
92e059d8 ILT |
280 | { |
281 | typename This::Shdr shdr(ps); | |
282 | ||
283 | unsigned int sh_type = shdr.get_sh_type(); | |
284 | if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) | |
285 | continue; | |
286 | ||
d491d34e | 287 | unsigned int shndx = this->adjust_shndx(shdr.get_sh_info()); |
2ea97941 | 288 | if (shndx >= shnum) |
92e059d8 | 289 | { |
75f2446e ILT |
290 | this->error(_("relocation section %u has bad info %u"), |
291 | i, shndx); | |
292 | continue; | |
92e059d8 ILT |
293 | } |
294 | ||
ef9beddf | 295 | Output_section* os = out_sections[shndx]; |
730cdc88 | 296 | if (os == NULL) |
92e059d8 ILT |
297 | continue; |
298 | ||
ead1e424 ILT |
299 | // We are scanning relocations in order to fill out the GOT and |
300 | // PLT sections. Relocations for sections which are not | |
301 | // allocated (typically debugging sections) should not add new | |
6a74a719 | 302 | // GOT and PLT entries. So we skip them unless this is a |
031cdbed ILT |
303 | // relocatable link or we need to emit relocations. FIXME: What |
304 | // should we do if a linker script maps a section with SHF_ALLOC | |
305 | // clear to a section with SHF_ALLOC set? | |
7019cd25 ILT |
306 | typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size); |
307 | bool is_section_allocated = ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) | |
308 | != 0); | |
309 | if (!is_section_allocated | |
8851ecca | 310 | && !parameters->options().relocatable() |
09ec0418 | 311 | && !parameters->options().emit_relocs() |
8c21d9d3 | 312 | && !parameters->incremental()) |
7019cd25 | 313 | continue; |
ead1e424 | 314 | |
d491d34e | 315 | if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_) |
92e059d8 | 316 | { |
75f2446e ILT |
317 | this->error(_("relocation section %u uses unexpected " |
318 | "symbol table %u"), | |
d491d34e | 319 | i, this->adjust_shndx(shdr.get_sh_link())); |
75f2446e | 320 | continue; |
92e059d8 ILT |
321 | } |
322 | ||
323 | off_t sh_size = shdr.get_sh_size(); | |
324 | ||
8dbe1edc ILT |
325 | if (sh_size == 0) |
326 | continue; | |
327 | ||
92e059d8 ILT |
328 | unsigned int reloc_size; |
329 | if (sh_type == elfcpp::SHT_REL) | |
330 | reloc_size = elfcpp::Elf_sizes<size>::rel_size; | |
331 | else | |
332 | reloc_size = elfcpp::Elf_sizes<size>::rela_size; | |
333 | if (reloc_size != shdr.get_sh_entsize()) | |
334 | { | |
75f2446e ILT |
335 | this->error(_("unexpected entsize for reloc section %u: %lu != %u"), |
336 | i, static_cast<unsigned long>(shdr.get_sh_entsize()), | |
337 | reloc_size); | |
338 | continue; | |
92e059d8 ILT |
339 | } |
340 | ||
341 | size_t reloc_count = sh_size / reloc_size; | |
f5c3f225 | 342 | if (static_cast<off_t>(reloc_count * reloc_size) != sh_size) |
92e059d8 | 343 | { |
75f2446e ILT |
344 | this->error(_("reloc section %u size %lu uneven"), |
345 | i, static_cast<unsigned long>(sh_size)); | |
346 | continue; | |
92e059d8 ILT |
347 | } |
348 | ||
349 | rd->relocs.push_back(Section_relocs()); | |
350 | Section_relocs& sr(rd->relocs.back()); | |
351 | sr.reloc_shndx = i; | |
352 | sr.data_shndx = shndx; | |
9eb9fa57 | 353 | sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size, |
39d0cb0e | 354 | true, true); |
92e059d8 ILT |
355 | sr.sh_type = sh_type; |
356 | sr.reloc_count = reloc_count; | |
730cdc88 | 357 | sr.output_section = os; |
a45248e0 | 358 | sr.needs_special_offset_handling = out_offsets[shndx] == invalid_address; |
7019cd25 | 359 | sr.is_data_section_allocated = is_section_allocated; |
92e059d8 ILT |
360 | } |
361 | ||
362 | // Read the local symbols. | |
a3ad94ed | 363 | gold_assert(this->symtab_shndx_ != -1U); |
645f8123 | 364 | if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0) |
92e059d8 ILT |
365 | rd->local_symbols = NULL; |
366 | else | |
367 | { | |
368 | typename This::Shdr symtabshdr(pshdrs | |
645f8123 | 369 | + this->symtab_shndx_ * This::shdr_size); |
a3ad94ed | 370 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
2ea97941 | 371 | const int sym_size = This::sym_size; |
92e059d8 | 372 | const unsigned int loccount = this->local_symbol_count_; |
a3ad94ed | 373 | gold_assert(loccount == symtabshdr.get_sh_info()); |
2ea97941 | 374 | off_t locsize = loccount * sym_size; |
92e059d8 | 375 | rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(), |
39d0cb0e | 376 | locsize, true, true); |
92e059d8 ILT |
377 | } |
378 | } | |
379 | ||
6d03d481 | 380 | // Process the relocs to generate mappings from source sections to referenced |
9b547ce6 | 381 | // sections. This is used during garbage collection to determine garbage |
6d03d481 ST |
382 | // sections. |
383 | ||
384 | template<int size, bool big_endian> | |
385 | void | |
6fa2a40b CC |
386 | Sized_relobj_file<size, big_endian>::do_gc_process_relocs(Symbol_table* symtab, |
387 | Layout* layout, | |
388 | Read_relocs_data* rd) | |
6d03d481 | 389 | { |
029ba973 ILT |
390 | Sized_target<size, big_endian>* target = |
391 | parameters->sized_target<size, big_endian>(); | |
6d03d481 ST |
392 | |
393 | const unsigned char* local_symbols; | |
394 | if (rd->local_symbols == NULL) | |
395 | local_symbols = NULL; | |
396 | else | |
397 | local_symbols = rd->local_symbols->data(); | |
398 | ||
399 | for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin(); | |
400 | p != rd->relocs.end(); | |
401 | ++p) | |
402 | { | |
403 | if (!parameters->options().relocatable()) | |
404 | { | |
405 | // As noted above, when not generating an object file, we | |
406 | // only scan allocated sections. We may see a non-allocated | |
407 | // section here if we are emitting relocs. | |
408 | if (p->is_data_section_allocated) | |
2ea97941 | 409 | target->gc_process_relocs(symtab, layout, this, |
6d03d481 ST |
410 | p->data_shndx, p->sh_type, |
411 | p->contents->data(), p->reloc_count, | |
412 | p->output_section, | |
413 | p->needs_special_offset_handling, | |
414 | this->local_symbol_count_, | |
415 | local_symbols); | |
416 | } | |
417 | } | |
418 | } | |
419 | ||
420 | ||
92e059d8 ILT |
421 | // Scan the relocs and adjust the symbol table. This looks for |
422 | // relocations which require GOT/PLT/COPY relocations. | |
423 | ||
424 | template<int size, bool big_endian> | |
425 | void | |
6fa2a40b | 426 | Sized_relobj_file<size, big_endian>::do_scan_relocs(Symbol_table* symtab, |
2ea97941 | 427 | Layout* layout, |
92e059d8 ILT |
428 | Read_relocs_data* rd) |
429 | { | |
029ba973 ILT |
430 | Sized_target<size, big_endian>* target = |
431 | parameters->sized_target<size, big_endian>(); | |
92e059d8 ILT |
432 | |
433 | const unsigned char* local_symbols; | |
434 | if (rd->local_symbols == NULL) | |
435 | local_symbols = NULL; | |
436 | else | |
437 | local_symbols = rd->local_symbols->data(); | |
438 | ||
09ec0418 CC |
439 | // For incremental links, allocate the counters for incremental relocations. |
440 | if (layout->incremental_inputs() != NULL) | |
441 | this->allocate_incremental_reloc_counts(); | |
442 | ||
92e059d8 ILT |
443 | for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin(); |
444 | p != rd->relocs.end(); | |
445 | ++p) | |
446 | { | |
6d03d481 ST |
447 | // When garbage collection is on, unreferenced sections are not included |
448 | // in the link that would have been included normally. This is known only | |
449 | // after Read_relocs hence this check has to be done again. | |
032ce4e9 ST |
450 | if (parameters->options().gc_sections() |
451 | || parameters->options().icf_enabled()) | |
6d03d481 ST |
452 | { |
453 | if (p->output_section == NULL) | |
454 | continue; | |
455 | } | |
8851ecca | 456 | if (!parameters->options().relocatable()) |
7019cd25 ILT |
457 | { |
458 | // As noted above, when not generating an object file, we | |
459 | // only scan allocated sections. We may see a non-allocated | |
460 | // section here if we are emitting relocs. | |
461 | if (p->is_data_section_allocated) | |
2ea97941 | 462 | target->scan_relocs(symtab, layout, this, p->data_shndx, |
7019cd25 ILT |
463 | p->sh_type, p->contents->data(), |
464 | p->reloc_count, p->output_section, | |
465 | p->needs_special_offset_handling, | |
466 | this->local_symbol_count_, | |
467 | local_symbols); | |
8851ecca | 468 | if (parameters->options().emit_relocs()) |
2ea97941 | 469 | this->emit_relocs_scan(symtab, layout, local_symbols, p); |
09ec0418 CC |
470 | if (layout->incremental_inputs() != NULL) |
471 | this->incremental_relocs_scan(p); | |
7019cd25 | 472 | } |
6a74a719 ILT |
473 | else |
474 | { | |
475 | Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx); | |
476 | gold_assert(rr != NULL); | |
477 | rr->set_reloc_count(p->reloc_count); | |
2ea97941 | 478 | target->scan_relocatable_relocs(symtab, layout, this, |
6a74a719 ILT |
479 | p->data_shndx, p->sh_type, |
480 | p->contents->data(), | |
481 | p->reloc_count, | |
482 | p->output_section, | |
483 | p->needs_special_offset_handling, | |
484 | this->local_symbol_count_, | |
485 | local_symbols, | |
486 | rr); | |
487 | } | |
488 | ||
92e059d8 ILT |
489 | delete p->contents; |
490 | p->contents = NULL; | |
491 | } | |
492 | ||
09ec0418 CC |
493 | // For incremental links, finalize the allocation of relocations. |
494 | if (layout->incremental_inputs() != NULL) | |
cdc29364 | 495 | this->finalize_incremental_relocs(layout, true); |
09ec0418 | 496 | |
92e059d8 ILT |
497 | if (rd->local_symbols != NULL) |
498 | { | |
499 | delete rd->local_symbols; | |
500 | rd->local_symbols = NULL; | |
501 | } | |
502 | } | |
503 | ||
7019cd25 ILT |
504 | // This is a strategy class we use when scanning for --emit-relocs. |
505 | ||
506 | template<int sh_type> | |
507 | class Emit_relocs_strategy | |
508 | { | |
509 | public: | |
510 | // A local non-section symbol. | |
511 | inline Relocatable_relocs::Reloc_strategy | |
68943102 | 512 | local_non_section_strategy(unsigned int, Relobj*, unsigned int) |
7019cd25 ILT |
513 | { return Relocatable_relocs::RELOC_COPY; } |
514 | ||
515 | // A local section symbol. | |
516 | inline Relocatable_relocs::Reloc_strategy | |
517 | local_section_strategy(unsigned int, Relobj*) | |
518 | { | |
519 | if (sh_type == elfcpp::SHT_RELA) | |
520 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA; | |
521 | else | |
522 | { | |
523 | // The addend is stored in the section contents. Since this | |
524 | // is not a relocatable link, we are going to apply the | |
525 | // relocation contents to the section as usual. This means | |
526 | // that we have no way to record the original addend. If the | |
527 | // original addend is not zero, there is basically no way for | |
528 | // the user to handle this correctly. Caveat emptor. | |
529 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0; | |
530 | } | |
531 | } | |
532 | ||
533 | // A global symbol. | |
534 | inline Relocatable_relocs::Reloc_strategy | |
535 | global_strategy(unsigned int, Relobj*, unsigned int) | |
536 | { return Relocatable_relocs::RELOC_COPY; } | |
537 | }; | |
538 | ||
539 | // Scan the input relocations for --emit-relocs. | |
540 | ||
541 | template<int size, bool big_endian> | |
542 | void | |
6fa2a40b | 543 | Sized_relobj_file<size, big_endian>::emit_relocs_scan( |
7019cd25 | 544 | Symbol_table* symtab, |
2ea97941 | 545 | Layout* layout, |
7019cd25 ILT |
546 | const unsigned char* plocal_syms, |
547 | const Read_relocs_data::Relocs_list::iterator& p) | |
548 | { | |
549 | Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx); | |
550 | gold_assert(rr != NULL); | |
551 | rr->set_reloc_count(p->reloc_count); | |
552 | ||
553 | if (p->sh_type == elfcpp::SHT_REL) | |
2ea97941 | 554 | this->emit_relocs_scan_reltype<elfcpp::SHT_REL>(symtab, layout, |
7019cd25 ILT |
555 | plocal_syms, p, rr); |
556 | else | |
557 | { | |
558 | gold_assert(p->sh_type == elfcpp::SHT_RELA); | |
2ea97941 | 559 | this->emit_relocs_scan_reltype<elfcpp::SHT_RELA>(symtab, layout, |
ad0f2072 | 560 | plocal_syms, p, rr); |
7019cd25 ILT |
561 | } |
562 | } | |
563 | ||
564 | // Scan the input relocation for --emit-relocs, templatized on the | |
565 | // type of the relocation section. | |
566 | ||
567 | template<int size, bool big_endian> | |
568 | template<int sh_type> | |
569 | void | |
6fa2a40b | 570 | Sized_relobj_file<size, big_endian>::emit_relocs_scan_reltype( |
7019cd25 | 571 | Symbol_table* symtab, |
2ea97941 | 572 | Layout* layout, |
7019cd25 ILT |
573 | const unsigned char* plocal_syms, |
574 | const Read_relocs_data::Relocs_list::iterator& p, | |
575 | Relocatable_relocs* rr) | |
576 | { | |
577 | scan_relocatable_relocs<size, big_endian, sh_type, | |
578 | Emit_relocs_strategy<sh_type> >( | |
7019cd25 | 579 | symtab, |
2ea97941 | 580 | layout, |
7019cd25 ILT |
581 | this, |
582 | p->data_shndx, | |
583 | p->contents->data(), | |
584 | p->reloc_count, | |
585 | p->output_section, | |
586 | p->needs_special_offset_handling, | |
587 | this->local_symbol_count_, | |
588 | plocal_syms, | |
589 | rr); | |
590 | } | |
591 | ||
09ec0418 CC |
592 | // Scan the input relocations for --incremental. |
593 | ||
594 | template<int size, bool big_endian> | |
595 | void | |
6fa2a40b | 596 | Sized_relobj_file<size, big_endian>::incremental_relocs_scan( |
09ec0418 CC |
597 | const Read_relocs_data::Relocs_list::iterator& p) |
598 | { | |
599 | if (p->sh_type == elfcpp::SHT_REL) | |
600 | this->incremental_relocs_scan_reltype<elfcpp::SHT_REL>(p); | |
601 | else | |
602 | { | |
603 | gold_assert(p->sh_type == elfcpp::SHT_RELA); | |
604 | this->incremental_relocs_scan_reltype<elfcpp::SHT_RELA>(p); | |
605 | } | |
606 | } | |
607 | ||
cdc29364 | 608 | // Scan the input relocation for --incremental, templatized on the |
09ec0418 CC |
609 | // type of the relocation section. |
610 | ||
611 | template<int size, bool big_endian> | |
612 | template<int sh_type> | |
613 | void | |
6fa2a40b | 614 | Sized_relobj_file<size, big_endian>::incremental_relocs_scan_reltype( |
09ec0418 CC |
615 | const Read_relocs_data::Relocs_list::iterator& p) |
616 | { | |
617 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; | |
618 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
619 | const unsigned char* prelocs = p->contents->data(); | |
620 | size_t reloc_count = p->reloc_count; | |
621 | ||
622 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
623 | { | |
624 | Reltype reloc(prelocs); | |
625 | ||
626 | if (p->needs_special_offset_handling | |
627 | && !p->output_section->is_input_address_mapped(this, p->data_shndx, | |
628 | reloc.get_r_offset())) | |
629 | continue; | |
630 | ||
631 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = | |
632 | reloc.get_r_info(); | |
633 | const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
634 | ||
635 | if (r_sym >= this->local_symbol_count_) | |
636 | this->count_incremental_reloc(r_sym - this->local_symbol_count_); | |
637 | } | |
638 | } | |
639 | ||
61ba1cf9 ILT |
640 | // Relocate the input sections and write out the local symbols. |
641 | ||
642 | template<int size, bool big_endian> | |
643 | void | |
6fa2a40b CC |
644 | Sized_relobj_file<size, big_endian>::do_relocate(const Symbol_table* symtab, |
645 | const Layout* layout, | |
646 | Output_file* of) | |
61ba1cf9 | 647 | { |
2ea97941 | 648 | unsigned int shnum = this->shnum(); |
61ba1cf9 ILT |
649 | |
650 | // Read the section headers. | |
645f8123 | 651 | const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(), |
2ea97941 | 652 | shnum * This::shdr_size, |
39d0cb0e | 653 | true, true); |
61ba1cf9 ILT |
654 | |
655 | Views views; | |
2ea97941 | 656 | views.resize(shnum); |
61ba1cf9 ILT |
657 | |
658 | // Make two passes over the sections. The first one copies the | |
659 | // section data to the output file. The second one applies | |
660 | // relocations. | |
661 | ||
487b39df | 662 | this->write_sections(layout, pshdrs, of, &views); |
61ba1cf9 | 663 | |
a9a60db6 ILT |
664 | // To speed up relocations, we set up hash tables for fast lookup of |
665 | // input offsets to output addresses. | |
666 | this->initialize_input_to_output_maps(); | |
667 | ||
61ba1cf9 ILT |
668 | // Apply relocations. |
669 | ||
09ec0418 | 670 | this->relocate_sections(symtab, layout, pshdrs, of, &views); |
61ba1cf9 | 671 | |
a9a60db6 ILT |
672 | // After we've done the relocations, we release the hash tables, |
673 | // since we no longer need them. | |
674 | this->free_input_to_output_maps(); | |
675 | ||
61ba1cf9 | 676 | // Write out the accumulated views. |
2ea97941 | 677 | for (unsigned int i = 1; i < shnum; ++i) |
61ba1cf9 ILT |
678 | { |
679 | if (views[i].view != NULL) | |
730cdc88 | 680 | { |
487b39df ILT |
681 | if (views[i].is_ctors_reverse_view) |
682 | this->reverse_words(views[i].view, views[i].view_size); | |
96803768 ILT |
683 | if (!views[i].is_postprocessing_view) |
684 | { | |
685 | if (views[i].is_input_output_view) | |
686 | of->write_input_output_view(views[i].offset, | |
687 | views[i].view_size, | |
688 | views[i].view); | |
689 | else | |
690 | of->write_output_view(views[i].offset, views[i].view_size, | |
691 | views[i].view); | |
692 | } | |
730cdc88 | 693 | } |
61ba1cf9 ILT |
694 | } |
695 | ||
696 | // Write out the local symbols. | |
2ea97941 | 697 | this->write_local_symbols(of, layout->sympool(), layout->dynpool(), |
cdc29364 | 698 | layout->symtab_xindex(), layout->dynsym_xindex(), |
bec5b579 | 699 | layout->symtab_section_offset()); |
61ba1cf9 ILT |
700 | } |
701 | ||
cb295612 ILT |
702 | // Sort a Read_multiple vector by file offset. |
703 | struct Read_multiple_compare | |
704 | { | |
705 | inline bool | |
706 | operator()(const File_read::Read_multiple_entry& rme1, | |
707 | const File_read::Read_multiple_entry& rme2) const | |
708 | { return rme1.file_offset < rme2.file_offset; } | |
709 | }; | |
710 | ||
61ba1cf9 ILT |
711 | // Write section data to the output file. PSHDRS points to the |
712 | // section headers. Record the views in *PVIEWS for use when | |
713 | // relocating. | |
714 | ||
715 | template<int size, bool big_endian> | |
716 | void | |
487b39df ILT |
717 | Sized_relobj_file<size, big_endian>::write_sections(const Layout* layout, |
718 | const unsigned char* pshdrs, | |
6fa2a40b CC |
719 | Output_file* of, |
720 | Views* pviews) | |
61ba1cf9 | 721 | { |
2ea97941 | 722 | unsigned int shnum = this->shnum(); |
ef9beddf | 723 | const Output_sections& out_sections(this->output_sections()); |
6fa2a40b | 724 | const std::vector<Address>& out_offsets(this->section_offsets()); |
61ba1cf9 | 725 | |
cb295612 ILT |
726 | File_read::Read_multiple rm; |
727 | bool is_sorted = true; | |
728 | ||
61ba1cf9 | 729 | const unsigned char* p = pshdrs + This::shdr_size; |
2ea97941 | 730 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) |
61ba1cf9 ILT |
731 | { |
732 | View_size* pvs = &(*pviews)[i]; | |
733 | ||
734 | pvs->view = NULL; | |
735 | ||
ef9beddf | 736 | const Output_section* os = out_sections[i]; |
61ba1cf9 ILT |
737 | if (os == NULL) |
738 | continue; | |
ef9beddf | 739 | Address output_offset = out_offsets[i]; |
61ba1cf9 ILT |
740 | |
741 | typename This::Shdr shdr(p); | |
742 | ||
743 | if (shdr.get_sh_type() == elfcpp::SHT_NOBITS) | |
744 | continue; | |
745 | ||
8851ecca ILT |
746 | if ((parameters->options().relocatable() |
747 | || parameters->options().emit_relocs()) | |
6a74a719 ILT |
748 | && (shdr.get_sh_type() == elfcpp::SHT_REL |
749 | || shdr.get_sh_type() == elfcpp::SHT_RELA) | |
750 | && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) | |
751 | { | |
7019cd25 ILT |
752 | // This is a reloc section in a relocatable link or when |
753 | // emitting relocs. We don't need to read the input file. | |
754 | // The size and file offset are stored in the | |
755 | // Relocatable_relocs structure. | |
6a74a719 ILT |
756 | Relocatable_relocs* rr = this->relocatable_relocs(i); |
757 | gold_assert(rr != NULL); | |
758 | Output_data* posd = rr->output_data(); | |
759 | gold_assert(posd != NULL); | |
760 | ||
761 | pvs->offset = posd->offset(); | |
762 | pvs->view_size = posd->data_size(); | |
763 | pvs->view = of->get_output_view(pvs->offset, pvs->view_size); | |
764 | pvs->address = posd->address(); | |
765 | pvs->is_input_output_view = false; | |
766 | pvs->is_postprocessing_view = false; | |
487b39df | 767 | pvs->is_ctors_reverse_view = false; |
6a74a719 ILT |
768 | |
769 | continue; | |
770 | } | |
771 | ||
96803768 ILT |
772 | // In the normal case, this input section is simply mapped to |
773 | // the output section at offset OUTPUT_OFFSET. | |
774 | ||
eff45813 CC |
775 | // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is |
776 | // handled specially--e.g., a .eh_frame section. The relocation | |
96803768 ILT |
777 | // routines need to check for each reloc where it should be |
778 | // applied. For this case, we need an input/output view for the | |
779 | // entire contents of the section in the output file. We don't | |
780 | // want to copy the contents of the input section to the output | |
781 | // section; the output section contents were already written, | |
782 | // and we waited for them in Relocate_task::is_runnable because | |
783 | // relocs_must_follow_section_writes is set for the object. | |
784 | ||
785 | // Regardless of which of the above cases is true, we have to | |
786 | // check requires_postprocessing of the output section. If that | |
787 | // is false, then we work with views of the output file | |
788 | // directly. If it is true, then we work with a separate | |
789 | // buffer, and the output section is responsible for writing the | |
790 | // final data to the output file. | |
791 | ||
2ea97941 | 792 | off_t output_section_offset; |
ef9beddf | 793 | Address output_section_size; |
96803768 ILT |
794 | if (!os->requires_postprocessing()) |
795 | { | |
2ea97941 | 796 | output_section_offset = os->offset(); |
ef9beddf | 797 | output_section_size = convert_types<Address, off_t>(os->data_size()); |
96803768 ILT |
798 | } |
799 | else | |
800 | { | |
2ea97941 | 801 | output_section_offset = 0; |
ef9beddf ILT |
802 | output_section_size = |
803 | convert_types<Address, off_t>(os->postprocessing_buffer_size()); | |
96803768 ILT |
804 | } |
805 | ||
730cdc88 | 806 | off_t view_start; |
fe8718a4 | 807 | section_size_type view_size; |
a2e47362 | 808 | bool must_decompress = false; |
eff45813 | 809 | if (output_offset != invalid_address) |
730cdc88 | 810 | { |
2ea97941 | 811 | view_start = output_section_offset + output_offset; |
fe8718a4 | 812 | view_size = convert_to_section_size_type(shdr.get_sh_size()); |
a2e47362 CC |
813 | section_size_type uncompressed_size; |
814 | if (this->section_is_compressed(i, &uncompressed_size)) | |
815 | { | |
816 | view_size = uncompressed_size; | |
817 | must_decompress = true; | |
818 | } | |
730cdc88 ILT |
819 | } |
820 | else | |
821 | { | |
2ea97941 | 822 | view_start = output_section_offset; |
fe8718a4 | 823 | view_size = convert_to_section_size_type(output_section_size); |
730cdc88 | 824 | } |
61ba1cf9 | 825 | |
730cdc88 | 826 | if (view_size == 0) |
ead1e424 ILT |
827 | continue; |
828 | ||
eff45813 | 829 | gold_assert(output_offset == invalid_address |
ef9beddf | 830 | || output_offset + view_size <= output_section_size); |
ead1e424 | 831 | |
2ea97941 | 832 | unsigned char* view; |
96803768 ILT |
833 | if (os->requires_postprocessing()) |
834 | { | |
835 | unsigned char* buffer = os->postprocessing_buffer(); | |
2ea97941 | 836 | view = buffer + view_start; |
a2e47362 | 837 | if (output_offset != invalid_address && !must_decompress) |
cb295612 ILT |
838 | { |
839 | off_t sh_offset = shdr.get_sh_offset(); | |
840 | if (!rm.empty() && rm.back().file_offset > sh_offset) | |
841 | is_sorted = false; | |
842 | rm.push_back(File_read::Read_multiple_entry(sh_offset, | |
2ea97941 | 843 | view_size, view)); |
cb295612 | 844 | } |
96803768 | 845 | } |
730cdc88 ILT |
846 | else |
847 | { | |
eff45813 | 848 | if (output_offset == invalid_address) |
2ea97941 | 849 | view = of->get_input_output_view(view_start, view_size); |
96803768 ILT |
850 | else |
851 | { | |
2ea97941 | 852 | view = of->get_output_view(view_start, view_size); |
a2e47362 CC |
853 | if (!must_decompress) |
854 | { | |
855 | off_t sh_offset = shdr.get_sh_offset(); | |
856 | if (!rm.empty() && rm.back().file_offset > sh_offset) | |
857 | is_sorted = false; | |
858 | rm.push_back(File_read::Read_multiple_entry(sh_offset, | |
859 | view_size, view)); | |
860 | } | |
96803768 | 861 | } |
730cdc88 | 862 | } |
92e059d8 | 863 | |
a2e47362 CC |
864 | if (must_decompress) |
865 | { | |
866 | // Read and decompress the section. | |
867 | section_size_type len; | |
868 | const unsigned char* p = this->section_contents(i, &len, false); | |
869 | if (!decompress_input_section(p, len, view, view_size)) | |
870 | this->error(_("could not decompress section %s"), | |
871 | this->section_name(i).c_str()); | |
872 | } | |
873 | ||
2ea97941 | 874 | pvs->view = view; |
730cdc88 | 875 | pvs->address = os->address(); |
eff45813 | 876 | if (output_offset != invalid_address) |
730cdc88 ILT |
877 | pvs->address += output_offset; |
878 | pvs->offset = view_start; | |
879 | pvs->view_size = view_size; | |
eff45813 | 880 | pvs->is_input_output_view = output_offset == invalid_address; |
96803768 | 881 | pvs->is_postprocessing_view = os->requires_postprocessing(); |
487b39df ILT |
882 | pvs->is_ctors_reverse_view = |
883 | (!parameters->options().relocatable() | |
884 | && view_size > size / 8 | |
885 | && (strcmp(os->name(), ".init_array") == 0 | |
886 | || strcmp(os->name(), ".fini_array") == 0) | |
887 | && layout->is_ctors_in_init_array(this, i)); | |
61ba1cf9 | 888 | } |
cb295612 ILT |
889 | |
890 | // Actually read the data. | |
891 | if (!rm.empty()) | |
892 | { | |
893 | if (!is_sorted) | |
894 | std::sort(rm.begin(), rm.end(), Read_multiple_compare()); | |
895 | this->read_multiple(rm); | |
896 | } | |
61ba1cf9 ILT |
897 | } |
898 | ||
899 | // Relocate section data. VIEWS points to the section data as views | |
900 | // in the output file. | |
901 | ||
902 | template<int size, bool big_endian> | |
903 | void | |
6fa2a40b | 904 | Sized_relobj_file<size, big_endian>::do_relocate_sections( |
92e059d8 | 905 | const Symbol_table* symtab, |
2ea97941 | 906 | const Layout* layout, |
92e059d8 | 907 | const unsigned char* pshdrs, |
09ec0418 | 908 | Output_file* of, |
92e059d8 | 909 | Views* pviews) |
61ba1cf9 | 910 | { |
2ea97941 | 911 | unsigned int shnum = this->shnum(); |
029ba973 ILT |
912 | Sized_target<size, big_endian>* target = |
913 | parameters->sized_target<size, big_endian>(); | |
61ba1cf9 | 914 | |
ef9beddf | 915 | const Output_sections& out_sections(this->output_sections()); |
6fa2a40b | 916 | const std::vector<Address>& out_offsets(this->section_offsets()); |
730cdc88 | 917 | |
92e059d8 | 918 | Relocate_info<size, big_endian> relinfo; |
92e059d8 | 919 | relinfo.symtab = symtab; |
2ea97941 | 920 | relinfo.layout = layout; |
92e059d8 | 921 | relinfo.object = this; |
92e059d8 | 922 | |
61ba1cf9 | 923 | const unsigned char* p = pshdrs + This::shdr_size; |
2ea97941 | 924 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) |
61ba1cf9 ILT |
925 | { |
926 | typename This::Shdr shdr(p); | |
927 | ||
928 | unsigned int sh_type = shdr.get_sh_type(); | |
929 | if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) | |
930 | continue; | |
931 | ||
1307d6cd ILT |
932 | off_t sh_size = shdr.get_sh_size(); |
933 | if (sh_size == 0) | |
934 | continue; | |
935 | ||
d491d34e | 936 | unsigned int index = this->adjust_shndx(shdr.get_sh_info()); |
61ba1cf9 ILT |
937 | if (index >= this->shnum()) |
938 | { | |
75f2446e ILT |
939 | this->error(_("relocation section %u has bad info %u"), |
940 | i, index); | |
941 | continue; | |
61ba1cf9 ILT |
942 | } |
943 | ||
ef9beddf | 944 | Output_section* os = out_sections[index]; |
730cdc88 | 945 | if (os == NULL) |
61ba1cf9 ILT |
946 | { |
947 | // This relocation section is against a section which we | |
948 | // discarded. | |
949 | continue; | |
950 | } | |
ef9beddf | 951 | Address output_offset = out_offsets[index]; |
61ba1cf9 | 952 | |
a3ad94ed | 953 | gold_assert((*pviews)[index].view != NULL); |
8851ecca | 954 | if (parameters->options().relocatable()) |
6a74a719 | 955 | gold_assert((*pviews)[i].view != NULL); |
61ba1cf9 | 956 | |
d491d34e | 957 | if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_) |
61ba1cf9 | 958 | { |
75f2446e ILT |
959 | gold_error(_("relocation section %u uses unexpected " |
960 | "symbol table %u"), | |
d491d34e | 961 | i, this->adjust_shndx(shdr.get_sh_link())); |
75f2446e | 962 | continue; |
61ba1cf9 ILT |
963 | } |
964 | ||
61ba1cf9 | 965 | const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(), |
39d0cb0e | 966 | sh_size, true, false); |
61ba1cf9 ILT |
967 | |
968 | unsigned int reloc_size; | |
969 | if (sh_type == elfcpp::SHT_REL) | |
970 | reloc_size = elfcpp::Elf_sizes<size>::rel_size; | |
971 | else | |
972 | reloc_size = elfcpp::Elf_sizes<size>::rela_size; | |
973 | ||
974 | if (reloc_size != shdr.get_sh_entsize()) | |
975 | { | |
75f2446e ILT |
976 | gold_error(_("unexpected entsize for reloc section %u: %lu != %u"), |
977 | i, static_cast<unsigned long>(shdr.get_sh_entsize()), | |
730cdc88 | 978 | reloc_size); |
75f2446e | 979 | continue; |
61ba1cf9 ILT |
980 | } |
981 | ||
982 | size_t reloc_count = sh_size / reloc_size; | |
f5c3f225 | 983 | if (static_cast<off_t>(reloc_count * reloc_size) != sh_size) |
61ba1cf9 | 984 | { |
75f2446e ILT |
985 | gold_error(_("reloc section %u size %lu uneven"), |
986 | i, static_cast<unsigned long>(sh_size)); | |
987 | continue; | |
61ba1cf9 ILT |
988 | } |
989 | ||
eff45813 | 990 | gold_assert(output_offset != invalid_address |
96803768 ILT |
991 | || this->relocs_must_follow_section_writes()); |
992 | ||
92e059d8 | 993 | relinfo.reloc_shndx = i; |
82bb573a | 994 | relinfo.reloc_shdr = p; |
92e059d8 | 995 | relinfo.data_shndx = index; |
82bb573a | 996 | relinfo.data_shdr = pshdrs + index * This::shdr_size; |
2ea97941 | 997 | unsigned char* view = (*pviews)[index].view; |
364c7fa5 ILT |
998 | Address address = (*pviews)[index].address; |
999 | section_size_type view_size = (*pviews)[index].view_size; | |
1000 | ||
1001 | Reloc_symbol_changes* reloc_map = NULL; | |
1002 | if (this->uses_split_stack() && output_offset != invalid_address) | |
1003 | { | |
1004 | typename This::Shdr data_shdr(pshdrs + index * This::shdr_size); | |
1005 | if ((data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0) | |
1006 | this->split_stack_adjust(symtab, pshdrs, sh_type, index, | |
2ea97941 | 1007 | prelocs, reloc_count, view, view_size, |
364c7fa5 ILT |
1008 | &reloc_map); |
1009 | } | |
1010 | ||
8851ecca | 1011 | if (!parameters->options().relocatable()) |
7019cd25 | 1012 | { |
364c7fa5 | 1013 | target->relocate_section(&relinfo, sh_type, prelocs, reloc_count, os, |
eff45813 | 1014 | output_offset == invalid_address, |
2ea97941 | 1015 | view, address, view_size, reloc_map); |
8851ecca | 1016 | if (parameters->options().emit_relocs()) |
7019cd25 | 1017 | this->emit_relocs(&relinfo, i, sh_type, prelocs, reloc_count, |
2ea97941 | 1018 | os, output_offset, view, address, view_size, |
364c7fa5 | 1019 | (*pviews)[i].view, (*pviews)[i].view_size); |
8c21d9d3 | 1020 | if (parameters->incremental()) |
09ec0418 CC |
1021 | this->incremental_relocs_write(&relinfo, sh_type, prelocs, |
1022 | reloc_count, os, output_offset, of); | |
7019cd25 | 1023 | } |
6a74a719 ILT |
1024 | else |
1025 | { | |
1026 | Relocatable_relocs* rr = this->relocatable_relocs(i); | |
364c7fa5 ILT |
1027 | target->relocate_for_relocatable(&relinfo, sh_type, prelocs, |
1028 | reloc_count, os, output_offset, rr, | |
2ea97941 | 1029 | view, address, view_size, |
6a74a719 ILT |
1030 | (*pviews)[i].view, |
1031 | (*pviews)[i].view_size); | |
1032 | } | |
61ba1cf9 ILT |
1033 | } |
1034 | } | |
1035 | ||
7019cd25 ILT |
1036 | // Emit the relocs for --emit-relocs. |
1037 | ||
1038 | template<int size, bool big_endian> | |
1039 | void | |
6fa2a40b | 1040 | Sized_relobj_file<size, big_endian>::emit_relocs( |
7019cd25 ILT |
1041 | const Relocate_info<size, big_endian>* relinfo, |
1042 | unsigned int i, | |
1043 | unsigned int sh_type, | |
1044 | const unsigned char* prelocs, | |
1045 | size_t reloc_count, | |
2ea97941 | 1046 | Output_section* output_section, |
ef9beddf | 1047 | typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section, |
2ea97941 | 1048 | unsigned char* view, |
7019cd25 ILT |
1049 | typename elfcpp::Elf_types<size>::Elf_Addr address, |
1050 | section_size_type view_size, | |
1051 | unsigned char* reloc_view, | |
1052 | section_size_type reloc_view_size) | |
1053 | { | |
1054 | if (sh_type == elfcpp::SHT_REL) | |
1055 | this->emit_relocs_reltype<elfcpp::SHT_REL>(relinfo, i, prelocs, | |
2ea97941 | 1056 | reloc_count, output_section, |
7019cd25 | 1057 | offset_in_output_section, |
2ea97941 | 1058 | view, address, view_size, |
7019cd25 ILT |
1059 | reloc_view, reloc_view_size); |
1060 | else | |
1061 | { | |
1062 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
1063 | this->emit_relocs_reltype<elfcpp::SHT_RELA>(relinfo, i, prelocs, | |
2ea97941 | 1064 | reloc_count, output_section, |
7019cd25 | 1065 | offset_in_output_section, |
2ea97941 | 1066 | view, address, view_size, |
7019cd25 ILT |
1067 | reloc_view, reloc_view_size); |
1068 | } | |
1069 | } | |
1070 | ||
1071 | // Emit the relocs for --emit-relocs, templatized on the type of the | |
1072 | // relocation section. | |
1073 | ||
1074 | template<int size, bool big_endian> | |
1075 | template<int sh_type> | |
1076 | void | |
6fa2a40b | 1077 | Sized_relobj_file<size, big_endian>::emit_relocs_reltype( |
7019cd25 ILT |
1078 | const Relocate_info<size, big_endian>* relinfo, |
1079 | unsigned int i, | |
1080 | const unsigned char* prelocs, | |
1081 | size_t reloc_count, | |
2ea97941 | 1082 | Output_section* output_section, |
ef9beddf | 1083 | typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section, |
2ea97941 | 1084 | unsigned char* view, |
7019cd25 ILT |
1085 | typename elfcpp::Elf_types<size>::Elf_Addr address, |
1086 | section_size_type view_size, | |
1087 | unsigned char* reloc_view, | |
1088 | section_size_type reloc_view_size) | |
1089 | { | |
1090 | const Relocatable_relocs* rr = this->relocatable_relocs(i); | |
1091 | relocate_for_relocatable<size, big_endian, sh_type>( | |
1092 | relinfo, | |
1093 | prelocs, | |
1094 | reloc_count, | |
2ea97941 | 1095 | output_section, |
7019cd25 ILT |
1096 | offset_in_output_section, |
1097 | rr, | |
2ea97941 | 1098 | view, |
7019cd25 ILT |
1099 | address, |
1100 | view_size, | |
1101 | reloc_view, | |
1102 | reloc_view_size); | |
1103 | } | |
1104 | ||
09ec0418 CC |
1105 | // Write the incremental relocs. |
1106 | ||
1107 | template<int size, bool big_endian> | |
1108 | void | |
6fa2a40b | 1109 | Sized_relobj_file<size, big_endian>::incremental_relocs_write( |
09ec0418 CC |
1110 | const Relocate_info<size, big_endian>* relinfo, |
1111 | unsigned int sh_type, | |
1112 | const unsigned char* prelocs, | |
1113 | size_t reloc_count, | |
1114 | Output_section* output_section, | |
1115 | Address output_offset, | |
1116 | Output_file* of) | |
1117 | { | |
1118 | if (sh_type == elfcpp::SHT_REL) | |
1119 | this->incremental_relocs_write_reltype<elfcpp::SHT_REL>( | |
1120 | relinfo, | |
1121 | prelocs, | |
1122 | reloc_count, | |
1123 | output_section, | |
1124 | output_offset, | |
1125 | of); | |
1126 | else | |
1127 | { | |
1128 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
1129 | this->incremental_relocs_write_reltype<elfcpp::SHT_RELA>( | |
1130 | relinfo, | |
1131 | prelocs, | |
1132 | reloc_count, | |
1133 | output_section, | |
1134 | output_offset, | |
1135 | of); | |
1136 | } | |
1137 | } | |
1138 | ||
1139 | // Write the incremental relocs, templatized on the type of the | |
1140 | // relocation section. | |
1141 | ||
1142 | template<int size, bool big_endian> | |
1143 | template<int sh_type> | |
1144 | void | |
6fa2a40b | 1145 | Sized_relobj_file<size, big_endian>::incremental_relocs_write_reltype( |
09ec0418 CC |
1146 | const Relocate_info<size, big_endian>* relinfo, |
1147 | const unsigned char* prelocs, | |
1148 | size_t reloc_count, | |
1149 | Output_section* output_section, | |
1150 | Address output_offset, | |
1151 | Output_file* of) | |
1152 | { | |
1153 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reloc; | |
1154 | const unsigned int reloc_size = | |
1155 | Reloc_types<sh_type, size, big_endian>::reloc_size; | |
1156 | const unsigned int sizeof_addr = size / 8; | |
cdc29364 CC |
1157 | const unsigned int incr_reloc_size = |
1158 | Incremental_relocs_reader<size, big_endian>::reloc_size; | |
09ec0418 CC |
1159 | |
1160 | unsigned int out_shndx = output_section->out_shndx(); | |
1161 | ||
1162 | // Get a view for the .gnu_incremental_relocs section. | |
1163 | ||
1164 | Incremental_inputs* inputs = relinfo->layout->incremental_inputs(); | |
1165 | gold_assert(inputs != NULL); | |
1166 | const off_t relocs_off = inputs->relocs_section()->offset(); | |
1167 | const off_t relocs_size = inputs->relocs_section()->data_size(); | |
1168 | unsigned char* const view = of->get_output_view(relocs_off, relocs_size); | |
1169 | ||
1170 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
1171 | { | |
1172 | Reloc reloc(prelocs); | |
1173 | ||
1174 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); | |
1175 | const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
1176 | const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); | |
1177 | ||
1178 | if (r_sym < this->local_symbol_count_) | |
1179 | continue; | |
1180 | ||
1181 | // Get the new offset--the location in the output section where | |
1182 | // this relocation should be applied. | |
1183 | ||
1184 | Address offset = reloc.get_r_offset(); | |
1185 | if (output_offset != invalid_address) | |
1186 | offset += output_offset; | |
1187 | else | |
1188 | { | |
1189 | section_offset_type sot_offset = | |
1190 | convert_types<section_offset_type, Address>(offset); | |
1191 | section_offset_type new_sot_offset = | |
1192 | output_section->output_offset(relinfo->object, | |
1193 | relinfo->data_shndx, | |
1194 | sot_offset); | |
1195 | gold_assert(new_sot_offset != -1); | |
1196 | offset += new_sot_offset; | |
1197 | } | |
1198 | ||
1199 | // Get the addend. | |
1200 | typename elfcpp::Elf_types<size>::Elf_Swxword addend; | |
1201 | if (sh_type == elfcpp::SHT_RELA) | |
1202 | addend = | |
1203 | Reloc_types<sh_type, size, big_endian>::get_reloc_addend(&reloc); | |
1204 | else | |
1205 | { | |
1206 | // FIXME: Get the addend for SHT_REL. | |
1207 | addend = 0; | |
1208 | } | |
1209 | ||
1210 | // Get the index of the output relocation. | |
1211 | ||
1212 | unsigned int reloc_index = | |
1213 | this->next_incremental_reloc_index(r_sym - this->local_symbol_count_); | |
1214 | ||
1215 | // Write the relocation. | |
1216 | ||
1217 | unsigned char* pov = view + reloc_index * incr_reloc_size; | |
1218 | elfcpp::Swap<32, big_endian>::writeval(pov, r_type); | |
1219 | elfcpp::Swap<32, big_endian>::writeval(pov + 4, out_shndx); | |
1220 | elfcpp::Swap<size, big_endian>::writeval(pov + 8, offset); | |
1221 | elfcpp::Swap<size, big_endian>::writeval(pov + 8 + sizeof_addr, addend); | |
1222 | of->write_output_view(pov - view, incr_reloc_size, view); | |
1223 | } | |
1224 | } | |
1225 | ||
a9a60db6 ILT |
1226 | // Create merge hash tables for the local symbols. These are used to |
1227 | // speed up relocations. | |
1228 | ||
1229 | template<int size, bool big_endian> | |
1230 | void | |
6fa2a40b | 1231 | Sized_relobj_file<size, big_endian>::initialize_input_to_output_maps() |
a9a60db6 ILT |
1232 | { |
1233 | const unsigned int loccount = this->local_symbol_count_; | |
1234 | for (unsigned int i = 1; i < loccount; ++i) | |
1235 | { | |
1236 | Symbol_value<size>& lv(this->local_values_[i]); | |
1237 | lv.initialize_input_to_output_map(this); | |
1238 | } | |
1239 | } | |
1240 | ||
1241 | // Free merge hash tables for the local symbols. | |
1242 | ||
1243 | template<int size, bool big_endian> | |
1244 | void | |
6fa2a40b | 1245 | Sized_relobj_file<size, big_endian>::free_input_to_output_maps() |
a9a60db6 ILT |
1246 | { |
1247 | const unsigned int loccount = this->local_symbol_count_; | |
1248 | for (unsigned int i = 1; i < loccount; ++i) | |
1249 | { | |
1250 | Symbol_value<size>& lv(this->local_values_[i]); | |
1251 | lv.free_input_to_output_map(); | |
1252 | } | |
1253 | } | |
1254 | ||
364c7fa5 ILT |
1255 | // If an object was compiled with -fsplit-stack, this is called to |
1256 | // check whether any relocations refer to functions defined in objects | |
1257 | // which were not compiled with -fsplit-stack. If they were, then we | |
1258 | // need to apply some target-specific adjustments to request | |
1259 | // additional stack space. | |
1260 | ||
1261 | template<int size, bool big_endian> | |
1262 | void | |
6fa2a40b | 1263 | Sized_relobj_file<size, big_endian>::split_stack_adjust( |
364c7fa5 ILT |
1264 | const Symbol_table* symtab, |
1265 | const unsigned char* pshdrs, | |
1266 | unsigned int sh_type, | |
1267 | unsigned int shndx, | |
1268 | const unsigned char* prelocs, | |
1269 | size_t reloc_count, | |
2ea97941 | 1270 | unsigned char* view, |
364c7fa5 ILT |
1271 | section_size_type view_size, |
1272 | Reloc_symbol_changes** reloc_map) | |
1273 | { | |
1274 | if (sh_type == elfcpp::SHT_REL) | |
1275 | this->split_stack_adjust_reltype<elfcpp::SHT_REL>(symtab, pshdrs, shndx, | |
1276 | prelocs, reloc_count, | |
2ea97941 | 1277 | view, view_size, |
364c7fa5 ILT |
1278 | reloc_map); |
1279 | else | |
1280 | { | |
1281 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
1282 | this->split_stack_adjust_reltype<elfcpp::SHT_RELA>(symtab, pshdrs, shndx, | |
1283 | prelocs, reloc_count, | |
2ea97941 | 1284 | view, view_size, |
364c7fa5 ILT |
1285 | reloc_map); |
1286 | } | |
1287 | } | |
1288 | ||
1289 | // Adjust for -fsplit-stack, templatized on the type of the relocation | |
1290 | // section. | |
1291 | ||
1292 | template<int size, bool big_endian> | |
1293 | template<int sh_type> | |
1294 | void | |
6fa2a40b | 1295 | Sized_relobj_file<size, big_endian>::split_stack_adjust_reltype( |
364c7fa5 ILT |
1296 | const Symbol_table* symtab, |
1297 | const unsigned char* pshdrs, | |
1298 | unsigned int shndx, | |
1299 | const unsigned char* prelocs, | |
1300 | size_t reloc_count, | |
2ea97941 | 1301 | unsigned char* view, |
364c7fa5 ILT |
1302 | section_size_type view_size, |
1303 | Reloc_symbol_changes** reloc_map) | |
1304 | { | |
1305 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; | |
1306 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
1307 | ||
1308 | size_t local_count = this->local_symbol_count(); | |
1309 | ||
1310 | std::vector<section_offset_type> non_split_refs; | |
1311 | ||
1312 | const unsigned char* pr = prelocs; | |
1313 | for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size) | |
1314 | { | |
1315 | Reltype reloc(pr); | |
1316 | ||
1317 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); | |
1318 | unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
1319 | if (r_sym < local_count) | |
1320 | continue; | |
1321 | ||
1322 | const Symbol* gsym = this->global_symbol(r_sym); | |
1323 | gold_assert(gsym != NULL); | |
1324 | if (gsym->is_forwarder()) | |
1325 | gsym = symtab->resolve_forwards(gsym); | |
1326 | ||
1327 | // See if this relocation refers to a function defined in an | |
1328 | // object compiled without -fsplit-stack. Note that we don't | |
1329 | // care about the type of relocation--this means that in some | |
1330 | // cases we will ask for a large stack unnecessarily, but this | |
1331 | // is not fatal. FIXME: Some targets have symbols which are | |
1332 | // functions but are not type STT_FUNC, e.g., STT_ARM_TFUNC. | |
b6848d3c | 1333 | if (!gsym->is_undefined() |
364c7fa5 ILT |
1334 | && gsym->source() == Symbol::FROM_OBJECT |
1335 | && !gsym->object()->uses_split_stack()) | |
1336 | { | |
b6848d3c ILT |
1337 | unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info()); |
1338 | if (parameters->target().is_call_to_non_split(gsym, r_type)) | |
1339 | { | |
1340 | section_offset_type offset = | |
1341 | convert_to_section_size_type(reloc.get_r_offset()); | |
1342 | non_split_refs.push_back(offset); | |
1343 | } | |
364c7fa5 ILT |
1344 | } |
1345 | } | |
1346 | ||
1347 | if (non_split_refs.empty()) | |
1348 | return; | |
1349 | ||
1350 | // At this point, every entry in NON_SPLIT_REFS indicates a | |
1351 | // relocation which refers to a function in an object compiled | |
1352 | // without -fsplit-stack. We now have to convert that list into a | |
1353 | // set of offsets to functions. First, we find all the functions. | |
1354 | ||
1355 | Function_offsets function_offsets; | |
1356 | this->find_functions(pshdrs, shndx, &function_offsets); | |
1357 | if (function_offsets.empty()) | |
1358 | return; | |
1359 | ||
1360 | // Now get a list of the function with references to non split-stack | |
1361 | // code. | |
1362 | ||
1363 | Function_offsets calls_non_split; | |
1364 | for (std::vector<section_offset_type>::const_iterator p | |
1365 | = non_split_refs.begin(); | |
1366 | p != non_split_refs.end(); | |
1367 | ++p) | |
1368 | { | |
1369 | Function_offsets::const_iterator low = function_offsets.lower_bound(*p); | |
1370 | if (low == function_offsets.end()) | |
1371 | --low; | |
1372 | else if (low->first == *p) | |
1373 | ; | |
1374 | else if (low == function_offsets.begin()) | |
1375 | continue; | |
1376 | else | |
1377 | --low; | |
1378 | ||
1379 | calls_non_split.insert(*low); | |
1380 | } | |
1381 | if (calls_non_split.empty()) | |
1382 | return; | |
1383 | ||
1384 | // Now we have a set of functions to adjust. The adjustments are | |
1385 | // target specific. Besides changing the output section view | |
1386 | // however, it likes, the target may request a relocation change | |
1387 | // from one global symbol name to another. | |
1388 | ||
1389 | for (Function_offsets::const_iterator p = calls_non_split.begin(); | |
1390 | p != calls_non_split.end(); | |
1391 | ++p) | |
1392 | { | |
1393 | std::string from; | |
1394 | std::string to; | |
1395 | parameters->target().calls_non_split(this, shndx, p->first, p->second, | |
2ea97941 | 1396 | view, view_size, &from, &to); |
364c7fa5 ILT |
1397 | if (!from.empty()) |
1398 | { | |
1399 | gold_assert(!to.empty()); | |
1400 | Symbol* tosym = NULL; | |
1401 | ||
1402 | // Find relocations in the relevant function which are for | |
1403 | // FROM. | |
1404 | pr = prelocs; | |
1405 | for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size) | |
1406 | { | |
1407 | Reltype reloc(pr); | |
1408 | ||
1409 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = | |
1410 | reloc.get_r_info(); | |
1411 | unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
1412 | if (r_sym < local_count) | |
1413 | continue; | |
1414 | ||
2ea97941 | 1415 | section_offset_type offset = |
364c7fa5 | 1416 | convert_to_section_size_type(reloc.get_r_offset()); |
2ea97941 ILT |
1417 | if (offset < p->first |
1418 | || (offset | |
364c7fa5 ILT |
1419 | >= (p->first |
1420 | + static_cast<section_offset_type>(p->second)))) | |
1421 | continue; | |
1422 | ||
1423 | const Symbol* gsym = this->global_symbol(r_sym); | |
1424 | if (from == gsym->name()) | |
1425 | { | |
1426 | if (tosym == NULL) | |
1427 | { | |
1428 | tosym = symtab->lookup(to.c_str()); | |
1429 | if (tosym == NULL) | |
1430 | { | |
1431 | this->error(_("could not convert call " | |
1432 | "to '%s' to '%s'"), | |
1433 | from.c_str(), to.c_str()); | |
1434 | break; | |
1435 | } | |
1436 | } | |
1437 | ||
1438 | if (*reloc_map == NULL) | |
1439 | *reloc_map = new Reloc_symbol_changes(reloc_count); | |
1440 | (*reloc_map)->set(i, tosym); | |
1441 | } | |
1442 | } | |
1443 | } | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | // Find all the function in this object defined in section SHNDX. | |
1448 | // Store their offsets in the section in FUNCTION_OFFSETS. | |
1449 | ||
1450 | template<int size, bool big_endian> | |
1451 | void | |
6fa2a40b | 1452 | Sized_relobj_file<size, big_endian>::find_functions( |
364c7fa5 ILT |
1453 | const unsigned char* pshdrs, |
1454 | unsigned int shndx, | |
6fa2a40b | 1455 | Sized_relobj_file<size, big_endian>::Function_offsets* function_offsets) |
364c7fa5 ILT |
1456 | { |
1457 | // We need to read the symbols to find the functions. If we wanted | |
1458 | // to, we could cache reading the symbols across all sections in the | |
1459 | // object. | |
2ea97941 ILT |
1460 | const unsigned int symtab_shndx = this->symtab_shndx_; |
1461 | typename This::Shdr symtabshdr(pshdrs + symtab_shndx * This::shdr_size); | |
364c7fa5 ILT |
1462 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
1463 | ||
1464 | typename elfcpp::Elf_types<size>::Elf_WXword sh_size = | |
1465 | symtabshdr.get_sh_size(); | |
1466 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), | |
1467 | sh_size, true, true); | |
1468 | ||
2ea97941 ILT |
1469 | const int sym_size = This::sym_size; |
1470 | const unsigned int symcount = sh_size / sym_size; | |
1471 | for (unsigned int i = 0; i < symcount; ++i, psyms += sym_size) | |
364c7fa5 ILT |
1472 | { |
1473 | typename elfcpp::Sym<size, big_endian> isym(psyms); | |
1474 | ||
1475 | // FIXME: Some targets can have functions which do not have type | |
1476 | // STT_FUNC, e.g., STT_ARM_TFUNC. | |
1477 | if (isym.get_st_type() != elfcpp::STT_FUNC | |
1478 | || isym.get_st_size() == 0) | |
1479 | continue; | |
1480 | ||
1481 | bool is_ordinary; | |
1482 | unsigned int sym_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(), | |
1483 | &is_ordinary); | |
1484 | if (!is_ordinary || sym_shndx != shndx) | |
1485 | continue; | |
1486 | ||
1487 | section_offset_type value = | |
1488 | convert_to_section_size_type(isym.get_st_value()); | |
1489 | section_size_type fnsize = | |
1490 | convert_to_section_size_type(isym.get_st_size()); | |
1491 | ||
1492 | (*function_offsets)[value] = fnsize; | |
1493 | } | |
1494 | } | |
1495 | ||
487b39df ILT |
1496 | // Reverse the words in a section. Used for .ctors sections mapped to |
1497 | // .init_array sections. See ctors_sections_in_init_array in | |
1498 | // layout.cc. | |
1499 | ||
1500 | template<int size, bool big_endian> | |
1501 | void | |
1502 | Sized_relobj_file<size, big_endian>::reverse_words(unsigned char* view, | |
1503 | section_size_type view_size) | |
1504 | { | |
1505 | typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype; | |
1506 | Valtype* vview = reinterpret_cast<Valtype*>(view); | |
1507 | section_size_type vview_size = view_size / (size / 8); | |
1508 | for (section_size_type i = 0; i < vview_size / 2; ++i) | |
1509 | { | |
1510 | Valtype tmp = vview[i]; | |
1511 | vview[i] = vview[vview_size - 1 - i]; | |
1512 | vview[vview_size - 1 - i] = tmp; | |
1513 | } | |
1514 | } | |
1515 | ||
a9a60db6 ILT |
1516 | // Class Merged_symbol_value. |
1517 | ||
1518 | template<int size> | |
1519 | void | |
1520 | Merged_symbol_value<size>::initialize_input_to_output_map( | |
1521 | const Relobj* object, | |
1522 | unsigned int input_shndx) | |
1523 | { | |
1524 | Object_merge_map* map = object->merge_map(); | |
1525 | map->initialize_input_to_output_map<size>(input_shndx, | |
1526 | this->output_start_address_, | |
1527 | &this->output_addresses_); | |
1528 | } | |
1529 | ||
1530 | // Get the output value corresponding to an input offset if we | |
1531 | // couldn't find it in the hash table. | |
1532 | ||
1533 | template<int size> | |
1534 | typename elfcpp::Elf_types<size>::Elf_Addr | |
1535 | Merged_symbol_value<size>::value_from_output_section( | |
1536 | const Relobj* object, | |
1537 | unsigned int input_shndx, | |
1538 | typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const | |
1539 | { | |
1540 | section_offset_type output_offset; | |
1541 | bool found = object->merge_map()->get_output_offset(NULL, input_shndx, | |
1542 | input_offset, | |
1543 | &output_offset); | |
1544 | ||
1545 | // If this assertion fails, it means that some relocation was | |
1546 | // against a portion of an input merge section which we didn't map | |
1547 | // to the output file and we didn't explicitly discard. We should | |
1548 | // always map all portions of input merge sections. | |
1549 | gold_assert(found); | |
1550 | ||
1551 | if (output_offset == -1) | |
1552 | return 0; | |
1553 | else | |
1554 | return this->output_start_address_ + output_offset; | |
1555 | } | |
1556 | ||
730cdc88 ILT |
1557 | // Track_relocs methods. |
1558 | ||
1559 | // Initialize the class to track the relocs. This gets the object, | |
1560 | // the reloc section index, and the type of the relocs. This returns | |
1561 | // false if something goes wrong. | |
1562 | ||
1563 | template<int size, bool big_endian> | |
1564 | bool | |
1565 | Track_relocs<size, big_endian>::initialize( | |
b696e6d4 | 1566 | Object* object, |
730cdc88 ILT |
1567 | unsigned int reloc_shndx, |
1568 | unsigned int reloc_type) | |
1569 | { | |
730cdc88 ILT |
1570 | // If RELOC_SHNDX is -1U, it means there is more than one reloc |
1571 | // section for the .eh_frame section. We can't handle that case. | |
1572 | if (reloc_shndx == -1U) | |
1573 | return false; | |
1574 | ||
1575 | // If RELOC_SHNDX is 0, there is no reloc section. | |
1576 | if (reloc_shndx == 0) | |
1577 | return true; | |
1578 | ||
1579 | // Get the contents of the reloc section. | |
1580 | this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false); | |
1581 | ||
1582 | if (reloc_type == elfcpp::SHT_REL) | |
1583 | this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size; | |
1584 | else if (reloc_type == elfcpp::SHT_RELA) | |
1585 | this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size; | |
1586 | else | |
1587 | gold_unreachable(); | |
1588 | ||
1589 | if (this->len_ % this->reloc_size_ != 0) | |
1590 | { | |
1591 | object->error(_("reloc section size %zu is not a multiple of " | |
1592 | "reloc size %d\n"), | |
1593 | static_cast<size_t>(this->len_), | |
1594 | this->reloc_size_); | |
1595 | return false; | |
1596 | } | |
1597 | ||
1598 | return true; | |
1599 | } | |
1600 | ||
1601 | // Return the offset of the next reloc, or -1 if there isn't one. | |
1602 | ||
1603 | template<int size, bool big_endian> | |
1604 | off_t | |
1605 | Track_relocs<size, big_endian>::next_offset() const | |
1606 | { | |
1607 | if (this->pos_ >= this->len_) | |
1608 | return -1; | |
1609 | ||
1610 | // Rel and Rela start out the same, so we can always use Rel to find | |
1611 | // the r_offset value. | |
1612 | elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_); | |
1613 | return rel.get_r_offset(); | |
1614 | } | |
1615 | ||
1616 | // Return the index of the symbol referenced by the next reloc, or -1U | |
1617 | // if there aren't any more relocs. | |
1618 | ||
1619 | template<int size, bool big_endian> | |
1620 | unsigned int | |
1621 | Track_relocs<size, big_endian>::next_symndx() const | |
1622 | { | |
1623 | if (this->pos_ >= this->len_) | |
1624 | return -1U; | |
1625 | ||
1626 | // Rel and Rela start out the same, so we can use Rel to find the | |
1627 | // symbol index. | |
1628 | elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_); | |
1629 | return elfcpp::elf_r_sym<size>(rel.get_r_info()); | |
1630 | } | |
1631 | ||
4dbfafcc ILT |
1632 | // Return the addend of the next reloc, or 0 if there isn't one. |
1633 | ||
1634 | template<int size, bool big_endian> | |
1635 | uint64_t | |
1636 | Track_relocs<size, big_endian>::next_addend() const | |
1637 | { | |
1638 | if (this->pos_ >= this->len_) | |
1639 | return 0; | |
1640 | if (this->reloc_size_ == elfcpp::Elf_sizes<size>::rel_size) | |
1641 | return 0; | |
1642 | elfcpp::Rela<size, big_endian> rela(this->prelocs_ + this->pos_); | |
1643 | return rela.get_r_addend(); | |
1644 | } | |
1645 | ||
730cdc88 ILT |
1646 | // Advance to the next reloc whose r_offset is greater than or equal |
1647 | // to OFFSET. Return the number of relocs we skip. | |
1648 | ||
1649 | template<int size, bool big_endian> | |
1650 | int | |
1651 | Track_relocs<size, big_endian>::advance(off_t offset) | |
1652 | { | |
1653 | int ret = 0; | |
1654 | while (this->pos_ < this->len_) | |
1655 | { | |
1656 | // Rel and Rela start out the same, so we can always use Rel to | |
1657 | // find the r_offset value. | |
1658 | elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_); | |
1659 | if (static_cast<off_t>(rel.get_r_offset()) >= offset) | |
1660 | break; | |
1661 | ++ret; | |
1662 | this->pos_ += this->reloc_size_; | |
1663 | } | |
1664 | return ret; | |
1665 | } | |
1666 | ||
12c0daef | 1667 | // Instantiate the templates we need. |
61ba1cf9 | 1668 | |
193a53d9 | 1669 | #ifdef HAVE_TARGET_32_LITTLE |
92e059d8 ILT |
1670 | template |
1671 | void | |
6fa2a40b | 1672 | Sized_relobj_file<32, false>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 1673 | #endif |
92e059d8 | 1674 | |
193a53d9 | 1675 | #ifdef HAVE_TARGET_32_BIG |
92e059d8 ILT |
1676 | template |
1677 | void | |
6fa2a40b | 1678 | Sized_relobj_file<32, true>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 1679 | #endif |
92e059d8 | 1680 | |
193a53d9 | 1681 | #ifdef HAVE_TARGET_64_LITTLE |
92e059d8 ILT |
1682 | template |
1683 | void | |
6fa2a40b | 1684 | Sized_relobj_file<64, false>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 1685 | #endif |
92e059d8 | 1686 | |
193a53d9 | 1687 | #ifdef HAVE_TARGET_64_BIG |
92e059d8 ILT |
1688 | template |
1689 | void | |
6fa2a40b | 1690 | Sized_relobj_file<64, true>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 1691 | #endif |
92e059d8 | 1692 | |
6d03d481 ST |
1693 | #ifdef HAVE_TARGET_32_LITTLE |
1694 | template | |
1695 | void | |
6fa2a40b CC |
1696 | Sized_relobj_file<32, false>::do_gc_process_relocs(Symbol_table* symtab, |
1697 | Layout* layout, | |
1698 | Read_relocs_data* rd); | |
6d03d481 ST |
1699 | #endif |
1700 | ||
1701 | #ifdef HAVE_TARGET_32_BIG | |
1702 | template | |
1703 | void | |
6fa2a40b CC |
1704 | Sized_relobj_file<32, true>::do_gc_process_relocs(Symbol_table* symtab, |
1705 | Layout* layout, | |
1706 | Read_relocs_data* rd); | |
6d03d481 ST |
1707 | #endif |
1708 | ||
1709 | #ifdef HAVE_TARGET_64_LITTLE | |
1710 | template | |
1711 | void | |
6fa2a40b CC |
1712 | Sized_relobj_file<64, false>::do_gc_process_relocs(Symbol_table* symtab, |
1713 | Layout* layout, | |
1714 | Read_relocs_data* rd); | |
6d03d481 ST |
1715 | #endif |
1716 | ||
1717 | #ifdef HAVE_TARGET_64_BIG | |
1718 | template | |
1719 | void | |
6fa2a40b CC |
1720 | Sized_relobj_file<64, true>::do_gc_process_relocs(Symbol_table* symtab, |
1721 | Layout* layout, | |
1722 | Read_relocs_data* rd); | |
6d03d481 ST |
1723 | #endif |
1724 | ||
193a53d9 | 1725 | #ifdef HAVE_TARGET_32_LITTLE |
92e059d8 ILT |
1726 | template |
1727 | void | |
6fa2a40b CC |
1728 | Sized_relobj_file<32, false>::do_scan_relocs(Symbol_table* symtab, |
1729 | Layout* layout, | |
1730 | Read_relocs_data* rd); | |
193a53d9 | 1731 | #endif |
92e059d8 | 1732 | |
193a53d9 | 1733 | #ifdef HAVE_TARGET_32_BIG |
92e059d8 ILT |
1734 | template |
1735 | void | |
6fa2a40b CC |
1736 | Sized_relobj_file<32, true>::do_scan_relocs(Symbol_table* symtab, |
1737 | Layout* layout, | |
1738 | Read_relocs_data* rd); | |
193a53d9 | 1739 | #endif |
92e059d8 | 1740 | |
193a53d9 | 1741 | #ifdef HAVE_TARGET_64_LITTLE |
92e059d8 ILT |
1742 | template |
1743 | void | |
6fa2a40b CC |
1744 | Sized_relobj_file<64, false>::do_scan_relocs(Symbol_table* symtab, |
1745 | Layout* layout, | |
1746 | Read_relocs_data* rd); | |
193a53d9 | 1747 | #endif |
92e059d8 | 1748 | |
193a53d9 | 1749 | #ifdef HAVE_TARGET_64_BIG |
92e059d8 ILT |
1750 | template |
1751 | void | |
6fa2a40b CC |
1752 | Sized_relobj_file<64, true>::do_scan_relocs(Symbol_table* symtab, |
1753 | Layout* layout, | |
1754 | Read_relocs_data* rd); | |
193a53d9 | 1755 | #endif |
92e059d8 | 1756 | |
193a53d9 | 1757 | #ifdef HAVE_TARGET_32_LITTLE |
61ba1cf9 ILT |
1758 | template |
1759 | void | |
6fa2a40b CC |
1760 | Sized_relobj_file<32, false>::do_relocate(const Symbol_table* symtab, |
1761 | const Layout* layout, | |
1762 | Output_file* of); | |
193a53d9 | 1763 | #endif |
61ba1cf9 | 1764 | |
193a53d9 | 1765 | #ifdef HAVE_TARGET_32_BIG |
61ba1cf9 ILT |
1766 | template |
1767 | void | |
6fa2a40b CC |
1768 | Sized_relobj_file<32, true>::do_relocate(const Symbol_table* symtab, |
1769 | const Layout* layout, | |
1770 | Output_file* of); | |
193a53d9 | 1771 | #endif |
61ba1cf9 | 1772 | |
193a53d9 | 1773 | #ifdef HAVE_TARGET_64_LITTLE |
61ba1cf9 ILT |
1774 | template |
1775 | void | |
6fa2a40b CC |
1776 | Sized_relobj_file<64, false>::do_relocate(const Symbol_table* symtab, |
1777 | const Layout* layout, | |
1778 | Output_file* of); | |
193a53d9 | 1779 | #endif |
61ba1cf9 | 1780 | |
193a53d9 | 1781 | #ifdef HAVE_TARGET_64_BIG |
61ba1cf9 ILT |
1782 | template |
1783 | void | |
6fa2a40b CC |
1784 | Sized_relobj_file<64, true>::do_relocate(const Symbol_table* symtab, |
1785 | const Layout* layout, | |
1786 | Output_file* of); | |
193a53d9 | 1787 | #endif |
61ba1cf9 | 1788 | |
72adc4fa DK |
1789 | #ifdef HAVE_TARGET_32_LITTLE |
1790 | template | |
1791 | void | |
6fa2a40b | 1792 | Sized_relobj_file<32, false>::do_relocate_sections( |
72adc4fa | 1793 | const Symbol_table* symtab, |
2ea97941 | 1794 | const Layout* layout, |
72adc4fa | 1795 | const unsigned char* pshdrs, |
09ec0418 | 1796 | Output_file* of, |
72adc4fa DK |
1797 | Views* pviews); |
1798 | #endif | |
1799 | ||
1800 | #ifdef HAVE_TARGET_32_BIG | |
1801 | template | |
1802 | void | |
6fa2a40b | 1803 | Sized_relobj_file<32, true>::do_relocate_sections( |
72adc4fa | 1804 | const Symbol_table* symtab, |
2ea97941 | 1805 | const Layout* layout, |
72adc4fa | 1806 | const unsigned char* pshdrs, |
09ec0418 | 1807 | Output_file* of, |
72adc4fa DK |
1808 | Views* pviews); |
1809 | #endif | |
1810 | ||
1811 | #ifdef HAVE_TARGET_64_LITTLE | |
1812 | template | |
1813 | void | |
6fa2a40b | 1814 | Sized_relobj_file<64, false>::do_relocate_sections( |
72adc4fa | 1815 | const Symbol_table* symtab, |
2ea97941 | 1816 | const Layout* layout, |
72adc4fa | 1817 | const unsigned char* pshdrs, |
09ec0418 | 1818 | Output_file* of, |
72adc4fa DK |
1819 | Views* pviews); |
1820 | #endif | |
1821 | ||
1822 | #ifdef HAVE_TARGET_64_BIG | |
1823 | template | |
1824 | void | |
6fa2a40b | 1825 | Sized_relobj_file<64, true>::do_relocate_sections( |
72adc4fa DK |
1826 | const Symbol_table* symtab, |
1827 | const Layout* layout, | |
1828 | const unsigned char* pshdrs, | |
09ec0418 | 1829 | Output_file* of, |
72adc4fa DK |
1830 | Views* pviews); |
1831 | #endif | |
1832 | ||
3e4afc80 ILT |
1833 | #ifdef HAVE_TARGET_32_LITTLE |
1834 | template | |
1835 | void | |
6fa2a40b | 1836 | Sized_relobj_file<32, false>::initialize_input_to_output_maps(); |
3e4afc80 ILT |
1837 | |
1838 | template | |
1839 | void | |
6fa2a40b | 1840 | Sized_relobj_file<32, false>::free_input_to_output_maps(); |
3e4afc80 ILT |
1841 | #endif |
1842 | ||
1843 | #ifdef HAVE_TARGET_32_BIG | |
1844 | template | |
1845 | void | |
6fa2a40b | 1846 | Sized_relobj_file<32, true>::initialize_input_to_output_maps(); |
3e4afc80 ILT |
1847 | |
1848 | template | |
1849 | void | |
6fa2a40b | 1850 | Sized_relobj_file<32, true>::free_input_to_output_maps(); |
3e4afc80 ILT |
1851 | #endif |
1852 | ||
1853 | #ifdef HAVE_TARGET_64_LITTLE | |
1854 | template | |
1855 | void | |
6fa2a40b | 1856 | Sized_relobj_file<64, false>::initialize_input_to_output_maps(); |
3e4afc80 ILT |
1857 | |
1858 | template | |
1859 | void | |
6fa2a40b | 1860 | Sized_relobj_file<64, false>::free_input_to_output_maps(); |
3e4afc80 ILT |
1861 | #endif |
1862 | ||
1863 | #ifdef HAVE_TARGET_64_BIG | |
1864 | template | |
1865 | void | |
6fa2a40b | 1866 | Sized_relobj_file<64, true>::initialize_input_to_output_maps(); |
3e4afc80 ILT |
1867 | |
1868 | template | |
1869 | void | |
6fa2a40b | 1870 | Sized_relobj_file<64, true>::free_input_to_output_maps(); |
3e4afc80 ILT |
1871 | #endif |
1872 | ||
a9a60db6 ILT |
1873 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
1874 | template | |
1875 | class Merged_symbol_value<32>; | |
1876 | #endif | |
1877 | ||
1878 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
1879 | template | |
1880 | class Merged_symbol_value<64>; | |
1881 | #endif | |
1882 | ||
1883 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
1884 | template | |
1885 | class Symbol_value<32>; | |
1886 | #endif | |
1887 | ||
1888 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
1889 | template | |
1890 | class Symbol_value<64>; | |
1891 | #endif | |
1892 | ||
730cdc88 ILT |
1893 | #ifdef HAVE_TARGET_32_LITTLE |
1894 | template | |
1895 | class Track_relocs<32, false>; | |
1896 | #endif | |
1897 | ||
1898 | #ifdef HAVE_TARGET_32_BIG | |
1899 | template | |
1900 | class Track_relocs<32, true>; | |
1901 | #endif | |
1902 | ||
1903 | #ifdef HAVE_TARGET_64_LITTLE | |
1904 | template | |
1905 | class Track_relocs<64, false>; | |
1906 | #endif | |
1907 | ||
1908 | #ifdef HAVE_TARGET_64_BIG | |
1909 | template | |
1910 | class Track_relocs<64, true>; | |
1911 | #endif | |
1912 | ||
61ba1cf9 | 1913 | } // End namespace gold. |