1 /* BFD support for handling relocation entries.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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.
32 Note that relocations are maintained on a per section basis,
33 whilst symbols are maintained on a per BFD basis.
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:
49 @node typedef arelent, howto manager, Relocations, Relocations
58 bfd_perform_relocation
61 The relocation routine returns as a status an enumerated type:
63 .typedef enum bfd_reloc_status {
68 The relocation was performed, but there was an overflow.
72 The address to relocate was not within the section supplied
74 . bfd_reloc_outofrange,
76 Used by special functions
82 . bfd_reloc_notsupported,
84 Unsupported relocation size requested.
88 The symbol to relocate against was undefined.
90 . bfd_reloc_undefined,
92 The relocation was performed, but may not be ok - presently
93 generated only when linking i960 coff files with i960 b.out symbols.
97 . bfd_reloc_status_type;
100 .typedef struct reloc_cache_entry
103 A pointer into the canonical table of pointers
105 . struct symbol_cache_entry **sym_ptr_ptr;
109 . rawdata_offset address;
111 addend for relocation value
115 if sym is null this is the section
117 . struct sec *section;
119 Pointer to how to perform the required relocation
121 . CONST struct reloc_howto_struct *howto;
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.
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
159 return foo[0x12345678];
163 Could be compiled into:
174 This could create a reloc pointing to foo, but leave the
175 offset in the data (something like)
178 RELOCATION RECORDS FOR [.text]:
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
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:
194 or.u r13,r0,hi16(_foo+0x12345678)
195 ld.b r2,r13,lo16(_foo+0x12345678)
199 This whould create two relocs, both pointing to _foo, and with
200 0x12340000 in their addend field. The data would consist of:
203 RELOCATION RECORDS FOR [.text]:
205 00000002 HVRT16 _foo+0x12340000
206 00000006 LVRT16 _foo+0x12340000
208 00000000 5da05678 ; or.u r13,r0,0x5678
209 00000004 1c4d5678 ; ld.b r2,r13,0x5678
210 00000008 f400c001 ; jmp r1
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.
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.
227 sethi %hi(_foo+0x12345678),%g2
228 ldsb [%g2+%lo(_foo+0x12345678)],%i0
233 Both relocs contains a pointer to foo, and the offsets would
238 RELOCATION RECORDS FOR [.text]:
240 00000004 HI22 _foo+0x12345678
241 00000008 LO10 _foo+0x12345678
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
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.
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.
282 .typedef CONST struct reloc_howto_struct
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.
293 The value the final relocation is shifted right by. This drops
294 unwanted data from the relocation.
296 . unsigned int rightshift;
298 The size of the item to be relocated - 0, is one byte, 1 is 2
299 bytes, 3 is four bytes.
305 . unsigned int bitsize;
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
312 . boolean pc_relative;
316 . unsigned int bitpos;
322 Causes the relocation routine to return an error if overflow
323 is detected when relocating.
325 . boolean complain_on_overflow;
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
332 . bfd_reloc_status_type (*special_function)();
334 The textual name of the relocation type.
338 When performing a partial link, some formats must modify the
339 relocations rather than the data - this flag signals this.
341 . boolean partial_inplace;
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.
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.
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;
373 The HOWTO define is horrible and will go away.
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}
380 And will be replaced with the totally magic way. But for the
381 moment, we are compatible, so do it this way..
384 .#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,false,false,FUNCTION, NAME,false,0,0,IN)
387 Helper routine to turn a symbol into a relocation value.
389 .#define HOWTO_PREPARE(relocation, symbol) \
391 . if (symbol != (asymbol *)NULL) { \
392 . if (symbol->flags & BSF_FORT_COMM) { \
396 . relocation = symbol->value; \
399 . if (symbol->section != (asection *)NULL) { \
400 . relocation += symbol->section->output_section->vma + \
401 . symbol->section->output_offset; \
413 How relocs are tied together
415 .typedef unsigned char bfd_byte;
417 .typedef struct relent_chain {
419 . struct relent_chain *next;
428 bfd_perform_relocation
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.
445 bfd_reloc_status_type
446 bfd_perform_relocation
448 arelent *reloc_entry,
450 asection *input_section,
455 bfd_reloc_status_type
456 DEFUN(bfd_perform_relocation,(abfd,
462 arelent *reloc_entry AND
464 asection *input_section AND
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;
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;
483 symbol = (asymbol*)NULL;
486 if (howto->special_function){
487 bfd_reloc_status_type cont;
488 cont = howto->special_function(abfd,
493 if (cont != bfd_reloc_continue) return cont;
497 Work out which section the relocation is targetted at and the
498 initial relocation command value.
502 if (symbol != (asymbol *)NULL){
503 if (symbol->flags & BSF_FORT_COMM) {
507 relocation = symbol->value;
509 if (symbol->section != (asection *)NULL)
511 reloc_target_input_section = symbol->section;
514 reloc_target_input_section = (asection *)NULL;
517 else if (reloc_entry->section != (asection *)NULL)
520 reloc_target_input_section = reloc_entry->section;
524 reloc_target_input_section = (asection *)NULL;
528 if (reloc_target_input_section != (asection *)NULL) {
530 reloc_target_output_section =
531 reloc_target_input_section->output_section;
533 if (output_bfd && howto->partial_inplace==false) {
537 output_base = reloc_target_output_section->vma;
541 relocation += output_base + reloc_target_input_section->output_offset;
544 relocation += reloc_entry->addend ;
547 if(reloc_entry->address > (bfd_vma)(input_section->size))
549 return bfd_reloc_outofrange;
553 if (howto->pc_relative == true)
556 Anything which started out as pc relative should end up that
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.
568 input_section->output_section->vma + input_section->output_offset;
570 if (howto->pcrel_offset == true) {
571 relocation -= reloc_entry->address;
576 if (output_bfd!= (bfd *)NULL) {
577 if ( howto->partial_inplace == false) {
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.
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;
589 reloc_entry->address +=
590 input_section->output_offset;
595 /* This is a partial relocation, but inplace, so modify the
598 If we've relocated with a symbol with a section, change
599 into a ref to the section belonging to the symbol
602 if (symbol != (asymbol *)NULL && reloc_target_input_section != (asection *)NULL)
604 reloc_entry->section = reloc_target_input_section;
605 reloc_entry->sym_ptr_ptr = (asymbol **)NULL;
611 reloc_entry->addend = 0;
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)
619 relocation >>= howto->rightshift;
621 /* Shift everything up to where it's going to be used */
623 relocation <<= howto->bitpos;
625 /* Wait for the day when all have the mask in them */
628 i instruction to be left alone
629 o offset within instruction
630 r relocation offset to apply
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 -----------------------
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 -----------------------
654 -----------------------
655 R R R R R R R R R R put into bfd_put<size>
659 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
665 char x = bfd_get_8(abfd, (char *)data + addr);
667 bfd_put_8(abfd,x, (unsigned char *) data + addr);
673 short x = bfd_get_16(abfd, (bfd_byte *)data + addr);
675 bfd_put_16(abfd, x, (unsigned char *)data + addr);
680 long x = bfd_get_32(abfd, (bfd_byte *) data + addr);
682 bfd_put_32(abfd,x, (bfd_byte *)data + addr);
690 return bfd_reloc_other;
699 @node howto manager, , typedef arelent, Relocations
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.
715 The insides of a reloc code
717 .typedef enum bfd_reloc_code_real {
719 16 bits wide, simple reloc
723 8 bits wide, but used to form an address like 0xffnn
731 8 bits wide, pc relative
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
741 . } bfd_reloc_code_real_type;
751 bfd_reloc_type_lookup
754 This routine returns a pointer to a howto struct which when
755 invoked, will perform the supplied relocation on data from the
759 CONST struct reloc_howto_struct *
760 bfd_reloc_type_lookup
761 (CONST bfd_arch_info_type *arch, bfd_reloc_code_type code);
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)
770 return arch->reloc_type_lookup(arch, code);
773 static reloc_howto_type bfd_howto_32 =
774 HOWTO(0, 00,2,32,false,0,false,true,0,"VRT32", false,0xffffffff,0xffffffff,true);
779 bfd_default_reloc_type_lookup
782 Provides a default relocation lookuperer for any architectue
785 CONST struct reloc_howto_struct *bfd_default_reloc_type_lookup
786 (CONST struct bfd_arch_info *,
787 bfd_reloc_code_type code);
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)
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) {
804 return &bfd_howto_32;
813 return (struct reloc_howto_struct *)NULL;