Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // reloc.cc -- relocate input files for gold. |
2 | ||
b3adc24a | 3 | // Copyright (C) 2006-2020 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
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, |
4d625b70 CC |
427 | Layout* layout, |
428 | Read_relocs_data* rd) | |
92e059d8 | 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 | // Scan the input relocations for --emit-relocs. |
505 | ||
506 | template<int size, bool big_endian> | |
507 | void | |
6fa2a40b | 508 | Sized_relobj_file<size, big_endian>::emit_relocs_scan( |
7019cd25 | 509 | Symbol_table* symtab, |
2ea97941 | 510 | Layout* layout, |
7019cd25 ILT |
511 | const unsigned char* plocal_syms, |
512 | const Read_relocs_data::Relocs_list::iterator& p) | |
513 | { | |
4d625b70 CC |
514 | Sized_target<size, big_endian>* target = |
515 | parameters->sized_target<size, big_endian>(); | |
516 | ||
7019cd25 ILT |
517 | Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx); |
518 | gold_assert(rr != NULL); | |
519 | rr->set_reloc_count(p->reloc_count); | |
4d625b70 | 520 | target->emit_relocs_scan( |
7019cd25 | 521 | symtab, |
2ea97941 | 522 | layout, |
7019cd25 ILT |
523 | this, |
524 | p->data_shndx, | |
4d625b70 | 525 | p->sh_type, |
7019cd25 ILT |
526 | p->contents->data(), |
527 | p->reloc_count, | |
528 | p->output_section, | |
529 | p->needs_special_offset_handling, | |
530 | this->local_symbol_count_, | |
531 | plocal_syms, | |
532 | rr); | |
533 | } | |
534 | ||
09ec0418 CC |
535 | // Scan the input relocations for --incremental. |
536 | ||
537 | template<int size, bool big_endian> | |
538 | void | |
6fa2a40b | 539 | Sized_relobj_file<size, big_endian>::incremental_relocs_scan( |
09ec0418 CC |
540 | const Read_relocs_data::Relocs_list::iterator& p) |
541 | { | |
542 | if (p->sh_type == elfcpp::SHT_REL) | |
543 | this->incremental_relocs_scan_reltype<elfcpp::SHT_REL>(p); | |
544 | else | |
545 | { | |
546 | gold_assert(p->sh_type == elfcpp::SHT_RELA); | |
547 | this->incremental_relocs_scan_reltype<elfcpp::SHT_RELA>(p); | |
548 | } | |
549 | } | |
550 | ||
cdc29364 | 551 | // Scan the input relocation for --incremental, templatized on the |
09ec0418 CC |
552 | // type of the relocation section. |
553 | ||
554 | template<int size, bool big_endian> | |
555 | template<int sh_type> | |
556 | void | |
6fa2a40b | 557 | Sized_relobj_file<size, big_endian>::incremental_relocs_scan_reltype( |
09ec0418 CC |
558 | const Read_relocs_data::Relocs_list::iterator& p) |
559 | { | |
560 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; | |
561 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
562 | const unsigned char* prelocs = p->contents->data(); | |
563 | size_t reloc_count = p->reloc_count; | |
564 | ||
565 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
566 | { | |
567 | Reltype reloc(prelocs); | |
568 | ||
569 | if (p->needs_special_offset_handling | |
570 | && !p->output_section->is_input_address_mapped(this, p->data_shndx, | |
571 | reloc.get_r_offset())) | |
572 | continue; | |
573 | ||
4d625b70 CC |
574 | // FIXME: Some targets have a non-standard r_info field. |
575 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); | |
09ec0418 CC |
576 | const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); |
577 | ||
578 | if (r_sym >= this->local_symbol_count_) | |
579 | this->count_incremental_reloc(r_sym - this->local_symbol_count_); | |
580 | } | |
581 | } | |
582 | ||
61ba1cf9 ILT |
583 | // Relocate the input sections and write out the local symbols. |
584 | ||
585 | template<int size, bool big_endian> | |
586 | void | |
6fa2a40b CC |
587 | Sized_relobj_file<size, big_endian>::do_relocate(const Symbol_table* symtab, |
588 | const Layout* layout, | |
589 | Output_file* of) | |
61ba1cf9 | 590 | { |
2ea97941 | 591 | unsigned int shnum = this->shnum(); |
61ba1cf9 ILT |
592 | |
593 | // Read the section headers. | |
645f8123 | 594 | const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(), |
2ea97941 | 595 | shnum * This::shdr_size, |
39d0cb0e | 596 | true, true); |
61ba1cf9 ILT |
597 | |
598 | Views views; | |
2ea97941 | 599 | views.resize(shnum); |
61ba1cf9 ILT |
600 | |
601 | // Make two passes over the sections. The first one copies the | |
602 | // section data to the output file. The second one applies | |
603 | // relocations. | |
604 | ||
487b39df | 605 | this->write_sections(layout, pshdrs, of, &views); |
61ba1cf9 | 606 | |
a9a60db6 ILT |
607 | // To speed up relocations, we set up hash tables for fast lookup of |
608 | // input offsets to output addresses. | |
609 | this->initialize_input_to_output_maps(); | |
610 | ||
6b2353a5 CC |
611 | // Make the views available through get_output_view() for the duration |
612 | // of this routine. This RAII class will reset output_views_ to NULL | |
613 | // when the views go out of scope. | |
614 | struct Set_output_views | |
615 | { | |
616 | Set_output_views(const Views** ppviews, const Views* pviews) | |
617 | { | |
618 | ppviews_ = ppviews; | |
619 | *ppviews = pviews; | |
620 | } | |
621 | ||
622 | ~Set_output_views() | |
623 | { *ppviews_ = NULL; } | |
624 | ||
625 | const Views** ppviews_; | |
626 | }; | |
627 | Set_output_views set_output_views(&this->output_views_, &views); | |
628 | ||
61ba1cf9 ILT |
629 | // Apply relocations. |
630 | ||
09ec0418 | 631 | this->relocate_sections(symtab, layout, pshdrs, of, &views); |
61ba1cf9 | 632 | |
a9a60db6 ILT |
633 | // After we've done the relocations, we release the hash tables, |
634 | // since we no longer need them. | |
635 | this->free_input_to_output_maps(); | |
636 | ||
61ba1cf9 | 637 | // Write out the accumulated views. |
2ea97941 | 638 | for (unsigned int i = 1; i < shnum; ++i) |
61ba1cf9 ILT |
639 | { |
640 | if (views[i].view != NULL) | |
730cdc88 | 641 | { |
487b39df ILT |
642 | if (views[i].is_ctors_reverse_view) |
643 | this->reverse_words(views[i].view, views[i].view_size); | |
96803768 ILT |
644 | if (!views[i].is_postprocessing_view) |
645 | { | |
646 | if (views[i].is_input_output_view) | |
647 | of->write_input_output_view(views[i].offset, | |
648 | views[i].view_size, | |
649 | views[i].view); | |
650 | else | |
651 | of->write_output_view(views[i].offset, views[i].view_size, | |
652 | views[i].view); | |
653 | } | |
730cdc88 | 654 | } |
61ba1cf9 ILT |
655 | } |
656 | ||
657 | // Write out the local symbols. | |
2ea97941 | 658 | this->write_local_symbols(of, layout->sympool(), layout->dynpool(), |
cdc29364 | 659 | layout->symtab_xindex(), layout->dynsym_xindex(), |
bec5b579 | 660 | layout->symtab_section_offset()); |
61ba1cf9 ILT |
661 | } |
662 | ||
cb295612 ILT |
663 | // Sort a Read_multiple vector by file offset. |
664 | struct Read_multiple_compare | |
665 | { | |
666 | inline bool | |
667 | operator()(const File_read::Read_multiple_entry& rme1, | |
668 | const File_read::Read_multiple_entry& rme2) const | |
669 | { return rme1.file_offset < rme2.file_offset; } | |
670 | }; | |
671 | ||
61ba1cf9 ILT |
672 | // Write section data to the output file. PSHDRS points to the |
673 | // section headers. Record the views in *PVIEWS for use when | |
674 | // relocating. | |
675 | ||
676 | template<int size, bool big_endian> | |
677 | void | |
487b39df ILT |
678 | Sized_relobj_file<size, big_endian>::write_sections(const Layout* layout, |
679 | const unsigned char* pshdrs, | |
6fa2a40b CC |
680 | Output_file* of, |
681 | Views* pviews) | |
61ba1cf9 | 682 | { |
2ea97941 | 683 | unsigned int shnum = this->shnum(); |
ef9beddf | 684 | const Output_sections& out_sections(this->output_sections()); |
6fa2a40b | 685 | const std::vector<Address>& out_offsets(this->section_offsets()); |
61ba1cf9 | 686 | |
cb295612 ILT |
687 | File_read::Read_multiple rm; |
688 | bool is_sorted = true; | |
689 | ||
61ba1cf9 | 690 | const unsigned char* p = pshdrs + This::shdr_size; |
2ea97941 | 691 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) |
61ba1cf9 ILT |
692 | { |
693 | View_size* pvs = &(*pviews)[i]; | |
694 | ||
695 | pvs->view = NULL; | |
696 | ||
ef9beddf | 697 | const Output_section* os = out_sections[i]; |
61ba1cf9 ILT |
698 | if (os == NULL) |
699 | continue; | |
ef9beddf | 700 | Address output_offset = out_offsets[i]; |
61ba1cf9 ILT |
701 | |
702 | typename This::Shdr shdr(p); | |
703 | ||
704 | if (shdr.get_sh_type() == elfcpp::SHT_NOBITS) | |
705 | continue; | |
706 | ||
8851ecca ILT |
707 | if ((parameters->options().relocatable() |
708 | || parameters->options().emit_relocs()) | |
6a74a719 ILT |
709 | && (shdr.get_sh_type() == elfcpp::SHT_REL |
710 | || shdr.get_sh_type() == elfcpp::SHT_RELA) | |
711 | && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) | |
712 | { | |
7019cd25 ILT |
713 | // This is a reloc section in a relocatable link or when |
714 | // emitting relocs. We don't need to read the input file. | |
715 | // The size and file offset are stored in the | |
716 | // Relocatable_relocs structure. | |
6a74a719 ILT |
717 | Relocatable_relocs* rr = this->relocatable_relocs(i); |
718 | gold_assert(rr != NULL); | |
719 | Output_data* posd = rr->output_data(); | |
720 | gold_assert(posd != NULL); | |
721 | ||
722 | pvs->offset = posd->offset(); | |
723 | pvs->view_size = posd->data_size(); | |
724 | pvs->view = of->get_output_view(pvs->offset, pvs->view_size); | |
725 | pvs->address = posd->address(); | |
726 | pvs->is_input_output_view = false; | |
727 | pvs->is_postprocessing_view = false; | |
487b39df | 728 | pvs->is_ctors_reverse_view = false; |
6a74a719 ILT |
729 | |
730 | continue; | |
731 | } | |
732 | ||
96803768 ILT |
733 | // In the normal case, this input section is simply mapped to |
734 | // the output section at offset OUTPUT_OFFSET. | |
735 | ||
eff45813 CC |
736 | // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is |
737 | // handled specially--e.g., a .eh_frame section. The relocation | |
96803768 ILT |
738 | // routines need to check for each reloc where it should be |
739 | // applied. For this case, we need an input/output view for the | |
740 | // entire contents of the section in the output file. We don't | |
741 | // want to copy the contents of the input section to the output | |
742 | // section; the output section contents were already written, | |
743 | // and we waited for them in Relocate_task::is_runnable because | |
744 | // relocs_must_follow_section_writes is set for the object. | |
745 | ||
746 | // Regardless of which of the above cases is true, we have to | |
747 | // check requires_postprocessing of the output section. If that | |
748 | // is false, then we work with views of the output file | |
749 | // directly. If it is true, then we work with a separate | |
750 | // buffer, and the output section is responsible for writing the | |
751 | // final data to the output file. | |
752 | ||
2ea97941 | 753 | off_t output_section_offset; |
ef9beddf | 754 | Address output_section_size; |
96803768 ILT |
755 | if (!os->requires_postprocessing()) |
756 | { | |
2ea97941 | 757 | output_section_offset = os->offset(); |
ef9beddf | 758 | output_section_size = convert_types<Address, off_t>(os->data_size()); |
96803768 ILT |
759 | } |
760 | else | |
761 | { | |
2ea97941 | 762 | output_section_offset = 0; |
ef9beddf ILT |
763 | output_section_size = |
764 | convert_types<Address, off_t>(os->postprocessing_buffer_size()); | |
96803768 ILT |
765 | } |
766 | ||
730cdc88 | 767 | off_t view_start; |
fe8718a4 | 768 | section_size_type view_size; |
a2e47362 | 769 | bool must_decompress = false; |
eff45813 | 770 | if (output_offset != invalid_address) |
730cdc88 | 771 | { |
2ea97941 | 772 | view_start = output_section_offset + output_offset; |
fe8718a4 | 773 | view_size = convert_to_section_size_type(shdr.get_sh_size()); |
a2e47362 CC |
774 | section_size_type uncompressed_size; |
775 | if (this->section_is_compressed(i, &uncompressed_size)) | |
776 | { | |
777 | view_size = uncompressed_size; | |
778 | must_decompress = true; | |
779 | } | |
730cdc88 ILT |
780 | } |
781 | else | |
782 | { | |
2ea97941 | 783 | view_start = output_section_offset; |
fe8718a4 | 784 | view_size = convert_to_section_size_type(output_section_size); |
730cdc88 | 785 | } |
61ba1cf9 | 786 | |
730cdc88 | 787 | if (view_size == 0) |
ead1e424 ILT |
788 | continue; |
789 | ||
eff45813 | 790 | gold_assert(output_offset == invalid_address |
ef9beddf | 791 | || output_offset + view_size <= output_section_size); |
ead1e424 | 792 | |
2ea97941 | 793 | unsigned char* view; |
96803768 ILT |
794 | if (os->requires_postprocessing()) |
795 | { | |
796 | unsigned char* buffer = os->postprocessing_buffer(); | |
2ea97941 | 797 | view = buffer + view_start; |
a2e47362 | 798 | if (output_offset != invalid_address && !must_decompress) |
cb295612 ILT |
799 | { |
800 | off_t sh_offset = shdr.get_sh_offset(); | |
801 | if (!rm.empty() && rm.back().file_offset > sh_offset) | |
802 | is_sorted = false; | |
803 | rm.push_back(File_read::Read_multiple_entry(sh_offset, | |
2ea97941 | 804 | view_size, view)); |
cb295612 | 805 | } |
96803768 | 806 | } |
730cdc88 ILT |
807 | else |
808 | { | |
eff45813 | 809 | if (output_offset == invalid_address) |
2ea97941 | 810 | view = of->get_input_output_view(view_start, view_size); |
96803768 ILT |
811 | else |
812 | { | |
2ea97941 | 813 | view = of->get_output_view(view_start, view_size); |
a2e47362 CC |
814 | if (!must_decompress) |
815 | { | |
816 | off_t sh_offset = shdr.get_sh_offset(); | |
817 | if (!rm.empty() && rm.back().file_offset > sh_offset) | |
818 | is_sorted = false; | |
819 | rm.push_back(File_read::Read_multiple_entry(sh_offset, | |
820 | view_size, view)); | |
821 | } | |
96803768 | 822 | } |
730cdc88 | 823 | } |
92e059d8 | 824 | |
a2e47362 CC |
825 | if (must_decompress) |
826 | { | |
827 | // Read and decompress the section. | |
828 | section_size_type len; | |
829 | const unsigned char* p = this->section_contents(i, &len, false); | |
48058663 L |
830 | if (!decompress_input_section(p, len, view, view_size, |
831 | size, big_endian, | |
832 | shdr.get_sh_flags())) | |
a2e47362 CC |
833 | this->error(_("could not decompress section %s"), |
834 | this->section_name(i).c_str()); | |
835 | } | |
836 | ||
2ea97941 | 837 | pvs->view = view; |
730cdc88 | 838 | pvs->address = os->address(); |
eff45813 | 839 | if (output_offset != invalid_address) |
730cdc88 ILT |
840 | pvs->address += output_offset; |
841 | pvs->offset = view_start; | |
842 | pvs->view_size = view_size; | |
eff45813 | 843 | pvs->is_input_output_view = output_offset == invalid_address; |
96803768 | 844 | pvs->is_postprocessing_view = os->requires_postprocessing(); |
487b39df ILT |
845 | pvs->is_ctors_reverse_view = |
846 | (!parameters->options().relocatable() | |
847 | && view_size > size / 8 | |
848 | && (strcmp(os->name(), ".init_array") == 0 | |
849 | || strcmp(os->name(), ".fini_array") == 0) | |
850 | && layout->is_ctors_in_init_array(this, i)); | |
61ba1cf9 | 851 | } |
cb295612 ILT |
852 | |
853 | // Actually read the data. | |
854 | if (!rm.empty()) | |
855 | { | |
856 | if (!is_sorted) | |
857 | std::sort(rm.begin(), rm.end(), Read_multiple_compare()); | |
858 | this->read_multiple(rm); | |
859 | } | |
61ba1cf9 ILT |
860 | } |
861 | ||
862 | // Relocate section data. VIEWS points to the section data as views | |
863 | // in the output file. | |
864 | ||
865 | template<int size, bool big_endian> | |
866 | void | |
6fa2a40b | 867 | Sized_relobj_file<size, big_endian>::do_relocate_sections( |
92e059d8 | 868 | const Symbol_table* symtab, |
2ea97941 | 869 | const Layout* layout, |
92e059d8 | 870 | const unsigned char* pshdrs, |
09ec0418 | 871 | Output_file* of, |
92e059d8 | 872 | Views* pviews) |
61ba1cf9 | 873 | { |
98461510 CC |
874 | this->relocate_section_range(symtab, layout, pshdrs, of, pviews, |
875 | 1, this->shnum() - 1); | |
876 | } | |
877 | ||
878 | // Relocate section data for the range of sections START_SHNDX through | |
879 | // END_SHNDX. | |
880 | ||
881 | template<int size, bool big_endian> | |
882 | void | |
883 | Sized_relobj_file<size, big_endian>::relocate_section_range( | |
884 | const Symbol_table* symtab, | |
885 | const Layout* layout, | |
886 | const unsigned char* pshdrs, | |
887 | Output_file* of, | |
888 | Views* pviews, | |
889 | unsigned int start_shndx, | |
890 | unsigned int end_shndx) | |
891 | { | |
892 | gold_assert(start_shndx >= 1); | |
893 | gold_assert(end_shndx < this->shnum()); | |
894 | ||
895 | if (end_shndx < start_shndx) | |
896 | return; | |
897 | ||
029ba973 ILT |
898 | Sized_target<size, big_endian>* target = |
899 | parameters->sized_target<size, big_endian>(); | |
61ba1cf9 | 900 | |
ef9beddf | 901 | const Output_sections& out_sections(this->output_sections()); |
6fa2a40b | 902 | const std::vector<Address>& out_offsets(this->section_offsets()); |
730cdc88 | 903 | |
92e059d8 | 904 | Relocate_info<size, big_endian> relinfo; |
92e059d8 | 905 | relinfo.symtab = symtab; |
2ea97941 | 906 | relinfo.layout = layout; |
92e059d8 | 907 | relinfo.object = this; |
92e059d8 | 908 | |
98461510 CC |
909 | const unsigned char* p = pshdrs + start_shndx * This::shdr_size; |
910 | for (unsigned int i = start_shndx; i <= end_shndx; ++i, p += This::shdr_size) | |
61ba1cf9 ILT |
911 | { |
912 | typename This::Shdr shdr(p); | |
913 | ||
914 | unsigned int sh_type = shdr.get_sh_type(); | |
915 | if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) | |
916 | continue; | |
917 | ||
1307d6cd ILT |
918 | off_t sh_size = shdr.get_sh_size(); |
919 | if (sh_size == 0) | |
920 | continue; | |
921 | ||
d491d34e | 922 | unsigned int index = this->adjust_shndx(shdr.get_sh_info()); |
61ba1cf9 ILT |
923 | if (index >= this->shnum()) |
924 | { | |
75f2446e ILT |
925 | this->error(_("relocation section %u has bad info %u"), |
926 | i, index); | |
927 | continue; | |
61ba1cf9 ILT |
928 | } |
929 | ||
ef9beddf | 930 | Output_section* os = out_sections[index]; |
730cdc88 | 931 | if (os == NULL) |
61ba1cf9 ILT |
932 | { |
933 | // This relocation section is against a section which we | |
934 | // discarded. | |
935 | continue; | |
936 | } | |
ef9beddf | 937 | Address output_offset = out_offsets[index]; |
61ba1cf9 | 938 | |
a3ad94ed | 939 | gold_assert((*pviews)[index].view != NULL); |
8851ecca | 940 | if (parameters->options().relocatable()) |
6a74a719 | 941 | gold_assert((*pviews)[i].view != NULL); |
61ba1cf9 | 942 | |
d491d34e | 943 | if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_) |
61ba1cf9 | 944 | { |
75f2446e ILT |
945 | gold_error(_("relocation section %u uses unexpected " |
946 | "symbol table %u"), | |
d491d34e | 947 | i, this->adjust_shndx(shdr.get_sh_link())); |
75f2446e | 948 | continue; |
61ba1cf9 ILT |
949 | } |
950 | ||
61ba1cf9 | 951 | const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(), |
39d0cb0e | 952 | sh_size, true, false); |
61ba1cf9 ILT |
953 | |
954 | unsigned int reloc_size; | |
955 | if (sh_type == elfcpp::SHT_REL) | |
956 | reloc_size = elfcpp::Elf_sizes<size>::rel_size; | |
957 | else | |
958 | reloc_size = elfcpp::Elf_sizes<size>::rela_size; | |
959 | ||
960 | if (reloc_size != shdr.get_sh_entsize()) | |
961 | { | |
75f2446e ILT |
962 | gold_error(_("unexpected entsize for reloc section %u: %lu != %u"), |
963 | i, static_cast<unsigned long>(shdr.get_sh_entsize()), | |
730cdc88 | 964 | reloc_size); |
75f2446e | 965 | continue; |
61ba1cf9 ILT |
966 | } |
967 | ||
968 | size_t reloc_count = sh_size / reloc_size; | |
f5c3f225 | 969 | if (static_cast<off_t>(reloc_count * reloc_size) != sh_size) |
61ba1cf9 | 970 | { |
75f2446e ILT |
971 | gold_error(_("reloc section %u size %lu uneven"), |
972 | i, static_cast<unsigned long>(sh_size)); | |
973 | continue; | |
61ba1cf9 ILT |
974 | } |
975 | ||
eff45813 | 976 | gold_assert(output_offset != invalid_address |
96803768 ILT |
977 | || this->relocs_must_follow_section_writes()); |
978 | ||
92e059d8 | 979 | relinfo.reloc_shndx = i; |
82bb573a | 980 | relinfo.reloc_shdr = p; |
92e059d8 | 981 | relinfo.data_shndx = index; |
82bb573a | 982 | relinfo.data_shdr = pshdrs + index * This::shdr_size; |
2ea97941 | 983 | unsigned char* view = (*pviews)[index].view; |
364c7fa5 ILT |
984 | Address address = (*pviews)[index].address; |
985 | section_size_type view_size = (*pviews)[index].view_size; | |
986 | ||
987 | Reloc_symbol_changes* reloc_map = NULL; | |
988 | if (this->uses_split_stack() && output_offset != invalid_address) | |
989 | { | |
990 | typename This::Shdr data_shdr(pshdrs + index * This::shdr_size); | |
991 | if ((data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0) | |
992 | this->split_stack_adjust(symtab, pshdrs, sh_type, index, | |
2ea97941 | 993 | prelocs, reloc_count, view, view_size, |
4d625b70 | 994 | &reloc_map, target); |
364c7fa5 ILT |
995 | } |
996 | ||
91a65d2f AM |
997 | Relocatable_relocs* rr = NULL; |
998 | if (parameters->options().emit_relocs() | |
999 | || parameters->options().relocatable()) | |
1000 | rr = this->relocatable_relocs(i); | |
1001 | relinfo.rr = rr; | |
1002 | ||
8851ecca | 1003 | if (!parameters->options().relocatable()) |
7019cd25 | 1004 | { |
364c7fa5 | 1005 | target->relocate_section(&relinfo, sh_type, prelocs, reloc_count, os, |
eff45813 | 1006 | output_offset == invalid_address, |
2ea97941 | 1007 | view, address, view_size, reloc_map); |
8851ecca | 1008 | if (parameters->options().emit_relocs()) |
91a65d2f AM |
1009 | target->relocate_relocs(&relinfo, sh_type, prelocs, reloc_count, |
1010 | os, output_offset, | |
1011 | view, address, view_size, | |
1012 | (*pviews)[i].view, | |
1013 | (*pviews)[i].view_size); | |
8c21d9d3 | 1014 | if (parameters->incremental()) |
09ec0418 CC |
1015 | this->incremental_relocs_write(&relinfo, sh_type, prelocs, |
1016 | reloc_count, os, output_offset, of); | |
7019cd25 | 1017 | } |
6a74a719 | 1018 | else |
91a65d2f AM |
1019 | target->relocate_relocs(&relinfo, sh_type, prelocs, reloc_count, |
1020 | os, output_offset, | |
1021 | view, address, view_size, | |
1022 | (*pviews)[i].view, | |
1023 | (*pviews)[i].view_size); | |
61ba1cf9 ILT |
1024 | } |
1025 | } | |
1026 | ||
6b2353a5 CC |
1027 | // Return the output view for section SHNDX. |
1028 | ||
1029 | template<int size, bool big_endian> | |
39040bb9 | 1030 | unsigned char* |
6b2353a5 CC |
1031 | Sized_relobj_file<size, big_endian>::do_get_output_view( |
1032 | unsigned int shndx, | |
1033 | section_size_type* plen) const | |
1034 | { | |
1035 | gold_assert(this->output_views_ != NULL); | |
1036 | gold_assert(shndx < this->output_views_->size()); | |
1037 | const View_size& v = (*this->output_views_)[shndx]; | |
1038 | *plen = v.view_size; | |
1039 | return v.view; | |
1040 | } | |
1041 | ||
09ec0418 CC |
1042 | // Write the incremental relocs. |
1043 | ||
1044 | template<int size, bool big_endian> | |
1045 | void | |
6fa2a40b | 1046 | Sized_relobj_file<size, big_endian>::incremental_relocs_write( |
09ec0418 CC |
1047 | const Relocate_info<size, big_endian>* relinfo, |
1048 | unsigned int sh_type, | |
1049 | const unsigned char* prelocs, | |
1050 | size_t reloc_count, | |
1051 | Output_section* output_section, | |
1052 | Address output_offset, | |
1053 | Output_file* of) | |
1054 | { | |
1055 | if (sh_type == elfcpp::SHT_REL) | |
1056 | this->incremental_relocs_write_reltype<elfcpp::SHT_REL>( | |
1057 | relinfo, | |
1058 | prelocs, | |
1059 | reloc_count, | |
1060 | output_section, | |
1061 | output_offset, | |
1062 | of); | |
1063 | else | |
1064 | { | |
1065 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
1066 | this->incremental_relocs_write_reltype<elfcpp::SHT_RELA>( | |
1067 | relinfo, | |
1068 | prelocs, | |
1069 | reloc_count, | |
1070 | output_section, | |
1071 | output_offset, | |
1072 | of); | |
1073 | } | |
1074 | } | |
1075 | ||
1076 | // Write the incremental relocs, templatized on the type of the | |
1077 | // relocation section. | |
1078 | ||
1079 | template<int size, bool big_endian> | |
1080 | template<int sh_type> | |
1081 | void | |
6fa2a40b | 1082 | Sized_relobj_file<size, big_endian>::incremental_relocs_write_reltype( |
09ec0418 CC |
1083 | const Relocate_info<size, big_endian>* relinfo, |
1084 | const unsigned char* prelocs, | |
1085 | size_t reloc_count, | |
1086 | Output_section* output_section, | |
1087 | Address output_offset, | |
1088 | Output_file* of) | |
1089 | { | |
1090 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reloc; | |
1091 | const unsigned int reloc_size = | |
1092 | Reloc_types<sh_type, size, big_endian>::reloc_size; | |
1093 | const unsigned int sizeof_addr = size / 8; | |
cdc29364 CC |
1094 | const unsigned int incr_reloc_size = |
1095 | Incremental_relocs_reader<size, big_endian>::reloc_size; | |
09ec0418 CC |
1096 | |
1097 | unsigned int out_shndx = output_section->out_shndx(); | |
1098 | ||
1099 | // Get a view for the .gnu_incremental_relocs section. | |
1100 | ||
1101 | Incremental_inputs* inputs = relinfo->layout->incremental_inputs(); | |
1102 | gold_assert(inputs != NULL); | |
1103 | const off_t relocs_off = inputs->relocs_section()->offset(); | |
1104 | const off_t relocs_size = inputs->relocs_section()->data_size(); | |
1105 | unsigned char* const view = of->get_output_view(relocs_off, relocs_size); | |
1106 | ||
1107 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
1108 | { | |
1109 | Reloc reloc(prelocs); | |
1110 | ||
4d625b70 | 1111 | // FIXME: Some targets have a non-standard r_info field. |
09ec0418 CC |
1112 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); |
1113 | const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
1114 | const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); | |
1115 | ||
1116 | if (r_sym < this->local_symbol_count_) | |
1117 | continue; | |
1118 | ||
1119 | // Get the new offset--the location in the output section where | |
1120 | // this relocation should be applied. | |
1121 | ||
1122 | Address offset = reloc.get_r_offset(); | |
1123 | if (output_offset != invalid_address) | |
1124 | offset += output_offset; | |
1125 | else | |
1126 | { | |
1127 | section_offset_type sot_offset = | |
1128 | convert_types<section_offset_type, Address>(offset); | |
1129 | section_offset_type new_sot_offset = | |
1130 | output_section->output_offset(relinfo->object, | |
1131 | relinfo->data_shndx, | |
1132 | sot_offset); | |
1133 | gold_assert(new_sot_offset != -1); | |
1134 | offset += new_sot_offset; | |
1135 | } | |
1136 | ||
1137 | // Get the addend. | |
1138 | typename elfcpp::Elf_types<size>::Elf_Swxword addend; | |
1139 | if (sh_type == elfcpp::SHT_RELA) | |
1140 | addend = | |
1141 | Reloc_types<sh_type, size, big_endian>::get_reloc_addend(&reloc); | |
1142 | else | |
1143 | { | |
1144 | // FIXME: Get the addend for SHT_REL. | |
1145 | addend = 0; | |
1146 | } | |
1147 | ||
1148 | // Get the index of the output relocation. | |
1149 | ||
1150 | unsigned int reloc_index = | |
1151 | this->next_incremental_reloc_index(r_sym - this->local_symbol_count_); | |
1152 | ||
1153 | // Write the relocation. | |
1154 | ||
1155 | unsigned char* pov = view + reloc_index * incr_reloc_size; | |
1156 | elfcpp::Swap<32, big_endian>::writeval(pov, r_type); | |
1157 | elfcpp::Swap<32, big_endian>::writeval(pov + 4, out_shndx); | |
1158 | elfcpp::Swap<size, big_endian>::writeval(pov + 8, offset); | |
1159 | elfcpp::Swap<size, big_endian>::writeval(pov + 8 + sizeof_addr, addend); | |
1160 | of->write_output_view(pov - view, incr_reloc_size, view); | |
1161 | } | |
1162 | } | |
1163 | ||
a9a60db6 ILT |
1164 | // Create merge hash tables for the local symbols. These are used to |
1165 | // speed up relocations. | |
1166 | ||
1167 | template<int size, bool big_endian> | |
1168 | void | |
6fa2a40b | 1169 | Sized_relobj_file<size, big_endian>::initialize_input_to_output_maps() |
a9a60db6 ILT |
1170 | { |
1171 | const unsigned int loccount = this->local_symbol_count_; | |
1172 | for (unsigned int i = 1; i < loccount; ++i) | |
1173 | { | |
1174 | Symbol_value<size>& lv(this->local_values_[i]); | |
1175 | lv.initialize_input_to_output_map(this); | |
1176 | } | |
1177 | } | |
1178 | ||
1179 | // Free merge hash tables for the local symbols. | |
1180 | ||
1181 | template<int size, bool big_endian> | |
1182 | void | |
6fa2a40b | 1183 | Sized_relobj_file<size, big_endian>::free_input_to_output_maps() |
a9a60db6 ILT |
1184 | { |
1185 | const unsigned int loccount = this->local_symbol_count_; | |
1186 | for (unsigned int i = 1; i < loccount; ++i) | |
1187 | { | |
1188 | Symbol_value<size>& lv(this->local_values_[i]); | |
1189 | lv.free_input_to_output_map(); | |
1190 | } | |
1191 | } | |
1192 | ||
364c7fa5 ILT |
1193 | // If an object was compiled with -fsplit-stack, this is called to |
1194 | // check whether any relocations refer to functions defined in objects | |
1195 | // which were not compiled with -fsplit-stack. If they were, then we | |
1196 | // need to apply some target-specific adjustments to request | |
1197 | // additional stack space. | |
1198 | ||
1199 | template<int size, bool big_endian> | |
1200 | void | |
6fa2a40b | 1201 | Sized_relobj_file<size, big_endian>::split_stack_adjust( |
364c7fa5 ILT |
1202 | const Symbol_table* symtab, |
1203 | const unsigned char* pshdrs, | |
1204 | unsigned int sh_type, | |
1205 | unsigned int shndx, | |
1206 | const unsigned char* prelocs, | |
1207 | size_t reloc_count, | |
2ea97941 | 1208 | unsigned char* view, |
364c7fa5 | 1209 | section_size_type view_size, |
4d625b70 CC |
1210 | Reloc_symbol_changes** reloc_map, |
1211 | const Sized_target<size, big_endian>* target) | |
364c7fa5 ILT |
1212 | { |
1213 | if (sh_type == elfcpp::SHT_REL) | |
1214 | this->split_stack_adjust_reltype<elfcpp::SHT_REL>(symtab, pshdrs, shndx, | |
1215 | prelocs, reloc_count, | |
2ea97941 | 1216 | view, view_size, |
4d625b70 | 1217 | reloc_map, target); |
364c7fa5 ILT |
1218 | else |
1219 | { | |
1220 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
1221 | this->split_stack_adjust_reltype<elfcpp::SHT_RELA>(symtab, pshdrs, shndx, | |
1222 | prelocs, reloc_count, | |
2ea97941 | 1223 | view, view_size, |
4d625b70 | 1224 | reloc_map, target); |
364c7fa5 ILT |
1225 | } |
1226 | } | |
1227 | ||
1228 | // Adjust for -fsplit-stack, templatized on the type of the relocation | |
1229 | // section. | |
1230 | ||
1231 | template<int size, bool big_endian> | |
1232 | template<int sh_type> | |
1233 | void | |
6fa2a40b | 1234 | Sized_relobj_file<size, big_endian>::split_stack_adjust_reltype( |
364c7fa5 ILT |
1235 | const Symbol_table* symtab, |
1236 | const unsigned char* pshdrs, | |
1237 | unsigned int shndx, | |
1238 | const unsigned char* prelocs, | |
1239 | size_t reloc_count, | |
2ea97941 | 1240 | unsigned char* view, |
364c7fa5 | 1241 | section_size_type view_size, |
4d625b70 CC |
1242 | Reloc_symbol_changes** reloc_map, |
1243 | const Sized_target<size, big_endian>* target) | |
364c7fa5 ILT |
1244 | { |
1245 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; | |
1246 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
1247 | ||
1248 | size_t local_count = this->local_symbol_count(); | |
1249 | ||
1250 | std::vector<section_offset_type> non_split_refs; | |
1251 | ||
1252 | const unsigned char* pr = prelocs; | |
1253 | for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size) | |
1254 | { | |
4d625b70 CC |
1255 | // Some supported targets have a non-standard r_info field. |
1256 | // If this call is too slow, we can move this routine to | |
1257 | // target-reloc.h and templatize it on Classify_reloc. | |
1258 | unsigned int r_sym = target->get_r_sym(pr); | |
364c7fa5 ILT |
1259 | if (r_sym < local_count) |
1260 | continue; | |
1261 | ||
1262 | const Symbol* gsym = this->global_symbol(r_sym); | |
1263 | gold_assert(gsym != NULL); | |
1264 | if (gsym->is_forwarder()) | |
1265 | gsym = symtab->resolve_forwards(gsym); | |
1266 | ||
1267 | // See if this relocation refers to a function defined in an | |
1268 | // object compiled without -fsplit-stack. Note that we don't | |
1269 | // care about the type of relocation--this means that in some | |
1270 | // cases we will ask for a large stack unnecessarily, but this | |
1271 | // is not fatal. FIXME: Some targets have symbols which are | |
1272 | // functions but are not type STT_FUNC, e.g., STT_ARM_TFUNC. | |
b6848d3c | 1273 | if (!gsym->is_undefined() |
364c7fa5 ILT |
1274 | && gsym->source() == Symbol::FROM_OBJECT |
1275 | && !gsym->object()->uses_split_stack()) | |
1276 | { | |
bd8d853f MK |
1277 | if (parameters->target().is_call_to_non_split(gsym, pr, view, |
1278 | view_size)) | |
b6848d3c | 1279 | { |
4d625b70 | 1280 | Reltype reloc(pr); |
b6848d3c ILT |
1281 | section_offset_type offset = |
1282 | convert_to_section_size_type(reloc.get_r_offset()); | |
1283 | non_split_refs.push_back(offset); | |
1284 | } | |
364c7fa5 ILT |
1285 | } |
1286 | } | |
1287 | ||
1288 | if (non_split_refs.empty()) | |
1289 | return; | |
1290 | ||
1291 | // At this point, every entry in NON_SPLIT_REFS indicates a | |
1292 | // relocation which refers to a function in an object compiled | |
1293 | // without -fsplit-stack. We now have to convert that list into a | |
1294 | // set of offsets to functions. First, we find all the functions. | |
1295 | ||
1296 | Function_offsets function_offsets; | |
1297 | this->find_functions(pshdrs, shndx, &function_offsets); | |
1298 | if (function_offsets.empty()) | |
1299 | return; | |
1300 | ||
1301 | // Now get a list of the function with references to non split-stack | |
1302 | // code. | |
1303 | ||
1304 | Function_offsets calls_non_split; | |
1305 | for (std::vector<section_offset_type>::const_iterator p | |
1306 | = non_split_refs.begin(); | |
1307 | p != non_split_refs.end(); | |
1308 | ++p) | |
1309 | { | |
1310 | Function_offsets::const_iterator low = function_offsets.lower_bound(*p); | |
1311 | if (low == function_offsets.end()) | |
1312 | --low; | |
1313 | else if (low->first == *p) | |
1314 | ; | |
1315 | else if (low == function_offsets.begin()) | |
1316 | continue; | |
1317 | else | |
1318 | --low; | |
1319 | ||
1320 | calls_non_split.insert(*low); | |
1321 | } | |
1322 | if (calls_non_split.empty()) | |
1323 | return; | |
1324 | ||
1325 | // Now we have a set of functions to adjust. The adjustments are | |
1326 | // target specific. Besides changing the output section view | |
1327 | // however, it likes, the target may request a relocation change | |
1328 | // from one global symbol name to another. | |
1329 | ||
1330 | for (Function_offsets::const_iterator p = calls_non_split.begin(); | |
1331 | p != calls_non_split.end(); | |
1332 | ++p) | |
1333 | { | |
1334 | std::string from; | |
1335 | std::string to; | |
1336 | parameters->target().calls_non_split(this, shndx, p->first, p->second, | |
6e0813d3 | 1337 | prelocs, reloc_count, |
2ea97941 | 1338 | view, view_size, &from, &to); |
364c7fa5 ILT |
1339 | if (!from.empty()) |
1340 | { | |
1341 | gold_assert(!to.empty()); | |
1342 | Symbol* tosym = NULL; | |
1343 | ||
1344 | // Find relocations in the relevant function which are for | |
1345 | // FROM. | |
1346 | pr = prelocs; | |
1347 | for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size) | |
1348 | { | |
1349 | Reltype reloc(pr); | |
1350 | ||
4d625b70 | 1351 | unsigned int r_sym = target->get_r_sym(pr); |
364c7fa5 ILT |
1352 | if (r_sym < local_count) |
1353 | continue; | |
1354 | ||
2ea97941 | 1355 | section_offset_type offset = |
364c7fa5 | 1356 | convert_to_section_size_type(reloc.get_r_offset()); |
2ea97941 ILT |
1357 | if (offset < p->first |
1358 | || (offset | |
364c7fa5 ILT |
1359 | >= (p->first |
1360 | + static_cast<section_offset_type>(p->second)))) | |
1361 | continue; | |
1362 | ||
1363 | const Symbol* gsym = this->global_symbol(r_sym); | |
1364 | if (from == gsym->name()) | |
1365 | { | |
1366 | if (tosym == NULL) | |
1367 | { | |
1368 | tosym = symtab->lookup(to.c_str()); | |
1369 | if (tosym == NULL) | |
1370 | { | |
1371 | this->error(_("could not convert call " | |
1372 | "to '%s' to '%s'"), | |
1373 | from.c_str(), to.c_str()); | |
1374 | break; | |
1375 | } | |
1376 | } | |
1377 | ||
1378 | if (*reloc_map == NULL) | |
1379 | *reloc_map = new Reloc_symbol_changes(reloc_count); | |
1380 | (*reloc_map)->set(i, tosym); | |
1381 | } | |
1382 | } | |
1383 | } | |
1384 | } | |
1385 | } | |
1386 | ||
1387 | // Find all the function in this object defined in section SHNDX. | |
1388 | // Store their offsets in the section in FUNCTION_OFFSETS. | |
1389 | ||
1390 | template<int size, bool big_endian> | |
1391 | void | |
6fa2a40b | 1392 | Sized_relobj_file<size, big_endian>::find_functions( |
364c7fa5 ILT |
1393 | const unsigned char* pshdrs, |
1394 | unsigned int shndx, | |
6fa2a40b | 1395 | Sized_relobj_file<size, big_endian>::Function_offsets* function_offsets) |
364c7fa5 ILT |
1396 | { |
1397 | // We need to read the symbols to find the functions. If we wanted | |
1398 | // to, we could cache reading the symbols across all sections in the | |
1399 | // object. | |
2ea97941 ILT |
1400 | const unsigned int symtab_shndx = this->symtab_shndx_; |
1401 | typename This::Shdr symtabshdr(pshdrs + symtab_shndx * This::shdr_size); | |
364c7fa5 ILT |
1402 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
1403 | ||
1404 | typename elfcpp::Elf_types<size>::Elf_WXword sh_size = | |
1405 | symtabshdr.get_sh_size(); | |
1406 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), | |
1407 | sh_size, true, true); | |
1408 | ||
2ea97941 ILT |
1409 | const int sym_size = This::sym_size; |
1410 | const unsigned int symcount = sh_size / sym_size; | |
1411 | for (unsigned int i = 0; i < symcount; ++i, psyms += sym_size) | |
364c7fa5 ILT |
1412 | { |
1413 | typename elfcpp::Sym<size, big_endian> isym(psyms); | |
1414 | ||
1415 | // FIXME: Some targets can have functions which do not have type | |
1416 | // STT_FUNC, e.g., STT_ARM_TFUNC. | |
1417 | if (isym.get_st_type() != elfcpp::STT_FUNC | |
1418 | || isym.get_st_size() == 0) | |
1419 | continue; | |
1420 | ||
1421 | bool is_ordinary; | |
bbec1a5d AM |
1422 | Symbol_location loc; |
1423 | loc.shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(), | |
1424 | &is_ordinary); | |
1425 | if (!is_ordinary) | |
1426 | continue; | |
1427 | ||
1428 | loc.object = this; | |
1429 | loc.offset = isym.get_st_value(); | |
1430 | parameters->target().function_location(&loc); | |
1431 | ||
1432 | if (loc.shndx != shndx) | |
364c7fa5 ILT |
1433 | continue; |
1434 | ||
1435 | section_offset_type value = | |
bbec1a5d | 1436 | convert_to_section_size_type(loc.offset); |
364c7fa5 ILT |
1437 | section_size_type fnsize = |
1438 | convert_to_section_size_type(isym.get_st_size()); | |
1439 | ||
1440 | (*function_offsets)[value] = fnsize; | |
1441 | } | |
1442 | } | |
1443 | ||
487b39df ILT |
1444 | // Reverse the words in a section. Used for .ctors sections mapped to |
1445 | // .init_array sections. See ctors_sections_in_init_array in | |
1446 | // layout.cc. | |
1447 | ||
1448 | template<int size, bool big_endian> | |
1449 | void | |
1450 | Sized_relobj_file<size, big_endian>::reverse_words(unsigned char* view, | |
1451 | section_size_type view_size) | |
1452 | { | |
1453 | typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype; | |
1454 | Valtype* vview = reinterpret_cast<Valtype*>(view); | |
1455 | section_size_type vview_size = view_size / (size / 8); | |
1456 | for (section_size_type i = 0; i < vview_size / 2; ++i) | |
1457 | { | |
1458 | Valtype tmp = vview[i]; | |
1459 | vview[i] = vview[vview_size - 1 - i]; | |
1460 | vview[vview_size - 1 - i] = tmp; | |
1461 | } | |
1462 | } | |
1463 | ||
a9a60db6 ILT |
1464 | // Class Merged_symbol_value. |
1465 | ||
1466 | template<int size> | |
1467 | void | |
1468 | Merged_symbol_value<size>::initialize_input_to_output_map( | |
1469 | const Relobj* object, | |
1470 | unsigned int input_shndx) | |
1471 | { | |
dbe40a88 RÁE |
1472 | object->initialize_input_to_output_map<size>(input_shndx, |
1473 | this->output_start_address_, | |
1474 | &this->output_addresses_); | |
a9a60db6 ILT |
1475 | } |
1476 | ||
1477 | // Get the output value corresponding to an input offset if we | |
1478 | // couldn't find it in the hash table. | |
1479 | ||
1480 | template<int size> | |
1481 | typename elfcpp::Elf_types<size>::Elf_Addr | |
1482 | Merged_symbol_value<size>::value_from_output_section( | |
1483 | const Relobj* object, | |
1484 | unsigned int input_shndx, | |
1485 | typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const | |
1486 | { | |
1487 | section_offset_type output_offset; | |
dbe40a88 RÁE |
1488 | bool found = object->merge_output_offset(input_shndx, input_offset, |
1489 | &output_offset); | |
a9a60db6 ILT |
1490 | |
1491 | // If this assertion fails, it means that some relocation was | |
1492 | // against a portion of an input merge section which we didn't map | |
1493 | // to the output file and we didn't explicitly discard. We should | |
1494 | // always map all portions of input merge sections. | |
1495 | gold_assert(found); | |
1496 | ||
1497 | if (output_offset == -1) | |
1498 | return 0; | |
1499 | else | |
1500 | return this->output_start_address_ + output_offset; | |
1501 | } | |
1502 | ||
730cdc88 ILT |
1503 | // Track_relocs methods. |
1504 | ||
1505 | // Initialize the class to track the relocs. This gets the object, | |
1506 | // the reloc section index, and the type of the relocs. This returns | |
1507 | // false if something goes wrong. | |
1508 | ||
1509 | template<int size, bool big_endian> | |
1510 | bool | |
1511 | Track_relocs<size, big_endian>::initialize( | |
b696e6d4 | 1512 | Object* object, |
730cdc88 ILT |
1513 | unsigned int reloc_shndx, |
1514 | unsigned int reloc_type) | |
1515 | { | |
730cdc88 ILT |
1516 | // If RELOC_SHNDX is -1U, it means there is more than one reloc |
1517 | // section for the .eh_frame section. We can't handle that case. | |
1518 | if (reloc_shndx == -1U) | |
1519 | return false; | |
1520 | ||
1521 | // If RELOC_SHNDX is 0, there is no reloc section. | |
1522 | if (reloc_shndx == 0) | |
1523 | return true; | |
1524 | ||
1525 | // Get the contents of the reloc section. | |
1526 | this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false); | |
1527 | ||
1528 | if (reloc_type == elfcpp::SHT_REL) | |
1529 | this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size; | |
1530 | else if (reloc_type == elfcpp::SHT_RELA) | |
1531 | this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size; | |
1532 | else | |
1533 | gold_unreachable(); | |
1534 | ||
1535 | if (this->len_ % this->reloc_size_ != 0) | |
1536 | { | |
1537 | object->error(_("reloc section size %zu is not a multiple of " | |
1538 | "reloc size %d\n"), | |
1539 | static_cast<size_t>(this->len_), | |
1540 | this->reloc_size_); | |
1541 | return false; | |
1542 | } | |
1543 | ||
1544 | return true; | |
1545 | } | |
1546 | ||
1547 | // Return the offset of the next reloc, or -1 if there isn't one. | |
1548 | ||
1549 | template<int size, bool big_endian> | |
1550 | off_t | |
1551 | Track_relocs<size, big_endian>::next_offset() const | |
1552 | { | |
1553 | if (this->pos_ >= this->len_) | |
1554 | return -1; | |
1555 | ||
1556 | // Rel and Rela start out the same, so we can always use Rel to find | |
1557 | // the r_offset value. | |
1558 | elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_); | |
1559 | return rel.get_r_offset(); | |
1560 | } | |
1561 | ||
1562 | // Return the index of the symbol referenced by the next reloc, or -1U | |
1563 | // if there aren't any more relocs. | |
1564 | ||
1565 | template<int size, bool big_endian> | |
1566 | unsigned int | |
1567 | Track_relocs<size, big_endian>::next_symndx() const | |
1568 | { | |
1569 | if (this->pos_ >= this->len_) | |
1570 | return -1U; | |
4d625b70 CC |
1571 | Sized_target<size, big_endian>* target |
1572 | = parameters->sized_target<size, big_endian>(); | |
1573 | return target->get_r_sym(this->prelocs_ + this->pos_); | |
730cdc88 ILT |
1574 | } |
1575 | ||
4dbfafcc ILT |
1576 | // Return the addend of the next reloc, or 0 if there isn't one. |
1577 | ||
1578 | template<int size, bool big_endian> | |
1579 | uint64_t | |
1580 | Track_relocs<size, big_endian>::next_addend() const | |
1581 | { | |
1582 | if (this->pos_ >= this->len_) | |
1583 | return 0; | |
1584 | if (this->reloc_size_ == elfcpp::Elf_sizes<size>::rel_size) | |
1585 | return 0; | |
1586 | elfcpp::Rela<size, big_endian> rela(this->prelocs_ + this->pos_); | |
1587 | return rela.get_r_addend(); | |
1588 | } | |
1589 | ||
730cdc88 ILT |
1590 | // Advance to the next reloc whose r_offset is greater than or equal |
1591 | // to OFFSET. Return the number of relocs we skip. | |
1592 | ||
1593 | template<int size, bool big_endian> | |
1594 | int | |
1595 | Track_relocs<size, big_endian>::advance(off_t offset) | |
1596 | { | |
1597 | int ret = 0; | |
1598 | while (this->pos_ < this->len_) | |
1599 | { | |
1600 | // Rel and Rela start out the same, so we can always use Rel to | |
1601 | // find the r_offset value. | |
1602 | elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_); | |
1603 | if (static_cast<off_t>(rel.get_r_offset()) >= offset) | |
1604 | break; | |
1605 | ++ret; | |
1606 | this->pos_ += this->reloc_size_; | |
1607 | } | |
1608 | return ret; | |
1609 | } | |
1610 | ||
12c0daef | 1611 | // Instantiate the templates we need. |
61ba1cf9 | 1612 | |
193a53d9 | 1613 | #ifdef HAVE_TARGET_32_LITTLE |
92e059d8 ILT |
1614 | template |
1615 | void | |
6fa2a40b | 1616 | Sized_relobj_file<32, false>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 1617 | #endif |
92e059d8 | 1618 | |
193a53d9 | 1619 | #ifdef HAVE_TARGET_32_BIG |
92e059d8 ILT |
1620 | template |
1621 | void | |
6fa2a40b | 1622 | Sized_relobj_file<32, true>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 1623 | #endif |
92e059d8 | 1624 | |
193a53d9 | 1625 | #ifdef HAVE_TARGET_64_LITTLE |
92e059d8 ILT |
1626 | template |
1627 | void | |
6fa2a40b | 1628 | Sized_relobj_file<64, false>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 1629 | #endif |
92e059d8 | 1630 | |
193a53d9 | 1631 | #ifdef HAVE_TARGET_64_BIG |
92e059d8 ILT |
1632 | template |
1633 | void | |
6fa2a40b | 1634 | Sized_relobj_file<64, true>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 1635 | #endif |
92e059d8 | 1636 | |
6d03d481 ST |
1637 | #ifdef HAVE_TARGET_32_LITTLE |
1638 | template | |
1639 | void | |
6fa2a40b CC |
1640 | Sized_relobj_file<32, false>::do_gc_process_relocs(Symbol_table* symtab, |
1641 | Layout* layout, | |
1642 | Read_relocs_data* rd); | |
6d03d481 ST |
1643 | #endif |
1644 | ||
1645 | #ifdef HAVE_TARGET_32_BIG | |
1646 | template | |
1647 | void | |
6fa2a40b CC |
1648 | Sized_relobj_file<32, true>::do_gc_process_relocs(Symbol_table* symtab, |
1649 | Layout* layout, | |
1650 | Read_relocs_data* rd); | |
6d03d481 ST |
1651 | #endif |
1652 | ||
1653 | #ifdef HAVE_TARGET_64_LITTLE | |
1654 | template | |
1655 | void | |
6fa2a40b CC |
1656 | Sized_relobj_file<64, false>::do_gc_process_relocs(Symbol_table* symtab, |
1657 | Layout* layout, | |
1658 | Read_relocs_data* rd); | |
6d03d481 ST |
1659 | #endif |
1660 | ||
1661 | #ifdef HAVE_TARGET_64_BIG | |
1662 | template | |
1663 | void | |
6fa2a40b CC |
1664 | Sized_relobj_file<64, true>::do_gc_process_relocs(Symbol_table* symtab, |
1665 | Layout* layout, | |
1666 | Read_relocs_data* rd); | |
6d03d481 ST |
1667 | #endif |
1668 | ||
193a53d9 | 1669 | #ifdef HAVE_TARGET_32_LITTLE |
92e059d8 ILT |
1670 | template |
1671 | void | |
6fa2a40b CC |
1672 | Sized_relobj_file<32, false>::do_scan_relocs(Symbol_table* symtab, |
1673 | Layout* layout, | |
1674 | Read_relocs_data* rd); | |
193a53d9 | 1675 | #endif |
92e059d8 | 1676 | |
193a53d9 | 1677 | #ifdef HAVE_TARGET_32_BIG |
92e059d8 ILT |
1678 | template |
1679 | void | |
6fa2a40b CC |
1680 | Sized_relobj_file<32, true>::do_scan_relocs(Symbol_table* symtab, |
1681 | Layout* layout, | |
1682 | Read_relocs_data* rd); | |
193a53d9 | 1683 | #endif |
92e059d8 | 1684 | |
193a53d9 | 1685 | #ifdef HAVE_TARGET_64_LITTLE |
92e059d8 ILT |
1686 | template |
1687 | void | |
6fa2a40b CC |
1688 | Sized_relobj_file<64, false>::do_scan_relocs(Symbol_table* symtab, |
1689 | Layout* layout, | |
1690 | Read_relocs_data* rd); | |
193a53d9 | 1691 | #endif |
92e059d8 | 1692 | |
193a53d9 | 1693 | #ifdef HAVE_TARGET_64_BIG |
92e059d8 ILT |
1694 | template |
1695 | void | |
6fa2a40b CC |
1696 | Sized_relobj_file<64, true>::do_scan_relocs(Symbol_table* symtab, |
1697 | Layout* layout, | |
1698 | Read_relocs_data* rd); | |
193a53d9 | 1699 | #endif |
92e059d8 | 1700 | |
193a53d9 | 1701 | #ifdef HAVE_TARGET_32_LITTLE |
61ba1cf9 ILT |
1702 | template |
1703 | void | |
6fa2a40b CC |
1704 | Sized_relobj_file<32, false>::do_relocate(const Symbol_table* symtab, |
1705 | const Layout* layout, | |
1706 | Output_file* of); | |
193a53d9 | 1707 | #endif |
61ba1cf9 | 1708 | |
193a53d9 | 1709 | #ifdef HAVE_TARGET_32_BIG |
61ba1cf9 ILT |
1710 | template |
1711 | void | |
6fa2a40b CC |
1712 | Sized_relobj_file<32, true>::do_relocate(const Symbol_table* symtab, |
1713 | const Layout* layout, | |
1714 | Output_file* of); | |
193a53d9 | 1715 | #endif |
61ba1cf9 | 1716 | |
193a53d9 | 1717 | #ifdef HAVE_TARGET_64_LITTLE |
61ba1cf9 ILT |
1718 | template |
1719 | void | |
6fa2a40b CC |
1720 | Sized_relobj_file<64, false>::do_relocate(const Symbol_table* symtab, |
1721 | const Layout* layout, | |
1722 | Output_file* of); | |
193a53d9 | 1723 | #endif |
61ba1cf9 | 1724 | |
193a53d9 | 1725 | #ifdef HAVE_TARGET_64_BIG |
61ba1cf9 ILT |
1726 | template |
1727 | void | |
6fa2a40b CC |
1728 | Sized_relobj_file<64, true>::do_relocate(const Symbol_table* symtab, |
1729 | const Layout* layout, | |
1730 | Output_file* of); | |
193a53d9 | 1731 | #endif |
61ba1cf9 | 1732 | |
72adc4fa DK |
1733 | #ifdef HAVE_TARGET_32_LITTLE |
1734 | template | |
1735 | void | |
6fa2a40b | 1736 | Sized_relobj_file<32, false>::do_relocate_sections( |
72adc4fa | 1737 | const Symbol_table* symtab, |
2ea97941 | 1738 | const Layout* layout, |
72adc4fa | 1739 | const unsigned char* pshdrs, |
09ec0418 | 1740 | Output_file* of, |
72adc4fa | 1741 | Views* pviews); |
6b2353a5 | 1742 | |
98461510 CC |
1743 | template |
1744 | void | |
1745 | Sized_relobj_file<32, false>::relocate_section_range( | |
1746 | const Symbol_table* symtab, | |
1747 | const Layout* layout, | |
1748 | const unsigned char* pshdrs, | |
1749 | Output_file* of, | |
1750 | Views* pviews, | |
1751 | unsigned int start_shndx, | |
1752 | unsigned int end_shndx); | |
1753 | ||
6b2353a5 | 1754 | template |
39040bb9 | 1755 | unsigned char* |
6b2353a5 CC |
1756 | Sized_relobj_file<32, false>::do_get_output_view( |
1757 | unsigned int shndx, | |
1758 | section_size_type* plen) const; | |
72adc4fa DK |
1759 | #endif |
1760 | ||
1761 | #ifdef HAVE_TARGET_32_BIG | |
1762 | template | |
1763 | void | |
6fa2a40b | 1764 | Sized_relobj_file<32, true>::do_relocate_sections( |
72adc4fa | 1765 | const Symbol_table* symtab, |
2ea97941 | 1766 | const Layout* layout, |
72adc4fa | 1767 | const unsigned char* pshdrs, |
09ec0418 | 1768 | Output_file* of, |
72adc4fa | 1769 | Views* pviews); |
6b2353a5 | 1770 | |
98461510 CC |
1771 | template |
1772 | void | |
1773 | Sized_relobj_file<32, true>::relocate_section_range( | |
1774 | const Symbol_table* symtab, | |
1775 | const Layout* layout, | |
1776 | const unsigned char* pshdrs, | |
1777 | Output_file* of, | |
1778 | Views* pviews, | |
1779 | unsigned int start_shndx, | |
1780 | unsigned int end_shndx); | |
1781 | ||
6b2353a5 | 1782 | template |
39040bb9 | 1783 | unsigned char* |
6b2353a5 CC |
1784 | Sized_relobj_file<32, true>::do_get_output_view( |
1785 | unsigned int shndx, | |
1786 | section_size_type* plen) const; | |
72adc4fa DK |
1787 | #endif |
1788 | ||
1789 | #ifdef HAVE_TARGET_64_LITTLE | |
1790 | template | |
1791 | void | |
6fa2a40b | 1792 | Sized_relobj_file<64, 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 | 1797 | Views* pviews); |
6b2353a5 | 1798 | |
98461510 CC |
1799 | template |
1800 | void | |
1801 | Sized_relobj_file<64, false>::relocate_section_range( | |
1802 | const Symbol_table* symtab, | |
1803 | const Layout* layout, | |
1804 | const unsigned char* pshdrs, | |
1805 | Output_file* of, | |
1806 | Views* pviews, | |
1807 | unsigned int start_shndx, | |
1808 | unsigned int end_shndx); | |
1809 | ||
6b2353a5 | 1810 | template |
39040bb9 | 1811 | unsigned char* |
6b2353a5 CC |
1812 | Sized_relobj_file<64, false>::do_get_output_view( |
1813 | unsigned int shndx, | |
1814 | section_size_type* plen) const; | |
72adc4fa DK |
1815 | #endif |
1816 | ||
1817 | #ifdef HAVE_TARGET_64_BIG | |
1818 | template | |
1819 | void | |
6fa2a40b | 1820 | Sized_relobj_file<64, true>::do_relocate_sections( |
72adc4fa DK |
1821 | const Symbol_table* symtab, |
1822 | const Layout* layout, | |
1823 | const unsigned char* pshdrs, | |
09ec0418 | 1824 | Output_file* of, |
72adc4fa | 1825 | Views* pviews); |
6b2353a5 | 1826 | |
98461510 CC |
1827 | template |
1828 | void | |
1829 | Sized_relobj_file<64, true>::relocate_section_range( | |
1830 | const Symbol_table* symtab, | |
1831 | const Layout* layout, | |
1832 | const unsigned char* pshdrs, | |
1833 | Output_file* of, | |
1834 | Views* pviews, | |
1835 | unsigned int start_shndx, | |
1836 | unsigned int end_shndx); | |
1837 | ||
6b2353a5 | 1838 | template |
39040bb9 | 1839 | unsigned char* |
6b2353a5 CC |
1840 | Sized_relobj_file<64, true>::do_get_output_view( |
1841 | unsigned int shndx, | |
1842 | section_size_type* plen) const; | |
72adc4fa DK |
1843 | #endif |
1844 | ||
3e4afc80 ILT |
1845 | #ifdef HAVE_TARGET_32_LITTLE |
1846 | template | |
1847 | void | |
6fa2a40b | 1848 | Sized_relobj_file<32, false>::initialize_input_to_output_maps(); |
3e4afc80 ILT |
1849 | |
1850 | template | |
1851 | void | |
6fa2a40b | 1852 | Sized_relobj_file<32, false>::free_input_to_output_maps(); |
3e4afc80 ILT |
1853 | #endif |
1854 | ||
1855 | #ifdef HAVE_TARGET_32_BIG | |
1856 | template | |
1857 | void | |
6fa2a40b | 1858 | Sized_relobj_file<32, true>::initialize_input_to_output_maps(); |
3e4afc80 ILT |
1859 | |
1860 | template | |
1861 | void | |
6fa2a40b | 1862 | Sized_relobj_file<32, true>::free_input_to_output_maps(); |
3e4afc80 ILT |
1863 | #endif |
1864 | ||
1865 | #ifdef HAVE_TARGET_64_LITTLE | |
1866 | template | |
1867 | void | |
6fa2a40b | 1868 | Sized_relobj_file<64, false>::initialize_input_to_output_maps(); |
3e4afc80 ILT |
1869 | |
1870 | template | |
1871 | void | |
6fa2a40b | 1872 | Sized_relobj_file<64, false>::free_input_to_output_maps(); |
3e4afc80 ILT |
1873 | #endif |
1874 | ||
1875 | #ifdef HAVE_TARGET_64_BIG | |
1876 | template | |
1877 | void | |
6fa2a40b | 1878 | Sized_relobj_file<64, true>::initialize_input_to_output_maps(); |
3e4afc80 ILT |
1879 | |
1880 | template | |
1881 | void | |
6fa2a40b | 1882 | Sized_relobj_file<64, true>::free_input_to_output_maps(); |
3e4afc80 ILT |
1883 | #endif |
1884 | ||
a9a60db6 ILT |
1885 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
1886 | template | |
1887 | class Merged_symbol_value<32>; | |
1888 | #endif | |
1889 | ||
1890 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
1891 | template | |
1892 | class Merged_symbol_value<64>; | |
1893 | #endif | |
1894 | ||
1895 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
1896 | template | |
1897 | class Symbol_value<32>; | |
1898 | #endif | |
1899 | ||
1900 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
1901 | template | |
1902 | class Symbol_value<64>; | |
1903 | #endif | |
1904 | ||
730cdc88 ILT |
1905 | #ifdef HAVE_TARGET_32_LITTLE |
1906 | template | |
1907 | class Track_relocs<32, false>; | |
1908 | #endif | |
1909 | ||
1910 | #ifdef HAVE_TARGET_32_BIG | |
1911 | template | |
1912 | class Track_relocs<32, true>; | |
1913 | #endif | |
1914 | ||
1915 | #ifdef HAVE_TARGET_64_LITTLE | |
1916 | template | |
1917 | class Track_relocs<64, false>; | |
1918 | #endif | |
1919 | ||
1920 | #ifdef HAVE_TARGET_64_BIG | |
1921 | template | |
1922 | class Track_relocs<64, true>; | |
1923 | #endif | |
1924 | ||
61ba1cf9 | 1925 | } // End namespace gold. |