]>
Commit | Line | Data |
---|---|---|
c618de01 SC |
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 | ||
0cda46cf SC |
21 | /* |
22 | SECTION | |
23 | Relocations | |
985fca12 | 24 | |
0cda46cf SC |
25 | BFD maintains relocations in much the same was as it maintains |
26 | symbols; they are left alone until required, then read in | |
27 | en-mass and traslated into an internal form. There is a common | |
28 | routine <<bfd_perform_relocation>> which acts upon the | |
29 | canonical form to to the actual fixup. | |
985fca12 | 30 | |
0cda46cf SC |
31 | Note that relocations are maintained on a per section basis, |
32 | whilst symbols are maintained on a per BFD basis. | |
985fca12 | 33 | |
0cda46cf SC |
34 | All a back end has to do to fit the BFD interface is to create |
35 | as many <<struct reloc_cache_entry>> as there are relocations | |
36 | in a particuar section, and fill in the right bits: | |
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" |
0cda46cf | 48 | /*doc* |
e98e6ec1 SC |
49 | INODE |
50 | typedef arelent, howto manager, Relocations, Relocations | |
985fca12 | 51 | |
0cda46cf SC |
52 | SUBSECTION |
53 | typedef arelent | |
985fca12 | 54 | |
e98e6ec1 | 55 | This is the structure of a relocation entry: |
985fca12 | 56 | |
e98e6ec1 SC |
57 | CODE_FRAGMENT |
58 | . | |
59 | .typedef enum bfd_reloc_status | |
60 | .{ | |
61 | . {* No errors detected *} | |
0cda46cf | 62 | . bfd_reloc_ok, |
e98e6ec1 SC |
63 | . |
64 | . {* The relocation was performed, but there was an overflow. *} | |
0cda46cf | 65 | . bfd_reloc_overflow, |
e98e6ec1 SC |
66 | . |
67 | . {* The address to relocate was not within the section supplied*} | |
0cda46cf | 68 | . bfd_reloc_outofrange, |
e98e6ec1 SC |
69 | . |
70 | . {* Used by special functions *} | |
0cda46cf | 71 | . bfd_reloc_continue, |
e98e6ec1 SC |
72 | . |
73 | . {* Unused *} | |
0cda46cf | 74 | . bfd_reloc_notsupported, |
e98e6ec1 SC |
75 | . |
76 | . {* Unsupported relocation size requested. *} | |
0cda46cf | 77 | . bfd_reloc_other, |
e98e6ec1 SC |
78 | . |
79 | . {* The symbol to relocate against was undefined.*} | |
0cda46cf | 80 | . bfd_reloc_undefined, |
e98e6ec1 SC |
81 | . |
82 | . {* The relocation was performed, but may not be ok - presently | |
83 | . generated only when linking i960 coff files with i960 b.out | |
84 | . symbols. *} | |
0cda46cf | 85 | . bfd_reloc_dangerous |
e98e6ec1 | 86 | . } |
0cda46cf | 87 | . bfd_reloc_status_type; |
e98e6ec1 SC |
88 | . |
89 | . | |
0cda46cf SC |
90 | .typedef struct reloc_cache_entry |
91 | .{ | |
e98e6ec1 SC |
92 | . {* A pointer into the canonical table of pointers *} |
93 | . struct symbol_cache_entry **sym_ptr_ptr; | |
94 | . | |
95 | . {* offset in section *} | |
96 | . rawdata_offset address; | |
97 | . | |
98 | . {* addend for relocation value *} | |
99 | . bfd_vma addend; | |
100 | . | |
101 | . {* Pointer to how to perform the required relocation *} | |
102 | . CONST struct reloc_howto_struct *howto; | |
103 | . | |
104 | .} arelent; | |
985fca12 | 105 | |
e98e6ec1 | 106 | */ |
985fca12 | 107 | |
e98e6ec1 SC |
108 | /* |
109 | DESCRIPTION | |
985fca12 | 110 | |
e98e6ec1 | 111 | Here is a description of each of the fields within a relent: |
985fca12 | 112 | |
e98e6ec1 | 113 | o sym_ptr_ptr |
985fca12 | 114 | |
e98e6ec1 SC |
115 | The symbol table pointer points to a pointer to the symbol |
116 | associated with the relocation request. This would naturally | |
117 | be the pointer into the table returned by the back end's | |
118 | get_symtab action. @xref{Symbols}. The symbol is referenced | |
119 | through a pointer to a pointer so that tools like the linker | |
120 | can fix up all the symbols of the same name by modifying only | |
121 | one pointer. The relocation routine looks in the symbol and | |
122 | uses the base of the section the symbol is attached to and the | |
123 | value of the symbol as the initial relocation offset. If the | |
124 | symbol pointer is zero, then the section provided is looked up. | |
985fca12 | 125 | |
e98e6ec1 | 126 | o address |
985fca12 | 127 | |
e98e6ec1 SC |
128 | The address field gives the offset in bytes from the base of |
129 | the section data which owns the relocation record to the first | |
130 | byte of relocatable information. The actual data relocated | |
131 | will be relative to this point - for example, a relocation | |
132 | type which modifies the bottom two bytes of a four byte word | |
133 | would not touch the first byte pointed to in a big endian | |
134 | world. @item addend The addend is a value provided by the back | |
135 | end to be added (!) to the relocation offset. Its | |
136 | interpretation is dependent upon the howto. For example, on | |
137 | the 68k the code: | |
985fca12 | 138 | |
985fca12 | 139 | |
e98e6ec1 SC |
140 | | char foo[]; |
141 | | main() | |
142 | | { | |
143 | | return foo[0x12345678]; | |
144 | | } | |
985fca12 | 145 | |
e98e6ec1 | 146 | Could be compiled into: |
985fca12 | 147 | |
e98e6ec1 SC |
148 | | linkw fp,#-4 |
149 | | moveb @@#12345678,d0 | |
150 | | extbl d0 | |
151 | | unlk fp | |
152 | | rts | |
985fca12 | 153 | |
985fca12 | 154 | |
e98e6ec1 SC |
155 | This could create a reloc pointing to foo, but leave the |
156 | offset in the data (something like) | |
0cda46cf | 157 | |
985fca12 | 158 | |
e98e6ec1 SC |
159 | |RELOCATION RECORDS FOR [.text]: |
160 | |offset type value | |
161 | |00000006 32 _foo | |
162 | | | |
163 | |00000000 4e56 fffc ; linkw fp,#-4 | |
164 | |00000004 1039 1234 5678 ; moveb @@#12345678,d0 | |
165 | |0000000a 49c0 ; extbl d0 | |
166 | |0000000c 4e5e ; unlk fp | |
167 | |0000000e 4e75 ; rts | |
0cda46cf | 168 | |
985fca12 | 169 | |
e98e6ec1 SC |
170 | Using coff and an 88k, some instructions don't have enough |
171 | space in them to represent the full address range, and | |
172 | pointers have to be loaded in two parts. So you'd get something like: | |
0cda46cf | 173 | |
985fca12 | 174 | |
e98e6ec1 SC |
175 | | or.u r13,r0,hi16(_foo+0x12345678) |
176 | | ld.b r2,r13,lo16(_foo+0x12345678) | |
177 | | jmp r1 | |
985fca12 | 178 | |
985fca12 | 179 | |
e98e6ec1 SC |
180 | This whould create two relocs, both pointing to _foo, and with |
181 | 0x12340000 in their addend field. The data would consist of: | |
0cda46cf | 182 | |
985fca12 | 183 | |
e98e6ec1 SC |
184 | |RELOCATION RECORDS FOR [.text]: |
185 | |offset type value | |
186 | |00000002 HVRT16 _foo+0x12340000 | |
187 | |00000006 LVRT16 _foo+0x12340000 | |
985fca12 | 188 | |
e98e6ec1 SC |
189 | |00000000 5da05678 ; or.u r13,r0,0x5678 |
190 | |00000004 1c4d5678 ; ld.b r2,r13,0x5678 | |
191 | |00000008 f400c001 ; jmp r1 | |
985fca12 | 192 | |
0cda46cf | 193 | |
e98e6ec1 SC |
194 | The relocation routine digs out the value from the data, adds |
195 | it to the addend to get the original offset and then adds the | |
196 | value of _foo. Note that all 32 bits have to be kept around | |
197 | somewhere, to cope with carry from bit 15 to bit 16. | |
985fca12 | 198 | |
e98e6ec1 SC |
199 | On further example is the sparc and the a.out format. The |
200 | sparc has a similar problem to the 88k, in that some | |
201 | instructions don't have room for an entire offset, but on the | |
202 | sparc the parts are created odd sized lumps. The designers of | |
203 | the a.out format chose not to use the data within the section | |
204 | for storing part of the offset; all the offset is kept within | |
205 | the reloc. Any thing in the data should be ignored. | |
0cda46cf | 206 | |
e98e6ec1 SC |
207 | | save %sp,-112,%sp |
208 | | sethi %hi(_foo+0x12345678),%g2 | |
209 | | ldsb [%g2+%lo(_foo+0x12345678)],%i0 | |
210 | | ret | |
211 | | restore | |
0cda46cf | 212 | |
e98e6ec1 SC |
213 | Both relocs contains a pointer to foo, and the offsets would |
214 | contain junk. | |
985fca12 | 215 | |
0cda46cf | 216 | |
e98e6ec1 SC |
217 | |RELOCATION RECORDS FOR [.text]: |
218 | |offset type value | |
219 | |00000004 HI22 _foo+0x12345678 | |
220 | |00000008 LO10 _foo+0x12345678 | |
221 | ||
222 | |00000000 9de3bf90 ; save %sp,-112,%sp | |
223 | |00000004 05000000 ; sethi %hi(_foo+0),%g2 | |
224 | |00000008 f048a000 ; ldsb [%g2+%lo(_foo+0)],%i0 | |
225 | |0000000c 81c7e008 ; ret | |
226 | |00000010 81e80000 ; restore | |
227 | ||
0cda46cf | 228 | |
e98e6ec1 SC |
229 | o howto |
230 | ||
231 | The howto field can be imagined as a | |
232 | relocation instruction. It is a pointer to a struct which | |
233 | contains information on what to do with all the other | |
234 | information in the reloc record and data section. A back end | |
235 | would normally have a relocation instruction set and turn | |
236 | relocations into pointers to the correct structure on input - | |
237 | but it would be possible to create each howto field on demand. | |
238 | ||
985fca12 SC |
239 | */ |
240 | ||
241 | ||
0cda46cf SC |
242 | /* |
243 | SUBSUBSECTION | |
e98e6ec1 | 244 | <<reloc_howto_type>> |
985fca12 | 245 | |
e98e6ec1 SC |
246 | The <<reloc_howto_type>> is a structure which contains all the |
247 | information that BFD needs to know to tie up a back end's data. | |
985fca12 | 248 | |
e98e6ec1 | 249 | CODE_FRAGMENT |
5022aea5 | 250 | .struct symbol_cache_entry; {* Forward declaration *} |
e98e6ec1 | 251 | . |
0cda46cf SC |
252 | .typedef CONST struct reloc_howto_struct |
253 | .{ | |
e98e6ec1 SC |
254 | . {* The type field has mainly a documetary use - the back end can |
255 | . to what it wants with it, though the normally the back end's | |
256 | . external idea of what a reloc number would be would be stored | |
257 | . in this field. For example, the a PC relative word relocation | |
258 | . in a coff environment would have the type 023 - because that's | |
259 | . what the outside world calls a R_PCRWORD reloc. *} | |
0cda46cf | 260 | . unsigned int type; |
e98e6ec1 SC |
261 | . |
262 | . {* The value the final relocation is shifted right by. This drops | |
263 | . unwanted data from the relocation. *} | |
0cda46cf | 264 | . unsigned int rightshift; |
e98e6ec1 SC |
265 | . |
266 | . {* The size of the item to be relocated - 0, is one byte, 1 is 2 | |
267 | . bytes, 3 is four bytes. *} | |
0cda46cf | 268 | . unsigned int size; |
e98e6ec1 SC |
269 | . |
270 | . {* Now obsolete *} | |
0cda46cf | 271 | . unsigned int bitsize; |
e98e6ec1 SC |
272 | . |
273 | . {* Notes that the relocation is relative to the location in the | |
274 | . data section of the addend. The relocation function will | |
275 | . subtract from the relocation value the address of the location | |
276 | . being relocated. *} | |
0cda46cf | 277 | . boolean pc_relative; |
e98e6ec1 SC |
278 | . |
279 | . {* Now obsolete *} | |
0cda46cf | 280 | . unsigned int bitpos; |
e98e6ec1 SC |
281 | . |
282 | . {* Now obsolete *} | |
0cda46cf | 283 | . boolean absolute; |
e98e6ec1 SC |
284 | . |
285 | . {* Causes the relocation routine to return an error if overflow | |
286 | . is detected when relocating. *} | |
0cda46cf | 287 | . boolean complain_on_overflow; |
e98e6ec1 SC |
288 | . |
289 | . {* If this field is non null, then the supplied function is | |
290 | . called rather than the normal function. This allows really | |
291 | . strange relocation methods to be accomodated (eg, i960 callj | |
292 | . instructions). *} | |
5022aea5 SC |
293 | . bfd_reloc_status_type EXFUN ((*special_function), |
294 | . (bfd *abfd, | |
295 | . arelent *reloc_entry, | |
296 | . struct symbol_cache_entry *symbol, | |
297 | . PTR data, | |
8070f29d KR |
298 | . asection *input_section, |
299 | . bfd *output_bfd )); | |
e98e6ec1 SC |
300 | . |
301 | . {* The textual name of the relocation type. *} | |
0cda46cf | 302 | . char *name; |
e98e6ec1 SC |
303 | . |
304 | . {* When performing a partial link, some formats must modify the | |
305 | . relocations rather than the data - this flag signals this.*} | |
0cda46cf | 306 | . boolean partial_inplace; |
e98e6ec1 SC |
307 | . |
308 | . {* The src_mask is used to select what parts of the read in data | |
309 | . are to be used in the relocation sum. Eg, if this was an 8 bit | |
310 | . bit of data which we read and relocated, this would be | |
311 | . 0x000000ff. When we have relocs which have an addend, such as | |
312 | . sun4 extended relocs, the value in the offset part of a | |
313 | . relocating field is garbage so we never use it. In this case | |
314 | . the mask would be 0x00000000. *} | |
0cda46cf | 315 | . bfd_word src_mask; |
e98e6ec1 SC |
316 | . |
317 | . {* The dst_mask is what parts of the instruction are replaced | |
318 | . into the instruction. In most cases src_mask == dst_mask, | |
319 | . except in the above special case, where dst_mask would be | |
320 | . 0x000000ff, and src_mask would be 0x00000000. *} | |
0cda46cf | 321 | . bfd_word dst_mask; |
e98e6ec1 SC |
322 | . |
323 | . {* When some formats create PC relative instructions, they leave | |
324 | . the value of the pc of the place being relocated in the offset | |
325 | . slot of the instruction, so that a PC relative relocation can | |
326 | . be made just by adding in an ordinary offset (eg sun3 a.out). | |
327 | . Some formats leave the displacement part of an instruction | |
328 | . empty (eg m88k bcs), this flag signals the fact.*} | |
0cda46cf | 329 | . boolean pcrel_offset; |
e98e6ec1 | 330 | . |
0cda46cf | 331 | .} reloc_howto_type; |
985fca12 | 332 | |
0cda46cf | 333 | */ |
985fca12 | 334 | |
0cda46cf SC |
335 | /* |
336 | FUNCTION | |
e98e6ec1 SC |
337 | the HOWTO macro |
338 | ||
0cda46cf SC |
339 | DESCRIPTION |
340 | The HOWTO define is horrible and will go away. | |
341 | ||
342 | ||
343 | .#define HOWTO(C, R,S,B, P, BI, ABS, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \ | |
344 | . {(unsigned)C,R,S,B, P, BI, ABS,O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC} | |
345 | ||
346 | DESCRIPTION | |
347 | And will be replaced with the totally magic way. But for the | |
348 | moment, we are compatible, so do it this way.. | |
349 | ||
350 | ||
351 | .#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,false,false,FUNCTION, NAME,false,0,0,IN) | |
352 | . | |
353 | DESCRIPTION | |
354 | Helper routine to turn a symbol into a relocation value. | |
355 | ||
e98e6ec1 SC |
356 | .#define HOWTO_PREPARE(relocation, symbol) \ |
357 | . { \ | |
358 | . if (symbol != (asymbol *)NULL) { \ | |
359 | . if (symbol->section == &bfd_com_section) { \ | |
360 | . relocation = 0; \ | |
361 | . } \ | |
362 | . else { \ | |
363 | . relocation = symbol->value; \ | |
364 | . } \ | |
365 | . } \ | |
366 | .} | |
985fca12 SC |
367 | |
368 | */ | |
369 | ||
0cda46cf SC |
370 | /* |
371 | TYPEDEF | |
372 | reloc_chain | |
985fca12 | 373 | |
0cda46cf | 374 | DESCRIPTION |
985fca12 | 375 | |
0cda46cf | 376 | How relocs are tied together |
985fca12 | 377 | |
0cda46cf SC |
378 | .typedef unsigned char bfd_byte; |
379 | . | |
380 | .typedef struct relent_chain { | |
381 | . arelent relent; | |
382 | . struct relent_chain *next; | |
383 | .} arelent_chain; | |
985fca12 SC |
384 | |
385 | */ | |
386 | ||
387 | ||
388 | ||
0cda46cf SC |
389 | /* |
390 | FUNCTION | |
391 | bfd_perform_relocation | |
392 | ||
e98e6ec1 SC |
393 | SYNOPSIS |
394 | bfd_reloc_status_type | |
395 | bfd_perform_relocation | |
396 | (bfd * abfd, | |
397 | arelent *reloc_entry, | |
398 | PTR data, | |
399 | asection *input_section, | |
400 | bfd *output_bfd); | |
401 | ||
0cda46cf SC |
402 | DESCRIPTION |
403 | If an output_bfd is supplied to this function the generated | |
404 | image will be relocatable, the relocations are copied to the | |
405 | output file after they have been changed to reflect the new | |
406 | state of the world. There are two ways of reflecting the | |
407 | results of partial linkage in an output file; by modifying the | |
408 | output data in place, and by modifying the relocation record. | |
409 | Some native formats (eg basic a.out and basic coff) have no | |
410 | way of specifying an addend in the relocation type, so the | |
411 | addend has to go in the output data. This is no big deal | |
412 | since in these formats the output data slot will always be big | |
413 | enough for the addend. Complex reloc types with addends were | |
414 | invented to solve just this problem. | |
415 | ||
985fca12 SC |
416 | */ |
417 | ||
418 | ||
0cda46cf | 419 | bfd_reloc_status_type |
985fca12 SC |
420 | DEFUN(bfd_perform_relocation,(abfd, |
421 | reloc_entry, | |
422 | data, | |
423 | input_section, | |
424 | output_bfd), | |
425 | bfd *abfd AND | |
426 | arelent *reloc_entry AND | |
427 | PTR data AND | |
428 | asection *input_section AND | |
429 | bfd *output_bfd) | |
430 | { | |
431 | bfd_vma relocation; | |
0cda46cf | 432 | bfd_reloc_status_type flag = bfd_reloc_ok; |
985fca12 SC |
433 | bfd_vma addr = reloc_entry->address ; |
434 | bfd_vma output_base = 0; | |
435 | reloc_howto_type *howto = reloc_entry->howto; | |
e98e6ec1 SC |
436 | asection *reloc_target_output_section ; |
437 | ||
985fca12 SC |
438 | asymbol *symbol; |
439 | ||
e98e6ec1 | 440 | symbol = *( reloc_entry->sym_ptr_ptr); |
5022aea5 SC |
441 | if ((symbol->section == &bfd_abs_section) |
442 | && output_bfd != (bfd *)NULL) | |
443 | { | |
444 | reloc_entry->address += input_section->output_offset; | |
445 | ||
446 | return bfd_reloc_ok; | |
447 | ||
448 | } | |
449 | ||
e98e6ec1 | 450 | if ((symbol->section == &bfd_und_section) && output_bfd == (bfd *)NULL) { |
5022aea5 SC |
451 | flag = bfd_reloc_undefined; |
452 | } | |
985fca12 | 453 | |
8070f29d | 454 | if (howto->special_function) { |
5022aea5 SC |
455 | bfd_reloc_status_type cont; |
456 | cont = howto->special_function(abfd, | |
457 | reloc_entry, | |
458 | symbol, | |
459 | data, | |
8070f29d KR |
460 | input_section, |
461 | output_bfd); | |
5022aea5 SC |
462 | if (cont != bfd_reloc_continue) return cont; |
463 | } | |
985fca12 SC |
464 | |
465 | /* | |
466 | Work out which section the relocation is targetted at and the | |
467 | initial relocation command value. | |
468 | */ | |
469 | ||
470 | ||
e98e6ec1 | 471 | if (symbol->section == &bfd_com_section) { |
5022aea5 SC |
472 | relocation = 0; |
473 | } | |
e98e6ec1 | 474 | else { |
5022aea5 SC |
475 | relocation = symbol->value; |
476 | } | |
985fca12 | 477 | |
985fca12 | 478 | |
e98e6ec1 | 479 | reloc_target_output_section = symbol->section->output_section; |
985fca12 | 480 | |
e98e6ec1 | 481 | if (output_bfd && howto->partial_inplace==false) { |
5022aea5 SC |
482 | output_base = 0; |
483 | } | |
e98e6ec1 | 484 | else { |
5022aea5 | 485 | output_base = reloc_target_output_section->vma; |
985fca12 | 486 | |
5022aea5 | 487 | } |
985fca12 | 488 | |
e98e6ec1 SC |
489 | relocation += output_base + symbol->section->output_offset; |
490 | ||
985fca12 SC |
491 | |
492 | relocation += reloc_entry->addend ; | |
493 | ||
494 | ||
e98e6ec1 SC |
495 | if(reloc_entry->address > input_section->_cooked_size) |
496 | { | |
497 | return bfd_reloc_outofrange; | |
498 | } | |
985fca12 SC |
499 | |
500 | ||
501 | if (howto->pc_relative == true) | |
e98e6ec1 SC |
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 | */ | |
985fca12 SC |
513 | |
514 | ||
e98e6ec1 SC |
515 | relocation -= |
516 | input_section->output_section->vma + input_section->output_offset; | |
985fca12 | 517 | |
e98e6ec1 | 518 | if (howto->pcrel_offset == true) { |
5022aea5 SC |
519 | relocation -= reloc_entry->address; |
520 | } | |
985fca12 | 521 | |
e98e6ec1 SC |
522 | } |
523 | ||
985fca12 | 524 | if (output_bfd!= (bfd *)NULL) { |
5022aea5 SC |
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->address += input_section->output_offset; | |
533 | return flag; | |
534 | } | |
535 | else | |
536 | { | |
537 | /* This is a partial relocation, but inplace, so modify the | |
538 | reloc record a bit. | |
539 | ||
540 | If we've relocated with a symbol with a section, change | |
541 | into a ref to the section belonging to the symbol | |
542 | */ | |
543 | reloc_entry->addend = relocation ; | |
544 | reloc_entry->address += input_section->output_offset; | |
e98e6ec1 SC |
545 | |
546 | ||
985fca12 | 547 | } |
5022aea5 | 548 | } |
e98e6ec1 SC |
549 | else |
550 | { | |
551 | ||
5022aea5 SC |
552 | reloc_entry->addend = 0; |
553 | } | |
e98e6ec1 | 554 | |
985fca12 SC |
555 | |
556 | ||
557 | /* | |
558 | Either we are relocating all the way, or we don't want to apply | |
559 | the relocation to the reloc entry (probably because there isn't | |
560 | any room in the output format to describe addends to relocs) | |
561 | */ | |
562 | relocation >>= howto->rightshift; | |
563 | ||
564 | /* Shift everything up to where it's going to be used */ | |
565 | ||
566 | relocation <<= howto->bitpos; | |
567 | ||
568 | /* Wait for the day when all have the mask in them */ | |
569 | ||
570 | /* What we do: | |
571 | i instruction to be left alone | |
572 | o offset within instruction | |
573 | r relocation offset to apply | |
574 | S src mask | |
575 | D dst mask | |
576 | N ~dst mask | |
577 | A part 1 | |
578 | B part 2 | |
579 | R result | |
580 | ||
581 | Do this: | |
582 | i i i i i o o o o o from bfd_get<size> | |
583 | and S S S S S to get the size offset we want | |
584 | + r r r r r r r r r r to get the final value to place | |
585 | and D D D D D to chop to right size | |
586 | ----------------------- | |
587 | A A A A A | |
588 | And this: | |
589 | ... i i i i i o o o o o from bfd_get<size> | |
590 | and N N N N N get instruction | |
591 | ----------------------- | |
592 | ... B B B B B | |
593 | ||
594 | And then: | |
595 | B B B B B | |
596 | or A A A A A | |
597 | ----------------------- | |
598 | R R R R R R R R R R put into bfd_put<size> | |
599 | */ | |
600 | ||
601 | #define DOIT(x) \ | |
602 | x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask)) | |
603 | ||
e98e6ec1 SC |
604 | switch (howto->size) |
605 | { | |
5022aea5 SC |
606 | case 0: |
607 | { | |
608 | char x = bfd_get_8(abfd, (char *)data + addr); | |
609 | DOIT(x); | |
610 | bfd_put_8(abfd,x, (unsigned char *) data + addr); | |
611 | } | |
e98e6ec1 SC |
612 | break; |
613 | ||
5022aea5 SC |
614 | case 1: |
615 | { | |
616 | short x = bfd_get_16(abfd, (bfd_byte *)data + addr); | |
617 | DOIT(x); | |
618 | bfd_put_16(abfd, x, (unsigned char *)data + addr); | |
619 | } | |
e98e6ec1 | 620 | break; |
5022aea5 SC |
621 | case 2: |
622 | { | |
623 | long x = bfd_get_32(abfd, (bfd_byte *) data + addr); | |
624 | DOIT(x); | |
625 | bfd_put_32(abfd,x, (bfd_byte *)data + addr); | |
626 | } | |
e98e6ec1 | 627 | break; |
5022aea5 | 628 | case 3: |
e98e6ec1 SC |
629 | |
630 | /* Do nothing */ | |
631 | break; | |
5022aea5 | 632 | default: |
e98e6ec1 SC |
633 | return bfd_reloc_other; |
634 | } | |
985fca12 SC |
635 | |
636 | return flag; | |
637 | } | |
c618de01 SC |
638 | |
639 | ||
2cf44d7b | 640 | |
0cda46cf | 641 | /* |
e98e6ec1 SC |
642 | INODE |
643 | howto manager, , typedef arelent, Relocations | |
644 | ||
0cda46cf SC |
645 | SECTION |
646 | The howto manager | |
2cf44d7b | 647 | |
0cda46cf SC |
648 | When an application wants to create a relocation, but doesn't |
649 | know what the target machine might call it, it can find out by | |
650 | using this bit of code. | |
2cf44d7b | 651 | |
0cda46cf | 652 | */ |
2cf44d7b | 653 | |
0cda46cf SC |
654 | /* |
655 | TYPEDEF | |
656 | bfd_reloc_code_type | |
2cf44d7b | 657 | |
0cda46cf SC |
658 | DESCRIPTION |
659 | The insides of a reloc code | |
660 | ||
e98e6ec1 SC |
661 | CODE_FRAGMENT |
662 | . | |
663 | .typedef enum bfd_reloc_code_real | |
664 | .{ | |
665 | . {* 16 bits wide, simple reloc *} | |
666 | . BFD_RELOC_16, | |
667 | . | |
668 | . {* 8 bits wide, but used to form an address like 0xffnn *} | |
0cda46cf | 669 | . BFD_RELOC_8_FFnn, |
e98e6ec1 SC |
670 | . |
671 | . {* 8 bits wide, simple *} | |
0cda46cf | 672 | . BFD_RELOC_8, |
e98e6ec1 SC |
673 | . |
674 | . {* 8 bits wide, pc relative *} | |
0cda46cf | 675 | . BFD_RELOC_8_PCREL, |
e98e6ec1 SC |
676 | . |
677 | . {* The type of reloc used to build a contructor table - at the | |
678 | . moment probably a 32 bit wide abs address, but the cpu can | |
679 | . choose. *} | |
680 | . | |
8070f29d KR |
681 | . BFD_RELOC_CTOR, |
682 | . | |
683 | . {* 32 bits wide, simple reloc *} | |
684 | . BFD_RELOC_32, | |
685 | . {* 32 bits, PC-relative *} | |
686 | . BFD_RELOC_32_PCREL, | |
687 | . | |
688 | . {* High 22 bits of 32-bit value; simple reloc. *} | |
689 | . BFD_RELOC_HI22, | |
690 | . {* Low 10 bits. *} | |
691 | . BFD_RELOC_LO10, | |
692 | . | |
693 | . {* Reloc types used for i960/b.out. *} | |
694 | . BFD_RELOC_24_PCREL, | |
695 | . BFD_RELOC_I960_CALLJ, | |
696 | . | |
697 | . BFD_RELOC_16_PCREL, | |
698 | . {* 32-bit pc-relative, shifted right 2 bits (i.e., 30-bit | |
699 | . word displacement, e.g. for SPARC) *} | |
700 | . BFD_RELOC_32_PCREL_S2, | |
701 | . | |
702 | . {* now for the sparc/elf codes *} | |
703 | . BFD_RELOC_NONE, {* actually used *} | |
704 | . BFD_RELOC_SPARC_WDISP22, | |
705 | . BFD_RELOC_SPARC22, | |
706 | . BFD_RELOC_SPARC13, | |
707 | . BFD_RELOC_SPARC_BASE13, | |
708 | . BFD_RELOC_SPARC_GOT10, | |
709 | . BFD_RELOC_SPARC_GOT13, | |
710 | . BFD_RELOC_SPARC_GOT22, | |
711 | . BFD_RELOC_SPARC_PC10, | |
712 | . BFD_RELOC_SPARC_PC22, | |
713 | . BFD_RELOC_SPARC_WPLT30, | |
714 | . BFD_RELOC_SPARC_COPY, | |
715 | . BFD_RELOC_SPARC_GLOB_DAT, | |
716 | . BFD_RELOC_SPARC_JMP_SLOT, | |
717 | . BFD_RELOC_SPARC_RELATIVE, | |
718 | . BFD_RELOC_SPARC_UA32, | |
719 | . | |
720 | . {* this one is a.out specific? *} | |
721 | . BFD_RELOC_SPARC_BASE22, | |
722 | . | |
723 | . {* this must be the highest numeric value *} | |
724 | . BFD_RELOC_UNUSED | |
0cda46cf | 725 | . } bfd_reloc_code_real_type; |
2cf44d7b SC |
726 | */ |
727 | ||
728 | ||
729 | ||
0cda46cf SC |
730 | /* |
731 | SECTION | |
732 | bfd_reloc_type_lookup | |
2cf44d7b | 733 | |
e98e6ec1 SC |
734 | SYNOPSIS |
735 | CONST struct reloc_howto_struct * | |
3860075f | 736 | bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code); |
e98e6ec1 | 737 | |
0cda46cf SC |
738 | DESCRIPTION |
739 | This routine returns a pointer to a howto struct which when | |
740 | invoked, will perform the supplied relocation on data from the | |
741 | architecture noted. | |
2cf44d7b | 742 | |
2cf44d7b SC |
743 | */ |
744 | ||
745 | ||
746 | CONST struct reloc_howto_struct * | |
3860075f | 747 | DEFUN(bfd_reloc_type_lookup,(abfd, code), |
8070f29d | 748 | bfd *abfd AND |
3860075f | 749 | bfd_reloc_code_real_type code) |
2cf44d7b | 750 | { |
8070f29d | 751 | return BFD_SEND (abfd, reloc_type_lookup, (abfd, code)); |
2cf44d7b SC |
752 | } |
753 | ||
0cda46cf SC |
754 | static reloc_howto_type bfd_howto_32 = |
755 | HOWTO(0, 00,2,32,false,0,false,true,0,"VRT32", false,0xffffffff,0xffffffff,true); | |
2cf44d7b SC |
756 | |
757 | ||
0cda46cf | 758 | /* |
e98e6ec1 | 759 | INTERNAL_FUNCTION |
0cda46cf SC |
760 | bfd_default_reloc_type_lookup |
761 | ||
0cda46cf SC |
762 | SYNOPSIS |
763 | CONST struct reloc_howto_struct *bfd_default_reloc_type_lookup | |
764 | (CONST struct bfd_arch_info *, | |
3860075f | 765 | bfd_reloc_code_real_type code); |
0cda46cf | 766 | |
e98e6ec1 SC |
767 | DESCRIPTION |
768 | Provides a default relocation lookuperer for any architectue | |
769 | ||
770 | ||
0cda46cf SC |
771 | */ |
772 | CONST struct reloc_howto_struct * | |
773 | DEFUN(bfd_default_reloc_type_lookup,(arch, code), | |
774 | CONST struct bfd_arch_info *arch AND | |
3860075f | 775 | bfd_reloc_code_real_type code) |
0cda46cf SC |
776 | { |
777 | switch (code) | |
778 | { | |
779 | case BFD_RELOC_CTOR: | |
780 | /* The type of reloc used in a ctor, which will be as wide as the | |
781 | address - so either a 64, 32, or 16 bitter.. */ | |
782 | switch (arch->bits_per_address) { | |
783 | case 64: | |
784 | BFD_FAIL(); | |
785 | case 32: | |
786 | return &bfd_howto_32; | |
787 | case 16: | |
788 | BFD_FAIL(); | |
789 | default: | |
790 | BFD_FAIL(); | |
791 | } | |
792 | default: | |
793 | BFD_FAIL(); | |
794 | } | |
795 | return (struct reloc_howto_struct *)NULL; | |
796 | } | |
e98e6ec1 SC |
797 | |
798 | ||
d58b7049 SC |
799 | /* |
800 | INTERNAL_FUNCTION | |
801 | bfd_generic_relax_section | |
802 | ||
803 | SYNOPSIS | |
804 | boolean bfd_generic_relax_section | |
805 | (bfd *abfd, | |
806 | asection *section, | |
5022aea5 | 807 | asymbol **symbols); |
d58b7049 SC |
808 | |
809 | DESCRIPTION | |
810 | Provides default handling for relaxing for back ends which | |
8070f29d | 811 | don't do relaxing -- i.e., does nothing. |
d58b7049 SC |
812 | */ |
813 | ||
814 | boolean | |
5022aea5 | 815 | DEFUN(bfd_generic_relax_section,(abfd, section, symbols), |
d58b7049 SC |
816 | bfd *abfd AND |
817 | asection *section AND | |
5022aea5 | 818 | asymbol **symbols) |
d58b7049 SC |
819 | { |
820 | ||
821 | return false; | |
822 | ||
823 | } | |
e98e6ec1 | 824 | |
d58b7049 | 825 | |
e98e6ec1 SC |
826 | /* |
827 | INTERNAL_FUNCTION | |
828 | bfd_generic_get_relocated_section_contents | |
829 | ||
830 | SYNOPSIS | |
831 | bfd_byte * | |
832 | bfd_generic_get_relocated_section_contents(bfd *abfd, | |
8070f29d KR |
833 | struct bfd_seclet_struct *seclet, |
834 | bfd_byte *data) | |
e98e6ec1 SC |
835 | |
836 | DESCRIPTION | |
837 | Provides default handling of relocation effort for back ends | |
838 | which can't be bothered to do it efficiently. | |
839 | ||
840 | */ | |
841 | ||
842 | bfd_byte * | |
8070f29d | 843 | DEFUN(bfd_generic_get_relocated_section_contents,(abfd, seclet, data), |
e98e6ec1 | 844 | bfd *abfd AND |
8070f29d KR |
845 | struct bfd_seclet_struct *seclet AND |
846 | bfd_byte *data) | |
e98e6ec1 SC |
847 | { |
848 | extern bfd_error_vector_type bfd_error_vector; | |
e98e6ec1 SC |
849 | |
850 | /* Get enough memory to hold the stuff */ | |
851 | bfd *input_bfd = seclet->u.indirect.section->owner; | |
852 | asection *input_section = seclet->u.indirect.section; | |
853 | ||
8070f29d | 854 | |
e98e6ec1 SC |
855 | |
856 | bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd, | |
857 | input_section); | |
8070f29d | 858 | arelent **reloc_vector = (arelent **) alloca(reloc_size); |
e98e6ec1 SC |
859 | |
860 | /* read in the section */ | |
861 | bfd_get_section_contents(input_bfd, | |
862 | input_section, | |
863 | data, | |
864 | 0, | |
865 | input_section->_raw_size); | |
866 | ||
867 | /* We're not relaxing the section, so just copy the size info */ | |
868 | input_section->_cooked_size = input_section->_raw_size; | |
869 | input_section->reloc_done = true; | |
870 | ||
871 | ||
872 | if (bfd_canonicalize_reloc(input_bfd, | |
873 | input_section, | |
874 | reloc_vector, | |
875 | seclet->u.indirect.symbols) ) | |
876 | { | |
877 | arelent **parent; | |
878 | for (parent = reloc_vector; * parent != (arelent *)NULL; | |
879 | parent++) | |
880 | { | |
881 | bfd_reloc_status_type r= | |
882 | bfd_perform_relocation(input_bfd, | |
883 | *parent, | |
884 | data, | |
885 | input_section, 0); | |
886 | ||
887 | ||
888 | if (r != bfd_reloc_ok) | |
889 | { | |
e98e6ec1 SC |
890 | switch (r) |
891 | { | |
892 | case bfd_reloc_undefined: | |
893 | bfd_error_vector.undefined_symbol(*parent, seclet); | |
894 | break; | |
895 | case bfd_reloc_dangerous: | |
896 | bfd_error_vector.reloc_dangerous(*parent, seclet); | |
897 | break; | |
898 | case bfd_reloc_outofrange: | |
899 | case bfd_reloc_overflow: | |
900 | bfd_error_vector.reloc_value_truncated(*parent, seclet); | |
901 | break; | |
902 | default: | |
903 | abort(); | |
904 | break; | |
905 | } | |
906 | ||
907 | } | |
908 | } | |
909 | } | |
910 | ||
8070f29d | 911 | |
e98e6ec1 SC |
912 | return data; |
913 | ||
914 | ||
915 | } | |
916 |