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