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