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