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