2001-10-20 H.J. Lu <hjl@gnu.org>
[platform/upstream/binutils.git] / bfd / elf32-cris.c
1 /* CRIS-specific support for 32-bit ELF.
2    Copyright 2000, 2001 Free Software Foundation, Inc.
3    Contributed by Axis Communications AB.
4    Written by Hans-Peter Nilsson, based on elf32-fr30.c
5    PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/cris.h"
28
29 /* Forward declarations.  */
30 static reloc_howto_type * cris_reloc_type_lookup
31   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
32
33 static void cris_info_to_howto_rela
34   PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
35
36 static boolean cris_elf_relocate_section
37   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
38            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
39
40 static bfd_reloc_status_type cris_final_link_relocate
41   PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
42            Elf_Internal_Rela *, bfd_vma));
43
44 static boolean cris_elf_gc_sweep_hook
45   PARAMS ((bfd *, struct bfd_link_info *, asection *,
46            const Elf_Internal_Rela *));
47
48 static asection * cris_elf_gc_mark_hook
49   PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
50            struct elf_link_hash_entry *, Elf_Internal_Sym *));
51
52 static boolean cris_elf_object_p PARAMS ((bfd *));
53
54 static void cris_elf_final_write_processing PARAMS ((bfd *, boolean));
55
56 static boolean cris_elf_print_private_bfd_data PARAMS ((bfd *, PTR));
57
58 static boolean cris_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
59
60 struct elf_cris_link_hash_entry;
61 static boolean elf_cris_discard_excess_dso_dynamics
62   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
63 static boolean elf_cris_discard_excess_program_dynamics
64   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
65 static boolean elf_cris_adjust_gotplt_to_got
66   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
67 static boolean elf_cris_try_fold_plt_to_got
68   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
69 static struct bfd_hash_entry *elf_cris_link_hash_newfunc
70   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
71 static struct bfd_link_hash_table *elf_cris_link_hash_table_create
72   PARAMS ((bfd *));
73 static boolean elf_cris_adjust_dynamic_symbol
74   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
75 static boolean cris_elf_check_relocs
76   PARAMS ((bfd *, struct bfd_link_info *, asection *,
77            const Elf_Internal_Rela *));
78
79 static boolean elf_cris_size_dynamic_sections
80   PARAMS ((bfd *, struct bfd_link_info *));
81 static boolean elf_cris_finish_dynamic_symbol
82   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
83            Elf_Internal_Sym *));
84 static boolean elf_cris_finish_dynamic_sections
85   PARAMS ((bfd *, struct bfd_link_info *));
86 static void elf_cris_hide_symbol
87   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
88 static enum elf_reloc_type_class elf_cris_reloc_type_class
89   PARAMS ((const Elf_Internal_Rela *));
90
91 static reloc_howto_type cris_elf_howto_table [] =
92 {
93   /* This reloc does nothing.  */
94   HOWTO (R_CRIS_NONE,           /* type */
95          0,                     /* rightshift */
96          2,                     /* size (0 = byte, 1 = short, 2 = long) */
97          32,                    /* bitsize */
98          false,                 /* pc_relative */
99          0,                     /* bitpos */
100          complain_overflow_bitfield, /* complain_on_overflow */
101          bfd_elf_generic_reloc, /* special_function */
102          "R_CRIS_NONE",         /* name */
103          false,                 /* partial_inplace */
104          0,                     /* src_mask */
105          0,                     /* dst_mask */
106          false),                /* pcrel_offset */
107
108   /* An 8 bit absolute relocation.  */
109   HOWTO (R_CRIS_8,              /* type */
110          0,                     /* rightshift */
111          0,                     /* size (0 = byte, 1 = short, 2 = long) */
112          8,                     /* bitsize */
113          false,                 /* pc_relative */
114          0,                     /* bitpos */
115          complain_overflow_bitfield, /* complain_on_overflow */
116          bfd_elf_generic_reloc, /* special_function */
117          "R_CRIS_8",            /* name */
118          false,                 /* partial_inplace */
119          0x0000,                /* src_mask */
120          0x00ff,                /* dst_mask */
121          false),                /* pcrel_offset */
122
123   /* A 16 bit absolute relocation.  */
124   HOWTO (R_CRIS_16,             /* type */
125          0,                     /* rightshift */
126          1,                     /* size (0 = byte, 1 = short, 2 = long) */
127          16,                    /* bitsize */
128          false,                 /* pc_relative */
129          0,                     /* bitpos */
130          complain_overflow_bitfield, /* complain_on_overflow */
131          bfd_elf_generic_reloc, /* special_function */
132          "R_CRIS_16",           /* name */
133          false,                 /* partial_inplace */
134          0x00000000,            /* src_mask */
135          0x0000ffff,            /* dst_mask */
136          false),                /* pcrel_offset */
137
138   /* A 32 bit absolute relocation.  */
139   HOWTO (R_CRIS_32,             /* type */
140          0,                     /* rightshift */
141          2,                     /* size (0 = byte, 1 = short, 2 = long) */
142          32,                    /* bitsize */
143          false,                 /* pc_relative */
144          0,                     /* bitpos */
145          complain_overflow_bitfield, /* complain_on_overflow */
146          bfd_elf_generic_reloc, /* special_function */
147          "R_CRIS_32",           /* name */
148          false,                 /* partial_inplace */
149          0x00000000,            /* src_mask */
150          0xffffffff,            /* dst_mask */
151          false),                /* pcrel_offset */
152
153   /* An 8 bit PC-relative relocation.  */
154   HOWTO (R_CRIS_8_PCREL,        /* type */
155          0,                     /* rightshift */
156          0,                     /* size (0 = byte, 1 = short, 2 = long) */
157          8,                     /* bitsize */
158          true,                  /* pc_relative */
159          0,                     /* bitpos */
160          complain_overflow_bitfield, /* complain_on_overflow */
161          bfd_elf_generic_reloc, /* special_function */
162          "R_CRIS_8_PCREL",      /* name */
163          false,                 /* partial_inplace */
164          0x0000,                /* src_mask */
165          0x00ff,                /* dst_mask */
166          true),                 /* pcrel_offset */
167
168   /* A 16 bit PC-relative relocation.  */
169   HOWTO (R_CRIS_16_PCREL,       /* type */
170          0,                     /* rightshift */
171          1,                     /* size (0 = byte, 1 = short, 2 = long) */
172          16,                    /* bitsize */
173          true,                  /* pc_relative */
174          0,                     /* bitpos */
175          complain_overflow_bitfield, /* complain_on_overflow */
176          bfd_elf_generic_reloc, /* special_function */
177          "R_CRIS_16",           /* name */
178          false,                 /* partial_inplace */
179          0x00000000,            /* src_mask */
180          0x0000ffff,            /* dst_mask */
181          true),                 /* pcrel_offset */
182
183   /* A 32 bit PC-relative relocation.  */
184   HOWTO (R_CRIS_32_PCREL,       /* type */
185          0,                     /* rightshift */
186          2,                     /* size (0 = byte, 1 = short, 2 = long) */
187          32,                    /* bitsize */
188          true,                  /* pc_relative */
189          0,                     /* bitpos */
190          complain_overflow_bitfield, /* complain_on_overflow */
191          bfd_elf_generic_reloc, /* special_function */
192          "R_CRIS_32",           /* name */
193          false,                 /* partial_inplace */
194          0x00000000,            /* src_mask */
195          0xffffffff,            /* dst_mask */
196          true),                 /* pcrel_offset */
197
198   /* GNU extension to record C++ vtable hierarchy.  */
199   HOWTO (R_CRIS_GNU_VTINHERIT,  /* type */
200          0,                     /* rightshift */
201          2,                     /* size (0 = byte, 1 = short, 2 = long) */
202          0,                     /* bitsize */
203          false,                 /* pc_relative */
204          0,                     /* bitpos */
205          complain_overflow_dont, /* complain_on_overflow */
206          NULL,                  /* special_function */
207          "R_CRIS_GNU_VTINHERIT", /* name */
208          false,                 /* partial_inplace */
209          0,                     /* src_mask */
210          0,                     /* dst_mask */
211          false),                /* pcrel_offset */
212
213   /* GNU extension to record C++ vtable member usage.  */
214   HOWTO (R_CRIS_GNU_VTENTRY,    /* type */
215          0,                     /* rightshift */
216          2,                     /* size (0 = byte, 1 = short, 2 = long) */
217          0,                     /* bitsize */
218          false,                 /* pc_relative */
219          0,                     /* bitpos */
220          complain_overflow_dont, /* complain_on_overflow */
221          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
222          "R_CRIS_GNU_VTENTRY",   /* name */
223          false,                 /* partial_inplace */
224          0,                     /* src_mask */
225          0,                     /* dst_mask */
226          false),                /* pcrel_offset */
227
228   /* This is used only by the dynamic linker.  The symbol should exist
229      both in the object being run and in some shared library.  The
230      dynamic linker copies the data addressed by the symbol from the
231      shared library into the object, because the object being
232      run has to have the data at some particular address.  */
233   HOWTO (R_CRIS_COPY,           /* type */
234          0,                     /* rightshift */
235          2,                     /* size (0 = byte, 1 = short, 2 = long) */
236          32,                    /* bitsize */
237          false,                 /* pc_relative */
238          0,                     /* bitpos */
239          complain_overflow_bitfield, /* complain_on_overflow */
240          bfd_elf_generic_reloc, /* special_function */
241          "R_CRIS_COPY",         /* name */
242          false,                 /* partial_inplace */
243          0,                     /* src_mask */
244          0,                     /* dst_mask */
245          false),                /* pcrel_offset */
246
247   /* Like R_CRIS_32, but used when setting global offset table entries.  */
248   HOWTO (R_CRIS_GLOB_DAT,       /* type */
249          0,                     /* rightshift */
250          2,                     /* size (0 = byte, 1 = short, 2 = long) */
251          32,                    /* bitsize */
252          false,                 /* pc_relative */
253          0,                     /* bitpos */
254          complain_overflow_bitfield, /* complain_on_overflow */
255          bfd_elf_generic_reloc, /* special_function */
256          "R_CRIS_GLOB_DAT",     /* name */
257          false,                 /* partial_inplace */
258          0,                     /* src_mask */
259          0xffffffff,            /* dst_mask */
260          false),                /* pcrel_offset */
261
262   /* Marks a procedure linkage table entry for a symbol.  */
263   HOWTO (R_CRIS_JUMP_SLOT,      /* type */
264          0,                     /* rightshift */
265          2,                     /* size (0 = byte, 1 = short, 2 = long) */
266          32,                    /* bitsize */
267          false,                 /* pc_relative */
268          0,                     /* bitpos */
269          complain_overflow_bitfield, /* complain_on_overflow */
270          bfd_elf_generic_reloc, /* special_function */
271          "R_CRIS_JUMP_SLOT",    /* name */
272          false,                 /* partial_inplace */
273          0,                     /* src_mask */
274          0,                     /* dst_mask */
275          false),                /* pcrel_offset */
276
277   /* Used only by the dynamic linker.  When the object is run, this
278      longword is set to the load address of the object, plus the
279      addend.  */
280   HOWTO (R_CRIS_RELATIVE,       /* type */
281          0,                     /* rightshift */
282          2,                     /* size (0 = byte, 1 = short, 2 = long) */
283          32,                    /* bitsize */
284          false,                 /* pc_relative */
285          0,                     /* bitpos */
286          complain_overflow_bitfield, /* complain_on_overflow */
287          bfd_elf_generic_reloc, /* special_function */
288          "R_CRIS_RELATIVE",     /* name */
289          false,                 /* partial_inplace */
290          0,                     /* src_mask */
291          0xffffffff,            /* dst_mask */
292          false),                /* pcrel_offset */
293
294   /* Like R_CRIS_32, but referring to the GOT table entry for the symbol.  */
295   HOWTO (R_CRIS_16_GOT,         /* type */
296          0,                     /* rightshift */
297          1,                     /* size (0 = byte, 1 = short, 2 = long) */
298          16,                    /* bitsize */
299          false,                 /* pc_relative */
300          0,                     /* bitpos */
301          complain_overflow_bitfield, /* complain_on_overflow */
302          bfd_elf_generic_reloc, /* special_function */
303          "R_CRIS_16_GOT",       /* name */
304          false,                 /* partial_inplace */
305          0,                     /* src_mask */
306          0xffff,                /* dst_mask */
307          false),                /* pcrel_offset */
308
309   HOWTO (R_CRIS_32_GOT,         /* type */
310          0,                     /* rightshift */
311          2,                     /* size (0 = byte, 1 = short, 2 = long) */
312          32,                    /* bitsize */
313          false,                 /* pc_relative */
314          0,                     /* bitpos */
315          complain_overflow_bitfield, /* complain_on_overflow */
316          bfd_elf_generic_reloc, /* special_function */
317          "R_CRIS_32_GOT",       /* name */
318          false,                 /* partial_inplace */
319          0,                     /* src_mask */
320          0xffffffff,            /* dst_mask */
321          false),                /* pcrel_offset */
322
323   /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
324      the GOT table for the symbol.  */
325   HOWTO (R_CRIS_16_GOTPLT,      /* type */
326          0,                     /* rightshift */
327          1,                     /* size (0 = byte, 1 = short, 2 = long) */
328          16,                    /* bitsize */
329          false,                 /* pc_relative */
330          0,                     /* bitpos */
331          complain_overflow_bitfield, /* complain_on_overflow */
332          bfd_elf_generic_reloc, /* special_function */
333          "R_CRIS_16_GOTPLT",    /* name */
334          false,                 /* partial_inplace */
335          0,                     /* src_mask */
336          0xffff,                /* dst_mask */
337          false),                /* pcrel_offset */
338
339   HOWTO (R_CRIS_32_GOTPLT,      /* type */
340          0,                     /* rightshift */
341          2,                     /* size (0 = byte, 1 = short, 2 = long) */
342          32,                    /* bitsize */
343          false,                 /* pc_relative */
344          0,                     /* bitpos */
345          complain_overflow_bitfield, /* complain_on_overflow */
346          bfd_elf_generic_reloc, /* special_function */
347          "R_CRIS_32_GOTPLT",    /* name */
348          false,                 /* partial_inplace */
349          0,                     /* src_mask */
350          0xffffffff,            /* dst_mask */
351          false),                /* pcrel_offset */
352
353   /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
354      be necessary.  */
355   HOWTO (R_CRIS_32_GOTREL,      /* type */
356          0,                     /* rightshift */
357          2,                     /* size (0 = byte, 1 = short, 2 = long) */
358          32,                    /* bitsize */
359          false,                 /* pc_relative */
360          0,                     /* bitpos */
361          complain_overflow_bitfield, /* complain_on_overflow */
362          bfd_elf_generic_reloc, /* special_function */
363          "R_CRIS_32_GOTREL",    /* name */
364          false,                 /* partial_inplace */
365          0,                     /* src_mask */
366          0xffffffff,            /* dst_mask */
367          false),                /* pcrel_offset */
368
369   /* A 32-bit offset from GOT to entry for this symbol in PLT and request
370      to create PLT entry for symbol.  */
371   HOWTO (R_CRIS_32_PLT_GOTREL,  /* type */
372          0,                     /* rightshift */
373          2,                     /* size (0 = byte, 1 = short, 2 = long) */
374          32,                    /* bitsize */
375          false,                 /* pc_relative */
376          0,                     /* bitpos */
377          complain_overflow_bitfield, /* complain_on_overflow */
378          bfd_elf_generic_reloc, /* special_function */
379          "R_CRIS_32_PLT_GOTREL", /* name */
380          false,                 /* partial_inplace */
381          0,                     /* src_mask */
382          0xffffffff,            /* dst_mask */
383          false),                /* pcrel_offset */
384
385   /* A 32-bit offset from PC (location after the relocation) + addend to
386      entry for this symbol in PLT and request to create PLT entry for
387      symbol.  */
388   HOWTO (R_CRIS_32_PLT_PCREL,   /* type */
389          0,                     /* rightshift */
390          2,                     /* size (0 = byte, 1 = short, 2 = long) */
391          32,                    /* bitsize */
392          true,                  /* pc_relative */
393          0,                     /* bitpos */
394          complain_overflow_bitfield, /* complain_on_overflow */
395          bfd_elf_generic_reloc, /* special_function */
396          "R_CRIS_32_PLT_PCREL", /* name */
397          false,                 /* partial_inplace */
398          0,                     /* src_mask */
399          0xffffffff,            /* dst_mask */
400          true)                  /* pcrel_offset */
401 };
402 \f
403 /* Map BFD reloc types to CRIS ELF reloc types.  */
404
405 struct cris_reloc_map
406 {
407   bfd_reloc_code_real_type bfd_reloc_val;
408   unsigned int cris_reloc_val;
409 };
410
411 static const struct cris_reloc_map cris_reloc_map [] =
412 {
413   { BFD_RELOC_NONE,             R_CRIS_NONE },
414   { BFD_RELOC_8,                R_CRIS_8 },
415   { BFD_RELOC_16,               R_CRIS_16 },
416   { BFD_RELOC_32,               R_CRIS_32 },
417   { BFD_RELOC_8_PCREL,          R_CRIS_8_PCREL },
418   { BFD_RELOC_16_PCREL,         R_CRIS_16_PCREL },
419   { BFD_RELOC_32_PCREL,         R_CRIS_32_PCREL },
420   { BFD_RELOC_VTABLE_INHERIT,   R_CRIS_GNU_VTINHERIT },
421   { BFD_RELOC_VTABLE_ENTRY,     R_CRIS_GNU_VTENTRY },
422   { BFD_RELOC_CRIS_COPY,        R_CRIS_COPY },
423   { BFD_RELOC_CRIS_GLOB_DAT,    R_CRIS_GLOB_DAT },
424   { BFD_RELOC_CRIS_JUMP_SLOT,   R_CRIS_JUMP_SLOT },
425   { BFD_RELOC_CRIS_RELATIVE,    R_CRIS_RELATIVE },
426   { BFD_RELOC_CRIS_16_GOT,      R_CRIS_16_GOT },
427   { BFD_RELOC_CRIS_32_GOT,      R_CRIS_32_GOT },
428   { BFD_RELOC_CRIS_16_GOTPLT,   R_CRIS_16_GOTPLT },
429   { BFD_RELOC_CRIS_32_GOTPLT,   R_CRIS_32_GOTPLT },
430   { BFD_RELOC_CRIS_32_GOTREL,   R_CRIS_32_GOTREL },
431   { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
432   { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL }
433 };
434
435 static reloc_howto_type *
436 cris_reloc_type_lookup (abfd, code)
437      bfd * abfd ATTRIBUTE_UNUSED;
438      bfd_reloc_code_real_type code;
439 {
440   unsigned int i;
441
442   for (i = sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]);
443        --i >= 0;)
444     if (cris_reloc_map [i].bfd_reloc_val == code)
445       return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
446
447   return NULL;
448 }
449
450 /* Set the howto pointer for an CRIS ELF reloc.  */
451
452 static void
453 cris_info_to_howto_rela (abfd, cache_ptr, dst)
454      bfd * abfd ATTRIBUTE_UNUSED;
455      arelent * cache_ptr;
456      Elf32_Internal_Rela * dst;
457 {
458   unsigned int r_type;
459
460   r_type = ELF32_R_TYPE (dst->r_info);
461   BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
462   cache_ptr->howto = & cris_elf_howto_table [r_type];
463 }
464 \f
465 /* The name of the dynamic interpreter.  This is put in the .interp
466    section.  */
467
468 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
469
470 /* The size in bytes of an entry in the procedure linkage table.  */
471
472 #define PLT_ENTRY_SIZE 20
473
474 /* The first entry in an absolute procedure linkage table looks like this.  */
475
476 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
477 {
478   0xfc, 0xe1,
479   0x7e, 0x7e,   /* push mof. */
480   0x7f, 0x0d,   /*  (dip [pc+]) */
481   0, 0, 0, 0,   /*  Replaced with address of .got + 4.  */
482   0x30, 0x7a,   /* move [...],mof */
483   0x7f, 0x0d,   /*  (dip [pc+]) */
484   0, 0, 0, 0,   /*  Replaced with address of .got + 8.  */
485   0x30, 0x09,   /* jump [...] */
486 };
487
488 /* Subsequent entries in an absolute procedure linkage table look like
489    this.  */
490
491 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
492 {
493   0x7f, 0x0d,   /*  (dip [pc+]) */
494   0, 0, 0, 0,   /*  Replaced with address of this symbol in .got.  */
495   0x30, 0x09,   /* jump [...] */
496   0x3f,  0x7e,  /* move [pc+],mof */
497   0, 0, 0, 0,   /*  Replaced with offset into relocation table.  */
498   0x2f, 0xfe,   /* add.d [pc+],pc */
499   0xec, 0xff,
500   0xff, 0xff    /*  Replaced with offset to start of .plt.  */
501 };
502
503 /* The first entry in a PIC procedure linkage table looks like this.  */
504
505 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
506 {
507   0xfc, 0xe1, 0x7e, 0x7e,       /* push mof */
508   0x04, 0x01, 0x30, 0x7a,       /* move [r0+4],mof */
509   0x08, 0x01, 0x30, 0x09,       /* jump [r0+8] */
510   0, 0, 0, 0, 0, 0, 0, 0,       /*  Pad out to 20 bytes.  */
511 };
512
513 /* Subsequent entries in a PIC procedure linkage table look like this.  */
514
515 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
516 {
517   0x6f, 0x0d,   /*  (bdap [pc+].d,r0) */
518   0, 0, 0, 0,   /*  Replaced with offset of this symbol in .got.  */
519   0x30, 0x09,   /* jump [...] */
520   0x3f, 0x7e,   /* move [pc+],mof */
521   0, 0, 0, 0,   /*  Replaced with offset into relocation table.  */
522   0x2f, 0xfe,   /* add.d [pc+],pc */
523   0xec, 0xff,   /*  Replaced with offset to start of .plt.  */
524   0xff, 0xff
525 };
526 \f
527 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
528    (and most other PIC/shlib stuff).  Check that we don't drift away
529    without reason.
530
531    The CRIS linker, like the m68k and i386 linkers (and probably the rest
532    too) needs to keep track of the number of relocs that it decides to
533    copy in check_relocs for each symbol.  This is so that it can discard
534    PC relative relocs if it doesn't need them when linking with
535    -Bsymbolic.  We store the information in a field extending the regular
536    ELF linker hash table.  */
537
538 /* This structure keeps track of the number of PC relative relocs we have
539    copied for a given symbol.  */
540
541 struct elf_cris_pcrel_relocs_copied
542 {
543   /* Next section.  */
544   struct elf_cris_pcrel_relocs_copied *next;
545   /* A section in dynobj.  */
546   asection *section;
547   /* Number of relocs copied in this section.  */
548   bfd_size_type count;
549 };
550
551 /* CRIS ELF linker hash entry.  */
552
553 struct elf_cris_link_hash_entry
554 {
555   struct elf_link_hash_entry root;
556
557   /* Number of PC relative relocs copied for this symbol.  */
558   struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
559
560   /* The GOTPLT references are CRIS-specific; the goal is to avoid having
561      both a general GOT and a PLT-specific GOT entry for the same symbol,
562      when it is referenced both as a function and as a function pointer.
563
564      Number of GOTPLT references for a function.  */
565   bfd_signed_vma gotplt_refcount;
566
567   /* Actual GOTPLT index for this symbol, if applicable, or zero if not
568      (zero is never used as an index).  FIXME: We should be able to fold
569      this with gotplt_refcount in a union, like the got and plt unions in
570      elf_link_hash_entry.  */
571   bfd_size_type gotplt_offset;
572 };
573
574 /* CRIS ELF linker hash table.  */
575
576 struct elf_cris_link_hash_table
577 {
578   struct elf_link_hash_table root;
579
580   /* We can't use the PLT offset and calculate to get the GOTPLT offset,
581      since we try and avoid creating GOTPLT:s when there's already a GOT.
582      Instead, we keep and update the next available index here.  */
583   bfd_size_type next_gotplt_entry;
584 };
585
586 /* Traverse a CRIS ELF linker hash table.  */
587
588 #define elf_cris_link_hash_traverse(table, func, info)                  \
589   (elf_link_hash_traverse                                               \
590    (&(table)->root,                                                     \
591     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),  \
592     (info)))
593
594 /* Get the CRIS ELF linker hash table from a link_info structure.  */
595
596 #define elf_cris_hash_table(p) \
597   ((struct elf_cris_link_hash_table *) (p)->hash)
598
599 /* Create an entry in a CRIS ELF linker hash table.  */
600
601 static struct bfd_hash_entry *
602 elf_cris_link_hash_newfunc (entry, table, string)
603      struct bfd_hash_entry *entry;
604      struct bfd_hash_table *table;
605      const char *string;
606 {
607   struct elf_cris_link_hash_entry *ret =
608     (struct elf_cris_link_hash_entry *) entry;
609
610   /* Allocate the structure if it has not already been allocated by a
611      subclass.  */
612   if (ret == (struct elf_cris_link_hash_entry *) NULL)
613     ret = ((struct elf_cris_link_hash_entry *)
614            bfd_hash_allocate (table,
615                               sizeof (struct elf_cris_link_hash_entry)));
616   if (ret == (struct elf_cris_link_hash_entry *) NULL)
617     return (struct bfd_hash_entry *) ret;
618
619   /* Call the allocation method of the superclass.  */
620   ret = ((struct elf_cris_link_hash_entry *)
621          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
622                                      table, string));
623   if (ret != (struct elf_cris_link_hash_entry *) NULL)
624     {
625       ret->pcrel_relocs_copied = NULL;
626       ret->gotplt_refcount = 0;
627       ret->gotplt_offset = 0;
628     }
629
630   return (struct bfd_hash_entry *) ret;
631 }
632
633 /* Create a CRIS ELF linker hash table.  */
634
635 static struct bfd_link_hash_table *
636 elf_cris_link_hash_table_create (abfd)
637      bfd *abfd;
638 {
639   struct elf_cris_link_hash_table *ret;
640   bfd_size_type amt = sizeof (struct elf_cris_link_hash_table);
641
642   ret = ((struct elf_cris_link_hash_table *) bfd_alloc (abfd, amt));
643   if (ret == (struct elf_cris_link_hash_table *) NULL)
644     return NULL;
645
646   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
647                                        elf_cris_link_hash_newfunc))
648     {
649       bfd_release (abfd, ret);
650       return NULL;
651     }
652
653   /* Initialize to skip over the first three entries in the gotplt; they
654      are used for run-time symbol evaluation.  */
655   ret->next_gotplt_entry = 12;
656
657   return &ret->root.root;
658 }
659 \f
660 /* Perform a single relocation.  By default we use the standard BFD
661    routines, with a few tweaks.  */
662
663 static bfd_reloc_status_type
664 cris_final_link_relocate (howto, input_bfd, input_section, contents, rel,
665                           relocation)
666      reloc_howto_type *  howto;
667      bfd *               input_bfd;
668      asection *          input_section;
669      bfd_byte *          contents;
670      Elf_Internal_Rela * rel;
671      bfd_vma             relocation;
672 {
673   bfd_reloc_status_type r;
674
675   /* PC-relative relocations are relative to the position *after*
676      the reloc.  Note that for R_CRIS_8_PCREL the adjustment is
677      not a single byte, since PC must be 16-bit-aligned.  */
678   switch (ELF32_R_TYPE (rel->r_info))
679     {
680       /* Check that the 16-bit GOT relocs are positive.  */
681     case R_CRIS_16_GOTPLT:
682     case R_CRIS_16_GOT:
683       if ((bfd_signed_vma) relocation < 0)
684         return bfd_reloc_overflow;
685       break;
686
687     case R_CRIS_32_PLT_PCREL:
688     case R_CRIS_32_PCREL:
689       relocation -= 2;
690       /* Fall through.  */
691     case R_CRIS_8_PCREL:
692     case R_CRIS_16_PCREL:
693       relocation -= 2;
694       break;
695
696     default:
697       break;
698     }
699
700   r = _bfd_final_link_relocate (howto, input_bfd, input_section,
701                                 contents, rel->r_offset,
702                                 relocation, rel->r_addend);
703   return r;
704 }
705 \f
706 /* Relocate an CRIS ELF section.  See elf32-fr30.c, from where this was
707    copied, for further comments.  */
708
709 static boolean
710 cris_elf_relocate_section (output_bfd, info, input_bfd, input_section,
711                            contents, relocs, local_syms, local_sections)
712      bfd *                   output_bfd ATTRIBUTE_UNUSED;
713      struct bfd_link_info *  info;
714      bfd *                   input_bfd;
715      asection *              input_section;
716      bfd_byte *              contents;
717      Elf_Internal_Rela *     relocs;
718      Elf_Internal_Sym *      local_syms;
719      asection **             local_sections;
720 {
721   bfd *dynobj;
722   Elf_Internal_Shdr *           symtab_hdr;
723   struct elf_link_hash_entry ** sym_hashes;
724   bfd_vma *local_got_offsets;
725   asection *sgot;
726   asection *splt;
727   asection *sreloc;
728   Elf_Internal_Rela *           rel;
729   Elf_Internal_Rela *           relend;
730
731   dynobj = elf_hash_table (info)->dynobj;
732   local_got_offsets = elf_local_got_offsets (input_bfd);
733   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
734   sym_hashes = elf_sym_hashes (input_bfd);
735   relend     = relocs + input_section->reloc_count;
736
737   /* It seems this can happen with erroneous or unsupported input (mixing
738      a.out and elf in an archive, for example.)  */
739   if (sym_hashes == NULL)
740     return false;
741
742   sgot = NULL;
743   splt = NULL;
744   sreloc = NULL;
745
746   if (dynobj != NULL)
747     {
748       splt = bfd_get_section_by_name (dynobj, ".plt");
749       sgot = bfd_get_section_by_name (dynobj, ".got");
750     }
751
752   for (rel = relocs; rel < relend; rel ++)
753     {
754       reloc_howto_type *           howto;
755       unsigned long                r_symndx;
756       Elf_Internal_Sym *           sym;
757       asection *                   sec;
758       struct elf_link_hash_entry * h;
759       bfd_vma                      relocation;
760       bfd_reloc_status_type        r;
761       const char *                 symname = NULL;
762       int                          r_type;
763
764       r_type = ELF32_R_TYPE (rel->r_info);
765
766       if (   r_type == R_CRIS_GNU_VTINHERIT
767           || r_type == R_CRIS_GNU_VTENTRY)
768         continue;
769
770       r_symndx = ELF32_R_SYM (rel->r_info);
771
772       if (info->relocateable)
773         {
774           /* This is a relocateable link.  We don't have to change
775              anything, unless the reloc is against a section symbol,
776              in which case we have to adjust according to where the
777              section symbol winds up in the output section.  */
778           if (r_symndx < symtab_hdr->sh_info)
779             {
780               sym = local_syms + r_symndx;
781
782               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
783                 {
784                   sec = local_sections [r_symndx];
785                   rel->r_addend += sec->output_offset + sym->st_value;
786                 }
787             }
788
789           continue;
790         }
791
792       /* This is a final link.  */
793       howto  = cris_elf_howto_table + r_type;
794       h      = NULL;
795       sym    = NULL;
796       sec    = NULL;
797
798       if (r_symndx < symtab_hdr->sh_info)
799         {
800           sym = local_syms + r_symndx;
801           sec = local_sections [r_symndx];
802           relocation = (sec->output_section->vma
803                         + sec->output_offset
804                         + sym->st_value);
805
806           symname = (bfd_elf_string_from_elf_section
807                      (input_bfd, symtab_hdr->sh_link, sym->st_name));
808           if (symname == NULL)
809             symname = bfd_section_name (input_bfd, sec);
810         }
811       else
812         {
813           h = sym_hashes [r_symndx - symtab_hdr->sh_info];
814
815           while (h->root.type == bfd_link_hash_indirect
816                  || h->root.type == bfd_link_hash_warning)
817             h = (struct elf_link_hash_entry *) h->root.u.i.link;
818
819           symname = h->root.root.string;
820
821           if (h->root.type == bfd_link_hash_defined
822               || h->root.type == bfd_link_hash_defweak)
823             {
824               sec = h->root.u.def.section;
825
826               /* Perhaps we should detect the cases that
827                  sec->output_section is expected to be NULL like i386 and
828                  m68k, but apparently (and according to elfxx-ia64.c) all
829                  valid cases are where the symbol is defined in a shared
830                  object which we link dynamically against.  This includes
831                  PLT relocs for which we've created a PLT entry and other
832                  relocs for which we're prepared to create dynamic
833                  relocations.
834
835                  For now, new situations cause us to just err when
836                  sec->output_offset is NULL but the object with the symbol
837                  is *not* dynamically linked against.  Thus this will
838                  automatically remind us so we can see if there are other
839                  valid cases we need to revisit.  */
840               if ((sec->output_section == NULL
841                    && (sec->owner->flags & DYNAMIC) != 0)
842
843                   /* Here follow the cases where the relocation value must
844                      be zero (or when further handling is simplified when
845                      zero).  I can't claim to understand the various
846                      conditions and they weren't described in the files
847                      where I copied them from (elf32-m68k.c and
848                      elf32-i386.c), but let's mention examples of where
849                      they happen.  FIXME: Perhaps define and use a
850                      dynamic_symbol_p function like ia64.
851
852                      - When creating a shared library, we can have an
853                      ordinary relocation for a symbol defined in a shared
854                      library (perhaps the one we create).  We then make
855                      the relocation value zero, as the value seen now will
856                      be added into the relocation addend in this shared
857                      library, but must be handled only at dynamic-link
858                      time.  FIXME: Not sure this example covers the
859                      h->elf_link_hash_flags test, though it's there in
860                      other targets.  */
861                   || (info->shared
862                       && ((! info->symbolic && h->dynindx != -1)
863                           || (h->elf_link_hash_flags
864                               & ELF_LINK_HASH_DEF_REGULAR) == 0)
865                       && (input_section->flags & SEC_ALLOC) != 0
866                       && (r_type == R_CRIS_8
867                           || r_type == R_CRIS_16
868                           || r_type == R_CRIS_32
869                           || r_type == R_CRIS_8_PCREL
870                           || r_type == R_CRIS_16_PCREL
871                           || r_type == R_CRIS_32_PCREL)))
872                 relocation = 0;
873               else if (sec->output_section != NULL)
874                 relocation = (h->root.u.def.value
875                               + sec->output_section->vma
876                               + sec->output_offset);
877               else
878                 {
879                   (*_bfd_error_handler)
880                     (_("%s: unresolvable relocation %s against symbol `%s' from %s section"),
881                      bfd_archive_filename (input_bfd),
882                      cris_elf_howto_table[r_type].name,
883                      symname,
884                      bfd_get_section_name (input_bfd, input_section));
885                   bfd_set_error (bfd_error_bad_value);
886                   return false;
887                 }
888             }
889           else if (h->root.type == bfd_link_hash_undefweak)
890             relocation = 0;
891           else if (info->shared
892                    && (!info->symbolic || info->allow_shlib_undefined)
893                    && !info->no_undefined
894                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
895             relocation = 0;
896           else
897             {
898               if (!(info->callbacks->undefined_symbol
899                     (info, symname, input_bfd,
900                      input_section, rel->r_offset,
901                      (!info->shared || info->no_undefined
902                       || ELF_ST_VISIBILITY (h->other)))))
903                 return false;
904               relocation = 0;
905             }
906         }
907
908       switch (r_type)
909         {
910         case R_CRIS_16_GOTPLT:
911         case R_CRIS_32_GOTPLT:
912           /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
913              but we require a PLT, and the PLT handling will take care of
914              filling in the PLT-specific GOT entry.  For the GOT offset,
915              calculate it as we do when filling it in for the .got.plt
916              section.  If we don't have a PLT, punt to GOT handling.  */
917           if (h != NULL
918               && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
919             {
920               asection *sgotplt
921                 = bfd_get_section_by_name (dynobj, ".got.plt");
922               bfd_vma got_offset;
923
924               BFD_ASSERT (h->dynindx != -1);
925               BFD_ASSERT (sgotplt != NULL);
926
927               got_offset
928                 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
929
930               relocation = got_offset;
931               break;
932             }
933
934           /* We didn't make a PLT entry for this symbol.  Maybe everything is
935              folded into the GOT.  Other than folding, this happens when
936              statically linking PIC code, or when using -Bsymbolic.  Check
937              that we instead have a GOT entry as done for us by
938              elf_cris_adjust_dynamic_symbol, and drop through into the
939              ordinary GOT cases.  */
940           if (h != NULL && h->got.offset == (bfd_vma) -1)
941             {
942               (*_bfd_error_handler)
943                 (_("%s: No PLT nor GOT for relocation %s against symbol `%s' from %s section"),
944                  bfd_archive_filename (input_bfd),
945                  cris_elf_howto_table[r_type].name,
946                  symname[0] != '\0' ? symname : _("[whose name is lost]"),
947                  bfd_get_section_name (input_bfd, input_section));
948
949               /* FIXME: Perhaps blaming input is not the right thing to
950                  do; this is probably an internal error.  But it is true
951                  that we didn't like that particular input.  */
952               bfd_set_error (bfd_error_bad_value);
953               return false;
954             }
955           /* Fall through.  */
956
957           /* The size of the actual relocation is not used here; we only
958              fill in the GOT table here.  */
959         case R_CRIS_16_GOT:
960         case R_CRIS_32_GOT:
961           {
962             bfd_vma off;
963
964             /* Note that despite using RELA relocations, the .got contents
965                is always filled in with the link-relative relocation
966                value; the addend.  */
967
968             if (h != NULL)
969               {
970                 off = h->got.offset;
971                 BFD_ASSERT (off != (bfd_vma) -1);
972
973                 if (!elf_hash_table (info)->dynamic_sections_created
974                     || (! info->shared && h->dynindx == -1)
975                     || (info->shared
976                         && (info->symbolic || h->dynindx == -1)
977                         && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
978                   {
979                     /* This wasn't checked above for ! info->shared, but
980                        must hold there if we get here; the symbol must not
981                        be used in, or defined by a DSO.  (Note that
982                        checking for ELF_LINK_HASH_DEF_REGULAR doesn't
983                        catch all cases.)  */
984                     BFD_ASSERT (info->shared
985                                 || (h->elf_link_hash_flags
986                                     & (ELF_LINK_HASH_REF_DYNAMIC
987                                        | ELF_LINK_HASH_DEF_DYNAMIC)) == 0);
988
989                     /* This is actually a static link, or it is a
990                        -Bsymbolic link and the symbol is defined
991                        locally, or the symbol was forced to be local
992                        because of a version file, or we're not creating a
993                        dynamic object and the symbol isn't referred to by
994                        a dynamic object.  We must initialize
995                        this entry in the global offset table.  Since
996                        the offset must always be a multiple of 4, we
997                        use the least significant bit to record whether
998                        we have initialized it already.
999
1000                        When doing a dynamic link, we create a .rela.got
1001                        relocation entry to initialize the value.  This
1002                        is done in the finish_dynamic_symbol routine.  */
1003                     if ((off & 1) != 0)
1004                       off &= ~1;
1005                     else
1006                       {
1007                         bfd_put_32 (output_bfd, relocation,
1008                                     sgot->contents + off);
1009                         h->got.offset |= 1;
1010                       }
1011                   }
1012               }
1013             else
1014               {
1015                 BFD_ASSERT (local_got_offsets != NULL
1016                             && local_got_offsets[r_symndx] != (bfd_vma) -1);
1017
1018                 off = local_got_offsets[r_symndx];
1019
1020                 /* The offset must always be a multiple of 4.  We use
1021                    the least significant bit to record whether we have
1022                    already generated the necessary reloc.  */
1023                 if ((off & 1) != 0)
1024                   off &= ~1;
1025                 else
1026                   {
1027                     bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1028
1029                     if (info->shared)
1030                       {
1031                         asection *srelgot;
1032                         Elf_Internal_Rela outrel;
1033
1034                         srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1035                         BFD_ASSERT (srelgot != NULL);
1036
1037                         outrel.r_offset = (sgot->output_section->vma
1038                                            + sgot->output_offset
1039                                            + off);
1040                         outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1041                         outrel.r_addend = relocation;
1042                         bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1043                                                    (((Elf32_External_Rela *)
1044                                                      srelgot->contents)
1045                                                     + srelgot->reloc_count));
1046                         ++srelgot->reloc_count;
1047                       }
1048
1049                     local_got_offsets[r_symndx] |= 1;
1050                   }
1051               }
1052
1053             relocation = sgot->output_offset + off;
1054             if (rel->r_addend != 0)
1055               {
1056                 /* We can't do anything for a relocation which is against
1057                    a symbol *plus offset*.  GOT holds relocations for
1058                    symbols.  Make this an error; the compiler isn't
1059                    allowed to pass us these kinds of things.  */
1060                 if (h == NULL)
1061                   (*_bfd_error_handler)
1062                     (_("%s: relocation %s with non-zero addend %d against local symbol from %s section"),
1063                      bfd_archive_filename (input_bfd),
1064                      cris_elf_howto_table[r_type].name,
1065                      rel->r_addend,
1066                      bfd_get_section_name (input_bfd, input_section));
1067                 else
1068                   (*_bfd_error_handler)
1069                     (_("%s: relocation %s with non-zero addend %d against symbol `%s' from %s section"),
1070                      bfd_archive_filename (input_bfd),
1071                      cris_elf_howto_table[r_type].name,
1072                      rel->r_addend,
1073                      symname[0] != '\0' ? symname : _("[whose name is lost]"),
1074                      bfd_get_section_name (input_bfd, input_section));
1075
1076                 bfd_set_error (bfd_error_bad_value);
1077                 return false;
1078               }
1079           }
1080           break;
1081
1082         case R_CRIS_32_GOTREL:
1083           /* This relocation must only be performed against local symbols.  */
1084           if (h != NULL)
1085             {
1086               (*_bfd_error_handler)
1087                 (_("%s: relocation %s is not allowed for global symbol: `%s' from %s section"),
1088                  bfd_archive_filename (input_bfd),
1089                  cris_elf_howto_table[r_type].name,
1090                  symname,
1091                  bfd_get_section_name (input_bfd, input_section));
1092               bfd_set_error (bfd_error_bad_value);
1093               return false;
1094             }
1095
1096           /* This relocation is like a PC-relative one, except the
1097              reference point is the location of GOT.  Note that
1098              sgot->output_offset is not involved in this calculation.  We
1099              always want the start of entire .got section, not the
1100              position after the reserved header.  */
1101           relocation -= sgot->output_section->vma;
1102           break;
1103
1104         case R_CRIS_32_PLT_PCREL:
1105           /* Relocation is to the entry for this symbol in the
1106              procedure linkage table.  */
1107
1108           /* Resolve a PLT_PCREL reloc against a local symbol directly,
1109              without using the procedure linkage table.  */
1110           if (h == NULL)
1111             break;
1112
1113           if (h->plt.offset == (bfd_vma) -1
1114               || splt == NULL)
1115             {
1116               /* We didn't make a PLT entry for this symbol.  This
1117                  happens when statically linking PIC code, or when
1118                  using -Bsymbolic.  */
1119               break;
1120             }
1121
1122           relocation = (splt->output_section->vma
1123                         + splt->output_offset
1124                         + h->plt.offset);
1125           break;
1126
1127         case R_CRIS_32_PLT_GOTREL:
1128           /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1129              start of the .got section.  See also comment at
1130              R_CRIS_32_GOT.  */
1131           relocation -= sgot->output_section->vma;
1132
1133           /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1134              without using the procedure linkage table.  */
1135           if (h == NULL)
1136             break;
1137
1138           if (h->plt.offset == (bfd_vma) -1
1139               || splt == NULL)
1140             {
1141               /* We didn't make a PLT entry for this symbol.  This
1142                  happens when statically linking PIC code, or when
1143                  using -Bsymbolic.  */
1144               break;
1145             }
1146
1147           relocation = (splt->output_section->vma
1148                         + splt->output_offset
1149                         + h->plt.offset
1150                         - sgot->output_section->vma);
1151           break;
1152
1153         case R_CRIS_8_PCREL:
1154         case R_CRIS_16_PCREL:
1155         case R_CRIS_32_PCREL:
1156           /* If the symbol was local, we need no shlib-specific handling.  */
1157           if (h == NULL)
1158             break;
1159
1160           /* Fall through.  */
1161         case R_CRIS_8:
1162         case R_CRIS_16:
1163         case R_CRIS_32:
1164           if (info->shared
1165               && (input_section->flags & SEC_ALLOC) != 0
1166               && ((r_type != R_CRIS_8_PCREL
1167                    && r_type != R_CRIS_16_PCREL
1168                    && r_type != R_CRIS_32_PCREL)
1169                   || (!info->symbolic
1170                       || (h->elf_link_hash_flags
1171                           & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1172             {
1173               Elf_Internal_Rela outrel;
1174               boolean skip, relocate;
1175
1176               /* When generating a shared object, these relocations
1177                  are copied into the output file to be resolved at run
1178                  time.  */
1179
1180               if (sreloc == NULL)
1181                 {
1182                   const char *name;
1183
1184                   name = (bfd_elf_string_from_elf_section
1185                           (input_bfd,
1186                            elf_elfheader (input_bfd)->e_shstrndx,
1187                            elf_section_data (input_section)->rel_hdr.sh_name));
1188                   if (name == NULL)
1189                     return false;
1190
1191                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1192                               && strcmp (bfd_get_section_name (input_bfd,
1193                                                                input_section),
1194                                          name + 5) == 0);
1195
1196                   sreloc = bfd_get_section_by_name (dynobj, name);
1197
1198                   /* That section should have been created in
1199                      cris_elf_check_relocs, but that function will not be
1200                      called for objects which fail in
1201                      cris_elf_merge_private_bfd_data.  */
1202                   if (sreloc == NULL)
1203                     {
1204                       (*_bfd_error_handler)
1205                         (_("%s: Internal inconsistency; no relocation section %s"),
1206                          bfd_archive_filename (input_bfd),
1207                          name);
1208
1209                       bfd_set_error (bfd_error_bad_value);
1210                       return false;
1211                     }
1212                 }
1213
1214               skip = false;
1215
1216               if (elf_section_data (input_section)->stab_info == NULL)
1217                 outrel.r_offset = rel->r_offset;
1218               else
1219                 {
1220                   bfd_vma off;
1221
1222                   off = (_bfd_stab_section_offset
1223                          (output_bfd, &elf_hash_table (info)->stab_info,
1224                           input_section,
1225                           &elf_section_data (input_section)->stab_info,
1226                           rel->r_offset));
1227                   if (off == (bfd_vma) -1)
1228                     skip = true;
1229                   outrel.r_offset = off;
1230                 }
1231
1232               outrel.r_offset += (input_section->output_section->vma
1233                                   + input_section->output_offset);
1234
1235               if (skip)
1236                 {
1237                   memset (&outrel, 0, sizeof outrel);
1238                   relocate = false;
1239                 }
1240               /* h->dynindx may be -1 if the symbol was marked to
1241                  become local.  */
1242               else if (h != NULL
1243                        && ((! info->symbolic && h->dynindx != -1)
1244                            || (h->elf_link_hash_flags
1245                                & ELF_LINK_HASH_DEF_REGULAR) == 0))
1246                 {
1247                   BFD_ASSERT (h->dynindx != -1);
1248                   relocate = false;
1249                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1250                   outrel.r_addend = relocation + rel->r_addend;
1251                 }
1252               else
1253                 {
1254                   if (r_type == R_CRIS_32)
1255                     {
1256                       relocate = true;
1257                       outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1258                       outrel.r_addend = relocation + rel->r_addend;
1259                     }
1260                   else
1261                     {
1262                       long indx;
1263
1264                       if (h == NULL)
1265                         sec = local_sections[r_symndx];
1266                       else
1267                         {
1268                           BFD_ASSERT (h->root.type == bfd_link_hash_defined
1269                                       || (h->root.type
1270                                           == bfd_link_hash_defweak));
1271                           sec = h->root.u.def.section;
1272                         }
1273                       if (sec != NULL && bfd_is_abs_section (sec))
1274                         indx = 0;
1275                       else if (sec == NULL || sec->owner == NULL)
1276                         {
1277                           bfd_set_error (bfd_error_bad_value);
1278                           return false;
1279                         }
1280                       else
1281                         {
1282                           asection *osec;
1283
1284                           osec = sec->output_section;
1285                           indx = elf_section_data (osec)->dynindx;
1286                           BFD_ASSERT (indx > 0);
1287                         }
1288
1289                       relocate = false;
1290                       outrel.r_info = ELF32_R_INFO (indx, r_type);
1291                       outrel.r_addend = relocation + rel->r_addend;
1292                     }
1293                 }
1294
1295               bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1296                                          (((Elf32_External_Rela *)
1297                                            sreloc->contents)
1298                                           + sreloc->reloc_count));
1299               ++sreloc->reloc_count;
1300
1301               /* This reloc will be computed at runtime, so there's no
1302                  need to do anything now, except for R_CRIS_32 relocations
1303                  that have been turned into R_CRIS_RELATIVE.  */
1304               if (!relocate)
1305                 continue;
1306             }
1307
1308           break;
1309         }
1310
1311       r = cris_final_link_relocate (howto, input_bfd, input_section,
1312                                      contents, rel, relocation);
1313
1314       if (r != bfd_reloc_ok)
1315         {
1316           const char * msg = (const char *) NULL;
1317
1318           switch (r)
1319             {
1320             case bfd_reloc_overflow:
1321               r = info->callbacks->reloc_overflow
1322                 (info, symname, howto->name, (bfd_vma) 0,
1323                  input_bfd, input_section, rel->r_offset);
1324               break;
1325
1326             case bfd_reloc_undefined:
1327               r = info->callbacks->undefined_symbol
1328                 (info, symname, input_bfd, input_section, rel->r_offset,
1329                  true);
1330               break;
1331
1332             case bfd_reloc_outofrange:
1333               msg = _("internal error: out of range error");
1334               break;
1335
1336             case bfd_reloc_notsupported:
1337               msg = _("internal error: unsupported relocation error");
1338               break;
1339
1340             case bfd_reloc_dangerous:
1341               msg = _("internal error: dangerous relocation");
1342               break;
1343
1344             default:
1345               msg = _("internal error: unknown error");
1346               break;
1347             }
1348
1349           if (msg)
1350             r = info->callbacks->warning
1351               (info, msg, symname, input_bfd, input_section, rel->r_offset);
1352
1353           if (! r)
1354             return false;
1355         }
1356     }
1357
1358   return true;
1359 }
1360 \f
1361 /* Finish up dynamic symbol handling.  We set the contents of various
1362    dynamic sections here.  */
1363
1364 static boolean
1365 elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
1366      bfd *output_bfd;
1367      struct bfd_link_info *info;
1368      struct elf_link_hash_entry *h;
1369      Elf_Internal_Sym *sym;
1370 {
1371   bfd *dynobj;
1372   int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
1373
1374   dynobj = elf_hash_table (info)->dynobj;
1375
1376   if (h->plt.offset != (bfd_vma) -1)
1377     {
1378       asection *splt;
1379       asection *sgotplt;
1380       asection *sgot;
1381       asection *srela;
1382       bfd_vma got_base;
1383
1384       bfd_vma gotplt_offset
1385         = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1386       Elf_Internal_Rela rela;
1387       boolean has_gotplt = gotplt_offset != 0;
1388
1389       /* Get the index in the procedure linkage table which
1390          corresponds to this symbol.  This is the index of this symbol
1391          in all the symbols for which we are making plt entries.  The
1392          first entry in the procedure linkage table is reserved.  */
1393       /* We have to count backwards here, and the result is only valid as
1394          an index into .got.plt and its relocations.  FIXME: Constants...  */
1395       bfd_vma gotplt_index = gotplt_offset/4 - 3;
1396
1397       /* Get the offset into the .got table of the entry that corresponds
1398          to this function.  Note that we embed knowledge that "incoming"
1399          .got goes after .got.plt in the output without padding (pointer
1400          aligned).  However, that knowledge is present in several other
1401          places too, here and in elflink.h at least.  */
1402       bfd_vma got_offset
1403         = (has_gotplt
1404            ? gotplt_offset
1405            : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
1406
1407       /* This symbol has an entry in the procedure linkage table.  Set it
1408          up.  */
1409
1410       BFD_ASSERT (h->dynindx != -1);
1411
1412       splt = bfd_get_section_by_name (dynobj, ".plt");
1413       sgot = bfd_get_section_by_name (dynobj, ".got");
1414       sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1415       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1416       BFD_ASSERT (splt != NULL && sgotplt != NULL
1417                   && (! has_gotplt || srela != NULL));
1418
1419       got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1420
1421       /* Fill in the entry in the procedure linkage table.  */
1422       if (! info->shared)
1423         {
1424           memcpy (splt->contents + h->plt.offset, elf_cris_plt_entry,
1425                   PLT_ENTRY_SIZE);
1426
1427           /* We need to enter the absolute address of the GOT entry here.  */
1428           bfd_put_32 (output_bfd, got_base + got_offset,
1429                       splt->contents + h->plt.offset + plt_off1);
1430         }
1431       else
1432         {
1433           memcpy (splt->contents + h->plt.offset, elf_cris_pic_plt_entry,
1434                   PLT_ENTRY_SIZE);
1435           bfd_put_32 (output_bfd, got_offset,
1436                       splt->contents + h->plt.offset + plt_off1);
1437         }
1438
1439       /* Fill in the plt entry and make a relocation, if this is a "real"
1440          PLT entry.  */
1441       if (has_gotplt)
1442         {
1443           /* Fill in the offset into the reloc table.  */
1444           bfd_put_32 (output_bfd,
1445                       gotplt_index * sizeof (Elf32_External_Rela),
1446                       splt->contents + h->plt.offset + plt_off2);
1447
1448           /* Fill in the offset to the first PLT entry, where to "jump".  */
1449           bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3 + 4),
1450                       splt->contents + h->plt.offset + plt_off3);
1451
1452           /* Fill in the entry in the global offset table with the address of
1453              the relocating stub.  */
1454           bfd_put_32 (output_bfd,
1455                       (splt->output_section->vma
1456                        + splt->output_offset
1457                        + h->plt.offset
1458                        + 8),
1459                       sgotplt->contents + got_offset);
1460
1461           /* Fill in the entry in the .rela.plt section.  */
1462           rela.r_offset = (sgotplt->output_section->vma
1463                            + sgotplt->output_offset
1464                            + got_offset);
1465           rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
1466           rela.r_addend = 0;
1467           bfd_elf32_swap_reloca_out (output_bfd, &rela,
1468                                      ((Elf32_External_Rela *) srela->contents
1469                                       + gotplt_index));
1470         }
1471
1472       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1473         {
1474           /* Mark the symbol as undefined, rather than as defined in
1475              the .plt section.  Leave the value alone.  */
1476           sym->st_shndx = SHN_UNDEF;
1477
1478           /* FIXME: From elf32-sparc.c 2001-02-19 (1.18).  I still don't
1479              know whether resetting the value is significant; if it really
1480              is, rather than a quirk or bug in the sparc port, then I
1481              believe we'd see this elsewhere.  */
1482           /* If the symbol is weak, we do need to clear the value.
1483              Otherwise, the PLT entry would provide a definition for
1484              the symbol even if the symbol wasn't defined anywhere,
1485              and so the symbol would never be NULL.  */
1486           if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1487               == 0)
1488             sym->st_value = 0;
1489         }
1490     }
1491
1492   /* We don't emit .got relocs for symbols that aren't in the
1493      dynamic-symbols table for an ordinary program.  */
1494   if (h->got.offset != (bfd_vma) -1
1495       && (info->shared || h->dynindx != -1))
1496     {
1497       asection *sgot;
1498       asection *srela;
1499       Elf_Internal_Rela rela;
1500       bfd_byte *where;
1501
1502       /* This symbol has an entry in the global offset table.  Set it up.  */
1503
1504       sgot = bfd_get_section_by_name (dynobj, ".got");
1505       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1506       BFD_ASSERT (sgot != NULL && srela != NULL);
1507
1508       rela.r_offset = (sgot->output_section->vma
1509                        + sgot->output_offset
1510                        + (h->got.offset &~ (bfd_vma) 1));
1511
1512       /* If this is a static link, or it is a -Bsymbolic link and the
1513          symbol is defined locally or was forced to be local because
1514          of a version file, we just want to emit a RELATIVE reloc.
1515          The entry in the global offset table will already have been
1516          initialized in the relocate_section function.  */
1517       where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
1518       if (! elf_hash_table (info)->dynamic_sections_created
1519           || (info->shared
1520               && (info->symbolic || h->dynindx == -1)
1521               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1522         {
1523           rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1524           rela.r_addend = bfd_get_signed_32 (output_bfd, where);
1525         }
1526       else
1527         {
1528           bfd_put_32 (output_bfd, (bfd_vma) 0, where);
1529           rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
1530           rela.r_addend = 0;
1531         }
1532
1533       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1534                                  ((Elf32_External_Rela *) srela->contents
1535                                   + srela->reloc_count));
1536       ++srela->reloc_count;
1537     }
1538
1539   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1540     {
1541       asection *s;
1542       Elf_Internal_Rela rela;
1543
1544       /* This symbol needs a copy reloc.  Set it up.  */
1545
1546       BFD_ASSERT (h->dynindx != -1
1547                   && (h->root.type == bfd_link_hash_defined
1548                       || h->root.type == bfd_link_hash_defweak));
1549
1550       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1551                                    ".rela.bss");
1552       BFD_ASSERT (s != NULL);
1553
1554       rela.r_offset = (h->root.u.def.value
1555                        + h->root.u.def.section->output_section->vma
1556                        + h->root.u.def.section->output_offset);
1557       rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
1558       rela.r_addend = 0;
1559       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1560                                  ((Elf32_External_Rela *) s->contents
1561                                   + s->reloc_count));
1562       ++s->reloc_count;
1563     }
1564
1565   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
1566   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1567       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1568     sym->st_shndx = SHN_ABS;
1569
1570   return true;
1571 }
1572 \f
1573 /* Finish up the dynamic sections.  */
1574
1575 static boolean
1576 elf_cris_finish_dynamic_sections (output_bfd, info)
1577      bfd *output_bfd;
1578      struct bfd_link_info *info;
1579 {
1580   bfd *dynobj;
1581   asection *sgot;
1582   asection *sdyn;
1583
1584   dynobj = elf_hash_table (info)->dynobj;
1585
1586   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1587   BFD_ASSERT (sgot != NULL);
1588   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1589
1590   if (elf_hash_table (info)->dynamic_sections_created)
1591     {
1592       asection *splt;
1593       Elf32_External_Dyn *dyncon, *dynconend;
1594
1595       splt = bfd_get_section_by_name (dynobj, ".plt");
1596       BFD_ASSERT (splt != NULL && sdyn != NULL);
1597
1598       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1599       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1600       for (; dyncon < dynconend; dyncon++)
1601         {
1602           Elf_Internal_Dyn dyn;
1603           asection *s;
1604
1605           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1606
1607           switch (dyn.d_tag)
1608             {
1609             default:
1610               break;
1611
1612             case DT_PLTGOT:
1613               s = bfd_get_section_by_name (output_bfd, ".got");
1614               BFD_ASSERT (s != NULL);
1615               dyn.d_un.d_ptr = s->vma;
1616               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1617               break;
1618
1619             case DT_JMPREL:
1620               /* Yes, we *can* have a .plt and no .plt.rela, for instance
1621                  if all symbols are found in the .got (not .got.plt).  */
1622               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1623               dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
1624               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1625               break;
1626
1627             case DT_PLTRELSZ:
1628               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1629               if (s == NULL)
1630                 dyn.d_un.d_val = 0;
1631               else if (s->_cooked_size != 0)
1632                 dyn.d_un.d_val = s->_cooked_size;
1633               else
1634                 dyn.d_un.d_val = s->_raw_size;
1635               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1636               break;
1637
1638             case DT_RELASZ:
1639               /* The procedure linkage table relocs (DT_JMPREL) should
1640                  not be included in the overall relocs (DT_RELA).
1641                  Therefore, we override the DT_RELASZ entry here to
1642                  make it not include the JMPREL relocs.  Since the
1643                  linker script arranges for .rela.plt to follow all
1644                  other relocation sections, we don't have to worry
1645                  about changing the DT_RELA entry.  */
1646               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1647               if (s != NULL)
1648                 {
1649                   if (s->_cooked_size != 0)
1650                     dyn.d_un.d_val -= s->_cooked_size;
1651                   else
1652                     dyn.d_un.d_val -= s->_raw_size;
1653                 }
1654               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1655               break;
1656             }
1657         }
1658
1659       /* Fill in the first entry in the procedure linkage table.  */
1660       if (splt->_raw_size > 0)
1661         {
1662           if (info->shared)
1663             memcpy (splt->contents, elf_cris_pic_plt0_entry, PLT_ENTRY_SIZE);
1664           else
1665             {
1666               memcpy (splt->contents, elf_cris_plt0_entry, PLT_ENTRY_SIZE);
1667               bfd_put_32 (output_bfd,
1668                           sgot->output_section->vma + sgot->output_offset + 4,
1669                           splt->contents + 6);
1670               bfd_put_32 (output_bfd,
1671                           sgot->output_section->vma + sgot->output_offset + 8,
1672                           splt->contents + 14);
1673
1674               elf_section_data (splt->output_section)->this_hdr.sh_entsize
1675                = PLT_ENTRY_SIZE;
1676             }
1677         }
1678     }
1679
1680   /* Fill in the first three entries in the global offset table.  */
1681   if (sgot->_raw_size > 0)
1682     {
1683       if (sdyn == NULL)
1684         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1685       else
1686         bfd_put_32 (output_bfd,
1687                     sdyn->output_section->vma + sdyn->output_offset,
1688                     sgot->contents);
1689       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1690       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1691     }
1692
1693   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1694
1695   return true;
1696 }
1697 \f
1698 /* Return the section that should be marked against GC for a given
1699    relocation.  */
1700
1701 static asection *
1702 cris_elf_gc_mark_hook (abfd, info, rel, h, sym)
1703      bfd *                        abfd;
1704      struct bfd_link_info *       info ATTRIBUTE_UNUSED;
1705      Elf_Internal_Rela *          rel;
1706      struct elf_link_hash_entry * h;
1707      Elf_Internal_Sym *           sym;
1708 {
1709   if (h != NULL)
1710     {
1711       switch (ELF32_R_TYPE (rel->r_info))
1712         {
1713         case R_CRIS_GNU_VTINHERIT:
1714         case R_CRIS_GNU_VTENTRY:
1715           break;
1716
1717         default:
1718           switch (h->root.type)
1719             {
1720             case bfd_link_hash_defined:
1721             case bfd_link_hash_defweak:
1722               return h->root.u.def.section;
1723
1724             case bfd_link_hash_common:
1725               return h->root.u.c.p->section;
1726
1727             default:
1728               break;
1729             }
1730         }
1731     }
1732   else
1733     {
1734       if (!(elf_bad_symtab (abfd)
1735             && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1736           && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1737                 && sym->st_shndx != SHN_COMMON))
1738         {
1739           return bfd_section_from_elf_index (abfd, sym->st_shndx);
1740         }
1741     }
1742
1743   return NULL;
1744 }
1745
1746 /* Update the got entry reference counts for the section being removed.  */
1747
1748 static boolean
1749 cris_elf_gc_sweep_hook (abfd, info, sec, relocs)
1750      bfd *                     abfd ATTRIBUTE_UNUSED;
1751      struct bfd_link_info *    info ATTRIBUTE_UNUSED;
1752      asection *                sec ATTRIBUTE_UNUSED;
1753      const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED;
1754 {
1755   Elf_Internal_Shdr *symtab_hdr;
1756   struct elf_link_hash_entry **sym_hashes;
1757   bfd_signed_vma *local_got_refcounts;
1758   const Elf_Internal_Rela *rel, *relend;
1759   unsigned long r_symndx;
1760   struct elf_link_hash_entry *h;
1761   bfd *dynobj;
1762   asection *sgot;
1763   asection *srelgot;
1764
1765   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1766   sym_hashes = elf_sym_hashes (abfd);
1767   local_got_refcounts = elf_local_got_refcounts (abfd);
1768
1769   dynobj = elf_hash_table (info)->dynobj;
1770   if (dynobj == NULL)
1771     return true;
1772
1773   sgot = bfd_get_section_by_name (dynobj, ".got");
1774   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1775
1776   relend = relocs + sec->reloc_count;
1777   for (rel = relocs; rel < relend; rel++)
1778     {
1779       switch (ELF32_R_TYPE (rel->r_info))
1780         {
1781         case R_CRIS_16_GOT:
1782         case R_CRIS_32_GOT:
1783           r_symndx = ELF32_R_SYM (rel->r_info);
1784           if (r_symndx >= symtab_hdr->sh_info)
1785             {
1786               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1787               if (h->got.refcount > 0)
1788                 {
1789                   --h->got.refcount;
1790                   if (h->got.refcount == 0)
1791                     {
1792                       /* We don't need the .got entry any more.  */
1793                       sgot->_raw_size -= 4;
1794                       srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1795                     }
1796                 }
1797               break;
1798             }
1799
1800         local_got_reloc:
1801           if (local_got_refcounts != NULL)
1802             {
1803               if (local_got_refcounts[r_symndx] > 0)
1804                 {
1805                   --local_got_refcounts[r_symndx];
1806                   if (local_got_refcounts[r_symndx] == 0)
1807                     {
1808                       /* We don't need the .got entry any more.  */
1809                       sgot->_raw_size -= 4;
1810                       if (info->shared)
1811                         srelgot->_raw_size -= sizeof (Elf32_External_Rela);
1812                     }
1813                 }
1814             }
1815           break;
1816
1817         case R_CRIS_16_GOTPLT:
1818         case R_CRIS_32_GOTPLT:
1819           /* For local symbols, treat these like GOT relocs.  */
1820           r_symndx = ELF32_R_SYM (rel->r_info);
1821           if (r_symndx < symtab_hdr->sh_info)
1822             goto local_got_reloc;
1823
1824         case R_CRIS_32_PLT_GOTREL:
1825           /* FIXME: We don't garbage-collect away the .got section.  */
1826           if (local_got_refcounts != NULL)
1827             local_got_refcounts[-1]--;
1828           /* Fall through.  */
1829
1830         case R_CRIS_8_PCREL:
1831         case R_CRIS_16_PCREL:
1832         case R_CRIS_32_PCREL:
1833         case R_CRIS_32_PLT_PCREL:
1834           r_symndx = ELF32_R_SYM (rel->r_info);
1835           if (r_symndx >= symtab_hdr->sh_info)
1836             {
1837               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1838               if (h->plt.refcount > 0)
1839                 --h->plt.refcount;
1840             }
1841           break;
1842
1843         default:
1844           break;
1845         }
1846     }
1847
1848   return true;
1849 }
1850
1851 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
1852    entry but we found we will not create any.  Called when we find we will
1853    not have any PLT for this symbol, by for example
1854    elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
1855    or elf_cris_size_dynamic_sections if no dynamic sections will be
1856    created (we're only linking static objects).  */
1857
1858 static boolean
1859 elf_cris_adjust_gotplt_to_got (h, p)
1860      struct elf_cris_link_hash_entry *h;
1861      PTR p;
1862 {
1863   struct bfd_link_info *info = (struct bfd_link_info *) p;
1864   bfd *dynobj = elf_hash_table (info)->dynobj;
1865
1866   BFD_ASSERT (dynobj != NULL);
1867
1868   /* If nobody wanted a GOTPLT with this symbol, we're done.  */
1869   if (h->gotplt_refcount <= 0)
1870     return true;
1871
1872   if (h->root.got.refcount > 0)
1873     {
1874       /* There's a GOT entry for this symbol.  Just adjust the refcount.
1875          Probably not necessary at this stage, but keeping it accurate
1876          helps avoiding surprises later.  */
1877       h->root.got.refcount += h->gotplt_refcount;
1878       h->gotplt_refcount = -1;
1879     }
1880   else
1881     {
1882       /* No GOT entry for this symbol.  We need to create one.  */
1883       asection *sgot = bfd_get_section_by_name (dynobj, ".got");
1884       asection *srelgot
1885         = bfd_get_section_by_name (dynobj, ".rela.got");
1886
1887       /* Put an accurate refcount there.  */
1888       h->root.got.refcount = h->gotplt_refcount;
1889
1890       h->gotplt_refcount = -1;
1891
1892       /* We always have a .got section when there are dynamic
1893          relocs.  */
1894       BFD_ASSERT (sgot != NULL /* Surely have .got section.  */);
1895
1896       /* We might have had a PLT but with no GOT entry and
1897          further no GOT reloc section at all needed before.
1898          Add it.  */
1899       if (srelgot == NULL)
1900         {
1901           srelgot = bfd_make_section (dynobj, ".rela.got");
1902
1903           if (srelgot == NULL
1904               || !bfd_set_section_flags (dynobj, srelgot,
1905                                          (SEC_ALLOC
1906                                           | SEC_LOAD
1907                                           | SEC_HAS_CONTENTS
1908                                           | SEC_IN_MEMORY
1909                                           | SEC_LINKER_CREATED
1910                                           | SEC_READONLY))
1911               || !bfd_set_section_alignment (dynobj, srelgot, 2))
1912             return false;
1913         }
1914
1915       /* Allocate space in the .got section.  */
1916       sgot->_raw_size += 4;
1917
1918       /* Allocate relocation space.  */
1919       srelgot->_raw_size += sizeof (Elf32_External_Rela);
1920     }
1921
1922   return true;
1923 }
1924
1925 /* Try to fold PLT entries with GOT entries.  There are two cases when we
1926    want to do this:
1927
1928    - When all PLT references are GOTPLT references, and there are GOT
1929      references.  We don't have to generate a PLT at all.
1930
1931    - When there are both (ordinary) PLT references and GOT references.
1932      We want to make the PLT reference use the ordinary GOT entry rather
1933      than a run-time dynamically resolved GOTPLT entry (since the GOT
1934      entry will have to be resolved at startup anyway).
1935
1936    Though the latter case is handled when room for the PLT is allocated,
1937    not here.
1938
1939    Note that this function is called before symbols are forced local by
1940    version scripts.  The differing cases are handled by
1941    elf_cris_hide_symbol.  */
1942
1943 static boolean
1944 elf_cris_try_fold_plt_to_got (h, p)
1945      struct elf_cris_link_hash_entry *h;
1946      PTR p;
1947 {
1948   struct bfd_link_info *info = (struct bfd_link_info *) p;
1949
1950   /* If there are no GOT references for this symbol, we can't fold any
1951      other reference so there's nothing to do.  Likewise if there are no
1952      PLT references; GOTPLT references included.  */
1953   if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
1954     return true;
1955
1956   /* GOTPLT relocs are supposed to be included into the PLT refcount.  */
1957   BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
1958
1959   if (h->gotplt_refcount == h->root.plt.refcount)
1960     {
1961       /* The only PLT references are GOTPLT references, and there are GOT
1962          references.  Convert PLT to GOT references.  */
1963       if (! elf_cris_adjust_gotplt_to_got (h, info))
1964         return false;
1965
1966       /* Clear the PLT references, so no PLT will be created.  */
1967       h->root.plt.offset = (bfd_vma) -1;
1968     }
1969
1970   return true;
1971 }
1972
1973 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
1974    to use a GOT entry (and create one) rather than requiring a GOTPLT
1975    entry.  */
1976
1977 static void
1978 elf_cris_hide_symbol (info, h)
1979      struct bfd_link_info *info;
1980      struct elf_link_hash_entry *h;
1981 {
1982   elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
1983
1984   _bfd_elf_link_hash_hide_symbol (info, h);
1985 }
1986
1987 /* Adjust a symbol defined by a dynamic object and referenced by a
1988    regular object.  The current definition is in some section of the
1989    dynamic object, but we're not including those sections.  We have to
1990    change the definition to something the rest of the link can
1991    understand.  */
1992
1993 static boolean
1994 elf_cris_adjust_dynamic_symbol (info, h)
1995      struct bfd_link_info *info;
1996      struct elf_link_hash_entry *h;
1997 {
1998   bfd *dynobj;
1999   asection *s;
2000   unsigned int power_of_two;
2001
2002   dynobj = elf_hash_table (info)->dynobj;
2003
2004   /* Make sure we know what is going on here.  */
2005   BFD_ASSERT (dynobj != NULL
2006               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2007                   || h->weakdef != NULL
2008                   || ((h->elf_link_hash_flags
2009                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2010                       && (h->elf_link_hash_flags
2011                           & ELF_LINK_HASH_REF_REGULAR) != 0
2012                       && (h->elf_link_hash_flags
2013                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2014
2015   /* If this is a function, put it in the procedure linkage table.  We
2016      will fill in the contents of the procedure linkage table later,
2017      when we know the address of the .got section.  */
2018   if (h->type == STT_FUNC
2019       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2020     {
2021       if (! info->shared
2022           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2023           && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
2024           /* We must always create the plt entry if it was referenced by a
2025              PLT relocation.  In this case we already recorded it as a
2026              dynamic symbol.  */
2027           /* FIXME: m68k and i386 differ here, for unclear reasons.  */
2028           && h->dynindx == -1)
2029         {
2030           /* This case can occur if we saw a PLT reloc in an input file,
2031              but the symbol was never referred to by a dynamic object.  In
2032              such a case, we don't actually need to build a procedure
2033              linkage table, and we can just do a PC reloc instead, or
2034              change a .got.plt index to a .got index for GOTPLT relocs.  */
2035           BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
2036           h->plt.offset = (bfd_vma) -1;
2037
2038           return
2039             elf_cris_adjust_gotplt_to_got ((struct
2040                                             elf_cris_link_hash_entry *) h,
2041                                            info);
2042         }
2043
2044       /* If there are only GOT references and GOTPLT references to this
2045          PLT entry, get rid of the PLT.  */
2046       if (! elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry *)
2047                                           h, info))
2048         return false;
2049
2050       /* GC or folding may have rendered this entry unused.  */
2051       if (h->plt.refcount <= 0)
2052         {
2053           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2054           h->plt.offset = (bfd_vma) -1;
2055           return true;
2056         }
2057
2058       /* Make sure this symbol is output as a dynamic symbol.  */
2059       if (h->dynindx == -1)
2060         {
2061           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2062             return false;
2063         }
2064
2065       s = bfd_get_section_by_name (dynobj, ".plt");
2066       BFD_ASSERT (s != NULL);
2067
2068       /* If this is the first .plt entry, make room for the special
2069          first entry.  */
2070       if (s->_raw_size == 0)
2071         s->_raw_size += PLT_ENTRY_SIZE;
2072
2073       /* If this symbol is not defined in a regular file, and we are
2074          not generating a shared library, then set the symbol to this
2075          location in the .plt.  This is required to make function
2076          pointers compare as equal between the normal executable and
2077          the shared library.  */
2078       if (!info->shared
2079           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2080         {
2081           h->root.u.def.section = s;
2082           h->root.u.def.value = s->_raw_size;
2083         }
2084
2085       /* If there's already a GOT entry, use that, not a .got.plt.  A
2086          GOT field still has a reference count when we get here; it's
2087          not yet changed to an offset.  */
2088       if (h->got.refcount > 0)
2089         {
2090           h->got.refcount += h->plt.refcount;
2091
2092           /* Mark the PLT offset to use the GOT entry by setting the low
2093              bit in the plt offset; it is always a multiple of
2094              pointer-size.  */
2095           BFD_ASSERT ((s->_raw_size & 3) == 0);
2096
2097           /* Change the PLT refcount to an offset.  */
2098           h->plt.offset = s->_raw_size;
2099
2100           /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2101              that the got entry should be used instead.  */
2102           BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2103                        h)->gotplt_offset == 0);
2104
2105           /* Make room for this entry.  */
2106           s->_raw_size += PLT_ENTRY_SIZE;
2107
2108           return true;
2109         }
2110
2111       /* No GOT reference for this symbol; prepare for an ordinary PLT.  */
2112       h->plt.offset = s->_raw_size;
2113
2114       /* Make room for this entry.  */
2115       s->_raw_size += PLT_ENTRY_SIZE;
2116
2117       /* We also need to make an entry in the .got.plt section, which
2118          will be placed in the .got section by the linker script.  */
2119       ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2120         = elf_cris_hash_table (info)->next_gotplt_entry;
2121       elf_cris_hash_table (info)->next_gotplt_entry += 4;
2122
2123       s = bfd_get_section_by_name (dynobj, ".got.plt");
2124       BFD_ASSERT (s != NULL);
2125       s->_raw_size += 4;
2126
2127       /* We also need to make an entry in the .rela.plt section.  */
2128
2129       s = bfd_get_section_by_name (dynobj, ".rela.plt");
2130       BFD_ASSERT (s != NULL);
2131       s->_raw_size += sizeof (Elf32_External_Rela);
2132
2133       return true;
2134     }
2135
2136   /* Reinitialize the plt offset now that it is not used as a reference
2137      count any more.  */
2138   h->plt.offset = (bfd_vma) -1;
2139
2140   /* If this is a weak symbol, and there is a real definition, the
2141      processor independent code will have arranged for us to see the
2142      real definition first, and we can just use the same value.  */
2143   if (h->weakdef != NULL)
2144     {
2145       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2146                   || h->weakdef->root.type == bfd_link_hash_defweak);
2147       h->root.u.def.section = h->weakdef->root.u.def.section;
2148       h->root.u.def.value = h->weakdef->root.u.def.value;
2149       return true;
2150     }
2151
2152   /* This is a reference to a symbol defined by a dynamic object which
2153      is not a function.  */
2154
2155   /* If we are creating a shared library, we must presume that the
2156      only references to the symbol are via the global offset table.
2157      For such cases we need not do anything here; the relocations will
2158      be handled correctly by relocate_section.  */
2159   if (info->shared)
2160     return true;
2161
2162   /* If there are no references to this symbol that do not use the
2163      GOT, we don't need to generate a copy reloc.  */
2164   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2165     return true;
2166
2167   /* We must allocate the symbol in our .dynbss section, which will
2168      become part of the .bss section of the executable.  There will be
2169      an entry for this symbol in the .dynsym section.  The dynamic
2170      object will contain position independent code, so all references
2171      from the dynamic object to this symbol will go through the global
2172      offset table.  The dynamic linker will use the .dynsym entry to
2173      determine the address it must put in the global offset table, so
2174      both the dynamic object and the regular object will refer to the
2175      same memory location for the variable.  */
2176
2177   s = bfd_get_section_by_name (dynobj, ".dynbss");
2178   BFD_ASSERT (s != NULL);
2179
2180   /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
2181      copy the initial value out of the dynamic object and into the
2182      runtime process image.  We need to remember the offset into the
2183      .rela.bss section we are going to use.  */
2184   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2185     {
2186       asection *srel;
2187
2188       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2189       BFD_ASSERT (srel != NULL);
2190       srel->_raw_size += sizeof (Elf32_External_Rela);
2191       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2192     }
2193
2194   /* Historic precedent: m68k and i386 allow max 8-byte alignment for the
2195      thing to copy; so do we.  */
2196
2197   /* We need to figure out the alignment required for this symbol.  I
2198      have no idea how ELF linkers handle this.  */
2199   power_of_two = bfd_log2 (h->size);
2200   if (power_of_two > 3)
2201     power_of_two = 3;
2202
2203   /* Apply the required alignment.  */
2204   s->_raw_size = BFD_ALIGN (s->_raw_size,
2205                             (bfd_size_type) (1 << power_of_two));
2206   if (power_of_two > bfd_get_section_alignment (dynobj, s))
2207     {
2208       if (!bfd_set_section_alignment (dynobj, s, power_of_two))
2209         return false;
2210     }
2211
2212   /* Define the symbol as being at this point in the section.  */
2213   h->root.u.def.section = s;
2214   h->root.u.def.value = s->_raw_size;
2215
2216   /* Increment the section size to make room for the symbol.  */
2217   s->_raw_size += h->size;
2218
2219   return true;
2220 }
2221
2222 /* Look through the relocs for a section during the first phase.  */
2223
2224 static boolean
2225 cris_elf_check_relocs (abfd, info, sec, relocs)
2226      bfd *abfd;
2227      struct bfd_link_info *info;
2228      asection *sec;
2229      const Elf_Internal_Rela *relocs;
2230 {
2231   bfd *dynobj;
2232   Elf_Internal_Shdr *symtab_hdr;
2233   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2234   bfd_signed_vma *local_got_refcounts;
2235   const Elf_Internal_Rela *rel;
2236   const Elf_Internal_Rela *rel_end;
2237   asection *sgot;
2238   asection *srelgot;
2239   asection *sreloc;
2240
2241   if (info->relocateable)
2242     return true;
2243
2244   dynobj = elf_hash_table (info)->dynobj;
2245   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2246   sym_hashes = elf_sym_hashes (abfd);
2247   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2248   local_got_refcounts = elf_local_got_refcounts (abfd);
2249
2250   sgot = NULL;
2251   srelgot = NULL;
2252   sreloc = NULL;
2253
2254   if (!elf_bad_symtab (abfd))
2255     sym_hashes_end -= symtab_hdr->sh_info;
2256
2257   rel_end = relocs + sec->reloc_count;
2258   for (rel = relocs; rel < rel_end; rel++)
2259     {
2260       struct elf_link_hash_entry *h;
2261       unsigned long r_symndx;
2262       enum elf_cris_reloc_type r_type;
2263
2264       r_symndx = ELF32_R_SYM (rel->r_info);
2265       if (r_symndx < symtab_hdr->sh_info)
2266         h = NULL;
2267       else
2268         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2269
2270       r_type = ELF32_R_TYPE (rel->r_info);
2271
2272       /* Some relocs require linker-created sections; we need to hang them
2273          on the first input bfd we found that contained dynamic relocs.  */
2274       switch (r_type)
2275         {
2276         case R_CRIS_16_GOT:
2277         case R_CRIS_32_GOT:
2278         case R_CRIS_32_GOTREL:
2279         case R_CRIS_32_PLT_GOTREL:
2280         case R_CRIS_32_PLT_PCREL:
2281         case R_CRIS_16_GOTPLT:
2282         case R_CRIS_32_GOTPLT:
2283           if (dynobj == NULL)
2284             {
2285               elf_hash_table (info)->dynobj = dynobj = abfd;
2286
2287               /* Create the .got section, so we can assume it's always
2288                  present whenever there's a dynobj.  */
2289               if (!_bfd_elf_create_got_section (dynobj, info))
2290                 return false;
2291             }
2292           break;
2293
2294         default:
2295           break;
2296         }
2297
2298       /* Some relocs require a global offset table (but perhaps not a
2299          specific GOT entry).  */
2300       switch (r_type)
2301         {
2302         case R_CRIS_16_GOT:
2303         case R_CRIS_32_GOT:
2304         case R_CRIS_32_GOTREL:
2305         case R_CRIS_32_PLT_GOTREL:
2306           if (sgot == NULL)
2307             sgot = bfd_get_section_by_name (dynobj, ".got");
2308
2309           if (local_got_refcounts == NULL)
2310             {
2311               bfd_size_type amt;
2312
2313               /* We use index local_got_refcounts[-1] to count all
2314                  GOT-relative relocations that do not have explicit
2315                  GOT entries.  */
2316               amt = symtab_hdr->sh_info + 1;
2317               amt *= sizeof (bfd_signed_vma);
2318               local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
2319               if (local_got_refcounts == NULL)
2320                 return false;
2321
2322               local_got_refcounts++;
2323               elf_local_got_refcounts (abfd) = local_got_refcounts;
2324             }
2325           break;
2326
2327         default:
2328           break;
2329         }
2330
2331       switch (r_type)
2332         {
2333         case R_CRIS_16_GOTPLT:
2334         case R_CRIS_32_GOTPLT:
2335           /* Mark that we need a GOT entry if the PLT entry (and its GOT
2336              entry) is eliminated.  We can only do this for a non-local
2337              symbol.  */
2338           if (h != NULL)
2339             {
2340               ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++;
2341               goto handle_gotplt_reloc;
2342             }
2343           /* If h is NULL then this is a local symbol, and we must make a
2344              GOT entry for it, so handle it like a GOT reloc.  */
2345           /* Fall through.  */
2346
2347         case R_CRIS_16_GOT:
2348         case R_CRIS_32_GOT:
2349           /* This symbol requires a global offset table entry.  */
2350
2351           if (srelgot == NULL
2352               && (h != NULL || info->shared))
2353             {
2354               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2355               if (srelgot == NULL)
2356                 {
2357                   srelgot = bfd_make_section (dynobj, ".rela.got");
2358                   if (srelgot == NULL
2359                       || !bfd_set_section_flags (dynobj, srelgot,
2360                                                  (SEC_ALLOC
2361                                                   | SEC_LOAD
2362                                                   | SEC_HAS_CONTENTS
2363                                                   | SEC_IN_MEMORY
2364                                                   | SEC_LINKER_CREATED
2365                                                   | SEC_READONLY))
2366                       || !bfd_set_section_alignment (dynobj, srelgot, 2))
2367                     return false;
2368                 }
2369             }
2370
2371           if (h != NULL)
2372             {
2373               if (h->got.refcount == 0)
2374                 {
2375                   /* Make sure this symbol is output as a dynamic symbol.  */
2376                   if (h->dynindx == -1)
2377                     {
2378                       if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2379                         return false;
2380                     }
2381
2382                   /* Allocate space in the .got section.  */
2383                   sgot->_raw_size += 4;
2384                   /* Allocate relocation space.  */
2385                   srelgot->_raw_size += sizeof (Elf32_External_Rela);
2386                 }
2387               h->got.refcount++;
2388             }
2389           else
2390             {
2391               /* This is a global offset table entry for a local symbol.  */
2392               if (local_got_refcounts[r_symndx] == 0)
2393                 {
2394                   sgot->_raw_size += 4;
2395                   if (info->shared)
2396                     {
2397                       /* If we are generating a shared object, we need to
2398                          output a R_CRIS_RELATIVE reloc so that the dynamic
2399                          linker can adjust this GOT entry.  */
2400                       srelgot->_raw_size += sizeof (Elf32_External_Rela);
2401                     }
2402                 }
2403               local_got_refcounts[r_symndx]++;
2404             }
2405           break;
2406
2407         case R_CRIS_32_GOTREL:
2408           /* This reference requires a global offset table.
2409              FIXME: The actual refcount isn't used currently; the .got
2410              section can't be removed if there were any references in the
2411              input.  */
2412           local_got_refcounts[-1]++;
2413           break;
2414
2415         handle_gotplt_reloc:
2416
2417         case R_CRIS_32_PLT_GOTREL:
2418           /* This reference requires a global offset table.  */
2419           local_got_refcounts[-1]++;
2420           /* Fall through.  */
2421
2422         case R_CRIS_32_PLT_PCREL:
2423           /* This symbol requires a procedure linkage table entry.  We
2424              actually build the entry in adjust_dynamic_symbol,
2425              because this might be a case of linking PIC code which is
2426              never referenced by a dynamic object, in which case we
2427              don't need to generate a procedure linkage table entry
2428              after all.  */
2429
2430           /* If this is a local symbol, we resolve it directly without
2431              creating a procedure linkage table entry.  */
2432           if (h == NULL)
2433             continue;
2434
2435           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2436           h->plt.refcount++;
2437           break;
2438
2439         case R_CRIS_8:
2440         case R_CRIS_16:
2441         case R_CRIS_32:
2442           /* Let's help debug shared library creation.  Any of these
2443              relocs can be used in shared libs, but pages containing them
2444              cannot be shared.  Don't warn for sections we don't care
2445              about, such as debug sections or non-constant sections.  We
2446              can't help tables of (global) function pointers, for example,
2447              though they must be emitted in a data section to avoid having
2448              impure text sections.  */
2449           if (info->shared
2450               && (sec->flags & SEC_ALLOC) != 0
2451               && (sec->flags & SEC_READONLY) != 0)
2452             {
2453               /* FIXME: How do we make this optionally a warning only?  */
2454               (*_bfd_error_handler)
2455                 (_("%s, section %s:\n  relocation %s should not be used in a shared object; recompile with -fPIC"),
2456                  bfd_archive_filename (abfd),
2457                  sec->name,
2458                  cris_elf_howto_table[r_type].name);
2459             }
2460           /* Fall through.  */
2461
2462         case R_CRIS_8_PCREL:
2463         case R_CRIS_16_PCREL:
2464         case R_CRIS_32_PCREL:
2465           if (h != NULL)
2466             {
2467               h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
2468
2469               /* Make sure a plt entry is created for this symbol if it
2470                  turns out to be a function defined by a dynamic object.  */
2471               h->plt.refcount++;
2472             }
2473
2474           /* If we are creating a shared library and this is not a local
2475              symbol, we need to copy the reloc into the shared library.
2476              However when linking with -Bsymbolic and this is a global
2477              symbol which is defined in an object we are including in the
2478              link (i.e., DEF_REGULAR is set), then we can resolve the
2479              reloc directly.  At this point we have not seen all the input
2480              files, so it is possible that DEF_REGULAR is not set now but
2481              will be set later (it is never cleared).  In case of a weak
2482              definition, DEF_REGULAR may be cleared later by a strong
2483              definition in a shared library.  We account for that
2484              possibility below by storing information in the relocs_copied
2485              field of the hash table entry.  A similar situation occurs
2486              when creating shared libraries and symbol visibility changes
2487              render the symbol local.  */
2488
2489           /* No need to do anything if we're not creating a shared object.  */
2490           if (! info->shared)
2491             break;
2492
2493           /* We don't need to handle relocs into sections not going into
2494              the "real" output.  */
2495           if ((sec->flags & SEC_ALLOC) == 0)
2496             break;
2497
2498           /* We can only eliminate PC-relative relocs.  */
2499           if (r_type == R_CRIS_8_PCREL
2500               || r_type == R_CRIS_16_PCREL
2501               || r_type == R_CRIS_32_PCREL)
2502             {
2503               /* If the symbol is local, then we can eliminate the reloc.  */
2504               if (h == NULL)
2505                 break;
2506
2507               /* If this is with -Bsymbolic and the symbol isn't weak, and
2508                  is defined by an ordinary object (the ones we include in
2509                  this shared library) then we can also eliminate the
2510                  reloc.  See comment above for more eliminable cases which
2511                  we can't identify at this time.  */
2512               if (info->symbolic
2513                   && h->root.type != bfd_link_hash_defweak
2514                   && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2515                 break;
2516             }
2517
2518           /* We create a reloc section in dynobj and make room for this
2519              reloc.  */
2520           if (sreloc == NULL)
2521             {
2522               const char *name;
2523
2524               name = (bfd_elf_string_from_elf_section
2525                       (abfd,
2526                        elf_elfheader (abfd)->e_shstrndx,
2527                        elf_section_data (sec)->rel_hdr.sh_name));
2528               if (name == NULL)
2529                 return false;
2530
2531               BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2532                           && strcmp (bfd_get_section_name (abfd, sec),
2533                                      name + 5) == 0);
2534
2535               sreloc = bfd_get_section_by_name (dynobj, name);
2536               if (sreloc == NULL)
2537                 {
2538                   sreloc = bfd_make_section (dynobj, name);
2539                   if (sreloc == NULL
2540                       || !bfd_set_section_flags (dynobj, sreloc,
2541                                                  (SEC_ALLOC
2542                                                   | SEC_LOAD
2543                                                   | SEC_HAS_CONTENTS
2544                                                   | SEC_IN_MEMORY
2545                                                   | SEC_LINKER_CREATED
2546                                                   | SEC_READONLY))
2547                       || !bfd_set_section_alignment (dynobj, sreloc, 2))
2548                     return false;
2549                 }
2550               if (sec->flags & SEC_READONLY)
2551                 info->flags |= DF_TEXTREL;
2552             }
2553
2554           sreloc->_raw_size += sizeof (Elf32_External_Rela);
2555
2556           /* If we are linking with -Bsymbolic, we count the number of PC
2557              relative relocations we have entered for this symbol, so that
2558              we can discard them again if the symbol is later defined by a
2559              regular object.  We know that h is really a pointer to an
2560              elf_cris_link_hash_entry.  */
2561           if ((r_type == R_CRIS_8_PCREL
2562                || r_type == R_CRIS_16_PCREL
2563                || r_type == R_CRIS_32_PCREL)
2564               && info->symbolic)
2565             {
2566               struct elf_cris_link_hash_entry *eh;
2567               struct elf_cris_pcrel_relocs_copied *p;
2568
2569               eh = (struct elf_cris_link_hash_entry *) h;
2570
2571               for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
2572                 if (p->section == sreloc)
2573                   break;
2574
2575               if (p == NULL)
2576                 {
2577                   p = ((struct elf_cris_pcrel_relocs_copied *)
2578                        bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
2579                   if (p == NULL)
2580                     return false;
2581                   p->next = eh->pcrel_relocs_copied;
2582                   eh->pcrel_relocs_copied = p;
2583                   p->section = sreloc;
2584                   p->count = 0;
2585                 }
2586
2587               ++p->count;
2588             }
2589           break;
2590
2591         /* This relocation describes the C++ object vtable hierarchy.
2592            Reconstruct it for later use during GC.  */
2593         case R_CRIS_GNU_VTINHERIT:
2594           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2595             return false;
2596           break;
2597
2598         /* This relocation describes which C++ vtable entries are actually
2599            used.  Record for later use during GC.  */
2600         case R_CRIS_GNU_VTENTRY:
2601           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2602             return false;
2603           break;
2604
2605         default:
2606           /* Other relocs do not appear here.  */
2607           bfd_set_error (bfd_error_bad_value);
2608           return false;
2609         }
2610     }
2611
2612   return true;
2613 }
2614
2615 /* Set the sizes of the dynamic sections.  */
2616
2617 static boolean
2618 elf_cris_size_dynamic_sections (output_bfd, info)
2619      bfd *output_bfd ATTRIBUTE_UNUSED;
2620      struct bfd_link_info *info;
2621 {
2622   bfd *dynobj;
2623   asection *s;
2624   boolean plt;
2625   boolean relocs;
2626
2627   dynobj = elf_hash_table (info)->dynobj;
2628   BFD_ASSERT (dynobj != NULL);
2629
2630   if (elf_hash_table (info)->dynamic_sections_created)
2631     {
2632       /* Set the contents of the .interp section to the interpreter.  */
2633       if (!info->shared)
2634         {
2635           s = bfd_get_section_by_name (dynobj, ".interp");
2636           BFD_ASSERT (s != NULL);
2637           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2638           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2639         }
2640     }
2641   else
2642     {
2643       /* Adjust all expected GOTPLT uses to use a GOT entry instead.  */
2644       elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2645                                    elf_cris_adjust_gotplt_to_got,
2646                                    (PTR) info);
2647
2648       /* We may have created entries in the .rela.got section.
2649          However, if we are not creating the dynamic sections, we will
2650          not actually use these entries.  Reset the size of .rela.got,
2651          which will cause it to get stripped from the output file
2652          below.  */
2653       s = bfd_get_section_by_name (dynobj, ".rela.got");
2654       if (s != NULL)
2655         s->_raw_size = 0;
2656     }
2657
2658   /* If this is a -Bsymbolic shared link, then we need to discard all PC
2659      relative relocs against symbols defined in a regular object.  We
2660      allocated space for them in the check_relocs routine, but we will not
2661      fill them in in the relocate_section routine.  We also discard space
2662      for relocs that have become for local symbols due to symbol
2663      visibility changes.  For programs, we discard space for relocs for
2664      symbols not referenced by any dynamic object.  */
2665   if (info->shared)
2666     elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2667                                  elf_cris_discard_excess_dso_dynamics,
2668                                  (PTR) info);
2669   else
2670     elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2671                                  elf_cris_discard_excess_program_dynamics,
2672                                  (PTR) info);
2673
2674   /* The check_relocs and adjust_dynamic_symbol entry points have
2675      determined the sizes of the various dynamic sections.  Allocate
2676      memory for them.  */
2677   plt = false;
2678   relocs = false;
2679   for (s = dynobj->sections; s != NULL; s = s->next)
2680     {
2681       const char *name;
2682       boolean strip;
2683
2684       if ((s->flags & SEC_LINKER_CREATED) == 0)
2685         continue;
2686
2687       /* It's OK to base decisions on the section name, because none
2688          of the dynobj section names depend upon the input files.  */
2689       name = bfd_get_section_name (dynobj, s);
2690
2691       strip = false;
2692
2693       if (strcmp (name, ".plt") == 0)
2694         {
2695           if (s->_raw_size == 0)
2696             {
2697               /* Strip this section if we don't need it; see the
2698                  comment below.  */
2699               strip = true;
2700             }
2701           else
2702             {
2703               /* Remember whether there is a PLT.  */
2704               plt = true;
2705             }
2706         }
2707       else if (strncmp (name, ".rela", 5) == 0)
2708         {
2709           if (s->_raw_size == 0)
2710             {
2711               /* If we don't need this section, strip it from the
2712                  output file.  This is mostly to handle .rela.bss and
2713                  .rela.plt.  We must create both sections in
2714                  create_dynamic_sections, because they must be created
2715                  before the linker maps input sections to output
2716                  sections.  The linker does that before
2717                  adjust_dynamic_symbol is called, and it is that
2718                  function which decides whether anything needs to go
2719                  into these sections.  */
2720               strip = true;
2721             }
2722           else
2723             {
2724               /* Remember whether there are any reloc sections other
2725                  than .rela.plt.  */
2726               if (strcmp (name, ".rela.plt") != 0)
2727                   relocs = true;
2728
2729               /* We use the reloc_count field as a counter if we need
2730                  to copy relocs into the output file.  */
2731               s->reloc_count = 0;
2732             }
2733         }
2734       else if (strncmp (name, ".got", 4) != 0)
2735         {
2736           /* It's not one of our sections, so don't allocate space.  */
2737           continue;
2738         }
2739
2740       if (strip)
2741         {
2742           _bfd_strip_section_from_output (info, s);
2743           continue;
2744         }
2745
2746       /* Allocate memory for the section contents. We use bfd_zalloc here
2747          in case unused entries are not reclaimed before the section's
2748          contents are written out.  This should not happen, but this way
2749          if it does, we will not write out garbage.  For reloc sections,
2750          this will make entries have the type R_CRIS_NONE.  */
2751       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2752       if (s->contents == NULL && s->_raw_size != 0)
2753         return false;
2754     }
2755
2756   if (elf_hash_table (info)->dynamic_sections_created)
2757     {
2758       /* Add some entries to the .dynamic section.  We fill in the
2759          values later, in elf_cris_finish_dynamic_sections, but we
2760          must add the entries now so that we get the correct size for
2761          the .dynamic section.  The DT_DEBUG entry is filled in by the
2762          dynamic linker and used by the debugger.  */
2763 #define add_dynamic_entry(TAG, VAL) \
2764   bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2765
2766       if (!info->shared)
2767         {
2768           if (!add_dynamic_entry (DT_DEBUG, 0))
2769             return false;
2770         }
2771
2772       if (plt)
2773         {
2774           if (!add_dynamic_entry (DT_PLTGOT, 0)
2775               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2776               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2777               || !add_dynamic_entry (DT_JMPREL, 0))
2778             return false;
2779         }
2780
2781       if (relocs)
2782         {
2783           if (!add_dynamic_entry (DT_RELA, 0)
2784               || !add_dynamic_entry (DT_RELASZ, 0)
2785               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2786             return false;
2787         }
2788
2789       if ((info->flags & DF_TEXTREL) != 0)
2790         {
2791           if (!add_dynamic_entry (DT_TEXTREL, 0))
2792             return false;
2793           info->flags |= DF_TEXTREL;
2794         }
2795     }
2796 #undef add_dynamic_entry
2797
2798   return true;
2799 }
2800
2801 /* This function is called via elf_cris_link_hash_traverse if we are
2802    creating a shared object.  In the -Bsymbolic case, it discards the
2803    space allocated to copy PC relative relocs against symbols which
2804    are defined in regular objects.  For the normal non-symbolic case,
2805    we also discard space for relocs that have become local due to
2806    symbol visibility changes.  We allocated space for them in the
2807    check_relocs routine, but we won't fill them in in the
2808    relocate_section routine.  */
2809
2810 static boolean
2811 elf_cris_discard_excess_dso_dynamics (h, inf)
2812      struct elf_cris_link_hash_entry *h;
2813      PTR inf;
2814 {
2815   struct elf_cris_pcrel_relocs_copied *s;
2816   struct bfd_link_info *info = (struct bfd_link_info *) inf;
2817
2818   /* If a symbol has been forced local or we have found a regular
2819      definition for the symbolic link case, then we won't be needing
2820      any relocs.  */
2821   if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2822       && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2823           || info->symbolic))
2824     {
2825       for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
2826         s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2827     }
2828
2829   return true;
2830 }
2831
2832 /* This function is called via elf_cris_link_hash_traverse if we are *not*
2833    creating a shared object.  We discard space for relocs for symbols put
2834    in the .got, but which we found we do not have to resolve at run-time.  */
2835
2836 static boolean
2837 elf_cris_discard_excess_program_dynamics (h, inf)
2838      struct elf_cris_link_hash_entry *h;
2839      PTR inf;
2840 {
2841   struct bfd_link_info *info = (struct bfd_link_info *) inf;
2842
2843   /* If we're not creating a shared library and have a symbol which is
2844      referred to by .got references, but the symbol is defined locally,
2845      (or rather, not referred to by a DSO and not defined by a DSO) then
2846      lose the reloc for the .got (don't allocate room for it).  */
2847   if ((h->root.elf_link_hash_flags
2848        & (ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_HASH_DEF_DYNAMIC)) == 0)
2849     {
2850       if (h->root.got.refcount > 0
2851           /* The size of this section is only valid and in sync with the
2852              various reference counts if we do dynamic; don't decrement it
2853              otherwise.  */
2854           && elf_hash_table (info)->dynamic_sections_created)
2855         {
2856           bfd *dynobj = elf_hash_table (info)->dynobj;
2857           asection *srelgot;
2858
2859           BFD_ASSERT (dynobj != NULL);
2860
2861           srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2862
2863           BFD_ASSERT (srelgot != NULL);
2864
2865           srelgot->_raw_size -= sizeof (Elf32_External_Rela);
2866         }
2867
2868       /* If the locally-defined symbol isn't used by a DSO, then we don't
2869          have to export it as a dynamic symbol.  This was already done for
2870          functions; doing this for all symbols would presumably not
2871          introduce new problems.  */
2872       h->root.dynindx = -1;
2873     }
2874
2875   return true;
2876 }
2877
2878 /* Reject a file depending on presence and expectation of prefixed
2879    underscores on symbols.  */
2880
2881 static boolean
2882 cris_elf_object_p (abfd)
2883      bfd *abfd;
2884 {
2885   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
2886     return (bfd_get_symbol_leading_char (abfd) == '_');
2887   else
2888     return (bfd_get_symbol_leading_char (abfd) == 0);
2889 }
2890
2891 /* Mark presence or absence of leading underscore.  */
2892
2893 static void
2894 cris_elf_final_write_processing (abfd, linker)
2895      bfd *abfd;
2896      boolean linker ATTRIBUTE_UNUSED;
2897 {
2898   if (bfd_get_symbol_leading_char (abfd) == '_')
2899     elf_elfheader (abfd)->e_flags |= EF_CRIS_UNDERSCORE;
2900   else
2901     elf_elfheader (abfd)->e_flags &= ~EF_CRIS_UNDERSCORE;
2902 }
2903
2904 /* Display the flags field.  */
2905
2906 static boolean
2907 cris_elf_print_private_bfd_data (abfd, ptr)
2908      bfd *abfd;
2909      PTR ptr;
2910 {
2911   FILE *file = (FILE *) ptr;
2912
2913   BFD_ASSERT (abfd != NULL && ptr != NULL)
2914
2915   _bfd_elf_print_private_bfd_data (abfd, ptr);
2916
2917   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2918
2919   if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
2920     fprintf (file, _(" [symbols have a _ prefix]"));
2921
2922   fputc ('\n', file);
2923   return true;
2924 }
2925
2926 /* Don't mix files with and without a leading underscore.  */
2927
2928 static boolean
2929 cris_elf_merge_private_bfd_data (ibfd, obfd)
2930      bfd *ibfd;
2931      bfd *obfd;
2932 {
2933   flagword old_flags, new_flags;
2934
2935   if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
2936     return false;
2937
2938   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2939       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2940     return true;
2941
2942   if (! elf_flags_init (obfd))
2943     {
2944       /* This happens when ld starts out with a 'blank' output file.  */
2945       elf_flags_init (obfd) = true;
2946
2947       /* Set flags according to current bfd_target.  */
2948       cris_elf_final_write_processing (obfd, false);
2949     }
2950
2951   old_flags = elf_elfheader (obfd)->e_flags;
2952   new_flags = elf_elfheader (ibfd)->e_flags;
2953
2954   /* Is this good or bad?  We'll follow with other excluding flags.  */
2955   if ((old_flags & EF_CRIS_UNDERSCORE) != (new_flags & EF_CRIS_UNDERSCORE))
2956     {
2957       (*_bfd_error_handler)
2958         ((new_flags & EF_CRIS_UNDERSCORE)
2959          ? _("%s: uses _-prefixed symbols, but writing file with non-prefixed symbols")
2960          : _("%s: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
2961          bfd_archive_filename (ibfd));
2962       bfd_set_error (bfd_error_bad_value);
2963       return false;
2964     }
2965
2966   return true;
2967 }
2968
2969
2970 static enum elf_reloc_type_class
2971 elf_cris_reloc_type_class (rela)
2972      const Elf_Internal_Rela *rela;
2973 {
2974   switch ((int) ELF32_R_TYPE (rela->r_info))
2975     {
2976     case R_CRIS_RELATIVE:
2977       return reloc_class_relative;
2978     case R_CRIS_JUMP_SLOT:
2979       return reloc_class_plt;
2980     case R_CRIS_COPY:
2981       return reloc_class_copy;
2982     default:
2983       return reloc_class_normal;
2984     }
2985 }
2986 \f
2987 #define ELF_ARCH                bfd_arch_cris
2988 #define ELF_MACHINE_CODE        EM_CRIS
2989 #define ELF_MAXPAGESIZE         0x2000
2990
2991 #define TARGET_LITTLE_SYM       bfd_elf32_cris_vec
2992 #define TARGET_LITTLE_NAME      "elf32-cris"
2993 #define elf_symbol_leading_char 0
2994
2995 #define elf_info_to_howto_rel                   NULL
2996 #define elf_info_to_howto                       cris_info_to_howto_rela
2997 #define elf_backend_relocate_section            cris_elf_relocate_section
2998 #define elf_backend_gc_mark_hook                cris_elf_gc_mark_hook
2999 #define elf_backend_gc_sweep_hook               cris_elf_gc_sweep_hook
3000 #define elf_backend_check_relocs                cris_elf_check_relocs
3001
3002 #define elf_backend_can_gc_sections             1
3003 #define elf_backend_can_refcount                1
3004
3005 #define elf_backend_object_p                    cris_elf_object_p
3006 #define elf_backend_final_write_processing \
3007         cris_elf_final_write_processing
3008 #define bfd_elf32_bfd_print_private_bfd_data \
3009         cris_elf_print_private_bfd_data
3010 #define bfd_elf32_bfd_merge_private_bfd_data \
3011         cris_elf_merge_private_bfd_data
3012
3013 #define bfd_elf32_bfd_reloc_type_lookup         cris_reloc_type_lookup
3014
3015 #define bfd_elf32_bfd_link_hash_table_create \
3016         elf_cris_link_hash_table_create
3017 #define elf_backend_adjust_dynamic_symbol \
3018         elf_cris_adjust_dynamic_symbol
3019 #define elf_backend_size_dynamic_sections \
3020         elf_cris_size_dynamic_sections
3021 #define elf_backend_finish_dynamic_symbol \
3022         elf_cris_finish_dynamic_symbol
3023 #define elf_backend_finish_dynamic_sections \
3024         elf_cris_finish_dynamic_sections
3025 #define elf_backend_create_dynamic_sections \
3026         _bfd_elf_create_dynamic_sections
3027 #define bfd_elf32_bfd_final_link \
3028         _bfd_elf32_gc_common_final_link
3029 #define elf_backend_hide_symbol                 elf_cris_hide_symbol
3030 #define elf_backend_reloc_type_class            elf_cris_reloc_type_class
3031
3032 #define elf_backend_want_got_plt        1
3033 #define elf_backend_plt_readonly        1
3034 #define elf_backend_want_plt_sym        0
3035 #define elf_backend_got_header_size     12
3036 #define elf_backend_plt_header_size     PLT_ENTRY_SIZE
3037
3038 /* Later, we my want to optimize RELA entries into REL entries for dynamic
3039    linking and libraries (if it's a win of any significance).  Until then,
3040    take the easy route.  */
3041 #define elf_backend_may_use_rel_p 0
3042 #define elf_backend_may_use_rela_p 1
3043
3044 #include "elf32-target.h"
3045
3046 #define INCLUDED_TARGET_FILE
3047
3048 #undef TARGET_LITTLE_SYM
3049 #undef TARGET_LITTLE_NAME
3050 #undef elf_symbol_leading_char
3051
3052 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
3053 #define TARGET_LITTLE_NAME "elf32-us-cris"
3054 #define elf_symbol_leading_char '_'
3055
3056 #include "elf32-target.h"