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