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