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;
457 bfd_perform_relocation
460 bfd_reloc_status_type
461 bfd_perform_relocation
463 arelent *reloc_entry,
465 asection *input_section,
467 char **error_message);
470 If @var{output_bfd} is supplied to this function, the
471 generated image will be relocatable; the relocations are
472 copied to the output file after they have been changed to
473 reflect the new state of the world. There are two ways of
474 reflecting the results of partial linkage in an output file:
475 by modifying the output data in place, and by modifying the
476 relocation record. Some native formats (e.g., basic a.out and
477 basic coff) have no way of specifying an addend in the
478 relocation type, so the addend has to go in the output data.
479 This is no big deal since in these formats the output data
480 slot will always be big enough for the addend. Complex reloc
481 types with addends were invented to solve just this problem.
482 The @var{error_message} argument is set to an error message if
483 this return @code{bfd_reloc_dangerous}.
488 bfd_reloc_status_type
489 bfd_perform_relocation (abfd, reloc_entry, data, input_section, output_bfd,
492 arelent *reloc_entry;
494 asection *input_section;
496 char **error_message;
499 bfd_reloc_status_type flag = bfd_reloc_ok;
500 bfd_size_type addr = reloc_entry->address;
501 bfd_vma output_base = 0;
502 reloc_howto_type *howto = reloc_entry->howto;
503 asection *reloc_target_output_section;
506 symbol = *(reloc_entry->sym_ptr_ptr);
507 if (bfd_is_abs_section (symbol->section)
508 && output_bfd != (bfd *) NULL)
510 reloc_entry->address += input_section->output_offset;
514 /* If we are not producing relocateable output, return an error if
515 the symbol is not defined. An undefined weak symbol is
516 considered to have a value of zero (SVR4 ABI, p. 4-27). */
517 if (bfd_is_und_section (symbol->section)
518 && (symbol->flags & BSF_WEAK) == 0
519 && output_bfd == (bfd *) NULL)
520 flag = bfd_reloc_undefined;
522 /* If there is a function supplied to handle this relocation type,
523 call it. It'll return `bfd_reloc_continue' if further processing
525 if (howto->special_function)
527 bfd_reloc_status_type cont;
528 cont = howto->special_function (abfd, reloc_entry, symbol, data,
529 input_section, output_bfd,
531 if (cont != bfd_reloc_continue)
535 /* Is the address of the relocation really within the section? */
536 if (reloc_entry->address > input_section->_cooked_size)
537 return bfd_reloc_outofrange;
539 /* Work out which section the relocation is targetted at and the
540 initial relocation command value. */
542 /* Get symbol value. (Common symbols are special.) */
543 if (bfd_is_com_section (symbol->section))
546 relocation = symbol->value;
549 reloc_target_output_section = symbol->section->output_section;
551 /* Convert input-section-relative symbol value to absolute. */
552 if (output_bfd && howto->partial_inplace == false)
555 output_base = reloc_target_output_section->vma;
557 relocation += output_base + symbol->section->output_offset;
559 /* Add in supplied addend. */
560 relocation += reloc_entry->addend;
562 /* Here the variable relocation holds the final address of the
563 symbol we are relocating against, plus any addend. */
565 if (howto->pc_relative == true)
567 /* This is a PC relative relocation. We want to set RELOCATION
568 to the distance between the address of the symbol and the
569 location. RELOCATION is already the address of the symbol.
571 We start by subtracting the address of the section containing
574 If pcrel_offset is set, we must further subtract the position
575 of the location within the section. Some targets arrange for
576 the addend to be the negative of the position of the location
577 within the section; for example, i386-aout does this. For
578 i386-aout, pcrel_offset is false. Some other targets do not
579 include the position of the location; for example, m88kbcs,
580 or ELF. For those targets, pcrel_offset is true.
582 If we are producing relocateable output, then we must ensure
583 that this reloc will be correctly computed when the final
584 relocation is done. If pcrel_offset is false we want to wind
585 up with the negative of the location within the section,
586 which means we must adjust the existing addend by the change
587 in the location within the section. If pcrel_offset is true
588 we do not want to adjust the existing addend at all.
590 FIXME: This seems logical to me, but for the case of
591 producing relocateable output it is not what the code
592 actually does. I don't want to change it, because it seems
593 far too likely that something will break. */
596 input_section->output_section->vma + input_section->output_offset;
598 if (howto->pcrel_offset == true)
599 relocation -= reloc_entry->address;
602 if (output_bfd != (bfd *) NULL)
604 if (howto->partial_inplace == false)
606 /* This is a partial relocation, and we want to apply the relocation
607 to the reloc entry rather than the raw data. Modify the reloc
608 inplace to reflect what we now know. */
609 reloc_entry->addend = relocation;
610 reloc_entry->address += input_section->output_offset;
615 /* This is a partial relocation, but inplace, so modify the
618 If we've relocated with a symbol with a section, change
619 into a ref to the section belonging to the symbol. */
621 reloc_entry->address += input_section->output_offset;
624 if (abfd->xvec->flavour == bfd_target_coff_flavour
625 && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
626 && strcmp (abfd->xvec->name, "xcoff-powermac") != 0
627 && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
628 && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
631 /* For m68k-coff, the addend was being subtracted twice during
632 relocation with -r. Removing the line below this comment
633 fixes that problem; see PR 2953.
635 However, Ian wrote the following, regarding removing the line below,
636 which explains why it is still enabled: --djm
638 If you put a patch like that into BFD you need to check all the COFF
639 linkers. I am fairly certain that patch will break coff-i386 (e.g.,
640 SCO); see coff_i386_reloc in coff-i386.c where I worked around the
641 problem in a different way. There may very well be a reason that the
642 code works as it does.
644 Hmmm. The first obvious point is that bfd_perform_relocation should
645 not have any tests that depend upon the flavour. It's seem like
646 entirely the wrong place for such a thing. The second obvious point
647 is that the current code ignores the reloc addend when producing
648 relocateable output for COFF. That's peculiar. In fact, I really
649 have no idea what the point of the line you want to remove is.
651 A typical COFF reloc subtracts the old value of the symbol and adds in
652 the new value to the location in the object file (if it's a pc
653 relative reloc it adds the difference between the symbol value and the
654 location). When relocating we need to preserve that property.
656 BFD handles this by setting the addend to the negative of the old
657 value of the symbol. Unfortunately it handles common symbols in a
658 non-standard way (it doesn't subtract the old value) but that's a
659 different story (we can't change it without losing backward
660 compatibility with old object files) (coff-i386 does subtract the old
661 value, to be compatible with existing coff-i386 targets, like SCO).
663 So everything works fine when not producing relocateable output. When
664 we are producing relocateable output, logically we should do exactly
665 what we do when not producing relocateable output. Therefore, your
666 patch is correct. In fact, it should probably always just set
667 reloc_entry->addend to 0 for all cases, since it is, in fact, going to
668 add the value into the object file. This won't hurt the COFF code,
669 which doesn't use the addend; I'm not sure what it will do to other
670 formats (the thing to check for would be whether any formats both use
671 the addend and set partial_inplace).
673 When I wanted to make coff-i386 produce relocateable output, I ran
674 into the problem that you are running into: I wanted to remove that
675 line. Rather than risk it, I made the coff-i386 relocs use a special
676 function; it's coff_i386_reloc in coff-i386.c. The function
677 specifically adds the addend field into the object file, knowing that
678 bfd_perform_relocation is not going to. If you remove that line, then
679 coff-i386.c will wind up adding the addend field in twice. It's
680 trivial to fix; it just needs to be done.
682 The problem with removing the line is just that it may break some
683 working code. With BFD it's hard to be sure of anything. The right
684 way to deal with this is simply to build and test at least all the
685 supported COFF targets. It should be straightforward if time and disk
686 space consuming. For each target:
688 2) generate some executable, and link it using -r (I would
689 probably use paranoia.o and link against newlib/libc.a, which
690 for all the supported targets would be available in
691 /usr/cygnus/progressive/H-host/target/lib/libc.a).
692 3) make the change to reloc.c
693 4) rebuild the linker
695 6) if the resulting object files are the same, you have at least
697 7) if they are different you have to figure out which version is
700 relocation -= reloc_entry->addend;
702 reloc_entry->addend = 0;
706 reloc_entry->addend = relocation;
712 reloc_entry->addend = 0;
715 /* FIXME: This overflow checking is incomplete, because the value
716 might have overflowed before we get here. For a correct check we
717 need to compute the value in a size larger than bitsize, but we
718 can't reasonably do that for a reloc the same size as a host
720 FIXME: We should also do overflow checking on the result after
721 adding in the value contained in the object file. */
722 if (howto->complain_on_overflow != complain_overflow_dont
723 && flag == bfd_reloc_ok)
727 /* Get the value that will be used for the relocation, but
728 starting at bit position zero. */
729 check = relocation >> howto->rightshift;
730 switch (howto->complain_on_overflow)
732 case complain_overflow_signed:
734 /* Assumes two's complement. */
735 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
736 bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
738 /* The above right shift is incorrect for a signed value.
739 Fix it up by forcing on the upper bits. */
740 if (howto->rightshift > 0
741 && (bfd_signed_vma) relocation < 0)
742 check |= ((bfd_vma) - 1
744 >> howto->rightshift));
745 if ((bfd_signed_vma) check > reloc_signed_max
746 || (bfd_signed_vma) check < reloc_signed_min)
747 flag = bfd_reloc_overflow;
750 case complain_overflow_unsigned:
752 /* Assumes two's complement. This expression avoids
753 overflow if howto->bitsize is the number of bits in
755 bfd_vma reloc_unsigned_max =
756 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
758 if ((bfd_vma) check > reloc_unsigned_max)
759 flag = bfd_reloc_overflow;
762 case complain_overflow_bitfield:
764 /* Assumes two's complement. This expression avoids
765 overflow if howto->bitsize is the number of bits in
767 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
769 if (((bfd_vma) check & ~reloc_bits) != 0
770 && ((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
772 /* The above right shift is incorrect for a signed
773 value. See if turning on the upper bits fixes the
775 if (howto->rightshift > 0
776 && (bfd_signed_vma) relocation < 0)
778 check |= ((bfd_vma) - 1
780 >> howto->rightshift));
781 if (((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
782 flag = bfd_reloc_overflow;
785 flag = bfd_reloc_overflow;
795 Either we are relocating all the way, or we don't want to apply
796 the relocation to the reloc entry (probably because there isn't
797 any room in the output format to describe addends to relocs)
800 /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
801 (OSF version 1.3, compiler version 3.11). It miscompiles the
815 x <<= (unsigned long) s.i0;
819 printf ("succeeded (%lx)\n", x);
823 relocation >>= (bfd_vma) howto->rightshift;
825 /* Shift everything up to where it's going to be used */
827 relocation <<= (bfd_vma) howto->bitpos;
829 /* Wait for the day when all have the mask in them */
832 i instruction to be left alone
833 o offset within instruction
834 r relocation offset to apply
843 i i i i i o o o o o from bfd_get<size>
844 and S S S S S to get the size offset we want
845 + r r r r r r r r r r to get the final value to place
846 and D D D D D to chop to right size
847 -----------------------
850 ... i i i i i o o o o o from bfd_get<size>
851 and N N N N N get instruction
852 -----------------------
858 -----------------------
859 R R R R R R R R R R put into bfd_put<size>
863 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
869 char x = bfd_get_8 (abfd, (char *) data + addr);
871 bfd_put_8 (abfd, x, (unsigned char *) data + addr);
877 short x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
879 bfd_put_16 (abfd, x, (unsigned char *) data + addr);
884 long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
886 bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
891 long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
892 relocation = -relocation;
894 bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
900 long x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
901 relocation = -relocation;
903 bfd_put_16 (abfd, x, (bfd_byte *) data + addr);
914 bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + addr);
916 bfd_put_64 (abfd, x, (bfd_byte *) data + addr);
923 return bfd_reloc_other;
931 bfd_install_relocation
934 bfd_reloc_status_type
935 bfd_install_relocation
937 arelent *reloc_entry,
938 PTR data, bfd_vma data_start,
939 asection *input_section,
940 char **error_message);
943 This looks remarkably like <<bfd_perform_relocation>>, except it
944 does not expect that the section contents have been filled in.
945 I.e., it's suitable for use when creating, rather than applying
948 For now, this function should be considered reserved for the
954 bfd_reloc_status_type
955 bfd_install_relocation (abfd, reloc_entry, data_start, data_start_offset,
956 input_section, error_message)
958 arelent *reloc_entry;
960 bfd_vma data_start_offset;
961 asection *input_section;
962 char **error_message;
965 bfd_reloc_status_type flag = bfd_reloc_ok;
966 bfd_size_type addr = reloc_entry->address;
967 bfd_vma output_base = 0;
968 reloc_howto_type *howto = reloc_entry->howto;
969 asection *reloc_target_output_section;
973 symbol = *(reloc_entry->sym_ptr_ptr);
974 if (bfd_is_abs_section (symbol->section))
976 reloc_entry->address += input_section->output_offset;
980 /* If there is a function supplied to handle this relocation type,
981 call it. It'll return `bfd_reloc_continue' if further processing
983 if (howto->special_function)
985 bfd_reloc_status_type cont;
987 /* XXX - The special_function calls haven't been fixed up to deal
988 with creating new relocations and section contents. */
989 cont = howto->special_function (abfd, reloc_entry, symbol,
990 /* XXX - Non-portable! */
991 ((bfd_byte *) data_start
992 - data_start_offset),
993 input_section, abfd, error_message);
994 if (cont != bfd_reloc_continue)
998 /* Is the address of the relocation really within the section? */
999 if (reloc_entry->address > input_section->_cooked_size)
1000 return bfd_reloc_outofrange;
1002 /* Work out which section the relocation is targetted at and the
1003 initial relocation command value. */
1005 /* Get symbol value. (Common symbols are special.) */
1006 if (bfd_is_com_section (symbol->section))
1009 relocation = symbol->value;
1011 reloc_target_output_section = symbol->section->output_section;
1013 /* Convert input-section-relative symbol value to absolute. */
1014 if (howto->partial_inplace == false)
1017 output_base = reloc_target_output_section->vma;
1019 relocation += output_base + symbol->section->output_offset;
1021 /* Add in supplied addend. */
1022 relocation += reloc_entry->addend;
1024 /* Here the variable relocation holds the final address of the
1025 symbol we are relocating against, plus any addend. */
1027 if (howto->pc_relative == true)
1029 /* This is a PC relative relocation. We want to set RELOCATION
1030 to the distance between the address of the symbol and the
1031 location. RELOCATION is already the address of the symbol.
1033 We start by subtracting the address of the section containing
1036 If pcrel_offset is set, we must further subtract the position
1037 of the location within the section. Some targets arrange for
1038 the addend to be the negative of the position of the location
1039 within the section; for example, i386-aout does this. For
1040 i386-aout, pcrel_offset is false. Some other targets do not
1041 include the position of the location; for example, m88kbcs,
1042 or ELF. For those targets, pcrel_offset is true.
1044 If we are producing relocateable output, then we must ensure
1045 that this reloc will be correctly computed when the final
1046 relocation is done. If pcrel_offset is false we want to wind
1047 up with the negative of the location within the section,
1048 which means we must adjust the existing addend by the change
1049 in the location within the section. If pcrel_offset is true
1050 we do not want to adjust the existing addend at all.
1052 FIXME: This seems logical to me, but for the case of
1053 producing relocateable output it is not what the code
1054 actually does. I don't want to change it, because it seems
1055 far too likely that something will break. */
1058 input_section->output_section->vma + input_section->output_offset;
1060 if (howto->pcrel_offset == true && howto->partial_inplace == true)
1061 relocation -= reloc_entry->address;
1064 if (howto->partial_inplace == false)
1066 /* This is a partial relocation, and we want to apply the relocation
1067 to the reloc entry rather than the raw data. Modify the reloc
1068 inplace to reflect what we now know. */
1069 reloc_entry->addend = relocation;
1070 reloc_entry->address += input_section->output_offset;
1075 /* This is a partial relocation, but inplace, so modify the
1078 If we've relocated with a symbol with a section, change
1079 into a ref to the section belonging to the symbol. */
1081 reloc_entry->address += input_section->output_offset;
1084 if (abfd->xvec->flavour == bfd_target_coff_flavour
1085 && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
1086 && strcmp (abfd->xvec->name, "xcoff-powermac") != 0
1087 && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
1088 && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
1091 /* For m68k-coff, the addend was being subtracted twice during
1092 relocation with -r. Removing the line below this comment
1093 fixes that problem; see PR 2953.
1095 However, Ian wrote the following, regarding removing the line below,
1096 which explains why it is still enabled: --djm
1098 If you put a patch like that into BFD you need to check all the COFF
1099 linkers. I am fairly certain that patch will break coff-i386 (e.g.,
1100 SCO); see coff_i386_reloc in coff-i386.c where I worked around the
1101 problem in a different way. There may very well be a reason that the
1102 code works as it does.
1104 Hmmm. The first obvious point is that bfd_install_relocation should
1105 not have any tests that depend upon the flavour. It's seem like
1106 entirely the wrong place for such a thing. The second obvious point
1107 is that the current code ignores the reloc addend when producing
1108 relocateable output for COFF. That's peculiar. In fact, I really
1109 have no idea what the point of the line you want to remove is.
1111 A typical COFF reloc subtracts the old value of the symbol and adds in
1112 the new value to the location in the object file (if it's a pc
1113 relative reloc it adds the difference between the symbol value and the
1114 location). When relocating we need to preserve that property.
1116 BFD handles this by setting the addend to the negative of the old
1117 value of the symbol. Unfortunately it handles common symbols in a
1118 non-standard way (it doesn't subtract the old value) but that's a
1119 different story (we can't change it without losing backward
1120 compatibility with old object files) (coff-i386 does subtract the old
1121 value, to be compatible with existing coff-i386 targets, like SCO).
1123 So everything works fine when not producing relocateable output. When
1124 we are producing relocateable output, logically we should do exactly
1125 what we do when not producing relocateable output. Therefore, your
1126 patch is correct. In fact, it should probably always just set
1127 reloc_entry->addend to 0 for all cases, since it is, in fact, going to
1128 add the value into the object file. This won't hurt the COFF code,
1129 which doesn't use the addend; I'm not sure what it will do to other
1130 formats (the thing to check for would be whether any formats both use
1131 the addend and set partial_inplace).
1133 When I wanted to make coff-i386 produce relocateable output, I ran
1134 into the problem that you are running into: I wanted to remove that
1135 line. Rather than risk it, I made the coff-i386 relocs use a special
1136 function; it's coff_i386_reloc in coff-i386.c. The function
1137 specifically adds the addend field into the object file, knowing that
1138 bfd_install_relocation is not going to. If you remove that line, then
1139 coff-i386.c will wind up adding the addend field in twice. It's
1140 trivial to fix; it just needs to be done.
1142 The problem with removing the line is just that it may break some
1143 working code. With BFD it's hard to be sure of anything. The right
1144 way to deal with this is simply to build and test at least all the
1145 supported COFF targets. It should be straightforward if time and disk
1146 space consuming. For each target:
1148 2) generate some executable, and link it using -r (I would
1149 probably use paranoia.o and link against newlib/libc.a, which
1150 for all the supported targets would be available in
1151 /usr/cygnus/progressive/H-host/target/lib/libc.a).
1152 3) make the change to reloc.c
1153 4) rebuild the linker
1155 6) if the resulting object files are the same, you have at least
1157 7) if they are different you have to figure out which version is
1160 relocation -= reloc_entry->addend;
1162 reloc_entry->addend = 0;
1166 reloc_entry->addend = relocation;
1170 /* FIXME: This overflow checking is incomplete, because the value
1171 might have overflowed before we get here. For a correct check we
1172 need to compute the value in a size larger than bitsize, but we
1173 can't reasonably do that for a reloc the same size as a host
1176 FIXME: We should also do overflow checking on the result after
1177 adding in the value contained in the object file. */
1178 if (howto->complain_on_overflow != complain_overflow_dont)
1182 /* Get the value that will be used for the relocation, but
1183 starting at bit position zero. */
1184 check = relocation >> howto->rightshift;
1185 switch (howto->complain_on_overflow)
1187 case complain_overflow_signed:
1189 /* Assumes two's complement. */
1190 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
1191 bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
1193 /* The above right shift is incorrect for a signed value.
1194 Fix it up by forcing on the upper bits. */
1195 if (howto->rightshift > 0
1196 && (bfd_signed_vma) relocation < 0)
1197 check |= ((bfd_vma) - 1
1199 >> howto->rightshift));
1200 if ((bfd_signed_vma) check > reloc_signed_max
1201 || (bfd_signed_vma) check < reloc_signed_min)
1202 flag = bfd_reloc_overflow;
1205 case complain_overflow_unsigned:
1207 /* Assumes two's complement. This expression avoids
1208 overflow if howto->bitsize is the number of bits in
1210 bfd_vma reloc_unsigned_max =
1211 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1213 if ((bfd_vma) check > reloc_unsigned_max)
1214 flag = bfd_reloc_overflow;
1217 case complain_overflow_bitfield:
1219 /* Assumes two's complement. This expression avoids
1220 overflow if howto->bitsize is the number of bits in
1222 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1224 if (((bfd_vma) check & ~reloc_bits) != 0
1225 && ((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
1227 /* The above right shift is incorrect for a signed
1228 value. See if turning on the upper bits fixes the
1230 if (howto->rightshift > 0
1231 && (bfd_signed_vma) relocation < 0)
1233 check |= ((bfd_vma) - 1
1235 >> howto->rightshift));
1236 if (((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
1237 flag = bfd_reloc_overflow;
1240 flag = bfd_reloc_overflow;
1250 Either we are relocating all the way, or we don't want to apply
1251 the relocation to the reloc entry (probably because there isn't
1252 any room in the output format to describe addends to relocs)
1255 /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
1256 (OSF version 1.3, compiler version 3.11). It miscompiles the
1270 x <<= (unsigned long) s.i0;
1272 printf ("failed\n");
1274 printf ("succeeded (%lx)\n", x);
1278 relocation >>= (bfd_vma) howto->rightshift;
1280 /* Shift everything up to where it's going to be used */
1282 relocation <<= (bfd_vma) howto->bitpos;
1284 /* Wait for the day when all have the mask in them */
1287 i instruction to be left alone
1288 o offset within instruction
1289 r relocation offset to apply
1298 i i i i i o o o o o from bfd_get<size>
1299 and S S S S S to get the size offset we want
1300 + r r r r r r r r r r to get the final value to place
1301 and D D D D D to chop to right size
1302 -----------------------
1305 ... i i i i i o o o o o from bfd_get<size>
1306 and N N N N N get instruction
1307 -----------------------
1313 -----------------------
1314 R R R R R R R R R R put into bfd_put<size>
1318 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
1320 data = (bfd_byte *) data_start + (addr - data_start_offset);
1322 switch (howto->size)
1326 char x = bfd_get_8 (abfd, (char *) data);
1328 bfd_put_8 (abfd, x, (unsigned char *) data);
1334 short x = bfd_get_16 (abfd, (bfd_byte *) data);
1336 bfd_put_16 (abfd, x, (unsigned char *) data);
1341 long x = bfd_get_32 (abfd, (bfd_byte *) data);
1343 bfd_put_32 (abfd, x, (bfd_byte *) data);
1348 long x = bfd_get_32 (abfd, (bfd_byte *) data);
1349 relocation = -relocation;
1351 bfd_put_32 (abfd, x, (bfd_byte *) data);
1361 bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data);
1363 bfd_put_64 (abfd, x, (bfd_byte *) data);
1367 return bfd_reloc_other;
1373 /* This relocation routine is used by some of the backend linkers.
1374 They do not construct asymbol or arelent structures, so there is no
1375 reason for them to use bfd_perform_relocation. Also,
1376 bfd_perform_relocation is so hacked up it is easier to write a new
1377 function than to try to deal with it.
1379 This routine does a final relocation. It should not be used when
1380 generating relocateable output.
1382 FIXME: This routine ignores any special_function in the HOWTO,
1383 since the existing special_function values have been written for
1384 bfd_perform_relocation.
1386 HOWTO is the reloc howto information.
1387 INPUT_BFD is the BFD which the reloc applies to.
1388 INPUT_SECTION is the section which the reloc applies to.
1389 CONTENTS is the contents of the section.
1390 ADDRESS is the address of the reloc within INPUT_SECTION.
1391 VALUE is the value of the symbol the reloc refers to.
1392 ADDEND is the addend of the reloc. */
1394 bfd_reloc_status_type
1395 _bfd_final_link_relocate (howto, input_bfd, input_section, contents, address,
1397 reloc_howto_type *howto;
1399 asection *input_section;
1407 /* Sanity check the address. */
1408 if (address > input_section->_raw_size)
1409 return bfd_reloc_outofrange;
1411 /* This function assumes that we are dealing with a basic relocation
1412 against a symbol. We want to compute the value of the symbol to
1413 relocate to. This is just VALUE, the value of the symbol, plus
1414 ADDEND, any addend associated with the reloc. */
1415 relocation = value + addend;
1417 /* If the relocation is PC relative, we want to set RELOCATION to
1418 the distance between the symbol (currently in RELOCATION) and the
1419 location we are relocating. Some targets (e.g., i386-aout)
1420 arrange for the contents of the section to be the negative of the
1421 offset of the location within the section; for such targets
1422 pcrel_offset is false. Other targets (e.g., m88kbcs or ELF)
1423 simply leave the contents of the section as zero; for such
1424 targets pcrel_offset is true. If pcrel_offset is false we do not
1425 need to subtract out the offset of the location within the
1426 section (which is just ADDRESS). */
1427 if (howto->pc_relative)
1429 relocation -= (input_section->output_section->vma
1430 + input_section->output_offset);
1431 if (howto->pcrel_offset)
1432 relocation -= address;
1435 return _bfd_relocate_contents (howto, input_bfd, relocation,
1436 contents + address);
1439 /* Relocate a given location using a given value and howto. */
1441 bfd_reloc_status_type
1442 _bfd_relocate_contents (howto, input_bfd, relocation, location)
1443 reloc_howto_type *howto;
1452 /* If the size is negative, negate RELOCATION. This isn't very
1454 if (howto->size < 0)
1455 relocation = -relocation;
1457 /* Get the value we are going to relocate. */
1458 size = bfd_get_reloc_size (howto);
1465 x = bfd_get_8 (input_bfd, location);
1468 x = bfd_get_16 (input_bfd, location);
1471 x = bfd_get_32 (input_bfd, location);
1475 x = bfd_get_64 (input_bfd, location);
1482 /* Check for overflow. FIXME: We may drop bits during the addition
1483 which we don't check for. We must either check at every single
1484 operation, which would be tedious, or we must do the computations
1485 in a type larger than bfd_vma, which would be inefficient. */
1487 if (howto->complain_on_overflow != complain_overflow_dont)
1490 bfd_signed_vma signed_check;
1492 bfd_signed_vma signed_add;
1494 if (howto->rightshift == 0)
1497 signed_check = (bfd_signed_vma) relocation;
1501 /* Drop unwanted bits from the value we are relocating to. */
1502 check = relocation >> howto->rightshift;
1504 /* If this is a signed value, the rightshift just dropped
1505 leading 1 bits (assuming twos complement). */
1506 if ((bfd_signed_vma) relocation >= 0)
1507 signed_check = check;
1509 signed_check = (check
1511 & ~((bfd_vma) - 1 >> howto->rightshift)));
1514 /* Get the value from the object file. */
1515 add = x & howto->src_mask;
1517 /* Get the value from the object file with an appropriate sign.
1518 The expression involving howto->src_mask isolates the upper
1519 bit of src_mask. If that bit is set in the value we are
1520 adding, it is negative, and we subtract out that number times
1521 two. If src_mask includes the highest possible bit, then we
1522 can not get the upper bit, but that does not matter since
1523 signed_add needs no adjustment to become negative in that
1526 if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
1527 signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
1529 /* Add the value from the object file, shifted so that it is a
1531 if (howto->bitpos == 0)
1534 signed_check += signed_add;
1538 check += add >> howto->bitpos;
1540 /* For the signed case we use ADD, rather than SIGNED_ADD,
1541 to avoid warnings from SVR4 cc. This is OK since we
1542 explictly handle the sign bits. */
1543 if (signed_add >= 0)
1544 signed_check += add >> howto->bitpos;
1546 signed_check += ((add >> howto->bitpos)
1548 & ~((bfd_vma) - 1 >> howto->bitpos)));
1551 switch (howto->complain_on_overflow)
1553 case complain_overflow_signed:
1555 /* Assumes two's complement. */
1556 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
1557 bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
1559 if (signed_check > reloc_signed_max
1560 || signed_check < reloc_signed_min)
1564 case complain_overflow_unsigned:
1566 /* Assumes two's complement. This expression avoids
1567 overflow if howto->bitsize is the number of bits in
1569 bfd_vma reloc_unsigned_max =
1570 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1572 if (check > reloc_unsigned_max)
1576 case complain_overflow_bitfield:
1578 /* Assumes two's complement. This expression avoids
1579 overflow if howto->bitsize is the number of bits in
1581 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1583 if ((check & ~reloc_bits) != 0
1584 && (((bfd_vma) signed_check & ~reloc_bits)
1585 != (-1 & ~reloc_bits)))
1594 /* Put RELOCATION in the right bits. */
1595 relocation >>= (bfd_vma) howto->rightshift;
1596 relocation <<= (bfd_vma) howto->bitpos;
1598 /* Add RELOCATION to the right bits of X. */
1599 x = ((x & ~howto->dst_mask)
1600 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
1602 /* Put the relocated value back in the object file. */
1609 bfd_put_8 (input_bfd, x, location);
1612 bfd_put_16 (input_bfd, x, location);
1615 bfd_put_32 (input_bfd, x, location);
1619 bfd_put_64 (input_bfd, x, location);
1626 return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
1632 howto manager, , typedef arelent, Relocations
1637 When an application wants to create a relocation, but doesn't
1638 know what the target machine might call it, it can find out by
1639 using this bit of code.
1648 The insides of a reloc code. The idea is that, eventually, there
1649 will be one enumerator for every type of relocation we ever do.
1650 Pass one of these values to <<bfd_reloc_type_lookup>>, and it'll
1651 return a howto pointer.
1653 This does mean that the application must determine the correct
1654 enumerator value; you can't get a howto pointer from a random set
1675 Basic absolute relocations of N bits.
1690 PC-relative relocations. Sometimes these are relative to the address
1691 of the relocation itself; sometimes they are relative to the start of
1692 the section containing the relocation. It depends on the specific target.
1694 The 24-bit relocation is used in some Intel 960 configurations.
1697 BFD_RELOC_32_GOT_PCREL
1699 BFD_RELOC_16_GOT_PCREL
1701 BFD_RELOC_8_GOT_PCREL
1707 BFD_RELOC_LO16_GOTOFF
1709 BFD_RELOC_HI16_GOTOFF
1711 BFD_RELOC_HI16_S_GOTOFF
1715 BFD_RELOC_32_PLT_PCREL
1717 BFD_RELOC_24_PLT_PCREL
1719 BFD_RELOC_16_PLT_PCREL
1721 BFD_RELOC_8_PLT_PCREL
1727 BFD_RELOC_LO16_PLTOFF
1729 BFD_RELOC_HI16_PLTOFF
1731 BFD_RELOC_HI16_S_PLTOFF
1738 BFD_RELOC_68K_GLOB_DAT
1740 BFD_RELOC_68K_JMP_SLOT
1742 BFD_RELOC_68K_RELATIVE
1744 Relocations used by 68K ELF.
1747 BFD_RELOC_32_BASEREL
1749 BFD_RELOC_16_BASEREL
1751 BFD_RELOC_LO16_BASEREL
1753 BFD_RELOC_HI16_BASEREL
1755 BFD_RELOC_HI16_S_BASEREL
1761 Linkage-table relative.
1766 Absolute 8-bit relocation, but used to form an address like 0xFFnn.
1769 BFD_RELOC_32_PCREL_S2
1771 BFD_RELOC_16_PCREL_S2
1773 BFD_RELOC_23_PCREL_S2
1775 These PC-relative relocations are stored as word displacements --
1776 i.e., byte displacements shifted right two bits. The 30-bit word
1777 displacement (<<32_PCREL_S2>> -- 32 bits, shifted 2) is used on the
1778 SPARC. (SPARC tools generally refer to this as <<WDISP30>>.) The
1779 signed 16-bit displacement is used on the MIPS, and the 23-bit
1780 displacement is used on the Alpha.
1787 High 22 bits and low 10 bits of 32-bit value, placed into lower bits of
1788 the target word. These are used on the SPARC.
1795 For systems that allocate a Global Pointer register, these are
1796 displacements off that register. These relocation types are
1797 handled specially, because the value the register will have is
1798 decided relatively late.
1802 BFD_RELOC_I960_CALLJ
1804 Reloc types used for i960/b.out.
1809 BFD_RELOC_SPARC_WDISP22
1815 BFD_RELOC_SPARC_GOT10
1817 BFD_RELOC_SPARC_GOT13
1819 BFD_RELOC_SPARC_GOT22
1821 BFD_RELOC_SPARC_PC10
1823 BFD_RELOC_SPARC_PC22
1825 BFD_RELOC_SPARC_WPLT30
1827 BFD_RELOC_SPARC_COPY
1829 BFD_RELOC_SPARC_GLOB_DAT
1831 BFD_RELOC_SPARC_JMP_SLOT
1833 BFD_RELOC_SPARC_RELATIVE
1835 BFD_RELOC_SPARC_UA32
1837 SPARC ELF relocations. There is probably some overlap with other
1838 relocation types already defined.
1841 BFD_RELOC_SPARC_BASE13
1843 BFD_RELOC_SPARC_BASE22
1845 I think these are specific to SPARC a.out (e.g., Sun 4).
1855 BFD_RELOC_SPARC_OLO10
1857 BFD_RELOC_SPARC_HH22
1859 BFD_RELOC_SPARC_HM10
1861 BFD_RELOC_SPARC_LM22
1863 BFD_RELOC_SPARC_PC_HH22
1865 BFD_RELOC_SPARC_PC_HM10
1867 BFD_RELOC_SPARC_PC_LM22
1869 BFD_RELOC_SPARC_WDISP16
1871 BFD_RELOC_SPARC_WDISP19
1873 BFD_RELOC_SPARC_GLOB_JMP
1881 Some relocations we're using for SPARC V9 -- subject to change.
1884 BFD_RELOC_ALPHA_GPDISP_HI16
1886 Alpha ECOFF and ELF relocations. Some of these treat the symbol or
1887 "addend" in some special way.
1888 For GPDISP_HI16 ("gpdisp") relocations, the symbol is ignored when
1889 writing; when reading, it will be the absolute section symbol. The
1890 addend is the displacement in bytes of the "lda" instruction from
1891 the "ldah" instruction (which is at the address of this reloc).
1893 BFD_RELOC_ALPHA_GPDISP_LO16
1895 For GPDISP_LO16 ("ignore") relocations, the symbol is handled as
1896 with GPDISP_HI16 relocs. The addend is ignored when writing the
1897 relocations out, and is filled in with the file's GP value on
1898 reading, for convenience.
1901 BFD_RELOC_ALPHA_GPDISP
1903 The ELF GPDISP relocation is exactly the same as the GPDISP_HI16
1904 relocation except that there is no accompanying GPDISP_LO16
1908 BFD_RELOC_ALPHA_LITERAL
1910 BFD_RELOC_ALPHA_ELF_LITERAL
1912 BFD_RELOC_ALPHA_LITUSE
1914 The Alpha LITERAL/LITUSE relocs are produced by a symbol reference;
1915 the assembler turns it into a LDQ instruction to load the address of
1916 the symbol, and then fills in a register in the real instruction.
1918 The LITERAL reloc, at the LDQ instruction, refers to the .lita
1919 section symbol. The addend is ignored when writing, but is filled
1920 in with the file's GP value on reading, for convenience, as with the
1923 The ELF_LITERAL reloc is somewhere between 16_GOTOFF and GPDISP_LO16.
1924 It should refer to the symbol to be referenced, as with 16_GOTOFF,
1925 but it generates output not based on the position within the .got
1926 section, but relative to the GP value chosen for the file during the
1929 The LITUSE reloc, on the instruction using the loaded address, gives
1930 information to the linker that it might be able to use to optimize
1931 away some literal section references. The symbol is ignored (read
1932 as the absolute section symbol), and the "addend" indicates the type
1933 of instruction using the register:
1934 1 - "memory" fmt insn
1935 2 - byte-manipulation (byte offset reg)
1936 3 - jsr (target of branch)
1938 The GNU linker currently doesn't do any of this optimizing.
1941 BFD_RELOC_ALPHA_HINT
1943 The HINT relocation indicates a value that should be filled into the
1944 "hint" field of a jmp/jsr/ret instruction, for possible branch-
1945 prediction logic which may be provided on some processors.
1948 BFD_RELOC_ALPHA_LINKAGE
1950 The LINKAGE relocation outputs a linkage pair in the object file,
1951 which is filled by the linker.
1954 BFD_RELOC_ALPHA_CODEADDR
1956 The CODEADDR relocation outputs a STO_CA in the object file,
1957 which is filled by the linker.
1962 Bits 27..2 of the relocation address shifted right 2 bits;
1963 simple reloc otherwise.
1966 BFD_RELOC_MIPS16_JMP
1968 The MIPS16 jump instruction.
1971 BFD_RELOC_MIPS16_GPREL
1973 MIPS16 GP relative reloc.
1978 High 16 bits of 32-bit value; simple reloc.
1982 High 16 bits of 32-bit value but the low 16 bits will be sign
1983 extended and added to form the final result. If the low 16
1984 bits form a negative number, we need to add one to the high value
1985 to compensate for the borrow when the low bits are added.
1991 BFD_RELOC_PCREL_HI16_S
1993 Like BFD_RELOC_HI16_S, but PC relative.
1995 BFD_RELOC_PCREL_LO16
1997 Like BFD_RELOC_LO16, but PC relative.
2000 BFD_RELOC_MIPS_GPREL
2003 Relocation relative to the global pointer.
2006 BFD_RELOC_MIPS_LITERAL
2008 Relocation against a MIPS literal section.
2011 BFD_RELOC_MIPS_GOT16
2013 BFD_RELOC_MIPS_CALL16
2015 BFD_RELOC_MIPS_GPREL32
2018 BFD_RELOC_MIPS_GOT_HI16
2020 BFD_RELOC_MIPS_GOT_LO16
2022 BFD_RELOC_MIPS_CALL_HI16
2024 BFD_RELOC_MIPS_CALL_LO16
2026 MIPS ELF relocations.
2035 BFD_RELOC_386_GLOB_DAT
2037 BFD_RELOC_386_JUMP_SLOT
2039 BFD_RELOC_386_RELATIVE
2041 BFD_RELOC_386_GOTOFF
2045 i386/elf relocations
2048 BFD_RELOC_NS32K_IMM_8
2050 BFD_RELOC_NS32K_IMM_16
2052 BFD_RELOC_NS32K_IMM_32
2054 BFD_RELOC_NS32K_IMM_8_PCREL
2056 BFD_RELOC_NS32K_IMM_16_PCREL
2058 BFD_RELOC_NS32K_IMM_32_PCREL
2060 BFD_RELOC_NS32K_DISP_8
2062 BFD_RELOC_NS32K_DISP_16
2064 BFD_RELOC_NS32K_DISP_32
2066 BFD_RELOC_NS32K_DISP_8_PCREL
2068 BFD_RELOC_NS32K_DISP_16_PCREL
2070 BFD_RELOC_NS32K_DISP_32_PCREL
2083 BFD_RELOC_PPC_B16_BRTAKEN
2085 BFD_RELOC_PPC_B16_BRNTAKEN
2089 BFD_RELOC_PPC_BA16_BRTAKEN
2091 BFD_RELOC_PPC_BA16_BRNTAKEN
2095 BFD_RELOC_PPC_GLOB_DAT
2097 BFD_RELOC_PPC_JMP_SLOT
2099 BFD_RELOC_PPC_RELATIVE
2101 BFD_RELOC_PPC_LOCAL24PC
2103 BFD_RELOC_PPC_EMB_NADDR32
2105 BFD_RELOC_PPC_EMB_NADDR16
2107 BFD_RELOC_PPC_EMB_NADDR16_LO
2109 BFD_RELOC_PPC_EMB_NADDR16_HI
2111 BFD_RELOC_PPC_EMB_NADDR16_HA
2113 BFD_RELOC_PPC_EMB_SDAI16
2115 BFD_RELOC_PPC_EMB_SDA2I16
2117 BFD_RELOC_PPC_EMB_SDA2REL
2119 BFD_RELOC_PPC_EMB_SDA21
2121 BFD_RELOC_PPC_EMB_MRKREF
2123 BFD_RELOC_PPC_EMB_RELSEC16
2125 BFD_RELOC_PPC_EMB_RELST_LO
2127 BFD_RELOC_PPC_EMB_RELST_HI
2129 BFD_RELOC_PPC_EMB_RELST_HA
2131 BFD_RELOC_PPC_EMB_BIT_FLD
2133 BFD_RELOC_PPC_EMB_RELSDA
2135 Power(rs6000) and PowerPC relocations.
2140 The type of reloc used to build a contructor table - at the moment
2141 probably a 32 bit wide absolute relocation, but the target can choose.
2142 It generally does map to one of the other relocation types.
2145 BFD_RELOC_ARM_PCREL_BRANCH
2147 ARM 26 bit pc-relative branch. The lowest two bits must be zero and are
2148 not stored in the instruction.
2150 BFD_RELOC_ARM_IMMEDIATE
2152 BFD_RELOC_ARM_OFFSET_IMM
2154 BFD_RELOC_ARM_SHIFT_IMM
2160 BFD_RELOC_ARM_CP_OFF_IMM
2162 BFD_RELOC_ARM_ADR_IMM
2164 BFD_RELOC_ARM_LDR_IMM
2166 BFD_RELOC_ARM_LITERAL
2168 BFD_RELOC_ARM_IN_POOL
2170 BFD_RELOC_ARM_OFFSET_IMM8
2172 BFD_RELOC_ARM_HWLITERAL
2174 BFD_RELOC_ARM_THUMB_ADD
2176 BFD_RELOC_ARM_THUMB_IMM
2178 BFD_RELOC_ARM_THUMB_SHIFT
2180 BFD_RELOC_ARM_THUMB_OFFSET
2182 These relocs are only used within the ARM assembler. They are not
2183 (at present) written to any object files.
2186 BFD_RELOC_SH_PCDISP8BY2
2188 BFD_RELOC_SH_PCDISP12BY2
2192 BFD_RELOC_SH_IMM4BY2
2194 BFD_RELOC_SH_IMM4BY4
2198 BFD_RELOC_SH_IMM8BY2
2200 BFD_RELOC_SH_IMM8BY4
2202 BFD_RELOC_SH_PCRELIMM8BY2
2204 BFD_RELOC_SH_PCRELIMM8BY4
2206 BFD_RELOC_SH_SWITCH16
2208 BFD_RELOC_SH_SWITCH32
2222 Hitachi SH relocs. Not all of these appear in object files.
2225 BFD_RELOC_THUMB_PCREL_BRANCH9
2227 BFD_RELOC_THUMB_PCREL_BRANCH12
2229 BFD_RELOC_THUMB_PCREL_BRANCH23
2231 Thumb 23-, 12- and 9-bit pc-relative branches. The lowest bit must
2232 be zero and is not stored in the instruction.
2235 BFD_RELOC_ARC_B22_PCREL
2237 Argonaut RISC Core (ARC) relocs.
2238 ARC 22 bit pc-relative branch. The lowest two bits must be zero and are
2239 not stored in the instruction. The high 20 bits are installed in bits 26
2240 through 7 of the instruction.
2244 ARC 26 bit absolute branch. The lowest two bits must be zero and are not
2245 stored in the instruction. The high 24 bits are installed in bits 23
2250 BFD_RELOC_D10V_10_PCREL_R
2252 Mitsubishi D10V relocs.
2253 This is a 10-bit reloc with the right 2 bits
2256 BFD_RELOC_D10V_10_PCREL_L
2258 Mitsubishi D10V relocs.
2259 This is a 10-bit reloc with the right 2 bits
2260 assumed to be 0. This is the same as the previous reloc
2261 except it is in the left container, i.e.,
2262 shifted left 15 bits.
2266 This is an 18-bit reloc with the right 2 bits
2269 BFD_RELOC_D10V_18_PCREL
2271 This is an 18-bit reloc with the right 2 bits
2276 {* start-sanitize-d30v *}
2280 Mitsubishi D30V relocs.
2281 This is a 6-bit absolute reloc.
2283 BFD_RELOC_D30V_9_PCREL
2285 This is a 6-bit pc-relative reloc with
2286 the right 3 bits assumed to be 0.
2288 BFD_RELOC_D30V_9_PCREL_R
2290 This is a 6-bit pc-relative reloc with
2291 the right 3 bits assumed to be 0. Same
2292 as the previous reloc but on the right side
2297 This is a 12-bit absolute reloc with the
2298 right 3 bitsassumed to be 0.
2300 BFD_RELOC_D30V_15_PCREL
2302 This is a 12-bit pc-relative reloc with
2303 the right 3 bits assumed to be 0.
2305 BFD_RELOC_D30V_15_PCREL_R
2307 This is a 12-bit pc-relative reloc with
2308 the right 3 bits assumed to be 0. Same
2309 as the previous reloc but on the right side
2314 This is an 18-bit absolute reloc with
2315 the right 3 bits assumed to be 0.
2317 BFD_RELOC_D30V_21_PCREL
2319 This is an 18-bit pc-relative reloc with
2320 the right 3 bits assumed to be 0.
2322 BFD_RELOC_D30V_21_PCREL_R
2324 This is an 18-bit pc-relative reloc with
2325 the right 3 bits assumed to be 0. Same
2326 as the previous reloc but on the right side
2331 This is a 32-bit absolute reloc.
2333 BFD_RELOC_D30V_32_PCREL
2335 This is a 32-bit pc-relative reloc.
2337 {* end-sanitize-d30v *}
2342 Mitsubishi M32R relocs.
2343 This is a 24 bit absolute address.
2345 BFD_RELOC_M32R_10_PCREL
2347 This is a 10-bit pc-relative reloc with the right 2 bits assumed to be 0.
2349 BFD_RELOC_M32R_18_PCREL
2351 This is an 18-bit reloc with the right 2 bits assumed to be 0.
2353 BFD_RELOC_M32R_26_PCREL
2355 This is a 26-bit reloc with the right 2 bits assumed to be 0.
2357 BFD_RELOC_M32R_HI16_ULO
2359 This is a 16-bit reloc containing the high 16 bits of an address
2360 used when the lower 16 bits are treated as unsigned.
2362 BFD_RELOC_M32R_HI16_SLO
2364 This is a 16-bit reloc containing the high 16 bits of an address
2365 used when the lower 16 bits are treated as signed.
2369 This is a 16-bit reloc containing the lower 16 bits of an address.
2371 BFD_RELOC_M32R_SDA16
2373 This is a 16-bit reloc containing the small data area offset for use in
2374 add3, load, and store instructions.
2377 BFD_RELOC_V850_9_PCREL
2379 This is a 9-bit reloc
2381 BFD_RELOC_V850_22_PCREL
2383 This is a 22-bit reloc
2386 BFD_RELOC_V850_SDA_16_16_OFFSET
2388 This is a 16 bit offset from the short data area pointer.
2390 BFD_RELOC_V850_SDA_15_16_OFFSET
2392 This is a 16 bit offset (of which only 15 bits are used) from the
2393 short data area pointer.
2395 BFD_RELOC_V850_ZDA_16_16_OFFSET
2397 This is a 16 bit offset from the zero data area pointer.
2399 BFD_RELOC_V850_ZDA_15_16_OFFSET
2401 This is a 16 bit offset (of which only 15 bits are used) from the
2402 zero data area pointer.
2404 BFD_RELOC_V850_TDA_6_8_OFFSET
2406 This is an 8 bit offset (of which only 6 bits are used) from the
2407 tiny data area pointer.
2409 BFD_RELOC_V850_TDA_7_8_OFFSET
2411 This is an 8bit offset (of which only 7 bits are used) from the tiny
2414 BFD_RELOC_V850_TDA_7_7_OFFSET
2416 This is a 7 bit offset from the tiny data area pointer.
2418 BFD_RELOC_V850_TDA_16_16_OFFSET
2420 This is a 16 bit offset from the tiny data area pointer.
2422 {* start-sanitize-v850e *}
2424 BFD_RELOC_V850_TDA_4_5_OFFSET
2426 This is a 5 bit offset (of which only 4 bits are used) from the tiny
2429 BFD_RELOC_V850_TDA_4_4_OFFSET
2431 This is a 4 bit offset from the tiny data area pointer.
2433 BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET
2435 This is a 16 bit offset from the short data area pointer, with the
2436 bits placed non-contigously in the instruction.
2438 BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET
2440 This is a 16 bit offset from the zero data area pointer, with the
2441 bits placed non-contigously in the instruction.
2443 {* end-sanitize-v850e *}
2446 BFD_RELOC_MN10300_32_PCREL
2448 This is a 32bit pcrel reloc for the mn10300, offset by two bytes in the
2451 BFD_RELOC_MN10300_16_PCREL
2453 This is a 16bit pcrel reloc for the mn10300, offset by two bytes in the
2459 .typedef enum bfd_reloc_code_real bfd_reloc_code_real_type;
2465 bfd_reloc_type_lookup
2469 bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
2472 Return a pointer to a howto structure which, when
2473 invoked, will perform the relocation @var{code} on data from the
2480 bfd_reloc_type_lookup (abfd, code)
2482 bfd_reloc_code_real_type code;
2484 return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
2487 static reloc_howto_type bfd_howto_32 =
2488 HOWTO (0, 00, 2, 32, false, 0, complain_overflow_bitfield, 0, "VRT32", false, 0xffffffff, 0xffffffff, true);
2493 bfd_default_reloc_type_lookup
2496 reloc_howto_type *bfd_default_reloc_type_lookup
2497 (bfd *abfd, bfd_reloc_code_real_type code);
2500 Provides a default relocation lookup routine for any architecture.
2506 bfd_default_reloc_type_lookup (abfd, code)
2508 bfd_reloc_code_real_type code;
2512 case BFD_RELOC_CTOR:
2513 /* The type of reloc used in a ctor, which will be as wide as the
2514 address - so either a 64, 32, or 16 bitter. */
2515 switch (bfd_get_arch_info (abfd)->bits_per_address)
2520 return &bfd_howto_32;
2529 return (reloc_howto_type *) NULL;
2534 bfd_get_reloc_code_name
2537 const char *bfd_get_reloc_code_name (bfd_reloc_code_real_type code);
2540 Provides a printable name for the supplied relocation code.
2541 Useful mainly for printing error messages.
2545 bfd_get_reloc_code_name (code)
2546 bfd_reloc_code_real_type code;
2548 if (code > BFD_RELOC_UNUSED)
2550 return bfd_reloc_code_real_names[(int)code];
2555 bfd_generic_relax_section
2558 boolean bfd_generic_relax_section
2561 struct bfd_link_info *,
2565 Provides default handling for relaxing for back ends which
2566 don't do relaxing -- i.e., does nothing.
2571 bfd_generic_relax_section (abfd, section, link_info, again)
2574 struct bfd_link_info *link_info;
2583 bfd_generic_get_relocated_section_contents
2587 bfd_generic_get_relocated_section_contents (bfd *abfd,
2588 struct bfd_link_info *link_info,
2589 struct bfd_link_order *link_order,
2591 boolean relocateable,
2595 Provides default handling of relocation effort for back ends
2596 which can't be bothered to do it efficiently.
2601 bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data,
2602 relocateable, symbols)
2604 struct bfd_link_info *link_info;
2605 struct bfd_link_order *link_order;
2607 boolean relocateable;
2610 /* Get enough memory to hold the stuff */
2611 bfd *input_bfd = link_order->u.indirect.section->owner;
2612 asection *input_section = link_order->u.indirect.section;
2614 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
2615 arelent **reloc_vector = NULL;
2621 reloc_vector = (arelent **) bfd_malloc ((size_t) reloc_size);
2622 if (reloc_vector == NULL && reloc_size != 0)
2625 /* read in the section */
2626 if (!bfd_get_section_contents (input_bfd,
2630 input_section->_raw_size))
2633 /* We're not relaxing the section, so just copy the size info */
2634 input_section->_cooked_size = input_section->_raw_size;
2635 input_section->reloc_done = true;
2637 reloc_count = bfd_canonicalize_reloc (input_bfd,
2641 if (reloc_count < 0)
2644 if (reloc_count > 0)
2647 for (parent = reloc_vector; *parent != (arelent *) NULL;
2650 char *error_message = (char *) NULL;
2651 bfd_reloc_status_type r =
2652 bfd_perform_relocation (input_bfd,
2656 relocateable ? abfd : (bfd *) NULL,
2661 asection *os = input_section->output_section;
2663 /* A partial link, so keep the relocs */
2664 os->orelocation[os->reloc_count] = *parent;
2668 if (r != bfd_reloc_ok)
2672 case bfd_reloc_undefined:
2673 if (!((*link_info->callbacks->undefined_symbol)
2674 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
2675 input_bfd, input_section, (*parent)->address)))
2678 case bfd_reloc_dangerous:
2679 BFD_ASSERT (error_message != (char *) NULL);
2680 if (!((*link_info->callbacks->reloc_dangerous)
2681 (link_info, error_message, input_bfd, input_section,
2682 (*parent)->address)))
2685 case bfd_reloc_overflow:
2686 if (!((*link_info->callbacks->reloc_overflow)
2687 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
2688 (*parent)->howto->name, (*parent)->addend,
2689 input_bfd, input_section, (*parent)->address)))
2692 case bfd_reloc_outofrange:
2701 if (reloc_vector != NULL)
2702 free (reloc_vector);
2706 if (reloc_vector != NULL)
2707 free (reloc_vector);