* elflink.c (is_reloc_section): New function. Returns true if the
[external/binutils.git] / bfd / elf32-cris.c
1 /* CRIS-specific support for 32-bit ELF.
2    Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3    Free Software Foundation, Inc.
4    Contributed by Axis Communications AB.
5    Written by Hans-Peter Nilsson, based on elf32-fr30.c
6    PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c.
7
8    This file is part of BFD, the Binary File Descriptor library.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23    MA 02110-1301, USA.  */
24
25 #include "sysdep.h"
26 #include "bfd.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "elf/cris.h"
30 #include <limits.h>
31
32 /* Forward declarations.  */
33 static reloc_howto_type * cris_reloc_type_lookup
34   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
35
36 static void cris_info_to_howto_rela
37   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
38
39 static bfd_reloc_status_type cris_elf_pcrel_reloc
40   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
41
42 static bfd_boolean cris_elf_grok_prstatus
43   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
44
45 static bfd_boolean cris_elf_grok_psinfo
46   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
47
48 static bfd_boolean cris_elf_relocate_section
49   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
50            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
51
52 static bfd_reloc_status_type cris_final_link_relocate
53   PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
54            Elf_Internal_Rela *, bfd_vma));
55
56 static bfd_boolean cris_elf_object_p PARAMS ((bfd *));
57
58 static void cris_elf_final_write_processing PARAMS ((bfd *, bfd_boolean));
59
60 static bfd_boolean cris_elf_set_mach_from_flags
61   PARAMS ((bfd *, unsigned long int));
62
63 static bfd_boolean cris_elf_print_private_bfd_data PARAMS ((bfd *, PTR));
64
65 static bfd_boolean cris_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
66 static bfd_boolean cris_elf_copy_private_bfd_data PARAMS ((bfd *, bfd *));
67
68 struct elf_cris_link_hash_entry;
69 static bfd_boolean elf_cris_discard_excess_dso_dynamics
70   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
71 static bfd_boolean elf_cris_discard_excess_program_dynamics
72   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
73 static bfd_boolean elf_cris_adjust_gotplt_to_got
74   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
75 static bfd_boolean elf_cris_try_fold_plt_to_got
76   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
77 static struct bfd_hash_entry *elf_cris_link_hash_newfunc
78   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
79 static struct bfd_link_hash_table *elf_cris_link_hash_table_create
80   PARAMS ((bfd *));
81 static bfd_boolean elf_cris_adjust_dynamic_symbol
82   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
83 static bfd_boolean cris_elf_check_relocs
84   PARAMS ((bfd *, struct bfd_link_info *, asection *,
85            const Elf_Internal_Rela *));
86
87 static bfd_boolean elf_cris_size_dynamic_sections
88   PARAMS ((bfd *, struct bfd_link_info *));
89 static bfd_boolean elf_cris_finish_dynamic_symbol
90   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
91            Elf_Internal_Sym *));
92 static bfd_boolean elf_cris_finish_dynamic_sections
93   PARAMS ((bfd *, struct bfd_link_info *));
94 static void elf_cris_hide_symbol
95   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
96 static enum elf_reloc_type_class elf_cris_reloc_type_class
97   PARAMS ((const Elf_Internal_Rela *));
98
99 static reloc_howto_type cris_elf_howto_table [] =
100 {
101   /* This reloc does nothing.  */
102   HOWTO (R_CRIS_NONE,           /* type */
103          0,                     /* rightshift */
104          2,                     /* size (0 = byte, 1 = short, 2 = long) */
105          32,                    /* bitsize */
106          FALSE,                 /* pc_relative */
107          0,                     /* bitpos */
108          complain_overflow_bitfield, /* complain_on_overflow */
109          bfd_elf_generic_reloc, /* special_function */
110          "R_CRIS_NONE",         /* name */
111          FALSE,                 /* partial_inplace */
112          0,                     /* src_mask */
113          0,                     /* dst_mask */
114          FALSE),                /* pcrel_offset */
115
116   /* An 8 bit absolute relocation.  */
117   HOWTO (R_CRIS_8,              /* type */
118          0,                     /* rightshift */
119          0,                     /* size (0 = byte, 1 = short, 2 = long) */
120          8,                     /* bitsize */
121          FALSE,                 /* pc_relative */
122          0,                     /* bitpos */
123          complain_overflow_bitfield, /* complain_on_overflow */
124          bfd_elf_generic_reloc, /* special_function */
125          "R_CRIS_8",            /* name */
126          FALSE,                 /* partial_inplace */
127          0x0000,                /* src_mask */
128          0x00ff,                /* dst_mask */
129          FALSE),                /* pcrel_offset */
130
131   /* A 16 bit absolute relocation.  */
132   HOWTO (R_CRIS_16,             /* type */
133          0,                     /* rightshift */
134          1,                     /* size (0 = byte, 1 = short, 2 = long) */
135          16,                    /* bitsize */
136          FALSE,                 /* pc_relative */
137          0,                     /* bitpos */
138          complain_overflow_bitfield, /* complain_on_overflow */
139          bfd_elf_generic_reloc, /* special_function */
140          "R_CRIS_16",           /* name */
141          FALSE,                 /* partial_inplace */
142          0x00000000,            /* src_mask */
143          0x0000ffff,            /* dst_mask */
144          FALSE),                /* pcrel_offset */
145
146   /* A 32 bit absolute relocation.  */
147   HOWTO (R_CRIS_32,             /* type */
148          0,                     /* rightshift */
149          2,                     /* size (0 = byte, 1 = short, 2 = long) */
150          32,                    /* bitsize */
151          FALSE,                 /* pc_relative */
152          0,                     /* bitpos */
153          /* We don't want overflow complaints for 64-bit vma builds
154             for e.g. sym+0x40000000 (or actually sym-0xc0000000 in
155             32-bit ELF) where sym=0xc0001234.
156             Don't do this for the PIC relocs, as we don't expect to
157             see them with large offsets.  */
158          complain_overflow_dont, /* complain_on_overflow */
159          bfd_elf_generic_reloc, /* special_function */
160          "R_CRIS_32",           /* name */
161          FALSE,                 /* partial_inplace */
162          0x00000000,            /* src_mask */
163          0xffffffff,            /* dst_mask */
164          FALSE),                /* pcrel_offset */
165
166   /* An 8 bit PC-relative relocation.  */
167   HOWTO (R_CRIS_8_PCREL,        /* type */
168          0,                     /* rightshift */
169          0,                     /* size (0 = byte, 1 = short, 2 = long) */
170          8,                     /* bitsize */
171          TRUE,                  /* pc_relative */
172          0,                     /* bitpos */
173          complain_overflow_bitfield, /* complain_on_overflow */
174          cris_elf_pcrel_reloc,  /* special_function */
175          "R_CRIS_8_PCREL",      /* name */
176          FALSE,                 /* partial_inplace */
177          0x0000,                /* src_mask */
178          0x00ff,                /* dst_mask */
179          TRUE),                 /* pcrel_offset */
180
181   /* A 16 bit PC-relative relocation.  */
182   HOWTO (R_CRIS_16_PCREL,       /* type */
183          0,                     /* rightshift */
184          1,                     /* size (0 = byte, 1 = short, 2 = long) */
185          16,                    /* bitsize */
186          TRUE,                  /* pc_relative */
187          0,                     /* bitpos */
188          complain_overflow_bitfield, /* complain_on_overflow */
189          cris_elf_pcrel_reloc,  /* special_function */
190          "R_CRIS_16_PCREL",     /* name */
191          FALSE,                 /* partial_inplace */
192          0x00000000,            /* src_mask */
193          0x0000ffff,            /* dst_mask */
194          TRUE),                 /* pcrel_offset */
195
196   /* A 32 bit PC-relative relocation.  */
197   HOWTO (R_CRIS_32_PCREL,       /* type */
198          0,                     /* rightshift */
199          2,                     /* size (0 = byte, 1 = short, 2 = long) */
200          32,                    /* bitsize */
201          TRUE,                  /* pc_relative */
202          0,                     /* bitpos */
203          complain_overflow_bitfield, /* complain_on_overflow */
204          cris_elf_pcrel_reloc,  /* special_function */
205          "R_CRIS_32_PCREL",     /* name */
206          FALSE,                 /* partial_inplace */
207          0x00000000,            /* src_mask */
208          0xffffffff,            /* dst_mask */
209          TRUE),                 /* pcrel_offset */
210
211   /* GNU extension to record C++ vtable hierarchy.  */
212   HOWTO (R_CRIS_GNU_VTINHERIT,  /* type */
213          0,                     /* rightshift */
214          2,                     /* size (0 = byte, 1 = short, 2 = long) */
215          0,                     /* bitsize */
216          FALSE,                 /* pc_relative */
217          0,                     /* bitpos */
218          complain_overflow_dont, /* complain_on_overflow */
219          NULL,                  /* special_function */
220          "R_CRIS_GNU_VTINHERIT", /* name */
221          FALSE,                 /* partial_inplace */
222          0,                     /* src_mask */
223          0,                     /* dst_mask */
224          FALSE),                /* pcrel_offset */
225
226   /* GNU extension to record C++ vtable member usage.  */
227   HOWTO (R_CRIS_GNU_VTENTRY,    /* type */
228          0,                     /* rightshift */
229          2,                     /* size (0 = byte, 1 = short, 2 = long) */
230          0,                     /* bitsize */
231          FALSE,                 /* pc_relative */
232          0,                     /* bitpos */
233          complain_overflow_dont, /* complain_on_overflow */
234          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
235          "R_CRIS_GNU_VTENTRY",   /* name */
236          FALSE,                 /* partial_inplace */
237          0,                     /* src_mask */
238          0,                     /* dst_mask */
239          FALSE),                /* pcrel_offset */
240
241   /* This is used only by the dynamic linker.  The symbol should exist
242      both in the object being run and in some shared library.  The
243      dynamic linker copies the data addressed by the symbol from the
244      shared library into the object, because the object being
245      run has to have the data at some particular address.  */
246   HOWTO (R_CRIS_COPY,           /* type */
247          0,                     /* rightshift */
248          2,                     /* size (0 = byte, 1 = short, 2 = long) */
249          32,                    /* bitsize */
250          FALSE,                 /* pc_relative */
251          0,                     /* bitpos */
252          complain_overflow_bitfield, /* complain_on_overflow */
253          bfd_elf_generic_reloc, /* special_function */
254          "R_CRIS_COPY",         /* name */
255          FALSE,                 /* partial_inplace */
256          0,                     /* src_mask */
257          0,                     /* dst_mask */
258          FALSE),                /* pcrel_offset */
259
260   /* Like R_CRIS_32, but used when setting global offset table entries.  */
261   HOWTO (R_CRIS_GLOB_DAT,       /* type */
262          0,                     /* rightshift */
263          2,                     /* size (0 = byte, 1 = short, 2 = long) */
264          32,                    /* bitsize */
265          FALSE,                 /* pc_relative */
266          0,                     /* bitpos */
267          complain_overflow_bitfield, /* complain_on_overflow */
268          bfd_elf_generic_reloc, /* special_function */
269          "R_CRIS_GLOB_DAT",     /* name */
270          FALSE,                 /* partial_inplace */
271          0,                     /* src_mask */
272          0xffffffff,            /* dst_mask */
273          FALSE),                /* pcrel_offset */
274
275   /* Marks a procedure linkage table entry for a symbol.  */
276   HOWTO (R_CRIS_JUMP_SLOT,      /* type */
277          0,                     /* rightshift */
278          2,                     /* size (0 = byte, 1 = short, 2 = long) */
279          32,                    /* bitsize */
280          FALSE,                 /* pc_relative */
281          0,                     /* bitpos */
282          complain_overflow_bitfield, /* complain_on_overflow */
283          bfd_elf_generic_reloc, /* special_function */
284          "R_CRIS_JUMP_SLOT",    /* name */
285          FALSE,                 /* partial_inplace */
286          0,                     /* src_mask */
287          0,                     /* dst_mask */
288          FALSE),                /* pcrel_offset */
289
290   /* Used only by the dynamic linker.  When the object is run, this
291      longword is set to the load address of the object, plus the
292      addend.  */
293   HOWTO (R_CRIS_RELATIVE,       /* type */
294          0,                     /* rightshift */
295          2,                     /* size (0 = byte, 1 = short, 2 = long) */
296          32,                    /* bitsize */
297          FALSE,                 /* pc_relative */
298          0,                     /* bitpos */
299          complain_overflow_bitfield, /* complain_on_overflow */
300          bfd_elf_generic_reloc, /* special_function */
301          "R_CRIS_RELATIVE",     /* name */
302          FALSE,                 /* partial_inplace */
303          0,                     /* src_mask */
304          0xffffffff,            /* dst_mask */
305          FALSE),                /* pcrel_offset */
306
307   /* Like R_CRIS_32, but referring to the GOT table entry for the symbol.  */
308   HOWTO (R_CRIS_16_GOT,         /* type */
309          0,                     /* rightshift */
310          1,                     /* size (0 = byte, 1 = short, 2 = long) */
311          16,                    /* bitsize */
312          FALSE,                 /* pc_relative */
313          0,                     /* bitpos */
314          complain_overflow_bitfield, /* complain_on_overflow */
315          bfd_elf_generic_reloc, /* special_function */
316          "R_CRIS_16_GOT",       /* name */
317          FALSE,                 /* partial_inplace */
318          0,                     /* src_mask */
319          0xffff,                /* dst_mask */
320          FALSE),                /* pcrel_offset */
321
322   HOWTO (R_CRIS_32_GOT,         /* type */
323          0,                     /* rightshift */
324          2,                     /* size (0 = byte, 1 = short, 2 = long) */
325          32,                    /* bitsize */
326          FALSE,                 /* pc_relative */
327          0,                     /* bitpos */
328          complain_overflow_bitfield, /* complain_on_overflow */
329          bfd_elf_generic_reloc, /* special_function */
330          "R_CRIS_32_GOT",       /* name */
331          FALSE,                 /* partial_inplace */
332          0,                     /* src_mask */
333          0xffffffff,            /* dst_mask */
334          FALSE),                /* pcrel_offset */
335
336   /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
337      the GOT table for the symbol.  */
338   HOWTO (R_CRIS_16_GOTPLT,      /* type */
339          0,                     /* rightshift */
340          1,                     /* size (0 = byte, 1 = short, 2 = long) */
341          16,                    /* bitsize */
342          FALSE,                 /* pc_relative */
343          0,                     /* bitpos */
344          complain_overflow_bitfield, /* complain_on_overflow */
345          bfd_elf_generic_reloc, /* special_function */
346          "R_CRIS_16_GOTPLT",    /* name */
347          FALSE,                 /* partial_inplace */
348          0,                     /* src_mask */
349          0xffff,                /* dst_mask */
350          FALSE),                /* pcrel_offset */
351
352   HOWTO (R_CRIS_32_GOTPLT,      /* type */
353          0,                     /* rightshift */
354          2,                     /* size (0 = byte, 1 = short, 2 = long) */
355          32,                    /* bitsize */
356          FALSE,                 /* pc_relative */
357          0,                     /* bitpos */
358          complain_overflow_bitfield, /* complain_on_overflow */
359          bfd_elf_generic_reloc, /* special_function */
360          "R_CRIS_32_GOTPLT",    /* name */
361          FALSE,                 /* partial_inplace */
362          0,                     /* src_mask */
363          0xffffffff,            /* dst_mask */
364          FALSE),                /* pcrel_offset */
365
366   /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
367      be necessary.  */
368   HOWTO (R_CRIS_32_GOTREL,      /* type */
369          0,                     /* rightshift */
370          2,                     /* size (0 = byte, 1 = short, 2 = long) */
371          32,                    /* bitsize */
372          FALSE,                 /* pc_relative */
373          0,                     /* bitpos */
374          complain_overflow_bitfield, /* complain_on_overflow */
375          bfd_elf_generic_reloc, /* special_function */
376          "R_CRIS_32_GOTREL",    /* name */
377          FALSE,                 /* partial_inplace */
378          0,                     /* src_mask */
379          0xffffffff,            /* dst_mask */
380          FALSE),                /* pcrel_offset */
381
382   /* A 32-bit offset from GOT to entry for this symbol in PLT and request
383      to create PLT entry for symbol.  */
384   HOWTO (R_CRIS_32_PLT_GOTREL,  /* type */
385          0,                     /* rightshift */
386          2,                     /* size (0 = byte, 1 = short, 2 = long) */
387          32,                    /* bitsize */
388          FALSE,                 /* pc_relative */
389          0,                     /* bitpos */
390          complain_overflow_bitfield, /* complain_on_overflow */
391          bfd_elf_generic_reloc, /* special_function */
392          "R_CRIS_32_PLT_GOTREL", /* name */
393          FALSE,                 /* partial_inplace */
394          0,                     /* src_mask */
395          0xffffffff,            /* dst_mask */
396          FALSE),                /* pcrel_offset */
397
398   /* A 32-bit offset from PC (location after the relocation) + addend to
399      entry for this symbol in PLT and request to create PLT entry for
400      symbol.  */
401   HOWTO (R_CRIS_32_PLT_PCREL,   /* type */
402          0,                     /* rightshift */
403          2,                     /* size (0 = byte, 1 = short, 2 = long) */
404          32,                    /* bitsize */
405          TRUE,                  /* pc_relative */
406          0,                     /* bitpos */
407          complain_overflow_bitfield, /* complain_on_overflow */
408          cris_elf_pcrel_reloc,  /* special_function */
409          "R_CRIS_32_PLT_PCREL", /* name */
410          FALSE,                 /* partial_inplace */
411          0,                     /* src_mask */
412          0xffffffff,            /* dst_mask */
413          TRUE),                 /* pcrel_offset */
414
415   /* We don't handle these in any special manner and cross-format
416      linking is not supported; just recognize them enough to pass them
417      around.  FIXME: do the same for most PIC relocs and add sanity
418      tests to actually refuse gracefully to handle these and PIC
419      relocs for cross-format linking.  */
420 #define TLSHOWTO32(name) \
421  HOWTO (name, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, \
422         bfd_elf_generic_reloc, #name, FALSE, 0, 0xffffffff, FALSE)
423 #define TLSHOWTO16X(name, X)         \
424  HOWTO (name, 0, 1, 16, FALSE, 0, complain_overflow_ ## X, \
425         bfd_elf_generic_reloc, #name, FALSE, 0, 0xffff, FALSE)
426 #define TLSHOWTO16(name) TLSHOWTO16X(name, unsigned)
427 #define TLSHOWTO16S(name) TLSHOWTO16X(name, signed)
428
429   TLSHOWTO32 (R_CRIS_32_GOT_GD),
430   TLSHOWTO16 (R_CRIS_16_GOT_GD),
431   TLSHOWTO32 (R_CRIS_32_GD),
432   TLSHOWTO32 (R_CRIS_DTP),
433   TLSHOWTO32 (R_CRIS_32_DTPREL),
434   TLSHOWTO16S (R_CRIS_16_DTPREL),
435   TLSHOWTO32 (R_CRIS_32_GOT_TPREL),
436   TLSHOWTO16S (R_CRIS_16_GOT_TPREL),
437   TLSHOWTO32 (R_CRIS_32_TPREL),
438   TLSHOWTO16S (R_CRIS_16_TPREL),
439   TLSHOWTO32 (R_CRIS_DTPMOD)
440 };
441 \f
442 /* Map BFD reloc types to CRIS ELF reloc types.  */
443
444 struct cris_reloc_map
445 {
446   bfd_reloc_code_real_type bfd_reloc_val;
447   unsigned int cris_reloc_val;
448 };
449
450 static const struct cris_reloc_map cris_reloc_map [] =
451 {
452   { BFD_RELOC_NONE,             R_CRIS_NONE },
453   { BFD_RELOC_8,                R_CRIS_8 },
454   { BFD_RELOC_16,               R_CRIS_16 },
455   { BFD_RELOC_32,               R_CRIS_32 },
456   { BFD_RELOC_8_PCREL,          R_CRIS_8_PCREL },
457   { BFD_RELOC_16_PCREL,         R_CRIS_16_PCREL },
458   { BFD_RELOC_32_PCREL,         R_CRIS_32_PCREL },
459   { BFD_RELOC_VTABLE_INHERIT,   R_CRIS_GNU_VTINHERIT },
460   { BFD_RELOC_VTABLE_ENTRY,     R_CRIS_GNU_VTENTRY },
461   { BFD_RELOC_CRIS_COPY,        R_CRIS_COPY },
462   { BFD_RELOC_CRIS_GLOB_DAT,    R_CRIS_GLOB_DAT },
463   { BFD_RELOC_CRIS_JUMP_SLOT,   R_CRIS_JUMP_SLOT },
464   { BFD_RELOC_CRIS_RELATIVE,    R_CRIS_RELATIVE },
465   { BFD_RELOC_CRIS_16_GOT,      R_CRIS_16_GOT },
466   { BFD_RELOC_CRIS_32_GOT,      R_CRIS_32_GOT },
467   { BFD_RELOC_CRIS_16_GOTPLT,   R_CRIS_16_GOTPLT },
468   { BFD_RELOC_CRIS_32_GOTPLT,   R_CRIS_32_GOTPLT },
469   { BFD_RELOC_CRIS_32_GOTREL,   R_CRIS_32_GOTREL },
470   { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
471   { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL },
472   { BFD_RELOC_CRIS_32_GOT_GD,   R_CRIS_32_GOT_GD },
473   { BFD_RELOC_CRIS_16_GOT_GD,   R_CRIS_16_GOT_GD },
474   { BFD_RELOC_CRIS_32_GD,       R_CRIS_32_GD },
475   { BFD_RELOC_CRIS_DTP, R_CRIS_DTP },
476   { BFD_RELOC_CRIS_32_DTPREL,   R_CRIS_32_DTPREL },
477   { BFD_RELOC_CRIS_16_DTPREL,   R_CRIS_16_DTPREL },
478   { BFD_RELOC_CRIS_32_GOT_TPREL, R_CRIS_32_GOT_TPREL },
479   { BFD_RELOC_CRIS_16_GOT_TPREL, R_CRIS_16_GOT_TPREL },
480   { BFD_RELOC_CRIS_32_TPREL,    R_CRIS_32_TPREL },
481   { BFD_RELOC_CRIS_16_TPREL,    R_CRIS_16_TPREL },
482   { BFD_RELOC_CRIS_DTPMOD,      R_CRIS_DTPMOD }
483 };
484
485 static reloc_howto_type *
486 cris_reloc_type_lookup (abfd, code)
487      bfd * abfd ATTRIBUTE_UNUSED;
488      bfd_reloc_code_real_type code;
489 {
490   unsigned int i;
491
492   for (i = 0; i < sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]); i++)
493     if (cris_reloc_map [i].bfd_reloc_val == code)
494       return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
495
496   return NULL;
497 }
498
499 static reloc_howto_type *
500 cris_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
501 {
502   unsigned int i;
503
504   for (i = 0;
505        i < sizeof (cris_elf_howto_table) / sizeof (cris_elf_howto_table[0]);
506        i++)
507     if (cris_elf_howto_table[i].name != NULL
508         && strcasecmp (cris_elf_howto_table[i].name, r_name) == 0)
509       return &cris_elf_howto_table[i];
510
511   return NULL;
512 }
513
514 /* Set the howto pointer for an CRIS ELF reloc.  */
515
516 static void
517 cris_info_to_howto_rela (abfd, cache_ptr, dst)
518      bfd * abfd ATTRIBUTE_UNUSED;
519      arelent * cache_ptr;
520      Elf_Internal_Rela * dst;
521 {
522   enum elf_cris_reloc_type r_type;
523
524   r_type = ELF32_R_TYPE (dst->r_info);
525   BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
526   cache_ptr->howto = & cris_elf_howto_table [r_type];
527 }
528
529 bfd_reloc_status_type
530 cris_elf_pcrel_reloc (abfd, reloc_entry, symbol, data, input_section,
531                       output_bfd, error_message)
532      bfd *abfd ATTRIBUTE_UNUSED;
533      arelent *reloc_entry;
534      asymbol *symbol;
535      PTR data ATTRIBUTE_UNUSED;
536      asection *input_section;
537      bfd *output_bfd;
538      char **error_message ATTRIBUTE_UNUSED;
539 {
540   /* By default (using only bfd_elf_generic_reloc when linking to
541      non-ELF formats) PC-relative relocs are relative to the beginning
542      of the reloc.  CRIS PC-relative relocs are relative to the position
543      *after* the reloc because that's what pre-CRISv32 PC points to
544      after reading an insn field with that reloc.  (For CRISv32, PC is
545      actually relative to the start of the insn, but we keep the old
546      definition.)  Still, we use as much generic machinery as we can.
547
548      Only adjust when doing a final link.  */
549   if (output_bfd == (bfd *) NULL)
550     reloc_entry->addend -= 1 << reloc_entry->howto->size;
551
552   return
553     bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
554                            input_section, output_bfd, error_message);
555 }
556 \f
557 /* Support for core dump NOTE sections.
558    The slightly unintuitive code layout is an attempt to keep at least
559    some similarities with other ports, hoping to simplify general
560    changes, while still keeping Linux/CRIS and Linux/CRISv32 code apart.  */
561
562 static bfd_boolean
563 cris_elf_grok_prstatus (abfd, note)
564      bfd *abfd;
565      Elf_Internal_Note *note;
566 {
567   int offset;
568   size_t size;
569
570   if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
571     switch (note->descsz)
572       {
573       default:
574         return FALSE;
575
576       case 202:         /* Linux/CRISv32 */
577         /* pr_cursig */
578         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
579
580         /* pr_pid */
581         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 22);
582
583         /* pr_reg */
584         offset = 70;
585         size = 128;
586
587         break;
588       }
589   else
590     switch (note->descsz)
591       {
592       default:
593         return FALSE;
594
595       case 214:         /* Linux/CRIS */
596         /* pr_cursig */
597         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
598
599         /* pr_pid */
600         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 22);
601
602         /* pr_reg */
603         offset = 70;
604         size = 140;
605
606         break;
607       }
608
609   /* Make a ".reg/999" section.  */
610   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
611                                           size, note->descpos + offset);
612 }
613
614 static bfd_boolean
615 cris_elf_grok_psinfo (abfd, note)
616      bfd *abfd;
617      Elf_Internal_Note *note;
618 {
619   if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
620     switch (note->descsz)
621       {
622       default:
623         return FALSE;
624
625       case 124:         /* Linux/CRISv32 elf_prpsinfo */
626         elf_tdata (abfd)->core_program
627           = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
628         elf_tdata (abfd)->core_command
629           = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
630       }
631   else
632     switch (note->descsz)
633       {
634       default:
635         return FALSE;
636
637       case 124:         /* Linux/CRIS elf_prpsinfo */
638         elf_tdata (abfd)->core_program
639           = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
640         elf_tdata (abfd)->core_command
641           = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
642       }
643
644   /* Note that for some reason, a spurious space is tacked
645      onto the end of the args in some (at least one anyway)
646      implementations, so strip it off if it exists.  */
647
648   {
649     char *command = elf_tdata (abfd)->core_command;
650     int n = strlen (command);
651
652     if (0 < n && command[n - 1] == ' ')
653       command[n - 1] = '\0';
654   }
655
656   return TRUE;
657 }
658 \f
659 /* The name of the dynamic interpreter.  This is put in the .interp
660    section.  */
661
662 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
663
664 /* The size in bytes of an entry in the procedure linkage table.  */
665
666 #define PLT_ENTRY_SIZE 20
667 #define PLT_ENTRY_SIZE_V32 26
668
669 /* The first entry in an absolute procedure linkage table looks like this.  */
670
671 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
672 {
673   0xfc, 0xe1,
674   0x7e, 0x7e,   /* push mof.  */
675   0x7f, 0x0d,   /*  (dip [pc+]) */
676   0, 0, 0, 0,   /*  Replaced with address of .got + 4.  */
677   0x30, 0x7a,   /* move [...],mof */
678   0x7f, 0x0d,   /*  (dip [pc+]) */
679   0, 0, 0, 0,   /*  Replaced with address of .got + 8.  */
680   0x30, 0x09    /* jump [...] */
681 };
682
683 static const bfd_byte elf_cris_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
684 {
685   0x84, 0xe2,   /* subq 4,$sp */
686   0x6f, 0xfe,   /* move.d 0,$acr */
687   0, 0, 0, 0,   /*  Replaced by address of .got + 4.  */
688   0x7e, 0x7a,   /* move $mof,[$sp] */
689   0x3f, 0x7a,   /* move [$acr],$mof */
690   0x04, 0xf2,   /* addq 4,acr */
691   0x6f, 0xfa,   /* move.d [$acr],$acr */
692   0xbf, 0x09,   /* jump $acr */
693   0xb0, 0x05,   /* nop */
694   0, 0          /*  Pad out to 26 bytes.  */
695 };
696
697 /* Subsequent entries in an absolute procedure linkage table look like
698    this.  */
699
700 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
701 {
702   0x7f, 0x0d,   /*  (dip [pc+]) */
703   0, 0, 0, 0,   /*  Replaced with address of this symbol in .got.  */
704   0x30, 0x09,   /* jump [...] */
705   0x3f,  0x7e,  /* move [pc+],mof */
706   0, 0, 0, 0,   /*  Replaced with offset into relocation table.  */
707   0x2f, 0xfe,   /* add.d [pc+],pc */
708   0xec, 0xff,
709   0xff, 0xff    /*  Replaced with offset to start of .plt.  */
710 };
711
712 static const bfd_byte elf_cris_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
713 {
714   0x6f, 0xfe,   /* move.d 0,$acr */
715   0, 0, 0, 0,   /*  Replaced with address of this symbol in .got.  */
716   0x6f, 0xfa,   /* move.d [$acr],$acr */
717   0xbf, 0x09,   /* jump $acr */
718   0xb0, 0x05,   /* nop */
719   0x3f, 0x7e,   /* move 0,mof */
720   0, 0, 0, 0,   /*  Replaced with offset into relocation table. */
721   0xbf, 0x0e,   /* ba start_of_plt0_entry */
722   0, 0, 0, 0,   /*  Replaced with offset to plt0 entry.  */
723   0xb0, 0x05    /* nop */
724 };
725
726 /* The first entry in a PIC procedure linkage table looks like this.  */
727
728 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
729 {
730   0xfc, 0xe1, 0x7e, 0x7e,       /* push mof */
731   0x04, 0x01, 0x30, 0x7a,       /* move [r0+4],mof */
732   0x08, 0x01, 0x30, 0x09,       /* jump [r0+8] */
733   0, 0, 0, 0, 0, 0, 0, 0,       /*  Pad out to 20 bytes.  */
734 };
735
736 static const bfd_byte elf_cris_pic_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
737 {
738   0x84, 0xe2,   /* subq 4,$sp */
739   0x04, 0x01,   /* addoq 4,$r0,$acr */
740   0x7e, 0x7a,   /* move $mof,[$sp] */
741   0x3f, 0x7a,   /* move [$acr],$mof */
742   0x04, 0xf2,   /* addq 4,$acr */
743   0x6f, 0xfa,   /* move.d [$acr],$acr */
744   0xbf, 0x09,   /* jump $acr */
745   0xb0, 0x05,   /* nop */
746   0, 0,         /*  Pad out to 26 bytes.  */
747   0, 0, 0, 0,
748   0, 0, 0, 0
749 };
750
751 /* Subsequent entries in a PIC procedure linkage table look like this.  */
752
753 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
754 {
755   0x6f, 0x0d,   /*  (bdap [pc+].d,r0) */
756   0, 0, 0, 0,   /*  Replaced with offset of this symbol in .got.  */
757   0x30, 0x09,   /* jump [...] */
758   0x3f, 0x7e,   /* move [pc+],mof */
759   0, 0, 0, 0,   /*  Replaced with offset into relocation table.  */
760   0x2f, 0xfe,   /* add.d [pc+],pc */
761   0xec, 0xff,   /*  Replaced with offset to start of .plt.  */
762   0xff, 0xff
763 };
764
765 static const bfd_byte elf_cris_pic_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
766 {
767   0x6f, 0x0d,   /* addo.d 0,$r0,$acr */
768   0, 0, 0, 0,   /*  Replaced with offset of this symbol in .got.  */
769   0x6f, 0xfa,   /* move.d [$acr],$acr */
770   0xbf, 0x09,   /* jump $acr */
771   0xb0, 0x05,   /* nop */
772   0x3f, 0x7e,   /* move relocoffs,$mof */
773   0, 0, 0, 0,   /*  Replaced with offset into relocation table.  */
774   0xbf, 0x0e,   /* ba start_of_plt */
775   0, 0, 0, 0,   /*  Replaced with offset to start of .plt.  */
776   0xb0, 0x05    /* nop */
777 };
778 \f
779 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
780    (and most other PIC/shlib stuff).  Check that we don't drift away
781    without reason.
782
783    The CRIS linker, like the m68k and i386 linkers (and probably the rest
784    too) needs to keep track of the number of relocs that it decides to
785    copy in check_relocs for each symbol.  This is so that it can discard
786    PC relative relocs if it doesn't need them when linking with
787    -Bsymbolic.  We store the information in a field extending the regular
788    ELF linker hash table.  */
789
790 /* This structure keeps track of the number of PC relative relocs we have
791    copied for a given symbol.  */
792
793 struct elf_cris_pcrel_relocs_copied
794 {
795   /* Next section.  */
796   struct elf_cris_pcrel_relocs_copied *next;
797   /* A section in dynobj.  */
798   asection *section;
799   /* Number of relocs copied in this section.  */
800   bfd_size_type count;
801 };
802
803 /* CRIS ELF linker hash entry.  */
804
805 struct elf_cris_link_hash_entry
806 {
807   struct elf_link_hash_entry root;
808
809   /* Number of PC relative relocs copied for this symbol.  */
810   struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
811
812   /* The GOTPLT references are CRIS-specific; the goal is to avoid having
813      both a general GOT and a PLT-specific GOT entry for the same symbol,
814      when it is referenced both as a function and as a function pointer.
815
816      Number of GOTPLT references for a function.  */
817   bfd_signed_vma gotplt_refcount;
818
819   /* Actual GOTPLT index for this symbol, if applicable, or zero if not
820      (zero is never used as an index).  FIXME: We should be able to fold
821      this with gotplt_refcount in a union, like the got and plt unions in
822      elf_link_hash_entry.  */
823   bfd_size_type gotplt_offset;
824
825   /* The root.got.refcount is the sum of the regular reference counts
826      (this) and those members below.  We have to keep a separate count
827      to track when we've found the first (or last) reference to a
828      regular got entry.  The offset is in root.got.offset.  */
829   bfd_signed_vma reg_got_refcount;
830
831   /* Similar to the above, the number of reloc references to this
832      symbols that need a R_CRIS_32_TPREL slot.  The offset is in
833      root.got.offset, because this and .dtp_refcount can't validly
834      happen when there's also a regular GOT entry; that's invalid
835      input for which an error is emitted.  */
836   bfd_signed_vma tprel_refcount;
837
838   /* Similar to the above, the number of reloc references to this
839      symbols that need a R_CRIS_DTP slot.  The offset is in
840      root.got.offset; plus 4 if .tprel_refcount > 0.  */
841   bfd_signed_vma dtp_refcount;
842 };
843
844 /* The local_got_refcounts and local_got_offsets are a multiple of
845    LSNUM in size, namely LGOT_ALLOC_NELTS_FOR(LSNUM) (plus one for the
846    refcount for GOT itself, see code), with the summary / group offset
847    for local symbols located at offset N, reference counts for
848    ordinary (address) relocs at offset N + LSNUM, for R_CRIS_DTP
849    relocs at offset N + 2*LSNUM, and for R_CRIS_32_TPREL relocs at N +
850    3*LSNUM.  */
851
852 #define LGOT_REG_NDX(x) ((x) + symtab_hdr->sh_info)
853 #define LGOT_DTP_NDX(x) ((x) + 2 * symtab_hdr->sh_info)
854 #define LGOT_TPREL_NDX(x) ((x) + 3 * symtab_hdr->sh_info)
855 #define LGOT_ALLOC_NELTS_FOR(x) ((x) * 4)
856
857 /* CRIS ELF linker hash table.  */
858
859 struct elf_cris_link_hash_table
860 {
861   struct elf_link_hash_table root;
862
863   /* We can't use the PLT offset and calculate to get the GOTPLT offset,
864      since we try and avoid creating GOTPLT:s when there's already a GOT.
865      Instead, we keep and update the next available index here.  */
866   bfd_size_type next_gotplt_entry;
867
868   /* The number of R_CRIS_32_DTPREL and R_CRIS_16_DTPREL that have
869      been seen for any input; if != 0, then the constant-offset
870      R_CRIS_DTPMOD is needed for this DSO/executable.  This turns
871      negative at relocation, so that we don't need an extra flag for
872      when the reloc is output.  */
873   bfd_signed_vma dtpmod_refcount;
874 };
875
876 /* Traverse a CRIS ELF linker hash table.  */
877
878 #define elf_cris_link_hash_traverse(table, func, info)                  \
879   (elf_link_hash_traverse                                               \
880    (&(table)->root,                                                     \
881     (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
882     (info)))
883
884 /* Get the CRIS ELF linker hash table from a link_info structure.  */
885
886 #define elf_cris_hash_table(p) \
887   ((struct elf_cris_link_hash_table *) (p)->hash)
888
889 /* Get the CRIS ELF linker hash entry from a regular hash entry (the
890    "parent class").  The .root reference is just a simple type
891    check on the argument.  */
892
893 #define elf_cris_hash_entry(p) \
894  ((struct elf_cris_link_hash_entry *) (&(p)->root))
895
896 /* Create an entry in a CRIS ELF linker hash table.  */
897
898 static struct bfd_hash_entry *
899 elf_cris_link_hash_newfunc (entry, table, string)
900      struct bfd_hash_entry *entry;
901      struct bfd_hash_table *table;
902      const char *string;
903 {
904   struct elf_cris_link_hash_entry *ret =
905     (struct elf_cris_link_hash_entry *) entry;
906
907   /* Allocate the structure if it has not already been allocated by a
908      subclass.  */
909   if (ret == (struct elf_cris_link_hash_entry *) NULL)
910     ret = ((struct elf_cris_link_hash_entry *)
911            bfd_hash_allocate (table,
912                               sizeof (struct elf_cris_link_hash_entry)));
913   if (ret == (struct elf_cris_link_hash_entry *) NULL)
914     return (struct bfd_hash_entry *) ret;
915
916   /* Call the allocation method of the superclass.  */
917   ret = ((struct elf_cris_link_hash_entry *)
918          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
919                                      table, string));
920   if (ret != (struct elf_cris_link_hash_entry *) NULL)
921     {
922       ret->pcrel_relocs_copied = NULL;
923       ret->gotplt_refcount = 0;
924       ret->gotplt_offset = 0;
925       ret->dtp_refcount = 0;
926       ret->tprel_refcount = 0;
927       ret->reg_got_refcount = 0;
928     }
929
930   return (struct bfd_hash_entry *) ret;
931 }
932
933 /* Create a CRIS ELF linker hash table.  */
934
935 static struct bfd_link_hash_table *
936 elf_cris_link_hash_table_create (abfd)
937      bfd *abfd;
938 {
939   struct elf_cris_link_hash_table *ret;
940   bfd_size_type amt = sizeof (struct elf_cris_link_hash_table);
941
942   ret = ((struct elf_cris_link_hash_table *) bfd_malloc (amt));
943   if (ret == (struct elf_cris_link_hash_table *) NULL)
944     return NULL;
945
946   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
947                                       elf_cris_link_hash_newfunc,
948                                       sizeof (struct elf_cris_link_hash_entry)))
949     {
950       free (ret);
951       return NULL;
952     }
953
954   /* Initialize to skip over the first three entries in the gotplt; they
955      are used for run-time symbol evaluation.  */
956   ret->next_gotplt_entry = 12;
957
958   /* We haven't seen any R_CRIS_nn_GOT_TPREL initially.  */
959   ret->dtpmod_refcount = 0;
960
961   return &ret->root.root;
962 }
963 \f
964 /* Perform a single relocation.  By default we use the standard BFD
965    routines, with a few tweaks.  */
966
967 static bfd_reloc_status_type
968 cris_final_link_relocate (howto, input_bfd, input_section, contents, rel,
969                           relocation)
970      reloc_howto_type *  howto;
971      bfd *               input_bfd;
972      asection *          input_section;
973      bfd_byte *          contents;
974      Elf_Internal_Rela * rel;
975      bfd_vma             relocation;
976 {
977   bfd_reloc_status_type r;
978   enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
979
980   /* PC-relative relocations are relative to the position *after*
981      the reloc.  Note that for R_CRIS_8_PCREL the adjustment is
982      not a single byte, since PC must be 16-bit-aligned.  */
983   switch (r_type)
984     {
985       /* Check that the 16-bit GOT relocs are positive.  */
986     case R_CRIS_16_GOTPLT:
987     case R_CRIS_16_GOT:
988       if ((bfd_signed_vma) relocation < 0)
989         return bfd_reloc_overflow;
990       break;
991
992     case R_CRIS_32_PLT_PCREL:
993     case R_CRIS_32_PCREL:
994       relocation -= 2;
995       /* Fall through.  */
996     case R_CRIS_8_PCREL:
997     case R_CRIS_16_PCREL:
998       relocation -= 2;
999       break;
1000
1001     default:
1002       break;
1003     }
1004
1005   r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1006                                 contents, rel->r_offset,
1007                                 relocation, rel->r_addend);
1008   return r;
1009 }
1010 \f
1011
1012 /* The number of errors left before we stop outputting reloc-specific
1013    explanatory messages.  By coincidence, this works nicely together
1014    with the default number of messages you'll get from LD about
1015    "relocation truncated to fit" messages before you get an
1016    "additional relocation overflows omitted from the output".  */
1017 static int additional_relocation_error_msg_count = 10;
1018
1019 /* Relocate an CRIS ELF section.  See elf32-fr30.c, from where this was
1020    copied, for further comments.  */
1021
1022 static bfd_boolean
1023 cris_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1024                            contents, relocs, local_syms, local_sections)
1025      bfd *output_bfd ATTRIBUTE_UNUSED;
1026      struct bfd_link_info *info;
1027      bfd *input_bfd;
1028      asection *input_section;
1029      bfd_byte *contents;
1030      Elf_Internal_Rela *relocs;
1031      Elf_Internal_Sym *local_syms;
1032      asection **local_sections;
1033 {
1034   bfd *dynobj;
1035   Elf_Internal_Shdr *symtab_hdr;
1036   struct elf_link_hash_entry **sym_hashes;
1037   bfd_vma *local_got_offsets;
1038   asection *sgot;
1039   asection *splt;
1040   asection *sreloc;
1041   Elf_Internal_Rela *rel;
1042   Elf_Internal_Rela *relend;
1043   asection *srelgot;
1044
1045   dynobj = elf_hash_table (info)->dynobj;
1046   local_got_offsets = elf_local_got_offsets (input_bfd);
1047   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1048   sym_hashes = elf_sym_hashes (input_bfd);
1049   relend     = relocs + input_section->reloc_count;
1050
1051   sgot = NULL;
1052   splt = NULL;
1053   sreloc = NULL;
1054   srelgot = NULL;
1055
1056   if (dynobj != NULL)
1057     {
1058       splt = bfd_get_section_by_name (dynobj, ".plt");
1059       sgot = bfd_get_section_by_name (dynobj, ".got");
1060     }
1061
1062   for (rel = relocs; rel < relend; rel ++)
1063     {
1064       reloc_howto_type *howto;
1065       unsigned long r_symndx;
1066       Elf_Internal_Sym *sym;
1067       asection *sec;
1068       struct elf_link_hash_entry *h;
1069       bfd_vma relocation;
1070       bfd_reloc_status_type r;
1071       const char *symname = NULL;
1072       enum elf_cris_reloc_type r_type;
1073
1074       r_type = ELF32_R_TYPE (rel->r_info);
1075
1076       if (   r_type == R_CRIS_GNU_VTINHERIT
1077           || r_type == R_CRIS_GNU_VTENTRY)
1078         continue;
1079
1080       r_symndx = ELF32_R_SYM (rel->r_info);
1081       howto  = cris_elf_howto_table + r_type;
1082       h      = NULL;
1083       sym    = NULL;
1084       sec    = NULL;
1085
1086       if (r_symndx < symtab_hdr->sh_info)
1087         {
1088           sym = local_syms + r_symndx;
1089           sec = local_sections [r_symndx];
1090           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1091
1092           symname = (bfd_elf_string_from_elf_section
1093                      (input_bfd, symtab_hdr->sh_link, sym->st_name));
1094           if (symname == NULL)
1095             symname = bfd_section_name (input_bfd, sec);
1096         }
1097       else
1098         {
1099           bfd_boolean warned;
1100           bfd_boolean unresolved_reloc;
1101
1102           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1103                                    r_symndx, symtab_hdr, sym_hashes,
1104                                    h, sec, relocation,
1105                                    unresolved_reloc, warned);
1106
1107           symname = h->root.root.string;
1108
1109           if (unresolved_reloc
1110               /* Perhaps we should detect the cases that
1111                  sec->output_section is expected to be NULL like i386 and
1112                  m68k, but apparently (and according to elfxx-ia64.c) all
1113                  valid cases are where the symbol is defined in a shared
1114                  object which we link dynamically against.  This includes
1115                  PLT relocs for which we've created a PLT entry and other
1116                  relocs for which we're prepared to create dynamic
1117                  relocations.
1118
1119                  For now, new situations cause us to just err when
1120                  sec->output_offset is NULL but the object with the symbol
1121                  is *not* dynamically linked against.  Thus this will
1122                  automatically remind us so we can see if there are other
1123                  valid cases we need to revisit.  */
1124               && (sec->owner->flags & DYNAMIC) != 0)
1125             relocation = 0;
1126
1127           else if (h->root.type == bfd_link_hash_defined
1128                    || h->root.type == bfd_link_hash_defweak)
1129             {
1130               /* Here follow the cases where the relocation value must
1131                  be zero (or when further handling is simplified when
1132                  zero).  I can't claim to understand the various
1133                  conditions and they weren't described in the files
1134                  where I copied them from (elf32-m68k.c and
1135                  elf32-i386.c), but let's mention examples of where
1136                  they happen.  FIXME: Perhaps define and use a
1137                  dynamic_symbol_p function like ia64.
1138
1139                  - When creating a shared library, we can have an
1140                  ordinary relocation for a symbol defined in a shared
1141                  library (perhaps the one we create).  We then make
1142                  the relocation value zero, as the value seen now will
1143                  be added into the relocation addend in this shared
1144                  library, but must be handled only at dynamic-link
1145                  time.  FIXME: Not sure this example covers the
1146                  h->elf_link_hash_flags test, though it's there in
1147                  other targets.  */
1148               if (info->shared
1149                   && ((! info->symbolic && h->dynindx != -1)
1150                       || !h->def_regular)
1151                   && (input_section->flags & SEC_ALLOC) != 0
1152                   && (r_type == R_CRIS_8
1153                       || r_type == R_CRIS_16
1154                       || r_type == R_CRIS_32
1155                       || r_type == R_CRIS_8_PCREL
1156                       || r_type == R_CRIS_16_PCREL
1157                       || r_type == R_CRIS_32_PCREL))
1158                 relocation = 0;
1159               else if (!info->relocatable && unresolved_reloc)
1160                 {
1161                   _bfd_error_handler
1162                     (_("%B, section %A: unresolvable relocation %s against symbol `%s'"),
1163                      input_bfd,
1164                      input_section,
1165                      cris_elf_howto_table[r_type].name,
1166                      symname);
1167                   bfd_set_error (bfd_error_bad_value);
1168                   return FALSE;
1169                 }
1170             }
1171         }
1172
1173       if (sec != NULL && elf_discarded_section (sec))
1174         {
1175           /* For relocs against symbols from removed linkonce sections,
1176              or sections discarded by a linker script, we just want the
1177              section contents zeroed.  Avoid any special processing.  */
1178           _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1179           rel->r_info = 0;
1180           rel->r_addend = 0;
1181           continue;
1182         }
1183
1184       if (info->relocatable)
1185         continue;
1186
1187       switch (r_type)
1188         {
1189         case R_CRIS_16_GOTPLT:
1190         case R_CRIS_32_GOTPLT:
1191           /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
1192              but we require a PLT, and the PLT handling will take care of
1193              filling in the PLT-specific GOT entry.  For the GOT offset,
1194              calculate it as we do when filling it in for the .got.plt
1195              section.  If we don't have a PLT, punt to GOT handling.  */
1196           if (h != NULL
1197               && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
1198             {
1199               asection *sgotplt
1200                 = bfd_get_section_by_name (dynobj, ".got.plt");
1201               bfd_vma got_offset;
1202
1203               BFD_ASSERT (h->dynindx != -1);
1204               BFD_ASSERT (sgotplt != NULL);
1205
1206               got_offset
1207                 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1208
1209               relocation = got_offset;
1210               break;
1211             }
1212
1213           /* We didn't make a PLT entry for this symbol.  Maybe everything is
1214              folded into the GOT.  Other than folding, this happens when
1215              statically linking PIC code, or when using -Bsymbolic.  Check
1216              that we instead have a GOT entry as done for us by
1217              elf_cris_adjust_dynamic_symbol, and drop through into the
1218              ordinary GOT cases.  This must not happen for the
1219              executable, because any reference it does to a function
1220              that is satisfied by a DSO must generate a PLT.  We assume
1221              these call-specific relocs don't address non-functions.  */
1222           if (h != NULL
1223               && (h->got.offset == (bfd_vma) -1
1224                   || (!info->shared
1225                       && !(h->def_regular
1226                            || (!h->def_dynamic
1227                                && h->root.type == bfd_link_hash_undefweak)))))
1228             {
1229               (*_bfd_error_handler)
1230                 ((h->got.offset == (bfd_vma) -1)
1231                  ? _("%B, section %A: No PLT nor GOT for relocation %s"
1232                      " against symbol `%s'")
1233                  : _("%B, section %A: No PLT for relocation %s"
1234                      " against symbol `%s'"),
1235                  input_bfd,
1236                  input_section,
1237                  cris_elf_howto_table[r_type].name,
1238                  (symname != NULL && symname[0] != '\0'
1239                   ? symname : _("[whose name is lost]")));
1240
1241               /* FIXME: Perhaps blaming input is not the right thing to
1242                  do; this is probably an internal error.  But it is true
1243                  that we didn't like that particular input.  */
1244               bfd_set_error (bfd_error_bad_value);
1245               return FALSE;
1246             }
1247           /* Fall through.  */
1248
1249           /* The size of the actual relocation is not used here; we only
1250              fill in the GOT table here.  */
1251         case R_CRIS_16_GOT:
1252         case R_CRIS_32_GOT:
1253           {
1254             bfd_vma off;
1255
1256             /* Note that despite using RELA relocations, the .got contents
1257                is always filled in with the link-relative relocation
1258                value; the addend.  */
1259
1260             if (h != NULL)
1261               {
1262                 off = h->got.offset;
1263                 BFD_ASSERT (off != (bfd_vma) -1);
1264
1265                 if (!elf_hash_table (info)->dynamic_sections_created
1266                     || (! info->shared
1267                         && (h->def_regular
1268                             || h->type == STT_FUNC
1269                             || h->needs_plt))
1270                     || (info->shared
1271                         && (info->symbolic || h->dynindx == -1)
1272                         && h->def_regular))
1273                   {
1274                     /* This wasn't checked above for ! info->shared, but
1275                        must hold there if we get here; the symbol must
1276                        be defined in the regular program or be undefweak
1277                        or be a function or otherwise need a PLT.  */
1278                     BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created
1279                                 || info->shared
1280                                 || h->def_regular
1281                                 || h->type == STT_FUNC
1282                                 || h->needs_plt
1283                                 || h->root.type == bfd_link_hash_undefweak);
1284
1285                     /* This is actually a static link, or it is a
1286                        -Bsymbolic link and the symbol is defined locally,
1287                        or is undefweak, or the symbol was forced to be
1288                        local because of a version file, or we're not
1289                        creating a dynamic object.  We must initialize this
1290                        entry in the global offset table.  Since the offset
1291                        must always be a multiple of 4, we use the least
1292                        significant bit to record whether we have
1293                        initialized it already.
1294
1295                        If this GOT entry should be runtime-initialized, we
1296                        will create a .rela.got relocation entry to
1297                        initialize the value.  This is done in the
1298                        finish_dynamic_symbol routine.  */
1299                     if ((off & 1) != 0)
1300                       off &= ~1;
1301                     else
1302                       {
1303                         bfd_put_32 (output_bfd, relocation,
1304                                     sgot->contents + off);
1305                         h->got.offset |= 1;
1306                       }
1307                   }
1308               }
1309             else
1310               {
1311                 BFD_ASSERT (local_got_offsets != NULL
1312                             && local_got_offsets[r_symndx] != (bfd_vma) -1);
1313
1314                 off = local_got_offsets[r_symndx];
1315
1316                 /* The offset must always be a multiple of 4.  We use
1317                    the least significant bit to record whether we have
1318                    already generated the necessary reloc.  */
1319                 if ((off & 1) != 0)
1320                   off &= ~1;
1321                 else
1322                   {
1323                     bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1324
1325                     if (info->shared)
1326                       {
1327                         Elf_Internal_Rela outrel;
1328                         bfd_byte *loc;
1329
1330                         if (srelgot == NULL)
1331                           srelgot
1332                             = bfd_get_section_by_name (dynobj, ".rela.got");
1333                         BFD_ASSERT (srelgot != NULL);
1334
1335                         outrel.r_offset = (sgot->output_section->vma
1336                                            + sgot->output_offset
1337                                            + off);
1338                         outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1339                         outrel.r_addend = relocation;
1340                         loc = srelgot->contents;
1341                         loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1342                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1343                       }
1344
1345                     local_got_offsets[r_symndx] |= 1;
1346                   }
1347               }
1348
1349             relocation = sgot->output_offset + off;
1350             if (rel->r_addend != 0)
1351               {
1352                 /* We can't do anything for a relocation which is against
1353                    a symbol *plus offset*.  GOT holds relocations for
1354                    symbols.  Make this an error; the compiler isn't
1355                    allowed to pass us these kinds of things.  */
1356                 if (h == NULL)
1357                   (*_bfd_error_handler)
1358                     (_("%B, section %A: relocation %s with non-zero addend %d"
1359                        " against local symbol"),
1360                      input_bfd,
1361                      input_section,
1362                      cris_elf_howto_table[r_type].name,
1363                      rel->r_addend);
1364                 else
1365                   (*_bfd_error_handler)
1366                     (_("%B, section %A: relocation %s with non-zero addend %d"
1367                        " against symbol `%s'"),
1368                      input_bfd,
1369                      input_section,
1370                      cris_elf_howto_table[r_type].name,
1371                      rel->r_addend,
1372                      symname[0] != '\0' ? symname : _("[whose name is lost]"));
1373
1374                 bfd_set_error (bfd_error_bad_value);
1375                 return FALSE;
1376               }
1377           }
1378           break;
1379
1380         case R_CRIS_32_GOTREL:
1381           /* This relocation must only be performed against local symbols.
1382              It's also ok when we link a program and the symbol is either
1383              defined in an ordinary (non-DSO) object or is undefined weak.  */
1384           if (h != NULL
1385               && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1386               && !(!info->shared
1387                    && (h->def_regular
1388                        || (!h->def_dynamic
1389                            && h->root.type == bfd_link_hash_undefweak))))
1390             {
1391               (*_bfd_error_handler)
1392                 (_("%B, section %A: relocation %s is"
1393                    " not allowed for global symbol: `%s'"),
1394                  input_bfd,
1395                  input_section,
1396                  cris_elf_howto_table[r_type].name,
1397                  symname);
1398               bfd_set_error (bfd_error_bad_value);
1399               return FALSE;
1400             }
1401
1402           /* This can happen if we get a link error with the input ELF
1403              variant mismatching the output variant.  Emit an error so
1404              it's noticed if it happens elsewhere.  */
1405           if (sgot == NULL)
1406             {
1407               (*_bfd_error_handler)
1408                 (_("%B, section %A: relocation %s with no GOT created"),
1409                  input_bfd,
1410                  input_section,
1411                  cris_elf_howto_table[r_type].name);
1412               bfd_set_error (bfd_error_bad_value);
1413               return FALSE;
1414             }
1415
1416           /* This relocation is like a PC-relative one, except the
1417              reference point is the location of GOT.  Note that
1418              sgot->output_offset is not involved in this calculation.  We
1419              always want the start of entire .got section, not the
1420              position after the reserved header.  */
1421           relocation -= sgot->output_section->vma;
1422           break;
1423
1424         case R_CRIS_32_PLT_PCREL:
1425           /* Relocation is to the entry for this symbol in the
1426              procedure linkage table.  */
1427
1428           /* Resolve a PLT_PCREL reloc against a local symbol directly,
1429              without using the procedure linkage table.  */
1430           if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1431             break;
1432
1433           if (h->plt.offset == (bfd_vma) -1
1434               || splt == NULL)
1435             {
1436               /* We didn't make a PLT entry for this symbol.  This
1437                  happens when statically linking PIC code, or when
1438                  using -Bsymbolic.  */
1439               break;
1440             }
1441
1442           relocation = (splt->output_section->vma
1443                         + splt->output_offset
1444                         + h->plt.offset);
1445           break;
1446
1447         case R_CRIS_32_PLT_GOTREL:
1448           /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1449              start of the .got section.  See also comment at
1450              R_CRIS_32_GOT.  */
1451           relocation -= sgot->output_section->vma;
1452
1453           /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1454              without using the procedure linkage table.  */
1455           if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1456             break;
1457
1458           if (h->plt.offset == (bfd_vma) -1
1459               || splt == NULL)
1460             {
1461               /* We didn't make a PLT entry for this symbol.  This
1462                  happens when statically linking PIC code, or when
1463                  using -Bsymbolic.  */
1464               break;
1465             }
1466
1467           relocation = (splt->output_section->vma
1468                         + splt->output_offset
1469                         + h->plt.offset
1470                         - sgot->output_section->vma);
1471           break;
1472
1473         case R_CRIS_8_PCREL:
1474         case R_CRIS_16_PCREL:
1475         case R_CRIS_32_PCREL:
1476           /* If the symbol was local, we need no shlib-specific handling.  */
1477           if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1478             break;
1479
1480           /* Fall through.  */
1481         case R_CRIS_8:
1482         case R_CRIS_16:
1483         case R_CRIS_32:
1484           if (info->shared
1485               && r_symndx != 0
1486               && (input_section->flags & SEC_ALLOC) != 0
1487               && ((r_type != R_CRIS_8_PCREL
1488                    && r_type != R_CRIS_16_PCREL
1489                    && r_type != R_CRIS_32_PCREL)
1490                   || (!info->symbolic
1491                       || !h->def_regular)))
1492             {
1493               Elf_Internal_Rela outrel;
1494               bfd_byte *loc;
1495               bfd_boolean skip, relocate;
1496
1497               /* When generating a shared object, these relocations
1498                  are copied into the output file to be resolved at run
1499                  time.  */
1500
1501               if (sreloc == NULL)
1502                 {
1503                   sreloc = _bfd_elf_get_dynamic_reloc_section
1504                     (input_bfd, input_section, /*rela?*/ TRUE);
1505                   /* The section should have been created in cris_elf_check_relocs,
1506                      but that function will not be called for objects which fail in
1507                      cris_elf_merge_private_bfd_data.  */
1508                   if (sreloc == NULL)
1509                     {
1510                       bfd_set_error (bfd_error_bad_value);
1511                       return FALSE;
1512                     }
1513                 }
1514
1515               skip = FALSE;
1516               relocate = FALSE;
1517
1518               outrel.r_offset =
1519                 _bfd_elf_section_offset (output_bfd, info, input_section,
1520                                          rel->r_offset);
1521               if (outrel.r_offset == (bfd_vma) -1)
1522                 skip = TRUE;
1523               else if (outrel.r_offset == (bfd_vma) -2)
1524                 skip = TRUE, relocate = TRUE;
1525               outrel.r_offset += (input_section->output_section->vma
1526                                   + input_section->output_offset);
1527
1528               if (skip)
1529                 memset (&outrel, 0, sizeof outrel);
1530               /* h->dynindx may be -1 if the symbol was marked to
1531                  become local.  */
1532               else if (h != NULL
1533                        && ((! info->symbolic && h->dynindx != -1)
1534                            || !h->def_regular))
1535                 {
1536                   BFD_ASSERT (h->dynindx != -1);
1537                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1538                   outrel.r_addend = relocation + rel->r_addend;
1539                 }
1540               else
1541                 {
1542                   outrel.r_addend = relocation + rel->r_addend;
1543
1544                   if (r_type == R_CRIS_32)
1545                     {
1546                       relocate = TRUE;
1547                       outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1548                     }
1549                   else
1550                     {
1551                       long indx;
1552
1553                       if (bfd_is_abs_section (sec))
1554                         indx = 0;
1555                       else if (sec == NULL || sec->owner == NULL)
1556                         {
1557                           bfd_set_error (bfd_error_bad_value);
1558                           return FALSE;
1559                         }
1560                       else
1561                         {
1562                           asection *osec;
1563
1564                           /* We are turning this relocation into one
1565                              against a section symbol.  It would be
1566                              proper to subtract the symbol's value,
1567                              osec->vma, from the emitted reloc addend,
1568                              but ld.so expects buggy relocs.  */
1569                           osec = sec->output_section;
1570                           indx = elf_section_data (osec)->dynindx;
1571                           if (indx == 0)
1572                             {
1573                               struct elf_cris_link_hash_table *htab;
1574                               htab = elf_cris_hash_table (info);
1575                               osec = htab->root.text_index_section;
1576                               indx = elf_section_data (osec)->dynindx;
1577                             }
1578                           BFD_ASSERT (indx != 0);
1579                         }
1580
1581                       outrel.r_info = ELF32_R_INFO (indx, r_type);
1582                     }
1583                 }
1584
1585               loc = sreloc->contents;
1586               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1587               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1588
1589               /* This reloc will be computed at runtime, so there's no
1590                  need to do anything now, except for R_CRIS_32 relocations
1591                  that have been turned into R_CRIS_RELATIVE.  */
1592               if (!relocate)
1593                 continue;
1594             }
1595
1596           break;
1597
1598         case R_CRIS_16_DTPREL:
1599         case R_CRIS_32_DTPREL:
1600           /* This relocation must only be performed against local
1601              symbols.  It's also ok when we link a program and the
1602              symbol is defined in an ordinary (non-DSO) object (if
1603              it's undefined there, we've already seen an error).  */
1604           if (h != NULL
1605               && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1606               && (info->shared
1607                   || (!h->def_regular
1608                       && h->root.type != bfd_link_hash_undefined)))
1609             {
1610               (*_bfd_error_handler)
1611                 ((h->root.type == bfd_link_hash_undefined)
1612                  /* We shouldn't get here for GCC-emitted code.  */
1613                  ? _("%B, section %A: relocation %s has an undefined"
1614                      " reference to `%s', perhaps a declaration mixup?")
1615                  : ("%B, section %A: relocation %s is"
1616                      " not allowed for `%s', a global symbol with default"
1617                      " visibility, perhaps a declaration mixup?"),
1618                  input_bfd,
1619                  input_section,
1620                  cris_elf_howto_table[r_type].name,
1621                  symname != NULL && symname[0] != '\0'
1622                  ? symname : _("[whose name is lost]"));
1623               bfd_set_error (bfd_error_bad_value);
1624               return FALSE;
1625             }
1626
1627           BFD_ASSERT (elf_cris_hash_table (info)->dtpmod_refcount != 0);
1628
1629           /* Fill in a R_CRIS_DTPMOD reloc at offset 3 if we haven't
1630              already done so.  Note that we do this in .got.plt, not
1631              in .got, as .got.plt contains the first part, still the
1632              reloc is against .got, because the linker script directs
1633              (is required to direct) them both into .got.  */
1634           if (elf_cris_hash_table (info)->dtpmod_refcount > 0)
1635             {
1636               asection *sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1637               BFD_ASSERT (sgotplt != NULL);
1638
1639               if (info->shared)
1640                 {
1641                   Elf_Internal_Rela outrel;
1642                   bfd_byte *loc;
1643
1644                   if (srelgot == NULL)
1645                     srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1646                   BFD_ASSERT (srelgot != NULL);
1647                   loc = srelgot->contents;
1648                   loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1649
1650                   bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 12);
1651                   bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 16);
1652                   outrel.r_offset = (sgotplt->output_section->vma
1653                                      + sgotplt->output_offset
1654                                      + 12);
1655                   outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTPMOD);
1656                   outrel.r_addend = 0;
1657                   bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1658                 }
1659               else
1660                 {
1661                   /* For an executable, the GOT entry contents is known.  */
1662                   bfd_put_32 (output_bfd, (bfd_vma) 1, sgotplt->contents + 12);
1663                   bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 16);
1664                 }
1665
1666               /* Reverse the sign to mark that we've emitted the
1667                  required GOT entry.  */
1668               elf_cris_hash_table (info)->dtpmod_refcount
1669                 = -elf_cris_hash_table (info)->dtpmod_refcount;
1670             }
1671
1672           /* The thread-based offset to the local symbol is the
1673              relocation.  */
1674           relocation -= elf_hash_table (info)->tls_sec == NULL
1675             ? 0 : elf_hash_table (info)->tls_sec->vma;
1676           break;
1677
1678         case R_CRIS_32_GD:
1679           if (info->shared)
1680             {
1681               bfd_set_error (bfd_error_invalid_operation);
1682
1683               /* We've already informed in cris_elf_check_relocs that
1684                  this is an error.  */
1685               return FALSE;
1686             }
1687           /* Fall through.  */
1688
1689         case R_CRIS_16_GOT_GD:
1690         case R_CRIS_32_GOT_GD:
1691           if (rel->r_addend != 0)
1692             {
1693               /* We can't do anything for a relocation which is against a
1694                  symbol *plus offset*.  The GOT holds relocations for
1695                  symbols.  Make this an error; the compiler isn't allowed
1696                  to pass us these kinds of things.  */
1697               (*_bfd_error_handler)
1698                 (_("%B, section %A: relocation %s with non-zero addend %d"
1699                    " against symbol `%s'"),
1700                  input_bfd,
1701                  input_section,
1702                  cris_elf_howto_table[r_type].name,
1703                  rel->r_addend,
1704                  symname[0] != '\0' ? symname : _("[whose name is lost]"));
1705
1706               bfd_set_error (bfd_error_bad_value);
1707               return FALSE;
1708             }
1709
1710           if (!info->shared && (h == NULL || h->def_regular))
1711             {
1712               /* Known contents of the GOT.  */
1713               bfd_vma off;
1714
1715               /* The symbol is defined in the program, so just write
1716                  (1, known_tpoffset) into the GOT.  */
1717               relocation -= elf_hash_table (info)->tls_sec->vma;
1718
1719               if (h != NULL)
1720                 {
1721                   off = elf_cris_hash_entry (h)->tprel_refcount > 0
1722                     ? h->got.offset + 4 : h->got.offset;
1723                 }
1724               else
1725                 {
1726                   off = local_got_offsets[r_symndx];
1727                   if (local_got_offsets[LGOT_TPREL_NDX (r_symndx)])
1728                     off += 4;
1729                 }
1730
1731               /* We use bit 1 of the offset as a flag for GOT entry with
1732                  the R_CRIS_DTP reloc, setting it when we've emitted the
1733                  GOT entry and reloc.  Bit 0 is used for R_CRIS_32_TPREL
1734                  relocs.  */
1735               if ((off & 2) == 0)
1736                 {
1737                   off &= ~3;
1738
1739                   if (h != NULL)
1740                     h->got.offset |= 2;
1741                   else
1742                     local_got_offsets[r_symndx] |= 2;
1743
1744                   bfd_put_32 (output_bfd, 1, sgot->contents + off);
1745                   bfd_put_32 (output_bfd, relocation, sgot->contents + off + 4);
1746                 }
1747               else
1748                 off &= ~3;
1749
1750               relocation = sgot->output_offset + off
1751                 + (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
1752             }
1753           else
1754             {
1755               /* Not all parts of the GOT entry are known; emit a real
1756                  relocation.  */
1757               bfd_vma off;
1758
1759               if (h != NULL)
1760                 off = elf_cris_hash_entry (h)->tprel_refcount > 0
1761                   ? h->got.offset + 4 : h->got.offset;
1762               else
1763                 {
1764                   off = local_got_offsets[r_symndx];
1765                   if (local_got_offsets[LGOT_TPREL_NDX (r_symndx)])
1766                     off += 4;
1767                 }
1768
1769               /* See above re bit 1 and bit 0 usage.  */
1770               if ((off & 2) == 0)
1771                 {
1772                   Elf_Internal_Rela outrel;
1773                   bfd_byte *loc;
1774
1775                   off &= ~3;
1776
1777                   if (h != NULL)
1778                     h->got.offset |= 2;
1779                   else
1780                     local_got_offsets[r_symndx] |= 2;
1781
1782                   /* Clear the target contents of the GOT (just as a
1783                      gesture; it's already cleared on allocation): this
1784                      relocation is not like the other dynrelocs.  */
1785                   bfd_put_32 (output_bfd, 0, sgot->contents + off);
1786                   bfd_put_32 (output_bfd, 0, sgot->contents + off + 4);
1787
1788                   if (srelgot == NULL)
1789                     srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1790                   BFD_ASSERT (srelgot != NULL);
1791
1792                   if (h != NULL && h->dynindx != -1)
1793                     {
1794                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_DTP);
1795                       relocation = 0;
1796                     }
1797                   else
1798                     {
1799                       outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTP);
1800
1801                       /* NULL if we had an error.  */
1802                       relocation -= elf_hash_table (info)->tls_sec == NULL
1803                         ? 0 : elf_hash_table (info)->tls_sec->vma;
1804                     }
1805
1806                   outrel.r_offset = (sgot->output_section->vma
1807                                      + sgot->output_offset
1808                                      + off);
1809                   outrel.r_addend = relocation;
1810                   loc = srelgot->contents;
1811                   loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1812
1813                   /* NULL if we had an error.  */
1814                   if (srelgot->contents != NULL)
1815                     bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1816                 }
1817               else
1818                 off &= ~3;
1819
1820               relocation = sgot->output_offset + off
1821                 + (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
1822             }
1823
1824           /* The GOT-relative offset to the GOT entry is the
1825              relocation, or for R_CRIS_32_GD, the actual address of
1826              the GOT entry.  */
1827           break;
1828
1829         case R_CRIS_32_GOT_TPREL:
1830         case R_CRIS_16_GOT_TPREL:
1831           if (rel->r_addend != 0)
1832             {
1833               /* We can't do anything for a relocation which is
1834                  against a symbol *plus offset*.  GOT holds
1835                  relocations for symbols.  Make this an error; the
1836                  compiler isn't allowed to pass us these kinds of
1837                  things.  */
1838               (*_bfd_error_handler)
1839                 (_("%B, section %A: relocation %s with non-zero addend %d"
1840                    " against symbol `%s'"),
1841                  input_bfd,
1842                  input_section,
1843                  cris_elf_howto_table[r_type].name,
1844                  rel->r_addend,
1845                  symname[0] != '\0' ? symname : _("[whose name is lost]"));
1846               bfd_set_error (bfd_error_bad_value);
1847               return FALSE;
1848             }
1849
1850           if (!info->shared && (h == NULL || h->def_regular))
1851             {
1852               /* Known contents of the GOT.  */
1853               bfd_vma off;
1854
1855               /* The symbol is defined in the program, so just write
1856                  the known_tpoffset into the GOT.  */
1857               relocation -= elf_hash_table (info)->tls_sec->vma;
1858
1859               if (h != NULL)
1860                 off = h->got.offset;
1861               else
1862                 off = local_got_offsets[r_symndx];
1863
1864               /* Bit 0 is used to mark whether we've emitted the required
1865                  entry (and if needed R_CRIS_32_TPREL reloc).  Bit 1
1866                  is used similarly for R_CRIS_DTP, see above.  */
1867               if ((off & 1) == 0)
1868                 {
1869                   off &= ~3;
1870
1871                   if (h != NULL)
1872                     h->got.offset |= 1;
1873                   else
1874                     local_got_offsets[r_symndx] |= 1;
1875
1876                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1877                 }
1878               else
1879                 off &= ~3;
1880
1881               relocation = sgot->output_offset + off;
1882             }
1883           else
1884             {
1885               /* Emit a real relocation.  */
1886               bfd_vma off;
1887
1888               if (h != NULL)
1889                 off = h->got.offset;
1890               else
1891                 off = local_got_offsets[r_symndx];
1892
1893               /* See above re usage of bit 0 and 1.  */
1894               if ((off & 1) == 0)
1895                 {
1896                   Elf_Internal_Rela outrel;
1897                   bfd_byte *loc;
1898
1899                   off &= ~3;
1900
1901                   if (h != NULL)
1902                     h->got.offset |= 1;
1903                   else
1904                     local_got_offsets[r_symndx] |= 1;
1905
1906                   if (srelgot == NULL)
1907                     srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1908                   BFD_ASSERT (srelgot != NULL);
1909
1910                   if (h != NULL && h->dynindx != -1)
1911                     {
1912                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_32_TPREL);
1913                       relocation = 0;
1914                     }
1915                   else
1916                     {
1917                       outrel.r_info = ELF32_R_INFO (0, R_CRIS_32_TPREL);
1918
1919                       /* NULL if we had an error.  */
1920                       relocation -= elf_hash_table (info)->tls_sec == NULL
1921                         ? 0 : elf_hash_table (info)->tls_sec->vma;
1922                     }
1923
1924                   /* Just "define" the initial contents in some
1925                      semi-logical way.  */
1926                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1927
1928                   outrel.r_offset = (sgot->output_section->vma
1929                                      + sgot->output_offset
1930                                      + off);
1931                   outrel.r_addend = relocation;
1932                   loc = srelgot->contents;
1933                   loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1934                   /* NULL if we had an error.  */
1935                   if (srelgot->contents != NULL)
1936                     bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1937                 }
1938               else
1939                 off &= ~3;
1940
1941               relocation = sgot->output_offset + off;
1942             }
1943
1944           /* The GOT-relative offset to the GOT entry is the relocation.  */
1945           break;
1946
1947         case R_CRIS_16_TPREL:
1948         case R_CRIS_32_TPREL:
1949           /* This relocation must only be performed against symbols
1950              defined in an ordinary (non-DSO) object.  */
1951           if (info->shared)
1952             {
1953               bfd_set_error (bfd_error_invalid_operation);
1954
1955               /* We've already informed in cris_elf_check_relocs that
1956                  this is an error.  */
1957               return FALSE;
1958             }
1959
1960           if (h != NULL
1961               && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1962               && !h->def_regular
1963               /* If it's undefined, then an error message has already
1964                  been emitted.  */
1965               && h->root.type != bfd_link_hash_undefined)
1966             {
1967               (*_bfd_error_handler)
1968                 (_("%B, section %A: relocation %s is"
1969                    " not allowed for symbol: `%s'"
1970                    " which is defined outside the program,"
1971                    " perhaps a declaration mixup?"),
1972                  input_bfd,
1973                  input_section,
1974                  cris_elf_howto_table[r_type].name,
1975                  symname);
1976               bfd_set_error (bfd_error_bad_value);
1977               return FALSE;
1978             }
1979
1980           /* NULL if we had an error.  */
1981           relocation -= elf_hash_table (info)->tls_sec == NULL
1982             ? 0 : elf_hash_table (info)->tls_sec->vma;
1983
1984           /* The TLS-relative offset is the relocation.  */
1985           break;
1986
1987         default:
1988           BFD_FAIL ();
1989           return FALSE;
1990         }
1991
1992       r = cris_final_link_relocate (howto, input_bfd, input_section,
1993                                      contents, rel, relocation);
1994
1995       if (r != bfd_reloc_ok)
1996         {
1997           const char * msg = (const char *) NULL;
1998
1999           switch (r)
2000             {
2001             case bfd_reloc_overflow:
2002               r = info->callbacks->reloc_overflow
2003                 (info, (h ? &h->root : NULL), symname, howto->name,
2004                  (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
2005               if (additional_relocation_error_msg_count > 0)
2006                 {
2007                   additional_relocation_error_msg_count--;
2008                   switch (r_type)
2009                     {
2010                     case R_CRIS_16_GOTPLT:
2011                     case R_CRIS_16_GOT:
2012
2013                       /* Not just TLS is involved here, so we make
2014                          generation and message depend on -fPIC/-fpic
2015                          only.  */
2016                     case R_CRIS_16_GOT_TPREL:
2017                     case R_CRIS_16_GOT_GD:
2018                       (*_bfd_error_handler)
2019                         (_("(too many global variables for -fpic:"
2020                            " recompile with -fPIC)"));
2021                       break;
2022
2023                     case R_CRIS_16_TPREL:
2024                     case R_CRIS_16_DTPREL:
2025                       (*_bfd_error_handler)
2026                         (_("(thread-local data too big for -fpic or"
2027                            " -msmall-tls: recompile with -fPIC or"
2028                            " -mno-small-tls)"));
2029                       break;
2030
2031                       /* No known cause for overflow for other relocs.  */
2032                     default:
2033                       break;
2034                     }
2035                 }
2036               break;
2037
2038             case bfd_reloc_undefined:
2039               r = info->callbacks->undefined_symbol
2040                 (info, symname, input_bfd, input_section, rel->r_offset,
2041                  TRUE);
2042               break;
2043
2044             case bfd_reloc_outofrange:
2045               msg = _("internal error: out of range error");
2046               break;
2047
2048             case bfd_reloc_notsupported:
2049               msg = _("internal error: unsupported relocation error");
2050               break;
2051
2052             case bfd_reloc_dangerous:
2053               msg = _("internal error: dangerous relocation");
2054               break;
2055
2056             default:
2057               msg = _("internal error: unknown error");
2058               break;
2059             }
2060
2061           if (msg)
2062             r = info->callbacks->warning
2063               (info, msg, symname, input_bfd, input_section, rel->r_offset);
2064
2065           if (! r)
2066             return FALSE;
2067         }
2068     }
2069
2070   return TRUE;
2071 }
2072 \f
2073 /* Finish up dynamic symbol handling.  We set the contents of various
2074    dynamic sections here.  */
2075
2076 static bfd_boolean
2077 elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
2078      bfd *output_bfd;
2079      struct bfd_link_info *info;
2080      struct elf_link_hash_entry *h;
2081      Elf_Internal_Sym *sym;
2082 {
2083   bfd *dynobj;
2084
2085   /* Where in the plt entry to put values.  */
2086   int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
2087
2088   /* What offset to add to the distance to the first PLT entry for the
2089      value at plt_off3.   */
2090   int plt_off3_value_bias = 4;
2091
2092   /* Where in the PLT entry the call-dynlink-stub is (happens to be same
2093      for PIC and non-PIC for v32 and pre-v32).  */
2094   int plt_stub_offset = 8;
2095   int plt_entry_size = PLT_ENTRY_SIZE;
2096   const bfd_byte *plt_entry = elf_cris_plt_entry;
2097   const bfd_byte *plt_pic_entry = elf_cris_pic_plt_entry;
2098
2099   /* Adjust the various PLT entry offsets.  */
2100   if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
2101     {
2102       plt_off2 = 14;
2103       plt_off3 = 20;
2104       plt_off3_value_bias = -2;
2105       plt_stub_offset = 12;
2106       plt_entry_size = PLT_ENTRY_SIZE_V32;
2107       plt_entry = elf_cris_plt_entry_v32;
2108       plt_pic_entry = elf_cris_pic_plt_entry_v32;
2109     }
2110
2111   dynobj = elf_hash_table (info)->dynobj;
2112
2113   if (h->plt.offset != (bfd_vma) -1)
2114     {
2115       asection *splt;
2116       asection *sgotplt;
2117       asection *sgot;
2118       asection *srela;
2119       bfd_vma got_base;
2120
2121       bfd_vma gotplt_offset
2122         = elf_cris_hash_entry (h)->gotplt_offset;
2123       Elf_Internal_Rela rela;
2124       bfd_byte *loc;
2125       bfd_boolean has_gotplt = gotplt_offset != 0;
2126
2127       /* Get the index in the procedure linkage table which
2128          corresponds to this symbol.  This is the index of this symbol
2129          in all the symbols for which we are making plt entries.  The
2130          first entry in the procedure linkage table is reserved.  */
2131       /* We have to count backwards here, and the result is only valid as
2132          an index into .got.plt and its relocations.  FIXME: Constants...  */
2133       bfd_vma gotplt_index = gotplt_offset/4 - 3;
2134
2135       /* Get the offset into the .got table of the entry that corresponds
2136          to this function.  Note that we embed knowledge that "incoming"
2137          .got goes after .got.plt in the output without padding (pointer
2138          aligned).  However, that knowledge is present in several other
2139          places too.  */
2140       bfd_vma got_offset
2141         = (has_gotplt
2142            ? gotplt_offset
2143            : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
2144
2145       /* This symbol has an entry in the procedure linkage table.  Set it
2146          up.  */
2147
2148       BFD_ASSERT (h->dynindx != -1);
2149
2150       splt = bfd_get_section_by_name (dynobj, ".plt");
2151       sgot = bfd_get_section_by_name (dynobj, ".got");
2152       sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
2153       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2154       BFD_ASSERT (splt != NULL && sgotplt != NULL
2155                   && (! has_gotplt || srela != NULL));
2156
2157       got_base = sgotplt->output_section->vma + sgotplt->output_offset;
2158
2159       /* Fill in the entry in the procedure linkage table.  */
2160       if (! info->shared)
2161         {
2162           memcpy (splt->contents + h->plt.offset, plt_entry,
2163                   plt_entry_size);
2164
2165           /* We need to enter the absolute address of the GOT entry here.  */
2166           bfd_put_32 (output_bfd, got_base + got_offset,
2167                       splt->contents + h->plt.offset + plt_off1);
2168         }
2169       else
2170         {
2171           memcpy (splt->contents + h->plt.offset, plt_pic_entry,
2172                   plt_entry_size);
2173           bfd_put_32 (output_bfd, got_offset,
2174                       splt->contents + h->plt.offset + plt_off1);
2175         }
2176
2177       /* Fill in the plt entry and make a relocation, if this is a "real"
2178          PLT entry.  */
2179       if (has_gotplt)
2180         {
2181           /* Fill in the offset to the reloc table.  */
2182           bfd_put_32 (output_bfd,
2183                       gotplt_index * sizeof (Elf32_External_Rela),
2184                       splt->contents + h->plt.offset + plt_off2);
2185
2186           /* Fill in the offset to the first PLT entry, where to "jump".  */
2187           bfd_put_32 (output_bfd,
2188                       - (h->plt.offset + plt_off3 + plt_off3_value_bias),
2189                       splt->contents + h->plt.offset + plt_off3);
2190
2191           /* Fill in the entry in the global offset table with the address of
2192              the relocating stub.  */
2193           bfd_put_32 (output_bfd,
2194                       (splt->output_section->vma
2195                        + splt->output_offset
2196                        + h->plt.offset
2197                        + plt_stub_offset),
2198                       sgotplt->contents + got_offset);
2199
2200           /* Fill in the entry in the .rela.plt section.  */
2201           rela.r_offset = (sgotplt->output_section->vma
2202                            + sgotplt->output_offset
2203                            + got_offset);
2204           rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
2205           rela.r_addend = 0;
2206           loc = srela->contents + gotplt_index * sizeof (Elf32_External_Rela);
2207           bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2208         }
2209
2210       if (!h->def_regular)
2211         {
2212           /* Mark the symbol as undefined, rather than as defined in
2213              the .plt section.  Leave the value alone.  */
2214           sym->st_shndx = SHN_UNDEF;
2215
2216           /* FIXME: From elf32-sparc.c 2001-02-19 (1.18).  I still don't
2217              know whether resetting the value is significant; if it really
2218              is, rather than a quirk or bug in the sparc port, then I
2219              believe we'd see this elsewhere.  */
2220           /* If the symbol is weak, we do need to clear the value.
2221              Otherwise, the PLT entry would provide a definition for
2222              the symbol even if the symbol wasn't defined anywhere,
2223              and so the symbol would never be NULL.  */
2224           if (!h->ref_regular_nonweak)
2225             sym->st_value = 0;
2226         }
2227     }
2228
2229   /* For an ordinary program, we emit .got relocs only for symbols that
2230      are in the dynamic-symbols table and are either defined by the
2231      program or are undefined weak symbols, or are function symbols
2232      where we do not output a PLT: the PLT reloc was output above and all
2233      references to the function symbol are redirected to the PLT.  */
2234   if (h->got.offset != (bfd_vma) -1
2235       && (elf_cris_hash_entry (h)->reg_got_refcount > 0)
2236       && (info->shared
2237           || (h->dynindx != -1
2238               && h->plt.offset == (bfd_vma) -1
2239               && !h->def_regular
2240               && h->root.type != bfd_link_hash_undefweak)))
2241     {
2242       asection *sgot;
2243       asection *srela;
2244       Elf_Internal_Rela rela;
2245       bfd_byte *loc;
2246       bfd_byte *where;
2247
2248       /* This symbol has an entry in the global offset table.  Set it up.  */
2249
2250       sgot = bfd_get_section_by_name (dynobj, ".got");
2251       srela = bfd_get_section_by_name (dynobj, ".rela.got");
2252       BFD_ASSERT (sgot != NULL && srela != NULL);
2253
2254       rela.r_offset = (sgot->output_section->vma
2255                        + sgot->output_offset
2256                        + (h->got.offset &~ (bfd_vma) 1));
2257
2258       /* If this is a static link, or it is a -Bsymbolic link and the
2259          symbol is defined locally or was forced to be local because
2260          of a version file, we just want to emit a RELATIVE reloc.
2261          The entry in the global offset table will already have been
2262          initialized in the relocate_section function.  */
2263       where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
2264       if (! elf_hash_table (info)->dynamic_sections_created
2265           || (info->shared
2266               && (info->symbolic || h->dynindx == -1)
2267               && h->def_regular))
2268         {
2269           rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
2270           rela.r_addend = bfd_get_signed_32 (output_bfd, where);
2271         }
2272       else
2273         {
2274           bfd_put_32 (output_bfd, (bfd_vma) 0, where);
2275           rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
2276           rela.r_addend = 0;
2277         }
2278
2279       loc = srela->contents;
2280       loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2281       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2282     }
2283
2284   if (h->needs_copy)
2285     {
2286       asection *s;
2287       Elf_Internal_Rela rela;
2288       bfd_byte *loc;
2289
2290       /* This symbol needs a copy reloc.  Set it up.  */
2291
2292       BFD_ASSERT (h->dynindx != -1
2293                   && (h->root.type == bfd_link_hash_defined
2294                       || h->root.type == bfd_link_hash_defweak));
2295
2296       s = bfd_get_section_by_name (h->root.u.def.section->owner,
2297                                    ".rela.bss");
2298       BFD_ASSERT (s != NULL);
2299
2300       rela.r_offset = (h->root.u.def.value
2301                        + h->root.u.def.section->output_section->vma
2302                        + h->root.u.def.section->output_offset);
2303       rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
2304       rela.r_addend = 0;
2305       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2306       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2307     }
2308
2309   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
2310   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2311       || h == elf_hash_table (info)->hgot)
2312     sym->st_shndx = SHN_ABS;
2313
2314   return TRUE;
2315 }
2316 \f
2317 /* Finish up the dynamic sections.  Do *not* emit relocs here, as their
2318    offsets were changed, as part of -z combreloc handling, from those we
2319    computed.  */
2320
2321 static bfd_boolean
2322 elf_cris_finish_dynamic_sections (output_bfd, info)
2323      bfd *output_bfd;
2324      struct bfd_link_info *info;
2325 {
2326   bfd *dynobj;
2327   asection *sgot;
2328   asection *sdyn;
2329
2330   dynobj = elf_hash_table (info)->dynobj;
2331
2332   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2333   BFD_ASSERT (sgot != NULL);
2334   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2335
2336   if (elf_hash_table (info)->dynamic_sections_created)
2337     {
2338       asection *splt;
2339       Elf32_External_Dyn *dyncon, *dynconend;
2340
2341       splt = bfd_get_section_by_name (dynobj, ".plt");
2342       BFD_ASSERT (splt != NULL && sdyn != NULL);
2343
2344       dyncon = (Elf32_External_Dyn *) sdyn->contents;
2345       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2346       for (; dyncon < dynconend; dyncon++)
2347         {
2348           Elf_Internal_Dyn dyn;
2349           asection *s;
2350
2351           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2352
2353           switch (dyn.d_tag)
2354             {
2355             default:
2356               break;
2357
2358             case DT_PLTGOT:
2359               s = bfd_get_section_by_name (output_bfd, ".got");
2360               BFD_ASSERT (s != NULL);
2361               dyn.d_un.d_ptr = s->vma;
2362               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2363               break;
2364
2365             case DT_JMPREL:
2366               /* Yes, we *can* have a .plt and no .plt.rela, for instance
2367                  if all symbols are found in the .got (not .got.plt).  */
2368               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2369               dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
2370               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2371               break;
2372
2373             case DT_PLTRELSZ:
2374               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2375               if (s == NULL)
2376                 dyn.d_un.d_val = 0;
2377               else
2378                 dyn.d_un.d_val = s->size;
2379               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2380               break;
2381
2382             case DT_RELASZ:
2383               /* The procedure linkage table relocs (DT_JMPREL) should
2384                  not be included in the overall relocs (DT_RELA).
2385                  Therefore, we override the DT_RELASZ entry here to
2386                  make it not include the JMPREL relocs.  Since the
2387                  linker script arranges for .rela.plt to follow all
2388                  other relocation sections, we don't have to worry
2389                  about changing the DT_RELA entry.  */
2390               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2391               if (s != NULL)
2392                 dyn.d_un.d_val -= s->size;
2393               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2394               break;
2395             }
2396         }
2397
2398       /* Fill in the first entry in the procedure linkage table.  */
2399       if (splt->size > 0)
2400         {
2401           if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
2402             {
2403               if (info->shared)
2404                 memcpy (splt->contents, elf_cris_pic_plt0_entry_v32,
2405                         PLT_ENTRY_SIZE_V32);
2406               else
2407                 {
2408                   memcpy (splt->contents, elf_cris_plt0_entry_v32,
2409                           PLT_ENTRY_SIZE_V32);
2410                   bfd_put_32 (output_bfd,
2411                               sgot->output_section->vma
2412                               + sgot->output_offset + 4,
2413                               splt->contents + 4);
2414
2415                   elf_section_data (splt->output_section)->this_hdr.sh_entsize
2416                     = PLT_ENTRY_SIZE_V32;
2417                 }
2418             }
2419           else
2420             {
2421               if (info->shared)
2422                 memcpy (splt->contents, elf_cris_pic_plt0_entry,
2423                         PLT_ENTRY_SIZE);
2424               else
2425                 {
2426                   memcpy (splt->contents, elf_cris_plt0_entry,
2427                           PLT_ENTRY_SIZE);
2428                   bfd_put_32 (output_bfd,
2429                               sgot->output_section->vma
2430                               + sgot->output_offset + 4,
2431                               splt->contents + 6);
2432                   bfd_put_32 (output_bfd,
2433                               sgot->output_section->vma
2434                               + sgot->output_offset + 8,
2435                               splt->contents + 14);
2436
2437                   elf_section_data (splt->output_section)->this_hdr.sh_entsize
2438                     = PLT_ENTRY_SIZE;
2439                 }
2440             }
2441         }
2442     }
2443
2444   /* Fill in the first three entries in the global offset table.  */
2445   if (sgot->size > 0)
2446     {
2447       if (sdyn == NULL)
2448         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2449       else
2450         bfd_put_32 (output_bfd,
2451                     sdyn->output_section->vma + sdyn->output_offset,
2452                     sgot->contents);
2453       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2454       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2455     }
2456
2457   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2458
2459   return TRUE;
2460 }
2461 \f
2462 /* Return the section that should be marked against GC for a given
2463    relocation.  */
2464
2465 static asection *
2466 cris_elf_gc_mark_hook (asection *sec,
2467                        struct bfd_link_info *info,
2468                        Elf_Internal_Rela *rel,
2469                        struct elf_link_hash_entry *h,
2470                        Elf_Internal_Sym *sym)
2471 {
2472   enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
2473   if (h != NULL)
2474     switch (r_type)
2475       {
2476       case R_CRIS_GNU_VTINHERIT:
2477       case R_CRIS_GNU_VTENTRY:
2478         return NULL;
2479
2480       default:
2481         break;
2482       }
2483
2484   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2485 }
2486
2487 /* Update the got entry reference counts for the section being removed.  */
2488
2489 static bfd_boolean
2490 cris_elf_gc_sweep_hook (bfd *abfd,
2491                         struct bfd_link_info *info,
2492                         asection *sec,
2493                         const Elf_Internal_Rela *relocs)
2494 {
2495   Elf_Internal_Shdr *symtab_hdr;
2496   struct elf_link_hash_entry **sym_hashes;
2497   bfd_signed_vma *local_got_refcounts;
2498   const Elf_Internal_Rela *rel, *relend;
2499   bfd *dynobj;
2500   asection *sgot;
2501   asection *srelgot;
2502
2503   if (info->relocatable)
2504     return TRUE;
2505
2506   dynobj = elf_hash_table (info)->dynobj;
2507   if (dynobj == NULL)
2508     return TRUE;
2509
2510   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2511   sym_hashes = elf_sym_hashes (abfd);
2512   local_got_refcounts = elf_local_got_refcounts (abfd);
2513
2514   sgot = bfd_get_section_by_name (dynobj, ".got");
2515   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2516
2517   relend = relocs + sec->reloc_count;
2518   for (rel = relocs; rel < relend; rel++)
2519     {
2520       unsigned long r_symndx;
2521       struct elf_link_hash_entry *h = NULL;
2522       bfd_signed_vma got_element_size = 4;
2523       bfd_signed_vma *specific_refcount = NULL;
2524       enum elf_cris_reloc_type r_type;
2525
2526       r_symndx = ELF32_R_SYM (rel->r_info);
2527       if (r_symndx >= symtab_hdr->sh_info)
2528         {
2529           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2530           while (h->root.type == bfd_link_hash_indirect
2531                  || h->root.type == bfd_link_hash_warning)
2532             h = (struct elf_link_hash_entry *) h->root.u.i.link;
2533         }
2534
2535       r_type = ELF32_R_TYPE (rel->r_info);
2536       switch (r_type)
2537         {
2538         case R_CRIS_32_GOT:
2539         case R_CRIS_16_GOT:
2540         case R_CRIS_16_GOTPLT:
2541         case R_CRIS_32_GOTPLT:
2542           specific_refcount = h != NULL
2543             ? &((struct elf_cris_link_hash_entry *) h)->reg_got_refcount
2544             : &local_got_refcounts[LGOT_REG_NDX (r_symndx)];
2545           break;
2546
2547         case R_CRIS_32_GD:
2548         case R_CRIS_32_GOT_GD:
2549         case R_CRIS_16_GOT_GD:
2550           got_element_size = 8;
2551           specific_refcount = h != NULL
2552             ? &((struct elf_cris_link_hash_entry *) h)->dtp_refcount
2553             : &local_got_refcounts[LGOT_DTP_NDX (r_symndx)];
2554           break;
2555
2556         case R_CRIS_16_GOT_TPREL:
2557         case R_CRIS_32_GOT_TPREL:
2558           specific_refcount = h != NULL
2559             ? &((struct elf_cris_link_hash_entry *) h)->tprel_refcount
2560             : &local_got_refcounts[LGOT_TPREL_NDX (r_symndx)];
2561           break;
2562
2563         default:
2564           break;
2565         }
2566
2567       switch (r_type)
2568         {
2569         case R_CRIS_32_GD:
2570         case R_CRIS_16_GOT_TPREL:
2571         case R_CRIS_32_GOT_TPREL:
2572         case R_CRIS_32_GOT_GD:
2573         case R_CRIS_16_GOT_GD:
2574         case R_CRIS_16_GOT:
2575         case R_CRIS_32_GOT:
2576           if (h != NULL)
2577             {
2578               /* If the counters are 0 when we got here, we've
2579                  miscounted somehow somewhere, an internal error.  */
2580               BFD_ASSERT (h->got.refcount > 0);
2581               --h->got.refcount;
2582
2583               BFD_ASSERT (*specific_refcount > 0);
2584               --*specific_refcount;
2585               if (*specific_refcount == 0)
2586                 {
2587                   /* We don't need the .got entry any more.  */
2588                   sgot->size -= got_element_size;
2589                   srelgot->size -= sizeof (Elf32_External_Rela);
2590                 }
2591               break;
2592             }
2593
2594         local_got_reloc:
2595           if (local_got_refcounts != NULL)
2596             {
2597               /* If the counters are 0 when we got here, we've
2598                  miscounted somehow somewhere, an internal error.  */
2599               BFD_ASSERT (local_got_refcounts[r_symndx] > 0);
2600               --local_got_refcounts[r_symndx];
2601
2602               BFD_ASSERT (*specific_refcount > 0);
2603               --*specific_refcount;
2604               if (*specific_refcount == 0)
2605                 {
2606                   /* We don't need the .got entry any more.  */
2607                   sgot->size -= got_element_size;
2608                   if (info->shared)
2609                     srelgot->size -= sizeof (Elf32_External_Rela);
2610                 }
2611             }
2612           break;
2613
2614         case R_CRIS_16_GOTPLT:
2615         case R_CRIS_32_GOTPLT:
2616           /* For local symbols, treat these like GOT relocs.  */
2617           if (h == NULL)
2618             goto local_got_reloc;
2619           /* Fall through.  */
2620
2621         case R_CRIS_32_PLT_GOTREL:
2622           /* FIXME: We don't garbage-collect away the .got section.  */
2623           if (local_got_refcounts != NULL)
2624             local_got_refcounts[-1]--;
2625           /* Fall through.  */
2626
2627         case R_CRIS_8_PCREL:
2628         case R_CRIS_16_PCREL:
2629         case R_CRIS_32_PCREL:
2630         case R_CRIS_32_PLT_PCREL:
2631           if (h != NULL)
2632             {
2633               if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2634                   && h->plt.refcount > 0)
2635                 --h->plt.refcount;
2636             }
2637           break;
2638
2639         case R_CRIS_32_DTPREL:
2640         case R_CRIS_16_DTPREL:
2641           elf_cris_hash_table (info)->dtpmod_refcount--;
2642           if (elf_cris_hash_table (info)->dtpmod_refcount == 0)
2643             elf_cris_hash_table (info)->next_gotplt_entry -= 8;
2644           BFD_ASSERT (local_got_refcounts != NULL);
2645           local_got_refcounts[-1]--;
2646           break;
2647
2648         default:
2649           break;
2650         }
2651     }
2652
2653   return TRUE;
2654 }
2655
2656 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
2657    entry but we found we will not create any.  Called when we find we will
2658    not have any PLT for this symbol, by for example
2659    elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
2660    or elf_cris_size_dynamic_sections if no dynamic sections will be
2661    created (we're only linking static objects).  */
2662
2663 static bfd_boolean
2664 elf_cris_adjust_gotplt_to_got (h, p)
2665      struct elf_cris_link_hash_entry *h;
2666      PTR p;
2667 {
2668   struct bfd_link_info *info = (struct bfd_link_info *) p;
2669
2670   if (h->root.root.type == bfd_link_hash_warning)
2671     h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
2672
2673   /* If nobody wanted a GOTPLT with this symbol, we're done.  */
2674   if (h->gotplt_refcount <= 0)
2675     return TRUE;
2676
2677   if (h->reg_got_refcount > 0)
2678     {
2679       /* There's a GOT entry for this symbol.  Just adjust the refcounts.
2680          Probably not necessary at this stage, but keeping them accurate
2681          helps avoiding surprises later.  */
2682       h->root.got.refcount += h->gotplt_refcount;
2683       h->reg_got_refcount += h->gotplt_refcount;
2684       h->gotplt_refcount = 0;
2685     }
2686   else
2687     {
2688       /* No GOT entry for this symbol.  We need to create one.  */
2689       bfd *dynobj = elf_hash_table (info)->dynobj;
2690       asection *sgot;
2691       asection *srelgot;
2692
2693       BFD_ASSERT (dynobj != NULL);
2694       sgot = bfd_get_section_by_name (dynobj, ".got");
2695       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2696
2697       /* Put accurate refcounts there.  */
2698       h->root.got.refcount += h->gotplt_refcount;
2699       h->reg_got_refcount = h->gotplt_refcount;
2700
2701       h->gotplt_refcount = 0;
2702
2703       /* We always have a .got and a .rela.got section if there were
2704          GOTPLT relocs in input.  */
2705       BFD_ASSERT (sgot != NULL && srelgot != NULL);
2706
2707       /* Allocate space in the .got section.  */
2708       sgot->size += 4;
2709
2710       /* Allocate relocation space.  */
2711       srelgot->size += sizeof (Elf32_External_Rela);
2712     }
2713
2714   return TRUE;
2715 }
2716
2717 /* Try to fold PLT entries with GOT entries.  There are two cases when we
2718    want to do this:
2719
2720    - When all PLT references are GOTPLT references, and there are GOT
2721      references, and this is not the executable.  We don't have to
2722      generate a PLT at all.
2723
2724    - When there are both (ordinary) PLT references and GOT references,
2725      and this isn't the executable.
2726      We want to make the PLT reference use the ordinary GOT entry rather
2727      than R_CRIS_JUMP_SLOT, a run-time dynamically resolved GOTPLT entry,
2728      since the GOT entry will have to be resolved at startup anyway.
2729
2730    Though the latter case is handled when room for the PLT is allocated,
2731    not here.
2732
2733    By folding into the GOT, we may need a round-trip to a PLT in the
2734    executable for calls, a loss in performance.  Still, losing a
2735    reloc is a win in size and at least in start-up time.
2736
2737    Note that this function is called before symbols are forced local by
2738    version scripts.  The differing cases are handled by
2739    elf_cris_hide_symbol.  */
2740
2741 static bfd_boolean
2742 elf_cris_try_fold_plt_to_got (h, p)
2743      struct elf_cris_link_hash_entry *h;
2744      PTR p;
2745 {
2746   struct bfd_link_info *info = (struct bfd_link_info *) p;
2747
2748   /* If there are no GOT references for this symbol, we can't fold any
2749      other reference so there's nothing to do.  Likewise if there are no
2750      PLT references; GOTPLT references included.  */
2751   if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
2752     return TRUE;
2753
2754   /* GOTPLT relocs are supposed to be included into the PLT refcount.  */
2755   BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
2756
2757   if (h->gotplt_refcount == h->root.plt.refcount)
2758     {
2759       /* The only PLT references are GOTPLT references, and there are GOT
2760          references.  Convert PLT to GOT references.  */
2761       if (! elf_cris_adjust_gotplt_to_got (h, info))
2762         return FALSE;
2763
2764       /* Clear the PLT references, so no PLT will be created.  */
2765       h->root.plt.offset = (bfd_vma) -1;
2766     }
2767
2768   return TRUE;
2769 }
2770
2771 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
2772    to use a GOT entry (and create one) rather than requiring a GOTPLT
2773    entry.  */
2774
2775 static void
2776 elf_cris_hide_symbol (info, h, force_local)
2777      struct bfd_link_info *info;
2778      struct elf_link_hash_entry *h;
2779      bfd_boolean force_local;
2780 {
2781   elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
2782
2783   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
2784 }
2785
2786 /* Adjust a symbol defined by a dynamic object and referenced by a
2787    regular object.  The current definition is in some section of the
2788    dynamic object, but we're not including those sections.  We have to
2789    change the definition to something the rest of the link can
2790    understand.  */
2791
2792 static bfd_boolean
2793 elf_cris_adjust_dynamic_symbol (info, h)
2794      struct bfd_link_info *info;
2795      struct elf_link_hash_entry *h;
2796 {
2797   bfd *dynobj;
2798   asection *s;
2799   bfd_size_type plt_entry_size;
2800
2801   dynobj = elf_hash_table (info)->dynobj;
2802
2803   /* Make sure we know what is going on here.  */
2804   BFD_ASSERT (dynobj != NULL
2805               && (h->needs_plt
2806                   || h->u.weakdef != NULL
2807                   || (h->def_dynamic
2808                       && h->ref_regular
2809                       && !h->def_regular)));
2810
2811   plt_entry_size
2812     = (bfd_get_mach (dynobj) == bfd_mach_cris_v32
2813        ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
2814
2815   /* If this is a function, put it in the procedure linkage table.  We
2816      will fill in the contents of the procedure linkage table later,
2817      when we know the address of the .got section.  */
2818   if (h->type == STT_FUNC
2819       || h->needs_plt)
2820     {
2821       /* If we link a program (not a DSO), we'll get rid of unnecessary
2822          PLT entries; we point to the actual symbols -- even for pic
2823          relocs, because a program built with -fpic should have the same
2824          result as one built without -fpic, specifically considering weak
2825          symbols.
2826          FIXME: m68k and i386 differ here, for unclear reasons.  */
2827       if (! info->shared
2828           && !h->def_dynamic)
2829         {
2830           /* This case can occur if we saw a PLT reloc in an input file,
2831              but the symbol was not defined by a dynamic object.  In such
2832              a case, we don't actually need to build a procedure linkage
2833              table, and we can just do an absolute or PC reloc instead, or
2834              change a .got.plt index to a .got index for GOTPLT relocs.  */
2835           BFD_ASSERT (h->needs_plt);
2836           h->needs_plt = 0;
2837           h->plt.offset = (bfd_vma) -1;
2838           return
2839             elf_cris_adjust_gotplt_to_got ((struct
2840                                             elf_cris_link_hash_entry *) h,
2841                                            info);
2842         }
2843
2844       /* If we had a R_CRIS_GLOB_DAT that didn't have to point to a PLT;
2845          where a pointer-equivalent symbol was unimportant (i.e. more
2846          like R_CRIS_JUMP_SLOT after symbol evaluation) we could get rid
2847          of the PLT.  We can't for the executable, because the GOT
2848          entries will point to the PLT there (and be constant).  */
2849       if (info->shared
2850           && !elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry*)
2851                                             h, info))
2852         return FALSE;
2853
2854       /* GC or folding may have rendered this entry unused.  */
2855       if (h->plt.refcount <= 0)
2856         {
2857           h->needs_plt = 0;
2858           h->plt.offset = (bfd_vma) -1;
2859           return TRUE;
2860         }
2861
2862       /* Make sure this symbol is output as a dynamic symbol.  */
2863       if (h->dynindx == -1)
2864         {
2865           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2866             return FALSE;
2867         }
2868
2869       s = bfd_get_section_by_name (dynobj, ".plt");
2870       BFD_ASSERT (s != NULL);
2871
2872       /* If this is the first .plt entry, make room for the special
2873          first entry.  */
2874       if (s->size == 0)
2875         s->size += plt_entry_size;
2876
2877       /* If this symbol is not defined in a regular file, and we are
2878          not generating a shared library, then set the symbol to this
2879          location in the .plt.  */
2880       if (!info->shared
2881           && !h->def_regular)
2882         {
2883           h->root.u.def.section = s;
2884           h->root.u.def.value = s->size;
2885         }
2886
2887       /* If there's already a GOT entry, use that, not a .got.plt.  A
2888          GOT field still has a reference count when we get here; it's
2889          not yet changed to an offset.  We can't do this for an
2890          executable, because then the reloc associated with the PLT
2891          would get a non-PLT reloc pointing to the PLT.  FIXME: Move
2892          this to elf_cris_try_fold_plt_to_got.  */
2893       if (info->shared && h->got.refcount > 0)
2894         {
2895           h->got.refcount += h->plt.refcount;
2896
2897           /* Mark the PLT offset to use the GOT entry by setting the low
2898              bit in the plt offset; it is always a multiple of
2899              plt_entry_size (which is at least a multiple of 2).  */
2900           BFD_ASSERT ((s->size % plt_entry_size) == 0);
2901
2902           /* Change the PLT refcount to an offset.  */
2903           h->plt.offset = s->size;
2904
2905           /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2906              that the got entry should be used instead.  */
2907           BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2908                        h)->gotplt_offset == 0);
2909
2910           /* Make room for this entry.  */
2911           s->size += plt_entry_size;
2912
2913           return TRUE;
2914         }
2915
2916       /* No GOT reference for this symbol; prepare for an ordinary PLT.  */
2917       h->plt.offset = s->size;
2918
2919       /* Make room for this entry.  */
2920       s->size += plt_entry_size;
2921
2922       /* We also need to make an entry in the .got.plt section, which
2923          will be placed in the .got section by the linker script.  */
2924       ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2925         = elf_cris_hash_table (info)->next_gotplt_entry;
2926       elf_cris_hash_table (info)->next_gotplt_entry += 4;
2927
2928       s = bfd_get_section_by_name (dynobj, ".got.plt");
2929       BFD_ASSERT (s != NULL);
2930       s->size += 4;
2931
2932       /* We also need to make an entry in the .rela.plt section.  */
2933
2934       s = bfd_get_section_by_name (dynobj, ".rela.plt");
2935       BFD_ASSERT (s != NULL);
2936       s->size += sizeof (Elf32_External_Rela);
2937
2938       return TRUE;
2939     }
2940
2941   /* Reinitialize the plt offset now that it is not used as a reference
2942      count any more.  */
2943   h->plt.offset = (bfd_vma) -1;
2944
2945   /* If this is a weak symbol, and there is a real definition, the
2946      processor independent code will have arranged for us to see the
2947      real definition first, and we can just use the same value.  */
2948   if (h->u.weakdef != NULL)
2949     {
2950       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2951                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
2952       h->root.u.def.section = h->u.weakdef->root.u.def.section;
2953       h->root.u.def.value = h->u.weakdef->root.u.def.value;
2954       return TRUE;
2955     }
2956
2957   /* This is a reference to a symbol defined by a dynamic object which
2958      is not a function.  */
2959
2960   /* If we are creating a shared library, we must presume that the
2961      only references to the symbol are via the global offset table.
2962      For such cases we need not do anything here; the relocations will
2963      be handled correctly by relocate_section.  */
2964   if (info->shared)
2965     return TRUE;
2966
2967   /* If there are no references to this symbol that do not use the
2968      GOT, we don't need to generate a copy reloc.  */
2969   if (!h->non_got_ref)
2970     return TRUE;
2971
2972   if (h->size == 0)
2973     {
2974       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
2975                              h->root.root.string);
2976       return TRUE;
2977     }
2978
2979   /* We must allocate the symbol in our .dynbss section, which will
2980      become part of the .bss section of the executable.  There will be
2981      an entry for this symbol in the .dynsym section.  The dynamic
2982      object will contain position independent code, so all references
2983      from the dynamic object to this symbol will go through the global
2984      offset table.  The dynamic linker will use the .dynsym entry to
2985      determine the address it must put in the global offset table, so
2986      both the dynamic object and the regular object will refer to the
2987      same memory location for the variable.  */
2988
2989   s = bfd_get_section_by_name (dynobj, ".dynbss");
2990   BFD_ASSERT (s != NULL);
2991
2992   /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
2993      copy the initial value out of the dynamic object and into the
2994      runtime process image.  We need to remember the offset into the
2995      .rela.bss section we are going to use.  */
2996   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2997     {
2998       asection *srel;
2999
3000       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
3001       BFD_ASSERT (srel != NULL);
3002       srel->size += sizeof (Elf32_External_Rela);
3003       h->needs_copy = 1;
3004     }
3005
3006   return _bfd_elf_adjust_dynamic_copy (h, s);
3007 }
3008
3009 /* Look through the relocs for a section during the first phase.  */
3010
3011 static bfd_boolean
3012 cris_elf_check_relocs (abfd, info, sec, relocs)
3013      bfd *abfd;
3014      struct bfd_link_info *info;
3015      asection *sec;
3016      const Elf_Internal_Rela *relocs;
3017 {
3018   bfd *dynobj;
3019   Elf_Internal_Shdr *symtab_hdr;
3020   struct elf_link_hash_entry **sym_hashes;
3021   bfd_signed_vma *local_got_refcounts;
3022   const Elf_Internal_Rela *rel;
3023   const Elf_Internal_Rela *rel_end;
3024   asection *sgot;
3025   asection *srelgot;
3026   asection *sreloc;
3027
3028   if (info->relocatable)
3029     return TRUE;
3030
3031   dynobj = elf_hash_table (info)->dynobj;
3032   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3033   sym_hashes = elf_sym_hashes (abfd);
3034   local_got_refcounts = elf_local_got_refcounts (abfd);
3035
3036   sgot = NULL;
3037   srelgot = NULL;
3038   sreloc = NULL;
3039
3040   rel_end = relocs + sec->reloc_count;
3041   for (rel = relocs; rel < rel_end; rel++)
3042     {
3043       struct elf_link_hash_entry *h;
3044       unsigned long r_symndx;
3045       enum elf_cris_reloc_type r_type;
3046       bfd_signed_vma got_element_size = 4;
3047       unsigned long r_symndx_lgot = INT_MAX;
3048
3049       r_symndx = ELF32_R_SYM (rel->r_info);
3050       if (r_symndx < symtab_hdr->sh_info)
3051         {
3052           h = NULL;
3053           r_symndx_lgot = LGOT_REG_NDX (r_symndx);
3054         }
3055       else
3056         {
3057           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3058           while (h->root.type == bfd_link_hash_indirect
3059                  || h->root.type == bfd_link_hash_warning)
3060             h = (struct elf_link_hash_entry *) h->root.u.i.link;
3061         }
3062
3063       r_type = ELF32_R_TYPE (rel->r_info);
3064
3065       /* Some relocs require linker-created sections; we need to hang them
3066          on the first input bfd we found that contained dynamic relocs.  */
3067       switch (r_type)
3068         {
3069         case R_CRIS_16_DTPREL:
3070         case R_CRIS_32_DTPREL:
3071           /* The first .got.plt entry is right after the R_CRIS_DTPMOD
3072              entry at index 3. */
3073           if (elf_cris_hash_table (info)->dtpmod_refcount == 0)
3074             elf_cris_hash_table (info)->next_gotplt_entry += 8;
3075           elf_cris_hash_table (info)->dtpmod_refcount++;
3076           /* Fall through.  */
3077
3078         case R_CRIS_32_GD:
3079         case R_CRIS_16_GOT_GD:
3080         case R_CRIS_32_GOT_GD:
3081         case R_CRIS_32_GOT_TPREL:
3082         case R_CRIS_16_GOT_TPREL:
3083         case R_CRIS_16_GOT:
3084         case R_CRIS_32_GOT:
3085         case R_CRIS_32_GOTREL:
3086         case R_CRIS_32_PLT_GOTREL:
3087         case R_CRIS_32_PLT_PCREL:
3088         case R_CRIS_16_GOTPLT:
3089         case R_CRIS_32_GOTPLT:
3090           if (dynobj == NULL)
3091             {
3092               elf_hash_table (info)->dynobj = dynobj = abfd;
3093
3094               /* We could handle this if we can get a handle on the
3095                  output bfd in elf_cris_adjust_dynamic_symbol.  Failing
3096                  that, we must insist on dynobj being a specific mach.  */
3097               if (bfd_get_mach (dynobj) == bfd_mach_cris_v10_v32)
3098                 {
3099                   (*_bfd_error_handler)
3100                     (_("%B, section %A:\n  v10/v32 compatible object %s"
3101                        " must not contain a PIC relocation"),
3102                      abfd, sec);
3103                   return FALSE;
3104                 }
3105
3106               /* Create the .got section, so we can assume it's always
3107                  present whenever there's a dynobj.  */
3108               if (!_bfd_elf_create_got_section (dynobj, info))
3109                 return FALSE;
3110             }
3111
3112           if (sgot == NULL)
3113             sgot = bfd_get_section_by_name (dynobj, ".got");
3114
3115           if (local_got_refcounts == NULL)
3116             {
3117               bfd_size_type amt;
3118
3119               /* We use index local_got_refcounts[-1] to count all
3120                  GOT-relative relocations that do not have explicit
3121                  GOT entries.  */
3122               amt = LGOT_ALLOC_NELTS_FOR (symtab_hdr->sh_info) + 1;
3123               amt *= sizeof (bfd_signed_vma);
3124               local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
3125               if (local_got_refcounts == NULL)
3126                 return FALSE;
3127
3128               local_got_refcounts++;
3129               elf_local_got_refcounts (abfd) = local_got_refcounts;
3130             }
3131           break;
3132
3133         default:
3134           break;
3135         }
3136
3137       /* Some relocs require a global offset table (but perhaps not a
3138          specific GOT entry).  */
3139       switch (r_type)
3140         {
3141         case R_CRIS_16_DTPREL:
3142         case R_CRIS_32_DTPREL:
3143           /* Not requesting .got.rela for an executable: the contents
3144              of the first entry is constant there.  For a shared
3145              library, we need .got.rela for the R_CRIS_DTPMOD
3146              relocation at index 3.  */
3147           if (!info->shared)
3148             break;
3149           /* Fall through.  */
3150
3151         case R_CRIS_32_GD:
3152         case R_CRIS_16_GOT_GD:
3153         case R_CRIS_32_GOT_GD:
3154         case R_CRIS_32_GOT_TPREL:
3155         case R_CRIS_16_GOT_TPREL:
3156           /* Fall through.  */
3157
3158           /* For R_CRIS_16_GOTPLT and R_CRIS_32_GOTPLT, we need a GOT
3159              entry only for local symbols.  Unfortunately, we don't know
3160              until later on if there's a version script that forces the
3161              symbol local.  We must have the .rela.got section in place
3162              before we know if the symbol looks global now, so we need
3163              to treat the reloc just like for R_CRIS_16_GOT and
3164              R_CRIS_32_GOT.  */
3165         case R_CRIS_16_GOTPLT:
3166         case R_CRIS_32_GOTPLT:
3167         case R_CRIS_16_GOT:
3168         case R_CRIS_32_GOT:
3169           if (srelgot == NULL
3170               && (h != NULL || info->shared))
3171             {
3172               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3173               if (srelgot == NULL)
3174                 {
3175                   srelgot = bfd_make_section_with_flags (dynobj,
3176                                                          ".rela.got",
3177                                                          (SEC_ALLOC
3178                                                           | SEC_LOAD
3179                                                           | SEC_HAS_CONTENTS
3180                                                           | SEC_IN_MEMORY
3181                                                           | SEC_LINKER_CREATED
3182                                                           | SEC_READONLY));
3183                   if (srelgot == NULL
3184                       || !bfd_set_section_alignment (dynobj, srelgot, 2))
3185                     return FALSE;
3186                 }
3187             }
3188           break;
3189
3190         default:
3191           break;
3192         }
3193
3194       /* Warn and error for invalid input.  */
3195       switch (r_type)
3196         {
3197         case R_CRIS_32_TPREL:
3198         case R_CRIS_16_TPREL:
3199         case R_CRIS_32_GD:
3200           if (info->shared)
3201             {
3202               (*_bfd_error_handler)
3203                 (_("%B, section %A:\n  relocation %s not valid"
3204                    " in a shared object;"
3205                    " typically an option mixup, recompile with -fPIC"),
3206                  abfd,
3207                  sec,
3208                  cris_elf_howto_table[r_type].name);
3209               /* Don't return FALSE here; we want messages for all of
3210                  these and the error behavior is ungraceful
3211                  anyway.  */
3212             }
3213         default:
3214           break;
3215         }
3216
3217       switch (r_type)
3218         {
3219         case R_CRIS_32_GD:
3220         case R_CRIS_16_GOT_GD:
3221         case R_CRIS_32_GOT_GD:
3222           /* These are requests for tls_index entries, run-time R_CRIS_DTP.  */
3223           got_element_size = 8;
3224           r_symndx_lgot = LGOT_DTP_NDX (r_symndx);
3225           break;
3226
3227         case R_CRIS_16_DTPREL:
3228         case R_CRIS_32_DTPREL:
3229           /* These two just request for the constant-index
3230              module-local tls_index-sized GOT entry, which we add
3231              elsewhere.  */
3232           break;
3233
3234         case R_CRIS_32_GOT_TPREL:
3235         case R_CRIS_16_GOT_TPREL:
3236           r_symndx_lgot = LGOT_TPREL_NDX (r_symndx);
3237
3238           /* Those two relocs also require that a DSO is of type
3239              Initial Exec.  Like other targets, we don't reset this
3240              flag even if the relocs are GC:ed away.  */
3241           if (info->shared)
3242             info->flags |= DF_STATIC_TLS;
3243           break;
3244
3245           /* Let's list the other assembler-generated TLS-relocs too,
3246              just to show that they're not forgotten. */
3247         case R_CRIS_16_TPREL:
3248         case R_CRIS_32_TPREL:
3249         default:
3250           break;
3251         }
3252
3253       switch (r_type)
3254         {
3255         case R_CRIS_16_GOTPLT:
3256         case R_CRIS_32_GOTPLT:
3257           /* Mark that we need a GOT entry if the PLT entry (and its GOT
3258              entry) is eliminated.  We can only do this for a non-local
3259              symbol.  */
3260           if (h != NULL)
3261             {
3262               elf_cris_hash_entry (h)->gotplt_refcount++;
3263               goto handle_gotplt_reloc;
3264             }
3265           /* If h is NULL then this is a local symbol, and we must make a
3266              GOT entry for it, so handle it like a GOT reloc.  */
3267           /* Fall through.  */
3268
3269         case R_CRIS_32_GD:
3270         case R_CRIS_16_GOT_GD:
3271         case R_CRIS_32_GOT_GD:
3272         case R_CRIS_32_GOT_TPREL:
3273         case R_CRIS_16_GOT_TPREL:
3274         case R_CRIS_16_GOT:
3275         case R_CRIS_32_GOT:
3276           /* This symbol requires a global offset table entry.  */
3277           if (h != NULL)
3278             {
3279               if (h->got.refcount == 0)
3280                 {
3281                   /* Make sure this symbol is output as a dynamic symbol.  */
3282                   if (h->dynindx == -1)
3283                     {
3284                       if (!bfd_elf_link_record_dynamic_symbol (info, h))
3285                         return FALSE;
3286                     }
3287                 }
3288
3289               /* Update the sum of reloc counts for this symbol.  */
3290               h->got.refcount++;
3291
3292               switch (r_type)
3293                 {
3294                 case R_CRIS_16_GOT:
3295                 case R_CRIS_32_GOT:
3296                   if (elf_cris_hash_entry (h)->reg_got_refcount == 0)
3297                     {
3298                       /* Allocate space in the .got section.  */
3299                       sgot->size += got_element_size;
3300                       /* Allocate relocation space.  */
3301                       srelgot->size += sizeof (Elf32_External_Rela);
3302                     }
3303                   elf_cris_hash_entry (h)->reg_got_refcount++;
3304                   break;
3305
3306                 case R_CRIS_32_GD:
3307                 case R_CRIS_16_GOT_GD:
3308                 case R_CRIS_32_GOT_GD:
3309                   if (elf_cris_hash_entry (h)->dtp_refcount == 0)
3310                     {
3311                       /* Allocate space in the .got section.  */
3312                       sgot->size += got_element_size;
3313                       /* Allocate relocation space.  */
3314                       srelgot->size += sizeof (Elf32_External_Rela);
3315                     }
3316                   elf_cris_hash_entry (h)->dtp_refcount++;
3317                   break;
3318
3319                 case R_CRIS_32_GOT_TPREL:
3320                 case R_CRIS_16_GOT_TPREL:
3321                   if (elf_cris_hash_entry (h)->tprel_refcount == 0)
3322                     {
3323                       /* Allocate space in the .got section.  */
3324                       sgot->size += got_element_size;
3325                       /* Allocate relocation space.  */
3326                       srelgot->size += sizeof (Elf32_External_Rela);
3327                     }
3328                   elf_cris_hash_entry (h)->tprel_refcount++;
3329                   break;
3330
3331                 default:
3332                   BFD_FAIL ();
3333                   break;
3334                 }
3335             }
3336           else
3337             {
3338               /* This is a global offset table entry for a local symbol.  */
3339               if (local_got_refcounts[r_symndx_lgot] == 0)
3340                 {
3341                   sgot->size += got_element_size;
3342                   if (info->shared)
3343                     {
3344                       /* If we are generating a shared object, we need
3345                          to output a R_CRIS_RELATIVE reloc so that the
3346                          dynamic linker can adjust this GOT entry.
3347                          Similarly for non-regular got entries.  */
3348                       srelgot->size += sizeof (Elf32_External_Rela);
3349                     }
3350                 }
3351               /* Update the reloc-specific count.  */
3352               local_got_refcounts[r_symndx_lgot]++;
3353
3354               /* This one is the sum of all the others.  */
3355               local_got_refcounts[r_symndx]++;
3356             }
3357           break;
3358
3359         case R_CRIS_16_DTPREL:
3360         case R_CRIS_32_DTPREL:
3361         case R_CRIS_32_GOTREL:
3362           /* This reference requires a global offset table.
3363              FIXME: The actual refcount isn't used currently; the .got
3364              section can't be removed if there were any references in the
3365              input.  */
3366           local_got_refcounts[-1]++;
3367           break;
3368
3369         handle_gotplt_reloc:
3370
3371         case R_CRIS_32_PLT_GOTREL:
3372           /* This reference requires a global offset table.  */
3373           local_got_refcounts[-1]++;
3374           /* Fall through.  */
3375
3376         case R_CRIS_32_PLT_PCREL:
3377           /* This symbol requires a procedure linkage table entry.  We
3378              actually build the entry in adjust_dynamic_symbol,
3379              because this might be a case of linking PIC code which is
3380              never referenced by a dynamic object, in which case we
3381              don't need to generate a procedure linkage table entry
3382              after all.  */
3383
3384           /* Beware: if we'd check for visibility of the symbol here
3385              (and not marking the need for a PLT when non-visible), we'd
3386              get into trouble with keeping handling consistent with
3387              regards to relocs found before definition and GOTPLT
3388              handling.  Eliminable PLT entries will be dealt with later
3389              anyway.  */
3390           if (h == NULL)
3391             continue;
3392
3393           h->needs_plt = 1;
3394           h->plt.refcount++;
3395           break;
3396
3397         case R_CRIS_8:
3398         case R_CRIS_16:
3399         case R_CRIS_32:
3400           /* Let's help debug shared library creation.  Any of these
3401              relocs can be used in shared libs, but pages containing them
3402              cannot be shared.  Don't warn for sections we don't care
3403              about, such as debug sections or non-constant sections.  We
3404              can't help tables of (global) function pointers, for example,
3405              though they must be emitted in a data section to avoid having
3406              impure text sections.  */
3407           if (info->shared
3408               && (sec->flags & SEC_ALLOC) != 0
3409               && (sec->flags & SEC_READONLY) != 0)
3410             {
3411               /* FIXME: How do we make this optionally a warning only?  */
3412               (*_bfd_error_handler)
3413                 (_("%B, section %A:\n  relocation %s should not"
3414                    " be used in a shared object; recompile with -fPIC"),
3415                  abfd,
3416                  sec,
3417                  cris_elf_howto_table[r_type].name);
3418             }
3419
3420           /* Fall through.  */
3421
3422         case R_CRIS_8_PCREL:
3423         case R_CRIS_16_PCREL:
3424         case R_CRIS_32_PCREL:
3425           if (h != NULL)
3426             {
3427               h->non_got_ref = 1;
3428
3429               /* Make sure a plt entry is created for this symbol if it
3430                  turns out to be a function defined by a dynamic object.  */
3431               if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3432                 h->plt.refcount++;
3433             }
3434
3435           /* If we are creating a shared library and this is not a local
3436              symbol, we need to copy the reloc into the shared library.
3437              However when linking with -Bsymbolic and this is a global
3438              symbol which is defined in an object we are including in the
3439              link (i.e., DEF_REGULAR is set), then we can resolve the
3440              reloc directly.  At this point we have not seen all the input
3441              files, so it is possible that DEF_REGULAR is not set now but
3442              will be set later (it is never cleared).  In case of a weak
3443              definition, DEF_REGULAR may be cleared later by a strong
3444              definition in a shared library.  We account for that
3445              possibility below by storing information in the relocs_copied
3446              field of the hash table entry.  A similar situation occurs
3447              when creating shared libraries and symbol visibility changes
3448              render the symbol local.  */
3449
3450           /* No need to do anything if we're not creating a shared object.  */
3451           if (! info->shared)
3452             break;
3453
3454           /* We don't need to handle relocs into sections not going into
3455              the "real" output.  */
3456           if ((sec->flags & SEC_ALLOC) == 0)
3457             break;
3458
3459           /* We can only eliminate PC-relative relocs.  */
3460           if (r_type == R_CRIS_8_PCREL
3461               || r_type == R_CRIS_16_PCREL
3462               || r_type == R_CRIS_32_PCREL)
3463             {
3464               /* If the symbol is local, then we can eliminate the reloc.  */
3465               if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3466                 break;
3467
3468               /* If this is with -Bsymbolic and the symbol isn't weak, and
3469                  is defined by an ordinary object (the ones we include in
3470                  this shared library) then we can also eliminate the
3471                  reloc.  See comment above for more eliminable cases which
3472                  we can't identify at this time.  */
3473               if (info->symbolic
3474                   && h->root.type != bfd_link_hash_defweak
3475                   && h->def_regular)
3476                 break;
3477
3478               if ((sec->flags & SEC_READONLY) != 0)
3479                 {
3480                   /* FIXME: How do we make this optionally a warning only?  */
3481                   (*_bfd_error_handler)
3482                     (_("%B, section %A:\n  relocation %s should not be used"
3483                        " in a shared object; recompile with -fPIC"),
3484                      abfd,
3485                      sec,
3486                      cris_elf_howto_table[r_type].name);
3487                 }
3488             }
3489
3490           /* We may need to create a reloc section in the dynobj and made room
3491              for this reloc.  */
3492           if (sreloc == NULL)
3493             {
3494               sreloc = _bfd_elf_make_dynamic_reloc_section
3495                 (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
3496
3497               if (sreloc == NULL)
3498                 return FALSE;
3499
3500               if (sec->flags & SEC_READONLY)
3501                 info->flags |= DF_TEXTREL;
3502             }
3503
3504           sreloc->size += sizeof (Elf32_External_Rela);
3505
3506           /* If we are linking with -Bsymbolic, we count the number of PC
3507              relative relocations we have entered for this symbol, so that
3508              we can discard them again if the symbol is later defined by a
3509              regular object.  We know that h is really a pointer to an
3510              elf_cris_link_hash_entry.  */
3511           if ((r_type == R_CRIS_8_PCREL
3512                || r_type == R_CRIS_16_PCREL
3513                || r_type == R_CRIS_32_PCREL)
3514               && info->symbolic)
3515             {
3516               struct elf_cris_link_hash_entry *eh;
3517               struct elf_cris_pcrel_relocs_copied *p;
3518
3519               eh = elf_cris_hash_entry (h);
3520
3521               for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
3522                 if (p->section == sreloc)
3523                   break;
3524
3525               if (p == NULL)
3526                 {
3527                   p = ((struct elf_cris_pcrel_relocs_copied *)
3528                        bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
3529                   if (p == NULL)
3530                     return FALSE;
3531                   p->next = eh->pcrel_relocs_copied;
3532                   eh->pcrel_relocs_copied = p;
3533                   p->section = sreloc;
3534                   p->count = 0;
3535                 }
3536
3537               ++p->count;
3538             }
3539           break;
3540
3541         /* This relocation describes the C++ object vtable hierarchy.
3542            Reconstruct it for later use during GC.  */
3543         case R_CRIS_GNU_VTINHERIT:
3544           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3545             return FALSE;
3546           break;
3547
3548         /* This relocation describes which C++ vtable entries are actually
3549            used.  Record for later use during GC.  */
3550         case R_CRIS_GNU_VTENTRY:
3551           BFD_ASSERT (h != NULL);
3552           if (h != NULL
3553               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3554             return FALSE;
3555           break;
3556
3557         case R_CRIS_16_TPREL:
3558         case R_CRIS_32_TPREL:
3559           /* Already warned above, when necessary.  */
3560           break;
3561
3562         default:
3563           /* Other relocs do not appear here.  */
3564           bfd_set_error (bfd_error_bad_value);
3565           return FALSE;
3566         }
3567     }
3568
3569   return TRUE;
3570 }
3571
3572 /* Set the sizes of the dynamic sections.  */
3573
3574 static bfd_boolean
3575 elf_cris_size_dynamic_sections (output_bfd, info)
3576      bfd *output_bfd ATTRIBUTE_UNUSED;
3577      struct bfd_link_info *info;
3578 {
3579   bfd *dynobj;
3580   asection *s;
3581   bfd_boolean plt;
3582   bfd_boolean relocs;
3583
3584   dynobj = elf_hash_table (info)->dynobj;
3585   BFD_ASSERT (dynobj != NULL);
3586
3587   if (elf_hash_table (info)->dynamic_sections_created)
3588     {
3589       /* Set the contents of the .interp section to the interpreter.  */
3590       if (info->executable)
3591         {
3592           s = bfd_get_section_by_name (dynobj, ".interp");
3593           BFD_ASSERT (s != NULL);
3594           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3595           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3596         }
3597     }
3598   else
3599     {
3600       /* Adjust all expected GOTPLT uses to use a GOT entry instead.  */
3601       elf_cris_link_hash_traverse (elf_cris_hash_table (info),
3602                                    elf_cris_adjust_gotplt_to_got,
3603                                    (PTR) info);
3604
3605       /* We may have created entries in the .rela.got section.
3606          However, if we are not creating the dynamic sections, we will
3607          not actually use these entries.  Reset the size of .rela.got,
3608          which will cause it to get stripped from the output file
3609          below.  */
3610       s = bfd_get_section_by_name (dynobj, ".rela.got");
3611       if (s != NULL)
3612         s->size = 0;
3613     }
3614
3615   /* If this is a -Bsymbolic shared link, then we need to discard all PC
3616      relative relocs against symbols defined in a regular object.  We
3617      allocated space for them in the check_relocs routine, but we will not
3618      fill them in in the relocate_section routine.  We also discard space
3619      for relocs that have become for local symbols due to symbol
3620      visibility changes.  For programs, we discard space for relocs for
3621      symbols not referenced by any dynamic object.  */
3622   if (info->shared)
3623     elf_cris_link_hash_traverse (elf_cris_hash_table (info),
3624                                  elf_cris_discard_excess_dso_dynamics,
3625                                  (PTR) info);
3626   else
3627     elf_cris_link_hash_traverse (elf_cris_hash_table (info),
3628                                  elf_cris_discard_excess_program_dynamics,
3629                                  (PTR) info);
3630
3631   /* The check_relocs and adjust_dynamic_symbol entry points have
3632      determined the sizes of the various dynamic sections.  Allocate
3633      memory for them.  */
3634   plt = FALSE;
3635   relocs = FALSE;
3636   for (s = dynobj->sections; s != NULL; s = s->next)
3637     {
3638       const char *name;
3639
3640       if ((s->flags & SEC_LINKER_CREATED) == 0)
3641         continue;
3642
3643       /* It's OK to base decisions on the section name, because none
3644          of the dynobj section names depend upon the input files.  */
3645       name = bfd_get_section_name (dynobj, s);
3646
3647       if (strcmp (name, ".plt") == 0)
3648         {
3649           /* Remember whether there is a PLT.  */
3650           plt = s->size != 0;
3651         }
3652       else if (strcmp (name, ".got.plt") == 0)
3653         {
3654           /* The .got.plt contains the .got header as well as the
3655              actual .got.plt contents.  The .got header may contain a
3656              R_CRIS_DTPMOD entry at index 3.  */
3657           s->size += elf_cris_hash_table (info)->dtpmod_refcount != 0
3658             ? 8 : 0;
3659         }
3660       else if (CONST_STRNEQ (name, ".rela"))
3661         {
3662           if (strcmp (name, ".rela.got") == 0
3663               && elf_cris_hash_table (info)->dtpmod_refcount != 0
3664               && info->shared)
3665             s->size += sizeof (Elf32_External_Rela);
3666
3667           if (s->size != 0)
3668             {
3669               /* Remember whether there are any reloc sections other
3670                  than .rela.plt.  */
3671               if (strcmp (name, ".rela.plt") != 0)
3672                   relocs = TRUE;
3673
3674               /* We use the reloc_count field as a counter if we need
3675                  to copy relocs into the output file.  */
3676               s->reloc_count = 0;
3677             }
3678         }
3679       else if (! CONST_STRNEQ (name, ".got")
3680                && strcmp (name, ".dynbss") != 0)
3681         {
3682           /* It's not one of our sections, so don't allocate space.  */
3683           continue;
3684         }
3685
3686       if (s->size == 0)
3687         {
3688           /* If we don't need this section, strip it from the
3689              output file.  This is mostly to handle .rela.bss and
3690              .rela.plt.  We must create both sections in
3691              create_dynamic_sections, because they must be created
3692              before the linker maps input sections to output
3693              sections.  The linker does that before
3694              adjust_dynamic_symbol is called, and it is that
3695              function which decides whether anything needs to go
3696              into these sections.  */
3697           s->flags |= SEC_EXCLUDE;
3698           continue;
3699         }
3700
3701       if ((s->flags & SEC_HAS_CONTENTS) == 0)
3702         continue;
3703
3704       /* Allocate memory for the section contents. We use bfd_zalloc here
3705          in case unused entries are not reclaimed before the section's
3706          contents are written out.  This should not happen, but this way
3707          if it does, we will not write out garbage.  For reloc sections,
3708          this will make entries have the type R_CRIS_NONE.  */
3709       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3710       if (s->contents == NULL)
3711         return FALSE;
3712     }
3713
3714   if (elf_hash_table (info)->dynamic_sections_created)
3715     {
3716       /* Add some entries to the .dynamic section.  We fill in the
3717          values later, in elf_cris_finish_dynamic_sections, but we
3718          must add the entries now so that we get the correct size for
3719          the .dynamic section.  The DT_DEBUG entry is filled in by the
3720          dynamic linker and used by the debugger.  */
3721 #define add_dynamic_entry(TAG, VAL) \
3722   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3723
3724       if (!info->shared)
3725         {
3726           if (!add_dynamic_entry (DT_DEBUG, 0))
3727             return FALSE;
3728         }
3729
3730       if (plt)
3731         {
3732           if (!add_dynamic_entry (DT_PLTGOT, 0)
3733               || !add_dynamic_entry (DT_PLTRELSZ, 0)
3734               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3735               || !add_dynamic_entry (DT_JMPREL, 0))
3736             return FALSE;
3737         }
3738
3739       if (relocs)
3740         {
3741           if (!add_dynamic_entry (DT_RELA, 0)
3742               || !add_dynamic_entry (DT_RELASZ, 0)
3743               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3744             return FALSE;
3745         }
3746
3747       if ((info->flags & DF_TEXTREL) != 0)
3748         {
3749           if (!add_dynamic_entry (DT_TEXTREL, 0))
3750             return FALSE;
3751           info->flags |= DF_TEXTREL;
3752         }
3753     }
3754 #undef add_dynamic_entry
3755
3756   return TRUE;
3757 }
3758
3759 /* This function is called via elf_cris_link_hash_traverse if we are
3760    creating a shared object.  In the -Bsymbolic case, it discards the
3761    space allocated to copy PC relative relocs against symbols which
3762    are defined in regular objects.  For the normal non-symbolic case,
3763    we also discard space for relocs that have become local due to
3764    symbol visibility changes.  We allocated space for them in the
3765    check_relocs routine, but we won't fill them in in the
3766    relocate_section routine.  */
3767
3768 static bfd_boolean
3769 elf_cris_discard_excess_dso_dynamics (h, inf)
3770      struct elf_cris_link_hash_entry *h;
3771      PTR inf;
3772 {
3773   struct elf_cris_pcrel_relocs_copied *s;
3774   struct bfd_link_info *info = (struct bfd_link_info *) inf;
3775
3776   if (h->root.root.type == bfd_link_hash_warning)
3777     h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
3778
3779   /* If a symbol has been forced local or we have found a regular
3780      definition for the symbolic link case, then we won't be needing
3781      any relocs.  */
3782   if (h->root.def_regular
3783       && (h->root.forced_local
3784           || info->symbolic))
3785     {
3786       for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
3787         s->section->size -= s->count * sizeof (Elf32_External_Rela);
3788     }
3789
3790   return TRUE;
3791 }
3792
3793 /* This function is called via elf_cris_link_hash_traverse if we are *not*
3794    creating a shared object.  We discard space for relocs for symbols put
3795    in the .got, but which we found we do not have to resolve at run-time.  */
3796
3797 static bfd_boolean
3798 elf_cris_discard_excess_program_dynamics (h, inf)
3799      struct elf_cris_link_hash_entry *h;
3800      PTR inf;
3801 {
3802   struct bfd_link_info *info = (struct bfd_link_info *) inf;
3803
3804   if (h->root.root.type == bfd_link_hash_warning)
3805     h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
3806
3807   /* If we're not creating a shared library and have a symbol which is
3808      referred to by .got references, but the symbol is defined locally,
3809      (or rather, not defined by a DSO) then lose the reloc for the .got
3810      (don't allocate room for it).  Likewise for relocs for something
3811      for which we create a PLT.  */
3812   if (!h->root.def_dynamic
3813       || h->root.plt.refcount > 0)
3814     {
3815       if (h->reg_got_refcount > 0
3816           /* The size of this section is only valid and in sync with the
3817              various reference counts if we do dynamic; don't decrement it
3818              otherwise.  */
3819           && elf_hash_table (info)->dynamic_sections_created)
3820         {
3821           bfd *dynobj = elf_hash_table (info)->dynobj;
3822           asection *srelgot;
3823
3824           BFD_ASSERT (dynobj != NULL);
3825
3826           srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3827
3828           BFD_ASSERT (srelgot != NULL);
3829
3830           srelgot->size -= sizeof (Elf32_External_Rela);
3831         }
3832
3833       /* If the locally-defined symbol isn't used by a DSO, then we don't
3834          have to export it as a dynamic symbol.  This was already done for
3835          functions; doing this for all symbols would presumably not
3836          introduce new problems.  Of course we don't do this if we're
3837          exporting all dynamic symbols.  */
3838       if (! info->export_dynamic
3839           && h->root.dynindx != -1
3840           && !h->root.def_dynamic
3841           && !h->root.ref_dynamic)
3842         {
3843           h->root.dynindx = -1;
3844           _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3845                                   h->root.dynstr_index);
3846         }
3847     }
3848
3849   return TRUE;
3850 }
3851
3852 /* Reject a file depending on presence and expectation of prefixed
3853    underscores on symbols.  */
3854
3855 static bfd_boolean
3856 cris_elf_object_p (abfd)
3857      bfd *abfd;
3858 {
3859   if (! cris_elf_set_mach_from_flags (abfd, elf_elfheader (abfd)->e_flags))
3860     return FALSE;
3861
3862   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
3863     return (bfd_get_symbol_leading_char (abfd) == '_');
3864   else
3865     return (bfd_get_symbol_leading_char (abfd) == 0);
3866 }
3867
3868 /* Mark presence or absence of leading underscore.  Set machine type
3869    flags from mach type.  */
3870
3871 static void
3872 cris_elf_final_write_processing (abfd, linker)
3873      bfd *abfd;
3874      bfd_boolean linker ATTRIBUTE_UNUSED;
3875 {
3876   unsigned long e_flags = elf_elfheader (abfd)->e_flags;
3877
3878   e_flags &= ~EF_CRIS_UNDERSCORE;
3879   if (bfd_get_symbol_leading_char (abfd) == '_')
3880     e_flags |= EF_CRIS_UNDERSCORE;
3881
3882   switch (bfd_get_mach (abfd))
3883     {
3884     case bfd_mach_cris_v0_v10:
3885       e_flags |= EF_CRIS_VARIANT_ANY_V0_V10;
3886       break;
3887
3888     case bfd_mach_cris_v10_v32:
3889       e_flags |= EF_CRIS_VARIANT_COMMON_V10_V32;
3890       break;
3891
3892     case bfd_mach_cris_v32:
3893       e_flags |= EF_CRIS_VARIANT_V32;
3894       break;
3895
3896     default:
3897       _bfd_abort (__FILE__, __LINE__,
3898                   _("Unexpected machine number"));
3899     }
3900
3901   elf_elfheader (abfd)->e_flags = e_flags;
3902 }
3903
3904 /* Set the mach type from e_flags value.  */
3905
3906 static bfd_boolean
3907 cris_elf_set_mach_from_flags (abfd, flags)
3908      bfd *abfd;
3909      unsigned long flags;
3910 {
3911   switch (flags & EF_CRIS_VARIANT_MASK)
3912     {
3913     case EF_CRIS_VARIANT_ANY_V0_V10:
3914       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v0_v10);
3915       break;
3916
3917     case EF_CRIS_VARIANT_V32:
3918       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v32);
3919       break;
3920
3921     case EF_CRIS_VARIANT_COMMON_V10_V32:
3922       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v10_v32);
3923       break;
3924
3925     default:
3926       /* Since we don't recognize them, we obviously can't support them
3927          with this code; we'd have to require that all future handling
3928          would be optional.  */
3929       bfd_set_error (bfd_error_wrong_format);
3930       return FALSE;
3931     }
3932
3933   return TRUE;
3934 }
3935
3936 /* Display the flags field.  */
3937
3938 static bfd_boolean
3939 cris_elf_print_private_bfd_data (abfd, ptr)
3940      bfd *abfd;
3941      PTR ptr;
3942 {
3943   FILE *file = (FILE *) ptr;
3944
3945   BFD_ASSERT (abfd != NULL && ptr != NULL);
3946
3947   _bfd_elf_print_private_bfd_data (abfd, ptr);
3948
3949   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
3950
3951   if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
3952     fprintf (file, _(" [symbols have a _ prefix]"));
3953   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
3954       == EF_CRIS_VARIANT_COMMON_V10_V32)
3955     fprintf (file, _(" [v10 and v32]"));
3956   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
3957       == EF_CRIS_VARIANT_V32)
3958     fprintf (file, _(" [v32]"));
3959
3960   fputc ('\n', file);
3961   return TRUE;
3962 }
3963
3964 /* Don't mix files with and without a leading underscore.  */
3965
3966 static bfd_boolean
3967 cris_elf_merge_private_bfd_data (ibfd, obfd)
3968      bfd *ibfd;
3969      bfd *obfd;
3970 {
3971   int imach, omach;
3972
3973   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3974     return FALSE;
3975
3976   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3977       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3978     return TRUE;
3979
3980   imach = bfd_get_mach (ibfd);
3981
3982   if (! elf_flags_init (obfd))
3983     {
3984       /* This happens when ld starts out with a 'blank' output file.  */
3985       elf_flags_init (obfd) = TRUE;
3986
3987       /* We ignore the linker-set mach, and instead set it according to
3988          the first input file.  This would also happen if we could
3989          somehow filter out the OUTPUT_ARCH () setting from elf.sc.
3990          This allows us to keep the same linker config across
3991          cris(v0..v10) and crisv32.  The drawback is that we can't force
3992          the output type, which might be a sane thing to do for a
3993          v10+v32 compatibility object.  */
3994       if (! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
3995         return FALSE;
3996     }
3997
3998   if (bfd_get_symbol_leading_char (ibfd)
3999       != bfd_get_symbol_leading_char (obfd))
4000     {
4001       (*_bfd_error_handler)
4002         (bfd_get_symbol_leading_char (ibfd) == '_'
4003          ? _("%B: uses _-prefixed symbols, but writing file with non-prefixed symbols")
4004          : _("%B: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
4005          ibfd);
4006       bfd_set_error (bfd_error_bad_value);
4007       return FALSE;
4008     }
4009
4010   omach = bfd_get_mach (obfd);
4011
4012   if (imach != omach)
4013     {
4014       /* We can get an incompatible combination only if either is
4015          bfd_mach_cris_v32, and the other one isn't compatible.  */
4016       if ((imach == bfd_mach_cris_v32
4017            && omach != bfd_mach_cris_v10_v32)
4018           || (omach == bfd_mach_cris_v32
4019               && imach != bfd_mach_cris_v10_v32))
4020         {
4021           (*_bfd_error_handler)
4022             ((imach == bfd_mach_cris_v32)
4023              ? _("%B contains CRIS v32 code, incompatible"
4024                  " with previous objects")
4025              : _("%B contains non-CRIS-v32 code, incompatible"
4026                  " with previous objects"),
4027              ibfd);
4028           bfd_set_error (bfd_error_bad_value);
4029           return FALSE;
4030         }
4031
4032       /* We don't have to check the case where the input is compatible
4033          with v10 and v32, because the output is already known to be set
4034          to the other (compatible) mach.  */
4035       if (omach == bfd_mach_cris_v10_v32
4036           && ! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
4037         return FALSE;
4038     }
4039
4040   return TRUE;
4041 }
4042
4043 /* Do side-effects of e_flags copying to obfd.  */
4044
4045 static bfd_boolean
4046 cris_elf_copy_private_bfd_data (ibfd, obfd)
4047      bfd *ibfd;
4048      bfd *obfd;
4049 {
4050   /* Call the base function.  */
4051   if (!_bfd_elf_copy_private_bfd_data (ibfd, obfd))
4052     return FALSE;
4053
4054   /* If output is big-endian for some obscure reason, stop here.  */
4055   if (_bfd_generic_verify_endian_match (ibfd, obfd) == FALSE)
4056     return FALSE;
4057
4058   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4059       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4060     return TRUE;
4061
4062   /* Do what we really came here for.  */
4063   return bfd_set_arch_mach (obfd, bfd_arch_cris, bfd_get_mach (ibfd));
4064 }
4065
4066 static enum elf_reloc_type_class
4067 elf_cris_reloc_type_class (rela)
4068      const Elf_Internal_Rela *rela;
4069 {
4070   enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rela->r_info);
4071   switch (r_type)
4072     {
4073     case R_CRIS_RELATIVE:
4074       return reloc_class_relative;
4075     case R_CRIS_JUMP_SLOT:
4076       return reloc_class_plt;
4077     case R_CRIS_COPY:
4078       return reloc_class_copy;
4079     default:
4080       return reloc_class_normal;
4081     }
4082 }
4083
4084 /* The elf_backend_got_elt_size worker.  For one symbol, we can have up to
4085    two GOT entries from three types with two different sizes.  We handle
4086    it as a single entry, so we can use the regular offset-calculation
4087    machinery.  */
4088
4089 static bfd_vma
4090 elf_cris_got_elt_size (bfd *abfd ATTRIBUTE_UNUSED,
4091                        struct bfd_link_info *info ATTRIBUTE_UNUSED,
4092                        struct elf_link_hash_entry *hr,
4093                        bfd *ibfd,
4094                        unsigned long symndx)
4095 {
4096   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) hr;
4097   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4098   bfd_vma eltsiz = 0;
4099
4100   /* We may have one regular GOT entry or up to two TLS GOT
4101      entries.  */
4102   if (h == NULL)
4103     {
4104       bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (ibfd);
4105
4106       BFD_ASSERT (local_got_refcounts != NULL);
4107
4108       if (local_got_refcounts[LGOT_REG_NDX (symndx)] > 0)
4109         {
4110           /* We can't have a variable referred to both as a regular
4111              variable and through TLS relocs.  */
4112           BFD_ASSERT (local_got_refcounts[LGOT_DTP_NDX (symndx)] == 0
4113                       && local_got_refcounts[LGOT_TPREL_NDX (symndx)] == 0);
4114           return 4;
4115         }
4116
4117       if (local_got_refcounts[LGOT_DTP_NDX (symndx)] > 0)
4118         eltsiz += 8;
4119
4120       if (local_got_refcounts[LGOT_TPREL_NDX (symndx)] > 0)
4121         eltsiz += 4;
4122     }
4123   else
4124     {
4125       struct elf_cris_link_hash_entry *hh = elf_cris_hash_entry (h);
4126       if (hh->reg_got_refcount > 0)
4127         {
4128           /* The actual error-on-input is emitted elsewhere.  */
4129           BFD_ASSERT (hh->dtp_refcount == 0 && hh->tprel_refcount == 0);
4130           return 4;
4131         }
4132
4133       if (hh->dtp_refcount > 0)
4134         eltsiz += 8;
4135
4136       if (hh->tprel_refcount > 0)
4137         eltsiz += 4;
4138     }
4139
4140   return eltsiz;
4141 }
4142 \f
4143 #define ELF_ARCH                bfd_arch_cris
4144 #define ELF_MACHINE_CODE        EM_CRIS
4145 #define ELF_MAXPAGESIZE         0x2000
4146
4147 #define TARGET_LITTLE_SYM       bfd_elf32_cris_vec
4148 #define TARGET_LITTLE_NAME      "elf32-cris"
4149 #define elf_symbol_leading_char 0
4150
4151 #define elf_info_to_howto_rel                   NULL
4152 #define elf_info_to_howto                       cris_info_to_howto_rela
4153 #define elf_backend_relocate_section            cris_elf_relocate_section
4154 #define elf_backend_gc_mark_hook                cris_elf_gc_mark_hook
4155 #define elf_backend_gc_sweep_hook               cris_elf_gc_sweep_hook
4156 #define elf_backend_check_relocs                cris_elf_check_relocs
4157 #define elf_backend_grok_prstatus               cris_elf_grok_prstatus
4158 #define elf_backend_grok_psinfo                 cris_elf_grok_psinfo
4159
4160 #define elf_backend_can_gc_sections             1
4161 #define elf_backend_can_refcount                1
4162
4163 #define elf_backend_object_p                    cris_elf_object_p
4164 #define elf_backend_final_write_processing \
4165         cris_elf_final_write_processing
4166 #define bfd_elf32_bfd_print_private_bfd_data \
4167         cris_elf_print_private_bfd_data
4168 #define bfd_elf32_bfd_merge_private_bfd_data \
4169         cris_elf_merge_private_bfd_data
4170 #define bfd_elf32_bfd_copy_private_bfd_data \
4171         cris_elf_copy_private_bfd_data
4172
4173 #define bfd_elf32_bfd_reloc_type_lookup         cris_reloc_type_lookup
4174 #define bfd_elf32_bfd_reloc_name_lookup cris_reloc_name_lookup
4175
4176 #define bfd_elf32_bfd_link_hash_table_create \
4177         elf_cris_link_hash_table_create
4178 #define elf_backend_adjust_dynamic_symbol \
4179         elf_cris_adjust_dynamic_symbol
4180 #define elf_backend_size_dynamic_sections \
4181         elf_cris_size_dynamic_sections
4182 #define elf_backend_init_index_section          _bfd_elf_init_1_index_section
4183 #define elf_backend_finish_dynamic_symbol \
4184         elf_cris_finish_dynamic_symbol
4185 #define elf_backend_finish_dynamic_sections \
4186         elf_cris_finish_dynamic_sections
4187 #define elf_backend_create_dynamic_sections \
4188         _bfd_elf_create_dynamic_sections
4189 #define bfd_elf32_bfd_final_link \
4190         bfd_elf_gc_common_final_link
4191 #define elf_backend_hide_symbol                 elf_cris_hide_symbol
4192 #define elf_backend_reloc_type_class            elf_cris_reloc_type_class
4193
4194 #define elf_backend_want_got_plt        1
4195 #define elf_backend_plt_readonly        1
4196 #define elf_backend_want_plt_sym        0
4197 #define elf_backend_got_header_size     12
4198 #define elf_backend_got_elt_size elf_cris_got_elt_size
4199
4200 /* Later, we my want to optimize RELA entries into REL entries for dynamic
4201    linking and libraries (if it's a win of any significance).  Until then,
4202    take the easy route.  */
4203 #define elf_backend_may_use_rel_p 0
4204 #define elf_backend_may_use_rela_p 1
4205 #define elf_backend_rela_normal         1
4206
4207 #include "elf32-target.h"
4208
4209 #undef TARGET_LITTLE_SYM
4210 #undef TARGET_LITTLE_NAME
4211 #undef elf_symbol_leading_char
4212
4213 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
4214 #define TARGET_LITTLE_NAME "elf32-us-cris"
4215 #define elf_symbol_leading_char '_'
4216 #undef elf32_bed
4217 #define elf32_bed elf32_us_cris_bed
4218
4219 #include "elf32-target.h"