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