Can now do a full static link of hello, world in C or C++
[external/binutils.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_object<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->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_shnum_)
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   if (this->symtab_shnum_ == 0 || this->local_symbol_count_ == 0)
231     rd->local_symbols = NULL;
232   else
233     {
234       typename This::Shdr symtabshdr(pshdrs
235                                      + this->symtab_shnum_ * This::shdr_size);
236       assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
237       const int sym_size = This::sym_size;
238       const unsigned int loccount = this->local_symbol_count_;
239       assert(loccount == symtabshdr.get_sh_info());
240       off_t locsize = loccount * sym_size;
241       rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
242                                                  locsize);
243     }
244 }
245
246 // Scan the relocs and adjust the symbol table.  This looks for
247 // relocations which require GOT/PLT/COPY relocations.
248
249 template<int size, bool big_endian>
250 void
251 Sized_object<size, big_endian>::do_scan_relocs(const General_options& options,
252                                                Symbol_table* symtab,
253                                                Layout* layout,
254                                                Read_relocs_data* rd)
255 {
256   Sized_target<size, big_endian>* target = this->sized_target();
257
258   const unsigned char* local_symbols;
259   if (rd->local_symbols == NULL)
260     local_symbols = NULL;
261   else
262     local_symbols = rd->local_symbols->data();
263
264   for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
265        p != rd->relocs.end();
266        ++p)
267     {
268       target->scan_relocs(options, symtab, layout, this, p->sh_type,
269                           p->contents->data(), p->reloc_count,
270                           this->local_symbol_count_,
271                           local_symbols,
272                           this->symbols_);
273       delete p->contents;
274       p->contents = NULL;
275     }
276
277   if (rd->local_symbols != NULL)
278     {
279       delete rd->local_symbols;
280       rd->local_symbols = NULL;
281     }
282 }
283
284 // Relocate the input sections and write out the local symbols.
285
286 template<int size, bool big_endian>
287 void
288 Sized_object<size, big_endian>::do_relocate(const General_options& options,
289                                             const Symbol_table* symtab,
290                                             const Layout* layout,
291                                             Output_file* of)
292 {
293   unsigned int shnum = this->shnum();
294
295   // Read the section headers.
296   const unsigned char* pshdrs = this->get_view(this->shoff_,
297                                                shnum * This::shdr_size);
298
299   Views views;
300   views.resize(shnum);
301
302   // Make two passes over the sections.  The first one copies the
303   // section data to the output file.  The second one applies
304   // relocations.
305
306   this->write_sections(pshdrs, of, &views);
307
308   // Apply relocations.
309
310   this->relocate_sections(options, symtab, layout, pshdrs, &views);
311
312   // Write out the accumulated views.
313   for (unsigned int i = 1; i < shnum; ++i)
314     {
315       if (views[i].view != NULL)
316         of->write_output_view(views[i].offset, views[i].view_size,
317                               views[i].view);
318     }
319
320   // Write out the local symbols.
321   this->write_local_symbols(of, layout->sympool());
322 }
323
324 // Write section data to the output file.  PSHDRS points to the
325 // section headers.  Record the views in *PVIEWS for use when
326 // relocating.
327
328 template<int size, bool big_endian>
329 void
330 Sized_object<size, big_endian>::write_sections(const unsigned char* pshdrs,
331                                                Output_file* of,
332                                                Views* pviews)
333 {
334   unsigned int shnum = this->shnum();
335   std::vector<Map_to_output>& map_sections(this->map_to_output());
336
337   const unsigned char* p = pshdrs + This::shdr_size;
338   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
339     {
340       View_size* pvs = &(*pviews)[i];
341
342       pvs->view = NULL;
343
344       const Output_section* os = map_sections[i].output_section;
345       if (os == NULL)
346         continue;
347
348       typename This::Shdr shdr(p);
349
350       if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
351         continue;
352
353       off_t start = os->offset() + map_sections[i].offset;
354       off_t sh_size = shdr.get_sh_size();
355
356       if (sh_size == 0)
357         continue;
358
359       assert(map_sections[i].offset >= 0
360              && map_sections[i].offset + sh_size <= os->data_size());
361
362       unsigned char* view = of->get_output_view(start, sh_size);
363       this->read(shdr.get_sh_offset(), sh_size, view);
364
365       pvs->view = view;
366       pvs->address = os->address() + map_sections[i].offset;
367       pvs->offset = start;
368       pvs->view_size = sh_size;
369     }
370 }
371
372 // Relocate section data.  VIEWS points to the section data as views
373 // in the output file.
374
375 template<int size, bool big_endian>
376 void
377 Sized_object<size, big_endian>::relocate_sections(
378     const General_options& options,
379     const Symbol_table* symtab,
380     const Layout* layout,
381     const unsigned char* pshdrs,
382     Views* pviews)
383 {
384   unsigned int shnum = this->shnum();
385   Sized_target<size, big_endian>* target = this->sized_target();
386
387   Relocate_info<size, big_endian> relinfo;
388   relinfo.options = &options;
389   relinfo.symtab = symtab;
390   relinfo.layout = layout;
391   relinfo.object = this;
392   relinfo.local_symbol_count = this->local_symbol_count_;
393   relinfo.values = this->values_;
394   relinfo.symbols = this->symbols_;
395
396   const unsigned char* p = pshdrs + This::shdr_size;
397   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
398     {
399       typename This::Shdr shdr(p);
400
401       unsigned int sh_type = shdr.get_sh_type();
402       if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
403         continue;
404
405       unsigned int index = shdr.get_sh_info();
406       if (index >= this->shnum())
407         {
408           fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"),
409                   program_name, this->name().c_str(), i, index);
410           gold_exit(false);
411         }
412
413       if (!this->is_section_included(index))
414         {
415           // This relocation section is against a section which we
416           // discarded.
417           continue;
418         }
419
420       assert((*pviews)[index].view != NULL);
421
422       if (shdr.get_sh_link() != this->symtab_shnum_)
423         {
424           fprintf(stderr,
425                   _("%s: %s: relocation section %u uses unexpected "
426                     "symbol table %u\n"),
427                   program_name, this->name().c_str(), i, shdr.get_sh_link());
428           gold_exit(false);
429         }
430
431       off_t sh_size = shdr.get_sh_size();
432       const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
433                                                     sh_size);
434
435       unsigned int reloc_size;
436       if (sh_type == elfcpp::SHT_REL)
437         reloc_size = elfcpp::Elf_sizes<size>::rel_size;
438       else
439         reloc_size = elfcpp::Elf_sizes<size>::rela_size;
440
441       if (reloc_size != shdr.get_sh_entsize())
442         {
443           fprintf(stderr,
444                   _("%s: %s: unexpected entsize for reloc section %u: "
445                     "%lu != %u"),
446                   program_name, this->name().c_str(), i,
447                   static_cast<unsigned long>(shdr.get_sh_entsize()),
448                   reloc_size);
449           gold_exit(false);
450         }
451
452       size_t reloc_count = sh_size / reloc_size;
453       if (reloc_count * reloc_size != sh_size)
454         {
455           fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"),
456                   program_name, this->name().c_str(), i,
457                   static_cast<unsigned long>(sh_size));
458           gold_exit(false);
459         }
460
461       relinfo.reloc_shndx = i;
462       relinfo.data_shndx = index;
463       target->relocate_section(&relinfo,
464                                sh_type,
465                                prelocs,
466                                reloc_count,
467                                (*pviews)[index].view,
468                                (*pviews)[index].address,
469                                (*pviews)[index].view_size);
470     }
471 }
472
473 // Instantiate the templates we need.  We could use the configure
474 // script to restrict this to only the ones for implemented targets.
475
476 template
477 void
478 Sized_object<32, false>::do_read_relocs(Read_relocs_data* rd);
479
480 template
481 void
482 Sized_object<32, true>::do_read_relocs(Read_relocs_data* rd);
483
484 template
485 void
486 Sized_object<64, false>::do_read_relocs(Read_relocs_data* rd);
487
488 template
489 void
490 Sized_object<64, true>::do_read_relocs(Read_relocs_data* rd);
491
492 template
493 void
494 Sized_object<32, false>::do_scan_relocs(const General_options& options,
495                                         Symbol_table* symtab,
496                                         Layout* layout,
497                                         Read_relocs_data* rd);
498
499 template
500 void
501 Sized_object<32, true>::do_scan_relocs(const General_options& options,
502                                        Symbol_table* symtab,
503                                        Layout* layout,
504                                        Read_relocs_data* rd);
505
506 template
507 void
508 Sized_object<64, false>::do_scan_relocs(const General_options& options,
509                                         Symbol_table* symtab,
510                                         Layout* layout,
511                                         Read_relocs_data* rd);
512
513 template
514 void
515 Sized_object<64, true>::do_scan_relocs(const General_options& options,
516                                        Symbol_table* symtab,
517                                        Layout* layout,
518                                        Read_relocs_data* rd);
519
520 template
521 void
522 Sized_object<32, false>::do_relocate(const General_options& options,
523                                      const Symbol_table* symtab,
524                                      const Layout* layout,
525                                      Output_file* of);
526
527 template
528 void
529 Sized_object<32, true>::do_relocate(const General_options& options,
530                                     const Symbol_table* symtab,
531                                     const Layout* layout,
532                                     Output_file* of);
533
534 template
535 void
536 Sized_object<64, false>::do_relocate(const General_options& options,
537                                      const Symbol_table* symtab,
538                                      const Layout* layout,
539                                      Output_file* of);
540
541 template
542 void
543 Sized_object<64, true>::do_relocate(const General_options& options,
544                                     const Symbol_table* symtab,
545                                     const Layout* layout,
546                                     Output_file* of);
547
548
549 } // End namespace gold.