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