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