fix mistakes in last change
[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"
985fca12 46#include "libbfd.h"
e98e6ec1 47#include "seclet.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
85. symbols. *}
0cda46cf 86. bfd_reloc_dangerous
e98e6ec1 87. }
0cda46cf 88. bfd_reloc_status_type;
e98e6ec1
SC
89.
90.
0cda46cf
SC
91.typedef struct reloc_cache_entry
92.{
e98e6ec1
SC
93. {* A pointer into the canonical table of pointers *}
94. struct symbol_cache_entry **sym_ptr_ptr;
95.
96. {* offset in section *}
65cab589 97. bfd_size_type address;
e98e6ec1
SC
98.
99. {* addend for relocation value *}
100. bfd_vma addend;
101.
102. {* Pointer to how to perform the required relocation *}
103. CONST struct reloc_howto_struct *howto;
104.
105.} arelent;
985fca12 106
e98e6ec1 107*/
985fca12 108
e98e6ec1
SC
109/*
110DESCRIPTION
985fca12 111
c188b0be 112 Here is a description of each of the fields within an <<arelent>>:
985fca12 113
c188b0be 114 o <<sym_ptr_ptr>>
985fca12 115
e98e6ec1 116 The symbol table pointer points to a pointer to the symbol
c188b0be
DM
117 associated with the relocation request. It is
118 the pointer into the table returned by the back end's
119 <<get_symtab>> action. @xref{Symbols}. The symbol is referenced
e98e6ec1
SC
120 through a pointer to a pointer so that tools like the linker
121 can fix up all the symbols of the same name by modifying only
122 one pointer. The relocation routine looks in the symbol and
123 uses the base of the section the symbol is attached to and the
124 value of the symbol as the initial relocation offset. If the
125 symbol pointer is zero, then the section provided is looked up.
985fca12 126
c188b0be 127 o <<address>>
985fca12 128
c188b0be 129 The <<address>> field gives the offset in bytes from the base of
e98e6ec1
SC
130 the section data which owns the relocation record to the first
131 byte of relocatable information. The actual data relocated
c188b0be 132 will be relative to this point; for example, a relocation
e98e6ec1
SC
133 type which modifies the bottom two bytes of a four byte word
134 would not touch the first byte pointed to in a big endian
c26d7d17
SC
135 world.
136
c188b0be 137 o <<addend>>
c26d7d17 138
c188b0be 139 The <<addend>> is a value provided by the back end to be added (!)
c26d7d17
SC
140 to the relocation offset. Its interpretation is dependent upon
141 the howto. For example, on the 68k the code:
985fca12 142
985fca12 143
e98e6ec1
SC
144| char foo[];
145| main()
146| {
147| return foo[0x12345678];
148| }
985fca12 149
e98e6ec1 150 Could be compiled into:
985fca12 151
e98e6ec1
SC
152| linkw fp,#-4
153| moveb @@#12345678,d0
154| extbl d0
155| unlk fp
156| rts
985fca12 157
985fca12 158
c188b0be
DM
159 This could create a reloc pointing to <<foo>>, but leave the
160 offset in the data, something like:
0cda46cf 161
985fca12 162
e98e6ec1
SC
163|RELOCATION RECORDS FOR [.text]:
164|offset type value
165|00000006 32 _foo
166|
167|00000000 4e56 fffc ; linkw fp,#-4
168|00000004 1039 1234 5678 ; moveb @@#12345678,d0
169|0000000a 49c0 ; extbl d0
170|0000000c 4e5e ; unlk fp
171|0000000e 4e75 ; rts
0cda46cf 172
985fca12 173
e98e6ec1
SC
174 Using coff and an 88k, some instructions don't have enough
175 space in them to represent the full address range, and
176 pointers have to be loaded in two parts. So you'd get something like:
0cda46cf 177
985fca12 178
e98e6ec1
SC
179| or.u r13,r0,hi16(_foo+0x12345678)
180| ld.b r2,r13,lo16(_foo+0x12345678)
181| jmp r1
985fca12 182
985fca12 183
c188b0be 184 This should create two relocs, both pointing to <<_foo>>, and with
e98e6ec1 185 0x12340000 in their addend field. The data would consist of:
0cda46cf 186
985fca12 187
e98e6ec1
SC
188|RELOCATION RECORDS FOR [.text]:
189|offset type value
190|00000002 HVRT16 _foo+0x12340000
191|00000006 LVRT16 _foo+0x12340000
985fca12 192
e98e6ec1
SC
193|00000000 5da05678 ; or.u r13,r0,0x5678
194|00000004 1c4d5678 ; ld.b r2,r13,0x5678
195|00000008 f400c001 ; jmp r1
985fca12 196
0cda46cf 197
e98e6ec1 198 The relocation routine digs out the value from the data, adds
c188b0be
DM
199 it to the addend to get the original offset, and then adds the
200 value of <<_foo>>. Note that all 32 bits have to be kept around
e98e6ec1 201 somewhere, to cope with carry from bit 15 to bit 16.
985fca12 202
65cab589 203 One further example is the sparc and the a.out format. The
e98e6ec1
SC
204 sparc has a similar problem to the 88k, in that some
205 instructions don't have room for an entire offset, but on the
c188b0be
DM
206 sparc the parts are created in odd sized lumps. The designers of
207 the a.out format chose to not use the data within the section
e98e6ec1 208 for storing part of the offset; all the offset is kept within
c188b0be 209 the reloc. Anything in the data should be ignored.
0cda46cf 210
e98e6ec1
SC
211| save %sp,-112,%sp
212| sethi %hi(_foo+0x12345678),%g2
213| ldsb [%g2+%lo(_foo+0x12345678)],%i0
214| ret
215| restore
0cda46cf 216
c188b0be 217 Both relocs contains a pointer to <<foo>>, and the offsets
e98e6ec1 218 contain junk.
985fca12 219
0cda46cf 220
e98e6ec1
SC
221|RELOCATION RECORDS FOR [.text]:
222|offset type value
223|00000004 HI22 _foo+0x12345678
224|00000008 LO10 _foo+0x12345678
225
226|00000000 9de3bf90 ; save %sp,-112,%sp
227|00000004 05000000 ; sethi %hi(_foo+0),%g2
228|00000008 f048a000 ; ldsb [%g2+%lo(_foo+0)],%i0
229|0000000c 81c7e008 ; ret
230|00000010 81e80000 ; restore
231
0cda46cf 232
c188b0be 233 o <<howto>>
e98e6ec1 234
c188b0be
DM
235 The <<howto>> field can be imagined as a
236 relocation instruction. It is a pointer to a structure which
237 contains information on what to do with all of the other
e98e6ec1
SC
238 information in the reloc record and data section. A back end
239 would normally have a relocation instruction set and turn
240 relocations into pointers to the correct structure on input -
241 but it would be possible to create each howto field on demand.
242
985fca12
SC
243*/
244
66a277ab
ILT
245/*
246SUBSUBSECTION
247 <<enum complain_overflow>>
248
249 Indicates what sort of overflow checking should be done when
250 performing a relocation.
251
252CODE_FRAGMENT
253.
254.enum complain_overflow
255.{
256. {* Do not complain on overflow. *}
257. complain_overflow_dont,
258.
259. {* Complain if the bitfield overflows, whether it is considered
260. as signed or unsigned. *}
261. complain_overflow_bitfield,
262.
263. {* Complain if the value overflows when considered as signed
264. number. *}
265. complain_overflow_signed,
266.
267. {* Complain if the value overflows when considered as an
268. unsigned number. *}
269. complain_overflow_unsigned
270.};
271
272*/
985fca12 273
0cda46cf
SC
274/*
275SUBSUBSECTION
e98e6ec1 276 <<reloc_howto_type>>
985fca12 277
e98e6ec1 278 The <<reloc_howto_type>> is a structure which contains all the
c188b0be 279 information that libbfd needs to know to tie up a back end's data.
985fca12 280
e98e6ec1 281CODE_FRAGMENT
5022aea5 282.struct symbol_cache_entry; {* Forward declaration *}
e98e6ec1 283.
c188b0be 284.typedef struct reloc_howto_struct
0cda46cf 285.{
e98e6ec1 286. {* The type field has mainly a documetary use - the back end can
c188b0be
DM
287. do what it wants with it, though normally the back end's
288. external idea of what a reloc number is stored
289. in this field. For example, a PC relative word relocation
290. in a coff environment has the type 023 - because that's
e98e6ec1 291. what the outside world calls a R_PCRWORD reloc. *}
0cda46cf 292. unsigned int type;
e98e6ec1
SC
293.
294. {* The value the final relocation is shifted right by. This drops
295. unwanted data from the relocation. *}
0cda46cf 296. unsigned int rightshift;
e98e6ec1 297.
fb32909a
KR
298. {* The size of the item to be relocated. This is *not* a
299. power-of-two measure.
300. 0 : one byte
301. 1 : two bytes
302. 2 : four bytes
303. 3 : nothing done (unless special_function is nonzero)
304. 4 : eight bytes
305. -2 : two bytes, result should be subtracted from the
306. data instead of added
307. There is currently no trivial way to extract a "number of
308. bytes" from a howto pointer. *}
c26d7d17 309. int size;
e98e6ec1 310.
66a277ab
ILT
311. {* The number of bits in the item to be relocated. This is used
312. when doing overflow checking. *}
0cda46cf 313. unsigned int bitsize;
e98e6ec1
SC
314.
315. {* Notes that the relocation is relative to the location in the
316. data section of the addend. The relocation function will
317. subtract from the relocation value the address of the location
318. being relocated. *}
0cda46cf 319. boolean pc_relative;
e98e6ec1 320.
66a277ab
ILT
321. {* The bit position of the reloc value in the destination.
322. The relocated value is left shifted by this amount. *}
0cda46cf 323. unsigned int bitpos;
e98e6ec1 324.
66a277ab
ILT
325. {* What type of overflow error should be checked for when
326. relocating. *}
327. enum complain_overflow complain_on_overflow;
e98e6ec1
SC
328.
329. {* If this field is non null, then the supplied function is
330. called rather than the normal function. This allows really
65cab589 331. strange relocation methods to be accomodated (e.g., i960 callj
e98e6ec1 332. instructions). *}
fefb4b30
JG
333. bfd_reloc_status_type (*special_function)
334. PARAMS ((bfd *abfd,
5022aea5
SC
335. arelent *reloc_entry,
336. struct symbol_cache_entry *symbol,
337. PTR data,
8070f29d 338. asection *input_section,
fefb4b30 339. bfd *output_bfd));
e98e6ec1
SC
340.
341. {* The textual name of the relocation type. *}
0cda46cf 342. char *name;
e98e6ec1
SC
343.
344. {* When performing a partial link, some formats must modify the
345. relocations rather than the data - this flag signals this.*}
0cda46cf 346. boolean partial_inplace;
e98e6ec1 347.
c188b0be 348. {* The src_mask selects which parts of the read in data
65cab589 349. are to be used in the relocation sum. E.g., if this was an 8 bit
e98e6ec1
SC
350. bit of data which we read and relocated, this would be
351. 0x000000ff. When we have relocs which have an addend, such as
352. sun4 extended relocs, the value in the offset part of a
353. relocating field is garbage so we never use it. In this case
354. the mask would be 0x00000000. *}
65cab589 355. bfd_vma src_mask;
e98e6ec1 356.
c188b0be 357. {* The dst_mask selects which parts of the instruction are replaced
e98e6ec1
SC
358. into the instruction. In most cases src_mask == dst_mask,
359. except in the above special case, where dst_mask would be
360. 0x000000ff, and src_mask would be 0x00000000. *}
65cab589 361. bfd_vma dst_mask;
e98e6ec1
SC
362.
363. {* When some formats create PC relative instructions, they leave
364. the value of the pc of the place being relocated in the offset
365. slot of the instruction, so that a PC relative relocation can
65cab589 366. be made just by adding in an ordinary offset (e.g., sun3 a.out).
e98e6ec1 367. Some formats leave the displacement part of an instruction
c188b0be 368. empty (e.g., m88k bcs); this flag signals the fact.*}
0cda46cf 369. boolean pcrel_offset;
e98e6ec1 370.
0cda46cf 371.} reloc_howto_type;
985fca12 372
0cda46cf 373*/
985fca12 374
0cda46cf
SC
375/*
376FUNCTION
c188b0be 377 The HOWTO Macro
e98e6ec1 378
0cda46cf
SC
379DESCRIPTION
380 The HOWTO define is horrible and will go away.
381
382
66a277ab
ILT
383.#define HOWTO(C, R,S,B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
384. {(unsigned)C,R,S,B, P, BI, O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
0cda46cf
SC
385
386DESCRIPTION
387 And will be replaced with the totally magic way. But for the
c188b0be 388 moment, we are compatible, so do it this way.
0cda46cf
SC
389
390
66a277ab 391.#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
392.
393DESCRIPTION
394 Helper routine to turn a symbol into a relocation value.
395
e98e6ec1
SC
396.#define HOWTO_PREPARE(relocation, symbol) \
397. { \
398. if (symbol != (asymbol *)NULL) { \
65cab589 399. if (bfd_is_com_section (symbol->section)) { \
e98e6ec1
SC
400. relocation = 0; \
401. } \
402. else { \
403. relocation = symbol->value; \
404. } \
405. } \
406.}
985fca12
SC
407
408*/
409
0cda46cf
SC
410/*
411TYPEDEF
c188b0be 412 arelent_chain
985fca12 413
0cda46cf 414DESCRIPTION
985fca12 415
c188b0be 416 How relocs are tied together in an <<asection>>:
985fca12 417
0cda46cf
SC
418.typedef unsigned char bfd_byte;
419.
420.typedef struct relent_chain {
421. arelent relent;
422. struct relent_chain *next;
423.} arelent_chain;
985fca12
SC
424
425*/
426
427
428
0cda46cf
SC
429/*
430FUNCTION
431 bfd_perform_relocation
432
e98e6ec1
SC
433SYNOPSIS
434 bfd_reloc_status_type
435 bfd_perform_relocation
c188b0be 436 (bfd *abfd,
e98e6ec1
SC
437 arelent *reloc_entry,
438 PTR data,
439 asection *input_section,
440 bfd *output_bfd);
441
0cda46cf 442DESCRIPTION
c188b0be
DM
443 If @var{output_bfd} is supplied to this function, the generated
444 image will be relocatable; the relocations are copied to the
0cda46cf
SC
445 output file after they have been changed to reflect the new
446 state of the world. There are two ways of reflecting the
c188b0be 447 results of partial linkage in an output file: by modifying the
0cda46cf 448 output data in place, and by modifying the relocation record.
65cab589 449 Some native formats (e.g., basic a.out and basic coff) have no
0cda46cf
SC
450 way of specifying an addend in the relocation type, so the
451 addend has to go in the output data. This is no big deal
452 since in these formats the output data slot will always be big
453 enough for the addend. Complex reloc types with addends were
454 invented to solve just this problem.
455
985fca12
SC
456*/
457
458
0cda46cf 459bfd_reloc_status_type
985fca12
SC
460DEFUN(bfd_perform_relocation,(abfd,
461 reloc_entry,
462 data,
463 input_section,
464 output_bfd),
465 bfd *abfd AND
466 arelent *reloc_entry AND
467 PTR data AND
468 asection *input_section AND
469 bfd *output_bfd)
470{
471 bfd_vma relocation;
0cda46cf 472 bfd_reloc_status_type flag = bfd_reloc_ok;
65cab589 473 bfd_size_type addr = reloc_entry->address ;
985fca12
SC
474 bfd_vma output_base = 0;
475 reloc_howto_type *howto = reloc_entry->howto;
e98e6ec1
SC
476 asection *reloc_target_output_section ;
477
985fca12
SC
478 asymbol *symbol;
479
e98e6ec1 480 symbol = *( reloc_entry->sym_ptr_ptr);
5022aea5
SC
481 if ((symbol->section == &bfd_abs_section)
482 && output_bfd != (bfd *)NULL)
58acdbd7
KR
483 {
484 reloc_entry->address += input_section->output_offset;
485 return bfd_reloc_ok;
486 }
487
fb32909a
KR
488 /* If we are not producing relocateable output, return an error if
489 the symbol is not defined. An undefined weak symbol is
490 considered to have a value of zero (SVR4 ABI, p. 4-27). */
491 if (symbol->section == &bfd_und_section
492 && (symbol->flags & BSF_WEAK) == 0
493 && output_bfd == (bfd *) NULL)
5022aea5 494 flag = bfd_reloc_undefined;
985fca12 495
58acdbd7
KR
496 /* If there is a function supplied to handle this relocation type,
497 call it. It'll return `bfd_reloc_continue' if further processing
498 can be done. */
499 if (howto->special_function)
500 {
501 bfd_reloc_status_type cont;
502 cont = howto->special_function (abfd, reloc_entry, symbol, data,
503 input_section, output_bfd);
504 if (cont != bfd_reloc_continue)
505 return cont;
506 }
985fca12 507
58acdbd7
KR
508 /* Is the address of the relocation really within the section? */
509 if (reloc_entry->address > input_section->_cooked_size)
510 return bfd_reloc_outofrange;
985fca12 511
58acdbd7
KR
512 /* Work out which section the relocation is targetted at and the
513 initial relocation command value. */
514
515 /* Get symbol value. (Common symbols are special.) */
516 if (bfd_is_com_section (symbol->section))
5022aea5 517 relocation = 0;
58acdbd7 518 else
5022aea5 519 relocation = symbol->value;
985fca12 520
985fca12 521
e98e6ec1 522 reloc_target_output_section = symbol->section->output_section;
985fca12 523
58acdbd7
KR
524 /* Convert input-section-relative symbol value to absolute. */
525 if (output_bfd && howto->partial_inplace==false)
5022aea5 526 output_base = 0;
58acdbd7 527 else
5022aea5 528 output_base = reloc_target_output_section->vma;
985fca12 529
65cab589 530 relocation += output_base + symbol->section->output_offset;
985fca12 531
58acdbd7 532 /* Add in supplied addend. */
65cab589 533 relocation += reloc_entry->addend;
985fca12 534
c188b0be
DM
535 /* Here the variable relocation holds the final address of the
536 symbol we are relocating against, plus any addend. */
537
985fca12 538 if (howto->pc_relative == true)
58acdbd7 539 {
c188b0be
DM
540 /* This is a PC relative relocation. We want to set RELOCATION
541 to the distance between the address of the symbol and the
542 location. RELOCATION is already the address of the symbol.
543
544 We start by subtracting the address of the section containing
545 the location.
546
547 If pcrel_offset is set, we must further subtract the position
548 of the location within the section. Some targets arrange for
549 the addend to be the negative of the position of the location
550 within the section; for example, i386-aout does this. For
551 i386-aout, pcrel_offset is false. Some other targets do not
552 include the position of the location; for example, m88kbcs,
553 or ELF. For those targets, pcrel_offset is true.
554
555 If we are producing relocateable output, then we must ensure
556 that this reloc will be correctly computed when the final
557 relocation is done. If pcrel_offset is false we want to wind
558 up with the negative of the location within the section,
559 which means we must adjust the existing addend by the change
560 in the location within the section. If pcrel_offset is true
561 we do not want to adjust the existing addend at all.
562
563 FIXME: This seems logical to me, but for the case of
564 producing relocateable output it is not what the code
565 actually does. I don't want to change it, because it seems
566 far too likely that something will break. */
985fca12 567
58acdbd7
KR
568 relocation -=
569 input_section->output_section->vma + input_section->output_offset;
570
571 if (howto->pcrel_offset == true)
572 relocation -= reloc_entry->address;
5022aea5 573 }
e98e6ec1 574
c26d7d17 575 if (output_bfd!= (bfd *)NULL)
5022aea5 576 {
58acdbd7
KR
577 if ( howto->partial_inplace == false)
578 {
579 /* This is a partial relocation, and we want to apply the relocation
580 to the reloc entry rather than the raw data. Modify the reloc
581 inplace to reflect what we now know. */
582 reloc_entry->addend = relocation;
583 reloc_entry->address += input_section->output_offset;
584 return flag;
585 }
c26d7d17 586 else
58acdbd7
KR
587 {
588 /* This is a partial relocation, but inplace, so modify the
589 reloc record a bit.
590
591 If we've relocated with a symbol with a section, change
592 into a ref to the section belonging to the symbol. */
593
594 reloc_entry->address += input_section->output_offset;
595
596 /* WTF?? */
597 if (abfd->xvec->flavour == bfd_target_coff_flavour)
598 {
c188b0be
DM
599#if 1
600 /* For m68k-coff, the addend was being subtracted twice during
601 relocation with -r. Removing the line below this comment
602 fixes that problem; see PR 2953.
603
604However, Ian wrote the following, regarding removing the line below,
605which explains why it is still enabled: --djm
606
607If you put a patch like that into BFD you need to check all the COFF
608linkers. I am fairly certain that patch will break coff-i386 (e.g.,
609SCO); see coff_i386_reloc in coff-i386.c where I worked around the
610problem in a different way. There may very well be a reason that the
611code works as it does.
612
613Hmmm. The first obvious point is that bfd_perform_relocation should
614not have any tests that depend upon the flavour. It's seem like
615entirely the wrong place for such a thing. The second obvious point
616is that the current code ignores the reloc addend when producing
617relocateable output for COFF. That's peculiar. In fact, I really
618have no idea what the point of the line you want to remove is.
619
620A typical COFF reloc subtracts the old value of the symbol and adds in
621the new value to the location in the object file (if it's a pc
622relative reloc it adds the difference between the symbol value and the
623location). When relocating we need to preserve that property.
624
625BFD handles this by setting the addend to the negative of the old
626value of the symbol. Unfortunately it handles common symbols in a
627non-standard way (it doesn't subtract the old value) but that's a
628different story (we can't change it without losing backward
629compatibility with old object files) (coff-i386 does subtract the old
630value, to be compatible with existing coff-i386 targets, like SCO).
631
632So everything works fine when not producing relocateable output. When
633we are producing relocateable output, logically we should do exactly
634what we do when not producing relocateable output. Therefore, your
635patch is correct. In fact, it should probably always just set
636reloc_entry->addend to 0 for all cases, since it is, in fact, going to
637add the value into the object file. This won't hurt the COFF code,
638which doesn't use the addend; I'm not sure what it will do to other
639formats (the thing to check for would be whether any formats both use
640the addend and set partial_inplace).
641
642When I wanted to make coff-i386 produce relocateable output, I ran
643into the problem that you are running into: I wanted to remove that
644line. Rather than risk it, I made the coff-i386 relocs use a special
645function; it's coff_i386_reloc in coff-i386.c. The function
646specifically adds the addend field into the object file, knowing that
647bfd_perform_relocation is not going to. If you remove that line, then
648coff-i386.c will wind up adding the addend field in twice. It's
649trivial to fix; it just needs to be done.
650
651The problem with removing the line is just that it may break some
652working code. With BFD it's hard to be sure of anything. The right
653way to deal with this is simply to build and test at least all the
654supported COFF targets. It should be straightforward if time and disk
655space consuming. For each target:
656 1) build the linker
657 2) generate some executable, and link it using -r (I would
658 probably use paranoia.o and link against newlib/libc.a, which
659 for all the supported targets would be available in
660 /usr/cygnus/progressive/H-host/target/lib/libc.a).
661 3) make the change to reloc.c
662 4) rebuild the linker
663 5) repeat step 2
664 6) if the resulting object files are the same, you have at least
665 made it no worse
666 7) if they are different you have to figure out which version is
667 right
668*/
58acdbd7 669 relocation -= reloc_entry->addend;
c188b0be 670#endif
58acdbd7
KR
671 reloc_entry->addend = 0;
672 }
673 else
674 {
675 reloc_entry->addend = relocation;
676 }
677 }
985fca12 678 }
e98e6ec1 679 else
58acdbd7
KR
680 {
681 reloc_entry->addend = 0;
682 }
985fca12 683
66a277ab
ILT
684 /* FIXME: This overflow checking is incomplete, because the value
685 might have overflowed before we get here. For a correct check we
686 need to compute the value in a size larger than bitsize, but we
687 can't reasonably do that for a reloc the same size as a host
a49880c8
KR
688 machine word.
689 FIXME: We should also do overflow checking on the result after
690 adding in the value contained in the object file. */
109a640b 691 if (howto->complain_on_overflow != complain_overflow_dont)
65cab589 692 {
109a640b
KR
693 bfd_vma check;
694
695 /* Get the value that will be used for the relocation, but
696 starting at bit position zero. */
697 if (howto->rightshift > howto->bitpos)
698 check = relocation >> (howto->rightshift - howto->bitpos);
699 else
700 check = relocation << (howto->bitpos - howto->rightshift);
701 switch (howto->complain_on_overflow)
702 {
703 case complain_overflow_signed:
704 {
705 /* Assumes two's complement. */
706 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
707 bfd_signed_vma reloc_signed_min = ~ reloc_signed_max;
708
709 /* The above right shift is incorrect for a signed value.
710 Fix it up by forcing on the upper bits. */
711 if (howto->rightshift > howto->bitpos
712 && (bfd_signed_vma) relocation < 0)
713 check |= ((bfd_vma) -1
714 &~ ((bfd_vma) -1
715 >> (howto->rightshift - howto->bitpos)));
716 if ((bfd_signed_vma) check > reloc_signed_max
717 || (bfd_signed_vma) check < reloc_signed_min)
718 flag = bfd_reloc_overflow;
719 }
720 break;
721 case complain_overflow_unsigned:
722 {
723 /* Assumes two's complement. This expression avoids
724 overflow if howto->bitsize is the number of bits in
725 bfd_vma. */
726 bfd_vma reloc_unsigned_max =
727 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
728
729 if ((bfd_vma) check > reloc_unsigned_max)
730 flag = bfd_reloc_overflow;
731 }
732 break;
733 case complain_overflow_bitfield:
734 {
735 /* Assumes two's complement. This expression avoids
736 overflow if howto->bitsize is the number of bits in
737 bfd_vma. */
738 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
739
740 if (((bfd_vma) check &~ reloc_bits) != 0
741 && ((bfd_vma) check &~ reloc_bits) != (-1 &~ reloc_bits))
a49880c8
KR
742 {
743 /* The above right shift is incorrect for a signed
744 value. See if turning on the upper bits fixes the
745 overflow. */
746 if (howto->rightshift > howto->bitpos
747 && (bfd_signed_vma) relocation < 0)
748 {
749 check |= ((bfd_vma) -1
750 &~ ((bfd_vma) -1
751 >> (howto->rightshift - howto->bitpos)));
752 if (((bfd_vma) check &~ reloc_bits) != (-1 &~ reloc_bits))
753 flag = bfd_reloc_overflow;
754 }
755 else
756 flag = bfd_reloc_overflow;
757 }
109a640b
KR
758 }
759 break;
760 default:
761 abort ();
762 }
65cab589
DM
763 }
764
985fca12
SC
765 /*
766 Either we are relocating all the way, or we don't want to apply
767 the relocation to the reloc entry (probably because there isn't
768 any room in the output format to describe addends to relocs)
769 */
c188b0be
DM
770
771 /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
772 (OSF version 1.3, compiler version 3.11). It miscompiles the
773 following program:
774
775 struct str
776 {
777 unsigned int i0;
778 } s = { 0 };
779
780 int
781 main ()
782 {
783 unsigned long x;
784
785 x = 0x100000000;
786 x <<= (unsigned long) s.i0;
787 if (x == 0)
788 printf ("failed\n");
789 else
790 printf ("succeeded (%lx)\n", x);
791 }
792 */
793
794 relocation >>= (bfd_vma) howto->rightshift;
985fca12
SC
795
796 /* Shift everything up to where it's going to be used */
797
c188b0be 798 relocation <<= (bfd_vma) howto->bitpos;
985fca12
SC
799
800 /* Wait for the day when all have the mask in them */
801
802 /* What we do:
803 i instruction to be left alone
804 o offset within instruction
805 r relocation offset to apply
806 S src mask
807 D dst mask
808 N ~dst mask
809 A part 1
810 B part 2
811 R result
812
813 Do this:
814 i i i i i o o o o o from bfd_get<size>
815 and S S S S S to get the size offset we want
816 + r r r r r r r r r r to get the final value to place
817 and D D D D D to chop to right size
818 -----------------------
819 A A A A A
820 And this:
821 ... i i i i i o o o o o from bfd_get<size>
822 and N N N N N get instruction
823 -----------------------
824 ... B B B B B
825
826 And then:
827 B B B B B
828 or A A A A A
829 -----------------------
830 R R R R R R R R R R put into bfd_put<size>
831 */
832
833#define DOIT(x) \
834 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
835
e98e6ec1 836 switch (howto->size)
65cab589
DM
837 {
838 case 0:
839 {
840 char x = bfd_get_8(abfd, (char *)data + addr);
841 DOIT(x);
842 bfd_put_8(abfd,x, (unsigned char *) data + addr);
843 }
844 break;
845
846 case 1:
847 if (relocation)
848 {
849 short x = bfd_get_16(abfd, (bfd_byte *)data + addr);
850 DOIT(x);
851 bfd_put_16(abfd, x, (unsigned char *)data + addr);
852 }
853 break;
854 case 2:
855 if (relocation)
856 {
857 long x = bfd_get_32 (abfd, (bfd_byte *) data + addr);
858 DOIT (x);
859 bfd_put_32 (abfd, x, (bfd_byte *)data + addr);
860 }
861 break;
862 case -2:
863 {
864 long x = bfd_get_32(abfd, (bfd_byte *) data + addr);
865 relocation = -relocation;
866 DOIT(x);
867 bfd_put_32(abfd,x, (bfd_byte *)data + addr);
868 }
869 break;
870
871 case 3:
65cab589
DM
872 /* Do nothing */
873 break;
109a640b
KR
874
875 case 4:
876#ifdef BFD64
877 if (relocation)
878 {
879 bfd_vma x = bfd_get_64 (abfd, (bfd_byte *) data + addr);
880 DOIT (x);
881 bfd_put_64 (abfd, x, (bfd_byte *) data + addr);
882 }
883#else
884 abort ();
885#endif
886 break;
65cab589
DM
887 default:
888 return bfd_reloc_other;
889 }
985fca12
SC
890
891 return flag;
892}
c618de01
SC
893
894
2cf44d7b 895
0cda46cf 896/*
c26d7d17 897DOCDD
e98e6ec1
SC
898INODE
899 howto manager, , typedef arelent, Relocations
900
0cda46cf
SC
901SECTION
902 The howto manager
2cf44d7b 903
0cda46cf
SC
904 When an application wants to create a relocation, but doesn't
905 know what the target machine might call it, it can find out by
906 using this bit of code.
2cf44d7b 907
0cda46cf 908*/
2cf44d7b 909
0cda46cf
SC
910/*
911TYPEDEF
912 bfd_reloc_code_type
2cf44d7b 913
0cda46cf 914DESCRIPTION
fb32909a
KR
915 The insides of a reloc code. The idea is that, eventually, there
916 will be one enumerator for every type of relocation we ever do.
917 Pass one of these values to <<bfd_reloc_type_lookup>>, and it'll
918 return a howto pointer.
919
920 This does mean that the application must determine the correct
921 enumerator value; you can't get a howto pointer from a random set
922 of attributes.
0cda46cf 923
e98e6ec1
SC
924CODE_FRAGMENT
925.
926.typedef enum bfd_reloc_code_real
927.{
fb32909a 928. {* Basic absolute relocations *}
defcfb55 929. BFD_RELOC_64,
defcfb55 930. BFD_RELOC_32,
e98e6ec1 931. BFD_RELOC_16,
0cda46cf 932. BFD_RELOC_8,
fb32909a
KR
933.
934. {* PC-relative relocations *}
935. BFD_RELOC_64_PCREL,
936. BFD_RELOC_32_PCREL,
937. BFD_RELOC_24_PCREL, {* used by i960 *}
938. BFD_RELOC_16_PCREL,
0cda46cf 939. BFD_RELOC_8_PCREL,
e98e6ec1 940.
fb32909a
KR
941. {* Linkage-table relative *}
942. BFD_RELOC_32_BASEREL,
943. BFD_RELOC_16_BASEREL,
944. BFD_RELOC_8_BASEREL,
e98e6ec1 945.
fb32909a
KR
946. {* The type of reloc used to build a contructor table - at the moment
947. probably a 32 bit wide abs address, but the cpu can choose. *}
8070f29d
KR
948. BFD_RELOC_CTOR,
949.
fb32909a
KR
950. {* 8 bits wide, but used to form an address like 0xffnn *}
951. BFD_RELOC_8_FFnn,
952.
953. {* 32-bit pc-relative, shifted right 2 bits (i.e., 30-bit
954. word displacement, e.g. for SPARC) *}
955. BFD_RELOC_32_PCREL_S2,
956.
957. {* High 22 bits of 32-bit value, placed into lower 22 bits of
958. target word; simple reloc. *}
8070f29d 959. BFD_RELOC_HI22,
fb32909a 960. {* Low 10 bits. *}
8070f29d
KR
961. BFD_RELOC_LO10,
962.
fb32909a 963. {* Reloc types used for i960/b.out. *}
8070f29d
KR
964. BFD_RELOC_I960_CALLJ,
965.
8070f29d
KR
966. {* now for the sparc/elf codes *}
967. BFD_RELOC_NONE, {* actually used *}
968. BFD_RELOC_SPARC_WDISP22,
969. BFD_RELOC_SPARC22,
970. BFD_RELOC_SPARC13,
8070f29d
KR
971. BFD_RELOC_SPARC_GOT10,
972. BFD_RELOC_SPARC_GOT13,
973. BFD_RELOC_SPARC_GOT22,
974. BFD_RELOC_SPARC_PC10,
975. BFD_RELOC_SPARC_PC22,
976. BFD_RELOC_SPARC_WPLT30,
977. BFD_RELOC_SPARC_COPY,
978. BFD_RELOC_SPARC_GLOB_DAT,
979. BFD_RELOC_SPARC_JMP_SLOT,
980. BFD_RELOC_SPARC_RELATIVE,
981. BFD_RELOC_SPARC_UA32,
982.
fb32909a 983. {* these are a.out specific? *}
58acdbd7 984. BFD_RELOC_SPARC_BASE13,
8070f29d
KR
985. BFD_RELOC_SPARC_BASE22,
986.
defcfb55 987. {* start-sanitize-v9 *}
defcfb55
KR
988. BFD_RELOC_SPARC_10,
989. BFD_RELOC_SPARC_11,
990.#define BFD_RELOC_SPARC_64 BFD_RELOC_64
991. BFD_RELOC_SPARC_OLO10,
992. BFD_RELOC_SPARC_HH22,
993. BFD_RELOC_SPARC_HM10,
994. BFD_RELOC_SPARC_LM22,
995. BFD_RELOC_SPARC_PC_HH22,
996. BFD_RELOC_SPARC_PC_HM10,
997. BFD_RELOC_SPARC_PC_LM22,
998. BFD_RELOC_SPARC_WDISP16,
58acdbd7 999. BFD_RELOC_SPARC_WDISP19,
defcfb55
KR
1000. BFD_RELOC_SPARC_GLOB_JMP,
1001. BFD_RELOC_SPARC_LO7,
1002. {* end-sanitize-v9 *}
58acdbd7 1003.
fb32909a
KR
1004. {* Bits 27..2 of the relocation address shifted right 2 bits;
1005. simple reloc otherwise. *}
65cab589
DM
1006. BFD_RELOC_MIPS_JMP,
1007.
fb32909a 1008. {* signed 16-bit pc-relative, shifted right 2 bits (e.g. for MIPS) *}
65cab589
DM
1009. BFD_RELOC_16_PCREL_S2,
1010.
fb32909a 1011. {* High 16 bits of 32-bit value; simple reloc. *}
65cab589 1012. BFD_RELOC_HI16,
fb32909a
KR
1013. {* High 16 bits of 32-bit value but the low 16 bits will be sign
1014. extended and added to form the final result. If the low 16
1015. bits form a negative number, we need to add one to the high value
1016. to compensate for the borrow when the low bits are added. *}
65cab589 1017. BFD_RELOC_HI16_S,
fb32909a 1018. {* Low 16 bits. *}
65cab589
DM
1019. BFD_RELOC_LO16,
1020.
fb32909a 1021. {* 16 bit relocation relative to the global pointer. *}
65cab589
DM
1022. BFD_RELOC_MIPS_GPREL,
1023.
c188b0be
DM
1024. {* Relocation against a MIPS literal section. *}
1025. BFD_RELOC_MIPS_LITERAL,
1026.
1027. {* MIPS ELF relocations. *}
1028. BFD_RELOC_MIPS_GOT16,
1029. BFD_RELOC_MIPS_CALL16,
1030. BFD_RELOC_MIPS_GPREL32,
1031.
fb32909a
KR
1032. {* These are, so far, specific to HPPA processors. I'm not sure that some
1033. don't duplicate other reloc types, such as BFD_RELOC_32 and _32_PCREL.
1034. Also, many more were in the list I got that don't fit in well in the
1035. model BFD uses, so I've omitted them for now. If we do make this reloc
1036. type get used for code that really does implement the funky reloc types,
1037. they'll have to be added to this list. *}
65cab589
DM
1038. BFD_RELOC_HPPA_32,
1039. BFD_RELOC_HPPA_11,
1040. BFD_RELOC_HPPA_14,
1041. BFD_RELOC_HPPA_17,
fb32909a 1042.
65cab589
DM
1043. BFD_RELOC_HPPA_L21,
1044. BFD_RELOC_HPPA_R11,
1045. BFD_RELOC_HPPA_R14,
1046. BFD_RELOC_HPPA_R17,
1047. BFD_RELOC_HPPA_LS21,
1048. BFD_RELOC_HPPA_RS11,
1049. BFD_RELOC_HPPA_RS14,
1050. BFD_RELOC_HPPA_RS17,
1051. BFD_RELOC_HPPA_LD21,
1052. BFD_RELOC_HPPA_RD11,
1053. BFD_RELOC_HPPA_RD14,
1054. BFD_RELOC_HPPA_RD17,
1055. BFD_RELOC_HPPA_LR21,
1056. BFD_RELOC_HPPA_RR14,
1057. BFD_RELOC_HPPA_RR17,
fb32909a 1058.
65cab589
DM
1059. BFD_RELOC_HPPA_GOTOFF_11,
1060. BFD_RELOC_HPPA_GOTOFF_14,
1061. BFD_RELOC_HPPA_GOTOFF_L21,
1062. BFD_RELOC_HPPA_GOTOFF_R11,
1063. BFD_RELOC_HPPA_GOTOFF_R14,
1064. BFD_RELOC_HPPA_GOTOFF_LS21,
1065. BFD_RELOC_HPPA_GOTOFF_RS11,
1066. BFD_RELOC_HPPA_GOTOFF_RS14,
1067. BFD_RELOC_HPPA_GOTOFF_LD21,
1068. BFD_RELOC_HPPA_GOTOFF_RD11,
1069. BFD_RELOC_HPPA_GOTOFF_RD14,
1070. BFD_RELOC_HPPA_GOTOFF_LR21,
1071. BFD_RELOC_HPPA_GOTOFF_RR14,
fb32909a 1072.
65cab589
DM
1073. BFD_RELOC_HPPA_DLT_32,
1074. BFD_RELOC_HPPA_DLT_11,
1075. BFD_RELOC_HPPA_DLT_14,
1076. BFD_RELOC_HPPA_DLT_L21,
1077. BFD_RELOC_HPPA_DLT_R11,
1078. BFD_RELOC_HPPA_DLT_R14,
fb32909a 1079.
65cab589
DM
1080. BFD_RELOC_HPPA_ABS_CALL_11,
1081. BFD_RELOC_HPPA_ABS_CALL_14,
1082. BFD_RELOC_HPPA_ABS_CALL_17,
1083. BFD_RELOC_HPPA_ABS_CALL_L21,
1084. BFD_RELOC_HPPA_ABS_CALL_R11,
1085. BFD_RELOC_HPPA_ABS_CALL_R14,
1086. BFD_RELOC_HPPA_ABS_CALL_R17,
1087. BFD_RELOC_HPPA_ABS_CALL_LS21,
1088. BFD_RELOC_HPPA_ABS_CALL_RS11,
1089. BFD_RELOC_HPPA_ABS_CALL_RS14,
1090. BFD_RELOC_HPPA_ABS_CALL_RS17,
1091. BFD_RELOC_HPPA_ABS_CALL_LD21,
1092. BFD_RELOC_HPPA_ABS_CALL_RD11,
1093. BFD_RELOC_HPPA_ABS_CALL_RD14,
1094. BFD_RELOC_HPPA_ABS_CALL_RD17,
1095. BFD_RELOC_HPPA_ABS_CALL_LR21,
1096. BFD_RELOC_HPPA_ABS_CALL_RR14,
1097. BFD_RELOC_HPPA_ABS_CALL_RR17,
fb32909a 1098.
65cab589
DM
1099. BFD_RELOC_HPPA_PCREL_CALL_11,
1100. BFD_RELOC_HPPA_PCREL_CALL_12,
1101. BFD_RELOC_HPPA_PCREL_CALL_14,
1102. BFD_RELOC_HPPA_PCREL_CALL_17,
1103. BFD_RELOC_HPPA_PCREL_CALL_L21,
1104. BFD_RELOC_HPPA_PCREL_CALL_R11,
1105. BFD_RELOC_HPPA_PCREL_CALL_R14,
1106. BFD_RELOC_HPPA_PCREL_CALL_R17,
1107. BFD_RELOC_HPPA_PCREL_CALL_LS21,
1108. BFD_RELOC_HPPA_PCREL_CALL_RS11,
1109. BFD_RELOC_HPPA_PCREL_CALL_RS14,
1110. BFD_RELOC_HPPA_PCREL_CALL_RS17,
1111. BFD_RELOC_HPPA_PCREL_CALL_LD21,
1112. BFD_RELOC_HPPA_PCREL_CALL_RD11,
1113. BFD_RELOC_HPPA_PCREL_CALL_RD14,
1114. BFD_RELOC_HPPA_PCREL_CALL_RD17,
1115. BFD_RELOC_HPPA_PCREL_CALL_LR21,
1116. BFD_RELOC_HPPA_PCREL_CALL_RR14,
1117. BFD_RELOC_HPPA_PCREL_CALL_RR17,
fb32909a 1118.
65cab589
DM
1119. BFD_RELOC_HPPA_PLABEL_32,
1120. BFD_RELOC_HPPA_PLABEL_11,
1121. BFD_RELOC_HPPA_PLABEL_14,
1122. BFD_RELOC_HPPA_PLABEL_L21,
1123. BFD_RELOC_HPPA_PLABEL_R11,
1124. BFD_RELOC_HPPA_PLABEL_R14,
fb32909a 1125.
65cab589
DM
1126. BFD_RELOC_HPPA_UNWIND_ENTRY,
1127. BFD_RELOC_HPPA_UNWIND_ENTRIES,
1128.
a49880c8
KR
1129. {* i386/elf relocations *}
1130. BFD_RELOC_386_GOT32,
1131. BFD_RELOC_386_PLT32,
1132. BFD_RELOC_386_COPY,
1133. BFD_RELOC_386_GLOB_DAT,
1134. BFD_RELOC_386_JUMP_SLOT,
1135. BFD_RELOC_386_RELATIVE,
1136. BFD_RELOC_386_GOTOFF,
1137. BFD_RELOC_386_GOTPC,
1138.
8070f29d
KR
1139. {* this must be the highest numeric value *}
1140. BFD_RELOC_UNUSED
0cda46cf 1141. } bfd_reloc_code_real_type;
2cf44d7b
SC
1142*/
1143
1144
0cda46cf 1145/*
c188b0be 1146FUNCTION
0cda46cf 1147 bfd_reloc_type_lookup
2cf44d7b 1148
e98e6ec1
SC
1149SYNOPSIS
1150 CONST struct reloc_howto_struct *
3860075f 1151 bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
e98e6ec1 1152
0cda46cf 1153DESCRIPTION
c188b0be
DM
1154 Return a pointer to a howto struct which, when
1155 invoked, will perform the relocation @var{code} on data from the
0cda46cf 1156 architecture noted.
2cf44d7b 1157
2cf44d7b
SC
1158*/
1159
1160
1161CONST struct reloc_howto_struct *
3860075f 1162DEFUN(bfd_reloc_type_lookup,(abfd, code),
8070f29d 1163 bfd *abfd AND
3860075f 1164 bfd_reloc_code_real_type code)
2cf44d7b 1165{
8070f29d 1166 return BFD_SEND (abfd, reloc_type_lookup, (abfd, code));
2cf44d7b
SC
1167}
1168
0cda46cf 1169static reloc_howto_type bfd_howto_32 =
66a277ab 1170 HOWTO(0, 00,2,32,false,0,complain_overflow_bitfield,0,"VRT32", false,0xffffffff,0xffffffff,true);
2cf44d7b
SC
1171
1172
0cda46cf 1173/*
e98e6ec1 1174INTERNAL_FUNCTION
0cda46cf
SC
1175 bfd_default_reloc_type_lookup
1176
0cda46cf
SC
1177SYNOPSIS
1178 CONST struct reloc_howto_struct *bfd_default_reloc_type_lookup
65cab589 1179 (bfd *abfd AND
3860075f 1180 bfd_reloc_code_real_type code);
0cda46cf 1181
e98e6ec1 1182DESCRIPTION
65cab589 1183 Provides a default relocation lookup routine for any architecture.
e98e6ec1
SC
1184
1185
0cda46cf 1186*/
65cab589 1187
0cda46cf 1188CONST struct reloc_howto_struct *
65cab589
DM
1189DEFUN(bfd_default_reloc_type_lookup, (abfd, code),
1190 bfd *abfd AND
1191 bfd_reloc_code_real_type code)
0cda46cf 1192{
65cab589 1193 switch (code)
0cda46cf 1194 {
65cab589
DM
1195 case BFD_RELOC_CTOR:
1196 /* The type of reloc used in a ctor, which will be as wide as the
fb32909a 1197 address - so either a 64, 32, or 16 bitter. */
65cab589
DM
1198 switch (bfd_get_arch_info (abfd)->bits_per_address) {
1199 case 64:
1200 BFD_FAIL();
1201 case 32:
1202 return &bfd_howto_32;
1203 case 16:
1204 BFD_FAIL();
1205 default:
0cda46cf 1206 BFD_FAIL();
65cab589
DM
1207 }
1208 default:
1209 BFD_FAIL();
0cda46cf 1210 }
65cab589 1211 return (CONST struct reloc_howto_struct *)NULL;
0cda46cf 1212}
e98e6ec1
SC
1213
1214
d58b7049
SC
1215/*
1216INTERNAL_FUNCTION
1217 bfd_generic_relax_section
1218
1219SYNOPSIS
1220 boolean bfd_generic_relax_section
1221 (bfd *abfd,
1222 asection *section,
5022aea5 1223 asymbol **symbols);
d58b7049
SC
1224
1225DESCRIPTION
1226 Provides default handling for relaxing for back ends which
8070f29d 1227 don't do relaxing -- i.e., does nothing.
d58b7049
SC
1228*/
1229
1230boolean
5022aea5 1231DEFUN(bfd_generic_relax_section,(abfd, section, symbols),
d58b7049
SC
1232 bfd *abfd AND
1233 asection *section AND
5022aea5 1234 asymbol **symbols)
d58b7049
SC
1235{
1236
1237 return false;
1238
1239}
e98e6ec1 1240
d58b7049 1241
e98e6ec1
SC
1242/*
1243INTERNAL_FUNCTION
1244 bfd_generic_get_relocated_section_contents
1245
1246SYNOPSIS
1247 bfd_byte *
65cab589
DM
1248 bfd_generic_get_relocated_section_contents (bfd *abfd,
1249 struct bfd_seclet *seclet,
1250 bfd_byte *data,
1251 boolean relocateable);
e98e6ec1
SC
1252
1253DESCRIPTION
1254 Provides default handling of relocation effort for back ends
1255 which can't be bothered to do it efficiently.
1256
1257*/
1258
1259bfd_byte *
65cab589
DM
1260DEFUN(bfd_generic_get_relocated_section_contents,(abfd,
1261 seclet,
1262 data,
1263 relocateable),
e98e6ec1 1264 bfd *abfd AND
65cab589
DM
1265 struct bfd_seclet *seclet AND
1266 bfd_byte *data AND
1267 boolean relocateable)
e98e6ec1
SC
1268{
1269 extern bfd_error_vector_type bfd_error_vector;
e98e6ec1
SC
1270
1271 /* Get enough memory to hold the stuff */
1272 bfd *input_bfd = seclet->u.indirect.section->owner;
1273 asection *input_section = seclet->u.indirect.section;
1274
8070f29d 1275
e98e6ec1 1276
defcfb55 1277 size_t reloc_size = bfd_get_reloc_upper_bound(input_bfd, input_section);
8070f29d 1278 arelent **reloc_vector = (arelent **) alloca(reloc_size);
e98e6ec1
SC
1279
1280 /* read in the section */
1281 bfd_get_section_contents(input_bfd,
1282 input_section,
1283 data,
1284 0,
1285 input_section->_raw_size);
1286
1287/* We're not relaxing the section, so just copy the size info */
1288 input_section->_cooked_size = input_section->_raw_size;
1289 input_section->reloc_done = true;
1290
1291
1292 if (bfd_canonicalize_reloc(input_bfd,
1293 input_section,
1294 reloc_vector,
1295 seclet->u.indirect.symbols) )
1296 {
1297 arelent **parent;
1298 for (parent = reloc_vector; * parent != (arelent *)NULL;
1299 parent++)
1300 {
1301 bfd_reloc_status_type r=
1302 bfd_perform_relocation(input_bfd,
1303 *parent,
1304 data,
65cab589
DM
1305 input_section,
1306 relocateable ? abfd : (bfd *) NULL);
e98e6ec1 1307
65cab589
DM
1308 if (relocateable)
1309 {
1310 asection *os = input_section->output_section;
1311
1312 /* A partial link, so keep the relocs */
1313 os->orelocation[os->reloc_count] = *parent;
1314 os->reloc_count++;
1315 }
e98e6ec1
SC
1316
1317 if (r != bfd_reloc_ok)
1318 {
e98e6ec1
SC
1319 switch (r)
1320 {
1321 case bfd_reloc_undefined:
1322 bfd_error_vector.undefined_symbol(*parent, seclet);
1323 break;
1324 case bfd_reloc_dangerous:
1325 bfd_error_vector.reloc_dangerous(*parent, seclet);
1326 break;
1327 case bfd_reloc_outofrange:
1328 case bfd_reloc_overflow:
1329 bfd_error_vector.reloc_value_truncated(*parent, seclet);
1330 break;
1331 default:
1332 abort();
1333 break;
1334 }
1335
1336 }
1337 }
1338 }
1339
8070f29d 1340
e98e6ec1
SC
1341 return data;
1342
1343
1344}
This page took 0.150508 seconds and 4 git commands to generate.