]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/reloc.c
2000-03-11 Mark Kettenis <kettenis@gnu.org>
[thirdparty/binutils-gdb.git] / bfd / reloc.c
CommitLineData
252b5132 1/* BFD support for handling relocation entries.
5b93d8bb 2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
252b5132
RH
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22/*
23SECTION
24 Relocations
25
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.
31
32 Relocations are maintained on a per section basis,
33 while symbols are maintained on a per BFD basis.
34
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.
38
39@menu
40@* typedef arelent::
41@* howto manager::
42@end menu
43
44*/
45
46/* DO compile in the reloc_code name table from libbfd.h. */
47#define _BFD_MAKE_TABLE_bfd_reloc_code_real
48
49#include "bfd.h"
50#include "sysdep.h"
51#include "bfdlink.h"
52#include "libbfd.h"
53/*
54DOCDD
55INODE
56 typedef arelent, howto manager, Relocations, Relocations
57
58SUBSECTION
59 typedef arelent
60
61 This is the structure of a relocation entry:
62
63CODE_FRAGMENT
64.
65.typedef enum bfd_reloc_status
66.{
67. {* No errors detected *}
68. bfd_reloc_ok,
69.
70. {* The relocation was performed, but there was an overflow. *}
71. bfd_reloc_overflow,
72.
73. {* The address to relocate was not within the section supplied. *}
74. bfd_reloc_outofrange,
75.
76. {* Used by special functions *}
77. bfd_reloc_continue,
78.
79. {* Unsupported relocation size requested. *}
80. bfd_reloc_notsupported,
81.
82. {* Unused *}
83. bfd_reloc_other,
84.
85. {* The symbol to relocate against was undefined. *}
86. bfd_reloc_undefined,
87.
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. *}
92. bfd_reloc_dangerous
93. }
94. bfd_reloc_status_type;
95.
96.
97.typedef struct reloc_cache_entry
98.{
99. {* A pointer into the canonical table of pointers *}
100. struct symbol_cache_entry **sym_ptr_ptr;
101.
102. {* offset in section *}
103. bfd_size_type address;
104.
105. {* addend for relocation value *}
106. bfd_vma addend;
107.
108. {* Pointer to how to perform the required relocation *}
109. reloc_howto_type *howto;
110.
111.} arelent;
112
113*/
114
115/*
116DESCRIPTION
117
118 Here is a description of each of the fields within an <<arelent>>:
119
120 o <<sym_ptr_ptr>>
121
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.
132
133 o <<address>>
134
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
141 world.
142
143 o <<addend>>
144
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:
148
149
150| char foo[];
151| main()
152| {
153| return foo[0x12345678];
154| }
155
156 Could be compiled into:
157
158| linkw fp,#-4
159| moveb @@#12345678,d0
160| extbl d0
161| unlk fp
162| rts
163
164
165 This could create a reloc pointing to <<foo>>, but leave the
166 offset in the data, something like:
167
168
169|RELOCATION RECORDS FOR [.text]:
170|offset type value
171|00000006 32 _foo
172|
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
177|0000000e 4e75 ; rts
178
179
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:
183
184
185| or.u r13,r0,hi16(_foo+0x12345678)
186| ld.b r2,r13,lo16(_foo+0x12345678)
187| jmp r1
188
189
190 This should create two relocs, both pointing to <<_foo>>, and with
191 0x12340000 in their addend field. The data would consist of:
192
193
194|RELOCATION RECORDS FOR [.text]:
195|offset type value
196|00000002 HVRT16 _foo+0x12340000
197|00000006 LVRT16 _foo+0x12340000
198|
199|00000000 5da05678 ; or.u r13,r0,0x5678
200|00000004 1c4d5678 ; ld.b r2,r13,0x5678
201|00000008 f400c001 ; jmp r1
202
203
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.
208
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.
216
217| save %sp,-112,%sp
218| sethi %hi(_foo+0x12345678),%g2
219| ldsb [%g2+%lo(_foo+0x12345678)],%i0
220| ret
221| restore
222
223 Both relocs contain a pointer to <<foo>>, and the offsets
224 contain junk.
225
226
227|RELOCATION RECORDS FOR [.text]:
228|offset type value
229|00000004 HI22 _foo+0x12345678
230|00000008 LO10 _foo+0x12345678
231|
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
237
238
239 o <<howto>>
240
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.
248
249*/
250
251/*
252SUBSUBSECTION
253 <<enum complain_overflow>>
254
255 Indicates what sort of overflow checking should be done when
256 performing a relocation.
257
258CODE_FRAGMENT
259.
260.enum complain_overflow
261.{
262. {* Do not complain on overflow. *}
263. complain_overflow_dont,
264.
265. {* Complain if the bitfield overflows, whether it is considered
266. as signed or unsigned. *}
267. complain_overflow_bitfield,
268.
269. {* Complain if the value overflows when considered as signed
270. number. *}
271. complain_overflow_signed,
272.
273. {* Complain if the value overflows when considered as an
274. unsigned number. *}
275. complain_overflow_unsigned
276.};
277
278*/
279
280/*
281SUBSUBSECTION
282 <<reloc_howto_type>>
283
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.
286
287CODE_FRAGMENT
288.struct symbol_cache_entry; {* Forward declaration *}
289.
290.struct reloc_howto_struct
291.{
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. *}
298. unsigned int type;
299.
300. {* The value the final relocation is shifted right by. This drops
301. unwanted data from the relocation. *}
302. unsigned int rightshift;
303.
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. *}
307. int size;
308.
309. {* The number of bits in the item to be relocated. This is used
310. when doing overflow checking. *}
311. unsigned int bitsize;
312.
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;
318.
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;
322.
323. {* What type of overflow error should be checked for when
324. relocating. *}
325. enum complain_overflow complain_on_overflow;
326.
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
330. instructions). *}
331. bfd_reloc_status_type (*special_function)
332. PARAMS ((bfd *abfd,
333. arelent *reloc_entry,
334. struct symbol_cache_entry *symbol,
335. PTR data,
336. asection *input_section,
337. bfd *output_bfd,
338. char **error_message));
339.
340. {* The textual name of the relocation type. *}
341. char *name;
342.
c1b7949f
DE
343. {* Some formats record a relocation addend in the section contents
344. rather than with the relocation. For ELF formats this is the
345. distinction between USE_REL and USE_RELA (though the code checks
346. for USE_REL == 1/0). The value of this field is TRUE if the
347. addend is recorded with the section contents; when performing a
348. partial link (ld -r) the section contents (the data) will be
349. modified. The value of this field is FALSE if addends are
350. recorded with the relocation (in arelent.addend); when performing
351. a partial link the relocation will be modified.
352. All relocations for all ELF USE_RELA targets should set this field
353. to FALSE (values of TRUE should be looked on with suspicion).
354. However, the converse is not true: not all relocations of all ELF
355. USE_REL targets set this field to TRUE. Why this is so is peculiar
356. to each particular target. For relocs that aren't used in partial
357. links (e.g. GOT stuff) it doesn't matter what this is set to. *}
252b5132
RH
358. boolean partial_inplace;
359.
360. {* The src_mask selects which parts of the read in data
361. are to be used in the relocation sum. E.g., if this was an 8 bit
362. bit of data which we read and relocated, this would be
363. 0x000000ff. When we have relocs which have an addend, such as
364. sun4 extended relocs, the value in the offset part of a
365. relocating field is garbage so we never use it. In this case
366. the mask would be 0x00000000. *}
367. bfd_vma src_mask;
368.
369. {* The dst_mask selects which parts of the instruction are replaced
370. into the instruction. In most cases src_mask == dst_mask,
371. except in the above special case, where dst_mask would be
372. 0x000000ff, and src_mask would be 0x00000000. *}
373. bfd_vma dst_mask;
374.
375. {* When some formats create PC relative instructions, they leave
376. the value of the pc of the place being relocated in the offset
377. slot of the instruction, so that a PC relative relocation can
378. be made just by adding in an ordinary offset (e.g., sun3 a.out).
379. Some formats leave the displacement part of an instruction
380. empty (e.g., m88k bcs); this flag signals the fact.*}
381. boolean pcrel_offset;
382.
383.};
384
385*/
386
387/*
388FUNCTION
389 The HOWTO Macro
390
391DESCRIPTION
392 The HOWTO define is horrible and will go away.
393
394
395.#define HOWTO(C, R,S,B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
396. {(unsigned)C,R,S,B, P, BI, O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
397
398DESCRIPTION
399 And will be replaced with the totally magic way. But for the
400 moment, we are compatible, so do it this way.
401
402
403.#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,complain_overflow_dont,FUNCTION, NAME,false,0,0,IN)
404.
5f771d47
ILT
405
406DESCRIPTION
407 This is used to fill in an empty howto entry in an array.
408
409.#define EMPTY_HOWTO(C) \
410. HOWTO((C),0,0,0,false,0,complain_overflow_dont,NULL,NULL,false,0,0,false)
411.
412
252b5132
RH
413DESCRIPTION
414 Helper routine to turn a symbol into a relocation value.
415
416.#define HOWTO_PREPARE(relocation, symbol) \
417. { \
418. if (symbol != (asymbol *)NULL) { \
419. if (bfd_is_com_section (symbol->section)) { \
420. relocation = 0; \
421. } \
422. else { \
423. relocation = symbol->value; \
424. } \
425. } \
426.}
427
428*/
429
430/*
431FUNCTION
432 bfd_get_reloc_size
433
434SYNOPSIS
435 unsigned int bfd_get_reloc_size (reloc_howto_type *);
436
437DESCRIPTION
438 For a reloc_howto_type that operates on a fixed number of bytes,
439 this returns the number of bytes operated on.
440 */
441
442unsigned int
443bfd_get_reloc_size (howto)
444 reloc_howto_type *howto;
445{
446 switch (howto->size)
447 {
448 case 0: return 1;
449 case 1: return 2;
450 case 2: return 4;
451 case 3: return 0;
452 case 4: return 8;
453 case 8: return 16;
454 case -2: return 4;
455 default: abort ();
456 }
457}
458
459/*
460TYPEDEF
461 arelent_chain
462
463DESCRIPTION
464
465 How relocs are tied together in an <<asection>>:
466
467.typedef struct relent_chain {
468. arelent relent;
469. struct relent_chain *next;
470.} arelent_chain;
471
472*/
473
474/* N_ONES produces N one bits, without overflowing machine arithmetic. */
475#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
476
477/*
478FUNCTION
479 bfd_check_overflow
480
481SYNOPSIS
482 bfd_reloc_status_type
483 bfd_check_overflow
484 (enum complain_overflow how,
485 unsigned int bitsize,
486 unsigned int rightshift,
487 unsigned int addrsize,
488 bfd_vma relocation);
489
490DESCRIPTION
491 Perform overflow checking on @var{relocation} which has
492 @var{bitsize} significant bits and will be shifted right by
493 @var{rightshift} bits, on a machine with addresses containing
494 @var{addrsize} significant bits. The result is either of
495 @code{bfd_reloc_ok} or @code{bfd_reloc_overflow}.
496
497*/
498
499bfd_reloc_status_type
500bfd_check_overflow (how, bitsize, rightshift, addrsize, relocation)
501 enum complain_overflow how;
502 unsigned int bitsize;
503 unsigned int rightshift;
504 unsigned int addrsize;
505 bfd_vma relocation;
506{
507 bfd_vma fieldmask, addrmask, signmask, ss, a;
508 bfd_reloc_status_type flag = bfd_reloc_ok;
509
510 a = relocation;
511
512 /* Note: BITSIZE should always be <= ADDRSIZE, but in case it's not,
513 we'll be permissive: extra bits in the field mask will
514 automatically extend the address mask for purposes of the
515 overflow check. */
516 fieldmask = N_ONES (bitsize);
517 addrmask = N_ONES (addrsize) | fieldmask;
518
519 switch (how)
520 {
521 case complain_overflow_dont:
522 break;
523
524 case complain_overflow_signed:
525 /* If any sign bits are set, all sign bits must be set. That
526 is, A must be a valid negative address after shifting. */
527 a = (a & addrmask) >> rightshift;
528 signmask = ~ (fieldmask >> 1);
529 ss = a & signmask;
530 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
531 flag = bfd_reloc_overflow;
532 break;
533
534 case complain_overflow_unsigned:
535 /* We have an overflow if the address does not fit in the field. */
536 a = (a & addrmask) >> rightshift;
537 if ((a & ~ fieldmask) != 0)
538 flag = bfd_reloc_overflow;
539 break;
540
541 case complain_overflow_bitfield:
542 /* Bitfields are sometimes signed, sometimes unsigned. We
543 overflow if the value has some, but not all, bits set outside
544 the field, or if it has any bits set outside the field but
545 the sign bit is not set. */
546 a >>= rightshift;
547 if ((a & ~ fieldmask) != 0)
548 {
549 signmask = (fieldmask >> 1) + 1;
550 ss = (signmask << rightshift) - 1;
551 if ((ss | relocation) != ~ (bfd_vma) 0)
552 flag = bfd_reloc_overflow;
553 }
554 break;
555
556 default:
557 abort ();
558 }
559
560 return flag;
561}
562
563/*
564FUNCTION
565 bfd_perform_relocation
566
567SYNOPSIS
568 bfd_reloc_status_type
569 bfd_perform_relocation
570 (bfd *abfd,
571 arelent *reloc_entry,
572 PTR data,
573 asection *input_section,
574 bfd *output_bfd,
575 char **error_message);
576
577DESCRIPTION
578 If @var{output_bfd} is supplied to this function, the
579 generated image will be relocatable; the relocations are
580 copied to the output file after they have been changed to
581 reflect the new state of the world. There are two ways of
582 reflecting the results of partial linkage in an output file:
583 by modifying the output data in place, and by modifying the
584 relocation record. Some native formats (e.g., basic a.out and
585 basic coff) have no way of specifying an addend in the
586 relocation type, so the addend has to go in the output data.
587 This is no big deal since in these formats the output data
588 slot will always be big enough for the addend. Complex reloc
589 types with addends were invented to solve just this problem.
590 The @var{error_message} argument is set to an error message if
591 this return @code{bfd_reloc_dangerous}.
592
593*/
594
595
596bfd_reloc_status_type
597bfd_perform_relocation (abfd, reloc_entry, data, input_section, output_bfd,
598 error_message)
599 bfd *abfd;
600 arelent *reloc_entry;
601 PTR data;
602 asection *input_section;
603 bfd *output_bfd;
604 char **error_message;
605{
606 bfd_vma relocation;
607 bfd_reloc_status_type flag = bfd_reloc_ok;
9a968f43 608 bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
252b5132
RH
609 bfd_vma output_base = 0;
610 reloc_howto_type *howto = reloc_entry->howto;
611 asection *reloc_target_output_section;
612 asymbol *symbol;
613
614 symbol = *(reloc_entry->sym_ptr_ptr);
615 if (bfd_is_abs_section (symbol->section)
616 && output_bfd != (bfd *) NULL)
617 {
618 reloc_entry->address += input_section->output_offset;
619 return bfd_reloc_ok;
620 }
621
622 /* If we are not producing relocateable output, return an error if
623 the symbol is not defined. An undefined weak symbol is
624 considered to have a value of zero (SVR4 ABI, p. 4-27). */
625 if (bfd_is_und_section (symbol->section)
626 && (symbol->flags & BSF_WEAK) == 0
627 && output_bfd == (bfd *) NULL)
628 flag = bfd_reloc_undefined;
629
630 /* If there is a function supplied to handle this relocation type,
631 call it. It'll return `bfd_reloc_continue' if further processing
632 can be done. */
633 if (howto->special_function)
634 {
635 bfd_reloc_status_type cont;
636 cont = howto->special_function (abfd, reloc_entry, symbol, data,
637 input_section, output_bfd,
638 error_message);
639 if (cont != bfd_reloc_continue)
640 return cont;
641 }
642
643 /* Is the address of the relocation really within the section? */
9a968f43
NC
644 if (reloc_entry->address > input_section->_cooked_size /
645 bfd_octets_per_byte (abfd))
252b5132
RH
646 return bfd_reloc_outofrange;
647
648 /* Work out which section the relocation is targetted at and the
649 initial relocation command value. */
650
651 /* Get symbol value. (Common symbols are special.) */
652 if (bfd_is_com_section (symbol->section))
653 relocation = 0;
654 else
655 relocation = symbol->value;
656
657
658 reloc_target_output_section = symbol->section->output_section;
659
660 /* Convert input-section-relative symbol value to absolute. */
661 if (output_bfd && howto->partial_inplace == false)
662 output_base = 0;
663 else
664 output_base = reloc_target_output_section->vma;
665
666 relocation += output_base + symbol->section->output_offset;
667
668 /* Add in supplied addend. */
669 relocation += reloc_entry->addend;
670
671 /* Here the variable relocation holds the final address of the
672 symbol we are relocating against, plus any addend. */
673
674 if (howto->pc_relative == true)
675 {
676 /* This is a PC relative relocation. We want to set RELOCATION
677 to the distance between the address of the symbol and the
678 location. RELOCATION is already the address of the symbol.
679
680 We start by subtracting the address of the section containing
681 the location.
682
683 If pcrel_offset is set, we must further subtract the position
684 of the location within the section. Some targets arrange for
685 the addend to be the negative of the position of the location
686 within the section; for example, i386-aout does this. For
687 i386-aout, pcrel_offset is false. Some other targets do not
688 include the position of the location; for example, m88kbcs,
689 or ELF. For those targets, pcrel_offset is true.
690
691 If we are producing relocateable output, then we must ensure
692 that this reloc will be correctly computed when the final
693 relocation is done. If pcrel_offset is false we want to wind
694 up with the negative of the location within the section,
695 which means we must adjust the existing addend by the change
696 in the location within the section. If pcrel_offset is true
697 we do not want to adjust the existing addend at all.
698
699 FIXME: This seems logical to me, but for the case of
700 producing relocateable output it is not what the code
701 actually does. I don't want to change it, because it seems
702 far too likely that something will break. */
703
704 relocation -=
705 input_section->output_section->vma + input_section->output_offset;
706
707 if (howto->pcrel_offset == true)
708 relocation -= reloc_entry->address;
709 }
710
711 if (output_bfd != (bfd *) NULL)
712 {
713 if (howto->partial_inplace == false)
714 {
715 /* This is a partial relocation, and we want to apply the relocation
716 to the reloc entry rather than the raw data. Modify the reloc
717 inplace to reflect what we now know. */
718 reloc_entry->addend = relocation;
719 reloc_entry->address += input_section->output_offset;
720 return flag;
721 }
722 else
723 {
724 /* This is a partial relocation, but inplace, so modify the
725 reloc record a bit.
726
727 If we've relocated with a symbol with a section, change
728 into a ref to the section belonging to the symbol. */
729
730 reloc_entry->address += input_section->output_offset;
731
732 /* WTF?? */
733 if (abfd->xvec->flavour == bfd_target_coff_flavour
734 && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
735 && strcmp (abfd->xvec->name, "xcoff-powermac") != 0
736 && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
737 && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
738 {
739#if 1
740 /* For m68k-coff, the addend was being subtracted twice during
741 relocation with -r. Removing the line below this comment
742 fixes that problem; see PR 2953.
743
744However, Ian wrote the following, regarding removing the line below,
745which explains why it is still enabled: --djm
746
747If you put a patch like that into BFD you need to check all the COFF
748linkers. I am fairly certain that patch will break coff-i386 (e.g.,
749SCO); see coff_i386_reloc in coff-i386.c where I worked around the
750problem in a different way. There may very well be a reason that the
751code works as it does.
752
753Hmmm. The first obvious point is that bfd_perform_relocation should
754not have any tests that depend upon the flavour. It's seem like
755entirely the wrong place for such a thing. The second obvious point
756is that the current code ignores the reloc addend when producing
757relocateable output for COFF. That's peculiar. In fact, I really
758have no idea what the point of the line you want to remove is.
759
760A typical COFF reloc subtracts the old value of the symbol and adds in
761the new value to the location in the object file (if it's a pc
762relative reloc it adds the difference between the symbol value and the
763location). When relocating we need to preserve that property.
764
765BFD handles this by setting the addend to the negative of the old
766value of the symbol. Unfortunately it handles common symbols in a
767non-standard way (it doesn't subtract the old value) but that's a
768different story (we can't change it without losing backward
769compatibility with old object files) (coff-i386 does subtract the old
770value, to be compatible with existing coff-i386 targets, like SCO).
771
772So everything works fine when not producing relocateable output. When
773we are producing relocateable output, logically we should do exactly
774what we do when not producing relocateable output. Therefore, your
775patch is correct. In fact, it should probably always just set
776reloc_entry->addend to 0 for all cases, since it is, in fact, going to
777add the value into the object file. This won't hurt the COFF code,
778which doesn't use the addend; I'm not sure what it will do to other
779formats (the thing to check for would be whether any formats both use
780the addend and set partial_inplace).
781
782When I wanted to make coff-i386 produce relocateable output, I ran
783into the problem that you are running into: I wanted to remove that
784line. Rather than risk it, I made the coff-i386 relocs use a special
785function; it's coff_i386_reloc in coff-i386.c. The function
786specifically adds the addend field into the object file, knowing that
787bfd_perform_relocation is not going to. If you remove that line, then
788coff-i386.c will wind up adding the addend field in twice. It's
789trivial to fix; it just needs to be done.
790
791The problem with removing the line is just that it may break some
792working code. With BFD it's hard to be sure of anything. The right
793way to deal with this is simply to build and test at least all the
794supported COFF targets. It should be straightforward if time and disk
795space consuming. For each target:
796 1) build the linker
797 2) generate some executable, and link it using -r (I would
798 probably use paranoia.o and link against newlib/libc.a, which
799 for all the supported targets would be available in
800 /usr/cygnus/progressive/H-host/target/lib/libc.a).
801 3) make the change to reloc.c
802 4) rebuild the linker
803 5) repeat step 2
804 6) if the resulting object files are the same, you have at least
805 made it no worse
806 7) if they are different you have to figure out which version is
807 right
808*/
809 relocation -= reloc_entry->addend;
810#endif
811 reloc_entry->addend = 0;
812 }
813 else
814 {
815 reloc_entry->addend = relocation;
816 }
817 }
818 }
819 else
820 {
821 reloc_entry->addend = 0;
822 }
823
824 /* FIXME: This overflow checking is incomplete, because the value
825 might have overflowed before we get here. For a correct check we
826 need to compute the value in a size larger than bitsize, but we
827 can't reasonably do that for a reloc the same size as a host
828 machine word.
829 FIXME: We should also do overflow checking on the result after
830 adding in the value contained in the object file. */
831 if (howto->complain_on_overflow != complain_overflow_dont
832 && flag == bfd_reloc_ok)
833 flag = bfd_check_overflow (howto->complain_on_overflow,
834 howto->bitsize,
835 howto->rightshift,
836 bfd_arch_bits_per_address (abfd),
837 relocation);
838
839 /*
840 Either we are relocating all the way, or we don't want to apply
841 the relocation to the reloc entry (probably because there isn't
842 any room in the output format to describe addends to relocs)
843 */
844
845 /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
846 (OSF version 1.3, compiler version 3.11). It miscompiles the
847 following program:
848
849 struct str
850 {
851 unsigned int i0;
852 } s = { 0 };
853
854 int
855 main ()
856 {
857 unsigned long x;
858
859 x = 0x100000000;
860 x <<= (unsigned long) s.i0;
861 if (x == 0)
862 printf ("failed\n");
863 else
864 printf ("succeeded (%lx)\n", x);
865 }
866 */
867
868 relocation >>= (bfd_vma) howto->rightshift;
869
870 /* Shift everything up to where it's going to be used */
871
872 relocation <<= (bfd_vma) howto->bitpos;
873
874 /* Wait for the day when all have the mask in them */
875
876 /* What we do:
877 i instruction to be left alone
878 o offset within instruction
879 r relocation offset to apply
880 S src mask
881 D dst mask
882 N ~dst mask
883 A part 1
884 B part 2
885 R result
886
887 Do this:
888 i i i i i o o o o o from bfd_get<size>
889 and S S S S S to get the size offset we want
890 + r r r r r r r r r r to get the final value to place
891 and D D D D D to chop to right size
892 -----------------------
893 A A A A A
894 And this:
895 ... i i i i i o o o o o from bfd_get<size>
896 and N N N N N get instruction
897 -----------------------
898 ... B B B B B
899
900 And then:
901 B B B B B
902 or A A A A A
903 -----------------------
904 R R R R R R R R R R put into bfd_put<size>
905 */
906
907#define DOIT(x) \
908 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
909
910 switch (howto->size)
911 {
912 case 0:
913 {
9a968f43 914 char x = bfd_get_8 (abfd, (char *) data + octets);
252b5132 915 DOIT (x);
9a968f43 916 bfd_put_8 (abfd, x, (unsigned char *) data + octets);
252b5132
RH
917 }
918 break;
919
920 case 1:
921 {
9a968f43 922 short x = bfd_get_16 (abfd, (bfd_byte *) data + octets);
252b5132 923 DOIT (x);
9a968f43 924 bfd_put_16 (abfd, x, (unsigned char *) data + octets);
252b5132
RH
925 }
926 break;
927 case 2:
928 {
9a968f43 929 long x = bfd_get_32 (abfd, (bfd_byte *) data + octets);
252b5132 930 DOIT (x);
9a968f43 931 bfd_put_32 (abfd, x, (bfd_byte *) data + octets);
252b5132
RH
932 }
933 break;
934 case -2:
935 {
9a968f43 936 long x = bfd_get_32 (abfd, (bfd_byte *) data + octets);
252b5132
RH
937 relocation = -relocation;
938 DOIT (x);
9a968f43 939 bfd_put_32 (abfd, x, (bfd_byte *) data + octets);
252b5132
RH
940 }
941 break;
942
943 case -1:
944 {
9a968f43 945 long x = bfd_get_16 (abfd, (bfd_byte *) data + octets);
252b5132
RH
946 relocation = -relocation;
947 DOIT (x);
9a968f43 948 bfd_put_16 (abfd, x, (bfd_byte *) data + octets);
252b5132
RH
949 }
950 break;
951
952 case 3:
953 /* Do nothing */
954 break;
955
956 case 4:
957#ifdef BFD64
958 {
9a968f43 959 bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + octets);
252b5132 960 DOIT (x);
9a968f43 961 bfd_put_64 (abfd, x, (bfd_byte *) data + octets);
252b5132
RH
962 }
963#else
964 abort ();
965#endif
966 break;
967 default:
968 return bfd_reloc_other;
969 }
970
971 return flag;
972}
973
974/*
975FUNCTION
976 bfd_install_relocation
977
978SYNOPSIS
979 bfd_reloc_status_type
980 bfd_install_relocation
981 (bfd *abfd,
982 arelent *reloc_entry,
983 PTR data, bfd_vma data_start,
984 asection *input_section,
985 char **error_message);
986
987DESCRIPTION
988 This looks remarkably like <<bfd_perform_relocation>>, except it
989 does not expect that the section contents have been filled in.
990 I.e., it's suitable for use when creating, rather than applying
991 a relocation.
992
993 For now, this function should be considered reserved for the
994 assembler.
995
996*/
997
998
999bfd_reloc_status_type
1000bfd_install_relocation (abfd, reloc_entry, data_start, data_start_offset,
1001 input_section, error_message)
1002 bfd *abfd;
1003 arelent *reloc_entry;
1004 PTR data_start;
1005 bfd_vma data_start_offset;
1006 asection *input_section;
1007 char **error_message;
1008{
1009 bfd_vma relocation;
1010 bfd_reloc_status_type flag = bfd_reloc_ok;
9a968f43 1011 bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
252b5132
RH
1012 bfd_vma output_base = 0;
1013 reloc_howto_type *howto = reloc_entry->howto;
1014 asection *reloc_target_output_section;
1015 asymbol *symbol;
1016 bfd_byte *data;
1017
1018 symbol = *(reloc_entry->sym_ptr_ptr);
1019 if (bfd_is_abs_section (symbol->section))
1020 {
1021 reloc_entry->address += input_section->output_offset;
1022 return bfd_reloc_ok;
1023 }
1024
1025 /* If there is a function supplied to handle this relocation type,
1026 call it. It'll return `bfd_reloc_continue' if further processing
1027 can be done. */
1028 if (howto->special_function)
1029 {
1030 bfd_reloc_status_type cont;
1031
1032 /* XXX - The special_function calls haven't been fixed up to deal
1033 with creating new relocations and section contents. */
1034 cont = howto->special_function (abfd, reloc_entry, symbol,
1035 /* XXX - Non-portable! */
1036 ((bfd_byte *) data_start
1037 - data_start_offset),
1038 input_section, abfd, error_message);
1039 if (cont != bfd_reloc_continue)
1040 return cont;
1041 }
1042
1043 /* Is the address of the relocation really within the section? */
1044 if (reloc_entry->address > input_section->_cooked_size)
1045 return bfd_reloc_outofrange;
1046
1047 /* Work out which section the relocation is targetted at and the
1048 initial relocation command value. */
1049
1050 /* Get symbol value. (Common symbols are special.) */
1051 if (bfd_is_com_section (symbol->section))
1052 relocation = 0;
1053 else
1054 relocation = symbol->value;
1055
1056 reloc_target_output_section = symbol->section->output_section;
1057
1058 /* Convert input-section-relative symbol value to absolute. */
1059 if (howto->partial_inplace == false)
1060 output_base = 0;
1061 else
1062 output_base = reloc_target_output_section->vma;
1063
1064 relocation += output_base + symbol->section->output_offset;
1065
1066 /* Add in supplied addend. */
1067 relocation += reloc_entry->addend;
1068
1069 /* Here the variable relocation holds the final address of the
1070 symbol we are relocating against, plus any addend. */
1071
1072 if (howto->pc_relative == true)
1073 {
1074 /* This is a PC relative relocation. We want to set RELOCATION
1075 to the distance between the address of the symbol and the
1076 location. RELOCATION is already the address of the symbol.
1077
1078 We start by subtracting the address of the section containing
1079 the location.
1080
1081 If pcrel_offset is set, we must further subtract the position
1082 of the location within the section. Some targets arrange for
1083 the addend to be the negative of the position of the location
1084 within the section; for example, i386-aout does this. For
1085 i386-aout, pcrel_offset is false. Some other targets do not
1086 include the position of the location; for example, m88kbcs,
1087 or ELF. For those targets, pcrel_offset is true.
1088
1089 If we are producing relocateable output, then we must ensure
1090 that this reloc will be correctly computed when the final
1091 relocation is done. If pcrel_offset is false we want to wind
1092 up with the negative of the location within the section,
1093 which means we must adjust the existing addend by the change
1094 in the location within the section. If pcrel_offset is true
1095 we do not want to adjust the existing addend at all.
1096
1097 FIXME: This seems logical to me, but for the case of
1098 producing relocateable output it is not what the code
1099 actually does. I don't want to change it, because it seems
1100 far too likely that something will break. */
1101
1102 relocation -=
1103 input_section->output_section->vma + input_section->output_offset;
1104
1105 if (howto->pcrel_offset == true && howto->partial_inplace == true)
1106 relocation -= reloc_entry->address;
1107 }
1108
1109 if (howto->partial_inplace == false)
1110 {
1111 /* This is a partial relocation, and we want to apply the relocation
1112 to the reloc entry rather than the raw data. Modify the reloc
1113 inplace to reflect what we now know. */
1114 reloc_entry->addend = relocation;
1115 reloc_entry->address += input_section->output_offset;
1116 return flag;
1117 }
1118 else
1119 {
1120 /* This is a partial relocation, but inplace, so modify the
1121 reloc record a bit.
1122
1123 If we've relocated with a symbol with a section, change
1124 into a ref to the section belonging to the symbol. */
1125
1126 reloc_entry->address += input_section->output_offset;
1127
1128 /* WTF?? */
1129 if (abfd->xvec->flavour == bfd_target_coff_flavour
1130 && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0
1131 && strcmp (abfd->xvec->name, "xcoff-powermac") != 0
1132 && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
1133 && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
1134 {
1135#if 1
1136/* For m68k-coff, the addend was being subtracted twice during
1137 relocation with -r. Removing the line below this comment
1138 fixes that problem; see PR 2953.
1139
1140However, Ian wrote the following, regarding removing the line below,
1141which explains why it is still enabled: --djm
1142
1143If you put a patch like that into BFD you need to check all the COFF
1144linkers. I am fairly certain that patch will break coff-i386 (e.g.,
1145SCO); see coff_i386_reloc in coff-i386.c where I worked around the
1146problem in a different way. There may very well be a reason that the
1147code works as it does.
1148
1149Hmmm. The first obvious point is that bfd_install_relocation should
1150not have any tests that depend upon the flavour. It's seem like
1151entirely the wrong place for such a thing. The second obvious point
1152is that the current code ignores the reloc addend when producing
1153relocateable output for COFF. That's peculiar. In fact, I really
1154have no idea what the point of the line you want to remove is.
1155
1156A typical COFF reloc subtracts the old value of the symbol and adds in
1157the new value to the location in the object file (if it's a pc
1158relative reloc it adds the difference between the symbol value and the
1159location). When relocating we need to preserve that property.
1160
1161BFD handles this by setting the addend to the negative of the old
1162value of the symbol. Unfortunately it handles common symbols in a
1163non-standard way (it doesn't subtract the old value) but that's a
1164different story (we can't change it without losing backward
1165compatibility with old object files) (coff-i386 does subtract the old
1166value, to be compatible with existing coff-i386 targets, like SCO).
1167
1168So everything works fine when not producing relocateable output. When
1169we are producing relocateable output, logically we should do exactly
1170what we do when not producing relocateable output. Therefore, your
1171patch is correct. In fact, it should probably always just set
1172reloc_entry->addend to 0 for all cases, since it is, in fact, going to
1173add the value into the object file. This won't hurt the COFF code,
1174which doesn't use the addend; I'm not sure what it will do to other
1175formats (the thing to check for would be whether any formats both use
1176the addend and set partial_inplace).
1177
1178When I wanted to make coff-i386 produce relocateable output, I ran
1179into the problem that you are running into: I wanted to remove that
1180line. Rather than risk it, I made the coff-i386 relocs use a special
1181function; it's coff_i386_reloc in coff-i386.c. The function
1182specifically adds the addend field into the object file, knowing that
1183bfd_install_relocation is not going to. If you remove that line, then
1184coff-i386.c will wind up adding the addend field in twice. It's
1185trivial to fix; it just needs to be done.
1186
1187The problem with removing the line is just that it may break some
1188working code. With BFD it's hard to be sure of anything. The right
1189way to deal with this is simply to build and test at least all the
1190supported COFF targets. It should be straightforward if time and disk
1191space consuming. For each target:
1192 1) build the linker
1193 2) generate some executable, and link it using -r (I would
1194 probably use paranoia.o and link against newlib/libc.a, which
1195 for all the supported targets would be available in
1196 /usr/cygnus/progressive/H-host/target/lib/libc.a).
1197 3) make the change to reloc.c
1198 4) rebuild the linker
1199 5) repeat step 2
1200 6) if the resulting object files are the same, you have at least
1201 made it no worse
1202 7) if they are different you have to figure out which version is
1203 right
1204*/
1205 relocation -= reloc_entry->addend;
1206#endif
1207 reloc_entry->addend = 0;
1208 }
1209 else
1210 {
1211 reloc_entry->addend = relocation;
1212 }
1213 }
1214
1215 /* FIXME: This overflow checking is incomplete, because the value
1216 might have overflowed before we get here. For a correct check we
1217 need to compute the value in a size larger than bitsize, but we
1218 can't reasonably do that for a reloc the same size as a host
1219 machine word.
1220 FIXME: We should also do overflow checking on the result after
1221 adding in the value contained in the object file. */
1222 if (howto->complain_on_overflow != complain_overflow_dont)
1223 flag = bfd_check_overflow (howto->complain_on_overflow,
1224 howto->bitsize,
1225 howto->rightshift,
1226 bfd_arch_bits_per_address (abfd),
1227 relocation);
1228
1229 /*
1230 Either we are relocating all the way, or we don't want to apply
1231 the relocation to the reloc entry (probably because there isn't
1232 any room in the output format to describe addends to relocs)
1233 */
1234
1235 /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
1236 (OSF version 1.3, compiler version 3.11). It miscompiles the
1237 following program:
1238
1239 struct str
1240 {
1241 unsigned int i0;
1242 } s = { 0 };
1243
1244 int
1245 main ()
1246 {
1247 unsigned long x;
1248
1249 x = 0x100000000;
1250 x <<= (unsigned long) s.i0;
1251 if (x == 0)
1252 printf ("failed\n");
1253 else
1254 printf ("succeeded (%lx)\n", x);
1255 }
1256 */
1257
1258 relocation >>= (bfd_vma) howto->rightshift;
1259
1260 /* Shift everything up to where it's going to be used */
1261
1262 relocation <<= (bfd_vma) howto->bitpos;
1263
1264 /* Wait for the day when all have the mask in them */
1265
1266 /* What we do:
1267 i instruction to be left alone
1268 o offset within instruction
1269 r relocation offset to apply
1270 S src mask
1271 D dst mask
1272 N ~dst mask
1273 A part 1
1274 B part 2
1275 R result
1276
1277 Do this:
1278 i i i i i o o o o o from bfd_get<size>
1279 and S S S S S to get the size offset we want
1280 + r r r r r r r r r r to get the final value to place
1281 and D D D D D to chop to right size
1282 -----------------------
1283 A A A A A
1284 And this:
1285 ... i i i i i o o o o o from bfd_get<size>
1286 and N N N N N get instruction
1287 -----------------------
1288 ... B B B B B
1289
1290 And then:
1291 B B B B B
1292 or A A A A A
1293 -----------------------
1294 R R R R R R R R R R put into bfd_put<size>
1295 */
1296
1297#define DOIT(x) \
1298 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
1299
9a968f43 1300 data = (bfd_byte *) data_start + (octets - data_start_offset);
252b5132
RH
1301
1302 switch (howto->size)
1303 {
1304 case 0:
1305 {
1306 char x = bfd_get_8 (abfd, (char *) data);
1307 DOIT (x);
1308 bfd_put_8 (abfd, x, (unsigned char *) data);
1309 }
1310 break;
1311
1312 case 1:
1313 {
1314 short x = bfd_get_16 (abfd, (bfd_byte *) data);
1315 DOIT (x);
1316 bfd_put_16 (abfd, x, (unsigned char *) data);
1317 }
1318 break;
1319 case 2:
1320 {
1321 long x = bfd_get_32 (abfd, (bfd_byte *) data);
1322 DOIT (x);
1323 bfd_put_32 (abfd, x, (bfd_byte *) data);
1324 }
1325 break;
1326 case -2:
1327 {
1328 long x = bfd_get_32 (abfd, (bfd_byte *) data);
1329 relocation = -relocation;
1330 DOIT (x);
1331 bfd_put_32 (abfd, x, (bfd_byte *) data);
1332 }
1333 break;
1334
1335 case 3:
1336 /* Do nothing */
1337 break;
1338
1339 case 4:
1340 {
1341 bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data);
1342 DOIT (x);
1343 bfd_put_64 (abfd, x, (bfd_byte *) data);
1344 }
1345 break;
1346 default:
1347 return bfd_reloc_other;
1348 }
1349
1350 return flag;
1351}
1352
1353/* This relocation routine is used by some of the backend linkers.
1354 They do not construct asymbol or arelent structures, so there is no
1355 reason for them to use bfd_perform_relocation. Also,
1356 bfd_perform_relocation is so hacked up it is easier to write a new
1357 function than to try to deal with it.
1358
1359 This routine does a final relocation. Whether it is useful for a
1360 relocateable link depends upon how the object format defines
1361 relocations.
1362
1363 FIXME: This routine ignores any special_function in the HOWTO,
1364 since the existing special_function values have been written for
1365 bfd_perform_relocation.
1366
1367 HOWTO is the reloc howto information.
1368 INPUT_BFD is the BFD which the reloc applies to.
1369 INPUT_SECTION is the section which the reloc applies to.
1370 CONTENTS is the contents of the section.
1371 ADDRESS is the address of the reloc within INPUT_SECTION.
1372 VALUE is the value of the symbol the reloc refers to.
1373 ADDEND is the addend of the reloc. */
1374
1375bfd_reloc_status_type
1376_bfd_final_link_relocate (howto, input_bfd, input_section, contents, address,
1377 value, addend)
1378 reloc_howto_type *howto;
1379 bfd *input_bfd;
1380 asection *input_section;
1381 bfd_byte *contents;
1382 bfd_vma address;
1383 bfd_vma value;
1384 bfd_vma addend;
1385{
1386 bfd_vma relocation;
1387
1388 /* Sanity check the address. */
1389 if (address > input_section->_raw_size)
1390 return bfd_reloc_outofrange;
1391
1392 /* This function assumes that we are dealing with a basic relocation
1393 against a symbol. We want to compute the value of the symbol to
1394 relocate to. This is just VALUE, the value of the symbol, plus
1395 ADDEND, any addend associated with the reloc. */
1396 relocation = value + addend;
1397
1398 /* If the relocation is PC relative, we want to set RELOCATION to
1399 the distance between the symbol (currently in RELOCATION) and the
1400 location we are relocating. Some targets (e.g., i386-aout)
1401 arrange for the contents of the section to be the negative of the
1402 offset of the location within the section; for such targets
1403 pcrel_offset is false. Other targets (e.g., m88kbcs or ELF)
1404 simply leave the contents of the section as zero; for such
1405 targets pcrel_offset is true. If pcrel_offset is false we do not
1406 need to subtract out the offset of the location within the
1407 section (which is just ADDRESS). */
1408 if (howto->pc_relative)
1409 {
1410 relocation -= (input_section->output_section->vma
1411 + input_section->output_offset);
1412 if (howto->pcrel_offset)
1413 relocation -= address;
1414 }
1415
1416 return _bfd_relocate_contents (howto, input_bfd, relocation,
1417 contents + address);
1418}
1419
1420/* Relocate a given location using a given value and howto. */
1421
1422bfd_reloc_status_type
1423_bfd_relocate_contents (howto, input_bfd, relocation, location)
1424 reloc_howto_type *howto;
1425 bfd *input_bfd;
1426 bfd_vma relocation;
1427 bfd_byte *location;
1428{
1429 int size;
7442e600 1430 bfd_vma x = 0;
252b5132
RH
1431 boolean overflow;
1432 unsigned int rightshift = howto->rightshift;
1433 unsigned int bitpos = howto->bitpos;
1434
1435 /* If the size is negative, negate RELOCATION. This isn't very
1436 general. */
1437 if (howto->size < 0)
1438 relocation = -relocation;
1439
1440 /* Get the value we are going to relocate. */
1441 size = bfd_get_reloc_size (howto);
1442 switch (size)
1443 {
1444 default:
1445 case 0:
1446 abort ();
1447 case 1:
1448 x = bfd_get_8 (input_bfd, location);
1449 break;
1450 case 2:
1451 x = bfd_get_16 (input_bfd, location);
1452 break;
1453 case 4:
1454 x = bfd_get_32 (input_bfd, location);
1455 break;
1456 case 8:
1457#ifdef BFD64
1458 x = bfd_get_64 (input_bfd, location);
1459#else
1460 abort ();
1461#endif
1462 break;
1463 }
1464
1465 /* Check for overflow. FIXME: We may drop bits during the addition
1466 which we don't check for. We must either check at every single
1467 operation, which would be tedious, or we must do the computations
1468 in a type larger than bfd_vma, which would be inefficient. */
1469 overflow = false;
1470 if (howto->complain_on_overflow != complain_overflow_dont)
1471 {
1472 bfd_vma addrmask, fieldmask, signmask, ss;
1473 bfd_vma a, b, sum;
1474
1475 /* Get the values to be added together. For signed and unsigned
1476 relocations, we assume that all values should be truncated to
1477 the size of an address. For bitfields, all the bits matter.
1478 See also bfd_check_overflow. */
1479 fieldmask = N_ONES (howto->bitsize);
1480 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
1481 a = relocation;
1482 b = x & howto->src_mask;
1483
1484 switch (howto->complain_on_overflow)
1485 {
1486 case complain_overflow_signed:
1487 a = (a & addrmask) >> rightshift;
1488
1489 /* If any sign bits are set, all sign bits must be set.
1490 That is, A must be a valid negative address after
1491 shifting. */
1492 signmask = ~ (fieldmask >> 1);
1493 ss = a & signmask;
1494 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
1495 overflow = true;
1496
1497 /* We only need this next bit of code if the sign bit of B
1498 is below the sign bit of A. This would only happen if
1499 SRC_MASK had fewer bits than BITSIZE. Note that if
1500 SRC_MASK has more bits than BITSIZE, we can get into
1501 trouble; we would need to verify that B is in range, as
1502 we do for A above. */
1503 signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
1504 if ((b & signmask) != 0)
1505 {
1506 /* Set all the bits above the sign bit. */
1507 b -= signmask <<= 1;
1508 }
1509
1510 b = (b & addrmask) >> bitpos;
1511
1512 /* Now we can do the addition. */
1513 sum = a + b;
1514
1515 /* See if the result has the correct sign. Bits above the
1516 sign bit are junk now; ignore them. If the sum is
1517 positive, make sure we did not have all negative inputs;
1518 if the sum is negative, make sure we did not have all
1519 positive inputs. The test below looks only at the sign
1520 bits, and it really just
1521 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
1522 */
1523 signmask = (fieldmask >> 1) + 1;
1524 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
1525 overflow = true;
1526
1527 break;
1528
1529 case complain_overflow_unsigned:
1530 /* Checking for an unsigned overflow is relatively easy:
1531 trim the addresses and add, and trim the result as well.
1532 Overflow is normally indicated when the result does not
1533 fit in the field. However, we also need to consider the
1534 case when, e.g., fieldmask is 0x7fffffff or smaller, an
1535 input is 0x80000000, and bfd_vma is only 32 bits; then we
1536 will get sum == 0, but there is an overflow, since the
1537 inputs did not fit in the field. Instead of doing a
1538 separate test, we can check for this by or-ing in the
1539 operands when testing for the sum overflowing its final
1540 field. */
1541 a = (a & addrmask) >> rightshift;
1542 b = (b & addrmask) >> bitpos;
1543 sum = (a + b) & addrmask;
1544 if ((a | b | sum) & ~ fieldmask)
1545 overflow = true;
1546
1547 break;
1548
1549 case complain_overflow_bitfield:
1550 /* Much like unsigned, except no trimming with addrmask. In
1551 addition, the sum overflows if there is a carry out of
1552 the bfd_vma, i.e., the sum is less than either input
1553 operand. */
1554 a >>= rightshift;
1555 b >>= bitpos;
1556
1557 /* Bitfields are sometimes used for signed numbers; for
1558 example, a 13-bit field sometimes represents values in
1559 0..8191 and sometimes represents values in -4096..4095.
1560 If the field is signed and a is -4095 (0x1001) and b is
1561 -1 (0x1fff), the sum is -4096 (0x1000), but (0x1001 +
1562 0x1fff is 0x3000). It's not clear how to handle this
1563 everywhere, since there is not way to know how many bits
1564 are significant in the relocation, but the original code
1565 assumed that it was fully sign extended, and we will keep
1566 that assumption. */
1567 signmask = (fieldmask >> 1) + 1;
1568
1569 if ((a & ~ fieldmask) != 0)
1570 {
1571 /* Some bits out of the field are set. This might not
1572 be a problem: if this is a signed bitfield, it is OK
1573 iff all the high bits are set, including the sign
1574 bit. We'll try setting all but the most significant
1575 bit in the original relocation value: if this is all
1576 ones, we are OK, assuming a signed bitfield. */
1577 ss = (signmask << rightshift) - 1;
1578 if ((ss | relocation) != ~ (bfd_vma) 0)
1579 overflow = true;
1580 a &= fieldmask;
1581 }
1582
1583 /* We just assume (b & ~ fieldmask) == 0. */
1584
44257b8b
ILT
1585 /* We explicitly permit wrap around if this relocation
1586 covers the high bit of an address. The Linux kernel
1587 relies on it, and it is the only way to write assembler
1588 code which can run when loaded at a location 0x80000000
1589 away from the location at which it is linked. */
1590 if (howto->bitsize + rightshift
1591 == bfd_arch_bits_per_address (input_bfd))
1592 break;
1593
252b5132
RH
1594 sum = a + b;
1595 if (sum < a || (sum & ~ fieldmask) != 0)
1596 {
1597 /* There was a carry out, or the field overflow. Test
1598 for signed operands again. Here is the overflow test
1599 is as for complain_overflow_signed. */
1600 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
1601 overflow = true;
1602 }
1603
1604 break;
1605
1606 default:
1607 abort ();
1608 }
1609 }
1610
1611 /* Put RELOCATION in the right bits. */
1612 relocation >>= (bfd_vma) rightshift;
1613 relocation <<= (bfd_vma) bitpos;
1614
1615 /* Add RELOCATION to the right bits of X. */
1616 x = ((x & ~howto->dst_mask)
1617 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
1618
1619 /* Put the relocated value back in the object file. */
1620 switch (size)
1621 {
1622 default:
1623 case 0:
1624 abort ();
1625 case 1:
1626 bfd_put_8 (input_bfd, x, location);
1627 break;
1628 case 2:
1629 bfd_put_16 (input_bfd, x, location);
1630 break;
1631 case 4:
1632 bfd_put_32 (input_bfd, x, location);
1633 break;
1634 case 8:
1635#ifdef BFD64
1636 bfd_put_64 (input_bfd, x, location);
1637#else
1638 abort ();
1639#endif
1640 break;
1641 }
1642
1643 return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
1644}
1645
1646/*
1647DOCDD
1648INODE
1649 howto manager, , typedef arelent, Relocations
1650
1651SECTION
1652 The howto manager
1653
1654 When an application wants to create a relocation, but doesn't
1655 know what the target machine might call it, it can find out by
1656 using this bit of code.
1657
1658*/
1659
1660/*
1661TYPEDEF
1662 bfd_reloc_code_type
1663
1664DESCRIPTION
1665 The insides of a reloc code. The idea is that, eventually, there
1666 will be one enumerator for every type of relocation we ever do.
1667 Pass one of these values to <<bfd_reloc_type_lookup>>, and it'll
1668 return a howto pointer.
1669
1670 This does mean that the application must determine the correct
1671 enumerator value; you can't get a howto pointer from a random set
1672 of attributes.
1673
1674SENUM
1675 bfd_reloc_code_real
1676
1677ENUM
1678 BFD_RELOC_64
1679ENUMX
1680 BFD_RELOC_32
1681ENUMX
1682 BFD_RELOC_26
1683ENUMX
1684 BFD_RELOC_24
1685ENUMX
1686 BFD_RELOC_16
1687ENUMX
1688 BFD_RELOC_14
1689ENUMX
1690 BFD_RELOC_8
1691ENUMDOC
1692 Basic absolute relocations of N bits.
1693
1694ENUM
1695 BFD_RELOC_64_PCREL
1696ENUMX
1697 BFD_RELOC_32_PCREL
1698ENUMX
1699 BFD_RELOC_24_PCREL
1700ENUMX
1701 BFD_RELOC_16_PCREL
1702ENUMX
1703 BFD_RELOC_12_PCREL
1704ENUMX
1705 BFD_RELOC_8_PCREL
1706ENUMDOC
1707 PC-relative relocations. Sometimes these are relative to the address
1708of the relocation itself; sometimes they are relative to the start of
1709the section containing the relocation. It depends on the specific target.
1710
1711The 24-bit relocation is used in some Intel 960 configurations.
1712
1713ENUM
1714 BFD_RELOC_32_GOT_PCREL
1715ENUMX
1716 BFD_RELOC_16_GOT_PCREL
1717ENUMX
1718 BFD_RELOC_8_GOT_PCREL
1719ENUMX
1720 BFD_RELOC_32_GOTOFF
1721ENUMX
1722 BFD_RELOC_16_GOTOFF
1723ENUMX
1724 BFD_RELOC_LO16_GOTOFF
1725ENUMX
1726 BFD_RELOC_HI16_GOTOFF
1727ENUMX
1728 BFD_RELOC_HI16_S_GOTOFF
1729ENUMX
1730 BFD_RELOC_8_GOTOFF
1731ENUMX
1732 BFD_RELOC_32_PLT_PCREL
1733ENUMX
1734 BFD_RELOC_24_PLT_PCREL
1735ENUMX
1736 BFD_RELOC_16_PLT_PCREL
1737ENUMX
1738 BFD_RELOC_8_PLT_PCREL
1739ENUMX
1740 BFD_RELOC_32_PLTOFF
1741ENUMX
1742 BFD_RELOC_16_PLTOFF
1743ENUMX
1744 BFD_RELOC_LO16_PLTOFF
1745ENUMX
1746 BFD_RELOC_HI16_PLTOFF
1747ENUMX
1748 BFD_RELOC_HI16_S_PLTOFF
1749ENUMX
1750 BFD_RELOC_8_PLTOFF
1751ENUMDOC
1752 For ELF.
1753
1754ENUM
1755 BFD_RELOC_68K_GLOB_DAT
1756ENUMX
1757 BFD_RELOC_68K_JMP_SLOT
1758ENUMX
1759 BFD_RELOC_68K_RELATIVE
1760ENUMDOC
1761 Relocations used by 68K ELF.
1762
1763ENUM
1764 BFD_RELOC_32_BASEREL
1765ENUMX
1766 BFD_RELOC_16_BASEREL
1767ENUMX
1768 BFD_RELOC_LO16_BASEREL
1769ENUMX
1770 BFD_RELOC_HI16_BASEREL
1771ENUMX
1772 BFD_RELOC_HI16_S_BASEREL
1773ENUMX
1774 BFD_RELOC_8_BASEREL
1775ENUMX
1776 BFD_RELOC_RVA
1777ENUMDOC
1778 Linkage-table relative.
1779
1780ENUM
1781 BFD_RELOC_8_FFnn
1782ENUMDOC
1783 Absolute 8-bit relocation, but used to form an address like 0xFFnn.
1784
1785ENUM
1786 BFD_RELOC_32_PCREL_S2
1787ENUMX
1788 BFD_RELOC_16_PCREL_S2
1789ENUMX
1790 BFD_RELOC_23_PCREL_S2
1791ENUMDOC
1792 These PC-relative relocations are stored as word displacements --
1793i.e., byte displacements shifted right two bits. The 30-bit word
1794displacement (<<32_PCREL_S2>> -- 32 bits, shifted 2) is used on the
1795SPARC. (SPARC tools generally refer to this as <<WDISP30>>.) The
1796signed 16-bit displacement is used on the MIPS, and the 23-bit
1797displacement is used on the Alpha.
1798
1799ENUM
1800 BFD_RELOC_HI22
1801ENUMX
1802 BFD_RELOC_LO10
1803ENUMDOC
1804 High 22 bits and low 10 bits of 32-bit value, placed into lower bits of
1805the target word. These are used on the SPARC.
1806
1807ENUM
1808 BFD_RELOC_GPREL16
1809ENUMX
1810 BFD_RELOC_GPREL32
1811ENUMDOC
1812 For systems that allocate a Global Pointer register, these are
1813displacements off that register. These relocation types are
1814handled specially, because the value the register will have is
1815decided relatively late.
1816
1817
1818ENUM
1819 BFD_RELOC_I960_CALLJ
1820ENUMDOC
1821 Reloc types used for i960/b.out.
1822
1823ENUM
1824 BFD_RELOC_NONE
1825ENUMX
1826 BFD_RELOC_SPARC_WDISP22
1827ENUMX
1828 BFD_RELOC_SPARC22
1829ENUMX
1830 BFD_RELOC_SPARC13
1831ENUMX
1832 BFD_RELOC_SPARC_GOT10
1833ENUMX
1834 BFD_RELOC_SPARC_GOT13
1835ENUMX
1836 BFD_RELOC_SPARC_GOT22
1837ENUMX
1838 BFD_RELOC_SPARC_PC10
1839ENUMX
1840 BFD_RELOC_SPARC_PC22
1841ENUMX
1842 BFD_RELOC_SPARC_WPLT30
1843ENUMX
1844 BFD_RELOC_SPARC_COPY
1845ENUMX
1846 BFD_RELOC_SPARC_GLOB_DAT
1847ENUMX
1848 BFD_RELOC_SPARC_JMP_SLOT
1849ENUMX
1850 BFD_RELOC_SPARC_RELATIVE
1851ENUMX
1852 BFD_RELOC_SPARC_UA32
1853ENUMDOC
1854 SPARC ELF relocations. There is probably some overlap with other
1855 relocation types already defined.
1856
1857ENUM
1858 BFD_RELOC_SPARC_BASE13
1859ENUMX
1860 BFD_RELOC_SPARC_BASE22
1861ENUMDOC
1862 I think these are specific to SPARC a.out (e.g., Sun 4).
1863
1864ENUMEQ
1865 BFD_RELOC_SPARC_64
1866 BFD_RELOC_64
1867ENUMX
1868 BFD_RELOC_SPARC_10
1869ENUMX
1870 BFD_RELOC_SPARC_11
1871ENUMX
1872 BFD_RELOC_SPARC_OLO10
1873ENUMX
1874 BFD_RELOC_SPARC_HH22
1875ENUMX
1876 BFD_RELOC_SPARC_HM10
1877ENUMX
1878 BFD_RELOC_SPARC_LM22
1879ENUMX
1880 BFD_RELOC_SPARC_PC_HH22
1881ENUMX
1882 BFD_RELOC_SPARC_PC_HM10
1883ENUMX
1884 BFD_RELOC_SPARC_PC_LM22
1885ENUMX
1886 BFD_RELOC_SPARC_WDISP16
1887ENUMX
1888 BFD_RELOC_SPARC_WDISP19
1889ENUMX
1890 BFD_RELOC_SPARC_7
1891ENUMX
1892 BFD_RELOC_SPARC_6
1893ENUMX
1894 BFD_RELOC_SPARC_5
1895ENUMEQX
1896 BFD_RELOC_SPARC_DISP64
1897 BFD_RELOC_64_PCREL
1898ENUMX
1899 BFD_RELOC_SPARC_PLT64
1900ENUMX
1901 BFD_RELOC_SPARC_HIX22
1902ENUMX
1903 BFD_RELOC_SPARC_LOX10
1904ENUMX
1905 BFD_RELOC_SPARC_H44
1906ENUMX
1907 BFD_RELOC_SPARC_M44
1908ENUMX
1909 BFD_RELOC_SPARC_L44
1910ENUMX
1911 BFD_RELOC_SPARC_REGISTER
1912ENUMDOC
1913 SPARC64 relocations
1914
1915ENUM
1916 BFD_RELOC_SPARC_REV32
1917ENUMDOC
1918 SPARC little endian relocation
1919
1920ENUM
1921 BFD_RELOC_ALPHA_GPDISP_HI16
1922ENUMDOC
1923 Alpha ECOFF and ELF relocations. Some of these treat the symbol or
1924 "addend" in some special way.
1925 For GPDISP_HI16 ("gpdisp") relocations, the symbol is ignored when
1926 writing; when reading, it will be the absolute section symbol. The
1927 addend is the displacement in bytes of the "lda" instruction from
1928 the "ldah" instruction (which is at the address of this reloc).
1929ENUM
1930 BFD_RELOC_ALPHA_GPDISP_LO16
1931ENUMDOC
1932 For GPDISP_LO16 ("ignore") relocations, the symbol is handled as
1933 with GPDISP_HI16 relocs. The addend is ignored when writing the
1934 relocations out, and is filled in with the file's GP value on
1935 reading, for convenience.
1936
1937ENUM
1938 BFD_RELOC_ALPHA_GPDISP
1939ENUMDOC
1940 The ELF GPDISP relocation is exactly the same as the GPDISP_HI16
1941 relocation except that there is no accompanying GPDISP_LO16
1942 relocation.
1943
1944ENUM
1945 BFD_RELOC_ALPHA_LITERAL
1946ENUMX
1947 BFD_RELOC_ALPHA_ELF_LITERAL
1948ENUMX
1949 BFD_RELOC_ALPHA_LITUSE
1950ENUMDOC
1951 The Alpha LITERAL/LITUSE relocs are produced by a symbol reference;
1952 the assembler turns it into a LDQ instruction to load the address of
1953 the symbol, and then fills in a register in the real instruction.
1954
1955 The LITERAL reloc, at the LDQ instruction, refers to the .lita
1956 section symbol. The addend is ignored when writing, but is filled
1957 in with the file's GP value on reading, for convenience, as with the
1958 GPDISP_LO16 reloc.
1959
1960 The ELF_LITERAL reloc is somewhere between 16_GOTOFF and GPDISP_LO16.
1961 It should refer to the symbol to be referenced, as with 16_GOTOFF,
1962 but it generates output not based on the position within the .got
1963 section, but relative to the GP value chosen for the file during the
1964 final link stage.
1965
1966 The LITUSE reloc, on the instruction using the loaded address, gives
1967 information to the linker that it might be able to use to optimize
1968 away some literal section references. The symbol is ignored (read
1969 as the absolute section symbol), and the "addend" indicates the type
1970 of instruction using the register:
1971 1 - "memory" fmt insn
1972 2 - byte-manipulation (byte offset reg)
1973 3 - jsr (target of branch)
1974
1975 The GNU linker currently doesn't do any of this optimizing.
1976
fe174262
MM
1977ENUM
1978 BFD_RELOC_ALPHA_USER_LITERAL
1979ENUMX
1980 BFD_RELOC_ALPHA_USER_LITUSE_BASE
1981ENUMX
1982 BFD_RELOC_ALPHA_USER_LITUSE_BYTOFF
1983ENUMX
1984 BFD_RELOC_ALPHA_USER_LITUSE_JSR
1985ENUMX
1986 BFD_RELOC_ALPHA_USER_GPDISP
1987ENUMX
1988 BFD_RELOC_ALPHA_USER_GPRELHIGH
1989ENUMX
1990 BFD_RELOC_ALPHA_USER_GPRELLOW
1991ENUMDOC
1992 The BFD_RELOC_ALPHA_USER_* relocations are used by the assembler to
1993 process the explicit !<reloc>!sequence relocations, and are mapped
1994 into the normal relocations at the end of processing.
1995
252b5132
RH
1996ENUM
1997 BFD_RELOC_ALPHA_HINT
1998ENUMDOC
1999 The HINT relocation indicates a value that should be filled into the
2000 "hint" field of a jmp/jsr/ret instruction, for possible branch-
2001 prediction logic which may be provided on some processors.
2002
2003ENUM
2004 BFD_RELOC_ALPHA_LINKAGE
2005ENUMDOC
2006 The LINKAGE relocation outputs a linkage pair in the object file,
2007 which is filled by the linker.
2008
2009ENUM
2010 BFD_RELOC_ALPHA_CODEADDR
2011ENUMDOC
2012 The CODEADDR relocation outputs a STO_CA in the object file,
2013 which is filled by the linker.
2014
2015ENUM
2016 BFD_RELOC_MIPS_JMP
2017ENUMDOC
2018 Bits 27..2 of the relocation address shifted right 2 bits;
2019 simple reloc otherwise.
2020
2021ENUM
2022 BFD_RELOC_MIPS16_JMP
2023ENUMDOC
2024 The MIPS16 jump instruction.
2025
2026ENUM
2027 BFD_RELOC_MIPS16_GPREL
2028ENUMDOC
2029 MIPS16 GP relative reloc.
2030
2031ENUM
2032 BFD_RELOC_HI16
2033ENUMDOC
2034 High 16 bits of 32-bit value; simple reloc.
2035ENUM
2036 BFD_RELOC_HI16_S
2037ENUMDOC
2038 High 16 bits of 32-bit value but the low 16 bits will be sign
2039 extended and added to form the final result. If the low 16
2040 bits form a negative number, we need to add one to the high value
2041 to compensate for the borrow when the low bits are added.
2042ENUM
2043 BFD_RELOC_LO16
2044ENUMDOC
2045 Low 16 bits.
2046ENUM
2047 BFD_RELOC_PCREL_HI16_S
2048ENUMDOC
2049 Like BFD_RELOC_HI16_S, but PC relative.
2050ENUM
2051 BFD_RELOC_PCREL_LO16
2052ENUMDOC
2053 Like BFD_RELOC_LO16, but PC relative.
2054
2055ENUMEQ
2056 BFD_RELOC_MIPS_GPREL
2057 BFD_RELOC_GPREL16
2058ENUMDOC
2059 Relocation relative to the global pointer.
2060
2061ENUM
2062 BFD_RELOC_MIPS_LITERAL
2063ENUMDOC
2064 Relocation against a MIPS literal section.
2065
2066ENUM
2067 BFD_RELOC_MIPS_GOT16
2068ENUMX
2069 BFD_RELOC_MIPS_CALL16
2070ENUMEQX
2071 BFD_RELOC_MIPS_GPREL32
2072 BFD_RELOC_GPREL32
2073ENUMX
2074 BFD_RELOC_MIPS_GOT_HI16
2075ENUMX
2076 BFD_RELOC_MIPS_GOT_LO16
2077ENUMX
2078 BFD_RELOC_MIPS_CALL_HI16
2079ENUMX
2080 BFD_RELOC_MIPS_CALL_LO16
3f830999
MM
2081ENUMX
2082 BFD_RELOC_MIPS_SUB
2083ENUMX
2084 BFD_RELOC_MIPS_GOT_PAGE
2085ENUMX
2086 BFD_RELOC_MIPS_GOT_OFST
2087ENUMX
2088 BFD_RELOC_MIPS_GOT_DISP
252b5132
RH
2089COMMENT
2090ENUMDOC
2091 MIPS ELF relocations.
2092
2093COMMENT
2094
2095ENUM
2096 BFD_RELOC_386_GOT32
2097ENUMX
2098 BFD_RELOC_386_PLT32
2099ENUMX
2100 BFD_RELOC_386_COPY
2101ENUMX
2102 BFD_RELOC_386_GLOB_DAT
2103ENUMX
2104 BFD_RELOC_386_JUMP_SLOT
2105ENUMX
2106 BFD_RELOC_386_RELATIVE
2107ENUMX
2108 BFD_RELOC_386_GOTOFF
2109ENUMX
2110 BFD_RELOC_386_GOTPC
2111ENUMDOC
2112 i386/elf relocations
2113
2114ENUM
2115 BFD_RELOC_NS32K_IMM_8
2116ENUMX
2117 BFD_RELOC_NS32K_IMM_16
2118ENUMX
2119 BFD_RELOC_NS32K_IMM_32
2120ENUMX
2121 BFD_RELOC_NS32K_IMM_8_PCREL
2122ENUMX
2123 BFD_RELOC_NS32K_IMM_16_PCREL
2124ENUMX
2125 BFD_RELOC_NS32K_IMM_32_PCREL
2126ENUMX
2127 BFD_RELOC_NS32K_DISP_8
2128ENUMX
2129 BFD_RELOC_NS32K_DISP_16
2130ENUMX
2131 BFD_RELOC_NS32K_DISP_32
2132ENUMX
2133 BFD_RELOC_NS32K_DISP_8_PCREL
2134ENUMX
2135 BFD_RELOC_NS32K_DISP_16_PCREL
2136ENUMX
2137 BFD_RELOC_NS32K_DISP_32_PCREL
2138ENUMDOC
2139 ns32k relocations
2140
0bcb993b
ILT
2141ENUM
2142 BFD_RELOC_PJ_CODE_HI16
2143ENUMX
2144 BFD_RELOC_PJ_CODE_LO16
2145ENUMX
2146 BFD_RELOC_PJ_CODE_DIR16
2147ENUMX
2148 BFD_RELOC_PJ_CODE_DIR32
2149ENUMX
2150 BFD_RELOC_PJ_CODE_REL16
2151ENUMX
2152 BFD_RELOC_PJ_CODE_REL32
2153ENUMDOC
2154 Picojava relocs. Not all of these appear in object files.
2155
252b5132
RH
2156ENUM
2157 BFD_RELOC_PPC_B26
2158ENUMX
2159 BFD_RELOC_PPC_BA26
2160ENUMX
2161 BFD_RELOC_PPC_TOC16
2162ENUMX
2163 BFD_RELOC_PPC_B16
2164ENUMX
2165 BFD_RELOC_PPC_B16_BRTAKEN
2166ENUMX
2167 BFD_RELOC_PPC_B16_BRNTAKEN
2168ENUMX
2169 BFD_RELOC_PPC_BA16
2170ENUMX
2171 BFD_RELOC_PPC_BA16_BRTAKEN
2172ENUMX
2173 BFD_RELOC_PPC_BA16_BRNTAKEN
2174ENUMX
2175 BFD_RELOC_PPC_COPY
2176ENUMX
2177 BFD_RELOC_PPC_GLOB_DAT
2178ENUMX
2179 BFD_RELOC_PPC_JMP_SLOT
2180ENUMX
2181 BFD_RELOC_PPC_RELATIVE
2182ENUMX
2183 BFD_RELOC_PPC_LOCAL24PC
2184ENUMX
2185 BFD_RELOC_PPC_EMB_NADDR32
2186ENUMX
2187 BFD_RELOC_PPC_EMB_NADDR16
2188ENUMX
2189 BFD_RELOC_PPC_EMB_NADDR16_LO
2190ENUMX
2191 BFD_RELOC_PPC_EMB_NADDR16_HI
2192ENUMX
2193 BFD_RELOC_PPC_EMB_NADDR16_HA
2194ENUMX
2195 BFD_RELOC_PPC_EMB_SDAI16
2196ENUMX
2197 BFD_RELOC_PPC_EMB_SDA2I16
2198ENUMX
2199 BFD_RELOC_PPC_EMB_SDA2REL
2200ENUMX
2201 BFD_RELOC_PPC_EMB_SDA21
2202ENUMX
2203 BFD_RELOC_PPC_EMB_MRKREF
2204ENUMX
2205 BFD_RELOC_PPC_EMB_RELSEC16
2206ENUMX
2207 BFD_RELOC_PPC_EMB_RELST_LO
2208ENUMX
2209 BFD_RELOC_PPC_EMB_RELST_HI
2210ENUMX
2211 BFD_RELOC_PPC_EMB_RELST_HA
2212ENUMX
2213 BFD_RELOC_PPC_EMB_BIT_FLD
2214ENUMX
2215 BFD_RELOC_PPC_EMB_RELSDA
2216ENUMDOC
2217 Power(rs6000) and PowerPC relocations.
2218
5b93d8bb
AM
2219ENUM
2220 BFD_RELOC_I370_D12
2221ENUMDOC
2222 IBM 370/390 relocations
2223
252b5132
RH
2224ENUM
2225 BFD_RELOC_CTOR
2226ENUMDOC
2227 The type of reloc used to build a contructor table - at the moment
2228 probably a 32 bit wide absolute relocation, but the target can choose.
2229 It generally does map to one of the other relocation types.
2230
2231ENUM
2232 BFD_RELOC_ARM_PCREL_BRANCH
2233ENUMDOC
2234 ARM 26 bit pc-relative branch. The lowest two bits must be zero and are
2235 not stored in the instruction.
2236ENUM
2237 BFD_RELOC_ARM_IMMEDIATE
752149a0
NC
2238ENUMX
2239 BFD_RELOC_ARM_ADRL_IMMEDIATE
252b5132
RH
2240ENUMX
2241 BFD_RELOC_ARM_OFFSET_IMM
2242ENUMX
2243 BFD_RELOC_ARM_SHIFT_IMM
2244ENUMX
2245 BFD_RELOC_ARM_SWI
2246ENUMX
2247 BFD_RELOC_ARM_MULTI
2248ENUMX
2249 BFD_RELOC_ARM_CP_OFF_IMM
2250ENUMX
2251 BFD_RELOC_ARM_ADR_IMM
2252ENUMX
2253 BFD_RELOC_ARM_LDR_IMM
2254ENUMX
2255 BFD_RELOC_ARM_LITERAL
2256ENUMX
2257 BFD_RELOC_ARM_IN_POOL
2258ENUMX
2259 BFD_RELOC_ARM_OFFSET_IMM8
2260ENUMX
2261 BFD_RELOC_ARM_HWLITERAL
2262ENUMX
2263 BFD_RELOC_ARM_THUMB_ADD
2264ENUMX
2265 BFD_RELOC_ARM_THUMB_IMM
2266ENUMX
2267 BFD_RELOC_ARM_THUMB_SHIFT
2268ENUMX
2269 BFD_RELOC_ARM_THUMB_OFFSET
2270ENUMX
2271 BFD_RELOC_ARM_GOT12
2272ENUMX
2273 BFD_RELOC_ARM_GOT32
2274ENUMX
2275 BFD_RELOC_ARM_JUMP_SLOT
2276ENUMX
2277 BFD_RELOC_ARM_COPY
2278ENUMX
2279 BFD_RELOC_ARM_GLOB_DAT
2280ENUMX
2281 BFD_RELOC_ARM_PLT32
2282ENUMX
2283 BFD_RELOC_ARM_RELATIVE
2284ENUMX
2285 BFD_RELOC_ARM_GOTOFF
2286ENUMX
2287 BFD_RELOC_ARM_GOTPC
2288ENUMDOC
2289 These relocs are only used within the ARM assembler. They are not
2290 (at present) written to any object files.
2291
2292ENUM
2293 BFD_RELOC_SH_PCDISP8BY2
2294ENUMX
2295 BFD_RELOC_SH_PCDISP12BY2
2296ENUMX
2297 BFD_RELOC_SH_IMM4
2298ENUMX
2299 BFD_RELOC_SH_IMM4BY2
2300ENUMX
2301 BFD_RELOC_SH_IMM4BY4
2302ENUMX
2303 BFD_RELOC_SH_IMM8
2304ENUMX
2305 BFD_RELOC_SH_IMM8BY2
2306ENUMX
2307 BFD_RELOC_SH_IMM8BY4
2308ENUMX
2309 BFD_RELOC_SH_PCRELIMM8BY2
2310ENUMX
2311 BFD_RELOC_SH_PCRELIMM8BY4
2312ENUMX
2313 BFD_RELOC_SH_SWITCH16
2314ENUMX
2315 BFD_RELOC_SH_SWITCH32
2316ENUMX
2317 BFD_RELOC_SH_USES
2318ENUMX
2319 BFD_RELOC_SH_COUNT
2320ENUMX
2321 BFD_RELOC_SH_ALIGN
2322ENUMX
2323 BFD_RELOC_SH_CODE
2324ENUMX
2325 BFD_RELOC_SH_DATA
2326ENUMX
2327 BFD_RELOC_SH_LABEL
2328ENUMDOC
2329 Hitachi SH relocs. Not all of these appear in object files.
2330
2331ENUM
2332 BFD_RELOC_THUMB_PCREL_BRANCH9
2333ENUMX
2334 BFD_RELOC_THUMB_PCREL_BRANCH12
2335ENUMX
2336 BFD_RELOC_THUMB_PCREL_BRANCH23
2337ENUMDOC
2338 Thumb 23-, 12- and 9-bit pc-relative branches. The lowest bit must
2339 be zero and is not stored in the instruction.
2340
2341ENUM
2342 BFD_RELOC_ARC_B22_PCREL
2343ENUMDOC
2344 Argonaut RISC Core (ARC) relocs.
2345 ARC 22 bit pc-relative branch. The lowest two bits must be zero and are
2346 not stored in the instruction. The high 20 bits are installed in bits 26
2347 through 7 of the instruction.
2348ENUM
2349 BFD_RELOC_ARC_B26
2350ENUMDOC
2351 ARC 26 bit absolute branch. The lowest two bits must be zero and are not
2352 stored in the instruction. The high 24 bits are installed in bits 23
2353 through 0.
2354
2355ENUM
2356 BFD_RELOC_D10V_10_PCREL_R
2357ENUMDOC
2358 Mitsubishi D10V relocs.
2359 This is a 10-bit reloc with the right 2 bits
2360 assumed to be 0.
2361ENUM
2362 BFD_RELOC_D10V_10_PCREL_L
2363ENUMDOC
2364 Mitsubishi D10V relocs.
2365 This is a 10-bit reloc with the right 2 bits
2366 assumed to be 0. This is the same as the previous reloc
2367 except it is in the left container, i.e.,
2368 shifted left 15 bits.
2369ENUM
2370 BFD_RELOC_D10V_18
2371ENUMDOC
2372 This is an 18-bit reloc with the right 2 bits
2373 assumed to be 0.
2374ENUM
2375 BFD_RELOC_D10V_18_PCREL
2376ENUMDOC
2377 This is an 18-bit reloc with the right 2 bits
2378 assumed to be 0.
2379
2380ENUM
2381 BFD_RELOC_D30V_6
2382ENUMDOC
2383 Mitsubishi D30V relocs.
2384 This is a 6-bit absolute reloc.
2385ENUM
2386 BFD_RELOC_D30V_9_PCREL
2387ENUMDOC
2388 This is a 6-bit pc-relative reloc with
2389 the right 3 bits assumed to be 0.
2390ENUM
2391 BFD_RELOC_D30V_9_PCREL_R
2392ENUMDOC
2393 This is a 6-bit pc-relative reloc with
2394 the right 3 bits assumed to be 0. Same
2395 as the previous reloc but on the right side
2396 of the container.
2397ENUM
2398 BFD_RELOC_D30V_15
2399ENUMDOC
2400 This is a 12-bit absolute reloc with the
2401 right 3 bitsassumed to be 0.
2402ENUM
2403 BFD_RELOC_D30V_15_PCREL
2404ENUMDOC
2405 This is a 12-bit pc-relative reloc with
2406 the right 3 bits assumed to be 0.
2407ENUM
2408 BFD_RELOC_D30V_15_PCREL_R
2409ENUMDOC
2410 This is a 12-bit pc-relative reloc with
2411 the right 3 bits assumed to be 0. Same
2412 as the previous reloc but on the right side
2413 of the container.
2414ENUM
2415 BFD_RELOC_D30V_21
2416ENUMDOC
2417 This is an 18-bit absolute reloc with
2418 the right 3 bits assumed to be 0.
2419ENUM
2420 BFD_RELOC_D30V_21_PCREL
2421ENUMDOC
2422 This is an 18-bit pc-relative reloc with
2423 the right 3 bits assumed to be 0.
2424ENUM
2425 BFD_RELOC_D30V_21_PCREL_R
2426ENUMDOC
2427 This is an 18-bit pc-relative reloc with
2428 the right 3 bits assumed to be 0. Same
2429 as the previous reloc but on the right side
2430 of the container.
2431ENUM
2432 BFD_RELOC_D30V_32
2433ENUMDOC
2434 This is a 32-bit absolute reloc.
2435ENUM
2436 BFD_RELOC_D30V_32_PCREL
2437ENUMDOC
2438 This is a 32-bit pc-relative reloc.
2439
2440ENUM
2441 BFD_RELOC_M32R_24
2442ENUMDOC
2443 Mitsubishi M32R relocs.
2444 This is a 24 bit absolute address.
2445ENUM
2446 BFD_RELOC_M32R_10_PCREL
2447ENUMDOC
2448 This is a 10-bit pc-relative reloc with the right 2 bits assumed to be 0.
2449ENUM
2450 BFD_RELOC_M32R_18_PCREL
2451ENUMDOC
2452 This is an 18-bit reloc with the right 2 bits assumed to be 0.
2453ENUM
2454 BFD_RELOC_M32R_26_PCREL
2455ENUMDOC
2456 This is a 26-bit reloc with the right 2 bits assumed to be 0.
2457ENUM
2458 BFD_RELOC_M32R_HI16_ULO
2459ENUMDOC
2460 This is a 16-bit reloc containing the high 16 bits of an address
2461 used when the lower 16 bits are treated as unsigned.
2462ENUM
2463 BFD_RELOC_M32R_HI16_SLO
2464ENUMDOC
2465 This is a 16-bit reloc containing the high 16 bits of an address
2466 used when the lower 16 bits are treated as signed.
2467ENUM
2468 BFD_RELOC_M32R_LO16
2469ENUMDOC
2470 This is a 16-bit reloc containing the lower 16 bits of an address.
2471ENUM
2472 BFD_RELOC_M32R_SDA16
2473ENUMDOC
2474 This is a 16-bit reloc containing the small data area offset for use in
2475 add3, load, and store instructions.
2476
2477ENUM
2478 BFD_RELOC_V850_9_PCREL
2479ENUMDOC
2480 This is a 9-bit reloc
2481ENUM
2482 BFD_RELOC_V850_22_PCREL
2483ENUMDOC
2484 This is a 22-bit reloc
2485
2486ENUM
2487 BFD_RELOC_V850_SDA_16_16_OFFSET
2488ENUMDOC
2489 This is a 16 bit offset from the short data area pointer.
2490ENUM
2491 BFD_RELOC_V850_SDA_15_16_OFFSET
2492ENUMDOC
2493 This is a 16 bit offset (of which only 15 bits are used) from the
2494 short data area pointer.
2495ENUM
2496 BFD_RELOC_V850_ZDA_16_16_OFFSET
2497ENUMDOC
2498 This is a 16 bit offset from the zero data area pointer.
2499ENUM
2500 BFD_RELOC_V850_ZDA_15_16_OFFSET
2501ENUMDOC
2502 This is a 16 bit offset (of which only 15 bits are used) from the
2503 zero data area pointer.
2504ENUM
2505 BFD_RELOC_V850_TDA_6_8_OFFSET
2506ENUMDOC
2507 This is an 8 bit offset (of which only 6 bits are used) from the
2508 tiny data area pointer.
2509ENUM
2510 BFD_RELOC_V850_TDA_7_8_OFFSET
2511ENUMDOC
2512 This is an 8bit offset (of which only 7 bits are used) from the tiny
2513 data area pointer.
2514ENUM
2515 BFD_RELOC_V850_TDA_7_7_OFFSET
2516ENUMDOC
2517 This is a 7 bit offset from the tiny data area pointer.
2518ENUM
2519 BFD_RELOC_V850_TDA_16_16_OFFSET
2520ENUMDOC
2521 This is a 16 bit offset from the tiny data area pointer.
2522COMMENT
2523ENUM
2524 BFD_RELOC_V850_TDA_4_5_OFFSET
2525ENUMDOC
2526 This is a 5 bit offset (of which only 4 bits are used) from the tiny
2527 data area pointer.
2528ENUM
2529 BFD_RELOC_V850_TDA_4_4_OFFSET
2530ENUMDOC
2531 This is a 4 bit offset from the tiny data area pointer.
2532ENUM
2533 BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET
2534ENUMDOC
2535 This is a 16 bit offset from the short data area pointer, with the
2536 bits placed non-contigously in the instruction.
2537ENUM
2538 BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET
2539ENUMDOC
2540 This is a 16 bit offset from the zero data area pointer, with the
2541 bits placed non-contigously in the instruction.
2542ENUM
2543 BFD_RELOC_V850_CALLT_6_7_OFFSET
2544ENUMDOC
2545 This is a 6 bit offset from the call table base pointer.
2546ENUM
2547 BFD_RELOC_V850_CALLT_16_16_OFFSET
2548ENUMDOC
2549 This is a 16 bit offset from the call table base pointer.
2550COMMENT
2551
2552ENUM
2553 BFD_RELOC_MN10300_32_PCREL
2554ENUMDOC
2555 This is a 32bit pcrel reloc for the mn10300, offset by two bytes in the
2556 instruction.
2557ENUM
2558 BFD_RELOC_MN10300_16_PCREL
2559ENUMDOC
2560 This is a 16bit pcrel reloc for the mn10300, offset by two bytes in the
2561 instruction.
2562
2563ENUM
2564 BFD_RELOC_TIC30_LDP
2565ENUMDOC
2566 This is a 8bit DP reloc for the tms320c30, where the most
2567 significant 8 bits of a 24 bit word are placed into the least
2568 significant 8 bits of the opcode.
2569
2570ENUM
2571 BFD_RELOC_FR30_48
2572ENUMDOC
2573 This is a 48 bit reloc for the FR30 that stores 32 bits.
2574ENUM
2575 BFD_RELOC_FR30_20
2576ENUMDOC
2577 This is a 32 bit reloc for the FR30 that stores 20 bits split up into
2578 two sections.
2579ENUM
2580 BFD_RELOC_FR30_6_IN_4
2581ENUMDOC
2582 This is a 16 bit reloc for the FR30 that stores a 6 bit word offset in
2583 4 bits.
2584ENUM
2585 BFD_RELOC_FR30_8_IN_8
2586ENUMDOC
2587 This is a 16 bit reloc for the FR30 that stores an 8 bit byte offset
2588 into 8 bits.
2589ENUM
2590 BFD_RELOC_FR30_9_IN_8
2591ENUMDOC
2592 This is a 16 bit reloc for the FR30 that stores a 9 bit short offset
2593 into 8 bits.
2594ENUM
2595 BFD_RELOC_FR30_10_IN_8
2596ENUMDOC
2597 This is a 16 bit reloc for the FR30 that stores a 10 bit word offset
2598 into 8 bits.
2599ENUM
2600 BFD_RELOC_FR30_9_PCREL
2601ENUMDOC
2602 This is a 16 bit reloc for the FR30 that stores a 9 bit pc relative
2603 short offset into 8 bits.
2604ENUM
2605 BFD_RELOC_FR30_12_PCREL
2606ENUMDOC
2607 This is a 16 bit reloc for the FR30 that stores a 12 bit pc relative
2608 short offset into 11 bits.
2609
2610ENUM
2611 BFD_RELOC_MCORE_PCREL_IMM8BY4
2612ENUMX
2613 BFD_RELOC_MCORE_PCREL_IMM11BY2
2614ENUMX
2615 BFD_RELOC_MCORE_PCREL_IMM4BY2
2616ENUMX
2617 BFD_RELOC_MCORE_PCREL_32
2618ENUMX
2619 BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2
36797d47
NC
2620ENUMX
2621 BFD_RELOC_MCORE_RVA
252b5132
RH
2622ENUMDOC
2623 Motorola Mcore relocations.
2624
2625ENUM
2626 BFD_RELOC_VTABLE_INHERIT
2627ENUMX
2628 BFD_RELOC_VTABLE_ENTRY
2629ENUMDOC
2630 These two relocations are used by the linker to determine which of
2631 the entries in a C++ virtual function table are actually used. When
2632 the --gc-sections option is given, the linker will zero out the entries
2633 that are not used, so that the code for those functions need not be
2634 included in the output.
2635
2636 VTABLE_INHERIT is a zero-space relocation used to describe to the
2637 linker the inheritence tree of a C++ virtual function table. The
2638 relocation's symbol should be the parent class' vtable, and the
2639 relocation should be located at the child vtable.
2640
2641 VTABLE_ENTRY is a zero-space relocation that describes the use of a
2642 virtual function table entry. The reloc's symbol should refer to the
2643 table of the class mentioned in the code. Off of that base, an offset
2644 describes the entry that is being used. For Rela hosts, this offset
2645 is stored in the reloc's addend. For Rel hosts, we are forced to put
2646 this offset in the reloc's section offset.
2647
2648ENDSENUM
2649 BFD_RELOC_UNUSED
2650CODE_FRAGMENT
2651.
2652.typedef enum bfd_reloc_code_real bfd_reloc_code_real_type;
2653*/
2654
2655
2656/*
2657FUNCTION
2658 bfd_reloc_type_lookup
2659
2660SYNOPSIS
2661 reloc_howto_type *
2662 bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
2663
2664DESCRIPTION
2665 Return a pointer to a howto structure which, when
2666 invoked, will perform the relocation @var{code} on data from the
2667 architecture noted.
2668
2669*/
2670
2671
2672reloc_howto_type *
2673bfd_reloc_type_lookup (abfd, code)
2674 bfd *abfd;
2675 bfd_reloc_code_real_type code;
2676{
2677 return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
2678}
2679
2680static reloc_howto_type bfd_howto_32 =
2681HOWTO (0, 00, 2, 32, false, 0, complain_overflow_bitfield, 0, "VRT32", false, 0xffffffff, 0xffffffff, true);
2682
2683
2684/*
2685INTERNAL_FUNCTION
2686 bfd_default_reloc_type_lookup
2687
2688SYNOPSIS
2689 reloc_howto_type *bfd_default_reloc_type_lookup
2690 (bfd *abfd, bfd_reloc_code_real_type code);
2691
2692DESCRIPTION
2693 Provides a default relocation lookup routine for any architecture.
2694
2695
2696*/
2697
2698reloc_howto_type *
2699bfd_default_reloc_type_lookup (abfd, code)
2700 bfd *abfd;
2701 bfd_reloc_code_real_type code;
2702{
2703 switch (code)
2704 {
2705 case BFD_RELOC_CTOR:
2706 /* The type of reloc used in a ctor, which will be as wide as the
2707 address - so either a 64, 32, or 16 bitter. */
2708 switch (bfd_get_arch_info (abfd)->bits_per_address)
2709 {
2710 case 64:
2711 BFD_FAIL ();
2712 case 32:
2713 return &bfd_howto_32;
2714 case 16:
2715 BFD_FAIL ();
2716 default:
2717 BFD_FAIL ();
2718 }
2719 default:
2720 BFD_FAIL ();
2721 }
2722 return (reloc_howto_type *) NULL;
2723}
2724
2725/*
2726FUNCTION
2727 bfd_get_reloc_code_name
2728
2729SYNOPSIS
2730 const char *bfd_get_reloc_code_name (bfd_reloc_code_real_type code);
2731
2732DESCRIPTION
2733 Provides a printable name for the supplied relocation code.
2734 Useful mainly for printing error messages.
2735*/
2736
2737const char *
2738bfd_get_reloc_code_name (code)
2739 bfd_reloc_code_real_type code;
2740{
2741 if (code > BFD_RELOC_UNUSED)
2742 return 0;
2743 return bfd_reloc_code_real_names[(int)code];
2744}
2745
2746/*
2747INTERNAL_FUNCTION
2748 bfd_generic_relax_section
2749
2750SYNOPSIS
2751 boolean bfd_generic_relax_section
2752 (bfd *abfd,
2753 asection *section,
2754 struct bfd_link_info *,
2755 boolean *);
2756
2757DESCRIPTION
2758 Provides default handling for relaxing for back ends which
2759 don't do relaxing -- i.e., does nothing.
2760*/
2761
2762/*ARGSUSED*/
2763boolean
2764bfd_generic_relax_section (abfd, section, link_info, again)
7442e600
ILT
2765 bfd *abfd ATTRIBUTE_UNUSED;
2766 asection *section ATTRIBUTE_UNUSED;
2767 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
252b5132
RH
2768 boolean *again;
2769{
2770 *again = false;
2771 return true;
2772}
2773
2774/*
2775INTERNAL_FUNCTION
2776 bfd_generic_gc_sections
2777
2778SYNOPSIS
2779 boolean bfd_generic_gc_sections
2780 (bfd *, struct bfd_link_info *);
2781
2782DESCRIPTION
2783 Provides default handling for relaxing for back ends which
2784 don't do section gc -- i.e., does nothing.
2785*/
2786
2787/*ARGSUSED*/
2788boolean
2789bfd_generic_gc_sections (abfd, link_info)
7442e600
ILT
2790 bfd *abfd ATTRIBUTE_UNUSED;
2791 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
252b5132
RH
2792{
2793 return true;
2794}
2795
2796/*
2797INTERNAL_FUNCTION
2798 bfd_generic_get_relocated_section_contents
2799
2800SYNOPSIS
2801 bfd_byte *
2802 bfd_generic_get_relocated_section_contents (bfd *abfd,
2803 struct bfd_link_info *link_info,
2804 struct bfd_link_order *link_order,
2805 bfd_byte *data,
2806 boolean relocateable,
2807 asymbol **symbols);
2808
2809DESCRIPTION
2810 Provides default handling of relocation effort for back ends
2811 which can't be bothered to do it efficiently.
2812
2813*/
2814
2815bfd_byte *
2816bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data,
2817 relocateable, symbols)
2818 bfd *abfd;
2819 struct bfd_link_info *link_info;
2820 struct bfd_link_order *link_order;
2821 bfd_byte *data;
2822 boolean relocateable;
2823 asymbol **symbols;
2824{
2825 /* Get enough memory to hold the stuff */
2826 bfd *input_bfd = link_order->u.indirect.section->owner;
2827 asection *input_section = link_order->u.indirect.section;
2828
2829 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
2830 arelent **reloc_vector = NULL;
2831 long reloc_count;
2832
2833 if (reloc_size < 0)
2834 goto error_return;
2835
2836 reloc_vector = (arelent **) bfd_malloc ((size_t) reloc_size);
2837 if (reloc_vector == NULL && reloc_size != 0)
2838 goto error_return;
2839
2840 /* read in the section */
2841 if (!bfd_get_section_contents (input_bfd,
2842 input_section,
2843 (PTR) data,
2844 0,
2845 input_section->_raw_size))
2846 goto error_return;
2847
2848 /* We're not relaxing the section, so just copy the size info */
2849 input_section->_cooked_size = input_section->_raw_size;
2850 input_section->reloc_done = true;
2851
2852 reloc_count = bfd_canonicalize_reloc (input_bfd,
2853 input_section,
2854 reloc_vector,
2855 symbols);
2856 if (reloc_count < 0)
2857 goto error_return;
2858
2859 if (reloc_count > 0)
2860 {
2861 arelent **parent;
2862 for (parent = reloc_vector; *parent != (arelent *) NULL;
2863 parent++)
2864 {
2865 char *error_message = (char *) NULL;
2866 bfd_reloc_status_type r =
2867 bfd_perform_relocation (input_bfd,
2868 *parent,
2869 (PTR) data,
2870 input_section,
2871 relocateable ? abfd : (bfd *) NULL,
2872 &error_message);
2873
2874 if (relocateable)
2875 {
2876 asection *os = input_section->output_section;
2877
2878 /* A partial link, so keep the relocs */
2879 os->orelocation[os->reloc_count] = *parent;
2880 os->reloc_count++;
2881 }
2882
2883 if (r != bfd_reloc_ok)
2884 {
2885 switch (r)
2886 {
2887 case bfd_reloc_undefined:
2888 if (!((*link_info->callbacks->undefined_symbol)
2889 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5cc7c785
L
2890 input_bfd, input_section, (*parent)->address,
2891 true)))
252b5132
RH
2892 goto error_return;
2893 break;
2894 case bfd_reloc_dangerous:
2895 BFD_ASSERT (error_message != (char *) NULL);
2896 if (!((*link_info->callbacks->reloc_dangerous)
2897 (link_info, error_message, input_bfd, input_section,
2898 (*parent)->address)))
2899 goto error_return;
2900 break;
2901 case bfd_reloc_overflow:
2902 if (!((*link_info->callbacks->reloc_overflow)
2903 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
2904 (*parent)->howto->name, (*parent)->addend,
2905 input_bfd, input_section, (*parent)->address)))
2906 goto error_return;
2907 break;
2908 case bfd_reloc_outofrange:
2909 default:
2910 abort ();
2911 break;
2912 }
2913
2914 }
2915 }
2916 }
2917 if (reloc_vector != NULL)
2918 free (reloc_vector);
2919 return data;
2920
2921error_return:
2922 if (reloc_vector != NULL)
2923 free (reloc_vector);
2924 return NULL;
2925}