Documentation
[binutils-gdb.git] / bfd / reloc.c
1 /* BFD support for handling relocation entries.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 /*doc*
22 @section Relocations
23
24 Bfd maintains relocations in much the same was as it maintains
25 symbols; they are left alone until required, then read in en-mass and
26 traslated into an internal form. There is a common routine
27 @code{bfd_perform_relocation} which acts upon the canonical form to to
28 the actual fixup.
29
30 Note that relocations are maintained on a per section basis, whilst
31 symbols are maintained on a per bfd basis.
32
33 All a back end has to do to fit the bfd interface is to create as many
34 @code{struct reloc_cache_entry} as there are relocations in a
35 particuar section, and fill in the right bits:
36
37 @menu
38 * typedef arelent::
39 * reloc handling functions::
40 @end menu
41
42 */
43 #include "sysdep.h"
44 #include "bfd.h"
45 #include "libbfd.h"
46 /*doc
47 @node typedef arelent, Relocations, reloc handling functions, Relocations
48 @section typedef arelent
49
50
51 */
52
53 /*proto* bfd_perform_relocation
54 The relocation routine returns as a status an enumerated type:
55
56 *+++
57
58 $typedef enum bfd_reloc_status {
59 No errors detected
60
61 $ bfd_reloc_ok,
62
63 The relocation was performed, but there was an overflow.
64
65 $ bfd_reloc_overflow,
66
67 The address to relocate was not within the section supplied
68
69 $ bfd_reloc_outofrange,
70
71 Used by special functions
72
73 $ bfd_reloc_continue,
74
75 Unused
76
77 $ bfd_reloc_notsupported,
78
79 Unsupported relocation size requested.
80
81 $ bfd_reloc_other,
82
83 The symbol to relocate against was undefined.
84
85 $ bfd_reloc_undefined,
86
87 The relocaction was performed, but may not be ok - presently generated
88 only when linking i960 coff files with i960 b.out symbols.
89
90 $ bfd_reloc_dangerous
91 $ }
92 $ bfd_reloc_status_enum_type;
93
94 *---
95
96 */
97
98 /*proto*
99
100 *+++
101
102 $typedef struct reloc_cache_entry
103 ${
104
105 A pointer into the canonical table of pointers
106
107 $ struct symbol_cache_entry **sym_ptr_ptr;
108
109 offset in section
110
111 $ rawdata_offset address;
112
113 addend for relocation value
114
115 $ bfd_vma addend;
116
117 if sym is null this is the section
118
119 $ struct sec *section;
120
121 Pointer to how to perform the required relocation
122
123 $ CONST struct reloc_howto_struct *howto;
124 $} arelent;
125
126 *---
127
128 */
129
130 /*doc*
131 @table @code
132 @item sym_ptr_ptr
133 The symbol table pointer points to a pointer to the symbol ascociated with the
134 relocation request. This would naturaly be the pointer into the table
135 returned by the back end's get_symtab action. @xref{Symbols}. The
136 symbol is referenced through a pointer to a pointer so that tools like
137 the linker can fixup all the symbols of the same name by modifying
138 only one pointer. The relocation routine looks in the symbol and uses
139 the base of the section the symbol is attatched to and the value of
140 the symbol as the initial relocation offset. If the symbol pointer is
141 zero, then the section provided is looked up.
142 @item address
143 The address field gives the offset in bytes from the base of the
144 section data which owns the relocation record to the first byte of
145 relocatable information. The actual data relocated will be relative to
146 this point - for example, a relocation type which modifies the bottom
147 two bytes of a four byte word would not touch the first byte pointed
148 to in a big endian world.
149 @item addend
150 The addend is a value provided by the back end to be added (!) to the
151 relocation offset. It's interpretation is dependent upon the howto.
152 For example, on the 68k the code:
153
154 *+
155 char foo[];
156 main()
157 {
158 return foo[0x12345678];
159 }
160 *-
161 Could be compiled into:
162
163 *+
164 linkw fp,#-4
165 moveb @@#12345678,d0
166 extbl d0
167 unlk fp
168 rts
169 *-
170
171 This could create a reloc pointing to foo, but leave the offset in the data
172 (something like)
173
174 *+
175 RELOCATION RECORDS FOR [.text]:
176 OFFSET TYPE VALUE
177 00000006 32 _foo
178
179 00000000 4e56 fffc ; linkw fp,#-4
180 00000004 1039 1234 5678 ; moveb @@#12345678,d0
181 0000000a 49c0 ; extbl d0
182 0000000c 4e5e ; unlk fp
183 0000000e 4e75 ; rts
184 *-
185 Using coff and an 88k, some instructions don't have enough space in them to
186 represent the full address range, and pointers have to be loaded in
187 two parts. So you'd get something like:
188
189 *+
190 or.u r13,r0,hi16(_foo+0x12345678)
191 ld.b r2,r13,lo16(_foo+0x12345678)
192 jmp r1
193 *-
194 This whould create two relocs, both pointing to _foo, and with 0x12340000
195 in their addend field. The data would consist of:
196
197 *+
198
199 RELOCATION RECORDS FOR [.text]:
200 OFFSET TYPE VALUE
201 00000002 HVRT16 _foo+0x12340000
202 00000006 LVRT16 _foo+0x12340000
203
204 00000000 5da05678 ; or.u r13,r0,0x5678
205 00000004 1c4d5678 ; ld.b r2,r13,0x5678
206 00000008 f400c001 ; jmp r1
207 *-
208 The relocation routine digs out the value from the data, adds it to
209 the addend to get the original offset and then adds the value of _foo.
210 Note that all 32 bits have to be kept around somewhere, to cope with
211 carry from bit 15 to bit 16.
212
213 On further example is the sparc and the a.out format. The sparc has a
214 similar problem to the 88k, in that some instructions don't have
215 room for an entire offset, but on the sparc the parts are created odd
216 sized lumps. The designers of the a.out format chose not to use the
217 data within the section for storing part of the offset; all the offset
218 is kept within the reloc. Any thing in the data should be ignored.
219
220 *+
221 save %sp,-112,%sp
222 sethi %hi(_foo+0x12345678),%g2
223 ldsb [%g2+%lo(_foo+0x12345678)],%i0
224 ret
225 restore
226 *-
227 Both relocs contains a pointer to foo, and the offsets would contain junk.
228
229 *+
230 RELOCATION RECORDS FOR [.text]:
231 OFFSET TYPE VALUE
232 00000004 HI22 _foo+0x12345678
233 00000008 LO10 _foo+0x12345678
234
235 00000000 9de3bf90 ; save %sp,-112,%sp
236 00000004 05000000 ; sethi %hi(_foo+0),%g2
237 00000008 f048a000 ; ldsb [%g2+%lo(_foo+0)],%i0
238 0000000c 81c7e008 ; ret
239 00000010 81e80000 ; restore
240 *-
241 @item section
242 The section field is only used when the symbol pointer field is null.
243 It supplies the section into which the data should be relocated. The
244 field's main use comes from assemblers which do most of the symbol fixups
245 themselves; an assembler may take an internal reference to a label,
246 but since it knows where the label is, it can turn the relocation
247 request from a symbol lookup into a section relative relocation - the
248 relocation emitted has no symbol, just a section to relocate against.
249
250 I'm not sure what it means when both a symbol pointer an a section
251 pointer are present. Some formats use this sort of mechanism to
252 describe PIC relocations, but bfd can't to that sort of thing yet.
253 @item howto
254 The howto field can be imagined as a relocation instruction. It is a
255 pointer to a struct which contains information on what to do with all
256 the other information in the reloc record and data section. A back end
257 would normally have a relocation instruction set and turn relocations
258 into pointers to the correct structure on input - but it would be
259 possible to create each howto field on demand.
260 @end table
261 */
262
263
264 /*proto* reloc_howto_type
265 The @code{reloc_howto_type} is a structure which contains all the
266 information that bfd needs to know to tie up a back end's data.
267
268 *+++
269
270 $typedef CONST struct reloc_howto_struct
271 ${
272 The type field has mainly a documetary use - the back end can to what
273 it wants with it, though the normally the back end's external idea of
274 what a reloc number would be would be stored in this field. For
275 example, the a PC relative word relocation in a coff environment would
276 have the type 023 - because that's what the outside world calls a
277 R_PCRWORD reloc.
278
279 $ unsigned int type;
280
281 The value the final relocation is shifted right by. This drops
282 unwanted data from the relocation.
283
284 $ unsigned int rightshift;
285
286 The size of the item to be relocated - 0, is one byte, 1 is 2 bytes, 3
287 is four bytes.
288
289 $ unsigned int size;
290
291 Now obsolete
292
293 $ unsigned int bitsize;
294
295 Notes that the relocation is relative to the location in the data
296 section of the addend. The relocation function will subtract from the
297 relocation value the address of the location being relocated.
298
299 $ boolean pc_relative;
300
301 Now obsolete
302
303 $ unsigned int bitpos;
304
305 Now obsolete
306
307 $ boolean absolute;
308
309 Causes the relocation routine to return an error if overflow is
310 detected when relocating.
311
312 $ boolean complain_on_overflow;
313
314 If this field is non null, then the supplied function is called rather
315 than the normal function. This allows really strange relocation
316 methods to be accomodated (eg, i960 callj instructions).
317
318 $ bfd_reloc_status_enum_type (*special_function)();
319
320 The textual name of the relocation type.
321
322 $ char *name;
323
324 When performing a partial link, some formats must modify the
325 relocations rather than the data - this flag signals this.
326
327 $ boolean partial_inplace;
328
329 The src_mask is used to select what parts of the read in data are to
330 be used in the relocation sum. Eg, if this was an 8 bit bit of data
331 which we read and relocated, this would be 0x000000ff. When we have
332 relocs which have an addend, such as sun4 extended relocs, the value
333 in the offset part of a relocating field is garbage so we never use
334 it. In this case the mask would be 0x00000000.
335
336 $ bfd_word src_mask;
337 The dst_mask is what parts of the instruction are replaced into the
338 instruction. In most cases src_mask == dst_mask, except in the above
339 special case, where dst_mask would be 0x000000ff, and src_mask would
340 be 0x00000000.
341
342 $ bfd_word dst_mask;
343
344 When some formats create PC relative instructions, they leave the
345 value of the pc of the place being relocated in the offset slot of the
346 instruction, so that a PC relative relocation can be made just by
347 adding in an ordinary offset (eg sun3 a.out). Some formats leave the
348 displacement part of an instruction empty (eg m88k bcs), this flag
349 signals the fact.
350
351 $ boolean pcrel_offset;
352 $} reloc_howto_type;
353 *---
354
355 */
356
357 /*proto* HOWTO
358 The HOWTO define is horrible and will go away.
359 *+
360 #define HOWTO(C, R,S,B, P, BI, ABS, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
361 {(unsigned)C,R,S,B, P, BI, ABS,O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
362 *-
363
364 */
365
366 /*proto* reloc_chain
367 *+
368 typedef unsigned char bfd_byte;
369
370 typedef struct relent_chain {
371 arelent relent;
372 struct relent_chain *next;
373 } arelent_chain;
374
375 *-
376
377 */
378
379
380
381 /*proto*
382 If an output_bfd is supplied to this function the generated image
383 will be relocatable, the relocations are copied to the output file
384 after they have been changed to reflect the new state of the world.
385 There are two ways of reflecting the results of partial linkage in an
386 output file; by modifying the output data in place, and by modifying
387 the relocation record. Some native formats (eg basic a.out and basic
388 coff) have no way of specifying an addend in the relocation type, so
389 the addend has to go in the output data. This is no big deal since in
390 these formats the output data slot will always be big enough for the
391 addend. Complex reloc types with addends were invented to solve just
392 this problem.
393 *; PROTO(bfd_reloc_status_enum_type,
394 bfd_perform_relocation,
395 (bfd * abfd,
396 arelent *reloc_entry,
397 PTR data,
398 asection *input_section,
399 bfd *output_bfd));
400 */
401
402
403 bfd_reloc_status_enum_type
404 DEFUN(bfd_perform_relocation,(abfd,
405 reloc_entry,
406 data,
407 input_section,
408 output_bfd),
409 bfd *abfd AND
410 arelent *reloc_entry AND
411 PTR data AND
412 asection *input_section AND
413 bfd *output_bfd)
414 {
415 bfd_vma relocation;
416 bfd_reloc_status_enum_type flag = bfd_reloc_ok;
417 bfd_vma addr = reloc_entry->address ;
418 bfd_vma output_base = 0;
419 reloc_howto_type *howto = reloc_entry->howto;
420 asection *reloc_target_output_section;
421 asection *reloc_target_input_section;
422 asymbol *symbol;
423
424 if (reloc_entry->sym_ptr_ptr) {
425 symbol = *( reloc_entry->sym_ptr_ptr);
426 if ((symbol->flags & BSF_UNDEFINED) && output_bfd == (bfd *)NULL) {
427 flag = bfd_reloc_undefined;
428 }
429 }
430 else {
431 symbol = (asymbol*)NULL;
432 }
433
434 if (howto->special_function){
435 bfd_reloc_status_enum_type cont;
436 cont = howto->special_function(abfd,
437 reloc_entry,
438 symbol,
439 data,
440 input_section);
441 if (cont != bfd_reloc_continue) return cont;
442 }
443
444 /*
445 Work out which section the relocation is targetted at and the
446 initial relocation command value.
447 */
448
449
450 if (symbol != (asymbol *)NULL){
451 if (symbol->flags & BSF_FORT_COMM) {
452 relocation = 0;
453 }
454 else {
455 relocation = symbol->value;
456 }
457 if (symbol->section != (asection *)NULL)
458 {
459 reloc_target_input_section = symbol->section;
460 }
461 else {
462 reloc_target_input_section = (asection *)NULL;
463 }
464 }
465 else if (reloc_entry->section != (asection *)NULL)
466 {
467 relocation = 0;
468 reloc_target_input_section = reloc_entry->section;
469 }
470 else {
471 relocation = 0;
472 reloc_target_input_section = (asection *)NULL;
473 }
474
475
476 if (reloc_target_input_section != (asection *)NULL) {
477
478 reloc_target_output_section =
479 reloc_target_input_section->output_section;
480
481 if (output_bfd && howto->partial_inplace==false) {
482 output_base = 0;
483 }
484 else {
485 output_base = reloc_target_output_section->vma;
486
487 }
488
489 relocation += output_base + reloc_target_input_section->output_offset;
490 }
491
492 relocation += reloc_entry->addend ;
493
494
495 if(reloc_entry->address > (bfd_vma)(input_section->size))
496 {
497 return bfd_reloc_outofrange;
498 }
499
500
501 if (howto->pc_relative == true)
502 {
503 /*
504 Anything which started out as pc relative should end up that
505 way too.
506
507 There are two ways we can see a pcrel instruction. Sometimes
508 the pcrel displacement has been partially calculated, it
509 includes the distance from the start of the section to the
510 instruction in it (eg sun3), and sometimes the field is
511 totally blank - eg m88kbcs.
512 */
513
514
515 relocation -=
516 output_base + input_section->output_offset;
517
518 if (howto->pcrel_offset == true) {
519 relocation -= reloc_entry->address;
520 }
521
522 }
523
524 if (output_bfd!= (bfd *)NULL) {
525 if ( howto->partial_inplace == false) {
526 /*
527 This is a partial relocation, and we want to apply the relocation
528 to the reloc entry rather than the raw data. Modify the reloc
529 inplace to reflect what we now know.
530 */
531 reloc_entry->addend = relocation ;
532 reloc_entry->section = reloc_target_input_section;
533 if (reloc_target_input_section != (asection *)NULL) {
534 /* If we know the output section we can forget the symbol */
535 reloc_entry->sym_ptr_ptr = (asymbol**)NULL;
536 }
537 reloc_entry->address +=
538 input_section->output_offset;
539 return flag;
540 }
541 else
542 {
543 /* This is a partial relocation, but inplace, so modify the
544 reloc record a bit.
545
546 If we've relocated with a symbol with a section, change
547 into a ref to the section belonging to the symbol
548 */
549
550 if (symbol != (asymbol *)NULL && reloc_target_input_section != (asection *)NULL)
551 {
552 reloc_entry->section = reloc_target_input_section;
553 reloc_entry->sym_ptr_ptr = (asymbol **)NULL;
554 }
555
556 }
557 }
558
559 reloc_entry->addend = 0;
560
561
562 /*
563 Either we are relocating all the way, or we don't want to apply
564 the relocation to the reloc entry (probably because there isn't
565 any room in the output format to describe addends to relocs)
566 */
567 relocation >>= howto->rightshift;
568
569 /* Shift everything up to where it's going to be used */
570
571 relocation <<= howto->bitpos;
572
573 /* Wait for the day when all have the mask in them */
574
575 /* What we do:
576 i instruction to be left alone
577 o offset within instruction
578 r relocation offset to apply
579 S src mask
580 D dst mask
581 N ~dst mask
582 A part 1
583 B part 2
584 R result
585
586 Do this:
587 i i i i i o o o o o from bfd_get<size>
588 and S S S S S to get the size offset we want
589 + r r r r r r r r r r to get the final value to place
590 and D D D D D to chop to right size
591 -----------------------
592 A A A A A
593 And this:
594 ... i i i i i o o o o o from bfd_get<size>
595 and N N N N N get instruction
596 -----------------------
597 ... B B B B B
598
599 And then:
600 B B B B B
601 or A A A A A
602 -----------------------
603 R R R R R R R R R R put into bfd_put<size>
604 */
605
606 #define DOIT(x) \
607 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
608
609 switch (howto->size)
610 {
611 case 0:
612 {
613 char x = bfd_get_8(abfd, (char *)data + addr);
614 DOIT(x);
615 bfd_put_8(abfd,x, (unsigned char *) data + addr);
616 }
617 break;
618
619 case 1:
620 {
621 short x = bfd_get_16(abfd, (bfd_byte *)data + addr);
622 DOIT(x);
623 bfd_put_16(abfd, x, (unsigned char *)data + addr);
624 }
625 break;
626 case 2:
627 {
628 long x = bfd_get_32(abfd, (bfd_byte *) data + addr);
629 DOIT(x);
630 bfd_put_32(abfd,x, (bfd_byte *)data + addr);
631 }
632 break;
633 case 3:
634
635 /* Do nothing */
636 break;
637 default:
638 return bfd_reloc_other;
639 }
640
641 return flag;
642 }
643
644