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