(bfd_perform_relocation): Handle 64-bit relocs.
[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 was as it maintains
26         symbols; they are left alone until required, then read in
27         en-mass and traslated into an internal form. There is a common
28         routine <<bfd_perform_relocation>> which acts upon the
29         canonical form to to the actual fixup.
30
31         Note that relocations are maintained on a per section basis,
32         whilst symbols are maintained on a per BFD basis.
33
34         All a back end has to do to fit the BFD interface is to create
35         as many <<struct reloc_cache_entry>> as there are relocations
36         in a particular section, and fill in the right bits:
37
38 @menu
39 @* typedef arelent::
40 @* howto manager::
41 @end menu
42
43 */
44 #include "bfd.h"
45 #include "sysdep.h"
46 #include "libbfd.h"
47 #include "seclet.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 .       {* Unused *}
75 .  bfd_reloc_notsupported,
76 .
77 .       {* Unsupported relocation size requested. *}
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. *}
86 .  bfd_reloc_dangerous
87 . }
88 . bfd_reloc_status_type;
89 .
90 .
91 .typedef struct reloc_cache_entry 
92 .{
93 .       {* A pointer into the canonical table of pointers  *}
94 .  struct symbol_cache_entry **sym_ptr_ptr;
95 .
96 .       {* offset in section *}
97 .  bfd_size_type address;
98 .
99 .       {* addend for relocation value *}
100 .  bfd_vma addend;    
101 .
102 .       {* Pointer to how to perform the required relocation *}
103 .  CONST struct reloc_howto_struct *howto;
104 .
105 .} arelent;
106
107 */
108
109 /*
110 DESCRIPTION
111
112         Here is a description of each of the fields within a relent:
113
114         o sym_ptr_ptr
115
116         The symbol table pointer points to a pointer to the symbol
117         associated with the relocation request. This would naturally
118         be the pointer into the table returned by the back end's
119         get_symtab action. @xref{Symbols}. The symbol is referenced
120         through a pointer to a pointer so that tools like the linker
121         can fix up all the symbols of the same name by modifying only
122         one pointer. The relocation routine looks in the symbol and
123         uses the base of the section the symbol is attached to and the
124         value of the symbol as the initial relocation offset. If the
125         symbol pointer is zero, then the section provided is looked up.
126
127         o address
128
129         The address field gives the offset in bytes from the base of
130         the section data which owns the relocation record to the first
131         byte of relocatable information. The actual data relocated
132         will be relative to this point - for example, a relocation
133         type which modifies the bottom two bytes of a four byte word
134         would not touch the first byte pointed to in a big endian
135         world.
136         
137         o addend
138
139         The addend is a value provided by the back end to be added (!)
140         to the relocation offset. Its interpretation is dependent upon
141         the howto. For example, on the 68k the code:
142
143
144 |        char foo[];
145 |        main()
146 |                {
147 |                return foo[0x12345678];
148 |                }
149
150         Could be compiled into:
151
152 |        linkw fp,#-4
153 |        moveb @@#12345678,d0
154 |        extbl d0
155 |        unlk fp
156 |        rts
157
158
159         This could create a reloc pointing to foo, but leave the
160         offset in the data (something like)
161
162
163 |RELOCATION RECORDS FOR [.text]:
164 |offset   type      value 
165 |00000006 32        _foo
166 |
167 |00000000 4e56 fffc          ; linkw fp,#-4
168 |00000004 1039 1234 5678     ; moveb @@#12345678,d0
169 |0000000a 49c0               ; extbl d0
170 |0000000c 4e5e               ; unlk fp
171 |0000000e 4e75               ; rts
172
173
174         Using coff and an 88k, some instructions don't have enough
175         space in them to represent the full address range, and
176         pointers have to be loaded in two parts. So you'd get something like:
177
178
179 |        or.u     r13,r0,hi16(_foo+0x12345678)
180 |        ld.b     r2,r13,lo16(_foo+0x12345678)
181 |        jmp      r1
182
183
184         This should create two relocs, both pointing to _foo, and with
185         0x12340000 in their addend field. The data would consist of:
186
187
188 |RELOCATION RECORDS FOR [.text]:
189 |offset   type      value 
190 |00000002 HVRT16    _foo+0x12340000
191 |00000006 LVRT16    _foo+0x12340000
192
193 |00000000 5da05678           ; or.u r13,r0,0x5678
194 |00000004 1c4d5678           ; ld.b r2,r13,0x5678
195 |00000008 f400c001           ; jmp r1
196
197
198         The relocation routine digs out the value from the data, adds
199         it to the addend to get the original offset and then adds the
200         value of _foo. Note that all 32 bits have to be kept around
201         somewhere, to cope with carry from bit 15 to bit 16.
202
203         One further example is the sparc and the a.out format. The
204         sparc has a similar problem to the 88k, in that some
205         instructions don't have room for an entire offset, but on the
206         sparc the parts are created odd sized lumps. The designers of
207         the a.out format chose not to use the data within the section
208         for storing part of the offset; all the offset is kept within
209         the reloc. Any thing in the data should be ignored. 
210
211 |        save %sp,-112,%sp
212 |        sethi %hi(_foo+0x12345678),%g2
213 |        ldsb [%g2+%lo(_foo+0x12345678)],%i0
214 |        ret
215 |        restore
216
217         Both relocs contains a pointer to foo, and the offsets would
218         contain junk.
219
220
221 |RELOCATION RECORDS FOR [.text]:
222 |offset   type      value 
223 |00000004 HI22      _foo+0x12345678
224 |00000008 LO10      _foo+0x12345678
225
226 |00000000 9de3bf90     ; save %sp,-112,%sp
227 |00000004 05000000     ; sethi %hi(_foo+0),%g2
228 |00000008 f048a000     ; ldsb [%g2+%lo(_foo+0)],%i0
229 |0000000c 81c7e008     ; ret
230 |00000010 81e80000     ; restore
231
232
233         o howto 
234
235         The howto field can be imagined as a
236         relocation instruction. It is a pointer to a struct which
237         contains information on what to do with all the other
238         information in the reloc record and data section. A back end
239         would normally have a relocation instruction set and turn
240         relocations into pointers to the correct structure on input -
241         but it would be possible to create each howto field on demand.
242         
243 */
244
245 /*
246 SUBSUBSECTION
247         <<enum complain_overflow>>
248
249         Indicates what sort of overflow checking should be done when
250         performing a relocation.
251
252 CODE_FRAGMENT
253 .
254 .enum complain_overflow
255 .{
256 .       {* Do not complain on overflow. *}
257 .  complain_overflow_dont,
258 .
259 .       {* Complain if the bitfield overflows, whether it is considered
260 .          as signed or unsigned. *}
261 .  complain_overflow_bitfield,
262 .
263 .       {* Complain if the value overflows when considered as signed
264 .          number. *}
265 .  complain_overflow_signed,
266 .
267 .       {* Complain if the value overflows when considered as an
268 .          unsigned number. *}
269 .  complain_overflow_unsigned
270 .};
271
272 */
273
274 /*
275 SUBSUBSECTION 
276         <<reloc_howto_type>>
277
278         The <<reloc_howto_type>> is a structure which contains all the
279         information that BFD needs to know to tie up a back end's data.
280
281 CODE_FRAGMENT
282 .struct symbol_cache_entry;             {* Forward declaration *}
283 .
284 .typedef CONST struct reloc_howto_struct 
285 .{ 
286 .       {*  The type field has mainly a documetary use - the back end can
287 .           to what it wants with it, though the normally the back end's
288 .           external idea of what a reloc number would be would be stored
289 .           in this field. For example, the a PC relative word relocation
290 .           in a coff environment would have the type 023 - because that's
291 .           what the outside world calls a R_PCRWORD reloc. *}
292 .  unsigned int type;
293 .
294 .       {*  The value the final relocation is shifted right by. This drops
295 .           unwanted data from the relocation.  *}
296 .  unsigned int rightshift;
297 .
298 .       {*  The size of the item to be relocated - 0, is one byte, 1 is 2
299 .           bytes, 2 is four bytes.  A negative value indicates that the
300 .           result is to be subtracted from the data.  *}
301 .  int size;
302 .
303 .       {*  The number of bits in the item to be relocated.  This is used
304 .           when doing overflow checking.  *}
305 .  unsigned int bitsize;
306 .
307 .       {*  Notes that the relocation is relative to the location in the
308 .           data section of the addend. The relocation function will
309 .           subtract from the relocation value the address of the location
310 .           being relocated. *}
311 .  boolean pc_relative;
312 .
313 .       {*  The bit position of the reloc value in the destination.
314 .           The relocated value is left shifted by this amount. *}
315 .  unsigned int bitpos;
316 .
317 .       {* What type of overflow error should be checked for when
318 .          relocating. *}
319 .  enum complain_overflow complain_on_overflow;
320 .
321 .       {* If this field is non null, then the supplied function is
322 .          called rather than the normal function. This allows really
323 .          strange relocation methods to be accomodated (e.g., i960 callj
324 .          instructions). *}
325 .  bfd_reloc_status_type (*special_function) 
326 .                                   PARAMS ((bfd *abfd,
327 .                                            arelent *reloc_entry,
328 .                                            struct symbol_cache_entry *symbol,
329 .                                            PTR data,
330 .                                            asection *input_section, 
331 .                                            bfd *output_bfd));
332 .
333 .       {* The textual name of the relocation type. *}
334 .  char *name;
335 .
336 .       {* When performing a partial link, some formats must modify the
337 .          relocations rather than the data - this flag signals this.*}
338 .  boolean partial_inplace;
339 .
340 .       {* The src_mask is used to select what parts of the read in data
341 .          are to be used in the relocation sum.  E.g., if this was an 8 bit
342 .          bit of data which we read and relocated, this would be
343 .          0x000000ff. When we have relocs which have an addend, such as
344 .          sun4 extended relocs, the value in the offset part of a
345 .          relocating field is garbage so we never use it. In this case
346 .          the mask would be 0x00000000. *}
347 .  bfd_vma src_mask;
348 .
349 .       {* The dst_mask is what parts of the instruction are replaced
350 .          into the instruction. In most cases src_mask == dst_mask,
351 .          except in the above special case, where dst_mask would be
352 .          0x000000ff, and src_mask would be 0x00000000.   *}
353 .  bfd_vma dst_mask;           
354 .
355 .       {* When some formats create PC relative instructions, they leave
356 .          the value of the pc of the place being relocated in the offset
357 .          slot of the instruction, so that a PC relative relocation can
358 .          be made just by adding in an ordinary offset (e.g., sun3 a.out).
359 .          Some formats leave the displacement part of an instruction
360 .          empty (e.g., m88k bcs), this flag signals the fact.*}
361 .  boolean pcrel_offset;
362 .
363 .} reloc_howto_type;
364
365 */
366
367 /*
368 FUNCTION
369         the HOWTO macro
370
371 DESCRIPTION
372         The HOWTO define is horrible and will go away.
373
374
375 .#define HOWTO(C, R,S,B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
376 .  {(unsigned)C,R,S,B, P, BI, O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
377
378 DESCRIPTION
379         And will be replaced with the totally magic way. But for the
380         moment, we are compatible, so do it this way..
381
382
383 .#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,complain_overflow_dont,FUNCTION, NAME,false,0,0,IN)
384 .
385 DESCRIPTION
386         Helper routine to turn a symbol into a relocation value.
387
388 .#define HOWTO_PREPARE(relocation, symbol)      \
389 .  {                                            \
390 .  if (symbol != (asymbol *)NULL) {             \
391 .    if (bfd_is_com_section (symbol->section)) { \
392 .      relocation = 0;                          \
393 .    }                                          \
394 .    else {                                     \
395 .      relocation = symbol->value;              \
396 .    }                                          \
397 .  }                                            \
398 .}                      
399
400 */
401
402 /*
403 TYPEDEF
404         reloc_chain
405
406 DESCRIPTION
407
408         How relocs are tied together
409
410 .typedef unsigned char bfd_byte;
411 .
412 .typedef struct relent_chain {
413 .  arelent relent;
414 .  struct   relent_chain *next;
415 .} arelent_chain;
416
417 */
418
419
420
421 /*
422 FUNCTION 
423         bfd_perform_relocation
424
425 SYNOPSIS
426         bfd_reloc_status_type
427                 bfd_perform_relocation
428                         (bfd * abfd,
429                         arelent *reloc_entry,
430                         PTR data,
431                         asection *input_section,
432                         bfd *output_bfd);
433
434 DESCRIPTION
435         If an output_bfd is supplied to this function the generated
436         image will be relocatable, the relocations are copied to the
437         output file after they have been changed to reflect the new
438         state of the world. There are two ways of reflecting the
439         results of partial linkage in an output file; by modifying the
440         output data in place, and by modifying the relocation record.
441         Some native formats (e.g., basic a.out and basic coff) have no
442         way of specifying an addend in the relocation type, so the
443         addend has to go in the output data.  This is no big deal
444         since in these formats the output data slot will always be big
445         enough for the addend. Complex reloc types with addends were
446         invented to solve just this problem.
447
448 */
449
450
451 bfd_reloc_status_type
452 DEFUN(bfd_perform_relocation,(abfd,
453                               reloc_entry,
454                               data,
455                               input_section,
456                               output_bfd),
457       bfd *abfd AND
458       arelent *reloc_entry AND
459       PTR data AND
460       asection *input_section AND
461       bfd *output_bfd)
462 {
463   bfd_vma relocation;
464   bfd_reloc_status_type flag = bfd_reloc_ok;
465   bfd_size_type addr = reloc_entry->address ;
466   bfd_vma output_base = 0;
467   reloc_howto_type *howto = reloc_entry->howto;
468   asection *reloc_target_output_section ;
469
470   asymbol *symbol;
471
472   symbol = *( reloc_entry->sym_ptr_ptr);
473   if ((symbol->section == &bfd_abs_section) 
474       && output_bfd != (bfd *)NULL) 
475     {
476       reloc_entry->address += input_section->output_offset;
477       return bfd_reloc_ok;
478     }
479
480   if ((symbol->section == &bfd_und_section) && output_bfd == (bfd *)NULL)
481     flag = bfd_reloc_undefined;
482
483   /* If there is a function supplied to handle this relocation type,
484      call it.  It'll return `bfd_reloc_continue' if further processing
485      can be done.  */
486   if (howto->special_function)
487     {
488       bfd_reloc_status_type cont;
489       cont = howto->special_function (abfd, reloc_entry, symbol, data,
490                                       input_section, output_bfd);
491       if (cont != bfd_reloc_continue)
492         return cont;
493     }
494
495   /* Is the address of the relocation really within the section?  */
496   if (reloc_entry->address > input_section->_cooked_size)
497     return bfd_reloc_outofrange;
498
499   /* Work out which section the relocation is targetted at and the
500      initial relocation command value.  */
501
502   /* Get symbol value.  (Common symbols are special.)  */
503   if (bfd_is_com_section (symbol->section))
504     relocation = 0;
505   else
506     relocation = symbol->value;
507
508
509   reloc_target_output_section = symbol->section->output_section;
510
511   /* Convert input-section-relative symbol value to absolute.  */
512   if (output_bfd && howto->partial_inplace==false)
513     output_base = 0;
514   else
515     output_base = reloc_target_output_section->vma;
516
517   relocation += output_base + symbol->section->output_offset;
518
519   /* Add in supplied addend.  */
520   relocation += reloc_entry->addend;
521
522   if (howto->pc_relative == true)
523     {
524       /* Anything which started out as pc relative should end up that
525          way too. 
526
527          There are two ways we can see a pcrel instruction. Sometimes
528          the pcrel displacement has been partially calculated, it
529          includes the distance from the start of the section to the
530          instruction in it (e.g., sun3), and sometimes the field is
531          totally blank - e.g., m88kbcs.  */
532
533       relocation -= 
534         input_section->output_section->vma + input_section->output_offset;
535
536       if (howto->pcrel_offset == true)
537         relocation -= reloc_entry->address;
538     }
539
540   if (output_bfd!= (bfd *)NULL) 
541     {
542       if ( howto->partial_inplace == false)  
543         {
544           /* This is a partial relocation, and we want to apply the relocation
545              to the reloc entry rather than the raw data. Modify the reloc
546              inplace to reflect what we now know.  */
547           reloc_entry->addend = relocation;
548           reloc_entry->address +=  input_section->output_offset;
549           return flag;
550         }
551       else
552         {
553           /* This is a partial relocation, but inplace, so modify the
554              reloc record a bit. 
555
556              If we've relocated with a symbol with a section, change
557              into a ref to the section belonging to the symbol.  */
558
559           reloc_entry->address += input_section->output_offset;
560
561           /* WTF?? */
562           if (abfd->xvec->flavour == bfd_target_coff_flavour) 
563             {
564               relocation -= reloc_entry->addend;
565               reloc_entry->addend = 0;
566             }
567           else
568             {
569               reloc_entry->addend = relocation;
570             }
571         }
572     }
573   else 
574     {
575       reloc_entry->addend = 0;
576     }
577
578   /* FIXME: This overflow checking is incomplete, because the value
579      might have overflowed before we get here.  For a correct check we
580      need to compute the value in a size larger than bitsize, but we
581      can't reasonably do that for a reloc the same size as a host
582      machine word.  */
583   if (howto->complain_on_overflow != complain_overflow_dont)
584     {
585       bfd_vma check;
586
587       /* Get the value that will be used for the relocation, but
588          starting at bit position zero.  */
589       if (howto->rightshift > howto->bitpos)
590         check = relocation >> (howto->rightshift - howto->bitpos);
591       else
592         check = relocation << (howto->bitpos - howto->rightshift);
593       switch (howto->complain_on_overflow)
594         {
595         case complain_overflow_signed:
596           {
597             /* Assumes two's complement.  */
598             bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
599             bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
600
601             /* The above right shift is incorrect for a signed value.
602                Fix it up by forcing on the upper bits.  */
603             if (howto->rightshift > howto->bitpos
604                 && (bfd_signed_vma) relocation < 0)
605               check |= ((bfd_vma) -1
606                         &~ ((bfd_vma) -1
607                             >> (howto->rightshift - howto->bitpos)));
608             if ((bfd_signed_vma) check > reloc_signed_max
609                 || (bfd_signed_vma) check < reloc_signed_min)
610               flag = bfd_reloc_overflow;
611           }
612           break;
613         case complain_overflow_unsigned:
614           {
615             /* Assumes two's complement.  This expression avoids
616                overflow if howto->bitsize is the number of bits in
617                bfd_vma.  */
618             bfd_vma reloc_unsigned_max =
619               (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
620
621             if ((bfd_vma) check > reloc_unsigned_max)
622               flag = bfd_reloc_overflow;
623           }
624           break;
625         case complain_overflow_bitfield:
626           {
627             /* Assumes two's complement.  This expression avoids
628                overflow if howto->bitsize is the number of bits in
629                bfd_vma.  */
630             bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
631
632             if (((bfd_vma) check &~ reloc_bits) != 0
633                 && ((bfd_vma) check &~ reloc_bits) != (-1 &~ reloc_bits))
634               flag = bfd_reloc_overflow;
635           }
636           break;
637         default:
638           abort ();
639         }
640     }
641   
642   /* 
643     Either we are relocating all the way, or we don't want to apply
644     the relocation to the reloc entry (probably because there isn't
645     any room in the output format to describe addends to relocs)
646     */
647   relocation >>= howto->rightshift;
648
649   /* Shift everything up to where it's going to be used */
650    
651   relocation <<= howto->bitpos;
652
653   /* Wait for the day when all have the mask in them */
654
655   /* What we do:
656      i instruction to be left alone
657      o offset within instruction
658      r relocation offset to apply
659      S src mask
660      D dst mask
661      N ~dst mask
662      A part 1
663      B part 2
664      R result
665      
666      Do this:
667      i i i i i o o o o o        from bfd_get<size>
668      and           S S S S S    to get the size offset we want
669      +   r r r r r r r r r r  to get the final value to place
670      and           D D D D D  to chop to right size
671      -----------------------
672      A A A A A 
673      And this:
674      ...   i i i i i o o o o o  from bfd_get<size>
675      and   N N N N N            get instruction
676      -----------------------
677      ...   B B B B B
678      
679      And then:       
680      B B B B B       
681      or              A A A A A     
682      -----------------------
683      R R R R R R R R R R        put into bfd_put<size>
684      */
685
686 #define DOIT(x) \
687   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))
688
689    switch (howto->size)
690      {
691      case 0:
692        {
693          char x = bfd_get_8(abfd, (char *)data + addr);
694          DOIT(x);
695          bfd_put_8(abfd,x, (unsigned char *) data + addr);
696        }
697        break;
698
699      case 1:
700        if (relocation)
701          {
702            short x = bfd_get_16(abfd, (bfd_byte *)data + addr);
703            DOIT(x);
704            bfd_put_16(abfd, x,   (unsigned char *)data + addr);
705          }
706        break;
707      case 2:
708        if (relocation)
709          {
710            long  x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
711            DOIT (x);
712            bfd_put_32 (abfd, x, (bfd_byte *)data + addr);
713          }
714        break;
715      case -2:
716        {
717          long  x = bfd_get_32(abfd, (bfd_byte *) data + addr);
718          relocation = -relocation;
719          DOIT(x);
720          bfd_put_32(abfd,x,    (bfd_byte *)data + addr);
721        }
722        break;
723
724      case 3:
725        /* Do nothing */
726        break;
727
728      case 4:
729 #ifdef BFD64
730        if (relocation)
731          {
732            bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + addr);
733            DOIT (x);
734            bfd_put_64 (abfd, x, (bfd_byte *) data + addr);
735          }
736 #else
737        abort ();
738 #endif
739        break;
740      default:
741        return bfd_reloc_other;
742      }
743
744   return flag;
745 }
746
747
748
749 /*
750 DOCDD
751 INODE
752         howto manager,  , typedef arelent, Relocations
753
754 SECTION
755         The howto manager 
756
757         When an application wants to create a relocation, but doesn't
758         know what the target machine might call it, it can find out by
759         using this bit of code.
760
761 */
762
763 /*
764 TYPEDEF
765         bfd_reloc_code_type
766
767 DESCRIPTION
768         The insides of a reloc code
769
770 CODE_FRAGMENT
771 .
772 .typedef enum bfd_reloc_code_real 
773 .
774 .{
775 .       {* 64 bits wide, simple reloc *}
776 .  BFD_RELOC_64,
777 .       {* 64 bits, PC-relative *}
778 .  BFD_RELOC_64_PCREL,
779 .
780 .       {* 32 bits wide, simple reloc *}
781 .  BFD_RELOC_32,
782 .       {* 32 bits, PC-relative *}
783 .  BFD_RELOC_32_PCREL,
784 .
785 .       {* 16 bits wide, simple reloc *}
786 .  BFD_RELOC_16,        
787 .       {* 16 bits, PC-relative *}
788 .  BFD_RELOC_16_PCREL,
789 .
790 .       {* 8 bits wide, simple *}
791 .  BFD_RELOC_8,
792 .       {* 8 bits wide, pc relative *}
793 .  BFD_RELOC_8_PCREL,
794 .       {* 8 bits wide, but used to form an address like 0xffnn *}
795 .  BFD_RELOC_8_FFnn,
796 .
797 .       {* The type of reloc used to build a contructor table - at the
798 .          moment probably a 32 bit wide abs address, but the cpu can
799 .          choose. *}
800 .
801 .  BFD_RELOC_CTOR,
802 .
803 .       {* High 22 bits of 32-bit value; simple reloc.  *}
804 .  BFD_RELOC_HI22,
805 .       {* Low 10 bits.  *}
806 .  BFD_RELOC_LO10,
807 .
808 .       {* Reloc types used for i960/b.out.  *}
809 .  BFD_RELOC_24_PCREL,
810 .  BFD_RELOC_I960_CALLJ,
811 .
812 .       {* 32-bit pc-relative, shifted right 2 bits (i.e., 30-bit
813 .          word displacement, e.g. for SPARC) *}
814 .  BFD_RELOC_32_PCREL_S2,
815 .
816 .  {* now for the sparc/elf codes *}
817 .  BFD_RELOC_NONE,              {* actually used *}
818 .  BFD_RELOC_SPARC_WDISP22,
819 .  BFD_RELOC_SPARC22,
820 .  BFD_RELOC_SPARC13,
821 .  BFD_RELOC_SPARC_GOT10,
822 .  BFD_RELOC_SPARC_GOT13,
823 .  BFD_RELOC_SPARC_GOT22,
824 .  BFD_RELOC_SPARC_PC10,
825 .  BFD_RELOC_SPARC_PC22,
826 .  BFD_RELOC_SPARC_WPLT30,
827 .  BFD_RELOC_SPARC_COPY,
828 .  BFD_RELOC_SPARC_GLOB_DAT,
829 .  BFD_RELOC_SPARC_JMP_SLOT,
830 .  BFD_RELOC_SPARC_RELATIVE,
831 .  BFD_RELOC_SPARC_UA32,
832 .
833 .  {* this one is a.out specific? *}
834 .  BFD_RELOC_SPARC_BASE13,
835 .  BFD_RELOC_SPARC_BASE22,
836 .
837 .  {* start-sanitize-v9 *}
838 .  BFD_RELOC_SPARC_10,
839 .  BFD_RELOC_SPARC_11,
840 .#define  BFD_RELOC_SPARC_64 BFD_RELOC_64
841 .  BFD_RELOC_SPARC_OLO10,
842 .  BFD_RELOC_SPARC_HH22,
843 .  BFD_RELOC_SPARC_HM10,
844 .  BFD_RELOC_SPARC_LM22,
845 .  BFD_RELOC_SPARC_PC_HH22,
846 .  BFD_RELOC_SPARC_PC_HM10,
847 .  BFD_RELOC_SPARC_PC_LM22,
848 .  BFD_RELOC_SPARC_WDISP16,
849 .  BFD_RELOC_SPARC_WDISP19,
850 .  BFD_RELOC_SPARC_GLOB_JMP,
851 .  BFD_RELOC_SPARC_LO7,
852 .  {* end-sanitize-v9 *}
853 .
854 .       {* Bits 27..2 of the relocation address shifted right 2 bits;
855 .         simple reloc otherwise.  *}
856 .  BFD_RELOC_MIPS_JMP,
857 .
858 .       {* signed 16-bit pc-relative, shifted right 2 bits (e.g. for MIPS) *}
859 .  BFD_RELOC_16_PCREL_S2,
860 .
861 .       {* High 16 bits of 32-bit value; simple reloc.  *}
862 .  BFD_RELOC_HI16,
863 .       {* High 16 bits of 32-bit value but the low 16 bits will be sign
864 .          extended and added to form the final result.  If the low 16
865 .          bits form a negative number, we need to add one to the high value
866 .          to compensate for the borrow when the low bits are added.  *}
867 .  BFD_RELOC_HI16_S,
868 .       {* Low 16 bits.  *}
869 .  BFD_RELOC_LO16,
870 .
871 .       {* 16 bit relocation relative to the global pointer.  *}
872 .  BFD_RELOC_MIPS_GPREL,
873 .
874 .       {* These are, so far, specific to HPPA processors.  I'm not sure that
875 .          some don't duplicate other reloc types, such as BFD_RELOC_32 and
876 .          _32_PCREL.  Also, many more were in the list I got that don't
877 .          fit in well in the model BFD uses, so I've omitted them for now.
878 .          If we do make this reloc type get used for code that really does
879 .          implement the funky reloc types, they'll have to be added to this
880 .          list.   *}
881 .  BFD_RELOC_HPPA_32,
882 .  BFD_RELOC_HPPA_11,
883 .  BFD_RELOC_HPPA_14,
884 .  BFD_RELOC_HPPA_17,
885 .  BFD_RELOC_HPPA_L21,
886 .  BFD_RELOC_HPPA_R11,
887 .  BFD_RELOC_HPPA_R14,
888 .  BFD_RELOC_HPPA_R17,
889 .  BFD_RELOC_HPPA_LS21,
890 .  BFD_RELOC_HPPA_RS11,
891 .  BFD_RELOC_HPPA_RS14,
892 .  BFD_RELOC_HPPA_RS17,
893 .  BFD_RELOC_HPPA_LD21,
894 .  BFD_RELOC_HPPA_RD11,
895 .  BFD_RELOC_HPPA_RD14,
896 .  BFD_RELOC_HPPA_RD17,
897 .  BFD_RELOC_HPPA_LR21,
898 .  BFD_RELOC_HPPA_RR14,
899 .  BFD_RELOC_HPPA_RR17,
900 .  BFD_RELOC_HPPA_GOTOFF_11,
901 .  BFD_RELOC_HPPA_GOTOFF_14,
902 .  BFD_RELOC_HPPA_GOTOFF_L21,
903 .  BFD_RELOC_HPPA_GOTOFF_R11,
904 .  BFD_RELOC_HPPA_GOTOFF_R14,
905 .  BFD_RELOC_HPPA_GOTOFF_LS21,
906 .  BFD_RELOC_HPPA_GOTOFF_RS11,
907 .  BFD_RELOC_HPPA_GOTOFF_RS14,
908 .  BFD_RELOC_HPPA_GOTOFF_LD21,
909 .  BFD_RELOC_HPPA_GOTOFF_RD11,
910 .  BFD_RELOC_HPPA_GOTOFF_RD14,
911 .  BFD_RELOC_HPPA_GOTOFF_LR21,
912 .  BFD_RELOC_HPPA_GOTOFF_RR14,
913 .  BFD_RELOC_HPPA_DLT_32,
914 .  BFD_RELOC_HPPA_DLT_11,
915 .  BFD_RELOC_HPPA_DLT_14,
916 .  BFD_RELOC_HPPA_DLT_L21,
917 .  BFD_RELOC_HPPA_DLT_R11,
918 .  BFD_RELOC_HPPA_DLT_R14,
919 .  BFD_RELOC_HPPA_ABS_CALL_11,
920 .  BFD_RELOC_HPPA_ABS_CALL_14,
921 .  BFD_RELOC_HPPA_ABS_CALL_17,
922 .  BFD_RELOC_HPPA_ABS_CALL_L21,
923 .  BFD_RELOC_HPPA_ABS_CALL_R11,
924 .  BFD_RELOC_HPPA_ABS_CALL_R14,
925 .  BFD_RELOC_HPPA_ABS_CALL_R17,
926 .  BFD_RELOC_HPPA_ABS_CALL_LS21,
927 .  BFD_RELOC_HPPA_ABS_CALL_RS11,
928 .  BFD_RELOC_HPPA_ABS_CALL_RS14,
929 .  BFD_RELOC_HPPA_ABS_CALL_RS17,
930 .  BFD_RELOC_HPPA_ABS_CALL_LD21,
931 .  BFD_RELOC_HPPA_ABS_CALL_RD11,
932 .  BFD_RELOC_HPPA_ABS_CALL_RD14,
933 .  BFD_RELOC_HPPA_ABS_CALL_RD17,
934 .  BFD_RELOC_HPPA_ABS_CALL_LR21,
935 .  BFD_RELOC_HPPA_ABS_CALL_RR14,
936 .  BFD_RELOC_HPPA_ABS_CALL_RR17,
937 .  BFD_RELOC_HPPA_PCREL_CALL_11,
938 .  BFD_RELOC_HPPA_PCREL_CALL_12,
939 .  BFD_RELOC_HPPA_PCREL_CALL_14,
940 .  BFD_RELOC_HPPA_PCREL_CALL_17,
941 .  BFD_RELOC_HPPA_PCREL_CALL_L21,
942 .  BFD_RELOC_HPPA_PCREL_CALL_R11,
943 .  BFD_RELOC_HPPA_PCREL_CALL_R14,
944 .  BFD_RELOC_HPPA_PCREL_CALL_R17,
945 .  BFD_RELOC_HPPA_PCREL_CALL_LS21,
946 .  BFD_RELOC_HPPA_PCREL_CALL_RS11,
947 .  BFD_RELOC_HPPA_PCREL_CALL_RS14,
948 .  BFD_RELOC_HPPA_PCREL_CALL_RS17,
949 .  BFD_RELOC_HPPA_PCREL_CALL_LD21,
950 .  BFD_RELOC_HPPA_PCREL_CALL_RD11,
951 .  BFD_RELOC_HPPA_PCREL_CALL_RD14,
952 .  BFD_RELOC_HPPA_PCREL_CALL_RD17,
953 .  BFD_RELOC_HPPA_PCREL_CALL_LR21,
954 .  BFD_RELOC_HPPA_PCREL_CALL_RR14,
955 .  BFD_RELOC_HPPA_PCREL_CALL_RR17,
956 .  BFD_RELOC_HPPA_PLABEL_32,
957 .  BFD_RELOC_HPPA_PLABEL_11,
958 .  BFD_RELOC_HPPA_PLABEL_14,
959 .  BFD_RELOC_HPPA_PLABEL_L21,
960 .  BFD_RELOC_HPPA_PLABEL_R11,
961 .  BFD_RELOC_HPPA_PLABEL_R14,
962 .  BFD_RELOC_HPPA_UNWIND_ENTRY,
963 .  BFD_RELOC_HPPA_UNWIND_ENTRIES,
964 .
965 .  {* this must be the highest numeric value *}
966 .  BFD_RELOC_UNUSED
967 . } bfd_reloc_code_real_type;
968 */
969
970
971
972 /*
973 SECTION
974         bfd_reloc_type_lookup
975
976 SYNOPSIS
977         CONST struct reloc_howto_struct *
978         bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
979
980 DESCRIPTION
981         This routine returns a pointer to a howto struct which when
982         invoked, will perform the supplied relocation on data from the
983         architecture noted.
984
985 */
986
987
988 CONST struct reloc_howto_struct *
989 DEFUN(bfd_reloc_type_lookup,(abfd, code),
990       bfd *abfd AND
991       bfd_reloc_code_real_type code)
992 {
993   return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
994 }
995
996 static reloc_howto_type bfd_howto_32 =
997  HOWTO(0, 00,2,32,false,0,complain_overflow_bitfield,0,"VRT32", false,0xffffffff,0xffffffff,true);
998
999
1000 /*
1001 INTERNAL_FUNCTION
1002         bfd_default_reloc_type_lookup
1003
1004 SYNOPSIS
1005         CONST struct reloc_howto_struct *bfd_default_reloc_type_lookup
1006         (bfd *abfd AND
1007          bfd_reloc_code_real_type  code);
1008
1009 DESCRIPTION
1010         Provides a default relocation lookup routine for any architecture.
1011
1012
1013 */
1014
1015 CONST struct reloc_howto_struct *
1016 DEFUN(bfd_default_reloc_type_lookup, (abfd, code),
1017       bfd *abfd AND
1018       bfd_reloc_code_real_type code)
1019 {
1020   switch (code) 
1021     {
1022     case BFD_RELOC_CTOR:
1023       /* The type of reloc used in a ctor, which will be as wide as the
1024          address - so either a 64, 32, or 16 bitter.. */
1025       switch (bfd_get_arch_info (abfd)->bits_per_address) {
1026       case 64:
1027         BFD_FAIL();
1028       case 32:
1029         return &bfd_howto_32;
1030       case 16:
1031         BFD_FAIL();
1032       default:
1033         BFD_FAIL();
1034       }
1035     default:
1036       BFD_FAIL();
1037     }
1038   return (CONST struct reloc_howto_struct *)NULL;
1039 }
1040
1041
1042 /*
1043 INTERNAL_FUNCTION
1044         bfd_generic_relax_section
1045
1046 SYNOPSIS
1047         boolean bfd_generic_relax_section
1048          (bfd *abfd,
1049           asection *section,
1050           asymbol **symbols);
1051
1052 DESCRIPTION
1053         Provides default handling for relaxing for back ends which
1054         don't do relaxing -- i.e., does nothing.
1055 */
1056
1057 boolean
1058 DEFUN(bfd_generic_relax_section,(abfd, section, symbols),
1059       bfd *abfd AND
1060       asection *section AND
1061       asymbol **symbols)
1062 {
1063   
1064   return false;
1065   
1066 }
1067
1068                 
1069 /*
1070 INTERNAL_FUNCTION
1071         bfd_generic_get_relocated_section_contents
1072
1073 SYNOPSIS
1074         bfd_byte *
1075            bfd_generic_get_relocated_section_contents (bfd *abfd,
1076              struct bfd_seclet *seclet,
1077              bfd_byte *data,
1078              boolean relocateable);
1079
1080 DESCRIPTION
1081         Provides default handling of relocation effort for back ends
1082         which can't be bothered to do it efficiently.
1083
1084 */
1085
1086 bfd_byte *
1087 DEFUN(bfd_generic_get_relocated_section_contents,(abfd,
1088                                                   seclet,
1089                                                   data,
1090                                                   relocateable),
1091       bfd *abfd AND
1092       struct bfd_seclet *seclet AND
1093       bfd_byte *data AND
1094       boolean relocateable)
1095 {
1096   extern bfd_error_vector_type bfd_error_vector;
1097
1098   /* Get enough memory to hold the stuff */
1099   bfd *input_bfd = seclet->u.indirect.section->owner;
1100   asection *input_section = seclet->u.indirect.section;
1101
1102
1103
1104   size_t reloc_size = bfd_get_reloc_upper_bound(input_bfd, input_section);
1105   arelent **reloc_vector = (arelent **) alloca(reloc_size);
1106   
1107   /* read in the section */
1108   bfd_get_section_contents(input_bfd,
1109                            input_section,
1110                            data,
1111                            0,
1112                            input_section->_raw_size);
1113   
1114 /* We're not relaxing the section, so just copy the size info */
1115   input_section->_cooked_size = input_section->_raw_size;
1116   input_section->reloc_done = true;
1117   
1118
1119   if (bfd_canonicalize_reloc(input_bfd, 
1120                              input_section,
1121                              reloc_vector,
1122                              seclet->u.indirect.symbols) )
1123   {
1124     arelent **parent;
1125     for (parent = reloc_vector;  * parent != (arelent *)NULL;
1126          parent++) 
1127     { 
1128       bfd_reloc_status_type r=
1129        bfd_perform_relocation(input_bfd,
1130                               *parent,
1131                               data,
1132                               input_section,
1133                               relocateable ? abfd : (bfd *) NULL);
1134       
1135       if (relocateable)
1136         {
1137           asection *os = input_section->output_section;
1138
1139           /* A partial link, so keep the relocs */
1140           os->orelocation[os->reloc_count] = *parent;
1141           os->reloc_count++;
1142         }
1143
1144       if (r != bfd_reloc_ok) 
1145       {
1146         switch (r)
1147         {
1148         case bfd_reloc_undefined:
1149           bfd_error_vector.undefined_symbol(*parent, seclet);
1150           break;
1151         case bfd_reloc_dangerous: 
1152           bfd_error_vector.reloc_dangerous(*parent, seclet);
1153           break;
1154         case bfd_reloc_outofrange:
1155         case bfd_reloc_overflow:
1156           bfd_error_vector.reloc_value_truncated(*parent, seclet);
1157           break;
1158         default:
1159           abort();
1160           break;
1161         }
1162
1163       }
1164     }    
1165   }
1166
1167
1168   return data;
1169
1170   
1171 }