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