Hash tables, dynamic section, i386 PLT, gold_assert.
[deliverable/binutils-gdb.git] / gold / reloc.cc
CommitLineData
61ba1cf9
ILT
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
10namespace gold
11{
12
92e059d8
ILT
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
19Task::Is_runnable_type
20Read_relocs::is_runnable(Workqueue*)
21{
22 return this->object_->is_locked() ? IS_LOCKED : IS_RUNNABLE;
23}
24
25// Lock the file.
26
27Task_locker*
28Read_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
35void
36Read_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_,
ead1e424
ILT
41 this->layout_, this->object_, rd,
42 this->symtab_lock_, this->blocker_));
92e059d8
ILT
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
52Task::Is_runnable_type
53Scan_relocs::is_runnable(Workqueue*)
54{
ead1e424
ILT
55 if (!this->symtab_lock_->is_writable() || this->object_->is_locked())
56 return IS_LOCKED;
57 return IS_RUNNABLE;
92e059d8
ILT
58}
59
60// Return the locks we hold: one on the file, one on the symbol table
61// and one blocker.
62
63class 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
78Task_locker*
79Scan_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
87void
88Scan_relocs::run(Workqueue*)
89{
ead1e424
ILT
90 this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
91 this->rd_);
92e059d8
ILT
92 delete this->rd_;
93 this->rd_ = NULL;
94}
95
61ba1cf9
ILT
96// Relocate_task methods.
97
98// These tasks are always runnable.
99
100Task::Is_runnable_type
101Relocate_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
109class 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
122Task_locker*
123Relocate_task::locks(Workqueue* workqueue)
124{
125 return new Relocate_locker(*this->final_blocker_, workqueue,
126 this->object_);
127}
128
129// Run the task.
130
131void
132Relocate_task::run(Workqueue*)
133{
92e059d8 134 this->object_->relocate(this->options_, this->symtab_, this->layout_,
61ba1cf9
ILT
135 this->of_);
136}
137
92e059d8
ILT
138// Read the relocs and local symbols from the object file and store
139// the information in RD.
140
141template<int size, bool big_endian>
142void
f6ce93d6 143Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
92e059d8
ILT
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
645f8123 153 const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(),
92e059d8
ILT
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
ead1e424
ILT
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
645f8123 184 if (shdr.get_sh_link() != this->symtab_shndx_)
92e059d8
ILT
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.
a3ad94ed 230 gold_assert(this->symtab_shndx_ != -1U);
645f8123 231 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
92e059d8
ILT
232 rd->local_symbols = NULL;
233 else
234 {
235 typename This::Shdr symtabshdr(pshdrs
645f8123 236 + this->symtab_shndx_ * This::shdr_size);
a3ad94ed 237 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
92e059d8
ILT
238 const int sym_size = This::sym_size;
239 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 240 gold_assert(loccount == symtabshdr.get_sh_info());
92e059d8
ILT
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
250template<int size, bool big_endian>
251void
f6ce93d6 252Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
92e059d8 253 Symbol_table* symtab,
ead1e424 254 Layout* layout,
92e059d8
ILT
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 {
a3ad94ed
ILT
269 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
270 p->sh_type, p->contents->data(), p->reloc_count,
92e059d8
ILT
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
61ba1cf9
ILT
285// Relocate the input sections and write out the local symbols.
286
287template<int size, bool big_endian>
288void
f6ce93d6 289Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
61ba1cf9 290 const Symbol_table* symtab,
92e059d8 291 const Layout* layout,
61ba1cf9
ILT
292 Output_file* of)
293{
294 unsigned int shnum = this->shnum();
295
296 // Read the section headers.
645f8123 297 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
61ba1cf9
ILT
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
92e059d8 311 this->relocate_sections(options, symtab, layout, pshdrs, &views);
61ba1cf9
ILT
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.
92e059d8 322 this->write_local_symbols(of, layout->sympool());
61ba1cf9
ILT
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
329template<int size, bool big_endian>
330void
f6ce93d6 331Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
61ba1cf9
ILT
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
61ba1cf9
ILT
354 off_t start = os->offset() + map_sections[i].offset;
355 off_t sh_size = shdr.get_sh_size();
356
ead1e424
ILT
357 if (sh_size == 0)
358 continue;
359
a3ad94ed
ILT
360 gold_assert(map_sections[i].offset >= 0
361 && map_sections[i].offset + sh_size <= os->data_size());
ead1e424 362
61ba1cf9 363 unsigned char* view = of->get_output_view(start, sh_size);
92e059d8
ILT
364 this->read(shdr.get_sh_offset(), sh_size, view);
365
61ba1cf9
ILT
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
376template<int size, bool big_endian>
377void
f6ce93d6 378Sized_relobj<size, big_endian>::relocate_sections(
92e059d8
ILT
379 const General_options& options,
380 const Symbol_table* symtab,
381 const Layout* layout,
382 const unsigned char* pshdrs,
383 Views* pviews)
61ba1cf9
ILT
384{
385 unsigned int shnum = this->shnum();
61ba1cf9
ILT
386 Sized_target<size, big_endian>* target = this->sized_target();
387
92e059d8
ILT
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_;
c06b7b0b 394 relinfo.local_values = &this->local_values_;
92e059d8
ILT
395 relinfo.symbols = this->symbols_;
396
61ba1cf9
ILT
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
92e059d8 414 if (!this->is_section_included(index))
61ba1cf9
ILT
415 {
416 // This relocation section is against a section which we
417 // discarded.
418 continue;
419 }
420
a3ad94ed 421 gold_assert((*pviews)[index].view != NULL);
61ba1cf9 422
645f8123 423 if (shdr.get_sh_link() != this->symtab_shndx_)
61ba1cf9
ILT
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
92e059d8
ILT
462 relinfo.reloc_shndx = i;
463 relinfo.data_shndx = index;
464 target->relocate_section(&relinfo,
465 sh_type,
466 prelocs,
467 reloc_count,
61ba1cf9
ILT
468 (*pviews)[index].view,
469 (*pviews)[index].address,
470 (*pviews)[index].view_size);
471 }
472}
473
a3ad94ed
ILT
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
479template<int size, bool big_endian>
480bool
481Relocate_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
61ba1cf9
ILT
497// Instantiate the templates we need. We could use the configure
498// script to restrict this to only the ones for implemented targets.
499
92e059d8
ILT
500template
501void
f6ce93d6 502Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
92e059d8
ILT
503
504template
505void
f6ce93d6 506Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
92e059d8
ILT
507
508template
509void
f6ce93d6 510Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
92e059d8
ILT
511
512template
513void
f6ce93d6 514Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
92e059d8
ILT
515
516template
517void
f6ce93d6 518Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
92e059d8 519 Symbol_table* symtab,
ead1e424 520 Layout* layout,
92e059d8
ILT
521 Read_relocs_data* rd);
522
523template
524void
f6ce93d6 525Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
92e059d8 526 Symbol_table* symtab,
ead1e424 527 Layout* layout,
92e059d8
ILT
528 Read_relocs_data* rd);
529
530template
531void
f6ce93d6 532Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
92e059d8 533 Symbol_table* symtab,
ead1e424 534 Layout* layout,
92e059d8
ILT
535 Read_relocs_data* rd);
536
537template
538void
f6ce93d6 539Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
92e059d8 540 Symbol_table* symtab,
ead1e424 541 Layout* layout,
92e059d8
ILT
542 Read_relocs_data* rd);
543
61ba1cf9
ILT
544template
545void
f6ce93d6 546Sized_relobj<32, false>::do_relocate(const General_options& options,
61ba1cf9 547 const Symbol_table* symtab,
92e059d8 548 const Layout* layout,
61ba1cf9
ILT
549 Output_file* of);
550
551template
552void
f6ce93d6 553Sized_relobj<32, true>::do_relocate(const General_options& options,
61ba1cf9 554 const Symbol_table* symtab,
92e059d8 555 const Layout* layout,
61ba1cf9
ILT
556 Output_file* of);
557
558template
559void
f6ce93d6 560Sized_relobj<64, false>::do_relocate(const General_options& options,
61ba1cf9 561 const Symbol_table* symtab,
92e059d8 562 const Layout* layout,
61ba1cf9
ILT
563 Output_file* of);
564
565template
566void
f6ce93d6 567Sized_relobj<64, true>::do_relocate(const General_options& options,
61ba1cf9 568 const Symbol_table* symtab,
92e059d8 569 const Layout* layout,
61ba1cf9
ILT
570 Output_file* of);
571
a3ad94ed
ILT
572template
573bool
574Relocate_functions<32, false>::need_copy_reloc(const General_options*,
575 Relobj*, unsigned int,
576 Symbol*);
577
578template
579bool
580Relocate_functions<32, true>::need_copy_reloc(const General_options*,
581 Relobj*, unsigned int,
582 Symbol*);
583
584template
585bool
586Relocate_functions<64, false>::need_copy_reloc(const General_options*,
587 Relobj*, unsigned int,
588 Symbol*);
589
590template
591bool
592Relocate_functions<64, true>::need_copy_reloc(const General_options*,
593 Relobj*, unsigned int,
594 Symbol*);
61ba1cf9
ILT
595
596} // End namespace gold.
This page took 0.051108 seconds and 4 git commands to generate.