"propogate" -> "propagate"
[binutils-gdb.git] / bfd / elf32-mips.c
1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
3
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23 /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
24 different MIPS ELF from other targets. This matters when linking.
25 This file supports both, switching at runtime. */
26
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "libbfd.h"
30 #include "bfdlink.h"
31 #include "genlink.h"
32 #include "elf-bfd.h"
33 #include "elf/mips.h"
34
35 /* Get the ECOFF swapping routines. */
36 #include "coff/sym.h"
37 #include "coff/symconst.h"
38 #include "coff/internal.h"
39 #include "coff/ecoff.h"
40 #include "coff/mips.h"
41 #define ECOFF_32
42 #include "ecoffswap.h"
43
44 static bfd_reloc_status_type mips32_64bit_reloc
45 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
47 PARAMS ((bfd *, bfd_reloc_code_real_type));
48 static void mips_info_to_howto_rel
49 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
50 static void bfd_mips_elf32_swap_gptab_in
51 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
52 static void bfd_mips_elf32_swap_gptab_out
53 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
54 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
55 static boolean mips_elf32_object_p PARAMS ((bfd *));
56 static boolean mips_elf_create_procedure_table
57 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
58 struct ecoff_debug_info *));
59 static int mips_elf_additional_program_headers PARAMS ((bfd *));
60 static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
61 static INLINE int elf_mips_isa PARAMS ((flagword));
62 static INLINE int elf_mips_mach PARAMS ((flagword));
63 static boolean mips_elf32_section_from_shdr
64 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
65 static boolean mips_elf32_section_processing
66 PARAMS ((bfd *, Elf32_Internal_Shdr *));
67 static boolean mips_elf_is_local_label_name
68 PARAMS ((bfd *, const char *));
69 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
70 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
71 static struct bfd_link_hash_table *mips_elf_link_hash_table_create
72 PARAMS ((bfd *));
73 static int gptab_compare PARAMS ((const void *, const void *));
74 static boolean mips_elf_final_link
75 PARAMS ((bfd *, struct bfd_link_info *));
76 static void mips_elf_relocate_hi16
77 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
78 bfd_vma));
79 static boolean mips_elf_relocate_got_local
80 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
81 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
82 static void mips_elf_relocate_global_got
83 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
84 static bfd_reloc_status_type mips16_jump_reloc
85 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
86 static bfd_reloc_status_type mips16_gprel_reloc
87 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
88 static boolean mips_elf_adjust_dynindx
89 PARAMS ((struct elf_link_hash_entry *, PTR));
90 static boolean mips_elf_relocate_section
91 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
92 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
93 static boolean mips_elf_link_output_symbol_hook
94 PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
95 asection *));
96 static boolean mips_elf_create_dynamic_sections
97 PARAMS ((bfd *, struct bfd_link_info *));
98 static boolean mips_elf_create_compact_rel_section
99 PARAMS ((bfd *, struct bfd_link_info *));
100 static boolean mips_elf_create_got_section
101 PARAMS ((bfd *, struct bfd_link_info *));
102 static boolean mips_elf_check_relocs
103 PARAMS ((bfd *, struct bfd_link_info *, asection *,
104 const Elf_Internal_Rela *));
105 static boolean mips_elf_adjust_dynamic_symbol
106 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
107 static boolean mips_elf_always_size_sections
108 PARAMS ((bfd *, struct bfd_link_info *));
109 static boolean mips_elf_size_dynamic_sections
110 PARAMS ((bfd *, struct bfd_link_info *));
111 static boolean mips_elf_finish_dynamic_symbol
112 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
113 Elf_Internal_Sym *));
114 static boolean mips_elf_finish_dynamic_sections
115 PARAMS ((bfd *, struct bfd_link_info *));
116 static boolean mips_elf_add_symbol_hook
117 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
118 const char **, flagword *, asection **, bfd_vma *));
119 static bfd_reloc_status_type mips_elf_final_gp
120 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
121 static bfd_byte *elf32_mips_get_relocated_section_contents
122 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
123 bfd_byte *, boolean, asymbol **));
124
125 /* This is true for Irix 5 executables, false for normal MIPS ELF ABI
126 executables. FIXME: At the moment, we default to always generating
127 Irix 5 executables. */
128
129 #define SGI_COMPAT(abfd) (1)
130
131 /* This structure is used to hold .got information when linking. It
132 is stored in the tdata field of the bfd_elf_section_data structure. */
133
134 struct mips_got_info
135 {
136 /* The symbol index of the first global .got symbol. */
137 unsigned long global_gotsym;
138 /* The number of local .got entries. */
139 unsigned int local_gotno;
140 /* The number of local .got entries we have used. */
141 unsigned int assigned_gotno;
142 };
143
144 /* The number of local .got entries we reserve. */
145 #define MIPS_RESERVED_GOTNO (2)
146
147 /* Instructions which appear in a stub. For some reason the stub is
148 slightly different on an SGI system. */
149 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
150 #define STUB_LW(abfd) \
151 (SGI_COMPAT (abfd) \
152 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
153 : 0x8f998000) /* lw t9,0x8000(gp) */
154 #define STUB_MOVE 0x03e07825 /* move t7,ra */
155 #define STUB_JALR 0x0320f809 /* jal t9 */
156 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
157 #define MIPS_FUNCTION_STUB_SIZE (16)
158
159 /* Names of sections which appear in the .dynsym section in an Irix 5
160 executable. */
161
162 static const char * const mips_elf_dynsym_sec_names[] =
163 {
164 ".text",
165 ".init",
166 ".fini",
167 ".data",
168 ".rodata",
169 ".sdata",
170 ".sbss",
171 ".bss",
172 NULL
173 };
174
175 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
176 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
177
178 /* The number of entries in mips_elf_dynsym_sec_names which go in the
179 text segment. */
180
181 #define MIPS_TEXT_DYNSYM_SECNO (3)
182
183 /* The names of the runtime procedure table symbols used on Irix 5. */
184
185 static const char * const mips_elf_dynsym_rtproc_names[] =
186 {
187 "_procedure_table",
188 "_procedure_string_table",
189 "_procedure_table_size",
190 NULL
191 };
192
193 /* These structures are used to generate the .compact_rel section on
194 Irix 5. */
195
196 typedef struct
197 {
198 unsigned long id1; /* Always one? */
199 unsigned long num; /* Number of compact relocation entries. */
200 unsigned long id2; /* Always two? */
201 unsigned long offset; /* The file offset of the first relocation. */
202 unsigned long reserved0; /* Zero? */
203 unsigned long reserved1; /* Zero? */
204 } Elf32_compact_rel;
205
206 typedef struct
207 {
208 bfd_byte id1[4];
209 bfd_byte num[4];
210 bfd_byte id2[4];
211 bfd_byte offset[4];
212 bfd_byte reserved0[4];
213 bfd_byte reserved1[4];
214 } Elf32_External_compact_rel;
215
216 typedef struct
217 {
218 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
219 unsigned int rtype : 4; /* Relocation types. See below. */
220 unsigned int dist2to : 8;
221 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
222 unsigned long konst; /* KONST field. See below. */
223 unsigned long vaddr; /* VADDR to be relocated. */
224 } Elf32_crinfo;
225
226 typedef struct
227 {
228 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
229 unsigned int rtype : 4; /* Relocation types. See below. */
230 unsigned int dist2to : 8;
231 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
232 unsigned long konst; /* KONST field. See below. */
233 } Elf32_crinfo2;
234
235 typedef struct
236 {
237 bfd_byte info[4];
238 bfd_byte konst[4];
239 bfd_byte vaddr[4];
240 } Elf32_External_crinfo;
241
242 typedef struct
243 {
244 bfd_byte info[4];
245 bfd_byte konst[4];
246 } Elf32_External_crinfo2;
247
248 /* These are the constants used to swap the bitfields in a crinfo. */
249
250 #define CRINFO_CTYPE (0x1)
251 #define CRINFO_CTYPE_SH (31)
252 #define CRINFO_RTYPE (0xf)
253 #define CRINFO_RTYPE_SH (27)
254 #define CRINFO_DIST2TO (0xff)
255 #define CRINFO_DIST2TO_SH (19)
256 #define CRINFO_RELVADDR (0x7ffff)
257 #define CRINFO_RELVADDR_SH (0)
258
259 /* A compact relocation info has long (3 words) or short (2 words)
260 formats. A short format doesn't have VADDR field and relvaddr
261 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
262 #define CRF_MIPS_LONG 1
263 #define CRF_MIPS_SHORT 0
264
265 /* There are 4 types of compact relocation at least. The value KONST
266 has different meaning for each type:
267
268 (type) (konst)
269 CT_MIPS_REL32 Address in data
270 CT_MIPS_WORD Address in word (XXX)
271 CT_MIPS_GPHI_LO GP - vaddr
272 CT_MIPS_JMPAD Address to jump
273 */
274
275 #define CRT_MIPS_REL32 0xa
276 #define CRT_MIPS_WORD 0xb
277 #define CRT_MIPS_GPHI_LO 0xc
278 #define CRT_MIPS_JMPAD 0xd
279
280 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
281 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
282 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
283 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
284
285 static void bfd_elf32_swap_compact_rel_out
286 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
287 static void bfd_elf32_swap_crinfo_out
288 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
289
290 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
291
292 enum reloc_type
293 {
294 R_MIPS_NONE = 0,
295 R_MIPS_16, R_MIPS_32,
296 R_MIPS_REL32, R_MIPS_26,
297 R_MIPS_HI16, R_MIPS_LO16,
298 R_MIPS_GPREL16, R_MIPS_LITERAL,
299 R_MIPS_GOT16, R_MIPS_PC16,
300 R_MIPS_CALL16, R_MIPS_GPREL32,
301 /* The remaining relocs are defined on Irix, although they are not
302 in the MIPS ELF ABI. */
303 R_MIPS_UNUSED1, R_MIPS_UNUSED2,
304 R_MIPS_UNUSED3,
305 R_MIPS_SHIFT5, R_MIPS_SHIFT6,
306 R_MIPS_64, R_MIPS_GOT_DISP,
307 R_MIPS_GOT_PAGE, R_MIPS_GOT_OFST,
308 R_MIPS_GOT_HI16, R_MIPS_GOT_LO16,
309 R_MIPS_SUB, R_MIPS_INSERT_A,
310 R_MIPS_INSERT_B, R_MIPS_DELETE,
311 R_MIPS_HIGHER, R_MIPS_HIGHEST,
312 R_MIPS_CALL_HI16, R_MIPS_CALL_LO16,
313 /* start-sanitize-r5900 */
314 /* This is used by a mips co-processor instruction. */
315 R_MIPS15_S3,
316 /* end-sanitize-r5900 */
317 R_MIPS_max,
318 /* These relocs are used for the mips16. */
319 R_MIPS16_26 = 100,
320 R_MIPS16_GPREL = 101,
321 /* start-sanitize-sky */
322 /* These relocs are for the dvp. */
323 R_MIPS_DVP_11_PCREL = 120,
324 R_MIPS_DVP_27_S4 = 121,
325 /* end-sanitize-sky */
326 /* These are GNU extensions to enable C++ vtable garbage collection. */
327 R_MIPS_GNU_VTINHERIT = 253,
328 R_MIPS_GNU_VTENTRY = 254
329 };
330
331 static reloc_howto_type elf_mips_howto_table[] =
332 {
333 /* No relocation. */
334 HOWTO (R_MIPS_NONE, /* type */
335 0, /* rightshift */
336 0, /* size (0 = byte, 1 = short, 2 = long) */
337 0, /* bitsize */
338 false, /* pc_relative */
339 0, /* bitpos */
340 complain_overflow_dont, /* complain_on_overflow */
341 bfd_elf_generic_reloc, /* special_function */
342 "R_MIPS_NONE", /* name */
343 false, /* partial_inplace */
344 0, /* src_mask */
345 0, /* dst_mask */
346 false), /* pcrel_offset */
347
348 /* 16 bit relocation. */
349 HOWTO (R_MIPS_16, /* type */
350 0, /* rightshift */
351 1, /* size (0 = byte, 1 = short, 2 = long) */
352 16, /* bitsize */
353 false, /* pc_relative */
354 0, /* bitpos */
355 complain_overflow_bitfield, /* complain_on_overflow */
356 bfd_elf_generic_reloc, /* special_function */
357 "R_MIPS_16", /* name */
358 true, /* partial_inplace */
359 0xffff, /* src_mask */
360 0xffff, /* dst_mask */
361 false), /* pcrel_offset */
362
363 /* 32 bit relocation. */
364 HOWTO (R_MIPS_32, /* type */
365 0, /* rightshift */
366 2, /* size (0 = byte, 1 = short, 2 = long) */
367 32, /* bitsize */
368 false, /* pc_relative */
369 0, /* bitpos */
370 complain_overflow_bitfield, /* complain_on_overflow */
371 bfd_elf_generic_reloc, /* special_function */
372 "R_MIPS_32", /* name */
373 true, /* partial_inplace */
374 0xffffffff, /* src_mask */
375 0xffffffff, /* dst_mask */
376 false), /* pcrel_offset */
377
378 /* 32 bit symbol relative relocation. */
379 HOWTO (R_MIPS_REL32, /* type */
380 0, /* rightshift */
381 2, /* size (0 = byte, 1 = short, 2 = long) */
382 32, /* bitsize */
383 false, /* pc_relative */
384 0, /* bitpos */
385 complain_overflow_bitfield, /* complain_on_overflow */
386 bfd_elf_generic_reloc, /* special_function */
387 "R_MIPS_REL32", /* name */
388 true, /* partial_inplace */
389 0xffffffff, /* src_mask */
390 0xffffffff, /* dst_mask */
391 false), /* pcrel_offset */
392
393 /* 26 bit branch address. */
394 HOWTO (R_MIPS_26, /* type */
395 2, /* rightshift */
396 2, /* size (0 = byte, 1 = short, 2 = long) */
397 26, /* bitsize */
398 false, /* pc_relative */
399 0, /* bitpos */
400 complain_overflow_dont, /* complain_on_overflow */
401 /* This needs complex overflow
402 detection, because the upper four
403 bits must match the PC. */
404 bfd_elf_generic_reloc, /* special_function */
405 "R_MIPS_26", /* name */
406 true, /* partial_inplace */
407 0x3ffffff, /* src_mask */
408 0x3ffffff, /* dst_mask */
409 false), /* pcrel_offset */
410
411 /* High 16 bits of symbol value. */
412 HOWTO (R_MIPS_HI16, /* type */
413 0, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 16, /* bitsize */
416 false, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_dont, /* complain_on_overflow */
419 _bfd_mips_elf_hi16_reloc, /* special_function */
420 "R_MIPS_HI16", /* name */
421 true, /* partial_inplace */
422 0xffff, /* src_mask */
423 0xffff, /* dst_mask */
424 false), /* pcrel_offset */
425
426 /* Low 16 bits of symbol value. */
427 HOWTO (R_MIPS_LO16, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 16, /* bitsize */
431 false, /* pc_relative */
432 0, /* bitpos */
433 complain_overflow_dont, /* complain_on_overflow */
434 _bfd_mips_elf_lo16_reloc, /* special_function */
435 "R_MIPS_LO16", /* name */
436 true, /* partial_inplace */
437 0xffff, /* src_mask */
438 0xffff, /* dst_mask */
439 false), /* pcrel_offset */
440
441 /* GP relative reference. */
442 HOWTO (R_MIPS_GPREL16, /* type */
443 0, /* rightshift */
444 2, /* size (0 = byte, 1 = short, 2 = long) */
445 16, /* bitsize */
446 false, /* pc_relative */
447 0, /* bitpos */
448 complain_overflow_signed, /* complain_on_overflow */
449 _bfd_mips_elf_gprel16_reloc, /* special_function */
450 "R_MIPS_GPREL16", /* name */
451 true, /* partial_inplace */
452 0xffff, /* src_mask */
453 0xffff, /* dst_mask */
454 false), /* pcrel_offset */
455
456 /* Reference to literal section. */
457 HOWTO (R_MIPS_LITERAL, /* type */
458 0, /* rightshift */
459 2, /* size (0 = byte, 1 = short, 2 = long) */
460 16, /* bitsize */
461 false, /* pc_relative */
462 0, /* bitpos */
463 complain_overflow_signed, /* complain_on_overflow */
464 _bfd_mips_elf_gprel16_reloc, /* special_function */
465 "R_MIPS_LITERAL", /* name */
466 true, /* partial_inplace */
467 0xffff, /* src_mask */
468 0xffff, /* dst_mask */
469 false), /* pcrel_offset */
470
471 /* Reference to global offset table. */
472 HOWTO (R_MIPS_GOT16, /* type */
473 0, /* rightshift */
474 2, /* size (0 = byte, 1 = short, 2 = long) */
475 16, /* bitsize */
476 false, /* pc_relative */
477 0, /* bitpos */
478 complain_overflow_signed, /* complain_on_overflow */
479 _bfd_mips_elf_got16_reloc, /* special_function */
480 "R_MIPS_GOT16", /* name */
481 false, /* partial_inplace */
482 0, /* src_mask */
483 0xffff, /* dst_mask */
484 false), /* pcrel_offset */
485
486 /* 16 bit PC relative reference. */
487 HOWTO (R_MIPS_PC16, /* type */
488 0, /* rightshift */
489 2, /* size (0 = byte, 1 = short, 2 = long) */
490 16, /* bitsize */
491 true, /* pc_relative */
492 0, /* bitpos */
493 complain_overflow_signed, /* complain_on_overflow */
494 bfd_elf_generic_reloc, /* special_function */
495 "R_MIPS_PC16", /* name */
496 true, /* partial_inplace */
497 0xffff, /* src_mask */
498 0xffff, /* dst_mask */
499 false), /* pcrel_offset */
500
501 /* 16 bit call through global offset table. */
502 /* FIXME: This is not handled correctly. */
503 HOWTO (R_MIPS_CALL16, /* type */
504 0, /* rightshift */
505 2, /* size (0 = byte, 1 = short, 2 = long) */
506 16, /* bitsize */
507 false, /* pc_relative */
508 0, /* bitpos */
509 complain_overflow_signed, /* complain_on_overflow */
510 bfd_elf_generic_reloc, /* special_function */
511 "R_MIPS_CALL16", /* name */
512 false, /* partial_inplace */
513 0, /* src_mask */
514 0xffff, /* dst_mask */
515 false), /* pcrel_offset */
516
517 /* 32 bit GP relative reference. */
518 HOWTO (R_MIPS_GPREL32, /* type */
519 0, /* rightshift */
520 2, /* size (0 = byte, 1 = short, 2 = long) */
521 32, /* bitsize */
522 false, /* pc_relative */
523 0, /* bitpos */
524 complain_overflow_bitfield, /* complain_on_overflow */
525 _bfd_mips_elf_gprel32_reloc, /* special_function */
526 "R_MIPS_GPREL32", /* name */
527 true, /* partial_inplace */
528 0xffffffff, /* src_mask */
529 0xffffffff, /* dst_mask */
530 false), /* pcrel_offset */
531
532 /* The remaining relocs are defined on Irix 5, although they are
533 not defined by the ABI. */
534 { 13 },
535 { 14 },
536 { 15 },
537
538 /* A 5 bit shift field. */
539 HOWTO (R_MIPS_SHIFT5, /* type */
540 0, /* rightshift */
541 2, /* size (0 = byte, 1 = short, 2 = long) */
542 5, /* bitsize */
543 false, /* pc_relative */
544 6, /* bitpos */
545 complain_overflow_bitfield, /* complain_on_overflow */
546 bfd_elf_generic_reloc, /* special_function */
547 "R_MIPS_SHIFT5", /* name */
548 true, /* partial_inplace */
549 0x000007c0, /* src_mask */
550 0x000007c0, /* dst_mask */
551 false), /* pcrel_offset */
552
553 /* A 6 bit shift field. */
554 /* FIXME: This is not handled correctly; a special function is
555 needed to put the most significant bit in the right place. */
556 HOWTO (R_MIPS_SHIFT6, /* type */
557 0, /* rightshift */
558 2, /* size (0 = byte, 1 = short, 2 = long) */
559 6, /* bitsize */
560 false, /* pc_relative */
561 6, /* bitpos */
562 complain_overflow_bitfield, /* complain_on_overflow */
563 bfd_elf_generic_reloc, /* special_function */
564 "R_MIPS_SHIFT6", /* name */
565 true, /* partial_inplace */
566 0x000007c4, /* src_mask */
567 0x000007c4, /* dst_mask */
568 false), /* pcrel_offset */
569
570 /* A 64 bit relocation. This is used in 32 bit ELF when addresses
571 are 64 bits long; the upper 32 bits are simply a sign extension.
572 The fields of the howto should be the same as for R_MIPS_32,
573 other than the type, name, and special_function. */
574 HOWTO (R_MIPS_64, /* type */
575 0, /* rightshift */
576 2, /* size (0 = byte, 1 = short, 2 = long) */
577 32, /* bitsize */
578 false, /* pc_relative */
579 0, /* bitpos */
580 complain_overflow_bitfield, /* complain_on_overflow */
581 mips32_64bit_reloc, /* special_function */
582 "R_MIPS_64", /* name */
583 true, /* partial_inplace */
584 0xffffffff, /* src_mask */
585 0xffffffff, /* dst_mask */
586 false), /* pcrel_offset */
587
588 /* Displacement in the global offset table. */
589 /* FIXME: Not handled correctly. */
590 HOWTO (R_MIPS_GOT_DISP, /* type */
591 0, /* rightshift */
592 2, /* size (0 = byte, 1 = short, 2 = long) */
593 16, /* bitsize */
594 false, /* pc_relative */
595 0, /* bitpos */
596 complain_overflow_bitfield, /* complain_on_overflow */
597 bfd_elf_generic_reloc, /* special_function */
598 "R_MIPS_GOT_DISP", /* name */
599 true, /* partial_inplace */
600 0x0000ffff, /* src_mask */
601 0x0000ffff, /* dst_mask */
602 false), /* pcrel_offset */
603
604 /* Displacement to page pointer in the global offset table. */
605 /* FIXME: Not handled correctly. */
606 HOWTO (R_MIPS_GOT_PAGE, /* type */
607 0, /* rightshift */
608 2, /* size (0 = byte, 1 = short, 2 = long) */
609 16, /* bitsize */
610 false, /* pc_relative */
611 0, /* bitpos */
612 complain_overflow_bitfield, /* complain_on_overflow */
613 bfd_elf_generic_reloc, /* special_function */
614 "R_MIPS_GOT_PAGE", /* name */
615 true, /* partial_inplace */
616 0x0000ffff, /* src_mask */
617 0x0000ffff, /* dst_mask */
618 false), /* pcrel_offset */
619
620 /* Offset from page pointer in the global offset table. */
621 /* FIXME: Not handled correctly. */
622 HOWTO (R_MIPS_GOT_OFST, /* type */
623 0, /* rightshift */
624 2, /* size (0 = byte, 1 = short, 2 = long) */
625 16, /* bitsize */
626 false, /* pc_relative */
627 0, /* bitpos */
628 complain_overflow_bitfield, /* complain_on_overflow */
629 bfd_elf_generic_reloc, /* special_function */
630 "R_MIPS_GOT_OFST", /* name */
631 true, /* partial_inplace */
632 0x0000ffff, /* src_mask */
633 0x0000ffff, /* dst_mask */
634 false), /* pcrel_offset */
635
636 /* High 16 bits of displacement in global offset table. */
637 /* FIXME: Not handled correctly. */
638 HOWTO (R_MIPS_GOT_HI16, /* type */
639 0, /* rightshift */
640 2, /* size (0 = byte, 1 = short, 2 = long) */
641 16, /* bitsize */
642 false, /* pc_relative */
643 0, /* bitpos */
644 complain_overflow_dont, /* complain_on_overflow */
645 bfd_elf_generic_reloc, /* special_function */
646 "R_MIPS_GOT_HI16", /* name */
647 true, /* partial_inplace */
648 0x0000ffff, /* src_mask */
649 0x0000ffff, /* dst_mask */
650 false), /* pcrel_offset */
651
652 /* Low 16 bits of displacement in global offset table. */
653 /* FIXME: Not handled correctly. */
654 HOWTO (R_MIPS_GOT_LO16, /* type */
655 0, /* rightshift */
656 2, /* size (0 = byte, 1 = short, 2 = long) */
657 16, /* bitsize */
658 false, /* pc_relative */
659 0, /* bitpos */
660 complain_overflow_dont, /* complain_on_overflow */
661 bfd_elf_generic_reloc, /* special_function */
662 "R_MIPS_GOT_LO16", /* name */
663 true, /* partial_inplace */
664 0x0000ffff, /* src_mask */
665 0x0000ffff, /* dst_mask */
666 false), /* pcrel_offset */
667
668 /* 64 bit subtraction. Presumably not used in 32 bit ELF. */
669 { R_MIPS_SUB },
670
671 /* Used to cause the linker to insert and delete instructions? */
672 { R_MIPS_INSERT_A },
673 { R_MIPS_INSERT_B },
674 { R_MIPS_DELETE },
675
676 /* Get the higher values of a 64 bit addend. Presumably not used in
677 32 bit ELF. */
678 { R_MIPS_HIGHER },
679 { R_MIPS_HIGHEST },
680
681 /* High 16 bits of displacement in global offset table. */
682 /* FIXME: Not handled correctly. */
683 HOWTO (R_MIPS_CALL_HI16, /* type */
684 0, /* rightshift */
685 2, /* size (0 = byte, 1 = short, 2 = long) */
686 16, /* bitsize */
687 false, /* pc_relative */
688 0, /* bitpos */
689 complain_overflow_dont, /* complain_on_overflow */
690 bfd_elf_generic_reloc, /* special_function */
691 "R_MIPS_CALL_HI16", /* name */
692 true, /* partial_inplace */
693 0x0000ffff, /* src_mask */
694 0x0000ffff, /* dst_mask */
695 false), /* pcrel_offset */
696
697 /* Low 16 bits of displacement in global offset table. */
698 /* FIXME: Not handled correctly. */
699 HOWTO (R_MIPS_CALL_LO16, /* type */
700 0, /* rightshift */
701 2, /* size (0 = byte, 1 = short, 2 = long) */
702 16, /* bitsize */
703 false, /* pc_relative */
704 0, /* bitpos */
705 complain_overflow_dont, /* complain_on_overflow */
706 bfd_elf_generic_reloc, /* special_function */
707 "R_MIPS_CALL_LO16", /* name */
708 true, /* partial_inplace */
709 0x0000ffff, /* src_mask */
710 0x0000ffff, /* dst_mask */
711 false), /* pcrel_offset */
712
713 /* start-sanitize-r5900 */
714 HOWTO (R_MIPS15_S3, /* type */
715 3, /* rightshift */
716 2, /* size (0 = byte, 1 = short, 2 = long) */
717 15, /* bitsize */
718 false, /* pc_relative */
719 6, /* bitpos */
720 complain_overflow_bitfield, /* complain_on_overflow */
721 bfd_elf_generic_reloc, /* special_function */
722 "R_MIPS15_S3", /* name */
723 true, /* partial_inplace */
724 0x001fffc0, /* src_mask */
725 0x001fffc0, /* dst_mask */
726 false) /* pcrel_offset */
727 /* end-sanitize-r5900 */
728
729 };
730
731 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
732 is a hack to make the linker think that we need 64 bit values. */
733 static reloc_howto_type elf_mips_ctor64_howto =
734 HOWTO (R_MIPS_64, /* type */
735 0, /* rightshift */
736 4, /* size (0 = byte, 1 = short, 2 = long) */
737 32, /* bitsize */
738 false, /* pc_relative */
739 0, /* bitpos */
740 complain_overflow_signed, /* complain_on_overflow */
741 mips32_64bit_reloc, /* special_function */
742 "R_MIPS_64", /* name */
743 true, /* partial_inplace */
744 0xffffffff, /* src_mask */
745 0xffffffff, /* dst_mask */
746 false); /* pcrel_offset */
747
748 /* The reloc used for the mips16 jump instruction. */
749 static reloc_howto_type elf_mips16_jump_howto =
750 HOWTO (R_MIPS16_26, /* type */
751 2, /* rightshift */
752 2, /* size (0 = byte, 1 = short, 2 = long) */
753 26, /* bitsize */
754 false, /* pc_relative */
755 0, /* bitpos */
756 complain_overflow_dont, /* complain_on_overflow */
757 /* This needs complex overflow
758 detection, because the upper four
759 bits must match the PC. */
760 mips16_jump_reloc, /* special_function */
761 "R_MIPS16_26", /* name */
762 true, /* partial_inplace */
763 0x3ffffff, /* src_mask */
764 0x3ffffff, /* dst_mask */
765 false); /* pcrel_offset */
766
767 /* The reloc used for the mips16 gprel instruction. The src_mask and
768 dsk_mask for this howto do not reflect the actual instruction, in
769 which the value is not contiguous; the masks are for the
770 convenience of the relocate_section routine. */
771 static reloc_howto_type elf_mips16_gprel_howto =
772 HOWTO (R_MIPS16_GPREL, /* type */
773 0, /* rightshift */
774 2, /* size (0 = byte, 1 = short, 2 = long) */
775 16, /* bitsize */
776 false, /* pc_relative */
777 0, /* bitpos */
778 complain_overflow_signed, /* complain_on_overflow */
779 mips16_gprel_reloc, /* special_function */
780 "R_MIPS16_GPREL", /* name */
781 true, /* partial_inplace */
782 0xffff, /* src_mask */
783 0xffff, /* dst_mask */
784 false); /* pcrel_offset */
785
786 /* start-sanitize-sky */
787 /* DVP relocations.
788 Note that partial_inplace and pcrel_offset are backwards from the
789 mips port. This is intentional as it seems more reasonable. */
790 static reloc_howto_type elf_mips_dvp_11_pcrel_howto =
791 HOWTO (R_MIPS_DVP_11_PCREL, /* type */
792 3, /* rightshift */
793 2, /* size (0 = byte, 1 = short, 2 = long) */
794 11, /* bitsize */
795 true, /* pc_relative */
796 0, /* bitpos */
797 complain_overflow_signed, /* complain_on_overflow */
798 bfd_elf_generic_reloc, /* special_function */
799 "R_MIPS_DVP_11_PCREL", /* name */
800 false, /* partial_inplace */
801 0x7ff, /* src_mask */
802 0x7ff, /* dst_mask */
803 true); /* pcrel_offset */
804 static reloc_howto_type elf_mips_dvp_27_s4_howto =
805 HOWTO (R_MIPS_DVP_27_S4, /* type */
806 4, /* rightshift */
807 2, /* size (0 = byte, 1 = short, 2 = long) */
808 27, /* bitsize */
809 false, /* pc_relative */
810 4, /* bitpos */
811 complain_overflow_unsigned, /* complain_on_overflow */
812 bfd_elf_generic_reloc, /* special_function */
813 "R_MIPS_DVP_27_S4", /* name */
814 false, /* partial_inplace */
815 0x7ffffff0, /* src_mask */
816 0x7ffffff0, /* dst_mask */
817 false); /* pcrel_offset */
818 /* end-sanitize-sky */
819
820 /* GNU extension to record C++ vtable hierarchy */
821 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
822 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
823 0, /* rightshift */
824 2, /* size (0 = byte, 1 = short, 2 = long) */
825 0, /* bitsize */
826 false, /* pc_relative */
827 0, /* bitpos */
828 complain_overflow_dont, /* complain_on_overflow */
829 NULL, /* special_function */
830 "R_MIPS_GNU_VTINHERIT", /* name */
831 false, /* partial_inplace */
832 0, /* src_mask */
833 0, /* dst_mask */
834 false); /* pcrel_offset */
835
836 /* GNU extension to record C++ vtable member usage */
837 static reloc_howto_type elf_mips_gnu_vtentry_howto =
838 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
839 0, /* rightshift */
840 2, /* size (0 = byte, 1 = short, 2 = long) */
841 0, /* bitsize */
842 false, /* pc_relative */
843 0, /* bitpos */
844 complain_overflow_dont, /* complain_on_overflow */
845 NULL, /* special_function */
846 "R_MIPS_GNU_VTENTRY", /* name */
847 false, /* partial_inplace */
848 0, /* src_mask */
849 0, /* dst_mask */
850 false); /* pcrel_offset */
851
852 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
853 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
854 the HI16. Here we just save the information we need; we do the
855 actual relocation when we see the LO16. MIPS ELF requires that the
856 LO16 immediately follow the HI16. As a GNU extension, we permit an
857 arbitrary number of HI16 relocs to be associated with a single LO16
858 reloc. This extension permits gcc to output the HI and LO relocs
859 itself. */
860
861 struct mips_hi16
862 {
863 struct mips_hi16 *next;
864 bfd_byte *addr;
865 bfd_vma addend;
866 };
867
868 /* FIXME: This should not be a static variable. */
869
870 static struct mips_hi16 *mips_hi16_list;
871
872 bfd_reloc_status_type
873 _bfd_mips_elf_hi16_reloc (abfd,
874 reloc_entry,
875 symbol,
876 data,
877 input_section,
878 output_bfd,
879 error_message)
880 bfd *abfd;
881 arelent *reloc_entry;
882 asymbol *symbol;
883 PTR data;
884 asection *input_section;
885 bfd *output_bfd;
886 char **error_message;
887 {
888 bfd_reloc_status_type ret;
889 bfd_vma relocation;
890 struct mips_hi16 *n;
891
892 /* If we're relocating, and this an external symbol, we don't want
893 to change anything. */
894 if (output_bfd != (bfd *) NULL
895 && (symbol->flags & BSF_SECTION_SYM) == 0
896 && reloc_entry->addend == 0)
897 {
898 reloc_entry->address += input_section->output_offset;
899 return bfd_reloc_ok;
900 }
901
902 ret = bfd_reloc_ok;
903
904 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
905 {
906 boolean relocateable;
907 bfd_vma gp;
908
909 if (ret == bfd_reloc_undefined)
910 abort ();
911
912 if (output_bfd != NULL)
913 relocateable = true;
914 else
915 {
916 relocateable = false;
917 output_bfd = symbol->section->output_section->owner;
918 }
919
920 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
921 error_message, &gp);
922 if (ret != bfd_reloc_ok)
923 return ret;
924
925 relocation = gp - reloc_entry->address;
926 }
927 else
928 {
929 if (bfd_is_und_section (symbol->section)
930 && output_bfd == (bfd *) NULL)
931 ret = bfd_reloc_undefined;
932
933 if (bfd_is_com_section (symbol->section))
934 relocation = 0;
935 else
936 relocation = symbol->value;
937 }
938
939 relocation += symbol->section->output_section->vma;
940 relocation += symbol->section->output_offset;
941 relocation += reloc_entry->addend;
942
943 if (reloc_entry->address > input_section->_cooked_size)
944 return bfd_reloc_outofrange;
945
946 /* Save the information, and let LO16 do the actual relocation. */
947 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
948 if (n == NULL)
949 return bfd_reloc_outofrange;
950 n->addr = (bfd_byte *) data + reloc_entry->address;
951 n->addend = relocation;
952 n->next = mips_hi16_list;
953 mips_hi16_list = n;
954
955 if (output_bfd != (bfd *) NULL)
956 reloc_entry->address += input_section->output_offset;
957
958 return ret;
959 }
960
961 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
962 inplace relocation; this function exists in order to do the
963 R_MIPS_HI16 relocation described above. */
964
965 bfd_reloc_status_type
966 _bfd_mips_elf_lo16_reloc (abfd,
967 reloc_entry,
968 symbol,
969 data,
970 input_section,
971 output_bfd,
972 error_message)
973 bfd *abfd;
974 arelent *reloc_entry;
975 asymbol *symbol;
976 PTR data;
977 asection *input_section;
978 bfd *output_bfd;
979 char **error_message;
980 {
981 arelent gp_disp_relent;
982
983 if (mips_hi16_list != NULL)
984 {
985 struct mips_hi16 *l;
986
987 l = mips_hi16_list;
988 while (l != NULL)
989 {
990 unsigned long insn;
991 unsigned long val;
992 unsigned long vallo;
993 struct mips_hi16 *next;
994
995 /* Do the HI16 relocation. Note that we actually don't need
996 to know anything about the LO16 itself, except where to
997 find the low 16 bits of the addend needed by the LO16. */
998 insn = bfd_get_32 (abfd, l->addr);
999 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
1000 & 0xffff);
1001 val = ((insn & 0xffff) << 16) + vallo;
1002 val += l->addend;
1003
1004 /* The low order 16 bits are always treated as a signed
1005 value. Therefore, a negative value in the low order bits
1006 requires an adjustment in the high order bits. We need
1007 to make this adjustment in two ways: once for the bits we
1008 took from the data, and once for the bits we are putting
1009 back in to the data. */
1010 if ((vallo & 0x8000) != 0)
1011 val -= 0x10000;
1012 if ((val & 0x8000) != 0)
1013 val += 0x10000;
1014
1015 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1016 bfd_put_32 (abfd, insn, l->addr);
1017
1018 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1019 {
1020 gp_disp_relent = *reloc_entry;
1021 reloc_entry = &gp_disp_relent;
1022 reloc_entry->addend = l->addend;
1023 }
1024
1025 next = l->next;
1026 free (l);
1027 l = next;
1028 }
1029
1030 mips_hi16_list = NULL;
1031 }
1032 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1033 {
1034 bfd_reloc_status_type ret;
1035 bfd_vma gp, relocation;
1036
1037 /* FIXME: Does this case ever occur? */
1038
1039 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
1040 if (ret != bfd_reloc_ok)
1041 return ret;
1042
1043 relocation = gp - reloc_entry->address;
1044 relocation += symbol->section->output_section->vma;
1045 relocation += symbol->section->output_offset;
1046 relocation += reloc_entry->addend;
1047
1048 if (reloc_entry->address > input_section->_cooked_size)
1049 return bfd_reloc_outofrange;
1050
1051 gp_disp_relent = *reloc_entry;
1052 reloc_entry = &gp_disp_relent;
1053 reloc_entry->addend = relocation - 4;
1054 }
1055
1056 /* Now do the LO16 reloc in the usual way. */
1057 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1058 input_section, output_bfd, error_message);
1059 }
1060
1061 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1062 table used for PIC code. If the symbol is an external symbol, the
1063 instruction is modified to contain the offset of the appropriate
1064 entry in the global offset table. If the symbol is a section
1065 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1066 addends are combined to form the real addend against the section
1067 symbol; the GOT16 is modified to contain the offset of an entry in
1068 the global offset table, and the LO16 is modified to offset it
1069 appropriately. Thus an offset larger than 16 bits requires a
1070 modified value in the global offset table.
1071
1072 This implementation suffices for the assembler, but the linker does
1073 not yet know how to create global offset tables. */
1074
1075 bfd_reloc_status_type
1076 _bfd_mips_elf_got16_reloc (abfd,
1077 reloc_entry,
1078 symbol,
1079 data,
1080 input_section,
1081 output_bfd,
1082 error_message)
1083 bfd *abfd;
1084 arelent *reloc_entry;
1085 asymbol *symbol;
1086 PTR data;
1087 asection *input_section;
1088 bfd *output_bfd;
1089 char **error_message;
1090 {
1091 /* If we're relocating, and this an external symbol, we don't want
1092 to change anything. */
1093 if (output_bfd != (bfd *) NULL
1094 && (symbol->flags & BSF_SECTION_SYM) == 0
1095 && reloc_entry->addend == 0)
1096 {
1097 reloc_entry->address += input_section->output_offset;
1098 return bfd_reloc_ok;
1099 }
1100
1101 /* If we're relocating, and this is a local symbol, we can handle it
1102 just like HI16. */
1103 if (output_bfd != (bfd *) NULL
1104 && (symbol->flags & BSF_SECTION_SYM) != 0)
1105 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1106 input_section, output_bfd, error_message);
1107
1108 abort ();
1109 }
1110
1111 /* We have to figure out the gp value, so that we can adjust the
1112 symbol value correctly. We look up the symbol _gp in the output
1113 BFD. If we can't find it, we're stuck. We cache it in the ELF
1114 target data. We don't need to adjust the symbol value for an
1115 external symbol if we are producing relocateable output. */
1116
1117 static bfd_reloc_status_type
1118 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1119 bfd *output_bfd;
1120 asymbol *symbol;
1121 boolean relocateable;
1122 char **error_message;
1123 bfd_vma *pgp;
1124 {
1125 if (bfd_is_und_section (symbol->section)
1126 && ! relocateable)
1127 {
1128 *pgp = 0;
1129 return bfd_reloc_undefined;
1130 }
1131
1132 *pgp = _bfd_get_gp_value (output_bfd);
1133 if (*pgp == 0
1134 && (! relocateable
1135 || (symbol->flags & BSF_SECTION_SYM) != 0))
1136 {
1137 if (relocateable)
1138 {
1139 /* Make up a value. */
1140 *pgp = symbol->section->output_section->vma + 0x4000;
1141 _bfd_set_gp_value (output_bfd, *pgp);
1142 }
1143 else
1144 {
1145 unsigned int count;
1146 asymbol **sym;
1147 unsigned int i;
1148
1149 count = bfd_get_symcount (output_bfd);
1150 sym = bfd_get_outsymbols (output_bfd);
1151
1152 if (sym == (asymbol **) NULL)
1153 i = count;
1154 else
1155 {
1156 for (i = 0; i < count; i++, sym++)
1157 {
1158 register CONST char *name;
1159
1160 name = bfd_asymbol_name (*sym);
1161 if (*name == '_' && strcmp (name, "_gp") == 0)
1162 {
1163 *pgp = bfd_asymbol_value (*sym);
1164 _bfd_set_gp_value (output_bfd, *pgp);
1165 break;
1166 }
1167 }
1168 }
1169
1170 if (i >= count)
1171 {
1172 /* Only get the error once. */
1173 *pgp = 4;
1174 _bfd_set_gp_value (output_bfd, *pgp);
1175 *error_message =
1176 (char *) _("GP relative relocation when _gp not defined");
1177 return bfd_reloc_dangerous;
1178 }
1179 }
1180 }
1181
1182 return bfd_reloc_ok;
1183 }
1184
1185 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1186 become the offset from the gp register. This function also handles
1187 R_MIPS_LITERAL relocations, although those can be handled more
1188 cleverly because the entries in the .lit8 and .lit4 sections can be
1189 merged. */
1190
1191 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1192 arelent *, asection *,
1193 boolean, PTR, bfd_vma));
1194
1195 bfd_reloc_status_type
1196 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1197 output_bfd, error_message)
1198 bfd *abfd;
1199 arelent *reloc_entry;
1200 asymbol *symbol;
1201 PTR data;
1202 asection *input_section;
1203 bfd *output_bfd;
1204 char **error_message;
1205 {
1206 boolean relocateable;
1207 bfd_reloc_status_type ret;
1208 bfd_vma gp;
1209
1210 /* If we're relocating, and this is an external symbol with no
1211 addend, we don't want to change anything. We will only have an
1212 addend if this is a newly created reloc, not read from an ELF
1213 file. */
1214 if (output_bfd != (bfd *) NULL
1215 && (symbol->flags & BSF_SECTION_SYM) == 0
1216 && reloc_entry->addend == 0)
1217 {
1218 reloc_entry->address += input_section->output_offset;
1219 return bfd_reloc_ok;
1220 }
1221
1222 if (output_bfd != (bfd *) NULL)
1223 relocateable = true;
1224 else
1225 {
1226 relocateable = false;
1227 output_bfd = symbol->section->output_section->owner;
1228 }
1229
1230 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1231 &gp);
1232 if (ret != bfd_reloc_ok)
1233 return ret;
1234
1235 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1236 relocateable, data, gp);
1237 }
1238
1239 static bfd_reloc_status_type
1240 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1241 gp)
1242 bfd *abfd;
1243 asymbol *symbol;
1244 arelent *reloc_entry;
1245 asection *input_section;
1246 boolean relocateable;
1247 PTR data;
1248 bfd_vma gp;
1249 {
1250 bfd_vma relocation;
1251 unsigned long insn;
1252 unsigned long val;
1253
1254 if (bfd_is_com_section (symbol->section))
1255 relocation = 0;
1256 else
1257 relocation = symbol->value;
1258
1259 relocation += symbol->section->output_section->vma;
1260 relocation += symbol->section->output_offset;
1261
1262 if (reloc_entry->address > input_section->_cooked_size)
1263 return bfd_reloc_outofrange;
1264
1265 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1266
1267 /* Set val to the offset into the section or symbol. */
1268 if (reloc_entry->howto->src_mask == 0)
1269 {
1270 /* This case occurs with the 64-bit MIPS ELF ABI. */
1271 val = reloc_entry->addend;
1272 }
1273 else
1274 {
1275 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1276 if (val & 0x8000)
1277 val -= 0x10000;
1278 }
1279
1280 /* Adjust val for the final section location and GP value. If we
1281 are producing relocateable output, we don't want to do this for
1282 an external symbol. */
1283 if (! relocateable
1284 || (symbol->flags & BSF_SECTION_SYM) != 0)
1285 val += relocation - gp;
1286
1287 insn = (insn &~ 0xffff) | (val & 0xffff);
1288 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1289
1290 if (relocateable)
1291 reloc_entry->address += input_section->output_offset;
1292
1293 /* Make sure it fit in 16 bits. */
1294 if (val >= 0x8000 && val < 0xffff8000)
1295 return bfd_reloc_overflow;
1296
1297 return bfd_reloc_ok;
1298 }
1299
1300 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1301 from the gp register? XXX */
1302
1303 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1304 arelent *, asection *,
1305 boolean, PTR, bfd_vma));
1306
1307 bfd_reloc_status_type
1308 _bfd_mips_elf_gprel32_reloc (abfd,
1309 reloc_entry,
1310 symbol,
1311 data,
1312 input_section,
1313 output_bfd,
1314 error_message)
1315 bfd *abfd;
1316 arelent *reloc_entry;
1317 asymbol *symbol;
1318 PTR data;
1319 asection *input_section;
1320 bfd *output_bfd;
1321 char **error_message;
1322 {
1323 boolean relocateable;
1324 bfd_reloc_status_type ret;
1325 bfd_vma gp;
1326
1327 /* If we're relocating, and this is an external symbol with no
1328 addend, we don't want to change anything. We will only have an
1329 addend if this is a newly created reloc, not read from an ELF
1330 file. */
1331 if (output_bfd != (bfd *) NULL
1332 && (symbol->flags & BSF_SECTION_SYM) == 0
1333 && reloc_entry->addend == 0)
1334 {
1335 *error_message = (char *)
1336 _("32bits gp relative relocation occurs for an external symbol");
1337 return bfd_reloc_outofrange;
1338 }
1339
1340 if (output_bfd != (bfd *) NULL)
1341 {
1342 relocateable = true;
1343 gp = _bfd_get_gp_value (output_bfd);
1344 }
1345 else
1346 {
1347 relocateable = false;
1348 output_bfd = symbol->section->output_section->owner;
1349
1350 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1351 error_message, &gp);
1352 if (ret != bfd_reloc_ok)
1353 return ret;
1354 }
1355
1356 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1357 relocateable, data, gp);
1358 }
1359
1360 static bfd_reloc_status_type
1361 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1362 gp)
1363 bfd *abfd;
1364 asymbol *symbol;
1365 arelent *reloc_entry;
1366 asection *input_section;
1367 boolean relocateable;
1368 PTR data;
1369 bfd_vma gp;
1370 {
1371 bfd_vma relocation;
1372 unsigned long val;
1373
1374 if (bfd_is_com_section (symbol->section))
1375 relocation = 0;
1376 else
1377 relocation = symbol->value;
1378
1379 relocation += symbol->section->output_section->vma;
1380 relocation += symbol->section->output_offset;
1381
1382 if (reloc_entry->address > input_section->_cooked_size)
1383 return bfd_reloc_outofrange;
1384
1385 if (reloc_entry->howto->src_mask == 0)
1386 {
1387 /* This case arises with the 64-bit MIPS ELF ABI. */
1388 val = 0;
1389 }
1390 else
1391 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1392
1393 /* Set val to the offset into the section or symbol. */
1394 val += reloc_entry->addend;
1395
1396 /* Adjust val for the final section location and GP value. If we
1397 are producing relocateable output, we don't want to do this for
1398 an external symbol. */
1399 if (! relocateable
1400 || (symbol->flags & BSF_SECTION_SYM) != 0)
1401 val += relocation - gp;
1402
1403 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1404
1405 if (relocateable)
1406 reloc_entry->address += input_section->output_offset;
1407
1408 return bfd_reloc_ok;
1409 }
1410
1411 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1412 generated when addreses are 64 bits. The upper 32 bits are a simle
1413 sign extension. */
1414
1415 static bfd_reloc_status_type
1416 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1417 output_bfd, error_message)
1418 bfd *abfd;
1419 arelent *reloc_entry;
1420 asymbol *symbol;
1421 PTR data;
1422 asection *input_section;
1423 bfd *output_bfd;
1424 char **error_message;
1425 {
1426 bfd_reloc_status_type r;
1427 arelent reloc32;
1428 unsigned long val;
1429 bfd_size_type addr;
1430
1431 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1432 input_section, output_bfd, error_message);
1433 if (r != bfd_reloc_continue)
1434 return r;
1435
1436 /* Do a normal 32 bit relocation on the lower 32 bits. */
1437 reloc32 = *reloc_entry;
1438 if (bfd_big_endian (abfd))
1439 reloc32.address += 4;
1440 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1441 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1442 output_bfd, error_message);
1443
1444 /* Sign extend into the upper 32 bits. */
1445 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1446 if ((val & 0x80000000) != 0)
1447 val = 0xffffffff;
1448 else
1449 val = 0;
1450 addr = reloc_entry->address;
1451 if (bfd_little_endian (abfd))
1452 addr += 4;
1453 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1454
1455 return r;
1456 }
1457
1458 /* Handle a mips16 jump. */
1459
1460 static bfd_reloc_status_type
1461 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1462 output_bfd, error_message)
1463 bfd *abfd;
1464 arelent *reloc_entry;
1465 asymbol *symbol;
1466 PTR data;
1467 asection *input_section;
1468 bfd *output_bfd;
1469 char **error_message;
1470 {
1471 if (output_bfd != (bfd *) NULL
1472 && (symbol->flags & BSF_SECTION_SYM) == 0
1473 && reloc_entry->addend == 0)
1474 {
1475 reloc_entry->address += input_section->output_offset;
1476 return bfd_reloc_ok;
1477 }
1478
1479 /* FIXME. */
1480 {
1481 static boolean warned;
1482
1483 if (! warned)
1484 (*_bfd_error_handler)
1485 (_("Linking mips16 objects into %s format is not supported"),
1486 bfd_get_target (input_section->output_section->owner));
1487 warned = true;
1488 }
1489
1490 return bfd_reloc_undefined;
1491 }
1492
1493 /* Handle a mips16 GP relative reloc. */
1494
1495 static bfd_reloc_status_type
1496 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1497 output_bfd, error_message)
1498 bfd *abfd;
1499 arelent *reloc_entry;
1500 asymbol *symbol;
1501 PTR data;
1502 asection *input_section;
1503 bfd *output_bfd;
1504 char **error_message;
1505 {
1506 boolean relocateable;
1507 bfd_reloc_status_type ret;
1508 bfd_vma gp;
1509 unsigned short extend, insn;
1510 unsigned long final;
1511
1512 /* If we're relocating, and this is an external symbol with no
1513 addend, we don't want to change anything. We will only have an
1514 addend if this is a newly created reloc, not read from an ELF
1515 file. */
1516 if (output_bfd != NULL
1517 && (symbol->flags & BSF_SECTION_SYM) == 0
1518 && reloc_entry->addend == 0)
1519 {
1520 reloc_entry->address += input_section->output_offset;
1521 return bfd_reloc_ok;
1522 }
1523
1524 if (output_bfd != NULL)
1525 relocateable = true;
1526 else
1527 {
1528 relocateable = false;
1529 output_bfd = symbol->section->output_section->owner;
1530 }
1531
1532 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1533 &gp);
1534 if (ret != bfd_reloc_ok)
1535 return ret;
1536
1537 if (reloc_entry->address > input_section->_cooked_size)
1538 return bfd_reloc_outofrange;
1539
1540 /* Pick up the mips16 extend instruction and the real instruction. */
1541 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1542 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1543
1544 /* Stuff the current addend back as a 32 bit value, do the usual
1545 relocation, and then clean up. */
1546 bfd_put_32 (abfd,
1547 (((extend & 0x1f) << 11)
1548 | (extend & 0x7e0)
1549 | (insn & 0x1f)),
1550 (bfd_byte *) data + reloc_entry->address);
1551
1552 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1553 relocateable, data, gp);
1554
1555 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1556 bfd_put_16 (abfd,
1557 ((extend & 0xf800)
1558 | ((final >> 11) & 0x1f)
1559 | (final & 0x7e0)),
1560 (bfd_byte *) data + reloc_entry->address);
1561 bfd_put_16 (abfd,
1562 ((insn & 0xffe0)
1563 | (final & 0x1f)),
1564 (bfd_byte *) data + reloc_entry->address + 2);
1565
1566 return ret;
1567 }
1568
1569 /* Return the ISA for a MIPS e_flags value. */
1570
1571 static INLINE int
1572 elf_mips_isa (flags)
1573 flagword flags;
1574 {
1575 switch (flags & EF_MIPS_ARCH)
1576 {
1577 case E_MIPS_ARCH_1:
1578 return 1;
1579 case E_MIPS_ARCH_2:
1580 return 2;
1581 case E_MIPS_ARCH_3:
1582 return 3;
1583 case E_MIPS_ARCH_4:
1584 return 4;
1585 }
1586 return 4;
1587 }
1588
1589 /* Return the MACH for a MIPS e_flags value. */
1590
1591 static INLINE int
1592 elf_mips_mach (flags)
1593 flagword flags;
1594 {
1595 switch (flags & EF_MIPS_MACH)
1596 {
1597 case E_MIPS_MACH_3900:
1598 return bfd_mach_mips3900;
1599
1600 case E_MIPS_MACH_4010:
1601 return bfd_mach_mips4010;
1602
1603 case E_MIPS_MACH_4100:
1604 return bfd_mach_mips4100;
1605 /* start-sanitize-vr4320 */
1606
1607 case E_MIPS_MACH_4320:
1608 return bfd_mach_mips4320;
1609 /* end-sanitize-vr4320 */
1610
1611 case E_MIPS_MACH_4650:
1612 return bfd_mach_mips4650;
1613 /* start-sanitize-tx49 */
1614
1615 case E_MIPS_MACH_4900:
1616 return bfd_mach_mips4900;
1617 /* end-sanitize-tx49 */
1618 /* start-sanitize-vr5400 */
1619
1620 case E_MIPS_MACH_5400:
1621 return bfd_mach_mips5400;
1622 /* end-sanitize-vr5400 */
1623 /* start-sanitize-r5900 */
1624
1625 case E_MIPS_MACH_5900:
1626 return bfd_mach_mips5900;
1627 /* end-sanitize-r5900 */
1628
1629 default:
1630 switch (flags & EF_MIPS_ARCH)
1631 {
1632 default:
1633 case E_MIPS_ARCH_1:
1634 return bfd_mach_mips3000;
1635 break;
1636
1637 case E_MIPS_ARCH_2:
1638 return bfd_mach_mips6000;
1639 break;
1640
1641 case E_MIPS_ARCH_3:
1642 return bfd_mach_mips4000;
1643 break;
1644
1645 case E_MIPS_ARCH_4:
1646 return bfd_mach_mips8000;
1647 break;
1648 }
1649 }
1650
1651 return 0;
1652 }
1653
1654 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1655
1656 struct elf_reloc_map {
1657 bfd_reloc_code_real_type bfd_reloc_val;
1658 enum reloc_type elf_reloc_val;
1659 };
1660
1661 static CONST struct elf_reloc_map mips_reloc_map[] =
1662 {
1663 { BFD_RELOC_NONE, R_MIPS_NONE, },
1664 { BFD_RELOC_16, R_MIPS_16 },
1665 { BFD_RELOC_32, R_MIPS_32 },
1666 { BFD_RELOC_64, R_MIPS_64 },
1667 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1668 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1669 { BFD_RELOC_LO16, R_MIPS_LO16 },
1670 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1671 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1672 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1673 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1674 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1675 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1676 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1677 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1678 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1679 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1680 /* start-sanitize-r5900 */
1681 { BFD_RELOC_MIPS15_S3, R_MIPS15_S3 },
1682 /* end-sanitize-r5900 */
1683 };
1684
1685 /* Given a BFD reloc type, return a howto structure. */
1686
1687 static reloc_howto_type *
1688 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1689 bfd *abfd;
1690 bfd_reloc_code_real_type code;
1691 {
1692 unsigned int i;
1693
1694 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1695 {
1696 if (mips_reloc_map[i].bfd_reloc_val == code)
1697 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1698 }
1699
1700 switch (code)
1701 {
1702 case BFD_RELOC_CTOR:
1703 /* We need to handle BFD_RELOC_CTOR specially.
1704 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1705 size of addresses on this architecture. */
1706 if (bfd_arch_bits_per_address (abfd) == 32)
1707 return &elf_mips_howto_table[(int) R_MIPS_32];
1708 else
1709 return &elf_mips_ctor64_howto;
1710
1711 case BFD_RELOC_MIPS16_JMP:
1712 return &elf_mips16_jump_howto;
1713 case BFD_RELOC_MIPS16_GPREL:
1714 return &elf_mips16_gprel_howto;
1715 /* start-sanitize-sky */
1716 case BFD_RELOC_MIPS_DVP_11_PCREL:
1717 return &elf_mips_dvp_11_pcrel_howto;
1718 case BFD_RELOC_MIPS_DVP_27_S4:
1719 return &elf_mips_dvp_27_s4_howto;
1720 /* end-sanitize-sky */
1721 case BFD_RELOC_VTABLE_INHERIT:
1722 return &elf_mips_gnu_vtinherit_howto;
1723 case BFD_RELOC_VTABLE_ENTRY:
1724 return &elf_mips_gnu_vtentry_howto;
1725 }
1726
1727 return NULL;
1728 }
1729
1730 /* Given a MIPS reloc type, fill in an arelent structure. */
1731
1732 static void
1733 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1734 bfd *abfd;
1735 arelent *cache_ptr;
1736 Elf32_Internal_Rel *dst;
1737 {
1738 unsigned int r_type;
1739
1740 r_type = ELF32_R_TYPE (dst->r_info);
1741 switch (r_type)
1742 {
1743 case R_MIPS16_26:
1744 cache_ptr->howto = &elf_mips16_jump_howto;
1745 break;
1746 case R_MIPS16_GPREL:
1747 cache_ptr->howto = &elf_mips16_gprel_howto;
1748 break;
1749 /* start-sanitize-sky */
1750 case R_MIPS_DVP_11_PCREL:
1751 cache_ptr->howto = &elf_mips_dvp_11_pcrel_howto;
1752 break;
1753 case R_MIPS_DVP_27_S4:
1754 cache_ptr->howto = &elf_mips_dvp_27_s4_howto;
1755 break;
1756 /* end-sanitize-sky */
1757 case R_MIPS_GNU_VTINHERIT:
1758 cache_ptr->howto = &elf_mips_gnu_vtinherit_howto;
1759 break;
1760 case R_MIPS_GNU_VTENTRY:
1761 cache_ptr->howto = &elf_mips_gnu_vtentry_howto;
1762 break;
1763
1764 default:
1765 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1766 cache_ptr->howto = &elf_mips_howto_table[r_type];
1767 break;
1768 }
1769
1770 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1771 value for the object file. We get the addend now, rather than
1772 when we do the relocation, because the symbol manipulations done
1773 by the linker may cause us to lose track of the input BFD. */
1774 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1775 && (r_type == (unsigned int) R_MIPS_GPREL16
1776 || r_type == (unsigned int) R_MIPS_LITERAL))
1777 cache_ptr->addend = elf_gp (abfd);
1778 }
1779 \f
1780 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1781 routines swap this structure in and out. They are used outside of
1782 BFD, so they are globally visible. */
1783
1784 void
1785 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1786 bfd *abfd;
1787 const Elf32_External_RegInfo *ex;
1788 Elf32_RegInfo *in;
1789 {
1790 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1791 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1792 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1793 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1794 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1795 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1796 }
1797
1798 void
1799 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1800 bfd *abfd;
1801 const Elf32_RegInfo *in;
1802 Elf32_External_RegInfo *ex;
1803 {
1804 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1805 (bfd_byte *) ex->ri_gprmask);
1806 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1807 (bfd_byte *) ex->ri_cprmask[0]);
1808 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1809 (bfd_byte *) ex->ri_cprmask[1]);
1810 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1811 (bfd_byte *) ex->ri_cprmask[2]);
1812 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1813 (bfd_byte *) ex->ri_cprmask[3]);
1814 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1815 (bfd_byte *) ex->ri_gp_value);
1816 }
1817
1818 /* In the 64 bit ABI, the .MIPS.options section holds register
1819 information in an Elf64_Reginfo structure. These routines swap
1820 them in and out. They are globally visible because they are used
1821 outside of BFD. These routines are here so that gas can call them
1822 without worrying about whether the 64 bit ABI has been included. */
1823
1824 void
1825 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1826 bfd *abfd;
1827 const Elf64_External_RegInfo *ex;
1828 Elf64_Internal_RegInfo *in;
1829 {
1830 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1831 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
1832 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1833 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1834 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1835 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1836 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
1837 }
1838
1839 void
1840 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
1841 bfd *abfd;
1842 const Elf64_Internal_RegInfo *in;
1843 Elf64_External_RegInfo *ex;
1844 {
1845 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1846 (bfd_byte *) ex->ri_gprmask);
1847 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
1848 (bfd_byte *) ex->ri_pad);
1849 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1850 (bfd_byte *) ex->ri_cprmask[0]);
1851 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1852 (bfd_byte *) ex->ri_cprmask[1]);
1853 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1854 (bfd_byte *) ex->ri_cprmask[2]);
1855 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1856 (bfd_byte *) ex->ri_cprmask[3]);
1857 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
1858 (bfd_byte *) ex->ri_gp_value);
1859 }
1860
1861 /* Swap an entry in a .gptab section. Note that these routines rely
1862 on the equivalence of the two elements of the union. */
1863
1864 static void
1865 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1866 bfd *abfd;
1867 const Elf32_External_gptab *ex;
1868 Elf32_gptab *in;
1869 {
1870 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
1871 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
1872 }
1873
1874 static void
1875 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1876 bfd *abfd;
1877 const Elf32_gptab *in;
1878 Elf32_External_gptab *ex;
1879 {
1880 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
1881 ex->gt_entry.gt_g_value);
1882 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
1883 ex->gt_entry.gt_bytes);
1884 }
1885
1886 static void
1887 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
1888 bfd *abfd;
1889 const Elf32_compact_rel *in;
1890 Elf32_External_compact_rel *ex;
1891 {
1892 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
1893 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
1894 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
1895 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
1896 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
1897 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
1898 }
1899
1900 static void
1901 bfd_elf32_swap_crinfo_out (abfd, in, ex)
1902 bfd *abfd;
1903 const Elf32_crinfo *in;
1904 Elf32_External_crinfo *ex;
1905 {
1906 unsigned long l;
1907
1908 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1909 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1910 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1911 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1912 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
1913 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
1914 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
1915 }
1916
1917 /* Swap in an options header. */
1918
1919 void
1920 bfd_mips_elf_swap_options_in (abfd, ex, in)
1921 bfd *abfd;
1922 const Elf_External_Options *ex;
1923 Elf_Internal_Options *in;
1924 {
1925 in->kind = bfd_h_get_8 (abfd, ex->kind);
1926 in->size = bfd_h_get_8 (abfd, ex->size);
1927 in->section = bfd_h_get_16 (abfd, ex->section);
1928 in->info = bfd_h_get_32 (abfd, ex->info);
1929 }
1930
1931 /* Swap out an options header. */
1932
1933 void
1934 bfd_mips_elf_swap_options_out (abfd, in, ex)
1935 bfd *abfd;
1936 const Elf_Internal_Options *in;
1937 Elf_External_Options *ex;
1938 {
1939 bfd_h_put_8 (abfd, in->kind, ex->kind);
1940 bfd_h_put_8 (abfd, in->size, ex->size);
1941 bfd_h_put_16 (abfd, in->section, ex->section);
1942 bfd_h_put_32 (abfd, in->info, ex->info);
1943 }
1944 \f
1945 /* Determine whether a symbol is global for the purposes of splitting
1946 the symbol table into global symbols and local symbols. At least
1947 on Irix 5, this split must be between section symbols and all other
1948 symbols. On most ELF targets the split is between static symbols
1949 and externally visible symbols. */
1950
1951 /*ARGSUSED*/
1952 static boolean
1953 mips_elf_sym_is_global (abfd, sym)
1954 bfd *abfd;
1955 asymbol *sym;
1956 {
1957 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
1958 }
1959 \f
1960 /* Set the right machine number for a MIPS ELF file. This is used for
1961 both the 32-bit and the 64-bit ABI. */
1962
1963 boolean
1964 _bfd_mips_elf_object_p (abfd)
1965 bfd *abfd;
1966 {
1967 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
1968 elf_mips_mach (elf_elfheader (abfd)->e_flags));
1969 return true;
1970 }
1971
1972 /* Set the right machine number for a 32-bit MIPS ELF file. */
1973
1974 static boolean
1975 mips_elf32_object_p (abfd)
1976 bfd *abfd;
1977 {
1978 /* Irix 5 is broken. Object file symbol tables are not always
1979 sorted correctly such that local symbols precede global symbols,
1980 and the sh_info field in the symbol table is not always right. */
1981 elf_bad_symtab (abfd) = true;
1982
1983 return _bfd_mips_elf_object_p (abfd);
1984 }
1985
1986 /* The final processing done just before writing out a MIPS ELF object
1987 file. This gets the MIPS architecture right based on the machine
1988 number. This is used by both the 32-bit and the 64-bit ABI. */
1989
1990 /*ARGSUSED*/
1991 void
1992 _bfd_mips_elf_final_write_processing (abfd, linker)
1993 bfd *abfd;
1994 boolean linker;
1995 {
1996 unsigned long val;
1997 unsigned int i;
1998 Elf_Internal_Shdr **hdrpp;
1999 const char *name;
2000 asection *sec;
2001
2002 switch (bfd_get_mach (abfd))
2003 {
2004 default:
2005 case bfd_mach_mips3000:
2006 val = E_MIPS_ARCH_1;
2007 break;
2008
2009 case bfd_mach_mips3900:
2010 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
2011 break;
2012
2013 case bfd_mach_mips6000:
2014 val = E_MIPS_ARCH_2;
2015 break;
2016
2017 case bfd_mach_mips4000:
2018 val = E_MIPS_ARCH_3;
2019 break;
2020
2021 case bfd_mach_mips4010:
2022 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
2023 break;
2024
2025 case bfd_mach_mips4100:
2026 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
2027 break;
2028 /* start-sanitize-vr4320 */
2029
2030 case bfd_mach_mips4320:
2031 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4320;
2032 break;
2033 /* end-sanitize-vr4320 */
2034
2035 case bfd_mach_mips4650:
2036 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
2037 break;
2038 /* start-sanitize-tx49 */
2039
2040 case bfd_mach_mips4900:
2041 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4900;
2042 break;
2043 /* end-sanitize-tx49 */
2044 /* start-sanitize-vr5400 */
2045
2046 case bfd_mach_mips5400:
2047 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5400;
2048 break;
2049 /* end-sanitize-vr5400 */
2050 /* start-sanitize-r5900 */
2051
2052 case bfd_mach_mips5900:
2053 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
2054 break;
2055 /* end-sanitize-r5900 */
2056
2057 case bfd_mach_mips8000:
2058 val = E_MIPS_ARCH_4;
2059 break;
2060 }
2061
2062 elf_elfheader (abfd)->e_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2063 elf_elfheader (abfd)->e_flags |= val;
2064
2065 /* Set the sh_info field for .gptab sections and other appropriate
2066 info for each special section. */
2067 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
2068 i < elf_elfheader (abfd)->e_shnum;
2069 i++, hdrpp++)
2070 {
2071 switch ((*hdrpp)->sh_type)
2072 {
2073 case SHT_MIPS_LIBLIST:
2074 sec = bfd_get_section_by_name (abfd, ".dynstr");
2075 if (sec != NULL)
2076 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2077 break;
2078
2079 case SHT_MIPS_GPTAB:
2080 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2081 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2082 BFD_ASSERT (name != NULL
2083 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
2084 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
2085 BFD_ASSERT (sec != NULL);
2086 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2087 break;
2088
2089 case SHT_MIPS_CONTENT:
2090 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2091 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2092 BFD_ASSERT (name != NULL
2093 && strncmp (name, ".MIPS.content",
2094 sizeof ".MIPS.content" - 1) == 0);
2095 sec = bfd_get_section_by_name (abfd,
2096 name + sizeof ".MIPS.content" - 1);
2097 BFD_ASSERT (sec != NULL);
2098 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2099 break;
2100
2101 case SHT_MIPS_SYMBOL_LIB:
2102 sec = bfd_get_section_by_name (abfd, ".dynsym");
2103 if (sec != NULL)
2104 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2105 sec = bfd_get_section_by_name (abfd, ".liblist");
2106 if (sec != NULL)
2107 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2108 break;
2109
2110 case SHT_MIPS_EVENTS:
2111 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2112 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2113 BFD_ASSERT (name != NULL);
2114 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
2115 sec = bfd_get_section_by_name (abfd,
2116 name + sizeof ".MIPS.events" - 1);
2117 else
2118 {
2119 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
2120 sizeof ".MIPS.post_rel" - 1) == 0);
2121 sec = bfd_get_section_by_name (abfd,
2122 (name
2123 + sizeof ".MIPS.post_rel" - 1));
2124 }
2125 BFD_ASSERT (sec != NULL);
2126 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2127 break;
2128
2129 /* start-sanitize-sky */
2130 case SHT_DVP_OVERLAY_TABLE:
2131 /* ??? This may not be technically necessary, just going with
2132 the flow ... */
2133 sec = bfd_get_section_by_name (abfd, SHNAME_DVP_OVERLAY_STRTAB);
2134 if (sec != NULL)
2135 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2136 break;
2137 /* end-sanitize-sky */
2138 }
2139 }
2140 }
2141 \f
2142 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
2143
2144 boolean
2145 _bfd_mips_elf_set_private_flags (abfd, flags)
2146 bfd *abfd;
2147 flagword flags;
2148 {
2149 BFD_ASSERT (!elf_flags_init (abfd)
2150 || elf_elfheader (abfd)->e_flags == flags);
2151
2152 elf_elfheader (abfd)->e_flags = flags;
2153 elf_flags_init (abfd) = true;
2154 return true;
2155 }
2156
2157 /* Copy backend specific data from one object module to another */
2158
2159 boolean
2160 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
2161 bfd *ibfd;
2162 bfd *obfd;
2163 {
2164 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2165 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2166 return true;
2167
2168 BFD_ASSERT (!elf_flags_init (obfd)
2169 || (elf_elfheader (obfd)->e_flags
2170 == elf_elfheader (ibfd)->e_flags));
2171
2172 elf_gp (obfd) = elf_gp (ibfd);
2173 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
2174 elf_flags_init (obfd) = true;
2175 return true;
2176 }
2177
2178 /* Merge backend specific data from an object file to the output
2179 object file when linking. */
2180
2181 boolean
2182 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
2183 bfd *ibfd;
2184 bfd *obfd;
2185 {
2186 flagword old_flags;
2187 flagword new_flags;
2188 boolean ok;
2189
2190 /* Check if we have the same endianess */
2191 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2192 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2193 {
2194 const char *msg;
2195
2196 if (bfd_big_endian (ibfd))
2197 msg = _("%s: compiled for a big endian system and target is little endian");
2198 else
2199 msg = _("%s: compiled for a little endian system and target is big endian");
2200
2201 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
2202
2203 bfd_set_error (bfd_error_wrong_format);
2204 return false;
2205 }
2206
2207 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2208 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2209 return true;
2210
2211 new_flags = elf_elfheader (ibfd)->e_flags;
2212 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2213 old_flags = elf_elfheader (obfd)->e_flags;
2214
2215 if (! elf_flags_init (obfd))
2216 {
2217 elf_flags_init (obfd) = true;
2218 elf_elfheader (obfd)->e_flags = new_flags;
2219
2220 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2221 && bfd_get_arch_info (obfd)->the_default)
2222 {
2223 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2224 bfd_get_mach (ibfd)))
2225 return false;
2226 }
2227
2228 return true;
2229 }
2230
2231 /* Check flag compatibility. */
2232
2233 new_flags &= ~EF_MIPS_NOREORDER;
2234 old_flags &= ~EF_MIPS_NOREORDER;
2235
2236 if (new_flags == old_flags)
2237 return true;
2238
2239 ok = true;
2240
2241 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2242 {
2243 new_flags &= ~EF_MIPS_PIC;
2244 old_flags &= ~EF_MIPS_PIC;
2245 (*_bfd_error_handler)
2246 (_("%s: linking PIC files with non-PIC files"),
2247 bfd_get_filename (ibfd));
2248 ok = false;
2249 }
2250
2251 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2252 {
2253 new_flags &= ~EF_MIPS_CPIC;
2254 old_flags &= ~EF_MIPS_CPIC;
2255 (*_bfd_error_handler)
2256 (_("%s: linking abicalls files with non-abicalls files"),
2257 bfd_get_filename (ibfd));
2258 ok = false;
2259 }
2260
2261 /* Compare the ISA's. */
2262 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2263 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2264 {
2265 /* If either has no machine specified, just compare the general isa's. */
2266 if ( !(new_flags & EF_MIPS_MACH) || !(old_flags & EF_MIPS_MACH))
2267 {
2268 int new_isa, old_isa;
2269
2270 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2271 and -mips4 code. They will normally use the same data sizes and
2272 calling conventions. */
2273
2274 new_isa = elf_mips_isa (new_flags);
2275 old_isa = elf_mips_isa (old_flags);
2276 if ((new_isa == 1 || new_isa == 2)
2277 ? (old_isa != 1 && old_isa != 2)
2278 : (old_isa == 1 || old_isa == 2))
2279 {
2280 (*_bfd_error_handler)
2281 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2282 bfd_get_filename (ibfd), new_isa, old_isa);
2283 ok = false;
2284 }
2285 }
2286
2287 else
2288 {
2289 (*_bfd_error_handler)
2290 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2291 bfd_get_filename (ibfd),
2292 elf_mips_mach (new_flags),
2293 elf_mips_mach (old_flags));
2294 ok = false;
2295 }
2296
2297 new_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2298 old_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2299 }
2300
2301 /* Warn about any other mismatches */
2302 if (new_flags != old_flags)
2303 {
2304 (*_bfd_error_handler)
2305 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2306 bfd_get_filename (ibfd), (unsigned long) new_flags,
2307 (unsigned long) old_flags);
2308 ok = false;
2309 }
2310
2311 if (! ok)
2312 {
2313 bfd_set_error (bfd_error_bad_value);
2314 return false;
2315 }
2316
2317 return true;
2318 }
2319 \f
2320 /* Handle a MIPS specific section when reading an object file. This
2321 is called when elfcode.h finds a section with an unknown type.
2322 This routine supports both the 32-bit and 64-bit ELF ABI.
2323
2324 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2325 how to. */
2326
2327 boolean
2328 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2329 bfd *abfd;
2330 Elf_Internal_Shdr *hdr;
2331 const char *name;
2332 {
2333 /* There ought to be a place to keep ELF backend specific flags, but
2334 at the moment there isn't one. We just keep track of the
2335 sections by their name, instead. Fortunately, the ABI gives
2336 suggested names for all the MIPS specific sections, so we will
2337 probably get away with this. */
2338 switch (hdr->sh_type)
2339 {
2340 case SHT_MIPS_LIBLIST:
2341 if (strcmp (name, _(".liblist")) != 0)
2342 return false;
2343 break;
2344 case SHT_MIPS_MSYM:
2345 if (strcmp (name, ".msym") != 0)
2346 return false;
2347 break;
2348 case SHT_MIPS_CONFLICT:
2349 if (strcmp (name, ".conflict") != 0)
2350 return false;
2351 break;
2352 case SHT_MIPS_GPTAB:
2353 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2354 return false;
2355 break;
2356 case SHT_MIPS_UCODE:
2357 if (strcmp (name, ".ucode") != 0)
2358 return false;
2359 break;
2360 case SHT_MIPS_DEBUG:
2361 if (strcmp (name, ".mdebug") != 0)
2362 return false;
2363 break;
2364 case SHT_MIPS_REGINFO:
2365 if (strcmp (name, ".reginfo") != 0
2366 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2367 return false;
2368 break;
2369 case SHT_MIPS_IFACE:
2370 if (strcmp (name, ".MIPS.interfaces") != 0)
2371 return false;
2372 break;
2373 case SHT_MIPS_CONTENT:
2374 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2375 return false;
2376 break;
2377 case SHT_MIPS_OPTIONS:
2378 if (strcmp (name, ".options") != 0
2379 && strcmp (name, ".MIPS.options") != 0)
2380 return false;
2381 break;
2382 case SHT_MIPS_DWARF:
2383 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2384 return false;
2385 break;
2386 case SHT_MIPS_SYMBOL_LIB:
2387 if (strcmp (name, ".MIPS.symlib") != 0)
2388 return false;
2389 break;
2390 case SHT_MIPS_EVENTS:
2391 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2392 && strncmp (name, ".MIPS.post_rel",
2393 sizeof ".MIPS.post_rel" - 1) != 0)
2394 return false;
2395 break;
2396 /* start-sanitize-sky */
2397 case SHT_DVP_OVERLAY_TABLE:
2398 if (strcmp (name, SHNAME_DVP_OVERLAY_TABLE) !=0)
2399 return false;
2400 break;
2401 case SHT_DVP_OVERLAY:
2402 if (strncmp (name, SHNAME_DVP_OVERLAY_PREFIX,
2403 sizeof (SHNAME_DVP_OVERLAY_PREFIX) - 1) !=0)
2404 return false;
2405 break;
2406 /* end-sanitize-sky */
2407 default:
2408 return false;
2409 }
2410
2411 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2412 return false;
2413
2414 if (hdr->sh_type == SHT_MIPS_DEBUG)
2415 {
2416 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2417 (bfd_get_section_flags (abfd,
2418 hdr->bfd_section)
2419 | SEC_DEBUGGING)))
2420 return false;
2421 }
2422
2423 return true;
2424 }
2425
2426 /* Handle a 32-bit MIPS ELF specific section. */
2427
2428 static boolean
2429 mips_elf32_section_from_shdr (abfd, hdr, name)
2430 bfd *abfd;
2431 Elf_Internal_Shdr *hdr;
2432 char *name;
2433 {
2434 if (! _bfd_mips_elf_section_from_shdr (abfd, hdr, name))
2435 return false;
2436
2437 /* FIXME: We should record sh_info for a .gptab section. */
2438
2439 /* For a .reginfo section, set the gp value in the tdata information
2440 from the contents of this section. We need the gp value while
2441 processing relocs, so we just get it now. The .reginfo section
2442 is not used in the 64-bit MIPS ELF ABI. */
2443 if (hdr->sh_type == SHT_MIPS_REGINFO)
2444 {
2445 Elf32_External_RegInfo ext;
2446 Elf32_RegInfo s;
2447
2448 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2449 (file_ptr) 0, sizeof ext))
2450 return false;
2451 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2452 elf_gp (abfd) = s.ri_gp_value;
2453 }
2454
2455 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2456 set the gp value based on what we find. We may see both
2457 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2458 they should agree. */
2459 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2460 {
2461 bfd_byte *contents, *l, *lend;
2462
2463 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2464 if (contents == NULL)
2465 return false;
2466 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2467 (file_ptr) 0, hdr->sh_size))
2468 {
2469 free (contents);
2470 return false;
2471 }
2472 l = contents;
2473 lend = contents + hdr->sh_size;
2474 while (l + sizeof (Elf_External_Options) <= lend)
2475 {
2476 Elf_Internal_Options intopt;
2477
2478 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2479 &intopt);
2480 if (intopt.kind == ODK_REGINFO)
2481 {
2482 Elf32_RegInfo intreg;
2483
2484 bfd_mips_elf32_swap_reginfo_in
2485 (abfd,
2486 ((Elf32_External_RegInfo *)
2487 (l + sizeof (Elf_External_Options))),
2488 &intreg);
2489 elf_gp (abfd) = intreg.ri_gp_value;
2490 }
2491 l += intopt.size;
2492 }
2493 free (contents);
2494 }
2495
2496 return true;
2497 }
2498
2499 /* Set the correct type for a MIPS ELF section. We do this by the
2500 section name, which is a hack, but ought to work. This routine is
2501 used by both the 32-bit and the 64-bit ABI. */
2502
2503 boolean
2504 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2505 bfd *abfd;
2506 Elf32_Internal_Shdr *hdr;
2507 asection *sec;
2508 {
2509 register const char *name;
2510
2511 name = bfd_get_section_name (abfd, sec);
2512
2513 if (strcmp (name, ".liblist") == 0)
2514 {
2515 hdr->sh_type = SHT_MIPS_LIBLIST;
2516 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2517 /* The sh_link field is set in final_write_processing. */
2518 }
2519 else if (strcmp (name, ".msym") == 0)
2520 {
2521 hdr->sh_type = SHT_MIPS_MSYM;
2522 hdr->sh_entsize = 8;
2523 /* FIXME: Set the sh_info field. */
2524 }
2525 else if (strcmp (name, ".conflict") == 0)
2526 hdr->sh_type = SHT_MIPS_CONFLICT;
2527 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2528 {
2529 hdr->sh_type = SHT_MIPS_GPTAB;
2530 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2531 /* The sh_info field is set in final_write_processing. */
2532 }
2533 else if (strcmp (name, ".ucode") == 0)
2534 hdr->sh_type = SHT_MIPS_UCODE;
2535 else if (strcmp (name, ".mdebug") == 0)
2536 {
2537 hdr->sh_type = SHT_MIPS_DEBUG;
2538 /* In a shared object on Irix 5.3, the .mdebug section has an
2539 entsize of 0. FIXME: Does this matter? */
2540 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2541 hdr->sh_entsize = 0;
2542 else
2543 hdr->sh_entsize = 1;
2544 }
2545 else if (strcmp (name, ".reginfo") == 0)
2546 {
2547 hdr->sh_type = SHT_MIPS_REGINFO;
2548 /* In a shared object on Irix 5.3, the .reginfo section has an
2549 entsize of 0x18. FIXME: Does this matter? */
2550 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2551 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2552 else
2553 hdr->sh_entsize = 1;
2554
2555 /* Force the section size to the correct value, even if the
2556 linker thinks it is larger. The link routine below will only
2557 write out this much data for .reginfo. */
2558 hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
2559 }
2560 else if (SGI_COMPAT (abfd)
2561 && (strcmp (name, ".hash") == 0
2562 || strcmp (name, ".dynamic") == 0
2563 || strcmp (name, ".dynstr") == 0))
2564 {
2565 hdr->sh_entsize = 0;
2566 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2567 }
2568 else if (strcmp (name, ".got") == 0
2569 || strcmp (name, ".sdata") == 0
2570 || strcmp (name, ".sbss") == 0
2571 || strcmp (name, ".lit4") == 0
2572 || strcmp (name, ".lit8") == 0)
2573 hdr->sh_flags |= SHF_MIPS_GPREL;
2574 else if (strcmp (name, ".MIPS.interfaces") == 0)
2575 {
2576 hdr->sh_type = SHT_MIPS_IFACE;
2577 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2578 }
2579 else if (strcmp (name, ".MIPS.content") == 0)
2580 {
2581 hdr->sh_type = SHT_MIPS_CONTENT;
2582 /* The sh_info field is set in final_write_processing. */
2583 }
2584 else if (strcmp (name, ".options") == 0
2585 || strcmp (name, ".MIPS.options") == 0)
2586 {
2587 hdr->sh_type = SHT_MIPS_OPTIONS;
2588 hdr->sh_entsize = 1;
2589 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2590 }
2591 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2592 hdr->sh_type = SHT_MIPS_DWARF;
2593 else if (strcmp (name, ".MIPS.symlib") == 0)
2594 {
2595 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2596 /* The sh_link and sh_info fields are set in
2597 final_write_processing. */
2598 }
2599 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2600 || strncmp (name, ".MIPS.post_rel",
2601 sizeof ".MIPS.post_rel" - 1) == 0)
2602 {
2603 hdr->sh_type = SHT_MIPS_EVENTS;
2604 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2605 /* The sh_link field is set in final_write_processing. */
2606 }
2607 /* start-sanitize-sky */
2608 else if (strcmp (name, SHNAME_DVP_OVERLAY_TABLE) == 0)
2609 {
2610 hdr->sh_type = SHT_DVP_OVERLAY_TABLE;
2611 hdr->sh_entsize = sizeof (Elf32_Dvp_External_Overlay);
2612 /* The sh_link field is set in final_write_processing. */
2613 }
2614 else if (strcmp (name, SHNAME_DVP_OVERLAY_STRTAB) == 0)
2615 hdr->sh_type = SHT_STRTAB;
2616 else if (strncmp (name, SHNAME_DVP_OVERLAY_PREFIX,
2617 sizeof (SHNAME_DVP_OVERLAY_PREFIX) - 1) == 0)
2618 hdr->sh_type = SHT_DVP_OVERLAY;
2619 /* end-sanitize-sky */
2620
2621 return true;
2622 }
2623
2624 /* Given a BFD section, try to locate the corresponding ELF section
2625 index. This is used by both the 32-bit and the 64-bit ABI.
2626 Actually, it's not clear to me that the 64-bit ABI supports these,
2627 but for non-PIC objects we will certainly want support for at least
2628 the .scommon section. */
2629
2630 boolean
2631 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2632 bfd *abfd;
2633 Elf32_Internal_Shdr *hdr;
2634 asection *sec;
2635 int *retval;
2636 {
2637 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2638 {
2639 *retval = SHN_MIPS_SCOMMON;
2640 return true;
2641 }
2642 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2643 {
2644 *retval = SHN_MIPS_ACOMMON;
2645 return true;
2646 }
2647 return false;
2648 }
2649
2650 /* When are writing out the .options or .MIPS.options section,
2651 remember the bytes we are writing out, so that we can install the
2652 GP value in the section_processing routine. */
2653
2654 boolean
2655 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2656 bfd *abfd;
2657 sec_ptr section;
2658 PTR location;
2659 file_ptr offset;
2660 bfd_size_type count;
2661 {
2662 if (strcmp (section->name, ".options") == 0
2663 || strcmp (section->name, ".MIPS.options") == 0)
2664 {
2665 bfd_byte *c;
2666
2667 if (elf_section_data (section) == NULL)
2668 {
2669 section->used_by_bfd =
2670 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2671 if (elf_section_data (section) == NULL)
2672 return false;
2673 }
2674 c = (bfd_byte *) elf_section_data (section)->tdata;
2675 if (c == NULL)
2676 {
2677 bfd_size_type size;
2678
2679 if (section->_cooked_size != 0)
2680 size = section->_cooked_size;
2681 else
2682 size = section->_raw_size;
2683 c = (bfd_byte *) bfd_zalloc (abfd, size);
2684 if (c == NULL)
2685 return false;
2686 elf_section_data (section)->tdata = (PTR) c;
2687 }
2688
2689 memcpy (c + offset, location, count);
2690 }
2691
2692 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2693 count);
2694 }
2695
2696 /* Work over a section just before writing it out. This routine is
2697 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2698 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2699 a better way. */
2700
2701 boolean
2702 _bfd_mips_elf_section_processing (abfd, hdr)
2703 bfd *abfd;
2704 Elf_Internal_Shdr *hdr;
2705 {
2706 if (hdr->bfd_section != NULL)
2707 {
2708 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
2709
2710 if (strcmp (name, ".sdata") == 0)
2711 {
2712 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2713 hdr->sh_type = SHT_PROGBITS;
2714 }
2715 else if (strcmp (name, ".sbss") == 0)
2716 {
2717 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2718 hdr->sh_type = SHT_NOBITS;
2719 }
2720 else if (strcmp (name, ".lit8") == 0
2721 || strcmp (name, ".lit4") == 0)
2722 {
2723 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2724 hdr->sh_type = SHT_PROGBITS;
2725 }
2726 else if (strcmp (name, ".compact_rel") == 0)
2727 {
2728 hdr->sh_flags = 0;
2729 hdr->sh_type = SHT_PROGBITS;
2730 }
2731 else if (strcmp (name, ".rtproc") == 0)
2732 {
2733 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
2734 {
2735 unsigned int adjust;
2736
2737 adjust = hdr->sh_size % hdr->sh_addralign;
2738 if (adjust != 0)
2739 hdr->sh_size += hdr->sh_addralign - adjust;
2740 }
2741 }
2742 }
2743
2744 return true;
2745 }
2746
2747 /* Work over a section just before writing it out. We update the GP
2748 value in the SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS sections based
2749 on the value we are using. */
2750
2751 static boolean
2752 mips_elf32_section_processing (abfd, hdr)
2753 bfd *abfd;
2754 Elf32_Internal_Shdr *hdr;
2755 {
2756 if (hdr->sh_type == SHT_MIPS_REGINFO)
2757 {
2758 bfd_byte buf[4];
2759
2760 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2761 BFD_ASSERT (hdr->contents == NULL);
2762
2763 if (bfd_seek (abfd,
2764 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2765 SEEK_SET) == -1)
2766 return false;
2767 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2768 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2769 return false;
2770 }
2771
2772 if (hdr->sh_type == SHT_MIPS_OPTIONS
2773 && hdr->bfd_section != NULL
2774 && elf_section_data (hdr->bfd_section) != NULL
2775 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2776 {
2777 bfd_byte *contents, *l, *lend;
2778
2779 /* We stored the section contents in the elf_section_data tdata
2780 field in the set_section_contents routine. We save the
2781 section contents so that we don't have to read them again.
2782 At this point we know that elf_gp is set, so we can look
2783 through the section contents to see if there is an
2784 ODK_REGINFO structure. */
2785
2786 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2787 l = contents;
2788 lend = contents + hdr->sh_size;
2789 while (l + sizeof (Elf_External_Options) <= lend)
2790 {
2791 Elf_Internal_Options intopt;
2792
2793 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2794 &intopt);
2795 if (intopt.kind == ODK_REGINFO)
2796 {
2797 bfd_byte buf[4];
2798
2799 if (bfd_seek (abfd,
2800 (hdr->sh_offset
2801 + (l - contents)
2802 + sizeof (Elf_External_Options)
2803 + (sizeof (Elf32_External_RegInfo) - 4)),
2804 SEEK_SET) == -1)
2805 return false;
2806 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
2807 if (bfd_write (buf, 1, 4, abfd) != 4)
2808 return false;
2809 }
2810 l += intopt.size;
2811 }
2812 }
2813
2814 return _bfd_mips_elf_section_processing (abfd, hdr);
2815 }
2816 \f
2817 /* MIPS ELF uses two common sections. One is the usual one, and the
2818 other is for small objects. All the small objects are kept
2819 together, and then referenced via the gp pointer, which yields
2820 faster assembler code. This is what we use for the small common
2821 section. This approach is copied from ecoff.c. */
2822 static asection mips_elf_scom_section;
2823 static asymbol mips_elf_scom_symbol;
2824 static asymbol *mips_elf_scom_symbol_ptr;
2825
2826 /* MIPS ELF also uses an acommon section, which represents an
2827 allocated common symbol which may be overridden by a
2828 definition in a shared library. */
2829 static asection mips_elf_acom_section;
2830 static asymbol mips_elf_acom_symbol;
2831 static asymbol *mips_elf_acom_symbol_ptr;
2832
2833 /* The Irix 5 support uses two virtual sections, which represent
2834 text/data symbols defined in dynamic objects. */
2835 static asection mips_elf_text_section;
2836 static asection *mips_elf_text_section_ptr;
2837 static asymbol mips_elf_text_symbol;
2838 static asymbol *mips_elf_text_symbol_ptr;
2839
2840 static asection mips_elf_data_section;
2841 static asection *mips_elf_data_section_ptr;
2842 static asymbol mips_elf_data_symbol;
2843 static asymbol *mips_elf_data_symbol_ptr;
2844
2845 /* Handle the special MIPS section numbers that a symbol may use.
2846 This is used for both the 32-bit and the 64-bit ABI. */
2847
2848 void
2849 _bfd_mips_elf_symbol_processing (abfd, asym)
2850 bfd *abfd;
2851 asymbol *asym;
2852 {
2853 elf_symbol_type *elfsym;
2854
2855 elfsym = (elf_symbol_type *) asym;
2856 switch (elfsym->internal_elf_sym.st_shndx)
2857 {
2858 case SHN_MIPS_ACOMMON:
2859 /* This section is used in a dynamically linked executable file.
2860 It is an allocated common section. The dynamic linker can
2861 either resolve these symbols to something in a shared
2862 library, or it can just leave them here. For our purposes,
2863 we can consider these symbols to be in a new section. */
2864 if (mips_elf_acom_section.name == NULL)
2865 {
2866 /* Initialize the acommon section. */
2867 mips_elf_acom_section.name = ".acommon";
2868 mips_elf_acom_section.flags = SEC_ALLOC;
2869 mips_elf_acom_section.output_section = &mips_elf_acom_section;
2870 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
2871 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
2872 mips_elf_acom_symbol.name = ".acommon";
2873 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
2874 mips_elf_acom_symbol.section = &mips_elf_acom_section;
2875 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
2876 }
2877 asym->section = &mips_elf_acom_section;
2878 break;
2879
2880 case SHN_COMMON:
2881 /* Common symbols less than the GP size are automatically
2882 treated as SHN_MIPS_SCOMMON symbols. */
2883 if (asym->value > elf_gp_size (abfd))
2884 break;
2885 /* Fall through. */
2886 case SHN_MIPS_SCOMMON:
2887 if (mips_elf_scom_section.name == NULL)
2888 {
2889 /* Initialize the small common section. */
2890 mips_elf_scom_section.name = ".scommon";
2891 mips_elf_scom_section.flags = SEC_IS_COMMON;
2892 mips_elf_scom_section.output_section = &mips_elf_scom_section;
2893 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
2894 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
2895 mips_elf_scom_symbol.name = ".scommon";
2896 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
2897 mips_elf_scom_symbol.section = &mips_elf_scom_section;
2898 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
2899 }
2900 asym->section = &mips_elf_scom_section;
2901 asym->value = elfsym->internal_elf_sym.st_size;
2902 break;
2903
2904 case SHN_MIPS_SUNDEFINED:
2905 asym->section = bfd_und_section_ptr;
2906 break;
2907
2908 #if 0 /* for SGI_COMPAT */
2909 case SHN_MIPS_TEXT:
2910 asym->section = mips_elf_text_section_ptr;
2911 break;
2912
2913 case SHN_MIPS_DATA:
2914 asym->section = mips_elf_data_section_ptr;
2915 break;
2916 #endif
2917 }
2918 }
2919 \f
2920 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
2921 segments. */
2922
2923 static int
2924 mips_elf_additional_program_headers (abfd)
2925 bfd *abfd;
2926 {
2927 asection *s;
2928 int ret;
2929
2930 ret = 0;
2931
2932 if (! SGI_COMPAT (abfd))
2933 return ret;
2934
2935 s = bfd_get_section_by_name (abfd, ".reginfo");
2936 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2937 {
2938 /* We need a PT_MIPS_REGINFO segment. */
2939 ++ret;
2940 }
2941
2942 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
2943 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2944 {
2945 /* We need a PT_MIPS_RTPROC segment. */
2946 ++ret;
2947 }
2948
2949 return ret;
2950 }
2951
2952 /* Modify the segment map for an Irix 5 executable. */
2953
2954 static boolean
2955 mips_elf_modify_segment_map (abfd)
2956 bfd *abfd;
2957 {
2958 asection *s;
2959 struct elf_segment_map *m, **pm;
2960
2961 if (! SGI_COMPAT (abfd))
2962 return true;
2963
2964 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2965 segment. */
2966 s = bfd_get_section_by_name (abfd, ".reginfo");
2967 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2968 {
2969 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2970 if (m->p_type == PT_MIPS_REGINFO)
2971 break;
2972 if (m == NULL)
2973 {
2974 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2975 if (m == NULL)
2976 return false;
2977
2978 m->p_type = PT_MIPS_REGINFO;
2979 m->count = 1;
2980 m->sections[0] = s;
2981
2982 /* We want to put it after the PHDR and INTERP segments. */
2983 pm = &elf_tdata (abfd)->segment_map;
2984 while (*pm != NULL
2985 && ((*pm)->p_type == PT_PHDR
2986 || (*pm)->p_type == PT_INTERP))
2987 pm = &(*pm)->next;
2988
2989 m->next = *pm;
2990 *pm = m;
2991 }
2992 }
2993
2994 /* If there are .dynamic and .mdebug sections, we make a room for
2995 the RTPROC header. FIXME: Rewrite without section names. */
2996 if (bfd_get_section_by_name (abfd, ".interp") == NULL
2997 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
2998 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2999 {
3000 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3001 if (m->p_type == PT_MIPS_RTPROC)
3002 break;
3003 if (m == NULL)
3004 {
3005 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3006 if (m == NULL)
3007 return false;
3008
3009 m->p_type = PT_MIPS_RTPROC;
3010
3011 s = bfd_get_section_by_name (abfd, ".rtproc");
3012 if (s == NULL)
3013 {
3014 m->count = 0;
3015 m->p_flags = 0;
3016 m->p_flags_valid = 1;
3017 }
3018 else
3019 {
3020 m->count = 1;
3021 m->sections[0] = s;
3022 }
3023
3024 /* We want to put it after the DYNAMIC segment. */
3025 pm = &elf_tdata (abfd)->segment_map;
3026 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
3027 pm = &(*pm)->next;
3028 if (*pm != NULL)
3029 pm = &(*pm)->next;
3030
3031 m->next = *pm;
3032 *pm = m;
3033 }
3034 }
3035
3036 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
3037 .dynsym, and .hash sections, and everything in between. */
3038 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
3039 if ((*pm)->p_type == PT_DYNAMIC)
3040 break;
3041 m = *pm;
3042 if (m != NULL
3043 && m->count == 1
3044 && strcmp (m->sections[0]->name, ".dynamic") == 0)
3045 {
3046 static const char *sec_names[] =
3047 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
3048 bfd_vma low, high;
3049 unsigned int i, c;
3050 struct elf_segment_map *n;
3051
3052 low = 0xffffffff;
3053 high = 0;
3054 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
3055 {
3056 s = bfd_get_section_by_name (abfd, sec_names[i]);
3057 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3058 {
3059 bfd_size_type sz;
3060
3061 if (low > s->vma)
3062 low = s->vma;
3063 sz = s->_cooked_size;
3064 if (sz == 0)
3065 sz = s->_raw_size;
3066 if (high < s->vma + sz)
3067 high = s->vma + sz;
3068 }
3069 }
3070
3071 c = 0;
3072 for (s = abfd->sections; s != NULL; s = s->next)
3073 if ((s->flags & SEC_LOAD) != 0
3074 && s->vma >= low
3075 && ((s->vma
3076 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3077 <= high))
3078 ++c;
3079
3080 n = ((struct elf_segment_map *)
3081 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
3082 if (n == NULL)
3083 return false;
3084 *n = *m;
3085 n->count = c;
3086
3087 i = 0;
3088 for (s = abfd->sections; s != NULL; s = s->next)
3089 {
3090 if ((s->flags & SEC_LOAD) != 0
3091 && s->vma >= low
3092 && ((s->vma
3093 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3094 <= high))
3095 {
3096 n->sections[i] = s;
3097 ++i;
3098 }
3099 }
3100
3101 *pm = n;
3102 }
3103
3104 return true;
3105 }
3106 \f
3107 /* The structure of the runtime procedure descriptor created by the
3108 loader for use by the static exception system. */
3109
3110 typedef struct runtime_pdr {
3111 bfd_vma adr; /* memory address of start of procedure */
3112 long regmask; /* save register mask */
3113 long regoffset; /* save register offset */
3114 long fregmask; /* save floating point register mask */
3115 long fregoffset; /* save floating point register offset */
3116 long frameoffset; /* frame size */
3117 short framereg; /* frame pointer register */
3118 short pcreg; /* offset or reg of return pc */
3119 long irpss; /* index into the runtime string table */
3120 long reserved;
3121 struct exception_info *exception_info;/* pointer to exception array */
3122 } RPDR, *pRPDR;
3123 #define cbRPDR sizeof(RPDR)
3124 #define rpdNil ((pRPDR) 0)
3125
3126 /* Swap RPDR (runtime procedure table entry) for output. */
3127
3128 static void ecoff_swap_rpdr_out
3129 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3130
3131 static void
3132 ecoff_swap_rpdr_out (abfd, in, ex)
3133 bfd *abfd;
3134 const RPDR *in;
3135 struct rpdr_ext *ex;
3136 {
3137 /* ecoff_put_off was defined in ecoffswap.h. */
3138 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
3139 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
3140 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
3141 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
3142 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
3143 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
3144
3145 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
3146 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
3147
3148 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
3149 #if 0 /* FIXME */
3150 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
3151 #endif
3152 }
3153 \f
3154 /* Read ECOFF debugging information from a .mdebug section into a
3155 ecoff_debug_info structure. */
3156
3157 boolean
3158 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3159 bfd *abfd;
3160 asection *section;
3161 struct ecoff_debug_info *debug;
3162 {
3163 HDRR *symhdr;
3164 const struct ecoff_debug_swap *swap;
3165 char *ext_hdr = NULL;
3166
3167 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3168
3169 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
3170 if (ext_hdr == NULL && swap->external_hdr_size != 0)
3171 goto error_return;
3172
3173 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3174 swap->external_hdr_size)
3175 == false)
3176 goto error_return;
3177
3178 symhdr = &debug->symbolic_header;
3179 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3180
3181 /* The symbolic header contains absolute file offsets and sizes to
3182 read. */
3183 #define READ(ptr, offset, count, size, type) \
3184 if (symhdr->count == 0) \
3185 debug->ptr = NULL; \
3186 else \
3187 { \
3188 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
3189 if (debug->ptr == NULL) \
3190 goto error_return; \
3191 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3192 || (bfd_read (debug->ptr, size, symhdr->count, \
3193 abfd) != size * symhdr->count)) \
3194 goto error_return; \
3195 }
3196
3197 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3198 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3199 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3200 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3201 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3202 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3203 union aux_ext *);
3204 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3205 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3206 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3207 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3208 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3209 #undef READ
3210
3211 debug->fdr = NULL;
3212 debug->adjust = NULL;
3213
3214 return true;
3215
3216 error_return:
3217 if (ext_hdr != NULL)
3218 free (ext_hdr);
3219 if (debug->line != NULL)
3220 free (debug->line);
3221 if (debug->external_dnr != NULL)
3222 free (debug->external_dnr);
3223 if (debug->external_pdr != NULL)
3224 free (debug->external_pdr);
3225 if (debug->external_sym != NULL)
3226 free (debug->external_sym);
3227 if (debug->external_opt != NULL)
3228 free (debug->external_opt);
3229 if (debug->external_aux != NULL)
3230 free (debug->external_aux);
3231 if (debug->ss != NULL)
3232 free (debug->ss);
3233 if (debug->ssext != NULL)
3234 free (debug->ssext);
3235 if (debug->external_fdr != NULL)
3236 free (debug->external_fdr);
3237 if (debug->external_rfd != NULL)
3238 free (debug->external_rfd);
3239 if (debug->external_ext != NULL)
3240 free (debug->external_ext);
3241 return false;
3242 }
3243 \f
3244 /* MIPS ELF local labels start with '$', not 'L'. */
3245
3246 /*ARGSUSED*/
3247 static boolean
3248 mips_elf_is_local_label_name (abfd, name)
3249 bfd *abfd;
3250 const char *name;
3251 {
3252 if (name[0] == '$')
3253 return true;
3254
3255 /* On Irix 6, the labels go back to starting with '.', so we accept
3256 the generic ELF local label syntax as well. */
3257 return _bfd_elf_is_local_label_name (abfd, name);
3258 }
3259
3260 /* MIPS ELF uses a special find_nearest_line routine in order the
3261 handle the ECOFF debugging information. */
3262
3263 struct mips_elf_find_line
3264 {
3265 struct ecoff_debug_info d;
3266 struct ecoff_find_line i;
3267 };
3268
3269 boolean
3270 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3271 functionname_ptr, line_ptr)
3272 bfd *abfd;
3273 asection *section;
3274 asymbol **symbols;
3275 bfd_vma offset;
3276 const char **filename_ptr;
3277 const char **functionname_ptr;
3278 unsigned int *line_ptr;
3279 {
3280 asection *msec;
3281
3282 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3283 filename_ptr, functionname_ptr,
3284 line_ptr))
3285 return true;
3286
3287 msec = bfd_get_section_by_name (abfd, ".mdebug");
3288 if (msec != NULL)
3289 {
3290 flagword origflags;
3291 struct mips_elf_find_line *fi;
3292 const struct ecoff_debug_swap * const swap =
3293 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3294
3295 /* If we are called during a link, mips_elf_final_link may have
3296 cleared the SEC_HAS_CONTENTS field. We force it back on here
3297 if appropriate (which it normally will be). */
3298 origflags = msec->flags;
3299 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3300 msec->flags |= SEC_HAS_CONTENTS;
3301
3302 fi = elf_tdata (abfd)->find_line_info;
3303 if (fi == NULL)
3304 {
3305 bfd_size_type external_fdr_size;
3306 char *fraw_src;
3307 char *fraw_end;
3308 struct fdr *fdr_ptr;
3309
3310 fi = ((struct mips_elf_find_line *)
3311 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3312 if (fi == NULL)
3313 {
3314 msec->flags = origflags;
3315 return false;
3316 }
3317
3318 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3319 {
3320 msec->flags = origflags;
3321 return false;
3322 }
3323
3324 /* Swap in the FDR information. */
3325 fi->d.fdr = ((struct fdr *)
3326 bfd_alloc (abfd,
3327 (fi->d.symbolic_header.ifdMax *
3328 sizeof (struct fdr))));
3329 if (fi->d.fdr == NULL)
3330 {
3331 msec->flags = origflags;
3332 return false;
3333 }
3334 external_fdr_size = swap->external_fdr_size;
3335 fdr_ptr = fi->d.fdr;
3336 fraw_src = (char *) fi->d.external_fdr;
3337 fraw_end = (fraw_src
3338 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3339 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3340 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3341
3342 elf_tdata (abfd)->find_line_info = fi;
3343
3344 /* Note that we don't bother to ever free this information.
3345 find_nearest_line is either called all the time, as in
3346 objdump -l, so the information should be saved, or it is
3347 rarely called, as in ld error messages, so the memory
3348 wasted is unimportant. Still, it would probably be a
3349 good idea for free_cached_info to throw it away. */
3350 }
3351
3352 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3353 &fi->i, filename_ptr, functionname_ptr,
3354 line_ptr))
3355 {
3356 msec->flags = origflags;
3357 return true;
3358 }
3359
3360 msec->flags = origflags;
3361 }
3362
3363 /* Fall back on the generic ELF find_nearest_line routine. */
3364
3365 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3366 filename_ptr, functionname_ptr,
3367 line_ptr);
3368 }
3369 \f
3370 /* The mips16 compiler uses a couple of special sections to handle
3371 floating point arguments.
3372
3373 Section names that look like .mips16.fn.FNNAME contain stubs that
3374 copy floating point arguments from the fp regs to the gp regs and
3375 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3376 call should be redirected to the stub instead. If no 32 bit
3377 function calls FNNAME, the stub should be discarded. We need to
3378 consider any reference to the function, not just a call, because
3379 if the address of the function is taken we will need the stub,
3380 since the address might be passed to a 32 bit function.
3381
3382 Section names that look like .mips16.call.FNNAME contain stubs
3383 that copy floating point arguments from the gp regs to the fp
3384 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3385 then any 16 bit function that calls FNNAME should be redirected
3386 to the stub instead. If FNNAME is not a 32 bit function, the
3387 stub should be discarded.
3388
3389 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3390 which call FNNAME and then copy the return value from the fp regs
3391 to the gp regs. These stubs store the return value in $18 while
3392 calling FNNAME; any function which might call one of these stubs
3393 must arrange to save $18 around the call. (This case is not
3394 needed for 32 bit functions that call 16 bit functions, because
3395 16 bit functions always return floating point values in both
3396 $f0/$f1 and $2/$3.)
3397
3398 Note that in all cases FNNAME might be defined statically.
3399 Therefore, FNNAME is not used literally. Instead, the relocation
3400 information will indicate which symbol the section is for.
3401
3402 We record any stubs that we find in the symbol table. */
3403
3404 #define FN_STUB ".mips16.fn."
3405 #define CALL_STUB ".mips16.call."
3406 #define CALL_FP_STUB ".mips16.call.fp."
3407
3408 /* The MIPS ELF linker needs additional information for each symbol in
3409 the global hash table. */
3410
3411 struct mips_elf_link_hash_entry
3412 {
3413 struct elf_link_hash_entry root;
3414
3415 /* External symbol information. */
3416 EXTR esym;
3417
3418 /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol. */
3419 unsigned int mips_32_relocs;
3420
3421 /* If there is a stub that 32 bit functions should use to call this
3422 16 bit function, this points to the section containing the stub. */
3423 asection *fn_stub;
3424
3425 /* Whether we need the fn_stub; this is set if this symbol appears
3426 in any relocs other than a 16 bit call. */
3427 boolean need_fn_stub;
3428
3429 /* If there is a stub that 16 bit functions should use to call this
3430 32 bit function, this points to the section containing the stub. */
3431 asection *call_stub;
3432
3433 /* This is like the call_stub field, but it is used if the function
3434 being called returns a floating point value. */
3435 asection *call_fp_stub;
3436 };
3437
3438 /* MIPS ELF linker hash table. */
3439
3440 struct mips_elf_link_hash_table
3441 {
3442 struct elf_link_hash_table root;
3443 /* String section indices for the dynamic section symbols. */
3444 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3445 /* The number of .rtproc entries. */
3446 bfd_size_type procedure_count;
3447 /* The size of the .compact_rel section (if SGI_COMPAT). */
3448 bfd_size_type compact_rel_size;
3449 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3450 entry is set to the address of __rld_obj_head as in Irix 5. */
3451 boolean use_rld_obj_head;
3452 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3453 bfd_vma rld_value;
3454 /* This is set if we see any mips16 stub sections. */
3455 boolean mips16_stubs_seen;
3456 };
3457
3458 /* Look up an entry in a MIPS ELF linker hash table. */
3459
3460 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3461 ((struct mips_elf_link_hash_entry *) \
3462 elf_link_hash_lookup (&(table)->root, (string), (create), \
3463 (copy), (follow)))
3464
3465 /* Traverse a MIPS ELF linker hash table. */
3466
3467 #define mips_elf_link_hash_traverse(table, func, info) \
3468 (elf_link_hash_traverse \
3469 (&(table)->root, \
3470 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3471 (info)))
3472
3473 /* Get the MIPS ELF linker hash table from a link_info structure. */
3474
3475 #define mips_elf_hash_table(p) \
3476 ((struct mips_elf_link_hash_table *) ((p)->hash))
3477
3478 static boolean mips_elf_output_extsym
3479 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3480
3481 /* Create an entry in a MIPS ELF linker hash table. */
3482
3483 static struct bfd_hash_entry *
3484 mips_elf_link_hash_newfunc (entry, table, string)
3485 struct bfd_hash_entry *entry;
3486 struct bfd_hash_table *table;
3487 const char *string;
3488 {
3489 struct mips_elf_link_hash_entry *ret =
3490 (struct mips_elf_link_hash_entry *) entry;
3491
3492 /* Allocate the structure if it has not already been allocated by a
3493 subclass. */
3494 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3495 ret = ((struct mips_elf_link_hash_entry *)
3496 bfd_hash_allocate (table,
3497 sizeof (struct mips_elf_link_hash_entry)));
3498 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3499 return (struct bfd_hash_entry *) ret;
3500
3501 /* Call the allocation method of the superclass. */
3502 ret = ((struct mips_elf_link_hash_entry *)
3503 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3504 table, string));
3505 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3506 {
3507 /* Set local fields. */
3508 memset (&ret->esym, 0, sizeof (EXTR));
3509 /* We use -2 as a marker to indicate that the information has
3510 not been set. -1 means there is no associated ifd. */
3511 ret->esym.ifd = -2;
3512 ret->mips_32_relocs = 0;
3513 ret->fn_stub = NULL;
3514 ret->need_fn_stub = false;
3515 ret->call_stub = NULL;
3516 ret->call_fp_stub = NULL;
3517 }
3518
3519 return (struct bfd_hash_entry *) ret;
3520 }
3521
3522 /* Create a MIPS ELF linker hash table. */
3523
3524 static struct bfd_link_hash_table *
3525 mips_elf_link_hash_table_create (abfd)
3526 bfd *abfd;
3527 {
3528 struct mips_elf_link_hash_table *ret;
3529 unsigned int i;
3530
3531 ret = ((struct mips_elf_link_hash_table *)
3532 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3533 if (ret == (struct mips_elf_link_hash_table *) NULL)
3534 return NULL;
3535
3536 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3537 mips_elf_link_hash_newfunc))
3538 {
3539 bfd_release (abfd, ret);
3540 return NULL;
3541 }
3542
3543 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3544 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3545 ret->procedure_count = 0;
3546 ret->compact_rel_size = 0;
3547 ret->use_rld_obj_head = false;
3548 ret->rld_value = 0;
3549 ret->mips16_stubs_seen = false;
3550
3551 return &ret->root.root;
3552 }
3553
3554 /* Hook called by the linker routine which adds symbols from an object
3555 file. We must handle the special MIPS section numbers here. */
3556
3557 /*ARGSUSED*/
3558 static boolean
3559 mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3560 bfd *abfd;
3561 struct bfd_link_info *info;
3562 const Elf_Internal_Sym *sym;
3563 const char **namep;
3564 flagword *flagsp;
3565 asection **secp;
3566 bfd_vma *valp;
3567 {
3568 if (SGI_COMPAT (abfd)
3569 && (abfd->flags & DYNAMIC) != 0
3570 && strcmp (*namep, "_rld_new_interface") == 0)
3571 {
3572 /* Skip Irix 5 rld entry name. */
3573 *namep = NULL;
3574 return true;
3575 }
3576
3577 switch (sym->st_shndx)
3578 {
3579 case SHN_COMMON:
3580 /* Common symbols less than the GP size are automatically
3581 treated as SHN_MIPS_SCOMMON symbols. */
3582 if (sym->st_size > elf_gp_size (abfd))
3583 break;
3584 /* Fall through. */
3585 case SHN_MIPS_SCOMMON:
3586 *secp = bfd_make_section_old_way (abfd, ".scommon");
3587 (*secp)->flags |= SEC_IS_COMMON;
3588 *valp = sym->st_size;
3589 break;
3590
3591 case SHN_MIPS_TEXT:
3592 /* This section is used in a shared object. */
3593 if (mips_elf_text_section_ptr == NULL)
3594 {
3595 /* Initialize the section. */
3596 mips_elf_text_section.name = ".text";
3597 mips_elf_text_section.flags = SEC_NO_FLAGS;
3598 mips_elf_text_section.output_section = NULL;
3599 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3600 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3601 mips_elf_text_symbol.name = ".text";
3602 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
3603 mips_elf_text_symbol.section = &mips_elf_text_section;
3604 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3605 mips_elf_text_section_ptr = &mips_elf_text_section;
3606 }
3607 /* This code used to do *secp = bfd_und_section_ptr if
3608 info->shared. I don't know why, and that doesn't make sense,
3609 so I took it out. */
3610 *secp = mips_elf_text_section_ptr;
3611 break;
3612
3613 case SHN_MIPS_ACOMMON:
3614 /* Fall through. XXX Can we treat this as allocated data? */
3615 case SHN_MIPS_DATA:
3616 /* This section is used in a shared object. */
3617 if (mips_elf_data_section_ptr == NULL)
3618 {
3619 /* Initialize the section. */
3620 mips_elf_data_section.name = ".data";
3621 mips_elf_data_section.flags = SEC_NO_FLAGS;
3622 mips_elf_data_section.output_section = NULL;
3623 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3624 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3625 mips_elf_data_symbol.name = ".data";
3626 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
3627 mips_elf_data_symbol.section = &mips_elf_data_section;
3628 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3629 mips_elf_data_section_ptr = &mips_elf_data_section;
3630 }
3631 /* This code used to do *secp = bfd_und_section_ptr if
3632 info->shared. I don't know why, and that doesn't make sense,
3633 so I took it out. */
3634 *secp = mips_elf_data_section_ptr;
3635 break;
3636
3637 case SHN_MIPS_SUNDEFINED:
3638 *secp = bfd_und_section_ptr;
3639 break;
3640 }
3641
3642 if (SGI_COMPAT (abfd)
3643 && ! info->shared
3644 && info->hash->creator == abfd->xvec
3645 && strcmp (*namep, "__rld_obj_head") == 0)
3646 {
3647 struct elf_link_hash_entry *h;
3648
3649 /* Mark __rld_obj_head as dynamic. */
3650 h = NULL;
3651 if (! (_bfd_generic_link_add_one_symbol
3652 (info, abfd, *namep, BSF_GLOBAL, *secp,
3653 (bfd_vma) *valp, (const char *) NULL, false,
3654 get_elf_backend_data (abfd)->collect,
3655 (struct bfd_link_hash_entry **) &h)))
3656 return false;
3657 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3658 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3659 h->type = STT_OBJECT;
3660
3661 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3662 return false;
3663
3664 mips_elf_hash_table (info)->use_rld_obj_head = true;
3665 }
3666
3667 /* If this is a mips16 text symbol, add 1 to the value to make it
3668 odd. This will cause something like .word SYM to come up with
3669 the right value when it is loaded into the PC. */
3670 if (sym->st_other == STO_MIPS16)
3671 ++*valp;
3672
3673 return true;
3674 }
3675
3676 /* Structure used to pass information to mips_elf_output_extsym. */
3677
3678 struct extsym_info
3679 {
3680 bfd *abfd;
3681 struct bfd_link_info *info;
3682 struct ecoff_debug_info *debug;
3683 const struct ecoff_debug_swap *swap;
3684 boolean failed;
3685 };
3686
3687 /* This routine is used to write out ECOFF debugging external symbol
3688 information. It is called via mips_elf_link_hash_traverse. The
3689 ECOFF external symbol information must match the ELF external
3690 symbol information. Unfortunately, at this point we don't know
3691 whether a symbol is required by reloc information, so the two
3692 tables may wind up being different. We must sort out the external
3693 symbol information before we can set the final size of the .mdebug
3694 section, and we must set the size of the .mdebug section before we
3695 can relocate any sections, and we can't know which symbols are
3696 required by relocation until we relocate the sections.
3697 Fortunately, it is relatively unlikely that any symbol will be
3698 stripped but required by a reloc. In particular, it can not happen
3699 when generating a final executable. */
3700
3701 static boolean
3702 mips_elf_output_extsym (h, data)
3703 struct mips_elf_link_hash_entry *h;
3704 PTR data;
3705 {
3706 struct extsym_info *einfo = (struct extsym_info *) data;
3707 boolean strip;
3708 asection *sec, *output_section;
3709
3710 if (h->root.indx == -2)
3711 strip = false;
3712 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3713 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3714 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3715 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3716 strip = true;
3717 else if (einfo->info->strip == strip_all
3718 || (einfo->info->strip == strip_some
3719 && bfd_hash_lookup (einfo->info->keep_hash,
3720 h->root.root.root.string,
3721 false, false) == NULL))
3722 strip = true;
3723 else
3724 strip = false;
3725
3726 if (strip)
3727 return true;
3728
3729 if (h->esym.ifd == -2)
3730 {
3731 h->esym.jmptbl = 0;
3732 h->esym.cobol_main = 0;
3733 h->esym.weakext = 0;
3734 h->esym.reserved = 0;
3735 h->esym.ifd = ifdNil;
3736 h->esym.asym.value = 0;
3737 h->esym.asym.st = stGlobal;
3738
3739 if (SGI_COMPAT (einfo->abfd)
3740 && (h->root.root.type == bfd_link_hash_undefined
3741 || h->root.root.type == bfd_link_hash_undefweak))
3742 {
3743 const char *name;
3744
3745 /* Use undefined class. Also, set class and type for some
3746 special symbols. */
3747 name = h->root.root.root.string;
3748 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3749 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3750 {
3751 h->esym.asym.sc = scData;
3752 h->esym.asym.st = stLabel;
3753 h->esym.asym.value = 0;
3754 }
3755 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3756 {
3757 h->esym.asym.sc = scAbs;
3758 h->esym.asym.st = stLabel;
3759 h->esym.asym.value =
3760 mips_elf_hash_table (einfo->info)->procedure_count;
3761 }
3762 else if (strcmp (name, "_gp_disp") == 0)
3763 {
3764 h->esym.asym.sc = scAbs;
3765 h->esym.asym.st = stLabel;
3766 h->esym.asym.value = elf_gp (einfo->abfd);
3767 }
3768 else
3769 h->esym.asym.sc = scUndefined;
3770 }
3771 else if (h->root.root.type != bfd_link_hash_defined
3772 && h->root.root.type != bfd_link_hash_defweak)
3773 h->esym.asym.sc = scAbs;
3774 else
3775 {
3776 const char *name;
3777
3778 sec = h->root.root.u.def.section;
3779 output_section = sec->output_section;
3780
3781 /* When making a shared library and symbol h is the one from
3782 the another shared library, OUTPUT_SECTION may be null. */
3783 if (output_section == NULL)
3784 h->esym.asym.sc = scUndefined;
3785 else
3786 {
3787 name = bfd_section_name (output_section->owner, output_section);
3788
3789 if (strcmp (name, ".text") == 0)
3790 h->esym.asym.sc = scText;
3791 else if (strcmp (name, ".data") == 0)
3792 h->esym.asym.sc = scData;
3793 else if (strcmp (name, ".sdata") == 0)
3794 h->esym.asym.sc = scSData;
3795 else if (strcmp (name, ".rodata") == 0
3796 || strcmp (name, ".rdata") == 0)
3797 h->esym.asym.sc = scRData;
3798 else if (strcmp (name, ".bss") == 0)
3799 h->esym.asym.sc = scBss;
3800 else if (strcmp (name, ".sbss") == 0)
3801 h->esym.asym.sc = scSBss;
3802 else if (strcmp (name, ".init") == 0)
3803 h->esym.asym.sc = scInit;
3804 else if (strcmp (name, ".fini") == 0)
3805 h->esym.asym.sc = scFini;
3806 else
3807 h->esym.asym.sc = scAbs;
3808 }
3809 }
3810
3811 h->esym.asym.reserved = 0;
3812 h->esym.asym.index = indexNil;
3813 }
3814
3815 if (h->root.root.type == bfd_link_hash_common)
3816 h->esym.asym.value = h->root.root.u.c.size;
3817 else if (h->root.root.type == bfd_link_hash_defined
3818 || h->root.root.type == bfd_link_hash_defweak)
3819 {
3820 if (h->esym.asym.sc == scCommon)
3821 h->esym.asym.sc = scBss;
3822 else if (h->esym.asym.sc == scSCommon)
3823 h->esym.asym.sc = scSBss;
3824
3825 sec = h->root.root.u.def.section;
3826 output_section = sec->output_section;
3827 if (output_section != NULL)
3828 h->esym.asym.value = (h->root.root.u.def.value
3829 + sec->output_offset
3830 + output_section->vma);
3831 else
3832 h->esym.asym.value = 0;
3833 }
3834 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3835 {
3836 /* Set type and value for a symbol with a function stub. */
3837 h->esym.asym.st = stProc;
3838 sec = h->root.root.u.def.section;
3839 if (sec == NULL)
3840 h->esym.asym.value = 0;
3841 else
3842 {
3843 output_section = sec->output_section;
3844 if (output_section != NULL)
3845 h->esym.asym.value = (h->root.plt.offset
3846 + sec->output_offset
3847 + output_section->vma);
3848 else
3849 h->esym.asym.value = 0;
3850 }
3851 #if 0 /* FIXME? */
3852 h->esym.ifd = 0;
3853 #endif
3854 }
3855
3856 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3857 h->root.root.root.string,
3858 &h->esym))
3859 {
3860 einfo->failed = true;
3861 return false;
3862 }
3863
3864 return true;
3865 }
3866
3867 /* Create a runtime procedure table from the .mdebug section. */
3868
3869 static boolean
3870 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
3871 PTR handle;
3872 bfd *abfd;
3873 struct bfd_link_info *info;
3874 asection *s;
3875 struct ecoff_debug_info *debug;
3876 {
3877 const struct ecoff_debug_swap *swap;
3878 HDRR *hdr = &debug->symbolic_header;
3879 RPDR *rpdr, *rp;
3880 struct rpdr_ext *erp;
3881 PTR rtproc;
3882 struct pdr_ext *epdr;
3883 struct sym_ext *esym;
3884 char *ss, **sv;
3885 char *str;
3886 unsigned long size, count;
3887 unsigned long sindex;
3888 unsigned long i;
3889 PDR pdr;
3890 SYMR sym;
3891 const char *no_name_func = _("static procedure (no name)");
3892
3893 epdr = NULL;
3894 rpdr = NULL;
3895 esym = NULL;
3896 ss = NULL;
3897 sv = NULL;
3898
3899 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3900
3901 sindex = strlen (no_name_func) + 1;
3902 count = hdr->ipdMax;
3903 if (count > 0)
3904 {
3905 size = swap->external_pdr_size;
3906
3907 epdr = (struct pdr_ext *) bfd_malloc (size * count);
3908 if (epdr == NULL)
3909 goto error_return;
3910
3911 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
3912 goto error_return;
3913
3914 size = sizeof (RPDR);
3915 rp = rpdr = (RPDR *) bfd_malloc (size * count);
3916 if (rpdr == NULL)
3917 goto error_return;
3918
3919 sv = (char **) bfd_malloc (sizeof (char *) * count);
3920 if (sv == NULL)
3921 goto error_return;
3922
3923 count = hdr->isymMax;
3924 size = swap->external_sym_size;
3925 esym = (struct sym_ext *) bfd_malloc (size * count);
3926 if (esym == NULL)
3927 goto error_return;
3928
3929 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
3930 goto error_return;
3931
3932 count = hdr->issMax;
3933 ss = (char *) bfd_malloc (count);
3934 if (ss == NULL)
3935 goto error_return;
3936 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
3937 goto error_return;
3938
3939 count = hdr->ipdMax;
3940 for (i = 0; i < count; i++, rp++)
3941 {
3942 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
3943 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
3944 rp->adr = sym.value;
3945 rp->regmask = pdr.regmask;
3946 rp->regoffset = pdr.regoffset;
3947 rp->fregmask = pdr.fregmask;
3948 rp->fregoffset = pdr.fregoffset;
3949 rp->frameoffset = pdr.frameoffset;
3950 rp->framereg = pdr.framereg;
3951 rp->pcreg = pdr.pcreg;
3952 rp->irpss = sindex;
3953 sv[i] = ss + sym.iss;
3954 sindex += strlen (sv[i]) + 1;
3955 }
3956 }
3957
3958 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
3959 size = BFD_ALIGN (size, 16);
3960 rtproc = (PTR) bfd_alloc (abfd, size);
3961 if (rtproc == NULL)
3962 {
3963 mips_elf_hash_table (info)->procedure_count = 0;
3964 goto error_return;
3965 }
3966
3967 mips_elf_hash_table (info)->procedure_count = count + 2;
3968
3969 erp = (struct rpdr_ext *) rtproc;
3970 memset (erp, 0, sizeof (struct rpdr_ext));
3971 erp++;
3972 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
3973 strcpy (str, no_name_func);
3974 str += strlen (no_name_func) + 1;
3975 for (i = 0; i < count; i++)
3976 {
3977 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
3978 strcpy (str, sv[i]);
3979 str += strlen (sv[i]) + 1;
3980 }
3981 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
3982
3983 /* Set the size and contents of .rtproc section. */
3984 s->_raw_size = size;
3985 s->contents = (bfd_byte *) rtproc;
3986
3987 /* Skip this section later on (I don't think this currently
3988 matters, but someday it might). */
3989 s->link_order_head = (struct bfd_link_order *) NULL;
3990
3991 if (epdr != NULL)
3992 free (epdr);
3993 if (rpdr != NULL)
3994 free (rpdr);
3995 if (esym != NULL)
3996 free (esym);
3997 if (ss != NULL)
3998 free (ss);
3999 if (sv != NULL)
4000 free (sv);
4001
4002 return true;
4003
4004 error_return:
4005 if (epdr != NULL)
4006 free (epdr);
4007 if (rpdr != NULL)
4008 free (rpdr);
4009 if (esym != NULL)
4010 free (esym);
4011 if (ss != NULL)
4012 free (ss);
4013 if (sv != NULL)
4014 free (sv);
4015 return false;
4016 }
4017
4018 /* A comparison routine used to sort .gptab entries. */
4019
4020 static int
4021 gptab_compare (p1, p2)
4022 const PTR p1;
4023 const PTR p2;
4024 {
4025 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
4026 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
4027
4028 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
4029 }
4030
4031 /* We need to use a special link routine to handle the .reginfo and
4032 the .mdebug sections. We need to merge all instances of these
4033 sections together, not write them all out sequentially. */
4034
4035 static boolean
4036 mips_elf_final_link (abfd, info)
4037 bfd *abfd;
4038 struct bfd_link_info *info;
4039 {
4040 asection **secpp;
4041 asection *o;
4042 struct bfd_link_order *p;
4043 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4044 asection *rtproc_sec;
4045 Elf32_RegInfo reginfo;
4046 struct ecoff_debug_info debug;
4047 const struct ecoff_debug_swap *swap
4048 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4049 HDRR *symhdr = &debug.symbolic_header;
4050 PTR mdebug_handle = NULL;
4051
4052 /* Drop the .options section, since it has special semantics which I
4053 haven't bothered to figure out. */
4054 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4055 {
4056 if (strcmp ((*secpp)->name, ".options") == 0)
4057 {
4058 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4059 if (p->type == bfd_indirect_link_order)
4060 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4061 (*secpp)->link_order_head = NULL;
4062 *secpp = (*secpp)->next;
4063 --abfd->section_count;
4064 break;
4065 }
4066 }
4067
4068 /* Get a value for the GP register. */
4069 if (elf_gp (abfd) == 0)
4070 {
4071 struct bfd_link_hash_entry *h;
4072
4073 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4074 if (h != (struct bfd_link_hash_entry *) NULL
4075 && h->type == bfd_link_hash_defined)
4076 elf_gp (abfd) = (h->u.def.value
4077 + h->u.def.section->output_section->vma
4078 + h->u.def.section->output_offset);
4079 else if (info->relocateable)
4080 {
4081 bfd_vma lo;
4082
4083 /* Make up a value. */
4084 lo = (bfd_vma) -1;
4085 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4086 {
4087 if (o->vma < lo
4088 && (strcmp (o->name, ".sbss") == 0
4089 || strcmp (o->name, ".sdata") == 0
4090 || strcmp (o->name, ".lit4") == 0
4091 || strcmp (o->name, ".lit8") == 0))
4092 lo = o->vma;
4093 }
4094 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4095 }
4096 else
4097 {
4098 /* If the relocate_section function needs to do a reloc
4099 involving the GP value, it should make a reloc_dangerous
4100 callback to warn that GP is not defined. */
4101 }
4102 }
4103
4104 /* Go through the sections and collect the .reginfo and .mdebug
4105 information. */
4106 reginfo_sec = NULL;
4107 mdebug_sec = NULL;
4108 gptab_data_sec = NULL;
4109 gptab_bss_sec = NULL;
4110 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4111 {
4112 if (strcmp (o->name, ".reginfo") == 0)
4113 {
4114 memset (&reginfo, 0, sizeof reginfo);
4115
4116 /* We have found the .reginfo section in the output file.
4117 Look through all the link_orders comprising it and merge
4118 the information together. */
4119 for (p = o->link_order_head;
4120 p != (struct bfd_link_order *) NULL;
4121 p = p->next)
4122 {
4123 asection *input_section;
4124 bfd *input_bfd;
4125 Elf32_External_RegInfo ext;
4126 Elf32_RegInfo sub;
4127
4128 if (p->type != bfd_indirect_link_order)
4129 {
4130 if (p->type == bfd_fill_link_order)
4131 continue;
4132 abort ();
4133 }
4134
4135 input_section = p->u.indirect.section;
4136 input_bfd = input_section->owner;
4137
4138 /* The linker emulation code has probably clobbered the
4139 size to be zero bytes. */
4140 if (input_section->_raw_size == 0)
4141 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4142
4143 if (! bfd_get_section_contents (input_bfd, input_section,
4144 (PTR) &ext,
4145 (file_ptr) 0,
4146 sizeof ext))
4147 return false;
4148
4149 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4150
4151 reginfo.ri_gprmask |= sub.ri_gprmask;
4152 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4153 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4154 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4155 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4156
4157 /* ri_gp_value is set by the function
4158 mips_elf32_section_processing when the section is
4159 finally written out. */
4160
4161 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4162 elf_link_input_bfd ignores this section. */
4163 input_section->flags &=~ SEC_HAS_CONTENTS;
4164 }
4165
4166 /* Force the section size to the value we want. */
4167 o->_raw_size = sizeof (Elf32_External_RegInfo);
4168
4169 /* Skip this section later on (I don't think this currently
4170 matters, but someday it might). */
4171 o->link_order_head = (struct bfd_link_order *) NULL;
4172
4173 reginfo_sec = o;
4174 }
4175
4176 if (strcmp (o->name, ".mdebug") == 0)
4177 {
4178 struct extsym_info einfo;
4179
4180 /* We have found the .mdebug section in the output file.
4181 Look through all the link_orders comprising it and merge
4182 the information together. */
4183 symhdr->magic = swap->sym_magic;
4184 /* FIXME: What should the version stamp be? */
4185 symhdr->vstamp = 0;
4186 symhdr->ilineMax = 0;
4187 symhdr->cbLine = 0;
4188 symhdr->idnMax = 0;
4189 symhdr->ipdMax = 0;
4190 symhdr->isymMax = 0;
4191 symhdr->ioptMax = 0;
4192 symhdr->iauxMax = 0;
4193 symhdr->issMax = 0;
4194 symhdr->issExtMax = 0;
4195 symhdr->ifdMax = 0;
4196 symhdr->crfd = 0;
4197 symhdr->iextMax = 0;
4198
4199 /* We accumulate the debugging information itself in the
4200 debug_info structure. */
4201 debug.line = NULL;
4202 debug.external_dnr = NULL;
4203 debug.external_pdr = NULL;
4204 debug.external_sym = NULL;
4205 debug.external_opt = NULL;
4206 debug.external_aux = NULL;
4207 debug.ss = NULL;
4208 debug.ssext = debug.ssext_end = NULL;
4209 debug.external_fdr = NULL;
4210 debug.external_rfd = NULL;
4211 debug.external_ext = debug.external_ext_end = NULL;
4212
4213 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4214 if (mdebug_handle == (PTR) NULL)
4215 return false;
4216
4217 if (SGI_COMPAT (abfd))
4218 {
4219 asection *s;
4220 EXTR esym;
4221 bfd_vma last;
4222 unsigned int i;
4223 static const char * const name[] =
4224 { ".text", ".init", ".fini", ".data",
4225 ".rodata", ".sdata", ".sbss", ".bss" };
4226 static const int sc[] = { scText, scInit, scFini, scData,
4227 scRData, scSData, scSBss, scBss };
4228
4229 esym.jmptbl = 0;
4230 esym.cobol_main = 0;
4231 esym.weakext = 0;
4232 esym.reserved = 0;
4233 esym.ifd = ifdNil;
4234 esym.asym.iss = issNil;
4235 esym.asym.st = stLocal;
4236 esym.asym.reserved = 0;
4237 esym.asym.index = indexNil;
4238 last = 0;
4239 for (i = 0; i < 8; i++)
4240 {
4241 esym.asym.sc = sc[i];
4242 s = bfd_get_section_by_name (abfd, name[i]);
4243 if (s != NULL)
4244 {
4245 esym.asym.value = s->vma;
4246 last = s->vma + s->_raw_size;
4247 }
4248 else
4249 esym.asym.value = last;
4250
4251 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4252 name[i], &esym))
4253 return false;
4254 }
4255 }
4256
4257 for (p = o->link_order_head;
4258 p != (struct bfd_link_order *) NULL;
4259 p = p->next)
4260 {
4261 asection *input_section;
4262 bfd *input_bfd;
4263 const struct ecoff_debug_swap *input_swap;
4264 struct ecoff_debug_info input_debug;
4265 char *eraw_src;
4266 char *eraw_end;
4267
4268 if (p->type != bfd_indirect_link_order)
4269 {
4270 if (p->type == bfd_fill_link_order)
4271 continue;
4272 abort ();
4273 }
4274
4275 input_section = p->u.indirect.section;
4276 input_bfd = input_section->owner;
4277
4278 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4279 || (get_elf_backend_data (input_bfd)
4280 ->elf_backend_ecoff_debug_swap) == NULL)
4281 {
4282 /* I don't know what a non MIPS ELF bfd would be
4283 doing with a .mdebug section, but I don't really
4284 want to deal with it. */
4285 continue;
4286 }
4287
4288 input_swap = (get_elf_backend_data (input_bfd)
4289 ->elf_backend_ecoff_debug_swap);
4290
4291 BFD_ASSERT (p->size == input_section->_raw_size);
4292
4293 /* The ECOFF linking code expects that we have already
4294 read in the debugging information and set up an
4295 ecoff_debug_info structure, so we do that now. */
4296 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4297 &input_debug))
4298 return false;
4299
4300 if (! (bfd_ecoff_debug_accumulate
4301 (mdebug_handle, abfd, &debug, swap, input_bfd,
4302 &input_debug, input_swap, info)))
4303 return false;
4304
4305 /* Loop through the external symbols. For each one with
4306 interesting information, try to find the symbol in
4307 the linker global hash table and save the information
4308 for the output external symbols. */
4309 eraw_src = input_debug.external_ext;
4310 eraw_end = (eraw_src
4311 + (input_debug.symbolic_header.iextMax
4312 * input_swap->external_ext_size));
4313 for (;
4314 eraw_src < eraw_end;
4315 eraw_src += input_swap->external_ext_size)
4316 {
4317 EXTR ext;
4318 const char *name;
4319 struct mips_elf_link_hash_entry *h;
4320
4321 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4322 if (ext.asym.sc == scNil
4323 || ext.asym.sc == scUndefined
4324 || ext.asym.sc == scSUndefined)
4325 continue;
4326
4327 name = input_debug.ssext + ext.asym.iss;
4328 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4329 name, false, false, true);
4330 if (h == NULL || h->esym.ifd != -2)
4331 continue;
4332
4333 if (ext.ifd != -1)
4334 {
4335 BFD_ASSERT (ext.ifd
4336 < input_debug.symbolic_header.ifdMax);
4337 ext.ifd = input_debug.ifdmap[ext.ifd];
4338 }
4339
4340 h->esym = ext;
4341 }
4342
4343 /* Free up the information we just read. */
4344 free (input_debug.line);
4345 free (input_debug.external_dnr);
4346 free (input_debug.external_pdr);
4347 free (input_debug.external_sym);
4348 free (input_debug.external_opt);
4349 free (input_debug.external_aux);
4350 free (input_debug.ss);
4351 free (input_debug.ssext);
4352 free (input_debug.external_fdr);
4353 free (input_debug.external_rfd);
4354 free (input_debug.external_ext);
4355
4356 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4357 elf_link_input_bfd ignores this section. */
4358 input_section->flags &=~ SEC_HAS_CONTENTS;
4359 }
4360
4361 if (SGI_COMPAT (abfd) && info->shared)
4362 {
4363 /* Create .rtproc section. */
4364 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4365 if (rtproc_sec == NULL)
4366 {
4367 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4368 | SEC_LINKER_CREATED | SEC_READONLY);
4369
4370 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4371 if (rtproc_sec == NULL
4372 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4373 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4374 return false;
4375 }
4376
4377 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4378 info, rtproc_sec, &debug))
4379 return false;
4380 }
4381
4382 /* Build the external symbol information. */
4383 einfo.abfd = abfd;
4384 einfo.info = info;
4385 einfo.debug = &debug;
4386 einfo.swap = swap;
4387 einfo.failed = false;
4388 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4389 mips_elf_output_extsym,
4390 (PTR) &einfo);
4391 if (einfo.failed)
4392 return false;
4393
4394 /* Set the size of the .mdebug section. */
4395 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4396
4397 /* Skip this section later on (I don't think this currently
4398 matters, but someday it might). */
4399 o->link_order_head = (struct bfd_link_order *) NULL;
4400
4401 mdebug_sec = o;
4402 }
4403
4404 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4405 {
4406 const char *subname;
4407 unsigned int c;
4408 Elf32_gptab *tab;
4409 Elf32_External_gptab *ext_tab;
4410 unsigned int i;
4411
4412 /* The .gptab.sdata and .gptab.sbss sections hold
4413 information describing how the small data area would
4414 change depending upon the -G switch. These sections
4415 not used in executables files. */
4416 if (! info->relocateable)
4417 {
4418 asection **secpp;
4419
4420 for (p = o->link_order_head;
4421 p != (struct bfd_link_order *) NULL;
4422 p = p->next)
4423 {
4424 asection *input_section;
4425
4426 if (p->type != bfd_indirect_link_order)
4427 {
4428 if (p->type == bfd_fill_link_order)
4429 continue;
4430 abort ();
4431 }
4432
4433 input_section = p->u.indirect.section;
4434
4435 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4436 elf_link_input_bfd ignores this section. */
4437 input_section->flags &=~ SEC_HAS_CONTENTS;
4438 }
4439
4440 /* Skip this section later on (I don't think this
4441 currently matters, but someday it might). */
4442 o->link_order_head = (struct bfd_link_order *) NULL;
4443
4444 /* Really remove the section. */
4445 for (secpp = &abfd->sections;
4446 *secpp != o;
4447 secpp = &(*secpp)->next)
4448 ;
4449 *secpp = (*secpp)->next;
4450 --abfd->section_count;
4451
4452 continue;
4453 }
4454
4455 /* There is one gptab for initialized data, and one for
4456 uninitialized data. */
4457 if (strcmp (o->name, ".gptab.sdata") == 0)
4458 gptab_data_sec = o;
4459 else if (strcmp (o->name, ".gptab.sbss") == 0)
4460 gptab_bss_sec = o;
4461 else
4462 {
4463 (*_bfd_error_handler)
4464 (_("%s: illegal section name `%s'"),
4465 bfd_get_filename (abfd), o->name);
4466 bfd_set_error (bfd_error_nonrepresentable_section);
4467 return false;
4468 }
4469
4470 /* The linker script always combines .gptab.data and
4471 .gptab.sdata into .gptab.sdata, and likewise for
4472 .gptab.bss and .gptab.sbss. It is possible that there is
4473 no .sdata or .sbss section in the output file, in which
4474 case we must change the name of the output section. */
4475 subname = o->name + sizeof ".gptab" - 1;
4476 if (bfd_get_section_by_name (abfd, subname) == NULL)
4477 {
4478 if (o == gptab_data_sec)
4479 o->name = ".gptab.data";
4480 else
4481 o->name = ".gptab.bss";
4482 subname = o->name + sizeof ".gptab" - 1;
4483 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4484 }
4485
4486 /* Set up the first entry. */
4487 c = 1;
4488 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4489 if (tab == NULL)
4490 return false;
4491 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4492 tab[0].gt_header.gt_unused = 0;
4493
4494 /* Combine the input sections. */
4495 for (p = o->link_order_head;
4496 p != (struct bfd_link_order *) NULL;
4497 p = p->next)
4498 {
4499 asection *input_section;
4500 bfd *input_bfd;
4501 bfd_size_type size;
4502 unsigned long last;
4503 bfd_size_type gpentry;
4504
4505 if (p->type != bfd_indirect_link_order)
4506 {
4507 if (p->type == bfd_fill_link_order)
4508 continue;
4509 abort ();
4510 }
4511
4512 input_section = p->u.indirect.section;
4513 input_bfd = input_section->owner;
4514
4515 /* Combine the gptab entries for this input section one
4516 by one. We know that the input gptab entries are
4517 sorted by ascending -G value. */
4518 size = bfd_section_size (input_bfd, input_section);
4519 last = 0;
4520 for (gpentry = sizeof (Elf32_External_gptab);
4521 gpentry < size;
4522 gpentry += sizeof (Elf32_External_gptab))
4523 {
4524 Elf32_External_gptab ext_gptab;
4525 Elf32_gptab int_gptab;
4526 unsigned long val;
4527 unsigned long add;
4528 boolean exact;
4529 unsigned int look;
4530
4531 if (! (bfd_get_section_contents
4532 (input_bfd, input_section, (PTR) &ext_gptab,
4533 gpentry, sizeof (Elf32_External_gptab))))
4534 {
4535 free (tab);
4536 return false;
4537 }
4538
4539 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4540 &int_gptab);
4541 val = int_gptab.gt_entry.gt_g_value;
4542 add = int_gptab.gt_entry.gt_bytes - last;
4543
4544 exact = false;
4545 for (look = 1; look < c; look++)
4546 {
4547 if (tab[look].gt_entry.gt_g_value >= val)
4548 tab[look].gt_entry.gt_bytes += add;
4549
4550 if (tab[look].gt_entry.gt_g_value == val)
4551 exact = true;
4552 }
4553
4554 if (! exact)
4555 {
4556 Elf32_gptab *new_tab;
4557 unsigned int max;
4558
4559 /* We need a new table entry. */
4560 new_tab = ((Elf32_gptab *)
4561 bfd_realloc ((PTR) tab,
4562 (c + 1) * sizeof (Elf32_gptab)));
4563 if (new_tab == NULL)
4564 {
4565 free (tab);
4566 return false;
4567 }
4568 tab = new_tab;
4569 tab[c].gt_entry.gt_g_value = val;
4570 tab[c].gt_entry.gt_bytes = add;
4571
4572 /* Merge in the size for the next smallest -G
4573 value, since that will be implied by this new
4574 value. */
4575 max = 0;
4576 for (look = 1; look < c; look++)
4577 {
4578 if (tab[look].gt_entry.gt_g_value < val
4579 && (max == 0
4580 || (tab[look].gt_entry.gt_g_value
4581 > tab[max].gt_entry.gt_g_value)))
4582 max = look;
4583 }
4584 if (max != 0)
4585 tab[c].gt_entry.gt_bytes +=
4586 tab[max].gt_entry.gt_bytes;
4587
4588 ++c;
4589 }
4590
4591 last = int_gptab.gt_entry.gt_bytes;
4592 }
4593
4594 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4595 elf_link_input_bfd ignores this section. */
4596 input_section->flags &=~ SEC_HAS_CONTENTS;
4597 }
4598
4599 /* The table must be sorted by -G value. */
4600 if (c > 2)
4601 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4602
4603 /* Swap out the table. */
4604 ext_tab = ((Elf32_External_gptab *)
4605 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4606 if (ext_tab == NULL)
4607 {
4608 free (tab);
4609 return false;
4610 }
4611
4612 for (i = 0; i < c; i++)
4613 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4614 free (tab);
4615
4616 o->_raw_size = c * sizeof (Elf32_External_gptab);
4617 o->contents = (bfd_byte *) ext_tab;
4618
4619 /* Skip this section later on (I don't think this currently
4620 matters, but someday it might). */
4621 o->link_order_head = (struct bfd_link_order *) NULL;
4622 }
4623 }
4624
4625 /* Invoke the regular ELF backend linker to do all the work. */
4626 if (! bfd_elf32_bfd_final_link (abfd, info))
4627 return false;
4628
4629 /* Now write out the computed sections. */
4630
4631 if (reginfo_sec != (asection *) NULL)
4632 {
4633 Elf32_External_RegInfo ext;
4634
4635 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
4636 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4637 (file_ptr) 0, sizeof ext))
4638 return false;
4639 }
4640
4641 if (mdebug_sec != (asection *) NULL)
4642 {
4643 BFD_ASSERT (abfd->output_has_begun);
4644 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4645 swap, info,
4646 mdebug_sec->filepos))
4647 return false;
4648
4649 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4650 }
4651
4652 if (gptab_data_sec != (asection *) NULL)
4653 {
4654 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4655 gptab_data_sec->contents,
4656 (file_ptr) 0,
4657 gptab_data_sec->_raw_size))
4658 return false;
4659 }
4660
4661 if (gptab_bss_sec != (asection *) NULL)
4662 {
4663 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4664 gptab_bss_sec->contents,
4665 (file_ptr) 0,
4666 gptab_bss_sec->_raw_size))
4667 return false;
4668 }
4669
4670 if (SGI_COMPAT (abfd))
4671 {
4672 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4673 if (rtproc_sec != NULL)
4674 {
4675 if (! bfd_set_section_contents (abfd, rtproc_sec,
4676 rtproc_sec->contents,
4677 (file_ptr) 0,
4678 rtproc_sec->_raw_size))
4679 return false;
4680 }
4681 }
4682
4683 return true;
4684 }
4685
4686 /* Handle a MIPS ELF HI16 reloc. */
4687
4688 static void
4689 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4690 bfd *input_bfd;
4691 Elf_Internal_Rela *relhi;
4692 Elf_Internal_Rela *rello;
4693 bfd_byte *contents;
4694 bfd_vma addend;
4695 {
4696 bfd_vma insn;
4697 bfd_vma addlo;
4698
4699 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4700
4701 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4702 addlo &= 0xffff;
4703
4704 addend += ((insn & 0xffff) << 16) + addlo;
4705
4706 if ((addlo & 0x8000) != 0)
4707 addend -= 0x10000;
4708 if ((addend & 0x8000) != 0)
4709 addend += 0x10000;
4710
4711 bfd_put_32 (input_bfd,
4712 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
4713 contents + relhi->r_offset);
4714 }
4715
4716 /* Handle a MIPS ELF local GOT16 reloc. */
4717
4718 static boolean
4719 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
4720 contents, addend)
4721 bfd *output_bfd;
4722 bfd *input_bfd;
4723 asection *sgot;
4724 Elf_Internal_Rela *relhi;
4725 Elf_Internal_Rela *rello;
4726 bfd_byte *contents;
4727 bfd_vma addend;
4728 {
4729 unsigned int assigned_gotno;
4730 unsigned int i;
4731 bfd_vma insn;
4732 bfd_vma addlo;
4733 bfd_vma address;
4734 bfd_vma hipage;
4735 bfd_byte *got_contents;
4736 struct mips_got_info *g;
4737
4738 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4739
4740 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4741 addlo &= 0xffff;
4742
4743 addend += ((insn & 0xffff) << 16) + addlo;
4744
4745 if ((addlo & 0x8000) != 0)
4746 addend -= 0x10000;
4747 if ((addend & 0x8000) != 0)
4748 addend += 0x10000;
4749
4750 /* Get a got entry representing requested hipage. */
4751 BFD_ASSERT (elf_section_data (sgot) != NULL);
4752 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4753 BFD_ASSERT (g != NULL);
4754
4755 assigned_gotno = g->assigned_gotno;
4756 got_contents = sgot->contents;
4757 hipage = addend & 0xffff0000;
4758
4759 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
4760 {
4761 address = bfd_get_32 (input_bfd, got_contents + i * 4);
4762 if (hipage == (address & 0xffff0000))
4763 break;
4764 }
4765
4766 if (i == assigned_gotno)
4767 {
4768 if (assigned_gotno >= g->local_gotno)
4769 {
4770 (*_bfd_error_handler)
4771 (_("more got entries are needed for hipage relocations"));
4772 bfd_set_error (bfd_error_bad_value);
4773 return false;
4774 }
4775
4776 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
4777 ++g->assigned_gotno;
4778 }
4779
4780 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
4781 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
4782 contents + relhi->r_offset);
4783
4784 return true;
4785 }
4786
4787 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
4788
4789 static void
4790 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
4791 bfd *input_bfd;
4792 Elf_Internal_Rela *rel;
4793 bfd_byte *contents;
4794 bfd_vma offset;
4795 {
4796 bfd_vma insn;
4797
4798 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
4799 bfd_put_32 (input_bfd,
4800 (insn & 0xffff0000) | (offset & 0xffff),
4801 contents + rel->r_offset);
4802 }
4803
4804 /* Relocate a MIPS ELF section. */
4805
4806 static boolean
4807 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4808 contents, relocs, local_syms, local_sections)
4809 bfd *output_bfd;
4810 struct bfd_link_info *info;
4811 bfd *input_bfd;
4812 asection *input_section;
4813 bfd_byte *contents;
4814 Elf_Internal_Rela *relocs;
4815 Elf_Internal_Sym *local_syms;
4816 asection **local_sections;
4817 {
4818 Elf_Internal_Shdr *symtab_hdr;
4819 size_t locsymcount;
4820 size_t extsymoff;
4821 asection *sgot, *sreloc, *scpt;
4822 bfd *dynobj;
4823 bfd_vma gp;
4824 Elf_Internal_Rela *rel;
4825 Elf_Internal_Rela *relend;
4826 struct mips_got_info *g;
4827
4828 dynobj = elf_hash_table (info)->dynobj;
4829 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4830
4831 sgot = NULL;
4832 sreloc = NULL;
4833 if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
4834 scpt = NULL;
4835 else
4836 scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4837 g = NULL;
4838
4839 if (elf_bad_symtab (input_bfd))
4840 {
4841 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4842 extsymoff = 0;
4843 }
4844 else
4845 {
4846 locsymcount = symtab_hdr->sh_info;
4847 extsymoff = symtab_hdr->sh_info;
4848 }
4849
4850 gp = _bfd_get_gp_value (output_bfd);
4851
4852 rel = relocs;
4853 relend = relocs + input_section->reloc_count;
4854 for (; rel < relend; rel++)
4855 {
4856 int r_type;
4857 reloc_howto_type *howto;
4858 unsigned long r_symndx;
4859 bfd_vma addend;
4860 struct elf_link_hash_entry *h;
4861 asection *sec;
4862 Elf_Internal_Sym *sym;
4863 struct mips_elf_link_hash_entry *mh;
4864 int other;
4865 bfd_reloc_status_type r;
4866
4867 r_type = ELF32_R_TYPE (rel->r_info);
4868 if (r_type == R_MIPS_GNU_VTINHERIT
4869 || r_type == R_MIPS_GNU_VTENTRY)
4870 continue;
4871 if ((r_type < 0 || r_type >= (int) R_MIPS_max)
4872 /* start-sanitize-sky */
4873 && r_type != R_MIPS_DVP_11_PCREL
4874 && r_type != R_MIPS_DVP_27_S4
4875 /* end-sanitize-sky */
4876 && r_type != R_MIPS16_26
4877 && r_type != R_MIPS16_GPREL)
4878 {
4879 bfd_set_error (bfd_error_bad_value);
4880 return false;
4881 }
4882 if (r_type == R_MIPS16_26)
4883 howto = &elf_mips16_jump_howto;
4884 else if (r_type == R_MIPS16_GPREL)
4885 howto = &elf_mips16_gprel_howto;
4886 /* start-sanitize-sky */
4887 else if (r_type == R_MIPS_DVP_11_PCREL)
4888 howto = &elf_mips_dvp_11_pcrel_howto;
4889 else if (r_type == R_MIPS_DVP_27_S4)
4890 howto = &elf_mips_dvp_27_s4_howto;
4891 /* end-sanitize-sky */
4892 else
4893 howto = elf_mips_howto_table + r_type;
4894
4895 if (dynobj != NULL
4896 && (r_type == R_MIPS_CALL16
4897 || r_type == R_MIPS_GOT16
4898 || r_type == R_MIPS_CALL_HI16
4899 || r_type == R_MIPS_CALL_LO16
4900 || r_type == R_MIPS_GOT_HI16
4901 || r_type == R_MIPS_GOT_LO16))
4902 {
4903 /* We need the .got section. */
4904 if (sgot == NULL)
4905 {
4906 sgot = bfd_get_section_by_name (dynobj, ".got");
4907 BFD_ASSERT (sgot != NULL);
4908 BFD_ASSERT (elf_section_data (sgot) != NULL);
4909 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4910 BFD_ASSERT (g != NULL);
4911 }
4912 }
4913
4914 r_symndx = ELF32_R_SYM (rel->r_info);
4915
4916 /* Mix in the change in GP address for a GP relative reloc. */
4917 if (r_type != R_MIPS_GPREL16
4918 && r_type != R_MIPS_LITERAL
4919 && r_type != R_MIPS_GPREL32
4920 && r_type != R_MIPS16_GPREL)
4921 addend = 0;
4922 else
4923 {
4924 if (gp == 0)
4925 {
4926 if (! ((*info->callbacks->reloc_dangerous)
4927 (info,
4928 _("GP relative relocation when GP not defined"),
4929 input_bfd, input_section,
4930 rel->r_offset)))
4931 return false;
4932 /* Only give the error once per link. */
4933 gp = 4;
4934 _bfd_set_gp_value (output_bfd, gp);
4935 }
4936
4937 if (r_symndx < extsymoff
4938 || (elf_bad_symtab (input_bfd)
4939 && local_sections[r_symndx] != NULL))
4940 {
4941 /* This is a relocation against a section. The current
4942 addend in the instruction is the difference between
4943 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
4944 must change this to be the difference between the
4945 final definition (which will end up in RELOCATION)
4946 and the GP value of OUTPUT_BFD (which is in GP). */
4947 addend = elf_gp (input_bfd) - gp;
4948 }
4949 else if (! info->relocateable)
4950 {
4951 /* We are doing a final link. The current addend in the
4952 instruction is simply the desired offset into the
4953 symbol (normally zero). We want the instruction to
4954 hold the difference between the final definition of
4955 the symbol (which will end up in RELOCATION) and the
4956 GP value of OUTPUT_BFD (which is in GP). */
4957 addend = - gp;
4958 }
4959 else
4960 {
4961 /* We are generating relocateable output, and we aren't
4962 going to define this symbol, so we just leave the
4963 instruction alone. */
4964 addend = 0;
4965 }
4966 }
4967
4968 h = NULL;
4969 sym = NULL;
4970 sec = NULL;
4971 if (info->relocateable)
4972 {
4973 /* This is a relocateable link. We don't have to change
4974 anything, unless the reloc is against a section symbol,
4975 in which case we have to adjust according to where the
4976 section symbol winds up in the output section. */
4977 if (r_symndx >= locsymcount
4978 || (elf_bad_symtab (input_bfd)
4979 && local_sections[r_symndx] == NULL))
4980 r = bfd_reloc_ok;
4981 else
4982 {
4983 sym = local_syms + r_symndx;
4984 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4985 r = bfd_reloc_ok;
4986 else
4987 {
4988 sec = local_sections[r_symndx];
4989
4990 /* It would be logical to add sym->st_value here,
4991 but Irix 5 sometimes generates a garbage symbol
4992 value. */
4993 addend += sec->output_offset;
4994
4995 /* If this is HI16 or GOT16 with an associated LO16,
4996 adjust the addend accordingly. Otherwise, just
4997 relocate. */
4998 if (r_type == R_MIPS_64 && bfd_big_endian (input_bfd))
4999 r = _bfd_relocate_contents (howto, input_bfd,
5000 addend,
5001 contents + rel->r_offset + 4);
5002 else if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
5003 r = _bfd_relocate_contents (howto, input_bfd,
5004 addend,
5005 contents + rel->r_offset);
5006 else
5007 {
5008 Elf_Internal_Rela *lorel;
5009
5010 /* As a GNU extension, permit an arbitrary
5011 number of R_MIPS_HI16 relocs before the
5012 R_MIPS_LO16 reloc. This permits gcc to emit
5013 the HI and LO relocs itself. */
5014 if (r_type == R_MIPS_GOT16)
5015 lorel = rel + 1;
5016 else
5017 {
5018 for (lorel = rel + 1;
5019 (lorel < relend
5020 && (ELF32_R_TYPE (lorel->r_info)
5021 == R_MIPS_HI16));
5022 lorel++)
5023 ;
5024 }
5025 if (lorel < relend
5026 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5027 {
5028 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5029 contents, addend);
5030 r = bfd_reloc_ok;
5031 }
5032 else
5033 r = _bfd_relocate_contents (howto, input_bfd,
5034 addend,
5035 contents + rel->r_offset);
5036 }
5037 }
5038 }
5039 }
5040 else
5041 {
5042 bfd_vma relocation;
5043 boolean local;
5044 boolean undefined_error;
5045
5046 /* This is a final link. */
5047 undefined_error = false;
5048 sym = NULL;
5049 if (r_symndx < extsymoff
5050 || (elf_bad_symtab (input_bfd)
5051 && local_sections[r_symndx] != NULL))
5052 {
5053 local = true;
5054 sym = local_syms + r_symndx;
5055 sec = local_sections[r_symndx];
5056 relocation = (sec->output_section->vma
5057 + sec->output_offset);
5058
5059 /* It would be logical to always add sym->st_value here,
5060 but Irix 5 sometimes generates a garbage symbol
5061 value. */
5062 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5063 relocation += sym->st_value;
5064
5065 /* mips16 text labels should be treated as odd. */
5066 if (sym->st_other == STO_MIPS16)
5067 ++relocation;
5068 }
5069 else
5070 {
5071 long indx;
5072
5073 local = false;
5074 indx = r_symndx - extsymoff;
5075 h = elf_sym_hashes (input_bfd)[indx];
5076 while (h->root.type == bfd_link_hash_indirect
5077 || h->root.type == bfd_link_hash_warning)
5078 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5079 if (strcmp (h->root.root.string, "_gp_disp") == 0)
5080 {
5081 if (gp == 0)
5082 {
5083 if (! ((*info->callbacks->reloc_dangerous)
5084 (info,
5085 _("_gp_disp used when GP not defined"),
5086 input_bfd, input_section,
5087 rel->r_offset)))
5088 return false;
5089 /* Only give the error once per link. */
5090 gp = 4;
5091 _bfd_set_gp_value (output_bfd, gp);
5092 relocation = 0;
5093 }
5094 else
5095 {
5096 sec = input_section;
5097 if (sec->output_section != NULL)
5098 relocation = (gp
5099 - (rel->r_offset
5100 + sec->output_section->vma
5101 + sec->output_offset));
5102 else
5103 relocation = gp - rel->r_offset;
5104 if (r_type == R_MIPS_LO16)
5105 relocation += 4;
5106 }
5107 }
5108 else if (h->root.type == bfd_link_hash_defined
5109 || h->root.type == bfd_link_hash_defweak)
5110 {
5111 sec = h->root.u.def.section;
5112 if (sec->output_section == NULL)
5113 relocation = 0;
5114 else
5115 relocation = (h->root.u.def.value
5116 + sec->output_section->vma
5117 + sec->output_offset);
5118 }
5119 else if (h->root.type == bfd_link_hash_undefweak)
5120 relocation = 0;
5121 else if (info->shared && ! info->symbolic)
5122 relocation = 0;
5123 else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
5124 {
5125 /* If this is a dynamic link, we should have created
5126 a _DYNAMIC_LINK symbol in
5127 mips_elf_create_dynamic_sections. Otherwise, we
5128 should define the symbol with a value of 0.
5129 FIXME: It should probably get into the symbol
5130 table somehow as well. */
5131 BFD_ASSERT (! info->shared);
5132 BFD_ASSERT (bfd_get_section_by_name (output_bfd,
5133 ".dynamic") == NULL);
5134 relocation = 0;
5135 }
5136 else
5137 {
5138 if (! ((*info->callbacks->undefined_symbol)
5139 (info, h->root.root.string, input_bfd,
5140 input_section, rel->r_offset)))
5141 return false;
5142 undefined_error = true;
5143 relocation = 0;
5144 }
5145 }
5146
5147 mh = (struct mips_elf_link_hash_entry *) h;
5148 if (h != NULL)
5149 other = h->other;
5150 else if (sym != NULL)
5151 other = sym->st_other;
5152 else
5153 other = 0;
5154
5155 /* If this function has an fn_stub, then it is a mips16
5156 function which needs a stub if it is called by a 32 bit
5157 function. If this reloc is anything other than a 16 bit
5158 call, redirect the reloc to the stub. We don't redirect
5159 relocs from other stub functions. */
5160 if (r_type != R_MIPS16_26
5161 && ((mh != NULL
5162 && mh->fn_stub != NULL)
5163 || (mh == NULL
5164 && elf_tdata (input_bfd)->local_stubs != NULL
5165 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5166 && strncmp (bfd_get_section_name (input_bfd, input_section),
5167 FN_STUB, sizeof FN_STUB - 1) != 0
5168 && strncmp (bfd_get_section_name (input_bfd, input_section),
5169 CALL_STUB, sizeof CALL_STUB - 1) != 0
5170 && strncmp (bfd_get_section_name (input_bfd, input_section),
5171 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) != 0)
5172 {
5173 if (mh != NULL)
5174 {
5175 BFD_ASSERT (mh->need_fn_stub);
5176 relocation = (mh->fn_stub->output_section->vma
5177 + mh->fn_stub->output_offset);
5178 }
5179 else
5180 {
5181 asection *fn_stub;
5182
5183 fn_stub = elf_tdata (input_bfd)->local_stubs[r_symndx];
5184 relocation = (fn_stub->output_section->vma
5185 + fn_stub->output_offset);
5186 }
5187
5188 /* RELOCATION now points to 32 bit code. */
5189 other = 0;
5190 }
5191
5192 /* If this function has a call_stub, then it is called by a
5193 mips16 function; the call needs to go through a stub if
5194 this function is a 32 bit function. If this reloc is a
5195 16 bit call, and the symbol is not a 16 bit function,
5196 then redirect the reloc to the stub. Note that we don't
5197 need to worry about calling the function through a
5198 function pointer; such calls are handled by routing
5199 through a special mips16 routine. We don't have to check
5200 whether this call is from a stub; it can't be, because a
5201 stub contains 32 bit code, and hence can not have a 16
5202 bit reloc. */
5203 if (r_type == R_MIPS16_26
5204 && mh != NULL
5205 && (mh->call_stub != NULL || mh->call_fp_stub != NULL)
5206 && other != STO_MIPS16)
5207 {
5208 asection *stub;
5209
5210 /* If both call_stub and call_fp_stub are defined, we
5211 can figure out which one to use by seeing which one
5212 appears in the input file. */
5213 if (mh->call_stub != NULL && mh->call_fp_stub != NULL)
5214 {
5215 asection *o;
5216
5217 stub = NULL;
5218 for (o = input_bfd->sections; o != NULL; o = o->next)
5219 {
5220 if (strncmp (bfd_get_section_name (input_bfd, o),
5221 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5222 {
5223 stub = mh->call_fp_stub;
5224 break;
5225 }
5226 }
5227 if (stub == NULL)
5228 stub = mh->call_stub;
5229 }
5230 else if (mh->call_stub != NULL)
5231 stub = mh->call_stub;
5232 else
5233 stub = mh->call_fp_stub;
5234
5235 BFD_ASSERT (stub->_raw_size > 0);
5236 relocation = stub->output_section->vma + stub->output_offset;
5237 }
5238
5239 if (r_type == R_MIPS_HI16)
5240 {
5241 Elf_Internal_Rela *lorel;
5242
5243 /* As a GNU extension, permit an arbitrary number of
5244 R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
5245 This permits gcc to emit the HI and LO relocs itself. */
5246 for (lorel = rel + 1;
5247 (lorel < relend
5248 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
5249 lorel++)
5250 ;
5251 if (lorel < relend
5252 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5253 {
5254 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5255 contents, relocation + addend);
5256 r = bfd_reloc_ok;
5257 }
5258 else
5259 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5260 contents, rel->r_offset,
5261 relocation, addend);
5262 }
5263 else if (r_type == R_MIPS_GOT16 && local)
5264 {
5265 /* GOT16 must also have an associated LO16 in the local
5266 case. In this case, the addend is extracted and the
5267 section in which the referenced object is determined.
5268 Then the final address of the object is computed and
5269 the GOT entry for the hipage (an aligned 64kb chunk)
5270 is added to .got section if needed. The offset field
5271 of the GOT16-relocated instruction is replaced by the
5272 index of this GOT entry for the hipage. */
5273 if ((rel + 1) < relend
5274 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
5275 {
5276 if (! mips_elf_relocate_got_local (output_bfd, input_bfd,
5277 sgot, rel, rel + 1,
5278 contents,
5279 relocation + addend))
5280 return false;
5281 r = bfd_reloc_ok;
5282 }
5283 else
5284 r = bfd_reloc_outofrange;
5285 }
5286 else if (r_type == R_MIPS_CALL16
5287 || r_type == R_MIPS_GOT16
5288 || r_type == R_MIPS_CALL_LO16
5289 || r_type == R_MIPS_GOT_LO16)
5290 {
5291 bfd_vma offset;
5292
5293 /* This symbol must be registered as a global symbol
5294 having the corresponding got entry. */
5295 BFD_ASSERT (h->got.offset != (bfd_vma) -1);
5296
5297 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5298 BFD_ASSERT (g->local_gotno <= offset
5299 && offset < sgot->_raw_size);
5300 bfd_put_32 (output_bfd, relocation + addend,
5301 sgot->contents + offset);
5302 offset = (sgot->output_section->vma + sgot->output_offset
5303 + offset - gp);
5304 mips_elf_relocate_global_got (input_bfd, rel, contents,
5305 offset);
5306 r = bfd_reloc_ok;
5307 }
5308 else if (r_type == R_MIPS_CALL_HI16
5309 || r_type == R_MIPS_GOT_HI16)
5310 {
5311 bfd_vma offset;
5312
5313 /* This must be a global symbol with a got entry. The
5314 next reloc must be the corresponding LO16 reloc. */
5315 BFD_ASSERT (h != NULL && h->got.offset != (bfd_vma) -1);
5316 BFD_ASSERT ((rel + 1) < relend);
5317 BFD_ASSERT ((int) ELF32_R_TYPE ((rel + 1)->r_info)
5318 == (r_type == R_MIPS_CALL_HI16
5319 ? (int) R_MIPS_CALL_LO16
5320 : (int) R_MIPS_GOT_LO16));
5321
5322 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5323 BFD_ASSERT (g->local_gotno <= offset
5324 && offset < sgot->_raw_size);
5325 bfd_put_32 (output_bfd, relocation + addend,
5326 sgot->contents + offset);
5327 offset = (sgot->output_section->vma + sgot->output_offset
5328 + offset - gp);
5329 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
5330 offset);
5331 r = bfd_reloc_ok;
5332 }
5333 else if (r_type == R_MIPS_REL32
5334 || r_type == R_MIPS_32)
5335 {
5336 Elf_Internal_Rel outrel;
5337 Elf32_crinfo cptrel;
5338 bfd_byte *cr;
5339
5340 if ((info->shared
5341 || (elf_hash_table (info)->dynamic_sections_created
5342 && h != NULL
5343 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
5344 == 0)))
5345 && (input_section->flags & SEC_ALLOC) != 0)
5346 {
5347 boolean skip;
5348
5349 /* When generating a shared object, these
5350 relocations are copied into the output file to be
5351 resolved at run time. */
5352 if (sreloc == NULL)
5353 {
5354 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
5355 BFD_ASSERT (sreloc != NULL);
5356 }
5357
5358 skip = false;
5359
5360 if (elf_section_data (input_section)->stab_info == NULL)
5361 outrel.r_offset = rel->r_offset;
5362 else
5363 {
5364 bfd_vma off;
5365
5366 off = (_bfd_stab_section_offset
5367 (output_bfd, &elf_hash_table (info)->stab_info,
5368 input_section,
5369 &elf_section_data (input_section)->stab_info,
5370 rel->r_offset));
5371 if (off == (bfd_vma) -1)
5372 skip = true;
5373 outrel.r_offset = off;
5374 }
5375
5376 outrel.r_offset += (input_section->output_section->vma
5377 + input_section->output_offset);
5378
5379 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
5380
5381 if (skip)
5382 memset (&outrel, 0, sizeof outrel);
5383 else if (h != NULL
5384 && (! info->symbolic
5385 || (h->elf_link_hash_flags
5386 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5387 {
5388 BFD_ASSERT (h->dynindx != -1);
5389 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
5390 sec = input_section;
5391 }
5392 else
5393 {
5394 long indx;
5395
5396 if (h == NULL)
5397 sec = local_sections[r_symndx];
5398 else
5399 {
5400 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5401 || (h->root.type
5402 == bfd_link_hash_defweak));
5403 sec = h->root.u.def.section;
5404 }
5405 if (sec != NULL && bfd_is_abs_section (sec))
5406 indx = 0;
5407 else if (sec == NULL || sec->owner == NULL)
5408 {
5409 bfd_set_error (bfd_error_bad_value);
5410 return false;
5411 }
5412 else
5413 {
5414 asection *osec;
5415
5416 osec = sec->output_section;
5417 indx = elf_section_data (osec)->dynindx;
5418 if (indx == 0)
5419 abort ();
5420 }
5421
5422 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5423 addend += relocation;
5424 }
5425
5426 if (! skip)
5427 bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
5428
5429 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5430 (((Elf32_External_Rel *)
5431 sreloc->contents)
5432 + sreloc->reloc_count));
5433 ++sreloc->reloc_count;
5434
5435 if (! skip && SGI_COMPAT (output_bfd))
5436 {
5437 if (scpt == NULL)
5438 continue;
5439
5440 /* Make an entry of compact relocation info. */
5441 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5442 cptrel.vaddr = (rel->r_offset
5443 + input_section->output_section->vma
5444 + input_section->output_offset);
5445 if (r_type == R_MIPS_REL32)
5446 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5447 else
5448 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5449 mips_elf_set_cr_dist2to (cptrel, 0);
5450 cptrel.konst = addend;
5451
5452 cr = (scpt->contents
5453 + sizeof (Elf32_External_compact_rel));
5454 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5455 ((Elf32_External_crinfo *) cr
5456 + scpt->reloc_count));
5457 ++scpt->reloc_count;
5458 }
5459
5460 /* This reloc will be computed at runtime, so
5461 there's no need to do anything now. */
5462 continue;
5463 }
5464 else
5465 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5466 contents, rel->r_offset,
5467 relocation, addend);
5468 }
5469 else if (r_type == R_MIPS_64)
5470 {
5471 bfd_size_type addr;
5472 unsigned long val;
5473
5474 /* Do a 32 bit relocation, and sign extend to 64 bits. */
5475 addr = rel->r_offset;
5476 if (bfd_big_endian (input_bfd))
5477 addr += 4;
5478 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5479 contents, addr, relocation,
5480 addend);
5481 val = bfd_get_32 (input_bfd, contents + addr);
5482 if ((val & 0x80000000) != 0)
5483 val = 0xffffffff;
5484 else
5485 val = 0;
5486 addr = rel->r_offset;
5487 if (bfd_little_endian (input_bfd))
5488 addr += 4;
5489 bfd_put_32 (input_bfd, val, contents + addr);
5490 }
5491 else if (r_type == R_MIPS_26 && other == STO_MIPS16)
5492 {
5493 unsigned long insn;
5494
5495 /* This is a jump to a mips16 routine from a mips32
5496 routine. We need to change jal into jalx. */
5497 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5498 if (((insn >> 26) & 0x3f) != 0x3
5499 && ((insn >> 26) & 0x3f) != 0x1d)
5500 {
5501 (*_bfd_error_handler)
5502 (_("%s: %s+0x%lx: jump to mips16 routine which is not jal"),
5503 bfd_get_filename (input_bfd),
5504 input_section->name,
5505 (unsigned long) rel->r_offset);
5506 bfd_set_error (bfd_error_bad_value);
5507 return false;
5508 }
5509 insn = (insn & 0x3ffffff) | (0x1d << 26);
5510 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5511 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5512 contents, rel->r_offset,
5513 relocation, addend);
5514 }
5515 else if (r_type == R_MIPS16_26)
5516 {
5517 /* It's easiest to do the normal relocation, and then
5518 dig out the instruction and swap the first word the
5519 way the mips16 expects it. If this is little endian,
5520 though, we need to swap the two words first, and then
5521 swap them back again later, so that the address looks
5522 right. */
5523
5524 if (bfd_little_endian (input_bfd))
5525 {
5526 unsigned long insn;
5527
5528 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5529 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5530 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5531 }
5532
5533 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5534 contents, rel->r_offset,
5535 relocation, addend);
5536 if (r == bfd_reloc_ok)
5537 {
5538 unsigned long insn;
5539
5540 if (bfd_little_endian (input_bfd))
5541 {
5542 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5543 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5544 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5545 }
5546
5547 insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
5548 insn = ((insn & 0xfc00)
5549 | ((insn & 0x1f) << 5)
5550 | ((insn & 0x3e0) >> 5));
5551 /* If this is a jump to a 32 bit routine, then make
5552 it jalx. */
5553 if (other != STO_MIPS16)
5554 insn |= 0x400;
5555 bfd_put_16 (input_bfd, insn, contents + rel->r_offset);
5556 }
5557 }
5558 else if (r_type == R_MIPS16_GPREL)
5559 {
5560 unsigned short extend, insn;
5561 bfd_byte buf[4];
5562 unsigned long final;
5563
5564 /* Extract the addend into buf, run the regular reloc,
5565 and stuff the resulting value back into the
5566 instructions. */
5567 if (rel->r_offset > input_section->_raw_size)
5568 r = bfd_reloc_outofrange;
5569 else
5570 {
5571 extend = bfd_get_16 (input_bfd, contents + rel->r_offset);
5572 insn = bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
5573 bfd_put_32 (input_bfd,
5574 (((extend & 0x1f) << 11)
5575 | (extend & 0x7e0)
5576 | (insn & 0x1f)),
5577 buf);
5578 r = _bfd_final_link_relocate (howto, input_bfd,
5579 input_section, buf,
5580 (bfd_vma) 0, relocation,
5581 addend);
5582 final = bfd_get_32 (input_bfd, buf);
5583 bfd_put_16 (input_bfd,
5584 ((extend & 0xf800)
5585 | ((final >> 11) & 0x1f)
5586 | (final & 0x7e0)),
5587 contents + rel->r_offset);
5588 bfd_put_16 (input_bfd,
5589 ((insn & 0xffe0)
5590 | (final & 0x1f)),
5591 contents + rel->r_offset + 2);
5592 }
5593 }
5594 else
5595 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5596 contents, rel->r_offset,
5597 relocation, addend);
5598
5599 /* The jal instruction can only jump to an address which is
5600 divisible by 4, and it can only jump to an address with
5601 the same upper 4 bits as the PC. */
5602 if (r == bfd_reloc_ok
5603 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26))
5604 {
5605 bfd_vma addr;
5606
5607 addr = relocation;
5608 if (other == STO_MIPS16)
5609 addr &= ~ (bfd_vma) 1;
5610 addr += addend;
5611 if ((addr & 3) != 0
5612 || ((addr & 0xf0000000)
5613 != ((input_section->output_section->vma
5614 + input_section->output_offset
5615 + rel->r_offset)
5616 & 0xf0000000)))
5617 r = bfd_reloc_overflow;
5618 }
5619
5620 /* Don't bother to report a relocation overflow for a call
5621 to a weak undefined symbol with a value of zero. This
5622 permits us to use
5623 if (!f) f();
5624 even if we aren't in range to call address zero. */
5625 if (r == bfd_reloc_overflow
5626 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26)
5627 && relocation + addend == 0
5628 && h != NULL
5629 && h->root.type == bfd_link_hash_undefweak)
5630 r = bfd_reloc_ok;
5631
5632 /* If we've already issued an error for an undefined symbol,
5633 don't issue another useless error. */
5634 if (undefined_error
5635 && (r == bfd_reloc_undefined || r == bfd_reloc_overflow))
5636 r = bfd_reloc_ok;
5637
5638 if (SGI_COMPAT (abfd)
5639 && scpt != NULL
5640 && (input_section->flags & SEC_ALLOC) != 0)
5641 {
5642 Elf32_crinfo cptrel;
5643 bfd_byte *cr;
5644
5645 /* Make an entry of compact relocation info. */
5646 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5647 cptrel.vaddr = (rel->r_offset
5648 + input_section->output_section->vma
5649 + input_section->output_offset);
5650
5651 switch (r_type)
5652 {
5653 case R_MIPS_26:
5654 mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
5655 /* XXX How should we set dist2to in this case. */
5656 mips_elf_set_cr_dist2to (cptrel, 8);
5657 cptrel.konst = addend + relocation;
5658 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5659 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5660 ((Elf32_External_crinfo *) cr
5661 + scpt->reloc_count));
5662 ++scpt->reloc_count;
5663 break;
5664
5665 case R_MIPS_GPREL16:
5666 case R_MIPS_LITERAL:
5667 case R_MIPS_GPREL32:
5668 mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
5669 cptrel.konst = gp - cptrel.vaddr;
5670 mips_elf_set_cr_dist2to (cptrel, 4);
5671 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5672 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5673 ((Elf32_External_crinfo *) cr
5674 + scpt->reloc_count));
5675 ++scpt->reloc_count;
5676 break;
5677
5678 default:
5679 break;
5680 }
5681 }
5682 }
5683
5684 if (r != bfd_reloc_ok)
5685 {
5686 switch (r)
5687 {
5688 default:
5689 case bfd_reloc_outofrange:
5690 abort ();
5691 case bfd_reloc_overflow:
5692 {
5693 const char *name;
5694
5695 if (h != NULL)
5696 name = h->root.root.string;
5697 else
5698 {
5699 name = bfd_elf_string_from_elf_section (input_bfd,
5700 symtab_hdr->sh_link,
5701 sym->st_name);
5702 if (name == NULL)
5703 return false;
5704 if (*name == '\0')
5705 name = bfd_section_name (input_bfd, sec);
5706 }
5707 if (! ((*info->callbacks->reloc_overflow)
5708 (info, name, howto->name, (bfd_vma) 0,
5709 input_bfd, input_section, rel->r_offset)))
5710 return false;
5711 }
5712 break;
5713 }
5714 }
5715 }
5716
5717 return true;
5718 }
5719
5720 /* This hook function is called before the linker writes out a global
5721 symbol. We mark symbols as small common if appropriate. This is
5722 also where we undo the increment of the value for a mips16 symbol. */
5723
5724 /*ARGSIGNORED*/
5725 static boolean
5726 mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5727 bfd *abfd;
5728 struct bfd_link_info *info;
5729 const char *name;
5730 Elf_Internal_Sym *sym;
5731 asection *input_sec;
5732 {
5733 /* If we see a common symbol, which implies a relocatable link, then
5734 if a symbol was small common in an input file, mark it as small
5735 common in the output file. */
5736 if (sym->st_shndx == SHN_COMMON
5737 && strcmp (input_sec->name, ".scommon") == 0)
5738 sym->st_shndx = SHN_MIPS_SCOMMON;
5739
5740 if (sym->st_other == STO_MIPS16
5741 && (sym->st_value & 1) != 0)
5742 --sym->st_value;
5743
5744 return true;
5745 }
5746 \f
5747 /* Functions for the dynamic linker. */
5748
5749 /* The name of the dynamic interpreter. This is put in the .interp
5750 section. */
5751
5752 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
5753
5754 /* Create dynamic sections when linking against a dynamic object. */
5755
5756 static boolean
5757 mips_elf_create_dynamic_sections (abfd, info)
5758 bfd *abfd;
5759 struct bfd_link_info *info;
5760 {
5761 struct elf_link_hash_entry *h;
5762 flagword flags;
5763 register asection *s;
5764 const char * const *namep;
5765
5766 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5767 | SEC_LINKER_CREATED | SEC_READONLY);
5768
5769 /* Mips ABI requests the .dynamic section to be read only. */
5770 s = bfd_get_section_by_name (abfd, ".dynamic");
5771 if (s != NULL)
5772 {
5773 if (! bfd_set_section_flags (abfd, s, flags))
5774 return false;
5775 }
5776
5777 /* We need to create .got section. */
5778 if (! mips_elf_create_got_section (abfd, info))
5779 return false;
5780
5781 /* Create .stub section. */
5782 if (bfd_get_section_by_name (abfd, ".stub") == NULL)
5783 {
5784 s = bfd_make_section (abfd, ".stub");
5785 if (s == NULL
5786 || ! bfd_set_section_flags (abfd, s, flags)
5787 || ! bfd_set_section_alignment (abfd, s, 2))
5788 return false;
5789 }
5790
5791 if (SGI_COMPAT (abfd)
5792 && !info->shared
5793 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5794 {
5795 s = bfd_make_section (abfd, ".rld_map");
5796 if (s == NULL
5797 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
5798 || ! bfd_set_section_alignment (abfd, s, 2))
5799 return false;
5800 }
5801
5802 if (SGI_COMPAT (abfd))
5803 {
5804 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5805 {
5806 h = NULL;
5807 if (! (_bfd_generic_link_add_one_symbol
5808 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
5809 (bfd_vma) 0, (const char *) NULL, false,
5810 get_elf_backend_data (abfd)->collect,
5811 (struct bfd_link_hash_entry **) &h)))
5812 return false;
5813 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5814 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5815 h->type = STT_SECTION;
5816
5817 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5818 return false;
5819 }
5820
5821 /* We need to create a .compact_rel section. */
5822 if (! mips_elf_create_compact_rel_section (abfd, info))
5823 return false;
5824
5825 /* Change aligments of some sections. */
5826 s = bfd_get_section_by_name (abfd, ".hash");
5827 if (s != NULL)
5828 bfd_set_section_alignment (abfd, s, 4);
5829 s = bfd_get_section_by_name (abfd, ".dynsym");
5830 if (s != NULL)
5831 bfd_set_section_alignment (abfd, s, 4);
5832 s = bfd_get_section_by_name (abfd, ".dynstr");
5833 if (s != NULL)
5834 bfd_set_section_alignment (abfd, s, 4);
5835 s = bfd_get_section_by_name (abfd, ".reginfo");
5836 if (s != NULL)
5837 bfd_set_section_alignment (abfd, s, 4);
5838 s = bfd_get_section_by_name (abfd, ".dynamic");
5839 if (s != NULL)
5840 bfd_set_section_alignment (abfd, s, 4);
5841 }
5842
5843 if (!info->shared)
5844 {
5845 h = NULL;
5846 if (! (_bfd_generic_link_add_one_symbol
5847 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
5848 (bfd_vma) 0, (const char *) NULL, false,
5849 get_elf_backend_data (abfd)->collect,
5850 (struct bfd_link_hash_entry **) &h)))
5851 return false;
5852 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5853 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5854 h->type = STT_SECTION;
5855
5856 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5857 return false;
5858
5859 if (! mips_elf_hash_table (info)->use_rld_obj_head)
5860 {
5861 /* __rld_map is a four byte word located in the .data section
5862 and is filled in by the rtld to contain a pointer to
5863 the _r_debug structure. Its symbol value will be set in
5864 mips_elf_finish_dynamic_symbol. */
5865 s = bfd_get_section_by_name (abfd, ".rld_map");
5866 BFD_ASSERT (s != NULL);
5867
5868 h = NULL;
5869 if (! (_bfd_generic_link_add_one_symbol
5870 (info, abfd, "__rld_map", BSF_GLOBAL, s,
5871 (bfd_vma) 0, (const char *) NULL, false,
5872 get_elf_backend_data (abfd)->collect,
5873 (struct bfd_link_hash_entry **) &h)))
5874 return false;
5875 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5876 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5877 h->type = STT_OBJECT;
5878
5879 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5880 return false;
5881 }
5882 }
5883
5884 return true;
5885 }
5886
5887 /* Create the .compact_rel section. */
5888
5889 static boolean
5890 mips_elf_create_compact_rel_section (abfd, info)
5891 bfd *abfd;
5892 struct bfd_link_info *info;
5893 {
5894 flagword flags;
5895 register asection *s;
5896
5897 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
5898 {
5899 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5900 | SEC_READONLY);
5901
5902 s = bfd_make_section (abfd, ".compact_rel");
5903 if (s == NULL
5904 || ! bfd_set_section_flags (abfd, s, flags)
5905 || ! bfd_set_section_alignment (abfd, s, 2))
5906 return false;
5907
5908 s->_raw_size = sizeof (Elf32_External_compact_rel);
5909 }
5910
5911 return true;
5912 }
5913
5914 /* Create the .got section to hold the global offset table. */
5915
5916 static boolean
5917 mips_elf_create_got_section (abfd, info)
5918 bfd *abfd;
5919 struct bfd_link_info *info;
5920 {
5921 flagword flags;
5922 register asection *s;
5923 struct elf_link_hash_entry *h;
5924 struct mips_got_info *g;
5925
5926 /* This function may be called more than once. */
5927 if (bfd_get_section_by_name (abfd, ".got") != NULL)
5928 return true;
5929
5930 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5931 | SEC_LINKER_CREATED);
5932
5933 s = bfd_make_section (abfd, ".got");
5934 if (s == NULL
5935 || ! bfd_set_section_flags (abfd, s, flags)
5936 || ! bfd_set_section_alignment (abfd, s, 4))
5937 return false;
5938
5939 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5940 linker script because we don't want to define the symbol if we
5941 are not creating a global offset table. */
5942 h = NULL;
5943 if (! (_bfd_generic_link_add_one_symbol
5944 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5945 (bfd_vma) 0, (const char *) NULL, false,
5946 get_elf_backend_data (abfd)->collect,
5947 (struct bfd_link_hash_entry **) &h)))
5948 return false;
5949 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5950 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5951 h->type = STT_OBJECT;
5952
5953 if (info->shared
5954 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
5955 return false;
5956
5957 /* The first several global offset table entries are reserved. */
5958 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
5959
5960 g = (struct mips_got_info *) bfd_alloc (abfd,
5961 sizeof (struct mips_got_info));
5962 if (g == NULL)
5963 return false;
5964 g->global_gotsym = 0;
5965 g->local_gotno = MIPS_RESERVED_GOTNO;
5966 g->assigned_gotno = MIPS_RESERVED_GOTNO;
5967 if (elf_section_data (s) == NULL)
5968 {
5969 s->used_by_bfd =
5970 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
5971 if (elf_section_data (s) == NULL)
5972 return false;
5973 }
5974 elf_section_data (s)->tdata = (PTR) g;
5975
5976 return true;
5977 }
5978
5979 /* Look through the relocs for a section during the first phase, and
5980 allocate space in the global offset table. */
5981
5982 static boolean
5983 mips_elf_check_relocs (abfd, info, sec, relocs)
5984 bfd *abfd;
5985 struct bfd_link_info *info;
5986 asection *sec;
5987 const Elf_Internal_Rela *relocs;
5988 {
5989 const char *name;
5990 bfd *dynobj;
5991 Elf_Internal_Shdr *symtab_hdr;
5992 struct elf_link_hash_entry **sym_hashes;
5993 struct mips_got_info *g;
5994 size_t extsymoff;
5995 const Elf_Internal_Rela *rel;
5996 const Elf_Internal_Rela *rel_end;
5997 asection *sgot;
5998 asection *sreloc;
5999
6000 if (info->relocateable)
6001 return true;
6002
6003 dynobj = elf_hash_table (info)->dynobj;
6004 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6005 sym_hashes = elf_sym_hashes (abfd);
6006 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6007
6008 /* Check for the mips16 stub sections. */
6009
6010 name = bfd_get_section_name (abfd, sec);
6011 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
6012 {
6013 unsigned long r_symndx;
6014
6015 /* Look at the relocation information to figure out which symbol
6016 this is for. */
6017
6018 r_symndx = ELF32_R_SYM (relocs->r_info);
6019
6020 if (r_symndx < extsymoff
6021 || sym_hashes[r_symndx - extsymoff] == NULL)
6022 {
6023 asection *o;
6024
6025 /* This stub is for a local symbol. This stub will only be
6026 needed if there is some relocation in this BFD, other
6027 than a 16 bit function call, which refers to this symbol. */
6028 for (o = abfd->sections; o != NULL; o = o->next)
6029 {
6030 Elf_Internal_Rela *sec_relocs;
6031 const Elf_Internal_Rela *r, *rend;
6032
6033 /* We can ignore stub sections when looking for relocs. */
6034 if ((o->flags & SEC_RELOC) == 0
6035 || o->reloc_count == 0
6036 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
6037 sizeof FN_STUB - 1) == 0
6038 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
6039 sizeof CALL_STUB - 1) == 0
6040 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
6041 sizeof CALL_FP_STUB - 1) == 0)
6042 continue;
6043
6044 sec_relocs = (_bfd_elf32_link_read_relocs
6045 (abfd, o, (PTR) NULL,
6046 (Elf_Internal_Rela *) NULL,
6047 info->keep_memory));
6048 if (sec_relocs == NULL)
6049 return false;
6050
6051 rend = sec_relocs + o->reloc_count;
6052 for (r = sec_relocs; r < rend; r++)
6053 if (ELF32_R_SYM (r->r_info) == r_symndx
6054 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
6055 break;
6056
6057 if (! info->keep_memory)
6058 free (sec_relocs);
6059
6060 if (r < rend)
6061 break;
6062 }
6063
6064 if (o == NULL)
6065 {
6066 /* There is no non-call reloc for this stub, so we do
6067 not need it. Since this function is called before
6068 the linker maps input sections to output sections, we
6069 can easily discard it by setting the SEC_EXCLUDE
6070 flag. */
6071 sec->flags |= SEC_EXCLUDE;
6072 return true;
6073 }
6074
6075 /* Record this stub in an array of local symbol stubs for
6076 this BFD. */
6077 if (elf_tdata (abfd)->local_stubs == NULL)
6078 {
6079 unsigned long symcount;
6080 asection **n;
6081
6082 if (elf_bad_symtab (abfd))
6083 symcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6084 else
6085 symcount = symtab_hdr->sh_info;
6086 n = (asection **) bfd_zalloc (abfd,
6087 symcount * sizeof (asection *));
6088 if (n == NULL)
6089 return false;
6090 elf_tdata (abfd)->local_stubs = n;
6091 }
6092
6093 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6094
6095 /* We don't need to set mips16_stubs_seen in this case.
6096 That flag is used to see whether we need to look through
6097 the global symbol table for stubs. We don't need to set
6098 it here, because we just have a local stub. */
6099 }
6100 else
6101 {
6102 struct mips_elf_link_hash_entry *h;
6103
6104 h = ((struct mips_elf_link_hash_entry *)
6105 sym_hashes[r_symndx - extsymoff]);
6106
6107 /* H is the symbol this stub is for. */
6108
6109 h->fn_stub = sec;
6110 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6111 }
6112 }
6113 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6114 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6115 {
6116 unsigned long r_symndx;
6117 struct mips_elf_link_hash_entry *h;
6118 asection **loc;
6119
6120 /* Look at the relocation information to figure out which symbol
6121 this is for. */
6122
6123 r_symndx = ELF32_R_SYM (relocs->r_info);
6124
6125 if (r_symndx < extsymoff
6126 || sym_hashes[r_symndx - extsymoff] == NULL)
6127 {
6128 /* This stub was actually built for a static symbol defined
6129 in the same file. We assume that all static symbols in
6130 mips16 code are themselves mips16, so we can simply
6131 discard this stub. Since this function is called before
6132 the linker maps input sections to output sections, we can
6133 easily discard it by setting the SEC_EXCLUDE flag. */
6134 sec->flags |= SEC_EXCLUDE;
6135 return true;
6136 }
6137
6138 h = ((struct mips_elf_link_hash_entry *)
6139 sym_hashes[r_symndx - extsymoff]);
6140
6141 /* H is the symbol this stub is for. */
6142
6143 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6144 loc = &h->call_fp_stub;
6145 else
6146 loc = &h->call_stub;
6147
6148 /* If we already have an appropriate stub for this function, we
6149 don't need another one, so we can discard this one. Since
6150 this function is called before the linker maps input sections
6151 to output sections, we can easily discard it by setting the
6152 SEC_EXCLUDE flag. We can also discard this section if we
6153 happen to already know that this is a mips16 function; it is
6154 not necessary to check this here, as it is checked later, but
6155 it is slightly faster to check now. */
6156 if (*loc != NULL || h->root.other == STO_MIPS16)
6157 {
6158 sec->flags |= SEC_EXCLUDE;
6159 return true;
6160 }
6161
6162 *loc = sec;
6163 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6164 }
6165
6166 if (dynobj == NULL)
6167 {
6168 sgot = NULL;
6169 g = NULL;
6170 }
6171 else
6172 {
6173 sgot = bfd_get_section_by_name (dynobj, ".got");
6174 if (sgot == NULL)
6175 g = NULL;
6176 else
6177 {
6178 BFD_ASSERT (elf_section_data (sgot) != NULL);
6179 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6180 BFD_ASSERT (g != NULL);
6181 }
6182 }
6183
6184 sreloc = NULL;
6185
6186 rel_end = relocs + sec->reloc_count;
6187 for (rel = relocs; rel < rel_end; rel++)
6188 {
6189 unsigned long r_symndx;
6190 struct elf_link_hash_entry *h;
6191
6192 r_symndx = ELF32_R_SYM (rel->r_info);
6193
6194 if (r_symndx < extsymoff)
6195 h = NULL;
6196 else
6197 {
6198 h = sym_hashes[r_symndx - extsymoff];
6199
6200 /* This may be an indirect symbol created because of a version. */
6201 if (h != NULL)
6202 {
6203 while (h->root.type == bfd_link_hash_indirect)
6204 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6205 }
6206 }
6207
6208 /* Some relocs require a global offset table. */
6209 if (dynobj == NULL || sgot == NULL)
6210 {
6211 switch (ELF32_R_TYPE (rel->r_info))
6212 {
6213 case R_MIPS_GOT16:
6214 case R_MIPS_CALL16:
6215 case R_MIPS_CALL_HI16:
6216 case R_MIPS_CALL_LO16:
6217 case R_MIPS_GOT_HI16:
6218 case R_MIPS_GOT_LO16:
6219 if (dynobj == NULL)
6220 elf_hash_table (info)->dynobj = dynobj = abfd;
6221 if (! mips_elf_create_got_section (dynobj, info))
6222 return false;
6223 sgot = bfd_get_section_by_name (dynobj, ".got");
6224 BFD_ASSERT (sgot != NULL);
6225 BFD_ASSERT (elf_section_data (sgot) != NULL);
6226 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6227 BFD_ASSERT (g != NULL);
6228 break;
6229
6230 case R_MIPS_32:
6231 case R_MIPS_REL32:
6232 if (dynobj == NULL
6233 && (info->shared || h != NULL)
6234 && (sec->flags & SEC_ALLOC) != 0)
6235 elf_hash_table (info)->dynobj = dynobj = abfd;
6236 break;
6237
6238 default:
6239 break;
6240 }
6241 }
6242
6243 switch (ELF32_R_TYPE (rel->r_info))
6244 {
6245 case R_MIPS_CALL16:
6246 case R_MIPS_CALL_HI16:
6247 case R_MIPS_CALL_LO16:
6248 /* This symbol requires a global offset table entry. */
6249
6250 if (h == NULL)
6251 {
6252 (*_bfd_error_handler)
6253 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
6254 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
6255 bfd_set_error (bfd_error_bad_value);
6256 return false;
6257 }
6258
6259 /* Make sure this symbol is output as a dynamic symbol. */
6260 if (h->dynindx == -1)
6261 {
6262 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6263 return false;
6264 }
6265
6266 if (h->got.offset != (bfd_vma) -1)
6267 {
6268 /* We have already allocated space in the .got. */
6269 break;
6270 }
6271
6272 /* Note the index of the first global got symbol in .dynsym. */
6273 if (g->global_gotsym == 0
6274 || g->global_gotsym > (unsigned long) h->dynindx)
6275 g->global_gotsym = h->dynindx;
6276
6277 /* Make this symbol to have the corresponding got entry. */
6278 h->got.offset = 0;
6279
6280 /* We need a stub, not a plt entry for the undefined
6281 function. But we record it as if it needs plt. See
6282 elf_adjust_dynamic_symbol in elflink.h. */
6283 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6284 h->type = STT_FUNC;
6285
6286 break;
6287
6288 case R_MIPS_GOT16:
6289 case R_MIPS_GOT_HI16:
6290 case R_MIPS_GOT_LO16:
6291 /* This symbol requires a global offset table entry. */
6292
6293 if (h != NULL)
6294 {
6295 /* Make sure this symbol is output as a dynamic symbol. */
6296 if (h->dynindx == -1)
6297 {
6298 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6299 return false;
6300 }
6301
6302 if (h->got.offset != (bfd_vma) -1)
6303 {
6304 /* We have already allocated space in the .got. */
6305 break;
6306 }
6307 /* Note the index of the first global got symbol in
6308 .dynsym. */
6309 if (g->global_gotsym == 0
6310 || g->global_gotsym > (unsigned long) h->dynindx)
6311 g->global_gotsym = h->dynindx;
6312
6313 /* Make this symbol to be the global got symbol. */
6314 h->got.offset = 0;
6315 }
6316
6317 break;
6318
6319 case R_MIPS_32:
6320 case R_MIPS_REL32:
6321 if ((info->shared || h != NULL)
6322 && (sec->flags & SEC_ALLOC) != 0)
6323 {
6324 if (sreloc == NULL)
6325 {
6326 const char *name = ".rel.dyn";
6327
6328 sreloc = bfd_get_section_by_name (dynobj, name);
6329 if (sreloc == NULL)
6330 {
6331 sreloc = bfd_make_section (dynobj, name);
6332 if (sreloc == NULL
6333 || ! bfd_set_section_flags (dynobj, sreloc,
6334 (SEC_ALLOC
6335 | SEC_LOAD
6336 | SEC_HAS_CONTENTS
6337 | SEC_IN_MEMORY
6338 | SEC_LINKER_CREATED
6339 | SEC_READONLY))
6340 || ! bfd_set_section_alignment (dynobj, sreloc,
6341 4))
6342 return false;
6343 }
6344 }
6345 if (info->shared)
6346 {
6347 /* When creating a shared object, we must copy these
6348 reloc types into the output file as R_MIPS_REL32
6349 relocs. We make room for this reloc in the
6350 .rel.dyn reloc section */
6351 if (sreloc->_raw_size == 0)
6352 {
6353 /* Add a null element. */
6354 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6355 ++sreloc->reloc_count;
6356 }
6357 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6358 }
6359 else
6360 {
6361 struct mips_elf_link_hash_entry *hmips;
6362
6363 /* We only need to copy this reloc if the symbol is
6364 defined in a dynamic object. */
6365 hmips = (struct mips_elf_link_hash_entry *) h;
6366 ++hmips->mips_32_relocs;
6367 }
6368 }
6369
6370 if (SGI_COMPAT (abfd))
6371 mips_elf_hash_table (info)->compact_rel_size +=
6372 sizeof (Elf32_External_crinfo);
6373
6374 break;
6375
6376 case R_MIPS_26:
6377 case R_MIPS_GPREL16:
6378 case R_MIPS_LITERAL:
6379 case R_MIPS_GPREL32:
6380 if (SGI_COMPAT (abfd))
6381 mips_elf_hash_table (info)->compact_rel_size +=
6382 sizeof (Elf32_External_crinfo);
6383 break;
6384
6385 /* This relocation describes the C++ object vtable hierarchy.
6386 Reconstruct it for later use during GC. */
6387 case R_MIPS_GNU_VTINHERIT:
6388 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6389 return false;
6390 break;
6391
6392 /* This relocation describes which C++ vtable entries are actually
6393 used. Record for later use during GC. */
6394 case R_MIPS_GNU_VTENTRY:
6395 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6396 return false;
6397 break;
6398
6399 default:
6400 break;
6401 }
6402
6403 /* If this reloc is not a 16 bit call, and it has a global
6404 symbol, then we will need the fn_stub if there is one.
6405 References from a stub section do not count. */
6406 if (h != NULL
6407 && ELF32_R_TYPE (rel->r_info) != R_MIPS16_26
6408 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6409 sizeof FN_STUB - 1) != 0
6410 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6411 sizeof CALL_STUB - 1) != 0
6412 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6413 sizeof CALL_FP_STUB - 1) != 0)
6414 {
6415 struct mips_elf_link_hash_entry *mh;
6416
6417 mh = (struct mips_elf_link_hash_entry *) h;
6418 mh->need_fn_stub = true;
6419 }
6420 }
6421
6422 return true;
6423 }
6424
6425 /* Return the section that should be marked against GC for a given
6426 relocation. */
6427
6428 static asection *
6429 mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
6430 bfd *abfd;
6431 struct bfd_link_info *info;
6432 Elf_Internal_Rela *rel;
6433 struct elf_link_hash_entry *h;
6434 Elf_Internal_Sym *sym;
6435 {
6436 /* ??? Do mips16 stub sections need to be handled special? */
6437
6438 if (h != NULL)
6439 {
6440 switch (ELF32_R_TYPE (rel->r_info))
6441 {
6442 case R_MIPS_GNU_VTINHERIT:
6443 case R_MIPS_GNU_VTENTRY:
6444 break;
6445
6446 default:
6447 switch (h->root.type)
6448 {
6449 case bfd_link_hash_defined:
6450 case bfd_link_hash_defweak:
6451 return h->root.u.def.section;
6452
6453 case bfd_link_hash_common:
6454 return h->root.u.c.p->section;
6455 }
6456 }
6457 }
6458 else
6459 {
6460 if (!(elf_bad_symtab (abfd)
6461 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
6462 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
6463 && sym->st_shndx != SHN_COMMON))
6464 {
6465 return bfd_section_from_elf_index (abfd, sym->st_shndx);
6466 }
6467 }
6468
6469 return NULL;
6470 }
6471
6472 /* Update the got entry reference counts for the section being removed. */
6473
6474 static boolean
6475 mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
6476 bfd *abfd;
6477 struct bfd_link_info *info;
6478 asection *sec;
6479 const Elf_Internal_Rela *relocs;
6480 {
6481 #if 0
6482 Elf_Internal_Shdr *symtab_hdr;
6483 struct elf_link_hash_entry **sym_hashes;
6484 bfd_signed_vma *local_got_refcounts;
6485 const Elf_Internal_Rela *rel, *relend;
6486 unsigned long r_symndx;
6487 struct elf_link_hash_entry *h;
6488
6489 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6490 sym_hashes = elf_sym_hashes (abfd);
6491 local_got_refcounts = elf_local_got_refcounts (abfd);
6492
6493 relend = relocs + sec->reloc_count;
6494 for (rel = relocs; rel < relend; rel++)
6495 switch (ELF32_R_TYPE (rel->r_info))
6496 {
6497 case R_MIPS_GOT16:
6498 case R_MIPS_CALL16:
6499 case R_MIPS_CALL_HI16:
6500 case R_MIPS_CALL_LO16:
6501 case R_MIPS_GOT_HI16:
6502 case R_MIPS_GOT_LO16:
6503 /* ??? It would seem that the existing MIPS code does no sort
6504 of reference counting or whatnot on its GOT and PLT entries,
6505 so it is not possible to garbage collect them at this time. */
6506 break;
6507
6508 default:
6509 break;
6510 }
6511 #endif
6512
6513 return true;
6514 }
6515
6516
6517 /* Adjust a symbol defined by a dynamic object and referenced by a
6518 regular object. The current definition is in some section of the
6519 dynamic object, but we're not including those sections. We have to
6520 change the definition to something the rest of the link can
6521 understand. */
6522
6523 static boolean
6524 mips_elf_adjust_dynamic_symbol (info, h)
6525 struct bfd_link_info *info;
6526 struct elf_link_hash_entry *h;
6527 {
6528 bfd *dynobj;
6529 struct mips_elf_link_hash_entry *hmips;
6530 asection *s;
6531
6532 dynobj = elf_hash_table (info)->dynobj;
6533
6534 /* Make sure we know what is going on here. */
6535 BFD_ASSERT (dynobj != NULL
6536 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
6537 || h->weakdef != NULL
6538 || ((h->elf_link_hash_flags
6539 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6540 && (h->elf_link_hash_flags
6541 & ELF_LINK_HASH_REF_REGULAR) != 0
6542 && (h->elf_link_hash_flags
6543 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
6544
6545 /* If this symbol is defined in a dynamic object, we need to copy
6546 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6547 file. */
6548 hmips = (struct mips_elf_link_hash_entry *) h;
6549 if (! info->relocateable
6550 && hmips->mips_32_relocs != 0
6551 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6552 {
6553 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6554 BFD_ASSERT (s != NULL);
6555
6556 if (s->_raw_size == 0)
6557 {
6558 /* Make room for a null element. */
6559 s->_raw_size += sizeof (Elf32_External_Rel);
6560 ++s->reloc_count;
6561 }
6562 s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
6563 }
6564
6565 /* For a function, create a stub, if needed. */
6566 if (h->type == STT_FUNC
6567 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
6568 {
6569 if (! elf_hash_table (info)->dynamic_sections_created)
6570 return true;
6571
6572 /* If this symbol is not defined in a regular file, then set
6573 the symbol to the stub location. This is required to make
6574 function pointers compare as equal between the normal
6575 executable and the shared library. */
6576 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6577 {
6578 /* We need .stub section. */
6579 s = bfd_get_section_by_name (dynobj, ".stub");
6580 BFD_ASSERT (s != NULL);
6581
6582 h->root.u.def.section = s;
6583 h->root.u.def.value = s->_raw_size;
6584
6585 /* XXX Write this stub address somewhere. */
6586 h->plt.offset = s->_raw_size;
6587
6588 /* Make room for this stub code. */
6589 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6590
6591 /* The last half word of the stub will be filled with the index
6592 of this symbol in .dynsym section. */
6593 return true;
6594 }
6595 }
6596
6597 /* If this is a weak symbol, and there is a real definition, the
6598 processor independent code will have arranged for us to see the
6599 real definition first, and we can just use the same value. */
6600 if (h->weakdef != NULL)
6601 {
6602 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
6603 || h->weakdef->root.type == bfd_link_hash_defweak);
6604 h->root.u.def.section = h->weakdef->root.u.def.section;
6605 h->root.u.def.value = h->weakdef->root.u.def.value;
6606 return true;
6607 }
6608
6609 /* This is a reference to a symbol defined by a dynamic object which
6610 is not a function. */
6611
6612 return true;
6613 }
6614
6615 /* This function is called after all the input files have been read,
6616 and the input sections have been assigned to output sections. We
6617 check for any mips16 stub sections that we can discard. */
6618
6619 static boolean mips_elf_check_mips16_stubs
6620 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
6621
6622 static boolean
6623 mips_elf_always_size_sections (output_bfd, info)
6624 bfd *output_bfd;
6625 struct bfd_link_info *info;
6626 {
6627 if (info->relocateable
6628 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
6629 return true;
6630
6631 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6632 mips_elf_check_mips16_stubs,
6633 (PTR) NULL);
6634
6635 return true;
6636 }
6637
6638 /* Check the mips16 stubs for a particular symbol, and see if we can
6639 discard them. */
6640
6641 /*ARGSUSED*/
6642 static boolean
6643 mips_elf_check_mips16_stubs (h, data)
6644 struct mips_elf_link_hash_entry *h;
6645 PTR data;
6646 {
6647 if (h->fn_stub != NULL
6648 && ! h->need_fn_stub)
6649 {
6650 /* We don't need the fn_stub; the only references to this symbol
6651 are 16 bit calls. Clobber the size to 0 to prevent it from
6652 being included in the link. */
6653 h->fn_stub->_raw_size = 0;
6654 h->fn_stub->_cooked_size = 0;
6655 h->fn_stub->flags &= ~ SEC_RELOC;
6656 h->fn_stub->reloc_count = 0;
6657 h->fn_stub->flags |= SEC_EXCLUDE;
6658 }
6659
6660 if (h->call_stub != NULL
6661 && h->root.other == STO_MIPS16)
6662 {
6663 /* We don't need the call_stub; this is a 16 bit function, so
6664 calls from other 16 bit functions are OK. Clobber the size
6665 to 0 to prevent it from being included in the link. */
6666 h->call_stub->_raw_size = 0;
6667 h->call_stub->_cooked_size = 0;
6668 h->call_stub->flags &= ~ SEC_RELOC;
6669 h->call_stub->reloc_count = 0;
6670 h->call_stub->flags |= SEC_EXCLUDE;
6671 }
6672
6673 if (h->call_fp_stub != NULL
6674 && h->root.other == STO_MIPS16)
6675 {
6676 /* We don't need the call_stub; this is a 16 bit function, so
6677 calls from other 16 bit functions are OK. Clobber the size
6678 to 0 to prevent it from being included in the link. */
6679 h->call_fp_stub->_raw_size = 0;
6680 h->call_fp_stub->_cooked_size = 0;
6681 h->call_fp_stub->flags &= ~ SEC_RELOC;
6682 h->call_fp_stub->reloc_count = 0;
6683 h->call_fp_stub->flags |= SEC_EXCLUDE;
6684 }
6685
6686 return true;
6687 }
6688
6689 /* Set the sizes of the dynamic sections. */
6690
6691 static boolean
6692 mips_elf_size_dynamic_sections (output_bfd, info)
6693 bfd *output_bfd;
6694 struct bfd_link_info *info;
6695 {
6696 bfd *dynobj;
6697 asection *s;
6698 boolean reltext;
6699 asection *sgot;
6700 struct mips_got_info *g;
6701
6702 dynobj = elf_hash_table (info)->dynobj;
6703 BFD_ASSERT (dynobj != NULL);
6704
6705 if (elf_hash_table (info)->dynamic_sections_created)
6706 {
6707 /* Set the contents of the .interp section to the interpreter. */
6708 if (! info->shared)
6709 {
6710 s = bfd_get_section_by_name (dynobj, ".interp");
6711 BFD_ASSERT (s != NULL);
6712 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
6713 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
6714 }
6715 }
6716
6717 /* Recompute the size of .got for local entires (reserved and
6718 hipages) if needed. To estimate it, get the upper bound of total
6719 size of loadable sections. */
6720 sgot = bfd_get_section_by_name (dynobj, ".got");
6721
6722 if (sgot != NULL)
6723 {
6724 bfd_size_type loadable_size = 0;
6725 bfd_size_type local_gotno;
6726 struct _bfd *sub;
6727
6728 BFD_ASSERT (elf_section_data (sgot) != NULL);
6729 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6730 BFD_ASSERT (g != NULL);
6731
6732 for (sub = info->input_bfds; sub; sub = sub->link_next)
6733 for (s = sub->sections; s != NULL; s = s->next)
6734 {
6735 if ((s->flags & SEC_ALLOC) == 0)
6736 continue;
6737 loadable_size += (s->_raw_size + 0xf) & ~0xf;
6738 }
6739
6740 loadable_size += MIPS_FUNCTION_STUB_SIZE;
6741
6742 /* Assume there are two loadable segments consisting of
6743 contiguous sections. Is 5 enough? */
6744 local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
6745 g->local_gotno = local_gotno;
6746 sgot->_raw_size += local_gotno * 4;
6747 }
6748
6749 /* The check_relocs and adjust_dynamic_symbol entry points have
6750 determined the sizes of the various dynamic sections. Allocate
6751 memory for them. */
6752 reltext = false;
6753 for (s = dynobj->sections; s != NULL; s = s->next)
6754 {
6755 const char *name;
6756 boolean strip;
6757
6758 /* It's OK to base decisions on the section name, because none
6759 of the dynobj section names depend upon the input files. */
6760 name = bfd_get_section_name (dynobj, s);
6761
6762 if ((s->flags & SEC_LINKER_CREATED) == 0)
6763 continue;
6764
6765 strip = false;
6766
6767 if (strncmp (name, ".rel", 4) == 0)
6768 {
6769 if (s->_raw_size == 0)
6770 {
6771 /* We only strip the section if the output section name
6772 has the same name. Otherwise, there might be several
6773 input sections for this output section. FIXME: This
6774 code is probably not needed these days anyhow, since
6775 the linker now does not create empty output sections. */
6776 if (s->output_section != NULL
6777 && strcmp (name,
6778 bfd_get_section_name (s->output_section->owner,
6779 s->output_section)) == 0)
6780 strip = true;
6781 }
6782 else
6783 {
6784 const char *outname;
6785 asection *target;
6786
6787 /* If this relocation section applies to a read only
6788 section, then we probably need a DT_TEXTREL entry.
6789 If the relocation section is .rel.dyn, we always
6790 assert a DT_TEXTREL entry rather than testing whether
6791 there exists a relocation to a read only section or
6792 not. */
6793 outname = bfd_get_section_name (output_bfd,
6794 s->output_section);
6795 target = bfd_get_section_by_name (output_bfd, outname + 4);
6796 if ((target != NULL
6797 && (target->flags & SEC_READONLY) != 0
6798 && (target->flags & SEC_ALLOC) != 0)
6799 || strcmp (outname, ".rel.dyn") == 0)
6800 reltext = true;
6801
6802 /* We use the reloc_count field as a counter if we need
6803 to copy relocs into the output file. */
6804 if (strcmp (name, ".rel.dyn") != 0)
6805 s->reloc_count = 0;
6806 }
6807 }
6808 else if (strncmp (name, ".got", 4) == 0)
6809 {
6810 int i;
6811
6812 BFD_ASSERT (elf_section_data (s) != NULL);
6813 g = (struct mips_got_info *) elf_section_data (s)->tdata;
6814 BFD_ASSERT (g != NULL);
6815
6816 /* Fix the size of .got section for the correspondence of
6817 global symbols and got entries. This adds some useless
6818 got entries. Is this required by ABI really? */
6819 i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
6820 s->_raw_size += i * 4;
6821 }
6822 else if (strncmp (name, ".stub", 5) == 0)
6823 {
6824 /* Irix rld assumes that the function stub isn't at the end
6825 of .text section. So put a dummy. XXX */
6826 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6827 }
6828 else if (! info->shared
6829 && ! mips_elf_hash_table (info)->use_rld_obj_head
6830 && strncmp (name, ".rld_map", 8) == 0)
6831 {
6832 /* We add a room for __rld_map. It will be filled in by the
6833 rtld to contain a pointer to the _r_debug structure. */
6834 s->_raw_size += 4;
6835 }
6836 else if (SGI_COMPAT (output_bfd)
6837 && strncmp (name, ".compact_rel", 12) == 0)
6838 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
6839 else if (strncmp (name, ".init", 5) != 0)
6840 {
6841 /* It's not one of our sections, so don't allocate space. */
6842 continue;
6843 }
6844
6845 if (strip)
6846 {
6847 asection **spp;
6848
6849 for (spp = &s->output_section->owner->sections;
6850 *spp != s->output_section;
6851 spp = &(*spp)->next)
6852 ;
6853 *spp = s->output_section->next;
6854 --s->output_section->owner->section_count;
6855
6856 continue;
6857 }
6858
6859 /* Allocate memory for the section contents. */
6860 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
6861 if (s->contents == NULL && s->_raw_size != 0)
6862 {
6863 bfd_set_error (bfd_error_no_memory);
6864 return false;
6865 }
6866 memset (s->contents, 0, s->_raw_size);
6867 }
6868
6869 if (elf_hash_table (info)->dynamic_sections_created)
6870 {
6871 /* Add some entries to the .dynamic section. We fill in the
6872 values later, in elf_mips_finish_dynamic_sections, but we
6873 must add the entries now so that we get the correct size for
6874 the .dynamic section. The DT_DEBUG entry is filled in by the
6875 dynamic linker and used by the debugger. */
6876 if (! info->shared)
6877 {
6878 if (SGI_COMPAT (output_bfd))
6879 {
6880 /* SGI object has the equivalence of DT_DEBUG in the
6881 DT_MIPS_RLD_MAP entry. */
6882 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
6883 return false;
6884 }
6885 else
6886 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
6887 return false;
6888 }
6889
6890 if (reltext)
6891 {
6892 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
6893 return false;
6894 }
6895
6896 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
6897 return false;
6898
6899 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
6900 {
6901 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
6902 return false;
6903
6904 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
6905 return false;
6906
6907 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
6908 return false;
6909 }
6910
6911 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
6912 return false;
6913
6914 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
6915 return false;
6916
6917 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
6918 {
6919 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
6920 return false;
6921
6922 s = bfd_get_section_by_name (dynobj, ".liblist");
6923 BFD_ASSERT (s != NULL);
6924
6925 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
6926 return false;
6927 }
6928
6929 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
6930 return false;
6931
6932 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
6933 return false;
6934
6935 #if 0
6936 /* Time stamps in executable files are a bad idea. */
6937 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
6938 return false;
6939 #endif
6940
6941 #if 0 /* FIXME */
6942 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
6943 return false;
6944 #endif
6945
6946 #if 0 /* FIXME */
6947 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
6948 return false;
6949 #endif
6950
6951 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
6952 return false;
6953
6954 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
6955 return false;
6956
6957 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
6958 return false;
6959
6960 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
6961 return false;
6962
6963 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
6964 return false;
6965
6966 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
6967 return false;
6968
6969 #if 0 /* (SGI_COMPAT) */
6970 if (! bfd_get_section_by_name (dynobj, ".init"))
6971 if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
6972 return false;
6973
6974 if (! bfd_get_section_by_name (dynobj, ".fini"))
6975 if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
6976 return false;
6977 #endif
6978 }
6979
6980 /* If we use dynamic linking, we generate a section symbol for each
6981 output section. These are local symbols, which means that they
6982 must come first in the dynamic symbol table.
6983 That means we must increment the dynamic symbol index of every
6984 other dynamic symbol. */
6985 {
6986 const char * const *namep;
6987 unsigned int c, i;
6988 bfd_size_type strindex;
6989 struct bfd_strtab_hash *dynstr;
6990 struct mips_got_info *g;
6991
6992 c = 0;
6993 if (elf_hash_table (info)->dynamic_sections_created)
6994 {
6995 if (SGI_COMPAT (output_bfd))
6996 {
6997 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
6998 elf_link_hash_traverse (elf_hash_table (info),
6999 mips_elf_adjust_dynindx,
7000 (PTR) &c);
7001 elf_hash_table (info)->dynsymcount += c;
7002
7003 dynstr = elf_hash_table (info)->dynstr;
7004 BFD_ASSERT (dynstr != NULL);
7005
7006 for (i = 1, namep = mips_elf_dynsym_sec_names;
7007 *namep != NULL;
7008 i++, namep++)
7009 {
7010 s = bfd_get_section_by_name (output_bfd, *namep);
7011 if (s != NULL)
7012 elf_section_data (s)->dynindx = i;
7013
7014 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
7015 if (strindex == (bfd_size_type) -1)
7016 return false;
7017
7018 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
7019 }
7020 }
7021 else
7022 {
7023 c = bfd_count_sections (output_bfd);
7024 elf_link_hash_traverse (elf_hash_table (info),
7025 mips_elf_adjust_dynindx,
7026 (PTR) &c);
7027 elf_hash_table (info)->dynsymcount += c;
7028
7029 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
7030 {
7031 elf_section_data (s)->dynindx = i;
7032 /* These symbols will have no names, so we don't need to
7033 fiddle with dynstr_index. */
7034 }
7035 }
7036 }
7037
7038 if (sgot != NULL)
7039 {
7040 BFD_ASSERT (elf_section_data (sgot) != NULL);
7041 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7042 BFD_ASSERT (g != NULL);
7043
7044 /* If there are no global got symbols, fake the last symbol so
7045 for safety. */
7046 if (g->global_gotsym)
7047 g->global_gotsym += c;
7048 else
7049 g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
7050 }
7051 }
7052
7053 return true;
7054 }
7055
7056 /* Increment the index of a dynamic symbol by a given amount. Called
7057 via elf_link_hash_traverse. */
7058
7059 static boolean
7060 mips_elf_adjust_dynindx (h, cparg)
7061 struct elf_link_hash_entry *h;
7062 PTR cparg;
7063 {
7064 unsigned int *cp = (unsigned int *) cparg;
7065
7066 if (h->dynindx != -1)
7067 h->dynindx += *cp;
7068 return true;
7069 }
7070
7071 /* Finish up dynamic symbol handling. We set the contents of various
7072 dynamic sections here. */
7073
7074 static boolean
7075 mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
7076 bfd *output_bfd;
7077 struct bfd_link_info *info;
7078 struct elf_link_hash_entry *h;
7079 Elf_Internal_Sym *sym;
7080 {
7081 bfd *dynobj;
7082 bfd_vma gval;
7083 asection *sgot;
7084 struct mips_got_info *g;
7085 const char *name;
7086
7087 dynobj = elf_hash_table (info)->dynobj;
7088 gval = sym->st_value;
7089
7090 if (h->plt.offset != (bfd_vma) -1)
7091 {
7092 asection *s;
7093 bfd_byte *p;
7094 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7095
7096 /* This symbol has a stub. Set it up. */
7097
7098 BFD_ASSERT (h->dynindx != -1);
7099
7100 s = bfd_get_section_by_name (dynobj, ".stub");
7101 BFD_ASSERT (s != NULL);
7102
7103 /* Fill the stub. */
7104 p = stub;
7105 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
7106 p += 4;
7107 bfd_put_32 (output_bfd, STUB_MOVE, p);
7108 p += 4;
7109
7110 /* FIXME: Can h->dynindex be more than 64K? */
7111 if (h->dynindx & 0xffff0000)
7112 return false;
7113
7114 bfd_put_32 (output_bfd, STUB_JALR, p);
7115 p += 4;
7116 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
7117
7118 BFD_ASSERT (h->plt.offset <= s->_raw_size);
7119 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7120
7121 /* Mark the symbol as undefined. plt.offset != -1 occurs
7122 only for the referenced symbol. */
7123 sym->st_shndx = SHN_UNDEF;
7124
7125 /* The run-time linker uses the st_value field of the symbol
7126 to reset the global offset table entry for this external
7127 to its stub address when unlinking a shared object. */
7128 gval = s->output_section->vma + s->output_offset + h->plt.offset;
7129 sym->st_value = gval;
7130 }
7131
7132 BFD_ASSERT (h->dynindx != -1);
7133
7134 sgot = bfd_get_section_by_name (dynobj, ".got");
7135 BFD_ASSERT (sgot != NULL);
7136 BFD_ASSERT (elf_section_data (sgot) != NULL);
7137 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7138 BFD_ASSERT (g != NULL);
7139
7140 if ((unsigned long) h->dynindx >= g->global_gotsym)
7141 {
7142 bfd_size_type offset;
7143
7144 /* This symbol has an entry in the global offset table. Set its
7145 value to the corresponding got entry, if needed. */
7146 if (h->got.offset == (bfd_vma) -1)
7147 {
7148 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
7149 BFD_ASSERT (g->local_gotno * 4 <= offset
7150 && offset < sgot->_raw_size);
7151 bfd_put_32 (output_bfd, gval, sgot->contents + offset);
7152 }
7153 }
7154
7155 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7156 name = h->root.root.string;
7157 if (strcmp (name, "_DYNAMIC") == 0
7158 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
7159 sym->st_shndx = SHN_ABS;
7160 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
7161 {
7162 sym->st_shndx = SHN_ABS;
7163 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7164 sym->st_value = 1;
7165 }
7166 else if (SGI_COMPAT (output_bfd))
7167 {
7168 if (strcmp (name, "_gp_disp") == 0)
7169 {
7170 sym->st_shndx = SHN_ABS;
7171 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7172 sym->st_value = elf_gp (output_bfd);
7173 }
7174 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7175 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7176 {
7177 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7178 sym->st_other = STO_PROTECTED;
7179 sym->st_value = 0;
7180 sym->st_shndx = SHN_MIPS_DATA;
7181 }
7182 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7183 {
7184 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7185 sym->st_other = STO_PROTECTED;
7186 sym->st_value = mips_elf_hash_table (info)->procedure_count;
7187 sym->st_shndx = SHN_ABS;
7188 }
7189 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7190 {
7191 if (h->type == STT_FUNC)
7192 sym->st_shndx = SHN_MIPS_TEXT;
7193 else if (h->type == STT_OBJECT)
7194 sym->st_shndx = SHN_MIPS_DATA;
7195 }
7196 }
7197
7198 if (SGI_COMPAT (output_bfd)
7199 && ! info->shared)
7200 {
7201 if (! mips_elf_hash_table (info)->use_rld_obj_head
7202 && strcmp (name, "__rld_map") == 0)
7203 {
7204 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7205 BFD_ASSERT (s != NULL);
7206 sym->st_value = s->output_section->vma + s->output_offset;
7207 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
7208 if (mips_elf_hash_table (info)->rld_value == 0)
7209 mips_elf_hash_table (info)->rld_value = sym->st_value;
7210 }
7211 else if (mips_elf_hash_table (info)->use_rld_obj_head
7212 && strcmp (name, "__rld_obj_head") == 0)
7213 {
7214 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7215 BFD_ASSERT (s != NULL);
7216 mips_elf_hash_table (info)->rld_value = sym->st_value;
7217 }
7218 }
7219
7220 /* If this is a mips16 symbol, force the value to be even. */
7221 if (sym->st_other == STO_MIPS16
7222 && (sym->st_value & 1) != 0)
7223 --sym->st_value;
7224
7225 return true;
7226 }
7227
7228 /* Finish up the dynamic sections. */
7229
7230 static boolean
7231 mips_elf_finish_dynamic_sections (output_bfd, info)
7232 bfd *output_bfd;
7233 struct bfd_link_info *info;
7234 {
7235 bfd *dynobj;
7236 asection *sdyn;
7237 asection *sgot;
7238 struct mips_got_info *g;
7239
7240 dynobj = elf_hash_table (info)->dynobj;
7241
7242 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7243
7244 sgot = bfd_get_section_by_name (dynobj, ".got");
7245 if (sgot == NULL)
7246 g = NULL;
7247 else
7248 {
7249 BFD_ASSERT (elf_section_data (sgot) != NULL);
7250 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7251 BFD_ASSERT (g != NULL);
7252 }
7253
7254 if (elf_hash_table (info)->dynamic_sections_created)
7255 {
7256 Elf32_External_Dyn *dyncon, *dynconend;
7257
7258 BFD_ASSERT (sdyn != NULL);
7259 BFD_ASSERT (g != NULL);
7260
7261 dyncon = (Elf32_External_Dyn *) sdyn->contents;
7262 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
7263 for (; dyncon < dynconend; dyncon++)
7264 {
7265 Elf_Internal_Dyn dyn;
7266 const char *name;
7267 size_t elemsize;
7268 asection *s;
7269
7270 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
7271
7272 switch (dyn.d_tag)
7273 {
7274 default:
7275 break;
7276
7277 case DT_RELENT:
7278 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7279 BFD_ASSERT (s != NULL);
7280 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
7281 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7282 break;
7283
7284 case DT_STRSZ:
7285 /* Rewrite DT_STRSZ. */
7286 dyn.d_un.d_val =
7287 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
7288 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7289 break;
7290
7291 case DT_PLTGOT:
7292 name = ".got";
7293 goto get_vma;
7294 case DT_MIPS_CONFLICT:
7295 name = ".conflict";
7296 goto get_vma;
7297 case DT_MIPS_LIBLIST:
7298 name = ".liblist";
7299 get_vma:
7300 s = bfd_get_section_by_name (output_bfd, name);
7301 BFD_ASSERT (s != NULL);
7302 dyn.d_un.d_ptr = s->vma;
7303 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7304 break;
7305
7306 case DT_MIPS_RLD_VERSION:
7307 dyn.d_un.d_val = 1; /* XXX */
7308 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7309 break;
7310
7311 case DT_MIPS_FLAGS:
7312 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
7313 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7314 break;
7315
7316 case DT_MIPS_CONFLICTNO:
7317 name = ".conflict";
7318 elemsize = sizeof (Elf32_Conflict);
7319 goto set_elemno;
7320
7321 case DT_MIPS_LIBLISTNO:
7322 name = ".liblist";
7323 elemsize = sizeof (Elf32_Lib);
7324 set_elemno:
7325 s = bfd_get_section_by_name (output_bfd, name);
7326 if (s != NULL)
7327 {
7328 if (s->_cooked_size != 0)
7329 dyn.d_un.d_val = s->_cooked_size / elemsize;
7330 else
7331 dyn.d_un.d_val = s->_raw_size / elemsize;
7332 }
7333 else
7334 dyn.d_un.d_val = 0;
7335
7336 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7337 break;
7338
7339 case DT_MIPS_TIME_STAMP:
7340 time ((time_t *) &dyn.d_un.d_val);
7341 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7342 break;
7343
7344 case DT_MIPS_ICHECKSUM:
7345 /* XXX FIXME: */
7346 break;
7347
7348 case DT_MIPS_IVERSION:
7349 /* XXX FIXME: */
7350 break;
7351
7352 case DT_MIPS_BASE_ADDRESS:
7353 s = output_bfd->sections;
7354 BFD_ASSERT (s != NULL);
7355 dyn.d_un.d_ptr = s->vma & ~(0xffff);
7356 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7357 break;
7358
7359 case DT_MIPS_LOCAL_GOTNO:
7360 dyn.d_un.d_val = g->local_gotno;
7361 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7362 break;
7363
7364 case DT_MIPS_SYMTABNO:
7365 name = ".dynsym";
7366 elemsize = sizeof (Elf32_External_Sym);
7367 s = bfd_get_section_by_name (output_bfd, name);
7368 BFD_ASSERT (s != NULL);
7369
7370 if (s->_cooked_size != 0)
7371 dyn.d_un.d_val = s->_cooked_size / elemsize;
7372 else
7373 dyn.d_un.d_val = s->_raw_size / elemsize;
7374 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7375 break;
7376
7377 case DT_MIPS_UNREFEXTNO:
7378 /* XXX FIXME: */
7379 dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
7380 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7381 break;
7382
7383 case DT_MIPS_GOTSYM:
7384 dyn.d_un.d_val = g->global_gotsym;
7385 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7386 break;
7387
7388 case DT_MIPS_HIPAGENO:
7389 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
7390 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7391 break;
7392
7393 case DT_MIPS_RLD_MAP:
7394 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
7395 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7396 break;
7397
7398 }
7399 }
7400 }
7401
7402 /* The first entry of the global offset table will be filled at
7403 runtime. The second entry will be used by some runtime loaders.
7404 This isn't the case of Irix rld. */
7405 if (sgot != NULL && sgot->_raw_size > 0)
7406 {
7407 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7408 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
7409 }
7410
7411 if (sgot != NULL)
7412 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7413
7414 {
7415 asection *sdynsym;
7416 asection *s;
7417 unsigned int i;
7418 bfd_vma last;
7419 Elf_Internal_Sym sym;
7420 long dindx;
7421 const char *name;
7422 const char * const * namep = mips_elf_dynsym_sec_names;
7423 Elf32_compact_rel cpt;
7424
7425 /* Set up the section symbols for the output sections. SGI sets
7426 the STT_NOTYPE attribute for these symbols. Should we do so? */
7427
7428 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
7429 if (sdynsym != NULL)
7430 {
7431 if (SGI_COMPAT (output_bfd))
7432 {
7433 sym.st_size = 0;
7434 sym.st_name = 0;
7435 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7436 sym.st_other = 0;
7437
7438 i = 0;
7439 last = 0;
7440 dindx = 0;
7441 while ((name = *namep++) != NULL)
7442 {
7443 s = bfd_get_section_by_name (output_bfd, name);
7444 if (s != NULL)
7445 {
7446 sym.st_value = s->vma;
7447 dindx = elf_section_data (s)->dynindx;
7448 last = s->vma + s->_raw_size;
7449 }
7450 else
7451 {
7452 sym.st_value = last;
7453 dindx++;
7454 }
7455
7456 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
7457 ? SHN_MIPS_TEXT
7458 : SHN_MIPS_DATA);
7459 ++i;
7460 sym.st_name =
7461 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
7462
7463 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7464 (((Elf32_External_Sym *)
7465 sdynsym->contents)
7466 + dindx));
7467 }
7468
7469 /* Set the sh_info field of the output .dynsym section to
7470 the index of the first global symbol. */
7471 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7472 SIZEOF_MIPS_DYNSYM_SECNAMES;
7473 }
7474 else
7475 {
7476 sym.st_size = 0;
7477 sym.st_name = 0;
7478 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7479 sym.st_other = 0;
7480
7481 for (s = output_bfd->sections; s != NULL; s = s->next)
7482 {
7483 int indx;
7484
7485 sym.st_value = s->vma;
7486
7487 indx = elf_section_data (s)->this_idx;
7488 BFD_ASSERT (indx > 0);
7489 sym.st_shndx = indx;
7490
7491 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7492 (((Elf32_External_Sym *)
7493 sdynsym->contents)
7494 + elf_section_data (s)->dynindx));
7495 }
7496
7497 /* Set the sh_info field of the output .dynsym section to
7498 the index of the first global symbol. */
7499 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7500 bfd_count_sections (output_bfd) + 1;
7501 }
7502 }
7503
7504 if (SGI_COMPAT (output_bfd))
7505 {
7506 /* Write .compact_rel section out. */
7507 s = bfd_get_section_by_name (dynobj, ".compact_rel");
7508 if (s != NULL)
7509 {
7510 cpt.id1 = 1;
7511 cpt.num = s->reloc_count;
7512 cpt.id2 = 2;
7513 cpt.offset = (s->output_section->filepos
7514 + sizeof (Elf32_External_compact_rel));
7515 cpt.reserved0 = 0;
7516 cpt.reserved1 = 0;
7517 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7518 ((Elf32_External_compact_rel *)
7519 s->contents));
7520
7521 /* Clean up a dummy stub function entry in .text. */
7522 s = bfd_get_section_by_name (dynobj, ".stub");
7523 if (s != NULL)
7524 {
7525 file_ptr dummy_offset;
7526
7527 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
7528 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
7529 memset (s->contents + dummy_offset, 0,
7530 MIPS_FUNCTION_STUB_SIZE);
7531 }
7532 }
7533 }
7534
7535 /* Clean up a first relocation in .rel.dyn. */
7536 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7537 if (s != NULL && s->_raw_size > 0)
7538 memset (s->contents, 0, sizeof (Elf32_External_Rel));
7539 }
7540
7541 return true;
7542 }
7543 \f
7544 /* This is almost identical to bfd_generic_get_... except that some
7545 MIPS relocations need to be handled specially. Sigh. */
7546
7547 static bfd_byte *
7548 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
7549 relocateable, symbols)
7550 bfd *abfd;
7551 struct bfd_link_info *link_info;
7552 struct bfd_link_order *link_order;
7553 bfd_byte *data;
7554 boolean relocateable;
7555 asymbol **symbols;
7556 {
7557 /* Get enough memory to hold the stuff */
7558 bfd *input_bfd = link_order->u.indirect.section->owner;
7559 asection *input_section = link_order->u.indirect.section;
7560
7561 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
7562 arelent **reloc_vector = NULL;
7563 long reloc_count;
7564
7565 if (reloc_size < 0)
7566 goto error_return;
7567
7568 reloc_vector = (arelent **) bfd_malloc (reloc_size);
7569 if (reloc_vector == NULL && reloc_size != 0)
7570 goto error_return;
7571
7572 /* read in the section */
7573 if (!bfd_get_section_contents (input_bfd,
7574 input_section,
7575 (PTR) data,
7576 0,
7577 input_section->_raw_size))
7578 goto error_return;
7579
7580 /* We're not relaxing the section, so just copy the size info */
7581 input_section->_cooked_size = input_section->_raw_size;
7582 input_section->reloc_done = true;
7583
7584 reloc_count = bfd_canonicalize_reloc (input_bfd,
7585 input_section,
7586 reloc_vector,
7587 symbols);
7588 if (reloc_count < 0)
7589 goto error_return;
7590
7591 if (reloc_count > 0)
7592 {
7593 arelent **parent;
7594 /* for mips */
7595 int gp_found;
7596 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
7597
7598 {
7599 struct bfd_hash_entry *h;
7600 struct bfd_link_hash_entry *lh;
7601 /* Skip all this stuff if we aren't mixing formats. */
7602 if (abfd && input_bfd
7603 && abfd->xvec == input_bfd->xvec)
7604 lh = 0;
7605 else
7606 {
7607 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
7608 lh = (struct bfd_link_hash_entry *) h;
7609 }
7610 lookup:
7611 if (lh)
7612 {
7613 switch (lh->type)
7614 {
7615 case bfd_link_hash_undefined:
7616 case bfd_link_hash_undefweak:
7617 case bfd_link_hash_common:
7618 gp_found = 0;
7619 break;
7620 case bfd_link_hash_defined:
7621 case bfd_link_hash_defweak:
7622 gp_found = 1;
7623 gp = lh->u.def.value;
7624 break;
7625 case bfd_link_hash_indirect:
7626 case bfd_link_hash_warning:
7627 lh = lh->u.i.link;
7628 /* @@FIXME ignoring warning for now */
7629 goto lookup;
7630 case bfd_link_hash_new:
7631 default:
7632 abort ();
7633 }
7634 }
7635 else
7636 gp_found = 0;
7637 }
7638 /* end mips */
7639 for (parent = reloc_vector; *parent != (arelent *) NULL;
7640 parent++)
7641 {
7642 char *error_message = (char *) NULL;
7643 bfd_reloc_status_type r;
7644
7645 /* Specific to MIPS: Deal with relocation types that require
7646 knowing the gp of the output bfd. */
7647 asymbol *sym = *(*parent)->sym_ptr_ptr;
7648 if (bfd_is_abs_section (sym->section) && abfd)
7649 {
7650 /* The special_function wouldn't get called anyways. */
7651 }
7652 else if (!gp_found)
7653 {
7654 /* The gp isn't there; let the special function code
7655 fall over on its own. */
7656 }
7657 else if ((*parent)->howto->special_function
7658 == _bfd_mips_elf_gprel16_reloc)
7659 {
7660 /* bypass special_function call */
7661 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
7662 relocateable, (PTR) data, gp);
7663 goto skip_bfd_perform_relocation;
7664 }
7665 /* end mips specific stuff */
7666
7667 r = bfd_perform_relocation (input_bfd,
7668 *parent,
7669 (PTR) data,
7670 input_section,
7671 relocateable ? abfd : (bfd *) NULL,
7672 &error_message);
7673 skip_bfd_perform_relocation:
7674
7675 if (relocateable)
7676 {
7677 asection *os = input_section->output_section;
7678
7679 /* A partial link, so keep the relocs */
7680 os->orelocation[os->reloc_count] = *parent;
7681 os->reloc_count++;
7682 }
7683
7684 if (r != bfd_reloc_ok)
7685 {
7686 switch (r)
7687 {
7688 case bfd_reloc_undefined:
7689 if (!((*link_info->callbacks->undefined_symbol)
7690 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7691 input_bfd, input_section, (*parent)->address)))
7692 goto error_return;
7693 break;
7694 case bfd_reloc_dangerous:
7695 BFD_ASSERT (error_message != (char *) NULL);
7696 if (!((*link_info->callbacks->reloc_dangerous)
7697 (link_info, error_message, input_bfd, input_section,
7698 (*parent)->address)))
7699 goto error_return;
7700 break;
7701 case bfd_reloc_overflow:
7702 if (!((*link_info->callbacks->reloc_overflow)
7703 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7704 (*parent)->howto->name, (*parent)->addend,
7705 input_bfd, input_section, (*parent)->address)))
7706 goto error_return;
7707 break;
7708 case bfd_reloc_outofrange:
7709 default:
7710 abort ();
7711 break;
7712 }
7713
7714 }
7715 }
7716 }
7717 if (reloc_vector != NULL)
7718 free (reloc_vector);
7719 return data;
7720
7721 error_return:
7722 if (reloc_vector != NULL)
7723 free (reloc_vector);
7724 return NULL;
7725 }
7726 #define bfd_elf32_bfd_get_relocated_section_contents \
7727 elf32_mips_get_relocated_section_contents
7728 \f
7729 /* ECOFF swapping routines. These are used when dealing with the
7730 .mdebug section, which is in the ECOFF debugging format. */
7731 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
7732 {
7733 /* Symbol table magic number. */
7734 magicSym,
7735 /* Alignment of debugging information. E.g., 4. */
7736 4,
7737 /* Sizes of external symbolic information. */
7738 sizeof (struct hdr_ext),
7739 sizeof (struct dnr_ext),
7740 sizeof (struct pdr_ext),
7741 sizeof (struct sym_ext),
7742 sizeof (struct opt_ext),
7743 sizeof (struct fdr_ext),
7744 sizeof (struct rfd_ext),
7745 sizeof (struct ext_ext),
7746 /* Functions to swap in external symbolic data. */
7747 ecoff_swap_hdr_in,
7748 ecoff_swap_dnr_in,
7749 ecoff_swap_pdr_in,
7750 ecoff_swap_sym_in,
7751 ecoff_swap_opt_in,
7752 ecoff_swap_fdr_in,
7753 ecoff_swap_rfd_in,
7754 ecoff_swap_ext_in,
7755 _bfd_ecoff_swap_tir_in,
7756 _bfd_ecoff_swap_rndx_in,
7757 /* Functions to swap out external symbolic data. */
7758 ecoff_swap_hdr_out,
7759 ecoff_swap_dnr_out,
7760 ecoff_swap_pdr_out,
7761 ecoff_swap_sym_out,
7762 ecoff_swap_opt_out,
7763 ecoff_swap_fdr_out,
7764 ecoff_swap_rfd_out,
7765 ecoff_swap_ext_out,
7766 _bfd_ecoff_swap_tir_out,
7767 _bfd_ecoff_swap_rndx_out,
7768 /* Function to read in symbolic data. */
7769 _bfd_mips_elf_read_ecoff_info
7770 };
7771 \f
7772 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
7773 #define TARGET_LITTLE_NAME "elf32-littlemips"
7774 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
7775 #define TARGET_BIG_NAME "elf32-bigmips"
7776 #define ELF_ARCH bfd_arch_mips
7777 #define ELF_MACHINE_CODE EM_MIPS
7778
7779 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
7780 a value of 0x1000, and we are compatible. */
7781 #define ELF_MAXPAGESIZE 0x1000
7782
7783 #define elf_backend_collect true
7784 #define elf_backend_type_change_ok true
7785 #define elf_backend_can_gc_sections true
7786 #define elf_info_to_howto 0
7787 #define elf_info_to_howto_rel mips_info_to_howto_rel
7788 #define elf_backend_sym_is_global mips_elf_sym_is_global
7789 #define elf_backend_object_p mips_elf32_object_p
7790 #define elf_backend_section_from_shdr mips_elf32_section_from_shdr
7791 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
7792 #define elf_backend_section_from_bfd_section \
7793 _bfd_mips_elf_section_from_bfd_section
7794 #define elf_backend_section_processing mips_elf32_section_processing
7795 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
7796 #define elf_backend_additional_program_headers \
7797 mips_elf_additional_program_headers
7798 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
7799 #define elf_backend_final_write_processing \
7800 _bfd_mips_elf_final_write_processing
7801 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
7802
7803 #define bfd_elf32_bfd_is_local_label_name \
7804 mips_elf_is_local_label_name
7805 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
7806 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
7807 #define bfd_elf32_bfd_link_hash_table_create \
7808 mips_elf_link_hash_table_create
7809 #define bfd_elf32_bfd_final_link mips_elf_final_link
7810 #define bfd_elf32_bfd_copy_private_bfd_data \
7811 _bfd_mips_elf_copy_private_bfd_data
7812 #define bfd_elf32_bfd_merge_private_bfd_data \
7813 _bfd_mips_elf_merge_private_bfd_data
7814 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
7815 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
7816 #define elf_backend_create_dynamic_sections \
7817 mips_elf_create_dynamic_sections
7818 #define elf_backend_check_relocs mips_elf_check_relocs
7819 #define elf_backend_adjust_dynamic_symbol \
7820 mips_elf_adjust_dynamic_symbol
7821 #define elf_backend_always_size_sections \
7822 mips_elf_always_size_sections
7823 #define elf_backend_size_dynamic_sections \
7824 mips_elf_size_dynamic_sections
7825 #define elf_backend_relocate_section mips_elf_relocate_section
7826 #define elf_backend_link_output_symbol_hook \
7827 mips_elf_link_output_symbol_hook
7828 #define elf_backend_finish_dynamic_symbol \
7829 mips_elf_finish_dynamic_symbol
7830 #define elf_backend_finish_dynamic_sections \
7831 mips_elf_finish_dynamic_sections
7832 #define elf_backend_gc_mark_hook mips_elf_gc_mark_hook
7833 #define elf_backend_gc_sweep_hook mips_elf_gc_sweep_hook
7834
7835 #include "elf32-target.h"