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