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