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