Initial revision
[deliverable/binutils-gdb.git] / bfd / reloc.c
1 /* BFD support for handling relocation entries.
2 Copyright (C) 1990-1991 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 DESCRIPTION
26 BFD maintains relocations in much the same was as it maintains
27 symbols; they are left alone until required, then read in
28 en-mass and traslated into an internal form. There is a common
29 routine <<bfd_perform_relocation>> which acts upon the
30 canonical form to to the actual fixup.
31
32 Note that relocations are maintained on a per section basis,
33 whilst symbols are maintained on a per BFD basis.
34
35 All a back end has to do to fit the BFD interface is to create
36 as many <<struct reloc_cache_entry>> as there are relocations
37 in a particuar section, and fill in the right bits:
38
39 @menu
40 * typedef arelent::
41 * howto manager::
42 @end menu
43
44 */
45 #include "bfd.h"
46 #include "sysdep.h"
47 #include "libbfd.h"
48 /*doc*
49 @node typedef arelent, howto manager, Relocations, Relocations
50
51 SUBSECTION
52 typedef arelent
53
54 */
55
56 /*
57 FUNCTION
58 bfd_perform_relocation
59
60 DESCRIPTION
61 The relocation routine returns as a status an enumerated type:
62
63 .typedef enum bfd_reloc_status {
64 No errors detected
65
66 . bfd_reloc_ok,
67
68 The relocation was performed, but there was an overflow.
69
70 . bfd_reloc_overflow,
71
72 The address to relocate was not within the section supplied
73
74 . bfd_reloc_outofrange,
75
76 Used by special functions
77
78 . bfd_reloc_continue,
79
80 Unused
81
82 . bfd_reloc_notsupported,
83
84 Unsupported relocation size requested.
85
86 . bfd_reloc_other,
87
88 The symbol to relocate against was undefined.
89
90 . bfd_reloc_undefined,
91
92 The relocation was performed, but may not be ok - presently
93 generated only when linking i960 coff files with i960 b.out symbols.
94
95 . bfd_reloc_dangerous
96 . }
97 . bfd_reloc_status_type;
98
99
100 .typedef struct reloc_cache_entry
101 .{
102
103 A pointer into the canonical table of pointers
104
105 . struct symbol_cache_entry **sym_ptr_ptr;
106
107 offset in section
108
109 . rawdata_offset address;
110
111 addend for relocation value
112
113 . bfd_vma addend;
114
115 if sym is null this is the section
116
117 . struct sec *section;
118
119 Pointer to how to perform the required relocation
120
121 . CONST struct reloc_howto_struct *howto;
122 .} arelent;
123
124
125 */
126
127 /*
128 DESCRIPTION
129
130 o sym_ptr_ptr
131 The symbol table pointer points to a pointer to the symbol
132 associated with the relocation request. This would naturally
133 be the pointer into the table returned by the back end's
134 get_symtab action. @xref{Symbols}. The symbol is referenced
135 through a pointer to a pointer so that tools like the linker
136 can fix up all the symbols of the same name by modifying only
137 one pointer. The relocation routine looks in the symbol and
138 uses the base of the section the symbol is attached to and the
139 value of the symbol as the initial relocation offset. If the
140 symbol pointer is zero, then the section provided is looked up.
141
142 o address
143 The address field gives the offset in bytes from the base of
144 the section data which owns the relocation record to the first
145 byte of relocatable information. The actual data relocated
146 will be relative to this point - for example, a relocation
147 type which modifies the bottom two bytes of a four byte word
148 would not touch the first byte pointed to in a big endian
149 world. @item addend The addend is a value provided by the back
150 end to be added (!) to the relocation offset. Its
151 interpretation is dependent upon the howto. For example, on
152 the 68k the code:
153
154 EXAMPLE
155
156 char foo[];
157 main()
158 {
159 return foo[0x12345678];
160 }
161
162 DESCRIPTION
163 Could be compiled into:
164
165 EXAMPLE
166 linkw fp,#-4
167 moveb @@#12345678,d0
168 extbl d0
169 unlk fp
170 rts
171
172 DESCRIPTION
173
174 This could create a reloc pointing to foo, but leave the
175 offset in the data (something like)
176
177 EXAMPLE
178 RELOCATION RECORDS FOR [.text]:
179 offset type value
180 00000006 32 _foo
181
182 00000000 4e56 fffc ; linkw fp,#-4
183 00000004 1039 1234 5678 ; moveb @@#12345678,d0
184 0000000a 49c0 ; extbl d0
185 0000000c 4e5e ; unlk fp
186 0000000e 4e75 ; rts
187 DESCRIPTION
188
189 Using coff and an 88k, some instructions don't have enough
190 space in them to represent the full address range, and
191 pointers have to be loaded in two parts. So you'd get something like:
192
193 EXAMPLE
194 or.u r13,r0,hi16(_foo+0x12345678)
195 ld.b r2,r13,lo16(_foo+0x12345678)
196 jmp r1
197
198 DESCRIPTION
199 This whould create two relocs, both pointing to _foo, and with
200 0x12340000 in their addend field. The data would consist of:
201
202 EXAMPLE
203 RELOCATION RECORDS FOR [.text]:
204 offset type value
205 00000002 HVRT16 _foo+0x12340000
206 00000006 LVRT16 _foo+0x12340000
207
208 00000000 5da05678 ; or.u r13,r0,0x5678
209 00000004 1c4d5678 ; ld.b r2,r13,0x5678
210 00000008 f400c001 ; jmp r1
211
212 DESCRIPTION
213 The relocation routine digs out the value from the data, adds
214 it to the addend to get the original offset and then adds the
215 value of _foo. Note that all 32 bits have to be kept around
216 somewhere, to cope with carry from bit 15 to bit 16.
217
218 On further example is the sparc and the a.out format. The
219 sparc has a similar problem to the 88k, in that some
220 instructions don't have room for an entire offset, but on the
221 sparc the parts are created odd sized lumps. The designers of
222 the a.out format chose not to use the data within the section
223 for storing part of the offset; all the offset is kept within
224 the reloc. Any thing in the data should be ignored.
225 EXAMPLE
226 save %sp,-112,%sp
227 sethi %hi(_foo+0x12345678),%g2
228 ldsb [%g2+%lo(_foo+0x12345678)],%i0
229 ret
230 restore
231
232 DESCRIPTION
233 Both relocs contains a pointer to foo, and the offsets would
234 contain junk.
235
236 EXAMPLE
237
238 RELOCATION RECORDS FOR [.text]:
239 offset type value
240 00000004 HI22 _foo+0x12345678
241 00000008 LO10 _foo+0x12345678
242
243 00000000 9de3bf90 ; save %sp,-112,%sp
244 00000004 05000000 ; sethi %hi(_foo+0),%g2
245 00000008 f048a000 ; ldsb [%g2+%lo(_foo+0)],%i0
246 0000000c 81c7e008 ; ret
247 00000010 81e80000 ; restore
248
249 DESCRIPTION
250
251 o section
252 The section field is only used when the symbol pointer field
253 is null. It supplies the section into which the data should be
254 relocated. The field's main use comes from assemblers which do
255 most of the symbol fixups themselves; an assembler may take an
256 internal reference to a label, but since it knows where the
257 label is, it can turn the relocation request from a symbol
258 lookup into a section relative relocation - the relocation
259 emitted has no symbol, just a section to relocate against. I'm
260 not sure what it means when both a symbol pointer an a section
261 pointer are present. Some formats use this sort of mechanism
262 to describe PIC relocations, but BFD can't to that sort of
263 thing yet. @item howto The howto field can be imagined as a
264 relocation instruction. It is a pointer to a struct which
265 contains information on what to do with all the other
266 information in the reloc record and data section. A back end
267 would normally have a relocation instruction set and turn
268 relocations into pointers to the correct structure on input -
269 but it would be possible to create each howto field on demand.
270
271 */
272
273
274 /*
275 SUBSUBSECTION
276 <<reloc_howto_type>>
277
278 DESCRIPTION
279 The <<reloc_howto_type>> is a structure which contains all the
280 information that BFD needs to know to tie up a back end's data.
281
282 .typedef CONST struct reloc_howto_struct
283 .{
284 The type field has mainly a documetary use - the back end can
285 to what it wants with it, though the normally the back end's
286 external idea of what a reloc number would be would be stored
287 in this field. For example, the a PC relative word relocation
288 in a coff environment would have the type 023 - because that's
289 what the outside world calls a R_PCRWORD reloc.
290
291 . unsigned int type;
292
293 The value the final relocation is shifted right by. This drops
294 unwanted data from the relocation.
295
296 . unsigned int rightshift;
297
298 The size of the item to be relocated - 0, is one byte, 1 is 2
299 bytes, 3 is four bytes.
300
301 . unsigned int size;
302
303 Now obsolete
304
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
312 . boolean pc_relative;
313
314 Now obsolete
315
316 . unsigned int bitpos;
317
318 Now obsolete
319
320 . boolean absolute;
321
322 Causes the relocation routine to return an error if overflow
323 is detected when relocating.
324
325 . boolean complain_on_overflow;
326
327 If this field is non null, then the supplied function is
328 called rather than the normal function. This allows really
329 strange relocation methods to be accomodated (eg, i960 callj
330 instructions).
331
332 . bfd_reloc_status_type (*special_function)();
333
334 The textual name of the relocation type.
335
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
341 . boolean partial_inplace;
342
343 The src_mask is used to select what parts of the read in data
344 are to be used in the relocation sum. Eg, if this was an 8 bit
345 bit of data which we read and relocated, this would be
346 0x000000ff. When we have relocs which have an addend, such as
347 sun4 extended relocs, the value in the offset part of a
348 relocating field is garbage so we never use it. In this case
349 the mask would be 0x00000000.
350 . bfd_word src_mask;
351
352 The dst_mask is what parts of the instruction are replaced
353 into the instruction. In most cases src_mask == dst_mask,
354 except in the above special case, where dst_mask would be
355 0x000000ff, and src_mask would be 0x00000000.
356 . bfd_word dst_mask;
357
358 When some formats create PC relative instructions, they leave
359 the value of the pc of the place being relocated in the offset
360 slot of the instruction, so that a PC relative relocation can
361 be made just by adding in an ordinary offset (eg sun3 a.out).
362 Some formats leave the displacement part of an instruction
363 empty (eg m88k bcs), this flag signals the fact.
364 . boolean pcrel_offset;
365 .} reloc_howto_type;
366
367 */
368
369 /*
370 FUNCTION
371 HOWTO
372 DESCRIPTION
373 The HOWTO define is horrible and will go away.
374
375
376 .#define HOWTO(C, R,S,B, P, BI, ABS, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
377 . {(unsigned)C,R,S,B, P, BI, ABS,O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
378
379 DESCRIPTION
380 And will be replaced with the totally magic way. But for the
381 moment, we are compatible, so do it this way..
382
383
384 .#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,false,false,FUNCTION, NAME,false,0,0,IN)
385 .
386 DESCRIPTION
387 Helper routine to turn a symbol into a relocation value.
388
389 .#define HOWTO_PREPARE(relocation, symbol) \
390 . { \
391 . if (symbol != (asymbol *)NULL) { \
392 . if (symbol->flags & BSF_FORT_COMM) { \
393 . relocation = 0; \
394 . } \
395 . else { \
396 . relocation = symbol->value; \
397 . } \
398 . } \
399 . if (symbol->section != (asection *)NULL) { \
400 . relocation += symbol->section->output_section->vma + \
401 . symbol->section->output_offset; \
402 . } \
403 .}
404
405 */
406
407 /*
408 TYPEDEF
409 reloc_chain
410
411 DESCRIPTION
412
413 How relocs are tied together
414
415 .typedef unsigned char bfd_byte;
416 .
417 .typedef struct relent_chain {
418 . arelent relent;
419 . struct relent_chain *next;
420 .} arelent_chain;
421
422 */
423
424
425
426 /*
427 FUNCTION
428 bfd_perform_relocation
429
430 DESCRIPTION
431 If an output_bfd is supplied to this function the generated
432 image will be relocatable, the relocations are copied to the
433 output file after they have been changed to reflect the new
434 state of the world. There are two ways of reflecting the
435 results of partial linkage in an output file; by modifying the
436 output data in place, and by modifying the relocation record.
437 Some native formats (eg basic a.out and basic coff) have no
438 way of specifying an addend in the relocation type, so the
439 addend has to go in the output data. This is no big deal
440 since in these formats the output data slot will always be big
441 enough for the addend. Complex reloc types with addends were
442 invented to solve just this problem.
443
444 SYNOPSIS
445 bfd_reloc_status_type
446 bfd_perform_relocation
447 (bfd * abfd,
448 arelent *reloc_entry,
449 PTR data,
450 asection *input_section,
451 bfd *output_bfd);
452 */
453
454
455 bfd_reloc_status_type
456 DEFUN(bfd_perform_relocation,(abfd,
457 reloc_entry,
458 data,
459 input_section,
460 output_bfd),
461 bfd *abfd AND
462 arelent *reloc_entry AND
463 PTR data AND
464 asection *input_section AND
465 bfd *output_bfd)
466 {
467 bfd_vma relocation;
468 bfd_reloc_status_type flag = bfd_reloc_ok;
469 bfd_vma addr = reloc_entry->address ;
470 bfd_vma output_base = 0;
471 reloc_howto_type *howto = reloc_entry->howto;
472 asection *reloc_target_output_section;
473 asection *reloc_target_input_section;
474 asymbol *symbol;
475
476 if (reloc_entry->sym_ptr_ptr) {
477 symbol = *( reloc_entry->sym_ptr_ptr);
478 if ((symbol->flags & BSF_UNDEFINED) && output_bfd == (bfd *)NULL) {
479 flag = bfd_reloc_undefined;
480 }
481 }
482 else {
483 symbol = (asymbol*)NULL;
484 }
485
486 if (howto->special_function){
487 bfd_reloc_status_type cont;
488 cont = howto->special_function(abfd,
489 reloc_entry,
490 symbol,
491 data,
492 input_section);
493 if (cont != bfd_reloc_continue) return cont;
494 }
495
496 /*
497 Work out which section the relocation is targetted at and the
498 initial relocation command value.
499 */
500
501
502 if (symbol != (asymbol *)NULL){
503 if (symbol->flags & BSF_FORT_COMM) {
504 relocation = 0;
505 }
506 else {
507 relocation = symbol->value;
508 }
509 if (symbol->section != (asection *)NULL)
510 {
511 reloc_target_input_section = symbol->section;
512 }
513 else {
514 reloc_target_input_section = (asection *)NULL;
515 }
516 }
517 else if (reloc_entry->section != (asection *)NULL)
518 {
519 relocation = 0;
520 reloc_target_input_section = reloc_entry->section;
521 }
522 else {
523 relocation = 0;
524 reloc_target_input_section = (asection *)NULL;
525 }
526
527
528 if (reloc_target_input_section != (asection *)NULL) {
529
530 reloc_target_output_section =
531 reloc_target_input_section->output_section;
532
533 if (output_bfd && howto->partial_inplace==false) {
534 output_base = 0;
535 }
536 else {
537 output_base = reloc_target_output_section->vma;
538
539 }
540
541 relocation += output_base + reloc_target_input_section->output_offset;
542 }
543
544 relocation += reloc_entry->addend ;
545
546
547 if(reloc_entry->address > (bfd_vma)(input_section->size))
548 {
549 return bfd_reloc_outofrange;
550 }
551
552
553 if (howto->pc_relative == true)
554 {
555 /*
556 Anything which started out as pc relative should end up that
557 way too.
558
559 There are two ways we can see a pcrel instruction. Sometimes
560 the pcrel displacement has been partially calculated, it
561 includes the distance from the start of the section to the
562 instruction in it (eg sun3), and sometimes the field is
563 totally blank - eg m88kbcs.
564 */
565
566
567 relocation -=
568 input_section->output_section->vma + input_section->output_offset;
569
570 if (howto->pcrel_offset == true) {
571 relocation -= reloc_entry->address;
572 }
573
574 }
575
576 if (output_bfd!= (bfd *)NULL) {
577 if ( howto->partial_inplace == false) {
578 /*
579 This is a partial relocation, and we want to apply the relocation
580 to the reloc entry rather than the raw data. Modify the reloc
581 inplace to reflect what we now know.
582 */
583 reloc_entry->addend = relocation ;
584 reloc_entry->section = reloc_target_input_section;
585 if (reloc_target_input_section != (asection *)NULL) {
586 /* If we know the output section we can forget the symbol */
587 reloc_entry->sym_ptr_ptr = (asymbol**)NULL;
588 }
589 reloc_entry->address +=
590 input_section->output_offset;
591 return flag;
592 }
593 else
594 {
595 /* This is a partial relocation, but inplace, so modify the
596 reloc record a bit.
597
598 If we've relocated with a symbol with a section, change
599 into a ref to the section belonging to the symbol
600 */
601
602 if (symbol != (asymbol *)NULL && reloc_target_input_section != (asection *)NULL)
603 {
604 reloc_entry->section = reloc_target_input_section;
605 reloc_entry->sym_ptr_ptr = (asymbol **)NULL;
606 }
607
608 }
609 }
610
611 reloc_entry->addend = 0;
612
613
614 /*
615 Either we are relocating all the way, or we don't want to apply
616 the relocation to the reloc entry (probably because there isn't
617 any room in the output format to describe addends to relocs)
618 */
619 relocation >>= howto->rightshift;
620
621 /* Shift everything up to where it's going to be used */
622
623 relocation <<= howto->bitpos;
624
625 /* Wait for the day when all have the mask in them */
626
627 /* What we do:
628 i instruction to be left alone
629 o offset within instruction
630 r relocation offset to apply
631 S src mask
632 D dst mask
633 N ~dst mask
634 A part 1
635 B part 2
636 R result
637
638 Do this:
639 i i i i i o o o o o from bfd_get<size>
640 and S S S S S to get the size offset we want
641 + r r r r r r r r r r to get the final value to place
642 and D D D D D to chop to right size
643 -----------------------
644 A A A A A
645 And this:
646 ... i i i i i o o o o o from bfd_get<size>
647 and N N N N N get instruction
648 -----------------------
649 ... B B B B B
650
651 And then:
652 B B B B B
653 or A A A A A
654 -----------------------
655 R R R R R R R R R R put into bfd_put<size>
656 */
657
658 #define DOIT(x) \
659 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
660
661 switch (howto->size)
662 {
663 case 0:
664 {
665 char x = bfd_get_8(abfd, (char *)data + addr);
666 DOIT(x);
667 bfd_put_8(abfd,x, (unsigned char *) data + addr);
668 }
669 break;
670
671 case 1:
672 {
673 short x = bfd_get_16(abfd, (bfd_byte *)data + addr);
674 DOIT(x);
675 bfd_put_16(abfd, x, (unsigned char *)data + addr);
676 }
677 break;
678 case 2:
679 {
680 long x = bfd_get_32(abfd, (bfd_byte *) data + addr);
681 DOIT(x);
682 bfd_put_32(abfd,x, (bfd_byte *)data + addr);
683 }
684 break;
685 case 3:
686
687 /* Do nothing */
688 break;
689 default:
690 return bfd_reloc_other;
691 }
692
693 return flag;
694 }
695
696
697
698 /*
699 @node howto manager, , typedef arelent, Relocations
700 SECTION
701 The howto manager
702
703 DESCRIPTION
704 When an application wants to create a relocation, but doesn't
705 know what the target machine might call it, it can find out by
706 using this bit of code.
707
708 */
709
710 /*
711 TYPEDEF
712 bfd_reloc_code_type
713
714 DESCRIPTION
715 The insides of a reloc code
716
717 .typedef enum bfd_reloc_code_real {
718
719 16 bits wide, simple reloc
720
721 . BFD_RELOC_16,
722
723 8 bits wide, but used to form an address like 0xffnn
724
725 . BFD_RELOC_8_FFnn,
726
727 8 bits wide, simple
728
729 . BFD_RELOC_8,
730
731 8 bits wide, pc relative
732
733 . BFD_RELOC_8_PCREL,
734
735 The type of reloc used to build a contructor table - at the
736 moment probably a 32 bit wide abs address, but the cpu can
737 choose.
738
739 . BFD_RELOC_CTOR
740
741 . } bfd_reloc_code_real_type;
742
743
744
745 */
746
747
748
749 /*
750 SECTION
751 bfd_reloc_type_lookup
752
753 DESCRIPTION
754 This routine returns a pointer to a howto struct which when
755 invoked, will perform the supplied relocation on data from the
756 architecture noted.
757
758 SYNOPSIS
759 CONST struct reloc_howto_struct *
760 bfd_reloc_type_lookup
761 (CONST bfd_arch_info_type *arch, bfd_reloc_code_type code);
762 */
763
764
765 CONST struct reloc_howto_struct *
766 DEFUN(bfd_reloc_type_lookup,(arch, code),
767 CONST bfd_arch_info_type *arch AND
768 bfd_reloc_code_type code)
769 {
770 return arch->reloc_type_lookup(arch, code);
771 }
772
773 static reloc_howto_type bfd_howto_32 =
774 HOWTO(0, 00,2,32,false,0,false,true,0,"VRT32", false,0xffffffff,0xffffffff,true);
775
776
777 /*
778 INTERNAL FUNCTION
779 bfd_default_reloc_type_lookup
780
781 DESCRIPTION
782 Provides a default relocation lookuperer for any architectue
783
784 SYNOPSIS
785 CONST struct reloc_howto_struct *bfd_default_reloc_type_lookup
786 (CONST struct bfd_arch_info *,
787 bfd_reloc_code_type code);
788
789 */
790 CONST struct reloc_howto_struct *
791 DEFUN(bfd_default_reloc_type_lookup,(arch, code),
792 CONST struct bfd_arch_info *arch AND
793 bfd_reloc_code_type code)
794 {
795 switch (code)
796 {
797 case BFD_RELOC_CTOR:
798 /* The type of reloc used in a ctor, which will be as wide as the
799 address - so either a 64, 32, or 16 bitter.. */
800 switch (arch->bits_per_address) {
801 case 64:
802 BFD_FAIL();
803 case 32:
804 return &bfd_howto_32;
805 case 16:
806 BFD_FAIL();
807 default:
808 BFD_FAIL();
809 }
810 default:
811 BFD_FAIL();
812 }
813 return (struct reloc_howto_struct *)NULL;
814 }
This page took 0.046875 seconds and 4 git commands to generate.