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