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