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