OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gdb-7.2/] [bfd/] [elf-m10300.c] - Blame information for rev 835

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 330 jeremybenn
/* 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
        {
1514
          /* For relocs against symbols from removed linkonce sections,
1515
             or sections discarded by a linker script, we just want the
1516
             section contents zeroed.  Avoid any special processing.  */
1517
          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1518
          rel->r_info = 0;
1519
          rel->r_addend = 0;
1520
          continue;
1521
        }
1522
 
1523
      if (info->relocatable)
1524
        continue;
1525
 
1526
      r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1527
                                           input_section,
1528
                                           contents, rel->r_offset,
1529
                                           relocation, rel->r_addend,
1530
                                           (struct elf_link_hash_entry *) h,
1531
                                           r_symndx,
1532
                                           info, sec, h == NULL);
1533
 
1534
      if (r != bfd_reloc_ok)
1535
        {
1536
          const char *name;
1537
          const char *msg = NULL;
1538
 
1539
          if (h != NULL)
1540
            name = h->root.root.root.string;
1541
          else
1542
            {
1543
              name = (bfd_elf_string_from_elf_section
1544
                      (input_bfd, symtab_hdr->sh_link, sym->st_name));
1545
              if (name == NULL || *name == '\0')
1546
                name = bfd_section_name (input_bfd, sec);
1547
            }
1548
 
1549
          switch (r)
1550
            {
1551
            case bfd_reloc_overflow:
1552
              if (! ((*info->callbacks->reloc_overflow)
1553
                     (info, (h ? &h->root.root : NULL), name,
1554
                      howto->name, (bfd_vma) 0, input_bfd,
1555
                      input_section, rel->r_offset)))
1556
                return FALSE;
1557
              break;
1558
 
1559
            case bfd_reloc_undefined:
1560
              if (! ((*info->callbacks->undefined_symbol)
1561
                     (info, name, input_bfd, input_section,
1562
                      rel->r_offset, TRUE)))
1563
                return FALSE;
1564
              break;
1565
 
1566
            case bfd_reloc_outofrange:
1567
              msg = _("internal error: out of range error");
1568
              goto common_error;
1569
 
1570
            case bfd_reloc_notsupported:
1571
              msg = _("internal error: unsupported relocation error");
1572
              goto common_error;
1573
 
1574
            case bfd_reloc_dangerous:
1575
              if (r_type == R_MN10300_PCREL32)
1576
                msg = _("error: inappropriate relocation type for shared"
1577
                        " library (did you forget -fpic?)");
1578
              else
1579
                msg = _("internal error: suspicious relocation type used"
1580
                        " in shared library");
1581
              goto common_error;
1582
 
1583
            default:
1584
              msg = _("internal error: unknown error");
1585
              /* Fall through.  */
1586
 
1587
            common_error:
1588
              if (!((*info->callbacks->warning)
1589
                    (info, msg, name, input_bfd, input_section,
1590
                     rel->r_offset)))
1591
                return FALSE;
1592
              break;
1593
            }
1594
        }
1595
    }
1596
 
1597
  return TRUE;
1598
}
1599
 
1600
/* Finish initializing one hash table entry.  */
1601
 
1602
static bfd_boolean
1603
elf32_mn10300_finish_hash_table_entry (struct bfd_hash_entry *gen_entry,
1604
                                       void * in_args)
1605
{
1606
  struct elf32_mn10300_link_hash_entry *entry;
1607
  struct bfd_link_info *link_info = (struct bfd_link_info *) in_args;
1608
  unsigned int byte_count = 0;
1609
 
1610
  entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
1611
 
1612
  if (entry->root.root.type == bfd_link_hash_warning)
1613
    entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
1614
 
1615
  /* If we already know we want to convert "call" to "calls" for calls
1616
     to this symbol, then return now.  */
1617
  if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
1618
    return TRUE;
1619
 
1620
  /* If there are no named calls to this symbol, or there's nothing we
1621
     can move from the function itself into the "call" instruction,
1622
     then note that all "call" instructions should be converted into
1623
     "calls" instructions and return.  If a symbol is available for
1624
     dynamic symbol resolution (overridable or overriding), avoid
1625
     custom calling conventions.  */
1626
  if (entry->direct_calls == 0
1627
      || (entry->stack_size == 0 && entry->movm_args == 0)
1628
      || (elf_hash_table (link_info)->dynamic_sections_created
1629
          && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
1630
          && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
1631
    {
1632
      /* Make a note that we should convert "call" instructions to "calls"
1633
         instructions for calls to this symbol.  */
1634
      entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1635
      return TRUE;
1636
    }
1637
 
1638
  /* We may be able to move some instructions from the function itself into
1639
     the "call" instruction.  Count how many bytes we might be able to
1640
     eliminate in the function itself.  */
1641
 
1642
  /* A movm instruction is two bytes.  */
1643
  if (entry->movm_args)
1644
    byte_count += 2;
1645
 
1646
  /* Count the insn to allocate stack space too.  */
1647
  if (entry->stack_size > 0)
1648
    {
1649
      if (entry->stack_size <= 128)
1650
        byte_count += 3;
1651
      else
1652
        byte_count += 4;
1653
    }
1654
 
1655
  /* If using "call" will result in larger code, then turn all
1656
     the associated "call" instructions into "calls" instructions.  */
1657
  if (byte_count < entry->direct_calls)
1658
    entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1659
 
1660
  /* This routine never fails.  */
1661
  return TRUE;
1662
}
1663
 
1664
/* Used to count hash table entries.  */
1665
 
1666
static bfd_boolean
1667
elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry *gen_entry ATTRIBUTE_UNUSED,
1668
                                        void * in_args)
1669
{
1670
  int *count = (int *) in_args;
1671
 
1672
  (*count) ++;
1673
  return TRUE;
1674
}
1675
 
1676
/* Used to enumerate hash table entries into a linear array.  */
1677
 
1678
static bfd_boolean
1679
elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry *gen_entry,
1680
                                       void * in_args)
1681
{
1682
  struct bfd_hash_entry ***ptr = (struct bfd_hash_entry ***) in_args;
1683
 
1684
  **ptr = gen_entry;
1685
  (*ptr) ++;
1686
  return TRUE;
1687
}
1688
 
1689
/* Used to sort the array created by the above.  */
1690
 
1691
static int
1692
sort_by_value (const void *va, const void *vb)
1693
{
1694
  struct elf32_mn10300_link_hash_entry *a
1695
    = *(struct elf32_mn10300_link_hash_entry **) va;
1696
  struct elf32_mn10300_link_hash_entry *b
1697
    = *(struct elf32_mn10300_link_hash_entry **) vb;
1698
 
1699
  return a->value - b->value;
1700
}
1701
 
1702
/* Compute the stack size and movm arguments for the function
1703
   referred to by HASH at address ADDR in section with
1704
   contents CONTENTS, store the information in the hash table.  */
1705
 
1706
static void
1707
compute_function_info (bfd *abfd,
1708
                       struct elf32_mn10300_link_hash_entry *hash,
1709
                       bfd_vma addr,
1710
                       unsigned char *contents)
1711
{
1712
  unsigned char byte1, byte2;
1713
  /* We only care about a very small subset of the possible prologue
1714
     sequences here.  Basically we look for:
1715
 
1716
     movm [d2,d3,a2,a3],sp (optional)
1717
     add <size>,sp (optional, and only for sizes which fit in an unsigned
1718
                    8 bit number)
1719
 
1720
     If we find anything else, we quit.  */
1721
 
1722
  /* Look for movm [regs],sp.  */
1723
  byte1 = bfd_get_8 (abfd, contents + addr);
1724
  byte2 = bfd_get_8 (abfd, contents + addr + 1);
1725
 
1726
  if (byte1 == 0xcf)
1727
    {
1728
      hash->movm_args = byte2;
1729
      addr += 2;
1730
      byte1 = bfd_get_8 (abfd, contents + addr);
1731
      byte2 = bfd_get_8 (abfd, contents + addr + 1);
1732
    }
1733
 
1734
  /* Now figure out how much stack space will be allocated by the movm
1735
     instruction.  We need this kept separate from the function's normal
1736
     stack space.  */
1737
  if (hash->movm_args)
1738
    {
1739
      /* Space for d2.  */
1740
      if (hash->movm_args & 0x80)
1741
        hash->movm_stack_size += 4;
1742
 
1743
      /* Space for d3.  */
1744
      if (hash->movm_args & 0x40)
1745
        hash->movm_stack_size += 4;
1746
 
1747
      /* Space for a2.  */
1748
      if (hash->movm_args & 0x20)
1749
        hash->movm_stack_size += 4;
1750
 
1751
      /* Space for a3.  */
1752
      if (hash->movm_args & 0x10)
1753
        hash->movm_stack_size += 4;
1754
 
1755
      /* "other" space.  d0, d1, a0, a1, mdr, lir, lar, 4 byte pad.  */
1756
      if (hash->movm_args & 0x08)
1757
        hash->movm_stack_size += 8 * 4;
1758
 
1759
      if (bfd_get_mach (abfd) == bfd_mach_am33
1760
          || bfd_get_mach (abfd) == bfd_mach_am33_2)
1761
        {
1762
          /* "exother" space.  e0, e1, mdrq, mcrh, mcrl, mcvf */
1763
          if (hash->movm_args & 0x1)
1764
            hash->movm_stack_size += 6 * 4;
1765
 
1766
          /* exreg1 space.  e4, e5, e6, e7 */
1767
          if (hash->movm_args & 0x2)
1768
            hash->movm_stack_size += 4 * 4;
1769
 
1770
          /* exreg0 space.  e2, e3  */
1771
          if (hash->movm_args & 0x4)
1772
            hash->movm_stack_size += 2 * 4;
1773
        }
1774
    }
1775
 
1776
  /* Now look for the two stack adjustment variants.  */
1777
  if (byte1 == 0xf8 && byte2 == 0xfe)
1778
    {
1779
      int temp = bfd_get_8 (abfd, contents + addr + 2);
1780
      temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
1781
 
1782
      hash->stack_size = -temp;
1783
    }
1784
  else if (byte1 == 0xfa && byte2 == 0xfe)
1785
    {
1786
      int temp = bfd_get_16 (abfd, contents + addr + 2);
1787
      temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
1788
      temp = -temp;
1789
 
1790
      if (temp < 255)
1791
        hash->stack_size = temp;
1792
    }
1793
 
1794
  /* If the total stack to be allocated by the call instruction is more
1795
     than 255 bytes, then we can't remove the stack adjustment by using
1796
     "call" (we might still be able to remove the "movm" instruction.  */
1797
  if (hash->stack_size + hash->movm_stack_size > 255)
1798
    hash->stack_size = 0;
1799
}
1800
 
1801
/* Delete some bytes from a section while relaxing.  */
1802
 
1803
static bfd_boolean
1804
mn10300_elf_relax_delete_bytes (bfd *abfd,
1805
                                asection *sec,
1806
                                bfd_vma addr,
1807
                                int count)
1808
{
1809
  Elf_Internal_Shdr *symtab_hdr;
1810
  unsigned int sec_shndx;
1811
  bfd_byte *contents;
1812
  Elf_Internal_Rela *irel, *irelend;
1813
  Elf_Internal_Rela *irelalign;
1814
  bfd_vma toaddr;
1815
  Elf_Internal_Sym *isym, *isymend;
1816
  struct elf_link_hash_entry **sym_hashes;
1817
  struct elf_link_hash_entry **end_hashes;
1818
  unsigned int symcount;
1819
 
1820
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1821
 
1822
  contents = elf_section_data (sec)->this_hdr.contents;
1823
 
1824
  irelalign = NULL;
1825
  toaddr = sec->size;
1826
 
1827
  irel = elf_section_data (sec)->relocs;
1828
  irelend = irel + sec->reloc_count;
1829
 
1830
  if (sec->reloc_count > 0)
1831
    {
1832
      /* If there is an align reloc at the end of the section ignore it.
1833
         GAS creates these relocs for reasons of its own, and they just
1834
         serve to keep the section artifically inflated.  */
1835
      if (ELF32_R_TYPE ((irelend - 1)->r_info) == (int) R_MN10300_ALIGN)
1836
        --irelend;
1837
 
1838
      /* The deletion must stop at the next ALIGN reloc for an aligment
1839
         power larger than, or not a multiple of, the number of bytes we
1840
         are deleting.  */
1841
      for (; irel < irelend; irel++)
1842
        {
1843
          int alignment = 1 << irel->r_addend;
1844
 
1845
          if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
1846
              && irel->r_offset > addr
1847
              && irel->r_offset < toaddr
1848
              && (count < alignment
1849
                  || alignment % count != 0))
1850
            {
1851
              irelalign = irel;
1852
              toaddr = irel->r_offset;
1853
              break;
1854
            }
1855
        }
1856
    }
1857
 
1858
  /* Actually delete the bytes.  */
1859
  memmove (contents + addr, contents + addr + count,
1860
           (size_t) (toaddr - addr - count));
1861
 
1862
  /* Adjust the section's size if we are shrinking it, or else
1863
     pad the bytes between the end of the shrunken region and
1864
     the start of the next region with NOP codes.  */
1865
  if (irelalign == NULL)
1866
    {
1867
      sec->size -= count;
1868
      /* Include symbols at the end of the section, but
1869
         not at the end of a sub-region of the section.  */
1870
      toaddr ++;
1871
    }
1872
  else
1873
    {
1874
      int i;
1875
 
1876
#define NOP_OPCODE 0xcb
1877
 
1878
      for (i = 0; i < count; i ++)
1879
        bfd_put_8 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
1880
    }
1881
 
1882
  /* Adjust all the relocs.  */
1883
  for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1884
    {
1885
      /* Get the new reloc address.  */
1886
      if ((irel->r_offset > addr
1887
           && irel->r_offset < toaddr)
1888
          || (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
1889
              && irel->r_offset == toaddr))
1890
        irel->r_offset -= count;
1891
    }
1892
 
1893
  /* Adjust the local symbols in the section, reducing their value
1894
     by the number of bytes deleted.  Note - symbols within the deleted
1895
     region are moved to the address of the start of the region, which
1896
     actually means that they will address the byte beyond the end of
1897
     the region once the deletion has been completed.  */
1898
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1899
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1900
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1901
    {
1902
      if (isym->st_shndx == sec_shndx
1903
          && isym->st_value > addr
1904
          && isym->st_value < toaddr)
1905
        {
1906
          if (isym->st_value < addr + count)
1907
            isym->st_value = addr;
1908
          else
1909
            isym->st_value -= count;
1910
        }
1911
      /* Adjust the function symbol's size as well.  */
1912
      else if (isym->st_shndx == sec_shndx
1913
               && ELF_ST_TYPE (isym->st_info) == STT_FUNC
1914
               && isym->st_value + isym->st_size > addr
1915
               && isym->st_value + isym->st_size < toaddr)
1916
        isym->st_size -= count;
1917
    }
1918
 
1919
  /* Now adjust the global symbols defined in this section.  */
1920
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1921
              - symtab_hdr->sh_info);
1922
  sym_hashes = elf_sym_hashes (abfd);
1923
  end_hashes = sym_hashes + symcount;
1924
  for (; sym_hashes < end_hashes; sym_hashes++)
1925
    {
1926
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
1927
 
1928
      if ((sym_hash->root.type == bfd_link_hash_defined
1929
           || sym_hash->root.type == bfd_link_hash_defweak)
1930
          && sym_hash->root.u.def.section == sec
1931
          && sym_hash->root.u.def.value > addr
1932
          && sym_hash->root.u.def.value < toaddr)
1933
        {
1934
          if (sym_hash->root.u.def.value < addr + count)
1935
            sym_hash->root.u.def.value = addr;
1936
          else
1937
            sym_hash->root.u.def.value -= count;
1938
        }
1939
      /* Adjust the function symbol's size as well.  */
1940
      else if (sym_hash->root.type == bfd_link_hash_defined
1941
               && sym_hash->root.u.def.section == sec
1942
               && sym_hash->type == STT_FUNC
1943
               && sym_hash->root.u.def.value + sym_hash->size > addr
1944
               && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1945
        sym_hash->size -= count;
1946
    }
1947
 
1948
  /* See if we can move the ALIGN reloc forward.
1949
     We have adjusted r_offset for it already.  */
1950
  if (irelalign != NULL)
1951
    {
1952
      bfd_vma alignto, alignaddr;
1953
 
1954
      if ((int) irelalign->r_addend > 0)
1955
        {
1956
          /* This is the old address.  */
1957
          alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1958
          /* This is where the align points to now.  */
1959
          alignaddr = BFD_ALIGN (irelalign->r_offset,
1960
                                 1 << irelalign->r_addend);
1961
          if (alignaddr < alignto)
1962
            /* Tail recursion.  */
1963
            return mn10300_elf_relax_delete_bytes (abfd, sec, alignaddr,
1964
                                                   (int) (alignto - alignaddr));
1965
        }
1966
    }
1967
 
1968
  return TRUE;
1969
}
1970
 
1971
/* Return TRUE if a symbol exists at the given address, else return
1972
   FALSE.  */
1973
 
1974
static bfd_boolean
1975
mn10300_elf_symbol_address_p (bfd *abfd,
1976
                              asection *sec,
1977
                              Elf_Internal_Sym *isym,
1978
                              bfd_vma addr)
1979
{
1980
  Elf_Internal_Shdr *symtab_hdr;
1981
  unsigned int sec_shndx;
1982
  Elf_Internal_Sym *isymend;
1983
  struct elf_link_hash_entry **sym_hashes;
1984
  struct elf_link_hash_entry **end_hashes;
1985
  unsigned int symcount;
1986
 
1987
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1988
 
1989
  /* Examine all the symbols.  */
1990
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1991
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1992
    if (isym->st_shndx == sec_shndx
1993
        && isym->st_value == addr)
1994
      return TRUE;
1995
 
1996
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1997
              - symtab_hdr->sh_info);
1998
  sym_hashes = elf_sym_hashes (abfd);
1999
  end_hashes = sym_hashes + symcount;
2000
  for (; sym_hashes < end_hashes; sym_hashes++)
2001
    {
2002
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
2003
 
2004
      if ((sym_hash->root.type == bfd_link_hash_defined
2005
           || sym_hash->root.type == bfd_link_hash_defweak)
2006
          && sym_hash->root.u.def.section == sec
2007
          && sym_hash->root.u.def.value == addr)
2008
        return TRUE;
2009
    }
2010
 
2011
  return FALSE;
2012
}
2013
 
2014
/* This function handles relaxing for the mn10300.
2015
 
2016
   There are quite a few relaxing opportunities available on the mn10300:
2017
 
2018
        * calls:32 -> calls:16                                     2 bytes
2019
        * call:32  -> call:16                                      2 bytes
2020
 
2021
        * call:32 -> calls:32                                      1 byte
2022
        * call:16 -> calls:16                                      1 byte
2023
                * These are done anytime using "calls" would result
2024
                in smaller code, or when necessary to preserve the
2025
                meaning of the program.
2026
 
2027
        * call:32                                                  varies
2028
        * call:16
2029
                * In some circumstances we can move instructions
2030
                from a function prologue into a "call" instruction.
2031
                This is only done if the resulting code is no larger
2032
                than the original code.
2033
 
2034
        * jmp:32 -> jmp:16                                         2 bytes
2035
        * jmp:16 -> bra:8                                          1 byte
2036
 
2037
                * If the previous instruction is a conditional branch
2038
                around the jump/bra, we may be able to reverse its condition
2039
                and change its target to the jump's target.  The jump/bra
2040
                can then be deleted.                               2 bytes
2041
 
2042
        * mov abs32 -> mov abs16                                   1 or 2 bytes
2043
 
2044
        * Most instructions which accept imm32 can relax to imm16  1 or 2 bytes
2045
        - Most instructions which accept imm16 can relax to imm8   1 or 2 bytes
2046
 
2047
        * Most instructions which accept d32 can relax to d16      1 or 2 bytes
2048
        - Most instructions which accept d16 can relax to d8       1 or 2 bytes
2049
 
2050
        We don't handle imm16->imm8 or d16->d8 as they're very rare
2051
        and somewhat more difficult to support.  */
2052
 
2053
static bfd_boolean
2054
mn10300_elf_relax_section (bfd *abfd,
2055
                           asection *sec,
2056
                           struct bfd_link_info *link_info,
2057
                           bfd_boolean *again)
2058
{
2059
  Elf_Internal_Shdr *symtab_hdr;
2060
  Elf_Internal_Rela *internal_relocs = NULL;
2061
  Elf_Internal_Rela *irel, *irelend;
2062
  bfd_byte *contents = NULL;
2063
  Elf_Internal_Sym *isymbuf = NULL;
2064
  struct elf32_mn10300_link_hash_table *hash_table;
2065
  asection *section = sec;
2066
  bfd_vma align_gap_adjustment;
2067
 
2068
  if (link_info->relocatable)
2069
    (*link_info->callbacks->einfo)
2070
      (_("%P%F: --relax and -r may not be used together\n"));
2071
 
2072
  /* Assume nothing changes.  */
2073
  *again = FALSE;
2074
 
2075
  /* We need a pointer to the mn10300 specific hash table.  */
2076
  hash_table = elf32_mn10300_hash_table (link_info);
2077
  if (hash_table == NULL)
2078
    return FALSE;
2079
 
2080
  /* Initialize fields in each hash table entry the first time through.  */
2081
  if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
2082
    {
2083
      bfd *input_bfd;
2084
 
2085
      /* Iterate over all the input bfds.  */
2086
      for (input_bfd = link_info->input_bfds;
2087
           input_bfd != NULL;
2088
           input_bfd = input_bfd->link_next)
2089
        {
2090
          /* We're going to need all the symbols for each bfd.  */
2091
          symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2092
          if (symtab_hdr->sh_info != 0)
2093
            {
2094
              isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2095
              if (isymbuf == NULL)
2096
                isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2097
                                                symtab_hdr->sh_info, 0,
2098
                                                NULL, NULL, NULL);
2099
              if (isymbuf == NULL)
2100
                goto error_return;
2101
            }
2102
 
2103
          /* Iterate over each section in this bfd.  */
2104
          for (section = input_bfd->sections;
2105
               section != NULL;
2106
               section = section->next)
2107
            {
2108
              struct elf32_mn10300_link_hash_entry *hash;
2109
              asection *sym_sec = NULL;
2110
              const char *sym_name;
2111
              char *new_name;
2112
 
2113
              /* If there's nothing to do in this section, skip it.  */
2114
              if (! ((section->flags & SEC_RELOC) != 0
2115
                     && section->reloc_count != 0))
2116
                continue;
2117
              if ((section->flags & SEC_ALLOC) == 0)
2118
                continue;
2119
 
2120
              /* Get cached copy of section contents if it exists.  */
2121
              if (elf_section_data (section)->this_hdr.contents != NULL)
2122
                contents = elf_section_data (section)->this_hdr.contents;
2123
              else if (section->size != 0)
2124
                {
2125
                  /* Go get them off disk.  */
2126
                  if (!bfd_malloc_and_get_section (input_bfd, section,
2127
                                                   &contents))
2128
                    goto error_return;
2129
                }
2130
              else
2131
                contents = NULL;
2132
 
2133
              /* If there aren't any relocs, then there's nothing to do.  */
2134
              if ((section->flags & SEC_RELOC) != 0
2135
                  && section->reloc_count != 0)
2136
                {
2137
                  /* Get a copy of the native relocations.  */
2138
                  internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2139
                                                               NULL, NULL,
2140
                                                               link_info->keep_memory);
2141
                  if (internal_relocs == NULL)
2142
                    goto error_return;
2143
 
2144
                  /* Now examine each relocation.  */
2145
                  irel = internal_relocs;
2146
                  irelend = irel + section->reloc_count;
2147
                  for (; irel < irelend; irel++)
2148
                    {
2149
                      long r_type;
2150
                      unsigned long r_index;
2151
                      unsigned char code;
2152
 
2153
                      r_type = ELF32_R_TYPE (irel->r_info);
2154
                      r_index = ELF32_R_SYM (irel->r_info);
2155
 
2156
                      if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
2157
                        goto error_return;
2158
 
2159
                      /* We need the name and hash table entry of the target
2160
                         symbol!  */
2161
                      hash = NULL;
2162
                      sym_sec = NULL;
2163
 
2164
                      if (r_index < symtab_hdr->sh_info)
2165
                        {
2166
                          /* A local symbol.  */
2167
                          Elf_Internal_Sym *isym;
2168
                          struct elf_link_hash_table *elftab;
2169
                          bfd_size_type amt;
2170
 
2171
                          isym = isymbuf + r_index;
2172
                          if (isym->st_shndx == SHN_UNDEF)
2173
                            sym_sec = bfd_und_section_ptr;
2174
                          else if (isym->st_shndx == SHN_ABS)
2175
                            sym_sec = bfd_abs_section_ptr;
2176
                          else if (isym->st_shndx == SHN_COMMON)
2177
                            sym_sec = bfd_com_section_ptr;
2178
                          else
2179
                            sym_sec
2180
                              = bfd_section_from_elf_index (input_bfd,
2181
                                                            isym->st_shndx);
2182
 
2183
                          sym_name
2184
                            = bfd_elf_string_from_elf_section (input_bfd,
2185
                                                               (symtab_hdr
2186
                                                                ->sh_link),
2187
                                                               isym->st_name);
2188
 
2189
                          /* If it isn't a function, then we don't care
2190
                             about it.  */
2191
                          if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
2192
                            continue;
2193
 
2194
                          /* Tack on an ID so we can uniquely identify this
2195
                             local symbol in the global hash table.  */
2196
                          amt = strlen (sym_name) + 10;
2197
                          new_name = bfd_malloc (amt);
2198
                          if (new_name == NULL)
2199
                            goto error_return;
2200
 
2201
                          sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2202
                          sym_name = new_name;
2203
 
2204
                          elftab = &hash_table->static_hash_table->root;
2205
                          hash = ((struct elf32_mn10300_link_hash_entry *)
2206
                                  elf_link_hash_lookup (elftab, sym_name,
2207
                                                        TRUE, TRUE, FALSE));
2208
                          free (new_name);
2209
                        }
2210
                      else
2211
                        {
2212
                          r_index -= symtab_hdr->sh_info;
2213
                          hash = (struct elf32_mn10300_link_hash_entry *)
2214
                                   elf_sym_hashes (input_bfd)[r_index];
2215
                        }
2216
 
2217
                      sym_name = hash->root.root.root.string;
2218
                      if ((section->flags & SEC_CODE) != 0)
2219
                        {
2220
                          /* If this is not a "call" instruction, then we
2221
                             should convert "call" instructions to "calls"
2222
                             instructions.  */
2223
                          code = bfd_get_8 (input_bfd,
2224
                                            contents + irel->r_offset - 1);
2225
                          if (code != 0xdd && code != 0xcd)
2226
                            hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2227
                        }
2228
 
2229
                      /* If this is a jump/call, then bump the
2230
                         direct_calls counter.  Else force "call" to
2231
                         "calls" conversions.  */
2232
                      if (r_type == R_MN10300_PCREL32
2233
                          || r_type == R_MN10300_PLT32
2234
                          || r_type == R_MN10300_PLT16
2235
                          || r_type == R_MN10300_PCREL16)
2236
                        hash->direct_calls++;
2237
                      else
2238
                        hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2239
                    }
2240
                }
2241
 
2242
              /* Now look at the actual contents to get the stack size,
2243
                 and a list of what registers were saved in the prologue
2244
                 (ie movm_args).  */
2245
              if ((section->flags & SEC_CODE) != 0)
2246
                {
2247
                  Elf_Internal_Sym *isym, *isymend;
2248
                  unsigned int sec_shndx;
2249
                  struct elf_link_hash_entry **hashes;
2250
                  struct elf_link_hash_entry **end_hashes;
2251
                  unsigned int symcount;
2252
 
2253
                  sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2254
                                                                 section);
2255
 
2256
                  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2257
                              - symtab_hdr->sh_info);
2258
                  hashes = elf_sym_hashes (input_bfd);
2259
                  end_hashes = hashes + symcount;
2260
 
2261
                  /* Look at each function defined in this section and
2262
                     update info for that function.  */
2263
                  isymend = isymbuf + symtab_hdr->sh_info;
2264
                  for (isym = isymbuf; isym < isymend; isym++)
2265
                    {
2266
                      if (isym->st_shndx == sec_shndx
2267
                          && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
2268
                        {
2269
                          struct elf_link_hash_table *elftab;
2270
                          bfd_size_type amt;
2271
                          struct elf_link_hash_entry **lhashes = hashes;
2272
 
2273
                          /* Skip a local symbol if it aliases a
2274
                             global one.  */
2275
                          for (; lhashes < end_hashes; lhashes++)
2276
                            {
2277
                              hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
2278
                              if ((hash->root.root.type == bfd_link_hash_defined
2279
                                   || hash->root.root.type == bfd_link_hash_defweak)
2280
                                  && hash->root.root.u.def.section == section
2281
                                  && hash->root.type == STT_FUNC
2282
                                  && hash->root.root.u.def.value == isym->st_value)
2283
                                break;
2284
                            }
2285
                          if (lhashes != end_hashes)
2286
                            continue;
2287
 
2288
                          if (isym->st_shndx == SHN_UNDEF)
2289
                            sym_sec = bfd_und_section_ptr;
2290
                          else if (isym->st_shndx == SHN_ABS)
2291
                            sym_sec = bfd_abs_section_ptr;
2292
                          else if (isym->st_shndx == SHN_COMMON)
2293
                            sym_sec = bfd_com_section_ptr;
2294
                          else
2295
                            sym_sec
2296
                              = bfd_section_from_elf_index (input_bfd,
2297
                                                            isym->st_shndx);
2298
 
2299
                          sym_name = (bfd_elf_string_from_elf_section
2300
                                      (input_bfd, symtab_hdr->sh_link,
2301
                                       isym->st_name));
2302
 
2303
                          /* Tack on an ID so we can uniquely identify this
2304
                             local symbol in the global hash table.  */
2305
                          amt = strlen (sym_name) + 10;
2306
                          new_name = bfd_malloc (amt);
2307
                          if (new_name == NULL)
2308
                            goto error_return;
2309
 
2310
                          sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2311
                          sym_name = new_name;
2312
 
2313
                          elftab = &hash_table->static_hash_table->root;
2314
                          hash = ((struct elf32_mn10300_link_hash_entry *)
2315
                                  elf_link_hash_lookup (elftab, sym_name,
2316
                                                        TRUE, TRUE, FALSE));
2317
                          free (new_name);
2318
                          compute_function_info (input_bfd, hash,
2319
                                                 isym->st_value, contents);
2320
                          hash->value = isym->st_value;
2321
                        }
2322
                    }
2323
 
2324
                  for (; hashes < end_hashes; hashes++)
2325
                    {
2326
                      hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2327
                      if ((hash->root.root.type == bfd_link_hash_defined
2328
                           || hash->root.root.type == bfd_link_hash_defweak)
2329
                          && hash->root.root.u.def.section == section
2330
                          && hash->root.type == STT_FUNC)
2331
                        compute_function_info (input_bfd, hash,
2332
                                               (hash)->root.root.u.def.value,
2333
                                               contents);
2334
                    }
2335
                }
2336
 
2337
              /* Cache or free any memory we allocated for the relocs.  */
2338
              if (internal_relocs != NULL
2339
                  && elf_section_data (section)->relocs != internal_relocs)
2340
                free (internal_relocs);
2341
              internal_relocs = NULL;
2342
 
2343
              /* Cache or free any memory we allocated for the contents.  */
2344
              if (contents != NULL
2345
                  && elf_section_data (section)->this_hdr.contents != contents)
2346
                {
2347
                  if (! link_info->keep_memory)
2348
                    free (contents);
2349
                  else
2350
                    {
2351
                      /* Cache the section contents for elf_link_input_bfd.  */
2352
                      elf_section_data (section)->this_hdr.contents = contents;
2353
                    }
2354
                }
2355
              contents = NULL;
2356
            }
2357
 
2358
          /* Cache or free any memory we allocated for the symbols.  */
2359
          if (isymbuf != NULL
2360
              && symtab_hdr->contents != (unsigned char *) isymbuf)
2361
            {
2362
              if (! link_info->keep_memory)
2363
                free (isymbuf);
2364
              else
2365
                {
2366
                  /* Cache the symbols for elf_link_input_bfd.  */
2367
                  symtab_hdr->contents = (unsigned char *) isymbuf;
2368
                }
2369
            }
2370
          isymbuf = NULL;
2371
        }
2372
 
2373
      /* Now iterate on each symbol in the hash table and perform
2374
         the final initialization steps on each.  */
2375
      elf32_mn10300_link_hash_traverse (hash_table,
2376
                                        elf32_mn10300_finish_hash_table_entry,
2377
                                        link_info);
2378
      elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2379
                                        elf32_mn10300_finish_hash_table_entry,
2380
                                        link_info);
2381
 
2382
      {
2383
        /* This section of code collects all our local symbols, sorts
2384
           them by value, and looks for multiple symbols referring to
2385
           the same address.  For those symbols, the flags are merged.
2386
           At this point, the only flag that can be set is
2387
           MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
2388
           together.  */
2389
        int static_count = 0, i;
2390
        struct elf32_mn10300_link_hash_entry **entries;
2391
        struct elf32_mn10300_link_hash_entry **ptr;
2392
 
2393
        elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2394
                                          elf32_mn10300_count_hash_table_entries,
2395
                                          &static_count);
2396
 
2397
        entries = bfd_malloc (static_count * sizeof (* ptr));
2398
 
2399
        ptr = entries;
2400
        elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2401
                                          elf32_mn10300_list_hash_table_entries,
2402
                                          & ptr);
2403
 
2404
        qsort (entries, static_count, sizeof (entries[0]), sort_by_value);
2405
 
2406
        for (i = 0; i < static_count - 1; i++)
2407
          if (entries[i]->value && entries[i]->value == entries[i+1]->value)
2408
            {
2409
              int v = entries[i]->flags;
2410
              int j;
2411
 
2412
              for (j = i + 1; j < static_count && entries[j]->value == entries[i]->value; j++)
2413
                v |= entries[j]->flags;
2414
 
2415
              for (j = i; j < static_count && entries[j]->value == entries[i]->value; j++)
2416
                entries[j]->flags = v;
2417
 
2418
              i = j - 1;
2419
            }
2420
      }
2421
 
2422
      /* All entries in the hash table are fully initialized.  */
2423
      hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
2424
 
2425
      /* Now that everything has been initialized, go through each
2426
         code section and delete any prologue insns which will be
2427
         redundant because their operations will be performed by
2428
         a "call" instruction.  */
2429
      for (input_bfd = link_info->input_bfds;
2430
           input_bfd != NULL;
2431
           input_bfd = input_bfd->link_next)
2432
        {
2433
          /* We're going to need all the local symbols for each bfd.  */
2434
          symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2435
          if (symtab_hdr->sh_info != 0)
2436
            {
2437
              isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2438
              if (isymbuf == NULL)
2439
                isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2440
                                                symtab_hdr->sh_info, 0,
2441
                                                NULL, NULL, NULL);
2442
              if (isymbuf == NULL)
2443
                goto error_return;
2444
            }
2445
 
2446
          /* Walk over each section in this bfd.  */
2447
          for (section = input_bfd->sections;
2448
               section != NULL;
2449
               section = section->next)
2450
            {
2451
              unsigned int sec_shndx;
2452
              Elf_Internal_Sym *isym, *isymend;
2453
              struct elf_link_hash_entry **hashes;
2454
              struct elf_link_hash_entry **end_hashes;
2455
              unsigned int symcount;
2456
 
2457
              /* Skip non-code sections and empty sections.  */
2458
              if ((section->flags & SEC_CODE) == 0 || section->size == 0)
2459
                continue;
2460
 
2461
              if (section->reloc_count != 0)
2462
                {
2463
                  /* Get a copy of the native relocations.  */
2464
                  internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2465
                                                               NULL, NULL,
2466
                                                               link_info->keep_memory);
2467
                  if (internal_relocs == NULL)
2468
                    goto error_return;
2469
                }
2470
 
2471
              /* Get cached copy of section contents if it exists.  */
2472
              if (elf_section_data (section)->this_hdr.contents != NULL)
2473
                contents = elf_section_data (section)->this_hdr.contents;
2474
              else
2475
                {
2476
                  /* Go get them off disk.  */
2477
                  if (!bfd_malloc_and_get_section (input_bfd, section,
2478
                                                   &contents))
2479
                    goto error_return;
2480
                }
2481
 
2482
              sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2483
                                                             section);
2484
 
2485
              /* Now look for any function in this section which needs
2486
                 insns deleted from its prologue.  */
2487
              isymend = isymbuf + symtab_hdr->sh_info;
2488
              for (isym = isymbuf; isym < isymend; isym++)
2489
                {
2490
                  struct elf32_mn10300_link_hash_entry *sym_hash;
2491
                  asection *sym_sec = NULL;
2492
                  const char *sym_name;
2493
                  char *new_name;
2494
                  struct elf_link_hash_table *elftab;
2495
                  bfd_size_type amt;
2496
 
2497
                  if (isym->st_shndx != sec_shndx)
2498
                    continue;
2499
 
2500
                  if (isym->st_shndx == SHN_UNDEF)
2501
                    sym_sec = bfd_und_section_ptr;
2502
                  else if (isym->st_shndx == SHN_ABS)
2503
                    sym_sec = bfd_abs_section_ptr;
2504
                  else if (isym->st_shndx == SHN_COMMON)
2505
                    sym_sec = bfd_com_section_ptr;
2506
                  else
2507
                    sym_sec
2508
                      = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2509
 
2510
                  sym_name
2511
                    = bfd_elf_string_from_elf_section (input_bfd,
2512
                                                       symtab_hdr->sh_link,
2513
                                                       isym->st_name);
2514
 
2515
                  /* Tack on an ID so we can uniquely identify this
2516
                     local symbol in the global hash table.  */
2517
                  amt = strlen (sym_name) + 10;
2518
                  new_name = bfd_malloc (amt);
2519
                  if (new_name == NULL)
2520
                    goto error_return;
2521
                  sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2522
                  sym_name = new_name;
2523
 
2524
                  elftab = & hash_table->static_hash_table->root;
2525
                  sym_hash = (struct elf32_mn10300_link_hash_entry *)
2526
                    elf_link_hash_lookup (elftab, sym_name,
2527
                                          FALSE, FALSE, FALSE);
2528
 
2529
                  free (new_name);
2530
                  if (sym_hash == NULL)
2531
                    continue;
2532
 
2533
                  if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2534
                      && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2535
                    {
2536
                      int bytes = 0;
2537
 
2538
                      /* Note that we've changed things.  */
2539
                      elf_section_data (section)->relocs = internal_relocs;
2540
                      elf_section_data (section)->this_hdr.contents = contents;
2541
                      symtab_hdr->contents = (unsigned char *) isymbuf;
2542
 
2543
                      /* Count how many bytes we're going to delete.  */
2544
                      if (sym_hash->movm_args)
2545
                        bytes += 2;
2546
 
2547
                      if (sym_hash->stack_size > 0)
2548
                        {
2549
                          if (sym_hash->stack_size <= 128)
2550
                            bytes += 3;
2551
                          else
2552
                            bytes += 4;
2553
                        }
2554
 
2555
                      /* Note that we've deleted prologue bytes for this
2556
                         function.  */
2557
                      sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2558
 
2559
                      /* Actually delete the bytes.  */
2560
                      if (!mn10300_elf_relax_delete_bytes (input_bfd,
2561
                                                           section,
2562
                                                           isym->st_value,
2563
                                                           bytes))
2564
                        goto error_return;
2565
 
2566
                      /* Something changed.  Not strictly necessary, but
2567
                         may lead to more relaxing opportunities.  */
2568
                      *again = TRUE;
2569
                    }
2570
                }
2571
 
2572
              /* Look for any global functions in this section which
2573
                 need insns deleted from their prologues.  */
2574
              symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2575
                          - symtab_hdr->sh_info);
2576
              hashes = elf_sym_hashes (input_bfd);
2577
              end_hashes = hashes + symcount;
2578
              for (; hashes < end_hashes; hashes++)
2579
                {
2580
                  struct elf32_mn10300_link_hash_entry *sym_hash;
2581
 
2582
                  sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2583
                  if ((sym_hash->root.root.type == bfd_link_hash_defined
2584
                       || sym_hash->root.root.type == bfd_link_hash_defweak)
2585
                      && sym_hash->root.root.u.def.section == section
2586
                      && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2587
                      && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2588
                    {
2589
                      int bytes = 0;
2590
                      bfd_vma symval;
2591
 
2592
                      /* Note that we've changed things.  */
2593
                      elf_section_data (section)->relocs = internal_relocs;
2594
                      elf_section_data (section)->this_hdr.contents = contents;
2595
                      symtab_hdr->contents = (unsigned char *) isymbuf;
2596
 
2597
                      /* Count how many bytes we're going to delete.  */
2598
                      if (sym_hash->movm_args)
2599
                        bytes += 2;
2600
 
2601
                      if (sym_hash->stack_size > 0)
2602
                        {
2603
                          if (sym_hash->stack_size <= 128)
2604
                            bytes += 3;
2605
                          else
2606
                            bytes += 4;
2607
                        }
2608
 
2609
                      /* Note that we've deleted prologue bytes for this
2610
                         function.  */
2611
                      sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2612
 
2613
                      /* Actually delete the bytes.  */
2614
                      symval = sym_hash->root.root.u.def.value;
2615
                      if (!mn10300_elf_relax_delete_bytes (input_bfd,
2616
                                                           section,
2617
                                                           symval,
2618
                                                           bytes))
2619
                        goto error_return;
2620
 
2621
                      /* Something changed.  Not strictly necessary, but
2622
                         may lead to more relaxing opportunities.  */
2623
                      *again = TRUE;
2624
                    }
2625
                }
2626
 
2627
              /* Cache or free any memory we allocated for the relocs.  */
2628
              if (internal_relocs != NULL
2629
                  && elf_section_data (section)->relocs != internal_relocs)
2630
                free (internal_relocs);
2631
              internal_relocs = NULL;
2632
 
2633
              /* Cache or free any memory we allocated for the contents.  */
2634
              if (contents != NULL
2635
                  && elf_section_data (section)->this_hdr.contents != contents)
2636
                {
2637
                  if (! link_info->keep_memory)
2638
                    free (contents);
2639
                  else
2640
                    /* Cache the section contents for elf_link_input_bfd.  */
2641
                    elf_section_data (section)->this_hdr.contents = contents;
2642
                }
2643
              contents = NULL;
2644
            }
2645
 
2646
          /* Cache or free any memory we allocated for the symbols.  */
2647
          if (isymbuf != NULL
2648
              && symtab_hdr->contents != (unsigned char *) isymbuf)
2649
            {
2650
              if (! link_info->keep_memory)
2651
                free (isymbuf);
2652
              else
2653
                /* Cache the symbols for elf_link_input_bfd.  */
2654
                symtab_hdr->contents = (unsigned char *) isymbuf;
2655
            }
2656
          isymbuf = NULL;
2657
        }
2658
    }
2659
 
2660
  /* (Re)initialize for the basic instruction shortening/relaxing pass.  */
2661
  contents = NULL;
2662
  internal_relocs = NULL;
2663
  isymbuf = NULL;
2664
  /* For error_return.  */
2665
  section = sec;
2666
 
2667
  /* We don't have to do anything for a relocatable link, if
2668
     this section does not have relocs, or if this is not a
2669
     code section.  */
2670
  if (link_info->relocatable
2671
      || (sec->flags & SEC_RELOC) == 0
2672
      || sec->reloc_count == 0
2673
      || (sec->flags & SEC_CODE) == 0)
2674
    return TRUE;
2675
 
2676
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2677
 
2678
  /* Get a copy of the native relocations.  */
2679
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2680
                                               link_info->keep_memory);
2681
  if (internal_relocs == NULL)
2682
    goto error_return;
2683
 
2684
  /* Scan for worst case alignment gap changes.  Note that this logic
2685
     is not ideal; what we should do is run this scan for every
2686
     opcode/address range and adjust accordingly, but that's
2687
     expensive.  Worst case is that for an alignment of N bytes, we
2688
     move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc
2689
     all before it.  Plus, this still doesn't cover cross-section
2690
     jumps with section alignment.  */
2691
  irelend = internal_relocs + sec->reloc_count;
2692
  align_gap_adjustment = 0;
2693
  for (irel = internal_relocs; irel < irelend; irel++)
2694
    {
2695
      if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN)
2696
        {
2697
          bfd_vma adj = 1 << irel->r_addend;
2698
          bfd_vma aend = irel->r_offset;
2699
 
2700
          aend = BFD_ALIGN (aend, 1 << irel->r_addend);
2701
          adj = 2 * adj - adj - 1;
2702
 
2703
          /* Record the biggest adjustmnet.  Skip any alignment at the
2704
             end of our section.  */
2705
          if (align_gap_adjustment < adj
2706
              && aend < sec->output_section->vma + sec->output_offset + sec->size)
2707
            align_gap_adjustment = adj;
2708
        }
2709
    }
2710
 
2711
  /* Walk through them looking for relaxing opportunities.  */
2712
  irelend = internal_relocs + sec->reloc_count;
2713
  for (irel = internal_relocs; irel < irelend; irel++)
2714
    {
2715
      bfd_vma symval;
2716
      bfd_signed_vma jump_offset;
2717
      asection *sym_sec = NULL;
2718
      struct elf32_mn10300_link_hash_entry *h = NULL;
2719
 
2720
      /* If this isn't something that can be relaxed, then ignore
2721
         this reloc.  */
2722
      if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
2723
          || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
2724
          || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
2725
        continue;
2726
 
2727
      /* Get the section contents if we haven't done so already.  */
2728
      if (contents == NULL)
2729
        {
2730
          /* Get cached copy if it exists.  */
2731
          if (elf_section_data (sec)->this_hdr.contents != NULL)
2732
            contents = elf_section_data (sec)->this_hdr.contents;
2733
          else
2734
            {
2735
              /* Go get them off disk.  */
2736
              if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2737
                goto error_return;
2738
            }
2739
        }
2740
 
2741
      /* Read this BFD's symbols if we haven't done so already.  */
2742
      if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2743
        {
2744
          isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2745
          if (isymbuf == NULL)
2746
            isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2747
                                            symtab_hdr->sh_info, 0,
2748
                                            NULL, NULL, NULL);
2749
          if (isymbuf == NULL)
2750
            goto error_return;
2751
        }
2752
 
2753
      /* Get the value of the symbol referred to by the reloc.  */
2754
      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2755
        {
2756
          Elf_Internal_Sym *isym;
2757
          const char *sym_name;
2758
          char *new_name;
2759
 
2760
          /* A local symbol.  */
2761
          isym = isymbuf + ELF32_R_SYM (irel->r_info);
2762
          if (isym->st_shndx == SHN_UNDEF)
2763
            sym_sec = bfd_und_section_ptr;
2764
          else if (isym->st_shndx == SHN_ABS)
2765
            sym_sec = bfd_abs_section_ptr;
2766
          else if (isym->st_shndx == SHN_COMMON)
2767
            sym_sec = bfd_com_section_ptr;
2768
          else
2769
            sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2770
 
2771
          sym_name = bfd_elf_string_from_elf_section (abfd,
2772
                                                      symtab_hdr->sh_link,
2773
                                                      isym->st_name);
2774
 
2775
          if ((sym_sec->flags & SEC_MERGE)
2776
              && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2777
            {
2778
              symval = isym->st_value;
2779
 
2780
              /* GAS may reduce relocations against symbols in SEC_MERGE
2781
                 sections to a relocation against the section symbol when
2782
                 the original addend was zero.  When the reloc is against
2783
                 a section symbol we should include the addend in the
2784
                 offset passed to _bfd_merged_section_offset, since the
2785
                 location of interest is the original symbol.  On the
2786
                 other hand, an access to "sym+addend" where "sym" is not
2787
                 a section symbol should not include the addend;  Such an
2788
                 access is presumed to be an offset from "sym";  The
2789
                 location of interest is just "sym".  */
2790
              if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
2791
                symval += irel->r_addend;
2792
 
2793
              symval = _bfd_merged_section_offset (abfd, & sym_sec,
2794
                                                   elf_section_data (sym_sec)->sec_info,
2795
                                                   symval);
2796
 
2797
              if (ELF_ST_TYPE (isym->st_info) != STT_SECTION)
2798
                symval += irel->r_addend;
2799
 
2800
              symval += sym_sec->output_section->vma
2801
                + sym_sec->output_offset - irel->r_addend;
2802
            }
2803
          else
2804
            symval = (isym->st_value
2805
                      + sym_sec->output_section->vma
2806
                      + sym_sec->output_offset);
2807
 
2808
          /* Tack on an ID so we can uniquely identify this
2809
             local symbol in the global hash table.  */
2810
          new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
2811
          if (new_name == NULL)
2812
            goto error_return;
2813
          sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2814
          sym_name = new_name;
2815
 
2816
          h = (struct elf32_mn10300_link_hash_entry *)
2817
                elf_link_hash_lookup (&hash_table->static_hash_table->root,
2818
                                      sym_name, FALSE, FALSE, FALSE);
2819
          free (new_name);
2820
        }
2821
      else
2822
        {
2823
          unsigned long indx;
2824
 
2825
          /* An external symbol.  */
2826
          indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2827
          h = (struct elf32_mn10300_link_hash_entry *)
2828
                (elf_sym_hashes (abfd)[indx]);
2829
          BFD_ASSERT (h != NULL);
2830
          if (h->root.root.type != bfd_link_hash_defined
2831
              && h->root.root.type != bfd_link_hash_defweak)
2832
            /* This appears to be a reference to an undefined
2833
               symbol.  Just ignore it--it will be caught by the
2834
               regular reloc processing.  */
2835
            continue;
2836
 
2837
          /* Check for a reference to a discarded symbol and ignore it.  */
2838
          if (h->root.root.u.def.section->output_section == NULL)
2839
            continue;
2840
 
2841
          sym_sec = h->root.root.u.def.section->output_section;
2842
 
2843
          symval = (h->root.root.u.def.value
2844
                    + h->root.root.u.def.section->output_section->vma
2845
                    + h->root.root.u.def.section->output_offset);
2846
        }
2847
 
2848
      /* For simplicity of coding, we are going to modify the section
2849
         contents, the section relocs, and the BFD symbol table.  We
2850
         must tell the rest of the code not to free up this
2851
         information.  It would be possible to instead create a table
2852
         of changes which have to be made, as is done in coff-mips.c;
2853
         that would be more work, but would require less memory when
2854
         the linker is run.  */
2855
 
2856
      /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2857
         branch/call, also deal with "call" -> "calls" conversions and
2858
         insertion of prologue data into "call" instructions.  */
2859
      if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
2860
          || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
2861
        {
2862
          bfd_vma value = symval;
2863
 
2864
          if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
2865
              && h != NULL
2866
              && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2867
              && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2868
              && h->root.plt.offset != (bfd_vma) -1)
2869
            {
2870
              asection * splt;
2871
 
2872
              splt = bfd_get_section_by_name (elf_hash_table (link_info)
2873
                                              ->dynobj, ".plt");
2874
 
2875
              value = ((splt->output_section->vma
2876
                        + splt->output_offset
2877
                        + h->root.plt.offset)
2878
                       - (sec->output_section->vma
2879
                          + sec->output_offset
2880
                          + irel->r_offset));
2881
            }
2882
 
2883
          /* If we've got a "call" instruction that needs to be turned
2884
             into a "calls" instruction, do so now.  It saves a byte.  */
2885
          if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2886
            {
2887
              unsigned char code;
2888
 
2889
              /* Get the opcode.  */
2890
              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2891
 
2892
              /* Make sure we're working with a "call" instruction!  */
2893
              if (code == 0xdd)
2894
                {
2895
                  /* Note that we've changed the relocs, section contents,
2896
                     etc.  */
2897
                  elf_section_data (sec)->relocs = internal_relocs;
2898
                  elf_section_data (sec)->this_hdr.contents = contents;
2899
                  symtab_hdr->contents = (unsigned char *) isymbuf;
2900
 
2901
                  /* Fix the opcode.  */
2902
                  bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
2903
                  bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2904
 
2905
                  /* Fix irel->r_offset and irel->r_addend.  */
2906
                  irel->r_offset += 1;
2907
                  irel->r_addend += 1;
2908
 
2909
                  /* Delete one byte of data.  */
2910
                  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2911
                                                       irel->r_offset + 3, 1))
2912
                    goto error_return;
2913
 
2914
                  /* That will change things, so, we should relax again.
2915
                     Note that this is not required, and it may be slow.  */
2916
                  *again = TRUE;
2917
                }
2918
            }
2919
          else if (h)
2920
            {
2921
              /* We've got a "call" instruction which needs some data
2922
                 from target function filled in.  */
2923
              unsigned char code;
2924
 
2925
              /* Get the opcode.  */
2926
              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2927
 
2928
              /* Insert data from the target function into the "call"
2929
                 instruction if needed.  */
2930
              if (code == 0xdd)
2931
                {
2932
                  bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
2933
                  bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2934
                             contents + irel->r_offset + 5);
2935
                }
2936
            }
2937
 
2938
          /* Deal with pc-relative gunk.  */
2939
          value -= (sec->output_section->vma + sec->output_offset);
2940
          value -= irel->r_offset;
2941
          value += irel->r_addend;
2942
 
2943
          /* See if the value will fit in 16 bits, note the high value is
2944
             0x7fff + 2 as the target will be two bytes closer if we are
2945
             able to relax, if it's in the same section.  */
2946
          if (sec->output_section == sym_sec->output_section)
2947
            jump_offset = 0x8001;
2948
          else
2949
            jump_offset = 0x7fff;
2950
 
2951
          /* Account for jumps across alignment boundaries using
2952
             align_gap_adjustment.  */
2953
          if ((bfd_signed_vma) value < jump_offset - (bfd_signed_vma) align_gap_adjustment
2954
              && ((bfd_signed_vma) value > -0x8000 + (bfd_signed_vma) align_gap_adjustment))
2955
            {
2956
              unsigned char code;
2957
 
2958
              /* Get the opcode.  */
2959
              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2960
 
2961
              if (code != 0xdc && code != 0xdd && code != 0xff)
2962
                continue;
2963
 
2964
              /* Note that we've changed the relocs, section contents, etc.  */
2965
              elf_section_data (sec)->relocs = internal_relocs;
2966
              elf_section_data (sec)->this_hdr.contents = contents;
2967
              symtab_hdr->contents = (unsigned char *) isymbuf;
2968
 
2969
              /* Fix the opcode.  */
2970
              if (code == 0xdc)
2971
                bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
2972
              else if (code == 0xdd)
2973
                bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
2974
              else if (code == 0xff)
2975
                bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2976
 
2977
              /* Fix the relocation's type.  */
2978
              irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2979
                                           (ELF32_R_TYPE (irel->r_info)
2980
                                            == (int) R_MN10300_PLT32)
2981
                                           ? R_MN10300_PLT16 :
2982
                                           R_MN10300_PCREL16);
2983
 
2984
              /* Delete two bytes of data.  */
2985
              if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2986
                                                   irel->r_offset + 1, 2))
2987
                goto error_return;
2988
 
2989
              /* That will change things, so, we should relax again.
2990
                 Note that this is not required, and it may be slow.  */
2991
              *again = TRUE;
2992
            }
2993
        }
2994
 
2995
      /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2996
         branch.  */
2997
      if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
2998
        {
2999
          bfd_vma value = symval;
3000
 
3001
          /* If we've got a "call" instruction that needs to be turned
3002
             into a "calls" instruction, do so now.  It saves a byte.  */
3003
          if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
3004
            {
3005
              unsigned char code;
3006
 
3007
              /* Get the opcode.  */
3008
              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3009
 
3010
              /* Make sure we're working with a "call" instruction!  */
3011
              if (code == 0xcd)
3012
                {
3013
                  /* Note that we've changed the relocs, section contents,
3014
                     etc.  */
3015
                  elf_section_data (sec)->relocs = internal_relocs;
3016
                  elf_section_data (sec)->this_hdr.contents = contents;
3017
                  symtab_hdr->contents = (unsigned char *) isymbuf;
3018
 
3019
                  /* Fix the opcode.  */
3020
                  bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
3021
                  bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
3022
 
3023
                  /* Fix irel->r_offset and irel->r_addend.  */
3024
                  irel->r_offset += 1;
3025
                  irel->r_addend += 1;
3026
 
3027
                  /* Delete one byte of data.  */
3028
                  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3029
                                                       irel->r_offset + 1, 1))
3030
                    goto error_return;
3031
 
3032
                  /* That will change things, so, we should relax again.
3033
                     Note that this is not required, and it may be slow.  */
3034
                  *again = TRUE;
3035
                }
3036
            }
3037
          else if (h)
3038
            {
3039
              unsigned char code;
3040
 
3041
              /* Get the opcode.  */
3042
              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3043
 
3044
              /* Insert data from the target function into the "call"
3045
                 instruction if needed.  */
3046
              if (code == 0xcd)
3047
                {
3048
                  bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
3049
                  bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
3050
                             contents + irel->r_offset + 3);
3051
                }
3052
            }
3053
 
3054
          /* Deal with pc-relative gunk.  */
3055
          value -= (sec->output_section->vma + sec->output_offset);
3056
          value -= irel->r_offset;
3057
          value += irel->r_addend;
3058
 
3059
          /* See if the value will fit in 8 bits, note the high value is
3060
             0x7f + 1 as the target will be one bytes closer if we are
3061
             able to relax.  */
3062
          if ((long) value < 0x80 && (long) value > -0x80)
3063
            {
3064
              unsigned char code;
3065
 
3066
              /* Get the opcode.  */
3067
              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3068
 
3069
              if (code != 0xcc)
3070
                continue;
3071
 
3072
              /* Note that we've changed the relocs, section contents, etc.  */
3073
              elf_section_data (sec)->relocs = internal_relocs;
3074
              elf_section_data (sec)->this_hdr.contents = contents;
3075
              symtab_hdr->contents = (unsigned char *) isymbuf;
3076
 
3077
              /* Fix the opcode.  */
3078
              bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
3079
 
3080
              /* Fix the relocation's type.  */
3081
              irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3082
                                           R_MN10300_PCREL8);
3083
 
3084
              /* Delete one byte of data.  */
3085
              if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3086
                                                   irel->r_offset + 1, 1))
3087
                goto error_return;
3088
 
3089
              /* That will change things, so, we should relax again.
3090
                 Note that this is not required, and it may be slow.  */
3091
              *again = TRUE;
3092
            }
3093
        }
3094
 
3095
      /* Try to eliminate an unconditional 8 bit pc-relative branch
3096
         which immediately follows a conditional 8 bit pc-relative
3097
         branch around the unconditional branch.
3098
 
3099
            original:           new:
3100
            bCC lab1            bCC' lab2
3101
            bra lab2
3102
           lab1:               lab1:
3103
 
3104
         This happens when the bCC can't reach lab2 at assembly time,
3105
         but due to other relaxations it can reach at link time.  */
3106
      if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
3107
        {
3108
          Elf_Internal_Rela *nrel;
3109
          bfd_vma value = symval;
3110
          unsigned char code;
3111
 
3112
          /* Deal with pc-relative gunk.  */
3113
          value -= (sec->output_section->vma + sec->output_offset);
3114
          value -= irel->r_offset;
3115
          value += irel->r_addend;
3116
 
3117
          /* Do nothing if this reloc is the last byte in the section.  */
3118
          if (irel->r_offset == sec->size)
3119
            continue;
3120
 
3121
          /* See if the next instruction is an unconditional pc-relative
3122
             branch, more often than not this test will fail, so we
3123
             test it first to speed things up.  */
3124
          code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
3125
          if (code != 0xca)
3126
            continue;
3127
 
3128
          /* Also make sure the next relocation applies to the next
3129
             instruction and that it's a pc-relative 8 bit branch.  */
3130
          nrel = irel + 1;
3131
          if (nrel == irelend
3132
              || irel->r_offset + 2 != nrel->r_offset
3133
              || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
3134
            continue;
3135
 
3136
          /* Make sure our destination immediately follows the
3137
             unconditional branch.  */
3138
          if (symval != (sec->output_section->vma + sec->output_offset
3139
                         + irel->r_offset + 3))
3140
            continue;
3141
 
3142
          /* Now make sure we are a conditional branch.  This may not
3143
             be necessary, but why take the chance.
3144
 
3145
             Note these checks assume that R_MN10300_PCREL8 relocs
3146
             only occur on bCC and bCCx insns.  If they occured
3147
             elsewhere, we'd need to know the start of this insn
3148
             for this check to be accurate.  */
3149
          code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3150
          if (code != 0xc0 && code != 0xc1 && code != 0xc2
3151
              && code != 0xc3 && code != 0xc4 && code != 0xc5
3152
              && code != 0xc6 && code != 0xc7 && code != 0xc8
3153
              && code != 0xc9 && code != 0xe8 && code != 0xe9
3154
              && code != 0xea && code != 0xeb)
3155
            continue;
3156
 
3157
          /* We also have to be sure there is no symbol/label
3158
             at the unconditional branch.  */
3159
          if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
3160
                                            irel->r_offset + 1))
3161
            continue;
3162
 
3163
          /* Note that we've changed the relocs, section contents, etc.  */
3164
          elf_section_data (sec)->relocs = internal_relocs;
3165
          elf_section_data (sec)->this_hdr.contents = contents;
3166
          symtab_hdr->contents = (unsigned char *) isymbuf;
3167
 
3168
          /* Reverse the condition of the first branch.  */
3169
          switch (code)
3170
            {
3171
            case 0xc8:
3172
              code = 0xc9;
3173
              break;
3174
            case 0xc9:
3175
              code = 0xc8;
3176
              break;
3177
            case 0xc0:
3178
              code = 0xc2;
3179
              break;
3180
            case 0xc2:
3181
              code = 0xc0;
3182
              break;
3183
            case 0xc3:
3184
              code = 0xc1;
3185
              break;
3186
            case 0xc1:
3187
              code = 0xc3;
3188
              break;
3189
            case 0xc4:
3190
              code = 0xc6;
3191
              break;
3192
            case 0xc6:
3193
              code = 0xc4;
3194
              break;
3195
            case 0xc7:
3196
              code = 0xc5;
3197
              break;
3198
            case 0xc5:
3199
              code = 0xc7;
3200
              break;
3201
            case 0xe8:
3202
              code = 0xe9;
3203
              break;
3204
            case 0x9d:
3205
              code = 0xe8;
3206
              break;
3207
            case 0xea:
3208
              code = 0xeb;
3209
              break;
3210
            case 0xeb:
3211
              code = 0xea;
3212
              break;
3213
            }
3214
          bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3215
 
3216
          /* Set the reloc type and symbol for the first branch
3217
             from the second branch.  */
3218
          irel->r_info = nrel->r_info;
3219
 
3220
          /* Make the reloc for the second branch a null reloc.  */
3221
          nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3222
                                       R_MN10300_NONE);
3223
 
3224
          /* Delete two bytes of data.  */
3225
          if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3226
                                               irel->r_offset + 1, 2))
3227
            goto error_return;
3228
 
3229
          /* That will change things, so, we should relax again.
3230
             Note that this is not required, and it may be slow.  */
3231
          *again = TRUE;
3232
        }
3233
 
3234
      /* Try to turn a 24 immediate, displacement or absolute address
3235
         into a 8 immediate, displacement or absolute address.  */
3236
      if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
3237
        {
3238
          bfd_vma value = symval;
3239
          value += irel->r_addend;
3240
 
3241
          /* See if the value will fit in 8 bits.  */
3242
          if ((long) value < 0x7f && (long) value > -0x80)
3243
            {
3244
              unsigned char code;
3245
 
3246
              /* AM33 insns which have 24 operands are 6 bytes long and
3247
                 will have 0xfd as the first byte.  */
3248
 
3249
              /* Get the first opcode.  */
3250
              code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3251
 
3252
              if (code == 0xfd)
3253
                {
3254
                  /* Get the second opcode.  */
3255
                  code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3256
 
3257
                  /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3258
                     equivalent instructions exists.  */
3259
                  if (code != 0x6b && code != 0x7b
3260
                      && code != 0x8b && code != 0x9b
3261
                      && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3262
                          || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3263
                          || (code & 0x0f) == 0x0e))
3264
                    {
3265
                      /* Not safe if the high bit is on as relaxing may
3266
                         move the value out of high mem and thus not fit
3267
                         in a signed 8bit value.  This is currently over
3268
                         conservative.  */
3269
                      if ((value & 0x80) == 0)
3270
                        {
3271
                          /* Note that we've changed the relocation contents,
3272
                             etc.  */
3273
                          elf_section_data (sec)->relocs = internal_relocs;
3274
                          elf_section_data (sec)->this_hdr.contents = contents;
3275
                          symtab_hdr->contents = (unsigned char *) isymbuf;
3276
 
3277
                          /* Fix the opcode.  */
3278
                          bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
3279
                          bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3280
 
3281
                          /* Fix the relocation's type.  */
3282
                          irel->r_info =
3283
                            ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3284
                                          R_MN10300_8);
3285
 
3286
                          /* Delete two bytes of data.  */
3287
                          if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3288
                                                               irel->r_offset + 1, 2))
3289
                            goto error_return;
3290
 
3291
                          /* That will change things, so, we should relax
3292
                             again.  Note that this is not required, and it
3293
                             may be slow.  */
3294
                          *again = TRUE;
3295
                          break;
3296
                        }
3297
                    }
3298
                }
3299
            }
3300
        }
3301
 
3302
      /* Try to turn a 32bit immediate, displacement or absolute address
3303
         into a 16bit immediate, displacement or absolute address.  */
3304
      if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
3305
          || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
3306
          || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3307
        {
3308
          bfd_vma value = symval;
3309
 
3310
          if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
3311
            {
3312
              asection * sgot;
3313
 
3314
              sgot = bfd_get_section_by_name (elf_hash_table (link_info)
3315
                                              ->dynobj, ".got");
3316
 
3317
              if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
3318
                {
3319
                  value = sgot->output_offset;
3320
 
3321
                  if (h)
3322
                    value += h->root.got.offset;
3323
                  else
3324
                    value += (elf_local_got_offsets
3325
                              (abfd)[ELF32_R_SYM (irel->r_info)]);
3326
                }
3327
              else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3328
                value -= sgot->output_section->vma;
3329
              else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
3330
                value = (sgot->output_section->vma
3331
                         - (sec->output_section->vma
3332
                            + sec->output_offset
3333
                            + irel->r_offset));
3334
              else
3335
                abort ();
3336
            }
3337
 
3338
          value += irel->r_addend;
3339
 
3340
          /* See if the value will fit in 24 bits.
3341
             We allow any 16bit match here.  We prune those we can't
3342
             handle below.  */
3343
          if ((long) value < 0x7fffff && (long) value > -0x800000)
3344
            {
3345
              unsigned char code;
3346
 
3347
              /* AM33 insns which have 32bit operands are 7 bytes long and
3348
                 will have 0xfe as the first byte.  */
3349
 
3350
              /* Get the first opcode.  */
3351
              code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3352
 
3353
              if (code == 0xfe)
3354
                {
3355
                  /* Get the second opcode.  */
3356
                  code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3357
 
3358
                  /* All the am33 32 -> 24 relaxing possibilities.  */
3359
                  /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3360
                     equivalent instructions exists.  */
3361
                  if (code != 0x6b && code != 0x7b
3362
                      && code != 0x8b && code != 0x9b
3363
                      && (ELF32_R_TYPE (irel->r_info)
3364
                          != (int) R_MN10300_GOTPC32)
3365
                      && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3366
                          || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3367
                          || (code & 0x0f) == 0x0e))
3368
                    {
3369
                      /* Not safe if the high bit is on as relaxing may
3370
                         move the value out of high mem and thus not fit
3371
                         in a signed 16bit value.  This is currently over
3372
                         conservative.  */
3373
                      if ((value & 0x8000) == 0)
3374
                        {
3375
                          /* Note that we've changed the relocation contents,
3376
                             etc.  */
3377
                          elf_section_data (sec)->relocs = internal_relocs;
3378
                          elf_section_data (sec)->this_hdr.contents = contents;
3379
                          symtab_hdr->contents = (unsigned char *) isymbuf;
3380
 
3381
                          /* Fix the opcode.  */
3382
                          bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
3383
                          bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3384
 
3385
                          /* Fix the relocation's type.  */
3386
                          irel->r_info =
3387
                            ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3388
                                          (ELF32_R_TYPE (irel->r_info)
3389
                                           == (int) R_MN10300_GOTOFF32)
3390
                                          ? R_MN10300_GOTOFF24
3391
                                          : (ELF32_R_TYPE (irel->r_info)
3392
                                             == (int) R_MN10300_GOT32)
3393
                                          ? R_MN10300_GOT24 :
3394
                                          R_MN10300_24);
3395
 
3396
                          /* Delete one byte of data.  */
3397
                          if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3398
                                                               irel->r_offset + 3, 1))
3399
                            goto error_return;
3400
 
3401
                          /* That will change things, so, we should relax
3402
                             again.  Note that this is not required, and it
3403
                             may be slow.  */
3404
                          *again = TRUE;
3405
                          break;
3406
                        }
3407
                    }
3408
                }
3409
            }
3410
 
3411
          /* See if the value will fit in 16 bits.
3412
             We allow any 16bit match here.  We prune those we can't
3413
             handle below.  */
3414
          if ((long) value < 0x7fff && (long) value > -0x8000)
3415
            {
3416
              unsigned char code;
3417
 
3418
              /* Most insns which have 32bit operands are 6 bytes long;
3419
                 exceptions are pcrel insns and bit insns.
3420
 
3421
                 We handle pcrel insns above.  We don't bother trying
3422
                 to handle the bit insns here.
3423
 
3424
                 The first byte of the remaining insns will be 0xfc.  */
3425
 
3426
              /* Get the first opcode.  */
3427
              code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3428
 
3429
              if (code != 0xfc)
3430
                continue;
3431
 
3432
              /* Get the second opcode.  */
3433
              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3434
 
3435
              if ((code & 0xf0) < 0x80)
3436
                switch (code & 0xf0)
3437
                  {
3438
                  /* mov (d32,am),dn   -> mov (d32,am),dn
3439
                     mov dm,(d32,am)   -> mov dn,(d32,am)
3440
                     mov (d32,am),an   -> mov (d32,am),an
3441
                     mov dm,(d32,am)   -> mov dn,(d32,am)
3442
                     movbu (d32,am),dn -> movbu (d32,am),dn
3443
                     movbu dm,(d32,am) -> movbu dn,(d32,am)
3444
                     movhu (d32,am),dn -> movhu (d32,am),dn
3445
                     movhu dm,(d32,am) -> movhu dn,(d32,am) */
3446
                  case 0x00:
3447
                  case 0x10:
3448
                  case 0x20:
3449
                  case 0x30:
3450
                  case 0x40:
3451
                  case 0x50:
3452
                  case 0x60:
3453
                  case 0x70:
3454
                    /* Not safe if the high bit is on as relaxing may
3455
                       move the value out of high mem and thus not fit
3456
                       in a signed 16bit value.  */
3457
                    if (code == 0xcc
3458
                        && (value & 0x8000))
3459
                      continue;
3460
 
3461
                    /* Note that we've changed the relocation contents, etc.  */
3462
                    elf_section_data (sec)->relocs = internal_relocs;
3463
                    elf_section_data (sec)->this_hdr.contents = contents;
3464
                    symtab_hdr->contents = (unsigned char *) isymbuf;
3465
 
3466
                    /* Fix the opcode.  */
3467
                    bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3468
                    bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3469
 
3470
                    /* Fix the relocation's type.  */
3471
                    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3472
                                                 (ELF32_R_TYPE (irel->r_info)
3473
                                                  == (int) R_MN10300_GOTOFF32)
3474
                                                 ? R_MN10300_GOTOFF16
3475
                                                 : (ELF32_R_TYPE (irel->r_info)
3476
                                                    == (int) R_MN10300_GOT32)
3477
                                                 ? R_MN10300_GOT16
3478
                                                 : (ELF32_R_TYPE (irel->r_info)
3479
                                                    == (int) R_MN10300_GOTPC32)
3480
                                                 ? R_MN10300_GOTPC16 :
3481
                                                 R_MN10300_16);
3482
 
3483
                    /* Delete two bytes of data.  */
3484
                    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3485
                                                         irel->r_offset + 2, 2))
3486
                      goto error_return;
3487
 
3488
                    /* That will change things, so, we should relax again.
3489
                       Note that this is not required, and it may be slow.  */
3490
                    *again = TRUE;
3491
                    break;
3492
                  }
3493
              else if ((code & 0xf0) == 0x80
3494
                       || (code & 0xf0) == 0x90)
3495
                switch (code & 0xf3)
3496
                  {
3497
                  /* mov dn,(abs32)   -> mov dn,(abs16)
3498
                     movbu dn,(abs32) -> movbu dn,(abs16)
3499
                     movhu dn,(abs32) -> movhu dn,(abs16)  */
3500
                  case 0x81:
3501
                  case 0x82:
3502
                  case 0x83:
3503
                    /* Note that we've changed the relocation contents, etc.  */
3504
                    elf_section_data (sec)->relocs = internal_relocs;
3505
                    elf_section_data (sec)->this_hdr.contents = contents;
3506
                    symtab_hdr->contents = (unsigned char *) isymbuf;
3507
 
3508
                    if ((code & 0xf3) == 0x81)
3509
                      code = 0x01 + (code & 0x0c);
3510
                    else if ((code & 0xf3) == 0x82)
3511
                      code = 0x02 + (code & 0x0c);
3512
                    else if ((code & 0xf3) == 0x83)
3513
                      code = 0x03 + (code & 0x0c);
3514
                    else
3515
                      abort ();
3516
 
3517
                    /* Fix the opcode.  */
3518
                    bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3519
 
3520
                    /* Fix the relocation's type.  */
3521
                    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3522
                                                 (ELF32_R_TYPE (irel->r_info)
3523
                                                  == (int) R_MN10300_GOTOFF32)
3524
                                                 ? R_MN10300_GOTOFF16
3525
                                                 : (ELF32_R_TYPE (irel->r_info)
3526
                                                    == (int) R_MN10300_GOT32)
3527
                                                 ? R_MN10300_GOT16
3528
                                                 : (ELF32_R_TYPE (irel->r_info)
3529
                                                    == (int) R_MN10300_GOTPC32)
3530
                                                 ? R_MN10300_GOTPC16 :
3531
                                                 R_MN10300_16);
3532
 
3533
                    /* The opcode got shorter too, so we have to fix the
3534
                       addend and offset too!  */
3535
                    irel->r_offset -= 1;
3536
 
3537
                    /* Delete three bytes of data.  */
3538
                    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3539
                                                         irel->r_offset + 1, 3))
3540
                      goto error_return;
3541
 
3542
                    /* That will change things, so, we should relax again.
3543
                       Note that this is not required, and it may be slow.  */
3544
                    *again = TRUE;
3545
                    break;
3546
 
3547
                  /* mov am,(abs32)    -> mov am,(abs16)
3548
                     mov am,(d32,sp)   -> mov am,(d16,sp)
3549
                     mov dm,(d32,sp)   -> mov dm,(d32,sp)
3550
                     movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3551
                     movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3552
                  case 0x80:
3553
                  case 0x90:
3554
                  case 0x91:
3555
                  case 0x92:
3556
                  case 0x93:
3557
                    /* sp-based offsets are zero-extended.  */
3558
                    if (code >= 0x90 && code <= 0x93
3559
                        && (long) value < 0)
3560
                      continue;
3561
 
3562
                    /* Note that we've changed the relocation contents, etc.  */
3563
                    elf_section_data (sec)->relocs = internal_relocs;
3564
                    elf_section_data (sec)->this_hdr.contents = contents;
3565
                    symtab_hdr->contents = (unsigned char *) isymbuf;
3566
 
3567
                    /* Fix the opcode.  */
3568
                    bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3569
                    bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3570
 
3571
                    /* Fix the relocation's type.  */
3572
                    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3573
                                                 (ELF32_R_TYPE (irel->r_info)
3574
                                                  == (int) R_MN10300_GOTOFF32)
3575
                                                 ? R_MN10300_GOTOFF16
3576
                                                 : (ELF32_R_TYPE (irel->r_info)
3577
                                                    == (int) R_MN10300_GOT32)
3578
                                                 ? R_MN10300_GOT16
3579
                                                 : (ELF32_R_TYPE (irel->r_info)
3580
                                                    == (int) R_MN10300_GOTPC32)
3581
                                                 ? R_MN10300_GOTPC16 :
3582
                                                 R_MN10300_16);
3583
 
3584
                    /* Delete two bytes of data.  */
3585
                    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3586
                                                         irel->r_offset + 2, 2))
3587
                      goto error_return;
3588
 
3589
                    /* That will change things, so, we should relax again.
3590
                       Note that this is not required, and it may be slow.  */
3591
                    *again = TRUE;
3592
                    break;
3593
                  }
3594
              else if ((code & 0xf0) < 0xf0)
3595
                switch (code & 0xfc)
3596
                  {
3597
                  /* mov imm32,dn     -> mov imm16,dn
3598
                     mov imm32,an     -> mov imm16,an
3599
                     mov (abs32),dn   -> mov (abs16),dn
3600
                     movbu (abs32),dn -> movbu (abs16),dn
3601
                     movhu (abs32),dn -> movhu (abs16),dn  */
3602
                  case 0xcc:
3603
                  case 0xdc:
3604
                  case 0xa4:
3605
                  case 0xa8:
3606
                  case 0xac:
3607
                    /* Not safe if the high bit is on as relaxing may
3608
                       move the value out of high mem and thus not fit
3609
                       in a signed 16bit value.  */
3610
                    if (code == 0xcc
3611
                        && (value & 0x8000))
3612
                      continue;
3613
 
3614
                    /* mov imm16, an zero-extends the immediate.  */
3615
                    if (code == 0xdc
3616
                        && (long) value < 0)
3617
                      continue;
3618
 
3619
                    /* Note that we've changed the relocation contents, etc.  */
3620
                    elf_section_data (sec)->relocs = internal_relocs;
3621
                    elf_section_data (sec)->this_hdr.contents = contents;
3622
                    symtab_hdr->contents = (unsigned char *) isymbuf;
3623
 
3624
                    if ((code & 0xfc) == 0xcc)
3625
                      code = 0x2c + (code & 0x03);
3626
                    else if ((code & 0xfc) == 0xdc)
3627
                      code = 0x24 + (code & 0x03);
3628
                    else if ((code & 0xfc) == 0xa4)
3629
                      code = 0x30 + (code & 0x03);
3630
                    else if ((code & 0xfc) == 0xa8)
3631
                      code = 0x34 + (code & 0x03);
3632
                    else if ((code & 0xfc) == 0xac)
3633
                      code = 0x38 + (code & 0x03);
3634
                    else
3635
                      abort ();
3636
 
3637
                    /* Fix the opcode.  */
3638
                    bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3639
 
3640
                    /* Fix the relocation's type.  */
3641
                    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3642
                                                 (ELF32_R_TYPE (irel->r_info)
3643
                                                  == (int) R_MN10300_GOTOFF32)
3644
                                                 ? R_MN10300_GOTOFF16
3645
                                                 : (ELF32_R_TYPE (irel->r_info)
3646
                                                    == (int) R_MN10300_GOT32)
3647
                                                 ? R_MN10300_GOT16
3648
                                                 : (ELF32_R_TYPE (irel->r_info)
3649
                                                    == (int) R_MN10300_GOTPC32)
3650
                                                 ? R_MN10300_GOTPC16 :
3651
                                                 R_MN10300_16);
3652
 
3653
                    /* The opcode got shorter too, so we have to fix the
3654
                       addend and offset too!  */
3655
                    irel->r_offset -= 1;
3656
 
3657
                    /* Delete three bytes of data.  */
3658
                    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3659
                                                         irel->r_offset + 1, 3))
3660
                      goto error_return;
3661
 
3662
                    /* That will change things, so, we should relax again.
3663
                       Note that this is not required, and it may be slow.  */
3664
                    *again = TRUE;
3665
                    break;
3666
 
3667
                  /* mov (abs32),an    -> mov (abs16),an
3668
                     mov (d32,sp),an   -> mov (d16,sp),an
3669
                     mov (d32,sp),dn   -> mov (d16,sp),dn
3670
                     movbu (d32,sp),dn -> movbu (d16,sp),dn
3671
                     movhu (d32,sp),dn -> movhu (d16,sp),dn
3672
                     add imm32,dn      -> add imm16,dn
3673
                     cmp imm32,dn      -> cmp imm16,dn
3674
                     add imm32,an      -> add imm16,an
3675
                     cmp imm32,an      -> cmp imm16,an
3676
                     and imm32,dn      -> and imm16,dn
3677
                     or imm32,dn       -> or imm16,dn
3678
                     xor imm32,dn      -> xor imm16,dn
3679
                     btst imm32,dn     -> btst imm16,dn */
3680
 
3681
                  case 0xa0:
3682
                  case 0xb0:
3683
                  case 0xb1:
3684
                  case 0xb2:
3685
                  case 0xb3:
3686
                  case 0xc0:
3687
                  case 0xc8:
3688
 
3689
                  case 0xd0:
3690
                  case 0xd8:
3691
                  case 0xe0:
3692
                  case 0xe1:
3693
                  case 0xe2:
3694
                  case 0xe3:
3695
                    /* cmp imm16, an zero-extends the immediate.  */
3696
                    if (code == 0xdc
3697
                        && (long) value < 0)
3698
                      continue;
3699
 
3700
                    /* So do sp-based offsets.  */
3701
                    if (code >= 0xb0 && code <= 0xb3
3702
                        && (long) value < 0)
3703
                      continue;
3704
 
3705
                    /* Note that we've changed the relocation contents, etc.  */
3706
                    elf_section_data (sec)->relocs = internal_relocs;
3707
                    elf_section_data (sec)->this_hdr.contents = contents;
3708
                    symtab_hdr->contents = (unsigned char *) isymbuf;
3709
 
3710
                    /* Fix the opcode.  */
3711
                    bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3712
                    bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3713
 
3714
                    /* Fix the relocation's type.  */
3715
                    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3716
                                                 (ELF32_R_TYPE (irel->r_info)
3717
                                                  == (int) R_MN10300_GOTOFF32)
3718
                                                 ? R_MN10300_GOTOFF16
3719
                                                 : (ELF32_R_TYPE (irel->r_info)
3720
                                                    == (int) R_MN10300_GOT32)
3721
                                                 ? R_MN10300_GOT16
3722
                                                 : (ELF32_R_TYPE (irel->r_info)
3723
                                                    == (int) R_MN10300_GOTPC32)
3724
                                                 ? R_MN10300_GOTPC16 :
3725
                                                 R_MN10300_16);
3726
 
3727
                    /* Delete two bytes of data.  */
3728
                    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3729
                                                         irel->r_offset + 2, 2))
3730
                      goto error_return;
3731
 
3732
                    /* That will change things, so, we should relax again.
3733
                       Note that this is not required, and it may be slow.  */
3734
                    *again = TRUE;
3735
                    break;
3736
                  }
3737
              else if (code == 0xfe)
3738
                {
3739
                  /* add imm32,sp -> add imm16,sp  */
3740
 
3741
                  /* Note that we've changed the relocation contents, etc.  */
3742
                  elf_section_data (sec)->relocs = internal_relocs;
3743
                  elf_section_data (sec)->this_hdr.contents = contents;
3744
                  symtab_hdr->contents = (unsigned char *) isymbuf;
3745
 
3746
                  /* Fix the opcode.  */
3747
                  bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3748
                  bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
3749
 
3750
                  /* Fix the relocation's type.  */
3751
                  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3752
                                               (ELF32_R_TYPE (irel->r_info)
3753
                                                == (int) R_MN10300_GOT32)
3754
                                               ? R_MN10300_GOT16
3755
                                               : (ELF32_R_TYPE (irel->r_info)
3756
                                                  == (int) R_MN10300_GOTOFF32)
3757
                                               ? R_MN10300_GOTOFF16
3758
                                               : (ELF32_R_TYPE (irel->r_info)
3759
                                                  == (int) R_MN10300_GOTPC32)
3760
                                               ? R_MN10300_GOTPC16 :
3761
                                               R_MN10300_16);
3762
 
3763
                  /* Delete two bytes of data.  */
3764
                  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3765
                                                       irel->r_offset + 2, 2))
3766
                    goto error_return;
3767
 
3768
                  /* That will change things, so, we should relax again.
3769
                     Note that this is not required, and it may be slow.  */
3770
                  *again = TRUE;
3771
                  break;
3772
                }
3773
            }
3774
        }
3775
    }
3776
 
3777
  if (isymbuf != NULL
3778
      && symtab_hdr->contents != (unsigned char *) isymbuf)
3779
    {
3780
      if (! link_info->keep_memory)
3781
        free (isymbuf);
3782
      else
3783
        {
3784
          /* Cache the symbols for elf_link_input_bfd.  */
3785
          symtab_hdr->contents = (unsigned char *) isymbuf;
3786
        }
3787
    }
3788
 
3789
  if (contents != NULL
3790
      && elf_section_data (sec)->this_hdr.contents != contents)
3791
    {
3792
      if (! link_info->keep_memory)
3793
        free (contents);
3794
      else
3795
        {
3796
          /* Cache the section contents for elf_link_input_bfd.  */
3797
          elf_section_data (sec)->this_hdr.contents = contents;
3798
        }
3799
    }
3800
 
3801
  if (internal_relocs != NULL
3802
      && elf_section_data (sec)->relocs != internal_relocs)
3803
    free (internal_relocs);
3804
 
3805
  return TRUE;
3806
 
3807
 error_return:
3808
  if (isymbuf != NULL
3809
      && symtab_hdr->contents != (unsigned char *) isymbuf)
3810
    free (isymbuf);
3811
  if (contents != NULL
3812
      && elf_section_data (section)->this_hdr.contents != contents)
3813
    free (contents);
3814
  if (internal_relocs != NULL
3815
      && elf_section_data (section)->relocs != internal_relocs)
3816
    free (internal_relocs);
3817
 
3818
  return FALSE;
3819
}
3820
 
3821
/* This is a version of bfd_generic_get_relocated_section_contents
3822
   which uses mn10300_elf_relocate_section.  */
3823
 
3824
static bfd_byte *
3825
mn10300_elf_get_relocated_section_contents (bfd *output_bfd,
3826
                                            struct bfd_link_info *link_info,
3827
                                            struct bfd_link_order *link_order,
3828
                                            bfd_byte *data,
3829
                                            bfd_boolean relocatable,
3830
                                            asymbol **symbols)
3831
{
3832
  Elf_Internal_Shdr *symtab_hdr;
3833
  asection *input_section = link_order->u.indirect.section;
3834
  bfd *input_bfd = input_section->owner;
3835
  asection **sections = NULL;
3836
  Elf_Internal_Rela *internal_relocs = NULL;
3837
  Elf_Internal_Sym *isymbuf = NULL;
3838
 
3839
  /* We only need to handle the case of relaxing, or of having a
3840
     particular set of section contents, specially.  */
3841
  if (relocatable
3842
      || elf_section_data (input_section)->this_hdr.contents == NULL)
3843
    return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3844
                                                       link_order, data,
3845
                                                       relocatable,
3846
                                                       symbols);
3847
 
3848
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3849
 
3850
  memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3851
          (size_t) input_section->size);
3852
 
3853
  if ((input_section->flags & SEC_RELOC) != 0
3854
      && input_section->reloc_count > 0)
3855
    {
3856
      asection **secpp;
3857
      Elf_Internal_Sym *isym, *isymend;
3858
      bfd_size_type amt;
3859
 
3860
      internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
3861
                                                   NULL, NULL, FALSE);
3862
      if (internal_relocs == NULL)
3863
        goto error_return;
3864
 
3865
      if (symtab_hdr->sh_info != 0)
3866
        {
3867
          isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3868
          if (isymbuf == NULL)
3869
            isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3870
                                            symtab_hdr->sh_info, 0,
3871
                                            NULL, NULL, NULL);
3872
          if (isymbuf == NULL)
3873
            goto error_return;
3874
        }
3875
 
3876
      amt = symtab_hdr->sh_info;
3877
      amt *= sizeof (asection *);
3878
      sections = bfd_malloc (amt);
3879
      if (sections == NULL && amt != 0)
3880
        goto error_return;
3881
 
3882
      isymend = isymbuf + symtab_hdr->sh_info;
3883
      for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3884
        {
3885
          asection *isec;
3886
 
3887
          if (isym->st_shndx == SHN_UNDEF)
3888
            isec = bfd_und_section_ptr;
3889
          else if (isym->st_shndx == SHN_ABS)
3890
            isec = bfd_abs_section_ptr;
3891
          else if (isym->st_shndx == SHN_COMMON)
3892
            isec = bfd_com_section_ptr;
3893
          else
3894
            isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3895
 
3896
          *secpp = isec;
3897
        }
3898
 
3899
      if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
3900
                                          input_section, data, internal_relocs,
3901
                                          isymbuf, sections))
3902
        goto error_return;
3903
 
3904
      if (sections != NULL)
3905
        free (sections);
3906
      if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3907
        free (isymbuf);
3908
      if (internal_relocs != elf_section_data (input_section)->relocs)
3909
        free (internal_relocs);
3910
    }
3911
 
3912
  return data;
3913
 
3914
 error_return:
3915
  if (sections != NULL)
3916
    free (sections);
3917
  if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3918
    free (isymbuf);
3919
  if (internal_relocs != NULL
3920
      && internal_relocs != elf_section_data (input_section)->relocs)
3921
    free (internal_relocs);
3922
  return NULL;
3923
}
3924
 
3925
/* Assorted hash table functions.  */
3926
 
3927
/* Initialize an entry in the link hash table.  */
3928
 
3929
/* Create an entry in an MN10300 ELF linker hash table.  */
3930
 
3931
static struct bfd_hash_entry *
3932
elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry *entry,
3933
                                 struct bfd_hash_table *table,
3934
                                 const char *string)
3935
{
3936
  struct elf32_mn10300_link_hash_entry *ret =
3937
    (struct elf32_mn10300_link_hash_entry *) entry;
3938
 
3939
  /* Allocate the structure if it has not already been allocated by a
3940
     subclass.  */
3941
  if (ret == NULL)
3942
    ret = (struct elf32_mn10300_link_hash_entry *)
3943
           bfd_hash_allocate (table, sizeof (* ret));
3944
  if (ret == NULL)
3945
    return (struct bfd_hash_entry *) ret;
3946
 
3947
  /* Call the allocation method of the superclass.  */
3948
  ret = (struct elf32_mn10300_link_hash_entry *)
3949
         _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3950
                                     table, string);
3951
  if (ret != NULL)
3952
    {
3953
      ret->direct_calls = 0;
3954
      ret->stack_size = 0;
3955
      ret->movm_args = 0;
3956
      ret->movm_stack_size = 0;
3957
      ret->flags = 0;
3958
      ret->value = 0;
3959
    }
3960
 
3961
  return (struct bfd_hash_entry *) ret;
3962
}
3963
 
3964
/* Create an mn10300 ELF linker hash table.  */
3965
 
3966
static struct bfd_link_hash_table *
3967
elf32_mn10300_link_hash_table_create (bfd *abfd)
3968
{
3969
  struct elf32_mn10300_link_hash_table *ret;
3970
  bfd_size_type amt = sizeof (* ret);
3971
 
3972
  ret = bfd_malloc (amt);
3973
  if (ret == NULL)
3974
    return NULL;
3975
 
3976
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3977
                                      elf32_mn10300_link_hash_newfunc,
3978
                                      sizeof (struct elf32_mn10300_link_hash_entry),
3979
                                      MN10300_ELF_DATA))
3980
    {
3981
      free (ret);
3982
      return NULL;
3983
    }
3984
 
3985
  ret->flags = 0;
3986
  amt = sizeof (struct elf_link_hash_table);
3987
  ret->static_hash_table = bfd_malloc (amt);
3988
  if (ret->static_hash_table == NULL)
3989
    {
3990
      free (ret);
3991
      return NULL;
3992
    }
3993
 
3994
  if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
3995
                                      elf32_mn10300_link_hash_newfunc,
3996
                                      sizeof (struct elf32_mn10300_link_hash_entry),
3997
                                      MN10300_ELF_DATA))
3998
    {
3999
      free (ret->static_hash_table);
4000
      free (ret);
4001
      return NULL;
4002
    }
4003
  return & ret->root.root;
4004
}
4005
 
4006
/* Free an mn10300 ELF linker hash table.  */
4007
 
4008
static void
4009
elf32_mn10300_link_hash_table_free (struct bfd_link_hash_table *hash)
4010
{
4011
  struct elf32_mn10300_link_hash_table *ret
4012
    = (struct elf32_mn10300_link_hash_table *) hash;
4013
 
4014
  _bfd_generic_link_hash_table_free
4015
    ((struct bfd_link_hash_table *) ret->static_hash_table);
4016
  _bfd_generic_link_hash_table_free
4017
    ((struct bfd_link_hash_table *) ret);
4018
}
4019
 
4020
static unsigned long
4021
elf_mn10300_mach (flagword flags)
4022
{
4023
  switch (flags & EF_MN10300_MACH)
4024
    {
4025
    case E_MN10300_MACH_MN10300:
4026
    default:
4027
      return bfd_mach_mn10300;
4028
 
4029
    case E_MN10300_MACH_AM33:
4030
      return bfd_mach_am33;
4031
 
4032
    case E_MN10300_MACH_AM33_2:
4033
      return bfd_mach_am33_2;
4034
    }
4035
}
4036
 
4037
/* The final processing done just before writing out a MN10300 ELF object
4038
   file.  This gets the MN10300 architecture right based on the machine
4039
   number.  */
4040
 
4041
static void
4042
_bfd_mn10300_elf_final_write_processing (bfd *abfd,
4043
                                         bfd_boolean linker ATTRIBUTE_UNUSED)
4044
{
4045
  unsigned long val;
4046
 
4047
  switch (bfd_get_mach (abfd))
4048
    {
4049
    default:
4050
    case bfd_mach_mn10300:
4051
      val = E_MN10300_MACH_MN10300;
4052
      break;
4053
 
4054
    case bfd_mach_am33:
4055
      val = E_MN10300_MACH_AM33;
4056
      break;
4057
 
4058
    case bfd_mach_am33_2:
4059
      val = E_MN10300_MACH_AM33_2;
4060
      break;
4061
    }
4062
 
4063
  elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
4064
  elf_elfheader (abfd)->e_flags |= val;
4065
}
4066
 
4067
static bfd_boolean
4068
_bfd_mn10300_elf_object_p (bfd *abfd)
4069
{
4070
  bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
4071
                             elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
4072
  return TRUE;
4073
}
4074
 
4075
/* Merge backend specific data from an object file to the output
4076
   object file when linking.  */
4077
 
4078
static bfd_boolean
4079
_bfd_mn10300_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4080
{
4081
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4082
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4083
    return TRUE;
4084
 
4085
  if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4086
      && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
4087
    {
4088
      if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4089
                               bfd_get_mach (ibfd)))
4090
        return FALSE;
4091
    }
4092
 
4093
  return TRUE;
4094
}
4095
 
4096
#define PLT0_ENTRY_SIZE     15
4097
#define PLT_ENTRY_SIZE      20
4098
#define PIC_PLT_ENTRY_SIZE  24
4099
 
4100
static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
4101
{
4102
  0xfc, 0xa0, 0, 0, 0, 0,   /* mov  (.got+8),a0 */
4103
  0xfe, 0xe, 0x10, 0, 0, 0, 0,      /* mov  (.got+4),r1 */
4104
  0xf0, 0xf4,                   /* jmp  (a0) */
4105
};
4106
 
4107
static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
4108
{
4109
  0xfc, 0xa0, 0, 0, 0, 0,   /* mov  (nameN@GOT + .got),a0 */
4110
  0xf0, 0xf4,                   /* jmp  (a0) */
4111
  0xfe, 8, 0, 0, 0, 0, 0,    /* mov  reloc-table-address,r0 */
4112
  0xdc, 0, 0, 0, 0,         /* jmp  .plt0 */
4113
};
4114
 
4115
static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
4116
{
4117
  0xfc, 0x22, 0, 0, 0, 0,   /* mov  (nameN@GOT,a2),a0 */
4118
  0xf0, 0xf4,                   /* jmp  (a0) */
4119
  0xfe, 8, 0, 0, 0, 0, 0,    /* mov  reloc-table-address,r0 */
4120
  0xf8, 0x22, 8,                /* mov  (8,a2),a0 */
4121
  0xfb, 0xa, 0x1a, 4,           /* mov  (4,a2),r1 */
4122
  0xf0, 0xf4,                   /* jmp  (a0) */
4123
};
4124
 
4125
/* Return size of the first PLT entry.  */
4126
#define elf_mn10300_sizeof_plt0(info) \
4127
  (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4128
 
4129
/* Return size of a PLT entry.  */
4130
#define elf_mn10300_sizeof_plt(info) \
4131
  (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4132
 
4133
/* Return offset of the PLT0 address in an absolute PLT entry.  */
4134
#define elf_mn10300_plt_plt0_offset(info) 16
4135
 
4136
/* Return offset of the linker in PLT0 entry.  */
4137
#define elf_mn10300_plt0_linker_offset(info) 2
4138
 
4139
/* Return offset of the GOT id in PLT0 entry.  */
4140
#define elf_mn10300_plt0_gotid_offset(info) 9
4141
 
4142
/* Return offset of the temporary in PLT entry.  */
4143
#define elf_mn10300_plt_temp_offset(info) 8
4144
 
4145
/* Return offset of the symbol in PLT entry.  */
4146
#define elf_mn10300_plt_symbol_offset(info) 2
4147
 
4148
/* Return offset of the relocation in PLT entry.  */
4149
#define elf_mn10300_plt_reloc_offset(info) 11
4150
 
4151
/* The name of the dynamic interpreter.  This is put in the .interp
4152
   section.  */
4153
 
4154
#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4155
 
4156
/* Create dynamic sections when linking against a dynamic object.  */
4157
 
4158
static bfd_boolean
4159
_bfd_mn10300_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4160
{
4161
  flagword   flags;
4162
  asection * s;
4163
  const struct elf_backend_data * bed = get_elf_backend_data (abfd);
4164
  int ptralign = 0;
4165
 
4166
  switch (bed->s->arch_size)
4167
    {
4168
    case 32:
4169
      ptralign = 2;
4170
      break;
4171
 
4172
    case 64:
4173
      ptralign = 3;
4174
      break;
4175
 
4176
    default:
4177
      bfd_set_error (bfd_error_bad_value);
4178
      return FALSE;
4179
    }
4180
 
4181
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4182
     .rel[a].bss sections.  */
4183
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4184
           | SEC_LINKER_CREATED);
4185
 
4186
  s = bfd_make_section_with_flags (abfd,
4187
                                   (bed->default_use_rela_p
4188
                                    ? ".rela.plt" : ".rel.plt"),
4189
                                   flags | SEC_READONLY);
4190
  if (s == NULL
4191
      || ! bfd_set_section_alignment (abfd, s, ptralign))
4192
    return FALSE;
4193
 
4194
  if (! _bfd_mn10300_elf_create_got_section (abfd, info))
4195
    return FALSE;
4196
 
4197
  {
4198
    const char * secname;
4199
    char *       relname;
4200
    flagword     secflags;
4201
    asection *   sec;
4202
 
4203
    for (sec = abfd->sections; sec; sec = sec->next)
4204
      {
4205
        secflags = bfd_get_section_flags (abfd, sec);
4206
        if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
4207
            || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
4208
          continue;
4209
 
4210
        secname = bfd_get_section_name (abfd, sec);
4211
        relname = bfd_malloc (strlen (secname) + 6);
4212
        strcpy (relname, ".rela");
4213
        strcat (relname, secname);
4214
 
4215
        s = bfd_make_section_with_flags (abfd, relname,
4216
                                         flags | SEC_READONLY);
4217
        if (s == NULL
4218
            || ! bfd_set_section_alignment (abfd, s, ptralign))
4219
          return FALSE;
4220
      }
4221
  }
4222
 
4223
  if (bed->want_dynbss)
4224
    {
4225
      /* The .dynbss section is a place to put symbols which are defined
4226
         by dynamic objects, are referenced by regular objects, and are
4227
         not functions.  We must allocate space for them in the process
4228
         image and use a R_*_COPY reloc to tell the dynamic linker to
4229
         initialize them at run time.  The linker script puts the .dynbss
4230
         section into the .bss section of the final image.  */
4231
      s = bfd_make_section_with_flags (abfd, ".dynbss",
4232
                                       SEC_ALLOC | SEC_LINKER_CREATED);
4233
      if (s == NULL)
4234
        return FALSE;
4235
 
4236
      /* The .rel[a].bss section holds copy relocs.  This section is not
4237
         normally needed.  We need to create it here, though, so that the
4238
         linker will map it to an output section.  We can't just create it
4239
         only if we need it, because we will not know whether we need it
4240
         until we have seen all the input files, and the first time the
4241
         main linker code calls BFD after examining all the input files
4242
         (size_dynamic_sections) the input sections have already been
4243
         mapped to the output sections.  If the section turns out not to
4244
         be needed, we can discard it later.  We will never need this
4245
         section when generating a shared object, since they do not use
4246
         copy relocs.  */
4247
      if (! info->shared)
4248
        {
4249
          s = bfd_make_section_with_flags (abfd,
4250
                                           (bed->default_use_rela_p
4251
                                            ? ".rela.bss" : ".rel.bss"),
4252
                                           flags | SEC_READONLY);
4253
          if (s == NULL
4254
              || ! bfd_set_section_alignment (abfd, s, ptralign))
4255
            return FALSE;
4256
        }
4257
    }
4258
 
4259
  return TRUE;
4260
}
4261
 
4262
/* Adjust a symbol defined by a dynamic object and referenced by a
4263
   regular object.  The current definition is in some section of the
4264
   dynamic object, but we're not including those sections.  We have to
4265
   change the definition to something the rest of the link can
4266
   understand.  */
4267
 
4268
static bfd_boolean
4269
_bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
4270
                                        struct elf_link_hash_entry * h)
4271
{
4272
  bfd * dynobj;
4273
  asection * s;
4274
 
4275
  dynobj = elf_hash_table (info)->dynobj;
4276
 
4277
  /* Make sure we know what is going on here.  */
4278
  BFD_ASSERT (dynobj != NULL
4279
              && (h->needs_plt
4280
                  || h->u.weakdef != NULL
4281
                  || (h->def_dynamic
4282
                      && h->ref_regular
4283
                      && !h->def_regular)));
4284
 
4285
  /* If this is a function, put it in the procedure linkage table.  We
4286
     will fill in the contents of the procedure linkage table later,
4287
     when we know the address of the .got section.  */
4288
  if (h->type == STT_FUNC
4289
      || h->needs_plt)
4290
    {
4291
      if (! info->shared
4292
          && !h->def_dynamic
4293
          && !h->ref_dynamic)
4294
        {
4295
          /* This case can occur if we saw a PLT reloc in an input
4296
             file, but the symbol was never referred to by a dynamic
4297
             object.  In such a case, we don't actually need to build
4298
             a procedure linkage table, and we can just do a REL32
4299
             reloc instead.  */
4300
          BFD_ASSERT (h->needs_plt);
4301
          return TRUE;
4302
        }
4303
 
4304
      /* Make sure this symbol is output as a dynamic symbol.  */
4305
      if (h->dynindx == -1)
4306
        {
4307
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
4308
            return FALSE;
4309
        }
4310
 
4311
      s = bfd_get_section_by_name (dynobj, ".plt");
4312
      BFD_ASSERT (s != NULL);
4313
 
4314
      /* If this is the first .plt entry, make room for the special
4315
         first entry.  */
4316
      if (s->size == 0)
4317
        s->size += elf_mn10300_sizeof_plt0 (info);
4318
 
4319
      /* If this symbol is not defined in a regular file, and we are
4320
         not generating a shared library, then set the symbol to this
4321
         location in the .plt.  This is required to make function
4322
         pointers compare as equal between the normal executable and
4323
         the shared library.  */
4324
      if (! info->shared
4325
          && !h->def_regular)
4326
        {
4327
          h->root.u.def.section = s;
4328
          h->root.u.def.value = s->size;
4329
        }
4330
 
4331
      h->plt.offset = s->size;
4332
 
4333
      /* Make room for this entry.  */
4334
      s->size += elf_mn10300_sizeof_plt (info);
4335
 
4336
      /* We also need to make an entry in the .got.plt section, which
4337
         will be placed in the .got section by the linker script.  */
4338
      s = bfd_get_section_by_name (dynobj, ".got.plt");
4339
      BFD_ASSERT (s != NULL);
4340
      s->size += 4;
4341
 
4342
      /* We also need to make an entry in the .rela.plt section.  */
4343
      s = bfd_get_section_by_name (dynobj, ".rela.plt");
4344
      BFD_ASSERT (s != NULL);
4345
      s->size += sizeof (Elf32_External_Rela);
4346
 
4347
      return TRUE;
4348
    }
4349
 
4350
  /* If this is a weak symbol, and there is a real definition, the
4351
     processor independent code will have arranged for us to see the
4352
     real definition first, and we can just use the same value.  */
4353
  if (h->u.weakdef != NULL)
4354
    {
4355
      BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4356
                  || h->u.weakdef->root.type == bfd_link_hash_defweak);
4357
      h->root.u.def.section = h->u.weakdef->root.u.def.section;
4358
      h->root.u.def.value = h->u.weakdef->root.u.def.value;
4359
      return TRUE;
4360
    }
4361
 
4362
  /* This is a reference to a symbol defined by a dynamic object which
4363
     is not a function.  */
4364
 
4365
  /* If we are creating a shared library, we must presume that the
4366
     only references to the symbol are via the global offset table.
4367
     For such cases we need not do anything here; the relocations will
4368
     be handled correctly by relocate_section.  */
4369
  if (info->shared)
4370
    return TRUE;
4371
 
4372
  /* If there are no references to this symbol that do not use the
4373
     GOT, we don't need to generate a copy reloc.  */
4374
  if (!h->non_got_ref)
4375
    return TRUE;
4376
 
4377
  if (h->size == 0)
4378
    {
4379
      (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
4380
                             h->root.root.string);
4381
      return TRUE;
4382
    }
4383
 
4384
  /* We must allocate the symbol in our .dynbss section, which will
4385
     become part of the .bss section of the executable.  There will be
4386
     an entry for this symbol in the .dynsym section.  The dynamic
4387
     object will contain position independent code, so all references
4388
     from the dynamic object to this symbol will go through the global
4389
     offset table.  The dynamic linker will use the .dynsym entry to
4390
     determine the address it must put in the global offset table, so
4391
     both the dynamic object and the regular object will refer to the
4392
     same memory location for the variable.  */
4393
 
4394
  s = bfd_get_section_by_name (dynobj, ".dynbss");
4395
  BFD_ASSERT (s != NULL);
4396
 
4397
  /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4398
     copy the initial value out of the dynamic object and into the
4399
     runtime process image.  We need to remember the offset into the
4400
     .rela.bss section we are going to use.  */
4401
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4402
    {
4403
      asection * srel;
4404
 
4405
      srel = bfd_get_section_by_name (dynobj, ".rela.bss");
4406
      BFD_ASSERT (srel != NULL);
4407
      srel->size += sizeof (Elf32_External_Rela);
4408
      h->needs_copy = 1;
4409
    }
4410
 
4411
  return _bfd_elf_adjust_dynamic_copy (h, s);
4412
}
4413
 
4414
/* Set the sizes of the dynamic sections.  */
4415
 
4416
static bfd_boolean
4417
_bfd_mn10300_elf_size_dynamic_sections (bfd * output_bfd,
4418
                                        struct bfd_link_info * info)
4419
{
4420
  bfd * dynobj;
4421
  asection * s;
4422
  bfd_boolean plt;
4423
  bfd_boolean relocs;
4424
  bfd_boolean reltext;
4425
 
4426
  dynobj = elf_hash_table (info)->dynobj;
4427
  BFD_ASSERT (dynobj != NULL);
4428
 
4429
  if (elf_hash_table (info)->dynamic_sections_created)
4430
    {
4431
      /* Set the contents of the .interp section to the interpreter.  */
4432
      if (info->executable)
4433
        {
4434
          s = bfd_get_section_by_name (dynobj, ".interp");
4435
          BFD_ASSERT (s != NULL);
4436
          s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4437
          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4438
        }
4439
    }
4440
  else
4441
    {
4442
      /* We may have created entries in the .rela.got section.
4443
         However, if we are not creating the dynamic sections, we will
4444
         not actually use these entries.  Reset the size of .rela.got,
4445
         which will cause it to get stripped from the output file
4446
         below.  */
4447
      s = bfd_get_section_by_name (dynobj, ".rela.got");
4448
      if (s != NULL)
4449
        s->size = 0;
4450
    }
4451
 
4452
  /* The check_relocs and adjust_dynamic_symbol entry points have
4453
     determined the sizes of the various dynamic sections.  Allocate
4454
     memory for them.  */
4455
  plt = FALSE;
4456
  relocs = FALSE;
4457
  reltext = FALSE;
4458
  for (s = dynobj->sections; s != NULL; s = s->next)
4459
    {
4460
      const char * name;
4461
 
4462
      if ((s->flags & SEC_LINKER_CREATED) == 0)
4463
        continue;
4464
 
4465
      /* It's OK to base decisions on the section name, because none
4466
         of the dynobj section names depend upon the input files.  */
4467
      name = bfd_get_section_name (dynobj, s);
4468
 
4469
      if (streq (name, ".plt"))
4470
        {
4471
          /* Remember whether there is a PLT.  */
4472
          plt = s->size != 0;
4473
        }
4474
      else if (CONST_STRNEQ (name, ".rela"))
4475
        {
4476
          if (s->size != 0)
4477
            {
4478
              asection * target;
4479
 
4480
              /* Remember whether there are any reloc sections other
4481
                 than .rela.plt.  */
4482
              if (! streq (name, ".rela.plt"))
4483
                {
4484
                  const char * outname;
4485
 
4486
                  relocs = TRUE;
4487
 
4488
                  /* If this relocation section applies to a read only
4489
                     section, then we probably need a DT_TEXTREL
4490
                     entry.  The entries in the .rela.plt section
4491
                     really apply to the .got section, which we
4492
                     created ourselves and so know is not readonly.  */
4493
                  outname = bfd_get_section_name (output_bfd,
4494
                                                  s->output_section);
4495
                  target = bfd_get_section_by_name (output_bfd, outname + 5);
4496
                  if (target != NULL
4497
                      && (target->flags & SEC_READONLY) != 0
4498
                      && (target->flags & SEC_ALLOC) != 0)
4499
                    reltext = TRUE;
4500
                }
4501
 
4502
              /* We use the reloc_count field as a counter if we need
4503
                 to copy relocs into the output file.  */
4504
              s->reloc_count = 0;
4505
            }
4506
        }
4507
      else if (! CONST_STRNEQ (name, ".got")
4508
               && ! streq (name, ".dynbss"))
4509
        /* It's not one of our sections, so don't allocate space.  */
4510
        continue;
4511
 
4512
      if (s->size == 0)
4513
        {
4514
          /* If we don't need this section, strip it from the
4515
             output file.  This is mostly to handle .rela.bss and
4516
             .rela.plt.  We must create both sections in
4517
             create_dynamic_sections, because they must be created
4518
             before the linker maps input sections to output
4519
             sections.  The linker does that before
4520
             adjust_dynamic_symbol is called, and it is that
4521
             function which decides whether anything needs to go
4522
             into these sections.  */
4523
          s->flags |= SEC_EXCLUDE;
4524
          continue;
4525
        }
4526
 
4527
        if ((s->flags & SEC_HAS_CONTENTS) == 0)
4528
          continue;
4529
 
4530
      /* Allocate memory for the section contents.  We use bfd_zalloc
4531
         here in case unused entries are not reclaimed before the
4532
         section's contents are written out.  This should not happen,
4533
         but this way if it does, we get a R_MN10300_NONE reloc
4534
         instead of garbage.  */
4535
      s->contents = bfd_zalloc (dynobj, s->size);
4536
      if (s->contents == NULL)
4537
        return FALSE;
4538
    }
4539
 
4540
  if (elf_hash_table (info)->dynamic_sections_created)
4541
    {
4542
      /* Add some entries to the .dynamic section.  We fill in the
4543
         values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4544
         but we must add the entries now so that we get the correct
4545
         size for the .dynamic section.  The DT_DEBUG entry is filled
4546
         in by the dynamic linker and used by the debugger.  */
4547
      if (! info->shared)
4548
        {
4549
          if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
4550
            return FALSE;
4551
        }
4552
 
4553
      if (plt)
4554
        {
4555
          if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
4556
              || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
4557
              || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
4558
              || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
4559
            return FALSE;
4560
        }
4561
 
4562
      if (relocs)
4563
        {
4564
          if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
4565
              || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
4566
              || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
4567
                                              sizeof (Elf32_External_Rela)))
4568
            return FALSE;
4569
        }
4570
 
4571
      if (reltext)
4572
        {
4573
          if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
4574
            return FALSE;
4575
        }
4576
    }
4577
 
4578
  return TRUE;
4579
}
4580
 
4581
/* Finish up dynamic symbol handling.  We set the contents of various
4582
   dynamic sections here.  */
4583
 
4584
static bfd_boolean
4585
_bfd_mn10300_elf_finish_dynamic_symbol (bfd * output_bfd,
4586
                                        struct bfd_link_info * info,
4587
                                        struct elf_link_hash_entry * h,
4588
                                        Elf_Internal_Sym * sym)
4589
{
4590
  bfd * dynobj;
4591
 
4592
  dynobj = elf_hash_table (info)->dynobj;
4593
 
4594
  if (h->plt.offset != (bfd_vma) -1)
4595
    {
4596
      asection *        splt;
4597
      asection *        sgot;
4598
      asection *        srel;
4599
      bfd_vma           plt_index;
4600
      bfd_vma           got_offset;
4601
      Elf_Internal_Rela rel;
4602
 
4603
      /* This symbol has an entry in the procedure linkage table.  Set
4604
         it up.  */
4605
 
4606
      BFD_ASSERT (h->dynindx != -1);
4607
 
4608
      splt = bfd_get_section_by_name (dynobj, ".plt");
4609
      sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4610
      srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4611
      BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4612
 
4613
      /* Get the index in the procedure linkage table which
4614
         corresponds to this symbol.  This is the index of this symbol
4615
         in all the symbols for which we are making plt entries.  The
4616
         first entry in the procedure linkage table is reserved.  */
4617
      plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
4618
                   / elf_mn10300_sizeof_plt (info));
4619
 
4620
      /* Get the offset into the .got table of the entry that
4621
         corresponds to this function.  Each .got entry is 4 bytes.
4622
         The first three are reserved.  */
4623
      got_offset = (plt_index + 3) * 4;
4624
 
4625
      /* Fill in the entry in the procedure linkage table.  */
4626
      if (! info->shared)
4627
        {
4628
          memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
4629
                  elf_mn10300_sizeof_plt (info));
4630
          bfd_put_32 (output_bfd,
4631
                      (sgot->output_section->vma
4632
                       + sgot->output_offset
4633
                       + got_offset),
4634
                      (splt->contents + h->plt.offset
4635
                       + elf_mn10300_plt_symbol_offset (info)));
4636
 
4637
          bfd_put_32 (output_bfd,
4638
                      (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
4639
                      (splt->contents + h->plt.offset
4640
                       + elf_mn10300_plt_plt0_offset (info)));
4641
        }
4642
      else
4643
        {
4644
          memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
4645
                  elf_mn10300_sizeof_plt (info));
4646
 
4647
          bfd_put_32 (output_bfd, got_offset,
4648
                      (splt->contents + h->plt.offset
4649
                       + elf_mn10300_plt_symbol_offset (info)));
4650
        }
4651
 
4652
      bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4653
                  (splt->contents + h->plt.offset
4654
                   + elf_mn10300_plt_reloc_offset (info)));
4655
 
4656
      /* Fill in the entry in the global offset table.  */
4657
      bfd_put_32 (output_bfd,
4658
                  (splt->output_section->vma
4659
                   + splt->output_offset
4660
                   + h->plt.offset
4661
                   + elf_mn10300_plt_temp_offset (info)),
4662
                  sgot->contents + got_offset);
4663
 
4664
      /* Fill in the entry in the .rela.plt section.  */
4665
      rel.r_offset = (sgot->output_section->vma
4666
                      + sgot->output_offset
4667
                      + got_offset);
4668
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
4669
      rel.r_addend = 0;
4670
      bfd_elf32_swap_reloca_out (output_bfd, &rel,
4671
                                 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4672
                                               + plt_index));
4673
 
4674
      if (!h->def_regular)
4675
        /* Mark the symbol as undefined, rather than as defined in
4676
           the .plt section.  Leave the value alone.  */
4677
        sym->st_shndx = SHN_UNDEF;
4678
    }
4679
 
4680
  if (h->got.offset != (bfd_vma) -1)
4681
    {
4682
      asection *        sgot;
4683
      asection *        srel;
4684
      Elf_Internal_Rela rel;
4685
 
4686
      /* This symbol has an entry in the global offset table.  Set it up.  */
4687
      sgot = bfd_get_section_by_name (dynobj, ".got");
4688
      srel = bfd_get_section_by_name (dynobj, ".rela.got");
4689
      BFD_ASSERT (sgot != NULL && srel != NULL);
4690
 
4691
      rel.r_offset = (sgot->output_section->vma
4692
                      + sgot->output_offset
4693
                      + (h->got.offset & ~1));
4694
 
4695
      /* If this is a -Bsymbolic link, and the symbol is defined
4696
         locally, we just want to emit a RELATIVE reloc.  Likewise if
4697
         the symbol was forced to be local because of a version file.
4698
         The entry in the global offset table will already have been
4699
         initialized in the relocate_section function.  */
4700
      if (info->shared
4701
          && (info->symbolic || h->dynindx == -1)
4702
          && h->def_regular)
4703
        {
4704
          rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
4705
          rel.r_addend = (h->root.u.def.value
4706
                          + h->root.u.def.section->output_section->vma
4707
                          + h->root.u.def.section->output_offset);
4708
        }
4709
      else
4710
        {
4711
          bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4712
          rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
4713
          rel.r_addend = 0;
4714
        }
4715
 
4716
      bfd_elf32_swap_reloca_out (output_bfd, &rel,
4717
                                 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4718
                                               + srel->reloc_count));
4719
      ++ srel->reloc_count;
4720
    }
4721
 
4722
  if (h->needs_copy)
4723
    {
4724
      asection *        s;
4725
      Elf_Internal_Rela rel;
4726
 
4727
      /* This symbol needs a copy reloc.  Set it up.  */
4728
      BFD_ASSERT (h->dynindx != -1
4729
                  && (h->root.type == bfd_link_hash_defined
4730
                      || h->root.type == bfd_link_hash_defweak));
4731
 
4732
      s = bfd_get_section_by_name (h->root.u.def.section->owner,
4733
                                   ".rela.bss");
4734
      BFD_ASSERT (s != NULL);
4735
 
4736
      rel.r_offset = (h->root.u.def.value
4737
                      + h->root.u.def.section->output_section->vma
4738
                      + h->root.u.def.section->output_offset);
4739
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
4740
      rel.r_addend = 0;
4741
      bfd_elf32_swap_reloca_out (output_bfd, & rel,
4742
                                 (bfd_byte *) ((Elf32_External_Rela *) s->contents
4743
                                               + s->reloc_count));
4744
      ++ s->reloc_count;
4745
    }
4746
 
4747
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
4748
  if (streq (h->root.root.string, "_DYNAMIC")
4749
      || h == elf_hash_table (info)->hgot)
4750
    sym->st_shndx = SHN_ABS;
4751
 
4752
  return TRUE;
4753
}
4754
 
4755
/* Finish up the dynamic sections.  */
4756
 
4757
static bfd_boolean
4758
_bfd_mn10300_elf_finish_dynamic_sections (bfd * output_bfd,
4759
                                          struct bfd_link_info * info)
4760
{
4761
  bfd *      dynobj;
4762
  asection * sgot;
4763
  asection * sdyn;
4764
 
4765
  dynobj = elf_hash_table (info)->dynobj;
4766
 
4767
  sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4768
  BFD_ASSERT (sgot != NULL);
4769
  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4770
 
4771
  if (elf_hash_table (info)->dynamic_sections_created)
4772
    {
4773
      asection *           splt;
4774
      Elf32_External_Dyn * dyncon;
4775
      Elf32_External_Dyn * dynconend;
4776
 
4777
      BFD_ASSERT (sdyn != NULL);
4778
 
4779
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
4780
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4781
 
4782
      for (; dyncon < dynconend; dyncon++)
4783
        {
4784
          Elf_Internal_Dyn dyn;
4785
          const char * name;
4786
          asection * s;
4787
 
4788
          bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4789
 
4790
          switch (dyn.d_tag)
4791
            {
4792
            default:
4793
              break;
4794
 
4795
            case DT_PLTGOT:
4796
              name = ".got";
4797
              goto get_vma;
4798
 
4799
            case DT_JMPREL:
4800
              name = ".rela.plt";
4801
            get_vma:
4802
              s = bfd_get_section_by_name (output_bfd, name);
4803
              BFD_ASSERT (s != NULL);
4804
              dyn.d_un.d_ptr = s->vma;
4805
              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4806
              break;
4807
 
4808
            case DT_PLTRELSZ:
4809
              s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4810
              BFD_ASSERT (s != NULL);
4811
              dyn.d_un.d_val = s->size;
4812
              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4813
              break;
4814
 
4815
            case DT_RELASZ:
4816
              /* My reading of the SVR4 ABI indicates that the
4817
                 procedure linkage table relocs (DT_JMPREL) should be
4818
                 included in the overall relocs (DT_RELA).  This is
4819
                 what Solaris does.  However, UnixWare can not handle
4820
                 that case.  Therefore, we override the DT_RELASZ entry
4821
                 here to make it not include the JMPREL relocs.  Since
4822
                 the linker script arranges for .rela.plt to follow all
4823
                 other relocation sections, we don't have to worry
4824
                 about changing the DT_RELA entry.  */
4825
              s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4826
              if (s != NULL)
4827
                dyn.d_un.d_val -= s->size;
4828
              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4829
              break;
4830
            }
4831
        }
4832
 
4833
      /* Fill in the first entry in the procedure linkage table.  */
4834
      splt = bfd_get_section_by_name (dynobj, ".plt");
4835
      if (splt && splt->size > 0)
4836
        {
4837
          if (info->shared)
4838
            {
4839
              memcpy (splt->contents, elf_mn10300_pic_plt_entry,
4840
                      elf_mn10300_sizeof_plt (info));
4841
            }
4842
          else
4843
            {
4844
              memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
4845
              bfd_put_32 (output_bfd,
4846
                          sgot->output_section->vma + sgot->output_offset + 4,
4847
                          splt->contents + elf_mn10300_plt0_gotid_offset (info));
4848
              bfd_put_32 (output_bfd,
4849
                          sgot->output_section->vma + sgot->output_offset + 8,
4850
                          splt->contents + elf_mn10300_plt0_linker_offset (info));
4851
            }
4852
 
4853
          /* UnixWare sets the entsize of .plt to 4, although that doesn't
4854
             really seem like the right value.  */
4855
          elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4856
        }
4857
    }
4858
 
4859
  /* Fill in the first three entries in the global offset table.  */
4860
  if (sgot->size > 0)
4861
    {
4862
      if (sdyn == NULL)
4863
        bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4864
      else
4865
        bfd_put_32 (output_bfd,
4866
                    sdyn->output_section->vma + sdyn->output_offset,
4867
                    sgot->contents);
4868
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4869
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4870
    }
4871
 
4872
  elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4873
 
4874
  return TRUE;
4875
}
4876
 
4877
/* Classify relocation types, such that combreloc can sort them
4878
   properly.  */
4879
 
4880
static enum elf_reloc_type_class
4881
_bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
4882
{
4883
  switch ((int) ELF32_R_TYPE (rela->r_info))
4884
    {
4885
    case R_MN10300_RELATIVE:    return reloc_class_relative;
4886
    case R_MN10300_JMP_SLOT:    return reloc_class_plt;
4887
    case R_MN10300_COPY:        return reloc_class_copy;
4888
    default:                    return reloc_class_normal;
4889
    }
4890
}
4891
 
4892
#ifndef ELF_ARCH
4893
#define TARGET_LITTLE_SYM       bfd_elf32_mn10300_vec
4894
#define TARGET_LITTLE_NAME      "elf32-mn10300"
4895
#define ELF_ARCH                bfd_arch_mn10300
4896
#define ELF_MACHINE_CODE        EM_MN10300
4897
#define ELF_MACHINE_ALT1        EM_CYGNUS_MN10300
4898
#define ELF_MAXPAGESIZE         0x1000
4899
#endif
4900
 
4901
#define elf_info_to_howto               mn10300_info_to_howto
4902
#define elf_info_to_howto_rel           0
4903
#define elf_backend_can_gc_sections     1
4904
#define elf_backend_rela_normal         1
4905
#define elf_backend_check_relocs        mn10300_elf_check_relocs
4906
#define elf_backend_gc_mark_hook        mn10300_elf_gc_mark_hook
4907
#define elf_backend_relocate_section    mn10300_elf_relocate_section
4908
#define bfd_elf32_bfd_relax_section     mn10300_elf_relax_section
4909
#define bfd_elf32_bfd_get_relocated_section_contents \
4910
                                mn10300_elf_get_relocated_section_contents
4911
#define bfd_elf32_bfd_link_hash_table_create \
4912
                                elf32_mn10300_link_hash_table_create
4913
#define bfd_elf32_bfd_link_hash_table_free \
4914
                                elf32_mn10300_link_hash_table_free
4915
 
4916
#ifndef elf_symbol_leading_char
4917
#define elf_symbol_leading_char '_'
4918
#endif
4919
 
4920
/* So we can set bits in e_flags.  */
4921
#define elf_backend_final_write_processing \
4922
                                        _bfd_mn10300_elf_final_write_processing
4923
#define elf_backend_object_p            _bfd_mn10300_elf_object_p
4924
 
4925
#define bfd_elf32_bfd_merge_private_bfd_data \
4926
                                        _bfd_mn10300_elf_merge_private_bfd_data
4927
 
4928
#define elf_backend_can_gc_sections     1
4929
#define elf_backend_create_dynamic_sections \
4930
  _bfd_mn10300_elf_create_dynamic_sections
4931
#define elf_backend_adjust_dynamic_symbol \
4932
  _bfd_mn10300_elf_adjust_dynamic_symbol
4933
#define elf_backend_size_dynamic_sections \
4934
  _bfd_mn10300_elf_size_dynamic_sections
4935
#define elf_backend_omit_section_dynsym \
4936
  ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4937
#define elf_backend_finish_dynamic_symbol \
4938
  _bfd_mn10300_elf_finish_dynamic_symbol
4939
#define elf_backend_finish_dynamic_sections \
4940
  _bfd_mn10300_elf_finish_dynamic_sections
4941
 
4942
#define elf_backend_reloc_type_class \
4943
  _bfd_mn10300_elf_reloc_type_class
4944
 
4945
#define elf_backend_want_got_plt        1
4946
#define elf_backend_plt_readonly        1
4947
#define elf_backend_want_plt_sym        0
4948
#define elf_backend_got_header_size     12
4949
 
4950
#include "elf32-target.h"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.