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