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