Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // reloc.cc -- relocate input files for gold. |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
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 | ||
25 | #include "workqueue.h" | |
26 | #include "object.h" | |
5a6f7e2d | 27 | #include "symtab.h" |
61ba1cf9 ILT |
28 | #include "output.h" |
29 | #include "reloc.h" | |
30 | ||
31 | namespace gold | |
32 | { | |
33 | ||
92e059d8 ILT |
34 | // Read_relocs methods. |
35 | ||
36 | // These tasks just read the relocation information from the file. | |
37 | // After reading it, the start another task to process the | |
38 | // information. These tasks requires access to the file. | |
39 | ||
40 | Task::Is_runnable_type | |
41 | Read_relocs::is_runnable(Workqueue*) | |
42 | { | |
43 | return this->object_->is_locked() ? IS_LOCKED : IS_RUNNABLE; | |
44 | } | |
45 | ||
46 | // Lock the file. | |
47 | ||
48 | Task_locker* | |
49 | Read_relocs::locks(Workqueue*) | |
50 | { | |
51 | return new Task_locker_obj<Object>(*this->object_); | |
52 | } | |
53 | ||
54 | // Read the relocations and then start a Scan_relocs_task. | |
55 | ||
56 | void | |
57 | Read_relocs::run(Workqueue* workqueue) | |
58 | { | |
59 | Read_relocs_data *rd = new Read_relocs_data; | |
60 | this->object_->read_relocs(rd); | |
61 | workqueue->queue_front(new Scan_relocs(this->options_, this->symtab_, | |
ead1e424 ILT |
62 | this->layout_, this->object_, rd, |
63 | this->symtab_lock_, this->blocker_)); | |
92e059d8 ILT |
64 | } |
65 | ||
66 | // Scan_relocs methods. | |
67 | ||
68 | // These tasks scan the relocations read by Read_relocs and mark up | |
69 | // the symbol table to indicate which relocations are required. We | |
70 | // use a lock on the symbol table to keep them from interfering with | |
71 | // each other. | |
72 | ||
73 | Task::Is_runnable_type | |
74 | Scan_relocs::is_runnable(Workqueue*) | |
75 | { | |
ead1e424 ILT |
76 | if (!this->symtab_lock_->is_writable() || this->object_->is_locked()) |
77 | return IS_LOCKED; | |
78 | return IS_RUNNABLE; | |
92e059d8 ILT |
79 | } |
80 | ||
81 | // Return the locks we hold: one on the file, one on the symbol table | |
82 | // and one blocker. | |
83 | ||
84 | class Scan_relocs::Scan_relocs_locker : public Task_locker | |
85 | { | |
86 | public: | |
87 | Scan_relocs_locker(Object* object, Task_token& symtab_lock, Task* task, | |
88 | Task_token& blocker, Workqueue* workqueue) | |
89 | : objlock_(*object), symtab_locker_(symtab_lock, task), | |
90 | blocker_(blocker, workqueue) | |
91 | { } | |
92 | ||
93 | private: | |
94 | Task_locker_obj<Object> objlock_; | |
95 | Task_locker_write symtab_locker_; | |
96 | Task_locker_block blocker_; | |
97 | }; | |
98 | ||
99 | Task_locker* | |
100 | Scan_relocs::locks(Workqueue* workqueue) | |
101 | { | |
102 | return new Scan_relocs_locker(this->object_, *this->symtab_lock_, this, | |
103 | *this->blocker_, workqueue); | |
104 | } | |
105 | ||
106 | // Scan the relocs. | |
107 | ||
108 | void | |
109 | Scan_relocs::run(Workqueue*) | |
110 | { | |
ead1e424 ILT |
111 | this->object_->scan_relocs(this->options_, this->symtab_, this->layout_, |
112 | this->rd_); | |
92e059d8 ILT |
113 | delete this->rd_; |
114 | this->rd_ = NULL; | |
115 | } | |
116 | ||
61ba1cf9 ILT |
117 | // Relocate_task methods. |
118 | ||
119 | // These tasks are always runnable. | |
120 | ||
121 | Task::Is_runnable_type | |
122 | Relocate_task::is_runnable(Workqueue*) | |
123 | { | |
124 | return IS_RUNNABLE; | |
125 | } | |
126 | ||
127 | // We want to lock the file while we run. We want to unblock | |
128 | // FINAL_BLOCKER when we are done. | |
129 | ||
130 | class Relocate_task::Relocate_locker : public Task_locker | |
131 | { | |
132 | public: | |
133 | Relocate_locker(Task_token& token, Workqueue* workqueue, | |
134 | Object* object) | |
135 | : blocker_(token, workqueue), objlock_(*object) | |
136 | { } | |
137 | ||
138 | private: | |
139 | Task_locker_block blocker_; | |
140 | Task_locker_obj<Object> objlock_; | |
141 | }; | |
142 | ||
143 | Task_locker* | |
144 | Relocate_task::locks(Workqueue* workqueue) | |
145 | { | |
146 | return new Relocate_locker(*this->final_blocker_, workqueue, | |
147 | this->object_); | |
148 | } | |
149 | ||
150 | // Run the task. | |
151 | ||
152 | void | |
153 | Relocate_task::run(Workqueue*) | |
154 | { | |
92e059d8 | 155 | this->object_->relocate(this->options_, this->symtab_, this->layout_, |
61ba1cf9 ILT |
156 | this->of_); |
157 | } | |
158 | ||
92e059d8 ILT |
159 | // Read the relocs and local symbols from the object file and store |
160 | // the information in RD. | |
161 | ||
162 | template<int size, bool big_endian> | |
163 | void | |
f6ce93d6 | 164 | Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd) |
92e059d8 ILT |
165 | { |
166 | rd->relocs.clear(); | |
167 | ||
168 | unsigned int shnum = this->shnum(); | |
169 | if (shnum == 0) | |
170 | return; | |
171 | ||
172 | rd->relocs.reserve(shnum / 2); | |
173 | ||
645f8123 | 174 | const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(), |
92e059d8 ILT |
175 | shnum * This::shdr_size); |
176 | // Skip the first, dummy, section. | |
177 | const unsigned char *ps = pshdrs + This::shdr_size; | |
178 | for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size) | |
179 | { | |
180 | typename This::Shdr shdr(ps); | |
181 | ||
182 | unsigned int sh_type = shdr.get_sh_type(); | |
183 | if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) | |
184 | continue; | |
185 | ||
186 | unsigned int shndx = shdr.get_sh_info(); | |
187 | if (shndx >= shnum) | |
188 | { | |
189 | fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"), | |
190 | program_name, this->name().c_str(), i, shndx); | |
191 | gold_exit(false); | |
192 | } | |
193 | ||
194 | if (!this->is_section_included(shndx)) | |
195 | continue; | |
196 | ||
ead1e424 ILT |
197 | // We are scanning relocations in order to fill out the GOT and |
198 | // PLT sections. Relocations for sections which are not | |
199 | // allocated (typically debugging sections) should not add new | |
200 | // GOT and PLT entries. So we skip them. | |
201 | typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size); | |
202 | if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) | |
203 | continue; | |
204 | ||
645f8123 | 205 | if (shdr.get_sh_link() != this->symtab_shndx_) |
92e059d8 ILT |
206 | { |
207 | fprintf(stderr, | |
208 | _("%s: %s: relocation section %u uses unexpected " | |
209 | "symbol table %u\n"), | |
210 | program_name, this->name().c_str(), i, shdr.get_sh_link()); | |
211 | gold_exit(false); | |
212 | } | |
213 | ||
214 | off_t sh_size = shdr.get_sh_size(); | |
215 | ||
216 | unsigned int reloc_size; | |
217 | if (sh_type == elfcpp::SHT_REL) | |
218 | reloc_size = elfcpp::Elf_sizes<size>::rel_size; | |
219 | else | |
220 | reloc_size = elfcpp::Elf_sizes<size>::rela_size; | |
221 | if (reloc_size != shdr.get_sh_entsize()) | |
222 | { | |
223 | fprintf(stderr, | |
224 | _("%s: %s: unexpected entsize for reloc section %u: " | |
225 | "%lu != %u"), | |
226 | program_name, this->name().c_str(), i, | |
227 | static_cast<unsigned long>(shdr.get_sh_entsize()), | |
228 | reloc_size); | |
229 | gold_exit(false); | |
230 | } | |
231 | ||
232 | size_t reloc_count = sh_size / reloc_size; | |
233 | if (reloc_count * reloc_size != sh_size) | |
234 | { | |
235 | fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"), | |
236 | program_name, this->name().c_str(), i, | |
237 | static_cast<unsigned long>(sh_size)); | |
238 | gold_exit(false); | |
239 | } | |
240 | ||
241 | rd->relocs.push_back(Section_relocs()); | |
242 | Section_relocs& sr(rd->relocs.back()); | |
243 | sr.reloc_shndx = i; | |
244 | sr.data_shndx = shndx; | |
245 | sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size); | |
246 | sr.sh_type = sh_type; | |
247 | sr.reloc_count = reloc_count; | |
248 | } | |
249 | ||
250 | // Read the local symbols. | |
a3ad94ed | 251 | gold_assert(this->symtab_shndx_ != -1U); |
645f8123 | 252 | if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0) |
92e059d8 ILT |
253 | rd->local_symbols = NULL; |
254 | else | |
255 | { | |
256 | typename This::Shdr symtabshdr(pshdrs | |
645f8123 | 257 | + this->symtab_shndx_ * This::shdr_size); |
a3ad94ed | 258 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
92e059d8 ILT |
259 | const int sym_size = This::sym_size; |
260 | const unsigned int loccount = this->local_symbol_count_; | |
a3ad94ed | 261 | gold_assert(loccount == symtabshdr.get_sh_info()); |
92e059d8 ILT |
262 | off_t locsize = loccount * sym_size; |
263 | rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(), | |
264 | locsize); | |
265 | } | |
266 | } | |
267 | ||
268 | // Scan the relocs and adjust the symbol table. This looks for | |
269 | // relocations which require GOT/PLT/COPY relocations. | |
270 | ||
271 | template<int size, bool big_endian> | |
272 | void | |
f6ce93d6 | 273 | Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options, |
92e059d8 | 274 | Symbol_table* symtab, |
ead1e424 | 275 | Layout* layout, |
92e059d8 ILT |
276 | Read_relocs_data* rd) |
277 | { | |
278 | Sized_target<size, big_endian>* target = this->sized_target(); | |
279 | ||
280 | const unsigned char* local_symbols; | |
281 | if (rd->local_symbols == NULL) | |
282 | local_symbols = NULL; | |
283 | else | |
284 | local_symbols = rd->local_symbols->data(); | |
285 | ||
286 | for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin(); | |
287 | p != rd->relocs.end(); | |
288 | ++p) | |
289 | { | |
a3ad94ed ILT |
290 | target->scan_relocs(options, symtab, layout, this, p->data_shndx, |
291 | p->sh_type, p->contents->data(), p->reloc_count, | |
92e059d8 ILT |
292 | this->local_symbol_count_, |
293 | local_symbols, | |
294 | this->symbols_); | |
295 | delete p->contents; | |
296 | p->contents = NULL; | |
297 | } | |
298 | ||
299 | if (rd->local_symbols != NULL) | |
300 | { | |
301 | delete rd->local_symbols; | |
302 | rd->local_symbols = NULL; | |
303 | } | |
304 | } | |
305 | ||
61ba1cf9 ILT |
306 | // Relocate the input sections and write out the local symbols. |
307 | ||
308 | template<int size, bool big_endian> | |
309 | void | |
f6ce93d6 | 310 | Sized_relobj<size, big_endian>::do_relocate(const General_options& options, |
61ba1cf9 | 311 | const Symbol_table* symtab, |
92e059d8 | 312 | const Layout* layout, |
61ba1cf9 ILT |
313 | Output_file* of) |
314 | { | |
315 | unsigned int shnum = this->shnum(); | |
316 | ||
317 | // Read the section headers. | |
645f8123 | 318 | const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(), |
61ba1cf9 ILT |
319 | shnum * This::shdr_size); |
320 | ||
321 | Views views; | |
322 | views.resize(shnum); | |
323 | ||
324 | // Make two passes over the sections. The first one copies the | |
325 | // section data to the output file. The second one applies | |
326 | // relocations. | |
327 | ||
328 | this->write_sections(pshdrs, of, &views); | |
329 | ||
330 | // Apply relocations. | |
331 | ||
92e059d8 | 332 | this->relocate_sections(options, symtab, layout, pshdrs, &views); |
61ba1cf9 ILT |
333 | |
334 | // Write out the accumulated views. | |
335 | for (unsigned int i = 1; i < shnum; ++i) | |
336 | { | |
337 | if (views[i].view != NULL) | |
338 | of->write_output_view(views[i].offset, views[i].view_size, | |
339 | views[i].view); | |
340 | } | |
341 | ||
342 | // Write out the local symbols. | |
92e059d8 | 343 | this->write_local_symbols(of, layout->sympool()); |
61ba1cf9 ILT |
344 | } |
345 | ||
346 | // Write section data to the output file. PSHDRS points to the | |
347 | // section headers. Record the views in *PVIEWS for use when | |
348 | // relocating. | |
349 | ||
350 | template<int size, bool big_endian> | |
351 | void | |
f6ce93d6 | 352 | Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs, |
61ba1cf9 ILT |
353 | Output_file* of, |
354 | Views* pviews) | |
355 | { | |
356 | unsigned int shnum = this->shnum(); | |
357 | std::vector<Map_to_output>& map_sections(this->map_to_output()); | |
358 | ||
359 | const unsigned char* p = pshdrs + This::shdr_size; | |
360 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) | |
361 | { | |
362 | View_size* pvs = &(*pviews)[i]; | |
363 | ||
364 | pvs->view = NULL; | |
365 | ||
b8e6aad9 ILT |
366 | if (map_sections[i].offset == -1) |
367 | continue; | |
368 | ||
61ba1cf9 ILT |
369 | const Output_section* os = map_sections[i].output_section; |
370 | if (os == NULL) | |
371 | continue; | |
372 | ||
373 | typename This::Shdr shdr(p); | |
374 | ||
375 | if (shdr.get_sh_type() == elfcpp::SHT_NOBITS) | |
376 | continue; | |
377 | ||
61ba1cf9 ILT |
378 | off_t start = os->offset() + map_sections[i].offset; |
379 | off_t sh_size = shdr.get_sh_size(); | |
380 | ||
ead1e424 ILT |
381 | if (sh_size == 0) |
382 | continue; | |
383 | ||
a3ad94ed ILT |
384 | gold_assert(map_sections[i].offset >= 0 |
385 | && map_sections[i].offset + sh_size <= os->data_size()); | |
ead1e424 | 386 | |
61ba1cf9 | 387 | unsigned char* view = of->get_output_view(start, sh_size); |
92e059d8 ILT |
388 | this->read(shdr.get_sh_offset(), sh_size, view); |
389 | ||
61ba1cf9 ILT |
390 | pvs->view = view; |
391 | pvs->address = os->address() + map_sections[i].offset; | |
392 | pvs->offset = start; | |
393 | pvs->view_size = sh_size; | |
394 | } | |
395 | } | |
396 | ||
397 | // Relocate section data. VIEWS points to the section data as views | |
398 | // in the output file. | |
399 | ||
400 | template<int size, bool big_endian> | |
401 | void | |
f6ce93d6 | 402 | Sized_relobj<size, big_endian>::relocate_sections( |
92e059d8 ILT |
403 | const General_options& options, |
404 | const Symbol_table* symtab, | |
405 | const Layout* layout, | |
406 | const unsigned char* pshdrs, | |
407 | Views* pviews) | |
61ba1cf9 ILT |
408 | { |
409 | unsigned int shnum = this->shnum(); | |
61ba1cf9 ILT |
410 | Sized_target<size, big_endian>* target = this->sized_target(); |
411 | ||
92e059d8 ILT |
412 | Relocate_info<size, big_endian> relinfo; |
413 | relinfo.options = &options; | |
414 | relinfo.symtab = symtab; | |
415 | relinfo.layout = layout; | |
416 | relinfo.object = this; | |
417 | relinfo.local_symbol_count = this->local_symbol_count_; | |
c06b7b0b | 418 | relinfo.local_values = &this->local_values_; |
92e059d8 ILT |
419 | relinfo.symbols = this->symbols_; |
420 | ||
61ba1cf9 ILT |
421 | const unsigned char* p = pshdrs + This::shdr_size; |
422 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) | |
423 | { | |
424 | typename This::Shdr shdr(p); | |
425 | ||
426 | unsigned int sh_type = shdr.get_sh_type(); | |
427 | if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) | |
428 | continue; | |
429 | ||
430 | unsigned int index = shdr.get_sh_info(); | |
431 | if (index >= this->shnum()) | |
432 | { | |
433 | fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"), | |
434 | program_name, this->name().c_str(), i, index); | |
435 | gold_exit(false); | |
436 | } | |
437 | ||
92e059d8 | 438 | if (!this->is_section_included(index)) |
61ba1cf9 ILT |
439 | { |
440 | // This relocation section is against a section which we | |
441 | // discarded. | |
442 | continue; | |
443 | } | |
444 | ||
a3ad94ed | 445 | gold_assert((*pviews)[index].view != NULL); |
61ba1cf9 | 446 | |
645f8123 | 447 | if (shdr.get_sh_link() != this->symtab_shndx_) |
61ba1cf9 ILT |
448 | { |
449 | fprintf(stderr, | |
450 | _("%s: %s: relocation section %u uses unexpected " | |
451 | "symbol table %u\n"), | |
452 | program_name, this->name().c_str(), i, shdr.get_sh_link()); | |
453 | gold_exit(false); | |
454 | } | |
455 | ||
456 | off_t sh_size = shdr.get_sh_size(); | |
457 | const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(), | |
458 | sh_size); | |
459 | ||
460 | unsigned int reloc_size; | |
461 | if (sh_type == elfcpp::SHT_REL) | |
462 | reloc_size = elfcpp::Elf_sizes<size>::rel_size; | |
463 | else | |
464 | reloc_size = elfcpp::Elf_sizes<size>::rela_size; | |
465 | ||
466 | if (reloc_size != shdr.get_sh_entsize()) | |
467 | { | |
468 | fprintf(stderr, | |
469 | _("%s: %s: unexpected entsize for reloc section %u: " | |
470 | "%lu != %u"), | |
471 | program_name, this->name().c_str(), i, | |
472 | static_cast<unsigned long>(shdr.get_sh_entsize()), | |
473 | reloc_size); | |
474 | gold_exit(false); | |
475 | } | |
476 | ||
477 | size_t reloc_count = sh_size / reloc_size; | |
478 | if (reloc_count * reloc_size != sh_size) | |
479 | { | |
480 | fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"), | |
481 | program_name, this->name().c_str(), i, | |
482 | static_cast<unsigned long>(sh_size)); | |
483 | gold_exit(false); | |
484 | } | |
485 | ||
92e059d8 ILT |
486 | relinfo.reloc_shndx = i; |
487 | relinfo.data_shndx = index; | |
488 | target->relocate_section(&relinfo, | |
489 | sh_type, | |
490 | prelocs, | |
491 | reloc_count, | |
61ba1cf9 ILT |
492 | (*pviews)[index].view, |
493 | (*pviews)[index].address, | |
494 | (*pviews)[index].view_size); | |
495 | } | |
496 | } | |
497 | ||
5a6f7e2d ILT |
498 | // Copy_relocs::Copy_reloc_entry methods. |
499 | ||
500 | // Return whether we should emit this reloc. We should emit it if the | |
501 | // symbol is still defined in a dynamic object. If we should not emit | |
502 | // it, we clear it, to save ourselves the test next time. | |
503 | ||
504 | template<int size, bool big_endian> | |
505 | bool | |
506 | Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit() | |
507 | { | |
508 | if (this->sym_ == NULL) | |
509 | return false; | |
14b31740 | 510 | if (this->sym_->is_from_dynobj()) |
5a6f7e2d ILT |
511 | return true; |
512 | this->sym_ = NULL; | |
513 | return false; | |
514 | } | |
515 | ||
516 | // Emit a reloc into a SHT_REL section. | |
517 | ||
518 | template<int size, bool big_endian> | |
519 | void | |
520 | Copy_relocs<size, big_endian>::Copy_reloc_entry::emit( | |
521 | Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data) | |
522 | { | |
16649710 | 523 | this->sym_->set_needs_dynsym_entry(); |
5a6f7e2d ILT |
524 | reloc_data->add_global(this->sym_, this->reloc_type_, this->relobj_, |
525 | this->shndx_, this->address_); | |
526 | } | |
527 | ||
528 | // Emit a reloc into a SHT_RELA section. | |
529 | ||
530 | template<int size, bool big_endian> | |
531 | void | |
532 | Copy_relocs<size, big_endian>::Copy_reloc_entry::emit( | |
533 | Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data) | |
534 | { | |
16649710 | 535 | this->sym_->set_needs_dynsym_entry(); |
5a6f7e2d ILT |
536 | reloc_data->add_global(this->sym_, this->reloc_type_, this->relobj_, |
537 | this->shndx_, this->address_, this->addend_); | |
538 | } | |
539 | ||
540 | // Copy_relocs methods. | |
a3ad94ed ILT |
541 | |
542 | // Return whether we need a COPY reloc for a relocation against GSYM. | |
543 | // The relocation is being applied to section SHNDX in OBJECT. | |
544 | ||
545 | template<int size, bool big_endian> | |
546 | bool | |
5a6f7e2d | 547 | Copy_relocs<size, big_endian>::need_copy_reloc( |
a3ad94ed ILT |
548 | const General_options*, |
549 | Relobj* object, | |
550 | unsigned int shndx, | |
5a6f7e2d | 551 | Sized_symbol<size>* sym) |
a3ad94ed ILT |
552 | { |
553 | // FIXME: Handle -z nocopyrelocs. | |
554 | ||
5a6f7e2d ILT |
555 | if (sym->symsize() == 0) |
556 | return false; | |
557 | ||
a3ad94ed ILT |
558 | // If this is a readonly section, then we need a COPY reloc. |
559 | // Otherwise we can use a dynamic reloc. | |
560 | if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0) | |
561 | return true; | |
562 | ||
563 | return false; | |
564 | } | |
565 | ||
5a6f7e2d ILT |
566 | // Save a Rel reloc. |
567 | ||
568 | template<int size, bool big_endian> | |
569 | void | |
570 | Copy_relocs<size, big_endian>::save( | |
571 | Symbol* sym, | |
572 | Relobj* relobj, | |
573 | unsigned int shndx, | |
574 | const elfcpp::Rel<size, big_endian>& rel) | |
575 | { | |
576 | unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info()); | |
577 | this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx, | |
578 | rel.get_r_offset(), 0)); | |
579 | } | |
580 | ||
581 | // Save a Rela reloc. | |
582 | ||
583 | template<int size, bool big_endian> | |
584 | void | |
585 | Copy_relocs<size, big_endian>::save( | |
586 | Symbol* sym, | |
587 | Relobj* relobj, | |
588 | unsigned int shndx, | |
589 | const elfcpp::Rela<size, big_endian>& rela) | |
590 | { | |
591 | unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info()); | |
592 | this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx, | |
593 | rela.get_r_offset(), | |
594 | rela.get_r_addend())); | |
595 | } | |
596 | ||
597 | // Return whether there are any relocs to emit. We don't want to emit | |
598 | // a reloc if the symbol is no longer defined in a dynamic object. | |
599 | ||
600 | template<int size, bool big_endian> | |
601 | bool | |
602 | Copy_relocs<size, big_endian>::any_to_emit() | |
603 | { | |
604 | for (typename Copy_reloc_entries::iterator p = this->entries_.begin(); | |
605 | p != this->entries_.end(); | |
606 | ++p) | |
607 | { | |
608 | if (p->should_emit()) | |
609 | return true; | |
610 | } | |
611 | return false; | |
612 | } | |
613 | ||
614 | // Emit relocs. | |
615 | ||
616 | template<int size, bool big_endian> | |
617 | template<int sh_type> | |
618 | void | |
619 | Copy_relocs<size, big_endian>::emit( | |
620 | Output_data_reloc<sh_type, true, size, big_endian>* reloc_data) | |
621 | { | |
622 | for (typename Copy_reloc_entries::iterator p = this->entries_.begin(); | |
623 | p != this->entries_.end(); | |
624 | ++p) | |
625 | { | |
626 | if (p->should_emit()) | |
627 | p->emit(reloc_data); | |
628 | } | |
629 | } | |
630 | ||
61ba1cf9 ILT |
631 | // Instantiate the templates we need. We could use the configure |
632 | // script to restrict this to only the ones for implemented targets. | |
633 | ||
193a53d9 | 634 | #ifdef HAVE_TARGET_32_LITTLE |
92e059d8 ILT |
635 | template |
636 | void | |
f6ce93d6 | 637 | Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 638 | #endif |
92e059d8 | 639 | |
193a53d9 | 640 | #ifdef HAVE_TARGET_32_BIG |
92e059d8 ILT |
641 | template |
642 | void | |
f6ce93d6 | 643 | Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 644 | #endif |
92e059d8 | 645 | |
193a53d9 | 646 | #ifdef HAVE_TARGET_64_LITTLE |
92e059d8 ILT |
647 | template |
648 | void | |
f6ce93d6 | 649 | Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 650 | #endif |
92e059d8 | 651 | |
193a53d9 | 652 | #ifdef HAVE_TARGET_64_BIG |
92e059d8 ILT |
653 | template |
654 | void | |
f6ce93d6 | 655 | Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 656 | #endif |
92e059d8 | 657 | |
193a53d9 | 658 | #ifdef HAVE_TARGET_32_LITTLE |
92e059d8 ILT |
659 | template |
660 | void | |
f6ce93d6 | 661 | Sized_relobj<32, false>::do_scan_relocs(const General_options& options, |
92e059d8 | 662 | Symbol_table* symtab, |
ead1e424 | 663 | Layout* layout, |
92e059d8 | 664 | Read_relocs_data* rd); |
193a53d9 | 665 | #endif |
92e059d8 | 666 | |
193a53d9 | 667 | #ifdef HAVE_TARGET_32_BIG |
92e059d8 ILT |
668 | template |
669 | void | |
f6ce93d6 | 670 | Sized_relobj<32, true>::do_scan_relocs(const General_options& options, |
92e059d8 | 671 | Symbol_table* symtab, |
ead1e424 | 672 | Layout* layout, |
92e059d8 | 673 | Read_relocs_data* rd); |
193a53d9 | 674 | #endif |
92e059d8 | 675 | |
193a53d9 | 676 | #ifdef HAVE_TARGET_64_LITTLE |
92e059d8 ILT |
677 | template |
678 | void | |
f6ce93d6 | 679 | Sized_relobj<64, false>::do_scan_relocs(const General_options& options, |
92e059d8 | 680 | Symbol_table* symtab, |
ead1e424 | 681 | Layout* layout, |
92e059d8 | 682 | Read_relocs_data* rd); |
193a53d9 | 683 | #endif |
92e059d8 | 684 | |
193a53d9 | 685 | #ifdef HAVE_TARGET_64_BIG |
92e059d8 ILT |
686 | template |
687 | void | |
f6ce93d6 | 688 | Sized_relobj<64, true>::do_scan_relocs(const General_options& options, |
92e059d8 | 689 | Symbol_table* symtab, |
ead1e424 | 690 | Layout* layout, |
92e059d8 | 691 | Read_relocs_data* rd); |
193a53d9 | 692 | #endif |
92e059d8 | 693 | |
193a53d9 | 694 | #ifdef HAVE_TARGET_32_LITTLE |
61ba1cf9 ILT |
695 | template |
696 | void | |
f6ce93d6 | 697 | Sized_relobj<32, false>::do_relocate(const General_options& options, |
61ba1cf9 | 698 | const Symbol_table* symtab, |
92e059d8 | 699 | const Layout* layout, |
61ba1cf9 | 700 | Output_file* of); |
193a53d9 | 701 | #endif |
61ba1cf9 | 702 | |
193a53d9 | 703 | #ifdef HAVE_TARGET_32_BIG |
61ba1cf9 ILT |
704 | template |
705 | void | |
f6ce93d6 | 706 | Sized_relobj<32, true>::do_relocate(const General_options& options, |
61ba1cf9 | 707 | const Symbol_table* symtab, |
92e059d8 | 708 | const Layout* layout, |
61ba1cf9 | 709 | Output_file* of); |
193a53d9 | 710 | #endif |
61ba1cf9 | 711 | |
193a53d9 | 712 | #ifdef HAVE_TARGET_64_LITTLE |
61ba1cf9 ILT |
713 | template |
714 | void | |
f6ce93d6 | 715 | Sized_relobj<64, false>::do_relocate(const General_options& options, |
61ba1cf9 | 716 | const Symbol_table* symtab, |
92e059d8 | 717 | const Layout* layout, |
61ba1cf9 | 718 | Output_file* of); |
193a53d9 | 719 | #endif |
61ba1cf9 | 720 | |
193a53d9 | 721 | #ifdef HAVE_TARGET_64_BIG |
61ba1cf9 ILT |
722 | template |
723 | void | |
f6ce93d6 | 724 | Sized_relobj<64, true>::do_relocate(const General_options& options, |
61ba1cf9 | 725 | const Symbol_table* symtab, |
92e059d8 | 726 | const Layout* layout, |
61ba1cf9 | 727 | Output_file* of); |
193a53d9 | 728 | #endif |
61ba1cf9 | 729 | |
193a53d9 | 730 | #ifdef HAVE_TARGET_32_LITTLE |
a3ad94ed | 731 | template |
5a6f7e2d | 732 | class Copy_relocs<32, false>; |
193a53d9 | 733 | #endif |
a3ad94ed | 734 | |
193a53d9 | 735 | #ifdef HAVE_TARGET_32_BIG |
a3ad94ed | 736 | template |
5a6f7e2d | 737 | class Copy_relocs<32, true>; |
193a53d9 | 738 | #endif |
a3ad94ed | 739 | |
193a53d9 | 740 | #ifdef HAVE_TARGET_64_LITTLE |
a3ad94ed | 741 | template |
5a6f7e2d | 742 | class Copy_relocs<64, false>; |
193a53d9 | 743 | #endif |
a3ad94ed | 744 | |
193a53d9 | 745 | #ifdef HAVE_TARGET_64_BIG |
a3ad94ed | 746 | template |
5a6f7e2d | 747 | class Copy_relocs<64, true>; |
193a53d9 | 748 | #endif |
5a6f7e2d | 749 | |
193a53d9 | 750 | #ifdef HAVE_TARGET_32_LITTLE |
5a6f7e2d ILT |
751 | template |
752 | void | |
753 | Copy_relocs<32, false>::emit<elfcpp::SHT_REL>( | |
754 | Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*); | |
193a53d9 | 755 | #endif |
5a6f7e2d | 756 | |
193a53d9 | 757 | #ifdef HAVE_TARGET_32_BIG |
5a6f7e2d ILT |
758 | template |
759 | void | |
760 | Copy_relocs<32, true>::emit<elfcpp::SHT_REL>( | |
761 | Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*); | |
193a53d9 | 762 | #endif |
5a6f7e2d | 763 | |
193a53d9 | 764 | #ifdef HAVE_TARGET_64_LITTLE |
5a6f7e2d ILT |
765 | template |
766 | void | |
767 | Copy_relocs<64, false>::emit<elfcpp::SHT_REL>( | |
768 | Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*); | |
193a53d9 | 769 | #endif |
5a6f7e2d | 770 | |
193a53d9 | 771 | #ifdef HAVE_TARGET_64_BIG |
5a6f7e2d ILT |
772 | template |
773 | void | |
774 | Copy_relocs<64, true>::emit<elfcpp::SHT_REL>( | |
775 | Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*); | |
193a53d9 | 776 | #endif |
5a6f7e2d | 777 | |
193a53d9 | 778 | #ifdef HAVE_TARGET_32_LITTLE |
5a6f7e2d ILT |
779 | template |
780 | void | |
781 | Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>( | |
782 | Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*); | |
193a53d9 | 783 | #endif |
5a6f7e2d | 784 | |
193a53d9 | 785 | #ifdef HAVE_TARGET_32_BIG |
5a6f7e2d ILT |
786 | template |
787 | void | |
788 | Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>( | |
789 | Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*); | |
193a53d9 | 790 | #endif |
5a6f7e2d | 791 | |
193a53d9 | 792 | #ifdef HAVE_TARGET_64_LITTLE |
5a6f7e2d ILT |
793 | template |
794 | void | |
795 | Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>( | |
796 | Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*); | |
193a53d9 | 797 | #endif |
5a6f7e2d | 798 | |
193a53d9 | 799 | #ifdef HAVE_TARGET_64_BIG |
5a6f7e2d ILT |
800 | template |
801 | void | |
802 | Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>( | |
803 | Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*); | |
193a53d9 | 804 | #endif |
61ba1cf9 ILT |
805 | |
806 | } // End namespace gold. |