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