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 / elf32-m68k.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  49KB  |  1,601 lines

  1. /* Motorola 68k series support for 32-bit ELF
  2.    Copyright 1993, 1995, 1996 Free Software Foundation, Inc.
  3.  
  4. This file is part of BFD, the Binary File Descriptor library.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  19.  
  20. #include "bfd.h"
  21. #include "sysdep.h"
  22. #include "bfdlink.h"
  23. #include "libbfd.h"
  24. #include "elf-bfd.h"
  25.  
  26. static reloc_howto_type *reloc_type_lookup
  27.   PARAMS ((bfd *, bfd_reloc_code_real_type));
  28. static void rtype_to_howto
  29.   PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
  30. static void rtype_to_howto_rel
  31.   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
  32. static boolean elf_m68k_check_relocs
  33.   PARAMS ((bfd *, struct bfd_link_info *, asection *,
  34.        const Elf_Internal_Rela *));
  35. static boolean elf_m68k_adjust_dynamic_symbol
  36.   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
  37. static boolean elf_m68k_size_dynamic_sections
  38.   PARAMS ((bfd *, struct bfd_link_info *));
  39. static boolean elf_m68k_relocate_section
  40.   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  41.        Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
  42. static boolean elf_m68k_finish_dynamic_symbol
  43.   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
  44.        Elf_Internal_Sym *));
  45. static boolean elf_m68k_finish_dynamic_sections
  46.   PARAMS ((bfd *, struct bfd_link_info *));
  47.  
  48. /* elf32 m68k code, generated by elf.el */
  49. enum reloc_type {
  50.   R_68K_NONE        =   0,
  51.   R_68K_32          =   1,
  52.   R_68K_16          =   2,
  53.   R_68K_8           =   3,
  54.   R_68K_PC32        =   4,
  55.   R_68K_PC16        =   5,
  56.   R_68K_PC8         =   6,
  57.   R_68K_GOT32       =   7,
  58.   R_68K_GOT16       =   8,
  59.   R_68K_GOT8        =   9,
  60.   R_68K_GOT32O      =  10,
  61.   R_68K_GOT16O      =  11,
  62.   R_68K_GOT8O       =  12,
  63.   R_68K_PLT32       =  13,
  64.   R_68K_PLT16       =  14,
  65.   R_68K_PLT8        =  15,
  66.   R_68K_PLT32O      =  16,
  67.   R_68K_PLT16O      =  17,
  68.   R_68K_PLT8O       =  18,
  69.   R_68K_COPY        =  19,
  70.   R_68K_GLOB_DAT    =  20,
  71.   R_68K_JMP_SLOT    =  21,
  72.   R_68K_RELATIVE    =  22,
  73.   R_68K__max
  74. };
  75.  
  76. static reloc_howto_type howto_table[] = {
  77.   HOWTO(R_68K_NONE,       0, 0, 0, false,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_NONE",      false, 0, 0x00000000,false),
  78.   HOWTO(R_68K_32,         0, 2,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32",        false, 0, 0xffffffff,false),
  79.   HOWTO(R_68K_16,         0, 1,16, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16",        false, 0, 0x0000ffff,false),
  80.   HOWTO(R_68K_8,          0, 0, 8, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8",         false, 0, 0x000000ff,false),
  81.   HOWTO(R_68K_PC32,       0, 2,32, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PC32",      false, 0, 0xffffffff,true),
  82.   HOWTO(R_68K_PC16,       0, 1,16, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PC16",      false, 0, 0x0000ffff,true),
  83.   HOWTO(R_68K_PC8,        0, 0, 8, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PC8",       false, 0, 0x000000ff,true),
  84.   HOWTO(R_68K_GOT32,      0, 2,32, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT32",     false, 0, 0xffffffff,true),
  85.   HOWTO(R_68K_GOT16,      0, 1,16, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT16",     false, 0, 0x0000ffff,true),
  86.   HOWTO(R_68K_GOT8,       0, 0, 8, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT8",      false, 0, 0x000000ff,true),
  87.   HOWTO(R_68K_GOT32O,     0, 2,32, false,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT32O",    false, 0, 0xffffffff,false),
  88.   HOWTO(R_68K_GOT16O,     0, 1,16, false,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT16O",    false, 0, 0x0000ffff,false),
  89.   HOWTO(R_68K_GOT8O,      0, 0, 8, false,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT8O",     false, 0, 0x000000ff,false),
  90.   HOWTO(R_68K_PLT32,      0, 2,32, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT32",     false, 0, 0xffffffff,true),
  91.   HOWTO(R_68K_PLT16,      0, 1,16, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT16",     false, 0, 0x0000ffff,true),
  92.   HOWTO(R_68K_PLT8,       0, 0, 8, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT8",      false, 0, 0x000000ff,true),
  93.   HOWTO(R_68K_PLT32O,     0, 2,32, false,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT32O",    false, 0, 0xffffffff,false),
  94.   HOWTO(R_68K_PLT16O,     0, 1,16, false,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT16O",    false, 0, 0x0000ffff,false),
  95.   HOWTO(R_68K_PLT8O,      0, 0, 8, false,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT8O",     false, 0, 0x000000ff,false),
  96.   HOWTO(R_68K_COPY,       0, 0, 0, false,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_COPY",      false, 0, 0xffffffff,false),
  97.   HOWTO(R_68K_GLOB_DAT,   0, 2,32, false,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_GLOB_DAT",  false, 0, 0xffffffff,false),
  98.   HOWTO(R_68K_JMP_SLOT,   0, 2,32, false,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_JMP_SLOT",  false, 0, 0xffffffff,false),
  99.   HOWTO(R_68K_RELATIVE,   0, 2,32, false,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_RELATIVE",  false, 0, 0xffffffff,false),
  100. };
  101.  
  102. static void
  103. rtype_to_howto (abfd, cache_ptr, dst)
  104.      bfd *abfd;
  105.      arelent *cache_ptr;
  106.      Elf_Internal_Rela *dst;
  107. {
  108.   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K__max);
  109.   cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
  110. }
  111.  
  112. static void
  113. rtype_to_howto_rel (abfd, cache_ptr, dst)
  114.      bfd *abfd;
  115.      arelent *cache_ptr;
  116.      Elf_Internal_Rel *dst;
  117. {
  118.   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K__max);
  119.   cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
  120. }
  121.  
  122. #define elf_info_to_howto rtype_to_howto
  123. #define elf_info_to_howto_rel rtype_to_howto_rel
  124.  
  125. static const struct { unsigned char bfd_val, elf_val; } reloc_map[] = {
  126.   { BFD_RELOC_NONE, R_68K_NONE },
  127.   { BFD_RELOC_32, R_68K_32 },
  128.   { BFD_RELOC_16, R_68K_16 },
  129.   { BFD_RELOC_8, R_68K_8 },
  130.   { BFD_RELOC_32_PCREL, R_68K_PC32 },
  131.   { BFD_RELOC_16_PCREL, R_68K_PC16 },
  132.   { BFD_RELOC_8_PCREL, R_68K_PC8 },
  133.   { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
  134.   { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
  135.   { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
  136.   { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
  137.   { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
  138.   { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
  139.   { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
  140.   { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
  141.   { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
  142.   { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
  143.   { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
  144.   { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
  145.   { BFD_RELOC_NONE, R_68K_COPY },
  146.   { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
  147.   { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
  148.   { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
  149.   { BFD_RELOC_CTOR, R_68K_32 },
  150. };
  151.  
  152. static reloc_howto_type *
  153. reloc_type_lookup (abfd, code)
  154.      bfd *abfd;
  155.      bfd_reloc_code_real_type code;
  156. {
  157.   unsigned int i;
  158.   for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
  159.     {
  160.       if (reloc_map[i].bfd_val == code)
  161.     return &howto_table[(int) reloc_map[i].elf_val];
  162.     }
  163.   return 0;
  164. }
  165.  
  166. #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
  167. #define ELF_ARCH bfd_arch_m68k
  168. /* end code generated by elf.el */
  169.  
  170. #define USE_RELA
  171.  
  172.  
  173. /* Functions for the m68k ELF linker.  */
  174.  
  175. /* The name of the dynamic interpreter.  This is put in the .interp
  176.    section.  */
  177.  
  178. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
  179.  
  180. /* The size in bytes of an entry in the procedure linkage table.  */
  181.  
  182. #define PLT_ENTRY_SIZE 20
  183.  
  184. /* The first entry in a procedure linkage table looks like this.  See
  185.    the SVR4 ABI m68k supplement to see how this works.  */
  186.  
  187. static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
  188. {
  189.   0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
  190.   0, 0, 0, 0,          /* replaced with offset to .got + 4.  */
  191.   0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
  192.   0, 0, 0, 0,          /* replaced with offset to .got + 8.  */
  193.   0, 0, 0, 0          /* pad out to 20 bytes.  */
  194. };
  195.  
  196. /* Subsequent entries in a procedure linkage table look like this.  */
  197.  
  198. static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
  199. {
  200.   0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
  201.   0, 0, 0, 0,          /* replaced with offset to symbol's .got entry.  */
  202.   0x2f, 0x3c,          /* move.l #offset,-(%sp) */
  203.   0, 0, 0, 0,          /* replaced with offset into relocation table.  */
  204.   0x60, 0xff,          /* bra.l .plt */
  205.   0, 0, 0, 0          /* replaced with offset to start of .plt.  */
  206. };
  207.  
  208. /* Look through the relocs for a section during the first phase, and
  209.    allocate space in the global offset table or procedure linkage
  210.    table.  */
  211.  
  212. static boolean
  213. elf_m68k_check_relocs (abfd, info, sec, relocs)
  214.      bfd *abfd;
  215.      struct bfd_link_info *info;
  216.      asection *sec;
  217.      const Elf_Internal_Rela *relocs;
  218. {
  219.   bfd *dynobj;
  220.   Elf_Internal_Shdr *symtab_hdr;
  221.   struct elf_link_hash_entry **sym_hashes;
  222.   bfd_vma *local_got_offsets;
  223.   const Elf_Internal_Rela *rel;
  224.   const Elf_Internal_Rela *rel_end;
  225.   asection *sgot;
  226.   asection *srelgot;
  227.   asection *sreloc;
  228.  
  229.   if (info->relocateable)
  230.     return true;
  231.  
  232.   dynobj = elf_hash_table (info)->dynobj;
  233.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  234.   sym_hashes = elf_sym_hashes (abfd);
  235.   local_got_offsets = elf_local_got_offsets (abfd);
  236.  
  237.   sgot = NULL;
  238.   srelgot = NULL;
  239.   sreloc = NULL;
  240.  
  241.   rel_end = relocs + sec->reloc_count;
  242.   for (rel = relocs; rel < rel_end; rel++)
  243.     {
  244.       unsigned long r_symndx;
  245.       struct elf_link_hash_entry *h;
  246.  
  247.       r_symndx = ELF32_R_SYM (rel->r_info);
  248.  
  249.       if (r_symndx < symtab_hdr->sh_info)
  250.     h = NULL;
  251.       else
  252.     h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  253.  
  254.       switch (ELF32_R_TYPE (rel->r_info))
  255.     {
  256.     case R_68K_GOT8:
  257.     case R_68K_GOT16:
  258.     case R_68K_GOT32:
  259.       if (h != NULL
  260.           && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  261.         break;
  262.       /* Fall through.  */
  263.     case R_68K_GOT8O:
  264.     case R_68K_GOT16O:
  265.     case R_68K_GOT32O:
  266.       /* This symbol requires a global offset table entry.  */
  267.  
  268.       if (dynobj == NULL)
  269.         {
  270.           /* Create the .got section.  */
  271.           elf_hash_table (info)->dynobj = dynobj = abfd;
  272.           if (!_bfd_elf_create_got_section (dynobj, info))
  273.         return false;
  274.         }
  275.  
  276.       if (sgot == NULL)
  277.         {
  278.           sgot = bfd_get_section_by_name (dynobj, ".got");
  279.           BFD_ASSERT (sgot != NULL);
  280.         }
  281.  
  282.       if (srelgot == NULL
  283.           && (h != NULL || info->shared))
  284.         {
  285.           srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
  286.           if (srelgot == NULL)
  287.         {
  288.           srelgot = bfd_make_section (dynobj, ".rela.got");
  289.           if (srelgot == NULL
  290.               || !bfd_set_section_flags (dynobj, srelgot,
  291.                          (SEC_ALLOC
  292.                           | SEC_LOAD
  293.                           | SEC_HAS_CONTENTS
  294.                           | SEC_IN_MEMORY
  295.                           | SEC_READONLY))
  296.               || !bfd_set_section_alignment (dynobj, srelgot, 2))
  297.             return false;
  298.         }
  299.         }
  300.  
  301.       if (h != NULL)
  302.         {
  303.           if (h->got_offset != (bfd_vma) -1)
  304.         {
  305.           /* We have already allocated space in the .got.  */
  306.           break;
  307.         }
  308.           h->got_offset = sgot->_raw_size;
  309.  
  310.           /* Make sure this symbol is output as a dynamic symbol.  */
  311.           if (h->dynindx == -1)
  312.         {
  313.           if (!bfd_elf32_link_record_dynamic_symbol (info, h))
  314.             return false;
  315.         }
  316.  
  317.           srelgot->_raw_size += sizeof (Elf32_External_Rela);
  318.         }
  319.       else
  320.         {
  321.           /* This is a global offset table entry for a local
  322.          symbol.  */
  323.           if (local_got_offsets == NULL)
  324.         {
  325.           size_t size;
  326.           register unsigned int i;
  327.  
  328.           size = symtab_hdr->sh_info * sizeof (bfd_vma);
  329.           local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
  330.           if (local_got_offsets == NULL)
  331.             return false;
  332.           elf_local_got_offsets (abfd) = local_got_offsets;
  333.           for (i = 0; i < symtab_hdr->sh_info; i++)
  334.             local_got_offsets[i] = (bfd_vma) -1;
  335.         }
  336.           if (local_got_offsets[r_symndx] != (bfd_vma) -1)
  337.         {
  338.           /* We have already allocated space in the .got.  */
  339.           break;
  340.         }
  341.           local_got_offsets[r_symndx] = sgot->_raw_size;
  342.  
  343.           if (info->shared)
  344.         {
  345.           /* If we are generating a shared object, we need to
  346.              output a R_68K_RELATIVE reloc so that the dynamic
  347.              linker can adjust this GOT entry.  */
  348.           srelgot->_raw_size += sizeof (Elf32_External_Rela);
  349.         }
  350.         }
  351.  
  352.       sgot->_raw_size += 4;
  353.       break;
  354.  
  355.     case R_68K_PLT8:
  356.     case R_68K_PLT16:
  357.     case R_68K_PLT32:
  358.       /* This symbol requires a procedure linkage table entry.  We
  359.          actually build the entry in adjust_dynamic_symbol,
  360.              because this might be a case of linking PIC code which is
  361.              never referenced by a dynamic object, in which case we
  362.              don't need to generate a procedure linkage table entry
  363.              after all.  */
  364.  
  365.       /* If this is a local symbol, we resolve it directly without
  366.          creating a procedure linkage table entry.  */
  367.       if (h == NULL)
  368.         continue;
  369.  
  370.       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
  371.       break;
  372.  
  373.     case R_68K_PLT8O:
  374.     case R_68K_PLT16O:
  375.     case R_68K_PLT32O:
  376.       /* This symbol requires a procedure linkage table entry.  */
  377.  
  378.       if (h == NULL)
  379.         {
  380.           /* It does not make sense to have this relocation for a
  381.          local symbol.  FIXME: does it?  How to handle it if
  382.          it does make sense?  */
  383.           bfd_set_error (bfd_error_bad_value);
  384.           return false;
  385.         }
  386.  
  387.       /* Make sure this symbol is output as a dynamic symbol.  */
  388.       if (h->dynindx == -1)
  389.         {
  390.           if (!bfd_elf32_link_record_dynamic_symbol (info, h))
  391.         return false;
  392.         }
  393.  
  394.       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
  395.       break;
  396.  
  397.     case R_68K_PC8:
  398.     case R_68K_PC16:
  399.     case R_68K_PC32:
  400.       if (h == NULL)
  401.         break;
  402.       /* Fall through.  */
  403.     case R_68K_8:
  404.     case R_68K_16:
  405.     case R_68K_32:
  406.       if (info->shared
  407.           && (sec->flags & SEC_ALLOC) != 0
  408.           && ((ELF32_R_TYPE (rel->r_info) != R_68K_PC8
  409.            && ELF32_R_TYPE (rel->r_info) != R_68K_PC16
  410.            && ELF32_R_TYPE (rel->r_info) != R_68K_PC32)
  411.           || (!info->symbolic
  412.               || (h->elf_link_hash_flags
  413.               & ELF_LINK_HASH_DEF_REGULAR) == 0)))
  414.         {
  415.           /* When creating a shared object, we must copy these
  416.          reloc types into the output file.  We create a reloc
  417.          section in dynobj and make room for this reloc.  */
  418.           if (sreloc == NULL)
  419.         {
  420.           const char *name;
  421.  
  422.           name = (bfd_elf_string_from_elf_section
  423.               (abfd,
  424.                elf_elfheader (abfd)->e_shstrndx,
  425.                elf_section_data (sec)->rel_hdr.sh_name));
  426.           if (name == NULL)
  427.             return false;
  428.  
  429.           BFD_ASSERT (strncmp (name, ".rela", 5) == 0
  430.                   && strcmp (bfd_get_section_name (abfd, sec),
  431.                      name + 5) == 0);
  432.  
  433.           sreloc = bfd_get_section_by_name (dynobj, name);
  434.           if (sreloc == NULL)
  435.             {
  436.               sreloc = bfd_make_section (dynobj, name);
  437.               if (sreloc == NULL
  438.               || !bfd_set_section_flags (dynobj, sreloc,
  439.                              (SEC_ALLOC
  440.                               | SEC_LOAD
  441.                               | SEC_HAS_CONTENTS
  442.                               | SEC_IN_MEMORY
  443.                               | SEC_READONLY))
  444.               || !bfd_set_section_alignment (dynobj, sreloc, 2))
  445.             return false;
  446.             }
  447.         }
  448.  
  449.           sreloc->_raw_size += sizeof (Elf32_External_Rela);
  450.         }
  451.  
  452.       break;
  453.  
  454.     default:
  455.       break;
  456.     }
  457.     }
  458.  
  459.   return true;
  460. }
  461.  
  462. /* Adjust a symbol defined by a dynamic object and referenced by a
  463.    regular object.  The current definition is in some section of the
  464.    dynamic object, but we're not including those sections.  We have to
  465.    change the definition to something the rest of the link can
  466.    understand.  */
  467.  
  468. static boolean
  469. elf_m68k_adjust_dynamic_symbol (info, h)
  470.      struct bfd_link_info *info;
  471.      struct elf_link_hash_entry *h;
  472. {
  473.   bfd *dynobj;
  474.   asection *s;
  475.   unsigned int power_of_two;
  476.  
  477.   dynobj = elf_hash_table (info)->dynobj;
  478.  
  479.   /* Make sure we know what is going on here.  */
  480.   BFD_ASSERT (dynobj != NULL
  481.           && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
  482.           || h->weakdef != NULL
  483.           || ((h->elf_link_hash_flags
  484.                & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  485.               && (h->elf_link_hash_flags
  486.               & ELF_LINK_HASH_REF_REGULAR) != 0
  487.               && (h->elf_link_hash_flags
  488.               & ELF_LINK_HASH_DEF_REGULAR) == 0)));
  489.  
  490.   /* If this is a function, put it in the procedure linkage table.  We
  491.      will fill in the contents of the procedure linkage table later,
  492.      when we know the address of the .got section.  */
  493.   if (h->type == STT_FUNC
  494.       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
  495.     {
  496.       if (! info->shared
  497.       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
  498.       && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
  499.       /* We must always create the plt entry if it was referenced
  500.          by a PLTxxO relocation.  In this case we already recorded
  501.          it as a dynamic symbol.  */
  502.       && h->dynindx == -1)
  503.     {
  504.       /* This case can occur if we saw a PLTxx reloc in an input
  505.          file, but the symbol was never referred to by a dynamic
  506.          object.  In such a case, we don't actually need to build
  507.          a procedure linkage table, and we can just do a PCxx
  508.          reloc instead.  */
  509.       BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
  510.       return true;
  511.     }
  512.  
  513.       /* Make sure this symbol is output as a dynamic symbol.  */
  514.       if (h->dynindx == -1)
  515.     {
  516.       if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  517.         return false;
  518.     }
  519.  
  520.       s = bfd_get_section_by_name (dynobj, ".plt");
  521.       BFD_ASSERT (s != NULL);
  522.  
  523.       /* If this is the first .plt entry, make room for the special
  524.      first entry.  */
  525.       if (s->_raw_size == 0)
  526.     s->_raw_size += PLT_ENTRY_SIZE;
  527.  
  528.       /* If this symbol is not defined in a regular file, and we are
  529.      not generating a shared library, then set the symbol to this
  530.      location in the .plt.  This is required to make function
  531.      pointers compare as equal between the normal executable and
  532.      the shared library.  */
  533.       if (!info->shared
  534.       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
  535.     {
  536.       h->root.u.def.section = s;
  537.       h->root.u.def.value = s->_raw_size;
  538.     }
  539.  
  540.       h->plt_offset = s->_raw_size;
  541.  
  542.       /* Make room for this entry.  */
  543.       s->_raw_size += PLT_ENTRY_SIZE;
  544.  
  545.       /* We also need to make an entry in the .got.plt section, which
  546.      will be placed in the .got section by the linker script.  */
  547.  
  548.       s = bfd_get_section_by_name (dynobj, ".got.plt");
  549.       BFD_ASSERT (s != NULL);
  550.       s->_raw_size += 4;
  551.  
  552.       /* We also need to make an entry in the .rela.plt section.  */
  553.  
  554.       s = bfd_get_section_by_name (dynobj, ".rela.plt");
  555.       BFD_ASSERT (s != NULL);
  556.       s->_raw_size += sizeof (Elf32_External_Rela);
  557.  
  558.       return true;
  559.     }
  560.  
  561.   /* If this is a weak symbol, and there is a real definition, the
  562.      processor independent code will have arranged for us to see the
  563.      real definition first, and we can just use the same value.  */
  564.   if (h->weakdef != NULL)
  565.     {
  566.       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
  567.           || h->weakdef->root.type == bfd_link_hash_defweak);
  568.       h->root.u.def.section = h->weakdef->root.u.def.section;
  569.       h->root.u.def.value = h->weakdef->root.u.def.value;
  570.       return true;
  571.     }
  572.  
  573.   /* This is a reference to a symbol defined by a dynamic object which
  574.      is not a function.  */
  575.  
  576.   /* If we are creating a shared library, we must presume that the
  577.      only references to the symbol are via the global offset table.
  578.      For such cases we need not do anything here; the relocations will
  579.      be handled correctly by relocate_section.  */
  580.   if (info->shared)
  581.     return true;
  582.  
  583.   /* We must allocate the symbol in our .dynbss section, which will
  584.      become part of the .bss section of the executable.  There will be
  585.      an entry for this symbol in the .dynsym section.  The dynamic
  586.      object will contain position independent code, so all references
  587.      from the dynamic object to this symbol will go through the global
  588.      offset table.  The dynamic linker will use the .dynsym entry to
  589.      determine the address it must put in the global offset table, so
  590.      both the dynamic object and the regular object will refer to the
  591.      same memory location for the variable.  */
  592.  
  593.   s = bfd_get_section_by_name (dynobj, ".dynbss");
  594.   BFD_ASSERT (s != NULL);
  595.  
  596.   /* If the symbol is currently defined in the .bss section of the
  597.      dynamic object, then it is OK to simply initialize it to zero.
  598.      If the symbol is in some other section, we must generate a
  599.      R_68K_COPY reloc to tell the dynamic linker to copy the initial
  600.      value out of the dynamic object and into the runtime process
  601.      image.  We need to remember the offset into the .rela.bss section
  602.      we are going to use.  */
  603.   if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
  604.     {
  605.       asection *srel;
  606.  
  607.       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
  608.       BFD_ASSERT (srel != NULL);
  609.       srel->_raw_size += sizeof (Elf32_External_Rela);
  610.       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
  611.     }
  612.  
  613.   /* We need to figure out the alignment required for this symbol.  I
  614.      have no idea how ELF linkers handle this.  */
  615.   power_of_two = bfd_log2 (h->size);
  616.   if (power_of_two > 3)
  617.     power_of_two = 3;
  618.  
  619.   /* Apply the required alignment.  */
  620.   s->_raw_size = BFD_ALIGN (s->_raw_size,
  621.                 (bfd_size_type) (1 << power_of_two));
  622.   if (power_of_two > bfd_get_section_alignment (dynobj, s))
  623.     {
  624.       if (!bfd_set_section_alignment (dynobj, s, power_of_two))
  625.     return false;
  626.     }
  627.  
  628.   /* Define the symbol as being at this point in the section.  */
  629.   h->root.u.def.section = s;
  630.   h->root.u.def.value = s->_raw_size;
  631.  
  632.   /* Increment the section size to make room for the symbol.  */
  633.   s->_raw_size += h->size;
  634.  
  635.   return true;
  636. }
  637.  
  638. /* Set the sizes of the dynamic sections.  */
  639.  
  640. static boolean
  641. elf_m68k_size_dynamic_sections (output_bfd, info)
  642.      bfd *output_bfd;
  643.      struct bfd_link_info *info;
  644. {
  645.   bfd *dynobj;
  646.   asection *s;
  647.   boolean plt;
  648.   boolean relocs;
  649.   boolean reltext;
  650.  
  651.   dynobj = elf_hash_table (info)->dynobj;
  652.   BFD_ASSERT (dynobj != NULL);
  653.  
  654.   if (elf_hash_table (info)->dynamic_sections_created)
  655.     {
  656.       /* Set the contents of the .interp section to the interpreter.  */
  657.       if (!info->shared)
  658.     {
  659.       s = bfd_get_section_by_name (dynobj, ".interp");
  660.       BFD_ASSERT (s != NULL);
  661.       s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
  662.       s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  663.     }
  664.     }
  665.   else
  666.     {
  667.       /* We may have created entries in the .rela.got section.
  668.      However, if we are not creating the dynamic sections, we will
  669.      not actually use these entries.  Reset the size of .rela.got,
  670.      which will cause it to get stripped from the output file
  671.      below.  */
  672.       s = bfd_get_section_by_name (dynobj, ".rela.got");
  673.       if (s != NULL)
  674.     s->_raw_size = 0;
  675.     }
  676.  
  677.   /* The check_relocs and adjust_dynamic_symbol entry points have
  678.      determined the sizes of the various dynamic sections.  Allocate
  679.      memory for them.  */
  680.   plt = false;
  681.   relocs = false;
  682.   reltext = false;
  683.   for (s = dynobj->sections; s != NULL; s = s->next)
  684.     {
  685.       const char *name;
  686.       boolean strip;
  687.  
  688.       if ((s->flags & SEC_IN_MEMORY) == 0)
  689.     continue;
  690.  
  691.       /* It's OK to base decisions on the section name, because none
  692.      of the dynobj section names depend upon the input files.  */
  693.       name = bfd_get_section_name (dynobj, s);
  694.  
  695.       strip = false;
  696.  
  697.       if (strcmp (name, ".plt") == 0)
  698.     {
  699.       if (s->_raw_size == 0)
  700.         {
  701.           /* Strip this section if we don't need it; see the
  702.                  comment below.  */
  703.           strip = true;
  704.         }
  705.       else
  706.         {
  707.           /* Remember whether there is a PLT.  */
  708.           plt = true;
  709.         }
  710.     }
  711.       else if (strncmp (name, ".rela", 5) == 0)
  712.     {
  713.       if (s->_raw_size == 0)
  714.         {
  715.           /* If we don't need this section, strip it from the
  716.          output file.  This is mostly to handle .rela.bss and
  717.          .rela.plt.  We must create both sections in
  718.          create_dynamic_sections, because they must be created
  719.          before the linker maps input sections to output
  720.          sections.  The linker does that before
  721.          adjust_dynamic_symbol is called, and it is that
  722.          function which decides whether anything needs to go
  723.          into these sections.  */
  724.           strip = true;
  725.         }
  726.       else
  727.         {
  728.           asection *target;
  729.  
  730.           /* Remember whether there are any reloc sections other
  731.                  than .rela.plt.  */
  732.           if (strcmp (name, ".rela.plt") != 0)
  733.         {
  734.           relocs = true;
  735.  
  736.           /* If this relocation section applies to a read only
  737.              section, then we probably need a DT_TEXTREL
  738.              entry.  .rela.plt is actually associated with
  739.              .got.plt, which is never readonly.  */
  740.           target = bfd_get_section_by_name (output_bfd, name + 5);
  741.           if (target != NULL
  742.               && (target->flags & SEC_READONLY) != 0)
  743.             reltext = true;
  744.         }
  745.  
  746.           /* We use the reloc_count field as a counter if we need
  747.          to copy relocs into the output file.  */
  748.           s->reloc_count = 0;
  749.         }
  750.     }
  751.       else if (strncmp (name, ".got", 4) != 0)
  752.     {
  753.       /* It's not one of our sections, so don't allocate space.  */
  754.       continue;
  755.     }
  756.  
  757.       if (strip)
  758.     {
  759.       asection **spp;
  760.  
  761.       for (spp = &s->output_section->owner->sections;
  762.            *spp != s->output_section;
  763.            spp = &(*spp)->next)
  764.         ;
  765.       *spp = s->output_section->next;
  766.       --s->output_section->owner->section_count;
  767.  
  768.       continue;
  769.     }
  770.  
  771.       /* Allocate memory for the section contents.  */
  772.       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
  773.       if (s->contents == NULL && s->_raw_size != 0)
  774.     return false;
  775.     }
  776.  
  777.   if (elf_hash_table (info)->dynamic_sections_created)
  778.     {
  779.       /* Add some entries to the .dynamic section.  We fill in the
  780.      values later, in elf_m68k_finish_dynamic_sections, but we
  781.      must add the entries now so that we get the correct size for
  782.      the .dynamic section.  The DT_DEBUG entry is filled in by the
  783.      dynamic linker and used by the debugger.  */
  784.       if (!info->shared)
  785.     {
  786.       if (!bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
  787.         return false;
  788.     }
  789.  
  790.       if (plt)
  791.     {
  792.       if (!bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
  793.           || !bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
  794.           || !bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
  795.           || !bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
  796.         return false;
  797.     }
  798.  
  799.       if (relocs)
  800.     {
  801.       if (!bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
  802.           || !bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
  803.           || !bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
  804.                            sizeof (Elf32_External_Rela)))
  805.         return false;
  806.     }
  807.  
  808.       if (reltext)
  809.     {
  810.       if (!bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
  811.         return false;
  812.     }
  813.     }
  814.  
  815.   return true;
  816. }
  817.  
  818. /* Relocate an M68K ELF section.  */
  819.  
  820. static boolean
  821. elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
  822.                contents, relocs, local_syms, local_sections)
  823.      bfd *output_bfd;
  824.      struct bfd_link_info *info;
  825.      bfd *input_bfd;
  826.      asection *input_section;
  827.      bfd_byte *contents;
  828.      Elf_Internal_Rela *relocs;
  829.      Elf_Internal_Sym *local_syms;
  830.      asection **local_sections;
  831. {
  832.   bfd *dynobj;
  833.   Elf_Internal_Shdr *symtab_hdr;
  834.   struct elf_link_hash_entry **sym_hashes;
  835.   bfd_vma *local_got_offsets;
  836.   asection *sgot;
  837.   asection *splt;
  838.   asection *sreloc;
  839.   Elf_Internal_Rela *rel;
  840.   Elf_Internal_Rela *relend;
  841.  
  842.   dynobj = elf_hash_table (info)->dynobj;
  843.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  844.   sym_hashes = elf_sym_hashes (input_bfd);
  845.   local_got_offsets = elf_local_got_offsets (input_bfd);
  846.  
  847.   sgot = NULL;
  848.   splt = NULL;
  849.   sreloc = NULL;
  850.  
  851.   rel = relocs;
  852.   relend = relocs + input_section->reloc_count;
  853.   for (; rel < relend; rel++)
  854.     {
  855.       int r_type;
  856.       reloc_howto_type *howto;
  857.       unsigned long r_symndx;
  858.       struct elf_link_hash_entry *h;
  859.       Elf_Internal_Sym *sym;
  860.       asection *sec;
  861.       bfd_vma relocation;
  862.       bfd_reloc_status_type r;
  863.  
  864.       r_type = ELF32_R_TYPE (rel->r_info);
  865.       if (r_type < 0 || r_type >= (int) R_68K__max)
  866.     {
  867.       bfd_set_error (bfd_error_bad_value);
  868.       return false;
  869.     }
  870.       howto = howto_table + r_type;
  871.  
  872.       r_symndx = ELF32_R_SYM (rel->r_info);
  873.  
  874.       if (info->relocateable)
  875.     {
  876.       /* This is a relocateable link.  We don't have to change
  877.          anything, unless the reloc is against a section symbol,
  878.          in which case we have to adjust according to where the
  879.          section symbol winds up in the output section.  */
  880.       if (r_symndx < symtab_hdr->sh_info)
  881.         {
  882.           sym = local_syms + r_symndx;
  883.           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  884.         {
  885.           sec = local_sections[r_symndx];
  886.           rel->r_addend += sec->output_offset + sym->st_value;
  887.         }
  888.         }
  889.  
  890.       continue;
  891.     }
  892.  
  893.       /* This is a final link.  */
  894.       h = NULL;
  895.       sym = NULL;
  896.       sec = NULL;
  897.       if (r_symndx < symtab_hdr->sh_info)
  898.     {
  899.       sym = local_syms + r_symndx;
  900.       sec = local_sections[r_symndx];
  901.       relocation = (sec->output_section->vma
  902.             + sec->output_offset
  903.             + sym->st_value);
  904.     }
  905.       else
  906.     {
  907.       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  908.       while (h->root.type == bfd_link_hash_indirect
  909.          || h->root.type == bfd_link_hash_warning)
  910.         h = (struct elf_link_hash_entry *) h->root.u.i.link;
  911.       if (h->root.type == bfd_link_hash_defined
  912.           || h->root.type == bfd_link_hash_defweak)
  913.         {
  914.           sec = h->root.u.def.section;
  915.           if (((r_type == R_68K_PLT8
  916.             || r_type == R_68K_PLT16
  917.             || r_type == R_68K_PLT32
  918.             || r_type == R_68K_PLT8O
  919.             || r_type == R_68K_PLT16O
  920.             || r_type == R_68K_PLT32O)
  921.            && h->plt_offset != (bfd_vma) -1)
  922.           || ((r_type == R_68K_GOT8O
  923.                || r_type == R_68K_GOT16O
  924.                || r_type == R_68K_GOT32O
  925.                || ((r_type == R_68K_GOT8
  926.                 || r_type == R_68K_GOT16
  927.                 || r_type == R_68K_GOT32)
  928.                && strcmp (h->root.root.string,
  929.                       "_GLOBAL_OFFSET_TABLE_") != 0))
  930.               && elf_hash_table (info)->dynamic_sections_created
  931.               && (! info->shared
  932.               || ! info->symbolic
  933.               || (h->elf_link_hash_flags
  934.                   & ELF_LINK_HASH_DEF_REGULAR) == 0))
  935.           || (info->shared
  936.               && (! info->symbolic
  937.               || (h->elf_link_hash_flags
  938.                   & ELF_LINK_HASH_DEF_REGULAR) == 0)
  939.               && (input_section->flags & SEC_ALLOC) != 0
  940.               && (r_type == R_68K_8
  941.               || r_type == R_68K_16
  942.               || r_type == R_68K_32
  943.               || r_type == R_68K_PC8
  944.               || r_type == R_68K_PC16
  945.               || r_type == R_68K_PC32)))
  946.         {
  947.           /* In these cases, we don't need the relocation
  948.              value.  We check specially because in some
  949.              obscure cases sec->output_section will be NULL.  */
  950.           relocation = 0;
  951.         }
  952.           else
  953.         relocation = (h->root.u.def.value
  954.                   + sec->output_section->vma
  955.                   + sec->output_offset);
  956.         }
  957.       else if (h->root.type == bfd_link_hash_undefweak)
  958.         relocation = 0;
  959.       else if (info->shared && !info->symbolic)
  960.         relocation = 0;
  961.       else
  962.         {
  963.           if (!(info->callbacks->undefined_symbol
  964.             (info, h->root.root.string, input_bfd,
  965.              input_section, rel->r_offset)))
  966.         return false;
  967.           relocation = 0;
  968.         }
  969.     }
  970.  
  971.       switch (r_type)
  972.     {
  973.     case R_68K_GOT8:
  974.     case R_68K_GOT16:
  975.     case R_68K_GOT32:
  976.       /* Relocation is to the address of the entry for this symbol
  977.          in the global offset table.  */
  978.       if (h != NULL
  979.           && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  980.         break;
  981.       /* Fall through.  */
  982.     case R_68K_GOT8O:
  983.     case R_68K_GOT16O:
  984.     case R_68K_GOT32O:
  985.       /* Relocation is the offset of the entry for this symbol in
  986.          the global offset table.  */
  987.  
  988.       {
  989.         bfd_vma off;
  990.         
  991.         if (sgot == NULL)
  992.           {
  993.         sgot = bfd_get_section_by_name (dynobj, ".got");
  994.         BFD_ASSERT (sgot != NULL);
  995.           }
  996.  
  997.         if (h != NULL)
  998.           {
  999.         off = h->got_offset;
  1000.         BFD_ASSERT (off != (bfd_vma) -1);
  1001.  
  1002.         if (!elf_hash_table (info)->dynamic_sections_created
  1003.             || (info->shared
  1004.             && info->symbolic
  1005.             && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
  1006.           {
  1007.             /* This is actually a static link, or it is a
  1008.                -Bsymbolic link and the symbol is defined
  1009.                locally.  We must initialize this entry in the
  1010.                global offset table.  Since the offset must
  1011.                always be a multiple of 4, we use the least
  1012.                significant bit to record whether we have
  1013.                initialized it already.
  1014.  
  1015.                When doing a dynamic link, we create a .rela.got
  1016.                relocation entry to initialize the value.  This
  1017.                is done in the finish_dynamic_symbol routine.  */
  1018.             if ((off & 1) != 0)
  1019.               off &= ~1;
  1020.             else
  1021.               {
  1022.             bfd_put_32 (output_bfd, relocation,
  1023.                     sgot->contents + off);
  1024.             h->got_offset |= 1;
  1025.               }
  1026.           }
  1027.           }
  1028.         else
  1029.           {
  1030.         BFD_ASSERT (local_got_offsets != NULL
  1031.                 && local_got_offsets[r_symndx] != (bfd_vma) -1);
  1032.  
  1033.         off = local_got_offsets[r_symndx];
  1034.  
  1035.         /* The offset must always be a multiple of 4.  We use
  1036.            the least significant bit to record whether we have
  1037.            already generated the necessary reloc.  */
  1038.         if ((off & 1) != 0)
  1039.           off &= ~1;
  1040.         else
  1041.           {
  1042.             bfd_put_32 (output_bfd, relocation, sgot->contents + off);
  1043.  
  1044.             if (info->shared)
  1045.               {
  1046.             asection *srelgot;
  1047.             Elf_Internal_Rela outrel;
  1048.  
  1049.             srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
  1050.             BFD_ASSERT (srelgot != NULL);
  1051.  
  1052.             outrel.r_offset = (sgot->output_section->vma
  1053.                        + sgot->output_offset
  1054.                        + off);
  1055.             outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
  1056.             outrel.r_addend = relocation;
  1057.             bfd_elf32_swap_reloca_out (output_bfd, &outrel,
  1058.                            (((Elf32_External_Rela *)
  1059.                              srelgot->contents)
  1060.                             + srelgot->reloc_count));
  1061.             ++srelgot->reloc_count;
  1062.               }
  1063.  
  1064.             local_got_offsets[r_symndx] |= 1;
  1065.           }
  1066.           }
  1067.  
  1068.         relocation = sgot->output_offset + off;
  1069.         if (r_type == R_68K_GOT8O
  1070.         || r_type == R_68K_GOT16O
  1071.         || r_type == R_68K_GOT32O)
  1072.           {
  1073.         /* This relocation does not use the addend.  */
  1074.         rel->r_addend = 0;
  1075.           }
  1076.         else
  1077.           relocation += sgot->output_section->vma;
  1078.       }
  1079.       break;
  1080.  
  1081.     case R_68K_PLT8:
  1082.     case R_68K_PLT16:
  1083.     case R_68K_PLT32:
  1084.       /* Relocation is to the entry for this symbol in the
  1085.          procedure linkage table.  */
  1086.  
  1087.       /* Resolve a PLTxx reloc against a local symbol directly,
  1088.          without using the procedure linkage table.  */
  1089.       if (h == NULL)
  1090.         break;
  1091.  
  1092.       if (h->plt_offset == (bfd_vma) -1)
  1093.         {
  1094.           /* We didn't make a PLT entry for this symbol.  This
  1095.          happens when statically linking PIC code, or when
  1096.          using -Bsymbolic.  */
  1097.           break;
  1098.         }
  1099.  
  1100.       if (splt == NULL)
  1101.         {
  1102.           splt = bfd_get_section_by_name (dynobj, ".plt");
  1103.           BFD_ASSERT (splt != NULL);
  1104.         }
  1105.  
  1106.       relocation = (splt->output_section->vma
  1107.             + splt->output_offset
  1108.             + h->plt_offset);
  1109.       break;
  1110.  
  1111.     case R_68K_PLT8O:
  1112.     case R_68K_PLT16O:
  1113.     case R_68K_PLT32O:
  1114.       /* Relocation is the offset of the entry for this symbol in
  1115.          the procedure linkage table.  */
  1116.       BFD_ASSERT (h != NULL && h->plt_offset == (bfd_vma) -1);
  1117.  
  1118.       if (splt == NULL)
  1119.         {
  1120.           splt = bfd_get_section_by_name (dynobj, ".plt");
  1121.           BFD_ASSERT (splt != NULL);
  1122.         }
  1123.  
  1124.       relocation = h->plt_offset;
  1125.  
  1126.       /* This relocation does not use the addend.  */
  1127.       rel->r_addend = 0;
  1128.  
  1129.       break;
  1130.  
  1131.     case R_68K_PC8:
  1132.     case R_68K_PC16:
  1133.     case R_68K_PC32:
  1134.       if (h == NULL)
  1135.         break;
  1136.       /* Fall through.  */
  1137.     case R_68K_8:
  1138.     case R_68K_16:
  1139.     case R_68K_32:
  1140.       if (info->shared
  1141.           && (input_section->flags & SEC_ALLOC) != 0
  1142.           && ((r_type != R_68K_PC8
  1143.            && r_type != R_68K_PC16
  1144.            && r_type != R_68K_PC32)
  1145.           || (!info->symbolic
  1146.               || (h->elf_link_hash_flags
  1147.               & ELF_LINK_HASH_DEF_REGULAR) == 0)))
  1148.         {
  1149.           Elf_Internal_Rela outrel;
  1150.           int relocate;
  1151.  
  1152.           /* When generating a shared object, these relocations
  1153.          are copied into the output file to be resolved at run
  1154.          time.  */
  1155.  
  1156.           if (sreloc == NULL)
  1157.         {
  1158.           const char *name;
  1159.  
  1160.           name = (bfd_elf_string_from_elf_section
  1161.               (input_bfd,
  1162.                elf_elfheader (input_bfd)->e_shstrndx,
  1163.                elf_section_data (input_section)->rel_hdr.sh_name));
  1164.           if (name == NULL)
  1165.             return false;
  1166.  
  1167.           BFD_ASSERT (strncmp (name, ".rela", 5) == 0
  1168.                   && strcmp (bfd_get_section_name (input_bfd,
  1169.                                    input_section),
  1170.                      name + 5) == 0);
  1171.  
  1172.           sreloc = bfd_get_section_by_name (dynobj, name);
  1173.           BFD_ASSERT (sreloc != NULL);
  1174.         }
  1175.  
  1176.           outrel.r_offset = (rel->r_offset
  1177.                  + input_section->output_section->vma
  1178.                  + input_section->output_offset);
  1179.           if (h != NULL
  1180.           && (! info->symbolic
  1181.               || (h->elf_link_hash_flags
  1182.               & ELF_LINK_HASH_DEF_REGULAR) == 0))
  1183.         {
  1184.           BFD_ASSERT (h->dynindx != -1);
  1185.           relocate = false;
  1186.           outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  1187.           outrel.r_addend = relocation + rel->r_addend;
  1188.         }
  1189.           else
  1190.         {
  1191.           if (r_type == R_68K_32)
  1192.             {
  1193.               relocate = true;
  1194.               outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
  1195.               outrel.r_addend = relocation + rel->r_addend;
  1196.             }
  1197.           else
  1198.             {
  1199.               long indx;
  1200.  
  1201.               if (h == NULL)
  1202.             sec = local_sections[r_symndx];
  1203.               else
  1204.             {
  1205.               BFD_ASSERT (h->root.type == bfd_link_hash_defined
  1206.                       || (h->root.type
  1207.                       == bfd_link_hash_defweak));
  1208.               sec = h->root.u.def.section;
  1209.             }
  1210.               if (sec != NULL && bfd_is_abs_section (sec))
  1211.             indx = 0;
  1212.               else if (sec == NULL || sec->owner == NULL)
  1213.             {
  1214.               bfd_set_error (bfd_error_bad_value);
  1215.               return false;
  1216.             }
  1217.               else
  1218.             {
  1219.               asection *osec;
  1220.  
  1221.               osec = sec->output_section;
  1222.               indx = elf_section_data (osec)->dynindx;
  1223.               if (indx == 0)
  1224.                 abort ();
  1225.             }
  1226.  
  1227.               relocate = false;
  1228.               outrel.r_info = ELF32_R_INFO (indx, r_type);
  1229.               outrel.r_addend = relocation + rel->r_addend;
  1230.             }
  1231.         }
  1232.  
  1233.           bfd_elf32_swap_reloca_out (output_bfd, &outrel,
  1234.                      (((Elf32_External_Rela *)
  1235.                        sreloc->contents)
  1236.                       + sreloc->reloc_count));
  1237.           ++sreloc->reloc_count;
  1238.  
  1239.           /* This reloc will be computed at runtime, so there's no
  1240.                  need to do anything now, except for R_68K_32
  1241.                  relocations that have been turned into
  1242.                  R_68K_RELATIVE.  */
  1243.           if (!relocate)
  1244.         continue;
  1245.         }
  1246.  
  1247.       break;
  1248.  
  1249.     default:
  1250.       break;
  1251.     }
  1252.  
  1253.       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1254.                     contents, rel->r_offset,
  1255.                     relocation, rel->r_addend);
  1256.  
  1257.       if (r != bfd_reloc_ok)
  1258.     {
  1259.       switch (r)
  1260.         {
  1261.         default:
  1262.         case bfd_reloc_outofrange:
  1263.           abort ();
  1264.         case bfd_reloc_overflow:
  1265.           {
  1266.         const char *name;
  1267.  
  1268.         if (h != NULL)
  1269.           name = h->root.root.string;
  1270.         else
  1271.           {
  1272.             name = bfd_elf_string_from_elf_section (input_bfd,
  1273.                                 symtab_hdr->sh_link,
  1274.                                 sym->st_name);
  1275.             if (name == NULL)
  1276.               return false;
  1277.             if (*name == '\0')
  1278.               name = bfd_section_name (input_bfd, sec);
  1279.           }
  1280.         if (!(info->callbacks->reloc_overflow
  1281.               (info, name, howto->name, (bfd_vma) 0,
  1282.                input_bfd, input_section, rel->r_offset)))
  1283.           return false;
  1284.           }
  1285.           break;
  1286.         }
  1287.     }
  1288.     }
  1289.  
  1290.   return true;
  1291. }
  1292.  
  1293. /* Finish up dynamic symbol handling.  We set the contents of various
  1294.    dynamic sections here.  */
  1295.  
  1296. static boolean
  1297. elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
  1298.      bfd *output_bfd;
  1299.      struct bfd_link_info *info;
  1300.      struct elf_link_hash_entry *h;
  1301.      Elf_Internal_Sym *sym;
  1302. {
  1303.   bfd *dynobj;
  1304.  
  1305.   dynobj = elf_hash_table (info)->dynobj;
  1306.  
  1307.   if (h->plt_offset != (bfd_vma) -1)
  1308.     {
  1309.       asection *splt;
  1310.       asection *sgot;
  1311.       asection *srela;
  1312.       bfd_vma plt_index;
  1313.       bfd_vma got_offset;
  1314.       Elf_Internal_Rela rela;
  1315.  
  1316.       /* This symbol has an entry in the procedure linkage table.  Set
  1317.      it up.  */
  1318.  
  1319.       BFD_ASSERT (h->dynindx != -1);
  1320.  
  1321.       splt = bfd_get_section_by_name (dynobj, ".plt");
  1322.       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
  1323.       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
  1324.       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
  1325.  
  1326.       /* Get the index in the procedure linkage table which
  1327.      corresponds to this symbol.  This is the index of this symbol
  1328.      in all the symbols for which we are making plt entries.  The
  1329.      first entry in the procedure linkage table is reserved.  */
  1330.       plt_index = h->plt_offset / PLT_ENTRY_SIZE - 1;
  1331.  
  1332.       /* Get the offset into the .got table of the entry that
  1333.      corresponds to this function.  Each .got entry is 4 bytes.
  1334.      The first three are reserved.  */
  1335.       got_offset = (plt_index + 3) * 4;
  1336.  
  1337.       /* Fill in the entry in the procedure linkage table.  */
  1338.       memcpy (splt->contents + h->plt_offset, elf_m68k_plt_entry,
  1339.           PLT_ENTRY_SIZE);
  1340.       /* The offset is relative to the first extension word.  */
  1341.       bfd_put_32 (output_bfd,
  1342.           (sgot->output_section->vma
  1343.            + sgot->output_offset
  1344.            + got_offset
  1345.            - (splt->output_section->vma
  1346.               + h->plt_offset + 2)),
  1347.           splt->contents + h->plt_offset + 4);
  1348.  
  1349.       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
  1350.           splt->contents + h->plt_offset + 10);
  1351.       bfd_put_32 (output_bfd, - (h->plt_offset + 16),
  1352.           splt->contents + h->plt_offset + 16);
  1353.  
  1354.       /* Fill in the entry in the global offset table.  */
  1355.       bfd_put_32 (output_bfd,
  1356.           (splt->output_section->vma
  1357.            + splt->output_offset
  1358.            + h->plt_offset
  1359.            + 8),
  1360.           sgot->contents + got_offset);
  1361.  
  1362.       /* Fill in the entry in the .rela.plt section.  */
  1363.       rela.r_offset = (sgot->output_section->vma
  1364.                + sgot->output_offset
  1365.                + got_offset);
  1366.       rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
  1367.       rela.r_addend = 0;
  1368.       bfd_elf32_swap_reloca_out (output_bfd, &rela,
  1369.                  ((Elf32_External_Rela *) srela->contents
  1370.                   + plt_index));
  1371.  
  1372.       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
  1373.     {
  1374.       /* Mark the symbol as undefined, rather than as defined in
  1375.          the .plt section.  Leave the value alone.  */
  1376.       sym->st_shndx = SHN_UNDEF;
  1377.     }
  1378.     }
  1379.  
  1380.   if (h->got_offset != (bfd_vma) -1)
  1381.     {
  1382.       asection *sgot;
  1383.       asection *srela;
  1384.       Elf_Internal_Rela rela;
  1385.  
  1386.       /* This symbol has an entry in the global offset table.  Set it
  1387.      up.  */
  1388.       
  1389.       BFD_ASSERT (h->dynindx != -1);
  1390.  
  1391.       sgot = bfd_get_section_by_name (dynobj, ".got");
  1392.       srela = bfd_get_section_by_name (dynobj, ".rela.got");
  1393.       BFD_ASSERT (sgot != NULL && srela != NULL);
  1394.  
  1395.       rela.r_offset = (sgot->output_section->vma
  1396.                + sgot->output_offset
  1397.                + (h->got_offset &~ 1));
  1398.  
  1399.       /* If this is a -Bsymbolic link, and the symbol is defined
  1400.      locally, we just want to emit a RELATIVE reloc.  The entry in
  1401.      the global offset table will already have been initialized in
  1402.      the relocate_section function.  */
  1403.       if (info->shared
  1404.       && info->symbolic
  1405.       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
  1406.     {
  1407.       rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
  1408.       rela.r_addend = bfd_get_32 (output_bfd,
  1409.                       sgot->contents + (h->got_offset & ~1));
  1410.     }
  1411.       else
  1412.     {
  1413.       bfd_put_32 (output_bfd, (bfd_vma) 0,
  1414.               sgot->contents + (h->got_offset & ~1));
  1415.       rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
  1416.       rela.r_addend = 0;
  1417.     }
  1418.  
  1419.       bfd_elf32_swap_reloca_out (output_bfd, &rela,
  1420.                  ((Elf32_External_Rela *) srela->contents
  1421.                   + srela->reloc_count));
  1422.       ++srela->reloc_count;
  1423.     }
  1424.  
  1425.   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
  1426.     {
  1427.       asection *s;
  1428.       Elf_Internal_Rela rela;
  1429.  
  1430.       /* This symbol needs a copy reloc.  Set it up.  */
  1431.  
  1432.       BFD_ASSERT (h->dynindx != -1
  1433.           && (h->root.type == bfd_link_hash_defined
  1434.               || h->root.type == bfd_link_hash_defweak));
  1435.  
  1436.       s = bfd_get_section_by_name (h->root.u.def.section->owner,
  1437.                    ".rela.bss");
  1438.       BFD_ASSERT (s != NULL);
  1439.  
  1440.       rela.r_offset = (h->root.u.def.value
  1441.                + h->root.u.def.section->output_section->vma
  1442.                + h->root.u.def.section->output_offset);
  1443.       rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
  1444.       rela.r_addend = 0;
  1445.       bfd_elf32_swap_reloca_out (output_bfd, &rela,
  1446.                  ((Elf32_External_Rela *) s->contents
  1447.                   + s->reloc_count));
  1448.       ++s->reloc_count;
  1449.     }
  1450.  
  1451.   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
  1452.   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
  1453.       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  1454.     sym->st_shndx = SHN_ABS;
  1455.  
  1456.   return true;
  1457. }
  1458.  
  1459. /* Finish up the dynamic sections.  */
  1460.  
  1461. static boolean
  1462. elf_m68k_finish_dynamic_sections (output_bfd, info)
  1463.      bfd *output_bfd;
  1464.      struct bfd_link_info *info;
  1465. {
  1466.   bfd *dynobj;
  1467.   asection *sgot;
  1468.   asection *sdyn;
  1469.  
  1470.   dynobj = elf_hash_table (info)->dynobj;
  1471.  
  1472.   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
  1473.   BFD_ASSERT (sgot != NULL);
  1474.   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
  1475.  
  1476.   if (elf_hash_table (info)->dynamic_sections_created)
  1477.     {
  1478.       asection *splt;
  1479.       Elf32_External_Dyn *dyncon, *dynconend;
  1480.  
  1481.       splt = bfd_get_section_by_name (dynobj, ".plt");
  1482.       BFD_ASSERT (splt != NULL && sdyn != NULL);
  1483.  
  1484.       dyncon = (Elf32_External_Dyn *) sdyn->contents;
  1485.       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
  1486.       for (; dyncon < dynconend; dyncon++)
  1487.     {
  1488.       Elf_Internal_Dyn dyn;
  1489.       const char *name;
  1490.       asection *s;
  1491.  
  1492.       bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  1493.  
  1494.       switch (dyn.d_tag)
  1495.         {
  1496.         default:
  1497.           break;
  1498.  
  1499.         case DT_PLTGOT:
  1500.           name = ".got";
  1501.           goto get_vma;
  1502.         case DT_JMPREL:
  1503.           name = ".rela.plt";
  1504.         get_vma:
  1505.           s = bfd_get_section_by_name (output_bfd, name);
  1506.           BFD_ASSERT (s != NULL);
  1507.           dyn.d_un.d_ptr = s->vma;
  1508.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  1509.           break;
  1510.  
  1511.         case DT_PLTRELSZ:
  1512.           s = bfd_get_section_by_name (output_bfd, ".rela.plt");
  1513.           BFD_ASSERT (s != NULL);
  1514.           if (s->_cooked_size != 0)
  1515.         dyn.d_un.d_val = s->_cooked_size;
  1516.           else
  1517.         dyn.d_un.d_val = s->_raw_size;
  1518.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  1519.           break;
  1520.  
  1521.         case DT_RELASZ:
  1522.           /* The procedure linkage table relocs (DT_JMPREL) should
  1523.          not be included in the overall relocs (DT_RELA).
  1524.          Therefore, we override the DT_RELASZ entry here to
  1525.          make it not include the JMPREL relocs.  Since the
  1526.          linker script arranges for .rela.plt to follow all
  1527.          other relocation sections, we don't have to worry
  1528.          about changing the DT_RELA entry.  */
  1529.           s = bfd_get_section_by_name (output_bfd, ".rela.plt");
  1530.           if (s != NULL)
  1531.         {
  1532.           if (s->_cooked_size != 0)
  1533.             dyn.d_un.d_val -= s->_cooked_size;
  1534.           else
  1535.             dyn.d_un.d_val -= s->_raw_size;
  1536.         }
  1537.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  1538.           break;
  1539.         }
  1540.     }
  1541.  
  1542.       /* Fill in the first entry in the procedure linkage table.  */
  1543.       if (splt->_raw_size > 0)
  1544.     {
  1545.       memcpy (splt->contents, elf_m68k_plt0_entry, PLT_ENTRY_SIZE);
  1546.       bfd_put_32 (output_bfd,
  1547.               (sgot->output_section->vma
  1548.                + sgot->output_offset + 4
  1549.                - (splt->output_section->vma + 2)),
  1550.               splt->contents + 4);
  1551.       bfd_put_32 (output_bfd,
  1552.               (sgot->output_section->vma
  1553.                + sgot->output_offset + 8
  1554.                - (splt->output_section->vma + 10)),
  1555.               splt->contents + 12);
  1556.     }
  1557.  
  1558.       elf_section_data (splt->output_section)->this_hdr.sh_entsize
  1559.     = PLT_ENTRY_SIZE;
  1560.     }
  1561.  
  1562.   /* Fill in the first three entries in the global offset table.  */
  1563.   if (sgot->_raw_size > 0)
  1564.     {
  1565.       if (sdyn == NULL)
  1566.     bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
  1567.       else
  1568.     bfd_put_32 (output_bfd,
  1569.             sdyn->output_section->vma + sdyn->output_offset,
  1570.             sgot->contents);
  1571.       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
  1572.       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
  1573.     }
  1574.  
  1575.   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
  1576.  
  1577.   return true;
  1578. }
  1579.  
  1580. #define TARGET_BIG_SYM            bfd_elf32_m68k_vec
  1581. #define TARGET_BIG_NAME            "elf32-m68k"
  1582. #define ELF_MACHINE_CODE        EM_68K
  1583. #define ELF_MAXPAGESIZE            0x2000
  1584. #define elf_backend_create_dynamic_sections \
  1585.                     _bfd_elf_create_dynamic_sections
  1586. #define elf_backend_check_relocs    elf_m68k_check_relocs
  1587. #define elf_backend_adjust_dynamic_symbol \
  1588.                     elf_m68k_adjust_dynamic_symbol
  1589. #define elf_backend_size_dynamic_sections \
  1590.                     elf_m68k_size_dynamic_sections
  1591. #define elf_backend_relocate_section    elf_m68k_relocate_section
  1592. #define elf_backend_finish_dynamic_symbol \
  1593.                     elf_m68k_finish_dynamic_symbol
  1594. #define elf_backend_finish_dynamic_sections \
  1595.                     elf_m68k_finish_dynamic_sections
  1596. #define elf_backend_want_got_plt 1
  1597. #define elf_backend_plt_readonly 1
  1598. #define elf_backend_want_plt_sym 0
  1599.  
  1600. #include "elf32-target.h"
  1601.