* libbfd-in.h: Remove alloca cruft. It was missing some necessary
[external/binutils.git] / bfd / reloc.c
1 /* BFD support for handling relocation entries.
2    Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3    Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 /*
22 SECTION
23         Relocations
24
25         BFD maintains relocations in much the same way it maintains
26         symbols: they are left alone until required, then read in
27         en-mass and translated into an internal form.  A common
28         routine <<bfd_perform_relocation>> acts upon the
29         canonical form to do the fixup.
30
31         Relocations are maintained on a per section basis,
32         while symbols are maintained on a per BFD basis.
33
34         All that a back end has to do to fit the BFD interface is to create
35         a <<struct reloc_cache_entry>> for each relocation
36         in a particular section, and fill in the right bits of the structures.
37
38 @menu
39 @* typedef arelent::
40 @* howto manager::
41 @end menu
42
43 */
44 #include "bfd.h"
45 #include "sysdep.h"
46 #include "bfdlink.h"
47 #include "libbfd.h"
48 /*
49 DOCDD
50 INODE
51         typedef arelent, howto manager, Relocations, Relocations
52
53 SUBSECTION
54         typedef arelent
55
56         This is the structure of a relocation entry:
57
58 CODE_FRAGMENT
59 .
60 .typedef enum bfd_reloc_status 
61 .{
62 .       {* No errors detected *}
63 .  bfd_reloc_ok,
64 .
65 .       {* The relocation was performed, but there was an overflow. *}
66 .  bfd_reloc_overflow,
67 .
68 .       {* The address to relocate was not within the section supplied. *}
69 .  bfd_reloc_outofrange,
70 .
71 .       {* Used by special functions *}
72 .  bfd_reloc_continue,
73 .
74 .       {* Unsupported relocation size requested. *}
75 .  bfd_reloc_notsupported,
76 .
77 .       {* Unused *}
78 .  bfd_reloc_other,
79 .
80 .       {* The symbol to relocate against was undefined. *}
81 .  bfd_reloc_undefined,
82 .
83 .       {* The relocation was performed, but may not be ok - presently
84 .          generated only when linking i960 coff files with i960 b.out
85 .          symbols.  If this type is returned, the error_message argument
86 .          to bfd_perform_relocation will be set.  *}
87 .  bfd_reloc_dangerous
88 . }
89 . bfd_reloc_status_type;
90 .
91 .
92 .typedef struct reloc_cache_entry 
93 .{
94 .       {* A pointer into the canonical table of pointers  *}
95 .  struct symbol_cache_entry **sym_ptr_ptr;
96 .
97 .       {* offset in section *}
98 .  bfd_size_type address;
99 .
100 .       {* addend for relocation value *}
101 .  bfd_vma addend;    
102 .
103 .       {* Pointer to how to perform the required relocation *}
104 .  const struct reloc_howto_struct *howto;
105 .
106 .} arelent;
107
108 */
109
110 /*
111 DESCRIPTION
112
113         Here is a description of each of the fields within an <<arelent>>:
114
115         o <<sym_ptr_ptr>>
116
117         The symbol table pointer points to a pointer to the symbol
118         associated with the relocation request.  It is
119         the pointer into the table returned by the back end's
120         <<get_symtab>> action. @xref{Symbols}. The symbol is referenced
121         through a pointer to a pointer so that tools like the linker
122         can fix up all the symbols of the same name by modifying only
123         one pointer. The relocation routine looks in the symbol and
124         uses the base of the section the symbol is attached to and the
125         value of the symbol as the initial relocation offset. If the
126         symbol pointer is zero, then the section provided is looked up.
127
128         o <<address>>
129
130         The <<address>> field gives the offset in bytes from the base of
131         the section data which owns the relocation record to the first
132         byte of relocatable information. The actual data relocated
133         will be relative to this point; for example, a relocation
134         type which modifies the bottom two bytes of a four byte word
135         would not touch the first byte pointed to in a big endian
136         world.
137         
138         o <<addend>>
139
140         The <<addend>> is a value provided by the back end to be added (!)
141         to the relocation offset. Its interpretation is dependent upon
142         the howto. For example, on the 68k the code:
143
144
145 |        char foo[];
146 |        main()
147 |                {
148 |                return foo[0x12345678];
149 |                }
150
151         Could be compiled into:
152
153 |        linkw fp,#-4
154 |        moveb @@#12345678,d0
155 |        extbl d0
156 |        unlk fp
157 |        rts
158
159
160         This could create a reloc pointing to <<foo>>, but leave the
161         offset in the data, something like:
162
163
164 |RELOCATION RECORDS FOR [.text]:
165 |offset   type      value 
166 |00000006 32        _foo
167 |
168 |00000000 4e56 fffc          ; linkw fp,#-4
169 |00000004 1039 1234 5678     ; moveb @@#12345678,d0
170 |0000000a 49c0               ; extbl d0
171 |0000000c 4e5e               ; unlk fp
172 |0000000e 4e75               ; rts
173
174
175         Using coff and an 88k, some instructions don't have enough
176         space in them to represent the full address range, and
177         pointers have to be loaded in two parts. So you'd get something like:
178
179
180 |        or.u     r13,r0,hi16(_foo+0x12345678)
181 |        ld.b     r2,r13,lo16(_foo+0x12345678)
182 |        jmp      r1
183
184
185         This should create two relocs, both pointing to <<_foo>>, and with
186         0x12340000 in their addend field. The data would consist of:
187
188
189 |RELOCATION RECORDS FOR [.text]:
190 |offset   type      value 
191 |00000002 HVRT16    _foo+0x12340000
192 |00000006 LVRT16    _foo+0x12340000
193 |
194 |00000000 5da05678           ; or.u r13,r0,0x5678
195 |00000004 1c4d5678           ; ld.b r2,r13,0x5678
196 |00000008 f400c001           ; jmp r1
197
198
199         The relocation routine digs out the value from the data, adds
200         it to the addend to get the original offset, and then adds the
201         value of <<_foo>>. Note that all 32 bits have to be kept around
202         somewhere, to cope with carry from bit 15 to bit 16.
203
204         One further example is the sparc and the a.out format. The
205         sparc has a similar problem to the 88k, in that some
206         instructions don't have room for an entire offset, but on the
207         sparc the parts are created in odd sized lumps. The designers of
208         the a.out format chose to not use the data within the section
209         for storing part of the offset; all the offset is kept within
210         the reloc. Anything in the data should be ignored. 
211
212 |        save %sp,-112,%sp
213 |        sethi %hi(_foo+0x12345678),%g2
214 |        ldsb [%g2+%lo(_foo+0x12345678)],%i0
215 |        ret
216 |        restore
217
218         Both relocs contain a pointer to <<foo>>, and the offsets
219         contain junk.
220
221
222 |RELOCATION RECORDS FOR [.text]:
223 |offset   type      value 
224 |00000004 HI22      _foo+0x12345678
225 |00000008 LO10      _foo+0x12345678
226 |
227 |00000000 9de3bf90     ; save %sp,-112,%sp
228 |00000004 05000000     ; sethi %hi(_foo+0),%g2
229 |00000008 f048a000     ; ldsb [%g2+%lo(_foo+0)],%i0
230 |0000000c 81c7e008     ; ret
231 |00000010 81e80000     ; restore
232
233
234         o <<howto>>
235
236         The <<howto>> field can be imagined as a
237         relocation instruction. It is a pointer to a structure which
238         contains information on what to do with all of the other
239         information in the reloc record and data section. A back end
240         would normally have a relocation instruction set and turn
241         relocations into pointers to the correct structure on input -
242         but it would be possible to create each howto field on demand.
243         
244 */
245
246 /*
247 SUBSUBSECTION
248         <<enum complain_overflow>>
249
250         Indicates what sort of overflow checking should be done when
251         performing a relocation.
252
253 CODE_FRAGMENT
254 .
255 .enum complain_overflow
256 .{
257 .       {* Do not complain on overflow. *}
258 .  complain_overflow_dont,
259 .
260 .       {* Complain if the bitfield overflows, whether it is considered
261 .          as signed or unsigned. *}
262 .  complain_overflow_bitfield,
263 .
264 .       {* Complain if the value overflows when considered as signed
265 .          number. *}
266 .  complain_overflow_signed,
267 .
268 .       {* Complain if the value overflows when considered as an
269 .          unsigned number. *}
270 .  complain_overflow_unsigned
271 .};
272
273 */
274
275 /*
276 SUBSUBSECTION 
277         <<reloc_howto_type>>
278
279         The <<reloc_howto_type>> is a structure which contains all the
280         information that libbfd needs to know to tie up a back end's data.
281
282 CODE_FRAGMENT
283 .struct symbol_cache_entry;             {* Forward declaration *}
284 .
285 .typedef struct reloc_howto_struct 
286 .{ 
287 .       {*  The type field has mainly a documetary use - the back end can
288 .           do what it wants with it, though normally the back end's
289 .           external idea of what a reloc number is stored
290 .           in this field. For example, a PC relative word relocation
291 .           in a coff environment has the type 023 - because that's
292 .           what the outside world calls a R_PCRWORD reloc. *}
293 .  unsigned int type;
294 .
295 .       {*  The value the final relocation is shifted right by. This drops
296 .           unwanted data from the relocation.  *}
297 .  unsigned int rightshift;
298 .
299 .       {*  The size of the item to be relocated.  This is *not* a
300 .           power-of-two measure.  To get the number of bytes operated
301 .           on by a type of relocation, use bfd_get_reloc_size.  *}
302 .  int size;
303 .
304 .       {*  The number of bits in the item to be relocated.  This is used
305 .           when doing overflow checking.  *}
306 .  unsigned int bitsize;
307 .
308 .       {*  Notes that the relocation is relative to the location in the
309 .           data section of the addend. The relocation function will
310 .           subtract from the relocation value the address of the location
311 .           being relocated. *}
312 .  boolean pc_relative;
313 .
314 .       {*  The bit position of the reloc value in the destination.
315 .           The relocated value is left shifted by this amount. *}
316 .  unsigned int bitpos;
317 .
318 .       {* What type of overflow error should be checked for when
319 .          relocating. *}
320 .  enum complain_overflow complain_on_overflow;
321 .
322 .       {* If this field is non null, then the supplied function is
323 .          called rather than the normal function. This allows really
324 .          strange relocation methods to be accomodated (e.g., i960 callj
325 .          instructions). *}
326 .  bfd_reloc_status_type (*special_function) 
327 .                                   PARAMS ((bfd *abfd,
328 .                                            arelent *reloc_entry,
329 .                                            struct symbol_cache_entry *symbol,
330 .                                            PTR data,
331 .                                            asection *input_section, 
332 .                                            bfd *output_bfd,
333 .                                            char **error_message));
334 .
335 .       {* The textual name of the relocation type. *}
336 .  char *name;
337 .
338 .       {* When performing a partial link, some formats must modify the
339 .          relocations rather than the data - this flag signals this.*}
340 .  boolean partial_inplace;
341 .
342 .       {* The src_mask selects which parts of the read in data
343 .          are to be used in the relocation sum.  E.g., if this was an 8 bit
344 .          bit of data which we read and relocated, this would be
345 .          0x000000ff. When we have relocs which have an addend, such as
346 .          sun4 extended relocs, the value in the offset part of a
347 .          relocating field is garbage so we never use it. In this case
348 .          the mask would be 0x00000000. *}
349 .  bfd_vma src_mask;
350 .
351 .       {* The dst_mask selects which parts of the instruction are replaced
352 .          into the instruction. In most cases src_mask == dst_mask,
353 .          except in the above special case, where dst_mask would be
354 .          0x000000ff, and src_mask would be 0x00000000.   *}
355 .  bfd_vma dst_mask;           
356 .
357 .       {* When some formats create PC relative instructions, they leave
358 .          the value of the pc of the place being relocated in the offset
359 .          slot of the instruction, so that a PC relative relocation can
360 .          be made just by adding in an ordinary offset (e.g., sun3 a.out).
361 .          Some formats leave the displacement part of an instruction
362 .          empty (e.g., m88k bcs); this flag signals the fact.*}
363 .  boolean pcrel_offset;
364 .
365 .} reloc_howto_type;
366
367 */
368
369 /*
370 FUNCTION
371         The HOWTO Macro
372
373 DESCRIPTION
374         The HOWTO define is horrible and will go away.
375
376
377 .#define HOWTO(C, R,S,B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
378 .  {(unsigned)C,R,S,B, P, BI, O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
379
380 DESCRIPTION
381         And will be replaced with the totally magic way. But for the
382         moment, we are compatible, so do it this way.
383
384
385 .#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,complain_overflow_dont,FUNCTION, NAME,false,0,0,IN)
386 .
387 DESCRIPTION
388         Helper routine to turn a symbol into a relocation value.
389
390 .#define HOWTO_PREPARE(relocation, symbol)      \
391 .  {                                            \
392 .  if (symbol != (asymbol *)NULL) {             \
393 .    if (bfd_is_com_section (symbol->section)) { \
394 .      relocation = 0;                          \
395 .    }                                          \
396 .    else {                                     \
397 .      relocation = symbol->value;              \
398 .    }                                          \
399 .  }                                            \
400 .}                      
401
402 */
403
404 /*
405 FUNCTION
406         bfd_get_reloc_size
407
408 SYNOPSIS
409         int bfd_get_reloc_size (const reloc_howto_type *);
410
411 DESCRIPTION
412         For a reloc_howto_type that operates on a fixed number of bytes,
413         this returns the number of bytes operated on.
414  */
415
416 int
417 bfd_get_reloc_size (howto)
418      const reloc_howto_type *howto;
419 {
420   switch (howto->size) {
421   case 0: return 1;
422   case 1: return 2;
423   case 2: return 4;
424   case 3: return 0;
425   case 4: return 8;
426   case -2: return 4;
427   default: abort ();
428   }
429 }
430
431 /*
432 TYPEDEF
433         arelent_chain
434
435 DESCRIPTION
436
437         How relocs are tied together in an <<asection>>:
438
439 .typedef unsigned char bfd_byte;
440 .
441 .typedef struct relent_chain {
442 .  arelent relent;
443 .  struct   relent_chain *next;
444 .} arelent_chain;
445
446 */
447
448
449
450 /*
451 FUNCTION 
452         bfd_perform_relocation
453
454 SYNOPSIS
455         bfd_reloc_status_type
456                 bfd_perform_relocation
457                         (bfd *abfd,
458                          arelent *reloc_entry,
459                          PTR data,
460                          asection *input_section,
461                          bfd *output_bfd,
462                          char **error_message);
463
464 DESCRIPTION
465         If @var{output_bfd} is supplied to this function, the
466         generated image will be relocatable; the relocations are
467         copied to the output file after they have been changed to
468         reflect the new state of the world. There are two ways of
469         reflecting the results of partial linkage in an output file:
470         by modifying the output data in place, and by modifying the
471         relocation record.  Some native formats (e.g., basic a.out and
472         basic coff) have no way of specifying an addend in the
473         relocation type, so the addend has to go in the output data.
474         This is no big deal since in these formats the output data
475         slot will always be big enough for the addend. Complex reloc
476         types with addends were invented to solve just this problem.
477         The @var{error_message} argument is set to an error message if
478         this return @code{bfd_reloc_dangerous}.
479
480 */
481
482
483 bfd_reloc_status_type
484 bfd_perform_relocation (abfd, reloc_entry, data, input_section, output_bfd,
485                         error_message)
486      bfd *abfd;
487      arelent *reloc_entry;
488      PTR data;
489      asection *input_section;
490      bfd *output_bfd;
491      char **error_message;
492 {
493   bfd_vma relocation;
494   bfd_reloc_status_type flag = bfd_reloc_ok;
495   bfd_size_type addr = reloc_entry->address ;
496   bfd_vma output_base = 0;
497   const reloc_howto_type *howto = reloc_entry->howto;
498   asection *reloc_target_output_section;
499   asymbol *symbol;
500
501   symbol = *(reloc_entry->sym_ptr_ptr);
502   if ((symbol->section == &bfd_abs_section) 
503       && output_bfd != (bfd *)NULL) 
504     {
505       reloc_entry->address += input_section->output_offset;
506       return bfd_reloc_ok;
507     }
508
509   /* If we are not producing relocateable output, return an error if
510      the symbol is not defined.  An undefined weak symbol is
511      considered to have a value of zero (SVR4 ABI, p. 4-27).  */
512   if (symbol->section == &bfd_und_section
513       && (symbol->flags & BSF_WEAK) == 0
514       && output_bfd == (bfd *) NULL)
515     flag = bfd_reloc_undefined;
516
517   /* If there is a function supplied to handle this relocation type,
518      call it.  It'll return `bfd_reloc_continue' if further processing
519      can be done.  */
520   if (howto->special_function)
521     {
522       bfd_reloc_status_type cont;
523       cont = howto->special_function (abfd, reloc_entry, symbol, data,
524                                       input_section, output_bfd,
525                                       error_message);
526       if (cont != bfd_reloc_continue)
527         return cont;
528     }
529
530   /* Is the address of the relocation really within the section?  */
531   if (reloc_entry->address > input_section->_cooked_size)
532     return bfd_reloc_outofrange;
533
534   /* Work out which section the relocation is targetted at and the
535      initial relocation command value.  */
536
537   /* Get symbol value.  (Common symbols are special.)  */
538   if (bfd_is_com_section (symbol->section))
539     relocation = 0;
540   else
541     relocation = symbol->value;
542
543
544   reloc_target_output_section = symbol->section->output_section;
545
546   /* Convert input-section-relative symbol value to absolute.  */
547   if (output_bfd && howto->partial_inplace==false)
548     output_base = 0;
549   else
550     output_base = reloc_target_output_section->vma;
551
552   relocation += output_base + symbol->section->output_offset;
553
554   /* Add in supplied addend.  */
555   relocation += reloc_entry->addend;
556
557   /* Here the variable relocation holds the final address of the
558      symbol we are relocating against, plus any addend.  */
559
560   if (howto->pc_relative == true)
561     {
562       /* This is a PC relative relocation.  We want to set RELOCATION
563          to the distance between the address of the symbol and the
564          location.  RELOCATION is already the address of the symbol.
565
566          We start by subtracting the address of the section containing
567          the location.
568
569          If pcrel_offset is set, we must further subtract the position
570          of the location within the section.  Some targets arrange for
571          the addend to be the negative of the position of the location
572          within the section; for example, i386-aout does this.  For
573          i386-aout, pcrel_offset is false.  Some other targets do not
574          include the position of the location; for example, m88kbcs,
575          or ELF.  For those targets, pcrel_offset is true.
576
577          If we are producing relocateable output, then we must ensure
578          that this reloc will be correctly computed when the final
579          relocation is done.  If pcrel_offset is false we want to wind
580          up with the negative of the location within the section,
581          which means we must adjust the existing addend by the change
582          in the location within the section.  If pcrel_offset is true
583          we do not want to adjust the existing addend at all.
584
585          FIXME: This seems logical to me, but for the case of
586          producing relocateable output it is not what the code
587          actually does.  I don't want to change it, because it seems
588          far too likely that something will break.  */
589
590       relocation -= 
591         input_section->output_section->vma + input_section->output_offset;
592
593       if (howto->pcrel_offset == true)
594         relocation -= reloc_entry->address;
595     }
596
597   if (output_bfd!= (bfd *)NULL) 
598     {
599       if ( howto->partial_inplace == false)  
600         {
601           /* This is a partial relocation, and we want to apply the relocation
602              to the reloc entry rather than the raw data. Modify the reloc
603              inplace to reflect what we now know.  */
604           reloc_entry->addend = relocation;
605           reloc_entry->address +=  input_section->output_offset;
606           return flag;
607         }
608       else
609         {
610           /* This is a partial relocation, but inplace, so modify the
611              reloc record a bit. 
612
613              If we've relocated with a symbol with a section, change
614              into a ref to the section belonging to the symbol.  */
615
616           reloc_entry->address += input_section->output_offset;
617
618           /* WTF?? */
619           if (abfd->xvec->flavour == bfd_target_coff_flavour
620               && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0)
621             {
622 #if 1
623               /* For m68k-coff, the addend was being subtracted twice during
624                  relocation with -r.  Removing the line below this comment
625                  fixes that problem; see PR 2953.
626
627 However, Ian wrote the following, regarding removing the line below,
628 which explains why it is still enabled:  --djm
629
630 If you put a patch like that into BFD you need to check all the COFF
631 linkers.  I am fairly certain that patch will break coff-i386 (e.g.,
632 SCO); see coff_i386_reloc in coff-i386.c where I worked around the
633 problem in a different way.  There may very well be a reason that the
634 code works as it does.
635
636 Hmmm.  The first obvious point is that bfd_perform_relocation should
637 not have any tests that depend upon the flavour.  It's seem like
638 entirely the wrong place for such a thing.  The second obvious point
639 is that the current code ignores the reloc addend when producing
640 relocateable output for COFF.  That's peculiar.  In fact, I really
641 have no idea what the point of the line you want to remove is.
642
643 A typical COFF reloc subtracts the old value of the symbol and adds in
644 the new value to the location in the object file (if it's a pc
645 relative reloc it adds the difference between the symbol value and the
646 location).  When relocating we need to preserve that property.
647
648 BFD handles this by setting the addend to the negative of the old
649 value of the symbol.  Unfortunately it handles common symbols in a
650 non-standard way (it doesn't subtract the old value) but that's a
651 different story (we can't change it without losing backward
652 compatibility with old object files) (coff-i386 does subtract the old
653 value, to be compatible with existing coff-i386 targets, like SCO).
654
655 So everything works fine when not producing relocateable output.  When
656 we are producing relocateable output, logically we should do exactly
657 what we do when not producing relocateable output.  Therefore, your
658 patch is correct.  In fact, it should probably always just set
659 reloc_entry->addend to 0 for all cases, since it is, in fact, going to
660 add the value into the object file.  This won't hurt the COFF code,
661 which doesn't use the addend; I'm not sure what it will do to other
662 formats (the thing to check for would be whether any formats both use
663 the addend and set partial_inplace).
664
665 When I wanted to make coff-i386 produce relocateable output, I ran
666 into the problem that you are running into: I wanted to remove that
667 line.  Rather than risk it, I made the coff-i386 relocs use a special
668 function; it's coff_i386_reloc in coff-i386.c.  The function
669 specifically adds the addend field into the object file, knowing that
670 bfd_perform_relocation is not going to.  If you remove that line, then
671 coff-i386.c will wind up adding the addend field in twice.  It's
672 trivial to fix; it just needs to be done.
673
674 The problem with removing the line is just that it may break some
675 working code.  With BFD it's hard to be sure of anything.  The right
676 way to deal with this is simply to build and test at least all the
677 supported COFF targets.  It should be straightforward if time and disk
678 space consuming.  For each target:
679     1) build the linker
680     2) generate some executable, and link it using -r (I would
681        probably use paranoia.o and link against newlib/libc.a, which
682        for all the supported targets would be available in
683        /usr/cygnus/progressive/H-host/target/lib/libc.a).
684     3) make the change to reloc.c
685     4) rebuild the linker
686     5) repeat step 2
687     6) if the resulting object files are the same, you have at least
688        made it no worse
689     7) if they are different you have to figure out which version is
690        right
691 */
692               relocation -= reloc_entry->addend;
693 #endif
694               reloc_entry->addend = 0;
695             }
696           else
697             {
698               reloc_entry->addend = relocation;
699             }
700         }
701     }
702   else 
703     {
704       reloc_entry->addend = 0;
705     }
706
707   /* FIXME: This overflow checking is incomplete, because the value
708      might have overflowed before we get here.  For a correct check we
709      need to compute the value in a size larger than bitsize, but we
710      can't reasonably do that for a reloc the same size as a host
711      machine word.
712      FIXME: We should also do overflow checking on the result after
713      adding in the value contained in the object file.  */
714   if (howto->complain_on_overflow != complain_overflow_dont)
715     {
716       bfd_vma check;
717
718       /* Get the value that will be used for the relocation, but
719          starting at bit position zero.  */
720       if (howto->rightshift > howto->bitpos)
721         check = relocation >> (howto->rightshift - howto->bitpos);
722       else
723         check = relocation << (howto->bitpos - howto->rightshift);
724       switch (howto->complain_on_overflow)
725         {
726         case complain_overflow_signed:
727           {
728             /* Assumes two's complement.  */
729             bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
730             bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
731
732             /* The above right shift is incorrect for a signed value.
733                Fix it up by forcing on the upper bits.  */
734             if (howto->rightshift > howto->bitpos
735                 && (bfd_signed_vma) relocation < 0)
736               check |= ((bfd_vma) -1
737                         &~ ((bfd_vma) -1
738                             >> (howto->rightshift - howto->bitpos)));
739             if ((bfd_signed_vma) check > reloc_signed_max
740                 || (bfd_signed_vma) check < reloc_signed_min)
741               flag = bfd_reloc_overflow;
742           }
743           break;
744         case complain_overflow_unsigned:
745           {
746             /* Assumes two's complement.  This expression avoids
747                overflow if howto->bitsize is the number of bits in
748                bfd_vma.  */
749             bfd_vma reloc_unsigned_max =
750               (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
751
752             if ((bfd_vma) check > reloc_unsigned_max)
753               flag = bfd_reloc_overflow;
754           }
755           break;
756         case complain_overflow_bitfield:
757           {
758             /* Assumes two's complement.  This expression avoids
759                overflow if howto->bitsize is the number of bits in
760                bfd_vma.  */
761             bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
762
763             if (((bfd_vma) check &~ reloc_bits) != 0
764                 && ((bfd_vma) check &~ reloc_bits) != (-1 &~ reloc_bits))
765               {
766                 /* The above right shift is incorrect for a signed
767                    value.  See if turning on the upper bits fixes the
768                    overflow.  */
769                 if (howto->rightshift > howto->bitpos
770                     && (bfd_signed_vma) relocation < 0)
771                   {
772                     check |= ((bfd_vma) -1
773                               &~ ((bfd_vma) -1
774                                   >> (howto->rightshift - howto->bitpos)));
775                     if (((bfd_vma) check &~ reloc_bits) != (-1 &~ reloc_bits))
776                       flag = bfd_reloc_overflow;
777                   }
778                 else
779                   flag = bfd_reloc_overflow;
780               }
781           }
782           break;
783         default:
784           abort ();
785         }
786     }
787   
788   /* 
789     Either we are relocating all the way, or we don't want to apply
790     the relocation to the reloc entry (probably because there isn't
791     any room in the output format to describe addends to relocs)
792     */
793
794   /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
795      (OSF version 1.3, compiler version 3.11).  It miscompiles the
796      following program:
797
798      struct str
799      {
800        unsigned int i0;
801      } s = { 0 };
802
803      int
804      main ()
805      {
806        unsigned long x;
807
808        x = 0x100000000;
809        x <<= (unsigned long) s.i0;
810        if (x == 0)
811          printf ("failed\n");
812        else
813          printf ("succeeded (%lx)\n", x);
814      }
815      */
816
817   relocation >>= (bfd_vma) howto->rightshift;
818
819   /* Shift everything up to where it's going to be used */
820    
821   relocation <<= (bfd_vma) howto->bitpos;
822
823   /* Wait for the day when all have the mask in them */
824
825   /* What we do:
826      i instruction to be left alone
827      o offset within instruction
828      r relocation offset to apply
829      S src mask
830      D dst mask
831      N ~dst mask
832      A part 1
833      B part 2
834      R result
835      
836      Do this:
837      i i i i i o o o o o        from bfd_get<size>
838      and           S S S S S    to get the size offset we want
839      +   r r r r r r r r r r  to get the final value to place
840      and           D D D D D  to chop to right size
841      -----------------------
842      A A A A A 
843      And this:
844      ...   i i i i i o o o o o  from bfd_get<size>
845      and   N N N N N            get instruction
846      -----------------------
847      ...   B B B B B
848      
849      And then:       
850      B B B B B       
851      or              A A A A A     
852      -----------------------
853      R R R R R R R R R R        put into bfd_put<size>
854      */
855
856 #define DOIT(x) \
857   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))
858
859    switch (howto->size)
860      {
861      case 0:
862        {
863          char x = bfd_get_8(abfd, (char *)data + addr);
864          DOIT(x);
865          bfd_put_8(abfd,x, (unsigned char *) data + addr);
866        }
867        break;
868
869      case 1:
870        if (relocation)
871          {
872            short x = bfd_get_16(abfd, (bfd_byte *)data + addr);
873            DOIT(x);
874            bfd_put_16(abfd, x,   (unsigned char *)data + addr);
875          }
876        break;
877      case 2:
878        if (relocation)
879          {
880            long  x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
881            DOIT (x);
882            bfd_put_32 (abfd, x, (bfd_byte *)data + addr);
883          }
884        break;
885      case -2:
886        {
887          long  x = bfd_get_32(abfd, (bfd_byte *) data + addr);
888          relocation = -relocation;
889          DOIT(x);
890          bfd_put_32(abfd,x,    (bfd_byte *)data + addr);
891        }
892        break;
893
894      case 3:
895        /* Do nothing */
896        break;
897
898      case 4:
899 #ifdef BFD64
900        if (relocation)
901          {
902            bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + addr);
903            DOIT (x);
904            bfd_put_64 (abfd, x, (bfd_byte *) data + addr);
905          }
906 #else
907        abort ();
908 #endif
909        break;
910      default:
911        return bfd_reloc_other;
912      }
913
914   return flag;
915 }
916
917 /* This relocation routine is used by some of the backend linkers.
918    They do not construct asymbol or arelent structures, so there is no
919    reason for them to use bfd_perform_relocation.  Also,
920    bfd_perform_relocation is so hacked up it is easier to write a new
921    function than to try to deal with it.
922
923    This routine does a final relocation.  It should not be used when
924    generating relocateable output.
925
926    FIXME: This routine ignores any special_function in the HOWTO,
927    since the existing special_function values have been written for
928    bfd_perform_relocation.
929
930    HOWTO is the reloc howto information.
931    INPUT_BFD is the BFD which the reloc applies to.
932    INPUT_SECTION is the section which the reloc applies to.
933    CONTENTS is the contents of the section.
934    ADDRESS is the address of the reloc within INPUT_SECTION.
935    VALUE is the value of the symbol the reloc refers to.
936    ADDEND is the addend of the reloc.  */
937
938 bfd_reloc_status_type
939 _bfd_final_link_relocate (howto, input_bfd, input_section, contents, address,
940                          value, addend)
941      const reloc_howto_type *howto;
942      bfd *input_bfd;
943      asection *input_section;
944      bfd_byte *contents;
945      bfd_vma address;
946      bfd_vma value;
947      bfd_vma addend;
948 {
949   bfd_vma relocation;
950
951   /* Sanity check the address.  */
952   if (address > input_section->_cooked_size)
953     return bfd_reloc_outofrange;
954
955   /* This function assumes that we are dealing with a basic relocation
956      against a symbol.  We want to compute the value of the symbol to
957      relocate to.  This is just VALUE, the value of the symbol, plus
958      ADDEND, any addend associated with the reloc.  */
959   relocation = value + addend;
960
961   /* If the relocation is PC relative, we want to set RELOCATION to
962      the distance between the symbol (currently in RELOCATION) and the
963      location we are relocating.  Some targets (e.g., i386-aout)
964      arrange for the contents of the section to be the negative of the
965      offset of the location within the section; for such targets
966      pcrel_offset is false.  Other targets (e.g., m88kbcs or ELF)
967      simply leave the contents of the section as zero; for such
968      targets pcrel_offset is true.  If pcrel_offset is false we do not
969      need to subtract out the offset of the location within the
970      section (which is just ADDRESS).  */
971   if (howto->pc_relative)
972     {
973       relocation -= (input_section->output_section->vma
974                      + input_section->output_offset);
975       if (howto->pcrel_offset)
976         relocation -= address;
977     }
978   
979   return _bfd_relocate_contents (howto, input_bfd, relocation,
980                                  contents + address);
981 }
982
983 /* Relocate a given location using a given value and howto.  */
984
985 bfd_reloc_status_type
986 _bfd_relocate_contents (howto, input_bfd, relocation, location)
987      const reloc_howto_type *howto;
988      bfd *input_bfd;
989      bfd_vma relocation;
990      bfd_byte *location;
991 {
992   int size;
993   bfd_vma x;
994   boolean overflow;
995
996   /* If the size is negative, negate RELOCATION.  This isn't very
997      general.  */
998   if (howto->size < 0)
999     relocation = - relocation;
1000
1001   /* Get the value we are going to relocate.  */
1002   size = bfd_get_reloc_size (howto);
1003   switch (size)
1004     {
1005     default:
1006     case 0:
1007       abort ();
1008     case 1:
1009       x = bfd_get_8 (input_bfd, location);
1010       break;
1011     case 2:
1012       x = bfd_get_16 (input_bfd, location);
1013       break;
1014     case 4:
1015       x = bfd_get_32 (input_bfd, location);
1016       break;
1017     case 8:
1018 #ifdef BFD64
1019       x = bfd_get_64 (input_bfd, location);
1020 #else
1021       abort ();
1022 #endif
1023       break;
1024     }
1025
1026   /* Check for overflow.  FIXME: We may drop bits during the addition
1027      which we don't check for.  We must either check at every single
1028      operation, which would be tedious, or we must do the computations
1029      in a type larger than bfd_vma, which would be inefficient.  */
1030   overflow = false;
1031   if (howto->complain_on_overflow != complain_overflow_dont)
1032     {
1033       bfd_vma check;
1034       bfd_signed_vma signed_check;
1035       bfd_vma add;
1036       bfd_signed_vma signed_add;
1037
1038       if (howto->rightshift == 0)
1039         {
1040           check = relocation;
1041           signed_check = (bfd_signed_vma) relocation;
1042         }
1043       else
1044         {
1045           /* Drop unwanted bits from the value we are relocating to.  */
1046           check = relocation >> howto->rightshift;
1047
1048           /* If this is a signed value, the rightshift just dropped
1049              leading 1 bits (assuming twos complement).  */
1050           if ((bfd_signed_vma) relocation >= 0)
1051             signed_check = check;
1052           else
1053             signed_check = (check
1054                             | ((bfd_vma) -1
1055                                &~ ((bfd_vma) -1 >> howto->rightshift)));
1056         }
1057
1058       /* Get the value from the object file.  */
1059       add = x & howto->src_mask;
1060
1061       /* Get the value from the object file with an appropriate sign.
1062          The expression involving howto->src_mask isolates the upper
1063          bit of src_mask.  If that bit is set in the value we are
1064          adding, it is negative, and we subtract out that number times
1065          two.  If src_mask includes the highest possible bit, then we
1066          can not get the upper bit, but that does not matter since
1067          signed_add needs no adjustment to become negative in that
1068          case.  */
1069       signed_add = add;
1070       if ((add & (((~ howto->src_mask) >> 1) & howto->src_mask)) != 0)
1071         signed_add -= (((~ howto->src_mask) >> 1) & howto->src_mask) << 1;
1072
1073       /* Add the value from the object file, shifted so that it is a
1074          straight number.  */
1075       if (howto->bitpos == 0)
1076         {
1077           check += add;
1078           signed_check += signed_add;
1079         }
1080       else
1081         {
1082           check += add >> howto->bitpos;
1083
1084           /* For the signed case we use ADD, rather than SIGNED_ADD,
1085              to avoid warnings from SVR4 cc.  This is OK since we
1086              explictly handle the sign bits.  */
1087           if (signed_add >= 0)
1088             signed_check += add >> howto->bitpos;
1089           else
1090             signed_check += ((add >> howto->bitpos)
1091                              | ((bfd_vma) -1
1092                                 &~ ((bfd_vma) -1 >> howto->bitpos)));
1093         }
1094
1095       switch (howto->complain_on_overflow)
1096         {
1097         case complain_overflow_signed:
1098           {
1099             /* Assumes two's complement.  */
1100             bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
1101             bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
1102
1103             if (signed_check > reloc_signed_max
1104                 || signed_check < reloc_signed_min)
1105               overflow = true;
1106           }
1107           break;
1108         case complain_overflow_unsigned:
1109           {
1110             /* Assumes two's complement.  This expression avoids
1111                overflow if howto->bitsize is the number of bits in
1112                bfd_vma.  */
1113             bfd_vma reloc_unsigned_max =
1114               (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1115
1116             if (check > reloc_unsigned_max)
1117               overflow = true;
1118           }
1119           break;
1120         case complain_overflow_bitfield:
1121           {
1122             /* Assumes two's complement.  This expression avoids
1123                overflow if howto->bitsize is the number of bits in
1124                bfd_vma.  */
1125             bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1126
1127             if ((check &~ reloc_bits) != 0
1128                 && (((bfd_vma) signed_check &~ reloc_bits)
1129                     != (-1 &~ reloc_bits)))
1130               overflow = true;
1131           }
1132           break;
1133         default:
1134           abort ();
1135         }
1136     }
1137
1138   /* Put RELOCATION in the right bits.  */
1139   relocation >>= (bfd_vma) howto->rightshift;
1140   relocation <<= (bfd_vma) howto->bitpos;
1141
1142   /* Add RELOCATION to the right bits of X.  */
1143   x = ((x &~ howto->dst_mask)
1144        | (((x & howto->src_mask) + relocation) & howto->dst_mask));
1145
1146   /* Put the relocated value back in the object file.  */
1147   switch (size)
1148     {
1149     default:
1150     case 0:
1151       abort ();
1152     case 1:
1153       bfd_put_8 (input_bfd, x, location);
1154       break;
1155     case 2:
1156       bfd_put_16 (input_bfd, x, location);
1157       break;
1158     case 4:
1159       bfd_put_32 (input_bfd, x, location);
1160       break;
1161     case 8:
1162 #ifdef BFD64
1163       bfd_put_64 (input_bfd, x, location);
1164 #else
1165       abort ();
1166 #endif
1167       break;
1168     }
1169
1170   return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
1171 }
1172
1173 /*
1174 DOCDD
1175 INODE
1176         howto manager,  , typedef arelent, Relocations
1177
1178 SECTION
1179         The howto manager 
1180
1181         When an application wants to create a relocation, but doesn't
1182         know what the target machine might call it, it can find out by
1183         using this bit of code.
1184
1185 */
1186
1187 /*
1188 TYPEDEF
1189         bfd_reloc_code_type
1190
1191 DESCRIPTION
1192         The insides of a reloc code.  The idea is that, eventually, there
1193         will be one enumerator for every type of relocation we ever do.
1194         Pass one of these values to <<bfd_reloc_type_lookup>>, and it'll
1195         return a howto pointer.
1196
1197         This does mean that the application must determine the correct
1198         enumerator value; you can't get a howto pointer from a random set
1199         of attributes.
1200
1201 CODE_FRAGMENT
1202 .
1203 .typedef enum bfd_reloc_code_real 
1204 .{
1205 .  {* Basic absolute relocations *}
1206 .  BFD_RELOC_64,
1207 .  BFD_RELOC_32,
1208 .  BFD_RELOC_16,        
1209 .  BFD_RELOC_14,
1210 .  BFD_RELOC_8,
1211 .
1212 .  {* PC-relative relocations *}
1213 .  BFD_RELOC_64_PCREL,
1214 .  BFD_RELOC_32_PCREL,
1215 .  BFD_RELOC_24_PCREL,    {* used by i960 *}
1216 .  BFD_RELOC_16_PCREL,
1217 .  BFD_RELOC_8_PCREL,
1218 .
1219 .  {* Linkage-table relative *}
1220 .  BFD_RELOC_32_BASEREL,
1221 .  BFD_RELOC_16_BASEREL,
1222 .  BFD_RELOC_8_BASEREL,
1223 .
1224 .  {* The type of reloc used to build a contructor table - at the moment
1225 .     probably a 32 bit wide abs address, but the cpu can choose. *}
1226 .  BFD_RELOC_CTOR,
1227 .
1228 .  {* 8 bits wide, but used to form an address like 0xffnn *}
1229 .  BFD_RELOC_8_FFnn,
1230 .
1231 .  {* 32-bit pc-relative, shifted right 2 bits (i.e., 30-bit
1232 .     word displacement, e.g. for SPARC) *}
1233 .  BFD_RELOC_32_PCREL_S2,
1234 .  {* signed 16-bit pc-relative, shifted right 2 bits (e.g. for MIPS) *}
1235 .  BFD_RELOC_16_PCREL_S2,
1236 .  {* this is used on the Alpha *}
1237 .  BFD_RELOC_23_PCREL_S2,
1238 .
1239 .  {* High 22 bits of 32-bit value, placed into lower 22 bits of
1240 .     target word; simple reloc.  *}
1241 .  BFD_RELOC_HI22,
1242 .  {* Low 10 bits.  *}
1243 .  BFD_RELOC_LO10,
1244 .
1245 .  {* For systems that allocate a Global Pointer register, these are
1246 .     displacements off that register.  These relocation types are
1247 .     handled specially, because the value the register will have is
1248 .     decided relatively late.  *}
1249 .  BFD_RELOC_GPREL16,
1250 .  BFD_RELOC_GPREL32,
1251 .
1252 .  {* Reloc types used for i960/b.out.  *}
1253 .  BFD_RELOC_I960_CALLJ,
1254 .
1255 .  {* now for the sparc/elf codes *}
1256 .  BFD_RELOC_NONE,              {* actually used *}
1257 .  BFD_RELOC_SPARC_WDISP22,
1258 .  BFD_RELOC_SPARC22,
1259 .  BFD_RELOC_SPARC13,
1260 .  BFD_RELOC_SPARC_GOT10,
1261 .  BFD_RELOC_SPARC_GOT13,
1262 .  BFD_RELOC_SPARC_GOT22,
1263 .  BFD_RELOC_SPARC_PC10,
1264 .  BFD_RELOC_SPARC_PC22,
1265 .  BFD_RELOC_SPARC_WPLT30,
1266 .  BFD_RELOC_SPARC_COPY,
1267 .  BFD_RELOC_SPARC_GLOB_DAT,
1268 .  BFD_RELOC_SPARC_JMP_SLOT,
1269 .  BFD_RELOC_SPARC_RELATIVE,
1270 .  BFD_RELOC_SPARC_UA32,
1271 .
1272 .  {* these are a.out specific? *}
1273 .  BFD_RELOC_SPARC_BASE13,
1274 .  BFD_RELOC_SPARC_BASE22,
1275 .
1276 .  {* start-sanitize-v9 *}
1277 .  BFD_RELOC_SPARC_10,
1278 .  BFD_RELOC_SPARC_11,
1279 .#define  BFD_RELOC_SPARC_64 BFD_RELOC_64
1280 .  BFD_RELOC_SPARC_OLO10,
1281 .  BFD_RELOC_SPARC_HH22,
1282 .  BFD_RELOC_SPARC_HM10,
1283 .  BFD_RELOC_SPARC_LM22,
1284 .  BFD_RELOC_SPARC_PC_HH22,
1285 .  BFD_RELOC_SPARC_PC_HM10,
1286 .  BFD_RELOC_SPARC_PC_LM22,
1287 .  BFD_RELOC_SPARC_WDISP16,
1288 .  BFD_RELOC_SPARC_WDISP19,
1289 .  BFD_RELOC_SPARC_GLOB_JMP,
1290 .  BFD_RELOC_SPARC_LO7,
1291 .  {* end-sanitize-v9 *}
1292 .
1293 .  {* Alpha ECOFF relocations.  Some of these treat the symbol or "addend"
1294 .     in some special way.  *}
1295 .  {* For GPDISP_HI16 ("gpdisp") relocations, the symbol is ignored when
1296 .     writing; when reading, it will be the absolute section symbol.  The
1297 .     addend is the displacement in bytes of the "lda" instruction from
1298 .     the "ldah" instruction (which is at the address of this reloc).  *}
1299 .  BFD_RELOC_ALPHA_GPDISP_HI16,
1300 .  {* For GPDISP_LO16 ("ignore") relocations, the symbol is handled as
1301 .     with GPDISP_HI16 relocs.  The addend is ignored when writing the
1302 .     relocations out, and is filled in with the file's GP value on
1303 .     reading, for convenience.  *}
1304 .  BFD_RELOC_ALPHA_GPDISP_LO16,
1305 .
1306 .  {* The Alpha LITERAL/LITUSE relocs are produced by a symbol reference;
1307 .     the assembler turns it into a LDQ instruction to load the address of
1308 .     the symbol, and then fills in a register in the real instruction.
1309 .
1310 .     The LITERAL reloc, at the LDQ instruction, refers to the .lita
1311 .     section symbol.  The addend is ignored when writing, but is filled
1312 .     in with the file's GP value on reading, for convenience, as with the
1313 .     GPDISP_LO16 reloc.
1314 .
1315 .     The LITUSE reloc, on the instruction using the loaded address, gives
1316 .     information to the linker that it might be able to use to optimize
1317 .     away some literal section references.  The symbol is ignored (read
1318 .     as the absolute section symbol), and the "addend" indicates the type
1319 .     of instruction using the register:
1320 .              1 - "memory" fmt insn
1321 .              2 - byte-manipulation (byte offset reg)
1322 .              3 - jsr (target of branch)
1323 .
1324 .     The GNU linker currently doesn't do any of this optimizing.  *}
1325 .  BFD_RELOC_ALPHA_LITERAL,
1326 .  BFD_RELOC_ALPHA_LITUSE,
1327 .
1328 .  {* The HINT relocation indicates a value that should be filled into the
1329 .     "hint" field of a jmp/jsr/ret instruction, for possible branch-
1330 .     prediction logic which may be provided on some processors.  *}
1331 .  BFD_RELOC_ALPHA_HINT,
1332 .
1333 .  {* Bits 27..2 of the relocation address shifted right 2 bits;
1334 .     simple reloc otherwise.  *}
1335 .  BFD_RELOC_MIPS_JMP,
1336 .
1337 .  {* High 16 bits of 32-bit value; simple reloc.  *}
1338 .  BFD_RELOC_HI16,
1339 .  {* High 16 bits of 32-bit value but the low 16 bits will be sign
1340 .     extended and added to form the final result.  If the low 16
1341 .     bits form a negative number, we need to add one to the high value
1342 .     to compensate for the borrow when the low bits are added.  *}
1343 .  BFD_RELOC_HI16_S,
1344 .  {* Low 16 bits.  *}
1345 .  BFD_RELOC_LO16,
1346 .
1347 .  {* relocation relative to the global pointer.  *}
1348 .#define BFD_RELOC_MIPS_GPREL BFD_RELOC_GPREL16
1349 .
1350 .  {* Relocation against a MIPS literal section.  *}
1351 .  BFD_RELOC_MIPS_LITERAL,
1352 .
1353 .  {* MIPS ELF relocations.  *}
1354 .  BFD_RELOC_MIPS_GOT16,
1355 .  BFD_RELOC_MIPS_CALL16,
1356 .#define BFD_RELOC_MIPS_GPREL32 BFD_RELOC_GPREL32
1357 .
1358 .  {* These are, so far, specific to HPPA processors.  I'm not sure that some
1359 .     don't duplicate other reloc types, such as BFD_RELOC_32 and _32_PCREL.
1360 .     Also, many more were in the list I got that don't fit in well in the
1361 .     model BFD uses, so I've omitted them for now.  If we do make this reloc
1362 .     type get used for code that really does implement the funky reloc types,
1363 .     they'll have to be added to this list.  *}
1364 .  BFD_RELOC_HPPA_32,
1365 .  BFD_RELOC_HPPA_11,
1366 .  BFD_RELOC_HPPA_14,
1367 .  BFD_RELOC_HPPA_17,
1368 .
1369 .  BFD_RELOC_HPPA_L21,
1370 .  BFD_RELOC_HPPA_R11,
1371 .  BFD_RELOC_HPPA_R14,
1372 .  BFD_RELOC_HPPA_R17,
1373 .  BFD_RELOC_HPPA_LS21,
1374 .  BFD_RELOC_HPPA_RS11,
1375 .  BFD_RELOC_HPPA_RS14,
1376 .  BFD_RELOC_HPPA_RS17,
1377 .  BFD_RELOC_HPPA_LD21,
1378 .  BFD_RELOC_HPPA_RD11,
1379 .  BFD_RELOC_HPPA_RD14,
1380 .  BFD_RELOC_HPPA_RD17,
1381 .  BFD_RELOC_HPPA_LR21,
1382 .  BFD_RELOC_HPPA_RR14,
1383 .  BFD_RELOC_HPPA_RR17,
1384 .
1385 .  BFD_RELOC_HPPA_GOTOFF_11,
1386 .  BFD_RELOC_HPPA_GOTOFF_14,
1387 .  BFD_RELOC_HPPA_GOTOFF_L21,
1388 .  BFD_RELOC_HPPA_GOTOFF_R11,
1389 .  BFD_RELOC_HPPA_GOTOFF_R14,
1390 .  BFD_RELOC_HPPA_GOTOFF_LS21,
1391 .  BFD_RELOC_HPPA_GOTOFF_RS11,
1392 .  BFD_RELOC_HPPA_GOTOFF_RS14,
1393 .  BFD_RELOC_HPPA_GOTOFF_LD21,
1394 .  BFD_RELOC_HPPA_GOTOFF_RD11,
1395 .  BFD_RELOC_HPPA_GOTOFF_RD14,
1396 .  BFD_RELOC_HPPA_GOTOFF_LR21,
1397 .  BFD_RELOC_HPPA_GOTOFF_RR14,
1398 .
1399 .  BFD_RELOC_HPPA_DLT_32,
1400 .  BFD_RELOC_HPPA_DLT_11,
1401 .  BFD_RELOC_HPPA_DLT_14,
1402 .  BFD_RELOC_HPPA_DLT_L21,
1403 .  BFD_RELOC_HPPA_DLT_R11,
1404 .  BFD_RELOC_HPPA_DLT_R14,
1405 .
1406 .  BFD_RELOC_HPPA_ABS_CALL_11,
1407 .  BFD_RELOC_HPPA_ABS_CALL_14,
1408 .  BFD_RELOC_HPPA_ABS_CALL_17,
1409 .  BFD_RELOC_HPPA_ABS_CALL_L21,
1410 .  BFD_RELOC_HPPA_ABS_CALL_R11,
1411 .  BFD_RELOC_HPPA_ABS_CALL_R14,
1412 .  BFD_RELOC_HPPA_ABS_CALL_R17,
1413 .  BFD_RELOC_HPPA_ABS_CALL_LS21,
1414 .  BFD_RELOC_HPPA_ABS_CALL_RS11,
1415 .  BFD_RELOC_HPPA_ABS_CALL_RS14,
1416 .  BFD_RELOC_HPPA_ABS_CALL_RS17,
1417 .  BFD_RELOC_HPPA_ABS_CALL_LD21,
1418 .  BFD_RELOC_HPPA_ABS_CALL_RD11,
1419 .  BFD_RELOC_HPPA_ABS_CALL_RD14,
1420 .  BFD_RELOC_HPPA_ABS_CALL_RD17,
1421 .  BFD_RELOC_HPPA_ABS_CALL_LR21,
1422 .  BFD_RELOC_HPPA_ABS_CALL_RR14,
1423 .  BFD_RELOC_HPPA_ABS_CALL_RR17,
1424 .
1425 .  BFD_RELOC_HPPA_PCREL_CALL_11,
1426 .  BFD_RELOC_HPPA_PCREL_CALL_12,
1427 .  BFD_RELOC_HPPA_PCREL_CALL_14,
1428 .  BFD_RELOC_HPPA_PCREL_CALL_17,
1429 .  BFD_RELOC_HPPA_PCREL_CALL_L21,
1430 .  BFD_RELOC_HPPA_PCREL_CALL_R11,
1431 .  BFD_RELOC_HPPA_PCREL_CALL_R14,
1432 .  BFD_RELOC_HPPA_PCREL_CALL_R17,
1433 .  BFD_RELOC_HPPA_PCREL_CALL_LS21,
1434 .  BFD_RELOC_HPPA_PCREL_CALL_RS11,
1435 .  BFD_RELOC_HPPA_PCREL_CALL_RS14,
1436 .  BFD_RELOC_HPPA_PCREL_CALL_RS17,
1437 .  BFD_RELOC_HPPA_PCREL_CALL_LD21,
1438 .  BFD_RELOC_HPPA_PCREL_CALL_RD11,
1439 .  BFD_RELOC_HPPA_PCREL_CALL_RD14,
1440 .  BFD_RELOC_HPPA_PCREL_CALL_RD17,
1441 .  BFD_RELOC_HPPA_PCREL_CALL_LR21,
1442 .  BFD_RELOC_HPPA_PCREL_CALL_RR14,
1443 .  BFD_RELOC_HPPA_PCREL_CALL_RR17,
1444 .
1445 .  BFD_RELOC_HPPA_PLABEL_32,
1446 .  BFD_RELOC_HPPA_PLABEL_11,
1447 .  BFD_RELOC_HPPA_PLABEL_14,
1448 .  BFD_RELOC_HPPA_PLABEL_L21,
1449 .  BFD_RELOC_HPPA_PLABEL_R11,
1450 .  BFD_RELOC_HPPA_PLABEL_R14,
1451 .
1452 .  BFD_RELOC_HPPA_UNWIND_ENTRY,
1453 .  BFD_RELOC_HPPA_UNWIND_ENTRIES,
1454 .
1455 .  {* i386/elf relocations *}
1456 .  BFD_RELOC_386_GOT32,
1457 .  BFD_RELOC_386_PLT32,
1458 .  BFD_RELOC_386_COPY,
1459 .  BFD_RELOC_386_GLOB_DAT,
1460 .  BFD_RELOC_386_JUMP_SLOT,
1461 .  BFD_RELOC_386_RELATIVE,
1462 .  BFD_RELOC_386_GOTOFF,
1463 .  BFD_RELOC_386_GOTPC,
1464 .
1465 .  {* PowerPC/POWER (RS/6000) relocs.  *}
1466 .  {* 26 bit relative branch.  Low two bits must be zero.  High 24
1467 .     bits installed in bits 6 through 29 of instruction.  *}
1468 .  BFD_RELOC_PPC_B26,
1469 .  {* 26 bit absolute branch, like BFD_RELOC_PPC_B26 but absolute.  *}
1470 .  BFD_RELOC_PPC_BA26,
1471 .  {* 16 bit TOC relative reference.  *}
1472 .  BFD_RELOC_PPC_TOC16,
1473 .
1474 .  {* this must be the highest numeric value *}
1475 .  BFD_RELOC_UNUSED
1476 . } bfd_reloc_code_real_type;
1477 */
1478
1479
1480 /*
1481 FUNCTION
1482         bfd_reloc_type_lookup
1483
1484 SYNOPSIS
1485         const struct reloc_howto_struct *
1486         bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
1487
1488 DESCRIPTION
1489         Return a pointer to a howto structure which, when
1490         invoked, will perform the relocation @var{code} on data from the
1491         architecture noted.
1492
1493 */
1494
1495
1496 const struct reloc_howto_struct *
1497 DEFUN(bfd_reloc_type_lookup,(abfd, code),
1498       bfd *abfd AND
1499       bfd_reloc_code_real_type code)
1500 {
1501   return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
1502 }
1503
1504 static reloc_howto_type bfd_howto_32 =
1505  HOWTO(0, 00,2,32,false,0,complain_overflow_bitfield,0,"VRT32", false,0xffffffff,0xffffffff,true);
1506
1507
1508 /*
1509 INTERNAL_FUNCTION
1510         bfd_default_reloc_type_lookup
1511
1512 SYNOPSIS
1513         const struct reloc_howto_struct *bfd_default_reloc_type_lookup
1514         (bfd *abfd AND
1515          bfd_reloc_code_real_type  code);
1516
1517 DESCRIPTION
1518         Provides a default relocation lookup routine for any architecture.
1519
1520
1521 */
1522
1523 const struct reloc_howto_struct *
1524 DEFUN(bfd_default_reloc_type_lookup, (abfd, code),
1525       bfd *abfd AND
1526       bfd_reloc_code_real_type code)
1527 {
1528   switch (code) 
1529     {
1530     case BFD_RELOC_CTOR:
1531       /* The type of reloc used in a ctor, which will be as wide as the
1532          address - so either a 64, 32, or 16 bitter.  */
1533       switch (bfd_get_arch_info (abfd)->bits_per_address) {
1534       case 64:
1535         BFD_FAIL();
1536       case 32:
1537         return &bfd_howto_32;
1538       case 16:
1539         BFD_FAIL();
1540       default:
1541         BFD_FAIL();
1542       }
1543     default:
1544       BFD_FAIL();
1545     }
1546   return (const struct reloc_howto_struct *)NULL;
1547 }
1548
1549
1550 /*
1551 INTERNAL_FUNCTION
1552         bfd_generic_relax_section
1553
1554 SYNOPSIS
1555         boolean bfd_generic_relax_section
1556          (bfd *abfd,
1557           asection *section,
1558           struct bfd_link_info *,
1559           asymbol **symbols);
1560
1561 DESCRIPTION
1562         Provides default handling for relaxing for back ends which
1563         don't do relaxing -- i.e., does nothing.
1564 */
1565
1566 /*ARGSUSED*/
1567 boolean
1568 bfd_generic_relax_section (abfd, section, link_info, symbols)
1569      bfd *abfd;
1570      asection *section;
1571      struct bfd_link_info *link_info;
1572      asymbol **symbols;
1573 {
1574   return false;
1575 }
1576                 
1577 /*
1578 INTERNAL_FUNCTION
1579         bfd_generic_get_relocated_section_contents
1580
1581 SYNOPSIS
1582         bfd_byte *
1583            bfd_generic_get_relocated_section_contents (bfd *abfd,
1584              struct bfd_link_info *link_info,
1585              struct bfd_link_order *link_order,
1586              bfd_byte *data,
1587              boolean relocateable,
1588              asymbol **symbols);
1589
1590 DESCRIPTION
1591         Provides default handling of relocation effort for back ends
1592         which can't be bothered to do it efficiently.
1593
1594 */
1595
1596 bfd_byte *
1597 bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data,
1598                                             relocateable, symbols)
1599      bfd *abfd;
1600      struct bfd_link_info *link_info;
1601      struct bfd_link_order *link_order;
1602      bfd_byte *data;
1603      boolean relocateable;
1604      asymbol **symbols;
1605 {
1606   /* Get enough memory to hold the stuff */
1607   bfd *input_bfd = link_order->u.indirect.section->owner;
1608   asection *input_section = link_order->u.indirect.section;
1609
1610   size_t reloc_size = bfd_get_reloc_upper_bound(input_bfd, input_section);
1611   arelent **reloc_vector = NULL;
1612
1613   reloc_vector = (arelent **) malloc (reloc_size);
1614   if (reloc_vector == NULL)
1615     {
1616       bfd_set_error (bfd_error_no_memory);
1617       goto error_return;
1618     }
1619   
1620   /* read in the section */
1621   if (!bfd_get_section_contents(input_bfd,
1622                                 input_section,
1623                                 (PTR) data,
1624                                 0,
1625                                 input_section->_raw_size))
1626     goto error_return;
1627
1628   /* We're not relaxing the section, so just copy the size info */
1629   input_section->_cooked_size = input_section->_raw_size;
1630   input_section->reloc_done = true;
1631
1632   if (!bfd_canonicalize_reloc (input_bfd, 
1633                                input_section,
1634                                reloc_vector,
1635                                symbols))
1636     goto error_return;
1637
1638   {
1639     arelent **parent;
1640     for (parent = reloc_vector;  * parent != (arelent *)NULL;
1641          parent++) 
1642     { 
1643       char *error_message = (char *) NULL;
1644       bfd_reloc_status_type r=
1645        bfd_perform_relocation(input_bfd,
1646                               *parent,
1647                               (PTR) data,
1648                               input_section,
1649                               relocateable ? abfd : (bfd *) NULL,
1650                               &error_message);
1651       
1652       if (relocateable)
1653         {
1654           asection *os = input_section->output_section;
1655
1656           /* A partial link, so keep the relocs */
1657           os->orelocation[os->reloc_count] = *parent;
1658           os->reloc_count++;
1659         }
1660
1661       if (r != bfd_reloc_ok) 
1662       {
1663         switch (r)
1664         {
1665         case bfd_reloc_undefined:
1666           if (! ((*link_info->callbacks->undefined_symbol)
1667                  (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
1668                   input_bfd, input_section, (*parent)->address)))
1669             goto error_return;
1670           break;
1671         case bfd_reloc_dangerous: 
1672           BFD_ASSERT (error_message != (char *) NULL);
1673           if (! ((*link_info->callbacks->reloc_dangerous)
1674                  (link_info, error_message, input_bfd, input_section,
1675                   (*parent)->address)))
1676             goto error_return;
1677           break;
1678         case bfd_reloc_overflow:
1679           if (! ((*link_info->callbacks->reloc_overflow)
1680                  (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
1681                   (*parent)->howto->name, (*parent)->addend,
1682                   input_bfd, input_section, (*parent)->address)))
1683             goto error_return;
1684           break;
1685         case bfd_reloc_outofrange:
1686         default:
1687           abort();
1688           break;
1689         }
1690
1691       }
1692     }    
1693   }
1694   if (reloc_vector != NULL)
1695     free (reloc_vector);
1696   return data;
1697
1698  error_return:
1699   if (reloc_vector != NULL)
1700     free (reloc_vector);
1701   return NULL;
1702 }