Commit | Line | Data |
---|---|---|
b3c0fc57 | 1 | /* MIPS-specific support for 32-bit ELF |
9783e04a | 2 | Copyright 1993, 1994 Free Software Foundation, Inc. |
6b4b4d17 | 3 | |
b3c0fc57 ILT |
4 | Most of the information added by Ian Lance Taylor, Cygnus Support, |
5 | <ian@cygnus.com>. | |
6 | ||
6b4b4d17 JK |
7 | This file is part of BFD, the Binary File Descriptor library. |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
22 | ||
23 | #include "bfd.h" | |
24 | #include "sysdep.h" | |
25 | #include "libbfd.h" | |
6e07e54f ILT |
26 | #include "bfdlink.h" |
27 | #include "genlink.h" | |
6b4b4d17 | 28 | #include "libelf.h" |
b3c0fc57 ILT |
29 | #include "elf/mips.h" |
30 | ||
6e07e54f ILT |
31 | /* Get the ECOFF swapping routines. */ |
32 | #include "coff/sym.h" | |
33 | #include "coff/symconst.h" | |
34 | #include "coff/internal.h" | |
35 | #include "coff/ecoff.h" | |
36 | #include "coff/mips.h" | |
37 | #define ECOFF_32 | |
38 | #include "ecoffswap.h" | |
39 | ||
b3c0fc57 ILT |
40 | static bfd_reloc_status_type mips_elf_hi16_reloc PARAMS ((bfd *abfd, |
41 | arelent *reloc, | |
42 | asymbol *symbol, | |
43 | PTR data, | |
44 | asection *section, | |
6e07e54f ILT |
45 | bfd *output_bfd, |
46 | char **error)); | |
47 | static bfd_reloc_status_type mips_elf_got16_reloc PARAMS ((bfd *abfd, | |
48 | arelent *reloc, | |
49 | asymbol *symbol, | |
50 | PTR data, | |
51 | asection *section, | |
52 | bfd *output_bfd, | |
53 | char **error)); | |
b3c0fc57 ILT |
54 | static bfd_reloc_status_type mips_elf_lo16_reloc PARAMS ((bfd *abfd, |
55 | arelent *reloc, | |
56 | asymbol *symbol, | |
57 | PTR data, | |
58 | asection *section, | |
6e07e54f ILT |
59 | bfd *output_bfd, |
60 | char **error)); | |
b3c0fc57 ILT |
61 | static bfd_reloc_status_type mips_elf_gprel16_reloc PARAMS ((bfd *abfd, |
62 | arelent *reloc, | |
63 | asymbol *symbol, | |
64 | PTR data, | |
65 | asection *section, | |
6e07e54f ILT |
66 | bfd *output_bfd, |
67 | char **error)); | |
aac6b32f ILT |
68 | static const struct reloc_howto_struct *bfd_elf32_bfd_reloc_type_lookup |
69 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
70 | static void mips_info_to_howto_rel | |
71 | PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *)); | |
72 | static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *)); | |
73 | static boolean mips_elf_object_p PARAMS ((bfd *)); | |
74 | static void mips_elf_final_write_processing PARAMS ((bfd *)); | |
75 | static boolean mips_elf_section_from_shdr | |
76 | PARAMS ((bfd *, Elf32_Internal_Shdr *, char *)); | |
77 | static boolean mips_elf_fake_sections | |
78 | PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *)); | |
79 | static boolean mips_elf_section_from_bfd_section | |
80 | PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *, int *)); | |
81 | static boolean mips_elf_section_processing | |
82 | PARAMS ((bfd *, Elf32_Internal_Shdr *)); | |
83 | static void mips_elf_symbol_processing PARAMS ((bfd *, asymbol *)); | |
84 | static boolean mips_elf_read_ecoff_info | |
85 | PARAMS ((bfd *, asection *, struct ecoff_debug_info *)); | |
86 | static struct bfd_hash_entry *mips_elf_link_hash_newfunc | |
87 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); | |
88 | static struct bfd_link_hash_table *mips_elf_link_hash_table_create | |
89 | PARAMS ((bfd *)); | |
90 | static int gptab_compare PARAMS ((const void *, const void *)); | |
91 | static boolean mips_elf_final_link | |
92 | PARAMS ((bfd *, struct bfd_link_info *)); | |
93 | static void mips_elf_relocate_hi16 | |
94 | PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *, | |
95 | bfd_vma)); | |
96 | static boolean mips_elf_relocate_section | |
97 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
98 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); | |
99 | static boolean mips_elf_add_symbol_hook | |
100 | PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *, | |
101 | const char **, flagword *, asection **, bfd_vma *)); | |
b3c0fc57 ILT |
102 | |
103 | #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */ | |
104 | ||
105 | enum reloc_type | |
106 | { | |
107 | R_MIPS_NONE = 0, | |
108 | R_MIPS_16, R_MIPS_32, | |
109 | R_MIPS_REL32, R_MIPS_26, | |
110 | R_MIPS_HI16, R_MIPS_LO16, | |
111 | R_MIPS_GPREL16, R_MIPS_LITERAL, | |
112 | R_MIPS_GOT16, R_MIPS_PC16, | |
113 | R_MIPS_CALL16, R_MIPS_GPREL32, | |
114 | R_MIPS_max | |
115 | }; | |
116 | ||
117 | static reloc_howto_type elf_mips_howto_table[] = | |
118 | { | |
119 | /* No relocation. */ | |
120 | HOWTO (R_MIPS_NONE, /* type */ | |
121 | 0, /* rightshift */ | |
122 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
123 | 0, /* bitsize */ | |
124 | false, /* pc_relative */ | |
125 | 0, /* bitpos */ | |
126 | complain_overflow_dont, /* complain_on_overflow */ | |
127 | bfd_elf_generic_reloc, /* special_function */ | |
128 | "R_MIPS_NONE", /* name */ | |
129 | false, /* partial_inplace */ | |
130 | 0, /* src_mask */ | |
131 | 0, /* dst_mask */ | |
132 | false), /* pcrel_offset */ | |
133 | ||
134 | /* 16 bit relocation. */ | |
135 | HOWTO (R_MIPS_16, /* type */ | |
136 | 0, /* rightshift */ | |
137 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
138 | 16, /* bitsize */ | |
139 | false, /* pc_relative */ | |
140 | 0, /* bitpos */ | |
141 | complain_overflow_bitfield, /* complain_on_overflow */ | |
142 | bfd_elf_generic_reloc, /* special_function */ | |
143 | "R_MIPS_16", /* name */ | |
144 | true, /* partial_inplace */ | |
145 | 0xffff, /* src_mask */ | |
146 | 0xffff, /* dst_mask */ | |
147 | false), /* pcrel_offset */ | |
148 | ||
149 | /* 32 bit relocation. */ | |
150 | HOWTO (R_MIPS_32, /* type */ | |
151 | 0, /* rightshift */ | |
152 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
153 | 32, /* bitsize */ | |
154 | false, /* pc_relative */ | |
155 | 0, /* bitpos */ | |
156 | complain_overflow_bitfield, /* complain_on_overflow */ | |
157 | bfd_elf_generic_reloc, /* special_function */ | |
158 | "R_MIPS_32", /* name */ | |
159 | true, /* partial_inplace */ | |
160 | 0xffffffff, /* src_mask */ | |
161 | 0xffffffff, /* dst_mask */ | |
162 | false), /* pcrel_offset */ | |
163 | ||
164 | /* 32 bit symbol relative relocation. */ | |
165 | HOWTO (R_MIPS_REL32, /* type */ | |
166 | 0, /* rightshift */ | |
167 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
168 | 32, /* bitsize */ | |
169 | false, /* pc_relative */ | |
170 | 0, /* bitpos */ | |
171 | complain_overflow_bitfield, /* complain_on_overflow */ | |
172 | bfd_elf_generic_reloc, /* special_function */ | |
173 | "R_MIPS_REL32", /* name */ | |
174 | true, /* partial_inplace */ | |
175 | 0xffffffff, /* src_mask */ | |
176 | 0xffffffff, /* dst_mask */ | |
177 | false), /* pcrel_offset */ | |
178 | ||
179 | /* 26 bit branch address. */ | |
180 | HOWTO (R_MIPS_26, /* type */ | |
181 | 2, /* rightshift */ | |
182 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
183 | 26, /* bitsize */ | |
184 | false, /* pc_relative */ | |
185 | 0, /* bitpos */ | |
6e07e54f ILT |
186 | complain_overflow_dont, /* complain_on_overflow */ |
187 | /* This needs complex overflow | |
188 | detection, because the upper four | |
189 | bits must match the PC. */ | |
b3c0fc57 ILT |
190 | bfd_elf_generic_reloc, /* special_function */ |
191 | "R_MIPS_26", /* name */ | |
192 | true, /* partial_inplace */ | |
193 | 0x3ffffff, /* src_mask */ | |
194 | 0x3ffffff, /* dst_mask */ | |
195 | false), /* pcrel_offset */ | |
196 | ||
197 | /* High 16 bits of symbol value. */ | |
198 | HOWTO (R_MIPS_HI16, /* type */ | |
199 | 0, /* rightshift */ | |
200 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
201 | 16, /* bitsize */ | |
202 | false, /* pc_relative */ | |
203 | 0, /* bitpos */ | |
204 | complain_overflow_dont, /* complain_on_overflow */ | |
205 | mips_elf_hi16_reloc, /* special_function */ | |
206 | "R_MIPS_HI16", /* name */ | |
207 | true, /* partial_inplace */ | |
208 | 0xffff, /* src_mask */ | |
209 | 0xffff, /* dst_mask */ | |
210 | false), /* pcrel_offset */ | |
211 | ||
212 | /* Low 16 bits of symbol value. */ | |
213 | HOWTO (R_MIPS_LO16, /* type */ | |
214 | 0, /* rightshift */ | |
215 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
216 | 16, /* bitsize */ | |
217 | false, /* pc_relative */ | |
218 | 0, /* bitpos */ | |
219 | complain_overflow_dont, /* complain_on_overflow */ | |
220 | mips_elf_lo16_reloc, /* special_function */ | |
221 | "R_MIPS_LO16", /* name */ | |
222 | true, /* partial_inplace */ | |
223 | 0xffff, /* src_mask */ | |
224 | 0xffff, /* dst_mask */ | |
225 | false), /* pcrel_offset */ | |
226 | ||
227 | /* GP relative reference. */ | |
228 | HOWTO (R_MIPS_GPREL16, /* type */ | |
229 | 0, /* rightshift */ | |
230 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
231 | 16, /* bitsize */ | |
232 | false, /* pc_relative */ | |
233 | 0, /* bitpos */ | |
234 | complain_overflow_signed, /* complain_on_overflow */ | |
235 | mips_elf_gprel16_reloc, /* special_function */ | |
236 | "R_MIPS_GPREL16", /* name */ | |
237 | true, /* partial_inplace */ | |
238 | 0xffff, /* src_mask */ | |
239 | 0xffff, /* dst_mask */ | |
240 | false), /* pcrel_offset */ | |
241 | ||
242 | /* Reference to literal section. */ | |
243 | HOWTO (R_MIPS_LITERAL, /* type */ | |
244 | 0, /* rightshift */ | |
245 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
246 | 16, /* bitsize */ | |
247 | false, /* pc_relative */ | |
248 | 0, /* bitpos */ | |
249 | complain_overflow_signed, /* complain_on_overflow */ | |
250 | mips_elf_gprel16_reloc, /* special_function */ | |
251 | "R_MIPS_LITERAL", /* name */ | |
252 | true, /* partial_inplace */ | |
253 | 0xffff, /* src_mask */ | |
254 | 0xffff, /* dst_mask */ | |
255 | false), /* pcrel_offset */ | |
256 | ||
257 | /* Reference to global offset table. */ | |
258 | /* FIXME: This is not handled correctly. */ | |
259 | HOWTO (R_MIPS_GOT16, /* type */ | |
260 | 0, /* rightshift */ | |
261 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
262 | 16, /* bitsize */ | |
263 | false, /* pc_relative */ | |
264 | 0, /* bitpos */ | |
265 | complain_overflow_signed, /* complain_on_overflow */ | |
6e07e54f | 266 | mips_elf_got16_reloc, /* special_function */ |
b3c0fc57 ILT |
267 | "R_MIPS_GOT16", /* name */ |
268 | false, /* partial_inplace */ | |
269 | 0, /* src_mask */ | |
270 | 0xffff, /* dst_mask */ | |
271 | false), /* pcrel_offset */ | |
272 | ||
273 | /* 16 bit PC relative reference. */ | |
274 | HOWTO (R_MIPS_PC16, /* type */ | |
275 | 0, /* rightshift */ | |
276 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
277 | 16, /* bitsize */ | |
278 | true, /* pc_relative */ | |
279 | 0, /* bitpos */ | |
280 | complain_overflow_signed, /* complain_on_overflow */ | |
281 | bfd_elf_generic_reloc, /* special_function */ | |
282 | "R_MIPS_PC16", /* name */ | |
283 | true, /* partial_inplace */ | |
284 | 0xffff, /* src_mask */ | |
285 | 0xffff, /* dst_mask */ | |
286 | false), /* pcrel_offset */ | |
287 | ||
288 | /* 16 bit call through global offset table. */ | |
289 | /* FIXME: This is not handled correctly. */ | |
290 | HOWTO (R_MIPS_CALL16, /* type */ | |
291 | 0, /* rightshift */ | |
292 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
293 | 16, /* bitsize */ | |
294 | false, /* pc_relative */ | |
295 | 0, /* bitpos */ | |
296 | complain_overflow_signed, /* complain_on_overflow */ | |
297 | bfd_elf_generic_reloc, /* special_function */ | |
298 | "R_MIPS_CALL16", /* name */ | |
299 | false, /* partial_inplace */ | |
300 | 0, /* src_mask */ | |
301 | 0xffff, /* dst_mask */ | |
302 | false), /* pcrel_offset */ | |
303 | ||
304 | /* 32 bit GP relative reference. */ | |
305 | /* FIXME: This is not handled correctly. */ | |
306 | HOWTO (R_MIPS_GPREL32, /* type */ | |
307 | 0, /* rightshift */ | |
308 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
309 | 32, /* bitsize */ | |
310 | false, /* pc_relative */ | |
311 | 0, /* bitpos */ | |
312 | complain_overflow_bitfield, /* complain_on_overflow */ | |
313 | bfd_elf_generic_reloc, /* special_function */ | |
314 | "R_MIPS_GPREL32", /* name */ | |
315 | true, /* partial_inplace */ | |
316 | 0xffffffff, /* src_mask */ | |
317 | 0xffffffff, /* dst_mask */ | |
318 | false) /* pcrel_offset */ | |
319 | }; | |
320 | ||
321 | /* Do a R_MIPS_HI16 relocation. This has to be done in combination | |
322 | with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to | |
323 | the HI16. Here we just save the information we need; we do the | |
324 | actual relocation when we see the LO16. MIPS ELF requires that the | |
325 | LO16 immediately follow the HI16, so this ought to work. */ | |
326 | ||
327 | static bfd_byte *mips_hi16_addr; | |
328 | static bfd_vma mips_hi16_addend; | |
329 | ||
330 | static bfd_reloc_status_type | |
331 | mips_elf_hi16_reloc (abfd, | |
332 | reloc_entry, | |
333 | symbol, | |
334 | data, | |
335 | input_section, | |
6e07e54f ILT |
336 | output_bfd, |
337 | error_message) | |
b3c0fc57 ILT |
338 | bfd *abfd; |
339 | arelent *reloc_entry; | |
340 | asymbol *symbol; | |
341 | PTR data; | |
342 | asection *input_section; | |
343 | bfd *output_bfd; | |
6e07e54f | 344 | char **error_message; |
b3c0fc57 ILT |
345 | { |
346 | bfd_reloc_status_type ret; | |
347 | bfd_vma relocation; | |
348 | ||
b3c0fc57 ILT |
349 | /* If we're relocating, and this an external symbol, we don't want |
350 | to change anything. */ | |
351 | if (output_bfd != (bfd *) NULL | |
352 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
353 | && reloc_entry->addend == 0) | |
354 | { | |
355 | reloc_entry->address += input_section->output_offset; | |
356 | return bfd_reloc_ok; | |
357 | } | |
358 | ||
6e07e54f ILT |
359 | /* FIXME: The symbol _gp_disp requires special handling, which we do |
360 | not do. */ | |
361 | if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0) | |
362 | abort (); | |
363 | ||
b3c0fc57 ILT |
364 | ret = bfd_reloc_ok; |
365 | if (symbol->section == &bfd_und_section | |
366 | && output_bfd == (bfd *) NULL) | |
367 | ret = bfd_reloc_undefined; | |
368 | ||
369 | if (bfd_is_com_section (symbol->section)) | |
370 | relocation = 0; | |
371 | else | |
372 | relocation = symbol->value; | |
373 | ||
374 | relocation += symbol->section->output_section->vma; | |
375 | relocation += symbol->section->output_offset; | |
376 | relocation += reloc_entry->addend; | |
377 | ||
378 | if (reloc_entry->address > input_section->_cooked_size) | |
379 | return bfd_reloc_outofrange; | |
380 | ||
381 | /* Save the information, and let LO16 do the actual relocation. */ | |
382 | mips_hi16_addr = (bfd_byte *) data + reloc_entry->address; | |
383 | mips_hi16_addend = relocation; | |
384 | ||
385 | if (output_bfd != (bfd *) NULL) | |
386 | reloc_entry->address += input_section->output_offset; | |
387 | ||
388 | return ret; | |
389 | } | |
390 | ||
391 | /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit | |
392 | inplace relocation; this function exists in order to do the | |
393 | R_MIPS_HI16 relocation described above. */ | |
394 | ||
395 | static bfd_reloc_status_type | |
396 | mips_elf_lo16_reloc (abfd, | |
397 | reloc_entry, | |
398 | symbol, | |
399 | data, | |
400 | input_section, | |
6e07e54f ILT |
401 | output_bfd, |
402 | error_message) | |
b3c0fc57 ILT |
403 | bfd *abfd; |
404 | arelent *reloc_entry; | |
405 | asymbol *symbol; | |
406 | PTR data; | |
407 | asection *input_section; | |
408 | bfd *output_bfd; | |
6e07e54f | 409 | char **error_message; |
b3c0fc57 ILT |
410 | { |
411 | /* FIXME: The symbol _gp_disp requires special handling, which we do | |
412 | not do. */ | |
6e07e54f ILT |
413 | if (output_bfd == (bfd *) NULL |
414 | && strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0) | |
b3c0fc57 ILT |
415 | abort (); |
416 | ||
417 | if (mips_hi16_addr != (bfd_byte *) NULL) | |
418 | { | |
419 | unsigned long insn; | |
420 | unsigned long val; | |
421 | unsigned long vallo; | |
422 | ||
423 | /* Do the HI16 relocation. Note that we actually don't need to | |
424 | know anything about the LO16 itself, except where to find the | |
425 | low 16 bits of the addend needed by the LO16. */ | |
426 | insn = bfd_get_32 (abfd, mips_hi16_addr); | |
427 | vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address) | |
428 | & 0xffff); | |
429 | val = ((insn & 0xffff) << 16) + vallo; | |
430 | val += mips_hi16_addend; | |
431 | ||
432 | /* The low order 16 bits are always treated as a signed value. | |
433 | Therefore, a negative value in the low order bits requires an | |
434 | adjustment in the high order bits. We need to make this | |
435 | adjustment in two ways: once for the bits we took from the | |
436 | data, and once for the bits we are putting back in to the | |
437 | data. */ | |
438 | if ((vallo & 0x8000) != 0) | |
439 | val -= 0x10000; | |
440 | if ((val & 0x8000) != 0) | |
441 | val += 0x10000; | |
442 | ||
443 | insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff); | |
444 | bfd_put_32 (abfd, insn, mips_hi16_addr); | |
445 | ||
446 | mips_hi16_addr = (bfd_byte *) NULL; | |
447 | } | |
448 | ||
449 | /* Now do the LO16 reloc in the usual way. */ | |
450 | return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data, | |
6e07e54f ILT |
451 | input_section, output_bfd, error_message); |
452 | } | |
453 | ||
454 | /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset | |
455 | table used for PIC code. If the symbol is an external symbol, the | |
456 | instruction is modified to contain the offset of the appropriate | |
457 | entry in the global offset table. If the symbol is a section | |
458 | symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit | |
459 | addends are combined to form the real addend against the section | |
460 | symbol; the GOT16 is modified to contain the offset of an entry in | |
461 | the global offset table, and the LO16 is modified to offset it | |
462 | appropriately. Thus an offset larger than 16 bits requires a | |
463 | modified value in the global offset table. | |
464 | ||
465 | This implementation suffices for the assembler, but the linker does | |
466 | not yet know how to create global offset tables. */ | |
467 | ||
468 | static bfd_reloc_status_type | |
469 | mips_elf_got16_reloc (abfd, | |
470 | reloc_entry, | |
471 | symbol, | |
472 | data, | |
473 | input_section, | |
474 | output_bfd, | |
475 | error_message) | |
476 | bfd *abfd; | |
477 | arelent *reloc_entry; | |
478 | asymbol *symbol; | |
479 | PTR data; | |
480 | asection *input_section; | |
481 | bfd *output_bfd; | |
482 | char **error_message; | |
483 | { | |
484 | /* If we're relocating, and this an external symbol, we don't want | |
485 | to change anything. */ | |
486 | if (output_bfd != (bfd *) NULL | |
487 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
488 | && reloc_entry->addend == 0) | |
489 | { | |
490 | reloc_entry->address += input_section->output_offset; | |
491 | return bfd_reloc_ok; | |
492 | } | |
493 | ||
494 | /* If we're relocating, and this is a local symbol, we can handle it | |
495 | just like HI16. */ | |
496 | if (output_bfd != (bfd *) NULL | |
497 | && (symbol->flags & BSF_SECTION_SYM) != 0) | |
498 | return mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data, | |
499 | input_section, output_bfd, error_message); | |
500 | ||
501 | abort (); | |
b3c0fc57 ILT |
502 | } |
503 | ||
504 | /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must | |
505 | become the offset from the gp register. This function also handles | |
506 | R_MIPS_LITERAL relocations, although those can be handled more | |
507 | cleverly because the entries in the .lit8 and .lit4 sections can be | |
508 | merged. */ | |
509 | ||
510 | static bfd_reloc_status_type | |
511 | mips_elf_gprel16_reloc (abfd, | |
512 | reloc_entry, | |
513 | symbol, | |
514 | data, | |
515 | input_section, | |
6e07e54f ILT |
516 | output_bfd, |
517 | error_message) | |
b3c0fc57 ILT |
518 | bfd *abfd; |
519 | arelent *reloc_entry; | |
520 | asymbol *symbol; | |
521 | PTR data; | |
522 | asection *input_section; | |
523 | bfd *output_bfd; | |
6e07e54f | 524 | char **error_message; |
b3c0fc57 ILT |
525 | { |
526 | boolean relocateable; | |
527 | bfd_vma relocation; | |
528 | unsigned long val; | |
529 | unsigned long insn; | |
530 | ||
531 | /* If we're relocating, and this is an external symbol with no | |
532 | addend, we don't want to change anything. We will only have an | |
533 | addend if this is a newly created reloc, not read from an ELF | |
534 | file. */ | |
535 | if (output_bfd != (bfd *) NULL | |
536 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
537 | && reloc_entry->addend == 0) | |
538 | { | |
539 | reloc_entry->address += input_section->output_offset; | |
540 | return bfd_reloc_ok; | |
541 | } | |
542 | ||
543 | if (output_bfd != (bfd *) NULL) | |
544 | relocateable = true; | |
545 | else | |
546 | { | |
547 | relocateable = false; | |
548 | output_bfd = symbol->section->output_section->owner; | |
549 | } | |
550 | ||
551 | if (symbol->section == &bfd_und_section | |
552 | && relocateable == false) | |
553 | return bfd_reloc_undefined; | |
554 | ||
555 | /* We have to figure out the gp value, so that we can adjust the | |
556 | symbol value correctly. We look up the symbol _gp in the output | |
557 | BFD. If we can't find it, we're stuck. We cache it in the ELF | |
558 | target data. We don't need to adjust the symbol value for an | |
559 | external symbol if we are producing relocateable output. */ | |
560 | if (elf_gp (output_bfd) == 0 | |
561 | && (relocateable == false | |
562 | || (symbol->flags & BSF_SECTION_SYM) != 0)) | |
563 | { | |
564 | if (relocateable != false) | |
565 | { | |
566 | /* Make up a value. */ | |
567 | elf_gp (output_bfd) = | |
568 | symbol->section->output_section->vma + 0x4000; | |
569 | } | |
570 | else | |
571 | { | |
572 | unsigned int count; | |
573 | asymbol **sym; | |
574 | unsigned int i; | |
575 | ||
576 | count = bfd_get_symcount (output_bfd); | |
577 | sym = bfd_get_outsymbols (output_bfd); | |
578 | ||
579 | if (sym == (asymbol **) NULL) | |
580 | i = count; | |
581 | else | |
582 | { | |
583 | for (i = 0; i < count; i++, sym++) | |
584 | { | |
585 | register CONST char *name; | |
586 | ||
587 | name = bfd_asymbol_name (*sym); | |
588 | if (*name == '_' && strcmp (name, "_gp") == 0) | |
589 | { | |
590 | elf_gp (output_bfd) = bfd_asymbol_value (*sym); | |
591 | break; | |
592 | } | |
593 | } | |
594 | } | |
595 | ||
596 | if (i >= count) | |
597 | { | |
598 | /* Only get the error once. */ | |
599 | elf_gp (output_bfd) = 4; | |
6e07e54f ILT |
600 | *error_message = |
601 | (char *) "GP relative relocation when _gp not defined"; | |
b3c0fc57 ILT |
602 | return bfd_reloc_dangerous; |
603 | } | |
604 | } | |
605 | } | |
606 | ||
607 | if (bfd_is_com_section (symbol->section)) | |
608 | relocation = 0; | |
609 | else | |
610 | relocation = symbol->value; | |
611 | ||
612 | relocation += symbol->section->output_section->vma; | |
613 | relocation += symbol->section->output_offset; | |
614 | ||
615 | if (reloc_entry->address > input_section->_cooked_size) | |
616 | return bfd_reloc_outofrange; | |
617 | ||
618 | insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address); | |
619 | ||
620 | /* Set val to the offset into the section or symbol. */ | |
621 | val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff; | |
622 | if (val & 0x8000) | |
623 | val -= 0x10000; | |
624 | ||
625 | /* Adjust val for the final section location and GP value. If we | |
626 | are producing relocateable output, we don't want to do this for | |
627 | an external symbol. */ | |
628 | if (relocateable == false | |
629 | || (symbol->flags & BSF_SECTION_SYM) != 0) | |
630 | val += relocation - elf_gp (output_bfd); | |
631 | ||
632 | insn = (insn &~ 0xffff) | (val & 0xffff); | |
633 | bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address); | |
634 | ||
635 | if (relocateable != false) | |
636 | reloc_entry->address += input_section->output_offset; | |
637 | ||
638 | /* Make sure it fit in 16 bits. */ | |
639 | if (val >= 0x8000 && val < 0xffff8000) | |
6e07e54f | 640 | return bfd_reloc_overflow; |
b3c0fc57 ILT |
641 | |
642 | return bfd_reloc_ok; | |
643 | } | |
6b4b4d17 | 644 | |
b3c0fc57 | 645 | /* A mapping from BFD reloc types to MIPS ELF reloc types. */ |
6b4b4d17 | 646 | |
b3c0fc57 ILT |
647 | struct elf_reloc_map { |
648 | bfd_reloc_code_real_type bfd_reloc_val; | |
649 | enum reloc_type elf_reloc_val; | |
650 | }; | |
651 | ||
652 | static CONST struct elf_reloc_map mips_reloc_map[] = | |
653 | { | |
654 | { BFD_RELOC_NONE, R_MIPS_NONE, }, | |
655 | { BFD_RELOC_16, R_MIPS_16 }, | |
656 | { BFD_RELOC_32, R_MIPS_32 }, | |
657 | { BFD_RELOC_CTOR, R_MIPS_32 }, | |
658 | { BFD_RELOC_32_PCREL, R_MIPS_REL32 }, | |
659 | { BFD_RELOC_MIPS_JMP, R_MIPS_26 }, | |
660 | { BFD_RELOC_HI16_S, R_MIPS_HI16 }, | |
661 | { BFD_RELOC_LO16, R_MIPS_LO16 }, | |
662 | { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 }, | |
663 | { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL }, | |
664 | { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 }, | |
665 | { BFD_RELOC_16_PCREL, R_MIPS_PC16 }, | |
666 | { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 }, | |
667 | { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 } | |
668 | }; | |
669 | ||
670 | /* Given a BFD reloc type, return a howto structure. */ | |
671 | ||
aac6b32f | 672 | static const struct reloc_howto_struct * |
b3c0fc57 ILT |
673 | bfd_elf32_bfd_reloc_type_lookup (abfd, code) |
674 | bfd *abfd; | |
675 | bfd_reloc_code_real_type code; | |
676 | { | |
677 | int i; | |
678 | ||
679 | for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++) | |
680 | { | |
681 | if (mips_reloc_map[i].bfd_reloc_val == code) | |
682 | return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val]; | |
683 | } | |
684 | return NULL; | |
685 | } | |
686 | ||
687 | /* Given a MIPS reloc type, fill in an arelent structure. */ | |
688 | ||
689 | static void | |
690 | mips_info_to_howto_rel (abfd, cache_ptr, dst) | |
691 | bfd *abfd; | |
692 | arelent *cache_ptr; | |
693 | Elf32_Internal_Rel *dst; | |
694 | { | |
695 | unsigned int r_type; | |
696 | ||
697 | r_type = ELF32_R_TYPE (dst->r_info); | |
698 | BFD_ASSERT (r_type < (unsigned int) R_MIPS_max); | |
699 | cache_ptr->howto = &elf_mips_howto_table[r_type]; | |
700 | ||
701 | /* The addend for a GPREL16 or LITERAL relocation comes from the GP | |
702 | value for the object file. We get the addend now, rather than | |
703 | when we do the relocation, because the symbol manipulations done | |
704 | by the linker may cause us to lose track of the input BFD. */ | |
705 | if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0 | |
706 | && (r_type == (unsigned int) R_MIPS_GPREL16 | |
707 | || r_type == (unsigned int) R_MIPS_LITERAL)) | |
708 | cache_ptr->addend = elf_gp (abfd); | |
709 | } | |
710 | \f | |
711 | /* A .reginfo section holds a single Elf32_RegInfo structure. These | |
712 | routines swap this structure in and out. They are used outside of | |
713 | BFD, so they are globally visible. */ | |
714 | ||
715 | void | |
716 | bfd_mips_elf32_swap_reginfo_in (abfd, ex, in) | |
717 | bfd *abfd; | |
718 | const Elf32_External_RegInfo *ex; | |
719 | Elf32_RegInfo *in; | |
720 | { | |
721 | in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask); | |
722 | in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]); | |
723 | in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]); | |
724 | in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]); | |
725 | in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]); | |
726 | in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value); | |
727 | } | |
728 | ||
729 | void | |
730 | bfd_mips_elf32_swap_reginfo_out (abfd, in, ex) | |
731 | bfd *abfd; | |
732 | const Elf32_RegInfo *in; | |
733 | Elf32_External_RegInfo *ex; | |
734 | { | |
735 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask, | |
736 | (bfd_byte *) ex->ri_gprmask); | |
737 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0], | |
738 | (bfd_byte *) ex->ri_cprmask[0]); | |
739 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1], | |
740 | (bfd_byte *) ex->ri_cprmask[1]); | |
741 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2], | |
742 | (bfd_byte *) ex->ri_cprmask[2]); | |
743 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3], | |
744 | (bfd_byte *) ex->ri_cprmask[3]); | |
745 | bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value, | |
746 | (bfd_byte *) ex->ri_gp_value); | |
747 | } | |
aac6b32f ILT |
748 | |
749 | /* Swap an entry in a .gptab section. Note that these routines rely | |
750 | on the equivalence of the two elements of the union. */ | |
751 | ||
752 | static void | |
753 | bfd_mips_elf32_swap_gptab_in (abfd, ex, in) | |
754 | bfd *abfd; | |
755 | const Elf32_External_gptab *ex; | |
756 | Elf32_gptab *in; | |
757 | { | |
758 | in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value); | |
759 | in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes); | |
760 | } | |
761 | ||
762 | static void | |
763 | bfd_mips_elf32_swap_gptab_out (abfd, in, ex) | |
764 | bfd *abfd; | |
765 | const Elf32_gptab *in; | |
766 | Elf32_External_gptab *ex; | |
767 | { | |
768 | bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value, | |
769 | ex->gt_entry.gt_g_value); | |
770 | bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes, | |
771 | ex->gt_entry.gt_bytes); | |
772 | } | |
b3c0fc57 | 773 | \f |
6e07e54f ILT |
774 | /* Determine whether a symbol is global for the purposes of splitting |
775 | the symbol table into global symbols and local symbols. At least | |
776 | on Irix 5, this split must be between section symbols and all other | |
777 | symbols. On most ELF targets the split is between static symbols | |
778 | and externally visible symbols. */ | |
779 | ||
780 | /*ARGSUSED*/ | |
781 | static boolean | |
782 | mips_elf_sym_is_global (abfd, sym) | |
783 | bfd *abfd; | |
784 | asymbol *sym; | |
785 | { | |
786 | return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false; | |
787 | } | |
788 | \f | |
789 | /* Set the right machine number for a MIPS ELF file. */ | |
790 | ||
791 | static boolean | |
792 | mips_elf_object_p (abfd) | |
793 | bfd *abfd; | |
794 | { | |
795 | switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) | |
796 | { | |
797 | default: | |
798 | case E_MIPS_ARCH_1: | |
799 | /* Just use the default, which was set in elfcode.h. */ | |
800 | break; | |
801 | ||
802 | case E_MIPS_ARCH_2: | |
803 | (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 6000); | |
804 | break; | |
805 | ||
806 | case E_MIPS_ARCH_3: | |
807 | (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 4000); | |
808 | break; | |
809 | } | |
810 | ||
aac6b32f ILT |
811 | /* Irix 5 is broken. Object file symbol tables are not always |
812 | sorted correctly such that local symbols precede global symbols, | |
813 | and the sh_info field in the symbol table is not always right. | |
814 | We try to quickly check whether the symbol table is broken for | |
815 | this BFD, and, if it is, we set elf_bad_symtab in tdata. */ | |
816 | if (elf_onesymtab (abfd) != 0) | |
817 | { | |
818 | Elf_Internal_Shdr *symtab_hdr; | |
819 | Elf32_External_Sym esym; | |
820 | ||
821 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
822 | if (bfd_seek (abfd, | |
823 | (symtab_hdr->sh_offset | |
824 | + symtab_hdr->sh_size | |
825 | - sizeof (Elf32_External_Sym)), | |
826 | SEEK_SET) != 0 | |
827 | || (bfd_read ((PTR) &esym, 1, sizeof (Elf32_External_Sym), abfd) | |
828 | != sizeof (Elf32_External_Sym))) | |
829 | return false; | |
830 | if (ELF_ST_BIND (bfd_h_get_8 (abfd, (bfd_byte *) esym.st_info)) | |
831 | == STB_LOCAL) | |
832 | elf_bad_symtab (abfd) = true; | |
833 | } | |
834 | ||
6e07e54f ILT |
835 | return true; |
836 | } | |
837 | ||
838 | /* The final processing done just before writing out a MIPS ELF object | |
839 | file. This gets the MIPS architecture right based on the machine | |
840 | number. */ | |
841 | ||
842 | static void | |
843 | mips_elf_final_write_processing (abfd) | |
844 | bfd *abfd; | |
845 | { | |
846 | unsigned long val; | |
aac6b32f ILT |
847 | unsigned int i; |
848 | Elf_Internal_Shdr **hdrpp; | |
6e07e54f ILT |
849 | |
850 | switch (bfd_get_mach (abfd)) | |
851 | { | |
852 | case 3000: | |
853 | val = E_MIPS_ARCH_1; | |
854 | break; | |
855 | ||
856 | case 6000: | |
857 | val = E_MIPS_ARCH_2; | |
858 | break; | |
859 | ||
860 | case 4000: | |
861 | val = E_MIPS_ARCH_3; | |
862 | break; | |
863 | ||
864 | default: | |
865 | return; | |
866 | } | |
867 | ||
868 | elf_elfheader (abfd)->e_flags &=~ EF_MIPS_ARCH; | |
869 | elf_elfheader (abfd)->e_flags |= val; | |
aac6b32f ILT |
870 | |
871 | /* Set the sh_info field for .gptab sections. */ | |
872 | for (i = 1, hdrpp = elf_elfsections (abfd) + 1; | |
873 | i < elf_elfheader (abfd)->e_shnum; | |
874 | i++, hdrpp++) | |
875 | { | |
876 | if ((*hdrpp)->sh_type == SHT_MIPS_GPTAB) | |
877 | { | |
878 | const char *name; | |
879 | asection *sec; | |
880 | ||
881 | BFD_ASSERT ((*hdrpp)->rawdata != NULL); | |
882 | name = ((asection *) (*hdrpp)->rawdata)->name; | |
883 | BFD_ASSERT (name != NULL | |
884 | && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0); | |
885 | sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1); | |
886 | BFD_ASSERT (sec != NULL); | |
887 | (*hdrpp)->sh_info = elf_section_data (sec)->this_idx; | |
888 | } | |
889 | } | |
6e07e54f ILT |
890 | } |
891 | \f | |
b3c0fc57 ILT |
892 | /* Handle a MIPS specific section when reading an object file. This |
893 | is called when elfcode.h finds a section with an unknown type. | |
894 | FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure | |
895 | how to. */ | |
896 | ||
897 | static boolean | |
898 | mips_elf_section_from_shdr (abfd, hdr, name) | |
899 | bfd *abfd; | |
900 | Elf32_Internal_Shdr *hdr; | |
901 | char *name; | |
902 | { | |
497c5434 ILT |
903 | asection *newsect; |
904 | ||
b3c0fc57 ILT |
905 | /* There ought to be a place to keep ELF backend specific flags, but |
906 | at the moment there isn't one. We just keep track of the | |
907 | sections by their name, instead. Fortunately, the ABI gives | |
908 | suggested names for all the MIPS specific sections, so we will | |
909 | probably get away with this. */ | |
910 | switch (hdr->sh_type) | |
911 | { | |
912 | case SHT_MIPS_LIBLIST: | |
913 | if (strcmp (name, ".liblist") != 0) | |
914 | return false; | |
915 | break; | |
916 | case SHT_MIPS_CONFLICT: | |
917 | if (strcmp (name, ".conflict") != 0) | |
918 | return false; | |
919 | break; | |
920 | case SHT_MIPS_GPTAB: | |
921 | if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0) | |
922 | return false; | |
923 | break; | |
924 | case SHT_MIPS_UCODE: | |
925 | if (strcmp (name, ".ucode") != 0) | |
926 | return false; | |
927 | break; | |
928 | case SHT_MIPS_DEBUG: | |
929 | if (strcmp (name, ".mdebug") != 0) | |
930 | return false; | |
931 | break; | |
932 | case SHT_MIPS_REGINFO: | |
933 | if (strcmp (name, ".reginfo") != 0 | |
934 | || hdr->sh_size != sizeof (Elf32_External_RegInfo)) | |
935 | return false; | |
936 | break; | |
6e07e54f ILT |
937 | case SHT_MIPS_OPTIONS: |
938 | if (strcmp (name, ".options") != 0) | |
939 | return false; | |
940 | break; | |
b3c0fc57 ILT |
941 | default: |
942 | return false; | |
943 | } | |
944 | ||
497c5434 ILT |
945 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) |
946 | return false; | |
947 | newsect = (asection *) hdr->rawdata; | |
b3c0fc57 | 948 | |
497c5434 ILT |
949 | if (hdr->sh_type == SHT_MIPS_DEBUG) |
950 | { | |
951 | if (! bfd_set_section_flags (abfd, newsect, | |
952 | (bfd_get_section_flags (abfd, newsect) | |
953 | | SEC_DEBUGGING))) | |
954 | return false; | |
955 | } | |
b3c0fc57 | 956 | |
497c5434 | 957 | /* FIXME: We should record sh_info for a .gptab section. */ |
b3c0fc57 | 958 | |
497c5434 ILT |
959 | /* For a .reginfo section, set the gp value in the tdata information |
960 | from the contents of this section. We need the gp value while | |
961 | processing relocs, so we just get it now. */ | |
962 | if (hdr->sh_type == SHT_MIPS_REGINFO) | |
963 | { | |
964 | Elf32_External_RegInfo ext; | |
965 | Elf32_RegInfo s; | |
b3c0fc57 | 966 | |
497c5434 ILT |
967 | if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext, |
968 | (file_ptr) 0, sizeof ext)) | |
969 | return false; | |
970 | bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s); | |
971 | elf_gp (abfd) = s.ri_gp_value; | |
b3c0fc57 ILT |
972 | } |
973 | ||
974 | return true; | |
975 | } | |
976 | ||
977 | /* Set the correct type for a MIPS ELF section. We do this by the | |
978 | section name, which is a hack, but ought to work. */ | |
979 | ||
980 | static boolean | |
981 | mips_elf_fake_sections (abfd, hdr, sec) | |
982 | bfd *abfd; | |
983 | Elf32_Internal_Shdr *hdr; | |
984 | asection *sec; | |
985 | { | |
986 | register const char *name; | |
987 | ||
988 | name = bfd_get_section_name (abfd, sec); | |
989 | ||
990 | if (strcmp (name, ".liblist") == 0) | |
991 | { | |
992 | hdr->sh_type = SHT_MIPS_LIBLIST; | |
993 | hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib); | |
994 | /* FIXME: Set the sh_link field. */ | |
995 | } | |
996 | else if (strcmp (name, ".conflict") == 0) | |
997 | hdr->sh_type = SHT_MIPS_CONFLICT; | |
998 | else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0) | |
999 | { | |
1000 | hdr->sh_type = SHT_MIPS_GPTAB; | |
aac6b32f ILT |
1001 | hdr->sh_entsize = sizeof (Elf32_External_gptab); |
1002 | /* The sh_info field is set in mips_elf_final_write_processing. */ | |
b3c0fc57 ILT |
1003 | } |
1004 | else if (strcmp (name, ".ucode") == 0) | |
1005 | hdr->sh_type = SHT_MIPS_UCODE; | |
1006 | else if (strcmp (name, ".mdebug") == 0) | |
6e07e54f ILT |
1007 | { |
1008 | hdr->sh_type = SHT_MIPS_DEBUG; | |
1009 | hdr->sh_entsize = 1; | |
1010 | } | |
b3c0fc57 ILT |
1011 | else if (strcmp (name, ".reginfo") == 0) |
1012 | { | |
1013 | hdr->sh_type = SHT_MIPS_REGINFO; | |
6e07e54f | 1014 | hdr->sh_entsize = 1; |
b3c0fc57 ILT |
1015 | |
1016 | /* Force the section size to the correct value, even if the | |
1017 | linker thinks it is larger. The link routine below will only | |
1018 | write out this much data for .reginfo. */ | |
1019 | hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo); | |
1020 | } | |
6e07e54f ILT |
1021 | else if (strcmp (name, ".options") == 0) |
1022 | { | |
1023 | hdr->sh_type = SHT_MIPS_OPTIONS; | |
1024 | hdr->sh_entsize = 1; | |
1025 | } | |
b3c0fc57 ILT |
1026 | |
1027 | return true; | |
1028 | } | |
1029 | ||
1030 | /* Given a BFD section, try to locate the corresponding ELF section | |
1031 | index. */ | |
1032 | ||
1033 | static boolean | |
1034 | mips_elf_section_from_bfd_section (abfd, hdr, sec, retval) | |
1035 | bfd *abfd; | |
1036 | Elf32_Internal_Shdr *hdr; | |
1037 | asection *sec; | |
1038 | int *retval; | |
1039 | { | |
1040 | if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0) | |
1041 | { | |
1042 | *retval = SHN_MIPS_SCOMMON; | |
1043 | return true; | |
1044 | } | |
1045 | if ((asection *) hdr->rawdata == sec) | |
1046 | return true; | |
1047 | return false; | |
1048 | } | |
1049 | ||
1050 | /* Work over a section just before writing it out. We update the GP | |
1051 | value in the .reginfo section based on the value we are using. | |
1052 | FIXME: We recognize sections that need the SHF_MIPS_GPREL flag by | |
1053 | name; there has to be a better way. */ | |
1054 | ||
1055 | static boolean | |
1056 | mips_elf_section_processing (abfd, hdr) | |
1057 | bfd *abfd; | |
1058 | Elf32_Internal_Shdr *hdr; | |
1059 | { | |
1060 | if (hdr->sh_type == SHT_MIPS_REGINFO) | |
1061 | { | |
1062 | bfd_byte buf[4]; | |
1063 | ||
1064 | BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo)); | |
1065 | BFD_ASSERT (hdr->contents == NULL); | |
1066 | ||
1067 | if (bfd_seek (abfd, | |
1068 | hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4, | |
1069 | SEEK_SET) == -1) | |
1070 | return false; | |
1071 | bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf); | |
1072 | if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4) | |
1073 | return false; | |
1074 | } | |
1075 | ||
1076 | if (hdr->rawdata != NULL) | |
1077 | { | |
1078 | const char *name = ((asection *) hdr->rawdata)->name; | |
1079 | ||
1080 | if (strcmp (name, ".sdata") == 0) | |
1081 | { | |
1082 | hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL; | |
1083 | hdr->sh_type = SHT_PROGBITS; | |
1084 | } | |
1085 | else if (strcmp (name, ".sbss") == 0) | |
1086 | { | |
1087 | hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL; | |
1088 | hdr->sh_type = SHT_NOBITS; | |
1089 | } | |
1090 | else if (strcmp (name, ".lit8") == 0 | |
1091 | || strcmp (name, ".lit4") == 0) | |
1092 | { | |
1093 | hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL; | |
1094 | hdr->sh_type = SHT_PROGBITS; | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | return true; | |
1099 | } | |
1100 | \f | |
aac6b32f ILT |
1101 | /* MIPS ELF uses two common sections. One is the usual one, and the |
1102 | other is for small objects. All the small objects are kept | |
1103 | together, and then referenced via the gp pointer, which yields | |
1104 | faster assembler code. This is what we use for the small common | |
1105 | section. This approach is copied from ecoff.c. */ | |
1106 | static asection mips_elf_scom_section; | |
1107 | static asymbol mips_elf_scom_symbol; | |
1108 | static asymbol *mips_elf_scom_symbol_ptr; | |
1109 | ||
1110 | /* MIPS ELF also uses an acommon section, which represents an | |
1111 | allocated common symbol which may be overridden by a | |
1112 | definition in a shared library. */ | |
1113 | static asection mips_elf_acom_section; | |
1114 | static asymbol mips_elf_acom_symbol; | |
1115 | static asymbol *mips_elf_acom_symbol_ptr; | |
1116 | ||
1117 | /* Handle the special MIPS section numbers that a symbol may use. */ | |
1118 | ||
1119 | static void | |
1120 | mips_elf_symbol_processing (abfd, asym) | |
1121 | bfd *abfd; | |
1122 | asymbol *asym; | |
1123 | { | |
1124 | elf_symbol_type *elfsym; | |
1125 | ||
1126 | elfsym = (elf_symbol_type *) asym; | |
1127 | switch (elfsym->internal_elf_sym.st_shndx) | |
1128 | { | |
1129 | case SHN_MIPS_ACOMMON: | |
1130 | /* This section is used in a dynamically linked executable file. | |
1131 | It is an allocated common section. The dynamic linker can | |
1132 | either resolve these symbols to something in a shared | |
1133 | library, or it can just leave them here. For our purposes, | |
1134 | we can consider these symbols to be in a new section. */ | |
1135 | if (mips_elf_acom_section.name == NULL) | |
1136 | { | |
1137 | /* Initialize the acommon section. */ | |
1138 | mips_elf_acom_section.name = ".acommon"; | |
1139 | mips_elf_acom_section.flags = SEC_NO_FLAGS; | |
1140 | mips_elf_acom_section.output_section = &mips_elf_acom_section; | |
1141 | mips_elf_acom_section.symbol = &mips_elf_acom_symbol; | |
1142 | mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr; | |
1143 | mips_elf_acom_symbol.name = ".acommon"; | |
1144 | mips_elf_acom_symbol.flags = BSF_SECTION_SYM; | |
1145 | mips_elf_acom_symbol.section = &mips_elf_acom_section; | |
1146 | mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol; | |
1147 | } | |
1148 | asym->section = &mips_elf_acom_section; | |
1149 | break; | |
1150 | ||
1151 | case SHN_COMMON: | |
1152 | /* Common symbols less than the GP size are automatically | |
1153 | treated as SHN_MIPS_SCOMMON symbols. */ | |
1154 | if (asym->value > elf_gp_size (abfd)) | |
1155 | break; | |
1156 | /* Fall through. */ | |
1157 | case SHN_MIPS_SCOMMON: | |
1158 | if (mips_elf_scom_section.name == NULL) | |
1159 | { | |
1160 | /* Initialize the small common section. */ | |
1161 | mips_elf_scom_section.name = ".scommon"; | |
1162 | mips_elf_scom_section.flags = SEC_IS_COMMON; | |
1163 | mips_elf_scom_section.output_section = &mips_elf_scom_section; | |
1164 | mips_elf_scom_section.symbol = &mips_elf_scom_symbol; | |
1165 | mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr; | |
1166 | mips_elf_scom_symbol.name = ".scommon"; | |
1167 | mips_elf_scom_symbol.flags = BSF_SECTION_SYM; | |
1168 | mips_elf_scom_symbol.section = &mips_elf_scom_section; | |
1169 | mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol; | |
1170 | } | |
1171 | asym->section = &mips_elf_scom_section; | |
1172 | asym->value = elfsym->internal_elf_sym.st_size; | |
1173 | break; | |
1174 | ||
1175 | case SHN_MIPS_SUNDEFINED: | |
1176 | asym->section = &bfd_und_section; | |
1177 | break; | |
1178 | } | |
1179 | } | |
1180 | \f | |
6e07e54f ILT |
1181 | /* Read ECOFF debugging information from a .mdebug section into a |
1182 | ecoff_debug_info structure. */ | |
b3c0fc57 ILT |
1183 | |
1184 | static boolean | |
6e07e54f | 1185 | mips_elf_read_ecoff_info (abfd, section, debug) |
b3c0fc57 | 1186 | bfd *abfd; |
6e07e54f ILT |
1187 | asection *section; |
1188 | struct ecoff_debug_info *debug; | |
b3c0fc57 | 1189 | { |
6e07e54f ILT |
1190 | HDRR *symhdr; |
1191 | const struct ecoff_debug_swap *swap; | |
a3a33af3 | 1192 | char *ext_hdr = NULL; |
6e07e54f ILT |
1193 | |
1194 | swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap; | |
1195 | ||
a3a33af3 ILT |
1196 | ext_hdr = (char *) malloc (swap->external_hdr_size); |
1197 | if (ext_hdr == NULL && swap->external_hdr_size != 0) | |
1198 | { | |
1199 | bfd_set_error (bfd_error_no_memory); | |
1200 | goto error_return; | |
1201 | } | |
6e07e54f ILT |
1202 | |
1203 | if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0, | |
1204 | swap->external_hdr_size) | |
1205 | == false) | |
a3a33af3 | 1206 | goto error_return; |
6e07e54f ILT |
1207 | |
1208 | symhdr = &debug->symbolic_header; | |
1209 | (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr); | |
1210 | ||
1211 | /* The symbolic header contains absolute file offsets and sizes to | |
1212 | read. */ | |
1213 | #define READ(ptr, offset, count, size, type) \ | |
1214 | if (symhdr->count == 0) \ | |
1215 | debug->ptr = NULL; \ | |
1216 | else \ | |
1217 | { \ | |
1218 | debug->ptr = (type) malloc (size * symhdr->count); \ | |
1219 | if (debug->ptr == NULL) \ | |
1220 | { \ | |
a3a33af3 ILT |
1221 | bfd_set_error (bfd_error_no_memory); \ |
1222 | goto error_return; \ | |
6e07e54f ILT |
1223 | } \ |
1224 | if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \ | |
1225 | || (bfd_read (debug->ptr, size, symhdr->count, \ | |
1226 | abfd) != size * symhdr->count)) \ | |
a3a33af3 | 1227 | goto error_return; \ |
6e07e54f ILT |
1228 | } |
1229 | ||
1230 | READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *); | |
1231 | READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR); | |
1232 | READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR); | |
1233 | READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR); | |
1234 | READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR); | |
1235 | READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext), | |
1236 | union aux_ext *); | |
1237 | READ (ss, cbSsOffset, issMax, sizeof (char), char *); | |
1238 | READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *); | |
1239 | READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR); | |
1240 | READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR); | |
aac6b32f ILT |
1241 | READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR); |
1242 | #undef READ | |
9783e04a DM |
1243 | |
1244 | debug->fdr = NULL; | |
a3a33af3 | 1245 | debug->adjust = NULL; |
6e07e54f ILT |
1246 | |
1247 | return true; | |
a3a33af3 ILT |
1248 | |
1249 | error_return: | |
1250 | if (ext_hdr != NULL) | |
1251 | free (ext_hdr); | |
a3a33af3 ILT |
1252 | if (debug->line != NULL) |
1253 | free (debug->line); | |
1254 | if (debug->external_dnr != NULL) | |
1255 | free (debug->external_dnr); | |
1256 | if (debug->external_pdr != NULL) | |
1257 | free (debug->external_pdr); | |
1258 | if (debug->external_sym != NULL) | |
1259 | free (debug->external_sym); | |
1260 | if (debug->external_opt != NULL) | |
1261 | free (debug->external_opt); | |
1262 | if (debug->external_aux != NULL) | |
1263 | free (debug->external_aux); | |
1264 | if (debug->ss != NULL) | |
1265 | free (debug->ss); | |
1266 | if (debug->ssext != NULL) | |
1267 | free (debug->ssext); | |
1268 | if (debug->external_fdr != NULL) | |
1269 | free (debug->external_fdr); | |
1270 | if (debug->external_rfd != NULL) | |
1271 | free (debug->external_rfd); | |
aac6b32f ILT |
1272 | if (debug->external_ext != NULL) |
1273 | free (debug->external_ext); | |
a3a33af3 | 1274 | return false; |
6e07e54f ILT |
1275 | } |
1276 | ||
aac6b32f ILT |
1277 | /* The MIPS ELF linker needs additional information for each symbol in |
1278 | the global hash table. */ | |
1279 | ||
1280 | struct mips_elf_link_hash_entry | |
1281 | { | |
1282 | struct elf_link_hash_entry root; | |
6e07e54f | 1283 | |
aac6b32f ILT |
1284 | /* External symbol information. */ |
1285 | EXTR esym; | |
1286 | }; | |
1287 | ||
1288 | /* MIPS ELF linker hash table. */ | |
1289 | ||
1290 | struct mips_elf_link_hash_table | |
1291 | { | |
1292 | struct elf_link_hash_table root; | |
1293 | }; | |
1294 | ||
1295 | /* Look up an entry in a MIPS ELF linker hash table. */ | |
1296 | ||
1297 | #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \ | |
1298 | ((struct mips_elf_link_hash_entry *) \ | |
1299 | elf_link_hash_lookup (&(table)->root, (string), (create), \ | |
1300 | (copy), (follow))) | |
1301 | ||
1302 | /* Traverse a MIPS ELF linker hash table. */ | |
1303 | ||
1304 | #define mips_elf_link_hash_traverse(table, func, info) \ | |
1305 | (elf_link_hash_traverse \ | |
1306 | (&(table)->root, \ | |
1307 | (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \ | |
1308 | (info))) | |
1309 | ||
1310 | /* Get the MIPS ELF linker hash table from a link_info structure. */ | |
1311 | ||
1312 | #define mips_elf_hash_table(p) \ | |
1313 | ((struct mips_elf_link_hash_table *) ((p)->hash)) | |
1314 | ||
1315 | static boolean mips_elf_output_extsym | |
1316 | PARAMS ((struct mips_elf_link_hash_entry *, PTR)); | |
1317 | ||
1318 | /* Create an entry in a MIPS ELF linker hash table. */ | |
1319 | ||
1320 | static struct bfd_hash_entry * | |
1321 | mips_elf_link_hash_newfunc (entry, table, string) | |
1322 | struct bfd_hash_entry *entry; | |
1323 | struct bfd_hash_table *table; | |
1324 | const char *string; | |
1325 | { | |
1326 | struct mips_elf_link_hash_entry *ret = | |
1327 | (struct mips_elf_link_hash_entry *) entry; | |
1328 | ||
1329 | /* Allocate the structure if it has not already been allocated by a | |
1330 | subclass. */ | |
1331 | if (ret == (struct mips_elf_link_hash_entry *) NULL) | |
1332 | ret = ((struct mips_elf_link_hash_entry *) | |
1333 | bfd_hash_allocate (table, | |
1334 | sizeof (struct mips_elf_link_hash_entry))); | |
1335 | if (ret == (struct mips_elf_link_hash_entry *) NULL) | |
1336 | { | |
1337 | bfd_set_error (bfd_error_no_memory); | |
1338 | return (struct bfd_hash_entry *) ret; | |
1339 | } | |
1340 | ||
1341 | /* Call the allocation method of the superclass. */ | |
1342 | ret = ((struct mips_elf_link_hash_entry *) | |
1343 | _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
1344 | table, string)); | |
1345 | if (ret != (struct mips_elf_link_hash_entry *) NULL) | |
1346 | { | |
1347 | /* Set local fields. */ | |
1348 | memset (&ret->esym, 0, sizeof (EXTR)); | |
1349 | /* We use -2 as a marker to indicate that the information has | |
1350 | not been set. -1 means there is no associated ifd. */ | |
1351 | ret->esym.ifd = -2; | |
1352 | } | |
1353 | ||
1354 | return (struct bfd_hash_entry *) ret; | |
1355 | } | |
1356 | ||
1357 | /* Create a MIPS ELF linker hash table. */ | |
1358 | ||
1359 | static struct bfd_link_hash_table * | |
1360 | mips_elf_link_hash_table_create (abfd) | |
1361 | bfd *abfd; | |
1362 | { | |
1363 | struct mips_elf_link_hash_table *ret; | |
1364 | ||
1365 | ret = ((struct mips_elf_link_hash_table *) | |
1366 | bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table))); | |
1367 | if (ret == (struct mips_elf_link_hash_table *) NULL) | |
1368 | { | |
1369 | bfd_set_error (bfd_error_no_memory); | |
1370 | return NULL; | |
1371 | } | |
1372 | ||
1373 | if (! _bfd_elf_link_hash_table_init (&ret->root, abfd, | |
1374 | mips_elf_link_hash_newfunc)) | |
1375 | { | |
1376 | bfd_release (abfd, ret); | |
1377 | return NULL; | |
1378 | } | |
1379 | ||
1380 | return &ret->root.root; | |
1381 | } | |
1382 | ||
1383 | /* Hook called by the linker routine which adds symbols from an object | |
1384 | file. We must handle the special MIPS section numbers here. */ | |
1385 | ||
1386 | /*ARGSUSED*/ | |
6e07e54f | 1387 | static boolean |
aac6b32f ILT |
1388 | mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp) |
1389 | bfd *abfd; | |
1390 | struct bfd_link_info *info; | |
1391 | const Elf_Internal_Sym *sym; | |
1392 | const char **namep; | |
1393 | flagword *flagsp; | |
1394 | asection **secp; | |
1395 | bfd_vma *valp; | |
1396 | { | |
1397 | switch (sym->st_shndx) | |
1398 | { | |
1399 | case SHN_COMMON: | |
1400 | /* Common symbols less than the GP size are automatically | |
1401 | treated as SHN_MIPS_SCOMMON symbols. */ | |
1402 | if (sym->st_size > elf_gp_size (abfd)) | |
1403 | break; | |
1404 | /* Fall through. */ | |
1405 | case SHN_MIPS_SCOMMON: | |
1406 | *secp = bfd_make_section_old_way (abfd, ".scommon"); | |
1407 | (*secp)->flags |= SEC_IS_COMMON; | |
1408 | *valp = sym->st_size; | |
1409 | break; | |
1410 | ||
1411 | case SHN_MIPS_SUNDEFINED: | |
1412 | *secp = &bfd_und_section; | |
1413 | break; | |
1414 | } | |
1415 | ||
1416 | return true; | |
1417 | } | |
1418 | ||
1419 | /* Structure used to pass information to mips_elf_output_extsym. */ | |
1420 | ||
1421 | struct extsym_info | |
6e07e54f | 1422 | { |
aac6b32f ILT |
1423 | bfd *abfd; |
1424 | struct bfd_link_info *info; | |
1425 | struct ecoff_debug_info *debug; | |
9783e04a | 1426 | const struct ecoff_debug_swap *swap; |
aac6b32f ILT |
1427 | boolean failed; |
1428 | }; | |
9783e04a | 1429 | |
aac6b32f ILT |
1430 | /* This routine is used to write out ECOFF debugging external symbol |
1431 | information. It is called via mips_elf_link_hash_traverse. The | |
1432 | ECOFF external symbol information must match the ELF external | |
1433 | symbol information. Unfortunately, at this point we don't know | |
1434 | whether a symbol is required by reloc information, so the two | |
1435 | tables may wind up being different. We must sort out the external | |
1436 | symbol information before we can set the final size of the .mdebug | |
1437 | section, and we must set the size of the .mdebug section before we | |
1438 | can relocate any sections, and we can't know which symbols are | |
1439 | required by relocation until we relocate the sections. | |
1440 | Fortunately, it is relatively unlikely that any symbol will be | |
1441 | stripped but required by a reloc. In particular, it can not happen | |
1442 | when generating a final executable. */ | |
1443 | ||
1444 | static boolean | |
1445 | mips_elf_output_extsym (h, data) | |
1446 | struct mips_elf_link_hash_entry *h; | |
1447 | PTR data; | |
1448 | { | |
1449 | struct extsym_info *einfo = (struct extsym_info *) data; | |
1450 | boolean strip; | |
1451 | ||
1452 | if (h->root.indx == -2) | |
1453 | strip = false; | |
1454 | else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
1455 | || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0) | |
1456 | && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
1457 | && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0) | |
1458 | strip = true; | |
1459 | else if (einfo->info->strip == strip_all | |
1460 | || (einfo->info->strip == strip_some | |
1461 | && bfd_hash_lookup (einfo->info->keep_hash, | |
1462 | h->root.root.root.string, | |
1463 | false, false) == NULL)) | |
1464 | strip = true; | |
1465 | else | |
1466 | strip = false; | |
1467 | ||
1468 | if (strip) | |
1469 | return true; | |
6e07e54f | 1470 | |
aac6b32f | 1471 | if (h->esym.ifd == -2) |
6e07e54f | 1472 | { |
aac6b32f ILT |
1473 | h->esym.jmptbl = 0; |
1474 | h->esym.cobol_main = 0; | |
1475 | h->esym.weakext = 0; | |
1476 | h->esym.reserved = 0; | |
1477 | h->esym.ifd = ifdNil; | |
1478 | h->esym.asym.value = 0; | |
1479 | h->esym.asym.st = stGlobal; | |
1480 | ||
1481 | if (h->root.root.type != bfd_link_hash_defined) | |
1482 | h->esym.asym.sc = scAbs; | |
1483 | else | |
1484 | { | |
1485 | asection *output_section; | |
1486 | const char *name; | |
1487 | ||
1488 | output_section = h->root.root.u.def.section->output_section; | |
1489 | name = bfd_section_name (output_section->owner, output_section); | |
1490 | ||
1491 | if (strcmp (name, ".text") == 0) | |
1492 | h->esym.asym.sc = scText; | |
1493 | else if (strcmp (name, ".data") == 0) | |
1494 | h->esym.asym.sc = scData; | |
1495 | else if (strcmp (name, ".sdata") == 0) | |
1496 | h->esym.asym.sc = scSData; | |
1497 | else if (strcmp (name, ".rodata") == 0 | |
1498 | || strcmp (name, ".rdata") == 0) | |
1499 | h->esym.asym.sc = scRData; | |
1500 | else if (strcmp (name, ".bss") == 0) | |
1501 | h->esym.asym.sc = scBss; | |
1502 | else if (strcmp (name, ".sbss") == 0) | |
1503 | h->esym.asym.sc = scSBss; | |
1504 | else if (strcmp (name, ".init") == 0) | |
1505 | h->esym.asym.sc = scInit; | |
1506 | else if (strcmp (name, ".fini") == 0) | |
1507 | h->esym.asym.sc = scFini; | |
1508 | else | |
1509 | h->esym.asym.sc = scAbs; | |
1510 | } | |
1511 | ||
1512 | h->esym.asym.reserved = 0; | |
1513 | h->esym.asym.index = indexNil; | |
6e07e54f ILT |
1514 | } |
1515 | ||
aac6b32f ILT |
1516 | |
1517 | if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap, | |
1518 | h->root.root.root.string, | |
1519 | &h->esym)) | |
1520 | { | |
1521 | einfo->failed = true; | |
1522 | return false; | |
1523 | } | |
6e07e54f ILT |
1524 | |
1525 | return true; | |
1526 | } | |
1527 | ||
aac6b32f | 1528 | /* A comparison routine used to sort .gptab entries. */ |
6e07e54f | 1529 | |
aac6b32f ILT |
1530 | static int |
1531 | gptab_compare (p1, p2) | |
1532 | const PTR p1; | |
1533 | const PTR p2; | |
6e07e54f | 1534 | { |
aac6b32f ILT |
1535 | const Elf32_gptab *a1 = (const Elf32_gptab *) p1; |
1536 | const Elf32_gptab *a2 = (const Elf32_gptab *) p2; | |
1537 | ||
1538 | return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value; | |
6e07e54f ILT |
1539 | } |
1540 | ||
1541 | /* We need to use a special link routine to handle the .reginfo and | |
1542 | the .mdebug sections. We need to merge all instances of these | |
1543 | sections together, not write them all out sequentially. */ | |
1544 | ||
1545 | static boolean | |
1546 | mips_elf_final_link (abfd, info) | |
1547 | bfd *abfd; | |
1548 | struct bfd_link_info *info; | |
1549 | { | |
6e07e54f ILT |
1550 | asection **secpp; |
1551 | asection *o; | |
1552 | struct bfd_link_order *p; | |
aac6b32f | 1553 | asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec; |
b3c0fc57 | 1554 | Elf32_RegInfo reginfo; |
6e07e54f ILT |
1555 | struct ecoff_debug_info debug; |
1556 | const struct ecoff_debug_swap *swap | |
1557 | = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap; | |
1558 | HDRR *symhdr = &debug.symbolic_header; | |
9783e04a | 1559 | PTR mdebug_handle = NULL; |
6e07e54f | 1560 | |
497c5434 | 1561 | /* Drop the .options section, since it has special semantics which I |
aac6b32f ILT |
1562 | haven't bothered to figure out. */ |
1563 | for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next) | |
6e07e54f | 1564 | { |
aac6b32f | 1565 | if (strcmp ((*secpp)->name, ".options") == 0) |
6e07e54f | 1566 | { |
aac6b32f ILT |
1567 | for (p = (*secpp)->link_order_head; p != NULL; p = p->next) |
1568 | if (p->type == bfd_indirect_link_order) | |
1569 | p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS; | |
1570 | (*secpp)->link_order_head = NULL; | |
6e07e54f ILT |
1571 | *secpp = (*secpp)->next; |
1572 | --abfd->section_count; | |
aac6b32f | 1573 | break; |
6e07e54f | 1574 | } |
6e07e54f | 1575 | } |
b3c0fc57 | 1576 | |
6e07e54f | 1577 | /* Go through the sections and collect the .reginfo and .mdebug |
aac6b32f | 1578 | information. */ |
9783e04a DM |
1579 | reginfo_sec = NULL; |
1580 | mdebug_sec = NULL; | |
aac6b32f ILT |
1581 | gptab_data_sec = NULL; |
1582 | gptab_bss_sec = NULL; | |
6e07e54f | 1583 | for (o = abfd->sections; o != (asection *) NULL; o = o->next) |
b3c0fc57 | 1584 | { |
6e07e54f | 1585 | if (strcmp (o->name, ".reginfo") == 0) |
b3c0fc57 | 1586 | { |
6e07e54f | 1587 | memset (®info, 0, sizeof reginfo); |
b3c0fc57 ILT |
1588 | |
1589 | /* We have found the .reginfo section in the output file. | |
6e07e54f ILT |
1590 | Look through all the link_orders comprising it and merge |
1591 | the information together. */ | |
1592 | for (p = o->link_order_head; | |
1593 | p != (struct bfd_link_order *) NULL; | |
b3c0fc57 ILT |
1594 | p = p->next) |
1595 | { | |
6e07e54f ILT |
1596 | asection *input_section; |
1597 | bfd *input_bfd; | |
1598 | Elf32_External_RegInfo ext; | |
1599 | Elf32_RegInfo sub; | |
1600 | ||
1601 | if (p->type != bfd_indirect_link_order) | |
aac6b32f ILT |
1602 | { |
1603 | if (p->type == bfd_fill_link_order) | |
1604 | continue; | |
1605 | abort (); | |
1606 | } | |
6e07e54f ILT |
1607 | |
1608 | input_section = p->u.indirect.section; | |
1609 | input_bfd = input_section->owner; | |
1610 | BFD_ASSERT (input_section->_raw_size | |
1611 | == sizeof (Elf32_External_RegInfo)); | |
1612 | if (! bfd_get_section_contents (input_bfd, input_section, | |
1613 | (PTR) &ext, | |
1614 | (file_ptr) 0, | |
1615 | sizeof ext)) | |
1616 | return false; | |
1617 | ||
1618 | bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub); | |
1619 | ||
1620 | reginfo.ri_gprmask |= sub.ri_gprmask; | |
1621 | reginfo.ri_cprmask[0] |= sub.ri_cprmask[0]; | |
1622 | reginfo.ri_cprmask[1] |= sub.ri_cprmask[1]; | |
1623 | reginfo.ri_cprmask[2] |= sub.ri_cprmask[2]; | |
1624 | reginfo.ri_cprmask[3] |= sub.ri_cprmask[3]; | |
1625 | ||
1626 | /* ri_gp_value is set by the function | |
1627 | mips_elf_section_processing when the section is | |
1628 | finally written out. */ | |
aac6b32f ILT |
1629 | |
1630 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
1631 | elf_link_input_bfd ignores this section. */ | |
1632 | input_section->flags &=~ SEC_HAS_CONTENTS; | |
6e07e54f ILT |
1633 | } |
1634 | ||
1635 | /* Force the section size to the value we want. */ | |
1636 | o->_raw_size = sizeof (Elf32_External_RegInfo); | |
1637 | ||
aac6b32f ILT |
1638 | /* Skip this section later on (I don't think this currently |
1639 | matters, but someday it might). */ | |
6e07e54f ILT |
1640 | o->link_order_head = (struct bfd_link_order *) NULL; |
1641 | ||
1642 | reginfo_sec = o; | |
1643 | } | |
1644 | ||
1645 | if (strcmp (o->name, ".mdebug") == 0) | |
1646 | { | |
aac6b32f ILT |
1647 | struct extsym_info einfo; |
1648 | ||
6e07e54f ILT |
1649 | /* We have found the .mdebug section in the output file. |
1650 | Look through all the link_orders comprising it and merge | |
1651 | the information together. */ | |
1652 | symhdr->magic = swap->sym_magic; | |
1653 | /* FIXME: What should the version stamp be? */ | |
1654 | symhdr->vstamp = 0; | |
1655 | symhdr->ilineMax = 0; | |
1656 | symhdr->cbLine = 0; | |
1657 | symhdr->idnMax = 0; | |
1658 | symhdr->ipdMax = 0; | |
1659 | symhdr->isymMax = 0; | |
1660 | symhdr->ioptMax = 0; | |
1661 | symhdr->iauxMax = 0; | |
1662 | symhdr->issMax = 0; | |
1663 | symhdr->issExtMax = 0; | |
1664 | symhdr->ifdMax = 0; | |
1665 | symhdr->crfd = 0; | |
1666 | symhdr->iextMax = 0; | |
1667 | ||
1668 | /* We accumulate the debugging information itself in the | |
1669 | debug_info structure. */ | |
9783e04a DM |
1670 | debug.line = NULL; |
1671 | debug.external_dnr = NULL; | |
1672 | debug.external_pdr = NULL; | |
1673 | debug.external_sym = NULL; | |
1674 | debug.external_opt = NULL; | |
1675 | debug.external_aux = NULL; | |
1676 | debug.ss = NULL; | |
6e07e54f | 1677 | debug.ssext = debug.ssext_end = NULL; |
9783e04a DM |
1678 | debug.external_fdr = NULL; |
1679 | debug.external_rfd = NULL; | |
6e07e54f ILT |
1680 | debug.external_ext = debug.external_ext_end = NULL; |
1681 | ||
9783e04a DM |
1682 | mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info); |
1683 | if (mdebug_handle == (PTR) NULL) | |
1684 | return false; | |
1685 | ||
6e07e54f ILT |
1686 | for (p = o->link_order_head; |
1687 | p != (struct bfd_link_order *) NULL; | |
1688 | p = p->next) | |
1689 | { | |
1690 | asection *input_section; | |
1691 | bfd *input_bfd; | |
1692 | const struct ecoff_debug_swap *input_swap; | |
1693 | struct ecoff_debug_info input_debug; | |
aac6b32f ILT |
1694 | char *eraw_src; |
1695 | char *eraw_end; | |
6e07e54f ILT |
1696 | |
1697 | if (p->type != bfd_indirect_link_order) | |
aac6b32f ILT |
1698 | { |
1699 | if (p->type == bfd_fill_link_order) | |
1700 | continue; | |
1701 | abort (); | |
1702 | } | |
6e07e54f ILT |
1703 | |
1704 | input_section = p->u.indirect.section; | |
1705 | input_bfd = input_section->owner; | |
1706 | ||
1707 | if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour | |
1708 | || (get_elf_backend_data (input_bfd) | |
1709 | ->elf_backend_ecoff_debug_swap) == NULL) | |
b3c0fc57 | 1710 | { |
6e07e54f ILT |
1711 | /* I don't know what a non MIPS ELF bfd would be |
1712 | doing with a .mdebug section, but I don't really | |
1713 | want to deal with it. */ | |
1714 | continue; | |
b3c0fc57 | 1715 | } |
6e07e54f ILT |
1716 | |
1717 | input_swap = (get_elf_backend_data (input_bfd) | |
1718 | ->elf_backend_ecoff_debug_swap); | |
1719 | ||
1720 | BFD_ASSERT (p->size == input_section->_raw_size); | |
1721 | ||
1722 | /* The ECOFF linking code expects that we have already | |
1723 | read in the debugging information and set up an | |
1724 | ecoff_debug_info structure, so we do that now. */ | |
1725 | if (! mips_elf_read_ecoff_info (input_bfd, input_section, | |
1726 | &input_debug)) | |
1727 | return false; | |
1728 | ||
1729 | if (! (bfd_ecoff_debug_accumulate | |
9783e04a DM |
1730 | (mdebug_handle, abfd, &debug, swap, input_bfd, |
1731 | &input_debug, input_swap, info))) | |
6e07e54f ILT |
1732 | return false; |
1733 | ||
1734 | /* Loop through the external symbols. For each one with | |
aac6b32f ILT |
1735 | interesting information, try to find the symbol in |
1736 | the linker global hash table and save the information | |
1737 | for the output external symbols. */ | |
1738 | eraw_src = input_debug.external_ext; | |
1739 | eraw_end = (eraw_src | |
1740 | + (input_debug.symbolic_header.iextMax | |
1741 | * input_swap->external_ext_size)); | |
1742 | for (; | |
1743 | eraw_src < eraw_end; | |
1744 | eraw_src += input_swap->external_ext_size) | |
6e07e54f | 1745 | { |
aac6b32f ILT |
1746 | EXTR ext; |
1747 | const char *name; | |
1748 | struct mips_elf_link_hash_entry *h; | |
1749 | ||
1750 | (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext); | |
1751 | if (ext.asym.sc == scNil | |
1752 | || ext.asym.sc == scUndefined | |
1753 | || ext.asym.sc == scSUndefined) | |
1754 | continue; | |
1755 | ||
1756 | name = input_debug.ssext + ext.asym.iss; | |
1757 | h = mips_elf_link_hash_lookup (mips_elf_hash_table (info), | |
1758 | name, false, false, true); | |
1759 | if (h == NULL || h->esym.ifd != -2) | |
1760 | continue; | |
1761 | ||
1762 | if (ext.ifd != -1) | |
6e07e54f | 1763 | { |
aac6b32f ILT |
1764 | BFD_ASSERT (ext.ifd |
1765 | < input_debug.symbolic_header.ifdMax); | |
1766 | ext.ifd = input_debug.ifdmap[ext.ifd]; | |
6e07e54f | 1767 | } |
aac6b32f ILT |
1768 | |
1769 | h->esym = ext; | |
6e07e54f ILT |
1770 | } |
1771 | ||
aac6b32f | 1772 | /* Free up the information we just read. */ |
6e07e54f ILT |
1773 | free (input_debug.line); |
1774 | free (input_debug.external_dnr); | |
1775 | free (input_debug.external_pdr); | |
1776 | free (input_debug.external_sym); | |
1777 | free (input_debug.external_opt); | |
1778 | free (input_debug.external_aux); | |
1779 | free (input_debug.ss); | |
1780 | free (input_debug.ssext); | |
1781 | free (input_debug.external_fdr); | |
1782 | free (input_debug.external_rfd); | |
aac6b32f ILT |
1783 | free (input_debug.external_ext); |
1784 | ||
1785 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
1786 | elf_link_input_bfd ignores this section. */ | |
1787 | input_section->flags &=~ SEC_HAS_CONTENTS; | |
b3c0fc57 ILT |
1788 | } |
1789 | ||
6e07e54f | 1790 | /* Build the external symbol information. */ |
aac6b32f ILT |
1791 | einfo.abfd = abfd; |
1792 | einfo.info = info; | |
1793 | einfo.debug = &debug; | |
1794 | einfo.swap = swap; | |
1795 | einfo.failed = false; | |
1796 | mips_elf_link_hash_traverse (mips_elf_hash_table (info), | |
1797 | mips_elf_output_extsym, | |
1798 | (PTR) &einfo); | |
1799 | if (einfo.failed) | |
b3c0fc57 ILT |
1800 | return false; |
1801 | ||
aac6b32f | 1802 | /* Set the size of the .mdebug section. */ |
6e07e54f ILT |
1803 | o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap); |
1804 | ||
aac6b32f ILT |
1805 | /* Skip this section later on (I don't think this currently |
1806 | matters, but someday it might). */ | |
6e07e54f ILT |
1807 | o->link_order_head = (struct bfd_link_order *) NULL; |
1808 | ||
1809 | mdebug_sec = o; | |
1810 | } | |
6e07e54f | 1811 | |
aac6b32f | 1812 | if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0) |
6e07e54f | 1813 | { |
aac6b32f ILT |
1814 | const char *subname; |
1815 | unsigned int c; | |
1816 | Elf32_gptab *tab; | |
1817 | Elf32_External_gptab *ext_tab; | |
1818 | unsigned int i; | |
1819 | ||
1820 | /* The .gptab.sdata and .gptab.sbss sections hold | |
1821 | information describing how the small data area would | |
1822 | change depending upon the -G switch. These sections | |
1823 | not used in executables files. */ | |
1824 | if (! info->relocateable) | |
1825 | { | |
1826 | asection **secpp; | |
1827 | ||
1828 | for (p = o->link_order_head; | |
1829 | p != (struct bfd_link_order *) NULL; | |
1830 | p = p->next) | |
1831 | { | |
1832 | asection *input_section; | |
1833 | ||
1834 | if (p->type != bfd_indirect_link_order) | |
1835 | { | |
1836 | if (p->type == bfd_fill_link_order) | |
1837 | continue; | |
1838 | abort (); | |
1839 | } | |
1840 | ||
1841 | input_section = p->u.indirect.section; | |
1842 | ||
1843 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
1844 | elf_link_input_bfd ignores this section. */ | |
1845 | input_section->flags &=~ SEC_HAS_CONTENTS; | |
1846 | } | |
1847 | ||
1848 | /* Skip this section later on (I don't think this | |
1849 | currently matters, but someday it might). */ | |
1850 | o->link_order_head = (struct bfd_link_order *) NULL; | |
1851 | ||
1852 | /* Really remove the section. */ | |
1853 | for (secpp = &abfd->sections; | |
1854 | *secpp != o; | |
1855 | secpp = &(*secpp)->next) | |
1856 | ; | |
1857 | *secpp = (*secpp)->next; | |
1858 | --abfd->section_count; | |
1859 | ||
1860 | continue; | |
1861 | } | |
1862 | ||
1863 | /* There is one gptab for initialized data, and one for | |
1864 | uninitialized data. */ | |
1865 | if (strcmp (o->name, ".gptab.sdata") == 0) | |
1866 | gptab_data_sec = o; | |
1867 | else if (strcmp (o->name, ".gptab.sbss") == 0) | |
1868 | gptab_bss_sec = o; | |
1869 | else | |
1870 | { | |
1871 | bfd_set_error (bfd_error_nonrepresentable_section); | |
1872 | return false; | |
1873 | } | |
1874 | ||
1875 | /* The linker script always combines .gptab.data and | |
1876 | .gptab.sdata into .gptab.sdata, and likewise for | |
1877 | .gptab.bss and .gptab.sbss. It is possible that there is | |
1878 | no .sdata or .sbss section in the output file, in which | |
1879 | case we must change the name of the output section. */ | |
1880 | subname = o->name + sizeof ".gptab" - 1; | |
1881 | if (bfd_get_section_by_name (abfd, subname) == NULL) | |
1882 | { | |
1883 | if (o == gptab_data_sec) | |
1884 | o->name = ".gptab.data"; | |
1885 | else | |
1886 | o->name = ".gptab.bss"; | |
1887 | subname = o->name + sizeof ".gptab" - 1; | |
1888 | BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL); | |
1889 | } | |
1890 | ||
1891 | /* Set up the first entry. */ | |
1892 | c = 1; | |
1893 | tab = (Elf32_gptab *) malloc (c * sizeof (Elf32_gptab)); | |
1894 | if (tab == NULL) | |
1895 | { | |
1896 | bfd_set_error (bfd_error_no_memory); | |
1897 | return false; | |
1898 | } | |
1899 | tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd); | |
1900 | tab[0].gt_header.gt_unused = 0; | |
1901 | ||
1902 | /* Combine the input sections. */ | |
6e07e54f ILT |
1903 | for (p = o->link_order_head; |
1904 | p != (struct bfd_link_order *) NULL; | |
1905 | p = p->next) | |
1906 | { | |
aac6b32f ILT |
1907 | asection *input_section; |
1908 | bfd *input_bfd; | |
1909 | bfd_size_type size; | |
1910 | unsigned long last; | |
1911 | bfd_size_type gpentry; | |
1912 | ||
1913 | if (p->type != bfd_indirect_link_order) | |
6e07e54f | 1914 | { |
aac6b32f ILT |
1915 | if (p->type == bfd_fill_link_order) |
1916 | continue; | |
1917 | abort (); | |
1918 | } | |
6e07e54f | 1919 | |
aac6b32f ILT |
1920 | input_section = p->u.indirect.section; |
1921 | input_bfd = input_section->owner; | |
1922 | ||
1923 | /* Combine the gptab entries for this input section one | |
1924 | by one. We know that the input gptab entries are | |
1925 | sorted by ascending -G value. */ | |
1926 | size = bfd_section_size (input_bfd, input_section); | |
1927 | last = 0; | |
1928 | for (gpentry = sizeof (Elf32_External_gptab); | |
1929 | gpentry < size; | |
1930 | gpentry += sizeof (Elf32_External_gptab)) | |
1931 | { | |
1932 | Elf32_External_gptab ext_gptab; | |
1933 | Elf32_gptab int_gptab; | |
1934 | unsigned long val; | |
1935 | unsigned long add; | |
1936 | boolean exact; | |
1937 | unsigned int look; | |
1938 | ||
1939 | if (! (bfd_get_section_contents | |
1940 | (input_bfd, input_section, (PTR) &ext_gptab, | |
1941 | gpentry, sizeof (Elf32_External_gptab)))) | |
9783e04a | 1942 | { |
aac6b32f | 1943 | free (tab); |
9783e04a DM |
1944 | return false; |
1945 | } | |
aac6b32f ILT |
1946 | |
1947 | bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab, | |
1948 | &int_gptab); | |
1949 | val = int_gptab.gt_entry.gt_g_value; | |
1950 | add = int_gptab.gt_entry.gt_bytes - last; | |
1951 | ||
1952 | exact = false; | |
1953 | for (look = 1; look < c; look++) | |
1954 | { | |
1955 | if (tab[look].gt_entry.gt_g_value >= val) | |
1956 | tab[look].gt_entry.gt_bytes += add; | |
1957 | ||
1958 | if (tab[look].gt_entry.gt_g_value == val) | |
1959 | exact = true; | |
1960 | } | |
1961 | ||
1962 | if (! exact) | |
1963 | { | |
1964 | Elf32_gptab *new_tab; | |
1965 | unsigned int max; | |
1966 | ||
1967 | /* We need a new table entry. */ | |
1968 | new_tab = ((Elf32_gptab *) | |
1969 | realloc ((PTR) tab, | |
1970 | (c + 1) * sizeof (Elf32_gptab))); | |
1971 | if (new_tab == NULL) | |
1972 | { | |
1973 | bfd_set_error (bfd_error_no_memory); | |
1974 | free (tab); | |
1975 | return false; | |
1976 | } | |
1977 | tab = new_tab; | |
1978 | tab[c].gt_entry.gt_g_value = val; | |
1979 | tab[c].gt_entry.gt_bytes = add; | |
1980 | ||
1981 | /* Merge in the size for the next smallest -G | |
1982 | value, since that will be implied by this new | |
1983 | value. */ | |
1984 | max = 0; | |
1985 | for (look = 1; look < c; look++) | |
1986 | { | |
1987 | if (tab[look].gt_entry.gt_g_value < val | |
1988 | && (max == 0 | |
1989 | || (tab[look].gt_entry.gt_g_value | |
1990 | > tab[max].gt_entry.gt_g_value))) | |
1991 | max = look; | |
1992 | } | |
1993 | if (max != 0) | |
1994 | tab[c].gt_entry.gt_bytes += | |
1995 | tab[max].gt_entry.gt_bytes; | |
1996 | ||
1997 | ++c; | |
1998 | } | |
1999 | ||
2000 | last = int_gptab.gt_entry.gt_bytes; | |
6e07e54f | 2001 | } |
aac6b32f ILT |
2002 | |
2003 | /* Hack: reset the SEC_HAS_CONTENTS flag so that | |
2004 | elf_link_input_bfd ignores this section. */ | |
2005 | input_section->flags &=~ SEC_HAS_CONTENTS; | |
6e07e54f | 2006 | } |
aac6b32f ILT |
2007 | |
2008 | /* The table must be sorted by -G value. */ | |
2009 | if (c > 2) | |
2010 | qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare); | |
2011 | ||
2012 | /* Swap out the table. */ | |
2013 | ext_tab = ((Elf32_External_gptab *) | |
2014 | bfd_alloc (abfd, c * sizeof (Elf32_External_gptab))); | |
2015 | if (ext_tab == NULL) | |
6e07e54f | 2016 | { |
aac6b32f ILT |
2017 | bfd_set_error (bfd_error_no_memory); |
2018 | free (tab); | |
2019 | return false; | |
2020 | } | |
2021 | ||
2022 | for (i = 0; i < c; i++) | |
2023 | bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i); | |
2024 | free (tab); | |
2025 | ||
2026 | o->_raw_size = c * sizeof (Elf32_External_gptab); | |
2027 | o->contents = (bfd_byte *) ext_tab; | |
2028 | ||
2029 | /* Skip this section later on (I don't think this currently | |
2030 | matters, but someday it might). */ | |
2031 | o->link_order_head = (struct bfd_link_order *) NULL; | |
2032 | } | |
2033 | } | |
2034 | ||
2035 | /* Get a value for the GP register. */ | |
2036 | if (elf_gp (abfd) == 0) | |
2037 | { | |
2038 | struct bfd_link_hash_entry *h; | |
2039 | ||
2040 | h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true); | |
2041 | if (h != (struct bfd_link_hash_entry *) NULL | |
2042 | && h->type == bfd_link_hash_defined) | |
2043 | elf_gp (abfd) = (h->u.def.value | |
2044 | + h->u.def.section->output_section->vma | |
2045 | + h->u.def.section->output_offset); | |
2046 | else if (info->relocateable) | |
2047 | { | |
2048 | bfd_vma lo; | |
2049 | ||
2050 | /* Make up a value. */ | |
2051 | lo = (bfd_vma) -1; | |
2052 | for (o = abfd->sections; o != (asection *) NULL; o = o->next) | |
2053 | { | |
2054 | if (o->vma < lo | |
2055 | && (strcmp (o->name, ".sbss") == 0 | |
2056 | || strcmp (o->name, ".sdata") == 0 | |
2057 | || strcmp (o->name, ".lit4") == 0 | |
2058 | || strcmp (o->name, ".lit8") == 0)) | |
2059 | lo = o->vma; | |
6e07e54f | 2060 | } |
aac6b32f ILT |
2061 | elf_gp (abfd) = lo + 0x8000; |
2062 | } | |
2063 | else | |
2064 | { | |
2065 | /* If the relocate_section function needs to do a reloc | |
2066 | involving the GP value, it should make a reloc_dangerous | |
2067 | callback to warn that GP is not defined. */ | |
6e07e54f ILT |
2068 | } |
2069 | } | |
b3c0fc57 | 2070 | |
aac6b32f ILT |
2071 | /* Invoke the regular ELF backend linker to do all the work. */ |
2072 | if (! bfd_elf32_bfd_final_link (abfd, info)) | |
2073 | return false; | |
2074 | ||
2075 | /* Now write out the computed sections. */ | |
2076 | ||
6e07e54f ILT |
2077 | if (reginfo_sec != (asection *) NULL) |
2078 | { | |
2079 | Elf32_External_RegInfo ext; | |
2080 | ||
2081 | bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext); | |
2082 | if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext, | |
2083 | (file_ptr) 0, sizeof ext)) | |
2084 | return false; | |
2085 | } | |
b3c0fc57 | 2086 | |
6e07e54f ILT |
2087 | if (mdebug_sec != (asection *) NULL) |
2088 | { | |
aac6b32f | 2089 | BFD_ASSERT (abfd->output_has_begun); |
9783e04a DM |
2090 | if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug, |
2091 | swap, info, | |
2092 | mdebug_sec->filepos)) | |
6e07e54f | 2093 | return false; |
9783e04a DM |
2094 | |
2095 | bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info); | |
b3c0fc57 ILT |
2096 | } |
2097 | ||
aac6b32f | 2098 | if (gptab_data_sec != (asection *) NULL) |
6e07e54f | 2099 | { |
aac6b32f ILT |
2100 | if (! bfd_set_section_contents (abfd, gptab_data_sec, |
2101 | gptab_data_sec->contents, | |
2102 | (file_ptr) 0, | |
2103 | gptab_data_sec->_raw_size)) | |
2104 | return false; | |
2105 | } | |
2106 | ||
2107 | if (gptab_bss_sec != (asection *) NULL) | |
2108 | { | |
2109 | if (! bfd_set_section_contents (abfd, gptab_bss_sec, | |
2110 | gptab_bss_sec->contents, | |
2111 | (file_ptr) 0, | |
2112 | gptab_bss_sec->_raw_size)) | |
2113 | return false; | |
6e07e54f ILT |
2114 | } |
2115 | ||
2116 | return true; | |
b3c0fc57 | 2117 | } |
6e07e54f | 2118 | |
aac6b32f | 2119 | /* Handle a MIPS ELF HI16 reloc. */ |
b3c0fc57 ILT |
2120 | |
2121 | static void | |
aac6b32f ILT |
2122 | mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend) |
2123 | bfd *input_bfd; | |
2124 | Elf_Internal_Rela *relhi; | |
2125 | Elf_Internal_Rela *rello; | |
2126 | bfd_byte *contents; | |
2127 | bfd_vma addend; | |
b3c0fc57 | 2128 | { |
aac6b32f ILT |
2129 | bfd_vma insn; |
2130 | bfd_vma addlo; | |
b3c0fc57 | 2131 | |
aac6b32f ILT |
2132 | insn = bfd_get_32 (input_bfd, contents + relhi->r_offset); |
2133 | ||
2134 | addlo = bfd_get_32 (input_bfd, contents + rello->r_offset); | |
2135 | addlo &= 0xffff; | |
2136 | ||
2137 | addend += ((insn & 0xffff) << 16) + addlo; | |
2138 | ||
2139 | if ((addlo & 0x8000) != 0) | |
2140 | addend -= 0x10000; | |
2141 | if ((addend & 0x8000) != 0) | |
2142 | addend += 0x10000; | |
2143 | ||
2144 | bfd_put_32 (input_bfd, | |
2145 | (insn & 0xffff0000) | ((addend >> 16) & 0xffff), | |
2146 | contents + relhi->r_offset); | |
2147 | } | |
2148 | ||
2149 | /* Relocate a MIPS ELF section. */ | |
2150 | ||
2151 | static boolean | |
2152 | mips_elf_relocate_section (output_bfd, info, input_bfd, input_section, | |
2153 | contents, relocs, local_syms, local_sections) | |
2154 | bfd *output_bfd; | |
2155 | struct bfd_link_info *info; | |
2156 | bfd *input_bfd; | |
2157 | asection *input_section; | |
2158 | bfd_byte *contents; | |
2159 | Elf_Internal_Rela *relocs; | |
2160 | Elf_Internal_Sym *local_syms; | |
2161 | asection **local_sections; | |
2162 | { | |
2163 | Elf_Internal_Shdr *symtab_hdr; | |
2164 | size_t locsymcount; | |
2165 | size_t extsymoff; | |
2166 | Elf_Internal_Rela *rel; | |
2167 | Elf_Internal_Rela *relend; | |
2168 | ||
2169 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
2170 | ||
2171 | if (elf_bad_symtab (input_bfd)) | |
b3c0fc57 | 2172 | { |
aac6b32f ILT |
2173 | locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym); |
2174 | extsymoff = 0; | |
2175 | } | |
2176 | else | |
2177 | { | |
2178 | locsymcount = symtab_hdr->sh_info; | |
2179 | extsymoff = symtab_hdr->sh_info; | |
2180 | } | |
2181 | ||
2182 | rel = relocs; | |
2183 | relend = relocs + input_section->reloc_count; | |
2184 | for (; rel < relend; rel++) | |
2185 | { | |
2186 | int r_type; | |
2187 | const reloc_howto_type *howto; | |
2188 | long r_symndx; | |
2189 | bfd_vma addend; | |
2190 | struct elf_link_hash_entry *h; | |
2191 | asection *sec; | |
2192 | Elf_Internal_Sym *sym; | |
2193 | bfd_reloc_status_type r; | |
2194 | ||
2195 | r_type = ELF32_R_TYPE (rel->r_info); | |
2196 | if (r_type < 0 || r_type >= (int) R_MIPS_max) | |
6e07e54f | 2197 | { |
aac6b32f ILT |
2198 | bfd_set_error (bfd_error_bad_value); |
2199 | return false; | |
6e07e54f | 2200 | } |
aac6b32f | 2201 | howto = elf_mips_howto_table + r_type; |
b3c0fc57 | 2202 | |
aac6b32f ILT |
2203 | r_symndx = ELF32_R_SYM (rel->r_info); |
2204 | ||
2205 | /* Mix in the change in GP address for a GP relative reloc. */ | |
2206 | if (r_type != R_MIPS_GPREL16 | |
2207 | && r_type != R_MIPS_LITERAL | |
2208 | && r_type != R_MIPS_GPREL32) | |
2209 | addend = 0; | |
2210 | else | |
b3c0fc57 | 2211 | { |
aac6b32f ILT |
2212 | if (elf_gp (output_bfd) == 0) |
2213 | { | |
2214 | if (! ((*info->callbacks->reloc_dangerous) | |
2215 | (info, | |
2216 | "GP relative relocation when GP not defined", | |
2217 | input_bfd, input_section, | |
2218 | rel->r_offset))) | |
2219 | return false; | |
2220 | /* Only give the error once per link. */ | |
2221 | elf_gp (output_bfd) = 4; | |
2222 | } | |
2223 | ||
2224 | if (r_symndx < extsymoff | |
2225 | || (elf_bad_symtab (input_bfd) | |
2226 | && local_sections[r_symndx] != NULL)) | |
2227 | { | |
2228 | /* This is a relocation against a section. The current | |
2229 | addend in the instruction is the difference between | |
2230 | INPUT_SECTION->vma and the GP value of INPUT_BFD. We | |
2231 | must change this to be the difference between the | |
2232 | final definition (which will end up in RELOCATION) | |
2233 | and the GP value of OUTPUT_BFD (which is in GP). */ | |
2234 | addend = elf_gp (input_bfd) - elf_gp (output_bfd); | |
2235 | } | |
2236 | else if (! info->relocateable) | |
2237 | { | |
2238 | /* We are doing a final link. The current addend in the | |
2239 | instruction is simply the desired offset into the | |
2240 | symbol (normally zero). We want the instruction to | |
2241 | hold the difference between the final definition of | |
2242 | the symbol (which will end up in RELOCATION) and the | |
2243 | GP value of OUTPUT_BFD (which is in GP). */ | |
2244 | addend = - elf_gp (output_bfd); | |
2245 | } | |
2246 | else | |
2247 | { | |
2248 | /* We are generating relocateable output, and we aren't | |
2249 | going to define this symbol, so we just leave the | |
2250 | instruction alone. */ | |
2251 | addend = 0; | |
2252 | } | |
b3c0fc57 | 2253 | } |
b3c0fc57 | 2254 | |
aac6b32f ILT |
2255 | h = NULL; |
2256 | sym = NULL; | |
2257 | sec = NULL; | |
2258 | if (info->relocateable) | |
2259 | { | |
2260 | /* This is a relocateable link. We don't have to change | |
2261 | anything, unless the reloc is against a section symbol, | |
2262 | in which case we have to adjust according to where the | |
2263 | section symbol winds up in the output section. */ | |
2264 | if (r_symndx >= locsymcount | |
2265 | || (elf_bad_symtab (input_bfd) | |
2266 | && local_sections[r_symndx] == NULL)) | |
2267 | r = bfd_reloc_ok; | |
2268 | else | |
2269 | { | |
2270 | sym = local_syms + r_symndx; | |
2271 | if (ELF_ST_TYPE (sym->st_info) != STT_SECTION) | |
2272 | r = bfd_reloc_ok; | |
2273 | else | |
2274 | { | |
2275 | sec = local_sections[r_symndx]; | |
2276 | ||
2277 | /* It would be logical to add sym->st_value here, | |
2278 | but Irix 5 sometimes generates a garbage symbol | |
2279 | value. */ | |
2280 | addend += sec->output_offset; | |
2281 | ||
2282 | /* If this is HI16 with an associated LO16, adjust | |
2283 | the addend accordingly. Otherwise, just | |
2284 | relocate. */ | |
2285 | if (r_type != R_MIPS_HI16 | |
2286 | || (rel + 1) >= relend | |
2287 | || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16) | |
2288 | r = _bfd_relocate_contents (howto, input_bfd, | |
2289 | addend, | |
2290 | contents + rel->r_offset); | |
2291 | else | |
2292 | { | |
2293 | mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, | |
2294 | contents, addend); | |
2295 | r = bfd_reloc_ok; | |
2296 | } | |
2297 | } | |
2298 | } | |
2299 | } | |
2300 | else | |
2301 | { | |
2302 | bfd_vma relocation; | |
2303 | ||
2304 | /* This is a final link. */ | |
2305 | sym = NULL; | |
2306 | if (r_symndx < extsymoff | |
2307 | || (elf_bad_symtab (input_bfd) | |
2308 | && local_sections[r_symndx] != NULL)) | |
2309 | { | |
2310 | sym = local_syms + r_symndx; | |
2311 | sec = local_sections[r_symndx]; | |
2312 | relocation = (sec->output_section->vma | |
2313 | + sec->output_offset); | |
2314 | ||
2315 | /* It would be logical to always add sym->st_value here, | |
2316 | but Irix 5 sometimes generates a garbage symbol | |
2317 | value. */ | |
2318 | if (ELF_ST_TYPE (sym->st_info) != STT_SECTION) | |
2319 | relocation += sym->st_value; | |
2320 | } | |
2321 | else | |
2322 | { | |
2323 | long indx; | |
2324 | ||
2325 | indx = r_symndx - extsymoff; | |
2326 | h = elf_sym_hashes (input_bfd)[indx]; | |
2327 | if (h->root.type == bfd_link_hash_defined) | |
2328 | { | |
2329 | sec = h->root.u.def.section; | |
2330 | relocation = (h->root.u.def.value | |
2331 | + sec->output_section->vma | |
2332 | + sec->output_offset); | |
2333 | } | |
2334 | else if (h->root.type == bfd_link_hash_weak) | |
2335 | relocation = 0; | |
2336 | else | |
2337 | { | |
2338 | if (! ((*info->callbacks->undefined_symbol) | |
2339 | (info, h->root.root.string, input_bfd, | |
2340 | input_section, rel->r_offset))) | |
2341 | return false; | |
2342 | relocation = 0; | |
2343 | } | |
2344 | } | |
2345 | ||
2346 | if (r_type != R_MIPS_HI16 | |
2347 | || (rel + 1) >= relend | |
2348 | || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16) | |
2349 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
2350 | contents, rel->r_offset, | |
2351 | relocation, addend); | |
2352 | else | |
2353 | { | |
2354 | mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, | |
2355 | contents, relocation + addend); | |
2356 | r = bfd_reloc_ok; | |
2357 | } | |
2358 | } | |
2359 | ||
2360 | if (r != bfd_reloc_ok) | |
2361 | { | |
2362 | switch (r) | |
2363 | { | |
2364 | default: | |
2365 | case bfd_reloc_outofrange: | |
2366 | abort (); | |
2367 | case bfd_reloc_overflow: | |
2368 | { | |
2369 | const char *name; | |
2370 | ||
2371 | if (h != NULL) | |
2372 | name = h->root.root.string; | |
2373 | else | |
2374 | { | |
2375 | name = elf_string_from_elf_section (input_bfd, | |
2376 | symtab_hdr->sh_link, | |
2377 | sym->st_name); | |
2378 | if (name == NULL) | |
2379 | return false; | |
2380 | if (*name == '\0') | |
2381 | name = bfd_section_name (input_bfd, sec); | |
2382 | } | |
2383 | if (! ((*info->callbacks->reloc_overflow) | |
2384 | (info, name, howto->name, (bfd_vma) 0, | |
2385 | input_bfd, input_section, rel->r_offset))) | |
2386 | return false; | |
2387 | } | |
2388 | break; | |
2389 | } | |
2390 | } | |
b3c0fc57 | 2391 | } |
aac6b32f ILT |
2392 | |
2393 | return true; | |
b3c0fc57 ILT |
2394 | } |
2395 | \f | |
6e07e54f ILT |
2396 | /* ECOFF swapping routines. These are used when dealing with the |
2397 | .mdebug section, which is in the ECOFF debugging format. */ | |
2398 | static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap = | |
2399 | { | |
2400 | /* Symbol table magic number. */ | |
2401 | magicSym, | |
2402 | /* Alignment of debugging information. E.g., 4. */ | |
2403 | 4, | |
2404 | /* Sizes of external symbolic information. */ | |
2405 | sizeof (struct hdr_ext), | |
2406 | sizeof (struct dnr_ext), | |
2407 | sizeof (struct pdr_ext), | |
2408 | sizeof (struct sym_ext), | |
2409 | sizeof (struct opt_ext), | |
2410 | sizeof (struct fdr_ext), | |
2411 | sizeof (struct rfd_ext), | |
2412 | sizeof (struct ext_ext), | |
2413 | /* Functions to swap in external symbolic data. */ | |
2414 | ecoff_swap_hdr_in, | |
2415 | ecoff_swap_dnr_in, | |
2416 | ecoff_swap_pdr_in, | |
2417 | ecoff_swap_sym_in, | |
2418 | ecoff_swap_opt_in, | |
2419 | ecoff_swap_fdr_in, | |
2420 | ecoff_swap_rfd_in, | |
2421 | ecoff_swap_ext_in, | |
75f3ef7a ILT |
2422 | _bfd_ecoff_swap_tir_in, |
2423 | _bfd_ecoff_swap_rndx_in, | |
6e07e54f ILT |
2424 | /* Functions to swap out external symbolic data. */ |
2425 | ecoff_swap_hdr_out, | |
2426 | ecoff_swap_dnr_out, | |
2427 | ecoff_swap_pdr_out, | |
2428 | ecoff_swap_sym_out, | |
2429 | ecoff_swap_opt_out, | |
2430 | ecoff_swap_fdr_out, | |
2431 | ecoff_swap_rfd_out, | |
aac6b32f | 2432 | ecoff_swap_ext_out, |
75f3ef7a ILT |
2433 | _bfd_ecoff_swap_tir_out, |
2434 | _bfd_ecoff_swap_rndx_out, | |
aac6b32f ILT |
2435 | /* Function to read in symbolic data. */ |
2436 | mips_elf_read_ecoff_info | |
6e07e54f ILT |
2437 | }; |
2438 | \f | |
6b4b4d17 JK |
2439 | #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec |
2440 | #define TARGET_LITTLE_NAME "elf32-littlemips" | |
2441 | #define TARGET_BIG_SYM bfd_elf32_bigmips_vec | |
2442 | #define TARGET_BIG_NAME "elf32-bigmips" | |
2443 | #define ELF_ARCH bfd_arch_mips | |
6e07e54f | 2444 | #define ELF_MACHINE_CODE EM_MIPS |
b3c0fc57 | 2445 | #define ELF_MAXPAGESIZE 0x10000 |
497c5434 | 2446 | #define elf_backend_collect true |
b3c0fc57 ILT |
2447 | #define elf_info_to_howto 0 |
2448 | #define elf_info_to_howto_rel mips_info_to_howto_rel | |
6e07e54f ILT |
2449 | #define elf_backend_sym_is_global mips_elf_sym_is_global |
2450 | #define elf_backend_object_p mips_elf_object_p | |
b3c0fc57 ILT |
2451 | #define elf_backend_section_from_shdr mips_elf_section_from_shdr |
2452 | #define elf_backend_fake_sections mips_elf_fake_sections | |
2453 | #define elf_backend_section_from_bfd_section \ | |
2454 | mips_elf_section_from_bfd_section | |
2455 | #define elf_backend_section_processing mips_elf_section_processing | |
2456 | #define elf_backend_symbol_processing mips_elf_symbol_processing | |
6e07e54f ILT |
2457 | #define elf_backend_final_write_processing \ |
2458 | mips_elf_final_write_processing | |
2459 | #define elf_backend_ecoff_debug_swap &mips_elf_ecoff_debug_swap | |
b3c0fc57 | 2460 | |
aac6b32f ILT |
2461 | #define bfd_elf32_bfd_link_hash_table_create \ |
2462 | mips_elf_link_hash_table_create | |
6e07e54f | 2463 | #define bfd_elf32_bfd_final_link mips_elf_final_link |
aac6b32f ILT |
2464 | #define elf_backend_relocate_section mips_elf_relocate_section |
2465 | #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook | |
6b4b4d17 JK |
2466 | |
2467 | #include "elf32-target.h" |