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