Thu Aug 8 12:21:56 1996 Klaus Kaempf <kkaempf@progis.de>
[binutils-gdb.git] / bfd / reloc.c
1 /* BFD support for handling relocation entries.
2 Copyright (C) 1990, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 /*
22 SECTION
23 Relocations
24
25 BFD maintains relocations in much the same way it maintains
26 symbols: they are left alone until required, then read in
27 en-mass and translated into an internal form. A common
28 routine <<bfd_perform_relocation>> acts upon the
29 canonical form to do the fixup.
30
31 Relocations are maintained on a per section basis,
32 while symbols are maintained on a per BFD basis.
33
34 All that a back end has to do to fit the BFD interface is to create
35 a <<struct reloc_cache_entry>> for each relocation
36 in a particular section, and fill in the right bits of the structures.
37
38 @menu
39 @* typedef arelent::
40 @* howto manager::
41 @end menu
42
43 */
44
45 /* DO compile in the reloc_code name table from libbfd.h. */
46 #define _BFD_MAKE_TABLE_bfd_reloc_code_real
47
48 #include "bfd.h"
49 #include "sysdep.h"
50 #include "bfdlink.h"
51 #include "libbfd.h"
52 /*
53 DOCDD
54 INODE
55 typedef arelent, howto manager, Relocations, Relocations
56
57 SUBSECTION
58 typedef arelent
59
60 This is the structure of a relocation entry:
61
62 CODE_FRAGMENT
63 .
64 .typedef enum bfd_reloc_status
65 .{
66 . {* No errors detected *}
67 . bfd_reloc_ok,
68 .
69 . {* The relocation was performed, but there was an overflow. *}
70 . bfd_reloc_overflow,
71 .
72 . {* The address to relocate was not within the section supplied. *}
73 . bfd_reloc_outofrange,
74 .
75 . {* Used by special functions *}
76 . bfd_reloc_continue,
77 .
78 . {* Unsupported relocation size requested. *}
79 . bfd_reloc_notsupported,
80 .
81 . {* Unused *}
82 . bfd_reloc_other,
83 .
84 . {* The symbol to relocate against was undefined. *}
85 . bfd_reloc_undefined,
86 .
87 . {* The relocation was performed, but may not be ok - presently
88 . generated only when linking i960 coff files with i960 b.out
89 . symbols. If this type is returned, the error_message argument
90 . to bfd_perform_relocation will be set. *}
91 . bfd_reloc_dangerous
92 . }
93 . bfd_reloc_status_type;
94 .
95 .
96 .typedef struct reloc_cache_entry
97 .{
98 . {* A pointer into the canonical table of pointers *}
99 . struct symbol_cache_entry **sym_ptr_ptr;
100 .
101 . {* offset in section *}
102 . bfd_size_type address;
103 .
104 . {* addend for relocation value *}
105 . bfd_vma addend;
106 .
107 . {* Pointer to how to perform the required relocation *}
108 . reloc_howto_type *howto;
109 .
110 .} arelent;
111
112 */
113
114 /*
115 DESCRIPTION
116
117 Here is a description of each of the fields within an <<arelent>>:
118
119 o <<sym_ptr_ptr>>
120
121 The symbol table pointer points to a pointer to the symbol
122 associated with the relocation request. It is
123 the pointer into the table returned by the back end's
124 <<get_symtab>> action. @xref{Symbols}. The symbol is referenced
125 through a pointer to a pointer so that tools like the linker
126 can fix up all the symbols of the same name by modifying only
127 one pointer. The relocation routine looks in the symbol and
128 uses the base of the section the symbol is attached to and the
129 value of the symbol as the initial relocation offset. If the
130 symbol pointer is zero, then the section provided is looked up.
131
132 o <<address>>
133
134 The <<address>> field gives the offset in bytes from the base of
135 the section data which owns the relocation record to the first
136 byte of relocatable information. The actual data relocated
137 will be relative to this point; for example, a relocation
138 type which modifies the bottom two bytes of a four byte word
139 would not touch the first byte pointed to in a big endian
140 world.
141
142 o <<addend>>
143
144 The <<addend>> is a value provided by the back end to be added (!)
145 to the relocation offset. Its interpretation is dependent upon
146 the howto. For example, on the 68k the code:
147
148
149 | char foo[];
150 | main()
151 | {
152 | return foo[0x12345678];
153 | }
154
155 Could be compiled into:
156
157 | linkw fp,#-4
158 | moveb @@#12345678,d0
159 | extbl d0
160 | unlk fp
161 | rts
162
163
164 This could create a reloc pointing to <<foo>>, but leave the
165 offset in the data, something like:
166
167
168 |RELOCATION RECORDS FOR [.text]:
169 |offset type value
170 |00000006 32 _foo
171 |
172 |00000000 4e56 fffc ; linkw fp,#-4
173 |00000004 1039 1234 5678 ; moveb @@#12345678,d0
174 |0000000a 49c0 ; extbl d0
175 |0000000c 4e5e ; unlk fp
176 |0000000e 4e75 ; rts
177
178
179 Using coff and an 88k, some instructions don't have enough
180 space in them to represent the full address range, and
181 pointers have to be loaded in two parts. So you'd get something like:
182
183
184 | or.u r13,r0,hi16(_foo+0x12345678)
185 | ld.b r2,r13,lo16(_foo+0x12345678)
186 | jmp r1
187
188
189 This should create two relocs, both pointing to <<_foo>>, and with
190 0x12340000 in their addend field. The data would consist of:
191
192
193 |RELOCATION RECORDS FOR [.text]:
194 |offset type value
195 |00000002 HVRT16 _foo+0x12340000
196 |00000006 LVRT16 _foo+0x12340000
197 |
198 |00000000 5da05678 ; or.u r13,r0,0x5678
199 |00000004 1c4d5678 ; ld.b r2,r13,0x5678
200 |00000008 f400c001 ; jmp r1
201
202
203 The relocation routine digs out the value from the data, adds
204 it to the addend to get the original offset, and then adds the
205 value of <<_foo>>. Note that all 32 bits have to be kept around
206 somewhere, to cope with carry from bit 15 to bit 16.
207
208 One further example is the sparc and the a.out format. The
209 sparc has a similar problem to the 88k, in that some
210 instructions don't have room for an entire offset, but on the
211 sparc the parts are created in odd sized lumps. The designers of
212 the a.out format chose to not use the data within the section
213 for storing part of the offset; all the offset is kept within
214 the reloc. Anything in the data should be ignored.
215
216 | save %sp,-112,%sp
217 | sethi %hi(_foo+0x12345678),%g2
218 | ldsb [%g2+%lo(_foo+0x12345678)],%i0
219 | ret
220 | restore
221
222 Both relocs contain a pointer to <<foo>>, and the offsets
223 contain junk.
224
225
226 |RELOCATION RECORDS FOR [.text]:
227 |offset type value
228 |00000004 HI22 _foo+0x12345678
229 |00000008 LO10 _foo+0x12345678
230 |
231 |00000000 9de3bf90 ; save %sp,-112,%sp
232 |00000004 05000000 ; sethi %hi(_foo+0),%g2
233 |00000008 f048a000 ; ldsb [%g2+%lo(_foo+0)],%i0
234 |0000000c 81c7e008 ; ret
235 |00000010 81e80000 ; restore
236
237
238 o <<howto>>
239
240 The <<howto>> field can be imagined as a
241 relocation instruction. It is a pointer to a structure which
242 contains information on what to do with all of the other
243 information in the reloc record and data section. A back end
244 would normally have a relocation instruction set and turn
245 relocations into pointers to the correct structure on input -
246 but it would be possible to create each howto field on demand.
247
248 */
249
250 /*
251 SUBSUBSECTION
252 <<enum complain_overflow>>
253
254 Indicates what sort of overflow checking should be done when
255 performing a relocation.
256
257 CODE_FRAGMENT
258 .
259 .enum complain_overflow
260 .{
261 . {* Do not complain on overflow. *}
262 . complain_overflow_dont,
263 .
264 . {* Complain if the bitfield overflows, whether it is considered
265 . as signed or unsigned. *}
266 . complain_overflow_bitfield,
267 .
268 . {* Complain if the value overflows when considered as signed
269 . number. *}
270 . complain_overflow_signed,
271 .
272 . {* Complain if the value overflows when considered as an
273 . unsigned number. *}
274 . complain_overflow_unsigned
275 .};
276
277 */
278
279 /*
280 SUBSUBSECTION
281 <<reloc_howto_type>>
282
283 The <<reloc_howto_type>> is a structure which contains all the
284 information that libbfd needs to know to tie up a back end's data.
285
286 CODE_FRAGMENT
287 .struct symbol_cache_entry; {* Forward declaration *}
288 .
289 .struct reloc_howto_struct
290 .{
291 . {* The type field has mainly a documetary use - the back end can
292 . do what it wants with it, though normally the back end's
293 . external idea of what a reloc number is stored
294 . in this field. For example, a PC relative word relocation
295 . in a coff environment has the type 023 - because that's
296 . what the outside world calls a R_PCRWORD reloc. *}
297 . unsigned int type;
298 .
299 . {* The value the final relocation is shifted right by. This drops
300 . unwanted data from the relocation. *}
301 . unsigned int rightshift;
302 .
303 . {* The size of the item to be relocated. This is *not* a
304 . power-of-two measure. To get the number of bytes operated
305 . on by a type of relocation, use bfd_get_reloc_size. *}
306 . int size;
307 .
308 . {* The number of bits in the item to be relocated. This is used
309 . when doing overflow checking. *}
310 . unsigned int bitsize;
311 .
312 . {* Notes that the relocation is relative to the location in the
313 . data section of the addend. The relocation function will
314 . subtract from the relocation value the address of the location
315 . being relocated. *}
316 . boolean pc_relative;
317 .
318 . {* The bit position of the reloc value in the destination.
319 . The relocated value is left shifted by this amount. *}
320 . unsigned int bitpos;
321 .
322 . {* What type of overflow error should be checked for when
323 . relocating. *}
324 . enum complain_overflow complain_on_overflow;
325 .
326 . {* If this field is non null, then the supplied function is
327 . called rather than the normal function. This allows really
328 . strange relocation methods to be accomodated (e.g., i960 callj
329 . instructions). *}
330 . bfd_reloc_status_type (*special_function)
331 . PARAMS ((bfd *abfd,
332 . arelent *reloc_entry,
333 . struct symbol_cache_entry *symbol,
334 . PTR data,
335 . asection *input_section,
336 . bfd *output_bfd,
337 . char **error_message));
338 .
339 . {* The textual name of the relocation type. *}
340 . char *name;
341 .
342 . {* When performing a partial link, some formats must modify the
343 . relocations rather than the data - this flag signals this.*}
344 . boolean partial_inplace;
345 .
346 . {* The src_mask selects which parts of the read in data
347 . are to be used in the relocation sum. E.g., if this was an 8 bit
348 . bit of data which we read and relocated, this would be
349 . 0x000000ff. When we have relocs which have an addend, such as
350 . sun4 extended relocs, the value in the offset part of a
351 . relocating field is garbage so we never use it. In this case
352 . the mask would be 0x00000000. *}
353 . bfd_vma src_mask;
354 .
355 . {* The dst_mask selects which parts of the instruction are replaced
356 . into the instruction. In most cases src_mask == dst_mask,
357 . except in the above special case, where dst_mask would be
358 . 0x000000ff, and src_mask would be 0x00000000. *}
359 . bfd_vma dst_mask;
360 .
361 . {* When some formats create PC relative instructions, they leave
362 . the value of the pc of the place being relocated in the offset
363 . slot of the instruction, so that a PC relative relocation can
364 . be made just by adding in an ordinary offset (e.g., sun3 a.out).
365 . Some formats leave the displacement part of an instruction
366 . empty (e.g., m88k bcs); this flag signals the fact.*}
367 . boolean pcrel_offset;
368 .
369 .};
370
371 */
372
373 /*
374 FUNCTION
375 The HOWTO Macro
376
377 DESCRIPTION
378 The HOWTO define is horrible and will go away.
379
380
381 .#define HOWTO(C, R,S,B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
382 . {(unsigned)C,R,S,B, P, BI, O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
383
384 DESCRIPTION
385 And will be replaced with the totally magic way. But for the
386 moment, we are compatible, so do it this way.
387
388
389 .#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,complain_overflow_dont,FUNCTION, NAME,false,0,0,IN)
390 .
391 DESCRIPTION
392 Helper routine to turn a symbol into a relocation value.
393
394 .#define HOWTO_PREPARE(relocation, symbol) \
395 . { \
396 . if (symbol != (asymbol *)NULL) { \
397 . if (bfd_is_com_section (symbol->section)) { \
398 . relocation = 0; \
399 . } \
400 . else { \
401 . relocation = symbol->value; \
402 . } \
403 . } \
404 .}
405
406 */
407
408 /*
409 FUNCTION
410 bfd_get_reloc_size
411
412 SYNOPSIS
413 int bfd_get_reloc_size (reloc_howto_type *);
414
415 DESCRIPTION
416 For a reloc_howto_type that operates on a fixed number of bytes,
417 this returns the number of bytes operated on.
418 */
419
420 int
421 bfd_get_reloc_size (howto)
422 reloc_howto_type *howto;
423 {
424 switch (howto->size)
425 {
426 case 0: return 1;
427 case 1: return 2;
428 case 2: return 4;
429 case 3: return 0;
430 case 4: return 8;
431 case 8: return 16;
432 case -2: return 4;
433 default: abort ();
434 }
435 }
436
437 /*
438 TYPEDEF
439 arelent_chain
440
441 DESCRIPTION
442
443 How relocs are tied together in an <<asection>>:
444
445 .typedef struct relent_chain {
446 . arelent relent;
447 . struct relent_chain *next;
448 .} arelent_chain;
449
450 */
451
452
453
454 /*
455 FUNCTION
456 bfd_perform_relocation
457
458 SYNOPSIS
459 bfd_reloc_status_type
460 bfd_perform_relocation
461 (bfd *abfd,
462 arelent *reloc_entry,
463 PTR data,
464 asection *input_section,
465 bfd *output_bfd,
466 char **error_message);
467
468 DESCRIPTION
469 If @var{output_bfd} is supplied to this function, the
470 generated image will be relocatable; the relocations are
471 copied to the output file after they have been changed to
472 reflect the new state of the world. There are two ways of
473 reflecting the results of partial linkage in an output file:
474 by modifying the output data in place, and by modifying the
475 relocation record. Some native formats (e.g., basic a.out and
476 basic coff) have no way of specifying an addend in the
477 relocation type, so the addend has to go in the output data.
478 This is no big deal since in these formats the output data
479 slot will always be big enough for the addend. Complex reloc
480 types with addends were invented to solve just this problem.
481 The @var{error_message} argument is set to an error message if
482 this return @code{bfd_reloc_dangerous}.
483
484 */
485
486
487 bfd_reloc_status_type
488 bfd_perform_relocation (abfd, reloc_entry, data, input_section, output_bfd,
489 error_message)
490 bfd *abfd;
491 arelent *reloc_entry;
492 PTR data;
493 asection *input_section;
494 bfd *output_bfd;
495 char **error_message;
496 {
497 bfd_vma relocation;
498 bfd_reloc_status_type flag = bfd_reloc_ok;
499 bfd_size_type addr = reloc_entry->address;
500 bfd_vma output_base = 0;
501 reloc_howto_type *howto = reloc_entry->howto;
502 asection *reloc_target_output_section;
503 asymbol *symbol;
504
505 symbol = *(reloc_entry->sym_ptr_ptr);
506 if (bfd_is_abs_section (symbol->section)
507 && output_bfd != (bfd *) NULL)
508 {
509 reloc_entry->address += input_section->output_offset;
510 return bfd_reloc_ok;
511 }
512
513 /* If we are not producing relocateable output, return an error if
514 the symbol is not defined. An undefined weak symbol is
515 considered to have a value of zero (SVR4 ABI, p. 4-27). */
516 if (bfd_is_und_section (symbol->section)
517 && (symbol->flags & BSF_WEAK) == 0
518 && output_bfd == (bfd *) NULL)
519 flag = bfd_reloc_undefined;
520
521 /* If there is a function supplied to handle this relocation type,
522 call it. It'll return `bfd_reloc_continue' if further processing
523 can be done. */
524 if (howto->special_function)
525 {
526 bfd_reloc_status_type cont;
527 cont = howto->special_function (abfd, reloc_entry, symbol, data,
528 input_section, output_bfd,
529 error_message);
530 if (cont != bfd_reloc_continue)
531 return cont;
532 }
533
534 /* Is the address of the relocation really within the section? */
535 if (reloc_entry->address > input_section->_cooked_size)
536 return bfd_reloc_outofrange;
537
538 /* Work out which section the relocation is targetted at and the
539 initial relocation command value. */
540
541 /* Get symbol value. (Common symbols are special.) */
542 if (bfd_is_com_section (symbol->section))
543 relocation = 0;
544 else
545 relocation = symbol->value;
546
547
548 reloc_target_output_section = symbol->section->output_section;
549
550 /* Convert input-section-relative symbol value to absolute. */
551 if (output_bfd && howto->partial_inplace == false)
552 output_base = 0;
553 else
554 output_base = reloc_target_output_section->vma;
555
556 relocation += output_base + symbol->section->output_offset;
557
558 /* Add in supplied addend. */
559 relocation += reloc_entry->addend;
560
561 /* Here the variable relocation holds the final address of the
562 symbol we are relocating against, plus any addend. */
563
564 if (howto->pc_relative == true)
565 {
566 /* This is a PC relative relocation. We want to set RELOCATION
567 to the distance between the address of the symbol and the
568 location. RELOCATION is already the address of the symbol.
569
570 We start by subtracting the address of the section containing
571 the location.
572
573 If pcrel_offset is set, we must further subtract the position
574 of the location within the section. Some targets arrange for
575 the addend to be the negative of the position of the location
576 within the section; for example, i386-aout does this. For
577 i386-aout, pcrel_offset is false. Some other targets do not
578 include the position of the location; for example, m88kbcs,
579 or ELF. For those targets, pcrel_offset is true.
580
581 If we are producing relocateable output, then we must ensure
582 that this reloc will be correctly computed when the final
583 relocation is done. If pcrel_offset is false we want to wind
584 up with the negative of the location within the section,
585 which means we must adjust the existing addend by the change
586 in the location within the section. If pcrel_offset is true
587 we do not want to adjust the existing addend at all.
588
589 FIXME: This seems logical to me, but for the case of
590 producing relocateable output it is not what the code
591 actually does. I don't want to change it, because it seems
592 far too likely that something will break. */
593
594 relocation -=
595 input_section->output_section->vma + input_section->output_offset;
596
597 if (howto->pcrel_offset == true)
598 relocation -= reloc_entry->address;
599 }
600
601 if (output_bfd != (bfd *) NULL)
602 {
603 if (howto->partial_inplace == false)
604 {
605 /* This is a partial relocation, and we want to apply the relocation
606 to the reloc entry rather than the raw data. Modify the reloc
607 inplace to reflect what we now know. */
608 reloc_entry->addend = relocation;
609 reloc_entry->address += input_section->output_offset;
610 return flag;
611 }
612 else
613 {
614 /* This is a partial relocation, but inplace, so modify the
615 reloc record a bit.
616
617 If we've relocated with a symbol with a section, change
618 into a ref to the section belonging to the symbol. */
619
620 reloc_entry->address += input_section->output_offset;
621
622 /* WTF?? */
623 if (abfd->xvec->flavour == bfd_target_coff_flavour
624 && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
625 && strcmp (abfd->xvec->name, "xcoff-powermac") != 0
626 && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
627 && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
628 {
629 #if 1
630 /* For m68k-coff, the addend was being subtracted twice during
631 relocation with -r. Removing the line below this comment
632 fixes that problem; see PR 2953.
633
634 However, Ian wrote the following, regarding removing the line below,
635 which explains why it is still enabled: --djm
636
637 If you put a patch like that into BFD you need to check all the COFF
638 linkers. I am fairly certain that patch will break coff-i386 (e.g.,
639 SCO); see coff_i386_reloc in coff-i386.c where I worked around the
640 problem in a different way. There may very well be a reason that the
641 code works as it does.
642
643 Hmmm. The first obvious point is that bfd_perform_relocation should
644 not have any tests that depend upon the flavour. It's seem like
645 entirely the wrong place for such a thing. The second obvious point
646 is that the current code ignores the reloc addend when producing
647 relocateable output for COFF. That's peculiar. In fact, I really
648 have no idea what the point of the line you want to remove is.
649
650 A typical COFF reloc subtracts the old value of the symbol and adds in
651 the new value to the location in the object file (if it's a pc
652 relative reloc it adds the difference between the symbol value and the
653 location). When relocating we need to preserve that property.
654
655 BFD handles this by setting the addend to the negative of the old
656 value of the symbol. Unfortunately it handles common symbols in a
657 non-standard way (it doesn't subtract the old value) but that's a
658 different story (we can't change it without losing backward
659 compatibility with old object files) (coff-i386 does subtract the old
660 value, to be compatible with existing coff-i386 targets, like SCO).
661
662 So everything works fine when not producing relocateable output. When
663 we are producing relocateable output, logically we should do exactly
664 what we do when not producing relocateable output. Therefore, your
665 patch is correct. In fact, it should probably always just set
666 reloc_entry->addend to 0 for all cases, since it is, in fact, going to
667 add the value into the object file. This won't hurt the COFF code,
668 which doesn't use the addend; I'm not sure what it will do to other
669 formats (the thing to check for would be whether any formats both use
670 the addend and set partial_inplace).
671
672 When I wanted to make coff-i386 produce relocateable output, I ran
673 into the problem that you are running into: I wanted to remove that
674 line. Rather than risk it, I made the coff-i386 relocs use a special
675 function; it's coff_i386_reloc in coff-i386.c. The function
676 specifically adds the addend field into the object file, knowing that
677 bfd_perform_relocation is not going to. If you remove that line, then
678 coff-i386.c will wind up adding the addend field in twice. It's
679 trivial to fix; it just needs to be done.
680
681 The problem with removing the line is just that it may break some
682 working code. With BFD it's hard to be sure of anything. The right
683 way to deal with this is simply to build and test at least all the
684 supported COFF targets. It should be straightforward if time and disk
685 space consuming. For each target:
686 1) build the linker
687 2) generate some executable, and link it using -r (I would
688 probably use paranoia.o and link against newlib/libc.a, which
689 for all the supported targets would be available in
690 /usr/cygnus/progressive/H-host/target/lib/libc.a).
691 3) make the change to reloc.c
692 4) rebuild the linker
693 5) repeat step 2
694 6) if the resulting object files are the same, you have at least
695 made it no worse
696 7) if they are different you have to figure out which version is
697 right
698 */
699 relocation -= reloc_entry->addend;
700 #endif
701 reloc_entry->addend = 0;
702 }
703 else
704 {
705 reloc_entry->addend = relocation;
706 }
707 }
708 }
709 else
710 {
711 reloc_entry->addend = 0;
712 }
713
714 /* FIXME: This overflow checking is incomplete, because the value
715 might have overflowed before we get here. For a correct check we
716 need to compute the value in a size larger than bitsize, but we
717 can't reasonably do that for a reloc the same size as a host
718 machine word.
719 FIXME: We should also do overflow checking on the result after
720 adding in the value contained in the object file. */
721 if (howto->complain_on_overflow != complain_overflow_dont
722 && flag == bfd_reloc_ok)
723 {
724 bfd_vma check;
725
726 /* Get the value that will be used for the relocation, but
727 starting at bit position zero. */
728 check = relocation >> howto->rightshift;
729 switch (howto->complain_on_overflow)
730 {
731 case complain_overflow_signed:
732 {
733 /* Assumes two's complement. */
734 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
735 bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
736
737 /* The above right shift is incorrect for a signed value.
738 Fix it up by forcing on the upper bits. */
739 if (howto->rightshift > 0
740 && (bfd_signed_vma) relocation < 0)
741 check |= ((bfd_vma) - 1
742 & ~((bfd_vma) - 1
743 >> howto->rightshift));
744 if ((bfd_signed_vma) check > reloc_signed_max
745 || (bfd_signed_vma) check < reloc_signed_min)
746 flag = bfd_reloc_overflow;
747 }
748 break;
749 case complain_overflow_unsigned:
750 {
751 /* Assumes two's complement. This expression avoids
752 overflow if howto->bitsize is the number of bits in
753 bfd_vma. */
754 bfd_vma reloc_unsigned_max =
755 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
756
757 if ((bfd_vma) check > reloc_unsigned_max)
758 flag = bfd_reloc_overflow;
759 }
760 break;
761 case complain_overflow_bitfield:
762 {
763 /* Assumes two's complement. This expression avoids
764 overflow if howto->bitsize is the number of bits in
765 bfd_vma. */
766 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
767
768 if (((bfd_vma) check & ~reloc_bits) != 0
769 && ((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
770 {
771 /* The above right shift is incorrect for a signed
772 value. See if turning on the upper bits fixes the
773 overflow. */
774 if (howto->rightshift > 0
775 && (bfd_signed_vma) relocation < 0)
776 {
777 check |= ((bfd_vma) - 1
778 & ~((bfd_vma) - 1
779 >> howto->rightshift));
780 if (((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
781 flag = bfd_reloc_overflow;
782 }
783 else
784 flag = bfd_reloc_overflow;
785 }
786 }
787 break;
788 default:
789 abort ();
790 }
791 }
792
793 /*
794 Either we are relocating all the way, or we don't want to apply
795 the relocation to the reloc entry (probably because there isn't
796 any room in the output format to describe addends to relocs)
797 */
798
799 /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
800 (OSF version 1.3, compiler version 3.11). It miscompiles the
801 following program:
802
803 struct str
804 {
805 unsigned int i0;
806 } s = { 0 };
807
808 int
809 main ()
810 {
811 unsigned long x;
812
813 x = 0x100000000;
814 x <<= (unsigned long) s.i0;
815 if (x == 0)
816 printf ("failed\n");
817 else
818 printf ("succeeded (%lx)\n", x);
819 }
820 */
821
822 relocation >>= (bfd_vma) howto->rightshift;
823
824 /* Shift everything up to where it's going to be used */
825
826 relocation <<= (bfd_vma) howto->bitpos;
827
828 /* Wait for the day when all have the mask in them */
829
830 /* What we do:
831 i instruction to be left alone
832 o offset within instruction
833 r relocation offset to apply
834 S src mask
835 D dst mask
836 N ~dst mask
837 A part 1
838 B part 2
839 R result
840
841 Do this:
842 i i i i i o o o o o from bfd_get<size>
843 and S S S S S to get the size offset we want
844 + r r r r r r r r r r to get the final value to place
845 and D D D D D to chop to right size
846 -----------------------
847 A A A A A
848 And this:
849 ... i i i i i o o o o o from bfd_get<size>
850 and N N N N N get instruction
851 -----------------------
852 ... B B B B B
853
854 And then:
855 B B B B B
856 or A A A A A
857 -----------------------
858 R R R R R R R R R R put into bfd_put<size>
859 */
860
861 #define DOIT(x) \
862 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
863
864 switch (howto->size)
865 {
866 case 0:
867 {
868 char x = bfd_get_8 (abfd, (char *) data + addr);
869 DOIT (x);
870 bfd_put_8 (abfd, x, (unsigned char *) data + addr);
871 }
872 break;
873
874 case 1:
875 if (relocation)
876 {
877 short x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
878 DOIT (x);
879 bfd_put_16 (abfd, x, (unsigned char *) data + addr);
880 }
881 break;
882 case 2:
883 if (relocation)
884 {
885 long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
886 DOIT (x);
887 bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
888 }
889 break;
890 case -2:
891 {
892 long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
893 relocation = -relocation;
894 DOIT (x);
895 bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
896 }
897 break;
898
899 case -1:
900 {
901 long x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
902 relocation = -relocation;
903 DOIT (x);
904 bfd_put_16 (abfd, x, (bfd_byte *) data + addr);
905 }
906 break;
907
908 case 3:
909 /* Do nothing */
910 break;
911
912 case 4:
913 #ifdef BFD64
914 if (relocation)
915 {
916 bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + addr);
917 DOIT (x);
918 bfd_put_64 (abfd, x, (bfd_byte *) data + addr);
919 }
920 #else
921 abort ();
922 #endif
923 break;
924 default:
925 return bfd_reloc_other;
926 }
927
928 return flag;
929 }
930
931 /*
932 FUNCTION
933 bfd_install_relocation
934
935 SYNOPSIS
936 bfd_reloc_status_type
937 bfd_install_relocation
938 (bfd *abfd,
939 arelent *reloc_entry,
940 PTR data, bfd_vma data_start,
941 asection *input_section,
942 char **error_message);
943
944 DESCRIPTION
945 This looks remarkably like <<bfd_perform_relocation>>, except it
946 does not expect that the section contents have been filled in.
947 I.e., it's suitable for use when creating, rather than applying
948 a relocation.
949
950 For now, this function should be considered reserved for the
951 assembler.
952
953 */
954
955
956 bfd_reloc_status_type
957 bfd_install_relocation (abfd, reloc_entry, data_start, data_start_offset,
958 input_section, error_message)
959 bfd *abfd;
960 arelent *reloc_entry;
961 PTR data_start;
962 bfd_vma data_start_offset;
963 asection *input_section;
964 char **error_message;
965 {
966 bfd_vma relocation;
967 bfd_reloc_status_type flag = bfd_reloc_ok;
968 bfd_size_type addr = reloc_entry->address;
969 bfd_vma output_base = 0;
970 reloc_howto_type *howto = reloc_entry->howto;
971 asection *reloc_target_output_section;
972 asymbol *symbol;
973 bfd_byte *data;
974
975 symbol = *(reloc_entry->sym_ptr_ptr);
976 if (bfd_is_abs_section (symbol->section))
977 {
978 reloc_entry->address += input_section->output_offset;
979 return bfd_reloc_ok;
980 }
981
982 /* If there is a function supplied to handle this relocation type,
983 call it. It'll return `bfd_reloc_continue' if further processing
984 can be done. */
985 if (howto->special_function)
986 {
987 bfd_reloc_status_type cont;
988 /* XXX - The special_function calls haven't been fixed up to deal
989 with creating new relocations and section contents. */
990 cont = howto->special_function (abfd, reloc_entry, symbol,
991 /* XXX - Non-portable! */
992 ((bfd_byte *) data_start
993 - data_start_offset),
994 input_section, abfd, error_message);
995 if (cont != bfd_reloc_continue)
996 return cont;
997 }
998
999 /* Is the address of the relocation really within the section? */
1000 if (reloc_entry->address > input_section->_cooked_size)
1001 return bfd_reloc_outofrange;
1002
1003 /* Work out which section the relocation is targetted at and the
1004 initial relocation command value. */
1005
1006 /* Get symbol value. (Common symbols are special.) */
1007 if (bfd_is_com_section (symbol->section))
1008 relocation = 0;
1009 else
1010 relocation = symbol->value;
1011
1012
1013 reloc_target_output_section = symbol->section->output_section;
1014
1015 /* Convert input-section-relative symbol value to absolute. */
1016 if (howto->partial_inplace == false)
1017 output_base = 0;
1018 else
1019 output_base = reloc_target_output_section->vma;
1020
1021 relocation += output_base + symbol->section->output_offset;
1022
1023 /* Add in supplied addend. */
1024 relocation += reloc_entry->addend;
1025
1026 /* Here the variable relocation holds the final address of the
1027 symbol we are relocating against, plus any addend. */
1028
1029 if (howto->pc_relative == true)
1030 {
1031 /* This is a PC relative relocation. We want to set RELOCATION
1032 to the distance between the address of the symbol and the
1033 location. RELOCATION is already the address of the symbol.
1034
1035 We start by subtracting the address of the section containing
1036 the location.
1037
1038 If pcrel_offset is set, we must further subtract the position
1039 of the location within the section. Some targets arrange for
1040 the addend to be the negative of the position of the location
1041 within the section; for example, i386-aout does this. For
1042 i386-aout, pcrel_offset is false. Some other targets do not
1043 include the position of the location; for example, m88kbcs,
1044 or ELF. For those targets, pcrel_offset is true.
1045
1046 If we are producing relocateable output, then we must ensure
1047 that this reloc will be correctly computed when the final
1048 relocation is done. If pcrel_offset is false we want to wind
1049 up with the negative of the location within the section,
1050 which means we must adjust the existing addend by the change
1051 in the location within the section. If pcrel_offset is true
1052 we do not want to adjust the existing addend at all.
1053
1054 FIXME: This seems logical to me, but for the case of
1055 producing relocateable output it is not what the code
1056 actually does. I don't want to change it, because it seems
1057 far too likely that something will break. */
1058
1059 relocation -=
1060 input_section->output_section->vma + input_section->output_offset;
1061
1062 if (howto->pcrel_offset == true && howto->partial_inplace == true)
1063 relocation -= reloc_entry->address;
1064 }
1065
1066 if (howto->partial_inplace == false)
1067 {
1068 /* This is a partial relocation, and we want to apply the relocation
1069 to the reloc entry rather than the raw data. Modify the reloc
1070 inplace to reflect what we now know. */
1071 reloc_entry->addend = relocation;
1072 reloc_entry->address += input_section->output_offset;
1073 return flag;
1074 }
1075 else
1076 {
1077 /* This is a partial relocation, but inplace, so modify the
1078 reloc record a bit.
1079
1080 If we've relocated with a symbol with a section, change
1081 into a ref to the section belonging to the symbol. */
1082
1083 reloc_entry->address += input_section->output_offset;
1084
1085 /* WTF?? */
1086 if (abfd->xvec->flavour == bfd_target_coff_flavour
1087 && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
1088 && strcmp (abfd->xvec->name, "xcoff-powermac") != 0
1089 && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
1090 && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
1091 {
1092 #if 1
1093 /* For m68k-coff, the addend was being subtracted twice during
1094 relocation with -r. Removing the line below this comment
1095 fixes that problem; see PR 2953.
1096
1097 However, Ian wrote the following, regarding removing the line below,
1098 which explains why it is still enabled: --djm
1099
1100 If you put a patch like that into BFD you need to check all the COFF
1101 linkers. I am fairly certain that patch will break coff-i386 (e.g.,
1102 SCO); see coff_i386_reloc in coff-i386.c where I worked around the
1103 problem in a different way. There may very well be a reason that the
1104 code works as it does.
1105
1106 Hmmm. The first obvious point is that bfd_install_relocation should
1107 not have any tests that depend upon the flavour. It's seem like
1108 entirely the wrong place for such a thing. The second obvious point
1109 is that the current code ignores the reloc addend when producing
1110 relocateable output for COFF. That's peculiar. In fact, I really
1111 have no idea what the point of the line you want to remove is.
1112
1113 A typical COFF reloc subtracts the old value of the symbol and adds in
1114 the new value to the location in the object file (if it's a pc
1115 relative reloc it adds the difference between the symbol value and the
1116 location). When relocating we need to preserve that property.
1117
1118 BFD handles this by setting the addend to the negative of the old
1119 value of the symbol. Unfortunately it handles common symbols in a
1120 non-standard way (it doesn't subtract the old value) but that's a
1121 different story (we can't change it without losing backward
1122 compatibility with old object files) (coff-i386 does subtract the old
1123 value, to be compatible with existing coff-i386 targets, like SCO).
1124
1125 So everything works fine when not producing relocateable output. When
1126 we are producing relocateable output, logically we should do exactly
1127 what we do when not producing relocateable output. Therefore, your
1128 patch is correct. In fact, it should probably always just set
1129 reloc_entry->addend to 0 for all cases, since it is, in fact, going to
1130 add the value into the object file. This won't hurt the COFF code,
1131 which doesn't use the addend; I'm not sure what it will do to other
1132 formats (the thing to check for would be whether any formats both use
1133 the addend and set partial_inplace).
1134
1135 When I wanted to make coff-i386 produce relocateable output, I ran
1136 into the problem that you are running into: I wanted to remove that
1137 line. Rather than risk it, I made the coff-i386 relocs use a special
1138 function; it's coff_i386_reloc in coff-i386.c. The function
1139 specifically adds the addend field into the object file, knowing that
1140 bfd_install_relocation is not going to. If you remove that line, then
1141 coff-i386.c will wind up adding the addend field in twice. It's
1142 trivial to fix; it just needs to be done.
1143
1144 The problem with removing the line is just that it may break some
1145 working code. With BFD it's hard to be sure of anything. The right
1146 way to deal with this is simply to build and test at least all the
1147 supported COFF targets. It should be straightforward if time and disk
1148 space consuming. For each target:
1149 1) build the linker
1150 2) generate some executable, and link it using -r (I would
1151 probably use paranoia.o and link against newlib/libc.a, which
1152 for all the supported targets would be available in
1153 /usr/cygnus/progressive/H-host/target/lib/libc.a).
1154 3) make the change to reloc.c
1155 4) rebuild the linker
1156 5) repeat step 2
1157 6) if the resulting object files are the same, you have at least
1158 made it no worse
1159 7) if they are different you have to figure out which version is
1160 right
1161 */
1162 relocation -= reloc_entry->addend;
1163 #endif
1164 reloc_entry->addend = 0;
1165 }
1166 else
1167 {
1168 reloc_entry->addend = relocation;
1169 }
1170 }
1171
1172 /* FIXME: This overflow checking is incomplete, because the value
1173 might have overflowed before we get here. For a correct check we
1174 need to compute the value in a size larger than bitsize, but we
1175 can't reasonably do that for a reloc the same size as a host
1176 machine word.
1177
1178 FIXME: We should also do overflow checking on the result after
1179 adding in the value contained in the object file. */
1180 if (howto->complain_on_overflow != complain_overflow_dont)
1181 {
1182 bfd_vma check;
1183
1184 /* Get the value that will be used for the relocation, but
1185 starting at bit position zero. */
1186 check = relocation >> howto->rightshift;
1187 switch (howto->complain_on_overflow)
1188 {
1189 case complain_overflow_signed:
1190 {
1191 /* Assumes two's complement. */
1192 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
1193 bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
1194
1195 /* The above right shift is incorrect for a signed value.
1196 Fix it up by forcing on the upper bits. */
1197 if (howto->rightshift > 0
1198 && (bfd_signed_vma) relocation < 0)
1199 check |= ((bfd_vma) - 1
1200 & ~((bfd_vma) - 1
1201 >> howto->rightshift));
1202 if ((bfd_signed_vma) check > reloc_signed_max
1203 || (bfd_signed_vma) check < reloc_signed_min)
1204 flag = bfd_reloc_overflow;
1205 }
1206 break;
1207 case complain_overflow_unsigned:
1208 {
1209 /* Assumes two's complement. This expression avoids
1210 overflow if howto->bitsize is the number of bits in
1211 bfd_vma. */
1212 bfd_vma reloc_unsigned_max =
1213 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1214
1215 if ((bfd_vma) check > reloc_unsigned_max)
1216 flag = bfd_reloc_overflow;
1217 }
1218 break;
1219 case complain_overflow_bitfield:
1220 {
1221 /* Assumes two's complement. This expression avoids
1222 overflow if howto->bitsize is the number of bits in
1223 bfd_vma. */
1224 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1225
1226 if (((bfd_vma) check & ~reloc_bits) != 0
1227 && ((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
1228 {
1229 /* The above right shift is incorrect for a signed
1230 value. See if turning on the upper bits fixes the
1231 overflow. */
1232 if (howto->rightshift > 0
1233 && (bfd_signed_vma) relocation < 0)
1234 {
1235 check |= ((bfd_vma) - 1
1236 & ~((bfd_vma) - 1
1237 >> howto->rightshift));
1238 if (((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
1239 flag = bfd_reloc_overflow;
1240 }
1241 else
1242 flag = bfd_reloc_overflow;
1243 }
1244 }
1245 break;
1246 default:
1247 abort ();
1248 }
1249 }
1250
1251 /*
1252 Either we are relocating all the way, or we don't want to apply
1253 the relocation to the reloc entry (probably because there isn't
1254 any room in the output format to describe addends to relocs)
1255 */
1256
1257 /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
1258 (OSF version 1.3, compiler version 3.11). It miscompiles the
1259 following program:
1260
1261 struct str
1262 {
1263 unsigned int i0;
1264 } s = { 0 };
1265
1266 int
1267 main ()
1268 {
1269 unsigned long x;
1270
1271 x = 0x100000000;
1272 x <<= (unsigned long) s.i0;
1273 if (x == 0)
1274 printf ("failed\n");
1275 else
1276 printf ("succeeded (%lx)\n", x);
1277 }
1278 */
1279
1280 relocation >>= (bfd_vma) howto->rightshift;
1281
1282 /* Shift everything up to where it's going to be used */
1283
1284 relocation <<= (bfd_vma) howto->bitpos;
1285
1286 /* Wait for the day when all have the mask in them */
1287
1288 /* What we do:
1289 i instruction to be left alone
1290 o offset within instruction
1291 r relocation offset to apply
1292 S src mask
1293 D dst mask
1294 N ~dst mask
1295 A part 1
1296 B part 2
1297 R result
1298
1299 Do this:
1300 i i i i i o o o o o from bfd_get<size>
1301 and S S S S S to get the size offset we want
1302 + r r r r r r r r r r to get the final value to place
1303 and D D D D D to chop to right size
1304 -----------------------
1305 A A A A A
1306 And this:
1307 ... i i i i i o o o o o from bfd_get<size>
1308 and N N N N N get instruction
1309 -----------------------
1310 ... B B B B B
1311
1312 And then:
1313 B B B B B
1314 or A A A A A
1315 -----------------------
1316 R R R R R R R R R R put into bfd_put<size>
1317 */
1318
1319 #define DOIT(x) \
1320 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
1321
1322 data = (bfd_byte *) data_start + (addr - data_start_offset);
1323
1324 switch (howto->size)
1325 {
1326 case 0:
1327 {
1328 char x = bfd_get_8 (abfd, (char *) data);
1329 DOIT (x);
1330 bfd_put_8 (abfd, x, (unsigned char *) data);
1331 }
1332 break;
1333
1334 case 1:
1335 if (relocation)
1336 {
1337 short x = bfd_get_16 (abfd, (bfd_byte *) data);
1338 DOIT (x);
1339 bfd_put_16 (abfd, x, (unsigned char *) data);
1340 }
1341 break;
1342 case 2:
1343 if (relocation)
1344 {
1345 long x = bfd_get_32 (abfd, (bfd_byte *) data);
1346 DOIT (x);
1347 bfd_put_32 (abfd, x, (bfd_byte *) data);
1348 }
1349 break;
1350 case -2:
1351 {
1352 long x = bfd_get_32 (abfd, (bfd_byte *) data);
1353 relocation = -relocation;
1354 DOIT (x);
1355 bfd_put_32 (abfd, x, (bfd_byte *) data);
1356 }
1357 break;
1358
1359 case 3:
1360 /* Do nothing */
1361 break;
1362
1363 case 4:
1364 if (relocation)
1365 {
1366 bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data);
1367 DOIT (x);
1368 bfd_put_64 (abfd, x, (bfd_byte *) data);
1369 }
1370 break;
1371 default:
1372 return bfd_reloc_other;
1373 }
1374
1375 return flag;
1376 }
1377
1378 /* This relocation routine is used by some of the backend linkers.
1379 They do not construct asymbol or arelent structures, so there is no
1380 reason for them to use bfd_perform_relocation. Also,
1381 bfd_perform_relocation is so hacked up it is easier to write a new
1382 function than to try to deal with it.
1383
1384 This routine does a final relocation. It should not be used when
1385 generating relocateable output.
1386
1387 FIXME: This routine ignores any special_function in the HOWTO,
1388 since the existing special_function values have been written for
1389 bfd_perform_relocation.
1390
1391 HOWTO is the reloc howto information.
1392 INPUT_BFD is the BFD which the reloc applies to.
1393 INPUT_SECTION is the section which the reloc applies to.
1394 CONTENTS is the contents of the section.
1395 ADDRESS is the address of the reloc within INPUT_SECTION.
1396 VALUE is the value of the symbol the reloc refers to.
1397 ADDEND is the addend of the reloc. */
1398
1399 bfd_reloc_status_type
1400 _bfd_final_link_relocate (howto, input_bfd, input_section, contents, address,
1401 value, addend)
1402 reloc_howto_type *howto;
1403 bfd *input_bfd;
1404 asection *input_section;
1405 bfd_byte *contents;
1406 bfd_vma address;
1407 bfd_vma value;
1408 bfd_vma addend;
1409 {
1410 bfd_vma relocation;
1411
1412 /* Sanity check the address. */
1413 if (address > input_section->_raw_size)
1414 return bfd_reloc_outofrange;
1415
1416 /* This function assumes that we are dealing with a basic relocation
1417 against a symbol. We want to compute the value of the symbol to
1418 relocate to. This is just VALUE, the value of the symbol, plus
1419 ADDEND, any addend associated with the reloc. */
1420 relocation = value + addend;
1421
1422 /* If the relocation is PC relative, we want to set RELOCATION to
1423 the distance between the symbol (currently in RELOCATION) and the
1424 location we are relocating. Some targets (e.g., i386-aout)
1425 arrange for the contents of the section to be the negative of the
1426 offset of the location within the section; for such targets
1427 pcrel_offset is false. Other targets (e.g., m88kbcs or ELF)
1428 simply leave the contents of the section as zero; for such
1429 targets pcrel_offset is true. If pcrel_offset is false we do not
1430 need to subtract out the offset of the location within the
1431 section (which is just ADDRESS). */
1432 if (howto->pc_relative)
1433 {
1434 relocation -= (input_section->output_section->vma
1435 + input_section->output_offset);
1436 if (howto->pcrel_offset)
1437 relocation -= address;
1438 }
1439
1440 return _bfd_relocate_contents (howto, input_bfd, relocation,
1441 contents + address);
1442 }
1443
1444 /* Relocate a given location using a given value and howto. */
1445
1446 bfd_reloc_status_type
1447 _bfd_relocate_contents (howto, input_bfd, relocation, location)
1448 reloc_howto_type *howto;
1449 bfd *input_bfd;
1450 bfd_vma relocation;
1451 bfd_byte *location;
1452 {
1453 int size;
1454 bfd_vma x;
1455 boolean overflow;
1456
1457 /* If the size is negative, negate RELOCATION. This isn't very
1458 general. */
1459 if (howto->size < 0)
1460 relocation = -relocation;
1461
1462 /* Get the value we are going to relocate. */
1463 size = bfd_get_reloc_size (howto);
1464 switch (size)
1465 {
1466 default:
1467 case 0:
1468 abort ();
1469 case 1:
1470 x = bfd_get_8 (input_bfd, location);
1471 break;
1472 case 2:
1473 x = bfd_get_16 (input_bfd, location);
1474 break;
1475 case 4:
1476 x = bfd_get_32 (input_bfd, location);
1477 break;
1478 case 8:
1479 #ifdef BFD64
1480 x = bfd_get_64 (input_bfd, location);
1481 #else
1482 abort ();
1483 #endif
1484 break;
1485 }
1486
1487 /* Check for overflow. FIXME: We may drop bits during the addition
1488 which we don't check for. We must either check at every single
1489 operation, which would be tedious, or we must do the computations
1490 in a type larger than bfd_vma, which would be inefficient. */
1491 overflow = false;
1492 if (howto->complain_on_overflow != complain_overflow_dont)
1493 {
1494 bfd_vma check;
1495 bfd_signed_vma signed_check;
1496 bfd_vma add;
1497 bfd_signed_vma signed_add;
1498
1499 if (howto->rightshift == 0)
1500 {
1501 check = relocation;
1502 signed_check = (bfd_signed_vma) relocation;
1503 }
1504 else
1505 {
1506 /* Drop unwanted bits from the value we are relocating to. */
1507 check = relocation >> howto->rightshift;
1508
1509 /* If this is a signed value, the rightshift just dropped
1510 leading 1 bits (assuming twos complement). */
1511 if ((bfd_signed_vma) relocation >= 0)
1512 signed_check = check;
1513 else
1514 signed_check = (check
1515 | ((bfd_vma) - 1
1516 & ~((bfd_vma) - 1 >> howto->rightshift)));
1517 }
1518
1519 /* Get the value from the object file. */
1520 add = x & howto->src_mask;
1521
1522 /* Get the value from the object file with an appropriate sign.
1523 The expression involving howto->src_mask isolates the upper
1524 bit of src_mask. If that bit is set in the value we are
1525 adding, it is negative, and we subtract out that number times
1526 two. If src_mask includes the highest possible bit, then we
1527 can not get the upper bit, but that does not matter since
1528 signed_add needs no adjustment to become negative in that
1529 case. */
1530 signed_add = add;
1531 if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
1532 signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
1533
1534 /* Add the value from the object file, shifted so that it is a
1535 straight number. */
1536 if (howto->bitpos == 0)
1537 {
1538 check += add;
1539 signed_check += signed_add;
1540 }
1541 else
1542 {
1543 check += add >> howto->bitpos;
1544
1545 /* For the signed case we use ADD, rather than SIGNED_ADD,
1546 to avoid warnings from SVR4 cc. This is OK since we
1547 explictly handle the sign bits. */
1548 if (signed_add >= 0)
1549 signed_check += add >> howto->bitpos;
1550 else
1551 signed_check += ((add >> howto->bitpos)
1552 | ((bfd_vma) - 1
1553 & ~((bfd_vma) - 1 >> howto->bitpos)));
1554 }
1555
1556 switch (howto->complain_on_overflow)
1557 {
1558 case complain_overflow_signed:
1559 {
1560 /* Assumes two's complement. */
1561 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
1562 bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
1563
1564 if (signed_check > reloc_signed_max
1565 || signed_check < reloc_signed_min)
1566 overflow = true;
1567 }
1568 break;
1569 case complain_overflow_unsigned:
1570 {
1571 /* Assumes two's complement. This expression avoids
1572 overflow if howto->bitsize is the number of bits in
1573 bfd_vma. */
1574 bfd_vma reloc_unsigned_max =
1575 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1576
1577 if (check > reloc_unsigned_max)
1578 overflow = true;
1579 }
1580 break;
1581 case complain_overflow_bitfield:
1582 {
1583 /* Assumes two's complement. This expression avoids
1584 overflow if howto->bitsize is the number of bits in
1585 bfd_vma. */
1586 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1587
1588 if ((check & ~reloc_bits) != 0
1589 && (((bfd_vma) signed_check & ~reloc_bits)
1590 != (-1 & ~reloc_bits)))
1591 overflow = true;
1592 }
1593 break;
1594 default:
1595 abort ();
1596 }
1597 }
1598
1599 /* Put RELOCATION in the right bits. */
1600 relocation >>= (bfd_vma) howto->rightshift;
1601 relocation <<= (bfd_vma) howto->bitpos;
1602
1603 /* Add RELOCATION to the right bits of X. */
1604 x = ((x & ~howto->dst_mask)
1605 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
1606
1607 /* Put the relocated value back in the object file. */
1608 switch (size)
1609 {
1610 default:
1611 case 0:
1612 abort ();
1613 case 1:
1614 bfd_put_8 (input_bfd, x, location);
1615 break;
1616 case 2:
1617 bfd_put_16 (input_bfd, x, location);
1618 break;
1619 case 4:
1620 bfd_put_32 (input_bfd, x, location);
1621 break;
1622 case 8:
1623 #ifdef BFD64
1624 bfd_put_64 (input_bfd, x, location);
1625 #else
1626 abort ();
1627 #endif
1628 break;
1629 }
1630
1631 return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
1632 }
1633
1634 /*
1635 DOCDD
1636 INODE
1637 howto manager, , typedef arelent, Relocations
1638
1639 SECTION
1640 The howto manager
1641
1642 When an application wants to create a relocation, but doesn't
1643 know what the target machine might call it, it can find out by
1644 using this bit of code.
1645
1646 */
1647
1648 /*
1649 TYPEDEF
1650 bfd_reloc_code_type
1651
1652 DESCRIPTION
1653 The insides of a reloc code. The idea is that, eventually, there
1654 will be one enumerator for every type of relocation we ever do.
1655 Pass one of these values to <<bfd_reloc_type_lookup>>, and it'll
1656 return a howto pointer.
1657
1658 This does mean that the application must determine the correct
1659 enumerator value; you can't get a howto pointer from a random set
1660 of attributes.
1661
1662 SENUM
1663 bfd_reloc_code_real
1664
1665 ENUM
1666 BFD_RELOC_64
1667 ENUMX
1668 BFD_RELOC_32
1669 ENUMX
1670 BFD_RELOC_26
1671 ENUMX
1672 BFD_RELOC_16
1673 ENUMX
1674 BFD_RELOC_14
1675 ENUMX
1676 BFD_RELOC_8
1677 ENUMDOC
1678 Basic absolute relocations of N bits.
1679
1680 ENUM
1681 BFD_RELOC_64_PCREL
1682 ENUMX
1683 BFD_RELOC_32_PCREL
1684 ENUMX
1685 BFD_RELOC_24_PCREL
1686 ENUMX
1687 BFD_RELOC_16_PCREL
1688 ENUMX
1689 BFD_RELOC_12_PCREL
1690 ENUMX
1691 BFD_RELOC_8_PCREL
1692 ENUMDOC
1693 PC-relative relocations. Sometimes these are relative to the address
1694 of the relocation itself; sometimes they are relative to the start of
1695 the section containing the relocation. It depends on the specific target.
1696
1697 The 24-bit relocation is used in some Intel 960 configurations.
1698
1699 ENUM
1700 BFD_RELOC_32_GOT_PCREL
1701 ENUMX
1702 BFD_RELOC_16_GOT_PCREL
1703 ENUMX
1704 BFD_RELOC_8_GOT_PCREL
1705 ENUMX
1706 BFD_RELOC_32_GOTOFF
1707 ENUMX
1708 BFD_RELOC_16_GOTOFF
1709 ENUMX
1710 BFD_RELOC_LO16_GOTOFF
1711 ENUMX
1712 BFD_RELOC_HI16_GOTOFF
1713 ENUMX
1714 BFD_RELOC_HI16_S_GOTOFF
1715 ENUMX
1716 BFD_RELOC_8_GOTOFF
1717 ENUMX
1718 BFD_RELOC_32_PLT_PCREL
1719 ENUMX
1720 BFD_RELOC_24_PLT_PCREL
1721 ENUMX
1722 BFD_RELOC_16_PLT_PCREL
1723 ENUMX
1724 BFD_RELOC_8_PLT_PCREL
1725 ENUMX
1726 BFD_RELOC_32_PLTOFF
1727 ENUMX
1728 BFD_RELOC_16_PLTOFF
1729 ENUMX
1730 BFD_RELOC_LO16_PLTOFF
1731 ENUMX
1732 BFD_RELOC_HI16_PLTOFF
1733 ENUMX
1734 BFD_RELOC_HI16_S_PLTOFF
1735 ENUMX
1736 BFD_RELOC_8_PLTOFF
1737 ENUMDOC
1738 For ELF.
1739
1740 ENUM
1741 BFD_RELOC_68K_GLOB_DAT
1742 ENUMX
1743 BFD_RELOC_68K_JMP_SLOT
1744 ENUMX
1745 BFD_RELOC_68K_RELATIVE
1746 ENUMDOC
1747 Relocations used by 68K ELF.
1748
1749 ENUM
1750 BFD_RELOC_32_BASEREL
1751 ENUMX
1752 BFD_RELOC_16_BASEREL
1753 ENUMX
1754 BFD_RELOC_LO16_BASEREL
1755 ENUMX
1756 BFD_RELOC_HI16_BASEREL
1757 ENUMX
1758 BFD_RELOC_HI16_S_BASEREL
1759 ENUMX
1760 BFD_RELOC_8_BASEREL
1761 ENUMX
1762 BFD_RELOC_RVA
1763 ENUMDOC
1764 Linkage-table relative.
1765
1766 ENUM
1767 BFD_RELOC_8_FFnn
1768 ENUMDOC
1769 Absolute 8-bit relocation, but used to form an address like 0xFFnn.
1770
1771 ENUM
1772 BFD_RELOC_32_PCREL_S2
1773 ENUMX
1774 BFD_RELOC_16_PCREL_S2
1775 ENUMX
1776 BFD_RELOC_23_PCREL_S2
1777 ENUMDOC
1778 These PC-relative relocations are stored as word displacements --
1779 i.e., byte displacements shifted right two bits. The 30-bit word
1780 displacement (<<32_PCREL_S2>> -- 32 bits, shifted 2) is used on the
1781 SPARC. (SPARC tools generally refer to this as <<WDISP30>>.) The
1782 signed 16-bit displacement is used on the MIPS, and the 23-bit
1783 displacement is used on the Alpha.
1784
1785 ENUM
1786 BFD_RELOC_HI22
1787 ENUMX
1788 BFD_RELOC_LO10
1789 ENUMDOC
1790 High 22 bits and low 10 bits of 32-bit value, placed into lower bits of
1791 the target word. These are used on the SPARC.
1792
1793 ENUM
1794 BFD_RELOC_GPREL16
1795 ENUMX
1796 BFD_RELOC_GPREL32
1797 ENUMDOC
1798 For systems that allocate a Global Pointer register, these are
1799 displacements off that register. These relocation types are
1800 handled specially, because the value the register will have is
1801 decided relatively late.
1802
1803
1804 ENUM
1805 BFD_RELOC_I960_CALLJ
1806 ENUMDOC
1807 Reloc types used for i960/b.out.
1808
1809 ENUM
1810 BFD_RELOC_NONE
1811 ENUMX
1812 BFD_RELOC_SPARC_WDISP22
1813 ENUMX
1814 BFD_RELOC_SPARC22
1815 ENUMX
1816 BFD_RELOC_SPARC13
1817 ENUMX
1818 BFD_RELOC_SPARC_GOT10
1819 ENUMX
1820 BFD_RELOC_SPARC_GOT13
1821 ENUMX
1822 BFD_RELOC_SPARC_GOT22
1823 ENUMX
1824 BFD_RELOC_SPARC_PC10
1825 ENUMX
1826 BFD_RELOC_SPARC_PC22
1827 ENUMX
1828 BFD_RELOC_SPARC_WPLT30
1829 ENUMX
1830 BFD_RELOC_SPARC_COPY
1831 ENUMX
1832 BFD_RELOC_SPARC_GLOB_DAT
1833 ENUMX
1834 BFD_RELOC_SPARC_JMP_SLOT
1835 ENUMX
1836 BFD_RELOC_SPARC_RELATIVE
1837 ENUMX
1838 BFD_RELOC_SPARC_UA32
1839 ENUMDOC
1840 SPARC ELF relocations. There is probably some overlap with other
1841 relocation types already defined.
1842
1843 ENUM
1844 BFD_RELOC_SPARC_BASE13
1845 ENUMX
1846 BFD_RELOC_SPARC_BASE22
1847 ENUMDOC
1848 I think these are specific to SPARC a.out (e.g., Sun 4).
1849
1850 ENUMEQ
1851 BFD_RELOC_SPARC_64
1852 BFD_RELOC_64
1853 ENUMX
1854 BFD_RELOC_SPARC_10
1855 ENUMX
1856 BFD_RELOC_SPARC_11
1857 ENUMX
1858 BFD_RELOC_SPARC_OLO10
1859 ENUMX
1860 BFD_RELOC_SPARC_HH22
1861 ENUMX
1862 BFD_RELOC_SPARC_HM10
1863 ENUMX
1864 BFD_RELOC_SPARC_LM22
1865 ENUMX
1866 BFD_RELOC_SPARC_PC_HH22
1867 ENUMX
1868 BFD_RELOC_SPARC_PC_HM10
1869 ENUMX
1870 BFD_RELOC_SPARC_PC_LM22
1871 ENUMX
1872 BFD_RELOC_SPARC_WDISP16
1873 ENUMX
1874 BFD_RELOC_SPARC_WDISP19
1875 ENUMX
1876 BFD_RELOC_SPARC_GLOB_JMP
1877 ENUMX
1878 BFD_RELOC_SPARC_7
1879 ENUMX
1880 BFD_RELOC_SPARC_6
1881 ENUMX
1882 BFD_RELOC_SPARC_5
1883 ENUMDOC
1884 Some relocations we're using for SPARC V9 -- subject to change.
1885
1886 ENUM
1887 BFD_RELOC_ALPHA_GPDISP_HI16
1888 ENUMDOC
1889 Alpha ECOFF and ELF relocations. Some of these treat the symbol or
1890 "addend" in some special way.
1891 For GPDISP_HI16 ("gpdisp") relocations, the symbol is ignored when
1892 writing; when reading, it will be the absolute section symbol. The
1893 addend is the displacement in bytes of the "lda" instruction from
1894 the "ldah" instruction (which is at the address of this reloc).
1895 ENUM
1896 BFD_RELOC_ALPHA_GPDISP_LO16
1897 ENUMDOC
1898 For GPDISP_LO16 ("ignore") relocations, the symbol is handled as
1899 with GPDISP_HI16 relocs. The addend is ignored when writing the
1900 relocations out, and is filled in with the file's GP value on
1901 reading, for convenience.
1902
1903 ENUM
1904 BFD_RELOC_ALPHA_GPDISP
1905 ENUMDOC
1906 The ELF GPDISP relocation is exactly the same as the GPDISP_HI16
1907 relocation except that there is no accompanying GPDISP_LO16
1908 relocation.
1909
1910 ENUM
1911 BFD_RELOC_ALPHA_LITERAL
1912 ENUMX
1913 BFD_RELOC_ALPHA_LITUSE
1914 ENUMDOC
1915 The Alpha LITERAL/LITUSE relocs are produced by a symbol reference;
1916 the assembler turns it into a LDQ instruction to load the address of
1917 the symbol, and then fills in a register in the real instruction.
1918
1919 The LITERAL reloc, at the LDQ instruction, refers to the .lita
1920 section symbol. The addend is ignored when writing, but is filled
1921 in with the file's GP value on reading, for convenience, as with the
1922 GPDISP_LO16 reloc.
1923
1924 The LITUSE reloc, on the instruction using the loaded address, gives
1925 information to the linker that it might be able to use to optimize
1926 away some literal section references. The symbol is ignored (read
1927 as the absolute section symbol), and the "addend" indicates the type
1928 of instruction using the register:
1929 1 - "memory" fmt insn
1930 2 - byte-manipulation (byte offset reg)
1931 3 - jsr (target of branch)
1932
1933 The GNU linker currently doesn't do any of this optimizing.
1934
1935 ENUM
1936 BFD_RELOC_ALPHA_HINT
1937 ENUMDOC
1938 The HINT relocation indicates a value that should be filled into the
1939 "hint" field of a jmp/jsr/ret instruction, for possible branch-
1940 prediction logic which may be provided on some processors.
1941
1942 ENUM
1943 BFD_RELOC_ALPHA_LINKAGE
1944 ENUMDOC
1945 The LINKAGE relocation outputs a linkage pair in the object file,
1946 which is filled by the linker.
1947
1948 ENUM
1949 BFD_RELOC_MIPS_JMP
1950 ENUMDOC
1951 Bits 27..2 of the relocation address shifted right 2 bits;
1952 simple reloc otherwise.
1953
1954 ENUM
1955 BFD_RELOC_HI16
1956 ENUMDOC
1957 High 16 bits of 32-bit value; simple reloc.
1958 ENUM
1959 BFD_RELOC_HI16_S
1960 ENUMDOC
1961 High 16 bits of 32-bit value but the low 16 bits will be sign
1962 extended and added to form the final result. If the low 16
1963 bits form a negative number, we need to add one to the high value
1964 to compensate for the borrow when the low bits are added.
1965 ENUM
1966 BFD_RELOC_LO16
1967 ENUMDOC
1968 Low 16 bits.
1969 ENUM
1970 BFD_RELOC_PCREL_HI16_S
1971 ENUMDOC
1972 Like BFD_RELOC_HI16_S, but PC relative.
1973 ENUM
1974 BFD_RELOC_PCREL_LO16
1975 ENUMDOC
1976 Like BFD_RELOC_LO16, but PC relative.
1977
1978 ENUMEQ
1979 BFD_RELOC_MIPS_GPREL
1980 BFD_RELOC_GPREL16
1981 ENUMDOC
1982 Relocation relative to the global pointer.
1983
1984 ENUM
1985 BFD_RELOC_MIPS_LITERAL
1986 ENUMDOC
1987 Relocation against a MIPS literal section.
1988
1989 ENUM
1990 BFD_RELOC_MIPS_GOT16
1991 ENUMX
1992 BFD_RELOC_MIPS_CALL16
1993 ENUMEQX
1994 BFD_RELOC_MIPS_GPREL32
1995 BFD_RELOC_GPREL32
1996 ENUMX
1997 BFD_RELOC_MIPS_GOT_HI16
1998 ENUMX
1999 BFD_RELOC_MIPS_GOT_LO16
2000 ENUMX
2001 BFD_RELOC_MIPS_CALL_HI16
2002 ENUMX
2003 BFD_RELOC_MIPS_CALL_LO16
2004 ENUMDOC
2005 MIPS ELF relocations.
2006
2007 ENUM
2008 BFD_RELOC_386_GOT32
2009 ENUMX
2010 BFD_RELOC_386_PLT32
2011 ENUMX
2012 BFD_RELOC_386_COPY
2013 ENUMX
2014 BFD_RELOC_386_GLOB_DAT
2015 ENUMX
2016 BFD_RELOC_386_JUMP_SLOT
2017 ENUMX
2018 BFD_RELOC_386_RELATIVE
2019 ENUMX
2020 BFD_RELOC_386_GOTOFF
2021 ENUMX
2022 BFD_RELOC_386_GOTPC
2023 ENUMDOC
2024 i386/elf relocations
2025
2026 ENUM
2027 BFD_RELOC_NS32K_IMM_8
2028 ENUMX
2029 BFD_RELOC_NS32K_IMM_16
2030 ENUMX
2031 BFD_RELOC_NS32K_IMM_32
2032 ENUMX
2033 BFD_RELOC_NS32K_IMM_8_PCREL
2034 ENUMX
2035 BFD_RELOC_NS32K_IMM_16_PCREL
2036 ENUMX
2037 BFD_RELOC_NS32K_IMM_32_PCREL
2038 ENUMX
2039 BFD_RELOC_NS32K_DISP_8
2040 ENUMX
2041 BFD_RELOC_NS32K_DISP_16
2042 ENUMX
2043 BFD_RELOC_NS32K_DISP_32
2044 ENUMX
2045 BFD_RELOC_NS32K_DISP_8_PCREL
2046 ENUMX
2047 BFD_RELOC_NS32K_DISP_16_PCREL
2048 ENUMX
2049 BFD_RELOC_NS32K_DISP_32_PCREL
2050 ENUMDOC
2051 ns32k relocations
2052
2053 ENUM
2054 BFD_RELOC_PPC_B26
2055 ENUMX
2056 BFD_RELOC_PPC_BA26
2057 ENUMX
2058 BFD_RELOC_PPC_TOC16
2059 ENUMX
2060 BFD_RELOC_PPC_B16
2061 ENUMX
2062 BFD_RELOC_PPC_B16_BRTAKEN
2063 ENUMX
2064 BFD_RELOC_PPC_B16_BRNTAKEN
2065 ENUMX
2066 BFD_RELOC_PPC_BA16
2067 ENUMX
2068 BFD_RELOC_PPC_BA16_BRTAKEN
2069 ENUMX
2070 BFD_RELOC_PPC_BA16_BRNTAKEN
2071 ENUMX
2072 BFD_RELOC_PPC_COPY
2073 ENUMX
2074 BFD_RELOC_PPC_GLOB_DAT
2075 ENUMX
2076 BFD_RELOC_PPC_JMP_SLOT
2077 ENUMX
2078 BFD_RELOC_PPC_RELATIVE
2079 ENUMX
2080 BFD_RELOC_PPC_LOCAL24PC
2081 ENUMX
2082 BFD_RELOC_PPC_EMB_NADDR32
2083 ENUMX
2084 BFD_RELOC_PPC_EMB_NADDR16
2085 ENUMX
2086 BFD_RELOC_PPC_EMB_NADDR16_LO
2087 ENUMX
2088 BFD_RELOC_PPC_EMB_NADDR16_HI
2089 ENUMX
2090 BFD_RELOC_PPC_EMB_NADDR16_HA
2091 ENUMX
2092 BFD_RELOC_PPC_EMB_SDAI16
2093 ENUMX
2094 BFD_RELOC_PPC_EMB_SDA2I16
2095 ENUMX
2096 BFD_RELOC_PPC_EMB_SDA2REL
2097 ENUMX
2098 BFD_RELOC_PPC_EMB_SDA21
2099 ENUMX
2100 BFD_RELOC_PPC_EMB_MRKREF
2101 ENUMX
2102 BFD_RELOC_PPC_EMB_RELSEC16
2103 ENUMX
2104 BFD_RELOC_PPC_EMB_RELST_LO
2105 ENUMX
2106 BFD_RELOC_PPC_EMB_RELST_HI
2107 ENUMX
2108 BFD_RELOC_PPC_EMB_RELST_HA
2109 ENUMX
2110 BFD_RELOC_PPC_EMB_BIT_FLD
2111 ENUMX
2112 BFD_RELOC_PPC_EMB_RELSDA
2113 ENUMDOC
2114 Power(rs6000) and PowerPC relocations.
2115
2116 ENUM
2117 BFD_RELOC_CTOR
2118 ENUMDOC
2119 The type of reloc used to build a contructor table - at the moment
2120 probably a 32 bit wide absolute relocation, but the target can choose.
2121 It generally does map to one of the other relocation types.
2122
2123 ENUM
2124 BFD_RELOC_ARM_PCREL_BRANCH
2125 ENUMDOC
2126 ARM 26 bit pc-relative branch. The lowest two bits must be zero and are
2127 not stored in the instruction.
2128 ENUM
2129 BFD_RELOC_ARM_IMMEDIATE
2130 ENUMX
2131 BFD_RELOC_ARM_OFFSET_IMM
2132 ENUMX
2133 BFD_RELOC_ARM_SHIFT_IMM
2134 ENUMX
2135 BFD_RELOC_ARM_SWI
2136 ENUMX
2137 BFD_RELOC_ARM_MULTI
2138 ENUMX
2139 BFD_RELOC_ARM_CP_OFF_IMM
2140 ENUMX
2141 BFD_RELOC_ARM_ADR_IMM
2142 ENUMX
2143 BFD_RELOC_ARM_LDR_IMM
2144 ENUMX
2145 BFD_RELOC_ARM_LITERAL
2146 ENUMX
2147 BFD_RELOC_ARM_IN_POOL
2148 ENUMX
2149 BFD_RELOC_ARM_OFFSET_IMM8
2150 ENUMX
2151 BFD_RELOC_ARM_HWLITERAL
2152 ENUMDOC
2153 These relocs are only used within the ARM assembler. They are not
2154 (at present) written to any object files.
2155
2156 COMMENT
2157 {* start-sanitize-arc *}
2158 ENUM
2159 BFD_RELOC_ARC_B22_PCREL
2160 ENUMDOC
2161 Argonaut RISC Core (ARC) relocs.
2162 ARC 22 bit pc-relative branch. The lowest two bits must be zero and are
2163 not stored in the instruction. The high 20 bits are installed in bits 26
2164 through 7 of the instruction.
2165 ENUM
2166 BFD_RELOC_ARC_B26
2167 ENUMDOC
2168 ARC 26 bit absolute branch. The lowest two bits must be zero and are not
2169 stored in the instruction. The high 24 bits are installed in bits 23
2170 through 0.
2171 COMMENT
2172 {* end-sanitize-arc *}
2173
2174 COMMENT
2175 {* start-sanitize-d10v *}
2176 ENUM
2177 BFD_RELOC_D10V_10_PCREL_R
2178 ENUMDOC
2179 Mitsubishi D10V relocs.
2180 This is a 10-bit reloc with the right 2 bits
2181 assumed to be 0.
2182 ENUM
2183 BFD_RELOC_D10V_10_PCREL_L
2184 ENUMDOC
2185 Mitsubishi D10V relocs.
2186 This is a 10-bit reloc with the right 2 bits
2187 assumed to be 0. This is the same as the previous reloc
2188 except it is in the left container, i.e.,
2189 shifted left 15 bits.
2190 ENUM
2191 BFD_RELOC_D10V_18
2192 ENUMDOC
2193 This is an 18-bit reloc with the right 2 bits
2194 assumed to be 0.
2195 ENUM
2196 BFD_RELOC_D10V_18_PCREL
2197 ENUMDOC
2198 This is an 18-bit reloc with the right 2 bits
2199 assumed to be 0.
2200 COMMENT
2201 {* end-sanitize-d10v *}
2202
2203 ENDSENUM
2204 BFD_RELOC_UNUSED
2205 CODE_FRAGMENT
2206 .
2207 .typedef enum bfd_reloc_code_real bfd_reloc_code_real_type;
2208 */
2209
2210
2211 /*
2212 FUNCTION
2213 bfd_reloc_type_lookup
2214
2215 SYNOPSIS
2216 reloc_howto_type *
2217 bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
2218
2219 DESCRIPTION
2220 Return a pointer to a howto structure which, when
2221 invoked, will perform the relocation @var{code} on data from the
2222 architecture noted.
2223
2224 */
2225
2226
2227 reloc_howto_type *
2228 bfd_reloc_type_lookup (abfd, code)
2229 bfd *abfd;
2230 bfd_reloc_code_real_type code;
2231 {
2232 return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
2233 }
2234
2235 static reloc_howto_type bfd_howto_32 =
2236 HOWTO (0, 00, 2, 32, false, 0, complain_overflow_bitfield, 0, "VRT32", false, 0xffffffff, 0xffffffff, true);
2237
2238
2239 /*
2240 INTERNAL_FUNCTION
2241 bfd_default_reloc_type_lookup
2242
2243 SYNOPSIS
2244 reloc_howto_type *bfd_default_reloc_type_lookup
2245 (bfd *abfd, bfd_reloc_code_real_type code);
2246
2247 DESCRIPTION
2248 Provides a default relocation lookup routine for any architecture.
2249
2250
2251 */
2252
2253 reloc_howto_type *
2254 bfd_default_reloc_type_lookup (abfd, code)
2255 bfd *abfd;
2256 bfd_reloc_code_real_type code;
2257 {
2258 switch (code)
2259 {
2260 case BFD_RELOC_CTOR:
2261 /* The type of reloc used in a ctor, which will be as wide as the
2262 address - so either a 64, 32, or 16 bitter. */
2263 switch (bfd_get_arch_info (abfd)->bits_per_address)
2264 {
2265 case 64:
2266 BFD_FAIL ();
2267 case 32:
2268 return &bfd_howto_32;
2269 case 16:
2270 BFD_FAIL ();
2271 default:
2272 BFD_FAIL ();
2273 }
2274 default:
2275 BFD_FAIL ();
2276 }
2277 return (reloc_howto_type *) NULL;
2278 }
2279
2280 /*
2281 FUNCTION
2282 bfd_get_reloc_code_name
2283
2284 SYNOPSIS
2285 const char *bfd_get_reloc_code_name (bfd_reloc_code_real_type code);
2286
2287 DESCRIPTION
2288 Provides a printable name for the supplied relocation code.
2289 Useful mainly for printing error messages.
2290 */
2291
2292 const char *
2293 bfd_get_reloc_code_name (code)
2294 bfd_reloc_code_real_type code;
2295 {
2296 if (code > BFD_RELOC_UNUSED)
2297 return 0;
2298 return bfd_reloc_code_real_names[(int)code];
2299 }
2300
2301 /*
2302 INTERNAL_FUNCTION
2303 bfd_generic_relax_section
2304
2305 SYNOPSIS
2306 boolean bfd_generic_relax_section
2307 (bfd *abfd,
2308 asection *section,
2309 struct bfd_link_info *,
2310 boolean *);
2311
2312 DESCRIPTION
2313 Provides default handling for relaxing for back ends which
2314 don't do relaxing -- i.e., does nothing.
2315 */
2316
2317 /*ARGSUSED*/
2318 boolean
2319 bfd_generic_relax_section (abfd, section, link_info, again)
2320 bfd *abfd;
2321 asection *section;
2322 struct bfd_link_info *link_info;
2323 boolean *again;
2324 {
2325 *again = false;
2326 return true;
2327 }
2328
2329 /*
2330 INTERNAL_FUNCTION
2331 bfd_generic_get_relocated_section_contents
2332
2333 SYNOPSIS
2334 bfd_byte *
2335 bfd_generic_get_relocated_section_contents (bfd *abfd,
2336 struct bfd_link_info *link_info,
2337 struct bfd_link_order *link_order,
2338 bfd_byte *data,
2339 boolean relocateable,
2340 asymbol **symbols);
2341
2342 DESCRIPTION
2343 Provides default handling of relocation effort for back ends
2344 which can't be bothered to do it efficiently.
2345
2346 */
2347
2348 bfd_byte *
2349 bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data,
2350 relocateable, symbols)
2351 bfd *abfd;
2352 struct bfd_link_info *link_info;
2353 struct bfd_link_order *link_order;
2354 bfd_byte *data;
2355 boolean relocateable;
2356 asymbol **symbols;
2357 {
2358 /* Get enough memory to hold the stuff */
2359 bfd *input_bfd = link_order->u.indirect.section->owner;
2360 asection *input_section = link_order->u.indirect.section;
2361
2362 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
2363 arelent **reloc_vector = NULL;
2364 long reloc_count;
2365
2366 if (reloc_size < 0)
2367 goto error_return;
2368
2369 reloc_vector = (arelent **) bfd_malloc ((size_t) reloc_size);
2370 if (reloc_vector == NULL && reloc_size != 0)
2371 goto error_return;
2372
2373 /* read in the section */
2374 if (!bfd_get_section_contents (input_bfd,
2375 input_section,
2376 (PTR) data,
2377 0,
2378 input_section->_raw_size))
2379 goto error_return;
2380
2381 /* We're not relaxing the section, so just copy the size info */
2382 input_section->_cooked_size = input_section->_raw_size;
2383 input_section->reloc_done = true;
2384
2385 reloc_count = bfd_canonicalize_reloc (input_bfd,
2386 input_section,
2387 reloc_vector,
2388 symbols);
2389 if (reloc_count < 0)
2390 goto error_return;
2391
2392 if (reloc_count > 0)
2393 {
2394 arelent **parent;
2395 for (parent = reloc_vector; *parent != (arelent *) NULL;
2396 parent++)
2397 {
2398 char *error_message = (char *) NULL;
2399 bfd_reloc_status_type r =
2400 bfd_perform_relocation (input_bfd,
2401 *parent,
2402 (PTR) data,
2403 input_section,
2404 relocateable ? abfd : (bfd *) NULL,
2405 &error_message);
2406
2407 if (relocateable)
2408 {
2409 asection *os = input_section->output_section;
2410
2411 /* A partial link, so keep the relocs */
2412 os->orelocation[os->reloc_count] = *parent;
2413 os->reloc_count++;
2414 }
2415
2416 if (r != bfd_reloc_ok)
2417 {
2418 switch (r)
2419 {
2420 case bfd_reloc_undefined:
2421 if (!((*link_info->callbacks->undefined_symbol)
2422 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
2423 input_bfd, input_section, (*parent)->address)))
2424 goto error_return;
2425 break;
2426 case bfd_reloc_dangerous:
2427 BFD_ASSERT (error_message != (char *) NULL);
2428 if (!((*link_info->callbacks->reloc_dangerous)
2429 (link_info, error_message, input_bfd, input_section,
2430 (*parent)->address)))
2431 goto error_return;
2432 break;
2433 case bfd_reloc_overflow:
2434 if (!((*link_info->callbacks->reloc_overflow)
2435 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
2436 (*parent)->howto->name, (*parent)->addend,
2437 input_bfd, input_section, (*parent)->address)))
2438 goto error_return;
2439 break;
2440 case bfd_reloc_outofrange:
2441 default:
2442 abort ();
2443 break;
2444 }
2445
2446 }
2447 }
2448 }
2449 if (reloc_vector != NULL)
2450 free (reloc_vector);
2451 return data;
2452
2453 error_return:
2454 if (reloc_vector != NULL)
2455 free (reloc_vector);
2456 return NULL;
2457 }