]> Git Repo - binutils.git/blame - bfd/reloc.c
Sun Jul 10 09:12:02 1994 D. V. Henkel-Wallace ([email protected])
[binutils.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*/
985fca12 44#include "bfd.h"
0cda46cf 45#include "sysdep.h"
4c3721d5 46#include "bfdlink.h"
985fca12 47#include "libbfd.h"
c26d7d17
SC
48/*
49DOCDD
e98e6ec1
SC
50INODE
51 typedef arelent, howto manager, Relocations, Relocations
985fca12 52
0cda46cf
SC
53SUBSECTION
54 typedef arelent
985fca12 55
e98e6ec1 56 This is the structure of a relocation entry:
985fca12 57
e98e6ec1
SC
58CODE_FRAGMENT
59.
326e32d7 60.typedef enum bfd_reloc_status
e98e6ec1
SC
61.{
62. {* No errors detected *}
0cda46cf 63. bfd_reloc_ok,
e98e6ec1
SC
64.
65. {* The relocation was performed, but there was an overflow. *}
0cda46cf 66. bfd_reloc_overflow,
e98e6ec1 67.
65cab589 68. {* The address to relocate was not within the section supplied. *}
0cda46cf 69. bfd_reloc_outofrange,
e98e6ec1
SC
70.
71. {* Used by special functions *}
0cda46cf 72. bfd_reloc_continue,
e98e6ec1 73.
c188b0be 74. {* Unsupported relocation size requested. *}
0cda46cf 75. bfd_reloc_notsupported,
e98e6ec1 76.
c188b0be 77. {* Unused *}
0cda46cf 78. bfd_reloc_other,
e98e6ec1 79.
65cab589 80. {* The symbol to relocate against was undefined. *}
0cda46cf 81. bfd_reloc_undefined,
e98e6ec1
SC
82.
83. {* The relocation was performed, but may not be ok - presently
84. generated only when linking i960 coff files with i960 b.out
4c3721d5
ILT
85. symbols. If this type is returned, the error_message argument
86. to bfd_perform_relocation will be set. *}
0cda46cf 87. bfd_reloc_dangerous
e98e6ec1 88. }
0cda46cf 89. bfd_reloc_status_type;
e98e6ec1
SC
90.
91.
326e32d7 92.typedef struct reloc_cache_entry
0cda46cf 93.{
e98e6ec1
SC
94. {* A pointer into the canonical table of pointers *}
95. struct symbol_cache_entry **sym_ptr_ptr;
96.
97. {* offset in section *}
65cab589 98. bfd_size_type address;
e98e6ec1
SC
99.
100. {* addend for relocation value *}
326e32d7 101. bfd_vma addend;
e98e6ec1
SC
102.
103. {* Pointer to how to perform the required relocation *}
4c3721d5 104. const struct reloc_howto_struct *howto;
e98e6ec1
SC
105.
106.} arelent;
985fca12 107
e98e6ec1 108*/
985fca12 109
e98e6ec1
SC
110/*
111DESCRIPTION
985fca12 112
c188b0be 113 Here is a description of each of the fields within an <<arelent>>:
985fca12 114
c188b0be 115 o <<sym_ptr_ptr>>
985fca12 116
e98e6ec1 117 The symbol table pointer points to a pointer to the symbol
c188b0be
DM
118 associated with the relocation request. It is
119 the pointer into the table returned by the back end's
120 <<get_symtab>> action. @xref{Symbols}. The symbol is referenced
e98e6ec1
SC
121 through a pointer to a pointer so that tools like the linker
122 can fix up all the symbols of the same name by modifying only
123 one pointer. The relocation routine looks in the symbol and
124 uses the base of the section the symbol is attached to and the
125 value of the symbol as the initial relocation offset. If the
126 symbol pointer is zero, then the section provided is looked up.
985fca12 127
c188b0be 128 o <<address>>
985fca12 129
c188b0be 130 The <<address>> field gives the offset in bytes from the base of
e98e6ec1
SC
131 the section data which owns the relocation record to the first
132 byte of relocatable information. The actual data relocated
c188b0be 133 will be relative to this point; for example, a relocation
e98e6ec1
SC
134 type which modifies the bottom two bytes of a four byte word
135 would not touch the first byte pointed to in a big endian
c26d7d17
SC
136 world.
137
c188b0be 138 o <<addend>>
c26d7d17 139
c188b0be 140 The <<addend>> is a value provided by the back end to be added (!)
c26d7d17
SC
141 to the relocation offset. Its interpretation is dependent upon
142 the howto. For example, on the 68k the code:
985fca12 143
985fca12 144
e98e6ec1
SC
145| char foo[];
146| main()
147| {
148| return foo[0x12345678];
149| }
985fca12 150
e98e6ec1 151 Could be compiled into:
985fca12 152
e98e6ec1
SC
153| linkw fp,#-4
154| moveb @@#12345678,d0
155| extbl d0
156| unlk fp
157| rts
985fca12 158
985fca12 159
c188b0be
DM
160 This could create a reloc pointing to <<foo>>, but leave the
161 offset in the data, something like:
0cda46cf 162
985fca12 163
e98e6ec1 164|RELOCATION RECORDS FOR [.text]:
326e32d7 165|offset type value
e98e6ec1
SC
166|00000006 32 _foo
167|
168|00000000 4e56 fffc ; linkw fp,#-4
169|00000004 1039 1234 5678 ; moveb @@#12345678,d0
170|0000000a 49c0 ; extbl d0
171|0000000c 4e5e ; unlk fp
172|0000000e 4e75 ; rts
0cda46cf 173
985fca12 174
e98e6ec1
SC
175 Using coff and an 88k, some instructions don't have enough
176 space in them to represent the full address range, and
177 pointers have to be loaded in two parts. So you'd get something like:
0cda46cf 178
985fca12 179
e98e6ec1
SC
180| or.u r13,r0,hi16(_foo+0x12345678)
181| ld.b r2,r13,lo16(_foo+0x12345678)
182| jmp r1
985fca12 183
985fca12 184
c188b0be 185 This should create two relocs, both pointing to <<_foo>>, and with
e98e6ec1 186 0x12340000 in their addend field. The data would consist of:
0cda46cf 187
985fca12 188
e98e6ec1 189|RELOCATION RECORDS FOR [.text]:
326e32d7 190|offset type value
e98e6ec1
SC
191|00000002 HVRT16 _foo+0x12340000
192|00000006 LVRT16 _foo+0x12340000
4c3721d5 193|
e98e6ec1
SC
194|00000000 5da05678 ; or.u r13,r0,0x5678
195|00000004 1c4d5678 ; ld.b r2,r13,0x5678
196|00000008 f400c001 ; jmp r1
985fca12 197
0cda46cf 198
e98e6ec1 199 The relocation routine digs out the value from the data, adds
c188b0be
DM
200 it to the addend to get the original offset, and then adds the
201 value of <<_foo>>. Note that all 32 bits have to be kept around
e98e6ec1 202 somewhere, to cope with carry from bit 15 to bit 16.
985fca12 203
65cab589 204 One further example is the sparc and the a.out format. The
e98e6ec1
SC
205 sparc has a similar problem to the 88k, in that some
206 instructions don't have room for an entire offset, but on the
c188b0be
DM
207 sparc the parts are created in odd sized lumps. The designers of
208 the a.out format chose to not use the data within the section
e98e6ec1 209 for storing part of the offset; all the offset is kept within
326e32d7 210 the reloc. Anything in the data should be ignored.
0cda46cf 211
e98e6ec1
SC
212| save %sp,-112,%sp
213| sethi %hi(_foo+0x12345678),%g2
214| ldsb [%g2+%lo(_foo+0x12345678)],%i0
215| ret
216| restore
0cda46cf 217
4c3721d5 218 Both relocs contain a pointer to <<foo>>, and the offsets
e98e6ec1 219 contain junk.
985fca12 220
0cda46cf 221
e98e6ec1 222|RELOCATION RECORDS FOR [.text]:
326e32d7 223|offset type value
e98e6ec1
SC
224|00000004 HI22 _foo+0x12345678
225|00000008 LO10 _foo+0x12345678
4c3721d5 226|
e98e6ec1
SC
227|00000000 9de3bf90 ; save %sp,-112,%sp
228|00000004 05000000 ; sethi %hi(_foo+0),%g2
229|00000008 f048a000 ; ldsb [%g2+%lo(_foo+0)],%i0
230|0000000c 81c7e008 ; ret
231|00000010 81e80000 ; restore
232
0cda46cf 233
c188b0be 234 o <<howto>>
e98e6ec1 235
c188b0be
DM
236 The <<howto>> field can be imagined as a
237 relocation instruction. It is a pointer to a structure which
238 contains information on what to do with all of the other
e98e6ec1
SC
239 information in the reloc record and data section. A back end
240 would normally have a relocation instruction set and turn
241 relocations into pointers to the correct structure on input -
242 but it would be possible to create each howto field on demand.
326e32d7 243
985fca12
SC
244*/
245
66a277ab
ILT
246/*
247SUBSUBSECTION
248 <<enum complain_overflow>>
249
250 Indicates what sort of overflow checking should be done when
251 performing a relocation.
252
253CODE_FRAGMENT
254.
255.enum complain_overflow
256.{
257. {* Do not complain on overflow. *}
258. complain_overflow_dont,
259.
260. {* Complain if the bitfield overflows, whether it is considered
261. as signed or unsigned. *}
262. complain_overflow_bitfield,
263.
264. {* Complain if the value overflows when considered as signed
265. number. *}
266. complain_overflow_signed,
267.
268. {* Complain if the value overflows when considered as an
269. unsigned number. *}
270. complain_overflow_unsigned
271.};
272
273*/
985fca12 274
0cda46cf 275/*
326e32d7 276SUBSUBSECTION
e98e6ec1 277 <<reloc_howto_type>>
985fca12 278
e98e6ec1 279 The <<reloc_howto_type>> is a structure which contains all the
c188b0be 280 information that libbfd needs to know to tie up a back end's data.
985fca12 281
e98e6ec1 282CODE_FRAGMENT
5022aea5 283.struct symbol_cache_entry; {* Forward declaration *}
e98e6ec1 284.
326e32d7
ILT
285.typedef struct reloc_howto_struct
286.{
e98e6ec1 287. {* The type field has mainly a documetary use - the back end can
c188b0be
DM
288. do what it wants with it, though normally the back end's
289. external idea of what a reloc number is stored
290. in this field. For example, a PC relative word relocation
291. in a coff environment has the type 023 - because that's
e98e6ec1 292. what the outside world calls a R_PCRWORD reloc. *}
0cda46cf 293. unsigned int type;
e98e6ec1
SC
294.
295. {* The value the final relocation is shifted right by. This drops
296. unwanted data from the relocation. *}
0cda46cf 297. unsigned int rightshift;
e98e6ec1 298.
fb32909a 299. {* The size of the item to be relocated. This is *not* a
4c3721d5
ILT
300. power-of-two measure. To get the number of bytes operated
301. on by a type of relocation, use bfd_get_reloc_size. *}
c26d7d17 302. int size;
e98e6ec1 303.
66a277ab
ILT
304. {* The number of bits in the item to be relocated. This is used
305. when doing overflow checking. *}
0cda46cf 306. unsigned int bitsize;
e98e6ec1
SC
307.
308. {* Notes that the relocation is relative to the location in the
309. data section of the addend. The relocation function will
310. subtract from the relocation value the address of the location
311. being relocated. *}
0cda46cf 312. boolean pc_relative;
e98e6ec1 313.
66a277ab
ILT
314. {* The bit position of the reloc value in the destination.
315. The relocated value is left shifted by this amount. *}
0cda46cf 316. unsigned int bitpos;
e98e6ec1 317.
66a277ab
ILT
318. {* What type of overflow error should be checked for when
319. relocating. *}
320. enum complain_overflow complain_on_overflow;
e98e6ec1
SC
321.
322. {* If this field is non null, then the supplied function is
323. called rather than the normal function. This allows really
65cab589 324. strange relocation methods to be accomodated (e.g., i960 callj
e98e6ec1 325. instructions). *}
326e32d7 326. bfd_reloc_status_type (*special_function)
fefb4b30 327. PARAMS ((bfd *abfd,
5022aea5
SC
328. arelent *reloc_entry,
329. struct symbol_cache_entry *symbol,
330. PTR data,
326e32d7 331. asection *input_section,
4c3721d5
ILT
332. bfd *output_bfd,
333. char **error_message));
e98e6ec1
SC
334.
335. {* The textual name of the relocation type. *}
0cda46cf 336. char *name;
e98e6ec1
SC
337.
338. {* When performing a partial link, some formats must modify the
339. relocations rather than the data - this flag signals this.*}
0cda46cf 340. boolean partial_inplace;
e98e6ec1 341.
c188b0be 342. {* The src_mask selects which parts of the read in data
65cab589 343. are to be used in the relocation sum. E.g., if this was an 8 bit
e98e6ec1
SC
344. bit of data which we read and relocated, this would be
345. 0x000000ff. When we have relocs which have an addend, such as
346. sun4 extended relocs, the value in the offset part of a
347. relocating field is garbage so we never use it. In this case
348. the mask would be 0x00000000. *}
65cab589 349. bfd_vma src_mask;
e98e6ec1 350.
c188b0be 351. {* The dst_mask selects which parts of the instruction are replaced
e98e6ec1
SC
352. into the instruction. In most cases src_mask == dst_mask,
353. except in the above special case, where dst_mask would be
354. 0x000000ff, and src_mask would be 0x00000000. *}
326e32d7 355. bfd_vma dst_mask;
e98e6ec1
SC
356.
357. {* When some formats create PC relative instructions, they leave
358. the value of the pc of the place being relocated in the offset
359. slot of the instruction, so that a PC relative relocation can
65cab589 360. be made just by adding in an ordinary offset (e.g., sun3 a.out).
e98e6ec1 361. Some formats leave the displacement part of an instruction
c188b0be 362. empty (e.g., m88k bcs); this flag signals the fact.*}
0cda46cf 363. boolean pcrel_offset;
e98e6ec1 364.
0cda46cf 365.} reloc_howto_type;
985fca12 366
0cda46cf 367*/
985fca12 368
0cda46cf
SC
369/*
370FUNCTION
c188b0be 371 The HOWTO Macro
e98e6ec1 372
0cda46cf
SC
373DESCRIPTION
374 The HOWTO define is horrible and will go away.
375
376
66a277ab
ILT
377.#define HOWTO(C, R,S,B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
378. {(unsigned)C,R,S,B, P, BI, O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
0cda46cf
SC
379
380DESCRIPTION
381 And will be replaced with the totally magic way. But for the
c188b0be 382 moment, we are compatible, so do it this way.
0cda46cf
SC
383
384
66a277ab 385.#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
386.
387DESCRIPTION
388 Helper routine to turn a symbol into a relocation value.
389
e98e6ec1
SC
390.#define HOWTO_PREPARE(relocation, symbol) \
391. { \
392. if (symbol != (asymbol *)NULL) { \
65cab589 393. if (bfd_is_com_section (symbol->section)) { \
e98e6ec1
SC
394. relocation = 0; \
395. } \
396. else { \
397. relocation = symbol->value; \
398. } \
399. } \
326e32d7 400.}
985fca12
SC
401
402*/
403
4c3721d5
ILT
404/*
405FUNCTION
406 bfd_get_reloc_size
407
408SYNOPSIS
409 int bfd_get_reloc_size (const reloc_howto_type *);
410
411DESCRIPTION
412 For a reloc_howto_type that operates on a fixed number of bytes,
413 this returns the number of bytes operated on.
414 */
415
416int
417bfd_get_reloc_size (howto)
418 const reloc_howto_type *howto;
419{
326e32d7
ILT
420 switch (howto->size)
421 {
422 case 0: return 1;
423 case 1: return 2;
424 case 2: return 4;
425 case 3: return 0;
426 case 4: return 8;
427 case -2: return 4;
428 default: abort ();
429 }
4c3721d5
ILT
430}
431
0cda46cf
SC
432/*
433TYPEDEF
c188b0be 434 arelent_chain
985fca12 435
0cda46cf 436DESCRIPTION
985fca12 437
c188b0be 438 How relocs are tied together in an <<asection>>:
985fca12 439
0cda46cf
SC
440.typedef unsigned char bfd_byte;
441.
442.typedef struct relent_chain {
443. arelent relent;
444. struct relent_chain *next;
445.} arelent_chain;
985fca12
SC
446
447*/
448
449
450
0cda46cf 451/*
326e32d7 452FUNCTION
0cda46cf
SC
453 bfd_perform_relocation
454
e98e6ec1
SC
455SYNOPSIS
456 bfd_reloc_status_type
457 bfd_perform_relocation
c188b0be 458 (bfd *abfd,
4c3721d5
ILT
459 arelent *reloc_entry,
460 PTR data,
461 asection *input_section,
462 bfd *output_bfd,
463 char **error_message);
e98e6ec1 464
0cda46cf 465DESCRIPTION
4c3721d5
ILT
466 If @var{output_bfd} is supplied to this function, the
467 generated image will be relocatable; the relocations are
468 copied to the output file after they have been changed to
469 reflect the new state of the world. There are two ways of
470 reflecting the results of partial linkage in an output file:
471 by modifying the output data in place, and by modifying the
472 relocation record. Some native formats (e.g., basic a.out and
473 basic coff) have no way of specifying an addend in the
474 relocation type, so the addend has to go in the output data.
475 This is no big deal since in these formats the output data
476 slot will always be big enough for the addend. Complex reloc
477 types with addends were invented to solve just this problem.
478 The @var{error_message} argument is set to an error message if
479 this return @code{bfd_reloc_dangerous}.
0cda46cf 480
985fca12
SC
481*/
482
483
0cda46cf 484bfd_reloc_status_type
4c3721d5
ILT
485bfd_perform_relocation (abfd, reloc_entry, data, input_section, output_bfd,
486 error_message)
487 bfd *abfd;
488 arelent *reloc_entry;
489 PTR data;
490 asection *input_section;
491 bfd *output_bfd;
492 char **error_message;
985fca12
SC
493{
494 bfd_vma relocation;
0cda46cf 495 bfd_reloc_status_type flag = bfd_reloc_ok;
326e32d7 496 bfd_size_type addr = reloc_entry->address;
985fca12 497 bfd_vma output_base = 0;
4c3721d5
ILT
498 const reloc_howto_type *howto = reloc_entry->howto;
499 asection *reloc_target_output_section;
985fca12
SC
500 asymbol *symbol;
501
4c3721d5 502 symbol = *(reloc_entry->sym_ptr_ptr);
326e32d7
ILT
503 if ((symbol->section == &bfd_abs_section)
504 && output_bfd != (bfd *) NULL)
58acdbd7
KR
505 {
506 reloc_entry->address += input_section->output_offset;
507 return bfd_reloc_ok;
508 }
509
fb32909a
KR
510 /* If we are not producing relocateable output, return an error if
511 the symbol is not defined. An undefined weak symbol is
512 considered to have a value of zero (SVR4 ABI, p. 4-27). */
513 if (symbol->section == &bfd_und_section
514 && (symbol->flags & BSF_WEAK) == 0
515 && output_bfd == (bfd *) NULL)
5022aea5 516 flag = bfd_reloc_undefined;
985fca12 517
58acdbd7
KR
518 /* If there is a function supplied to handle this relocation type,
519 call it. It'll return `bfd_reloc_continue' if further processing
520 can be done. */
521 if (howto->special_function)
522 {
523 bfd_reloc_status_type cont;
524 cont = howto->special_function (abfd, reloc_entry, symbol, data,
4c3721d5
ILT
525 input_section, output_bfd,
526 error_message);
58acdbd7
KR
527 if (cont != bfd_reloc_continue)
528 return cont;
529 }
985fca12 530
58acdbd7
KR
531 /* Is the address of the relocation really within the section? */
532 if (reloc_entry->address > input_section->_cooked_size)
533 return bfd_reloc_outofrange;
985fca12 534
58acdbd7
KR
535 /* Work out which section the relocation is targetted at and the
536 initial relocation command value. */
537
538 /* Get symbol value. (Common symbols are special.) */
539 if (bfd_is_com_section (symbol->section))
5022aea5 540 relocation = 0;
58acdbd7 541 else
5022aea5 542 relocation = symbol->value;
985fca12 543
985fca12 544
e98e6ec1 545 reloc_target_output_section = symbol->section->output_section;
985fca12 546
58acdbd7 547 /* Convert input-section-relative symbol value to absolute. */
326e32d7 548 if (output_bfd && howto->partial_inplace == false)
5022aea5 549 output_base = 0;
58acdbd7 550 else
5022aea5 551 output_base = reloc_target_output_section->vma;
985fca12 552
65cab589 553 relocation += output_base + symbol->section->output_offset;
985fca12 554
58acdbd7 555 /* Add in supplied addend. */
65cab589 556 relocation += reloc_entry->addend;
985fca12 557
c188b0be
DM
558 /* Here the variable relocation holds the final address of the
559 symbol we are relocating against, plus any addend. */
560
985fca12 561 if (howto->pc_relative == true)
58acdbd7 562 {
c188b0be
DM
563 /* This is a PC relative relocation. We want to set RELOCATION
564 to the distance between the address of the symbol and the
565 location. RELOCATION is already the address of the symbol.
566
567 We start by subtracting the address of the section containing
568 the location.
569
570 If pcrel_offset is set, we must further subtract the position
571 of the location within the section. Some targets arrange for
572 the addend to be the negative of the position of the location
573 within the section; for example, i386-aout does this. For
574 i386-aout, pcrel_offset is false. Some other targets do not
575 include the position of the location; for example, m88kbcs,
576 or ELF. For those targets, pcrel_offset is true.
577
578 If we are producing relocateable output, then we must ensure
579 that this reloc will be correctly computed when the final
580 relocation is done. If pcrel_offset is false we want to wind
581 up with the negative of the location within the section,
582 which means we must adjust the existing addend by the change
583 in the location within the section. If pcrel_offset is true
584 we do not want to adjust the existing addend at all.
585
586 FIXME: This seems logical to me, but for the case of
587 producing relocateable output it is not what the code
588 actually does. I don't want to change it, because it seems
589 far too likely that something will break. */
985fca12 590
326e32d7 591 relocation -=
58acdbd7
KR
592 input_section->output_section->vma + input_section->output_offset;
593
594 if (howto->pcrel_offset == true)
595 relocation -= reloc_entry->address;
5022aea5 596 }
e98e6ec1 597
326e32d7 598 if (output_bfd != (bfd *) NULL)
5022aea5 599 {
326e32d7 600 if (howto->partial_inplace == false)
58acdbd7
KR
601 {
602 /* This is a partial relocation, and we want to apply the relocation
603 to the reloc entry rather than the raw data. Modify the reloc
604 inplace to reflect what we now know. */
605 reloc_entry->addend = relocation;
326e32d7 606 reloc_entry->address += input_section->output_offset;
58acdbd7
KR
607 return flag;
608 }
c26d7d17 609 else
58acdbd7
KR
610 {
611 /* This is a partial relocation, but inplace, so modify the
326e32d7 612 reloc record a bit.
58acdbd7
KR
613
614 If we've relocated with a symbol with a section, change
615 into a ref to the section belonging to the symbol. */
616
617 reloc_entry->address += input_section->output_offset;
618
619 /* WTF?? */
3d51f02f
ILT
620 if (abfd->xvec->flavour == bfd_target_coff_flavour
621 && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0)
58acdbd7 622 {
c188b0be
DM
623#if 1
624 /* For m68k-coff, the addend was being subtracted twice during
625 relocation with -r. Removing the line below this comment
626 fixes that problem; see PR 2953.
627
628However, Ian wrote the following, regarding removing the line below,
629which explains why it is still enabled: --djm
630
631If you put a patch like that into BFD you need to check all the COFF
632linkers. I am fairly certain that patch will break coff-i386 (e.g.,
633SCO); see coff_i386_reloc in coff-i386.c where I worked around the
634problem in a different way. There may very well be a reason that the
635code works as it does.
636
637Hmmm. The first obvious point is that bfd_perform_relocation should
638not have any tests that depend upon the flavour. It's seem like
639entirely the wrong place for such a thing. The second obvious point
640is that the current code ignores the reloc addend when producing
641relocateable output for COFF. That's peculiar. In fact, I really
642have no idea what the point of the line you want to remove is.
643
644A typical COFF reloc subtracts the old value of the symbol and adds in
645the new value to the location in the object file (if it's a pc
646relative reloc it adds the difference between the symbol value and the
647location). When relocating we need to preserve that property.
648
649BFD handles this by setting the addend to the negative of the old
650value of the symbol. Unfortunately it handles common symbols in a
651non-standard way (it doesn't subtract the old value) but that's a
652different story (we can't change it without losing backward
653compatibility with old object files) (coff-i386 does subtract the old
654value, to be compatible with existing coff-i386 targets, like SCO).
655
656So everything works fine when not producing relocateable output. When
657we are producing relocateable output, logically we should do exactly
658what we do when not producing relocateable output. Therefore, your
659patch is correct. In fact, it should probably always just set
660reloc_entry->addend to 0 for all cases, since it is, in fact, going to
661add the value into the object file. This won't hurt the COFF code,
662which doesn't use the addend; I'm not sure what it will do to other
663formats (the thing to check for would be whether any formats both use
664the addend and set partial_inplace).
665
666When I wanted to make coff-i386 produce relocateable output, I ran
667into the problem that you are running into: I wanted to remove that
668line. Rather than risk it, I made the coff-i386 relocs use a special
669function; it's coff_i386_reloc in coff-i386.c. The function
670specifically adds the addend field into the object file, knowing that
671bfd_perform_relocation is not going to. If you remove that line, then
672coff-i386.c will wind up adding the addend field in twice. It's
673trivial to fix; it just needs to be done.
674
675The problem with removing the line is just that it may break some
676working code. With BFD it's hard to be sure of anything. The right
677way to deal with this is simply to build and test at least all the
678supported COFF targets. It should be straightforward if time and disk
679space consuming. For each target:
680 1) build the linker
681 2) generate some executable, and link it using -r (I would
682 probably use paranoia.o and link against newlib/libc.a, which
683 for all the supported targets would be available in
684 /usr/cygnus/progressive/H-host/target/lib/libc.a).
685 3) make the change to reloc.c
686 4) rebuild the linker
687 5) repeat step 2
688 6) if the resulting object files are the same, you have at least
689 made it no worse
690 7) if they are different you have to figure out which version is
691 right
692*/
58acdbd7 693 relocation -= reloc_entry->addend;
c188b0be 694#endif
58acdbd7
KR
695 reloc_entry->addend = 0;
696 }
697 else
698 {
699 reloc_entry->addend = relocation;
700 }
701 }
985fca12 702 }
326e32d7 703 else
58acdbd7
KR
704 {
705 reloc_entry->addend = 0;
706 }
985fca12 707
66a277ab
ILT
708 /* FIXME: This overflow checking is incomplete, because the value
709 might have overflowed before we get here. For a correct check we
710 need to compute the value in a size larger than bitsize, but we
711 can't reasonably do that for a reloc the same size as a host
a49880c8
KR
712 machine word.
713 FIXME: We should also do overflow checking on the result after
714 adding in the value contained in the object file. */
109a640b 715 if (howto->complain_on_overflow != complain_overflow_dont)
65cab589 716 {
109a640b
KR
717 bfd_vma check;
718
719 /* Get the value that will be used for the relocation, but
720 starting at bit position zero. */
721 if (howto->rightshift > howto->bitpos)
722 check = relocation >> (howto->rightshift - howto->bitpos);
723 else
724 check = relocation << (howto->bitpos - howto->rightshift);
725 switch (howto->complain_on_overflow)
726 {
727 case complain_overflow_signed:
728 {
729 /* Assumes two's complement. */
730 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
326e32d7 731 bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
109a640b
KR
732
733 /* The above right shift is incorrect for a signed value.
734 Fix it up by forcing on the upper bits. */
735 if (howto->rightshift > howto->bitpos
736 && (bfd_signed_vma) relocation < 0)
326e32d7
ILT
737 check |= ((bfd_vma) - 1
738 & ~((bfd_vma) - 1
109a640b
KR
739 >> (howto->rightshift - howto->bitpos)));
740 if ((bfd_signed_vma) check > reloc_signed_max
741 || (bfd_signed_vma) check < reloc_signed_min)
742 flag = bfd_reloc_overflow;
743 }
744 break;
745 case complain_overflow_unsigned:
746 {
747 /* Assumes two's complement. This expression avoids
748 overflow if howto->bitsize is the number of bits in
749 bfd_vma. */
750 bfd_vma reloc_unsigned_max =
326e32d7 751 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
109a640b
KR
752
753 if ((bfd_vma) check > reloc_unsigned_max)
754 flag = bfd_reloc_overflow;
755 }
756 break;
757 case complain_overflow_bitfield:
758 {
759 /* Assumes two's complement. This expression avoids
760 overflow if howto->bitsize is the number of bits in
761 bfd_vma. */
762 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
763
326e32d7
ILT
764 if (((bfd_vma) check & ~reloc_bits) != 0
765 && ((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
a49880c8
KR
766 {
767 /* The above right shift is incorrect for a signed
768 value. See if turning on the upper bits fixes the
769 overflow. */
770 if (howto->rightshift > howto->bitpos
771 && (bfd_signed_vma) relocation < 0)
772 {
326e32d7
ILT
773 check |= ((bfd_vma) - 1
774 & ~((bfd_vma) - 1
a49880c8 775 >> (howto->rightshift - howto->bitpos)));
326e32d7 776 if (((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
a49880c8
KR
777 flag = bfd_reloc_overflow;
778 }
779 else
780 flag = bfd_reloc_overflow;
781 }
109a640b
KR
782 }
783 break;
784 default:
785 abort ();
786 }
65cab589 787 }
326e32d7
ILT
788
789 /*
985fca12
SC
790 Either we are relocating all the way, or we don't want to apply
791 the relocation to the reloc entry (probably because there isn't
792 any room in the output format to describe addends to relocs)
793 */
c188b0be
DM
794
795 /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
796 (OSF version 1.3, compiler version 3.11). It miscompiles the
797 following program:
798
799 struct str
800 {
801 unsigned int i0;
802 } s = { 0 };
803
804 int
805 main ()
806 {
807 unsigned long x;
808
809 x = 0x100000000;
810 x <<= (unsigned long) s.i0;
811 if (x == 0)
812 printf ("failed\n");
813 else
814 printf ("succeeded (%lx)\n", x);
815 }
816 */
817
818 relocation >>= (bfd_vma) howto->rightshift;
985fca12
SC
819
820 /* Shift everything up to where it's going to be used */
326e32d7 821
c188b0be 822 relocation <<= (bfd_vma) howto->bitpos;
985fca12
SC
823
824 /* Wait for the day when all have the mask in them */
825
826 /* What we do:
827 i instruction to be left alone
828 o offset within instruction
829 r relocation offset to apply
830 S src mask
831 D dst mask
832 N ~dst mask
833 A part 1
834 B part 2
835 R result
326e32d7 836
985fca12
SC
837 Do this:
838 i i i i i o o o o o from bfd_get<size>
839 and S S S S S to get the size offset we want
840 + r r r r r r r r r r to get the final value to place
841 and D D D D D to chop to right size
842 -----------------------
326e32d7 843 A A A A A
985fca12
SC
844 And this:
845 ... i i i i i o o o o o from bfd_get<size>
846 and N N N N N get instruction
847 -----------------------
848 ... B B B B B
326e32d7
ILT
849
850 And then:
851 B B B B B
852 or A A A A A
985fca12
SC
853 -----------------------
854 R R R R R R R R R R put into bfd_put<size>
855 */
856
857#define DOIT(x) \
858 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
859
326e32d7
ILT
860 switch (howto->size)
861 {
862 case 0:
863 {
864 char x = bfd_get_8 (abfd, (char *) data + addr);
865 DOIT (x);
866 bfd_put_8 (abfd, x, (unsigned char *) data + addr);
867 }
868 break;
869
870 case 1:
871 if (relocation)
872 {
873 short x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
874 DOIT (x);
875 bfd_put_16 (abfd, x, (unsigned char *) data + addr);
876 }
877 break;
878 case 2:
879 if (relocation)
880 {
881 long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
882 DOIT (x);
883 bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
884 }
885 break;
886 case -2:
887 {
888 long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
889 relocation = -relocation;
890 DOIT (x);
891 bfd_put_32 (abfd, x, (bfd_byte *) data + addr);
892 }
893 break;
894
895 case 3:
896 /* Do nothing */
897 break;
898
899 case 4:
109a640b 900#ifdef BFD64
326e32d7
ILT
901 if (relocation)
902 {
903 bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + addr);
904 DOIT (x);
905 bfd_put_64 (abfd, x, (bfd_byte *) data + addr);
906 }
109a640b 907#else
326e32d7 908 abort ();
109a640b 909#endif
326e32d7
ILT
910 break;
911 default:
912 return bfd_reloc_other;
913 }
985fca12
SC
914
915 return flag;
916}
c618de01 917
4c3721d5
ILT
918/* This relocation routine is used by some of the backend linkers.
919 They do not construct asymbol or arelent structures, so there is no
920 reason for them to use bfd_perform_relocation. Also,
921 bfd_perform_relocation is so hacked up it is easier to write a new
922 function than to try to deal with it.
923
924 This routine does a final relocation. It should not be used when
925 generating relocateable output.
926
927 FIXME: This routine ignores any special_function in the HOWTO,
928 since the existing special_function values have been written for
929 bfd_perform_relocation.
930
931 HOWTO is the reloc howto information.
932 INPUT_BFD is the BFD which the reloc applies to.
933 INPUT_SECTION is the section which the reloc applies to.
934 CONTENTS is the contents of the section.
935 ADDRESS is the address of the reloc within INPUT_SECTION.
936 VALUE is the value of the symbol the reloc refers to.
937 ADDEND is the addend of the reloc. */
938
939bfd_reloc_status_type
940_bfd_final_link_relocate (howto, input_bfd, input_section, contents, address,
326e32d7 941 value, addend)
4c3721d5
ILT
942 const reloc_howto_type *howto;
943 bfd *input_bfd;
944 asection *input_section;
945 bfd_byte *contents;
946 bfd_vma address;
947 bfd_vma value;
948 bfd_vma addend;
949{
950 bfd_vma relocation;
c618de01 951
4c3721d5
ILT
952 /* Sanity check the address. */
953 if (address > input_section->_cooked_size)
954 return bfd_reloc_outofrange;
955
956 /* This function assumes that we are dealing with a basic relocation
957 against a symbol. We want to compute the value of the symbol to
958 relocate to. This is just VALUE, the value of the symbol, plus
959 ADDEND, any addend associated with the reloc. */
960 relocation = value + addend;
961
962 /* If the relocation is PC relative, we want to set RELOCATION to
963 the distance between the symbol (currently in RELOCATION) and the
964 location we are relocating. Some targets (e.g., i386-aout)
965 arrange for the contents of the section to be the negative of the
966 offset of the location within the section; for such targets
967 pcrel_offset is false. Other targets (e.g., m88kbcs or ELF)
968 simply leave the contents of the section as zero; for such
969 targets pcrel_offset is true. If pcrel_offset is false we do not
970 need to subtract out the offset of the location within the
971 section (which is just ADDRESS). */
972 if (howto->pc_relative)
973 {
974 relocation -= (input_section->output_section->vma
975 + input_section->output_offset);
976 if (howto->pcrel_offset)
977 relocation -= address;
978 }
326e32d7 979
4c3721d5
ILT
980 return _bfd_relocate_contents (howto, input_bfd, relocation,
981 contents + address);
982}
983
984/* Relocate a given location using a given value and howto. */
985
986bfd_reloc_status_type
987_bfd_relocate_contents (howto, input_bfd, relocation, location)
988 const reloc_howto_type *howto;
989 bfd *input_bfd;
990 bfd_vma relocation;
991 bfd_byte *location;
992{
993 int size;
994 bfd_vma x;
995 boolean overflow;
996
997 /* If the size is negative, negate RELOCATION. This isn't very
998 general. */
999 if (howto->size < 0)
326e32d7 1000 relocation = -relocation;
4c3721d5
ILT
1001
1002 /* Get the value we are going to relocate. */
1003 size = bfd_get_reloc_size (howto);
1004 switch (size)
1005 {
1006 default:
1007 case 0:
1008 abort ();
1009 case 1:
1010 x = bfd_get_8 (input_bfd, location);
1011 break;
1012 case 2:
1013 x = bfd_get_16 (input_bfd, location);
1014 break;
1015 case 4:
1016 x = bfd_get_32 (input_bfd, location);
1017 break;
1018 case 8:
1019#ifdef BFD64
1020 x = bfd_get_64 (input_bfd, location);
1021#else
1022 abort ();
1023#endif
1024 break;
1025 }
1026
1027 /* Check for overflow. FIXME: We may drop bits during the addition
1028 which we don't check for. We must either check at every single
1029 operation, which would be tedious, or we must do the computations
1030 in a type larger than bfd_vma, which would be inefficient. */
1031 overflow = false;
1032 if (howto->complain_on_overflow != complain_overflow_dont)
1033 {
1034 bfd_vma check;
1035 bfd_signed_vma signed_check;
1036 bfd_vma add;
563eb766 1037 bfd_signed_vma signed_add;
4c3721d5
ILT
1038
1039 if (howto->rightshift == 0)
1040 {
1041 check = relocation;
1042 signed_check = (bfd_signed_vma) relocation;
1043 }
1044 else
1045 {
1046 /* Drop unwanted bits from the value we are relocating to. */
1047 check = relocation >> howto->rightshift;
1048
1049 /* If this is a signed value, the rightshift just dropped
1050 leading 1 bits (assuming twos complement). */
1051 if ((bfd_signed_vma) relocation >= 0)
1052 signed_check = check;
1053 else
1054 signed_check = (check
326e32d7
ILT
1055 | ((bfd_vma) - 1
1056 & ~((bfd_vma) - 1 >> howto->rightshift)));
4c3721d5
ILT
1057 }
1058
3d51f02f 1059 /* Get the value from the object file. */
4c3721d5 1060 add = x & howto->src_mask;
3d51f02f
ILT
1061
1062 /* Get the value from the object file with an appropriate sign.
1063 The expression involving howto->src_mask isolates the upper
1064 bit of src_mask. If that bit is set in the value we are
1065 adding, it is negative, and we subtract out that number times
1066 two. If src_mask includes the highest possible bit, then we
1067 can not get the upper bit, but that does not matter since
1068 signed_add needs no adjustment to become negative in that
1069 case. */
1070 signed_add = add;
326e32d7
ILT
1071 if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
1072 signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
3d51f02f
ILT
1073
1074 /* Add the value from the object file, shifted so that it is a
1075 straight number. */
4c3721d5
ILT
1076 if (howto->bitpos == 0)
1077 {
1078 check += add;
563eb766 1079 signed_check += signed_add;
4c3721d5
ILT
1080 }
1081 else
1082 {
563eb766 1083 check += add >> howto->bitpos;
3d51f02f
ILT
1084
1085 /* For the signed case we use ADD, rather than SIGNED_ADD,
1086 to avoid warnings from SVR4 cc. This is OK since we
1087 explictly handle the sign bits. */
563eb766 1088 if (signed_add >= 0)
3d51f02f 1089 signed_check += add >> howto->bitpos;
563eb766 1090 else
3d51f02f 1091 signed_check += ((add >> howto->bitpos)
326e32d7
ILT
1092 | ((bfd_vma) - 1
1093 & ~((bfd_vma) - 1 >> howto->bitpos)));
4c3721d5
ILT
1094 }
1095
1096 switch (howto->complain_on_overflow)
1097 {
1098 case complain_overflow_signed:
1099 {
1100 /* Assumes two's complement. */
1101 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
326e32d7 1102 bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
4c3721d5
ILT
1103
1104 if (signed_check > reloc_signed_max
1105 || signed_check < reloc_signed_min)
1106 overflow = true;
1107 }
1108 break;
1109 case complain_overflow_unsigned:
1110 {
1111 /* Assumes two's complement. This expression avoids
1112 overflow if howto->bitsize is the number of bits in
1113 bfd_vma. */
1114 bfd_vma reloc_unsigned_max =
326e32d7 1115 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
4c3721d5
ILT
1116
1117 if (check > reloc_unsigned_max)
1118 overflow = true;
1119 }
1120 break;
1121 case complain_overflow_bitfield:
1122 {
1123 /* Assumes two's complement. This expression avoids
1124 overflow if howto->bitsize is the number of bits in
1125 bfd_vma. */
1126 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1127
326e32d7
ILT
1128 if ((check & ~reloc_bits) != 0
1129 && (((bfd_vma) signed_check & ~reloc_bits)
1130 != (-1 & ~reloc_bits)))
4c3721d5
ILT
1131 overflow = true;
1132 }
1133 break;
1134 default:
1135 abort ();
1136 }
1137 }
1138
1139 /* Put RELOCATION in the right bits. */
1140 relocation >>= (bfd_vma) howto->rightshift;
1141 relocation <<= (bfd_vma) howto->bitpos;
1142
1143 /* Add RELOCATION to the right bits of X. */
326e32d7 1144 x = ((x & ~howto->dst_mask)
4c3721d5
ILT
1145 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
1146
1147 /* Put the relocated value back in the object file. */
1148 switch (size)
1149 {
1150 default:
1151 case 0:
1152 abort ();
1153 case 1:
1154 bfd_put_8 (input_bfd, x, location);
1155 break;
1156 case 2:
1157 bfd_put_16 (input_bfd, x, location);
1158 break;
1159 case 4:
1160 bfd_put_32 (input_bfd, x, location);
1161 break;
1162 case 8:
1163#ifdef BFD64
1164 bfd_put_64 (input_bfd, x, location);
1165#else
1166 abort ();
1167#endif
1168 break;
1169 }
1170
1171 return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
1172}
2cf44d7b 1173
0cda46cf 1174/*
c26d7d17 1175DOCDD
e98e6ec1
SC
1176INODE
1177 howto manager, , typedef arelent, Relocations
1178
0cda46cf 1179SECTION
326e32d7 1180 The howto manager
2cf44d7b 1181
0cda46cf
SC
1182 When an application wants to create a relocation, but doesn't
1183 know what the target machine might call it, it can find out by
1184 using this bit of code.
2cf44d7b 1185
0cda46cf 1186*/
2cf44d7b 1187
0cda46cf
SC
1188/*
1189TYPEDEF
1190 bfd_reloc_code_type
2cf44d7b 1191
0cda46cf 1192DESCRIPTION
fb32909a
KR
1193 The insides of a reloc code. The idea is that, eventually, there
1194 will be one enumerator for every type of relocation we ever do.
1195 Pass one of these values to <<bfd_reloc_type_lookup>>, and it'll
1196 return a howto pointer.
1197
1198 This does mean that the application must determine the correct
1199 enumerator value; you can't get a howto pointer from a random set
1200 of attributes.
0cda46cf 1201
e98e6ec1
SC
1202CODE_FRAGMENT
1203.
326e32d7 1204.typedef enum bfd_reloc_code_real
e98e6ec1 1205.{
fb32909a 1206. {* Basic absolute relocations *}
defcfb55 1207. BFD_RELOC_64,
defcfb55 1208. BFD_RELOC_32,
e770a594 1209. BFD_RELOC_26,
326e32d7 1210. BFD_RELOC_16,
563eb766 1211. BFD_RELOC_14,
0cda46cf 1212. BFD_RELOC_8,
fb32909a
KR
1213.
1214. {* PC-relative relocations *}
1215. BFD_RELOC_64_PCREL,
1216. BFD_RELOC_32_PCREL,
1217. BFD_RELOC_24_PCREL, {* used by i960 *}
1218. BFD_RELOC_16_PCREL,
0cda46cf 1219. BFD_RELOC_8_PCREL,
e98e6ec1 1220.
fb32909a
KR
1221. {* Linkage-table relative *}
1222. BFD_RELOC_32_BASEREL,
1223. BFD_RELOC_16_BASEREL,
1224. BFD_RELOC_8_BASEREL,
e98e6ec1 1225.
fb32909a
KR
1226. {* The type of reloc used to build a contructor table - at the moment
1227. probably a 32 bit wide abs address, but the cpu can choose. *}
8070f29d
KR
1228. BFD_RELOC_CTOR,
1229.
fb32909a
KR
1230. {* 8 bits wide, but used to form an address like 0xffnn *}
1231. BFD_RELOC_8_FFnn,
1232.
1233. {* 32-bit pc-relative, shifted right 2 bits (i.e., 30-bit
1234. word displacement, e.g. for SPARC) *}
1235. BFD_RELOC_32_PCREL_S2,
563eb766
KR
1236. {* signed 16-bit pc-relative, shifted right 2 bits (e.g. for MIPS) *}
1237. BFD_RELOC_16_PCREL_S2,
1238. {* this is used on the Alpha *}
1239. BFD_RELOC_23_PCREL_S2,
fb32909a
KR
1240.
1241. {* High 22 bits of 32-bit value, placed into lower 22 bits of
1242. target word; simple reloc. *}
8070f29d 1243. BFD_RELOC_HI22,
fb32909a 1244. {* Low 10 bits. *}
8070f29d
KR
1245. BFD_RELOC_LO10,
1246.
563eb766
KR
1247. {* For systems that allocate a Global Pointer register, these are
1248. displacements off that register. These relocation types are
1249. handled specially, because the value the register will have is
1250. decided relatively late. *}
1251. BFD_RELOC_GPREL16,
1252. BFD_RELOC_GPREL32,
1253.
fb32909a 1254. {* Reloc types used for i960/b.out. *}
8070f29d
KR
1255. BFD_RELOC_I960_CALLJ,
1256.
8070f29d
KR
1257. {* now for the sparc/elf codes *}
1258. BFD_RELOC_NONE, {* actually used *}
1259. BFD_RELOC_SPARC_WDISP22,
1260. BFD_RELOC_SPARC22,
1261. BFD_RELOC_SPARC13,
8070f29d
KR
1262. BFD_RELOC_SPARC_GOT10,
1263. BFD_RELOC_SPARC_GOT13,
1264. BFD_RELOC_SPARC_GOT22,
1265. BFD_RELOC_SPARC_PC10,
1266. BFD_RELOC_SPARC_PC22,
1267. BFD_RELOC_SPARC_WPLT30,
1268. BFD_RELOC_SPARC_COPY,
1269. BFD_RELOC_SPARC_GLOB_DAT,
1270. BFD_RELOC_SPARC_JMP_SLOT,
1271. BFD_RELOC_SPARC_RELATIVE,
1272. BFD_RELOC_SPARC_UA32,
1273.
fb32909a 1274. {* these are a.out specific? *}
58acdbd7 1275. BFD_RELOC_SPARC_BASE13,
8070f29d
KR
1276. BFD_RELOC_SPARC_BASE22,
1277.
9180892d
KR
1278. {* some relocations we're using for sparc v9
1279. -- subject to change *}
defcfb55
KR
1280. BFD_RELOC_SPARC_10,
1281. BFD_RELOC_SPARC_11,
1282.#define BFD_RELOC_SPARC_64 BFD_RELOC_64
1283. BFD_RELOC_SPARC_OLO10,
1284. BFD_RELOC_SPARC_HH22,
1285. BFD_RELOC_SPARC_HM10,
1286. BFD_RELOC_SPARC_LM22,
1287. BFD_RELOC_SPARC_PC_HH22,
1288. BFD_RELOC_SPARC_PC_HM10,
1289. BFD_RELOC_SPARC_PC_LM22,
1290. BFD_RELOC_SPARC_WDISP16,
58acdbd7 1291. BFD_RELOC_SPARC_WDISP19,
defcfb55
KR
1292. BFD_RELOC_SPARC_GLOB_JMP,
1293. BFD_RELOC_SPARC_LO7,
58acdbd7 1294.
563eb766
KR
1295. {* Alpha ECOFF relocations. Some of these treat the symbol or "addend"
1296. in some special way. *}
1297. {* For GPDISP_HI16 ("gpdisp") relocations, the symbol is ignored when
1298. writing; when reading, it will be the absolute section symbol. The
1299. addend is the displacement in bytes of the "lda" instruction from
1300. the "ldah" instruction (which is at the address of this reloc). *}
1301. BFD_RELOC_ALPHA_GPDISP_HI16,
1302. {* For GPDISP_LO16 ("ignore") relocations, the symbol is handled as
1303. with GPDISP_HI16 relocs. The addend is ignored when writing the
1304. relocations out, and is filled in with the file's GP value on
1305. reading, for convenience. *}
1306. BFD_RELOC_ALPHA_GPDISP_LO16,
1307.
1308. {* The Alpha LITERAL/LITUSE relocs are produced by a symbol reference;
1309. the assembler turns it into a LDQ instruction to load the address of
1310. the symbol, and then fills in a register in the real instruction.
1311.
1312. The LITERAL reloc, at the LDQ instruction, refers to the .lita
1313. section symbol. The addend is ignored when writing, but is filled
1314. in with the file's GP value on reading, for convenience, as with the
1315. GPDISP_LO16 reloc.
1316.
1317. The LITUSE reloc, on the instruction using the loaded address, gives
1318. information to the linker that it might be able to use to optimize
1319. away some literal section references. The symbol is ignored (read
1320. as the absolute section symbol), and the "addend" indicates the type
1321. of instruction using the register:
1322. 1 - "memory" fmt insn
1323. 2 - byte-manipulation (byte offset reg)
1324. 3 - jsr (target of branch)
1325.
1326. The GNU linker currently doesn't do any of this optimizing. *}
1327. BFD_RELOC_ALPHA_LITERAL,
1328. BFD_RELOC_ALPHA_LITUSE,
1329.
1330. {* The HINT relocation indicates a value that should be filled into the
1331. "hint" field of a jmp/jsr/ret instruction, for possible branch-
1332. prediction logic which may be provided on some processors. *}
1333. BFD_RELOC_ALPHA_HINT,
1334.
fb32909a
KR
1335. {* Bits 27..2 of the relocation address shifted right 2 bits;
1336. simple reloc otherwise. *}
65cab589
DM
1337. BFD_RELOC_MIPS_JMP,
1338.
fb32909a 1339. {* High 16 bits of 32-bit value; simple reloc. *}
65cab589 1340. BFD_RELOC_HI16,
fb32909a
KR
1341. {* High 16 bits of 32-bit value but the low 16 bits will be sign
1342. extended and added to form the final result. If the low 16
1343. bits form a negative number, we need to add one to the high value
1344. to compensate for the borrow when the low bits are added. *}
65cab589 1345. BFD_RELOC_HI16_S,
fb32909a 1346. {* Low 16 bits. *}
65cab589 1347. BFD_RELOC_LO16,
9180892d
KR
1348. {* Like BFD_RELOC_HI16_S, but PC relative. *}
1349. BFD_RELOC_PCREL_HI16_S,
1350. {* Like BFD_RELOC_LO16, but PC relative. *}
1351. BFD_RELOC_PCREL_LO16,
65cab589 1352.
563eb766
KR
1353. {* relocation relative to the global pointer. *}
1354.#define BFD_RELOC_MIPS_GPREL BFD_RELOC_GPREL16
65cab589 1355.
c188b0be
DM
1356. {* Relocation against a MIPS literal section. *}
1357. BFD_RELOC_MIPS_LITERAL,
1358.
1359. {* MIPS ELF relocations. *}
1360. BFD_RELOC_MIPS_GOT16,
1361. BFD_RELOC_MIPS_CALL16,
563eb766 1362.#define BFD_RELOC_MIPS_GPREL32 BFD_RELOC_GPREL32
c188b0be 1363.
a49880c8
KR
1364. {* i386/elf relocations *}
1365. BFD_RELOC_386_GOT32,
1366. BFD_RELOC_386_PLT32,
1367. BFD_RELOC_386_COPY,
1368. BFD_RELOC_386_GLOB_DAT,
1369. BFD_RELOC_386_JUMP_SLOT,
1370. BFD_RELOC_386_RELATIVE,
1371. BFD_RELOC_386_GOTOFF,
1372. BFD_RELOC_386_GOTPC,
1373.
3d51f02f
ILT
1374. {* PowerPC/POWER (RS/6000) relocs. *}
1375. {* 26 bit relative branch. Low two bits must be zero. High 24
1376. bits installed in bits 6 through 29 of instruction. *}
1377. BFD_RELOC_PPC_B26,
1378. {* 26 bit absolute branch, like BFD_RELOC_PPC_B26 but absolute. *}
1379. BFD_RELOC_PPC_BA26,
1380. {* 16 bit TOC relative reference. *}
1381. BFD_RELOC_PPC_TOC16,
1382.
8070f29d
KR
1383. {* this must be the highest numeric value *}
1384. BFD_RELOC_UNUSED
0cda46cf 1385. } bfd_reloc_code_real_type;
2cf44d7b
SC
1386*/
1387
1388
0cda46cf 1389/*
c188b0be 1390FUNCTION
0cda46cf 1391 bfd_reloc_type_lookup
2cf44d7b 1392
e98e6ec1 1393SYNOPSIS
4c3721d5 1394 const struct reloc_howto_struct *
3860075f 1395 bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
e98e6ec1 1396
0cda46cf 1397DESCRIPTION
4c3721d5 1398 Return a pointer to a howto structure which, when
c188b0be 1399 invoked, will perform the relocation @var{code} on data from the
0cda46cf 1400 architecture noted.
2cf44d7b 1401
2cf44d7b
SC
1402*/
1403
1404
4c3721d5 1405const struct reloc_howto_struct *
326e32d7
ILT
1406bfd_reloc_type_lookup (abfd, code)
1407 bfd *abfd;
1408 bfd_reloc_code_real_type code;
2cf44d7b 1409{
8070f29d 1410 return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
2cf44d7b
SC
1411}
1412
0cda46cf 1413static reloc_howto_type bfd_howto_32 =
326e32d7 1414HOWTO (0, 00, 2, 32, false, 0, complain_overflow_bitfield, 0, "VRT32", false, 0xffffffff, 0xffffffff, true);
2cf44d7b
SC
1415
1416
0cda46cf 1417/*
e98e6ec1 1418INTERNAL_FUNCTION
0cda46cf
SC
1419 bfd_default_reloc_type_lookup
1420
0cda46cf 1421SYNOPSIS
4c3721d5 1422 const struct reloc_howto_struct *bfd_default_reloc_type_lookup
326e32d7 1423 (bfd *abfd, bfd_reloc_code_real_type code);
0cda46cf 1424
e98e6ec1 1425DESCRIPTION
65cab589 1426 Provides a default relocation lookup routine for any architecture.
e98e6ec1
SC
1427
1428
0cda46cf 1429*/
65cab589 1430
4c3721d5 1431const struct reloc_howto_struct *
326e32d7
ILT
1432bfd_default_reloc_type_lookup (abfd, code)
1433 bfd *abfd;
1434 bfd_reloc_code_real_type code;
0cda46cf 1435{
326e32d7 1436 switch (code)
0cda46cf 1437 {
65cab589
DM
1438 case BFD_RELOC_CTOR:
1439 /* The type of reloc used in a ctor, which will be as wide as the
fb32909a 1440 address - so either a 64, 32, or 16 bitter. */
326e32d7
ILT
1441 switch (bfd_get_arch_info (abfd)->bits_per_address)
1442 {
1443 case 64:
1444 BFD_FAIL ();
1445 case 32:
1446 return &bfd_howto_32;
1447 case 16:
1448 BFD_FAIL ();
1449 default:
1450 BFD_FAIL ();
1451 }
65cab589 1452 default:
326e32d7 1453 BFD_FAIL ();
0cda46cf 1454 }
326e32d7 1455 return (const struct reloc_howto_struct *) NULL;
0cda46cf 1456}
e98e6ec1
SC
1457
1458
d58b7049
SC
1459/*
1460INTERNAL_FUNCTION
1461 bfd_generic_relax_section
1462
1463SYNOPSIS
1464 boolean bfd_generic_relax_section
1465 (bfd *abfd,
1466 asection *section,
4c3721d5 1467 struct bfd_link_info *,
326e32d7 1468 boolean *);
d58b7049
SC
1469
1470DESCRIPTION
1471 Provides default handling for relaxing for back ends which
8070f29d 1472 don't do relaxing -- i.e., does nothing.
d58b7049
SC
1473*/
1474
563eb766 1475/*ARGSUSED*/
d58b7049 1476boolean
326e32d7 1477bfd_generic_relax_section (abfd, section, link_info, again)
4c3721d5
ILT
1478 bfd *abfd;
1479 asection *section;
1480 struct bfd_link_info *link_info;
326e32d7 1481 boolean *again;
d58b7049 1482{
326e32d7
ILT
1483 *again = false;
1484 return true;
d58b7049 1485}
326e32d7 1486
e98e6ec1
SC
1487/*
1488INTERNAL_FUNCTION
1489 bfd_generic_get_relocated_section_contents
1490
1491SYNOPSIS
1492 bfd_byte *
65cab589 1493 bfd_generic_get_relocated_section_contents (bfd *abfd,
4c3721d5
ILT
1494 struct bfd_link_info *link_info,
1495 struct bfd_link_order *link_order,
65cab589 1496 bfd_byte *data,
4c3721d5
ILT
1497 boolean relocateable,
1498 asymbol **symbols);
e98e6ec1
SC
1499
1500DESCRIPTION
1501 Provides default handling of relocation effort for back ends
1502 which can't be bothered to do it efficiently.
1503
1504*/
1505
1506bfd_byte *
4c3721d5
ILT
1507bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data,
1508 relocateable, symbols)
1509 bfd *abfd;
1510 struct bfd_link_info *link_info;
1511 struct bfd_link_order *link_order;
1512 bfd_byte *data;
1513 boolean relocateable;
1514 asymbol **symbols;
e98e6ec1 1515{
e98e6ec1 1516 /* Get enough memory to hold the stuff */
4c3721d5
ILT
1517 bfd *input_bfd = link_order->u.indirect.section->owner;
1518 asection *input_section = link_order->u.indirect.section;
e98e6ec1 1519
326e32d7 1520 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
80425e6c 1521 arelent **reloc_vector = NULL;
326e32d7
ILT
1522 long reloc_count;
1523
1524 if (reloc_size < 0)
1525 goto error_return;
80425e6c
JK
1526
1527 reloc_vector = (arelent **) malloc (reloc_size);
326e32d7 1528 if (reloc_vector == NULL && reloc_size != 0)
80425e6c
JK
1529 {
1530 bfd_set_error (bfd_error_no_memory);
1531 goto error_return;
1532 }
326e32d7 1533
e98e6ec1 1534 /* read in the section */
326e32d7
ILT
1535 if (!bfd_get_section_contents (input_bfd,
1536 input_section,
1537 (PTR) data,
1538 0,
1539 input_section->_raw_size))
80425e6c
JK
1540 goto error_return;
1541
1542 /* We're not relaxing the section, so just copy the size info */
e98e6ec1
SC
1543 input_section->_cooked_size = input_section->_raw_size;
1544 input_section->reloc_done = true;
e98e6ec1 1545
326e32d7
ILT
1546 reloc_count = bfd_canonicalize_reloc (input_bfd,
1547 input_section,
1548 reloc_vector,
1549 symbols);
1550 if (reloc_count < 0)
80425e6c
JK
1551 goto error_return;
1552
326e32d7
ILT
1553 if (reloc_count > 0)
1554 {
1555 arelent **parent;
1556 for (parent = reloc_vector; *parent != (arelent *) NULL;
1557 parent++)
65cab589 1558 {
326e32d7
ILT
1559 char *error_message = (char *) NULL;
1560 bfd_reloc_status_type r =
1561 bfd_perform_relocation (input_bfd,
1562 *parent,
1563 (PTR) data,
1564 input_section,
1565 relocateable ? abfd : (bfd *) NULL,
1566 &error_message);
1567
1568 if (relocateable)
1569 {
1570 asection *os = input_section->output_section;
65cab589 1571
326e32d7
ILT
1572 /* A partial link, so keep the relocs */
1573 os->orelocation[os->reloc_count] = *parent;
1574 os->reloc_count++;
1575 }
e98e6ec1 1576
326e32d7
ILT
1577 if (r != bfd_reloc_ok)
1578 {
1579 switch (r)
1580 {
1581 case bfd_reloc_undefined:
1582 if (!((*link_info->callbacks->undefined_symbol)
1583 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
1584 input_bfd, input_section, (*parent)->address)))
1585 goto error_return;
1586 break;
1587 case bfd_reloc_dangerous:
1588 BFD_ASSERT (error_message != (char *) NULL);
1589 if (!((*link_info->callbacks->reloc_dangerous)
1590 (link_info, error_message, input_bfd, input_section,
1591 (*parent)->address)))
1592 goto error_return;
1593 break;
1594 case bfd_reloc_overflow:
1595 if (!((*link_info->callbacks->reloc_overflow)
1596 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
1597 (*parent)->howto->name, (*parent)->addend,
1598 input_bfd, input_section, (*parent)->address)))
1599 goto error_return;
1600 break;
1601 case bfd_reloc_outofrange:
1602 default:
1603 abort ();
1604 break;
1605 }
e98e6ec1 1606
326e32d7
ILT
1607 }
1608 }
1609 }
80425e6c
JK
1610 if (reloc_vector != NULL)
1611 free (reloc_vector);
e98e6ec1
SC
1612 return data;
1613
326e32d7 1614error_return:
80425e6c
JK
1615 if (reloc_vector != NULL)
1616 free (reloc_vector);
1617 return NULL;
e98e6ec1 1618}
This page took 0.485947 seconds and 4 git commands to generate.