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