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