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