1 /* BFD back-end for AMD 29000 COFF binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1997, 1999, 2000, 2001,
4 Free Software Foundation, Inc.
5 Contributed by David Wood at New York University 7/8/91.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include "coff/a29k.h"
29 #include "coff/internal.h"
32 static long get_symbol_value
PARAMS ((asymbol
*));
33 static bfd_reloc_status_type a29k_reloc
34 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
35 static bfd_boolean coff_a29k_relocate_section
36 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
37 struct internal_reloc
*, struct internal_syment
*, asection
**));
38 static bfd_boolean coff_a29k_adjust_symndx
39 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
40 struct internal_reloc
*, bfd_boolean
*));
41 static void reloc_processing
42 PARAMS ((arelent
*, struct internal_reloc
*, asymbol
**, bfd
*, asection
*));
44 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
46 #define INSERT_HWORD(WORD,HWORD) \
47 (((WORD) & 0xff00ff00) | (((HWORD) & 0xff00) << 8) | ((HWORD)& 0xff))
48 #define EXTRACT_HWORD(WORD) \
49 ((((WORD) & 0x00ff0000) >> 8) | ((WORD) & 0xff))
50 #define SIGN_EXTEND_HWORD(HWORD) \
51 (((HWORD) ^ 0x8000) - 0x8000)
53 /* Provided the symbol, returns the value reffed. */
56 get_symbol_value (symbol
)
61 if (bfd_is_com_section (symbol
->section
))
64 relocation
= symbol
->value
+
65 symbol
->section
->output_section
->vma
+
66 symbol
->section
->output_offset
;
71 /* This function is in charge of performing all the 29k relocations. */
73 static bfd_reloc_status_type
74 a29k_reloc (abfd
, reloc_entry
, symbol_in
, data
, input_section
, output_bfd
,
80 asection
*input_section
;
84 /* The consth relocation comes in two parts, we have to remember
85 the state between calls, in these variables. */
86 static bfd_boolean part1_consth_active
= FALSE
;
87 static unsigned long part1_consth_value
;
89 unsigned long sym_value
;
90 unsigned long unsigned_value
;
91 unsigned short r_type
;
93 unsigned long addr
= reloc_entry
->address
; /*+ input_section->vma*/
94 bfd_byte
*hit_data
=addr
+ (bfd_byte
*) (data
);
96 r_type
= reloc_entry
->howto
->type
;
100 /* Partial linking - do nothing. */
101 reloc_entry
->address
+= input_section
->output_offset
;
105 if (symbol_in
!= NULL
106 && bfd_is_und_section (symbol_in
->section
))
108 /* Keep the state machine happy in case we're called again. */
109 if (r_type
== R_IHIHALF
)
111 part1_consth_active
= TRUE
;
112 part1_consth_value
= 0;
114 return bfd_reloc_undefined
;
117 if ((part1_consth_active
) && (r_type
!= R_IHCONST
))
119 part1_consth_active
= FALSE
;
120 *error_message
= (char *) _("Missing IHCONST");
122 return bfd_reloc_dangerous
;
125 sym_value
= get_symbol_value(symbol_in
);
130 insn
= bfd_get_32 (abfd
, hit_data
);
131 /* Take the value in the field and sign extend it. */
132 signed_value
= EXTRACT_HWORD(insn
);
133 signed_value
= SIGN_EXTEND_HWORD(signed_value
);
136 /* See the note on the R_IREL reloc in coff_a29k_relocate_section. */
137 if (signed_value
== - (long) reloc_entry
->address
)
140 signed_value
+= sym_value
+ reloc_entry
->addend
;
141 if ((signed_value
& ~0x3ffff) == 0)
142 { /* Absolute jmp/call */
143 insn
|= (1 << 24); /* Make it absolute */
144 /* FIXME: Should we change r_type to R_IABS. */
148 /* Relative jmp/call, so subtract from the value the
149 address of the place we're coming from. */
150 signed_value
-= (reloc_entry
->address
151 + input_section
->output_section
->vma
152 + input_section
->output_offset
);
153 if (signed_value
> 0x1ffff || signed_value
< -0x20000)
154 return bfd_reloc_overflow
;
157 insn
= INSERT_HWORD (insn
, signed_value
);
158 bfd_put_32 (abfd
, (bfd_vma
) insn
,hit_data
);
161 insn
= bfd_get_32 (abfd
, hit_data
);
162 unsigned_value
= EXTRACT_HWORD(insn
);
163 unsigned_value
+= sym_value
+ reloc_entry
->addend
;
164 insn
= INSERT_HWORD(insn
, unsigned_value
);
165 bfd_put_32 (abfd
, (bfd_vma
) insn
, hit_data
);
168 insn
= bfd_get_32 (abfd
, hit_data
);
170 Just get the symbol value that is referenced. */
171 part1_consth_active
= TRUE
;
172 part1_consth_value
= sym_value
+ reloc_entry
->addend
;
173 /* Don't modify insn until R_IHCONST. */
176 insn
= bfd_get_32 (abfd
, hit_data
);
178 Now relocate the reference. */
179 if (! part1_consth_active
)
181 *error_message
= (char *) _("Missing IHIHALF");
182 return bfd_reloc_dangerous
;
184 /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
185 unsigned_value
= 0; /*EXTRACT_HWORD(insn) << 16;*/
186 unsigned_value
+= reloc_entry
->addend
; /* r_symndx */
187 unsigned_value
+= part1_consth_value
;
188 unsigned_value
= unsigned_value
>> 16;
189 insn
= INSERT_HWORD(insn
, unsigned_value
);
190 part1_consth_active
= FALSE
;
191 bfd_put_32 (abfd
, (bfd_vma
) insn
, hit_data
);
194 insn
= bfd_get_8 (abfd
, hit_data
);
195 unsigned_value
= insn
+ sym_value
+ reloc_entry
->addend
;
196 if (unsigned_value
& 0xffffff00)
197 return bfd_reloc_overflow
;
198 bfd_put_8 (abfd
, unsigned_value
, hit_data
);
201 insn
= bfd_get_16 (abfd
, hit_data
);
202 unsigned_value
= insn
+ sym_value
+ reloc_entry
->addend
;
203 if (unsigned_value
& 0xffff0000)
204 return bfd_reloc_overflow
;
205 bfd_put_16 (abfd
, (bfd_vma
) insn
, hit_data
);
208 insn
= bfd_get_32 (abfd
, hit_data
);
209 insn
+= sym_value
+ reloc_entry
->addend
;
210 bfd_put_32 (abfd
, (bfd_vma
) insn
, hit_data
);
213 *error_message
= _("Unrecognized reloc");
214 return bfd_reloc_dangerous
;
217 return(bfd_reloc_ok
);
220 /*FIXME: I'm not real sure about this table. */
221 static reloc_howto_type howto_table
[] =
223 {R_ABS
, 0, 3, 32, FALSE
, 0, complain_overflow_bitfield
,a29k_reloc
,"ABS", TRUE
, 0xffffffff,0xffffffff, FALSE
},
247 {R_IREL
, 0, 3, 32, TRUE
, 0, complain_overflow_signed
,a29k_reloc
,"IREL", TRUE
, 0xffffffff,0xffffffff, FALSE
},
248 {R_IABS
, 0, 3, 32, FALSE
, 0, complain_overflow_bitfield
, a29k_reloc
,"IABS", TRUE
, 0xffffffff,0xffffffff, FALSE
},
249 {R_ILOHALF
, 0, 3, 16, TRUE
, 0, complain_overflow_signed
, a29k_reloc
,"ILOHALF", TRUE
, 0x0000ffff,0x0000ffff, FALSE
},
250 {R_IHIHALF
, 0, 3, 16, TRUE
, 16, complain_overflow_signed
, a29k_reloc
,"IHIHALF", TRUE
, 0xffff0000,0xffff0000, FALSE
},
251 {R_IHCONST
, 0, 3, 16, TRUE
, 0, complain_overflow_signed
, a29k_reloc
,"IHCONST", TRUE
, 0xffff0000,0xffff0000, FALSE
},
252 {R_BYTE
, 0, 0, 8, FALSE
, 0, complain_overflow_bitfield
, a29k_reloc
,"BYTE", TRUE
, 0x000000ff,0x000000ff, FALSE
},
253 {R_HWORD
, 0, 1, 16, FALSE
, 0, complain_overflow_bitfield
, a29k_reloc
,"HWORD", TRUE
, 0x0000ffff,0x0000ffff, FALSE
},
254 {R_WORD
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
, a29k_reloc
,"WORD", TRUE
, 0xffffffff,0xffffffff, FALSE
},
257 #define BADMAG(x) A29KBADMAG(x)
259 #define RELOC_PROCESSING(relent, reloc, symbols, abfd, section) \
260 reloc_processing(relent, reloc, symbols, abfd, section)
263 reloc_processing (relent
,reloc
, symbols
, abfd
, section
)
265 struct internal_reloc
*reloc
;
270 static bfd_vma ihihalf_vaddr
= (bfd_vma
) -1;
272 relent
->address
= reloc
->r_vaddr
;
273 relent
->howto
= howto_table
+ reloc
->r_type
;
274 if (reloc
->r_type
== R_IHCONST
)
276 /* The address of an R_IHCONST should always be the address of
277 the immediately preceding R_IHIHALF. relocs generated by gas
278 are correct, but relocs generated by High C are different (I
279 can't figure out what the address means for High C). We can
280 handle both gas and High C by ignoring the address here, and
281 simply reusing the address saved for R_IHIHALF. */
282 if (ihihalf_vaddr
== (bfd_vma
) -1)
284 relent
->address
= ihihalf_vaddr
;
285 ihihalf_vaddr
= (bfd_vma
) -1;
286 relent
->addend
= reloc
->r_symndx
;
287 relent
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
293 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
295 ptr
= *(relent
->sym_ptr_ptr
);
298 && bfd_asymbol_bfd(ptr
) == abfd
299 && ((ptr
->flags
& BSF_OLD_COMMON
) == 0))
304 relent
->address
-= section
->vma
;
305 if (reloc
->r_type
== R_IHIHALF
)
306 ihihalf_vaddr
= relent
->address
;
307 else if (ihihalf_vaddr
!= (bfd_vma
) -1)
312 /* The reloc processing routine for the optimized COFF linker. */
315 coff_a29k_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
316 contents
, relocs
, syms
, sections
)
317 bfd
*output_bfd ATTRIBUTE_UNUSED
;
318 struct bfd_link_info
*info
;
320 asection
*input_section
;
322 struct internal_reloc
*relocs
;
323 struct internal_syment
*syms
;
326 struct internal_reloc
*rel
;
327 struct internal_reloc
*relend
;
331 /* If we are performing a relocatable link, we don't need to do a
332 thing. The caller will take care of adjusting the reloc
333 addresses and symbol indices. */
334 if (info
->relocatable
)
341 relend
= rel
+ input_section
->reloc_count
;
342 for (; rel
< relend
; rel
++)
346 struct coff_link_hash_entry
*h
;
347 struct internal_syment
*sym
;
350 bfd_boolean overflow
;
353 unsigned long unsigned_value
;
354 bfd_reloc_status_type rstat
;
356 symndx
= rel
->r_symndx
;
357 loc
= contents
+ rel
->r_vaddr
- input_section
->vma
;
359 if (symndx
== -1 || rel
->r_type
== R_IHCONST
)
362 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
368 /* An R_IHCONST reloc does not have a symbol. Instead, the
369 symbol index is an addend. R_IHCONST is always used in
370 conjunction with R_IHHALF. */
371 if (rel
->r_type
!= R_IHCONST
)
376 sec
= bfd_abs_section_ptr
;
380 sec
= sections
[symndx
];
381 val
= (sec
->output_section
->vma
389 if ( h
->root
.type
== bfd_link_hash_defined
390 || h
->root
.type
== bfd_link_hash_defweak
)
392 sec
= h
->root
.u
.def
.section
;
393 val
= (h
->root
.u
.def
.value
394 + sec
->output_section
->vma
395 + sec
->output_offset
);
399 if (! ((*info
->callbacks
->undefined_symbol
)
400 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
401 rel
->r_vaddr
- input_section
->vma
, TRUE
)))
408 if (! ((*info
->callbacks
->reloc_dangerous
)
409 (info
, _("missing IHCONST reloc"), input_bfd
,
410 input_section
, rel
->r_vaddr
- input_section
->vma
)))
421 bfd_set_error (bfd_error_bad_value
);
425 insn
= bfd_get_32 (input_bfd
, loc
);
427 /* Extract the addend. */
428 signed_value
= EXTRACT_HWORD (insn
);
429 signed_value
= SIGN_EXTEND_HWORD (signed_value
);
432 /* Unfortunately, there are two different versions of COFF
433 a29k. In the original AMD version, the value stored in
434 the field for the R_IREL reloc is a simple addend. In
435 the GNU version, the value is the negative of the address
436 of the reloc within section. We try to cope here by
437 assuming the AMD version, unless the addend is exactly
438 the negative of the address; in the latter case we assume
439 the GNU version. This means that something like
443 will fail, because the addend of -4 will happen to equal
444 the negative of the address within the section. The
445 compiler will never generate code like this.
447 At some point in the future we may want to take out this
450 if (signed_value
== - (long) (rel
->r_vaddr
- input_section
->vma
))
453 /* Determine the destination of the jump. */
456 if ((signed_value
& ~0x3ffff) == 0)
458 /* We can use an absolute jump. */
463 /* Make the destination PC relative. */
464 signed_value
-= (input_section
->output_section
->vma
465 + input_section
->output_offset
466 + (rel
->r_vaddr
- input_section
->vma
));
467 if (signed_value
> 0x1ffff || signed_value
< - 0x20000)
474 /* Put the adjusted value back into the instruction. */
476 insn
= INSERT_HWORD (insn
, signed_value
);
478 bfd_put_32 (input_bfd
, (bfd_vma
) insn
, loc
);
482 insn
= bfd_get_32 (input_bfd
, loc
);
483 unsigned_value
= EXTRACT_HWORD (insn
);
484 unsigned_value
+= val
;
485 insn
= INSERT_HWORD (insn
, unsigned_value
);
486 bfd_put_32 (input_bfd
, (bfd_vma
) insn
, loc
);
490 /* Save the value for the R_IHCONST reloc. */
498 if (! ((*info
->callbacks
->reloc_dangerous
)
499 (info
, _("missing IHIHALF reloc"), input_bfd
,
500 input_section
, rel
->r_vaddr
- input_section
->vma
)))
505 insn
= bfd_get_32 (input_bfd
, loc
);
506 unsigned_value
= rel
->r_symndx
+ hihalf_val
;
507 unsigned_value
>>= 16;
508 insn
= INSERT_HWORD (insn
, unsigned_value
);
509 bfd_put_32 (input_bfd
, (bfd_vma
) insn
, loc
);
518 rstat
= _bfd_relocate_contents (howto_table
+ rel
->r_type
,
519 input_bfd
, val
, loc
);
520 if (rstat
== bfd_reloc_overflow
)
522 else if (rstat
!= bfd_reloc_ok
)
530 char buf
[SYMNMLEN
+ 1];
535 name
= h
->root
.root
.string
;
536 else if (sym
== NULL
)
538 else if (sym
->_n
._n_n
._n_zeroes
== 0
539 && sym
->_n
._n_n
._n_offset
!= 0)
540 name
= obj_coff_strings (input_bfd
) + sym
->_n
._n_n
._n_offset
;
543 strncpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
544 buf
[SYMNMLEN
] = '\0';
548 if (! ((*info
->callbacks
->reloc_overflow
)
549 (info
, name
, howto_table
[rel
->r_type
].name
, (bfd_vma
) 0,
550 input_bfd
, input_section
,
551 rel
->r_vaddr
- input_section
->vma
)))
559 #define coff_relocate_section coff_a29k_relocate_section
561 /* We don't want to change the symndx of a R_IHCONST reloc, since it
562 is actually an addend, not a symbol index at all. */
565 coff_a29k_adjust_symndx (obfd
, info
, ibfd
, sec
, irel
, adjustedp
)
566 bfd
*obfd ATTRIBUTE_UNUSED
;
567 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
568 bfd
*ibfd ATTRIBUTE_UNUSED
;
569 asection
*sec ATTRIBUTE_UNUSED
;
570 struct internal_reloc
*irel
;
571 bfd_boolean
*adjustedp
;
573 if (irel
->r_type
== R_IHCONST
)
580 #define coff_adjust_symndx coff_a29k_adjust_symndx
582 #include "coffcode.h"
584 CREATE_BIG_COFF_TARGET_VEC (a29kcoff_big_vec
, "coff-a29k-big", 0, SEC_READONLY
, '_', NULL
, COFF_SWAP_TABLE
)