779375416c673d46415d56984b6b37ff14eacf7a
[external/binutils.git] / bfd / elf32-score.c
1 /* 32-bit ELF support for S+core.
2    Copyright 2006 Free Software Foundation, Inc.
3    Contributed by
4    Mei Ligang (ligang@sunnorth.com.cn)
5    Pei-Lin Tsai (pltsai@sunplus.com)
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "libiberty.h"
27 #include "elf-bfd.h"
28 #include "elf/score.h"
29 #include "elf/common.h"
30 #include "elf/internal.h"
31 #include "hashtab.h"
32
33
34 /* Score ELF linker hash table.  */
35
36 struct score_elf_link_hash_table
37 {
38   /* The main hash table.  */
39   struct elf_link_hash_table root;
40 };
41
42 /* The SCORE ELF linker needs additional information for each symbol in
43    the global hash table.  */
44
45 struct score_elf_link_hash_entry
46 {
47   struct elf_link_hash_entry root;
48
49   /* Number of R_SCORE_ABS32, R_SCORE_REL32 relocs against this symbol.  */
50   unsigned int possibly_dynamic_relocs;
51
52   /* If the R_SCORE_ABS32, R_SCORE_REL32 reloc is against a readonly section.  */
53   bfd_boolean readonly_reloc;
54
55   /* We must not create a stub for a symbol that has relocations related to
56      taking the function's address, i.e. any but R_SCORE_CALL15 ones.  */
57   bfd_boolean no_fn_stub;
58
59   /* Are we forced local?  This will only be set if we have converted
60      the initial global GOT entry to a local GOT entry.  */
61   bfd_boolean forced_local;
62 };
63
64 /* Traverse a score ELF linker hash table.  */
65 #define score_elf_link_hash_traverse(table, func, info) \
66   (elf_link_hash_traverse \
67    (&(table)->root, \
68     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
69     (info)))
70
71 /* Get the SCORE elf linker hash table from a link_info structure.  */
72 #define score_elf_hash_table(info) \
73   ((struct score_elf_link_hash_table *) ((info)->hash))
74
75 /* This structure is used to hold .got entries while estimating got sizes.  */
76 struct score_got_entry
77 {
78   /* The input bfd in which the symbol is defined.  */
79   bfd *abfd;
80   /* The index of the symbol, as stored in the relocation r_info, if
81      we have a local symbol; -1 otherwise.  */
82   long symndx;
83   union
84   {
85     /* If abfd == NULL, an address that must be stored in the got.  */
86     bfd_vma address;
87     /* If abfd != NULL && symndx != -1, the addend of the relocation
88        that should be added to the symbol value.  */
89     bfd_vma addend;
90     /* If abfd != NULL && symndx == -1, the hash table entry
91        corresponding to a global symbol in the got (or, local, if
92        h->forced_local).  */
93     struct score_elf_link_hash_entry *h;
94   } d;
95
96   /* The offset from the beginning of the .got section to the entry
97      corresponding to this symbol+addend.  If it's a global symbol
98      whose offset is yet to be decided, it's going to be -1.  */
99   long gotidx;
100 };
101
102 /* This structure is passed to score_elf_sort_hash_table_f when sorting
103    the dynamic symbols.  */
104
105 struct score_elf_hash_sort_data
106 {
107   /* The symbol in the global GOT with the lowest dynamic symbol table index.  */
108   struct elf_link_hash_entry *low;
109   /* The least dynamic symbol table index corresponding to a symbol with a GOT entry.  */
110   long min_got_dynindx;
111   /* The greatest dynamic symbol table index corresponding to a symbol
112      with a GOT entry that is not referenced (e.g., a dynamic symbol
113      with dynamic relocations pointing to it from non-primary GOTs).  */
114   long max_unref_got_dynindx;
115   /* The greatest dynamic symbol table index not corresponding to a
116      symbol without a GOT entry.  */
117   long max_non_got_dynindx;
118 };
119
120 struct score_got_info
121 {
122   /* The global symbol in the GOT with the lowest index in the dynamic
123      symbol table.  */
124   struct elf_link_hash_entry *global_gotsym;
125   /* The number of global .got entries.  */
126   unsigned int global_gotno;
127   /* The number of local .got entries.  */
128   unsigned int local_gotno;
129   /* The number of local .got entries we have used.  */
130   unsigned int assigned_gotno;
131   /* A hash table holding members of the got.  */
132   struct htab *got_entries;
133   /* In multi-got links, a pointer to the next got (err, rather, most
134      of the time, it points to the previous got).  */
135   struct score_got_info *next;
136 };
137
138 /* A structure used to count GOT entries, for GOT entry or ELF symbol table traversal.  */
139 struct _score_elf_section_data
140 {
141   struct bfd_elf_section_data elf;
142   union
143   {
144     struct score_got_info *got_info;
145     bfd_byte *tdata;
146   }
147   u;
148 };
149
150 #define score_elf_section_data(sec) \
151   ((struct _score_elf_section_data *) elf_section_data (sec))
152
153 /* The size of a symbol-table entry.  */
154 #define SCORE_ELF_SYM_SIZE(abfd)  \
155   (get_elf_backend_data (abfd)->s->sizeof_sym)
156
157 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
158    from smaller values.  Start with zero, widen, *then* decrement.  */
159 #define MINUS_ONE (((bfd_vma)0) - 1)
160 #define MINUS_TWO (((bfd_vma)0) - 2)
161
162 #define PDR_SIZE 32
163
164
165 /* The number of local .got entries we reserve.  */
166 #define SCORE_RESERVED_GOTNO (2)
167 #define ELF_DYNAMIC_INTERPRETER     "/usr/lib/ld.so.1"
168
169 /* The offset of $gp from the beginning of the .got section.  */
170 #define ELF_SCORE_GP_OFFSET(abfd) (0x3ff0)
171 /* The maximum size of the GOT for it to be addressable using 15-bit offsets from $gp.  */
172 #define SCORE_ELF_GOT_MAX_SIZE(abfd) (ELF_SCORE_GP_OFFSET(abfd) + 0x3fff)
173
174 #define SCORE_ELF_STUB_SECTION_NAME  (".SCORE.stub")
175 #define SCORE_FUNCTION_STUB_SIZE (16)
176
177 #define STUB_LW      0xc3bcc010     /* lw r29, [r28, -0x3ff0]  */
178 #define STUB_MOVE    0x8363bc56     /* mv r27, r3  */
179 #define STUB_LI16    0x87548000     /* ori r26, .dynsym_index  */
180 #define STUB_BRL     0x801dbc09     /* brl r29  */
181
182 #define SCORE_ELF_GOT_SIZE(abfd)   \
183   (get_elf_backend_data (abfd)->s->arch_size / 8)
184
185 #define SCORE_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
186         (_bfd_elf_add_dynamic_entry (info, (bfd_vma) tag, (bfd_vma) val))
187
188 /* The size of an external dynamic table entry.  */
189 #define SCORE_ELF_DYN_SIZE(abfd) \
190   (get_elf_backend_data (abfd)->s->sizeof_dyn)
191
192 /* The size of an external REL relocation.  */
193 #define SCORE_ELF_REL_SIZE(abfd) \
194   (get_elf_backend_data (abfd)->s->sizeof_rel)
195
196 /* The default alignment for sections, as a power of two.  */
197 #define SCORE_ELF_LOG_FILE_ALIGN(abfd)\
198   (get_elf_backend_data (abfd)->s->log_file_align)
199
200 #ifndef NUM_ELEM
201 #define NUM_ELEM(a)  (sizeof (a) / (sizeof (a)[0]))
202 #endif
203
204 static bfd_byte *hi16_rel_addr;
205
206 /* This will be used when we sort the dynamic relocation records.  */
207 static bfd *reldyn_sorting_bfd;
208
209 /* SCORE ELF uses two common sections.  One is the usual one, and the
210    other is for small objects.  All the small objects are kept
211    together, and then referenced via the gp pointer, which yields
212    faster assembler code.  This is what we use for the small common
213    section.  This approach is copied from ecoff.c.  */
214 static asection score_elf_scom_section;
215 static asymbol  score_elf_scom_symbol;
216 static asymbol  *score_elf_scom_symbol_ptr;
217
218 static bfd_reloc_status_type
219 score_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED,
220                       arelent *reloc_entry,
221                       asymbol *symbol ATTRIBUTE_UNUSED,
222                       void * data,
223                       asection *input_section ATTRIBUTE_UNUSED,
224                       bfd *output_bfd ATTRIBUTE_UNUSED,
225                       char **error_message ATTRIBUTE_UNUSED)
226 {
227   hi16_rel_addr = (bfd_byte *) data + reloc_entry->address;
228   return bfd_reloc_ok;
229 }
230
231 static bfd_reloc_status_type
232 score_elf_lo16_reloc (bfd *abfd,
233                       arelent *reloc_entry,
234                       asymbol *symbol ATTRIBUTE_UNUSED,
235                       void * data,
236                       asection *input_section,
237                       bfd *output_bfd ATTRIBUTE_UNUSED,
238                       char **error_message ATTRIBUTE_UNUSED)
239 {
240   bfd_vma addend = 0, offset = 0;
241   unsigned long val;
242   unsigned long hi16_offset, hi16_value, uvalue;
243
244   hi16_value = bfd_get_32 (abfd, hi16_rel_addr);
245   hi16_offset = ((((hi16_value >> 16) & 0x3) << 15) | (hi16_value & 0x7fff)) >> 1;
246   addend = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
247   offset = ((((addend >> 16) & 0x3) << 15) | (addend & 0x7fff)) >> 1;
248   val = reloc_entry->addend;
249   if (reloc_entry->address > input_section->size)
250     return bfd_reloc_outofrange;
251   uvalue = ((hi16_offset << 16) | (offset & 0xffff)) + val;
252   hi16_offset = (uvalue >> 16) << 1;
253   hi16_value = (hi16_value & ~0x37fff) | (hi16_offset & 0x7fff) | ((hi16_offset << 1) & 0x30000);
254   bfd_put_32 (abfd, hi16_value, hi16_rel_addr);
255   offset = (uvalue & 0xffff) << 1;
256   addend = (addend & ~0x37fff) | (offset & 0x7fff) | ((offset << 1) & 0x30000);
257   bfd_put_32 (abfd, addend, (bfd_byte *) data + reloc_entry->address);
258   return bfd_reloc_ok;
259 }
260
261 /* Set the GP value for OUTPUT_BFD.  Returns FALSE if this is a
262    dangerous relocation.  */
263
264 static bfd_boolean
265 score_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp)
266 {
267   unsigned int count;
268   asymbol **sym;
269   unsigned int i;
270
271   /* If we've already figured out what GP will be, just return it.  */
272   *pgp = _bfd_get_gp_value (output_bfd);
273   if (*pgp)
274     return TRUE;
275
276   count = bfd_get_symcount (output_bfd);
277   sym = bfd_get_outsymbols (output_bfd);
278
279   /* The linker script will have created a symbol named `_gp' with the
280      appropriate value.  */
281   if (sym == NULL)
282     i = count;
283   else
284     {
285       for (i = 0; i < count; i++, sym++)
286         {
287           const char *name;
288
289           name = bfd_asymbol_name (*sym);
290           if (*name == '_' && strcmp (name, "_gp") == 0)
291             {
292               *pgp = bfd_asymbol_value (*sym);
293               _bfd_set_gp_value (output_bfd, *pgp);
294               break;
295             }
296         }
297     }
298
299   if (i >= count)
300     {
301       /* Only get the error once.  */
302       *pgp = 4;
303       _bfd_set_gp_value (output_bfd, *pgp);
304       return FALSE;
305     }
306
307   return TRUE;
308 }
309
310 /* We have to figure out the gp value, so that we can adjust the
311    symbol value correctly.  We look up the symbol _gp in the output
312    BFD.  If we can't find it, we're stuck.  We cache it in the ELF
313    target data.  We don't need to adjust the symbol value for an
314    external symbol if we are producing relocatable output.  */
315
316 static bfd_reloc_status_type
317 score_elf_final_gp (bfd *output_bfd,
318                     asymbol *symbol,
319                     bfd_boolean relocatable,
320                     char **error_message,
321                     bfd_vma *pgp)
322 {
323   if (bfd_is_und_section (symbol->section)
324       && ! relocatable)
325     {
326       *pgp = 0;
327       return bfd_reloc_undefined;
328     }
329
330   *pgp = _bfd_get_gp_value (output_bfd);
331   if (*pgp == 0
332       && (! relocatable
333           || (symbol->flags & BSF_SECTION_SYM) != 0))
334     {
335       if (relocatable)
336         {
337           /* Make up a value.  */
338           *pgp = symbol->section->output_section->vma + 0x4000;
339           _bfd_set_gp_value (output_bfd, *pgp);
340         }
341       else if (!score_elf_assign_gp (output_bfd, pgp))
342         {
343             *error_message =
344               (char *) _("GP relative relocation when _gp not defined");
345             return bfd_reloc_dangerous;
346         }
347     }
348
349   return bfd_reloc_ok;
350 }
351
352 static bfd_reloc_status_type
353 score_elf_gprel15_with_gp (bfd *abfd,
354                            asymbol *symbol,
355                            arelent *reloc_entry,
356                            asection *input_section,
357                            bfd_boolean relocateable,
358                            void * data,
359                            bfd_vma gp ATTRIBUTE_UNUSED)
360 {
361   bfd_vma relocation;
362   unsigned long insn;
363
364   if (bfd_is_com_section (symbol->section))
365     relocation = 0;
366   else
367     relocation = symbol->value;
368
369   relocation += symbol->section->output_section->vma;
370   relocation += symbol->section->output_offset;
371   if (reloc_entry->address > input_section->size)
372     return bfd_reloc_outofrange;
373
374   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
375   if (((reloc_entry->addend & 0xffffc000) != 0)
376       && ((reloc_entry->addend & 0xffffc000) != 0xffffc000))
377     return bfd_reloc_overflow;
378
379   insn = (insn & ~0x7fff) | (reloc_entry->addend & 0x7fff);
380   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
381   if (relocateable)
382     reloc_entry->address += input_section->output_offset;
383
384   return bfd_reloc_ok;
385 }
386
387 static bfd_reloc_status_type
388 gprel32_with_gp (bfd *abfd, asymbol *symbol, arelent *reloc_entry,
389                  asection *input_section, bfd_boolean relocatable,
390                  void *data, bfd_vma gp)
391 {
392   bfd_vma relocation;
393   bfd_vma val;
394
395   if (bfd_is_com_section (symbol->section))
396     relocation = 0;
397   else
398     relocation = symbol->value;
399
400   relocation += symbol->section->output_section->vma;
401   relocation += symbol->section->output_offset;
402
403   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
404     return bfd_reloc_outofrange;
405
406   /* Set val to the offset into the section or symbol.  */
407   val = reloc_entry->addend;
408
409   if (reloc_entry->howto->partial_inplace)
410     val += bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
411
412   /* Adjust val for the final section location and GP value.  If we
413      are producing relocatable output, we don't want to do this for
414      an external symbol.  */
415   if (! relocatable
416       || (symbol->flags & BSF_SECTION_SYM) != 0)
417     val += relocation - gp;
418
419   if (reloc_entry->howto->partial_inplace)
420     bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
421   else
422     reloc_entry->addend = val;
423
424   if (relocatable)
425     reloc_entry->address += input_section->output_offset;
426
427   return bfd_reloc_ok;
428 }
429
430 static bfd_reloc_status_type
431 score_elf_gprel15_reloc (bfd *abfd,
432                          arelent *reloc_entry,
433                          asymbol *symbol,
434                          void * data,
435                          asection *input_section,
436                          bfd *output_bfd,
437                          char **error_message)
438 {
439   bfd_boolean relocateable;
440   bfd_reloc_status_type ret;
441   bfd_vma gp;
442
443   if (output_bfd != (bfd *) NULL
444       && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
445     {
446       reloc_entry->address += input_section->output_offset;
447       return bfd_reloc_ok;
448     }
449   if (output_bfd != (bfd *) NULL)
450     relocateable = TRUE;
451   else
452     {
453       relocateable = FALSE;
454       output_bfd = symbol->section->output_section->owner;
455     }
456
457   ret = score_elf_final_gp (output_bfd, symbol, relocateable, error_message, &gp);
458   if (ret != bfd_reloc_ok)
459     return ret;
460
461   return score_elf_gprel15_with_gp (abfd, symbol, reloc_entry,
462                                          input_section, relocateable, data, gp);
463 }
464
465 /* Do a R_SCORE_GPREL32 relocation.  This is a 32 bit value which must
466    become the offset from the gp register.  */
467
468 static bfd_reloc_status_type
469 score_elf_gprel32_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
470                         void *data, asection *input_section, bfd *output_bfd,
471                         char **error_message)
472 {
473   bfd_boolean relocatable;
474   bfd_reloc_status_type ret;
475   bfd_vma gp;
476
477   /* R_SCORE_GPREL32 relocations are defined for local symbols only.  */
478   if (output_bfd != NULL
479       && (symbol->flags & BSF_SECTION_SYM) == 0
480       && (symbol->flags & BSF_LOCAL) != 0)
481     {
482       *error_message = (char *)
483         _("32bits gp relative relocation occurs for an external symbol");
484       return bfd_reloc_outofrange;
485     }
486
487   if (output_bfd != NULL)
488     relocatable = TRUE;
489   else
490     {
491       relocatable = FALSE;
492       output_bfd = symbol->section->output_section->owner;
493     }
494
495   ret = score_elf_final_gp (output_bfd, symbol, relocatable, error_message, &gp);
496   if (ret != bfd_reloc_ok)
497     return ret;
498
499   gp = 0;   /* FIXME.  */
500   return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
501                           relocatable, data, gp);
502 }
503
504 /* A howto special_function for R_SCORE_GOT15 relocations.  This is just
505    like any other 16-bit relocation when applied to global symbols, but is
506    treated in the same as R_SCORE_HI16 when applied to local symbols.  */
507
508 static bfd_reloc_status_type
509 score_elf_got15_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
510                        void *data, asection *input_section,
511                        bfd *output_bfd, char **error_message)
512 {
513   if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
514       || bfd_is_und_section (bfd_get_section (symbol))
515       || bfd_is_com_section (bfd_get_section (symbol)))
516     /* The relocation is against a global symbol.  */
517     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
518                                   input_section, output_bfd,
519                                   error_message);
520
521   return score_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
522                                input_section, output_bfd, error_message);
523 }
524
525 static bfd_reloc_status_type
526 score_elf_got_lo16_reloc (bfd *abfd,
527                           arelent *reloc_entry,
528                           asymbol *symbol ATTRIBUTE_UNUSED,
529                           void * data,
530                           asection *input_section,
531                           bfd *output_bfd ATTRIBUTE_UNUSED,
532                           char **error_message ATTRIBUTE_UNUSED)
533 {
534   bfd_vma addend = 0, offset = 0;
535   unsigned long val;
536   unsigned long hi16_offset, hi16_value, uvalue;
537
538   hi16_value = bfd_get_32 (abfd, hi16_rel_addr);
539   hi16_offset = ((((hi16_value >> 16) & 0x3) << 15) | (hi16_value & 0x7fff)) >> 1;
540   addend = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
541   offset = ((((addend >> 16) & 0x3) << 15) | (addend & 0x7fff)) >> 1;
542   val = reloc_entry->addend;
543   if (reloc_entry->address > input_section->size)
544     return bfd_reloc_outofrange;
545   uvalue = ((hi16_offset << 16) | (offset & 0xffff)) + val;
546   hi16_offset = (uvalue >> 16) & 0x7fff;
547   hi16_value = (hi16_value & ~0x37fff) | (hi16_offset & 0x7fff) | ((hi16_offset << 1) & 0x30000);
548   bfd_put_32 (abfd, hi16_value, hi16_rel_addr);
549   offset = (uvalue & 0xffff) << 1;
550   addend = (addend & ~0x37fff) | (offset & 0x7fff) | ((offset << 1) & 0x30000);
551   bfd_put_32 (abfd, addend, (bfd_byte *) data + reloc_entry->address);
552   return bfd_reloc_ok;
553 }
554
555 static reloc_howto_type elf32_score_howto_table[] =
556 {
557   /* No relocation.  */
558   HOWTO (R_SCORE_NONE,          /* type */
559          0,                     /* rightshift */
560          0,                     /* size (0 = byte, 1 = short, 2 = long) */
561          0,                     /* bitsize */
562          FALSE,                 /* pc_relative */
563          0,                     /* bitpos */
564          complain_overflow_dont,/* complain_on_overflow */
565          bfd_elf_generic_reloc, /* special_function */
566          "R_SCORE_NONE",        /* name */
567          FALSE,                 /* partial_inplace */
568          0,                     /* src_mask */
569          0,                     /* dst_mask */
570          FALSE),                /* pcrel_offset */
571
572   /* R_SCORE_HI16 */
573   HOWTO (R_SCORE_HI16,          /* type */
574          0,                     /* rightshift */
575          2,                     /* size (0 = byte, 1 = short, 2 = long) */
576          16,                    /* bitsize */
577          FALSE,                 /* pc_relative */
578          1,                     /* bitpos */
579          complain_overflow_dont,/* complain_on_overflow */
580          score_elf_hi16_reloc,  /* special_function */
581          "R_SCORE_HI16",        /* name */
582          TRUE,                  /* partial_inplace */
583          0x37fff,               /* src_mask */
584          0x37fff,               /* dst_mask */
585          FALSE),                /* pcrel_offset */
586
587   /* R_SCORE_LO16 */
588   HOWTO (R_SCORE_LO16,          /* type */
589          0,                     /* rightshift */
590          2,                     /* size (0 = byte, 1 = short, 2 = long) */
591          16,                    /* bitsize */
592          FALSE,                 /* pc_relative */
593          1,                     /* bitpos */
594          complain_overflow_dont,/* complain_on_overflow */
595          score_elf_lo16_reloc,  /* special_function */
596          "R_SCORE_LO16",        /* name */
597          TRUE,                  /* partial_inplace */
598          0x37fff,               /* src_mask */
599          0x37fff,               /* dst_mask */
600          FALSE),                /* pcrel_offset */
601
602   /*  R_SCORE_DUMMY1 */
603   HOWTO (R_SCORE_DUMMY1,        /* type */
604          0,                     /* rightshift */
605          2,                     /* size (0 = byte, 1 = short, 2 = long) */
606          16,                    /* bitsize */
607          FALSE,                 /* pc_relative */
608          1,                     /* bitpos */
609          complain_overflow_dont,/* complain_on_overflow */
610          bfd_elf_generic_reloc, /* special_function */
611          "R_SCORE_DUMMY1",      /* name */
612          TRUE,                  /* partial_inplace */
613          0x0000ffff,            /* src_mask */
614          0x0000ffff,            /* dst_mask */
615          FALSE),                /* pcrel_offset */
616
617   /*R_SCORE_24 */
618   HOWTO (R_SCORE_24,            /* type */
619          1,                     /* rightshift */
620          2,                     /* size (0 = byte, 1 = short, 2 = long) */
621          24,                    /* bitsize */
622          FALSE,                 /* pc_relative */
623          1,                     /* bitpos */
624          complain_overflow_dont,/* complain_on_overflow */
625          bfd_elf_generic_reloc, /* special_function */
626          "R_SCORE_24",          /* name */
627          FALSE,                 /* partial_inplace */
628          0x3ff7fff,             /* src_mask */
629          0x3ff7fff,             /* dst_mask */
630          FALSE),                /* pcrel_offset */
631
632   /*R_SCORE_PC19 */
633   HOWTO (R_SCORE_PC19,          /* type */
634          1,                     /* rightshift */
635          2,                     /* size (0 = byte, 1 = short, 2 = long) */
636          19,                    /* bitsize */
637          TRUE,                  /* pc_relative */
638          1,                     /* bitpos */
639          complain_overflow_dont,/* complain_on_overflow */
640          bfd_elf_generic_reloc, /* special_function */
641          "R_SCORE_PC19",        /* name */
642          FALSE,                 /* partial_inplace */
643          0x3ff03fe,             /* src_mask */
644          0x3ff03fe,             /* dst_mask */
645          FALSE),                /* pcrel_offset */
646
647   /*R_SCORE16_11 */
648   HOWTO (R_SCORE16_11,          /* type */
649          1,                     /* rightshift */
650          1,                     /* size (0 = byte, 1 = short, 2 = long) */
651          11,                    /* bitsize */
652          FALSE,                 /* pc_relative */
653          1,                     /* bitpos */
654          complain_overflow_dont,/* complain_on_overflow */
655          bfd_elf_generic_reloc, /* special_function */
656          "R_SCORE16_11",        /* name */
657          FALSE,                 /* partial_inplace */
658          0x000000ffe,           /* src_mask */
659          0x000000ffe,           /* dst_mask */
660          FALSE),                /* pcrel_offset */
661
662   /* R_SCORE16_PC8 */
663   HOWTO (R_SCORE16_PC8,         /* type */
664          1,                     /* rightshift */
665          1,                     /* size (0 = byte, 1 = short, 2 = long) */
666          8,                     /* bitsize */
667          TRUE,                  /* pc_relative */
668          0,                     /* bitpos */
669          complain_overflow_dont,/* complain_on_overflow */
670          bfd_elf_generic_reloc, /* special_function */
671          "R_SCORE16_PC8",       /* name */
672          FALSE,                 /* partial_inplace */
673          0x000000ff,            /* src_mask */
674          0x000000ff,            /* dst_mask */
675          FALSE),                /* pcrel_offset */
676
677   /* 32 bit absolute */
678   HOWTO (R_SCORE_ABS32,         /* type  8 */
679          0,                     /* rightshift */
680          2,                     /* size (0 = byte, 1 = short, 2 = long) */
681          32,                    /* bitsize */
682          FALSE,                 /* pc_relative */
683          0,                     /* bitpos */
684          complain_overflow_bitfield,    /* complain_on_overflow */
685          bfd_elf_generic_reloc, /* special_function */
686          "R_SCORE_ABS32",       /* name */
687          FALSE,                 /* partial_inplace */
688          0xffffffff,            /* src_mask */
689          0xffffffff,            /* dst_mask */
690          FALSE),                /* pcrel_offset */
691
692   /* 16 bit absolute */
693   HOWTO (R_SCORE_ABS16,         /* type 11 */
694          0,                     /* rightshift */
695          1,                     /* size (0 = byte, 1 = short, 2 = long) */
696          16,                    /* bitsize */
697          FALSE,                 /* pc_relative */
698          0,                     /* bitpos */
699          complain_overflow_bitfield,    /* complain_on_overflow */
700          bfd_elf_generic_reloc, /* special_function */
701          "R_SCORE_ABS16",       /* name */
702          FALSE,                 /* partial_inplace */
703          0x0000ffff,            /* src_mask */
704          0x0000ffff,            /* dst_mask */
705          FALSE),                /* pcrel_offset */
706
707   /* R_SCORE_DUMMY2 */
708   HOWTO (R_SCORE_DUMMY2,        /* type */
709          0,                     /* rightshift */
710          2,                     /* size (0 = byte, 1 = short, 2 = long) */
711          16,                    /* bitsize */
712          FALSE,                 /* pc_relative */
713          0,                     /* bitpos */
714          complain_overflow_dont,/* complain_on_overflow */
715          bfd_elf_generic_reloc, /* special_function */
716          "R_SCORE_DUMMY2",      /* name */
717          TRUE,                  /* partial_inplace */
718          0x00007fff,            /* src_mask */
719          0x00007fff,            /* dst_mask */
720          FALSE),                /* pcrel_offset */
721
722   /* R_SCORE_GP15 */
723   HOWTO (R_SCORE_GP15,          /* type */
724          0,                     /* rightshift */
725          2,                     /* size (0 = byte, 1 = short, 2 = long) */
726          16,                    /* bitsize */
727          FALSE,                 /* pc_relative */
728          0,                     /* bitpos */
729          complain_overflow_dont,/* complain_on_overflow */
730          score_elf_gprel15_reloc,/* special_function */
731          "R_SCORE_GP15",        /* name */
732          TRUE,                  /* partial_inplace */
733          0x00007fff,            /* src_mask */
734          0x00007fff,            /* dst_mask */
735          FALSE),                /* pcrel_offset */
736
737   /* GNU extension to record C++ vtable hierarchy.  */
738   HOWTO (R_SCORE_GNU_VTINHERIT, /* type */
739          0,                     /* rightshift */
740          2,                     /* size (0 = byte, 1 = short, 2 = long) */
741          0,                     /* bitsize */
742          FALSE,                 /* pc_relative */
743          0,                     /* bitpos */
744          complain_overflow_dont,/* complain_on_overflow */
745          NULL,                  /* special_function */
746          "R_SCORE_GNU_VTINHERIT",       /* name */
747          FALSE,                 /* partial_inplace */
748          0,                     /* src_mask */
749          0,                     /* dst_mask */
750          FALSE),                /* pcrel_offset */
751
752   /* GNU extension to record C++ vtable member usage */
753   HOWTO (R_SCORE_GNU_VTENTRY,   /* type */
754          0,                     /* rightshift */
755          2,                     /* size (0 = byte, 1 = short, 2 = long) */
756          0,                     /* bitsize */
757          FALSE,                 /* pc_relative */
758          0,                     /* bitpos */
759          complain_overflow_dont,/* complain_on_overflow */
760          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
761          "R_SCORE_GNU_VTENTRY", /* name */
762          FALSE,                 /* partial_inplace */
763          0,                     /* src_mask */
764          0,                     /* dst_mask */
765          FALSE),                /* pcrel_offset */
766
767   /* Reference to global offset table.  */
768   HOWTO (R_SCORE_GOT15,         /* type */
769          0,                     /* rightshift */
770          2,                     /* size (0 = byte, 1 = short, 2 = long) */
771          16,                    /* bitsize */
772          FALSE,                 /* pc_relative */
773          0,                     /* bitpos */
774          complain_overflow_signed,      /* complain_on_overflow */
775          score_elf_got15_reloc, /* special_function */
776          "R_SCORE_GOT15",       /* name */
777          TRUE,                  /* partial_inplace */
778          0x00007fff,            /* src_mask */
779          0x00007fff,            /* dst_mask */
780          FALSE),                /* pcrel_offset */
781
782   /* Low 16 bits of displacement in global offset table.  */
783   HOWTO (R_SCORE_GOT_LO16,      /* type */
784          0,                     /* rightshift */
785          2,                     /* size (0 = byte, 1 = short, 2 = long) */
786          16,                    /* bitsize */
787          FALSE,                 /* pc_relative */
788          1,                     /* bitpos */
789          complain_overflow_dont,/* complain_on_overflow */
790          score_elf_got_lo16_reloc, /* special_function */
791          "R_SCORE_GOT_LO16",    /* name */
792          TRUE,                  /* partial_inplace */
793          0x37ffe,               /* src_mask */
794          0x37ffe,               /* dst_mask */
795          FALSE),                /* pcrel_offset */
796
797   /* 15 bit call through global offset table.  */
798   HOWTO (R_SCORE_CALL15,        /* type */
799          0,                     /* rightshift */
800          2,                     /* size (0 = byte, 1 = short, 2 = long) */
801          16,                    /* bitsize */
802          FALSE,                 /* pc_relative */
803          0,                     /* bitpos */
804          complain_overflow_signed, /* complain_on_overflow */
805          bfd_elf_generic_reloc, /* special_function */
806          "R_SCORE_CALL15",      /* name */
807          TRUE,                  /* partial_inplace */
808          0x0000ffff,            /* src_mask */
809          0x0000ffff,            /* dst_mask */
810          FALSE),                /* pcrel_offset */
811
812   /* 32 bit GP relative reference.  */
813   HOWTO (R_SCORE_GPREL32,       /* type */
814          0,                     /* rightshift */
815          2,                     /* size (0 = byte, 1 = short, 2 = long) */
816          32,                    /* bitsize */
817          FALSE,                 /* pc_relative */
818          0,                     /* bitpos */
819          complain_overflow_dont,/* complain_on_overflow */
820          score_elf_gprel32_reloc, /* special_function */
821          "R_SCORE_GPREL32",     /* name */
822          TRUE,                  /* partial_inplace */
823          0xffffffff,            /* src_mask */
824          0xffffffff,            /* dst_mask */
825          FALSE),                /* pcrel_offset */
826
827   /* 32 bit symbol relative relocation.  */
828   HOWTO (R_SCORE_REL32,         /* type */
829          0,                     /* rightshift */
830          2,                     /* size (0 = byte, 1 = short, 2 = long) */
831          32,                    /* bitsize */
832          FALSE,                 /* pc_relative */
833          0,                     /* bitpos */
834          complain_overflow_dont,/* complain_on_overflow */
835          bfd_elf_generic_reloc, /* special_function */
836          "R_SCORE_REL32",       /* name */
837          TRUE,                  /* partial_inplace */
838          0xffffffff,            /* src_mask */
839          0xffffffff,            /* dst_mask */
840          FALSE),                /* pcrel_offset */
841
842   /* R_SCORE_DUMMY_HI16 */
843   HOWTO (R_SCORE_DUMMY_HI16,    /* type */
844          0,                     /* rightshift */
845          2,                     /* size (0 = byte, 1 = short, 2 = long) */
846          16,                    /* bitsize */
847          FALSE,                 /* pc_relative */
848          1,                     /* bitpos */
849          complain_overflow_dont,/* complain_on_overflow */
850          score_elf_hi16_reloc,  /* special_function */
851          "R_SCORE_DUMMY_HI16",  /* name */
852          TRUE,                  /* partial_inplace */
853          0x37fff,               /* src_mask */
854          0x37fff,               /* dst_mask */
855          FALSE),                /* pcrel_offset */
856 };
857
858 struct score_reloc_map
859 {
860   bfd_reloc_code_real_type bfd_reloc_val;
861   unsigned char elf_reloc_val;
862 };
863
864 static const struct score_reloc_map elf32_score_reloc_map[] =
865 {
866   {BFD_RELOC_NONE,               R_SCORE_NONE},
867   {BFD_RELOC_HI16_S,             R_SCORE_HI16},
868   {BFD_RELOC_LO16,               R_SCORE_LO16},
869   {BFD_RELOC_SCORE_DUMMY1,       R_SCORE_DUMMY1},
870   {BFD_RELOC_SCORE_JMP,          R_SCORE_24},
871   {BFD_RELOC_SCORE_BRANCH,       R_SCORE_PC19},
872   {BFD_RELOC_SCORE16_JMP,        R_SCORE16_11},
873   {BFD_RELOC_SCORE16_BRANCH,     R_SCORE16_PC8},
874   {BFD_RELOC_32,                 R_SCORE_ABS32},
875   {BFD_RELOC_16,                 R_SCORE_ABS16},
876   {BFD_RELOC_SCORE_DUMMY2,       R_SCORE_DUMMY2},
877   {BFD_RELOC_SCORE_GPREL15,      R_SCORE_GP15},
878   {BFD_RELOC_VTABLE_INHERIT,     R_SCORE_GNU_VTINHERIT},
879   {BFD_RELOC_VTABLE_ENTRY,       R_SCORE_GNU_VTENTRY},
880   {BFD_RELOC_SCORE_GOT15,        R_SCORE_GOT15},
881   {BFD_RELOC_SCORE_GOT_LO16,     R_SCORE_GOT_LO16},
882   {BFD_RELOC_SCORE_CALL15,       R_SCORE_CALL15},
883   {BFD_RELOC_GPREL32,            R_SCORE_GPREL32},
884   {BFD_RELOC_32_PCREL,           R_SCORE_REL32},
885   {BFD_RELOC_SCORE_DUMMY_HI16,   R_SCORE_DUMMY_HI16},
886 };
887
888 /* got_entries only match if they're identical, except for gotidx, so
889    use all fields to compute the hash, and compare the appropriate
890    union members.  */
891
892 static hashval_t
893 score_elf_got_entry_hash (const void *entry_)
894 {
895   const struct score_got_entry *entry = (struct score_got_entry *)entry_;
896
897   return entry->symndx
898     + (!entry->abfd ? entry->d.address : entry->abfd->id);
899 }
900
901 static int
902 score_elf_got_entry_eq (const void *entry1, const void *entry2)
903 {
904   const struct score_got_entry *e1 = (struct score_got_entry *)entry1;
905   const struct score_got_entry *e2 = (struct score_got_entry *)entry2;
906
907   return e1->abfd == e2->abfd && e1->symndx == e2->symndx
908     && (! e1->abfd ? e1->d.address == e2->d.address
909         : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
910         : e1->d.h == e2->d.h);
911 }
912
913 /* If H needs a GOT entry, assign it the highest available dynamic
914    index.  Otherwise, assign it the lowest available dynamic
915    index.  */
916
917 static bfd_boolean
918 score_elf_sort_hash_table_f (struct score_elf_link_hash_entry *h, void *data)
919 {
920   struct score_elf_hash_sort_data *hsd = data;
921
922   if (h->root.root.type == bfd_link_hash_warning)
923     h = (struct score_elf_link_hash_entry *) h->root.root.u.i.link;
924
925   /* Symbols without dynamic symbol table entries aren't interesting at all.  */
926   if (h->root.dynindx == -1)
927     return TRUE;
928
929   /* Global symbols that need GOT entries that are not explicitly
930      referenced are marked with got offset 2.  Those that are
931      referenced get a 1, and those that don't need GOT entries get
932      -1.  */
933   if (h->root.got.offset == 2)
934     {
935       if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
936         hsd->low = (struct elf_link_hash_entry *) h;
937       h->root.dynindx = hsd->max_unref_got_dynindx++;
938     }
939   else if (h->root.got.offset != 1)
940     h->root.dynindx = hsd->max_non_got_dynindx++;
941   else
942     {
943       h->root.dynindx = --hsd->min_got_dynindx;
944       hsd->low = (struct elf_link_hash_entry *) h;
945     }
946
947   return TRUE;
948 }
949
950 static asection *
951 score_elf_got_section (bfd *abfd, bfd_boolean maybe_excluded)
952 {
953   asection *sgot = bfd_get_section_by_name (abfd, ".got");
954
955   if (sgot == NULL || (! maybe_excluded && (sgot->flags & SEC_EXCLUDE) != 0))
956     return NULL;
957   return sgot;
958 }
959
960 /* Returns the GOT information associated with the link indicated by
961    INFO.  If SGOTP is non-NULL, it is filled in with the GOT section.  */
962
963 static struct score_got_info *
964 score_elf_got_info (bfd *abfd, asection **sgotp)
965 {
966   asection *sgot;
967   struct score_got_info *g;
968
969   sgot = score_elf_got_section (abfd, TRUE);
970   BFD_ASSERT (sgot != NULL);
971   BFD_ASSERT (elf_section_data (sgot) != NULL);
972   g = score_elf_section_data (sgot)->u.got_info;
973   BFD_ASSERT (g != NULL);
974
975   if (sgotp)
976     *sgotp = sgot;
977   return g;
978 }
979
980 /* Sort the dynamic symbol table so that symbols that need GOT entries
981    appear towards the end.  This reduces the amount of GOT space
982    required.  MAX_LOCAL is used to set the number of local symbols
983    known to be in the dynamic symbol table.  During
984    _bfd_score_elf_size_dynamic_sections, this value is 1.  Afterward, the
985    section symbols are added and the count is higher.  */
986
987 static bfd_boolean
988 score_elf_sort_hash_table (struct bfd_link_info *info,
989                            unsigned long max_local)
990 {
991   struct score_elf_hash_sort_data hsd;
992   struct score_got_info *g;
993   bfd *dynobj;
994
995   dynobj = elf_hash_table (info)->dynobj;
996
997   g = score_elf_got_info (dynobj, NULL);
998
999   hsd.low = NULL;
1000   hsd.max_unref_got_dynindx =
1001     hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount
1002     /* In the multi-got case, assigned_gotno of the master got_info
1003        indicate the number of entries that aren't referenced in the
1004        primary GOT, but that must have entries because there are
1005        dynamic relocations that reference it.  Since they aren't
1006        referenced, we move them to the end of the GOT, so that they
1007        don't prevent other entries that are referenced from getting
1008        too large offsets.  */
1009     - (g->next ? g->assigned_gotno : 0);
1010   hsd.max_non_got_dynindx = max_local;
1011   score_elf_link_hash_traverse (((struct score_elf_link_hash_table *)
1012                                  elf_hash_table (info)),
1013                                  score_elf_sort_hash_table_f,
1014                                  &hsd);
1015
1016   /* There should have been enough room in the symbol table to
1017      accommodate both the GOT and non-GOT symbols.  */
1018   BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
1019   BFD_ASSERT ((unsigned long)hsd.max_unref_got_dynindx
1020               <= elf_hash_table (info)->dynsymcount);
1021
1022   /* Now we know which dynamic symbol has the lowest dynamic symbol
1023      table index in the GOT.  */
1024   g->global_gotsym = hsd.low;
1025
1026   return TRUE;
1027 }
1028
1029 /* Create an entry in an score ELF linker hash table.  */
1030
1031 static struct bfd_hash_entry *
1032 score_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1033                              struct bfd_hash_table *table,
1034                              const char *string)
1035 {
1036   struct score_elf_link_hash_entry *ret = (struct score_elf_link_hash_entry *)entry;
1037
1038   /* Allocate the structure if it has not already been allocated by a subclass.  */
1039   if (ret == NULL)
1040     ret = bfd_hash_allocate (table, sizeof (struct score_elf_link_hash_entry));
1041   if (ret == NULL)
1042     return (struct bfd_hash_entry *)ret;
1043
1044   /* Call the allocation method of the superclass.  */
1045   ret = ((struct score_elf_link_hash_entry *)
1046          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *)ret, table, string));
1047
1048   if (ret != NULL)
1049     {
1050       ret->possibly_dynamic_relocs = 0;
1051       ret->readonly_reloc = FALSE;
1052       ret->no_fn_stub = FALSE;
1053       ret->forced_local = FALSE;
1054     }
1055
1056   return (struct bfd_hash_entry *)ret;
1057 }
1058
1059 /* Returns the first relocation of type r_type found, beginning with
1060    RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
1061
1062 static const Elf_Internal_Rela *
1063 score_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
1064                            const Elf_Internal_Rela *relocation,
1065                            const Elf_Internal_Rela *relend)
1066 {
1067   while (relocation < relend)
1068     {
1069       if (ELF32_R_TYPE (relocation->r_info) == r_type)
1070         return relocation;
1071
1072       ++relocation;
1073     }
1074
1075   /* We didn't find it.  */
1076   bfd_set_error (bfd_error_bad_value);
1077   return NULL;
1078 }
1079
1080 /* This function is called via qsort() to sort the dynamic relocation
1081    entries by increasing r_symndx value.  */
1082
1083 static int
1084 score_elf_sort_dynamic_relocs (const void *arg1, const void *arg2)
1085 {
1086   Elf_Internal_Rela int_reloc1;
1087   Elf_Internal_Rela int_reloc2;
1088
1089   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
1090   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
1091
1092   return (ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info));
1093 }
1094
1095 /* Return whether a relocation is against a local symbol.  */
1096
1097 static bfd_boolean
1098 score_elf_local_relocation_p (bfd *input_bfd,
1099                               const Elf_Internal_Rela *relocation,
1100                               asection **local_sections,
1101                               bfd_boolean check_forced)
1102 {
1103   unsigned long r_symndx;
1104   Elf_Internal_Shdr *symtab_hdr;
1105   struct score_elf_link_hash_entry *h;
1106   size_t extsymoff;
1107
1108   r_symndx = ELF32_R_SYM (relocation->r_info);
1109   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1110   extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
1111
1112   if (r_symndx < extsymoff)
1113     return TRUE;
1114   if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
1115     return TRUE;
1116
1117   if (check_forced)
1118     {
1119       /* Look up the hash table to check whether the symbol was forced local.  */
1120       h = (struct score_elf_link_hash_entry *)
1121         elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
1122       /* Find the real hash-table entry for this symbol.  */
1123       while (h->root.root.type == bfd_link_hash_indirect
1124              || h->root.root.type == bfd_link_hash_warning)
1125         h = (struct score_elf_link_hash_entry *) h->root.root.u.i.link;
1126       if (h->root.forced_local)
1127         return TRUE;
1128     }
1129
1130   return FALSE;
1131 }
1132
1133 /* Returns the dynamic relocation section for DYNOBJ.  */
1134
1135 static asection *
1136 score_elf_rel_dyn_section (bfd *dynobj, bfd_boolean create_p)
1137 {
1138   static const char dname[] = ".rel.dyn";
1139   asection *sreloc;
1140
1141   sreloc = bfd_get_section_by_name (dynobj, dname);
1142   if (sreloc == NULL && create_p)
1143     {
1144       sreloc = bfd_make_section (dynobj, dname);
1145       if (sreloc == NULL
1146           || ! bfd_set_section_flags (dynobj, sreloc,
1147                                       (SEC_ALLOC
1148                                        | SEC_LOAD
1149                                        | SEC_HAS_CONTENTS
1150                                        | SEC_IN_MEMORY
1151                                        | SEC_LINKER_CREATED
1152                                        | SEC_READONLY))
1153           || ! bfd_set_section_alignment (dynobj, sreloc, SCORE_ELF_LOG_FILE_ALIGN (dynobj)))
1154         return NULL;
1155     }
1156
1157   return sreloc;
1158 }
1159
1160 static void
1161 score_elf_allocate_dynamic_relocations (bfd *abfd, unsigned int n)
1162 {
1163   asection *s;
1164
1165   s = score_elf_rel_dyn_section (abfd, FALSE);
1166   BFD_ASSERT (s != NULL);
1167
1168   if (s->size == 0)
1169     {
1170       /* Make room for a null element.  */
1171       s->size += SCORE_ELF_REL_SIZE (abfd);
1172       ++s->reloc_count;
1173     }
1174   s->size += n * SCORE_ELF_REL_SIZE (abfd);
1175 }
1176
1177 /* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
1178    is the original relocation, which is now being transformed into a
1179    dynamic relocation.  The ADDENDP is adjusted if necessary; the
1180    caller should store the result in place of the original addend.  */
1181
1182 static bfd_boolean
1183 score_elf_create_dynamic_relocation (bfd *output_bfd,
1184                                      struct bfd_link_info *info,
1185                                      const Elf_Internal_Rela *rel,
1186                                      struct score_elf_link_hash_entry *h,
1187                                      asection *sec, bfd_vma symbol,
1188                                      bfd_vma *addendp, asection *input_section)
1189 {
1190   Elf_Internal_Rela outrel[3];
1191   asection *sreloc;
1192   bfd *dynobj;
1193   int r_type;
1194   long indx;
1195   bfd_boolean defined_p;
1196
1197   r_type = ELF32_R_TYPE (rel->r_info);
1198   dynobj = elf_hash_table (info)->dynobj;
1199   sreloc = score_elf_rel_dyn_section (dynobj, FALSE);
1200   BFD_ASSERT (sreloc != NULL);
1201   BFD_ASSERT (sreloc->contents != NULL);
1202   BFD_ASSERT (sreloc->reloc_count * SCORE_ELF_REL_SIZE (output_bfd) < sreloc->size);
1203
1204   outrel[0].r_offset =
1205     _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
1206   outrel[1].r_offset =
1207     _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
1208   outrel[2].r_offset =
1209     _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
1210
1211   if (outrel[0].r_offset == MINUS_ONE)
1212     /* The relocation field has been deleted.  */
1213     return TRUE;
1214
1215   if (outrel[0].r_offset == MINUS_TWO)
1216     {
1217       /* The relocation field has been converted into a relative value of
1218          some sort.  Functions like _bfd_elf_write_section_eh_frame expect
1219          the field to be fully relocated, so add in the symbol's value.  */
1220       *addendp += symbol;
1221       return TRUE;
1222     }
1223
1224   /* We must now calculate the dynamic symbol table index to use
1225      in the relocation.  */
1226   if (h != NULL
1227       && (! info->symbolic || !h->root.def_regular)
1228       /* h->root.dynindx may be -1 if this symbol was marked to
1229          become local.  */
1230       && h->root.dynindx != -1)
1231     {
1232       indx = h->root.dynindx;
1233         /* ??? glibc's ld.so just adds the final GOT entry to the
1234            relocation field.  It therefore treats relocs against
1235            defined symbols in the same way as relocs against
1236            undefined symbols.  */
1237       defined_p = FALSE;
1238     }
1239   else
1240     {
1241       if (sec != NULL && bfd_is_abs_section (sec))
1242         indx = 0;
1243       else if (sec == NULL || sec->owner == NULL)
1244         {
1245           bfd_set_error (bfd_error_bad_value);
1246           return FALSE;
1247         }
1248       else
1249         {
1250           indx = elf_section_data (sec->output_section)->dynindx;
1251           if (indx == 0)
1252             abort ();
1253         }
1254
1255       /* Instead of generating a relocation using the section
1256          symbol, we may as well make it a fully relative
1257          relocation.  We want to avoid generating relocations to
1258          local symbols because we used to generate them
1259          incorrectly, without adding the original symbol value,
1260          which is mandated by the ABI for section symbols.  In
1261          order to give dynamic loaders and applications time to
1262          phase out the incorrect use, we refrain from emitting
1263          section-relative relocations.  It's not like they're
1264          useful, after all.  This should be a bit more efficient
1265          as well.  */
1266       /* ??? Although this behavior is compatible with glibc's ld.so,
1267          the ABI says that relocations against STN_UNDEF should have
1268          a symbol value of 0.  Irix rld honors this, so relocations
1269          against STN_UNDEF have no effect.  */
1270       defined_p = TRUE;
1271     }
1272
1273   /* If the relocation was previously an absolute relocation and
1274      this symbol will not be referred to by the relocation, we must
1275      adjust it by the value we give it in the dynamic symbol table.
1276      Otherwise leave the job up to the dynamic linker.  */
1277   if (defined_p && r_type != R_SCORE_REL32)
1278     *addendp += symbol;
1279
1280   /* The relocation is always an REL32 relocation because we don't
1281      know where the shared library will wind up at load-time.  */
1282   outrel[0].r_info = ELF32_R_INFO ((unsigned long) indx, R_SCORE_REL32);
1283
1284   /* For strict adherence to the ABI specification, we should
1285      generate a R_SCORE_64 relocation record by itself before the
1286      _REL32/_64 record as well, such that the addend is read in as
1287      a 64-bit value (REL32 is a 32-bit relocation, after all).
1288      However, since none of the existing ELF64 SCORE dynamic
1289      loaders seems to care, we don't waste space with these
1290      artificial relocations.  If this turns out to not be true,
1291      score_elf_allocate_dynamic_relocations() should be tweaked so
1292      as to make room for a pair of dynamic relocations per
1293      invocation if ABI_64_P, and here we should generate an
1294      additional relocation record with R_SCORE_64 by itself for a
1295      NULL symbol before this relocation record.  */
1296   outrel[1].r_info = ELF32_R_INFO (0, R_SCORE_NONE);
1297   outrel[2].r_info = ELF32_R_INFO (0, R_SCORE_NONE);
1298
1299   /* Adjust the output offset of the relocation to reference the
1300      correct location in the output file.  */
1301   outrel[0].r_offset += (input_section->output_section->vma
1302                          + input_section->output_offset);
1303   outrel[1].r_offset += (input_section->output_section->vma
1304                          + input_section->output_offset);
1305   outrel[2].r_offset += (input_section->output_section->vma
1306                          + input_section->output_offset);
1307
1308   /* Put the relocation back out.  We have to use the special
1309      relocation outputter in the 64-bit case since the 64-bit
1310      relocation format is non-standard.  */
1311   bfd_elf32_swap_reloc_out
1312       (output_bfd, &outrel[0],
1313        (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
1314
1315   /* We've now added another relocation.  */
1316   ++sreloc->reloc_count;
1317
1318   /* Make sure the output section is writable.  The dynamic linker
1319      will be writing to it.  */
1320   elf_section_data (input_section->output_section)->this_hdr.sh_flags |= SHF_WRITE;
1321
1322   return TRUE;
1323 }
1324
1325 static bfd_boolean
1326 score_elf_create_got_section (bfd *abfd,
1327                               struct bfd_link_info *info,
1328                               bfd_boolean maybe_exclude)
1329 {
1330   flagword flags;
1331   asection *s;
1332   struct elf_link_hash_entry *h;
1333   struct bfd_link_hash_entry *bh;
1334   struct score_got_info *g;
1335   bfd_size_type amt;
1336
1337   /* This function may be called more than once.  */
1338   s = score_elf_got_section (abfd, TRUE);
1339   if (s)
1340     {
1341       if (! maybe_exclude)
1342         s->flags &= ~SEC_EXCLUDE;
1343       return TRUE;
1344     }
1345
1346   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1347
1348   if (maybe_exclude)
1349     flags |= SEC_EXCLUDE;
1350
1351   /* We have to use an alignment of 2**4 here because this is hardcoded
1352      in the function stub generation and in the linker script.  */
1353   s = bfd_make_section (abfd, ".got");
1354   if (s == NULL
1355       || ! bfd_set_section_flags (abfd, s, flags)
1356       || ! bfd_set_section_alignment (abfd, s, 4))
1357     return FALSE;
1358
1359   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
1360      linker script because we don't want to define the symbol if we
1361      are not creating a global offset table.  */
1362   bh = NULL;
1363   if (! (_bfd_generic_link_add_one_symbol
1364          (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
1365           0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
1366     return FALSE;
1367
1368   h = (struct elf_link_hash_entry *) bh;
1369   h->non_elf = 0;
1370   h->def_regular = 1;
1371   h->type = STT_OBJECT;
1372
1373   if (info->shared && ! bfd_elf_link_record_dynamic_symbol (info, h))
1374     return FALSE;
1375
1376   amt = sizeof (struct score_got_info);
1377   g = bfd_alloc (abfd, amt);
1378   if (g == NULL)
1379     return FALSE;
1380
1381   g->global_gotsym = NULL;
1382   g->global_gotno = 0;
1383
1384   g->local_gotno = SCORE_RESERVED_GOTNO;
1385   g->assigned_gotno = SCORE_RESERVED_GOTNO;
1386   g->next = NULL;
1387
1388   g->got_entries = htab_try_create (1, score_elf_got_entry_hash,
1389                                     score_elf_got_entry_eq, NULL);
1390   if (g->got_entries == NULL)
1391     return FALSE;
1392   score_elf_section_data (s)->u.got_info = g;
1393   score_elf_section_data (s)->elf.this_hdr.sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_SCORE_GPREL;
1394
1395   return TRUE;
1396 }
1397
1398 /* Calculate the %high function.  */
1399
1400 static bfd_vma
1401 score_elf_high (bfd_vma value)
1402 {
1403   return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
1404 }
1405
1406 /* Create a local GOT entry for VALUE.  Return the index of the entry,
1407    or -1 if it could not be created.  */
1408
1409 static struct score_got_entry *
1410 score_elf_create_local_got_entry (bfd *abfd,
1411                                   bfd *ibfd ATTRIBUTE_UNUSED,
1412                                   struct score_got_info *gg,
1413                                   asection *sgot, bfd_vma value,
1414                                   unsigned long r_symndx ATTRIBUTE_UNUSED,
1415                                   struct score_elf_link_hash_entry *h ATTRIBUTE_UNUSED,
1416                                   int r_type ATTRIBUTE_UNUSED)
1417 {
1418   struct score_got_entry entry, **loc;
1419   struct score_got_info *g;
1420
1421   entry.abfd = NULL;
1422   entry.symndx = -1;
1423   entry.d.address = value;
1424
1425   g = gg;
1426   loc = (struct score_got_entry **) htab_find_slot (g->got_entries, &entry, INSERT);
1427   if (*loc)
1428     return *loc;
1429
1430   entry.gotidx = SCORE_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
1431
1432   *loc = bfd_alloc (abfd, sizeof entry);
1433
1434   if (! *loc)
1435     return NULL;
1436
1437   memcpy (*loc, &entry, sizeof entry);
1438
1439   if (g->assigned_gotno >= g->local_gotno)
1440     {
1441       (*loc)->gotidx = -1;
1442       /* We didn't allocate enough space in the GOT.  */
1443       (*_bfd_error_handler)
1444         (_("not enough GOT space for local GOT entries"));
1445       bfd_set_error (bfd_error_bad_value);
1446       return NULL;
1447     }
1448
1449   bfd_put_32 (abfd, value, (sgot->contents + entry.gotidx));
1450
1451   return *loc;
1452 }
1453
1454 /* Find a GOT entry whose higher-order 16 bits are the same as those
1455    for value.  Return the index into the GOT for this entry.  */
1456
1457 static bfd_vma
1458 score_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
1459                       bfd_vma value, bfd_boolean external)
1460 {
1461   asection *sgot;
1462   struct score_got_info *g;
1463   struct score_got_entry *entry;
1464
1465   if (!external)
1466     {
1467       /* Although the ABI says that it is "the high-order 16 bits" that we
1468          want, it is really the %high value.  The complete value is
1469          calculated with a `addiu' of a LO16 relocation, just as with a
1470          HI16/LO16 pair.  */
1471       value = score_elf_high (value) << 16;
1472     }
1473
1474   g = score_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
1475
1476   entry = score_elf_create_local_got_entry (abfd, ibfd, g, sgot, value, 0, NULL,
1477                                             R_SCORE_GOT15);
1478   if (entry)
1479     return entry->gotidx;
1480   else
1481     return MINUS_ONE;
1482 }
1483
1484 static void
1485 _bfd_score_elf_hide_symbol (struct bfd_link_info *info,
1486                             struct elf_link_hash_entry *entry,
1487                             bfd_boolean force_local)
1488 {
1489   bfd *dynobj;
1490   asection *got;
1491   struct score_got_info *g;
1492   struct score_elf_link_hash_entry *h;
1493
1494   h = (struct score_elf_link_hash_entry *) entry;
1495   if (h->forced_local)
1496     return;
1497   h->forced_local = TRUE;
1498
1499   dynobj = elf_hash_table (info)->dynobj;
1500   if (dynobj != NULL && force_local)
1501     {
1502       got = score_elf_got_section (dynobj, FALSE);
1503       if (got == NULL)
1504         return;
1505       g = score_elf_section_data (got)->u.got_info;
1506
1507       if (g->next)
1508         {
1509           struct score_got_entry e;
1510           struct score_got_info *gg = g;
1511
1512           /* Since we're turning what used to be a global symbol into a
1513              local one, bump up the number of local entries of each GOT
1514              that had an entry for it.  This will automatically decrease
1515              the number of global entries, since global_gotno is actually
1516              the upper limit of global entries.  */
1517           e.abfd = dynobj;
1518           e.symndx = -1;
1519           e.d.h = h;
1520
1521           for (g = g->next; g != gg; g = g->next)
1522             if (htab_find (g->got_entries, &e))
1523               {
1524                 BFD_ASSERT (g->global_gotno > 0);
1525                 g->local_gotno++;
1526                 g->global_gotno--;
1527               }
1528
1529           /* If this was a global symbol forced into the primary GOT, we
1530              no longer need an entry for it.  We can't release the entry
1531              at this point, but we must at least stop counting it as one
1532              of the symbols that required a forced got entry.  */
1533           if (h->root.got.offset == 2)
1534             {
1535               BFD_ASSERT (gg->assigned_gotno > 0);
1536               gg->assigned_gotno--;
1537             }
1538         }
1539       else if (g->global_gotno == 0 && g->global_gotsym == NULL)
1540         /* If we haven't got through GOT allocation yet, just bump up the
1541               number of local entries, as this symbol won't be counted as
1542               global.  */
1543         g->local_gotno++;
1544       else if (h->root.got.offset == 1)
1545         {
1546           /* If we're past non-multi-GOT allocation and this symbol had
1547                   been marked for a global got entry, give it a local entry
1548                   instead.  */
1549           BFD_ASSERT (g->global_gotno > 0);
1550           g->local_gotno++;
1551           g->global_gotno--;
1552         }
1553     }
1554
1555   _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
1556 }
1557
1558 /* If H is a symbol that needs a global GOT entry, but has a dynamic
1559    symbol table index lower than any we've seen to date, record it for
1560    posterity.  */
1561
1562 static bfd_boolean
1563 score_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
1564                                     bfd *abfd,
1565                                     struct bfd_link_info *info,
1566                                     struct score_got_info *g)
1567 {
1568   struct score_got_entry entry, **loc;
1569
1570   /* A global symbol in the GOT must also be in the dynamic symbol table.  */
1571   if (h->dynindx == -1)
1572     {
1573       switch (ELF_ST_VISIBILITY (h->other))
1574         {
1575         case STV_INTERNAL:
1576         case STV_HIDDEN:
1577           _bfd_score_elf_hide_symbol (info, h, TRUE);
1578           break;
1579         }
1580       if (!bfd_elf_link_record_dynamic_symbol (info, h))
1581         return FALSE;
1582     }
1583
1584   entry.abfd = abfd;
1585   entry.symndx = -1;
1586   entry.d.h = (struct score_elf_link_hash_entry *)h;
1587
1588   loc = (struct score_got_entry **)htab_find_slot (g->got_entries, &entry, INSERT);
1589
1590   /* If we've already marked this entry as needing GOT space, we don't
1591      need to do it again.  */
1592   if (*loc)
1593     return TRUE;
1594
1595   *loc = bfd_alloc (abfd, sizeof entry);
1596   if (! *loc)
1597     return FALSE;
1598
1599   entry.gotidx = -1;
1600
1601   memcpy (*loc, &entry, sizeof (entry));
1602
1603   if (h->got.offset != MINUS_ONE)
1604     return TRUE;
1605
1606   /* By setting this to a value other than -1, we are indicating that
1607      there needs to be a GOT entry for H.  Avoid using zero, as the
1608      generic ELF copy_indirect_symbol tests for <= 0.  */
1609   h->got.offset = 1;
1610
1611   return TRUE;
1612 }
1613
1614 /* Reserve space in G for a GOT entry containing the value of symbol
1615    SYMNDX in input bfd ABDF, plus ADDEND.  */
1616
1617 static bfd_boolean
1618 score_elf_record_local_got_symbol (bfd *abfd,
1619                                    long symndx,
1620                                    bfd_vma addend,
1621                                    struct score_got_info *g)
1622 {
1623   struct score_got_entry entry, **loc;
1624
1625   entry.abfd = abfd;
1626   entry.symndx = symndx;
1627   entry.d.addend = addend;
1628   loc = (struct score_got_entry **)htab_find_slot (g->got_entries, &entry, INSERT);
1629
1630   if (*loc)
1631     return TRUE;
1632
1633   entry.gotidx = g->local_gotno++;
1634
1635   *loc = bfd_alloc (abfd, sizeof(entry));
1636   if (! *loc)
1637     return FALSE;
1638
1639   memcpy (*loc, &entry, sizeof (entry));
1640
1641   return TRUE;
1642 }
1643
1644 /* Returns the GOT offset at which the indicated address can be found.
1645    If there is not yet a GOT entry for this value, create one.
1646    Returns -1 if no satisfactory GOT offset can be found.  */
1647
1648 static bfd_vma
1649 score_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
1650                           bfd_vma value, unsigned long r_symndx,
1651                           struct score_elf_link_hash_entry *h, int r_type)
1652 {
1653   asection *sgot;
1654   struct score_got_info *g;
1655   struct score_got_entry *entry;
1656
1657   g = score_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
1658
1659   entry = score_elf_create_local_got_entry (abfd, ibfd, g, sgot, value,
1660                                             r_symndx, h, r_type);
1661   if (!entry)
1662     return MINUS_ONE;
1663
1664   else
1665     return entry->gotidx;
1666 }
1667
1668 /* Returns the GOT index for the global symbol indicated by H.  */
1669
1670 static bfd_vma
1671 score_elf_global_got_index (bfd *abfd, struct elf_link_hash_entry *h)
1672 {
1673   bfd_vma index;
1674   asection *sgot;
1675   struct score_got_info *g;
1676   long global_got_dynindx = 0;
1677
1678   g = score_elf_got_info (abfd, &sgot);
1679   if (g->global_gotsym != NULL)
1680     global_got_dynindx = g->global_gotsym->dynindx;
1681
1682   /* Once we determine the global GOT entry with the lowest dynamic
1683      symbol table index, we must put all dynamic symbols with greater
1684      indices into the GOT.  That makes it easy to calculate the GOT
1685      offset.  */
1686   BFD_ASSERT (h->dynindx >= global_got_dynindx);
1687   index = ((h->dynindx - global_got_dynindx + g->local_gotno) * SCORE_ELF_GOT_SIZE (abfd));
1688   BFD_ASSERT (index < sgot->size);
1689
1690   return index;
1691 }
1692
1693 /* Returns the offset for the entry at the INDEXth position in the GOT.  */
1694
1695 static bfd_vma
1696 score_elf_got_offset_from_index (bfd *dynobj, bfd *output_bfd,
1697                                  bfd *input_bfd ATTRIBUTE_UNUSED, bfd_vma index)
1698 {
1699   asection *sgot;
1700   bfd_vma gp;
1701   struct score_got_info *g;
1702
1703   g = score_elf_got_info (dynobj, &sgot);
1704   gp = _bfd_get_gp_value (output_bfd);
1705
1706   return sgot->output_section->vma + sgot->output_offset + index - gp;
1707 }
1708
1709 /* Follow indirect and warning hash entries so that each got entry
1710    points to the final symbol definition.  P must point to a pointer
1711    to the hash table we're traversing.  Since this traversal may
1712    modify the hash table, we set this pointer to NULL to indicate
1713    we've made a potentially-destructive change to the hash table, so
1714    the traversal must be restarted.  */
1715 static int
1716 score_elf_resolve_final_got_entry (void **entryp, void *p)
1717 {
1718   struct score_got_entry *entry = (struct score_got_entry *)*entryp;
1719   htab_t got_entries = *(htab_t *)p;
1720
1721   if (entry->abfd != NULL && entry->symndx == -1)
1722     {
1723       struct score_elf_link_hash_entry *h = entry->d.h;
1724
1725       while (h->root.root.type == bfd_link_hash_indirect
1726              || h->root.root.type == bfd_link_hash_warning)
1727         h = (struct score_elf_link_hash_entry *) h->root.root.u.i.link;
1728
1729       if (entry->d.h == h)
1730         return 1;
1731
1732       entry->d.h = h;
1733
1734       /* If we can't find this entry with the new bfd hash, re-insert
1735          it, and get the traversal restarted.  */
1736       if (! htab_find (got_entries, entry))
1737         {
1738           htab_clear_slot (got_entries, entryp);
1739           entryp = htab_find_slot (got_entries, entry, INSERT);
1740           if (! *entryp)
1741             *entryp = entry;
1742           /* Abort the traversal, since the whole table may have
1743              moved, and leave it up to the parent to restart the
1744              process.  */
1745           *(htab_t *)p = NULL;
1746           return 0;
1747         }
1748       /* We might want to decrement the global_gotno count, but it's
1749          either too early or too late for that at this point.  */
1750     }
1751
1752   return 1;
1753 }
1754
1755 /* Turn indirect got entries in a got_entries table into their final locations.  */
1756 static void
1757 score_elf_resolve_final_got_entries (struct score_got_info *g)
1758 {
1759   htab_t got_entries;
1760
1761   do
1762     {
1763       got_entries = g->got_entries;
1764
1765       htab_traverse (got_entries,
1766                      score_elf_resolve_final_got_entry,
1767                      &got_entries);
1768     }
1769   while (got_entries == NULL);
1770 }
1771
1772 /* Add INCREMENT to the reloc (of type HOWTO) at ADDRESS. for -r  */
1773
1774 static void
1775 score_elf_add_to_rel (bfd *abfd,
1776                       bfd_byte *address,
1777                       reloc_howto_type *howto,
1778                       bfd_signed_vma increment)
1779 {
1780   bfd_signed_vma addend;
1781   bfd_vma contents;
1782   unsigned long offset;
1783   unsigned long r_type = howto->type;
1784   unsigned long hi16_addend, hi16_offset, hi16_value, uvalue;
1785
1786   contents = bfd_get_32 (abfd, address);
1787   /* Get the (signed) value from the instruction.  */
1788   addend = contents & howto->src_mask;
1789   if (addend & ((howto->src_mask + 1) >> 1))
1790     {
1791       bfd_signed_vma mask;
1792
1793       mask = -1;
1794       mask &= ~howto->src_mask;
1795       addend |= mask;
1796     }
1797   /* Add in the increment, (which is a byte value).  */
1798   switch (r_type)
1799     {
1800     case R_SCORE_PC19:
1801       offset =
1802         (((contents & howto->src_mask) & 0x3ff0000) >> 6) | ((contents & howto->src_mask) & 0x3ff);
1803       offset += increment;
1804       contents =
1805         (contents & ~howto->
1806          src_mask) | (((offset << 6) & howto->src_mask) & 0x3ff0000) | (offset & 0x3ff);
1807       bfd_put_32 (abfd, contents, address);
1808       break;
1809     case R_SCORE_HI16:
1810       break;
1811     case R_SCORE_LO16:
1812       hi16_addend = bfd_get_32 (abfd, address - 4);
1813       hi16_offset = ((((hi16_addend >> 16) & 0x3) << 15) | (hi16_addend & 0x7fff)) >> 1;
1814       offset = ((((contents >> 16) & 0x3) << 15) | (contents & 0x7fff)) >> 1;
1815       offset = (hi16_offset << 16) | (offset & 0xffff);
1816       uvalue = increment + offset;
1817       hi16_offset = (uvalue >> 16) << 1;
1818       hi16_value = (hi16_addend & (~(howto->dst_mask)))
1819         | (hi16_offset & 0x7fff) | ((hi16_offset << 1) & 0x30000);
1820       bfd_put_32 (abfd, hi16_value, address - 4);
1821       offset = (uvalue & 0xffff) << 1;
1822       contents = (contents & (~(howto->dst_mask))) | (offset & 0x7fff) | ((offset << 1) & 0x30000);
1823       bfd_put_32 (abfd, contents, address);
1824       break;
1825     case R_SCORE_24:
1826       offset =
1827         (((contents & howto->src_mask) >> 1) & 0x1ff8000) | ((contents & howto->src_mask) & 0x7fff);
1828       offset += increment;
1829       contents =
1830         (contents & ~howto->
1831          src_mask) | (((offset << 1) & howto->src_mask) & 0x3ff0000) | (offset & 0x7fff);
1832       bfd_put_32 (abfd, contents, address);
1833       break;
1834     case R_SCORE16_11:
1835
1836       contents = bfd_get_16 (abfd, address);
1837       offset = contents & howto->src_mask;
1838       offset += increment;
1839       contents = (contents & ~howto->src_mask) | (offset & howto->src_mask);
1840       bfd_put_16 (abfd, contents, address);
1841
1842       break;
1843     case R_SCORE16_PC8:
1844
1845       contents = bfd_get_16 (abfd, address);
1846       offset = (contents & howto->src_mask) + ((increment >> 1) & 0xff);
1847       contents = (contents & (~howto->src_mask)) | (offset & howto->src_mask);
1848       bfd_put_16 (abfd, contents, address);
1849
1850       break;
1851     default:
1852       addend += increment;
1853       contents = (contents & ~howto->dst_mask) | (addend & howto->dst_mask);
1854       bfd_put_32 (abfd, contents, address);
1855       break;
1856     }
1857 }
1858
1859 /* Perform a relocation as part of a final link.  */
1860
1861 static bfd_reloc_status_type
1862 score_elf_final_link_relocate (reloc_howto_type *howto,
1863                                bfd *input_bfd,
1864                                bfd *output_bfd,
1865                                asection *input_section,
1866                                bfd_byte *contents,
1867                                Elf_Internal_Rela *rel,
1868                                Elf_Internal_Rela *relocs,
1869                                bfd_vma symbol,
1870                                struct bfd_link_info *info,
1871                                asection *sym_sec,
1872                                const char *sym_name ATTRIBUTE_UNUSED,
1873                                int sym_flags ATTRIBUTE_UNUSED,
1874                                struct score_elf_link_hash_entry *h,
1875                                asection **local_sections,
1876                                bfd_boolean gp_disp_p)
1877 {
1878   unsigned long r_type;
1879   unsigned long r_symndx;
1880   bfd_byte *hit_data = contents + rel->r_offset;
1881   bfd_vma addend;
1882   /* The final GP value to be used for the relocatable, executable, or
1883      shared object file being produced.  */
1884   bfd_vma gp = MINUS_ONE;
1885   /* The place (section offset or address) of the storage unit being relocated.  */
1886   bfd_vma rel_addr;
1887   /* The value of GP used to create the relocatable object.  */
1888   bfd_vma gp0 = MINUS_ONE;
1889   /* The offset into the global offset table at which the address of the relocation entry
1890      symbol, adjusted by the addend, resides during execution.  */
1891   bfd_vma g = MINUS_ONE;
1892   /* TRUE if the symbol referred to by this relocation is a local symbol.  */
1893   bfd_boolean local_p;
1894   /* The eventual value we will relocate.  */
1895   bfd_vma value = symbol;
1896   unsigned long hi16_addend, hi16_offset, hi16_value, uvalue, offset, abs_value = 0;
1897
1898   if (elf_gp (output_bfd) == 0)
1899     {
1900       struct bfd_link_hash_entry *bh;
1901       asection *o;
1902
1903       bh = bfd_link_hash_lookup (info->hash, "_gp", 0, 0, 1);
1904       if (bh != (struct bfd_link_hash_entry *)NULL && bh->type == bfd_link_hash_defined)
1905         elf_gp (output_bfd) = (bh->u.def.value
1906                                + bh->u.def.section->output_section->vma
1907                                + bh->u.def.section->output_offset);
1908       else if (info->relocatable)
1909         {
1910           bfd_vma lo = -1;
1911
1912           /* Find the GP-relative section with the lowest offset.  */
1913           for (o = output_bfd->sections; o != (asection *) NULL; o = o->next)
1914             if (o->vma < lo)
1915               lo = o->vma;
1916           /* And calculate GP relative to that.  */
1917           elf_gp (output_bfd) = lo + ELF_SCORE_GP_OFFSET (input_bfd);
1918         }
1919       else
1920         {
1921           /* If the relocate_section function needs to do a reloc
1922              involving the GP value, it should make a reloc_dangerous
1923              callback to warn that GP is not defined.  */
1924         }
1925     }
1926
1927   /* Parse the relocation.  */
1928   r_symndx = ELF32_R_SYM (rel->r_info);
1929   r_type = ELF32_R_TYPE (rel->r_info);
1930   rel_addr = (input_section->output_section->vma + input_section->output_offset + rel->r_offset);
1931
1932   /* If the start address has been set, then set the EF_SCORE_HASENTRY
1933      flag.  Setting this more than once is redundant, but the cost is
1934      not too high, and it keeps the code simple.
1935      The test is done  here, rather than somewhere else, because the
1936      start address is only set just before the final link commences.
1937      Note - if the user deliberately sets a start address of 0, the flag will not be set.  */
1938   if (bfd_get_start_address (output_bfd) != 0)
1939     elf_elfheader (output_bfd)->e_flags |= EF_SCORE_HASENTRY;
1940
1941   if (r_type == R_SCORE_GOT15)
1942     {
1943       const Elf_Internal_Rela *relend;
1944       const Elf_Internal_Rela *lo16_rel;
1945       const struct elf_backend_data *bed;
1946       bfd_vma lo_value = 0;
1947
1948       addend = (bfd_get_32 (input_bfd, hit_data) >> howto->bitpos) & howto->src_mask;
1949
1950       bed = get_elf_backend_data (output_bfd);
1951       relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
1952       lo16_rel = score_elf_next_relocation (input_bfd, R_SCORE_GOT_LO16, rel, relend);
1953       if (lo16_rel != NULL)
1954         {
1955           lo_value = (bfd_get_32 (input_bfd, contents + lo16_rel->r_offset) >> howto->bitpos)
1956                       & howto->src_mask;
1957         }
1958       addend = (addend << 16) + lo_value;
1959     }
1960   else
1961     {
1962       addend = (bfd_get_32 (input_bfd, hit_data) >> howto->bitpos) & howto->src_mask;
1963     }
1964
1965   local_p = score_elf_local_relocation_p (input_bfd, rel, local_sections, TRUE);
1966
1967   /* If we haven't already determined the GOT offset, or the GP value,
1968      and we're going to need it, get it now.  */
1969   switch (r_type)
1970     {
1971     case R_SCORE_CALL15:
1972     case R_SCORE_GOT15:
1973       if (!local_p)
1974         {
1975           g = score_elf_global_got_index (elf_hash_table (info)->dynobj,
1976                                           (struct elf_link_hash_entry *) h);
1977         }
1978       else if (r_type == R_SCORE_GOT15 || r_type == R_SCORE_CALL15)
1979         {
1980           /* There's no need to create a local GOT entry here; the
1981              calculation for a local GOT15 entry does not involve G.  */
1982           ;
1983         }
1984       else
1985         {
1986           g = score_elf_local_got_index (output_bfd, input_bfd, info,
1987                                          symbol + addend, r_symndx, h, r_type);
1988           if (g == MINUS_ONE)
1989             return bfd_reloc_outofrange;
1990         }
1991
1992       /* Convert GOT indices to actual offsets.  */
1993       g = score_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
1994                                            output_bfd, input_bfd, g);
1995       break;
1996
1997     case R_SCORE_HI16:
1998     case R_SCORE_LO16:
1999     case R_SCORE_GPREL32:
2000       gp0 = _bfd_get_gp_value (input_bfd);
2001       gp = _bfd_get_gp_value (output_bfd);
2002       break;
2003
2004     case R_SCORE_GP15:
2005       gp = _bfd_get_gp_value (output_bfd);
2006
2007     default:
2008       break;
2009     }
2010
2011   switch (r_type)
2012     {
2013     case R_SCORE_NONE:
2014       return bfd_reloc_ok;
2015
2016     case R_SCORE_ABS32:
2017     case R_SCORE_REL32:
2018       if ((info->shared
2019            || (elf_hash_table (info)->dynamic_sections_created
2020                && h != NULL
2021                && h->root.def_dynamic
2022                && !h->root.def_regular))
2023            && r_symndx != 0
2024            && (input_section->flags & SEC_ALLOC) != 0)
2025         {
2026           /* If we're creating a shared library, or this relocation is against a symbol
2027              in a shared library, then we can't know where the symbol will end up.
2028              So, we create a relocation record in the output, and leave the job up
2029              to the dynamic linker.  */
2030           value = addend;
2031           if (!score_elf_create_dynamic_relocation (output_bfd, info, rel, h,
2032                                                     sym_sec, symbol, &value,
2033                                                     input_section))
2034             return bfd_reloc_undefined;
2035         }
2036       else
2037         {
2038           if (r_type != R_SCORE_REL32)
2039             value = symbol + addend;
2040           else
2041             value = addend;
2042         }
2043       value &= howto->dst_mask;
2044       bfd_put_32 (input_bfd, value, hit_data);
2045       return bfd_reloc_ok;
2046
2047     case R_SCORE_ABS16:
2048       value += addend;
2049       if ((long)value > 0x7fff || (long)value < -0x8000)
2050         return bfd_reloc_overflow;
2051       bfd_put_16 (input_bfd, value, hit_data);
2052       return bfd_reloc_ok;
2053
2054     case R_SCORE_24:
2055       addend = bfd_get_32 (input_bfd, hit_data);
2056       offset = (((addend & howto->src_mask) >> 1) & 0x1ff8000) | ((addend & howto->src_mask) & 0x7fff);
2057       if ((offset & 0x1000000) != 0)
2058         offset |= 0xfe000000;
2059       value += offset;
2060       addend = (addend & ~howto->src_mask)
2061                 | (((value << 1) & howto->src_mask) & 0x3ff0000) | (value & 0x7fff);
2062       bfd_put_32 (input_bfd, addend, hit_data);
2063       return bfd_reloc_ok;
2064
2065     case R_SCORE_PC19:
2066       addend = bfd_get_32 (input_bfd, hit_data);
2067       offset = (((addend & howto->src_mask) & 0x3ff0000) >> 6) | ((addend & howto->src_mask) & 0x3ff);
2068       if ((offset & 0x80000) != 0)
2069         offset |= 0xfff00000;
2070       abs_value = value = value - rel_addr + offset;
2071       /* exceed 20 bit : overflow.  */
2072       if ((abs_value & 0x80000000) == 0x80000000)
2073         abs_value = 0xffffffff - value + 1;
2074       if ((abs_value & 0xfff80000) != 0)
2075         return bfd_reloc_overflow;
2076       addend = (addend & ~howto->src_mask)
2077                 | (((value << 6) & howto->src_mask) & 0x3ff0000) | (value & 0x3ff);
2078       bfd_put_32 (input_bfd, addend, hit_data);
2079       return bfd_reloc_ok;
2080
2081     case R_SCORE16_11:
2082       addend = bfd_get_16 (input_bfd, hit_data);
2083       offset = addend & howto->src_mask;
2084       if ((offset & 0x800) != 0)        /* Offset is negative.  */
2085         offset |= 0xfffff000;
2086       value += offset;
2087       addend = (addend & ~howto->src_mask) | (value & howto->src_mask);
2088       bfd_put_16 (input_bfd, addend, hit_data);
2089       return bfd_reloc_ok;
2090
2091     case R_SCORE16_PC8:
2092       addend = bfd_get_16 (input_bfd, hit_data);
2093       offset = (addend & howto->src_mask) << 1;
2094       if ((offset & 0x100) != 0)        /* Offset is negative.  */
2095         offset |= 0xfffffe00;
2096       abs_value = value = value - rel_addr + offset;
2097       /* Sign bit + exceed 9 bit.  */
2098       if (((value & 0xffffff00) != 0) && ((value & 0xffffff00) != 0xffffff00))
2099         return bfd_reloc_overflow;
2100       value >>= 1;
2101       addend = (addend & ~howto->src_mask) | (value & howto->src_mask);
2102       bfd_put_16 (input_bfd, addend, hit_data);
2103       return bfd_reloc_ok;
2104
2105     case R_SCORE_HI16:
2106       return bfd_reloc_ok;
2107
2108     case R_SCORE_LO16:
2109       hi16_addend = bfd_get_32 (input_bfd, hit_data - 4);
2110       hi16_offset = ((((hi16_addend >> 16) & 0x3) << 15) | (hi16_addend & 0x7fff)) >> 1;
2111       addend = bfd_get_32 (input_bfd, hit_data);
2112       offset = ((((addend >> 16) & 0x3) << 15) | (addend & 0x7fff)) >> 1;
2113       offset = (hi16_offset << 16) | (offset & 0xffff);
2114
2115       if (!gp_disp_p)
2116         uvalue = value + offset;
2117       else
2118         uvalue = offset + gp - rel_addr + 4;
2119
2120       hi16_offset = (uvalue >> 16) << 1;
2121       hi16_value = (hi16_addend & (~(howto->dst_mask)))
2122                         | (hi16_offset & 0x7fff) | ((hi16_offset << 1) & 0x30000);
2123       bfd_put_32 (input_bfd, hi16_value, hit_data - 4);
2124       offset = (uvalue & 0xffff) << 1;
2125       value = (addend & (~(howto->dst_mask))) | (offset & 0x7fff) | ((offset << 1) & 0x30000);
2126       bfd_put_32 (input_bfd, value, hit_data);
2127       return bfd_reloc_ok;
2128
2129     case R_SCORE_GP15:
2130       addend = bfd_get_32 (input_bfd, hit_data);
2131       offset = addend & 0x7fff;
2132       if ((offset & 0x4000) == 0x4000)
2133         offset |= 0xffffc000;
2134       value = value + offset - gp;
2135       if (((value & 0xffffc000) != 0) && ((value & 0xffffc000) != 0xffffc000))
2136         return bfd_reloc_overflow;
2137       value = (addend & ~howto->src_mask) | (value & howto->src_mask);
2138       bfd_put_32 (input_bfd, value, hit_data);
2139       return bfd_reloc_ok;
2140
2141     case R_SCORE_GOT15:
2142     case R_SCORE_CALL15:
2143       if (local_p)
2144         {
2145           bfd_boolean forced;
2146
2147           /* The special case is when the symbol is forced to be local.  We need the
2148              full address in the GOT since no R_SCORE_GOT_LO16 relocation follows.  */
2149           forced = ! score_elf_local_relocation_p (input_bfd, rel,
2150                                                    local_sections, FALSE);
2151           value = score_elf_got16_entry (output_bfd, input_bfd, info,
2152                                          symbol + addend, forced);
2153           if (value == MINUS_ONE)
2154             return bfd_reloc_outofrange;
2155           value = score_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
2156                                                    output_bfd, input_bfd, value);
2157         }
2158       else
2159         {
2160           value = g;
2161         }
2162
2163       if ((long) value > 0x3fff || (long) value < -0x4000)
2164         return bfd_reloc_overflow;
2165       bfd_put_16 (input_bfd, value, hit_data + 2);
2166       return bfd_reloc_ok;
2167
2168     case R_SCORE_GPREL32:
2169       value = (addend + symbol - gp);
2170       value &= howto->dst_mask;
2171       bfd_put_32 (input_bfd, value, hit_data);
2172       return bfd_reloc_ok;
2173
2174     case R_SCORE_GOT_LO16:
2175       addend = bfd_get_32 (input_bfd, hit_data);
2176       value = ((((addend >> 16) & 0x3) << 15) | (addend & 0x7fff)) >> 1;
2177       value += symbol;
2178       offset = (value & 0xffff) << 1;
2179       value = (addend & (~(howto->dst_mask))) | (offset & 0x7fff) | ((offset << 1) & 0x30000);
2180
2181       bfd_put_32 (input_bfd, value, hit_data);
2182       return bfd_reloc_ok;
2183
2184     case R_SCORE_DUMMY_HI16:
2185       return bfd_reloc_ok;
2186
2187     case R_SCORE_GNU_VTINHERIT:
2188     case R_SCORE_GNU_VTENTRY:
2189       /* We don't do anything with these at present.  */
2190       return bfd_reloc_continue;
2191
2192     default:
2193       return bfd_reloc_notsupported;
2194     }
2195 }
2196
2197 /* Score backend functions.  */
2198
2199 static void
2200 _bfd_score_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
2201                           arelent *bfd_reloc,
2202                           Elf_Internal_Rela *elf_reloc)
2203 {
2204   unsigned int r_type;
2205
2206   r_type = ELF32_R_TYPE (elf_reloc->r_info);
2207   if (r_type >= NUM_ELEM (elf32_score_howto_table))
2208     bfd_reloc->howto = NULL;
2209   else
2210     bfd_reloc->howto = &elf32_score_howto_table[r_type];
2211 }
2212
2213 /* Relocate an score ELF section.  */
2214
2215 static bfd_boolean
2216 _bfd_score_elf_relocate_section (bfd *output_bfd,
2217                                  struct bfd_link_info *info,
2218                                  bfd *input_bfd,
2219                                  asection *input_section,
2220                                  bfd_byte *contents,
2221                                  Elf_Internal_Rela *relocs,
2222                                  Elf_Internal_Sym *local_syms,
2223                                  asection **local_sections)
2224 {
2225   Elf_Internal_Shdr *symtab_hdr;
2226   struct elf_link_hash_entry **sym_hashes;
2227   Elf_Internal_Rela *rel;
2228   Elf_Internal_Rela *relend;
2229   const char *name;
2230   unsigned long offset;
2231   unsigned long hi16_addend, hi16_offset, hi16_value, uvalue;
2232   size_t extsymoff;
2233   bfd_boolean gp_disp_p = FALSE;
2234
2235 #ifndef USE_REL
2236   if (info->relocatable)
2237     return TRUE;
2238 #endif
2239
2240   /* Sort dynsym.  */
2241   if (elf_hash_table (info)->dynamic_sections_created)
2242     {
2243       bfd_size_type dynsecsymcount = 0;
2244       if (info->shared)
2245         {
2246           asection * p;
2247           const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
2248
2249           for (p = output_bfd->sections; p ; p = p->next)
2250             if ((p->flags & SEC_EXCLUDE) == 0
2251                 && (p->flags & SEC_ALLOC) != 0
2252                 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
2253               ++ dynsecsymcount;
2254         }
2255
2256       if (!score_elf_sort_hash_table (info, dynsecsymcount + 1))
2257         return FALSE;
2258     }
2259
2260   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2261   extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
2262   sym_hashes = elf_sym_hashes (input_bfd);
2263   rel = relocs;
2264   relend = relocs + input_section->reloc_count;
2265   for (; rel < relend; rel++)
2266     {
2267       int r_type;
2268       reloc_howto_type *howto;
2269       unsigned long r_symndx;
2270       Elf_Internal_Sym *sym;
2271       asection *sec;
2272       struct score_elf_link_hash_entry *h;
2273       bfd_vma relocation = 0;
2274       bfd_reloc_status_type r;
2275       arelent bfd_reloc;
2276
2277       r_symndx = ELF32_R_SYM (rel->r_info);
2278       r_type = ELF32_R_TYPE (rel->r_info);
2279
2280       _bfd_score_info_to_howto (input_bfd, &bfd_reloc, (Elf_Internal_Rela *) rel);
2281       howto = bfd_reloc.howto;
2282
2283       if (info->relocatable)
2284         {
2285           /* This is a relocatable link.  We don't have to change
2286              anything, unless the reloc is against a section symbol,
2287              in which case we have to adjust according to where the
2288              section symbol winds up in the output section.  */
2289           if (r_symndx < symtab_hdr->sh_info)
2290             {
2291               sym = local_syms + r_symndx;
2292               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2293                 {
2294                   sec = local_sections[r_symndx];
2295                   score_elf_add_to_rel (input_bfd, contents + rel->r_offset,
2296                                     howto, (bfd_signed_vma) (sec->output_offset + sym->st_value));
2297                 }
2298             }
2299           continue;
2300         }
2301
2302       /* This is a final link.  */
2303       h = NULL;
2304       sym = NULL;
2305       sec = NULL;
2306
2307       if (r_symndx < extsymoff)
2308         {
2309           sym = local_syms + r_symndx;
2310           sec = local_sections[r_symndx];
2311           relocation = (sec->output_section->vma
2312                         + sec->output_offset
2313                         + sym->st_value);
2314           name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
2315
2316           if ((sec->flags & SEC_MERGE)
2317               && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2318             {
2319               asection *msec;
2320               bfd_vma addend, value;
2321
2322               switch (r_type)
2323                 {
2324                 case R_SCORE_HI16:
2325                   break;
2326                 case R_SCORE_LO16:
2327                   hi16_addend = bfd_get_32 (input_bfd, contents + rel->r_offset - 4);
2328                   hi16_offset = ((((hi16_addend >> 16) & 0x3) << 15) | (hi16_addend & 0x7fff)) >> 1;
2329                   value = bfd_get_32 (input_bfd, contents + rel->r_offset);
2330                   offset = ((((value >> 16) & 0x3) << 15) | (value & 0x7fff)) >> 1;
2331                   addend = (hi16_offset << 16) | (offset & 0xffff);
2332                   msec = sec;
2333                   addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend);
2334                   addend -= relocation;
2335                   addend += msec->output_section->vma + msec->output_offset;
2336                   uvalue = addend;
2337                   hi16_offset = (uvalue >> 16) << 1;
2338                   hi16_value = (hi16_addend & (~(howto->dst_mask)))
2339                     | (hi16_offset & 0x7fff) | ((hi16_offset << 1) & 0x30000);
2340                   bfd_put_32 (input_bfd, hi16_value, contents + rel->r_offset - 4);
2341                   offset = (uvalue & 0xffff) << 1;
2342                   value = (value & (~(howto->dst_mask)))
2343                     | (offset & 0x7fff) | ((offset << 1) & 0x30000);
2344                   bfd_put_32 (input_bfd, value, contents + rel->r_offset);
2345                   break;
2346                 default:
2347                   value = bfd_get_32 (input_bfd, contents + rel->r_offset);
2348                   /* Get the (signed) value from the instruction.  */
2349                   addend = value & howto->src_mask;
2350                   if (addend & ((howto->src_mask + 1) >> 1))
2351                     {
2352                       bfd_signed_vma mask;
2353
2354                       mask = -1;
2355                       mask &= ~howto->src_mask;
2356                       addend |= mask;
2357                     }
2358                   msec = sec;
2359                   addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend) - relocation;
2360                   addend += msec->output_section->vma + msec->output_offset;
2361                   value = (value & ~howto->dst_mask) | (addend & howto->dst_mask);
2362                   bfd_put_32 (input_bfd, value, contents + rel->r_offset);
2363                   break;
2364                 }
2365             }
2366         }
2367       else
2368         {
2369           /* For global symbols we look up the symbol in the hash-table.  */
2370           h = ((struct score_elf_link_hash_entry *)
2371                elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
2372           /* Find the real hash-table entry for this symbol.  */
2373           while (h->root.root.type == bfd_link_hash_indirect
2374                  || h->root.root.type == bfd_link_hash_warning)
2375             h = (struct score_elf_link_hash_entry *) h->root.root.u.i.link;
2376
2377           /* Record the name of this symbol, for our caller.  */
2378           name = h->root.root.root.string;
2379
2380           /* See if this is the special GP_DISP_LABEL symbol.  Note that such a
2381              symbol must always be a global symbol.  */
2382           if (strcmp (name, GP_DISP_LABEL) == 0)
2383             {
2384               /* Relocations against GP_DISP_LABEL are permitted only with
2385                  R_SCORE_HI16 and R_SCORE_LO16 relocations.  */
2386               if (r_type != R_SCORE_HI16 && r_type != R_SCORE_LO16)
2387                 return bfd_reloc_notsupported;
2388
2389               gp_disp_p = TRUE;
2390             }
2391
2392           /* If this symbol is defined, calculate its address.  Note that
2393               GP_DISP_LABEL is a magic symbol, always implicitly defined by the
2394               linker, so it's inappropriate to check to see whether or not
2395               its defined.  */
2396           else if ((h->root.root.type == bfd_link_hash_defined
2397                     || h->root.root.type == bfd_link_hash_defweak)
2398                    && h->root.root.u.def.section)
2399             {
2400               sec = h->root.root.u.def.section;
2401               if (sec->output_section)
2402                 relocation = (h->root.root.u.def.value
2403                               + sec->output_section->vma
2404                               + sec->output_offset);
2405               else
2406                 {
2407                   relocation = h->root.root.u.def.value;
2408                 }
2409             }
2410           else if (h->root.root.type == bfd_link_hash_undefweak)
2411             /* We allow relocations against undefined weak symbols, giving
2412                it the value zero, so that you can undefined weak functions
2413                and check to see if they exist by looking at their addresses.  */
2414             relocation = 0;
2415           else if (info->unresolved_syms_in_objects == RM_IGNORE
2416                    && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
2417             relocation = 0;
2418           else if (strcmp (name, "_DYNAMIC_LINK") == 0)
2419             {
2420               /* If this is a dynamic link, we should have created a _DYNAMIC_LINK symbol
2421                  in _bfd_score_elf_create_dynamic_sections.  Otherwise, we should define
2422                  the symbol with a value of 0.  */
2423               BFD_ASSERT (! info->shared);
2424               BFD_ASSERT (bfd_get_section_by_name (output_bfd, ".dynamic") == NULL);
2425               relocation = 0;
2426             }
2427           else
2428             {
2429               if (! ((*info->callbacks->undefined_symbol)
2430                      (info, h->root.root.root.string, input_bfd,
2431                       input_section, rel->r_offset,
2432                       (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
2433                       || ELF_ST_VISIBILITY (h->root.other))))
2434                 return bfd_reloc_undefined;
2435               relocation = 0;
2436             }
2437         }
2438
2439       r = score_elf_final_link_relocate (howto, input_bfd, output_bfd,
2440                                          input_section, contents, rel, relocs,
2441                                          relocation, info, sec, name,
2442                                          (h ? ELF_ST_TYPE ((unsigned int)h->root.root.type) :
2443                                          ELF_ST_TYPE ((unsigned int)sym->st_info)), h, local_sections,
2444                                          gp_disp_p);
2445
2446       if (r != bfd_reloc_ok)
2447         {
2448           const char *msg = (const char *)0;
2449
2450           switch (r)
2451             {
2452             case bfd_reloc_overflow:
2453               /* If the overflowing reloc was to an undefined symbol,
2454                  we have already printed one error message and there
2455                  is no point complaining again.  */
2456               if (((!h) || (h->root.root.type != bfd_link_hash_undefined))
2457                   && (!((*info->callbacks->reloc_overflow)
2458                         (info, NULL, name, howto->name, (bfd_vma) 0,
2459                          input_bfd, input_section, rel->r_offset))))
2460                 return FALSE;
2461               break;
2462             case bfd_reloc_undefined:
2463               if (!((*info->callbacks->undefined_symbol)
2464                     (info, name, input_bfd, input_section, rel->r_offset, TRUE)))
2465                 return FALSE;
2466               break;
2467
2468             case bfd_reloc_outofrange:
2469               msg = _("internal error: out of range error");
2470               goto common_error;
2471
2472             case bfd_reloc_notsupported:
2473               msg = _("internal error: unsupported relocation error");
2474               goto common_error;
2475
2476             case bfd_reloc_dangerous:
2477               msg = _("internal error: dangerous error");
2478               goto common_error;
2479
2480             default:
2481               msg = _("internal error: unknown error");
2482               /* fall through */
2483
2484             common_error:
2485               if (!((*info->callbacks->warning)
2486                     (info, msg, name, input_bfd, input_section, rel->r_offset)))
2487                 return FALSE;
2488               break;
2489             }
2490         }
2491     }
2492
2493   return TRUE;
2494 }
2495
2496 /* Look through the relocs for a section during the first phase, and
2497    allocate space in the global offset table.  */
2498
2499 static bfd_boolean
2500 _bfd_score_elf_check_relocs (bfd *abfd,
2501                              struct bfd_link_info *info,
2502                              asection *sec,
2503                              const Elf_Internal_Rela *relocs)
2504 {
2505   const char *name;
2506   bfd *dynobj;
2507   Elf_Internal_Shdr *symtab_hdr;
2508   struct elf_link_hash_entry **sym_hashes;
2509   struct score_got_info *g;
2510   size_t extsymoff;
2511   const Elf_Internal_Rela *rel;
2512   const Elf_Internal_Rela *rel_end;
2513   asection *sgot;
2514   asection *sreloc;
2515   const struct elf_backend_data *bed;
2516
2517   if (info->relocatable)
2518     return TRUE;
2519
2520   dynobj = elf_hash_table (info)->dynobj;
2521   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2522   sym_hashes = elf_sym_hashes (abfd);
2523   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
2524
2525   name = bfd_get_section_name (abfd, sec);
2526
2527   if (dynobj == NULL)
2528     {
2529       sgot = NULL;
2530       g = NULL;
2531     }
2532   else
2533     {
2534       sgot = score_elf_got_section (dynobj, FALSE);
2535       if (sgot == NULL)
2536         g = NULL;
2537       else
2538         {
2539           BFD_ASSERT (score_elf_section_data (sgot) != NULL);
2540           g = score_elf_section_data (sgot)->u.got_info;
2541           BFD_ASSERT (g != NULL);
2542         }
2543     }
2544
2545   sreloc = NULL;
2546   bed = get_elf_backend_data (abfd);
2547   rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
2548   for (rel = relocs; rel < rel_end; ++rel)
2549     {
2550       unsigned long r_symndx;
2551       unsigned int r_type;
2552       struct elf_link_hash_entry *h;
2553
2554       r_symndx = ELF32_R_SYM (rel->r_info);
2555       r_type = ELF32_R_TYPE (rel->r_info);
2556
2557       if (r_symndx < extsymoff)
2558         {
2559           h = NULL;
2560         }
2561       else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
2562         {
2563           (*_bfd_error_handler) (_("%s: Malformed reloc detected for section %s"), abfd, name);
2564           bfd_set_error (bfd_error_bad_value);
2565           return FALSE;
2566         }
2567       else
2568         {
2569           h = sym_hashes[r_symndx - extsymoff];
2570
2571           /* This may be an indirect symbol created because of a version.  */
2572           if (h != NULL)
2573             {
2574               while (h->root.type == bfd_link_hash_indirect)
2575                 h = (struct elf_link_hash_entry *)h->root.u.i.link;
2576             }
2577         }
2578
2579       /* Some relocs require a global offset table.  */
2580       if (dynobj == NULL || sgot == NULL)
2581         {
2582           switch (r_type)
2583             {
2584             case R_SCORE_GOT15:
2585             case R_SCORE_CALL15:
2586               if (dynobj == NULL)
2587                 elf_hash_table (info)->dynobj = dynobj = abfd;
2588               if (!score_elf_create_got_section (dynobj, info, FALSE))
2589                 return FALSE;
2590               g = score_elf_got_info (dynobj, &sgot);
2591               break;
2592             case R_SCORE_ABS32:
2593             case R_SCORE_REL32:
2594               if (dynobj == NULL && (info->shared || h != NULL) && (sec->flags & SEC_ALLOC) != 0)
2595                 elf_hash_table (info)->dynobj = dynobj = abfd;
2596               break;
2597             default:
2598               break;
2599             }
2600         }
2601
2602       if (!h && (r_type == R_SCORE_GOT_LO16))
2603         {
2604           if (! score_elf_record_local_got_symbol (abfd, r_symndx, rel->r_addend, g))
2605             return FALSE;
2606         }
2607
2608       switch (r_type)
2609         {
2610         case R_SCORE_CALL15:
2611           if (h == NULL)
2612             {
2613               (*_bfd_error_handler)
2614                 (_("%B: CALL15 reloc at 0x%lx not against global symbol"),
2615                  abfd, (unsigned long) rel->r_offset);
2616               bfd_set_error (bfd_error_bad_value);
2617               return FALSE;
2618             }
2619           else
2620             {
2621               /* This symbol requires a global offset table entry.  */
2622               if (! score_elf_record_global_got_symbol (h, abfd, info, g))
2623                 return FALSE;
2624
2625               /* We need a stub, not a plt entry for the undefined function.  But we record
2626                  it as if it needs plt.  See _bfd_elf_adjust_dynamic_symbol.  */
2627               h->needs_plt = 1;
2628               h->type = STT_FUNC;
2629             }
2630           break;
2631         case R_SCORE_GOT15:
2632           if (h && ! score_elf_record_global_got_symbol (h, abfd, info, g))
2633             return FALSE;
2634           break;
2635         case R_SCORE_ABS32:
2636         case R_SCORE_REL32:
2637           if ((info->shared || h != NULL) && (sec->flags & SEC_ALLOC) != 0)
2638             {
2639               if (sreloc == NULL)
2640                 {
2641                   sreloc = score_elf_rel_dyn_section (dynobj, TRUE);
2642                   if (sreloc == NULL)
2643                     return FALSE;
2644                 }
2645 #define SCORE_READONLY_SECTION (SEC_ALLOC | SEC_LOAD | SEC_READONLY)
2646               if (info->shared)
2647                 {
2648                   /* When creating a shared object, we must copy these reloc types into
2649                      the output file as R_SCORE_REL32 relocs.  We make room for this reloc
2650                      in the .rel.dyn reloc section.  */
2651                   score_elf_allocate_dynamic_relocations (dynobj, 1);
2652                   if ((sec->flags & SCORE_READONLY_SECTION)
2653                       == SCORE_READONLY_SECTION)
2654                     /* We tell the dynamic linker that there are
2655                        relocations against the text segment.  */
2656                     info->flags |= DF_TEXTREL;
2657                 }
2658               else
2659                 {
2660                   struct score_elf_link_hash_entry *hscore;
2661
2662                   /* We only need to copy this reloc if the symbol is
2663                      defined in a dynamic object.  */
2664                   hscore = (struct score_elf_link_hash_entry *)h;
2665                   ++hscore->possibly_dynamic_relocs;
2666                   if ((sec->flags & SCORE_READONLY_SECTION)
2667                       == SCORE_READONLY_SECTION)
2668                     /* We need it to tell the dynamic linker if there
2669                        are relocations against the text segment.  */
2670                     hscore->readonly_reloc = TRUE;
2671                 }
2672
2673               /* Even though we don't directly need a GOT entry for this symbol,
2674                  a symbol must have a dynamic symbol table index greater that
2675                  DT_SCORE_GOTSYM if there are dynamic relocations against it.  */
2676               if (h != NULL)
2677                 {
2678                   if (dynobj == NULL)
2679                     elf_hash_table (info)->dynobj = dynobj = abfd;
2680                   if (! score_elf_create_got_section (dynobj, info, TRUE))
2681                     return FALSE;
2682                   g = score_elf_got_info (dynobj, &sgot);
2683                   if (! score_elf_record_global_got_symbol (h, abfd, info, g))
2684                     return FALSE;
2685                 }
2686             }
2687           break;
2688
2689           /* This relocation describes the C++ object vtable hierarchy.
2690              Reconstruct it for later use during GC.  */
2691         case R_SCORE_GNU_VTINHERIT:
2692           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2693             return FALSE;
2694           break;
2695
2696           /* This relocation describes which C++ vtable entries are actually
2697              used.  Record for later use during GC.  */
2698         case R_SCORE_GNU_VTENTRY:
2699           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
2700             return FALSE;
2701           break;
2702         default:
2703           break;
2704         }
2705
2706       /* We must not create a stub for a symbol that has relocations
2707          related to taking the function's address.  */
2708       switch (r_type)
2709         {
2710         default:
2711           if (h != NULL)
2712             {
2713               struct score_elf_link_hash_entry *sh;
2714
2715               sh = (struct score_elf_link_hash_entry *) h;
2716               sh->no_fn_stub = TRUE;
2717             }
2718           break;
2719         case R_SCORE_CALL15:
2720           break;
2721         }
2722     }
2723
2724   return TRUE;
2725 }
2726
2727 static bfd_boolean
2728 _bfd_score_elf_add_symbol_hook (bfd *abfd,
2729                                 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2730                                 Elf_Internal_Sym *sym,
2731                                 const char **namep ATTRIBUTE_UNUSED,
2732                                 flagword *flagsp ATTRIBUTE_UNUSED,
2733                                 asection **secp,
2734                                 bfd_vma *valp)
2735 {
2736   switch (sym->st_shndx)
2737     {
2738     case SHN_COMMON:
2739       if (sym->st_size > elf_gp_size (abfd))
2740         break;
2741       /* Fall through.  */
2742     case SHN_SCORE_SCOMMON:
2743       *secp = bfd_make_section_old_way (abfd, ".scommon");
2744       (*secp)->flags |= SEC_IS_COMMON;
2745       *valp = sym->st_size;
2746       break;
2747     }
2748
2749   return TRUE;
2750 }
2751
2752 static void
2753 _bfd_score_elf_symbol_processing (bfd *abfd, asymbol *asym)
2754 {
2755   elf_symbol_type *elfsym;
2756
2757   elfsym = (elf_symbol_type *) asym;
2758   switch (elfsym->internal_elf_sym.st_shndx)
2759     {
2760     case SHN_COMMON:
2761       if (asym->value > elf_gp_size (abfd))
2762         break;
2763       /* Fall through.  */
2764     case SHN_SCORE_SCOMMON:
2765       if (score_elf_scom_section.name == NULL)
2766         {
2767           /* Initialize the small common section.  */
2768           score_elf_scom_section.name = ".scommon";
2769           score_elf_scom_section.flags = SEC_IS_COMMON;
2770           score_elf_scom_section.output_section = &score_elf_scom_section;
2771           score_elf_scom_section.symbol = &score_elf_scom_symbol;
2772           score_elf_scom_section.symbol_ptr_ptr = &score_elf_scom_symbol_ptr;
2773           score_elf_scom_symbol.name = ".scommon";
2774           score_elf_scom_symbol.flags = BSF_SECTION_SYM;
2775           score_elf_scom_symbol.section = &score_elf_scom_section;
2776           score_elf_scom_symbol_ptr = &score_elf_scom_symbol;
2777         }
2778       asym->section = &score_elf_scom_section;
2779       asym->value = elfsym->internal_elf_sym.st_size;
2780       break;
2781     }
2782 }
2783
2784 static bfd_boolean
2785 _bfd_score_elf_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
2786      const char *name ATTRIBUTE_UNUSED,
2787      Elf_Internal_Sym *sym,
2788      asection *input_sec,
2789      struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
2790 {
2791   /* If we see a common symbol, which implies a relocatable link, then
2792      if a symbol was small common in an input file, mark it as small
2793      common in the output file.  */
2794   if (sym->st_shndx == SHN_COMMON && strcmp (input_sec->name, ".scommon") == 0)
2795     sym->st_shndx = SHN_SCORE_SCOMMON;
2796
2797   return TRUE;
2798 }
2799
2800 static bfd_boolean
2801 _bfd_score_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
2802                                          asection *sec,
2803                                          int *retval)
2804 {
2805   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2806     {
2807       *retval = SHN_SCORE_SCOMMON;
2808       return TRUE;
2809     }
2810
2811   return FALSE;
2812 }
2813
2814 /* Adjust a symbol defined by a dynamic object and referenced by a
2815    regular object.  The current definition is in some section of the
2816    dynamic object, but we're not including those sections.  We have to
2817    change the definition to something the rest of the link can understand.  */
2818
2819 static bfd_boolean
2820 _bfd_score_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2821                                       struct elf_link_hash_entry *h)
2822 {
2823   bfd *dynobj;
2824   struct score_elf_link_hash_entry *hscore;
2825   asection *s;
2826
2827   dynobj = elf_hash_table (info)->dynobj;
2828
2829   /* Make sure we know what is going on here.  */
2830   BFD_ASSERT (dynobj != NULL
2831               && (h->needs_plt
2832                   || h->u.weakdef != NULL
2833                   || (h->def_dynamic && h->ref_regular && !h->def_regular)));
2834
2835   /* If this symbol is defined in a dynamic object, we need to copy
2836      any R_SCORE_ABS32 or R_SCORE_REL32 relocs against it into the output
2837      file.  */
2838   hscore = (struct score_elf_link_hash_entry *)h;
2839   if (!info->relocatable
2840       && hscore->possibly_dynamic_relocs != 0
2841       && (h->root.type == bfd_link_hash_defweak || !h->def_regular))
2842     {
2843       score_elf_allocate_dynamic_relocations (dynobj, hscore->possibly_dynamic_relocs);
2844       if (hscore->readonly_reloc)
2845         /* We tell the dynamic linker that there are relocations
2846            against the text segment.  */
2847         info->flags |= DF_TEXTREL;
2848     }
2849
2850   /* For a function, create a stub, if allowed.  */
2851   if (!hscore->no_fn_stub && h->needs_plt)
2852     {
2853       if (!elf_hash_table (info)->dynamic_sections_created)
2854         return TRUE;
2855
2856       /* If this symbol is not defined in a regular file, then set
2857          the symbol to the stub location.  This is required to make
2858          function pointers compare as equal between the normal
2859          executable and the shared library.  */
2860       if (!h->def_regular)
2861         {
2862           /* We need .stub section.  */
2863           s = bfd_get_section_by_name (dynobj, SCORE_ELF_STUB_SECTION_NAME);
2864           BFD_ASSERT (s != NULL);
2865
2866           h->root.u.def.section = s;
2867           h->root.u.def.value = s->size;
2868
2869           /* XXX Write this stub address somewhere.  */
2870           h->plt.offset = s->size;
2871
2872           /* Make room for this stub code.  */
2873           s->size += SCORE_FUNCTION_STUB_SIZE;
2874
2875           /* The last half word of the stub will be filled with the index
2876              of this symbol in .dynsym section.  */
2877           return TRUE;
2878         }
2879     }
2880   else if ((h->type == STT_FUNC) && !h->needs_plt)
2881     {
2882       /* This will set the entry for this symbol in the GOT to 0, and
2883          the dynamic linker will take care of this.  */
2884       h->root.u.def.value = 0;
2885       return TRUE;
2886     }
2887
2888   /* If this is a weak symbol, and there is a real definition, the
2889      processor independent code will have arranged for us to see the
2890      real definition first, and we can just use the same value.  */
2891   if (h->u.weakdef != NULL)
2892     {
2893       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2894                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
2895       h->root.u.def.section = h->u.weakdef->root.u.def.section;
2896       h->root.u.def.value = h->u.weakdef->root.u.def.value;
2897       return TRUE;
2898     }
2899
2900   /* This is a reference to a symbol defined by a dynamic object which
2901      is not a function.  */
2902   return TRUE;
2903 }
2904
2905 /* This function is called after all the input files have been read,
2906    and the input sections have been assigned to output sections.  */
2907
2908 static bfd_boolean
2909 _bfd_score_elf_always_size_sections (bfd *output_bfd,
2910                                      struct bfd_link_info *info)
2911 {
2912   bfd *dynobj;
2913   asection *s;
2914   struct score_got_info *g;
2915   int i;
2916   bfd_size_type loadable_size = 0;
2917   bfd_size_type local_gotno;
2918   bfd *sub;
2919
2920   dynobj = elf_hash_table (info)->dynobj;
2921   if (dynobj == NULL)
2922     /* Relocatable links don't have it.  */
2923     return TRUE;
2924
2925   g = score_elf_got_info (dynobj, &s);
2926   if (s == NULL)
2927     return TRUE;
2928
2929   /* Calculate the total loadable size of the output.  That will give us the
2930      maximum number of GOT_PAGE entries required.  */
2931   for (sub = info->input_bfds; sub; sub = sub->link_next)
2932     {
2933       asection *subsection;
2934
2935       for (subsection = sub->sections;
2936            subsection;
2937            subsection = subsection->next)
2938         {
2939           if ((subsection->flags & SEC_ALLOC) == 0)
2940             continue;
2941           loadable_size += ((subsection->size + 0xf)
2942                             &~ (bfd_size_type) 0xf);
2943         }
2944     }
2945
2946   /* There has to be a global GOT entry for every symbol with
2947      a dynamic symbol table index of DT_SCORE_GOTSYM or
2948      higher.  Therefore, it make sense to put those symbols
2949      that need GOT entries at the end of the symbol table.  We
2950      do that here.  */
2951   if (! score_elf_sort_hash_table (info, 1))
2952     return FALSE;
2953
2954   if (g->global_gotsym != NULL)
2955     i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
2956   else
2957     /* If there are no global symbols, or none requiring
2958        relocations, then GLOBAL_GOTSYM will be NULL.  */
2959     i = 0;
2960
2961   /* In the worst case, we'll get one stub per dynamic symbol.  */
2962   loadable_size += SCORE_FUNCTION_STUB_SIZE * i;
2963
2964   /* Assume there are two loadable segments consisting of
2965      contiguous sections.  Is 5 enough?  */
2966   local_gotno = (loadable_size >> 16) + 5;
2967
2968   g->local_gotno += local_gotno;
2969   s->size += g->local_gotno * SCORE_ELF_GOT_SIZE (output_bfd);
2970
2971   g->global_gotno = i;
2972   s->size += i * SCORE_ELF_GOT_SIZE (output_bfd);
2973
2974   score_elf_resolve_final_got_entries (g);
2975
2976   if (s->size > SCORE_ELF_GOT_MAX_SIZE (output_bfd))
2977     {
2978       /* Fixme. Error message or Warning message should be issued here.  */
2979     }
2980
2981   return TRUE;
2982 }
2983
2984 /* Set the sizes of the dynamic sections.  */
2985
2986 static bfd_boolean
2987 _bfd_score_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
2988 {
2989   bfd *dynobj;
2990   asection *s;
2991   bfd_boolean reltext;
2992
2993   dynobj = elf_hash_table (info)->dynobj;
2994   BFD_ASSERT (dynobj != NULL);
2995
2996   if (elf_hash_table (info)->dynamic_sections_created)
2997     {
2998       /* Set the contents of the .interp section to the interpreter.  */
2999       if (!info->shared)
3000         {
3001           s = bfd_get_section_by_name (dynobj, ".interp");
3002           BFD_ASSERT (s != NULL);
3003           s->size = strlen (ELF_DYNAMIC_INTERPRETER) + 1;
3004           s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
3005         }
3006     }
3007
3008   /* The check_relocs and adjust_dynamic_symbol entry points have
3009      determined the sizes of the various dynamic sections.  Allocate
3010      memory for them.  */
3011   reltext = FALSE;
3012   for (s = dynobj->sections; s != NULL; s = s->next)
3013     {
3014       const char *name;
3015
3016       if ((s->flags & SEC_LINKER_CREATED) == 0)
3017         continue;
3018
3019       /* It's OK to base decisions on the section name, because none
3020          of the dynobj section names depend upon the input files.  */
3021       name = bfd_get_section_name (dynobj, s);
3022
3023       if (CONST_STRNEQ (name, ".rel"))
3024         {
3025           if (s->size == 0)
3026             {
3027               /* We only strip the section if the output section name
3028                  has the same name.  Otherwise, there might be several
3029                  input sections for this output section.  FIXME: This
3030                  code is probably not needed these days anyhow, since
3031                  the linker now does not create empty output sections.  */
3032               if (s->output_section != NULL
3033                   && strcmp (name,
3034                              bfd_get_section_name (s->output_section->owner,
3035                                                    s->output_section)) == 0)
3036                 s->flags |= SEC_EXCLUDE;
3037             }
3038           else
3039             {
3040               const char *outname;
3041               asection *target;
3042
3043               /* If this relocation section applies to a read only
3044                  section, then we probably need a DT_TEXTREL entry.
3045                  If the relocation section is .rel.dyn, we always
3046                  assert a DT_TEXTREL entry rather than testing whether
3047                  there exists a relocation to a read only section or
3048                  not.  */
3049               outname = bfd_get_section_name (output_bfd, s->output_section);
3050               target = bfd_get_section_by_name (output_bfd, outname + 4);
3051               if ((target != NULL
3052                    && (target->flags & SEC_READONLY) != 0
3053                    && (target->flags & SEC_ALLOC) != 0) || strcmp (outname, ".rel.dyn") == 0)
3054                 reltext = TRUE;
3055
3056               /* We use the reloc_count field as a counter if we need
3057                  to copy relocs into the output file.  */
3058               if (strcmp (name, ".rel.dyn") != 0)
3059                 s->reloc_count = 0;
3060             }
3061         }
3062       else if (CONST_STRNEQ (name, ".got"))
3063         {
3064           /* _bfd_score_elf_always_size_sections() has already done
3065              most of the work, but some symbols may have been mapped
3066              to versions that we must now resolve in the got_entries
3067              hash tables.  */
3068         }
3069       else if (strcmp (name, SCORE_ELF_STUB_SECTION_NAME) == 0)
3070         {
3071           /* IRIX rld assumes that the function stub isn't at the end
3072              of .text section. So put a dummy. XXX  */
3073           s->size += SCORE_FUNCTION_STUB_SIZE;
3074         }
3075       else if (! CONST_STRNEQ (name, ".init"))
3076         {
3077           /* It's not one of our sections, so don't allocate space.  */
3078           continue;
3079         }
3080
3081       /* Allocate memory for the section contents.  */
3082       s->contents = bfd_zalloc (dynobj, s->size);
3083       if (s->contents == NULL && s->size != 0)
3084         {
3085           bfd_set_error (bfd_error_no_memory);
3086           return FALSE;
3087         }
3088     }
3089
3090   if (elf_hash_table (info)->dynamic_sections_created)
3091     {
3092       /* Add some entries to the .dynamic section.  We fill in the
3093          values later, in _bfd_score_elf_finish_dynamic_sections, but we
3094          must add the entries now so that we get the correct size for
3095          the .dynamic section.  The DT_DEBUG entry is filled in by the
3096          dynamic linker and used by the debugger.  */
3097
3098       if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
3099         return FALSE;
3100
3101       if (reltext)
3102         info->flags |= DF_TEXTREL;
3103
3104       if ((info->flags & DF_TEXTREL) != 0)
3105         {
3106           if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
3107             return FALSE;
3108         }
3109
3110       if (! SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
3111         return FALSE;
3112
3113       if (score_elf_rel_dyn_section (dynobj, FALSE))
3114         {
3115           if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
3116             return FALSE;
3117
3118           if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
3119             return FALSE;
3120
3121           if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
3122             return FALSE;
3123         }
3124
3125       if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_SCORE_BASE_ADDRESS, 0))
3126         return FALSE;
3127
3128       if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_SCORE_LOCAL_GOTNO, 0))
3129         return FALSE;
3130
3131       if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_SCORE_SYMTABNO, 0))
3132         return FALSE;
3133
3134       if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_SCORE_UNREFEXTNO, 0))
3135         return FALSE;
3136
3137       if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_SCORE_GOTSYM, 0))
3138         return FALSE;
3139
3140       if (!SCORE_ELF_ADD_DYNAMIC_ENTRY (info, DT_SCORE_HIPAGENO, 0))
3141         return FALSE;
3142     }
3143
3144   return TRUE;
3145 }
3146
3147 static bfd_boolean
3148 _bfd_score_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3149 {
3150   struct elf_link_hash_entry *h;
3151   struct bfd_link_hash_entry *bh;
3152   flagword flags;
3153   asection *s;
3154
3155   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3156            | SEC_LINKER_CREATED | SEC_READONLY);
3157
3158   /* ABI requests the .dynamic section to be read only.  */
3159   s = bfd_get_section_by_name (abfd, ".dynamic");
3160   if (s != NULL)
3161     {
3162       if (!bfd_set_section_flags (abfd, s, flags))
3163         return FALSE;
3164     }
3165
3166   /* We need to create .got section.  */
3167   if (!score_elf_create_got_section (abfd, info, FALSE))
3168     return FALSE;
3169
3170   if (!score_elf_rel_dyn_section (elf_hash_table (info)->dynobj, TRUE))
3171     return FALSE;
3172
3173   /* Create .stub section.  */
3174   if (bfd_get_section_by_name (abfd, SCORE_ELF_STUB_SECTION_NAME) == NULL)
3175     {
3176       s = bfd_make_section (abfd, SCORE_ELF_STUB_SECTION_NAME);
3177       if (s == NULL
3178           || !bfd_set_section_flags (abfd, s, flags | SEC_CODE)
3179           || !bfd_set_section_alignment (abfd, s, 2))
3180
3181         return FALSE;
3182     }
3183
3184   if (!info->shared)
3185     {
3186       const char *name;
3187
3188       name = "_DYNAMIC_LINK";
3189       bh = NULL;
3190       if (!(_bfd_generic_link_add_one_symbol
3191             (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr,
3192              (bfd_vma) 0, (const char *)NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
3193         return FALSE;
3194
3195       h = (struct elf_link_hash_entry *)bh;
3196       h->non_elf = 0;
3197       h->def_regular = 1;
3198       h->type = STT_SECTION;
3199
3200       if (!bfd_elf_link_record_dynamic_symbol (info, h))
3201         return FALSE;
3202     }
3203
3204   return TRUE;
3205 }
3206
3207
3208 /* Finish up dynamic symbol handling.  We set the contents of various
3209    dynamic sections here.  */
3210
3211 static bfd_boolean
3212 _bfd_score_elf_finish_dynamic_symbol (bfd *output_bfd,
3213                                       struct bfd_link_info *info,
3214                                       struct elf_link_hash_entry *h,
3215                                       Elf_Internal_Sym *sym)
3216 {
3217   bfd *dynobj;
3218   asection *sgot;
3219   struct score_got_info *g;
3220   const char *name;
3221
3222   dynobj = elf_hash_table (info)->dynobj;
3223
3224   if (h->plt.offset != MINUS_ONE)
3225     {
3226       asection *s;
3227       bfd_byte stub[SCORE_FUNCTION_STUB_SIZE];
3228
3229       /* This symbol has a stub.  Set it up.  */
3230       BFD_ASSERT (h->dynindx != -1);
3231
3232       s = bfd_get_section_by_name (dynobj, SCORE_ELF_STUB_SECTION_NAME);
3233       BFD_ASSERT (s != NULL);
3234
3235       /* FIXME: Can h->dynindex be more than 64K?  */
3236       if (h->dynindx & 0xffff0000)
3237         return FALSE;
3238
3239       /* Fill the stub.  */
3240       bfd_put_32 (output_bfd, STUB_LW, stub);
3241       bfd_put_32 (output_bfd, STUB_MOVE, stub + 4);
3242       bfd_put_32 (output_bfd, STUB_LI16 | (h->dynindx << 1), stub + 8);
3243       bfd_put_32 (output_bfd, STUB_BRL, stub + 12);
3244
3245       BFD_ASSERT (h->plt.offset <= s->size);
3246       memcpy (s->contents + h->plt.offset, stub, SCORE_FUNCTION_STUB_SIZE);
3247
3248       /* Mark the symbol as undefined.  plt.offset != -1 occurs
3249          only for the referenced symbol.  */
3250       sym->st_shndx = SHN_UNDEF;
3251
3252       /* The run-time linker uses the st_value field of the symbol
3253           to reset the global offset table entry for this external
3254           to its stub address when unlinking a shared object.  */
3255       sym->st_value = (s->output_section->vma + s->output_offset + h->plt.offset);
3256     }
3257
3258   BFD_ASSERT (h->dynindx != -1 || h->forced_local);
3259
3260   sgot = score_elf_got_section (dynobj, FALSE);
3261   BFD_ASSERT (sgot != NULL);
3262   BFD_ASSERT (score_elf_section_data (sgot) != NULL);
3263   g = score_elf_section_data (sgot)->u.got_info;
3264   BFD_ASSERT (g != NULL);
3265
3266   /* Run through the global symbol table, creating GOT entries for all
3267      the symbols that need them.  */
3268   if (g->global_gotsym != NULL && h->dynindx >= g->global_gotsym->dynindx)
3269     {
3270       bfd_vma offset;
3271       bfd_vma value;
3272
3273       value = sym->st_value;
3274       offset = score_elf_global_got_index (dynobj, h);
3275       bfd_put_32 (output_bfd, value, sgot->contents + offset);
3276     }
3277
3278   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
3279   name = h->root.root.string;
3280   if (strcmp (name, "_DYNAMIC") == 0 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
3281     sym->st_shndx = SHN_ABS;
3282   else if (strcmp (name, "_DYNAMIC_LINK") == 0)
3283     {
3284       sym->st_shndx = SHN_ABS;
3285       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
3286       sym->st_value = 1;
3287     }
3288   else if (strcmp (name, GP_DISP_LABEL) == 0)
3289     {
3290       sym->st_shndx = SHN_ABS;
3291       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
3292       sym->st_value = elf_gp (output_bfd);
3293     }
3294
3295   return TRUE;
3296 }
3297
3298 /* Finish up the dynamic sections.  */
3299
3300 static bfd_boolean
3301 _bfd_score_elf_finish_dynamic_sections (bfd *output_bfd,
3302                                         struct bfd_link_info *info)
3303 {
3304   bfd *dynobj;
3305   asection *sdyn;
3306   asection *sgot;
3307   asection *s;
3308   struct score_got_info *g;
3309
3310   dynobj = elf_hash_table (info)->dynobj;
3311
3312   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3313
3314   sgot = score_elf_got_section (dynobj, FALSE);
3315   if (sgot == NULL)
3316     g = NULL;
3317   else
3318     {
3319       BFD_ASSERT (score_elf_section_data (sgot) != NULL);
3320       g = score_elf_section_data (sgot)->u.got_info;
3321       BFD_ASSERT (g != NULL);
3322     }
3323
3324   if (elf_hash_table (info)->dynamic_sections_created)
3325     {
3326       bfd_byte *b;
3327
3328       BFD_ASSERT (sdyn != NULL);
3329       BFD_ASSERT (g != NULL);
3330
3331       for (b = sdyn->contents;
3332            b < sdyn->contents + sdyn->size;
3333            b += SCORE_ELF_DYN_SIZE (dynobj))
3334         {
3335           Elf_Internal_Dyn dyn;
3336           const char *name;
3337           size_t elemsize;
3338           bfd_boolean swap_out_p;
3339
3340           /* Read in the current dynamic entry.  */
3341           (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
3342
3343           /* Assume that we're going to modify it and write it out.  */
3344           swap_out_p = TRUE;
3345
3346           switch (dyn.d_tag)
3347             {
3348             case DT_RELENT:
3349               s = score_elf_rel_dyn_section (dynobj, FALSE);
3350               BFD_ASSERT (s != NULL);
3351               dyn.d_un.d_val = SCORE_ELF_REL_SIZE (dynobj);
3352               break;
3353
3354             case DT_STRSZ:
3355               /* Rewrite DT_STRSZ.  */
3356               dyn.d_un.d_val = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3357                     break;
3358
3359             case DT_PLTGOT:
3360               name = ".got";
3361               s = bfd_get_section_by_name (output_bfd, name);
3362               BFD_ASSERT (s != NULL);
3363               dyn.d_un.d_ptr = s->vma;
3364               break;
3365
3366             case DT_SCORE_BASE_ADDRESS:
3367               s = output_bfd->sections;
3368               BFD_ASSERT (s != NULL);
3369               dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
3370               break;
3371
3372             case DT_SCORE_LOCAL_GOTNO:
3373               dyn.d_un.d_val = g->local_gotno;
3374               break;
3375
3376             case DT_SCORE_UNREFEXTNO:
3377               /* The index into the dynamic symbol table which is the
3378                  entry of the first external symbol that is not
3379                  referenced within the same object.  */
3380               dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
3381               break;
3382
3383             case DT_SCORE_GOTSYM:
3384               if (g->global_gotsym)
3385                 {
3386                   dyn.d_un.d_val = g->global_gotsym->dynindx;
3387                   break;
3388                 }
3389               /* In case if we don't have global got symbols we default
3390                   to setting DT_SCORE_GOTSYM to the same value as
3391                   DT_SCORE_SYMTABNO, so we just fall through.  */
3392
3393             case DT_SCORE_SYMTABNO:
3394               name = ".dynsym";
3395               elemsize = SCORE_ELF_SYM_SIZE (output_bfd);
3396               s = bfd_get_section_by_name (output_bfd, name);
3397               BFD_ASSERT (s != NULL);
3398
3399               dyn.d_un.d_val = s->size / elemsize;
3400               break;
3401
3402             case DT_SCORE_HIPAGENO:
3403               dyn.d_un.d_val = g->local_gotno - SCORE_RESERVED_GOTNO;
3404               break;
3405
3406             default:
3407               swap_out_p = FALSE;
3408               break;
3409             }
3410
3411           if (swap_out_p)
3412             (*get_elf_backend_data (dynobj)->s->swap_dyn_out) (dynobj, &dyn, b);
3413         }
3414     }
3415
3416   /* The first entry of the global offset table will be filled at
3417      runtime. The second entry will be used by some runtime loaders.
3418      This isn't the case of IRIX rld.  */
3419   if (sgot != NULL && sgot->size > 0)
3420     {
3421       bfd_put_32 (output_bfd, 0, sgot->contents);
3422       bfd_put_32 (output_bfd, 0x80000000, sgot->contents + SCORE_ELF_GOT_SIZE (output_bfd));
3423     }
3424
3425   if (sgot != NULL)
3426     elf_section_data (sgot->output_section)->this_hdr.sh_entsize
3427       = SCORE_ELF_GOT_SIZE (output_bfd);
3428
3429
3430   /* We need to sort the entries of the dynamic relocation section.  */
3431   s = score_elf_rel_dyn_section (dynobj, FALSE);
3432
3433   if (s != NULL && s->size > (bfd_vma)2 * SCORE_ELF_REL_SIZE (output_bfd))
3434     {
3435       reldyn_sorting_bfd = output_bfd;
3436       qsort ((Elf32_External_Rel *) s->contents + 1, s->reloc_count - 1,
3437              sizeof (Elf32_External_Rel), score_elf_sort_dynamic_relocs);
3438     }
3439
3440   return TRUE;
3441 }
3442
3443 /* This function set up the ELF section header for a BFD section in preparation for writing
3444    it out.  This is where the flags and type fields are set for unusual sections.  */
3445
3446 static bfd_boolean
3447 _bfd_score_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
3448                               Elf_Internal_Shdr *hdr,
3449                               asection *sec)
3450 {
3451   const char *name;
3452
3453   name = bfd_get_section_name (abfd, sec);
3454
3455   if (strcmp (name, ".got") == 0
3456       || strcmp (name, ".srdata") == 0
3457       || strcmp (name, ".sdata") == 0
3458       || strcmp (name, ".sbss") == 0)
3459     hdr->sh_flags |= SHF_SCORE_GPREL;
3460
3461   return TRUE;
3462 }
3463
3464 /* This function do additional processing on the ELF section header before writing
3465    it out.  This is used to set the flags and type fields for some sections.  */
3466
3467 /* assign_file_positions_except_relocs() check section flag and if it is allocatable,
3468    warning message will be issued.  backend_fake_section is called before
3469    assign_file_positions_except_relocs(); backend_section_processing after it.  so, we
3470    modify section flag there, but not backend_fake_section.  */
3471
3472 static bfd_boolean
3473 _bfd_score_elf_section_processing (bfd *abfd ATTRIBUTE_UNUSED, Elf_Internal_Shdr *hdr)
3474 {
3475   if (hdr->bfd_section != NULL)
3476     {
3477       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
3478
3479       if (strcmp (name, ".sdata") == 0)
3480         {
3481           hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_SCORE_GPREL;
3482           hdr->sh_type = SHT_PROGBITS;
3483         }
3484       else if (strcmp (name, ".sbss") == 0)
3485         {
3486           hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_SCORE_GPREL;
3487           hdr->sh_type = SHT_NOBITS;
3488         }
3489       else if (strcmp (name, ".srdata") == 0)
3490         {
3491           hdr->sh_flags |= SHF_ALLOC | SHF_SCORE_GPREL;
3492           hdr->sh_type = SHT_PROGBITS;
3493         }
3494     }
3495
3496   return TRUE;
3497 }
3498
3499 static bfd_boolean
3500 _bfd_score_elf_write_section (bfd *output_bfd, asection *sec, bfd_byte *contents)
3501 {
3502   bfd_byte *to, *from, *end;
3503   int i;
3504
3505   if (strcmp (sec->name, ".pdr") != 0)
3506     return FALSE;
3507
3508   if (score_elf_section_data (sec)->u.tdata == NULL)
3509     return FALSE;
3510
3511   to = contents;
3512   end = contents + sec->size;
3513   for (from = contents, i = 0; from < end; from += PDR_SIZE, i++)
3514     {
3515       if ((score_elf_section_data (sec)->u.tdata)[i] == 1)
3516         continue;
3517
3518       if (to != from)
3519         memcpy (to, from, PDR_SIZE);
3520
3521       to += PDR_SIZE;
3522     }
3523   bfd_set_section_contents (output_bfd, sec->output_section, contents,
3524                             (file_ptr) sec->output_offset, sec->size);
3525
3526   return TRUE;
3527 }
3528
3529 /* Copy data from a SCORE ELF indirect symbol to its direct symbol, hiding the old
3530    indirect symbol.  Process additional relocation information.  */
3531
3532 static void
3533 _bfd_score_elf_copy_indirect_symbol (struct bfd_link_info *info,
3534                                      struct elf_link_hash_entry *dir,
3535                                      struct elf_link_hash_entry *ind)
3536 {
3537   struct score_elf_link_hash_entry *dirscore, *indscore;
3538
3539   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3540
3541   if (ind->root.type != bfd_link_hash_indirect)
3542     return;
3543
3544   dirscore = (struct score_elf_link_hash_entry *) dir;
3545   indscore = (struct score_elf_link_hash_entry *) ind;
3546   dirscore->possibly_dynamic_relocs += indscore->possibly_dynamic_relocs;
3547
3548   if (indscore->readonly_reloc)
3549     dirscore->readonly_reloc = TRUE;
3550
3551   if (indscore->no_fn_stub)
3552     dirscore->no_fn_stub = TRUE;
3553 }
3554
3555 /* Remove information about discarded functions from other sections which mention them.  */
3556
3557 static bfd_boolean
3558 _bfd_score_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
3559                          struct bfd_link_info *info)
3560 {
3561   asection *o;
3562   bfd_boolean ret = FALSE;
3563   unsigned char *tdata;
3564   size_t i, skip;
3565
3566   o = bfd_get_section_by_name (abfd, ".pdr");
3567   if ((!o) || (o->size == 0) || (o->size % PDR_SIZE != 0)
3568       || (o->output_section != NULL && bfd_is_abs_section (o->output_section)))
3569     return FALSE;
3570
3571   tdata = bfd_zmalloc (o->size / PDR_SIZE);
3572   if (!tdata)
3573     return FALSE;
3574
3575   cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, info->keep_memory);
3576   if (!cookie->rels)
3577     {
3578       free (tdata);
3579       return FALSE;
3580     }
3581
3582   cookie->rel = cookie->rels;
3583   cookie->relend = cookie->rels + o->reloc_count;
3584
3585   for (i = 0, skip = 0; i < o->size; i++)
3586     {
3587       if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
3588         {
3589           tdata[i] = 1;
3590           skip++;
3591         }
3592     }
3593
3594   if (skip != 0)
3595     {
3596       score_elf_section_data (o)->u.tdata = tdata;
3597       o->size -= skip * PDR_SIZE;
3598       ret = TRUE;
3599     }
3600   else
3601     free (tdata);
3602
3603   if (!info->keep_memory)
3604     free (cookie->rels);
3605
3606   return ret;
3607 }
3608
3609 /* Signal that discard_info() has removed the discarded relocations for this section.  */
3610
3611 static bfd_boolean
3612 _bfd_score_elf_ignore_discarded_relocs (asection *sec)
3613 {
3614   if (strcmp (sec->name, ".pdr") == 0)
3615     return TRUE;
3616   return FALSE;
3617 }
3618
3619 /* This function discover the section a particular relocation refers to.
3620    Return the section that should be marked against GC for a given relocation.  */
3621
3622 static asection *
3623 _bfd_score_elf_gc_mark_hook (asection *sec,
3624                              struct bfd_link_info *info ATTRIBUTE_UNUSED,
3625                              Elf_Internal_Rela *rel,
3626                              struct elf_link_hash_entry *h,
3627                              Elf_Internal_Sym *sym)
3628 {
3629   if (h != NULL)
3630     {
3631       switch (ELF32_R_TYPE (rel->r_info))
3632         {
3633         case R_SCORE_GNU_VTINHERIT:
3634         case R_SCORE_GNU_VTENTRY:
3635           break;
3636         default:
3637           switch (h->root.type)
3638             {
3639             case bfd_link_hash_defined:
3640             case bfd_link_hash_defweak:
3641               return h->root.u.def.section;
3642             case bfd_link_hash_common:
3643               return h->root.u.c.p->section;
3644             default:
3645               break;
3646             }
3647         }
3648     }
3649   else
3650     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3651
3652   return NULL;
3653 }
3654
3655 /* Support for core dump NOTE sections.  */
3656
3657 static bfd_boolean
3658 _bfd_score_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3659 {
3660   int offset;
3661   unsigned int raw_size;
3662
3663   switch (note->descsz)
3664     {
3665     default:
3666       return FALSE;
3667
3668     case 148:                  /* Linux/Score 32-bit.  */
3669       /* pr_cursig */
3670       elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
3671
3672       /* pr_pid */
3673       elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
3674
3675       /* pr_reg */
3676       offset = 72;
3677       raw_size = 72;
3678
3679       break;
3680     }
3681
3682   /* Make a ".reg/999" section.  */
3683   return _bfd_elfcore_make_pseudosection (abfd, ".reg", raw_size, note->descpos + offset);
3684 }
3685
3686 static bfd_boolean
3687 _bfd_score_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3688 {
3689   switch (note->descsz)
3690     {
3691     default:
3692       return FALSE;
3693
3694     case 124:                  /* Linux/Score elf_prpsinfo.  */
3695       elf_tdata (abfd)->core_program = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3696       elf_tdata (abfd)->core_command = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3697     }
3698
3699   /* Note that for some reason, a spurious space is tacked
3700      onto the end of the args in some (at least one anyway)
3701      implementations, so strip it off if it exists.  */
3702
3703   {
3704     char *command = elf_tdata (abfd)->core_command;
3705     int n = strlen (command);
3706
3707     if (0 < n && command[n - 1] == ' ')
3708       command[n - 1] = '\0';
3709   }
3710
3711   return TRUE;
3712 }
3713
3714
3715 /* Score BFD functions.  */
3716
3717 static reloc_howto_type *
3718 elf32_score_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, bfd_reloc_code_real_type code)
3719 {
3720   unsigned int i;
3721
3722   for (i = 0; i < NUM_ELEM (elf32_score_reloc_map); i++)
3723     if (elf32_score_reloc_map[i].bfd_reloc_val == code)
3724       return &elf32_score_howto_table[elf32_score_reloc_map[i].elf_reloc_val];
3725
3726   return NULL;
3727 }
3728
3729 /* Create a score elf linker hash table.  */
3730
3731 static struct bfd_link_hash_table *
3732 elf32_score_link_hash_table_create (bfd *abfd)
3733 {
3734   struct score_elf_link_hash_table *ret;
3735   bfd_size_type amt = sizeof (struct score_elf_link_hash_table);
3736
3737   ret = bfd_malloc (amt);
3738   if (ret == NULL)
3739     return NULL;
3740
3741   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd, score_elf_link_hash_newfunc,
3742                                       sizeof (struct score_elf_link_hash_entry)))
3743     {
3744       free (ret);
3745       return NULL;
3746     }
3747
3748   return &ret->root.root;
3749 }
3750
3751 static bfd_boolean
3752 elf32_score_print_private_bfd_data (bfd *abfd, void * ptr)
3753 {
3754   FILE *file = (FILE *) ptr;
3755
3756   BFD_ASSERT (abfd != NULL && ptr != NULL);
3757
3758   /* Print normal ELF private data.  */
3759   _bfd_elf_print_private_bfd_data (abfd, ptr);
3760
3761   /* xgettext:c-format */
3762   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
3763   if (elf_elfheader (abfd)->e_flags & EF_SCORE_PIC)
3764     {
3765       fprintf (file, _(" [pic]"));
3766     }
3767   if (elf_elfheader (abfd)->e_flags & EF_SCORE_FIXDEP)
3768     {
3769       fprintf (file, _(" [fix dep]"));
3770     }
3771   fputc ('\n', file);
3772
3773   return TRUE;
3774 }
3775
3776 static bfd_boolean
3777 elf32_score_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3778 {
3779   flagword in_flags;
3780   flagword out_flags;
3781
3782   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
3783     return FALSE;
3784
3785   in_flags  = elf_elfheader (ibfd)->e_flags;
3786   out_flags = elf_elfheader (obfd)->e_flags;
3787
3788   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3789       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3790     return TRUE;
3791
3792   in_flags = elf_elfheader (ibfd)->e_flags;
3793   out_flags = elf_elfheader (obfd)->e_flags;
3794
3795   if (! elf_flags_init (obfd))
3796     {
3797       elf_flags_init (obfd) = TRUE;
3798       elf_elfheader (obfd)->e_flags = in_flags;
3799
3800       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3801           && bfd_get_arch_info (obfd)->the_default)
3802         {
3803           return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
3804         }
3805
3806       return TRUE;
3807     }
3808
3809   if (((in_flags & EF_SCORE_PIC) != 0) != ((out_flags & EF_SCORE_PIC) != 0))
3810     {
3811       (*_bfd_error_handler) (_("%B: warning: linking PIC files with non-PIC files"), ibfd);
3812     }
3813
3814   /* FIXME: Maybe dependency fix compatibility should be checked here.  */
3815
3816   return TRUE;
3817 }
3818
3819 static bfd_boolean
3820 elf32_score_new_section_hook (bfd *abfd, asection *sec)
3821 {
3822   struct _score_elf_section_data *sdata;
3823   bfd_size_type amt = sizeof (*sdata);
3824
3825   sdata = bfd_zalloc (abfd, amt);
3826   if (sdata == NULL)
3827     return FALSE;
3828   sec->used_by_bfd = sdata;
3829
3830   return _bfd_elf_new_section_hook (abfd, sec);
3831 }
3832
3833
3834 #define USE_REL                         1
3835 #define TARGET_LITTLE_SYM               bfd_elf32_littlescore_vec
3836 #define TARGET_LITTLE_NAME              "elf32-littlescore"
3837 #define TARGET_BIG_SYM                  bfd_elf32_bigscore_vec
3838 #define TARGET_BIG_NAME                 "elf32-bigscore"
3839 #define ELF_ARCH                        bfd_arch_score
3840 #define ELF_MACHINE_CODE                EM_SCORE
3841 #define ELF_MAXPAGESIZE                 0x8000
3842
3843 #define elf_info_to_howto                             0
3844 #define elf_info_to_howto_rel                         _bfd_score_info_to_howto
3845 #define elf_backend_relocate_section                  _bfd_score_elf_relocate_section
3846 #define elf_backend_check_relocs                      _bfd_score_elf_check_relocs
3847 #define elf_backend_add_symbol_hook                   _bfd_score_elf_add_symbol_hook
3848 #define elf_backend_symbol_processing                 _bfd_score_elf_symbol_processing
3849 #define elf_backend_link_output_symbol_hook           _bfd_score_elf_link_output_symbol_hook
3850 #define elf_backend_section_from_bfd_section          _bfd_score_elf_section_from_bfd_section
3851 #define elf_backend_adjust_dynamic_symbol             _bfd_score_elf_adjust_dynamic_symbol
3852 #define elf_backend_always_size_sections              _bfd_score_elf_always_size_sections
3853 #define elf_backend_size_dynamic_sections             _bfd_score_elf_size_dynamic_sections
3854 #define elf_backend_create_dynamic_sections           _bfd_score_elf_create_dynamic_sections
3855 #define elf_backend_finish_dynamic_symbol             _bfd_score_elf_finish_dynamic_symbol
3856 #define elf_backend_finish_dynamic_sections           _bfd_score_elf_finish_dynamic_sections
3857 #define elf_backend_fake_sections                     _bfd_score_elf_fake_sections
3858 #define elf_backend_section_processing                _bfd_score_elf_section_processing
3859 #define elf_backend_write_section                     _bfd_score_elf_write_section
3860 #define elf_backend_copy_indirect_symbol              _bfd_score_elf_copy_indirect_symbol
3861 #define elf_backend_hide_symbol                       _bfd_score_elf_hide_symbol
3862 #define elf_backend_discard_info                      _bfd_score_elf_discard_info
3863 #define elf_backend_ignore_discarded_relocs           _bfd_score_elf_ignore_discarded_relocs
3864 #define elf_backend_gc_mark_hook                      _bfd_score_elf_gc_mark_hook
3865 #define elf_backend_grok_prstatus                     _bfd_score_elf_grok_prstatus
3866 #define elf_backend_grok_psinfo                       _bfd_score_elf_grok_psinfo
3867 #define elf_backend_can_gc_sections                   1
3868 #define elf_backend_want_plt_sym                      0
3869 #define elf_backend_got_header_size                   (4 * SCORE_RESERVED_GOTNO)
3870 #define elf_backend_plt_header_size                   0
3871 #define elf_backend_collect                           TRUE
3872 #define elf_backend_type_change_ok                    TRUE
3873
3874 #define bfd_elf32_bfd_reloc_type_lookup               elf32_score_reloc_type_lookup
3875 #define bfd_elf32_bfd_link_hash_table_create          elf32_score_link_hash_table_create
3876 #define bfd_elf32_bfd_print_private_bfd_data          elf32_score_print_private_bfd_data
3877 #define bfd_elf32_bfd_merge_private_bfd_data          elf32_score_merge_private_bfd_data
3878 #define bfd_elf32_new_section_hook                    elf32_score_new_section_hook
3879
3880 #include "elf32-target.h"