xref: /netbsd-src/external/gpl3/gdb/dist/bfd/elf-m10300.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /* Matsushita 10300 specific support for 32-bit ELF
2    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3    2006, 2007, 2008, 2009, 2010 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 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21 
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/mn10300.h"
27 #include "libiberty.h"
28 
29 /* The mn10300 linker needs to keep track of the number of relocs that
30    it decides to copy in check_relocs for each symbol.  This is so
31    that it can discard PC relative relocs if it doesn't need them when
32    linking with -Bsymbolic.  We store the information in a field
33    extending the regular ELF linker hash table.  */
34 
35 struct elf32_mn10300_link_hash_entry
36 {
37   /* The basic elf link hash table entry.  */
38   struct elf_link_hash_entry root;
39 
40   /* For function symbols, the number of times this function is
41      called directly (ie by name).  */
42   unsigned int direct_calls;
43 
44   /* For function symbols, the size of this function's stack
45      (if <= 255 bytes).  We stuff this into "call" instructions
46      to this target when it's valid and profitable to do so.
47 
48      This does not include stack allocated by movm!  */
49   unsigned char stack_size;
50 
51   /* For function symbols, arguments (if any) for movm instruction
52      in the prologue.  We stuff this value into "call" instructions
53      to the target when it's valid and profitable to do so.  */
54   unsigned char movm_args;
55 
56   /* For function symbols, the amount of stack space that would be allocated
57      by the movm instruction.  This is redundant with movm_args, but we
58      add it to the hash table to avoid computing it over and over.  */
59   unsigned char movm_stack_size;
60 
61 /* When set, convert all "call" instructions to this target into "calls"
62    instructions.  */
63 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
64 
65 /* Used to mark functions which have had redundant parts of their
66    prologue deleted.  */
67 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
68   unsigned char flags;
69 
70   /* Calculated value.  */
71   bfd_vma value;
72 };
73 
74 /* We derive a hash table from the main elf linker hash table so
75    we can store state variables and a secondary hash table without
76    resorting to global variables.  */
77 struct elf32_mn10300_link_hash_table
78 {
79   /* The main hash table.  */
80   struct elf_link_hash_table root;
81 
82   /* A hash table for static functions.  We could derive a new hash table
83      instead of using the full elf32_mn10300_link_hash_table if we wanted
84      to save some memory.  */
85   struct elf32_mn10300_link_hash_table *static_hash_table;
86 
87   /* Random linker state flags.  */
88 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
89   char flags;
90 };
91 
92 #ifndef streq
93 #define streq(a, b) (strcmp ((a),(b)) == 0)
94 #endif
95 
96 /* For MN10300 linker hash table.  */
97 
98 /* Get the MN10300 ELF linker hash table from a link_info structure.  */
99 
100 #define elf32_mn10300_hash_table(p) \
101   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
102   == MN10300_ELF_DATA ? ((struct elf32_mn10300_link_hash_table *) ((p)->hash)) : NULL)
103 
104 #define elf32_mn10300_link_hash_traverse(table, func, info)		\
105   (elf_link_hash_traverse						\
106    (&(table)->root,							\
107     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),	\
108     (info)))
109 
110 static reloc_howto_type elf_mn10300_howto_table[] =
111 {
112   /* Dummy relocation.  Does nothing.  */
113   HOWTO (R_MN10300_NONE,
114 	 0,
115 	 2,
116 	 16,
117 	 FALSE,
118 	 0,
119 	 complain_overflow_bitfield,
120 	 bfd_elf_generic_reloc,
121 	 "R_MN10300_NONE",
122 	 FALSE,
123 	 0,
124 	 0,
125 	 FALSE),
126   /* Standard 32 bit reloc.  */
127   HOWTO (R_MN10300_32,
128 	 0,
129 	 2,
130 	 32,
131 	 FALSE,
132 	 0,
133 	 complain_overflow_bitfield,
134 	 bfd_elf_generic_reloc,
135 	 "R_MN10300_32",
136 	 FALSE,
137 	 0xffffffff,
138 	 0xffffffff,
139 	 FALSE),
140   /* Standard 16 bit reloc.  */
141   HOWTO (R_MN10300_16,
142 	 0,
143 	 1,
144 	 16,
145 	 FALSE,
146 	 0,
147 	 complain_overflow_bitfield,
148 	 bfd_elf_generic_reloc,
149 	 "R_MN10300_16",
150 	 FALSE,
151 	 0xffff,
152 	 0xffff,
153 	 FALSE),
154   /* Standard 8 bit reloc.  */
155   HOWTO (R_MN10300_8,
156 	 0,
157 	 0,
158 	 8,
159 	 FALSE,
160 	 0,
161 	 complain_overflow_bitfield,
162 	 bfd_elf_generic_reloc,
163 	 "R_MN10300_8",
164 	 FALSE,
165 	 0xff,
166 	 0xff,
167 	 FALSE),
168   /* Standard 32bit pc-relative reloc.  */
169   HOWTO (R_MN10300_PCREL32,
170 	 0,
171 	 2,
172 	 32,
173 	 TRUE,
174 	 0,
175 	 complain_overflow_bitfield,
176 	 bfd_elf_generic_reloc,
177 	 "R_MN10300_PCREL32",
178 	 FALSE,
179 	 0xffffffff,
180 	 0xffffffff,
181 	 TRUE),
182   /* Standard 16bit pc-relative reloc.  */
183   HOWTO (R_MN10300_PCREL16,
184 	 0,
185 	 1,
186 	 16,
187 	 TRUE,
188 	 0,
189 	 complain_overflow_bitfield,
190 	 bfd_elf_generic_reloc,
191 	 "R_MN10300_PCREL16",
192 	 FALSE,
193 	 0xffff,
194 	 0xffff,
195 	 TRUE),
196   /* Standard 8 pc-relative reloc.  */
197   HOWTO (R_MN10300_PCREL8,
198 	 0,
199 	 0,
200 	 8,
201 	 TRUE,
202 	 0,
203 	 complain_overflow_bitfield,
204 	 bfd_elf_generic_reloc,
205 	 "R_MN10300_PCREL8",
206 	 FALSE,
207 	 0xff,
208 	 0xff,
209 	 TRUE),
210 
211   /* GNU extension to record C++ vtable hierarchy.  */
212   HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
213 	 0,			/* rightshift */
214 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
215 	 0,			/* bitsize */
216 	 FALSE,			/* pc_relative */
217 	 0,			/* bitpos */
218 	 complain_overflow_dont, /* complain_on_overflow */
219 	 NULL,			/* special_function */
220 	 "R_MN10300_GNU_VTINHERIT", /* name */
221 	 FALSE,			/* partial_inplace */
222 	 0,			/* src_mask */
223 	 0,			/* dst_mask */
224 	 FALSE),		/* pcrel_offset */
225 
226   /* GNU extension to record C++ vtable member usage */
227   HOWTO (R_MN10300_GNU_VTENTRY,	/* type */
228 	 0,			/* rightshift */
229 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
230 	 0,			/* bitsize */
231 	 FALSE,			/* pc_relative */
232 	 0,			/* bitpos */
233 	 complain_overflow_dont, /* complain_on_overflow */
234 	 NULL,			/* special_function */
235 	 "R_MN10300_GNU_VTENTRY", /* name */
236 	 FALSE,			/* partial_inplace */
237 	 0,			/* src_mask */
238 	 0,			/* dst_mask */
239 	 FALSE),		/* pcrel_offset */
240 
241   /* Standard 24 bit reloc.  */
242   HOWTO (R_MN10300_24,
243 	 0,
244 	 2,
245 	 24,
246 	 FALSE,
247 	 0,
248 	 complain_overflow_bitfield,
249 	 bfd_elf_generic_reloc,
250 	 "R_MN10300_24",
251 	 FALSE,
252 	 0xffffff,
253 	 0xffffff,
254 	 FALSE),
255   HOWTO (R_MN10300_GOTPC32,	/* type */
256 	 0,			/* rightshift */
257 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
258 	 32,			/* bitsize */
259 	 TRUE,			/* pc_relative */
260 	 0,			/* bitpos */
261 	 complain_overflow_bitfield, /* complain_on_overflow */
262 	 bfd_elf_generic_reloc, /* */
263 	 "R_MN10300_GOTPC32",	/* name */
264 	 FALSE,			/* partial_inplace */
265 	 0xffffffff,		/* src_mask */
266 	 0xffffffff,		/* dst_mask */
267 	 TRUE),			/* pcrel_offset */
268 
269   HOWTO (R_MN10300_GOTPC16,	/* type */
270 	 0,			/* rightshift */
271 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
272 	 16,			/* bitsize */
273 	 TRUE,			/* pc_relative */
274 	 0,			/* bitpos */
275 	 complain_overflow_bitfield, /* complain_on_overflow */
276 	 bfd_elf_generic_reloc, /* */
277 	 "R_MN10300_GOTPC16",	/* name */
278 	 FALSE,			/* partial_inplace */
279 	 0xffff,		/* src_mask */
280 	 0xffff,		/* dst_mask */
281 	 TRUE),			/* pcrel_offset */
282 
283   HOWTO (R_MN10300_GOTOFF32,	/* type */
284 	 0,			/* rightshift */
285 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
286 	 32,			/* bitsize */
287 	 FALSE,			/* pc_relative */
288 	 0,			/* bitpos */
289 	 complain_overflow_bitfield, /* complain_on_overflow */
290 	 bfd_elf_generic_reloc, /* */
291 	 "R_MN10300_GOTOFF32",	/* name */
292 	 FALSE,			/* partial_inplace */
293 	 0xffffffff,		/* src_mask */
294 	 0xffffffff,		/* dst_mask */
295 	 FALSE),		/* pcrel_offset */
296 
297   HOWTO (R_MN10300_GOTOFF24,	/* type */
298 	 0,			/* rightshift */
299 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
300 	 24,			/* bitsize */
301 	 FALSE,			/* pc_relative */
302 	 0,			/* bitpos */
303 	 complain_overflow_bitfield, /* complain_on_overflow */
304 	 bfd_elf_generic_reloc, /* */
305 	 "R_MN10300_GOTOFF24",	/* name */
306 	 FALSE,			/* partial_inplace */
307 	 0xffffff,		/* src_mask */
308 	 0xffffff,		/* dst_mask */
309 	 FALSE),		/* pcrel_offset */
310 
311   HOWTO (R_MN10300_GOTOFF16,	/* type */
312 	 0,			/* rightshift */
313 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
314 	 16,			/* bitsize */
315 	 FALSE,			/* pc_relative */
316 	 0,			/* bitpos */
317 	 complain_overflow_bitfield, /* complain_on_overflow */
318 	 bfd_elf_generic_reloc, /* */
319 	 "R_MN10300_GOTOFF16",	/* name */
320 	 FALSE,			/* partial_inplace */
321 	 0xffff,		/* src_mask */
322 	 0xffff,		/* dst_mask */
323 	 FALSE),		/* pcrel_offset */
324 
325   HOWTO (R_MN10300_PLT32,	/* type */
326 	 0,			/* rightshift */
327 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
328 	 32,			/* bitsize */
329 	 TRUE,			/* pc_relative */
330 	 0,			/* bitpos */
331 	 complain_overflow_bitfield, /* complain_on_overflow */
332 	 bfd_elf_generic_reloc, /* */
333 	 "R_MN10300_PLT32",	/* name */
334 	 FALSE,			/* partial_inplace */
335 	 0xffffffff,		/* src_mask */
336 	 0xffffffff,		/* dst_mask */
337 	 TRUE),			/* pcrel_offset */
338 
339   HOWTO (R_MN10300_PLT16,	/* type */
340 	 0,			/* rightshift */
341 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
342 	 16,			/* bitsize */
343 	 TRUE,			/* pc_relative */
344 	 0,			/* bitpos */
345 	 complain_overflow_bitfield, /* complain_on_overflow */
346 	 bfd_elf_generic_reloc, /* */
347 	 "R_MN10300_PLT16",	/* name */
348 	 FALSE,			/* partial_inplace */
349 	 0xffff,		/* src_mask */
350 	 0xffff,		/* dst_mask */
351 	 TRUE),			/* pcrel_offset */
352 
353   HOWTO (R_MN10300_GOT32,	/* type */
354 	 0,			/* rightshift */
355 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
356 	 32,			/* bitsize */
357 	 FALSE,			/* pc_relative */
358 	 0,			/* bitpos */
359 	 complain_overflow_bitfield, /* complain_on_overflow */
360 	 bfd_elf_generic_reloc, /* */
361 	 "R_MN10300_GOT32",	/* name */
362 	 FALSE,			/* partial_inplace */
363 	 0xffffffff,		/* src_mask */
364 	 0xffffffff,		/* dst_mask */
365 	 FALSE),		/* pcrel_offset */
366 
367   HOWTO (R_MN10300_GOT24,	/* type */
368 	 0,			/* rightshift */
369 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
370 	 24,			/* bitsize */
371 	 FALSE,			/* pc_relative */
372 	 0,			/* bitpos */
373 	 complain_overflow_bitfield, /* complain_on_overflow */
374 	 bfd_elf_generic_reloc, /* */
375 	 "R_MN10300_GOT24",	/* name */
376 	 FALSE,			/* partial_inplace */
377 	 0xffffffff,		/* src_mask */
378 	 0xffffffff,		/* dst_mask */
379 	 FALSE),		/* pcrel_offset */
380 
381   HOWTO (R_MN10300_GOT16,	/* type */
382 	 0,			/* rightshift */
383 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
384 	 16,			/* bitsize */
385 	 FALSE,			/* pc_relative */
386 	 0,			/* bitpos */
387 	 complain_overflow_bitfield, /* complain_on_overflow */
388 	 bfd_elf_generic_reloc, /* */
389 	 "R_MN10300_GOT16",	/* name */
390 	 FALSE,			/* partial_inplace */
391 	 0xffffffff,		/* src_mask */
392 	 0xffffffff,		/* dst_mask */
393 	 FALSE),		/* pcrel_offset */
394 
395   HOWTO (R_MN10300_COPY,	/* type */
396 	 0,			/* rightshift */
397 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
398 	 32,			/* bitsize */
399 	 FALSE,			/* pc_relative */
400 	 0,			/* bitpos */
401 	 complain_overflow_bitfield, /* complain_on_overflow */
402 	 bfd_elf_generic_reloc, /* */
403 	 "R_MN10300_COPY",		/* name */
404 	 FALSE,			/* partial_inplace */
405 	 0xffffffff,		/* src_mask */
406 	 0xffffffff,		/* dst_mask */
407 	 FALSE),		/* pcrel_offset */
408 
409   HOWTO (R_MN10300_GLOB_DAT,	/* type */
410 	 0,			/* rightshift */
411 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
412 	 32,			/* bitsize */
413 	 FALSE,			/* pc_relative */
414 	 0,			/* bitpos */
415 	 complain_overflow_bitfield, /* complain_on_overflow */
416 	 bfd_elf_generic_reloc, /* */
417 	 "R_MN10300_GLOB_DAT",	/* name */
418 	 FALSE,			/* partial_inplace */
419 	 0xffffffff,		/* src_mask */
420 	 0xffffffff,		/* dst_mask */
421 	 FALSE),		/* pcrel_offset */
422 
423   HOWTO (R_MN10300_JMP_SLOT,	/* type */
424 	 0,			/* rightshift */
425 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
426 	 32,			/* bitsize */
427 	 FALSE,			/* pc_relative */
428 	 0,			/* bitpos */
429 	 complain_overflow_bitfield, /* complain_on_overflow */
430 	 bfd_elf_generic_reloc, /* */
431 	 "R_MN10300_JMP_SLOT",	/* name */
432 	 FALSE,			/* partial_inplace */
433 	 0xffffffff,		/* src_mask */
434 	 0xffffffff,		/* dst_mask */
435 	 FALSE),		/* pcrel_offset */
436 
437   HOWTO (R_MN10300_RELATIVE,	/* type */
438 	 0,			/* rightshift */
439 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
440 	 32,			/* bitsize */
441 	 FALSE,			/* pc_relative */
442 	 0,			/* bitpos */
443 	 complain_overflow_bitfield, /* complain_on_overflow */
444 	 bfd_elf_generic_reloc, /* */
445 	 "R_MN10300_RELATIVE",	/* name */
446 	 FALSE,			/* partial_inplace */
447 	 0xffffffff,		/* src_mask */
448 	 0xffffffff,		/* dst_mask */
449 	 FALSE),		/* pcrel_offset */
450 
451   EMPTY_HOWTO (24),
452   EMPTY_HOWTO (25),
453   EMPTY_HOWTO (26),
454   EMPTY_HOWTO (27),
455   EMPTY_HOWTO (28),
456   EMPTY_HOWTO (29),
457   EMPTY_HOWTO (30),
458   EMPTY_HOWTO (31),
459   EMPTY_HOWTO (32),
460 
461   HOWTO (R_MN10300_SYM_DIFF,	/* type */
462 	 0,			/* rightshift */
463 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
464 	 32,			/* bitsize */
465 	 FALSE,			/* pc_relative */
466 	 0,			/* bitpos */
467 	 complain_overflow_dont,/* complain_on_overflow */
468 	 NULL, 			/* special handler.  */
469 	 "R_MN10300_SYM_DIFF",	/* name */
470 	 FALSE,			/* partial_inplace */
471 	 0xffffffff,		/* src_mask */
472 	 0xffffffff,		/* dst_mask */
473 	 FALSE),		/* pcrel_offset */
474 
475   HOWTO (R_MN10300_ALIGN,	/* type */
476 	 0,			/* rightshift */
477 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
478 	 32,			/* bitsize */
479 	 FALSE,			/* pc_relative */
480 	 0,			/* bitpos */
481 	 complain_overflow_dont,/* complain_on_overflow */
482 	 NULL, 			/* special handler.  */
483 	 "R_MN10300_ALIGN",	/* name */
484 	 FALSE,			/* partial_inplace */
485 	 0,			/* src_mask */
486 	 0,			/* dst_mask */
487 	 FALSE)			/* pcrel_offset */
488 };
489 
490 struct mn10300_reloc_map
491 {
492   bfd_reloc_code_real_type bfd_reloc_val;
493   unsigned char elf_reloc_val;
494 };
495 
496 static const struct mn10300_reloc_map mn10300_reloc_map[] =
497 {
498   { BFD_RELOC_NONE, R_MN10300_NONE, },
499   { BFD_RELOC_32, R_MN10300_32, },
500   { BFD_RELOC_16, R_MN10300_16, },
501   { BFD_RELOC_8, R_MN10300_8, },
502   { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
503   { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
504   { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
505   { BFD_RELOC_24, R_MN10300_24, },
506   { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
507   { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
508   { BFD_RELOC_32_GOT_PCREL, R_MN10300_GOTPC32 },
509   { BFD_RELOC_16_GOT_PCREL, R_MN10300_GOTPC16 },
510   { BFD_RELOC_32_GOTOFF, R_MN10300_GOTOFF32 },
511   { BFD_RELOC_MN10300_GOTOFF24, R_MN10300_GOTOFF24 },
512   { BFD_RELOC_16_GOTOFF, R_MN10300_GOTOFF16 },
513   { BFD_RELOC_32_PLT_PCREL, R_MN10300_PLT32 },
514   { BFD_RELOC_16_PLT_PCREL, R_MN10300_PLT16 },
515   { BFD_RELOC_MN10300_GOT32, R_MN10300_GOT32 },
516   { BFD_RELOC_MN10300_GOT24, R_MN10300_GOT24 },
517   { BFD_RELOC_MN10300_GOT16, R_MN10300_GOT16 },
518   { BFD_RELOC_MN10300_COPY, R_MN10300_COPY },
519   { BFD_RELOC_MN10300_GLOB_DAT, R_MN10300_GLOB_DAT },
520   { BFD_RELOC_MN10300_JMP_SLOT, R_MN10300_JMP_SLOT },
521   { BFD_RELOC_MN10300_RELATIVE, R_MN10300_RELATIVE },
522   { BFD_RELOC_MN10300_SYM_DIFF, R_MN10300_SYM_DIFF },
523   { BFD_RELOC_MN10300_ALIGN, R_MN10300_ALIGN }
524 };
525 
526 /* Create the GOT section.  */
527 
528 static bfd_boolean
529 _bfd_mn10300_elf_create_got_section (bfd * abfd,
530 				     struct bfd_link_info * info)
531 {
532   flagword   flags;
533   flagword   pltflags;
534   asection * s;
535   struct elf_link_hash_entry * h;
536   const struct elf_backend_data * bed = get_elf_backend_data (abfd);
537   int ptralign;
538 
539   /* This function may be called more than once.  */
540   if (bfd_get_section_by_name (abfd, ".got") != NULL)
541     return TRUE;
542 
543   switch (bed->s->arch_size)
544     {
545     case 32:
546       ptralign = 2;
547       break;
548 
549     case 64:
550       ptralign = 3;
551       break;
552 
553     default:
554       bfd_set_error (bfd_error_bad_value);
555       return FALSE;
556     }
557 
558   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
559 	   | SEC_LINKER_CREATED);
560 
561   pltflags = flags;
562   pltflags |= SEC_CODE;
563   if (bed->plt_not_loaded)
564     pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
565   if (bed->plt_readonly)
566     pltflags |= SEC_READONLY;
567 
568   s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
569   if (s == NULL
570       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
571     return FALSE;
572 
573   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
574      .plt section.  */
575   if (bed->want_plt_sym)
576     {
577       h = _bfd_elf_define_linkage_sym (abfd, info, s,
578 				       "_PROCEDURE_LINKAGE_TABLE_");
579       elf_hash_table (info)->hplt = h;
580       if (h == NULL)
581 	return FALSE;
582     }
583 
584   s = bfd_make_section_with_flags (abfd, ".got", flags);
585   if (s == NULL
586       || ! bfd_set_section_alignment (abfd, s, ptralign))
587     return FALSE;
588 
589   if (bed->want_got_plt)
590     {
591       s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
592       if (s == NULL
593 	  || ! bfd_set_section_alignment (abfd, s, ptralign))
594 	return FALSE;
595     }
596 
597   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
598      (or .got.plt) section.  We don't do this in the linker script
599      because we don't want to define the symbol if we are not creating
600      a global offset table.  */
601   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
602   elf_hash_table (info)->hgot = h;
603   if (h == NULL)
604     return FALSE;
605 
606   /* The first bit of the global offset table is the header.  */
607   s->size += bed->got_header_size;
608 
609   return TRUE;
610 }
611 
612 static reloc_howto_type *
613 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
614 				 bfd_reloc_code_real_type code)
615 {
616   unsigned int i;
617 
618   for (i = ARRAY_SIZE (mn10300_reloc_map); i--;)
619     if (mn10300_reloc_map[i].bfd_reloc_val == code)
620       return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
621 
622   return NULL;
623 }
624 
625 static reloc_howto_type *
626 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
627 				 const char *r_name)
628 {
629   unsigned int i;
630 
631   for (i = ARRAY_SIZE (elf_mn10300_howto_table); i--;)
632     if (elf_mn10300_howto_table[i].name != NULL
633 	&& strcasecmp (elf_mn10300_howto_table[i].name, r_name) == 0)
634       return elf_mn10300_howto_table + i;
635 
636   return NULL;
637 }
638 
639 /* Set the howto pointer for an MN10300 ELF reloc.  */
640 
641 static void
642 mn10300_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
643 		       arelent *cache_ptr,
644 		       Elf_Internal_Rela *dst)
645 {
646   unsigned int r_type;
647 
648   r_type = ELF32_R_TYPE (dst->r_info);
649   BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
650   cache_ptr->howto = elf_mn10300_howto_table + r_type;
651 }
652 
653 /* Look through the relocs for a section during the first phase.
654    Since we don't do .gots or .plts, we just need to consider the
655    virtual table relocs for gc.  */
656 
657 static bfd_boolean
658 mn10300_elf_check_relocs (bfd *abfd,
659 			  struct bfd_link_info *info,
660 			  asection *sec,
661 			  const Elf_Internal_Rela *relocs)
662 {
663   bfd_boolean sym_diff_reloc_seen;
664   Elf_Internal_Shdr *symtab_hdr;
665   Elf_Internal_Sym * isymbuf = NULL;
666   struct elf_link_hash_entry **sym_hashes;
667   const Elf_Internal_Rela *rel;
668   const Elf_Internal_Rela *rel_end;
669   bfd *      dynobj;
670   bfd_vma *  local_got_offsets;
671   asection * sgot;
672   asection * srelgot;
673   asection * sreloc;
674   bfd_boolean result = FALSE;
675 
676   sgot    = NULL;
677   srelgot = NULL;
678   sreloc  = NULL;
679 
680   if (info->relocatable)
681     return TRUE;
682 
683   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
684   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
685   sym_hashes = elf_sym_hashes (abfd);
686 
687   dynobj = elf_hash_table (info)->dynobj;
688   local_got_offsets = elf_local_got_offsets (abfd);
689   rel_end = relocs + sec->reloc_count;
690   sym_diff_reloc_seen = FALSE;
691 
692   for (rel = relocs; rel < rel_end; rel++)
693     {
694       struct elf_link_hash_entry *h;
695       unsigned long r_symndx;
696       unsigned int r_type;
697 
698       r_symndx = ELF32_R_SYM (rel->r_info);
699       if (r_symndx < symtab_hdr->sh_info)
700 	h = NULL;
701       else
702 	{
703 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
704 	  while (h->root.type == bfd_link_hash_indirect
705 		 || h->root.type == bfd_link_hash_warning)
706 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
707 	}
708 
709       r_type = ELF32_R_TYPE (rel->r_info);
710 
711       /* Some relocs require a global offset table.  */
712       if (dynobj == NULL)
713 	{
714 	  switch (r_type)
715 	    {
716 	    case R_MN10300_GOT32:
717 	    case R_MN10300_GOT24:
718 	    case R_MN10300_GOT16:
719 	    case R_MN10300_GOTOFF32:
720 	    case R_MN10300_GOTOFF24:
721 	    case R_MN10300_GOTOFF16:
722 	    case R_MN10300_GOTPC32:
723 	    case R_MN10300_GOTPC16:
724 	      elf_hash_table (info)->dynobj = dynobj = abfd;
725 	      if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
726 		goto fail;
727 	      break;
728 
729 	    default:
730 	      break;
731 	    }
732 	}
733 
734       switch (r_type)
735 	{
736 	/* This relocation describes the C++ object vtable hierarchy.
737 	   Reconstruct it for later use during GC.  */
738 	case R_MN10300_GNU_VTINHERIT:
739 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
740 	    goto fail;
741 	  break;
742 
743 	/* This relocation describes which C++ vtable entries are actually
744 	   used.  Record for later use during GC.  */
745 	case R_MN10300_GNU_VTENTRY:
746 	  BFD_ASSERT (h != NULL);
747 	  if (h != NULL
748 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
749 	    goto fail;
750 	  break;
751 
752 	case R_MN10300_GOT32:
753 	case R_MN10300_GOT24:
754 	case R_MN10300_GOT16:
755 	  /* This symbol requires a global offset table entry.  */
756 
757 	  if (sgot == NULL)
758 	    {
759 	      sgot = bfd_get_section_by_name (dynobj, ".got");
760 	      BFD_ASSERT (sgot != NULL);
761 	    }
762 
763 	  if (srelgot == NULL
764 	      && (h != NULL || info->shared))
765 	    {
766 	      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
767 	      if (srelgot == NULL)
768 		{
769 		  srelgot = bfd_make_section_with_flags (dynobj,
770 							 ".rela.got",
771 							 (SEC_ALLOC
772 							  | SEC_LOAD
773 							  | SEC_HAS_CONTENTS
774 							  | SEC_IN_MEMORY
775 							  | SEC_LINKER_CREATED
776 							  | SEC_READONLY));
777 		  if (srelgot == NULL
778 		      || ! bfd_set_section_alignment (dynobj, srelgot, 2))
779 		    goto fail;
780 		}
781 	    }
782 
783 	  if (h != NULL)
784 	    {
785 	      if (h->got.offset != (bfd_vma) -1)
786 		/* We have already allocated space in the .got.  */
787 		break;
788 
789 	      h->got.offset = sgot->size;
790 
791 	      /* Make sure this symbol is output as a dynamic symbol.  */
792 	      if (h->dynindx == -1)
793 		{
794 		  if (! bfd_elf_link_record_dynamic_symbol (info, h))
795 		    goto fail;
796 		}
797 
798 	      srelgot->size += sizeof (Elf32_External_Rela);
799 	    }
800 	  else
801 	    {
802 	      /* This is a global offset table entry for a local
803 		 symbol.  */
804 	      if (local_got_offsets == NULL)
805 		{
806 		  size_t       size;
807 		  unsigned int i;
808 
809 		  size = symtab_hdr->sh_info * sizeof (bfd_vma);
810 		  local_got_offsets = bfd_alloc (abfd, size);
811 
812 		  if (local_got_offsets == NULL)
813 		    goto fail;
814 
815 		  elf_local_got_offsets (abfd) = local_got_offsets;
816 
817 		  for (i = 0; i < symtab_hdr->sh_info; i++)
818 		    local_got_offsets[i] = (bfd_vma) -1;
819 		}
820 
821 	      if (local_got_offsets[r_symndx] != (bfd_vma) -1)
822 		/* We have already allocated space in the .got.  */
823 		break;
824 
825 	      local_got_offsets[r_symndx] = sgot->size;
826 
827 	      if (info->shared)
828 		/* If we are generating a shared object, we need to
829 		   output a R_MN10300_RELATIVE reloc so that the dynamic
830 		   linker can adjust this GOT entry.  */
831 		srelgot->size += sizeof (Elf32_External_Rela);
832 	    }
833 
834 	  sgot->size += 4;
835 	  break;
836 
837 	case R_MN10300_PLT32:
838 	case R_MN10300_PLT16:
839 	  /* This symbol requires a procedure linkage table entry.  We
840 	     actually build the entry in adjust_dynamic_symbol,
841 	     because this might be a case of linking PIC code which is
842 	     never referenced by a dynamic object, in which case we
843 	     don't need to generate a procedure linkage table entry
844 	     after all.  */
845 
846 	  /* If this is a local symbol, we resolve it directly without
847 	     creating a procedure linkage table entry.  */
848 	  if (h == NULL)
849 	    continue;
850 
851 	  if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
852 	      || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
853 	    break;
854 
855 	  h->needs_plt = 1;
856 	  break;
857 
858 	case R_MN10300_24:
859 	case R_MN10300_16:
860 	case R_MN10300_8:
861 	case R_MN10300_PCREL32:
862 	case R_MN10300_PCREL16:
863 	case R_MN10300_PCREL8:
864 	  if (h != NULL)
865 	    h->non_got_ref = 1;
866 	  break;
867 
868 	case R_MN10300_SYM_DIFF:
869 	  sym_diff_reloc_seen = TRUE;
870 	  break;
871 
872 	case R_MN10300_32:
873 	  if (h != NULL)
874 	    h->non_got_ref = 1;
875 
876 	  /* If we are creating a shared library, then we
877 	     need to copy the reloc into the shared library.  */
878 	  if (info->shared
879 	      && (sec->flags & SEC_ALLOC) != 0
880 	      /* Do not generate a dynamic reloc for a
881 		 reloc associated with a SYM_DIFF operation.  */
882 	      && ! sym_diff_reloc_seen)
883 	    {
884 	      asection * sym_section = NULL;
885 
886 	      /* Find the section containing the
887 		 symbol involved in the relocation.  */
888 	      if (h == NULL)
889 		{
890 		  Elf_Internal_Sym * isym;
891 
892 		  if (isymbuf == NULL)
893 		    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
894 						    symtab_hdr->sh_info, 0,
895 						    NULL, NULL, NULL);
896 		  if (isymbuf)
897 		    {
898 		      isym = isymbuf + r_symndx;
899 		      /* All we care about is whether this local symbol is absolute.  */
900 		      if (isym->st_shndx == SHN_ABS)
901 			sym_section = bfd_abs_section_ptr;
902 		    }
903 		}
904 	      else
905 		{
906 		  if (h->root.type == bfd_link_hash_defined
907 		      || h->root.type == bfd_link_hash_defweak)
908 		    sym_section = h->root.u.def.section;
909 		}
910 
911 	      /* If the symbol is absolute then the relocation can
912 		 be resolved during linking and there is no need for
913 		 a dynamic reloc.  */
914 	      if (sym_section != bfd_abs_section_ptr)
915 		{
916 		  /* When creating a shared object, we must copy these
917 		     reloc types into the output file.  We create a reloc
918 		     section in dynobj and make room for this reloc.  */
919 		  if (sreloc == NULL)
920 		    {
921 		      sreloc = _bfd_elf_make_dynamic_reloc_section
922 			(sec, dynobj, 2, abfd, /*rela?*/ TRUE);
923 		      if (sreloc == NULL)
924 			goto fail;
925 		    }
926 
927 		  sreloc->size += sizeof (Elf32_External_Rela);
928 		}
929 	    }
930 
931 	  break;
932 	}
933 
934       if (ELF32_R_TYPE (rel->r_info) != R_MN10300_SYM_DIFF)
935 	sym_diff_reloc_seen = FALSE;
936     }
937 
938   result = TRUE;
939  fail:
940   if (isymbuf != NULL)
941     free (isymbuf);
942 
943   return result;
944 }
945 
946 /* Return the section that should be marked against GC for a given
947    relocation.  */
948 
949 static asection *
950 mn10300_elf_gc_mark_hook (asection *sec,
951 			  struct bfd_link_info *info,
952 			  Elf_Internal_Rela *rel,
953 			  struct elf_link_hash_entry *h,
954 			  Elf_Internal_Sym *sym)
955 {
956   if (h != NULL)
957     switch (ELF32_R_TYPE (rel->r_info))
958       {
959       case R_MN10300_GNU_VTINHERIT:
960       case R_MN10300_GNU_VTENTRY:
961 	return NULL;
962       }
963 
964   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
965 }
966 
967 /* Perform a relocation as part of a final link.  */
968 
969 static bfd_reloc_status_type
970 mn10300_elf_final_link_relocate (reloc_howto_type *howto,
971 				 bfd *input_bfd,
972 				 bfd *output_bfd ATTRIBUTE_UNUSED,
973 				 asection *input_section,
974 				 bfd_byte *contents,
975 				 bfd_vma offset,
976 				 bfd_vma value,
977 				 bfd_vma addend,
978 				 struct elf_link_hash_entry * h,
979 				 unsigned long symndx,
980 				 struct bfd_link_info *info,
981 				 asection *sym_sec ATTRIBUTE_UNUSED,
982 				 int is_local ATTRIBUTE_UNUSED)
983 {
984   static asection *  sym_diff_section;
985   static bfd_vma     sym_diff_value;
986   bfd_boolean is_sym_diff_reloc;
987   unsigned long r_type = howto->type;
988   bfd_byte * hit_data = contents + offset;
989   bfd *      dynobj;
990   asection * sgot;
991   asection * splt;
992   asection * sreloc;
993 
994   dynobj = elf_hash_table (info)->dynobj;
995   sgot   = NULL;
996   splt   = NULL;
997   sreloc = NULL;
998 
999   switch (r_type)
1000     {
1001     case R_MN10300_24:
1002     case R_MN10300_16:
1003     case R_MN10300_8:
1004     case R_MN10300_PCREL8:
1005     case R_MN10300_PCREL16:
1006     case R_MN10300_PCREL32:
1007     case R_MN10300_GOTOFF32:
1008     case R_MN10300_GOTOFF24:
1009     case R_MN10300_GOTOFF16:
1010       if (info->shared
1011 	  && (input_section->flags & SEC_ALLOC) != 0
1012 	  && h != NULL
1013 	  && ! SYMBOL_REFERENCES_LOCAL (info, h))
1014 	return bfd_reloc_dangerous;
1015     }
1016 
1017   is_sym_diff_reloc = FALSE;
1018   if (sym_diff_section != NULL)
1019     {
1020       BFD_ASSERT (sym_diff_section == input_section);
1021 
1022       switch (r_type)
1023 	{
1024 	case R_MN10300_32:
1025 	case R_MN10300_24:
1026 	case R_MN10300_16:
1027 	case R_MN10300_8:
1028 	  value -= sym_diff_value;
1029 	  /* If we are computing a 32-bit value for the location lists
1030 	     and the result is 0 then we add one to the value.  A zero
1031 	     value can result because of linker relaxation deleteing
1032 	     prologue instructions and using a value of 1 (for the begin
1033 	     and end offsets in the location list entry) results in a
1034 	     nul entry which does not prevent the following entries from
1035 	     being parsed.  */
1036 	  if (r_type == R_MN10300_32
1037 	      && value == 0
1038 	      && strcmp (input_section->name, ".debug_loc") == 0)
1039 	    value = 1;
1040 	  sym_diff_section = NULL;
1041 	  is_sym_diff_reloc = TRUE;
1042 	  break;
1043 
1044 	default:
1045 	  sym_diff_section = NULL;
1046 	  break;
1047 	}
1048     }
1049 
1050   switch (r_type)
1051     {
1052     case R_MN10300_SYM_DIFF:
1053       BFD_ASSERT (addend == 0);
1054       /* Cache the input section and value.
1055 	 The offset is unreliable, since relaxation may
1056 	 have reduced the following reloc's offset.  */
1057       sym_diff_section = input_section;
1058       sym_diff_value = value;
1059       return bfd_reloc_ok;
1060 
1061     case R_MN10300_ALIGN:
1062     case R_MN10300_NONE:
1063       return bfd_reloc_ok;
1064 
1065     case R_MN10300_32:
1066       if (info->shared
1067 	  /* Do not generate relocs when an R_MN10300_32 has been used
1068 	     with an R_MN10300_SYM_DIFF to compute a difference of two
1069 	     symbols.  */
1070 	  && is_sym_diff_reloc == FALSE
1071 	  /* Also, do not generate a reloc when the symbol associated
1072 	     with the R_MN10300_32 reloc is absolute - there is no
1073 	     need for a run time computation in this case.  */
1074 	  && sym_sec != bfd_abs_section_ptr
1075 	  /* If the section is not going to be allocated at load time
1076 	     then there is no need to generate relocs for it.  */
1077 	  && (input_section->flags & SEC_ALLOC) != 0)
1078 	{
1079 	  Elf_Internal_Rela outrel;
1080 	  bfd_boolean skip, relocate;
1081 
1082 	  /* When generating a shared object, these relocations are
1083 	     copied into the output file to be resolved at run
1084 	     time.  */
1085 	  if (sreloc == NULL)
1086 	    {
1087 	      sreloc = _bfd_elf_get_dynamic_reloc_section
1088 		(input_bfd, input_section, /*rela?*/ TRUE);
1089 	      if (sreloc == NULL)
1090 		return FALSE;
1091 	    }
1092 
1093 	  skip = FALSE;
1094 
1095 	  outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
1096 						     input_section, offset);
1097 	  if (outrel.r_offset == (bfd_vma) -1)
1098 	    skip = TRUE;
1099 
1100 	  outrel.r_offset += (input_section->output_section->vma
1101 			      + input_section->output_offset);
1102 
1103 	  if (skip)
1104 	    {
1105 	      memset (&outrel, 0, sizeof outrel);
1106 	      relocate = FALSE;
1107 	    }
1108 	  else
1109 	    {
1110 	      /* h->dynindx may be -1 if this symbol was marked to
1111 		 become local.  */
1112 	      if (h == NULL
1113 		  || SYMBOL_REFERENCES_LOCAL (info, h))
1114 		{
1115 		  relocate = TRUE;
1116 		  outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1117 		  outrel.r_addend = value + addend;
1118 		}
1119 	      else
1120 		{
1121 		  BFD_ASSERT (h->dynindx != -1);
1122 		  relocate = FALSE;
1123 		  outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
1124 		  outrel.r_addend = value + addend;
1125 		}
1126 	    }
1127 
1128 	  bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1129 				     (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
1130 						   + sreloc->reloc_count));
1131 	  ++sreloc->reloc_count;
1132 
1133 	  /* If this reloc is against an external symbol, we do
1134 	     not want to fiddle with the addend.  Otherwise, we
1135 	     need to include the symbol value so that it becomes
1136 	     an addend for the dynamic reloc.  */
1137 	  if (! relocate)
1138 	    return bfd_reloc_ok;
1139 	}
1140       value += addend;
1141       bfd_put_32 (input_bfd, value, hit_data);
1142       return bfd_reloc_ok;
1143 
1144     case R_MN10300_24:
1145       value += addend;
1146 
1147       if ((long) value > 0x7fffff || (long) value < -0x800000)
1148 	return bfd_reloc_overflow;
1149 
1150       bfd_put_8 (input_bfd, value & 0xff, hit_data);
1151       bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1152       bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1153       return bfd_reloc_ok;
1154 
1155     case R_MN10300_16:
1156       value += addend;
1157 
1158       if ((long) value > 0x7fff || (long) value < -0x8000)
1159 	return bfd_reloc_overflow;
1160 
1161       bfd_put_16 (input_bfd, value, hit_data);
1162       return bfd_reloc_ok;
1163 
1164     case R_MN10300_8:
1165       value += addend;
1166 
1167       if ((long) value > 0x7f || (long) value < -0x80)
1168 	return bfd_reloc_overflow;
1169 
1170       bfd_put_8 (input_bfd, value, hit_data);
1171       return bfd_reloc_ok;
1172 
1173     case R_MN10300_PCREL8:
1174       value -= (input_section->output_section->vma
1175 		+ input_section->output_offset);
1176       value -= offset;
1177       value += addend;
1178 
1179       if ((long) value > 0x7f || (long) value < -0x80)
1180 	return bfd_reloc_overflow;
1181 
1182       bfd_put_8 (input_bfd, value, hit_data);
1183       return bfd_reloc_ok;
1184 
1185     case R_MN10300_PCREL16:
1186       value -= (input_section->output_section->vma
1187 		+ input_section->output_offset);
1188       value -= offset;
1189       value += addend;
1190 
1191       if ((long) value > 0x7fff || (long) value < -0x8000)
1192 	return bfd_reloc_overflow;
1193 
1194       bfd_put_16 (input_bfd, value, hit_data);
1195       return bfd_reloc_ok;
1196 
1197     case R_MN10300_PCREL32:
1198       value -= (input_section->output_section->vma
1199 		+ input_section->output_offset);
1200       value -= offset;
1201       value += addend;
1202 
1203       bfd_put_32 (input_bfd, value, hit_data);
1204       return bfd_reloc_ok;
1205 
1206     case R_MN10300_GNU_VTINHERIT:
1207     case R_MN10300_GNU_VTENTRY:
1208       return bfd_reloc_ok;
1209 
1210     case R_MN10300_GOTPC32:
1211       /* Use global offset table as symbol value.  */
1212       value = bfd_get_section_by_name (dynobj,
1213 				       ".got")->output_section->vma;
1214       value -= (input_section->output_section->vma
1215 		+ input_section->output_offset);
1216       value -= offset;
1217       value += addend;
1218 
1219       bfd_put_32 (input_bfd, value, hit_data);
1220       return bfd_reloc_ok;
1221 
1222     case R_MN10300_GOTPC16:
1223       /* Use global offset table as symbol value.  */
1224       value = bfd_get_section_by_name (dynobj,
1225 				       ".got")->output_section->vma;
1226       value -= (input_section->output_section->vma
1227 		+ input_section->output_offset);
1228       value -= offset;
1229       value += addend;
1230 
1231       if ((long) value > 0x7fff || (long) value < -0x8000)
1232 	return bfd_reloc_overflow;
1233 
1234       bfd_put_16 (input_bfd, value, hit_data);
1235       return bfd_reloc_ok;
1236 
1237     case R_MN10300_GOTOFF32:
1238       value -= bfd_get_section_by_name (dynobj,
1239 					".got")->output_section->vma;
1240       value += addend;
1241 
1242       bfd_put_32 (input_bfd, value, hit_data);
1243       return bfd_reloc_ok;
1244 
1245     case R_MN10300_GOTOFF24:
1246       value -= bfd_get_section_by_name (dynobj,
1247 					".got")->output_section->vma;
1248       value += addend;
1249 
1250       if ((long) value > 0x7fffff || (long) value < -0x800000)
1251 	return bfd_reloc_overflow;
1252 
1253       bfd_put_8 (input_bfd, value, hit_data);
1254       bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1255       bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1256       return bfd_reloc_ok;
1257 
1258     case R_MN10300_GOTOFF16:
1259       value -= bfd_get_section_by_name (dynobj,
1260 					".got")->output_section->vma;
1261       value += addend;
1262 
1263       if ((long) value > 0x7fff || (long) value < -0x8000)
1264 	return bfd_reloc_overflow;
1265 
1266       bfd_put_16 (input_bfd, value, hit_data);
1267       return bfd_reloc_ok;
1268 
1269     case R_MN10300_PLT32:
1270       if (h != NULL
1271 	  && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1272 	  && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1273 	  && h->plt.offset != (bfd_vma) -1)
1274 	{
1275 	  splt = bfd_get_section_by_name (dynobj, ".plt");
1276 
1277 	  value = (splt->output_section->vma
1278 		   + splt->output_offset
1279 		   + h->plt.offset) - value;
1280 	}
1281 
1282       value -= (input_section->output_section->vma
1283 		+ input_section->output_offset);
1284       value -= offset;
1285       value += addend;
1286 
1287       bfd_put_32 (input_bfd, value, hit_data);
1288       return bfd_reloc_ok;
1289 
1290     case R_MN10300_PLT16:
1291       if (h != NULL
1292 	  && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1293 	  && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1294 	  && h->plt.offset != (bfd_vma) -1)
1295 	{
1296 	  splt = bfd_get_section_by_name (dynobj, ".plt");
1297 
1298 	  value = (splt->output_section->vma
1299 		   + splt->output_offset
1300 		   + h->plt.offset) - value;
1301 	}
1302 
1303       value -= (input_section->output_section->vma
1304 		+ input_section->output_offset);
1305       value -= offset;
1306       value += addend;
1307 
1308       if ((long) value > 0x7fff || (long) value < -0x8000)
1309 	return bfd_reloc_overflow;
1310 
1311       bfd_put_16 (input_bfd, value, hit_data);
1312       return bfd_reloc_ok;
1313 
1314     case R_MN10300_GOT32:
1315     case R_MN10300_GOT24:
1316     case R_MN10300_GOT16:
1317       {
1318 	sgot = bfd_get_section_by_name (dynobj, ".got");
1319 
1320 	  if (h != NULL)
1321 	    {
1322 	      bfd_vma off;
1323 
1324 	      off = h->got.offset;
1325 	      BFD_ASSERT (off != (bfd_vma) -1);
1326 
1327 	      if (! elf_hash_table (info)->dynamic_sections_created
1328 		  || SYMBOL_REFERENCES_LOCAL (info, h))
1329 		/* This is actually a static link, or it is a
1330 		   -Bsymbolic link and the symbol is defined
1331 		   locally, or the symbol was forced to be local
1332 		   because of a version file.  We must initialize
1333 		   this entry in the global offset table.
1334 
1335 		   When doing a dynamic link, we create a .rela.got
1336 		   relocation entry to initialize the value.  This
1337 		   is done in the finish_dynamic_symbol routine.  */
1338 		bfd_put_32 (output_bfd, value,
1339 			    sgot->contents + off);
1340 
1341 	      value = sgot->output_offset + off;
1342 	    }
1343 	  else
1344 	    {
1345 	      bfd_vma off;
1346 
1347 	      off = elf_local_got_offsets (input_bfd)[symndx];
1348 
1349 	      bfd_put_32 (output_bfd, value, sgot->contents + off);
1350 
1351 	      if (info->shared)
1352 		{
1353 		  asection * srelgot;
1354 		  Elf_Internal_Rela outrel;
1355 
1356 		  srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1357 		  BFD_ASSERT (srelgot != NULL);
1358 
1359 		  outrel.r_offset = (sgot->output_section->vma
1360 				     + sgot->output_offset
1361 				     + off);
1362 		  outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1363 		  outrel.r_addend = value;
1364 		  bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1365 					     (bfd_byte *) (((Elf32_External_Rela *)
1366 							    srelgot->contents)
1367 							   + srelgot->reloc_count));
1368 		  ++ srelgot->reloc_count;
1369 		}
1370 
1371 	      value = sgot->output_offset + off;
1372 	    }
1373       }
1374 
1375       value += addend;
1376 
1377       if (r_type == R_MN10300_GOT32)
1378 	{
1379 	  bfd_put_32 (input_bfd, value, hit_data);
1380 	  return bfd_reloc_ok;
1381 	}
1382       else if (r_type == R_MN10300_GOT24)
1383 	{
1384 	  if ((long) value > 0x7fffff || (long) value < -0x800000)
1385 	    return bfd_reloc_overflow;
1386 
1387 	  bfd_put_8 (input_bfd, value & 0xff, hit_data);
1388 	  bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1389 	  bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1390 	  return bfd_reloc_ok;
1391 	}
1392       else if (r_type == R_MN10300_GOT16)
1393 	{
1394 	  if ((long) value > 0x7fff || (long) value < -0x8000)
1395 	    return bfd_reloc_overflow;
1396 
1397 	  bfd_put_16 (input_bfd, value, hit_data);
1398 	  return bfd_reloc_ok;
1399 	}
1400       /* Fall through.  */
1401 
1402     default:
1403       return bfd_reloc_notsupported;
1404     }
1405 }
1406 
1407 /* Relocate an MN10300 ELF section.  */
1408 
1409 static bfd_boolean
1410 mn10300_elf_relocate_section (bfd *output_bfd,
1411 			      struct bfd_link_info *info,
1412 			      bfd *input_bfd,
1413 			      asection *input_section,
1414 			      bfd_byte *contents,
1415 			      Elf_Internal_Rela *relocs,
1416 			      Elf_Internal_Sym *local_syms,
1417 			      asection **local_sections)
1418 {
1419   Elf_Internal_Shdr *symtab_hdr;
1420   struct elf_link_hash_entry **sym_hashes;
1421   Elf_Internal_Rela *rel, *relend;
1422 
1423   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1424   sym_hashes = elf_sym_hashes (input_bfd);
1425 
1426   rel = relocs;
1427   relend = relocs + input_section->reloc_count;
1428   for (; rel < relend; rel++)
1429     {
1430       int r_type;
1431       reloc_howto_type *howto;
1432       unsigned long r_symndx;
1433       Elf_Internal_Sym *sym;
1434       asection *sec;
1435       struct elf32_mn10300_link_hash_entry *h;
1436       bfd_vma relocation;
1437       bfd_reloc_status_type r;
1438 
1439       r_symndx = ELF32_R_SYM (rel->r_info);
1440       r_type = ELF32_R_TYPE (rel->r_info);
1441       howto = elf_mn10300_howto_table + r_type;
1442 
1443       /* Just skip the vtable gc relocs.  */
1444       if (r_type == R_MN10300_GNU_VTINHERIT
1445 	  || r_type == R_MN10300_GNU_VTENTRY)
1446 	continue;
1447 
1448       h = NULL;
1449       sym = NULL;
1450       sec = NULL;
1451       if (r_symndx < symtab_hdr->sh_info)
1452 	{
1453 	  sym = local_syms + r_symndx;
1454 	  sec = local_sections[r_symndx];
1455 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1456 	}
1457       else
1458 	{
1459 	  bfd_boolean unresolved_reloc;
1460 	  bfd_boolean warned;
1461 	  struct elf_link_hash_entry *hh;
1462 
1463 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1464 				   r_symndx, symtab_hdr, sym_hashes,
1465 				   hh, sec, relocation,
1466 				   unresolved_reloc, warned);
1467 
1468 	  h = (struct elf32_mn10300_link_hash_entry *) hh;
1469 
1470 	  if ((h->root.root.type == bfd_link_hash_defined
1471 	      || h->root.root.type == bfd_link_hash_defweak)
1472 	      && (   r_type == R_MN10300_GOTPC32
1473 		  || r_type == R_MN10300_GOTPC16
1474 		  || ((   r_type == R_MN10300_PLT32
1475 		       || r_type == R_MN10300_PLT16)
1476 		      && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
1477 		      && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
1478 		      && h->root.plt.offset != (bfd_vma) -1)
1479 		  || ((   r_type == R_MN10300_GOT32
1480 		       || r_type == R_MN10300_GOT24
1481 		       || r_type == R_MN10300_GOT16)
1482 		      && elf_hash_table (info)->dynamic_sections_created
1483 		      && !SYMBOL_REFERENCES_LOCAL (info, hh))
1484 		  || (r_type == R_MN10300_32
1485 		      /* _32 relocs in executables force _COPY relocs,
1486 			 such that the address of the symbol ends up
1487 			 being local.  */
1488 		      && !info->executable
1489 		      && !SYMBOL_REFERENCES_LOCAL (info, hh)
1490 		      && ((input_section->flags & SEC_ALLOC) != 0
1491 			  /* DWARF will emit R_MN10300_32 relocations
1492 			     in its sections against symbols defined
1493 			     externally in shared libraries.  We can't
1494 			     do anything with them here.  */
1495 			  || ((input_section->flags & SEC_DEBUGGING) != 0
1496 			      && h->root.def_dynamic)))))
1497 	    /* In these cases, we don't need the relocation
1498 	       value.  We check specially because in some
1499 	       obscure cases sec->output_section will be NULL.  */
1500 	    relocation = 0;
1501 
1502 	  else if (!info->relocatable && unresolved_reloc)
1503 	    (*_bfd_error_handler)
1504 	      (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1505 	       input_bfd,
1506 	       input_section,
1507 	       (long) rel->r_offset,
1508 	       howto->name,
1509 	       h->root.root.root.string);
1510 	}
1511 
1512       if (sec != NULL && elf_discarded_section (sec))
1513 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1514 					 rel, relend, howto, contents);
1515 
1516       if (info->relocatable)
1517 	continue;
1518 
1519       r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1520 					   input_section,
1521 					   contents, rel->r_offset,
1522 					   relocation, rel->r_addend,
1523 					   (struct elf_link_hash_entry *) h,
1524 					   r_symndx,
1525 					   info, sec, h == NULL);
1526 
1527       if (r != bfd_reloc_ok)
1528 	{
1529 	  const char *name;
1530 	  const char *msg = NULL;
1531 
1532 	  if (h != NULL)
1533 	    name = h->root.root.root.string;
1534 	  else
1535 	    {
1536 	      name = (bfd_elf_string_from_elf_section
1537 		      (input_bfd, symtab_hdr->sh_link, sym->st_name));
1538 	      if (name == NULL || *name == '\0')
1539 		name = bfd_section_name (input_bfd, sec);
1540 	    }
1541 
1542 	  switch (r)
1543 	    {
1544 	    case bfd_reloc_overflow:
1545 	      if (! ((*info->callbacks->reloc_overflow)
1546 		     (info, (h ? &h->root.root : NULL), name,
1547 		      howto->name, (bfd_vma) 0, input_bfd,
1548 		      input_section, rel->r_offset)))
1549 		return FALSE;
1550 	      break;
1551 
1552 	    case bfd_reloc_undefined:
1553 	      if (! ((*info->callbacks->undefined_symbol)
1554 		     (info, name, input_bfd, input_section,
1555 		      rel->r_offset, TRUE)))
1556 		return FALSE;
1557 	      break;
1558 
1559 	    case bfd_reloc_outofrange:
1560 	      msg = _("internal error: out of range error");
1561 	      goto common_error;
1562 
1563 	    case bfd_reloc_notsupported:
1564 	      msg = _("internal error: unsupported relocation error");
1565 	      goto common_error;
1566 
1567 	    case bfd_reloc_dangerous:
1568 	      if (r_type == R_MN10300_PCREL32)
1569 		msg = _("error: inappropriate relocation type for shared"
1570 			" library (did you forget -fpic?)");
1571 	      else
1572 		msg = _("internal error: suspicious relocation type used"
1573 			" in shared library");
1574 	      goto common_error;
1575 
1576 	    default:
1577 	      msg = _("internal error: unknown error");
1578 	      /* Fall through.  */
1579 
1580 	    common_error:
1581 	      if (!((*info->callbacks->warning)
1582 		    (info, msg, name, input_bfd, input_section,
1583 		     rel->r_offset)))
1584 		return FALSE;
1585 	      break;
1586 	    }
1587 	}
1588     }
1589 
1590   return TRUE;
1591 }
1592 
1593 /* Finish initializing one hash table entry.  */
1594 
1595 static bfd_boolean
1596 elf32_mn10300_finish_hash_table_entry (struct bfd_hash_entry *gen_entry,
1597 				       void * in_args)
1598 {
1599   struct elf32_mn10300_link_hash_entry *entry;
1600   struct bfd_link_info *link_info = (struct bfd_link_info *) in_args;
1601   unsigned int byte_count = 0;
1602 
1603   entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
1604 
1605   if (entry->root.root.type == bfd_link_hash_warning)
1606     entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
1607 
1608   /* If we already know we want to convert "call" to "calls" for calls
1609      to this symbol, then return now.  */
1610   if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
1611     return TRUE;
1612 
1613   /* If there are no named calls to this symbol, or there's nothing we
1614      can move from the function itself into the "call" instruction,
1615      then note that all "call" instructions should be converted into
1616      "calls" instructions and return.  If a symbol is available for
1617      dynamic symbol resolution (overridable or overriding), avoid
1618      custom calling conventions.  */
1619   if (entry->direct_calls == 0
1620       || (entry->stack_size == 0 && entry->movm_args == 0)
1621       || (elf_hash_table (link_info)->dynamic_sections_created
1622 	  && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
1623 	  && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
1624     {
1625       /* Make a note that we should convert "call" instructions to "calls"
1626 	 instructions for calls to this symbol.  */
1627       entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1628       return TRUE;
1629     }
1630 
1631   /* We may be able to move some instructions from the function itself into
1632      the "call" instruction.  Count how many bytes we might be able to
1633      eliminate in the function itself.  */
1634 
1635   /* A movm instruction is two bytes.  */
1636   if (entry->movm_args)
1637     byte_count += 2;
1638 
1639   /* Count the insn to allocate stack space too.  */
1640   if (entry->stack_size > 0)
1641     {
1642       if (entry->stack_size <= 128)
1643 	byte_count += 3;
1644       else
1645 	byte_count += 4;
1646     }
1647 
1648   /* If using "call" will result in larger code, then turn all
1649      the associated "call" instructions into "calls" instructions.  */
1650   if (byte_count < entry->direct_calls)
1651     entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1652 
1653   /* This routine never fails.  */
1654   return TRUE;
1655 }
1656 
1657 /* Used to count hash table entries.  */
1658 
1659 static bfd_boolean
1660 elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry *gen_entry ATTRIBUTE_UNUSED,
1661 					void * in_args)
1662 {
1663   int *count = (int *) in_args;
1664 
1665   (*count) ++;
1666   return TRUE;
1667 }
1668 
1669 /* Used to enumerate hash table entries into a linear array.  */
1670 
1671 static bfd_boolean
1672 elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry *gen_entry,
1673 				       void * in_args)
1674 {
1675   struct bfd_hash_entry ***ptr = (struct bfd_hash_entry ***) in_args;
1676 
1677   **ptr = gen_entry;
1678   (*ptr) ++;
1679   return TRUE;
1680 }
1681 
1682 /* Used to sort the array created by the above.  */
1683 
1684 static int
1685 sort_by_value (const void *va, const void *vb)
1686 {
1687   struct elf32_mn10300_link_hash_entry *a
1688     = *(struct elf32_mn10300_link_hash_entry **) va;
1689   struct elf32_mn10300_link_hash_entry *b
1690     = *(struct elf32_mn10300_link_hash_entry **) vb;
1691 
1692   return a->value - b->value;
1693 }
1694 
1695 /* Compute the stack size and movm arguments for the function
1696    referred to by HASH at address ADDR in section with
1697    contents CONTENTS, store the information in the hash table.  */
1698 
1699 static void
1700 compute_function_info (bfd *abfd,
1701 		       struct elf32_mn10300_link_hash_entry *hash,
1702 		       bfd_vma addr,
1703 		       unsigned char *contents)
1704 {
1705   unsigned char byte1, byte2;
1706   /* We only care about a very small subset of the possible prologue
1707      sequences here.  Basically we look for:
1708 
1709      movm [d2,d3,a2,a3],sp (optional)
1710      add <size>,sp (optional, and only for sizes which fit in an unsigned
1711 		    8 bit number)
1712 
1713      If we find anything else, we quit.  */
1714 
1715   /* Look for movm [regs],sp.  */
1716   byte1 = bfd_get_8 (abfd, contents + addr);
1717   byte2 = bfd_get_8 (abfd, contents + addr + 1);
1718 
1719   if (byte1 == 0xcf)
1720     {
1721       hash->movm_args = byte2;
1722       addr += 2;
1723       byte1 = bfd_get_8 (abfd, contents + addr);
1724       byte2 = bfd_get_8 (abfd, contents + addr + 1);
1725     }
1726 
1727   /* Now figure out how much stack space will be allocated by the movm
1728      instruction.  We need this kept separate from the function's normal
1729      stack space.  */
1730   if (hash->movm_args)
1731     {
1732       /* Space for d2.  */
1733       if (hash->movm_args & 0x80)
1734 	hash->movm_stack_size += 4;
1735 
1736       /* Space for d3.  */
1737       if (hash->movm_args & 0x40)
1738 	hash->movm_stack_size += 4;
1739 
1740       /* Space for a2.  */
1741       if (hash->movm_args & 0x20)
1742 	hash->movm_stack_size += 4;
1743 
1744       /* Space for a3.  */
1745       if (hash->movm_args & 0x10)
1746 	hash->movm_stack_size += 4;
1747 
1748       /* "other" space.  d0, d1, a0, a1, mdr, lir, lar, 4 byte pad.  */
1749       if (hash->movm_args & 0x08)
1750 	hash->movm_stack_size += 8 * 4;
1751 
1752       if (bfd_get_mach (abfd) == bfd_mach_am33
1753 	  || bfd_get_mach (abfd) == bfd_mach_am33_2)
1754 	{
1755 	  /* "exother" space.  e0, e1, mdrq, mcrh, mcrl, mcvf */
1756 	  if (hash->movm_args & 0x1)
1757 	    hash->movm_stack_size += 6 * 4;
1758 
1759 	  /* exreg1 space.  e4, e5, e6, e7 */
1760 	  if (hash->movm_args & 0x2)
1761 	    hash->movm_stack_size += 4 * 4;
1762 
1763 	  /* exreg0 space.  e2, e3  */
1764 	  if (hash->movm_args & 0x4)
1765 	    hash->movm_stack_size += 2 * 4;
1766 	}
1767     }
1768 
1769   /* Now look for the two stack adjustment variants.  */
1770   if (byte1 == 0xf8 && byte2 == 0xfe)
1771     {
1772       int temp = bfd_get_8 (abfd, contents + addr + 2);
1773       temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
1774 
1775       hash->stack_size = -temp;
1776     }
1777   else if (byte1 == 0xfa && byte2 == 0xfe)
1778     {
1779       int temp = bfd_get_16 (abfd, contents + addr + 2);
1780       temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
1781       temp = -temp;
1782 
1783       if (temp < 255)
1784 	hash->stack_size = temp;
1785     }
1786 
1787   /* If the total stack to be allocated by the call instruction is more
1788      than 255 bytes, then we can't remove the stack adjustment by using
1789      "call" (we might still be able to remove the "movm" instruction.  */
1790   if (hash->stack_size + hash->movm_stack_size > 255)
1791     hash->stack_size = 0;
1792 }
1793 
1794 /* Delete some bytes from a section while relaxing.  */
1795 
1796 static bfd_boolean
1797 mn10300_elf_relax_delete_bytes (bfd *abfd,
1798 				asection *sec,
1799 				bfd_vma addr,
1800 				int count)
1801 {
1802   Elf_Internal_Shdr *symtab_hdr;
1803   unsigned int sec_shndx;
1804   bfd_byte *contents;
1805   Elf_Internal_Rela *irel, *irelend;
1806   Elf_Internal_Rela *irelalign;
1807   bfd_vma toaddr;
1808   Elf_Internal_Sym *isym, *isymend;
1809   struct elf_link_hash_entry **sym_hashes;
1810   struct elf_link_hash_entry **end_hashes;
1811   unsigned int symcount;
1812 
1813   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1814 
1815   contents = elf_section_data (sec)->this_hdr.contents;
1816 
1817   irelalign = NULL;
1818   toaddr = sec->size;
1819 
1820   irel = elf_section_data (sec)->relocs;
1821   irelend = irel + sec->reloc_count;
1822 
1823   if (sec->reloc_count > 0)
1824     {
1825       /* If there is an align reloc at the end of the section ignore it.
1826 	 GAS creates these relocs for reasons of its own, and they just
1827 	 serve to keep the section artifically inflated.  */
1828       if (ELF32_R_TYPE ((irelend - 1)->r_info) == (int) R_MN10300_ALIGN)
1829 	--irelend;
1830 
1831       /* The deletion must stop at the next ALIGN reloc for an aligment
1832 	 power larger than, or not a multiple of, the number of bytes we
1833 	 are deleting.  */
1834       for (; irel < irelend; irel++)
1835 	{
1836 	  int alignment = 1 << irel->r_addend;
1837 
1838 	  if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
1839 	      && irel->r_offset > addr
1840 	      && irel->r_offset < toaddr
1841 	      && (count < alignment
1842 		  || alignment % count != 0))
1843 	    {
1844 	      irelalign = irel;
1845 	      toaddr = irel->r_offset;
1846 	      break;
1847 	    }
1848 	}
1849     }
1850 
1851   /* Actually delete the bytes.  */
1852   memmove (contents + addr, contents + addr + count,
1853 	   (size_t) (toaddr - addr - count));
1854 
1855   /* Adjust the section's size if we are shrinking it, or else
1856      pad the bytes between the end of the shrunken region and
1857      the start of the next region with NOP codes.  */
1858   if (irelalign == NULL)
1859     {
1860       sec->size -= count;
1861       /* Include symbols at the end of the section, but
1862 	 not at the end of a sub-region of the section.  */
1863       toaddr ++;
1864     }
1865   else
1866     {
1867       int i;
1868 
1869 #define NOP_OPCODE 0xcb
1870 
1871       for (i = 0; i < count; i ++)
1872 	bfd_put_8 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
1873     }
1874 
1875   /* Adjust all the relocs.  */
1876   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1877     {
1878       /* Get the new reloc address.  */
1879       if ((irel->r_offset > addr
1880 	   && irel->r_offset < toaddr)
1881 	  || (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
1882 	      && irel->r_offset == toaddr))
1883 	irel->r_offset -= count;
1884     }
1885 
1886   /* Adjust the local symbols in the section, reducing their value
1887      by the number of bytes deleted.  Note - symbols within the deleted
1888      region are moved to the address of the start of the region, which
1889      actually means that they will address the byte beyond the end of
1890      the region once the deletion has been completed.  */
1891   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1892   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1893   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1894     {
1895       if (isym->st_shndx == sec_shndx
1896 	  && isym->st_value > addr
1897 	  && isym->st_value < toaddr)
1898 	{
1899 	  if (isym->st_value < addr + count)
1900 	    isym->st_value = addr;
1901 	  else
1902 	    isym->st_value -= count;
1903 	}
1904       /* Adjust the function symbol's size as well.  */
1905       else if (isym->st_shndx == sec_shndx
1906 	       && ELF_ST_TYPE (isym->st_info) == STT_FUNC
1907 	       && isym->st_value + isym->st_size > addr
1908 	       && isym->st_value + isym->st_size < toaddr)
1909 	isym->st_size -= count;
1910     }
1911 
1912   /* Now adjust the global symbols defined in this section.  */
1913   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1914 	      - symtab_hdr->sh_info);
1915   sym_hashes = elf_sym_hashes (abfd);
1916   end_hashes = sym_hashes + symcount;
1917   for (; sym_hashes < end_hashes; sym_hashes++)
1918     {
1919       struct elf_link_hash_entry *sym_hash = *sym_hashes;
1920 
1921       if ((sym_hash->root.type == bfd_link_hash_defined
1922 	   || sym_hash->root.type == bfd_link_hash_defweak)
1923 	  && sym_hash->root.u.def.section == sec
1924 	  && sym_hash->root.u.def.value > addr
1925 	  && sym_hash->root.u.def.value < toaddr)
1926 	{
1927 	  if (sym_hash->root.u.def.value < addr + count)
1928 	    sym_hash->root.u.def.value = addr;
1929 	  else
1930 	    sym_hash->root.u.def.value -= count;
1931 	}
1932       /* Adjust the function symbol's size as well.  */
1933       else if (sym_hash->root.type == bfd_link_hash_defined
1934 	       && sym_hash->root.u.def.section == sec
1935 	       && sym_hash->type == STT_FUNC
1936 	       && sym_hash->root.u.def.value + sym_hash->size > addr
1937 	       && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1938 	sym_hash->size -= count;
1939     }
1940 
1941   /* See if we can move the ALIGN reloc forward.
1942      We have adjusted r_offset for it already.  */
1943   if (irelalign != NULL)
1944     {
1945       bfd_vma alignto, alignaddr;
1946 
1947       if ((int) irelalign->r_addend > 0)
1948 	{
1949 	  /* This is the old address.  */
1950 	  alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1951 	  /* This is where the align points to now.  */
1952 	  alignaddr = BFD_ALIGN (irelalign->r_offset,
1953 				 1 << irelalign->r_addend);
1954 	  if (alignaddr < alignto)
1955 	    /* Tail recursion.  */
1956 	    return mn10300_elf_relax_delete_bytes (abfd, sec, alignaddr,
1957 						   (int) (alignto - alignaddr));
1958 	}
1959     }
1960 
1961   return TRUE;
1962 }
1963 
1964 /* Return TRUE if a symbol exists at the given address, else return
1965    FALSE.  */
1966 
1967 static bfd_boolean
1968 mn10300_elf_symbol_address_p (bfd *abfd,
1969 			      asection *sec,
1970 			      Elf_Internal_Sym *isym,
1971 			      bfd_vma addr)
1972 {
1973   Elf_Internal_Shdr *symtab_hdr;
1974   unsigned int sec_shndx;
1975   Elf_Internal_Sym *isymend;
1976   struct elf_link_hash_entry **sym_hashes;
1977   struct elf_link_hash_entry **end_hashes;
1978   unsigned int symcount;
1979 
1980   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1981 
1982   /* Examine all the symbols.  */
1983   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1984   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1985     if (isym->st_shndx == sec_shndx
1986 	&& isym->st_value == addr)
1987       return TRUE;
1988 
1989   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1990 	      - symtab_hdr->sh_info);
1991   sym_hashes = elf_sym_hashes (abfd);
1992   end_hashes = sym_hashes + symcount;
1993   for (; sym_hashes < end_hashes; sym_hashes++)
1994     {
1995       struct elf_link_hash_entry *sym_hash = *sym_hashes;
1996 
1997       if ((sym_hash->root.type == bfd_link_hash_defined
1998 	   || sym_hash->root.type == bfd_link_hash_defweak)
1999 	  && sym_hash->root.u.def.section == sec
2000 	  && sym_hash->root.u.def.value == addr)
2001 	return TRUE;
2002     }
2003 
2004   return FALSE;
2005 }
2006 
2007 /* This function handles relaxing for the mn10300.
2008 
2009    There are quite a few relaxing opportunities available on the mn10300:
2010 
2011 	* calls:32 -> calls:16 					   2 bytes
2012 	* call:32  -> call:16					   2 bytes
2013 
2014 	* call:32 -> calls:32					   1 byte
2015 	* call:16 -> calls:16					   1 byte
2016 		* These are done anytime using "calls" would result
2017 		in smaller code, or when necessary to preserve the
2018 		meaning of the program.
2019 
2020 	* call:32						   varies
2021 	* call:16
2022 		* In some circumstances we can move instructions
2023 		from a function prologue into a "call" instruction.
2024 		This is only done if the resulting code is no larger
2025 		than the original code.
2026 
2027 	* jmp:32 -> jmp:16					   2 bytes
2028 	* jmp:16 -> bra:8					   1 byte
2029 
2030 		* If the previous instruction is a conditional branch
2031 		around the jump/bra, we may be able to reverse its condition
2032 		and change its target to the jump's target.  The jump/bra
2033 		can then be deleted.				   2 bytes
2034 
2035 	* mov abs32 -> mov abs16				   1 or 2 bytes
2036 
2037 	* Most instructions which accept imm32 can relax to imm16  1 or 2 bytes
2038 	- Most instructions which accept imm16 can relax to imm8   1 or 2 bytes
2039 
2040 	* Most instructions which accept d32 can relax to d16	   1 or 2 bytes
2041 	- Most instructions which accept d16 can relax to d8	   1 or 2 bytes
2042 
2043 	We don't handle imm16->imm8 or d16->d8 as they're very rare
2044 	and somewhat more difficult to support.  */
2045 
2046 static bfd_boolean
2047 mn10300_elf_relax_section (bfd *abfd,
2048 			   asection *sec,
2049 			   struct bfd_link_info *link_info,
2050 			   bfd_boolean *again)
2051 {
2052   Elf_Internal_Shdr *symtab_hdr;
2053   Elf_Internal_Rela *internal_relocs = NULL;
2054   Elf_Internal_Rela *irel, *irelend;
2055   bfd_byte *contents = NULL;
2056   Elf_Internal_Sym *isymbuf = NULL;
2057   struct elf32_mn10300_link_hash_table *hash_table;
2058   asection *section = sec;
2059   bfd_vma align_gap_adjustment;
2060 
2061   if (link_info->relocatable)
2062     (*link_info->callbacks->einfo)
2063       (_("%P%F: --relax and -r may not be used together\n"));
2064 
2065   /* Assume nothing changes.  */
2066   *again = FALSE;
2067 
2068   /* We need a pointer to the mn10300 specific hash table.  */
2069   hash_table = elf32_mn10300_hash_table (link_info);
2070   if (hash_table == NULL)
2071     return FALSE;
2072 
2073   /* Initialize fields in each hash table entry the first time through.  */
2074   if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
2075     {
2076       bfd *input_bfd;
2077 
2078       /* Iterate over all the input bfds.  */
2079       for (input_bfd = link_info->input_bfds;
2080 	   input_bfd != NULL;
2081 	   input_bfd = input_bfd->link_next)
2082 	{
2083 	  /* We're going to need all the symbols for each bfd.  */
2084 	  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2085 	  if (symtab_hdr->sh_info != 0)
2086 	    {
2087 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2088 	      if (isymbuf == NULL)
2089 		isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2090 						symtab_hdr->sh_info, 0,
2091 						NULL, NULL, NULL);
2092 	      if (isymbuf == NULL)
2093 		goto error_return;
2094 	    }
2095 
2096 	  /* Iterate over each section in this bfd.  */
2097 	  for (section = input_bfd->sections;
2098 	       section != NULL;
2099 	       section = section->next)
2100 	    {
2101 	      struct elf32_mn10300_link_hash_entry *hash;
2102 	      asection *sym_sec = NULL;
2103 	      const char *sym_name;
2104 	      char *new_name;
2105 
2106 	      /* If there's nothing to do in this section, skip it.  */
2107 	      if (! ((section->flags & SEC_RELOC) != 0
2108 		     && section->reloc_count != 0))
2109 		continue;
2110 	      if ((section->flags & SEC_ALLOC) == 0)
2111 		continue;
2112 
2113 	      /* Get cached copy of section contents if it exists.  */
2114 	      if (elf_section_data (section)->this_hdr.contents != NULL)
2115 		contents = elf_section_data (section)->this_hdr.contents;
2116 	      else if (section->size != 0)
2117 		{
2118 		  /* Go get them off disk.  */
2119 		  if (!bfd_malloc_and_get_section (input_bfd, section,
2120 						   &contents))
2121 		    goto error_return;
2122 		}
2123 	      else
2124 		contents = NULL;
2125 
2126 	      /* If there aren't any relocs, then there's nothing to do.  */
2127 	      if ((section->flags & SEC_RELOC) != 0
2128 		  && section->reloc_count != 0)
2129 		{
2130 		  /* Get a copy of the native relocations.  */
2131 		  internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2132 							       NULL, NULL,
2133 							       link_info->keep_memory);
2134 		  if (internal_relocs == NULL)
2135 		    goto error_return;
2136 
2137 		  /* Now examine each relocation.  */
2138 		  irel = internal_relocs;
2139 		  irelend = irel + section->reloc_count;
2140 		  for (; irel < irelend; irel++)
2141 		    {
2142 		      long r_type;
2143 		      unsigned long r_index;
2144 		      unsigned char code;
2145 
2146 		      r_type = ELF32_R_TYPE (irel->r_info);
2147 		      r_index = ELF32_R_SYM (irel->r_info);
2148 
2149 		      if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
2150 			goto error_return;
2151 
2152 		      /* We need the name and hash table entry of the target
2153 			 symbol!  */
2154 		      hash = NULL;
2155 		      sym_sec = NULL;
2156 
2157 		      if (r_index < symtab_hdr->sh_info)
2158 			{
2159 			  /* A local symbol.  */
2160 			  Elf_Internal_Sym *isym;
2161 			  struct elf_link_hash_table *elftab;
2162 			  bfd_size_type amt;
2163 
2164 			  isym = isymbuf + r_index;
2165 			  if (isym->st_shndx == SHN_UNDEF)
2166 			    sym_sec = bfd_und_section_ptr;
2167 			  else if (isym->st_shndx == SHN_ABS)
2168 			    sym_sec = bfd_abs_section_ptr;
2169 			  else if (isym->st_shndx == SHN_COMMON)
2170 			    sym_sec = bfd_com_section_ptr;
2171 			  else
2172 			    sym_sec
2173 			      = bfd_section_from_elf_index (input_bfd,
2174 							    isym->st_shndx);
2175 
2176 			  sym_name
2177 			    = bfd_elf_string_from_elf_section (input_bfd,
2178 							       (symtab_hdr
2179 								->sh_link),
2180 							       isym->st_name);
2181 
2182 			  /* If it isn't a function, then we don't care
2183 			     about it.  */
2184 			  if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
2185 			    continue;
2186 
2187 			  /* Tack on an ID so we can uniquely identify this
2188 			     local symbol in the global hash table.  */
2189 			  amt = strlen (sym_name) + 10;
2190 			  new_name = bfd_malloc (amt);
2191 			  if (new_name == NULL)
2192 			    goto error_return;
2193 
2194 			  sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2195 			  sym_name = new_name;
2196 
2197 			  elftab = &hash_table->static_hash_table->root;
2198 			  hash = ((struct elf32_mn10300_link_hash_entry *)
2199 				  elf_link_hash_lookup (elftab, sym_name,
2200 							TRUE, TRUE, FALSE));
2201 			  free (new_name);
2202 			}
2203 		      else
2204 			{
2205 			  r_index -= symtab_hdr->sh_info;
2206 			  hash = (struct elf32_mn10300_link_hash_entry *)
2207 				   elf_sym_hashes (input_bfd)[r_index];
2208 			}
2209 
2210 		      sym_name = hash->root.root.root.string;
2211 		      if ((section->flags & SEC_CODE) != 0)
2212 			{
2213 			  /* If this is not a "call" instruction, then we
2214 			     should convert "call" instructions to "calls"
2215 			     instructions.  */
2216 			  code = bfd_get_8 (input_bfd,
2217 					    contents + irel->r_offset - 1);
2218 			  if (code != 0xdd && code != 0xcd)
2219 			    hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2220 			}
2221 
2222 		      /* If this is a jump/call, then bump the
2223 			 direct_calls counter.  Else force "call" to
2224 			 "calls" conversions.  */
2225 		      if (r_type == R_MN10300_PCREL32
2226 			  || r_type == R_MN10300_PLT32
2227 			  || r_type == R_MN10300_PLT16
2228 			  || r_type == R_MN10300_PCREL16)
2229 			hash->direct_calls++;
2230 		      else
2231 			hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2232 		    }
2233 		}
2234 
2235 	      /* Now look at the actual contents to get the stack size,
2236 		 and a list of what registers were saved in the prologue
2237 		 (ie movm_args).  */
2238 	      if ((section->flags & SEC_CODE) != 0)
2239 		{
2240 		  Elf_Internal_Sym *isym, *isymend;
2241 		  unsigned int sec_shndx;
2242 		  struct elf_link_hash_entry **hashes;
2243 		  struct elf_link_hash_entry **end_hashes;
2244 		  unsigned int symcount;
2245 
2246 		  sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2247 								 section);
2248 
2249 		  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2250 			      - symtab_hdr->sh_info);
2251 		  hashes = elf_sym_hashes (input_bfd);
2252 		  end_hashes = hashes + symcount;
2253 
2254 		  /* Look at each function defined in this section and
2255 		     update info for that function.  */
2256 		  isymend = isymbuf + symtab_hdr->sh_info;
2257 		  for (isym = isymbuf; isym < isymend; isym++)
2258 		    {
2259 		      if (isym->st_shndx == sec_shndx
2260 			  && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
2261 			{
2262 			  struct elf_link_hash_table *elftab;
2263 			  bfd_size_type amt;
2264 			  struct elf_link_hash_entry **lhashes = hashes;
2265 
2266 			  /* Skip a local symbol if it aliases a
2267 			     global one.  */
2268 			  for (; lhashes < end_hashes; lhashes++)
2269 			    {
2270 			      hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
2271 			      if ((hash->root.root.type == bfd_link_hash_defined
2272 				   || hash->root.root.type == bfd_link_hash_defweak)
2273 				  && hash->root.root.u.def.section == section
2274 				  && hash->root.type == STT_FUNC
2275 				  && hash->root.root.u.def.value == isym->st_value)
2276 				break;
2277 			    }
2278 			  if (lhashes != end_hashes)
2279 			    continue;
2280 
2281 			  if (isym->st_shndx == SHN_UNDEF)
2282 			    sym_sec = bfd_und_section_ptr;
2283 			  else if (isym->st_shndx == SHN_ABS)
2284 			    sym_sec = bfd_abs_section_ptr;
2285 			  else if (isym->st_shndx == SHN_COMMON)
2286 			    sym_sec = bfd_com_section_ptr;
2287 			  else
2288 			    sym_sec
2289 			      = bfd_section_from_elf_index (input_bfd,
2290 							    isym->st_shndx);
2291 
2292 			  sym_name = (bfd_elf_string_from_elf_section
2293 				      (input_bfd, symtab_hdr->sh_link,
2294 				       isym->st_name));
2295 
2296 			  /* Tack on an ID so we can uniquely identify this
2297 			     local symbol in the global hash table.  */
2298 			  amt = strlen (sym_name) + 10;
2299 			  new_name = bfd_malloc (amt);
2300 			  if (new_name == NULL)
2301 			    goto error_return;
2302 
2303 			  sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2304 			  sym_name = new_name;
2305 
2306 			  elftab = &hash_table->static_hash_table->root;
2307 			  hash = ((struct elf32_mn10300_link_hash_entry *)
2308 				  elf_link_hash_lookup (elftab, sym_name,
2309 							TRUE, TRUE, FALSE));
2310 			  free (new_name);
2311 			  compute_function_info (input_bfd, hash,
2312 						 isym->st_value, contents);
2313 			  hash->value = isym->st_value;
2314 			}
2315 		    }
2316 
2317 		  for (; hashes < end_hashes; hashes++)
2318 		    {
2319 		      hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2320 		      if ((hash->root.root.type == bfd_link_hash_defined
2321 			   || hash->root.root.type == bfd_link_hash_defweak)
2322 			  && hash->root.root.u.def.section == section
2323 			  && hash->root.type == STT_FUNC)
2324 			compute_function_info (input_bfd, hash,
2325 					       (hash)->root.root.u.def.value,
2326 					       contents);
2327 		    }
2328 		}
2329 
2330 	      /* Cache or free any memory we allocated for the relocs.  */
2331 	      if (internal_relocs != NULL
2332 		  && elf_section_data (section)->relocs != internal_relocs)
2333 		free (internal_relocs);
2334 	      internal_relocs = NULL;
2335 
2336 	      /* Cache or free any memory we allocated for the contents.  */
2337 	      if (contents != NULL
2338 		  && elf_section_data (section)->this_hdr.contents != contents)
2339 		{
2340 		  if (! link_info->keep_memory)
2341 		    free (contents);
2342 		  else
2343 		    {
2344 		      /* Cache the section contents for elf_link_input_bfd.  */
2345 		      elf_section_data (section)->this_hdr.contents = contents;
2346 		    }
2347 		}
2348 	      contents = NULL;
2349 	    }
2350 
2351 	  /* Cache or free any memory we allocated for the symbols.  */
2352 	  if (isymbuf != NULL
2353 	      && symtab_hdr->contents != (unsigned char *) isymbuf)
2354 	    {
2355 	      if (! link_info->keep_memory)
2356 		free (isymbuf);
2357 	      else
2358 		{
2359 		  /* Cache the symbols for elf_link_input_bfd.  */
2360 		  symtab_hdr->contents = (unsigned char *) isymbuf;
2361 		}
2362 	    }
2363 	  isymbuf = NULL;
2364 	}
2365 
2366       /* Now iterate on each symbol in the hash table and perform
2367 	 the final initialization steps on each.  */
2368       elf32_mn10300_link_hash_traverse (hash_table,
2369 					elf32_mn10300_finish_hash_table_entry,
2370 					link_info);
2371       elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2372 					elf32_mn10300_finish_hash_table_entry,
2373 					link_info);
2374 
2375       {
2376 	/* This section of code collects all our local symbols, sorts
2377 	   them by value, and looks for multiple symbols referring to
2378 	   the same address.  For those symbols, the flags are merged.
2379 	   At this point, the only flag that can be set is
2380 	   MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
2381 	   together.  */
2382 	int static_count = 0, i;
2383 	struct elf32_mn10300_link_hash_entry **entries;
2384 	struct elf32_mn10300_link_hash_entry **ptr;
2385 
2386 	elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2387 					  elf32_mn10300_count_hash_table_entries,
2388 					  &static_count);
2389 
2390 	entries = bfd_malloc (static_count * sizeof (* ptr));
2391 
2392 	ptr = entries;
2393 	elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2394 					  elf32_mn10300_list_hash_table_entries,
2395 					  & ptr);
2396 
2397 	qsort (entries, static_count, sizeof (entries[0]), sort_by_value);
2398 
2399 	for (i = 0; i < static_count - 1; i++)
2400 	  if (entries[i]->value && entries[i]->value == entries[i+1]->value)
2401 	    {
2402 	      int v = entries[i]->flags;
2403 	      int j;
2404 
2405 	      for (j = i + 1; j < static_count && entries[j]->value == entries[i]->value; j++)
2406 		v |= entries[j]->flags;
2407 
2408 	      for (j = i; j < static_count && entries[j]->value == entries[i]->value; j++)
2409 		entries[j]->flags = v;
2410 
2411 	      i = j - 1;
2412 	    }
2413       }
2414 
2415       /* All entries in the hash table are fully initialized.  */
2416       hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
2417 
2418       /* Now that everything has been initialized, go through each
2419 	 code section and delete any prologue insns which will be
2420 	 redundant because their operations will be performed by
2421 	 a "call" instruction.  */
2422       for (input_bfd = link_info->input_bfds;
2423 	   input_bfd != NULL;
2424 	   input_bfd = input_bfd->link_next)
2425 	{
2426 	  /* We're going to need all the local symbols for each bfd.  */
2427 	  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2428 	  if (symtab_hdr->sh_info != 0)
2429 	    {
2430 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2431 	      if (isymbuf == NULL)
2432 		isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2433 						symtab_hdr->sh_info, 0,
2434 						NULL, NULL, NULL);
2435 	      if (isymbuf == NULL)
2436 		goto error_return;
2437 	    }
2438 
2439 	  /* Walk over each section in this bfd.  */
2440 	  for (section = input_bfd->sections;
2441 	       section != NULL;
2442 	       section = section->next)
2443 	    {
2444 	      unsigned int sec_shndx;
2445 	      Elf_Internal_Sym *isym, *isymend;
2446 	      struct elf_link_hash_entry **hashes;
2447 	      struct elf_link_hash_entry **end_hashes;
2448 	      unsigned int symcount;
2449 
2450 	      /* Skip non-code sections and empty sections.  */
2451 	      if ((section->flags & SEC_CODE) == 0 || section->size == 0)
2452 		continue;
2453 
2454 	      if (section->reloc_count != 0)
2455 		{
2456 		  /* Get a copy of the native relocations.  */
2457 		  internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2458 							       NULL, NULL,
2459 							       link_info->keep_memory);
2460 		  if (internal_relocs == NULL)
2461 		    goto error_return;
2462 		}
2463 
2464 	      /* Get cached copy of section contents if it exists.  */
2465 	      if (elf_section_data (section)->this_hdr.contents != NULL)
2466 		contents = elf_section_data (section)->this_hdr.contents;
2467 	      else
2468 		{
2469 		  /* Go get them off disk.  */
2470 		  if (!bfd_malloc_and_get_section (input_bfd, section,
2471 						   &contents))
2472 		    goto error_return;
2473 		}
2474 
2475 	      sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2476 							     section);
2477 
2478 	      /* Now look for any function in this section which needs
2479 		 insns deleted from its prologue.  */
2480 	      isymend = isymbuf + symtab_hdr->sh_info;
2481 	      for (isym = isymbuf; isym < isymend; isym++)
2482 		{
2483 		  struct elf32_mn10300_link_hash_entry *sym_hash;
2484 		  asection *sym_sec = NULL;
2485 		  const char *sym_name;
2486 		  char *new_name;
2487 		  struct elf_link_hash_table *elftab;
2488 		  bfd_size_type amt;
2489 
2490 		  if (isym->st_shndx != sec_shndx)
2491 		    continue;
2492 
2493 		  if (isym->st_shndx == SHN_UNDEF)
2494 		    sym_sec = bfd_und_section_ptr;
2495 		  else if (isym->st_shndx == SHN_ABS)
2496 		    sym_sec = bfd_abs_section_ptr;
2497 		  else if (isym->st_shndx == SHN_COMMON)
2498 		    sym_sec = bfd_com_section_ptr;
2499 		  else
2500 		    sym_sec
2501 		      = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2502 
2503 		  sym_name
2504 		    = bfd_elf_string_from_elf_section (input_bfd,
2505 						       symtab_hdr->sh_link,
2506 						       isym->st_name);
2507 
2508 		  /* Tack on an ID so we can uniquely identify this
2509 		     local symbol in the global hash table.  */
2510 		  amt = strlen (sym_name) + 10;
2511 		  new_name = bfd_malloc (amt);
2512 		  if (new_name == NULL)
2513 		    goto error_return;
2514 		  sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2515 		  sym_name = new_name;
2516 
2517 		  elftab = & hash_table->static_hash_table->root;
2518 		  sym_hash = (struct elf32_mn10300_link_hash_entry *)
2519 		    elf_link_hash_lookup (elftab, sym_name,
2520 					  FALSE, FALSE, FALSE);
2521 
2522 		  free (new_name);
2523 		  if (sym_hash == NULL)
2524 		    continue;
2525 
2526 		  if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2527 		      && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2528 		    {
2529 		      int bytes = 0;
2530 
2531 		      /* Note that we've changed things.  */
2532 		      elf_section_data (section)->relocs = internal_relocs;
2533 		      elf_section_data (section)->this_hdr.contents = contents;
2534 		      symtab_hdr->contents = (unsigned char *) isymbuf;
2535 
2536 		      /* Count how many bytes we're going to delete.  */
2537 		      if (sym_hash->movm_args)
2538 			bytes += 2;
2539 
2540 		      if (sym_hash->stack_size > 0)
2541 			{
2542 			  if (sym_hash->stack_size <= 128)
2543 			    bytes += 3;
2544 			  else
2545 			    bytes += 4;
2546 			}
2547 
2548 		      /* Note that we've deleted prologue bytes for this
2549 			 function.  */
2550 		      sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2551 
2552 		      /* Actually delete the bytes.  */
2553 		      if (!mn10300_elf_relax_delete_bytes (input_bfd,
2554 							   section,
2555 							   isym->st_value,
2556 							   bytes))
2557 			goto error_return;
2558 
2559 		      /* Something changed.  Not strictly necessary, but
2560 			 may lead to more relaxing opportunities.  */
2561 		      *again = TRUE;
2562 		    }
2563 		}
2564 
2565 	      /* Look for any global functions in this section which
2566 		 need insns deleted from their prologues.  */
2567 	      symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2568 			  - symtab_hdr->sh_info);
2569 	      hashes = elf_sym_hashes (input_bfd);
2570 	      end_hashes = hashes + symcount;
2571 	      for (; hashes < end_hashes; hashes++)
2572 		{
2573 		  struct elf32_mn10300_link_hash_entry *sym_hash;
2574 
2575 		  sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2576 		  if ((sym_hash->root.root.type == bfd_link_hash_defined
2577 		       || sym_hash->root.root.type == bfd_link_hash_defweak)
2578 		      && sym_hash->root.root.u.def.section == section
2579 		      && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2580 		      && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2581 		    {
2582 		      int bytes = 0;
2583 		      bfd_vma symval;
2584 
2585 		      /* Note that we've changed things.  */
2586 		      elf_section_data (section)->relocs = internal_relocs;
2587 		      elf_section_data (section)->this_hdr.contents = contents;
2588 		      symtab_hdr->contents = (unsigned char *) isymbuf;
2589 
2590 		      /* Count how many bytes we're going to delete.  */
2591 		      if (sym_hash->movm_args)
2592 			bytes += 2;
2593 
2594 		      if (sym_hash->stack_size > 0)
2595 			{
2596 			  if (sym_hash->stack_size <= 128)
2597 			    bytes += 3;
2598 			  else
2599 			    bytes += 4;
2600 			}
2601 
2602 		      /* Note that we've deleted prologue bytes for this
2603 			 function.  */
2604 		      sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2605 
2606 		      /* Actually delete the bytes.  */
2607 		      symval = sym_hash->root.root.u.def.value;
2608 		      if (!mn10300_elf_relax_delete_bytes (input_bfd,
2609 							   section,
2610 							   symval,
2611 							   bytes))
2612 			goto error_return;
2613 
2614 		      /* Something changed.  Not strictly necessary, but
2615 			 may lead to more relaxing opportunities.  */
2616 		      *again = TRUE;
2617 		    }
2618 		}
2619 
2620 	      /* Cache or free any memory we allocated for the relocs.  */
2621 	      if (internal_relocs != NULL
2622 		  && elf_section_data (section)->relocs != internal_relocs)
2623 		free (internal_relocs);
2624 	      internal_relocs = NULL;
2625 
2626 	      /* Cache or free any memory we allocated for the contents.  */
2627 	      if (contents != NULL
2628 		  && elf_section_data (section)->this_hdr.contents != contents)
2629 		{
2630 		  if (! link_info->keep_memory)
2631 		    free (contents);
2632 		  else
2633 		    /* Cache the section contents for elf_link_input_bfd.  */
2634 		    elf_section_data (section)->this_hdr.contents = contents;
2635 		}
2636 	      contents = NULL;
2637 	    }
2638 
2639 	  /* Cache or free any memory we allocated for the symbols.  */
2640 	  if (isymbuf != NULL
2641 	      && symtab_hdr->contents != (unsigned char *) isymbuf)
2642 	    {
2643 	      if (! link_info->keep_memory)
2644 		free (isymbuf);
2645 	      else
2646 		/* Cache the symbols for elf_link_input_bfd.  */
2647 		symtab_hdr->contents = (unsigned char *) isymbuf;
2648 	    }
2649 	  isymbuf = NULL;
2650 	}
2651     }
2652 
2653   /* (Re)initialize for the basic instruction shortening/relaxing pass.  */
2654   contents = NULL;
2655   internal_relocs = NULL;
2656   isymbuf = NULL;
2657   /* For error_return.  */
2658   section = sec;
2659 
2660   /* We don't have to do anything for a relocatable link, if
2661      this section does not have relocs, or if this is not a
2662      code section.  */
2663   if (link_info->relocatable
2664       || (sec->flags & SEC_RELOC) == 0
2665       || sec->reloc_count == 0
2666       || (sec->flags & SEC_CODE) == 0)
2667     return TRUE;
2668 
2669   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2670 
2671   /* Get a copy of the native relocations.  */
2672   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2673 					       link_info->keep_memory);
2674   if (internal_relocs == NULL)
2675     goto error_return;
2676 
2677   /* Scan for worst case alignment gap changes.  Note that this logic
2678      is not ideal; what we should do is run this scan for every
2679      opcode/address range and adjust accordingly, but that's
2680      expensive.  Worst case is that for an alignment of N bytes, we
2681      move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc
2682      all before it.  Plus, this still doesn't cover cross-section
2683      jumps with section alignment.  */
2684   irelend = internal_relocs + sec->reloc_count;
2685   align_gap_adjustment = 0;
2686   for (irel = internal_relocs; irel < irelend; irel++)
2687     {
2688       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN)
2689 	{
2690 	  bfd_vma adj = 1 << irel->r_addend;
2691 	  bfd_vma aend = irel->r_offset;
2692 
2693 	  aend = BFD_ALIGN (aend, 1 << irel->r_addend);
2694 	  adj = 2 * adj - adj - 1;
2695 
2696 	  /* Record the biggest adjustmnet.  Skip any alignment at the
2697 	     end of our section.  */
2698 	  if (align_gap_adjustment < adj
2699 	      && aend < sec->output_section->vma + sec->output_offset + sec->size)
2700 	    align_gap_adjustment = adj;
2701 	}
2702     }
2703 
2704   /* Walk through them looking for relaxing opportunities.  */
2705   irelend = internal_relocs + sec->reloc_count;
2706   for (irel = internal_relocs; irel < irelend; irel++)
2707     {
2708       bfd_vma symval;
2709       bfd_signed_vma jump_offset;
2710       asection *sym_sec = NULL;
2711       struct elf32_mn10300_link_hash_entry *h = NULL;
2712 
2713       /* If this isn't something that can be relaxed, then ignore
2714 	 this reloc.  */
2715       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
2716 	  || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
2717 	  || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
2718 	continue;
2719 
2720       /* Get the section contents if we haven't done so already.  */
2721       if (contents == NULL)
2722 	{
2723 	  /* Get cached copy if it exists.  */
2724 	  if (elf_section_data (sec)->this_hdr.contents != NULL)
2725 	    contents = elf_section_data (sec)->this_hdr.contents;
2726 	  else
2727 	    {
2728 	      /* Go get them off disk.  */
2729 	      if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2730 		goto error_return;
2731 	    }
2732 	}
2733 
2734       /* Read this BFD's symbols if we haven't done so already.  */
2735       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2736 	{
2737 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2738 	  if (isymbuf == NULL)
2739 	    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2740 					    symtab_hdr->sh_info, 0,
2741 					    NULL, NULL, NULL);
2742 	  if (isymbuf == NULL)
2743 	    goto error_return;
2744 	}
2745 
2746       /* Get the value of the symbol referred to by the reloc.  */
2747       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2748 	{
2749 	  Elf_Internal_Sym *isym;
2750 	  const char *sym_name;
2751 	  char *new_name;
2752 
2753 	  /* A local symbol.  */
2754 	  isym = isymbuf + ELF32_R_SYM (irel->r_info);
2755 	  if (isym->st_shndx == SHN_UNDEF)
2756 	    sym_sec = bfd_und_section_ptr;
2757 	  else if (isym->st_shndx == SHN_ABS)
2758 	    sym_sec = bfd_abs_section_ptr;
2759 	  else if (isym->st_shndx == SHN_COMMON)
2760 	    sym_sec = bfd_com_section_ptr;
2761 	  else
2762 	    sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2763 
2764 	  sym_name = bfd_elf_string_from_elf_section (abfd,
2765 						      symtab_hdr->sh_link,
2766 						      isym->st_name);
2767 
2768 	  if ((sym_sec->flags & SEC_MERGE)
2769 	      && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2770 	    {
2771 	      symval = isym->st_value;
2772 
2773 	      /* GAS may reduce relocations against symbols in SEC_MERGE
2774 		 sections to a relocation against the section symbol when
2775 		 the original addend was zero.  When the reloc is against
2776 		 a section symbol we should include the addend in the
2777 		 offset passed to _bfd_merged_section_offset, since the
2778 		 location of interest is the original symbol.  On the
2779 		 other hand, an access to "sym+addend" where "sym" is not
2780 		 a section symbol should not include the addend;  Such an
2781 		 access is presumed to be an offset from "sym";  The
2782 		 location of interest is just "sym".  */
2783 	      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
2784 		symval += irel->r_addend;
2785 
2786 	      symval = _bfd_merged_section_offset (abfd, & sym_sec,
2787 						   elf_section_data (sym_sec)->sec_info,
2788 						   symval);
2789 
2790 	      if (ELF_ST_TYPE (isym->st_info) != STT_SECTION)
2791 		symval += irel->r_addend;
2792 
2793 	      symval += sym_sec->output_section->vma
2794 		+ sym_sec->output_offset - irel->r_addend;
2795 	    }
2796 	  else
2797 	    symval = (isym->st_value
2798 		      + sym_sec->output_section->vma
2799 		      + sym_sec->output_offset);
2800 
2801 	  /* Tack on an ID so we can uniquely identify this
2802 	     local symbol in the global hash table.  */
2803 	  new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
2804 	  if (new_name == NULL)
2805 	    goto error_return;
2806 	  sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2807 	  sym_name = new_name;
2808 
2809 	  h = (struct elf32_mn10300_link_hash_entry *)
2810 		elf_link_hash_lookup (&hash_table->static_hash_table->root,
2811 				      sym_name, FALSE, FALSE, FALSE);
2812 	  free (new_name);
2813 	}
2814       else
2815 	{
2816 	  unsigned long indx;
2817 
2818 	  /* An external symbol.  */
2819 	  indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2820 	  h = (struct elf32_mn10300_link_hash_entry *)
2821 		(elf_sym_hashes (abfd)[indx]);
2822 	  BFD_ASSERT (h != NULL);
2823 	  if (h->root.root.type != bfd_link_hash_defined
2824 	      && h->root.root.type != bfd_link_hash_defweak)
2825 	    /* This appears to be a reference to an undefined
2826 	       symbol.  Just ignore it--it will be caught by the
2827 	       regular reloc processing.  */
2828 	    continue;
2829 
2830 	  /* Check for a reference to a discarded symbol and ignore it.  */
2831 	  if (h->root.root.u.def.section->output_section == NULL)
2832 	    continue;
2833 
2834 	  sym_sec = h->root.root.u.def.section->output_section;
2835 
2836 	  symval = (h->root.root.u.def.value
2837 		    + h->root.root.u.def.section->output_section->vma
2838 		    + h->root.root.u.def.section->output_offset);
2839 	}
2840 
2841       /* For simplicity of coding, we are going to modify the section
2842 	 contents, the section relocs, and the BFD symbol table.  We
2843 	 must tell the rest of the code not to free up this
2844 	 information.  It would be possible to instead create a table
2845 	 of changes which have to be made, as is done in coff-mips.c;
2846 	 that would be more work, but would require less memory when
2847 	 the linker is run.  */
2848 
2849       /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2850 	 branch/call, also deal with "call" -> "calls" conversions and
2851 	 insertion of prologue data into "call" instructions.  */
2852       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
2853 	  || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
2854 	{
2855 	  bfd_vma value = symval;
2856 
2857 	  if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
2858 	      && h != NULL
2859 	      && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2860 	      && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2861 	      && h->root.plt.offset != (bfd_vma) -1)
2862 	    {
2863 	      asection * splt;
2864 
2865 	      splt = bfd_get_section_by_name (elf_hash_table (link_info)
2866 					      ->dynobj, ".plt");
2867 
2868 	      value = ((splt->output_section->vma
2869 			+ splt->output_offset
2870 			+ h->root.plt.offset)
2871 		       - (sec->output_section->vma
2872 			  + sec->output_offset
2873 			  + irel->r_offset));
2874 	    }
2875 
2876 	  /* If we've got a "call" instruction that needs to be turned
2877 	     into a "calls" instruction, do so now.  It saves a byte.  */
2878 	  if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2879 	    {
2880 	      unsigned char code;
2881 
2882 	      /* Get the opcode.  */
2883 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2884 
2885 	      /* Make sure we're working with a "call" instruction!  */
2886 	      if (code == 0xdd)
2887 		{
2888 		  /* Note that we've changed the relocs, section contents,
2889 		     etc.  */
2890 		  elf_section_data (sec)->relocs = internal_relocs;
2891 		  elf_section_data (sec)->this_hdr.contents = contents;
2892 		  symtab_hdr->contents = (unsigned char *) isymbuf;
2893 
2894 		  /* Fix the opcode.  */
2895 		  bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
2896 		  bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2897 
2898 		  /* Fix irel->r_offset and irel->r_addend.  */
2899 		  irel->r_offset += 1;
2900 		  irel->r_addend += 1;
2901 
2902 		  /* Delete one byte of data.  */
2903 		  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2904 						       irel->r_offset + 3, 1))
2905 		    goto error_return;
2906 
2907 		  /* That will change things, so, we should relax again.
2908 		     Note that this is not required, and it may be slow.  */
2909 		  *again = TRUE;
2910 		}
2911 	    }
2912 	  else if (h)
2913 	    {
2914 	      /* We've got a "call" instruction which needs some data
2915 		 from target function filled in.  */
2916 	      unsigned char code;
2917 
2918 	      /* Get the opcode.  */
2919 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2920 
2921 	      /* Insert data from the target function into the "call"
2922 		 instruction if needed.  */
2923 	      if (code == 0xdd)
2924 		{
2925 		  bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
2926 		  bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2927 			     contents + irel->r_offset + 5);
2928 		}
2929 	    }
2930 
2931 	  /* Deal with pc-relative gunk.  */
2932 	  value -= (sec->output_section->vma + sec->output_offset);
2933 	  value -= irel->r_offset;
2934 	  value += irel->r_addend;
2935 
2936 	  /* See if the value will fit in 16 bits, note the high value is
2937 	     0x7fff + 2 as the target will be two bytes closer if we are
2938 	     able to relax, if it's in the same section.  */
2939 	  if (sec->output_section == sym_sec->output_section)
2940 	    jump_offset = 0x8001;
2941 	  else
2942 	    jump_offset = 0x7fff;
2943 
2944 	  /* Account for jumps across alignment boundaries using
2945 	     align_gap_adjustment.  */
2946 	  if ((bfd_signed_vma) value < jump_offset - (bfd_signed_vma) align_gap_adjustment
2947 	      && ((bfd_signed_vma) value > -0x8000 + (bfd_signed_vma) align_gap_adjustment))
2948 	    {
2949 	      unsigned char code;
2950 
2951 	      /* Get the opcode.  */
2952 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2953 
2954 	      if (code != 0xdc && code != 0xdd && code != 0xff)
2955 		continue;
2956 
2957 	      /* Note that we've changed the relocs, section contents, etc.  */
2958 	      elf_section_data (sec)->relocs = internal_relocs;
2959 	      elf_section_data (sec)->this_hdr.contents = contents;
2960 	      symtab_hdr->contents = (unsigned char *) isymbuf;
2961 
2962 	      /* Fix the opcode.  */
2963 	      if (code == 0xdc)
2964 		bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
2965 	      else if (code == 0xdd)
2966 		bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
2967 	      else if (code == 0xff)
2968 		bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2969 
2970 	      /* Fix the relocation's type.  */
2971 	      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2972 					   (ELF32_R_TYPE (irel->r_info)
2973 					    == (int) R_MN10300_PLT32)
2974 					   ? R_MN10300_PLT16 :
2975 					   R_MN10300_PCREL16);
2976 
2977 	      /* Delete two bytes of data.  */
2978 	      if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2979 						   irel->r_offset + 1, 2))
2980 		goto error_return;
2981 
2982 	      /* That will change things, so, we should relax again.
2983 		 Note that this is not required, and it may be slow.  */
2984 	      *again = TRUE;
2985 	    }
2986 	}
2987 
2988       /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2989 	 branch.  */
2990       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
2991 	{
2992 	  bfd_vma value = symval;
2993 
2994 	  /* If we've got a "call" instruction that needs to be turned
2995 	     into a "calls" instruction, do so now.  It saves a byte.  */
2996 	  if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2997 	    {
2998 	      unsigned char code;
2999 
3000 	      /* Get the opcode.  */
3001 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3002 
3003 	      /* Make sure we're working with a "call" instruction!  */
3004 	      if (code == 0xcd)
3005 		{
3006 		  /* Note that we've changed the relocs, section contents,
3007 		     etc.  */
3008 		  elf_section_data (sec)->relocs = internal_relocs;
3009 		  elf_section_data (sec)->this_hdr.contents = contents;
3010 		  symtab_hdr->contents = (unsigned char *) isymbuf;
3011 
3012 		  /* Fix the opcode.  */
3013 		  bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
3014 		  bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
3015 
3016 		  /* Fix irel->r_offset and irel->r_addend.  */
3017 		  irel->r_offset += 1;
3018 		  irel->r_addend += 1;
3019 
3020 		  /* Delete one byte of data.  */
3021 		  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3022 						       irel->r_offset + 1, 1))
3023 		    goto error_return;
3024 
3025 		  /* That will change things, so, we should relax again.
3026 		     Note that this is not required, and it may be slow.  */
3027 		  *again = TRUE;
3028 		}
3029 	    }
3030 	  else if (h)
3031 	    {
3032 	      unsigned char code;
3033 
3034 	      /* Get the opcode.  */
3035 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3036 
3037 	      /* Insert data from the target function into the "call"
3038 		 instruction if needed.  */
3039 	      if (code == 0xcd)
3040 		{
3041 		  bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
3042 		  bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
3043 			     contents + irel->r_offset + 3);
3044 		}
3045 	    }
3046 
3047 	  /* Deal with pc-relative gunk.  */
3048 	  value -= (sec->output_section->vma + sec->output_offset);
3049 	  value -= irel->r_offset;
3050 	  value += irel->r_addend;
3051 
3052 	  /* See if the value will fit in 8 bits, note the high value is
3053 	     0x7f + 1 as the target will be one bytes closer if we are
3054 	     able to relax.  */
3055 	  if ((long) value < 0x80 && (long) value > -0x80)
3056 	    {
3057 	      unsigned char code;
3058 
3059 	      /* Get the opcode.  */
3060 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3061 
3062 	      if (code != 0xcc)
3063 		continue;
3064 
3065 	      /* Note that we've changed the relocs, section contents, etc.  */
3066 	      elf_section_data (sec)->relocs = internal_relocs;
3067 	      elf_section_data (sec)->this_hdr.contents = contents;
3068 	      symtab_hdr->contents = (unsigned char *) isymbuf;
3069 
3070 	      /* Fix the opcode.  */
3071 	      bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
3072 
3073 	      /* Fix the relocation's type.  */
3074 	      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3075 					   R_MN10300_PCREL8);
3076 
3077 	      /* Delete one byte of data.  */
3078 	      if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3079 						   irel->r_offset + 1, 1))
3080 		goto error_return;
3081 
3082 	      /* That will change things, so, we should relax again.
3083 		 Note that this is not required, and it may be slow.  */
3084 	      *again = TRUE;
3085 	    }
3086 	}
3087 
3088       /* Try to eliminate an unconditional 8 bit pc-relative branch
3089 	 which immediately follows a conditional 8 bit pc-relative
3090 	 branch around the unconditional branch.
3091 
3092 	    original:		new:
3093 	    bCC lab1		bCC' lab2
3094 	    bra lab2
3095 	   lab1:	       lab1:
3096 
3097 	 This happens when the bCC can't reach lab2 at assembly time,
3098 	 but due to other relaxations it can reach at link time.  */
3099       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
3100 	{
3101 	  Elf_Internal_Rela *nrel;
3102 	  bfd_vma value = symval;
3103 	  unsigned char code;
3104 
3105 	  /* Deal with pc-relative gunk.  */
3106 	  value -= (sec->output_section->vma + sec->output_offset);
3107 	  value -= irel->r_offset;
3108 	  value += irel->r_addend;
3109 
3110 	  /* Do nothing if this reloc is the last byte in the section.  */
3111 	  if (irel->r_offset == sec->size)
3112 	    continue;
3113 
3114 	  /* See if the next instruction is an unconditional pc-relative
3115 	     branch, more often than not this test will fail, so we
3116 	     test it first to speed things up.  */
3117 	  code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
3118 	  if (code != 0xca)
3119 	    continue;
3120 
3121 	  /* Also make sure the next relocation applies to the next
3122 	     instruction and that it's a pc-relative 8 bit branch.  */
3123 	  nrel = irel + 1;
3124 	  if (nrel == irelend
3125 	      || irel->r_offset + 2 != nrel->r_offset
3126 	      || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
3127 	    continue;
3128 
3129 	  /* Make sure our destination immediately follows the
3130 	     unconditional branch.  */
3131 	  if (symval != (sec->output_section->vma + sec->output_offset
3132 			 + irel->r_offset + 3))
3133 	    continue;
3134 
3135 	  /* Now make sure we are a conditional branch.  This may not
3136 	     be necessary, but why take the chance.
3137 
3138 	     Note these checks assume that R_MN10300_PCREL8 relocs
3139 	     only occur on bCC and bCCx insns.  If they occured
3140 	     elsewhere, we'd need to know the start of this insn
3141 	     for this check to be accurate.  */
3142 	  code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3143 	  if (code != 0xc0 && code != 0xc1 && code != 0xc2
3144 	      && code != 0xc3 && code != 0xc4 && code != 0xc5
3145 	      && code != 0xc6 && code != 0xc7 && code != 0xc8
3146 	      && code != 0xc9 && code != 0xe8 && code != 0xe9
3147 	      && code != 0xea && code != 0xeb)
3148 	    continue;
3149 
3150 	  /* We also have to be sure there is no symbol/label
3151 	     at the unconditional branch.  */
3152 	  if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
3153 					    irel->r_offset + 1))
3154 	    continue;
3155 
3156 	  /* Note that we've changed the relocs, section contents, etc.  */
3157 	  elf_section_data (sec)->relocs = internal_relocs;
3158 	  elf_section_data (sec)->this_hdr.contents = contents;
3159 	  symtab_hdr->contents = (unsigned char *) isymbuf;
3160 
3161 	  /* Reverse the condition of the first branch.  */
3162 	  switch (code)
3163 	    {
3164 	    case 0xc8:
3165 	      code = 0xc9;
3166 	      break;
3167 	    case 0xc9:
3168 	      code = 0xc8;
3169 	      break;
3170 	    case 0xc0:
3171 	      code = 0xc2;
3172 	      break;
3173 	    case 0xc2:
3174 	      code = 0xc0;
3175 	      break;
3176 	    case 0xc3:
3177 	      code = 0xc1;
3178 	      break;
3179 	    case 0xc1:
3180 	      code = 0xc3;
3181 	      break;
3182 	    case 0xc4:
3183 	      code = 0xc6;
3184 	      break;
3185 	    case 0xc6:
3186 	      code = 0xc4;
3187 	      break;
3188 	    case 0xc7:
3189 	      code = 0xc5;
3190 	      break;
3191 	    case 0xc5:
3192 	      code = 0xc7;
3193 	      break;
3194 	    case 0xe8:
3195 	      code = 0xe9;
3196 	      break;
3197 	    case 0x9d:
3198 	      code = 0xe8;
3199 	      break;
3200 	    case 0xea:
3201 	      code = 0xeb;
3202 	      break;
3203 	    case 0xeb:
3204 	      code = 0xea;
3205 	      break;
3206 	    }
3207 	  bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3208 
3209 	  /* Set the reloc type and symbol for the first branch
3210 	     from the second branch.  */
3211 	  irel->r_info = nrel->r_info;
3212 
3213 	  /* Make the reloc for the second branch a null reloc.  */
3214 	  nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3215 				       R_MN10300_NONE);
3216 
3217 	  /* Delete two bytes of data.  */
3218 	  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3219 					       irel->r_offset + 1, 2))
3220 	    goto error_return;
3221 
3222 	  /* That will change things, so, we should relax again.
3223 	     Note that this is not required, and it may be slow.  */
3224 	  *again = TRUE;
3225 	}
3226 
3227       /* Try to turn a 24 immediate, displacement or absolute address
3228 	 into a 8 immediate, displacement or absolute address.  */
3229       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
3230 	{
3231 	  bfd_vma value = symval;
3232 	  value += irel->r_addend;
3233 
3234 	  /* See if the value will fit in 8 bits.  */
3235 	  if ((long) value < 0x7f && (long) value > -0x80)
3236 	    {
3237 	      unsigned char code;
3238 
3239 	      /* AM33 insns which have 24 operands are 6 bytes long and
3240 		 will have 0xfd as the first byte.  */
3241 
3242 	      /* Get the first opcode.  */
3243 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3244 
3245 	      if (code == 0xfd)
3246 		{
3247 		  /* Get the second opcode.  */
3248 		  code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3249 
3250 		  /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3251 		     equivalent instructions exists.  */
3252 		  if (code != 0x6b && code != 0x7b
3253 		      && code != 0x8b && code != 0x9b
3254 		      && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3255 			  || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3256 			  || (code & 0x0f) == 0x0e))
3257 		    {
3258 		      /* Not safe if the high bit is on as relaxing may
3259 			 move the value out of high mem and thus not fit
3260 			 in a signed 8bit value.  This is currently over
3261 			 conservative.  */
3262 		      if ((value & 0x80) == 0)
3263 			{
3264 			  /* Note that we've changed the relocation contents,
3265 			     etc.  */
3266 			  elf_section_data (sec)->relocs = internal_relocs;
3267 			  elf_section_data (sec)->this_hdr.contents = contents;
3268 			  symtab_hdr->contents = (unsigned char *) isymbuf;
3269 
3270 			  /* Fix the opcode.  */
3271 			  bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
3272 			  bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3273 
3274 			  /* Fix the relocation's type.  */
3275 			  irel->r_info =
3276 			    ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3277 					  R_MN10300_8);
3278 
3279 			  /* Delete two bytes of data.  */
3280 			  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3281 							       irel->r_offset + 1, 2))
3282 			    goto error_return;
3283 
3284 			  /* That will change things, so, we should relax
3285 			     again.  Note that this is not required, and it
3286 			     may be slow.  */
3287 			  *again = TRUE;
3288 			  break;
3289 			}
3290 		    }
3291 		}
3292 	    }
3293 	}
3294 
3295       /* Try to turn a 32bit immediate, displacement or absolute address
3296 	 into a 16bit immediate, displacement or absolute address.  */
3297       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
3298 	  || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
3299 	  || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3300 	{
3301 	  bfd_vma value = symval;
3302 
3303 	  if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
3304 	    {
3305 	      asection * sgot;
3306 
3307 	      sgot = bfd_get_section_by_name (elf_hash_table (link_info)
3308 					      ->dynobj, ".got");
3309 
3310 	      if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
3311 		{
3312 		  value = sgot->output_offset;
3313 
3314 		  if (h)
3315 		    value += h->root.got.offset;
3316 		  else
3317 		    value += (elf_local_got_offsets
3318 			      (abfd)[ELF32_R_SYM (irel->r_info)]);
3319 		}
3320 	      else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3321 		value -= sgot->output_section->vma;
3322 	      else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
3323 		value = (sgot->output_section->vma
3324 			 - (sec->output_section->vma
3325 			    + sec->output_offset
3326 			    + irel->r_offset));
3327 	      else
3328 		abort ();
3329 	    }
3330 
3331 	  value += irel->r_addend;
3332 
3333 	  /* See if the value will fit in 24 bits.
3334 	     We allow any 16bit match here.  We prune those we can't
3335 	     handle below.  */
3336 	  if ((long) value < 0x7fffff && (long) value > -0x800000)
3337 	    {
3338 	      unsigned char code;
3339 
3340 	      /* AM33 insns which have 32bit operands are 7 bytes long and
3341 		 will have 0xfe as the first byte.  */
3342 
3343 	      /* Get the first opcode.  */
3344 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3345 
3346 	      if (code == 0xfe)
3347 		{
3348 		  /* Get the second opcode.  */
3349 		  code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3350 
3351 		  /* All the am33 32 -> 24 relaxing possibilities.  */
3352 		  /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3353 		     equivalent instructions exists.  */
3354 		  if (code != 0x6b && code != 0x7b
3355 		      && code != 0x8b && code != 0x9b
3356 		      && (ELF32_R_TYPE (irel->r_info)
3357 			  != (int) R_MN10300_GOTPC32)
3358 		      && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3359 			  || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3360 			  || (code & 0x0f) == 0x0e))
3361 		    {
3362 		      /* Not safe if the high bit is on as relaxing may
3363 			 move the value out of high mem and thus not fit
3364 			 in a signed 16bit value.  This is currently over
3365 			 conservative.  */
3366 		      if ((value & 0x8000) == 0)
3367 			{
3368 			  /* Note that we've changed the relocation contents,
3369 			     etc.  */
3370 			  elf_section_data (sec)->relocs = internal_relocs;
3371 			  elf_section_data (sec)->this_hdr.contents = contents;
3372 			  symtab_hdr->contents = (unsigned char *) isymbuf;
3373 
3374 			  /* Fix the opcode.  */
3375 			  bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
3376 			  bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3377 
3378 			  /* Fix the relocation's type.  */
3379 			  irel->r_info =
3380 			    ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3381 					  (ELF32_R_TYPE (irel->r_info)
3382 					   == (int) R_MN10300_GOTOFF32)
3383 					  ? R_MN10300_GOTOFF24
3384 					  : (ELF32_R_TYPE (irel->r_info)
3385 					     == (int) R_MN10300_GOT32)
3386 					  ? R_MN10300_GOT24 :
3387 					  R_MN10300_24);
3388 
3389 			  /* Delete one byte of data.  */
3390 			  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3391 							       irel->r_offset + 3, 1))
3392 			    goto error_return;
3393 
3394 			  /* That will change things, so, we should relax
3395 			     again.  Note that this is not required, and it
3396 			     may be slow.  */
3397 			  *again = TRUE;
3398 			  break;
3399 			}
3400 		    }
3401 		}
3402 	    }
3403 
3404 	  /* See if the value will fit in 16 bits.
3405 	     We allow any 16bit match here.  We prune those we can't
3406 	     handle below.  */
3407 	  if ((long) value < 0x7fff && (long) value > -0x8000)
3408 	    {
3409 	      unsigned char code;
3410 
3411 	      /* Most insns which have 32bit operands are 6 bytes long;
3412 		 exceptions are pcrel insns and bit insns.
3413 
3414 		 We handle pcrel insns above.  We don't bother trying
3415 		 to handle the bit insns here.
3416 
3417 		 The first byte of the remaining insns will be 0xfc.  */
3418 
3419 	      /* Get the first opcode.  */
3420 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3421 
3422 	      if (code != 0xfc)
3423 		continue;
3424 
3425 	      /* Get the second opcode.  */
3426 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3427 
3428 	      if ((code & 0xf0) < 0x80)
3429 		switch (code & 0xf0)
3430 		  {
3431 		  /* mov (d32,am),dn   -> mov (d32,am),dn
3432 		     mov dm,(d32,am)   -> mov dn,(d32,am)
3433 		     mov (d32,am),an   -> mov (d32,am),an
3434 		     mov dm,(d32,am)   -> mov dn,(d32,am)
3435 		     movbu (d32,am),dn -> movbu (d32,am),dn
3436 		     movbu dm,(d32,am) -> movbu dn,(d32,am)
3437 		     movhu (d32,am),dn -> movhu (d32,am),dn
3438 		     movhu dm,(d32,am) -> movhu dn,(d32,am) */
3439 		  case 0x00:
3440 		  case 0x10:
3441 		  case 0x20:
3442 		  case 0x30:
3443 		  case 0x40:
3444 		  case 0x50:
3445 		  case 0x60:
3446 		  case 0x70:
3447 		    /* Not safe if the high bit is on as relaxing may
3448 		       move the value out of high mem and thus not fit
3449 		       in a signed 16bit value.  */
3450 		    if (code == 0xcc
3451 			&& (value & 0x8000))
3452 		      continue;
3453 
3454 		    /* Note that we've changed the relocation contents, etc.  */
3455 		    elf_section_data (sec)->relocs = internal_relocs;
3456 		    elf_section_data (sec)->this_hdr.contents = contents;
3457 		    symtab_hdr->contents = (unsigned char *) isymbuf;
3458 
3459 		    /* Fix the opcode.  */
3460 		    bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3461 		    bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3462 
3463 		    /* Fix the relocation's type.  */
3464 		    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3465 						 (ELF32_R_TYPE (irel->r_info)
3466 						  == (int) R_MN10300_GOTOFF32)
3467 						 ? R_MN10300_GOTOFF16
3468 						 : (ELF32_R_TYPE (irel->r_info)
3469 						    == (int) R_MN10300_GOT32)
3470 						 ? R_MN10300_GOT16
3471 						 : (ELF32_R_TYPE (irel->r_info)
3472 						    == (int) R_MN10300_GOTPC32)
3473 						 ? R_MN10300_GOTPC16 :
3474 						 R_MN10300_16);
3475 
3476 		    /* Delete two bytes of data.  */
3477 		    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3478 							 irel->r_offset + 2, 2))
3479 		      goto error_return;
3480 
3481 		    /* That will change things, so, we should relax again.
3482 		       Note that this is not required, and it may be slow.  */
3483 		    *again = TRUE;
3484 		    break;
3485 		  }
3486 	      else if ((code & 0xf0) == 0x80
3487 		       || (code & 0xf0) == 0x90)
3488 		switch (code & 0xf3)
3489 		  {
3490 		  /* mov dn,(abs32)   -> mov dn,(abs16)
3491 		     movbu dn,(abs32) -> movbu dn,(abs16)
3492 		     movhu dn,(abs32) -> movhu dn,(abs16)  */
3493 		  case 0x81:
3494 		  case 0x82:
3495 		  case 0x83:
3496 		    /* Note that we've changed the relocation contents, etc.  */
3497 		    elf_section_data (sec)->relocs = internal_relocs;
3498 		    elf_section_data (sec)->this_hdr.contents = contents;
3499 		    symtab_hdr->contents = (unsigned char *) isymbuf;
3500 
3501 		    if ((code & 0xf3) == 0x81)
3502 		      code = 0x01 + (code & 0x0c);
3503 		    else if ((code & 0xf3) == 0x82)
3504 		      code = 0x02 + (code & 0x0c);
3505 		    else if ((code & 0xf3) == 0x83)
3506 		      code = 0x03 + (code & 0x0c);
3507 		    else
3508 		      abort ();
3509 
3510 		    /* Fix the opcode.  */
3511 		    bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3512 
3513 		    /* Fix the relocation's type.  */
3514 		    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3515 						 (ELF32_R_TYPE (irel->r_info)
3516 						  == (int) R_MN10300_GOTOFF32)
3517 						 ? R_MN10300_GOTOFF16
3518 						 : (ELF32_R_TYPE (irel->r_info)
3519 						    == (int) R_MN10300_GOT32)
3520 						 ? R_MN10300_GOT16
3521 						 : (ELF32_R_TYPE (irel->r_info)
3522 						    == (int) R_MN10300_GOTPC32)
3523 						 ? R_MN10300_GOTPC16 :
3524 						 R_MN10300_16);
3525 
3526 		    /* The opcode got shorter too, so we have to fix the
3527 		       addend and offset too!  */
3528 		    irel->r_offset -= 1;
3529 
3530 		    /* Delete three bytes of data.  */
3531 		    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3532 							 irel->r_offset + 1, 3))
3533 		      goto error_return;
3534 
3535 		    /* That will change things, so, we should relax again.
3536 		       Note that this is not required, and it may be slow.  */
3537 		    *again = TRUE;
3538 		    break;
3539 
3540 		  /* mov am,(abs32)    -> mov am,(abs16)
3541 		     mov am,(d32,sp)   -> mov am,(d16,sp)
3542 		     mov dm,(d32,sp)   -> mov dm,(d32,sp)
3543 		     movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3544 		     movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3545 		  case 0x80:
3546 		  case 0x90:
3547 		  case 0x91:
3548 		  case 0x92:
3549 		  case 0x93:
3550 		    /* sp-based offsets are zero-extended.  */
3551 		    if (code >= 0x90 && code <= 0x93
3552 			&& (long) value < 0)
3553 		      continue;
3554 
3555 		    /* Note that we've changed the relocation contents, etc.  */
3556 		    elf_section_data (sec)->relocs = internal_relocs;
3557 		    elf_section_data (sec)->this_hdr.contents = contents;
3558 		    symtab_hdr->contents = (unsigned char *) isymbuf;
3559 
3560 		    /* Fix the opcode.  */
3561 		    bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3562 		    bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3563 
3564 		    /* Fix the relocation's type.  */
3565 		    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3566 						 (ELF32_R_TYPE (irel->r_info)
3567 						  == (int) R_MN10300_GOTOFF32)
3568 						 ? R_MN10300_GOTOFF16
3569 						 : (ELF32_R_TYPE (irel->r_info)
3570 						    == (int) R_MN10300_GOT32)
3571 						 ? R_MN10300_GOT16
3572 						 : (ELF32_R_TYPE (irel->r_info)
3573 						    == (int) R_MN10300_GOTPC32)
3574 						 ? R_MN10300_GOTPC16 :
3575 						 R_MN10300_16);
3576 
3577 		    /* Delete two bytes of data.  */
3578 		    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3579 							 irel->r_offset + 2, 2))
3580 		      goto error_return;
3581 
3582 		    /* That will change things, so, we should relax again.
3583 		       Note that this is not required, and it may be slow.  */
3584 		    *again = TRUE;
3585 		    break;
3586 		  }
3587 	      else if ((code & 0xf0) < 0xf0)
3588 		switch (code & 0xfc)
3589 		  {
3590 		  /* mov imm32,dn     -> mov imm16,dn
3591 		     mov imm32,an     -> mov imm16,an
3592 		     mov (abs32),dn   -> mov (abs16),dn
3593 		     movbu (abs32),dn -> movbu (abs16),dn
3594 		     movhu (abs32),dn -> movhu (abs16),dn  */
3595 		  case 0xcc:
3596 		  case 0xdc:
3597 		  case 0xa4:
3598 		  case 0xa8:
3599 		  case 0xac:
3600 		    /* Not safe if the high bit is on as relaxing may
3601 		       move the value out of high mem and thus not fit
3602 		       in a signed 16bit value.  */
3603 		    if (code == 0xcc
3604 			&& (value & 0x8000))
3605 		      continue;
3606 
3607 		    /* mov imm16, an zero-extends the immediate.  */
3608 		    if (code == 0xdc
3609 			&& (long) value < 0)
3610 		      continue;
3611 
3612 		    /* Note that we've changed the relocation contents, etc.  */
3613 		    elf_section_data (sec)->relocs = internal_relocs;
3614 		    elf_section_data (sec)->this_hdr.contents = contents;
3615 		    symtab_hdr->contents = (unsigned char *) isymbuf;
3616 
3617 		    if ((code & 0xfc) == 0xcc)
3618 		      code = 0x2c + (code & 0x03);
3619 		    else if ((code & 0xfc) == 0xdc)
3620 		      code = 0x24 + (code & 0x03);
3621 		    else if ((code & 0xfc) == 0xa4)
3622 		      code = 0x30 + (code & 0x03);
3623 		    else if ((code & 0xfc) == 0xa8)
3624 		      code = 0x34 + (code & 0x03);
3625 		    else if ((code & 0xfc) == 0xac)
3626 		      code = 0x38 + (code & 0x03);
3627 		    else
3628 		      abort ();
3629 
3630 		    /* Fix the opcode.  */
3631 		    bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3632 
3633 		    /* Fix the relocation's type.  */
3634 		    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3635 						 (ELF32_R_TYPE (irel->r_info)
3636 						  == (int) R_MN10300_GOTOFF32)
3637 						 ? R_MN10300_GOTOFF16
3638 						 : (ELF32_R_TYPE (irel->r_info)
3639 						    == (int) R_MN10300_GOT32)
3640 						 ? R_MN10300_GOT16
3641 						 : (ELF32_R_TYPE (irel->r_info)
3642 						    == (int) R_MN10300_GOTPC32)
3643 						 ? R_MN10300_GOTPC16 :
3644 						 R_MN10300_16);
3645 
3646 		    /* The opcode got shorter too, so we have to fix the
3647 		       addend and offset too!  */
3648 		    irel->r_offset -= 1;
3649 
3650 		    /* Delete three bytes of data.  */
3651 		    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3652 							 irel->r_offset + 1, 3))
3653 		      goto error_return;
3654 
3655 		    /* That will change things, so, we should relax again.
3656 		       Note that this is not required, and it may be slow.  */
3657 		    *again = TRUE;
3658 		    break;
3659 
3660 		  /* mov (abs32),an    -> mov (abs16),an
3661 		     mov (d32,sp),an   -> mov (d16,sp),an
3662 		     mov (d32,sp),dn   -> mov (d16,sp),dn
3663 		     movbu (d32,sp),dn -> movbu (d16,sp),dn
3664 		     movhu (d32,sp),dn -> movhu (d16,sp),dn
3665 		     add imm32,dn      -> add imm16,dn
3666 		     cmp imm32,dn      -> cmp imm16,dn
3667 		     add imm32,an      -> add imm16,an
3668 		     cmp imm32,an      -> cmp imm16,an
3669 		     and imm32,dn      -> and imm16,dn
3670 		     or imm32,dn       -> or imm16,dn
3671 		     xor imm32,dn      -> xor imm16,dn
3672 		     btst imm32,dn     -> btst imm16,dn */
3673 
3674 		  case 0xa0:
3675 		  case 0xb0:
3676 		  case 0xb1:
3677 		  case 0xb2:
3678 		  case 0xb3:
3679 		  case 0xc0:
3680 		  case 0xc8:
3681 
3682 		  case 0xd0:
3683 		  case 0xd8:
3684 		  case 0xe0:
3685 		  case 0xe1:
3686 		  case 0xe2:
3687 		  case 0xe3:
3688 		    /* cmp imm16, an zero-extends the immediate.  */
3689 		    if (code == 0xdc
3690 			&& (long) value < 0)
3691 		      continue;
3692 
3693 		    /* So do sp-based offsets.  */
3694 		    if (code >= 0xb0 && code <= 0xb3
3695 			&& (long) value < 0)
3696 		      continue;
3697 
3698 		    /* Note that we've changed the relocation contents, etc.  */
3699 		    elf_section_data (sec)->relocs = internal_relocs;
3700 		    elf_section_data (sec)->this_hdr.contents = contents;
3701 		    symtab_hdr->contents = (unsigned char *) isymbuf;
3702 
3703 		    /* Fix the opcode.  */
3704 		    bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3705 		    bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3706 
3707 		    /* Fix the relocation's type.  */
3708 		    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3709 						 (ELF32_R_TYPE (irel->r_info)
3710 						  == (int) R_MN10300_GOTOFF32)
3711 						 ? R_MN10300_GOTOFF16
3712 						 : (ELF32_R_TYPE (irel->r_info)
3713 						    == (int) R_MN10300_GOT32)
3714 						 ? R_MN10300_GOT16
3715 						 : (ELF32_R_TYPE (irel->r_info)
3716 						    == (int) R_MN10300_GOTPC32)
3717 						 ? R_MN10300_GOTPC16 :
3718 						 R_MN10300_16);
3719 
3720 		    /* Delete two bytes of data.  */
3721 		    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3722 							 irel->r_offset + 2, 2))
3723 		      goto error_return;
3724 
3725 		    /* That will change things, so, we should relax again.
3726 		       Note that this is not required, and it may be slow.  */
3727 		    *again = TRUE;
3728 		    break;
3729 		  }
3730 	      else if (code == 0xfe)
3731 		{
3732 		  /* add imm32,sp -> add imm16,sp  */
3733 
3734 		  /* Note that we've changed the relocation contents, etc.  */
3735 		  elf_section_data (sec)->relocs = internal_relocs;
3736 		  elf_section_data (sec)->this_hdr.contents = contents;
3737 		  symtab_hdr->contents = (unsigned char *) isymbuf;
3738 
3739 		  /* Fix the opcode.  */
3740 		  bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3741 		  bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
3742 
3743 		  /* Fix the relocation's type.  */
3744 		  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3745 					       (ELF32_R_TYPE (irel->r_info)
3746 						== (int) R_MN10300_GOT32)
3747 					       ? R_MN10300_GOT16
3748 					       : (ELF32_R_TYPE (irel->r_info)
3749 						  == (int) R_MN10300_GOTOFF32)
3750 					       ? R_MN10300_GOTOFF16
3751 					       : (ELF32_R_TYPE (irel->r_info)
3752 						  == (int) R_MN10300_GOTPC32)
3753 					       ? R_MN10300_GOTPC16 :
3754 					       R_MN10300_16);
3755 
3756 		  /* Delete two bytes of data.  */
3757 		  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3758 						       irel->r_offset + 2, 2))
3759 		    goto error_return;
3760 
3761 		  /* That will change things, so, we should relax again.
3762 		     Note that this is not required, and it may be slow.  */
3763 		  *again = TRUE;
3764 		  break;
3765 		}
3766 	    }
3767 	}
3768     }
3769 
3770   if (isymbuf != NULL
3771       && symtab_hdr->contents != (unsigned char *) isymbuf)
3772     {
3773       if (! link_info->keep_memory)
3774 	free (isymbuf);
3775       else
3776 	{
3777 	  /* Cache the symbols for elf_link_input_bfd.  */
3778 	  symtab_hdr->contents = (unsigned char *) isymbuf;
3779 	}
3780     }
3781 
3782   if (contents != NULL
3783       && elf_section_data (sec)->this_hdr.contents != contents)
3784     {
3785       if (! link_info->keep_memory)
3786 	free (contents);
3787       else
3788 	{
3789 	  /* Cache the section contents for elf_link_input_bfd.  */
3790 	  elf_section_data (sec)->this_hdr.contents = contents;
3791 	}
3792     }
3793 
3794   if (internal_relocs != NULL
3795       && elf_section_data (sec)->relocs != internal_relocs)
3796     free (internal_relocs);
3797 
3798   return TRUE;
3799 
3800  error_return:
3801   if (isymbuf != NULL
3802       && symtab_hdr->contents != (unsigned char *) isymbuf)
3803     free (isymbuf);
3804   if (contents != NULL
3805       && elf_section_data (section)->this_hdr.contents != contents)
3806     free (contents);
3807   if (internal_relocs != NULL
3808       && elf_section_data (section)->relocs != internal_relocs)
3809     free (internal_relocs);
3810 
3811   return FALSE;
3812 }
3813 
3814 /* This is a version of bfd_generic_get_relocated_section_contents
3815    which uses mn10300_elf_relocate_section.  */
3816 
3817 static bfd_byte *
3818 mn10300_elf_get_relocated_section_contents (bfd *output_bfd,
3819 					    struct bfd_link_info *link_info,
3820 					    struct bfd_link_order *link_order,
3821 					    bfd_byte *data,
3822 					    bfd_boolean relocatable,
3823 					    asymbol **symbols)
3824 {
3825   Elf_Internal_Shdr *symtab_hdr;
3826   asection *input_section = link_order->u.indirect.section;
3827   bfd *input_bfd = input_section->owner;
3828   asection **sections = NULL;
3829   Elf_Internal_Rela *internal_relocs = NULL;
3830   Elf_Internal_Sym *isymbuf = NULL;
3831 
3832   /* We only need to handle the case of relaxing, or of having a
3833      particular set of section contents, specially.  */
3834   if (relocatable
3835       || elf_section_data (input_section)->this_hdr.contents == NULL)
3836     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3837 						       link_order, data,
3838 						       relocatable,
3839 						       symbols);
3840 
3841   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3842 
3843   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3844 	  (size_t) input_section->size);
3845 
3846   if ((input_section->flags & SEC_RELOC) != 0
3847       && input_section->reloc_count > 0)
3848     {
3849       asection **secpp;
3850       Elf_Internal_Sym *isym, *isymend;
3851       bfd_size_type amt;
3852 
3853       internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
3854 						   NULL, NULL, FALSE);
3855       if (internal_relocs == NULL)
3856 	goto error_return;
3857 
3858       if (symtab_hdr->sh_info != 0)
3859 	{
3860 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3861 	  if (isymbuf == NULL)
3862 	    isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3863 					    symtab_hdr->sh_info, 0,
3864 					    NULL, NULL, NULL);
3865 	  if (isymbuf == NULL)
3866 	    goto error_return;
3867 	}
3868 
3869       amt = symtab_hdr->sh_info;
3870       amt *= sizeof (asection *);
3871       sections = bfd_malloc (amt);
3872       if (sections == NULL && amt != 0)
3873 	goto error_return;
3874 
3875       isymend = isymbuf + symtab_hdr->sh_info;
3876       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3877 	{
3878 	  asection *isec;
3879 
3880 	  if (isym->st_shndx == SHN_UNDEF)
3881 	    isec = bfd_und_section_ptr;
3882 	  else if (isym->st_shndx == SHN_ABS)
3883 	    isec = bfd_abs_section_ptr;
3884 	  else if (isym->st_shndx == SHN_COMMON)
3885 	    isec = bfd_com_section_ptr;
3886 	  else
3887 	    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3888 
3889 	  *secpp = isec;
3890 	}
3891 
3892       if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
3893 					  input_section, data, internal_relocs,
3894 					  isymbuf, sections))
3895 	goto error_return;
3896 
3897       if (sections != NULL)
3898 	free (sections);
3899       if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3900 	free (isymbuf);
3901       if (internal_relocs != elf_section_data (input_section)->relocs)
3902 	free (internal_relocs);
3903     }
3904 
3905   return data;
3906 
3907  error_return:
3908   if (sections != NULL)
3909     free (sections);
3910   if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3911     free (isymbuf);
3912   if (internal_relocs != NULL
3913       && internal_relocs != elf_section_data (input_section)->relocs)
3914     free (internal_relocs);
3915   return NULL;
3916 }
3917 
3918 /* Assorted hash table functions.  */
3919 
3920 /* Initialize an entry in the link hash table.  */
3921 
3922 /* Create an entry in an MN10300 ELF linker hash table.  */
3923 
3924 static struct bfd_hash_entry *
3925 elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry *entry,
3926 				 struct bfd_hash_table *table,
3927 				 const char *string)
3928 {
3929   struct elf32_mn10300_link_hash_entry *ret =
3930     (struct elf32_mn10300_link_hash_entry *) entry;
3931 
3932   /* Allocate the structure if it has not already been allocated by a
3933      subclass.  */
3934   if (ret == NULL)
3935     ret = (struct elf32_mn10300_link_hash_entry *)
3936 	   bfd_hash_allocate (table, sizeof (* ret));
3937   if (ret == NULL)
3938     return (struct bfd_hash_entry *) ret;
3939 
3940   /* Call the allocation method of the superclass.  */
3941   ret = (struct elf32_mn10300_link_hash_entry *)
3942 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3943 				     table, string);
3944   if (ret != NULL)
3945     {
3946       ret->direct_calls = 0;
3947       ret->stack_size = 0;
3948       ret->movm_args = 0;
3949       ret->movm_stack_size = 0;
3950       ret->flags = 0;
3951       ret->value = 0;
3952     }
3953 
3954   return (struct bfd_hash_entry *) ret;
3955 }
3956 
3957 /* Create an mn10300 ELF linker hash table.  */
3958 
3959 static struct bfd_link_hash_table *
3960 elf32_mn10300_link_hash_table_create (bfd *abfd)
3961 {
3962   struct elf32_mn10300_link_hash_table *ret;
3963   bfd_size_type amt = sizeof (* ret);
3964 
3965   ret = bfd_malloc (amt);
3966   if (ret == NULL)
3967     return NULL;
3968 
3969   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3970 				      elf32_mn10300_link_hash_newfunc,
3971 				      sizeof (struct elf32_mn10300_link_hash_entry),
3972 				      MN10300_ELF_DATA))
3973     {
3974       free (ret);
3975       return NULL;
3976     }
3977 
3978   ret->flags = 0;
3979   amt = sizeof (struct elf_link_hash_table);
3980   ret->static_hash_table = bfd_malloc (amt);
3981   if (ret->static_hash_table == NULL)
3982     {
3983       free (ret);
3984       return NULL;
3985     }
3986 
3987   if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
3988 				      elf32_mn10300_link_hash_newfunc,
3989 				      sizeof (struct elf32_mn10300_link_hash_entry),
3990 				      MN10300_ELF_DATA))
3991     {
3992       free (ret->static_hash_table);
3993       free (ret);
3994       return NULL;
3995     }
3996   return & ret->root.root;
3997 }
3998 
3999 /* Free an mn10300 ELF linker hash table.  */
4000 
4001 static void
4002 elf32_mn10300_link_hash_table_free (struct bfd_link_hash_table *hash)
4003 {
4004   struct elf32_mn10300_link_hash_table *ret
4005     = (struct elf32_mn10300_link_hash_table *) hash;
4006 
4007   _bfd_generic_link_hash_table_free
4008     ((struct bfd_link_hash_table *) ret->static_hash_table);
4009   _bfd_generic_link_hash_table_free
4010     ((struct bfd_link_hash_table *) ret);
4011 }
4012 
4013 static unsigned long
4014 elf_mn10300_mach (flagword flags)
4015 {
4016   switch (flags & EF_MN10300_MACH)
4017     {
4018     case E_MN10300_MACH_MN10300:
4019     default:
4020       return bfd_mach_mn10300;
4021 
4022     case E_MN10300_MACH_AM33:
4023       return bfd_mach_am33;
4024 
4025     case E_MN10300_MACH_AM33_2:
4026       return bfd_mach_am33_2;
4027     }
4028 }
4029 
4030 /* The final processing done just before writing out a MN10300 ELF object
4031    file.  This gets the MN10300 architecture right based on the machine
4032    number.  */
4033 
4034 static void
4035 _bfd_mn10300_elf_final_write_processing (bfd *abfd,
4036 					 bfd_boolean linker ATTRIBUTE_UNUSED)
4037 {
4038   unsigned long val;
4039 
4040   switch (bfd_get_mach (abfd))
4041     {
4042     default:
4043     case bfd_mach_mn10300:
4044       val = E_MN10300_MACH_MN10300;
4045       break;
4046 
4047     case bfd_mach_am33:
4048       val = E_MN10300_MACH_AM33;
4049       break;
4050 
4051     case bfd_mach_am33_2:
4052       val = E_MN10300_MACH_AM33_2;
4053       break;
4054     }
4055 
4056   elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
4057   elf_elfheader (abfd)->e_flags |= val;
4058 }
4059 
4060 static bfd_boolean
4061 _bfd_mn10300_elf_object_p (bfd *abfd)
4062 {
4063   bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
4064 			     elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
4065   return TRUE;
4066 }
4067 
4068 /* Merge backend specific data from an object file to the output
4069    object file when linking.  */
4070 
4071 static bfd_boolean
4072 _bfd_mn10300_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4073 {
4074   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4075       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4076     return TRUE;
4077 
4078   if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4079       && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
4080     {
4081       if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4082 			       bfd_get_mach (ibfd)))
4083 	return FALSE;
4084     }
4085 
4086   return TRUE;
4087 }
4088 
4089 #define PLT0_ENTRY_SIZE     15
4090 #define PLT_ENTRY_SIZE      20
4091 #define PIC_PLT_ENTRY_SIZE  24
4092 
4093 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
4094 {
4095   0xfc, 0xa0, 0, 0, 0, 0,	/* mov	(.got+8),a0 */
4096   0xfe, 0xe, 0x10, 0, 0, 0, 0,	/* mov	(.got+4),r1 */
4097   0xf0, 0xf4,			/* jmp	(a0) */
4098 };
4099 
4100 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
4101 {
4102   0xfc, 0xa0, 0, 0, 0, 0,	/* mov	(nameN@GOT + .got),a0 */
4103   0xf0, 0xf4,			/* jmp	(a0) */
4104   0xfe, 8, 0, 0, 0, 0, 0,	/* mov	reloc-table-address,r0 */
4105   0xdc, 0, 0, 0, 0,		/* jmp	.plt0 */
4106 };
4107 
4108 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
4109 {
4110   0xfc, 0x22, 0, 0, 0, 0,	/* mov	(nameN@GOT,a2),a0 */
4111   0xf0, 0xf4,			/* jmp	(a0) */
4112   0xfe, 8, 0, 0, 0, 0, 0,	/* mov	reloc-table-address,r0 */
4113   0xf8, 0x22, 8,		/* mov	(8,a2),a0 */
4114   0xfb, 0xa, 0x1a, 4,		/* mov	(4,a2),r1 */
4115   0xf0, 0xf4,			/* jmp	(a0) */
4116 };
4117 
4118 /* Return size of the first PLT entry.  */
4119 #define elf_mn10300_sizeof_plt0(info) \
4120   (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4121 
4122 /* Return size of a PLT entry.  */
4123 #define elf_mn10300_sizeof_plt(info) \
4124   (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4125 
4126 /* Return offset of the PLT0 address in an absolute PLT entry.  */
4127 #define elf_mn10300_plt_plt0_offset(info) 16
4128 
4129 /* Return offset of the linker in PLT0 entry.  */
4130 #define elf_mn10300_plt0_linker_offset(info) 2
4131 
4132 /* Return offset of the GOT id in PLT0 entry.  */
4133 #define elf_mn10300_plt0_gotid_offset(info) 9
4134 
4135 /* Return offset of the temporary in PLT entry.  */
4136 #define elf_mn10300_plt_temp_offset(info) 8
4137 
4138 /* Return offset of the symbol in PLT entry.  */
4139 #define elf_mn10300_plt_symbol_offset(info) 2
4140 
4141 /* Return offset of the relocation in PLT entry.  */
4142 #define elf_mn10300_plt_reloc_offset(info) 11
4143 
4144 /* The name of the dynamic interpreter.  This is put in the .interp
4145    section.  */
4146 
4147 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4148 
4149 /* Create dynamic sections when linking against a dynamic object.  */
4150 
4151 static bfd_boolean
4152 _bfd_mn10300_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4153 {
4154   flagword   flags;
4155   asection * s;
4156   const struct elf_backend_data * bed = get_elf_backend_data (abfd);
4157   int ptralign = 0;
4158 
4159   switch (bed->s->arch_size)
4160     {
4161     case 32:
4162       ptralign = 2;
4163       break;
4164 
4165     case 64:
4166       ptralign = 3;
4167       break;
4168 
4169     default:
4170       bfd_set_error (bfd_error_bad_value);
4171       return FALSE;
4172     }
4173 
4174   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4175      .rel[a].bss sections.  */
4176   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4177 	   | SEC_LINKER_CREATED);
4178 
4179   s = bfd_make_section_with_flags (abfd,
4180 				   (bed->default_use_rela_p
4181 				    ? ".rela.plt" : ".rel.plt"),
4182 				   flags | SEC_READONLY);
4183   if (s == NULL
4184       || ! bfd_set_section_alignment (abfd, s, ptralign))
4185     return FALSE;
4186 
4187   if (! _bfd_mn10300_elf_create_got_section (abfd, info))
4188     return FALSE;
4189 
4190   {
4191     const char * secname;
4192     char *       relname;
4193     flagword     secflags;
4194     asection *   sec;
4195 
4196     for (sec = abfd->sections; sec; sec = sec->next)
4197       {
4198 	secflags = bfd_get_section_flags (abfd, sec);
4199 	if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
4200 	    || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
4201 	  continue;
4202 
4203 	secname = bfd_get_section_name (abfd, sec);
4204 	relname = bfd_malloc (strlen (secname) + 6);
4205 	strcpy (relname, ".rela");
4206 	strcat (relname, secname);
4207 
4208 	s = bfd_make_section_with_flags (abfd, relname,
4209 					 flags | SEC_READONLY);
4210 	if (s == NULL
4211 	    || ! bfd_set_section_alignment (abfd, s, ptralign))
4212 	  return FALSE;
4213       }
4214   }
4215 
4216   if (bed->want_dynbss)
4217     {
4218       /* The .dynbss section is a place to put symbols which are defined
4219 	 by dynamic objects, are referenced by regular objects, and are
4220 	 not functions.  We must allocate space for them in the process
4221 	 image and use a R_*_COPY reloc to tell the dynamic linker to
4222 	 initialize them at run time.  The linker script puts the .dynbss
4223 	 section into the .bss section of the final image.  */
4224       s = bfd_make_section_with_flags (abfd, ".dynbss",
4225 				       SEC_ALLOC | SEC_LINKER_CREATED);
4226       if (s == NULL)
4227 	return FALSE;
4228 
4229       /* The .rel[a].bss section holds copy relocs.  This section is not
4230 	 normally needed.  We need to create it here, though, so that the
4231 	 linker will map it to an output section.  We can't just create it
4232 	 only if we need it, because we will not know whether we need it
4233 	 until we have seen all the input files, and the first time the
4234 	 main linker code calls BFD after examining all the input files
4235 	 (size_dynamic_sections) the input sections have already been
4236 	 mapped to the output sections.  If the section turns out not to
4237 	 be needed, we can discard it later.  We will never need this
4238 	 section when generating a shared object, since they do not use
4239 	 copy relocs.  */
4240       if (! info->shared)
4241 	{
4242 	  s = bfd_make_section_with_flags (abfd,
4243 					   (bed->default_use_rela_p
4244 					    ? ".rela.bss" : ".rel.bss"),
4245 					   flags | SEC_READONLY);
4246 	  if (s == NULL
4247 	      || ! bfd_set_section_alignment (abfd, s, ptralign))
4248 	    return FALSE;
4249 	}
4250     }
4251 
4252   return TRUE;
4253 }
4254 
4255 /* Adjust a symbol defined by a dynamic object and referenced by a
4256    regular object.  The current definition is in some section of the
4257    dynamic object, but we're not including those sections.  We have to
4258    change the definition to something the rest of the link can
4259    understand.  */
4260 
4261 static bfd_boolean
4262 _bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
4263 					struct elf_link_hash_entry * h)
4264 {
4265   bfd * dynobj;
4266   asection * s;
4267 
4268   dynobj = elf_hash_table (info)->dynobj;
4269 
4270   /* Make sure we know what is going on here.  */
4271   BFD_ASSERT (dynobj != NULL
4272 	      && (h->needs_plt
4273 		  || h->u.weakdef != NULL
4274 		  || (h->def_dynamic
4275 		      && h->ref_regular
4276 		      && !h->def_regular)));
4277 
4278   /* If this is a function, put it in the procedure linkage table.  We
4279      will fill in the contents of the procedure linkage table later,
4280      when we know the address of the .got section.  */
4281   if (h->type == STT_FUNC
4282       || h->needs_plt)
4283     {
4284       if (! info->shared
4285 	  && !h->def_dynamic
4286 	  && !h->ref_dynamic)
4287 	{
4288 	  /* This case can occur if we saw a PLT reloc in an input
4289 	     file, but the symbol was never referred to by a dynamic
4290 	     object.  In such a case, we don't actually need to build
4291 	     a procedure linkage table, and we can just do a REL32
4292 	     reloc instead.  */
4293 	  BFD_ASSERT (h->needs_plt);
4294 	  return TRUE;
4295 	}
4296 
4297       /* Make sure this symbol is output as a dynamic symbol.  */
4298       if (h->dynindx == -1)
4299 	{
4300 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
4301 	    return FALSE;
4302 	}
4303 
4304       s = bfd_get_section_by_name (dynobj, ".plt");
4305       BFD_ASSERT (s != NULL);
4306 
4307       /* If this is the first .plt entry, make room for the special
4308 	 first entry.  */
4309       if (s->size == 0)
4310 	s->size += elf_mn10300_sizeof_plt0 (info);
4311 
4312       /* If this symbol is not defined in a regular file, and we are
4313 	 not generating a shared library, then set the symbol to this
4314 	 location in the .plt.  This is required to make function
4315 	 pointers compare as equal between the normal executable and
4316 	 the shared library.  */
4317       if (! info->shared
4318 	  && !h->def_regular)
4319 	{
4320 	  h->root.u.def.section = s;
4321 	  h->root.u.def.value = s->size;
4322 	}
4323 
4324       h->plt.offset = s->size;
4325 
4326       /* Make room for this entry.  */
4327       s->size += elf_mn10300_sizeof_plt (info);
4328 
4329       /* We also need to make an entry in the .got.plt section, which
4330 	 will be placed in the .got section by the linker script.  */
4331       s = bfd_get_section_by_name (dynobj, ".got.plt");
4332       BFD_ASSERT (s != NULL);
4333       s->size += 4;
4334 
4335       /* We also need to make an entry in the .rela.plt section.  */
4336       s = bfd_get_section_by_name (dynobj, ".rela.plt");
4337       BFD_ASSERT (s != NULL);
4338       s->size += sizeof (Elf32_External_Rela);
4339 
4340       return TRUE;
4341     }
4342 
4343   /* If this is a weak symbol, and there is a real definition, the
4344      processor independent code will have arranged for us to see the
4345      real definition first, and we can just use the same value.  */
4346   if (h->u.weakdef != NULL)
4347     {
4348       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4349 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
4350       h->root.u.def.section = h->u.weakdef->root.u.def.section;
4351       h->root.u.def.value = h->u.weakdef->root.u.def.value;
4352       return TRUE;
4353     }
4354 
4355   /* This is a reference to a symbol defined by a dynamic object which
4356      is not a function.  */
4357 
4358   /* If we are creating a shared library, we must presume that the
4359      only references to the symbol are via the global offset table.
4360      For such cases we need not do anything here; the relocations will
4361      be handled correctly by relocate_section.  */
4362   if (info->shared)
4363     return TRUE;
4364 
4365   /* If there are no references to this symbol that do not use the
4366      GOT, we don't need to generate a copy reloc.  */
4367   if (!h->non_got_ref)
4368     return TRUE;
4369 
4370   if (h->size == 0)
4371     {
4372       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
4373 			     h->root.root.string);
4374       return TRUE;
4375     }
4376 
4377   /* We must allocate the symbol in our .dynbss section, which will
4378      become part of the .bss section of the executable.  There will be
4379      an entry for this symbol in the .dynsym section.  The dynamic
4380      object will contain position independent code, so all references
4381      from the dynamic object to this symbol will go through the global
4382      offset table.  The dynamic linker will use the .dynsym entry to
4383      determine the address it must put in the global offset table, so
4384      both the dynamic object and the regular object will refer to the
4385      same memory location for the variable.  */
4386 
4387   s = bfd_get_section_by_name (dynobj, ".dynbss");
4388   BFD_ASSERT (s != NULL);
4389 
4390   /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4391      copy the initial value out of the dynamic object and into the
4392      runtime process image.  We need to remember the offset into the
4393      .rela.bss section we are going to use.  */
4394   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4395     {
4396       asection * srel;
4397 
4398       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
4399       BFD_ASSERT (srel != NULL);
4400       srel->size += sizeof (Elf32_External_Rela);
4401       h->needs_copy = 1;
4402     }
4403 
4404   return _bfd_elf_adjust_dynamic_copy (h, s);
4405 }
4406 
4407 /* Set the sizes of the dynamic sections.  */
4408 
4409 static bfd_boolean
4410 _bfd_mn10300_elf_size_dynamic_sections (bfd * output_bfd,
4411 					struct bfd_link_info * info)
4412 {
4413   bfd * dynobj;
4414   asection * s;
4415   bfd_boolean plt;
4416   bfd_boolean relocs;
4417   bfd_boolean reltext;
4418 
4419   dynobj = elf_hash_table (info)->dynobj;
4420   BFD_ASSERT (dynobj != NULL);
4421 
4422   if (elf_hash_table (info)->dynamic_sections_created)
4423     {
4424       /* Set the contents of the .interp section to the interpreter.  */
4425       if (info->executable)
4426 	{
4427 	  s = bfd_get_section_by_name (dynobj, ".interp");
4428 	  BFD_ASSERT (s != NULL);
4429 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4430 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4431 	}
4432     }
4433   else
4434     {
4435       /* We may have created entries in the .rela.got section.
4436 	 However, if we are not creating the dynamic sections, we will
4437 	 not actually use these entries.  Reset the size of .rela.got,
4438 	 which will cause it to get stripped from the output file
4439 	 below.  */
4440       s = bfd_get_section_by_name (dynobj, ".rela.got");
4441       if (s != NULL)
4442 	s->size = 0;
4443     }
4444 
4445   /* The check_relocs and adjust_dynamic_symbol entry points have
4446      determined the sizes of the various dynamic sections.  Allocate
4447      memory for them.  */
4448   plt = FALSE;
4449   relocs = FALSE;
4450   reltext = FALSE;
4451   for (s = dynobj->sections; s != NULL; s = s->next)
4452     {
4453       const char * name;
4454 
4455       if ((s->flags & SEC_LINKER_CREATED) == 0)
4456 	continue;
4457 
4458       /* It's OK to base decisions on the section name, because none
4459 	 of the dynobj section names depend upon the input files.  */
4460       name = bfd_get_section_name (dynobj, s);
4461 
4462       if (streq (name, ".plt"))
4463 	{
4464 	  /* Remember whether there is a PLT.  */
4465 	  plt = s->size != 0;
4466 	}
4467       else if (CONST_STRNEQ (name, ".rela"))
4468 	{
4469 	  if (s->size != 0)
4470 	    {
4471 	      asection * target;
4472 
4473 	      /* Remember whether there are any reloc sections other
4474 		 than .rela.plt.  */
4475 	      if (! streq (name, ".rela.plt"))
4476 		{
4477 		  const char * outname;
4478 
4479 		  relocs = TRUE;
4480 
4481 		  /* If this relocation section applies to a read only
4482 		     section, then we probably need a DT_TEXTREL
4483 		     entry.  The entries in the .rela.plt section
4484 		     really apply to the .got section, which we
4485 		     created ourselves and so know is not readonly.  */
4486 		  outname = bfd_get_section_name (output_bfd,
4487 						  s->output_section);
4488 		  target = bfd_get_section_by_name (output_bfd, outname + 5);
4489 		  if (target != NULL
4490 		      && (target->flags & SEC_READONLY) != 0
4491 		      && (target->flags & SEC_ALLOC) != 0)
4492 		    reltext = TRUE;
4493 		}
4494 
4495 	      /* We use the reloc_count field as a counter if we need
4496 		 to copy relocs into the output file.  */
4497 	      s->reloc_count = 0;
4498 	    }
4499 	}
4500       else if (! CONST_STRNEQ (name, ".got")
4501 	       && ! streq (name, ".dynbss"))
4502 	/* It's not one of our sections, so don't allocate space.  */
4503 	continue;
4504 
4505       if (s->size == 0)
4506 	{
4507 	  /* If we don't need this section, strip it from the
4508 	     output file.  This is mostly to handle .rela.bss and
4509 	     .rela.plt.  We must create both sections in
4510 	     create_dynamic_sections, because they must be created
4511 	     before the linker maps input sections to output
4512 	     sections.  The linker does that before
4513 	     adjust_dynamic_symbol is called, and it is that
4514 	     function which decides whether anything needs to go
4515 	     into these sections.  */
4516 	  s->flags |= SEC_EXCLUDE;
4517 	  continue;
4518 	}
4519 
4520 	if ((s->flags & SEC_HAS_CONTENTS) == 0)
4521 	  continue;
4522 
4523       /* Allocate memory for the section contents.  We use bfd_zalloc
4524 	 here in case unused entries are not reclaimed before the
4525 	 section's contents are written out.  This should not happen,
4526 	 but this way if it does, we get a R_MN10300_NONE reloc
4527 	 instead of garbage.  */
4528       s->contents = bfd_zalloc (dynobj, s->size);
4529       if (s->contents == NULL)
4530 	return FALSE;
4531     }
4532 
4533   if (elf_hash_table (info)->dynamic_sections_created)
4534     {
4535       /* Add some entries to the .dynamic section.  We fill in the
4536 	 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4537 	 but we must add the entries now so that we get the correct
4538 	 size for the .dynamic section.  The DT_DEBUG entry is filled
4539 	 in by the dynamic linker and used by the debugger.  */
4540       if (! info->shared)
4541 	{
4542 	  if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
4543 	    return FALSE;
4544 	}
4545 
4546       if (plt)
4547 	{
4548 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
4549 	      || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
4550 	      || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
4551 	      || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
4552 	    return FALSE;
4553 	}
4554 
4555       if (relocs)
4556 	{
4557 	  if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
4558 	      || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
4559 	      || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
4560 					      sizeof (Elf32_External_Rela)))
4561 	    return FALSE;
4562 	}
4563 
4564       if (reltext)
4565 	{
4566 	  if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
4567 	    return FALSE;
4568 	}
4569     }
4570 
4571   return TRUE;
4572 }
4573 
4574 /* Finish up dynamic symbol handling.  We set the contents of various
4575    dynamic sections here.  */
4576 
4577 static bfd_boolean
4578 _bfd_mn10300_elf_finish_dynamic_symbol (bfd * output_bfd,
4579 					struct bfd_link_info * info,
4580 					struct elf_link_hash_entry * h,
4581 					Elf_Internal_Sym * sym)
4582 {
4583   bfd * dynobj;
4584 
4585   dynobj = elf_hash_table (info)->dynobj;
4586 
4587   if (h->plt.offset != (bfd_vma) -1)
4588     {
4589       asection *        splt;
4590       asection *        sgot;
4591       asection *        srel;
4592       bfd_vma           plt_index;
4593       bfd_vma           got_offset;
4594       Elf_Internal_Rela rel;
4595 
4596       /* This symbol has an entry in the procedure linkage table.  Set
4597 	 it up.  */
4598 
4599       BFD_ASSERT (h->dynindx != -1);
4600 
4601       splt = bfd_get_section_by_name (dynobj, ".plt");
4602       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4603       srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4604       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4605 
4606       /* Get the index in the procedure linkage table which
4607 	 corresponds to this symbol.  This is the index of this symbol
4608 	 in all the symbols for which we are making plt entries.  The
4609 	 first entry in the procedure linkage table is reserved.  */
4610       plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
4611 		   / elf_mn10300_sizeof_plt (info));
4612 
4613       /* Get the offset into the .got table of the entry that
4614 	 corresponds to this function.  Each .got entry is 4 bytes.
4615 	 The first three are reserved.  */
4616       got_offset = (plt_index + 3) * 4;
4617 
4618       /* Fill in the entry in the procedure linkage table.  */
4619       if (! info->shared)
4620 	{
4621 	  memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
4622 		  elf_mn10300_sizeof_plt (info));
4623 	  bfd_put_32 (output_bfd,
4624 		      (sgot->output_section->vma
4625 		       + sgot->output_offset
4626 		       + got_offset),
4627 		      (splt->contents + h->plt.offset
4628 		       + elf_mn10300_plt_symbol_offset (info)));
4629 
4630 	  bfd_put_32 (output_bfd,
4631 		      (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
4632 		      (splt->contents + h->plt.offset
4633 		       + elf_mn10300_plt_plt0_offset (info)));
4634 	}
4635       else
4636 	{
4637 	  memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
4638 		  elf_mn10300_sizeof_plt (info));
4639 
4640 	  bfd_put_32 (output_bfd, got_offset,
4641 		      (splt->contents + h->plt.offset
4642 		       + elf_mn10300_plt_symbol_offset (info)));
4643 	}
4644 
4645       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4646 		  (splt->contents + h->plt.offset
4647 		   + elf_mn10300_plt_reloc_offset (info)));
4648 
4649       /* Fill in the entry in the global offset table.  */
4650       bfd_put_32 (output_bfd,
4651 		  (splt->output_section->vma
4652 		   + splt->output_offset
4653 		   + h->plt.offset
4654 		   + elf_mn10300_plt_temp_offset (info)),
4655 		  sgot->contents + got_offset);
4656 
4657       /* Fill in the entry in the .rela.plt section.  */
4658       rel.r_offset = (sgot->output_section->vma
4659 		      + sgot->output_offset
4660 		      + got_offset);
4661       rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
4662       rel.r_addend = 0;
4663       bfd_elf32_swap_reloca_out (output_bfd, &rel,
4664 				 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4665 					       + plt_index));
4666 
4667       if (!h->def_regular)
4668 	/* Mark the symbol as undefined, rather than as defined in
4669 	   the .plt section.  Leave the value alone.  */
4670 	sym->st_shndx = SHN_UNDEF;
4671     }
4672 
4673   if (h->got.offset != (bfd_vma) -1)
4674     {
4675       asection *        sgot;
4676       asection *        srel;
4677       Elf_Internal_Rela rel;
4678 
4679       /* This symbol has an entry in the global offset table.  Set it up.  */
4680       sgot = bfd_get_section_by_name (dynobj, ".got");
4681       srel = bfd_get_section_by_name (dynobj, ".rela.got");
4682       BFD_ASSERT (sgot != NULL && srel != NULL);
4683 
4684       rel.r_offset = (sgot->output_section->vma
4685 		      + sgot->output_offset
4686 		      + (h->got.offset & ~1));
4687 
4688       /* If this is a -Bsymbolic link, and the symbol is defined
4689 	 locally, we just want to emit a RELATIVE reloc.  Likewise if
4690 	 the symbol was forced to be local because of a version file.
4691 	 The entry in the global offset table will already have been
4692 	 initialized in the relocate_section function.  */
4693       if (info->shared
4694 	  && (info->symbolic || h->dynindx == -1)
4695 	  && h->def_regular)
4696 	{
4697 	  rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
4698 	  rel.r_addend = (h->root.u.def.value
4699 			  + h->root.u.def.section->output_section->vma
4700 			  + h->root.u.def.section->output_offset);
4701 	}
4702       else
4703 	{
4704 	  bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4705 	  rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
4706 	  rel.r_addend = 0;
4707 	}
4708 
4709       bfd_elf32_swap_reloca_out (output_bfd, &rel,
4710 				 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4711 					       + srel->reloc_count));
4712       ++ srel->reloc_count;
4713     }
4714 
4715   if (h->needs_copy)
4716     {
4717       asection *        s;
4718       Elf_Internal_Rela rel;
4719 
4720       /* This symbol needs a copy reloc.  Set it up.  */
4721       BFD_ASSERT (h->dynindx != -1
4722 		  && (h->root.type == bfd_link_hash_defined
4723 		      || h->root.type == bfd_link_hash_defweak));
4724 
4725       s = bfd_get_section_by_name (h->root.u.def.section->owner,
4726 				   ".rela.bss");
4727       BFD_ASSERT (s != NULL);
4728 
4729       rel.r_offset = (h->root.u.def.value
4730 		      + h->root.u.def.section->output_section->vma
4731 		      + h->root.u.def.section->output_offset);
4732       rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
4733       rel.r_addend = 0;
4734       bfd_elf32_swap_reloca_out (output_bfd, & rel,
4735 				 (bfd_byte *) ((Elf32_External_Rela *) s->contents
4736 					       + s->reloc_count));
4737       ++ s->reloc_count;
4738     }
4739 
4740   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
4741   if (streq (h->root.root.string, "_DYNAMIC")
4742       || h == elf_hash_table (info)->hgot)
4743     sym->st_shndx = SHN_ABS;
4744 
4745   return TRUE;
4746 }
4747 
4748 /* Finish up the dynamic sections.  */
4749 
4750 static bfd_boolean
4751 _bfd_mn10300_elf_finish_dynamic_sections (bfd * output_bfd,
4752 					  struct bfd_link_info * info)
4753 {
4754   bfd *      dynobj;
4755   asection * sgot;
4756   asection * sdyn;
4757 
4758   dynobj = elf_hash_table (info)->dynobj;
4759 
4760   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4761   BFD_ASSERT (sgot != NULL);
4762   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4763 
4764   if (elf_hash_table (info)->dynamic_sections_created)
4765     {
4766       asection *           splt;
4767       Elf32_External_Dyn * dyncon;
4768       Elf32_External_Dyn * dynconend;
4769 
4770       BFD_ASSERT (sdyn != NULL);
4771 
4772       dyncon = (Elf32_External_Dyn *) sdyn->contents;
4773       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4774 
4775       for (; dyncon < dynconend; dyncon++)
4776 	{
4777 	  Elf_Internal_Dyn dyn;
4778 	  const char * name;
4779 	  asection * s;
4780 
4781 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4782 
4783 	  switch (dyn.d_tag)
4784 	    {
4785 	    default:
4786 	      break;
4787 
4788 	    case DT_PLTGOT:
4789 	      name = ".got";
4790 	      goto get_vma;
4791 
4792 	    case DT_JMPREL:
4793 	      name = ".rela.plt";
4794 	    get_vma:
4795 	      s = bfd_get_section_by_name (output_bfd, name);
4796 	      BFD_ASSERT (s != NULL);
4797 	      dyn.d_un.d_ptr = s->vma;
4798 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4799 	      break;
4800 
4801 	    case DT_PLTRELSZ:
4802 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4803 	      BFD_ASSERT (s != NULL);
4804 	      dyn.d_un.d_val = s->size;
4805 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4806 	      break;
4807 
4808 	    case DT_RELASZ:
4809 	      /* My reading of the SVR4 ABI indicates that the
4810 		 procedure linkage table relocs (DT_JMPREL) should be
4811 		 included in the overall relocs (DT_RELA).  This is
4812 		 what Solaris does.  However, UnixWare can not handle
4813 		 that case.  Therefore, we override the DT_RELASZ entry
4814 		 here to make it not include the JMPREL relocs.  Since
4815 		 the linker script arranges for .rela.plt to follow all
4816 		 other relocation sections, we don't have to worry
4817 		 about changing the DT_RELA entry.  */
4818 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4819 	      if (s != NULL)
4820 		dyn.d_un.d_val -= s->size;
4821 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4822 	      break;
4823 	    }
4824 	}
4825 
4826       /* Fill in the first entry in the procedure linkage table.  */
4827       splt = bfd_get_section_by_name (dynobj, ".plt");
4828       if (splt && splt->size > 0)
4829 	{
4830 	  if (info->shared)
4831 	    {
4832 	      memcpy (splt->contents, elf_mn10300_pic_plt_entry,
4833 		      elf_mn10300_sizeof_plt (info));
4834 	    }
4835 	  else
4836 	    {
4837 	      memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
4838 	      bfd_put_32 (output_bfd,
4839 			  sgot->output_section->vma + sgot->output_offset + 4,
4840 			  splt->contents + elf_mn10300_plt0_gotid_offset (info));
4841 	      bfd_put_32 (output_bfd,
4842 			  sgot->output_section->vma + sgot->output_offset + 8,
4843 			  splt->contents + elf_mn10300_plt0_linker_offset (info));
4844 	    }
4845 
4846 	  /* UnixWare sets the entsize of .plt to 4, although that doesn't
4847 	     really seem like the right value.  */
4848 	  elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4849 	}
4850     }
4851 
4852   /* Fill in the first three entries in the global offset table.  */
4853   if (sgot->size > 0)
4854     {
4855       if (sdyn == NULL)
4856 	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4857       else
4858 	bfd_put_32 (output_bfd,
4859 		    sdyn->output_section->vma + sdyn->output_offset,
4860 		    sgot->contents);
4861       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4862       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4863     }
4864 
4865   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4866 
4867   return TRUE;
4868 }
4869 
4870 /* Classify relocation types, such that combreloc can sort them
4871    properly.  */
4872 
4873 static enum elf_reloc_type_class
4874 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
4875 {
4876   switch ((int) ELF32_R_TYPE (rela->r_info))
4877     {
4878     case R_MN10300_RELATIVE:	return reloc_class_relative;
4879     case R_MN10300_JMP_SLOT:	return reloc_class_plt;
4880     case R_MN10300_COPY:	return reloc_class_copy;
4881     default:			return reloc_class_normal;
4882     }
4883 }
4884 
4885 #ifndef ELF_ARCH
4886 #define TARGET_LITTLE_SYM	bfd_elf32_mn10300_vec
4887 #define TARGET_LITTLE_NAME	"elf32-mn10300"
4888 #define ELF_ARCH		bfd_arch_mn10300
4889 #define ELF_TARGET_ID		MN10300_ELF_DATA
4890 #define ELF_MACHINE_CODE	EM_MN10300
4891 #define ELF_MACHINE_ALT1	EM_CYGNUS_MN10300
4892 #define ELF_MAXPAGESIZE		0x1000
4893 #endif
4894 
4895 #define elf_info_to_howto		mn10300_info_to_howto
4896 #define elf_info_to_howto_rel		0
4897 #define elf_backend_can_gc_sections	1
4898 #define elf_backend_rela_normal		1
4899 #define elf_backend_check_relocs	mn10300_elf_check_relocs
4900 #define elf_backend_gc_mark_hook	mn10300_elf_gc_mark_hook
4901 #define elf_backend_relocate_section	mn10300_elf_relocate_section
4902 #define bfd_elf32_bfd_relax_section	mn10300_elf_relax_section
4903 #define bfd_elf32_bfd_get_relocated_section_contents \
4904 				mn10300_elf_get_relocated_section_contents
4905 #define bfd_elf32_bfd_link_hash_table_create \
4906 				elf32_mn10300_link_hash_table_create
4907 #define bfd_elf32_bfd_link_hash_table_free \
4908 				elf32_mn10300_link_hash_table_free
4909 
4910 #ifndef elf_symbol_leading_char
4911 #define elf_symbol_leading_char '_'
4912 #endif
4913 
4914 /* So we can set bits in e_flags.  */
4915 #define elf_backend_final_write_processing \
4916 					_bfd_mn10300_elf_final_write_processing
4917 #define elf_backend_object_p		_bfd_mn10300_elf_object_p
4918 
4919 #define bfd_elf32_bfd_merge_private_bfd_data \
4920 					_bfd_mn10300_elf_merge_private_bfd_data
4921 
4922 #define elf_backend_can_gc_sections	1
4923 #define elf_backend_create_dynamic_sections \
4924   _bfd_mn10300_elf_create_dynamic_sections
4925 #define elf_backend_adjust_dynamic_symbol \
4926   _bfd_mn10300_elf_adjust_dynamic_symbol
4927 #define elf_backend_size_dynamic_sections \
4928   _bfd_mn10300_elf_size_dynamic_sections
4929 #define elf_backend_omit_section_dynsym \
4930   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4931 #define elf_backend_finish_dynamic_symbol \
4932   _bfd_mn10300_elf_finish_dynamic_symbol
4933 #define elf_backend_finish_dynamic_sections \
4934   _bfd_mn10300_elf_finish_dynamic_sections
4935 
4936 #define elf_backend_reloc_type_class \
4937   _bfd_mn10300_elf_reloc_type_class
4938 
4939 #define elf_backend_want_got_plt	1
4940 #define elf_backend_plt_readonly	1
4941 #define elf_backend_want_plt_sym	0
4942 #define elf_backend_got_header_size	12
4943 
4944 #include "elf32-target.h"
4945