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