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