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