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