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