aafa71a877a59290c2027ea2335285c43389d34a
[platform/upstream/binutils.git] / bfd / reloc.c
1 /* BFD support for handling relocation entries.
2    Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3    Free Software Foundation, Inc.
4    Written by Cygnus Support.
5
6 This file is part of BFD, the Binary File Descriptor library.
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 /*
23 SECTION
24         Relocations
25
26         BFD maintains relocations in much the same way it maintains
27         symbols: they are left alone until required, then read in
28         en-masse and translated into an internal form.  A common
29         routine <<bfd_perform_relocation>> acts upon the
30         canonical form to do the fixup.
31
32         Relocations are maintained on a per section basis,
33         while symbols are maintained on a per BFD basis.
34
35         All that a back end has to do to fit the BFD interface is to create
36         a <<struct reloc_cache_entry>> for each relocation
37         in a particular section, and fill in the right bits of the structures.
38
39 @menu
40 @* typedef arelent::
41 @* howto manager::
42 @end menu
43
44 */
45
46 /* DO compile in the reloc_code name table from libbfd.h.  */
47 #define _BFD_MAKE_TABLE_bfd_reloc_code_real
48
49 #include "bfd.h"
50 #include "sysdep.h"
51 #include "bfdlink.h"
52 #include "libbfd.h"
53 /*
54 DOCDD
55 INODE
56         typedef arelent, howto manager, Relocations, Relocations
57
58 SUBSECTION
59         typedef arelent
60
61         This is the structure of a relocation entry:
62
63 CODE_FRAGMENT
64 .
65 .typedef enum bfd_reloc_status
66 .{
67 .       {* No errors detected *}
68 .  bfd_reloc_ok,
69 .
70 .       {* The relocation was performed, but there was an overflow. *}
71 .  bfd_reloc_overflow,
72 .
73 .       {* The address to relocate was not within the section supplied. *}
74 .  bfd_reloc_outofrange,
75 .
76 .       {* Used by special functions *}
77 .  bfd_reloc_continue,
78 .
79 .       {* Unsupported relocation size requested. *}
80 .  bfd_reloc_notsupported,
81 .
82 .       {* Unused *}
83 .  bfd_reloc_other,
84 .
85 .       {* The symbol to relocate against was undefined. *}
86 .  bfd_reloc_undefined,
87 .
88 .       {* The relocation was performed, but may not be ok - presently
89 .          generated only when linking i960 coff files with i960 b.out
90 .          symbols.  If this type is returned, the error_message argument
91 .          to bfd_perform_relocation will be set.  *}
92 .  bfd_reloc_dangerous
93 . }
94 . bfd_reloc_status_type;
95 .
96 .
97 .typedef struct reloc_cache_entry
98 .{
99 .       {* A pointer into the canonical table of pointers  *}
100 .  struct symbol_cache_entry **sym_ptr_ptr;
101 .
102 .       {* offset in section *}
103 .  bfd_size_type address;
104 .
105 .       {* addend for relocation value *}
106 .  bfd_vma addend;
107 .
108 .       {* Pointer to how to perform the required relocation *}
109 .  reloc_howto_type *howto;
110 .
111 .} arelent;
112
113 */
114
115 /*
116 DESCRIPTION
117
118         Here is a description of each of the fields within an <<arelent>>:
119
120         o <<sym_ptr_ptr>>
121
122         The symbol table pointer points to a pointer to the symbol
123         associated with the relocation request.  It is
124         the pointer into the table returned by the back end's
125         <<get_symtab>> action. @xref{Symbols}. The symbol is referenced
126         through a pointer to a pointer so that tools like the linker
127         can fix up all the symbols of the same name by modifying only
128         one pointer. The relocation routine looks in the symbol and
129         uses the base of the section the symbol is attached to and the
130         value of the symbol as the initial relocation offset. If the
131         symbol pointer is zero, then the section provided is looked up.
132
133         o <<address>>
134
135         The <<address>> field gives the offset in bytes from the base of
136         the section data which owns the relocation record to the first
137         byte of relocatable information. The actual data relocated
138         will be relative to this point; for example, a relocation
139         type which modifies the bottom two bytes of a four byte word
140         would not touch the first byte pointed to in a big endian
141         world.
142
143         o <<addend>>
144
145         The <<addend>> is a value provided by the back end to be added (!)
146         to the relocation offset. Its interpretation is dependent upon
147         the howto. For example, on the 68k the code:
148
149
150 |        char foo[];
151 |        main()
152 |                {
153 |                return foo[0x12345678];
154 |                }
155
156         Could be compiled into:
157
158 |        linkw fp,#-4
159 |        moveb @@#12345678,d0
160 |        extbl d0
161 |        unlk fp
162 |        rts
163
164
165         This could create a reloc pointing to <<foo>>, but leave the
166         offset in the data, something like:
167
168
169 |RELOCATION RECORDS FOR [.text]:
170 |offset   type      value
171 |00000006 32        _foo
172 |
173 |00000000 4e56 fffc          ; linkw fp,#-4
174 |00000004 1039 1234 5678     ; moveb @@#12345678,d0
175 |0000000a 49c0               ; extbl d0
176 |0000000c 4e5e               ; unlk fp
177 |0000000e 4e75               ; rts
178
179
180         Using coff and an 88k, some instructions don't have enough
181         space in them to represent the full address range, and
182         pointers have to be loaded in two parts. So you'd get something like:
183
184
185 |        or.u     r13,r0,hi16(_foo+0x12345678)
186 |        ld.b     r2,r13,lo16(_foo+0x12345678)
187 |        jmp      r1
188
189
190         This should create two relocs, both pointing to <<_foo>>, and with
191         0x12340000 in their addend field. The data would consist of:
192
193
194 |RELOCATION RECORDS FOR [.text]:
195 |offset   type      value
196 |00000002 HVRT16    _foo+0x12340000
197 |00000006 LVRT16    _foo+0x12340000
198 |
199 |00000000 5da05678           ; or.u r13,r0,0x5678
200 |00000004 1c4d5678           ; ld.b r2,r13,0x5678
201 |00000008 f400c001           ; jmp r1
202
203
204         The relocation routine digs out the value from the data, adds
205         it to the addend to get the original offset, and then adds the
206         value of <<_foo>>. Note that all 32 bits have to be kept around
207         somewhere, to cope with carry from bit 15 to bit 16.
208
209         One further example is the sparc and the a.out format. The
210         sparc has a similar problem to the 88k, in that some
211         instructions don't have room for an entire offset, but on the
212         sparc the parts are created in odd sized lumps. The designers of
213         the a.out format chose to not use the data within the section
214         for storing part of the offset; all the offset is kept within
215         the reloc. Anything in the data should be ignored.
216
217 |        save %sp,-112,%sp
218 |        sethi %hi(_foo+0x12345678),%g2
219 |        ldsb [%g2+%lo(_foo+0x12345678)],%i0
220 |        ret
221 |        restore
222
223         Both relocs contain a pointer to <<foo>>, and the offsets
224         contain junk.
225
226
227 |RELOCATION RECORDS FOR [.text]:
228 |offset   type      value
229 |00000004 HI22      _foo+0x12345678
230 |00000008 LO10      _foo+0x12345678
231 |
232 |00000000 9de3bf90     ; save %sp,-112,%sp
233 |00000004 05000000     ; sethi %hi(_foo+0),%g2
234 |00000008 f048a000     ; ldsb [%g2+%lo(_foo+0)],%i0
235 |0000000c 81c7e008     ; ret
236 |00000010 81e80000     ; restore
237
238
239         o <<howto>>
240
241         The <<howto>> field can be imagined as a
242         relocation instruction. It is a pointer to a structure which
243         contains information on what to do with all of the other
244         information in the reloc record and data section. A back end
245         would normally have a relocation instruction set and turn
246         relocations into pointers to the correct structure on input -
247         but it would be possible to create each howto field on demand.
248
249 */
250
251 /*
252 SUBSUBSECTION
253         <<enum complain_overflow>>
254
255         Indicates what sort of overflow checking should be done when
256         performing a relocation.
257
258 CODE_FRAGMENT
259 .
260 .enum complain_overflow
261 .{
262 .       {* Do not complain on overflow. *}
263 .  complain_overflow_dont,
264 .
265 .       {* Complain if the bitfield overflows, whether it is considered
266 .          as signed or unsigned. *}
267 .  complain_overflow_bitfield,
268 .
269 .       {* Complain if the value overflows when considered as signed
270 .          number. *}
271 .  complain_overflow_signed,
272 .
273 .       {* Complain if the value overflows when considered as an
274 .          unsigned number. *}
275 .  complain_overflow_unsigned
276 .};
277
278 */
279
280 /*
281 SUBSUBSECTION
282         <<reloc_howto_type>>
283
284         The <<reloc_howto_type>> is a structure which contains all the
285         information that libbfd needs to know to tie up a back end's data.
286
287 CODE_FRAGMENT
288 .struct symbol_cache_entry;             {* Forward declaration *}
289 .
290 .struct reloc_howto_struct
291 .{
292 .       {*  The type field has mainly a documentary use - the back end can
293 .           do what it wants with it, though normally the back end's
294 .           external idea of what a reloc number is stored
295 .           in this field. For example, a PC relative word relocation
296 .           in a coff environment has the type 023 - because that's
297 .           what the outside world calls a R_PCRWORD reloc. *}
298 .  unsigned int type;
299 .
300 .       {*  The value the final relocation is shifted right by. This drops
301 .           unwanted data from the relocation.  *}
302 .  unsigned int rightshift;
303 .
304 .       {*  The size of the item to be relocated.  This is *not* a
305 .           power-of-two measure.  To get the number of bytes operated
306 .           on by a type of relocation, use bfd_get_reloc_size.  *}
307 .  int size;
308 .
309 .       {*  The number of bits in the item to be relocated.  This is used
310 .           when doing overflow checking.  *}
311 .  unsigned int bitsize;
312 .
313 .       {*  Notes that the relocation is relative to the location in the
314 .           data section of the addend. The relocation function will
315 .           subtract from the relocation value the address of the location
316 .           being relocated. *}
317 .  boolean pc_relative;
318 .
319 .       {*  The bit position of the reloc value in the destination.
320 .           The relocated value is left shifted by this amount. *}
321 .  unsigned int bitpos;
322 .
323 .       {* What type of overflow error should be checked for when
324 .          relocating. *}
325 .  enum complain_overflow complain_on_overflow;
326 .
327 .       {* If this field is non null, then the supplied function is
328 .          called rather than the normal function. This allows really
329 .          strange relocation methods to be accomodated (e.g., i960 callj
330 .          instructions). *}
331 .  bfd_reloc_status_type (*special_function)
332 .                                   PARAMS ((bfd *abfd,
333 .                                            arelent *reloc_entry,
334 .                                            struct symbol_cache_entry *symbol,
335 .                                            PTR data,
336 .                                            asection *input_section,
337 .                                            bfd *output_bfd,
338 .                                            char **error_message));
339 .
340 .       {* The textual name of the relocation type. *}
341 .  char *name;
342 .
343 .       {* Some formats record a relocation addend in the section contents
344 .          rather than with the relocation.  For ELF formats this is the
345 .          distinction between USE_REL and USE_RELA (though the code checks
346 .          for USE_REL == 1/0).  The value of this field is TRUE if the
347 .          addend is recorded with the section contents; when performing a
348 .          partial link (ld -r) the section contents (the data) will be
349 .          modified.  The value of this field is FALSE if addends are
350 .          recorded with the relocation (in arelent.addend); when performing
351 .          a partial link the relocation will be modified.
352 .          All relocations for all ELF USE_RELA targets should set this field
353 .          to FALSE (values of TRUE should be looked on with suspicion).
354 .          However, the converse is not true: not all relocations of all ELF
355 .          USE_REL targets set this field to TRUE.  Why this is so is peculiar
356 .          to each particular target.  For relocs that aren't used in partial
357 .          links (e.g. GOT stuff) it doesn't matter what this is set to.  *}
358 .  boolean partial_inplace;
359 .
360 .       {* The src_mask selects which parts of the read in data
361 .          are to be used in the relocation sum.  E.g., if this was an 8 bit
362 .          byte of data which we read and relocated, this would be
363 .          0x000000ff. When we have relocs which have an addend, such as
364 .          sun4 extended relocs, the value in the offset part of a
365 .          relocating field is garbage so we never use it. In this case
366 .          the mask would be 0x00000000. *}
367 .  bfd_vma src_mask;
368 .
369 .       {* The dst_mask selects which parts of the instruction are replaced
370 .          into the instruction. In most cases src_mask == dst_mask,
371 .          except in the above special case, where dst_mask would be
372 .          0x000000ff, and src_mask would be 0x00000000.   *}
373 .  bfd_vma dst_mask;
374 .
375 .       {* When some formats create PC relative instructions, they leave
376 .          the value of the pc of the place being relocated in the offset
377 .          slot of the instruction, so that a PC relative relocation can
378 .          be made just by adding in an ordinary offset (e.g., sun3 a.out).
379 .          Some formats leave the displacement part of an instruction
380 .          empty (e.g., m88k bcs); this flag signals the fact.*}
381 .  boolean pcrel_offset;
382 .
383 .};
384
385 */
386
387 /*
388 FUNCTION
389         The HOWTO Macro
390
391 DESCRIPTION
392         The HOWTO define is horrible and will go away.
393
394
395 .#define HOWTO(C, R,S,B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
396 .  {(unsigned)C,R,S,B, P, BI, O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
397
398 DESCRIPTION
399         And will be replaced with the totally magic way. But for the
400         moment, we are compatible, so do it this way.
401
402
403 .#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,complain_overflow_dont,FUNCTION, NAME,false,0,0,IN)
404 .
405
406 DESCRIPTION
407         This is used to fill in an empty howto entry in an array.
408
409 .#define EMPTY_HOWTO(C) \
410 .  HOWTO((C),0,0,0,false,0,complain_overflow_dont,NULL,NULL,false,0,0,false)
411 .
412
413 DESCRIPTION
414         Helper routine to turn a symbol into a relocation value.
415
416 .#define HOWTO_PREPARE(relocation, symbol)      \
417 .  {                                            \
418 .  if (symbol != (asymbol *)NULL) {             \
419 .    if (bfd_is_com_section (symbol->section)) { \
420 .      relocation = 0;                          \
421 .    }                                          \
422 .    else {                                     \
423 .      relocation = symbol->value;              \
424 .    }                                          \
425 .  }                                            \
426 .}
427
428 */
429
430 /*
431 FUNCTION
432         bfd_get_reloc_size
433
434 SYNOPSIS
435         unsigned int bfd_get_reloc_size (reloc_howto_type *);
436
437 DESCRIPTION
438         For a reloc_howto_type that operates on a fixed number of bytes,
439         this returns the number of bytes operated on.
440  */
441
442 unsigned int
443 bfd_get_reloc_size (howto)
444      reloc_howto_type *howto;
445 {
446   switch (howto->size)
447     {
448     case 0: return 1;
449     case 1: return 2;
450     case 2: return 4;
451     case 3: return 0;
452     case 4: return 8;
453     case 8: return 16;
454     case -2: return 4;
455     default: abort ();
456     }
457 }
458
459 /*
460 TYPEDEF
461         arelent_chain
462
463 DESCRIPTION
464
465         How relocs are tied together in an <<asection>>:
466
467 .typedef struct relent_chain {
468 .  arelent relent;
469 .  struct   relent_chain *next;
470 .} arelent_chain;
471
472 */
473
474 /* N_ONES produces N one bits, without overflowing machine arithmetic.  */
475 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
476
477 /*
478 FUNCTION
479         bfd_check_overflow
480
481 SYNOPSIS
482         bfd_reloc_status_type
483                 bfd_check_overflow
484                         (enum complain_overflow how,
485                          unsigned int bitsize,
486                          unsigned int rightshift,
487                          unsigned int addrsize,
488                          bfd_vma relocation);
489
490 DESCRIPTION
491         Perform overflow checking on @var{relocation} which has
492         @var{bitsize} significant bits and will be shifted right by
493         @var{rightshift} bits, on a machine with addresses containing
494         @var{addrsize} significant bits.  The result is either of
495         @code{bfd_reloc_ok} or @code{bfd_reloc_overflow}.
496
497 */
498
499 bfd_reloc_status_type
500 bfd_check_overflow (how, bitsize, rightshift, addrsize, relocation)
501      enum complain_overflow how;
502      unsigned int bitsize;
503      unsigned int rightshift;
504      unsigned int addrsize;
505      bfd_vma relocation;
506 {
507   bfd_vma fieldmask, addrmask, signmask, ss, a;
508   bfd_reloc_status_type flag = bfd_reloc_ok;
509
510   a = relocation;
511
512   /* Note: BITSIZE should always be <= ADDRSIZE, but in case it's not,
513      we'll be permissive: extra bits in the field mask will
514      automatically extend the address mask for purposes of the
515      overflow check.  */
516   fieldmask = N_ONES (bitsize);
517   addrmask = N_ONES (addrsize) | fieldmask;
518
519   switch (how)
520     {
521     case complain_overflow_dont:
522       break;
523
524     case complain_overflow_signed:
525       /* If any sign bits are set, all sign bits must be set.  That
526          is, A must be a valid negative address after shifting.  */
527       a = (a & addrmask) >> rightshift;
528       signmask = ~ (fieldmask >> 1);
529       ss = a & signmask;
530       if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
531         flag = bfd_reloc_overflow;
532       break;
533
534     case complain_overflow_unsigned:
535       /* We have an overflow if the address does not fit in the field.  */
536       a = (a & addrmask) >> rightshift;
537       if ((a & ~ fieldmask) != 0)
538         flag = bfd_reloc_overflow;
539       break;
540
541     case complain_overflow_bitfield:
542       /* Bitfields are sometimes signed, sometimes unsigned.  We
543          explicitly allow an address wrap too, which means a bitfield
544          of n bits is allowed to store -2**n to 2**n-1.  Thus overflow
545          if the value has some, but not all, bits set outside the
546          field.  */
547       a >>= rightshift;
548       ss = a & ~ fieldmask;
549       if (ss != 0 && ss != (((bfd_vma) -1 >> rightshift) & ~ fieldmask))
550         flag = bfd_reloc_overflow;
551       break;
552
553     default:
554       abort ();
555     }
556
557   return flag;
558 }
559
560 /*
561 FUNCTION
562         bfd_perform_relocation
563
564 SYNOPSIS
565         bfd_reloc_status_type
566                 bfd_perform_relocation
567                         (bfd *abfd,
568                          arelent *reloc_entry,
569                          PTR data,
570                          asection *input_section,
571                          bfd *output_bfd,
572                          char **error_message);
573
574 DESCRIPTION
575         If @var{output_bfd} is supplied to this function, the
576         generated image will be relocatable; the relocations are
577         copied to the output file after they have been changed to
578         reflect the new state of the world. There are two ways of
579         reflecting the results of partial linkage in an output file:
580         by modifying the output data in place, and by modifying the
581         relocation record.  Some native formats (e.g., basic a.out and
582         basic coff) have no way of specifying an addend in the
583         relocation type, so the addend has to go in the output data.
584         This is no big deal since in these formats the output data
585         slot will always be big enough for the addend. Complex reloc
586         types with addends were invented to solve just this problem.
587         The @var{error_message} argument is set to an error message if
588         this return @code{bfd_reloc_dangerous}.
589
590 */
591
592
593 bfd_reloc_status_type
594 bfd_perform_relocation (abfd, reloc_entry, data, input_section, output_bfd,
595                         error_message)
596      bfd *abfd;
597      arelent *reloc_entry;
598      PTR data;
599      asection *input_section;
600      bfd *output_bfd;
601      char **error_message;
602 {
603   bfd_vma relocation;
604   bfd_reloc_status_type flag = bfd_reloc_ok;
605   bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
606   bfd_vma output_base = 0;
607   reloc_howto_type *howto = reloc_entry->howto;
608   asection *reloc_target_output_section;
609   asymbol *symbol;
610
611   symbol = *(reloc_entry->sym_ptr_ptr);
612   if (bfd_is_abs_section (symbol->section)
613       && output_bfd != (bfd *) NULL)
614     {
615       reloc_entry->address += input_section->output_offset;
616       return bfd_reloc_ok;
617     }
618
619   /* If we are not producing relocateable output, return an error if
620      the symbol is not defined.  An undefined weak symbol is
621      considered to have a value of zero (SVR4 ABI, p. 4-27).  */
622   if (bfd_is_und_section (symbol->section)
623       && (symbol->flags & BSF_WEAK) == 0
624       && output_bfd == (bfd *) NULL)
625     flag = bfd_reloc_undefined;
626
627   /* If there is a function supplied to handle this relocation type,
628      call it.  It'll return `bfd_reloc_continue' if further processing
629      can be done.  */
630   if (howto->special_function)
631     {
632       bfd_reloc_status_type cont;
633       cont = howto->special_function (abfd, reloc_entry, symbol, data,
634                                       input_section, output_bfd,
635                                       error_message);
636       if (cont != bfd_reloc_continue)
637         return cont;
638     }
639
640   /* Is the address of the relocation really within the section?  */
641   if (reloc_entry->address > input_section->_cooked_size /
642       bfd_octets_per_byte (abfd))
643     return bfd_reloc_outofrange;
644
645   /* Work out which section the relocation is targetted at and the
646      initial relocation command value.  */
647
648   /* Get symbol value.  (Common symbols are special.)  */
649   if (bfd_is_com_section (symbol->section))
650     relocation = 0;
651   else
652     relocation = symbol->value;
653
654
655   reloc_target_output_section = symbol->section->output_section;
656
657   /* Convert input-section-relative symbol value to absolute.  */
658   if (output_bfd && howto->partial_inplace == false)
659     output_base = 0;
660   else
661     output_base = reloc_target_output_section->vma;
662
663   relocation += output_base + symbol->section->output_offset;
664
665   /* Add in supplied addend.  */
666   relocation += reloc_entry->addend;
667
668   /* Here the variable relocation holds the final address of the
669      symbol we are relocating against, plus any addend.  */
670
671   if (howto->pc_relative == true)
672     {
673       /* This is a PC relative relocation.  We want to set RELOCATION
674          to the distance between the address of the symbol and the
675          location.  RELOCATION is already the address of the symbol.
676
677          We start by subtracting the address of the section containing
678          the location.
679
680          If pcrel_offset is set, we must further subtract the position
681          of the location within the section.  Some targets arrange for
682          the addend to be the negative of the position of the location
683          within the section; for example, i386-aout does this.  For
684          i386-aout, pcrel_offset is false.  Some other targets do not
685          include the position of the location; for example, m88kbcs,
686          or ELF.  For those targets, pcrel_offset is true.
687
688          If we are producing relocateable output, then we must ensure
689          that this reloc will be correctly computed when the final
690          relocation is done.  If pcrel_offset is false we want to wind
691          up with the negative of the location within the section,
692          which means we must adjust the existing addend by the change
693          in the location within the section.  If pcrel_offset is true
694          we do not want to adjust the existing addend at all.
695
696          FIXME: This seems logical to me, but for the case of
697          producing relocateable output it is not what the code
698          actually does.  I don't want to change it, because it seems
699          far too likely that something will break.  */
700
701       relocation -=
702         input_section->output_section->vma + input_section->output_offset;
703
704       if (howto->pcrel_offset == true)
705         relocation -= reloc_entry->address;
706     }
707
708   if (output_bfd != (bfd *) NULL)
709     {
710       if (howto->partial_inplace == false)
711         {
712           /* This is a partial relocation, and we want to apply the relocation
713              to the reloc entry rather than the raw data. Modify the reloc
714              inplace to reflect what we now know.  */
715           reloc_entry->addend = relocation;
716           reloc_entry->address += input_section->output_offset;
717           return flag;
718         }
719       else
720         {
721           /* This is a partial relocation, but inplace, so modify the
722              reloc record a bit.
723
724              If we've relocated with a symbol with a section, change
725              into a ref to the section belonging to the symbol.  */
726
727           reloc_entry->address += input_section->output_offset;
728
729           /* WTF?? */
730           if (abfd->xvec->flavour == bfd_target_coff_flavour
731               && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
732               && strcmp (abfd->xvec->name, "xcoff-powermac") != 0
733               && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
734               && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
735             {
736 #if 1
737               /* For m68k-coff, the addend was being subtracted twice during
738                  relocation with -r.  Removing the line below this comment
739                  fixes that problem; see PR 2953.
740
741 However, Ian wrote the following, regarding removing the line below,
742 which explains why it is still enabled:  --djm
743
744 If you put a patch like that into BFD you need to check all the COFF
745 linkers.  I am fairly certain that patch will break coff-i386 (e.g.,
746 SCO); see coff_i386_reloc in coff-i386.c where I worked around the
747 problem in a different way.  There may very well be a reason that the
748 code works as it does.
749
750 Hmmm.  The first obvious point is that bfd_perform_relocation should
751 not have any tests that depend upon the flavour.  It's seem like
752 entirely the wrong place for such a thing.  The second obvious point
753 is that the current code ignores the reloc addend when producing
754 relocateable output for COFF.  That's peculiar.  In fact, I really
755 have no idea what the point of the line you want to remove is.
756
757 A typical COFF reloc subtracts the old value of the symbol and adds in
758 the new value to the location in the object file (if it's a pc
759 relative reloc it adds the difference between the symbol value and the
760 location).  When relocating we need to preserve that property.
761
762 BFD handles this by setting the addend to the negative of the old
763 value of the symbol.  Unfortunately it handles common symbols in a
764 non-standard way (it doesn't subtract the old value) but that's a
765 different story (we can't change it without losing backward
766 compatibility with old object files) (coff-i386 does subtract the old
767 value, to be compatible with existing coff-i386 targets, like SCO).
768
769 So everything works fine when not producing relocateable output.  When
770 we are producing relocateable output, logically we should do exactly
771 what we do when not producing relocateable output.  Therefore, your
772 patch is correct.  In fact, it should probably always just set
773 reloc_entry->addend to 0 for all cases, since it is, in fact, going to
774 add the value into the object file.  This won't hurt the COFF code,
775 which doesn't use the addend; I'm not sure what it will do to other
776 formats (the thing to check for would be whether any formats both use
777 the addend and set partial_inplace).
778
779 When I wanted to make coff-i386 produce relocateable output, I ran
780 into the problem that you are running into: I wanted to remove that
781 line.  Rather than risk it, I made the coff-i386 relocs use a special
782 function; it's coff_i386_reloc in coff-i386.c.  The function
783 specifically adds the addend field into the object file, knowing that
784 bfd_perform_relocation is not going to.  If you remove that line, then
785 coff-i386.c will wind up adding the addend field in twice.  It's
786 trivial to fix; it just needs to be done.
787
788 The problem with removing the line is just that it may break some
789 working code.  With BFD it's hard to be sure of anything.  The right
790 way to deal with this is simply to build and test at least all the
791 supported COFF targets.  It should be straightforward if time and disk
792 space consuming.  For each target:
793     1) build the linker
794     2) generate some executable, and link it using -r (I would
795        probably use paranoia.o and link against newlib/libc.a, which
796        for all the supported targets would be available in
797        /usr/cygnus/progressive/H-host/target/lib/libc.a).
798     3) make the change to reloc.c
799     4) rebuild the linker
800     5) repeat step 2
801     6) if the resulting object files are the same, you have at least
802        made it no worse
803     7) if they are different you have to figure out which version is
804        right
805 */
806               relocation -= reloc_entry->addend;
807 #endif
808               reloc_entry->addend = 0;
809             }
810           else
811             {
812               reloc_entry->addend = relocation;
813             }
814         }
815     }
816   else
817     {
818       reloc_entry->addend = 0;
819     }
820
821   /* FIXME: This overflow checking is incomplete, because the value
822      might have overflowed before we get here.  For a correct check we
823      need to compute the value in a size larger than bitsize, but we
824      can't reasonably do that for a reloc the same size as a host
825      machine word.
826      FIXME: We should also do overflow checking on the result after
827      adding in the value contained in the object file.  */
828   if (howto->complain_on_overflow != complain_overflow_dont
829       && flag == bfd_reloc_ok)
830     flag = bfd_check_overflow (howto->complain_on_overflow,
831                                howto->bitsize,
832                                howto->rightshift,
833                                bfd_arch_bits_per_address (abfd),
834                                relocation);
835
836   /*
837     Either we are relocating all the way, or we don't want to apply
838     the relocation to the reloc entry (probably because there isn't
839     any room in the output format to describe addends to relocs)
840     */
841
842   /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
843      (OSF version 1.3, compiler version 3.11).  It miscompiles the
844      following program:
845
846      struct str
847      {
848        unsigned int i0;
849      } s = { 0 };
850
851      int
852      main ()
853      {
854        unsigned long x;
855
856        x = 0x100000000;
857        x <<= (unsigned long) s.i0;
858        if (x == 0)
859          printf ("failed\n");
860        else
861          printf ("succeeded (%lx)\n", x);
862      }
863      */
864
865   relocation >>= (bfd_vma) howto->rightshift;
866
867   /* Shift everything up to where it's going to be used */
868
869   relocation <<= (bfd_vma) howto->bitpos;
870
871   /* Wait for the day when all have the mask in them */
872
873   /* What we do:
874      i instruction to be left alone
875      o offset within instruction
876      r relocation offset to apply
877      S src mask
878      D dst mask
879      N ~dst mask
880      A part 1
881      B part 2
882      R result
883
884      Do this:
885      ((  i i i i i o o o o o  from bfd_get<size>
886      and           S S S S S) to get the size offset we want
887      +   r r r r r r r r r r) to get the final value to place
888      and           D D D D D  to chop to right size
889      -----------------------
890      =             A A A A A
891      And this:
892      (   i i i i i o o o o o  from bfd_get<size>
893      and N N N N N          ) get instruction
894      -----------------------
895      =   B B B B B
896
897      And then:
898      (   B B B B B
899      or            A A A A A)
900      -----------------------
901      =   R R R R R R R R R R  put into bfd_put<size>
902      */
903
904 #define DOIT(x) \
905   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))
906
907   switch (howto->size)
908     {
909     case 0:
910       {
911         char x = bfd_get_8 (abfd, (char *) data + octets);
912         DOIT (x);
913         bfd_put_8 (abfd, x, (unsigned char *) data + octets);
914       }
915       break;
916
917     case 1:
918       {
919         short x = bfd_get_16 (abfd, (bfd_byte *) data + octets);
920         DOIT (x);
921         bfd_put_16 (abfd, x, (unsigned char *) data + octets);
922       }
923       break;
924     case 2:
925       {
926         long x = bfd_get_32 (abfd, (bfd_byte *) data + octets);
927         DOIT (x);
928         bfd_put_32 (abfd, x, (bfd_byte *) data + octets);
929       }
930       break;
931     case -2:
932       {
933         long x = bfd_get_32 (abfd, (bfd_byte *) data + octets);
934         relocation = -relocation;
935         DOIT (x);
936         bfd_put_32 (abfd, x, (bfd_byte *) data + octets);
937       }
938       break;
939
940     case -1:
941       {
942         long x = bfd_get_16 (abfd, (bfd_byte *) data + octets);
943         relocation = -relocation;
944         DOIT (x);
945         bfd_put_16 (abfd, x, (bfd_byte *) data + octets);
946       }
947       break;
948
949     case 3:
950       /* Do nothing */
951       break;
952
953     case 4:
954 #ifdef BFD64
955       {
956         bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + octets);
957         DOIT (x);
958         bfd_put_64 (abfd, x, (bfd_byte *) data + octets);
959       }
960 #else
961       abort ();
962 #endif
963       break;
964     default:
965       return bfd_reloc_other;
966     }
967
968   return flag;
969 }
970
971 /*
972 FUNCTION
973         bfd_install_relocation
974
975 SYNOPSIS
976         bfd_reloc_status_type
977                 bfd_install_relocation
978                         (bfd *abfd,
979                          arelent *reloc_entry,
980                          PTR data, bfd_vma data_start,
981                          asection *input_section,
982                          char **error_message);
983
984 DESCRIPTION
985         This looks remarkably like <<bfd_perform_relocation>>, except it
986         does not expect that the section contents have been filled in.
987         I.e., it's suitable for use when creating, rather than applying
988         a relocation.
989
990         For now, this function should be considered reserved for the
991         assembler.
992
993 */
994
995
996 bfd_reloc_status_type
997 bfd_install_relocation (abfd, reloc_entry, data_start, data_start_offset,
998                         input_section, error_message)
999      bfd *abfd;
1000      arelent *reloc_entry;
1001      PTR data_start;
1002      bfd_vma data_start_offset;
1003      asection *input_section;
1004      char **error_message;
1005 {
1006   bfd_vma relocation;
1007   bfd_reloc_status_type flag = bfd_reloc_ok;
1008   bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1009   bfd_vma output_base = 0;
1010   reloc_howto_type *howto = reloc_entry->howto;
1011   asection *reloc_target_output_section;
1012   asymbol *symbol;
1013   bfd_byte *data;
1014
1015   symbol = *(reloc_entry->sym_ptr_ptr);
1016   if (bfd_is_abs_section (symbol->section))
1017     {
1018       reloc_entry->address += input_section->output_offset;
1019       return bfd_reloc_ok;
1020     }
1021
1022   /* If there is a function supplied to handle this relocation type,
1023      call it.  It'll return `bfd_reloc_continue' if further processing
1024      can be done.  */
1025   if (howto->special_function)
1026     {
1027       bfd_reloc_status_type cont;
1028
1029       /* XXX - The special_function calls haven't been fixed up to deal
1030          with creating new relocations and section contents.  */
1031       cont = howto->special_function (abfd, reloc_entry, symbol,
1032                                       /* XXX - Non-portable! */
1033                                       ((bfd_byte *) data_start
1034                                        - data_start_offset),
1035                                       input_section, abfd, error_message);
1036       if (cont != bfd_reloc_continue)
1037         return cont;
1038     }
1039
1040   /* Is the address of the relocation really within the section?  */
1041   if (reloc_entry->address > input_section->_cooked_size)
1042     return bfd_reloc_outofrange;
1043
1044   /* Work out which section the relocation is targetted at and the
1045      initial relocation command value.  */
1046
1047   /* Get symbol value.  (Common symbols are special.)  */
1048   if (bfd_is_com_section (symbol->section))
1049     relocation = 0;
1050   else
1051     relocation = symbol->value;
1052
1053   reloc_target_output_section = symbol->section->output_section;
1054
1055   /* Convert input-section-relative symbol value to absolute.  */
1056   if (howto->partial_inplace == false)
1057     output_base = 0;
1058   else
1059     output_base = reloc_target_output_section->vma;
1060
1061   relocation += output_base + symbol->section->output_offset;
1062
1063   /* Add in supplied addend.  */
1064   relocation += reloc_entry->addend;
1065
1066   /* Here the variable relocation holds the final address of the
1067      symbol we are relocating against, plus any addend.  */
1068
1069   if (howto->pc_relative == true)
1070     {
1071       /* This is a PC relative relocation.  We want to set RELOCATION
1072          to the distance between the address of the symbol and the
1073          location.  RELOCATION is already the address of the symbol.
1074
1075          We start by subtracting the address of the section containing
1076          the location.
1077
1078          If pcrel_offset is set, we must further subtract the position
1079          of the location within the section.  Some targets arrange for
1080          the addend to be the negative of the position of the location
1081          within the section; for example, i386-aout does this.  For
1082          i386-aout, pcrel_offset is false.  Some other targets do not
1083          include the position of the location; for example, m88kbcs,
1084          or ELF.  For those targets, pcrel_offset is true.
1085
1086          If we are producing relocateable output, then we must ensure
1087          that this reloc will be correctly computed when the final
1088          relocation is done.  If pcrel_offset is false we want to wind
1089          up with the negative of the location within the section,
1090          which means we must adjust the existing addend by the change
1091          in the location within the section.  If pcrel_offset is true
1092          we do not want to adjust the existing addend at all.
1093
1094          FIXME: This seems logical to me, but for the case of
1095          producing relocateable output it is not what the code
1096          actually does.  I don't want to change it, because it seems
1097          far too likely that something will break.  */
1098
1099       relocation -=
1100         input_section->output_section->vma + input_section->output_offset;
1101
1102       if (howto->pcrel_offset == true && howto->partial_inplace == true)
1103         relocation -= reloc_entry->address;
1104     }
1105
1106   if (howto->partial_inplace == false)
1107     {
1108       /* This is a partial relocation, and we want to apply the relocation
1109          to the reloc entry rather than the raw data. Modify the reloc
1110          inplace to reflect what we now know.  */
1111       reloc_entry->addend = relocation;
1112       reloc_entry->address += input_section->output_offset;
1113       return flag;
1114     }
1115   else
1116     {
1117       /* This is a partial relocation, but inplace, so modify the
1118          reloc record a bit.
1119
1120          If we've relocated with a symbol with a section, change
1121          into a ref to the section belonging to the symbol.  */
1122
1123       reloc_entry->address += input_section->output_offset;
1124
1125       /* WTF?? */
1126       if (abfd->xvec->flavour == bfd_target_coff_flavour
1127           && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
1128           && strcmp (abfd->xvec->name, "xcoff-powermac") != 0
1129           && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
1130           && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
1131         {
1132 #if 1
1133 /* For m68k-coff, the addend was being subtracted twice during
1134    relocation with -r.  Removing the line below this comment
1135    fixes that problem; see PR 2953.
1136
1137 However, Ian wrote the following, regarding removing the line below,
1138 which explains why it is still enabled:  --djm
1139
1140 If you put a patch like that into BFD you need to check all the COFF
1141 linkers.  I am fairly certain that patch will break coff-i386 (e.g.,
1142 SCO); see coff_i386_reloc in coff-i386.c where I worked around the
1143 problem in a different way.  There may very well be a reason that the
1144 code works as it does.
1145
1146 Hmmm.  The first obvious point is that bfd_install_relocation should
1147 not have any tests that depend upon the flavour.  It's seem like
1148 entirely the wrong place for such a thing.  The second obvious point
1149 is that the current code ignores the reloc addend when producing
1150 relocateable output for COFF.  That's peculiar.  In fact, I really
1151 have no idea what the point of the line you want to remove is.
1152
1153 A typical COFF reloc subtracts the old value of the symbol and adds in
1154 the new value to the location in the object file (if it's a pc
1155 relative reloc it adds the difference between the symbol value and the
1156 location).  When relocating we need to preserve that property.
1157
1158 BFD handles this by setting the addend to the negative of the old
1159 value of the symbol.  Unfortunately it handles common symbols in a
1160 non-standard way (it doesn't subtract the old value) but that's a
1161 different story (we can't change it without losing backward
1162 compatibility with old object files) (coff-i386 does subtract the old
1163 value, to be compatible with existing coff-i386 targets, like SCO).
1164
1165 So everything works fine when not producing relocateable output.  When
1166 we are producing relocateable output, logically we should do exactly
1167 what we do when not producing relocateable output.  Therefore, your
1168 patch is correct.  In fact, it should probably always just set
1169 reloc_entry->addend to 0 for all cases, since it is, in fact, going to
1170 add the value into the object file.  This won't hurt the COFF code,
1171 which doesn't use the addend; I'm not sure what it will do to other
1172 formats (the thing to check for would be whether any formats both use
1173 the addend and set partial_inplace).
1174
1175 When I wanted to make coff-i386 produce relocateable output, I ran
1176 into the problem that you are running into: I wanted to remove that
1177 line.  Rather than risk it, I made the coff-i386 relocs use a special
1178 function; it's coff_i386_reloc in coff-i386.c.  The function
1179 specifically adds the addend field into the object file, knowing that
1180 bfd_install_relocation is not going to.  If you remove that line, then
1181 coff-i386.c will wind up adding the addend field in twice.  It's
1182 trivial to fix; it just needs to be done.
1183
1184 The problem with removing the line is just that it may break some
1185 working code.  With BFD it's hard to be sure of anything.  The right
1186 way to deal with this is simply to build and test at least all the
1187 supported COFF targets.  It should be straightforward if time and disk
1188 space consuming.  For each target:
1189     1) build the linker
1190     2) generate some executable, and link it using -r (I would
1191        probably use paranoia.o and link against newlib/libc.a, which
1192        for all the supported targets would be available in
1193        /usr/cygnus/progressive/H-host/target/lib/libc.a).
1194     3) make the change to reloc.c
1195     4) rebuild the linker
1196     5) repeat step 2
1197     6) if the resulting object files are the same, you have at least
1198        made it no worse
1199     7) if they are different you have to figure out which version is
1200        right
1201 */
1202           relocation -= reloc_entry->addend;
1203 #endif
1204           reloc_entry->addend = 0;
1205         }
1206       else
1207         {
1208           reloc_entry->addend = relocation;
1209         }
1210     }
1211
1212   /* FIXME: This overflow checking is incomplete, because the value
1213      might have overflowed before we get here.  For a correct check we
1214      need to compute the value in a size larger than bitsize, but we
1215      can't reasonably do that for a reloc the same size as a host
1216      machine word.
1217      FIXME: We should also do overflow checking on the result after
1218      adding in the value contained in the object file.  */
1219   if (howto->complain_on_overflow != complain_overflow_dont)
1220     flag = bfd_check_overflow (howto->complain_on_overflow,
1221                                howto->bitsize,
1222                                howto->rightshift,
1223                                bfd_arch_bits_per_address (abfd),
1224                                relocation);
1225
1226   /*
1227     Either we are relocating all the way, or we don't want to apply
1228     the relocation to the reloc entry (probably because there isn't
1229     any room in the output format to describe addends to relocs)
1230     */
1231
1232   /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
1233      (OSF version 1.3, compiler version 3.11).  It miscompiles the
1234      following program:
1235
1236      struct str
1237      {
1238        unsigned int i0;
1239      } s = { 0 };
1240
1241      int
1242      main ()
1243      {
1244        unsigned long x;
1245
1246        x = 0x100000000;
1247        x <<= (unsigned long) s.i0;
1248        if (x == 0)
1249          printf ("failed\n");
1250        else
1251          printf ("succeeded (%lx)\n", x);
1252      }
1253      */
1254
1255   relocation >>= (bfd_vma) howto->rightshift;
1256
1257   /* Shift everything up to where it's going to be used */
1258
1259   relocation <<= (bfd_vma) howto->bitpos;
1260
1261   /* Wait for the day when all have the mask in them */
1262
1263   /* What we do:
1264      i instruction to be left alone
1265      o offset within instruction
1266      r relocation offset to apply
1267      S src mask
1268      D dst mask
1269      N ~dst mask
1270      A part 1
1271      B part 2
1272      R result
1273
1274      Do this:
1275      ((  i i i i i o o o o o  from bfd_get<size>
1276      and           S S S S S) to get the size offset we want
1277      +   r r r r r r r r r r) to get the final value to place
1278      and           D D D D D  to chop to right size
1279      -----------------------
1280      =             A A A A A
1281      And this:
1282      (   i i i i i o o o o o  from bfd_get<size>
1283      and N N N N N          ) get instruction
1284      -----------------------
1285      =   B B B B B
1286
1287      And then:
1288      (   B B B B B
1289      or            A A A A A)
1290      -----------------------
1291      =   R R R R R R R R R R  put into bfd_put<size>
1292      */
1293
1294 #define DOIT(x) \
1295   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))
1296
1297   data = (bfd_byte *) data_start + (octets - data_start_offset);
1298
1299   switch (howto->size)
1300     {
1301     case 0:
1302       {
1303         char x = bfd_get_8 (abfd, (char *) data);
1304         DOIT (x);
1305         bfd_put_8 (abfd, x, (unsigned char *) data);
1306       }
1307       break;
1308
1309     case 1:
1310       {
1311         short x = bfd_get_16 (abfd, (bfd_byte *) data);
1312         DOIT (x);
1313         bfd_put_16 (abfd, x, (unsigned char *) data);
1314       }
1315       break;
1316     case 2:
1317       {
1318         long x = bfd_get_32 (abfd, (bfd_byte *) data);
1319         DOIT (x);
1320         bfd_put_32 (abfd, x, (bfd_byte *) data);
1321       }
1322       break;
1323     case -2:
1324       {
1325         long x = bfd_get_32 (abfd, (bfd_byte *) data);
1326         relocation = -relocation;
1327         DOIT (x);
1328         bfd_put_32 (abfd, x, (bfd_byte *) data);
1329       }
1330       break;
1331
1332     case 3:
1333       /* Do nothing */
1334       break;
1335
1336     case 4:
1337       {
1338         bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data);
1339         DOIT (x);
1340         bfd_put_64 (abfd, x, (bfd_byte *) data);
1341       }
1342       break;
1343     default:
1344       return bfd_reloc_other;
1345     }
1346
1347   return flag;
1348 }
1349
1350 /* This relocation routine is used by some of the backend linkers.
1351    They do not construct asymbol or arelent structures, so there is no
1352    reason for them to use bfd_perform_relocation.  Also,
1353    bfd_perform_relocation is so hacked up it is easier to write a new
1354    function than to try to deal with it.
1355
1356    This routine does a final relocation.  Whether it is useful for a
1357    relocateable link depends upon how the object format defines
1358    relocations.
1359
1360    FIXME: This routine ignores any special_function in the HOWTO,
1361    since the existing special_function values have been written for
1362    bfd_perform_relocation.
1363
1364    HOWTO is the reloc howto information.
1365    INPUT_BFD is the BFD which the reloc applies to.
1366    INPUT_SECTION is the section which the reloc applies to.
1367    CONTENTS is the contents of the section.
1368    ADDRESS is the address of the reloc within INPUT_SECTION.
1369    VALUE is the value of the symbol the reloc refers to.
1370    ADDEND is the addend of the reloc.  */
1371
1372 bfd_reloc_status_type
1373 _bfd_final_link_relocate (howto, input_bfd, input_section, contents, address,
1374                           value, addend)
1375      reloc_howto_type *howto;
1376      bfd *input_bfd;
1377      asection *input_section;
1378      bfd_byte *contents;
1379      bfd_vma address;
1380      bfd_vma value;
1381      bfd_vma addend;
1382 {
1383   bfd_vma relocation;
1384
1385   /* Sanity check the address.  */
1386   if (address > input_section->_raw_size)
1387     return bfd_reloc_outofrange;
1388
1389   /* This function assumes that we are dealing with a basic relocation
1390      against a symbol.  We want to compute the value of the symbol to
1391      relocate to.  This is just VALUE, the value of the symbol, plus
1392      ADDEND, any addend associated with the reloc.  */
1393   relocation = value + addend;
1394
1395   /* If the relocation is PC relative, we want to set RELOCATION to
1396      the distance between the symbol (currently in RELOCATION) and the
1397      location we are relocating.  Some targets (e.g., i386-aout)
1398      arrange for the contents of the section to be the negative of the
1399      offset of the location within the section; for such targets
1400      pcrel_offset is false.  Other targets (e.g., m88kbcs or ELF)
1401      simply leave the contents of the section as zero; for such
1402      targets pcrel_offset is true.  If pcrel_offset is false we do not
1403      need to subtract out the offset of the location within the
1404      section (which is just ADDRESS).  */
1405   if (howto->pc_relative)
1406     {
1407       relocation -= (input_section->output_section->vma
1408                      + input_section->output_offset);
1409       if (howto->pcrel_offset)
1410         relocation -= address;
1411     }
1412
1413   return _bfd_relocate_contents (howto, input_bfd, relocation,
1414                                  contents + address);
1415 }
1416
1417 /* Relocate a given location using a given value and howto.  */
1418
1419 bfd_reloc_status_type
1420 _bfd_relocate_contents (howto, input_bfd, relocation, location)
1421      reloc_howto_type *howto;
1422      bfd *input_bfd;
1423      bfd_vma relocation;
1424      bfd_byte *location;
1425 {
1426   int size;
1427   bfd_vma x = 0;
1428   bfd_reloc_status_type flag;
1429   unsigned int rightshift = howto->rightshift;
1430   unsigned int bitpos = howto->bitpos;
1431
1432   /* If the size is negative, negate RELOCATION.  This isn't very
1433      general.  */
1434   if (howto->size < 0)
1435     relocation = -relocation;
1436
1437   /* Get the value we are going to relocate.  */
1438   size = bfd_get_reloc_size (howto);
1439   switch (size)
1440     {
1441     default:
1442     case 0:
1443       abort ();
1444     case 1:
1445       x = bfd_get_8 (input_bfd, location);
1446       break;
1447     case 2:
1448       x = bfd_get_16 (input_bfd, location);
1449       break;
1450     case 4:
1451       x = bfd_get_32 (input_bfd, location);
1452       break;
1453     case 8:
1454 #ifdef BFD64
1455       x = bfd_get_64 (input_bfd, location);
1456 #else
1457       abort ();
1458 #endif
1459       break;
1460     }
1461
1462   /* Check for overflow.  FIXME: We may drop bits during the addition
1463      which we don't check for.  We must either check at every single
1464      operation, which would be tedious, or we must do the computations
1465      in a type larger than bfd_vma, which would be inefficient.  */
1466   flag = bfd_reloc_ok;
1467   if (howto->complain_on_overflow != complain_overflow_dont)
1468     {
1469       bfd_vma addrmask, fieldmask, signmask, ss;
1470       bfd_vma a, b, sum;
1471
1472       /* Get the values to be added together.  For signed and unsigned
1473          relocations, we assume that all values should be truncated to
1474          the size of an address.  For bitfields, all the bits matter.
1475          See also bfd_check_overflow.  */
1476       fieldmask = N_ONES (howto->bitsize);
1477       addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
1478       a = relocation;
1479       b = x & howto->src_mask;
1480
1481       switch (howto->complain_on_overflow)
1482         {
1483         case complain_overflow_signed:
1484           a = (a & addrmask) >> rightshift;
1485
1486           /* If any sign bits are set, all sign bits must be set.
1487              That is, A must be a valid negative address after
1488              shifting.  */
1489           signmask = ~ (fieldmask >> 1);
1490           ss = a & signmask;
1491           if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
1492             flag = bfd_reloc_overflow;
1493
1494           /* We only need this next bit of code if the sign bit of B
1495              is below the sign bit of A.  This would only happen if
1496              SRC_MASK had fewer bits than BITSIZE.  Note that if
1497              SRC_MASK has more bits than BITSIZE, we can get into
1498              trouble; we would need to verify that B is in range, as
1499              we do for A above.  */
1500           signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
1501           if ((b & signmask) != 0)
1502             {
1503               /* Set all the bits above the sign bit.  */
1504               b -= signmask << 1;
1505             }
1506
1507           b = (b & addrmask) >> bitpos;
1508
1509           /* Now we can do the addition.  */
1510           sum = a + b;
1511
1512           /* See if the result has the correct sign.  Bits above the
1513              sign bit are junk now; ignore them.  If the sum is
1514              positive, make sure we did not have all negative inputs;
1515              if the sum is negative, make sure we did not have all
1516              positive inputs.  The test below looks only at the sign
1517              bits, and it really just
1518                  SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
1519              */
1520           signmask = (fieldmask >> 1) + 1;
1521           if (((~ (a ^ b)) & (a ^ sum)) & signmask)
1522             flag = bfd_reloc_overflow;
1523
1524           break;
1525
1526         case complain_overflow_unsigned:
1527           /* Checking for an unsigned overflow is relatively easy:
1528              trim the addresses and add, and trim the result as well.
1529              Overflow is normally indicated when the result does not
1530              fit in the field.  However, we also need to consider the
1531              case when, e.g., fieldmask is 0x7fffffff or smaller, an
1532              input is 0x80000000, and bfd_vma is only 32 bits; then we
1533              will get sum == 0, but there is an overflow, since the
1534              inputs did not fit in the field.  Instead of doing a
1535              separate test, we can check for this by or-ing in the
1536              operands when testing for the sum overflowing its final
1537              field.  */
1538           a = (a & addrmask) >> rightshift;
1539           b = (b & addrmask) >> bitpos;
1540           sum = (a + b) & addrmask;
1541           if ((a | b | sum) & ~ fieldmask)
1542             flag = bfd_reloc_overflow;
1543
1544           break;
1545
1546         case complain_overflow_bitfield:
1547           /* Much like the signed check, but for a field one bit
1548              wider, and no trimming with addrmask.  We allow a
1549              bitfield to represent numbers in the range -2**n to
1550              2**n-1, where n is the number of bits in the field.
1551              Note that when bfd_vma is 32 bits, a 32-bit reloc can't
1552              overflow, which is exactly what we want.  */
1553           a >>= rightshift;
1554
1555           signmask = ~ fieldmask;
1556           ss = a & signmask;
1557           if (ss != 0 && ss != (((bfd_vma) -1 >> rightshift) & signmask))
1558             flag = bfd_reloc_overflow;
1559
1560           signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
1561           if ((b & signmask) != 0)
1562             b -= signmask << 1;
1563
1564           b >>= bitpos;
1565
1566           sum = a + b;
1567
1568           signmask = fieldmask + 1;
1569           if (((~ (a ^ b)) & (a ^ sum)) & signmask)
1570             flag = bfd_reloc_overflow;
1571
1572           break;
1573
1574         default:
1575           abort ();
1576         }
1577     }
1578
1579   /* Put RELOCATION in the right bits.  */
1580   relocation >>= (bfd_vma) rightshift;
1581   relocation <<= (bfd_vma) bitpos;
1582
1583   /* Add RELOCATION to the right bits of X.  */
1584   x = ((x & ~howto->dst_mask)
1585        | (((x & howto->src_mask) + relocation) & howto->dst_mask));
1586
1587   /* Put the relocated value back in the object file.  */
1588   switch (size)
1589     {
1590     default:
1591     case 0:
1592       abort ();
1593     case 1:
1594       bfd_put_8 (input_bfd, x, location);
1595       break;
1596     case 2:
1597       bfd_put_16 (input_bfd, x, location);
1598       break;
1599     case 4:
1600       bfd_put_32 (input_bfd, x, location);
1601       break;
1602     case 8:
1603 #ifdef BFD64
1604       bfd_put_64 (input_bfd, x, location);
1605 #else
1606       abort ();
1607 #endif
1608       break;
1609     }
1610
1611   return flag;
1612 }
1613
1614 /*
1615 DOCDD
1616 INODE
1617         howto manager,  , typedef arelent, Relocations
1618
1619 SECTION
1620         The howto manager
1621
1622         When an application wants to create a relocation, but doesn't
1623         know what the target machine might call it, it can find out by
1624         using this bit of code.
1625
1626 */
1627
1628 /*
1629 TYPEDEF
1630         bfd_reloc_code_type
1631
1632 DESCRIPTION
1633         The insides of a reloc code.  The idea is that, eventually, there
1634         will be one enumerator for every type of relocation we ever do.
1635         Pass one of these values to <<bfd_reloc_type_lookup>>, and it'll
1636         return a howto pointer.
1637
1638         This does mean that the application must determine the correct
1639         enumerator value; you can't get a howto pointer from a random set
1640         of attributes.
1641
1642 SENUM
1643    bfd_reloc_code_real
1644
1645 ENUM
1646   BFD_RELOC_64
1647 ENUMX
1648   BFD_RELOC_32
1649 ENUMX
1650   BFD_RELOC_26
1651 ENUMX
1652   BFD_RELOC_24
1653 ENUMX
1654   BFD_RELOC_16
1655 ENUMX
1656   BFD_RELOC_14
1657 ENUMX
1658   BFD_RELOC_8
1659 ENUMDOC
1660   Basic absolute relocations of N bits.
1661
1662 ENUM
1663   BFD_RELOC_64_PCREL
1664 ENUMX
1665   BFD_RELOC_32_PCREL
1666 ENUMX
1667   BFD_RELOC_24_PCREL
1668 ENUMX
1669   BFD_RELOC_16_PCREL
1670 ENUMX
1671   BFD_RELOC_12_PCREL
1672 ENUMX
1673   BFD_RELOC_8_PCREL
1674 ENUMDOC
1675   PC-relative relocations.  Sometimes these are relative to the address
1676 of the relocation itself; sometimes they are relative to the start of
1677 the section containing the relocation.  It depends on the specific target.
1678
1679 The 24-bit relocation is used in some Intel 960 configurations.
1680
1681 ENUM
1682   BFD_RELOC_32_GOT_PCREL
1683 ENUMX
1684   BFD_RELOC_16_GOT_PCREL
1685 ENUMX
1686   BFD_RELOC_8_GOT_PCREL
1687 ENUMX
1688   BFD_RELOC_32_GOTOFF
1689 ENUMX
1690   BFD_RELOC_16_GOTOFF
1691 ENUMX
1692   BFD_RELOC_LO16_GOTOFF
1693 ENUMX
1694   BFD_RELOC_HI16_GOTOFF
1695 ENUMX
1696   BFD_RELOC_HI16_S_GOTOFF
1697 ENUMX
1698   BFD_RELOC_8_GOTOFF
1699 ENUMX
1700   BFD_RELOC_32_PLT_PCREL
1701 ENUMX
1702   BFD_RELOC_24_PLT_PCREL
1703 ENUMX
1704   BFD_RELOC_16_PLT_PCREL
1705 ENUMX
1706   BFD_RELOC_8_PLT_PCREL
1707 ENUMX
1708   BFD_RELOC_32_PLTOFF
1709 ENUMX
1710   BFD_RELOC_16_PLTOFF
1711 ENUMX
1712   BFD_RELOC_LO16_PLTOFF
1713 ENUMX
1714   BFD_RELOC_HI16_PLTOFF
1715 ENUMX
1716   BFD_RELOC_HI16_S_PLTOFF
1717 ENUMX
1718   BFD_RELOC_8_PLTOFF
1719 ENUMDOC
1720   For ELF.
1721
1722 ENUM
1723   BFD_RELOC_68K_GLOB_DAT
1724 ENUMX
1725   BFD_RELOC_68K_JMP_SLOT
1726 ENUMX
1727   BFD_RELOC_68K_RELATIVE
1728 ENUMDOC
1729   Relocations used by 68K ELF.
1730
1731 ENUM
1732   BFD_RELOC_32_BASEREL
1733 ENUMX
1734   BFD_RELOC_16_BASEREL
1735 ENUMX
1736   BFD_RELOC_LO16_BASEREL
1737 ENUMX
1738   BFD_RELOC_HI16_BASEREL
1739 ENUMX
1740   BFD_RELOC_HI16_S_BASEREL
1741 ENUMX
1742   BFD_RELOC_8_BASEREL
1743 ENUMX
1744   BFD_RELOC_RVA
1745 ENUMDOC
1746   Linkage-table relative.
1747
1748 ENUM
1749   BFD_RELOC_8_FFnn
1750 ENUMDOC
1751   Absolute 8-bit relocation, but used to form an address like 0xFFnn.
1752
1753 ENUM
1754   BFD_RELOC_32_PCREL_S2
1755 ENUMX
1756   BFD_RELOC_16_PCREL_S2
1757 ENUMX
1758   BFD_RELOC_23_PCREL_S2
1759 ENUMDOC
1760   These PC-relative relocations are stored as word displacements --
1761 i.e., byte displacements shifted right two bits.  The 30-bit word
1762 displacement (<<32_PCREL_S2>> -- 32 bits, shifted 2) is used on the
1763 SPARC.  (SPARC tools generally refer to this as <<WDISP30>>.)  The
1764 signed 16-bit displacement is used on the MIPS, and the 23-bit
1765 displacement is used on the Alpha.
1766
1767 ENUM
1768   BFD_RELOC_HI22
1769 ENUMX
1770   BFD_RELOC_LO10
1771 ENUMDOC
1772   High 22 bits and low 10 bits of 32-bit value, placed into lower bits of
1773 the target word.  These are used on the SPARC.
1774
1775 ENUM
1776   BFD_RELOC_GPREL16
1777 ENUMX
1778   BFD_RELOC_GPREL32
1779 ENUMDOC
1780   For systems that allocate a Global Pointer register, these are
1781 displacements off that register.  These relocation types are
1782 handled specially, because the value the register will have is
1783 decided relatively late.
1784
1785
1786 ENUM
1787   BFD_RELOC_I960_CALLJ
1788 ENUMDOC
1789   Reloc types used for i960/b.out.
1790
1791 ENUM
1792   BFD_RELOC_NONE
1793 ENUMX
1794   BFD_RELOC_SPARC_WDISP22
1795 ENUMX
1796   BFD_RELOC_SPARC22
1797 ENUMX
1798   BFD_RELOC_SPARC13
1799 ENUMX
1800   BFD_RELOC_SPARC_GOT10
1801 ENUMX
1802   BFD_RELOC_SPARC_GOT13
1803 ENUMX
1804   BFD_RELOC_SPARC_GOT22
1805 ENUMX
1806   BFD_RELOC_SPARC_PC10
1807 ENUMX
1808   BFD_RELOC_SPARC_PC22
1809 ENUMX
1810   BFD_RELOC_SPARC_WPLT30
1811 ENUMX
1812   BFD_RELOC_SPARC_COPY
1813 ENUMX
1814   BFD_RELOC_SPARC_GLOB_DAT
1815 ENUMX
1816   BFD_RELOC_SPARC_JMP_SLOT
1817 ENUMX
1818   BFD_RELOC_SPARC_RELATIVE
1819 ENUMX
1820   BFD_RELOC_SPARC_UA32
1821 ENUMDOC
1822   SPARC ELF relocations.  There is probably some overlap with other
1823   relocation types already defined.
1824
1825 ENUM
1826   BFD_RELOC_SPARC_BASE13
1827 ENUMX
1828   BFD_RELOC_SPARC_BASE22
1829 ENUMDOC
1830   I think these are specific to SPARC a.out (e.g., Sun 4).
1831
1832 ENUMEQ
1833   BFD_RELOC_SPARC_64
1834   BFD_RELOC_64
1835 ENUMX
1836   BFD_RELOC_SPARC_10
1837 ENUMX
1838   BFD_RELOC_SPARC_11
1839 ENUMX
1840   BFD_RELOC_SPARC_OLO10
1841 ENUMX
1842   BFD_RELOC_SPARC_HH22
1843 ENUMX
1844   BFD_RELOC_SPARC_HM10
1845 ENUMX
1846   BFD_RELOC_SPARC_LM22
1847 ENUMX
1848   BFD_RELOC_SPARC_PC_HH22
1849 ENUMX
1850   BFD_RELOC_SPARC_PC_HM10
1851 ENUMX
1852   BFD_RELOC_SPARC_PC_LM22
1853 ENUMX
1854   BFD_RELOC_SPARC_WDISP16
1855 ENUMX
1856   BFD_RELOC_SPARC_WDISP19
1857 ENUMX
1858   BFD_RELOC_SPARC_7
1859 ENUMX
1860   BFD_RELOC_SPARC_6
1861 ENUMX
1862   BFD_RELOC_SPARC_5
1863 ENUMEQX
1864   BFD_RELOC_SPARC_DISP64
1865   BFD_RELOC_64_PCREL
1866 ENUMX
1867   BFD_RELOC_SPARC_PLT64
1868 ENUMX
1869   BFD_RELOC_SPARC_HIX22
1870 ENUMX
1871   BFD_RELOC_SPARC_LOX10
1872 ENUMX
1873   BFD_RELOC_SPARC_H44
1874 ENUMX
1875   BFD_RELOC_SPARC_M44
1876 ENUMX
1877   BFD_RELOC_SPARC_L44
1878 ENUMX
1879   BFD_RELOC_SPARC_REGISTER
1880 ENUMDOC
1881   SPARC64 relocations
1882
1883 ENUM
1884   BFD_RELOC_SPARC_REV32
1885 ENUMDOC
1886   SPARC little endian relocation
1887
1888 ENUM
1889   BFD_RELOC_ALPHA_GPDISP_HI16
1890 ENUMDOC
1891   Alpha ECOFF and ELF relocations.  Some of these treat the symbol or
1892      "addend" in some special way.
1893   For GPDISP_HI16 ("gpdisp") relocations, the symbol is ignored when
1894      writing; when reading, it will be the absolute section symbol.  The
1895      addend is the displacement in bytes of the "lda" instruction from
1896      the "ldah" instruction (which is at the address of this reloc).
1897 ENUM
1898   BFD_RELOC_ALPHA_GPDISP_LO16
1899 ENUMDOC
1900   For GPDISP_LO16 ("ignore") relocations, the symbol is handled as
1901      with GPDISP_HI16 relocs.  The addend is ignored when writing the
1902      relocations out, and is filled in with the file's GP value on
1903      reading, for convenience.
1904
1905 ENUM
1906   BFD_RELOC_ALPHA_GPDISP
1907 ENUMDOC
1908   The ELF GPDISP relocation is exactly the same as the GPDISP_HI16
1909      relocation except that there is no accompanying GPDISP_LO16
1910      relocation.
1911
1912 ENUM
1913   BFD_RELOC_ALPHA_LITERAL
1914 ENUMX
1915   BFD_RELOC_ALPHA_ELF_LITERAL
1916 ENUMX
1917   BFD_RELOC_ALPHA_LITUSE
1918 ENUMDOC
1919   The Alpha LITERAL/LITUSE relocs are produced by a symbol reference;
1920      the assembler turns it into a LDQ instruction to load the address of
1921      the symbol, and then fills in a register in the real instruction.
1922
1923      The LITERAL reloc, at the LDQ instruction, refers to the .lita
1924      section symbol.  The addend is ignored when writing, but is filled
1925      in with the file's GP value on reading, for convenience, as with the
1926      GPDISP_LO16 reloc.
1927
1928      The ELF_LITERAL reloc is somewhere between 16_GOTOFF and GPDISP_LO16.
1929      It should refer to the symbol to be referenced, as with 16_GOTOFF,
1930      but it generates output not based on the position within the .got
1931      section, but relative to the GP value chosen for the file during the
1932      final link stage.
1933
1934      The LITUSE reloc, on the instruction using the loaded address, gives
1935      information to the linker that it might be able to use to optimize
1936      away some literal section references.  The symbol is ignored (read
1937      as the absolute section symbol), and the "addend" indicates the type
1938      of instruction using the register:
1939               1 - "memory" fmt insn
1940               2 - byte-manipulation (byte offset reg)
1941               3 - jsr (target of branch)
1942
1943      The GNU linker currently doesn't do any of this optimizing.
1944
1945 ENUM
1946   BFD_RELOC_ALPHA_USER_LITERAL
1947 ENUMX
1948   BFD_RELOC_ALPHA_USER_LITUSE_BASE
1949 ENUMX
1950   BFD_RELOC_ALPHA_USER_LITUSE_BYTOFF
1951 ENUMX
1952   BFD_RELOC_ALPHA_USER_LITUSE_JSR
1953 ENUMX
1954   BFD_RELOC_ALPHA_USER_GPDISP
1955 ENUMX
1956   BFD_RELOC_ALPHA_USER_GPRELHIGH
1957 ENUMX
1958   BFD_RELOC_ALPHA_USER_GPRELLOW
1959 ENUMDOC
1960   The BFD_RELOC_ALPHA_USER_* relocations are used by the assembler to
1961      process the explicit !<reloc>!sequence relocations, and are mapped
1962      into the normal relocations at the end of processing.
1963
1964 ENUM
1965   BFD_RELOC_ALPHA_HINT
1966 ENUMDOC
1967   The HINT relocation indicates a value that should be filled into the
1968      "hint" field of a jmp/jsr/ret instruction, for possible branch-
1969      prediction logic which may be provided on some processors.
1970
1971 ENUM
1972   BFD_RELOC_ALPHA_LINKAGE
1973 ENUMDOC
1974   The LINKAGE relocation outputs a linkage pair in the object file,
1975      which is filled by the linker.
1976
1977 ENUM
1978   BFD_RELOC_ALPHA_CODEADDR
1979 ENUMDOC
1980   The CODEADDR relocation outputs a STO_CA in the object file,
1981      which is filled by the linker.
1982
1983 ENUM
1984   BFD_RELOC_MIPS_JMP
1985 ENUMDOC
1986   Bits 27..2 of the relocation address shifted right 2 bits;
1987      simple reloc otherwise.
1988
1989 ENUM
1990   BFD_RELOC_MIPS16_JMP
1991 ENUMDOC
1992   The MIPS16 jump instruction.
1993
1994 ENUM
1995   BFD_RELOC_MIPS16_GPREL
1996 ENUMDOC
1997   MIPS16 GP relative reloc.
1998
1999 ENUM
2000   BFD_RELOC_HI16
2001 ENUMDOC
2002   High 16 bits of 32-bit value; simple reloc.
2003 ENUM
2004   BFD_RELOC_HI16_S
2005 ENUMDOC
2006   High 16 bits of 32-bit value but the low 16 bits will be sign
2007      extended and added to form the final result.  If the low 16
2008      bits form a negative number, we need to add one to the high value
2009      to compensate for the borrow when the low bits are added.
2010 ENUM
2011   BFD_RELOC_LO16
2012 ENUMDOC
2013   Low 16 bits.
2014 ENUM
2015   BFD_RELOC_PCREL_HI16_S
2016 ENUMDOC
2017   Like BFD_RELOC_HI16_S, but PC relative.
2018 ENUM
2019   BFD_RELOC_PCREL_LO16
2020 ENUMDOC
2021   Like BFD_RELOC_LO16, but PC relative.
2022
2023 ENUMEQ
2024   BFD_RELOC_MIPS_GPREL
2025   BFD_RELOC_GPREL16
2026 ENUMDOC
2027   Relocation relative to the global pointer.
2028
2029 ENUM
2030   BFD_RELOC_MIPS_LITERAL
2031 ENUMDOC
2032   Relocation against a MIPS literal section.
2033
2034 ENUM
2035   BFD_RELOC_MIPS_GOT16
2036 ENUMX
2037   BFD_RELOC_MIPS_CALL16
2038 ENUMEQX
2039   BFD_RELOC_MIPS_GPREL32
2040   BFD_RELOC_GPREL32
2041 ENUMX
2042   BFD_RELOC_MIPS_GOT_HI16
2043 ENUMX
2044   BFD_RELOC_MIPS_GOT_LO16
2045 ENUMX
2046   BFD_RELOC_MIPS_CALL_HI16
2047 ENUMX
2048   BFD_RELOC_MIPS_CALL_LO16
2049 ENUMX
2050   BFD_RELOC_MIPS_SUB
2051 ENUMX
2052   BFD_RELOC_MIPS_GOT_PAGE
2053 ENUMX
2054   BFD_RELOC_MIPS_GOT_OFST
2055 ENUMX
2056   BFD_RELOC_MIPS_GOT_DISP
2057 COMMENT
2058 ENUMDOC
2059   MIPS ELF relocations.
2060
2061 COMMENT
2062
2063 ENUM
2064   BFD_RELOC_386_GOT32
2065 ENUMX
2066   BFD_RELOC_386_PLT32
2067 ENUMX
2068   BFD_RELOC_386_COPY
2069 ENUMX
2070   BFD_RELOC_386_GLOB_DAT
2071 ENUMX
2072   BFD_RELOC_386_JUMP_SLOT
2073 ENUMX
2074   BFD_RELOC_386_RELATIVE
2075 ENUMX
2076   BFD_RELOC_386_GOTOFF
2077 ENUMX
2078   BFD_RELOC_386_GOTPC
2079 ENUMDOC
2080   i386/elf relocations
2081
2082 ENUM
2083   BFD_RELOC_NS32K_IMM_8
2084 ENUMX
2085   BFD_RELOC_NS32K_IMM_16
2086 ENUMX
2087   BFD_RELOC_NS32K_IMM_32
2088 ENUMX
2089   BFD_RELOC_NS32K_IMM_8_PCREL
2090 ENUMX
2091   BFD_RELOC_NS32K_IMM_16_PCREL
2092 ENUMX
2093   BFD_RELOC_NS32K_IMM_32_PCREL
2094 ENUMX
2095   BFD_RELOC_NS32K_DISP_8
2096 ENUMX
2097   BFD_RELOC_NS32K_DISP_16
2098 ENUMX
2099   BFD_RELOC_NS32K_DISP_32
2100 ENUMX
2101   BFD_RELOC_NS32K_DISP_8_PCREL
2102 ENUMX
2103   BFD_RELOC_NS32K_DISP_16_PCREL
2104 ENUMX
2105   BFD_RELOC_NS32K_DISP_32_PCREL
2106 ENUMDOC
2107   ns32k relocations
2108
2109 ENUM
2110   BFD_RELOC_PJ_CODE_HI16
2111 ENUMX
2112   BFD_RELOC_PJ_CODE_LO16
2113 ENUMX
2114   BFD_RELOC_PJ_CODE_DIR16
2115 ENUMX
2116   BFD_RELOC_PJ_CODE_DIR32
2117 ENUMX
2118   BFD_RELOC_PJ_CODE_REL16
2119 ENUMX
2120   BFD_RELOC_PJ_CODE_REL32
2121 ENUMDOC
2122   Picojava relocs.  Not all of these appear in object files.
2123
2124 ENUM
2125   BFD_RELOC_PPC_B26
2126 ENUMX
2127   BFD_RELOC_PPC_BA26
2128 ENUMX
2129   BFD_RELOC_PPC_TOC16
2130 ENUMX
2131   BFD_RELOC_PPC_B16
2132 ENUMX
2133   BFD_RELOC_PPC_B16_BRTAKEN
2134 ENUMX
2135   BFD_RELOC_PPC_B16_BRNTAKEN
2136 ENUMX
2137   BFD_RELOC_PPC_BA16
2138 ENUMX
2139   BFD_RELOC_PPC_BA16_BRTAKEN
2140 ENUMX
2141   BFD_RELOC_PPC_BA16_BRNTAKEN
2142 ENUMX
2143   BFD_RELOC_PPC_COPY
2144 ENUMX
2145   BFD_RELOC_PPC_GLOB_DAT
2146 ENUMX
2147   BFD_RELOC_PPC_JMP_SLOT
2148 ENUMX
2149   BFD_RELOC_PPC_RELATIVE
2150 ENUMX
2151   BFD_RELOC_PPC_LOCAL24PC
2152 ENUMX
2153   BFD_RELOC_PPC_EMB_NADDR32
2154 ENUMX
2155   BFD_RELOC_PPC_EMB_NADDR16
2156 ENUMX
2157   BFD_RELOC_PPC_EMB_NADDR16_LO
2158 ENUMX
2159   BFD_RELOC_PPC_EMB_NADDR16_HI
2160 ENUMX
2161   BFD_RELOC_PPC_EMB_NADDR16_HA
2162 ENUMX
2163   BFD_RELOC_PPC_EMB_SDAI16
2164 ENUMX
2165   BFD_RELOC_PPC_EMB_SDA2I16
2166 ENUMX
2167   BFD_RELOC_PPC_EMB_SDA2REL
2168 ENUMX
2169   BFD_RELOC_PPC_EMB_SDA21
2170 ENUMX
2171   BFD_RELOC_PPC_EMB_MRKREF
2172 ENUMX
2173   BFD_RELOC_PPC_EMB_RELSEC16
2174 ENUMX
2175   BFD_RELOC_PPC_EMB_RELST_LO
2176 ENUMX
2177   BFD_RELOC_PPC_EMB_RELST_HI
2178 ENUMX
2179   BFD_RELOC_PPC_EMB_RELST_HA
2180 ENUMX
2181   BFD_RELOC_PPC_EMB_BIT_FLD
2182 ENUMX
2183   BFD_RELOC_PPC_EMB_RELSDA
2184 ENUMDOC
2185   Power(rs6000) and PowerPC relocations.
2186
2187 ENUM
2188   BFD_RELOC_I370_D12
2189 ENUMDOC
2190   IBM 370/390 relocations
2191
2192 ENUM
2193   BFD_RELOC_CTOR
2194 ENUMDOC
2195   The type of reloc used to build a contructor table - at the moment
2196   probably a 32 bit wide absolute relocation, but the target can choose.
2197   It generally does map to one of the other relocation types.
2198
2199 ENUM
2200   BFD_RELOC_ARM_PCREL_BRANCH
2201 ENUMDOC
2202   ARM 26 bit pc-relative branch.  The lowest two bits must be zero and are
2203   not stored in the instruction.
2204 ENUM
2205   BFD_RELOC_ARM_IMMEDIATE
2206 ENUMX
2207   BFD_RELOC_ARM_ADRL_IMMEDIATE
2208 ENUMX
2209   BFD_RELOC_ARM_OFFSET_IMM
2210 ENUMX
2211   BFD_RELOC_ARM_SHIFT_IMM
2212 ENUMX
2213   BFD_RELOC_ARM_SWI
2214 ENUMX
2215   BFD_RELOC_ARM_MULTI
2216 ENUMX
2217   BFD_RELOC_ARM_CP_OFF_IMM
2218 ENUMX
2219   BFD_RELOC_ARM_ADR_IMM
2220 ENUMX
2221   BFD_RELOC_ARM_LDR_IMM
2222 ENUMX
2223   BFD_RELOC_ARM_LITERAL
2224 ENUMX
2225   BFD_RELOC_ARM_IN_POOL
2226 ENUMX
2227   BFD_RELOC_ARM_OFFSET_IMM8
2228 ENUMX
2229   BFD_RELOC_ARM_HWLITERAL
2230 ENUMX
2231   BFD_RELOC_ARM_THUMB_ADD
2232 ENUMX
2233   BFD_RELOC_ARM_THUMB_IMM
2234 ENUMX
2235   BFD_RELOC_ARM_THUMB_SHIFT
2236 ENUMX
2237   BFD_RELOC_ARM_THUMB_OFFSET
2238 ENUMX
2239   BFD_RELOC_ARM_GOT12
2240 ENUMX
2241   BFD_RELOC_ARM_GOT32
2242 ENUMX
2243   BFD_RELOC_ARM_JUMP_SLOT
2244 ENUMX
2245   BFD_RELOC_ARM_COPY
2246 ENUMX
2247   BFD_RELOC_ARM_GLOB_DAT
2248 ENUMX
2249   BFD_RELOC_ARM_PLT32
2250 ENUMX
2251   BFD_RELOC_ARM_RELATIVE
2252 ENUMX
2253   BFD_RELOC_ARM_GOTOFF
2254 ENUMX
2255   BFD_RELOC_ARM_GOTPC
2256 ENUMDOC
2257   These relocs are only used within the ARM assembler.  They are not
2258   (at present) written to any object files.
2259
2260 ENUM
2261   BFD_RELOC_SH_PCDISP8BY2
2262 ENUMX
2263   BFD_RELOC_SH_PCDISP12BY2
2264 ENUMX
2265   BFD_RELOC_SH_IMM4
2266 ENUMX
2267   BFD_RELOC_SH_IMM4BY2
2268 ENUMX
2269   BFD_RELOC_SH_IMM4BY4
2270 ENUMX
2271   BFD_RELOC_SH_IMM8
2272 ENUMX
2273   BFD_RELOC_SH_IMM8BY2
2274 ENUMX
2275   BFD_RELOC_SH_IMM8BY4
2276 ENUMX
2277   BFD_RELOC_SH_PCRELIMM8BY2
2278 ENUMX
2279   BFD_RELOC_SH_PCRELIMM8BY4
2280 ENUMX
2281   BFD_RELOC_SH_SWITCH16
2282 ENUMX
2283   BFD_RELOC_SH_SWITCH32
2284 ENUMX
2285   BFD_RELOC_SH_USES
2286 ENUMX
2287   BFD_RELOC_SH_COUNT
2288 ENUMX
2289   BFD_RELOC_SH_ALIGN
2290 ENUMX
2291   BFD_RELOC_SH_CODE
2292 ENUMX
2293   BFD_RELOC_SH_DATA
2294 ENUMX
2295   BFD_RELOC_SH_LABEL
2296 ENUMX
2297   BFD_RELOC_SH_LOOP_START
2298 ENUMX
2299   BFD_RELOC_SH_LOOP_END
2300 ENUMDOC
2301   Hitachi SH relocs.  Not all of these appear in object files.
2302
2303 ENUM
2304   BFD_RELOC_THUMB_PCREL_BRANCH9
2305 ENUMX
2306   BFD_RELOC_THUMB_PCREL_BRANCH12
2307 ENUMX
2308   BFD_RELOC_THUMB_PCREL_BRANCH23
2309 ENUMDOC
2310   Thumb 23-, 12- and 9-bit pc-relative branches.  The lowest bit must
2311   be zero and is not stored in the instruction.
2312
2313 ENUM
2314   BFD_RELOC_ARC_B22_PCREL
2315 ENUMDOC
2316   Argonaut RISC Core (ARC) relocs.
2317   ARC 22 bit pc-relative branch.  The lowest two bits must be zero and are
2318   not stored in the instruction.  The high 20 bits are installed in bits 26
2319   through 7 of the instruction.
2320 ENUM
2321   BFD_RELOC_ARC_B26
2322 ENUMDOC
2323   ARC 26 bit absolute branch.  The lowest two bits must be zero and are not
2324   stored in the instruction.  The high 24 bits are installed in bits 23
2325   through 0.
2326
2327 ENUM
2328   BFD_RELOC_D10V_10_PCREL_R
2329 ENUMDOC
2330   Mitsubishi D10V relocs.
2331   This is a 10-bit reloc with the right 2 bits
2332   assumed to be 0.
2333 ENUM
2334   BFD_RELOC_D10V_10_PCREL_L
2335 ENUMDOC
2336   Mitsubishi D10V relocs.
2337   This is a 10-bit reloc with the right 2 bits
2338   assumed to be 0.  This is the same as the previous reloc
2339   except it is in the left container, i.e.,
2340   shifted left 15 bits.
2341 ENUM
2342   BFD_RELOC_D10V_18
2343 ENUMDOC
2344   This is an 18-bit reloc with the right 2 bits
2345   assumed to be 0.
2346 ENUM
2347   BFD_RELOC_D10V_18_PCREL
2348 ENUMDOC
2349   This is an 18-bit reloc with the right 2 bits
2350   assumed to be 0.
2351
2352 ENUM
2353   BFD_RELOC_D30V_6
2354 ENUMDOC
2355   Mitsubishi D30V relocs.
2356   This is a 6-bit absolute reloc.
2357 ENUM
2358   BFD_RELOC_D30V_9_PCREL
2359 ENUMDOC
2360   This is a 6-bit pc-relative reloc with
2361   the right 3 bits assumed to be 0.
2362 ENUM
2363   BFD_RELOC_D30V_9_PCREL_R
2364 ENUMDOC
2365   This is a 6-bit pc-relative reloc with
2366   the right 3 bits assumed to be 0. Same
2367   as the previous reloc but on the right side
2368   of the container.
2369 ENUM
2370   BFD_RELOC_D30V_15
2371 ENUMDOC
2372   This is a 12-bit absolute reloc with the
2373   right 3 bitsassumed to be 0.
2374 ENUM
2375   BFD_RELOC_D30V_15_PCREL
2376 ENUMDOC
2377   This is a 12-bit pc-relative reloc with
2378   the right 3 bits assumed to be 0.
2379 ENUM
2380   BFD_RELOC_D30V_15_PCREL_R
2381 ENUMDOC
2382   This is a 12-bit pc-relative reloc with
2383   the right 3 bits assumed to be 0. Same
2384   as the previous reloc but on the right side
2385   of the container.
2386 ENUM
2387   BFD_RELOC_D30V_21
2388 ENUMDOC
2389   This is an 18-bit absolute reloc with
2390   the right 3 bits assumed to be 0.
2391 ENUM
2392   BFD_RELOC_D30V_21_PCREL
2393 ENUMDOC
2394   This is an 18-bit pc-relative reloc with
2395   the right 3 bits assumed to be 0.
2396 ENUM
2397   BFD_RELOC_D30V_21_PCREL_R
2398 ENUMDOC
2399   This is an 18-bit pc-relative reloc with
2400   the right 3 bits assumed to be 0. Same
2401   as the previous reloc but on the right side
2402   of the container.
2403 ENUM
2404   BFD_RELOC_D30V_32
2405 ENUMDOC
2406   This is a 32-bit absolute reloc.
2407 ENUM
2408   BFD_RELOC_D30V_32_PCREL
2409 ENUMDOC
2410   This is a 32-bit pc-relative reloc.
2411
2412 ENUM
2413   BFD_RELOC_M32R_24
2414 ENUMDOC
2415   Mitsubishi M32R relocs.
2416   This is a 24 bit absolute address.
2417 ENUM
2418   BFD_RELOC_M32R_10_PCREL
2419 ENUMDOC
2420   This is a 10-bit pc-relative reloc with the right 2 bits assumed to be 0.
2421 ENUM
2422   BFD_RELOC_M32R_18_PCREL
2423 ENUMDOC
2424   This is an 18-bit reloc with the right 2 bits assumed to be 0.
2425 ENUM
2426   BFD_RELOC_M32R_26_PCREL
2427 ENUMDOC
2428   This is a 26-bit reloc with the right 2 bits assumed to be 0.
2429 ENUM
2430   BFD_RELOC_M32R_HI16_ULO
2431 ENUMDOC
2432   This is a 16-bit reloc containing the high 16 bits of an address
2433   used when the lower 16 bits are treated as unsigned.
2434 ENUM
2435   BFD_RELOC_M32R_HI16_SLO
2436 ENUMDOC
2437   This is a 16-bit reloc containing the high 16 bits of an address
2438   used when the lower 16 bits are treated as signed.
2439 ENUM
2440   BFD_RELOC_M32R_LO16
2441 ENUMDOC
2442   This is a 16-bit reloc containing the lower 16 bits of an address.
2443 ENUM
2444   BFD_RELOC_M32R_SDA16
2445 ENUMDOC
2446   This is a 16-bit reloc containing the small data area offset for use in
2447   add3, load, and store instructions.
2448
2449 ENUM
2450   BFD_RELOC_V850_9_PCREL
2451 ENUMDOC
2452   This is a 9-bit reloc
2453 ENUM
2454   BFD_RELOC_V850_22_PCREL
2455 ENUMDOC
2456   This is a 22-bit reloc
2457
2458 ENUM
2459   BFD_RELOC_V850_SDA_16_16_OFFSET
2460 ENUMDOC
2461   This is a 16 bit offset from the short data area pointer.
2462 ENUM
2463   BFD_RELOC_V850_SDA_15_16_OFFSET
2464 ENUMDOC
2465   This is a 16 bit offset (of which only 15 bits are used) from the
2466   short data area pointer.
2467 ENUM
2468   BFD_RELOC_V850_ZDA_16_16_OFFSET
2469 ENUMDOC
2470   This is a 16 bit offset from the zero data area pointer.
2471 ENUM
2472   BFD_RELOC_V850_ZDA_15_16_OFFSET
2473 ENUMDOC
2474   This is a 16 bit offset (of which only 15 bits are used) from the
2475   zero data area pointer.
2476 ENUM
2477   BFD_RELOC_V850_TDA_6_8_OFFSET
2478 ENUMDOC
2479   This is an 8 bit offset (of which only 6 bits are used) from the
2480   tiny data area pointer.
2481 ENUM
2482   BFD_RELOC_V850_TDA_7_8_OFFSET
2483 ENUMDOC
2484   This is an 8bit offset (of which only 7 bits are used) from the tiny
2485   data area pointer.
2486 ENUM
2487   BFD_RELOC_V850_TDA_7_7_OFFSET
2488 ENUMDOC
2489   This is a 7 bit offset from the tiny data area pointer.
2490 ENUM
2491   BFD_RELOC_V850_TDA_16_16_OFFSET
2492 ENUMDOC
2493   This is a 16 bit offset from the tiny data area pointer.
2494 COMMENT
2495 ENUM
2496   BFD_RELOC_V850_TDA_4_5_OFFSET
2497 ENUMDOC
2498   This is a 5 bit offset (of which only 4 bits are used) from the tiny
2499   data area pointer.
2500 ENUM
2501   BFD_RELOC_V850_TDA_4_4_OFFSET
2502 ENUMDOC
2503   This is a 4 bit offset from the tiny data area pointer.
2504 ENUM
2505   BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET
2506 ENUMDOC
2507   This is a 16 bit offset from the short data area pointer, with the
2508   bits placed non-contigously in the instruction.
2509 ENUM
2510   BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET
2511 ENUMDOC
2512   This is a 16 bit offset from the zero data area pointer, with the
2513   bits placed non-contigously in the instruction.
2514 ENUM
2515   BFD_RELOC_V850_CALLT_6_7_OFFSET
2516 ENUMDOC
2517   This is a 6 bit offset from the call table base pointer.
2518 ENUM
2519   BFD_RELOC_V850_CALLT_16_16_OFFSET
2520 ENUMDOC
2521   This is a 16 bit offset from the call table base pointer.
2522 COMMENT
2523
2524 ENUM
2525   BFD_RELOC_MN10300_32_PCREL
2526 ENUMDOC
2527   This is a 32bit pcrel reloc for the mn10300, offset by two bytes in the
2528   instruction.
2529 ENUM
2530   BFD_RELOC_MN10300_16_PCREL
2531 ENUMDOC
2532   This is a 16bit pcrel reloc for the mn10300, offset by two bytes in the
2533   instruction.
2534
2535 ENUM
2536   BFD_RELOC_TIC30_LDP
2537 ENUMDOC
2538   This is a 8bit DP reloc for the tms320c30, where the most
2539   significant 8 bits of a 24 bit word are placed into the least
2540   significant 8 bits of the opcode.
2541
2542 COMMENT
2543 ENUM
2544   BFD_RELOC_TIC54X_PARTLS7
2545 ENUMDOC
2546   This is a 7bit reloc for the tms320c54x, where the least
2547   significant 7 bits of a 16 bit word are placed into the least
2548   significant 7 bits of the opcode.
2549
2550 ENUM
2551   BFD_RELOC_TIC54X_PARTMS9
2552 ENUMDOC
2553   This is a 9bit DP reloc for the tms320c54x, where the most
2554   significant 9 bits of a 16 bit word are placed into the least
2555   significant 9 bits of the opcode.
2556
2557 ENUM
2558   BFD_RELOC_TIC54X_23
2559 ENUMDOC
2560   This is an extended address 23-bit reloc for the tms320c54x.
2561
2562 ENUM
2563   BFD_RELOC_TIC54X_16_OF_23
2564 ENUMDOC
2565   This is a 16-bit reloc for the tms320c54x, where the least 
2566   significant 16 bits of a 23-bit extended address are placed into 
2567   the opcode.
2568
2569 ENUM
2570   BFD_RELOC_TIC54X_MS7_OF_23
2571 ENUMDOC
2572   This is a reloc for the tms320c54x, where the most
2573   significant 7 bits of a 23-bit extended address are placed into 
2574   the opcode.
2575 COMMENT
2576
2577 ENUM
2578   BFD_RELOC_FR30_48
2579 ENUMDOC
2580   This is a 48 bit reloc for the FR30 that stores 32 bits.
2581 ENUM
2582   BFD_RELOC_FR30_20
2583 ENUMDOC
2584   This is a 32 bit reloc for the FR30 that stores 20 bits split up into
2585   two sections.
2586 ENUM
2587   BFD_RELOC_FR30_6_IN_4
2588 ENUMDOC
2589   This is a 16 bit reloc for the FR30 that stores a 6 bit word offset in
2590   4 bits.
2591 ENUM
2592   BFD_RELOC_FR30_8_IN_8
2593 ENUMDOC
2594   This is a 16 bit reloc for the FR30 that stores an 8 bit byte offset
2595   into 8 bits.
2596 ENUM
2597   BFD_RELOC_FR30_9_IN_8
2598 ENUMDOC
2599   This is a 16 bit reloc for the FR30 that stores a 9 bit short offset
2600   into 8 bits.
2601 ENUM
2602   BFD_RELOC_FR30_10_IN_8
2603 ENUMDOC
2604   This is a 16 bit reloc for the FR30 that stores a 10 bit word offset
2605   into 8 bits.
2606 ENUM
2607   BFD_RELOC_FR30_9_PCREL
2608 ENUMDOC
2609   This is a 16 bit reloc for the FR30 that stores a 9 bit pc relative
2610   short offset into 8 bits.
2611 ENUM
2612   BFD_RELOC_FR30_12_PCREL
2613 ENUMDOC
2614   This is a 16 bit reloc for the FR30 that stores a 12 bit pc relative
2615   short offset into 11 bits.
2616
2617 ENUM
2618   BFD_RELOC_MCORE_PCREL_IMM8BY4
2619 ENUMX
2620   BFD_RELOC_MCORE_PCREL_IMM11BY2
2621 ENUMX
2622   BFD_RELOC_MCORE_PCREL_IMM4BY2
2623 ENUMX
2624   BFD_RELOC_MCORE_PCREL_32
2625 ENUMX
2626   BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2
2627 ENUMX
2628   BFD_RELOC_MCORE_RVA
2629 ENUMDOC
2630   Motorola Mcore relocations.
2631
2632 ENUM
2633   BFD_RELOC_AVR_7_PCREL
2634 ENUMDOC
2635   This is a 16 bit reloc for the AVR that stores 8 bit pc relative
2636   short offset into 7 bits.
2637 ENUM
2638   BFD_RELOC_AVR_13_PCREL
2639 ENUMDOC
2640   This is a 16 bit reloc for the AVR that stores 13 bit pc relative
2641   short offset into 12 bits.
2642 ENUM
2643   BFD_RELOC_AVR_16_PM
2644 ENUMDOC
2645   This is a 16 bit reloc for the AVR that stores 17 bit value (usually
2646   program memory address) into 16 bits.  
2647 ENUM
2648   BFD_RELOC_AVR_LO8_LDI
2649 ENUMDOC
2650   This is a 16 bit reloc for the AVR that stores 8 bit value (usually
2651   data memory address) into 8 bit immediate value of LDI insn.
2652 ENUM
2653   BFD_RELOC_AVR_HI8_LDI
2654 ENUMDOC
2655   This is a 16 bit reloc for the AVR that stores 8 bit value (high 8 bit
2656   of data memory address) into 8 bit immediate value of LDI insn.
2657 ENUM
2658   BFD_RELOC_AVR_HH8_LDI
2659 ENUMDOC
2660   This is a 16 bit reloc for the AVR that stores 8 bit value (most high 8 bit
2661   of program memory address) into 8 bit immediate value of LDI insn.
2662 ENUM
2663   BFD_RELOC_AVR_LO8_LDI_NEG
2664 ENUMDOC
2665   This is a 16 bit reloc for the AVR that stores negated 8 bit value
2666   (usually data memory address) into 8 bit immediate value of SUBI insn.
2667 ENUM
2668   BFD_RELOC_AVR_HI8_LDI_NEG
2669 ENUMDOC
2670   This is a 16 bit reloc for the AVR that stores negated 8 bit value
2671   (high 8 bit of data memory address) into 8 bit immediate value of
2672   SUBI insn.
2673 ENUM
2674   BFD_RELOC_AVR_HH8_LDI_NEG
2675 ENUMDOC
2676   This is a 16 bit reloc for the AVR that stores negated 8 bit value
2677   (most high 8 bit of program memory address) into 8 bit immediate value
2678   of LDI or SUBI insn.
2679 ENUM
2680   BFD_RELOC_AVR_LO8_LDI_PM
2681 ENUMDOC
2682   This is a 16 bit reloc for the AVR that stores 8 bit value (usually
2683   command address) into 8 bit immediate value of LDI insn.
2684 ENUM
2685   BFD_RELOC_AVR_HI8_LDI_PM
2686 ENUMDOC
2687   This is a 16 bit reloc for the AVR that stores 8 bit value (high 8 bit
2688   of command address) into 8 bit immediate value of LDI insn.
2689 ENUM
2690   BFD_RELOC_AVR_HH8_LDI_PM
2691 ENUMDOC
2692   This is a 16 bit reloc for the AVR that stores 8 bit value (most high 8 bit
2693   of command address) into 8 bit immediate value of LDI insn.
2694 ENUM
2695   BFD_RELOC_AVR_LO8_LDI_PM_NEG
2696 ENUMDOC
2697   This is a 16 bit reloc for the AVR that stores negated 8 bit value
2698   (usually command address) into 8 bit immediate value of SUBI insn.
2699 ENUM
2700   BFD_RELOC_AVR_HI8_LDI_PM_NEG
2701 ENUMDOC
2702   This is a 16 bit reloc for the AVR that stores negated 8 bit value
2703   (high 8 bit of 16 bit command address) into 8 bit immediate value
2704   of SUBI insn.
2705 ENUM
2706   BFD_RELOC_AVR_HH8_LDI_PM_NEG
2707 ENUMDOC
2708   This is a 16 bit reloc for the AVR that stores negated 8 bit value
2709   (high 6 bit of 22 bit command address) into 8 bit immediate
2710   value of SUBI insn.
2711 ENUM
2712   BFD_RELOC_AVR_CALL
2713 ENUMDOC
2714   This is a 32 bit reloc for the AVR that stores 23 bit value
2715   into 22 bits.
2716
2717 ENUM
2718   BFD_RELOC_VTABLE_INHERIT
2719 ENUMX
2720   BFD_RELOC_VTABLE_ENTRY
2721 ENUMDOC
2722   These two relocations are used by the linker to determine which of
2723   the entries in a C++ virtual function table are actually used.  When
2724   the --gc-sections option is given, the linker will zero out the entries
2725   that are not used, so that the code for those functions need not be
2726   included in the output.
2727
2728   VTABLE_INHERIT is a zero-space relocation used to describe to the
2729   linker the inheritence tree of a C++ virtual function table.  The
2730   relocation's symbol should be the parent class' vtable, and the
2731   relocation should be located at the child vtable.
2732
2733   VTABLE_ENTRY is a zero-space relocation that describes the use of a
2734   virtual function table entry.  The reloc's symbol should refer to the
2735   table of the class mentioned in the code.  Off of that base, an offset
2736   describes the entry that is being used.  For Rela hosts, this offset
2737   is stored in the reloc's addend.  For Rel hosts, we are forced to put
2738   this offset in the reloc's section offset.
2739
2740 ENDSENUM
2741   BFD_RELOC_UNUSED
2742 CODE_FRAGMENT
2743 .
2744 .typedef enum bfd_reloc_code_real bfd_reloc_code_real_type;
2745 */
2746
2747
2748 /*
2749 FUNCTION
2750         bfd_reloc_type_lookup
2751
2752 SYNOPSIS
2753         reloc_howto_type *
2754         bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
2755
2756 DESCRIPTION
2757         Return a pointer to a howto structure which, when
2758         invoked, will perform the relocation @var{code} on data from the
2759         architecture noted.
2760
2761 */
2762
2763
2764 reloc_howto_type *
2765 bfd_reloc_type_lookup (abfd, code)
2766      bfd *abfd;
2767      bfd_reloc_code_real_type code;
2768 {
2769   return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
2770 }
2771
2772 static reloc_howto_type bfd_howto_32 =
2773 HOWTO (0, 00, 2, 32, false, 0, complain_overflow_bitfield, 0, "VRT32", false, 0xffffffff, 0xffffffff, true);
2774
2775
2776 /*
2777 INTERNAL_FUNCTION
2778         bfd_default_reloc_type_lookup
2779
2780 SYNOPSIS
2781         reloc_howto_type *bfd_default_reloc_type_lookup
2782         (bfd *abfd, bfd_reloc_code_real_type  code);
2783
2784 DESCRIPTION
2785         Provides a default relocation lookup routine for any architecture.
2786
2787
2788 */
2789
2790 reloc_howto_type *
2791 bfd_default_reloc_type_lookup (abfd, code)
2792      bfd *abfd;
2793      bfd_reloc_code_real_type code;
2794 {
2795   switch (code)
2796     {
2797     case BFD_RELOC_CTOR:
2798       /* The type of reloc used in a ctor, which will be as wide as the
2799          address - so either a 64, 32, or 16 bitter.  */
2800       switch (bfd_get_arch_info (abfd)->bits_per_address)
2801         {
2802         case 64:
2803           BFD_FAIL ();
2804         case 32:
2805           return &bfd_howto_32;
2806         case 16:
2807           BFD_FAIL ();
2808         default:
2809           BFD_FAIL ();
2810         }
2811     default:
2812       BFD_FAIL ();
2813     }
2814   return (reloc_howto_type *) NULL;
2815 }
2816
2817 /*
2818 FUNCTION
2819         bfd_get_reloc_code_name
2820
2821 SYNOPSIS
2822         const char *bfd_get_reloc_code_name (bfd_reloc_code_real_type code);
2823
2824 DESCRIPTION
2825         Provides a printable name for the supplied relocation code.
2826         Useful mainly for printing error messages.
2827 */
2828
2829 const char *
2830 bfd_get_reloc_code_name (code)
2831      bfd_reloc_code_real_type code;
2832 {
2833   if (code > BFD_RELOC_UNUSED)
2834     return 0;
2835   return bfd_reloc_code_real_names[(int)code];
2836 }
2837
2838 /*
2839 INTERNAL_FUNCTION
2840         bfd_generic_relax_section
2841
2842 SYNOPSIS
2843         boolean bfd_generic_relax_section
2844          (bfd *abfd,
2845           asection *section,
2846           struct bfd_link_info *,
2847           boolean *);
2848
2849 DESCRIPTION
2850         Provides default handling for relaxing for back ends which
2851         don't do relaxing -- i.e., does nothing.
2852 */
2853
2854 /*ARGSUSED*/
2855 boolean
2856 bfd_generic_relax_section (abfd, section, link_info, again)
2857      bfd *abfd ATTRIBUTE_UNUSED;
2858      asection *section ATTRIBUTE_UNUSED;
2859      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
2860      boolean *again;
2861 {
2862   *again = false;
2863   return true;
2864 }
2865
2866 /*
2867 INTERNAL_FUNCTION
2868         bfd_generic_gc_sections
2869
2870 SYNOPSIS
2871         boolean bfd_generic_gc_sections
2872          (bfd *, struct bfd_link_info *);
2873
2874 DESCRIPTION
2875         Provides default handling for relaxing for back ends which
2876         don't do section gc -- i.e., does nothing.
2877 */
2878
2879 /*ARGSUSED*/
2880 boolean
2881 bfd_generic_gc_sections (abfd, link_info)
2882      bfd *abfd ATTRIBUTE_UNUSED;
2883      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
2884 {
2885   return true;
2886 }
2887
2888 /*
2889 INTERNAL_FUNCTION
2890         bfd_generic_get_relocated_section_contents
2891
2892 SYNOPSIS
2893         bfd_byte *
2894            bfd_generic_get_relocated_section_contents (bfd *abfd,
2895              struct bfd_link_info *link_info,
2896              struct bfd_link_order *link_order,
2897              bfd_byte *data,
2898              boolean relocateable,
2899              asymbol **symbols);
2900
2901 DESCRIPTION
2902         Provides default handling of relocation effort for back ends
2903         which can't be bothered to do it efficiently.
2904
2905 */
2906
2907 bfd_byte *
2908 bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data,
2909                                             relocateable, symbols)
2910      bfd *abfd;
2911      struct bfd_link_info *link_info;
2912      struct bfd_link_order *link_order;
2913      bfd_byte *data;
2914      boolean relocateable;
2915      asymbol **symbols;
2916 {
2917   /* Get enough memory to hold the stuff */
2918   bfd *input_bfd = link_order->u.indirect.section->owner;
2919   asection *input_section = link_order->u.indirect.section;
2920
2921   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
2922   arelent **reloc_vector = NULL;
2923   long reloc_count;
2924
2925   if (reloc_size < 0)
2926     goto error_return;
2927
2928   reloc_vector = (arelent **) bfd_malloc ((size_t) reloc_size);
2929   if (reloc_vector == NULL && reloc_size != 0)
2930     goto error_return;
2931
2932   /* read in the section */
2933   if (!bfd_get_section_contents (input_bfd,
2934                                  input_section,
2935                                  (PTR) data,
2936                                  0,
2937                                  input_section->_raw_size))
2938     goto error_return;
2939
2940   /* We're not relaxing the section, so just copy the size info */
2941   input_section->_cooked_size = input_section->_raw_size;
2942   input_section->reloc_done = true;
2943
2944   reloc_count = bfd_canonicalize_reloc (input_bfd,
2945                                         input_section,
2946                                         reloc_vector,
2947                                         symbols);
2948   if (reloc_count < 0)
2949     goto error_return;
2950
2951   if (reloc_count > 0)
2952     {
2953       arelent **parent;
2954       for (parent = reloc_vector; *parent != (arelent *) NULL;
2955            parent++)
2956         {
2957           char *error_message = (char *) NULL;
2958           bfd_reloc_status_type r =
2959             bfd_perform_relocation (input_bfd,
2960                                     *parent,
2961                                     (PTR) data,
2962                                     input_section,
2963                                     relocateable ? abfd : (bfd *) NULL,
2964                                     &error_message);
2965
2966           if (relocateable)
2967             {
2968               asection *os = input_section->output_section;
2969
2970               /* A partial link, so keep the relocs */
2971               os->orelocation[os->reloc_count] = *parent;
2972               os->reloc_count++;
2973             }
2974
2975           if (r != bfd_reloc_ok)
2976             {
2977               switch (r)
2978                 {
2979                 case bfd_reloc_undefined:
2980                   if (!((*link_info->callbacks->undefined_symbol)
2981                         (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
2982                          input_bfd, input_section, (*parent)->address,
2983                          true)))
2984                     goto error_return;
2985                   break;
2986                 case bfd_reloc_dangerous:
2987                   BFD_ASSERT (error_message != (char *) NULL);
2988                   if (!((*link_info->callbacks->reloc_dangerous)
2989                         (link_info, error_message, input_bfd, input_section,
2990                          (*parent)->address)))
2991                     goto error_return;
2992                   break;
2993                 case bfd_reloc_overflow:
2994                   if (!((*link_info->callbacks->reloc_overflow)
2995                         (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
2996                          (*parent)->howto->name, (*parent)->addend,
2997                          input_bfd, input_section, (*parent)->address)))
2998                     goto error_return;
2999                   break;
3000                 case bfd_reloc_outofrange:
3001                 default:
3002                   abort ();
3003                   break;
3004                 }
3005
3006             }
3007         }
3008     }
3009   if (reloc_vector != NULL)
3010     free (reloc_vector);
3011   return data;
3012
3013 error_return:
3014   if (reloc_vector != NULL)
3015     free (reloc_vector);
3016   return NULL;
3017 }