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