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