* elflink.h (struct elf_link_sort_rela): Turn rel and rela
[deliverable/binutils-gdb.git] / bfd / elfxx-mips.c
1 /* MIPS-specific support for ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 <ian@cygnus.com>.
7 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8 <mark@codesourcery.com>
9 Traditional MIPS targets support added by Koundinya.K, Dansk Data
10 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11
12 This file is part of BFD, the Binary File Descriptor library.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27
28 /* This file handles functionality common to the different MIPS ABI's. */
29
30 #include "bfd.h"
31 #include "sysdep.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elfxx-mips.h"
35 #include "elf/mips.h"
36
37 /* Get the ECOFF swapping routines. */
38 #include "coff/sym.h"
39 #include "coff/symconst.h"
40 #include "coff/ecoff.h"
41 #include "coff/mips.h"
42
43 /* This structure is used to hold .got information when linking. It
44 is stored in the tdata field of the bfd_elf_section_data structure. */
45
46 struct mips_got_info
47 {
48 /* The global symbol in the GOT with the lowest index in the dynamic
49 symbol table. */
50 struct elf_link_hash_entry *global_gotsym;
51 /* The number of global .got entries. */
52 unsigned int global_gotno;
53 /* The number of local .got entries. */
54 unsigned int local_gotno;
55 /* The number of local .got entries we have used. */
56 unsigned int assigned_gotno;
57 };
58
59 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
60 the dynamic symbols. */
61
62 struct mips_elf_hash_sort_data
63 {
64 /* The symbol in the global GOT with the lowest dynamic symbol table
65 index. */
66 struct elf_link_hash_entry *low;
67 /* The least dynamic symbol table index corresponding to a symbol
68 with a GOT entry. */
69 long min_got_dynindx;
70 /* The greatest dynamic symbol table index not corresponding to a
71 symbol without a GOT entry. */
72 long max_non_got_dynindx;
73 };
74
75 /* The MIPS ELF linker needs additional information for each symbol in
76 the global hash table. */
77
78 struct mips_elf_link_hash_entry
79 {
80 struct elf_link_hash_entry root;
81
82 /* External symbol information. */
83 EXTR esym;
84
85 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
86 this symbol. */
87 unsigned int possibly_dynamic_relocs;
88
89 /* If the R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 reloc is against
90 a readonly section. */
91 boolean readonly_reloc;
92
93 /* The index of the first dynamic relocation (in the .rel.dyn
94 section) against this symbol. */
95 unsigned int min_dyn_reloc_index;
96
97 /* We must not create a stub for a symbol that has relocations
98 related to taking the function's address, i.e. any but
99 R_MIPS_CALL*16 ones -- see "MIPS ABI Supplement, 3rd Edition",
100 p. 4-20. */
101 boolean no_fn_stub;
102
103 /* If there is a stub that 32 bit functions should use to call this
104 16 bit function, this points to the section containing the stub. */
105 asection *fn_stub;
106
107 /* Whether we need the fn_stub; this is set if this symbol appears
108 in any relocs other than a 16 bit call. */
109 boolean need_fn_stub;
110
111 /* If there is a stub that 16 bit functions should use to call this
112 32 bit function, this points to the section containing the stub. */
113 asection *call_stub;
114
115 /* This is like the call_stub field, but it is used if the function
116 being called returns a floating point value. */
117 asection *call_fp_stub;
118
119 /* Are we forced local? .*/
120 boolean forced_local;
121 };
122
123 /* MIPS ELF linker hash table. */
124
125 struct mips_elf_link_hash_table
126 {
127 struct elf_link_hash_table root;
128 #if 0
129 /* We no longer use this. */
130 /* String section indices for the dynamic section symbols. */
131 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
132 #endif
133 /* The number of .rtproc entries. */
134 bfd_size_type procedure_count;
135 /* The size of the .compact_rel section (if SGI_COMPAT). */
136 bfd_size_type compact_rel_size;
137 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
138 entry is set to the address of __rld_obj_head as in IRIX5. */
139 boolean use_rld_obj_head;
140 /* This is the value of the __rld_map or __rld_obj_head symbol. */
141 bfd_vma rld_value;
142 /* This is set if we see any mips16 stub sections. */
143 boolean mips16_stubs_seen;
144 };
145
146 /* Structure used to pass information to mips_elf_output_extsym. */
147
148 struct extsym_info
149 {
150 bfd *abfd;
151 struct bfd_link_info *info;
152 struct ecoff_debug_info *debug;
153 const struct ecoff_debug_swap *swap;
154 boolean failed;
155 };
156
157 /* The names of the runtime procedure table symbols used on IRIX5. */
158
159 static const char * const mips_elf_dynsym_rtproc_names[] =
160 {
161 "_procedure_table",
162 "_procedure_string_table",
163 "_procedure_table_size",
164 NULL
165 };
166
167 /* These structures are used to generate the .compact_rel section on
168 IRIX5. */
169
170 typedef struct
171 {
172 unsigned long id1; /* Always one? */
173 unsigned long num; /* Number of compact relocation entries. */
174 unsigned long id2; /* Always two? */
175 unsigned long offset; /* The file offset of the first relocation. */
176 unsigned long reserved0; /* Zero? */
177 unsigned long reserved1; /* Zero? */
178 } Elf32_compact_rel;
179
180 typedef struct
181 {
182 bfd_byte id1[4];
183 bfd_byte num[4];
184 bfd_byte id2[4];
185 bfd_byte offset[4];
186 bfd_byte reserved0[4];
187 bfd_byte reserved1[4];
188 } Elf32_External_compact_rel;
189
190 typedef struct
191 {
192 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
193 unsigned int rtype : 4; /* Relocation types. See below. */
194 unsigned int dist2to : 8;
195 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
196 unsigned long konst; /* KONST field. See below. */
197 unsigned long vaddr; /* VADDR to be relocated. */
198 } Elf32_crinfo;
199
200 typedef struct
201 {
202 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
203 unsigned int rtype : 4; /* Relocation types. See below. */
204 unsigned int dist2to : 8;
205 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
206 unsigned long konst; /* KONST field. See below. */
207 } Elf32_crinfo2;
208
209 typedef struct
210 {
211 bfd_byte info[4];
212 bfd_byte konst[4];
213 bfd_byte vaddr[4];
214 } Elf32_External_crinfo;
215
216 typedef struct
217 {
218 bfd_byte info[4];
219 bfd_byte konst[4];
220 } Elf32_External_crinfo2;
221
222 /* These are the constants used to swap the bitfields in a crinfo. */
223
224 #define CRINFO_CTYPE (0x1)
225 #define CRINFO_CTYPE_SH (31)
226 #define CRINFO_RTYPE (0xf)
227 #define CRINFO_RTYPE_SH (27)
228 #define CRINFO_DIST2TO (0xff)
229 #define CRINFO_DIST2TO_SH (19)
230 #define CRINFO_RELVADDR (0x7ffff)
231 #define CRINFO_RELVADDR_SH (0)
232
233 /* A compact relocation info has long (3 words) or short (2 words)
234 formats. A short format doesn't have VADDR field and relvaddr
235 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
236 #define CRF_MIPS_LONG 1
237 #define CRF_MIPS_SHORT 0
238
239 /* There are 4 types of compact relocation at least. The value KONST
240 has different meaning for each type:
241
242 (type) (konst)
243 CT_MIPS_REL32 Address in data
244 CT_MIPS_WORD Address in word (XXX)
245 CT_MIPS_GPHI_LO GP - vaddr
246 CT_MIPS_JMPAD Address to jump
247 */
248
249 #define CRT_MIPS_REL32 0xa
250 #define CRT_MIPS_WORD 0xb
251 #define CRT_MIPS_GPHI_LO 0xc
252 #define CRT_MIPS_JMPAD 0xd
253
254 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
255 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
256 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
257 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
258 \f
259 /* The structure of the runtime procedure descriptor created by the
260 loader for use by the static exception system. */
261
262 typedef struct runtime_pdr {
263 bfd_vma adr; /* memory address of start of procedure */
264 long regmask; /* save register mask */
265 long regoffset; /* save register offset */
266 long fregmask; /* save floating point register mask */
267 long fregoffset; /* save floating point register offset */
268 long frameoffset; /* frame size */
269 short framereg; /* frame pointer register */
270 short pcreg; /* offset or reg of return pc */
271 long irpss; /* index into the runtime string table */
272 long reserved;
273 struct exception_info *exception_info;/* pointer to exception array */
274 } RPDR, *pRPDR;
275 #define cbRPDR sizeof (RPDR)
276 #define rpdNil ((pRPDR) 0)
277 \f
278 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
279 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
280 static void ecoff_swap_rpdr_out
281 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
282 static boolean mips_elf_create_procedure_table
283 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
284 struct ecoff_debug_info *));
285 static boolean mips_elf_check_mips16_stubs
286 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
287 static void bfd_mips_elf32_swap_gptab_in
288 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
289 static void bfd_mips_elf32_swap_gptab_out
290 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
291 static void bfd_elf32_swap_compact_rel_out
292 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
293 static void bfd_elf32_swap_crinfo_out
294 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
295 #if 0
296 static void bfd_mips_elf_swap_msym_in
297 PARAMS ((bfd *, const Elf32_External_Msym *, Elf32_Internal_Msym *));
298 #endif
299 static void bfd_mips_elf_swap_msym_out
300 PARAMS ((bfd *, const Elf32_Internal_Msym *, Elf32_External_Msym *));
301 static int sort_dynamic_relocs
302 PARAMS ((const void *, const void *));
303 static boolean mips_elf_output_extsym
304 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
305 static int gptab_compare PARAMS ((const void *, const void *));
306 static asection * mips_elf_got_section PARAMS ((bfd *));
307 static struct mips_got_info *mips_elf_got_info
308 PARAMS ((bfd *, asection **));
309 static bfd_vma mips_elf_local_got_index
310 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
311 static bfd_vma mips_elf_global_got_index
312 PARAMS ((bfd *, struct elf_link_hash_entry *));
313 static bfd_vma mips_elf_got_page
314 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, bfd_vma *));
315 static bfd_vma mips_elf_got16_entry
316 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, boolean));
317 static bfd_vma mips_elf_got_offset_from_index
318 PARAMS ((bfd *, bfd *, bfd_vma));
319 static bfd_vma mips_elf_create_local_got_entry
320 PARAMS ((bfd *, struct mips_got_info *, asection *, bfd_vma));
321 static boolean mips_elf_sort_hash_table
322 PARAMS ((struct bfd_link_info *, unsigned long));
323 static boolean mips_elf_sort_hash_table_f
324 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
325 static boolean mips_elf_record_global_got_symbol
326 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *,
327 struct mips_got_info *));
328 static const Elf_Internal_Rela *mips_elf_next_relocation
329 PARAMS ((bfd *, unsigned int, const Elf_Internal_Rela *,
330 const Elf_Internal_Rela *));
331 static boolean mips_elf_local_relocation_p
332 PARAMS ((bfd *, const Elf_Internal_Rela *, asection **, boolean));
333 static bfd_vma mips_elf_sign_extend PARAMS ((bfd_vma, int));
334 static boolean mips_elf_overflow_p PARAMS ((bfd_vma, int));
335 static bfd_vma mips_elf_high PARAMS ((bfd_vma));
336 static bfd_vma mips_elf_higher PARAMS ((bfd_vma));
337 static bfd_vma mips_elf_highest PARAMS ((bfd_vma));
338 static boolean mips_elf_create_compact_rel_section
339 PARAMS ((bfd *, struct bfd_link_info *));
340 static boolean mips_elf_create_got_section
341 PARAMS ((bfd *, struct bfd_link_info *));
342 static asection *mips_elf_create_msym_section
343 PARAMS ((bfd *));
344 static bfd_reloc_status_type mips_elf_calculate_relocation
345 PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *,
346 const Elf_Internal_Rela *, bfd_vma, reloc_howto_type *,
347 Elf_Internal_Sym *, asection **, bfd_vma *, const char **,
348 boolean *, 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, save_addend)
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 boolean save_addend;
2023 {
2024 /* The eventual value we will return. */
2025 bfd_vma value;
2026 /* The address of the symbol against which the relocation is
2027 occurring. */
2028 bfd_vma symbol = 0;
2029 /* The final GP value to be used for the relocatable, executable, or
2030 shared object file being produced. */
2031 bfd_vma gp = MINUS_ONE;
2032 /* The place (section offset or address) of the storage unit being
2033 relocated. */
2034 bfd_vma p;
2035 /* The value of GP used to create the relocatable object. */
2036 bfd_vma gp0 = MINUS_ONE;
2037 /* The offset into the global offset table at which the address of
2038 the relocation entry symbol, adjusted by the addend, resides
2039 during execution. */
2040 bfd_vma g = MINUS_ONE;
2041 /* The section in which the symbol referenced by the relocation is
2042 located. */
2043 asection *sec = NULL;
2044 struct mips_elf_link_hash_entry *h = NULL;
2045 /* True if the symbol referred to by this relocation is a local
2046 symbol. */
2047 boolean local_p, was_local_p;
2048 /* True if the symbol referred to by this relocation is "_gp_disp". */
2049 boolean gp_disp_p = false;
2050 Elf_Internal_Shdr *symtab_hdr;
2051 size_t extsymoff;
2052 unsigned long r_symndx;
2053 int r_type;
2054 /* True if overflow occurred during the calculation of the
2055 relocation value. */
2056 boolean overflowed_p;
2057 /* True if this relocation refers to a MIPS16 function. */
2058 boolean target_is_16_bit_code_p = false;
2059
2060 /* Parse the relocation. */
2061 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
2062 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
2063 p = (input_section->output_section->vma
2064 + input_section->output_offset
2065 + relocation->r_offset);
2066
2067 /* Assume that there will be no overflow. */
2068 overflowed_p = false;
2069
2070 /* Figure out whether or not the symbol is local, and get the offset
2071 used in the array of hash table entries. */
2072 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2073 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
2074 local_sections, false);
2075 was_local_p = local_p;
2076 if (! elf_bad_symtab (input_bfd))
2077 extsymoff = symtab_hdr->sh_info;
2078 else
2079 {
2080 /* The symbol table does not follow the rule that local symbols
2081 must come before globals. */
2082 extsymoff = 0;
2083 }
2084
2085 /* Figure out the value of the symbol. */
2086 if (local_p)
2087 {
2088 Elf_Internal_Sym *sym;
2089
2090 sym = local_syms + r_symndx;
2091 sec = local_sections[r_symndx];
2092
2093 symbol = sec->output_section->vma + sec->output_offset;
2094 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
2095 || (sec->flags & SEC_MERGE))
2096 symbol += sym->st_value;
2097 if ((sec->flags & SEC_MERGE)
2098 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2099 {
2100 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
2101 addend -= symbol;
2102 addend += sec->output_section->vma + sec->output_offset;
2103 }
2104
2105 /* MIPS16 text labels should be treated as odd. */
2106 if (sym->st_other == STO_MIPS16)
2107 ++symbol;
2108
2109 /* Record the name of this symbol, for our caller. */
2110 *namep = bfd_elf_string_from_elf_section (input_bfd,
2111 symtab_hdr->sh_link,
2112 sym->st_name);
2113 if (*namep == '\0')
2114 *namep = bfd_section_name (input_bfd, sec);
2115
2116 target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
2117 }
2118 else
2119 {
2120 /* For global symbols we look up the symbol in the hash-table. */
2121 h = ((struct mips_elf_link_hash_entry *)
2122 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
2123 /* Find the real hash-table entry for this symbol. */
2124 while (h->root.root.type == bfd_link_hash_indirect
2125 || h->root.root.type == bfd_link_hash_warning)
2126 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2127
2128 /* Record the name of this symbol, for our caller. */
2129 *namep = h->root.root.root.string;
2130
2131 /* See if this is the special _gp_disp symbol. Note that such a
2132 symbol must always be a global symbol. */
2133 if (strcmp (h->root.root.root.string, "_gp_disp") == 0
2134 && ! NEWABI_P (input_bfd))
2135 {
2136 /* Relocations against _gp_disp are permitted only with
2137 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
2138 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16)
2139 return bfd_reloc_notsupported;
2140
2141 gp_disp_p = true;
2142 }
2143 /* If this symbol is defined, calculate its address. Note that
2144 _gp_disp is a magic symbol, always implicitly defined by the
2145 linker, so it's inappropriate to check to see whether or not
2146 its defined. */
2147 else if ((h->root.root.type == bfd_link_hash_defined
2148 || h->root.root.type == bfd_link_hash_defweak)
2149 && h->root.root.u.def.section)
2150 {
2151 sec = h->root.root.u.def.section;
2152 if (sec->output_section)
2153 symbol = (h->root.root.u.def.value
2154 + sec->output_section->vma
2155 + sec->output_offset);
2156 else
2157 symbol = h->root.root.u.def.value;
2158 }
2159 else if (h->root.root.type == bfd_link_hash_undefweak)
2160 /* We allow relocations against undefined weak symbols, giving
2161 it the value zero, so that you can undefined weak functions
2162 and check to see if they exist by looking at their
2163 addresses. */
2164 symbol = 0;
2165 else if (info->shared
2166 && (!info->symbolic || info->allow_shlib_undefined)
2167 && !info->no_undefined
2168 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
2169 symbol = 0;
2170 else if (strcmp (h->root.root.root.string, "_DYNAMIC_LINK") == 0 ||
2171 strcmp (h->root.root.root.string, "_DYNAMIC_LINKING") == 0)
2172 {
2173 /* If this is a dynamic link, we should have created a
2174 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
2175 in in _bfd_mips_elf_create_dynamic_sections.
2176 Otherwise, we should define the symbol with a value of 0.
2177 FIXME: It should probably get into the symbol table
2178 somehow as well. */
2179 BFD_ASSERT (! info->shared);
2180 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
2181 symbol = 0;
2182 }
2183 else
2184 {
2185 if (! ((*info->callbacks->undefined_symbol)
2186 (info, h->root.root.root.string, input_bfd,
2187 input_section, relocation->r_offset,
2188 (!info->shared || info->no_undefined
2189 || ELF_ST_VISIBILITY (h->root.other)))))
2190 return bfd_reloc_undefined;
2191 symbol = 0;
2192 }
2193
2194 target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
2195 }
2196
2197 /* If this is a 32- or 64-bit call to a 16-bit function with a stub, we
2198 need to redirect the call to the stub, unless we're already *in*
2199 a stub. */
2200 if (r_type != R_MIPS16_26 && !info->relocateable
2201 && ((h != NULL && h->fn_stub != NULL)
2202 || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
2203 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
2204 && !mips_elf_stub_section_p (input_bfd, input_section))
2205 {
2206 /* This is a 32- or 64-bit call to a 16-bit function. We should
2207 have already noticed that we were going to need the
2208 stub. */
2209 if (local_p)
2210 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
2211 else
2212 {
2213 BFD_ASSERT (h->need_fn_stub);
2214 sec = h->fn_stub;
2215 }
2216
2217 symbol = sec->output_section->vma + sec->output_offset;
2218 }
2219 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
2220 need to redirect the call to the stub. */
2221 else if (r_type == R_MIPS16_26 && !info->relocateable
2222 && h != NULL
2223 && (h->call_stub != NULL || h->call_fp_stub != NULL)
2224 && !target_is_16_bit_code_p)
2225 {
2226 /* If both call_stub and call_fp_stub are defined, we can figure
2227 out which one to use by seeing which one appears in the input
2228 file. */
2229 if (h->call_stub != NULL && h->call_fp_stub != NULL)
2230 {
2231 asection *o;
2232
2233 sec = NULL;
2234 for (o = input_bfd->sections; o != NULL; o = o->next)
2235 {
2236 if (strncmp (bfd_get_section_name (input_bfd, o),
2237 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
2238 {
2239 sec = h->call_fp_stub;
2240 break;
2241 }
2242 }
2243 if (sec == NULL)
2244 sec = h->call_stub;
2245 }
2246 else if (h->call_stub != NULL)
2247 sec = h->call_stub;
2248 else
2249 sec = h->call_fp_stub;
2250
2251 BFD_ASSERT (sec->_raw_size > 0);
2252 symbol = sec->output_section->vma + sec->output_offset;
2253 }
2254
2255 /* Calls from 16-bit code to 32-bit code and vice versa require the
2256 special jalx instruction. */
2257 *require_jalxp = (!info->relocateable
2258 && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
2259 || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
2260
2261 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
2262 local_sections, true);
2263
2264 /* If we haven't already determined the GOT offset, or the GP value,
2265 and we're going to need it, get it now. */
2266 switch (r_type)
2267 {
2268 case R_MIPS_CALL16:
2269 case R_MIPS_GOT16:
2270 case R_MIPS_GOT_DISP:
2271 case R_MIPS_GOT_HI16:
2272 case R_MIPS_CALL_HI16:
2273 case R_MIPS_GOT_LO16:
2274 case R_MIPS_CALL_LO16:
2275 /* Find the index into the GOT where this value is located. */
2276 if (!local_p)
2277 {
2278 BFD_ASSERT (addend == 0);
2279 g = mips_elf_global_got_index (elf_hash_table (info)->dynobj,
2280 (struct elf_link_hash_entry *) h);
2281 if (! elf_hash_table(info)->dynamic_sections_created
2282 || (info->shared
2283 && (info->symbolic || h->root.dynindx == -1)
2284 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2285 {
2286 /* This is a static link or a -Bsymbolic link. The
2287 symbol is defined locally, or was forced to be local.
2288 We must initialize this entry in the GOT. */
2289 bfd *tmpbfd = elf_hash_table (info)->dynobj;
2290 asection *sgot = mips_elf_got_section(tmpbfd);
2291 MIPS_ELF_PUT_WORD (tmpbfd, symbol + addend, sgot->contents + g);
2292 }
2293 }
2294 else if (r_type == R_MIPS_GOT16 || r_type == R_MIPS_CALL16)
2295 /* There's no need to create a local GOT entry here; the
2296 calculation for a local GOT16 entry does not involve G. */
2297 break;
2298 else
2299 {
2300 g = mips_elf_local_got_index (abfd, info, symbol + addend);
2301 if (g == MINUS_ONE)
2302 return bfd_reloc_outofrange;
2303 }
2304
2305 /* Convert GOT indices to actual offsets. */
2306 g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
2307 abfd, g);
2308 break;
2309
2310 case R_MIPS_HI16:
2311 case R_MIPS_LO16:
2312 case R_MIPS16_GPREL:
2313 case R_MIPS_GPREL16:
2314 case R_MIPS_GPREL32:
2315 case R_MIPS_LITERAL:
2316 gp0 = _bfd_get_gp_value (input_bfd);
2317 gp = _bfd_get_gp_value (abfd);
2318 break;
2319
2320 default:
2321 break;
2322 }
2323
2324 /* Figure out what kind of relocation is being performed. */
2325 switch (r_type)
2326 {
2327 case R_MIPS_NONE:
2328 return bfd_reloc_continue;
2329
2330 case R_MIPS_16:
2331 value = symbol + mips_elf_sign_extend (addend, 16);
2332 overflowed_p = mips_elf_overflow_p (value, 16);
2333 break;
2334
2335 case R_MIPS_32:
2336 case R_MIPS_REL32:
2337 case R_MIPS_64:
2338 if ((info->shared
2339 || (elf_hash_table (info)->dynamic_sections_created
2340 && h != NULL
2341 && ((h->root.elf_link_hash_flags
2342 & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
2343 && ((h->root.elf_link_hash_flags
2344 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
2345 && r_symndx != 0
2346 && (input_section->flags & SEC_ALLOC) != 0)
2347 {
2348 /* If we're creating a shared library, or this relocation is
2349 against a symbol in a shared library, then we can't know
2350 where the symbol will end up. So, we create a relocation
2351 record in the output, and leave the job up to the dynamic
2352 linker. */
2353 value = addend;
2354 if (!mips_elf_create_dynamic_relocation (abfd,
2355 info,
2356 relocation,
2357 h,
2358 sec,
2359 symbol,
2360 &value,
2361 input_section))
2362 return bfd_reloc_undefined;
2363 }
2364 else
2365 {
2366 if (r_type != R_MIPS_REL32)
2367 value = symbol + addend;
2368 else
2369 value = addend;
2370 }
2371 value &= howto->dst_mask;
2372 break;
2373
2374 case R_MIPS_PC32:
2375 case R_MIPS_PC64:
2376 case R_MIPS_GNU_REL_LO16:
2377 value = symbol + addend - p;
2378 value &= howto->dst_mask;
2379 break;
2380
2381 case R_MIPS_GNU_REL16_S2:
2382 value = symbol + mips_elf_sign_extend (addend << 2, 18) - p;
2383 overflowed_p = mips_elf_overflow_p (value, 18);
2384 value = (value >> 2) & howto->dst_mask;
2385 break;
2386
2387 case R_MIPS_GNU_REL_HI16:
2388 /* Instead of subtracting 'p' here, we should be subtracting the
2389 equivalent value for the LO part of the reloc, since the value
2390 here is relative to that address. Because that's not easy to do,
2391 we adjust 'addend' in _bfd_mips_elf_relocate_section(). See also
2392 the comment there for more information. */
2393 value = mips_elf_high (addend + symbol - p);
2394 value &= howto->dst_mask;
2395 break;
2396
2397 case R_MIPS16_26:
2398 /* The calculation for R_MIPS16_26 is just the same as for an
2399 R_MIPS_26. It's only the storage of the relocated field into
2400 the output file that's different. That's handled in
2401 mips_elf_perform_relocation. So, we just fall through to the
2402 R_MIPS_26 case here. */
2403 case R_MIPS_26:
2404 if (local_p)
2405 value = (((addend << 2) | ((p + 4) & 0xf0000000)) + symbol) >> 2;
2406 else
2407 value = (mips_elf_sign_extend (addend << 2, 28) + symbol) >> 2;
2408 value &= howto->dst_mask;
2409 break;
2410
2411 case R_MIPS_HI16:
2412 if (!gp_disp_p)
2413 {
2414 value = mips_elf_high (addend + symbol);
2415 value &= howto->dst_mask;
2416 }
2417 else
2418 {
2419 value = mips_elf_high (addend + gp - p);
2420 overflowed_p = mips_elf_overflow_p (value, 16);
2421 }
2422 break;
2423
2424 case R_MIPS_LO16:
2425 if (!gp_disp_p)
2426 value = (symbol + addend) & howto->dst_mask;
2427 else
2428 {
2429 value = addend + gp - p + 4;
2430 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
2431 for overflow. But, on, say, IRIX5, relocations against
2432 _gp_disp are normally generated from the .cpload
2433 pseudo-op. It generates code that normally looks like
2434 this:
2435
2436 lui $gp,%hi(_gp_disp)
2437 addiu $gp,$gp,%lo(_gp_disp)
2438 addu $gp,$gp,$t9
2439
2440 Here $t9 holds the address of the function being called,
2441 as required by the MIPS ELF ABI. The R_MIPS_LO16
2442 relocation can easily overflow in this situation, but the
2443 R_MIPS_HI16 relocation will handle the overflow.
2444 Therefore, we consider this a bug in the MIPS ABI, and do
2445 not check for overflow here. */
2446 }
2447 break;
2448
2449 case R_MIPS_LITERAL:
2450 /* Because we don't merge literal sections, we can handle this
2451 just like R_MIPS_GPREL16. In the long run, we should merge
2452 shared literals, and then we will need to additional work
2453 here. */
2454
2455 /* Fall through. */
2456
2457 case R_MIPS16_GPREL:
2458 /* The R_MIPS16_GPREL performs the same calculation as
2459 R_MIPS_GPREL16, but stores the relocated bits in a different
2460 order. We don't need to do anything special here; the
2461 differences are handled in mips_elf_perform_relocation. */
2462 case R_MIPS_GPREL16:
2463 /* Only sign-extend the addend if it was extracted from the
2464 instruction. If the addend was separate, leave it alone,
2465 otherwise we may lose significant bits. */
2466 if (howto->partial_inplace)
2467 addend = mips_elf_sign_extend (addend, 16);
2468 value = symbol + addend - gp;
2469 /* If the symbol was local, any earlier relocatable links will
2470 have adjusted its addend with the gp offset, so compensate
2471 for that now. Don't do it for symbols forced local in this
2472 link, though, since they won't have had the gp offset applied
2473 to them before. */
2474 if (was_local_p)
2475 value += gp0;
2476 overflowed_p = mips_elf_overflow_p (value, 16);
2477 break;
2478
2479 case R_MIPS_GOT16:
2480 case R_MIPS_CALL16:
2481 if (local_p)
2482 {
2483 boolean forced;
2484
2485 /* The special case is when the symbol is forced to be local. We
2486 need the full address in the GOT since no R_MIPS_LO16 relocation
2487 follows. */
2488 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
2489 local_sections, false);
2490 value = mips_elf_got16_entry (abfd, info, symbol + addend, forced);
2491 if (value == MINUS_ONE)
2492 return bfd_reloc_outofrange;
2493 value
2494 = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
2495 abfd, value);
2496 overflowed_p = mips_elf_overflow_p (value, 16);
2497 break;
2498 }
2499
2500 /* Fall through. */
2501
2502 case R_MIPS_GOT_DISP:
2503 value = g;
2504 overflowed_p = mips_elf_overflow_p (value, 16);
2505 break;
2506
2507 case R_MIPS_GPREL32:
2508 value = (addend + symbol + gp0 - gp);
2509 if (!save_addend)
2510 value &= howto->dst_mask;
2511 break;
2512
2513 case R_MIPS_PC16:
2514 value = mips_elf_sign_extend (addend, 16) + symbol - p;
2515 overflowed_p = mips_elf_overflow_p (value, 16);
2516 value = (bfd_vma) ((bfd_signed_vma) value / 4);
2517 break;
2518
2519 case R_MIPS_GOT_HI16:
2520 case R_MIPS_CALL_HI16:
2521 /* We're allowed to handle these two relocations identically.
2522 The dynamic linker is allowed to handle the CALL relocations
2523 differently by creating a lazy evaluation stub. */
2524 value = g;
2525 value = mips_elf_high (value);
2526 value &= howto->dst_mask;
2527 break;
2528
2529 case R_MIPS_GOT_LO16:
2530 case R_MIPS_CALL_LO16:
2531 value = g & howto->dst_mask;
2532 break;
2533
2534 case R_MIPS_GOT_PAGE:
2535 value = mips_elf_got_page (abfd, info, symbol + addend, NULL);
2536 if (value == MINUS_ONE)
2537 return bfd_reloc_outofrange;
2538 value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
2539 abfd, value);
2540 overflowed_p = mips_elf_overflow_p (value, 16);
2541 break;
2542
2543 case R_MIPS_GOT_OFST:
2544 mips_elf_got_page (abfd, info, symbol + addend, &value);
2545 overflowed_p = mips_elf_overflow_p (value, 16);
2546 break;
2547
2548 case R_MIPS_SUB:
2549 value = symbol - addend;
2550 value &= howto->dst_mask;
2551 break;
2552
2553 case R_MIPS_HIGHER:
2554 value = mips_elf_higher (addend + symbol);
2555 value &= howto->dst_mask;
2556 break;
2557
2558 case R_MIPS_HIGHEST:
2559 value = mips_elf_highest (addend + symbol);
2560 value &= howto->dst_mask;
2561 break;
2562
2563 case R_MIPS_SCN_DISP:
2564 value = symbol + addend - sec->output_offset;
2565 value &= howto->dst_mask;
2566 break;
2567
2568 case R_MIPS_PJUMP:
2569 case R_MIPS_JALR:
2570 /* Both of these may be ignored. R_MIPS_JALR is an optimization
2571 hint; we could improve performance by honoring that hint. */
2572 return bfd_reloc_continue;
2573
2574 case R_MIPS_GNU_VTINHERIT:
2575 case R_MIPS_GNU_VTENTRY:
2576 /* We don't do anything with these at present. */
2577 return bfd_reloc_continue;
2578
2579 default:
2580 /* An unrecognized relocation type. */
2581 return bfd_reloc_notsupported;
2582 }
2583
2584 /* Store the VALUE for our caller. */
2585 *valuep = value;
2586 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
2587 }
2588
2589 /* Obtain the field relocated by RELOCATION. */
2590
2591 static bfd_vma
2592 mips_elf_obtain_contents (howto, relocation, input_bfd, contents)
2593 reloc_howto_type *howto;
2594 const Elf_Internal_Rela *relocation;
2595 bfd *input_bfd;
2596 bfd_byte *contents;
2597 {
2598 bfd_vma x;
2599 bfd_byte *location = contents + relocation->r_offset;
2600
2601 /* Obtain the bytes. */
2602 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
2603
2604 if ((ELF_R_TYPE (input_bfd, relocation->r_info) == R_MIPS16_26
2605 || ELF_R_TYPE (input_bfd, relocation->r_info) == R_MIPS16_GPREL)
2606 && bfd_little_endian (input_bfd))
2607 /* The two 16-bit words will be reversed on a little-endian system.
2608 See mips_elf_perform_relocation for more details. */
2609 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
2610
2611 return x;
2612 }
2613
2614 /* It has been determined that the result of the RELOCATION is the
2615 VALUE. Use HOWTO to place VALUE into the output file at the
2616 appropriate position. The SECTION is the section to which the
2617 relocation applies. If REQUIRE_JALX is true, then the opcode used
2618 for the relocation must be either JAL or JALX, and it is
2619 unconditionally converted to JALX.
2620
2621 Returns false if anything goes wrong. */
2622
2623 static boolean
2624 mips_elf_perform_relocation (info, howto, relocation, value, input_bfd,
2625 input_section, contents, require_jalx)
2626 struct bfd_link_info *info;
2627 reloc_howto_type *howto;
2628 const Elf_Internal_Rela *relocation;
2629 bfd_vma value;
2630 bfd *input_bfd;
2631 asection *input_section;
2632 bfd_byte *contents;
2633 boolean require_jalx;
2634 {
2635 bfd_vma x;
2636 bfd_byte *location;
2637 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
2638
2639 /* Figure out where the relocation is occurring. */
2640 location = contents + relocation->r_offset;
2641
2642 /* Obtain the current value. */
2643 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
2644
2645 /* Clear the field we are setting. */
2646 x &= ~howto->dst_mask;
2647
2648 /* If this is the R_MIPS16_26 relocation, we must store the
2649 value in a funny way. */
2650 if (r_type == R_MIPS16_26)
2651 {
2652 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2653 Most mips16 instructions are 16 bits, but these instructions
2654 are 32 bits.
2655
2656 The format of these instructions is:
2657
2658 +--------------+--------------------------------+
2659 ! JALX ! X! Imm 20:16 ! Imm 25:21 !
2660 +--------------+--------------------------------+
2661 ! Immediate 15:0 !
2662 +-----------------------------------------------+
2663
2664 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2665 Note that the immediate value in the first word is swapped.
2666
2667 When producing a relocateable object file, R_MIPS16_26 is
2668 handled mostly like R_MIPS_26. In particular, the addend is
2669 stored as a straight 26-bit value in a 32-bit instruction.
2670 (gas makes life simpler for itself by never adjusting a
2671 R_MIPS16_26 reloc to be against a section, so the addend is
2672 always zero). However, the 32 bit instruction is stored as 2
2673 16-bit values, rather than a single 32-bit value. In a
2674 big-endian file, the result is the same; in a little-endian
2675 file, the two 16-bit halves of the 32 bit value are swapped.
2676 This is so that a disassembler can recognize the jal
2677 instruction.
2678
2679 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2680 instruction stored as two 16-bit values. The addend A is the
2681 contents of the targ26 field. The calculation is the same as
2682 R_MIPS_26. When storing the calculated value, reorder the
2683 immediate value as shown above, and don't forget to store the
2684 value as two 16-bit values.
2685
2686 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2687 defined as
2688
2689 big-endian:
2690 +--------+----------------------+
2691 | | |
2692 | | targ26-16 |
2693 |31 26|25 0|
2694 +--------+----------------------+
2695
2696 little-endian:
2697 +----------+------+-------------+
2698 | | | |
2699 | sub1 | | sub2 |
2700 |0 9|10 15|16 31|
2701 +----------+--------------------+
2702 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2703 ((sub1 << 16) | sub2)).
2704
2705 When producing a relocateable object file, the calculation is
2706 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2707 When producing a fully linked file, the calculation is
2708 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2709 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff) */
2710
2711 if (!info->relocateable)
2712 /* Shuffle the bits according to the formula above. */
2713 value = (((value & 0x1f0000) << 5)
2714 | ((value & 0x3e00000) >> 5)
2715 | (value & 0xffff));
2716 }
2717 else if (r_type == R_MIPS16_GPREL)
2718 {
2719 /* R_MIPS16_GPREL is used for GP-relative addressing in mips16
2720 mode. A typical instruction will have a format like this:
2721
2722 +--------------+--------------------------------+
2723 ! EXTEND ! Imm 10:5 ! Imm 15:11 !
2724 +--------------+--------------------------------+
2725 ! Major ! rx ! ry ! Imm 4:0 !
2726 +--------------+--------------------------------+
2727
2728 EXTEND is the five bit value 11110. Major is the instruction
2729 opcode.
2730
2731 This is handled exactly like R_MIPS_GPREL16, except that the
2732 addend is retrieved and stored as shown in this diagram; that
2733 is, the Imm fields above replace the V-rel16 field.
2734
2735 All we need to do here is shuffle the bits appropriately. As
2736 above, the two 16-bit halves must be swapped on a
2737 little-endian system. */
2738 value = (((value & 0x7e0) << 16)
2739 | ((value & 0xf800) << 5)
2740 | (value & 0x1f));
2741 }
2742
2743 /* Set the field. */
2744 x |= (value & howto->dst_mask);
2745
2746 /* If required, turn JAL into JALX. */
2747 if (require_jalx)
2748 {
2749 boolean ok;
2750 bfd_vma opcode = x >> 26;
2751 bfd_vma jalx_opcode;
2752
2753 /* Check to see if the opcode is already JAL or JALX. */
2754 if (r_type == R_MIPS16_26)
2755 {
2756 ok = ((opcode == 0x6) || (opcode == 0x7));
2757 jalx_opcode = 0x7;
2758 }
2759 else
2760 {
2761 ok = ((opcode == 0x3) || (opcode == 0x1d));
2762 jalx_opcode = 0x1d;
2763 }
2764
2765 /* If the opcode is not JAL or JALX, there's a problem. */
2766 if (!ok)
2767 {
2768 (*_bfd_error_handler)
2769 (_("%s: %s+0x%lx: jump to stub routine which is not jal"),
2770 bfd_archive_filename (input_bfd),
2771 input_section->name,
2772 (unsigned long) relocation->r_offset);
2773 bfd_set_error (bfd_error_bad_value);
2774 return false;
2775 }
2776
2777 /* Make this the JALX opcode. */
2778 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
2779 }
2780
2781 /* Swap the high- and low-order 16 bits on little-endian systems
2782 when doing a MIPS16 relocation. */
2783 if ((r_type == R_MIPS16_GPREL || r_type == R_MIPS16_26)
2784 && bfd_little_endian (input_bfd))
2785 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
2786
2787 /* Put the value into the output. */
2788 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
2789 return true;
2790 }
2791
2792 /* Returns true if SECTION is a MIPS16 stub section. */
2793
2794 static boolean
2795 mips_elf_stub_section_p (abfd, section)
2796 bfd *abfd ATTRIBUTE_UNUSED;
2797 asection *section;
2798 {
2799 const char *name = bfd_get_section_name (abfd, section);
2800
2801 return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
2802 || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
2803 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
2804 }
2805 \f
2806 /* Add room for N relocations to the .rel.dyn section in ABFD. */
2807
2808 static void
2809 mips_elf_allocate_dynamic_relocations (abfd, n)
2810 bfd *abfd;
2811 unsigned int n;
2812 {
2813 asection *s;
2814
2815 s = bfd_get_section_by_name (abfd, ".rel.dyn");
2816 BFD_ASSERT (s != NULL);
2817
2818 if (s->_raw_size == 0)
2819 {
2820 /* Make room for a null element. */
2821 s->_raw_size += MIPS_ELF_REL_SIZE (abfd);
2822 ++s->reloc_count;
2823 }
2824 s->_raw_size += n * MIPS_ELF_REL_SIZE (abfd);
2825 }
2826
2827 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
2828 is the original relocation, which is now being transformed into a
2829 dynamic relocation. The ADDENDP is adjusted if necessary; the
2830 caller should store the result in place of the original addend. */
2831
2832 static boolean
2833 mips_elf_create_dynamic_relocation (output_bfd, info, rel, h, sec,
2834 symbol, addendp, input_section)
2835 bfd *output_bfd;
2836 struct bfd_link_info *info;
2837 const Elf_Internal_Rela *rel;
2838 struct mips_elf_link_hash_entry *h;
2839 asection *sec;
2840 bfd_vma symbol;
2841 bfd_vma *addendp;
2842 asection *input_section;
2843 {
2844 Elf_Internal_Rel outrel[3];
2845 boolean skip;
2846 asection *sreloc;
2847 bfd *dynobj;
2848 int r_type;
2849
2850 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
2851 dynobj = elf_hash_table (info)->dynobj;
2852 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
2853 BFD_ASSERT (sreloc != NULL);
2854 BFD_ASSERT (sreloc->contents != NULL);
2855 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
2856 < sreloc->_raw_size);
2857
2858 skip = false;
2859 outrel[0].r_offset =
2860 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
2861 outrel[1].r_offset =
2862 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
2863 outrel[2].r_offset =
2864 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
2865
2866 #if 0
2867 /* We begin by assuming that the offset for the dynamic relocation
2868 is the same as for the original relocation. We'll adjust this
2869 later to reflect the correct output offsets. */
2870 if (elf_section_data (input_section)->sec_info_type != ELF_INFO_TYPE_STABS)
2871 {
2872 outrel[1].r_offset = rel[1].r_offset;
2873 outrel[2].r_offset = rel[2].r_offset;
2874 }
2875 else
2876 {
2877 /* Except that in a stab section things are more complex.
2878 Because we compress stab information, the offset given in the
2879 relocation may not be the one we want; we must let the stabs
2880 machinery tell us the offset. */
2881 outrel[1].r_offset = outrel[0].r_offset;
2882 outrel[2].r_offset = outrel[0].r_offset;
2883 /* If we didn't need the relocation at all, this value will be
2884 -1. */
2885 if (outrel[0].r_offset == (bfd_vma) -1)
2886 skip = true;
2887 }
2888 #endif
2889
2890 if (outrel[0].r_offset == (bfd_vma) -1)
2891 skip = true;
2892 /* FIXME: For -2 runtime relocation needs to be skipped, but
2893 properly resolved statically and installed. */
2894 BFD_ASSERT (outrel[0].r_offset != (bfd_vma) -2);
2895
2896 /* If we've decided to skip this relocation, just output an empty
2897 record. Note that R_MIPS_NONE == 0, so that this call to memset
2898 is a way of setting R_TYPE to R_MIPS_NONE. */
2899 if (skip)
2900 memset (outrel, 0, sizeof (Elf_Internal_Rel) * 3);
2901 else
2902 {
2903 long indx;
2904 bfd_vma section_offset;
2905
2906 /* We must now calculate the dynamic symbol table index to use
2907 in the relocation. */
2908 if (h != NULL
2909 && (! info->symbolic || (h->root.elf_link_hash_flags
2910 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2911 {
2912 indx = h->root.dynindx;
2913 /* h->root.dynindx may be -1 if this symbol was marked to
2914 become local. */
2915 if (indx == -1)
2916 indx = 0;
2917 }
2918 else
2919 {
2920 if (sec != NULL && bfd_is_abs_section (sec))
2921 indx = 0;
2922 else if (sec == NULL || sec->owner == NULL)
2923 {
2924 bfd_set_error (bfd_error_bad_value);
2925 return false;
2926 }
2927 else
2928 {
2929 indx = elf_section_data (sec->output_section)->dynindx;
2930 if (indx == 0)
2931 abort ();
2932 }
2933
2934 /* Figure out how far the target of the relocation is from
2935 the beginning of its section. */
2936 section_offset = symbol - sec->output_section->vma;
2937 /* The relocation we're building is section-relative.
2938 Therefore, the original addend must be adjusted by the
2939 section offset. */
2940 *addendp += section_offset;
2941 /* Now, the relocation is just against the section. */
2942 symbol = sec->output_section->vma;
2943 }
2944
2945 /* If the relocation was previously an absolute relocation and
2946 this symbol will not be referred to by the relocation, we must
2947 adjust it by the value we give it in the dynamic symbol table.
2948 Otherwise leave the job up to the dynamic linker. */
2949 if (!indx && r_type != R_MIPS_REL32)
2950 *addendp += symbol;
2951
2952 /* The relocation is always an REL32 relocation because we don't
2953 know where the shared library will wind up at load-time. */
2954 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
2955 R_MIPS_REL32);
2956 outrel[1].r_info = ELF_R_INFO (output_bfd, (unsigned long) 0,
2957 ABI_64_P (output_bfd)
2958 ? R_MIPS_64
2959 : R_MIPS_NONE);
2960 outrel[2].r_info = ELF_R_INFO (output_bfd, (unsigned long) 0,
2961 R_MIPS_NONE);
2962
2963 /* Adjust the output offset of the relocation to reference the
2964 correct location in the output file. */
2965 outrel[0].r_offset += (input_section->output_section->vma
2966 + input_section->output_offset);
2967 outrel[1].r_offset += (input_section->output_section->vma
2968 + input_section->output_offset);
2969 outrel[2].r_offset += (input_section->output_section->vma
2970 + input_section->output_offset);
2971 }
2972
2973 /* Put the relocation back out. We have to use the special
2974 relocation outputter in the 64-bit case since the 64-bit
2975 relocation format is non-standard. */
2976 if (ABI_64_P (output_bfd))
2977 {
2978 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2979 (output_bfd, &outrel[0],
2980 (sreloc->contents
2981 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
2982 }
2983 else
2984 bfd_elf32_swap_reloc_out (output_bfd, &outrel[0],
2985 (((Elf32_External_Rel *)
2986 sreloc->contents)
2987 + sreloc->reloc_count));
2988
2989 /* Record the index of the first relocation referencing H. This
2990 information is later emitted in the .msym section. */
2991 if (h != NULL
2992 && (h->min_dyn_reloc_index == 0
2993 || sreloc->reloc_count < h->min_dyn_reloc_index))
2994 h->min_dyn_reloc_index = sreloc->reloc_count;
2995
2996 /* We've now added another relocation. */
2997 ++sreloc->reloc_count;
2998
2999 /* Make sure the output section is writable. The dynamic linker
3000 will be writing to it. */
3001 elf_section_data (input_section->output_section)->this_hdr.sh_flags
3002 |= SHF_WRITE;
3003
3004 /* On IRIX5, make an entry of compact relocation info. */
3005 if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5)
3006 {
3007 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
3008 bfd_byte *cr;
3009
3010 if (scpt)
3011 {
3012 Elf32_crinfo cptrel;
3013
3014 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
3015 cptrel.vaddr = (rel->r_offset
3016 + input_section->output_section->vma
3017 + input_section->output_offset);
3018 if (r_type == R_MIPS_REL32)
3019 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
3020 else
3021 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
3022 mips_elf_set_cr_dist2to (cptrel, 0);
3023 cptrel.konst = *addendp;
3024
3025 cr = (scpt->contents
3026 + sizeof (Elf32_External_compact_rel));
3027 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
3028 ((Elf32_External_crinfo *) cr
3029 + scpt->reloc_count));
3030 ++scpt->reloc_count;
3031 }
3032 }
3033
3034 return true;
3035 }
3036 \f
3037 /* Return the ISA for a MIPS e_flags value. */
3038
3039 static INLINE int
3040 elf_mips_isa (flags)
3041 flagword flags;
3042 {
3043 switch (flags & EF_MIPS_ARCH)
3044 {
3045 case E_MIPS_ARCH_1:
3046 return 1;
3047 case E_MIPS_ARCH_2:
3048 return 2;
3049 case E_MIPS_ARCH_3:
3050 return 3;
3051 case E_MIPS_ARCH_4:
3052 return 4;
3053 case E_MIPS_ARCH_5:
3054 return 5;
3055 case E_MIPS_ARCH_32:
3056 return 32;
3057 case E_MIPS_ARCH_64:
3058 return 64;
3059 }
3060 return 4;
3061 }
3062
3063 /* Return the MACH for a MIPS e_flags value. */
3064
3065 unsigned long
3066 _bfd_elf_mips_mach (flags)
3067 flagword flags;
3068 {
3069 switch (flags & EF_MIPS_MACH)
3070 {
3071 case E_MIPS_MACH_3900:
3072 return bfd_mach_mips3900;
3073
3074 case E_MIPS_MACH_4010:
3075 return bfd_mach_mips4010;
3076
3077 case E_MIPS_MACH_4100:
3078 return bfd_mach_mips4100;
3079
3080 case E_MIPS_MACH_4111:
3081 return bfd_mach_mips4111;
3082
3083 case E_MIPS_MACH_4120:
3084 return bfd_mach_mips4120;
3085
3086 case E_MIPS_MACH_4650:
3087 return bfd_mach_mips4650;
3088
3089 case E_MIPS_MACH_5400:
3090 return bfd_mach_mips5400;
3091
3092 case E_MIPS_MACH_5500:
3093 return bfd_mach_mips5500;
3094
3095 case E_MIPS_MACH_SB1:
3096 return bfd_mach_mips_sb1;
3097
3098 default:
3099 switch (flags & EF_MIPS_ARCH)
3100 {
3101 default:
3102 case E_MIPS_ARCH_1:
3103 return bfd_mach_mips3000;
3104 break;
3105
3106 case E_MIPS_ARCH_2:
3107 return bfd_mach_mips6000;
3108 break;
3109
3110 case E_MIPS_ARCH_3:
3111 return bfd_mach_mips4000;
3112 break;
3113
3114 case E_MIPS_ARCH_4:
3115 return bfd_mach_mips8000;
3116 break;
3117
3118 case E_MIPS_ARCH_5:
3119 return bfd_mach_mips5;
3120 break;
3121
3122 case E_MIPS_ARCH_32:
3123 return bfd_mach_mipsisa32;
3124 break;
3125
3126 case E_MIPS_ARCH_64:
3127 return bfd_mach_mipsisa64;
3128 break;
3129 }
3130 }
3131
3132 return 0;
3133 }
3134
3135 /* Return printable name for ABI. */
3136
3137 static INLINE char *
3138 elf_mips_abi_name (abfd)
3139 bfd *abfd;
3140 {
3141 flagword flags;
3142
3143 flags = elf_elfheader (abfd)->e_flags;
3144 switch (flags & EF_MIPS_ABI)
3145 {
3146 case 0:
3147 if (ABI_N32_P (abfd))
3148 return "N32";
3149 else if (ABI_64_P (abfd))
3150 return "64";
3151 else
3152 return "none";
3153 case E_MIPS_ABI_O32:
3154 return "O32";
3155 case E_MIPS_ABI_O64:
3156 return "O64";
3157 case E_MIPS_ABI_EABI32:
3158 return "EABI32";
3159 case E_MIPS_ABI_EABI64:
3160 return "EABI64";
3161 default:
3162 return "unknown abi";
3163 }
3164 }
3165 \f
3166 /* MIPS ELF uses two common sections. One is the usual one, and the
3167 other is for small objects. All the small objects are kept
3168 together, and then referenced via the gp pointer, which yields
3169 faster assembler code. This is what we use for the small common
3170 section. This approach is copied from ecoff.c. */
3171 static asection mips_elf_scom_section;
3172 static asymbol mips_elf_scom_symbol;
3173 static asymbol *mips_elf_scom_symbol_ptr;
3174
3175 /* MIPS ELF also uses an acommon section, which represents an
3176 allocated common symbol which may be overridden by a
3177 definition in a shared library. */
3178 static asection mips_elf_acom_section;
3179 static asymbol mips_elf_acom_symbol;
3180 static asymbol *mips_elf_acom_symbol_ptr;
3181
3182 /* Handle the special MIPS section numbers that a symbol may use.
3183 This is used for both the 32-bit and the 64-bit ABI. */
3184
3185 void
3186 _bfd_mips_elf_symbol_processing (abfd, asym)
3187 bfd *abfd;
3188 asymbol *asym;
3189 {
3190 elf_symbol_type *elfsym;
3191
3192 elfsym = (elf_symbol_type *) asym;
3193 switch (elfsym->internal_elf_sym.st_shndx)
3194 {
3195 case SHN_MIPS_ACOMMON:
3196 /* This section is used in a dynamically linked executable file.
3197 It is an allocated common section. The dynamic linker can
3198 either resolve these symbols to something in a shared
3199 library, or it can just leave them here. For our purposes,
3200 we can consider these symbols to be in a new section. */
3201 if (mips_elf_acom_section.name == NULL)
3202 {
3203 /* Initialize the acommon section. */
3204 mips_elf_acom_section.name = ".acommon";
3205 mips_elf_acom_section.flags = SEC_ALLOC;
3206 mips_elf_acom_section.output_section = &mips_elf_acom_section;
3207 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
3208 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
3209 mips_elf_acom_symbol.name = ".acommon";
3210 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
3211 mips_elf_acom_symbol.section = &mips_elf_acom_section;
3212 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
3213 }
3214 asym->section = &mips_elf_acom_section;
3215 break;
3216
3217 case SHN_COMMON:
3218 /* Common symbols less than the GP size are automatically
3219 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
3220 if (asym->value > elf_gp_size (abfd)
3221 || IRIX_COMPAT (abfd) == ict_irix6)
3222 break;
3223 /* Fall through. */
3224 case SHN_MIPS_SCOMMON:
3225 if (mips_elf_scom_section.name == NULL)
3226 {
3227 /* Initialize the small common section. */
3228 mips_elf_scom_section.name = ".scommon";
3229 mips_elf_scom_section.flags = SEC_IS_COMMON;
3230 mips_elf_scom_section.output_section = &mips_elf_scom_section;
3231 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
3232 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
3233 mips_elf_scom_symbol.name = ".scommon";
3234 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
3235 mips_elf_scom_symbol.section = &mips_elf_scom_section;
3236 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
3237 }
3238 asym->section = &mips_elf_scom_section;
3239 asym->value = elfsym->internal_elf_sym.st_size;
3240 break;
3241
3242 case SHN_MIPS_SUNDEFINED:
3243 asym->section = bfd_und_section_ptr;
3244 break;
3245
3246 #if 0 /* for SGI_COMPAT */
3247 case SHN_MIPS_TEXT:
3248 asym->section = mips_elf_text_section_ptr;
3249 break;
3250
3251 case SHN_MIPS_DATA:
3252 asym->section = mips_elf_data_section_ptr;
3253 break;
3254 #endif
3255 }
3256 }
3257 \f
3258 /* Work over a section just before writing it out. This routine is
3259 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
3260 sections that need the SHF_MIPS_GPREL flag by name; there has to be
3261 a better way. */
3262
3263 boolean
3264 _bfd_mips_elf_section_processing (abfd, hdr)
3265 bfd *abfd;
3266 Elf_Internal_Shdr *hdr;
3267 {
3268 if (hdr->sh_type == SHT_MIPS_REGINFO
3269 && hdr->sh_size > 0)
3270 {
3271 bfd_byte buf[4];
3272
3273 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
3274 BFD_ASSERT (hdr->contents == NULL);
3275
3276 if (bfd_seek (abfd,
3277 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
3278 SEEK_SET) != 0)
3279 return false;
3280 H_PUT_32 (abfd, elf_gp (abfd), buf);
3281 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
3282 return false;
3283 }
3284
3285 if (hdr->sh_type == SHT_MIPS_OPTIONS
3286 && hdr->bfd_section != NULL
3287 && elf_section_data (hdr->bfd_section) != NULL
3288 && elf_section_data (hdr->bfd_section)->tdata != NULL)
3289 {
3290 bfd_byte *contents, *l, *lend;
3291
3292 /* We stored the section contents in the elf_section_data tdata
3293 field in the set_section_contents routine. We save the
3294 section contents so that we don't have to read them again.
3295 At this point we know that elf_gp is set, so we can look
3296 through the section contents to see if there is an
3297 ODK_REGINFO structure. */
3298
3299 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
3300 l = contents;
3301 lend = contents + hdr->sh_size;
3302 while (l + sizeof (Elf_External_Options) <= lend)
3303 {
3304 Elf_Internal_Options intopt;
3305
3306 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
3307 &intopt);
3308 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
3309 {
3310 bfd_byte buf[8];
3311
3312 if (bfd_seek (abfd,
3313 (hdr->sh_offset
3314 + (l - contents)
3315 + sizeof (Elf_External_Options)
3316 + (sizeof (Elf64_External_RegInfo) - 8)),
3317 SEEK_SET) != 0)
3318 return false;
3319 H_PUT_64 (abfd, elf_gp (abfd), buf);
3320 if (bfd_bwrite (buf, (bfd_size_type) 8, abfd) != 8)
3321 return false;
3322 }
3323 else if (intopt.kind == ODK_REGINFO)
3324 {
3325 bfd_byte buf[4];
3326
3327 if (bfd_seek (abfd,
3328 (hdr->sh_offset
3329 + (l - contents)
3330 + sizeof (Elf_External_Options)
3331 + (sizeof (Elf32_External_RegInfo) - 4)),
3332 SEEK_SET) != 0)
3333 return false;
3334 H_PUT_32 (abfd, elf_gp (abfd), buf);
3335 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
3336 return false;
3337 }
3338 l += intopt.size;
3339 }
3340 }
3341
3342 if (hdr->bfd_section != NULL)
3343 {
3344 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
3345
3346 if (strcmp (name, ".sdata") == 0
3347 || strcmp (name, ".lit8") == 0
3348 || strcmp (name, ".lit4") == 0)
3349 {
3350 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3351 hdr->sh_type = SHT_PROGBITS;
3352 }
3353 else if (strcmp (name, ".sbss") == 0)
3354 {
3355 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3356 hdr->sh_type = SHT_NOBITS;
3357 }
3358 else if (strcmp (name, ".srdata") == 0)
3359 {
3360 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
3361 hdr->sh_type = SHT_PROGBITS;
3362 }
3363 else if (strcmp (name, ".compact_rel") == 0)
3364 {
3365 hdr->sh_flags = 0;
3366 hdr->sh_type = SHT_PROGBITS;
3367 }
3368 else if (strcmp (name, ".rtproc") == 0)
3369 {
3370 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
3371 {
3372 unsigned int adjust;
3373
3374 adjust = hdr->sh_size % hdr->sh_addralign;
3375 if (adjust != 0)
3376 hdr->sh_size += hdr->sh_addralign - adjust;
3377 }
3378 }
3379 }
3380
3381 return true;
3382 }
3383
3384 /* Handle a MIPS specific section when reading an object file. This
3385 is called when elfcode.h finds a section with an unknown type.
3386 This routine supports both the 32-bit and 64-bit ELF ABI.
3387
3388 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
3389 how to. */
3390
3391 boolean
3392 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
3393 bfd *abfd;
3394 Elf_Internal_Shdr *hdr;
3395 const char *name;
3396 {
3397 flagword flags = 0;
3398
3399 /* There ought to be a place to keep ELF backend specific flags, but
3400 at the moment there isn't one. We just keep track of the
3401 sections by their name, instead. Fortunately, the ABI gives
3402 suggested names for all the MIPS specific sections, so we will
3403 probably get away with this. */
3404 switch (hdr->sh_type)
3405 {
3406 case SHT_MIPS_LIBLIST:
3407 if (strcmp (name, ".liblist") != 0)
3408 return false;
3409 break;
3410 case SHT_MIPS_MSYM:
3411 if (strcmp (name, ".msym") != 0)
3412 return false;
3413 break;
3414 case SHT_MIPS_CONFLICT:
3415 if (strcmp (name, ".conflict") != 0)
3416 return false;
3417 break;
3418 case SHT_MIPS_GPTAB:
3419 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
3420 return false;
3421 break;
3422 case SHT_MIPS_UCODE:
3423 if (strcmp (name, ".ucode") != 0)
3424 return false;
3425 break;
3426 case SHT_MIPS_DEBUG:
3427 if (strcmp (name, ".mdebug") != 0)
3428 return false;
3429 flags = SEC_DEBUGGING;
3430 break;
3431 case SHT_MIPS_REGINFO:
3432 if (strcmp (name, ".reginfo") != 0
3433 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
3434 return false;
3435 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
3436 break;
3437 case SHT_MIPS_IFACE:
3438 if (strcmp (name, ".MIPS.interfaces") != 0)
3439 return false;
3440 break;
3441 case SHT_MIPS_CONTENT:
3442 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
3443 return false;
3444 break;
3445 case SHT_MIPS_OPTIONS:
3446 if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
3447 return false;
3448 break;
3449 case SHT_MIPS_DWARF:
3450 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
3451 return false;
3452 break;
3453 case SHT_MIPS_SYMBOL_LIB:
3454 if (strcmp (name, ".MIPS.symlib") != 0)
3455 return false;
3456 break;
3457 case SHT_MIPS_EVENTS:
3458 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
3459 && strncmp (name, ".MIPS.post_rel",
3460 sizeof ".MIPS.post_rel" - 1) != 0)
3461 return false;
3462 break;
3463 default:
3464 return false;
3465 }
3466
3467 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
3468 return false;
3469
3470 if (flags)
3471 {
3472 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
3473 (bfd_get_section_flags (abfd,
3474 hdr->bfd_section)
3475 | flags)))
3476 return false;
3477 }
3478
3479 /* FIXME: We should record sh_info for a .gptab section. */
3480
3481 /* For a .reginfo section, set the gp value in the tdata information
3482 from the contents of this section. We need the gp value while
3483 processing relocs, so we just get it now. The .reginfo section
3484 is not used in the 64-bit MIPS ELF ABI. */
3485 if (hdr->sh_type == SHT_MIPS_REGINFO)
3486 {
3487 Elf32_External_RegInfo ext;
3488 Elf32_RegInfo s;
3489
3490 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
3491 (file_ptr) 0,
3492 (bfd_size_type) sizeof ext))
3493 return false;
3494 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
3495 elf_gp (abfd) = s.ri_gp_value;
3496 }
3497
3498 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
3499 set the gp value based on what we find. We may see both
3500 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
3501 they should agree. */
3502 if (hdr->sh_type == SHT_MIPS_OPTIONS)
3503 {
3504 bfd_byte *contents, *l, *lend;
3505
3506 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
3507 if (contents == NULL)
3508 return false;
3509 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
3510 (file_ptr) 0, hdr->sh_size))
3511 {
3512 free (contents);
3513 return false;
3514 }
3515 l = contents;
3516 lend = contents + hdr->sh_size;
3517 while (l + sizeof (Elf_External_Options) <= lend)
3518 {
3519 Elf_Internal_Options intopt;
3520
3521 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
3522 &intopt);
3523 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
3524 {
3525 Elf64_Internal_RegInfo intreg;
3526
3527 bfd_mips_elf64_swap_reginfo_in
3528 (abfd,
3529 ((Elf64_External_RegInfo *)
3530 (l + sizeof (Elf_External_Options))),
3531 &intreg);
3532 elf_gp (abfd) = intreg.ri_gp_value;
3533 }
3534 else if (intopt.kind == ODK_REGINFO)
3535 {
3536 Elf32_RegInfo intreg;
3537
3538 bfd_mips_elf32_swap_reginfo_in
3539 (abfd,
3540 ((Elf32_External_RegInfo *)
3541 (l + sizeof (Elf_External_Options))),
3542 &intreg);
3543 elf_gp (abfd) = intreg.ri_gp_value;
3544 }
3545 l += intopt.size;
3546 }
3547 free (contents);
3548 }
3549
3550 return true;
3551 }
3552
3553 /* Set the correct type for a MIPS ELF section. We do this by the
3554 section name, which is a hack, but ought to work. This routine is
3555 used by both the 32-bit and the 64-bit ABI. */
3556
3557 boolean
3558 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
3559 bfd *abfd;
3560 Elf32_Internal_Shdr *hdr;
3561 asection *sec;
3562 {
3563 register const char *name;
3564
3565 name = bfd_get_section_name (abfd, sec);
3566
3567 if (strcmp (name, ".liblist") == 0)
3568 {
3569 hdr->sh_type = SHT_MIPS_LIBLIST;
3570 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
3571 /* The sh_link field is set in final_write_processing. */
3572 }
3573 else if (strcmp (name, ".conflict") == 0)
3574 hdr->sh_type = SHT_MIPS_CONFLICT;
3575 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
3576 {
3577 hdr->sh_type = SHT_MIPS_GPTAB;
3578 hdr->sh_entsize = sizeof (Elf32_External_gptab);
3579 /* The sh_info field is set in final_write_processing. */
3580 }
3581 else if (strcmp (name, ".ucode") == 0)
3582 hdr->sh_type = SHT_MIPS_UCODE;
3583 else if (strcmp (name, ".mdebug") == 0)
3584 {
3585 hdr->sh_type = SHT_MIPS_DEBUG;
3586 /* In a shared object on IRIX 5.3, the .mdebug section has an
3587 entsize of 0. FIXME: Does this matter? */
3588 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
3589 hdr->sh_entsize = 0;
3590 else
3591 hdr->sh_entsize = 1;
3592 }
3593 else if (strcmp (name, ".reginfo") == 0)
3594 {
3595 hdr->sh_type = SHT_MIPS_REGINFO;
3596 /* In a shared object on IRIX 5.3, the .reginfo section has an
3597 entsize of 0x18. FIXME: Does this matter? */
3598 if (SGI_COMPAT (abfd))
3599 {
3600 if ((abfd->flags & DYNAMIC) != 0)
3601 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
3602 else
3603 hdr->sh_entsize = 1;
3604 }
3605 else
3606 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
3607 }
3608 else if (SGI_COMPAT (abfd)
3609 && (strcmp (name, ".hash") == 0
3610 || strcmp (name, ".dynamic") == 0
3611 || strcmp (name, ".dynstr") == 0))
3612 {
3613 if (SGI_COMPAT (abfd))
3614 hdr->sh_entsize = 0;
3615 #if 0
3616 /* This isn't how the IRIX6 linker behaves. */
3617 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
3618 #endif
3619 }
3620 else if (strcmp (name, ".got") == 0
3621 || strcmp (name, ".srdata") == 0
3622 || strcmp (name, ".sdata") == 0
3623 || strcmp (name, ".sbss") == 0
3624 || strcmp (name, ".lit4") == 0
3625 || strcmp (name, ".lit8") == 0)
3626 hdr->sh_flags |= SHF_MIPS_GPREL;
3627 else if (strcmp (name, ".MIPS.interfaces") == 0)
3628 {
3629 hdr->sh_type = SHT_MIPS_IFACE;
3630 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3631 }
3632 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
3633 {
3634 hdr->sh_type = SHT_MIPS_CONTENT;
3635 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3636 /* The sh_info field is set in final_write_processing. */
3637 }
3638 else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
3639 {
3640 hdr->sh_type = SHT_MIPS_OPTIONS;
3641 hdr->sh_entsize = 1;
3642 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3643 }
3644 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
3645 hdr->sh_type = SHT_MIPS_DWARF;
3646 else if (strcmp (name, ".MIPS.symlib") == 0)
3647 {
3648 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
3649 /* The sh_link and sh_info fields are set in
3650 final_write_processing. */
3651 }
3652 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
3653 || strncmp (name, ".MIPS.post_rel",
3654 sizeof ".MIPS.post_rel" - 1) == 0)
3655 {
3656 hdr->sh_type = SHT_MIPS_EVENTS;
3657 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3658 /* The sh_link field is set in final_write_processing. */
3659 }
3660 else if (strcmp (name, ".msym") == 0)
3661 {
3662 hdr->sh_type = SHT_MIPS_MSYM;
3663 hdr->sh_flags |= SHF_ALLOC;
3664 hdr->sh_entsize = 8;
3665 }
3666
3667 /* The generic elf_fake_sections will set up REL_HDR using the
3668 default kind of relocations. But, we may actually need both
3669 kinds of relocations, so we set up the second header here.
3670
3671 This is not necessary for the O32 ABI since that only uses Elf32_Rel
3672 relocations (cf. System V ABI, MIPS RISC Processor Supplement,
3673 3rd Edition, p. 4-17). It breaks the IRIX 5/6 32-bit ld, since one
3674 of the resulting empty .rela.<section> sections starts with
3675 sh_offset == object size, and ld doesn't allow that. While the check
3676 is arguably bogus for empty or SHT_NOBITS sections, it can easily be
3677 avoided by not emitting those useless sections in the first place. */
3678 if (! SGI_COMPAT (abfd) && ! NEWABI_P(abfd)
3679 && (sec->flags & SEC_RELOC) != 0)
3680 {
3681 struct bfd_elf_section_data *esd;
3682 bfd_size_type amt = sizeof (Elf_Internal_Shdr);
3683
3684 esd = elf_section_data (sec);
3685 BFD_ASSERT (esd->rel_hdr2 == NULL);
3686 esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
3687 if (!esd->rel_hdr2)
3688 return false;
3689 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
3690 !elf_section_data (sec)->use_rela_p);
3691 }
3692
3693 return true;
3694 }
3695
3696 /* Given a BFD section, try to locate the corresponding ELF section
3697 index. This is used by both the 32-bit and the 64-bit ABI.
3698 Actually, it's not clear to me that the 64-bit ABI supports these,
3699 but for non-PIC objects we will certainly want support for at least
3700 the .scommon section. */
3701
3702 boolean
3703 _bfd_mips_elf_section_from_bfd_section (abfd, sec, retval)
3704 bfd *abfd ATTRIBUTE_UNUSED;
3705 asection *sec;
3706 int *retval;
3707 {
3708 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
3709 {
3710 *retval = SHN_MIPS_SCOMMON;
3711 return true;
3712 }
3713 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
3714 {
3715 *retval = SHN_MIPS_ACOMMON;
3716 return true;
3717 }
3718 return false;
3719 }
3720 \f
3721 /* Hook called by the linker routine which adds symbols from an object
3722 file. We must handle the special MIPS section numbers here. */
3723
3724 boolean
3725 _bfd_mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3726 bfd *abfd;
3727 struct bfd_link_info *info;
3728 const Elf_Internal_Sym *sym;
3729 const char **namep;
3730 flagword *flagsp ATTRIBUTE_UNUSED;
3731 asection **secp;
3732 bfd_vma *valp;
3733 {
3734 if (SGI_COMPAT (abfd)
3735 && (abfd->flags & DYNAMIC) != 0
3736 && strcmp (*namep, "_rld_new_interface") == 0)
3737 {
3738 /* Skip IRIX5 rld entry name. */
3739 *namep = NULL;
3740 return true;
3741 }
3742
3743 switch (sym->st_shndx)
3744 {
3745 case SHN_COMMON:
3746 /* Common symbols less than the GP size are automatically
3747 treated as SHN_MIPS_SCOMMON symbols. */
3748 if (sym->st_size > elf_gp_size (abfd)
3749 || IRIX_COMPAT (abfd) == ict_irix6)
3750 break;
3751 /* Fall through. */
3752 case SHN_MIPS_SCOMMON:
3753 *secp = bfd_make_section_old_way (abfd, ".scommon");
3754 (*secp)->flags |= SEC_IS_COMMON;
3755 *valp = sym->st_size;
3756 break;
3757
3758 case SHN_MIPS_TEXT:
3759 /* This section is used in a shared object. */
3760 if (elf_tdata (abfd)->elf_text_section == NULL)
3761 {
3762 asymbol *elf_text_symbol;
3763 asection *elf_text_section;
3764 bfd_size_type amt = sizeof (asection);
3765
3766 elf_text_section = bfd_zalloc (abfd, amt);
3767 if (elf_text_section == NULL)
3768 return false;
3769
3770 amt = sizeof (asymbol);
3771 elf_text_symbol = bfd_zalloc (abfd, amt);
3772 if (elf_text_symbol == NULL)
3773 return false;
3774
3775 /* Initialize the section. */
3776
3777 elf_tdata (abfd)->elf_text_section = elf_text_section;
3778 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
3779
3780 elf_text_section->symbol = elf_text_symbol;
3781 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
3782
3783 elf_text_section->name = ".text";
3784 elf_text_section->flags = SEC_NO_FLAGS;
3785 elf_text_section->output_section = NULL;
3786 elf_text_section->owner = abfd;
3787 elf_text_symbol->name = ".text";
3788 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
3789 elf_text_symbol->section = elf_text_section;
3790 }
3791 /* This code used to do *secp = bfd_und_section_ptr if
3792 info->shared. I don't know why, and that doesn't make sense,
3793 so I took it out. */
3794 *secp = elf_tdata (abfd)->elf_text_section;
3795 break;
3796
3797 case SHN_MIPS_ACOMMON:
3798 /* Fall through. XXX Can we treat this as allocated data? */
3799 case SHN_MIPS_DATA:
3800 /* This section is used in a shared object. */
3801 if (elf_tdata (abfd)->elf_data_section == NULL)
3802 {
3803 asymbol *elf_data_symbol;
3804 asection *elf_data_section;
3805 bfd_size_type amt = sizeof (asection);
3806
3807 elf_data_section = bfd_zalloc (abfd, amt);
3808 if (elf_data_section == NULL)
3809 return false;
3810
3811 amt = sizeof (asymbol);
3812 elf_data_symbol = bfd_zalloc (abfd, amt);
3813 if (elf_data_symbol == NULL)
3814 return false;
3815
3816 /* Initialize the section. */
3817
3818 elf_tdata (abfd)->elf_data_section = elf_data_section;
3819 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
3820
3821 elf_data_section->symbol = elf_data_symbol;
3822 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
3823
3824 elf_data_section->name = ".data";
3825 elf_data_section->flags = SEC_NO_FLAGS;
3826 elf_data_section->output_section = NULL;
3827 elf_data_section->owner = abfd;
3828 elf_data_symbol->name = ".data";
3829 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
3830 elf_data_symbol->section = elf_data_section;
3831 }
3832 /* This code used to do *secp = bfd_und_section_ptr if
3833 info->shared. I don't know why, and that doesn't make sense,
3834 so I took it out. */
3835 *secp = elf_tdata (abfd)->elf_data_section;
3836 break;
3837
3838 case SHN_MIPS_SUNDEFINED:
3839 *secp = bfd_und_section_ptr;
3840 break;
3841 }
3842
3843 if (SGI_COMPAT (abfd)
3844 && ! info->shared
3845 && info->hash->creator == abfd->xvec
3846 && strcmp (*namep, "__rld_obj_head") == 0)
3847 {
3848 struct elf_link_hash_entry *h;
3849 struct bfd_link_hash_entry *bh;
3850
3851 /* Mark __rld_obj_head as dynamic. */
3852 bh = NULL;
3853 if (! (_bfd_generic_link_add_one_symbol
3854 (info, abfd, *namep, BSF_GLOBAL, *secp,
3855 (bfd_vma) *valp, (const char *) NULL, false,
3856 get_elf_backend_data (abfd)->collect, &bh)))
3857 return false;
3858
3859 h = (struct elf_link_hash_entry *) bh;
3860 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
3861 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3862 h->type = STT_OBJECT;
3863
3864 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3865 return false;
3866
3867 mips_elf_hash_table (info)->use_rld_obj_head = true;
3868 }
3869
3870 /* If this is a mips16 text symbol, add 1 to the value to make it
3871 odd. This will cause something like .word SYM to come up with
3872 the right value when it is loaded into the PC. */
3873 if (sym->st_other == STO_MIPS16)
3874 ++*valp;
3875
3876 return true;
3877 }
3878
3879 /* This hook function is called before the linker writes out a global
3880 symbol. We mark symbols as small common if appropriate. This is
3881 also where we undo the increment of the value for a mips16 symbol. */
3882
3883 boolean
3884 _bfd_mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
3885 bfd *abfd ATTRIBUTE_UNUSED;
3886 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3887 const char *name ATTRIBUTE_UNUSED;
3888 Elf_Internal_Sym *sym;
3889 asection *input_sec;
3890 {
3891 /* If we see a common symbol, which implies a relocatable link, then
3892 if a symbol was small common in an input file, mark it as small
3893 common in the output file. */
3894 if (sym->st_shndx == SHN_COMMON
3895 && strcmp (input_sec->name, ".scommon") == 0)
3896 sym->st_shndx = SHN_MIPS_SCOMMON;
3897
3898 if (sym->st_other == STO_MIPS16
3899 && (sym->st_value & 1) != 0)
3900 --sym->st_value;
3901
3902 return true;
3903 }
3904 \f
3905 /* Functions for the dynamic linker. */
3906
3907 /* Create dynamic sections when linking against a dynamic object. */
3908
3909 boolean
3910 _bfd_mips_elf_create_dynamic_sections (abfd, info)
3911 bfd *abfd;
3912 struct bfd_link_info *info;
3913 {
3914 struct elf_link_hash_entry *h;
3915 struct bfd_link_hash_entry *bh;
3916 flagword flags;
3917 register asection *s;
3918 const char * const *namep;
3919
3920 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3921 | SEC_LINKER_CREATED | SEC_READONLY);
3922
3923 /* Mips ABI requests the .dynamic section to be read only. */
3924 s = bfd_get_section_by_name (abfd, ".dynamic");
3925 if (s != NULL)
3926 {
3927 if (! bfd_set_section_flags (abfd, s, flags))
3928 return false;
3929 }
3930
3931 /* We need to create .got section. */
3932 if (! mips_elf_create_got_section (abfd, info))
3933 return false;
3934
3935 /* Create the .msym section on IRIX6. It is used by the dynamic
3936 linker to speed up dynamic relocations, and to avoid computing
3937 the ELF hash for symbols. */
3938 if (IRIX_COMPAT (abfd) == ict_irix6
3939 && !mips_elf_create_msym_section (abfd))
3940 return false;
3941
3942 /* Create .stub section. */
3943 if (bfd_get_section_by_name (abfd,
3944 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
3945 {
3946 s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
3947 if (s == NULL
3948 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
3949 || ! bfd_set_section_alignment (abfd, s,
3950 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
3951 return false;
3952 }
3953
3954 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
3955 && !info->shared
3956 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
3957 {
3958 s = bfd_make_section (abfd, ".rld_map");
3959 if (s == NULL
3960 || ! bfd_set_section_flags (abfd, s, flags &~ (flagword) SEC_READONLY)
3961 || ! bfd_set_section_alignment (abfd, s,
3962 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
3963 return false;
3964 }
3965
3966 /* On IRIX5, we adjust add some additional symbols and change the
3967 alignments of several sections. There is no ABI documentation
3968 indicating that this is necessary on IRIX6, nor any evidence that
3969 the linker takes such action. */
3970 if (IRIX_COMPAT (abfd) == ict_irix5)
3971 {
3972 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
3973 {
3974 bh = NULL;
3975 if (! (_bfd_generic_link_add_one_symbol
3976 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
3977 (bfd_vma) 0, (const char *) NULL, false,
3978 get_elf_backend_data (abfd)->collect, &bh)))
3979 return false;
3980
3981 h = (struct elf_link_hash_entry *) bh;
3982 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
3983 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3984 h->type = STT_SECTION;
3985
3986 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3987 return false;
3988 }
3989
3990 /* We need to create a .compact_rel section. */
3991 if (SGI_COMPAT (abfd))
3992 {
3993 if (!mips_elf_create_compact_rel_section (abfd, info))
3994 return false;
3995 }
3996
3997 /* Change alignments of some sections. */
3998 s = bfd_get_section_by_name (abfd, ".hash");
3999 if (s != NULL)
4000 bfd_set_section_alignment (abfd, s, 4);
4001 s = bfd_get_section_by_name (abfd, ".dynsym");
4002 if (s != NULL)
4003 bfd_set_section_alignment (abfd, s, 4);
4004 s = bfd_get_section_by_name (abfd, ".dynstr");
4005 if (s != NULL)
4006 bfd_set_section_alignment (abfd, s, 4);
4007 s = bfd_get_section_by_name (abfd, ".reginfo");
4008 if (s != NULL)
4009 bfd_set_section_alignment (abfd, s, 4);
4010 s = bfd_get_section_by_name (abfd, ".dynamic");
4011 if (s != NULL)
4012 bfd_set_section_alignment (abfd, s, 4);
4013 }
4014
4015 if (!info->shared)
4016 {
4017 const char *name;
4018
4019 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
4020 bh = NULL;
4021 if (!(_bfd_generic_link_add_one_symbol
4022 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr,
4023 (bfd_vma) 0, (const char *) NULL, false,
4024 get_elf_backend_data (abfd)->collect, &bh)))
4025 return false;
4026
4027 h = (struct elf_link_hash_entry *) bh;
4028 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
4029 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4030 h->type = STT_SECTION;
4031
4032 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4033 return false;
4034
4035 if (! mips_elf_hash_table (info)->use_rld_obj_head)
4036 {
4037 /* __rld_map is a four byte word located in the .data section
4038 and is filled in by the rtld to contain a pointer to
4039 the _r_debug structure. Its symbol value will be set in
4040 _bfd_mips_elf_finish_dynamic_symbol. */
4041 s = bfd_get_section_by_name (abfd, ".rld_map");
4042 BFD_ASSERT (s != NULL);
4043
4044 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
4045 bh = NULL;
4046 if (!(_bfd_generic_link_add_one_symbol
4047 (info, abfd, name, BSF_GLOBAL, s,
4048 (bfd_vma) 0, (const char *) NULL, false,
4049 get_elf_backend_data (abfd)->collect, &bh)))
4050 return false;
4051
4052 h = (struct elf_link_hash_entry *) bh;
4053 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
4054 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4055 h->type = STT_OBJECT;
4056
4057 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4058 return false;
4059 }
4060 }
4061
4062 return true;
4063 }
4064 \f
4065 /* Look through the relocs for a section during the first phase, and
4066 allocate space in the global offset table. */
4067
4068 boolean
4069 _bfd_mips_elf_check_relocs (abfd, info, sec, relocs)
4070 bfd *abfd;
4071 struct bfd_link_info *info;
4072 asection *sec;
4073 const Elf_Internal_Rela *relocs;
4074 {
4075 const char *name;
4076 bfd *dynobj;
4077 Elf_Internal_Shdr *symtab_hdr;
4078 struct elf_link_hash_entry **sym_hashes;
4079 struct mips_got_info *g;
4080 size_t extsymoff;
4081 const Elf_Internal_Rela *rel;
4082 const Elf_Internal_Rela *rel_end;
4083 asection *sgot;
4084 asection *sreloc;
4085 struct elf_backend_data *bed;
4086
4087 if (info->relocateable)
4088 return true;
4089
4090 dynobj = elf_hash_table (info)->dynobj;
4091 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4092 sym_hashes = elf_sym_hashes (abfd);
4093 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
4094
4095 /* Check for the mips16 stub sections. */
4096
4097 name = bfd_get_section_name (abfd, sec);
4098 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
4099 {
4100 unsigned long r_symndx;
4101
4102 /* Look at the relocation information to figure out which symbol
4103 this is for. */
4104
4105 r_symndx = ELF_R_SYM (abfd, relocs->r_info);
4106
4107 if (r_symndx < extsymoff
4108 || sym_hashes[r_symndx - extsymoff] == NULL)
4109 {
4110 asection *o;
4111
4112 /* This stub is for a local symbol. This stub will only be
4113 needed if there is some relocation in this BFD, other
4114 than a 16 bit function call, which refers to this symbol. */
4115 for (o = abfd->sections; o != NULL; o = o->next)
4116 {
4117 Elf_Internal_Rela *sec_relocs;
4118 const Elf_Internal_Rela *r, *rend;
4119
4120 /* We can ignore stub sections when looking for relocs. */
4121 if ((o->flags & SEC_RELOC) == 0
4122 || o->reloc_count == 0
4123 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
4124 sizeof FN_STUB - 1) == 0
4125 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
4126 sizeof CALL_STUB - 1) == 0
4127 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
4128 sizeof CALL_FP_STUB - 1) == 0)
4129 continue;
4130
4131 sec_relocs = (_bfd_elf32_link_read_relocs
4132 (abfd, o, (PTR) NULL,
4133 (Elf_Internal_Rela *) NULL,
4134 info->keep_memory));
4135 if (sec_relocs == NULL)
4136 return false;
4137
4138 rend = sec_relocs + o->reloc_count;
4139 for (r = sec_relocs; r < rend; r++)
4140 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
4141 && ELF_R_TYPE (abfd, r->r_info) != R_MIPS16_26)
4142 break;
4143
4144 if (elf_section_data (o)->relocs != sec_relocs)
4145 free (sec_relocs);
4146
4147 if (r < rend)
4148 break;
4149 }
4150
4151 if (o == NULL)
4152 {
4153 /* There is no non-call reloc for this stub, so we do
4154 not need it. Since this function is called before
4155 the linker maps input sections to output sections, we
4156 can easily discard it by setting the SEC_EXCLUDE
4157 flag. */
4158 sec->flags |= SEC_EXCLUDE;
4159 return true;
4160 }
4161
4162 /* Record this stub in an array of local symbol stubs for
4163 this BFD. */
4164 if (elf_tdata (abfd)->local_stubs == NULL)
4165 {
4166 unsigned long symcount;
4167 asection **n;
4168 bfd_size_type amt;
4169
4170 if (elf_bad_symtab (abfd))
4171 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
4172 else
4173 symcount = symtab_hdr->sh_info;
4174 amt = symcount * sizeof (asection *);
4175 n = (asection **) bfd_zalloc (abfd, amt);
4176 if (n == NULL)
4177 return false;
4178 elf_tdata (abfd)->local_stubs = n;
4179 }
4180
4181 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
4182
4183 /* We don't need to set mips16_stubs_seen in this case.
4184 That flag is used to see whether we need to look through
4185 the global symbol table for stubs. We don't need to set
4186 it here, because we just have a local stub. */
4187 }
4188 else
4189 {
4190 struct mips_elf_link_hash_entry *h;
4191
4192 h = ((struct mips_elf_link_hash_entry *)
4193 sym_hashes[r_symndx - extsymoff]);
4194
4195 /* H is the symbol this stub is for. */
4196
4197 h->fn_stub = sec;
4198 mips_elf_hash_table (info)->mips16_stubs_seen = true;
4199 }
4200 }
4201 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
4202 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
4203 {
4204 unsigned long r_symndx;
4205 struct mips_elf_link_hash_entry *h;
4206 asection **loc;
4207
4208 /* Look at the relocation information to figure out which symbol
4209 this is for. */
4210
4211 r_symndx = ELF_R_SYM (abfd, relocs->r_info);
4212
4213 if (r_symndx < extsymoff
4214 || sym_hashes[r_symndx - extsymoff] == NULL)
4215 {
4216 /* This stub was actually built for a static symbol defined
4217 in the same file. We assume that all static symbols in
4218 mips16 code are themselves mips16, so we can simply
4219 discard this stub. Since this function is called before
4220 the linker maps input sections to output sections, we can
4221 easily discard it by setting the SEC_EXCLUDE flag. */
4222 sec->flags |= SEC_EXCLUDE;
4223 return true;
4224 }
4225
4226 h = ((struct mips_elf_link_hash_entry *)
4227 sym_hashes[r_symndx - extsymoff]);
4228
4229 /* H is the symbol this stub is for. */
4230
4231 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
4232 loc = &h->call_fp_stub;
4233 else
4234 loc = &h->call_stub;
4235
4236 /* If we already have an appropriate stub for this function, we
4237 don't need another one, so we can discard this one. Since
4238 this function is called before the linker maps input sections
4239 to output sections, we can easily discard it by setting the
4240 SEC_EXCLUDE flag. We can also discard this section if we
4241 happen to already know that this is a mips16 function; it is
4242 not necessary to check this here, as it is checked later, but
4243 it is slightly faster to check now. */
4244 if (*loc != NULL || h->root.other == STO_MIPS16)
4245 {
4246 sec->flags |= SEC_EXCLUDE;
4247 return true;
4248 }
4249
4250 *loc = sec;
4251 mips_elf_hash_table (info)->mips16_stubs_seen = true;
4252 }
4253
4254 if (dynobj == NULL)
4255 {
4256 sgot = NULL;
4257 g = NULL;
4258 }
4259 else
4260 {
4261 sgot = mips_elf_got_section (dynobj);
4262 if (sgot == NULL)
4263 g = NULL;
4264 else
4265 {
4266 BFD_ASSERT (elf_section_data (sgot) != NULL);
4267 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4268 BFD_ASSERT (g != NULL);
4269 }
4270 }
4271
4272 sreloc = NULL;
4273 bed = get_elf_backend_data (abfd);
4274 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
4275 for (rel = relocs; rel < rel_end; ++rel)
4276 {
4277 unsigned long r_symndx;
4278 unsigned int r_type;
4279 struct elf_link_hash_entry *h;
4280
4281 r_symndx = ELF_R_SYM (abfd, rel->r_info);
4282 r_type = ELF_R_TYPE (abfd, rel->r_info);
4283
4284 if (r_symndx < extsymoff)
4285 h = NULL;
4286 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
4287 {
4288 (*_bfd_error_handler)
4289 (_("%s: Malformed reloc detected for section %s"),
4290 bfd_archive_filename (abfd), name);
4291 bfd_set_error (bfd_error_bad_value);
4292 return false;
4293 }
4294 else
4295 {
4296 h = sym_hashes[r_symndx - extsymoff];
4297
4298 /* This may be an indirect symbol created because of a version. */
4299 if (h != NULL)
4300 {
4301 while (h->root.type == bfd_link_hash_indirect)
4302 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4303 }
4304 }
4305
4306 /* Some relocs require a global offset table. */
4307 if (dynobj == NULL || sgot == NULL)
4308 {
4309 switch (r_type)
4310 {
4311 case R_MIPS_GOT16:
4312 case R_MIPS_CALL16:
4313 case R_MIPS_CALL_HI16:
4314 case R_MIPS_CALL_LO16:
4315 case R_MIPS_GOT_HI16:
4316 case R_MIPS_GOT_LO16:
4317 case R_MIPS_GOT_PAGE:
4318 case R_MIPS_GOT_OFST:
4319 case R_MIPS_GOT_DISP:
4320 if (dynobj == NULL)
4321 elf_hash_table (info)->dynobj = dynobj = abfd;
4322 if (! mips_elf_create_got_section (dynobj, info))
4323 return false;
4324 g = mips_elf_got_info (dynobj, &sgot);
4325 break;
4326
4327 case R_MIPS_32:
4328 case R_MIPS_REL32:
4329 case R_MIPS_64:
4330 if (dynobj == NULL
4331 && (info->shared || h != NULL)
4332 && (sec->flags & SEC_ALLOC) != 0)
4333 elf_hash_table (info)->dynobj = dynobj = abfd;
4334 break;
4335
4336 default:
4337 break;
4338 }
4339 }
4340
4341 if (!h && (r_type == R_MIPS_CALL_LO16
4342 || r_type == R_MIPS_GOT_LO16
4343 || r_type == R_MIPS_GOT_DISP))
4344 {
4345 /* We may need a local GOT entry for this relocation. We
4346 don't count R_MIPS_GOT_PAGE because we can estimate the
4347 maximum number of pages needed by looking at the size of
4348 the segment. Similar comments apply to R_MIPS_GOT16 and
4349 R_MIPS_CALL16. We don't count R_MIPS_GOT_HI16, or
4350 R_MIPS_CALL_HI16 because these are always followed by an
4351 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
4352
4353 This estimation is very conservative since we can merge
4354 duplicate entries in the GOT. In order to be less
4355 conservative, we could actually build the GOT here,
4356 rather than in relocate_section. */
4357 g->local_gotno++;
4358 sgot->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
4359 }
4360
4361 switch (r_type)
4362 {
4363 case R_MIPS_CALL16:
4364 if (h == NULL)
4365 {
4366 (*_bfd_error_handler)
4367 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
4368 bfd_archive_filename (abfd), (unsigned long) rel->r_offset);
4369 bfd_set_error (bfd_error_bad_value);
4370 return false;
4371 }
4372 /* Fall through. */
4373
4374 case R_MIPS_CALL_HI16:
4375 case R_MIPS_CALL_LO16:
4376 if (h != NULL)
4377 {
4378 /* This symbol requires a global offset table entry. */
4379 if (! mips_elf_record_global_got_symbol (h, info, g))
4380 return false;
4381
4382 /* We need a stub, not a plt entry for the undefined
4383 function. But we record it as if it needs plt. See
4384 elf_adjust_dynamic_symbol in elflink.h. */
4385 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4386 h->type = STT_FUNC;
4387 }
4388 break;
4389
4390 case R_MIPS_GOT16:
4391 case R_MIPS_GOT_HI16:
4392 case R_MIPS_GOT_LO16:
4393 case R_MIPS_GOT_DISP:
4394 /* This symbol requires a global offset table entry. */
4395 if (h && ! mips_elf_record_global_got_symbol (h, info, g))
4396 return false;
4397 break;
4398
4399 case R_MIPS_32:
4400 case R_MIPS_REL32:
4401 case R_MIPS_64:
4402 if ((info->shared || h != NULL)
4403 && (sec->flags & SEC_ALLOC) != 0)
4404 {
4405 if (sreloc == NULL)
4406 {
4407 const char *dname = ".rel.dyn";
4408
4409 sreloc = bfd_get_section_by_name (dynobj, dname);
4410 if (sreloc == NULL)
4411 {
4412 sreloc = bfd_make_section (dynobj, dname);
4413 if (sreloc == NULL
4414 || ! bfd_set_section_flags (dynobj, sreloc,
4415 (SEC_ALLOC
4416 | SEC_LOAD
4417 | SEC_HAS_CONTENTS
4418 | SEC_IN_MEMORY
4419 | SEC_LINKER_CREATED
4420 | SEC_READONLY))
4421 || ! bfd_set_section_alignment (dynobj, sreloc,
4422 4))
4423 return false;
4424 }
4425 }
4426 #define MIPS_READONLY_SECTION (SEC_ALLOC | SEC_LOAD | SEC_READONLY)
4427 if (info->shared)
4428 {
4429 /* When creating a shared object, we must copy these
4430 reloc types into the output file as R_MIPS_REL32
4431 relocs. We make room for this reloc in the
4432 .rel.dyn reloc section. */
4433 mips_elf_allocate_dynamic_relocations (dynobj, 1);
4434 if ((sec->flags & MIPS_READONLY_SECTION)
4435 == MIPS_READONLY_SECTION)
4436 /* We tell the dynamic linker that there are
4437 relocations against the text segment. */
4438 info->flags |= DF_TEXTREL;
4439 }
4440 else
4441 {
4442 struct mips_elf_link_hash_entry *hmips;
4443
4444 /* We only need to copy this reloc if the symbol is
4445 defined in a dynamic object. */
4446 hmips = (struct mips_elf_link_hash_entry *) h;
4447 ++hmips->possibly_dynamic_relocs;
4448 if ((sec->flags & MIPS_READONLY_SECTION)
4449 == MIPS_READONLY_SECTION)
4450 /* We need it to tell the dynamic linker if there
4451 are relocations against the text segment. */
4452 hmips->readonly_reloc = true;
4453 }
4454
4455 /* Even though we don't directly need a GOT entry for
4456 this symbol, a symbol must have a dynamic symbol
4457 table index greater that DT_MIPS_GOTSYM if there are
4458 dynamic relocations against it. */
4459 if (h != NULL
4460 && ! mips_elf_record_global_got_symbol (h, info, g))
4461 return false;
4462 }
4463
4464 if (SGI_COMPAT (abfd))
4465 mips_elf_hash_table (info)->compact_rel_size +=
4466 sizeof (Elf32_External_crinfo);
4467 break;
4468
4469 case R_MIPS_26:
4470 case R_MIPS_GPREL16:
4471 case R_MIPS_LITERAL:
4472 case R_MIPS_GPREL32:
4473 if (SGI_COMPAT (abfd))
4474 mips_elf_hash_table (info)->compact_rel_size +=
4475 sizeof (Elf32_External_crinfo);
4476 break;
4477
4478 /* This relocation describes the C++ object vtable hierarchy.
4479 Reconstruct it for later use during GC. */
4480 case R_MIPS_GNU_VTINHERIT:
4481 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4482 return false;
4483 break;
4484
4485 /* This relocation describes which C++ vtable entries are actually
4486 used. Record for later use during GC. */
4487 case R_MIPS_GNU_VTENTRY:
4488 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
4489 return false;
4490 break;
4491
4492 default:
4493 break;
4494 }
4495
4496 /* We must not create a stub for a symbol that has relocations
4497 related to taking the function's address. */
4498 switch (r_type)
4499 {
4500 default:
4501 if (h != NULL)
4502 {
4503 struct mips_elf_link_hash_entry *mh;
4504
4505 mh = (struct mips_elf_link_hash_entry *) h;
4506 mh->no_fn_stub = true;
4507 }
4508 break;
4509 case R_MIPS_CALL16:
4510 case R_MIPS_CALL_HI16:
4511 case R_MIPS_CALL_LO16:
4512 break;
4513 }
4514
4515 /* If this reloc is not a 16 bit call, and it has a global
4516 symbol, then we will need the fn_stub if there is one.
4517 References from a stub section do not count. */
4518 if (h != NULL
4519 && r_type != R_MIPS16_26
4520 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
4521 sizeof FN_STUB - 1) != 0
4522 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
4523 sizeof CALL_STUB - 1) != 0
4524 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
4525 sizeof CALL_FP_STUB - 1) != 0)
4526 {
4527 struct mips_elf_link_hash_entry *mh;
4528
4529 mh = (struct mips_elf_link_hash_entry *) h;
4530 mh->need_fn_stub = true;
4531 }
4532 }
4533
4534 return true;
4535 }
4536 \f
4537 /* Adjust a symbol defined by a dynamic object and referenced by a
4538 regular object. The current definition is in some section of the
4539 dynamic object, but we're not including those sections. We have to
4540 change the definition to something the rest of the link can
4541 understand. */
4542
4543 boolean
4544 _bfd_mips_elf_adjust_dynamic_symbol (info, h)
4545 struct bfd_link_info *info;
4546 struct elf_link_hash_entry *h;
4547 {
4548 bfd *dynobj;
4549 struct mips_elf_link_hash_entry *hmips;
4550 asection *s;
4551
4552 dynobj = elf_hash_table (info)->dynobj;
4553
4554 /* Make sure we know what is going on here. */
4555 BFD_ASSERT (dynobj != NULL
4556 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
4557 || h->weakdef != NULL
4558 || ((h->elf_link_hash_flags
4559 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4560 && (h->elf_link_hash_flags
4561 & ELF_LINK_HASH_REF_REGULAR) != 0
4562 && (h->elf_link_hash_flags
4563 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
4564
4565 /* If this symbol is defined in a dynamic object, we need to copy
4566 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
4567 file. */
4568 hmips = (struct mips_elf_link_hash_entry *) h;
4569 if (! info->relocateable
4570 && hmips->possibly_dynamic_relocs != 0
4571 && (h->root.type == bfd_link_hash_defweak
4572 || (h->elf_link_hash_flags
4573 & ELF_LINK_HASH_DEF_REGULAR) == 0))
4574 {
4575 mips_elf_allocate_dynamic_relocations (dynobj,
4576 hmips->possibly_dynamic_relocs);
4577 if (hmips->readonly_reloc)
4578 /* We tell the dynamic linker that there are relocations
4579 against the text segment. */
4580 info->flags |= DF_TEXTREL;
4581 }
4582
4583 /* For a function, create a stub, if allowed. */
4584 if (! hmips->no_fn_stub
4585 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4586 {
4587 if (! elf_hash_table (info)->dynamic_sections_created)
4588 return true;
4589
4590 /* If this symbol is not defined in a regular file, then set
4591 the symbol to the stub location. This is required to make
4592 function pointers compare as equal between the normal
4593 executable and the shared library. */
4594 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4595 {
4596 /* We need .stub section. */
4597 s = bfd_get_section_by_name (dynobj,
4598 MIPS_ELF_STUB_SECTION_NAME (dynobj));
4599 BFD_ASSERT (s != NULL);
4600
4601 h->root.u.def.section = s;
4602 h->root.u.def.value = s->_raw_size;
4603
4604 /* XXX Write this stub address somewhere. */
4605 h->plt.offset = s->_raw_size;
4606
4607 /* Make room for this stub code. */
4608 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
4609
4610 /* The last half word of the stub will be filled with the index
4611 of this symbol in .dynsym section. */
4612 return true;
4613 }
4614 }
4615 else if ((h->type == STT_FUNC)
4616 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4617 {
4618 /* This will set the entry for this symbol in the GOT to 0, and
4619 the dynamic linker will take care of this. */
4620 h->root.u.def.value = 0;
4621 return true;
4622 }
4623
4624 /* If this is a weak symbol, and there is a real definition, the
4625 processor independent code will have arranged for us to see the
4626 real definition first, and we can just use the same value. */
4627 if (h->weakdef != NULL)
4628 {
4629 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4630 || h->weakdef->root.type == bfd_link_hash_defweak);
4631 h->root.u.def.section = h->weakdef->root.u.def.section;
4632 h->root.u.def.value = h->weakdef->root.u.def.value;
4633 return true;
4634 }
4635
4636 /* This is a reference to a symbol defined by a dynamic object which
4637 is not a function. */
4638
4639 return true;
4640 }
4641 \f
4642 /* This function is called after all the input files have been read,
4643 and the input sections have been assigned to output sections. We
4644 check for any mips16 stub sections that we can discard. */
4645
4646 boolean
4647 _bfd_mips_elf_always_size_sections (output_bfd, info)
4648 bfd *output_bfd;
4649 struct bfd_link_info *info;
4650 {
4651 asection *ri;
4652
4653 /* The .reginfo section has a fixed size. */
4654 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
4655 if (ri != NULL)
4656 bfd_set_section_size (output_bfd, ri,
4657 (bfd_size_type) sizeof (Elf32_External_RegInfo));
4658
4659 if (info->relocateable
4660 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
4661 return true;
4662
4663 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4664 mips_elf_check_mips16_stubs,
4665 (PTR) NULL);
4666
4667 return true;
4668 }
4669
4670 /* Set the sizes of the dynamic sections. */
4671
4672 boolean
4673 _bfd_mips_elf_size_dynamic_sections (output_bfd, info)
4674 bfd *output_bfd;
4675 struct bfd_link_info *info;
4676 {
4677 bfd *dynobj;
4678 asection *s;
4679 boolean reltext;
4680 struct mips_got_info *g = NULL;
4681
4682 dynobj = elf_hash_table (info)->dynobj;
4683 BFD_ASSERT (dynobj != NULL);
4684
4685 if (elf_hash_table (info)->dynamic_sections_created)
4686 {
4687 /* Set the contents of the .interp section to the interpreter. */
4688 if (! info->shared)
4689 {
4690 s = bfd_get_section_by_name (dynobj, ".interp");
4691 BFD_ASSERT (s != NULL);
4692 s->_raw_size
4693 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
4694 s->contents
4695 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
4696 }
4697 }
4698
4699 /* The check_relocs and adjust_dynamic_symbol entry points have
4700 determined the sizes of the various dynamic sections. Allocate
4701 memory for them. */
4702 reltext = false;
4703 for (s = dynobj->sections; s != NULL; s = s->next)
4704 {
4705 const char *name;
4706 boolean strip;
4707
4708 /* It's OK to base decisions on the section name, because none
4709 of the dynobj section names depend upon the input files. */
4710 name = bfd_get_section_name (dynobj, s);
4711
4712 if ((s->flags & SEC_LINKER_CREATED) == 0)
4713 continue;
4714
4715 strip = false;
4716
4717 if (strncmp (name, ".rel", 4) == 0)
4718 {
4719 if (s->_raw_size == 0)
4720 {
4721 /* We only strip the section if the output section name
4722 has the same name. Otherwise, there might be several
4723 input sections for this output section. FIXME: This
4724 code is probably not needed these days anyhow, since
4725 the linker now does not create empty output sections. */
4726 if (s->output_section != NULL
4727 && strcmp (name,
4728 bfd_get_section_name (s->output_section->owner,
4729 s->output_section)) == 0)
4730 strip = true;
4731 }
4732 else
4733 {
4734 const char *outname;
4735 asection *target;
4736
4737 /* If this relocation section applies to a read only
4738 section, then we probably need a DT_TEXTREL entry.
4739 If the relocation section is .rel.dyn, we always
4740 assert a DT_TEXTREL entry rather than testing whether
4741 there exists a relocation to a read only section or
4742 not. */
4743 outname = bfd_get_section_name (output_bfd,
4744 s->output_section);
4745 target = bfd_get_section_by_name (output_bfd, outname + 4);
4746 if ((target != NULL
4747 && (target->flags & SEC_READONLY) != 0
4748 && (target->flags & SEC_ALLOC) != 0)
4749 || strcmp (outname, ".rel.dyn") == 0)
4750 reltext = true;
4751
4752 /* We use the reloc_count field as a counter if we need
4753 to copy relocs into the output file. */
4754 if (strcmp (name, ".rel.dyn") != 0)
4755 s->reloc_count = 0;
4756 }
4757 }
4758 else if (strncmp (name, ".got", 4) == 0)
4759 {
4760 int i;
4761 bfd_size_type loadable_size = 0;
4762 bfd_size_type local_gotno;
4763 bfd *sub;
4764
4765 BFD_ASSERT (elf_section_data (s) != NULL);
4766 g = (struct mips_got_info *) elf_section_data (s)->tdata;
4767 BFD_ASSERT (g != NULL);
4768
4769 /* Calculate the total loadable size of the output. That
4770 will give us the maximum number of GOT_PAGE entries
4771 required. */
4772 for (sub = info->input_bfds; sub; sub = sub->link_next)
4773 {
4774 asection *subsection;
4775
4776 for (subsection = sub->sections;
4777 subsection;
4778 subsection = subsection->next)
4779 {
4780 if ((subsection->flags & SEC_ALLOC) == 0)
4781 continue;
4782 loadable_size += ((subsection->_raw_size + 0xf)
4783 &~ (bfd_size_type) 0xf);
4784 }
4785 }
4786 loadable_size += MIPS_FUNCTION_STUB_SIZE;
4787
4788 /* Assume there are two loadable segments consisting of
4789 contiguous sections. Is 5 enough? */
4790 local_gotno = (loadable_size >> 16) + 5;
4791 if (NEWABI_P (output_bfd))
4792 /* It's possible we will need GOT_PAGE entries as well as
4793 GOT16 entries. Often, these will be able to share GOT
4794 entries, but not always. */
4795 local_gotno *= 2;
4796
4797 g->local_gotno += local_gotno;
4798 s->_raw_size += local_gotno * MIPS_ELF_GOT_SIZE (dynobj);
4799
4800 /* There has to be a global GOT entry for every symbol with
4801 a dynamic symbol table index of DT_MIPS_GOTSYM or
4802 higher. Therefore, it make sense to put those symbols
4803 that need GOT entries at the end of the symbol table. We
4804 do that here. */
4805 if (! mips_elf_sort_hash_table (info, 1))
4806 return false;
4807
4808 if (g->global_gotsym != NULL)
4809 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
4810 else
4811 /* If there are no global symbols, or none requiring
4812 relocations, then GLOBAL_GOTSYM will be NULL. */
4813 i = 0;
4814 g->global_gotno = i;
4815 s->_raw_size += i * MIPS_ELF_GOT_SIZE (dynobj);
4816 }
4817 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
4818 {
4819 /* IRIX rld assumes that the function stub isn't at the end
4820 of .text section. So put a dummy. XXX */
4821 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
4822 }
4823 else if (! info->shared
4824 && ! mips_elf_hash_table (info)->use_rld_obj_head
4825 && strncmp (name, ".rld_map", 8) == 0)
4826 {
4827 /* We add a room for __rld_map. It will be filled in by the
4828 rtld to contain a pointer to the _r_debug structure. */
4829 s->_raw_size += 4;
4830 }
4831 else if (SGI_COMPAT (output_bfd)
4832 && strncmp (name, ".compact_rel", 12) == 0)
4833 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
4834 else if (strcmp (name, ".msym") == 0)
4835 s->_raw_size = (sizeof (Elf32_External_Msym)
4836 * (elf_hash_table (info)->dynsymcount
4837 + bfd_count_sections (output_bfd)));
4838 else if (strncmp (name, ".init", 5) != 0)
4839 {
4840 /* It's not one of our sections, so don't allocate space. */
4841 continue;
4842 }
4843
4844 if (strip)
4845 {
4846 _bfd_strip_section_from_output (info, s);
4847 continue;
4848 }
4849
4850 /* Allocate memory for the section contents. */
4851 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4852 if (s->contents == NULL && s->_raw_size != 0)
4853 {
4854 bfd_set_error (bfd_error_no_memory);
4855 return false;
4856 }
4857 }
4858
4859 if (elf_hash_table (info)->dynamic_sections_created)
4860 {
4861 /* Add some entries to the .dynamic section. We fill in the
4862 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
4863 must add the entries now so that we get the correct size for
4864 the .dynamic section. The DT_DEBUG entry is filled in by the
4865 dynamic linker and used by the debugger. */
4866 if (! info->shared)
4867 {
4868 /* SGI object has the equivalence of DT_DEBUG in the
4869 DT_MIPS_RLD_MAP entry. */
4870 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
4871 return false;
4872 if (!SGI_COMPAT (output_bfd))
4873 {
4874 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
4875 return false;
4876 }
4877 }
4878 else
4879 {
4880 /* Shared libraries on traditional mips have DT_DEBUG. */
4881 if (!SGI_COMPAT (output_bfd))
4882 {
4883 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
4884 return false;
4885 }
4886 }
4887
4888 if (reltext && SGI_COMPAT (output_bfd))
4889 info->flags |= DF_TEXTREL;
4890
4891 if ((info->flags & DF_TEXTREL) != 0)
4892 {
4893 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
4894 return false;
4895 }
4896
4897 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
4898 return false;
4899
4900 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
4901 {
4902 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
4903 return false;
4904
4905 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
4906 return false;
4907
4908 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
4909 return false;
4910 }
4911
4912 if (SGI_COMPAT (output_bfd))
4913 {
4914 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICTNO, 0))
4915 return false;
4916 }
4917
4918 if (SGI_COMPAT (output_bfd))
4919 {
4920 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLISTNO, 0))
4921 return false;
4922 }
4923
4924 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
4925 {
4926 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICT, 0))
4927 return false;
4928
4929 s = bfd_get_section_by_name (dynobj, ".liblist");
4930 BFD_ASSERT (s != NULL);
4931
4932 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLIST, 0))
4933 return false;
4934 }
4935
4936 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
4937 return false;
4938
4939 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
4940 return false;
4941
4942 #if 0
4943 /* Time stamps in executable files are a bad idea. */
4944 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_TIME_STAMP, 0))
4945 return false;
4946 #endif
4947
4948 #if 0 /* FIXME */
4949 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_ICHECKSUM, 0))
4950 return false;
4951 #endif
4952
4953 #if 0 /* FIXME */
4954 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_IVERSION, 0))
4955 return false;
4956 #endif
4957
4958 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
4959 return false;
4960
4961 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
4962 return false;
4963
4964 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
4965 return false;
4966
4967 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
4968 return false;
4969
4970 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
4971 return false;
4972
4973 if (IRIX_COMPAT (dynobj) == ict_irix5
4974 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
4975 return false;
4976
4977 if (IRIX_COMPAT (dynobj) == ict_irix6
4978 && (bfd_get_section_by_name
4979 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
4980 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
4981 return false;
4982
4983 if (bfd_get_section_by_name (dynobj, ".msym")
4984 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_MSYM, 0))
4985 return false;
4986 }
4987
4988 return true;
4989 }
4990 \f
4991 /* Relocate a MIPS ELF section. */
4992
4993 boolean
4994 _bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4995 contents, relocs, local_syms, local_sections)
4996 bfd *output_bfd;
4997 struct bfd_link_info *info;
4998 bfd *input_bfd;
4999 asection *input_section;
5000 bfd_byte *contents;
5001 Elf_Internal_Rela *relocs;
5002 Elf_Internal_Sym *local_syms;
5003 asection **local_sections;
5004 {
5005 Elf_Internal_Rela *rel;
5006 const Elf_Internal_Rela *relend;
5007 bfd_vma addend = 0;
5008 boolean use_saved_addend_p = false;
5009 struct elf_backend_data *bed;
5010
5011 bed = get_elf_backend_data (output_bfd);
5012 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
5013 for (rel = relocs; rel < relend; ++rel)
5014 {
5015 const char *name;
5016 bfd_vma value;
5017 reloc_howto_type *howto;
5018 boolean require_jalx;
5019 /* True if the relocation is a RELA relocation, rather than a
5020 REL relocation. */
5021 boolean rela_relocation_p = true;
5022 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5023 const char * msg = (const char *) NULL;
5024
5025 /* Find the relocation howto for this relocation. */
5026 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
5027 {
5028 /* Some 32-bit code uses R_MIPS_64. In particular, people use
5029 64-bit code, but make sure all their addresses are in the
5030 lowermost or uppermost 32-bit section of the 64-bit address
5031 space. Thus, when they use an R_MIPS_64 they mean what is
5032 usually meant by R_MIPS_32, with the exception that the
5033 stored value is sign-extended to 64 bits. */
5034 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
5035
5036 /* On big-endian systems, we need to lie about the position
5037 of the reloc. */
5038 if (bfd_big_endian (input_bfd))
5039 rel->r_offset += 4;
5040 }
5041 else
5042 /* NewABI defaults to RELA relocations. */
5043 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
5044 NEWABI_P (input_bfd));
5045
5046 if (!use_saved_addend_p)
5047 {
5048 Elf_Internal_Shdr *rel_hdr;
5049
5050 /* If these relocations were originally of the REL variety,
5051 we must pull the addend out of the field that will be
5052 relocated. Otherwise, we simply use the contents of the
5053 RELA relocation. To determine which flavor or relocation
5054 this is, we depend on the fact that the INPUT_SECTION's
5055 REL_HDR is read before its REL_HDR2. */
5056 rel_hdr = &elf_section_data (input_section)->rel_hdr;
5057 if ((size_t) (rel - relocs)
5058 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
5059 rel_hdr = elf_section_data (input_section)->rel_hdr2;
5060 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
5061 {
5062 /* Note that this is a REL relocation. */
5063 rela_relocation_p = false;
5064
5065 /* Get the addend, which is stored in the input file. */
5066 addend = mips_elf_obtain_contents (howto, rel, input_bfd,
5067 contents);
5068 addend &= howto->src_mask;
5069 addend <<= howto->rightshift;
5070
5071 /* For some kinds of relocations, the ADDEND is a
5072 combination of the addend stored in two different
5073 relocations. */
5074 if (r_type == R_MIPS_HI16
5075 || r_type == R_MIPS_GNU_REL_HI16
5076 || (r_type == R_MIPS_GOT16
5077 && mips_elf_local_relocation_p (input_bfd, rel,
5078 local_sections, false)))
5079 {
5080 bfd_vma l;
5081 const Elf_Internal_Rela *lo16_relocation;
5082 reloc_howto_type *lo16_howto;
5083 unsigned int lo;
5084
5085 /* The combined value is the sum of the HI16 addend,
5086 left-shifted by sixteen bits, and the LO16
5087 addend, sign extended. (Usually, the code does
5088 a `lui' of the HI16 value, and then an `addiu' of
5089 the LO16 value.)
5090
5091 Scan ahead to find a matching LO16 relocation. */
5092 if (r_type == R_MIPS_GNU_REL_HI16)
5093 lo = R_MIPS_GNU_REL_LO16;
5094 else
5095 lo = R_MIPS_LO16;
5096 lo16_relocation = mips_elf_next_relocation (input_bfd, lo,
5097 rel, relend);
5098 if (lo16_relocation == NULL)
5099 return false;
5100
5101 /* Obtain the addend kept there. */
5102 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, lo, false);
5103 l = mips_elf_obtain_contents (lo16_howto, lo16_relocation,
5104 input_bfd, contents);
5105 l &= lo16_howto->src_mask;
5106 l <<= lo16_howto->rightshift;
5107 l = mips_elf_sign_extend (l, 16);
5108
5109 addend <<= 16;
5110
5111 /* Compute the combined addend. */
5112 addend += l;
5113
5114 /* If PC-relative, subtract the difference between the
5115 address of the LO part of the reloc and the address of
5116 the HI part. The relocation is relative to the LO
5117 part, but mips_elf_calculate_relocation() doesn't
5118 know its address or the difference from the HI part, so
5119 we subtract that difference here. See also the
5120 comment in mips_elf_calculate_relocation(). */
5121 if (r_type == R_MIPS_GNU_REL_HI16)
5122 addend -= (lo16_relocation->r_offset - rel->r_offset);
5123 }
5124 else if (r_type == R_MIPS16_GPREL)
5125 {
5126 /* The addend is scrambled in the object file. See
5127 mips_elf_perform_relocation for details on the
5128 format. */
5129 addend = (((addend & 0x1f0000) >> 5)
5130 | ((addend & 0x7e00000) >> 16)
5131 | (addend & 0x1f));
5132 }
5133 }
5134 else
5135 addend = rel->r_addend;
5136 }
5137
5138 if (info->relocateable)
5139 {
5140 Elf_Internal_Sym *sym;
5141 unsigned long r_symndx;
5142
5143 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
5144 && bfd_big_endian (input_bfd))
5145 rel->r_offset -= 4;
5146
5147 /* Since we're just relocating, all we need to do is copy
5148 the relocations back out to the object file, unless
5149 they're against a section symbol, in which case we need
5150 to adjust by the section offset, or unless they're GP
5151 relative in which case we need to adjust by the amount
5152 that we're adjusting GP in this relocateable object. */
5153
5154 if (! mips_elf_local_relocation_p (input_bfd, rel, local_sections,
5155 false))
5156 /* There's nothing to do for non-local relocations. */
5157 continue;
5158
5159 if (r_type == R_MIPS16_GPREL
5160 || r_type == R_MIPS_GPREL16
5161 || r_type == R_MIPS_GPREL32
5162 || r_type == R_MIPS_LITERAL)
5163 addend -= (_bfd_get_gp_value (output_bfd)
5164 - _bfd_get_gp_value (input_bfd));
5165
5166 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
5167 sym = local_syms + r_symndx;
5168 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5169 /* Adjust the addend appropriately. */
5170 addend += local_sections[r_symndx]->output_offset;
5171
5172 if (howto->partial_inplace)
5173 {
5174 /* If the relocation is for a R_MIPS_HI16 or R_MIPS_GOT16,
5175 then we only want to write out the high-order 16 bits.
5176 The subsequent R_MIPS_LO16 will handle the low-order bits.
5177 */
5178 if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16
5179 || r_type == R_MIPS_GNU_REL_HI16)
5180 addend = mips_elf_high (addend);
5181 else if (r_type == R_MIPS_HIGHER)
5182 addend = mips_elf_higher (addend);
5183 else if (r_type == R_MIPS_HIGHEST)
5184 addend = mips_elf_highest (addend);
5185 }
5186
5187 if (rela_relocation_p)
5188 /* If this is a RELA relocation, just update the addend.
5189 We have to cast away constness for REL. */
5190 rel->r_addend = addend;
5191 else
5192 {
5193 /* Otherwise, we have to write the value back out. Note
5194 that we use the source mask, rather than the
5195 destination mask because the place to which we are
5196 writing will be source of the addend in the final
5197 link. */
5198 addend >>= howto->rightshift;
5199 addend &= howto->src_mask;
5200
5201 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
5202 /* See the comment above about using R_MIPS_64 in the 32-bit
5203 ABI. Here, we need to update the addend. It would be
5204 possible to get away with just using the R_MIPS_32 reloc
5205 but for endianness. */
5206 {
5207 bfd_vma sign_bits;
5208 bfd_vma low_bits;
5209 bfd_vma high_bits;
5210
5211 if (addend & ((bfd_vma) 1 << 31))
5212 #ifdef BFD64
5213 sign_bits = ((bfd_vma) 1 << 32) - 1;
5214 #else
5215 sign_bits = -1;
5216 #endif
5217 else
5218 sign_bits = 0;
5219
5220 /* If we don't know that we have a 64-bit type,
5221 do two separate stores. */
5222 if (bfd_big_endian (input_bfd))
5223 {
5224 /* Store the sign-bits (which are most significant)
5225 first. */
5226 low_bits = sign_bits;
5227 high_bits = addend;
5228 }
5229 else
5230 {
5231 low_bits = addend;
5232 high_bits = sign_bits;
5233 }
5234 bfd_put_32 (input_bfd, low_bits,
5235 contents + rel->r_offset);
5236 bfd_put_32 (input_bfd, high_bits,
5237 contents + rel->r_offset + 4);
5238 continue;
5239 }
5240
5241 if (! mips_elf_perform_relocation (info, howto, rel, addend,
5242 input_bfd, input_section,
5243 contents, false))
5244 return false;
5245 }
5246
5247 /* Go on to the next relocation. */
5248 continue;
5249 }
5250
5251 /* In the N32 and 64-bit ABIs there may be multiple consecutive
5252 relocations for the same offset. In that case we are
5253 supposed to treat the output of each relocation as the addend
5254 for the next. */
5255 if (rel + 1 < relend
5256 && rel->r_offset == rel[1].r_offset
5257 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
5258 use_saved_addend_p = true;
5259 else
5260 use_saved_addend_p = false;
5261
5262 addend >>= howto->rightshift;
5263
5264 /* Figure out what value we are supposed to relocate. */
5265 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
5266 input_section, info, rel,
5267 addend, howto, local_syms,
5268 local_sections, &value,
5269 &name, &require_jalx,
5270 use_saved_addend_p))
5271 {
5272 case bfd_reloc_continue:
5273 /* There's nothing to do. */
5274 continue;
5275
5276 case bfd_reloc_undefined:
5277 /* mips_elf_calculate_relocation already called the
5278 undefined_symbol callback. There's no real point in
5279 trying to perform the relocation at this point, so we
5280 just skip ahead to the next relocation. */
5281 continue;
5282
5283 case bfd_reloc_notsupported:
5284 msg = _("internal error: unsupported relocation error");
5285 info->callbacks->warning
5286 (info, msg, name, input_bfd, input_section, rel->r_offset);
5287 return false;
5288
5289 case bfd_reloc_overflow:
5290 if (use_saved_addend_p)
5291 /* Ignore overflow until we reach the last relocation for
5292 a given location. */
5293 ;
5294 else
5295 {
5296 BFD_ASSERT (name != NULL);
5297 if (! ((*info->callbacks->reloc_overflow)
5298 (info, name, howto->name, (bfd_vma) 0,
5299 input_bfd, input_section, rel->r_offset)))
5300 return false;
5301 }
5302 break;
5303
5304 case bfd_reloc_ok:
5305 break;
5306
5307 default:
5308 abort ();
5309 break;
5310 }
5311
5312 /* If we've got another relocation for the address, keep going
5313 until we reach the last one. */
5314 if (use_saved_addend_p)
5315 {
5316 addend = value;
5317 continue;
5318 }
5319
5320 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
5321 /* See the comment above about using R_MIPS_64 in the 32-bit
5322 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
5323 that calculated the right value. Now, however, we
5324 sign-extend the 32-bit result to 64-bits, and store it as a
5325 64-bit value. We are especially generous here in that we
5326 go to extreme lengths to support this usage on systems with
5327 only a 32-bit VMA. */
5328 {
5329 bfd_vma sign_bits;
5330 bfd_vma low_bits;
5331 bfd_vma high_bits;
5332
5333 if (value & ((bfd_vma) 1 << 31))
5334 #ifdef BFD64
5335 sign_bits = ((bfd_vma) 1 << 32) - 1;
5336 #else
5337 sign_bits = -1;
5338 #endif
5339 else
5340 sign_bits = 0;
5341
5342 /* If we don't know that we have a 64-bit type,
5343 do two separate stores. */
5344 if (bfd_big_endian (input_bfd))
5345 {
5346 /* Undo what we did above. */
5347 rel->r_offset -= 4;
5348 /* Store the sign-bits (which are most significant)
5349 first. */
5350 low_bits = sign_bits;
5351 high_bits = value;
5352 }
5353 else
5354 {
5355 low_bits = value;
5356 high_bits = sign_bits;
5357 }
5358 bfd_put_32 (input_bfd, low_bits,
5359 contents + rel->r_offset);
5360 bfd_put_32 (input_bfd, high_bits,
5361 contents + rel->r_offset + 4);
5362 continue;
5363 }
5364
5365 /* Actually perform the relocation. */
5366 if (! mips_elf_perform_relocation (info, howto, rel, value,
5367 input_bfd, input_section,
5368 contents, require_jalx))
5369 return false;
5370 }
5371
5372 return true;
5373 }
5374 \f
5375 /* If NAME is one of the special IRIX6 symbols defined by the linker,
5376 adjust it appropriately now. */
5377
5378 static void
5379 mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym)
5380 bfd *abfd ATTRIBUTE_UNUSED;
5381 const char *name;
5382 Elf_Internal_Sym *sym;
5383 {
5384 /* The linker script takes care of providing names and values for
5385 these, but we must place them into the right sections. */
5386 static const char* const text_section_symbols[] = {
5387 "_ftext",
5388 "_etext",
5389 "__dso_displacement",
5390 "__elf_header",
5391 "__program_header_table",
5392 NULL
5393 };
5394
5395 static const char* const data_section_symbols[] = {
5396 "_fdata",
5397 "_edata",
5398 "_end",
5399 "_fbss",
5400 NULL
5401 };
5402
5403 const char* const *p;
5404 int i;
5405
5406 for (i = 0; i < 2; ++i)
5407 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
5408 *p;
5409 ++p)
5410 if (strcmp (*p, name) == 0)
5411 {
5412 /* All of these symbols are given type STT_SECTION by the
5413 IRIX6 linker. */
5414 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5415
5416 /* The IRIX linker puts these symbols in special sections. */
5417 if (i == 0)
5418 sym->st_shndx = SHN_MIPS_TEXT;
5419 else
5420 sym->st_shndx = SHN_MIPS_DATA;
5421
5422 break;
5423 }
5424 }
5425
5426 /* Finish up dynamic symbol handling. We set the contents of various
5427 dynamic sections here. */
5428
5429 boolean
5430 _bfd_mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
5431 bfd *output_bfd;
5432 struct bfd_link_info *info;
5433 struct elf_link_hash_entry *h;
5434 Elf_Internal_Sym *sym;
5435 {
5436 bfd *dynobj;
5437 bfd_vma gval;
5438 asection *sgot;
5439 asection *smsym;
5440 struct mips_got_info *g;
5441 const char *name;
5442 struct mips_elf_link_hash_entry *mh;
5443
5444 dynobj = elf_hash_table (info)->dynobj;
5445 gval = sym->st_value;
5446 mh = (struct mips_elf_link_hash_entry *) h;
5447
5448 if (h->plt.offset != (bfd_vma) -1)
5449 {
5450 asection *s;
5451 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
5452
5453 /* This symbol has a stub. Set it up. */
5454
5455 BFD_ASSERT (h->dynindx != -1);
5456
5457 s = bfd_get_section_by_name (dynobj,
5458 MIPS_ELF_STUB_SECTION_NAME (dynobj));
5459 BFD_ASSERT (s != NULL);
5460
5461 /* FIXME: Can h->dynindex be more than 64K? */
5462 if (h->dynindx & 0xffff0000)
5463 return false;
5464
5465 /* Fill the stub. */
5466 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub);
5467 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + 4);
5468 bfd_put_32 (output_bfd, STUB_JALR, stub + 8);
5469 bfd_put_32 (output_bfd, STUB_LI16 (output_bfd) + h->dynindx, stub + 12);
5470
5471 BFD_ASSERT (h->plt.offset <= s->_raw_size);
5472 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
5473
5474 /* Mark the symbol as undefined. plt.offset != -1 occurs
5475 only for the referenced symbol. */
5476 sym->st_shndx = SHN_UNDEF;
5477
5478 /* The run-time linker uses the st_value field of the symbol
5479 to reset the global offset table entry for this external
5480 to its stub address when unlinking a shared object. */
5481 gval = s->output_section->vma + s->output_offset + h->plt.offset;
5482 sym->st_value = gval;
5483 }
5484
5485 BFD_ASSERT (h->dynindx != -1
5486 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0);
5487
5488 sgot = mips_elf_got_section (dynobj);
5489 BFD_ASSERT (sgot != NULL);
5490 BFD_ASSERT (elf_section_data (sgot) != NULL);
5491 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5492 BFD_ASSERT (g != NULL);
5493
5494 /* Run through the global symbol table, creating GOT entries for all
5495 the symbols that need them. */
5496 if (g->global_gotsym != NULL
5497 && h->dynindx >= g->global_gotsym->dynindx)
5498 {
5499 bfd_vma offset;
5500 bfd_vma value;
5501
5502 if (sym->st_value)
5503 value = sym->st_value;
5504 else
5505 {
5506 /* For an entity defined in a shared object, this will be
5507 NULL. (For functions in shared objects for
5508 which we have created stubs, ST_VALUE will be non-NULL.
5509 That's because such the functions are now no longer defined
5510 in a shared object.) */
5511
5512 if (info->shared && h->root.type == bfd_link_hash_undefined)
5513 value = 0;
5514 else
5515 value = h->root.u.def.value;
5516 }
5517 offset = mips_elf_global_got_index (dynobj, h);
5518 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
5519 }
5520
5521 /* Create a .msym entry, if appropriate. */
5522 smsym = bfd_get_section_by_name (dynobj, ".msym");
5523 if (smsym)
5524 {
5525 Elf32_Internal_Msym msym;
5526
5527 msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
5528 /* It is undocumented what the `1' indicates, but IRIX6 uses
5529 this value. */
5530 msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
5531 bfd_mips_elf_swap_msym_out
5532 (dynobj, &msym,
5533 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
5534 }
5535
5536 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5537 name = h->root.root.string;
5538 if (strcmp (name, "_DYNAMIC") == 0
5539 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
5540 sym->st_shndx = SHN_ABS;
5541 else if (strcmp (name, "_DYNAMIC_LINK") == 0
5542 || strcmp (name, "_DYNAMIC_LINKING") == 0)
5543 {
5544 sym->st_shndx = SHN_ABS;
5545 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5546 sym->st_value = 1;
5547 }
5548 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
5549 {
5550 sym->st_shndx = SHN_ABS;
5551 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5552 sym->st_value = elf_gp (output_bfd);
5553 }
5554 else if (SGI_COMPAT (output_bfd))
5555 {
5556 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
5557 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
5558 {
5559 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5560 sym->st_other = STO_PROTECTED;
5561 sym->st_value = 0;
5562 sym->st_shndx = SHN_MIPS_DATA;
5563 }
5564 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
5565 {
5566 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5567 sym->st_other = STO_PROTECTED;
5568 sym->st_value = mips_elf_hash_table (info)->procedure_count;
5569 sym->st_shndx = SHN_ABS;
5570 }
5571 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
5572 {
5573 if (h->type == STT_FUNC)
5574 sym->st_shndx = SHN_MIPS_TEXT;
5575 else if (h->type == STT_OBJECT)
5576 sym->st_shndx = SHN_MIPS_DATA;
5577 }
5578 }
5579
5580 /* Handle the IRIX6-specific symbols. */
5581 if (IRIX_COMPAT (output_bfd) == ict_irix6)
5582 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
5583
5584 if (! info->shared)
5585 {
5586 if (! mips_elf_hash_table (info)->use_rld_obj_head
5587 && (strcmp (name, "__rld_map") == 0
5588 || strcmp (name, "__RLD_MAP") == 0))
5589 {
5590 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
5591 BFD_ASSERT (s != NULL);
5592 sym->st_value = s->output_section->vma + s->output_offset;
5593 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
5594 if (mips_elf_hash_table (info)->rld_value == 0)
5595 mips_elf_hash_table (info)->rld_value = sym->st_value;
5596 }
5597 else if (mips_elf_hash_table (info)->use_rld_obj_head
5598 && strcmp (name, "__rld_obj_head") == 0)
5599 {
5600 /* IRIX6 does not use a .rld_map section. */
5601 if (IRIX_COMPAT (output_bfd) == ict_irix5
5602 || IRIX_COMPAT (output_bfd) == ict_none)
5603 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
5604 != NULL);
5605 mips_elf_hash_table (info)->rld_value = sym->st_value;
5606 }
5607 }
5608
5609 /* If this is a mips16 symbol, force the value to be even. */
5610 if (sym->st_other == STO_MIPS16
5611 && (sym->st_value & 1) != 0)
5612 --sym->st_value;
5613
5614 return true;
5615 }
5616
5617 /* Finish up the dynamic sections. */
5618
5619 boolean
5620 _bfd_mips_elf_finish_dynamic_sections (output_bfd, info)
5621 bfd *output_bfd;
5622 struct bfd_link_info *info;
5623 {
5624 bfd *dynobj;
5625 asection *sdyn;
5626 asection *sgot;
5627 struct mips_got_info *g;
5628
5629 dynobj = elf_hash_table (info)->dynobj;
5630
5631 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5632
5633 sgot = bfd_get_section_by_name (dynobj, ".got");
5634 if (sgot == NULL)
5635 g = NULL;
5636 else
5637 {
5638 BFD_ASSERT (elf_section_data (sgot) != NULL);
5639 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5640 BFD_ASSERT (g != NULL);
5641 }
5642
5643 if (elf_hash_table (info)->dynamic_sections_created)
5644 {
5645 bfd_byte *b;
5646
5647 BFD_ASSERT (sdyn != NULL);
5648 BFD_ASSERT (g != NULL);
5649
5650 for (b = sdyn->contents;
5651 b < sdyn->contents + sdyn->_raw_size;
5652 b += MIPS_ELF_DYN_SIZE (dynobj))
5653 {
5654 Elf_Internal_Dyn dyn;
5655 const char *name;
5656 size_t elemsize;
5657 asection *s;
5658 boolean swap_out_p;
5659
5660 /* Read in the current dynamic entry. */
5661 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
5662
5663 /* Assume that we're going to modify it and write it out. */
5664 swap_out_p = true;
5665
5666 switch (dyn.d_tag)
5667 {
5668 case DT_RELENT:
5669 s = (bfd_get_section_by_name (dynobj, ".rel.dyn"));
5670 BFD_ASSERT (s != NULL);
5671 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
5672 break;
5673
5674 case DT_STRSZ:
5675 /* Rewrite DT_STRSZ. */
5676 dyn.d_un.d_val =
5677 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5678 break;
5679
5680 case DT_PLTGOT:
5681 name = ".got";
5682 goto get_vma;
5683 case DT_MIPS_CONFLICT:
5684 name = ".conflict";
5685 goto get_vma;
5686 case DT_MIPS_LIBLIST:
5687 name = ".liblist";
5688 get_vma:
5689 s = bfd_get_section_by_name (output_bfd, name);
5690 BFD_ASSERT (s != NULL);
5691 dyn.d_un.d_ptr = s->vma;
5692 break;
5693
5694 case DT_MIPS_RLD_VERSION:
5695 dyn.d_un.d_val = 1; /* XXX */
5696 break;
5697
5698 case DT_MIPS_FLAGS:
5699 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
5700 break;
5701
5702 case DT_MIPS_CONFLICTNO:
5703 name = ".conflict";
5704 elemsize = sizeof (Elf32_Conflict);
5705 goto set_elemno;
5706
5707 case DT_MIPS_LIBLISTNO:
5708 name = ".liblist";
5709 elemsize = sizeof (Elf32_Lib);
5710 set_elemno:
5711 s = bfd_get_section_by_name (output_bfd, name);
5712 if (s != NULL)
5713 {
5714 if (s->_cooked_size != 0)
5715 dyn.d_un.d_val = s->_cooked_size / elemsize;
5716 else
5717 dyn.d_un.d_val = s->_raw_size / elemsize;
5718 }
5719 else
5720 dyn.d_un.d_val = 0;
5721 break;
5722
5723 case DT_MIPS_TIME_STAMP:
5724 time ((time_t *) &dyn.d_un.d_val);
5725 break;
5726
5727 case DT_MIPS_ICHECKSUM:
5728 /* XXX FIXME: */
5729 swap_out_p = false;
5730 break;
5731
5732 case DT_MIPS_IVERSION:
5733 /* XXX FIXME: */
5734 swap_out_p = false;
5735 break;
5736
5737 case DT_MIPS_BASE_ADDRESS:
5738 s = output_bfd->sections;
5739 BFD_ASSERT (s != NULL);
5740 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
5741 break;
5742
5743 case DT_MIPS_LOCAL_GOTNO:
5744 dyn.d_un.d_val = g->local_gotno;
5745 break;
5746
5747 case DT_MIPS_UNREFEXTNO:
5748 /* The index into the dynamic symbol table which is the
5749 entry of the first external symbol that is not
5750 referenced within the same object. */
5751 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
5752 break;
5753
5754 case DT_MIPS_GOTSYM:
5755 if (g->global_gotsym)
5756 {
5757 dyn.d_un.d_val = g->global_gotsym->dynindx;
5758 break;
5759 }
5760 /* In case if we don't have global got symbols we default
5761 to setting DT_MIPS_GOTSYM to the same value as
5762 DT_MIPS_SYMTABNO, so we just fall through. */
5763
5764 case DT_MIPS_SYMTABNO:
5765 name = ".dynsym";
5766 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
5767 s = bfd_get_section_by_name (output_bfd, name);
5768 BFD_ASSERT (s != NULL);
5769
5770 if (s->_cooked_size != 0)
5771 dyn.d_un.d_val = s->_cooked_size / elemsize;
5772 else
5773 dyn.d_un.d_val = s->_raw_size / elemsize;
5774 break;
5775
5776 case DT_MIPS_HIPAGENO:
5777 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
5778 break;
5779
5780 case DT_MIPS_RLD_MAP:
5781 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
5782 break;
5783
5784 case DT_MIPS_OPTIONS:
5785 s = (bfd_get_section_by_name
5786 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
5787 dyn.d_un.d_ptr = s->vma;
5788 break;
5789
5790 case DT_MIPS_MSYM:
5791 s = (bfd_get_section_by_name (output_bfd, ".msym"));
5792 dyn.d_un.d_ptr = s->vma;
5793 break;
5794
5795 default:
5796 swap_out_p = false;
5797 break;
5798 }
5799
5800 if (swap_out_p)
5801 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
5802 (dynobj, &dyn, b);
5803 }
5804 }
5805
5806 /* The first entry of the global offset table will be filled at
5807 runtime. The second entry will be used by some runtime loaders.
5808 This isn't the case of IRIX rld. */
5809 if (sgot != NULL && sgot->_raw_size > 0)
5810 {
5811 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
5812 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000,
5813 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
5814 }
5815
5816 if (sgot != NULL)
5817 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
5818 = MIPS_ELF_GOT_SIZE (output_bfd);
5819
5820 {
5821 asection *smsym;
5822 asection *s;
5823 Elf32_compact_rel cpt;
5824
5825 /* ??? The section symbols for the output sections were set up in
5826 _bfd_elf_final_link. SGI sets the STT_NOTYPE attribute for these
5827 symbols. Should we do so? */
5828
5829 smsym = bfd_get_section_by_name (dynobj, ".msym");
5830 if (smsym != NULL)
5831 {
5832 Elf32_Internal_Msym msym;
5833
5834 msym.ms_hash_value = 0;
5835 msym.ms_info = ELF32_MS_INFO (0, 1);
5836
5837 for (s = output_bfd->sections; s != NULL; s = s->next)
5838 {
5839 long dynindx = elf_section_data (s)->dynindx;
5840
5841 bfd_mips_elf_swap_msym_out
5842 (output_bfd, &msym,
5843 (((Elf32_External_Msym *) smsym->contents)
5844 + dynindx));
5845 }
5846 }
5847
5848 if (SGI_COMPAT (output_bfd))
5849 {
5850 /* Write .compact_rel section out. */
5851 s = bfd_get_section_by_name (dynobj, ".compact_rel");
5852 if (s != NULL)
5853 {
5854 cpt.id1 = 1;
5855 cpt.num = s->reloc_count;
5856 cpt.id2 = 2;
5857 cpt.offset = (s->output_section->filepos
5858 + sizeof (Elf32_External_compact_rel));
5859 cpt.reserved0 = 0;
5860 cpt.reserved1 = 0;
5861 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
5862 ((Elf32_External_compact_rel *)
5863 s->contents));
5864
5865 /* Clean up a dummy stub function entry in .text. */
5866 s = bfd_get_section_by_name (dynobj,
5867 MIPS_ELF_STUB_SECTION_NAME (dynobj));
5868 if (s != NULL)
5869 {
5870 file_ptr dummy_offset;
5871
5872 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
5873 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
5874 memset (s->contents + dummy_offset, 0,
5875 MIPS_FUNCTION_STUB_SIZE);
5876 }
5877 }
5878 }
5879
5880 /* We need to sort the entries of the dynamic relocation section. */
5881
5882 if (!ABI_64_P (output_bfd))
5883 {
5884 asection *reldyn;
5885
5886 reldyn = bfd_get_section_by_name (dynobj, ".rel.dyn");
5887 if (reldyn != NULL && reldyn->reloc_count > 2)
5888 {
5889 reldyn_sorting_bfd = output_bfd;
5890 qsort ((Elf32_External_Rel *) reldyn->contents + 1,
5891 (size_t) reldyn->reloc_count - 1,
5892 sizeof (Elf32_External_Rel), sort_dynamic_relocs);
5893 }
5894 }
5895
5896 /* Clean up a first relocation in .rel.dyn. */
5897 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
5898 if (s != NULL && s->_raw_size > 0)
5899 memset (s->contents, 0, MIPS_ELF_REL_SIZE (dynobj));
5900 }
5901
5902 return true;
5903 }
5904
5905 /* The final processing done just before writing out a MIPS ELF object
5906 file. This gets the MIPS architecture right based on the machine
5907 number. This is used by both the 32-bit and the 64-bit ABI. */
5908
5909 void
5910 _bfd_mips_elf_final_write_processing (abfd, linker)
5911 bfd *abfd;
5912 boolean linker ATTRIBUTE_UNUSED;
5913 {
5914 unsigned long val;
5915 unsigned int i;
5916 Elf_Internal_Shdr **hdrpp;
5917 const char *name;
5918 asection *sec;
5919
5920 switch (bfd_get_mach (abfd))
5921 {
5922 default:
5923 case bfd_mach_mips3000:
5924 val = E_MIPS_ARCH_1;
5925 break;
5926
5927 case bfd_mach_mips3900:
5928 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
5929 break;
5930
5931 case bfd_mach_mips6000:
5932 val = E_MIPS_ARCH_2;
5933 break;
5934
5935 case bfd_mach_mips4000:
5936 case bfd_mach_mips4300:
5937 case bfd_mach_mips4400:
5938 case bfd_mach_mips4600:
5939 val = E_MIPS_ARCH_3;
5940 break;
5941
5942 case bfd_mach_mips4010:
5943 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
5944 break;
5945
5946 case bfd_mach_mips4100:
5947 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
5948 break;
5949
5950 case bfd_mach_mips4111:
5951 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
5952 break;
5953
5954 case bfd_mach_mips4120:
5955 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
5956 break;
5957
5958 case bfd_mach_mips4650:
5959 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
5960 break;
5961
5962 case bfd_mach_mips5400:
5963 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
5964 break;
5965
5966 case bfd_mach_mips5500:
5967 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
5968 break;
5969
5970 case bfd_mach_mips5000:
5971 case bfd_mach_mips8000:
5972 case bfd_mach_mips10000:
5973 case bfd_mach_mips12000:
5974 val = E_MIPS_ARCH_4;
5975 break;
5976
5977 case bfd_mach_mips5:
5978 val = E_MIPS_ARCH_5;
5979 break;
5980
5981 case bfd_mach_mips_sb1:
5982 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
5983 break;
5984
5985 case bfd_mach_mipsisa32:
5986 val = E_MIPS_ARCH_32;
5987 break;
5988
5989 case bfd_mach_mipsisa64:
5990 val = E_MIPS_ARCH_64;
5991 }
5992
5993 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
5994 elf_elfheader (abfd)->e_flags |= val;
5995
5996 /* Set the sh_info field for .gptab sections and other appropriate
5997 info for each special section. */
5998 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
5999 i < elf_numsections (abfd);
6000 i++, hdrpp++)
6001 {
6002 switch ((*hdrpp)->sh_type)
6003 {
6004 case SHT_MIPS_MSYM:
6005 case SHT_MIPS_LIBLIST:
6006 sec = bfd_get_section_by_name (abfd, ".dynstr");
6007 if (sec != NULL)
6008 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
6009 break;
6010
6011 case SHT_MIPS_GPTAB:
6012 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
6013 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
6014 BFD_ASSERT (name != NULL
6015 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
6016 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
6017 BFD_ASSERT (sec != NULL);
6018 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
6019 break;
6020
6021 case SHT_MIPS_CONTENT:
6022 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
6023 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
6024 BFD_ASSERT (name != NULL
6025 && strncmp (name, ".MIPS.content",
6026 sizeof ".MIPS.content" - 1) == 0);
6027 sec = bfd_get_section_by_name (abfd,
6028 name + sizeof ".MIPS.content" - 1);
6029 BFD_ASSERT (sec != NULL);
6030 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
6031 break;
6032
6033 case SHT_MIPS_SYMBOL_LIB:
6034 sec = bfd_get_section_by_name (abfd, ".dynsym");
6035 if (sec != NULL)
6036 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
6037 sec = bfd_get_section_by_name (abfd, ".liblist");
6038 if (sec != NULL)
6039 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
6040 break;
6041
6042 case SHT_MIPS_EVENTS:
6043 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
6044 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
6045 BFD_ASSERT (name != NULL);
6046 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
6047 sec = bfd_get_section_by_name (abfd,
6048 name + sizeof ".MIPS.events" - 1);
6049 else
6050 {
6051 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
6052 sizeof ".MIPS.post_rel" - 1) == 0);
6053 sec = bfd_get_section_by_name (abfd,
6054 (name
6055 + sizeof ".MIPS.post_rel" - 1));
6056 }
6057 BFD_ASSERT (sec != NULL);
6058 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
6059 break;
6060
6061 }
6062 }
6063 }
6064 \f
6065 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
6066 segments. */
6067
6068 int
6069 _bfd_mips_elf_additional_program_headers (abfd)
6070 bfd *abfd;
6071 {
6072 asection *s;
6073 int ret = 0;
6074
6075 /* See if we need a PT_MIPS_REGINFO segment. */
6076 s = bfd_get_section_by_name (abfd, ".reginfo");
6077 if (s && (s->flags & SEC_LOAD))
6078 ++ret;
6079
6080 /* See if we need a PT_MIPS_OPTIONS segment. */
6081 if (IRIX_COMPAT (abfd) == ict_irix6
6082 && bfd_get_section_by_name (abfd,
6083 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
6084 ++ret;
6085
6086 /* See if we need a PT_MIPS_RTPROC segment. */
6087 if (IRIX_COMPAT (abfd) == ict_irix5
6088 && bfd_get_section_by_name (abfd, ".dynamic")
6089 && bfd_get_section_by_name (abfd, ".mdebug"))
6090 ++ret;
6091
6092 return ret;
6093 }
6094
6095 /* Modify the segment map for an IRIX5 executable. */
6096
6097 boolean
6098 _bfd_mips_elf_modify_segment_map (abfd)
6099 bfd *abfd;
6100 {
6101 asection *s;
6102 struct elf_segment_map *m, **pm;
6103 bfd_size_type amt;
6104
6105 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
6106 segment. */
6107 s = bfd_get_section_by_name (abfd, ".reginfo");
6108 if (s != NULL && (s->flags & SEC_LOAD) != 0)
6109 {
6110 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
6111 if (m->p_type == PT_MIPS_REGINFO)
6112 break;
6113 if (m == NULL)
6114 {
6115 amt = sizeof *m;
6116 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
6117 if (m == NULL)
6118 return false;
6119
6120 m->p_type = PT_MIPS_REGINFO;
6121 m->count = 1;
6122 m->sections[0] = s;
6123
6124 /* We want to put it after the PHDR and INTERP segments. */
6125 pm = &elf_tdata (abfd)->segment_map;
6126 while (*pm != NULL
6127 && ((*pm)->p_type == PT_PHDR
6128 || (*pm)->p_type == PT_INTERP))
6129 pm = &(*pm)->next;
6130
6131 m->next = *pm;
6132 *pm = m;
6133 }
6134 }
6135
6136 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
6137 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
6138 PT_OPTIONS segment immediately following the program header
6139 table. */
6140 if (NEWABI_P (abfd))
6141 {
6142 for (s = abfd->sections; s; s = s->next)
6143 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
6144 break;
6145
6146 if (s)
6147 {
6148 struct elf_segment_map *options_segment;
6149
6150 /* Usually, there's a program header table. But, sometimes
6151 there's not (like when running the `ld' testsuite). So,
6152 if there's no program header table, we just put the
6153 options segment at the end. */
6154 for (pm = &elf_tdata (abfd)->segment_map;
6155 *pm != NULL;
6156 pm = &(*pm)->next)
6157 if ((*pm)->p_type == PT_PHDR)
6158 break;
6159
6160 amt = sizeof (struct elf_segment_map);
6161 options_segment = bfd_zalloc (abfd, amt);
6162 options_segment->next = *pm;
6163 options_segment->p_type = PT_MIPS_OPTIONS;
6164 options_segment->p_flags = PF_R;
6165 options_segment->p_flags_valid = true;
6166 options_segment->count = 1;
6167 options_segment->sections[0] = s;
6168 *pm = options_segment;
6169 }
6170 }
6171 else
6172 {
6173 if (IRIX_COMPAT (abfd) == ict_irix5)
6174 {
6175 /* If there are .dynamic and .mdebug sections, we make a room
6176 for the RTPROC header. FIXME: Rewrite without section names. */
6177 if (bfd_get_section_by_name (abfd, ".interp") == NULL
6178 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
6179 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
6180 {
6181 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
6182 if (m->p_type == PT_MIPS_RTPROC)
6183 break;
6184 if (m == NULL)
6185 {
6186 amt = sizeof *m;
6187 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
6188 if (m == NULL)
6189 return false;
6190
6191 m->p_type = PT_MIPS_RTPROC;
6192
6193 s = bfd_get_section_by_name (abfd, ".rtproc");
6194 if (s == NULL)
6195 {
6196 m->count = 0;
6197 m->p_flags = 0;
6198 m->p_flags_valid = 1;
6199 }
6200 else
6201 {
6202 m->count = 1;
6203 m->sections[0] = s;
6204 }
6205
6206 /* We want to put it after the DYNAMIC segment. */
6207 pm = &elf_tdata (abfd)->segment_map;
6208 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
6209 pm = &(*pm)->next;
6210 if (*pm != NULL)
6211 pm = &(*pm)->next;
6212
6213 m->next = *pm;
6214 *pm = m;
6215 }
6216 }
6217 }
6218 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
6219 .dynstr, .dynsym, and .hash sections, and everything in
6220 between. */
6221 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
6222 pm = &(*pm)->next)
6223 if ((*pm)->p_type == PT_DYNAMIC)
6224 break;
6225 m = *pm;
6226 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
6227 {
6228 /* For a normal mips executable the permissions for the PT_DYNAMIC
6229 segment are read, write and execute. We do that here since
6230 the code in elf.c sets only the read permission. This matters
6231 sometimes for the dynamic linker. */
6232 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
6233 {
6234 m->p_flags = PF_R | PF_W | PF_X;
6235 m->p_flags_valid = 1;
6236 }
6237 }
6238 if (m != NULL
6239 && m->count == 1 && strcmp (m->sections[0]->name, ".dynamic") == 0)
6240 {
6241 static const char *sec_names[] =
6242 {
6243 ".dynamic", ".dynstr", ".dynsym", ".hash"
6244 };
6245 bfd_vma low, high;
6246 unsigned int i, c;
6247 struct elf_segment_map *n;
6248
6249 low = 0xffffffff;
6250 high = 0;
6251 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
6252 {
6253 s = bfd_get_section_by_name (abfd, sec_names[i]);
6254 if (s != NULL && (s->flags & SEC_LOAD) != 0)
6255 {
6256 bfd_size_type sz;
6257
6258 if (low > s->vma)
6259 low = s->vma;
6260 sz = s->_cooked_size;
6261 if (sz == 0)
6262 sz = s->_raw_size;
6263 if (high < s->vma + sz)
6264 high = s->vma + sz;
6265 }
6266 }
6267
6268 c = 0;
6269 for (s = abfd->sections; s != NULL; s = s->next)
6270 if ((s->flags & SEC_LOAD) != 0
6271 && s->vma >= low
6272 && ((s->vma
6273 + (s->_cooked_size !=
6274 0 ? s->_cooked_size : s->_raw_size)) <= high))
6275 ++c;
6276
6277 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
6278 n = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
6279 if (n == NULL)
6280 return false;
6281 *n = *m;
6282 n->count = c;
6283
6284 i = 0;
6285 for (s = abfd->sections; s != NULL; s = s->next)
6286 {
6287 if ((s->flags & SEC_LOAD) != 0
6288 && s->vma >= low
6289 && ((s->vma
6290 + (s->_cooked_size != 0 ?
6291 s->_cooked_size : s->_raw_size)) <= high))
6292 {
6293 n->sections[i] = s;
6294 ++i;
6295 }
6296 }
6297
6298 *pm = n;
6299 }
6300 }
6301
6302 return true;
6303 }
6304 \f
6305 /* Return the section that should be marked against GC for a given
6306 relocation. */
6307
6308 asection *
6309 _bfd_mips_elf_gc_mark_hook (sec, info, rel, h, sym)
6310 asection *sec;
6311 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6312 Elf_Internal_Rela *rel;
6313 struct elf_link_hash_entry *h;
6314 Elf_Internal_Sym *sym;
6315 {
6316 /* ??? Do mips16 stub sections need to be handled special? */
6317
6318 if (h != NULL)
6319 {
6320 switch (ELF_R_TYPE (sec->owner, rel->r_info))
6321 {
6322 case R_MIPS_GNU_VTINHERIT:
6323 case R_MIPS_GNU_VTENTRY:
6324 break;
6325
6326 default:
6327 switch (h->root.type)
6328 {
6329 case bfd_link_hash_defined:
6330 case bfd_link_hash_defweak:
6331 return h->root.u.def.section;
6332
6333 case bfd_link_hash_common:
6334 return h->root.u.c.p->section;
6335
6336 default:
6337 break;
6338 }
6339 }
6340 }
6341 else
6342 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6343
6344 return NULL;
6345 }
6346
6347 /* Update the got entry reference counts for the section being removed. */
6348
6349 boolean
6350 _bfd_mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
6351 bfd *abfd ATTRIBUTE_UNUSED;
6352 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6353 asection *sec ATTRIBUTE_UNUSED;
6354 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
6355 {
6356 #if 0
6357 Elf_Internal_Shdr *symtab_hdr;
6358 struct elf_link_hash_entry **sym_hashes;
6359 bfd_signed_vma *local_got_refcounts;
6360 const Elf_Internal_Rela *rel, *relend;
6361 unsigned long r_symndx;
6362 struct elf_link_hash_entry *h;
6363
6364 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6365 sym_hashes = elf_sym_hashes (abfd);
6366 local_got_refcounts = elf_local_got_refcounts (abfd);
6367
6368 relend = relocs + sec->reloc_count;
6369 for (rel = relocs; rel < relend; rel++)
6370 switch (ELF_R_TYPE (abfd, rel->r_info))
6371 {
6372 case R_MIPS_GOT16:
6373 case R_MIPS_CALL16:
6374 case R_MIPS_CALL_HI16:
6375 case R_MIPS_CALL_LO16:
6376 case R_MIPS_GOT_HI16:
6377 case R_MIPS_GOT_LO16:
6378 case R_MIPS_GOT_DISP:
6379 case R_MIPS_GOT_PAGE:
6380 case R_MIPS_GOT_OFST:
6381 /* ??? It would seem that the existing MIPS code does no sort
6382 of reference counting or whatnot on its GOT and PLT entries,
6383 so it is not possible to garbage collect them at this time. */
6384 break;
6385
6386 default:
6387 break;
6388 }
6389 #endif
6390
6391 return true;
6392 }
6393 \f
6394 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
6395 hiding the old indirect symbol. Process additional relocation
6396 information. Also called for weakdefs, in which case we just let
6397 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
6398
6399 void
6400 _bfd_mips_elf_copy_indirect_symbol (bed, dir, ind)
6401 struct elf_backend_data *bed;
6402 struct elf_link_hash_entry *dir, *ind;
6403 {
6404 struct mips_elf_link_hash_entry *dirmips, *indmips;
6405
6406 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
6407
6408 if (ind->root.type != bfd_link_hash_indirect)
6409 return;
6410
6411 dirmips = (struct mips_elf_link_hash_entry *) dir;
6412 indmips = (struct mips_elf_link_hash_entry *) ind;
6413 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
6414 if (indmips->readonly_reloc)
6415 dirmips->readonly_reloc = true;
6416 if (dirmips->min_dyn_reloc_index == 0
6417 || (indmips->min_dyn_reloc_index != 0
6418 && indmips->min_dyn_reloc_index < dirmips->min_dyn_reloc_index))
6419 dirmips->min_dyn_reloc_index = indmips->min_dyn_reloc_index;
6420 if (indmips->no_fn_stub)
6421 dirmips->no_fn_stub = true;
6422 }
6423
6424 void
6425 _bfd_mips_elf_hide_symbol (info, entry, force_local)
6426 struct bfd_link_info *info;
6427 struct elf_link_hash_entry *entry;
6428 boolean force_local;
6429 {
6430 bfd *dynobj;
6431 asection *got;
6432 struct mips_got_info *g;
6433 struct mips_elf_link_hash_entry *h;
6434
6435 h = (struct mips_elf_link_hash_entry *) entry;
6436 if (h->forced_local)
6437 return;
6438 h->forced_local = true;
6439
6440 dynobj = elf_hash_table (info)->dynobj;
6441 got = bfd_get_section_by_name (dynobj, ".got");
6442 g = (struct mips_got_info *) elf_section_data (got)->tdata;
6443
6444 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
6445
6446 /* FIXME: Do we allocate too much GOT space here? */
6447 g->local_gotno++;
6448 got->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
6449 }
6450 \f
6451 #define PDR_SIZE 32
6452
6453 boolean
6454 _bfd_mips_elf_discard_info (abfd, cookie, info)
6455 bfd *abfd;
6456 struct elf_reloc_cookie *cookie;
6457 struct bfd_link_info *info;
6458 {
6459 asection *o;
6460 boolean ret = false;
6461 unsigned char *tdata;
6462 size_t i, skip;
6463
6464 o = bfd_get_section_by_name (abfd, ".pdr");
6465 if (! o)
6466 return false;
6467 if (o->_raw_size == 0)
6468 return false;
6469 if (o->_raw_size % PDR_SIZE != 0)
6470 return false;
6471 if (o->output_section != NULL
6472 && bfd_is_abs_section (o->output_section))
6473 return false;
6474
6475 tdata = bfd_zmalloc (o->_raw_size / PDR_SIZE);
6476 if (! tdata)
6477 return false;
6478
6479 cookie->rels = _bfd_elf32_link_read_relocs (abfd, o, (PTR) NULL,
6480 (Elf_Internal_Rela *) NULL,
6481 info->keep_memory);
6482 if (!cookie->rels)
6483 {
6484 free (tdata);
6485 return false;
6486 }
6487
6488 cookie->rel = cookie->rels;
6489 cookie->relend = cookie->rels + o->reloc_count;
6490
6491 for (i = 0, skip = 0; i < o->_raw_size; i ++)
6492 {
6493 if (_bfd_elf32_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
6494 {
6495 tdata[i] = 1;
6496 skip ++;
6497 }
6498 }
6499
6500 if (skip != 0)
6501 {
6502 elf_section_data (o)->tdata = tdata;
6503 o->_cooked_size = o->_raw_size - skip * PDR_SIZE;
6504 ret = true;
6505 }
6506 else
6507 free (tdata);
6508
6509 if (! info->keep_memory)
6510 free (cookie->rels);
6511
6512 return ret;
6513 }
6514
6515 boolean
6516 _bfd_mips_elf_ignore_discarded_relocs (sec)
6517 asection *sec;
6518 {
6519 if (strcmp (sec->name, ".pdr") == 0)
6520 return true;
6521 return false;
6522 }
6523
6524 boolean
6525 _bfd_mips_elf_write_section (output_bfd, sec, contents)
6526 bfd *output_bfd;
6527 asection *sec;
6528 bfd_byte *contents;
6529 {
6530 bfd_byte *to, *from, *end;
6531 int i;
6532
6533 if (strcmp (sec->name, ".pdr") != 0)
6534 return false;
6535
6536 if (elf_section_data (sec)->tdata == NULL)
6537 return false;
6538
6539 to = contents;
6540 end = contents + sec->_raw_size;
6541 for (from = contents, i = 0;
6542 from < end;
6543 from += PDR_SIZE, i++)
6544 {
6545 if (((unsigned char *) elf_section_data (sec)->tdata)[i] == 1)
6546 continue;
6547 if (to != from)
6548 memcpy (to, from, PDR_SIZE);
6549 to += PDR_SIZE;
6550 }
6551 bfd_set_section_contents (output_bfd, sec->output_section, contents,
6552 (file_ptr) sec->output_offset,
6553 sec->_cooked_size);
6554 return true;
6555 }
6556 \f
6557 /* MIPS ELF uses a special find_nearest_line routine in order the
6558 handle the ECOFF debugging information. */
6559
6560 struct mips_elf_find_line
6561 {
6562 struct ecoff_debug_info d;
6563 struct ecoff_find_line i;
6564 };
6565
6566 boolean
6567 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
6568 functionname_ptr, line_ptr)
6569 bfd *abfd;
6570 asection *section;
6571 asymbol **symbols;
6572 bfd_vma offset;
6573 const char **filename_ptr;
6574 const char **functionname_ptr;
6575 unsigned int *line_ptr;
6576 {
6577 asection *msec;
6578
6579 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6580 filename_ptr, functionname_ptr,
6581 line_ptr))
6582 return true;
6583
6584 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6585 filename_ptr, functionname_ptr,
6586 line_ptr,
6587 (unsigned) (ABI_64_P (abfd) ? 8 : 0),
6588 &elf_tdata (abfd)->dwarf2_find_line_info))
6589 return true;
6590
6591 msec = bfd_get_section_by_name (abfd, ".mdebug");
6592 if (msec != NULL)
6593 {
6594 flagword origflags;
6595 struct mips_elf_find_line *fi;
6596 const struct ecoff_debug_swap * const swap =
6597 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
6598
6599 /* If we are called during a link, mips_elf_final_link may have
6600 cleared the SEC_HAS_CONTENTS field. We force it back on here
6601 if appropriate (which it normally will be). */
6602 origflags = msec->flags;
6603 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
6604 msec->flags |= SEC_HAS_CONTENTS;
6605
6606 fi = elf_tdata (abfd)->find_line_info;
6607 if (fi == NULL)
6608 {
6609 bfd_size_type external_fdr_size;
6610 char *fraw_src;
6611 char *fraw_end;
6612 struct fdr *fdr_ptr;
6613 bfd_size_type amt = sizeof (struct mips_elf_find_line);
6614
6615 fi = (struct mips_elf_find_line *) bfd_zalloc (abfd, amt);
6616 if (fi == NULL)
6617 {
6618 msec->flags = origflags;
6619 return false;
6620 }
6621
6622 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
6623 {
6624 msec->flags = origflags;
6625 return false;
6626 }
6627
6628 /* Swap in the FDR information. */
6629 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
6630 fi->d.fdr = (struct fdr *) bfd_alloc (abfd, amt);
6631 if (fi->d.fdr == NULL)
6632 {
6633 msec->flags = origflags;
6634 return false;
6635 }
6636 external_fdr_size = swap->external_fdr_size;
6637 fdr_ptr = fi->d.fdr;
6638 fraw_src = (char *) fi->d.external_fdr;
6639 fraw_end = (fraw_src
6640 + fi->d.symbolic_header.ifdMax * external_fdr_size);
6641 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
6642 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
6643
6644 elf_tdata (abfd)->find_line_info = fi;
6645
6646 /* Note that we don't bother to ever free this information.
6647 find_nearest_line is either called all the time, as in
6648 objdump -l, so the information should be saved, or it is
6649 rarely called, as in ld error messages, so the memory
6650 wasted is unimportant. Still, it would probably be a
6651 good idea for free_cached_info to throw it away. */
6652 }
6653
6654 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
6655 &fi->i, filename_ptr, functionname_ptr,
6656 line_ptr))
6657 {
6658 msec->flags = origflags;
6659 return true;
6660 }
6661
6662 msec->flags = origflags;
6663 }
6664
6665 /* Fall back on the generic ELF find_nearest_line routine. */
6666
6667 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
6668 filename_ptr, functionname_ptr,
6669 line_ptr);
6670 }
6671 \f
6672 /* When are writing out the .options or .MIPS.options section,
6673 remember the bytes we are writing out, so that we can install the
6674 GP value in the section_processing routine. */
6675
6676 boolean
6677 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
6678 bfd *abfd;
6679 sec_ptr section;
6680 PTR location;
6681 file_ptr offset;
6682 bfd_size_type count;
6683 {
6684 if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
6685 {
6686 bfd_byte *c;
6687
6688 if (elf_section_data (section) == NULL)
6689 {
6690 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
6691 section->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
6692 if (elf_section_data (section) == NULL)
6693 return false;
6694 }
6695 c = (bfd_byte *) elf_section_data (section)->tdata;
6696 if (c == NULL)
6697 {
6698 bfd_size_type size;
6699
6700 if (section->_cooked_size != 0)
6701 size = section->_cooked_size;
6702 else
6703 size = section->_raw_size;
6704 c = (bfd_byte *) bfd_zalloc (abfd, size);
6705 if (c == NULL)
6706 return false;
6707 elf_section_data (section)->tdata = (PTR) c;
6708 }
6709
6710 memcpy (c + offset, location, (size_t) count);
6711 }
6712
6713 return _bfd_elf_set_section_contents (abfd, section, location, offset,
6714 count);
6715 }
6716
6717 /* This is almost identical to bfd_generic_get_... except that some
6718 MIPS relocations need to be handled specially. Sigh. */
6719
6720 bfd_byte *
6721 _bfd_elf_mips_get_relocated_section_contents (abfd, link_info, link_order,
6722 data, relocateable, symbols)
6723 bfd *abfd;
6724 struct bfd_link_info *link_info;
6725 struct bfd_link_order *link_order;
6726 bfd_byte *data;
6727 boolean relocateable;
6728 asymbol **symbols;
6729 {
6730 /* Get enough memory to hold the stuff */
6731 bfd *input_bfd = link_order->u.indirect.section->owner;
6732 asection *input_section = link_order->u.indirect.section;
6733
6734 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
6735 arelent **reloc_vector = NULL;
6736 long reloc_count;
6737
6738 if (reloc_size < 0)
6739 goto error_return;
6740
6741 reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
6742 if (reloc_vector == NULL && reloc_size != 0)
6743 goto error_return;
6744
6745 /* read in the section */
6746 if (!bfd_get_section_contents (input_bfd,
6747 input_section,
6748 (PTR) data,
6749 (file_ptr) 0,
6750 input_section->_raw_size))
6751 goto error_return;
6752
6753 /* We're not relaxing the section, so just copy the size info */
6754 input_section->_cooked_size = input_section->_raw_size;
6755 input_section->reloc_done = true;
6756
6757 reloc_count = bfd_canonicalize_reloc (input_bfd,
6758 input_section,
6759 reloc_vector,
6760 symbols);
6761 if (reloc_count < 0)
6762 goto error_return;
6763
6764 if (reloc_count > 0)
6765 {
6766 arelent **parent;
6767 /* for mips */
6768 int gp_found;
6769 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
6770
6771 {
6772 struct bfd_hash_entry *h;
6773 struct bfd_link_hash_entry *lh;
6774 /* Skip all this stuff if we aren't mixing formats. */
6775 if (abfd && input_bfd
6776 && abfd->xvec == input_bfd->xvec)
6777 lh = 0;
6778 else
6779 {
6780 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
6781 lh = (struct bfd_link_hash_entry *) h;
6782 }
6783 lookup:
6784 if (lh)
6785 {
6786 switch (lh->type)
6787 {
6788 case bfd_link_hash_undefined:
6789 case bfd_link_hash_undefweak:
6790 case bfd_link_hash_common:
6791 gp_found = 0;
6792 break;
6793 case bfd_link_hash_defined:
6794 case bfd_link_hash_defweak:
6795 gp_found = 1;
6796 gp = lh->u.def.value;
6797 break;
6798 case bfd_link_hash_indirect:
6799 case bfd_link_hash_warning:
6800 lh = lh->u.i.link;
6801 /* @@FIXME ignoring warning for now */
6802 goto lookup;
6803 case bfd_link_hash_new:
6804 default:
6805 abort ();
6806 }
6807 }
6808 else
6809 gp_found = 0;
6810 }
6811 /* end mips */
6812 for (parent = reloc_vector; *parent != (arelent *) NULL;
6813 parent++)
6814 {
6815 char *error_message = (char *) NULL;
6816 bfd_reloc_status_type r;
6817
6818 /* Specific to MIPS: Deal with relocation types that require
6819 knowing the gp of the output bfd. */
6820 asymbol *sym = *(*parent)->sym_ptr_ptr;
6821 if (bfd_is_abs_section (sym->section) && abfd)
6822 {
6823 /* The special_function wouldn't get called anyway. */
6824 }
6825 else if (!gp_found)
6826 {
6827 /* The gp isn't there; let the special function code
6828 fall over on its own. */
6829 }
6830 else if ((*parent)->howto->special_function
6831 == _bfd_mips_elf32_gprel16_reloc)
6832 {
6833 /* bypass special_function call */
6834 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
6835 input_section, relocateable,
6836 (PTR) data, gp);
6837 goto skip_bfd_perform_relocation;
6838 }
6839 /* end mips specific stuff */
6840
6841 r = bfd_perform_relocation (input_bfd,
6842 *parent,
6843 (PTR) data,
6844 input_section,
6845 relocateable ? abfd : (bfd *) NULL,
6846 &error_message);
6847 skip_bfd_perform_relocation:
6848
6849 if (relocateable)
6850 {
6851 asection *os = input_section->output_section;
6852
6853 /* A partial link, so keep the relocs */
6854 os->orelocation[os->reloc_count] = *parent;
6855 os->reloc_count++;
6856 }
6857
6858 if (r != bfd_reloc_ok)
6859 {
6860 switch (r)
6861 {
6862 case bfd_reloc_undefined:
6863 if (!((*link_info->callbacks->undefined_symbol)
6864 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
6865 input_bfd, input_section, (*parent)->address,
6866 true)))
6867 goto error_return;
6868 break;
6869 case bfd_reloc_dangerous:
6870 BFD_ASSERT (error_message != (char *) NULL);
6871 if (!((*link_info->callbacks->reloc_dangerous)
6872 (link_info, error_message, input_bfd, input_section,
6873 (*parent)->address)))
6874 goto error_return;
6875 break;
6876 case bfd_reloc_overflow:
6877 if (!((*link_info->callbacks->reloc_overflow)
6878 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
6879 (*parent)->howto->name, (*parent)->addend,
6880 input_bfd, input_section, (*parent)->address)))
6881 goto error_return;
6882 break;
6883 case bfd_reloc_outofrange:
6884 default:
6885 abort ();
6886 break;
6887 }
6888
6889 }
6890 }
6891 }
6892 if (reloc_vector != NULL)
6893 free (reloc_vector);
6894 return data;
6895
6896 error_return:
6897 if (reloc_vector != NULL)
6898 free (reloc_vector);
6899 return NULL;
6900 }
6901 \f
6902 /* Create a MIPS ELF linker hash table. */
6903
6904 struct bfd_link_hash_table *
6905 _bfd_mips_elf_link_hash_table_create (abfd)
6906 bfd *abfd;
6907 {
6908 struct mips_elf_link_hash_table *ret;
6909 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
6910
6911 ret = (struct mips_elf_link_hash_table *) bfd_malloc (amt);
6912 if (ret == (struct mips_elf_link_hash_table *) NULL)
6913 return NULL;
6914
6915 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
6916 mips_elf_link_hash_newfunc))
6917 {
6918 free (ret);
6919 return NULL;
6920 }
6921
6922 #if 0
6923 /* We no longer use this. */
6924 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
6925 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
6926 #endif
6927 ret->procedure_count = 0;
6928 ret->compact_rel_size = 0;
6929 ret->use_rld_obj_head = false;
6930 ret->rld_value = 0;
6931 ret->mips16_stubs_seen = false;
6932
6933 return &ret->root.root;
6934 }
6935 \f
6936 /* We need to use a special link routine to handle the .reginfo and
6937 the .mdebug sections. We need to merge all instances of these
6938 sections together, not write them all out sequentially. */
6939
6940 boolean
6941 _bfd_mips_elf_final_link (abfd, info)
6942 bfd *abfd;
6943 struct bfd_link_info *info;
6944 {
6945 asection **secpp;
6946 asection *o;
6947 struct bfd_link_order *p;
6948 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
6949 asection *rtproc_sec;
6950 Elf32_RegInfo reginfo;
6951 struct ecoff_debug_info debug;
6952 const struct ecoff_debug_swap *swap
6953 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
6954 HDRR *symhdr = &debug.symbolic_header;
6955 PTR mdebug_handle = NULL;
6956 asection *s;
6957 EXTR esym;
6958 unsigned int i;
6959 bfd_size_type amt;
6960
6961 static const char * const secname[] =
6962 {
6963 ".text", ".init", ".fini", ".data",
6964 ".rodata", ".sdata", ".sbss", ".bss"
6965 };
6966 static const int sc[] =
6967 {
6968 scText, scInit, scFini, scData,
6969 scRData, scSData, scSBss, scBss
6970 };
6971
6972 /* If all the things we linked together were PIC, but we're
6973 producing an executable (rather than a shared object), then the
6974 resulting file is CPIC (i.e., it calls PIC code.) */
6975 if (!info->shared
6976 && !info->relocateable
6977 && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
6978 {
6979 elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
6980 elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
6981 }
6982
6983 /* We'd carefully arranged the dynamic symbol indices, and then the
6984 generic size_dynamic_sections renumbered them out from under us.
6985 Rather than trying somehow to prevent the renumbering, just do
6986 the sort again. */
6987 if (elf_hash_table (info)->dynamic_sections_created)
6988 {
6989 bfd *dynobj;
6990 asection *got;
6991 struct mips_got_info *g;
6992
6993 /* When we resort, we must tell mips_elf_sort_hash_table what
6994 the lowest index it may use is. That's the number of section
6995 symbols we're going to add. The generic ELF linker only
6996 adds these symbols when building a shared object. Note that
6997 we count the sections after (possibly) removing the .options
6998 section above. */
6999 if (! mips_elf_sort_hash_table (info, (info->shared
7000 ? bfd_count_sections (abfd) + 1
7001 : 1)))
7002 return false;
7003
7004 /* Make sure we didn't grow the global .got region. */
7005 dynobj = elf_hash_table (info)->dynobj;
7006 got = bfd_get_section_by_name (dynobj, ".got");
7007 g = (struct mips_got_info *) elf_section_data (got)->tdata;
7008
7009 if (g->global_gotsym != NULL)
7010 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
7011 - g->global_gotsym->dynindx)
7012 <= g->global_gotno);
7013 }
7014
7015 #if 0
7016 /* We want to set the GP value for ld -r. */
7017 /* On IRIX5, we omit the .options section. On IRIX6, however, we
7018 include it, even though we don't process it quite right. (Some
7019 entries are supposed to be merged.) Empirically, we seem to be
7020 better off including it then not. */
7021 if (IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
7022 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
7023 {
7024 if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
7025 {
7026 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
7027 if (p->type == bfd_indirect_link_order)
7028 p->u.indirect.section->flags &= ~SEC_HAS_CONTENTS;
7029 (*secpp)->link_order_head = NULL;
7030 bfd_section_list_remove (abfd, secpp);
7031 --abfd->section_count;
7032
7033 break;
7034 }
7035 }
7036
7037 /* We include .MIPS.options, even though we don't process it quite right.
7038 (Some entries are supposed to be merged.) At IRIX6 empirically we seem
7039 to be better off including it than not. */
7040 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
7041 {
7042 if (strcmp ((*secpp)->name, ".MIPS.options") == 0)
7043 {
7044 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
7045 if (p->type == bfd_indirect_link_order)
7046 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
7047 (*secpp)->link_order_head = NULL;
7048 bfd_section_list_remove (abfd, secpp);
7049 --abfd->section_count;
7050
7051 break;
7052 }
7053 }
7054 #endif
7055
7056 /* Get a value for the GP register. */
7057 if (elf_gp (abfd) == 0)
7058 {
7059 struct bfd_link_hash_entry *h;
7060
7061 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
7062 if (h != (struct bfd_link_hash_entry *) NULL
7063 && h->type == bfd_link_hash_defined)
7064 elf_gp (abfd) = (h->u.def.value
7065 + h->u.def.section->output_section->vma
7066 + h->u.def.section->output_offset);
7067 else if (info->relocateable)
7068 {
7069 bfd_vma lo = MINUS_ONE;
7070
7071 /* Find the GP-relative section with the lowest offset. */
7072 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
7073 if (o->vma < lo
7074 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
7075 lo = o->vma;
7076
7077 /* And calculate GP relative to that. */
7078 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
7079 }
7080 else
7081 {
7082 /* If the relocate_section function needs to do a reloc
7083 involving the GP value, it should make a reloc_dangerous
7084 callback to warn that GP is not defined. */
7085 }
7086 }
7087
7088 /* Go through the sections and collect the .reginfo and .mdebug
7089 information. */
7090 reginfo_sec = NULL;
7091 mdebug_sec = NULL;
7092 gptab_data_sec = NULL;
7093 gptab_bss_sec = NULL;
7094 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
7095 {
7096 if (strcmp (o->name, ".reginfo") == 0)
7097 {
7098 memset (&reginfo, 0, sizeof reginfo);
7099
7100 /* We have found the .reginfo section in the output file.
7101 Look through all the link_orders comprising it and merge
7102 the information together. */
7103 for (p = o->link_order_head;
7104 p != (struct bfd_link_order *) NULL;
7105 p = p->next)
7106 {
7107 asection *input_section;
7108 bfd *input_bfd;
7109 Elf32_External_RegInfo ext;
7110 Elf32_RegInfo sub;
7111
7112 if (p->type != bfd_indirect_link_order)
7113 {
7114 if (p->type == bfd_data_link_order)
7115 continue;
7116 abort ();
7117 }
7118
7119 input_section = p->u.indirect.section;
7120 input_bfd = input_section->owner;
7121
7122 /* The linker emulation code has probably clobbered the
7123 size to be zero bytes. */
7124 if (input_section->_raw_size == 0)
7125 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
7126
7127 if (! bfd_get_section_contents (input_bfd, input_section,
7128 (PTR) &ext,
7129 (file_ptr) 0,
7130 (bfd_size_type) sizeof ext))
7131 return false;
7132
7133 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
7134
7135 reginfo.ri_gprmask |= sub.ri_gprmask;
7136 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
7137 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
7138 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
7139 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
7140
7141 /* ri_gp_value is set by the function
7142 mips_elf32_section_processing when the section is
7143 finally written out. */
7144
7145 /* Hack: reset the SEC_HAS_CONTENTS flag so that
7146 elf_link_input_bfd ignores this section. */
7147 input_section->flags &= ~SEC_HAS_CONTENTS;
7148 }
7149
7150 /* Size has been set in _bfd_mips_elf_always_size_sections. */
7151 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
7152
7153 /* Skip this section later on (I don't think this currently
7154 matters, but someday it might). */
7155 o->link_order_head = (struct bfd_link_order *) NULL;
7156
7157 reginfo_sec = o;
7158 }
7159
7160 if (strcmp (o->name, ".mdebug") == 0)
7161 {
7162 struct extsym_info einfo;
7163 bfd_vma last;
7164
7165 /* We have found the .mdebug section in the output file.
7166 Look through all the link_orders comprising it and merge
7167 the information together. */
7168 symhdr->magic = swap->sym_magic;
7169 /* FIXME: What should the version stamp be? */
7170 symhdr->vstamp = 0;
7171 symhdr->ilineMax = 0;
7172 symhdr->cbLine = 0;
7173 symhdr->idnMax = 0;
7174 symhdr->ipdMax = 0;
7175 symhdr->isymMax = 0;
7176 symhdr->ioptMax = 0;
7177 symhdr->iauxMax = 0;
7178 symhdr->issMax = 0;
7179 symhdr->issExtMax = 0;
7180 symhdr->ifdMax = 0;
7181 symhdr->crfd = 0;
7182 symhdr->iextMax = 0;
7183
7184 /* We accumulate the debugging information itself in the
7185 debug_info structure. */
7186 debug.line = NULL;
7187 debug.external_dnr = NULL;
7188 debug.external_pdr = NULL;
7189 debug.external_sym = NULL;
7190 debug.external_opt = NULL;
7191 debug.external_aux = NULL;
7192 debug.ss = NULL;
7193 debug.ssext = debug.ssext_end = NULL;
7194 debug.external_fdr = NULL;
7195 debug.external_rfd = NULL;
7196 debug.external_ext = debug.external_ext_end = NULL;
7197
7198 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
7199 if (mdebug_handle == (PTR) NULL)
7200 return false;
7201
7202 esym.jmptbl = 0;
7203 esym.cobol_main = 0;
7204 esym.weakext = 0;
7205 esym.reserved = 0;
7206 esym.ifd = ifdNil;
7207 esym.asym.iss = issNil;
7208 esym.asym.st = stLocal;
7209 esym.asym.reserved = 0;
7210 esym.asym.index = indexNil;
7211 last = 0;
7212 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
7213 {
7214 esym.asym.sc = sc[i];
7215 s = bfd_get_section_by_name (abfd, secname[i]);
7216 if (s != NULL)
7217 {
7218 esym.asym.value = s->vma;
7219 last = s->vma + s->_raw_size;
7220 }
7221 else
7222 esym.asym.value = last;
7223 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
7224 secname[i], &esym))
7225 return false;
7226 }
7227
7228 for (p = o->link_order_head;
7229 p != (struct bfd_link_order *) NULL;
7230 p = p->next)
7231 {
7232 asection *input_section;
7233 bfd *input_bfd;
7234 const struct ecoff_debug_swap *input_swap;
7235 struct ecoff_debug_info input_debug;
7236 char *eraw_src;
7237 char *eraw_end;
7238
7239 if (p->type != bfd_indirect_link_order)
7240 {
7241 if (p->type == bfd_data_link_order)
7242 continue;
7243 abort ();
7244 }
7245
7246 input_section = p->u.indirect.section;
7247 input_bfd = input_section->owner;
7248
7249 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
7250 || (get_elf_backend_data (input_bfd)
7251 ->elf_backend_ecoff_debug_swap) == NULL)
7252 {
7253 /* I don't know what a non MIPS ELF bfd would be
7254 doing with a .mdebug section, but I don't really
7255 want to deal with it. */
7256 continue;
7257 }
7258
7259 input_swap = (get_elf_backend_data (input_bfd)
7260 ->elf_backend_ecoff_debug_swap);
7261
7262 BFD_ASSERT (p->size == input_section->_raw_size);
7263
7264 /* The ECOFF linking code expects that we have already
7265 read in the debugging information and set up an
7266 ecoff_debug_info structure, so we do that now. */
7267 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
7268 &input_debug))
7269 return false;
7270
7271 if (! (bfd_ecoff_debug_accumulate
7272 (mdebug_handle, abfd, &debug, swap, input_bfd,
7273 &input_debug, input_swap, info)))
7274 return false;
7275
7276 /* Loop through the external symbols. For each one with
7277 interesting information, try to find the symbol in
7278 the linker global hash table and save the information
7279 for the output external symbols. */
7280 eraw_src = input_debug.external_ext;
7281 eraw_end = (eraw_src
7282 + (input_debug.symbolic_header.iextMax
7283 * input_swap->external_ext_size));
7284 for (;
7285 eraw_src < eraw_end;
7286 eraw_src += input_swap->external_ext_size)
7287 {
7288 EXTR ext;
7289 const char *name;
7290 struct mips_elf_link_hash_entry *h;
7291
7292 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
7293 if (ext.asym.sc == scNil
7294 || ext.asym.sc == scUndefined
7295 || ext.asym.sc == scSUndefined)
7296 continue;
7297
7298 name = input_debug.ssext + ext.asym.iss;
7299 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
7300 name, false, false, true);
7301 if (h == NULL || h->esym.ifd != -2)
7302 continue;
7303
7304 if (ext.ifd != -1)
7305 {
7306 BFD_ASSERT (ext.ifd
7307 < input_debug.symbolic_header.ifdMax);
7308 ext.ifd = input_debug.ifdmap[ext.ifd];
7309 }
7310
7311 h->esym = ext;
7312 }
7313
7314 /* Free up the information we just read. */
7315 free (input_debug.line);
7316 free (input_debug.external_dnr);
7317 free (input_debug.external_pdr);
7318 free (input_debug.external_sym);
7319 free (input_debug.external_opt);
7320 free (input_debug.external_aux);
7321 free (input_debug.ss);
7322 free (input_debug.ssext);
7323 free (input_debug.external_fdr);
7324 free (input_debug.external_rfd);
7325 free (input_debug.external_ext);
7326
7327 /* Hack: reset the SEC_HAS_CONTENTS flag so that
7328 elf_link_input_bfd ignores this section. */
7329 input_section->flags &= ~SEC_HAS_CONTENTS;
7330 }
7331
7332 if (SGI_COMPAT (abfd) && info->shared)
7333 {
7334 /* Create .rtproc section. */
7335 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
7336 if (rtproc_sec == NULL)
7337 {
7338 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
7339 | SEC_LINKER_CREATED | SEC_READONLY);
7340
7341 rtproc_sec = bfd_make_section (abfd, ".rtproc");
7342 if (rtproc_sec == NULL
7343 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
7344 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
7345 return false;
7346 }
7347
7348 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
7349 info, rtproc_sec,
7350 &debug))
7351 return false;
7352 }
7353
7354 /* Build the external symbol information. */
7355 einfo.abfd = abfd;
7356 einfo.info = info;
7357 einfo.debug = &debug;
7358 einfo.swap = swap;
7359 einfo.failed = false;
7360 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7361 mips_elf_output_extsym,
7362 (PTR) &einfo);
7363 if (einfo.failed)
7364 return false;
7365
7366 /* Set the size of the .mdebug section. */
7367 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
7368
7369 /* Skip this section later on (I don't think this currently
7370 matters, but someday it might). */
7371 o->link_order_head = (struct bfd_link_order *) NULL;
7372
7373 mdebug_sec = o;
7374 }
7375
7376 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
7377 {
7378 const char *subname;
7379 unsigned int c;
7380 Elf32_gptab *tab;
7381 Elf32_External_gptab *ext_tab;
7382 unsigned int j;
7383
7384 /* The .gptab.sdata and .gptab.sbss sections hold
7385 information describing how the small data area would
7386 change depending upon the -G switch. These sections
7387 not used in executables files. */
7388 if (! info->relocateable)
7389 {
7390 for (p = o->link_order_head;
7391 p != (struct bfd_link_order *) NULL;
7392 p = p->next)
7393 {
7394 asection *input_section;
7395
7396 if (p->type != bfd_indirect_link_order)
7397 {
7398 if (p->type == bfd_data_link_order)
7399 continue;
7400 abort ();
7401 }
7402
7403 input_section = p->u.indirect.section;
7404
7405 /* Hack: reset the SEC_HAS_CONTENTS flag so that
7406 elf_link_input_bfd ignores this section. */
7407 input_section->flags &= ~SEC_HAS_CONTENTS;
7408 }
7409
7410 /* Skip this section later on (I don't think this
7411 currently matters, but someday it might). */
7412 o->link_order_head = (struct bfd_link_order *) NULL;
7413
7414 /* Really remove the section. */
7415 for (secpp = &abfd->sections;
7416 *secpp != o;
7417 secpp = &(*secpp)->next)
7418 ;
7419 bfd_section_list_remove (abfd, secpp);
7420 --abfd->section_count;
7421
7422 continue;
7423 }
7424
7425 /* There is one gptab for initialized data, and one for
7426 uninitialized data. */
7427 if (strcmp (o->name, ".gptab.sdata") == 0)
7428 gptab_data_sec = o;
7429 else if (strcmp (o->name, ".gptab.sbss") == 0)
7430 gptab_bss_sec = o;
7431 else
7432 {
7433 (*_bfd_error_handler)
7434 (_("%s: illegal section name `%s'"),
7435 bfd_get_filename (abfd), o->name);
7436 bfd_set_error (bfd_error_nonrepresentable_section);
7437 return false;
7438 }
7439
7440 /* The linker script always combines .gptab.data and
7441 .gptab.sdata into .gptab.sdata, and likewise for
7442 .gptab.bss and .gptab.sbss. It is possible that there is
7443 no .sdata or .sbss section in the output file, in which
7444 case we must change the name of the output section. */
7445 subname = o->name + sizeof ".gptab" - 1;
7446 if (bfd_get_section_by_name (abfd, subname) == NULL)
7447 {
7448 if (o == gptab_data_sec)
7449 o->name = ".gptab.data";
7450 else
7451 o->name = ".gptab.bss";
7452 subname = o->name + sizeof ".gptab" - 1;
7453 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
7454 }
7455
7456 /* Set up the first entry. */
7457 c = 1;
7458 amt = c * sizeof (Elf32_gptab);
7459 tab = (Elf32_gptab *) bfd_malloc (amt);
7460 if (tab == NULL)
7461 return false;
7462 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
7463 tab[0].gt_header.gt_unused = 0;
7464
7465 /* Combine the input sections. */
7466 for (p = o->link_order_head;
7467 p != (struct bfd_link_order *) NULL;
7468 p = p->next)
7469 {
7470 asection *input_section;
7471 bfd *input_bfd;
7472 bfd_size_type size;
7473 unsigned long last;
7474 bfd_size_type gpentry;
7475
7476 if (p->type != bfd_indirect_link_order)
7477 {
7478 if (p->type == bfd_data_link_order)
7479 continue;
7480 abort ();
7481 }
7482
7483 input_section = p->u.indirect.section;
7484 input_bfd = input_section->owner;
7485
7486 /* Combine the gptab entries for this input section one
7487 by one. We know that the input gptab entries are
7488 sorted by ascending -G value. */
7489 size = bfd_section_size (input_bfd, input_section);
7490 last = 0;
7491 for (gpentry = sizeof (Elf32_External_gptab);
7492 gpentry < size;
7493 gpentry += sizeof (Elf32_External_gptab))
7494 {
7495 Elf32_External_gptab ext_gptab;
7496 Elf32_gptab int_gptab;
7497 unsigned long val;
7498 unsigned long add;
7499 boolean exact;
7500 unsigned int look;
7501
7502 if (! (bfd_get_section_contents
7503 (input_bfd, input_section, (PTR) &ext_gptab,
7504 (file_ptr) gpentry,
7505 (bfd_size_type) sizeof (Elf32_External_gptab))))
7506 {
7507 free (tab);
7508 return false;
7509 }
7510
7511 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
7512 &int_gptab);
7513 val = int_gptab.gt_entry.gt_g_value;
7514 add = int_gptab.gt_entry.gt_bytes - last;
7515
7516 exact = false;
7517 for (look = 1; look < c; look++)
7518 {
7519 if (tab[look].gt_entry.gt_g_value >= val)
7520 tab[look].gt_entry.gt_bytes += add;
7521
7522 if (tab[look].gt_entry.gt_g_value == val)
7523 exact = true;
7524 }
7525
7526 if (! exact)
7527 {
7528 Elf32_gptab *new_tab;
7529 unsigned int max;
7530
7531 /* We need a new table entry. */
7532 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
7533 new_tab = (Elf32_gptab *) bfd_realloc ((PTR) tab, amt);
7534 if (new_tab == NULL)
7535 {
7536 free (tab);
7537 return false;
7538 }
7539 tab = new_tab;
7540 tab[c].gt_entry.gt_g_value = val;
7541 tab[c].gt_entry.gt_bytes = add;
7542
7543 /* Merge in the size for the next smallest -G
7544 value, since that will be implied by this new
7545 value. */
7546 max = 0;
7547 for (look = 1; look < c; look++)
7548 {
7549 if (tab[look].gt_entry.gt_g_value < val
7550 && (max == 0
7551 || (tab[look].gt_entry.gt_g_value
7552 > tab[max].gt_entry.gt_g_value)))
7553 max = look;
7554 }
7555 if (max != 0)
7556 tab[c].gt_entry.gt_bytes +=
7557 tab[max].gt_entry.gt_bytes;
7558
7559 ++c;
7560 }
7561
7562 last = int_gptab.gt_entry.gt_bytes;
7563 }
7564
7565 /* Hack: reset the SEC_HAS_CONTENTS flag so that
7566 elf_link_input_bfd ignores this section. */
7567 input_section->flags &= ~SEC_HAS_CONTENTS;
7568 }
7569
7570 /* The table must be sorted by -G value. */
7571 if (c > 2)
7572 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
7573
7574 /* Swap out the table. */
7575 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
7576 ext_tab = (Elf32_External_gptab *) bfd_alloc (abfd, amt);
7577 if (ext_tab == NULL)
7578 {
7579 free (tab);
7580 return false;
7581 }
7582
7583 for (j = 0; j < c; j++)
7584 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
7585 free (tab);
7586
7587 o->_raw_size = c * sizeof (Elf32_External_gptab);
7588 o->contents = (bfd_byte *) ext_tab;
7589
7590 /* Skip this section later on (I don't think this currently
7591 matters, but someday it might). */
7592 o->link_order_head = (struct bfd_link_order *) NULL;
7593 }
7594 }
7595
7596 /* Invoke the regular ELF backend linker to do all the work. */
7597 if (ABI_64_P (abfd))
7598 {
7599 #ifdef BFD64
7600 if (!bfd_elf64_bfd_final_link (abfd, info))
7601 return false;
7602 #else
7603 abort ();
7604 return false;
7605 #endif /* BFD64 */
7606 }
7607 else if (!bfd_elf32_bfd_final_link (abfd, info))
7608 return false;
7609
7610 /* Now write out the computed sections. */
7611
7612 if (reginfo_sec != (asection *) NULL)
7613 {
7614 Elf32_External_RegInfo ext;
7615
7616 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
7617 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
7618 (file_ptr) 0,
7619 (bfd_size_type) sizeof ext))
7620 return false;
7621 }
7622
7623 if (mdebug_sec != (asection *) NULL)
7624 {
7625 BFD_ASSERT (abfd->output_has_begun);
7626 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
7627 swap, info,
7628 mdebug_sec->filepos))
7629 return false;
7630
7631 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
7632 }
7633
7634 if (gptab_data_sec != (asection *) NULL)
7635 {
7636 if (! bfd_set_section_contents (abfd, gptab_data_sec,
7637 gptab_data_sec->contents,
7638 (file_ptr) 0,
7639 gptab_data_sec->_raw_size))
7640 return false;
7641 }
7642
7643 if (gptab_bss_sec != (asection *) NULL)
7644 {
7645 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
7646 gptab_bss_sec->contents,
7647 (file_ptr) 0,
7648 gptab_bss_sec->_raw_size))
7649 return false;
7650 }
7651
7652 if (SGI_COMPAT (abfd))
7653 {
7654 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
7655 if (rtproc_sec != NULL)
7656 {
7657 if (! bfd_set_section_contents (abfd, rtproc_sec,
7658 rtproc_sec->contents,
7659 (file_ptr) 0,
7660 rtproc_sec->_raw_size))
7661 return false;
7662 }
7663 }
7664
7665 return true;
7666 }
7667 \f
7668 /* Return true if machine EXTENSION is an extension of machine BASE,
7669 meaning that it should be safe to link code for the two machines
7670 and set the output machine to EXTENSION. EXTENSION and BASE are
7671 both submasks of EF_MIPS_MACH. */
7672
7673 static boolean
7674 _bfd_mips_elf_mach_extends_p (base, extension)
7675 flagword base, extension;
7676 {
7677 /* The vr5500 ISA is an extension of the core vr5400 ISA, but doesn't
7678 include the multimedia stuff. It seems better to allow vr5400
7679 and vr5500 code to be merged anyway, since many libraries will
7680 just use the core ISA. Perhaps we could add some sort of ASE
7681 flag if this ever proves a problem. */
7682 return (base == 0
7683 || (base == E_MIPS_MACH_5400 && extension == E_MIPS_MACH_5500)
7684 || (base == E_MIPS_MACH_4100 && extension == E_MIPS_MACH_4111)
7685 || (base == E_MIPS_MACH_4100 && extension == E_MIPS_MACH_4120));
7686 }
7687
7688 /* Merge backend specific data from an object file to the output
7689 object file when linking. */
7690
7691 boolean
7692 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
7693 bfd *ibfd;
7694 bfd *obfd;
7695 {
7696 flagword old_flags;
7697 flagword new_flags;
7698 boolean ok;
7699 boolean null_input_bfd = true;
7700 asection *sec;
7701
7702 /* Check if we have the same endianess */
7703 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
7704 return false;
7705
7706 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7707 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7708 return true;
7709
7710 new_flags = elf_elfheader (ibfd)->e_flags;
7711 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
7712 old_flags = elf_elfheader (obfd)->e_flags;
7713
7714 if (! elf_flags_init (obfd))
7715 {
7716 elf_flags_init (obfd) = true;
7717 elf_elfheader (obfd)->e_flags = new_flags;
7718 elf_elfheader (obfd)->e_ident[EI_CLASS]
7719 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
7720
7721 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
7722 && bfd_get_arch_info (obfd)->the_default)
7723 {
7724 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
7725 bfd_get_mach (ibfd)))
7726 return false;
7727 }
7728
7729 return true;
7730 }
7731
7732 /* Check flag compatibility. */
7733
7734 new_flags &= ~EF_MIPS_NOREORDER;
7735 old_flags &= ~EF_MIPS_NOREORDER;
7736
7737 if (new_flags == old_flags)
7738 return true;
7739
7740 /* Check to see if the input BFD actually contains any sections.
7741 If not, its flags may not have been initialised either, but it cannot
7742 actually cause any incompatibility. */
7743 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7744 {
7745 /* Ignore synthetic sections and empty .text, .data and .bss sections
7746 which are automatically generated by gas. */
7747 if (strcmp (sec->name, ".reginfo")
7748 && strcmp (sec->name, ".mdebug")
7749 && ((!strcmp (sec->name, ".text")
7750 || !strcmp (sec->name, ".data")
7751 || !strcmp (sec->name, ".bss"))
7752 && sec->_raw_size != 0))
7753 {
7754 null_input_bfd = false;
7755 break;
7756 }
7757 }
7758 if (null_input_bfd)
7759 return true;
7760
7761 ok = true;
7762
7763 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
7764 {
7765 new_flags &= ~EF_MIPS_PIC;
7766 old_flags &= ~EF_MIPS_PIC;
7767 (*_bfd_error_handler)
7768 (_("%s: linking PIC files with non-PIC files"),
7769 bfd_archive_filename (ibfd));
7770 ok = false;
7771 }
7772
7773 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
7774 {
7775 new_flags &= ~EF_MIPS_CPIC;
7776 old_flags &= ~EF_MIPS_CPIC;
7777 (*_bfd_error_handler)
7778 (_("%s: linking abicalls files with non-abicalls files"),
7779 bfd_archive_filename (ibfd));
7780 ok = false;
7781 }
7782
7783 /* Compare the ISA's. */
7784 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
7785 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
7786 {
7787 int new_mach = new_flags & EF_MIPS_MACH;
7788 int old_mach = old_flags & EF_MIPS_MACH;
7789 int new_isa = elf_mips_isa (new_flags);
7790 int old_isa = elf_mips_isa (old_flags);
7791
7792 /* If either has no machine specified, just compare the general isa's.
7793 Some combinations of machines are ok, if the isa's match. */
7794 if (new_mach == old_mach
7795 || _bfd_mips_elf_mach_extends_p (new_mach, old_mach)
7796 || _bfd_mips_elf_mach_extends_p (old_mach, new_mach))
7797 {
7798 /* Don't warn about mixing code using 32-bit ISAs, or mixing code
7799 using 64-bit ISAs. They will normally use the same data sizes
7800 and calling conventions. */
7801
7802 if (( (new_isa == 1 || new_isa == 2 || new_isa == 32)
7803 ^ (old_isa == 1 || old_isa == 2 || old_isa == 32)) != 0)
7804 {
7805 (*_bfd_error_handler)
7806 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
7807 bfd_archive_filename (ibfd), new_isa, old_isa);
7808 ok = false;
7809 }
7810 else
7811 {
7812 /* Do we need to update the mach field? */
7813 if (_bfd_mips_elf_mach_extends_p (old_mach, new_mach))
7814 {
7815 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_MACH;
7816 elf_elfheader (obfd)->e_flags |= new_mach;
7817 }
7818
7819 /* Do we need to update the ISA field? */
7820 if (new_isa > old_isa)
7821 {
7822 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_ARCH;
7823 elf_elfheader (obfd)->e_flags
7824 |= new_flags & EF_MIPS_ARCH;
7825 }
7826 }
7827 }
7828 else
7829 {
7830 (*_bfd_error_handler)
7831 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
7832 bfd_archive_filename (ibfd),
7833 _bfd_elf_mips_mach (new_flags),
7834 _bfd_elf_mips_mach (old_flags));
7835 ok = false;
7836 }
7837
7838 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
7839 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
7840 }
7841
7842 /* Compare ABI's. The 64-bit ABI does not use EF_MIPS_ABI. But, it
7843 does set EI_CLASS differently from any 32-bit ABI. */
7844 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
7845 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7846 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
7847 {
7848 /* Only error if both are set (to different values). */
7849 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
7850 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7851 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
7852 {
7853 (*_bfd_error_handler)
7854 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
7855 bfd_archive_filename (ibfd),
7856 elf_mips_abi_name (ibfd),
7857 elf_mips_abi_name (obfd));
7858 ok = false;
7859 }
7860 new_flags &= ~EF_MIPS_ABI;
7861 old_flags &= ~EF_MIPS_ABI;
7862 }
7863
7864 /* For now, allow arbitrary mixing of ASEs (retain the union). */
7865 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
7866 {
7867 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
7868
7869 new_flags &= ~ EF_MIPS_ARCH_ASE;
7870 old_flags &= ~ EF_MIPS_ARCH_ASE;
7871 }
7872
7873 /* Warn about any other mismatches */
7874 if (new_flags != old_flags)
7875 {
7876 (*_bfd_error_handler)
7877 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
7878 bfd_archive_filename (ibfd), (unsigned long) new_flags,
7879 (unsigned long) old_flags);
7880 ok = false;
7881 }
7882
7883 if (! ok)
7884 {
7885 bfd_set_error (bfd_error_bad_value);
7886 return false;
7887 }
7888
7889 return true;
7890 }
7891
7892 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
7893
7894 boolean
7895 _bfd_mips_elf_set_private_flags (abfd, flags)
7896 bfd *abfd;
7897 flagword flags;
7898 {
7899 BFD_ASSERT (!elf_flags_init (abfd)
7900 || elf_elfheader (abfd)->e_flags == flags);
7901
7902 elf_elfheader (abfd)->e_flags = flags;
7903 elf_flags_init (abfd) = true;
7904 return true;
7905 }
7906
7907 boolean
7908 _bfd_mips_elf_print_private_bfd_data (abfd, ptr)
7909 bfd *abfd;
7910 PTR ptr;
7911 {
7912 FILE *file = (FILE *) ptr;
7913
7914 BFD_ASSERT (abfd != NULL && ptr != NULL);
7915
7916 /* Print normal ELF private data. */
7917 _bfd_elf_print_private_bfd_data (abfd, ptr);
7918
7919 /* xgettext:c-format */
7920 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
7921
7922 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
7923 fprintf (file, _(" [abi=O32]"));
7924 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
7925 fprintf (file, _(" [abi=O64]"));
7926 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
7927 fprintf (file, _(" [abi=EABI32]"));
7928 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7929 fprintf (file, _(" [abi=EABI64]"));
7930 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
7931 fprintf (file, _(" [abi unknown]"));
7932 else if (ABI_N32_P (abfd))
7933 fprintf (file, _(" [abi=N32]"));
7934 else if (ABI_64_P (abfd))
7935 fprintf (file, _(" [abi=64]"));
7936 else
7937 fprintf (file, _(" [no abi set]"));
7938
7939 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
7940 fprintf (file, _(" [mips1]"));
7941 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
7942 fprintf (file, _(" [mips2]"));
7943 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
7944 fprintf (file, _(" [mips3]"));
7945 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
7946 fprintf (file, _(" [mips4]"));
7947 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
7948 fprintf (file, _(" [mips5]"));
7949 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
7950 fprintf (file, _(" [mips32]"));
7951 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
7952 fprintf (file, _(" [mips64]"));
7953 else
7954 fprintf (file, _(" [unknown ISA]"));
7955
7956 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
7957 fprintf (file, _(" [mdmx]"));
7958
7959 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
7960 fprintf (file, _(" [mips16]"));
7961
7962 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
7963 fprintf (file, _(" [32bitmode]"));
7964 else
7965 fprintf (file, _(" [not 32bitmode]"));
7966
7967 fputc ('\n', file);
7968
7969 return true;
7970 }
This page took 0.208425 seconds and 5 git commands to generate.