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