* copy-relocs.cc: New file.
[external/binutils.git] / gold / reloc.h
1 // reloc.h -- relocate input files for gold   -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008 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
23 #ifndef GOLD_RELOC_H
24 #define GOLD_RELOC_H
25
26 #include <vector>
27 #include <byteswap.h>
28
29 #include "elfcpp.h"
30 #include "workqueue.h"
31
32 namespace gold
33 {
34
35 class General_options;
36 class Object;
37 class Relobj;
38 class Read_relocs_data;
39 class Symbol;
40 class Layout;
41 class Output_data;
42 class Output_section;
43
44 template<int size>
45 class Sized_symbol;
46
47 template<int size, bool big_endian>
48 class Sized_relobj;
49
50 template<int size>
51 class Symbol_value;
52
53 template<int sh_type, bool dynamic, int size, bool big_endian>
54 class Output_data_reloc;
55
56 // A class to read the relocations for an object file, and then queue
57 // up a task to see if they require any GOT/PLT/COPY relocations in
58 // the symbol table.
59
60 class Read_relocs : public Task
61 {
62  public:
63   // SYMTAB_LOCK is used to lock the symbol table.  BLOCKER should be
64   // unblocked when the Scan_relocs task completes.
65   Read_relocs(const General_options& options, Symbol_table* symtab,
66               Layout* layout, Relobj* object, Task_token* symtab_lock,
67               Task_token* blocker)
68     : options_(options), symtab_(symtab), layout_(layout), object_(object),
69       symtab_lock_(symtab_lock), blocker_(blocker)
70   { }
71
72   // The standard Task methods.
73
74   Task_token*
75   is_runnable();
76
77   void
78   locks(Task_locker*);
79
80   void
81   run(Workqueue*);
82
83   std::string
84   get_name() const;
85
86  private:
87   const General_options& options_;
88   Symbol_table* symtab_;
89   Layout* layout_;
90   Relobj* object_;
91   Task_token* symtab_lock_;
92   Task_token* blocker_;
93 };
94
95 // Scan the relocations for an object to see if they require any
96 // GOT/PLT/COPY relocations.
97
98 class Scan_relocs : public Task
99 {
100  public:
101   // SYMTAB_LOCK is used to lock the symbol table.  BLOCKER should be
102   // unblocked when the task completes.
103   Scan_relocs(const General_options& options, Symbol_table* symtab,
104               Layout* layout, Relobj* object, Read_relocs_data* rd,
105               Task_token* symtab_lock, Task_token* blocker)
106     : options_(options), symtab_(symtab), layout_(layout), object_(object),
107       rd_(rd), symtab_lock_(symtab_lock), blocker_(blocker)
108   { }
109
110   // The standard Task methods.
111
112   Task_token*
113   is_runnable();
114
115   void
116   locks(Task_locker*);
117
118   void
119   run(Workqueue*);
120
121   std::string
122   get_name() const;
123
124  private:
125   const General_options& options_;
126   Symbol_table* symtab_;
127   Layout* layout_;
128   Relobj* object_;
129   Read_relocs_data* rd_;
130   Task_token* symtab_lock_;
131   Task_token* blocker_;
132 };
133
134 // A class to perform all the relocations for an object file.
135
136 class Relocate_task : public Task
137 {
138  public:
139   Relocate_task(const General_options& options, const Symbol_table* symtab,
140                 const Layout* layout, Relobj* object, Output_file* of,
141                 Task_token* input_sections_blocker,
142                 Task_token* output_sections_blocker, Task_token* final_blocker)
143     : options_(options), symtab_(symtab), layout_(layout), object_(object),
144       of_(of), input_sections_blocker_(input_sections_blocker),
145       output_sections_blocker_(output_sections_blocker),
146       final_blocker_(final_blocker)
147   { }
148
149   // The standard Task methods.
150
151   Task_token*
152   is_runnable();
153
154   void
155   locks(Task_locker*);
156
157   void
158   run(Workqueue*);
159
160   std::string
161   get_name() const;
162
163  private:
164   const General_options& options_;
165   const Symbol_table* symtab_;
166   const Layout* layout_;
167   Relobj* object_;
168   Output_file* of_;
169   Task_token* input_sections_blocker_;
170   Task_token* output_sections_blocker_;
171   Task_token* final_blocker_;
172 };
173
174 // During a relocatable link, this class records how relocations
175 // should be handled for a single input reloc section.  An instance of
176 // this class is created while scanning relocs, and it is used while
177 // processing relocs.
178
179 class Relocatable_relocs
180 {
181  public:
182   // We use a vector of unsigned char to indicate how the input relocs
183   // should be handled.  Each element is one of the following values.
184   // We create this vector when we initially scan the relocations.
185   enum Reloc_strategy
186   {
187     // Copy the input reloc.  Don't modify it other than updating the
188     // r_offset field and the r_sym part of the r_info field.
189     RELOC_COPY,
190     // Copy the input reloc which is against an STT_SECTION symbol.
191     // Update the r_offset and r_sym part of the r_info field.  Adjust
192     // the addend by subtracting the value of the old local symbol and
193     // adding the value of the new local symbol.  The addend is in the
194     // SHT_RELA reloc and the contents of the data section do not need
195     // to be changed.
196     RELOC_ADJUST_FOR_SECTION_RELA,
197     // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be
198     // adjusted.
199     RELOC_ADJUST_FOR_SECTION_0,
200     // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the
201     // section need to be changed.  The number indicates the number of
202     // bytes in the addend in the section contents.
203     RELOC_ADJUST_FOR_SECTION_1,
204     RELOC_ADJUST_FOR_SECTION_2,
205     RELOC_ADJUST_FOR_SECTION_4,
206     RELOC_ADJUST_FOR_SECTION_8,
207     // Discard the input reloc--process it completely when relocating
208     // the data section contents.
209     RELOC_DISCARD,
210     // An input reloc which is not discarded, but which requires
211     // target specific processing in order to update it.
212     RELOC_SPECIAL
213   };
214
215   Relocatable_relocs()
216     : reloc_strategies_(), output_reloc_count_(0), posd_(NULL)
217   { }
218
219   // Record the number of relocs.
220   void
221   set_reloc_count(size_t reloc_count)
222   { this->reloc_strategies_.reserve(reloc_count); }
223
224   // Record what to do for the next reloc.
225   void
226   set_next_reloc_strategy(Reloc_strategy strategy)
227   {
228     this->reloc_strategies_.push_back(static_cast<unsigned char>(strategy));
229     if (strategy != RELOC_DISCARD)
230       ++this->output_reloc_count_;
231   }
232
233   // Record the Output_data associated with this reloc section.
234   void
235   set_output_data(Output_data* posd)
236   {
237     gold_assert(this->posd_ == NULL);
238     this->posd_ = posd;
239   }
240
241   // Return the Output_data associated with this reloc section.
242   Output_data*
243   output_data() const
244   { return this->posd_; }
245
246   // Return what to do for reloc I.
247   Reloc_strategy
248   strategy(unsigned int i) const
249   {
250     gold_assert(i < this->reloc_strategies_.size());
251     return static_cast<Reloc_strategy>(this->reloc_strategies_[i]);
252   }
253
254   // Return the number of relocations to create in the output file.
255   size_t
256   output_reloc_count() const
257   { return this->output_reloc_count_; }
258
259  private:
260   typedef std::vector<unsigned char> Reloc_strategies;
261
262   // The strategies for the input reloc.  There is one entry in this
263   // vector for each relocation in the input section.
264   Reloc_strategies reloc_strategies_;
265   // The number of relocations to be created in the output file.
266   size_t output_reloc_count_;
267   // The output data structure associated with this relocation.
268   Output_data* posd_;
269 };
270
271 // Standard relocation routines which are used on many targets.  Here
272 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
273
274 template<int size, bool big_endian>
275 class Relocate_functions
276 {
277 private:
278   // Do a simple relocation with the addend in the section contents.
279   // VALSIZE is the size of the value.
280   template<int valsize>
281   static inline void
282   rel(unsigned char* view,
283       typename elfcpp::Swap<valsize, big_endian>::Valtype value)
284   {
285     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
286     Valtype* wv = reinterpret_cast<Valtype*>(view);
287     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
288     elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
289   }
290
291   // Do a simple relocation using a Symbol_value with the addend in
292   // the section contents.  VALSIZE is the size of the value to
293   // relocate.
294   template<int valsize>
295   static inline void
296   rel(unsigned char* view,
297       const Sized_relobj<size, big_endian>* object,
298       const Symbol_value<size>* psymval)
299   {
300     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
301     Valtype* wv = reinterpret_cast<Valtype*>(view);
302     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
303     x = psymval->value(object, x);
304     elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
305   }
306
307   // Do a simple relocation with the addend in the relocation.
308   // VALSIZE is the size of the value.
309   template<int valsize>
310   static inline void
311   rela(unsigned char* view,
312        typename elfcpp::Swap<valsize, big_endian>::Valtype value,
313        typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
314   {
315     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
316     Valtype* wv = reinterpret_cast<Valtype*>(view);
317     elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend);
318   }
319
320   // Do a simple relocation using a symbol value with the addend in
321   // the relocation.  VALSIZE is the size of the value.
322   template<int valsize>
323   static inline void
324   rela(unsigned char* view,
325        const Sized_relobj<size, big_endian>* object,
326        const Symbol_value<size>* psymval,
327        typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
328   {
329     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
330     Valtype* wv = reinterpret_cast<Valtype*>(view);
331     Valtype x = psymval->value(object, addend);
332     elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
333   }
334
335   // Do a simple PC relative relocation with the addend in the section
336   // contents.  VALSIZE is the size of the value.
337   template<int valsize>
338   static inline void
339   pcrel(unsigned char* view,
340         typename elfcpp::Swap<valsize, big_endian>::Valtype value,
341         typename elfcpp::Elf_types<size>::Elf_Addr address)
342   {
343     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
344     Valtype* wv = reinterpret_cast<Valtype*>(view);
345     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
346     elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
347   }
348
349   // Do a simple PC relative relocation with a Symbol_value with the
350   // addend in the section contents.  VALSIZE is the size of the
351   // value.
352   template<int valsize>
353   static inline void
354   pcrel(unsigned char* view,
355         const Sized_relobj<size, big_endian>* object,
356         const Symbol_value<size>* psymval,
357         typename elfcpp::Elf_types<size>::Elf_Addr address)
358   {
359     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
360     Valtype* wv = reinterpret_cast<Valtype*>(view);
361     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
362     x = psymval->value(object, x);
363     elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
364   }
365
366   // Do a simple PC relative relocation with the addend in the
367   // relocation.  VALSIZE is the size of the value.
368   template<int valsize>
369   static inline void
370   pcrela(unsigned char* view,
371          typename elfcpp::Swap<valsize, big_endian>::Valtype value,
372          typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
373          typename elfcpp::Elf_types<size>::Elf_Addr address)
374   {
375     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
376     Valtype* wv = reinterpret_cast<Valtype*>(view);
377     elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address);
378   }
379
380   // Do a simple PC relative relocation with a Symbol_value with the
381   // addend in the relocation.  VALSIZE is the size of the value.
382   template<int valsize>
383   static inline void
384   pcrela(unsigned char* view,
385          const Sized_relobj<size, big_endian>* object,
386          const Symbol_value<size>* psymval,
387          typename elfcpp::Swap<valsize, big_endian>::Valtype addend,
388          typename elfcpp::Elf_types<size>::Elf_Addr address)
389   {
390     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
391     Valtype* wv = reinterpret_cast<Valtype*>(view);
392     Valtype x = psymval->value(object, addend);
393     elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
394   }
395
396   typedef Relocate_functions<size, big_endian> This;
397
398 public:
399   // Do a simple 8-bit REL relocation with the addend in the section
400   // contents.
401   static inline void
402   rel8(unsigned char* view, unsigned char value)
403   { This::template rel<8>(view, value); }
404
405   static inline void
406   rel8(unsigned char* view,
407        const Sized_relobj<size, big_endian>* object,
408        const Symbol_value<size>* psymval)
409   { This::template rel<8>(view, object, psymval); }
410
411   // Do an 8-bit RELA relocation with the addend in the relocation.
412   static inline void
413   rela8(unsigned char* view, unsigned char value, unsigned char addend)
414   { This::template rela<8>(view, value, addend); }
415
416   static inline void
417   rela8(unsigned char* view,
418         const Sized_relobj<size, big_endian>* object,
419         const Symbol_value<size>* psymval,
420         unsigned char addend)
421   { This::template rela<8>(view, object, psymval, addend); }
422
423   // Do a simple 8-bit PC relative relocation with the addend in the
424   // section contents.
425   static inline void
426   pcrel8(unsigned char* view, unsigned char value,
427          typename elfcpp::Elf_types<size>::Elf_Addr address)
428   { This::template pcrel<8>(view, value, address); }
429
430   static inline void
431   pcrel8(unsigned char* view,
432          const Sized_relobj<size, big_endian>* object,
433          const Symbol_value<size>* psymval,
434          typename elfcpp::Elf_types<size>::Elf_Addr address)
435   { This::template pcrel<8>(view, object, psymval, address); }
436
437   // Do a simple 8-bit PC relative RELA relocation with the addend in
438   // the reloc.
439   static inline void
440   pcrela8(unsigned char* view, unsigned char value, unsigned char addend,
441           typename elfcpp::Elf_types<size>::Elf_Addr address)
442   { This::template pcrela<8>(view, value, addend, address); }
443
444   static inline void
445   pcrela8(unsigned char* view,
446           const Sized_relobj<size, big_endian>* object,
447           const Symbol_value<size>* psymval,
448           unsigned char addend,
449           typename elfcpp::Elf_types<size>::Elf_Addr address)
450   { This::template pcrela<8>(view, object, psymval, addend, address); }
451
452   // Do a simple 16-bit REL relocation with the addend in the section
453   // contents.
454   static inline void
455   rel16(unsigned char* view, elfcpp::Elf_Half value)
456   { This::template rel<16>(view, value); }
457
458   static inline void
459   rel16(unsigned char* view,
460         const Sized_relobj<size, big_endian>* object,
461         const Symbol_value<size>* psymval)
462   { This::template rel<16>(view, object, psymval); }
463
464   // Do an 16-bit RELA relocation with the addend in the relocation.
465   static inline void
466   rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend)
467   { This::template rela<16>(view, value, addend); }
468
469   static inline void
470   rela16(unsigned char* view,
471          const Sized_relobj<size, big_endian>* object,
472          const Symbol_value<size>* psymval,
473          elfcpp::Elf_Half addend)
474   { This::template rela<16>(view, object, psymval, addend); }
475
476   // Do a simple 16-bit PC relative REL relocation with the addend in
477   // the section contents.
478   static inline void
479   pcrel16(unsigned char* view, elfcpp::Elf_Half value,
480           typename elfcpp::Elf_types<size>::Elf_Addr address)
481   { This::template pcrel<16>(view, value, address); }
482
483   static inline void
484   pcrel16(unsigned char* view,
485           const Sized_relobj<size, big_endian>* object,
486           const Symbol_value<size>* psymval,
487           typename elfcpp::Elf_types<size>::Elf_Addr address)
488   { This::template pcrel<16>(view, object, psymval, address); }
489
490   // Do a simple 16-bit PC relative RELA relocation with the addend in
491   // the reloc.
492   static inline void
493   pcrela16(unsigned char* view, elfcpp::Elf_Half value,
494            elfcpp::Elf_Half addend,
495            typename elfcpp::Elf_types<size>::Elf_Addr address)
496   { This::template pcrela<16>(view, value, addend, address); }
497
498   static inline void
499   pcrela16(unsigned char* view,
500            const Sized_relobj<size, big_endian>* object,
501            const Symbol_value<size>* psymval,
502            elfcpp::Elf_Half addend,
503            typename elfcpp::Elf_types<size>::Elf_Addr address)
504   { This::template pcrela<16>(view, object, psymval, addend, address); }
505
506   // Do a simple 32-bit REL relocation with the addend in the section
507   // contents.
508   static inline void
509   rel32(unsigned char* view, elfcpp::Elf_Word value)
510   { This::template rel<32>(view, value); }
511
512   static inline void
513   rel32(unsigned char* view,
514         const Sized_relobj<size, big_endian>* object,
515         const Symbol_value<size>* psymval)
516   { This::template rel<32>(view, object, psymval); }
517
518   // Do an 32-bit RELA relocation with the addend in the relocation.
519   static inline void
520   rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend)
521   { This::template rela<32>(view, value, addend); }
522
523   static inline void
524   rela32(unsigned char* view,
525          const Sized_relobj<size, big_endian>* object,
526          const Symbol_value<size>* psymval,
527          elfcpp::Elf_Word addend)
528   { This::template rela<32>(view, object, psymval, addend); }
529
530   // Do a simple 32-bit PC relative REL relocation with the addend in
531   // the section contents.
532   static inline void
533   pcrel32(unsigned char* view, elfcpp::Elf_Word value,
534           typename elfcpp::Elf_types<size>::Elf_Addr address)
535   { This::template pcrel<32>(view, value, address); }
536
537   static inline void
538   pcrel32(unsigned char* view,
539           const Sized_relobj<size, big_endian>* object,
540           const Symbol_value<size>* psymval,
541           typename elfcpp::Elf_types<size>::Elf_Addr address)
542   { This::template pcrel<32>(view, object, psymval, address); }
543
544   // Do a simple 32-bit PC relative RELA relocation with the addend in
545   // the relocation.
546   static inline void
547   pcrela32(unsigned char* view, elfcpp::Elf_Word value,
548            elfcpp::Elf_Word addend,
549            typename elfcpp::Elf_types<size>::Elf_Addr address)
550   { This::template pcrela<32>(view, value, addend, address); }
551
552   static inline void
553   pcrela32(unsigned char* view,
554            const Sized_relobj<size, big_endian>* object,
555            const Symbol_value<size>* psymval,
556            elfcpp::Elf_Word addend,
557            typename elfcpp::Elf_types<size>::Elf_Addr address)
558   { This::template pcrela<32>(view, object, psymval, addend, address); }
559
560   // Do a simple 64-bit REL relocation with the addend in the section
561   // contents.
562   static inline void
563   rel64(unsigned char* view, elfcpp::Elf_Xword value)
564   { This::template rel<64>(view, value); }
565
566   static inline void
567   rel64(unsigned char* view,
568         const Sized_relobj<size, big_endian>* object,
569         const Symbol_value<size>* psymval)
570   { This::template rel<64>(view, object, psymval); }
571
572   // Do a 64-bit RELA relocation with the addend in the relocation.
573   static inline void
574   rela64(unsigned char* view, elfcpp::Elf_Xword value,
575          elfcpp::Elf_Xword addend)
576   { This::template rela<64>(view, value, addend); }
577
578   static inline void
579   rela64(unsigned char* view,
580          const Sized_relobj<size, big_endian>* object,
581          const Symbol_value<size>* psymval,
582          elfcpp::Elf_Xword addend)
583   { This::template rela<64>(view, object, psymval, addend); }
584
585   // Do a simple 64-bit PC relative REL relocation with the addend in
586   // the section contents.
587   static inline void
588   pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
589           typename elfcpp::Elf_types<size>::Elf_Addr address)
590   { This::template pcrel<64>(view, value, address); }
591
592   static inline void
593   pcrel64(unsigned char* view,
594           const Sized_relobj<size, big_endian>* object,
595           const Symbol_value<size>* psymval,
596           typename elfcpp::Elf_types<size>::Elf_Addr address)
597   { This::template pcrel<64>(view, object, psymval, address); }
598
599   // Do a simple 64-bit PC relative RELA relocation with the addend in
600   // the relocation.
601   static inline void
602   pcrela64(unsigned char* view, elfcpp::Elf_Xword value,
603            elfcpp::Elf_Xword addend,
604            typename elfcpp::Elf_types<size>::Elf_Addr address)
605   { This::template pcrela<64>(view, value, addend, address); }
606
607   static inline void
608   pcrela64(unsigned char* view,
609            const Sized_relobj<size, big_endian>* object,
610            const Symbol_value<size>* psymval,
611            elfcpp::Elf_Xword addend,
612            typename elfcpp::Elf_types<size>::Elf_Addr address)
613   { This::template pcrela<64>(view, object, psymval, addend, address); }
614 };
615
616 // Track relocations while reading a section.  This lets you ask for
617 // the relocation at a certain offset, and see how relocs occur
618 // between points of interest.
619
620 template<int size, bool big_endian>
621 class Track_relocs
622 {
623  public:
624   Track_relocs()
625     : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
626   { }
627
628   // Initialize the Track_relocs object.  OBJECT is the object holding
629   // the reloc section, RELOC_SHNDX is the section index of the reloc
630   // section, and RELOC_TYPE is the type of the reloc section
631   // (elfcpp::SHT_REL or elfcpp::SHT_RELA).  This returns false if
632   // something went wrong.
633   bool
634   initialize(Object* object, unsigned int reloc_shndx,
635              unsigned int reloc_type);
636
637   // Return the offset in the data section to which the next reloc
638   // applies.  THis returns -1 if there is no next reloc.
639   off_t
640   next_offset() const;
641
642   // Return the symbol index of the next reloc.  This returns -1U if
643   // there is no next reloc.
644   unsigned int
645   next_symndx() const;
646
647   // Advance to OFFSET within the data section, and return the number
648   // of relocs which would be skipped.
649   int
650   advance(off_t offset);
651
652  private:
653   // The contents of the input object's reloc section.
654   const unsigned char* prelocs_;
655   // The length of the reloc section.
656   section_size_type len_;
657   // Our current position in the reloc section.
658   section_size_type pos_;
659   // The size of the relocs in the section.
660   int reloc_size_;
661 };
662
663 } // End namespace gold.
664
665 #endif // !defined(GOLD_RELOC_H)