home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / coff-h8300.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  43KB  |  1,388 lines

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