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