Hash tables, dynamic section, i386 PLT, gold_assert.
[deliverable/binutils-gdb.git] / gold / reloc.cc
1 // reloc.cc -- relocate input files for gold.
2
3 #include "gold.h"
4
5 #include "workqueue.h"
6 #include "object.h"
7 #include "output.h"
8 #include "reloc.h"
9
10 namespace gold
11 {
12
13 // Read_relocs methods.
14
15 // These tasks just read the relocation information from the file.
16 // After reading it, the start another task to process the
17 // information. These tasks requires access to the file.
18
19 Task::Is_runnable_type
20 Read_relocs::is_runnable(Workqueue*)
21 {
22 return this->object_->is_locked() ? IS_LOCKED : IS_RUNNABLE;
23 }
24
25 // Lock the file.
26
27 Task_locker*
28 Read_relocs::locks(Workqueue*)
29 {
30 return new Task_locker_obj<Object>(*this->object_);
31 }
32
33 // Read the relocations and then start a Scan_relocs_task.
34
35 void
36 Read_relocs::run(Workqueue* workqueue)
37 {
38 Read_relocs_data *rd = new Read_relocs_data;
39 this->object_->read_relocs(rd);
40 workqueue->queue_front(new Scan_relocs(this->options_, this->symtab_,
41 this->layout_, this->object_, rd,
42 this->symtab_lock_, this->blocker_));
43 }
44
45 // Scan_relocs methods.
46
47 // These tasks scan the relocations read by Read_relocs and mark up
48 // the symbol table to indicate which relocations are required. We
49 // use a lock on the symbol table to keep them from interfering with
50 // each other.
51
52 Task::Is_runnable_type
53 Scan_relocs::is_runnable(Workqueue*)
54 {
55 if (!this->symtab_lock_->is_writable() || this->object_->is_locked())
56 return IS_LOCKED;
57 return IS_RUNNABLE;
58 }
59
60 // Return the locks we hold: one on the file, one on the symbol table
61 // and one blocker.
62
63 class Scan_relocs::Scan_relocs_locker : public Task_locker
64 {
65 public:
66 Scan_relocs_locker(Object* object, Task_token& symtab_lock, Task* task,
67 Task_token& blocker, Workqueue* workqueue)
68 : objlock_(*object), symtab_locker_(symtab_lock, task),
69 blocker_(blocker, workqueue)
70 { }
71
72 private:
73 Task_locker_obj<Object> objlock_;
74 Task_locker_write symtab_locker_;
75 Task_locker_block blocker_;
76 };
77
78 Task_locker*
79 Scan_relocs::locks(Workqueue* workqueue)
80 {
81 return new Scan_relocs_locker(this->object_, *this->symtab_lock_, this,
82 *this->blocker_, workqueue);
83 }
84
85 // Scan the relocs.
86
87 void
88 Scan_relocs::run(Workqueue*)
89 {
90 this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
91 this->rd_);
92 delete this->rd_;
93 this->rd_ = NULL;
94 }
95
96 // Relocate_task methods.
97
98 // These tasks are always runnable.
99
100 Task::Is_runnable_type
101 Relocate_task::is_runnable(Workqueue*)
102 {
103 return IS_RUNNABLE;
104 }
105
106 // We want to lock the file while we run. We want to unblock
107 // FINAL_BLOCKER when we are done.
108
109 class Relocate_task::Relocate_locker : public Task_locker
110 {
111 public:
112 Relocate_locker(Task_token& token, Workqueue* workqueue,
113 Object* object)
114 : blocker_(token, workqueue), objlock_(*object)
115 { }
116
117 private:
118 Task_locker_block blocker_;
119 Task_locker_obj<Object> objlock_;
120 };
121
122 Task_locker*
123 Relocate_task::locks(Workqueue* workqueue)
124 {
125 return new Relocate_locker(*this->final_blocker_, workqueue,
126 this->object_);
127 }
128
129 // Run the task.
130
131 void
132 Relocate_task::run(Workqueue*)
133 {
134 this->object_->relocate(this->options_, this->symtab_, this->layout_,
135 this->of_);
136 }
137
138 // Read the relocs and local symbols from the object file and store
139 // the information in RD.
140
141 template<int size, bool big_endian>
142 void
143 Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
144 {
145 rd->relocs.clear();
146
147 unsigned int shnum = this->shnum();
148 if (shnum == 0)
149 return;
150
151 rd->relocs.reserve(shnum / 2);
152
153 const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(),
154 shnum * This::shdr_size);
155 // Skip the first, dummy, section.
156 const unsigned char *ps = pshdrs + This::shdr_size;
157 for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
158 {
159 typename This::Shdr shdr(ps);
160
161 unsigned int sh_type = shdr.get_sh_type();
162 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
163 continue;
164
165 unsigned int shndx = shdr.get_sh_info();
166 if (shndx >= shnum)
167 {
168 fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"),
169 program_name, this->name().c_str(), i, shndx);
170 gold_exit(false);
171 }
172
173 if (!this->is_section_included(shndx))
174 continue;
175
176 // We are scanning relocations in order to fill out the GOT and
177 // PLT sections. Relocations for sections which are not
178 // allocated (typically debugging sections) should not add new
179 // GOT and PLT entries. So we skip them.
180 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
181 if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
182 continue;
183
184 if (shdr.get_sh_link() != this->symtab_shndx_)
185 {
186 fprintf(stderr,
187 _("%s: %s: relocation section %u uses unexpected "
188 "symbol table %u\n"),
189 program_name, this->name().c_str(), i, shdr.get_sh_link());
190 gold_exit(false);
191 }
192
193 off_t sh_size = shdr.get_sh_size();
194
195 unsigned int reloc_size;
196 if (sh_type == elfcpp::SHT_REL)
197 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
198 else
199 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
200 if (reloc_size != shdr.get_sh_entsize())
201 {
202 fprintf(stderr,
203 _("%s: %s: unexpected entsize for reloc section %u: "
204 "%lu != %u"),
205 program_name, this->name().c_str(), i,
206 static_cast<unsigned long>(shdr.get_sh_entsize()),
207 reloc_size);
208 gold_exit(false);
209 }
210
211 size_t reloc_count = sh_size / reloc_size;
212 if (reloc_count * reloc_size != sh_size)
213 {
214 fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"),
215 program_name, this->name().c_str(), i,
216 static_cast<unsigned long>(sh_size));
217 gold_exit(false);
218 }
219
220 rd->relocs.push_back(Section_relocs());
221 Section_relocs& sr(rd->relocs.back());
222 sr.reloc_shndx = i;
223 sr.data_shndx = shndx;
224 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size);
225 sr.sh_type = sh_type;
226 sr.reloc_count = reloc_count;
227 }
228
229 // Read the local symbols.
230 gold_assert(this->symtab_shndx_ != -1U);
231 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
232 rd->local_symbols = NULL;
233 else
234 {
235 typename This::Shdr symtabshdr(pshdrs
236 + this->symtab_shndx_ * This::shdr_size);
237 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
238 const int sym_size = This::sym_size;
239 const unsigned int loccount = this->local_symbol_count_;
240 gold_assert(loccount == symtabshdr.get_sh_info());
241 off_t locsize = loccount * sym_size;
242 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
243 locsize);
244 }
245 }
246
247 // Scan the relocs and adjust the symbol table. This looks for
248 // relocations which require GOT/PLT/COPY relocations.
249
250 template<int size, bool big_endian>
251 void
252 Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
253 Symbol_table* symtab,
254 Layout* layout,
255 Read_relocs_data* rd)
256 {
257 Sized_target<size, big_endian>* target = this->sized_target();
258
259 const unsigned char* local_symbols;
260 if (rd->local_symbols == NULL)
261 local_symbols = NULL;
262 else
263 local_symbols = rd->local_symbols->data();
264
265 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
266 p != rd->relocs.end();
267 ++p)
268 {
269 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
270 p->sh_type, p->contents->data(), p->reloc_count,
271 this->local_symbol_count_,
272 local_symbols,
273 this->symbols_);
274 delete p->contents;
275 p->contents = NULL;
276 }
277
278 if (rd->local_symbols != NULL)
279 {
280 delete rd->local_symbols;
281 rd->local_symbols = NULL;
282 }
283 }
284
285 // Relocate the input sections and write out the local symbols.
286
287 template<int size, bool big_endian>
288 void
289 Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
290 const Symbol_table* symtab,
291 const Layout* layout,
292 Output_file* of)
293 {
294 unsigned int shnum = this->shnum();
295
296 // Read the section headers.
297 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
298 shnum * This::shdr_size);
299
300 Views views;
301 views.resize(shnum);
302
303 // Make two passes over the sections. The first one copies the
304 // section data to the output file. The second one applies
305 // relocations.
306
307 this->write_sections(pshdrs, of, &views);
308
309 // Apply relocations.
310
311 this->relocate_sections(options, symtab, layout, pshdrs, &views);
312
313 // Write out the accumulated views.
314 for (unsigned int i = 1; i < shnum; ++i)
315 {
316 if (views[i].view != NULL)
317 of->write_output_view(views[i].offset, views[i].view_size,
318 views[i].view);
319 }
320
321 // Write out the local symbols.
322 this->write_local_symbols(of, layout->sympool());
323 }
324
325 // Write section data to the output file. PSHDRS points to the
326 // section headers. Record the views in *PVIEWS for use when
327 // relocating.
328
329 template<int size, bool big_endian>
330 void
331 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
332 Output_file* of,
333 Views* pviews)
334 {
335 unsigned int shnum = this->shnum();
336 std::vector<Map_to_output>& map_sections(this->map_to_output());
337
338 const unsigned char* p = pshdrs + This::shdr_size;
339 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
340 {
341 View_size* pvs = &(*pviews)[i];
342
343 pvs->view = NULL;
344
345 const Output_section* os = map_sections[i].output_section;
346 if (os == NULL)
347 continue;
348
349 typename This::Shdr shdr(p);
350
351 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
352 continue;
353
354 off_t start = os->offset() + map_sections[i].offset;
355 off_t sh_size = shdr.get_sh_size();
356
357 if (sh_size == 0)
358 continue;
359
360 gold_assert(map_sections[i].offset >= 0
361 && map_sections[i].offset + sh_size <= os->data_size());
362
363 unsigned char* view = of->get_output_view(start, sh_size);
364 this->read(shdr.get_sh_offset(), sh_size, view);
365
366 pvs->view = view;
367 pvs->address = os->address() + map_sections[i].offset;
368 pvs->offset = start;
369 pvs->view_size = sh_size;
370 }
371 }
372
373 // Relocate section data. VIEWS points to the section data as views
374 // in the output file.
375
376 template<int size, bool big_endian>
377 void
378 Sized_relobj<size, big_endian>::relocate_sections(
379 const General_options& options,
380 const Symbol_table* symtab,
381 const Layout* layout,
382 const unsigned char* pshdrs,
383 Views* pviews)
384 {
385 unsigned int shnum = this->shnum();
386 Sized_target<size, big_endian>* target = this->sized_target();
387
388 Relocate_info<size, big_endian> relinfo;
389 relinfo.options = &options;
390 relinfo.symtab = symtab;
391 relinfo.layout = layout;
392 relinfo.object = this;
393 relinfo.local_symbol_count = this->local_symbol_count_;
394 relinfo.local_values = &this->local_values_;
395 relinfo.symbols = this->symbols_;
396
397 const unsigned char* p = pshdrs + This::shdr_size;
398 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
399 {
400 typename This::Shdr shdr(p);
401
402 unsigned int sh_type = shdr.get_sh_type();
403 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
404 continue;
405
406 unsigned int index = shdr.get_sh_info();
407 if (index >= this->shnum())
408 {
409 fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"),
410 program_name, this->name().c_str(), i, index);
411 gold_exit(false);
412 }
413
414 if (!this->is_section_included(index))
415 {
416 // This relocation section is against a section which we
417 // discarded.
418 continue;
419 }
420
421 gold_assert((*pviews)[index].view != NULL);
422
423 if (shdr.get_sh_link() != this->symtab_shndx_)
424 {
425 fprintf(stderr,
426 _("%s: %s: relocation section %u uses unexpected "
427 "symbol table %u\n"),
428 program_name, this->name().c_str(), i, shdr.get_sh_link());
429 gold_exit(false);
430 }
431
432 off_t sh_size = shdr.get_sh_size();
433 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
434 sh_size);
435
436 unsigned int reloc_size;
437 if (sh_type == elfcpp::SHT_REL)
438 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
439 else
440 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
441
442 if (reloc_size != shdr.get_sh_entsize())
443 {
444 fprintf(stderr,
445 _("%s: %s: unexpected entsize for reloc section %u: "
446 "%lu != %u"),
447 program_name, this->name().c_str(), i,
448 static_cast<unsigned long>(shdr.get_sh_entsize()),
449 reloc_size);
450 gold_exit(false);
451 }
452
453 size_t reloc_count = sh_size / reloc_size;
454 if (reloc_count * reloc_size != sh_size)
455 {
456 fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"),
457 program_name, this->name().c_str(), i,
458 static_cast<unsigned long>(sh_size));
459 gold_exit(false);
460 }
461
462 relinfo.reloc_shndx = i;
463 relinfo.data_shndx = index;
464 target->relocate_section(&relinfo,
465 sh_type,
466 prelocs,
467 reloc_count,
468 (*pviews)[index].view,
469 (*pviews)[index].address,
470 (*pviews)[index].view_size);
471 }
472 }
473
474 // Relocate_functions functions.
475
476 // Return whether we need a COPY reloc for a relocation against GSYM.
477 // The relocation is being applied to section SHNDX in OBJECT.
478
479 template<int size, bool big_endian>
480 bool
481 Relocate_functions<size, big_endian>::need_copy_reloc(
482 const General_options*,
483 Relobj* object,
484 unsigned int shndx,
485 Symbol*)
486 {
487 // FIXME: Handle -z nocopyrelocs.
488
489 // If this is a readonly section, then we need a COPY reloc.
490 // Otherwise we can use a dynamic reloc.
491 if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
492 return true;
493
494 return false;
495 }
496
497 // Instantiate the templates we need. We could use the configure
498 // script to restrict this to only the ones for implemented targets.
499
500 template
501 void
502 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
503
504 template
505 void
506 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
507
508 template
509 void
510 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
511
512 template
513 void
514 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
515
516 template
517 void
518 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
519 Symbol_table* symtab,
520 Layout* layout,
521 Read_relocs_data* rd);
522
523 template
524 void
525 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
526 Symbol_table* symtab,
527 Layout* layout,
528 Read_relocs_data* rd);
529
530 template
531 void
532 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
533 Symbol_table* symtab,
534 Layout* layout,
535 Read_relocs_data* rd);
536
537 template
538 void
539 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
540 Symbol_table* symtab,
541 Layout* layout,
542 Read_relocs_data* rd);
543
544 template
545 void
546 Sized_relobj<32, false>::do_relocate(const General_options& options,
547 const Symbol_table* symtab,
548 const Layout* layout,
549 Output_file* of);
550
551 template
552 void
553 Sized_relobj<32, true>::do_relocate(const General_options& options,
554 const Symbol_table* symtab,
555 const Layout* layout,
556 Output_file* of);
557
558 template
559 void
560 Sized_relobj<64, false>::do_relocate(const General_options& options,
561 const Symbol_table* symtab,
562 const Layout* layout,
563 Output_file* of);
564
565 template
566 void
567 Sized_relobj<64, true>::do_relocate(const General_options& options,
568 const Symbol_table* symtab,
569 const Layout* layout,
570 Output_file* of);
571
572 template
573 bool
574 Relocate_functions<32, false>::need_copy_reloc(const General_options*,
575 Relobj*, unsigned int,
576 Symbol*);
577
578 template
579 bool
580 Relocate_functions<32, true>::need_copy_reloc(const General_options*,
581 Relobj*, unsigned int,
582 Symbol*);
583
584 template
585 bool
586 Relocate_functions<64, false>::need_copy_reloc(const General_options*,
587 Relobj*, unsigned int,
588 Symbol*);
589
590 template
591 bool
592 Relocate_functions<64, true>::need_copy_reloc(const General_options*,
593 Relobj*, unsigned int,
594 Symbol*);
595
596 } // End namespace gold.
This page took 0.051497 seconds and 5 git commands to generate.