]> Git Repo - binutils.git/blob - bfd/elf32-m68k.c
* Makefile.in (install): Move subdir_do out of conditional. From
[binutils.git] / bfd / elf32-m68k.c
1 /* Motorola 68k series support for 32-bit ELF
2    Copyright 1993, 1995, 1996 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "bfdlink.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25
26 static reloc_howto_type *reloc_type_lookup
27   PARAMS ((bfd *, bfd_reloc_code_real_type));
28 static void rtype_to_howto
29   PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
30 static void rtype_to_howto_rel
31   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
32 static boolean elf_m68k_check_relocs
33   PARAMS ((bfd *, struct bfd_link_info *, asection *,
34            const Elf_Internal_Rela *));
35 static boolean elf_m68k_adjust_dynamic_symbol
36   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
37 static boolean elf_m68k_size_dynamic_sections
38   PARAMS ((bfd *, struct bfd_link_info *));
39 static boolean elf_m68k_relocate_section
40   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
41            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
42 static boolean elf_m68k_finish_dynamic_symbol
43   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
44            Elf_Internal_Sym *));
45 static boolean elf_m68k_finish_dynamic_sections
46   PARAMS ((bfd *, struct bfd_link_info *));
47
48 /* elf32 m68k code, generated by elf.el */
49 enum reloc_type {
50   R_68K_NONE        =   0,
51   R_68K_32          =   1,
52   R_68K_16          =   2,
53   R_68K_8           =   3,
54   R_68K_PC32        =   4,
55   R_68K_PC16        =   5,
56   R_68K_PC8         =   6,
57   R_68K_GOT32       =   7,
58   R_68K_GOT16       =   8,
59   R_68K_GOT8        =   9,
60   R_68K_GOT32O      =  10,
61   R_68K_GOT16O      =  11,
62   R_68K_GOT8O       =  12,
63   R_68K_PLT32       =  13,
64   R_68K_PLT16       =  14,
65   R_68K_PLT8        =  15,
66   R_68K_PLT32O      =  16,
67   R_68K_PLT16O      =  17,
68   R_68K_PLT8O       =  18,
69   R_68K_COPY        =  19,
70   R_68K_GLOB_DAT    =  20,
71   R_68K_JMP_SLOT    =  21,
72   R_68K_RELATIVE    =  22,
73   R_68K__max
74 };
75
76 static reloc_howto_type howto_table[] = {
77   HOWTO(R_68K_NONE,       0, 0, 0, false,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_NONE",      false, 0, 0x00000000,false),
78   HOWTO(R_68K_32,         0, 2,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32",        false, 0, 0xffffffff,false),
79   HOWTO(R_68K_16,         0, 1,16, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16",        false, 0, 0x0000ffff,false),
80   HOWTO(R_68K_8,          0, 0, 8, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8",         false, 0, 0x000000ff,false),
81   HOWTO(R_68K_PC32,       0, 2,32, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PC32",      false, 0, 0xffffffff,true),
82   HOWTO(R_68K_PC16,       0, 1,16, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PC16",      false, 0, 0x0000ffff,true),
83   HOWTO(R_68K_PC8,        0, 0, 8, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PC8",       false, 0, 0x000000ff,true),
84   HOWTO(R_68K_GOT32,      0, 2,32, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT32",     false, 0, 0xffffffff,true),
85   HOWTO(R_68K_GOT16,      0, 1,16, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT16",     false, 0, 0x0000ffff,true),
86   HOWTO(R_68K_GOT8,       0, 0, 8, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT8",      false, 0, 0x000000ff,true),
87   HOWTO(R_68K_GOT32O,     0, 2,32, false,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT32O",    false, 0, 0xffffffff,false),
88   HOWTO(R_68K_GOT16O,     0, 1,16, false,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT16O",    false, 0, 0x0000ffff,false),
89   HOWTO(R_68K_GOT8O,      0, 0, 8, false,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT8O",     false, 0, 0x000000ff,false),
90   HOWTO(R_68K_PLT32,      0, 2,32, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT32",     false, 0, 0xffffffff,true),
91   HOWTO(R_68K_PLT16,      0, 1,16, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT16",     false, 0, 0x0000ffff,true),
92   HOWTO(R_68K_PLT8,       0, 0, 8, true, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT8",      false, 0, 0x000000ff,true),
93   HOWTO(R_68K_PLT32O,     0, 2,32, false,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT32O",    false, 0, 0xffffffff,false),
94   HOWTO(R_68K_PLT16O,     0, 1,16, false,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT16O",    false, 0, 0x0000ffff,false),
95   HOWTO(R_68K_PLT8O,      0, 0, 8, false,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT8O",     false, 0, 0x000000ff,false),
96   HOWTO(R_68K_COPY,       0, 0, 0, false,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_COPY",      false, 0, 0xffffffff,false),
97   HOWTO(R_68K_GLOB_DAT,   0, 2,32, false,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_GLOB_DAT",  false, 0, 0xffffffff,false),
98   HOWTO(R_68K_JMP_SLOT,   0, 2,32, false,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_JMP_SLOT",  false, 0, 0xffffffff,false),
99   HOWTO(R_68K_RELATIVE,   0, 2,32, false,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_RELATIVE",  false, 0, 0xffffffff,false),
100 };
101
102 static void
103 rtype_to_howto (abfd, cache_ptr, dst)
104      bfd *abfd;
105      arelent *cache_ptr;
106      Elf_Internal_Rela *dst;
107 {
108   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K__max);
109   cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
110 }
111
112 static void
113 rtype_to_howto_rel (abfd, cache_ptr, dst)
114      bfd *abfd;
115      arelent *cache_ptr;
116      Elf_Internal_Rel *dst;
117 {
118   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K__max);
119   cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
120 }
121
122 #define elf_info_to_howto rtype_to_howto
123 #define elf_info_to_howto_rel rtype_to_howto_rel
124
125 static const struct { unsigned char bfd_val, elf_val; } reloc_map[] = {
126   { BFD_RELOC_NONE, R_68K_NONE },
127   { BFD_RELOC_32, R_68K_32 },
128   { BFD_RELOC_16, R_68K_16 },
129   { BFD_RELOC_8, R_68K_8 },
130   { BFD_RELOC_32_PCREL, R_68K_PC32 },
131   { BFD_RELOC_16_PCREL, R_68K_PC16 },
132   { BFD_RELOC_8_PCREL, R_68K_PC8 },
133   { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
134   { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
135   { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
136   { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
137   { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
138   { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
139   { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
140   { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
141   { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
142   { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
143   { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
144   { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
145   { BFD_RELOC_NONE, R_68K_COPY },
146   { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
147   { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
148   { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
149   { BFD_RELOC_CTOR, R_68K_32 },
150 };
151
152 static reloc_howto_type *
153 reloc_type_lookup (abfd, code)
154      bfd *abfd;
155      bfd_reloc_code_real_type code;
156 {
157   unsigned int i;
158   for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
159     {
160       if (reloc_map[i].bfd_val == code)
161         return &howto_table[(int) reloc_map[i].elf_val];
162     }
163   return 0;
164 }
165
166 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
167 #define ELF_ARCH bfd_arch_m68k
168 /* end code generated by elf.el */
169
170 #define USE_RELA
171
172 \f
173 /* Functions for the m68k ELF linker.  */
174
175 /* The name of the dynamic interpreter.  This is put in the .interp
176    section.  */
177
178 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
179
180 /* The size in bytes of an entry in the procedure linkage table.  */
181
182 #define PLT_ENTRY_SIZE 20
183
184 /* The first entry in a procedure linkage table looks like this.  See
185    the SVR4 ABI m68k supplement to see how this works.  */
186
187 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
188 {
189   0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
190   0, 0, 0, 0,             /* replaced with address of .got + 4.  */
191   0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
192   0, 0, 0, 0,             /* replaced with address of .got + 8.  */
193   0, 0, 0, 0              /* pad out to 20 bytes.  */
194 };
195
196 /* Subsequent entries in a procedure linkage table look like this.  */
197
198 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
199 {
200   0x4e, 0xfb, 0x01, 0x71, /* jmp ([addr]) */
201   0, 0, 0, 0,             /* replaced with address of this symbol in .got.  */
202   0x2f, 0x3c,             /* move.l #offset,-(%sp) */
203   0, 0, 0, 0,             /* replaced with offset into relocation table.  */
204   0x60, 0xff,             /* bra.l .plt */
205   0, 0, 0, 0              /* replaced with offset to start of .plt.  */
206 };
207
208 /* Look through the relocs for a section during the first phase, and
209    allocate space in the global offset table or procedure linkage
210    table.  */
211
212 static boolean
213 elf_m68k_check_relocs (abfd, info, sec, relocs)
214      bfd *abfd;
215      struct bfd_link_info *info;
216      asection *sec;
217      const Elf_Internal_Rela *relocs;
218 {
219   bfd *dynobj;
220   Elf_Internal_Shdr *symtab_hdr;
221   struct elf_link_hash_entry **sym_hashes;
222   bfd_vma *local_got_offsets;
223   const Elf_Internal_Rela *rel;
224   const Elf_Internal_Rela *rel_end;
225   asection *sgot;
226   asection *srelgot;
227   asection *sreloc;
228
229   if (info->relocateable)
230     return true;
231
232   dynobj = elf_hash_table (info)->dynobj;
233   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
234   sym_hashes = elf_sym_hashes (abfd);
235   local_got_offsets = elf_local_got_offsets (abfd);
236
237   sgot = NULL;
238   srelgot = NULL;
239   sreloc = NULL;
240
241   rel_end = relocs + sec->reloc_count;
242   for (rel = relocs; rel < rel_end; rel++)
243     {
244       unsigned long r_symndx;
245       struct elf_link_hash_entry *h;
246
247       r_symndx = ELF32_R_SYM (rel->r_info);
248
249       if (r_symndx < symtab_hdr->sh_info)
250         h = NULL;
251       else
252         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
253
254       switch (ELF32_R_TYPE (rel->r_info))
255         {
256         case R_68K_GOT8:
257         case R_68K_GOT16:
258         case R_68K_GOT32:
259         case R_68K_GOT8O:
260         case R_68K_GOT16O:
261         case R_68K_GOT32O:
262           /* This symbol requires a global offset table entry.  */
263
264           if (h != NULL
265               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
266             break;
267
268           if (dynobj == NULL)
269             {
270               /* Create the .got section.  */
271               elf_hash_table (info)->dynobj = dynobj = abfd;
272               if (!_bfd_elf_create_got_section (dynobj, info))
273                 return false;
274             }
275
276           if (sgot == NULL)
277             {
278               sgot = bfd_get_section_by_name (dynobj, ".got");
279               BFD_ASSERT (sgot != NULL);
280             }
281
282           if (srelgot == NULL
283               && (h != NULL || info->shared))
284             {
285               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
286               if (srelgot == NULL)
287                 {
288                   srelgot = bfd_make_section (dynobj, ".rela.got");
289                   if (srelgot == NULL
290                       || !bfd_set_section_flags (dynobj, srelgot,
291                                                  (SEC_ALLOC
292                                                   | SEC_LOAD
293                                                   | SEC_HAS_CONTENTS
294                                                   | SEC_IN_MEMORY
295                                                   | SEC_READONLY))
296                       || !bfd_set_section_alignment (dynobj, srelgot, 2))
297                     return false;
298                 }
299             }
300
301           if (h != NULL)
302             {
303               if (h->got_offset != (bfd_vma) -1)
304                 {
305                   /* We have already allocated space in the .got.  */
306                   break;
307                 }
308               h->got_offset = sgot->_raw_size;
309
310               /* Make sure this symbol is output as a dynamic symbol.  */
311               if (h->dynindx == -1)
312                 {
313                   if (!bfd_elf32_link_record_dynamic_symbol (info, h))
314                     return false;
315                 }
316
317               srelgot->_raw_size += sizeof (Elf32_External_Rela);
318             }
319           else
320             {
321               /* This is a global offset table entry for a local
322                  symbol.  */
323               if (local_got_offsets == NULL)
324                 {
325                   size_t size;
326                   register unsigned int i;
327
328                   size = symtab_hdr->sh_info * sizeof (bfd_vma);
329                   local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
330                   if (local_got_offsets == NULL)
331                     return false;
332                   elf_local_got_offsets (abfd) = local_got_offsets;
333                   for (i = 0; i < symtab_hdr->sh_info; i++)
334                     local_got_offsets[i] = (bfd_vma) -1;
335                 }
336               if (local_got_offsets[r_symndx] != (bfd_vma) -1)
337                 {
338                   /* We have already allocated space in the .got.  */
339                   break;
340                 }
341               local_got_offsets[r_symndx] = sgot->_raw_size;
342
343               if (info->shared)
344                 {
345                   /* If we are generating a shared object, we need to
346                      output a R_68K_RELATIVE reloc so that the dynamic
347                      linker can adjust this GOT entry.  */
348                   srelgot->_raw_size += sizeof (Elf32_External_Rela);
349                 }
350             }
351
352           sgot->_raw_size += 4;
353           break;
354
355         case R_68K_PLT8:
356         case R_68K_PLT16:
357         case R_68K_PLT32:
358         case R_68K_PLT8O:
359         case R_68K_PLT16O:
360         case R_68K_PLT32O:
361           /* This symbol requires a procedure linkage table entry.  We
362              actually build the entry in adjust_dynamic_symbol,
363              because this might be a case of linking PIC code without
364              linking in any dynamic objects, in which case we don't
365              need to generate a procedure linkage table after all.  */
366           
367           /* If this is a local symbol, we resolve it directly without
368              creating a procedure linkage table entry.  */
369           if (h == NULL)
370             continue;
371
372           /* Make sure this symbol is output as a dynamic symbol.  */
373           if (h->dynindx == -1)
374             {
375               if (!bfd_elf32_link_record_dynamic_symbol (info, h))
376                 return false;
377             }
378
379           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
380           break;
381
382         case R_68K_PC8:
383         case R_68K_PC16:
384         case R_68K_PC32:
385           if (h == NULL)
386             break;
387           /* Fall through.  */
388         case R_68K_8:
389         case R_68K_16:
390         case R_68K_32:
391           if (info->shared
392               && (sec->flags & SEC_ALLOC) != 0)
393             {
394               /* When creating a shared object, we must copy these
395                  reloc types into the output file.  We create a reloc
396                  section in dynobj and make room for this reloc.  */
397               if (sreloc == NULL)
398                 {
399                   const char *name;
400
401                   name = (bfd_elf_string_from_elf_section
402                           (abfd,
403                            elf_elfheader (abfd)->e_shstrndx,
404                            elf_section_data (sec)->rel_hdr.sh_name));
405                   if (name == NULL)
406                     return false;
407
408                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
409                               && strcmp (bfd_get_section_name (abfd, sec),
410                                          name + 5) == 0);
411
412                   sreloc = bfd_get_section_by_name (dynobj, name);
413                   if (sreloc == NULL)
414                     {
415                       sreloc = bfd_make_section (dynobj, name);
416                       if (sreloc == NULL
417                           || !bfd_set_section_flags (dynobj, sreloc,
418                                                      (SEC_ALLOC
419                                                       | SEC_LOAD
420                                                       | SEC_HAS_CONTENTS
421                                                       | SEC_IN_MEMORY
422                                                       | SEC_READONLY))
423                           || !bfd_set_section_alignment (dynobj, sreloc, 2))
424                         return false;
425                     }
426                 }
427
428               sreloc->_raw_size += sizeof (Elf32_External_Rela);
429             }
430
431           break;
432
433         default:
434           break;
435         }
436     }
437
438   return true;
439 }
440
441 /* Adjust a symbol defined by a dynamic object and referenced by a
442    regular object.  The current definition is in some section of the
443    dynamic object, but we're not including those sections.  We have to
444    change the definition to something the rest of the link can
445    understand.  */
446
447 static boolean
448 elf_m68k_adjust_dynamic_symbol (info, h)
449      struct bfd_link_info *info;
450      struct elf_link_hash_entry *h;
451 {
452   bfd *dynobj;
453   asection *s;
454   unsigned int power_of_two;
455
456   dynobj = elf_hash_table (info)->dynobj;
457
458   /* Make sure we know what is going on here.  */
459   BFD_ASSERT (dynobj != NULL
460               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
461                   || h->weakdef != NULL
462                   || ((h->elf_link_hash_flags
463                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
464                       && (h->elf_link_hash_flags
465                           & ELF_LINK_HASH_REF_REGULAR) != 0
466                       && (h->elf_link_hash_flags
467                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
468
469   /* If this is a function, put it in the procedure linkage table.  We
470      will fill in the contents of the procedure linkage table later,
471      when we know the address of the .got section.  */
472   if (h->type == STT_FUNC
473       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
474     {
475       if (!elf_hash_table (info)->dynamic_sections_created)
476         {
477           /* This case can occur if we saw a PLT32 reloc in an input
478              file, but none of the input files were dynamic objects.
479              In such a case, we don't actually need to build a
480              procedure linkage table, and we can just do a PC32 reloc
481              instead.  */
482           BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
483           return true;
484         }
485
486       s = bfd_get_section_by_name (dynobj, ".plt");
487       BFD_ASSERT (s != NULL);
488
489       /* If this is the first .plt entry, make room for the special
490          first entry.  */
491       if (s->_raw_size == 0)
492         s->_raw_size += PLT_ENTRY_SIZE;
493
494       /* If this symbol is not defined in a regular file, and we are
495          not generating a shared library, then set the symbol to this
496          location in the .plt.  This is required to make function
497          pointers compare as equal between the normal executable and
498          the shared library.  */
499       if (!info->shared
500           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
501         {
502           h->root.u.def.section = s;
503           h->root.u.def.value = s->_raw_size;
504         }
505
506       h->plt_offset = s->_raw_size;
507
508       /* Make room for this entry.  */
509       s->_raw_size += PLT_ENTRY_SIZE;
510
511       /* We also need to make an entry in the .got.plt section, which
512          will be placed in the .got section by the linker script.  */
513
514       s = bfd_get_section_by_name (dynobj, ".got.plt");
515       BFD_ASSERT (s != NULL);
516       s->_raw_size += 4;
517
518       /* We also need to make an entry in the .rela.plt section.  */
519
520       s = bfd_get_section_by_name (dynobj, ".rela.plt");
521       BFD_ASSERT (s != NULL);
522       s->_raw_size += sizeof (Elf32_External_Rela);
523
524       return true;
525     }
526
527   /* If this is a weak symbol, and there is a real definition, the
528      processor independent code will have arranged for us to see the
529      real definition first, and we can just use the same value.  */
530   if (h->weakdef != NULL)
531     {
532       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
533                   || h->weakdef->root.type == bfd_link_hash_defweak);
534       h->root.u.def.section = h->weakdef->root.u.def.section;
535       h->root.u.def.value = h->weakdef->root.u.def.value;
536       return true;
537     }
538
539   /* This is a reference to a symbol defined by a dynamic object which
540      is not a function.  */
541
542   /* If we are creating a shared library, we must presume that the
543      only references to the symbol are via the global offset table.
544      For such cases we need not do anything here; the relocations will
545      be handled correctly by relocate_section.  */
546   if (info->shared)
547     return true;
548
549   /* We must allocate the symbol in our .dynbss section, which will
550      become part of the .bss section of the executable.  There will be
551      an entry for this symbol in the .dynsym section.  The dynamic
552      object will contain position independent code, so all references
553      from the dynamic object to this symbol will go through the global
554      offset table.  The dynamic linker will use the .dynsym entry to
555      determine the address it must put in the global offset table, so
556      both the dynamic object and the regular object will refer to the
557      same memory location for the variable.  */
558
559   s = bfd_get_section_by_name (dynobj, ".dynbss");
560   BFD_ASSERT (s != NULL);
561
562   /* If the symbol is currently defined in the .bss section of the
563      dynamic object, then it is OK to simply initialize it to zero.
564      If the symbol is in some other section, we must generate a
565      R_68K_COPY reloc to tell the dynamic linker to copy the initial
566      value out of the dynamic object and into the runtime process
567      image.  We need to remember the offset into the .rela.bss section
568      we are going to use.  */
569   if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
570     {
571       asection *srel;
572
573       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
574       BFD_ASSERT (srel != NULL);
575       srel->_raw_size += sizeof (Elf32_External_Rela);
576       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
577     }
578
579   /* We need to figure out the alignment required for this symbol.  I
580      have no idea how ELF linkers handle this.  */
581   power_of_two = bfd_log2 (h->size);
582   if (power_of_two > 3)
583     power_of_two = 3;
584
585   /* Apply the required alignment.  */
586   s->_raw_size = BFD_ALIGN (s->_raw_size,
587                             (bfd_size_type) (1 << power_of_two));
588   if (power_of_two > bfd_get_section_alignment (dynobj, s))
589     {
590       if (!bfd_set_section_alignment (dynobj, s, power_of_two))
591         return false;
592     }
593
594   /* Define the symbol as being at this point in the section.  */
595   h->root.u.def.section = s;
596   h->root.u.def.value = s->_raw_size;
597
598   /* Increment the section size to make room for the symbol.  */
599   s->_raw_size += h->size;
600
601   return true;
602 }
603
604 /* Set the sizes of the dynamic sections.  */
605
606 static boolean
607 elf_m68k_size_dynamic_sections (output_bfd, info)
608      bfd *output_bfd;
609      struct bfd_link_info *info;
610 {
611   bfd *dynobj;
612   asection *s;
613   boolean plt;
614   boolean relocs;
615   boolean reltext;
616
617   dynobj = elf_hash_table (info)->dynobj;
618   BFD_ASSERT (dynobj != NULL);
619
620   if (elf_hash_table (info)->dynamic_sections_created)
621     {
622       /* Set the contents of the .interp section to the interpreter.  */
623       if (!info->shared)
624         {
625           s = bfd_get_section_by_name (dynobj, ".interp");
626           BFD_ASSERT (s != NULL);
627           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
628           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
629         }
630     }
631   else
632     {
633       /* We may have created entries in the .rela.got section.
634          However, if we are not creating the dynamic sections, we will
635          not actually use these entries.  Reset the size of .rela.got,
636          which will cause it to get stripped from the output file
637          below.  */
638       s = bfd_get_section_by_name (dynobj, ".rela.got");
639       if (s != NULL)
640         s->_raw_size = 0;
641     }
642
643   /* The check_relocs and adjust_dynamic_symbol entry points have
644      determined the sizes of the various dynamic sections.  Allocate
645      memory for them.  */
646   plt = false;
647   relocs = false;
648   reltext = false;
649   for (s = dynobj->sections; s != NULL; s = s->next)
650     {
651       const char *name;
652       boolean strip;
653
654       if ((s->flags & SEC_IN_MEMORY) == 0)
655         continue;
656
657       /* It's OK to base decisions on the section name, because none
658          of the dynobj section names depend upon the input files.  */
659       name = bfd_get_section_name (dynobj, s);
660
661       strip = false;
662
663       if (strcmp (name, ".plt") == 0)
664         {
665           if (s->_raw_size == 0)
666             {
667               /* Strip this section if we don't need it; see the
668                  comment below.  */
669               strip = true;
670             }
671           else
672             {
673               /* Remember whether there is a PLT.  */
674               plt = true;
675             }
676         }
677       else if (strncmp (name, ".rela", 5) == 0)
678         {
679           if (s->_raw_size == 0)
680             {
681               /* If we don't need this section, strip it from the
682                  output file.  This is mostly to handle .rela.bss and
683                  .rela.plt.  We must create both sections in
684                  create_dynamic_sections, because they must be created
685                  before the linker maps input sections to output
686                  sections.  The linker does that before
687                  adjust_dynamic_symbol is called, and it is that
688                  function which decides whether anything needs to go
689                  into these sections.  */
690               strip = true;
691             }
692           else
693             {
694               asection *target;
695
696               /* Remember whether there are any reloc sections other
697                  than .rela.plt.  */
698               if (strcmp (name, ".rela.plt") != 0)
699                 {
700                   relocs = true;
701
702                   /* If this relocation section applies to a read only
703                      section, then we probably need a DT_TEXTREL
704                      entry.  .rela.plt is actually associated with
705                      .got.plt, which is never readonly.  */
706                   target = bfd_get_section_by_name (output_bfd, name + 5);
707                   if (target != NULL
708                       && (target->flags & SEC_READONLY) != 0)
709                     reltext = true;
710                 }
711
712               /* We use the reloc_count field as a counter if we need
713                  to copy relocs into the output file.  */
714               s->reloc_count = 0;
715             }
716         }
717       else if (strncmp (name, ".got", 4) != 0)
718         {
719           /* It's not one of our sections, so don't allocate space.  */
720           continue;
721         }
722
723       if (strip)
724         {
725           asection **spp;
726
727           for (spp = &s->output_section->owner->sections;
728                *spp != s->output_section;
729                spp = &(*spp)->next)
730             ;
731           *spp = s->output_section->next;
732           --s->output_section->owner->section_count;
733
734           continue;
735         }
736
737       /* Allocate memory for the section contents.  */
738       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
739       if (s->contents == NULL && s->_raw_size != 0)
740         return false;
741     }
742
743   if (elf_hash_table (info)->dynamic_sections_created)
744     {
745       /* Add some entries to the .dynamic section.  We fill in the
746          values later, in elf_m68k_finish_dynamic_sections, but we
747          must add the entries now so that we get the correct size for
748          the .dynamic section.  The DT_DEBUG entry is filled in by the
749          dynamic linker and used by the debugger.  */
750       if (!info->shared)
751         {
752           if (!bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
753             return false;
754         }
755
756       if (plt)
757         {
758           if (!bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
759               || !bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
760               || !bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
761               || !bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
762             return false;
763         }
764
765       if (relocs)
766         {
767           if (!bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
768               || !bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
769               || !bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
770                                                sizeof (Elf32_External_Rela)))
771             return false;
772         }
773
774       if (reltext)
775         {
776           if (!bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
777             return false;
778         }
779     }
780
781   return true;
782 }
783
784 /* Relocate an M68K ELF section.  */
785
786 static boolean
787 elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
788                            contents, relocs, local_syms, local_sections)
789      bfd *output_bfd;
790      struct bfd_link_info *info;
791      bfd *input_bfd;
792      asection *input_section;
793      bfd_byte *contents;
794      Elf_Internal_Rela *relocs;
795      Elf_Internal_Sym *local_syms;
796      asection **local_sections;
797 {
798   bfd *dynobj;
799   Elf_Internal_Shdr *symtab_hdr;
800   struct elf_link_hash_entry **sym_hashes;
801   bfd_vma *local_got_offsets;
802   asection *sgot;
803   asection *sgotplt;
804   asection *splt;
805   asection *sreloc;
806   Elf_Internal_Rela *rel;
807   Elf_Internal_Rela *relend;
808
809   dynobj = elf_hash_table (info)->dynobj;
810   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
811   sym_hashes = elf_sym_hashes (input_bfd);
812   local_got_offsets = elf_local_got_offsets (input_bfd);
813
814   sgot = NULL;
815   sgotplt = NULL;
816   splt = NULL;
817   sreloc = NULL;
818
819   rel = relocs;
820   relend = relocs + input_section->reloc_count;
821   for (; rel < relend; rel++)
822     {
823       int r_type;
824       reloc_howto_type *howto;
825       unsigned long r_symndx;
826       struct elf_link_hash_entry *h;
827       Elf_Internal_Sym *sym;
828       asection *sec;
829       bfd_vma relocation;
830       bfd_reloc_status_type r;
831
832       r_type = ELF32_R_TYPE (rel->r_info);
833       if (r_type < 0 || r_type >= (int) R_68K__max)
834         {
835           bfd_set_error (bfd_error_bad_value);
836           return false;
837         }
838       howto = howto_table + r_type;
839
840       r_symndx = ELF32_R_SYM (rel->r_info);
841
842       if (info->relocateable)
843         {
844           /* This is a relocateable link.  We don't have to change
845              anything, unless the reloc is against a section symbol,
846              in which case we have to adjust according to where the
847              section symbol winds up in the output section.  */
848           if (r_symndx < symtab_hdr->sh_info)
849             {
850               sym = local_syms + r_symndx;
851               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
852                 {
853                   sec = local_sections[r_symndx];
854                   rel->r_addend += sec->output_offset + sym->st_value;
855                 }
856             }
857
858           continue;
859         }
860
861       /* This is a final link.  */
862       h = NULL;
863       sym = NULL;
864       sec = NULL;
865       if (r_symndx < symtab_hdr->sh_info)
866         {
867           sym = local_syms + r_symndx;
868           sec = local_sections[r_symndx];
869           relocation = (sec->output_section->vma
870                         + sec->output_offset
871                         + sym->st_value);
872         }
873       else
874         {
875           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
876           while (h->root.type == bfd_link_hash_indirect
877                  || h->root.type == bfd_link_hash_warning)
878             h = (struct elf_link_hash_entry *) h->root.u.i.link;
879           if (h->root.type == bfd_link_hash_defined
880               || h->root.type == bfd_link_hash_defweak)
881             {
882               sec = h->root.u.def.section;
883               if (((r_type == R_68K_PLT8
884                     || r_type == R_68K_PLT16
885                     || r_type == R_68K_PLT32
886                     || r_type == R_68K_PLT8O
887                     || r_type == R_68K_PLT16O
888                     || r_type == R_68K_PLT32O)
889                    && h->plt_offset != (bfd_vma) -1)
890                   || ((r_type == R_68K_GOT8O
891                        || r_type == R_68K_GOT16O
892                        || r_type == R_68K_GOT32O
893                        || ((r_type == R_68K_GOT8
894                             || r_type == R_68K_GOT16
895                             || r_type == R_68K_GOT32)
896                            && strcmp (h->root.root.string,
897                                       "_GLOBAL_OFFSET_TABLE_") != 0))
898                       && elf_hash_table (info)->dynamic_sections_created
899                       && (! info->shared
900                           || ! info->symbolic
901                           || (h->elf_link_hash_flags
902                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
903                   || (info->shared
904                       && (! info->symbolic
905                           || (h->elf_link_hash_flags
906                               & ELF_LINK_HASH_DEF_REGULAR) == 0)
907                       && (input_section->flags & SEC_ALLOC) != 0
908                       && (r_type == R_68K_8
909                           || r_type == R_68K_16
910                           || r_type == R_68K_32
911                           || r_type == R_68K_PC8
912                           || r_type == R_68K_PC16
913                           || r_type == R_68K_PC32)))
914                 {
915                   /* In these cases, we don't need the relocation
916                      value.  We check specially because in some
917                      obscure cases sec->output_section will be NULL.  */
918                   relocation = 0;
919                 }
920               else
921                 relocation = (h->root.u.def.value
922                               + sec->output_section->vma
923                               + sec->output_offset);
924             }
925           else if (h->root.type == bfd_link_hash_undefweak)
926             relocation = 0;
927           else if (info->shared && !info->symbolic)
928             relocation = 0;
929           else
930             {
931               if (!(info->callbacks->undefined_symbol
932                     (info, h->root.root.string, input_bfd,
933                      input_section, rel->r_offset)))
934                 return false;
935               relocation = 0;
936             }
937         }
938
939       switch (r_type)
940         {
941         case R_68K_GOT8:
942         case R_68K_GOT16:
943         case R_68K_GOT32:
944           /* Relocation is to the entry for this symbol in the global
945              offset table.  */
946           if (h != NULL
947               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
948             break;
949           /* Fall through.  */
950         case R_68K_GOT8O:
951         case R_68K_GOT16O:
952         case R_68K_GOT32O:
953           /* Relocation is the offset of the entry for this symbol in
954              the global offset table.  */
955
956           if (sgot == NULL)
957             {
958               sgot = bfd_get_section_by_name (dynobj, ".got");
959               BFD_ASSERT (sgot != NULL);
960             }
961
962           if (sgotplt == NULL)
963             {
964               sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
965               BFD_ASSERT (sgotplt != NULL);
966             }
967
968           if (h != NULL)
969             {
970               bfd_vma off;
971
972               off = h->got_offset;
973               BFD_ASSERT (off != (bfd_vma) -1);
974
975               if (!elf_hash_table (info)->dynamic_sections_created
976                   || (info->shared
977                       && info->symbolic
978                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
979                 {
980                   /* This is actually a static link, or it is a
981                      -Bsymbolic link and the symbol is defined
982                      locally.  We must initialize this entry in the
983                      global offset table.  Since the offset must
984                      always be a multiple of 4, we use the least
985                      significant bit to record whether we have
986                      initialized it already.
987
988                      When doing a dynamic link, we create a .rela.got
989                      relocation entry to initialize the value.  This
990                      is done in the finish_dynamic_symbol routine.  */
991                   if ((off & 1) != 0)
992                     off &= ~1;
993                   else
994                     {
995                       bfd_put_32 (output_bfd, relocation,
996                                   sgot->contents + off);
997                       h->got_offset |= 1;
998                     }
999                 }
1000
1001               relocation = sgot->output_offset + off;
1002               if (r_type == R_68K_GOT8O
1003                   || r_type == R_68K_GOT16O
1004                   || r_type == R_68K_GOT32O)
1005                 relocation -= sgotplt->output_offset;
1006             }
1007           else
1008             {
1009               bfd_vma off;
1010
1011               BFD_ASSERT (local_got_offsets != NULL
1012                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
1013
1014               off = local_got_offsets[r_symndx];
1015
1016               /* The offset must always be a multiple of 4.  We use
1017                  the least significant bit to record whether we have
1018                  already generated the necessary reloc.  */
1019               if ((off & 1) != 0)
1020                 off &= ~1;
1021               else
1022                 {
1023                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1024
1025                   if (info->shared)
1026                     {
1027                       asection *srelgot;
1028                       Elf_Internal_Rela outrel;
1029
1030                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1031                       BFD_ASSERT (srelgot != NULL);
1032
1033                       outrel.r_offset = (sgot->output_section->vma
1034                                          + sgot->output_offset
1035                                          + off);
1036                       outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1037                       outrel.r_addend = 0;
1038                       bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1039                                                  (((Elf32_External_Rela *)
1040                                                    srelgot->contents)
1041                                                   + srelgot->reloc_count));
1042                       ++srelgot->reloc_count;
1043                     }
1044
1045                   local_got_offsets[r_symndx] |= 1;
1046                 }
1047
1048               relocation = sgot->output_offset + off;
1049               if (r_type == R_68K_GOT8O
1050                   || r_type == R_68K_GOT16O
1051                   || r_type == R_68K_GOT32O)
1052                 relocation -= sgotplt->output_offset;
1053             }
1054
1055           break;
1056
1057         case R_68K_PLT8:
1058         case R_68K_PLT16:
1059         case R_68K_PLT32:
1060           /* Relocation is to the entry for this symbol in the
1061              procedure linkage table.  */
1062
1063           /* Resolve a PLT32 reloc against a local symbol directly,
1064              without using the procedure linkage table.  */
1065           if (h == NULL)
1066             break;
1067
1068           if (h->plt_offset == (bfd_vma) -1)
1069             {
1070               /* We didn't make a PLT entry for this symbol.  This
1071                  happens when statically linking PIC code, or when
1072                  using -Bsymbolic.  */
1073               break;
1074             }
1075
1076           if (splt == NULL)
1077             {
1078               splt = bfd_get_section_by_name (dynobj, ".plt");
1079               BFD_ASSERT (splt != NULL);
1080             }
1081
1082           relocation = (splt->output_section->vma
1083                         + splt->output_offset
1084                         + h->plt_offset);
1085           break;
1086
1087         case R_68K_PLT8O:
1088         case R_68K_PLT16O:
1089         case R_68K_PLT32O:
1090           /* Relocation is the offset of the entry for this symbol in
1091              the procedure linkage table.  */
1092           BFD_ASSERT (h != NULL);
1093
1094           if (h->plt_offset == (bfd_vma) -1)
1095             {
1096               /* We didn't make a PLT entry for this symbol.  This
1097                  happens when statically linking PIC code.  */
1098               break;
1099             }
1100
1101           if (splt == NULL)
1102             {
1103               splt = bfd_get_section_by_name (dynobj, ".plt");
1104               BFD_ASSERT (splt != NULL);
1105             }
1106
1107           relocation = h->plt_offset;
1108           break;
1109
1110         case R_68K_PC8:
1111         case R_68K_PC16:
1112         case R_68K_PC32:
1113           if (h == NULL)
1114             break;
1115           /* Fall through.  */
1116         case R_68K_8:
1117         case R_68K_16:
1118         case R_68K_32:
1119           if (info->shared
1120               && (input_section->flags & SEC_ALLOC) != 0)
1121             {
1122               Elf_Internal_Rela outrel;
1123
1124               /* When generating a shared object, these relocations
1125                  are copied into the output file to be resolved at run
1126                  time.  */
1127
1128               if (sreloc == NULL)
1129                 {
1130                   const char *name;
1131
1132                   name = (bfd_elf_string_from_elf_section
1133                           (input_bfd,
1134                            elf_elfheader (input_bfd)->e_shstrndx,
1135                            elf_section_data (input_section)->rel_hdr.sh_name));
1136                   if (name == NULL)
1137                     return false;
1138
1139                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1140                               && strcmp (bfd_get_section_name (input_bfd,
1141                                                                input_section),
1142                                          name + 5) == 0);
1143
1144                   sreloc = bfd_get_section_by_name (dynobj, name);
1145                   BFD_ASSERT (sreloc != NULL);
1146                 }
1147
1148               outrel.r_offset = (rel->r_offset
1149                                  + input_section->output_section->vma
1150                                  + input_section->output_offset);
1151               if (h != NULL
1152                   && (! info->symbolic
1153                       || (h->elf_link_hash_flags
1154                           & ELF_LINK_HASH_DEF_REGULAR) == 0))
1155                 {
1156                   BFD_ASSERT (h->dynindx != -1);
1157                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1158                   outrel.r_addend = rel->r_addend;
1159                 }
1160               else
1161                 {
1162                   if (r_type == R_68K_32)
1163                     {
1164                       outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1165                       outrel.r_addend = relocation + rel->r_addend;
1166                     }
1167                   else
1168                     {
1169                       long indx;
1170
1171                       if (h == NULL)
1172                         sec = local_sections[r_symndx];
1173                       else
1174                         {
1175                           BFD_ASSERT (h->root.type == bfd_link_hash_defined
1176                                       || (h->root.type
1177                                           == bfd_link_hash_defweak));
1178                           sec = h->root.u.def.section;
1179                         }
1180                       if (sec != NULL && bfd_is_abs_section (sec))
1181                         indx = 0;
1182                       else if (sec == NULL || sec->owner == NULL)
1183                         {
1184                           bfd_set_error (bfd_error_bad_value);
1185                           return false;
1186                         }
1187                       else
1188                         {
1189                           asection *osec;
1190
1191                           osec = sec->output_section;
1192                           indx = elf_section_data (osec)->dynindx;
1193                           if (indx == 0)
1194                             abort ();
1195                         }
1196
1197                       outrel.r_info = ELF32_R_INFO (indx, r_type);
1198                       outrel.r_addend = relocation + rel->r_addend;
1199                     }
1200                 }
1201
1202               bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1203                                          (((Elf32_External_Rela *)
1204                                            sreloc->contents)
1205                                           + sreloc->reloc_count));
1206               ++sreloc->reloc_count;
1207
1208               /* This reloc will be computed at runtime, so there's no
1209                  need to do anything now.  */
1210               continue;
1211             }
1212
1213           break;
1214
1215         default:
1216           break;
1217         }
1218
1219       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1220                                     contents, rel->r_offset,
1221                                     relocation, rel->r_addend);
1222
1223       if (r != bfd_reloc_ok)
1224         {
1225           switch (r)
1226             {
1227             default:
1228             case bfd_reloc_outofrange:
1229               abort ();
1230             case bfd_reloc_overflow:
1231               {
1232                 const char *name;
1233
1234                 if (h != NULL)
1235                   name = h->root.root.string;
1236                 else
1237                   {
1238                     name = bfd_elf_string_from_elf_section (input_bfd,
1239                                                             symtab_hdr->sh_link,
1240                                                             sym->st_name);
1241                     if (name == NULL)
1242                       return false;
1243                     if (*name == '\0')
1244                       name = bfd_section_name (input_bfd, sec);
1245                   }
1246                 if (!(info->callbacks->reloc_overflow
1247                       (info, name, howto->name, (bfd_vma) 0,
1248                        input_bfd, input_section, rel->r_offset)))
1249                   return false;
1250               }
1251               break;
1252             }
1253         }
1254     }
1255
1256   return true;
1257 }
1258
1259 /* Finish up dynamic symbol handling.  We set the contents of various
1260    dynamic sections here.  */
1261
1262 static boolean
1263 elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
1264      bfd *output_bfd;
1265      struct bfd_link_info *info;
1266      struct elf_link_hash_entry *h;
1267      Elf_Internal_Sym *sym;
1268 {
1269   bfd *dynobj;
1270
1271   dynobj = elf_hash_table (info)->dynobj;
1272
1273   if (h->plt_offset != (bfd_vma) -1)
1274     {
1275       asection *splt;
1276       asection *sgot;
1277       asection *srela;
1278       bfd_vma plt_index;
1279       bfd_vma got_offset;
1280       Elf_Internal_Rela rela;
1281
1282       /* This symbol has an entry in the procedure linkage table.  Set
1283          it up.  */
1284
1285       BFD_ASSERT (h->dynindx != -1);
1286
1287       splt = bfd_get_section_by_name (dynobj, ".plt");
1288       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1289       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1290       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1291
1292       /* Get the index in the procedure linkage table which
1293          corresponds to this symbol.  This is the index of this symbol
1294          in all the symbols for which we are making plt entries.  The
1295          first entry in the procedure linkage table is reserved.  */
1296       plt_index = h->plt_offset / PLT_ENTRY_SIZE - 1;
1297
1298       /* Get the offset into the .got table of the entry that
1299          corresponds to this function.  Each .got entry is 4 bytes.
1300          The first three are reserved.  */
1301       got_offset = (plt_index + 3) * 4;
1302
1303       /* Fill in the entry in the procedure linkage table.  */
1304       memcpy (splt->contents + h->plt_offset, elf_m68k_plt_entry,
1305               PLT_ENTRY_SIZE);
1306       /* The offset is relative to the first extension word.  */
1307       bfd_put_32 (output_bfd,
1308                   (sgot->output_section->vma
1309                    + sgot->output_offset
1310                    + got_offset
1311                    - (splt->output_section->vma
1312                       + h->plt_offset + 2)),
1313                   splt->contents + h->plt_offset + 4);
1314
1315       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
1316                   splt->contents + h->plt_offset + 10);
1317       bfd_put_32 (output_bfd, - (h->plt_offset + 16),
1318                   splt->contents + h->plt_offset + 16);
1319
1320       /* Fill in the entry in the global offset table.  */
1321       bfd_put_32 (output_bfd,
1322                   (splt->output_section->vma
1323                    + splt->output_offset
1324                    + h->plt_offset
1325                    + 8),
1326                   sgot->contents + got_offset);
1327
1328       /* Fill in the entry in the .rela.plt section.  */
1329       rela.r_offset = (sgot->output_section->vma
1330                        + sgot->output_offset
1331                        + got_offset);
1332       rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
1333       rela.r_addend = 0;
1334       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1335                                  ((Elf32_External_Rela *) srela->contents
1336                                   + plt_index));
1337
1338       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1339         {
1340           /* Mark the symbol as undefined, rather than as defined in
1341              the .plt section.  Leave the value alone.  */
1342           sym->st_shndx = SHN_UNDEF;
1343         }
1344     }
1345
1346   if (h->got_offset != (bfd_vma) -1)
1347     {
1348       asection *sgot;
1349       asection *srela;
1350       Elf_Internal_Rela rela;
1351
1352       /* This symbol has an entry in the global offset table.  Set it
1353          up.  */
1354       
1355       BFD_ASSERT (h->dynindx != -1);
1356
1357       sgot = bfd_get_section_by_name (dynobj, ".got");
1358       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1359       BFD_ASSERT (sgot != NULL && srela != NULL);
1360
1361       rela.r_offset = (sgot->output_section->vma
1362                        + sgot->output_offset
1363                        + (h->got_offset &~ 1));
1364
1365       /* If this is a -Bsymbolic link, and the symbol is defined
1366          locally, we just want to emit a RELATIVE reloc.  The entry in
1367          the global offset table will already have been initialized in
1368          the relocate_section function.  */
1369       if (info->shared
1370           && info->symbolic
1371           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1372         rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1373       else
1374         {
1375           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
1376           rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
1377         }
1378
1379       rela.r_addend = 0;
1380       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1381                                  ((Elf32_External_Rela *) srela->contents
1382                                   + srela->reloc_count));
1383       ++srela->reloc_count;
1384     }
1385
1386   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1387     {
1388       asection *s;
1389       Elf_Internal_Rela rela;
1390
1391       /* This symbol needs a copy reloc.  Set it up.  */
1392
1393       BFD_ASSERT (h->dynindx != -1
1394                   && (h->root.type == bfd_link_hash_defined
1395                       || h->root.type == bfd_link_hash_defweak));
1396
1397       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1398                                    ".rela.bss");
1399       BFD_ASSERT (s != NULL);
1400
1401       rela.r_offset = (h->root.u.def.value
1402                        + h->root.u.def.section->output_section->vma
1403                        + h->root.u.def.section->output_offset);
1404       rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
1405       rela.r_addend = 0;
1406       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1407                                  ((Elf32_External_Rela *) s->contents
1408                                   + s->reloc_count));
1409       ++s->reloc_count;
1410     }
1411
1412   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
1413   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1414       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1415     sym->st_shndx = SHN_ABS;
1416
1417   return true;
1418 }
1419
1420 /* Finish up the dynamic sections.  */
1421
1422 static boolean
1423 elf_m68k_finish_dynamic_sections (output_bfd, info)
1424      bfd *output_bfd;
1425      struct bfd_link_info *info;
1426 {
1427   bfd *dynobj;
1428   asection *sgot;
1429   asection *sdyn;
1430
1431   dynobj = elf_hash_table (info)->dynobj;
1432
1433   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1434   BFD_ASSERT (sgot != NULL);
1435   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1436
1437   if (elf_hash_table (info)->dynamic_sections_created)
1438     {
1439       asection *splt;
1440       Elf32_External_Dyn *dyncon, *dynconend;
1441
1442       splt = bfd_get_section_by_name (dynobj, ".plt");
1443       BFD_ASSERT (splt != NULL && sdyn != NULL);
1444
1445       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1446       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1447       for (; dyncon < dynconend; dyncon++)
1448         {
1449           Elf_Internal_Dyn dyn;
1450           const char *name;
1451           asection *s;
1452
1453           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1454
1455           switch (dyn.d_tag)
1456             {
1457             default:
1458               break;
1459
1460             case DT_PLTGOT:
1461               name = ".got";
1462               goto get_vma;
1463             case DT_JMPREL:
1464               name = ".rela.plt";
1465             get_vma:
1466               s = bfd_get_section_by_name (output_bfd, name);
1467               BFD_ASSERT (s != NULL);
1468               dyn.d_un.d_ptr = s->vma;
1469               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1470               break;
1471
1472             case DT_PLTRELSZ:
1473               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1474               BFD_ASSERT (s != NULL);
1475               if (s->_cooked_size != 0)
1476                 dyn.d_un.d_val = s->_cooked_size;
1477               else
1478                 dyn.d_un.d_val = s->_raw_size;
1479               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1480               break;
1481
1482             case DT_RELASZ:
1483               /* My reading of the SVR4 ABI indicates that the
1484                  procedure linkage table relocs (DT_JMPREL) should be
1485                  included in the overall relocs (DT_RELA).  This is
1486                  what Solaris does.  However, UnixWare can not handle
1487                  that case.  Therefore, we override the DT_RELASZ entry
1488                  here to make it not include the JMPREL relocs.  Since
1489                  the linker script arranges for .rela.plt to follow all
1490                  other relocation sections, we don't have to worry
1491                  about changing the DT_RELA entry.  */
1492               /* FIXME: This comment is from elf32-i386.c, what about
1493                  the SVR4/m68k implementations? */
1494               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1495               if (s != NULL)
1496                 {
1497                   if (s->_cooked_size != 0)
1498                     dyn.d_un.d_val -= s->_cooked_size;
1499                   else
1500                     dyn.d_un.d_val -= s->_raw_size;
1501                 }
1502               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1503               break;
1504             }
1505         }
1506
1507       /* Fill in the first entry in the procedure linkage table.  */
1508       if (splt->_raw_size > 0)
1509         {
1510           memcpy (splt->contents, elf_m68k_plt0_entry, PLT_ENTRY_SIZE);
1511           bfd_put_32 (output_bfd,
1512                       (sgot->output_section->vma
1513                        + sgot->output_offset + 4
1514                        - (splt->output_section->vma + 2)),
1515                       splt->contents + 4);
1516           bfd_put_32 (output_bfd,
1517                       (sgot->output_section->vma
1518                        + sgot->output_offset + 8
1519                        - (splt->output_section->vma + 10)),
1520                       splt->contents + 12);
1521         }
1522
1523       elf_section_data (splt->output_section)->this_hdr.sh_entsize
1524         = PLT_ENTRY_SIZE;
1525     }
1526
1527   /* Fill in the first three entries in the global offset table.  */
1528   if (sgot->_raw_size > 0)
1529     {
1530       if (sdyn == NULL)
1531         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1532       else
1533         bfd_put_32 (output_bfd,
1534                     sdyn->output_section->vma + sdyn->output_offset,
1535                     sgot->contents);
1536       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1537       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1538     }
1539
1540   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1541
1542   return true;
1543 }
1544
1545 #define TARGET_BIG_SYM                  bfd_elf32_m68k_vec
1546 #define TARGET_BIG_NAME                 "elf32-m68k"
1547 #define ELF_MACHINE_CODE                EM_68K
1548 #define ELF_MAXPAGESIZE                 0x2000
1549 #define elf_backend_create_dynamic_sections \
1550                                         _bfd_elf_create_dynamic_sections
1551 #define elf_backend_check_relocs        elf_m68k_check_relocs
1552 #define elf_backend_adjust_dynamic_symbol \
1553                                         elf_m68k_adjust_dynamic_symbol
1554 #define elf_backend_size_dynamic_sections \
1555                                         elf_m68k_size_dynamic_sections
1556 #define elf_backend_relocate_section    elf_m68k_relocate_section
1557 #define elf_backend_finish_dynamic_symbol \
1558                                         elf_m68k_finish_dynamic_symbol
1559 #define elf_backend_finish_dynamic_sections \
1560                                         elf_m68k_finish_dynamic_sections
1561 #define elf_backend_want_got_plt 1
1562 #define elf_backend_plt_readonly 1
1563 #define elf_backend_want_plt_sym 0
1564
1565 #include "elf32-target.h"
This page took 0.116705 seconds and 4 git commands to generate.