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