]>
Commit | Line | Data |
---|---|---|
c2dcd04e | 1 | /* BFD back-end for Renesas H8/300 COFF binaries. |
7898deda | 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
ca9a79a1 | 3 | 2000, 2001, 2002, 2003, 2004 |
5f771d47 | 4 | Free Software Foundation, Inc. |
252b5132 RH |
5 | Written by Steve Chamberlain, <[email protected]>. |
6 | ||
e514ac71 | 7 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 8 | |
e514ac71 NC |
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. | |
252b5132 | 13 | |
e514ac71 NC |
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. | |
252b5132 | 18 | |
e514ac71 NC |
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. */ | |
252b5132 RH |
22 | |
23 | #include "bfd.h" | |
24 | #include "sysdep.h" | |
25 | #include "libbfd.h" | |
26 | #include "bfdlink.h" | |
27 | #include "genlink.h" | |
28 | #include "coff/h8300.h" | |
29 | #include "coff/internal.h" | |
30 | #include "libcoff.h" | |
0171ee92 | 31 | #include "libiberty.h" |
252b5132 RH |
32 | |
33 | #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1) | |
34 | ||
35 | /* We derive a hash table from the basic BFD hash table to | |
5fcfd273 | 36 | hold entries in the function vector. Aside from the |
252b5132 RH |
37 | info stored by the basic hash table, we need the offset |
38 | of a particular entry within the hash table as well as | |
39 | the offset where we'll add the next entry. */ | |
40 | ||
41 | struct funcvec_hash_entry | |
f4ffd778 NC |
42 | { |
43 | /* The basic hash table entry. */ | |
44 | struct bfd_hash_entry root; | |
252b5132 | 45 | |
f4ffd778 NC |
46 | /* The offset within the vectors section where |
47 | this entry lives. */ | |
48 | bfd_vma offset; | |
49 | }; | |
252b5132 RH |
50 | |
51 | struct funcvec_hash_table | |
f4ffd778 NC |
52 | { |
53 | /* The basic hash table. */ | |
54 | struct bfd_hash_table root; | |
252b5132 | 55 | |
f4ffd778 | 56 | bfd *abfd; |
252b5132 | 57 | |
f4ffd778 NC |
58 | /* Offset at which we'll add the next entry. */ |
59 | unsigned int offset; | |
60 | }; | |
252b5132 RH |
61 | |
62 | static struct bfd_hash_entry * | |
63 | funcvec_hash_newfunc | |
c6baf75e | 64 | (struct bfd_hash_entry *, struct bfd_hash_table *, const char *); |
252b5132 | 65 | |
b34976b6 | 66 | static bfd_boolean |
252b5132 | 67 | funcvec_hash_table_init |
c6baf75e RS |
68 | (struct funcvec_hash_table *, bfd *, |
69 | struct bfd_hash_entry *(*) (struct bfd_hash_entry *, | |
70 | struct bfd_hash_table *, | |
71 | const char *)); | |
b34976b6 AM |
72 | |
73 | static bfd_reloc_status_type special | |
c6baf75e | 74 | (bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **); |
b34976b6 | 75 | static int select_reloc |
c6baf75e | 76 | (reloc_howto_type *); |
b34976b6 | 77 | static void rtype2howto |
c6baf75e | 78 | (arelent *, struct internal_reloc *); |
b34976b6 | 79 | static void reloc_processing |
c6baf75e | 80 | (arelent *, struct internal_reloc *, asymbol **, bfd *, asection *); |
b34976b6 | 81 | static bfd_boolean h8300_symbol_address_p |
c6baf75e | 82 | (bfd *, asection *, bfd_vma); |
b34976b6 | 83 | static int h8300_reloc16_estimate |
c6baf75e RS |
84 | (bfd *, asection *, arelent *, unsigned int, |
85 | struct bfd_link_info *); | |
b34976b6 | 86 | static void h8300_reloc16_extra_cases |
c6baf75e RS |
87 | (bfd *, struct bfd_link_info *, struct bfd_link_order *, arelent *, |
88 | bfd_byte *, unsigned int *, unsigned int *); | |
b34976b6 | 89 | static bfd_boolean h8300_bfd_link_add_symbols |
c6baf75e | 90 | (bfd *, struct bfd_link_info *); |
f4ffd778 | 91 | |
252b5132 RH |
92 | /* To lookup a value in the function vector hash table. */ |
93 | #define funcvec_hash_lookup(table, string, create, copy) \ | |
94 | ((struct funcvec_hash_entry *) \ | |
95 | bfd_hash_lookup (&(table)->root, (string), (create), (copy))) | |
96 | ||
97 | /* The derived h8300 COFF linker table. Note it's derived from | |
98 | the generic linker hash table, not the COFF backend linker hash | |
99 | table! We use this to attach additional data structures we | |
100 | need while linking on the h8300. */ | |
bc7eab72 | 101 | struct h8300_coff_link_hash_table { |
252b5132 RH |
102 | /* The main hash table. */ |
103 | struct generic_link_hash_table root; | |
104 | ||
105 | /* Section for the vectors table. This gets attached to a | |
106 | random input bfd, we keep it here for easy access. */ | |
107 | asection *vectors_sec; | |
108 | ||
109 | /* Hash table of the functions we need to enter into the function | |
110 | vector. */ | |
111 | struct funcvec_hash_table *funcvec_hash_table; | |
112 | }; | |
113 | ||
c6baf75e | 114 | static struct bfd_link_hash_table *h8300_coff_link_hash_table_create (bfd *); |
252b5132 RH |
115 | |
116 | /* Get the H8/300 COFF linker hash table from a link_info structure. */ | |
117 | ||
118 | #define h8300_coff_hash_table(p) \ | |
119 | ((struct h8300_coff_link_hash_table *) ((coff_hash_table (p)))) | |
120 | ||
121 | /* Initialize fields within a funcvec hash table entry. Called whenever | |
122 | a new entry is added to the funcvec hash table. */ | |
123 | ||
124 | static struct bfd_hash_entry * | |
c6baf75e RS |
125 | funcvec_hash_newfunc (struct bfd_hash_entry *entry, |
126 | struct bfd_hash_table *gen_table, | |
127 | const char *string) | |
252b5132 RH |
128 | { |
129 | struct funcvec_hash_entry *ret; | |
130 | struct funcvec_hash_table *table; | |
131 | ||
132 | ret = (struct funcvec_hash_entry *) entry; | |
133 | table = (struct funcvec_hash_table *) gen_table; | |
134 | ||
135 | /* Allocate the structure if it has not already been allocated by a | |
136 | subclass. */ | |
137 | if (ret == NULL) | |
138 | ret = ((struct funcvec_hash_entry *) | |
0171ee92 AM |
139 | bfd_hash_allocate (gen_table, |
140 | sizeof (struct funcvec_hash_entry))); | |
252b5132 RH |
141 | if (ret == NULL) |
142 | return NULL; | |
143 | ||
144 | /* Call the allocation method of the superclass. */ | |
145 | ret = ((struct funcvec_hash_entry *) | |
bc7eab72 | 146 | bfd_hash_newfunc ((struct bfd_hash_entry *) ret, gen_table, string)); |
252b5132 RH |
147 | |
148 | if (ret == NULL) | |
149 | return NULL; | |
150 | ||
151 | /* Note where this entry will reside in the function vector table. */ | |
152 | ret->offset = table->offset; | |
153 | ||
154 | /* Bump the offset at which we store entries in the function | |
155 | vector. We'd like to bump up the size of the vectors section, | |
156 | but it's not easily available here. */ | |
d4e2de6b NC |
157 | switch (bfd_get_mach (table->abfd)) |
158 | { | |
159 | case bfd_mach_h8300: | |
160 | case bfd_mach_h8300hn: | |
161 | case bfd_mach_h8300sn: | |
162 | table->offset += 2; | |
163 | break; | |
164 | case bfd_mach_h8300h: | |
165 | case bfd_mach_h8300s: | |
166 | table->offset += 4; | |
167 | break; | |
168 | default: | |
169 | return NULL; | |
170 | } | |
252b5132 RH |
171 | |
172 | /* Everything went OK. */ | |
173 | return (struct bfd_hash_entry *) ret; | |
174 | } | |
175 | ||
176 | /* Initialize the function vector hash table. */ | |
177 | ||
b34976b6 | 178 | static bfd_boolean |
c6baf75e RS |
179 | funcvec_hash_table_init (struct funcvec_hash_table *table, |
180 | bfd *abfd, | |
181 | struct bfd_hash_entry *(*newfunc) | |
182 | (struct bfd_hash_entry *, | |
183 | struct bfd_hash_table *, | |
184 | const char *)) | |
252b5132 RH |
185 | { |
186 | /* Initialize our local fields, then call the generic initialization | |
187 | routine. */ | |
188 | table->offset = 0; | |
189 | table->abfd = abfd; | |
190 | return (bfd_hash_table_init (&table->root, newfunc)); | |
191 | } | |
192 | ||
193 | /* Create the derived linker hash table. We use a derived hash table | |
19852a2a | 194 | basically to hold "static" information during an H8/300 coff link |
252b5132 RH |
195 | without using static variables. */ |
196 | ||
197 | static struct bfd_link_hash_table * | |
c6baf75e | 198 | h8300_coff_link_hash_table_create (bfd *abfd) |
252b5132 RH |
199 | { |
200 | struct h8300_coff_link_hash_table *ret; | |
dc810e39 AM |
201 | bfd_size_type amt = sizeof (struct h8300_coff_link_hash_table); |
202 | ||
e2d34d7d | 203 | ret = (struct h8300_coff_link_hash_table *) bfd_malloc (amt); |
252b5132 RH |
204 | if (ret == NULL) |
205 | return NULL; | |
dc810e39 AM |
206 | if (!_bfd_link_hash_table_init (&ret->root.root, abfd, |
207 | _bfd_generic_link_hash_newfunc)) | |
252b5132 | 208 | { |
e2d34d7d | 209 | free (ret); |
252b5132 RH |
210 | return NULL; |
211 | } | |
212 | ||
213 | /* Initialize our data. */ | |
214 | ret->vectors_sec = NULL; | |
215 | ret->funcvec_hash_table = NULL; | |
216 | ||
2ab1486e | 217 | /* OK. Everything's initialized, return the base pointer. */ |
252b5132 RH |
218 | return &ret->root.root; |
219 | } | |
220 | ||
cc040812 | 221 | /* Special handling for H8/300 relocs. |
252b5132 RH |
222 | We only come here for pcrel stuff and return normally if not an -r link. |
223 | When doing -r, we can't do any arithmetic for the pcrel stuff, because | |
224 | the code in reloc.c assumes that we can manipulate the targets of | |
5fcfd273 | 225 | the pcrel branches. This isn't so, since the H8/300 can do relaxing, |
252b5132 | 226 | which means that the gap after the instruction may not be enough to |
d562d2fb | 227 | contain the offset required for the branch, so we have to use only |
cc040812 | 228 | the addend until the final link. */ |
252b5132 RH |
229 | |
230 | static bfd_reloc_status_type | |
c6baf75e RS |
231 | special (bfd *abfd ATTRIBUTE_UNUSED, |
232 | arelent *reloc_entry ATTRIBUTE_UNUSED, | |
233 | asymbol *symbol ATTRIBUTE_UNUSED, | |
234 | PTR data ATTRIBUTE_UNUSED, | |
235 | asection *input_section ATTRIBUTE_UNUSED, | |
236 | bfd *output_bfd, | |
237 | char **error_message ATTRIBUTE_UNUSED) | |
252b5132 RH |
238 | { |
239 | if (output_bfd == (bfd *) NULL) | |
240 | return bfd_reloc_continue; | |
241 | ||
d562d2fb AM |
242 | /* Adjust the reloc address to that in the output section. */ |
243 | reloc_entry->address += input_section->output_offset; | |
252b5132 RH |
244 | return bfd_reloc_ok; |
245 | } | |
246 | ||
bc7eab72 | 247 | static reloc_howto_type howto_table[] = { |
b34976b6 AM |
248 | HOWTO (R_RELBYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "8", FALSE, 0x000000ff, 0x000000ff, FALSE), |
249 | HOWTO (R_RELWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "16", FALSE, 0x0000ffff, 0x0000ffff, FALSE), | |
250 | HOWTO (R_RELLONG, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, special, "32", FALSE, 0xffffffff, 0xffffffff, FALSE), | |
251 | HOWTO (R_PCRBYTE, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "DISP8", FALSE, 0x000000ff, 0x000000ff, TRUE), | |
252 | HOWTO (R_PCRWORD, 0, 1, 16, TRUE, 0, complain_overflow_signed, special, "DISP16", FALSE, 0x0000ffff, 0x0000ffff, TRUE), | |
253 | HOWTO (R_PCRLONG, 0, 2, 32, TRUE, 0, complain_overflow_signed, special, "DISP32", FALSE, 0xffffffff, 0xffffffff, TRUE), | |
254 | HOWTO (R_MOV16B1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "relaxable mov.b:16", FALSE, 0x0000ffff, 0x0000ffff, FALSE), | |
255 | HOWTO (R_MOV16B2, 0, 1, 8, FALSE, 0, complain_overflow_bitfield, special, "relaxed mov.b:16", FALSE, 0x000000ff, 0x000000ff, FALSE), | |
256 | HOWTO (R_JMP1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "16/pcrel", FALSE, 0x0000ffff, 0x0000ffff, FALSE), | |
257 | HOWTO (R_JMP2, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "pcrecl/16", FALSE, 0x000000ff, 0x000000ff, FALSE), | |
258 | HOWTO (R_JMPL1, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, special, "24/pcrell", FALSE, 0x00ffffff, 0x00ffffff, FALSE), | |
259 | HOWTO (R_JMPL2, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "pc8/24", FALSE, 0x000000ff, 0x000000ff, FALSE), | |
260 | HOWTO (R_MOV24B1, 0, 1, 32, FALSE, 0, complain_overflow_bitfield, special, "relaxable mov.b:24", FALSE, 0xffffffff, 0xffffffff, FALSE), | |
261 | HOWTO (R_MOV24B2, 0, 1, 8, FALSE, 0, complain_overflow_bitfield, special, "relaxed mov.b:24", FALSE, 0x0000ffff, 0x0000ffff, FALSE), | |
252b5132 RH |
262 | |
263 | /* An indirect reference to a function. This causes the function's address | |
264 | to be added to the function vector in lo-mem and puts the address of | |
265 | the function vector's entry in the jsr instruction. */ | |
b34976b6 | 266 | HOWTO (R_MEM_INDIRECT, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "8/indirect", FALSE, 0x000000ff, 0x000000ff, FALSE), |
252b5132 | 267 | |
e804e836 KH |
268 | /* Internal reloc for relaxing. This is created when a 16-bit pc-relative |
269 | branch is turned into an 8-bit pc-relative branch. */ | |
b34976b6 | 270 | HOWTO (R_PCRWORD_B, 0, 0, 8, TRUE, 0, complain_overflow_bitfield, special, "relaxed bCC:16", FALSE, 0x000000ff, 0x000000ff, FALSE), |
252b5132 | 271 | |
b34976b6 | 272 | HOWTO (R_MOVL1, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,special, "32/24 relaxable move", FALSE, 0xffffffff, 0xffffffff, FALSE), |
252b5132 | 273 | |
b34976b6 | 274 | HOWTO (R_MOVL2, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "32/24 relaxed move", FALSE, 0x0000ffff, 0x0000ffff, FALSE), |
252b5132 | 275 | |
b34976b6 | 276 | HOWTO (R_BCC_INV, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "DISP8 inverted", FALSE, 0x000000ff, 0x000000ff, TRUE), |
252b5132 | 277 | |
b34976b6 | 278 | HOWTO (R_JMP_DEL, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "Deleted jump", FALSE, 0x000000ff, 0x000000ff, TRUE), |
252b5132 RH |
279 | }; |
280 | ||
cc040812 | 281 | /* Turn a howto into a reloc number. */ |
252b5132 RH |
282 | |
283 | #define SELECT_RELOC(x,howto) \ | |
bc7eab72 | 284 | { x.r_type = select_reloc (howto); } |
252b5132 | 285 | |
8d9cd6b1 NC |
286 | #define BADMAG(x) (H8300BADMAG (x) && H8300HBADMAG (x) && H8300SBADMAG (x) \ |
287 | && H8300HNBADMAG(x) && H8300SNBADMAG(x)) | |
288 | #define H8300 1 /* Customize coffcode.h */ | |
252b5132 RH |
289 | #define __A_MAGIC_SET__ |
290 | ||
cc040812 | 291 | /* Code to swap in the reloc. */ |
dc810e39 AM |
292 | #define SWAP_IN_RELOC_OFFSET H_GET_32 |
293 | #define SWAP_OUT_RELOC_OFFSET H_PUT_32 | |
252b5132 RH |
294 | #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \ |
295 | dst->r_stuff[0] = 'S'; \ | |
296 | dst->r_stuff[1] = 'C'; | |
297 | ||
252b5132 | 298 | static int |
c6baf75e | 299 | select_reloc (reloc_howto_type *howto) |
252b5132 RH |
300 | { |
301 | return howto->type; | |
302 | } | |
303 | ||
cc040812 | 304 | /* Code to turn a r_type into a howto ptr, uses the above howto table. */ |
252b5132 RH |
305 | |
306 | static void | |
c6baf75e | 307 | rtype2howto (arelent *internal, struct internal_reloc *dst) |
252b5132 RH |
308 | { |
309 | switch (dst->r_type) | |
310 | { | |
311 | case R_RELBYTE: | |
312 | internal->howto = howto_table + 0; | |
313 | break; | |
314 | case R_RELWORD: | |
315 | internal->howto = howto_table + 1; | |
316 | break; | |
317 | case R_RELLONG: | |
318 | internal->howto = howto_table + 2; | |
319 | break; | |
320 | case R_PCRBYTE: | |
321 | internal->howto = howto_table + 3; | |
322 | break; | |
323 | case R_PCRWORD: | |
324 | internal->howto = howto_table + 4; | |
325 | break; | |
326 | case R_PCRLONG: | |
327 | internal->howto = howto_table + 5; | |
328 | break; | |
329 | case R_MOV16B1: | |
330 | internal->howto = howto_table + 6; | |
331 | break; | |
332 | case R_MOV16B2: | |
333 | internal->howto = howto_table + 7; | |
334 | break; | |
335 | case R_JMP1: | |
336 | internal->howto = howto_table + 8; | |
337 | break; | |
338 | case R_JMP2: | |
339 | internal->howto = howto_table + 9; | |
340 | break; | |
341 | case R_JMPL1: | |
342 | internal->howto = howto_table + 10; | |
343 | break; | |
344 | case R_JMPL2: | |
345 | internal->howto = howto_table + 11; | |
346 | break; | |
347 | case R_MOV24B1: | |
348 | internal->howto = howto_table + 12; | |
349 | break; | |
350 | case R_MOV24B2: | |
351 | internal->howto = howto_table + 13; | |
352 | break; | |
353 | case R_MEM_INDIRECT: | |
354 | internal->howto = howto_table + 14; | |
355 | break; | |
356 | case R_PCRWORD_B: | |
357 | internal->howto = howto_table + 15; | |
358 | break; | |
359 | case R_MOVL1: | |
360 | internal->howto = howto_table + 16; | |
361 | break; | |
362 | case R_MOVL2: | |
363 | internal->howto = howto_table + 17; | |
364 | break; | |
365 | case R_BCC_INV: | |
366 | internal->howto = howto_table + 18; | |
367 | break; | |
368 | case R_JMP_DEL: | |
369 | internal->howto = howto_table + 19; | |
370 | break; | |
371 | default: | |
372 | abort (); | |
373 | break; | |
374 | } | |
375 | } | |
376 | ||
bc7eab72 | 377 | #define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry) |
252b5132 | 378 | |
cc040812 | 379 | /* Perform any necessary magic to the addend in a reloc entry. */ |
252b5132 RH |
380 | |
381 | #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \ | |
bc7eab72 | 382 | cache_ptr->addend = ext_reloc.r_offset; |
252b5132 | 383 | |
252b5132 | 384 | #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \ |
bc7eab72 | 385 | reloc_processing (relent, reloc, symbols, abfd, section) |
252b5132 RH |
386 | |
387 | static void | |
c6baf75e RS |
388 | reloc_processing (arelent *relent, struct internal_reloc *reloc, |
389 | asymbol **symbols, bfd *abfd, asection *section) | |
252b5132 RH |
390 | { |
391 | relent->address = reloc->r_vaddr; | |
392 | rtype2howto (relent, reloc); | |
393 | ||
394 | if (((int) reloc->r_symndx) > 0) | |
2ab1486e | 395 | relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx]; |
252b5132 | 396 | else |
2ab1486e | 397 | relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; |
252b5132 | 398 | |
252b5132 | 399 | relent->addend = reloc->r_offset; |
252b5132 | 400 | relent->address -= section->vma; |
252b5132 RH |
401 | } |
402 | ||
b34976b6 | 403 | static bfd_boolean |
c6baf75e | 404 | h8300_symbol_address_p (bfd *abfd, asection *input_section, bfd_vma address) |
252b5132 RH |
405 | { |
406 | asymbol **s; | |
407 | ||
408 | s = _bfd_generic_link_get_symbols (abfd); | |
409 | BFD_ASSERT (s != (asymbol **) NULL); | |
410 | ||
411 | /* Search all the symbols for one in INPUT_SECTION with | |
412 | address ADDRESS. */ | |
cc040812 | 413 | while (*s) |
252b5132 RH |
414 | { |
415 | asymbol *p = *s; | |
2ab1486e | 416 | |
252b5132 RH |
417 | if (p->section == input_section |
418 | && (input_section->output_section->vma | |
419 | + input_section->output_offset | |
420 | + p->value) == address) | |
b34976b6 | 421 | return TRUE; |
252b5132 | 422 | s++; |
cc040812 | 423 | } |
b34976b6 | 424 | return FALSE; |
252b5132 RH |
425 | } |
426 | ||
252b5132 RH |
427 | /* If RELOC represents a relaxable instruction/reloc, change it into |
428 | the relaxed reloc, notify the linker that symbol addresses | |
429 | have changed (bfd_perform_slip) and return how much the current | |
430 | section has shrunk by. | |
431 | ||
432 | FIXME: Much of this code has knowledge of the ordering of entries | |
433 | in the howto table. This needs to be fixed. */ | |
434 | ||
435 | static int | |
c6baf75e RS |
436 | h8300_reloc16_estimate (bfd *abfd, asection *input_section, arelent *reloc, |
437 | unsigned int shrink, struct bfd_link_info *link_info) | |
252b5132 | 438 | { |
cc040812 | 439 | bfd_vma value; |
252b5132 RH |
440 | bfd_vma dot; |
441 | bfd_vma gap; | |
442 | static asection *last_input_section = NULL; | |
443 | static arelent *last_reloc = NULL; | |
444 | ||
5fcfd273 | 445 | /* The address of the thing to be relocated will have moved back by |
252b5132 RH |
446 | the size of the shrink - but we don't change reloc->address here, |
447 | since we need it to know where the relocation lives in the source | |
448 | uncooked section. */ | |
449 | bfd_vma address = reloc->address - shrink; | |
450 | ||
451 | if (input_section != last_input_section) | |
452 | last_reloc = NULL; | |
453 | ||
454 | /* Only examine the relocs which might be relaxable. */ | |
455 | switch (reloc->howto->type) | |
5fcfd273 | 456 | { |
e804e836 KH |
457 | /* This is the 16-/24-bit absolute branch which could become an |
458 | 8-bit pc-relative branch. */ | |
252b5132 RH |
459 | case R_JMP1: |
460 | case R_JMPL1: | |
461 | /* Get the address of the target of this branch. */ | |
cc040812 | 462 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); |
252b5132 RH |
463 | |
464 | /* Get the address of the next instruction (not the reloc). */ | |
465 | dot = (input_section->output_section->vma | |
466 | + input_section->output_offset + address); | |
467 | ||
468 | /* Adjust for R_JMP1 vs R_JMPL1. */ | |
469 | dot += (reloc->howto->type == R_JMP1 ? 1 : 2); | |
470 | ||
471 | /* Compute the distance from this insn to the branch target. */ | |
472 | gap = value - dot; | |
cc040812 | 473 | |
252b5132 RH |
474 | /* If the distance is within -128..+128 inclusive, then we can relax |
475 | this jump. +128 is valid since the target will move two bytes | |
476 | closer if we do relax this branch. */ | |
bc7eab72 | 477 | if ((int) gap >= -128 && (int) gap <= 128) |
5fcfd273 | 478 | { |
e514ac71 NC |
479 | bfd_byte code; |
480 | ||
481 | if (!bfd_get_section_contents (abfd, input_section, & code, | |
482 | reloc->address, 1)) | |
483 | break; | |
484 | code = bfd_get_8 (abfd, & code); | |
485 | ||
252b5132 RH |
486 | /* It's possible we may be able to eliminate this branch entirely; |
487 | if the previous instruction is a branch around this instruction, | |
488 | and there's no label at this instruction, then we can reverse | |
489 | the condition on the previous branch and eliminate this jump. | |
490 | ||
491 | original: new: | |
492 | bCC lab1 bCC' lab2 | |
493 | jmp lab2 | |
494 | lab1: lab1: | |
5fcfd273 | 495 | |
252b5132 | 496 | This saves 4 bytes instead of two, and should be relatively |
e514ac71 NC |
497 | common. |
498 | ||
499 | Only perform this optimisation for jumps (code 0x5a) not | |
500 | subroutine calls, as otherwise it could transform: | |
b34976b6 | 501 | |
0171ee92 AM |
502 | mov.w r0,r0 |
503 | beq .L1 | |
504 | jsr @_bar | |
505 | .L1: rts | |
506 | _bar: rts | |
e514ac71 | 507 | into: |
0171ee92 AM |
508 | mov.w r0,r0 |
509 | bne _bar | |
510 | rts | |
511 | _bar: rts | |
b34976b6 | 512 | |
e514ac71 NC |
513 | which changes the call (jsr) into a branch (bne). */ |
514 | if (code == 0x5a | |
515 | && gap <= 126 | |
252b5132 RH |
516 | && last_reloc |
517 | && last_reloc->howto->type == R_PCRBYTE) | |
518 | { | |
519 | bfd_vma last_value; | |
520 | last_value = bfd_coff_reloc16_get_value (last_reloc, link_info, | |
521 | input_section) + 1; | |
522 | ||
523 | if (last_value == dot + 2 | |
524 | && last_reloc->address + 1 == reloc->address | |
cc040812 | 525 | && !h8300_symbol_address_p (abfd, input_section, dot - 2)) |
252b5132 RH |
526 | { |
527 | reloc->howto = howto_table + 19; | |
528 | last_reloc->howto = howto_table + 18; | |
529 | last_reloc->sym_ptr_ptr = reloc->sym_ptr_ptr; | |
530 | last_reloc->addend = reloc->addend; | |
531 | shrink += 4; | |
532 | bfd_perform_slip (abfd, 4, input_section, address); | |
533 | break; | |
534 | } | |
535 | } | |
536 | ||
537 | /* Change the reloc type. */ | |
cc040812 | 538 | reloc->howto = reloc->howto + 1; |
252b5132 RH |
539 | |
540 | /* This shrinks this section by two bytes. */ | |
541 | shrink += 2; | |
cc040812 | 542 | bfd_perform_slip (abfd, 2, input_section, address); |
252b5132 RH |
543 | } |
544 | break; | |
545 | ||
e804e836 | 546 | /* This is the 16-bit pc-relative branch which could become an 8-bit |
252b5132 RH |
547 | pc-relative branch. */ |
548 | case R_PCRWORD: | |
549 | /* Get the address of the target of this branch, add one to the value | |
0171ee92 | 550 | because the addend field in PCrel jumps is off by -1. */ |
cc040812 NC |
551 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section) + 1; |
552 | ||
252b5132 RH |
553 | /* Get the address of the next instruction if we were to relax. */ |
554 | dot = input_section->output_section->vma + | |
555 | input_section->output_offset + address; | |
cc040812 | 556 | |
252b5132 RH |
557 | /* Compute the distance from this insn to the branch target. */ |
558 | gap = value - dot; | |
559 | ||
560 | /* If the distance is within -128..+128 inclusive, then we can relax | |
561 | this jump. +128 is valid since the target will move two bytes | |
562 | closer if we do relax this branch. */ | |
bc7eab72 | 563 | if ((int) gap >= -128 && (int) gap <= 128) |
5fcfd273 | 564 | { |
252b5132 RH |
565 | /* Change the reloc type. */ |
566 | reloc->howto = howto_table + 15; | |
567 | ||
568 | /* This shrinks this section by two bytes. */ | |
569 | shrink += 2; | |
cc040812 | 570 | bfd_perform_slip (abfd, 2, input_section, address); |
252b5132 RH |
571 | } |
572 | break; | |
573 | ||
e804e836 KH |
574 | /* This is a 16-bit absolute address in a mov.b insn, which can |
575 | become an 8-bit absolute address if it's in the right range. */ | |
252b5132 RH |
576 | case R_MOV16B1: |
577 | /* Get the address of the data referenced by this mov.b insn. */ | |
cc040812 | 578 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); |
7a9823f1 | 579 | value = bfd_h8300_pad_address (abfd, value); |
252b5132 | 580 | |
7a9823f1 RS |
581 | /* If the address is in the top 256 bytes of the address space |
582 | then we can relax this instruction. */ | |
583 | if (value >= 0xffffff00u) | |
252b5132 RH |
584 | { |
585 | /* Change the reloc type. */ | |
586 | reloc->howto = reloc->howto + 1; | |
587 | ||
588 | /* This shrinks this section by two bytes. */ | |
589 | shrink += 2; | |
cc040812 | 590 | bfd_perform_slip (abfd, 2, input_section, address); |
252b5132 RH |
591 | } |
592 | break; | |
593 | ||
e804e836 KH |
594 | /* Similarly for a 24-bit absolute address in a mov.b. Note that |
595 | if we can't relax this into an 8-bit absolute, we'll fall through | |
596 | and try to relax it into a 16-bit absolute. */ | |
252b5132 RH |
597 | case R_MOV24B1: |
598 | /* Get the address of the data referenced by this mov.b insn. */ | |
cc040812 | 599 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); |
7a9823f1 | 600 | value = bfd_h8300_pad_address (abfd, value); |
252b5132 | 601 | |
7a9823f1 | 602 | if (value >= 0xffffff00u) |
252b5132 RH |
603 | { |
604 | /* Change the reloc type. */ | |
605 | reloc->howto = reloc->howto + 1; | |
606 | ||
607 | /* This shrinks this section by four bytes. */ | |
608 | shrink += 4; | |
cc040812 | 609 | bfd_perform_slip (abfd, 4, input_section, address); |
252b5132 RH |
610 | |
611 | /* Done with this reloc. */ | |
612 | break; | |
613 | } | |
614 | ||
e804e836 | 615 | /* FALLTHROUGH and try to turn the 24-/32-bit reloc into a 16-bit |
252b5132 RH |
616 | reloc. */ |
617 | ||
e804e836 KH |
618 | /* This is a 24-/32-bit absolute address in a mov insn, which can |
619 | become an 16-bit absolute address if it's in the right range. */ | |
252b5132 RH |
620 | case R_MOVL1: |
621 | /* Get the address of the data referenced by this mov insn. */ | |
cc040812 | 622 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); |
7a9823f1 | 623 | value = bfd_h8300_pad_address (abfd, value); |
252b5132 | 624 | |
7a9823f1 RS |
625 | /* If the address is a sign-extended 16-bit value then we can |
626 | relax this instruction. */ | |
627 | if (value <= 0x7fff || value >= 0xffff8000u) | |
252b5132 RH |
628 | { |
629 | /* Change the reloc type. */ | |
630 | reloc->howto = howto_table + 17; | |
631 | ||
632 | /* This shrinks this section by two bytes. */ | |
633 | shrink += 2; | |
cc040812 | 634 | bfd_perform_slip (abfd, 2, input_section, address); |
252b5132 RH |
635 | } |
636 | break; | |
637 | ||
638 | /* No other reloc types represent relaxing opportunities. */ | |
cc040812 NC |
639 | default: |
640 | break; | |
252b5132 RH |
641 | } |
642 | ||
643 | last_reloc = reloc; | |
644 | last_input_section = input_section; | |
645 | return shrink; | |
646 | } | |
647 | ||
252b5132 RH |
648 | /* Handle relocations for the H8/300, including relocs for relaxed |
649 | instructions. | |
650 | ||
651 | FIXME: Not all relocations check for overflow! */ | |
652 | ||
653 | static void | |
c6baf75e RS |
654 | h8300_reloc16_extra_cases (bfd *abfd, struct bfd_link_info *link_info, |
655 | struct bfd_link_order *link_order, arelent *reloc, | |
656 | bfd_byte *data, unsigned int *src_ptr, | |
657 | unsigned int *dst_ptr) | |
252b5132 RH |
658 | { |
659 | unsigned int src_address = *src_ptr; | |
660 | unsigned int dst_address = *dst_ptr; | |
661 | asection *input_section = link_order->u.indirect.section; | |
662 | bfd_vma value; | |
663 | bfd_vma dot; | |
cc040812 | 664 | int gap, tmp; |
ca9a79a1 | 665 | unsigned char temp_code; |
252b5132 RH |
666 | |
667 | switch (reloc->howto->type) | |
668 | { | |
e804e836 | 669 | /* Generic 8-bit pc-relative relocation. */ |
252b5132 RH |
670 | case R_PCRBYTE: |
671 | /* Get the address of the target of this branch. */ | |
cc040812 | 672 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); |
252b5132 | 673 | |
cc040812 NC |
674 | dot = (link_order->offset |
675 | + dst_address | |
252b5132 RH |
676 | + link_order->u.indirect.section->output_section->vma); |
677 | ||
678 | gap = value - dot; | |
679 | ||
680 | /* Sanity check. */ | |
681 | if (gap < -128 || gap > 126) | |
682 | { | |
683 | if (! ((*link_info->callbacks->reloc_overflow) | |
dfeffb9f L |
684 | (link_info, NULL, |
685 | bfd_asymbol_name (*reloc->sym_ptr_ptr), | |
252b5132 RH |
686 | reloc->howto->name, reloc->addend, input_section->owner, |
687 | input_section, reloc->address))) | |
688 | abort (); | |
689 | } | |
690 | ||
691 | /* Everything looks OK. Apply the relocation and update the | |
692 | src/dst address appropriately. */ | |
252b5132 RH |
693 | bfd_put_8 (abfd, gap, data + dst_address); |
694 | dst_address++; | |
695 | src_address++; | |
696 | ||
697 | /* All done. */ | |
698 | break; | |
699 | ||
e804e836 | 700 | /* Generic 16-bit pc-relative relocation. */ |
252b5132 RH |
701 | case R_PCRWORD: |
702 | /* Get the address of the target of this branch. */ | |
cc040812 | 703 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); |
252b5132 RH |
704 | |
705 | /* Get the address of the instruction (not the reloc). */ | |
5fcfd273 KH |
706 | dot = (link_order->offset |
707 | + dst_address | |
252b5132 RH |
708 | + link_order->u.indirect.section->output_section->vma + 1); |
709 | ||
710 | gap = value - dot; | |
711 | ||
712 | /* Sanity check. */ | |
713 | if (gap > 32766 || gap < -32768) | |
714 | { | |
715 | if (! ((*link_info->callbacks->reloc_overflow) | |
dfeffb9f L |
716 | (link_info, NULL, |
717 | bfd_asymbol_name (*reloc->sym_ptr_ptr), | |
252b5132 RH |
718 | reloc->howto->name, reloc->addend, input_section->owner, |
719 | input_section, reloc->address))) | |
720 | abort (); | |
721 | } | |
722 | ||
723 | /* Everything looks OK. Apply the relocation and update the | |
724 | src/dst address appropriately. */ | |
dc810e39 | 725 | bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address); |
252b5132 RH |
726 | dst_address += 2; |
727 | src_address += 2; | |
728 | ||
729 | /* All done. */ | |
730 | break; | |
731 | ||
e804e836 | 732 | /* Generic 8-bit absolute relocation. */ |
252b5132 RH |
733 | case R_RELBYTE: |
734 | /* Get the address of the object referenced by this insn. */ | |
735 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); | |
736 | ||
7a9823f1 RS |
737 | bfd_put_8 (abfd, value & 0xff, data + dst_address); |
738 | dst_address += 1; | |
739 | src_address += 1; | |
252b5132 RH |
740 | |
741 | /* All done. */ | |
742 | break; | |
743 | ||
e804e836 | 744 | /* Various simple 16-bit absolute relocations. */ |
252b5132 RH |
745 | case R_MOV16B1: |
746 | case R_JMP1: | |
747 | case R_RELWORD: | |
cc040812 | 748 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); |
252b5132 RH |
749 | bfd_put_16 (abfd, value, data + dst_address); |
750 | dst_address += 2; | |
751 | src_address += 2; | |
752 | break; | |
753 | ||
e804e836 | 754 | /* Various simple 24-/32-bit absolute relocations. */ |
252b5132 RH |
755 | case R_MOV24B1: |
756 | case R_MOVL1: | |
757 | case R_RELLONG: | |
758 | /* Get the address of the target of this branch. */ | |
cc040812 | 759 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); |
252b5132 RH |
760 | bfd_put_32 (abfd, value, data + dst_address); |
761 | dst_address += 4; | |
762 | src_address += 4; | |
763 | break; | |
764 | ||
e804e836 | 765 | /* Another 24-/32-bit absolute relocation. */ |
252b5132 RH |
766 | case R_JMPL1: |
767 | /* Get the address of the target of this branch. */ | |
768 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); | |
769 | ||
770 | value = ((value & 0x00ffffff) | |
771 | | (bfd_get_32 (abfd, data + src_address) & 0xff000000)); | |
772 | bfd_put_32 (abfd, value, data + dst_address); | |
773 | dst_address += 4; | |
774 | src_address += 4; | |
775 | break; | |
776 | ||
7e89635a KH |
777 | /* This is a 24-/32-bit absolute address in one of the following |
778 | instructions: | |
779 | ||
780 | "band", "bclr", "biand", "bild", "bior", "bist", "bixor", | |
3255318a NC |
781 | "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", "ldc.w", |
782 | "stc.w" and "mov.[bwl]" | |
7e89635a KH |
783 | |
784 | We may relax this into an 16-bit absolute address if it's in | |
785 | the right range. */ | |
252b5132 RH |
786 | case R_MOVL2: |
787 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); | |
7a9823f1 | 788 | value = bfd_h8300_pad_address (abfd, value); |
252b5132 RH |
789 | |
790 | /* Sanity check. */ | |
7a9823f1 | 791 | if (value <= 0x7fff || value >= 0xffff8000u) |
252b5132 | 792 | { |
e804e836 | 793 | /* Insert the 16-bit value into the proper location. */ |
252b5132 RH |
794 | bfd_put_16 (abfd, value, data + dst_address); |
795 | ||
7e89635a KH |
796 | /* Fix the opcode. For all the instructions that belong to |
797 | this relaxation, we simply need to turn off bit 0x20 in | |
798 | the previous byte. */ | |
bc7eab72 | 799 | data[dst_address - 1] &= ~0x20; |
252b5132 RH |
800 | dst_address += 2; |
801 | src_address += 4; | |
802 | } | |
803 | else | |
804 | { | |
805 | if (! ((*link_info->callbacks->reloc_overflow) | |
dfeffb9f L |
806 | (link_info, NULL, |
807 | bfd_asymbol_name (*reloc->sym_ptr_ptr), | |
252b5132 RH |
808 | reloc->howto->name, reloc->addend, input_section->owner, |
809 | input_section, reloc->address))) | |
810 | abort (); | |
bc7eab72 | 811 | } |
252b5132 RH |
812 | break; |
813 | ||
e804e836 | 814 | /* A 16-bit absolute branch that is now an 8-bit pc-relative branch. */ |
252b5132 RH |
815 | case R_JMP2: |
816 | /* Get the address of the target of this branch. */ | |
817 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); | |
818 | ||
819 | /* Get the address of the next instruction. */ | |
820 | dot = (link_order->offset | |
821 | + dst_address | |
822 | + link_order->u.indirect.section->output_section->vma + 1); | |
823 | ||
824 | gap = value - dot; | |
825 | ||
826 | /* Sanity check. */ | |
827 | if (gap < -128 || gap > 126) | |
828 | { | |
829 | if (! ((*link_info->callbacks->reloc_overflow) | |
dfeffb9f L |
830 | (link_info, NULL, |
831 | bfd_asymbol_name (*reloc->sym_ptr_ptr), | |
252b5132 RH |
832 | reloc->howto->name, reloc->addend, input_section->owner, |
833 | input_section, reloc->address))) | |
834 | abort (); | |
835 | } | |
836 | ||
837 | /* Now fix the instruction itself. */ | |
838 | switch (data[dst_address - 1]) | |
839 | { | |
840 | case 0x5e: | |
841 | /* jsr -> bsr */ | |
842 | bfd_put_8 (abfd, 0x55, data + dst_address - 1); | |
843 | break; | |
844 | case 0x5a: | |
7e89635a | 845 | /* jmp -> bra */ |
252b5132 RH |
846 | bfd_put_8 (abfd, 0x40, data + dst_address - 1); |
847 | break; | |
848 | ||
849 | default: | |
850 | abort (); | |
851 | } | |
852 | ||
e804e836 | 853 | /* Write out the 8-bit value. */ |
252b5132 RH |
854 | bfd_put_8 (abfd, gap, data + dst_address); |
855 | ||
856 | dst_address += 1; | |
857 | src_address += 3; | |
858 | ||
859 | break; | |
860 | ||
e804e836 | 861 | /* A 16-bit pc-relative branch that is now an 8-bit pc-relative branch. */ |
252b5132 RH |
862 | case R_PCRWORD_B: |
863 | /* Get the address of the target of this branch. */ | |
864 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); | |
865 | ||
866 | /* Get the address of the instruction (not the reloc). */ | |
867 | dot = (link_order->offset | |
868 | + dst_address | |
869 | + link_order->u.indirect.section->output_section->vma - 1); | |
870 | ||
871 | gap = value - dot; | |
872 | ||
873 | /* Sanity check. */ | |
874 | if (gap < -128 || gap > 126) | |
875 | { | |
876 | if (! ((*link_info->callbacks->reloc_overflow) | |
dfeffb9f L |
877 | (link_info, NULL, |
878 | bfd_asymbol_name (*reloc->sym_ptr_ptr), | |
252b5132 RH |
879 | reloc->howto->name, reloc->addend, input_section->owner, |
880 | input_section, reloc->address))) | |
881 | abort (); | |
882 | } | |
883 | ||
884 | /* Now fix the instruction. */ | |
885 | switch (data[dst_address - 2]) | |
886 | { | |
887 | case 0x58: | |
888 | /* bCC:16 -> bCC:8 */ | |
7e89635a KH |
889 | /* Get the second byte of the original insn, which contains |
890 | the condition code. */ | |
252b5132 | 891 | tmp = data[dst_address - 1]; |
7e89635a KH |
892 | |
893 | /* Compute the fisrt byte of the relaxed instruction. The | |
894 | original sequence 0x58 0xX0 is relaxed to 0x4X, where X | |
895 | represents the condition code. */ | |
252b5132 RH |
896 | tmp &= 0xf0; |
897 | tmp >>= 4; | |
252b5132 RH |
898 | tmp |= 0x40; |
899 | ||
900 | /* Write it. */ | |
901 | bfd_put_8 (abfd, tmp, data + dst_address - 2); | |
902 | break; | |
d562d2fb | 903 | |
4259e8b6 JL |
904 | case 0x5c: |
905 | /* bsr:16 -> bsr:8 */ | |
906 | bfd_put_8 (abfd, 0x55, data + dst_address - 2); | |
907 | break; | |
252b5132 RH |
908 | |
909 | default: | |
910 | abort (); | |
911 | } | |
912 | ||
bc7eab72 KH |
913 | /* Output the target. */ |
914 | bfd_put_8 (abfd, gap, data + dst_address - 1); | |
252b5132 | 915 | |
e804e836 | 916 | /* We don't advance dst_address -- the 8-bit reloc is applied at |
bc7eab72 KH |
917 | dst_address - 1, so the next insn should begin at dst_address. */ |
918 | src_address += 2; | |
252b5132 | 919 | |
bc7eab72 | 920 | break; |
5fcfd273 | 921 | |
e804e836 | 922 | /* Similarly for a 24-bit absolute that is now 8 bits. */ |
252b5132 RH |
923 | case R_JMPL2: |
924 | /* Get the address of the target of this branch. */ | |
925 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); | |
926 | ||
927 | /* Get the address of the instruction (not the reloc). */ | |
928 | dot = (link_order->offset | |
929 | + dst_address | |
930 | + link_order->u.indirect.section->output_section->vma + 2); | |
931 | ||
932 | gap = value - dot; | |
933 | ||
934 | /* Fix the instruction. */ | |
935 | switch (data[src_address]) | |
936 | { | |
937 | case 0x5e: | |
938 | /* jsr -> bsr */ | |
939 | bfd_put_8 (abfd, 0x55, data + dst_address); | |
940 | break; | |
941 | case 0x5a: | |
942 | /* jmp ->bra */ | |
943 | bfd_put_8 (abfd, 0x40, data + dst_address); | |
944 | break; | |
945 | default: | |
946 | abort (); | |
947 | } | |
948 | ||
949 | bfd_put_8 (abfd, gap, data + dst_address + 1); | |
950 | dst_address += 2; | |
951 | src_address += 4; | |
952 | ||
953 | break; | |
954 | ||
630a7b0a KH |
955 | /* This is a 16-bit absolute address in one of the following |
956 | instructions: | |
957 | ||
958 | "band", "bclr", "biand", "bild", "bior", "bist", "bixor", | |
959 | "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and | |
960 | "mov.b" | |
961 | ||
962 | We may relax this into an 8-bit absolute address if it's in | |
963 | the right range. */ | |
252b5132 RH |
964 | case R_MOV16B2: |
965 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); | |
966 | ||
630a7b0a | 967 | /* All instructions with R_H8_DIR16B2 start with 0x6a. */ |
252b5132 RH |
968 | if (data[dst_address - 2] != 0x6a) |
969 | abort (); | |
970 | ||
ca9a79a1 | 971 | temp_code = data[src_address - 1]; |
630a7b0a KH |
972 | |
973 | /* If this is a mov.b instruction, clear the lower nibble, which | |
974 | contains the source/destination register number. */ | |
ca9a79a1 NC |
975 | if ((temp_code & 0x10) != 0x10) |
976 | temp_code &= 0xf0; | |
977 | ||
252b5132 | 978 | /* Fix up the opcode. */ |
ca9a79a1 | 979 | switch (temp_code) |
252b5132 RH |
980 | { |
981 | case 0x00: | |
630a7b0a | 982 | /* This is mov.b @aa:16,Rd. */ |
cc040812 | 983 | data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x20; |
252b5132 RH |
984 | break; |
985 | case 0x80: | |
630a7b0a | 986 | /* This is mov.b Rs,@aa:16. */ |
cc040812 | 987 | data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x30; |
252b5132 | 988 | break; |
ca9a79a1 | 989 | case 0x18: |
630a7b0a KH |
990 | /* This is a bit-maniputation instruction that stores one |
991 | bit into memory, one of "bclr", "bist", "bnot", "bset", | |
992 | and "bst". */ | |
ca9a79a1 NC |
993 | data[dst_address - 2] = 0x7f; |
994 | break; | |
995 | case 0x10: | |
630a7b0a KH |
996 | /* This is a bit-maniputation instruction that loads one bit |
997 | from memory, one of "band", "biand", "bild", "bior", | |
998 | "bixor", "bld", "bor", "btst", and "bxor". */ | |
ca9a79a1 NC |
999 | data[dst_address - 2] = 0x7e; |
1000 | break; | |
252b5132 RH |
1001 | default: |
1002 | abort (); | |
1003 | } | |
1004 | ||
1005 | bfd_put_8 (abfd, value & 0xff, data + dst_address - 1); | |
1006 | src_address += 2; | |
1007 | break; | |
1008 | ||
630a7b0a KH |
1009 | /* This is a 24-bit absolute address in one of the following |
1010 | instructions: | |
1011 | ||
1012 | "band", "bclr", "biand", "bild", "bior", "bist", "bixor", | |
1013 | "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and | |
1014 | "mov.b" | |
1015 | ||
1016 | We may relax this into an 8-bit absolute address if it's in | |
1017 | the right range. */ | |
252b5132 RH |
1018 | case R_MOV24B2: |
1019 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); | |
1020 | ||
630a7b0a | 1021 | /* All instructions with R_MOV24B2 start with 0x6a. */ |
252b5132 RH |
1022 | if (data[dst_address - 2] != 0x6a) |
1023 | abort (); | |
1024 | ||
ca9a79a1 | 1025 | temp_code = data[src_address - 1]; |
630a7b0a KH |
1026 | |
1027 | /* If this is a mov.b instruction, clear the lower nibble, which | |
1028 | contains the source/destination register number. */ | |
ca9a79a1 NC |
1029 | if ((temp_code & 0x30) != 0x30) |
1030 | temp_code &= 0xf0; | |
1031 | ||
252b5132 | 1032 | /* Fix up the opcode. */ |
ca9a79a1 | 1033 | switch (temp_code) |
252b5132 RH |
1034 | { |
1035 | case 0x20: | |
630a7b0a | 1036 | /* This is mov.b @aa:24/32,Rd. */ |
cc040812 | 1037 | data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x20; |
252b5132 RH |
1038 | break; |
1039 | case 0xa0: | |
630a7b0a | 1040 | /* This is mov.b Rs,@aa:24/32. */ |
cc040812 | 1041 | data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x30; |
252b5132 | 1042 | break; |
ca9a79a1 | 1043 | case 0x38: |
630a7b0a KH |
1044 | /* This is a bit-maniputation instruction that stores one |
1045 | bit into memory, one of "bclr", "bist", "bnot", "bset", | |
1046 | and "bst". */ | |
ca9a79a1 NC |
1047 | data[dst_address - 2] = 0x7f; |
1048 | break; | |
1049 | case 0x30: | |
630a7b0a KH |
1050 | /* This is a bit-maniputation instruction that loads one bit |
1051 | from memory, one of "band", "biand", "bild", "bior", | |
1052 | "bixor", "bld", "bor", "btst", and "bxor". */ | |
ca9a79a1 NC |
1053 | data[dst_address - 2] = 0x7e; |
1054 | break; | |
252b5132 RH |
1055 | default: |
1056 | abort (); | |
1057 | } | |
1058 | ||
1059 | bfd_put_8 (abfd, value & 0xff, data + dst_address - 1); | |
1060 | src_address += 4; | |
1061 | break; | |
1062 | ||
1063 | case R_BCC_INV: | |
1064 | /* Get the address of the target of this branch. */ | |
cc040812 | 1065 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); |
252b5132 | 1066 | |
cc040812 NC |
1067 | dot = (link_order->offset |
1068 | + dst_address | |
252b5132 RH |
1069 | + link_order->u.indirect.section->output_section->vma) + 1; |
1070 | ||
1071 | gap = value - dot; | |
1072 | ||
1073 | /* Sanity check. */ | |
1074 | if (gap < -128 || gap > 126) | |
1075 | { | |
1076 | if (! ((*link_info->callbacks->reloc_overflow) | |
dfeffb9f L |
1077 | (link_info, NULL, |
1078 | bfd_asymbol_name (*reloc->sym_ptr_ptr), | |
252b5132 RH |
1079 | reloc->howto->name, reloc->addend, input_section->owner, |
1080 | input_section, reloc->address))) | |
1081 | abort (); | |
1082 | } | |
1083 | ||
1084 | /* Everything looks OK. Fix the condition in the instruction, apply | |
1085 | the relocation, and update the src/dst address appropriately. */ | |
1086 | ||
1087 | bfd_put_8 (abfd, bfd_get_8 (abfd, data + dst_address - 1) ^ 1, | |
1088 | data + dst_address - 1); | |
1089 | bfd_put_8 (abfd, gap, data + dst_address); | |
1090 | dst_address++; | |
1091 | src_address++; | |
1092 | ||
1093 | /* All done. */ | |
1094 | break; | |
1095 | ||
1096 | case R_JMP_DEL: | |
1097 | src_address += 4; | |
1098 | break; | |
1099 | ||
e804e836 | 1100 | /* An 8-bit memory indirect instruction (jmp/jsr). |
252b5132 RH |
1101 | |
1102 | There's several things that need to be done to handle | |
1103 | this relocation. | |
1104 | ||
1105 | If this is a reloc against the absolute symbol, then | |
1106 | we should handle it just R_RELBYTE. Likewise if it's | |
1107 | for a symbol with a value ge 0 and le 0xff. | |
1108 | ||
1109 | Otherwise it's a jump/call through the function vector, | |
1110 | and the linker is expected to set up the function vector | |
1111 | and put the right value into the jump/call instruction. */ | |
1112 | case R_MEM_INDIRECT: | |
1113 | { | |
1114 | /* We need to find the symbol so we can determine it's | |
1115 | address in the function vector table. */ | |
1116 | asymbol *symbol; | |
252b5132 | 1117 | const char *name; |
dc810e39 | 1118 | struct funcvec_hash_table *ftab; |
252b5132 | 1119 | struct funcvec_hash_entry *h; |
0171ee92 AM |
1120 | struct h8300_coff_link_hash_table *htab; |
1121 | asection *vectors_sec; | |
1122 | ||
1123 | if (link_info->hash->creator != abfd->xvec) | |
1124 | { | |
1125 | (*_bfd_error_handler) | |
1126 | (_("cannot handle R_MEM_INDIRECT reloc when using %s output"), | |
1127 | link_info->hash->creator->name); | |
1128 | ||
1129 | /* What else can we do? This function doesn't allow return | |
1130 | of an error, and we don't want to call abort as that | |
1131 | indicates an internal error. */ | |
1132 | #ifndef EXIT_FAILURE | |
1133 | #define EXIT_FAILURE 1 | |
1134 | #endif | |
1135 | xexit (EXIT_FAILURE); | |
1136 | } | |
1137 | htab = h8300_coff_hash_table (link_info); | |
1138 | vectors_sec = htab->vectors_sec; | |
252b5132 RH |
1139 | |
1140 | /* First see if this is a reloc against the absolute symbol | |
1141 | or against a symbol with a nonnegative value <= 0xff. */ | |
1142 | symbol = *(reloc->sym_ptr_ptr); | |
1143 | value = bfd_coff_reloc16_get_value (reloc, link_info, input_section); | |
1144 | if (symbol == bfd_abs_section_ptr->symbol | |
5f771d47 | 1145 | || value <= 0xff) |
252b5132 RH |
1146 | { |
1147 | /* This should be handled in a manner very similar to | |
1148 | R_RELBYTES. If the value is in range, then just slam | |
1149 | the value into the right location. Else trigger a | |
1150 | reloc overflow callback. */ | |
5f771d47 | 1151 | if (value <= 0xff) |
252b5132 RH |
1152 | { |
1153 | bfd_put_8 (abfd, value, data + dst_address); | |
1154 | dst_address += 1; | |
1155 | src_address += 1; | |
1156 | } | |
1157 | else | |
1158 | { | |
1159 | if (! ((*link_info->callbacks->reloc_overflow) | |
dfeffb9f L |
1160 | (link_info, NULL, |
1161 | bfd_asymbol_name (*reloc->sym_ptr_ptr), | |
252b5132 RH |
1162 | reloc->howto->name, reloc->addend, input_section->owner, |
1163 | input_section, reloc->address))) | |
1164 | abort (); | |
1165 | } | |
1166 | break; | |
1167 | } | |
1168 | ||
1169 | /* This is a jump/call through a function vector, and we're | |
5fcfd273 | 1170 | expected to create the function vector ourselves. |
252b5132 RH |
1171 | |
1172 | First look up this symbol in the linker hash table -- we need | |
1173 | the derived linker symbol which holds this symbol's index | |
1174 | in the function vector. */ | |
1175 | name = symbol->name; | |
1176 | if (symbol->flags & BSF_LOCAL) | |
1177 | { | |
dc810e39 | 1178 | char *new_name = bfd_malloc ((bfd_size_type) strlen (name) + 9); |
d4e2de6b | 1179 | |
252b5132 RH |
1180 | if (new_name == NULL) |
1181 | abort (); | |
1182 | ||
1183 | strcpy (new_name, name); | |
1184 | sprintf (new_name + strlen (name), "_%08x", | |
cc040812 | 1185 | (int) symbol->section); |
252b5132 RH |
1186 | name = new_name; |
1187 | } | |
1188 | ||
0171ee92 | 1189 | ftab = htab->funcvec_hash_table; |
b34976b6 | 1190 | h = funcvec_hash_lookup (ftab, name, FALSE, FALSE); |
252b5132 RH |
1191 | |
1192 | /* This shouldn't ever happen. If it does that means we've got | |
1193 | data corruption of some kind. Aborting seems like a reasonable | |
0171ee92 | 1194 | thing to do here. */ |
252b5132 RH |
1195 | if (h == NULL || vectors_sec == NULL) |
1196 | abort (); | |
1197 | ||
1198 | /* Place the address of the function vector entry into the | |
1199 | reloc's address. */ | |
1200 | bfd_put_8 (abfd, | |
1201 | vectors_sec->output_offset + h->offset, | |
1202 | data + dst_address); | |
1203 | ||
1204 | dst_address++; | |
1205 | src_address++; | |
1206 | ||
1207 | /* Now create an entry in the function vector itself. */ | |
d4e2de6b NC |
1208 | switch (bfd_get_mach (input_section->owner)) |
1209 | { | |
1210 | case bfd_mach_h8300: | |
1211 | case bfd_mach_h8300hn: | |
1212 | case bfd_mach_h8300sn: | |
1213 | bfd_put_16 (abfd, | |
1214 | bfd_coff_reloc16_get_value (reloc, | |
1215 | link_info, | |
1216 | input_section), | |
1217 | vectors_sec->contents + h->offset); | |
1218 | break; | |
1219 | case bfd_mach_h8300h: | |
1220 | case bfd_mach_h8300s: | |
1221 | bfd_put_32 (abfd, | |
1222 | bfd_coff_reloc16_get_value (reloc, | |
1223 | link_info, | |
1224 | input_section), | |
1225 | vectors_sec->contents + h->offset); | |
1226 | break; | |
1227 | default: | |
1228 | abort (); | |
1229 | } | |
252b5132 RH |
1230 | |
1231 | /* Gross. We've already written the contents of the vector section | |
1232 | before we get here... So we write it again with the new data. */ | |
1233 | bfd_set_section_contents (vectors_sec->output_section->owner, | |
1234 | vectors_sec->output_section, | |
1235 | vectors_sec->contents, | |
dc810e39 | 1236 | (file_ptr) vectors_sec->output_offset, |
eea6121a | 1237 | vectors_sec->size); |
252b5132 RH |
1238 | break; |
1239 | } | |
1240 | ||
1241 | default: | |
1242 | abort (); | |
1243 | break; | |
1244 | ||
1245 | } | |
1246 | ||
1247 | *src_ptr = src_address; | |
1248 | *dst_ptr = dst_address; | |
1249 | } | |
1250 | ||
252b5132 RH |
1251 | /* Routine for the h8300 linker. |
1252 | ||
1253 | This routine is necessary to handle the special R_MEM_INDIRECT | |
1254 | relocs on the h8300. It's responsible for generating a vectors | |
1255 | section and attaching it to an input bfd as well as sizing | |
1256 | the vectors section. It also creates our vectors hash table. | |
1257 | ||
1258 | It uses the generic linker routines to actually add the symbols. | |
1259 | from this BFD to the bfd linker hash table. It may add a few | |
1260 | selected static symbols to the bfd linker hash table. */ | |
1261 | ||
b34976b6 | 1262 | static bfd_boolean |
c6baf75e | 1263 | h8300_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info) |
252b5132 RH |
1264 | { |
1265 | asection *sec; | |
1266 | struct funcvec_hash_table *funcvec_hash_table; | |
dc810e39 | 1267 | bfd_size_type amt; |
0171ee92 AM |
1268 | struct h8300_coff_link_hash_table *htab; |
1269 | ||
1270 | /* Add the symbols using the generic code. */ | |
1271 | _bfd_generic_link_add_symbols (abfd, info); | |
1272 | ||
1273 | if (info->hash->creator != abfd->xvec) | |
1274 | return TRUE; | |
1275 | ||
1276 | htab = h8300_coff_hash_table (info); | |
252b5132 RH |
1277 | |
1278 | /* If we haven't created a vectors section, do so now. */ | |
0171ee92 | 1279 | if (!htab->vectors_sec) |
252b5132 RH |
1280 | { |
1281 | flagword flags; | |
1282 | ||
1283 | /* Make sure the appropriate flags are set, including SEC_IN_MEMORY. */ | |
1284 | flags = (SEC_ALLOC | SEC_LOAD | |
1285 | | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_READONLY); | |
0171ee92 | 1286 | htab->vectors_sec = bfd_make_section (abfd, ".vectors"); |
252b5132 RH |
1287 | |
1288 | /* If the section wasn't created, or we couldn't set the flags, | |
0171ee92 AM |
1289 | quit quickly now, rather than dying a painful death later. */ |
1290 | if (!htab->vectors_sec | |
1291 | || !bfd_set_section_flags (abfd, htab->vectors_sec, flags)) | |
b34976b6 | 1292 | return FALSE; |
252b5132 RH |
1293 | |
1294 | /* Also create the vector hash table. */ | |
dc810e39 AM |
1295 | amt = sizeof (struct funcvec_hash_table); |
1296 | funcvec_hash_table = (struct funcvec_hash_table *) bfd_alloc (abfd, amt); | |
252b5132 RH |
1297 | |
1298 | if (!funcvec_hash_table) | |
b34976b6 | 1299 | return FALSE; |
252b5132 RH |
1300 | |
1301 | /* And initialize the funcvec hash table. */ | |
1302 | if (!funcvec_hash_table_init (funcvec_hash_table, abfd, | |
1303 | funcvec_hash_newfunc)) | |
1304 | { | |
1305 | bfd_release (abfd, funcvec_hash_table); | |
b34976b6 | 1306 | return FALSE; |
252b5132 RH |
1307 | } |
1308 | ||
1309 | /* Store away a pointer to the funcvec hash table. */ | |
0171ee92 | 1310 | htab->funcvec_hash_table = funcvec_hash_table; |
252b5132 RH |
1311 | } |
1312 | ||
1313 | /* Load up the function vector hash table. */ | |
0171ee92 | 1314 | funcvec_hash_table = htab->funcvec_hash_table; |
252b5132 RH |
1315 | |
1316 | /* Now scan the relocs for all the sections in this bfd; create | |
1317 | additional space in the .vectors section as needed. */ | |
1318 | for (sec = abfd->sections; sec; sec = sec->next) | |
1319 | { | |
1320 | long reloc_size, reloc_count, i; | |
1321 | asymbol **symbols; | |
1322 | arelent **relocs; | |
1323 | ||
1324 | /* Suck in the relocs, symbols & canonicalize them. */ | |
1325 | reloc_size = bfd_get_reloc_upper_bound (abfd, sec); | |
1326 | if (reloc_size <= 0) | |
1327 | continue; | |
1328 | ||
dc810e39 | 1329 | relocs = (arelent **) bfd_malloc ((bfd_size_type) reloc_size); |
252b5132 | 1330 | if (!relocs) |
b34976b6 | 1331 | return FALSE; |
252b5132 RH |
1332 | |
1333 | /* The symbols should have been read in by _bfd_generic link_add_symbols | |
1334 | call abovec, so we can cheat and use the pointer to them that was | |
1335 | saved in the above call. */ | |
1336 | symbols = _bfd_generic_link_get_symbols(abfd); | |
1337 | reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, symbols); | |
1338 | if (reloc_count <= 0) | |
1339 | { | |
1340 | free (relocs); | |
1341 | continue; | |
1342 | } | |
1343 | ||
1344 | /* Now walk through all the relocations in this section. */ | |
1345 | for (i = 0; i < reloc_count; i++) | |
1346 | { | |
1347 | arelent *reloc = relocs[i]; | |
1348 | asymbol *symbol = *(reloc->sym_ptr_ptr); | |
1349 | const char *name; | |
1350 | ||
1351 | /* We've got an indirect reloc. See if we need to add it | |
1352 | to the function vector table. At this point, we have | |
1353 | to add a new entry for each unique symbol referenced | |
1354 | by an R_MEM_INDIRECT relocation except for a reloc | |
1355 | against the absolute section symbol. */ | |
1356 | if (reloc->howto->type == R_MEM_INDIRECT | |
1357 | && symbol != bfd_abs_section_ptr->symbol) | |
1358 | ||
1359 | { | |
dc810e39 | 1360 | struct funcvec_hash_table *ftab; |
252b5132 RH |
1361 | struct funcvec_hash_entry *h; |
1362 | ||
1363 | name = symbol->name; | |
1364 | if (symbol->flags & BSF_LOCAL) | |
1365 | { | |
dc810e39 | 1366 | char *new_name; |
252b5132 | 1367 | |
dc810e39 | 1368 | new_name = bfd_malloc ((bfd_size_type) strlen (name) + 9); |
252b5132 RH |
1369 | if (new_name == NULL) |
1370 | abort (); | |
1371 | ||
1372 | strcpy (new_name, name); | |
1373 | sprintf (new_name + strlen (name), "_%08x", | |
cc040812 | 1374 | (int) symbol->section); |
252b5132 RH |
1375 | name = new_name; |
1376 | } | |
1377 | ||
1378 | /* Look this symbol up in the function vector hash table. */ | |
0171ee92 | 1379 | ftab = htab->funcvec_hash_table; |
b34976b6 | 1380 | h = funcvec_hash_lookup (ftab, name, FALSE, FALSE); |
252b5132 | 1381 | |
252b5132 RH |
1382 | /* If this symbol isn't already in the hash table, add |
1383 | it and bump up the size of the hash table. */ | |
1384 | if (h == NULL) | |
1385 | { | |
b34976b6 | 1386 | h = funcvec_hash_lookup (ftab, name, TRUE, TRUE); |
252b5132 RH |
1387 | if (h == NULL) |
1388 | { | |
1389 | free (relocs); | |
b34976b6 | 1390 | return FALSE; |
252b5132 RH |
1391 | } |
1392 | ||
1393 | /* Bump the size of the vectors section. Each vector | |
1394 | takes 2 bytes on the h8300 and 4 bytes on the h8300h. */ | |
d4e2de6b NC |
1395 | switch (bfd_get_mach (abfd)) |
1396 | { | |
1397 | case bfd_mach_h8300: | |
1398 | case bfd_mach_h8300hn: | |
1399 | case bfd_mach_h8300sn: | |
eea6121a | 1400 | htab->vectors_sec->size += 2; |
d4e2de6b NC |
1401 | break; |
1402 | case bfd_mach_h8300h: | |
1403 | case bfd_mach_h8300s: | |
eea6121a | 1404 | htab->vectors_sec->size += 4; |
d4e2de6b NC |
1405 | break; |
1406 | default: | |
1407 | abort (); | |
1408 | } | |
252b5132 RH |
1409 | } |
1410 | } | |
1411 | } | |
1412 | ||
1413 | /* We're done with the relocations, release them. */ | |
1414 | free (relocs); | |
1415 | } | |
1416 | ||
1417 | /* Now actually allocate some space for the function vector. It's | |
1418 | wasteful to do this more than once, but this is easier. */ | |
0171ee92 | 1419 | sec = htab->vectors_sec; |
eea6121a | 1420 | if (sec->size != 0) |
252b5132 RH |
1421 | { |
1422 | /* Free the old contents. */ | |
dc810e39 AM |
1423 | if (sec->contents) |
1424 | free (sec->contents); | |
252b5132 RH |
1425 | |
1426 | /* Allocate new contents. */ | |
eea6121a | 1427 | sec->contents = bfd_malloc (sec->size); |
252b5132 RH |
1428 | } |
1429 | ||
b34976b6 | 1430 | return TRUE; |
252b5132 RH |
1431 | } |
1432 | ||
1433 | #define coff_reloc16_extra_cases h8300_reloc16_extra_cases | |
1434 | #define coff_reloc16_estimate h8300_reloc16_estimate | |
1435 | #define coff_bfd_link_add_symbols h8300_bfd_link_add_symbols | |
1436 | #define coff_bfd_link_hash_table_create h8300_coff_link_hash_table_create | |
1437 | ||
1438 | #define COFF_LONG_FILENAMES | |
1439 | #include "coffcode.h" | |
1440 | ||
252b5132 RH |
1441 | #undef coff_bfd_get_relocated_section_contents |
1442 | #undef coff_bfd_relax_section | |
1443 | #define coff_bfd_get_relocated_section_contents \ | |
1444 | bfd_coff_reloc16_get_relocated_section_contents | |
1445 | #define coff_bfd_relax_section bfd_coff_reloc16_relax_section | |
1446 | ||
3fa78519 | 1447 | CREATE_BIG_COFF_TARGET_VEC (h8300coff_vec, "coff-h8300", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE) |