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