Back to index

cell-binutils  2.17cvs20070401
elf32-cris.c
Go to the documentation of this file.
00001 /* CRIS-specific support for 32-bit ELF.
00002    Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
00003    Free Software Foundation, Inc.
00004    Contributed by Axis Communications AB.
00005    Written by Hans-Peter Nilsson, based on elf32-fr30.c
00006    PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c.
00007 
00008    This file is part of BFD, the Binary File Descriptor library.
00009 
00010    This program is free software; you can redistribute it and/or modify
00011    it under the terms of the GNU General Public License as published by
00012    the Free Software Foundation; either version 2 of the License, or
00013    (at your option) any later version.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License
00021    along with this program; if not, write to the Free Software
00022    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00023 
00024 #include "bfd.h"
00025 #include "sysdep.h"
00026 #include "libbfd.h"
00027 #include "elf-bfd.h"
00028 #include "elf/cris.h"
00029 
00030 /* Forward declarations.  */
00031 static reloc_howto_type * cris_reloc_type_lookup
00032   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
00033 
00034 static void cris_info_to_howto_rela
00035   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
00036 
00037 static bfd_reloc_status_type cris_elf_pcrel_reloc
00038   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
00039 
00040 static bfd_boolean cris_elf_grok_prstatus
00041   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
00042 
00043 static bfd_boolean cris_elf_grok_psinfo
00044   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
00045 
00046 static bfd_boolean cris_elf_relocate_section
00047   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
00048           Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
00049 
00050 static bfd_reloc_status_type cris_final_link_relocate
00051   PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
00052           Elf_Internal_Rela *, bfd_vma));
00053 
00054 static bfd_boolean cris_elf_object_p PARAMS ((bfd *));
00055 
00056 static void cris_elf_final_write_processing PARAMS ((bfd *, bfd_boolean));
00057 
00058 static bfd_boolean cris_elf_set_mach_from_flags
00059   PARAMS ((bfd *, unsigned long int));
00060 
00061 static bfd_boolean cris_elf_print_private_bfd_data PARAMS ((bfd *, PTR));
00062 
00063 static bfd_boolean cris_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
00064 static bfd_boolean cris_elf_copy_private_bfd_data PARAMS ((bfd *, bfd *));
00065 
00066 struct elf_cris_link_hash_entry;
00067 static bfd_boolean elf_cris_discard_excess_dso_dynamics
00068   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
00069 static bfd_boolean elf_cris_discard_excess_program_dynamics
00070   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
00071 static bfd_boolean elf_cris_adjust_gotplt_to_got
00072   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
00073 static bfd_boolean elf_cris_try_fold_plt_to_got
00074   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
00075 static struct bfd_hash_entry *elf_cris_link_hash_newfunc
00076   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
00077 static struct bfd_link_hash_table *elf_cris_link_hash_table_create
00078   PARAMS ((bfd *));
00079 static bfd_boolean elf_cris_adjust_dynamic_symbol
00080   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
00081 static bfd_boolean cris_elf_check_relocs
00082   PARAMS ((bfd *, struct bfd_link_info *, asection *,
00083           const Elf_Internal_Rela *));
00084 
00085 static bfd_boolean elf_cris_size_dynamic_sections
00086   PARAMS ((bfd *, struct bfd_link_info *));
00087 static bfd_boolean elf_cris_finish_dynamic_symbol
00088   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
00089           Elf_Internal_Sym *));
00090 static bfd_boolean elf_cris_finish_dynamic_sections
00091   PARAMS ((bfd *, struct bfd_link_info *));
00092 static void elf_cris_hide_symbol
00093   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
00094 static enum elf_reloc_type_class elf_cris_reloc_type_class
00095   PARAMS ((const Elf_Internal_Rela *));
00096 
00097 static reloc_howto_type cris_elf_howto_table [] =
00098 {
00099   /* This reloc does nothing.  */
00100   HOWTO (R_CRIS_NONE,              /* type */
00101         0,                  /* rightshift */
00102         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00103         32,                 /* bitsize */
00104         FALSE,                     /* pc_relative */
00105         0,                  /* bitpos */
00106         complain_overflow_bitfield, /* complain_on_overflow */
00107         bfd_elf_generic_reloc,     /* special_function */
00108         "R_CRIS_NONE",             /* name */
00109         FALSE,                     /* partial_inplace */
00110         0,                  /* src_mask */
00111         0,                  /* dst_mask */
00112         FALSE),             /* pcrel_offset */
00113 
00114   /* An 8 bit absolute relocation.  */
00115   HOWTO (R_CRIS_8,          /* type */
00116         0,                  /* rightshift */
00117         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00118         8,                  /* bitsize */
00119         FALSE,                     /* pc_relative */
00120         0,                  /* bitpos */
00121         complain_overflow_bitfield, /* complain_on_overflow */
00122         bfd_elf_generic_reloc,     /* special_function */
00123         "R_CRIS_8",         /* name */
00124         FALSE,                     /* partial_inplace */
00125         0x0000,             /* src_mask */
00126         0x00ff,             /* dst_mask */
00127         FALSE),             /* pcrel_offset */
00128 
00129   /* A 16 bit absolute relocation.  */
00130   HOWTO (R_CRIS_16,         /* type */
00131         0,                  /* rightshift */
00132         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00133         16,                 /* bitsize */
00134         FALSE,                     /* pc_relative */
00135         0,                  /* bitpos */
00136         complain_overflow_bitfield, /* complain_on_overflow */
00137         bfd_elf_generic_reloc,     /* special_function */
00138         "R_CRIS_16",        /* name */
00139         FALSE,                     /* partial_inplace */
00140         0x00000000,         /* src_mask */
00141         0x0000ffff,         /* dst_mask */
00142         FALSE),             /* pcrel_offset */
00143 
00144   /* A 32 bit absolute relocation.  */
00145   HOWTO (R_CRIS_32,         /* type */
00146         0,                  /* rightshift */
00147         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00148         32,                 /* bitsize */
00149         FALSE,                     /* pc_relative */
00150         0,                  /* bitpos */
00151         /* We don't want overflow complaints for 64-bit vma builds
00152            for e.g. sym+0x40000000 (or actually sym-0xc0000000 in
00153            32-bit ELF) where sym=0xc0001234.
00154            Don't do this for the PIC relocs, as we don't expect to
00155            see them with large offsets.  */
00156         complain_overflow_dont, /* complain_on_overflow */
00157         bfd_elf_generic_reloc,     /* special_function */
00158         "R_CRIS_32",        /* name */
00159         FALSE,                     /* partial_inplace */
00160         0x00000000,         /* src_mask */
00161         0xffffffff,         /* dst_mask */
00162         FALSE),             /* pcrel_offset */
00163 
00164   /* An 8 bit PC-relative relocation.  */
00165   HOWTO (R_CRIS_8_PCREL,    /* type */
00166         0,                  /* rightshift */
00167         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00168         8,                  /* bitsize */
00169         TRUE,               /* pc_relative */
00170         0,                  /* bitpos */
00171         complain_overflow_bitfield, /* complain_on_overflow */
00172         cris_elf_pcrel_reloc,      /* special_function */
00173         "R_CRIS_8_PCREL",   /* name */
00174         FALSE,                     /* partial_inplace */
00175         0x0000,             /* src_mask */
00176         0x00ff,             /* dst_mask */
00177         TRUE),                     /* pcrel_offset */
00178 
00179   /* A 16 bit PC-relative relocation.  */
00180   HOWTO (R_CRIS_16_PCREL,   /* type */
00181         0,                  /* rightshift */
00182         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00183         16,                 /* bitsize */
00184         TRUE,               /* pc_relative */
00185         0,                  /* bitpos */
00186         complain_overflow_bitfield, /* complain_on_overflow */
00187         cris_elf_pcrel_reloc,      /* special_function */
00188         "R_CRIS_16_PCREL",  /* name */
00189         FALSE,                     /* partial_inplace */
00190         0x00000000,         /* src_mask */
00191         0x0000ffff,         /* dst_mask */
00192         TRUE),                     /* pcrel_offset */
00193 
00194   /* A 32 bit PC-relative relocation.  */
00195   HOWTO (R_CRIS_32_PCREL,   /* type */
00196         0,                  /* rightshift */
00197         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00198         32,                 /* bitsize */
00199         TRUE,               /* pc_relative */
00200         0,                  /* bitpos */
00201         complain_overflow_bitfield, /* complain_on_overflow */
00202         cris_elf_pcrel_reloc,      /* special_function */
00203         "R_CRIS_32_PCREL",  /* name */
00204         FALSE,                     /* partial_inplace */
00205         0x00000000,         /* src_mask */
00206         0xffffffff,         /* dst_mask */
00207         TRUE),                     /* pcrel_offset */
00208 
00209   /* GNU extension to record C++ vtable hierarchy.  */
00210   HOWTO (R_CRIS_GNU_VTINHERIT,     /* type */
00211         0,                  /* rightshift */
00212         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00213         0,                  /* bitsize */
00214         FALSE,                     /* pc_relative */
00215         0,                  /* bitpos */
00216         complain_overflow_dont, /* complain_on_overflow */
00217         NULL,               /* special_function */
00218         "R_CRIS_GNU_VTINHERIT", /* name */
00219         FALSE,                     /* partial_inplace */
00220         0,                  /* src_mask */
00221         0,                  /* dst_mask */
00222         FALSE),             /* pcrel_offset */
00223 
00224   /* GNU extension to record C++ vtable member usage.  */
00225   HOWTO (R_CRIS_GNU_VTENTRY,       /* type */
00226         0,                  /* rightshift */
00227         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00228         0,                  /* bitsize */
00229         FALSE,                     /* pc_relative */
00230         0,                  /* bitpos */
00231         complain_overflow_dont, /* complain_on_overflow */
00232         _bfd_elf_rel_vtable_reloc_fn,     /* special_function */
00233         "R_CRIS_GNU_VTENTRY",       /* name */
00234         FALSE,                     /* partial_inplace */
00235         0,                  /* src_mask */
00236         0,                  /* dst_mask */
00237         FALSE),             /* pcrel_offset */
00238 
00239   /* This is used only by the dynamic linker.  The symbol should exist
00240      both in the object being run and in some shared library.  The
00241      dynamic linker copies the data addressed by the symbol from the
00242      shared library into the object, because the object being
00243      run has to have the data at some particular address.  */
00244   HOWTO (R_CRIS_COPY,              /* type */
00245         0,                  /* rightshift */
00246         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00247         32,                 /* bitsize */
00248         FALSE,                     /* pc_relative */
00249         0,                  /* bitpos */
00250         complain_overflow_bitfield, /* complain_on_overflow */
00251         bfd_elf_generic_reloc,     /* special_function */
00252         "R_CRIS_COPY",             /* name */
00253         FALSE,                     /* partial_inplace */
00254         0,                  /* src_mask */
00255         0,                  /* dst_mask */
00256         FALSE),             /* pcrel_offset */
00257 
00258   /* Like R_CRIS_32, but used when setting global offset table entries.  */
00259   HOWTO (R_CRIS_GLOB_DAT,   /* type */
00260         0,                  /* rightshift */
00261         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00262         32,                 /* bitsize */
00263         FALSE,                     /* pc_relative */
00264         0,                  /* bitpos */
00265         complain_overflow_bitfield, /* complain_on_overflow */
00266         bfd_elf_generic_reloc,     /* special_function */
00267         "R_CRIS_GLOB_DAT",  /* name */
00268         FALSE,                     /* partial_inplace */
00269         0,                  /* src_mask */
00270         0xffffffff,         /* dst_mask */
00271         FALSE),             /* pcrel_offset */
00272 
00273   /* Marks a procedure linkage table entry for a symbol.  */
00274   HOWTO (R_CRIS_JUMP_SLOT,  /* type */
00275         0,                  /* rightshift */
00276         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00277         32,                 /* bitsize */
00278         FALSE,                     /* pc_relative */
00279         0,                  /* bitpos */
00280         complain_overflow_bitfield, /* complain_on_overflow */
00281         bfd_elf_generic_reloc,     /* special_function */
00282         "R_CRIS_JUMP_SLOT", /* name */
00283         FALSE,                     /* partial_inplace */
00284         0,                  /* src_mask */
00285         0,                  /* dst_mask */
00286         FALSE),             /* pcrel_offset */
00287 
00288   /* Used only by the dynamic linker.  When the object is run, this
00289      longword is set to the load address of the object, plus the
00290      addend.  */
00291   HOWTO (R_CRIS_RELATIVE,   /* type */
00292         0,                  /* rightshift */
00293         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00294         32,                 /* bitsize */
00295         FALSE,                     /* pc_relative */
00296         0,                  /* bitpos */
00297         complain_overflow_bitfield, /* complain_on_overflow */
00298         bfd_elf_generic_reloc,     /* special_function */
00299         "R_CRIS_RELATIVE",  /* name */
00300         FALSE,                     /* partial_inplace */
00301         0,                  /* src_mask */
00302         0xffffffff,         /* dst_mask */
00303         FALSE),             /* pcrel_offset */
00304 
00305   /* Like R_CRIS_32, but referring to the GOT table entry for the symbol.  */
00306   HOWTO (R_CRIS_16_GOT,            /* type */
00307         0,                  /* rightshift */
00308         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00309         16,                 /* bitsize */
00310         FALSE,                     /* pc_relative */
00311         0,                  /* bitpos */
00312         complain_overflow_bitfield, /* complain_on_overflow */
00313         bfd_elf_generic_reloc,     /* special_function */
00314         "R_CRIS_16_GOT",    /* name */
00315         FALSE,                     /* partial_inplace */
00316         0,                  /* src_mask */
00317         0xffff,             /* dst_mask */
00318         FALSE),             /* pcrel_offset */
00319 
00320   HOWTO (R_CRIS_32_GOT,            /* type */
00321         0,                  /* rightshift */
00322         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00323         32,                 /* bitsize */
00324         FALSE,                     /* pc_relative */
00325         0,                  /* bitpos */
00326         complain_overflow_bitfield, /* complain_on_overflow */
00327         bfd_elf_generic_reloc,     /* special_function */
00328         "R_CRIS_32_GOT",    /* name */
00329         FALSE,                     /* partial_inplace */
00330         0,                  /* src_mask */
00331         0xffffffff,         /* dst_mask */
00332         FALSE),             /* pcrel_offset */
00333 
00334   /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
00335      the GOT table for the symbol.  */
00336   HOWTO (R_CRIS_16_GOTPLT,  /* type */
00337         0,                  /* rightshift */
00338         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00339         16,                 /* bitsize */
00340         FALSE,                     /* pc_relative */
00341         0,                  /* bitpos */
00342         complain_overflow_bitfield, /* complain_on_overflow */
00343         bfd_elf_generic_reloc,     /* special_function */
00344         "R_CRIS_16_GOTPLT", /* name */
00345         FALSE,                     /* partial_inplace */
00346         0,                  /* src_mask */
00347         0xffff,             /* dst_mask */
00348         FALSE),             /* pcrel_offset */
00349 
00350   HOWTO (R_CRIS_32_GOTPLT,  /* type */
00351         0,                  /* rightshift */
00352         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00353         32,                 /* bitsize */
00354         FALSE,                     /* pc_relative */
00355         0,                  /* bitpos */
00356         complain_overflow_bitfield, /* complain_on_overflow */
00357         bfd_elf_generic_reloc,     /* special_function */
00358         "R_CRIS_32_GOTPLT", /* name */
00359         FALSE,                     /* partial_inplace */
00360         0,                  /* src_mask */
00361         0xffffffff,         /* dst_mask */
00362         FALSE),             /* pcrel_offset */
00363 
00364   /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
00365      be necessary.  */
00366   HOWTO (R_CRIS_32_GOTREL,  /* type */
00367         0,                  /* rightshift */
00368         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00369         32,                 /* bitsize */
00370         FALSE,                     /* pc_relative */
00371         0,                  /* bitpos */
00372         complain_overflow_bitfield, /* complain_on_overflow */
00373         bfd_elf_generic_reloc,     /* special_function */
00374         "R_CRIS_32_GOTREL", /* name */
00375         FALSE,                     /* partial_inplace */
00376         0,                  /* src_mask */
00377         0xffffffff,         /* dst_mask */
00378         FALSE),             /* pcrel_offset */
00379 
00380   /* A 32-bit offset from GOT to entry for this symbol in PLT and request
00381      to create PLT entry for symbol.  */
00382   HOWTO (R_CRIS_32_PLT_GOTREL,     /* type */
00383         0,                  /* rightshift */
00384         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00385         32,                 /* bitsize */
00386         FALSE,                     /* pc_relative */
00387         0,                  /* bitpos */
00388         complain_overflow_bitfield, /* complain_on_overflow */
00389         bfd_elf_generic_reloc,     /* special_function */
00390         "R_CRIS_32_PLT_GOTREL", /* name */
00391         FALSE,                     /* partial_inplace */
00392         0,                  /* src_mask */
00393         0xffffffff,         /* dst_mask */
00394         FALSE),             /* pcrel_offset */
00395 
00396   /* A 32-bit offset from PC (location after the relocation) + addend to
00397      entry for this symbol in PLT and request to create PLT entry for
00398      symbol.  */
00399   HOWTO (R_CRIS_32_PLT_PCREL,      /* type */
00400         0,                  /* rightshift */
00401         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00402         32,                 /* bitsize */
00403         TRUE,               /* pc_relative */
00404         0,                  /* bitpos */
00405         complain_overflow_bitfield, /* complain_on_overflow */
00406         cris_elf_pcrel_reloc,      /* special_function */
00407         "R_CRIS_32_PLT_PCREL",     /* name */
00408         FALSE,                     /* partial_inplace */
00409         0,                  /* src_mask */
00410         0xffffffff,         /* dst_mask */
00411         TRUE)               /* pcrel_offset */
00412 };
00413 
00414 /* Map BFD reloc types to CRIS ELF reloc types.  */
00415 
00416 struct cris_reloc_map
00417 {
00418   bfd_reloc_code_real_type bfd_reloc_val;
00419   unsigned int cris_reloc_val;
00420 };
00421 
00422 static const struct cris_reloc_map cris_reloc_map [] =
00423 {
00424   { BFD_RELOC_NONE,         R_CRIS_NONE },
00425   { BFD_RELOC_8,            R_CRIS_8 },
00426   { BFD_RELOC_16,           R_CRIS_16 },
00427   { BFD_RELOC_32,           R_CRIS_32 },
00428   { BFD_RELOC_8_PCREL,             R_CRIS_8_PCREL },
00429   { BFD_RELOC_16_PCREL,            R_CRIS_16_PCREL },
00430   { BFD_RELOC_32_PCREL,            R_CRIS_32_PCREL },
00431   { BFD_RELOC_VTABLE_INHERIT,      R_CRIS_GNU_VTINHERIT },
00432   { BFD_RELOC_VTABLE_ENTRY, R_CRIS_GNU_VTENTRY },
00433   { BFD_RELOC_CRIS_COPY,    R_CRIS_COPY },
00434   { BFD_RELOC_CRIS_GLOB_DAT,       R_CRIS_GLOB_DAT },
00435   { BFD_RELOC_CRIS_JUMP_SLOT,      R_CRIS_JUMP_SLOT },
00436   { BFD_RELOC_CRIS_RELATIVE,       R_CRIS_RELATIVE },
00437   { BFD_RELOC_CRIS_16_GOT,  R_CRIS_16_GOT },
00438   { BFD_RELOC_CRIS_32_GOT,  R_CRIS_32_GOT },
00439   { BFD_RELOC_CRIS_16_GOTPLT,      R_CRIS_16_GOTPLT },
00440   { BFD_RELOC_CRIS_32_GOTPLT,      R_CRIS_32_GOTPLT },
00441   { BFD_RELOC_CRIS_32_GOTREL,      R_CRIS_32_GOTREL },
00442   { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
00443   { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL }
00444 };
00445 
00446 static reloc_howto_type *
00447 cris_reloc_type_lookup (abfd, code)
00448      bfd * abfd ATTRIBUTE_UNUSED;
00449      bfd_reloc_code_real_type code;
00450 {
00451   unsigned int i;
00452 
00453   for (i = 0; i < sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]); i++)
00454     if (cris_reloc_map [i].bfd_reloc_val == code)
00455       return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
00456 
00457   return NULL;
00458 }
00459 
00460 static reloc_howto_type *
00461 cris_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
00462 {
00463   unsigned int i;
00464 
00465   for (i = 0;
00466        i < sizeof (cris_elf_howto_table) / sizeof (cris_elf_howto_table[0]);
00467        i++)
00468     if (cris_elf_howto_table[i].name != NULL
00469        && strcasecmp (cris_elf_howto_table[i].name, r_name) == 0)
00470       return &cris_elf_howto_table[i];
00471 
00472   return NULL;
00473 }
00474 
00475 /* Set the howto pointer for an CRIS ELF reloc.  */
00476 
00477 static void
00478 cris_info_to_howto_rela (abfd, cache_ptr, dst)
00479      bfd * abfd ATTRIBUTE_UNUSED;
00480      arelent * cache_ptr;
00481      Elf_Internal_Rela * dst;
00482 {
00483   unsigned int r_type;
00484 
00485   r_type = ELF32_R_TYPE (dst->r_info);
00486   BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
00487   cache_ptr->howto = & cris_elf_howto_table [r_type];
00488 }
00489 
00490 bfd_reloc_status_type
00491 cris_elf_pcrel_reloc (abfd, reloc_entry, symbol, data, input_section,
00492                     output_bfd, error_message)
00493      bfd *abfd ATTRIBUTE_UNUSED;
00494      arelent *reloc_entry;
00495      asymbol *symbol;
00496      PTR data ATTRIBUTE_UNUSED;
00497      asection *input_section;
00498      bfd *output_bfd;
00499      char **error_message ATTRIBUTE_UNUSED;
00500 {
00501   /* By default (using only bfd_elf_generic_reloc when linking to
00502      non-ELF formats) PC-relative relocs are relative to the beginning
00503      of the reloc.  CRIS PC-relative relocs are relative to the position
00504      *after* the reloc because that's what pre-CRISv32 PC points to
00505      after reading an insn field with that reloc.  (For CRISv32, PC is
00506      actually relative to the start of the insn, but we keep the old
00507      definition.)  Still, we use as much generic machinery as we can.
00508 
00509      Only adjust when doing a final link.  */
00510   if (output_bfd == (bfd *) NULL)
00511     reloc_entry->addend -= 1 << reloc_entry->howto->size;
00512 
00513   return
00514     bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
00515                         input_section, output_bfd, error_message);
00516 }
00517 
00518 /* Support for core dump NOTE sections.
00519    The slightly unintuitive code layout is an attempt to keep at least
00520    some similarities with other ports, hoping to simplify general
00521    changes, while still keeping Linux/CRIS and Linux/CRISv32 code apart.  */
00522 
00523 static bfd_boolean
00524 cris_elf_grok_prstatus (abfd, note)
00525      bfd *abfd;
00526      Elf_Internal_Note *note;
00527 {
00528   int offset;
00529   size_t size;
00530 
00531   if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
00532     switch (note->descsz)
00533       {
00534       default:
00535        return FALSE;
00536 
00537       case 202:             /* Linux/CRISv32 */
00538        /* pr_cursig */
00539        elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
00540 
00541        /* pr_pid */
00542        elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 22);
00543 
00544        /* pr_reg */
00545        offset = 70;
00546        size = 128;
00547 
00548        break;
00549       }
00550   else
00551     switch (note->descsz)
00552       {
00553       default:
00554        return FALSE;
00555 
00556       case 214:             /* Linux/CRIS */
00557        /* pr_cursig */
00558        elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
00559 
00560        /* pr_pid */
00561        elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 22);
00562 
00563        /* pr_reg */
00564        offset = 70;
00565        size = 140;
00566 
00567        break;
00568       }
00569 
00570   /* Make a ".reg/999" section.  */
00571   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
00572                                      size, note->descpos + offset);
00573 }
00574 
00575 static bfd_boolean
00576 cris_elf_grok_psinfo (abfd, note)
00577      bfd *abfd;
00578      Elf_Internal_Note *note;
00579 {
00580   if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
00581     switch (note->descsz)
00582       {
00583       default:
00584        return FALSE;
00585 
00586       case 124:             /* Linux/CRISv32 elf_prpsinfo */
00587        elf_tdata (abfd)->core_program
00588          = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
00589        elf_tdata (abfd)->core_command
00590          = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
00591       }
00592   else
00593     switch (note->descsz)
00594       {
00595       default:
00596        return FALSE;
00597 
00598       case 124:             /* Linux/CRIS elf_prpsinfo */
00599        elf_tdata (abfd)->core_program
00600          = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
00601        elf_tdata (abfd)->core_command
00602          = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
00603       }
00604 
00605   /* Note that for some reason, a spurious space is tacked
00606      onto the end of the args in some (at least one anyway)
00607      implementations, so strip it off if it exists.  */
00608 
00609   {
00610     char *command = elf_tdata (abfd)->core_command;
00611     int n = strlen (command);
00612 
00613     if (0 < n && command[n - 1] == ' ')
00614       command[n - 1] = '\0';
00615   }
00616 
00617   return TRUE;
00618 }
00619 
00620 /* The name of the dynamic interpreter.  This is put in the .interp
00621    section.  */
00622 
00623 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
00624 
00625 /* The size in bytes of an entry in the procedure linkage table.  */
00626 
00627 #define PLT_ENTRY_SIZE 20
00628 #define PLT_ENTRY_SIZE_V32 26
00629 
00630 /* The first entry in an absolute procedure linkage table looks like this.  */
00631 
00632 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
00633 {
00634   0xfc, 0xe1,
00635   0x7e, 0x7e, /* push mof.  */
00636   0x7f, 0x0d,   /*  (dip [pc+]) */
00637   0, 0, 0, 0, /*  Replaced with address of .got + 4.  */
00638   0x30, 0x7a, /* move [...],mof */
00639   0x7f, 0x0d,   /*  (dip [pc+]) */
00640   0, 0, 0, 0, /*  Replaced with address of .got + 8.  */
00641   0x30, 0x09  /* jump [...] */
00642 };
00643 
00644 static const bfd_byte elf_cris_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
00645 {
00646   0x84, 0xe2, /* subq 4,$sp */
00647   0x6f, 0xfe, /* move.d 0,$acr */
00648   0, 0, 0, 0, /*  Replaced by address of .got + 4.  */
00649   0x7e, 0x7a, /* move $mof,[$sp] */
00650   0x3f, 0x7a, /* move [$acr],$mof */
00651   0x04, 0xf2, /* addq 4,acr */
00652   0x6f, 0xfa, /* move.d [$acr],$acr */
00653   0xbf, 0x09, /* jump $acr */
00654   0xb0, 0x05, /* nop */
00655   0, 0        /*  Pad out to 26 bytes.  */
00656 };
00657 
00658 /* Subsequent entries in an absolute procedure linkage table look like
00659    this.  */
00660 
00661 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
00662 {
00663   0x7f, 0x0d,   /*  (dip [pc+]) */
00664   0, 0, 0, 0, /*  Replaced with address of this symbol in .got.  */
00665   0x30, 0x09, /* jump [...] */
00666   0x3f,        0x7e, /* move [pc+],mof */
00667   0, 0, 0, 0, /*  Replaced with offset into relocation table.  */
00668   0x2f, 0xfe, /* add.d [pc+],pc */
00669   0xec, 0xff,
00670   0xff, 0xff  /*  Replaced with offset to start of .plt.  */
00671 };
00672 
00673 static const bfd_byte elf_cris_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
00674 {
00675   0x6f, 0xfe, /* move.d 0,$acr */
00676   0, 0, 0, 0, /*  Replaced with address of this symbol in .got.  */
00677   0x6f, 0xfa,   /* move.d [$acr],$acr */
00678   0xbf, 0x09,   /* jump $acr */
00679   0xb0, 0x05, /* nop */
00680   0x3f, 0x7e, /* move 0,mof */
00681   0, 0, 0, 0, /*  Replaced with offset into relocation table. */
00682   0xbf, 0x0e, /* ba start_of_plt0_entry */
00683   0, 0, 0, 0, /*  Replaced with offset to plt0 entry.  */
00684   0xb0, 0x05  /* nop */
00685 };
00686 
00687 /* The first entry in a PIC procedure linkage table looks like this.  */
00688 
00689 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
00690 {
00691   0xfc, 0xe1, 0x7e, 0x7e,   /* push mof */
00692   0x04, 0x01, 0x30, 0x7a,   /* move [r0+4],mof */
00693   0x08, 0x01, 0x30, 0x09,   /* jump [r0+8] */
00694   0, 0, 0, 0, 0, 0, 0, 0,   /*  Pad out to 20 bytes.  */
00695 };
00696 
00697 static const bfd_byte elf_cris_pic_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
00698 {
00699   0x84, 0xe2, /* subq 4,$sp */
00700   0x04, 0x01, /* addoq 4,$r0,$acr */
00701   0x7e, 0x7a, /* move $mof,[$sp] */
00702   0x3f, 0x7a, /* move [$acr],$mof */
00703   0x04, 0xf2, /* addq 4,$acr */
00704   0x6f, 0xfa, /* move.d [$acr],$acr */
00705   0xbf, 0x09, /* jump $acr */
00706   0xb0, 0x05, /* nop */
00707   0, 0,              /*  Pad out to 26 bytes.  */
00708   0, 0, 0, 0,
00709   0, 0, 0, 0
00710 };
00711 
00712 /* Subsequent entries in a PIC procedure linkage table look like this.  */
00713 
00714 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
00715 {
00716   0x6f, 0x0d,   /*  (bdap [pc+].d,r0) */
00717   0, 0, 0, 0, /*  Replaced with offset of this symbol in .got.  */
00718   0x30, 0x09, /* jump [...] */
00719   0x3f, 0x7e, /* move [pc+],mof */
00720   0, 0, 0, 0, /*  Replaced with offset into relocation table.  */
00721   0x2f, 0xfe, /* add.d [pc+],pc */
00722   0xec, 0xff, /*  Replaced with offset to start of .plt.  */
00723   0xff, 0xff
00724 };
00725 
00726 static const bfd_byte elf_cris_pic_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
00727 {
00728   0x6f, 0x0d, /* addo.d 0,$r0,$acr */
00729   0, 0, 0, 0, /*  Replaced with offset of this symbol in .got.  */
00730   0x6f, 0xfa, /* move.d [$acr],$acr */
00731   0xbf, 0x09, /* jump $acr */
00732   0xb0, 0x05, /* nop */
00733   0x3f, 0x7e, /* move relocoffs,$mof */
00734   0, 0, 0, 0, /*  Replaced with offset into relocation table.  */
00735   0xbf, 0x0e, /* ba start_of_plt */
00736   0, 0, 0, 0, /*  Replaced with offset to start of .plt.  */
00737   0xb0, 0x05  /* nop */
00738 };
00739 
00740 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
00741    (and most other PIC/shlib stuff).  Check that we don't drift away
00742    without reason.
00743 
00744    The CRIS linker, like the m68k and i386 linkers (and probably the rest
00745    too) needs to keep track of the number of relocs that it decides to
00746    copy in check_relocs for each symbol.  This is so that it can discard
00747    PC relative relocs if it doesn't need them when linking with
00748    -Bsymbolic.  We store the information in a field extending the regular
00749    ELF linker hash table.  */
00750 
00751 /* This structure keeps track of the number of PC relative relocs we have
00752    copied for a given symbol.  */
00753 
00754 struct elf_cris_pcrel_relocs_copied
00755 {
00756   /* Next section.  */
00757   struct elf_cris_pcrel_relocs_copied *next;
00758   /* A section in dynobj.  */
00759   asection *section;
00760   /* Number of relocs copied in this section.  */
00761   bfd_size_type count;
00762 };
00763 
00764 /* CRIS ELF linker hash entry.  */
00765 
00766 struct elf_cris_link_hash_entry
00767 {
00768   struct elf_link_hash_entry root;
00769 
00770   /* Number of PC relative relocs copied for this symbol.  */
00771   struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
00772 
00773   /* The GOTPLT references are CRIS-specific; the goal is to avoid having
00774      both a general GOT and a PLT-specific GOT entry for the same symbol,
00775      when it is referenced both as a function and as a function pointer.
00776 
00777      Number of GOTPLT references for a function.  */
00778   bfd_signed_vma gotplt_refcount;
00779 
00780   /* Actual GOTPLT index for this symbol, if applicable, or zero if not
00781      (zero is never used as an index).  FIXME: We should be able to fold
00782      this with gotplt_refcount in a union, like the got and plt unions in
00783      elf_link_hash_entry.  */
00784   bfd_size_type gotplt_offset;
00785 };
00786 
00787 /* CRIS ELF linker hash table.  */
00788 
00789 struct elf_cris_link_hash_table
00790 {
00791   struct elf_link_hash_table root;
00792 
00793   /* We can't use the PLT offset and calculate to get the GOTPLT offset,
00794      since we try and avoid creating GOTPLT:s when there's already a GOT.
00795      Instead, we keep and update the next available index here.  */
00796   bfd_size_type next_gotplt_entry;
00797 };
00798 
00799 /* Traverse a CRIS ELF linker hash table.  */
00800 
00801 #define elf_cris_link_hash_traverse(table, func, info)                \
00802   (elf_link_hash_traverse                                      \
00803    (&(table)->root,                                            \
00804     (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
00805     (info)))
00806 
00807 /* Get the CRIS ELF linker hash table from a link_info structure.  */
00808 
00809 #define elf_cris_hash_table(p) \
00810   ((struct elf_cris_link_hash_table *) (p)->hash)
00811 
00812 /* Create an entry in a CRIS ELF linker hash table.  */
00813 
00814 static struct bfd_hash_entry *
00815 elf_cris_link_hash_newfunc (entry, table, string)
00816      struct bfd_hash_entry *entry;
00817      struct bfd_hash_table *table;
00818      const char *string;
00819 {
00820   struct elf_cris_link_hash_entry *ret =
00821     (struct elf_cris_link_hash_entry *) entry;
00822 
00823   /* Allocate the structure if it has not already been allocated by a
00824      subclass.  */
00825   if (ret == (struct elf_cris_link_hash_entry *) NULL)
00826     ret = ((struct elf_cris_link_hash_entry *)
00827           bfd_hash_allocate (table,
00828                            sizeof (struct elf_cris_link_hash_entry)));
00829   if (ret == (struct elf_cris_link_hash_entry *) NULL)
00830     return (struct bfd_hash_entry *) ret;
00831 
00832   /* Call the allocation method of the superclass.  */
00833   ret = ((struct elf_cris_link_hash_entry *)
00834         _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
00835                                  table, string));
00836   if (ret != (struct elf_cris_link_hash_entry *) NULL)
00837     {
00838       ret->pcrel_relocs_copied = NULL;
00839       ret->gotplt_refcount = 0;
00840       ret->gotplt_offset = 0;
00841     }
00842 
00843   return (struct bfd_hash_entry *) ret;
00844 }
00845 
00846 /* Create a CRIS ELF linker hash table.  */
00847 
00848 static struct bfd_link_hash_table *
00849 elf_cris_link_hash_table_create (abfd)
00850      bfd *abfd;
00851 {
00852   struct elf_cris_link_hash_table *ret;
00853   bfd_size_type amt = sizeof (struct elf_cris_link_hash_table);
00854 
00855   ret = ((struct elf_cris_link_hash_table *) bfd_malloc (amt));
00856   if (ret == (struct elf_cris_link_hash_table *) NULL)
00857     return NULL;
00858 
00859   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
00860                                   elf_cris_link_hash_newfunc,
00861                                   sizeof (struct elf_cris_link_hash_entry)))
00862     {
00863       free (ret);
00864       return NULL;
00865     }
00866 
00867   /* Initialize to skip over the first three entries in the gotplt; they
00868      are used for run-time symbol evaluation.  */
00869   ret->next_gotplt_entry = 12;
00870 
00871   return &ret->root.root;
00872 }
00873 
00874 /* Perform a single relocation.  By default we use the standard BFD
00875    routines, with a few tweaks.  */
00876 
00877 static bfd_reloc_status_type
00878 cris_final_link_relocate (howto, input_bfd, input_section, contents, rel,
00879                        relocation)
00880      reloc_howto_type *  howto;
00881      bfd *               input_bfd;
00882      asection *          input_section;
00883      bfd_byte *          contents;
00884      Elf_Internal_Rela * rel;
00885      bfd_vma             relocation;
00886 {
00887   bfd_reloc_status_type r;
00888 
00889   /* PC-relative relocations are relative to the position *after*
00890      the reloc.  Note that for R_CRIS_8_PCREL the adjustment is
00891      not a single byte, since PC must be 16-bit-aligned.  */
00892   switch (ELF32_R_TYPE (rel->r_info))
00893     {
00894       /* Check that the 16-bit GOT relocs are positive.  */
00895     case R_CRIS_16_GOTPLT:
00896     case R_CRIS_16_GOT:
00897       if ((bfd_signed_vma) relocation < 0)
00898        return bfd_reloc_overflow;
00899       break;
00900 
00901     case R_CRIS_32_PLT_PCREL:
00902     case R_CRIS_32_PCREL:
00903       relocation -= 2;
00904       /* Fall through.  */
00905     case R_CRIS_8_PCREL:
00906     case R_CRIS_16_PCREL:
00907       relocation -= 2;
00908       break;
00909 
00910     default:
00911       break;
00912     }
00913 
00914   r = _bfd_final_link_relocate (howto, input_bfd, input_section,
00915                             contents, rel->r_offset,
00916                             relocation, rel->r_addend);
00917   return r;
00918 }
00919 
00920 /* Relocate an CRIS ELF section.  See elf32-fr30.c, from where this was
00921    copied, for further comments.  */
00922 
00923 static bfd_boolean
00924 cris_elf_relocate_section (output_bfd, info, input_bfd, input_section,
00925                         contents, relocs, local_syms, local_sections)
00926      bfd *output_bfd ATTRIBUTE_UNUSED;
00927      struct bfd_link_info *info;
00928      bfd *input_bfd;
00929      asection *input_section;
00930      bfd_byte *contents;
00931      Elf_Internal_Rela *relocs;
00932      Elf_Internal_Sym *local_syms;
00933      asection **local_sections;
00934 {
00935   bfd *dynobj;
00936   Elf_Internal_Shdr *symtab_hdr;
00937   struct elf_link_hash_entry **sym_hashes;
00938   bfd_vma *local_got_offsets;
00939   asection *sgot;
00940   asection *splt;
00941   asection *sreloc;
00942   Elf_Internal_Rela *rel;
00943   Elf_Internal_Rela *relend;
00944 
00945   dynobj = elf_hash_table (info)->dynobj;
00946   local_got_offsets = elf_local_got_offsets (input_bfd);
00947   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
00948   sym_hashes = elf_sym_hashes (input_bfd);
00949   relend     = relocs + input_section->reloc_count;
00950 
00951   sgot = NULL;
00952   splt = NULL;
00953   sreloc = NULL;
00954 
00955   if (dynobj != NULL)
00956     {
00957       splt = bfd_get_section_by_name (dynobj, ".plt");
00958       sgot = bfd_get_section_by_name (dynobj, ".got");
00959     }
00960 
00961   for (rel = relocs; rel < relend; rel ++)
00962     {
00963       reloc_howto_type *howto;
00964       unsigned long r_symndx;
00965       Elf_Internal_Sym *sym;
00966       asection *sec;
00967       struct elf_link_hash_entry *h;
00968       bfd_vma relocation;
00969       bfd_reloc_status_type r;
00970       const char *symname = NULL;
00971       int r_type;
00972 
00973       r_type = ELF32_R_TYPE (rel->r_info);
00974 
00975       if (   r_type == R_CRIS_GNU_VTINHERIT
00976          || r_type == R_CRIS_GNU_VTENTRY)
00977        continue;
00978 
00979       r_symndx = ELF32_R_SYM (rel->r_info);
00980       howto  = cris_elf_howto_table + r_type;
00981       h      = NULL;
00982       sym    = NULL;
00983       sec    = NULL;
00984 
00985       if (r_symndx < symtab_hdr->sh_info)
00986        {
00987          sym = local_syms + r_symndx;
00988          sec = local_sections [r_symndx];
00989          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
00990 
00991          symname = (bfd_elf_string_from_elf_section
00992                    (input_bfd, symtab_hdr->sh_link, sym->st_name));
00993          if (symname == NULL)
00994            symname = bfd_section_name (input_bfd, sec);
00995        }
00996       else
00997        {
00998          bfd_boolean warned;
00999          bfd_boolean unresolved_reloc;
01000 
01001          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
01002                                r_symndx, symtab_hdr, sym_hashes,
01003                                h, sec, relocation,
01004                                unresolved_reloc, warned);
01005 
01006          if (unresolved_reloc
01007              /* Perhaps we should detect the cases that
01008                sec->output_section is expected to be NULL like i386 and
01009                m68k, but apparently (and according to elfxx-ia64.c) all
01010                valid cases are where the symbol is defined in a shared
01011                object which we link dynamically against.  This includes
01012                PLT relocs for which we've created a PLT entry and other
01013                relocs for which we're prepared to create dynamic
01014                relocations.
01015 
01016                For now, new situations cause us to just err when
01017                sec->output_offset is NULL but the object with the symbol
01018                is *not* dynamically linked against.  Thus this will
01019                automatically remind us so we can see if there are other
01020                valid cases we need to revisit.  */
01021              && (sec->owner->flags & DYNAMIC) != 0)
01022            relocation = 0;
01023 
01024          else if (h->root.type == bfd_link_hash_defined
01025                  || h->root.type == bfd_link_hash_defweak)
01026            {
01027              /* Here follow the cases where the relocation value must
01028                be zero (or when further handling is simplified when
01029                zero).  I can't claim to understand the various
01030                conditions and they weren't described in the files
01031                where I copied them from (elf32-m68k.c and
01032                elf32-i386.c), but let's mention examples of where
01033                they happen.  FIXME: Perhaps define and use a
01034                dynamic_symbol_p function like ia64.
01035 
01036                - When creating a shared library, we can have an
01037                ordinary relocation for a symbol defined in a shared
01038                library (perhaps the one we create).  We then make
01039                the relocation value zero, as the value seen now will
01040                be added into the relocation addend in this shared
01041                library, but must be handled only at dynamic-link
01042                time.  FIXME: Not sure this example covers the
01043                h->elf_link_hash_flags test, though it's there in
01044                other targets.  */
01045              if (info->shared
01046                 && ((! info->symbolic && h->dynindx != -1)
01047                     || !h->def_regular)
01048                 && (input_section->flags & SEC_ALLOC) != 0
01049                 && (r_type == R_CRIS_8
01050                     || r_type == R_CRIS_16
01051                     || r_type == R_CRIS_32
01052                     || r_type == R_CRIS_8_PCREL
01053                     || r_type == R_CRIS_16_PCREL
01054                     || r_type == R_CRIS_32_PCREL))
01055               relocation = 0;
01056              else if (!info->relocatable && unresolved_reloc)
01057               {
01058                 _bfd_error_handler
01059                   (_("%B, section %A: unresolvable relocation %s against symbol `%s'"),
01060                    input_bfd,
01061                    input_section,
01062                    cris_elf_howto_table[r_type].name,
01063                    symname);
01064                 bfd_set_error (bfd_error_bad_value);
01065                 return FALSE;
01066               }
01067            }
01068        }
01069 
01070       if (sec != NULL && elf_discarded_section (sec))
01071        {
01072          /* For relocs against symbols from removed linkonce sections,
01073             or sections discarded by a linker script, we just want the
01074             section contents zeroed.  Avoid any special processing.  */
01075          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
01076          rel->r_info = 0;
01077          rel->r_addend = 0;
01078          continue;
01079        }
01080 
01081       if (info->relocatable)
01082        continue;
01083 
01084       switch (r_type)
01085        {
01086        case R_CRIS_16_GOTPLT:
01087        case R_CRIS_32_GOTPLT:
01088          /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
01089             but we require a PLT, and the PLT handling will take care of
01090             filling in the PLT-specific GOT entry.  For the GOT offset,
01091             calculate it as we do when filling it in for the .got.plt
01092             section.  If we don't have a PLT, punt to GOT handling.  */
01093          if (h != NULL
01094              && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
01095            {
01096              asection *sgotplt
01097               = bfd_get_section_by_name (dynobj, ".got.plt");
01098              bfd_vma got_offset;
01099 
01100              BFD_ASSERT (h->dynindx != -1);
01101              BFD_ASSERT (sgotplt != NULL);
01102 
01103              got_offset
01104               = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
01105 
01106              relocation = got_offset;
01107              break;
01108            }
01109 
01110          /* We didn't make a PLT entry for this symbol.  Maybe everything is
01111             folded into the GOT.  Other than folding, this happens when
01112             statically linking PIC code, or when using -Bsymbolic.  Check
01113             that we instead have a GOT entry as done for us by
01114             elf_cris_adjust_dynamic_symbol, and drop through into the
01115             ordinary GOT cases.  This must not happen for the
01116             executable, because any reference it does to a function
01117             that is satisfied by a DSO must generate a PLT.  We assume
01118             these call-specific relocs don't address non-functions.  */
01119          if (h != NULL
01120              && (h->got.offset == (bfd_vma) -1
01121                 || (!info->shared
01122                     && !(h->def_regular
01123                         || (!h->def_dynamic
01124                             && h->root.type == bfd_link_hash_undefweak)))))
01125            {
01126              (*_bfd_error_handler)
01127               ((h->got.offset == (bfd_vma) -1)
01128                ? _("%B, section %A: No PLT nor GOT for relocation %s"
01129                    " against symbol `%s'")
01130                : _("%B, section %A: No PLT for relocation %s"
01131                    " against symbol `%s'"),
01132                input_bfd,
01133                input_section,
01134                cris_elf_howto_table[r_type].name,
01135                (symname != NULL && symname[0] != '\0'
01136                 ? symname : _("[whose name is lost]")));
01137 
01138              /* FIXME: Perhaps blaming input is not the right thing to
01139                do; this is probably an internal error.  But it is true
01140                that we didn't like that particular input.  */
01141              bfd_set_error (bfd_error_bad_value);
01142              return FALSE;
01143            }
01144          /* Fall through.  */
01145 
01146          /* The size of the actual relocation is not used here; we only
01147             fill in the GOT table here.  */
01148        case R_CRIS_16_GOT:
01149        case R_CRIS_32_GOT:
01150          {
01151            bfd_vma off;
01152 
01153            /* Note that despite using RELA relocations, the .got contents
01154               is always filled in with the link-relative relocation
01155               value; the addend.  */
01156 
01157            if (h != NULL)
01158              {
01159               off = h->got.offset;
01160               BFD_ASSERT (off != (bfd_vma) -1);
01161 
01162               if (!elf_hash_table (info)->dynamic_sections_created
01163                   || (! info->shared
01164                      && (h->def_regular
01165                          || h->type == STT_FUNC
01166                          || h->needs_plt))
01167                   || (info->shared
01168                      && (info->symbolic || h->dynindx == -1)
01169                      && h->def_regular))
01170                 {
01171                   /* This wasn't checked above for ! info->shared, but
01172                      must hold there if we get here; the symbol must
01173                      be defined in the regular program or be undefweak
01174                      or be a function or otherwise need a PLT.  */
01175                   BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created
01176                             || info->shared
01177                             || h->def_regular
01178                             || h->type == STT_FUNC
01179                             || h->needs_plt
01180                             || h->root.type == bfd_link_hash_undefweak);
01181 
01182                   /* This is actually a static link, or it is a
01183                      -Bsymbolic link and the symbol is defined locally,
01184                      or is undefweak, or the symbol was forced to be
01185                      local because of a version file, or we're not
01186                      creating a dynamic object.  We must initialize this
01187                      entry in the global offset table.  Since the offset
01188                      must always be a multiple of 4, we use the least
01189                      significant bit to record whether we have
01190                      initialized it already.
01191 
01192                      If this GOT entry should be runtime-initialized, we
01193                      will create a .rela.got relocation entry to
01194                      initialize the value.  This is done in the
01195                      finish_dynamic_symbol routine.  */
01196                   if ((off & 1) != 0)
01197                     off &= ~1;
01198                   else
01199                     {
01200                      bfd_put_32 (output_bfd, relocation,
01201                                 sgot->contents + off);
01202                      h->got.offset |= 1;
01203                     }
01204                 }
01205              }
01206            else
01207              {
01208               BFD_ASSERT (local_got_offsets != NULL
01209                          && local_got_offsets[r_symndx] != (bfd_vma) -1);
01210 
01211               off = local_got_offsets[r_symndx];
01212 
01213               /* The offset must always be a multiple of 4.  We use
01214                  the least significant bit to record whether we have
01215                  already generated the necessary reloc.  */
01216               if ((off & 1) != 0)
01217                 off &= ~1;
01218               else
01219                 {
01220                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
01221 
01222                   if (info->shared)
01223                     {
01224                      asection *s;
01225                      Elf_Internal_Rela outrel;
01226                      bfd_byte *loc;
01227 
01228                      s = bfd_get_section_by_name (dynobj, ".rela.got");
01229                      BFD_ASSERT (s != NULL);
01230 
01231                      outrel.r_offset = (sgot->output_section->vma
01232                                       + sgot->output_offset
01233                                       + off);
01234                      outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
01235                      outrel.r_addend = relocation;
01236                      loc = s->contents;
01237                      loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
01238                      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
01239                     }
01240 
01241                   local_got_offsets[r_symndx] |= 1;
01242                 }
01243              }
01244 
01245            relocation = sgot->output_offset + off;
01246            if (rel->r_addend != 0)
01247              {
01248               /* We can't do anything for a relocation which is against
01249                  a symbol *plus offset*.  GOT holds relocations for
01250                  symbols.  Make this an error; the compiler isn't
01251                  allowed to pass us these kinds of things.  */
01252               if (h == NULL)
01253                 (*_bfd_error_handler)
01254                   (_("%B, section %A: relocation %s with non-zero addend %d"
01255                      " against local symbol"),
01256                    input_bfd,
01257                    input_section,
01258                    cris_elf_howto_table[r_type].name,
01259                    rel->r_addend);
01260               else
01261                 (*_bfd_error_handler)
01262                   (_("%B, section %A: relocation %s with non-zero addend %d"
01263                      " against symbol `%s'"),
01264                    input_bfd,
01265                    input_section,
01266                    cris_elf_howto_table[r_type].name,
01267                    rel->r_addend,
01268                    symname[0] != '\0' ? symname : _("[whose name is lost]"));
01269 
01270               bfd_set_error (bfd_error_bad_value);
01271               return FALSE;
01272              }
01273          }
01274          break;
01275 
01276        case R_CRIS_32_GOTREL:
01277          /* This relocation must only be performed against local symbols.
01278             It's also ok when we link a program and the symbol is either
01279             defined in an ordinary (non-DSO) object or is undefined weak.  */
01280          if (h != NULL
01281              && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
01282              && !(!info->shared
01283                  && (h->def_regular
01284                      || (!h->def_dynamic
01285                         && h->root.type == bfd_link_hash_undefweak))))
01286            {
01287              (*_bfd_error_handler)
01288               (_("%B, section %A: relocation %s is"
01289                  " not allowed for global symbol: `%s'"),
01290                input_bfd,
01291                input_section,
01292                cris_elf_howto_table[r_type].name,
01293                symname);
01294              bfd_set_error (bfd_error_bad_value);
01295              return FALSE;
01296            }
01297 
01298          /* This can happen if we get a link error with the input ELF
01299             variant mismatching the output variant.  Emit an error so
01300             it's noticed if it happens elsewhere.  */
01301          if (sgot == NULL)
01302            {
01303              (*_bfd_error_handler)
01304               (_("%B, section %A: relocation %s with no GOT created"),
01305                input_bfd,
01306                input_section,
01307                cris_elf_howto_table[r_type].name);
01308              bfd_set_error (bfd_error_bad_value);
01309              return FALSE;
01310            }
01311 
01312          /* This relocation is like a PC-relative one, except the
01313             reference point is the location of GOT.  Note that
01314             sgot->output_offset is not involved in this calculation.  We
01315             always want the start of entire .got section, not the
01316             position after the reserved header.  */
01317          relocation -= sgot->output_section->vma;
01318          break;
01319 
01320        case R_CRIS_32_PLT_PCREL:
01321          /* Relocation is to the entry for this symbol in the
01322             procedure linkage table.  */
01323 
01324          /* Resolve a PLT_PCREL reloc against a local symbol directly,
01325             without using the procedure linkage table.  */
01326          if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
01327            break;
01328 
01329          if (h->plt.offset == (bfd_vma) -1
01330              || splt == NULL)
01331            {
01332              /* We didn't make a PLT entry for this symbol.  This
01333                happens when statically linking PIC code, or when
01334                using -Bsymbolic.  */
01335              break;
01336            }
01337 
01338          relocation = (splt->output_section->vma
01339                      + splt->output_offset
01340                      + h->plt.offset);
01341          break;
01342 
01343        case R_CRIS_32_PLT_GOTREL:
01344          /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
01345             start of the .got section.  See also comment at
01346             R_CRIS_32_GOT.  */
01347          relocation -= sgot->output_section->vma;
01348 
01349          /* Resolve a PLT_GOTREL reloc against a local symbol directly,
01350             without using the procedure linkage table.  */
01351          if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
01352            break;
01353 
01354          if (h->plt.offset == (bfd_vma) -1
01355              || splt == NULL)
01356            {
01357              /* We didn't make a PLT entry for this symbol.  This
01358                happens when statically linking PIC code, or when
01359                using -Bsymbolic.  */
01360              break;
01361            }
01362 
01363          relocation = (splt->output_section->vma
01364                      + splt->output_offset
01365                      + h->plt.offset
01366                      - sgot->output_section->vma);
01367          break;
01368 
01369        case R_CRIS_8_PCREL:
01370        case R_CRIS_16_PCREL:
01371        case R_CRIS_32_PCREL:
01372          /* If the symbol was local, we need no shlib-specific handling.  */
01373          if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
01374            break;
01375 
01376          /* Fall through.  */
01377        case R_CRIS_8:
01378        case R_CRIS_16:
01379        case R_CRIS_32:
01380          if (info->shared
01381              && r_symndx != 0
01382              && (input_section->flags & SEC_ALLOC) != 0
01383              && ((r_type != R_CRIS_8_PCREL
01384                  && r_type != R_CRIS_16_PCREL
01385                  && r_type != R_CRIS_32_PCREL)
01386                 || (!info->symbolic
01387                     || !h->def_regular)))
01388            {
01389              Elf_Internal_Rela outrel;
01390              bfd_byte *loc;
01391              bfd_boolean skip, relocate;
01392 
01393              /* When generating a shared object, these relocations
01394                are copied into the output file to be resolved at run
01395                time.  */
01396 
01397              if (sreloc == NULL)
01398               {
01399                 const char *name;
01400 
01401                 name = (bfd_elf_string_from_elf_section
01402                        (input_bfd,
01403                         elf_elfheader (input_bfd)->e_shstrndx,
01404                         elf_section_data (input_section)->rel_hdr.sh_name));
01405                 if (name == NULL)
01406                   return FALSE;
01407 
01408                 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
01409                            && strcmp (bfd_get_section_name (input_bfd,
01410                                                         input_section),
01411                                     name + 5) == 0);
01412 
01413                 sreloc = bfd_get_section_by_name (dynobj, name);
01414 
01415                 /* That section should have been created in
01416                    cris_elf_check_relocs, but that function will not be
01417                    called for objects which fail in
01418                    cris_elf_merge_private_bfd_data.  */
01419                 if (sreloc == NULL)
01420                   {
01421                     (*_bfd_error_handler)
01422                      (_("%B: Internal inconsistency; no relocation section %s"),
01423                       input_bfd,
01424                       name);
01425 
01426                     bfd_set_error (bfd_error_bad_value);
01427                     return FALSE;
01428                   }
01429               }
01430 
01431              skip = FALSE;
01432              relocate = FALSE;
01433 
01434              outrel.r_offset =
01435               _bfd_elf_section_offset (output_bfd, info, input_section,
01436                                     rel->r_offset);
01437              if (outrel.r_offset == (bfd_vma) -1)
01438               skip = TRUE;
01439              else if (outrel.r_offset == (bfd_vma) -2)
01440               skip = TRUE, relocate = TRUE;
01441              outrel.r_offset += (input_section->output_section->vma
01442                               + input_section->output_offset);
01443 
01444              if (skip)
01445               memset (&outrel, 0, sizeof outrel);
01446              /* h->dynindx may be -1 if the symbol was marked to
01447                become local.  */
01448              else if (h != NULL
01449                      && ((! info->symbolic && h->dynindx != -1)
01450                         || !h->def_regular))
01451               {
01452                 BFD_ASSERT (h->dynindx != -1);
01453                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
01454                 outrel.r_addend = relocation + rel->r_addend;
01455               }
01456              else
01457               {
01458                 outrel.r_addend = relocation + rel->r_addend;
01459 
01460                 if (r_type == R_CRIS_32)
01461                   {
01462                     relocate = TRUE;
01463                     outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
01464                   }
01465                 else
01466                   {
01467                     long indx;
01468 
01469                     if (bfd_is_abs_section (sec))
01470                      indx = 0;
01471                     else if (sec == NULL || sec->owner == NULL)
01472                      {
01473                        bfd_set_error (bfd_error_bad_value);
01474                        return FALSE;
01475                      }
01476                     else
01477                      {
01478                        asection *osec;
01479 
01480                        /* We are turning this relocation into one
01481                           against a section symbol.  It would be
01482                           proper to subtract the symbol's value,
01483                           osec->vma, from the emitted reloc addend,
01484                           but ld.so expects buggy relocs.  */
01485                        osec = sec->output_section;
01486                        indx = elf_section_data (osec)->dynindx;
01487                        if (indx == 0)
01488                          {
01489                            struct elf_cris_link_hash_table *htab;
01490                            htab = elf_cris_hash_table (info);
01491                            osec = htab->root.text_index_section;
01492                            indx = elf_section_data (osec)->dynindx;
01493                          }
01494                        BFD_ASSERT (indx != 0);
01495                      }
01496 
01497                     outrel.r_info = ELF32_R_INFO (indx, r_type);
01498                   }
01499               }
01500 
01501              loc = sreloc->contents;
01502              loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
01503              bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
01504 
01505              /* This reloc will be computed at runtime, so there's no
01506                  need to do anything now, except for R_CRIS_32 relocations
01507                  that have been turned into R_CRIS_RELATIVE.  */
01508              if (!relocate)
01509               continue;
01510            }
01511 
01512          break;
01513        }
01514 
01515       r = cris_final_link_relocate (howto, input_bfd, input_section,
01516                                  contents, rel, relocation);
01517 
01518       if (r != bfd_reloc_ok)
01519        {
01520          const char * msg = (const char *) NULL;
01521 
01522          switch (r)
01523            {
01524            case bfd_reloc_overflow:
01525              r = info->callbacks->reloc_overflow
01526               (info, (h ? &h->root : NULL), symname, howto->name,
01527                (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
01528              break;
01529 
01530            case bfd_reloc_undefined:
01531              r = info->callbacks->undefined_symbol
01532               (info, symname, input_bfd, input_section, rel->r_offset,
01533                TRUE);
01534              break;
01535 
01536            case bfd_reloc_outofrange:
01537              msg = _("internal error: out of range error");
01538              break;
01539 
01540            case bfd_reloc_notsupported:
01541              msg = _("internal error: unsupported relocation error");
01542              break;
01543 
01544            case bfd_reloc_dangerous:
01545              msg = _("internal error: dangerous relocation");
01546              break;
01547 
01548            default:
01549              msg = _("internal error: unknown error");
01550              break;
01551            }
01552 
01553          if (msg)
01554            r = info->callbacks->warning
01555              (info, msg, symname, input_bfd, input_section, rel->r_offset);
01556 
01557          if (! r)
01558            return FALSE;
01559        }
01560     }
01561 
01562   return TRUE;
01563 }
01564 
01565 /* Finish up dynamic symbol handling.  We set the contents of various
01566    dynamic sections here.  */
01567 
01568 static bfd_boolean
01569 elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
01570      bfd *output_bfd;
01571      struct bfd_link_info *info;
01572      struct elf_link_hash_entry *h;
01573      Elf_Internal_Sym *sym;
01574 {
01575   bfd *dynobj;
01576 
01577   /* Where in the plt entry to put values.  */
01578   int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
01579 
01580   /* What offset to add to the distance to the first PLT entry for the
01581      value at plt_off3.   */
01582   int plt_off3_value_bias = 4;
01583 
01584   /* Where in the PLT entry the call-dynlink-stub is (happens to be same
01585      for PIC and non-PIC for v32 and pre-v32).  */
01586   int plt_stub_offset = 8;
01587   int plt_entry_size = PLT_ENTRY_SIZE;
01588   const bfd_byte *plt_entry = elf_cris_plt_entry;
01589   const bfd_byte *plt_pic_entry = elf_cris_pic_plt_entry;
01590 
01591   /* Adjust the various PLT entry offsets.  */
01592   if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
01593     {
01594       plt_off2 = 14;
01595       plt_off3 = 20;
01596       plt_off3_value_bias = -2;
01597       plt_stub_offset = 12;
01598       plt_entry_size = PLT_ENTRY_SIZE_V32;
01599       plt_entry = elf_cris_plt_entry_v32;
01600       plt_pic_entry = elf_cris_pic_plt_entry_v32;
01601     }
01602 
01603   dynobj = elf_hash_table (info)->dynobj;
01604 
01605   if (h->plt.offset != (bfd_vma) -1)
01606     {
01607       asection *splt;
01608       asection *sgotplt;
01609       asection *sgot;
01610       asection *srela;
01611       bfd_vma got_base;
01612 
01613       bfd_vma gotplt_offset
01614        = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
01615       Elf_Internal_Rela rela;
01616       bfd_byte *loc;
01617       bfd_boolean has_gotplt = gotplt_offset != 0;
01618 
01619       /* Get the index in the procedure linkage table which
01620         corresponds to this symbol.  This is the index of this symbol
01621         in all the symbols for which we are making plt entries.  The
01622         first entry in the procedure linkage table is reserved.  */
01623       /* We have to count backwards here, and the result is only valid as
01624         an index into .got.plt and its relocations.  FIXME: Constants...  */
01625       bfd_vma gotplt_index = gotplt_offset/4 - 3;
01626 
01627       /* Get the offset into the .got table of the entry that corresponds
01628         to this function.  Note that we embed knowledge that "incoming"
01629         .got goes after .got.plt in the output without padding (pointer
01630         aligned).  However, that knowledge is present in several other
01631         places too.  */
01632       bfd_vma got_offset
01633        = (has_gotplt
01634           ? gotplt_offset
01635           : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
01636 
01637       /* This symbol has an entry in the procedure linkage table.  Set it
01638         up.  */
01639 
01640       BFD_ASSERT (h->dynindx != -1);
01641 
01642       splt = bfd_get_section_by_name (dynobj, ".plt");
01643       sgot = bfd_get_section_by_name (dynobj, ".got");
01644       sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
01645       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
01646       BFD_ASSERT (splt != NULL && sgotplt != NULL
01647                 && (! has_gotplt || srela != NULL));
01648 
01649       got_base = sgotplt->output_section->vma + sgotplt->output_offset;
01650 
01651       /* Fill in the entry in the procedure linkage table.  */
01652       if (! info->shared)
01653        {
01654          memcpy (splt->contents + h->plt.offset, plt_entry,
01655                 plt_entry_size);
01656 
01657          /* We need to enter the absolute address of the GOT entry here.  */
01658          bfd_put_32 (output_bfd, got_base + got_offset,
01659                     splt->contents + h->plt.offset + plt_off1);
01660        }
01661       else
01662        {
01663          memcpy (splt->contents + h->plt.offset, plt_pic_entry,
01664                 plt_entry_size);
01665          bfd_put_32 (output_bfd, got_offset,
01666                     splt->contents + h->plt.offset + plt_off1);
01667        }
01668 
01669       /* Fill in the plt entry and make a relocation, if this is a "real"
01670         PLT entry.  */
01671       if (has_gotplt)
01672        {
01673          /* Fill in the offset to the reloc table.  */
01674          bfd_put_32 (output_bfd,
01675                     gotplt_index * sizeof (Elf32_External_Rela),
01676                     splt->contents + h->plt.offset + plt_off2);
01677 
01678          /* Fill in the offset to the first PLT entry, where to "jump".  */
01679          bfd_put_32 (output_bfd,
01680                     - (h->plt.offset + plt_off3 + plt_off3_value_bias),
01681                     splt->contents + h->plt.offset + plt_off3);
01682 
01683          /* Fill in the entry in the global offset table with the address of
01684             the relocating stub.  */
01685          bfd_put_32 (output_bfd,
01686                     (splt->output_section->vma
01687                      + splt->output_offset
01688                      + h->plt.offset
01689                      + plt_stub_offset),
01690                     sgotplt->contents + got_offset);
01691 
01692          /* Fill in the entry in the .rela.plt section.  */
01693          rela.r_offset = (sgotplt->output_section->vma
01694                         + sgotplt->output_offset
01695                         + got_offset);
01696          rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
01697          rela.r_addend = 0;
01698          loc = srela->contents + gotplt_index * sizeof (Elf32_External_Rela);
01699          bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
01700        }
01701 
01702       if (!h->def_regular)
01703        {
01704          /* Mark the symbol as undefined, rather than as defined in
01705             the .plt section.  Leave the value alone.  */
01706          sym->st_shndx = SHN_UNDEF;
01707 
01708          /* FIXME: From elf32-sparc.c 2001-02-19 (1.18).  I still don't
01709             know whether resetting the value is significant; if it really
01710             is, rather than a quirk or bug in the sparc port, then I
01711             believe we'd see this elsewhere.  */
01712          /* If the symbol is weak, we do need to clear the value.
01713             Otherwise, the PLT entry would provide a definition for
01714             the symbol even if the symbol wasn't defined anywhere,
01715             and so the symbol would never be NULL.  */
01716          if (!h->ref_regular_nonweak)
01717            sym->st_value = 0;
01718        }
01719     }
01720 
01721   /* For an ordinary program, we emit .got relocs only for symbols that
01722      are in the dynamic-symbols table and are either defined by the
01723      program or are undefined weak symbols, or are function symbols
01724      where we do not output a PLT: the PLT reloc was output above and all
01725      references to the function symbol are redirected to the PLT.  */
01726   if (h->got.offset != (bfd_vma) -1
01727       && (info->shared
01728          || (h->dynindx != -1
01729              && h->plt.offset == (bfd_vma) -1
01730              && !h->def_regular
01731              && h->root.type != bfd_link_hash_undefweak)))
01732     {
01733       asection *sgot;
01734       asection *srela;
01735       Elf_Internal_Rela rela;
01736       bfd_byte *loc;
01737       bfd_byte *where;
01738 
01739       /* This symbol has an entry in the global offset table.  Set it up.  */
01740 
01741       sgot = bfd_get_section_by_name (dynobj, ".got");
01742       srela = bfd_get_section_by_name (dynobj, ".rela.got");
01743       BFD_ASSERT (sgot != NULL && srela != NULL);
01744 
01745       rela.r_offset = (sgot->output_section->vma
01746                      + sgot->output_offset
01747                      + (h->got.offset &~ (bfd_vma) 1));
01748 
01749       /* If this is a static link, or it is a -Bsymbolic link and the
01750         symbol is defined locally or was forced to be local because
01751         of a version file, we just want to emit a RELATIVE reloc.
01752         The entry in the global offset table will already have been
01753         initialized in the relocate_section function.  */
01754       where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
01755       if (! elf_hash_table (info)->dynamic_sections_created
01756          || (info->shared
01757              && (info->symbolic || h->dynindx == -1)
01758              && h->def_regular))
01759        {
01760          rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
01761          rela.r_addend = bfd_get_signed_32 (output_bfd, where);
01762        }
01763       else
01764        {
01765          bfd_put_32 (output_bfd, (bfd_vma) 0, where);
01766          rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
01767          rela.r_addend = 0;
01768        }
01769 
01770       loc = srela->contents;
01771       loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
01772       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
01773     }
01774 
01775   if (h->needs_copy)
01776     {
01777       asection *s;
01778       Elf_Internal_Rela rela;
01779       bfd_byte *loc;
01780 
01781       /* This symbol needs a copy reloc.  Set it up.  */
01782 
01783       BFD_ASSERT (h->dynindx != -1
01784                 && (h->root.type == bfd_link_hash_defined
01785                     || h->root.type == bfd_link_hash_defweak));
01786 
01787       s = bfd_get_section_by_name (h->root.u.def.section->owner,
01788                                ".rela.bss");
01789       BFD_ASSERT (s != NULL);
01790 
01791       rela.r_offset = (h->root.u.def.value
01792                      + h->root.u.def.section->output_section->vma
01793                      + h->root.u.def.section->output_offset);
01794       rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
01795       rela.r_addend = 0;
01796       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
01797       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
01798     }
01799 
01800   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
01801   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
01802       || h == elf_hash_table (info)->hgot)
01803     sym->st_shndx = SHN_ABS;
01804 
01805   return TRUE;
01806 }
01807 
01808 /* Finish up the dynamic sections.  */
01809 
01810 static bfd_boolean
01811 elf_cris_finish_dynamic_sections (output_bfd, info)
01812      bfd *output_bfd;
01813      struct bfd_link_info *info;
01814 {
01815   bfd *dynobj;
01816   asection *sgot;
01817   asection *sdyn;
01818 
01819   dynobj = elf_hash_table (info)->dynobj;
01820 
01821   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
01822   BFD_ASSERT (sgot != NULL);
01823   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
01824 
01825   if (elf_hash_table (info)->dynamic_sections_created)
01826     {
01827       asection *splt;
01828       Elf32_External_Dyn *dyncon, *dynconend;
01829 
01830       splt = bfd_get_section_by_name (dynobj, ".plt");
01831       BFD_ASSERT (splt != NULL && sdyn != NULL);
01832 
01833       dyncon = (Elf32_External_Dyn *) sdyn->contents;
01834       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
01835       for (; dyncon < dynconend; dyncon++)
01836        {
01837          Elf_Internal_Dyn dyn;
01838          asection *s;
01839 
01840          bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
01841 
01842          switch (dyn.d_tag)
01843            {
01844            default:
01845              break;
01846 
01847            case DT_PLTGOT:
01848              s = bfd_get_section_by_name (output_bfd, ".got");
01849              BFD_ASSERT (s != NULL);
01850              dyn.d_un.d_ptr = s->vma;
01851              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
01852              break;
01853 
01854            case DT_JMPREL:
01855              /* Yes, we *can* have a .plt and no .plt.rela, for instance
01856                if all symbols are found in the .got (not .got.plt).  */
01857              s = bfd_get_section_by_name (output_bfd, ".rela.plt");
01858              dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
01859              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
01860              break;
01861 
01862            case DT_PLTRELSZ:
01863              s = bfd_get_section_by_name (output_bfd, ".rela.plt");
01864              if (s == NULL)
01865               dyn.d_un.d_val = 0;
01866              else
01867               dyn.d_un.d_val = s->size;
01868              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
01869              break;
01870 
01871            case DT_RELASZ:
01872              /* The procedure linkage table relocs (DT_JMPREL) should
01873                not be included in the overall relocs (DT_RELA).
01874                Therefore, we override the DT_RELASZ entry here to
01875                make it not include the JMPREL relocs.  Since the
01876                linker script arranges for .rela.plt to follow all
01877                other relocation sections, we don't have to worry
01878                about changing the DT_RELA entry.  */
01879              s = bfd_get_section_by_name (output_bfd, ".rela.plt");
01880              if (s != NULL)
01881               dyn.d_un.d_val -= s->size;
01882              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
01883              break;
01884            }
01885        }
01886 
01887       /* Fill in the first entry in the procedure linkage table.  */
01888       if (splt->size > 0)
01889        {
01890          if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
01891            {
01892              if (info->shared)
01893               memcpy (splt->contents, elf_cris_pic_plt0_entry_v32,
01894                      PLT_ENTRY_SIZE_V32);
01895              else
01896               {
01897                 memcpy (splt->contents, elf_cris_plt0_entry_v32,
01898                        PLT_ENTRY_SIZE_V32);
01899                 bfd_put_32 (output_bfd,
01900                            sgot->output_section->vma
01901                            + sgot->output_offset + 4,
01902                            splt->contents + 4);
01903 
01904                 elf_section_data (splt->output_section)->this_hdr.sh_entsize
01905                   = PLT_ENTRY_SIZE_V32;
01906               }
01907            }
01908          else
01909            {
01910              if (info->shared)
01911               memcpy (splt->contents, elf_cris_pic_plt0_entry,
01912                      PLT_ENTRY_SIZE);
01913              else
01914               {
01915                 memcpy (splt->contents, elf_cris_plt0_entry,
01916                        PLT_ENTRY_SIZE);
01917                 bfd_put_32 (output_bfd,
01918                            sgot->output_section->vma
01919                            + sgot->output_offset + 4,
01920                            splt->contents + 6);
01921                 bfd_put_32 (output_bfd,
01922                            sgot->output_section->vma
01923                            + sgot->output_offset + 8,
01924                            splt->contents + 14);
01925 
01926                 elf_section_data (splt->output_section)->this_hdr.sh_entsize
01927                   = PLT_ENTRY_SIZE;
01928               }
01929             }
01930        }
01931     }
01932 
01933   /* Fill in the first three entries in the global offset table.  */
01934   if (sgot->size > 0)
01935     {
01936       if (sdyn == NULL)
01937        bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
01938       else
01939        bfd_put_32 (output_bfd,
01940                   sdyn->output_section->vma + sdyn->output_offset,
01941                   sgot->contents);
01942       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
01943       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
01944     }
01945 
01946   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
01947 
01948   return TRUE;
01949 }
01950 
01951 /* Return the section that should be marked against GC for a given
01952    relocation.  */
01953 
01954 static asection *
01955 cris_elf_gc_mark_hook (asection *sec,
01956                      struct bfd_link_info *info,
01957                      Elf_Internal_Rela *rel,
01958                      struct elf_link_hash_entry *h,
01959                      Elf_Internal_Sym *sym)
01960 {
01961   if (h != NULL)
01962     switch (ELF32_R_TYPE (rel->r_info))
01963       {
01964       case R_CRIS_GNU_VTINHERIT:
01965       case R_CRIS_GNU_VTENTRY:
01966        return NULL;
01967       }
01968 
01969   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
01970 }
01971 
01972 /* Update the got entry reference counts for the section being removed.  */
01973 
01974 static bfd_boolean
01975 cris_elf_gc_sweep_hook (bfd *abfd,
01976                      struct bfd_link_info *info,
01977                      asection *sec,
01978                      const Elf_Internal_Rela *relocs)
01979 {
01980   Elf_Internal_Shdr *symtab_hdr;
01981   struct elf_link_hash_entry **sym_hashes;
01982   bfd_signed_vma *local_got_refcounts;
01983   const Elf_Internal_Rela *rel, *relend;
01984   bfd *dynobj;
01985   asection *sgot;
01986   asection *srelgot;
01987 
01988   dynobj = elf_hash_table (info)->dynobj;
01989   if (dynobj == NULL)
01990     return TRUE;
01991 
01992   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
01993   sym_hashes = elf_sym_hashes (abfd);
01994   local_got_refcounts = elf_local_got_refcounts (abfd);
01995 
01996   sgot = bfd_get_section_by_name (dynobj, ".got");
01997   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
01998 
01999   relend = relocs + sec->reloc_count;
02000   for (rel = relocs; rel < relend; rel++)
02001     {
02002       unsigned long r_symndx;
02003       struct elf_link_hash_entry *h = NULL;
02004 
02005       r_symndx = ELF32_R_SYM (rel->r_info);
02006       if (r_symndx >= symtab_hdr->sh_info)
02007        {
02008          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
02009          while (h->root.type == bfd_link_hash_indirect
02010                || h->root.type == bfd_link_hash_warning)
02011            h = (struct elf_link_hash_entry *) h->root.u.i.link;
02012        }
02013 
02014       switch (ELF32_R_TYPE (rel->r_info))
02015        {
02016        case R_CRIS_16_GOT:
02017        case R_CRIS_32_GOT:
02018          if (h != NULL)
02019            {
02020              if (h->got.refcount > 0)
02021               {
02022                 --h->got.refcount;
02023                 if (h->got.refcount == 0)
02024                   {
02025                     /* We don't need the .got entry any more.  */
02026                     sgot->size -= 4;
02027                     srelgot->size -= sizeof (Elf32_External_Rela);
02028                   }
02029               }
02030              break;
02031            }
02032 
02033        local_got_reloc:
02034          if (local_got_refcounts != NULL)
02035            {
02036              if (local_got_refcounts[r_symndx] > 0)
02037               {
02038                 --local_got_refcounts[r_symndx];
02039                 if (local_got_refcounts[r_symndx] == 0)
02040                   {
02041                     /* We don't need the .got entry any more.  */
02042                     sgot->size -= 4;
02043                     if (info->shared)
02044                      srelgot->size -= sizeof (Elf32_External_Rela);
02045                   }
02046               }
02047            }
02048          break;
02049 
02050        case R_CRIS_16_GOTPLT:
02051        case R_CRIS_32_GOTPLT:
02052          /* For local symbols, treat these like GOT relocs.  */
02053          if (h == NULL)
02054            goto local_got_reloc;
02055          /* Fall through.  */
02056 
02057        case R_CRIS_32_PLT_GOTREL:
02058          /* FIXME: We don't garbage-collect away the .got section.  */
02059          if (local_got_refcounts != NULL)
02060            local_got_refcounts[-1]--;
02061          /* Fall through.  */
02062 
02063        case R_CRIS_8_PCREL:
02064        case R_CRIS_16_PCREL:
02065        case R_CRIS_32_PCREL:
02066        case R_CRIS_32_PLT_PCREL:
02067          if (h != NULL)
02068            {
02069              if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
02070                 && h->plt.refcount > 0)
02071               --h->plt.refcount;
02072            }
02073          break;
02074 
02075        default:
02076          break;
02077        }
02078     }
02079 
02080   return TRUE;
02081 }
02082 
02083 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
02084    entry but we found we will not create any.  Called when we find we will
02085    not have any PLT for this symbol, by for example
02086    elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
02087    or elf_cris_size_dynamic_sections if no dynamic sections will be
02088    created (we're only linking static objects).  */
02089 
02090 static bfd_boolean
02091 elf_cris_adjust_gotplt_to_got (h, p)
02092      struct elf_cris_link_hash_entry *h;
02093      PTR p;
02094 {
02095   struct bfd_link_info *info = (struct bfd_link_info *) p;
02096 
02097   if (h->root.root.type == bfd_link_hash_warning)
02098     h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
02099 
02100   /* If nobody wanted a GOTPLT with this symbol, we're done.  */
02101   if (h->gotplt_refcount <= 0)
02102     return TRUE;
02103 
02104   if (h->root.got.refcount > 0)
02105     {
02106       /* There's a GOT entry for this symbol.  Just adjust the refcount.
02107         Probably not necessary at this stage, but keeping it accurate
02108         helps avoiding surprises later.  */
02109       h->root.got.refcount += h->gotplt_refcount;
02110       h->gotplt_refcount = 0;
02111     }
02112   else
02113     {
02114       /* No GOT entry for this symbol.  We need to create one.  */
02115       bfd *dynobj = elf_hash_table (info)->dynobj;
02116       asection *sgot;
02117       asection *srelgot;
02118 
02119       BFD_ASSERT (dynobj != NULL);
02120       sgot = bfd_get_section_by_name (dynobj, ".got");
02121       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
02122 
02123       /* Put an accurate refcount there.  */
02124       h->root.got.refcount = h->gotplt_refcount;
02125 
02126       h->gotplt_refcount = 0;
02127 
02128       /* We always have a .got and a .rela.got section if there were
02129         GOTPLT relocs in input.  */
02130       BFD_ASSERT (sgot != NULL && srelgot != NULL);
02131 
02132       /* Allocate space in the .got section.  */
02133       sgot->size += 4;
02134 
02135       /* Allocate relocation space.  */
02136       srelgot->size += sizeof (Elf32_External_Rela);
02137     }
02138 
02139   return TRUE;
02140 }
02141 
02142 /* Try to fold PLT entries with GOT entries.  There are two cases when we
02143    want to do this:
02144 
02145    - When all PLT references are GOTPLT references, and there are GOT
02146      references, and this is not the executable.  We don't have to
02147      generate a PLT at all.
02148 
02149    - When there are both (ordinary) PLT references and GOT references,
02150      and this isn't the executable.
02151      We want to make the PLT reference use the ordinary GOT entry rather
02152      than R_CRIS_JUMP_SLOT, a run-time dynamically resolved GOTPLT entry,
02153      since the GOT entry will have to be resolved at startup anyway.
02154 
02155    Though the latter case is handled when room for the PLT is allocated,
02156    not here.
02157 
02158    By folding into the GOT, we may need a round-trip to a PLT in the
02159    executable for calls, a loss in performance.  Still, losing a
02160    reloc is a win in size and at least in start-up time.
02161 
02162    Note that this function is called before symbols are forced local by
02163    version scripts.  The differing cases are handled by
02164    elf_cris_hide_symbol.  */
02165 
02166 static bfd_boolean
02167 elf_cris_try_fold_plt_to_got (h, p)
02168      struct elf_cris_link_hash_entry *h;
02169      PTR p;
02170 {
02171   struct bfd_link_info *info = (struct bfd_link_info *) p;
02172 
02173   /* If there are no GOT references for this symbol, we can't fold any
02174      other reference so there's nothing to do.  Likewise if there are no
02175      PLT references; GOTPLT references included.  */
02176   if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
02177     return TRUE;
02178 
02179   /* GOTPLT relocs are supposed to be included into the PLT refcount.  */
02180   BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
02181 
02182   if (h->gotplt_refcount == h->root.plt.refcount)
02183     {
02184       /* The only PLT references are GOTPLT references, and there are GOT
02185         references.  Convert PLT to GOT references.  */
02186       if (! elf_cris_adjust_gotplt_to_got (h, info))
02187        return FALSE;
02188 
02189       /* Clear the PLT references, so no PLT will be created.  */
02190       h->root.plt.offset = (bfd_vma) -1;
02191     }
02192 
02193   return TRUE;
02194 }
02195 
02196 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
02197    to use a GOT entry (and create one) rather than requiring a GOTPLT
02198    entry.  */
02199 
02200 static void
02201 elf_cris_hide_symbol (info, h, force_local)
02202      struct bfd_link_info *info;
02203      struct elf_link_hash_entry *h;
02204      bfd_boolean force_local;
02205 {
02206   elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
02207 
02208   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
02209 }
02210 
02211 /* Adjust a symbol defined by a dynamic object and referenced by a
02212    regular object.  The current definition is in some section of the
02213    dynamic object, but we're not including those sections.  We have to
02214    change the definition to something the rest of the link can
02215    understand.  */
02216 
02217 static bfd_boolean
02218 elf_cris_adjust_dynamic_symbol (info, h)
02219      struct bfd_link_info *info;
02220      struct elf_link_hash_entry *h;
02221 {
02222   bfd *dynobj;
02223   asection *s;
02224   unsigned int power_of_two;
02225   bfd_size_type plt_entry_size;
02226 
02227   dynobj = elf_hash_table (info)->dynobj;
02228 
02229   /* Make sure we know what is going on here.  */
02230   BFD_ASSERT (dynobj != NULL
02231              && (h->needs_plt
02232                 || h->u.weakdef != NULL
02233                 || (h->def_dynamic
02234                     && h->ref_regular
02235                     && !h->def_regular)));
02236 
02237   plt_entry_size
02238     = (bfd_get_mach (dynobj) == bfd_mach_cris_v32
02239        ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
02240 
02241   /* If this is a function, put it in the procedure linkage table.  We
02242      will fill in the contents of the procedure linkage table later,
02243      when we know the address of the .got section.  */
02244   if (h->type == STT_FUNC
02245       || h->needs_plt)
02246     {
02247       /* If we link a program (not a DSO), we'll get rid of unnecessary
02248         PLT entries; we point to the actual symbols -- even for pic
02249         relocs, because a program built with -fpic should have the same
02250         result as one built without -fpic, specifically considering weak
02251         symbols.
02252         FIXME: m68k and i386 differ here, for unclear reasons.  */
02253       if (! info->shared
02254          && !h->def_dynamic)
02255        {
02256          /* This case can occur if we saw a PLT reloc in an input file,
02257             but the symbol was not defined by a dynamic object.  In such
02258             a case, we don't actually need to build a procedure linkage
02259             table, and we can just do an absolute or PC reloc instead, or
02260             change a .got.plt index to a .got index for GOTPLT relocs.  */
02261          BFD_ASSERT (h->needs_plt);
02262          h->needs_plt = 0;
02263          h->plt.offset = (bfd_vma) -1;
02264          return
02265            elf_cris_adjust_gotplt_to_got ((struct
02266                                        elf_cris_link_hash_entry *) h,
02267                                       info);
02268        }
02269 
02270       /* If we had a R_CRIS_GLOB_DAT that didn't have to point to a PLT;
02271         where a pointer-equivalent symbol was unimportant (i.e. more
02272         like R_CRIS_JUMP_SLOT after symbol evaluation) we could get rid
02273         of the PLT.  We can't for the executable, because the GOT
02274         entries will point to the PLT there (and be constant).  */
02275       if (info->shared
02276          && !elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry*)
02277                                        h, info))
02278        return FALSE;
02279 
02280       /* GC or folding may have rendered this entry unused.  */
02281       if (h->plt.refcount <= 0)
02282        {
02283          h->needs_plt = 0;
02284          h->plt.offset = (bfd_vma) -1;
02285          return TRUE;
02286        }
02287 
02288       /* Make sure this symbol is output as a dynamic symbol.  */
02289       if (h->dynindx == -1)
02290        {
02291          if (! bfd_elf_link_record_dynamic_symbol (info, h))
02292            return FALSE;
02293        }
02294 
02295       s = bfd_get_section_by_name (dynobj, ".plt");
02296       BFD_ASSERT (s != NULL);
02297 
02298       /* If this is the first .plt entry, make room for the special
02299         first entry.  */
02300       if (s->size == 0)
02301        s->size += plt_entry_size;
02302 
02303       /* If this symbol is not defined in a regular file, and we are
02304         not generating a shared library, then set the symbol to this
02305         location in the .plt.  */
02306       if (!info->shared
02307          && !h->def_regular)
02308        {
02309          h->root.u.def.section = s;
02310          h->root.u.def.value = s->size;
02311        }
02312 
02313       /* If there's already a GOT entry, use that, not a .got.plt.  A
02314         GOT field still has a reference count when we get here; it's
02315         not yet changed to an offset.  We can't do this for an
02316         executable, because then the reloc associated with the PLT
02317         would get a non-PLT reloc pointing to the PLT.  FIXME: Move
02318         this to elf_cris_try_fold_plt_to_got.  */
02319       if (info->shared && h->got.refcount > 0)
02320        {
02321          h->got.refcount += h->plt.refcount;
02322 
02323          /* Mark the PLT offset to use the GOT entry by setting the low
02324             bit in the plt offset; it is always a multiple of
02325             plt_entry_size (which is at least a multiple of 2).  */
02326          BFD_ASSERT ((s->size % plt_entry_size) == 0);
02327 
02328          /* Change the PLT refcount to an offset.  */
02329          h->plt.offset = s->size;
02330 
02331          /* By not setting gotplt_offset (i.e. it remains at 0), we signal
02332             that the got entry should be used instead.  */
02333          BFD_ASSERT (((struct elf_cris_link_hash_entry *)
02334                      h)->gotplt_offset == 0);
02335 
02336          /* Make room for this entry.  */
02337          s->size += plt_entry_size;
02338 
02339          return TRUE;
02340        }
02341 
02342       /* No GOT reference for this symbol; prepare for an ordinary PLT.  */
02343       h->plt.offset = s->size;
02344 
02345       /* Make room for this entry.  */
02346       s->size += plt_entry_size;
02347 
02348       /* We also need to make an entry in the .got.plt section, which
02349         will be placed in the .got section by the linker script.  */
02350       ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
02351        = elf_cris_hash_table (info)->next_gotplt_entry;
02352       elf_cris_hash_table (info)->next_gotplt_entry += 4;
02353 
02354       s = bfd_get_section_by_name (dynobj, ".got.plt");
02355       BFD_ASSERT (s != NULL);
02356       s->size += 4;
02357 
02358       /* We also need to make an entry in the .rela.plt section.  */
02359 
02360       s = bfd_get_section_by_name (dynobj, ".rela.plt");
02361       BFD_ASSERT (s != NULL);
02362       s->size += sizeof (Elf32_External_Rela);
02363 
02364       return TRUE;
02365     }
02366 
02367   /* Reinitialize the plt offset now that it is not used as a reference
02368      count any more.  */
02369   h->plt.offset = (bfd_vma) -1;
02370 
02371   /* If this is a weak symbol, and there is a real definition, the
02372      processor independent code will have arranged for us to see the
02373      real definition first, and we can just use the same value.  */
02374   if (h->u.weakdef != NULL)
02375     {
02376       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
02377                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
02378       h->root.u.def.section = h->u.weakdef->root.u.def.section;
02379       h->root.u.def.value = h->u.weakdef->root.u.def.value;
02380       return TRUE;
02381     }
02382 
02383   /* This is a reference to a symbol defined by a dynamic object which
02384      is not a function.  */
02385 
02386   /* If we are creating a shared library, we must presume that the
02387      only references to the symbol are via the global offset table.
02388      For such cases we need not do anything here; the relocations will
02389      be handled correctly by relocate_section.  */
02390   if (info->shared)
02391     return TRUE;
02392 
02393   /* If there are no references to this symbol that do not use the
02394      GOT, we don't need to generate a copy reloc.  */
02395   if (!h->non_got_ref)
02396     return TRUE;
02397 
02398   if (h->size == 0)
02399     {
02400       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
02401                           h->root.root.string);
02402       return TRUE;
02403     }
02404 
02405   /* We must allocate the symbol in our .dynbss section, which will
02406      become part of the .bss section of the executable.  There will be
02407      an entry for this symbol in the .dynsym section.  The dynamic
02408      object will contain position independent code, so all references
02409      from the dynamic object to this symbol will go through the global
02410      offset table.  The dynamic linker will use the .dynsym entry to
02411      determine the address it must put in the global offset table, so
02412      both the dynamic object and the regular object will refer to the
02413      same memory location for the variable.  */
02414 
02415   s = bfd_get_section_by_name (dynobj, ".dynbss");
02416   BFD_ASSERT (s != NULL);
02417 
02418   /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
02419      copy the initial value out of the dynamic object and into the
02420      runtime process image.  We need to remember the offset into the
02421      .rela.bss section we are going to use.  */
02422   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
02423     {
02424       asection *srel;
02425 
02426       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
02427       BFD_ASSERT (srel != NULL);
02428       srel->size += sizeof (Elf32_External_Rela);
02429       h->needs_copy = 1;
02430     }
02431 
02432   /* Historic precedent: m68k and i386 allow max 8-byte alignment for the
02433      thing to copy; so do we.  */
02434 
02435   /* We need to figure out the alignment required for this symbol.  I
02436      have no idea how ELF linkers handle this.  */
02437   power_of_two = bfd_log2 (h->size);
02438   if (power_of_two > 3)
02439     power_of_two = 3;
02440 
02441   /* Apply the required alignment.  */
02442   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
02443   if (power_of_two > bfd_get_section_alignment (dynobj, s))
02444     {
02445       if (!bfd_set_section_alignment (dynobj, s, power_of_two))
02446        return FALSE;
02447     }
02448 
02449   /* Define the symbol as being at this point in the section.  */
02450   h->root.u.def.section = s;
02451   h->root.u.def.value = s->size;
02452 
02453   /* Increment the section size to make room for the symbol.  */
02454   s->size += h->size;
02455 
02456   return TRUE;
02457 }
02458 
02459 /* Look through the relocs for a section during the first phase.  */
02460 
02461 static bfd_boolean
02462 cris_elf_check_relocs (abfd, info, sec, relocs)
02463      bfd *abfd;
02464      struct bfd_link_info *info;
02465      asection *sec;
02466      const Elf_Internal_Rela *relocs;
02467 {
02468   bfd *dynobj;
02469   Elf_Internal_Shdr *symtab_hdr;
02470   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
02471   bfd_signed_vma *local_got_refcounts;
02472   const Elf_Internal_Rela *rel;
02473   const Elf_Internal_Rela *rel_end;
02474   asection *sgot;
02475   asection *srelgot;
02476   asection *sreloc;
02477 
02478   if (info->relocatable)
02479     return TRUE;
02480 
02481   dynobj = elf_hash_table (info)->dynobj;
02482   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
02483   sym_hashes = elf_sym_hashes (abfd);
02484   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
02485   local_got_refcounts = elf_local_got_refcounts (abfd);
02486 
02487   sgot = NULL;
02488   srelgot = NULL;
02489   sreloc = NULL;
02490 
02491   if (!elf_bad_symtab (abfd))
02492     sym_hashes_end -= symtab_hdr->sh_info;
02493 
02494   rel_end = relocs + sec->reloc_count;
02495   for (rel = relocs; rel < rel_end; rel++)
02496     {
02497       struct elf_link_hash_entry *h;
02498       unsigned long r_symndx;
02499       enum elf_cris_reloc_type r_type;
02500 
02501       r_symndx = ELF32_R_SYM (rel->r_info);
02502       if (r_symndx < symtab_hdr->sh_info)
02503         h = NULL;
02504       else
02505        {
02506          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
02507          while (h->root.type == bfd_link_hash_indirect
02508                || h->root.type == bfd_link_hash_warning)
02509            h = (struct elf_link_hash_entry *) h->root.u.i.link;
02510        }
02511 
02512       r_type = ELF32_R_TYPE (rel->r_info);
02513 
02514       /* Some relocs require linker-created sections; we need to hang them
02515         on the first input bfd we found that contained dynamic relocs.  */
02516       switch (r_type)
02517        {
02518        case R_CRIS_16_GOT:
02519        case R_CRIS_32_GOT:
02520        case R_CRIS_32_GOTREL:
02521        case R_CRIS_32_PLT_GOTREL:
02522        case R_CRIS_32_PLT_PCREL:
02523        case R_CRIS_16_GOTPLT:
02524        case R_CRIS_32_GOTPLT:
02525          if (dynobj == NULL)
02526            {
02527              elf_hash_table (info)->dynobj = dynobj = abfd;
02528 
02529              /* We could handle this if we can get a handle on the
02530                output bfd in elf_cris_adjust_dynamic_symbol.  Failing
02531                that, we must insist on dynobj being a specific mach.  */
02532              if (bfd_get_mach (dynobj) == bfd_mach_cris_v10_v32)
02533               {
02534                 (*_bfd_error_handler)
02535                   (_("%B, section %A:\n  v10/v32 compatible object %s"
02536                      " must not contain a PIC relocation"),
02537                    abfd, sec);
02538                 return FALSE;
02539               }
02540 
02541              /* Create the .got section, so we can assume it's always
02542                present whenever there's a dynobj.  */
02543              if (!_bfd_elf_create_got_section (dynobj, info))
02544               return FALSE;
02545            }
02546          break;
02547 
02548        default:
02549          break;
02550        }
02551 
02552       /* Some relocs require a global offset table (but perhaps not a
02553         specific GOT entry).  */
02554       switch (r_type)
02555        {
02556          /* For R_CRIS_16_GOTPLT and R_CRIS_32_GOTPLT, we need a GOT
02557             entry only for local symbols.  Unfortunately, we don't know
02558             until later on if there's a version script that forces the
02559             symbol local.  We must have the .rela.got section in place
02560             before we know if the symbol looks global now, so we need
02561             to treat the reloc just like for R_CRIS_16_GOT and
02562             R_CRIS_32_GOT.  */
02563        case R_CRIS_16_GOTPLT:
02564        case R_CRIS_32_GOTPLT:
02565        case R_CRIS_16_GOT:
02566        case R_CRIS_32_GOT:
02567          if (srelgot == NULL
02568              && (h != NULL || info->shared))
02569            {
02570              srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
02571              if (srelgot == NULL)
02572               {
02573                 srelgot = bfd_make_section_with_flags (dynobj,
02574                                                   ".rela.got",
02575                                                   (SEC_ALLOC
02576                                                    | SEC_LOAD
02577                                                    | SEC_HAS_CONTENTS
02578                                                    | SEC_IN_MEMORY
02579                                                    | SEC_LINKER_CREATED
02580                                                    | SEC_READONLY));
02581                 if (srelgot == NULL
02582                     || !bfd_set_section_alignment (dynobj, srelgot, 2))
02583                   return FALSE;
02584               }
02585            }
02586          /* Fall through.  */
02587 
02588        case R_CRIS_32_GOTREL:
02589        case R_CRIS_32_PLT_GOTREL:
02590          if (sgot == NULL)
02591            sgot = bfd_get_section_by_name (dynobj, ".got");
02592 
02593          if (local_got_refcounts == NULL)
02594            {
02595              bfd_size_type amt;
02596 
02597              /* We use index local_got_refcounts[-1] to count all
02598                GOT-relative relocations that do not have explicit
02599                GOT entries.  */
02600              amt = symtab_hdr->sh_info + 1;
02601              amt *= sizeof (bfd_signed_vma);
02602              local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
02603              if (local_got_refcounts == NULL)
02604               return FALSE;
02605 
02606              local_got_refcounts++;
02607              elf_local_got_refcounts (abfd) = local_got_refcounts;
02608            }
02609          break;
02610 
02611        default:
02612          break;
02613        }
02614 
02615       switch (r_type)
02616         {
02617        case R_CRIS_16_GOTPLT:
02618        case R_CRIS_32_GOTPLT:
02619          /* Mark that we need a GOT entry if the PLT entry (and its GOT
02620             entry) is eliminated.  We can only do this for a non-local
02621             symbol.  */
02622          if (h != NULL)
02623            {
02624              ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++;
02625              goto handle_gotplt_reloc;
02626            }
02627          /* If h is NULL then this is a local symbol, and we must make a
02628             GOT entry for it, so handle it like a GOT reloc.  */
02629          /* Fall through.  */
02630 
02631        case R_CRIS_16_GOT:
02632        case R_CRIS_32_GOT:
02633          /* This symbol requires a global offset table entry.  */
02634          if (h != NULL)
02635            {
02636              if (h->got.refcount == 0)
02637               {
02638                 /* Make sure this symbol is output as a dynamic symbol.  */
02639                 if (h->dynindx == -1)
02640                   {
02641                     if (!bfd_elf_link_record_dynamic_symbol (info, h))
02642                      return FALSE;
02643                   }
02644 
02645                 /* Allocate space in the .got section.  */
02646                 sgot->size += 4;
02647                 /* Allocate relocation space.  */
02648                 srelgot->size += sizeof (Elf32_External_Rela);
02649               }
02650              h->got.refcount++;
02651            }
02652          else
02653            {
02654              /* This is a global offset table entry for a local symbol.  */
02655              if (local_got_refcounts[r_symndx] == 0)
02656               {
02657                 sgot->size += 4;
02658                 if (info->shared)
02659                   {
02660                     /* If we are generating a shared object, we need to
02661                       output a R_CRIS_RELATIVE reloc so that the dynamic
02662                       linker can adjust this GOT entry.  */
02663                     srelgot->size += sizeof (Elf32_External_Rela);
02664                   }
02665               }
02666              local_got_refcounts[r_symndx]++;
02667            }
02668          break;
02669 
02670        case R_CRIS_32_GOTREL:
02671          /* This reference requires a global offset table.
02672             FIXME: The actual refcount isn't used currently; the .got
02673             section can't be removed if there were any references in the
02674             input.  */
02675          local_got_refcounts[-1]++;
02676          break;
02677 
02678        handle_gotplt_reloc:
02679 
02680        case R_CRIS_32_PLT_GOTREL:
02681          /* This reference requires a global offset table.  */
02682          local_got_refcounts[-1]++;
02683          /* Fall through.  */
02684 
02685        case R_CRIS_32_PLT_PCREL:
02686          /* This symbol requires a procedure linkage table entry.  We
02687             actually build the entry in adjust_dynamic_symbol,
02688              because this might be a case of linking PIC code which is
02689              never referenced by a dynamic object, in which case we
02690              don't need to generate a procedure linkage table entry
02691              after all.  */
02692 
02693          /* Beware: if we'd check for visibility of the symbol here
02694             (and not marking the need for a PLT when non-visible), we'd
02695             get into trouble with keeping handling consistent with
02696             regards to relocs found before definition and GOTPLT
02697             handling.  Eliminable PLT entries will be dealt with later
02698             anyway.  */
02699          if (h == NULL)
02700            continue;
02701 
02702          h->needs_plt = 1;
02703          h->plt.refcount++;
02704          break;
02705 
02706        case R_CRIS_8:
02707        case R_CRIS_16:
02708        case R_CRIS_32:
02709          /* Let's help debug shared library creation.  Any of these
02710             relocs can be used in shared libs, but pages containing them
02711             cannot be shared.  Don't warn for sections we don't care
02712             about, such as debug sections or non-constant sections.  We
02713             can't help tables of (global) function pointers, for example,
02714             though they must be emitted in a data section to avoid having
02715             impure text sections.  */
02716          if (info->shared
02717              && (sec->flags & SEC_ALLOC) != 0
02718              && (sec->flags & SEC_READONLY) != 0)
02719            {
02720              /* FIXME: How do we make this optionally a warning only?  */
02721              (*_bfd_error_handler)
02722               (_("%B, section %A:\n  relocation %s should not"
02723                  " be used in a shared object; recompile with -fPIC"),
02724                abfd,
02725                sec,
02726                cris_elf_howto_table[r_type].name);
02727            }
02728          /* Fall through.  */
02729 
02730        case R_CRIS_8_PCREL:
02731        case R_CRIS_16_PCREL:
02732        case R_CRIS_32_PCREL:
02733          if (h != NULL)
02734            {
02735              h->non_got_ref = 1;
02736 
02737              /* Make sure a plt entry is created for this symbol if it
02738                turns out to be a function defined by a dynamic object.  */
02739              if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
02740               h->plt.refcount++;
02741            }
02742 
02743          /* If we are creating a shared library and this is not a local
02744             symbol, we need to copy the reloc into the shared library.
02745             However when linking with -Bsymbolic and this is a global
02746             symbol which is defined in an object we are including in the
02747             link (i.e., DEF_REGULAR is set), then we can resolve the
02748             reloc directly.  At this point we have not seen all the input
02749             files, so it is possible that DEF_REGULAR is not set now but
02750             will be set later (it is never cleared).  In case of a weak
02751             definition, DEF_REGULAR may be cleared later by a strong
02752             definition in a shared library.  We account for that
02753             possibility below by storing information in the relocs_copied
02754             field of the hash table entry.  A similar situation occurs
02755             when creating shared libraries and symbol visibility changes
02756             render the symbol local.  */
02757 
02758          /* No need to do anything if we're not creating a shared object.  */
02759          if (! info->shared)
02760            break;
02761 
02762          /* We don't need to handle relocs into sections not going into
02763             the "real" output.  */
02764          if ((sec->flags & SEC_ALLOC) == 0)
02765            break;
02766 
02767          /* We can only eliminate PC-relative relocs.  */
02768          if (r_type == R_CRIS_8_PCREL
02769              || r_type == R_CRIS_16_PCREL
02770              || r_type == R_CRIS_32_PCREL)
02771            {
02772              /* If the symbol is local, then we can eliminate the reloc.  */
02773              if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
02774               break;
02775 
02776              /* If this is with -Bsymbolic and the symbol isn't weak, and
02777                is defined by an ordinary object (the ones we include in
02778                this shared library) then we can also eliminate the
02779                reloc.  See comment above for more eliminable cases which
02780                we can't identify at this time.  */
02781              if (info->symbolic
02782                 && h->root.type != bfd_link_hash_defweak
02783                 && h->def_regular)
02784               break;
02785 
02786              if ((sec->flags & SEC_READONLY) != 0)
02787               {
02788                 /* FIXME: How do we make this optionally a warning only?  */
02789                 (*_bfd_error_handler)
02790                   (_("%B, section %A:\n  relocation %s should not be used"
02791                      " in a shared object; recompile with -fPIC"),
02792                    abfd,
02793                    sec,
02794                    cris_elf_howto_table[r_type].name);
02795               }
02796            }
02797 
02798          /* We create a reloc section in dynobj and make room for this
02799             reloc.  */
02800          if (sreloc == NULL)
02801            {
02802              const char *name;
02803 
02804              name = (bfd_elf_string_from_elf_section
02805                     (abfd,
02806                      elf_elfheader (abfd)->e_shstrndx,
02807                      elf_section_data (sec)->rel_hdr.sh_name));
02808              if (name == NULL)
02809               return FALSE;
02810 
02811              BFD_ASSERT (CONST_STRNEQ (name, ".rela")
02812                        && strcmp (bfd_get_section_name (abfd, sec),
02813                                  name + 5) == 0);
02814 
02815              sreloc = bfd_get_section_by_name (dynobj, name);
02816              if (sreloc == NULL)
02817               {
02818                 sreloc = bfd_make_section_with_flags (dynobj, name,
02819                                                  (SEC_ALLOC
02820                                                   | SEC_LOAD
02821                                                   | SEC_HAS_CONTENTS
02822                                                   | SEC_IN_MEMORY
02823                                                   | SEC_LINKER_CREATED
02824                                                   | SEC_READONLY));
02825                 if (sreloc == NULL
02826                     || !bfd_set_section_alignment (dynobj, sreloc, 2))
02827                   return FALSE;
02828               }
02829              if (sec->flags & SEC_READONLY)
02830               info->flags |= DF_TEXTREL;
02831            }
02832 
02833          sreloc->size += sizeof (Elf32_External_Rela);
02834 
02835          /* If we are linking with -Bsymbolic, we count the number of PC
02836             relative relocations we have entered for this symbol, so that
02837             we can discard them again if the symbol is later defined by a
02838             regular object.  We know that h is really a pointer to an
02839             elf_cris_link_hash_entry.  */
02840          if ((r_type == R_CRIS_8_PCREL
02841               || r_type == R_CRIS_16_PCREL
02842               || r_type == R_CRIS_32_PCREL)
02843              && info->symbolic)
02844            {
02845              struct elf_cris_link_hash_entry *eh;
02846              struct elf_cris_pcrel_relocs_copied *p;
02847 
02848              eh = (struct elf_cris_link_hash_entry *) h;
02849 
02850              for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
02851               if (p->section == sreloc)
02852                 break;
02853 
02854              if (p == NULL)
02855               {
02856                 p = ((struct elf_cris_pcrel_relocs_copied *)
02857                      bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
02858                 if (p == NULL)
02859                   return FALSE;
02860                 p->next = eh->pcrel_relocs_copied;
02861                 eh->pcrel_relocs_copied = p;
02862                 p->section = sreloc;
02863                 p->count = 0;
02864               }
02865 
02866              ++p->count;
02867            }
02868          break;
02869 
02870         /* This relocation describes the C++ object vtable hierarchy.
02871            Reconstruct it for later use during GC.  */
02872         case R_CRIS_GNU_VTINHERIT:
02873           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
02874             return FALSE;
02875           break;
02876 
02877         /* This relocation describes which C++ vtable entries are actually
02878            used.  Record for later use during GC.  */
02879         case R_CRIS_GNU_VTENTRY:
02880           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
02881             return FALSE;
02882           break;
02883 
02884        default:
02885          /* Other relocs do not appear here.  */
02886          bfd_set_error (bfd_error_bad_value);
02887          return FALSE;
02888         }
02889     }
02890 
02891   return TRUE;
02892 }
02893 
02894 /* Set the sizes of the dynamic sections.  */
02895 
02896 static bfd_boolean
02897 elf_cris_size_dynamic_sections (output_bfd, info)
02898      bfd *output_bfd ATTRIBUTE_UNUSED;
02899      struct bfd_link_info *info;
02900 {
02901   bfd *dynobj;
02902   asection *s;
02903   bfd_boolean plt;
02904   bfd_boolean relocs;
02905 
02906   dynobj = elf_hash_table (info)->dynobj;
02907   BFD_ASSERT (dynobj != NULL);
02908 
02909   if (elf_hash_table (info)->dynamic_sections_created)
02910     {
02911       /* Set the contents of the .interp section to the interpreter.  */
02912       if (info->executable)
02913        {
02914          s = bfd_get_section_by_name (dynobj, ".interp");
02915          BFD_ASSERT (s != NULL);
02916          s->size = sizeof ELF_DYNAMIC_INTERPRETER;
02917          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
02918        }
02919     }
02920   else
02921     {
02922       /* Adjust all expected GOTPLT uses to use a GOT entry instead.  */
02923       elf_cris_link_hash_traverse (elf_cris_hash_table (info),
02924                                elf_cris_adjust_gotplt_to_got,
02925                                (PTR) info);
02926 
02927       /* We may have created entries in the .rela.got section.
02928         However, if we are not creating the dynamic sections, we will
02929         not actually use these entries.  Reset the size of .rela.got,
02930         which will cause it to get stripped from the output file
02931         below.  */
02932       s = bfd_get_section_by_name (dynobj, ".rela.got");
02933       if (s != NULL)
02934        s->size = 0;
02935     }
02936 
02937   /* If this is a -Bsymbolic shared link, then we need to discard all PC
02938      relative relocs against symbols defined in a regular object.  We
02939      allocated space for them in the check_relocs routine, but we will not
02940      fill them in in the relocate_section routine.  We also discard space
02941      for relocs that have become for local symbols due to symbol
02942      visibility changes.  For programs, we discard space for relocs for
02943      symbols not referenced by any dynamic object.  */
02944   if (info->shared)
02945     elf_cris_link_hash_traverse (elf_cris_hash_table (info),
02946                              elf_cris_discard_excess_dso_dynamics,
02947                              (PTR) info);
02948   else
02949     elf_cris_link_hash_traverse (elf_cris_hash_table (info),
02950                              elf_cris_discard_excess_program_dynamics,
02951                              (PTR) info);
02952 
02953   /* The check_relocs and adjust_dynamic_symbol entry points have
02954      determined the sizes of the various dynamic sections.  Allocate
02955      memory for them.  */
02956   plt = FALSE;
02957   relocs = FALSE;
02958   for (s = dynobj->sections; s != NULL; s = s->next)
02959     {
02960       const char *name;
02961 
02962       if ((s->flags & SEC_LINKER_CREATED) == 0)
02963        continue;
02964 
02965       /* It's OK to base decisions on the section name, because none
02966         of the dynobj section names depend upon the input files.  */
02967       name = bfd_get_section_name (dynobj, s);
02968 
02969       if (strcmp (name, ".plt") == 0)
02970        {
02971          /* Remember whether there is a PLT.  */
02972          plt = s->size != 0;
02973        }
02974       else if (CONST_STRNEQ (name, ".rela"))
02975        {
02976          if (s->size != 0)
02977            {
02978              /* Remember whether there are any reloc sections other
02979                  than .rela.plt.  */
02980              if (strcmp (name, ".rela.plt") != 0)
02981                 relocs = TRUE;
02982 
02983              /* We use the reloc_count field as a counter if we need
02984                to copy relocs into the output file.  */
02985              s->reloc_count = 0;
02986            }
02987        }
02988       else if (! CONST_STRNEQ (name, ".got")
02989               && strcmp (name, ".dynbss") != 0)
02990        {
02991          /* It's not one of our sections, so don't allocate space.  */
02992          continue;
02993        }
02994 
02995       if (s->size == 0)
02996        {
02997          /* If we don't need this section, strip it from the
02998             output file.  This is mostly to handle .rela.bss and
02999             .rela.plt.  We must create both sections in
03000             create_dynamic_sections, because they must be created
03001             before the linker maps input sections to output
03002             sections.  The linker does that before
03003             adjust_dynamic_symbol is called, and it is that
03004             function which decides whether anything needs to go
03005             into these sections.  */
03006          s->flags |= SEC_EXCLUDE;
03007          continue;
03008        }
03009 
03010       if ((s->flags & SEC_HAS_CONTENTS) == 0)
03011        continue;
03012 
03013       /* Allocate memory for the section contents. We use bfd_zalloc here
03014         in case unused entries are not reclaimed before the section's
03015         contents are written out.  This should not happen, but this way
03016         if it does, we will not write out garbage.  For reloc sections,
03017         this will make entries have the type R_CRIS_NONE.  */
03018       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
03019       if (s->contents == NULL)
03020        return FALSE;
03021     }
03022 
03023   if (elf_hash_table (info)->dynamic_sections_created)
03024     {
03025       /* Add some entries to the .dynamic section.  We fill in the
03026         values later, in elf_cris_finish_dynamic_sections, but we
03027         must add the entries now so that we get the correct size for
03028         the .dynamic section.  The DT_DEBUG entry is filled in by the
03029         dynamic linker and used by the debugger.  */
03030 #define add_dynamic_entry(TAG, VAL) \
03031   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
03032 
03033       if (!info->shared)
03034        {
03035          if (!add_dynamic_entry (DT_DEBUG, 0))
03036            return FALSE;
03037        }
03038 
03039       if (plt)
03040        {
03041          if (!add_dynamic_entry (DT_PLTGOT, 0)
03042              || !add_dynamic_entry (DT_PLTRELSZ, 0)
03043              || !add_dynamic_entry (DT_PLTREL, DT_RELA)
03044              || !add_dynamic_entry (DT_JMPREL, 0))
03045            return FALSE;
03046        }
03047 
03048       if (relocs)
03049        {
03050          if (!add_dynamic_entry (DT_RELA, 0)
03051              || !add_dynamic_entry (DT_RELASZ, 0)
03052              || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
03053            return FALSE;
03054        }
03055 
03056       if ((info->flags & DF_TEXTREL) != 0)
03057        {
03058          if (!add_dynamic_entry (DT_TEXTREL, 0))
03059            return FALSE;
03060          info->flags |= DF_TEXTREL;
03061        }
03062     }
03063 #undef add_dynamic_entry
03064 
03065   return TRUE;
03066 }
03067 
03068 /* This function is called via elf_cris_link_hash_traverse if we are
03069    creating a shared object.  In the -Bsymbolic case, it discards the
03070    space allocated to copy PC relative relocs against symbols which
03071    are defined in regular objects.  For the normal non-symbolic case,
03072    we also discard space for relocs that have become local due to
03073    symbol visibility changes.  We allocated space for them in the
03074    check_relocs routine, but we won't fill them in in the
03075    relocate_section routine.  */
03076 
03077 static bfd_boolean
03078 elf_cris_discard_excess_dso_dynamics (h, inf)
03079      struct elf_cris_link_hash_entry *h;
03080      PTR inf;
03081 {
03082   struct elf_cris_pcrel_relocs_copied *s;
03083   struct bfd_link_info *info = (struct bfd_link_info *) inf;
03084 
03085   if (h->root.root.type == bfd_link_hash_warning)
03086     h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
03087 
03088   /* If a symbol has been forced local or we have found a regular
03089      definition for the symbolic link case, then we won't be needing
03090      any relocs.  */
03091   if (h->root.def_regular
03092       && (h->root.forced_local
03093          || info->symbolic))
03094     {
03095       for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
03096        s->section->size -= s->count * sizeof (Elf32_External_Rela);
03097     }
03098 
03099   return TRUE;
03100 }
03101 
03102 /* This function is called via elf_cris_link_hash_traverse if we are *not*
03103    creating a shared object.  We discard space for relocs for symbols put
03104    in the .got, but which we found we do not have to resolve at run-time.  */
03105 
03106 static bfd_boolean
03107 elf_cris_discard_excess_program_dynamics (h, inf)
03108      struct elf_cris_link_hash_entry *h;
03109      PTR inf;
03110 {
03111   struct bfd_link_info *info = (struct bfd_link_info *) inf;
03112 
03113   if (h->root.root.type == bfd_link_hash_warning)
03114     h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
03115 
03116   /* If we're not creating a shared library and have a symbol which is
03117      referred to by .got references, but the symbol is defined locally,
03118      (or rather, not defined by a DSO) then lose the reloc for the .got
03119      (don't allocate room for it).  Likewise for relocs for something
03120      for which we create a PLT.  */
03121   if (!h->root.def_dynamic
03122       || h->root.plt.refcount > 0)
03123     {
03124       if (h->root.got.refcount > 0
03125          /* The size of this section is only valid and in sync with the
03126             various reference counts if we do dynamic; don't decrement it
03127             otherwise.  */
03128          && elf_hash_table (info)->dynamic_sections_created)
03129        {
03130          bfd *dynobj = elf_hash_table (info)->dynobj;
03131          asection *srelgot;
03132 
03133          BFD_ASSERT (dynobj != NULL);
03134 
03135          srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
03136 
03137          BFD_ASSERT (srelgot != NULL);
03138 
03139          srelgot->size -= sizeof (Elf32_External_Rela);
03140        }
03141 
03142       /* If the locally-defined symbol isn't used by a DSO, then we don't
03143         have to export it as a dynamic symbol.  This was already done for
03144         functions; doing this for all symbols would presumably not
03145         introduce new problems.  Of course we don't do this if we're
03146         exporting all dynamic symbols.  */
03147       if (! info->export_dynamic
03148          && h->root.dynindx != -1
03149          && !h->root.def_dynamic
03150          && !h->root.ref_dynamic)
03151        {
03152          h->root.dynindx = -1;
03153          _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
03154                               h->root.dynstr_index);
03155        }
03156     }
03157 
03158   return TRUE;
03159 }
03160 
03161 /* Reject a file depending on presence and expectation of prefixed
03162    underscores on symbols.  */
03163 
03164 static bfd_boolean
03165 cris_elf_object_p (abfd)
03166      bfd *abfd;
03167 {
03168   if (! cris_elf_set_mach_from_flags (abfd, elf_elfheader (abfd)->e_flags))
03169     return FALSE;
03170 
03171   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
03172     return (bfd_get_symbol_leading_char (abfd) == '_');
03173   else
03174     return (bfd_get_symbol_leading_char (abfd) == 0);
03175 }
03176 
03177 /* Mark presence or absence of leading underscore.  Set machine type
03178    flags from mach type.  */
03179 
03180 static void
03181 cris_elf_final_write_processing (abfd, linker)
03182      bfd *abfd;
03183      bfd_boolean linker ATTRIBUTE_UNUSED;
03184 {
03185   unsigned long e_flags = elf_elfheader (abfd)->e_flags;
03186 
03187   e_flags &= ~EF_CRIS_UNDERSCORE;
03188   if (bfd_get_symbol_leading_char (abfd) == '_')
03189     e_flags |= EF_CRIS_UNDERSCORE;
03190 
03191   switch (bfd_get_mach (abfd))
03192     {
03193     case bfd_mach_cris_v0_v10:
03194       e_flags |= EF_CRIS_VARIANT_ANY_V0_V10;
03195       break;
03196 
03197     case bfd_mach_cris_v10_v32:
03198       e_flags |= EF_CRIS_VARIANT_COMMON_V10_V32;
03199       break;
03200 
03201     case bfd_mach_cris_v32:
03202       e_flags |= EF_CRIS_VARIANT_V32;
03203       break;
03204 
03205     default:
03206       _bfd_abort (__FILE__, __LINE__,
03207                 _("Unexpected machine number"));
03208     }
03209 
03210   elf_elfheader (abfd)->e_flags = e_flags;
03211 }
03212 
03213 /* Set the mach type from e_flags value.  */
03214 
03215 static bfd_boolean
03216 cris_elf_set_mach_from_flags (abfd, flags)
03217      bfd *abfd;
03218      unsigned long flags;
03219 {
03220   switch (flags & EF_CRIS_VARIANT_MASK)
03221     {
03222     case EF_CRIS_VARIANT_ANY_V0_V10:
03223       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v0_v10);
03224       break;
03225 
03226     case EF_CRIS_VARIANT_V32:
03227       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v32);
03228       break;
03229 
03230     case EF_CRIS_VARIANT_COMMON_V10_V32:
03231       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v10_v32);
03232       break;
03233 
03234     default:
03235       /* Since we don't recognize them, we obviously can't support them
03236         with this code; we'd have to require that all future handling
03237         would be optional.  */
03238       bfd_set_error (bfd_error_wrong_format);
03239       return FALSE;
03240     }
03241 
03242   return TRUE;
03243 }
03244 
03245 /* Display the flags field.  */
03246 
03247 static bfd_boolean
03248 cris_elf_print_private_bfd_data (abfd, ptr)
03249      bfd *abfd;
03250      PTR ptr;
03251 {
03252   FILE *file = (FILE *) ptr;
03253 
03254   BFD_ASSERT (abfd != NULL && ptr != NULL);
03255 
03256   _bfd_elf_print_private_bfd_data (abfd, ptr);
03257 
03258   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
03259 
03260   if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
03261     fprintf (file, _(" [symbols have a _ prefix]"));
03262   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
03263       == EF_CRIS_VARIANT_COMMON_V10_V32)
03264     fprintf (file, _(" [v10 and v32]"));
03265   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
03266       == EF_CRIS_VARIANT_V32)
03267     fprintf (file, _(" [v32]"));
03268 
03269   fputc ('\n', file);
03270   return TRUE;
03271 }
03272 
03273 /* Don't mix files with and without a leading underscore.  */
03274 
03275 static bfd_boolean
03276 cris_elf_merge_private_bfd_data (ibfd, obfd)
03277      bfd *ibfd;
03278      bfd *obfd;
03279 {
03280   int imach, omach;
03281 
03282   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
03283     return FALSE;
03284 
03285   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
03286       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
03287     return TRUE;
03288 
03289   imach = bfd_get_mach (ibfd);
03290 
03291   if (! elf_flags_init (obfd))
03292     {
03293       /* This happens when ld starts out with a 'blank' output file.  */
03294       elf_flags_init (obfd) = TRUE;
03295 
03296       /* We ignore the linker-set mach, and instead set it according to
03297         the first input file.  This would also happen if we could
03298         somehow filter out the OUTPUT_ARCH () setting from elf.sc.
03299         This allows us to keep the same linker config across
03300         cris(v0..v10) and crisv32.  The drawback is that we can't force
03301         the output type, which might be a sane thing to do for a
03302         v10+v32 compatibility object.  */
03303       if (! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
03304        return FALSE;
03305     }
03306 
03307   if (bfd_get_symbol_leading_char (ibfd)
03308       != bfd_get_symbol_leading_char (obfd))
03309     {
03310       (*_bfd_error_handler)
03311        (bfd_get_symbol_leading_char (ibfd) == '_'
03312         ? _("%B: uses _-prefixed symbols, but writing file with non-prefixed symbols")
03313         : _("%B: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
03314         ibfd);
03315       bfd_set_error (bfd_error_bad_value);
03316       return FALSE;
03317     }
03318 
03319   omach = bfd_get_mach (obfd);
03320 
03321   if (imach != omach)
03322     {
03323       /* We can get an incompatible combination only if either is
03324         bfd_mach_cris_v32, and the other one isn't compatible.  */
03325       if ((imach == bfd_mach_cris_v32
03326           && omach != bfd_mach_cris_v10_v32)
03327          || (omach == bfd_mach_cris_v32
03328              && imach != bfd_mach_cris_v10_v32))
03329        {
03330          (*_bfd_error_handler)
03331            ((imach == bfd_mach_cris_v32)
03332             ? _("%B contains CRIS v32 code, incompatible"
03333                " with previous objects")
03334             : _("%B contains non-CRIS-v32 code, incompatible"
03335                " with previous objects"),
03336             ibfd);
03337          bfd_set_error (bfd_error_bad_value);
03338          return FALSE;
03339        }
03340 
03341       /* We don't have to check the case where the input is compatible
03342         with v10 and v32, because the output is already known to be set
03343         to the other (compatible) mach.  */
03344       if (omach == bfd_mach_cris_v10_v32
03345          && ! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
03346        return FALSE;
03347     }
03348 
03349   return TRUE;
03350 }
03351 
03352 /* Do side-effects of e_flags copying to obfd.  */
03353 
03354 static bfd_boolean
03355 cris_elf_copy_private_bfd_data (ibfd, obfd)
03356      bfd *ibfd;
03357      bfd *obfd;
03358 {
03359   /* Call the base function.  */
03360   if (!_bfd_elf_copy_private_bfd_data (ibfd, obfd))
03361     return FALSE;
03362 
03363   /* If output is big-endian for some obscure reason, stop here.  */
03364   if (_bfd_generic_verify_endian_match (ibfd, obfd) == FALSE)
03365     return FALSE;
03366 
03367   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
03368       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
03369     return TRUE;
03370 
03371   /* Do what we really came here for.  */
03372   return bfd_set_arch_mach (obfd, bfd_arch_cris, bfd_get_mach (ibfd));
03373 }
03374 
03375 static enum elf_reloc_type_class
03376 elf_cris_reloc_type_class (rela)
03377      const Elf_Internal_Rela *rela;
03378 {
03379   switch ((int) ELF32_R_TYPE (rela->r_info))
03380     {
03381     case R_CRIS_RELATIVE:
03382       return reloc_class_relative;
03383     case R_CRIS_JUMP_SLOT:
03384       return reloc_class_plt;
03385     case R_CRIS_COPY:
03386       return reloc_class_copy;
03387     default:
03388       return reloc_class_normal;
03389     }
03390 }
03391 
03392 #define ELF_ARCH            bfd_arch_cris
03393 #define ELF_MACHINE_CODE    EM_CRIS
03394 #define ELF_MAXPAGESIZE            0x2000
03395 
03396 #define TARGET_LITTLE_SYM   bfd_elf32_cris_vec
03397 #define TARGET_LITTLE_NAME  "elf32-cris"
03398 #define elf_symbol_leading_char 0
03399 
03400 #define elf_info_to_howto_rel                    NULL
03401 #define elf_info_to_howto                 cris_info_to_howto_rela
03402 #define elf_backend_relocate_section             cris_elf_relocate_section
03403 #define elf_backend_gc_mark_hook          cris_elf_gc_mark_hook
03404 #define elf_backend_gc_sweep_hook         cris_elf_gc_sweep_hook
03405 #define elf_backend_check_relocs                cris_elf_check_relocs
03406 #define elf_backend_grok_prstatus         cris_elf_grok_prstatus
03407 #define elf_backend_grok_psinfo                  cris_elf_grok_psinfo
03408 
03409 #define elf_backend_can_gc_sections              1
03410 #define elf_backend_can_refcount          1
03411 
03412 #define elf_backend_object_p                     cris_elf_object_p
03413 #define elf_backend_final_write_processing \
03414        cris_elf_final_write_processing
03415 #define bfd_elf32_bfd_print_private_bfd_data \
03416        cris_elf_print_private_bfd_data
03417 #define bfd_elf32_bfd_merge_private_bfd_data \
03418        cris_elf_merge_private_bfd_data
03419 #define bfd_elf32_bfd_copy_private_bfd_data \
03420        cris_elf_copy_private_bfd_data
03421 
03422 #define bfd_elf32_bfd_reloc_type_lookup          cris_reloc_type_lookup
03423 #define bfd_elf32_bfd_reloc_name_lookup   cris_reloc_name_lookup
03424 
03425 #define bfd_elf32_bfd_link_hash_table_create \
03426        elf_cris_link_hash_table_create
03427 #define elf_backend_adjust_dynamic_symbol \
03428        elf_cris_adjust_dynamic_symbol
03429 #define elf_backend_size_dynamic_sections \
03430        elf_cris_size_dynamic_sections
03431 #define elf_backend_init_index_section           _bfd_elf_init_1_index_section
03432 #define elf_backend_finish_dynamic_symbol \
03433        elf_cris_finish_dynamic_symbol
03434 #define elf_backend_finish_dynamic_sections \
03435        elf_cris_finish_dynamic_sections
03436 #define elf_backend_create_dynamic_sections \
03437        _bfd_elf_create_dynamic_sections
03438 #define bfd_elf32_bfd_final_link \
03439        bfd_elf_gc_common_final_link
03440 #define elf_backend_hide_symbol                  elf_cris_hide_symbol
03441 #define elf_backend_reloc_type_class             elf_cris_reloc_type_class
03442 
03443 #define elf_backend_want_got_plt   1
03444 #define elf_backend_plt_readonly   1
03445 #define elf_backend_want_plt_sym   0
03446 #define elf_backend_got_header_size       12
03447 
03448 /* Later, we my want to optimize RELA entries into REL entries for dynamic
03449    linking and libraries (if it's a win of any significance).  Until then,
03450    take the easy route.  */
03451 #define elf_backend_may_use_rel_p 0
03452 #define elf_backend_may_use_rela_p 1
03453 #define elf_backend_rela_normal           1
03454 
03455 #include "elf32-target.h"
03456 
03457 #undef TARGET_LITTLE_SYM
03458 #undef TARGET_LITTLE_NAME
03459 #undef elf_symbol_leading_char
03460 
03461 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
03462 #define TARGET_LITTLE_NAME "elf32-us-cris"
03463 #define elf_symbol_leading_char '_'
03464 #undef elf32_bed
03465 #define elf32_bed elf32_us_cris_bed
03466 
03467 #include "elf32-target.h"