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