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