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