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