Re: ELF final_write_processing
[deliverable/binutils-gdb.git] / bfd / elf32-lm32.c
CommitLineData
84e94c90 1/* Lattice Mico32-specific support for 32-bit ELF
82704155 2 Copyright (C) 2008-2019 Free Software Foundation, Inc.
84e94c90
NC
3 Contributed by Jon Beniston <jon@beniston.com>
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
84e94c90 22#include "sysdep.h"
691bf19c 23#include "bfd.h"
84e94c90
NC
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf/lm32.h"
27
28#define DEFAULT_STACK_SIZE 0x20000
29
30#define PLT_ENTRY_SIZE 20
31
32#define PLT0_ENTRY_WORD0 0
33#define PLT0_ENTRY_WORD1 0
34#define PLT0_ENTRY_WORD2 0
35#define PLT0_ENTRY_WORD3 0
36#define PLT0_ENTRY_WORD4 0
37
38#define PLT0_PIC_ENTRY_WORD0 0
39#define PLT0_PIC_ENTRY_WORD1 0
40#define PLT0_PIC_ENTRY_WORD2 0
41#define PLT0_PIC_ENTRY_WORD3 0
42#define PLT0_PIC_ENTRY_WORD4 0
43
44#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
45
6d00b590 46extern const bfd_target lm32_elf32_fdpic_vec;
84e94c90 47
6d00b590 48#define IS_FDPIC(bfd) ((bfd)->xvec == &lm32_elf32_fdpic_vec)
84e94c90
NC
49
50static bfd_reloc_status_type lm32_elf_gprel_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52
84e94c90
NC
53/* lm32 ELF linker hash entry. */
54
55struct elf_lm32_link_hash_entry
56{
57 struct elf_link_hash_entry root;
58
59 /* Track dynamic relocs copied for this symbol. */
3bf083ed 60 struct elf_dyn_relocs *dyn_relocs;
84e94c90
NC
61};
62
63/* lm32 ELF linker hash table. */
64
65struct elf_lm32_link_hash_table
66{
67 struct elf_link_hash_table root;
68
69 /* Short-cuts to get to dynamic linker sections. */
84e94c90 70 asection *sfixup32;
84e94c90
NC
71 asection *sdynbss;
72 asection *srelbss;
73
74 int relocs32;
75};
76
77/* Get the lm32 ELF linker hash table from a link_info structure. */
78
79#define lm32_elf_hash_table(p) \
4dfe6ac6
NC
80 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
81 == LM32_ELF_DATA ? ((struct elf_lm32_link_hash_table *) ((p)->hash)) : NULL)
84e94c90
NC
82
83#define lm32fdpic_got_section(info) \
ce558b89 84 (lm32_elf_hash_table (info)->root.sgot)
84e94c90 85#define lm32fdpic_gotrel_section(info) \
ce558b89 86 (lm32_elf_hash_table (info)->root.srelgot)
84e94c90
NC
87#define lm32fdpic_fixup32_section(info) \
88 (lm32_elf_hash_table (info)->sfixup32)
89
90struct weak_symbol_list
91{
92 const char *name;
93 struct weak_symbol_list *next;
94};
95
96/* Create an entry in an lm32 ELF linker hash table. */
97
98static struct bfd_hash_entry *
99lm32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
100 struct bfd_hash_table *table,
101 const char *string)
102{
103 struct elf_lm32_link_hash_entry *ret =
104 (struct elf_lm32_link_hash_entry *) entry;
105
106 /* Allocate the structure if it has not already been allocated by a
107 subclass. */
108 if (ret == NULL)
109 ret = bfd_hash_allocate (table,
110 sizeof (struct elf_lm32_link_hash_entry));
111 if (ret == NULL)
112 return NULL;
113
114 /* Call the allocation method of the superclass. */
115 ret = ((struct elf_lm32_link_hash_entry *)
07d6d2b8
AM
116 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
117 table, string));
84e94c90
NC
118 if (ret != NULL)
119 {
120 struct elf_lm32_link_hash_entry *eh;
121
122 eh = (struct elf_lm32_link_hash_entry *) ret;
123 eh->dyn_relocs = NULL;
124 }
125
126 return (struct bfd_hash_entry *) ret;
127}
128
129/* Create an lm32 ELF linker hash table. */
130
131static struct bfd_link_hash_table *
132lm32_elf_link_hash_table_create (bfd *abfd)
133{
134 struct elf_lm32_link_hash_table *ret;
135 bfd_size_type amt = sizeof (struct elf_lm32_link_hash_table);
136
7bf52ea2 137 ret = bfd_zmalloc (amt);
84e94c90
NC
138 if (ret == NULL)
139 return NULL;
140
141 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
142 lm32_elf_link_hash_newfunc,
4dfe6ac6
NC
143 sizeof (struct elf_lm32_link_hash_entry),
144 LM32_ELF_DATA))
84e94c90
NC
145 {
146 free (ret);
147 return NULL;
148 }
149
84e94c90
NC
150 return &ret->root.root;
151}
152
153/* Add a fixup to the ROFIXUP section. */
154
155static bfd_vma
156_lm32fdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma relocation)
157{
158 bfd_vma fixup_offset;
159
160 if (rofixup->flags & SEC_EXCLUDE)
161 return -1;
162
163 fixup_offset = rofixup->reloc_count * 4;
164 if (rofixup->contents)
165 {
166 BFD_ASSERT (fixup_offset < rofixup->size);
167 if (fixup_offset < rofixup->size)
168 bfd_put_32 (output_bfd, relocation, rofixup->contents + fixup_offset);
169 }
170 rofixup->reloc_count++;
171
172 return fixup_offset;
173}
174
84e94c90
NC
175/* Create .rofixup sections in DYNOBJ, and set up
176 shortcuts to them in our hash table. */
177
178static bfd_boolean
179create_rofixup_section (bfd *dynobj, struct bfd_link_info *info)
180{
181 struct elf_lm32_link_hash_table *htab;
182 htab = lm32_elf_hash_table (info);
183
4dfe6ac6
NC
184 if (htab == NULL)
185 return FALSE;
186
187 /* Fixup section for R_LM32_32 relocs. */
3d4d4302
AM
188 lm32fdpic_fixup32_section (info)
189 = bfd_make_section_anyway_with_flags (dynobj,
190 ".rofixup",
191 (SEC_ALLOC
192 | SEC_LOAD
193 | SEC_HAS_CONTENTS
194 | SEC_IN_MEMORY
195 | SEC_LINKER_CREATED
196 | SEC_READONLY));
84e94c90 197 if (lm32fdpic_fixup32_section (info) == NULL
3d4d4302
AM
198 || ! bfd_set_section_alignment (dynobj,
199 lm32fdpic_fixup32_section (info), 2))
84e94c90
NC
200 return FALSE;
201
202 return TRUE;
203}
204
205static reloc_howto_type lm32_elf_howto_table [] =
206{
207 /* This reloc does nothing. */
07d6d2b8
AM
208 HOWTO (R_LM32_NONE, /* type */
209 0, /* rightshift */
210 3, /* size (0 = byte, 1 = short, 2 = long) */
211 0, /* bitsize */
212 FALSE, /* pc_relative */
213 0, /* bitpos */
214 complain_overflow_dont, /* complain_on_overflow */
215 bfd_elf_generic_reloc, /* special_function */
216 "R_LM32_NONE", /* name */
217 FALSE, /* partial_inplace */
218 0, /* src_mask */
219 0, /* dst_mask */
220 FALSE), /* pcrel_offset */
84e94c90
NC
221
222 /* An 8 bit absolute relocation. */
07d6d2b8
AM
223 HOWTO (R_LM32_8, /* type */
224 0, /* rightshift */
225 0, /* size (0 = byte, 1 = short, 2 = long) */
226 8, /* bitsize */
227 FALSE, /* pc_relative */
228 0, /* bitpos */
229 complain_overflow_bitfield,/* complain_on_overflow */
230 bfd_elf_generic_reloc, /* special_function */
231 "R_LM32_8", /* name */
232 FALSE, /* partial_inplace */
233 0, /* src_mask */
234 0xff, /* dst_mask */
235 FALSE), /* pcrel_offset */
84e94c90
NC
236
237 /* A 16 bit absolute relocation. */
07d6d2b8
AM
238 HOWTO (R_LM32_16, /* type */
239 0, /* rightshift */
240 1, /* size (0 = byte, 1 = short, 2 = long) */
241 16, /* bitsize */
242 FALSE, /* pc_relative */
243 0, /* bitpos */
244 complain_overflow_bitfield,/* complain_on_overflow */
245 bfd_elf_generic_reloc, /* special_function */
246 "R_LM32_16", /* name */
247 FALSE, /* partial_inplace */
248 0, /* src_mask */
249 0xffff, /* dst_mask */
250 FALSE), /* pcrel_offset */
84e94c90
NC
251
252 /* A 32 bit absolute relocation. */
07d6d2b8
AM
253 HOWTO (R_LM32_32, /* type */
254 0, /* rightshift */
255 2, /* size (0 = byte, 1 = short, 2 = long) */
256 32, /* bitsize */
257 FALSE, /* pc_relative */
258 0, /* bitpos */
259 complain_overflow_bitfield,/* complain_on_overflow */
260 bfd_elf_generic_reloc, /* special_function */
261 "R_LM32_32", /* name */
262 FALSE, /* partial_inplace */
263 0, /* src_mask */
264 0xffffffff, /* dst_mask */
265 FALSE), /* pcrel_offset */
266
267 HOWTO (R_LM32_HI16, /* type */
268 16, /* rightshift */
269 2, /* size (0 = byte, 1 = short, 2 = long) */
270 16, /* bitsize */
271 FALSE, /* pc_relative */
272 0, /* bitpos */
273 complain_overflow_bitfield,/* complain_on_overflow */
274 bfd_elf_generic_reloc, /* special_function */
275 "R_LM32_HI16", /* name */
276 FALSE, /* partial_inplace */
277 0, /* src_mask */
278 0xffff, /* dst_mask */
279 FALSE), /* pcrel_offset */
280
281 HOWTO (R_LM32_LO16, /* type */
282 0, /* rightshift */
283 2, /* size (0 = byte, 1 = short, 2 = long) */
284 16, /* bitsize */
285 FALSE, /* pc_relative */
286 0, /* bitpos */
287 complain_overflow_dont, /* complain_on_overflow */
288 bfd_elf_generic_reloc, /* special_function */
289 "R_LM32_LO16", /* name */
290 FALSE, /* partial_inplace */
291 0, /* src_mask */
292 0xffff, /* dst_mask */
293 FALSE), /* pcrel_offset */
294
295 HOWTO (R_LM32_GPREL16, /* type */
296 0, /* rightshift */
297 2, /* size (0 = byte, 1 = short, 2 = long) */
298 16, /* bitsize */
299 FALSE, /* pc_relative */
300 0, /* bitpos */
301 complain_overflow_dont, /* complain_on_overflow */
302 lm32_elf_gprel_reloc, /* special_function */
303 "R_LM32_GPREL16", /* name */
304 FALSE, /* partial_inplace */
305 0, /* src_mask */
306 0xffff, /* dst_mask */
307 FALSE), /* pcrel_offset */
308
309 HOWTO (R_LM32_CALL, /* type */
310 2, /* rightshift */
311 2, /* size (0 = byte, 1 = short, 2 = long) */
312 26, /* bitsize */
313 TRUE, /* pc_relative */
314 0, /* bitpos */
315 complain_overflow_signed, /* complain_on_overflow */
316 bfd_elf_generic_reloc, /* special_function */
317 "R_LM32_CALL", /* name */
318 FALSE, /* partial_inplace */
319 0, /* src_mask */
320 0x3ffffff, /* dst_mask */
321 TRUE), /* pcrel_offset */
322
323 HOWTO (R_LM32_BRANCH, /* type */
324 2, /* rightshift */
325 2, /* size (0 = byte, 1 = short, 2 = long) */
326 16, /* bitsize */
327 TRUE, /* pc_relative */
328 0, /* bitpos */
329 complain_overflow_signed, /* complain_on_overflow */
330 bfd_elf_generic_reloc, /* special_function */
331 "R_LM32_BRANCH", /* name */
332 FALSE, /* partial_inplace */
333 0, /* src_mask */
334 0xffff, /* dst_mask */
335 TRUE), /* pcrel_offset */
84e94c90
NC
336
337 /* GNU extension to record C++ vtable hierarchy. */
07d6d2b8
AM
338 HOWTO (R_LM32_GNU_VTINHERIT, /* type */
339 0, /* rightshift */
340 2, /* size (0 = byte, 1 = short, 2 = long) */
341 0, /* bitsize */
342 FALSE, /* pc_relative */
343 0, /* bitpos */
344 complain_overflow_dont, /* complain_on_overflow */
345 NULL, /* special_function */
346 "R_LM32_GNU_VTINHERIT", /* name */
347 FALSE, /* partial_inplace */
348 0, /* src_mask */
349 0, /* dst_mask */
350 FALSE), /* pcrel_offset */
84e94c90
NC
351
352 /* GNU extension to record C++ vtable member usage. */
07d6d2b8
AM
353 HOWTO (R_LM32_GNU_VTENTRY, /* type */
354 0, /* rightshift */
355 2, /* size (0 = byte, 1 = short, 2 = long) */
356 0, /* bitsize */
357 FALSE, /* pc_relative */
358 0, /* bitpos */
359 complain_overflow_dont, /* complain_on_overflow */
360 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
361 "R_LM32_GNU_VTENTRY", /* name */
362 FALSE, /* partial_inplace */
363 0, /* src_mask */
364 0, /* dst_mask */
365 FALSE), /* pcrel_offset */
366
367 HOWTO (R_LM32_16_GOT, /* type */
368 0, /* rightshift */
369 2, /* size (0 = byte, 1 = short, 2 = long) */
370 16, /* bitsize */
371 FALSE, /* pc_relative */
372 0, /* bitpos */
373 complain_overflow_signed, /* complain_on_overflow */
374 bfd_elf_generic_reloc, /* special_function */
375 "R_LM32_16_GOT", /* name */
376 FALSE, /* partial_inplace */
377 0, /* src_mask */
378 0xffff, /* dst_mask */
379 FALSE), /* pcrel_offset */
380
381 HOWTO (R_LM32_GOTOFF_HI16, /* type */
382 16, /* rightshift */
383 2, /* size (0 = byte, 1 = short, 2 = long) */
384 16, /* bitsize */
385 FALSE, /* pc_relative */
386 0, /* bitpos */
387 complain_overflow_dont, /* complain_on_overflow */
388 bfd_elf_generic_reloc, /* special_function */
389 "R_LM32_GOTOFF_HI16", /* name */
390 FALSE, /* partial_inplace */
391 0xffff, /* src_mask */
392 0xffff, /* dst_mask */
393 FALSE), /* pcrel_offset */
394
395 HOWTO (R_LM32_GOTOFF_LO16, /* type */
396 0, /* rightshift */
397 2, /* size (0 = byte, 1 = short, 2 = long) */
398 16, /* bitsize */
399 FALSE, /* pc_relative */
400 0, /* bitpos */
401 complain_overflow_dont, /* complain_on_overflow */
402 bfd_elf_generic_reloc, /* special_function */
403 "R_LM32_GOTOFF_LO16", /* name */
404 FALSE, /* partial_inplace */
405 0xffff, /* src_mask */
406 0xffff, /* dst_mask */
407 FALSE), /* pcrel_offset */
84e94c90
NC
408
409 HOWTO (R_LM32_COPY, /* type */
410 0, /* rightshift */
411 2, /* size (0 = byte, 1 = short, 2 = long) */
412 32, /* bitsize */
413 FALSE, /* pc_relative */
414 0, /* bitpos */
415 complain_overflow_bitfield, /* complain_on_overflow */
416 bfd_elf_generic_reloc, /* special_function */
417 "R_LM32_COPY", /* name */
418 FALSE, /* partial_inplace */
419 0xffffffff, /* src_mask */
420 0xffffffff, /* dst_mask */
421 FALSE), /* pcrel_offset */
422
423 HOWTO (R_LM32_GLOB_DAT, /* type */
424 0, /* rightshift */
425 2, /* size (0 = byte, 1 = short, 2 = long) */
426 32, /* bitsize */
427 FALSE, /* pc_relative */
428 0, /* bitpos */
429 complain_overflow_bitfield, /* complain_on_overflow */
430 bfd_elf_generic_reloc, /* special_function */
431 "R_LM32_GLOB_DAT", /* name */
432 FALSE, /* partial_inplace */
433 0xffffffff, /* src_mask */
434 0xffffffff, /* dst_mask */
435 FALSE), /* pcrel_offset */
436
437 HOWTO (R_LM32_JMP_SLOT, /* type */
438 0, /* rightshift */
439 2, /* size (0 = byte, 1 = short, 2 = long) */
440 32, /* bitsize */
441 FALSE, /* pc_relative */
442 0, /* bitpos */
443 complain_overflow_bitfield, /* complain_on_overflow */
444 bfd_elf_generic_reloc, /* special_function */
445 "R_LM32_JMP_SLOT", /* name */
446 FALSE, /* partial_inplace */
447 0xffffffff, /* src_mask */
448 0xffffffff, /* dst_mask */
449 FALSE), /* pcrel_offset */
450
451 HOWTO (R_LM32_RELATIVE, /* type */
452 0, /* rightshift */
453 2, /* size (0 = byte, 1 = short, 2 = long) */
454 32, /* bitsize */
455 FALSE, /* pc_relative */
456 0, /* bitpos */
457 complain_overflow_bitfield, /* complain_on_overflow */
458 bfd_elf_generic_reloc, /* special_function */
459 "R_LM32_RELATIVE", /* name */
460 FALSE, /* partial_inplace */
461 0xffffffff, /* src_mask */
462 0xffffffff, /* dst_mask */
463 FALSE), /* pcrel_offset */
464
465};
466
467/* Map BFD reloc types to lm32 ELF reloc types. */
468
469struct lm32_reloc_map
470{
471 bfd_reloc_code_real_type bfd_reloc_val;
472 unsigned char elf_reloc_val;
473};
474
475static const struct lm32_reloc_map lm32_reloc_map[] =
476{
07d6d2b8
AM
477 { BFD_RELOC_NONE, R_LM32_NONE },
478 { BFD_RELOC_8, R_LM32_8 },
479 { BFD_RELOC_16, R_LM32_16 },
480 { BFD_RELOC_32, R_LM32_32 },
481 { BFD_RELOC_HI16, R_LM32_HI16 },
482 { BFD_RELOC_LO16, R_LM32_LO16 },
483 { BFD_RELOC_GPREL16, R_LM32_GPREL16 },
484 { BFD_RELOC_LM32_CALL, R_LM32_CALL },
485 { BFD_RELOC_LM32_BRANCH, R_LM32_BRANCH },
486 { BFD_RELOC_VTABLE_INHERIT, R_LM32_GNU_VTINHERIT },
487 { BFD_RELOC_VTABLE_ENTRY, R_LM32_GNU_VTENTRY },
488 { BFD_RELOC_LM32_16_GOT, R_LM32_16_GOT },
84e94c90
NC
489 { BFD_RELOC_LM32_GOTOFF_HI16, R_LM32_GOTOFF_HI16 },
490 { BFD_RELOC_LM32_GOTOFF_LO16, R_LM32_GOTOFF_LO16 },
07d6d2b8
AM
491 { BFD_RELOC_LM32_COPY, R_LM32_COPY },
492 { BFD_RELOC_LM32_GLOB_DAT, R_LM32_GLOB_DAT },
493 { BFD_RELOC_LM32_JMP_SLOT, R_LM32_JMP_SLOT },
494 { BFD_RELOC_LM32_RELATIVE, R_LM32_RELATIVE },
84e94c90
NC
495};
496
497static reloc_howto_type *
498lm32_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
07d6d2b8 499 bfd_reloc_code_real_type code)
84e94c90
NC
500{
501 unsigned int i;
502
503 for (i = 0; i < sizeof (lm32_reloc_map) / sizeof (lm32_reloc_map[0]); i++)
504 if (lm32_reloc_map[i].bfd_reloc_val == code)
505 return &lm32_elf_howto_table[lm32_reloc_map[i].elf_reloc_val];
506 return NULL;
507}
508
509static reloc_howto_type *
510lm32_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
511 const char *r_name)
512{
513 unsigned int i;
514
515 for (i = 0;
516 i < sizeof (lm32_elf_howto_table) / sizeof (lm32_elf_howto_table[0]);
517 i++)
518 if (lm32_elf_howto_table[i].name != NULL
519 && strcasecmp (lm32_elf_howto_table[i].name, r_name) == 0)
520 return &lm32_elf_howto_table[i];
521
522 return NULL;
523}
524
525
526/* Set the howto pointer for an Lattice Mico32 ELF reloc. */
527
f3185997 528static bfd_boolean
0aa13fee 529lm32_info_to_howto_rela (bfd *abfd,
07d6d2b8
AM
530 arelent *cache_ptr,
531 Elf_Internal_Rela *dst)
84e94c90
NC
532{
533 unsigned int r_type;
534
535 r_type = ELF32_R_TYPE (dst->r_info);
5860e3f8
NC
536 if (r_type >= (unsigned int) R_LM32_max)
537 {
695344c0 538 /* xgettext:c-format */
0aa13fee
AM
539 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
540 abfd, r_type);
f3185997
NC
541 bfd_set_error (bfd_error_bad_value);
542 return FALSE;
5860e3f8 543 }
84e94c90 544 cache_ptr->howto = &lm32_elf_howto_table[r_type];
f3185997 545 return TRUE;
84e94c90
NC
546}
547
548/* Set the right machine number for an Lattice Mico32 ELF file. */
549
550static bfd_boolean
551lm32_elf_object_p (bfd *abfd)
552{
553 return bfd_default_set_arch_mach (abfd, bfd_arch_lm32, bfd_mach_lm32);
554}
555
556/* Set machine type flags just before file is written out. */
557
558static void
06f44071 559lm32_elf_final_write_processing (bfd *abfd, bfd_boolean linker)
84e94c90
NC
560{
561 elf_elfheader (abfd)->e_machine = EM_LATTICEMICO32;
562 elf_elfheader (abfd)->e_flags &=~ EF_LM32_MACH;
563 switch (bfd_get_mach (abfd))
564 {
565 case bfd_mach_lm32:
07d6d2b8
AM
566 elf_elfheader (abfd)->e_flags |= E_LM32_MACH;
567 break;
84e94c90 568 default:
07d6d2b8 569 abort ();
84e94c90 570 }
06f44071 571 _bfd_elf_final_write_processing (abfd, linker);
84e94c90
NC
572}
573
574/* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
575 dangerous relocation. */
576
577static bfd_boolean
578lm32_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp)
579{
580 unsigned int count;
581 asymbol **sym;
582 unsigned int i;
583
584 /* If we've already figured out what GP will be, just return it. */
585 *pgp = _bfd_get_gp_value (output_bfd);
586 if (*pgp)
587 return TRUE;
588
589 count = bfd_get_symcount (output_bfd);
590 sym = bfd_get_outsymbols (output_bfd);
591
592 /* The linker script will have created a symbol named `_gp' with the
593 appropriate value. */
594 if (sym == NULL)
595 i = count;
596 else
597 {
598 for (i = 0; i < count; i++, sym++)
599 {
600 const char *name;
601
602 name = bfd_asymbol_name (*sym);
603 if (*name == '_' && strcmp (name, "_gp") == 0)
604 {
605 *pgp = bfd_asymbol_value (*sym);
606 _bfd_set_gp_value (output_bfd, *pgp);
607 break;
608 }
609 }
610 }
611
612 if (i >= count)
613 {
614 /* Only get the error once. */
615 *pgp = 4;
616 _bfd_set_gp_value (output_bfd, *pgp);
617 return FALSE;
618 }
619
620 return TRUE;
621}
622
623/* We have to figure out the gp value, so that we can adjust the
624 symbol value correctly. We look up the symbol _gp in the output
625 BFD. If we can't find it, we're stuck. We cache it in the ELF
626 target data. We don't need to adjust the symbol value for an
627 external symbol if we are producing relocatable output. */
628
629static bfd_reloc_status_type
630lm32_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
07d6d2b8 631 char **error_message, bfd_vma *pgp)
84e94c90
NC
632{
633 if (bfd_is_und_section (symbol->section) && !relocatable)
634 {
635 *pgp = 0;
636 return bfd_reloc_undefined;
637 }
638
639 *pgp = _bfd_get_gp_value (output_bfd);
640 if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0))
641 {
642 if (relocatable)
643 {
644 /* Make up a value. */
645 *pgp = symbol->section->output_section->vma + 0x4000;
646 _bfd_set_gp_value (output_bfd, *pgp);
647 }
648 else if (!lm32_elf_assign_gp (output_bfd, pgp))
649 {
650 *error_message =
651 (char *)
652 _("global pointer relative relocation when _gp not defined");
653 return bfd_reloc_dangerous;
654 }
655 }
656
657 return bfd_reloc_ok;
658}
659
660static bfd_reloc_status_type
661lm32_elf_do_gprel_relocate (bfd *abfd,
662 reloc_howto_type *howto,
663 asection *input_section ATTRIBUTE_UNUSED,
664 bfd_byte *data,
665 bfd_vma offset,
666 bfd_vma symbol_value,
667 bfd_vma addend)
668{
669 return _bfd_final_link_relocate (howto, abfd, input_section,
670 data, offset, symbol_value, addend);
671}
672
673static bfd_reloc_status_type
674lm32_elf_gprel_reloc (bfd *abfd,
675 arelent *reloc_entry,
676 asymbol *symbol,
677 void *data,
678 asection *input_section,
679 bfd *output_bfd,
680 char **msg)
681{
682 bfd_vma relocation;
683 bfd_vma gp;
684 bfd_reloc_status_type r;
685
686 if (output_bfd != (bfd *) NULL
687 && (symbol->flags & BSF_SECTION_SYM) == 0
688 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
689 {
690 reloc_entry->address += input_section->output_offset;
691 return bfd_reloc_ok;
692 }
693
694 if (output_bfd != NULL)
695 return bfd_reloc_ok;
696
697 relocation = symbol->value
698 + symbol->section->output_section->vma + symbol->section->output_offset;
699
700 if ((r =
701 lm32_elf_final_gp (abfd, symbol, FALSE, msg, &gp)) == bfd_reloc_ok)
702 {
703 relocation = relocation + reloc_entry->addend - gp;
704 reloc_entry->addend = 0;
705 if ((signed) relocation < -32768 || (signed) relocation > 32767)
706 {
707 *msg = _("global pointer relative address out of range");
708 r = bfd_reloc_outofrange;
709 }
710 else
711 {
712 r = lm32_elf_do_gprel_relocate (abfd, reloc_entry->howto,
713 input_section,
714 data, reloc_entry->address,
715 relocation, reloc_entry->addend);
716 }
717 }
718
719 return r;
720}
721
722/* Find the segment number in which OSEC, and output section, is
723 located. */
724
725static unsigned
726_lm32fdpic_osec_to_segment (bfd *output_bfd, asection *osec)
727{
728 struct elf_segment_map *m;
729 Elf_Internal_Phdr *p;
730
731 /* Find the segment that contains the output_section. */
12bd6957 732 for (m = elf_seg_map (output_bfd), p = elf_tdata (output_bfd)->phdr;
84e94c90
NC
733 m != NULL;
734 m = m->next, p++)
735 {
736 int i;
737
738 for (i = m->count - 1; i >= 0; i--)
739 if (m->sections[i] == osec)
740 break;
741
742 if (i >= 0)
743 break;
744 }
745
746 return p - elf_tdata (output_bfd)->phdr;
747}
748
749/* Determine if an output section is read-only. */
750
751inline static bfd_boolean
752_lm32fdpic_osec_readonly_p (bfd *output_bfd, asection *osec)
753{
754 unsigned seg = _lm32fdpic_osec_to_segment (output_bfd, osec);
755
756 return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
757}
758
759/* Relocate a section */
760
761static bfd_boolean
762lm32_elf_relocate_section (bfd *output_bfd,
07d6d2b8
AM
763 struct bfd_link_info *info,
764 bfd *input_bfd,
765 asection *input_section,
766 bfd_byte *contents,
767 Elf_Internal_Rela *relocs,
768 Elf_Internal_Sym *local_syms,
769 asection **local_sections)
84e94c90
NC
770{
771 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
772 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
773 Elf_Internal_Rela *rel, *relend;
84e94c90 774 struct elf_lm32_link_hash_table *htab = lm32_elf_hash_table (info);
84e94c90 775 bfd_vma *local_got_offsets;
c7e2358a 776 asection *sgot;
84e94c90 777
4dfe6ac6
NC
778 if (htab == NULL)
779 return FALSE;
780
84e94c90
NC
781 local_got_offsets = elf_local_got_offsets (input_bfd);
782
ce558b89 783 sgot = htab->root.sgot;
84e94c90
NC
784
785 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
786 sym_hashes = elf_sym_hashes (input_bfd);
787
788 rel = relocs;
789 relend = relocs + input_section->reloc_count;
790 for (; rel < relend; rel++)
791 {
792 reloc_howto_type *howto;
793 unsigned int r_type;
794 unsigned long r_symndx;
795 Elf_Internal_Sym *sym;
796 asection *sec;
797 struct elf_link_hash_entry *h;
798 bfd_vma relocation;
799 bfd_vma gp;
800 bfd_reloc_status_type r;
801 const char *name = NULL;
84e94c90
NC
802
803 r_symndx = ELF32_R_SYM (rel->r_info);
804 r_type = ELF32_R_TYPE (rel->r_info);
805
806 if (r_type == R_LM32_GNU_VTENTRY
07d6d2b8
AM
807 || r_type == R_LM32_GNU_VTINHERIT )
808 continue;
84e94c90
NC
809
810 h = NULL;
811 sym = NULL;
812 sec = NULL;
813
814 howto = lm32_elf_howto_table + r_type;
815
816 if (r_symndx < symtab_hdr->sh_info)
07d6d2b8
AM
817 {
818 /* It's a local symbol. */
819 sym = local_syms + r_symndx;
820 sec = local_sections[r_symndx];
821 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
822 name = bfd_elf_string_from_elf_section
84e94c90
NC
823 (input_bfd, symtab_hdr->sh_link, sym->st_name);
824 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
07d6d2b8 825 }
84e94c90 826 else
07d6d2b8
AM
827 {
828 /* It's a global symbol. */
829 bfd_boolean unresolved_reloc;
62d887d4 830 bfd_boolean warned, ignored;
84e94c90
NC
831
832 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
833 r_symndx, symtab_hdr, sym_hashes,
834 h, sec, relocation,
62d887d4 835 unresolved_reloc, warned, ignored);
84e94c90 836 name = h->root.root.string;
07d6d2b8 837 }
84e94c90 838
dbaa2011 839 if (sec != NULL && discarded_section (sec))
e4067dbb 840 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b 841 rel, 1, relend, howto, 0, contents);
84e94c90 842
0e1862bb 843 if (bfd_link_relocatable (info))
07d6d2b8 844 {
84e94c90
NC
845 /* This is a relocatable link. We don't have to change
846 anything, unless the reloc is against a section symbol,
847 in which case we have to adjust according to where the
848 section symbol winds up in the output section. */
849 if (sym == NULL || ELF_ST_TYPE (sym->st_info) != STT_SECTION)
850 continue;
851
852 /* If partial_inplace, we need to store any additional addend
853 back in the section. */
854 if (! howto->partial_inplace)
855 continue;
856
07d6d2b8 857 /* Shouldn't reach here. */
84e94c90
NC
858 abort ();
859 r = bfd_reloc_ok;
07d6d2b8 860 }
84e94c90 861 else
07d6d2b8
AM
862 {
863 switch (howto->type)
864 {
865 case R_LM32_GPREL16:
866 if (!lm32_elf_assign_gp (output_bfd, &gp))
867 r = bfd_reloc_dangerous;
868 else
869 {
870 relocation = relocation + rel->r_addend - gp;
871 rel->r_addend = 0;
872 if ((signed)relocation < -32768 || (signed)relocation > 32767)
873 r = bfd_reloc_outofrange;
874 else
875 {
876 r = _bfd_final_link_relocate (howto, input_bfd,
877 input_section, contents,
878 rel->r_offset, relocation,
879 rel->r_addend);
880 }
881 }
882 break;
883 case R_LM32_16_GOT:
884 /* Relocation is to the entry for this symbol in the global
885 offset table. */
886 BFD_ASSERT (sgot != NULL);
887 if (h != NULL)
888 {
889 bfd_boolean dyn;
890 bfd_vma off;
891
892 off = h->got.offset;
893 BFD_ASSERT (off != (bfd_vma) -1);
894
895 dyn = htab->root.dynamic_sections_created;
896 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
0e1862bb
L
897 bfd_link_pic (info),
898 h)
07d6d2b8
AM
899 || (bfd_link_pic (info)
900 && (info->symbolic
901 || h->dynindx == -1
902 || h->forced_local)
903 && h->def_regular))
904 {
905 /* This is actually a static link, or it is a
906 -Bsymbolic link and the symbol is defined
907 locally, or the symbol was forced to be local
908 because of a version file. We must initialize
909 this entry in the global offset table. Since the
910 offset must always be a multiple of 4, we use the
911 least significant bit to record whether we have
912 initialized it already.
913
914 When doing a dynamic link, we create a .rela.got
915 relocation entry to initialize the value. This
916 is done in the finish_dynamic_symbol routine. */
917 if ((off & 1) != 0)
918 off &= ~1;
919 else
920 {
921 /* Write entry in GOT */
922 bfd_put_32 (output_bfd, relocation,
923 sgot->contents + off);
924 /* Create entry in .rofixup pointing to GOT entry. */
925 if (IS_FDPIC (output_bfd) && h->root.type != bfd_link_hash_undefweak)
926 {
927 _lm32fdpic_add_rofixup (output_bfd,
928 lm32fdpic_fixup32_section
929 (info),
930 sgot->output_section->vma
931 + sgot->output_offset
932 + off);
933 }
934 /* Mark GOT entry as having been written. */
935 h->got.offset |= 1;
936 }
937 }
938
939 relocation = sgot->output_offset + off;
940 }
941 else
942 {
943 bfd_vma off;
944 bfd_byte *loc;
945
946 BFD_ASSERT (local_got_offsets != NULL
947 && local_got_offsets[r_symndx] != (bfd_vma) -1);
948
949 /* Get offset into GOT table. */
950 off = local_got_offsets[r_symndx];
951
952 /* The offset must always be a multiple of 4. We use
953 the least significant bit to record whether we have
954 already processed this entry. */
955 if ((off & 1) != 0)
956 off &= ~1;
957 else
958 {
959 /* Write entry in GOT. */
960 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
961 /* Create entry in .rofixup pointing to GOT entry. */
962 if (IS_FDPIC (output_bfd))
963 {
964 _lm32fdpic_add_rofixup (output_bfd,
965 lm32fdpic_fixup32_section
966 (info),
967 sgot->output_section->vma
968 + sgot->output_offset
969 + off);
970 }
971
972 if (bfd_link_pic (info))
973 {
974 asection *srelgot;
975 Elf_Internal_Rela outrel;
976
977 /* We need to generate a R_LM32_RELATIVE reloc
978 for the dynamic linker. */
979 srelgot = htab->root.srelgot;
980 BFD_ASSERT (srelgot != NULL);
981
982 outrel.r_offset = (sgot->output_section->vma
983 + sgot->output_offset
984 + off);
985 outrel.r_info = ELF32_R_INFO (0, R_LM32_RELATIVE);
986 outrel.r_addend = relocation;
987 loc = srelgot->contents;
988 loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
989 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
990 ++srelgot->reloc_count;
991 }
992
993 local_got_offsets[r_symndx] |= 1;
994 }
995
996
997 relocation = sgot->output_offset + off;
998 }
999
1000 /* Addend should be zero. */
1001 if (rel->r_addend != 0)
38f14ab8
AM
1002 _bfd_error_handler
1003 (_("internal error: addend should be zero for %s"),
1004 "R_LM32_16_GOT");
84e94c90 1005
07d6d2b8
AM
1006 r = _bfd_final_link_relocate (howto,
1007 input_bfd,
1008 input_section,
1009 contents,
1010 rel->r_offset,
1011 relocation,
1012 rel->r_addend);
1013 break;
1014
1015 case R_LM32_GOTOFF_LO16:
1016 case R_LM32_GOTOFF_HI16:
1017 /* Relocation is offset from GOT. */
84e94c90
NC
1018 BFD_ASSERT (sgot != NULL);
1019 relocation -= sgot->output_section->vma;
1020 /* Account for sign-extension. */
07d6d2b8
AM
1021 if ((r_type == R_LM32_GOTOFF_HI16)
1022 && ((relocation + rel->r_addend) & 0x8000))
1023 rel->r_addend += 0x10000;
1024 r = _bfd_final_link_relocate (howto,
1025 input_bfd,
1026 input_section,
1027 contents,
1028 rel->r_offset,
1029 relocation,
1030 rel->r_addend);
1031 break;
1032
1033 case R_LM32_32:
1034 if (IS_FDPIC (output_bfd))
1035 {
1036 if ((!h) || (h && h->root.type != bfd_link_hash_undefweak))
1037 {
1038 /* Only create .rofixup entries for relocs in loadable sections. */
1039 if ((bfd_get_section_flags (output_bfd, input_section->output_section)
1040 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
1041
1042 {
1043 /* Check address to be modified is writable. */
1044 if (_lm32fdpic_osec_readonly_p (output_bfd,
1045 input_section
1046 ->output_section))
1047 {
1048 info->callbacks->warning
1049 (info,
1050 _("cannot emit dynamic relocations in read-only section"),
1051 name, input_bfd, input_section, rel->r_offset);
1052 return FALSE;
1053 }
1054 /* Create entry in .rofixup section. */
1055 _lm32fdpic_add_rofixup (output_bfd,
1056 lm32fdpic_fixup32_section (info),
1057 input_section->output_section->vma
1058 + input_section->output_offset
1059 + rel->r_offset);
1060 }
1061 }
1062 }
1063 /* Fall through. */
1064
1065 default:
1066 r = _bfd_final_link_relocate (howto,
1067 input_bfd,
1068 input_section,
1069 contents,
1070 rel->r_offset,
1071 relocation,
1072 rel->r_addend);
1073 break;
1074 }
1075 }
84e94c90
NC
1076
1077 if (r != bfd_reloc_ok)
07d6d2b8
AM
1078 {
1079 const char *msg = NULL;
1080 arelent bfd_reloc;
1081
f3185997
NC
1082 if (! lm32_info_to_howto_rela (input_bfd, &bfd_reloc, rel))
1083 continue;
07d6d2b8
AM
1084 howto = bfd_reloc.howto;
1085
1086 if (h != NULL)
1087 name = h->root.root.string;
1088 else
1089 {
1090 name = (bfd_elf_string_from_elf_section
1091 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1092 if (name == NULL || *name == '\0')
1093 name = bfd_section_name (input_bfd, sec);
1094 }
1095
1096 switch (r)
1097 {
84e94c90
NC
1098 case bfd_reloc_overflow:
1099 if ((h != NULL)
07d6d2b8
AM
1100 && (h->root.type == bfd_link_hash_undefweak))
1101 break;
1a72702b
AM
1102 (*info->callbacks->reloc_overflow)
1103 (info, (h ? &h->root : NULL), name, howto->name,
1104 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
84e94c90
NC
1105 break;
1106
07d6d2b8 1107 case bfd_reloc_undefined:
1a72702b
AM
1108 (*info->callbacks->undefined_symbol)
1109 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
07d6d2b8 1110 break;
84e94c90 1111
07d6d2b8
AM
1112 case bfd_reloc_outofrange:
1113 msg = _("internal error: out of range error");
1114 goto common_error;
84e94c90 1115
07d6d2b8
AM
1116 case bfd_reloc_notsupported:
1117 msg = _("internal error: unsupported relocation error");
1118 goto common_error;
84e94c90 1119
07d6d2b8
AM
1120 case bfd_reloc_dangerous:
1121 msg = _("internal error: dangerous error");
1122 goto common_error;
84e94c90 1123
07d6d2b8
AM
1124 default:
1125 msg = _("internal error: unknown error");
1126 /* fall through */
84e94c90 1127
07d6d2b8 1128 common_error:
1a72702b
AM
1129 (*info->callbacks->warning) (info, msg, name, input_bfd,
1130 input_section, rel->r_offset);
07d6d2b8
AM
1131 break;
1132 }
1133 }
84e94c90
NC
1134 }
1135
1136 return TRUE;
1137}
1138
1139static asection *
1140lm32_elf_gc_mark_hook (asection *sec,
07d6d2b8
AM
1141 struct bfd_link_info *info,
1142 Elf_Internal_Rela *rel,
1143 struct elf_link_hash_entry *h,
1144 Elf_Internal_Sym *sym)
84e94c90
NC
1145{
1146 if (h != NULL)
1147 switch (ELF32_R_TYPE (rel->r_info))
1148 {
1149 case R_LM32_GNU_VTINHERIT:
1150 case R_LM32_GNU_VTENTRY:
1151 return NULL;
1152 }
1153
1154 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1155}
1156
84e94c90
NC
1157/* Look through the relocs for a section during the first phase. */
1158
1159static bfd_boolean
1160lm32_elf_check_relocs (bfd *abfd,
07d6d2b8
AM
1161 struct bfd_link_info *info,
1162 asection *sec,
1163 const Elf_Internal_Rela *relocs)
84e94c90
NC
1164{
1165 Elf_Internal_Shdr *symtab_hdr;
1166 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
1167 const Elf_Internal_Rela *rel;
1168 const Elf_Internal_Rela *rel_end;
1169 struct elf_lm32_link_hash_table *htab;
1170 bfd *dynobj;
84e94c90 1171
0e1862bb 1172 if (bfd_link_relocatable (info))
84e94c90
NC
1173 return TRUE;
1174
65281396
AM
1175 /* Don't do anything special with non-loaded, non-alloced sections.
1176 In particular, any relocs in such sections should not affect GOT
1177 and PLT reference counting (ie. we don't allow them to create GOT
1178 or PLT entries), there's no possibility or desire to optimize TLS
1179 relocs, and there's not much point in propagating relocs to shared
1180 libs that the dynamic linker won't relocate. */
1181 if ((sec->flags & SEC_ALLOC) == 0)
1182 return TRUE;
1183
84e94c90
NC
1184 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1185 sym_hashes = elf_sym_hashes (abfd);
1186 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
1187 if (!elf_bad_symtab (abfd))
1188 sym_hashes_end -= symtab_hdr->sh_info;
1189
1190 htab = lm32_elf_hash_table (info);
4dfe6ac6
NC
1191 if (htab == NULL)
1192 return FALSE;
1193
84e94c90 1194 dynobj = htab->root.dynobj;
84e94c90
NC
1195
1196 rel_end = relocs + sec->reloc_count;
1197 for (rel = relocs; rel < rel_end; rel++)
1198 {
1199 int r_type;
1200 struct elf_link_hash_entry *h;
1201 unsigned long r_symndx;
1202
1203 r_symndx = ELF32_R_SYM (rel->r_info);
1204 r_type = ELF32_R_TYPE (rel->r_info);
1205 if (r_symndx < symtab_hdr->sh_info)
07d6d2b8 1206 h = NULL;
84e94c90
NC
1207 else
1208 {
1209 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1210 while (h->root.type == bfd_link_hash_indirect
1211 || h->root.type == bfd_link_hash_warning)
1212 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1213 }
1214
1215 /* Some relocs require a global offset table. */
ce558b89 1216 if (htab->root.sgot == NULL)
07d6d2b8
AM
1217 {
1218 switch (r_type)
1219 {
1220 case R_LM32_16_GOT:
1221 case R_LM32_GOTOFF_HI16:
1222 case R_LM32_GOTOFF_LO16:
1223 if (dynobj == NULL)
1224 htab->root.dynobj = dynobj = abfd;
1225 if (!_bfd_elf_create_got_section (dynobj, info))
1226 return FALSE;
1227 break;
1228 }
1229 }
84e94c90
NC
1230
1231 /* Some relocs require a rofixup table. */
1232 if (IS_FDPIC (abfd))
07d6d2b8
AM
1233 {
1234 switch (r_type)
1235 {
1236 case R_LM32_32:
1237 /* FDPIC requires a GOT if there is a .rofixup section
1238 (Normal ELF doesn't). */
1239 if (dynobj == NULL)
1240 htab->root.dynobj = dynobj = abfd;
1241 if (!_bfd_elf_create_got_section (dynobj, info))
1242 return FALSE;
1243 /* Create .rofixup section */
1244 if (htab->sfixup32 == NULL)
1245 {
1246 if (! create_rofixup_section (dynobj, info))
1247 return FALSE;
1248 }
1249 break;
1250 case R_LM32_16_GOT:
1251 case R_LM32_GOTOFF_HI16:
1252 case R_LM32_GOTOFF_LO16:
1253 /* Create .rofixup section. */
1254 if (htab->sfixup32 == NULL)
1255 {
3d4d4302
AM
1256 if (dynobj == NULL)
1257 htab->root.dynobj = dynobj = abfd;
07d6d2b8
AM
1258 if (! create_rofixup_section (dynobj, info))
1259 return FALSE;
1260 }
1261 break;
1262 }
1263 }
84e94c90
NC
1264
1265 switch (r_type)
1266 {
1267 case R_LM32_16_GOT:
07d6d2b8
AM
1268 if (h != NULL)
1269 h->got.refcount += 1;
1270 else
1271 {
1272 bfd_signed_vma *local_got_refcounts;
1273
1274 /* This is a global offset table entry for a local symbol. */
1275 local_got_refcounts = elf_local_got_refcounts (abfd);
1276 if (local_got_refcounts == NULL)
1277 {
1278 bfd_size_type size;
1279
1280 size = symtab_hdr->sh_info;
1281 size *= sizeof (bfd_signed_vma);
1282 local_got_refcounts = bfd_zalloc (abfd, size);
1283 if (local_got_refcounts == NULL)
1284 return FALSE;
1285 elf_local_got_refcounts (abfd) = local_got_refcounts;
1286 }
1287 local_got_refcounts[r_symndx] += 1;
1288 }
1289 break;
1290
1291 /* This relocation describes the C++ object vtable hierarchy.
1292 Reconstruct it for later use during GC. */
1293 case R_LM32_GNU_VTINHERIT:
1294 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1295 return FALSE;
1296 break;
1297
1298 /* This relocation describes which C++ vtable entries are actually
1299 used. Record for later use during GC. */
1300 case R_LM32_GNU_VTENTRY:
1301 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1302 return FALSE;
1303 break;
1304
1305 }
84e94c90
NC
1306 }
1307
1308 return TRUE;
1309}
1310
1311/* Finish up the dynamic sections. */
1312
1313static bfd_boolean
1314lm32_elf_finish_dynamic_sections (bfd *output_bfd,
1315 struct bfd_link_info *info)
1316{
1317 struct elf_lm32_link_hash_table *htab;
1318 bfd *dynobj;
1319 asection *sdyn;
1320 asection *sgot;
1321
1322 htab = lm32_elf_hash_table (info);
4dfe6ac6
NC
1323 if (htab == NULL)
1324 return FALSE;
1325
84e94c90
NC
1326 dynobj = htab->root.dynobj;
1327
ce558b89 1328 sgot = htab->root.sgotplt;
3d4d4302 1329 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
84e94c90
NC
1330
1331 if (htab->root.dynamic_sections_created)
1332 {
1333 asection *splt;
1334 Elf32_External_Dyn *dyncon, *dynconend;
1335
1336 BFD_ASSERT (sgot != NULL && sdyn != NULL);
1337
1338 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1339 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
1340
1341 for (; dyncon < dynconend; dyncon++)
07d6d2b8
AM
1342 {
1343 Elf_Internal_Dyn dyn;
1344 asection *s;
1345
1346 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1347
1348 switch (dyn.d_tag)
1349 {
1350 default:
1351 break;
1352
1353 case DT_PLTGOT:
1354 s = htab->root.sgotplt;
1355 goto get_vma;
1356 case DT_JMPREL:
1357 s = htab->root.srelplt;
1358 get_vma:
1359 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
1360 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1361 break;
1362
1363 case DT_PLTRELSZ:
1364 s = htab->root.srelplt;
84e94c90 1365 dyn.d_un.d_val = s->size;
07d6d2b8
AM
1366 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1367 break;
1368 }
1369 }
84e94c90
NC
1370
1371 /* Fill in the first entry in the procedure linkage table. */
ce558b89 1372 splt = htab->root.splt;
84e94c90 1373 if (splt && splt->size > 0)
07d6d2b8
AM
1374 {
1375 if (bfd_link_pic (info))
1376 {
1377 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0, splt->contents);
1378 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1, splt->contents + 4);
1379 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2, splt->contents + 8);
1380 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3, splt->contents + 12);
1381 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4, splt->contents + 16);
1382 }
1383 else
1384 {
1385 unsigned long addr;
1386 /* addr = .got + 4 */
1387 addr = sgot->output_section->vma + sgot->output_offset + 4;
1388 bfd_put_32 (output_bfd,
84e94c90
NC
1389 PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
1390 splt->contents);
07d6d2b8 1391 bfd_put_32 (output_bfd,
84e94c90
NC
1392 PLT0_ENTRY_WORD1 | (addr & 0xffff),
1393 splt->contents + 4);
07d6d2b8
AM
1394 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
1395 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
1396 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
1397 }
1398
1399 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1400 PLT_ENTRY_SIZE;
1401 }
84e94c90
NC
1402 }
1403
1404 /* Fill in the first three entries in the global offset table. */
1405 if (sgot && sgot->size > 0)
1406 {
1407 if (sdyn == NULL)
07d6d2b8 1408 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
84e94c90 1409 else
07d6d2b8
AM
1410 bfd_put_32 (output_bfd,
1411 sdyn->output_section->vma + sdyn->output_offset,
1412 sgot->contents);
84e94c90
NC
1413 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1414 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1415
1416 /* FIXME: This can be null if create_dynamic_sections wasn't called. */
1417 if (elf_section_data (sgot->output_section) != NULL)
07d6d2b8 1418 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
84e94c90
NC
1419 }
1420
1421 if (lm32fdpic_fixup32_section (info))
1422 {
1423 struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
1424 bfd_vma got_value = hgot->root.u.def.value
07d6d2b8
AM
1425 + hgot->root.u.def.section->output_section->vma
1426 + hgot->root.u.def.section->output_offset;
84e94c90
NC
1427 struct bfd_link_hash_entry *hend;
1428
1429 /* Last entry is pointer to GOT. */
1430 _lm32fdpic_add_rofixup (output_bfd, lm32fdpic_fixup32_section (info), got_value);
1431
1432 /* Check we wrote enough entries. */
1433 if (lm32fdpic_fixup32_section (info)->size
07d6d2b8
AM
1434 != (lm32fdpic_fixup32_section (info)->reloc_count * 4))
1435 {
4eca0228 1436 _bfd_error_handler
2dcf00ce
AM
1437 ("LINKER BUG: .rofixup section size mismatch: size/4 %" PRId64
1438 " != relocs %d",
1439 (int64_t) (lm32fdpic_fixup32_section (info)->size / 4),
07d6d2b8
AM
1440 lm32fdpic_fixup32_section (info)->reloc_count);
1441 return FALSE;
1442 }
84e94c90
NC
1443
1444 hend = bfd_link_hash_lookup (info->hash, "__ROFIXUP_END__",
07d6d2b8 1445 FALSE, FALSE, TRUE);
84e94c90 1446 if (hend
07d6d2b8 1447 && (hend->type == bfd_link_hash_defined
a0f6fd21
AM
1448 || hend->type == bfd_link_hash_defweak)
1449 && hend->u.def.section->output_section != NULL)
07d6d2b8
AM
1450 {
1451 bfd_vma value =
1452 lm32fdpic_fixup32_section (info)->output_section->vma
1453 + lm32fdpic_fixup32_section (info)->output_offset
1454 + lm32fdpic_fixup32_section (info)->size
1455 - hend->u.def.section->output_section->vma
1456 - hend->u.def.section->output_offset;
1457 BFD_ASSERT (hend->u.def.value == value);
1458 if (hend->u.def.value != value)
1459 {
4eca0228 1460 _bfd_error_handler
2dcf00ce
AM
1461 ("LINKER BUG: .rofixup section hend->u.def.value != value: %"
1462 PRId64 " != %" PRId64,
1463 (int64_t) hend->u.def.value, (int64_t) value);
07d6d2b8
AM
1464 return FALSE;
1465 }
1466 }
84e94c90
NC
1467 }
1468
1469 return TRUE;
1470}
1471
1472/* Finish up dynamic symbol handling. We set the contents of various
1473 dynamic sections here. */
1474
1475static bfd_boolean
1476lm32_elf_finish_dynamic_symbol (bfd *output_bfd,
1477 struct bfd_link_info *info,
1478 struct elf_link_hash_entry *h,
1479 Elf_Internal_Sym *sym)
1480{
1481 struct elf_lm32_link_hash_table *htab;
84e94c90
NC
1482 bfd_byte *loc;
1483
1484 htab = lm32_elf_hash_table (info);
4dfe6ac6
NC
1485 if (htab == NULL)
1486 return FALSE;
1487
84e94c90
NC
1488 if (h->plt.offset != (bfd_vma) -1)
1489 {
1490 asection *splt;
1491 asection *sgot;
1492 asection *srela;
1493
1494 bfd_vma plt_index;
1495 bfd_vma got_offset;
1496 Elf_Internal_Rela rela;
1497
1498 /* This symbol has an entry in the procedure linkage table. Set
07d6d2b8 1499 it up. */
84e94c90
NC
1500 BFD_ASSERT (h->dynindx != -1);
1501
ce558b89
AM
1502 splt = htab->root.splt;
1503 sgot = htab->root.sgotplt;
1504 srela = htab->root.srelplt;
84e94c90
NC
1505 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1506
1507 /* Get the index in the procedure linkage table which
07d6d2b8
AM
1508 corresponds to this symbol. This is the index of this symbol
1509 in all the symbols for which we are making plt entries. The
1510 first entry in the procedure linkage table is reserved. */
84e94c90
NC
1511 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1512
1513 /* Get the offset into the .got table of the entry that
07d6d2b8
AM
1514 corresponds to this function. Each .got entry is 4 bytes.
1515 The first three are reserved. */
84e94c90
NC
1516 got_offset = (plt_index + 3) * 4;
1517
1518 /* Fill in the entry in the procedure linkage table. */
0e1862bb 1519 if (! bfd_link_pic (info))
07d6d2b8
AM
1520 {
1521 /* TODO */
1522 }
84e94c90 1523 else
07d6d2b8
AM
1524 {
1525 /* TODO */
1526 }
84e94c90
NC
1527
1528 /* Fill in the entry in the global offset table. */
1529 bfd_put_32 (output_bfd,
07d6d2b8
AM
1530 (splt->output_section->vma
1531 + splt->output_offset
1532 + h->plt.offset
1533 + 12), /* same offset */
1534 sgot->contents + got_offset);
84e94c90
NC
1535
1536 /* Fill in the entry in the .rela.plt section. */
1537 rela.r_offset = (sgot->output_section->vma
07d6d2b8
AM
1538 + sgot->output_offset
1539 + got_offset);
84e94c90
NC
1540 rela.r_info = ELF32_R_INFO (h->dynindx, R_LM32_JMP_SLOT);
1541 rela.r_addend = 0;
1542 loc = srela->contents;
1543 loc += plt_index * sizeof (Elf32_External_Rela);
1544 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1545
1546 if (!h->def_regular)
07d6d2b8
AM
1547 {
1548 /* Mark the symbol as undefined, rather than as defined in
1549 the .plt section. Leave the value alone. */
1550 sym->st_shndx = SHN_UNDEF;
1551 }
84e94c90
NC
1552
1553 }
1554
1555 if (h->got.offset != (bfd_vma) -1)
1556 {
1557 asection *sgot;
1558 asection *srela;
1559 Elf_Internal_Rela rela;
1560
1561 /* This symbol has an entry in the global offset table. Set it
07d6d2b8 1562 up. */
ce558b89
AM
1563 sgot = htab->root.sgot;
1564 srela = htab->root.srelgot;
84e94c90
NC
1565 BFD_ASSERT (sgot != NULL && srela != NULL);
1566
1567 rela.r_offset = (sgot->output_section->vma
07d6d2b8
AM
1568 + sgot->output_offset
1569 + (h->got.offset &~ 1));
84e94c90
NC
1570
1571 /* If this is a -Bsymbolic link, and the symbol is defined
07d6d2b8
AM
1572 locally, we just want to emit a RELATIVE reloc. Likewise if
1573 the symbol was forced to be local because of a version file.
1574 The entry in the global offset table will already have been
1575 initialized in the relocate_section function. */
0e1862bb 1576 if (bfd_link_pic (info)
07d6d2b8 1577 && (info->symbolic
84e94c90
NC
1578 || h->dynindx == -1
1579 || h->forced_local)
07d6d2b8
AM
1580 && h->def_regular)
1581 {
1582 rela.r_info = ELF32_R_INFO (0, R_LM32_RELATIVE);
1583 rela.r_addend = (h->root.u.def.value
1584 + h->root.u.def.section->output_section->vma
1585 + h->root.u.def.section->output_offset);
1586 }
84e94c90 1587 else
07d6d2b8 1588 {
84e94c90 1589 BFD_ASSERT ((h->got.offset & 1) == 0);
07d6d2b8
AM
1590 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1591 rela.r_info = ELF32_R_INFO (h->dynindx, R_LM32_GLOB_DAT);
1592 rela.r_addend = 0;
1593 }
84e94c90
NC
1594
1595 loc = srela->contents;
1596 loc += srela->reloc_count * sizeof (Elf32_External_Rela);
1597 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1598 ++srela->reloc_count;
1599 }
1600
1601 if (h->needs_copy)
1602 {
1603 asection *s;
1604 Elf_Internal_Rela rela;
1605
1606 /* This symbols needs a copy reloc. Set it up. */
1607 BFD_ASSERT (h->dynindx != -1
07d6d2b8
AM
1608 && (h->root.type == bfd_link_hash_defined
1609 || h->root.type == bfd_link_hash_defweak));
84e94c90 1610
3d4d4302 1611 s = bfd_get_linker_section (htab->root.dynobj, ".rela.bss");
84e94c90
NC
1612 BFD_ASSERT (s != NULL);
1613
1614 rela.r_offset = (h->root.u.def.value
07d6d2b8
AM
1615 + h->root.u.def.section->output_section->vma
1616 + h->root.u.def.section->output_offset);
84e94c90
NC
1617 rela.r_info = ELF32_R_INFO (h->dynindx, R_LM32_COPY);
1618 rela.r_addend = 0;
1619 loc = s->contents;
1620 loc += s->reloc_count * sizeof (Elf32_External_Rela);
1621 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1622 ++s->reloc_count;
1623 }
1624
1625 /* Mark some specially defined symbols as absolute. */
9637f6ef 1626 if (h == htab->root.hdynamic || h == htab->root.hgot)
84e94c90
NC
1627 sym->st_shndx = SHN_ABS;
1628
1629 return TRUE;
1630}
1631
1632static enum elf_reloc_type_class
7e612e98
AM
1633lm32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
1634 const asection *rel_sec ATTRIBUTE_UNUSED,
1635 const Elf_Internal_Rela *rela)
84e94c90
NC
1636{
1637 switch ((int) ELF32_R_TYPE (rela->r_info))
1638 {
1639 case R_LM32_RELATIVE: return reloc_class_relative;
1640 case R_LM32_JMP_SLOT: return reloc_class_plt;
1641 case R_LM32_COPY: return reloc_class_copy;
07d6d2b8 1642 default: return reloc_class_normal;
84e94c90
NC
1643 }
1644}
1645
63c1f59d
AM
1646/* Find dynamic relocs for H that apply to read-only sections. */
1647
1648static asection *
1649readonly_dynrelocs (struct elf_link_hash_entry *h)
1650{
3bf083ed 1651 struct elf_dyn_relocs *p;
63c1f59d
AM
1652 struct elf_lm32_link_hash_entry *eh = (struct elf_lm32_link_hash_entry *) h;
1653
1654 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1655 {
1656 asection *s = p->sec->output_section;
1657
1658 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1659 return p->sec;
1660 }
1661 return NULL;
1662}
1663
84e94c90
NC
1664/* Adjust a symbol defined by a dynamic object and referenced by a
1665 regular object. The current definition is in some section of the
1666 dynamic object, but we're not including those sections. We have to
1667 change the definition to something the rest of the link can
1668 understand. */
1669
1670static bfd_boolean
1671lm32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1672 struct elf_link_hash_entry *h)
1673{
1674 struct elf_lm32_link_hash_table *htab;
84e94c90
NC
1675 bfd *dynobj;
1676 asection *s;
1677
1678 dynobj = elf_hash_table (info)->dynobj;
1679
1680 /* Make sure we know what is going on here. */
1681 BFD_ASSERT (dynobj != NULL
07d6d2b8
AM
1682 && (h->needs_plt
1683 || h->is_weakalias
1684 || (h->def_dynamic
1685 && h->ref_regular
1686 && !h->def_regular)));
84e94c90
NC
1687
1688 /* If this is a function, put it in the procedure linkage table. We
1689 will fill in the contents of the procedure linkage table later,
1690 when we know the address of the .got section. */
1691 if (h->type == STT_FUNC
1692 || h->needs_plt)
1693 {
0e1862bb 1694 if (! bfd_link_pic (info)
07d6d2b8
AM
1695 && !h->def_dynamic
1696 && !h->ref_dynamic
84e94c90
NC
1697 && h->root.type != bfd_link_hash_undefweak
1698 && h->root.type != bfd_link_hash_undefined)
07d6d2b8
AM
1699 {
1700 /* This case can occur if we saw a PLT reloc in an input
1701 file, but the symbol was never referred to by a dynamic
1702 object. In such a case, we don't actually need to build
1703 a procedure linkage table, and we can just do a PCREL
1704 reloc instead. */
1705 h->plt.offset = (bfd_vma) -1;
1706 h->needs_plt = 0;
1707 }
84e94c90
NC
1708
1709 return TRUE;
1710 }
1711 else
1712 h->plt.offset = (bfd_vma) -1;
1713
1714 /* If this is a weak symbol, and there is a real definition, the
1715 processor independent code will have arranged for us to see the
1716 real definition first, and we can just use the same value. */
60d67dc8 1717 if (h->is_weakalias)
84e94c90 1718 {
60d67dc8
AM
1719 struct elf_link_hash_entry *def = weakdef (h);
1720 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1721 h->root.u.def.section = def->root.u.def.section;
1722 h->root.u.def.value = def->root.u.def.value;
84e94c90
NC
1723 return TRUE;
1724 }
1725
1726 /* This is a reference to a symbol defined by a dynamic object which
1727 is not a function. */
1728
1729 /* If we are creating a shared library, we must presume that the
1730 only references to the symbol are via the global offset table.
1731 For such cases we need not do anything here; the relocations will
1732 be handled correctly by relocate_section. */
0e1862bb 1733 if (bfd_link_pic (info))
84e94c90
NC
1734 return TRUE;
1735
1736 /* If there are no references to this symbol that do not use the
1737 GOT, we don't need to generate a copy reloc. */
1738 if (!h->non_got_ref)
1739 return TRUE;
1740
1741 /* If -z nocopyreloc was given, we won't generate them either. */
3bf083ed 1742 if (0 && info->nocopyreloc)
84e94c90
NC
1743 {
1744 h->non_got_ref = 0;
1745 return TRUE;
1746 }
1747
3bf083ed
AM
1748 /* If we don't find any dynamic relocs in read-only sections, then
1749 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1750 if (0 && !readonly_dynrelocs (h))
84e94c90
NC
1751 {
1752 h->non_got_ref = 0;
1753 return TRUE;
1754 }
1755
84e94c90
NC
1756 /* We must allocate the symbol in our .dynbss section, which will
1757 become part of the .bss section of the executable. There will be
1758 an entry for this symbol in the .dynsym section. The dynamic
1759 object will contain position independent code, so all references
1760 from the dynamic object to this symbol will go through the global
1761 offset table. The dynamic linker will use the .dynsym entry to
1762 determine the address it must put in the global offset table, so
1763 both the dynamic object and the regular object will refer to the
1764 same memory location for the variable. */
1765
1766 htab = lm32_elf_hash_table (info);
4dfe6ac6
NC
1767 if (htab == NULL)
1768 return FALSE;
1769
84e94c90
NC
1770 s = htab->sdynbss;
1771 BFD_ASSERT (s != NULL);
1772
1773 /* We must generate a R_LM32_COPY reloc to tell the dynamic linker
1774 to copy the initial value out of the dynamic object and into the
1775 runtime process image. We need to remember the offset into the
1776 .rela.bss section we are going to use. */
1d7e9d18 1777 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
84e94c90
NC
1778 {
1779 asection *srel;
1780
1781 srel = htab->srelbss;
1782 BFD_ASSERT (srel != NULL);
1783 srel->size += sizeof (Elf32_External_Rela);
1784 h->needs_copy = 1;
1785 }
1786
6cabe1ea 1787 return _bfd_elf_adjust_dynamic_copy (info, h, s);
84e94c90
NC
1788}
1789
1790/* Allocate space in .plt, .got and associated reloc sections for
1791 dynamic relocs. */
1792
1793static bfd_boolean
1794allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1795{
1796 struct bfd_link_info *info;
1797 struct elf_lm32_link_hash_table *htab;
1798 struct elf_lm32_link_hash_entry *eh;
3bf083ed 1799 struct elf_dyn_relocs *p;
84e94c90
NC
1800
1801 if (h->root.type == bfd_link_hash_indirect)
1802 return TRUE;
1803
84e94c90
NC
1804 info = (struct bfd_link_info *) inf;
1805 htab = lm32_elf_hash_table (info);
4dfe6ac6
NC
1806 if (htab == NULL)
1807 return FALSE;
84e94c90
NC
1808
1809 eh = (struct elf_lm32_link_hash_entry *) h;
1810
1811 if (htab->root.dynamic_sections_created
1812 && h->plt.refcount > 0)
1813 {
1814 /* Make sure this symbol is output as a dynamic symbol.
07d6d2b8 1815 Undefined weak syms won't yet be marked as dynamic. */
84e94c90 1816 if (h->dynindx == -1
07d6d2b8
AM
1817 && !h->forced_local)
1818 {
1819 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1820 return FALSE;
1821 }
84e94c90 1822
0e1862bb 1823 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
07d6d2b8
AM
1824 {
1825 asection *s = htab->root.splt;
1826
1827 /* If this is the first .plt entry, make room for the special
1828 first entry. */
1829 if (s->size == 0)
1830 s->size += PLT_ENTRY_SIZE;
1831
1832 h->plt.offset = s->size;
1833
1834 /* If this symbol is not defined in a regular file, and we are
1835 not generating a shared library, then set the symbol to this
1836 location in the .plt. This is required to make function
1837 pointers compare as equal between the normal executable and
1838 the shared library. */
1839 if (! bfd_link_pic (info)
1840 && !h->def_regular)
1841 {
1842 h->root.u.def.section = s;
1843 h->root.u.def.value = h->plt.offset;
1844 }
1845
1846 /* Make room for this entry. */
1847 s->size += PLT_ENTRY_SIZE;
1848
1849 /* We also need to make an entry in the .got.plt section, which
1850 will be placed in the .got section by the linker script. */
1851 htab->root.sgotplt->size += 4;
1852
1853 /* We also need to make an entry in the .rel.plt section. */
1854 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
1855 }
84e94c90 1856 else
07d6d2b8
AM
1857 {
1858 h->plt.offset = (bfd_vma) -1;
1859 h->needs_plt = 0;
1860 }
84e94c90
NC
1861 }
1862 else
1863 {
1864 h->plt.offset = (bfd_vma) -1;
1865 h->needs_plt = 0;
1866 }
1867
1868 if (h->got.refcount > 0)
1869 {
1870 asection *s;
1871 bfd_boolean dyn;
1872
1873 /* Make sure this symbol is output as a dynamic symbol.
07d6d2b8 1874 Undefined weak syms won't yet be marked as dynamic. */
84e94c90 1875 if (h->dynindx == -1
07d6d2b8
AM
1876 && !h->forced_local)
1877 {
1878 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1879 return FALSE;
1880 }
84e94c90 1881
ce558b89 1882 s = htab->root.sgot;
84e94c90
NC
1883
1884 h->got.offset = s->size;
1885 s->size += 4;
1886 dyn = htab->root.dynamic_sections_created;
0e1862bb 1887 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
07d6d2b8 1888 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
84e94c90
NC
1889 }
1890 else
1891 h->got.offset = (bfd_vma) -1;
1892
1893 if (eh->dyn_relocs == NULL)
1894 return TRUE;
1895
1896 /* In the shared -Bsymbolic case, discard space allocated for
1897 dynamic pc-relative relocs against symbols which turn out to be
1898 defined in regular objects. For the normal shared case, discard
1899 space for pc-relative relocs that have become local due to symbol
1900 visibility changes. */
1901
0e1862bb 1902 if (bfd_link_pic (info))
84e94c90
NC
1903 {
1904 if (h->def_regular
07d6d2b8
AM
1905 && (h->forced_local
1906 || info->symbolic))
1907 {
1908 struct elf_dyn_relocs **pp;
1909
1910 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
1911 {
1912 p->count -= p->pc_count;
1913 p->pc_count = 0;
1914 if (p->count == 0)
1915 *pp = p->next;
1916 else
1917 pp = &p->next;
1918 }
1919 }
84e94c90
NC
1920
1921 /* Also discard relocs on undefined weak syms with non-default
1922 visibility. */
1923 if (eh->dyn_relocs != NULL
1924 && h->root.type == bfd_link_hash_undefweak)
1925 {
1926 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1927 eh->dyn_relocs = NULL;
1928
1929 /* Make sure undefined weak symbols are output as a dynamic
1930 symbol in PIEs. */
1931 else if (h->dynindx == -1
1932 && !h->forced_local)
1933 {
1934 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1935 return FALSE;
1936 }
1937 }
1938 }
1939 else
1940 {
1941 /* For the non-shared case, discard space for relocs against
07d6d2b8
AM
1942 symbols which turn out to need copy relocs or are not
1943 dynamic. */
84e94c90
NC
1944
1945 if (!h->non_got_ref
07d6d2b8
AM
1946 && ((h->def_dynamic
1947 && !h->def_regular)
1948 || (htab->root.dynamic_sections_created
1949 && (h->root.type == bfd_link_hash_undefweak
1950 || h->root.type == bfd_link_hash_undefined))))
1951 {
1952 /* Make sure this symbol is output as a dynamic symbol.
1953 Undefined weak syms won't yet be marked as dynamic. */
1954 if (h->dynindx == -1
1955 && !h->forced_local)
1956 {
1957 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1958 return FALSE;
1959 }
1960
1961 /* If that succeeded, we know we'll be keeping all the
1962 relocs. */
1963 if (h->dynindx != -1)
1964 goto keep;
1965 }
84e94c90
NC
1966
1967 eh->dyn_relocs = NULL;
1968
1969 keep: ;
1970 }
1971
1972 /* Finally, allocate space. */
1973 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1974 {
1975 asection *sreloc = elf_section_data (p->sec)->sreloc;
1976 sreloc->size += p->count * sizeof (Elf32_External_Rela);
1977 }
1978
1979 return TRUE;
1980}
1981
63c1f59d
AM
1982/* Set DF_TEXTREL if we find any dynamic relocs that apply to
1983 read-only sections. */
84e94c90
NC
1984
1985static bfd_boolean
63c1f59d 1986maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
84e94c90 1987{
63c1f59d 1988 asection *sec;
84e94c90 1989
63c1f59d
AM
1990 if (h->root.type == bfd_link_hash_indirect)
1991 return TRUE;
84e94c90 1992
63c1f59d
AM
1993 sec = readonly_dynrelocs (h);
1994 if (sec != NULL)
1995 {
1996 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
84e94c90 1997
63c1f59d
AM
1998 info->flags |= DF_TEXTREL;
1999 info->callbacks->minfo
c1c8c1ef 2000 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
63c1f59d 2001 sec->owner, h->root.root.string, sec);
84e94c90 2002
63c1f59d
AM
2003 /* Not an error, just cut short the traversal. */
2004 return FALSE;
84e94c90
NC
2005 }
2006 return TRUE;
2007}
2008
2009/* Set the sizes of the dynamic sections. */
2010
2011static bfd_boolean
2012lm32_elf_size_dynamic_sections (bfd *output_bfd,
07d6d2b8 2013 struct bfd_link_info *info)
84e94c90
NC
2014{
2015 struct elf_lm32_link_hash_table *htab;
2016 bfd *dynobj;
2017 asection *s;
2018 bfd_boolean relocs;
2019 bfd *ibfd;
2020
2021 htab = lm32_elf_hash_table (info);
4dfe6ac6
NC
2022 if (htab == NULL)
2023 return FALSE;
2024
84e94c90
NC
2025 dynobj = htab->root.dynobj;
2026 BFD_ASSERT (dynobj != NULL);
2027
2028 if (htab->root.dynamic_sections_created)
2029 {
2030 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 2031 if (bfd_link_executable (info) && !info->nointerp)
84e94c90 2032 {
3d4d4302 2033 s = bfd_get_linker_section (dynobj, ".interp");
84e94c90
NC
2034 BFD_ASSERT (s != NULL);
2035 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2036 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2037 }
2038 }
2039
2040 /* Set up .got offsets for local syms, and space for local dynamic
2041 relocs. */
c72f2fb2 2042 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
84e94c90
NC
2043 {
2044 bfd_signed_vma *local_got;
2045 bfd_signed_vma *end_local_got;
2046 bfd_size_type locsymcount;
2047 Elf_Internal_Shdr *symtab_hdr;
2048 asection *srel;
2049
2050 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
07d6d2b8 2051 continue;
84e94c90
NC
2052
2053 for (s = ibfd->sections; s != NULL; s = s->next)
07d6d2b8
AM
2054 {
2055 struct elf_dyn_relocs *p;
2056
2057 for (p = ((struct elf_dyn_relocs *)
2058 elf_section_data (s)->local_dynrel);
2059 p != NULL;
2060 p = p->next)
2061 {
2062 if (! bfd_is_abs_section (p->sec)
2063 && bfd_is_abs_section (p->sec->output_section))
2064 {
2065 /* Input section has been discarded, either because
2066 it is a copy of a linkonce section or due to
2067 linker script /DISCARD/, so we'll be discarding
2068 the relocs too. */
2069 }
2070 else if (p->count != 0)
2071 {
2072 srel = elf_section_data (p->sec)->sreloc;
2073 srel->size += p->count * sizeof (Elf32_External_Rela);
2074 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2075 info->flags |= DF_TEXTREL;
2076 }
2077 }
2078 }
84e94c90
NC
2079
2080 local_got = elf_local_got_refcounts (ibfd);
2081 if (!local_got)
07d6d2b8 2082 continue;
84e94c90
NC
2083
2084 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2085 locsymcount = symtab_hdr->sh_info;
2086 end_local_got = local_got + locsymcount;
ce558b89
AM
2087 s = htab->root.sgot;
2088 srel = htab->root.srelgot;
84e94c90 2089 for (; local_got < end_local_got; ++local_got)
07d6d2b8
AM
2090 {
2091 if (*local_got > 0)
2092 {
2093 *local_got = s->size;
2094 s->size += 4;
2095 if (bfd_link_pic (info))
2096 srel->size += sizeof (Elf32_External_Rela);
2097 }
2098 else
2099 *local_got = (bfd_vma) -1;
2100 }
84e94c90
NC
2101 }
2102
2103 /* Allocate global sym .plt and .got entries, and space for global
2104 sym dynamic relocs. */
2105 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
2106
2107 /* We now have determined the sizes of the various dynamic sections.
2108 Allocate memory for them. */
2109 relocs = FALSE;
2110 for (s = dynobj->sections; s != NULL; s = s->next)
2111 {
2112 if ((s->flags & SEC_LINKER_CREATED) == 0)
07d6d2b8 2113 continue;
84e94c90 2114
ce558b89 2115 if (s == htab->root.splt
07d6d2b8
AM
2116 || s == htab->root.sgot
2117 || s == htab->root.sgotplt
84e94c90 2118 || s == htab->sdynbss)
07d6d2b8
AM
2119 {
2120 /* Strip this section if we don't need it; see the
2121 comment below. */
2122 }
84e94c90 2123 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
07d6d2b8
AM
2124 {
2125 if (s->size != 0 && s != htab->root.srelplt)
2126 relocs = TRUE;
2127
2128 /* We use the reloc_count field as a counter if we need
2129 to copy relocs into the output file. */
2130 s->reloc_count = 0;
2131 }
84e94c90
NC
2132 else
2133 /* It's not one of our sections, so don't allocate space. */
2134 continue;
2135
2136 if (s->size == 0)
07d6d2b8
AM
2137 {
2138 /* If we don't need this section, strip it from the
2139 output file. This is mostly to handle .rela.bss and
2140 .rela.plt. We must create both sections in
2141 create_dynamic_sections, because they must be created
2142 before the linker maps input sections to output
2143 sections. The linker does that before
2144 adjust_dynamic_symbol is called, and it is that
2145 function which decides whether anything needs to go
2146 into these sections. */
2147 s->flags |= SEC_EXCLUDE;
2148 continue;
2149 }
84e94c90
NC
2150
2151 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2152 continue;
2153
2154 /* Allocate memory for the section contents. We use bfd_zalloc
07d6d2b8
AM
2155 here in case unused entries are not reclaimed before the
2156 section's contents are written out. This should not happen,
2157 but this way if it does, we get a R_LM32_NONE reloc instead
2158 of garbage. */
84e94c90
NC
2159 s->contents = bfd_zalloc (dynobj, s->size);
2160 if (s->contents == NULL)
07d6d2b8 2161 return FALSE;
84e94c90
NC
2162 }
2163
2164 if (htab->root.dynamic_sections_created)
2165 {
2166 /* Add some entries to the .dynamic section. We fill in the
2167 values later, in lm32_elf_finish_dynamic_sections, but we
2168 must add the entries now so that we get the correct size for
2169 the .dynamic section. The DT_DEBUG entry is filled in by the
2170 dynamic linker and used by the debugger. */
2171#define add_dynamic_entry(TAG, VAL) \
2172 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2173
0e1862bb 2174 if (bfd_link_executable (info))
84e94c90
NC
2175 {
2176 if (! add_dynamic_entry (DT_DEBUG, 0))
2177 return FALSE;
2178 }
2179
ce558b89 2180 if (htab->root.splt->size != 0)
07d6d2b8
AM
2181 {
2182 if (! add_dynamic_entry (DT_PLTGOT, 0)
2183 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2184 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2185 || ! add_dynamic_entry (DT_JMPREL, 0))
2186 return FALSE;
2187 }
84e94c90
NC
2188
2189 if (relocs)
07d6d2b8
AM
2190 {
2191 if (! add_dynamic_entry (DT_RELA, 0)
2192 || ! add_dynamic_entry (DT_RELASZ, 0)
2193 || ! add_dynamic_entry (DT_RELAENT,
2194 sizeof (Elf32_External_Rela)))
2195 return FALSE;
2196
2197 /* If any dynamic relocs apply to a read-only section,
2198 then we need a DT_TEXTREL entry. */
2199 if ((info->flags & DF_TEXTREL) == 0)
2200 elf_link_hash_traverse (&htab->root, maybe_set_textrel, info);
2201
2202 if ((info->flags & DF_TEXTREL) != 0)
2203 {
2204 if (! add_dynamic_entry (DT_TEXTREL, 0))
2205 return FALSE;
2206 }
2207 }
84e94c90
NC
2208 }
2209#undef add_dynamic_entry
2210
2211 /* Allocate .rofixup section. */
2212 if (IS_FDPIC (output_bfd))
2213 {
2214 struct weak_symbol_list *list_start = NULL, *list_end = NULL;
2215 int rgot_weak_count = 0;
2216 int r32_count = 0;
2217 int rgot_count = 0;
2218 /* Look for deleted sections. */
c72f2fb2 2219 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
07d6d2b8
AM
2220 {
2221 for (s = ibfd->sections; s != NULL; s = s->next)
2222 {
2223 if (s->reloc_count)
2224 {
2225 /* Count relocs that need .rofixup entires. */
2226 Elf_Internal_Rela *internal_relocs, *end;
2227 internal_relocs = elf_section_data (s)->relocs;
2228 if (internal_relocs == NULL)
2229 internal_relocs = (_bfd_elf_link_read_relocs (ibfd, s, NULL, NULL, FALSE));
2230 if (internal_relocs != NULL)
2231 {
2232 end = internal_relocs + s->reloc_count;
2233 while (internal_relocs < end)
2234 {
2235 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2236 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
2237 unsigned long r_symndx;
2238 struct elf_link_hash_entry *h;
2239
2240 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2241 sym_hashes = elf_sym_hashes (ibfd);
2242 r_symndx = ELF32_R_SYM (internal_relocs->r_info);
2243 h = NULL;
2244 if (r_symndx < symtab_hdr->sh_info)
2245 {
2246 }
2247 else
2248 {
2249 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2250 while (h->root.type == bfd_link_hash_indirect
2251 || h->root.type == bfd_link_hash_warning)
2252 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2253 }
2254
2255 /* Don't generate entries for weak symbols. */
2256 if (!h || (h && h->root.type != bfd_link_hash_undefweak))
2257 {
2258 if (!discarded_section (s) && !((bfd_get_section_flags (ibfd, s) & SEC_ALLOC) == 0))
2259 {
2260 switch (ELF32_R_TYPE (internal_relocs->r_info))
2261 {
2262 case R_LM32_32:
2263 r32_count++;
2264 break;
2265 case R_LM32_16_GOT:
2266 rgot_count++;
2267 break;
2268 }
2269 }
2270 }
2271 else
2272 {
2273 struct weak_symbol_list *current, *new_entry;
2274 /* Is this symbol already in the list? */
2275 for (current = list_start; current; current = current->next)
2276 {
2277 if (!strcmp (current->name, h->root.root.string))
2278 break;
2279 }
2280 if (!current && !discarded_section (s) && (bfd_get_section_flags (ibfd, s) & SEC_ALLOC))
2281 {
2282 /* Will this have an entry in the GOT. */
2283 if (ELF32_R_TYPE (internal_relocs->r_info) == R_LM32_16_GOT)
2284 {
2285 /* Create a new entry. */
2286 new_entry = malloc (sizeof (struct weak_symbol_list));
2287 if (!new_entry)
2288 return FALSE;
2289 new_entry->name = h->root.root.string;
2290 new_entry->next = NULL;
2291 /* Add to list */
2292 if (list_start == NULL)
2293 {
2294 list_start = new_entry;
2295 list_end = new_entry;
2296 }
2297 else
2298 {
2299 list_end->next = new_entry;
2300 list_end = new_entry;
2301 }
2302 /* Increase count of undefined weak symbols in the got. */
2303 rgot_weak_count++;
2304 }
2305 }
2306 }
2307 internal_relocs++;
2308 }
2309 }
2310 else
2311 return FALSE;
2312 }
2313 }
2314 }
84e94c90
NC
2315 /* Free list. */
2316 while (list_start)
07d6d2b8
AM
2317 {
2318 list_end = list_start->next;
2319 free (list_start);
2320 list_start = list_end;
2321 }
84e94c90
NC
2322
2323 /* Size sections. */
ce558b89
AM
2324 lm32fdpic_fixup32_section (info)->size
2325 = (r32_count + (htab->root.sgot->size / 4) - rgot_weak_count + 1) * 4;
84e94c90 2326 if (lm32fdpic_fixup32_section (info)->size == 0)
07d6d2b8 2327 lm32fdpic_fixup32_section (info)->flags |= SEC_EXCLUDE;
84e94c90 2328 else
07d6d2b8
AM
2329 {
2330 lm32fdpic_fixup32_section (info)->contents =
2331 bfd_zalloc (dynobj, lm32fdpic_fixup32_section (info)->size);
2332 if (lm32fdpic_fixup32_section (info)->contents == NULL)
2333 return FALSE;
2334 }
84e94c90
NC
2335 }
2336
2337 return TRUE;
2338}
2339
2340/* Create dynamic sections when linking against a dynamic object. */
2341
2342static bfd_boolean
2343lm32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2344{
2345 struct elf_lm32_link_hash_table *htab;
2346 flagword flags, pltflags;
2347 asection *s;
2348 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2349 int ptralign = 2; /* 32bit */
2350
2351 htab = lm32_elf_hash_table (info);
4dfe6ac6
NC
2352 if (htab == NULL)
2353 return FALSE;
84e94c90
NC
2354
2355 /* Make sure we have a GOT - For the case where we have a dynamic object
2356 but none of the relocs in check_relocs */
ce558b89 2357 if (!_bfd_elf_create_got_section (abfd, info))
84e94c90
NC
2358 return FALSE;
2359 if (IS_FDPIC (abfd) && (htab->sfixup32 == NULL))
2360 {
2361 if (! create_rofixup_section (abfd, info))
07d6d2b8 2362 return FALSE;
84e94c90
NC
2363 }
2364
2365 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2366 .rel[a].bss sections. */
2367 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
07d6d2b8 2368 | SEC_LINKER_CREATED);
84e94c90
NC
2369
2370 pltflags = flags;
2371 pltflags |= SEC_CODE;
2372 if (bed->plt_not_loaded)
2373 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2374 if (bed->plt_readonly)
2375 pltflags |= SEC_READONLY;
2376
3d4d4302 2377 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
ce558b89 2378 htab->root.splt = s;
84e94c90
NC
2379 if (s == NULL
2380 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2381 return FALSE;
2382
2383 if (bed->want_plt_sym)
2384 {
2385 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
07d6d2b8 2386 .plt section. */
84e94c90
NC
2387 struct bfd_link_hash_entry *bh = NULL;
2388 struct elf_link_hash_entry *h;
2389
2390 if (! (_bfd_generic_link_add_one_symbol
07d6d2b8
AM
2391 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2392 (bfd_vma) 0, NULL, FALSE,
2393 get_elf_backend_data (abfd)->collect, &bh)))
2394 return FALSE;
84e94c90
NC
2395 h = (struct elf_link_hash_entry *) bh;
2396 h->def_regular = 1;
2397 h->type = STT_OBJECT;
2398 htab->root.hplt = h;
2399
0e1862bb 2400 if (bfd_link_pic (info)
07d6d2b8
AM
2401 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2402 return FALSE;
84e94c90
NC
2403 }
2404
3d4d4302
AM
2405 s = bfd_make_section_anyway_with_flags (abfd,
2406 bed->default_use_rela_p
2407 ? ".rela.plt" : ".rel.plt",
2408 flags | SEC_READONLY);
ce558b89 2409 htab->root.srelplt = s;
84e94c90
NC
2410 if (s == NULL
2411 || ! bfd_set_section_alignment (abfd, s, ptralign))
2412 return FALSE;
2413
ce558b89
AM
2414 if (htab->root.sgot == NULL
2415 && !_bfd_elf_create_got_section (abfd, info))
84e94c90
NC
2416 return FALSE;
2417
84e94c90
NC
2418 if (bed->want_dynbss)
2419 {
2420 /* The .dynbss section is a place to put symbols which are defined
07d6d2b8
AM
2421 by dynamic objects, are referenced by regular objects, and are
2422 not functions. We must allocate space for them in the process
2423 image and use a R_*_COPY reloc to tell the dynamic linker to
2424 initialize them at run time. The linker script puts the .dynbss
2425 section into the .bss section of the final image. */
3d4d4302
AM
2426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
2427 SEC_ALLOC | SEC_LINKER_CREATED);
84e94c90
NC
2428 htab->sdynbss = s;
2429 if (s == NULL)
07d6d2b8 2430 return FALSE;
84e94c90 2431 /* The .rel[a].bss section holds copy relocs. This section is not
07d6d2b8
AM
2432 normally needed. We need to create it here, though, so that the
2433 linker will map it to an output section. We can't just create it
2434 only if we need it, because we will not know whether we need it
2435 until we have seen all the input files, and the first time the
2436 main linker code calls BFD after examining all the input files
2437 (size_dynamic_sections) the input sections have already been
2438 mapped to the output sections. If the section turns out not to
2439 be needed, we can discard it later. We will never need this
2440 section when generating a shared object, since they do not use
2441 copy relocs. */
0e1862bb 2442 if (! bfd_link_pic (info))
07d6d2b8
AM
2443 {
2444 s = bfd_make_section_anyway_with_flags (abfd,
3d4d4302
AM
2445 (bed->default_use_rela_p
2446 ? ".rela.bss" : ".rel.bss"),
2447 flags | SEC_READONLY);
07d6d2b8
AM
2448 htab->srelbss = s;
2449 if (s == NULL
2450 || ! bfd_set_section_alignment (abfd, s, ptralign))
2451 return FALSE;
2452 }
84e94c90
NC
2453 }
2454
2455 return TRUE;
2456}
2457
2458/* Copy the extra info we tack onto an elf_link_hash_entry. */
2459
2460static void
2461lm32_elf_copy_indirect_symbol (struct bfd_link_info *info,
07d6d2b8
AM
2462 struct elf_link_hash_entry *dir,
2463 struct elf_link_hash_entry *ind)
84e94c90
NC
2464{
2465 struct elf_lm32_link_hash_entry * edir;
2466 struct elf_lm32_link_hash_entry * eind;
2467
2468 edir = (struct elf_lm32_link_hash_entry *) dir;
2469 eind = (struct elf_lm32_link_hash_entry *) ind;
2470
2471 if (eind->dyn_relocs != NULL)
2472 {
2473 if (edir->dyn_relocs != NULL)
07d6d2b8
AM
2474 {
2475 struct elf_dyn_relocs **pp;
2476 struct elf_dyn_relocs *p;
2477
2478 /* Add reloc counts against the indirect sym to the direct sym
2479 list. Merge any entries against the same section. */
2480 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
2481 {
2482 struct elf_dyn_relocs *q;
2483
2484 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2485 if (q->sec == p->sec)
2486 {
2487 q->pc_count += p->pc_count;
2488 q->count += p->count;
2489 *pp = p->next;
2490 break;
2491 }
2492 if (q == NULL)
2493 pp = &p->next;
2494 }
2495 *pp = edir->dyn_relocs;
2496 }
84e94c90
NC
2497
2498 edir->dyn_relocs = eind->dyn_relocs;
2499 eind->dyn_relocs = NULL;
2500 }
2501
2502 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2503}
2504
2505static bfd_boolean
04c3a755 2506lm32_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
84e94c90 2507{
0e1862bb 2508 if (!bfd_link_relocatable (info))
84e94c90 2509 {
04c3a755
NS
2510 if (!bfd_elf_stack_segment_size (output_bfd, info,
2511 "__stacksize", DEFAULT_STACK_SIZE))
2512 return FALSE;
84e94c90 2513
84e94c90 2514 asection *sec = bfd_get_section_by_name (output_bfd, ".stack");
84e94c90 2515 if (sec)
04c3a755 2516 sec->size = info->stacksize >= 0 ? info->stacksize : 0;
84e94c90
NC
2517 }
2518
2519 return TRUE;
2520}
2521
84e94c90
NC
2522static bfd_boolean
2523lm32_elf_fdpic_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2524{
2525 unsigned i;
2526
2527 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2528 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2529 return TRUE;
2530
e2349352 2531 if (! _bfd_elf_copy_private_bfd_data (ibfd, obfd))
84e94c90
NC
2532 return FALSE;
2533
2534 if (! elf_tdata (ibfd) || ! elf_tdata (ibfd)->phdr
2535 || ! elf_tdata (obfd) || ! elf_tdata (obfd)->phdr)
2536 return TRUE;
2537
2538 /* Copy the stack size. */
2539 for (i = 0; i < elf_elfheader (ibfd)->e_phnum; i++)
2540 if (elf_tdata (ibfd)->phdr[i].p_type == PT_GNU_STACK)
2541 {
2542 Elf_Internal_Phdr *iphdr = &elf_tdata (ibfd)->phdr[i];
2543
2544 for (i = 0; i < elf_elfheader (obfd)->e_phnum; i++)
2545 if (elf_tdata (obfd)->phdr[i].p_type == PT_GNU_STACK)
2546 {
2547 memcpy (&elf_tdata (obfd)->phdr[i], iphdr, sizeof (*iphdr));
2548
2549 /* Rewrite the phdrs, since we're only called after they were first written. */
2550 if (bfd_seek (obfd, (bfd_signed_vma) get_elf_backend_data (obfd)
2551 ->s->sizeof_ehdr, SEEK_SET) != 0
2552 || get_elf_backend_data (obfd)->s->write_out_phdrs (obfd, elf_tdata (obfd)->phdr,
2553 elf_elfheader (obfd)->e_phnum) != 0)
2554 return FALSE;
2555 break;
2556 }
2557
2558 break;
2559 }
2560
2561 return TRUE;
2562}
2563
2564
07d6d2b8 2565#define ELF_ARCH bfd_arch_lm32
ae95ffa6 2566#define ELF_TARGET_ID LM32_ELF_DATA
07d6d2b8
AM
2567#define ELF_MACHINE_CODE EM_LATTICEMICO32
2568#define ELF_MAXPAGESIZE 0x1000
2569
2570#define TARGET_BIG_SYM lm32_elf32_vec
2571#define TARGET_BIG_NAME "elf32-lm32"
2572
2573#define bfd_elf32_bfd_reloc_type_lookup lm32_reloc_type_lookup
2574#define bfd_elf32_bfd_reloc_name_lookup lm32_reloc_name_lookup
2575#define elf_info_to_howto lm32_info_to_howto_rela
f3185997 2576#define elf_info_to_howto_rel NULL
07d6d2b8
AM
2577#define elf_backend_rela_normal 1
2578#define elf_backend_object_p lm32_elf_object_p
2579#define elf_backend_final_write_processing lm32_elf_final_write_processing
04c3a755 2580#define elf_backend_stack_align 8
07d6d2b8
AM
2581#define elf_backend_can_gc_sections 1
2582#define elf_backend_can_refcount 1
2583#define elf_backend_gc_mark_hook lm32_elf_gc_mark_hook
2584#define elf_backend_plt_readonly 1
2585#define elf_backend_want_got_plt 1
2586#define elf_backend_want_plt_sym 0
2587#define elf_backend_got_header_size 12
64f52338 2588#define elf_backend_dtrel_excludes_plt 1
07d6d2b8
AM
2589#define bfd_elf32_bfd_link_hash_table_create lm32_elf_link_hash_table_create
2590#define elf_backend_check_relocs lm32_elf_check_relocs
2591#define elf_backend_reloc_type_class lm32_elf_reloc_type_class
2592#define elf_backend_copy_indirect_symbol lm32_elf_copy_indirect_symbol
2593#define elf_backend_size_dynamic_sections lm32_elf_size_dynamic_sections
d00dd7dc 2594#define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
07d6d2b8
AM
2595#define elf_backend_create_dynamic_sections lm32_elf_create_dynamic_sections
2596#define elf_backend_finish_dynamic_sections lm32_elf_finish_dynamic_sections
2597#define elf_backend_adjust_dynamic_symbol lm32_elf_adjust_dynamic_symbol
2598#define elf_backend_finish_dynamic_symbol lm32_elf_finish_dynamic_symbol
2599#define elf_backend_relocate_section lm32_elf_relocate_section
84e94c90
NC
2600
2601#include "elf32-target.h"
2602
2603#undef ELF_MAXPAGESIZE
2604#define ELF_MAXPAGESIZE 0x4000
2605
2606
2607#undef TARGET_BIG_SYM
07d6d2b8
AM
2608#define TARGET_BIG_SYM lm32_elf32_fdpic_vec
2609#undef TARGET_BIG_NAME
84e94c90
NC
2610#define TARGET_BIG_NAME "elf32-lm32fdpic"
2611#undef elf32_bed
2612#define elf32_bed elf32_lm32fdpic_bed
2613
07d6d2b8
AM
2614#undef elf_backend_always_size_sections
2615#define elf_backend_always_size_sections lm32_elf_always_size_sections
2616#undef bfd_elf32_bfd_copy_private_bfd_data
2617#define bfd_elf32_bfd_copy_private_bfd_data lm32_elf_fdpic_copy_private_bfd_data
84e94c90
NC
2618
2619#include "elf32-target.h"
This page took 0.829022 seconds and 4 git commands to generate.