* scripttempl/aout.sc: Pad .text to DATA_ALIGNMENT if relocating;
[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*/
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.
60.typedef enum bfd_reloc_status
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.
0cda46cf
SC
92.typedef struct reloc_cache_entry
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 *}
101. bfd_vma addend;
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
SC
164|RELOCATION RECORDS FOR [.text]:
165|offset type value
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
SC
189|RELOCATION RECORDS FOR [.text]:
190|offset type value
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
c188b0be 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
SC
222|RELOCATION RECORDS FOR [.text]:
223|offset type value
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.
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
SC
275/*
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.
c188b0be 285.typedef struct reloc_howto_struct
0cda46cf 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). *}
fefb4b30
JG
326. bfd_reloc_status_type (*special_function)
327. PARAMS ((bfd *abfd,
5022aea5
SC
328. arelent *reloc_entry,
329. struct symbol_cache_entry *symbol,
330. PTR data,
8070f29d 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. *}
65cab589 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. } \
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{
420 switch (howto->size) {
421 case 0: return 1;
422 case 1: return 2;
423 case 2: return 4;
424 case 3: return 0;
425 case 4: return 8;
426 case -2: return 2;
427 default: abort ();
428 }
429}
430
0cda46cf
SC
431/*
432TYPEDEF
c188b0be 433 arelent_chain
985fca12 434
0cda46cf 435DESCRIPTION
985fca12 436
c188b0be 437 How relocs are tied together in an <<asection>>:
985fca12 438
0cda46cf
SC
439.typedef unsigned char bfd_byte;
440.
441.typedef struct relent_chain {
442. arelent relent;
443. struct relent_chain *next;
444.} arelent_chain;
985fca12
SC
445
446*/
447
448
449
0cda46cf
SC
450/*
451FUNCTION
452 bfd_perform_relocation
453
e98e6ec1
SC
454SYNOPSIS
455 bfd_reloc_status_type
456 bfd_perform_relocation
c188b0be 457 (bfd *abfd,
4c3721d5
ILT
458 arelent *reloc_entry,
459 PTR data,
460 asection *input_section,
461 bfd *output_bfd,
462 char **error_message);
e98e6ec1 463
0cda46cf 464DESCRIPTION
4c3721d5
ILT
465 If @var{output_bfd} is supplied to this function, the
466 generated image will be relocatable; the relocations are
467 copied to the output file after they have been changed to
468 reflect the new state of the world. There are two ways of
469 reflecting the results of partial linkage in an output file:
470 by modifying the output data in place, and by modifying the
471 relocation record. Some native formats (e.g., basic a.out and
472 basic coff) have no way of specifying an addend in the
473 relocation type, so the addend has to go in the output data.
474 This is no big deal since in these formats the output data
475 slot will always be big enough for the addend. Complex reloc
476 types with addends were invented to solve just this problem.
477 The @var{error_message} argument is set to an error message if
478 this return @code{bfd_reloc_dangerous}.
0cda46cf 479
985fca12
SC
480*/
481
482
0cda46cf 483bfd_reloc_status_type
4c3721d5
ILT
484bfd_perform_relocation (abfd, reloc_entry, data, input_section, output_bfd,
485 error_message)
486 bfd *abfd;
487 arelent *reloc_entry;
488 PTR data;
489 asection *input_section;
490 bfd *output_bfd;
491 char **error_message;
985fca12
SC
492{
493 bfd_vma relocation;
0cda46cf 494 bfd_reloc_status_type flag = bfd_reloc_ok;
65cab589 495 bfd_size_type addr = reloc_entry->address ;
985fca12 496 bfd_vma output_base = 0;
4c3721d5
ILT
497 const reloc_howto_type *howto = reloc_entry->howto;
498 asection *reloc_target_output_section;
985fca12
SC
499 asymbol *symbol;
500
4c3721d5 501 symbol = *(reloc_entry->sym_ptr_ptr);
5022aea5
SC
502 if ((symbol->section == &bfd_abs_section)
503 && output_bfd != (bfd *)NULL)
58acdbd7
KR
504 {
505 reloc_entry->address += input_section->output_offset;
506 return bfd_reloc_ok;
507 }
508
fb32909a
KR
509 /* If we are not producing relocateable output, return an error if
510 the symbol is not defined. An undefined weak symbol is
511 considered to have a value of zero (SVR4 ABI, p. 4-27). */
512 if (symbol->section == &bfd_und_section
513 && (symbol->flags & BSF_WEAK) == 0
514 && output_bfd == (bfd *) NULL)
5022aea5 515 flag = bfd_reloc_undefined;
985fca12 516
58acdbd7
KR
517 /* If there is a function supplied to handle this relocation type,
518 call it. It'll return `bfd_reloc_continue' if further processing
519 can be done. */
520 if (howto->special_function)
521 {
522 bfd_reloc_status_type cont;
523 cont = howto->special_function (abfd, reloc_entry, symbol, data,
4c3721d5
ILT
524 input_section, output_bfd,
525 error_message);
58acdbd7
KR
526 if (cont != bfd_reloc_continue)
527 return cont;
528 }
985fca12 529
58acdbd7
KR
530 /* Is the address of the relocation really within the section? */
531 if (reloc_entry->address > input_section->_cooked_size)
532 return bfd_reloc_outofrange;
985fca12 533
58acdbd7
KR
534 /* Work out which section the relocation is targetted at and the
535 initial relocation command value. */
536
537 /* Get symbol value. (Common symbols are special.) */
538 if (bfd_is_com_section (symbol->section))
5022aea5 539 relocation = 0;
58acdbd7 540 else
5022aea5 541 relocation = symbol->value;
985fca12 542
985fca12 543
e98e6ec1 544 reloc_target_output_section = symbol->section->output_section;
985fca12 545
58acdbd7
KR
546 /* Convert input-section-relative symbol value to absolute. */
547 if (output_bfd && howto->partial_inplace==false)
5022aea5 548 output_base = 0;
58acdbd7 549 else
5022aea5 550 output_base = reloc_target_output_section->vma;
985fca12 551
65cab589 552 relocation += output_base + symbol->section->output_offset;
985fca12 553
58acdbd7 554 /* Add in supplied addend. */
65cab589 555 relocation += reloc_entry->addend;
985fca12 556
c188b0be
DM
557 /* Here the variable relocation holds the final address of the
558 symbol we are relocating against, plus any addend. */
559
985fca12 560 if (howto->pc_relative == true)
58acdbd7 561 {
c188b0be
DM
562 /* This is a PC relative relocation. We want to set RELOCATION
563 to the distance between the address of the symbol and the
564 location. RELOCATION is already the address of the symbol.
565
566 We start by subtracting the address of the section containing
567 the location.
568
569 If pcrel_offset is set, we must further subtract the position
570 of the location within the section. Some targets arrange for
571 the addend to be the negative of the position of the location
572 within the section; for example, i386-aout does this. For
573 i386-aout, pcrel_offset is false. Some other targets do not
574 include the position of the location; for example, m88kbcs,
575 or ELF. For those targets, pcrel_offset is true.
576
577 If we are producing relocateable output, then we must ensure
578 that this reloc will be correctly computed when the final
579 relocation is done. If pcrel_offset is false we want to wind
580 up with the negative of the location within the section,
581 which means we must adjust the existing addend by the change
582 in the location within the section. If pcrel_offset is true
583 we do not want to adjust the existing addend at all.
584
585 FIXME: This seems logical to me, but for the case of
586 producing relocateable output it is not what the code
587 actually does. I don't want to change it, because it seems
588 far too likely that something will break. */
985fca12 589
58acdbd7
KR
590 relocation -=
591 input_section->output_section->vma + input_section->output_offset;
592
593 if (howto->pcrel_offset == true)
594 relocation -= reloc_entry->address;
5022aea5 595 }
e98e6ec1 596
c26d7d17 597 if (output_bfd!= (bfd *)NULL)
5022aea5 598 {
58acdbd7
KR
599 if ( howto->partial_inplace == false)
600 {
601 /* This is a partial relocation, and we want to apply the relocation
602 to the reloc entry rather than the raw data. Modify the reloc
603 inplace to reflect what we now know. */
604 reloc_entry->addend = relocation;
605 reloc_entry->address += input_section->output_offset;
606 return flag;
607 }
c26d7d17 608 else
58acdbd7
KR
609 {
610 /* This is a partial relocation, but inplace, so modify the
611 reloc record a bit.
612
613 If we've relocated with a symbol with a section, change
614 into a ref to the section belonging to the symbol. */
615
616 reloc_entry->address += input_section->output_offset;
617
618 /* WTF?? */
619 if (abfd->xvec->flavour == bfd_target_coff_flavour)
620 {
c188b0be
DM
621#if 1
622 /* For m68k-coff, the addend was being subtracted twice during
623 relocation with -r. Removing the line below this comment
624 fixes that problem; see PR 2953.
625
626However, Ian wrote the following, regarding removing the line below,
627which explains why it is still enabled: --djm
628
629If you put a patch like that into BFD you need to check all the COFF
630linkers. I am fairly certain that patch will break coff-i386 (e.g.,
631SCO); see coff_i386_reloc in coff-i386.c where I worked around the
632problem in a different way. There may very well be a reason that the
633code works as it does.
634
635Hmmm. The first obvious point is that bfd_perform_relocation should
636not have any tests that depend upon the flavour. It's seem like
637entirely the wrong place for such a thing. The second obvious point
638is that the current code ignores the reloc addend when producing
639relocateable output for COFF. That's peculiar. In fact, I really
640have no idea what the point of the line you want to remove is.
641
642A typical COFF reloc subtracts the old value of the symbol and adds in
643the new value to the location in the object file (if it's a pc
644relative reloc it adds the difference between the symbol value and the
645location). When relocating we need to preserve that property.
646
647BFD handles this by setting the addend to the negative of the old
648value of the symbol. Unfortunately it handles common symbols in a
649non-standard way (it doesn't subtract the old value) but that's a
650different story (we can't change it without losing backward
651compatibility with old object files) (coff-i386 does subtract the old
652value, to be compatible with existing coff-i386 targets, like SCO).
653
654So everything works fine when not producing relocateable output. When
655we are producing relocateable output, logically we should do exactly
656what we do when not producing relocateable output. Therefore, your
657patch is correct. In fact, it should probably always just set
658reloc_entry->addend to 0 for all cases, since it is, in fact, going to
659add the value into the object file. This won't hurt the COFF code,
660which doesn't use the addend; I'm not sure what it will do to other
661formats (the thing to check for would be whether any formats both use
662the addend and set partial_inplace).
663
664When I wanted to make coff-i386 produce relocateable output, I ran
665into the problem that you are running into: I wanted to remove that
666line. Rather than risk it, I made the coff-i386 relocs use a special
667function; it's coff_i386_reloc in coff-i386.c. The function
668specifically adds the addend field into the object file, knowing that
669bfd_perform_relocation is not going to. If you remove that line, then
670coff-i386.c will wind up adding the addend field in twice. It's
671trivial to fix; it just needs to be done.
672
673The problem with removing the line is just that it may break some
674working code. With BFD it's hard to be sure of anything. The right
675way to deal with this is simply to build and test at least all the
676supported COFF targets. It should be straightforward if time and disk
677space consuming. For each target:
678 1) build the linker
679 2) generate some executable, and link it using -r (I would
680 probably use paranoia.o and link against newlib/libc.a, which
681 for all the supported targets would be available in
682 /usr/cygnus/progressive/H-host/target/lib/libc.a).
683 3) make the change to reloc.c
684 4) rebuild the linker
685 5) repeat step 2
686 6) if the resulting object files are the same, you have at least
687 made it no worse
688 7) if they are different you have to figure out which version is
689 right
690*/
58acdbd7 691 relocation -= reloc_entry->addend;
c188b0be 692#endif
58acdbd7
KR
693 reloc_entry->addend = 0;
694 }
695 else
696 {
697 reloc_entry->addend = relocation;
698 }
699 }
985fca12 700 }
e98e6ec1 701 else
58acdbd7
KR
702 {
703 reloc_entry->addend = 0;
704 }
985fca12 705
66a277ab
ILT
706 /* FIXME: This overflow checking is incomplete, because the value
707 might have overflowed before we get here. For a correct check we
708 need to compute the value in a size larger than bitsize, but we
709 can't reasonably do that for a reloc the same size as a host
a49880c8
KR
710 machine word.
711 FIXME: We should also do overflow checking on the result after
712 adding in the value contained in the object file. */
109a640b 713 if (howto->complain_on_overflow != complain_overflow_dont)
65cab589 714 {
109a640b
KR
715 bfd_vma check;
716
717 /* Get the value that will be used for the relocation, but
718 starting at bit position zero. */
719 if (howto->rightshift > howto->bitpos)
720 check = relocation >> (howto->rightshift - howto->bitpos);
721 else
722 check = relocation << (howto->bitpos - howto->rightshift);
723 switch (howto->complain_on_overflow)
724 {
725 case complain_overflow_signed:
726 {
727 /* Assumes two's complement. */
728 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
729 bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
730
731 /* The above right shift is incorrect for a signed value.
732 Fix it up by forcing on the upper bits. */
733 if (howto->rightshift > howto->bitpos
734 && (bfd_signed_vma) relocation < 0)
735 check |= ((bfd_vma) -1
736 &~ ((bfd_vma) -1
737 >> (howto->rightshift - howto->bitpos)));
738 if ((bfd_signed_vma) check > reloc_signed_max
739 || (bfd_signed_vma) check < reloc_signed_min)
740 flag = bfd_reloc_overflow;
741 }
742 break;
743 case complain_overflow_unsigned:
744 {
745 /* Assumes two's complement. This expression avoids
746 overflow if howto->bitsize is the number of bits in
747 bfd_vma. */
748 bfd_vma reloc_unsigned_max =
749 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
750
751 if ((bfd_vma) check > reloc_unsigned_max)
752 flag = bfd_reloc_overflow;
753 }
754 break;
755 case complain_overflow_bitfield:
756 {
757 /* Assumes two's complement. This expression avoids
758 overflow if howto->bitsize is the number of bits in
759 bfd_vma. */
760 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
761
762 if (((bfd_vma) check &~ reloc_bits) != 0
763 && ((bfd_vma) check &~ reloc_bits) != (-1 &~ reloc_bits))
a49880c8
KR
764 {
765 /* The above right shift is incorrect for a signed
766 value. See if turning on the upper bits fixes the
767 overflow. */
768 if (howto->rightshift > howto->bitpos
769 && (bfd_signed_vma) relocation < 0)
770 {
771 check |= ((bfd_vma) -1
772 &~ ((bfd_vma) -1
773 >> (howto->rightshift - howto->bitpos)));
774 if (((bfd_vma) check &~ reloc_bits) != (-1 &~ reloc_bits))
775 flag = bfd_reloc_overflow;
776 }
777 else
778 flag = bfd_reloc_overflow;
779 }
109a640b
KR
780 }
781 break;
782 default:
783 abort ();
784 }
65cab589
DM
785 }
786
985fca12
SC
787 /*
788 Either we are relocating all the way, or we don't want to apply
789 the relocation to the reloc entry (probably because there isn't
790 any room in the output format to describe addends to relocs)
791 */
c188b0be
DM
792
793 /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
794 (OSF version 1.3, compiler version 3.11). It miscompiles the
795 following program:
796
797 struct str
798 {
799 unsigned int i0;
800 } s = { 0 };
801
802 int
803 main ()
804 {
805 unsigned long x;
806
807 x = 0x100000000;
808 x <<= (unsigned long) s.i0;
809 if (x == 0)
810 printf ("failed\n");
811 else
812 printf ("succeeded (%lx)\n", x);
813 }
814 */
815
816 relocation >>= (bfd_vma) howto->rightshift;
985fca12
SC
817
818 /* Shift everything up to where it's going to be used */
819
c188b0be 820 relocation <<= (bfd_vma) howto->bitpos;
985fca12
SC
821
822 /* Wait for the day when all have the mask in them */
823
824 /* What we do:
825 i instruction to be left alone
826 o offset within instruction
827 r relocation offset to apply
828 S src mask
829 D dst mask
830 N ~dst mask
831 A part 1
832 B part 2
833 R result
834
835 Do this:
836 i i i i i o o o o o from bfd_get<size>
837 and S S S S S to get the size offset we want
838 + r r r r r r r r r r to get the final value to place
839 and D D D D D to chop to right size
840 -----------------------
841 A A A A A
842 And this:
843 ... i i i i i o o o o o from bfd_get<size>
844 and N N N N N get instruction
845 -----------------------
846 ... B B B B B
847
848 And then:
849 B B B B B
850 or A A A A A
851 -----------------------
852 R R R R R R R R R R put into bfd_put<size>
853 */
854
855#define DOIT(x) \
856 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
857
e98e6ec1 858 switch (howto->size)
65cab589
DM
859 {
860 case 0:
861 {
862 char x = bfd_get_8(abfd, (char *)data + addr);
863 DOIT(x);
864 bfd_put_8(abfd,x, (unsigned char *) data + addr);
865 }
866 break;
867
868 case 1:
869 if (relocation)
870 {
871 short x = bfd_get_16(abfd, (bfd_byte *)data + addr);
872 DOIT(x);
873 bfd_put_16(abfd, x, (unsigned char *)data + addr);
874 }
875 break;
876 case 2:
877 if (relocation)
878 {
879 long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
880 DOIT (x);
881 bfd_put_32 (abfd, x, (bfd_byte *)data + addr);
882 }
883 break;
884 case -2:
885 {
886 long x = bfd_get_32(abfd, (bfd_byte *) data + addr);
887 relocation = -relocation;
888 DOIT(x);
889 bfd_put_32(abfd,x, (bfd_byte *)data + addr);
890 }
891 break;
892
893 case 3:
65cab589
DM
894 /* Do nothing */
895 break;
109a640b
KR
896
897 case 4:
898#ifdef BFD64
899 if (relocation)
900 {
901 bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + addr);
902 DOIT (x);
903 bfd_put_64 (abfd, x, (bfd_byte *) data + addr);
904 }
905#else
906 abort ();
907#endif
908 break;
65cab589
DM
909 default:
910 return bfd_reloc_other;
911 }
985fca12
SC
912
913 return flag;
914}
c618de01 915
4c3721d5
ILT
916/* This relocation routine is used by some of the backend linkers.
917 They do not construct asymbol or arelent structures, so there is no
918 reason for them to use bfd_perform_relocation. Also,
919 bfd_perform_relocation is so hacked up it is easier to write a new
920 function than to try to deal with it.
921
922 This routine does a final relocation. It should not be used when
923 generating relocateable output.
924
925 FIXME: This routine ignores any special_function in the HOWTO,
926 since the existing special_function values have been written for
927 bfd_perform_relocation.
928
929 HOWTO is the reloc howto information.
930 INPUT_BFD is the BFD which the reloc applies to.
931 INPUT_SECTION is the section which the reloc applies to.
932 CONTENTS is the contents of the section.
933 ADDRESS is the address of the reloc within INPUT_SECTION.
934 VALUE is the value of the symbol the reloc refers to.
935 ADDEND is the addend of the reloc. */
936
937bfd_reloc_status_type
938_bfd_final_link_relocate (howto, input_bfd, input_section, contents, address,
939 value, addend)
940 const reloc_howto_type *howto;
941 bfd *input_bfd;
942 asection *input_section;
943 bfd_byte *contents;
944 bfd_vma address;
945 bfd_vma value;
946 bfd_vma addend;
947{
948 bfd_vma relocation;
c618de01 949
4c3721d5
ILT
950 /* Sanity check the address. */
951 if (address > input_section->_cooked_size)
952 return bfd_reloc_outofrange;
953
954 /* This function assumes that we are dealing with a basic relocation
955 against a symbol. We want to compute the value of the symbol to
956 relocate to. This is just VALUE, the value of the symbol, plus
957 ADDEND, any addend associated with the reloc. */
958 relocation = value + addend;
959
960 /* If the relocation is PC relative, we want to set RELOCATION to
961 the distance between the symbol (currently in RELOCATION) and the
962 location we are relocating. Some targets (e.g., i386-aout)
963 arrange for the contents of the section to be the negative of the
964 offset of the location within the section; for such targets
965 pcrel_offset is false. Other targets (e.g., m88kbcs or ELF)
966 simply leave the contents of the section as zero; for such
967 targets pcrel_offset is true. If pcrel_offset is false we do not
968 need to subtract out the offset of the location within the
969 section (which is just ADDRESS). */
970 if (howto->pc_relative)
971 {
972 relocation -= (input_section->output_section->vma
973 + input_section->output_offset);
974 if (howto->pcrel_offset)
975 relocation -= address;
976 }
977
978 return _bfd_relocate_contents (howto, input_bfd, relocation,
979 contents + address);
980}
981
982/* Relocate a given location using a given value and howto. */
983
984bfd_reloc_status_type
985_bfd_relocate_contents (howto, input_bfd, relocation, location)
986 const reloc_howto_type *howto;
987 bfd *input_bfd;
988 bfd_vma relocation;
989 bfd_byte *location;
990{
991 int size;
992 bfd_vma x;
993 boolean overflow;
994
995 /* If the size is negative, negate RELOCATION. This isn't very
996 general. */
997 if (howto->size < 0)
998 relocation = - relocation;
999
1000 /* Get the value we are going to relocate. */
1001 size = bfd_get_reloc_size (howto);
1002 switch (size)
1003 {
1004 default:
1005 case 0:
1006 abort ();
1007 case 1:
1008 x = bfd_get_8 (input_bfd, location);
1009 break;
1010 case 2:
1011 x = bfd_get_16 (input_bfd, location);
1012 break;
1013 case 4:
1014 x = bfd_get_32 (input_bfd, location);
1015 break;
1016 case 8:
1017#ifdef BFD64
1018 x = bfd_get_64 (input_bfd, location);
1019#else
1020 abort ();
1021#endif
1022 break;
1023 }
1024
1025 /* Check for overflow. FIXME: We may drop bits during the addition
1026 which we don't check for. We must either check at every single
1027 operation, which would be tedious, or we must do the computations
1028 in a type larger than bfd_vma, which would be inefficient. */
1029 overflow = false;
1030 if (howto->complain_on_overflow != complain_overflow_dont)
1031 {
1032 bfd_vma check;
1033 bfd_signed_vma signed_check;
1034 bfd_vma add;
1035
1036 if (howto->rightshift == 0)
1037 {
1038 check = relocation;
1039 signed_check = (bfd_signed_vma) relocation;
1040 }
1041 else
1042 {
1043 /* Drop unwanted bits from the value we are relocating to. */
1044 check = relocation >> howto->rightshift;
1045
1046 /* If this is a signed value, the rightshift just dropped
1047 leading 1 bits (assuming twos complement). */
1048 if ((bfd_signed_vma) relocation >= 0)
1049 signed_check = check;
1050 else
1051 signed_check = (check
1052 | ((bfd_vma) -1
1053 &~ ((bfd_vma) -1 >> howto->rightshift)));
1054 }
1055
1056 /* Add in the value from the object file, shifted down so that
1057 it is a straight number. */
1058 add = x & howto->src_mask;
1059 if (howto->bitpos == 0)
1060 {
1061 check += add;
1062 signed_check += add;
1063 }
1064 else
1065 {
1066 add >>= howto->bitpos;
1067 check += add;
1068 signed_check += (add
1069 | ((bfd_vma) -1
1070 &~ ((bfd_vma) -1 >> howto->bitpos)));
1071 }
1072
1073 switch (howto->complain_on_overflow)
1074 {
1075 case complain_overflow_signed:
1076 {
1077 /* Assumes two's complement. */
1078 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
1079 bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
1080
1081 if (signed_check > reloc_signed_max
1082 || signed_check < reloc_signed_min)
1083 overflow = true;
1084 }
1085 break;
1086 case complain_overflow_unsigned:
1087 {
1088 /* Assumes two's complement. This expression avoids
1089 overflow if howto->bitsize is the number of bits in
1090 bfd_vma. */
1091 bfd_vma reloc_unsigned_max =
1092 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1093
1094 if (check > reloc_unsigned_max)
1095 overflow = true;
1096 }
1097 break;
1098 case complain_overflow_bitfield:
1099 {
1100 /* Assumes two's complement. This expression avoids
1101 overflow if howto->bitsize is the number of bits in
1102 bfd_vma. */
1103 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
1104
1105 if ((check &~ reloc_bits) != 0
1106 && (((bfd_vma) signed_check &~ reloc_bits)
1107 != (-1 &~ reloc_bits)))
1108 overflow = true;
1109 }
1110 break;
1111 default:
1112 abort ();
1113 }
1114 }
1115
1116 /* Put RELOCATION in the right bits. */
1117 relocation >>= (bfd_vma) howto->rightshift;
1118 relocation <<= (bfd_vma) howto->bitpos;
1119
1120 /* Add RELOCATION to the right bits of X. */
1121 x = ((x &~ howto->dst_mask)
1122 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
1123
1124 /* Put the relocated value back in the object file. */
1125 switch (size)
1126 {
1127 default:
1128 case 0:
1129 abort ();
1130 case 1:
1131 bfd_put_8 (input_bfd, x, location);
1132 break;
1133 case 2:
1134 bfd_put_16 (input_bfd, x, location);
1135 break;
1136 case 4:
1137 bfd_put_32 (input_bfd, x, location);
1138 break;
1139 case 8:
1140#ifdef BFD64
1141 bfd_put_64 (input_bfd, x, location);
1142#else
1143 abort ();
1144#endif
1145 break;
1146 }
1147
1148 return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
1149}
2cf44d7b 1150
0cda46cf 1151/*
c26d7d17 1152DOCDD
e98e6ec1
SC
1153INODE
1154 howto manager, , typedef arelent, Relocations
1155
0cda46cf
SC
1156SECTION
1157 The howto manager
2cf44d7b 1158
0cda46cf
SC
1159 When an application wants to create a relocation, but doesn't
1160 know what the target machine might call it, it can find out by
1161 using this bit of code.
2cf44d7b 1162
0cda46cf 1163*/
2cf44d7b 1164
0cda46cf
SC
1165/*
1166TYPEDEF
1167 bfd_reloc_code_type
2cf44d7b 1168
0cda46cf 1169DESCRIPTION
fb32909a
KR
1170 The insides of a reloc code. The idea is that, eventually, there
1171 will be one enumerator for every type of relocation we ever do.
1172 Pass one of these values to <<bfd_reloc_type_lookup>>, and it'll
1173 return a howto pointer.
1174
1175 This does mean that the application must determine the correct
1176 enumerator value; you can't get a howto pointer from a random set
1177 of attributes.
0cda46cf 1178
e98e6ec1
SC
1179CODE_FRAGMENT
1180.
1181.typedef enum bfd_reloc_code_real
1182.{
fb32909a 1183. {* Basic absolute relocations *}
defcfb55 1184. BFD_RELOC_64,
defcfb55 1185. BFD_RELOC_32,
e98e6ec1 1186. BFD_RELOC_16,
0cda46cf 1187. BFD_RELOC_8,
fb32909a
KR
1188.
1189. {* PC-relative relocations *}
1190. BFD_RELOC_64_PCREL,
1191. BFD_RELOC_32_PCREL,
1192. BFD_RELOC_24_PCREL, {* used by i960 *}
1193. BFD_RELOC_16_PCREL,
0cda46cf 1194. BFD_RELOC_8_PCREL,
e98e6ec1 1195.
fb32909a
KR
1196. {* Linkage-table relative *}
1197. BFD_RELOC_32_BASEREL,
1198. BFD_RELOC_16_BASEREL,
1199. BFD_RELOC_8_BASEREL,
e98e6ec1 1200.
fb32909a
KR
1201. {* The type of reloc used to build a contructor table - at the moment
1202. probably a 32 bit wide abs address, but the cpu can choose. *}
8070f29d
KR
1203. BFD_RELOC_CTOR,
1204.
fb32909a
KR
1205. {* 8 bits wide, but used to form an address like 0xffnn *}
1206. BFD_RELOC_8_FFnn,
1207.
1208. {* 32-bit pc-relative, shifted right 2 bits (i.e., 30-bit
1209. word displacement, e.g. for SPARC) *}
1210. BFD_RELOC_32_PCREL_S2,
1211.
1212. {* High 22 bits of 32-bit value, placed into lower 22 bits of
1213. target word; simple reloc. *}
8070f29d 1214. BFD_RELOC_HI22,
fb32909a 1215. {* Low 10 bits. *}
8070f29d
KR
1216. BFD_RELOC_LO10,
1217.
fb32909a 1218. {* Reloc types used for i960/b.out. *}
8070f29d
KR
1219. BFD_RELOC_I960_CALLJ,
1220.
8070f29d
KR
1221. {* now for the sparc/elf codes *}
1222. BFD_RELOC_NONE, {* actually used *}
1223. BFD_RELOC_SPARC_WDISP22,
1224. BFD_RELOC_SPARC22,
1225. BFD_RELOC_SPARC13,
8070f29d
KR
1226. BFD_RELOC_SPARC_GOT10,
1227. BFD_RELOC_SPARC_GOT13,
1228. BFD_RELOC_SPARC_GOT22,
1229. BFD_RELOC_SPARC_PC10,
1230. BFD_RELOC_SPARC_PC22,
1231. BFD_RELOC_SPARC_WPLT30,
1232. BFD_RELOC_SPARC_COPY,
1233. BFD_RELOC_SPARC_GLOB_DAT,
1234. BFD_RELOC_SPARC_JMP_SLOT,
1235. BFD_RELOC_SPARC_RELATIVE,
1236. BFD_RELOC_SPARC_UA32,
1237.
fb32909a 1238. {* these are a.out specific? *}
58acdbd7 1239. BFD_RELOC_SPARC_BASE13,
8070f29d
KR
1240. BFD_RELOC_SPARC_BASE22,
1241.
defcfb55 1242. {* start-sanitize-v9 *}
defcfb55
KR
1243. BFD_RELOC_SPARC_10,
1244. BFD_RELOC_SPARC_11,
1245.#define BFD_RELOC_SPARC_64 BFD_RELOC_64
1246. BFD_RELOC_SPARC_OLO10,
1247. BFD_RELOC_SPARC_HH22,
1248. BFD_RELOC_SPARC_HM10,
1249. BFD_RELOC_SPARC_LM22,
1250. BFD_RELOC_SPARC_PC_HH22,
1251. BFD_RELOC_SPARC_PC_HM10,
1252. BFD_RELOC_SPARC_PC_LM22,
1253. BFD_RELOC_SPARC_WDISP16,
58acdbd7 1254. BFD_RELOC_SPARC_WDISP19,
defcfb55
KR
1255. BFD_RELOC_SPARC_GLOB_JMP,
1256. BFD_RELOC_SPARC_LO7,
1257. {* end-sanitize-v9 *}
58acdbd7 1258.
fb32909a
KR
1259. {* Bits 27..2 of the relocation address shifted right 2 bits;
1260. simple reloc otherwise. *}
65cab589
DM
1261. BFD_RELOC_MIPS_JMP,
1262.
fb32909a 1263. {* signed 16-bit pc-relative, shifted right 2 bits (e.g. for MIPS) *}
65cab589
DM
1264. BFD_RELOC_16_PCREL_S2,
1265.
fb32909a 1266. {* High 16 bits of 32-bit value; simple reloc. *}
65cab589 1267. BFD_RELOC_HI16,
fb32909a
KR
1268. {* High 16 bits of 32-bit value but the low 16 bits will be sign
1269. extended and added to form the final result. If the low 16
1270. bits form a negative number, we need to add one to the high value
1271. to compensate for the borrow when the low bits are added. *}
65cab589 1272. BFD_RELOC_HI16_S,
fb32909a 1273. {* Low 16 bits. *}
65cab589
DM
1274. BFD_RELOC_LO16,
1275.
fb32909a 1276. {* 16 bit relocation relative to the global pointer. *}
65cab589
DM
1277. BFD_RELOC_MIPS_GPREL,
1278.
c188b0be
DM
1279. {* Relocation against a MIPS literal section. *}
1280. BFD_RELOC_MIPS_LITERAL,
1281.
1282. {* MIPS ELF relocations. *}
1283. BFD_RELOC_MIPS_GOT16,
1284. BFD_RELOC_MIPS_CALL16,
1285. BFD_RELOC_MIPS_GPREL32,
1286.
fb32909a
KR
1287. {* These are, so far, specific to HPPA processors. I'm not sure that some
1288. don't duplicate other reloc types, such as BFD_RELOC_32 and _32_PCREL.
1289. Also, many more were in the list I got that don't fit in well in the
1290. model BFD uses, so I've omitted them for now. If we do make this reloc
1291. type get used for code that really does implement the funky reloc types,
1292. they'll have to be added to this list. *}
65cab589
DM
1293. BFD_RELOC_HPPA_32,
1294. BFD_RELOC_HPPA_11,
1295. BFD_RELOC_HPPA_14,
1296. BFD_RELOC_HPPA_17,
fb32909a 1297.
65cab589
DM
1298. BFD_RELOC_HPPA_L21,
1299. BFD_RELOC_HPPA_R11,
1300. BFD_RELOC_HPPA_R14,
1301. BFD_RELOC_HPPA_R17,
1302. BFD_RELOC_HPPA_LS21,
1303. BFD_RELOC_HPPA_RS11,
1304. BFD_RELOC_HPPA_RS14,
1305. BFD_RELOC_HPPA_RS17,
1306. BFD_RELOC_HPPA_LD21,
1307. BFD_RELOC_HPPA_RD11,
1308. BFD_RELOC_HPPA_RD14,
1309. BFD_RELOC_HPPA_RD17,
1310. BFD_RELOC_HPPA_LR21,
1311. BFD_RELOC_HPPA_RR14,
1312. BFD_RELOC_HPPA_RR17,
fb32909a 1313.
65cab589
DM
1314. BFD_RELOC_HPPA_GOTOFF_11,
1315. BFD_RELOC_HPPA_GOTOFF_14,
1316. BFD_RELOC_HPPA_GOTOFF_L21,
1317. BFD_RELOC_HPPA_GOTOFF_R11,
1318. BFD_RELOC_HPPA_GOTOFF_R14,
1319. BFD_RELOC_HPPA_GOTOFF_LS21,
1320. BFD_RELOC_HPPA_GOTOFF_RS11,
1321. BFD_RELOC_HPPA_GOTOFF_RS14,
1322. BFD_RELOC_HPPA_GOTOFF_LD21,
1323. BFD_RELOC_HPPA_GOTOFF_RD11,
1324. BFD_RELOC_HPPA_GOTOFF_RD14,
1325. BFD_RELOC_HPPA_GOTOFF_LR21,
1326. BFD_RELOC_HPPA_GOTOFF_RR14,
fb32909a 1327.
65cab589
DM
1328. BFD_RELOC_HPPA_DLT_32,
1329. BFD_RELOC_HPPA_DLT_11,
1330. BFD_RELOC_HPPA_DLT_14,
1331. BFD_RELOC_HPPA_DLT_L21,
1332. BFD_RELOC_HPPA_DLT_R11,
1333. BFD_RELOC_HPPA_DLT_R14,
fb32909a 1334.
65cab589
DM
1335. BFD_RELOC_HPPA_ABS_CALL_11,
1336. BFD_RELOC_HPPA_ABS_CALL_14,
1337. BFD_RELOC_HPPA_ABS_CALL_17,
1338. BFD_RELOC_HPPA_ABS_CALL_L21,
1339. BFD_RELOC_HPPA_ABS_CALL_R11,
1340. BFD_RELOC_HPPA_ABS_CALL_R14,
1341. BFD_RELOC_HPPA_ABS_CALL_R17,
1342. BFD_RELOC_HPPA_ABS_CALL_LS21,
1343. BFD_RELOC_HPPA_ABS_CALL_RS11,
1344. BFD_RELOC_HPPA_ABS_CALL_RS14,
1345. BFD_RELOC_HPPA_ABS_CALL_RS17,
1346. BFD_RELOC_HPPA_ABS_CALL_LD21,
1347. BFD_RELOC_HPPA_ABS_CALL_RD11,
1348. BFD_RELOC_HPPA_ABS_CALL_RD14,
1349. BFD_RELOC_HPPA_ABS_CALL_RD17,
1350. BFD_RELOC_HPPA_ABS_CALL_LR21,
1351. BFD_RELOC_HPPA_ABS_CALL_RR14,
1352. BFD_RELOC_HPPA_ABS_CALL_RR17,
fb32909a 1353.
65cab589
DM
1354. BFD_RELOC_HPPA_PCREL_CALL_11,
1355. BFD_RELOC_HPPA_PCREL_CALL_12,
1356. BFD_RELOC_HPPA_PCREL_CALL_14,
1357. BFD_RELOC_HPPA_PCREL_CALL_17,
1358. BFD_RELOC_HPPA_PCREL_CALL_L21,
1359. BFD_RELOC_HPPA_PCREL_CALL_R11,
1360. BFD_RELOC_HPPA_PCREL_CALL_R14,
1361. BFD_RELOC_HPPA_PCREL_CALL_R17,
1362. BFD_RELOC_HPPA_PCREL_CALL_LS21,
1363. BFD_RELOC_HPPA_PCREL_CALL_RS11,
1364. BFD_RELOC_HPPA_PCREL_CALL_RS14,
1365. BFD_RELOC_HPPA_PCREL_CALL_RS17,
1366. BFD_RELOC_HPPA_PCREL_CALL_LD21,
1367. BFD_RELOC_HPPA_PCREL_CALL_RD11,
1368. BFD_RELOC_HPPA_PCREL_CALL_RD14,
1369. BFD_RELOC_HPPA_PCREL_CALL_RD17,
1370. BFD_RELOC_HPPA_PCREL_CALL_LR21,
1371. BFD_RELOC_HPPA_PCREL_CALL_RR14,
1372. BFD_RELOC_HPPA_PCREL_CALL_RR17,
fb32909a 1373.
65cab589
DM
1374. BFD_RELOC_HPPA_PLABEL_32,
1375. BFD_RELOC_HPPA_PLABEL_11,
1376. BFD_RELOC_HPPA_PLABEL_14,
1377. BFD_RELOC_HPPA_PLABEL_L21,
1378. BFD_RELOC_HPPA_PLABEL_R11,
1379. BFD_RELOC_HPPA_PLABEL_R14,
fb32909a 1380.
65cab589
DM
1381. BFD_RELOC_HPPA_UNWIND_ENTRY,
1382. BFD_RELOC_HPPA_UNWIND_ENTRIES,
1383.
a49880c8
KR
1384. {* i386/elf relocations *}
1385. BFD_RELOC_386_GOT32,
1386. BFD_RELOC_386_PLT32,
1387. BFD_RELOC_386_COPY,
1388. BFD_RELOC_386_GLOB_DAT,
1389. BFD_RELOC_386_JUMP_SLOT,
1390. BFD_RELOC_386_RELATIVE,
1391. BFD_RELOC_386_GOTOFF,
1392. BFD_RELOC_386_GOTPC,
1393.
8070f29d
KR
1394. {* this must be the highest numeric value *}
1395. BFD_RELOC_UNUSED
0cda46cf 1396. } bfd_reloc_code_real_type;
2cf44d7b
SC
1397*/
1398
1399
0cda46cf 1400/*
c188b0be 1401FUNCTION
0cda46cf 1402 bfd_reloc_type_lookup
2cf44d7b 1403
e98e6ec1 1404SYNOPSIS
4c3721d5 1405 const struct reloc_howto_struct *
3860075f 1406 bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
e98e6ec1 1407
0cda46cf 1408DESCRIPTION
4c3721d5 1409 Return a pointer to a howto structure which, when
c188b0be 1410 invoked, will perform the relocation @var{code} on data from the
0cda46cf 1411 architecture noted.
2cf44d7b 1412
2cf44d7b
SC
1413*/
1414
1415
4c3721d5 1416const struct reloc_howto_struct *
3860075f 1417DEFUN(bfd_reloc_type_lookup,(abfd, code),
8070f29d 1418 bfd *abfd AND
3860075f 1419 bfd_reloc_code_real_type code)
2cf44d7b 1420{
8070f29d 1421 return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
2cf44d7b
SC
1422}
1423
0cda46cf 1424static reloc_howto_type bfd_howto_32 =
66a277ab 1425 HOWTO(0, 00,2,32,false,0,complain_overflow_bitfield,0,"VRT32", false,0xffffffff,0xffffffff,true);
2cf44d7b
SC
1426
1427
0cda46cf 1428/*
e98e6ec1 1429INTERNAL_FUNCTION
0cda46cf
SC
1430 bfd_default_reloc_type_lookup
1431
0cda46cf 1432SYNOPSIS
4c3721d5 1433 const struct reloc_howto_struct *bfd_default_reloc_type_lookup
65cab589 1434 (bfd *abfd AND
3860075f 1435 bfd_reloc_code_real_type code);
0cda46cf 1436
e98e6ec1 1437DESCRIPTION
65cab589 1438 Provides a default relocation lookup routine for any architecture.
e98e6ec1
SC
1439
1440
0cda46cf 1441*/
65cab589 1442
4c3721d5 1443const struct reloc_howto_struct *
65cab589
DM
1444DEFUN(bfd_default_reloc_type_lookup, (abfd, code),
1445 bfd *abfd AND
1446 bfd_reloc_code_real_type code)
0cda46cf 1447{
65cab589 1448 switch (code)
0cda46cf 1449 {
65cab589
DM
1450 case BFD_RELOC_CTOR:
1451 /* The type of reloc used in a ctor, which will be as wide as the
fb32909a 1452 address - so either a 64, 32, or 16 bitter. */
65cab589
DM
1453 switch (bfd_get_arch_info (abfd)->bits_per_address) {
1454 case 64:
1455 BFD_FAIL();
1456 case 32:
1457 return &bfd_howto_32;
1458 case 16:
1459 BFD_FAIL();
1460 default:
0cda46cf 1461 BFD_FAIL();
65cab589
DM
1462 }
1463 default:
1464 BFD_FAIL();
0cda46cf 1465 }
4c3721d5 1466 return (const struct reloc_howto_struct *)NULL;
0cda46cf 1467}
e98e6ec1
SC
1468
1469
d58b7049
SC
1470/*
1471INTERNAL_FUNCTION
1472 bfd_generic_relax_section
1473
1474SYNOPSIS
1475 boolean bfd_generic_relax_section
1476 (bfd *abfd,
1477 asection *section,
4c3721d5 1478 struct bfd_link_info *,
5022aea5 1479 asymbol **symbols);
d58b7049
SC
1480
1481DESCRIPTION
1482 Provides default handling for relaxing for back ends which
8070f29d 1483 don't do relaxing -- i.e., does nothing.
d58b7049
SC
1484*/
1485
1486boolean
4c3721d5
ILT
1487bfd_generic_relax_section (abfd, section, link_info, symbols)
1488 bfd *abfd;
1489 asection *section;
1490 struct bfd_link_info *link_info;
1491 asymbol **symbols;
d58b7049 1492{
d58b7049 1493 return false;
d58b7049 1494}
d58b7049 1495
e98e6ec1
SC
1496/*
1497INTERNAL_FUNCTION
1498 bfd_generic_get_relocated_section_contents
1499
1500SYNOPSIS
1501 bfd_byte *
65cab589 1502 bfd_generic_get_relocated_section_contents (bfd *abfd,
4c3721d5
ILT
1503 struct bfd_link_info *link_info,
1504 struct bfd_link_order *link_order,
65cab589 1505 bfd_byte *data,
4c3721d5
ILT
1506 boolean relocateable,
1507 asymbol **symbols);
e98e6ec1
SC
1508
1509DESCRIPTION
1510 Provides default handling of relocation effort for back ends
1511 which can't be bothered to do it efficiently.
1512
1513*/
1514
1515bfd_byte *
4c3721d5
ILT
1516bfd_generic_get_relocated_section_contents (abfd, link_info, link_order, data,
1517 relocateable, symbols)
1518 bfd *abfd;
1519 struct bfd_link_info *link_info;
1520 struct bfd_link_order *link_order;
1521 bfd_byte *data;
1522 boolean relocateable;
1523 asymbol **symbols;
e98e6ec1 1524{
e98e6ec1 1525 /* Get enough memory to hold the stuff */
4c3721d5
ILT
1526 bfd *input_bfd = link_order->u.indirect.section->owner;
1527 asection *input_section = link_order->u.indirect.section;
e98e6ec1 1528
8070f29d 1529
e98e6ec1 1530
defcfb55 1531 size_t reloc_size = bfd_get_reloc_upper_bound(input_bfd, input_section);
8070f29d 1532 arelent **reloc_vector = (arelent **) alloca(reloc_size);
e98e6ec1
SC
1533
1534 /* read in the section */
1535 bfd_get_section_contents(input_bfd,
1536 input_section,
1537 data,
1538 0,
1539 input_section->_raw_size);
1540
1541/* We're not relaxing the section, so just copy the size info */
1542 input_section->_cooked_size = input_section->_raw_size;
1543 input_section->reloc_done = true;
1544
1545
1546 if (bfd_canonicalize_reloc(input_bfd,
1547 input_section,
1548 reloc_vector,
4c3721d5 1549 symbols) )
e98e6ec1
SC
1550 {
1551 arelent **parent;
1552 for (parent = reloc_vector; * parent != (arelent *)NULL;
1553 parent++)
1554 {
4c3721d5 1555 char *error_message = (char *) NULL;
e98e6ec1
SC
1556 bfd_reloc_status_type r=
1557 bfd_perform_relocation(input_bfd,
1558 *parent,
1559 data,
65cab589 1560 input_section,
4c3721d5
ILT
1561 relocateable ? abfd : (bfd *) NULL,
1562 &error_message);
e98e6ec1 1563
65cab589
DM
1564 if (relocateable)
1565 {
1566 asection *os = input_section->output_section;
1567
1568 /* A partial link, so keep the relocs */
1569 os->orelocation[os->reloc_count] = *parent;
1570 os->reloc_count++;
1571 }
e98e6ec1
SC
1572
1573 if (r != bfd_reloc_ok)
1574 {
e98e6ec1
SC
1575 switch (r)
1576 {
1577 case bfd_reloc_undefined:
4c3721d5
ILT
1578 if (! ((*link_info->callbacks->undefined_symbol)
1579 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
1580 input_bfd, input_section, (*parent)->address)))
1581 return NULL;
e98e6ec1
SC
1582 break;
1583 case bfd_reloc_dangerous:
4c3721d5
ILT
1584 BFD_ASSERT (error_message != (char *) NULL);
1585 if (! ((*link_info->callbacks->reloc_dangerous)
1586 (link_info, error_message, input_bfd, input_section,
1587 (*parent)->address)))
1588 return NULL;
e98e6ec1 1589 break;
e98e6ec1 1590 case bfd_reloc_overflow:
4c3721d5
ILT
1591 if (! ((*link_info->callbacks->reloc_overflow)
1592 (link_info, input_bfd, input_section, (*parent)->address)))
1593 return NULL;
e98e6ec1 1594 break;
4c3721d5 1595 case bfd_reloc_outofrange:
e98e6ec1
SC
1596 default:
1597 abort();
1598 break;
1599 }
1600
1601 }
1602 }
1603 }
1604
8070f29d 1605
e98e6ec1
SC
1606 return data;
1607
1608
1609}
This page took 0.173456 seconds and 4 git commands to generate.