]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf32-mips.c
* elf32-mips.c: Add dvp support.
[thirdparty/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 false); /* 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 && r_type != R_MIPS16_26
4633 && r_type != R_MIPS16_GPREL)
4634 {
4635 bfd_set_error (bfd_error_bad_value);
4636 return false;
4637 }
4638 if (r_type == R_MIPS16_26)
4639 howto = &elf_mips16_jump_howto;
4640 else if (r_type == R_MIPS16_GPREL)
4641 howto = &elf_mips16_gprel_howto;
4642 else
4643 howto = elf_mips_howto_table + r_type;
4644
4645 if (dynobj != NULL
4646 && (r_type == R_MIPS_CALL16
4647 || r_type == R_MIPS_GOT16
4648 || r_type == R_MIPS_CALL_HI16
4649 || r_type == R_MIPS_CALL_LO16
4650 || r_type == R_MIPS_GOT_HI16
4651 || r_type == R_MIPS_GOT_LO16))
4652 {
4653 /* We need the .got section. */
4654 if (sgot == NULL)
4655 {
4656 sgot = bfd_get_section_by_name (dynobj, ".got");
4657 BFD_ASSERT (sgot != NULL);
4658 BFD_ASSERT (elf_section_data (sgot) != NULL);
4659 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4660 BFD_ASSERT (g != NULL);
4661 }
4662 }
4663
4664 r_symndx = ELF32_R_SYM (rel->r_info);
4665
4666 /* Mix in the change in GP address for a GP relative reloc. */
4667 if (r_type != R_MIPS_GPREL16
4668 && r_type != R_MIPS_LITERAL
4669 && r_type != R_MIPS_GPREL32
4670 && r_type != R_MIPS16_GPREL)
4671 addend = 0;
4672 else
4673 {
4674 if (gp == 0)
4675 {
4676 if (! ((*info->callbacks->reloc_dangerous)
4677 (info,
4678 "GP relative relocation when GP not defined",
4679 input_bfd, input_section,
4680 rel->r_offset)))
4681 return false;
4682 /* Only give the error once per link. */
4683 gp = 4;
4684 _bfd_set_gp_value (output_bfd, gp);
4685 }
4686
4687 if (r_symndx < extsymoff
4688 || (elf_bad_symtab (input_bfd)
4689 && local_sections[r_symndx] != NULL))
4690 {
4691 /* This is a relocation against a section. The current
4692 addend in the instruction is the difference between
4693 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
4694 must change this to be the difference between the
4695 final definition (which will end up in RELOCATION)
4696 and the GP value of OUTPUT_BFD (which is in GP). */
4697 addend = elf_gp (input_bfd) - gp;
4698 }
4699 else if (! info->relocateable)
4700 {
4701 /* We are doing a final link. The current addend in the
4702 instruction is simply the desired offset into the
4703 symbol (normally zero). We want the instruction to
4704 hold the difference between the final definition of
4705 the symbol (which will end up in RELOCATION) and the
4706 GP value of OUTPUT_BFD (which is in GP). */
4707 addend = - gp;
4708 }
4709 else
4710 {
4711 /* We are generating relocateable output, and we aren't
4712 going to define this symbol, so we just leave the
4713 instruction alone. */
4714 addend = 0;
4715 }
4716 }
4717
4718 h = NULL;
4719 sym = NULL;
4720 sec = NULL;
4721 if (info->relocateable)
4722 {
4723 /* This is a relocateable link. We don't have to change
4724 anything, unless the reloc is against a section symbol,
4725 in which case we have to adjust according to where the
4726 section symbol winds up in the output section. */
4727 if (r_symndx >= locsymcount
4728 || (elf_bad_symtab (input_bfd)
4729 && local_sections[r_symndx] == NULL))
4730 r = bfd_reloc_ok;
4731 else
4732 {
4733 sym = local_syms + r_symndx;
4734 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4735 r = bfd_reloc_ok;
4736 else
4737 {
4738 sec = local_sections[r_symndx];
4739
4740 /* It would be logical to add sym->st_value here,
4741 but Irix 5 sometimes generates a garbage symbol
4742 value. */
4743 addend += sec->output_offset;
4744
4745 /* If this is HI16 or GOT16 with an associated LO16,
4746 adjust the addend accordingly. Otherwise, just
4747 relocate. */
4748 if (r_type == R_MIPS_64 && bfd_big_endian (input_bfd))
4749 r = _bfd_relocate_contents (howto, input_bfd,
4750 addend,
4751 contents + rel->r_offset + 4);
4752 else if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
4753 r = _bfd_relocate_contents (howto, input_bfd,
4754 addend,
4755 contents + rel->r_offset);
4756 else
4757 {
4758 Elf_Internal_Rela *lorel;
4759
4760 /* As a GNU extension, permit an arbitrary
4761 number of R_MIPS_HI16 relocs before the
4762 R_MIPS_LO16 reloc. This permits gcc to emit
4763 the HI and LO relocs itself. */
4764 if (r_type == R_MIPS_GOT16)
4765 lorel = rel + 1;
4766 else
4767 {
4768 for (lorel = rel + 1;
4769 (lorel < relend
4770 && (ELF32_R_TYPE (lorel->r_info)
4771 == R_MIPS_HI16));
4772 lorel++)
4773 ;
4774 }
4775 if (lorel < relend
4776 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
4777 {
4778 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
4779 contents, addend);
4780 r = bfd_reloc_ok;
4781 }
4782 else
4783 r = _bfd_relocate_contents (howto, input_bfd,
4784 addend,
4785 contents + rel->r_offset);
4786 }
4787 }
4788 }
4789 }
4790 else
4791 {
4792 bfd_vma relocation;
4793 boolean local;
4794
4795 /* This is a final link. */
4796 sym = NULL;
4797 if (r_symndx < extsymoff
4798 || (elf_bad_symtab (input_bfd)
4799 && local_sections[r_symndx] != NULL))
4800 {
4801 local = true;
4802 sym = local_syms + r_symndx;
4803 sec = local_sections[r_symndx];
4804 relocation = (sec->output_section->vma
4805 + sec->output_offset);
4806
4807 /* It would be logical to always add sym->st_value here,
4808 but Irix 5 sometimes generates a garbage symbol
4809 value. */
4810 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4811 relocation += sym->st_value;
4812
4813 /* mips16 text labels should be treated as odd. */
4814 if (sym->st_other == STO_MIPS16)
4815 ++relocation;
4816 }
4817 else
4818 {
4819 long indx;
4820
4821 local = false;
4822 indx = r_symndx - extsymoff;
4823 h = elf_sym_hashes (input_bfd)[indx];
4824 while (h->root.type == bfd_link_hash_indirect
4825 || h->root.type == bfd_link_hash_warning)
4826 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4827 if (strcmp (h->root.root.string, "_gp_disp") == 0)
4828 {
4829 if (gp == 0)
4830 {
4831 if (! ((*info->callbacks->reloc_dangerous)
4832 (info,
4833 "_gp_disp used when GP not defined",
4834 input_bfd, input_section,
4835 rel->r_offset)))
4836 return false;
4837 /* Only give the error once per link. */
4838 gp = 4;
4839 _bfd_set_gp_value (output_bfd, gp);
4840 relocation = 0;
4841 }
4842 else
4843 {
4844 sec = input_section;
4845 if (sec->output_section != NULL)
4846 relocation = (gp
4847 - (rel->r_offset
4848 + sec->output_section->vma
4849 + sec->output_offset));
4850 else
4851 relocation = gp - rel->r_offset;
4852 if (r_type == R_MIPS_LO16)
4853 relocation += 4;
4854 }
4855 }
4856 else if (h->root.type == bfd_link_hash_defined
4857 || h->root.type == bfd_link_hash_defweak)
4858 {
4859 sec = h->root.u.def.section;
4860 if (sec->output_section == NULL)
4861 relocation = 0;
4862 else
4863 relocation = (h->root.u.def.value
4864 + sec->output_section->vma
4865 + sec->output_offset);
4866 }
4867 else if (h->root.type == bfd_link_hash_undefweak)
4868 relocation = 0;
4869 else if (info->shared && ! info->symbolic)
4870 relocation = 0;
4871 else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
4872 {
4873 /* If this is a dynamic link, we should have created
4874 a _DYNAMIC_LINK symbol in
4875 mips_elf_create_dynamic_sections. Otherwise, we
4876 should define the symbol with a value of 0.
4877 FIXME: It should probably get into the symbol
4878 table somehow as well. */
4879 BFD_ASSERT (! info->shared);
4880 BFD_ASSERT (bfd_get_section_by_name (output_bfd,
4881 ".dynamic") == NULL);
4882 relocation = 0;
4883 }
4884 else
4885 {
4886 if (! ((*info->callbacks->undefined_symbol)
4887 (info, h->root.root.string, input_bfd,
4888 input_section, rel->r_offset)))
4889 return false;
4890 relocation = 0;
4891 }
4892 }
4893
4894 mh = (struct mips_elf_link_hash_entry *) h;
4895 if (h != NULL)
4896 other = h->other;
4897 else if (sym != NULL)
4898 other = sym->st_other;
4899 else
4900 other = 0;
4901
4902 /* If this function has an fn_stub, then it is a mips16
4903 function which needs a stub if it is called by a 32 bit
4904 function. If this reloc is anything other than a 16 bit
4905 call, redirect the reloc to the stub. We don't redirect
4906 relocs from other stub functions. */
4907 if (r_type != R_MIPS16_26
4908 && ((mh != NULL
4909 && mh->fn_stub != NULL)
4910 || (mh == NULL
4911 && elf_tdata (input_bfd)->local_stubs != NULL
4912 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
4913 && strncmp (bfd_get_section_name (input_bfd, input_section),
4914 FN_STUB, sizeof FN_STUB - 1) != 0
4915 && strncmp (bfd_get_section_name (input_bfd, input_section),
4916 CALL_STUB, sizeof CALL_STUB - 1) != 0
4917 && strncmp (bfd_get_section_name (input_bfd, input_section),
4918 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) != 0)
4919 {
4920 if (mh != NULL)
4921 {
4922 BFD_ASSERT (mh->need_fn_stub);
4923 relocation = (mh->fn_stub->output_section->vma
4924 + mh->fn_stub->output_offset);
4925 }
4926 else
4927 {
4928 asection *fn_stub;
4929
4930 fn_stub = elf_tdata (input_bfd)->local_stubs[r_symndx];
4931 relocation = (fn_stub->output_section->vma
4932 + fn_stub->output_offset);
4933 }
4934
4935 /* RELOCATION now points to 32 bit code. */
4936 other = 0;
4937 }
4938
4939 /* If this function has a call_stub, then it is called by a
4940 mips16 function; the call needs to go through a stub if
4941 this function is a 32 bit function. If this reloc is a
4942 16 bit call, and the symbol is not a 16 bit function,
4943 then redirect the reloc to the stub. Note that we don't
4944 need to worry about calling the function through a
4945 function pointer; such calls are handled by routing
4946 through a special mips16 routine. We don't have to check
4947 whether this call is from a stub; it can't be, because a
4948 stub contains 32 bit code, and hence can not have a 16
4949 bit reloc. */
4950 if (r_type == R_MIPS16_26
4951 && mh != NULL
4952 && (mh->call_stub != NULL || mh->call_fp_stub != NULL)
4953 && other != STO_MIPS16)
4954 {
4955 asection *stub;
4956
4957 /* If both call_stub and call_fp_stub are defined, we
4958 can figure out which one to use by seeing which one
4959 appears in the input file. */
4960 if (mh->call_stub != NULL && mh->call_fp_stub != NULL)
4961 {
4962 asection *o;
4963
4964 stub = NULL;
4965 for (o = input_bfd->sections; o != NULL; o = o->next)
4966 {
4967 if (strncmp (bfd_get_section_name (input_bfd, o),
4968 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
4969 {
4970 stub = mh->call_fp_stub;
4971 break;
4972 }
4973 }
4974 if (stub == NULL)
4975 stub = mh->call_stub;
4976 }
4977 else if (mh->call_stub != NULL)
4978 stub = mh->call_stub;
4979 else
4980 stub = mh->call_fp_stub;
4981
4982 BFD_ASSERT (stub->_raw_size > 0);
4983 relocation = stub->output_section->vma + stub->output_offset;
4984 }
4985
4986 if (r_type == R_MIPS_HI16)
4987 {
4988 Elf_Internal_Rela *lorel;
4989
4990 /* As a GNU extension, permit an arbitrary number of
4991 R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
4992 This permits gcc to emit the HI and LO relocs itself. */
4993 for (lorel = rel + 1;
4994 (lorel < relend
4995 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
4996 lorel++)
4997 ;
4998 if (lorel < relend
4999 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5000 {
5001 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5002 contents, relocation + addend);
5003 r = bfd_reloc_ok;
5004 }
5005 else
5006 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5007 contents, rel->r_offset,
5008 relocation, addend);
5009 }
5010 else if (r_type == R_MIPS_GOT16 && local)
5011 {
5012 /* GOT16 must also have an associated LO16 in the local
5013 case. In this case, the addend is extracted and the
5014 section in which the referenced object is determined.
5015 Then the final address of the object is computed and
5016 the GOT entry for the hipage (an aligned 64kb chunk)
5017 is added to .got section if needed. The offset field
5018 of the GOT16-relocated instruction is replaced by the
5019 index of this GOT entry for the hipage. */
5020 if ((rel + 1) < relend
5021 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
5022 {
5023 if (! mips_elf_relocate_got_local (output_bfd, input_bfd,
5024 sgot, rel, rel + 1,
5025 contents,
5026 relocation + addend))
5027 return false;
5028 r = bfd_reloc_ok;
5029 }
5030 else
5031 r = bfd_reloc_outofrange;
5032 }
5033 else if (r_type == R_MIPS_CALL16
5034 || r_type == R_MIPS_GOT16
5035 || r_type == R_MIPS_CALL_LO16
5036 || r_type == R_MIPS_GOT_LO16)
5037 {
5038 bfd_vma offset;
5039
5040 /* This symbol must be registered as a global symbol
5041 having the corresponding got entry. */
5042 BFD_ASSERT (h->got_offset != (bfd_vma) -1);
5043
5044 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5045 BFD_ASSERT (g->local_gotno <= offset
5046 && offset < sgot->_raw_size);
5047 bfd_put_32 (output_bfd, relocation + addend,
5048 sgot->contents + offset);
5049 offset = (sgot->output_section->vma + sgot->output_offset
5050 + offset - gp);
5051 mips_elf_relocate_global_got (input_bfd, rel, contents,
5052 offset);
5053 r = bfd_reloc_ok;
5054 }
5055 else if (r_type == R_MIPS_CALL_HI16
5056 || r_type == R_MIPS_GOT_HI16)
5057 {
5058 bfd_vma offset;
5059
5060 /* This must be a global symbol with a got entry. The
5061 next reloc must be the corresponding LO16 reloc. */
5062 BFD_ASSERT (h != NULL && h->got_offset != (bfd_vma) -1);
5063 BFD_ASSERT ((rel + 1) < relend);
5064 BFD_ASSERT (ELF32_R_TYPE ((rel + 1)->r_info)
5065 == (r_type == R_MIPS_CALL_HI16
5066 ? R_MIPS_CALL_LO16
5067 : R_MIPS_GOT_LO16));
5068
5069 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5070 BFD_ASSERT (g->local_gotno <= offset
5071 && offset < sgot->_raw_size);
5072 bfd_put_32 (output_bfd, relocation + addend,
5073 sgot->contents + offset);
5074 offset = (sgot->output_section->vma + sgot->output_offset
5075 + offset - gp);
5076 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
5077 offset);
5078 r = bfd_reloc_ok;
5079 }
5080 else if (r_type == R_MIPS_REL32
5081 || r_type == R_MIPS_32)
5082 {
5083 Elf_Internal_Rel outrel;
5084 Elf32_crinfo cptrel;
5085 bfd_byte *cr;
5086
5087 if ((info->shared
5088 || (elf_hash_table (info)->dynamic_sections_created
5089 && h != NULL
5090 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
5091 == 0)))
5092 && (input_section->flags & SEC_ALLOC) != 0)
5093 {
5094 boolean skip;
5095
5096 /* When generating a shared object, these
5097 relocations are copied into the output file to be
5098 resolved at run time. */
5099 if (sreloc == NULL)
5100 {
5101 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
5102 BFD_ASSERT (sreloc != NULL);
5103 }
5104
5105 skip = false;
5106
5107 if (elf_section_data (input_section)->stab_info == NULL)
5108 outrel.r_offset = rel->r_offset;
5109 else
5110 {
5111 bfd_vma off;
5112
5113 off = (_bfd_stab_section_offset
5114 (output_bfd, &elf_hash_table (info)->stab_info,
5115 input_section,
5116 &elf_section_data (input_section)->stab_info,
5117 rel->r_offset));
5118 if (off == (bfd_vma) -1)
5119 skip = true;
5120 outrel.r_offset = off;
5121 }
5122
5123 outrel.r_offset += (input_section->output_section->vma
5124 + input_section->output_offset);
5125
5126 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
5127
5128 if (skip)
5129 memset (&outrel, 0, sizeof outrel);
5130 else if (h != NULL
5131 && (! info->symbolic
5132 || (h->elf_link_hash_flags
5133 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5134 {
5135 BFD_ASSERT (h->dynindx != -1);
5136 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
5137 sec = input_section;
5138 }
5139 else
5140 {
5141 long indx;
5142
5143 if (h == NULL)
5144 sec = local_sections[r_symndx];
5145 else
5146 {
5147 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5148 || (h->root.type
5149 == bfd_link_hash_defweak));
5150 sec = h->root.u.def.section;
5151 }
5152 if (sec != NULL && bfd_is_abs_section (sec))
5153 indx = 0;
5154 else if (sec == NULL || sec->owner == NULL)
5155 {
5156 bfd_set_error (bfd_error_bad_value);
5157 return false;
5158 }
5159 else
5160 {
5161 asection *osec;
5162
5163 osec = sec->output_section;
5164 indx = elf_section_data (osec)->dynindx;
5165 if (indx == 0)
5166 abort ();
5167 }
5168
5169 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5170 addend += relocation;
5171 }
5172
5173 if (! skip)
5174 bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
5175
5176 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5177 (((Elf32_External_Rel *)
5178 sreloc->contents)
5179 + sreloc->reloc_count));
5180 ++sreloc->reloc_count;
5181
5182 if (! skip && SGI_COMPAT (output_bfd))
5183 {
5184 if (scpt == NULL)
5185 continue;
5186
5187 /* Make an entry of compact relocation info. */
5188 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5189 cptrel.vaddr = (rel->r_offset
5190 + input_section->output_section->vma
5191 + input_section->output_offset);
5192 if (r_type == R_MIPS_REL32)
5193 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5194 else
5195 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5196 mips_elf_set_cr_dist2to (cptrel, 0);
5197 cptrel.konst = addend;
5198
5199 cr = (scpt->contents
5200 + sizeof (Elf32_External_compact_rel));
5201 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5202 ((Elf32_External_crinfo *) cr
5203 + scpt->reloc_count));
5204 ++scpt->reloc_count;
5205 }
5206
5207 /* This reloc will be computed at runtime, so
5208 there's no need to do anything now. */
5209 continue;
5210 }
5211 else
5212 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5213 contents, rel->r_offset,
5214 relocation, addend);
5215 }
5216 else if (r_type == R_MIPS_64)
5217 {
5218 bfd_size_type addr;
5219 unsigned long val;
5220
5221 /* Do a 32 bit relocation, and sign extend to 64 bits. */
5222 addr = rel->r_offset;
5223 if (bfd_big_endian (input_bfd))
5224 addr += 4;
5225 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5226 contents, addr, relocation,
5227 addend);
5228 val = bfd_get_32 (input_bfd, contents + addr);
5229 if ((val & 0x80000000) != 0)
5230 val = 0xffffffff;
5231 else
5232 val = 0;
5233 addr = rel->r_offset;
5234 if (bfd_little_endian (input_bfd))
5235 addr += 4;
5236 bfd_put_32 (input_bfd, val, contents + addr);
5237 }
5238 else if (r_type == R_MIPS_26 && other == STO_MIPS16)
5239 {
5240 unsigned long insn;
5241
5242 /* This is a jump to a mips16 routine from a mips32
5243 routine. We need to change jal into jalx. */
5244 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5245 if (((insn >> 26) & 0x3f) != 0x3
5246 && ((insn >> 26) & 0x3f) != 0x1d)
5247 {
5248 (*_bfd_error_handler)
5249 ("%s: %s+0x%lx: jump to mips16 routine which is not jal",
5250 bfd_get_filename (input_bfd),
5251 input_section->name,
5252 (unsigned long) rel->r_offset);
5253 bfd_set_error (bfd_error_bad_value);
5254 return false;
5255 }
5256 insn = (insn & 0x3ffffff) | (0x1d << 26);
5257 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5258 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5259 contents, rel->r_offset,
5260 relocation, addend);
5261 }
5262 else if (r_type == R_MIPS16_26)
5263 {
5264 /* It's easiest to do the normal relocation, and then
5265 dig out the instruction and swap the first word the
5266 way the mips16 expects it. If this is little endian,
5267 though, we need to swap the two words first, and then
5268 swap them back again later, so that the address looks
5269 right. */
5270
5271 if (bfd_little_endian (input_bfd))
5272 {
5273 unsigned long insn;
5274
5275 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5276 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5277 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5278 }
5279
5280 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5281 contents, rel->r_offset,
5282 relocation, addend);
5283 if (r == bfd_reloc_ok)
5284 {
5285 unsigned long insn;
5286
5287 if (bfd_little_endian (input_bfd))
5288 {
5289 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5290 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5291 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5292 }
5293
5294 insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
5295 insn = ((insn & 0xfc00)
5296 | ((insn & 0x1f) << 5)
5297 | ((insn & 0x3e0) >> 5));
5298 /* If this is a jump to a 32 bit routine, then make
5299 it jalx. */
5300 if (other != STO_MIPS16)
5301 insn |= 0x400;
5302 bfd_put_16 (input_bfd, insn, contents + rel->r_offset);
5303 }
5304 }
5305 else if (r_type == R_MIPS16_GPREL)
5306 {
5307 unsigned short extend, insn;
5308 bfd_byte buf[4];
5309 unsigned long final;
5310
5311 /* Extract the addend into buf, run the regular reloc,
5312 and stuff the resulting value back into the
5313 instructions. */
5314 if (rel->r_offset > input_section->_raw_size)
5315 r = bfd_reloc_outofrange;
5316 else
5317 {
5318 extend = bfd_get_16 (input_bfd, contents + rel->r_offset);
5319 insn = bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
5320 bfd_put_32 (input_bfd,
5321 (((extend & 0x1f) << 11)
5322 | (extend & 0x7e0)
5323 | (insn & 0x1f)),
5324 buf);
5325 r = _bfd_final_link_relocate (howto, input_bfd,
5326 input_section, buf,
5327 (bfd_vma) 0, relocation,
5328 addend);
5329 final = bfd_get_32 (input_bfd, buf);
5330 bfd_put_16 (input_bfd,
5331 ((extend & 0xf800)
5332 | ((final >> 11) & 0x1f)
5333 | (final & 0x7e0)),
5334 contents + rel->r_offset);
5335 bfd_put_16 (input_bfd,
5336 ((insn & 0xffe0)
5337 | (final & 0x1f)),
5338 contents + rel->r_offset + 2);
5339 }
5340 }
5341 else
5342 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5343 contents, rel->r_offset,
5344 relocation, addend);
5345
5346 /* The jal instruction can only jump to an address which is
5347 divisible by 4, and it can only jump to an address with
5348 the same upper 4 bits as the PC. */
5349 if (r == bfd_reloc_ok
5350 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26))
5351 {
5352 bfd_vma addr;
5353
5354 addr = relocation;
5355 if (other == STO_MIPS16)
5356 addr &= ~ (bfd_vma) 1;
5357 addr += addend;
5358 if ((addr & 3) != 0
5359 || ((addr & 0xf0000000)
5360 != ((input_section->output_section->vma
5361 + input_section->output_offset
5362 + rel->r_offset)
5363 & 0xf0000000)))
5364 r = bfd_reloc_overflow;
5365 }
5366
5367 if (SGI_COMPAT (abfd)
5368 && scpt != NULL
5369 && (input_section->flags & SEC_ALLOC) != 0)
5370 {
5371 Elf32_crinfo cptrel;
5372 bfd_byte *cr;
5373
5374 /* Make an entry of compact relocation info. */
5375 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5376 cptrel.vaddr = (rel->r_offset
5377 + input_section->output_section->vma
5378 + input_section->output_offset);
5379
5380 switch (r_type)
5381 {
5382 case R_MIPS_26:
5383 mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
5384 /* XXX How should we set dist2to in this case. */
5385 mips_elf_set_cr_dist2to (cptrel, 8);
5386 cptrel.konst = addend + relocation;
5387 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5388 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5389 ((Elf32_External_crinfo *) cr
5390 + scpt->reloc_count));
5391 ++scpt->reloc_count;
5392 break;
5393
5394 case R_MIPS_GPREL16:
5395 case R_MIPS_LITERAL:
5396 case R_MIPS_GPREL32:
5397 mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
5398 cptrel.konst = gp - cptrel.vaddr;
5399 mips_elf_set_cr_dist2to (cptrel, 4);
5400 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5401 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5402 ((Elf32_External_crinfo *) cr
5403 + scpt->reloc_count));
5404 ++scpt->reloc_count;
5405 break;
5406
5407 default:
5408 break;
5409 }
5410 }
5411 }
5412
5413 if (r != bfd_reloc_ok)
5414 {
5415 switch (r)
5416 {
5417 default:
5418 case bfd_reloc_outofrange:
5419 abort ();
5420 case bfd_reloc_overflow:
5421 {
5422 const char *name;
5423
5424 if (h != NULL)
5425 name = h->root.root.string;
5426 else
5427 {
5428 name = bfd_elf_string_from_elf_section (input_bfd,
5429 symtab_hdr->sh_link,
5430 sym->st_name);
5431 if (name == NULL)
5432 return false;
5433 if (*name == '\0')
5434 name = bfd_section_name (input_bfd, sec);
5435 }
5436 if (! ((*info->callbacks->reloc_overflow)
5437 (info, name, howto->name, (bfd_vma) 0,
5438 input_bfd, input_section, rel->r_offset)))
5439 return false;
5440 }
5441 break;
5442 }
5443 }
5444 }
5445
5446 return true;
5447 }
5448
5449 /* This hook function is called before the linker writes out a global
5450 symbol. We mark symbols as small common if appropriate. This is
5451 also where we undo the increment of the value for a mips16 symbol. */
5452
5453 /*ARGSIGNORED*/
5454 static boolean
5455 mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5456 bfd *abfd;
5457 struct bfd_link_info *info;
5458 const char *name;
5459 Elf_Internal_Sym *sym;
5460 asection *input_sec;
5461 {
5462 /* If we see a common symbol, which implies a relocatable link, then
5463 if a symbol was small common in an input file, mark it as small
5464 common in the output file. */
5465 if (sym->st_shndx == SHN_COMMON
5466 && strcmp (input_sec->name, ".scommon") == 0)
5467 sym->st_shndx = SHN_MIPS_SCOMMON;
5468
5469 if (sym->st_other == STO_MIPS16
5470 && (sym->st_value & 1) != 0)
5471 --sym->st_value;
5472
5473 return true;
5474 }
5475 \f
5476 /* Functions for the dynamic linker. */
5477
5478 /* The name of the dynamic interpreter. This is put in the .interp
5479 section. */
5480
5481 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
5482
5483 /* Create dynamic sections when linking against a dynamic object. */
5484
5485 static boolean
5486 mips_elf_create_dynamic_sections (abfd, info)
5487 bfd *abfd;
5488 struct bfd_link_info *info;
5489 {
5490 struct elf_link_hash_entry *h;
5491 flagword flags;
5492 register asection *s;
5493 const char * const *namep;
5494
5495 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5496 | SEC_LINKER_CREATED | SEC_READONLY);
5497
5498 /* Mips ABI requests the .dynamic section to be read only. */
5499 s = bfd_get_section_by_name (abfd, ".dynamic");
5500 if (s != NULL)
5501 {
5502 if (! bfd_set_section_flags (abfd, s, flags))
5503 return false;
5504 }
5505
5506 /* We need to create .got section. */
5507 if (! mips_elf_create_got_section (abfd, info))
5508 return false;
5509
5510 /* Create .stub section. */
5511 if (bfd_get_section_by_name (abfd, ".stub") == NULL)
5512 {
5513 s = bfd_make_section (abfd, ".stub");
5514 if (s == NULL
5515 || ! bfd_set_section_flags (abfd, s, flags)
5516 || ! bfd_set_section_alignment (abfd, s, 2))
5517 return false;
5518 }
5519
5520 if (SGI_COMPAT (abfd)
5521 && !info->shared
5522 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5523 {
5524 s = bfd_make_section (abfd, ".rld_map");
5525 if (s == NULL
5526 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
5527 || ! bfd_set_section_alignment (abfd, s, 2))
5528 return false;
5529 }
5530
5531 if (SGI_COMPAT (abfd))
5532 {
5533 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5534 {
5535 h = NULL;
5536 if (! (_bfd_generic_link_add_one_symbol
5537 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
5538 (bfd_vma) 0, (const char *) NULL, false,
5539 get_elf_backend_data (abfd)->collect,
5540 (struct bfd_link_hash_entry **) &h)))
5541 return false;
5542 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5543 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5544 h->type = STT_SECTION;
5545
5546 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5547 return false;
5548 }
5549
5550 /* We need to create a .compact_rel section. */
5551 if (! mips_elf_create_compact_rel_section (abfd, info))
5552 return false;
5553
5554 /* Change aligments of some sections. */
5555 s = bfd_get_section_by_name (abfd, ".hash");
5556 if (s != NULL)
5557 bfd_set_section_alignment (abfd, s, 4);
5558 s = bfd_get_section_by_name (abfd, ".dynsym");
5559 if (s != NULL)
5560 bfd_set_section_alignment (abfd, s, 4);
5561 s = bfd_get_section_by_name (abfd, ".dynstr");
5562 if (s != NULL)
5563 bfd_set_section_alignment (abfd, s, 4);
5564 s = bfd_get_section_by_name (abfd, ".reginfo");
5565 if (s != NULL)
5566 bfd_set_section_alignment (abfd, s, 4);
5567 s = bfd_get_section_by_name (abfd, ".dynamic");
5568 if (s != NULL)
5569 bfd_set_section_alignment (abfd, s, 4);
5570 }
5571
5572 if (!info->shared)
5573 {
5574 h = NULL;
5575 if (! (_bfd_generic_link_add_one_symbol
5576 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
5577 (bfd_vma) 0, (const char *) NULL, false,
5578 get_elf_backend_data (abfd)->collect,
5579 (struct bfd_link_hash_entry **) &h)))
5580 return false;
5581 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5582 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5583 h->type = STT_SECTION;
5584
5585 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5586 return false;
5587
5588 if (! mips_elf_hash_table (info)->use_rld_obj_head)
5589 {
5590 /* __rld_map is a four byte word located in the .data section
5591 and is filled in by the rtld to contain a pointer to
5592 the _r_debug structure. Its symbol value will be set in
5593 mips_elf_finish_dynamic_symbol. */
5594 s = bfd_get_section_by_name (abfd, ".rld_map");
5595 BFD_ASSERT (s != NULL);
5596
5597 h = NULL;
5598 if (! (_bfd_generic_link_add_one_symbol
5599 (info, abfd, "__rld_map", BSF_GLOBAL, s,
5600 (bfd_vma) 0, (const char *) NULL, false,
5601 get_elf_backend_data (abfd)->collect,
5602 (struct bfd_link_hash_entry **) &h)))
5603 return false;
5604 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5605 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5606 h->type = STT_OBJECT;
5607
5608 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5609 return false;
5610 }
5611 }
5612
5613 return true;
5614 }
5615
5616 /* Create the .compact_rel section. */
5617
5618 static boolean
5619 mips_elf_create_compact_rel_section (abfd, info)
5620 bfd *abfd;
5621 struct bfd_link_info *info;
5622 {
5623 flagword flags;
5624 register asection *s;
5625
5626 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
5627 {
5628 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5629 | SEC_READONLY);
5630
5631 s = bfd_make_section (abfd, ".compact_rel");
5632 if (s == NULL
5633 || ! bfd_set_section_flags (abfd, s, flags)
5634 || ! bfd_set_section_alignment (abfd, s, 2))
5635 return false;
5636
5637 s->_raw_size = sizeof (Elf32_External_compact_rel);
5638 }
5639
5640 return true;
5641 }
5642
5643 /* Create the .got section to hold the global offset table. */
5644
5645 static boolean
5646 mips_elf_create_got_section (abfd, info)
5647 bfd *abfd;
5648 struct bfd_link_info *info;
5649 {
5650 flagword flags;
5651 register asection *s;
5652 struct elf_link_hash_entry *h;
5653 struct mips_got_info *g;
5654
5655 /* This function may be called more than once. */
5656 if (bfd_get_section_by_name (abfd, ".got") != NULL)
5657 return true;
5658
5659 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5660 | SEC_LINKER_CREATED);
5661
5662 s = bfd_make_section (abfd, ".got");
5663 if (s == NULL
5664 || ! bfd_set_section_flags (abfd, s, flags)
5665 || ! bfd_set_section_alignment (abfd, s, 4))
5666 return false;
5667
5668 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5669 linker script because we don't want to define the symbol if we
5670 are not creating a global offset table. */
5671 h = NULL;
5672 if (! (_bfd_generic_link_add_one_symbol
5673 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5674 (bfd_vma) 0, (const char *) NULL, false,
5675 get_elf_backend_data (abfd)->collect,
5676 (struct bfd_link_hash_entry **) &h)))
5677 return false;
5678 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5679 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5680 h->type = STT_OBJECT;
5681
5682 if (info->shared
5683 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
5684 return false;
5685
5686 /* The first several global offset table entries are reserved. */
5687 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
5688
5689 g = (struct mips_got_info *) bfd_alloc (abfd,
5690 sizeof (struct mips_got_info));
5691 if (g == NULL)
5692 return false;
5693 g->global_gotsym = 0;
5694 g->local_gotno = MIPS_RESERVED_GOTNO;
5695 g->assigned_gotno = MIPS_RESERVED_GOTNO;
5696 if (elf_section_data (s) == NULL)
5697 {
5698 s->used_by_bfd =
5699 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
5700 if (elf_section_data (s) == NULL)
5701 return false;
5702 }
5703 elf_section_data (s)->tdata = (PTR) g;
5704
5705 return true;
5706 }
5707
5708 /* Look through the relocs for a section during the first phase, and
5709 allocate space in the global offset table. */
5710
5711 static boolean
5712 mips_elf_check_relocs (abfd, info, sec, relocs)
5713 bfd *abfd;
5714 struct bfd_link_info *info;
5715 asection *sec;
5716 const Elf_Internal_Rela *relocs;
5717 {
5718 const char *name;
5719 bfd *dynobj;
5720 Elf_Internal_Shdr *symtab_hdr;
5721 struct elf_link_hash_entry **sym_hashes;
5722 struct mips_got_info *g;
5723 size_t extsymoff;
5724 const Elf_Internal_Rela *rel;
5725 const Elf_Internal_Rela *rel_end;
5726 asection *sgot;
5727 asection *sreloc;
5728
5729 if (info->relocateable)
5730 return true;
5731
5732 dynobj = elf_hash_table (info)->dynobj;
5733 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5734 sym_hashes = elf_sym_hashes (abfd);
5735 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
5736
5737 /* Check for the mips16 stub sections. */
5738
5739 name = bfd_get_section_name (abfd, sec);
5740 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
5741 {
5742 unsigned long r_symndx;
5743
5744 /* Look at the relocation information to figure out which symbol
5745 this is for. */
5746
5747 r_symndx = ELF32_R_SYM (relocs->r_info);
5748
5749 if (r_symndx < extsymoff
5750 || sym_hashes[r_symndx - extsymoff] == NULL)
5751 {
5752 asection *o;
5753
5754 /* This stub is for a local symbol. This stub will only be
5755 needed if there is some relocation in this BFD, other
5756 than a 16 bit function call, which refers to this symbol. */
5757 for (o = abfd->sections; o != NULL; o = o->next)
5758 {
5759 Elf_Internal_Rela *sec_relocs;
5760 const Elf_Internal_Rela *r, *rend;
5761
5762 /* We can ignore stub sections when looking for relocs. */
5763 if ((o->flags & SEC_RELOC) == 0
5764 || o->reloc_count == 0
5765 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
5766 sizeof FN_STUB - 1) == 0
5767 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
5768 sizeof CALL_STUB - 1) == 0
5769 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
5770 sizeof CALL_FP_STUB - 1) == 0)
5771 continue;
5772
5773 sec_relocs = (_bfd_elf32_link_read_relocs
5774 (abfd, o, (PTR) NULL,
5775 (Elf_Internal_Rela *) NULL,
5776 info->keep_memory));
5777 if (sec_relocs == NULL)
5778 return false;
5779
5780 rend = sec_relocs + o->reloc_count;
5781 for (r = sec_relocs; r < rend; r++)
5782 if (ELF32_R_SYM (r->r_info) == r_symndx
5783 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
5784 break;
5785
5786 if (! info->keep_memory)
5787 free (sec_relocs);
5788
5789 if (r < rend)
5790 break;
5791 }
5792
5793 if (o == NULL)
5794 {
5795 /* There is no non-call reloc for this stub, so we do
5796 not need it. Since this function is called before
5797 the linker maps input sections to output sections, we
5798 can easily discard it by setting the SEC_EXCLUDE
5799 flag. */
5800 sec->flags |= SEC_EXCLUDE;
5801 return true;
5802 }
5803
5804 /* Record this stub in an array of local symbol stubs for
5805 this BFD. */
5806 if (elf_tdata (abfd)->local_stubs == NULL)
5807 {
5808 unsigned long symcount;
5809 asection **n;
5810
5811 if (elf_bad_symtab (abfd))
5812 symcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
5813 else
5814 symcount = symtab_hdr->sh_info;
5815 n = (asection **) bfd_zalloc (abfd,
5816 symcount * sizeof (asection *));
5817 if (n == NULL)
5818 return false;
5819 elf_tdata (abfd)->local_stubs = n;
5820 }
5821
5822 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
5823
5824 /* We don't need to set mips16_stubs_seen in this case.
5825 That flag is used to see whether we need to look through
5826 the global symbol table for stubs. We don't need to set
5827 it here, because we just have a local stub. */
5828 }
5829 else
5830 {
5831 struct mips_elf_link_hash_entry *h;
5832
5833 h = ((struct mips_elf_link_hash_entry *)
5834 sym_hashes[r_symndx - extsymoff]);
5835
5836 /* H is the symbol this stub is for. */
5837
5838 h->fn_stub = sec;
5839 mips_elf_hash_table (info)->mips16_stubs_seen = true;
5840 }
5841 }
5842 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
5843 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5844 {
5845 unsigned long r_symndx;
5846 struct mips_elf_link_hash_entry *h;
5847 asection **loc;
5848
5849 /* Look at the relocation information to figure out which symbol
5850 this is for. */
5851
5852 r_symndx = ELF32_R_SYM (relocs->r_info);
5853
5854 if (r_symndx < extsymoff
5855 || sym_hashes[r_symndx - extsymoff] == NULL)
5856 {
5857 /* This stub was actually built for a static symbol defined
5858 in the same file. We assume that all static symbols in
5859 mips16 code are themselves mips16, so we can simply
5860 discard this stub. Since this function is called before
5861 the linker maps input sections to output sections, we can
5862 easily discard it by setting the SEC_EXCLUDE flag. */
5863 sec->flags |= SEC_EXCLUDE;
5864 return true;
5865 }
5866
5867 h = ((struct mips_elf_link_hash_entry *)
5868 sym_hashes[r_symndx - extsymoff]);
5869
5870 /* H is the symbol this stub is for. */
5871
5872 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5873 loc = &h->call_fp_stub;
5874 else
5875 loc = &h->call_stub;
5876
5877 /* If we already have an appropriate stub for this function, we
5878 don't need another one, so we can discard this one. Since
5879 this function is called before the linker maps input sections
5880 to output sections, we can easily discard it by setting the
5881 SEC_EXCLUDE flag. We can also discard this section if we
5882 happen to already know that this is a mips16 function; it is
5883 not necessary to check this here, as it is checked later, but
5884 it is slightly faster to check now. */
5885 if (*loc != NULL || h->root.other == STO_MIPS16)
5886 {
5887 sec->flags |= SEC_EXCLUDE;
5888 return true;
5889 }
5890
5891 *loc = sec;
5892 mips_elf_hash_table (info)->mips16_stubs_seen = true;
5893 }
5894
5895 if (dynobj == NULL)
5896 {
5897 sgot = NULL;
5898 g = NULL;
5899 }
5900 else
5901 {
5902 sgot = bfd_get_section_by_name (dynobj, ".got");
5903 if (sgot == NULL)
5904 g = NULL;
5905 else
5906 {
5907 BFD_ASSERT (elf_section_data (sgot) != NULL);
5908 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5909 BFD_ASSERT (g != NULL);
5910 }
5911 }
5912
5913 sreloc = NULL;
5914
5915 rel_end = relocs + sec->reloc_count;
5916 for (rel = relocs; rel < rel_end; rel++)
5917 {
5918 unsigned long r_symndx;
5919 struct elf_link_hash_entry *h;
5920
5921 r_symndx = ELF32_R_SYM (rel->r_info);
5922
5923 if (r_symndx < extsymoff)
5924 h = NULL;
5925 else
5926 {
5927 h = sym_hashes[r_symndx - extsymoff];
5928
5929 /* This may be an indirect symbol created because of a version. */
5930 if (h != NULL)
5931 {
5932 while (h->root.type == bfd_link_hash_indirect)
5933 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5934 }
5935 }
5936
5937 /* Some relocs require a global offset table. */
5938 if (dynobj == NULL || sgot == NULL)
5939 {
5940 switch (ELF32_R_TYPE (rel->r_info))
5941 {
5942 case R_MIPS_GOT16:
5943 case R_MIPS_CALL16:
5944 case R_MIPS_CALL_HI16:
5945 case R_MIPS_CALL_LO16:
5946 case R_MIPS_GOT_HI16:
5947 case R_MIPS_GOT_LO16:
5948 if (dynobj == NULL)
5949 elf_hash_table (info)->dynobj = dynobj = abfd;
5950 if (! mips_elf_create_got_section (dynobj, info))
5951 return false;
5952 sgot = bfd_get_section_by_name (dynobj, ".got");
5953 BFD_ASSERT (sgot != NULL);
5954 BFD_ASSERT (elf_section_data (sgot) != NULL);
5955 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5956 BFD_ASSERT (g != NULL);
5957 break;
5958
5959 case R_MIPS_32:
5960 case R_MIPS_REL32:
5961 if (dynobj == NULL
5962 && (info->shared || h != NULL)
5963 && (sec->flags & SEC_ALLOC) != 0)
5964 elf_hash_table (info)->dynobj = dynobj = abfd;
5965 break;
5966
5967 default:
5968 break;
5969 }
5970 }
5971
5972 switch (ELF32_R_TYPE (rel->r_info))
5973 {
5974 case R_MIPS_CALL16:
5975 case R_MIPS_CALL_HI16:
5976 case R_MIPS_CALL_LO16:
5977 /* This symbol requires a global offset table entry. */
5978
5979 if (h == NULL)
5980 {
5981 (*_bfd_error_handler)
5982 ("%s: CALL16 reloc at 0x%lx not against global symbol",
5983 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
5984 bfd_set_error (bfd_error_bad_value);
5985 return false;
5986 }
5987
5988 /* Make sure this symbol is output as a dynamic symbol. */
5989 if (h->dynindx == -1)
5990 {
5991 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5992 return false;
5993 }
5994
5995 if (h->got_offset != (bfd_vma) -1)
5996 {
5997 /* We have already allocated space in the .got. */
5998 break;
5999 }
6000
6001 /* Note the index of the first global got symbol in .dynsym. */
6002 if (g->global_gotsym == 0
6003 || g->global_gotsym > (unsigned long) h->dynindx)
6004 g->global_gotsym = h->dynindx;
6005
6006 /* Make this symbol to have the corresponding got entry. */
6007 h->got_offset = 0;
6008
6009 /* We need a stub, not a plt entry for the undefined
6010 function. But we record it as if it needs plt. See
6011 elf_adjust_dynamic_symbol in elflink.h. */
6012 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6013 h->type = STT_FUNC;
6014
6015 break;
6016
6017 case R_MIPS_GOT16:
6018 case R_MIPS_GOT_HI16:
6019 case R_MIPS_GOT_LO16:
6020 /* This symbol requires a global offset table entry. */
6021
6022 if (h != NULL)
6023 {
6024 /* Make sure this symbol is output as a dynamic symbol. */
6025 if (h->dynindx == -1)
6026 {
6027 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6028 return false;
6029 }
6030
6031 if (h->got_offset != (bfd_vma) -1)
6032 {
6033 /* We have already allocated space in the .got. */
6034 break;
6035 }
6036 /* Note the index of the first global got symbol in
6037 .dynsym. */
6038 if (g->global_gotsym == 0
6039 || g->global_gotsym > (unsigned long) h->dynindx)
6040 g->global_gotsym = h->dynindx;
6041
6042 /* Make this symbol to be the global got symbol. */
6043 h->got_offset = 0;
6044 }
6045
6046 break;
6047
6048 case R_MIPS_32:
6049 case R_MIPS_REL32:
6050 if ((info->shared || h != NULL)
6051 && (sec->flags & SEC_ALLOC) != 0)
6052 {
6053 if (sreloc == NULL)
6054 {
6055 const char *name = ".rel.dyn";
6056
6057 sreloc = bfd_get_section_by_name (dynobj, name);
6058 if (sreloc == NULL)
6059 {
6060 sreloc = bfd_make_section (dynobj, name);
6061 if (sreloc == NULL
6062 || ! bfd_set_section_flags (dynobj, sreloc,
6063 (SEC_ALLOC
6064 | SEC_LOAD
6065 | SEC_HAS_CONTENTS
6066 | SEC_IN_MEMORY
6067 | SEC_LINKER_CREATED
6068 | SEC_READONLY))
6069 || ! bfd_set_section_alignment (dynobj, sreloc,
6070 4))
6071 return false;
6072 }
6073 }
6074 if (info->shared)
6075 {
6076 /* When creating a shared object, we must copy these
6077 reloc types into the output file as R_MIPS_REL32
6078 relocs. We make room for this reloc in the
6079 .rel.dyn reloc section */
6080 if (sreloc->_raw_size == 0)
6081 {
6082 /* Add a null element. */
6083 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6084 ++sreloc->reloc_count;
6085 }
6086 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6087 }
6088 else
6089 {
6090 struct mips_elf_link_hash_entry *hmips;
6091
6092 /* We only need to copy this reloc if the symbol is
6093 defined in a dynamic object. */
6094 hmips = (struct mips_elf_link_hash_entry *) h;
6095 ++hmips->mips_32_relocs;
6096 }
6097 }
6098
6099 if (SGI_COMPAT (abfd))
6100 mips_elf_hash_table (info)->compact_rel_size +=
6101 sizeof (Elf32_External_crinfo);
6102
6103 break;
6104
6105 case R_MIPS_26:
6106 case R_MIPS_GPREL16:
6107 case R_MIPS_LITERAL:
6108 case R_MIPS_GPREL32:
6109 if (SGI_COMPAT (abfd))
6110 mips_elf_hash_table (info)->compact_rel_size +=
6111 sizeof (Elf32_External_crinfo);
6112 break;
6113
6114 default:
6115 break;
6116 }
6117
6118 /* If this reloc is not a 16 bit call, and it has a global
6119 symbol, then we will need the fn_stub if there is one.
6120 References from a stub section do not count. */
6121 if (h != NULL
6122 && ELF32_R_TYPE (rel->r_info) != R_MIPS16_26
6123 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6124 sizeof FN_STUB - 1) != 0
6125 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6126 sizeof CALL_STUB - 1) != 0
6127 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6128 sizeof CALL_FP_STUB - 1) != 0)
6129 {
6130 struct mips_elf_link_hash_entry *mh;
6131
6132 mh = (struct mips_elf_link_hash_entry *) h;
6133 mh->need_fn_stub = true;
6134 }
6135 }
6136
6137 return true;
6138 }
6139
6140 /* Adjust a symbol defined by a dynamic object and referenced by a
6141 regular object. The current definition is in some section of the
6142 dynamic object, but we're not including those sections. We have to
6143 change the definition to something the rest of the link can
6144 understand. */
6145
6146 static boolean
6147 mips_elf_adjust_dynamic_symbol (info, h)
6148 struct bfd_link_info *info;
6149 struct elf_link_hash_entry *h;
6150 {
6151 bfd *dynobj;
6152 struct mips_elf_link_hash_entry *hmips;
6153 asection *s;
6154
6155 dynobj = elf_hash_table (info)->dynobj;
6156
6157 /* Make sure we know what is going on here. */
6158 BFD_ASSERT (dynobj != NULL
6159 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
6160 || h->weakdef != NULL
6161 || ((h->elf_link_hash_flags
6162 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6163 && (h->elf_link_hash_flags
6164 & ELF_LINK_HASH_REF_REGULAR) != 0
6165 && (h->elf_link_hash_flags
6166 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
6167
6168 /* If this symbol is defined in a dynamic object, we need to copy
6169 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6170 file. */
6171 hmips = (struct mips_elf_link_hash_entry *) h;
6172 if (! info->relocateable
6173 && hmips->mips_32_relocs != 0
6174 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6175 {
6176 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6177 BFD_ASSERT (s != NULL);
6178
6179 if (s->_raw_size == 0)
6180 {
6181 /* Make room for a null element. */
6182 s->_raw_size += sizeof (Elf32_External_Rel);
6183 ++s->reloc_count;
6184 }
6185 s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
6186 }
6187
6188 /* For a function, create a stub, if needed. */
6189 if (h->type == STT_FUNC
6190 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
6191 {
6192 if (! elf_hash_table (info)->dynamic_sections_created)
6193 return true;
6194
6195 /* If this symbol is not defined in a regular file, then set
6196 the symbol to the stub location. This is required to make
6197 function pointers compare as equal between the normal
6198 executable and the shared library. */
6199 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6200 {
6201 /* We need .stub section. */
6202 s = bfd_get_section_by_name (dynobj, ".stub");
6203 BFD_ASSERT (s != NULL);
6204
6205 h->root.u.def.section = s;
6206 h->root.u.def.value = s->_raw_size;
6207
6208 /* XXX Write this stub address somewhere. */
6209 h->plt_offset = s->_raw_size;
6210
6211 /* Make room for this stub code. */
6212 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6213
6214 /* The last half word of the stub will be filled with the index
6215 of this symbol in .dynsym section. */
6216 return true;
6217 }
6218 }
6219
6220 /* If this is a weak symbol, and there is a real definition, the
6221 processor independent code will have arranged for us to see the
6222 real definition first, and we can just use the same value. */
6223 if (h->weakdef != NULL)
6224 {
6225 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
6226 || h->weakdef->root.type == bfd_link_hash_defweak);
6227 h->root.u.def.section = h->weakdef->root.u.def.section;
6228 h->root.u.def.value = h->weakdef->root.u.def.value;
6229 return true;
6230 }
6231
6232 /* This is a reference to a symbol defined by a dynamic object which
6233 is not a function. */
6234
6235 return true;
6236 }
6237
6238 /* This function is called after all the input files have been read,
6239 and the input sections have been assigned to output sections. We
6240 check for any mips16 stub sections that we can discard. */
6241
6242 static boolean mips_elf_check_mips16_stubs
6243 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
6244
6245 static boolean
6246 mips_elf_always_size_sections (output_bfd, info)
6247 bfd *output_bfd;
6248 struct bfd_link_info *info;
6249 {
6250 if (info->relocateable
6251 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
6252 return true;
6253
6254 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6255 mips_elf_check_mips16_stubs,
6256 (PTR) NULL);
6257
6258 return true;
6259 }
6260
6261 /* Check the mips16 stubs for a particular symbol, and see if we can
6262 discard them. */
6263
6264 /*ARGSUSED*/
6265 static boolean
6266 mips_elf_check_mips16_stubs (h, data)
6267 struct mips_elf_link_hash_entry *h;
6268 PTR data;
6269 {
6270 if (h->fn_stub != NULL
6271 && ! h->need_fn_stub)
6272 {
6273 /* We don't need the fn_stub; the only references to this symbol
6274 are 16 bit calls. Clobber the size to 0 to prevent it from
6275 being included in the link. */
6276 h->fn_stub->_raw_size = 0;
6277 h->fn_stub->_cooked_size = 0;
6278 h->fn_stub->flags &= ~ SEC_RELOC;
6279 h->fn_stub->reloc_count = 0;
6280 h->fn_stub->flags |= SEC_EXCLUDE;
6281 }
6282
6283 if (h->call_stub != NULL
6284 && h->root.other == STO_MIPS16)
6285 {
6286 /* We don't need the call_stub; this is a 16 bit function, so
6287 calls from other 16 bit functions are OK. Clobber the size
6288 to 0 to prevent it from being included in the link. */
6289 h->call_stub->_raw_size = 0;
6290 h->call_stub->_cooked_size = 0;
6291 h->call_stub->flags &= ~ SEC_RELOC;
6292 h->call_stub->reloc_count = 0;
6293 h->call_stub->flags |= SEC_EXCLUDE;
6294 }
6295
6296 if (h->call_fp_stub != NULL
6297 && h->root.other == STO_MIPS16)
6298 {
6299 /* We don't need the call_stub; this is a 16 bit function, so
6300 calls from other 16 bit functions are OK. Clobber the size
6301 to 0 to prevent it from being included in the link. */
6302 h->call_fp_stub->_raw_size = 0;
6303 h->call_fp_stub->_cooked_size = 0;
6304 h->call_fp_stub->flags &= ~ SEC_RELOC;
6305 h->call_fp_stub->reloc_count = 0;
6306 h->call_fp_stub->flags |= SEC_EXCLUDE;
6307 }
6308
6309 return true;
6310 }
6311
6312 /* Set the sizes of the dynamic sections. */
6313
6314 static boolean
6315 mips_elf_size_dynamic_sections (output_bfd, info)
6316 bfd *output_bfd;
6317 struct bfd_link_info *info;
6318 {
6319 bfd *dynobj;
6320 asection *s;
6321 boolean reltext;
6322 asection *sgot;
6323 struct mips_got_info *g;
6324
6325 dynobj = elf_hash_table (info)->dynobj;
6326 BFD_ASSERT (dynobj != NULL);
6327
6328 if (elf_hash_table (info)->dynamic_sections_created)
6329 {
6330 /* Set the contents of the .interp section to the interpreter. */
6331 if (! info->shared)
6332 {
6333 s = bfd_get_section_by_name (dynobj, ".interp");
6334 BFD_ASSERT (s != NULL);
6335 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
6336 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
6337 }
6338 }
6339
6340 /* Recompute the size of .got for local entires (reserved and
6341 hipages) if needed. To estimate it, get the upper bound of total
6342 size of loadable sections. */
6343 sgot = bfd_get_section_by_name (dynobj, ".got");
6344
6345 if (sgot != NULL)
6346 {
6347 bfd_size_type loadable_size = 0;
6348 bfd_size_type local_gotno;
6349 struct _bfd *sub;
6350
6351 BFD_ASSERT (elf_section_data (sgot) != NULL);
6352 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6353 BFD_ASSERT (g != NULL);
6354
6355 for (sub = info->input_bfds; sub; sub = sub->link_next)
6356 for (s = sub->sections; s != NULL; s = s->next)
6357 {
6358 if ((s->flags & SEC_ALLOC) == 0)
6359 continue;
6360 loadable_size += (s->_raw_size + 0xf) & ~0xf;
6361 }
6362
6363 loadable_size += MIPS_FUNCTION_STUB_SIZE;
6364
6365 /* Assume there are two loadable segments consisting of
6366 contiguous sections. Is 5 enough? */
6367 local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
6368 g->local_gotno = local_gotno;
6369 sgot->_raw_size += local_gotno * 4;
6370 }
6371
6372 /* The check_relocs and adjust_dynamic_symbol entry points have
6373 determined the sizes of the various dynamic sections. Allocate
6374 memory for them. */
6375 reltext = false;
6376 for (s = dynobj->sections; s != NULL; s = s->next)
6377 {
6378 const char *name;
6379 boolean strip;
6380
6381 /* It's OK to base decisions on the section name, because none
6382 of the dynobj section names depend upon the input files. */
6383 name = bfd_get_section_name (dynobj, s);
6384
6385 if ((s->flags & SEC_LINKER_CREATED) == 0)
6386 continue;
6387
6388 strip = false;
6389
6390 if (strncmp (name, ".rel", 4) == 0)
6391 {
6392 if (s->_raw_size == 0)
6393 strip = true;
6394 else
6395 {
6396 const char *outname;
6397 asection *target;
6398
6399 /* If this relocation section applies to a read only
6400 section, then we probably need a DT_TEXTREL entry.
6401 If the relocation section is .rel.dyn, we always
6402 assert a DT_TEXTREL entry rather than testing whether
6403 there exists a relocation to a read only section or
6404 not. */
6405 outname = bfd_get_section_name (output_bfd,
6406 s->output_section);
6407 target = bfd_get_section_by_name (output_bfd, outname + 4);
6408 if ((target != NULL
6409 && (target->flags & SEC_READONLY) != 0
6410 && (target->flags & SEC_ALLOC) != 0)
6411 || strcmp (outname, ".rel.dyn") == 0)
6412 reltext = true;
6413
6414 /* We use the reloc_count field as a counter if we need
6415 to copy relocs into the output file. */
6416 if (strcmp (name, ".rel.dyn") != 0)
6417 s->reloc_count = 0;
6418 }
6419 }
6420 else if (strncmp (name, ".got", 4) == 0)
6421 {
6422 int i;
6423
6424 BFD_ASSERT (elf_section_data (s) != NULL);
6425 g = (struct mips_got_info *) elf_section_data (s)->tdata;
6426 BFD_ASSERT (g != NULL);
6427
6428 /* Fix the size of .got section for the correspondence of
6429 global symbols and got entries. This adds some useless
6430 got entries. Is this required by ABI really? */
6431 i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
6432 s->_raw_size += i * 4;
6433 }
6434 else if (strncmp (name, ".stub", 5) == 0)
6435 {
6436 /* Irix rld assumes that the function stub isn't at the end
6437 of .text section. So put a dummy. XXX */
6438 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6439 }
6440 else if (! info->shared
6441 && ! mips_elf_hash_table (info)->use_rld_obj_head
6442 && strncmp (name, ".rld_map", 8) == 0)
6443 {
6444 /* We add a room for __rld_map. It will be filled in by the
6445 rtld to contain a pointer to the _r_debug structure. */
6446 s->_raw_size += 4;
6447 }
6448 else if (SGI_COMPAT (output_bfd)
6449 && strncmp (name, ".compact_rel", 12) == 0)
6450 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
6451 else if (strncmp (name, ".init", 5) != 0)
6452 {
6453 /* It's not one of our sections, so don't allocate space. */
6454 continue;
6455 }
6456
6457 if (strip)
6458 {
6459 asection **spp;
6460
6461 for (spp = &s->output_section->owner->sections;
6462 *spp != s->output_section;
6463 spp = &(*spp)->next)
6464 ;
6465 *spp = s->output_section->next;
6466 --s->output_section->owner->section_count;
6467
6468 continue;
6469 }
6470
6471 /* Allocate memory for the section contents. */
6472 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
6473 if (s->contents == NULL && s->_raw_size != 0)
6474 {
6475 bfd_set_error (bfd_error_no_memory);
6476 return false;
6477 }
6478 memset (s->contents, 0, s->_raw_size);
6479 }
6480
6481 if (elf_hash_table (info)->dynamic_sections_created)
6482 {
6483 /* Add some entries to the .dynamic section. We fill in the
6484 values later, in elf_mips_finish_dynamic_sections, but we
6485 must add the entries now so that we get the correct size for
6486 the .dynamic section. The DT_DEBUG entry is filled in by the
6487 dynamic linker and used by the debugger. */
6488 if (! info->shared)
6489 {
6490 if (SGI_COMPAT (output_bfd))
6491 {
6492 /* SGI object has the equivalence of DT_DEBUG in the
6493 DT_MIPS_RLD_MAP entry. */
6494 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
6495 return false;
6496 }
6497 else
6498 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
6499 return false;
6500 }
6501
6502 if (reltext)
6503 {
6504 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
6505 return false;
6506 }
6507
6508 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
6509 return false;
6510
6511 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
6512 {
6513 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
6514 return false;
6515
6516 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
6517 return false;
6518
6519 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
6520 return false;
6521 }
6522
6523 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
6524 return false;
6525
6526 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
6527 return false;
6528
6529 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
6530 {
6531 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
6532 return false;
6533
6534 s = bfd_get_section_by_name (dynobj, ".liblist");
6535 BFD_ASSERT (s != NULL);
6536
6537 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
6538 return false;
6539 }
6540
6541 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
6542 return false;
6543
6544 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
6545 return false;
6546
6547 #if 0
6548 /* Time stamps in executable files are a bad idea. */
6549 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
6550 return false;
6551 #endif
6552
6553 #if 0 /* FIXME */
6554 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
6555 return false;
6556 #endif
6557
6558 #if 0 /* FIXME */
6559 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
6560 return false;
6561 #endif
6562
6563 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
6564 return false;
6565
6566 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
6567 return false;
6568
6569 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
6570 return false;
6571
6572 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
6573 return false;
6574
6575 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
6576 return false;
6577
6578 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
6579 return false;
6580
6581 #if 0 /* (SGI_COMPAT) */
6582 if (! bfd_get_section_by_name (dynobj, ".init"))
6583 if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
6584 return false;
6585
6586 if (! bfd_get_section_by_name (dynobj, ".fini"))
6587 if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
6588 return false;
6589 #endif
6590 }
6591
6592 /* If we use dynamic linking, we generate a section symbol for each
6593 output section. These are local symbols, which means that they
6594 must come first in the dynamic symbol table.
6595 That means we must increment the dynamic symbol index of every
6596 other dynamic symbol. */
6597 {
6598 const char * const *namep;
6599 unsigned int c, i;
6600 bfd_size_type strindex;
6601 struct bfd_strtab_hash *dynstr;
6602 struct mips_got_info *g;
6603
6604 c = 0;
6605 if (elf_hash_table (info)->dynamic_sections_created)
6606 {
6607 if (SGI_COMPAT (output_bfd))
6608 {
6609 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
6610 elf_link_hash_traverse (elf_hash_table (info),
6611 mips_elf_adjust_dynindx,
6612 (PTR) &c);
6613 elf_hash_table (info)->dynsymcount += c;
6614
6615 dynstr = elf_hash_table (info)->dynstr;
6616 BFD_ASSERT (dynstr != NULL);
6617
6618 for (i = 1, namep = mips_elf_dynsym_sec_names;
6619 *namep != NULL;
6620 i++, namep++)
6621 {
6622 s = bfd_get_section_by_name (output_bfd, *namep);
6623 if (s != NULL)
6624 elf_section_data (s)->dynindx = i;
6625
6626 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
6627 if (strindex == (bfd_size_type) -1)
6628 return false;
6629
6630 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
6631 }
6632 }
6633 else
6634 {
6635 c = bfd_count_sections (output_bfd);
6636 elf_link_hash_traverse (elf_hash_table (info),
6637 mips_elf_adjust_dynindx,
6638 (PTR) &c);
6639 elf_hash_table (info)->dynsymcount += c;
6640
6641 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
6642 {
6643 elf_section_data (s)->dynindx = i;
6644 /* These symbols will have no names, so we don't need to
6645 fiddle with dynstr_index. */
6646 }
6647 }
6648 }
6649
6650 if (sgot != NULL)
6651 {
6652 BFD_ASSERT (elf_section_data (sgot) != NULL);
6653 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6654 BFD_ASSERT (g != NULL);
6655
6656 /* If there are no global got symbols, fake the last symbol so
6657 for safety. */
6658 if (g->global_gotsym)
6659 g->global_gotsym += c;
6660 else
6661 g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
6662 }
6663 }
6664
6665 return true;
6666 }
6667
6668 /* Increment the index of a dynamic symbol by a given amount. Called
6669 via elf_link_hash_traverse. */
6670
6671 static boolean
6672 mips_elf_adjust_dynindx (h, cparg)
6673 struct elf_link_hash_entry *h;
6674 PTR cparg;
6675 {
6676 unsigned int *cp = (unsigned int *) cparg;
6677
6678 if (h->dynindx != -1)
6679 h->dynindx += *cp;
6680 return true;
6681 }
6682
6683 /* Finish up dynamic symbol handling. We set the contents of various
6684 dynamic sections here. */
6685
6686 static boolean
6687 mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
6688 bfd *output_bfd;
6689 struct bfd_link_info *info;
6690 struct elf_link_hash_entry *h;
6691 Elf_Internal_Sym *sym;
6692 {
6693 bfd *dynobj;
6694 bfd_vma gval;
6695 asection *sgot;
6696 struct mips_got_info *g;
6697 const char *name;
6698
6699 dynobj = elf_hash_table (info)->dynobj;
6700 gval = sym->st_value;
6701
6702 if (h->plt_offset != (bfd_vma) -1)
6703 {
6704 asection *s;
6705 bfd_byte *p;
6706 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
6707
6708 /* This symbol has a stub. Set it up. */
6709
6710 BFD_ASSERT (h->dynindx != -1);
6711
6712 s = bfd_get_section_by_name (dynobj, ".stub");
6713 BFD_ASSERT (s != NULL);
6714
6715 /* Fill the stub. */
6716 p = stub;
6717 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
6718 p += 4;
6719 bfd_put_32 (output_bfd, STUB_MOVE, p);
6720 p += 4;
6721
6722 /* FIXME: Can h->dynindex be more than 64K? */
6723 if (h->dynindx & 0xffff0000)
6724 return false;
6725
6726 bfd_put_32 (output_bfd, STUB_JALR, p);
6727 p += 4;
6728 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
6729
6730 BFD_ASSERT (h->plt_offset <= s->_raw_size);
6731 memcpy (s->contents + h->plt_offset, stub, MIPS_FUNCTION_STUB_SIZE);
6732
6733 /* Mark the symbol as undefined. plt_offset != -1 occurs
6734 only for the referenced symbol. */
6735 sym->st_shndx = SHN_UNDEF;
6736
6737 /* The run-time linker uses the st_value field of the symbol
6738 to reset the global offset table entry for this external
6739 to its stub address when unlinking a shared object. */
6740 gval = s->output_section->vma + s->output_offset + h->plt_offset;
6741 sym->st_value = gval;
6742 }
6743
6744 BFD_ASSERT (h->dynindx != -1);
6745
6746 sgot = bfd_get_section_by_name (dynobj, ".got");
6747 BFD_ASSERT (sgot != NULL);
6748 BFD_ASSERT (elf_section_data (sgot) != NULL);
6749 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6750 BFD_ASSERT (g != NULL);
6751
6752 if ((unsigned long) h->dynindx >= g->global_gotsym)
6753 {
6754 bfd_size_type offset;
6755
6756 /* This symbol has an entry in the global offset table. Set its
6757 value to the corresponding got entry, if needed. */
6758 if (h->got_offset == (bfd_vma) -1)
6759 {
6760 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
6761 BFD_ASSERT (g->local_gotno * 4 <= offset
6762 && offset < sgot->_raw_size);
6763 bfd_put_32 (output_bfd, gval, sgot->contents + offset);
6764 }
6765 }
6766
6767 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
6768 name = h->root.root.string;
6769 if (strcmp (name, "_DYNAMIC") == 0
6770 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
6771 sym->st_shndx = SHN_ABS;
6772 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
6773 {
6774 sym->st_shndx = SHN_ABS;
6775 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6776 sym->st_value = 1;
6777 }
6778 else if (SGI_COMPAT (output_bfd))
6779 {
6780 if (strcmp (name, "_gp_disp") == 0)
6781 {
6782 sym->st_shndx = SHN_ABS;
6783 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6784 sym->st_value = elf_gp (output_bfd);
6785 }
6786 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
6787 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
6788 {
6789 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6790 sym->st_other = STO_PROTECTED;
6791 sym->st_value = 0;
6792 sym->st_shndx = SHN_MIPS_DATA;
6793 }
6794 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
6795 {
6796 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6797 sym->st_other = STO_PROTECTED;
6798 sym->st_value = mips_elf_hash_table (info)->procedure_count;
6799 sym->st_shndx = SHN_ABS;
6800 }
6801 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
6802 {
6803 if (h->type == STT_FUNC)
6804 sym->st_shndx = SHN_MIPS_TEXT;
6805 else if (h->type == STT_OBJECT)
6806 sym->st_shndx = SHN_MIPS_DATA;
6807 }
6808 }
6809
6810 if (SGI_COMPAT (output_bfd)
6811 && ! info->shared)
6812 {
6813 if (! mips_elf_hash_table (info)->use_rld_obj_head
6814 && strcmp (name, "__rld_map") == 0)
6815 {
6816 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
6817 BFD_ASSERT (s != NULL);
6818 sym->st_value = s->output_section->vma + s->output_offset;
6819 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
6820 if (mips_elf_hash_table (info)->rld_value == 0)
6821 mips_elf_hash_table (info)->rld_value = sym->st_value;
6822 }
6823 else if (mips_elf_hash_table (info)->use_rld_obj_head
6824 && strcmp (name, "__rld_obj_head") == 0)
6825 {
6826 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
6827 BFD_ASSERT (s != NULL);
6828 mips_elf_hash_table (info)->rld_value = sym->st_value;
6829 }
6830 }
6831
6832 /* If this is a mips16 symbol, force the value to be even. */
6833 if (sym->st_other == STO_MIPS16
6834 && (sym->st_value & 1) != 0)
6835 --sym->st_value;
6836
6837 return true;
6838 }
6839
6840 /* Finish up the dynamic sections. */
6841
6842 static boolean
6843 mips_elf_finish_dynamic_sections (output_bfd, info)
6844 bfd *output_bfd;
6845 struct bfd_link_info *info;
6846 {
6847 bfd *dynobj;
6848 asection *sdyn;
6849 asection *sgot;
6850 struct mips_got_info *g;
6851
6852 dynobj = elf_hash_table (info)->dynobj;
6853
6854 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6855
6856 sgot = bfd_get_section_by_name (dynobj, ".got");
6857 if (sgot == NULL)
6858 g = NULL;
6859 else
6860 {
6861 BFD_ASSERT (elf_section_data (sgot) != NULL);
6862 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6863 BFD_ASSERT (g != NULL);
6864 }
6865
6866 if (elf_hash_table (info)->dynamic_sections_created)
6867 {
6868 Elf32_External_Dyn *dyncon, *dynconend;
6869
6870 BFD_ASSERT (sdyn != NULL);
6871 BFD_ASSERT (g != NULL);
6872
6873 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6874 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
6875 for (; dyncon < dynconend; dyncon++)
6876 {
6877 Elf_Internal_Dyn dyn;
6878 const char *name;
6879 size_t elemsize;
6880 asection *s;
6881
6882 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6883
6884 switch (dyn.d_tag)
6885 {
6886 default:
6887 break;
6888
6889 case DT_RELENT:
6890 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6891 BFD_ASSERT (s != NULL);
6892 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
6893 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6894 break;
6895
6896 case DT_STRSZ:
6897 /* Rewrite DT_STRSZ. */
6898 dyn.d_un.d_val =
6899 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
6900 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6901 break;
6902
6903 case DT_PLTGOT:
6904 name = ".got";
6905 goto get_vma;
6906 case DT_MIPS_CONFLICT:
6907 name = ".conflict";
6908 goto get_vma;
6909 case DT_MIPS_LIBLIST:
6910 name = ".liblist";
6911 get_vma:
6912 s = bfd_get_section_by_name (output_bfd, name);
6913 BFD_ASSERT (s != NULL);
6914 dyn.d_un.d_ptr = s->vma;
6915 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6916 break;
6917
6918 case DT_MIPS_RLD_VERSION:
6919 dyn.d_un.d_val = 1; /* XXX */
6920 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6921 break;
6922
6923 case DT_MIPS_FLAGS:
6924 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
6925 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6926 break;
6927
6928 case DT_MIPS_CONFLICTNO:
6929 name = ".conflict";
6930 elemsize = sizeof (Elf32_Conflict);
6931 goto set_elemno;
6932
6933 case DT_MIPS_LIBLISTNO:
6934 name = ".liblist";
6935 elemsize = sizeof (Elf32_Lib);
6936 set_elemno:
6937 s = bfd_get_section_by_name (output_bfd, name);
6938 if (s != NULL)
6939 {
6940 if (s->_cooked_size != 0)
6941 dyn.d_un.d_val = s->_cooked_size / elemsize;
6942 else
6943 dyn.d_un.d_val = s->_raw_size / elemsize;
6944 }
6945 else
6946 dyn.d_un.d_val = 0;
6947
6948 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6949 break;
6950
6951 case DT_MIPS_TIME_STAMP:
6952 time ((time_t *) &dyn.d_un.d_val);
6953 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6954 break;
6955
6956 case DT_MIPS_ICHECKSUM:
6957 /* XXX FIXME: */
6958 break;
6959
6960 case DT_MIPS_IVERSION:
6961 /* XXX FIXME: */
6962 break;
6963
6964 case DT_MIPS_BASE_ADDRESS:
6965 s = output_bfd->sections;
6966 BFD_ASSERT (s != NULL);
6967 dyn.d_un.d_ptr = s->vma & ~(0xffff);
6968 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6969 break;
6970
6971 case DT_MIPS_LOCAL_GOTNO:
6972 dyn.d_un.d_val = g->local_gotno;
6973 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6974 break;
6975
6976 case DT_MIPS_SYMTABNO:
6977 name = ".dynsym";
6978 elemsize = sizeof (Elf32_External_Sym);
6979 s = bfd_get_section_by_name (output_bfd, name);
6980 BFD_ASSERT (s != NULL);
6981
6982 if (s->_cooked_size != 0)
6983 dyn.d_un.d_val = s->_cooked_size / elemsize;
6984 else
6985 dyn.d_un.d_val = s->_raw_size / elemsize;
6986 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6987 break;
6988
6989 case DT_MIPS_UNREFEXTNO:
6990 /* XXX FIXME: */
6991 dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
6992 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6993 break;
6994
6995 case DT_MIPS_GOTSYM:
6996 dyn.d_un.d_val = g->global_gotsym;
6997 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6998 break;
6999
7000 case DT_MIPS_HIPAGENO:
7001 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
7002 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7003 break;
7004
7005 case DT_MIPS_RLD_MAP:
7006 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
7007 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7008 break;
7009
7010 }
7011 }
7012 }
7013
7014 /* The first entry of the global offset table will be filled at
7015 runtime. The second entry will be used by some runtime loaders.
7016 This isn't the case of Irix rld. */
7017 if (sgot != NULL && sgot->_raw_size > 0)
7018 {
7019 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7020 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
7021 }
7022
7023 if (sgot != NULL)
7024 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7025
7026 {
7027 asection *sdynsym;
7028 asection *s;
7029 unsigned int i;
7030 bfd_vma last;
7031 Elf_Internal_Sym sym;
7032 long dindx;
7033 const char *name;
7034 const char * const * namep = mips_elf_dynsym_sec_names;
7035 Elf32_compact_rel cpt;
7036
7037 /* Set up the section symbols for the output sections. SGI sets
7038 the STT_NOTYPE attribute for these symbols. Should we do so? */
7039
7040 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
7041 if (sdynsym != NULL)
7042 {
7043 if (SGI_COMPAT (output_bfd))
7044 {
7045 sym.st_size = 0;
7046 sym.st_name = 0;
7047 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7048 sym.st_other = 0;
7049
7050 i = 0;
7051 while ((name = *namep++) != NULL)
7052 {
7053 s = bfd_get_section_by_name (output_bfd, name);
7054 if (s != NULL)
7055 {
7056 sym.st_value = s->vma;
7057 dindx = elf_section_data (s)->dynindx;
7058 last = s->vma + s->_raw_size;
7059 }
7060 else
7061 {
7062 sym.st_value = last;
7063 dindx++;
7064 }
7065
7066 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
7067 ? SHN_MIPS_TEXT
7068 : SHN_MIPS_DATA);
7069 ++i;
7070 sym.st_name =
7071 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
7072
7073 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7074 (((Elf32_External_Sym *)
7075 sdynsym->contents)
7076 + dindx));
7077 }
7078
7079 /* Set the sh_info field of the output .dynsym section to
7080 the index of the first global symbol. */
7081 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7082 SIZEOF_MIPS_DYNSYM_SECNAMES;
7083 }
7084 else
7085 {
7086 sym.st_size = 0;
7087 sym.st_name = 0;
7088 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7089 sym.st_other = 0;
7090
7091 for (s = output_bfd->sections; s != NULL; s = s->next)
7092 {
7093 int indx;
7094
7095 sym.st_value = s->vma;
7096
7097 indx = elf_section_data (s)->this_idx;
7098 BFD_ASSERT (indx > 0);
7099 sym.st_shndx = indx;
7100
7101 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7102 (((Elf32_External_Sym *)
7103 sdynsym->contents)
7104 + elf_section_data (s)->dynindx));
7105 }
7106
7107 /* Set the sh_info field of the output .dynsym section to
7108 the index of the first global symbol. */
7109 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7110 bfd_count_sections (output_bfd) + 1;
7111 }
7112 }
7113
7114 if (SGI_COMPAT (output_bfd))
7115 {
7116 /* Write .compact_rel section out. */
7117 s = bfd_get_section_by_name (dynobj, ".compact_rel");
7118 if (s != NULL)
7119 {
7120 cpt.id1 = 1;
7121 cpt.num = s->reloc_count;
7122 cpt.id2 = 2;
7123 cpt.offset = (s->output_section->filepos
7124 + sizeof (Elf32_External_compact_rel));
7125 cpt.reserved0 = 0;
7126 cpt.reserved1 = 0;
7127 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7128 ((Elf32_External_compact_rel *)
7129 s->contents));
7130
7131 /* Clean up a dummy stub function entry in .text. */
7132 s = bfd_get_section_by_name (dynobj, ".stub");
7133 if (s != NULL)
7134 {
7135 file_ptr dummy_offset;
7136
7137 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
7138 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
7139 memset (s->contents + dummy_offset, 0,
7140 MIPS_FUNCTION_STUB_SIZE);
7141 }
7142 }
7143 }
7144
7145 /* Clean up a first relocation in .rel.dyn. */
7146 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7147 if (s != NULL && s->_raw_size > 0)
7148 memset (s->contents, 0, sizeof (Elf32_External_Rel));
7149 }
7150
7151 return true;
7152 }
7153 \f
7154 /* This is almost identical to bfd_generic_get_... except that some
7155 MIPS relocations need to be handled specially. Sigh. */
7156
7157 static bfd_byte *
7158 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
7159 relocateable, symbols)
7160 bfd *abfd;
7161 struct bfd_link_info *link_info;
7162 struct bfd_link_order *link_order;
7163 bfd_byte *data;
7164 boolean relocateable;
7165 asymbol **symbols;
7166 {
7167 /* Get enough memory to hold the stuff */
7168 bfd *input_bfd = link_order->u.indirect.section->owner;
7169 asection *input_section = link_order->u.indirect.section;
7170
7171 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
7172 arelent **reloc_vector = NULL;
7173 long reloc_count;
7174
7175 if (reloc_size < 0)
7176 goto error_return;
7177
7178 reloc_vector = (arelent **) bfd_malloc (reloc_size);
7179 if (reloc_vector == NULL && reloc_size != 0)
7180 goto error_return;
7181
7182 /* read in the section */
7183 if (!bfd_get_section_contents (input_bfd,
7184 input_section,
7185 (PTR) data,
7186 0,
7187 input_section->_raw_size))
7188 goto error_return;
7189
7190 /* We're not relaxing the section, so just copy the size info */
7191 input_section->_cooked_size = input_section->_raw_size;
7192 input_section->reloc_done = true;
7193
7194 reloc_count = bfd_canonicalize_reloc (input_bfd,
7195 input_section,
7196 reloc_vector,
7197 symbols);
7198 if (reloc_count < 0)
7199 goto error_return;
7200
7201 if (reloc_count > 0)
7202 {
7203 arelent **parent;
7204 /* for mips */
7205 int gp_found;
7206 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
7207
7208 {
7209 struct bfd_hash_entry *h;
7210 struct bfd_link_hash_entry *lh;
7211 /* Skip all this stuff if we aren't mixing formats. */
7212 if (abfd && input_bfd
7213 && abfd->xvec == input_bfd->xvec)
7214 lh = 0;
7215 else
7216 {
7217 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
7218 lh = (struct bfd_link_hash_entry *) h;
7219 }
7220 lookup:
7221 if (lh)
7222 {
7223 switch (lh->type)
7224 {
7225 case bfd_link_hash_undefined:
7226 case bfd_link_hash_undefweak:
7227 case bfd_link_hash_common:
7228 gp_found = 0;
7229 break;
7230 case bfd_link_hash_defined:
7231 case bfd_link_hash_defweak:
7232 gp_found = 1;
7233 gp = lh->u.def.value;
7234 break;
7235 case bfd_link_hash_indirect:
7236 case bfd_link_hash_warning:
7237 lh = lh->u.i.link;
7238 /* @@FIXME ignoring warning for now */
7239 goto lookup;
7240 case bfd_link_hash_new:
7241 default:
7242 abort ();
7243 }
7244 }
7245 else
7246 gp_found = 0;
7247 }
7248 /* end mips */
7249 for (parent = reloc_vector; *parent != (arelent *) NULL;
7250 parent++)
7251 {
7252 char *error_message = (char *) NULL;
7253 bfd_reloc_status_type r;
7254
7255 /* Specific to MIPS: Deal with relocation types that require
7256 knowing the gp of the output bfd. */
7257 asymbol *sym = *(*parent)->sym_ptr_ptr;
7258 if (bfd_is_abs_section (sym->section) && abfd)
7259 {
7260 /* The special_function wouldn't get called anyways. */
7261 }
7262 else if (!gp_found)
7263 {
7264 /* The gp isn't there; let the special function code
7265 fall over on its own. */
7266 }
7267 else if ((*parent)->howto->special_function
7268 == _bfd_mips_elf_gprel16_reloc)
7269 {
7270 /* bypass special_function call */
7271 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
7272 relocateable, (PTR) data, gp);
7273 goto skip_bfd_perform_relocation;
7274 }
7275 /* end mips specific stuff */
7276
7277 r = bfd_perform_relocation (input_bfd,
7278 *parent,
7279 (PTR) data,
7280 input_section,
7281 relocateable ? abfd : (bfd *) NULL,
7282 &error_message);
7283 skip_bfd_perform_relocation:
7284
7285 if (relocateable)
7286 {
7287 asection *os = input_section->output_section;
7288
7289 /* A partial link, so keep the relocs */
7290 os->orelocation[os->reloc_count] = *parent;
7291 os->reloc_count++;
7292 }
7293
7294 if (r != bfd_reloc_ok)
7295 {
7296 switch (r)
7297 {
7298 case bfd_reloc_undefined:
7299 if (!((*link_info->callbacks->undefined_symbol)
7300 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7301 input_bfd, input_section, (*parent)->address)))
7302 goto error_return;
7303 break;
7304 case bfd_reloc_dangerous:
7305 BFD_ASSERT (error_message != (char *) NULL);
7306 if (!((*link_info->callbacks->reloc_dangerous)
7307 (link_info, error_message, input_bfd, input_section,
7308 (*parent)->address)))
7309 goto error_return;
7310 break;
7311 case bfd_reloc_overflow:
7312 if (!((*link_info->callbacks->reloc_overflow)
7313 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7314 (*parent)->howto->name, (*parent)->addend,
7315 input_bfd, input_section, (*parent)->address)))
7316 goto error_return;
7317 break;
7318 case bfd_reloc_outofrange:
7319 default:
7320 abort ();
7321 break;
7322 }
7323
7324 }
7325 }
7326 }
7327 if (reloc_vector != NULL)
7328 free (reloc_vector);
7329 return data;
7330
7331 error_return:
7332 if (reloc_vector != NULL)
7333 free (reloc_vector);
7334 return NULL;
7335 }
7336 #define bfd_elf32_bfd_get_relocated_section_contents \
7337 elf32_mips_get_relocated_section_contents
7338 \f
7339 /* ECOFF swapping routines. These are used when dealing with the
7340 .mdebug section, which is in the ECOFF debugging format. */
7341 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
7342 {
7343 /* Symbol table magic number. */
7344 magicSym,
7345 /* Alignment of debugging information. E.g., 4. */
7346 4,
7347 /* Sizes of external symbolic information. */
7348 sizeof (struct hdr_ext),
7349 sizeof (struct dnr_ext),
7350 sizeof (struct pdr_ext),
7351 sizeof (struct sym_ext),
7352 sizeof (struct opt_ext),
7353 sizeof (struct fdr_ext),
7354 sizeof (struct rfd_ext),
7355 sizeof (struct ext_ext),
7356 /* Functions to swap in external symbolic data. */
7357 ecoff_swap_hdr_in,
7358 ecoff_swap_dnr_in,
7359 ecoff_swap_pdr_in,
7360 ecoff_swap_sym_in,
7361 ecoff_swap_opt_in,
7362 ecoff_swap_fdr_in,
7363 ecoff_swap_rfd_in,
7364 ecoff_swap_ext_in,
7365 _bfd_ecoff_swap_tir_in,
7366 _bfd_ecoff_swap_rndx_in,
7367 /* Functions to swap out external symbolic data. */
7368 ecoff_swap_hdr_out,
7369 ecoff_swap_dnr_out,
7370 ecoff_swap_pdr_out,
7371 ecoff_swap_sym_out,
7372 ecoff_swap_opt_out,
7373 ecoff_swap_fdr_out,
7374 ecoff_swap_rfd_out,
7375 ecoff_swap_ext_out,
7376 _bfd_ecoff_swap_tir_out,
7377 _bfd_ecoff_swap_rndx_out,
7378 /* Function to read in symbolic data. */
7379 _bfd_mips_elf_read_ecoff_info
7380 };
7381 \f
7382 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
7383 #define TARGET_LITTLE_NAME "elf32-littlemips"
7384 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
7385 #define TARGET_BIG_NAME "elf32-bigmips"
7386 #define ELF_ARCH bfd_arch_mips
7387 #define ELF_MACHINE_CODE EM_MIPS
7388
7389 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
7390 a value of 0x1000, and we are compatible. */
7391 #define ELF_MAXPAGESIZE 0x1000
7392
7393 #define elf_backend_collect true
7394 #define elf_backend_type_change_ok true
7395 #define elf_info_to_howto 0
7396 #define elf_info_to_howto_rel mips_info_to_howto_rel
7397 #define elf_backend_sym_is_global mips_elf_sym_is_global
7398 #define elf_backend_object_p mips_elf32_object_p
7399 #define elf_backend_section_from_shdr mips_elf32_section_from_shdr
7400 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
7401 #define elf_backend_section_from_bfd_section \
7402 _bfd_mips_elf_section_from_bfd_section
7403 #define elf_backend_section_processing mips_elf32_section_processing
7404 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
7405 #define elf_backend_additional_program_headers \
7406 mips_elf_additional_program_headers
7407 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
7408 #define elf_backend_final_write_processing \
7409 _bfd_mips_elf_final_write_processing
7410 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
7411
7412 #define bfd_elf32_bfd_is_local_label_name \
7413 mips_elf_is_local_label_name
7414 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
7415 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
7416 #define bfd_elf32_bfd_link_hash_table_create \
7417 mips_elf_link_hash_table_create
7418 #define bfd_elf32_bfd_final_link mips_elf_final_link
7419 #define bfd_elf32_bfd_copy_private_bfd_data \
7420 _bfd_mips_elf_copy_private_bfd_data
7421 #define bfd_elf32_bfd_merge_private_bfd_data \
7422 _bfd_mips_elf_merge_private_bfd_data
7423 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
7424 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
7425 #define elf_backend_create_dynamic_sections \
7426 mips_elf_create_dynamic_sections
7427 #define elf_backend_check_relocs mips_elf_check_relocs
7428 #define elf_backend_adjust_dynamic_symbol \
7429 mips_elf_adjust_dynamic_symbol
7430 #define elf_backend_always_size_sections \
7431 mips_elf_always_size_sections
7432 #define elf_backend_size_dynamic_sections \
7433 mips_elf_size_dynamic_sections
7434 #define elf_backend_relocate_section mips_elf_relocate_section
7435 #define elf_backend_link_output_symbol_hook \
7436 mips_elf_link_output_symbol_hook
7437 #define elf_backend_finish_dynamic_symbol \
7438 mips_elf_finish_dynamic_symbol
7439 #define elf_backend_finish_dynamic_sections \
7440 mips_elf_finish_dynamic_sections
7441
7442 #include "elf32-target.h"