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