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