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