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