1 /* Motorola 88k series support for 32-bit ELF
2    Copyright 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004, 2005, 2006 Free Software Foundation, Inc.
4 
5    This file is part of BFD, the Binary File Descriptor library.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20 
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/m88k.h"
27 
28 static reloc_howto_type *reloc_type_lookup (bfd *, bfd_reloc_code_real_type);
29 static void rtype_to_howto (bfd *, arelent *, Elf_Internal_Rela *);
30 static struct bfd_hash_entry *elf_m88k_link_hash_newfunc
31   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
32 static struct bfd_link_hash_table *elf_m88k_link_hash_table_create (bfd *);
33 static bfd_boolean elf_m88k_check_relocs
34   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
35 static asection *elf_m88k_gc_mark_hook
36   (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
37    struct elf_link_hash_entry *, Elf_Internal_Sym *);
38 static bfd_boolean elf_m88k_gc_sweep_hook
39   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
40 static bfd_boolean elf_m88k_adjust_dynamic_symbol
41   (struct bfd_link_info *, struct elf_link_hash_entry *);
42 static bfd_boolean elf_m88k_size_dynamic_sections
43   (bfd *, struct bfd_link_info *);
44 static bfd_boolean elf_m88k_discard_copies
45   (struct elf_link_hash_entry *, PTR);
46 static bfd_boolean elf_m88k_relocate_section
47   (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
48    Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
49 static bfd_boolean elf_m88k_finish_dynamic_symbol
50   (bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
51    Elf_Internal_Sym *);
52 static bfd_boolean elf_m88k_finish_dynamic_sections
53   (bfd *, struct bfd_link_info *);
54 
55 static bfd_boolean elf32_m88k_set_private_flags (bfd *, flagword);
56 static bfd_boolean elf32_m88k_merge_private_bfd_data (bfd *, bfd *);
57 static bfd_boolean elf32_m88k_print_private_bfd_data (bfd *, PTR);
58 static enum elf_reloc_type_class elf32_m88k_reloc_type_class
59   (const Elf_Internal_Rela *);
60 
61 #define UNHANDLED_HOWTO(C) \
62   HOWTO ((C), 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL, __STRING(C), \
63 	 FALSE, 0, 0, FALSE)
64 #define UNIMPLEMENTED_HOWTO \
65   HOWTO (R_88K_UNIMPLEMENTED, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL, \
66 	 "R_88K_UNIMPLEMENTED", FALSE, 0, 0, FALSE)
67 
68 static reloc_howto_type howto_table[] = {
69   HOWTO (R_88K_NONE,       0, 0, 0, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_88K_NONE",      FALSE, 0, 0x00000000,FALSE),
70   UNHANDLED_HOWTO (R_88K_COPY),
71   UNHANDLED_HOWTO (R_88K_GOTP_ENT),
72   UNIMPLEMENTED_HOWTO,
73   HOWTO (R_88K_8,          0, 0, 8, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_88K_8",         FALSE, 0, 0x000000ff,FALSE),
74   UNHANDLED_HOWTO (R_88K_8S),
75   UNIMPLEMENTED_HOWTO,
76   UNHANDLED_HOWTO (R_88K_16S),
77   HOWTO (R_88K_DISP16,     2, 2,16, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_88K_DISP16",      FALSE, 0, 0x0000ffff,TRUE),
78   UNIMPLEMENTED_HOWTO,
79   HOWTO (R_88K_DISP26,     2, 2,26, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_88K_DISP26",      FALSE, 0, 0x03ffffff,TRUE),
80   UNIMPLEMENTED_HOWTO,
81   UNIMPLEMENTED_HOWTO,
82   UNIMPLEMENTED_HOWTO,
83   HOWTO (R_88K_PLT_DISP26, 2, 2,26, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_88K_PLT_DISP26",  FALSE, 0, 0x03ffffff,TRUE),
84   UNIMPLEMENTED_HOWTO,
85   UNHANDLED_HOWTO (R_88K_BBASED_32),
86   UNHANDLED_HOWTO (R_88K_BBASED_32UA),
87   UNHANDLED_HOWTO (R_88K_BBASED_16H),
88   UNHANDLED_HOWTO (R_88K_BBASED_16L),
89   UNIMPLEMENTED_HOWTO,
90   UNIMPLEMENTED_HOWTO,
91   UNIMPLEMENTED_HOWTO,
92   UNIMPLEMENTED_HOWTO,
93   UNHANDLED_HOWTO (R_88K_ABDIFF_32),
94   UNHANDLED_HOWTO (R_88K_ABDIFF_32UA),
95   HOWTO (R_88K_ABDIFF_16H, 16, 1,16, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_88K_ABDIFF_16H", FALSE, 0, 0x0000ffff,FALSE),
96   HOWTO (R_88K_ABDIFF_16L, 0,  1,16, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_88K_ABDIFF_16L", FALSE, 0, 0x0000ffff,FALSE),
97   UNHANDLED_HOWTO (R_88K_ABDIFF_16),
98   UNIMPLEMENTED_HOWTO,
99   UNIMPLEMENTED_HOWTO,
100   UNIMPLEMENTED_HOWTO,
101   HOWTO (R_88K_32,         0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_88K_32",        FALSE, 0, 0xffffffff,FALSE),
102   UNHANDLED_HOWTO (R_88K_32UA),
103   HOWTO (R_88K_16H,       16, 1,16, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_88K_16H",      FALSE, 0, 0x0000ffff,FALSE),
104   HOWTO (R_88K_16L,        0, 1,16, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_88K_16L",      FALSE, 0, 0x0000ffff,FALSE),
105   HOWTO (R_88K_16,         0, 1,16, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_88K_16",        FALSE, 0, 0x0000ffff,FALSE),
106   UNIMPLEMENTED_HOWTO,
107   UNIMPLEMENTED_HOWTO,
108   UNIMPLEMENTED_HOWTO,
109   UNHANDLED_HOWTO (R_88K_GOT_32),
110   UNHANDLED_HOWTO (R_88K_GOT_32UA),
111   HOWTO (R_88K_GOT_16H,    16, 1,16, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_88K_GOT_16H",    FALSE, 0, 0x0000ffff,FALSE),
112   HOWTO (R_88K_GOT_16L,    0,  1,16, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_88K_GOT_16L",    FALSE, 0, 0x0000ffff,FALSE),
113   UNHANDLED_HOWTO (R_88K_GOT_16),
114   UNIMPLEMENTED_HOWTO,
115   UNIMPLEMENTED_HOWTO,
116   UNIMPLEMENTED_HOWTO,
117   UNHANDLED_HOWTO (R_88K_GOTP_32),
118   UNHANDLED_HOWTO (R_88K_GOTP_32UA),
119   UNHANDLED_HOWTO (R_88K_GOTP_16H),
120   UNHANDLED_HOWTO (R_88K_GOTP_16L),
121   UNHANDLED_HOWTO (R_88K_GOTP_16),
122   UNIMPLEMENTED_HOWTO,
123   UNIMPLEMENTED_HOWTO,
124   UNIMPLEMENTED_HOWTO,
125   HOWTO (R_88K_PLT_32,     0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_88K_PLT_32",    FALSE, 0, 0xffffffff,FALSE),
126   UNHANDLED_HOWTO (R_88K_PLT_32UA),
127   UNHANDLED_HOWTO (R_88K_PLT_16H),
128   UNHANDLED_HOWTO (R_88K_PLT_16L),
129   UNHANDLED_HOWTO (R_88K_PLT_16),
130   UNIMPLEMENTED_HOWTO,
131   UNIMPLEMENTED_HOWTO,
132   UNIMPLEMENTED_HOWTO,
133   UNHANDLED_HOWTO (R_88K_ABREL_32),
134   UNHANDLED_HOWTO (R_88K_ABREL_32UA),
135   UNHANDLED_HOWTO (R_88K_ABREL_16H),
136   UNHANDLED_HOWTO (R_88K_ABREL_16L),
137   UNHANDLED_HOWTO (R_88K_ABREL_16),
138   UNIMPLEMENTED_HOWTO,
139   UNIMPLEMENTED_HOWTO,
140   UNIMPLEMENTED_HOWTO,
141   UNHANDLED_HOWTO (R_88K_GOT_ABREL_32),
142   UNHANDLED_HOWTO (R_88K_GOT_ABREL_32UA),
143   UNHANDLED_HOWTO (R_88K_GOT_ABREL_16H),
144   UNHANDLED_HOWTO (R_88K_GOT_ABREL_16L),
145   UNHANDLED_HOWTO (R_88K_GOT_ABREL_16),
146   UNIMPLEMENTED_HOWTO,
147   UNIMPLEMENTED_HOWTO,
148   UNIMPLEMENTED_HOWTO,
149   UNHANDLED_HOWTO (R_88K_GOTP_ABREL_32),
150   UNHANDLED_HOWTO (R_88K_GOTP_ABREL_32UA),
151   UNHANDLED_HOWTO (R_88K_GOTP_ABREL_16H),
152   UNHANDLED_HOWTO (R_88K_GOTP_ABREL_16L),
153   UNHANDLED_HOWTO (R_88K_GOTP_ABREL_16),
154   UNIMPLEMENTED_HOWTO,
155   UNIMPLEMENTED_HOWTO,
156   UNIMPLEMENTED_HOWTO,
157   UNHANDLED_HOWTO (R_88K_PLT_ABREL_32),
158   UNHANDLED_HOWTO (R_88K_PLT_ABREL_32UA),
159   UNHANDLED_HOWTO (R_88K_PLT_ABREL_16H),
160   UNHANDLED_HOWTO (R_88K_PLT_ABREL_16L),
161   UNHANDLED_HOWTO (R_88K_PLT_ABREL_16),
162   UNIMPLEMENTED_HOWTO,
163   UNIMPLEMENTED_HOWTO,
164   UNIMPLEMENTED_HOWTO,
165   UNHANDLED_HOWTO (R_88K_SREL_32),
166   UNHANDLED_HOWTO (R_88K_SREL_32UA),
167   UNHANDLED_HOWTO (R_88K_SREL_16H),
168   UNHANDLED_HOWTO (R_88K_SREL_16L),
169   /* GNU extension to record C++ vtable hierarchy */
170   HOWTO (R_88K_GNU_VTINHERIT,0,2,0, FALSE,0, complain_overflow_dont,     NULL,                  "R_88K_GNU_VTINHERIT",FALSE,0,0,        FALSE),
171   /* GNU extension to record C++ vtable member usage */
172   HOWTO (R_88K_GNU_VTENTRY,0, 2, 0, FALSE,0, complain_overflow_dont,     _bfd_elf_rel_vtable_reloc_fn,"R_88K_GNU_VTENTRY",FALSE,0,0,    FALSE)
173 };
174 
175 static void
176 rtype_to_howto (abfd, cache_ptr, dst)
177      bfd *abfd ATTRIBUTE_UNUSED;
178      arelent *cache_ptr;
179      Elf_Internal_Rela *dst;
180 {
181   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_88K_UNIMPLEMENTED);
182   cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
183 }
184 
185 #define elf_info_to_howto rtype_to_howto
186 
187 static const struct
188 {
189   bfd_reloc_code_real_type bfd_val;
190   int elf_val;
191 } reloc_map[] = {
192   { BFD_RELOC_NONE,           R_88K_NONE },
193   { BFD_RELOC_LO16,           R_88K_16L },
194   { BFD_RELOC_HI16,           R_88K_16H },
195   { BFD_RELOC_18_PCREL_S2,    R_88K_DISP16 },
196   { BFD_RELOC_28_PCREL_S2,    R_88K_DISP26 },
197   { BFD_RELOC_8,              R_88K_8 },
198   { BFD_RELOC_16,             R_88K_16 },
199   { BFD_RELOC_32,             R_88K_32 },
200 
201   { BFD_RELOC_LO16_BASEREL,   R_88K_ABDIFF_16L },
202   { BFD_RELOC_HI16_BASEREL,   R_88K_ABDIFF_16H },
203   { BFD_RELOC_LO16_GOTOFF,    R_88K_GOT_16L },
204   { BFD_RELOC_HI16_GOTOFF,    R_88K_GOT_16H },
205   { BFD_RELOC_32_PLTOFF,      R_88K_PLT_32 },
206   { BFD_RELOC_32_PLT_PCREL,   R_88K_PLT_DISP26 },
207 
208   { BFD_RELOC_VTABLE_INHERIT, R_88K_GNU_VTINHERIT },
209   { BFD_RELOC_VTABLE_ENTRY,   R_88K_GNU_VTENTRY }
210 };
211 
212 static reloc_howto_type *
213 reloc_type_lookup (abfd, code)
214      bfd *abfd ATTRIBUTE_UNUSED;
215      bfd_reloc_code_real_type code;
216 {
217   unsigned int i;
218   for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
219     {
220       if (reloc_map[i].bfd_val == code)
221 	return &howto_table[reloc_map[i].elf_val];
222     }
223   return 0;
224 }
225 
226 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
227 #define ELF_ARCH bfd_arch_m88k
228 
229 /* Functions for the m88k ELF linker.  */
230 
231 /* The name of the dynamic interpreter.  This is put in the .interp
232    section.  */
233 
234 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
235 
236 /* The size in bytes of an entry in the procedure linkage table.  */
237 
238 #define PLT_ENTRY_SIZE 24
239 
240 /* The size in bytes of the first entry in the procedure linkage table.  */
241 
242 #define PLT_ENTRY0_SIZE (3 * PLT_ENTRY_SIZE)
243 
244 /* The first entry in a procedure linkage table looks like this.  See
245    the SVR4 ABI m88k supplement to see how this works.  */
246 
247 static const bfd_byte elf_m88k_plt0_entry[PLT_ENTRY0_SIZE] =
248 {
249   0x67, 0xff, 0x00, 0x10, /*	subu r31, r31, 0x10             */
250   0x24, 0x3f, 0x00, 0x0c, /*	st   r1,  r31, 0x0c             */
251   0x25, 0x7f, 0x00, 0x08, /*	st   r11, r31, 0x08             */
252   0xc8, 0x00, 0x00, 0x01, /*	bsr  1f                         */
253   0x5d, 0x60, 0x00, 0x00, /* 1:	or.u r11, r0,  %hi16(1b#abdiff) */
254   0x59, 0x6b, 0x00, 0x00, /*	or   r11, r11, %lo16(1b#abdiff) */
255   0xf5, 0x6b, 0x60, 0x01, /*	addu r11, r11, r1               */
256   0x14, 0x2b, 0x00, 0x04, /*	ld   r1,  r11, 0x04             */
257   0x24, 0x3f, 0x00, 0x04, /*	st   r1,  r31, 0x04             */
258   0x15, 0x6b, 0x00, 0x08, /*	ld   r11, r11, 0x08             */
259   0x24, 0x1f, 0x00, 0x00, /*	st   r0,  r31, 0                */
260   0xf4, 0x00, 0xc8, 0x0b, /*	jsr  r11                        */
261   0xf4, 0x00, 0x58, 0x00, /*	nop                             */
262   0xf4, 0x00, 0x58, 0x00, /*	nop                             */
263   0xf4, 0x00, 0x58, 0x00, /*	nop                             */
264   0xf4, 0x00, 0x58, 0x00, /*	nop                             */
265   0xf4, 0x00, 0x58, 0x00, /*	nop                             */
266   0xf4, 0x00, 0x58, 0x00  /*	nop                             */
267 };
268 
269 /* Subsequent entries in a procedure linkage table look like this.  */
270 
271 static const bfd_byte elf_m88k_plt_entry[PLT_ENTRY_SIZE] =
272 {
273   0x5d, 0x60, 0x00, 0x00, /*	or.u r11, r0,  %hi16(symbol#gotrel) */
274   0x15, 0x6b, 0x00, 0x00, /*	ld   r11, r11, %lo16(symbol#gotrel) */
275   0xf4, 0x00, 0xc0, 0x0b, /*	jmp  r11                            */
276   0x5d, 0x60, 0x00, 0x00, /* 1:	or.u r11, r0,  %hi16(.rela offset)  */
277   0x59, 0x6b, 0x00, 0x00, /*	or   r11, r11, %lo16(.rela offset)  */
278   0xc0, 0x00, 0x00, 0x00  /*	br   resolver                       */
279 };
280 
281 /* The m88k linker needs to keep track of the number of relocs that it
282    decides to copy in check_relocs for each symbol.  This is so that it
283    can discard PC relative relocs if it doesn't need them when linking
284    with -Bsymbolic.  We store the information in a field extending the
285    regular ELF linker hash table.  */
286 
287 /* This structure keeps track of the number of PC relative relocs we have
288    copied for a given symbol.  */
289 
290 struct elf_m88k_pcrel_relocs_copied
291 {
292   /* Next section.  */
293   struct elf_m88k_pcrel_relocs_copied *next;
294   /* A section in dynobj.  */
295   asection *section;
296   /* Number of relocs copied in this section.  */
297   bfd_size_type count;
298 };
299 
300 /* m88k ELF linker hash entry.  */
301 
302 struct elf_m88k_link_hash_entry
303 {
304   struct elf_link_hash_entry root;
305 
306   /* Number of PC relative relocs copied for this symbol.  */
307   struct elf_m88k_pcrel_relocs_copied *pcrel_relocs_copied;
308 };
309 
310 #define elf_m88k_hash_entry(ent) ((struct elf_m88k_link_hash_entry *) (ent))
311 
312 /* m88k ELF linker hash table.  */
313 
314 struct elf_m88k_link_hash_table
315 {
316   struct elf_link_hash_table root;
317 
318   /* Small local sym to section mapping cache.  */
319   struct sym_sec_cache sym_sec;
320 };
321 
322 /* Get the m88k ELF linker hash table from a link_info structure.  */
323 
324 #define elf_m88k_hash_table(p) \
325   ((struct elf_m88k_link_hash_table *) (p)->hash)
326 
327 /* Create an entry in an m88k ELF linker hash table.  */
328 
329 static struct bfd_hash_entry *
330 elf_m88k_link_hash_newfunc (entry, table, string)
331      struct bfd_hash_entry *entry;
332      struct bfd_hash_table *table;
333      const char *string;
334 {
335   struct bfd_hash_entry *ret = entry;
336 
337   /* Allocate the structure if it has not already been allocated by a
338      subclass.  */
339   if (ret == NULL)
340     ret = bfd_hash_allocate (table,
341 			     sizeof (struct elf_m88k_link_hash_entry));
342   if (ret == NULL)
343     return ret;
344 
345   /* Call the allocation method of the superclass.  */
346   ret = _bfd_elf_link_hash_newfunc (ret, table, string);
347   if (ret != NULL)
348     elf_m88k_hash_entry (ret)->pcrel_relocs_copied = NULL;
349 
350   return ret;
351 }
352 
353 /* Create an m88k ELF linker hash table.  */
354 
355 static struct bfd_link_hash_table *
356 elf_m88k_link_hash_table_create (abfd)
357      bfd *abfd;
358 {
359   struct elf_m88k_link_hash_table *ret;
360   bfd_size_type amt = sizeof (struct elf_m88k_link_hash_table);
361 
362   ret = (struct elf_m88k_link_hash_table *) bfd_malloc (amt);
363   if (ret == (struct elf_m88k_link_hash_table *) NULL)
364     return NULL;
365 
366   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
367 				      elf_m88k_link_hash_newfunc,
368 				      sizeof (struct elf_m88k_link_hash_entry)))
369     {
370       free (ret);
371       return NULL;
372     }
373 
374   ret->sym_sec.abfd = NULL;
375 
376   return &ret->root.root;
377 }
378 
379 /* Keep m88k-specific flags in the ELF header.  */
380 static bfd_boolean
381 elf32_m88k_set_private_flags (abfd, flags)
382      bfd *abfd;
383      flagword flags;
384 {
385   elf_elfheader (abfd)->e_flags = flags;
386   elf_flags_init (abfd) = TRUE;
387   return TRUE;
388 }
389 
390 /* Merge backend specific data from an object file to the output
391    object file when linking.  */
392 static bfd_boolean
393 elf32_m88k_merge_private_bfd_data (ibfd, obfd)
394      bfd *ibfd;
395      bfd *obfd;
396 {
397   flagword out_flags;
398   flagword in_flags;
399 
400   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
401       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
402     return TRUE;
403 
404   in_flags  = elf_elfheader (ibfd)->e_flags;
405   out_flags = elf_elfheader (obfd)->e_flags;
406 
407   if (!elf_flags_init (obfd))
408     {
409       elf_flags_init (obfd) = TRUE;
410       elf_elfheader (obfd)->e_flags = in_flags;
411     }
412 
413   return TRUE;
414 }
415 
416 /* Display the flags field.  */
417 static bfd_boolean
418 elf32_m88k_print_private_bfd_data (abfd, ptr)
419      bfd *abfd;
420      PTR ptr;
421 {
422   FILE *file = (FILE *) ptr;
423   flagword eflags;
424 
425   BFD_ASSERT (abfd != NULL && ptr != NULL);
426 
427   /* Print normal ELF private data.  */
428   _bfd_elf_print_private_bfd_data (abfd, ptr);
429 
430   eflags = elf_elfheader (abfd)->e_flags;
431 
432   /* xgettext:c-format */
433   fprintf (file, _("private flags = %lx:"), (long) eflags);
434 
435   if (eflags & EF_NABI)
436     fprintf (file, _(" [not ABI]"));
437 
438   if (eflags & EF_M88110)
439     fprintf (file, _(" [m88110]"));
440 
441   fputc ('\n', file);
442 
443   return TRUE;
444 }
445 
446 /* Look through the relocs for a section during the first phase, and
447    allocate space in the global offset table or procedure linkage
448    table.  */
449 
450 static bfd_boolean
451 elf_m88k_check_relocs (abfd, info, sec, relocs)
452      bfd *abfd;
453      struct bfd_link_info *info;
454      asection *sec;
455      const Elf_Internal_Rela *relocs;
456 {
457   bfd *dynobj;
458   Elf_Internal_Shdr *symtab_hdr;
459   struct elf_link_hash_entry **sym_hashes;
460   bfd_signed_vma *local_got_refcounts;
461   const Elf_Internal_Rela *rel;
462   const Elf_Internal_Rela *rel_end;
463   asection *sgot;
464   asection *srelgot;
465   asection *sreloc;
466 
467   if (info->relocatable)
468     return TRUE;
469 
470   dynobj = elf_hash_table (info)->dynobj;
471   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
472   sym_hashes = elf_sym_hashes (abfd);
473   local_got_refcounts = elf_local_got_refcounts (abfd);
474 
475   sgot = NULL;
476   srelgot = NULL;
477   sreloc = NULL;
478 
479   rel_end = relocs + sec->reloc_count;
480   for (rel = relocs; rel < rel_end; rel++)
481     {
482       unsigned long r_symndx;
483       struct elf_link_hash_entry *h;
484 
485       r_symndx = ELF32_R_SYM (rel->r_info);
486 
487       if (r_symndx < symtab_hdr->sh_info)
488 	h = NULL;
489       else
490 	{
491 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
492 	  while (h->root.type == bfd_link_hash_indirect
493 		 || h->root.type == bfd_link_hash_warning)
494 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
495 	}
496 
497       switch (ELF32_R_TYPE (rel->r_info))
498 	{
499 	case R_88K_GOT_16L:
500 	case R_88K_GOT_16H:
501 	  /* This symbol requires a global offset table entry.  */
502 
503 	  if (dynobj == NULL)
504 	    {
505 	      /* Create the .got section.  */
506 	      elf_hash_table (info)->dynobj = dynobj = abfd;
507 	      if (!_bfd_elf_create_got_section (dynobj, info))
508 		return FALSE;
509 	    }
510 
511 	  if (sgot == NULL)
512 	    {
513 	      sgot = bfd_get_section_by_name (dynobj, ".got");
514 	      BFD_ASSERT (sgot != NULL);
515 	    }
516 
517 	  if (srelgot == NULL
518 	      && (h != NULL || info->shared))
519 	    {
520 	      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
521 	      if (srelgot == NULL)
522 		{
523 		  srelgot = bfd_make_section_with_flags (dynobj,
524 							 ".rela.got",
525 							 (SEC_ALLOC
526 							  | SEC_LOAD
527 							  | SEC_HAS_CONTENTS
528 							  | SEC_IN_MEMORY
529 							  | SEC_LINKER_CREATED
530 							  | SEC_READONLY));
531 		  if (srelgot == NULL
532 		      || !bfd_set_section_alignment (dynobj, srelgot, 2))
533 		    return FALSE;
534 		}
535 	    }
536 
537 	  if (h != NULL)
538 	    {
539 	      if (h->got.refcount == 0)
540 		{
541 		  /* Make sure this symbol is output as a dynamic symbol.  */
542 		  if (h->dynindx == -1
543 		      && !h->forced_local)
544 		    {
545 		      if (!bfd_elf_link_record_dynamic_symbol (info, h))
546 			return FALSE;
547 		    }
548 
549 		  /* Allocate space in the .got section.  */
550 		  sgot->size += 4;
551 		  /* Allocate relocation space.  */
552 		  srelgot->size += sizeof (Elf32_External_Rela);
553 		}
554 	      h->got.refcount++;
555 	    }
556 	  else
557 	    {
558 	      /* This is a global offset table entry for a local symbol.  */
559 	      if (local_got_refcounts == NULL)
560 		{
561 		  bfd_size_type size;
562 
563 		  size = symtab_hdr->sh_info;
564 		  size *= sizeof (bfd_signed_vma);
565 		  local_got_refcounts = ((bfd_signed_vma *)
566 					 bfd_zalloc (abfd, size));
567 		  if (local_got_refcounts == NULL)
568 		    return FALSE;
569 		  elf_local_got_refcounts (abfd) = local_got_refcounts;
570 		}
571 	      if (local_got_refcounts[r_symndx] == 0)
572 		{
573 		  sgot->size += 4;
574 		  if (info->shared)
575 		    {
576 		      /* If we are generating a shared object, we need to
577 			 output a R_88K_BBASED reloc so that the dynamic
578 			 linker can adjust this GOT entry.  */
579 		      srelgot->size += sizeof (Elf32_External_Rela);
580 		    }
581 		}
582 	      local_got_refcounts[r_symndx]++;
583 	    }
584 	  break;
585 
586 	case R_88K_PLT_DISP26:
587 	  /* This symbol requires a procedure linkage table entry.  We
588 	     actually build the entry in adjust_dynamic_symbol,
589              because this might be a case of linking PIC code which is
590              never referenced by a dynamic object, in which case we
591              don't need to generate a procedure linkage table entry
592              after all.  */
593 
594 	  /* If this is a local symbol, we resolve it directly without
595 	     creating a procedure linkage table entry.  */
596 	  if (h == NULL)
597 	    continue;
598 
599 	  h->needs_plt = 1;
600 	  h->plt.refcount++;
601 	  break;
602 
603 	case R_88K_PLT_32:
604 	  /* This symbol requires a procedure linkage table entry.  */
605 
606 	  if (h == NULL)
607 	    {
608 	      /* It does not make sense to have this relocation for a
609 		 local symbol.  FIXME: does it?  How to handle it if
610 		 it does make sense?  */
611 	      bfd_set_error (bfd_error_bad_value);
612 	      return FALSE;
613 	    }
614 
615 	  /* Make sure this symbol is output as a dynamic symbol.  */
616 	  if (h->dynindx == -1
617 	      && !h->forced_local)
618 	    {
619 	      if (!bfd_elf_link_record_dynamic_symbol (info, h))
620 		return FALSE;
621 	    }
622 
623 	  h->needs_plt = 1;
624 	  h->plt.refcount++;
625 	  break;
626 
627 	case R_88K_DISP16:
628 	case R_88K_DISP26:
629 	  /* If we are creating a shared library and this is not a local
630 	     symbol, we need to copy the reloc into the shared library.
631 	     However when linking with -Bsymbolic and this is a global
632 	     symbol which is defined in an object we are including in the
633 	     link (i.e., DEF_REGULAR is set), then we can resolve the
634 	     reloc directly.  At this point we have not seen all the input
635 	     files, so it is possible that DEF_REGULAR is not set now but
636 	     will be set later (it is never cleared).  We account for that
637 	     possibility below by storing information in the
638 	     pcrel_relocs_copied field of the hash table entry.  */
639 	  if (!(info->shared
640 		&& (sec->flags & SEC_ALLOC) != 0
641 		&& h != NULL
642 		&& (!info->symbolic
643 		    || h->root.type == bfd_link_hash_defweak
644 		    || !h->def_regular)))
645 	    {
646 	      if (h != NULL)
647 		{
648 		  /* Make sure a plt entry is created for this symbol if
649 		     it turns out to be a function defined by a dynamic
650 		     object.  */
651 		  h->plt.refcount++;
652 		}
653 	      break;
654 	    }
655 	  /* Fall through.  */
656 	case R_88K_32:
657 	case R_88K_16L:
658 	case R_88K_16H:
659 	  if (h != NULL)
660 	    {
661 	      /* Make sure a plt entry is created for this symbol if it
662 		 turns out to be a function defined by a dynamic object.  */
663 	      h->plt.refcount++;
664 	    }
665 
666 	  /* If we are creating a shared library, we need to copy the
667 	     reloc into the shared library.  */
668 	  if (info->shared
669 	      && (sec->flags & SEC_ALLOC) != 0)
670 	    {
671 	      /* When creating a shared object, we must copy these
672 		 reloc types into the output file.  We create a reloc
673 		 section in dynobj and make room for this reloc.  */
674 	      if (sreloc == NULL)
675 		{
676 		  const char *name;
677 
678 		  name = (bfd_elf_string_from_elf_section
679 			  (abfd,
680 			   elf_elfheader (abfd)->e_shstrndx,
681 			   elf_section_data (sec)->rel_hdr.sh_name));
682 		  if (name == NULL)
683 		    return FALSE;
684 
685 		  BFD_ASSERT (strncmp (name, ".rela", 5) == 0
686 			      && strcmp (bfd_get_section_name (abfd, sec),
687 					 name + 5) == 0);
688 
689 		  sreloc = bfd_get_section_by_name (dynobj, name);
690 		  if (sreloc == NULL)
691 		    {
692 		      sreloc = bfd_make_section_with_flags (dynobj,
693 							    name,
694 							    (SEC_ALLOC
695 							     | SEC_LOAD
696 							     | SEC_HAS_CONTENTS
697 							     | SEC_IN_MEMORY
698 							     | SEC_LINKER_CREATED
699 							     | SEC_READONLY));
700 		      if (sreloc == NULL
701 			  || !bfd_set_section_alignment (dynobj, sreloc, 2))
702 			return FALSE;
703 		    }
704 		  elf_section_data (sec)->sreloc = sreloc;
705 		}
706 
707 	      if (sec->flags & SEC_READONLY
708 		  /* Don't set DF_TEXTREL yet for PC relative
709 		     relocations, they might be discarded later.  */
710 		  && !(ELF32_R_TYPE (rel->r_info) == R_88K_DISP16
711 		       || ELF32_R_TYPE (rel->r_info) == R_88K_DISP26))
712 		    info->flags |= DF_TEXTREL;
713 
714 	      sreloc->size += sizeof (Elf32_External_Rela);
715 
716 	      /* We count the number of PC relative relocations we have
717 		 entered for this symbol, so that we can discard them
718 		 again if, in the -Bsymbolic case, the symbol is later
719 		 defined by a regular object, or, in the normal shared
720 		 case, the symbol is forced to be local.  Note that this
721 		 function is only called if we are using an m88kelf linker
722 		 hash table, which means that h is really a pointer to an
723 		 elf_m88k_link_hash_entry.  */
724 	      if (ELF32_R_TYPE (rel->r_info) == R_88K_DISP16
725 		  || ELF32_R_TYPE (rel->r_info) == R_88K_DISP26)
726 		{
727 		  struct elf_m88k_pcrel_relocs_copied *p;
728 		  struct elf_m88k_pcrel_relocs_copied **head;
729 
730 		  if (h != NULL)
731 		    {
732 		      struct elf_m88k_link_hash_entry *eh
733 			= elf_m88k_hash_entry (h);
734 		      head = &eh->pcrel_relocs_copied;
735 		    }
736 		  else
737 		    {
738 		      asection *s;
739 		      void *vpp;
740 
741 		      s = (bfd_section_from_r_symndx
742 			   (abfd, &elf_m88k_hash_table (info)->sym_sec,
743 			    sec, r_symndx));
744 		      if (s == NULL)
745 			return FALSE;
746 
747 		      vpp = &elf_section_data (s)->local_dynrel;
748 		      head = (struct elf_m88k_pcrel_relocs_copied **) vpp;
749 		    }
750 
751 		  for (p = *head; p != NULL; p = p->next)
752 		    if (p->section == sreloc)
753 		      break;
754 
755 		  if (p == NULL)
756 		    {
757 		      p = ((struct elf_m88k_pcrel_relocs_copied *)
758 			   bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
759 		      if (p == NULL)
760 			return FALSE;
761 		      p->next = *head;
762 		      *head = p;
763 		      p->section = sreloc;
764 		      p->count = 0;
765 		    }
766 
767 		  ++p->count;
768 		}
769 	    }
770 
771 	  break;
772 
773 	  /* This relocation describes the C++ object vtable hierarchy.
774 	     Reconstruct it for later use during GC.  */
775 	case R_88K_GNU_VTINHERIT:
776 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
777 	    return FALSE;
778 	  break;
779 
780 	  /* This relocation describes which C++ vtable entries are actually
781 	     used.  Record for later use during GC.  */
782 	case R_88K_GNU_VTENTRY:
783 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
784 	    return FALSE;
785 	  break;
786 
787 	default:
788 	  break;
789 	}
790     }
791 
792   return TRUE;
793 }
794 
795 /* Return the section that should be marked against GC for a given
796    relocation.  */
797 
798 static asection *
799 elf_m88k_gc_mark_hook (sec, info, rel, h, sym)
800      asection *sec;
801      struct bfd_link_info *info ATTRIBUTE_UNUSED;
802      Elf_Internal_Rela *rel;
803      struct elf_link_hash_entry *h;
804      Elf_Internal_Sym *sym;
805 {
806   if (h != NULL)
807     {
808       switch (ELF32_R_TYPE (rel->r_info))
809 	{
810 	case R_88K_GNU_VTINHERIT:
811 	case R_88K_GNU_VTENTRY:
812 	  break;
813 
814 	default:
815 	  switch (h->root.type)
816 	    {
817 	    default:
818 	      break;
819 
820 	    case bfd_link_hash_defined:
821 	    case bfd_link_hash_defweak:
822 	      return h->root.u.def.section;
823 
824 	    case bfd_link_hash_common:
825 	      return h->root.u.c.p->section;
826 	    }
827 	}
828     }
829   else
830     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
831 
832   return NULL;
833 }
834 
835 /* Update the got entry reference counts for the section being removed.  */
836 
837 static bfd_boolean
838 elf_m88k_gc_sweep_hook (abfd, info, sec, relocs)
839      bfd *abfd;
840      struct bfd_link_info *info;
841      asection *sec;
842      const Elf_Internal_Rela *relocs;
843 {
844   Elf_Internal_Shdr *symtab_hdr;
845   struct elf_link_hash_entry **sym_hashes;
846   bfd_signed_vma *local_got_refcounts;
847   const Elf_Internal_Rela *rel, *relend;
848   bfd *dynobj;
849   asection *sgot;
850   asection *srelgot;
851 
852   dynobj = elf_hash_table (info)->dynobj;
853   if (dynobj == NULL)
854     return TRUE;
855 
856   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
857   sym_hashes = elf_sym_hashes (abfd);
858   local_got_refcounts = elf_local_got_refcounts (abfd);
859 
860   sgot = bfd_get_section_by_name (dynobj, ".got");
861   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
862 
863   relend = relocs + sec->reloc_count;
864   for (rel = relocs; rel < relend; rel++)
865     {
866       unsigned long r_symndx;
867       struct elf_link_hash_entry *h = NULL;
868 
869       r_symndx = ELF32_R_SYM (rel->r_info);
870       if (r_symndx >= symtab_hdr->sh_info)
871 	{
872 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
873 	  while (h->root.type == bfd_link_hash_indirect
874 		 || h->root.type == bfd_link_hash_warning)
875 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
876 	}
877 
878       switch (ELF32_R_TYPE (rel->r_info))
879 	{
880 	case R_88K_GOT_16L:
881 	case R_88K_GOT_16H:
882 	  if (h != NULL)
883 	    {
884 	      if (h->got.refcount > 0)
885 		{
886 		  --h->got.refcount;
887 		  if (h->got.refcount == 0)
888 		    {
889 		      /* We don't need the .got entry any more.  */
890 		      sgot->size -= 4;
891 		      srelgot->size -= sizeof (Elf32_External_Rela);
892 		    }
893 		}
894 	    }
895 	  else if (local_got_refcounts != NULL)
896 	    {
897 	      if (local_got_refcounts[r_symndx] > 0)
898 		{
899 		  --local_got_refcounts[r_symndx];
900 		  if (local_got_refcounts[r_symndx] == 0)
901 		    {
902 		      /* We don't need the .got entry any more.  */
903 		      sgot->size -= 4;
904 		      if (info->shared)
905 			srelgot->size -= sizeof (Elf32_External_Rela);
906 		    }
907 		}
908 	    }
909 	  break;
910 
911 	case R_88K_PLT_DISP26:
912 	case R_88K_PLT_32:
913 	case R_88K_DISP16:
914 	case R_88K_DISP26:
915 	case R_88K_32:
916 	case R_88K_16L:
917 	case R_88K_16H:
918 	  if (h != NULL)
919 	    {
920 	      if (h->plt.refcount > 0)
921 		--h->plt.refcount;
922 	    }
923 	  break;
924 
925 	default:
926 	  break;
927 	}
928     }
929 
930   return TRUE;
931 }
932 
933 /* Adjust a symbol defined by a dynamic object and referenced by a
934    regular object.  The current definition is in some section of the
935    dynamic object, but we're not including those sections.  We have to
936    change the definition to something the rest of the link can
937    understand.  */
938 
939 static bfd_boolean
940 elf_m88k_adjust_dynamic_symbol (info, h)
941      struct bfd_link_info *info;
942      struct elf_link_hash_entry *h;
943 {
944   bfd *dynobj;
945   asection *s;
946   unsigned int power_of_two;
947 
948   dynobj = elf_hash_table (info)->dynobj;
949 
950   /* Make sure we know what is going on here.  */
951   BFD_ASSERT (dynobj != NULL
952 	      && (h->needs_plt
953 		  || h->u.weakdef != NULL
954 		  || (h->def_dynamic
955 		      && h->ref_regular
956 		      && !h->def_regular)));
957 
958   /* If this is a function, put it in the procedure linkage table.  We
959      will fill in the contents of the procedure linkage table later,
960      when we know the address of the .got section.  */
961   if (h->type == STT_FUNC
962       || h->needs_plt)
963     {
964       if ((h->plt.refcount <= 0
965 	   || SYMBOL_CALLS_LOCAL (info, h)
966 	   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
967 	       && h->root.type == bfd_link_hash_undefweak))
968 	  /* We must always create the plt entry if it was referenced
969 	     by a PLTxxO relocation.  In this case we already recorded
970 	     it as a dynamic symbol.  */
971 	  && h->dynindx == -1)
972 	{
973 	  /* This case can occur if we saw a PLTxx reloc in an input
974 	     file, but the symbol was never referred to by a dynamic
975 	     object, or if all references were garbage collected.  In
976 	     such a case, we don't actually need to build a procedure
977 	     linkage table, and we can just do a PCxx reloc instead.  */
978 	  BFD_ASSERT (h->needs_plt);
979 	  h->plt.offset = (bfd_vma) -1;
980 	  h->needs_plt = 0;
981 	  return TRUE;
982 	}
983 
984       /* Make sure this symbol is output as a dynamic symbol.  */
985       if (h->dynindx == -1
986 	  && !h->forced_local)
987 	{
988 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
989 	    return FALSE;
990 	}
991 
992       s = bfd_get_section_by_name (dynobj, ".plt");
993       BFD_ASSERT (s != NULL);
994 
995       /* If this is the first .plt entry, make room for the special
996 	 first entry.  */
997       if (s->size == 0)
998 	{
999 	  s->size += PLT_ENTRY0_SIZE;
1000 	}
1001 
1002       /* If this symbol is not defined in a regular file, and we are
1003 	 not generating a shared library, then set the symbol to this
1004 	 location in the .plt.  This is required to make function
1005 	 pointers compare as equal between the normal executable and
1006 	 the shared library.  */
1007       if (!info->shared
1008 	  && !h->def_regular)
1009 	{
1010 	  h->root.u.def.section = s;
1011 	  h->root.u.def.value = s->size;
1012 	}
1013 
1014       h->plt.offset = s->size;
1015 
1016       /* Make room for this entry.  */
1017       s->size += PLT_ENTRY_SIZE;
1018 
1019       /* We also need to make an entry in the .got.plt section, which
1020 	 will be placed in the .got section by the linker script.  */
1021       s = bfd_get_section_by_name (dynobj, ".got.plt");
1022       BFD_ASSERT (s != NULL);
1023       s->size += 4;
1024 
1025       /* We also need to make an entry in the .rela.plt section.  */
1026       s = bfd_get_section_by_name (dynobj, ".rela.plt");
1027       BFD_ASSERT (s != NULL);
1028       s->size += sizeof (Elf32_External_Rela);
1029 
1030       return TRUE;
1031     }
1032 
1033   /* Reinitialize the plt offset now that it is not used as a reference
1034      count any more.  */
1035   h->plt.offset = (bfd_vma) -1;
1036 
1037   /* If this is a weak symbol, and there is a real definition, the
1038      processor independent code will have arranged for us to see the
1039      real definition first, and we can just use the same value.  */
1040   if (h->u.weakdef != NULL)
1041     {
1042       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1043 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
1044       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1045       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1046       return TRUE;
1047     }
1048 
1049   /* This is a reference to a symbol defined by a dynamic object which
1050      is not a function.  */
1051 
1052   /* If we are creating a shared library, we must presume that the
1053      only references to the symbol are via the global offset table.
1054      For such cases we need not do anything here; the relocations will
1055      be handled correctly by relocate_section.  */
1056   if (info->shared)
1057     return TRUE;
1058 
1059   if (h->size == 0)
1060     {
1061       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1062 			    h->root.root.string);
1063       return TRUE;
1064     }
1065 
1066   /* We must allocate the symbol in our .dynbss section, which will
1067      become part of the .bss section of the executable.  There will be
1068      an entry for this symbol in the .dynsym section.  The dynamic
1069      object will contain position independent code, so all references
1070      from the dynamic object to this symbol will go through the global
1071      offset table.  The dynamic linker will use the .dynsym entry to
1072      determine the address it must put in the global offset table, so
1073      both the dynamic object and the regular object will refer to the
1074      same memory location for the variable.  */
1075 
1076   s = bfd_get_section_by_name (dynobj, ".dynbss");
1077   BFD_ASSERT (s != NULL);
1078 
1079   /* We must generate a R_88K_COPY reloc to tell the dynamic linker to
1080      copy the initial value out of the dynamic object and into the
1081      runtime process image.  We need to remember the offset into the
1082      .rela.bss section we are going to use.  */
1083   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1084     {
1085       asection *srel;
1086 
1087       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1088       BFD_ASSERT (srel != NULL);
1089       srel->size += sizeof (Elf32_External_Rela);
1090       h->needs_copy = 1;
1091     }
1092 
1093   /* We need to figure out the alignment required for this symbol.  I
1094      have no idea how other ELF linkers handle this.  */
1095   power_of_two = bfd_log2 (h->size);
1096   if (power_of_two > 3)
1097     power_of_two = 3;
1098 
1099   /* Apply the required alignment.  */
1100   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1101   if (power_of_two > bfd_get_section_alignment (dynobj, s))
1102     {
1103       if (!bfd_set_section_alignment (dynobj, s, power_of_two))
1104 	return FALSE;
1105     }
1106 
1107   /* Define the symbol as being at this point in the section.  */
1108   h->root.u.def.section = s;
1109   h->root.u.def.value = s->size;
1110 
1111   /* Increment the section size to make room for the symbol.  */
1112   s->size += h->size;
1113 
1114   return TRUE;
1115 }
1116 
1117 /* Set the sizes of the dynamic sections.  */
1118 
1119 static bfd_boolean
1120 elf_m88k_size_dynamic_sections (output_bfd, info)
1121      bfd *output_bfd ATTRIBUTE_UNUSED;
1122      struct bfd_link_info *info;
1123 {
1124   bfd *dynobj;
1125   asection *s;
1126   bfd_boolean plt;
1127   bfd_boolean relocs;
1128 
1129   dynobj = elf_hash_table (info)->dynobj;
1130   BFD_ASSERT (dynobj != NULL);
1131 
1132   if (elf_hash_table (info)->dynamic_sections_created)
1133     {
1134       /* Set the contents of the .interp section to the interpreter.  */
1135       if (info->executable && !info->static_link)
1136 	{
1137 	  s = bfd_get_section_by_name (dynobj, ".interp");
1138 	  BFD_ASSERT (s != NULL);
1139 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1140 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1141 	}
1142     }
1143   else
1144     {
1145       /* We may have created entries in the .rela.got section.
1146 	 However, if we are not creating the dynamic sections, we will
1147 	 not actually use these entries.  Reset the size of .rela.got,
1148 	 which will cause it to get stripped from the output file
1149 	 below.  */
1150       s = bfd_get_section_by_name (dynobj, ".rela.got");
1151       if (s != NULL)
1152 	s->size = 0;
1153     }
1154 
1155   /* If this is a -Bsymbolic shared link, then we need to discard all
1156      PC relative relocs against symbols defined in a regular object.
1157      For the normal shared case we discard the PC relative relocs
1158      against symbols that have become local due to visibility changes.
1159      We allocated space for them in the check_relocs routine, but we
1160      will not fill them in in the relocate_section routine.  */
1161   if (info->shared)
1162     elf_link_hash_traverse (elf_hash_table (info),
1163 			    elf_m88k_discard_copies,
1164 			    (PTR) info);
1165 
1166   /* The check_relocs and adjust_dynamic_symbol entry points have
1167      determined the sizes of the various dynamic sections.  Allocate
1168      memory for them.  */
1169   plt = FALSE;
1170   relocs = FALSE;
1171   for (s = dynobj->sections; s != NULL; s = s->next)
1172     {
1173       const char *name;
1174 
1175       if ((s->flags & SEC_LINKER_CREATED) == 0)
1176 	continue;
1177 
1178       /* It's OK to base decisions on the section name, because none
1179 	 of the dynobj section names depend upon the input files.  */
1180       name = bfd_get_section_name (dynobj, s);
1181 
1182       if (strcmp (name, ".plt") == 0)
1183 	{
1184 	  /* Remember whether there is a PLT.  */
1185 	  plt = s->size != 0;
1186 	}
1187       else if (strncmp (name, ".rela", 5) == 0)
1188 	{
1189 	  if (s->size != 0)
1190 	    {
1191 	      relocs = TRUE;
1192 
1193 	      /* We use the reloc_count field as a counter if we need
1194 		 to copy relocs into the output file.  */
1195 	      s->reloc_count = 0;
1196 	    }
1197 	}
1198       else if (strncmp (name, ".got", 4) != 0
1199 	       && strcmp (name, ".dynbss") != 0)
1200 	{
1201 	  /* It's not one of our sections, so don't allocate space.  */
1202 	  continue;
1203 	}
1204 
1205       if (s->size == 0)
1206 	{
1207 	  /* If we don't need this section, strip it from the
1208 	     output file.  This is mostly to handle .rela.bss and
1209 	     .rela.plt.  We must create both sections in
1210 	     create_dynamic_sections, because they must be created
1211 	     before the linker maps input sections to output
1212 	     sections.  The linker does that before
1213 	     adjust_dynamic_symbol is called, and it is that
1214 	     function which decides whether anything needs to go
1215 	     into these sections.  */
1216 	  s->flags |= SEC_EXCLUDE;
1217 	  continue;
1218 	}
1219 
1220       if ((s->flags & SEC_HAS_CONTENTS) == 0)
1221 	continue;
1222 
1223       /* Allocate memory for the section contents.  */
1224       /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1225 	 Unused entries should be reclaimed before the section's contents
1226 	 are written out, but at the moment this does not happen.  Thus in
1227 	 order to prevent writing out garbage, we initialise the section's
1228 	 contents to zero.  */
1229       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1230       if (s->contents == NULL)
1231 	return FALSE;
1232     }
1233 
1234   if (elf_hash_table (info)->dynamic_sections_created)
1235     {
1236       /* Add some entries to the .dynamic section.  We fill in the
1237 	 values later, in elf_m88k_finish_dynamic_sections, but we
1238 	 must add the entries now so that we get the correct size for
1239 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
1240 	 dynamic linker and used by the debugger.  */
1241 #define add_dynamic_entry(TAG, VAL) \
1242   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1243 
1244       if (!info->shared)
1245 	{
1246 	  if (!add_dynamic_entry (DT_DEBUG, 0))
1247 	    return FALSE;
1248 	}
1249 
1250       if (plt)
1251 	{
1252 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
1253 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
1254 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1255 	      || !add_dynamic_entry (DT_JMPREL, 0)
1256 	      || !add_dynamic_entry (DT_88K_PLTSTART, 0)
1257 	      || !add_dynamic_entry (DT_88K_PLTEND, 0))
1258 	    return FALSE;
1259 	}
1260 
1261       if (relocs)
1262 	{
1263 	  if (!add_dynamic_entry (DT_RELA, 0)
1264 	      || !add_dynamic_entry (DT_RELASZ, 0)
1265 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1266 	    return FALSE;
1267 	}
1268 
1269       if ((info->flags & DF_TEXTREL) != 0)
1270 	{
1271 	  if (!add_dynamic_entry (DT_TEXTREL, 0))
1272 	    return FALSE;
1273 	}
1274     }
1275 #undef add_dynamic_entry
1276 
1277   return TRUE;
1278 }
1279 
1280 /* This function is called via elf_link_hash_traverse if we are
1281    creating a shared object.  In the -Bsymbolic case it discards the
1282    space allocated to copy PC relative relocs against symbols which
1283    are defined in regular objects.  For the normal shared case, it
1284    discards space for pc-relative relocs that have become local due to
1285    symbol visibility changes.  We allocated space for them in the
1286    check_relocs routine, but we won't fill them in in the
1287    relocate_section routine.
1288 
1289    We also check whether any of the remaining relocations apply
1290    against a readonly section, and set the DF_TEXTREL flag in this
1291    case.  */
1292 
1293 static bfd_boolean
1294 elf_m88k_discard_copies (h, inf)
1295      struct elf_link_hash_entry *h;
1296      PTR inf;
1297 {
1298   struct bfd_link_info *info = (struct bfd_link_info *) inf;
1299   struct elf_m88k_pcrel_relocs_copied *s;
1300 
1301   if (h->root.type == bfd_link_hash_warning)
1302     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1303 
1304   if (!h->def_regular
1305       || (!info->symbolic
1306 	  && !h->forced_local))
1307     {
1308       if ((info->flags & DF_TEXTREL) == 0)
1309 	{
1310 	  /* Look for relocations against read-only sections.  */
1311 	  for (s = elf_m88k_hash_entry (h)->pcrel_relocs_copied;
1312 	       s != NULL;
1313 	       s = s->next)
1314 	    if ((s->section->flags & SEC_READONLY) != 0)
1315 	      {
1316 		info->flags |= DF_TEXTREL;
1317 		break;
1318 	      }
1319 	}
1320 
1321       return TRUE;
1322     }
1323 
1324   for (s = elf_m88k_hash_entry (h)->pcrel_relocs_copied;
1325        s != NULL;
1326        s = s->next)
1327     s->section->size -= s->count * sizeof (Elf32_External_Rela);
1328 
1329   return TRUE;
1330 }
1331 
1332 /* Relocate an M88K ELF section.  */
1333 
1334 static bfd_boolean
1335 elf_m88k_relocate_section (output_bfd, info, input_bfd, input_section,
1336 			   contents, relocs, local_syms, local_sections)
1337      bfd *output_bfd;
1338      struct bfd_link_info *info;
1339      bfd *input_bfd;
1340      asection *input_section;
1341      bfd_byte *contents;
1342      Elf_Internal_Rela *relocs;
1343      Elf_Internal_Sym *local_syms;
1344      asection **local_sections;
1345 {
1346   bfd *dynobj;
1347   Elf_Internal_Shdr *symtab_hdr;
1348   struct elf_link_hash_entry **sym_hashes;
1349   bfd_vma *local_got_offsets;
1350   asection *sgot;
1351   asection *splt;
1352   asection *sreloc;
1353   Elf_Internal_Rela *rel;
1354   Elf_Internal_Rela *relend;
1355 
1356   if (info->relocatable)
1357     return TRUE;
1358 
1359   dynobj = elf_hash_table (info)->dynobj;
1360   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1361   sym_hashes = elf_sym_hashes (input_bfd);
1362   local_got_offsets = elf_local_got_offsets (input_bfd);
1363 
1364   sgot = NULL;
1365   splt = NULL;
1366   sreloc = NULL;
1367 
1368   rel = relocs;
1369   relend = relocs + input_section->reloc_count;
1370   for (; rel < relend; rel++)
1371     {
1372       int r_type;
1373       const char *sym_name;
1374       reloc_howto_type *howto;
1375       unsigned long r_symndx;
1376       struct elf_link_hash_entry *h;
1377       Elf_Internal_Sym *sym;
1378       asection *sec;
1379       bfd_vma relocation;
1380       bfd_boolean unresolved_reloc;
1381       bfd_reloc_status_type r;
1382 
1383       r_type = ELF32_R_TYPE (rel->r_info);
1384       if (r_type < 0 || r_type >= (int) R_88K_UNIMPLEMENTED)
1385 	{
1386 	  bfd_set_error (bfd_error_bad_value);
1387 	  return FALSE;
1388 	}
1389       howto = howto_table + r_type;
1390 
1391       r_symndx = ELF32_R_SYM (rel->r_info);
1392 
1393       h = NULL;
1394       sym = NULL;
1395       sec = NULL;
1396       unresolved_reloc = FALSE;
1397 
1398       if (r_symndx < symtab_hdr->sh_info)
1399 	{
1400 	  sym = local_syms + r_symndx;
1401 	  sec = local_sections[r_symndx];
1402 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1403 	}
1404       else
1405 	{
1406 	  bfd_boolean warned;
1407 
1408 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1409 				   r_symndx, symtab_hdr, sym_hashes,
1410 				   h, sec, relocation,
1411 				   unresolved_reloc, warned);
1412 	}
1413 
1414       if (h != NULL)
1415 	{
1416 	  sym_name = h->root.root.string;
1417 	}
1418       else
1419 	{
1420 	  sym_name = bfd_elf_string_from_elf_section (input_bfd,
1421 						      symtab_hdr->sh_link,
1422 						      sym->st_name);
1423 	  if (sym_name != NULL && *sym_name == '\0')
1424 	    sym_name = bfd_section_name (input_bfd, sec);
1425 	}
1426 
1427       switch (r_type)
1428 	{
1429 	case R_88K_GOT_16L:
1430 	case R_88K_GOT_16H:
1431 	  /* Relocation is the offset of the entry for this symbol in
1432 	     the global offset table.  */
1433 	  {
1434 	    bfd_vma off;
1435 
1436 	    if (sgot == NULL)
1437 	      {
1438 		sgot = bfd_get_section_by_name (dynobj, ".got");
1439 		BFD_ASSERT (sgot != NULL);
1440 	      }
1441 
1442 	    if (h != NULL)
1443 	      {
1444 		bfd_boolean dyn;
1445 
1446 		off = h->got.offset;
1447 		BFD_ASSERT (off != (bfd_vma) -1);
1448 
1449 		dyn = elf_hash_table (info)->dynamic_sections_created;
1450 		if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1451 		    || (info->shared
1452 			&& (info->symbolic
1453 			    || h->dynindx == -1
1454 			    || h->forced_local)
1455 			&& h->def_regular))
1456 		  {
1457 		    /* This is actually a static link, or it is a
1458 		       -Bsymbolic link and the symbol is defined
1459 		       locally, or the symbol was forced to be local
1460 		       because of a version file..  We must initialize
1461 		       this entry in the global offset table.  Since
1462 		       the offset must always be a multiple of 4, we
1463 		       use the least significant bit to record whether
1464 		       we have initialized it already.
1465 
1466 		       When doing a dynamic link, we create a .rela.got
1467 		       relocation entry to initialize the value.  This
1468 		       is done in the finish_dynamic_symbol routine.  */
1469 		    if ((off & 1) != 0)
1470 		      off &= ~1;
1471 		    else
1472 		      {
1473 			bfd_put_32 (output_bfd, relocation,
1474 				    sgot->contents + off);
1475 			h->got.offset |= 1;
1476 		      }
1477 		  }
1478 		else
1479 		  unresolved_reloc = FALSE;
1480 	      }
1481 	    else
1482 	      {
1483 		BFD_ASSERT (local_got_offsets != NULL
1484 			    && local_got_offsets[r_symndx] != (bfd_vma) -1);
1485 
1486 		off = local_got_offsets[r_symndx];
1487 
1488 		/* The offset must always be a multiple of 4.  We use
1489 		   the least significant bit to record whether we have
1490 		   already generated the necessary reloc.  */
1491 		if ((off & 1) != 0)
1492 		  off &= ~1;
1493 		else
1494 		  {
1495 		    bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1496 
1497 		    if (info->shared)
1498 		      {
1499 			asection *s;
1500 			Elf_Internal_Rela outrel;
1501 			bfd_byte *loc;
1502 
1503 			s = bfd_get_section_by_name (dynobj, ".rela.got");
1504 			BFD_ASSERT (s != NULL);
1505 
1506 			outrel.r_offset = (sgot->output_section->vma
1507 					   + sgot->output_offset
1508 					   + off);
1509 			outrel.r_info = ELF32_R_INFO (0, R_88K_BBASED_32);
1510 			outrel.r_addend = relocation;
1511 			loc = s->contents;
1512 			loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1513 			bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1514 		      }
1515 
1516 		    local_got_offsets[r_symndx] |= 1;
1517 		  }
1518 	      }
1519 
1520 	    /* Addends on got relocations don't make much sense.
1521 	       x+off@got is actually x@got+off, and since the got is
1522 	       generated by a hash table traversal, the value in the
1523 	       got at entry m+n bears little relation to the entry m.  */
1524 	    if (rel->r_addend != 0)
1525 	      (*_bfd_error_handler)
1526 		(_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
1527 		 input_bfd, input_section, (long) rel->r_offset, howto->name,
1528 		 sym_name);
1529 
1530 	    relocation = sgot->output_offset + off;
1531 	  }
1532 	  break;
1533 
1534 	case R_88K_ABDIFF_16L:
1535 	case R_88K_ABDIFF_16H:
1536 	  /* Relocation is the offset from this symbol to the address of
1537 	     the global offset table.  */
1538 
1539 	  if (sgot == NULL)
1540 	    {
1541 	      sgot = bfd_get_section_by_name (dynobj, ".got");
1542 	      BFD_ASSERT (sgot != NULL);
1543 	    }
1544 
1545 	  relocation = sgot->output_section->vma
1546 		       - (input_section->output_section->vma
1547 			  + input_section->output_offset + rel->r_addend);
1548 	  rel->r_addend = 0;
1549 	  unresolved_reloc = FALSE;
1550 	  break;
1551 
1552 	case R_88K_PLT_DISP26:
1553 	  /* Relocation is to the entry for this symbol in the
1554 	     procedure linkage table.  */
1555 
1556 	  /* Resolve a PLTxx reloc against a local symbol directly,
1557 	     without using the procedure linkage table.  */
1558 	  if (h == NULL)
1559 	    break;
1560 
1561 	  if (h->plt.offset == (bfd_vma) -1
1562 	      || !elf_hash_table (info)->dynamic_sections_created)
1563 	    {
1564 	      /* We didn't make a PLT entry for this symbol.  This
1565 		 happens when statically linking PIC code, or when
1566 		 using -Bsymbolic.  */
1567 	      break;
1568 	    }
1569 
1570 	  if (splt == NULL)
1571 	    {
1572 	      splt = bfd_get_section_by_name (dynobj, ".plt");
1573 	      BFD_ASSERT (splt != NULL);
1574 	    }
1575 
1576 	  relocation = (splt->output_section->vma
1577 			+ splt->output_offset
1578 			+ h->plt.offset);
1579 	  unresolved_reloc = FALSE;
1580 	  break;
1581 
1582 	case R_88K_PLT_32:
1583 	  /* Relocation is the offset of the entry for this symbol in
1584 	     the procedure linkage table.  */
1585 	  BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1586 
1587 	  if (splt == NULL)
1588 	    {
1589 	      splt = bfd_get_section_by_name (dynobj, ".plt");
1590 	      BFD_ASSERT (splt != NULL);
1591 	    }
1592 
1593 	  relocation = h->plt.offset;
1594 	  unresolved_reloc = FALSE;
1595 
1596 	  /* This relocation does not use the addend.  */
1597 	  rel->r_addend = 0;
1598 
1599 	  break;
1600 
1601 	case R_88K_DISP16:
1602 	case R_88K_DISP26:
1603 	  if (h == NULL
1604 	      || (info->shared
1605 		  && h->forced_local))
1606 	    break;
1607 	  /* Fall through.  */
1608 	case R_88K_8:
1609 	case R_88K_16:
1610 	case R_88K_32:
1611 	case R_88K_16L:
1612 	case R_88K_16H:
1613 	  if (info->shared
1614 	      && r_symndx != 0
1615 	      && (input_section->flags & SEC_ALLOC) != 0
1616 	      && (h == NULL
1617 		  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1618 		  || h->root.type != bfd_link_hash_undefweak)
1619 	      && ((r_type != R_88K_DISP16
1620 		   && r_type != R_88K_DISP26)
1621 		  || (h != NULL
1622 		      && h->dynindx != -1
1623 		      && (!info->symbolic
1624 			  || !h->def_regular))))
1625 	    {
1626 	      Elf_Internal_Rela outrel;
1627 	      bfd_byte *loc;
1628 	      bfd_boolean skip, relocate;
1629 
1630 	      /* When generating a shared object, these relocations
1631 		 are copied into the output file to be resolved at run
1632 		 time.  */
1633 
1634 	      skip = FALSE;
1635 	      relocate = FALSE;
1636 
1637 	      outrel.r_offset =
1638 		_bfd_elf_section_offset (output_bfd, info, input_section,
1639 					 rel->r_offset);
1640 	      if (outrel.r_offset == (bfd_vma) -1)
1641 		skip = TRUE;
1642 	      else if (outrel.r_offset == (bfd_vma) -2)
1643 		skip = TRUE, relocate = TRUE;
1644 	      outrel.r_offset += (input_section->output_section->vma
1645 				  + input_section->output_offset);
1646 
1647 	      if (skip)
1648 		memset (&outrel, 0, sizeof outrel);
1649 	      else if (h != NULL
1650 		       && h->dynindx != -1
1651 		       && (r_type == R_88K_DISP16
1652 			   || r_type == R_88K_DISP26
1653 			   || !info->shared
1654 			   || !info->symbolic
1655 			   || !h->def_regular))
1656 		{
1657 		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1658 		  outrel.r_addend = rel->r_addend;
1659 		}
1660 	      else
1661 		{
1662 		  /* This symbol is local, or marked to become local.  */
1663 		  if (r_type == R_88K_32)
1664 		    {
1665 		      relocate = TRUE;
1666 		      outrel.r_info = ELF32_R_INFO (0, R_88K_BBASED_32);
1667 		      outrel.r_addend = relocation + rel->r_addend;
1668 		    }
1669 		  else
1670 		    {
1671 		      long indx;
1672 
1673 		      if (bfd_is_abs_section (sec))
1674 			indx = 0;
1675 		      else if (sec == NULL || sec->owner == NULL)
1676 			{
1677 			  bfd_set_error (bfd_error_bad_value);
1678 			  return FALSE;
1679 			}
1680 		      else
1681 			{
1682 			  asection *osec;
1683 
1684 			  osec = sec->output_section;
1685 			  indx = elf_section_data (osec)->dynindx;
1686 			  BFD_ASSERT (indx > 0);
1687 			}
1688 
1689 		      outrel.r_info = ELF32_R_INFO (indx, r_type);
1690 		      outrel.r_addend = relocation + rel->r_addend;
1691 		    }
1692 		}
1693 
1694 	      sreloc = elf_section_data (input_section)->sreloc;
1695 	      if (sreloc == NULL)
1696 		abort ();
1697 
1698 	      loc = sreloc->contents;
1699 	      loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1700 	      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1701 
1702 	      /* This reloc will be computed at runtime, so there's no
1703                  need to do anything now, except for R_88K_32
1704                  relocations that have been turned into
1705                  R_88K_BBASED_32.  */
1706 	      if (!relocate)
1707 		continue;
1708 	    }
1709 
1710 	  break;
1711 
1712 	case R_88K_GNU_VTINHERIT:
1713 	case R_88K_GNU_VTENTRY:
1714 	  /* These are no-ops in the end.  */
1715 	  continue;
1716 
1717 	default:
1718 	  break;
1719 	}
1720 
1721       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1722 	 because such sections are not SEC_ALLOC and thus ld.so will
1723 	 not process them.  */
1724       if (unresolved_reloc
1725 	  && !((input_section->flags & SEC_DEBUGGING) != 0
1726 	       && h->def_dynamic))
1727 	{
1728 	  (*_bfd_error_handler)
1729 	    (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1730 	     input_bfd, input_section, (long) rel->r_offset, howto->name,
1731 	     h->root.root.string);
1732 	  return FALSE;
1733 	}
1734 
1735       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1736 				    contents, rel->r_offset,
1737 				    relocation, rel->r_addend);
1738 
1739       if (r != bfd_reloc_ok)
1740 	{
1741 	  if (sym_name == NULL)
1742 	    return FALSE;
1743 
1744 	  if (r == bfd_reloc_overflow)
1745 	    {
1746 	      if (!(info->callbacks->reloc_overflow
1747 		    (info, (h ? &h->root : NULL), sym_name, howto->name,
1748 		     (bfd_vma) 0, input_bfd, input_section,
1749 		     rel->r_offset)))
1750 		return FALSE;
1751 	    }
1752 	  else
1753 	    {
1754 	      (*_bfd_error_handler)
1755 		(_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
1756 		 input_bfd, input_section, (long) rel->r_offset, howto->name,
1757 		 sym_name, (int) r);
1758 	      return FALSE;
1759 	    }
1760 	}
1761     }
1762 
1763   return TRUE;
1764 }
1765 
1766 /* Finish up dynamic symbol handling.  We set the contents of various
1767    dynamic sections here.  */
1768 
1769 static bfd_boolean
1770 elf_m88k_finish_dynamic_symbol (output_bfd, info, h, sym)
1771      bfd *output_bfd;
1772      struct bfd_link_info *info;
1773      struct elf_link_hash_entry *h;
1774      Elf_Internal_Sym *sym;
1775 {
1776   bfd *dynobj;
1777 
1778   dynobj = elf_hash_table (info)->dynobj;
1779 
1780   if (h->plt.offset != (bfd_vma) -1)
1781     {
1782       asection *splt;
1783       asection *sgot;
1784       asection *srela;
1785       bfd_vma plt_index;
1786       bfd_vma got_offset;
1787       bfd_vma dyn_offset;
1788       Elf_Internal_Rela rela;
1789       unsigned int br;
1790 
1791       /* This symbol has an entry in the procedure linkage table.  Set
1792 	 it up.  */
1793 
1794       BFD_ASSERT (h->dynindx != -1);
1795 
1796       splt = bfd_get_section_by_name (dynobj, ".plt");
1797       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1798       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1799       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1800 
1801       /* Get the index in the procedure linkage table which
1802 	 corresponds to this symbol.  This is the index of this symbol
1803 	 in all the symbols for which we are making plt entries.  The
1804 	 first entry in the procedure linkage table, spanning three
1805 	 regular entries, is reserved.  */
1806       plt_index = (h->plt.offset - PLT_ENTRY0_SIZE) / PLT_ENTRY_SIZE;
1807 
1808       /* Get the offset into the .got table of the entry that
1809 	 corresponds to this function.  Each .got entry is 4 bytes.
1810 	 The first three are reserved.  */
1811       got_offset = (plt_index + 3) * 4;
1812 
1813       /* Fill in the entry in the procedure linkage table.  */
1814       memcpy (splt->contents + h->plt.offset, elf_m88k_plt_entry,
1815 	      PLT_ENTRY_SIZE);
1816 
1817       dyn_offset = sgot->output_section->vma + sgot->output_offset + got_offset;
1818       bfd_put_16 (output_bfd, dyn_offset >> 16,
1819 		  splt->contents + h->plt.offset + 2);
1820       bfd_put_16 (output_bfd, dyn_offset & 0xffff,
1821 		  splt->contents + h->plt.offset + 6);
1822 
1823       dyn_offset = plt_index * sizeof (Elf32_External_Rela);
1824       bfd_put_16 (output_bfd, dyn_offset >> 16,
1825 		  splt->contents + h->plt.offset + 14);
1826       bfd_put_16 (output_bfd, dyn_offset & 0xffff,
1827 		  splt->contents + h->plt.offset + 18);
1828 
1829       br = bfd_get_32 (output_bfd, splt->contents + h->plt.offset + 20);
1830       br &= ~0x03ffffff;
1831       br |= 0x04000000 -  ((h->plt.offset + 20) >> 2);
1832       bfd_put_32 (output_bfd, br,
1833 		  splt->contents + h->plt.offset + 20);
1834 
1835       /* Fill in the entry in the global offset table.  */
1836       bfd_put_32 (output_bfd,
1837 		  splt->output_section->vma
1838 		  + splt->output_offset
1839 		  + h->plt.offset
1840 		  + 12,
1841 		  sgot->contents + got_offset);
1842 
1843       /* Fill in the entry in the .rela.plt section.  */
1844       rela.r_offset = (sgot->output_section->vma
1845 		       + sgot->output_offset
1846 		       + got_offset);
1847       rela.r_info = ELF32_R_INFO (h->dynindx, R_88K_GOTP_ENT);
1848       rela.r_addend = splt->output_section->vma + h->plt.offset;
1849       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1850 				 srela->contents
1851 				 + plt_index * sizeof (Elf32_External_Rela));
1852 
1853       if (!h->def_regular)
1854 	{
1855 	  /* Mark the symbol as undefined, rather than as defined in
1856 	     the .plt section.  Leave the value alone.  */
1857 	  sym->st_shndx = SHN_UNDEF;
1858 	}
1859     }
1860 
1861   if (h->got.offset != (bfd_vma) -1)
1862     {
1863       asection *sgot;
1864       asection *srela;
1865       Elf_Internal_Rela rela;
1866       bfd_byte *loc;
1867 
1868       /* This symbol has an entry in the global offset table.  Set it
1869 	 up.  */
1870 
1871       sgot = bfd_get_section_by_name (dynobj, ".got");
1872       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1873       BFD_ASSERT (sgot != NULL && srela != NULL);
1874 
1875       rela.r_offset = (sgot->output_section->vma
1876 		       + sgot->output_offset
1877 		       + (h->got.offset &~ (bfd_vma) 1));
1878 
1879       /* If this is a -Bsymbolic link, and the symbol is defined
1880 	 locally, we just want to emit a BBASED reloc.  Likewise if
1881 	 the symbol was forced to be local because of a version file.
1882 	 The entry in the global offset table will already have been
1883 	 initialized in the relocate_section function.  */
1884       if (info->shared
1885 	  && (info->symbolic
1886 	      || h->dynindx == -1
1887 	      || h->forced_local)
1888 	  && h->def_regular)
1889 	{
1890 	  rela.r_info = ELF32_R_INFO (0, R_88K_BBASED_32);
1891 	  rela.r_addend = bfd_get_signed_32 (output_bfd,
1892 					     (sgot->contents
1893 					      + (h->got.offset &~ (bfd_vma) 1)));
1894 	}
1895       else
1896 	{
1897 	  bfd_put_32 (output_bfd, (bfd_vma) 0,
1898 		      sgot->contents + (h->got.offset &~ (bfd_vma) 1));
1899 	  rela.r_info = ELF32_R_INFO (h->dynindx, R_88K_32);
1900 	  rela.r_addend = 0;
1901 	}
1902 
1903       loc = srela->contents;
1904       loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
1905       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1906     }
1907 
1908   if (h->needs_copy)
1909     {
1910       asection *s;
1911       Elf_Internal_Rela rela;
1912       bfd_byte *loc;
1913 
1914       /* This symbol needs a copy reloc.  Set it up.  */
1915 
1916       BFD_ASSERT (h->dynindx != -1
1917 		  && (h->root.type == bfd_link_hash_defined
1918 		      || h->root.type == bfd_link_hash_defweak));
1919 
1920       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1921 				   ".rela.bss");
1922       BFD_ASSERT (s != NULL);
1923 
1924       rela.r_offset = (h->root.u.def.value
1925 		       + h->root.u.def.section->output_section->vma
1926 		       + h->root.u.def.section->output_offset);
1927       rela.r_info = ELF32_R_INFO (h->dynindx, R_88K_COPY);
1928       rela.r_addend = 0;
1929       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
1930       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1931     }
1932 
1933   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
1934   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1935       || h == elf_hash_table (info)->hgot)
1936     sym->st_shndx = SHN_ABS;
1937 
1938   return TRUE;
1939 }
1940 
1941 /* Finish up the dynamic sections.  */
1942 
1943 static bfd_boolean
1944 elf_m88k_finish_dynamic_sections (output_bfd, info)
1945      bfd *output_bfd;
1946      struct bfd_link_info *info;
1947 {
1948   bfd *dynobj;
1949   asection *sgot;
1950   asection *sdyn;
1951 
1952   dynobj = elf_hash_table (info)->dynobj;
1953 
1954   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1955   BFD_ASSERT (sgot != NULL);
1956   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1957 
1958   if (elf_hash_table (info)->dynamic_sections_created)
1959     {
1960       asection *splt;
1961       Elf32_External_Dyn *dyncon, *dynconend;
1962 
1963       splt = bfd_get_section_by_name (dynobj, ".plt");
1964       BFD_ASSERT (splt != NULL && sdyn != NULL);
1965 
1966       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1967       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
1968       for (; dyncon < dynconend; dyncon++)
1969 	{
1970 	  Elf_Internal_Dyn dyn;
1971 	  const char *name;
1972 	  asection *s;
1973 
1974 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1975 
1976 	  switch (dyn.d_tag)
1977 	    {
1978 	    default:
1979 	      break;
1980 
1981 	    case DT_PLTGOT:
1982 	      name = ".got";
1983 	      goto get_vma;
1984 	    case DT_JMPREL:
1985 	      name = ".rela.plt";
1986 	    get_vma:
1987 	      s = bfd_get_section_by_name (output_bfd, name);
1988 	      BFD_ASSERT (s != NULL);
1989 	      dyn.d_un.d_ptr = s->vma;
1990 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1991 	      break;
1992 
1993 	    case DT_PLTRELSZ:
1994 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1995 	      BFD_ASSERT (s != NULL);
1996 	      dyn.d_un.d_val = s->size;
1997 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1998 	      break;
1999 
2000 	    case DT_RELASZ:
2001 	      /* The procedure linkage table relocs (DT_JMPREL) should
2002 		 not be included in the overall relocs (DT_RELA).
2003 		 Therefore, we override the DT_RELASZ entry here to
2004 		 make it not include the JMPREL relocs.  Since the
2005 		 linker script arranges for .rela.plt to follow all
2006 		 other relocation sections, we don't have to worry
2007 		 about changing the DT_RELA entry.  */
2008 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2009 	      if (s != NULL)
2010 		dyn.d_un.d_val -= s->size;
2011 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2012 	      break;
2013 
2014 	    case DT_88K_PLTSTART:
2015 	      name = ".plt";
2016 	      goto get_vma;
2017 	    case DT_88K_PLTEND:
2018 	      s = bfd_get_section_by_name (output_bfd, ".plt");
2019 	      BFD_ASSERT (s != NULL);
2020 	      dyn.d_un.d_ptr = s->vma + s->size;
2021 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2022 	      break;
2023 	    }
2024 	}
2025 
2026       /* Fill in the first entry in the procedure linkage table.  */
2027       if (splt->size > 0)
2028 	{
2029 	      memcpy (splt->contents, elf_m88k_plt0_entry, PLT_ENTRY0_SIZE);
2030 	      bfd_put_16 (output_bfd,
2031 		          (sgot->output_section->vma
2032 		           + sgot->output_offset
2033 		           - (splt->output_section->vma + 16)) >> 16,
2034 		          splt->contents + 18);
2035 	      bfd_put_16 (output_bfd,
2036 		          (sgot->output_section->vma
2037 		           + sgot->output_offset
2038 		           - (splt->output_section->vma + 16)) & 0xffff,
2039 		          splt->contents + 22);
2040               elf_section_data (splt->output_section)->this_hdr.sh_entsize
2041                = PLT_ENTRY0_SIZE;
2042 	}
2043     }
2044 
2045   /* Fill in the first three entries in the global offset table.  */
2046   if (sgot->size > 0)
2047     {
2048       if (sdyn == NULL)
2049 	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2050       else
2051 	bfd_put_32 (output_bfd,
2052 		    sdyn->output_section->vma + sdyn->output_offset,
2053 		    sgot->contents);
2054       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2055       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2056     }
2057 
2058   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2059 
2060   return TRUE;
2061 }
2062 
2063 static enum elf_reloc_type_class
2064 elf32_m88k_reloc_type_class (rela)
2065      const Elf_Internal_Rela *rela;
2066 {
2067   switch ((int) ELF32_R_TYPE (rela->r_info))
2068     {
2069     case R_88K_BBASED_32:
2070     case R_88K_BBASED_16H:
2071     case R_88K_BBASED_16L:
2072       return reloc_class_relative;
2073     case R_88K_GOTP_ENT:
2074       return reloc_class_plt;
2075     case R_88K_COPY:
2076       return reloc_class_copy;
2077     default:
2078       return reloc_class_normal;
2079     }
2080 }
2081 
2082 /* Return address for Ith PLT stub in section PLT, for relocation REL
2083    or (bfd_vma) -1 if it should not be included.  */
2084 static bfd_vma
2085 elf_m88k_plt_sym_val (bfd_vma i, const asection *plt,
2086 		      const arelent *rel ATTRIBUTE_UNUSED)
2087 {
2088   return plt->vma + PLT_ENTRY0_SIZE + i * PLT_ENTRY_SIZE;
2089 }
2090 
2091 #define TARGET_BIG_SYM			bfd_elf32_m88k_vec
2092 #define TARGET_BIG_NAME			"elf32-m88k"
2093 #define ELF_MACHINE_CODE		EM_88K
2094 #define ELF_MAXPAGESIZE			0x1000
2095 #define elf_backend_create_dynamic_sections \
2096 					_bfd_elf_create_dynamic_sections
2097 #define bfd_elf32_bfd_link_hash_table_create \
2098 					elf_m88k_link_hash_table_create
2099 #define bfd_elf32_bfd_final_link	bfd_elf_gc_common_final_link
2100 
2101 #define elf_backend_check_relocs	elf_m88k_check_relocs
2102 #define elf_backend_adjust_dynamic_symbol \
2103 					elf_m88k_adjust_dynamic_symbol
2104 #define elf_backend_size_dynamic_sections \
2105 					elf_m88k_size_dynamic_sections
2106 #define elf_backend_relocate_section	elf_m88k_relocate_section
2107 #define elf_backend_finish_dynamic_symbol \
2108 					elf_m88k_finish_dynamic_symbol
2109 #define elf_backend_finish_dynamic_sections \
2110 					elf_m88k_finish_dynamic_sections
2111 #define elf_backend_gc_mark_hook	elf_m88k_gc_mark_hook
2112 #define elf_backend_gc_sweep_hook	elf_m88k_gc_sweep_hook
2113 #define bfd_elf32_bfd_merge_private_bfd_data \
2114                                         elf32_m88k_merge_private_bfd_data
2115 #define bfd_elf32_bfd_set_private_flags \
2116                                         elf32_m88k_set_private_flags
2117 #define bfd_elf32_bfd_print_private_bfd_data \
2118                                         elf32_m88k_print_private_bfd_data
2119 #define elf_backend_reloc_type_class	elf32_m88k_reloc_type_class
2120 #define elf_backend_plt_sym_val		elf_m88k_plt_sym_val
2121 
2122 #define elf_backend_can_gc_sections	1
2123 #define elf_backend_can_refcount	1
2124 #define elf_backend_want_got_plt	1
2125 #define elf_backend_plt_readonly	0
2126 #define elf_backend_want_plt_sym	0
2127 #define elf_backend_got_header_size	12
2128 #define elf_backend_rela_normal		1
2129 
2130 #include "elf32-target.h"
2131