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