1 /* BFD support for handling relocation entries.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 1997
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 BFD maintains relocations in much the same way it maintains
27 symbols: they are left alone until required, then read in
28 en-mass and translated into an internal form. A common
29 routine <<bfd_perform_relocation>> acts upon the
30 canonical form to do the fixup.
32 Relocations are maintained on a per section basis,
33 while symbols are maintained on a per BFD basis.
35 All that a back end has to do to fit the BFD interface is to create
36 a <<struct reloc_cache_entry>> for each relocation
37 in a particular section, and fill in the right bits of the structures.
46 /* DO compile in the reloc_code name table from libbfd.h. */
47 #define _BFD_MAKE_TABLE_bfd_reloc_code_real
56 typedef arelent, howto manager, Relocations, Relocations
61 This is the structure of a relocation entry:
65 .typedef enum bfd_reloc_status
67 . {* No errors detected *}
70 . {* The relocation was performed, but there was an overflow. *}
73 . {* The address to relocate was not within the section supplied. *}
74 . bfd_reloc_outofrange,
76 . {* Used by special functions *}
79 . {* Unsupported relocation size requested. *}
80 . bfd_reloc_notsupported,
85 . {* The symbol to relocate against was undefined. *}
86 . bfd_reloc_undefined,
88 . {* The relocation was performed, but may not be ok - presently
89 . generated only when linking i960 coff files with i960 b.out
90 . symbols. If this type is returned, the error_message argument
91 . to bfd_perform_relocation will be set. *}
94 . bfd_reloc_status_type;
97 .typedef struct reloc_cache_entry
99 . {* A pointer into the canonical table of pointers *}
100 . struct symbol_cache_entry **sym_ptr_ptr;
102 . {* offset in section *}
103 . bfd_size_type address;
105 . {* addend for relocation value *}
108 . {* Pointer to how to perform the required relocation *}
109 . reloc_howto_type *howto;
118 Here is a description of each of the fields within an <<arelent>>:
122 The symbol table pointer points to a pointer to the symbol
123 associated with the relocation request. It is
124 the pointer into the table returned by the back end's
125 <<get_symtab>> action. @xref{Symbols}. The symbol is referenced
126 through a pointer to a pointer so that tools like the linker
127 can fix up all the symbols of the same name by modifying only
128 one pointer. The relocation routine looks in the symbol and
129 uses the base of the section the symbol is attached to and the
130 value of the symbol as the initial relocation offset. If the
131 symbol pointer is zero, then the section provided is looked up.
135 The <<address>> field gives the offset in bytes from the base of
136 the section data which owns the relocation record to the first
137 byte of relocatable information. The actual data relocated
138 will be relative to this point; for example, a relocation
139 type which modifies the bottom two bytes of a four byte word
140 would not touch the first byte pointed to in a big endian
145 The <<addend>> is a value provided by the back end to be added (!)
146 to the relocation offset. Its interpretation is dependent upon
147 the howto. For example, on the 68k the code:
153 | return foo[0x12345678];
156 Could be compiled into:
159 | moveb @@#12345678,d0
165 This could create a reloc pointing to <<foo>>, but leave the
166 offset in the data, something like:
169 |RELOCATION RECORDS FOR [.text]:
173 |00000000 4e56 fffc ; linkw fp,#-4
174 |00000004 1039 1234 5678 ; moveb @@#12345678,d0
175 |0000000a 49c0 ; extbl d0
176 |0000000c 4e5e ; unlk fp
180 Using coff and an 88k, some instructions don't have enough
181 space in them to represent the full address range, and
182 pointers have to be loaded in two parts. So you'd get something like:
185 | or.u r13,r0,hi16(_foo+0x12345678)
186 | ld.b r2,r13,lo16(_foo+0x12345678)
190 This should create two relocs, both pointing to <<_foo>>, and with
191 0x12340000 in their addend field. The data would consist of:
194 |RELOCATION RECORDS FOR [.text]:
196 |00000002 HVRT16 _foo+0x12340000
197 |00000006 LVRT16 _foo+0x12340000
199 |00000000 5da05678 ; or.u r13,r0,0x5678
200 |00000004 1c4d5678 ; ld.b r2,r13,0x5678
201 |00000008 f400c001 ; jmp r1
204 The relocation routine digs out the value from the data, adds
205 it to the addend to get the original offset, and then adds the
206 value of <<_foo>>. Note that all 32 bits have to be kept around
207 somewhere, to cope with carry from bit 15 to bit 16.
209 One further example is the sparc and the a.out format. The
210 sparc has a similar problem to the 88k, in that some
211 instructions don't have room for an entire offset, but on the
212 sparc the parts are created in odd sized lumps. The designers of
213 the a.out format chose to not use the data within the section
214 for storing part of the offset; all the offset is kept within
215 the reloc. Anything in the data should be ignored.
218 | sethi %hi(_foo+0x12345678),%g2
219 | ldsb [%g2+%lo(_foo+0x12345678)],%i0
223 Both relocs contain a pointer to <<foo>>, and the offsets
227 |RELOCATION RECORDS FOR [.text]:
229 |00000004 HI22 _foo+0x12345678
230 |00000008 LO10 _foo+0x12345678
232 |00000000 9de3bf90 ; save %sp,-112,%sp
233 |00000004 05000000 ; sethi %hi(_foo+0),%g2
234 |00000008 f048a000 ; ldsb [%g2+%lo(_foo+0)],%i0
235 |0000000c 81c7e008 ; ret
236 |00000010 81e80000 ; restore
241 The <<howto>> field can be imagined as a
242 relocation instruction. It is a pointer to a structure which
243 contains information on what to do with all of the other
244 information in the reloc record and data section. A back end
245 would normally have a relocation instruction set and turn
246 relocations into pointers to the correct structure on input -
247 but it would be possible to create each howto field on demand.
253 <<enum complain_overflow>>
255 Indicates what sort of overflow checking should be done when
256 performing a relocation.
260 .enum complain_overflow
262 . {* Do not complain on overflow. *}
263 . complain_overflow_dont,
265 . {* Complain if the bitfield overflows, whether it is considered
266 . as signed or unsigned. *}
267 . complain_overflow_bitfield,
269 . {* Complain if the value overflows when considered as signed
271 . complain_overflow_signed,
273 . {* Complain if the value overflows when considered as an
274 . unsigned number. *}
275 . complain_overflow_unsigned
284 The <<reloc_howto_type>> is a structure which contains all the
285 information that libbfd needs to know to tie up a back end's data.
288 .struct symbol_cache_entry; {* Forward declaration *}
290 .struct reloc_howto_struct
292 . {* The type field has mainly a documentary use - the back end can
293 . do what it wants with it, though normally the back end's
294 . external idea of what a reloc number is stored
295 . in this field. For example, a PC relative word relocation
296 . in a coff environment has the type 023 - because that's
297 . what the outside world calls a R_PCRWORD reloc. *}
300 . {* The value the final relocation is shifted right by. This drops
301 . unwanted data from the relocation. *}
302 . unsigned int rightshift;
304 . {* The size of the item to be relocated. This is *not* a
305 . power-of-two measure. To get the number of bytes operated
306 . on by a type of relocation, use bfd_get_reloc_size. *}
309 . {* The number of bits in the item to be relocated. This is used
310 . when doing overflow checking. *}
311 . unsigned int bitsize;
313 . {* Notes that the relocation is relative to the location in the
314 . data section of the addend. The relocation function will
315 . subtract from the relocation value the address of the location
316 . being relocated. *}
317 . boolean pc_relative;
319 . {* The bit position of the reloc value in the destination.
320 . The relocated value is left shifted by this amount. *}
321 . unsigned int bitpos;
323 . {* What type of overflow error should be checked for when
325 . enum complain_overflow 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 (e.g., i960 callj
331 . bfd_reloc_status_type (*special_function)
332 . PARAMS ((bfd *abfd,
333 . arelent *reloc_entry,
334 . struct symbol_cache_entry *symbol,
336 . asection *input_section,
338 . char **error_message));
340 . {* The textual name of the relocation type. *}
343 . {* When performing a partial link, some formats must modify the
344 . relocations rather than the data - this flag signals this.*}
345 . boolean partial_inplace;
347 . {* The src_mask selects which parts of the read in data
348 . are to be used in the relocation sum. E.g., if this was an 8 bit
349 . bit of data which we read and relocated, this would be
350 . 0x000000ff. When we have relocs which have an addend, such as
351 . sun4 extended relocs, the value in the offset part of a
352 . relocating field is garbage so we never use it. In this case
353 . the mask would be 0x00000000. *}
356 . {* The dst_mask selects which parts of the instruction are replaced
357 . into the instruction. In most cases src_mask == dst_mask,
358 . except in the above special case, where dst_mask would be
359 . 0x000000ff, and src_mask would be 0x00000000. *}
362 . {* When some formats create PC relative instructions, they leave
363 . the value of the pc of the place being relocated in the offset
364 . slot of the instruction, so that a PC relative relocation can
365 . be made just by adding in an ordinary offset (e.g., sun3 a.out).
366 . Some formats leave the displacement part of an instruction
367 . empty (e.g., m88k bcs); this flag signals the fact.*}
368 . boolean pcrel_offset;
379 The HOWTO define is horrible and will go away.
382 .#define HOWTO(C, R,S,B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
383 . {(unsigned)C,R,S,B, P, BI, O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
386 And will be replaced with the totally magic way. But for the
387 moment, we are compatible, so do it this way.
390 .#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,complain_overflow_dont,FUNCTION, NAME,false,0,0,IN)
393 Helper routine to turn a symbol into a relocation value.
395 .#define HOWTO_PREPARE(relocation, symbol) \
397 . if (symbol != (asymbol *)NULL) { \
398 . if (bfd_is_com_section (symbol->section)) { \
402 . relocation = symbol->value; \
414 int bfd_get_reloc_size (reloc_howto_type *);
417 For a reloc_howto_type that operates on a fixed number of bytes,
418 this returns the number of bytes operated on.
422 bfd_get_reloc_size (howto)
423 reloc_howto_type *howto;
444 How relocs are tied together in an <<asection>>:
446 .typedef struct relent_chain {
448 . struct relent_chain *next;
459 bfd_reloc_status_type
461 (enum complain_overflow how,
462 unsigned int bitsize,
463 unsigned int rightshift,
467 Perform overflow checking on @var{relocation} which has @var{bitsize}
468 significant bits and will be shifted right by @var{rightshift} bits.
469 The result is either of @code{bfd_reloc_ok} or
470 @code{bfd_reloc_overflow}.
474 bfd_reloc_status_type
475 bfd_check_overflow (how, bitsize, rightshift, relocation)
476 enum complain_overflow how;
477 unsigned int bitsize, rightshift;
481 bfd_reloc_status_type flag = bfd_reloc_ok;
483 /* Get the value that will be used for the relocation, but
484 starting at bit position zero. */
485 check = relocation >> rightshift;
489 case complain_overflow_dont:
492 case complain_overflow_signed:
494 /* Assumes two's complement. */
495 bfd_signed_vma reloc_signed_max = (1 << (bitsize - 1)) - 1;
496 bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
498 /* The above right shift is incorrect for a signed value.
499 Fix it up by forcing on the upper bits. */
501 && (bfd_signed_vma) relocation < 0)
502 check |= ((bfd_vma) - 1
505 if ((bfd_signed_vma) check > reloc_signed_max
506 || (bfd_signed_vma) check < reloc_signed_min)
507 flag = bfd_reloc_overflow;
511 case complain_overflow_unsigned:
513 /* Assumes two's complement. This expression avoids
514 overflow if `bitsize' is the number of bits in
516 bfd_vma reloc_unsigned_max = (((1 << (bitsize - 1)) - 1) << 1) | 1;
518 if ((bfd_vma) check > reloc_unsigned_max)
519 flag = bfd_reloc_overflow;
523 case complain_overflow_bitfield:
525 /* Assumes two's complement. This expression avoids
526 overflow if `bitsize' is the number of bits in
528 bfd_vma reloc_bits = (((1 << (bitsize - 1)) - 1) << 1) | 1;
530 if (((bfd_vma) check & ~reloc_bits) != 0
531 && ((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
533 /* The above right shift is incorrect for a signed
534 value. See if turning on the upper bits fixes the
537 && (bfd_signed_vma) relocation < 0)
539 check |= ((bfd_vma) - 1
542 if (((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
543 flag = bfd_reloc_overflow;
546 flag = bfd_reloc_overflow;
561 bfd_perform_relocation
564 bfd_reloc_status_type
565 bfd_perform_relocation
567 arelent *reloc_entry,
569 asection *input_section,
571 char **error_message);
574 If @var{output_bfd} is supplied to this function, the
575 generated image will be relocatable; the relocations are
576 copied to the output file after they have been changed to
577 reflect the new state of the world. There are two ways of
578 reflecting the results of partial linkage in an output file:
579 by modifying the output data in place, and by modifying the
580 relocation record. Some native formats (e.g., basic a.out and
581 basic coff) have no way of specifying an addend in the
582 relocation type, so the addend has to go in the output data.
583 This is no big deal since in these formats the output data
584 slot will always be big enough for the addend. Complex reloc
585 types with addends were invented to solve just this problem.
586 The @var{error_message} argument is set to an error message if
587 this return @code{bfd_reloc_dangerous}.
592 bfd_reloc_status_type
593 bfd_perform_relocation (abfd, reloc_entry, data, input_section, output_bfd,
596 arelent *reloc_entry;
598 asection *input_section;
600 char **error_message;
603 bfd_reloc_status_type flag = bfd_reloc_ok;
604 bfd_size_type addr = reloc_entry->address;
605 bfd_vma output_base = 0;
606 reloc_howto_type *howto = reloc_entry->howto;
607 asection *reloc_target_output_section;
610 symbol = *(reloc_entry->sym_ptr_ptr);
611 if (bfd_is_abs_section (symbol->section)
612 && output_bfd != (bfd *) NULL)
614 reloc_entry->address += input_section->output_offset;
618 /* If we are not producing relocateable output, return an error if
619 the symbol is not defined. An undefined weak symbol is
620 considered to have a value of zero (SVR4 ABI, p. 4-27). */
621 if (bfd_is_und_section (symbol->section)
622 && (symbol->flags & BSF_WEAK) == 0
623 && output_bfd == (bfd *) NULL)
624 flag = bfd_reloc_undefined;
626 /* If there is a function supplied to handle this relocation type,
627 call it. It'll return `bfd_reloc_continue' if further processing
629 if (howto->special_function)
631 bfd_reloc_status_type cont;
632 cont = howto->special_function (abfd, reloc_entry, symbol, data,
633 input_section, output_bfd,
635 if (cont != bfd_reloc_continue)
639 /* Is the address of the relocation really within the section? */
640 if (reloc_entry->address > input_section->_cooked_size)
641 return bfd_reloc_outofrange;
643 /* Work out which section the relocation is targetted at and the
644 initial relocation command value. */
646 /* Get symbol value. (Common symbols are special.) */
647 if (bfd_is_com_section (symbol->section))
650 relocation = symbol->value;
653 reloc_target_output_section = symbol->section->output_section;
655 /* Convert input-section-relative symbol value to absolute. */
656 if (output_bfd && howto->partial_inplace == false)
659 output_base = reloc_target_output_section->vma;
661 relocation += output_base + symbol->section->output_offset;
663 /* Add in supplied addend. */
664 relocation += reloc_entry->addend;
666 /* Here the variable relocation holds the final address of the
667 symbol we are relocating against, plus any addend. */
669 if (howto->pc_relative == true)
671 /* This is a PC relative relocation. We want to set RELOCATION
672 to the distance between the address of the symbol and the
673 location. RELOCATION is already the address of the symbol.
675 We start by subtracting the address of the section containing
678 If pcrel_offset is set, we must further subtract the position
679 of the location within the section. Some targets arrange for
680 the addend to be the negative of the position of the location
681 within the section; for example, i386-aout does this. For
682 i386-aout, pcrel_offset is false. Some other targets do not
683 include the position of the location; for example, m88kbcs,
684 or ELF. For those targets, pcrel_offset is true.
686 If we are producing relocateable output, then we must ensure
687 that this reloc will be correctly computed when the final
688 relocation is done. If pcrel_offset is false we want to wind
689 up with the negative of the location within the section,
690 which means we must adjust the existing addend by the change
691 in the location within the section. If pcrel_offset is true
692 we do not want to adjust the existing addend at all.
694 FIXME: This seems logical to me, but for the case of
695 producing relocateable output it is not what the code
696 actually does. I don't want to change it, because it seems
697 far too likely that something will break. */
700 input_section->output_section->vma + input_section->output_offset;
702 if (howto->pcrel_offset == true)
703 relocation -= reloc_entry->address;
706 if (output_bfd != (bfd *) NULL)
708 if (howto->partial_inplace == false)
710 /* This is a partial relocation, and we want to apply the relocation
711 to the reloc entry rather than the raw data. Modify the reloc
712 inplace to reflect what we now know. */
713 reloc_entry->addend = relocation;
714 reloc_entry->address += input_section->output_offset;
719 /* This is a partial relocation, but inplace, so modify the
722 If we've relocated with a symbol with a section, change
723 into a ref to the section belonging to the symbol. */
725 reloc_entry->address += input_section->output_offset;
728 if (abfd->xvec->flavour == bfd_target_coff_flavour
729 && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
730 && strcmp (abfd->xvec->name, "xcoff-powermac") != 0
731 && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
732 && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
735 /* For m68k-coff, the addend was being subtracted twice during
736 relocation with -r. Removing the line below this comment
737 fixes that problem; see PR 2953.
739 However, Ian wrote the following, regarding removing the line below,
740 which explains why it is still enabled: --djm
742 If you put a patch like that into BFD you need to check all the COFF
743 linkers. I am fairly certain that patch will break coff-i386 (e.g.,
744 SCO); see coff_i386_reloc in coff-i386.c where I worked around the
745 problem in a different way. There may very well be a reason that the
746 code works as it does.
748 Hmmm. The first obvious point is that bfd_perform_relocation should
749 not have any tests that depend upon the flavour. It's seem like
750 entirely the wrong place for such a thing. The second obvious point
751 is that the current code ignores the reloc addend when producing
752 relocateable output for COFF. That's peculiar. In fact, I really
753 have no idea what the point of the line you want to remove is.
755 A typical COFF reloc subtracts the old value of the symbol and adds in
756 the new value to the location in the object file (if it's a pc
757 relative reloc it adds the difference between the symbol value and the
758 location). When relocating we need to preserve that property.
760 BFD handles this by setting the addend to the negative of the old
761 value of the symbol. Unfortunately it handles common symbols in a
762 non-standard way (it doesn't subtract the old value) but that's a
763 different story (we can't change it without losing backward
764 compatibility with old object files) (coff-i386 does subtract the old
765 value, to be compatible with existing coff-i386 targets, like SCO).
767 So everything works fine when not producing relocateable output. When
768 we are producing relocateable output, logically we should do exactly
769 what we do when not producing relocateable output. Therefore, your
770 patch is correct. In fact, it should probably always just set
771 reloc_entry->addend to 0 for all cases, since it is, in fact, going to
772 add the value into the object file. This won't hurt the COFF code,
773 which doesn't use the addend; I'm not sure what it will do to other
774 formats (the thing to check for would be whether any formats both use
775 the addend and set partial_inplace).
777 When I wanted to make coff-i386 produce relocateable output, I ran
778 into the problem that you are running into: I wanted to remove that
779 line. Rather than risk it, I made the coff-i386 relocs use a special
780 function; it's coff_i386_reloc in coff-i386.c. The function
781 specifically adds the addend field into the object file, knowing that
782 bfd_perform_relocation is not going to. If you remove that line, then
783 coff-i386.c will wind up adding the addend field in twice. It's
784 trivial to fix; it just needs to be done.
786 The problem with removing the line is just that it may break some
787 working code. With BFD it's hard to be sure of anything. The right
788 way to deal with this is simply to build and test at least all the
789 supported COFF targets. It should be straightforward if time and disk
790 space consuming. For each target:
792 2) generate some executable, and link it using -r (I would
793 probably use paranoia.o and link against newlib/libc.a, which
794 for all the supported targets would be available in
795 /usr/cygnus/progressive/H-host/target/lib/libc.a).
796 3) make the change to reloc.c
797 4) rebuild the linker
799 6) if the resulting object files are the same, you have at least
801 7) if they are different you have to figure out which version is
804 relocation -= reloc_entry->addend;
806 reloc_entry->addend = 0;
810 reloc_entry->addend = relocation;
816 reloc_entry->addend = 0;
819 /* FIXME: This overflow checking is incomplete, because the value
820 might have overflowed before we get here. For a correct check we
821 need to compute the value in a size larger than bitsize, but we
822 can't reasonably do that for a reloc the same size as a host
824 FIXME: We should also do overflow checking on the result after
825 adding in the value contained in the object file. */
826 if (howto->complain_on_overflow != complain_overflow_dont
827 && flag == bfd_reloc_ok)
828 flag = bfd_check_overflow (howto->complain_on_overflow, howto->bitsize,
829 howto->rightshift, relocation);
832 Either we are relocating all the way, or we don't want to apply
833 the relocation to the reloc entry (probably because there isn't
834 any room in the output format to describe addends to relocs)
837 /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
838 (OSF version 1.3, compiler version 3.11). It miscompiles the
852 x <<= (unsigned long) s.i0;
856 printf ("succeeded (%lx)\n", x);
860 relocation >>= (bfd_vma) howto->rightshift;
862 /* Shift everything up to where it's going to be used */
864 relocation <<= (bfd_vma) howto->bitpos;
866 /* Wait for the day when all have the mask in them */
869 i instruction to be left alone
870 o offset within instruction
871 r relocation offset to apply
880 i i i i i o o o o o from bfd_get<size>
881 and S S S S S to get the size offset we want
882 + r r r r r r r r r r to get the final value to place
883 and D D D D D to chop to right size
884 -----------------------
887 ... i i i i i o o o o o from bfd_get<size>
888 and N N N N N get instruction
889 -----------------------
895 -----------------------
896 R R R R R R R R R R put into bfd_put<size>
900 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
906 char x = bfd_get_8 (abfd, (char *) data + addr);
908 bfd_put_8 (abfd, x, (unsigned char *) data + addr);
914 short x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
916 bfd_put_16 (abfd, x, (unsigned char *) data + addr);
921 long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
923 bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
928 long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
929 relocation = -relocation;
931 bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
937 long x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
938 relocation = -relocation;
940 bfd_put_16 (abfd, x, (bfd_byte *) data + addr);
951 bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + addr);
953 bfd_put_64 (abfd, x, (bfd_byte *) data + addr);
960 return bfd_reloc_other;
968 bfd_install_relocation
971 bfd_reloc_status_type
972 bfd_install_relocation
974 arelent *reloc_entry,
975 PTR data, bfd_vma data_start,
976 asection *input_section,
977 char **error_message);
980 This looks remarkably like <<bfd_perform_relocation>>, except it
981 does not expect that the section contents have been filled in.
982 I.e., it's suitable for use when creating, rather than applying
985 For now, this function should be considered reserved for the
991 bfd_reloc_status_type
992 bfd_install_relocation (abfd, reloc_entry, data_start, data_start_offset,
993 input_section, error_message)
995 arelent *reloc_entry;
997 bfd_vma data_start_offset;
998 asection *input_section;
999 char **error_message;
1002 bfd_reloc_status_type flag = bfd_reloc_ok;
1003 bfd_size_type addr = reloc_entry->address;
1004 bfd_vma output_base = 0;
1005 reloc_howto_type *howto = reloc_entry->howto;
1006 asection *reloc_target_output_section;
1010 symbol = *(reloc_entry->sym_ptr_ptr);
1011 if (bfd_is_abs_section (symbol->section))
1013 reloc_entry->address += input_section->output_offset;
1014 return bfd_reloc_ok;
1017 /* If there is a function supplied to handle this relocation type,
1018 call it. It'll return `bfd_reloc_continue' if further processing
1020 if (howto->special_function)
1022 bfd_reloc_status_type cont;
1024 /* XXX - The special_function calls haven't been fixed up to deal
1025 with creating new relocations and section contents. */
1026 cont = howto->special_function (abfd, reloc_entry, symbol,
1027 /* XXX - Non-portable! */
1028 ((bfd_byte *) data_start
1029 - data_start_offset),
1030 input_section, abfd, error_message);
1031 if (cont != bfd_reloc_continue)
1035 /* Is the address of the relocation really within the section? */
1036 if (reloc_entry->address > input_section->_cooked_size)
1037 return bfd_reloc_outofrange;
1039 /* Work out which section the relocation is targetted at and the
1040 initial relocation command value. */
1042 /* Get symbol value. (Common symbols are special.) */
1043 if (bfd_is_com_section (symbol->section))
1046 relocation = symbol->value;
1048 reloc_target_output_section = symbol->section->output_section;
1050 /* Convert input-section-relative symbol value to absolute. */
1051 if (howto->partial_inplace == false)
1054 output_base = reloc_target_output_section->vma;
1056 relocation += output_base + symbol->section->output_offset;
1058 /* Add in supplied addend. */
1059 relocation += reloc_entry->addend;
1061 /* Here the variable relocation holds the final address of the
1062 symbol we are relocating against, plus any addend. */
1064 if (howto->pc_relative == true)
1066 /* This is a PC relative relocation. We want to set RELOCATION
1067 to the distance between the address of the symbol and the
1068 location. RELOCATION is already the address of the symbol.
1070 We start by subtracting the address of the section containing
1073 If pcrel_offset is set, we must further subtract the position
1074 of the location within the section. Some targets arrange for
1075 the addend to be the negative of the position of the location
1076 within the section; for example, i386-aout does this. For
1077 i386-aout, pcrel_offset is false. Some other targets do not
1078 include the position of the location; for example, m88kbcs,
1079 or ELF. For those targets, pcrel_offset is true.
1081 If we are producing relocateable output, then we must ensure
1082 that this reloc will be correctly computed when the final
1083 relocation is done. If pcrel_offset is false we want to wind
1084 up with the negative of the location within the section,
1085 which means we must adjust the existing addend by the change
1086 in the location within the section. If pcrel_offset is true
1087 we do not want to adjust the existing addend at all.
1089 FIXME: This seems logical to me, but for the case of
1090 producing relocateable output it is not what the code
1091 actually does. I don't want to change it, because it seems
1092 far too likely that something will break. */
1095 input_section->output_section->vma + input_section->output_offset;
1097 if (howto->pcrel_offset == true && howto->partial_inplace == true)
1098 relocation -= reloc_entry->address;
1101 if (howto->partial_inplace == false)
1103 /* This is a partial relocation, and we want to apply the relocation
1104 to the reloc entry rather than the raw data. Modify the reloc
1105 inplace to reflect what we now know. */
1106 reloc_entry->addend = relocation;
1107 reloc_entry->address += input_section->output_offset;
1112 /* This is a partial relocation, but inplace, so modify the
1115 If we've relocated with a symbol with a section, change
1116 into a ref to the section belonging to the symbol. */
1118 reloc_entry->address += input_section->output_offset;
1121 if (abfd->xvec->flavour == bfd_target_coff_flavour
1122 && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
1123 && strcmp (abfd->xvec->name, "xcoff-powermac") != 0
1124 && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
1125 && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
1128 /* For m68k-coff, the addend was being subtracted twice during
1129 relocation with -r. Removing the line below this comment
1130 fixes that problem; see PR 2953.
1132 However, Ian wrote the following, regarding removing the line below,
1133 which explains why it is still enabled: --djm
1135 If you put a patch like that into BFD you need to check all the COFF
1136 linkers. I am fairly certain that patch will break coff-i386 (e.g.,
1137 SCO); see coff_i386_reloc in coff-i386.c where I worked around the
1138 problem in a different way. There may very well be a reason that the
1139 code works as it does.
1141 Hmmm. The first obvious point is that bfd_install_relocation should
1142 not have any tests that depend upon the flavour. It's seem like
1143 entirely the wrong place for such a thing. The second obvious point
1144 is that the current code ignores the reloc addend when producing
1145 relocateable output for COFF. That's peculiar. In fact, I really
1146 have no idea what the point of the line you want to remove is.
1148 A typical COFF reloc subtracts the old value of the symbol and adds in
1149 the new value to the location in the object file (if it's a pc
1150 relative reloc it adds the difference between the symbol value and the
1151 location). When relocating we need to preserve that property.
1153 BFD handles this by setting the addend to the negative of the old
1154 value of the symbol. Unfortunately it handles common symbols in a
1155 non-standard way (it doesn't subtract the old value) but that's a
1156 different story (we can't change it without losing backward
1157 compatibility with old object files) (coff-i386 does subtract the old
1158 value, to be compatible with existing coff-i386 targets, like SCO).
1160 So everything works fine when not producing relocateable output. When
1161 we are producing relocateable output, logically we should do exactly
1162 what we do when not producing relocateable output. Therefore, your
1163 patch is correct. In fact, it should probably always just set
1164 reloc_entry->addend to 0 for all cases, since it is, in fact, going to
1165 add the value into the object file. This won't hurt the COFF code,
1166 which doesn't use the addend; I'm not sure what it will do to other
1167 formats (the thing to check for would be whether any formats both use
1168 the addend and set partial_inplace).
1170 When I wanted to make coff-i386 produce relocateable output, I ran
1171 into the problem that you are running into: I wanted to remove that
1172 line. Rather than risk it, I made the coff-i386 relocs use a special
1173 function; it's coff_i386_reloc in coff-i386.c. The function
1174 specifically adds the addend field into the object file, knowing that
1175 bfd_install_relocation is not going to. If you remove that line, then
1176 coff-i386.c will wind up adding the addend field in twice. It's
1177 trivial to fix; it just needs to be done.
1179 The problem with removing the line is just that it may break some
1180 working code. With BFD it's hard to be sure of anything. The right
1181 way to deal with this is simply to build and test at least all the
1182 supported COFF targets. It should be straightforward if time and disk
1183 space consuming. For each target:
1185 2) generate some executable, and link it using -r (I would
1186 probably use paranoia.o and link against newlib/libc.a, which
1187 for all the supported targets would be available in
1188 /usr/cygnus/progressive/H-host/target/lib/libc.a).
1189 3) make the change to reloc.c
1190 4) rebuild the linker
1192 6) if the resulting object files are the same, you have at least
1194 7) if they are different you have to figure out which version is
1197 relocation -= reloc_entry->addend;
1199 reloc_entry->addend = 0;
1203 reloc_entry->addend = relocation;
1207 /* FIXME: This overflow checking is incomplete, because the value
1208 might have overflowed before we get here. For a correct check we
1209 need to compute the value in a size larger than bitsize, but we
1210 can't reasonably do that for a reloc the same size as a host
1212 FIXME: We should also do overflow checking on the result after
1213 adding in the value contained in the object file. */
1214 if (howto->complain_on_overflow != complain_overflow_dont)
1215 flag = bfd_check_overflow (howto->complain_on_overflow, howto->bitsize,
1216 howto->rightshift, relocation);
1219 Either we are relocating all the way, or we don't want to apply
1220 the relocation to the reloc entry (probably because there isn't
1221 any room in the output format to describe addends to relocs)
1224 /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
1225 (OSF version 1.3, compiler version 3.11). It miscompiles the
1239 x <<= (unsigned long) s.i0;
1241 printf ("failed\n");
1243 printf ("succeeded (%lx)\n", x);
1247 relocation >>= (bfd_vma) howto->rightshift;
1249 /* Shift everything up to where it's going to be used */
1251 relocation <<= (bfd_vma) howto->bitpos;
1253 /* Wait for the day when all have the mask in them */
1256 i instruction to be left alone
1257 o offset within instruction
1258 r relocation offset to apply
1267 i i i i i o o o o o from bfd_get<size>
1268 and S S S S S to get the size offset we want
1269 + r r r r r r r r r r to get the final value to place
1270 and D D D D D to chop to right size
1271 -----------------------
1274 ... i i i i i o o o o o from bfd_get<size>
1275 and N N N N N get instruction
1276 -----------------------
1282 -----------------------
1283 R R R R R R R R R R put into bfd_put<size>
1287 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
1289 data = (bfd_byte *) data_start + (addr - data_start_offset);
1291 switch (howto->size)
1295 char x = bfd_get_8 (abfd, (char *) data);
1297 bfd_put_8 (abfd, x, (unsigned char *) data);
1303 short x = bfd_get_16 (abfd, (bfd_byte *) data);
1305 bfd_put_16 (abfd, x, (unsigned char *) data);
1310 long x = bfd_get_32 (abfd, (bfd_byte *) data);
1312 bfd_put_32 (abfd, x, (bfd_byte *) data);
1317 long x = bfd_get_32 (abfd, (bfd_byte *) data);
1318 relocation = -relocation;
1320 bfd_put_32 (abfd, x, (bfd_byte *) data);
1330 bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data);
1332 bfd_put_64 (abfd, x, (bfd_byte *) data);
1336 return bfd_reloc_other;
1342 /* This relocation routine is used by some of the backend linkers.
1343 They do not construct asymbol or arelent structures, so there is no
1344 reason for them to use bfd_perform_relocation. Also,
1345 bfd_perform_relocation is so hacked up it is easier to write a new
1346 function than to try to deal with it.
1348 This routine does a final relocation. Whether it is useful for a
1349 relocateable link depends upon how the object format defines
1352 FIXME: This routine ignores any special_function in the HOWTO,
1353 since the existing special_function values have been written for
1354 bfd_perform_relocation.
1356 HOWTO is the reloc howto information.
1357 INPUT_BFD is the BFD which the reloc applies to.
1358 INPUT_SECTION is the section which the reloc applies to.
1359 CONTENTS is the contents of the section.
1360 ADDRESS is the address of the reloc within INPUT_SECTION.
1361 VALUE is the value of the symbol the reloc refers to.
1362 ADDEND is the addend of the reloc. */
1364 bfd_reloc_status_type
1365 _bfd_final_link_relocate (howto, input_bfd, input_section, contents, address,
1367 reloc_howto_type *howto;
1369 asection *input_section;
1377 /* Sanity check the address. */
1378 if (address > input_section->_raw_size)
1379 return bfd_reloc_outofrange;
1381 /* This function assumes that we are dealing with a basic relocation
1382 against a symbol. We want to compute the value of the symbol to
1383 relocate to. This is just VALUE, the value of the symbol, plus
1384 ADDEND, any addend associated with the reloc. */
1385 relocation = value + addend;
1387 /* If the relocation is PC relative, we want to set RELOCATION to
1388 the distance between the symbol (currently in RELOCATION) and the
1389 location we are relocating. Some targets (e.g., i386-aout)
1390 arrange for the contents of the section to be the negative of the
1391 offset of the location within the section; for such targets
1392 pcrel_offset is false. Other targets (e.g., m88kbcs or ELF)
1393 simply leave the contents of the section as zero; for such
1394 targets pcrel_offset is true. If pcrel_offset is false we do not
1395 need to subtract out the offset of the location within the
1396 section (which is just ADDRESS). */
1397 if (howto->pc_relative)
1399 relocation -= (input_section->output_section->vma
1400 + input_section->output_offset);
1401 if (howto->pcrel_offset)
1402 relocation -= address;
1405 return _bfd_relocate_contents (howto, input_bfd, relocation,
1406 contents + address);
1409 /* Relocate a given location using a given value and howto. */
1411 bfd_reloc_status_type
1412 _bfd_relocate_contents (howto, input_bfd, relocation, location)
1413 reloc_howto_type *howto;
1422 /* If the size is negative, negate RELOCATION. This isn't very
1424 if (howto->size < 0)
1425 relocation = -relocation;
1427 /* Get the value we are going to relocate. */
1428 size = bfd_get_reloc_size (howto);
1435 x = bfd_get_8 (input_bfd, location);
1438 x = bfd_get_16 (input_bfd, location);
1441 x = bfd_get_32 (input_bfd, location);
1445 x = bfd_get_64 (input_bfd, location);
1452 /* Check for overflow. FIXME: We may drop bits during the addition
1453 which we don't check for. We must either check at every single
1454 operation, which would be tedious, or we must do the computations
1455 in a type larger than bfd_vma, which would be inefficient. */
1457 if (howto->complain_on_overflow != complain_overflow_dont)
1460 bfd_signed_vma signed_check;
1462 bfd_signed_vma signed_add;
1464 if (howto->rightshift == 0)
1467 signed_check = (bfd_signed_vma) relocation;
1471 /* Drop unwanted bits from the value we are relocating to. */
1472 check = relocation >> howto->rightshift;
1474 /* If this is a signed value, the rightshift just dropped
1475 leading 1 bits (assuming twos complement). */
1476 if ((bfd_signed_vma) relocation >= 0)
1477 signed_check = check;
1479 signed_check = (check
1481 & ~((bfd_vma) - 1 >> howto->rightshift)));
1484 /* Get the value from the object file. */
1485 add = x & howto->src_mask;
1487 /* Get the value from the object file with an appropriate sign.
1488 The expression involving howto->src_mask isolates the upper
1489 bit of src_mask. If that bit is set in the value we are
1490 adding, it is negative, and we subtract out that number times
1491 two. If src_mask includes the highest possible bit, then we
1492 can not get the upper bit, but that does not matter since
1493 signed_add needs no adjustment to become negative in that
1496 if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
1497 signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
1499 /* Add the value from the object file, shifted so that it is a
1501 if (howto->bitpos == 0)
1504 signed_check += signed_add;
1508 check += add >> howto->bitpos;
1510 /* For the signed case we use ADD, rather than SIGNED_ADD,
1511 to avoid warnings from SVR4 cc. This is OK since we
1512 explictly handle the sign bits. */
1513 if (signed_add >= 0)
1514 signed_check += add >> howto->bitpos;
1516 signed_check += ((add >> howto->bitpos)
1518 & ~((bfd_vma) - 1 >> howto->bitpos)));
1521 switch (howto->complain_on_overflow)
1523 case complain_overflow_signed:
1525 /* Assumes two's complement. */
1526 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
1527 bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
1529 if (signed_check > reloc_signed_max
1530 || signed_check < reloc_signed_min)
1534 case complain_overflow_unsigned:
1536 /* Assumes two's complement. This expression avoids
1537 overflow if howto->bitsize is the number of bits in
1539 bfd_vma reloc_unsigned_max =
1540 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1542 if (check > reloc_unsigned_max)
1546 case complain_overflow_bitfield:
1548 /* Assumes two's complement. This expression avoids
1549 overflow if howto->bitsize is the number of bits in
1551 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1553 if ((check & ~reloc_bits) != 0
1554 && (((bfd_vma) signed_check & ~reloc_bits)
1555 != (-1 & ~reloc_bits)))
1564 /* Put RELOCATION in the right bits. */
1565 relocation >>= (bfd_vma) howto->rightshift;
1566 relocation <<= (bfd_vma) howto->bitpos;
1568 /* Add RELOCATION to the right bits of X. */
1569 x = ((x & ~howto->dst_mask)
1570 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
1572 /* Put the relocated value back in the object file. */
1579 bfd_put_8 (input_bfd, x, location);
1582 bfd_put_16 (input_bfd, x, location);
1585 bfd_put_32 (input_bfd, x, location);
1589 bfd_put_64 (input_bfd, x, location);
1596 return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
1602 howto manager, , typedef arelent, Relocations
1607 When an application wants to create a relocation, but doesn't
1608 know what the target machine might call it, it can find out by
1609 using this bit of code.
1618 The insides of a reloc code. The idea is that, eventually, there
1619 will be one enumerator for every type of relocation we ever do.
1620 Pass one of these values to <<bfd_reloc_type_lookup>>, and it'll
1621 return a howto pointer.
1623 This does mean that the application must determine the correct
1624 enumerator value; you can't get a howto pointer from a random set
1645 Basic absolute relocations of N bits.
1660 PC-relative relocations. Sometimes these are relative to the address
1661 of the relocation itself; sometimes they are relative to the start of
1662 the section containing the relocation. It depends on the specific target.
1664 The 24-bit relocation is used in some Intel 960 configurations.
1667 BFD_RELOC_32_GOT_PCREL
1669 BFD_RELOC_16_GOT_PCREL
1671 BFD_RELOC_8_GOT_PCREL
1677 BFD_RELOC_LO16_GOTOFF
1679 BFD_RELOC_HI16_GOTOFF
1681 BFD_RELOC_HI16_S_GOTOFF
1685 BFD_RELOC_32_PLT_PCREL
1687 BFD_RELOC_24_PLT_PCREL
1689 BFD_RELOC_16_PLT_PCREL
1691 BFD_RELOC_8_PLT_PCREL
1697 BFD_RELOC_LO16_PLTOFF
1699 BFD_RELOC_HI16_PLTOFF
1701 BFD_RELOC_HI16_S_PLTOFF
1708 BFD_RELOC_68K_GLOB_DAT
1710 BFD_RELOC_68K_JMP_SLOT
1712 BFD_RELOC_68K_RELATIVE
1714 Relocations used by 68K ELF.
1717 BFD_RELOC_32_BASEREL
1719 BFD_RELOC_16_BASEREL
1721 BFD_RELOC_LO16_BASEREL
1723 BFD_RELOC_HI16_BASEREL
1725 BFD_RELOC_HI16_S_BASEREL
1731 Linkage-table relative.
1736 Absolute 8-bit relocation, but used to form an address like 0xFFnn.
1739 BFD_RELOC_32_PCREL_S2
1741 BFD_RELOC_16_PCREL_S2
1743 BFD_RELOC_23_PCREL_S2
1745 These PC-relative relocations are stored as word displacements --
1746 i.e., byte displacements shifted right two bits. The 30-bit word
1747 displacement (<<32_PCREL_S2>> -- 32 bits, shifted 2) is used on the
1748 SPARC. (SPARC tools generally refer to this as <<WDISP30>>.) The
1749 signed 16-bit displacement is used on the MIPS, and the 23-bit
1750 displacement is used on the Alpha.
1757 High 22 bits and low 10 bits of 32-bit value, placed into lower bits of
1758 the target word. These are used on the SPARC.
1765 For systems that allocate a Global Pointer register, these are
1766 displacements off that register. These relocation types are
1767 handled specially, because the value the register will have is
1768 decided relatively late.
1772 BFD_RELOC_I960_CALLJ
1774 Reloc types used for i960/b.out.
1779 BFD_RELOC_SPARC_WDISP22
1785 BFD_RELOC_SPARC_GOT10
1787 BFD_RELOC_SPARC_GOT13
1789 BFD_RELOC_SPARC_GOT22
1791 BFD_RELOC_SPARC_PC10
1793 BFD_RELOC_SPARC_PC22
1795 BFD_RELOC_SPARC_WPLT30
1797 BFD_RELOC_SPARC_COPY
1799 BFD_RELOC_SPARC_GLOB_DAT
1801 BFD_RELOC_SPARC_JMP_SLOT
1803 BFD_RELOC_SPARC_RELATIVE
1805 BFD_RELOC_SPARC_UA32
1807 SPARC ELF relocations. There is probably some overlap with other
1808 relocation types already defined.
1811 BFD_RELOC_SPARC_BASE13
1813 BFD_RELOC_SPARC_BASE22
1815 I think these are specific to SPARC a.out (e.g., Sun 4).
1825 BFD_RELOC_SPARC_OLO10
1827 BFD_RELOC_SPARC_HH22
1829 BFD_RELOC_SPARC_HM10
1831 BFD_RELOC_SPARC_LM22
1833 BFD_RELOC_SPARC_PC_HH22
1835 BFD_RELOC_SPARC_PC_HM10
1837 BFD_RELOC_SPARC_PC_LM22
1839 BFD_RELOC_SPARC_WDISP16
1841 BFD_RELOC_SPARC_WDISP19
1849 BFD_RELOC_SPARC_DISP64
1852 BFD_RELOC_SPARC_PLT64
1854 BFD_RELOC_SPARC_HIX22
1856 BFD_RELOC_SPARC_LOX10
1864 BFD_RELOC_SPARC_REGISTER
1869 BFD_RELOC_ALPHA_GPDISP_HI16
1871 Alpha ECOFF and ELF relocations. Some of these treat the symbol or
1872 "addend" in some special way.
1873 For GPDISP_HI16 ("gpdisp") relocations, the symbol is ignored when
1874 writing; when reading, it will be the absolute section symbol. The
1875 addend is the displacement in bytes of the "lda" instruction from
1876 the "ldah" instruction (which is at the address of this reloc).
1878 BFD_RELOC_ALPHA_GPDISP_LO16
1880 For GPDISP_LO16 ("ignore") relocations, the symbol is handled as
1881 with GPDISP_HI16 relocs. The addend is ignored when writing the
1882 relocations out, and is filled in with the file's GP value on
1883 reading, for convenience.
1886 BFD_RELOC_ALPHA_GPDISP
1888 The ELF GPDISP relocation is exactly the same as the GPDISP_HI16
1889 relocation except that there is no accompanying GPDISP_LO16
1893 BFD_RELOC_ALPHA_LITERAL
1895 BFD_RELOC_ALPHA_ELF_LITERAL
1897 BFD_RELOC_ALPHA_LITUSE
1899 The Alpha LITERAL/LITUSE relocs are produced by a symbol reference;
1900 the assembler turns it into a LDQ instruction to load the address of
1901 the symbol, and then fills in a register in the real instruction.
1903 The LITERAL reloc, at the LDQ instruction, refers to the .lita
1904 section symbol. The addend is ignored when writing, but is filled
1905 in with the file's GP value on reading, for convenience, as with the
1908 The ELF_LITERAL reloc is somewhere between 16_GOTOFF and GPDISP_LO16.
1909 It should refer to the symbol to be referenced, as with 16_GOTOFF,
1910 but it generates output not based on the position within the .got
1911 section, but relative to the GP value chosen for the file during the
1914 The LITUSE reloc, on the instruction using the loaded address, gives
1915 information to the linker that it might be able to use to optimize
1916 away some literal section references. The symbol is ignored (read
1917 as the absolute section symbol), and the "addend" indicates the type
1918 of instruction using the register:
1919 1 - "memory" fmt insn
1920 2 - byte-manipulation (byte offset reg)
1921 3 - jsr (target of branch)
1923 The GNU linker currently doesn't do any of this optimizing.
1926 BFD_RELOC_ALPHA_HINT
1928 The HINT relocation indicates a value that should be filled into the
1929 "hint" field of a jmp/jsr/ret instruction, for possible branch-
1930 prediction logic which may be provided on some processors.
1933 BFD_RELOC_ALPHA_LINKAGE
1935 The LINKAGE relocation outputs a linkage pair in the object file,
1936 which is filled by the linker.
1939 BFD_RELOC_ALPHA_CODEADDR
1941 The CODEADDR relocation outputs a STO_CA in the object file,
1942 which is filled by the linker.
1947 Bits 27..2 of the relocation address shifted right 2 bits;
1948 simple reloc otherwise.
1951 BFD_RELOC_MIPS16_JMP
1953 The MIPS16 jump instruction.
1956 BFD_RELOC_MIPS16_GPREL
1958 MIPS16 GP relative reloc.
1963 High 16 bits of 32-bit value; simple reloc.
1967 High 16 bits of 32-bit value but the low 16 bits will be sign
1968 extended and added to form the final result. If the low 16
1969 bits form a negative number, we need to add one to the high value
1970 to compensate for the borrow when the low bits are added.
1976 BFD_RELOC_PCREL_HI16_S
1978 Like BFD_RELOC_HI16_S, but PC relative.
1980 BFD_RELOC_PCREL_LO16
1982 Like BFD_RELOC_LO16, but PC relative.
1985 BFD_RELOC_MIPS_GPREL
1988 Relocation relative to the global pointer.
1991 BFD_RELOC_MIPS_LITERAL
1993 Relocation against a MIPS literal section.
1996 BFD_RELOC_MIPS_GOT16
1998 BFD_RELOC_MIPS_CALL16
2000 BFD_RELOC_MIPS_GPREL32
2003 BFD_RELOC_MIPS_GOT_HI16
2005 BFD_RELOC_MIPS_GOT_LO16
2007 BFD_RELOC_MIPS_CALL_HI16
2009 BFD_RELOC_MIPS_CALL_LO16
2011 MIPS ELF relocations.
2020 BFD_RELOC_386_GLOB_DAT
2022 BFD_RELOC_386_JUMP_SLOT
2024 BFD_RELOC_386_RELATIVE
2026 BFD_RELOC_386_GOTOFF
2030 i386/elf relocations
2033 BFD_RELOC_NS32K_IMM_8
2035 BFD_RELOC_NS32K_IMM_16
2037 BFD_RELOC_NS32K_IMM_32
2039 BFD_RELOC_NS32K_IMM_8_PCREL
2041 BFD_RELOC_NS32K_IMM_16_PCREL
2043 BFD_RELOC_NS32K_IMM_32_PCREL
2045 BFD_RELOC_NS32K_DISP_8
2047 BFD_RELOC_NS32K_DISP_16
2049 BFD_RELOC_NS32K_DISP_32
2051 BFD_RELOC_NS32K_DISP_8_PCREL
2053 BFD_RELOC_NS32K_DISP_16_PCREL
2055 BFD_RELOC_NS32K_DISP_32_PCREL
2068 BFD_RELOC_PPC_B16_BRTAKEN
2070 BFD_RELOC_PPC_B16_BRNTAKEN
2074 BFD_RELOC_PPC_BA16_BRTAKEN
2076 BFD_RELOC_PPC_BA16_BRNTAKEN
2080 BFD_RELOC_PPC_GLOB_DAT
2082 BFD_RELOC_PPC_JMP_SLOT
2084 BFD_RELOC_PPC_RELATIVE
2086 BFD_RELOC_PPC_LOCAL24PC
2088 BFD_RELOC_PPC_EMB_NADDR32
2090 BFD_RELOC_PPC_EMB_NADDR16
2092 BFD_RELOC_PPC_EMB_NADDR16_LO
2094 BFD_RELOC_PPC_EMB_NADDR16_HI
2096 BFD_RELOC_PPC_EMB_NADDR16_HA
2098 BFD_RELOC_PPC_EMB_SDAI16
2100 BFD_RELOC_PPC_EMB_SDA2I16
2102 BFD_RELOC_PPC_EMB_SDA2REL
2104 BFD_RELOC_PPC_EMB_SDA21
2106 BFD_RELOC_PPC_EMB_MRKREF
2108 BFD_RELOC_PPC_EMB_RELSEC16
2110 BFD_RELOC_PPC_EMB_RELST_LO
2112 BFD_RELOC_PPC_EMB_RELST_HI
2114 BFD_RELOC_PPC_EMB_RELST_HA
2116 BFD_RELOC_PPC_EMB_BIT_FLD
2118 BFD_RELOC_PPC_EMB_RELSDA
2120 Power(rs6000) and PowerPC relocations.
2125 The type of reloc used to build a contructor table - at the moment
2126 probably a 32 bit wide absolute relocation, but the target can choose.
2127 It generally does map to one of the other relocation types.
2130 BFD_RELOC_ARM_PCREL_BRANCH
2132 ARM 26 bit pc-relative branch. The lowest two bits must be zero and are
2133 not stored in the instruction.
2135 BFD_RELOC_ARM_IMMEDIATE
2137 BFD_RELOC_ARM_OFFSET_IMM
2139 BFD_RELOC_ARM_SHIFT_IMM
2145 BFD_RELOC_ARM_CP_OFF_IMM
2147 BFD_RELOC_ARM_ADR_IMM
2149 BFD_RELOC_ARM_LDR_IMM
2151 BFD_RELOC_ARM_LITERAL
2153 BFD_RELOC_ARM_IN_POOL
2155 BFD_RELOC_ARM_OFFSET_IMM8
2157 BFD_RELOC_ARM_HWLITERAL
2159 BFD_RELOC_ARM_THUMB_ADD
2161 BFD_RELOC_ARM_THUMB_IMM
2163 BFD_RELOC_ARM_THUMB_SHIFT
2165 BFD_RELOC_ARM_THUMB_OFFSET
2167 These relocs are only used within the ARM assembler. They are not
2168 (at present) written to any object files.
2171 BFD_RELOC_SH_PCDISP8BY2
2173 BFD_RELOC_SH_PCDISP12BY2
2177 BFD_RELOC_SH_IMM4BY2
2179 BFD_RELOC_SH_IMM4BY4
2183 BFD_RELOC_SH_IMM8BY2
2185 BFD_RELOC_SH_IMM8BY4
2187 BFD_RELOC_SH_PCRELIMM8BY2
2189 BFD_RELOC_SH_PCRELIMM8BY4
2191 BFD_RELOC_SH_SWITCH16
2193 BFD_RELOC_SH_SWITCH32
2207 Hitachi SH relocs. Not all of these appear in object files.
2210 BFD_RELOC_THUMB_PCREL_BRANCH9
2212 BFD_RELOC_THUMB_PCREL_BRANCH12
2214 BFD_RELOC_THUMB_PCREL_BRANCH23
2216 Thumb 23-, 12- and 9-bit pc-relative branches. The lowest bit must
2217 be zero and is not stored in the instruction.
2220 BFD_RELOC_ARC_B22_PCREL
2222 Argonaut RISC Core (ARC) relocs.
2223 ARC 22 bit pc-relative branch. The lowest two bits must be zero and are
2224 not stored in the instruction. The high 20 bits are installed in bits 26
2225 through 7 of the instruction.
2229 ARC 26 bit absolute branch. The lowest two bits must be zero and are not
2230 stored in the instruction. The high 24 bits are installed in bits 23
2235 BFD_RELOC_D10V_10_PCREL_R
2237 Mitsubishi D10V relocs.
2238 This is a 10-bit reloc with the right 2 bits
2241 BFD_RELOC_D10V_10_PCREL_L
2243 Mitsubishi D10V relocs.
2244 This is a 10-bit reloc with the right 2 bits
2245 assumed to be 0. This is the same as the previous reloc
2246 except it is in the left container, i.e.,
2247 shifted left 15 bits.
2251 This is an 18-bit reloc with the right 2 bits
2254 BFD_RELOC_D10V_18_PCREL
2256 This is an 18-bit reloc with the right 2 bits
2261 {* start-sanitize-d30v *}
2265 Mitsubishi D30V relocs.
2266 This is a 6-bit absolute reloc.
2268 BFD_RELOC_D30V_9_PCREL
2270 This is a 6-bit pc-relative reloc with
2271 the right 3 bits assumed to be 0.
2273 BFD_RELOC_D30V_9_PCREL_R
2275 This is a 6-bit pc-relative reloc with
2276 the right 3 bits assumed to be 0. Same
2277 as the previous reloc but on the right side
2282 This is a 12-bit absolute reloc with the
2283 right 3 bitsassumed to be 0.
2285 BFD_RELOC_D30V_15_PCREL
2287 This is a 12-bit pc-relative reloc with
2288 the right 3 bits assumed to be 0.
2290 BFD_RELOC_D30V_15_PCREL_R
2292 This is a 12-bit pc-relative reloc with
2293 the right 3 bits assumed to be 0. Same
2294 as the previous reloc but on the right side
2299 This is an 18-bit absolute reloc with
2300 the right 3 bits assumed to be 0.
2302 BFD_RELOC_D30V_21_PCREL
2304 This is an 18-bit pc-relative reloc with
2305 the right 3 bits assumed to be 0.
2307 BFD_RELOC_D30V_21_PCREL_R
2309 This is an 18-bit pc-relative reloc with
2310 the right 3 bits assumed to be 0. Same
2311 as the previous reloc but on the right side
2316 This is a 32-bit absolute reloc.
2318 BFD_RELOC_D30V_32_PCREL
2320 This is a 32-bit pc-relative reloc.
2322 {* end-sanitize-d30v *}
2327 Mitsubishi M32R relocs.
2328 This is a 24 bit absolute address.
2330 BFD_RELOC_M32R_10_PCREL
2332 This is a 10-bit pc-relative reloc with the right 2 bits assumed to be 0.
2334 BFD_RELOC_M32R_18_PCREL
2336 This is an 18-bit reloc with the right 2 bits assumed to be 0.
2338 BFD_RELOC_M32R_26_PCREL
2340 This is a 26-bit reloc with the right 2 bits assumed to be 0.
2342 BFD_RELOC_M32R_HI16_ULO
2344 This is a 16-bit reloc containing the high 16 bits of an address
2345 used when the lower 16 bits are treated as unsigned.
2347 BFD_RELOC_M32R_HI16_SLO
2349 This is a 16-bit reloc containing the high 16 bits of an address
2350 used when the lower 16 bits are treated as signed.
2354 This is a 16-bit reloc containing the lower 16 bits of an address.
2356 BFD_RELOC_M32R_SDA16
2358 This is a 16-bit reloc containing the small data area offset for use in
2359 add3, load, and store instructions.
2362 BFD_RELOC_V850_9_PCREL
2364 This is a 9-bit reloc
2366 BFD_RELOC_V850_22_PCREL
2368 This is a 22-bit reloc
2371 BFD_RELOC_V850_SDA_16_16_OFFSET
2373 This is a 16 bit offset from the short data area pointer.
2375 BFD_RELOC_V850_SDA_15_16_OFFSET
2377 This is a 16 bit offset (of which only 15 bits are used) from the
2378 short data area pointer.
2380 BFD_RELOC_V850_ZDA_16_16_OFFSET
2382 This is a 16 bit offset from the zero data area pointer.
2384 BFD_RELOC_V850_ZDA_15_16_OFFSET
2386 This is a 16 bit offset (of which only 15 bits are used) from the
2387 zero data area pointer.
2389 BFD_RELOC_V850_TDA_6_8_OFFSET
2391 This is an 8 bit offset (of which only 6 bits are used) from the
2392 tiny data area pointer.
2394 BFD_RELOC_V850_TDA_7_8_OFFSET
2396 This is an 8bit offset (of which only 7 bits are used) from the tiny
2399 BFD_RELOC_V850_TDA_7_7_OFFSET
2401 This is a 7 bit offset from the tiny data area pointer.
2403 BFD_RELOC_V850_TDA_16_16_OFFSET
2405 This is a 16 bit offset from the tiny data area pointer.
2407 {* start-sanitize-v850e *}
2409 BFD_RELOC_V850_TDA_4_5_OFFSET
2411 This is a 5 bit offset (of which only 4 bits are used) from the tiny
2414 BFD_RELOC_V850_TDA_4_4_OFFSET
2416 This is a 4 bit offset from the tiny data area pointer.
2418 BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET
2420 This is a 16 bit offset from the short data area pointer, with the
2421 bits placed non-contigously in the instruction.
2423 BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET
2425 This is a 16 bit offset from the zero data area pointer, with the
2426 bits placed non-contigously in the instruction.
2428 BFD_RELOC_V850_CALLT_6_7_OFFSET
2430 This is a 6 bit offset from the call table base pointer.
2432 BFD_RELOC_V850_CALLT_16_16_OFFSET
2434 This is a 16 bit offset from the call table base pointer.
2436 {* end-sanitize-v850e *}
2439 BFD_RELOC_MN10300_32_PCREL
2441 This is a 32bit pcrel reloc for the mn10300, offset by two bytes in the
2444 BFD_RELOC_MN10300_16_PCREL
2446 This is a 16bit pcrel reloc for the mn10300, offset by two bytes in the
2450 {* start-sanitize-sky *}
2452 BFD_RELOC_TXVU_11_PCREL
2454 SKY TXVU Relocations.
2455 This is an 11-bit pc relative reloc. The recorded address is for the
2456 lower instruction word.
2458 {* end-sanitize-sky *}
2464 .typedef enum bfd_reloc_code_real bfd_reloc_code_real_type;
2470 bfd_reloc_type_lookup
2474 bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
2477 Return a pointer to a howto structure which, when
2478 invoked, will perform the relocation @var{code} on data from the
2485 bfd_reloc_type_lookup (abfd, code)
2487 bfd_reloc_code_real_type code;
2489 return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
2492 static reloc_howto_type bfd_howto_32 =
2493 HOWTO (0, 00, 2, 32, false, 0, complain_overflow_bitfield, 0, "VRT32", false, 0xffffffff, 0xffffffff, true);
2498 bfd_default_reloc_type_lookup
2501 reloc_howto_type *bfd_default_reloc_type_lookup
2502 (bfd *abfd, bfd_reloc_code_real_type code);
2505 Provides a default relocation lookup routine for any architecture.
2511 bfd_default_reloc_type_lookup (abfd, code)
2513 bfd_reloc_code_real_type code;
2517 case BFD_RELOC_CTOR:
2518 /* The type of reloc used in a ctor, which will be as wide as the
2519 address - so either a 64, 32, or 16 bitter. */
2520 switch (bfd_get_arch_info (abfd)->bits_per_address)
2525 return &bfd_howto_32;
2534 return (reloc_howto_type *) NULL;
2539 bfd_get_reloc_code_name
2542 const char *bfd_get_reloc_code_name (bfd_reloc_code_real_type code);
2545 Provides a printable name for the supplied relocation code.
2546 Useful mainly for printing error messages.
2550 bfd_get_reloc_code_name (code)
2551 bfd_reloc_code_real_type code;
2553 if (code > BFD_RELOC_UNUSED)
2555 return bfd_reloc_code_real_names[(int)code];
2560 bfd_generic_relax_section
2563 boolean bfd_generic_relax_section
2566 struct bfd_link_info *,
2570 Provides default handling for relaxing for back ends which
2571 don't do relaxing -- i.e., does nothing.
2576 bfd_generic_relax_section (abfd, section, link_info, again)
2579 struct bfd_link_info *link_info;
2588 bfd_generic_get_relocated_section_contents
2592 bfd_generic_get_relocated_section_contents (bfd *abfd,
2593 struct bfd_link_info *link_info,
2594 struct bfd_link_order *link_order,
2596 boolean relocateable,
2600 Provides default handling of relocation effort for back ends
2601 which can't be bothered to do it efficiently.
2606 bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data,
2607 relocateable, symbols)
2609 struct bfd_link_info *link_info;
2610 struct bfd_link_order *link_order;
2612 boolean relocateable;
2615 /* Get enough memory to hold the stuff */
2616 bfd *input_bfd = link_order->u.indirect.section->owner;
2617 asection *input_section = link_order->u.indirect.section;
2619 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
2620 arelent **reloc_vector = NULL;
2626 reloc_vector = (arelent **) bfd_malloc ((size_t) reloc_size);
2627 if (reloc_vector == NULL && reloc_size != 0)
2630 /* read in the section */
2631 if (!bfd_get_section_contents (input_bfd,
2635 input_section->_raw_size))
2638 /* We're not relaxing the section, so just copy the size info */
2639 input_section->_cooked_size = input_section->_raw_size;
2640 input_section->reloc_done = true;
2642 reloc_count = bfd_canonicalize_reloc (input_bfd,
2646 if (reloc_count < 0)
2649 if (reloc_count > 0)
2652 for (parent = reloc_vector; *parent != (arelent *) NULL;
2655 char *error_message = (char *) NULL;
2656 bfd_reloc_status_type r =
2657 bfd_perform_relocation (input_bfd,
2661 relocateable ? abfd : (bfd *) NULL,
2666 asection *os = input_section->output_section;
2668 /* A partial link, so keep the relocs */
2669 os->orelocation[os->reloc_count] = *parent;
2673 if (r != bfd_reloc_ok)
2677 case bfd_reloc_undefined:
2678 if (!((*link_info->callbacks->undefined_symbol)
2679 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
2680 input_bfd, input_section, (*parent)->address)))
2683 case bfd_reloc_dangerous:
2684 BFD_ASSERT (error_message != (char *) NULL);
2685 if (!((*link_info->callbacks->reloc_dangerous)
2686 (link_info, error_message, input_bfd, input_section,
2687 (*parent)->address)))
2690 case bfd_reloc_overflow:
2691 if (!((*link_info->callbacks->reloc_overflow)
2692 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
2693 (*parent)->howto->name, (*parent)->addend,
2694 input_bfd, input_section, (*parent)->address)))
2697 case bfd_reloc_outofrange:
2706 if (reloc_vector != NULL)
2707 free (reloc_vector);
2711 if (reloc_vector != NULL)
2712 free (reloc_vector);